Repository navigation
Expand file tree
/
Copy pathXmlMetaProvider.cs
More file actions
613 lines (528 loc) · 18.8 KB
/
Copy pathXmlMetaProvider.cs
File metadata and controls
613 lines (528 loc) · 18.8 KB
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/*
Copyright 2017 YANG Huan ([email protected]).
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using Microsoft.CodeAnalysis;
namespace CSharpLua {
public sealed class XmlMetaProvider {
[XmlRoot("meta")]
public sealed class XmlMetaModel {
public sealed class TemplateModel {
[XmlAttribute]
public string Template;
}
public class MemberModel {
[XmlAttribute]
public string name;
[XmlAttribute]
public string Baned;
protected static bool TryTryParseBool(string v, out bool b) {
b = false;
if (v != null) {
if (v.Equals(bool.TrueString, StringComparison.OrdinalIgnoreCase)) {
b = true;
return true;
}
if (v.Equals(bool.FalseString, StringComparison.OrdinalIgnoreCase)) {
b = false;
return false;
}
}
return false;
}
internal bool IsBaned {
get {
if (!string.IsNullOrEmpty(Baned)) {
if (TryTryParseBool(Baned, out bool b)) {
return b;
}
return true;
}
return false;
}
}
private string BanedMessage {
get {
if (!string.IsNullOrEmpty(Baned)) {
if (TryTryParseBool(Baned, out bool b)) {
return b ? "cannot use" : null;
}
return Baned;
}
return null;
}
}
public void CheckBaned(ISymbol symbol) {
if (IsBaned) {
throw new CompilationErrorException($"{symbol} is baned, {BanedMessage}");
}
}
}
public sealed class PropertyModel : MemberModel {
[XmlAttribute]
public string Name;
[XmlElement]
public TemplateModel set;
[XmlElement]
public TemplateModel get;
[XmlAttribute]
public string IsField;
public bool? CheckIsField {
get {
if (TryTryParseBool(IsField, out bool b)) {
return b;
}
return null;
}
}
}
public sealed class FieldModel : MemberModel {
[XmlAttribute]
public string Template;
[XmlAttribute]
public bool IsProperty;
}
public sealed class ArgumentModel {
[XmlAttribute]
public string type;
[XmlElement("arg")]
public ArgumentModel[] GenericArgs;
}
public sealed class MethodModel : MemberModel {
[XmlAttribute]
public string Name;
[XmlAttribute]
public string Template;
[XmlAttribute]
public int ArgCount = -1;
[XmlElement("arg")]
public ArgumentModel[] Args;
[XmlAttribute]
public string RetType;
[XmlAttribute]
public int GenericArgCount = -1;
[XmlAttribute]
public bool IgnoreGeneric;
internal string GetMetaInfo(MethodMetaType type) {
switch (type) {
case MethodMetaType.Name: {
return Name;
}
case MethodMetaType.CodeTemplate: {
return Template;
}
case MethodMetaType.IgnoreGeneric: {
return IgnoreGeneric ? bool.TrueString : bool.FalseString;
}
default: {
throw new InvalidOperationException();
}
}
}
}
public sealed class ClassModel : MemberModel {
[XmlAttribute]
public string Name;
[XmlElement("property")]
public PropertyModel[] Properties;
[XmlElement("field")]
public FieldModel[] Fields;
[XmlElement("method")]
public MethodModel[] Methods;
[XmlAttribute]
public bool IgnoreGeneric;
[XmlAttribute]
public bool Readonly;
}
public sealed class NamespaceModel : MemberModel {
[XmlAttribute]
public string Name;
[XmlElement("class")]
public ClassModel[] Classes;
}
public sealed class AssemblyModel {
[XmlElement("namespace")]
public NamespaceModel[] Namespaces;
[XmlElement("class")]
public ClassModel[] Classes;
}
[XmlElement("assembly")]
public AssemblyModel Assembly;
}
internal enum MethodMetaType {
Name,
CodeTemplate,
IgnoreGeneric,
}
private sealed class MethodMetaInfo {
private readonly List<XmlMetaModel.MethodModel> models_ = new();
private bool isSingleModel_;
public void Add(XmlMetaModel.MethodModel model) {
models_.Add(model);
CheckIsSingleModel();
}
private void CheckIsSingleModel() {
bool isSingle = false;
if (models_.Count == 1) {
var model = models_.First();
if (model.ArgCount == -1 && model.Args == null && model.RetType == null && model.GenericArgCount == -1) {
isSingle = true;
}
}
isSingleModel_ = isSingle;
}
private static string GetTypeString(ITypeSymbol symbol) {
if (symbol.Kind == SymbolKind.TypeParameter) {
return symbol.Name;
}
StringBuilder sb = new StringBuilder();
INamedTypeSymbol typeSymbol = (INamedTypeSymbol)symbol.OriginalDefinition;
var namespaceSymbol = typeSymbol.ContainingNamespace;
if(symbol.ContainingType != null) {
sb.Append(GetTypeString(symbol.ContainingType));
sb.Append('.');
}
else if (!namespaceSymbol.IsGlobalNamespace) {
sb.Append(namespaceSymbol);
sb.Append('.');
}
sb.Append(symbol.Name);
if (typeSymbol.TypeArguments.Length > 0) {
sb.Append('`');
sb.Append(typeSymbol.TypeArguments.Length);
}
return sb.ToString();
}
private static bool IsTypeMatch(ITypeSymbol symbol, string typeString) {
if (symbol.Kind == SymbolKind.ArrayType) {
var typeSymbol = (IArrayTypeSymbol)symbol;
string elementTypeName = GetTypeString(typeSymbol.ElementType);
return elementTypeName + "[]" == typeString;
}
string name = GetTypeString(symbol);
return name == typeString;
}
private static bool IsArgMatch(ITypeSymbol symbol, XmlMetaModel.ArgumentModel parameterModel) {
if (!IsTypeMatch(symbol, parameterModel.type)) {
return false;
}
if (parameterModel.GenericArgs != null) {
var typeSymbol = (INamedTypeSymbol)symbol;
if (typeSymbol.TypeArguments.Length != parameterModel.GenericArgs.Length) {
return false;
}
int index = 0;
foreach (var typeArgument in typeSymbol.TypeArguments) {
var genericArgModel = parameterModel.GenericArgs[index];
if (!IsArgMatch(typeArgument, genericArgModel)) {
return false;
}
++index;
}
}
return true;
}
private bool IsMethodMatch(XmlMetaModel.MethodModel model, IMethodSymbol symbol) {
if (model.name != symbol.Name) {
return false;
}
if (model.ArgCount != -1) {
if (symbol.Parameters.Length != model.ArgCount) {
return false;
}
}
if (model.GenericArgCount != -1) {
if (symbol.TypeArguments.Length != model.GenericArgCount) {
return false;
}
}
if (!string.IsNullOrEmpty(model.RetType)) {
if (!IsTypeMatch(symbol.ReturnType, model.RetType)) {
return false;
}
}
if (model.Args != null) {
if (symbol.Parameters.Length != model.Args.Length) {
return false;
}
int index = 0;
foreach (var parameter in symbol.Parameters) {
var parameterModel = model.Args[index];
if (!IsArgMatch(parameter.Type, parameterModel)) {
return false;
}
++index;
}
}
return true;
}
private XmlMetaModel.MethodModel GetMethodModel(IMethodSymbol symbol, bool isCheckBaned) {
var methodModel = isSingleModel_
? models_.First()
: models_.Find(i => IsMethodMatch(i, symbol));
if (methodModel != null && isCheckBaned) {
methodModel.CheckBaned(symbol);
}
return methodModel;
}
public string GetMetaInfo(IMethodSymbol symbol, MethodMetaType type) {
return GetMethodModel(symbol, type == MethodMetaType.CodeTemplate)?.GetMetaInfo(type);
}
}
private sealed class TypeMetaInfo {
private readonly XmlMetaModel.ClassModel model_;
private readonly Dictionary<string, XmlMetaModel.FieldModel> fields_ = new();
private readonly Dictionary<string, XmlMetaModel.PropertyModel> properties_ = new();
private readonly Dictionary<string, MethodMetaInfo> methods_ = new();
public TypeMetaInfo(XmlMetaModel.ClassModel model) {
model_ = model;
Field();
Property();
Method();
}
public XmlMetaModel.ClassModel Model {
get {
return model_;
}
}
private void Field() {
if (model_.Fields != null) {
foreach (var fieldModel in model_.Fields) {
if (string.IsNullOrEmpty(fieldModel.name)) {
throw new ArgumentException($"type [{model_.name}] has a field name is empty");
}
if (fields_.ContainsKey(fieldModel.name)) {
throw new ArgumentException($"type [{model_.name}]'s field [{fieldModel.name}] is already exists");
}
fields_.Add(fieldModel.name, fieldModel);
}
}
}
private void Property() {
if (model_.Properties != null) {
foreach (var propertyModel in model_.Properties) {
if (string.IsNullOrEmpty(propertyModel.name)) {
throw new ArgumentException($"type [{model_.name}] has a property name is empty");
}
if (fields_.ContainsKey(propertyModel.name)) {
throw new ArgumentException($"type [{model_.name}]'s property [{propertyModel.name}] is already exists");
}
properties_.Add(propertyModel.name, propertyModel);
}
}
}
private void Method() {
if (model_.Methods != null) {
foreach (var methodModel in model_.Methods) {
if (string.IsNullOrEmpty(methodModel.name)) {
throw new ArgumentException($"type [{model_.name}] has a method name is empty");
}
var info = methods_.GetOrDefault(methodModel.name);
if (info == null) {
info = new MethodMetaInfo();
methods_.Add(methodModel.name, info);
}
info.Add(methodModel);
}
}
}
public XmlMetaModel.FieldModel GetFieldModel(string name) {
return fields_.GetOrDefault(name);
}
public XmlMetaModel.PropertyModel GetPropertyModel(string name) {
return properties_.GetOrDefault(name);
}
public MethodMetaInfo GetMethodMetaInfo(string name) {
return methods_.GetOrDefault(name);
}
}
private readonly Dictionary<string, XmlMetaModel.NamespaceModel> namespaceNameMaps_ = new();
private readonly Dictionary<string, TypeMetaInfo> typeMetas_ = new();
public XmlMetaProvider(IEnumerable<Stream> streams) {
foreach (Stream stream in streams) {
var serializer = new XmlSerializer(typeof(XmlMetaModel));
XmlMetaModel model = (XmlMetaModel)serializer.Deserialize(stream);
var assembly = model.Assembly;
if (assembly != null) {
if (assembly.Namespaces != null) {
foreach (var namespaceModel in assembly.Namespaces) {
LoadNamespace(namespaceModel);
}
}
if (assembly.Classes != null) {
LoadType(string.Empty, assembly.Classes);
}
}
}
}
private void LoadNamespace(XmlMetaModel.NamespaceModel model) {
string namespaceName = model.name;
if (namespaceName == null) {
throw new ArgumentException("namespace.name is null");
}
if (namespaceName.Length > 0) {
if (namespaceNameMaps_.ContainsKey(namespaceName)) {
throw new ArgumentException($"namespace [{namespaceName}] is already has");
}
if (!string.IsNullOrEmpty(model.Name) || model.IsBaned) {
namespaceNameMaps_.Add(namespaceName, model);
}
}
if (model.Classes != null) {
string name = !string.IsNullOrEmpty(model.Name) ? model.Name : namespaceName;
LoadType(name, model.Classes);
}
}
private void LoadType(string namespaceName, XmlMetaModel.ClassModel[] classes) {
foreach (var classModel in classes) {
string className = classModel.name;
if (string.IsNullOrEmpty(className)) {
throw new ArgumentException($"namespace [{namespaceName}] has a class's name is empty");
}
string classesFullName = namespaceName.Length > 0 ? namespaceName + '.' + className : className;
classesFullName = classesFullName.Replace('`', '_');
if (typeMetas_.ContainsKey(classesFullName)) {
throw new ArgumentException($"type [{classesFullName}] is already has");
}
TypeMetaInfo info = new TypeMetaInfo(classModel);
typeMetas_.Add(classesFullName, info);
}
}
public string GetNamespaceMapName(INamespaceSymbol symbol, string original) {
var info = namespaceNameMaps_.GetOrDefault(original);
if (info != null) {
info.CheckBaned(symbol);
return info.Name;
}
return null;
}
internal bool MayHaveCodeMeta(ISymbol symbol) {
return symbol.DeclaredAccessibility == Accessibility.Public && symbol.IsFromAssembly();
}
private string GetTypeShortString(ISymbol symbol) {
INamedTypeSymbol typeSymbol = (INamedTypeSymbol)symbol.OriginalDefinition;
return typeSymbol.GetTypeShortName(GetNamespaceMapName);
}
internal string GetTypeMapName(ISymbol symbol, string shortName) {
if (MayHaveCodeMeta(symbol)) {
var info = GetTypeMetaInfo(symbol, shortName);
return info?.Model.Name;
}
return null;
}
internal bool IsTypeIgnoreGeneric(INamedTypeSymbol typeSymbol) {
if (MayHaveCodeMeta(typeSymbol)) {
var info = GetTypeMetaInfo(typeSymbol);
return info != null && info.Model.IgnoreGeneric;
}
return false;
}
internal bool IsTypeReadOnly(INamedTypeSymbol typeSymbol) {
if (MayHaveCodeMeta(typeSymbol)) {
var info = GetTypeMetaInfo(typeSymbol);
return info != null && info.Model.Readonly;
}
return false;
}
private TypeMetaInfo GetTypeMetaInfo(ISymbol symbol, string shortName) {
var info = typeMetas_.GetOrDefault(shortName);
info?.Model.CheckBaned(symbol);
return info;
}
private TypeMetaInfo GetTypeMetaInfo(INamedTypeSymbol typeSymbol) {
string shortName = GetTypeShortString(typeSymbol);
return GetTypeMetaInfo(typeSymbol, shortName);
}
private TypeMetaInfo GetTypeMetaInfo(ISymbol memberSymbol) {
return GetTypeMetaInfo(memberSymbol.ContainingType);
}
private XmlMetaModel.FieldModel GetFieldMetaInfo(IFieldSymbol symbol) {
if (MayHaveCodeMeta(symbol)) {
return GetTypeMetaInfo(symbol)?.GetFieldModel(symbol.Name);
}
return null;
}
public string GetFieldCodeTemplate(IFieldSymbol symbol) {
return GetFieldMetaInfo(symbol)?.Template ?? symbol.GetCodeTemplateFromAttribute();
}
public bool IsFieldForceProperty(IFieldSymbol symbol) {
return GetFieldMetaInfo(symbol)?.IsProperty ?? false;
}
private XmlMetaModel.PropertyModel GetPropertyMetaInfo(IPropertySymbol symbol) {
if (MayHaveCodeMeta(symbol)) {
var info = GetTypeMetaInfo(symbol)?.GetPropertyModel(symbol.Name);
info?.CheckBaned(symbol);
return info;
}
return null;
}
public bool? IsPropertyField(IPropertySymbol symbol) {
return GetPropertyMetaInfo(symbol)?.CheckIsField;
}
public string GetPropertyCodeTemplate(IPropertySymbol symbol, bool isGet) {
var info = GetPropertyMetaInfo(symbol);
if (info != null) {
return isGet ? info.get?.Template : info.set?.Template;
}
return null;
}
public string GetPropertyMapName(IPropertySymbol symbol) {
return GetPropertyMetaInfo(symbol)?.Name;
}
private string GetInternalMethodMetaInfo(IMethodSymbol symbol, MethodMetaType metaType) {
Contract.Assert(symbol != null);
if (!symbol.IsPublic()) {
return null;
}
string metaInfo = null;
if (symbol.IsFromAssembly()) {
metaInfo = GetTypeMetaInfo(symbol)?.GetMethodMetaInfo(symbol.Name)?.GetMetaInfo(symbol, metaType);
}
if (metaInfo == null) {
if (symbol.IsOverride) {
if (symbol.OverriddenMethod != null) {
metaInfo = GetInternalMethodMetaInfo(symbol.OverriddenMethod, metaType);
}
} else {
var interfaceImplementations = symbol.InterfaceImplementations();
if (interfaceImplementations != null) {
foreach (IMethodSymbol interfaceMethod in interfaceImplementations) {
metaInfo = GetInternalMethodMetaInfo(interfaceMethod, metaType);
if (metaInfo != null) {
break;
}
}
}
}
}
return metaInfo;
}
private string GetMethodMetaInfo(IMethodSymbol symbol, MethodMetaType metaType) {
Utility.CheckMethodDefinition(ref symbol);
return GetInternalMethodMetaInfo(symbol, metaType);
}
public string GetMethodMapName(IMethodSymbol symbol) {
return GetMethodMetaInfo(symbol, MethodMetaType.Name);
}
public string GetMethodCodeTemplate(IMethodSymbol symbol) {
return GetMethodMetaInfo(symbol, MethodMetaType.CodeTemplate) ?? symbol.GetCodeTemplateFromAttribute();
}
public bool IsMethodIgnoreGeneric(IMethodSymbol symbol) {
return GetMethodMetaInfo(symbol, MethodMetaType.IgnoreGeneric) == bool.TrueString;
}
}
}