/// <summary> /// Constructor. /// </summary> /// <param name="program">Program</param> internal NamespaceDeclaration(IPSharpProgram program) : base(program) { this.IdentifierTokens = new List <Token>(); this.EventDeclarations = new EventDeclarations(); this.MachineDeclarations = new List <MachineDeclaration>(); }
/// <summary> /// Constructor. /// </summary> /// <param name="program">Program</param> /// <param name="namespaceNode">NamespaceDeclaration</param> /// <param name="isMonitor">Is a monitor</param> /// <param name="modSet">Modifier set</param> internal MachineDeclaration(IPSharpProgram program, NamespaceDeclaration namespaceNode, bool isMonitor, ModifierSet modSet) : base(program) { this.Namespace = namespaceNode; this.IsMonitor = isMonitor; this.AccessModifier = modSet.AccessModifier; this.InheritanceModifier = modSet.InheritanceModifier; this.IsPartial = modSet.IsPartial; this.BaseNameTokens = new List <Token>(); this.EventDeclarations = new EventDeclarations(); this.FieldDeclarations = new List <FieldDeclaration>(); this.StateDeclarations = new List <StateDeclaration>(); this.StateGroupDeclarations = new List <StateGroupDeclaration>(); this.MethodDeclarations = new List <MethodDeclaration>(); this.RewrittenMethods = new HashSet <QualifiedMethod>(); }
/// <summary> /// Returns the rewritten event declaration. /// </summary> private string GetRewrittenEventDeclaration(int indentLevel) { var indent0 = GetIndent(indentLevel); var indent1 = GetIndent(indentLevel + 1); var indent2 = GetIndent(indentLevel + 2); string text = string.Empty; if ((this.Program as AbstractPSharpProgram).GetProject().CompilationContext. Configuration.CompilationTarget == CompilationTarget.Remote) { text += indent0 + "[System.Runtime.Serialization.DataContract]\n"; } text += indent0; if (this.AccessModifier == AccessModifier.None) { // If the the event was declared in the scope of a machine it is private; // otherwise it was declared in the scope of a namespace and is public. text += this.Machine != null ? "private " : "public "; } else if (this.AccessModifier == AccessModifier.Public) { text += "public "; } else if (this.AccessModifier == AccessModifier.Internal) { text += "internal "; } text += "class " + this.Identifier.TextUnit.Text; var allDecls = EventDeclarations.EnumerateInheritance(this).ToArray(); void appendGenericType(EventDeclaration decl) { // The GenericTypes contains the leading < and > as well as the type identifier(s and comma(s)). foreach (var token in decl.GenericType) { text += token.TextUnit.Text + (token.Type == TokenType.Comma ? " " : string.Empty); } } appendGenericType(this); text += " : "; if (this.BaseClassDecl != null) { text += this.BaseClassDecl.Identifier.Text; appendGenericType(this.BaseClassDecl); } else { text += "Event"; } text += "\n" + indent0 + "{\n"; var newLine = string.Empty; for (int i = 0; i < this.PayloadIdentifiers.Count; i++) { text += indent1 + "public "; text += this.PayloadTypes[i].TextUnit.Text + " "; text += this.PayloadIdentifiers[i].TextUnit.Text + ";\n"; newLine = "\n"; // Not included in payload lines } text += newLine; text += indent1 + "public "; text += this.Identifier.TextUnit.Text + "("; var separator = string.Empty; foreach (var decl in allDecls) { for (int i = 0; i < decl.PayloadIdentifiers.Count; i++) { text += $"{separator}{decl.PayloadTypes[i].TextUnit.Text} {decl.PayloadIdentifiers[i].TextUnit.Text}"; separator = ", "; } } text += ")\n"; text += indent2 + ": base("; if (allDecls.Length > 1) { // We don't pass the most-derived decl's params to the base class separator = string.Empty; foreach (var decl in allDecls.Take(allDecls.Length - 1)) { for (int i = 0; i < decl.PayloadIdentifiers.Count; i++) { text += $"{separator}{decl.PayloadIdentifiers[i].TextUnit.Text}"; separator = ", "; } } } text += ")\n"; text += indent1 + "{\n"; for (int i = 0; i < this.PayloadIdentifiers.Count; i++) { text += indent2 + "this." + this.PayloadIdentifiers[i].TextUnit.Text + " = "; text += this.PayloadIdentifiers[i].TextUnit.Text + ";\n"; } text += indent1 + "}\n"; text += indent0 + "}\n"; return(text); }
/// <summary> /// Returns the rewritten event declaration. /// </summary> /// <returns>Text</returns> private string GetRewrittenEventDeclaration(int indentLevel) { var indent0 = GetIndent(indentLevel); var indent1 = GetIndent(indentLevel + 1); var indent2 = GetIndent(indentLevel + 2); string text = ""; if ((this.Program as AbstractPSharpProgram).GetProject().CompilationContext. Configuration.CompilationTarget == CompilationTarget.Remote) { text += indent0 + "[System.Runtime.Serialization.DataContract]\n"; } text += indent0; if (this.AccessModifier == AccessModifier.None) { // If the the event was declared in the scope of a machine it is private; // otherwise it was declared in the scope of a namespace and is public. text += this.Machine != null ? "private " : "public "; } else if (this.AccessModifier == AccessModifier.Public) { text += "public "; } else if (this.AccessModifier == AccessModifier.Internal) { text += "internal "; } text += "class " + this.Identifier.TextUnit.Text; var allDecls = EventDeclarations.EnumerateInheritance(this).ToArray(); void appendGenericType(EventDeclaration decl) { // The GenericTypes contains the leading < and > as well as the type identifier(s and comma(s)). foreach (var token in decl.GenericType) { text += token.TextUnit.Text + (token.Type == TokenType.Comma ? " " : ""); } } appendGenericType(this); text += " : "; if (this.BaseClassDecl != null) { text += this.BaseClassDecl.Identifier.Text; appendGenericType(this.BaseClassDecl); } else { text += "Event"; } text += "\n" + indent0 + "{\n"; var newLine = ""; for (int i = 0; i < this.PayloadIdentifiers.Count; i++) { text += indent1 + "public "; text += this.PayloadTypes[i].TextUnit.Text + " "; text += this.PayloadIdentifiers[i].TextUnit.Text + ";\n"; newLine = "\n"; // Not included in payload lines } text += newLine; text += indent1 + "public "; text += this.Identifier.TextUnit.Text + "("; var separator = string.Empty; foreach (var decl in allDecls) { for (int i = 0; i < decl.PayloadIdentifiers.Count; i++) { text += $"{separator}{decl.PayloadTypes[i].TextUnit.Text} {decl.PayloadIdentifiers[i].TextUnit.Text}"; separator = ", "; } } text += ")\n"; text += indent2 + ": base("; void addAssertAssumeParams(bool forDerived) { if (this.AssertKeyword != null) { text += this.AssertValue + ", -1"; } else if (this.AssumeKeyword != null) { text += "-1, " + this.AssumeValue; } else if (forDerived) { text += "-1, -1"; } } if (allDecls.Length > 1) { // We don't pass the most-derived decl's params to the base class separator = string.Empty; foreach (var decl in allDecls.Take(allDecls.Length - 1)) { for (int i = 0; i < decl.PayloadIdentifiers.Count; i++) { text += $"{separator}{decl.PayloadIdentifiers[i].TextUnit.Text}"; separator = ", "; } } } else { // Assert/Assume are passed as params to ctor overload for classes that derive directly from Event. addAssertAssumeParams(forDerived: false); } text += ")\n"; text += indent1 + "{\n"; for (int i = 0; i < this.PayloadIdentifiers.Count; i++) { text += indent2 + "this." + this.PayloadIdentifiers[i].TextUnit.Text + " = "; text += this.PayloadIdentifiers[i].TextUnit.Text + ";\n"; } if (this.BaseClassDecl != null) { // Assert/Assume are passed as to a protected method for classes that don't derive directly from Event. // Override any base-class declaration; if none are specified on the derived class then this will turn it off. text += indent2 + "base.SetCardinalityConstraints("; addAssertAssumeParams(forDerived: true); text += ");\n"; } text += indent1 + "}\n"; text += indent0 + "}\n"; return(text); }