コード例 #1
0
        internal static DestructorDeclarationSyntax GenerateDestructorDeclaration(
            IMethodSymbol destructor, CodeGenerationDestination destination, CodeGenerationOptions options)
        {
            options = options ?? CodeGenerationOptions.Default;

            var reusableSyntax = GetReuseableSyntaxNodeForSymbol <DestructorDeclarationSyntax>(destructor, options);

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

            bool hasNoBody = !options.GenerateMethodBodies;

            var declaration = SyntaxFactory.DestructorDeclaration(
                attributeLists: AttributeGenerator.GenerateAttributeLists(destructor.GetAttributes(), options),
                modifiers: default(SyntaxTokenList),
                tildeToken: SyntaxFactory.Token(SyntaxKind.TildeToken),
                identifier: CodeGenerationDestructorInfo.GetTypeName(destructor).ToIdentifierToken(),
                parameterList: SyntaxFactory.ParameterList(),
                body: hasNoBody ? null : GenerateBlock(destructor),
                semicolonToken: hasNoBody ? SyntaxFactory.Token(SyntaxKind.SemicolonToken) : default(SyntaxToken));

            return(AddCleanupAnnotationsTo(
                       ConditionallyAddDocumentationCommentTo(declaration, destructor, options)));
        }
コード例 #2
0
        internal static ConstructorDeclarationSyntax GenerateConstructorDeclaration(
            IMethodSymbol constructor,
            Workspace workspace,
            CodeGenerationOptions options,
            ParseOptions parseOptions)
        {
            options ??= CodeGenerationOptions.Default;

            var reusableSyntax = GetReuseableSyntaxNodeForSymbol <ConstructorDeclarationSyntax>(constructor, options);

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

            var hasNoBody = !options.GenerateMethodBodies;

            var declaration = SyntaxFactory.ConstructorDeclaration(
                attributeLists: AttributeGenerator.GenerateAttributeLists(constructor.GetAttributes(), options),
                modifiers: GenerateModifiers(constructor, options),
                identifier: CodeGenerationConstructorInfo.GetTypeName(constructor).ToIdentifierToken(),
                parameterList: ParameterGenerator.GenerateParameterList(constructor.Parameters, isExplicit: false, options: options),
                initializer: GenerateConstructorInitializer(constructor),
                body: hasNoBody ? null : GenerateBlock(constructor),
                semicolonToken: hasNoBody ? SyntaxFactory.Token(SyntaxKind.SemicolonToken) : default);
コード例 #3
0
        public static FieldDeclarationSyntax GenerateFieldDeclaration(
            IFieldSymbol field, CSharpCodeGenerationContextInfo info, CancellationToken cancellationToken)
        {
            var reusableSyntax = GetReuseableSyntaxNodeForSymbol <FieldDeclarationSyntax>(field, info);

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

            var initializer = CodeGenerationFieldInfo.GetInitializer(field) is ExpressionSyntax initializerNode
                ? SyntaxFactory.EqualsValueClause(initializerNode)
                : GenerateEqualsValue(field);

            var fieldDeclaration = SyntaxFactory.FieldDeclaration(
                AttributeGenerator.GenerateAttributeLists(field.GetAttributes(), info),
                GenerateModifiers(field, info),
                SyntaxFactory.VariableDeclaration(
                    field.Type.GenerateTypeSyntax(),
                    SyntaxFactory.SingletonSeparatedList(
                        AddAnnotationsTo(field, SyntaxFactory.VariableDeclarator(field.Name.ToIdentifierToken(), null, initializer)))));

            return(AddFormatterAndCodeGeneratorAnnotationsTo(
                       ConditionallyAddDocumentationCommentTo(fieldDeclaration, field, info, cancellationToken)));
        }
コード例 #4
0
        internal static ConstructorDeclarationSyntax GenerateConstructorDeclaration(
            IMethodSymbol constructor, CodeGenerationDestination destination,
            Workspace workspace, CodeGenerationOptions options, ParseOptions parseOptions)
        {
            options = options ?? CodeGenerationOptions.Default;

            var reusableSyntax = GetReuseableSyntaxNodeForSymbol <ConstructorDeclarationSyntax>(constructor, options);

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

            var hasNoBody = !options.GenerateMethodBodies;

            var declaration = SyntaxFactory.ConstructorDeclaration(
                attributeLists: AttributeGenerator.GenerateAttributeLists(constructor.GetAttributes(), options),
                modifiers: GenerateModifiers(constructor, options),
                identifier: CodeGenerationConstructorInfo.GetTypeName(constructor).ToIdentifierToken(),
                parameterList: ParameterGenerator.GenerateParameterList(constructor.Parameters, isExplicit: false, options: options),
                initializer: GenerateConstructorInitializer(constructor),
                body: hasNoBody ? null : GenerateBlock(constructor),
                semicolonToken: hasNoBody ? SyntaxFactory.Token(SyntaxKind.SemicolonToken) : default(SyntaxToken));

            declaration = UseExpressionBodyIfDesired(workspace, declaration, parseOptions);

            return(AddCleanupAnnotationsTo(
                       ConditionallyAddDocumentationCommentTo(declaration, constructor, options)));
        }
コード例 #5
0
        private static ConversionOperatorDeclarationSyntax GenerateConversionDeclarationWorker(
            IMethodSymbol method,
            CodeGenerationDestination destination,
            CodeGenerationOptions options)
        {
            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);

            return(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()));
        }
