1
1
using System . Collections . Immutable ;
2
2
using Microsoft . CodeAnalysis ;
3
+ using Microsoft . CodeAnalysis . Diagnostics ;
3
4
4
5
namespace Stereologue . SourceGenerator ;
5
6
@@ -16,14 +17,7 @@ internal enum DeclarationType
16
17
Logged ,
17
18
Struct ,
18
19
Protobuf ,
19
- Boolean ,
20
- Float ,
21
- Double ,
22
- Integer ,
23
- String ,
24
- Raw ,
25
- Char ,
26
- ULong ,
20
+ SpecialType ,
27
21
}
28
22
29
23
internal enum DeclarationKind
@@ -38,8 +32,10 @@ internal enum DeclarationKind
38
32
NullableReferenceType
39
33
}
40
34
35
+ internal record MemberDeclaration ( DeclarationType LoggedType , SpecialType SpecialType , DeclarationKind LoggedKind , string ? FQN ) ;
36
+
41
37
// Contains all information about a loggable member
42
- internal record LoggableMember ( string Name , MemberType MemberType , DeclarationType LoggedType , DeclarationKind LoggedKind , LogAttributeInfo AttributeInfo ) ;
38
+ internal record LoggableMember ( string Name , MemberType MemberType , MemberDeclaration MemberDeclaration , LogAttributeInfo AttributeInfo ) ;
43
39
44
40
internal static class LoggableMemberExtensions
45
41
{
@@ -87,24 +83,32 @@ private static DeclarationKind GetInnerType(this ITypeSymbol typeSymbol, out ITy
87
83
return innerType . IsReferenceType ? DeclarationKind . NullableReferenceType : DeclarationKind . None ;
88
84
}
89
85
90
- private static ( DeclarationType , DeclarationKind ) ? GetDeclarationType ( this ITypeSymbol typeSymbol , LogAttributeInfo attributeInfo , CancellationToken token )
86
+ private static MemberDeclaration ? GetDeclarationType ( this ITypeSymbol typeSymbol , LogAttributeInfo attributeInfo , CancellationToken token )
91
87
{
92
88
token . ThrowIfCancellationRequested ( ) ;
93
89
94
90
var nestedKind = typeSymbol . GetInnerType ( out typeSymbol ) ;
95
91
96
92
token . ThrowIfCancellationRequested ( ) ;
97
93
94
+ // TODO support IntPtr and NIntPtr
95
+
96
+ if ( typeSymbol . SpecialType != SpecialType . None )
97
+ {
98
+ // We're a built in special type, no need to check for anything else
99
+ return new ( DeclarationType . SpecialType , typeSymbol . SpecialType , nestedKind , null ) ;
100
+ }
101
+
98
102
// If we know we're generating a loggable implementation
99
103
if ( typeSymbol . GetAttributes ( ) . Where ( x => x . AttributeClass ? . ToDisplayString ( ) == "Stereologue.GenerateLogAttribute" ) . Any ( ) )
100
104
{
101
- return ( DeclarationType . Logged , nestedKind ) ;
105
+ return new ( DeclarationType . Logged , SpecialType . None , nestedKind , null ) ;
102
106
}
103
107
token . ThrowIfCancellationRequested ( ) ;
104
108
// If we know we already implement ILogged
105
109
if ( typeSymbol . AllInterfaces . Where ( x => x . ToDisplayString ( ) == "Stereologue.ILogged" ) . Any ( ) )
106
110
{
107
- return ( DeclarationType . Logged , nestedKind ) ;
111
+ return new ( DeclarationType . Logged , SpecialType . None , nestedKind , null ) ;
108
112
}
109
113
token . ThrowIfCancellationRequested ( ) ;
110
114
// If we have an UpdateMonologue function
@@ -128,7 +132,7 @@ private static (DeclarationType, DeclarationKind)? GetDeclarationType(this IType
128
132
}
129
133
if ( parameters [ 0 ] . Type . SpecialType == SpecialType . System_String && parameters [ 1 ] . Type . ToDisplayString ( ) == "Stereologue.Stereologuer" )
130
134
{
131
- return ( DeclarationType . Logged , nestedKind ) ;
135
+ return new ( DeclarationType . Logged , SpecialType . None , nestedKind , null ) ;
132
136
}
133
137
}
134
138
}
@@ -149,55 +153,23 @@ private static (DeclarationType, DeclarationKind)? GetDeclarationType(this IType
149
153
{
150
154
if ( ! attributeInfo . UseProtobuf )
151
155
{
152
- return ( DeclarationType . Struct , nestedKind ) ;
156
+ return new ( DeclarationType . Struct , SpecialType . None , nestedKind , typeSymbol . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) ) ;
153
157
}
154
158
}
155
159
else if ( interfaceName == protobufName )
156
160
{
157
161
if ( attributeInfo . UseProtobuf )
158
162
{
159
- // TODO disallow arrays of protobuf types
160
- return ( DeclarationType . Protobuf , nestedKind ) ;
163
+ return new ( DeclarationType . Protobuf , SpecialType . None , nestedKind , typeSymbol . ToDisplayString ( SymbolDisplayFormat . FullyQualifiedFormat ) ) ;
161
164
}
162
165
}
163
166
}
164
167
165
- // Special case non nested and nullables
166
- if ( nestedKind == DeclarationKind . None || nestedKind == DeclarationKind . NullableReferenceType || nestedKind == DeclarationKind . NullableValueType )
167
- {
168
- return ( fullTypeName switch
169
- {
170
- "System.Single" => DeclarationType . Float ,
171
- "System.Double" => DeclarationType . Double ,
172
- "System.Byte" => DeclarationType . Integer ,
173
- "System.SByte" => DeclarationType . Integer ,
174
- "System.Int16" => DeclarationType . Integer ,
175
- "System.UInt16" => DeclarationType . Integer ,
176
- "System.Int32" => DeclarationType . Integer ,
177
- "System.UInt32" => DeclarationType . Integer ,
178
- "System.Int64" => DeclarationType . Integer ,
179
- "System.UInt64" => DeclarationType . ULong ,
180
- "System.Boolean" => DeclarationType . Boolean ,
181
- "System.Char" => DeclarationType . Char ,
182
- "System.String" => DeclarationType . String ,
183
- _ => DeclarationType . None
184
- } , nestedKind ) ;
185
- }
186
-
187
-
188
- return ( fullTypeName switch
189
- {
190
- "System.Single" => DeclarationType . Float ,
191
- "System.Double" => DeclarationType . Double ,
192
- "System.Byte" => DeclarationType . Raw ,
193
- "System.Int64" => DeclarationType . Integer ,
194
- "System.Boolean" => DeclarationType . Boolean ,
195
- "System.String" => DeclarationType . String ,
196
- _ => DeclarationType . None
197
- } , nestedKind ) ;
168
+ // We get here by attempting to log a type we have no clue about
169
+ return new ( DeclarationType . None , SpecialType . None , DeclarationKind . None , null ) ;
198
170
}
199
171
200
- public static LoggableMember ? ToLoggableMember ( this ISymbol member , CancellationToken token , out DiagnosticInfo ? diagnostic )
172
+ public static LoggableMember ? ToLoggableMember ( this ISymbol member , CancellationToken token )
201
173
{
202
174
var attributes = member . GetAttributes ( ) ;
203
175
foreach ( AttributeData attribute in attributes )
@@ -231,20 +203,17 @@ private static (DeclarationType, DeclarationKind)? GetDeclarationType(this IType
231
203
{
232
204
if ( method . ReturnsVoid )
233
205
{
234
- diagnostic = DiagnosticInfo . Create ( GeneratorDiagnostics . LoggedMethodDoesntReturnVoid , null , [ method. Name ] ) ;
235
206
return null ;
236
207
}
237
208
if ( ! method . Parameters . IsEmpty )
238
209
{
239
- diagnostic = DiagnosticInfo . Create ( GeneratorDiagnostics . LoggedMethodTakesArguments , null , [ method. Name ] ) ;
240
210
return null ;
241
211
}
242
212
logType = method . ReturnType ;
243
213
memberType = MemberType . Method ;
244
214
}
245
215
else
246
216
{
247
- diagnostic = DiagnosticInfo . Create ( GeneratorDiagnostics . LoggedMemberTypeNotSupported , null , [ member . Name ] ) ;
248
217
return null ;
249
218
}
250
219
token . ThrowIfCancellationRequested ( ) ;
@@ -253,15 +222,11 @@ private static (DeclarationType, DeclarationKind)? GetDeclarationType(this IType
253
222
token . ThrowIfCancellationRequested ( ) ;
254
223
if ( declType is null )
255
224
{
256
- // TODO change this to unsupported type
257
- diagnostic = DiagnosticInfo . Create ( GeneratorDiagnostics . GeneratedTypeIsInterface , null , null ) ;
258
225
return null ;
259
226
}
260
227
261
- diagnostic = null ;
262
- return new LoggableMember ( member . Name , memberType , declType . Value . Item1 , declType . Value . Item2 , attributeInfo ) ;
228
+ return new LoggableMember ( member . Name , memberType , declType , attributeInfo ) ;
263
229
}
264
- diagnostic = null ;
265
230
return null ;
266
231
}
267
232
}
0 commit comments