logo

class

compiler::ReflectNamespace

sys::Obj
  compiler::Namespace
    compiler::ReflectNamespace
   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  ** ReflectNamespace implements Namespace using reflection to
  12  ** compile against the VM's current pod repository.
  13  **
  14  class ReflectNamespace : Namespace
  15  {
  16  
  17  //////////////////////////////////////////////////////////////////////////
  18  // Constructor
  19  //////////////////////////////////////////////////////////////////////////
  20  
  21    **
  22    ** Construct a ReflectNamespace
  23    **
  24    new make()
  25    {
  26      init
  27    }
  28  
  29  //////////////////////////////////////////////////////////////////////////
  30  // Namespace
  31  //////////////////////////////////////////////////////////////////////////
  32  
  33    **
  34    ** Map an imported Pod into a CPod
  35    **
  36    override ReflectPod resolvePod(Str podName, Bool checked)
  37    {
  38      if (pods == null) pods = Str:ReflectPod[:]
  39  
  40      // check cache
  41      cpod := pods[podName]
  42      if (cpod != null) return cpod
  43  
  44      // try to load it
  45      pod := Pod.find(podName, checked)
  46      if (pod == null) return null
  47      pods[podName] = cpod = ReflectPod.make(this, pod)
  48      return cpod
  49    }
  50  
  51  //////////////////////////////////////////////////////////////////////////
  52  // Mapping
  53  //////////////////////////////////////////////////////////////////////////
  54  
  55    **
  56    ** Map an imported Pod into a CPod
  57    **
  58    ReflectPod importPod(Pod pod)
  59    {
  60      return resolvePod(pod.name, true)
  61    }
  62  
  63    **
  64    ** Map an imported Type into a CType
  65    **
  66    CType importType(Type t)
  67    {
  68      if (t == null) return null
  69      return resolveType(t.signature)
  70    }
  71  
  72    **
  73    ** Map a list of imported Types into a CTypes
  74    **
  75    CType[] importTypes(Type[] t)
  76    {
  77      return (CType[])t.map(CType[,]) |Type x->Obj| { return importType(x) }
  78    }
  79  
  80    **
  81    ** Map an imported Slot into a CSlot
  82    **
  83    CSlot importSlot(Slot slot)
  84    {
  85      if (slot is Method)
  86        return importMethod((Method)slot)
  87      else
  88        return importField((Field)slot)
  89    }
  90  
  91    **
  92    ** Map an imported Field into a CField
  93    **
  94    CField importField(Field f)
  95    {
  96      return ReflectField.make((ReflectType)importType(f.parent), f)
  97    }
  98  
  99    **
 100    ** Map an imported Method into a CMethod
 101    **
 102    CMethod importMethod(Method m)
 103    {
 104      return ReflectMethod.make((ReflectType)importType(m.parent), m)
 105    }
 106  
 107  //////////////////////////////////////////////////////////////////////////
 108  // Fields
 109  //////////////////////////////////////////////////////////////////////////
 110  
 111    private Str:ReflectPod pods  // keyed by pod name
 112  
 113  }