public void AddTypeDeclaration(string typeName) { TypeDeclaration.Add( (FieldDeclarationSyntax)CSharpHelper.MkCSharpFieldDeclaration( SyntaxFactory.IdentifierName("PrtType"), typeName, SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword))); }
public static InvocationExpressionSyntax MkCSharpInvocationExpression(SyntaxNode first, params ExpressionSyntax[] pars) { var args = CSharpHelper.MkCSharpArgumentList(pars); return(SyntaxFactory.InvocationExpression((ExpressionSyntax)first) .WithArgumentList( SyntaxFactory.ArgumentList( SyntaxFactory.SeparatedList <ArgumentSyntax>(args)))); }
public static StatementSyntax MkCSharpPrint(string msg, List <ExpressionSyntax> pars) { msg = "<PrintLog>" + " " + msg; var allPars = new List <ExpressionSyntax>(pars); allPars.Insert(0, CSharpHelper.MkCSharpStringLiteralExpression(msg)); return(SyntaxFactory.ExpressionStatement(MkCSharpInvocationExpression( MkCSharpDot("application", "Trace"), allPars.ToArray()))); }
public static ConstructorDeclarationSyntax MkCSharpConstructor(SyntaxToken name, SyntaxTokenList modifiers, List <SyntaxNode> constrPars, ConstructorInitializerSyntax initializer, List <StatementSyntax> body) { List <SyntaxNodeOrToken> pars = CSharpHelper.MkCSharpParameterList(constrPars); return(SyntaxFactory.ConstructorDeclaration(name) .WithModifiers(modifiers) .WithParameterList(SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList <ParameterSyntax>(pars))) .WithInitializer(initializer) .WithBody(SyntaxFactory.Block(body))); }
public static SyntaxNode MkCSharpMethodDeclaration(SyntaxNode type, SyntaxToken name, SyntaxToken[] attrs, SyntaxList <StatementSyntax> body, List <SyntaxNode> methodPars) { List <SyntaxNodeOrToken> pars = CSharpHelper.MkCSharpParameterList(methodPars); return(SyntaxFactory.MethodDeclaration((TypeSyntax)type, name) .WithModifiers(SyntaxFactory.TokenList(attrs)) .WithParameterList( SyntaxFactory.ParameterList( SyntaxFactory.SeparatedList <ParameterSyntax>(pars))) .WithBody(SyntaxFactory.Block(body)) .NormalizeWhitespace()); }
public static ObjectCreationExpressionSyntax MkCSharpObjectCreationExpression(SyntaxNode type, params SyntaxNode[] names) { List <SyntaxNode> hd = new List <SyntaxNode>(); if (names.Length == 0) { return(SyntaxFactory.ObjectCreationExpression( (TypeSyntax)type) .WithArgumentList( SyntaxFactory.ArgumentList())); } //TODO(improve): merge this case with the general case else if (names.Length == 1) { return(SyntaxFactory.ObjectCreationExpression( (TypeSyntax)type) .WithArgumentList( SyntaxFactory.ArgumentList( SyntaxFactory.SingletonSeparatedList <ArgumentSyntax>( SyntaxFactory.Argument(((ExpressionSyntax)names[0]))))) //IdentifierName("p1"))))) .NormalizeWhitespace()); } else { hd.Add(SyntaxFactory.Argument((ExpressionSyntax)names[0])); for (int i = 1; i < names.Length; i++) { ArgumentSyntax tl = SyntaxFactory.Argument((ExpressionSyntax)names[i]); hd.Add(tl); } //hd contains list of Argument(IdentifierName(names[i])) //Insert Token(SyntaxKind.CommaToken) after each Argument except the last one //and create new SyntaxNodeOrToken[] out of the result: List <SyntaxNodeOrToken> hdWithCommas = CSharpHelper.MkCSharpParameterList(hd); //TODO(question): in Roslyn quoter, the initialization for List<SyntaxNodeOrToken> //looks different: "new SyntaxNodeOrToken[]{ el1, el2, ... } //Does that matter? return(SyntaxFactory.ObjectCreationExpression( (TypeSyntax)type) .WithArgumentList( SyntaxFactory.ArgumentList( SyntaxFactory.SeparatedList <ArgumentSyntax>( new List <SyntaxNodeOrToken>(hdWithCommas)))) .NormalizeWhitespace()); } }
private List <SyntaxNode> MkAppConstructors(string testName) { //parameterless constructor List <SyntaxNode> constructorList = new List <SyntaxNode>(); var constructor_1 = generator.ConstructorDeclaration("Application", null, Accessibility.Public, baseConstructorArguments: new SyntaxNode[0]); constructorList.Add(constructor_1); //constructor with the initialize paramenter which is called by Ptester at the start. var constructorParameters = new SyntaxNode[] { generator.ParameterDeclaration("initialize", generator.TypeExpression(SpecialType.System_Boolean)) }; List <SyntaxNode> stmtList = new List <SyntaxNode>(); foreach (var monName in allTests[testName].specMachineMap.Keys) { stmtList.Add(SyntaxFactory.ExpressionStatement( CSharpHelper.MkCSharpInvocationExpression(SyntaxFactory.IdentifierName("CreateSpecMachine"), CSharpHelper.MkCSharpStringLiteralExpression(monName)))); } var constructorBody = generator.ExpressionStatement( generator.InvocationExpression( generator.IdentifierName("CreateMainMachine"))); stmtList.Add(constructorBody); var constructor_2 = generator.ConstructorDeclaration("Application", constructorParameters, Accessibility.Public, baseConstructorArguments: new SyntaxNode[0], statements: stmtList.ToArray()); constructorList.Add(constructor_2); //skeleton constructor used by the clone function. var makeSkeletonMethodBody = generator.ReturnStatement(generator.ObjectCreationExpression(generator.IdentifierName("Application"))); var makeSkeletonMethodDecl = generator.MethodDeclaration("MakeSkeleton", null, null, generator.IdentifierName("StateImpl"), Accessibility.Public, DeclarationModifiers.Override, new SyntaxNode[] { makeSkeletonMethodBody }); constructorList.Add(makeSkeletonMethodDecl); return(constructorList); }
private void MkCreateSpecMachineMethod(string machineName) { var fields = new List <SyntaxNode>(); //create machine fields.Add( generator.LocalDeclarationStatement( generator.IdentifierName("var"), "machine", generator.ObjectCreationExpression( generator.IdentifierName(machineName), new List <SyntaxNode> { generator.IdentifierName("application") }))); //log fields.Add(CSharpHelper.MkCSharpTrace($"<CreateLog> Created spec Machine {machineName}")); fields.AddRange(allMachines[machineName].observesEvents.Select(x => CSharpHelper.MkCSharpInvocationExpression(CSharpHelper.MkCSharpDot("machine", "observes", "Add"), GetEventVar(x)))); var prtvalueNull = (ExpressionSyntax)generator.MemberAccessExpression(IdentifierName("PrtEventValue"), IdentifierName("@null")); //execute the entry function of the machine fields.Add( CSharpHelper.MkCSharpInvocationExpression( CSharpHelper.MkCSharpDot("machine", "PrtEnqueueEvent"), prtvalueNull, prtvalueNull, LiteralExpression(SyntaxKind.NullLiteralExpression))); //return machine; fields.Add(generator.ReturnStatement(generator.IdentifierName("machine"))); var methodPars = new[] { generator.ParameterDeclaration("application", generator.IdentifierName("StateImpl")) }; SyntaxNode makeCreateSpecDecl = generator.MethodDeclaration( $"CreateSpecMachine_{machineName}", methodPars, null, IdentifierName("PrtSpecMachine"), Accessibility.Public, DeclarationModifiers.Static, fields); members.Add(makeCreateSpecDecl); }
private ExpressionSyntax ConstructType(FuncTerm type) { string typeKind = ((Id)type.Function).Name; ExpressionSyntax typeExpr; string typeName; if (ImportedTypes.ContainsKey(Factory.Instance.ToAST(type))) { typeName = GetNextTypeName(ImportedTypes[Factory.Instance.ToAST(type)]); return(GetTypeExpr(typeName)); } var originalName = "Interface"; if (ExportedTypes.ContainsKey(Factory.Instance.ToAST(type))) { originalName = ExportedTypes[Factory.Instance.ToAST(type)]; typeName = GetNextTypeName(ExportedTypes[Factory.Instance.ToAST(type)]); typeExpr = GetTypeExpr(typeName); } else { typeName = GetNextTypeName(); typeExpr = GetTypeExpr(typeName); } // add declaration and initialization if (typeKind == "BaseType") { string primitiveType = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(((Id)GetArgByIndex(type, 0)).Name.ToLowerInvariant()); Debug.Assert( primitiveType == "Any" || primitiveType == "Null" || primitiveType == "Bool" || primitiveType == "Int" || primitiveType == "Float" || primitiveType == "Event" || primitiveType == "Machine", $"Illegal BaseType: {primitiveType}"); if (primitiveType != "Any") { AddTypeInitialization( typeExpr, CSharpHelper.MkCSharpObjectCreationExpression(SyntaxFactory.IdentifierName($"Prt{primitiveType}Type"))); AddTypeDeclaration(typeName); } } else if (typeKind == "AnyType") { AddTypeDeclaration(typeName); AddTypeInitialization( typeExpr, CSharpHelper.MkCSharpObjectCreationExpression(SyntaxFactory.IdentifierName("PrtAnyType"))); } else if (typeKind == "NameType") { string enumTypeName = (GetArgByIndex(type, 0) as Cnst).GetStringValue(); if (pToCSharp.allEnums.ContainsKey(enumTypeName)) { var args = new List <ExpressionSyntax> { CSharpHelper.MkCSharpStringLiteralExpression(enumTypeName) }; foreach (KeyValuePair <string, int> x in pToCSharp.allEnums[enumTypeName]) { args.Add(CSharpHelper.MkCSharpStringLiteralExpression(x.Key)); args.Add(CSharpHelper.MkCSharpNumericLiteralExpression(x.Value)); } AddTypeDeclaration(typeName); AddTypeInitialization( typeExpr, CSharpHelper.MkCSharpObjectCreationExpression(SyntaxFactory.IdentifierName("PrtEnumType"), args.ToArray())); } else { var args = new List <ExpressionSyntax>(); AddTypeDeclaration(typeName); AddTypeInitialization( typeExpr, CSharpHelper.MkCSharpObjectCreationExpression(SyntaxFactory.IdentifierName($"Foreign_{enumTypeName}"), args.ToArray())); } } else if (typeKind == "TupType") { var memberTypes = new List <SyntaxNodeOrToken>(); while (type != null) { memberTypes.Add(PTypeToCSharpExpr((FuncTerm)GetArgByIndex(type, 0))); type = GetArgByIndex(type, 1) as FuncTerm; } var initializer = CSharpHelper.Intersperse(memberTypes, SyntaxFactory.Token(SyntaxKind.CommaToken)); AddTypeDeclaration(typeName); AddTypeInitialization( typeExpr, CSharpHelper.MkCSharpObjectCreationExpression( SyntaxFactory.IdentifierName("PrtTupleType"), CSharpHelper.MkCSharpArrayCreationExpression("PrtType", initializer.ToArray()))); } else if (typeKind == "NmdTupType") { var memberNames = new List <SyntaxNode>(); var memberTypes = new List <SyntaxNode>(); while (type != null) { var typeField = (FuncTerm)GetArgByIndex(type, 0); string nameField = ((Cnst)GetArgByIndex(typeField, 0)).GetStringValue(); memberNames.Add(CSharpHelper.MkCSharpStringLiteralExpression(nameField)); memberTypes.Add(PTypeToCSharpExpr((FuncTerm)GetArgByIndex(typeField, 1))); type = GetArgByIndex(type, 1) as FuncTerm; } var initializer = new List <SyntaxNodeOrToken>(); var ind = 0; foreach (SyntaxNode memberName in memberNames) { initializer.Add(memberName); initializer.Add(SyntaxFactory.Token(SyntaxKind.CommaToken)); initializer.Add(memberTypes[ind++]); initializer.Add(SyntaxFactory.Token(SyntaxKind.CommaToken)); } initializer.RemoveAt(initializer.Count - 1); AddTypeDeclaration(typeName); AddTypeInitialization( typeExpr, CSharpHelper.MkCSharpObjectCreationExpression( SyntaxFactory.IdentifierName("PrtNamedTupleType"), CSharpHelper.MkCSharpArrayCreationExpression("object", initializer.ToArray()))); } else if (typeKind == "SeqType") { SyntaxNode innerType = PTypeToCSharpExpr((FuncTerm)GetArgByIndex(type, 0)); AddTypeDeclaration(typeName); AddTypeInitialization( typeExpr, CSharpHelper.MkCSharpObjectCreationExpression(SyntaxFactory.IdentifierName("PrtSeqType"), innerType)); } else if (typeKind == "MapType") { SyntaxNode keyType = PTypeToCSharpExpr((FuncTerm)GetArgByIndex(type, 0)); SyntaxNode valType = PTypeToCSharpExpr((FuncTerm)GetArgByIndex(type, 1)); AddTypeDeclaration(typeName); AddTypeInitialization( typeExpr, CSharpHelper.MkCSharpObjectCreationExpression(SyntaxFactory.IdentifierName("PrtMapType"), keyType, valType)); } else { // typekind == "InterfaceType" ObjectCreationExpressionSyntax initializer = CSharpHelper.MkCSharpObjectCreationExpression( SyntaxFactory.IdentifierName("PrtInterfaceType"), CSharpHelper.MkCSharpStringLiteralExpression(originalName)); AddTypeDeclaration(typeName); AddTypeInitialization(typeExpr, initializer); } return(typeExpr); }
public void AddTypeInitialization(SyntaxNode lhs, SyntaxNode rhs) { TypeInitialization.Add(CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement(lhs, rhs)); }
private void MkCreateRealMachineMethod(string machineName) { var fields = new List <SyntaxNode>(); //create machine MachineInfo machineInfo = allMachines[machineName]; //There are three cases: //- default (no constraint on queue size): maxQueueSizeAssumed == false; maxQueueSize = default (10?) //- assume <maxQueueSize>: maxQueueSize > 0, maxQueueSizeAssumed == true; //- assert <maxQueueSize>: maxQueueSize > 0, maxQueueSizeAssumed == false; if (machineInfo.maxQueueSize > 0) { fields.Add( generator.LocalDeclarationStatement( generator.IdentifierName("var"), "machine", generator.ObjectCreationExpression( generator.IdentifierName(machineName), new List <SyntaxNode> { generator.IdentifierName("application"), generator.LiteralExpression(machineInfo.maxQueueSize), generator.LiteralExpression(machineInfo.maxQueueSizeAssumed) }))); } else { fields.Add( generator.LocalDeclarationStatement( generator.IdentifierName("var"), "machine", generator.ObjectCreationExpression( generator.IdentifierName(machineName), new List <SyntaxNode> { generator.IdentifierName("application"), MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, IdentifierName("PrtImplMachine"), IdentifierName("DefaultMaxBufferSize")), generator.LiteralExpression(machineInfo.maxQueueSizeAssumed) }))); } // log fields.Add( CSharpHelper.MkCSharpTrace( $"<CreateLog> Created Machine {machineName}-{{0}}", CSharpHelper.MkCSharpDot("machine", "instanceNumber"))); //initialize the permission set for self if (allMachines[machineName].receiveSet == null) { fields.Add( CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement( CSharpHelper.MkCSharpDot(IdentifierName("machine"), "self", "permissions"), LiteralExpression(SyntaxKind.NullLiteralExpression))); } else { fields.AddRange( allMachines[machineName].receiveSet.Select( ev => CSharpHelper.MkCSharpInvocationExpression( CSharpHelper.MkCSharpDot(IdentifierName("machine"), "self", "permissions", "Add"), GetEventVar(ev)))); } //initialize the send set if (allMachines[machineName].sendsSet == null) { fields.Add( CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement( CSharpHelper.MkCSharpDot(IdentifierName("machine"), "sends"), LiteralExpression(SyntaxKind.NullLiteralExpression))); } else { fields.AddRange( allMachines[machineName].sendsSet.Select( ev => CSharpHelper.MkCSharpInvocationExpression( CSharpHelper.MkCSharpDot(IdentifierName("machine"), "sends", "Add"), GetEventVar(ev)))); } //machine.currentPayload = payload; fields.Add( CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement( CSharpHelper.MkCSharpDot("machine", "currentPayload"), IdentifierName("payload"))); //return machine; fields.Add(generator.ReturnStatement(generator.IdentifierName("machine"))); var methodPars = new[] { generator.ParameterDeclaration("application", generator.IdentifierName("StateImpl")), generator.ParameterDeclaration("payload", generator.IdentifierName("PrtValue")) }; SyntaxNode makeCreateMachineDecl = generator.MethodDeclaration( $"CreateMachine_{machineName}", methodPars, null, // type parameters generator.IdentifierName("PrtImplMachine"), Accessibility.Public, DeclarationModifiers.Static, fields); members.Add(makeCreateMachineDecl); }
private void MkEvents() { var evDeclarations = new List <SyntaxNode>(); var eventInitializationStmts = new List <StatementSyntax>(); const string eventsClassName = "Events"; foreach (string evName in exportedEvents) { //add declaration evDeclarations.Add( CSharpHelper.MkCSharpFieldDeclaration( IdentifierName("PrtEventValue"), EventName(evName), Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword))); //add initialization SyntaxNode payloadType = typeContext.PTypeToCSharpExpr(allEvents[evName].payloadType); SyntaxNode maxInstances; if (allEvents[evName].maxInstances == -1) { var s = "PrtEvent.DefaultMaxInstances"; maxInstances = IdentifierName(s); } else { maxInstances = CSharpHelper.MkCSharpNumericLiteralExpression(allEvents[evName].maxInstances); } SyntaxNode doAssume = allEvents[evName].maxInstancesAssumed ? CSharpHelper.MkCSharpTrueLiteralExpression() : CSharpHelper.MkCSharpFalseLiteralExpression(); SyntaxNode lhs = IdentifierName(EventName(evName)); SyntaxNode rhs = CSharpHelper.MkCSharpObjectCreationExpression( IdentifierName("PrtEventValue"), CSharpHelper.MkCSharpObjectCreationExpression( IdentifierName("PrtEvent"), CSharpHelper.MkCSharpStringLiteralExpression(evName), payloadType, maxInstances, doAssume)); eventInitializationStmts.Add(CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement(lhs, rhs)); } string staticMethodName = "Events_" + Path.GetFileNameWithoutExtension(cSharpFileName.ToLower()); MethodDeclarationSyntax staticInitializer = MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(staticMethodName)) .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword))) .WithBody(Block(eventInitializationStmts)).NormalizeWhitespace(); evDeclarations.Add(staticInitializer); SyntaxNode eventsClass = generator.ClassDeclaration( eventsClassName, null, Accessibility.Public, DeclarationModifiers.Partial, members: evDeclarations); members.Add(eventsClass); }
private SyntaxNode MkStaticAppConstructor(string testName) { var stmtList = new List <StatementSyntax>(); //Initialize types and events var nodes = dependsOn.Keys.Select(s => s.ToLower()).ToList(); foreach (var files in dependsOn.Values) { foreach (var f in files) { if (!nodes.Contains(f)) { nodes.Add(f); } } } var edges = new List <Tuple <string, string> >(); foreach (var file in dependsOn) { foreach (var dep in file.Value) { if (file.Key != dep) { edges.Add(new Tuple <string, string>(dep.ToLower(), file.Key.ToLower())); } } } List <string> topoOrder = TopologicalSortFiles <string>(nodes, edges); //make invocations to the initialization of all types foreach (var fName in topoOrder) { stmtList.Add( SyntaxFactory.ExpressionStatement(CSharpHelper.MkCSharpInvocationExpression( SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("Types"), SyntaxFactory.IdentifierName("Types_" + fName)) ))); } //make invocation to the initialization of all events foreach (var fName in topoOrder) { stmtList.Add( SyntaxFactory.ExpressionStatement(CSharpHelper.MkCSharpInvocationExpression( SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("Events"), SyntaxFactory.IdentifierName("Events_" + fName)) ))); } //Initialize all the maps //safe map foreach (var sm in allTests[testName].isSafeMap) { var safeadd = CSharpHelper.MkCSharpInvocationExpression( CSharpHelper.MkCSharpDot("isSafeMap", "Add"), CSharpHelper.MkCSharpStringLiteralExpression(sm.Key), sm.Value ? CSharpHelper.MkCSharpTrueLiteralExpression() : CSharpHelper.MkCSharpFalseLiteralExpression() ); stmtList.Add(SyntaxFactory.ExpressionStatement(safeadd)); } //rename map foreach (var rename in allTests[testName].renameMap) { var renameadd = CSharpHelper.MkCSharpInvocationExpression( CSharpHelper.MkCSharpDot("renameMap", "Add"), CSharpHelper.MkCSharpStringLiteralExpression(rename.Key), CSharpHelper.MkCSharpStringLiteralExpression(rename.Value) ); stmtList.Add(SyntaxFactory.ExpressionStatement(renameadd)); } //create map var listOfSpecMachineNames = allTests[testName].renameMap.Where(item => allTests[testName].specMachineMap.ContainsKey(item.Key)).Select(item => item.Value).Distinct(); var listOfMachineNames = allTests[testName].renameMap.Where(item => !allTests[testName].specMachineMap.ContainsKey(item.Key)).Select(item => item.Value).Distinct(); foreach (var specMachine in listOfSpecMachineNames) { var createadd = CSharpHelper.MkCSharpInvocationExpression( CSharpHelper.MkCSharpDot("createSpecMap", "Add"), CSharpHelper.MkCSharpStringLiteralExpression(specMachine), SyntaxFactory.IdentifierName(string.Format("CreateSpecMachine_{0}", specMachine))); stmtList.Add(SyntaxFactory.ExpressionStatement(createadd)); } foreach (var machine in listOfMachineNames) { //its a machine var createadd = CSharpHelper.MkCSharpInvocationExpression( CSharpHelper.MkCSharpDot("createMachineMap", "Add"), CSharpHelper.MkCSharpStringLiteralExpression(machine), SyntaxFactory.IdentifierName(string.Format("CreateMachine_{0}", machine)) ); stmtList.Add(SyntaxFactory.ExpressionStatement(createadd)); } //interface map foreach (var it in allTests[testName].interfaceMap) { var eventsParams = new SeparatedSyntaxList <ExpressionSyntax>(); foreach (var ev in it.Value) { eventsParams = eventsParams.Add(GetEventVar(ev)); } var initStmt = CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement( SyntaxFactory.ElementAccessExpression( SyntaxFactory.IdentifierName("interfaceMap"), SyntaxFactory.BracketedArgumentList(SyntaxFactory.SingletonSeparatedList <ArgumentSyntax>(SyntaxFactory.Argument(CSharpHelper.MkCSharpStringLiteralExpression(it.Key)))) ), SyntaxFactory.ObjectCreationExpression( SyntaxFactory.GenericName(SyntaxFactory.Identifier("List"), SyntaxFactory.TypeArgumentList(SyntaxFactory.SingletonSeparatedList <TypeSyntax>(SyntaxFactory.IdentifierName("PrtEventValue")))), SyntaxFactory.ArgumentList(), SyntaxFactory.InitializerExpression(SyntaxKind.CollectionInitializerExpression, eventsParams)) ); stmtList.Add(initStmt); } //send actions { var eventsParams = new SeparatedSyntaxList <ExpressionSyntax>(); foreach (var ev in allTests[testName].sendActions) { eventsParams = eventsParams.Add(CSharpHelper.MkCSharpStringLiteralExpression(ev)); } var initStmt = CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement( SyntaxFactory.IdentifierName("visibleEvents"), SyntaxFactory.ObjectCreationExpression( SyntaxFactory.GenericName(SyntaxFactory.Identifier("List"), SyntaxFactory.TypeArgumentList(SyntaxFactory.SingletonSeparatedList <TypeSyntax>(SyntaxFactory.IdentifierName("string")))), SyntaxFactory.ArgumentList(), SyntaxFactory.InitializerExpression(SyntaxKind.CollectionInitializerExpression, eventsParams)) ); stmtList.Add(initStmt); } //interface actions { var interfaceParams = new SeparatedSyntaxList <ExpressionSyntax>(); foreach (var it in allTests[testName].createActions) { interfaceParams = interfaceParams.Add(CSharpHelper.MkCSharpStringLiteralExpression(it)); } var initStmt = CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement( SyntaxFactory.IdentifierName("visibleInterfaces"), SyntaxFactory.ObjectCreationExpression( SyntaxFactory.GenericName(SyntaxFactory.Identifier("List"), SyntaxFactory.TypeArgumentList(SyntaxFactory.SingletonSeparatedList <TypeSyntax>(SyntaxFactory.IdentifierName("string")))), SyntaxFactory.ArgumentList(), SyntaxFactory.InitializerExpression(SyntaxKind.CollectionInitializerExpression, interfaceParams)) ); stmtList.Add(initStmt); } //initialize the interface types foreach (var it in allTests[testName].interfaceMap) { var initStmt = CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement( SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.ParenthesizedExpression( CSharpHelper.MkCSharpCastExpression("PrtInterfaceType", SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("Types"), SyntaxFactory.IdentifierName(String.Format("type_{0}", it.Key))))), SyntaxFactory.IdentifierName("permissions")), SyntaxFactory.ElementAccessExpression( SyntaxFactory.IdentifierName("interfaceMap"), SyntaxFactory.BracketedArgumentList(SyntaxFactory.SingletonSeparatedList <ArgumentSyntax>(SyntaxFactory.Argument(CSharpHelper.MkCSharpStringLiteralExpression(it.Key)))) ) ); stmtList.Add(initStmt); } //spec machine map foreach (var specMachine in allTests[testName].specMachineMap) { SeparatedSyntaxList <ExpressionSyntax> machines = new SeparatedSyntaxList <ExpressionSyntax>(); machines = machines.AddRange(specMachine.Value.Select(x => CSharpHelper.MkCSharpStringLiteralExpression(x))); var addstmt = CSharpHelper.MkCSharpInvocationExpression( CSharpHelper.MkCSharpDot("specMachineMap", "Add"), CSharpHelper.MkCSharpStringLiteralExpression(specMachine.Key), SyntaxFactory.ObjectCreationExpression( CSharpHelper.MkCSharpGenericListType(SyntaxFactory.IdentifierName("string")), SyntaxFactory.ArgumentList(), SyntaxFactory.InitializerExpression(SyntaxKind.CollectionInitializerExpression, machines)) ); stmtList.Add(SyntaxFactory.ExpressionStatement(addstmt)); } //link map //declare temp variable var dictStringString = SyntaxFactory.GenericName( SyntaxFactory.Identifier("Dictionary"), SyntaxFactory.TypeArgumentList(SyntaxFactory.SeparatedList <TypeSyntax>( new SyntaxNodeOrToken[] { SyntaxFactory.PredefinedType( SyntaxFactory.Token(SyntaxKind.StringKeyword)), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.PredefinedType( SyntaxFactory.Token(SyntaxKind.StringKeyword)) })) ); stmtList.Add( SyntaxFactory.LocalDeclarationStatement( SyntaxFactory.VariableDeclaration( dictStringString, SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>( SyntaxFactory.VariableDeclarator( SyntaxFactory.Identifier("_temp")) ) ) ) ); foreach (var creatorM in allTests[testName].linkMap) { var tempInit = CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement( SyntaxFactory.IdentifierName("_temp"), CSharpHelper.MkCSharpObjectCreationExpression(dictStringString) ); stmtList.Add(tempInit); foreach (var linkmapitem in creatorM.Value) { stmtList.Add( SyntaxFactory.ExpressionStatement( CSharpHelper.MkCSharpInvocationExpression( CSharpHelper.MkCSharpDot("_temp", "Add"), CSharpHelper.MkCSharpStringLiteralExpression(linkmapitem.Key), CSharpHelper.MkCSharpStringLiteralExpression(linkmapitem.Value) )) ); } var addLinkItem = SyntaxFactory.ExpressionStatement( CSharpHelper.MkCSharpInvocationExpression( CSharpHelper.MkCSharpDot("linkMap", "Add"), CSharpHelper.MkCSharpStringLiteralExpression(creatorM.Key), SyntaxFactory.IdentifierName("_temp")) ); stmtList.Add(addLinkItem); } var staticConstrutor = SyntaxFactory.ConstructorDeclaration( SyntaxFactory.Identifier("Application")) .WithModifiers( SyntaxFactory.TokenList( SyntaxFactory.Token(SyntaxKind.StaticKeyword))) .WithBody(SyntaxFactory.Block(stmtList)) .NormalizeWhitespace(); return(staticConstrutor); }
public static StatementSyntax MkCSharpAssert(ExpressionSyntax expr, string errorMsg) { return(SyntaxFactory.IfStatement( CSharpHelper.MkCSharpNot(expr), SyntaxFactory.ThrowStatement(MkCSharpObjectCreationExpression(SyntaxFactory.IdentifierName("PrtAssertFailureException"), CSharpHelper.MkCSharpStringLiteralExpression(errorMsg))))); }
public static ExpressionSyntax MkCSharpDot(string first, params string[] names) { return(CSharpHelper.MkCSharpDot(SyntaxFactory.IdentifierName(first), names)); }
public SyntaxNode MkClass() { var generator = translator.generator; //StartState property (getter only, since there's no setter in the base class): string startState = machineInfo.initStateName; var startStateProperty = SyntaxFactory.PropertyDeclaration( SyntaxFactory.IdentifierName("PrtState"), SyntaxFactory.Identifier("StartState")) .WithModifiers( SyntaxFactory.TokenList( new[] { SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.OverrideKeyword) })) .WithAccessorList( SyntaxFactory.AccessorList( SyntaxFactory.SingletonList <AccessorDeclarationSyntax>( SyntaxFactory.AccessorDeclaration( SyntaxKind.GetAccessorDeclaration, SyntaxFactory.Block( SyntaxFactory.SingletonList <StatementSyntax>( SyntaxFactory.ReturnStatement( SyntaxFactory.IdentifierName(startState)))))))) .NormalizeWhitespace(); machineMembers.Add(startStateProperty); //Add getters/setters for machine variables: int ind = 0; foreach (var pair in machineInfo.localVariableToVarInfo) { SyntaxTokenList modifiers = new SyntaxTokenList(); modifiers = modifiers.Add(SyntaxFactory.Token(SyntaxKind.PublicKeyword)); var getBody = SyntaxFactory.SingletonList <StatementSyntax>(SyntaxFactory.ReturnStatement( (ExpressionSyntax)CSharpHelper.MkCSharpElementAccessExpression( SyntaxFactory.IdentifierName("fields"), ind))); var setBody = SyntaxFactory.SingletonList <StatementSyntax>((StatementSyntax)CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement( (ExpressionSyntax)CSharpHelper.MkCSharpElementAccessExpression( SyntaxFactory.IdentifierName("fields"), ind), SyntaxFactory.IdentifierName("value"))); AccessorDeclarationSyntax[] accessorList = new AccessorDeclarationSyntax[] { CSharpHelper.MkCSharpAccessor("get", getBody), CSharpHelper.MkCSharpAccessor("set", setBody) }; machineMembers.Add(CSharpHelper.MkCSharpPropertyDecl("PrtValue", VarName(pair.Key), modifiers, accessorList)); ind += 1; } var skeletonMethodBody = generator.ReturnStatement(generator.ObjectCreationExpression(generator.IdentifierName(machineName))); var skeletonMethodDecl = generator.MethodDeclaration("MakeSkeleton", null, null, generator.IdentifierName(machineInfo.IsReal ? "PrtImplMachine" : "PrtSpecMachine"), Accessibility.Public, DeclarationModifiers.Override, new SyntaxNode[] { skeletonMethodBody }); machineMembers.Add(skeletonMethodDecl); //(this.GetType()) var returnMethodPars = new SyntaxNode[] { generator.InvocationExpression(generator.MemberAccessExpression(generator.ThisExpression(), generator.IdentifierName("GetType"))) }; //return app.NextMachineInstanceNumber(this.GetType()); var nextInstanceNumberMethodBody = generator.ReturnStatement(generator.InvocationExpression(generator.MemberAccessExpression( generator.IdentifierName("app"), generator.IdentifierName("NextMachineInstanceNumber")), returnMethodPars)); //(StateImpl app) if (machineInfo.IsReal) { var methodPars = new SyntaxNode[] { generator.ParameterDeclaration("app", generator.IdentifierName("StateImpl")) }; var nextInstanceNumberMethodDecl = generator.MethodDeclaration("NextInstanceNumber", methodPars, //null, generator.IdentifierName("int"), null, generator.TypeExpression(SpecialType.System_Int32), Accessibility.Public, DeclarationModifiers.Override, new SyntaxNode[] { nextInstanceNumberMethodBody }); machineMembers.Add(nextInstanceNumberMethodDecl); } //Name property (getter only): var nameProperty = SyntaxFactory.PropertyDeclaration( SyntaxFactory.PredefinedType( SyntaxFactory.Token(SyntaxKind.StringKeyword)), SyntaxFactory.Identifier("Name")) .WithModifiers( SyntaxFactory.TokenList( new[] { SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.OverrideKeyword) })) .WithAccessorList( SyntaxFactory.AccessorList( SyntaxFactory.SingletonList <AccessorDeclarationSyntax>( SyntaxFactory.AccessorDeclaration( SyntaxKind.GetAccessorDeclaration, SyntaxFactory.Block( SyntaxFactory.SingletonList <StatementSyntax>( SyntaxFactory.ReturnStatement( SyntaxFactory.IdentifierName("\"" + machineName + "\"")))))))) .NormalizeWhitespace(); machineMembers.Add(nameProperty); //constructor for cloning var constructor_1 = generator.ConstructorDeclaration(machineName, null, Accessibility.Public, baseConstructorArguments: new SyntaxNode[0]); machineMembers.Add(constructor_1); //Machine class constructor //public PONG(StateImpl app, int maxB, bool assume): base (app, maxB, assume) List <SyntaxNode> constructorStmtList = new List <SyntaxNode>(); foreach (var varInfo in machineInfo.localVariableToVarInfo.Values) { var defaultValue = CSharpHelper.MkCSharpInvocationExpression(SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("PrtValue"), SyntaxFactory.IdentifierName("PrtMkDefaultValue")), translator.typeContext.PTypeToCSharpExpr(varInfo.type)); constructorStmtList.Add(SyntaxFactory.ExpressionStatement(CSharpHelper.MkCSharpInvocationExpression(CSharpHelper.MkCSharpDot("fields", "Add"), defaultValue))); } if (machineInfo.IsReal) { var constructorPars = new SyntaxNode[] { generator.ParameterDeclaration("app", generator.IdentifierName("StateImpl")), generator.ParameterDeclaration("maxB", generator.TypeExpression(SpecialType.System_Int32)), generator.ParameterDeclaration("assume", generator.TypeExpression(SpecialType.System_Boolean)) }; var baseConstructorArgs = new SyntaxNode[] { generator.IdentifierName("app"), generator.IdentifierName("maxB"), generator.IdentifierName("assume") }; var constructor_2 = generator.ConstructorDeclaration(machineName, constructorPars, Accessibility.Public, baseConstructorArguments: baseConstructorArgs, statements: constructorStmtList); machineMembers.Add(constructor_2); } else { var constructorPars = new SyntaxNode[] { generator.ParameterDeclaration("app", generator.IdentifierName("StateImpl")) }; var baseConstructorArgs = new SyntaxNode[] { generator.IdentifierName("app") }; var constructor_2 = generator.ConstructorDeclaration(machineName, constructorPars, Accessibility.Public, baseConstructorArguments: baseConstructorArgs, statements: constructorStmtList); machineMembers.Add(constructor_2); } // Fun classes for all functions of the machine: foreach (var pair in machineInfo.funNameToFunInfo) { MkFunctionDecl funDecl = new MkFunctionDecl(pair.Key, pair.Value, this, translator); funDecl.AddFunClass(); } foreach (var pair in machineInfo.stateNameToStateInfo) { string stateName = pair.Key; string stateType = stateName + "_Class"; //state instantiation for the machine class constructor: //Example: //PONG_Pong_WaitPing = new PONG_Pong_WaitPing_Class("PONG_Pong_WaitPing", AnonFun1, AnonFun0, false, StateTemperature.Warm); //Consider 6 cases (write a mmethod): for each bool value of hasNullTransition, there are 3 possible "temperatures" mainConstructorFields.Add(MkStateInstantiation(stateName, stateType, pair.Value.entryActionName, pair.Value.exitFunName, pair.Value.hasNullTransition, pair.Value.temperature)); } //State classes for all states of the machine: foreach (var pair in machineInfo.stateNameToStateInfo) { string stateName = pair.Key; string stateType = stateName + "_Class"; //state class declaration: var stateClassDecl = SyntaxFactory.ClassDeclaration(stateType) .WithModifiers( SyntaxFactory.TokenList( SyntaxFactory.Token(SyntaxKind.PublicKeyword))) .WithBaseList( SyntaxFactory.BaseList( SyntaxFactory.SingletonSeparatedList <BaseTypeSyntax>( SyntaxFactory.SimpleBaseType( SyntaxFactory.IdentifierName("PrtState"))))) .WithMembers( SyntaxFactory.SingletonList <MemberDeclarationSyntax>( SyntaxFactory.ConstructorDeclaration( SyntaxFactory.Identifier(stateType)) .WithModifiers( SyntaxFactory.TokenList( SyntaxFactory.Token(SyntaxKind.PublicKeyword))) .WithParameterList( SyntaxFactory.ParameterList( SyntaxFactory.SeparatedList <ParameterSyntax>( new SyntaxNodeOrToken[] { SyntaxFactory.Parameter( SyntaxFactory.Identifier("name")) .WithType( SyntaxFactory.PredefinedType( SyntaxFactory.Token(SyntaxKind.StringKeyword))), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Parameter( SyntaxFactory.Identifier("entryFun")) .WithType( SyntaxFactory.IdentifierName("PrtFun")), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Parameter( SyntaxFactory.Identifier("exitFun")) .WithType( SyntaxFactory.IdentifierName("PrtFun")), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Parameter( SyntaxFactory.Identifier("hasNullTransition")) .WithType( SyntaxFactory.PredefinedType( SyntaxFactory.Token(SyntaxKind.BoolKeyword))), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Parameter( SyntaxFactory.Identifier("temperature")) .WithType( SyntaxFactory.IdentifierName("StateTemperature")) }))) .WithInitializer( SyntaxFactory.ConstructorInitializer( SyntaxKind.BaseConstructorInitializer, SyntaxFactory.ArgumentList( SyntaxFactory.SeparatedList <ArgumentSyntax>( new SyntaxNodeOrToken[] { SyntaxFactory.Argument( SyntaxFactory.IdentifierName("name")), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument( SyntaxFactory.IdentifierName("entryFun")), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument( SyntaxFactory.IdentifierName("exitFun")), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument( SyntaxFactory.IdentifierName("hasNullTransition")), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument( SyntaxFactory.IdentifierName("temperature")) })))) .WithBody( SyntaxFactory.Block()))) .NormalizeWhitespace(); machineMembers.Add(stateClassDecl); //state declaration: //Example: public static Init Init_State; var stateDeclaration = SyntaxFactory.FieldDeclaration( SyntaxFactory.VariableDeclaration( SyntaxFactory.IdentifierName(stateType)) .WithVariables( SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>( SyntaxFactory.VariableDeclarator( SyntaxFactory.Identifier(stateName))))) .WithModifiers( SyntaxFactory.TokenList( new[] { SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword) })) .NormalizeWhitespace(); machineMembers.Add(stateDeclaration); //Add DoDecls to the StateInfo: foreach (var doFun in pair.Value.dos) { ExpressionSyntax doFunExpr = SyntaxFactory.IdentifierName(doFun.Value); if (doFun.Value == "ignore") { doFunExpr = SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("PrtFun"), SyntaxFactory.IdentifierName("IgnoreFun")); } mainConstructorFields.Add( SyntaxFactory.ExpressionStatement( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(stateName), SyntaxFactory.IdentifierName("dos")), SyntaxFactory.IdentifierName("Add"))) .WithArgumentList( SyntaxFactory.ArgumentList( SyntaxFactory.SeparatedList <ArgumentSyntax>( new SyntaxNodeOrToken[] { SyntaxFactory.Argument( translator.GetEventVar(doFun.Key)), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument(doFunExpr) })))) .NormalizeWhitespace() ); } //Add deferred events (if any) to the state: foreach (var deferredEvent in pair.Value.deferredEvents) { mainConstructorFields.Add( SyntaxFactory.ExpressionStatement( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(stateName), SyntaxFactory.IdentifierName("deferredSet")), SyntaxFactory.IdentifierName("Add"))) .WithArgumentList( SyntaxFactory.ArgumentList( SyntaxFactory.SingletonSeparatedList <ArgumentSyntax>( SyntaxFactory.Argument( translator.GetEventVar(deferredEvent)))))) .NormalizeWhitespace() ); } //Transition instantiations for the state of the machine: //"transition" + "_" + transition_count foreach (var transition in pair.Value.transitions) { string trigger = transition.Key; string transition_name = "transition" + "_" + transition_count; if (transition.Value.IsPush) { //push transition: mainConstructorFields.Add( SyntaxFactory.LocalDeclarationStatement( SyntaxFactory.VariableDeclaration( SyntaxFactory.IdentifierName("PrtTransition")) .WithVariables( SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>( SyntaxFactory.VariableDeclarator( SyntaxFactory.Identifier(transition_name)) .WithInitializer( SyntaxFactory.EqualsValueClause( SyntaxFactory.ObjectCreationExpression( SyntaxFactory.IdentifierName("PrtTransition")) .WithArgumentList( SyntaxFactory.ArgumentList( SyntaxFactory.SeparatedList <ArgumentSyntax>( new SyntaxNodeOrToken[] { SyntaxFactory.Argument( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("PrtFun"), SyntaxFactory.IdentifierName("IgnoreFun"))), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument( SyntaxFactory.IdentifierName(transition.Value.target)), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument( SyntaxFactory.LiteralExpression( SyntaxKind.TrueLiteralExpression)) })))))))) .NormalizeWhitespace()); } else { //goto transition: mainConstructorFields.Add( SyntaxFactory.LocalDeclarationStatement( SyntaxFactory.VariableDeclaration( SyntaxFactory.IdentifierName("PrtTransition")) .WithVariables( SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>( SyntaxFactory.VariableDeclarator( SyntaxFactory.Identifier("transition" + "_" + transition_count)) .WithInitializer( SyntaxFactory.EqualsValueClause( SyntaxFactory.ObjectCreationExpression( SyntaxFactory.IdentifierName("PrtTransition")) .WithArgumentList( SyntaxFactory.ArgumentList( SyntaxFactory.SeparatedList <ArgumentSyntax>( new SyntaxNodeOrToken[] { SyntaxFactory.Argument( SyntaxFactory.IdentifierName(transition.Value.transFunName)), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument( SyntaxFactory.IdentifierName(transition.Value.target)), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument( SyntaxFactory.LiteralExpression( SyntaxKind.FalseLiteralExpression)) })))))))) .NormalizeWhitespace()); } //Add transition to the state: mainConstructorFields.Add( SyntaxFactory.ExpressionStatement( SyntaxFactory.InvocationExpression( SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName(stateName), SyntaxFactory.IdentifierName("transitions")), SyntaxFactory.IdentifierName("Add"))) .WithArgumentList( SyntaxFactory.ArgumentList( SyntaxFactory.SeparatedList <ArgumentSyntax>( new SyntaxNodeOrToken[] { SyntaxFactory.Argument( translator.GetEventVar(trigger)), SyntaxFactory.Token(SyntaxKind.CommaToken), SyntaxFactory.Argument( SyntaxFactory.IdentifierName(transition_name)) })))) .NormalizeWhitespace() ); transition_count += 1; } } //Constructor for the machine class: //static Machine() var mainConstructor = SyntaxFactory.ConstructorDeclaration( SyntaxFactory.Identifier(machineName)) .WithModifiers( SyntaxFactory.TokenList( SyntaxFactory.Token(SyntaxKind.StaticKeyword))) .WithBody( SyntaxFactory.Block(mainConstructorFields )) .NormalizeWhitespace(); machineMembers.Add(mainConstructor); var mainMachineClassDecl = generator.ClassDeclaration( machineName, typeParameters: null, accessibility: Accessibility.Public, baseType: generator.IdentifierName(machineInfo.IsReal ? "PrtImplMachine" : "PrtSpecMachine"), members: machineMembers); return(mainMachineClassDecl); }