Type models a static type definition for an Obj class. A Type lifecycle: 1) Hollow: in this state we know basic identity of the type, and it's inheritance hierarchy. A type is setup to be hollow during Pod.load(). 2) Reflected: in this state we read all the slot definitions from the fcode to populate the slot tables used to for reflection. At this point clients can discover the signatures of the Type. 3) Emitted: the final state of loading a Type is to emit to a Java class called "fan.{pod}.{type}". Once emitted we can instantiate the type or call it's methods. 4) Finished: once we have reflected the slots into memory and emitted the Java class, the last stage is to bind the all the java.lang.reflect representations to the Slots for dynamic dispatch. We delay this until needed by Method or Field for a reflection invocation Type models sys::Type. Implementation classes are: - ClassType - GenericType (ListType, MapType, FuncType) - NullableType
상속: FanObj, Literal
예제 #1
0
파일: Fan.cs 프로젝트: syatanic/fantom
        int executeType(string target)
        {
            if (target.IndexOf("::") < 0)
            {
                target += "::Main.main";
            }
            else if (target.IndexOf('.') < 0)
            {
                target += ".main";
            }

            try
            {
                int    dot  = target.IndexOf('.');
                Type   type = Type.find(target.Substring(0, dot), true);
                Method main = type.method(target.Substring(dot + 1), true);
                return(callMain(type, main));
            }
            catch (Exception e)
            {
                if (e is Err.Val)
                {
                    ((Err.Val)e).err().trace();
                }
                else
                {
                    Err.dumpStack(e);
                }
                return(-1);
            }
        }
예제 #2
0
파일: FuncType.cs 프로젝트: nomit007/f4
        //////////////////////////////////////////////////////////////////////////
        // Constructor
        //////////////////////////////////////////////////////////////////////////
        public FuncType(Type[] pars, Type ret)
            : base(Sys.FuncType)
        {
            this.m_params = pars;
              this.m_ret    = ret;

              // I am a generic parameter type if any my args or
              // return type are generic parameter types.
              this.m_genericParameterType |= ret.isGenericParameter();
              for (int i=0; i<m_params.Length; ++i)
            this.m_genericParameterType |= m_params[i].isGenericParameter();
        }
예제 #3
0
파일: Service_.cs 프로젝트: nomit007/f4
 public static List findAll(Type t)
 {
     string qname = t.qname();
       List list = new List(Sys.ServiceType);
       lock (m_lock)
       {
     Node node = (Node)byType[qname];
     while (node != null)
     {
       list.add(node.service);
       node = node.next;
     }
       }
       return list.ro();
 }
예제 #4
0
파일: Enum.cs 프로젝트: nomit007/f4
 protected static Enum doFromStr(Type t, string name, bool check)
 {
     // the compiler marks the value fields with the Enum flag
       Slot slot = t.slot(name, false);
       if (slot != null && (slot.m_flags & FConst.Enum) != 0)
       {
     try
     {
       return (Enum)((Field)slot).get(null);
     }
     catch (System.Exception)
     {
     }
       }
       if (!check) return null;
       throw ParseErr.make(t.qname(), name).val;
 }
예제 #5
0
파일: Fan.cs 프로젝트: syatanic/fantom
        public int executeFile(FileInfo file)
        {
            LocalFile f = (LocalFile)(new LocalFile(file).normalize());

            // use Fantom reflection to run compiler::Main.compileScript(File)
            Pod pod = null;

            try
            {
                pod = Env.cur().compileScript(f).pod();
            }
            catch (Err.Val e)
            {
                System.Console.WriteLine("ERROR: cannot compile script");
                e.err().trace();
                return(-1);
            }
            catch (Exception e)
            {
                System.Console.WriteLine("ERROR: cannot compile script");
                System.Console.WriteLine(e);
                return(-1);
            }

            List   types = pod.types();
            Type   type  = null;
            Method main  = null;

            for (int i = 0; i < types.sz(); ++i)
            {
                type = (Type)types.get(i);
                main = type.method("main", false);
                if (main != null)
                {
                    break;
                }
            }

            if (main == null)
            {
                System.Console.WriteLine("ERROR: missing main method: " + ((Type)types.get(0)).name() + ".main()");
                return(-1);
            }

            return(callMain(type, main));
        }
예제 #6
0
파일: Facets.cs 프로젝트: nomit007/f4
        public Facet decode(Type type, string s)
        {
            try
              {
            // if no string use make/defVal
            if (s.Length == 0) return (Facet)type.make();

            // decode using normal Fantom serialization
            return (Facet)ObjDecoder.decode(s);
              }
              catch (System.Exception e)
              {
            string msg = "ERROR: Cannot decode facet " + type + ": " + s;
            System.Console.WriteLine(msg);
            Err.dumpStack(e);
            m_map.Remove(type);
            throw IOErr.make(msg).val;
              }
        }