コード例 #6
0
ファイル: DestructorGenerator.cs プロジェクト: belav/roslyn
        internal static DestructorDeclarationSyntax GenerateDestructorDeclaration(
            IMethodSymbol destructor,
            CodeGenerationOptions options
            )
        {
            options ??= CodeGenerationOptions.Default;

            var reusableSyntax = GetReuseableSyntaxNodeForSymbol <DestructorDeclarationSyntax>(
                destructor,
                options
                );

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

            var hasNoBody = !options.GenerateMethodBodies;

            var declaration = SyntaxFactory.DestructorDeclaration(
                attributeLists: AttributeGenerator.GenerateAttributeLists(
                    destructor.GetAttributes(),
                    options
                    ),
                modifiers: default,
コード例 #7
0
        private static MemberDeclarationSyntax GenerateIndexerDeclaration(
            IPropertySymbol property,
            CodeGenerationDestination destination,
            CodeGenerationOptions options,
            ParseOptions parseOptions
            )
        {
            var explicitInterfaceSpecifier = GenerateExplicitInterfaceSpecifier(
                property.ExplicitInterfaceImplementations
                );

            var declaration = SyntaxFactory.IndexerDeclaration(
                attributeLists: AttributeGenerator.GenerateAttributeLists(
                    property.GetAttributes(),
                    options
                    ),
                modifiers: GenerateModifiers(property, destination, options),
                type: GenerateTypeSyntax(property),
                explicitInterfaceSpecifier: explicitInterfaceSpecifier,
                parameterList: ParameterGenerator.GenerateBracketedParameterList(
                    property.Parameters,
                    explicitInterfaceSpecifier != null,
                    options
                    ),
                accessorList: GenerateAccessorList(property, destination, options, parseOptions)
                );

            declaration = UseExpressionBodyIfDesired(options, declaration, parseOptions);

            return(AddFormatterAndCodeGeneratorAnnotationsTo(
                       AddAnnotationsTo(property, declaration)
                       ));
        }
コード例 #8
0
        private static MemberDeclarationSyntax GeneratePropertyDeclaration(
            IPropertySymbol property, CodeGenerationDestination destination,
            Workspace workspace, CodeGenerationOptions options, ParseOptions parseOptions)
        {
            var initializer = CodeGenerationPropertyInfo.GetInitializer(property) is ExpressionSyntax initializerNode
                ? SyntaxFactory.EqualsValueClause(initializerNode)
                : null;

            var explicitInterfaceSpecifier = GenerateExplicitInterfaceSpecifier(property.ExplicitInterfaceImplementations);

            var accessorList = GenerateAccessorList(property, destination, workspace, options, parseOptions);

            var propertyDeclaration = SyntaxFactory.PropertyDeclaration(
                attributeLists: AttributeGenerator.GenerateAttributeLists(property.GetAttributes(), options),
                modifiers: GenerateModifiers(property, destination, options),
                type: GenerateTypeSyntax(property),
                explicitInterfaceSpecifier: explicitInterfaceSpecifier,
                identifier: property.Name.ToIdentifierToken(),
                accessorList: accessorList,
                expressionBody: null,
                initializer: initializer);

            propertyDeclaration = UseExpressionBodyIfDesired(
                workspace, propertyDeclaration, parseOptions);

            return(AddFormatterAndCodeGeneratorAnnotationsTo(
                       AddAnnotationsTo(property, propertyDeclaration)));
        }
コード例 #9
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()));
        }
