|
| 1 | +// Copyright (c) Files Community |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +namespace Files.Core.SourceGenerator.Generators |
| 5 | +{ |
| 6 | + [Generator(LanguageNames.CSharp)] |
| 7 | + internal class VTableFunctionGenerator : IIncrementalGenerator |
| 8 | + { |
| 9 | + public void Initialize(IncrementalGeneratorInitializationContext context) |
| 10 | + { |
| 11 | + var sources = context.SyntaxProvider.ForAttributeWithMetadataName( |
| 12 | + "Files.Shared.Attributes.GeneratedVTableFunctionAttribute", |
| 13 | + static (node, token) => |
| 14 | + { |
| 15 | + token.ThrowIfCancellationRequested(); |
| 16 | + |
| 17 | + // Check if the method has partial modifier and is public or internal (and not static) |
| 18 | + if (node is not MethodDeclarationSyntax { AttributeLists.Count: > 0 } method || |
| 19 | + !method.Modifiers.Any(SyntaxKind.PartialKeyword) || |
| 20 | + !(method.Modifiers.Any(SyntaxKind.PublicKeyword) || method.Modifiers.Any(SyntaxKind.InternalKeyword)) || |
| 21 | + method.Modifiers.Any(SyntaxKind.StaticKeyword)) |
| 22 | + return false; |
| 23 | + |
| 24 | + // Check if the type containing the method has partial modifier and is a struct |
| 25 | + if (node.Parent is not TypeDeclarationSyntax { Keyword.RawKind: (int)SyntaxKind.StructKeyword, Modifiers: { } modifiers } || |
| 26 | + !modifiers.Any(SyntaxKind.PartialKeyword)) |
| 27 | + return false; |
| 28 | + |
| 29 | + return true; |
| 30 | + }, |
| 31 | + static (context, token) => |
| 32 | + { |
| 33 | + token.ThrowIfCancellationRequested(); |
| 34 | + |
| 35 | + var fullyQualifiedParentTypeName = context.TargetSymbol.ContainingType.ToString(); |
| 36 | + var structNamespace = context.TargetSymbol.ContainingType.ContainingNamespace.ToString(); |
| 37 | + var structName = context.TargetSymbol.ContainingType.Name; |
| 38 | + var methodSymbol = (IMethodSymbol)context.TargetSymbol; |
| 39 | + var functionName = methodSymbol.Name; |
| 40 | + var returnTypeName = methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); |
| 41 | + var parameters = methodSymbol.Parameters.Select(x => new ParameterTypeNamePair(x.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), x.Name)); |
| 42 | + var index = (int)context.Attributes[0].NamedArguments.FirstOrDefault(x => x.Key.Equals("Index")).Value.Value!; |
| 43 | + |
| 44 | + return new VTableFunctionInfo(fullyQualifiedParentTypeName, structNamespace, structName, functionName, returnTypeName, index, new(parameters.ToImmutableArray())); |
| 45 | + }) |
| 46 | + .Where(static item => item is not null) |
| 47 | + .Collect() |
| 48 | + .Select((items, token) => |
| 49 | + { |
| 50 | + token.ThrowIfCancellationRequested(); |
| 51 | + |
| 52 | + return items.GroupBy(source => source.FullyQualifiedParentTypeName, StringComparer.OrdinalIgnoreCase).ToImmutableArray(); |
| 53 | + }); |
| 54 | + |
| 55 | + |
| 56 | + context.RegisterSourceOutput(sources, (context, sources) => |
| 57 | + { |
| 58 | + foreach (var source in sources) |
| 59 | + { |
| 60 | + string vtableFunctionsCode = GenerateVtableFunctionsForStruct(source.ToImmutableArray()); |
| 61 | + context.AddSource($"{source.Key}_VTableFunctions.g.cs", vtableFunctionsCode); |
| 62 | + } |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + private string GenerateVtableFunctionsForStruct(ImmutableArray<VTableFunctionInfo> sources) |
| 67 | + { |
| 68 | + StringBuilder builder = new(); |
| 69 | + |
| 70 | + builder.AppendLine($"// <auto-generated/>"); |
| 71 | + builder.AppendLine(); |
| 72 | + builder.AppendLine($"using global::System.Runtime.CompilerServices;"); |
| 73 | + builder.AppendLine(); |
| 74 | + builder.AppendLine($"#pragma warning disable"); |
| 75 | + builder.AppendLine(); |
| 76 | + |
| 77 | + builder.AppendLine($"namespace {sources.ElementAt(0).ParentTypeNamespace};"); |
| 78 | + builder.AppendLine(); |
| 79 | + |
| 80 | + builder.AppendLine($"public unsafe partial struct {sources.ElementAt(0).ParentTypeName}"); |
| 81 | + builder.AppendLine($"{{"); |
| 82 | + |
| 83 | + builder.AppendLine($" private void** lpVtbl;"); |
| 84 | + builder.AppendLine(); |
| 85 | + |
| 86 | + var sourceIndex = 0; |
| 87 | + var sourceCount = sources.Count(); |
| 88 | + |
| 89 | + foreach (var source in sources) |
| 90 | + { |
| 91 | + builder.AppendLine($" [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]"); |
| 92 | + |
| 93 | + builder.AppendLine($" public partial {source.ReturnTypeName} {source.Name}({string.Join(", ", source.Parameters.Select(x => $"{x.FullyQualifiedTypeName} {x.ValueName}"))})"); |
| 94 | + builder.AppendLine($" {{"); |
| 95 | + builder.AppendLine($" return ({source.ReturnTypeName})((delegate* unmanaged[MemberFunction]<{sources.ElementAt(0).ParentTypeName}*, {string.Join(", ", source.Parameters.Select(x => $"{x.FullyQualifiedTypeName}"))}, int>)(lpVtbl[{source.Index}]))"); |
| 96 | + builder.AppendLine($" (({sources.ElementAt(0).ParentTypeName}*)global::System.Runtime.CompilerServices.Unsafe.AsPointer(ref this), {string.Join(", ", source.Parameters.Select(x => $"{x.ValueName}"))});"); |
| 97 | + builder.AppendLine($" }}"); |
| 98 | + |
| 99 | + if (sourceIndex < sourceCount - 1) |
| 100 | + builder.AppendLine(); |
| 101 | + |
| 102 | + sourceIndex++; |
| 103 | + } |
| 104 | + |
| 105 | + builder.AppendLine($"}}"); |
| 106 | + |
| 107 | + return builder.ToString(); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments