コード例 #1
0
        private static ConversionOperatorDeclarationSyntax GenerateConversionDeclarationWorker(
            IMethodSymbol method,
            CodeGenerationDestination destination,
            Workspace workspace,
            CodeGenerationOptions options,
            ParseOptions parseOptions)
        {
            var hasNoBody = !options.GenerateMethodBodies || method.IsExtern;

            var reusableSyntax = GetReuseableSyntaxNodeForSymbol <ConversionOperatorDeclarationSyntax>(method, options);

            if (reusableSyntax != null)
            {
                return(reusableSyntax);
            }

            var operatorToken = SyntaxFactory.Token(SyntaxFacts.GetOperatorKind(method.MetadataName));
            var keyword       = method.MetadataName == WellKnownMemberNames.ImplicitConversionName
                ? SyntaxFactory.Token(SyntaxKind.ImplicitKeyword)
                : SyntaxFactory.Token(SyntaxKind.ExplicitKeyword);

            var declaration = SyntaxFactory.ConversionOperatorDeclaration(
                attributeLists: AttributeGenerator.GenerateAttributeLists(method.GetAttributes(), options),
                modifiers: GenerateModifiers(method),
                implicitOrExplicitKeyword: keyword,
                operatorKeyword: SyntaxFactory.Token(SyntaxKind.OperatorKeyword),
                type: method.ReturnType.GenerateTypeSyntax(),
                parameterList: ParameterGenerator.GenerateParameterList(method.Parameters, isExplicit: false, options: options),
                body: hasNoBody ? null : StatementGenerator.GenerateBlock(method),
                semicolonToken: hasNoBody ? SyntaxFactory.Token(SyntaxKind.SemicolonToken) : new SyntaxToken());

            declaration = UseExpressionBodyIfDesired(workspace, declaration, parseOptions);

            return(declaration);
        }
コード例 #2
0
        private static OperatorDeclarationSyntax GenerateOperatorDeclarationWorker(
            IMethodSymbol method,
            CodeGenerationDestination destination,
            CodeGenerationOptions options)
        {
            var hasNoBody = !options.GenerateMethodBodies || method.IsExtern;

            var operatorSyntaxKind = SyntaxFacts.GetOperatorKind(method.MetadataName);

            if (operatorSyntaxKind == SyntaxKind.None)
            {
                throw new ArgumentException(string.Format(WorkspacesResources.Cannot_generate_code_for_unsupported_operator_0, method.Name), nameof(method));
            }

            var operatorToken = SyntaxFactory.Token(operatorSyntaxKind);

            return(SyntaxFactory.OperatorDeclaration(
                       attributeLists: AttributeGenerator.GenerateAttributeLists(method.GetAttributes(), options),
                       modifiers: GenerateModifiers(method),
                       returnType: method.ReturnType.GenerateTypeSyntax(),
                       operatorKeyword: SyntaxFactory.Token(SyntaxKind.OperatorKeyword),
                       operatorToken: operatorToken,
                       parameterList: ParameterGenerator.GenerateParameterList(method.Parameters, isExplicit: false, options: options),
                       body: hasNoBody ? null : StatementGenerator.GenerateBlock(method),
                       semicolonToken: hasNoBody ? SyntaxFactory.Token(SyntaxKind.SemicolonToken) : new SyntaxToken()));
        }
コード例 #3
0
        private static BlockSyntax GenerateBlock(
            IMethodSymbol constructor)
        {
            var statements = CodeGenerationDestructorInfo.GetStatements(constructor) == null
                ? default
                : StatementGenerator.GenerateStatements(CodeGenerationDestructorInfo.GetStatements(constructor));

            return(SyntaxFactory.Block(statements));
        }
コード例 #4
0
        private static TDeclarationNode AddStatementsToMemberDeclaration <TDeclarationNode>(TDeclarationNode destinationMember, IEnumerable <SyntaxNode> statements, MemberDeclarationSyntax memberDeclaration) where TDeclarationNode : SyntaxNode
        {
            var body = memberDeclaration.GetBody();

            if (body == null)
            {
                return(destinationMember);
            }

            var statementNodes = body.Statements.ToList();

            statementNodes.AddRange(StatementGenerator.GenerateStatements(statements));

            var finalBody   = body.WithStatements(SyntaxFactory.List <StatementSyntax>(statementNodes));
            var finalMember = memberDeclaration.WithBody(finalBody);

            return(Cast <TDeclarationNode>(finalMember));
        }
コード例 #5
0
 internal static BlockSyntax GenerateBlock(IMethodSymbol method)
 {
     return(SyntaxFactory.Block(
                StatementGenerator.GenerateStatements(CodeGenerationMethodInfo.GetStatements(method))));
 }