| 1 |
using System.Collections.Immutable; |
| 2 |
using System.Linq; |
| 3 |
using Microsoft.CodeAnalysis; |
| 4 |
using Microsoft.CodeAnalysis.CSharp; |
| 5 |
using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 6 |
using Microsoft.CodeAnalysis.Diagnostics; |
| 7 |
|
| 8 |
namespace Godot.SourceGenerators |
| 9 |
{ |
| 10 |
[DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 11 |
public sealed class MustBeVariantAnalyzer : DiagnosticAnalyzer |
| 12 |
{ |
| 13 |
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics |
| 14 |
=> ImmutableArray.Create( |
| 15 |
Common.GenericTypeArgumentMustBeVariantRule, |
| 16 |
Common.GenericTypeParameterMustBeVariantAnnotatedRule, |
| 17 |
Common.TypeArgumentParentSymbolUnhandledRule); |
| 18 |
|
| 19 |
public override void Initialize(AnalysisContext context) |
| 20 |
{ |
| 21 |
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 22 |
context.EnableConcurrentExecution(); |
| 23 |
context.RegisterSyntaxNodeAction(AnalyzeNode, SyntaxKind.TypeArgumentList); |
| 24 |
} |
| 25 |
|
| 26 |
private void AnalyzeNode(SyntaxNodeAnalysisContext context) |
| 27 |
{ |
| 28 |
|
| 29 |
if (IsInsideDocumentation(context.Node)) |
| 30 |
return; |
| 31 |
|
| 32 |
var typeArgListSyntax = (TypeArgumentListSyntax)context.Node; |
| 33 |
|
| 34 |
|
| 35 |
var parentSyntax = context.Node.Parent; |
| 36 |
Helper.ThrowIfNull(parentSyntax); |
| 37 |
|
| 38 |
var sm = context.SemanticModel; |
| 39 |
|
| 40 |
var typeCache = new MarshalUtils.TypeCache(context.Compilation); |
| 41 |
|
| 42 |
for (int i = 0; i < typeArgListSyntax.Arguments.Count; i++) |
| 43 |
{ |
| 44 |
var typeSyntax = typeArgListSyntax.Arguments[i]; |
| 45 |
|
| 46 |
|
| 47 |
if (typeSyntax is OmittedTypeArgumentSyntax) |
| 48 |
continue; |
| 49 |
|
| 50 |
var typeSymbol = sm.GetSymbolInfo(typeSyntax).Symbol as ITypeSymbol; |
| 51 |
Helper.ThrowIfNull(typeSymbol); |
| 52 |
|
| 53 |
var parentSymbolInfo = sm.GetSymbolInfo(parentSyntax); |
| 54 |
var parentSymbol = parentSymbolInfo.Symbol; |
| 55 |
if (parentSymbol == null) |
| 56 |
{ |
| 57 |
if (parentSymbolInfo.CandidateReason == CandidateReason.LateBound) |
| 58 |
{ |
| 59 |
|
| 60 |
continue; |
| 61 |
} |
| 62 |
|
| 63 |
Helper.ThrowIfNull(parentSymbol); |
| 64 |
} |
| 65 |
|
| 66 |
if (!ShouldCheckTypeArgument(context, parentSyntax, parentSymbol, typeSyntax, typeSymbol, i)) |
| 67 |
{ |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
if (typeSymbol is ITypeParameterSymbol typeParamSymbol) |
| 72 |
{ |
| 73 |
if (!typeParamSymbol.GetAttributes().Any(a => a.AttributeClass?.IsGodotMustBeVariantAttribute() ?? false)) |
| 74 |
{ |
| 75 |
context.ReportDiagnostic(Diagnostic.Create( |
| 76 |
Common.GenericTypeParameterMustBeVariantAnnotatedRule, |
| 77 |
typeSyntax.GetLocation(), |
| 78 |
typeSymbol.ToDisplayString() |
| 79 |
)); |
| 80 |
} |
| 81 |
continue; |
| 82 |
} |
| 83 |
|
| 84 |
var marshalType = MarshalUtils.ConvertManagedTypeToMarshalType(typeSymbol, typeCache); |
| 85 |
|
| 86 |
if (marshalType is null) |
| 87 |
{ |
| 88 |
context.ReportDiagnostic(Diagnostic.Create( |
| 89 |
Common.GenericTypeArgumentMustBeVariantRule, |
| 90 |
typeSyntax.GetLocation(), |
| 91 |
typeSymbol.ToDisplayString() |
| 92 |
)); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
private bool IsInsideDocumentation(SyntaxNode? syntax) |
| 103 |
{ |
| 104 |
while (syntax != null) |
| 105 |
{ |
| 106 |
if (syntax is DocumentationCommentTriviaSyntax) |
| 107 |
{ |
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
syntax = syntax.Parent; |
| 112 |
} |
| 113 |
|
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
private bool ShouldCheckTypeArgument( |
| 129 |
SyntaxNodeAnalysisContext context, |
| 130 |
SyntaxNode parentSyntax, |
| 131 |
ISymbol parentSymbol, |
| 132 |
TypeSyntax typeArgumentSyntax, |
| 133 |
ITypeSymbol typeArgumentSymbol, |
| 134 |
int typeArgumentIndex) |
| 135 |
{ |
| 136 |
ITypeParameterSymbol? typeParamSymbol = parentSymbol switch |
| 137 |
{ |
| 138 |
IMethodSymbol methodSymbol when parentSyntax.Ancestors().Any(s => s is AttributeSyntax) && |
| 139 |
methodSymbol.ContainingType.TypeParameters.Length > 0 |
| 140 |
=> methodSymbol.ContainingType.TypeParameters[typeArgumentIndex], |
| 141 |
|
| 142 |
IMethodSymbol { TypeParameters.Length: > 0 } methodSymbol |
| 143 |
=> methodSymbol.TypeParameters[typeArgumentIndex], |
| 144 |
|
| 145 |
INamedTypeSymbol { TypeParameters.Length: > 0 } typeSymbol |
| 146 |
=> typeSymbol.TypeParameters[typeArgumentIndex], |
| 147 |
|
| 148 |
_ |
| 149 |
=> null |
| 150 |
}; |
| 151 |
|
| 152 |
if (typeParamSymbol != null) |
| 153 |
{ |
| 154 |
return typeParamSymbol.GetAttributes() |
| 155 |
.Any(a => a.AttributeClass?.IsGodotMustBeVariantAttribute() ?? false); |
| 156 |
} |
| 157 |
|
| 158 |
context.ReportDiagnostic(Diagnostic.Create( |
| 159 |
Common.TypeArgumentParentSymbolUnhandledRule, |
| 160 |
typeArgumentSyntax.GetLocation(), |
| 161 |
parentSymbol.ToDisplayString() |
| 162 |
)); |
| 163 |
|
| 164 |
return false; |
| 165 |
} |
| 166 |
} |
| 167 |
} |