예제 #7
0
파일: FuncType.cs 프로젝트: nomit007/f4
        public override bool @is(Type type)
        {
            if (this == type) return true;
              if (type is FuncType)
              {
            FuncType t = (FuncType)type;

            // match return type (if void is needed, anything matches)
            if (t.m_ret != Sys.VoidType && !m_ret.@is(t.m_ret)) return false;

            // match params - it is ok for me to have less than
            // the type params (if I want to ignore them), but I
            // must have no more
            if (m_params.Length > t.m_params.Length) return false;
            for (int i=0; i<m_params.Length; ++i)
              if (!t.m_params[i].@is(m_params[i])) return false;

            // this method works for the specified method type
            return true;
              }
              return @base().@is(type);
        }
예제 #8
0
파일: Fan.cs 프로젝트: syatanic/fantom
        static int callMain(Type t, Method m)
        {
            // check parameter type and build main arguments
            List args;
            List pars = m.@params();

            if (pars.sz() == 0)
            {
                args = null;
            }
            else if (((Param)pars.get(0)).type().@is(Sys.StrType.toListOf()) &&
                     (pars.sz() == 1 || ((Param)pars.get(1)).hasDefault()))
            {
                args = new List(Sys.ObjType, new object[] { Env.cur().args() });
            }
            else
            {
                System.Console.WriteLine("ERROR: Invalid parameters for main: " + m.signature());
                return(-1);
            }

            // invoke
            try
            {
                if (m.isStatic())
                {
                    return(toResult(m.callList(args)));
                }
                else
                {
                    return(toResult(m.callOn(t.make(), args)));
                }
            }
            catch (Err.Val ex)
            {
                ex.err().trace();
                return(-1);
            }
        }
예제 #9
0
파일: Service_.cs 프로젝트: nomit007/f4
 static bool isServiceType(Type t)
 {
     return t != Sys.ObjType && t != Sys.ServiceType && t.isPublic();
 }
예제 #10
0
파일: FTypeEmit.cs 프로젝트: nomit007/f4
        private void findMixins(Type t, Hashtable acc)
        {
            // if mixin I haven't seen add to accumulator
              string qname = t.qname();
              if (t.isMixin() && acc[qname] == null)
            acc[qname] = t;

              // recurse
              for (int i=0; i<t.mixins().sz(); ++i)
            findMixins((Type)t.mixins().get(i), acc);
        }
예제 #11
0
파일: NullableType.cs 프로젝트: nomit007/f4
 //////////////////////////////////////////////////////////////////////////
 // Constructor
 //////////////////////////////////////////////////////////////////////////
 internal NullableType(Type root)
 {
     m_root = root;
       m_signature = root.signature() + "?";
 }
예제 #12
0
파일: GenericType.cs 프로젝트: nomit007/f4
        /// <summary>
        /// Recursively parameterize the pars of a method type.
        /// </summary>
        internal FuncType parameterizeFuncType(FuncType t)
        {
            Type[] pars = new Type[t.m_params.Length];
              for (int i=0; i<pars.Length; i++)
              {
            Type param = t.m_params[i];
            if (param.isGenericParameter()) param = doParameterize(param);
            pars[i] = param;
              }

              Type ret = t.m_ret;
              if (ret.isGenericParameter()) ret = doParameterize(ret);

              return new FuncType(pars, ret);
        }
예제 #13
0
파일: GenericType.cs 프로젝트: nomit007/f4
 public override sealed Type toNullable()
 {
     if (m_nullable == null) m_nullable = new NullableType(this);
       return m_nullable;
 }
예제 #14
0
파일: Fant.cs 프로젝트: nomit007/f4
        private Method[] methods(Type type, string methodName)
        {
            // named test
              if (methodName != "*") return new Method[] { type.method(methodName, true) };

              // all methods which start with "test"
              List all = type.methods();
              ArrayList acc = new ArrayList();
              for (int i=0; i<all.sz(); i++)
              {
            Method m = (Method)all.get(i);
            if (m.name().StartsWith("test") && !m.isAbstract()) acc.Add(m);
              }
              return (Method[])acc.ToArray(System.Type.GetType("Fan.Sys.Method"));
        }
예제 #15
0
파일: OpUtil.cs 프로젝트: nomit007/f4
 //////////////////////////////////////////////////////////////////////////
 // Is/As
 //////////////////////////////////////////////////////////////////////////
 public static bool @is(object instance, Type type)
 {
     if (instance == null) return false;
       return FanObj.@typeof(instance).fits(type);
 }
