11// Copyright (c) Files Community
22// Licensed under the MIT License.
33
4- using Microsoft . CodeAnalysis ;
5- using System . Reflection ;
6-
74namespace 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