コード例 #10
0
ファイル: NamedTypeGenerator.cs プロジェクト: belav/roslyn
 private static SyntaxList <AttributeListSyntax> GenerateAttributeDeclarations(
     INamedTypeSymbol namedType,
     CodeGenerationOptions options
     )
 {
     return(AttributeGenerator.GenerateAttributeLists(namedType.GetAttributes(), options));
 }
コード例 #11
0
        private static OperatorDeclarationSyntax GenerateOperatorDeclarationWorker(
            IMethodSymbol method,
            CodeGenerationDestination destination,
            CSharpCodeGenerationContextInfo info)
        {
            var hasNoBody = !info.Context.GenerateMethodBodies || method.IsExtern || method.IsAbstract;

            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);
            var checkedToken  = SyntaxFacts.IsCheckedOperator(method.MetadataName)
                ? SyntaxFactory.Token(SyntaxKind.CheckedKeyword)
                : default;

            var operatorDecl = SyntaxFactory.OperatorDeclaration(
                attributeLists: AttributeGenerator.GenerateAttributeLists(method.GetAttributes(), info),
                modifiers: GenerateModifiers(method, destination, hasNoBody),
                returnType: method.ReturnType.GenerateTypeSyntax(),
                explicitInterfaceSpecifier: GenerateExplicitInterfaceSpecifier(method.ExplicitInterfaceImplementations),
                operatorKeyword: SyntaxFactory.Token(SyntaxKind.OperatorKeyword),
                checkedKeyword: checkedToken,
                operatorToken: operatorToken,
                parameterList: ParameterGenerator.GenerateParameterList(method.Parameters, isExplicit: false, info: info),
                body: hasNoBody ? null : StatementGenerator.GenerateBlock(method),
                expressionBody: null,
                semicolonToken: hasNoBody ? SyntaxFactory.Token(SyntaxKind.SemicolonToken) : new SyntaxToken());

            operatorDecl = UseExpressionBodyIfDesired(info, operatorDecl);
            return(operatorDecl);
        }
コード例 #12
0
        public static FieldDeclarationSyntax GenerateFieldDeclaration(
            IFieldSymbol field, CodeGenerationOptions options)
        {
            var reusableSyntax = GetReuseableSyntaxNodeForSymbol <VariableDeclaratorSyntax>(field, options);

            if (reusableSyntax != null)
            {
                if (reusableSyntax.Parent is VariableDeclarationSyntax variableDeclaration)
                {
                    var newVariableDeclaratorsList = new SeparatedSyntaxList <VariableDeclaratorSyntax>().Add(reusableSyntax);
                    var newVariableDeclaration     = variableDeclaration.WithVariables(newVariableDeclaratorsList);
                    if (variableDeclaration.Parent is FieldDeclarationSyntax fieldDecl)
                    {
                        return(fieldDecl.WithDeclaration(newVariableDeclaration));
                    }
                }
            }

            var initializer = CodeGenerationFieldInfo.GetInitializer(field) is ExpressionSyntax initializerNode
                ? SyntaxFactory.EqualsValueClause(initializerNode)
                : GenerateEqualsValue(field);

            var fieldDeclaration = SyntaxFactory.FieldDeclaration(
                AttributeGenerator.GenerateAttributeLists(field.GetAttributes(), options),
                GenerateModifiers(field, options),
                SyntaxFactory.VariableDeclaration(
                    field.Type.GenerateTypeSyntax(),
                    SyntaxFactory.SingletonSeparatedList(
                        AddAnnotationsTo(field, SyntaxFactory.VariableDeclarator(field.Name.ToIdentifierToken(), null, initializer)))));

            return(AddFormatterAndCodeGeneratorAnnotationsTo(
                       ConditionallyAddDocumentationCommentTo(fieldDeclaration, field, options)));
        }
コード例 #13
0
        public override TDeclarationNode AddAttributes <TDeclarationNode>(
            TDeclarationNode destination,
            IEnumerable <AttributeData> attributes,
            SyntaxToken?target,
            CodeGenerationOptions options,
            CancellationToken cancellationToken)
        {
            if (target.HasValue && !target.Value.IsValidAttributeTarget())
            {
                throw new ArgumentException("target");
            }

            var attributeSyntaxList = AttributeGenerator.GenerateAttributeLists(attributes.ToImmutableArray(), options, target).ToArray();

            // Handle all members including types.
            var member = destination as MemberDeclarationSyntax;

            if (member != null)
            {
                return(Cast <TDeclarationNode>(member.AddAttributeLists(attributeSyntaxList)));
            }

            // Handle accessors
            var accessor = destination as AccessorDeclarationSyntax;

            if (accessor != null)
            {
                return(Cast <TDeclarationNode>(accessor.AddAttributeLists(attributeSyntaxList)));
            }

            // Handle global attributes
            var compilationUnit = destination as CompilationUnitSyntax;

            if (compilationUnit != null)
            {
                return(Cast <TDeclarationNode>(compilationUnit.AddAttributeLists(attributeSyntaxList)));
            }

            // Handle parameters
            var parameter = destination as ParameterSyntax;

            if (parameter != null)
            {
                return(Cast <TDeclarationNode>(parameter.AddAttributeLists(attributeSyntaxList)));
            }

            var typeParameter = destination as TypeParameterSyntax;

            if (typeParameter != null)
            {
                return(Cast <TDeclarationNode>(typeParameter.AddAttributeLists(attributeSyntaxList)));
            }

            return(destination);
        }
コード例 #14
0
        private static TypeParameterSyntax GenerateTypeParameter(ITypeParameterSymbol symbol, CodeGenerationOptions options)
        {
            var varianceKeyword =
                symbol.Variance == VarianceKind.In ? SyntaxFactory.Token(SyntaxKind.InKeyword) :
                symbol.Variance == VarianceKind.Out ? SyntaxFactory.Token(SyntaxKind.OutKeyword) : default;

            return(SyntaxFactory.TypeParameter(
                       AttributeGenerator.GenerateAttributeLists(symbol.GetAttributes(), options),
                       varianceKeyword,
                       symbol.Name.ToIdentifierToken()));
        }
コード例 #15
0
 private static MemberDeclarationSyntax GenerateEventFieldDeclaration(
     IEventSymbol @event, CodeGenerationDestination destination, CSharpCodeGenerationContextInfo info)
 {
     return(AddFormatterAndCodeGeneratorAnnotationsTo(
                AddAnnotationsTo(@event,
                                 SyntaxFactory.EventFieldDeclaration(
                                     AttributeGenerator.GenerateAttributeLists(@event.GetAttributes(), info),
                                     GenerateModifiers(@event, destination, info),
                                     SyntaxFactory.VariableDeclaration(
                                         @event.Type.GenerateTypeSyntax(),
                                         SyntaxFactory.SingletonSeparatedList(SyntaxFactory.VariableDeclarator(@event.Name.ToIdentifierToken())))))));
 }
