Example #1
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()));
        }
Example #2
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);
        }
Example #3
0
        private static MemberDeclarationSyntax GenerateEventDeclarationWorker(
            IEventSymbol @event, CodeGenerationDestination destination, CodeGenerationOptions options)
        {
            var explicitInterfaceSpecifier = GenerateExplicitInterfaceSpecifier(@event.ExplicitInterfaceImplementations);

            return(AddFormatterAndCodeGeneratorAnnotationsTo(SyntaxFactory.EventDeclaration(
                                                                 attributeLists: AttributeGenerator.GenerateAttributeLists(@event.GetAttributes(), options),
                                                                 modifiers: GenerateModifiers(@event, destination, options),
                                                                 type: @event.Type.GenerateTypeSyntax(),
                                                                 explicitInterfaceSpecifier: explicitInterfaceSpecifier,
                                                                 identifier: @event.Name.ToIdentifierToken(),
        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()));
        }
Example #5
0
        private static MemberDeclarationSyntax GenerateIndexerDeclaration(
            IPropertySymbol property,
            CodeGenerationDestination destination,
            Workspace workspace,
            CodeGenerationOptions options,
            ParseOptions parseOptions)
        {
            var explicitInterfaceSpecifier = GenerateExplicitInterfaceSpecifier(property.ExplicitInterfaceImplementations);

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

            declaration = UseExpressionBodyIfDesired(workspace, declaration, parseOptions);

            return(AddFormatterAndCodeGeneratorAnnotationsTo(
                       AddAnnotationsTo(property, declaration)));
        }
Example #6
0
        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: property.GenerateTypeSyntax(),
                explicitInterfaceSpecifier: explicitInterfaceSpecifier,
                identifier: property.Name.ToIdentifierToken(),
                accessorList: accessorList,
                expressionBody: default,
Example #7
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);
        }
Example #8
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),
                parameterList: ParameterGenerator.GenerateParameterList(constructor.Parameters, isExplicit: false, options: options),
                initializer: GenerateConstructorInitializer(constructor),
                body: hasNoBody ? null : GenerateBlock(constructor),
                expressionBody: null,
                eosToken: hasNoBody ? SyntaxFactory.Token(SyntaxKind.SemicolonToken) : default);
Example #9
0
        public static FieldDeclarationSyntax GenerateFieldDeclaration(
            IFieldSymbol field, CodeGenerationDestination destination, CodeGenerationOptions options)
        {
            var reusableSyntax = GetReuseableSyntaxNodeForSymbol <VariableDeclarationSyntax>(field, options);

            if (reusableSyntax != null)
            {
                // TODO
            }

            var initializerNode = CodeGenerationFieldInfo.GetInitializer(field) as ExpressionSyntax;

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

            var fieldDeclaration = SyntaxFactory.FieldDeclaration(
                AttributeGenerator.GenerateAttributeLists(field.GetAttributes(), options),
                GenerateModifiers(field, options),
                SyntaxFactory.VariableDeclaration(SyntaxFactory.Token(SyntaxKind.VarKeyword), field.Name.ToIdentifierToken(), field.Type.GenerateTypeSyntax(), initializer));

            return(AddFormatterAndCodeGeneratorAnnotationsTo(
                       ConditionallyAddDocumentationCommentTo(fieldDeclaration, field, options)));
        }
Example #10
0
 private static SyntaxList <AttributeSyntax> GenerateAttributeDeclarations(
     INamedTypeSymbol namedType, CodeGenerationOptions options)
 {
     return(AttributeGenerator.GenerateAttributeLists(namedType.GetAttributes(), options));
 }