public override void Visit (Constructor c) { ConstructorDeclaration newConstructor = new ConstructorDeclaration (); var location = LocationsBag.GetMemberLocation (c); AddModifiers (newConstructor, location); newConstructor.AddChild (new Identifier (c.MemberName.Name, Convert (c.MemberName.Location)), AbstractNode.Roles.Identifier); if (location != null) { newConstructor.AddChild (new CSharpTokenNode (Convert (location[0]), 1), MethodDeclaration.Roles.LPar); newConstructor.AddChild (new CSharpTokenNode (Convert (location[1]), 1), MethodDeclaration.Roles.RPar); } if (c.Block != null) newConstructor.AddChild ((INode)c.Block.Accept (this), ConstructorDeclaration.Roles.Body); typeStack.Peek ().AddChild (newConstructor, TypeDeclaration.Roles.Member); }
/// <summary> /// Defines the default constructors /// </summary> protected virtual Constructor DefineDefaultConstructor (bool is_static) { // The default instance constructor is public // If the class is abstract, the default constructor is protected // The default static constructor is private Modifiers mods; ParametersCompiled parameters = null; if (is_static) { mods = Modifiers.STATIC | Modifiers.PRIVATE; parameters = ParametersCompiled.EmptyReadOnlyParameters; } else { mods = ((ModFlags & Modifiers.ABSTRACT) != 0) ? Modifiers.PROTECTED : Modifiers.PUBLIC; parameters = PrimaryConstructorParameters ?? ParametersCompiled.EmptyReadOnlyParameters; } var c = new Constructor (this, MemberName.Name, mods, null, parameters, Location); if (Kind == MemberKind.Class) c.Initializer = new GeneratedBaseInitializer (Location, PrimaryConstructorBaseArguments); if (PrimaryConstructorParameters != null && !is_static) { c.IsPrimaryConstructor = true; c.caching_flags |= Flags.MethodOverloadsExist; } AddConstructor (c, true); if (PrimaryConstructorBlock == null) { c.Block = new ToplevelBlock (Compiler, parameters, Location) { IsCompilerGenerated = true }; } else { c.Block = PrimaryConstructorBlock; } return c; }
public override void Visit (Constructor c) { DomMethod method = new DomMethod (); method.Name = ".ctor"; method.Documentation = RetrieveDocumentation (c.Location.Row); method.Location = Convert (c.MemberName.Location); method.Modifiers = ConvertModifiers (c.ModFlags); if (c.Block != null) { var location = LocationsBag.GetMemberLocation (c); var region = ConvertRegion (location != null ? location[1] : c.Block.StartLocation, c.Block.EndLocation); if (location != null) region.Start = new DomLocation (region.Start.Line, region.Start.Column + 1); method.BodyRegion = region; } method.Modifiers = ConvertModifiers (c.ModFlags) | MonoDevelop.Projects.Dom.Modifiers.SpecialName; method.MethodModifier |= MethodModifier.IsConstructor; AddAttributes (method, c.OptAttributes, c); AddParameter (method, c.ParameterInfo); AddExplicitInterfaces (method, c); method.DeclaringType = typeStack.Peek (); typeStack.Peek ().Add (method); }
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); }
public void AddConstructor (Constructor c, bool isDefault) { bool is_static = (c.ModFlags & Modifiers.STATIC) != 0; if (!isDefault) AddNameToContainer (c, is_static ? Constructor.TypeConstructorName : Constructor.ConstructorName); if (is_static && c.ParameterInfo.IsEmpty) { PartialContainer.has_static_constructor = true; } else { PartialContainer.HasInstanceConstructor = true; } members.Add (c); }
public override void Visit (Constructor c) { ConstructorDeclaration newConstructor = new ConstructorDeclaration (); var location = LocationsBag.GetMemberLocation (c); AddModifiers (newConstructor, location); newConstructor.AddChild (new Identifier (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) { newConstructor.Initializer = new ConstructorInitializer(); if (c.Initializer is ConstructorBaseInitializer) newConstructor.Initializer.ConstructorInitializerType = ConstructorInitializerType.Base; if (c.Initializer is ConstructorThisInitializer) newConstructor.Initializer.ConstructorInitializerType = ConstructorInitializerType.This; AddArguments(newConstructor.Initializer, location, c.Initializer.Arguments); } if (c.Block != null) newConstructor.AddChild ((BlockStatement)c.Block.Accept (this), ConstructorDeclaration.Roles.Body); typeStack.Peek ().AddChild (newConstructor, TypeDeclaration.MemberRole); }
void case_274() #line 2276 "cs-parser.jay" { valid_param_mod = 0; current_local_parameters = (ParametersCompiled) yyVals[-1+yyTop]; var lt = (LocatedToken) yyVals[-4+yyTop]; var mods = (Modifiers) yyVals[-5+yyTop]; var c = new Constructor (current_type, lt.Value, mods, (Attributes) yyVals[-6+yyTop], current_local_parameters, lt.Location); if (lt.Value != current_container.MemberName.Name) { report.Error (1520, c.Location, "Class, struct, or interface method must have a return type"); } else if ((mods & Modifiers.STATIC) != 0) { if ((mods & Modifiers.AccessibilityMask) != 0){ report.Error (515, c.Location, "`{0}': static constructor cannot have an access modifier", c.GetSignatureForError ()); } } current_type.AddConstructor (c); lbag.AddMember (c, GetModifierLocations (), GetLocation (yyVals[-2+yyTop]), GetLocation (yyVals[0+yyTop])); yyVal = c; /**/ /* start block here, so possible anonymous methods inside*/ /* constructor initializer can get correct parent block*/ /**/ start_block (lexer.Location); } void case_275() #line 2305 "cs-parser.jay" { if (yyVals[0+yyTop] != null) { var c = (Constructor) yyVals[-1+yyTop]; c.Initializer = (ConstructorInitializer) yyVals[0+yyTop]; if (c.IsStatic) { report.Error (514, c.Location, "`{0}': static constructor cannot have an explicit `this' or `base' constructor call", c.GetSignatureForError ()); } } yyVal = yyVals[-1+yyTop]; } void case_281() #line 2337 "cs-parser.jay" { --lexer.parsing_block; yyVal = new ConstructorBaseInitializer ((Arguments) yyVals[-1+yyTop], GetLocation (yyVals[-4+yyTop])); lbag.AddLocation (yyVal, GetLocation (yyVals[-5+yyTop]), GetLocation (yyVals[-3+yyTop]), GetLocation (yyVals[0+yyTop])); } void case_283() #line 2347 "cs-parser.jay" { --lexer.parsing_block; yyVal = new ConstructorThisInitializer ((Arguments) yyVals[-1+yyTop], GetLocation (yyVals[-4+yyTop])); lbag.AddLocation (yyVal, GetLocation (yyVals[-5+yyTop]), GetLocation (yyVals[-3+yyTop]), GetLocation (yyVals[0+yyTop])); } void case_284() #line 2353 "cs-parser.jay" { Error_SyntaxError (yyToken); yyVal = new ConstructorThisInitializer (null, GetLocation (yyVals[0+yyTop])); lbag.AddLocation (yyVal, GetLocation (yyVals[-1+yyTop])); } void case_285() #line 2359 "cs-parser.jay" { Error_SyntaxError (yyToken); yyVal = null; } void case_286() #line 2367 "cs-parser.jay" { if (doc_support) { tmpComment = Lexer.consume_doc_comment (); Lexer.doc_state = XmlCommentState.NotAllowed; } current_local_parameters = ParametersCompiled.EmptyReadOnlyParameters; } void case_287() #line 2376 "cs-parser.jay" { var lt = (LocatedToken) yyVals[-3+yyTop]; if (lt.Value != current_container.MemberName.Name){ report.Error (574, lt.Location, "Name of destructor must match name of class"); } else if (current_container.Kind != MemberKind.Class){ report.Error (575, lt.Location, "Only class types can contain destructor"); } Destructor d = new Destructor (current_type, (Modifiers) yyVals[-6+yyTop], ParametersCompiled.EmptyReadOnlyParameters, (Attributes) yyVals[-7+yyTop], lt.Location); d.Identifier = lt.Value; if (doc_support) d.DocComment = ConsumeStoredComment (); d.Block = (ToplevelBlock) yyVals[0+yyTop]; current_type.AddMember (d); lbag.AddMember (d, GetModifierLocations (), GetLocation (yyVals[-5+yyTop]), GetLocation (yyVals[-2+yyTop]), GetLocation (yyVals[-1+yyTop])); current_local_parameters = null; } void case_288() #line 2402 "cs-parser.jay" { current_event_field = new EventField (current_type, (FullNamedExpression) yyVals[-1+yyTop], (Modifiers) yyVals[-3+yyTop], (MemberName) yyVals[0+yyTop], (Attributes) yyVals[-4+yyTop]); current_type.AddMember (current_event_field); if (current_event_field.MemberName.ExplicitInterface != null) { report.Error (71, current_event_field.Location, "`{0}': An explicit interface implementation of an event must use property syntax", current_event_field.GetSignatureForError ()); } yyVal = current_event_field; } void case_289() #line 2416 "cs-parser.jay" { if (doc_support) { current_event_field.DocComment = Lexer.consume_doc_comment (); Lexer.doc_state = XmlCommentState.Allowed; } if (current_event_field.Initializer != null) { lbag.AddMember (current_event_field, GetModifierLocations (), GetLocation (yyVals[-6+yyTop]), savedEventAssignLocation, GetLocation (yyVals[0+yyTop])); } else { lbag.AddMember (current_event_field, GetModifierLocations (), GetLocation (yyVals[-6+yyTop]), GetLocation (yyVals[0+yyTop])); } current_event_field = null; } void case_290() #line 2432 "cs-parser.jay" { current_event = new EventProperty (current_type, (FullNamedExpression) yyVals[-2+yyTop], (Modifiers) yyVals[-4+yyTop], (MemberName) yyVals[-1+yyTop], (Attributes) yyVals[-5+yyTop]); current_type.AddMember (current_event); lbag.AddMember (current_event, GetModifierLocations (), GetLocation (yyVals[-3+yyTop]), GetLocation (yyVals[0+yyTop])); lexer.EventParsing = true; } void case_291() #line 2440 "cs-parser.jay" { if (current_container.Kind == MemberKind.Interface) report.Error (69, GetLocation (yyVals[-2+yyTop]), "Event in interface cannot have add or remove accessors"); lexer.EventParsing = false; } void case_292() #line 2447 "cs-parser.jay" { if (doc_support) { current_event.DocComment = Lexer.consume_doc_comment (); Lexer.doc_state = XmlCommentState.Allowed; } lbag.AppendToMember (current_event, GetLocation (yyVals[-1+yyTop])); current_event = null; current_local_parameters = null; } void case_293() #line 2460 "cs-parser.jay" { Error_SyntaxError (yyToken); current_type.AddMember (new EventField (current_type, (FullNamedExpression) yyVals[-1+yyTop], (Modifiers) yyVals[-3+yyTop], MemberName.Null, (Attributes) yyVals[-4+yyTop])); } void case_296() #line 2474 "cs-parser.jay" { --lexer.parsing_block; savedEventAssignLocation = GetLocation (yyVals[-2+yyTop]); current_event_field.Initializer = (Expression) yyVals[0+yyTop]; } void case_301() #line 2499 "cs-parser.jay" { var lt = (LocatedToken) yyVals[0+yyTop]; yyVal = new FieldDeclarator (new SimpleMemberName (lt.Value, lt.Location), null); lbag.AddLocation (yyVal, GetLocation (yyVals[-1+yyTop])); } void case_303() #line 2509 "cs-parser.jay" { --lexer.parsing_block; var lt = (LocatedToken) yyVals[-3+yyTop]; yyVal = new FieldDeclarator (new SimpleMemberName (lt.Value, lt.Location), (Expression) yyVals[0+yyTop]); lbag.AddLocation (yyVal, GetLocation (yyVals[-4+yyTop]), GetLocation (yyVals[-2+yyTop])); } void case_304() #line 2518 "cs-parser.jay" { if (current_container.Kind == MemberKind.Interface) { report.Error (68, lexer.Location, "`{0}': event in interface cannot have an initializer", current_event_field.GetSignatureForError ()); } if ((current_event_field.ModFlags & Modifiers.ABSTRACT) != 0) { report.Error (74, lexer.Location, "`{0}': abstract event cannot have an initializer", current_event_field.GetSignatureForError ()); } } void case_308() #line 2539 "cs-parser.jay" { report.Error (65, lexer.Location, "`{0}': event property must have both add and remove accessors", current_event.GetSignatureForError ()); } void case_309() #line 2544 "cs-parser.jay" { report.Error (65, lexer.Location, "`{0}': event property must have both add and remove accessors", current_event.GetSignatureForError ()); } void case_310() #line 2549 "cs-parser.jay" { report.Error (1055, GetLocation (yyVals[0+yyTop]), "An add or remove accessor expected"); yyVal = null; } void case_311() #line 2557 "cs-parser.jay" { if (yyVals[-1+yyTop] != ModifierNone) { report.Error (1609, GetLocation (yyVals[-1+yyTop]), "Modifiers cannot be placed on event accessor declarations"); } current_event.Add = new EventProperty.AddDelegateMethod (current_event, (Attributes) yyVals[-2+yyTop], GetLocation (yyVals[0+yyTop])); current_local_parameters = current_event.Add.ParameterInfo; lbag.AddMember (current_event.Add, GetModifierLocations ()); lexer.EventParsing = false; } void case_312() #line 2569 "cs-parser.jay" { lexer.EventParsing = true; current_event.Add.Block = (ToplevelBlock) yyVals[0+yyTop]; if (current_container.Kind == MemberKind.Interface) { report.Error (531, current_event.Add.Block.StartLocation, "`{0}': interface members cannot have a definition", current_event.Add.GetSignatureForError ()); } current_local_parameters = null; } void case_313() #line 2585 "cs-parser.jay" { if (yyVals[-1+yyTop] != ModifierNone) { report.Error (1609, GetLocation (yyVals[-1+yyTop]), "Modifiers cannot be placed on event accessor declarations"); } current_event.Remove = new EventProperty.RemoveDelegateMethod (current_event, (Attributes) yyVals[-2+yyTop], GetLocation (yyVals[0+yyTop])); current_local_parameters = current_event.Remove.ParameterInfo; lbag.AddMember (current_event.Remove, GetModifierLocations ()); lexer.EventParsing = false; } void case_314() #line 2597 "cs-parser.jay" { lexer.EventParsing = true; current_event.Remove.Block = (ToplevelBlock) yyVals[0+yyTop]; if (current_container.Kind == MemberKind.Interface) { report.Error (531, current_event.Remove.Block.StartLocation, "`{0}': interface members cannot have a definition", current_event.Remove.GetSignatureForError ()); } current_local_parameters = null; } void case_315() #line 2613 "cs-parser.jay" { report.Error (73, lexer.Location, "An add or remove accessor must have a body"); yyVal = null; } void case_317() #line 2622 "cs-parser.jay" { current_type.UnattachedAttributes = (Attributes) yyVals[-1+yyTop]; report.Error (1519, GetLocation (yyVals[-1+yyTop]), "An attribute is missing member declaration"); lexer.putback ('}'); } void case_318() #line 2633 "cs-parser.jay" { report.Error (1519, lexer.Location, "Unexpected symbol `}' in class, struct, or interface member declaration"); lexer.putback ('}'); lexer.parsing_generic_declaration = false; FullNamedExpression type = (FullNamedExpression) yyVals[-1+yyTop]; current_field = new Field (current_type, type, (Modifiers) yyVals[-2+yyTop], MemberName.Null, (Attributes) yyVals[-3+yyTop]); current_type.AddField (current_field); lbag.AddMember (current_field, GetModifierLocations ()); yyVal = current_field; } void case_319() #line 2653 "cs-parser.jay" { if (doc_support) enumTypeComment = Lexer.consume_doc_comment (); } void case_320() #line 2658 "cs-parser.jay" { if (doc_support) Lexer.doc_state = XmlCommentState.Allowed; MemberName name = (MemberName) yyVals[-3+yyTop]; if (name.IsGeneric) { report.Error (1675, name.Location, "Enums cannot have type parameters"); } push_current_container (new Enum (current_container, (FullNamedExpression) yyVals[-2+yyTop], (Modifiers) yyVals[-5+yyTop], name, (Attributes) yyVals[-6+yyTop]), null); if (yyVals[-2+yyTop] != null) { lbag.AddMember (current_container, GetModifierLocations (), GetLocation (yyVals[-4+yyTop]), savedLocation, GetLocation (yyVals[0+yyTop])); } else { lbag.AddMember (current_container, GetModifierLocations (), GetLocation (yyVals[-4+yyTop]), GetLocation (yyVals[0+yyTop])); } } void case_321() #line 2675 "cs-parser.jay" { /* here will be evaluated after CLOSE_BLACE is consumed.*/ if (doc_support) Lexer.doc_state = XmlCommentState.Allowed; }
/// <summary> /// Defines the default constructors /// </summary> protected virtual Constructor DefineDefaultConstructor (bool is_static) { // The default instance constructor is public // If the class is abstract, the default constructor is protected // The default static constructor is private Modifiers mods; if (is_static) { mods = Modifiers.STATIC | Modifiers.PRIVATE; } else { mods = ((ModFlags & Modifiers.ABSTRACT) != 0) ? Modifiers.PROTECTED : Modifiers.PUBLIC; } var c = new Constructor (this, MemberName.Name, mods, null, ParametersCompiled.EmptyReadOnlyParameters, Location); c.Initializer = new GeneratedBaseInitializer (Location); AddConstructor (c, true); c.Block = new ToplevelBlock (Compiler, ParametersCompiled.EmptyReadOnlyParameters, Location) { IsCompilerGenerated = true }; return c; }
public static AnonymousTypeClass Create (CompilerContext ctx, TypeContainer parent, IList<AnonymousTypeParameter> parameters, Location loc) { string name = ClassNamePrefix + types_counter++; SimpleName [] t_args = new SimpleName [parameters.Count]; TypeParameterName [] t_params = new TypeParameterName [parameters.Count]; Parameter [] ctor_params = new Parameter [parameters.Count]; for (int i = 0; i < parameters.Count; ++i) { AnonymousTypeParameter p = (AnonymousTypeParameter) parameters [i]; t_args [i] = new SimpleName ("<" + p.Name + ">__T", p.Location); t_params [i] = new TypeParameterName (t_args [i].Name, null, p.Location); ctor_params [i] = new Parameter (t_args [i], p.Name, 0, null, p.Location); } // // Create generic anonymous type host with generic arguments // named upon properties names // AnonymousTypeClass a_type = new AnonymousTypeClass (parent.NamespaceEntry.SlaveDeclSpace, new MemberName (name, new TypeArguments (t_params), loc), parameters, loc); if (parameters.Count > 0) a_type.SetParameterInfo (null); Constructor c = new Constructor (a_type, name, Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN, null, new AnonymousParameters (ctx, ctor_params), null, loc); c.Block = new ToplevelBlock (ctx, c.ParameterInfo, loc); // // Create fields and contructor body with field initialization // bool error = false; for (int i = 0; i < parameters.Count; ++i) { AnonymousTypeParameter p = (AnonymousTypeParameter) parameters [i]; Field f = new Field (a_type, t_args [i], Modifiers.PRIVATE | Modifiers.READONLY, new MemberName ("<" + p.Name + ">", p.Location), null); if (!a_type.AddField (f)) { error = true; continue; } c.Block.AddStatement (new StatementExpression ( new SimpleAssign (new MemberAccess (new This (p.Location), f.Name), c.Block.GetParameterReference (p.Name, p.Location)))); ToplevelBlock get_block = new ToplevelBlock (ctx, p.Location); get_block.AddStatement (new Return ( new MemberAccess (new This (p.Location), f.Name), p.Location)); Property prop = new Property (a_type, t_args [i], Modifiers.PUBLIC, new MemberName (p.Name, p.Location), null); prop.Get = new Property.GetMethod (prop, 0, null, p.Location); prop.Get.Block = get_block; a_type.AddProperty (prop); } if (error) return null; a_type.AddConstructor (c); return a_type; }
protected override bool DoDefineMembers () { var ctor_parameters = ParametersCompiled.CreateFullyResolved ( new [] { new Parameter (new TypeExpression (TypeManager.object_type, Location), "object", Parameter.Modifier.NONE, null, Location), new Parameter (new TypeExpression (TypeManager.intptr_type, Location), "method", Parameter.Modifier.NONE, null, Location) }, new [] { TypeManager.object_type, TypeManager.intptr_type } ); Constructor = new Constructor (this, System.Reflection.ConstructorInfo.ConstructorName, Modifiers.PUBLIC, null, ctor_parameters, null, Location); Constructor.Define (); // // Here the various methods like Invoke, BeginInvoke etc are defined // // First, call the `out of band' special method for // defining recursively any types we need: // var p = Parameters; if (!p.Resolve (this)) return false; // // Invoke method // // Check accessibility foreach (var partype in p.Types) { if (!IsAccessibleAs (partype)) { Report.SymbolRelatedToPreviousError (partype); Report.Error (59, Location, "Inconsistent accessibility: parameter type `{0}' is less accessible than delegate `{1}'", TypeManager.CSharpName (partype), GetSignatureForError ()); } } ReturnType = ReturnType.ResolveAsTypeTerminal (this, false); if (ReturnType == null) return false; var ret_type = ReturnType.Type; // // We don't have to check any others because they are all // guaranteed to be accessible - they are standard types. // if (!IsAccessibleAs (ret_type)) { Report.SymbolRelatedToPreviousError (ret_type); Report.Error (58, Location, "Inconsistent accessibility: return type `" + TypeManager.CSharpName (ret_type) + "' is less " + "accessible than delegate `" + GetSignatureForError () + "'"); return false; } CheckProtectedModifier (); if (RootContext.StdLib && TypeManager.IsSpecialType (ret_type)) { Method.Error1599 (Location, ret_type, Report); return false; } TypeManager.CheckTypeVariance (ret_type, Variance.Covariant, this); InvokeBuilder = new Method (this, null, ReturnType, MethodModifiers, new MemberName (InvokeMethodName), p, null); InvokeBuilder.Define (); // // Don't emit async method for compiler generated delegates (e.g. dynamic site containers) // if (TypeManager.iasyncresult_type != null && TypeManager.asynccallback_type != null && !IsCompilerGenerated) { DefineAsyncMethods (Parameters.CallingConvention); } return true; }
public virtual void Visit (Constructor c) { }
// // Defines the default constructors // void DefineDefaultConstructor (bool is_static) { Constructor c; int mods = 0; c = new Constructor (Basename, Parameters.EmptyReadOnlyParameters, new ConstructorBaseInitializer ( null, Parameters.EmptyReadOnlyParameters, Location.Null), Location.Null); if (is_static) mods = Modifiers.STATIC; c.ModFlags = mods; AddConstructor (c); c.Block = new Block (null); }
public AdditionResult AddConstructor (Constructor c) { if (c.Name != Basename) return AdditionResult.NotAConstructor; bool is_static = (c.ModFlags & Modifiers.STATIC) != 0; if (is_static){ have_static_constructor = true; if (default_static_constructor != null){ Console.WriteLine ("I have a static constructor already"); Console.WriteLine (" " + default_static_constructor); return AdditionResult.MethodExists; } default_static_constructor = c; } else { if (c.IsDefault ()){ if (default_constructor != null) return AdditionResult.MethodExists; default_constructor = c; } if (instance_constructors == null) instance_constructors = new ArrayList (); instance_constructors.Add (c); } return AdditionResult.Success; }
public virtual void CloseType () { if ((caching_flags & Flags.CloseTypeCreated) != 0) return; // Close base type container first to avoid TypeLoadException if (spec.BaseType != null) { var btype = spec.BaseType.MemberDefinition as TypeContainer; if (btype != null) { btype.CloseType (); if ((caching_flags & Flags.CloseTypeCreated) != 0) return; } } try { caching_flags |= Flags.CloseTypeCreated; TypeBuilder.CreateType (); } catch (TypeLoadException){ // // This is fine, the code still created the type // // Report.Warning (-20, "Exception while creating class: " + TypeBuilder.Name); // Console.WriteLine (e.Message); } catch (Exception e) { throw new InternalErrorException (this, e); } if (Types != null){ foreach (TypeContainer tc in Types) tc.CloseType (); } if (compiler_generated != null) foreach (CompilerGeneratedClass c in compiler_generated) c.CloseType (); types = null; initialized_fields = null; initialized_static_fields = null; constants = null; ordered_explicit_member_list = null; ordered_member_list = null; methods = null; events = null; indexers = null; operators = null; compiler_generated = null; default_constructor = null; default_static_constructor = null; type_bases = null; OptAttributes = 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 void AddConstructor (Constructor c) { bool is_static = (c.ModFlags & Modifiers.STATIC) != 0; if (!AddToContainer (c, is_static ? Constructor.ConstructorName : Constructor.TypeConstructorName)) return; if (is_static && c.ParameterInfo.IsEmpty){ if (default_static_constructor != null) { Report.SymbolRelatedToPreviousError (default_static_constructor); Report.Error (111, c.Location, "A member `{0}' is already defined. Rename this member or use different parameter types", c.GetSignatureForError ()); return; } default_static_constructor = c; } else { if (c.ParameterInfo.IsEmpty) default_constructor = c; if (instance_constructors == null) instance_constructors = new List<Constructor> (); instance_constructors.Add (c); } }
protected override bool DoDefineMembers () { var builtin_types = Compiler.BuiltinTypes; var ctor_parameters = ParametersCompiled.CreateFullyResolved ( new [] { new Parameter (new TypeExpression (builtin_types.Object, Location), "object", Parameter.Modifier.NONE, null, Location), new Parameter (new TypeExpression (builtin_types.IntPtr, Location), "method", Parameter.Modifier.NONE, null, Location) }, new [] { builtin_types.Object, builtin_types.IntPtr } ); Constructor = new Constructor (this, Constructor.ConstructorName, Modifiers.PUBLIC, null, ctor_parameters, Location); Constructor.Define (); // // Here the various methods like Invoke, BeginInvoke etc are defined // // First, call the `out of band' special method for // defining recursively any types we need: // var p = parameters; if (!p.Resolve (this)) return false; // // Invoke method // // Check accessibility foreach (var partype in p.Types) { if (!IsAccessibleAs (partype)) { Report.SymbolRelatedToPreviousError (partype); Report.Error (59, Location, "Inconsistent accessibility: parameter type `{0}' is less accessible than delegate `{1}'", partype.GetSignatureForError (), GetSignatureForError ()); } } var ret_type = ReturnType.ResolveAsType (this); if (ret_type == null) return false; // // We don't have to check any others because they are all // guaranteed to be accessible - they are standard types. // if (!IsAccessibleAs (ret_type)) { Report.SymbolRelatedToPreviousError (ret_type); Report.Error (58, Location, "Inconsistent accessibility: return type `" + ret_type.GetSignatureForError () + "' is less " + "accessible than delegate `" + GetSignatureForError () + "'"); return false; } CheckProtectedModifier (); if (Compiler.Settings.StdLib && ret_type.IsSpecialRuntimeType) { Method.Error1599 (Location, ret_type, Report); return false; } VarianceDecl.CheckTypeVariance (ret_type, Variance.Covariant, this); var resolved_rt = new TypeExpression (ret_type, Location); InvokeBuilder = new Method (this, resolved_rt, MethodModifiers, new MemberName (InvokeMethodName), p, null); InvokeBuilder.Define (); // // Don't emit async method for compiler generated delegates (e.g. dynamic site containers) // if (!IsCompilerGenerated) { DefineAsyncMethods (resolved_rt); } return true; }
public void AddConstructor (Constructor c) { AddConstructor (c, false); }
// <summary> // Resolve is used in method definitions // </summary> public virtual TypeSpec Resolve (IMemberContext rc, int index) { if (parameter_type != null) return parameter_type; if (attributes != null) attributes.AttachTo (this, rc); var ctor = rc.CurrentMemberDefinition as Constructor; if (ctor != null && ctor.IsPrimaryConstructor) primary_constructor = ctor; parameter_type = texpr.ResolveAsType (rc); if (parameter_type == null) return null; this.idx = index; if ((modFlags & Parameter.Modifier.RefOutMask) != 0 && parameter_type.IsSpecialRuntimeType) { rc.Module.Compiler.Report.Error (1601, Location, "Method or delegate parameter cannot be of type `{0}'", GetSignatureForError ()); return null; } VarianceDecl.CheckTypeVariance (parameter_type, (modFlags & Parameter.Modifier.RefOutMask) != 0 ? Variance.None : Variance.Contravariant, rc); if (parameter_type.IsStatic) { rc.Module.Compiler.Report.Error (721, Location, "`{0}': static types cannot be used as parameters", texpr.GetSignatureForError ()); return parameter_type; } if ((modFlags & Modifier.This) != 0 && (parameter_type.IsPointer || parameter_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic)) { rc.Module.Compiler.Report.Error (1103, Location, "The extension method cannot be of type `{0}'", parameter_type.GetSignatureForError ()); } return parameter_type; }
public static AnonymousTypeClass Create (TypeContainer parent, IList<AnonymousTypeParameter> parameters, Location loc) { string name = ClassNamePrefix + parent.Module.CounterAnonymousTypes++; ParametersCompiled all_parameters; TypeParameters tparams = null; SimpleName[] t_args; if (parameters.Count == 0) { all_parameters = ParametersCompiled.EmptyReadOnlyParameters; t_args = null; } else { t_args = new SimpleName[parameters.Count]; tparams = new TypeParameters (); Parameter[] ctor_params = new Parameter[parameters.Count]; for (int i = 0; i < parameters.Count; ++i) { AnonymousTypeParameter p = parameters[i]; for (int ii = 0; ii < i; ++ii) { if (parameters[ii].Name == p.Name) { parent.Compiler.Report.Error (833, parameters[ii].Location, "`{0}': An anonymous type cannot have multiple properties with the same name", p.Name); p = new AnonymousTypeParameter (null, "$" + i.ToString (), p.Location); parameters[i] = p; break; } } t_args[i] = new SimpleName ("<" + p.Name + ">__T", p.Location); tparams.Add (new TypeParameter (i, new MemberName (t_args[i].Name, p.Location), null, null, Variance.None)); ctor_params[i] = new Parameter (t_args[i], p.Name, Parameter.Modifier.NONE, null, p.Location); } all_parameters = new ParametersCompiled (ctor_params); } // // Create generic anonymous type host with generic arguments // named upon properties names // AnonymousTypeClass a_type = new AnonymousTypeClass (parent.Module, new MemberName (name, tparams, loc), parameters, loc); Constructor c = new Constructor (a_type, name, Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN, null, all_parameters, loc); c.Block = new ToplevelBlock (parent.Module.Compiler, c.ParameterInfo, loc); // // Create fields and constructor body with field initialization // bool error = false; for (int i = 0; i < parameters.Count; ++i) { AnonymousTypeParameter p = parameters [i]; Field f = new Field (a_type, t_args [i], Modifiers.PRIVATE | Modifiers.READONLY | Modifiers.DEBUGGER_HIDDEN, new MemberName ("<" + p.Name + ">", p.Location), null); if (!a_type.AddField (f)) { error = true; continue; } c.Block.AddStatement (new StatementExpression ( new SimpleAssign (new MemberAccess (new This (p.Location), f.Name), c.Block.GetParameterReference (i, p.Location)))); ToplevelBlock get_block = new ToplevelBlock (parent.Module.Compiler, p.Location); get_block.AddStatement (new Return ( new MemberAccess (new This (p.Location), f.Name), p.Location)); Property prop = new Property (a_type, t_args [i], Modifiers.PUBLIC, new MemberName (p.Name, p.Location), null); prop.Get = new Property.GetMethod (prop, 0, null, p.Location); prop.Get.Block = get_block; a_type.AddMember (prop); } if (error) return null; a_type.AddConstructor (c); return a_type; }
public override void CloseType () { if ((caching_flags & Flags.CloseTypeCreated) != 0) return; try { caching_flags |= Flags.CloseTypeCreated; TypeBuilder.CreateType (); } catch (TypeLoadException){ // // This is fine, the code still created the type // // Report.Warning (-20, "Exception while creating class: " + TypeBuilder.Name); // Console.WriteLine (e.Message); } catch (Exception e) { throw new InternalErrorException (this, e); } if (Types != null){ foreach (TypeContainer tc in Types) if (tc.Kind == Kind.Struct) tc.CloseType (); foreach (TypeContainer tc in Types) if (tc.Kind != Kind.Struct) tc.CloseType (); } if (Delegates != null) foreach (Delegate d in Delegates) d.CloseType (); if (compiler_generated != null) foreach (CompilerGeneratedClass c in compiler_generated) c.CloseType (); PartialContainer = null; types = null; // properties = null; delegates = null; fields = null; initialized_fields = null; initialized_static_fields = null; constants = null; ordered_explicit_member_list = null; ordered_member_list = null; methods = null; events = null; indexers = null; operators = null; compiler_generated = null; default_constructor = null; default_static_constructor = null; type_bases = null; OptAttributes = null; ifaces = null; base_cache = null; member_cache = null; }