private void InjectStaticInitializerIfNoneSpecified(TypeNode typeNode){ if (typeNode.NodeType == NodeType.EnumNode) return; MemberList staticCons = typeNode.GetMembersNamed(StandardIds.CCtor); if (staticCons != null && staticCons.Count > 0) return; StatementList statements = null; MemberList members = typeNode.Members; for (int i = 0, n = members == null ? 0 : members.Count; i < n; i++){ Field f = members[i] as Field; if (f == null) continue; if (!f.IsStatic) continue; if (f.IsLiteral) continue; if (f.Initializer == null) continue; statements = new StatementList(1); break; } if (statements == null && typeNode.Contract != null && typeNode.Contract.FrameField != null) statements = new StatementList(1); if (statements != null){ FieldInitializerBlock finitBlock = new FieldInitializerBlock(typeNode, true); statements.Add(finitBlock); StaticInitializer cctor = new StaticInitializer(typeNode, null, new Block(statements)); typeNode.Members.Add(cctor); if (typeNode.PartiallyDefines != null){ staticCons = typeNode.PartiallyDefines.GetMembersNamed(StandardIds.CCtor); if (staticCons == null || staticCons.Count == 0){ finitBlock.Type = typeNode.PartiallyDefines; cctor.DeclaringType = typeNode.PartiallyDefines; typeNode.PartiallyDefines.Members.Add(cctor); } } } }
/// <summary> /// Write out a static constructor name /// </summary> /// <param name="constructor">The static constructor for which to write out the name</param> /// <param name="sb">The string builder to which the name is written</param> private static void WriteStaticConstructor(StaticInitializer constructor, StringBuilder sb) { WriteType(constructor.DeclaringType, sb); sb.Append(".#cctor"); WriteParameters(constructor.Parameters, sb); }
public virtual StaticInitializer VisitStaticInitializer(StaticInitializer cons) { return (StaticInitializer)this.VisitMethod(cons); }
private void ParseStaticConstructor(TypeNode parentType, AttributeList attributes, TokenList modifierTokens, SourceContextList modifierContexts, MethodFlags flags, object sctx, SourceContext idCtx, TokenSet followers){ if (parentType is Interface){ this.HandleError(idCtx, Error.InterfacesCannotContainConstructors); }else{ for (int i = 0, n = modifierTokens.Length; i < n; i++){ Token tok = modifierTokens[i]; if (tok == Token.Static || tok == Token.Extern) continue; if (tok == Token.Unsafe){ if (!this.allowUnsafeCode){ this.allowUnsafeCode = true; this.HandleError(modifierContexts[i], Error.IllegalUnsafe); } this.inUnsafeCode = true; continue; } this.HandleError(modifierContexts[i], Error.StaticConstructorWithAccessModifiers); } } StaticInitializer c = new StaticInitializer(parentType, attributes, new Block(new StatementList(2), this.insideCheckedBlock, this.insideUncheckedBlock, this.inUnsafeCode), this.TypeExpressionFor(Token.Void)); c.Name = new Identifier(".cctor", idCtx); parentType.Members.Add(c); c.Flags |= flags; c.HasCompilerGeneratedSignature = false; ParameterList plist = c.Parameters = this.ParseParameters(Token.RightParenthesis, followers|Token.LeftBrace|Token.Colon); if (plist != null && plist.Count > 0){ this.HandleError(plist[0].SourceContext, Error.StaticConstParam); c.Parameters = null; } if (this.currentToken == Token.Colon){ SourceContext ctx = this.scanner.CurrentSourceContext; this.GetNextToken(); if (this.currentToken == Token.This || this.currentToken == Token.Base){ ctx.EndPos = this.scanner.endPos; this.GetNextToken(); if (this.currentToken == Token.LeftParenthesis){ SourceContext lpCtx = this.scanner.CurrentSourceContext; this.Skip(Token.LeftParenthesis); this.ParseArgumentList(followers|Token.LeftBrace, lpCtx, out ctx.EndPos); } }else this.SkipTo(followers|Token.LeftBrace, Error.None); this.HandleError(ctx, Error.StaticConstructorWithExplicitConstructorCall); } Block b = this.ParseBody(c, sctx, followers); c.Body.Statements.Add(new FieldInitializerBlock(parentType, true)); c.Body.Statements.Add(b); }
private static void WriteStaticConstructor(StaticInitializer constructor, TextWriter writer) { WriteType(constructor.DeclaringType, writer); writer.Write(".#cctor"); WriteParameters(constructor.Parameters, writer); }
public override StaticInitializer VisitStaticInitializer(StaticInitializer cons) { WriteStart("static "); this.VisitIdentifier(cons.DeclaringType.Name); Write("("); In(); this.VisitParameterList(cons.Parameters); Out(); WriteFinish(")"); this.VisitBlock(cons.Body); return cons; }
public EventingVisitor(Action<StaticInitializer> visitStaticInitializer) { VisitedStaticInitializer += visitStaticInitializer; } public event Action<StaticInitializer> VisitedStaticInitializer; public override StaticInitializer VisitStaticInitializer(StaticInitializer cons) { if (VisitedStaticInitializer != null) VisitedStaticInitializer(cons); return base.VisitStaticInitializer(cons); }