예제 #16
0
파일: ObjDecoder.cs 프로젝트: nomit007/f4
 internal UsingType(Type t, string n)
 {
     type = t; name = n;
 }
예제 #17
0
파일: NullableType.cs 프로젝트: nomit007/f4
 public override bool @is(Type type)
 {
     return m_root.@is(type);
 }
예제 #18
0
파일: Service_.cs 프로젝트: nomit007/f4
 public static Service find(Type t)
 {
     return find(t.qname(), true);
 }
예제 #19
0
파일: Fant.cs 프로젝트: nomit007/f4
        private int runTest(Type type, Method method)
        {
            Method setup    = type.method("setup", true);
              Method teardown = type.method("teardown", true);

              FanSysTest test = null;
              List args = null;
              try
              {
            test = (FanSysTest)type.make();
            args = new List(Sys.ObjType, new object[] {test});
              }
              catch (System.Exception e)
              {
            System.Console.WriteLine();
            System.Console.WriteLine("ERROR: Cannot make test " + type);
            if (e is Err.Val)
              ((Err.Val)e).err().trace();
            else
              Err.dumpStack(e);
            return -1;
              }

              try
              {
            test.m_curTestMethod = method;
            setup.callList(args);
            method.callList(args);
            return test.verifyCount;
              }
              catch (System.Exception e)
              {
            //System.Console.WriteLine(" -- " + e.GetType() + " -- ");
            System.Console.WriteLine();
            System.Console.WriteLine("TEST FAILED");
            if (e is Err.Val)
              ((Err.Val)e).err().trace();
            else
              Err.dumpStack(e);
            return -1;
              }
              finally
              {
            try
            {
              if (args != null) teardown.callList(args);
            }
            catch (System.Exception e)
            {
              Err.dumpStack(e);
            }
            test.m_curTestMethod = null;
              }
        }
예제 #20
0
파일: Service_.cs 프로젝트: nomit007/f4
 public static Service find(Type t, bool check)
 {
     return find(t.qname(), check);
 }
예제 #21
0
파일: GenericType.cs 프로젝트: nomit007/f4
 //////////////////////////////////////////////////////////////////////////
 // Constructor
 //////////////////////////////////////////////////////////////////////////
 internal GenericType(Type baseType)
 {
     m_base = baseType;
 }
예제 #22
0
파일: FErrEmit.cs 프로젝트: nomit007/f4
 //////////////////////////////////////////////////////////////////////////
 // Constructor
 //////////////////////////////////////////////////////////////////////////
 public FErrEmit(Emitter emitter, Type parent, FType type)
     : base(emitter, parent, type)
 {
 }
예제 #23
0
파일: GenericType.cs 프로젝트: nomit007/f4
 public override Facet facet(Type t, bool c)
 {
     return m_base.facet(t, c);
 }
예제 #24
0
파일: GenericType.cs 프로젝트: nomit007/f4
 /// <summary>
 /// Parameterize t, where t is a generic parameter type such as V.
 /// </summary>
 internal Type parameterize(Type t)
 {
     bool nullable = t.isNullable();
       Type nn = t.toNonNullable();
       if (nn is ListType)
     t = parameterizeListType((ListType)nn);
       else if (nn is FuncType)
     t = parameterizeFuncType((FuncType)nn);
       else
     t = doParameterize(nn);
       return nullable ? t.toNullable() : t;
 }
예제 #25
0
파일: ActorPool.cs 프로젝트: nomit007/f4
 //////////////////////////////////////////////////////////////////////////
 // Obj
 //////////////////////////////////////////////////////////////////////////
 public override Type @typeof()
 {
     if (m_type == null) m_type = Type.find("concurrent::ActorPool");
       return m_type;
 }
예제 #26
0
파일: GenericType.cs 프로젝트: nomit007/f4
 /// <summary>
 /// Parameterize t, where t is a generic parameter type such as V.
 /// </summary>
 protected abstract Type doParameterize(Type t);
예제 #27
0
파일: ObjDecoder.cs 프로젝트: nomit007/f4
 void complexAdd(Type t, object obj, Method addMethod, object val, int line)
 {
     try
       {
     addMethod.invoke(obj, new object[] { val });
       }
       catch (System.Exception ex)
       {
     throw err("Cannot call " + t.qname() + ".add: " + ex, line, ex);
       }
 }
예제 #28
0
파일: GenericType.cs 프로젝트: nomit007/f4
 public override bool @is(Type type)
 {
     if (type == this || type == m_base) return true;
       return m_base.@is(type);
 }
예제 #29
0
 //////////////////////////////////////////////////////////////////////////
 // Constructor
 //////////////////////////////////////////////////////////////////////////
 public FMixinInterfaceEmit(Emitter emitter, Type parent, FType type)
     : base(emitter, parent, type)
 {
 }
