Пример #1
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        public override FieldDeclarationSyntax EmitSyntax()
        {
            var declarator = VariableDeclarator(Identifier(Member.Name));

            if (Member.Initializer != null)
            {
                declarator = declarator.WithInitializer(EqualsValueClause(ExpressionSyntaxEmitter.EmitSyntax(Member.Initializer)));
            }

            OutputSyntax =
                FieldDeclaration(
                    VariableDeclaration(
                        Member.Type.GetTypeNameSyntax()
                        )
                    .WithVariables(
                        SingletonSeparatedList <VariableDeclaratorSyntax>(
                            declarator
                            )
                        )
                    )
                .WithModifiers(
                    EmitMemberModifiers()
                    );

            //TODO: emit attributes

            return(OutputSyntax);
        }
Пример #2
0
        public static StatementSyntax EmitSyntax(AbstractStatement statement)
        {
            if (statement is ReturnStatement statementReturn)
            {
                return(ReturnStatement(ExpressionSyntaxEmitter.EmitSyntax(statementReturn.Expression)));
            }
            if (statement is BlockStatement statementBlock)
            {
                return(statementBlock.ToSyntax());
            }
            if (statement is ThrowStatement statementThrow)
            {
                return(ThrowStatement(ExpressionSyntaxEmitter.EmitSyntax(statementThrow.Exception)));
            }
            if (statement is ExpressionStatement statementExpression)
            {
                return(ExpressionStatement(ExpressionSyntaxEmitter.EmitSyntax(statementExpression.Expression)));
            }
            if (statement is VariableDeclarationStatement statementVariable)
            {
                return(EmitLocalDeclarationSyntax(statementVariable));
            }
            if (statement is IfStatement statementIf)
            {
                return(EmitIfStatementSyntax(statementIf));
            }
            if (statement is LockStatement statementLock)
            {
                return(LockStatement(ExpressionSyntaxEmitter.EmitSyntax(statementLock.SyncRoot), statementLock.Body.ToSyntax()));
            }

            //TODO: support other types of statements

            throw new NotSupportedException($"Syntax emitter is not supported for statement of type '{statement.GetType().Name}'.");
        }
Пример #3
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        private static StatementSyntax EmitIfStatementSyntax(IfStatement statement)
        {
            var syntax = IfStatement(ExpressionSyntaxEmitter.EmitSyntax(statement.Condition), statement.ThenBlock.ToSyntax());

            if (statement.ElseBlock != null)
            {
                syntax = syntax.WithElse(ElseClause(statement.ElseBlock.ToSyntax()));
            }

            return(syntax);
        }
Пример #4
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        private static LocalDeclarationStatementSyntax EmitLocalDeclarationSyntax(VariableDeclarationStatement statement)
        {
            var variable = statement.Variable;

            var declaration = (variable.Type != null
                ? VariableDeclaration(variable.Type.GetTypeNameSyntax())
                : VariableDeclaration(IdentifierName("var")));

            var declarator = VariableDeclarator(Identifier(variable.Name));

            if (statement.InitialValue != null)
            {
                declarator = declarator.WithInitializer(EqualsValueClause(ExpressionSyntaxEmitter.EmitSyntax(statement.InitialValue)));
            }

            return(LocalDeclarationStatement(declaration.WithVariables(SingletonSeparatedList(declarator))));
        }
Пример #5
0
        //-----------------------------------------------------------------------------------------------------------------------------------------------------

        private static ArgumentSyntax EmitArgument(Argument argument)
        {
            var syntax = Argument(ExpressionSyntaxEmitter.EmitSyntax(argument.Expression));

            switch (argument.Modifier)
            {
            case MethodParameterModifier.Ref:
                syntax = syntax.WithRefOrOutKeyword(Token(SyntaxKind.RefKeyword));
                break;

            case MethodParameterModifier.Out:
                syntax = syntax.WithRefOrOutKeyword(Token(SyntaxKind.OutKeyword));
                break;
            }

            return(syntax);
        }
Пример #6
0
        public static AttributeSyntax EmitSyntax(AttributeDescription description)
        {
            var syntax = Attribute(SyntaxHelpers.GetTypeFullNameSyntax(description.AttributeType));

            if (description.ConstructorArguments.Count > 0 || description.PropertyValues.Count > 0)
            {
                syntax = syntax
                         .WithArgumentList(
                    AttributeArgumentList(
                        SeparatedList <AttributeArgumentSyntax>(
                            description.ConstructorArguments.Select(arg =>
                                                                    AttributeArgument(SyntaxHelpers.GetLiteralSyntax(arg)))
                            .Concat(description.PropertyValues.Select(pv =>
                                                                      AttributeArgument(ExpressionSyntaxEmitter.EmitSyntax(pv.Value))
                                                                      .WithNameEquals(NameEquals(IdentifierName(pv.Name)))
                                                                      ))
                            )
                        )
                    );
            }

            return(syntax);
        }