// // Emits the code // public void Emit (TypeContainer parent) { ILGenerator ig = ConstructorBuilder.GetILGenerator (); EmitContext ec = new EmitContext (parent, Location, ig, null, ModFlags, true); // // extern methods have no bodies // if ((ModFlags & Modifiers.EXTERN) != 0) { if ((block != null) && ((ModFlags & Modifiers.EXTERN) != 0)) { Report.Error ( 179, Location, "External constructor `" + TypeManager.CSharpSignature (ConstructorBuilder) + "' can not have a body"); return; } } else if (block == null) { Report.Error ( 501, Location, "Constructor `" + TypeManager.CSharpSignature (ConstructorBuilder) + "' must declare a body since it is not marked extern"); return; } if ((ModFlags & Modifiers.STATIC) == 0){ if (parent is Class && Initializer == null) Initializer = new ConstructorBaseInitializer ( null, Parameters.EmptyReadOnlyParameters, Location.Null); // // Spec mandates that Initializers will not have // `this' access // ec.IsStatic = true; if (Initializer != null && !Initializer.Resolve (ec)) return; ec.IsStatic = false; } LabelParameters (ec, ConstructorBuilder, OptAttributes); SymbolWriter sw = CodeGen.SymbolWriter; bool generate_debugging = false; if ((sw != null) && (block != null) && !Location.IsNull (Location) && !Location.IsNull (block.EndLocation)) { sw.OpenMethod (parent, ConstructorBuilder, Location, block.EndLocation); generate_debugging = true; } // // Classes can have base initializers and instance field initializers. // if (parent is Class){ if ((ModFlags & Modifiers.STATIC) == 0) parent.EmitFieldInitializers (ec); } if (Initializer != null) Initializer.Emit (ec); if ((ModFlags & Modifiers.STATIC) != 0) parent.EmitFieldInitializers (ec); Attribute.ApplyAttributes (ec, ConstructorBuilder, this, OptAttributes); // If this is a non-static `struct' constructor and doesn't have any // initializer, it must initialize all of the struct's fields. if ((parent is Struct) && ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null)) Block.AddThisVariable (parent, Location); ec.EmitTopBlock (block, ParameterInfo, Location); if (generate_debugging) sw.CloseMethod (); block = null; }