public FieldNode(string name, SignatureNode type, bool isStatic = false, FieldReference field = null) { Name = name; Field = field; Type = type; Static = isStatic; }
public MethodNode(string name, SignatureNode type, bool isStatic = false, bool builtIn = false, MethodReference method = null) { Name = name; Type = type; Static = isStatic; Method = method; BuiltIn = builtIn; }
public ParameterNode(string name, SignatureNode type, int id) { Name = name; Type = type; Id = id; }
/// <summary> /// Resolve a type in a SignatureNode /// </summary> /// <param name="node">Signature node</param> /// <returns></returns> public TypeReference ResolveType(SignatureNode node) { node.CompiledType = ResolveType(node.Signature); return node.CompiledType; }
/// <summary> /// Create a method in a type /// </summary> /// <param name="owner">Owner type node</param> /// <param name="name">Method name</param> /// <param name="type">Method return value type signature</param> /// <param name="parameters">Method parameters list</param> /// <param name="isStatic">Static flag</param> /// <returns></returns> public MethodNode CreateMethod(TypeNode owner, string name, SignatureNode type, HashList<ParameterNode> parameters = null, bool isStatic = false, bool prepare = false) { // cannot add methods to built-in types if (owner.BuiltIn) throw new CompilerException(String.Format(Resources.errExtendBuiltInType, owner.Name)); // create method in assembly and in Mirelle Registry var method = new MethodNode(name, type, isStatic); method.SetParameters(parameters); // check if an identical method hasn't already been declared if (owner.Methods.Contains(method.Name)) { if(owner.Methods[method.Name].Exists(curr => curr.Signature == method.Signature)) throw new CompilerException(String.Format(Resources.errMethodRedefinition, method.Name, owner.Name)); } // register method in the parent type AddMethod(owner, method); method.Owner = owner; if (prepare) PrepareMethod(method); return method; }
/// <summary> /// Create a field in a type /// </summary> /// <param name="owner">Owner type node</param> /// <param name="name">Field name</param> /// <param name="type">Field type signature</param> /// <param name="isStatic">Static flag</param> /// <returns></returns> public FieldNode CreateField(TypeNode owner, string name, SignatureNode type, bool isStatic = false) { // cut the atmark if(name[0] == '@') name = name.SafeSubstring(1, name.Length - 1); // cannot add fields to built-in types if (owner.BuiltIn) throw new CompilerException(String.Format(Resources.errExtendBuiltInType, owner.Name)); // check if such a field has already been registered if (owner.Fields.Contains(name)) throw new CompilerException(String.Format(Resources.errFieldRedefinition, name, owner.Name)); // create field in Mirelle registry var field = new FieldNode(name, type, isStatic); owner.Fields.Add(name, field); field.Owner = owner; return field; }
/// <summary> /// Import a method from any assembly into Mirelle registry /// </summary> /// <param name="baseType">Actual type</param> /// <param name="baseName">Method name within actual type</param> /// <param name="owner">Owner type name within Mirelle</param> /// <param name="name">Method name within mirelle</param> /// <param name="type">Type</param> /// <param name="isStatic">Static flag</param> /// <param name="parameters">Parameter list</param> /// <returns></returns> public MethodNode ImportMethod(Type baseType, string baseName, string owner, string name, string type, bool isStatic = false, params string[] parameters) { var ownerType = FindType(owner); if (ownerType == null) throw new CompilerException(String.Format(Resources.errTypeNotFound, owner)); // find method in the base type and import it var types = new Type[parameters.Length]; var idx = 0; foreach (var curr in parameters) { types[idx] = ResolveBasicType(curr); idx++; } MethodReference importedMethod; if(baseName == ".ctor") importedMethod = AssemblyImport(baseType.GetConstructor(types)); else importedMethod = AssemblyImport(baseType.GetMethod(baseName, types)); var typeNode = new SignatureNode(type); ResolveType(typeNode); var method = new MethodNode(name, typeNode, isStatic, true, importedMethod); // parameters var sb = new StringBuilder(name); idx = 0; foreach (var curr in parameters) { // declared as extension method ? // first parameter is the invoker itself, remove it from parameters list if (!importedMethod.HasThis && !isStatic && idx == 0) { idx++; continue; } var signNode = new SignatureNode(curr); sb.Append(" "); sb.Append(curr); var param = new ParameterNode("p" + idx.ToString(), signNode, idx); method.Parameters.Add(param.Name, param); idx++; } method.Signature = sb.ToString(); // check if an identical method hasn't already been declared if (ownerType.Methods.Contains(method.Signature)) throw new CompilerException(String.Format(Resources.errMethodRedefinition, method.Name, owner)); // register method in the owner type AddMethod(ownerType, method); method.Owner = ownerType; return method; }
/// <summary> /// Import a field /// </summary> /// <param name="baseType">Actual type</param> /// <param name="baseName">Name of the field in actual type</param> /// <param name="owner">Owner type name within Mirelle</param> /// <param name="name">Field name within Mirelle</param> /// <param name="type">Field value type</param> /// <param name="isStatic">Static flag</param> /// <returns></returns> public FieldNode ImportField(Type baseType, string baseName, string owner, string name, string type, bool isStatic = false) { // check if type exists var ownerType = FindType(owner); if (ownerType == null) throw new CompilerException(String.Format(Resources.errTypeNotFound, owner)); // check if field is not yet defined try { FindField(owner, name); throw new CompilerException(String.Format(Resources.errFieldRedefinition, name, owner)); } catch { } var importedField = AssemblyImport(baseType.GetField(baseName)); var typeNode = new SignatureNode(type); ResolveType(typeNode); var field = new FieldNode(name, typeNode, isStatic, importedField); ownerType.Fields.Add(name, field); return field; }