1
1
// Copyright (c) Files Community
2
2
// Licensed under the MIT License.
3
3
4
- using Microsoft . CodeAnalysis ;
5
- using System . Reflection ;
6
-
7
4
namespace Files . Core . SourceGenerator . Generators
8
5
{
9
6
[ Generator ( LanguageNames . CSharp ) ]
@@ -41,32 +38,32 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
41
38
var methodSymbol = ( IMethodSymbol ) context . TargetSymbol ;
42
39
var functionName = methodSymbol . Name ;
43
40
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 ) ) ;
45
42
var index = ( int ) context . Attributes [ 0 ] . NamedArguments . FirstOrDefault ( x => x . Key . Equals ( "Index" ) ) . Value . Value ! ;
46
43
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 ( ) ) ) ;
48
45
} )
49
46
. Where ( static item => item is not null )
50
47
. Collect ( )
51
48
. Select ( ( items , token ) =>
52
49
{
53
50
token . ThrowIfCancellationRequested ( ) ;
54
51
55
- return items . GroupBy ( source => source . FullyQualifiedParentTypeName , StringComparer . OrdinalIgnoreCase ) ;
52
+ return items . GroupBy ( source => source . FullyQualifiedParentTypeName , StringComparer . OrdinalIgnoreCase ) . ToImmutableArray ( ) ;
56
53
} ) ;
57
54
58
55
59
56
context . RegisterSourceOutput ( sources , ( context , sources ) =>
60
57
{
61
58
foreach ( var source in sources )
62
59
{
63
- string vtableFunctionsCode = GenerateVtableFunctionsForStruct ( source ) ;
60
+ string vtableFunctionsCode = GenerateVtableFunctionsForStruct ( source . ToImmutableArray ( ) ) ;
64
61
context . AddSource ( $ "{ source . Key } _VTableFunctions.g.cs", vtableFunctionsCode ) ;
65
62
}
66
63
} ) ;
67
64
}
68
65
69
- private string GenerateVtableFunctionsForStruct ( IEnumerable < VTableFunctionInfo > sources )
66
+ private string GenerateVtableFunctionsForStruct ( ImmutableArray < VTableFunctionInfo > sources )
70
67
{
71
68
StringBuilder builder = new ( ) ;
72
69
@@ -91,14 +88,12 @@ private string GenerateVtableFunctionsForStruct(IEnumerable<VTableFunctionInfo>
91
88
92
89
foreach ( var source in sources )
93
90
{
94
- var parameters = source . Parameters . Cast < IParameterSymbol > ( ) . ToDictionary ( x => x . Type . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) , x => x . Name ) ;
95
-
96
91
builder . AppendLine ( $ " [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]") ;
97
92
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 } ") ) } )") ;
99
94
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 } ") ) } );") ;
102
97
builder . AppendLine ( $ " }}") ;
103
98
104
99
if ( sourceIndex < sourceCount - 1 )
0 commit comments