@@ -29,16 +29,14 @@ public void Initialize(GeneratorInitializationContext context)
29
29
30
30
public void Execute ( GeneratorExecutionContext context )
31
31
{
32
- MarshalBuilder marshalBuilder ;
33
-
34
32
if ( ! context . Compilation . ReferencedAssemblyNames . Any
35
33
( ai => ai . Name . Equals ( "Silk.NET.Core" , StringComparison . OrdinalIgnoreCase ) ) )
36
34
{
37
35
context . ReportDiagnostic ( Diagnostic . Create ( Diagnostics . SilkNetCoreMissing , Location . None ) ) ;
38
36
return ;
39
37
}
40
38
41
- if ( ! ( context . SyntaxReceiver is SyntaxReceiver receiver ) )
39
+ if ( context . SyntaxContextReceiver is not SyntaxReceiver receiver )
42
40
return ;
43
41
44
42
var nativeApiAttribute = context . Compilation . GetTypeByMetadataName
@@ -62,7 +60,7 @@ public void Execute(GeneratorExecutionContext context)
62
60
( string ) array [ 1 ] . Value ! /* first return is just the lib target */ , new PInvokeNativeContextOverride ( ) ) ;
63
61
64
62
65
- marshalBuilder = new MarshalBuilder ( ) ;
63
+ var marshalBuilder = new MarshalBuilder ( ) ;
66
64
67
65
// begin | end
68
66
marshalBuilder . Use ( Middlewares . InjectMiddleware ) ;
@@ -79,22 +77,23 @@ public void Execute(GeneratorExecutionContext context)
79
77
// pre load | post load
80
78
81
79
List < ITypeSymbol > processedSymbols = new List < ITypeSymbol > ( ) ;
82
-
83
80
84
- foreach ( var receiverClassDeclaration in receiver . ClassDeclarations )
81
+
82
+ foreach ( var group in receiver . ClassDeclarations . Select ( x => ( x . Item1 , x . Item2 , x . Item2 . GetDeclaredSymbol ( x . Item1 ) ) )
83
+ . GroupBy ( x => x . Item3 , SymbolEqualityComparer . Default ) )
85
84
{
86
85
try
87
86
{
88
87
var s = ProcessClassDeclaration
89
88
(
90
- receiverClassDeclaration , context , nativeApiAttribute , marshalBuilder , ref processedSymbols ,
91
- excludeFromOverrideAttribute
89
+ group . Select ( x => ( x . Item1 , x . Item2 ) ) , context , nativeApiAttribute , marshalBuilder , ref processedSymbols ,
90
+ excludeFromOverrideAttribute , ( INamedTypeSymbol ) group . Key
92
91
) ;
93
92
94
93
if ( s is null ) continue ;
95
94
96
95
var name =
97
- $ "{ receiverClassDeclaration . Identifier . Text } .{ receiverClassDeclaration . GetHashCode ( ) } .gen";
96
+ $ "{ group . Key . Name } .{ Guid . NewGuid ( ) } .gen";
98
97
context . AddSource ( name , SourceText . From ( s , Encoding . UTF8 ) ) ;
99
98
// File.WriteAllText(@"C:\SILK.NET\src\Lab\" + name, s);
100
99
}
@@ -103,48 +102,47 @@ public void Execute(GeneratorExecutionContext context)
103
102
context . ReportDiagnostic
104
103
(
105
104
Diagnostic . Create
106
- ( Diagnostics . ProcessClassFailure , receiverClassDeclaration . GetLocation ( ) , ex . ToString ( ) )
105
+ ( Diagnostics . ProcessClassFailure , group . First ( ) . Item1 . GetLocation ( ) , ex . ToString ( ) )
107
106
) ;
108
107
}
109
108
}
110
109
}
111
110
112
111
private string ProcessClassDeclaration
113
112
(
114
- ClassDeclarationSyntax classDeclaration ,
113
+ IEnumerable < ( ClassDeclarationSyntax , SemanticModel ) > classDeclarations ,
115
114
GeneratorExecutionContext sourceContext ,
116
115
INamedTypeSymbol nativeApiAttributeSymbol ,
117
116
MarshalBuilder rootMarshalBuilder ,
118
117
ref List < ITypeSymbol > processedSymbols ,
119
- INamedTypeSymbol excludeFromOverrideAttribute
118
+ INamedTypeSymbol excludeFromOverrideAttribute ,
119
+ INamedTypeSymbol sharedClassSymbol
120
120
)
121
121
{
122
122
var stopwatch = Stopwatch . StartNew ( ) ;
123
123
var compilation = sourceContext . Compilation ;
124
- if ( ! classDeclaration . Modifiers . Any ( x => x . IsKind ( SyntaxKind . PartialKeyword ) ) )
124
+ if ( ! classDeclarations . First ( ) . Item1 . Modifiers . Any ( x => x . IsKind ( SyntaxKind . PartialKeyword ) ) )
125
125
return null ;
126
126
127
- if ( ! classDeclaration . Parent . IsKind ( SyntaxKind . NamespaceDeclaration ) )
127
+ if ( ! classDeclarations . All ( x => x . Item1 . Parent . IsKind ( SyntaxKind . NamespaceDeclaration ) ) )
128
128
return null ;
129
- var namespaceDeclaration = ( NamespaceDeclarationSyntax ) classDeclaration . Parent ;
129
+
130
+ var namespaceDeclaration = ( NamespaceDeclarationSyntax ) classDeclarations . First ( ) . Item1 . Parent ;
130
131
131
132
if ( ! namespaceDeclaration . Parent . IsKind ( SyntaxKind . CompilationUnit ) )
132
133
return null ;
133
134
134
135
var compilationUnit = ( CompilationUnitSyntax ) namespaceDeclaration . Parent ;
135
136
136
- var classSymbol = ModelExtensions . GetDeclaredSymbol
137
- ( compilation . GetSemanticModel ( classDeclaration . SyntaxTree ) , classDeclaration ) as ITypeSymbol ;
138
-
139
137
if ( ! compilation . HasImplicitConversion
140
- ( classSymbol , compilation . GetTypeByMetadataName ( "Silk.NET.Core.Native.NativeApiContainer" ) ) )
138
+ ( sharedClassSymbol , compilation . GetTypeByMetadataName ( "Silk.NET.Core.Native.NativeApiContainer" ) ) )
141
139
return null ;
142
140
143
- var classIsSealed = classDeclaration . Modifiers . Any ( x => x . Text == "sealed" ) ;
141
+ var classIsSealed = classDeclarations . First ( ) . Item1 . Modifiers . Any ( x => x . Text == "sealed" ) ;
144
142
var generateSeal = false ;
145
143
146
144
if ( sourceContext . AnalyzerConfigOptions . GetOptions
147
- ( classDeclaration . SyntaxTree )
145
+ ( classDeclarations . First ( ) . Item1 . SyntaxTree )
148
146
. TryGetValue ( "silk_touch_sealed_vtable_creation" , out var generateSealstr ) )
149
147
{
150
148
if ( bool . TryParse ( generateSealstr , out var v ) )
@@ -155,7 +153,7 @@ INamedTypeSymbol excludeFromOverrideAttribute
155
153
var generateVTable = false ;
156
154
157
155
if ( sourceContext . AnalyzerConfigOptions . GetOptions
158
- ( classDeclaration . SyntaxTree )
156
+ ( classDeclarations . First ( ) . Item1 . SyntaxTree )
159
157
. TryGetValue ( "silk_touch_vtable_generate" , out var genvtablestr ) )
160
158
{
161
159
if ( bool . TryParse ( genvtablestr , out var v ) )
@@ -165,7 +163,7 @@ INamedTypeSymbol excludeFromOverrideAttribute
165
163
var preloadVTable = false ;
166
164
167
165
if ( sourceContext . AnalyzerConfigOptions . GetOptions
168
- ( classDeclaration . SyntaxTree )
166
+ ( classDeclarations . First ( ) . Item1 . SyntaxTree )
169
167
. TryGetValue ( "silk_touch_vtable_preload" , out var vtablepreloadstr ) )
170
168
{
171
169
if ( bool . TryParse ( vtablepreloadstr , out var v ) )
@@ -175,14 +173,14 @@ INamedTypeSymbol excludeFromOverrideAttribute
175
173
var emitAssert = false ;
176
174
177
175
if ( sourceContext . AnalyzerConfigOptions . GetOptions
178
- ( classDeclaration . SyntaxTree )
176
+ ( classDeclarations . First ( ) . Item1 . SyntaxTree )
179
177
. TryGetValue ( "silk_touch_vtable_tree_emit_assert" , out var emitAssertStr ) )
180
178
{
181
179
if ( bool . TryParse ( emitAssertStr , out var v ) )
182
180
emitAssert = v ;
183
181
}
184
182
185
- var classAttribute = classSymbol . GetAttributes ( )
183
+ var classAttribute = sharedClassSymbol . GetAttributes ( )
186
184
. FirstOrDefault ( x => SymbolEqualityComparer . Default . Equals ( x . AttributeClass , nativeApiAttributeSymbol ) ) ;
187
185
188
186
var classNativeApiAttribute = classAttribute == default
@@ -198,18 +196,18 @@ INamedTypeSymbol excludeFromOverrideAttribute
198
196
Dictionary < int , string > entryPoints = new Dictionary < int , string > ( ) ;
199
197
var processedEntrypoints = new List < EntryPoint > ( ) ;
200
198
foreach ( var ( declaration , symbol , entryPoint , callingConvention ) in from declaration in
201
- from member in classDeclaration . Members
202
- where member . IsKind ( SyntaxKind . MethodDeclaration )
203
- select ( MethodDeclarationSyntax ) member
204
- let symbol = compilation . GetSemanticModel ( declaration . SyntaxTree ) . GetDeclaredSymbol ( declaration )
199
+ from member in classDeclarations . SelectMany ( x => x . Item1 . Members . Select ( x2 => ( x2 , x . Item2 ) ) )
200
+ where member . x2 . IsKind ( SyntaxKind . MethodDeclaration )
201
+ select ( ( MethodDeclarationSyntax ) member . x2 , member . Item2 )
202
+ let symbol = declaration . Item2 . GetDeclaredSymbol ( declaration . Item1 )
205
203
where symbol is not null
206
204
let attribute = ToNativeApiAttribute
207
205
(
208
206
symbol . GetAttributes ( )
209
207
. FirstOrDefault
210
208
( att => SymbolEqualityComparer . Default . Equals ( att . AttributeClass , nativeApiAttributeSymbol ) )
211
209
)
212
- where declaration . Modifiers . Any
210
+ where declaration . Item1 . Modifiers . Any
213
211
( modifier => modifier . IsKind ( SyntaxKind . PartialKeyword ) ) && symbol . PartialImplementationPart is null
214
212
let entryPoint = NativeApiAttribute . GetEntryPoint ( attribute , classNativeApiAttribute , symbol . Name )
215
213
let callingConvention = NativeApiAttribute . GetCallingConvention ( attribute , classNativeApiAttribute )
@@ -220,14 +218,14 @@ where declaration.Modifiers.Any
220
218
ProcessMethod
221
219
(
222
220
sourceContext , rootMarshalBuilder , callingConvention , entryPoints , entryPoint , classIsSealed ,
223
- generateSeal , generateVTable , slot , compilation , symbol , declaration , newMembers ,
221
+ generateSeal , generateVTable , slot , compilation , symbol , declaration . Item1 , newMembers ,
224
222
ref gcCount , processedEntrypoints , generatedVTableName
225
223
) ;
226
224
}
227
225
228
226
if ( slotCount > 0 )
229
227
{
230
- if ( ! processedSymbols . Contains ( classSymbol ) )
228
+ if ( ! processedSymbols . Contains ( sharedClassSymbol ) )
231
229
{
232
230
newMembers . Add
233
231
(
@@ -283,7 +281,7 @@ where declaration.Modifiers.Any
283
281
) ;
284
282
}
285
283
286
- processedSymbols . Add ( classSymbol ) ;
284
+ processedSymbols . Add ( sharedClassSymbol ) ;
287
285
}
288
286
289
287
if ( newMembers . Count == 0 )
@@ -330,21 +328,21 @@ where declaration.Modifiers.Any
330
328
) ;
331
329
}
332
330
333
- ProcessNativeContextOverrides ( processedEntrypoints . ToArray ( ) , ref newMembers , classSymbol , classDeclaration , sourceContext . Compilation , excludeFromOverrideAttribute ) ;
334
-
331
+ ProcessNativeContextOverrides ( processedEntrypoints . ToArray ( ) , ref newMembers , sharedClassSymbol , excludeFromOverrideAttribute ) ;
332
+
335
333
var newNamespace = namespaceDeclaration . WithMembers
336
334
(
337
335
List
338
336
(
339
337
new MemberDeclarationSyntax [ ]
340
338
{
341
- classDeclaration . WithMembers
339
+ classDeclarations . First ( ) . Item1 . WithMembers
342
340
( List ( newMembers ) )
343
341
. WithAttributeLists ( List < AttributeListSyntax > ( ) )
344
342
}
345
343
)
346
344
)
347
- . WithUsings ( compilationUnit . Usings ) ;
345
+ . WithUsings ( compilationUnit . Usings . Add ( UsingDirective ( IdentifierName ( "Silk.NET.Core.Native" ) ) ) . Add ( UsingDirective ( IdentifierName ( "Silk.NET.Core.Contexts" ) ) ) ) ;
348
346
349
347
var result = newNamespace . NormalizeWhitespace ( ) . ToFullString ( ) ;
350
348
stopwatch . Stop ( ) ;
@@ -358,7 +356,7 @@ where declaration.Modifiers.Any
358
356
(
359
357
Diagnostic . Create
360
358
(
361
- Diagnostics . BuildInfo , classDeclaration . GetLocation ( ) , slotCount , gcCount ,
359
+ Diagnostics . BuildInfo , classDeclarations . First ( ) . Item1 . GetLocation ( ) , slotCount , gcCount ,
362
360
stopwatch . ElapsedMilliseconds + "ms"
363
361
)
364
362
) ;
0 commit comments