| | | 1 | | using Microsoft.CodeAnalysis; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr.Generators; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Helper for discovering open generic decorator types from Roslyn symbols. |
| | | 7 | | /// </summary> |
| | | 8 | | internal static class OpenDecoratorDiscoveryHelper |
| | | 9 | | { |
| | | 10 | | private const string OpenDecoratorForAttributeName = "OpenDecoratorForAttribute"; |
| | | 11 | | private const string OpenDecoratorForAttributeFullName = "NexusLabs.Needlr.Generators.OpenDecoratorForAttribute"; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Result of open generic decorator discovery. |
| | | 15 | | /// </summary> |
| | | 16 | | public readonly struct OpenDecoratorInfo |
| | | 17 | | { |
| | | 18 | | public OpenDecoratorInfo( |
| | | 19 | | INamedTypeSymbol decoratorType, |
| | | 20 | | INamedTypeSymbol openGenericInterface, |
| | | 21 | | int order) |
| | | 22 | | { |
| | 7 | 23 | | DecoratorType = decoratorType; |
| | 7 | 24 | | OpenGenericInterface = openGenericInterface; |
| | 7 | 25 | | Order = order; |
| | 7 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary>The open generic decorator type symbol (e.g., LoggingDecorator{T}).</summary> |
| | 7 | 29 | | public INamedTypeSymbol DecoratorType { get; } |
| | | 30 | | |
| | | 31 | | /// <summary>The open generic interface being decorated (e.g., IHandler{T}).</summary> |
| | 7 | 32 | | public INamedTypeSymbol OpenGenericInterface { get; } |
| | | 33 | | |
| | | 34 | | /// <summary>Order in which decorator wraps (lower = outer).</summary> |
| | 7 | 35 | | public int Order { get; } |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets all OpenDecoratorFor attributes applied to a type. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 42 | | /// <returns>A list of open decorator info for each OpenDecoratorFor attribute found.</returns> |
| | | 43 | | public static IReadOnlyList<OpenDecoratorInfo> GetOpenDecoratorForAttributes(INamedTypeSymbol typeSymbol) |
| | | 44 | | { |
| | 1420254 | 45 | | var result = new List<OpenDecoratorInfo>(); |
| | | 46 | | |
| | 6828174 | 47 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 48 | | { |
| | 1993833 | 49 | | var attrClass = attribute.AttributeClass; |
| | 1993833 | 50 | | if (attrClass is null) |
| | | 51 | | continue; |
| | | 52 | | |
| | | 53 | | // Check if this is OpenDecoratorForAttribute |
| | 1993833 | 54 | | if (attrClass.Name != OpenDecoratorForAttributeName) |
| | | 55 | | continue; |
| | | 56 | | |
| | 7 | 57 | | var fullName = attrClass.ToDisplayString(); |
| | 7 | 58 | | if (fullName != OpenDecoratorForAttributeFullName) |
| | | 59 | | continue; |
| | | 60 | | |
| | | 61 | | // Get the type argument from the constructor (first positional argument) |
| | 7 | 62 | | if (attribute.ConstructorArguments.Length < 1) |
| | | 63 | | continue; |
| | | 64 | | |
| | 7 | 65 | | var typeArg = attribute.ConstructorArguments[0]; |
| | 7 | 66 | | if (typeArg.Value is not INamedTypeSymbol openGenericInterface) |
| | | 67 | | continue; |
| | | 68 | | |
| | | 69 | | // Validate it's an open generic interface |
| | 7 | 70 | | if (!openGenericInterface.IsUnboundGenericType || openGenericInterface.TypeKind != TypeKind.Interface) |
| | | 71 | | continue; |
| | | 72 | | |
| | | 73 | | // Get the Order property value |
| | 7 | 74 | | int order = 0; |
| | 17 | 75 | | foreach (var namedArg in attribute.NamedArguments) |
| | | 76 | | { |
| | 3 | 77 | | if (namedArg.Key == "Order" && namedArg.Value.Value is int orderValue) |
| | | 78 | | { |
| | 3 | 79 | | order = orderValue; |
| | 3 | 80 | | break; |
| | | 81 | | } |
| | | 82 | | } |
| | | 83 | | |
| | 7 | 84 | | result.Add(new OpenDecoratorInfo(typeSymbol, openGenericInterface, order)); |
| | | 85 | | } |
| | | 86 | | |
| | 1420254 | 87 | | return result; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Checks if a type has any OpenDecoratorFor attributes. |
| | | 92 | | /// </summary> |
| | | 93 | | /// <param name="typeSymbol">The type symbol to check.</param> |
| | | 94 | | /// <returns>True if the type has at least one OpenDecoratorFor attribute.</returns> |
| | | 95 | | public static bool HasOpenDecoratorForAttribute(INamedTypeSymbol typeSymbol) |
| | | 96 | | { |
| | 46 | 97 | | foreach (var attribute in typeSymbol.GetAttributes()) |
| | | 98 | | { |
| | 7 | 99 | | var attrClass = attribute.AttributeClass; |
| | 7 | 100 | | if (attrClass is null) |
| | | 101 | | continue; |
| | | 102 | | |
| | 7 | 103 | | if (attrClass.Name == OpenDecoratorForAttributeName) |
| | | 104 | | { |
| | 0 | 105 | | var fullName = attrClass.ToDisplayString(); |
| | 0 | 106 | | if (fullName == OpenDecoratorForAttributeFullName) |
| | 0 | 107 | | return true; |
| | | 108 | | } |
| | | 109 | | } |
| | | 110 | | |
| | 16 | 111 | | return false; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Finds all closed implementations of an open generic interface in the given types. |
| | | 116 | | /// </summary> |
| | | 117 | | /// <param name="openGenericInterface">The open generic interface (e.g., IHandler{}).</param> |
| | | 118 | | /// <param name="allTypes">All types to search through.</param> |
| | | 119 | | /// <returns>A dictionary mapping closed interface types to their implementing types.</returns> |
| | | 120 | | public static Dictionary<INamedTypeSymbol, List<INamedTypeSymbol>> FindClosedImplementations( |
| | | 121 | | INamedTypeSymbol openGenericInterface, |
| | | 122 | | IEnumerable<INamedTypeSymbol> allTypes) |
| | | 123 | | { |
| | 0 | 124 | | var result = new Dictionary<INamedTypeSymbol, List<INamedTypeSymbol>>(SymbolEqualityComparer.Default); |
| | | 125 | | |
| | 0 | 126 | | foreach (var type in allTypes) |
| | | 127 | | { |
| | 0 | 128 | | if (type.IsAbstract || type.TypeKind != TypeKind.Class) |
| | | 129 | | continue; |
| | | 130 | | |
| | | 131 | | // Check all interfaces implemented by this type |
| | 0 | 132 | | foreach (var iface in type.AllInterfaces) |
| | | 133 | | { |
| | | 134 | | // Check if this interface is a closed version of the open generic |
| | 0 | 135 | | if (iface.OriginalDefinition.Equals(openGenericInterface, SymbolEqualityComparer.Default)) |
| | | 136 | | { |
| | 0 | 137 | | if (!result.TryGetValue(iface, out var implementors)) |
| | | 138 | | { |
| | 0 | 139 | | implementors = new List<INamedTypeSymbol>(); |
| | 0 | 140 | | result[iface] = implementors; |
| | | 141 | | } |
| | 0 | 142 | | implementors.Add(type); |
| | | 143 | | } |
| | | 144 | | } |
| | | 145 | | } |
| | | 146 | | |
| | 0 | 147 | | return result; |
| | | 148 | | } |
| | | 149 | | } |