-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathXmlGenerator.cs
545 lines (471 loc) · 22.4 KB
/
XmlGenerator.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
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 XmlGenerator : IIncrementalGenerator
{
private record ClassGenInfo
{
public required string m_shortName;
public required HierarchyInfo m_hierarchy;
public required bool m_hasBaseType;
public required bool m_isValueType;
public required string? m_className;
public EquatableArray<FieldGenInfo> m_fields;
public EquatableArray<FieldGenInfo> m_bodies;
public string m_overrideSpecifier
{
get
{
if (m_isValueType) return "";
return m_hasBaseType ? " override" : " virtual";
}
}
}
private record FieldGenInfo
{
public required string m_fieldName;
public required string? m_xmlName;
public required string m_shortTypeName;
public required string m_qualifiedTypeName;
public required bool m_isPrimitive;
public required bool m_isValueType;
public required bool m_isList;
public required string? m_elementTypeShortName;
public required string? m_elementTypeQualifiedName;
public char? m_splitChar;
public bool IsList() => m_isList;
public bool IsString() => m_shortTypeName == "String";
public bool IsSpan() => m_shortTypeName == "ReadOnlySpan";
public bool IsPrimitive() => m_isPrimitive;
}
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var typeDeclarations = context.SyntaxProvider.ForAttributeWithMetadataName(
"StackXML.XmlClsAttribute",
(syntaxNode, _) => syntaxNode is TypeDeclarationSyntax { AttributeLists.Count: > 0 }, TransformClass);
context.RegisterSourceOutput(typeDeclarations, Process);
}
private static ClassGenInfo TransformClass(GeneratorAttributeSyntaxContext syntaxContext, CancellationToken token)
{
var typeSymbol = (INamedTypeSymbol)syntaxContext.TargetSymbol;
var classInfo = new ClassGenInfo
{
m_shortName = typeSymbol.Name,
m_hierarchy = HierarchyInfo.From(typeSymbol),
m_className = syntaxContext.Attributes[0].ConstructorArguments[0].Value?.ToString(),
m_hasBaseType = typeSymbol.BaseType != null &&
typeSymbol.BaseType.GetFullyQualifiedMetadataName() != "System.Object" &&
typeSymbol.BaseType.GetFullyQualifiedMetadataName() != "System.ValueType",
m_isValueType = typeSymbol.IsValueType
};
var bodies = new List<FieldGenInfo>();
var fields = new List<FieldGenInfo>();
foreach (var member in typeSymbol.GetMembers())
{
if (member is not IFieldSymbol fieldSymbol)
{
continue;
}
var isField = member.TryGetAttributeWithFullyQualifiedMetadataName("StackXML.XmlFieldAttribute", out var xmlFieldAttribute);
var isBody = member.TryGetAttributeWithFullyQualifiedMetadataName("StackXML.XmlBodyAttribute", out var xmlBodyAttribute);
if (!isField && !isBody)
{
continue;
}
var isList = fieldSymbol.Type.GetFullyQualifiedMetadataName() == "System.Collections.Generic.List`1";
var elementType = fieldSymbol.Type;
if (isList)
{
elementType = ((INamedTypeSymbol)fieldSymbol.Type).TypeArguments[0];
}
var interfaceSymbol = syntaxContext.SemanticModel.Compilation.GetTypeByMetadataName("StackXML.IXmlSerializable")!;;
var isClassBody =
elementType.TryGetAttributeWithFullyQualifiedMetadataName("StackXML.XmlClsAttribute", out var innerClsAttribute) ||
elementType.HasInterfaceWithType(interfaceSymbol);
string? xmlName;
if (isField)
{
xmlName = xmlFieldAttribute!.ConstructorArguments[0].Value!.ToString();
}
else
{
// body
xmlName = xmlBodyAttribute!.ConstructorArguments[0].Value?.ToString();
if (xmlName == null && innerClsAttribute != null)
{
// todo: does this break incremental?
xmlName = innerClsAttribute.ConstructorArguments[0].Value?.ToString();
}
}
var memberInfo = new FieldGenInfo
{
m_fieldName = fieldSymbol.Name,
m_xmlName = xmlName,
m_shortTypeName = fieldSymbol.Type.Name,
m_qualifiedTypeName = fieldSymbol.Type.GetFullyQualifiedName(),
m_elementTypeShortName = elementType.Name,
m_elementTypeQualifiedName = elementType.GetFullyQualifiedName(),
m_isPrimitive = !isClassBody,
m_isValueType = elementType.IsValueType,
m_isList = isList
};
if (member.TryGetAttributeWithFullyQualifiedMetadataName("StackXML.XmlSplitStrAttribute", out var xmlSplitStrAttribute))
{
memberInfo.m_splitChar = (char)xmlSplitStrAttribute.ConstructorArguments[0].Value!;
}
if (isField)
{
fields.Add(memberInfo);
}
else
{
bodies.Add(memberInfo);
}
}
classInfo.m_fields = fields.ToImmutableArray().AsEquatableArray();
classInfo.m_bodies = bodies.ToImmutableArray().AsEquatableArray();
return classInfo;
}
private static void Process(SourceProductionContext productionContext, ClassGenInfo classGenInfo)
{
using var w = new IndentedTextWriter();
w.WriteLine("using System;");
w.WriteLine("using System.IO;");
w.WriteLine("using System.Collections.Generic;");
w.WriteLine("using StackXML;");
w.WriteLine("using StackXML.Str;");
w.WriteLine();
string[] baseTypes = [];
if (!classGenInfo.m_hasBaseType)
{
baseTypes = ["IXmlSerializable"];
}
var callbacks = new List<IndentedTextWriter.Callback<ClassGenInfo>>();
if (classGenInfo.m_className != null)
{
callbacks.Add(Process_GetClassName);
}
callbacks.Add(Process_Fields);
callbacks.Add(Process_Bodies);
classGenInfo.m_hierarchy.WriteSyntax(classGenInfo, w, baseTypes, callbacks.ToArray());
productionContext.AddSource($"{classGenInfo.m_hierarchy.FullyQualifiedMetadataName}.cs", w.ToString());
}
private static void Process_GetClassName(ClassGenInfo classGenInfo, IndentedTextWriter writer)
{
writer.WriteLine($"public{classGenInfo.m_overrideSpecifier} ReadOnlySpan<char> GetNodeName()");
writer.WriteLine("{");
writer.IncreaseIndent();
writer.WriteLine($"return \"{classGenInfo.m_className}\";");
writer.DecreaseIndent();
writer.WriteLine("}");
}
private static void Process_Fields(ClassGenInfo classGenInfo, IndentedTextWriter writer)
{
WriteAttrParseMethod(classGenInfo, writer);
writer.WriteLine();
WriteAttSerializeMethod(classGenInfo, writer);
}
private static void Process_Bodies(ClassGenInfo classGenInfo, IndentedTextWriter writer)
{
WriteParseBodyMethods(classGenInfo, writer);
writer.WriteLine();
WriteSerializeBodyMethod(classGenInfo, writer);
}
private static void WriteAttrParseMethod(ClassGenInfo classGenInfo, IndentedTextWriter writer)
{
writer.WriteLine($"public{classGenInfo.m_overrideSpecifier} bool ParseAttribute(ref XmlReadBuffer buffer, ReadOnlySpan<char> name, ReadOnlySpan<char> value)");
writer.WriteLine("{");
writer.IncreaseIndent();
if (classGenInfo.m_hasBaseType)
{
writer.WriteLine("if (base.ParseAttribute(ref buffer, name, value)) return true;");
}
writer.WriteLine("switch (name)");
writer.WriteLine("{");
writer.IncreaseIndent();
foreach (var field in classGenInfo.m_fields.OrderBy(x => x.m_xmlName!.Length))
{
writer.WriteLine($"case \"{field.m_xmlName}\": {{");
writer.IncreaseIndent();
if (field.m_splitChar != null)
{
writer.WriteLine($"var lst = new {field.m_qualifiedTypeName}();");
writer.WriteLine($"var reader = new StrReader(value, '{field.m_splitChar}', buffer.m_params.m_stringParser);");
var readerMethod = StrGenerator.GetReaderForType(field.m_elementTypeShortName!, field.m_elementTypeQualifiedName!);
writer.WriteLine("while (reader.HasRemaining())");
writer.WriteLine("{");
writer.IncreaseIndent();
writer.WriteLine($"lst.Add({readerMethod});");
writer.DecreaseIndent();
writer.WriteLine("}");
writer.WriteLine($"this.{field.m_fieldName} = lst;");
} else
{
var readCommand = GetParseAttributeAction(field);
writer.WriteLine($"this.{field.m_fieldName} = {readCommand};");
}
writer.WriteLine("return true;");
writer.DecreaseIndent();
writer.WriteLine("}");
}
writer.DecreaseIndent();
writer.WriteLine("}");
writer.WriteLine("return false;");
writer.DecreaseIndent();
writer.WriteLine("}");
}
private static void WriteAttSerializeMethod(ClassGenInfo classGenInfo, IndentedTextWriter writer)
{
writer.WriteLine($"public{classGenInfo.m_overrideSpecifier} void SerializeAttributes(ref XmlWriteBuffer buffer)");
writer.WriteLine("{");
writer.IncreaseIndent();
if (classGenInfo.m_hasBaseType)
{
writer.WriteLine("base.SerializeAttributes(ref buffer);");
}
foreach (var field in classGenInfo.m_fields)
{
if (field.m_splitChar != null)
{
writer.WriteLine("{");
writer.IncreaseIndent();
writer.WriteLine($"using var writer = new StrWriter('{field.m_splitChar}', buffer.m_params.m_stringFormatter);");
writer.WriteLine($"foreach (var val in {field.m_fieldName})");
writer.WriteLine("{");
writer.IncreaseIndent();
writer.WriteLine($"{StrGenerator.GetWriterForType(field.m_elementTypeShortName!, "val")};");
writer.DecreaseIndent();
writer.WriteLine("}");
writer.WriteLine($"buffer.PutAttribute(\"{field.m_xmlName}\", writer.m_builtSpan);");
writer.WriteLine("writer.Dispose();");
writer.DecreaseIndent();
writer.WriteLine("}");
} else
{
var writerAction = GetPutAttributeAction(field);
writer.WriteLine(writerAction);
}
}
writer.DecreaseIndent();
writer.WriteLine("}");
}
private static string GetPutAttributeAction(FieldGenInfo field)
{
var writerAction = field.m_elementTypeShortName switch
{
_ => $"buffer.PutAttribute(\"{field.m_xmlName}\", {field.m_fieldName});"
};
return writerAction;
}
private static string GetParseAttributeAction(FieldGenInfo field)
{
var readCommand = field.m_shortTypeName switch
{
"String" => "value.ToString()",
"ReadOnlySpan" => "value", // todo: ReadOnlySpan<char> only...
_ => $"buffer.m_params.m_stringParser.Parse<{field.m_qualifiedTypeName}>(value)"
};
return readCommand;
}
private static void WriteParseBodyMethods(ClassGenInfo classGenInfo, IndentedTextWriter writer)
{
var needsInlineBody = false;
var needsSubBody = false;
foreach (var field in classGenInfo.m_bodies)
{
if (field.IsPrimitive() && field.m_xmlName == null)
{
needsInlineBody = true;
} else
{
needsSubBody = true;
}
}
if (needsInlineBody && needsSubBody)
throw new Exception($"{classGenInfo.m_hierarchy.FullyQualifiedMetadataName} needs inline body and sub body");
if (needsInlineBody)
{
writer.WriteLine($"public{classGenInfo.m_overrideSpecifier} bool ParseFullBody(ref XmlReadBuffer buffer, ReadOnlySpan<char> bodySpan, ref int end)");
writer.WriteLine("{");
writer.IncreaseIndent();
foreach (var body in classGenInfo.m_bodies)
{
if (body.IsString())
{
writer.WriteLine($"{body.m_fieldName} = buffer.DeserializeCDATA(bodySpan, out end).ToString();");
} else if (body.IsSpan())
{
writer.WriteLine($"{body.m_fieldName} = buffer.DeserializeCDATA(bodySpan, out end);");
} else
{
throw new NotImplementedException($"Xml:WriteParseBodyMethods: how to inline body {body.m_qualifiedTypeName}");
}
}
writer.WriteLine("return true;");
writer.DecreaseIndent();
writer.WriteLine("}");
} else if (needsSubBody)
{
WriteListConstructor(classGenInfo, writer);
writer.WriteLine();
WriteParseSubBodyMethod(classGenInfo, writer);
}
if (!needsInlineBody)
{
writer.WriteLine($"public{classGenInfo.m_overrideSpecifier} bool ParseFullBody(ref XmlReadBuffer buffer, ReadOnlySpan<char> bodySpan, ref int end)");
writer.WriteLine("{");
writer.IncreaseIndent();
if (classGenInfo.m_hasBaseType)
{
// doesn't really make sense but lets be safe...
writer.WriteLine("return base.ParseFullBody(ref buffer, bodySpan, ref end);");
} else
{
writer.WriteLine("return false;");
}
writer.DecreaseIndent();
writer.WriteLine("}");
}
if (!needsSubBody)
{
writer.WriteLine($"public{classGenInfo.m_overrideSpecifier} bool ParseSubBody(ref XmlReadBuffer buffer, ReadOnlySpan<char> name, ReadOnlySpan<char> bodySpan, ReadOnlySpan<char> innerBodySpan, ref int end, ref int endInner)");
writer.WriteLine("{");
writer.IncreaseIndent();
if (classGenInfo.m_hasBaseType)
{
// doesn't really make sense but lets be safe...
writer.WriteLine("return base.ParseSubBody(ref buffer, name, bodySpan, innerBodySpan, ref end, ref endInner);");
} else
{
writer.WriteLine("return false;");
}
writer.DecreaseIndent();
writer.WriteLine("}");
}
}
private static void WriteParseSubBodyMethod(ClassGenInfo classGenInfo, IndentedTextWriter writer)
{
writer.WriteLine($"public{classGenInfo.m_overrideSpecifier} bool ParseSubBody(ref XmlReadBuffer buffer, ReadOnlySpan<char> name, ReadOnlySpan<char> bodySpan, ReadOnlySpan<char> innerBodySpan, ref int end, ref int endInner)");
writer.WriteLine("{");
writer.IncreaseIndent();
if (classGenInfo.m_hasBaseType)
{
writer.WriteLine("if (base.ParseSubBody(ref buffer, name, bodySpan, innerBodySpan, ref end, ref endInner)) return true;");
}
writer.WriteLine("switch (name)");
writer.WriteLine("{");
writer.IncreaseIndent();
foreach (var field in classGenInfo.m_bodies)
{
var nameToCheck = field.m_xmlName ?? throw new InvalidDataException("no body name??");
writer.WriteLine($"case \"{nameToCheck}\": {{");
writer.IncreaseIndent();
if (field.IsString())
{
writer.WriteLine($"{field.m_fieldName} = buffer.DeserializeCDATA(innerBodySpan, out endInner).ToString();");
} else if (field.IsSpan())
{
writer.WriteLine($"{field.m_fieldName} = buffer.DeserializeCDATA(innerBodySpan, out endInner);");
} else if (field.IsList())
{
writer.WriteLine($"{field.m_fieldName}.Add(buffer.Read<{field.m_elementTypeQualifiedName}>(bodySpan, out end));");
} else
{
if (!field.m_isValueType)
{
writer.WriteLine($"if ({field.m_fieldName} != null) throw new InvalidDataException(\"duplicate non-list body {nameToCheck}\");");
writer.WriteLine($"{field.m_fieldName} = buffer.Read<{field.m_elementTypeQualifiedName}>(bodySpan, out end);");
} else
{
writer.WriteLine($"buffer.ReadInto<{field.m_elementTypeQualifiedName}>(bodySpan, ref {field.m_fieldName}, out end);");
}
}
writer.WriteLine("return true;");
writer.DecreaseIndent();
writer.WriteLine("}");
}
writer.DecreaseIndent();
writer.WriteLine("}");
writer.WriteLine("return false;");
writer.DecreaseIndent();
writer.WriteLine("}");
}
private static void WriteListConstructor(ClassGenInfo classGenInfo, IndentedTextWriter writer)
{
writer.WriteLine($"public {classGenInfo.m_shortName}()");
writer.WriteLine("{");
writer.IncreaseIndent();
foreach (var body in classGenInfo.m_bodies)
{
if (!body.IsList()) continue;
writer.WriteLine($"{body.m_fieldName} = new {body.m_qualifiedTypeName}();");
}
writer.DecreaseIndent();
writer.WriteLine("}");
}
private static void WriteSerializeBodyMethod(ClassGenInfo classGenInfo, IndentedTextWriter writer)
{
writer.WriteLine($"public{classGenInfo.m_overrideSpecifier} void SerializeBody(ref XmlWriteBuffer buffer)");
writer.WriteLine("{");
writer.IncreaseIndent();
if (classGenInfo.m_hasBaseType)
{
writer.WriteLine("base.SerializeBody(ref buffer);");
}
foreach (var field in classGenInfo.m_bodies)
{
var isList = field.IsList();
if (isList)
{
if (field.IsPrimitive())
{
throw new Exception("for xml body of type list<T>, T must be IXmlSerializable");
}
writer.WriteLine($"foreach (var obj in {field.m_fieldName})");
writer.WriteLine("{");
writer.IncreaseIndent();
writer.WriteLine("buffer.PutObject(obj);");
writer.DecreaseIndent();
writer.WriteLine("}");
} else if (!field.IsPrimitive()) // another IXmlSerializable
{
writer.WriteLine($"buffer.PutObject({field.m_fieldName});");
} else
{
if (field.m_xmlName != null)
{
writer.WriteLine("{");
writer.IncreaseIndent();
writer.WriteLine($"var node = buffer.StartNodeHead(\"{field.m_xmlName}\");");
}
if (field.IsString() || field.IsSpan())
{
writer.WriteLine($"buffer.PutCData({field.m_fieldName});");
} else
{
throw new Exception($"how to put sub body {field.m_qualifiedTypeName}");
}
if (field.m_xmlName != null)
{
writer.WriteLine($"buffer.EndNode(ref node);");
writer.DecreaseIndent();
writer.WriteLine("}");
}
}
}
writer.DecreaseIndent();
writer.WriteLine("}");
}
}
}