| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Collections.Immutable; |
| | | 4 | | using System.Linq; |
| | | 5 | | using Microsoft.CodeAnalysis; |
| | | 6 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 7 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 8 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 9 | | |
| | | 10 | | namespace Hertzole.ScriptableValues.Generator; |
| | | 11 | | |
| | | 12 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 13 | | public sealed class NoCallbackImplementationAnalyzer : DiagnosticAnalyzer |
| | | 14 | | { |
| | 0 | 15 | | private static readonly ImmutableArray<DiagnosticDescriptor> supportedDiagnostics = ImmutableArray.Create(Diagnostic |
| | | 16 | | |
| | 0 | 17 | | private static readonly SyntaxKind[] supportedSyntaxKinds = [SyntaxKind.Attribute]; |
| | | 18 | | |
| | | 19 | | /// <inheritdoc /> |
| | | 20 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics |
| | | 21 | | { |
| | 0 | 22 | | get { return supportedDiagnostics; } |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public override void Initialize(AnalysisContext context) |
| | | 27 | | { |
| | 0 | 28 | | context.EnableConcurrentExecution(); |
| | 0 | 29 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | | 30 | | |
| | 0 | 31 | | context.RegisterSyntaxNodeAction(Action, supportedSyntaxKinds); |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | private static void Action(SyntaxNodeAnalysisContext context) |
| | | 35 | | { |
| | 0 | 36 | | if (context.Node is not AttributeSyntax attribute) |
| | | 37 | | { |
| | 0 | 38 | | return; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | // Get symbol |
| | 0 | 42 | | if (context.SemanticModel.GetSymbolInfo(attribute).Symbol is not IMethodSymbol methodSymbol) |
| | | 43 | | { |
| | 0 | 44 | | return; |
| | | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | INamedTypeSymbol? attributeType = methodSymbol.ContainingType; |
| | 0 | 48 | | if (attributeType == null) |
| | | 49 | | { |
| | 0 | 50 | | return; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | // Check if the attribute is a callback |
| | 0 | 54 | | if (!attributeType.TryGetCallbackAttribute(out CallbackType callbackType)) |
| | | 55 | | { |
| | 0 | 56 | | return; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | // Get member declaration syntax in parent |
| | 0 | 60 | | MemberDeclarationSyntax? memberDeclaration = null; |
| | 0 | 61 | | SyntaxNode? parent = attribute.Parent; |
| | 0 | 62 | | while (memberDeclaration == null && parent != null) |
| | | 63 | | { |
| | 0 | 64 | | if (parent is MemberDeclarationSyntax member) |
| | | 65 | | { |
| | 0 | 66 | | memberDeclaration = member; |
| | 0 | 67 | | break; |
| | | 68 | | } |
| | | 69 | | |
| | 0 | 70 | | parent = parent.Parent; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | // No member was found. Weird... |
| | 0 | 74 | | if (memberDeclaration == null) |
| | | 75 | | { |
| | 0 | 76 | | return; |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | // Get symbol for the member |
| | 0 | 80 | | if (!memberDeclaration.TryGetFieldOrPropertyWrapper(context.SemanticModel, out ImmutableArray<FieldOrPropertyWra |
| | 0 | 81 | | wrappers.IsDefaultOrEmpty) |
| | | 82 | | { |
| | 0 | 83 | | return; |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | // Get the containing type of the member |
| | 0 | 87 | | SyntaxNode? containingTypeNode = memberDeclaration.Parent; |
| | 0 | 88 | | if (containingTypeNode == null || context.SemanticModel.GetDeclaredSymbol(containingTypeNode) is not INamedTypeS |
| | | 89 | | { |
| | 0 | 90 | | return; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | // Must have the marker attribute. |
| | 0 | 94 | | if (!containingType.HasAttribute(Types.GLOBAL_MARKER_ATTRIBUTE)) |
| | | 95 | | { |
| | | 96 | | Log.Info($"No marker attribute found on containing type {containingType}. Skipping."); |
| | 0 | 97 | | return; |
| | | 98 | | } |
| | | 99 | | |
| | 0 | 100 | | if (!ScriptableValueHelper.DoesTypeSupportCallbacks(containingType, out _)) |
| | | 101 | | { |
| | 0 | 102 | | return; |
| | | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | using ArrayBuilder<ITypeSymbol> parametersBuilder = new ArrayBuilder<ITypeSymbol>(); |
| | | 106 | | |
| | 0 | 107 | | for (int i = 0; i < wrappers.Length; i++) |
| | | 108 | | { |
| | 0 | 109 | | parametersBuilder.Clear(); |
| | | 110 | | |
| | 0 | 111 | | CallbackFlags flags = CallbackFlags.None; |
| | 0 | 112 | | FieldOrPropertyWrapper wrapper = wrappers.ItemRef(i); |
| | | 113 | | |
| | 0 | 114 | | if (!ScriptableValueHelper.TryGetScriptableType(context.SemanticModel, (INamedTypeSymbol?) wrapper.Type, out |
| | 0 | 115 | | out ITypeSymbol? genericType)) |
| | | 116 | | { |
| | | 117 | | continue; |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | // Check if the attribute supports this type. |
| | 0 | 121 | | if (!ScriptableValueHelper.IsSupportedType(in callbackType, in scriptableType)) |
| | | 122 | | { |
| | | 123 | | continue; |
| | | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | string? callbackName = null; |
| | 0 | 127 | | ImmutableArray<AttributeData> attributes = wrapper.Symbol.GetAttributes(); |
| | 0 | 128 | | for (int j = 0; j < attributes.Length; j++) |
| | | 129 | | { |
| | 0 | 130 | | if (methodSymbol.Equals(attributes[j].AttributeConstructor, SymbolEqualityComparer.Default)) |
| | | 131 | | { |
| | 0 | 132 | | if (scriptableType == ScriptableType.Value) |
| | | 133 | | { |
| | | 134 | | // This is the same attribute as the one we are analyzing |
| | | 135 | | // Check if the attribute has a callback flag |
| | 0 | 136 | | if (attributes[j].ConstructorArguments.Length > 0) |
| | | 137 | | { |
| | 0 | 138 | | if (attributes[j].ConstructorArguments[0].Value is int callbackFlags) |
| | | 139 | | { |
| | 0 | 140 | | if (callbackFlags == 0) // Changing |
| | | 141 | | { |
| | 0 | 142 | | flags = CallbackFlags.PreInvoke; |
| | | 143 | | } |
| | 0 | 144 | | else if (callbackFlags == 1) |
| | | 145 | | { |
| | 0 | 146 | | flags = CallbackFlags.PostInvoke; |
| | | 147 | | } |
| | | 148 | | } |
| | | 149 | | } |
| | | 150 | | else |
| | | 151 | | { |
| | | 152 | | // There are no arguments. Then it's a "Changed" callback |
| | 0 | 153 | | flags = CallbackFlags.PostInvoke; |
| | | 154 | | } |
| | | 155 | | } |
| | | 156 | | |
| | 0 | 157 | | if (!attributes[j].NamedArguments.IsDefaultOrEmpty && attributes[j].NamedArguments.Any(kvp => kvp.Ke |
| | | 158 | | { |
| | 0 | 159 | | KeyValuePair<string, TypedConstant> namedArg = attributes[j].NamedArguments.First(kvp => kvp.Key |
| | 0 | 160 | | if (namedArg.Value.Value is string name) |
| | | 161 | | { |
| | 0 | 162 | | callbackName = name; |
| | | 163 | | } |
| | | 164 | | } |
| | | 165 | | } |
| | | 166 | | } |
| | | 167 | | |
| | 0 | 168 | | callbackName ??= Naming.CreateCallbackName(wrapper.Name, in scriptableType, in flags); |
| | | 169 | | |
| | | 170 | | switch (scriptableType) |
| | | 171 | | { |
| | | 172 | | case ScriptableType.Value: |
| | 0 | 173 | | parametersBuilder.Add(genericType!); |
| | 0 | 174 | | parametersBuilder.Add(genericType!); |
| | 0 | 175 | | break; |
| | | 176 | | case ScriptableType.GenericEvent: |
| | 0 | 177 | | parametersBuilder.Add(context.Compilation.GetSpecialType(SpecialType.System_Object)); |
| | 0 | 178 | | parametersBuilder.Add(genericType!); |
| | 0 | 179 | | break; |
| | | 180 | | case ScriptableType.Event: |
| | 0 | 181 | | parametersBuilder.Add(context.Compilation.GetSpecialType(SpecialType.System_Object)); |
| | 0 | 182 | | parametersBuilder.Add(context.Compilation.GetTypeByMetadataName("System.EventArgs")!); |
| | 0 | 183 | | break; |
| | | 184 | | case ScriptableType.Pool: |
| | 0 | 185 | | parametersBuilder.Add(context.Compilation.GetTypeByMetadataName("Hertzole.ScriptableValues.PoolActio |
| | 0 | 186 | | parametersBuilder.Add(genericType!); |
| | 0 | 187 | | break; |
| | | 188 | | case ScriptableType.List: |
| | | 189 | | case ScriptableType.Dictionary: |
| | 0 | 190 | | parametersBuilder.Add(context.Compilation.GetCollectionChangedArgs(in genericType!)); |
| | 0 | 191 | | break; |
| | | 192 | | default: |
| | 0 | 193 | | throw new ArgumentOutOfRangeException(); |
| | | 194 | | } |
| | | 195 | | |
| | 0 | 196 | | ImmutableArray<ITypeSymbol> parameters = parametersBuilder.ToImmutable(); |
| | | 197 | | |
| | | 198 | | // Check if the method is already implemented with correct parameters |
| | 0 | 199 | | if (HasCorrectCallbackImplementation(containingType, callbackName, parameters)) |
| | | 200 | | { |
| | | 201 | | continue; // Skip reporting diagnostic if method exists |
| | | 202 | | } |
| | | 203 | | |
| | 0 | 204 | | ImmutableDictionary<string, string?>.Builder properties = ImmutableDictionary.CreateBuilder<string, string?> |
| | 0 | 205 | | properties.Add("callbackName", callbackName); |
| | 0 | 206 | | properties.Add("fieldName", wrapper.Name); |
| | | 207 | | |
| | 0 | 208 | | context.ReportDiagnostic(Diagnostic.Create(DiagnosticRules.NoCallbackImplementation, attribute.GetLocation() |
| | 0 | 209 | | callbackName)); |
| | | 210 | | } |
| | 0 | 211 | | } |
| | | 212 | | |
| | | 213 | | private static bool HasCorrectCallbackImplementation(INamedTypeSymbol containingType, string callbackName, Immutable |
| | | 214 | | { |
| | | 215 | | // Get all methods from the class with the matching name |
| | 0 | 216 | | ImmutableArray<IMethodSymbol> candidateMethods = containingType.GetMembers(callbackName) |
| | 0 | 217 | | .OfType<IMethodSymbol>() |
| | 0 | 218 | | .Where(static m => m.MethodKind == MethodKind.Ord |
| | 0 | 219 | | .ToImmutableArray(); |
| | | 220 | | |
| | 0 | 221 | | if (!candidateMethods.Any()) |
| | | 222 | | { |
| | 0 | 223 | | return false; // No method with the correct name found |
| | | 224 | | } |
| | | 225 | | |
| | 0 | 226 | | for (int i = 0; i < candidateMethods.Length; i++) |
| | | 227 | | { |
| | 0 | 228 | | IMethodSymbol method = candidateMethods.ItemRef(i); |
| | | 229 | | |
| | 0 | 230 | | if (method.PartialImplementationPart == null) |
| | | 231 | | { |
| | | 232 | | continue; |
| | | 233 | | } |
| | | 234 | | |
| | 0 | 235 | | if (method.Parameters.Length != parameters.Length) |
| | | 236 | | { |
| | | 237 | | continue; |
| | | 238 | | } |
| | | 239 | | |
| | | 240 | | // Check if the parameters match the expected types |
| | 0 | 241 | | bool allParametersMatch = true; |
| | 0 | 242 | | for (int j = 0; j < method.Parameters.Length; j++) |
| | | 243 | | { |
| | 0 | 244 | | if (!SymbolEqualityComparer.Default.Equals(method.Parameters[j].Type, parameters[j])) |
| | | 245 | | { |
| | 0 | 246 | | allParametersMatch = false; |
| | 0 | 247 | | break; |
| | | 248 | | } |
| | | 249 | | } |
| | | 250 | | |
| | 0 | 251 | | if (allParametersMatch) |
| | | 252 | | { |
| | 0 | 253 | | return true; // Found a matching method |
| | | 254 | | } |
| | | 255 | | } |
| | | 256 | | |
| | 0 | 257 | | return false; |
| | | 258 | | } |
| | | 259 | | } |