logo
abstract class

compiler::Namespace

sys::Obj
  compiler::Namespace
   1  //
   2  // Copyright (c) 2006, Brian Frank and Andy Frank
   3  // Licensed under the Academic Free License version 3.0
   4  //
   5  // History:
   6  //   15 Sep 05  Brian Frank  Creation
   7  //   29 Aug 06  Brian Frank  Ported from Java to Fan
   8  //
   9  
  10  **
  11  ** Namespace is responsible for providing a unified view pods, types,
  12  ** and slots between the entities currently being compiled and the
  13  ** entities being imported from pre-compiled pods.
  14  **
  15  abstract class Namespace
  16  {
  17  
  18  //////////////////////////////////////////////////////////////////////////
  19  // Initialization
  20  //////////////////////////////////////////////////////////////////////////
  21  
  22    **
  23    ** Once the sub class is initialized, it must call this
  24    ** method to initialize our all predefined values.
  25    **
  26    protected Void init()
  27    {
  28      // sys pod
  29      sysPod = resolvePod("sys"true)
  30  
  31      // error placeholder type
  32      error = GenericParameterType.make(this"Error")
  33  
  34      // generic parameter types
  35      genericParams =
  36      [
  37        "A": genericParam("A"),
  38        "B": genericParam("B"),
  39        "C": genericParam("C"),
  40        "D": genericParam("D"),
  41        "E": genericParam("E"),
  42        "F": genericParam("F"),
  43        "G": genericParam("G"),
  44        "H": genericParam("H"),
  45        "K": genericParam("K"),
  46        "L": genericParam("L"),
  47        "M": genericParam("M"),
  48        "R": genericParam("R"),
  49        "V": genericParam("V"),
  50      ].ro()
  51  
  52      // types
  53      objType      = sysType("Obj")
  54      boolType     = sysType("Bool")
  55      enumType     = sysType("Enum")
  56      intType      = sysType("Int")
  57      floatType    = sysType("Float")
  58      strType      = sysType("Str")
  59      strBufType   = sysType("StrBuf")
  60      durationType = sysType("Duration")
  61      listType     = sysType("List")
  62      mapType      = sysType("Map")
  63      funcType     = sysType("Func")
  64      errType      = sysType("Err")
  65      typeType     = sysType("Type")
  66      slotType     = sysType("Slot")
  67      rangeType    = sysType("Range")
  68      uriType      = sysType("Uri")
  69      voidType     = sysType("Void")
  70  
  71      // methods
  72      objTrap      = sysMethod(objType,    "trap")
  73      boolNot      = sysMethod(boolType,   "not")
  74      intIncrement = sysMethod(intType,    "increment")
  75      intDecrement = sysMethod(intType,    "decrement")
  76      intPlus      = sysMethod(intType,    "plus")
  77      floatPlus    = sysMethod(floatType,  "plus")
  78      floatMinus   = sysMethod(floatType,  "minus")
  79      strPlus      = sysMethod(strType,    "plus")
  80      strBufMake   = sysMethod(strBufType, "make")
  81      strBufAdd    = sysMethod(strBufType, "add")
  82      strBufToStr  = sysMethod(strBufType, "toStr")
  83      listMake     = sysMethod(listType,   "make")
  84      listMakeObj  = sysMethod(listType,   "makeObj")
  85      listAdd      = sysMethod(listType,   "add")
  86      mapMake      = sysMethod(mapType,    "make")
  87      mapSet       = sysMethod(mapType,    "set")
  88      enumOrdinal  = sysMethod(enumType,   "ordinal")
  89      rangeMakeInclusive = sysMethod(rangeType, "makeInclusive")
  90      rangeMakeExclusive = sysMethod(rangeType, "makeExclusive")
  91      slotFindFunc       = sysMethod(slotType,  "findFunc")
  92    }
  93  
  94    private CType genericParam(Str name)
  95    {
  96      t := GenericParameterType.make(this, name)
  97      types[t.qname] = t
  98      return t
  99    }
 100  
 101    private CType sysType(Str name)
 102    {
 103      return sysPod.resolveType(name, true)
 104    }
 105  
 106    private CMethod sysMethod(CType t, Str name)
 107    {
 108      m := t.method(name)
 109      if (m == null) throw Err.make("Cannot resolve '${t.qname}.$name' method in namespace")
 110      return m
 111    }
 112  
 113  //////////////////////////////////////////////////////////////////////////
 114  // Resolution
 115  //////////////////////////////////////////////////////////////////////////
 116  
 117    **
 118    ** Attempt to import the specified pod name against our
 119    ** dependency library.  If not found and checked is true
 120    ** throw UnknownPodErr otherwise return null.
 121    **
 122    abstract CPod resolvePod(Str podName, Bool checked)
 123  
 124    **
 125    ** Attempt resolve a signature against our dependency
 126    ** library.  If not a valid signature or it can't be
 127    ** resolved, then throw Err.
 128    **
 129    CType resolveType(Str sig)
 130    {
 131      // check our cache first
 132      t := types[sig]
 133      if (t != null) return t
 134  
 135      // parse it into a CType
 136      t = TypeParser.resolve(this, sig)
 137      types[sig] = t
 138      return t
 139    }
 140    internal Str:CType types := Str:CType[:]   // keyed by signature
 141  
 142    **
 143    ** Map one of the generic parameter types such as "sys::V" into a CType
 144    **
 145    CType genericParameter(Str id)
 146    {
 147      t := genericParams[id]
 148      if (t == null) throw UnknownTypeErr.make("sys::$id")
 149      return t
 150    }
 151  
 152  //////////////////////////////////////////////////////////////////////////
 153  // Dependencies
 154  //////////////////////////////////////////////////////////////////////////
 155  
 156    **
 157    ** Map of dependencies keyed by pod name set in ResolveDepends.
 158    **
 159    Str:Depend depends
 160  
 161  //////////////////////////////////////////////////////////////////////////
 162  // Predefined
 163  //////////////////////////////////////////////////////////////////////////
 164  
 165    readonly CPod sysPod
 166  
 167    // generic parameters like sys::K, sys::V
 168    readonly Str:CType genericParams
 169  
 170    // place holder type used for resolve errors
 171    readonly CType error
 172  
 173    readonly CType objType
 174    readonly CType boolType
 175    readonly CType enumType
 176    readonly CType intType
 177    readonly CType floatType
 178    readonly CType strType
 179    readonly CType strBufType
 180    readonly CType durationType
 181    readonly CType listType
 182    readonly CType mapType
 183    readonly CType funcType
 184    readonly CType errType
 185    readonly CType typeType
 186    readonly CType slotType
 187    readonly CType rangeType
 188    readonly CType uriType
 189    readonly CType voidType
 190  
 191    readonly CMethod objTrap
 192    readonly CMethod boolNot
 193    readonly CMethod intIncrement
 194    readonly CMethod intDecrement
 195    readonly CMethod intPlus
 196    readonly CMethod floatPlus
 197    readonly CMethod floatMinus
 198    readonly CMethod strPlus
 199    readonly CMethod strBufMake
 200    readonly CMethod strBufAdd
 201    readonly CMethod strBufToStr
 202    readonly CMethod listMake
 203    readonly CMethod listMakeObj
 204    readonly CMethod listAdd
 205    readonly CMethod mapMake
 206    readonly CMethod mapSet
 207    readonly CMethod enumOrdinal
 208    readonly CMethod rangeMakeInclusive
 209    readonly CMethod rangeMakeExclusive
 210    readonly CMethod slotFindFunc
 211  
 212  }