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); } }
/// <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); }
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(); }
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; }
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; }
static void AppendFields(StringBuilder sb, HeronType t) { sb.AppendLine(" fields"); sb.AppendLine(" {"); foreach (ExposedField f in t.GetExposedFields()) { sb.AppendLine(" " + f.ToString() + ";"); } sb.AppendLine(" }"); }
static void AppendMethods(StringBuilder sb, HeronType t) { sb.AppendLine(" methods"); sb.AppendLine(" {"); foreach (ExposedMethodValue m in t.GetExposedMethods()) { sb.AppendLine(" " + m.ToString() + ";"); } sb.AppendLine(" }"); }
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); }
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); }
// 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()); } } }
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); }
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; } }
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); }
public bool IsAssignableFrom(HeronType type) { if (Equals(type)) { return(true); } if (type.baseType != null) { return(IsAssignableFrom(type.baseType)); } else { return(false); } }
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); } }
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); }
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)); }
/// <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); }
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); }
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); }
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); }
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; }
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); }
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); } }
/// <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); }
public CodeModelType(HeronType basetype, Type t) : base(basetype, null, t, t.Name) { }
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); }
/// <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()); }
static void AddType(HeronType prim) { Debug.Assert(prim != null); types.Add(prim.name, prim); }
static void AppendFields(StringBuilder sb, HeronType t) { sb.AppendLine(" fields"); sb.AppendLine(" {"); foreach (ExposedField f in t.GetExposedFields()) sb.AppendLine(" " + f.ToString() + ";"); sb.AppendLine(" }"); }
public void AddPrimitive(string s, HeronType t) { if (FindType(s) != null) throw new Exception("Type '" + s + "' already exists"); types.Add(s, t); }
static void AppendMethods(StringBuilder sb, HeronType t) { sb.AppendLine(" methods"); sb.AppendLine(" {"); foreach (ExposedMethodValue m in t.GetExposedMethods()) sb.AppendLine(" " + m.ToString() + ";"); sb.AppendLine(" }"); }
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; }
/// <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; }
public TypeRef(HeronType t, bool nullable) : this(t.name, nullable) { type = t; }
public TypeRef(HeronType t) : this(t.name) { type = t; }
public ListToIterValue(IEnumerable <HeronValue> iter, HeronType elementType) { list = new List <HeronValue>(iter); current = 0; this.elementType = elementType; }
public bool IsAssignableFrom(HeronType type) { if (Equals(type)) return true; if (type.baseType != null) return IsAssignableFrom(type.baseType); else return false; }