|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using Microsoft.CodeAnalysis; |
| 9 | + |
| 10 | +namespace Microsoft.Azure.Functions.Worker.Sdk.Generators |
| 11 | +{ |
| 12 | + internal static class ISymbolExtensions |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Walks the symbol tree to generate the fully qualified name of a type symbol. |
| 16 | + /// Ex input: A Symbol for "Task" token |
| 17 | + /// Output: "System.Threading.Tasks.Task" |
| 18 | + /// </summary> |
| 19 | + internal static string GetFullName(this ITypeSymbol typeSymbol) |
| 20 | + { |
| 21 | + var symbol = typeSymbol as ISymbol; |
| 22 | + |
| 23 | + if (symbol == null || IsRootNamespace(symbol)) |
| 24 | + { |
| 25 | + return string.Empty; |
| 26 | + } |
| 27 | + |
| 28 | + var sb = new StringBuilder(symbol.MetadataName); |
| 29 | + |
| 30 | + if (symbol is IArrayTypeSymbol arraySymbol) // arrays need to be handled differently b/c the properties used to get the full name for other symbols are null for IArrayTypeSymbols |
| 31 | + { |
| 32 | + sb.Append(arraySymbol.ElementType.GetFullName()); // ex: for string[], the ElementType is System.String and that is the full name returned at this step. |
| 33 | + sb.Append("[]"); // System.Byte[], System.String[] are the full names for array types of element type Byte, String and we auto-add the brackets here. |
| 34 | + } |
| 35 | + else |
| 36 | + { |
| 37 | + symbol = symbol.ContainingSymbol; |
| 38 | + |
| 39 | + while (!IsRootNamespace(symbol)) |
| 40 | + { |
| 41 | + sb.Insert(0, '.'); |
| 42 | + sb.Insert(0, symbol.MetadataName); |
| 43 | + symbol = symbol.ContainingSymbol; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return sb.ToString(); |
| 48 | + } |
| 49 | + |
| 50 | + private static bool IsRootNamespace(ISymbol symbol) |
| 51 | + { |
| 52 | + if (symbol is INamespaceSymbol namespaceSymbol) |
| 53 | + { |
| 54 | + return namespaceSymbol.IsGlobalNamespace; |
| 55 | + } |
| 56 | + |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + internal static bool IsOrDerivedFrom(this ITypeSymbol symbol, ITypeSymbol? other) |
| 61 | + { |
| 62 | + if (other is null) |
| 63 | + { |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + var current = symbol; |
| 68 | + |
| 69 | + while (current != null) |
| 70 | + { |
| 71 | + if(SymbolEqualityComparer.Default.Equals(current, other) || SymbolEqualityComparer.Default.Equals(current.OriginalDefinition, other)) |
| 72 | + { |
| 73 | + return true; |
| 74 | + } |
| 75 | + |
| 76 | + current = current.BaseType; |
| 77 | + } |
| 78 | + |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + internal static bool IsOrImplements(this ITypeSymbol symbol, ITypeSymbol? other) |
| 83 | + { |
| 84 | + if (other is null) |
| 85 | + { |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + var current = symbol; |
| 90 | + |
| 91 | + while (current != null) |
| 92 | + { |
| 93 | + foreach (var member in current.Interfaces) |
| 94 | + { |
| 95 | + if (member.IsOrDerivedFrom(other)) |
| 96 | + { |
| 97 | + return true; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + current = current.BaseType; |
| 102 | + } |
| 103 | + |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + internal static bool IsOrImplementsOrDerivesFrom(this ITypeSymbol symbol, ITypeSymbol? other) |
| 108 | + { |
| 109 | + return symbol.IsOrImplements(other) || symbol.IsOrDerivedFrom(other); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments