|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using Microsoft.CodeAnalysis; |
| 6 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 7 | + |
| 8 | +namespace FunctionalStateMachine.Core.Generator; |
| 9 | + |
| 10 | +[Generator] |
| 11 | +public sealed class TriggerTypeGenerator : IIncrementalGenerator |
| 12 | +{ |
| 13 | + private const string CreateMethodName = "Create"; |
| 14 | + // StateMachine<TState, TTrigger, TData, TCommand> — the 4-param with-data variant |
| 15 | + private const string StateMachineMetadataName4 = "StateMachine`4"; |
| 16 | + // StateMachine<TState, TTrigger, TCommand> — the 3-param NoData variant |
| 17 | + private const string StateMachineMetadataName3 = "StateMachine`3"; |
| 18 | + private const string StateMachineNamespace = "FunctionalStateMachine.Core"; |
| 19 | + |
| 20 | + public void Initialize(IncrementalGeneratorInitializationContext context) |
| 21 | + { |
| 22 | + var triggerTypes = context.SyntaxProvider |
| 23 | + .CreateSyntaxProvider( |
| 24 | + static (node, _) => IsCreateInvocation(node), |
| 25 | + static (ctx, _) => GetTriggerType(ctx)) |
| 26 | + .Where(static symbol => symbol is not null) |
| 27 | + .Select(static (symbol, _) => symbol!) |
| 28 | + .Collect(); |
| 29 | + |
| 30 | + var combined = triggerTypes.Combine(context.CompilationProvider); |
| 31 | + context.RegisterSourceOutput(combined, static (ctx, data) => |
| 32 | + { |
| 33 | + var (triggerTypeSymbols, compilation) = data; |
| 34 | + if (triggerTypeSymbols.IsDefaultOrEmpty) |
| 35 | + { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + Generate(ctx, compilation, triggerTypeSymbols); |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + private static bool IsCreateInvocation(SyntaxNode node) |
| 44 | + { |
| 45 | + if (node is not InvocationExpressionSyntax invocation) |
| 46 | + { |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + return invocation.Expression is MemberAccessExpressionSyntax |
| 51 | + { |
| 52 | + Name: { Identifier.ValueText: CreateMethodName } |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + private static INamedTypeSymbol? GetTriggerType(GeneratorSyntaxContext context) |
| 57 | + { |
| 58 | + if (context.Node is not InvocationExpressionSyntax invocation) |
| 59 | + { |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + var symbolInfo = context.SemanticModel.GetSymbolInfo(invocation); |
| 64 | + if (symbolInfo.Symbol is not IMethodSymbol { Name: CreateMethodName } methodSymbol) |
| 65 | + { |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + var containingType = methodSymbol.ContainingType; |
| 70 | + if (containingType is null) |
| 71 | + { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + // Match both 4-param (with data) and 3-param (NoData) StateMachine |
| 76 | + var metadataName = containingType.OriginalDefinition.MetadataName; |
| 77 | + if (!string.Equals(metadataName, StateMachineMetadataName4, StringComparison.Ordinal) && |
| 78 | + !string.Equals(metadataName, StateMachineMetadataName3, StringComparison.Ordinal)) |
| 79 | + { |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + if (!string.Equals( |
| 84 | + containingType.ContainingNamespace?.ToDisplayString(), |
| 85 | + StateMachineNamespace, |
| 86 | + StringComparison.Ordinal)) |
| 87 | + { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + // TTrigger is always the 2nd type argument (index 1) |
| 92 | + if (containingType.TypeArguments.Length < 2) |
| 93 | + { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + return containingType.TypeArguments[1] as INamedTypeSymbol; |
| 98 | + } |
| 99 | + |
| 100 | + private static void Generate( |
| 101 | + SourceProductionContext context, |
| 102 | + Compilation compilation, |
| 103 | + IReadOnlyList<INamedTypeSymbol> triggerBaseTypes) |
| 104 | + { |
| 105 | + // Only generate if [ModuleInitializer] is available in the target framework |
| 106 | + var moduleInitAttr = compilation.GetTypeByMetadataName( |
| 107 | + "System.Runtime.CompilerServices.ModuleInitializerAttribute"); |
| 108 | + if (moduleInitAttr is null) |
| 109 | + { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + // Collect all types in the current assembly (including nested) |
| 114 | + var allTypes = new List<INamedTypeSymbol>(); |
| 115 | + CollectTypes(compilation.Assembly.GlobalNamespace, allTypes); |
| 116 | + |
| 117 | + // Deduplicate trigger base types |
| 118 | + var uniqueTriggerTypes = new List<INamedTypeSymbol>(); |
| 119 | + var seen = new HashSet<INamedTypeSymbol>(SymbolEqualityComparer.Default); |
| 120 | + foreach (var t in triggerBaseTypes) |
| 121 | + { |
| 122 | + if (seen.Add(t)) |
| 123 | + { |
| 124 | + uniqueTriggerTypes.Add(t); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + var source = new StringBuilder(); |
| 129 | + source.AppendLine("// <auto-generated />"); |
| 130 | + source.AppendLine("using System;"); |
| 131 | + source.AppendLine("using System.Runtime.CompilerServices;"); |
| 132 | + source.AppendLine("using FunctionalStateMachine.Core;"); |
| 133 | + source.AppendLine(); |
| 134 | + source.AppendLine("namespace FunctionalStateMachine.Core.Generated"); |
| 135 | + source.AppendLine("{"); |
| 136 | + source.AppendLine(" internal static class TriggerTypeRegistrationBootstrap"); |
| 137 | + source.AppendLine(" {"); |
| 138 | + source.AppendLine(" [ModuleInitializer]"); |
| 139 | + source.AppendLine(" internal static void Initialize()"); |
| 140 | + source.AppendLine(" {"); |
| 141 | + |
| 142 | + foreach (var triggerBaseType in uniqueTriggerTypes) |
| 143 | + { |
| 144 | + // Skip trigger types that are not accessible from the generated module initializer |
| 145 | + if (!IsAccessibleFromAssembly(triggerBaseType)) |
| 146 | + { |
| 147 | + continue; |
| 148 | + } |
| 149 | + |
| 150 | + var concreteTypes = GetConcreteTypes(triggerBaseType, allTypes); |
| 151 | + |
| 152 | + // Nothing to register if no accessible concrete types were found |
| 153 | + if (concreteTypes.Count == 0) |
| 154 | + { |
| 155 | + continue; |
| 156 | + } |
| 157 | + |
| 158 | + var baseTypeName = triggerBaseType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| 159 | + var typeArray = string.Join(", ", concreteTypes.Select(t => |
| 160 | + $"typeof({t.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)})")); |
| 161 | + source.AppendLine( |
| 162 | + $" TriggerTypeRegistry.Register<{baseTypeName}>(new[] {{ {typeArray} }});"); |
| 163 | + } |
| 164 | + |
| 165 | + source.AppendLine(" }"); |
| 166 | + source.AppendLine(" }"); |
| 167 | + source.AppendLine("}"); |
| 168 | + |
| 169 | + context.AddSource("TriggerTypeRegistry.g.cs", source.ToString()); |
| 170 | + } |
| 171 | + |
| 172 | + private static List<INamedTypeSymbol> GetConcreteTypes( |
| 173 | + INamedTypeSymbol baseType, |
| 174 | + List<INamedTypeSymbol> allTypes) |
| 175 | + { |
| 176 | + // For enum TTrigger, register the enum type itself (no subtypes) |
| 177 | + if (baseType.TypeKind == TypeKind.Enum) |
| 178 | + { |
| 179 | + return new List<INamedTypeSymbol> { baseType }; |
| 180 | + } |
| 181 | + |
| 182 | + // Find all non-abstract, non-generic types that derive from the base type |
| 183 | + // Only include types that are accessible from the generated module initializer |
| 184 | + var derivedTypes = allTypes |
| 185 | + .Where(t => !t.IsAbstract && t.TypeParameters.IsEmpty) |
| 186 | + .Where(t => !SymbolEqualityComparer.Default.Equals(t, baseType) && IsAssignableTo(t, baseType)) |
| 187 | + .Where(IsAccessibleFromAssembly) |
| 188 | + .OrderBy(t => t.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), StringComparer.Ordinal) |
| 189 | + .ToList(); |
| 190 | + |
| 191 | + // If no derived types found, register the base type itself as a fallback |
| 192 | + // (only if the base type itself is accessible) |
| 193 | + if (derivedTypes.Count == 0 && IsAccessibleFromAssembly(baseType)) |
| 194 | + { |
| 195 | + return new List<INamedTypeSymbol> { baseType }; |
| 196 | + } |
| 197 | + |
| 198 | + return derivedTypes; |
| 199 | + } |
| 200 | + |
| 201 | + /// <summary> |
| 202 | + /// Returns true if the type and all its containing types are at least internal, |
| 203 | + /// making them accessible from the generated module initializer class in the same assembly. |
| 204 | + /// </summary> |
| 205 | + private static bool IsAccessibleFromAssembly(INamedTypeSymbol type) |
| 206 | + { |
| 207 | + var current = type; |
| 208 | + while (current is not null) |
| 209 | + { |
| 210 | + switch (current.DeclaredAccessibility) |
| 211 | + { |
| 212 | + case Accessibility.Private: |
| 213 | + case Accessibility.Protected: |
| 214 | + case Accessibility.ProtectedAndInternal: |
| 215 | + return false; |
| 216 | + } |
| 217 | + |
| 218 | + current = current.ContainingType; |
| 219 | + } |
| 220 | + |
| 221 | + return true; |
| 222 | + } |
| 223 | + |
| 224 | + private static bool IsAssignableTo(INamedTypeSymbol type, INamedTypeSymbol baseType) |
| 225 | + { |
| 226 | + var current = type.BaseType; |
| 227 | + while (current is not null) |
| 228 | + { |
| 229 | + if (SymbolEqualityComparer.Default.Equals(current.OriginalDefinition, baseType.OriginalDefinition)) |
| 230 | + { |
| 231 | + return true; |
| 232 | + } |
| 233 | + |
| 234 | + current = current.BaseType; |
| 235 | + } |
| 236 | + |
| 237 | + return false; |
| 238 | + } |
| 239 | + |
| 240 | + private static void CollectTypes(INamespaceSymbol namespaceSymbol, List<INamedTypeSymbol> types) |
| 241 | + { |
| 242 | + foreach (var type in namespaceSymbol.GetTypeMembers()) |
| 243 | + { |
| 244 | + types.Add(type); |
| 245 | + CollectNestedTypes(type, types); |
| 246 | + } |
| 247 | + |
| 248 | + foreach (var nestedNamespace in namespaceSymbol.GetNamespaceMembers()) |
| 249 | + { |
| 250 | + CollectTypes(nestedNamespace, types); |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + private static void CollectNestedTypes(INamedTypeSymbol type, List<INamedTypeSymbol> types) |
| 255 | + { |
| 256 | + foreach (var nested in type.GetTypeMembers()) |
| 257 | + { |
| 258 | + types.Add(nested); |
| 259 | + CollectNestedTypes(nested, types); |
| 260 | + } |
| 261 | + } |
| 262 | +} |
0 commit comments