Esempio n. 1
0
        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);
        }
 public void AddTypeDeclaration(string typeName)
 {
     TypeDeclaration.Add(
         (FieldDeclarationSyntax)CSharpHelper.MkCSharpFieldDeclaration(
             SyntaxFactory.IdentifierName("PrtType"),
             typeName,
             SyntaxFactory.Token(SyntaxKind.PublicKeyword),
             SyntaxFactory.Token(SyntaxKind.StaticKeyword)));
 }