コード例 #16
0
 private static MemberDeclarationSyntax GenerateEventFieldDeclaration(
     IEventSymbol @event, CodeGenerationDestination destination, CodeGenerationOptions options)
 {
     return(AddCleanupAnnotationsTo(
                AddAnnotationsTo(@event,
                                 SyntaxFactory.EventFieldDeclaration(
                                     AttributeGenerator.GenerateAttributeLists(@event.GetAttributes(), options),
                                     GenerateModifiers(@event, destination, options),
                                     SyntaxFactory.VariableDeclaration(
                                         @event.Type.GenerateTypeSyntax(),
                                         SyntaxFactory.SingletonSeparatedList(SyntaxFactory.VariableDeclarator(@event.Name.ToIdentifierToken())))))));
 }
コード例 #17
0
        private static SyntaxList <AttributeListSyntax> GenerateAttributes(
            IMethodSymbol method, CodeGenerationOptions options, bool isExplicit)
        {
            var attributes = new List <AttributeListSyntax>();

            if (!isExplicit)
            {
                attributes.AddRange(AttributeGenerator.GenerateAttributeLists(method.GetAttributes(), options));
                attributes.AddRange(AttributeGenerator.GenerateAttributeLists(method.GetReturnTypeAttributes(), options, SyntaxFactory.Token(SyntaxKind.ReturnKeyword)));
            }

            return(attributes.ToSyntaxList());
        }
コード例 #18
0
        private static MemberDeclarationSyntax GenerateEventDeclarationWorker(
            IEventSymbol @event, CodeGenerationDestination destination, CodeGenerationOptions options)
        {
            var explicitInterfaceSpecifier = GenerateExplicitInterfaceSpecifier(@event.ExplicitInterfaceImplementations);

            return(AddCleanupAnnotationsTo(SyntaxFactory.EventDeclaration(
                                               attributeLists: AttributeGenerator.GenerateAttributeLists(@event.GetAttributes(), options),
                                               modifiers: GenerateModifiers(@event, destination, options),
                                               type: @event.Type.GenerateTypeSyntax(),
                                               explicitInterfaceSpecifier: explicitInterfaceSpecifier,
                                               identifier: @event.Name.ToIdentifierToken(),
                                               accessorList: GenerateAccessorList(@event, destination, options))));
        }
コード例 #19
0
        public static MemberDeclarationSyntax GeneratePropertyDeclaration(
            IPropertySymbol property, CodeGenerationDestination destination, CodeGenerationOptions options)
        {
            var explicitInterfaceSpecifier = GenerateExplicitInterfaceSpecifier(property.ExplicitInterfaceImplementations);

            return(AddCleanupAnnotationsTo(
                       AddAnnotationsTo(property, SyntaxFactory.PropertyDeclaration(
                                            attributeLists: AttributeGenerator.GenerateAttributeLists(property.GetAttributes(), options),
                                            modifiers: GenerateModifiers(property, destination, options),
                                            type: property.Type.GenerateTypeSyntax(),
                                            explicitInterfaceSpecifier: explicitInterfaceSpecifier,
                                            identifier: property.Name.ToIdentifierToken(),
                                            accessorList: GenerateAccessorList(property, destination, options)))));
        }
