private ISymbol GenerateMember(
                ISymbol member,
                bool addUnsafe,
                CancellationToken cancellationToken)
            {
                var modifiers     = DeclarationModifiers.None.WithIsOverride(true).WithIsUnsafe(addUnsafe);
                var accessibility = member.ComputeResultantAccessibility(_state.ClassType);

                if (member.Kind == SymbolKind.Method)
                {
                    return(GenerateMethod((IMethodSymbol)member, modifiers, accessibility, cancellationToken));
                }
                else if (member.Kind == SymbolKind.Property)
                {
                    return(GenerateProperty((IPropertySymbol)member, modifiers, accessibility, cancellationToken));
                }
                else if (member.Kind == SymbolKind.Event)
                {
                    var @event = (IEventSymbol)member;
                    return(CodeGenerationSymbolFactory.CreateEventSymbol(
                               @event,
                               accessibility: accessibility,
                               modifiers: modifiers));
                }

                return(null);
            }
Exemple #2
0
        private static ISymbol MakeAbstractVersion(ISymbol member)
        {
            if (member.IsAbstract)
            {
                return(member);
            }

            var modifier = DeclarationModifiers.From(member).WithIsAbstract(true);

            if (member is IMethodSymbol methodSymbol)
            {
                return(CodeGenerationSymbolFactory.CreateMethodSymbol(methodSymbol, modifiers: modifier));
            }
            else if (member is IPropertySymbol propertySymbol)
            {
                return(CodeGenerationSymbolFactory.CreatePropertySymbol(propertySymbol, modifiers: modifier, getMethod: propertySymbol.GetMethod, setMethod: propertySymbol.SetMethod));
            }
            else if (member is IEventSymbol eventSymbol)
            {
                return(CodeGenerationSymbolFactory.CreateEventSymbol(eventSymbol, modifiers: modifier));
            }
            else
            {
                throw ExceptionUtilities.UnexpectedValue(member);
            }
        }
        internal static async Task TestAddEventAsync(
            string initial,
            string expected,
            string name = "E",
            IList <AttributeData> attributes = null,
            Accessibility accessibility      = Accessibility.Public,
            DeclarationModifiers modifiers   = default(DeclarationModifiers),
            IList <Func <SemanticModel, IParameterSymbol> > parameters = null,
            Type type = null,
            IEventSymbol explicitInterfaceSymbol = null,
            IMethodSymbol addMethod    = null,
            IMethodSymbol removeMethod = null,
            IMethodSymbol raiseMethod  = null,
            CodeGenerationOptions codeGenerationOptions = default(CodeGenerationOptions),
            bool compareTokens = true)
        {
            using (var context = await TestContext.CreateAsync(initial, expected, compareTokens))
            {
                type = type ?? typeof(Action);

                var parameterSymbols = GetParameterSymbols(parameters, context);
                var typeSymbol       = GetTypeSymbol(type)(context.SemanticModel);
                var @event           = CodeGenerationSymbolFactory.CreateEventSymbol(
                    attributes,
                    accessibility,
                    modifiers,
                    typeSymbol,
                    explicitInterfaceSymbol,
                    name,
                    addMethod,
                    removeMethod,
                    raiseMethod);
                context.Result = await context.Service.AddEventAsync(context.Solution, (INamedTypeSymbol)context.GetDestination(), @event, codeGenerationOptions);
            }
        }
Exemple #4
0
        internal static async Task TestAddEventAsync(
            string initial,
            string expected,
            string name = "E",
            ImmutableArray <AttributeData> attributes = default(ImmutableArray <AttributeData>),
            Accessibility accessibility            = Accessibility.Public,
            Editing.DeclarationModifiers modifiers = default(Editing.DeclarationModifiers),
            ImmutableArray <Func <SemanticModel, IParameterSymbol> > parameters = default(ImmutableArray <Func <SemanticModel, IParameterSymbol> >),
            Type type = null,
            Func <SemanticModel, ImmutableArray <IEventSymbol> > getExplicitInterfaceImplementations = null,
            IMethodSymbol addMethod    = null,
            IMethodSymbol removeMethod = null,
            IMethodSymbol raiseMethod  = null,
            CodeGenerationOptions codeGenerationOptions = default(CodeGenerationOptions),
            bool ignoreTrivia = true)
        {
            using (var context = await TestContext.CreateAsync(initial, expected, ignoreTrivia))
            {
                type = type ?? typeof(Action);

                var parameterSymbols = GetParameterSymbols(parameters, context);
                var typeSymbol       = GetTypeSymbol(type)(context.SemanticModel);
                var @event           = CodeGenerationSymbolFactory.CreateEventSymbol(
                    attributes,
                    accessibility,
                    modifiers,
                    typeSymbol,
                    getExplicitInterfaceImplementations?.Invoke(context.SemanticModel) ?? default,
                    name,
                    addMethod,
                    removeMethod,
                    raiseMethod);
                context.Result = await context.Service.AddEventAsync(context.Solution, (INamedTypeSymbol)context.GetDestination(), @event, codeGenerationOptions);
            }
        }
Exemple #5
0
        private IList <ISymbol> CreateInterfaceMembers(IEnumerable <ISymbol> includedMembers)
        {
            var interfaceMembers = new List <ISymbol>();

            foreach (var member in includedMembers)
            {
                switch (member.Kind)
                {
                case SymbolKind.Event:
                    var @event = member as IEventSymbol;
                    interfaceMembers.Add(CodeGenerationSymbolFactory.CreateEventSymbol(
                                             attributes: SpecializedCollections.EmptyList <AttributeData>(),
                                             accessibility: Accessibility.Public,
                                             modifiers: new DeclarationModifiers(isAbstract: true),
                                             type: @event.Type,
                                             explicitInterfaceSymbol: null,
                                             name: @event.Name));
                    break;

                case SymbolKind.Method:
                    var method = member as IMethodSymbol;
                    interfaceMembers.Add(CodeGenerationSymbolFactory.CreateMethodSymbol(
                                             attributes: SpecializedCollections.EmptyList <AttributeData>(),
                                             accessibility: Accessibility.Public,
                                             modifiers: new DeclarationModifiers(isAbstract: true, isUnsafe: method.IsUnsafe()),
                                             returnType: method.ReturnType,
                                             returnsByRef: method.ReturnsByRef,
                                             explicitInterfaceSymbol: null,
                                             name: method.Name,
                                             typeParameters: method.TypeParameters,
                                             parameters: method.Parameters));
                    break;

                case SymbolKind.Property:
                    var property = member as IPropertySymbol;
                    interfaceMembers.Add(CodeGenerationSymbolFactory.CreatePropertySymbol(
                                             attributes: SpecializedCollections.EmptyList <AttributeData>(),
                                             accessibility: Accessibility.Public,
                                             modifiers: new DeclarationModifiers(isAbstract: true, isUnsafe: property.IsUnsafe()),
                                             type: property.Type,
                                             returnsByRef: property.ReturnsByRef,
                                             explicitInterfaceSymbol: null,
                                             name: property.Name,
                                             parameters: property.Parameters,
                                             getMethod: property.GetMethod == null ? null : (property.GetMethod.DeclaredAccessibility == Accessibility.Public ? property.GetMethod : null),
                                             setMethod: property.SetMethod == null ? null : (property.SetMethod.DeclaredAccessibility == Accessibility.Public ? property.SetMethod : null),
                                             isIndexer: property.IsIndexer));
                    break;

                default:
                    Debug.Assert(false, string.Format(FeaturesResources.Unexpected_interface_member_kind_colon_0, member.Kind.ToString()));
                    break;
                }
            }

            return(interfaceMembers);
        }
Exemple #6
0
        internal static IEventSymbol GeneratePropertyChangedEvent(Compilation compilation)
        {
            var propertyChangedEventHandlerType = compilation.GetTypeByMetadataName("System.ComponentModel.PropertyChangedEventHandler");

            return(CodeGenerationSymbolFactory.CreateEventSymbol(
                       attributes: null,
                       accessibility: Microsoft.CodeAnalysis.Accessibility.Public,
                       modifiers: new SymbolModifiers(),
                       type: propertyChangedEventHandlerType,
                       explicitInterfaceSymbol: null,
                       name: "PropertyChanged"));
        }
Exemple #7
0
 public static IEventSymbol OverrideEvent(
     this SyntaxGenerator codeFactory,
     IEventSymbol overriddenEvent,
     DeclarationModifiers modifiers,
     INamedTypeSymbol newContainingType)
 {
     return(CodeGenerationSymbolFactory.CreateEventSymbol(
                overriddenEvent,
                attributes: default(ImmutableArray <AttributeData>),
                accessibility: overriddenEvent.ComputeResultantAccessibility(newContainingType),
                modifiers: modifiers,
                explicitInterfaceImplementations: default,
            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);
            }
 public static IEventSymbol OverrideEvent(
     this SyntaxGenerator codeFactory,
     IEventSymbol overriddenEvent,
     DeclarationModifiers modifiers,
     INamedTypeSymbol newContainingType)
 {
     return(CodeGenerationSymbolFactory.CreateEventSymbol(
                overriddenEvent,
                attributes: null,
                accessibility: overriddenEvent.ComputeResultantAccessibility(newContainingType),
                modifiers: modifiers,
                explicitInterfaceSymbol: null,
                name: overriddenEvent.Name));
 }
Exemple #10
0
        private static void ChangeEventToPublicAndNonStatic(
            ICodeGenerationService codeGenerationService,
            DocumentEditor editor,
            IEventSymbol eventSymbol,
            SyntaxNode eventDeclaration,
            DeclarationModifiers modifiers
            )
        {
            var declaration = editor.Generator.GetDeclaration(eventDeclaration);
            var isEventHasExplicitAddOrRemoveMethod =
                (eventSymbol.AddMethod != null && !eventSymbol.AddMethod.IsImplicitlyDeclared) ||
                (
                    eventSymbol.RemoveMethod != null &&
                    !eventSymbol.RemoveMethod.IsImplicitlyDeclared
                );

            // There are three situations here:
            // 1. Single Event.
            // 2. Several events exist in one declaration.
            // 3. Event has add or remove method(user declared).
            // For situation 1, declaration is EventFieldDeclaration, eventDeclaration is variableDeclaration.
            // For situation 2, declaration and eventDeclaration are both EventDeclaration, which are same.
            // For situation 3, it is same as situation 2, but has add or remove method.
            if (declaration.Equals(eventDeclaration) && !isEventHasExplicitAddOrRemoveMethod)
            {
                // Several events are declared in same line
                var publicAndNonStaticSymbol = CodeGenerationSymbolFactory.CreateEventSymbol(
                    eventSymbol,
                    accessibility: Accessibility.Public,
                    modifiers: modifiers
                    );
                var options = new CodeGenerationOptions(generateMethodBodies: false);
                var publicAndNonStaticSyntax = codeGenerationService.CreateEventDeclaration(
                    publicAndNonStaticSymbol,
                    destination: CodeGenerationDestination.ClassType,
                    options: options
                    );
                // Insert a new declaration and remove the original declaration
                editor.InsertAfter(declaration, publicAndNonStaticSyntax);
                editor.RemoveNode(eventDeclaration);
            }
            else
            {
                // Handle both single event and event has add or remove method
                editor.SetAccessibility(declaration, Accessibility.Public);
                editor.SetModifiers(declaration, modifiers);
            }
        }
Exemple #11
0
            private ISymbol GenerateMember(
                ISymbol member,
                bool addUnsafe,
                ImplementTypePropertyGenerationBehavior propertyGenerationBehavior,
                CancellationToken cancellationToken)
            {
                var modifiers     = new DeclarationModifiers(isOverride: true, isUnsafe: addUnsafe);
                var accessibility = member.ComputeResultantAccessibility(_state.ClassType);

                return(member switch
                {
                    IMethodSymbol method => GenerateMethod(method, modifiers, accessibility, cancellationToken),
                    IPropertySymbol property => GenerateProperty(property, modifiers, accessibility, propertyGenerationBehavior),
                    IEventSymbol @event => CodeGenerationSymbolFactory.CreateEventSymbol(
                        @event, accessibility: accessibility, modifiers: modifiers),
                    _ => null,
                });
        protected SyntaxNode CreateEventDeclaration(SyntaxNode containerNode, string name, EnvDTE.vsCMAccess access, ITypeSymbol type, bool createPropertyStyleEvent)
        {
            var destination = CodeModelService.GetDestination(containerNode);

            IMethodSymbol addMethod    = null;
            IMethodSymbol removeMethod = null;

            if (createPropertyStyleEvent)
            {
                addMethod = CodeGenerationSymbolFactory.CreateMethodSymbol(
                    attributes: default(ImmutableArray <AttributeData>),
                    accessibility: Accessibility.NotApplicable,
                    modifiers: new DeclarationModifiers(),
                    returnType: null,
                    returnsByRef: false,
                    explicitInterfaceSymbol: null,
                    name: "add_" + name,
                    typeParameters: default(ImmutableArray <ITypeParameterSymbol>),
                    parameters: default(ImmutableArray <IParameterSymbol>));

                removeMethod = CodeGenerationSymbolFactory.CreateMethodSymbol(
                    attributes: default(ImmutableArray <AttributeData>),
                    accessibility: Accessibility.NotApplicable,
                    modifiers: new DeclarationModifiers(),
                    returnType: null,
                    returnsByRef: false,
                    explicitInterfaceSymbol: null,
                    name: "remove_" + name,
                    typeParameters: default(ImmutableArray <ITypeParameterSymbol>),
                    parameters: default(ImmutableArray <IParameterSymbol>));
            }

            var newEventSymbol = CodeGenerationSymbolFactory.CreateEventSymbol(
                attributes: default(ImmutableArray <AttributeData>),
                accessibility: CodeModelService.GetAccessibility(access, SymbolKind.Event, destination),
                modifiers: new DeclarationModifiers(),
                type: type,
                explicitInterfaceSymbol: null,
                name: name,
                addMethod: addMethod,
                removeMethod: removeMethod);

            return(CodeGenerationService.CreateEventDeclaration(
                       newEventSymbol, destination,
                       options: GetCodeGenerationOptions(access, containerNode.SyntaxTree.Options)));
        }