// // Emits the code // public override void Emit () { if (Parent.PartialContainer.IsComImport) { if (!IsDefault ()) { Report.Error (669, Location, "`{0}': A class with the ComImport attribute cannot have a user-defined constructor", Parent.GetSignatureForError ()); } // Set as internal implementation and reset block data // to ensure no IL is generated ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall); block = null; } if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0) Module.PredefinedAttributes.DebuggerHidden.EmitAttribute (ConstructorBuilder); if (OptAttributes != null) OptAttributes.Emit (); base.Emit (); parameters.ApplyAttributes (this, ConstructorBuilder); BlockContext bc = new BlockContext (this, block, Compiler.BuiltinTypes.Void); bc.Set (ResolveContext.Options.ConstructorScope); if (block != null) { // // If we use a "this (...)" constructor initializer, then // do not emit field initializers, they are initialized in the other constructor // if (!(Initializer is ConstructorThisInitializer)) Parent.PartialContainer.ResolveFieldInitializers (bc); if (!IsStatic) { if (Initializer == null) { if (Parent.PartialContainer.Kind == MemberKind.Struct) { // // If this is a non-static `struct' constructor and doesn't have any // initializer, it must initialize all of the struct's fields. // block.AddThisVariable (bc); } else if (Parent.PartialContainer.Kind == MemberKind.Class) { Initializer = new GeneratedBaseInitializer (Location); } } if (Initializer != null && !(bc.FileType == SourceFileType.PlayScript && Initializer.IsAsExplicitSuperCall)) { // // mdb format does not support reqions. Try to workaround this by emitting the // sequence point at initializer. Any breakpoint at constructor header should // be adjusted to this sequence point as it's the next one which follows. // block.AddScopeStatement (new StatementExpression (Initializer)); } } if (block.Resolve (null, bc, this)) { debug_builder = Parent.CreateMethodSymbolEntry (); EmitContext ec = new EmitContext (this, ConstructorBuilder.GetILGenerator (), bc.ReturnType, debug_builder); ec.With (EmitContext.Options.ConstructorScope, true); block.Emit (ec); } } if (declarative_security != null) { foreach (var de in declarative_security) { #if STATIC ConstructorBuilder.__AddDeclarativeSecurity (de); #else ConstructorBuilder.AddDeclarativeSecurity (de.Key, de.Value); #endif } } block = null; }
public override void EmitJs (JsEmitContext jec) { base.EmitJs (jec); bool is_static = (this.ModFlags & Modifiers.STATIC) != 0; if (!is_static) { jec.Buf.Write ("\tfunction " + this.Parent.MemberName.Name + "(", Location); parameters.EmitJs (jec); jec.Buf.Write (") "); } BlockContext bc = new BlockContext (this, block, Compiler.BuiltinTypes.Void); bc.Set (ResolveContext.Options.ConstructorScope); bool emitted_block = false; if (block != null) { // // If we use a "this (...)" constructor initializer, then // do not emit field initializers, they are initialized in the other constructor // if (!(Initializer is ConstructorThisInitializer)) Parent.PartialContainer.ResolveFieldInitializers (bc); if (!IsStatic) { if (Initializer == null) { if (Parent.PartialContainer.Kind == MemberKind.Struct) { // // If this is a non-static `struct' constructor and doesn't have any // initializer, it must initialize all of the struct's fields. // block.AddThisVariable (bc); } else if (Parent.PartialContainer.Kind == MemberKind.Class) { Initializer = new GeneratedBaseInitializer (Location); } } if (Initializer != null && !(bc.FileType == SourceFileType.PlayScript && Initializer.IsAsExplicitSuperCall)) { // // mdb format does not support reqions. Try to workaround this by emitting the // sequence point at initializer. Any breakpoint at constructor header should // be adjusted to this sequence point as it's the next one which follows. // block.AddScopeStatement (new StatementExpression (Initializer)); } } if (block.Resolve (null, bc, this)) { block.EmitBlockJs (jec, false, is_static); emitted_block = true; } } if (!is_static) { if (!emitted_block) jec.Buf.Write ("{\n\t}", Location); jec.Buf.Write ("\n"); } block = null; }
public override void Visit (Constructor c) { ConstructorDeclaration newConstructor = new ConstructorDeclaration (); AddAttributeSection (newConstructor, c); var location = LocationsBag.GetMemberLocation (c); AddModifiers (newConstructor, location); newConstructor.AddChild (Identifier.Create (c.MemberName.Name, Convert (c.MemberName.Location)), AstNode.Roles.Identifier); if (location != null) newConstructor.AddChild (new CSharpTokenNode (Convert (location[0]), 1), MethodDeclaration.Roles.LPar); AddParameter (newConstructor, c.ParameterInfo); if (location != null) newConstructor.AddChild (new CSharpTokenNode (Convert (location[1]), 1), MethodDeclaration.Roles.RPar); if (c.Initializer != null) { var initializer = new ConstructorInitializer (); initializer.ConstructorInitializerType = c.Initializer is ConstructorBaseInitializer ? ConstructorInitializerType.Base : ConstructorInitializerType.This; var initializerLocation = LocationsBag.GetLocations (c.Initializer); if (initializerLocation != null) newConstructor.AddChild (new CSharpTokenNode (Convert (initializerLocation[0]), 1), ConstructorDeclaration.Roles.Colon); // this and base has the same length initializer.AddChild (new CSharpTokenNode (Convert (c.Initializer.Location), "this".Length), ConstructorDeclaration.Roles.Keyword); if (initializerLocation != null) initializer.AddChild (new CSharpTokenNode (Convert (initializerLocation[1]), 1), ConstructorDeclaration.Roles.LPar); AddArguments (initializer, LocationsBag.GetLocations (c.Initializer.Arguments), c.Initializer.Arguments); if (initializerLocation != null) initializer.AddChild (new CSharpTokenNode (Convert (initializerLocation[2]), 1), ConstructorDeclaration.Roles.RPar); newConstructor.AddChild (initializer, ConstructorDeclaration.InitializerRole); } if (c.Block != null) newConstructor.AddChild ((BlockStatement)c.Block.Accept (this), ConstructorDeclaration.Roles.Body); typeStack.Peek ().AddChild (newConstructor, TypeDeclaration.MemberRole); }
public override void Visit(Constructor c) { var newConstructor = new ConstructorDeclaration(); AddAttributeSection(newConstructor, c); var location = LocationsBag.GetMemberLocation(c); AddModifiers(newConstructor, location); newConstructor.AddChild(Identifier.Create(c.MemberName.Name, Convert(c.MemberName.Location)), Roles.Identifier); if (location != null && location.Count > 0) newConstructor.AddChild(new CSharpTokenNode(Convert(location [0]), Roles.LPar), Roles.LPar); AddParameter(newConstructor, c.ParameterInfo); if (location != null && location.Count > 1) newConstructor.AddChild(new CSharpTokenNode(Convert(location [1]), Roles.RPar), Roles.RPar); if (c.Initializer != null) { var initializer = new ConstructorInitializer(); initializer.ConstructorInitializerType = c.Initializer is ConstructorBaseInitializer ? ConstructorInitializerType.Base : ConstructorInitializerType.This; var initializerLocation = LocationsBag.GetLocations(c.Initializer); if (initializerLocation != null) newConstructor.AddChild(new CSharpTokenNode(Convert(initializerLocation [0]), Roles.Colon), Roles.Colon); if (initializerLocation != null && initializerLocation.Count > 1) { // this and base has the same length var r = initializer.ConstructorInitializerType == ConstructorInitializerType.This ? ConstructorInitializer.ThisKeywordRole : ConstructorInitializer.BaseKeywordRole; initializer.AddChild(new CSharpTokenNode(Convert(c.Initializer.Location), r), r); initializer.AddChild(new CSharpTokenNode(Convert(initializerLocation [1]), Roles.LPar), Roles.LPar); AddArguments(initializer, c.Initializer.Arguments); initializer.AddChild(new CSharpTokenNode(Convert(initializerLocation [2]), Roles.RPar), Roles.RPar); newConstructor.AddChild(initializer, ConstructorDeclaration.InitializerRole); } } if (c.Block != null) newConstructor.AddChild((BlockStatement)c.Block.Accept(this), Roles.Body); typeStack.Peek().AddChild(newConstructor, Roles.TypeMemberRole); }
// // Emits the code // public override void Emit () { if (Parent.PartialContainer.IsComImport) { if (!IsDefault ()) { Report.Error (669, Location, "`{0}': A class with the ComImport attribute cannot have a user-defined constructor", Parent.GetSignatureForError ()); } ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall); } if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0) Compiler.PredefinedAttributes.DebuggerHidden.EmitAttribute (ConstructorBuilder); if (OptAttributes != null) OptAttributes.Emit (); base.Emit (); // // If we use a "this (...)" constructor initializer, then // do not emit field initializers, they are initialized in the other constructor // bool emit_field_initializers = ((ModFlags & Modifiers.STATIC) != 0) || !(Initializer is ConstructorThisInitializer); BlockContext bc = new BlockContext (this, block, TypeManager.void_type); bc.Set (ResolveContext.Options.ConstructorScope); if (emit_field_initializers) Parent.PartialContainer.ResolveFieldInitializers (bc); if (block != null) { // 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.PartialContainer.Kind == MemberKind.Struct) && ((ModFlags & Modifiers.STATIC) == 0) && (Initializer == null)) block.AddThisVariable (bc, Parent, Location); if (block != null && (ModFlags & Modifiers.STATIC) == 0){ if (Parent.PartialContainer.Kind == MemberKind.Class && Initializer == null) Initializer = new GeneratedBaseInitializer (Location); if (Initializer != null) { block.AddScopeStatement (new StatementExpression (Initializer)); } } } parameters.ApplyAttributes (this, ConstructorBuilder); SourceMethod source = SourceMethod.Create (Parent, ConstructorBuilder, block); if (block != null) { if (block.Resolve (null, bc, this)) { EmitContext ec = new EmitContext (this, ConstructorBuilder.GetILGenerator (), bc.ReturnType); ec.With (EmitContext.Options.ConstructorScope, true); if (!ec.HasReturnLabel && bc.HasReturnLabel) { ec.ReturnLabel = bc.ReturnLabel; ec.HasReturnLabel = true; } block.Emit (ec); } } if (source != null) source.CloseMethod (); if (declarative_security != null) { foreach (var de in declarative_security) { ConstructorBuilder.AddDeclarativeSecurity (de.Key, de.Value); } } block = null; }
// // The spec claims that static is not permitted, but // my very own code has static constructors. // public Constructor (DeclSpace parent, string name, Modifiers mod, Attributes attrs, ParametersCompiled args, ConstructorInitializer init, Location loc) : base (parent, null, null, mod, AllowedModifiers, new MemberName (name, loc), attrs, args) { Initializer = init; }
// // Emits the code // public override void Emit () { if (Parent.PartialContainer.IsComImport) { if (!IsDefault ()) { Report.Error (669, Location, "`{0}': A class with the ComImport attribute cannot have a user-defined constructor", Parent.GetSignatureForError ()); } // Set as internal implementation and reset block data // to ensure no IL is generated ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.InternalCall); block = null; } if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0) Module.PredefinedAttributes.DebuggerHidden.EmitAttribute (ConstructorBuilder); if (OptAttributes != null) OptAttributes.Emit (); base.Emit (); parameters.ApplyAttributes (this, ConstructorBuilder); BlockContext bc = new BlockContext (this, block, Compiler.BuiltinTypes.Void); bc.Set (ResolveContext.Options.ConstructorScope); // // If we use a "this (...)" constructor initializer, then // do not emit field initializers, they are initialized in the other constructor // if (!(Initializer is ConstructorThisInitializer)) Parent.PartialContainer.ResolveFieldInitializers (bc); if (block != null) { if (!IsStatic) { if (Initializer == null) { if (Parent.PartialContainer.Kind == MemberKind.Struct) { // // If this is a non-static `struct' constructor and doesn't have any // initializer, it must initialize all of the struct's fields. // block.AddThisVariable (bc); } else if (Parent.PartialContainer.Kind == MemberKind.Class) { Initializer = new GeneratedBaseInitializer (Location); } } if (Initializer != null) { // // Use location of the constructor to emit sequence point of initializers // at beginning of constructor name // // TODO: Need to extend mdb to support line regions to allow set a breakpoint at // initializer // block.AddScopeStatement (new StatementExpression (Initializer, Location)); } } if (block.Resolve (null, bc, this)) { EmitContext ec = new EmitContext (this, ConstructorBuilder.GetILGenerator (), bc.ReturnType); ec.With (EmitContext.Options.ConstructorScope, true); SourceMethod source = SourceMethod.Create (Parent, ConstructorBuilder); block.Emit (ec); if (source != null) source.CloseMethod (); } } if (declarative_security != null) { foreach (var de in declarative_security) { #if STATIC ConstructorBuilder.__AddDeclarativeSecurity (de); #else ConstructorBuilder.AddDeclarativeSecurity (de.Key, de.Value); #endif } } block = null; }
// // 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; }
// // The spec claims that static is not permitted, but // my very own code has static constructors. // public Constructor (string name, Parameters args, ConstructorInitializer init, Location l) : base (null, 0, AllowedModifiers, name, null, args, l) { Initializer = init; }