Skip to content

Commit de611c0

Browse files
committed
Improved func pointer cast, return type gen, etc
1 parent 1532364 commit de611c0

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ internal record VTableFunctionInfo(
77
string FullyQualifiedParentTypeName,
88
string ParentTypeNamespace,
99
string ParentTypeName,
10+
bool IsReturnTypeVoid,
1011
string Name,
1112
string ReturnTypeName,
1213
int Index,

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
3636
var structNamespace = context.TargetSymbol.ContainingType.ContainingNamespace.ToString();
3737
var structName = context.TargetSymbol.ContainingType.Name;
3838
var methodSymbol = (IMethodSymbol)context.TargetSymbol;
39+
var isReturnTypeVoid = methodSymbol.ReturnsVoid;
3940
var functionName = methodSymbol.Name;
4041
var returnTypeName = methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
4142
var parameters = methodSymbol.Parameters.Select(x => new ParameterTypeNamePair(x.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), x.Name));
4243
var index = (int)context.Attributes[0].NamedArguments.FirstOrDefault(x => x.Key.Equals("Index")).Value.Value!;
4344

44-
return new VTableFunctionInfo(fullyQualifiedParentTypeName, structNamespace, structName, functionName, returnTypeName, index, new(parameters.ToImmutableArray()));
45+
return new VTableFunctionInfo(fullyQualifiedParentTypeName, structNamespace, structName, isReturnTypeVoid, functionName, returnTypeName, index, new(parameters.ToImmutableArray()));
4546
})
4647
.Where(static item => item is not null)
4748
.Collect()
@@ -88,12 +89,14 @@ private string GenerateVtableFunctionsForStruct(ImmutableArray<VTableFunctionInf
8889

8990
foreach (var source in sources)
9091
{
92+
var returnTypeName = source.IsReturnTypeVoid ? "void" : "int";
93+
9194
builder.AppendLine($" [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]");
9295

9396
builder.AppendLine($" public partial {source.ReturnTypeName} {source.Name}({string.Join(", ", source.Parameters.Select(x => $"{x.FullyQualifiedTypeName} {x.ValueName}"))})");
9497
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}"))});");
98+
builder.AppendLine($" return ({source.ReturnTypeName})((delegate* unmanaged[MemberFunction]<{sources.ElementAt(0).FullyQualifiedParentTypeName}*, {string.Join(", ", source.Parameters.Select(x => $"{x.FullyQualifiedTypeName}"))}, {returnTypeName}>)(lpVtbl[{source.Index}]))");
99+
builder.AppendLine($" (({sources.ElementAt(0).FullyQualifiedParentTypeName}*)global::System.Runtime.CompilerServices.Unsafe.AsPointer(ref this), {string.Join(", ", source.Parameters.Select(x => $"{x.ValueName}"))});");
97100
builder.AppendLine($" }}");
98101

99102
if (sourceIndex < sourceCount - 1)

0 commit comments

Comments
 (0)