|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Microsoft.CodeAnalysis; |
| 5 | + |
| 6 | +namespace System.Text.Json.Reflection |
| 7 | +{ |
| 8 | + internal static partial class RoslynExtensions |
| 9 | + { |
| 10 | + // Copied from: https://github.com/dotnet/roslyn/blob/main/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/CompilationExtensions.cs |
| 11 | + /// <summary> |
| 12 | + /// Gets a type by its metadata name to use for code analysis within a <see cref="Compilation"/>. This method |
| 13 | + /// attempts to find the "best" symbol to use for code analysis, which is the symbol matching the first of the |
| 14 | + /// following rules. |
| 15 | + /// |
| 16 | + /// <list type="number"> |
| 17 | + /// <item><description> |
| 18 | + /// If only one type with the given name is found within the compilation and its referenced assemblies, that |
| 19 | + /// type is returned regardless of accessibility. |
| 20 | + /// </description></item> |
| 21 | + /// <item><description> |
| 22 | + /// If the current <paramref name="compilation"/> defines the symbol, that symbol is returned. |
| 23 | + /// </description></item> |
| 24 | + /// <item><description> |
| 25 | + /// If exactly one referenced assembly defines the symbol in a manner that makes it visible to the current |
| 26 | + /// <paramref name="compilation"/>, that symbol is returned. |
| 27 | + /// </description></item> |
| 28 | + /// <item><description> |
| 29 | + /// Otherwise, this method returns <see langword="null"/>. |
| 30 | + /// </description></item> |
| 31 | + /// </list> |
| 32 | + /// </summary> |
| 33 | + /// <param name="compilation">The <see cref="Compilation"/> to consider for analysis.</param> |
| 34 | + /// <param name="fullyQualifiedMetadataName">The fully-qualified metadata type name to find.</param> |
| 35 | + /// <returns>The symbol to use for code analysis; otherwise, <see langword="null"/>.</returns> |
| 36 | + public static INamedTypeSymbol? GetBestTypeByMetadataName(this Compilation compilation, string fullyQualifiedMetadataName) |
| 37 | + { |
| 38 | + // Try to get the unique type with this name, ignoring accessibility |
| 39 | + var type = compilation.GetTypeByMetadataName(fullyQualifiedMetadataName); |
| 40 | + |
| 41 | + // Otherwise, try to get the unique type with this name originally defined in 'compilation' |
| 42 | + type ??= compilation.Assembly.GetTypeByMetadataName(fullyQualifiedMetadataName); |
| 43 | + |
| 44 | + // Otherwise, try to get the unique accessible type with this name from a reference |
| 45 | + if (type is null) |
| 46 | + { |
| 47 | + foreach (var module in compilation.Assembly.Modules) |
| 48 | + { |
| 49 | + foreach (var referencedAssembly in module.ReferencedAssemblySymbols) |
| 50 | + { |
| 51 | + var currentType = referencedAssembly.GetTypeByMetadataName(fullyQualifiedMetadataName); |
| 52 | + if (currentType is null) |
| 53 | + continue; |
| 54 | + |
| 55 | + switch (currentType.GetResultantVisibility()) |
| 56 | + { |
| 57 | + case SymbolVisibility.Public: |
| 58 | + case SymbolVisibility.Internal when referencedAssembly.GivesAccessTo(compilation.Assembly): |
| 59 | + break; |
| 60 | + |
| 61 | + default: |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + if (type is object) |
| 66 | + { |
| 67 | + // Multiple visible types with the same metadata name are present |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + type = currentType; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return type; |
| 77 | + } |
| 78 | + |
| 79 | + // copied from https://github.com/dotnet/roslyn/blob/main/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/ISymbolExtensions.cs |
| 80 | + private static SymbolVisibility GetResultantVisibility(this ISymbol symbol) |
| 81 | + { |
| 82 | + // Start by assuming it's visible. |
| 83 | + SymbolVisibility visibility = SymbolVisibility.Public; |
| 84 | + |
| 85 | + switch (symbol.Kind) |
| 86 | + { |
| 87 | + case SymbolKind.Alias: |
| 88 | + // Aliases are uber private. They're only visible in the same file that they |
| 89 | + // were declared in. |
| 90 | + return SymbolVisibility.Private; |
| 91 | + |
| 92 | + case SymbolKind.Parameter: |
| 93 | + // Parameters are only as visible as their containing symbol |
| 94 | + return GetResultantVisibility(symbol.ContainingSymbol); |
| 95 | + |
| 96 | + case SymbolKind.TypeParameter: |
| 97 | + // Type Parameters are private. |
| 98 | + return SymbolVisibility.Private; |
| 99 | + } |
| 100 | + |
| 101 | + while (symbol != null && symbol.Kind != SymbolKind.Namespace) |
| 102 | + { |
| 103 | + switch (symbol.DeclaredAccessibility) |
| 104 | + { |
| 105 | + // If we see anything private, then the symbol is private. |
| 106 | + case Accessibility.NotApplicable: |
| 107 | + case Accessibility.Private: |
| 108 | + return SymbolVisibility.Private; |
| 109 | + |
| 110 | + // If we see anything internal, then knock it down from public to |
| 111 | + // internal. |
| 112 | + case Accessibility.Internal: |
| 113 | + case Accessibility.ProtectedAndInternal: |
| 114 | + visibility = SymbolVisibility.Internal; |
| 115 | + break; |
| 116 | + |
| 117 | + // For anything else (Public, Protected, ProtectedOrInternal), the |
| 118 | + // symbol stays at the level we've gotten so far. |
| 119 | + } |
| 120 | + |
| 121 | + symbol = symbol.ContainingSymbol; |
| 122 | + } |
| 123 | + |
| 124 | + return visibility; |
| 125 | + } |
| 126 | + |
| 127 | + // Copied from: https://github.com/dotnet/roslyn/blob/main/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Utilities/SymbolVisibility.cs |
| 128 | +#pragma warning disable CA1027 // Mark enums with FlagsAttribute |
| 129 | + private enum SymbolVisibility |
| 130 | +#pragma warning restore CA1027 // Mark enums with FlagsAttribute |
| 131 | + { |
| 132 | + Public = 0, |
| 133 | + Internal = 1, |
| 134 | + Private = 2, |
| 135 | + Friend = Internal, |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments