logo
const class

compiler::FTypeRef

sys::Obj
  compiler::FTypeRef
   1  //
   2  // Copyright (c) 2006, Brian Frank and Andy Frank
   3  // Licensed under the Academic Free License version 3.0
   4  //
   5  // History:
   6  //   30 Jan 06  Brian Frank  Creation
   7  //   19 Aug 06  Brian Frank  Ported from Java to Fan
   8  //
   9  
  10  **
  11  ** FTypeRef stores a typeRef structure used to reference type signatures.
  12  **
  13  const class FTypeRef
  14  {
  15  
  16    new make(Int podName, Int typeName, Str sig)
  17    {
  18      this.podName  = podName
  19      this.typeName = typeName
  20      this.sig      = sig
  21      this.hashcode = (podName << 17) ^ (typeName) ^ (sig.hash)
  22    }
  23  
  24    override Int hash() { return hashcode }
  25  
  26    override Bool equals(Obj obj)
  27    {
  28      x := (FTypeRef)obj
  29      return podName === x.podName && typeName === x.typeName && sig == x.sig
  30    }
  31  
  32    Bool isGenericInstance() { return sig.size > 0 }
  33  
  34    Str signature(FPod pod)
  35    {
  36      if (isGenericInstance) return sig
  37      return pod.n(podName) + "::" + pod.n(typeName)
  38    }
  39  
  40    Str format(FPod pod) { return signature(pod) }
  41  
  42    Void write(OutStream out)
  43    {
  44      out.writeI2(podName)
  45      out.writeI2(typeName)
  46      out.writeUtf(sig)
  47    }
  48  
  49    static FTypeRef read(InStream in)
  50    {
  51      return make(in.readU2, in.readU2, in.readUtf)
  52    }
  53  
  54    const Int podName     // names index
  55    const Int typeName    // names index
  56    const Str sig
  57    const Int hashcode
  58  
  59  }
  60  
  61  **************************************************************************
  62  ** FFieldRef
  63  **************************************************************************
  64  
  65  const class FFieldRef
  66  {
  67  
  68    new make(Int parent, Int name, Int typeRef)
  69    {
  70      this.parent  = parent
  71      this.name    = name
  72      this.typeRef = typeRef
  73      this.hashcode = (parent << 23) ^ (name << 7) ^ (typeRef)
  74    }
  75  
  76    override Int hash()
  77    {
  78      return hashcode
  79    }
  80  
  81    override Bool equals(Obj obj)
  82    {
  83      x := (FFieldRef)obj
  84      return parent === x.parent && name === x.name && typeRef === x.typeRef
  85    }
  86  
  87    Str format(FPod pod)
  88    {
  89      return pod.typeRefStr(parent) + "." + pod.names[name] + " -> " + pod.typeRefStr(typeRef)
  90    }
  91  
  92    Void write(OutStream out)
  93    {
  94      out.writeI2(parent)
  95      out.writeI2(name)
  96      out.writeI2(typeRef)
  97    }
  98  
  99    static FFieldRef read(InStream in)
 100    {
 101      return make(in.readU2, in.readU2, in.readU2)
 102    }
 103  
 104    const Int parent    // typeRefs index
 105    const Int name      // names index
 106    const Int typeRef   // typeRefs index
 107    const Int hashcode
 108  }
 109  
 110  **************************************************************************
 111  ** FMethodRef
 112  **************************************************************************
 113  
 114  const class FMethodRef
 115  {
 116  
 117    new make(Int parent, Int name, Int ret, Int[] params)
 118    {
 119      this.parent  = parent
 120      this.name    = name
 121      this.ret     = ret
 122      this.params  = params.toImmutable
 123      this.hashcode = (parent << 23) ^ (name << 7) ^ (ret)
 124    }
 125  
 126    override Int hash()
 127    {
 128      return hashcode
 129    }
 130  
 131    override Bool equals(Obj obj)
 132    {
 133      x := (FMethodRef)obj
 134      return parent === x.parent && name === x.name && ret === x.ret && params == x.params
 135    }
 136  
 137    Str format(FPod pod)
 138    {
 139      s := pod.typeRefStr(parent) + "." + pod.names[name] + "("
 140      params.each |Int p, Int i|
 141      {
 142        if (i > 0) s += ", "
 143        s += pod.typeRefStr(p)
 144      }
 145      s += ") -> " + pod.typeRefStr(ret)
 146      return s
 147    }
 148  
 149    Void write(OutStream out)
 150    {
 151      out.writeI2(parent)
 152      out.writeI2(name)
 153      out.writeI2(ret)
 154      out.write(params.size)
 155      params.each |Int param| { out.writeI2(param) }
 156    }
 157  
 158    static FMethodRef read(InStream in)
 159    {
 160      parent := in.readU2
 161      name   := in.readU2
 162      ret    := in.readU2
 163      p := Int[,]
 164      in.readU1.times |,| { p.add(in.readU2) }
 165      return make(parent, name, ret, p)
 166    }
 167  
 168    const Int parent    // typeRefs index
 169    const Int name      // names index
 170    const Int ret       // typeRefs index
 171    const Int[] params  // typeRefs indices
 172    const Int hashcode
 173  }