예제 #30
0
파일: FMethodEmit.cs 프로젝트: nomit007/f4
        /// <summary>
        /// The CLR requires all methods implemented by an interface
        /// to be marked as virtual.  However, Fantom (and Java) allow
        /// this:
        ///
        ///   mixin Mixin
        ///   {
        ///     abstract Long foo()
        ///   }
        ///
        ///   class Base
        ///   {
        ///     Long foo() { return 5 }
        ///   }
        ///
        ///   class Child : Base, Mixin
        ///   {
        ///     // don't need to implement foo() since Base defined it
        ///   }
        ///
        /// So to work correctly in the CLR we need to trap this case
        /// and emit a virtual router method on child to satisfy the
        /// interface requirement:
        ///
        ///   class Child : Base, Mixin
        ///   {
        ///     public virtual Long foo() { return base.foo(); }
        ///   }
        ///
        ///   TODO - optimize the intra-pod case
        ///
        /// </summary>
        public void emitInterfaceRouter(Type implType, Method m)
        {
            string impl = FanUtil.toDotnetTypeName(implType);
              string name = m.name();
              string ret  = FanUtil.toDotnetTypeName(m.inheritedReturns());
              List pars   = m.@params();
              int paramCount = pars.sz();

              // find first param with default value
              int firstDefault = paramCount;
              for (int i=0; i<paramCount; i++)
            if (((Param)pars.get(i)).hasDefault())
              { firstDefault = i; break; }

              // generate routers
              for (int i=firstDefault; i<=paramCount; i++)
              {
            string[] myParams = new string[i];
            string[] myParamNames = new string[i];

            for (int j=0; j<i; j++)
            {
              Param param = (Param)m.@params().get(j);
              Type pt = param.type();
              string s = FanUtil.toDotnetTypeName(pt);
              myParams[j] = s;
              myParamNames[j] = param.name();
            }

            // CLR requires public virtual
            PERWAPI.MethAttr attr = PERWAPI.MethAttr.Public | PERWAPI.MethAttr.Virtual;

            PERWAPI.CILInstructions code = emitter.emitMethod(name, ret, myParamNames, myParams,
              attr, new string[0], new string[0]);
            code.Inst(PERWAPI.Op.ldarg_0); // push this
            for (int p=0; p<i; p++)
            {
              // push args
              Param param = (Param)m.@params().get(p);
              FCodeEmit.loadVar(code, FanUtil.toDotnetStackType(param.type()), p+1);
            }
            PERWAPI.Method meth = emitter.findMethod(impl, name, myParams, ret);
            code.MethInst(PERWAPI.MethodOp.call, meth);
            code.Inst(PERWAPI.Op.ret);
              }
        }
예제 #31
0
파일: FTypeEmit.cs 프로젝트: nomit007/f4
        private static Hashtable ftypes = new Hashtable(); // ftype[] lookup

        #endregion Fields

        #region Constructors

        //////////////////////////////////////////////////////////////////////////
        // Constructor
        //////////////////////////////////////////////////////////////////////////
        protected FTypeEmit(Emitter emitter, Type parent, FType type)
        {
            this.emitter = emitter;
              this.parent  = parent;
              this.pod     = type.m_pod;
              this.type    = type;
        }
예제 #32
0
파일: EnvScripts.cs 프로젝트: nomit007/f4
        void putCache(File file, Type t)
        {
            CachedScript c = new CachedScript();
              c.modified = file.modified();
              c.size     = file.size();
              c.typeName = t.qname();

              lock (m_cache) { m_cache[cacheKey(file)] = c; }
        }
예제 #33
0
파일: FTypeEmit.cs 프로젝트: nomit007/f4
        private void emitMixinRouters(Type type)
        {
            // generate router method for each concrete instance method
              List methods = type.methods();
              for (int i=0; i<methods.sz(); i++)
              {
            Method m = (Method)methods.get(i);
            string name = m.name();

            // only emit router for non-abstract instance methods
            if (m.isStatic()) continue;
            if (m.isAbstract())
            {
              // however if abstract, check if a base class
              // has already implemented this method
              if (m.parent() == type && parent.slot(name, true).parent() == type)
              {
            Type b = parent.@base();
            while (b != null)
            {
              Slot s = b.slot(name, false);
              if (s != null && s.parent() == b)
              {
                new FMethodEmit(this).emitInterfaceRouter(b, m);
                break;
              }
              b = b.@base();
            }
              }
              continue;
            }

            // only emit the router unless this is the exact one I inherit
            if (parent.slot(name, true).parent() != type) continue;

            // do it
            new FMethodEmit(this).emitMixinRouter(m);
              }
        }