1
1
using System . Collections . Generic ;
2
+ using System . Collections . Immutable ;
2
3
using System . Linq ;
3
4
using System . Numerics ;
4
5
using System . Text ;
@@ -19,54 +20,39 @@ public class UnionGenerator : IIncrementalGenerator
19
20
public void Initialize ( IncrementalGeneratorInitializationContext context )
20
21
{
21
22
var sources = context . SyntaxProvider . ForAttributeWithMetadataName (
22
- "Coplt.Union.UnionAttribute" ,
23
- static ( syntax , _ ) => syntax is StructDeclarationSyntax or ClassDeclarationSyntax ,
24
- static ( ctx , _ ) =>
25
- {
26
- var diagnostics = new List < Diagnostic > ( ) ;
27
- var attr = ctx . Attributes . First ( ) ;
28
- var union_attr = UnionAttr . FromData ( attr , diagnostics ) ;
29
- var syntax = ( TypeDeclarationSyntax ) ctx . TargetNode ;
30
- var semanticModel = ctx . SemanticModel ;
31
- var symbol = ( INamedTypeSymbol ) ctx . TargetSymbol ;
32
- var rawFullName = symbol . ToDisplayString ( ) ;
33
- var nameWraps = symbol . WrapNames ( ) ;
34
- var nameWrap = symbol . WrapName ( ) ;
35
- var readOnly = symbol . IsReadOnly ;
36
- var isClass = syntax is ClassDeclarationSyntax ;
37
- var hasToString = symbol . GetMembers ( "ToString" )
38
- . Where ( s => s is IMethodSymbol )
39
- . Cast < IMethodSymbol > ( )
40
- . Any ( s => s . TypeParameters . Length == 0 && s . Parameters . Length == 0 ) ;
41
- var NamedArguments = attr . NamedArguments . ToDictionary ( a => a . Key , a => a . Value ) ;
42
- var genToString = NamedArguments . TryGetValue ( "GenerateToString" , out var _GenerateToString )
43
- ? ( bool ) _GenerateToString . Value !
44
- : ! hasToString ;
45
- var genEquals = ! NamedArguments . TryGetValue ( "GenerateEquals" , out var _GenerateEquals ) ||
46
- ( bool ) _GenerateEquals . Value ! ;
47
- var genCompareTo = ! NamedArguments . TryGetValue ( "GenerateCompareTo" , out var _GenerateCompareTo ) ||
48
- ( bool ) _GenerateCompareTo . Value ! ;
49
- var genMethods = new UnionGenerateMethod ( genToString , genEquals , genCompareTo ) ;
50
- return ( syntax , semanticModel , union_attr , readOnly , isClass , genMethods ,
51
- rawFullName , nameWraps , nameWrap , AlwaysEq . Create ( diagnostics ) ) ;
52
- }
53
- )
54
- . Combine ( context . CompilationProvider )
55
- . Select ( static ( input , _ ) =>
23
+ "Coplt.Union.UnionAttribute" ,
24
+ static ( syntax , _ ) => syntax is StructDeclarationSyntax or ClassDeclarationSyntax ,
25
+ static ( ctx , _ ) =>
56
26
{
57
- var ( ( syntax , semanticModel , union_attr , readOnly , isClass , genMethods , rawFullName , nameWraps ,
58
- nameWrap , diagnostics ) , compilation ) =
59
- input ;
60
- var nullable = compilation . Options . NullableContextOptions ;
61
- var usings = new HashSet < string > ( ) ;
62
- Utils . GetUsings ( syntax , usings ) ;
63
- var genBase = new GenBase ( rawFullName , nullable , usings , nameWraps , nameWrap ) ;
64
- var templates = syntax . Members
27
+ var diagnostics = new List < Diagnostic > ( ) ;
28
+ var compilation = ctx . SemanticModel . Compilation ;
29
+ var attr = ctx . Attributes . First ( ) ;
30
+ var union_attr = UnionAttr . FromData ( attr , diagnostics ) ;
31
+ var syntax = ( TypeDeclarationSyntax ) ctx . TargetNode ;
32
+ var semantic_model = ctx . SemanticModel ;
33
+ var symbol = ( INamedTypeSymbol ) ctx . TargetSymbol ;
34
+ var GenBase = Utils . BuildGenBase ( syntax , symbol , compilation ) ;
35
+ var IsReadOnly = symbol . IsReadOnly ;
36
+ var IsClass = syntax is ClassDeclarationSyntax ;
37
+ var HasToString = symbol . GetMembers ( "ToString" )
38
+ . Where ( s => s is IMethodSymbol )
39
+ . Cast < IMethodSymbol > ( )
40
+ . Any ( s => s . TypeParameters . Length == 0 && s . Parameters . Length == 0 ) ;
41
+ var NamedArguments = attr . NamedArguments . ToDictionary ( a => a . Key , a => a . Value ) ;
42
+ var GenToString = NamedArguments . TryGetValue ( "GenerateToString" , out var _GenerateToString )
43
+ ? ( bool ) _GenerateToString . Value !
44
+ : ! HasToString ;
45
+ var GenEquals = ! NamedArguments . TryGetValue ( "GenerateEquals" , out var _GenerateEquals ) ||
46
+ ( bool ) _GenerateEquals . Value ! ;
47
+ var GenCompareTo = ! NamedArguments . TryGetValue ( "GenerateCompareTo" , out var _GenerateCompareTo ) ||
48
+ ( bool ) _GenerateCompareTo . Value ! ;
49
+ var GenMethods = new UnionGenerateMethod ( GenToString , GenEquals , GenCompareTo ) ;
50
+ var Templates = syntax . Members
65
51
. Where ( static t => t is InterfaceDeclarationSyntax )
66
52
. Cast < InterfaceDeclarationSyntax > ( )
67
53
. Select ( t =>
68
54
{
69
- var symbol = semanticModel . GetDeclaredSymbol ( t ) ;
55
+ var symbol = semantic_model . GetDeclaredSymbol ( t ) ;
70
56
return ( t , symbol ) ;
71
57
} )
72
58
. Where ( i =>
@@ -75,18 +61,18 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
75
61
return symbol ! . GetAttributes ( ) . Any ( a =>
76
62
a . AttributeClass ? . ToDisplayString ( ) == "Coplt.Union.UnionTemplateAttribute" ) ;
77
63
} )
78
- . ToArray ( ) ;
79
- if ( templates . Length > 1 )
64
+ . ToList ( ) ;
65
+ if ( Templates . Count > 1 )
80
66
{
81
67
var desc = Utils . MakeWarning ( Id , Strings . Get ( "Generator.Union.Error.MultiTemplate" ) ) ;
82
- foreach ( var t in templates )
68
+ foreach ( var t in Templates )
83
69
{
84
- diagnostics . Value . Add ( Diagnostic . Create ( desc , t . t . Identifier . GetLocation ( ) ) ) ;
70
+ diagnostics . Add ( Diagnostic . Create ( desc , t . t . Identifier . GetLocation ( ) ) ) ;
85
71
}
86
72
}
87
- var cases = new List < UnionCase > ( ) ;
88
- var any_generic = false ;
89
- foreach ( var ( template , _) in templates )
73
+ var cases = ImmutableArray . CreateBuilder < UnionCase > ( ) ;
74
+ var AnyGeneric = false ;
75
+ foreach ( var ( template , _) in Templates )
90
76
{
91
77
foreach ( var ( member , i ) in template . Members . Select ( ( a , b ) => ( a , b ) ) )
92
78
{
@@ -95,7 +81,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
95
81
var case_name = mds . Identifier . ToString ( ) ;
96
82
var ret_type = mds . ReturnType . ToString ( ) ;
97
83
var kind = UnionCaseTypeKind . None ;
98
- var member_symbol = ( IMethodSymbol ) semanticModel . GetDeclaredSymbol ( mds ) ! ;
84
+ var member_symbol = ( IMethodSymbol ) semantic_model . GetDeclaredSymbol ( mds ) ! ;
99
85
var tag_attr = member_symbol . GetAttributes ( ) . FirstOrDefault ( a =>
100
86
a . AttributeClass ? . ToDisplayString ( ) == "Coplt.Union.UnionTagAttribute" ) ;
101
87
string ? tag = null ;
@@ -109,7 +95,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
109
95
}
110
96
var ret_type_symbol = member_symbol . ReturnType ;
111
97
var is_generic = ret_type_symbol . IsNotInstGenericType ( ) ;
112
- if ( is_generic ) any_generic = true ;
98
+ if ( is_generic ) AnyGeneric = true ;
113
99
if ( ret_type_symbol . IsUnmanagedType ) kind = UnionCaseTypeKind . Unmanaged ;
114
100
else if ( ret_type_symbol . IsReferenceType ) kind = UnionCaseTypeKind . Class ;
115
101
if ( is_generic )
@@ -152,7 +138,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
152
138
{
153
139
var desc = Utils . MakeInfo ( Id ,
154
140
Strings . Get ( "Generator.Union.Info.PossiblyInvalidSymbol" ) ) ;
155
- diagnostics . Value . Add ( Diagnostic . Create ( desc , member . GetLocation ( ) ) ) ;
141
+ diagnostics . Add ( Diagnostic . Create ( desc , member . GetLocation ( ) ) ) ;
156
142
}
157
143
}
158
144
cases . Add ( new UnionCase ( case_name , tag , ret_type , kind , is_generic ) ) ;
@@ -163,37 +149,41 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
163
149
Strings . Get ( "Generator.Union.Error.IllegalTemplateMember" ) ) ;
164
150
if ( member is BaseTypeDeclarationSyntax bts )
165
151
{
166
- diagnostics . Value . Add ( Diagnostic . Create ( desc , bts . Identifier . GetLocation ( ) ) ) ;
152
+ diagnostics . Add ( Diagnostic . Create ( desc , bts . Identifier . GetLocation ( ) ) ) ;
167
153
}
168
154
else
169
155
{
170
- diagnostics . Value . Add ( Diagnostic . Create ( desc , member . GetLocation ( ) ) ) ;
156
+ diagnostics . Add ( Diagnostic . Create ( desc , member . GetLocation ( ) ) ) ;
171
157
}
172
158
}
173
159
}
174
160
}
175
- var name = syntax . Identifier . ToString ( ) ;
176
- return ( name , union_attr , readOnly , isClass , cases , any_generic , genMethods , genBase , diagnostics ) ;
177
- } ) ;
161
+ var Name = syntax . Identifier . ToString ( ) ;
162
+ return (
163
+ Name , UnionAttr : union_attr , IsReadOnly , isClass : IsClass ,
164
+ cases . ToImmutableArray ( ) , AnyGeneric , GenMethods , GenBase ,
165
+ AlwaysEq . Create ( diagnostics )
166
+ ) ;
167
+ }
168
+ ) ;
178
169
179
170
context . RegisterSourceOutput ( sources , static ( ctx , input ) =>
180
171
{
181
- var ( name , union_attr , readOnly , isClass , cases , any_generic , genMethods , genBase , diagnostics ) = input ;
182
- if ( diagnostics . Value . Count > 0 )
172
+ var ( Name , UnionAttr , IsReadOnly , IsClass , Cases , AnyGeneric , GenMethods , GenBase , Diagnostics ) = input ;
173
+ if ( Diagnostics . Value . Count > 0 )
183
174
{
184
- foreach ( var diagnostic in diagnostics . Value )
175
+ foreach ( var diagnostic in Diagnostics . Value )
185
176
{
186
177
ctx . ReportDiagnostic ( diagnostic ) ;
187
178
}
188
179
}
189
180
var code = new TemplateStructUnion (
190
- genBase , name , union_attr , readOnly , isClass , cases , any_generic , genMethods
191
- )
192
- . Gen ( ) ;
193
- var sourceText = SourceText . From ( code , Encoding . UTF8 ) ;
194
- var rawSourceFileName = genBase . FileFullName ;
195
- var sourceFileName = $ "{ rawSourceFileName } .union.g.cs";
196
- ctx . AddSource ( sourceFileName , sourceText ) ;
181
+ GenBase , Name , UnionAttr , IsReadOnly , IsClass , Cases , AnyGeneric , GenMethods
182
+ ) . Gen ( ) ;
183
+ var source_text = SourceText . From ( code , Encoding . UTF8 ) ;
184
+ var raw_source_file_name = GenBase . FileFullName ;
185
+ var sourceFileName = $ "{ raw_source_file_name } .union.g.cs";
186
+ ctx . AddSource ( sourceFileName , source_text ) ;
197
187
} ) ;
198
188
}
199
189
}
0 commit comments