Skip to content

Commit 179f3df

Browse files
committed
Do not use symbol classes
1 parent 46b34cd commit 179f3df

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Files.Core.SourceGenerator.Data
6+
{
7+
internal record ParameterTypeNamePair(string FullyQualifiedTypeName, string ValueName);
8+
}

src/Files.Core.SourceGenerator/Data/VTableFunctionInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ internal record VTableFunctionInfo(
1010
string Name,
1111
string ReturnTypeName,
1212
int Index,
13-
EquatableArray<ISymbol> Parameters);
13+
EquatableArray<ParameterTypeNamePair> Parameters);
1414
}

src/Files.Core.SourceGenerator/Generators/VTableFunctionGenerator.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright (c) Files Community
22
// Licensed under the MIT License.
33

4-
using Microsoft.CodeAnalysis;
5-
using System.Reflection;
6-
74
namespace Files.Core.SourceGenerator.Generators
85
{
96
[Generator(LanguageNames.CSharp)]
@@ -41,32 +38,32 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
4138
var methodSymbol = (IMethodSymbol)context.TargetSymbol;
4239
var functionName = methodSymbol.Name;
4340
var returnTypeName = methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
44-
var parameters = methodSymbol.Parameters.CastArray<ISymbol>();
41+
var parameters = methodSymbol.Parameters.Select(x => new ParameterTypeNamePair(x.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), x.Name));
4542
var index = (int)context.Attributes[0].NamedArguments.FirstOrDefault(x => x.Key.Equals("Index")).Value.Value!;
4643

47-
return new VTableFunctionInfo(fullyQualifiedParentTypeName, structNamespace, structName, functionName, returnTypeName, index, new(parameters));
44+
return new VTableFunctionInfo(fullyQualifiedParentTypeName, structNamespace, structName, functionName, returnTypeName, index, new(parameters.ToImmutableArray()));
4845
})
4946
.Where(static item => item is not null)
5047
.Collect()
5148
.Select((items, token) =>
5249
{
5350
token.ThrowIfCancellationRequested();
5451

55-
return items.GroupBy(source => source.FullyQualifiedParentTypeName, StringComparer.OrdinalIgnoreCase);
52+
return items.GroupBy(source => source.FullyQualifiedParentTypeName, StringComparer.OrdinalIgnoreCase).ToImmutableArray();
5653
});
5754

5855

5956
context.RegisterSourceOutput(sources, (context, sources) =>
6057
{
6158
foreach (var source in sources)
6259
{
63-
string vtableFunctionsCode = GenerateVtableFunctionsForStruct(source);
60+
string vtableFunctionsCode = GenerateVtableFunctionsForStruct(source.ToImmutableArray());
6461
context.AddSource($"{source.Key}_VTableFunctions.g.cs", vtableFunctionsCode);
6562
}
6663
});
6764
}
6865

69-
private string GenerateVtableFunctionsForStruct(IEnumerable<VTableFunctionInfo> sources)
66+
private string GenerateVtableFunctionsForStruct(ImmutableArray<VTableFunctionInfo> sources)
7067
{
7168
StringBuilder builder = new();
7269

@@ -91,14 +88,12 @@ private string GenerateVtableFunctionsForStruct(IEnumerable<VTableFunctionInfo>
9188

9289
foreach (var source in sources)
9390
{
94-
var parameters = source.Parameters.Cast<IParameterSymbol>().ToDictionary(x => x.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), x => x.Name);
95-
9691
builder.AppendLine($" [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]");
9792

98-
builder.AppendLine($" public partial {source.ReturnTypeName} {source.Name}({string.Join(", ", parameters.Select(x => $"{x.Key} {x.Value}"))})");
93+
builder.AppendLine($" public partial {source.ReturnTypeName} {source.Name}({string.Join(", ", source.Parameters.Select(x => $"{x.FullyQualifiedTypeName} {x.ValueName}"))})");
9994
builder.AppendLine($" {{");
100-
builder.AppendLine($" return ({source.ReturnTypeName})((delegate* unmanaged[MemberFunction]<{sources.ElementAt(0).ParentTypeName}*, {string.Join(", ", parameters.Select(x => $"{x.Key}"))}, int>)(lpVtbl[{source.Index}]))");
101-
builder.AppendLine($" (({sources.ElementAt(0).ParentTypeName}*)global::System.Runtime.CompilerServices.Unsafe.AsPointer(ref this), {string.Join(", ", parameters.Select(x => $"{x.Value}"))});");
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}"))});");
10297
builder.AppendLine($" }}");
10398

10499
if (sourceIndex < sourceCount - 1)

0 commit comments

Comments
 (0)