Skip to content

Commit a7ea817

Browse files
committed
Everything but ILogged
1 parent f91cbdf commit a7ea817

File tree

5 files changed

+131
-286
lines changed

5 files changed

+131
-286
lines changed

sourcegeneration/StereologueSourceGenerator/LoggableMember.cs

Lines changed: 23 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Immutable;
22
using Microsoft.CodeAnalysis;
3+
using Microsoft.CodeAnalysis.Diagnostics;
34

45
namespace Stereologue.SourceGenerator;
56

@@ -16,14 +17,7 @@ internal enum DeclarationType
1617
Logged,
1718
Struct,
1819
Protobuf,
19-
Boolean,
20-
Float,
21-
Double,
22-
Integer,
23-
String,
24-
Raw,
25-
Char,
26-
ULong,
20+
SpecialType,
2721
}
2822

2923
internal enum DeclarationKind
@@ -38,8 +32,10 @@ internal enum DeclarationKind
3832
NullableReferenceType
3933
}
4034

35+
internal record MemberDeclaration(DeclarationType LoggedType, SpecialType SpecialType, DeclarationKind LoggedKind, string? FQN);
36+
4137
// 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);
4339

4440
internal static class LoggableMemberExtensions
4541
{
@@ -87,24 +83,32 @@ private static DeclarationKind GetInnerType(this ITypeSymbol typeSymbol, out ITy
8783
return innerType.IsReferenceType ? DeclarationKind.NullableReferenceType : DeclarationKind.None;
8884
}
8985

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)
9187
{
9288
token.ThrowIfCancellationRequested();
9389

9490
var nestedKind = typeSymbol.GetInnerType(out typeSymbol);
9591

9692
token.ThrowIfCancellationRequested();
9793

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+
98102
// If we know we're generating a loggable implementation
99103
if (typeSymbol.GetAttributes().Where(x => x.AttributeClass?.ToDisplayString() == "Stereologue.GenerateLogAttribute").Any())
100104
{
101-
return (DeclarationType.Logged, nestedKind);
105+
return new(DeclarationType.Logged, SpecialType.None, nestedKind, null);
102106
}
103107
token.ThrowIfCancellationRequested();
104108
// If we know we already implement ILogged
105109
if (typeSymbol.AllInterfaces.Where(x => x.ToDisplayString() == "Stereologue.ILogged").Any())
106110
{
107-
return (DeclarationType.Logged, nestedKind);
111+
return new(DeclarationType.Logged, SpecialType.None, nestedKind, null);
108112
}
109113
token.ThrowIfCancellationRequested();
110114
// If we have an UpdateMonologue function
@@ -128,7 +132,7 @@ private static (DeclarationType, DeclarationKind)? GetDeclarationType(this IType
128132
}
129133
if (parameters[0].Type.SpecialType == SpecialType.System_String && parameters[1].Type.ToDisplayString() == "Stereologue.Stereologuer")
130134
{
131-
return (DeclarationType.Logged, nestedKind);
135+
return new(DeclarationType.Logged, SpecialType.None, nestedKind, null);
132136
}
133137
}
134138
}
@@ -149,55 +153,23 @@ private static (DeclarationType, DeclarationKind)? GetDeclarationType(this IType
149153
{
150154
if (!attributeInfo.UseProtobuf)
151155
{
152-
return (DeclarationType.Struct, nestedKind);
156+
return new(DeclarationType.Struct, SpecialType.None, nestedKind, typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat));
153157
}
154158
}
155159
else if (interfaceName == protobufName)
156160
{
157161
if (attributeInfo.UseProtobuf)
158162
{
159-
// TODO disallow arrays of protobuf types
160-
return (DeclarationType.Protobuf, nestedKind);
163+
return new(DeclarationType.Protobuf, SpecialType.None, nestedKind, typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat));
161164
}
162165
}
163166
}
164167

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);
198170
}
199171

200-
public static LoggableMember? ToLoggableMember(this ISymbol member, CancellationToken token, out DiagnosticInfo? diagnostic)
172+
public static LoggableMember? ToLoggableMember(this ISymbol member, CancellationToken token)
201173
{
202174
var attributes = member.GetAttributes();
203175
foreach (AttributeData attribute in attributes)
@@ -231,20 +203,17 @@ private static (DeclarationType, DeclarationKind)? GetDeclarationType(this IType
231203
{
232204
if (method.ReturnsVoid)
233205
{
234-
diagnostic = DiagnosticInfo.Create(GeneratorDiagnostics.LoggedMethodDoesntReturnVoid, null, [method.Name]);
235206
return null;
236207
}
237208
if (!method.Parameters.IsEmpty)
238209
{
239-
diagnostic = DiagnosticInfo.Create(GeneratorDiagnostics.LoggedMethodTakesArguments, null, [method.Name]);
240210
return null;
241211
}
242212
logType = method.ReturnType;
243213
memberType = MemberType.Method;
244214
}
245215
else
246216
{
247-
diagnostic = DiagnosticInfo.Create(GeneratorDiagnostics.LoggedMemberTypeNotSupported, null, [member.Name]);
248217
return null;
249218
}
250219
token.ThrowIfCancellationRequested();
@@ -253,15 +222,11 @@ private static (DeclarationType, DeclarationKind)? GetDeclarationType(this IType
253222
token.ThrowIfCancellationRequested();
254223
if (declType is null)
255224
{
256-
// TODO change this to unsupported type
257-
diagnostic = DiagnosticInfo.Create(GeneratorDiagnostics.GeneratedTypeIsInterface, null, null);
258225
return null;
259226
}
260227

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);
263229
}
264-
diagnostic = null;
265230
return null;
266231
}
267232
}

0 commit comments

Comments
 (0)