| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | using System.Linq; |
| | | 3 | | |
| | | 4 | | using Microsoft.CodeAnalysis; |
| | | 5 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 6 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 7 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 8 | | |
| | | 9 | | namespace NexusLabs.Needlr.Generators; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Analyzer that validates [RegisterClosedOverImplementationsOf] attribute usage: |
| | | 13 | | /// - NDLRGEN035: Source type argument must be an open generic interface |
| | | 14 | | /// - NDLRGEN036: Composition class must be an open generic with matching arity |
| | | 15 | | /// - NDLRGEN037: Composition class must specify and implement the As service type |
| | | 16 | | /// </summary> |
| | | 17 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 18 | | public sealed class RegisterClosedOverImplementationsOfAttributeAnalyzer : DiagnosticAnalyzer |
| | | 19 | | { |
| | | 20 | | private const string AttributeName = "RegisterClosedOverImplementationsOfAttribute"; |
| | | 21 | | private const string GeneratorsNamespace = "NexusLabs.Needlr.Generators"; |
| | | 22 | | |
| | | 23 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 300 | 24 | | ImmutableArray.Create( |
| | 300 | 25 | | DiagnosticDescriptors.ComposedSourceNotOpenGenericInterface, |
| | 300 | 26 | | DiagnosticDescriptors.ComposedClassNotOpenGeneric, |
| | 300 | 27 | | DiagnosticDescriptors.ComposedClassNotImplementingAs); |
| | | 28 | | |
| | | 29 | | public override void Initialize(AnalysisContext context) |
| | | 30 | | { |
| | 24 | 31 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 24 | 32 | | context.EnableConcurrentExecution(); |
| | | 33 | | |
| | 24 | 34 | | context.RegisterSyntaxNodeAction(AnalyzeAttribute, SyntaxKind.Attribute); |
| | 24 | 35 | | } |
| | | 36 | | |
| | | 37 | | private static void AnalyzeAttribute(SyntaxNodeAnalysisContext context) |
| | | 38 | | { |
| | 180 | 39 | | var attributeSyntax = (AttributeSyntax)context.Node; |
| | 180 | 40 | | var attributeSymbol = context.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol?.ContainingType; |
| | | 41 | | |
| | 180 | 42 | | if (attributeSymbol is null) |
| | 0 | 43 | | return; |
| | | 44 | | |
| | 180 | 45 | | if (!IsComposedAttribute(attributeSymbol)) |
| | 165 | 46 | | return; |
| | | 47 | | |
| | 15 | 48 | | if (attributeSyntax.Parent?.Parent is not ClassDeclarationSyntax classDeclaration) |
| | 0 | 49 | | return; |
| | | 50 | | |
| | 15 | 51 | | if (context.SemanticModel.GetDeclaredSymbol(classDeclaration) is not INamedTypeSymbol classSymbol) |
| | 0 | 52 | | return; |
| | | 53 | | |
| | 15 | 54 | | var sourceType = GetSourceTypeArgument(attributeSyntax, context.SemanticModel); |
| | 15 | 55 | | if (sourceType is null) |
| | 0 | 56 | | return; |
| | | 57 | | |
| | | 58 | | // NDLRGEN035: source must be an open generic interface. |
| | 15 | 59 | | if (!IsOpenGenericInterface(sourceType, out var typeDescription)) |
| | | 60 | | { |
| | 4 | 61 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 4 | 62 | | DiagnosticDescriptors.ComposedSourceNotOpenGenericInterface, |
| | 4 | 63 | | attributeSyntax.GetLocation(), |
| | 4 | 64 | | sourceType.ToDisplayString(), |
| | 4 | 65 | | typeDescription)); |
| | 4 | 66 | | return; |
| | | 67 | | } |
| | | 68 | | |
| | 11 | 69 | | if (sourceType is not INamedTypeSymbol sourceInterface) |
| | 0 | 70 | | return; |
| | | 71 | | |
| | 11 | 72 | | var expectedArity = sourceInterface.TypeParameters.Length; |
| | | 73 | | |
| | | 74 | | // NDLRGEN036: composition must be an open generic with matching arity. |
| | 11 | 75 | | if (!classSymbol.IsGenericType || classSymbol.TypeParameters.Length != expectedArity) |
| | | 76 | | { |
| | 4 | 77 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 4 | 78 | | DiagnosticDescriptors.ComposedClassNotOpenGeneric, |
| | 4 | 79 | | attributeSyntax.GetLocation(), |
| | 4 | 80 | | classSymbol.Name, |
| | 4 | 81 | | sourceInterface.ToDisplayString(), |
| | 4 | 82 | | expectedArity)); |
| | 4 | 83 | | return; |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | // NDLRGEN037: composition must specify and implement the As service type. |
| | 7 | 87 | | var asType = GetAsServiceType(attributeSyntax, context.SemanticModel); |
| | 7 | 88 | | if (asType is null || !ImplementsServiceType(classSymbol, asType)) |
| | | 89 | | { |
| | 4 | 90 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 4 | 91 | | DiagnosticDescriptors.ComposedClassNotImplementingAs, |
| | 4 | 92 | | attributeSyntax.GetLocation(), |
| | 4 | 93 | | classSymbol.Name)); |
| | | 94 | | } |
| | 7 | 95 | | } |
| | | 96 | | |
| | | 97 | | private static bool IsComposedAttribute(INamedTypeSymbol attributeSymbol) => |
| | 180 | 98 | | attributeSymbol.Name == AttributeName && |
| | 180 | 99 | | attributeSymbol.ContainingNamespace?.ToString() == GeneratorsNamespace; |
| | | 100 | | |
| | | 101 | | private static ITypeSymbol? GetSourceTypeArgument(AttributeSyntax attributeSyntax, SemanticModel semanticModel) |
| | | 102 | | { |
| | 15 | 103 | | var argumentList = attributeSyntax.ArgumentList; |
| | 15 | 104 | | if (argumentList is null || argumentList.Arguments.Count == 0) |
| | 0 | 105 | | return null; |
| | | 106 | | |
| | | 107 | | // The first positional argument is the source open generic interface. |
| | 30 | 108 | | var firstArgument = argumentList.Arguments.FirstOrDefault(a => a.NameEquals is null); |
| | 15 | 109 | | if (firstArgument?.Expression is TypeOfExpressionSyntax typeOfExpression) |
| | 15 | 110 | | return semanticModel.GetTypeInfo(typeOfExpression.Type).Type; |
| | | 111 | | |
| | 0 | 112 | | return null; |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | private static ITypeSymbol? GetAsServiceType(AttributeSyntax attributeSyntax, SemanticModel semanticModel) |
| | | 116 | | { |
| | 7 | 117 | | var argumentList = attributeSyntax.ArgumentList; |
| | 7 | 118 | | if (argumentList is null) |
| | 0 | 119 | | return null; |
| | | 120 | | |
| | 7 | 121 | | var asArgument = argumentList.Arguments |
| | 19 | 122 | | .FirstOrDefault(a => a.NameEquals?.Name.Identifier.Text == "As"); |
| | | 123 | | |
| | 7 | 124 | | if (asArgument?.Expression is TypeOfExpressionSyntax typeOfExpression) |
| | 5 | 125 | | return semanticModel.GetTypeInfo(typeOfExpression.Type).Type; |
| | | 126 | | |
| | 2 | 127 | | return null; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | private static bool IsOpenGenericInterface(ITypeSymbol? typeSymbol, out string typeDescription) |
| | | 131 | | { |
| | 15 | 132 | | if (typeSymbol is null) |
| | | 133 | | { |
| | 0 | 134 | | typeDescription = "null"; |
| | 0 | 135 | | return false; |
| | | 136 | | } |
| | | 137 | | |
| | 15 | 138 | | if (typeSymbol is not INamedTypeSymbol namedType) |
| | | 139 | | { |
| | 0 | 140 | | typeDescription = $"{typeSymbol.TypeKind}"; |
| | 0 | 141 | | return false; |
| | | 142 | | } |
| | | 143 | | |
| | 15 | 144 | | if (namedType.TypeKind != TypeKind.Interface) |
| | | 145 | | { |
| | 2 | 146 | | typeDescription = $"{namedType.TypeKind} (not an interface)"; |
| | 2 | 147 | | return false; |
| | | 148 | | } |
| | | 149 | | |
| | 13 | 150 | | if (!namedType.IsGenericType) |
| | | 151 | | { |
| | 2 | 152 | | typeDescription = "non-generic interface"; |
| | 2 | 153 | | return false; |
| | | 154 | | } |
| | | 155 | | |
| | 11 | 156 | | if (!namedType.IsUnboundGenericType && |
| | 11 | 157 | | !namedType.TypeArguments.All(t => t.TypeKind == TypeKind.TypeParameter)) |
| | | 158 | | { |
| | 0 | 159 | | typeDescription = "closed generic interface (use typeof(IInterface<>) not typeof(IInterface<T>))"; |
| | 0 | 160 | | return false; |
| | | 161 | | } |
| | | 162 | | |
| | 11 | 163 | | typeDescription = "open generic interface"; |
| | 11 | 164 | | return true; |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | private static bool ImplementsServiceType(INamedTypeSymbol classSymbol, ITypeSymbol serviceType) |
| | | 168 | | { |
| | 5 | 169 | | if (serviceType.TypeKind == TypeKind.Interface) |
| | 10 | 170 | | return classSymbol.AllInterfaces.Any(i => SymbolEqualityComparer.Default.Equals(i, serviceType)); |
| | | 171 | | |
| | 0 | 172 | | for (var baseType = classSymbol.BaseType; baseType is not null; baseType = baseType.BaseType) |
| | | 173 | | { |
| | 0 | 174 | | if (SymbolEqualityComparer.Default.Equals(baseType, serviceType)) |
| | 0 | 175 | | return true; |
| | | 176 | | } |
| | | 177 | | |
| | 0 | 178 | | return false; |
| | | 179 | | } |
| | | 180 | | } |