Esempio n. 1
0
        private static string GetLocationString(
            CSharpCompilation compilation,
            Symbol unwrappedSymbol
            )
        {
            Debug.Assert((object)unwrappedSymbol == UnwrapSymbol(unwrappedSymbol));

            ImmutableArray <SyntaxReference> syntaxReferences =
                unwrappedSymbol.DeclaringSyntaxReferences;

            if (syntaxReferences.Length > 0)
            {
                var    tree = syntaxReferences[0].SyntaxTree;
                var    span = syntaxReferences[0].Span;
                string path = tree.GetDisplayPath(
                    span,
                    (compilation != null) ? compilation.Options.SourceReferenceResolver : null
                    );
                if (!string.IsNullOrEmpty(path))
                {
                    return($"{path}({tree.GetDisplayLineNumber(span)})");
                }
            }

            AssemblySymbol containingAssembly = unwrappedSymbol.ContainingAssembly;

            if ((object)containingAssembly != null)
            {
                if (compilation != null)
                {
                    PortableExecutableReference metadataReference =
                        compilation.GetMetadataReference(containingAssembly)
                        as PortableExecutableReference;
                    if (metadataReference != null)
                    {
                        string path = metadataReference.FilePath;
                        if (!string.IsNullOrEmpty(path))
                        {
                            return(path);
                        }
                    }
                }

                return(containingAssembly.Identity.ToString());
            }

            Debug.Assert(
                unwrappedSymbol.Kind == SymbolKind.DynamicType ||
                unwrappedSymbol.Kind == SymbolKind.ErrorType ||
                unwrappedSymbol.Kind == SymbolKind.FunctionPointerType
                );
            return(null);
        }
Esempio n. 2
0
        private string GuessExternAlias(NamespaceSymbol @namespace, HashSet <string> validAliases)
        {
            AssemblySymbol containingAssembly = @namespace.ContainingAssembly;

            if ((object)containingAssembly != null && containingAssembly != _compilation.Assembly)
            {
                MetadataReference reference = _compilation.GetMetadataReference(containingAssembly);
                if (reference != null)
                {
                    ImmutableArray <string> aliases = reference.Properties.Aliases;
                    if (!aliases.IsDefaultOrEmpty)
                    {
                        if (aliases.Contains(MetadataReferenceProperties.GlobalAlias))
                        {
                            // If the namespace can be referenced without alias qualification, don't use any.
                            return(null);
                        }

                        foreach (string alias in aliases)
                        {
                            if (validAliases.Contains(alias))
                            {
                                // CONSIDER: Dev12 uses the one that appeared in source, whereas we use
                                // the first one that COULD have appeared in source.  (DevDiv #913022)
                                // NOTE: The reason we're not just using the alias from the syntax is that
                                // it is non-trivial to locate.  In particular, since "." may be used in
                                // place of "::", determining whether the first identifier in the name is
                                // the alias requires binding.  For example, "using A.B;" could refer to
                                // either "A::B" or "global::A.B".
                                return(alias);
                            }
                        }

                        Debug.Assert(false, $"None of the aliases of {@namespace} is valid in this scope");
                    }
                }
            }

            return(null);
        }