-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathStrGenerator.cs
281 lines (243 loc) · 10.8 KB
/
StrGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Models;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace StackXML.Generator
{
[Generator]
public class StrGenerator : IIncrementalGenerator
{
private record ClassGenInfo
{
public required HierarchyInfo m_hierarchy;
public EquatableArray<FieldGenInfo> m_fields;
}
private record FieldGenInfo
{
public readonly HierarchyInfo m_ownerHierarchy;
public readonly int? m_group;
public readonly string? m_defaultValue;
public readonly string m_fieldName;
public readonly string m_typeShortName;
public readonly string m_typeQualifiedName;
public readonly bool m_isNullable;
public readonly bool m_isStrBody;
public FieldGenInfo(IFieldSymbol fieldSymbol, VariableDeclaratorSyntax variableDeclaratorSyntax)
{
m_ownerHierarchy = HierarchyInfo.From(fieldSymbol.ContainingType);
m_defaultValue = variableDeclaratorSyntax.Initializer?.Value.ToString();
m_fieldName = fieldSymbol.Name;
var type = (INamedTypeSymbol)fieldSymbol.Type;
m_isNullable = ExtractNullable(ref type);
m_typeShortName = type.Name;
m_typeQualifiedName = type.GetFullyQualifiedName();
if (fieldSymbol.TryGetAttributeWithFullyQualifiedMetadataName("StackXML.Str.StrOptionalAttribute", out var optionalAttribute))
{
m_group = (int)optionalAttribute.ConstructorArguments[0].Value!;
}
foreach (var member in type.GetMembers())
{
if (member is not IFieldSymbol childFieldSymbol)
{
continue;
}
if (!childFieldSymbol.TryGetAttributeWithFullyQualifiedMetadataName("StackXML.Str.StrFieldAttribute", out _))
{
continue;
}
m_isStrBody = true;
break;
}
}
}
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var fieldDeclarations = context.SyntaxProvider.ForAttributeWithMetadataName(
"StackXML.Str.StrFieldAttribute",
(syntaxNode, _) => syntaxNode is VariableDeclaratorSyntax { Parent: VariableDeclarationSyntax { Parent: FieldDeclarationSyntax { Parent: TypeDeclarationSyntax, AttributeLists.Count: > 0 } } },
TransformField);
// group by containing type
var typeDeclarations = fieldDeclarations.GroupBy(static x => x.m_ownerHierarchy, static x => x).Select((x, token) => new ClassGenInfo
{
m_hierarchy = x.Key,
m_fields = x.Right
});
context.RegisterSourceOutput(typeDeclarations, Process);
}
private static FieldGenInfo TransformField(GeneratorAttributeSyntaxContext context, CancellationToken token)
{
return new FieldGenInfo((IFieldSymbol)context.TargetSymbol, (VariableDeclaratorSyntax)context.TargetNode);
}
private static void Process(SourceProductionContext productionContext, ClassGenInfo classGenInfo)
{
using var w = new IndentedTextWriter();
w.WriteLine("using StackXML;");
w.WriteLine("using StackXML.Str;");
w.WriteLine();
classGenInfo.m_hierarchy.WriteSyntax(classGenInfo, w, ["IStrClass"], [ProcessClass]);
productionContext.AddSource($"{classGenInfo.m_hierarchy.FullyQualifiedMetadataName}.cs", w.ToString());
}
private static void ProcessClass(ClassGenInfo classGenInfo, IndentedTextWriter writer)
{
WriteDeserializeMethod(classGenInfo, writer);
writer.WriteLine();
WriteSerializeMethod(classGenInfo, writer);
}
private static void WriteDeserializeMethod(ClassGenInfo classGenInfo, IndentedTextWriter writer)
{
writer.WriteLine("public void Deserialize(ref StrReader reader)");
writer.WriteLine("{");
writer.IncreaseIndent();
HashSet<int> groupsStarted = new HashSet<int>();
int? currentGroup = null;
foreach (var field in classGenInfo.m_fields)
{
if (currentGroup != field.m_group)
{
if (currentGroup != null)
{
writer.DecreaseIndent();
writer.WriteLine("}");
}
if (field.m_group != null)
{
const string c_conditionName = "read";
if (groupsStarted.Add(field.m_group.Value))
{
writer.WriteLine($"var {c_conditionName}{field.m_group.Value} = reader.HasRemaining();");
}
writer.WriteLine($"if ({c_conditionName}{field.m_group.Value})");
writer.WriteLine("{");
writer.IncreaseIndent();
}
currentGroup = field.m_group;
}
if (field.m_isStrBody)
{
writer.WriteLine($"{field.m_fieldName} = new {field.m_typeQualifiedName}();");
writer.WriteLine($"{field.m_fieldName}.Deserialize(ref reader);");
} else
{
var reader = GetReaderForType(field.m_typeShortName, field.m_typeQualifiedName);
writer.WriteLine($"{field.m_fieldName} = {reader};");
}
}
if (currentGroup != null)
{
writer.DecreaseIndent();
writer.WriteLine("}");
}
writer.DecreaseIndent();
writer.WriteLine("}");
}
private static void WriteSerializeMethod(ClassGenInfo classGenInfo, IndentedTextWriter writer)
{
writer.WriteLine("public void Serialize(ref StrWriter writer)");
writer.WriteLine("{");
writer.IncreaseIndent();
{
HashSet<int> allGroups = new HashSet<int>();
foreach (var field in classGenInfo.m_fields)
{
if (field.m_group != null) allGroups.Add(field.m_group.Value);
}
const string c_conditionName = "doGroup";
HashSet<int> setupGroups = new HashSet<int>();
List<string> groupConditions = new List<string>();
foreach (var field in classGenInfo.m_fields)
{
if (field.m_group != null && setupGroups.Add(field.m_group.Value))
{
List<string> boolOrs = new List<string>();
foreach (var existingGroup in allGroups)
{
if (existingGroup <= field.m_group) continue;
boolOrs.Add($"{c_conditionName}{existingGroup}");
}
if (field.m_defaultValue != null)
{
boolOrs.Add($"{field.m_fieldName} != {field.m_defaultValue}");
} else
{
boolOrs.Add($"{field.m_fieldName} != default");
}
groupConditions.Add($"bool {c_conditionName}{field.m_group} = {string.Join(" || ", boolOrs)};");
}
}
groupConditions.Reverse();
foreach (var condition in groupConditions)
{
writer.WriteLine(condition);
}
int? currentGroup = null;
foreach (var field in classGenInfo.m_fields)
{
if (currentGroup != field.m_group)
{
if (currentGroup != null)
{
writer.DecreaseIndent();
writer.WriteLine("}");
}
if (field.m_group != null)
{
writer.WriteLine($"if ({c_conditionName}{field.m_group.Value})");
writer.WriteLine("{");
writer.IncreaseIndent();
}
currentGroup = field.m_group;
}
var toWrite = field.m_fieldName;
if (field.m_isNullable)
{
toWrite += ".Value";
}
if (field.m_isStrBody)
{
writer.WriteLine($"{toWrite}.Serialize(ref writer);");
} else
{
var writerFunc = GetWriterForType(field.m_fieldName, toWrite);
writer.WriteLine($"{writerFunc};");
}
}
if (currentGroup != null)
{
writer.DecreaseIndent();
writer.WriteLine("}");
}
}
writer.DecreaseIndent();
writer.WriteLine("}");
}
private static bool ExtractNullable(ref INamedTypeSymbol type)
{
if (type.Name != "Nullable") return false;
type = (INamedTypeSymbol)type.TypeArguments[0];
return true;
}
public static string GetWriterForType(string type, string toWrite)
{
var result = type switch
{
_ => $"writer.Put({toWrite})"
};
return result;
}
public static string GetReaderForType(string shortName, string qualifiedName)
{
var result = shortName switch
{
"String" => "reader.GetString().ToString()",
"ReadOnlySpan" => "reader.GetString()", // todo: ReadOnlySpan<char> only...
"SpanStr" => "reader.GetSpanString()",
_ => $"reader.Get<{qualifiedName}>()"
};
return result;
}
}
}