コード例 #20
0
        internal static DestructorDeclarationSyntax GenerateDestructorDeclaration(
            IMethodSymbol destructor, CSharpCodeGenerationContextInfo info, CancellationToken cancellationToken)
        {
            var reusableSyntax = GetReuseableSyntaxNodeForSymbol <DestructorDeclarationSyntax>(destructor, info);

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

            var hasNoBody = !info.Context.GenerateMethodBodies;

            var declaration = SyntaxFactory.DestructorDeclaration(
                attributeLists: AttributeGenerator.GenerateAttributeLists(destructor.GetAttributes(), info),
                modifiers: default,
コード例 #21
0
        private static MemberDeclarationSyntax GenerateIndexerDeclaration(
            IPropertySymbol property,
            CodeGenerationDestination destination,
            CodeGenerationOptions options)
        {
            var explicitInterfaceSpecifier = GenerateExplicitInterfaceSpecifier(property.ExplicitInterfaceImplementations);

            return(AddCleanupAnnotationsTo(
                       AddAnnotationsTo(property, SyntaxFactory.IndexerDeclaration(
                                            attributeLists: AttributeGenerator.GenerateAttributeLists(property.GetAttributes(), options),
                                            modifiers: GenerateModifiers(property, destination, options),
                                            type: property.Type.GenerateTypeSyntax(),
                                            explicitInterfaceSpecifier: explicitInterfaceSpecifier,
                                            parameterList: ParameterGenerator.GenerateBracketedParameterList(property.Parameters, explicitInterfaceSpecifier != null, options),
                                            accessorList: GenerateAccessorList(property, destination, options)))));
        }
コード例 #22
0
        private static SyntaxList <AttributeListSyntax> GenerateAttributes(
            IParameterSymbol parameter, bool isExplicit, CodeGenerationOptions options)
        {
            if (isExplicit)
            {
                return(default(SyntaxList <AttributeListSyntax>));
            }

            var attributes = parameter.GetAttributes();

            if (attributes.Length == 0)
            {
                return(default(SyntaxList <AttributeListSyntax>));
            }

            return(AttributeGenerator.GenerateAttributeLists(attributes, options));
        }
コード例 #23
0
        public static MemberDeclarationSyntax GeneratePropertyDeclaration(
            IPropertySymbol property, CodeGenerationDestination destination, CodeGenerationOptions options)
        {
            var initializerNode = CodeGenerationPropertyInfo.GetInitializer(property) as ExpressionSyntax;

            var initializer = initializerNode != null
                ? SyntaxFactory.EqualsValueClause(initializerNode)
                : default(EqualsValueClauseSyntax);

            var explicitInterfaceSpecifier = GenerateExplicitInterfaceSpecifier(property.ExplicitInterfaceImplementations);

            return(AddCleanupAnnotationsTo(
                       AddAnnotationsTo(property, SyntaxFactory.PropertyDeclaration(
                                            attributeLists: AttributeGenerator.GenerateAttributeLists(property.GetAttributes(), options),
                                            modifiers: GenerateModifiers(property, destination, options),
                                            type: property.Type.GenerateTypeSyntax(),
                                            explicitInterfaceSpecifier: explicitInterfaceSpecifier,
                                            identifier: property.Name.ToIdentifierToken(),
                                            accessorList: GenerateAccessorList(property, destination, options),
                                            initializer: initializer))));
        }
コード例 #24
0
        private static ConversionOperatorDeclarationSyntax GenerateConversionDeclarationWorker(
            IMethodSymbol method,
            CodeGenerationDestination destination,
            CSharpCodeGenerationContextInfo info)
        {
            var hasNoBody = !info.Context.GenerateMethodBodies || method.IsExtern;

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

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

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

            var checkedToken = SyntaxFacts.IsCheckedOperator(method.MetadataName)
                ? SyntaxFactory.Token(SyntaxKind.CheckedKeyword)
                : default;

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

            declaration = UseExpressionBodyIfDesired(info, declaration);

            return(declaration);
        }
コード例 #25
0
ファイル: PropertyGenerator.cs プロジェクト: tohfe/roslyn
        private static MemberDeclarationSyntax GeneratePropertyDeclaration(
            IPropertySymbol property, CodeGenerationDestination destination,
            Workspace workspace, CodeGenerationOptions options, ParseOptions parseOptions)
        {
            var initializerNode = CodeGenerationPropertyInfo.GetInitializer(property) as ExpressionSyntax;

            var initializer = initializerNode != null
                ? SyntaxFactory.EqualsValueClause(initializerNode)
                : default;

            var explicitInterfaceSpecifier = GenerateExplicitInterfaceSpecifier(property.ExplicitInterfaceImplementations);

            var accessorList = GenerateAccessorList(property, destination, workspace, options, parseOptions);

            var propertyDeclaration = SyntaxFactory.PropertyDeclaration(
                attributeLists: AttributeGenerator.GenerateAttributeLists(property.GetAttributes(), options),
                modifiers: GenerateModifiers(property, destination, options),
                type: GeneratePropertyType(property),
                explicitInterfaceSpecifier: explicitInterfaceSpecifier,
                identifier: property.Name.ToIdentifierToken(),
                accessorList: accessorList,
                expressionBody: default,
コード例 #26
0
        internal static ConstructorDeclarationSyntax GenerateConstructorDeclaration(
            IMethodSymbol constructor,
            CSharpCodeGenerationContextInfo info,
            CancellationToken cancellationToken)
        {
            var reusableSyntax = GetReuseableSyntaxNodeForSymbol <ConstructorDeclarationSyntax>(constructor, info);

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

            var hasNoBody = !info.Context.GenerateMethodBodies;

            var declaration = SyntaxFactory.ConstructorDeclaration(
                attributeLists: AttributeGenerator.GenerateAttributeLists(constructor.GetAttributes(), info),
                modifiers: GenerateModifiers(constructor, info),
                identifier: CodeGenerationConstructorInfo.GetTypeName(constructor).ToIdentifierToken(),
                parameterList: ParameterGenerator.GenerateParameterList(constructor.Parameters, isExplicit: false, info: info),
                initializer: GenerateConstructorInitializer(constructor),
                body: hasNoBody ? null : GenerateBlock(constructor),
                semicolonToken: hasNoBody ? SyntaxFactory.Token(SyntaxKind.SemicolonToken) : default);
コード例 #27
0
        public override TDeclarationNode AddAttributes <TDeclarationNode>(
            TDeclarationNode destination,
            IEnumerable <AttributeData> attributes,
            SyntaxToken?target,
            CodeGenerationOptions options,
            CancellationToken cancellationToken)
        {
            if (target.HasValue && !target.Value.IsValidAttributeTarget())
            {
                throw new ArgumentException("target");
            }

            var attributeSyntaxList = AttributeGenerator.GenerateAttributeLists(attributes.ToImmutableArray(), options, target).ToArray();

            switch (destination)
            {
            case MemberDeclarationSyntax member:
                // Handle all members including types.
                return(Cast <TDeclarationNode>(member.AddAttributeLists(attributeSyntaxList)));

            case AccessorDeclarationSyntax accessor:
                // Handle accessors
                return(Cast <TDeclarationNode>(accessor.AddAttributeLists(attributeSyntaxList)));

            case CompilationUnitSyntax compilationUnit:
                // Handle global attributes
                return(Cast <TDeclarationNode>(compilationUnit.AddAttributeLists(attributeSyntaxList)));

            case ParameterSyntax parameter:
                // Handle parameters
                return(Cast <TDeclarationNode>(parameter.AddAttributeLists(attributeSyntaxList)));

            case TypeParameterSyntax typeParameter:
                return(Cast <TDeclarationNode>(typeParameter.AddAttributeLists(attributeSyntaxList)));
            }

            return(destination);
        }
コード例 #28
0
        public override TDeclarationNode AddAttributes <TDeclarationNode>(
            TDeclarationNode destination,
            IEnumerable <AttributeData> attributes,
            SyntaxToken?target,
            CodeGenerationOptions options,
            CancellationToken cancellationToken)
        {
            if (target.HasValue && !target.Value.IsValidAttributeTarget())
            {
                throw new ArgumentException("target");
            }

            var attributeSyntaxList = AttributeGenerator.GenerateAttributeLists(attributes.ToImmutableArray(), options, target).ToArray();

            return(destination switch
            {
                MemberDeclarationSyntax member => Cast <TDeclarationNode>(member.AddAttributeLists(attributeSyntaxList)),
                AccessorDeclarationSyntax accessor => Cast <TDeclarationNode>(accessor.AddAttributeLists(attributeSyntaxList)),
                CompilationUnitSyntax compilationUnit => Cast <TDeclarationNode>(compilationUnit.AddAttributeLists(attributeSyntaxList)),
                ParameterSyntax parameter => Cast <TDeclarationNode>(parameter.AddAttributeLists(attributeSyntaxList)),
                TypeParameterSyntax typeParameter => Cast <TDeclarationNode>(typeParameter.AddAttributeLists(attributeSyntaxList)),
                _ => destination,
            });