Esempio n. 1
0
 public void ResolveTypes(ModuleDefn global, ModuleDefn m)
 {
     if (baseclass != null)
     {
         baseclass.Resolve(global, m);
     }
     for (int i = 0; i < interfaces.Count; ++i)
     {
         interfaces[i].Resolve(global, m);
         HeronType     t  = interfaces[i].type;
         InterfaceDefn id = t as InterfaceDefn;
         if (id == null)
         {
             throw new Exception(t.name + " is not an interface");
         }
     }
     foreach (FieldDefn f in GetFields())
     {
         f.ResolveTypes(global, m);
     }
     foreach (FunctionDefn f in GetAllMethods())
     {
         f.ResolveTypes(global, m);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Sets the value associated with the named field
        /// </summary>
        /// <param name="name"></param>
        /// <param name="val"></param>
        public virtual void SetField(string name, HeronValue val)
        {
            HeronType t  = Type;
            FieldDefn fi = t.GetField(name);

            fi.SetValue(this, val);
        }
Esempio n. 3
0
 public void AddPrimitive(string s, HeronType t)
 {
     if (FindType(s) != null)
     {
         throw new Exception("Type '" + s + "' already exists");
     }
     types.Add(s, t);
 }
 public override HeronValue As(HeronType t)
 {
     if (t == PrimitiveTypes.ListType)
     {
         return(ToList());
     }
     return(base.As(t));
 }
 public ListValue(IteratorValue val)
 {
     list = new List <HeronValue>();
     while (val.MoveNext())
     {
         Add(val.GetValue());
     }
     elementType = val.GetElementType();
 }
Esempio n. 6
0
 public HeronType(HeronType baseType, ModuleDefn m, Type t, string name)
 {
     module = m;
     type = t;
     this.baseType = baseType;
     Debug.Assert(t != null);
     this.name = name;
     StoreExposedFunctionsAndFields();
 }
 public ListValue(IList xs, HeronType elementType)
 {
     list = new List <HeronValue>();
     foreach (Object x in xs)
     {
         list.Add(DotNetObject.Marshal(x));
     }
     this.elementType = elementType;
 }
Esempio n. 8
0
 public HeronType(HeronType baseType, ModuleDefn m, Type t, string name)
 {
     module        = m;
     type          = t;
     this.baseType = baseType;
     Debug.Assert(t != null);
     this.name = name;
     StoreExposedFunctionsAndFields();
 }
Esempio n. 9
0
        public override bool Equals(object obj)
        {
            HeronType t = obj as HeronType;

            if (t == null)
            {
                return(false);
            }
            return(name == t.name);
        }
 public ListToIterValue(IEnumerable iter, HeronType elementType)
 {
     list = new List <HeronValue>();
     foreach (Object o in iter)
     {
         list.Add(HeronDotNet.Marshal(o));
     }
     current          = 0;
     this.elementType = elementType;
 }
Esempio n. 11
0
 static void AppendFields(StringBuilder sb, HeronType t)
 {
     sb.AppendLine("  fields");
     sb.AppendLine("  {");
     foreach (ExposedField f in t.GetExposedFields())
     {
         sb.AppendLine("    " + f.ToString() + ";");
     }
     sb.AppendLine("  }");
 }
Esempio n. 12
0
 static void AppendMethods(StringBuilder sb, HeronType t)
 {
     sb.AppendLine("  methods");
     sb.AppendLine("  {");
     foreach (ExposedMethodValue m in t.GetExposedMethods())
     {
         sb.AppendLine("    " + m.ToString() + ";");
     }
     sb.AppendLine("  }");
 }
Esempio n. 13
0
 public void Resolve(ModuleDefn global, ModuleDefn m)
 {
     type = m.FindType(name);
     if (type == null)
         type = global.FindType(name);
     if (type == null)
         throw new Exception("Could not resolve type " + name);
     if (type.name != name)
         throw new Exception("Internal error during type resolution of " + name);
 }
Esempio n. 14
0
        public override HeronValue As(HeronType t)
        {
            if (type.name == t.name)
            {
                return(obj);
            }

            if (type is ClassDefn)
            {
                ClassInstance inst = obj as ClassInstance;
                if (inst == null)
                {
                    throw new Exception("Expected an instance of a class");
                }
                return(inst.As(t));
            }
            else if (type is InterfaceDefn)
            {
                InterfaceInstance ii = obj as InterfaceInstance;
                if (ii == null)
                {
                    throw new Exception("Expected an instance of an interface");
                }
                return(ii.As(t));
            }
            else if (type is DotNetClass)
            {
                if (!(t is DotNetClass))
                {
                    throw new Exception("External objects can only be cast to the type 'DotNetClass'");
                }

                if (t.Equals(type))
                {
                    return(obj);
                }
            }
            else if (t.name == "Any")
            {
                return(this);
            }
            else
            {
                Type from = type.GetSystemType();
                Type to   = t.GetSystemType();

                if (from != null && to != null && to.IsAssignableFrom(from))
                {
                    return(obj);
                }
            }

            return(null);
        }
Esempio n. 15
0
        // TODO: I may want to change the requirement that DuckValue needs a
        // class instances, same with interface instance
        public DuckValue(ClassInstance obj, InterfaceDefn i)
            : base(obj, i)
        {
            HeronType t = obj.Type;

            foreach (FunctionDefn f in i.GetAllMethods())
            {
                if (!obj.SupportsFunction(f))
                {
                    throw new Exception("Failed to duck-type, object of type " + t.GetName() + " does not match interface " + i.GetName());
                }
            }
        }
Esempio n. 16
0
        public override HeronValue As(HeronType t)
        {
            InterfaceDefn id = t as InterfaceDefn;

            if (id == null)
            {
                return(null);
            }
            if (hinterface.Implements(id))
            {
                return(new InterfaceInstance(obj, id));
            }
            return(null);
        }
Esempio n. 17
0
 public AnyValue(HeronValue obj)
 {
     AnyValue av = (obj as AnyValue);
     if (av != null)
     {
         this.obj = av.obj;
         this.type = av.type;
     }
     else
     {
         this.obj = obj;
         this.type = obj.Type;
     }
 }
Esempio n. 18
0
        public override int GetHierarchyDepth()
        {
            int r = 1;

            foreach (TypeRef tr in basetypes)
            {
                HeronType t   = tr.type;
                int       tmp = t.GetHierarchyDepth() + 1;
                if (tmp > r)
                {
                    r = tmp;
                }
            }
            return(r);
        }
Esempio n. 19
0
 public bool IsAssignableFrom(HeronType type)
 {
     if (Equals(type))
     {
         return(true);
     }
     if (type.baseType != null)
     {
         return(IsAssignableFrom(type.baseType));
     }
     else
     {
         return(false);
     }
 }
Esempio n. 20
0
        public AnyValue(HeronValue obj)
        {
            AnyValue av = (obj as AnyValue);

            if (av != null)
            {
                this.obj  = av.obj;
                this.type = av.type;
            }
            else
            {
                this.obj  = obj;
                this.type = obj.Type;
            }
        }
Esempio n. 21
0
 public void Resolve(ModuleDefn global, ModuleDefn m)
 {
     type = m.FindType(name);
     if (type == null)
     {
         type = global.FindType(name);
     }
     if (type == null)
     {
         throw new Exception("Could not resolve type " + name);
     }
     if (type.name != name)
     {
         throw new Exception("Internal error during type resolution of " + name);
     }
 }
Esempio n. 22
0
 public virtual HeronValue As(HeronType t)
 {
     if (t.IsAssignableFrom(Type))
     {
         return(this);
     }
     if (t == PrimitiveTypes.AnyType)
     {
         return(new AnyValue(this));
     }
     if (t == PrimitiveTypes.UnknownType)
     {
         return(this);
     }
     return(null);
 }
Esempio n. 23
0
        public override HeronValue Eval(VM vm)
        {
            HeronValue a = operand1.Eval(vm);
            HeronValue b = operand2.Eval(vm);

            switch (opcode)
            {
            case OpCode.opEq:
                return(new BoolValue(a.Equals(b)));

            case OpCode.opNEq:
                return(new BoolValue(!a.Equals(b)));

            case OpCode.opIs:
            {
                TypeValue tv = b as TypeValue;
                if (tv == null)
                {
                    throw new Exception("The second argument of the 'is' operator must be a type");
                }
                return(new BoolValue(a.Is(tv.Type)));
            }

            case OpCode.opAs:
            {
                HeronType t = b as HeronType;
                if (t == null)
                {
                    throw new Exception("The 'as' operator expects a type as a right hand argument");
                }
                HeronValue r = a.As(t);
                if (r != null)
                {
                    return(r);
                }
                if (t is InterfaceDefn && a is ClassInstance)
                {
                    DuckValue dv = new DuckValue(a as ClassInstance, t as InterfaceDefn);
                    return(dv);
                }
                throw new Exception("Failed to convert " + a.Type.name + " to a " + t.name);
            };
            }

            return(a.InvokeBinaryOperator(vm, opcode, b));
        }
Esempio n. 24
0
        /// <summary>
        /// Given a name returns the appropriate field (or method)
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public virtual HeronValue GetFieldOrMethod(string name)
        {
            HeronType          t = Type;
            ExposedMethodValue m = t.GetMethod(name);

            if (m != null)
            {
                return(m.CreateBoundMethod(this));
            }
            FieldDefn f = t.GetField(name);

            if (f != null)
            {
                return(f.GetValue(this));
            }
            return(null);
        }
Esempio n. 25
0
        public HeronType FindType(string s)
        {
            HeronType r = FindTypeLocally(s);

            if (r != null)
            {
                return(r);
            }
            foreach (ModuleDefn def in GetImportedModuleDefns())
            {
                r = def.FindTypeLocally(s);
                if (r != null)
                {
                    return(r);
                }
            }
            return(null);
        }
Esempio n. 26
0
 public bool IsCompatible(ListValue x)
 {
     if (x.InternalCount() != Count)
     {
         return(false);
     }
     for (int i = 0; i < Count; ++i)
     {
         HeronValue val  = x.InternalAt(i);
         HeronType  type = GetTypes()[i];
         HeronValue test = val.As(type);
         if (test == null)
         {
             return(false);
         }
     }
     return(true);
 }
Esempio n. 27
0
        public static FunctionDefn CreateMethod(ParseNode x, HeronType parent)
        {
            ModuleDefn   module = parent.GetModule();
            FunctionDefn r      = new FunctionDefn(parent);

            r.annotations = CreateAnnotations(x);
            r.node        = x;
            ParseNode fundecl = x.GetChild("fundecl");

            r.name    = fundecl.GetChild("name").ToString();
            r.formals = CreateFormalArgs(fundecl.GetChild("arglist"));
            ParseNode rt = x.GetChild("typedecl");

            r.rettype = CreateTypeRef(rt);
            ParseNode codeblock = x.GetChild("codeblock");

            r.body = CreateCodeBlock(codeblock);
            return(r);
        }
Esempio n. 28
0
        public override HeronValue As(HeronType t)
        {
            if (type.name == t.name)
                return obj;

            if (type is ClassDefn)
            {
                ClassInstance inst = obj as ClassInstance;
                if (inst == null)
                    throw new Exception("Expected an instance of a class");
                return inst.As(t);
            }
            else if (type is InterfaceDefn)
            {
                InterfaceInstance ii = obj as InterfaceInstance;
                if (ii == null)
                    throw new Exception("Expected an instance of an interface");
                return ii.As(t);
            }
            else if (type is DotNetClass)
            {
                if (!(t is DotNetClass))
                    throw new Exception("External objects can only be cast to the type 'DotNetClass'");

                if (t.Equals(type))
                    return obj;
            }
            else if (t.name == "Any")
            {
                return this;
            }
            else 
            {
                Type from = type.GetSystemType();
                Type to = t.GetSystemType();

                if (from != null && to != null && to.IsAssignableFrom(from))
                    return obj;
            }

            return null;
        }
Esempio n. 29
0
 public bool IsCompatible(RecordValue x)
 {
     for (int i = 0; i < names.Count; ++i)
     {
         string    name = names[i];
         HeronType type = types[i];
         int       n    = x.GetFieldIndex(name);
         if (n < 0)
         {
             return(false);
         }
         HeronValue val  = x.GetValue(n);
         HeronValue test = val.As(type);
         if (test == null)
         {
             return(false);
         }
     }
     return(true);
 }
Esempio n. 30
0
        public void ResolveTypes(ModuleDefn global, ModuleDefn m)
        {
            foreach (FieldInfo fi in GetInstanceFields())
            {
                if (fi.FieldType.Equals(typeof(HeronType)))
                {
                    HeronType t = fi.GetValue(this) as HeronType;
                    fi.SetValue(this, t.Resolve(global, m));
                }
                else if (fi.FieldType.Equals(typeof(VarDesc)))
                {
                    VarDesc vd = fi.GetValue(this) as VarDesc;
                    vd.ResolveTypes(global, m);
                }
            }

            foreach (Expression x in GetSubExpressions())
            {
                x.ResolveAllTypes(global, m);
            }
        }
Esempio n. 31
0
        /// <summary>
        /// Used to cast the class instance to its base class or an interface.
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public override HeronValue As(HeronType t)
        {
            if (t is ClassDefn)
            {
                ClassDefn c1 = cls;
                ClassDefn c2 = t as ClassDefn;

                if (c2.name == c1.name)
                {
                    return(this);
                }

                if (GetBase() == null)
                {
                    return(null);
                }

                return(GetBase().As(t));
            }
            else if (t is InterfaceDefn)
            {
                if (cls.Implements(t as InterfaceDefn))
                {
                    return(new InterfaceInstance(this, t as InterfaceDefn));
                }

                return(null);
            }
            else if (t == PrimitiveTypes.AnyType)
            {
                return(new AnyValue(this));
            }
            else if (t == PrimitiveTypes.UnknownType)
            {
                return(this);
            }
            return(null);
        }
Esempio n. 32
0
 public CodeModelType(HeronType basetype, Type t)
     : base(basetype, null, t, t.Name)
 {
 }
Esempio n. 33
0
        public HeronValue LookupName(string s)
        {
            // Look in the scopes starting with the innermost
            // and moving to the outermost.
            // The outermost scope contains the arguments
            for (int i = scopes.Count; i > 0; --i)
            {
                Scope tbl = scopes[i - 1];
                if (tbl.HasName(s))
                {
                    return(tbl[s]);
                }
            }

            // Nothing found in the local vars,
            // So we look in the "this" pointer (called "self")
            // Note that "self" may be a class instance, or a moduleDef
            // instance
            if (self != null)
            {
                HeronValue r = self.GetFieldOrMethod(s);
                if (r != null)
                {
                    return(r);
                }

                // Nothing found in the "this" pointer. So
                // we look if it has an enclosing module instance pointer.
                // And use that

                ModuleInstance mi = self.GetModuleInstance();
                if (mi != null)
                {
                    r = mi.GetFieldOrMethod(s);
                    if (r != null)
                    {
                        return(r);
                    }
                }
            }

            // Look to see if the name is a type in the current module definition.
            if (moduleDef != null)
            {
                HeronType t = moduleDef.FindType(s);
                if (t != null)
                {
                    return(t);
                }

                // Look to see if the name is a type in one of the imported module definitions.
                List <HeronType> candidates = new List <HeronType>();
                foreach (ModuleDefn defn in moduleDef.GetImportedModuleDefns())
                {
                    t = defn.FindType(s);
                    if (t != null)
                    {
                        candidates.Add(t);
                    }
                }

                if (candidates.Count > 1)
                {
                    throw new Exception("Ambiguous name resolution. Multiple modules contain a type named " + s);
                }
                if (candidates.Count == 1)
                {
                    return(candidates[1]);
                }
            }

            return(null);
        }
Esempio n. 34
0
 /// <summary>
 /// This is private because you should used DotNetObject.Marshal instead
 /// </summary>
 /// <param name="obj"></param>
 private DotNetObject(Object obj)
 {
     this.obj = obj;
     type     = DotNetClass.Create(this.obj.GetType());
 }
Esempio n. 35
0
 static void AddType(HeronType prim)
 {
     Debug.Assert(prim != null);
     types.Add(prim.name, prim);
 }
Esempio n. 36
0
 static void AppendFields(StringBuilder sb, HeronType t)
 {
     sb.AppendLine("  fields");
     sb.AppendLine("  {");
     foreach (ExposedField f in t.GetExposedFields())
         sb.AppendLine("    " + f.ToString() + ";");             
     sb.AppendLine("  }");
 }
Esempio n. 37
0
 public void AddPrimitive(string s, HeronType t)
 {
     if (FindType(s) != null)
         throw new Exception("Type '" + s + "' already exists");
     types.Add(s, t);
 }
Esempio n. 38
0
 static void AppendMethods(StringBuilder sb, HeronType t)
 {
     sb.AppendLine("  methods");
     sb.AppendLine("  {");
     foreach (ExposedMethodValue m in t.GetExposedMethods())
         sb.AppendLine("    " + m.ToString() + ";");
     sb.AppendLine("  }");
 }
Esempio n. 39
0
 public override HeronValue As(HeronType t)
 {
     InterfaceDefn id = t as InterfaceDefn;
     if (id == null)
         return null;
     if (hinterface.Implements(id))
         return new InterfaceInstance(obj, id);
     return null;
 }
Esempio n. 40
0
        /// <summary>
        /// Used to cast the class instance to its base class or an interface.
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public override HeronValue As(HeronType t)
        {
            if (t is ClassDefn)
            {
                ClassDefn c1 = cls;
                ClassDefn c2 = t as ClassDefn;

                if (c2.name == c1.name)
                    return this;

                if (GetBase() == null)
                    return null;

                return GetBase().As(t);
            }
            else if (t is InterfaceDefn)
            {
                if (cls.Implements(t as InterfaceDefn))
                    return new InterfaceInstance(this, t as InterfaceDefn);

                return null;
            }
            else if (t == PrimitiveTypes.AnyType)
            {
                return new AnyValue(this);
            }
            else if (t == PrimitiveTypes.UnknownType)
            {
                return this;
            }
            return null;
        }
Esempio n. 41
0
 public TypeRef(HeronType t, bool nullable) : this(t.name, nullable) { type = t; }
Esempio n. 42
0
 public TypeRef(HeronType t) : this(t.name) { type = t; }
Esempio n. 43
0
 /// <summary>
 /// This is private because you should used DotNetObject.Marshal instead
 /// </summary>
 /// <param name="obj"></param>
 private DotNetObject(Object obj)
 {
     this.obj = obj;
     type = DotNetClass.Create(this.obj.GetType());
 }
 public ListToIterValue(IEnumerable <HeronValue> iter, HeronType elementType)
 {
     list             = new List <HeronValue>(iter);
     current          = 0;
     this.elementType = elementType;
 }
Esempio n. 45
0
 public bool IsAssignableFrom(HeronType type)
 {
     if (Equals(type)) 
         return true;
     if (type.baseType != null)
         return IsAssignableFrom(type.baseType);
     else
         return false;
 }