|
| 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 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Diagnostics.CodeAnalysis; |
| 7 | +using CommunityToolkit.GeneratedDependencyProperty.Helpers; |
| 8 | +using Microsoft.CodeAnalysis; |
| 9 | + |
| 10 | +namespace CommunityToolkit.GeneratedDependencyProperty.Extensions; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Extension methods for <see cref="ITypeSymbol"/> types. |
| 14 | +/// </summary> |
| 15 | +internal static class ITypeSymbolExtensions |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Checks whether a given type has a default value of <see langword="null"/>. |
| 19 | + /// </summary> |
| 20 | + /// <param name="symbol">The input <see cref="ITypeSymbol"/> instance to check.</param> |
| 21 | + /// <returns>Whether the default value of <paramref name="symbol"/> is <see langword="null"/>.</returns> |
| 22 | + public static bool IsDefaultValueNull(this ITypeSymbol symbol) |
| 23 | + { |
| 24 | + return symbol is { IsValueType: false } or INamedTypeSymbol { IsGenericType: true, ConstructedFrom.SpecialType: SpecialType.System_Nullable_T }; |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Tries to get the default value of a given enum type. |
| 29 | + /// </summary> |
| 30 | + /// <param name="symbol">The input <see cref="ITypeSymbol"/> instance to check.</param> |
| 31 | + /// <param name="value">The resulting default value for <paramref name="symbol"/>, if it was an enum type.</param> |
| 32 | + /// <returns>Whether <paramref name="value"/> was retrieved successfully.</returns> |
| 33 | + public static bool TryGetDefaultValueForEnumType(this ITypeSymbol symbol, [NotNullWhen(true)] out object? value) |
| 34 | + { |
| 35 | + if (symbol.TypeKind is not TypeKind.Enum) |
| 36 | + { |
| 37 | + value = default; |
| 38 | + |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + // The default value of the enum is the value of its first constant field |
| 43 | + foreach (ISymbol memberSymbol in symbol.GetMembers()) |
| 44 | + { |
| 45 | + if (memberSymbol is IFieldSymbol { IsConst: true, ConstantValue: object defaultValue }) |
| 46 | + { |
| 47 | + value = defaultValue; |
| 48 | + |
| 49 | + return true; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + value = default; |
| 54 | + |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Checks whether or not a given type symbol has a specified fully qualified metadata name. |
| 60 | + /// </summary> |
| 61 | + /// <param name="symbol">The input <see cref="ITypeSymbol"/> instance to check.</param> |
| 62 | + /// <param name="name">The full name to check.</param> |
| 63 | + /// <returns>Whether <paramref name="symbol"/> has a full name equals to <paramref name="name"/>.</returns> |
| 64 | + public static bool HasFullyQualifiedMetadataName(this ITypeSymbol symbol, string name) |
| 65 | + { |
| 66 | + using ImmutableArrayBuilder<char> builder = new(); |
| 67 | + |
| 68 | + symbol.AppendFullyQualifiedMetadataName(in builder); |
| 69 | + |
| 70 | + return builder.WrittenSpan.SequenceEqual(name.AsSpan()); |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Checks whether or not a given <see cref="ITypeSymbol"/> inherits from a specified type. |
| 75 | + /// </summary> |
| 76 | + /// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param> |
| 77 | + /// <param name="baseTypeSymbol">The <see cref="ITypeSymbol"/> instance to check for inheritance from.</param> |
| 78 | + /// <returns>Whether or not <paramref name="typeSymbol"/> inherits from <paramref name="baseTypeSymbol"/>.</returns> |
| 79 | + public static bool InheritsFromType(this ITypeSymbol typeSymbol, ITypeSymbol baseTypeSymbol) |
| 80 | + { |
| 81 | + INamedTypeSymbol? currentBaseTypeSymbol = typeSymbol.BaseType; |
| 82 | + |
| 83 | + while (currentBaseTypeSymbol is not null) |
| 84 | + { |
| 85 | + if (SymbolEqualityComparer.Default.Equals(currentBaseTypeSymbol, baseTypeSymbol)) |
| 86 | + { |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + currentBaseTypeSymbol = currentBaseTypeSymbol.BaseType; |
| 91 | + } |
| 92 | + |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Checks whether or not a given <see cref="ITypeSymbol"/> inherits from a specified type. |
| 98 | + /// </summary> |
| 99 | + /// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param> |
| 100 | + /// <param name="name">The full name of the type to check for inheritance.</param> |
| 101 | + /// <returns>Whether or not <paramref name="typeSymbol"/> inherits from <paramref name="name"/>.</returns> |
| 102 | + public static bool InheritsFromFullyQualifiedMetadataName(this ITypeSymbol typeSymbol, string name) |
| 103 | + { |
| 104 | + INamedTypeSymbol? baseType = typeSymbol.BaseType; |
| 105 | + |
| 106 | + while (baseType is not null) |
| 107 | + { |
| 108 | + if (baseType.HasFullyQualifiedMetadataName(name)) |
| 109 | + { |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + baseType = baseType.BaseType; |
| 114 | + } |
| 115 | + |
| 116 | + return false; |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Gets the fully qualified metadata name for a given <see cref="ITypeSymbol"/> instance. |
| 121 | + /// </summary> |
| 122 | + /// <param name="symbol">The input <see cref="ITypeSymbol"/> instance.</param> |
| 123 | + /// <returns>The fully qualified metadata name for <paramref name="symbol"/>.</returns> |
| 124 | + public static string GetFullyQualifiedMetadataName(this ITypeSymbol symbol) |
| 125 | + { |
| 126 | + using ImmutableArrayBuilder<char> builder = new(); |
| 127 | + |
| 128 | + symbol.AppendFullyQualifiedMetadataName(in builder); |
| 129 | + |
| 130 | + return builder.ToString(); |
| 131 | + } |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// Appends the fully qualified metadata name for a given symbol to a target builder. |
| 135 | + /// </summary> |
| 136 | + /// <param name="symbol">The input <see cref="ITypeSymbol"/> instance.</param> |
| 137 | + /// <param name="builder">The target <see cref="ImmutableArrayBuilder{T}"/> instance.</param> |
| 138 | + public static void AppendFullyQualifiedMetadataName(this ITypeSymbol symbol, ref readonly ImmutableArrayBuilder<char> builder) |
| 139 | + { |
| 140 | + static void BuildFrom(ISymbol? symbol, ref readonly ImmutableArrayBuilder<char> builder) |
| 141 | + { |
| 142 | + switch (symbol) |
| 143 | + { |
| 144 | + // Namespaces that are nested also append a leading '.' |
| 145 | + case INamespaceSymbol { ContainingNamespace.IsGlobalNamespace: false }: |
| 146 | + BuildFrom(symbol.ContainingNamespace, in builder); |
| 147 | + builder.Add('.'); |
| 148 | + builder.AddRange(symbol.MetadataName.AsSpan()); |
| 149 | + break; |
| 150 | + |
| 151 | + // Other namespaces (i.e. the one right before global) skip the leading '.' |
| 152 | + case INamespaceSymbol { IsGlobalNamespace: false }: |
| 153 | + builder.AddRange(symbol.MetadataName.AsSpan()); |
| 154 | + break; |
| 155 | + |
| 156 | + // Types with no namespace just have their metadata name directly written |
| 157 | + case ITypeSymbol { ContainingSymbol: INamespaceSymbol { IsGlobalNamespace: true } }: |
| 158 | + builder.AddRange(symbol.MetadataName.AsSpan()); |
| 159 | + break; |
| 160 | + |
| 161 | + // Types with a containing non-global namespace also append a leading '.' |
| 162 | + case ITypeSymbol { ContainingSymbol: INamespaceSymbol namespaceSymbol }: |
| 163 | + BuildFrom(namespaceSymbol, in builder); |
| 164 | + builder.Add('.'); |
| 165 | + builder.AddRange(symbol.MetadataName.AsSpan()); |
| 166 | + break; |
| 167 | + |
| 168 | + // Nested types append a leading '+' |
| 169 | + case ITypeSymbol { ContainingSymbol: ITypeSymbol typeSymbol }: |
| 170 | + BuildFrom(typeSymbol, in builder); |
| 171 | + builder.Add('+'); |
| 172 | + builder.AddRange(symbol.MetadataName.AsSpan()); |
| 173 | + break; |
| 174 | + default: |
| 175 | + break; |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + BuildFrom(symbol, in builder); |
| 180 | + } |
| 181 | + |
| 182 | + /// <summary> |
| 183 | + /// Checks whether a given type is contained in a namespace with a specified name. |
| 184 | + /// </summary> |
| 185 | + /// <param name="symbol">The input <see cref="ITypeSymbol"/> instance.</param> |
| 186 | + /// <param name="namespaceName">The namespace to check.</param> |
| 187 | + /// <returns>Whether <paramref name="symbol"/> is contained within <paramref name="namespaceName"/>.</returns> |
| 188 | + public static bool IsContainedInNamespace(this ITypeSymbol symbol, string? namespaceName) |
| 189 | + { |
| 190 | + static void BuildFrom(INamespaceSymbol? symbol, ref readonly ImmutableArrayBuilder<char> builder) |
| 191 | + { |
| 192 | + switch (symbol) |
| 193 | + { |
| 194 | + // Namespaces that are nested also append a leading '.' |
| 195 | + case INamespaceSymbol { ContainingNamespace.IsGlobalNamespace: false }: |
| 196 | + BuildFrom(symbol.ContainingNamespace, in builder); |
| 197 | + builder.Add('.'); |
| 198 | + builder.AddRange(symbol.MetadataName.AsSpan()); |
| 199 | + break; |
| 200 | + |
| 201 | + // Other namespaces (i.e. the one right before global) skip the leading '.' |
| 202 | + case INamespaceSymbol { IsGlobalNamespace: false }: |
| 203 | + builder.AddRange(symbol.MetadataName.AsSpan()); |
| 204 | + break; |
| 205 | + default: |
| 206 | + break; |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + // Special case for no containing namespace |
| 211 | + if (symbol.ContainingNamespace is not { } containingNamespace) |
| 212 | + { |
| 213 | + return namespaceName is null; |
| 214 | + } |
| 215 | + |
| 216 | + // Special case if the type is directly in the global namespace |
| 217 | + if (containingNamespace.IsGlobalNamespace) |
| 218 | + { |
| 219 | + return containingNamespace.MetadataName == namespaceName; |
| 220 | + } |
| 221 | + |
| 222 | + using ImmutableArrayBuilder<char> builder = new(); |
| 223 | + |
| 224 | + BuildFrom(containingNamespace, in builder); |
| 225 | + |
| 226 | + return builder.WrittenSpan.SequenceEqual(namespaceName.AsSpan()); |
| 227 | + } |
| 228 | +} |
0 commit comments