private IMethodSymbol GenerateGetAccessor(
                Compilation compilation,
                IPropertySymbol property,
                Accessibility accessibility,
                bool generateAbstractly,
                bool useExplicitInterfaceSymbol,
                INamedTypeSymbol[] attributesToRemove,
                CancellationToken cancellationToken)
            {
                if (property.GetMethod == null)
                {
                    return(null);
                }

                var getMethod = property.GetMethod.RemoveInaccessibleAttributesAndAttributesOfTypes(
                    this.State.ClassOrStructType,
                    attributesToRemove);

                return(CodeGenerationSymbolFactory.CreateAccessorSymbol(
                           getMethod,
                           attributes: default(ImmutableArray <AttributeData>),
                           accessibility: accessibility,
                           explicitInterfaceSymbol: useExplicitInterfaceSymbol ? property.GetMethod : null,
                           statements: GetGetAccessorStatements(compilation, property, generateAbstractly, cancellationToken)));
            }
Ejemplo n.º 2
0
        private IPropertySymbol CreateProperty(
            IParameterSymbol parameter, ImmutableArray <NamingRule> rules, List <string> parameterNameParts)
        {
            foreach (var rule in rules)
            {
                if (rule.SymbolSpecification.AppliesTo(SymbolKind.Property, Accessibility.Public))
                {
                    var uniqueName = GenerateUniqueName(parameter, parameterNameParts, rule);

                    var getMethod = CodeGenerationSymbolFactory.CreateAccessorSymbol(
                        default(ImmutableArray <AttributeData>),
                        Accessibility.Public,
                        default(ImmutableArray <SyntaxNode>));

                    return(CodeGenerationSymbolFactory.CreatePropertySymbol(
                               default(ImmutableArray <AttributeData>),
                               Accessibility.Public,
                               new DeclarationModifiers(),
                               parameter.Type,
                               returnsByRef: false,
                               explicitInterfaceSymbol: null,
                               name: uniqueName,
                               parameters: default(ImmutableArray <IParameterSymbol>),
                               getMethod: getMethod,
                               setMethod: null));
                }
            }

            // We place a special rule in s_builtInRules that matches all properties.  So we should
            // always find a matching rule.
            throw ExceptionUtilities.Unreachable;
        }
            private IPropertySymbol GenerateProperty(
                IPropertySymbol property,
                DeclarationModifiers modifiers,
                Accessibility accessibility,
                CancellationToken cancellationToken)
            {
                var syntaxFactory = _document.Project.LanguageServices.GetService <SyntaxGenerator>();

                var throwingBody = syntaxFactory.CreateThrowNotImplementedStatementBlock(
                    _model.Compilation);

                var getMethod = ShouldGenerateAccessor(property.GetMethod)
                                        ? CodeGenerationSymbolFactory.CreateAccessorSymbol(
                    property.GetMethod,
                    attributes: null,
                    accessibility: property.GetMethod.ComputeResultantAccessibility(_state.ClassType),
                    statements: throwingBody)
                                        : null;

                var setMethod = ShouldGenerateAccessor(property.SetMethod)
                                        ? CodeGenerationSymbolFactory.CreateAccessorSymbol(
                    property.SetMethod,
                    attributes: null,
                    accessibility: property.SetMethod.ComputeResultantAccessibility(_state.ClassType),
                    statements: throwingBody)
                                        : null;

                return(CodeGenerationSymbolFactory.CreatePropertySymbol(
                           property,
                           accessibility: accessibility,
                           modifiers: modifiers,
                           getMethod: getMethod,
                           setMethod: setMethod));
            }
Ejemplo n.º 4
0
            internal IPropertySymbol GenerateProperty(
                SyntaxGenerator factory,
                bool isAbstract, bool includeSetter,
                CancellationToken cancellationToken)
            {
                var accessibility = DetermineAccessibility(isAbstract);
                var getMethod     = CodeGenerationSymbolFactory.CreateAccessorSymbol(
                    attributes: default(ImmutableArray <AttributeData>),
                    accessibility: accessibility,
                    statements: GenerateStatements(factory, isAbstract, cancellationToken));

                var setMethod = includeSetter ? getMethod : null;

                return(CodeGenerationSymbolFactory.CreatePropertySymbol(
                           attributes: default(ImmutableArray <AttributeData>),
                           accessibility: accessibility,
                           modifiers: new DeclarationModifiers(isStatic: State.IsStatic, isAbstract: isAbstract),
                           type: DetermineReturnType(cancellationToken),
                           returnsByRef: DetermineReturnsByRef(cancellationToken),
                           explicitInterfaceSymbol: null,
                           name: this.State.IdentifierToken.ValueText,
                           parameters: DetermineParameters(cancellationToken),
                           getMethod: getMethod,
                           setMethod: setMethod));
            }
            private IMethodSymbol GenerateSetAccessor(
                Compilation compilation,
                IPropertySymbol property,
                Accessibility accessibility,
                bool generateAbstractly,
                bool useExplicitInterfaceSymbol,
                ImplementTypePropertyGenerationBehavior propertyGenerationBehavior,
                INamedTypeSymbol[] attributesToRemove,
                CancellationToken cancellationToken)
            {
                if (property.SetMethod == null)
                {
                    return(null);
                }

                if (property.GetMethod == null)
                {
                    // Can't have an auto-prop with just a setter.
                    propertyGenerationBehavior = ImplementTypePropertyGenerationBehavior.PreferThrowingProperties;
                }

                var setMethod = property.SetMethod.RemoveInaccessibleAttributesAndAttributesOfTypes(
                    this.State.ClassOrStructType,
                    attributesToRemove);

                return(CodeGenerationSymbolFactory.CreateAccessorSymbol(
                           setMethod,
                           attributes: default(ImmutableArray <AttributeData>),
                           accessibility: accessibility,
                           explicitInterfaceSymbol: useExplicitInterfaceSymbol ? property.SetMethod : null,
                           statements: GetSetAccessorStatements(
                               compilation, property, generateAbstractly, propertyGenerationBehavior, cancellationToken)));
            }
Ejemplo n.º 6
0
 private IMethodSymbol CreateAccessor(
     Accessibility accessibility, CancellationToken cancellationToken)
 {
     return(CodeGenerationSymbolFactory.CreateAccessorSymbol(
                attributes: default(ImmutableArray <AttributeData>),
                accessibility: accessibility,
                statements: GenerateStatements(cancellationToken)));
 }
 private IMethodSymbol CreateAccessor(
     Accessibility accessibility, CancellationToken cancellationToken)
 {
     return(CodeGenerationSymbolFactory.CreateAccessorSymbol(
                attributes: null,
                accessibility: accessibility,
                statements: GenerateStatements(cancellationToken)));
 }
Ejemplo n.º 8
0
            protected override async Task <Document> GetChangedDocumentAsync(CancellationToken cancellationToken)
            {
                var syntaxTree = await _document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);

                var generateUnsafe = _state.TypeMemberType.IsUnsafe() &&
                                     !_state.IsContainedInUnsafeType;

                if (_generateProperty)
                {
                    var getAccessor = CodeGenerationSymbolFactory.CreateAccessorSymbol(
                        attributes: null,
                        accessibility: DetermineMaximalAccessibility(_state),
                        statements: null);
                    var setAccessor = _isReadonly ? null : CodeGenerationSymbolFactory.CreateAccessorSymbol(
                        attributes: null,
                        accessibility: DetermineMinimalAccessibility(_state),
                        statements: null);

                    var result = await CodeGenerator.AddPropertyDeclarationAsync(
                        _document.Project.Solution,
                        _state.TypeToGenerateIn,
                        CodeGenerationSymbolFactory.CreatePropertySymbol(
                            attributes: null,
                            accessibility: DetermineMaximalAccessibility(_state),
                            modifiers: new DeclarationModifiers(isStatic: _state.IsStatic, isUnsafe: generateUnsafe),
                            type: _state.TypeMemberType,
                            explicitInterfaceSymbol: null,
                            name: _state.IdentifierToken.ValueText,
                            isIndexer: _state.IsIndexer,
                            parameters: _state.Parameters,
                            getMethod: getAccessor,
                            setMethod: setAccessor),
                        new CodeGenerationOptions(contextLocation : _state.IdentifierToken.GetLocation()),
                        cancellationToken : cancellationToken)
                                 .ConfigureAwait(false);

                    return(result);
                }
                else
                {
                    var result = await CodeGenerator.AddFieldDeclarationAsync(
                        _document.Project.Solution,
                        _state.TypeToGenerateIn,
                        CodeGenerationSymbolFactory.CreateFieldSymbol(
                            attributes: null,
                            accessibility: DetermineMinimalAccessibility(_state),
                            modifiers: _isConstant ?
                            new DeclarationModifiers(isConst: true, isUnsafe: generateUnsafe) :
                            new DeclarationModifiers(isStatic: _state.IsStatic, isReadOnly: _isReadonly, isUnsafe: generateUnsafe),
                            type: _state.TypeMemberType,
                            name: _state.IdentifierToken.ValueText),
                        new CodeGenerationOptions(contextLocation : _state.IdentifierToken.GetLocation()),
                        cancellationToken : cancellationToken)
                                 .ConfigureAwait(false);

                    return(result);
                }
            }
        protected IMethodSymbol CreateGet(string originalFieldName, IFieldSymbol field, SyntaxGenerator factory)
        {
            var body = factory.ReturnStatement(
                factory.IdentifierName(originalFieldName));

            return(CodeGenerationSymbolFactory.CreateAccessorSymbol(SpecializedCollections.EmptyList <AttributeData>(),
                                                                    Accessibility.NotApplicable,
                                                                    new[] { body }.ToList()));
        }
            protected override async Task <Document> GetChangedDocumentAsync(CancellationToken cancellationToken)
            {
                var generateUnsafe = _state.TypeMemberType.IsUnsafe() &&
                                     !_state.IsContainedInUnsafeType;

                if (_generateProperty)
                {
                    var getAccessor = CodeGenerationSymbolFactory.CreateAccessorSymbol(
                        attributes: null,
                        accessibility: DetermineMaximalAccessibility(_state),
                        statements: null);
                    var setAccessor = _isReadonly ? null : CodeGenerationSymbolFactory.CreateAccessorSymbol(
                        attributes: null,
                        accessibility: DetermineMinimalAccessibility(_state),
                        statements: null);

                    var result = await ICSharpCode.NRefactory6.CSharp.CodeGenerator.AddPropertyDeclarationAsync(
                        _document.Project.Solution,
                        _state.TypeToGenerateIn,
                        CodeGenerationSymbolFactory.CreatePropertySymbol(
                            attributes: null,
                            accessibility: DetermineMaximalAccessibility(_state),
                            modifiers: DeclarationModifiers.None.WithIsStatic(_state.IsStatic).WithIsUnsafe(generateUnsafe),
                            type: _state.TypeMemberType,
                            explicitInterfaceSymbol: null,
                            name: _state.IdentifierToken.ValueText,
                            isIndexer: _state.IsIndexer,
                            parameters: _state.Parameters,
                            getMethod: getAccessor,
                            setMethod: setAccessor),
                        new CodeGenerationOptions(contextLocation : _state.IdentifierToken.GetLocation(), generateDefaultAccessibility : false),
                        cancellationToken : cancellationToken)
                                 .ConfigureAwait(false);

                    return(await AnnotateInsertionMode(_document.Project.Solution.GetDocument(result.Id), result));
                }
                else
                {
                    var result = await ICSharpCode.NRefactory6.CSharp.CodeGenerator.AddFieldDeclarationAsync(
                        _document.Project.Solution,
                        _state.TypeToGenerateIn,
                        CodeGenerationSymbolFactory.CreateFieldSymbol(
                            attributes: null,
                            accessibility: DetermineMinimalAccessibility(_state),
                            modifiers: _isConstant ?
                            DeclarationModifiers.None.WithIsConst(true).WithIsUnsafe(generateUnsafe) :
                            DeclarationModifiers.None.WithIsStatic(_state.IsStatic).WithIsReadOnly(_isReadonly).WithIsUnsafe(generateUnsafe),
                            type: _state.TypeMemberType,
                            name: _state.IdentifierToken.ValueText),
                        new CodeGenerationOptions(contextLocation : _state.IdentifierToken.GetLocation(), generateDefaultAccessibility : false),
                        cancellationToken : cancellationToken)
                                 .ConfigureAwait(false);

                    return(await AnnotateInsertionMode(_document.Project.Solution.GetDocument(result.Id), result));
                }
            }
Ejemplo n.º 11
0
            private ISymbol GenerateProperty(
                Compilation compilation,
                IPropertySymbol property,
                Accessibility accessibility,
                DeclarationModifiers modifiers,
                bool generateAbstractly,
                bool useExplicitInterfaceSymbol,
                string memberName,
                CancellationToken cancellationToken)
            {
                var factory = this.Document.GetLanguageService <SyntaxGenerator>();
                var comAliasNameAttribute = compilation.ComAliasNameAttributeType();

                var getAccessor = property.GetMethod == null
                    ? null
                    : CodeGenerationSymbolFactory.CreateAccessorSymbol(
                    property.GetMethod.RemoveInaccessibleAttributesAndAttributesOfType(
                        accessibleWithin: this.State.ClassOrStructType,
                        removeAttributeType: comAliasNameAttribute),
                    attributes: null,
                    accessibility: accessibility,
                    explicitInterfaceSymbol: useExplicitInterfaceSymbol ? property.GetMethod : null,
                    statements: GetGetAccessorStatements(compilation, property, generateAbstractly, cancellationToken));

                var setAccessor = property.SetMethod == null
                    ? null
                    : CodeGenerationSymbolFactory.CreateAccessorSymbol(
                    property.SetMethod.RemoveInaccessibleAttributesAndAttributesOfType(
                        accessibleWithin: this.State.ClassOrStructType,
                        removeAttributeType: comAliasNameAttribute),
                    attributes: null,
                    accessibility: accessibility,
                    explicitInterfaceSymbol: useExplicitInterfaceSymbol ? property.SetMethod : null,
                    statements: GetSetAccessorStatements(compilation, property, generateAbstractly, cancellationToken));

                var syntaxFacts    = Document.GetLanguageService <ISyntaxFactsService>();
                var parameterNames = NameGenerator.EnsureUniqueness(
                    property.Parameters.Select(p => p.Name).ToList(), isCaseSensitive: syntaxFacts.IsCaseSensitive);

                var updatedProperty = property.RenameParameters(parameterNames);

                updatedProperty = updatedProperty.RemoveAttributeFromParameters(comAliasNameAttribute);

                // TODO(cyrusn): Delegate through throughMember if it's non-null.
                return(CodeGenerationSymbolFactory.CreatePropertySymbol(
                           updatedProperty,
                           accessibility: accessibility,
                           modifiers: modifiers,
                           explicitInterfaceSymbol: useExplicitInterfaceSymbol ? property : null,
                           name: memberName,
                           getMethod: getAccessor,
                           setMethod: setAccessor));
            }
            private ISymbol GenerateMember(
                Compilation compilation,
                ISymbol member,
                string memberName,
                bool generateInvisibly,
                bool generateAbstractly,
                bool addNew,
                bool addUnsafe,
                CancellationToken cancellationToken)
            {
                var factory   = this.Document.GetLanguageService <SyntaxGenerator>();
                var modifiers = new DeclarationModifiers(isAbstract: generateAbstractly, isNew: addNew, isUnsafe: addUnsafe);

                var useExplicitInterfaceSymbol = generateInvisibly || !Service.CanImplementImplicitly;
                var accessibility = member.Name == memberName || generateAbstractly
                    ? Accessibility.Public
                    : Accessibility.Private;

                if (member.Kind == SymbolKind.Method)
                {
                    var method = (IMethodSymbol)member;

                    return(GenerateMethod(compilation, method, accessibility, modifiers, generateAbstractly, useExplicitInterfaceSymbol, memberName, cancellationToken));
                }
                else if (member.Kind == SymbolKind.Property)
                {
                    var property = (IPropertySymbol)member;

                    return(GenerateProperty(compilation, property, accessibility, modifiers, generateAbstractly, useExplicitInterfaceSymbol, memberName, cancellationToken));
                }
                else if (member.Kind == SymbolKind.Event)
                {
                    var @event = (IEventSymbol)member;

                    var accessor = CodeGenerationSymbolFactory.CreateAccessorSymbol(
                        attributes: null,
                        accessibility: Accessibility.NotApplicable,
                        statements: factory.CreateThrowNotImplementedStatementBlock(compilation));

                    return(CodeGenerationSymbolFactory.CreateEventSymbol(
                               @event,
                               accessibility: accessibility,
                               modifiers: modifiers,
                               explicitInterfaceSymbol: useExplicitInterfaceSymbol ? @event : null,
                               name: memberName,
                               addMethod: generateInvisibly ? accessor : null,
                               removeMethod: generateInvisibly ? accessor : null));
                }

                return(null);
            }
Ejemplo n.º 13
0
        protected IMethodSymbol CreateGet(string originalFieldName, IFieldSymbol field, SyntaxGenerator factory)
        {
            var value = !field.IsStatic
                ? factory.MemberAccessExpression(
                factory.ThisExpression(),
                factory.IdentifierName(originalFieldName))
                : factory.IdentifierName(originalFieldName);

            var body = factory.ReturnStatement(
                value.WithAdditionalAnnotations(Simplifier.Annotation));

            return(CodeGenerationSymbolFactory.CreateAccessorSymbol(SpecializedCollections.EmptyList <AttributeData>(),
                                                                    Accessibility.NotApplicable,
                                                                    new[] { body }.ToList()));
        }
Ejemplo n.º 14
0
        protected IMethodSymbol CreateGet(string originalFieldName, IFieldSymbol field, SyntaxGenerator factory)
        {
            var value = !field.IsStatic
                ? factory.MemberAccessExpression(
                factory.ThisExpression(),
                factory.IdentifierName(originalFieldName))
                : factory.IdentifierName(originalFieldName);

            var body = factory.ReturnStatement(
                value.WithAdditionalAnnotations(Simplifier.Annotation));

            return(CodeGenerationSymbolFactory.CreateAccessorSymbol(
                       ImmutableArray <AttributeData> .Empty,
                       Accessibility.NotApplicable,
                       ImmutableArray.Create(body)));
        }
            private IPropertySymbol GenerateProperty(
                IPropertySymbol property,
                DeclarationModifiers modifiers,
                Accessibility accessibility,
                ImplementTypePropertyGenerationBehavior propertyGenerationBehavior,
                CancellationToken cancellationToken)
            {
                if (property.GetMethod == null)
                {
                    // Can't generate an auto-prop for a setter-only property.
                    propertyGenerationBehavior = ImplementTypePropertyGenerationBehavior.PreferThrowingProperties;
                }

                var syntaxFactory = _document.GetLanguageService <SyntaxGenerator>();

                var accessorBody = propertyGenerationBehavior == ImplementTypePropertyGenerationBehavior.PreferAutoProperties
                    ? default
                    : syntaxFactory.CreateThrowNotImplementedStatementBlock(_model.Compilation);

                var getMethod = ShouldGenerateAccessor(property.GetMethod)
                    ? CodeGenerationSymbolFactory.CreateAccessorSymbol(
                    property.GetMethod,
                    attributes: default,
                    accessibility: property.GetMethod.ComputeResultantAccessibility(_state.ClassType),
                    statements: accessorBody)
                    : null;

                var setMethod = ShouldGenerateAccessor(property.SetMethod)
                    ? CodeGenerationSymbolFactory.CreateAccessorSymbol(
                    property.SetMethod,
                    attributes: default,
                    accessibility: property.SetMethod.ComputeResultantAccessibility(_state.ClassType),
                    statements: accessorBody)
                    : null;

                return(CodeGenerationSymbolFactory.CreatePropertySymbol(
                           property,
                           accessibility: accessibility,
                           modifiers: modifiers,
                           getMethod: getMethod,
                           setMethod: setMethod));
            }
        private IPropertySymbol CreateProperty(
            IParameterSymbol parameter, ImmutableArray <NamingRule> rules, List <string> parameterNameParts)
        {
            foreach (var rule in rules)
            {
                if (rule.SymbolSpecification.AppliesTo(SymbolKind.Property, Accessibility.Public))
                {
                    var uniqueName = GenerateUniqueName(parameter, parameterNameParts, rule);

                    var getMethod = CodeGenerationSymbolFactory.CreateAccessorSymbol(
                        default(ImmutableArray <AttributeData>),
                        Accessibility.Public,
                        default(ImmutableArray <SyntaxNode>));

                    return(CodeGenerationSymbolFactory.CreatePropertySymbol(
                               default(ImmutableArray <AttributeData>),
                               Accessibility.Public,
                               new DeclarationModifiers(),
                               parameter.Type,
                               returnsByRef: false,
                               explicitInterfaceImplementations: default,