Esempio n. 1
0
        internal static Task <SymbolAndProjectId> FindSourceDefinitionAsync(
            SymbolAndProjectId symbolAndProjectId, Solution solution, CancellationToken cancellationToken = default)
        {
            var symbol = symbolAndProjectId.Symbol;

            if (symbol != null)
            {
                symbol             = symbol.GetOriginalUnreducedDefinition();
                symbolAndProjectId = symbolAndProjectId.WithSymbol(symbol);
                switch (symbol.Kind)
                {
                case SymbolKind.Event:
                case SymbolKind.Field:
                case SymbolKind.Method:
                case SymbolKind.Local:
                case SymbolKind.NamedType:
                case SymbolKind.Parameter:
                case SymbolKind.Property:
                case SymbolKind.TypeParameter:
                case SymbolKind.Namespace:
                    return(FindSourceDefinitionWorkerAsync(symbolAndProjectId, solution, cancellationToken));
                }
            }

            return(SpecializedTasks.Default <SymbolAndProjectId>());
        }
Esempio n. 2
0
        internal static async Task <ImmutableArray <SymbolAndProjectId> > FindImplementedInterfaceMembersAsync(
            SymbolAndProjectId symbolAndProjectId, Solution solution, IImmutableSet <Project> projects = null, CancellationToken cancellationToken = default)
        {
            // Member can only implement interface members if it is an explicit member, or if it is
            // public and non static.
            var symbol = symbolAndProjectId.Symbol;

            if (symbol != null)
            {
                var explicitImplementations = symbol.ExplicitInterfaceImplementations();
                if (explicitImplementations.Length > 0)
                {
                    return(explicitImplementations.SelectAsArray(symbolAndProjectId.WithSymbol));
                }
                else if (
                    symbol.DeclaredAccessibility == Accessibility.Public && !symbol.IsStatic &&
                    (symbol.ContainingType.TypeKind == TypeKind.Class || symbol.ContainingType.TypeKind == TypeKind.Struct))
                {
                    // Interface implementation is a tricky thing.  A method may implement an interface
                    // method, even if its containing type doesn't state that it implements the
                    // interface.  For example:
                    //
                    //  interface IGoo { void Goo(); }
                    //
                    //  class Base { public void Goo(); }
                    //
                    //  class Derived : Base, IGoo { }
                    //
                    // In this case, Base.Goo *does* implement IGoo.Goo in the context of the type
                    // Derived.
                    var containingType = symbolAndProjectId.WithSymbol(
                        symbol.ContainingType.OriginalDefinition);
                    var derivedClasses = await SymbolFinder.FindDerivedClassesAsync(
                        containingType, solution, projects, cancellationToken).ConfigureAwait(false);

                    var allTypes = derivedClasses.Concat(containingType);

                    var builder = ArrayBuilder <SymbolAndProjectId> .GetInstance();

                    foreach (var type in allTypes.Convert <INamedTypeSymbol, ITypeSymbol>())
                    {
                        foreach (var interfaceType in GetAllInterfaces(type))
                        {
                            // We don't want to look inside this type if we can avoid it. So first
                            // make sure that the interface even contains a symbol with the same
                            // name as the symbol we're looking for.
                            var nameToLookFor = symbol.IsPropertyAccessor()
                                ? ((IMethodSymbol)symbol).AssociatedSymbol.Name
                                : symbol.Name;
                            if (interfaceType.Symbol.MemberNames.Contains(nameToLookFor))
                            {
                                foreach (var m in GetMembers(interfaceType, symbol.Name))
                                {
                                    var sourceMethod = await FindSourceDefinitionAsync(m, solution, cancellationToken).ConfigureAwait(false);

                                    var bestMethod = sourceMethod.Symbol != null ? sourceMethod : m;

                                    var implementations = type.FindImplementationsForInterfaceMember(
                                        bestMethod.Symbol, solution.Workspace, cancellationToken);
                                    foreach (var implementation in implementations)
                                    {
                                        if (implementation.Symbol != null &&
                                            SymbolEquivalenceComparer.Instance.Equals(implementation.Symbol.OriginalDefinition, symbol.OriginalDefinition))
                                        {
                                            builder.Add(bestMethod);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    var result = builder.Distinct(SymbolAndProjectIdComparer.SymbolEquivalenceInstance)
                                 .ToImmutableArray();
                    builder.Free();
                    return(result);
                }
            }

            return(ImmutableArray <SymbolAndProjectId> .Empty);
        }