/// <summary> /// Create underlying FieldReference in assembly /// </summary> /// <param name="field">Field node</param> public void PrepareField(FieldNode field) { // calculate required attributes var attribs = FieldAttributes.Public; if (field.Static) attribs |= FieldAttributes.Static; // create field in assembly var nativeField = new FieldDefinition(field.Name, attribs, ResolveType(field.Type)); field.Field = nativeField; ((TypeDefinition)field.Owner.Type).Fields.Add(nativeField); }
/// <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; }
/// <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> /// Save a field /// </summary> /// <param name="field">Field node</param> public void EmitSaveField(FieldNode field) { var mp = GetMethodProcessor(); if (field.Static) mp.Emit(OpCodes.Stsfld, field.Field); else mp.Emit(OpCodes.Stfld, field.Field); }