-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignatures.cs
785 lines (675 loc) · 22.7 KB
/
Signatures.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
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
// CodeNode is written though reflection
#pragma warning disable 0649 // CS0649: Field '...' is never assigned to
// II.23.2 Blobs and signatures
// Most defined in II.23.2.3, superset of defintions in .1 .2 .5
sealed class CallingConvention : CodeNode
{
public LowerBits Kind { get; private set; }
public UpperBits Flags { get; private set; }
protected override void InnerRead() {
var data = Bytes.Read<byte>();
Kind = (LowerBits)(data & lowerMask);
Flags = (UpperBits)(data & flagsMask);
}
public override string NodeValue => (new Enum[] { Kind, Flags }).GetString();
public override string Description => string.Join("\n", Kind.Describe().Concat(Flags.Describe()));
public const byte lowerMask = 0xF;
public enum LowerBits : byte
{
[Description("Normal .NET method call.")]
DEFAULT = 0x0,
[Description("Unmanaged standard C style call")]
C = 0x1,
[Description("Unmanaged standard C++ style call.")]
STDCALL = 0x2,
[Description("Unmanaged call expecting an implicit this pointer.")]
THISCALL = 0x3,
[Description("Unmanaged C style fastcall.")]
FASTCALL = 0x4,
[Description("Accepting a variable number of arguments.")]
VARARG = 0x5,
FIELD = 0x6,
LOCAL_SIG = 0x7,
PROPERTY = 0x8,
}
const byte flagsMask = 0xF0;
[Flags]
public enum UpperBits : byte
{
[Description("The method has one or more generic parameters.")]
GENERIC = 0x10,
[Description("Instance member: this shall be passed.")]
HASTHIS = 0x20,
[Description("First param specifies the type of this pointer.")]
EXPLICITTHIS = 0x40,
}
}
[Ecma("II.23.2")]
sealed class UnsignedCompressed : CodeNode
{
public uint Value { get; private set; }
public override string NodeValue => Value.GetString();
// Number is compressed into one of these byte formats, where x is a bit of the number
// 0xxxxxxx
// 10xxxxxx xxxxxxxx
// 110xxxxx xxxxxxxx xxxxxxxx xxxxxxxx
protected override void InnerRead() {
var first = Bytes.Read<byte>();
switch (GetWidth(first)) {
case 1:
Value = first;
break;
case 2:
var second = Bytes.Read<byte>();
Value = (uint)(((first & ~0xC0) << 8) + second);
break;
case 4:
second = Bytes.Read<byte>();
var third = Bytes.Read<byte>();
var fourth = Bytes.Read<byte>();
Value = (uint)(((first & ~0xC0) << 24) + (second << 16) + (third << 8) + fourth);
break;
default:
throw new InvalidDataException();
}
}
static int GetWidth(byte b) {
switch (b & 0xE0) {
case 0x00:
case 0x20:
case 0x40:
case 0x60:
return 1;
case 0x80:
case 0xA0:
return 2;
case 0xC0:
return 4;
case 0xE0:
throw new InvalidOperationException("Not expecting null string!");
default:
throw new InvalidOperationException();
}
}
}
[Ecma("II.23.2")]
sealed class SignedCompressed : CodeNode
{
public int Value { get; private set; }
public override string NodeValue => Value.GetString();
// Number is compressed into one of these byte formats, where x is a bit of the number
// 0xxxxxxx
// 10xxxxxx xxxxxxxx
// 110xxxxx xxxxxxxx xxxxxxxx xxxxxxxx
protected override void InnerRead() {
var unsigned = Bytes.ReadClass<UnsignedCompressed>();
var sum = (int)unsigned.Value;
if (sum % 2 == 0) {
Value = sum >> 1;
return;
}
int negativeMask = (unsigned.End - unsigned.Start) switch {
1 => unchecked((int)0xFFFFFFC0),
2 => unchecked((int)0xFFFFE000),
4 => unchecked((int)0xF0000000),
_ => throw new InvalidOperationException()
};
Value = negativeMask | (sum >> 1);
}
}
//TODO(Index4Bytes) This and EitherSignature assume that blob heap index is 2-bytes wide
sealed class Signature<T> : SizedSignature<short, T> where T : CodeNode, new()
{
}
abstract class SizedSignature<Ti, Ts> : CodeNode where Ti : struct where Ts : CodeNode, new()
{
// Same shape as BlobHeapIndex but allow for custom types in the blob heap
public Ti Index;
Lazy<Ts> link;
public SizedSignature() {
link = new Lazy<Ts>(() => Bytes.BlobHeap.GetCustom<Ts>(Index.GetInt32()));
}
public Ts Value => link.Value;
public override CodeNode Link => Value;
public override string NodeValue => Value.NodeValue;
protected override void InnerRead() {
Index = Bytes.Read<Ti>();
// Actually read the sig from the blob heap later, AFTER all tilde metadata tables are read
}
}
interface INamedValue
{
string NamedValue(string name);
}
sealed class EitherSignature : CodeNode
{
// This exists to solve a tricky problem: the two Tables that use EitherSignature can contain either some method sig, or a different sig. So peek at the first byte of the sig to figure out the kind, then use the appropriate type to read it.
public short Index;
Lazy<CodeNode> link;
string methodSigName;
string ecma;
CallingConvention.LowerBits allowedKind;
public EitherSignature(string methodSigName, string ecma, CallingConvention.LowerBits kind) {
link = new Lazy<CodeNode>(ReadLink);
this.methodSigName = methodSigName;
this.ecma = ecma;
this.allowedKind = kind;
}
public override CodeNode Link => link.Value;
public override string NodeValue => Link.NodeValue;
public string NamedValue(string name) => ((INamedValue)Link).NamedValue(name);
public override string EcmaSection => ecma;
public FieldSig FieldSig { get; private set; }
public LocalVarSig LocalVarSig { get; private set; }
public PropertySig PropertySig { get; private set; }
public MethodDefRefSig MethodDefRefSig { get; private set; }
void CheckKind(CallingConvention.LowerBits actual) {
if (allowedKind != actual) {
Errors.Add($"Expected {methodSigName} or {allowedKind} but found {actual}");
}
}
public CodeNode ReadLink() {
var b = Bytes.BlobHeap.GetByte(Index);
var kind = (CallingConvention.LowerBits)(CallingConvention.lowerMask & b);
switch (kind) {
case CallingConvention.LowerBits.FIELD:
CheckKind(kind);
return FieldSig = Bytes.BlobHeap.GetCustom<FieldSig>(Index);
case CallingConvention.LowerBits.LOCAL_SIG:
CheckKind(kind);
return LocalVarSig = Bytes.BlobHeap.GetCustom<LocalVarSig>(Index);
case CallingConvention.LowerBits.PROPERTY:
CheckKind(kind);
return PropertySig = Bytes.BlobHeap.GetCustom<PropertySig>(Index);
default:
var sig = Bytes.BlobHeap.GetCustom<MethodDefRefSig>(Index);
sig.NodeName = methodSigName;
sig.EcmaSection = ecma;
sig.Kind.EcmaSection = ecma;
return MethodDefRefSig = sig;
}
}
protected override void InnerRead() {
Index = Bytes.Read<short>();
// Actually read the sig from the blob heap later, AFTER all tilde metadata tables are read
}
}
// II.23.2.1 MethodDefSig
// II.23.2.2 MethodRefSig
// II.23.2.3 StandAloneMethodSig
// The same heap bytes can be used for MethodRefSig and MethodDefSig, so munge the types together so two different types don't exist at the same location.
sealed class MethodDefRefSig : CodeNode, INamedValue
{
public CallingConvention Kind;
public UnsignedCompressed GenParamCount;
public UnsignedCompressed ParamCount;
public RetType RetType;
public ParamSig[] Params;
public ElementType VarArgSentinel;
public ParamSig[] VarArgParams;
public override string NodeValue => NamedValue("");
public string NamedValue(string name) {
var genVal = GenParamCount?.Value > 0 ? "<>" : ""; //TODO(link) pull in Name, in/out, struct/new(), constraints from GenericParam and GenericParamConstraint
var values = Params.Select(p => p.NodeValue);
if (Kind.Kind == CallingConvention.LowerBits.VARARG) {
values = values.Concat(new[] { "..." }).Concat(VarArgParams.Select(p => p.NodeValue));
}
var callKind = (Kind.Kind) switch {
CallingConvention.LowerBits.DEFAULT => "",
CallingConvention.LowerBits.C => "cdecl",
CallingConvention.LowerBits.STDCALL => "stdcall",
CallingConvention.LowerBits.THISCALL => "thiscall",
CallingConvention.LowerBits.FASTCALL => "fastcall",
CallingConvention.LowerBits.VARARG => "vararg",
_ => throw new InvalidOperationException(),
};
var parts = new[] {
Kind.Flags.HasFlag(CallingConvention.UpperBits.HASTHIS) ? "" : "static",
Kind.Flags.HasFlag(CallingConvention.UpperBits.EXPLICITTHIS) ? "explicitthis" : "",
callKind,
RetType.NodeValue,
$"{name}{genVal}({string.Join(", ", values)})",
};
return string.Join(" ", parts.Where(s => !string.IsNullOrEmpty(s)));
}
protected override void InnerRead() {
AddChild(nameof(Kind));
if (Kind.Flags.HasFlag(CallingConvention.UpperBits.GENERIC)) {
AddChild(nameof(GenParamCount));
}
AddChild(nameof(ParamCount));
AddChild(nameof(RetType));
// Tricky code to fill Params with m params, then after Sentinel fill VarArgParams with N-m params
List<ParamSig> ps = new List<ParamSig>(), vaps = new List<ParamSig>(), curr = ps;
var currName = nameof(Params);
var expectingSentinel = Kind.Kind == CallingConvention.LowerBits.VARARG;
for (var i = 0; i < ParamCount.Value; ++i) {
if (expectingSentinel && TryAddChild(nameof(VarArgSentinel), ElementType.Sentinel)) {
expectingSentinel = false;
curr = vaps;
currName = nameof(VarArgParams);
}
var p = Bytes.ReadClass<ParamSig>();
p.NodeName = $"{currName}[{curr.Count}]";
curr.Add(p);
Children.Add(p);
}
Params = ps.ToArray();
VarArgParams = vaps.ToArray();
if (expectingSentinel && TryAddChild(nameof(VarArgSentinel), ElementType.Sentinel)) {
//TODO(SpecViolation) ilasm will put a sentinel here even if there are no varargs params
}
}
}
[Ecma("II.23.2.4")]
sealed class FieldSig : CodeNode, INamedValue
{
[Expected(0x6)]
public byte FIELD;
public CustomMods CustomMods;
public TypeSig Type;
public override string NodeValue => NamedValue("");
public string NamedValue(string name) {
var parts = new[] {
Type.NodeValue,
CustomMods.NodeValue,
name,
};
return string.Join(" ", parts.Where(s => !string.IsNullOrEmpty(s)));
}
protected override void InnerRead() {
AddChild(nameof(FIELD));
AddChild(nameof(CustomMods));
ResizeLastChild();
AddChild(nameof(Type));
}
}
[Ecma("II.23.2.5")]
sealed class PropertySig : CodeNode, INamedValue
{
[Ecma("II.23.2.5")]
public CallingConvention Kind;
public UnsignedCompressed ParamCount;
public CustomMods CustomMods;
public TypeSig Type;
public ParamSig[] Param;
public override string NodeValue => NamedValue("");
public string NamedValue(string name) {
var parts = new[] {
Kind.Flags.HasFlag(CallingConvention.UpperBits.HASTHIS) ? "" : "static",
Type.NodeValue,
name,
Param.Any() ? $"({string.Join(", ", Param.Select(p => p.NodeValue))})" : "",
CustomMods.NodeValue,
};
return string.Join(" ", parts.Where(s => !string.IsNullOrEmpty(s)));
}
protected override void InnerRead() {
AddChild(nameof(Kind));
AddChild(nameof(ParamCount));
AddChild(nameof(CustomMods));
ResizeLastChild();
AddChild(nameof(Type));
AddChildren(nameof(Param), (int)ParamCount.Value);
}
}
[Ecma("II.23.2.6")]
sealed class LocalVarSig : CodeNode
{
[Expected(0x7)]
public byte LocalSig;
[OrderedField]
public UnsignedCompressed Count;
[OrderedField]
[Ecma("II.23.2.6")]
public LocalVar[] Types;
public override string NodeValue => string.Join(", ", Types.Select(t => t.NodeValue));
protected override int GetCount(string field) => field switch {
nameof(Types) => (int)Count.Value,
_ => base.GetCount(field),
};
public sealed class LocalVar : CodeNode
{
public CustomMods CustomMods;
[Ecma("II.23.2.9")]
public ElementType Constraint;
public ElementType ByRef;
public TypeSig Type;
string value;
public override string NodeValue {
get {
if (value != null) return value;
var parts = new[] {
Type.NodeValue,
ByRef != default ? "&" : "",
Constraint != default ? Constraint.S() : "",
CustomMods.NodeValue,
};
return string.Join(" ", parts.Where(s => !string.IsNullOrEmpty(s))).Replace(" &", "&");
}
}
protected override void InnerRead() {
TryAddChild(nameof(Constraint), ElementType.Pinned);
if (Bytes.Peek<ElementType>() == ElementType.TypedByRef) {
value = Bytes.Read<ElementType>().S();
return;
}
AddChild(nameof(CustomMods));
ResizeLastChild();
TryAddChild(nameof(Constraint), ElementType.Pinned);
TryAddChild(nameof(ByRef), ElementType.ByRef);
AddChild(nameof(Type));
if (Children.Count == 1) {
Children = Type.Children;
}
}
}
}
[Ecma("II.23.2.7")]
sealed class CustomMod : CodeNode
{
public ElementType OptOrReq;
public TypeDefOrRefOrSpecEncoded Token;
protected override void InnerRead() {
AddChild(nameof(OptOrReq));
AddChild(nameof(Token));
string name;
switch (OptOrReq) {
case ElementType.CModOpt:
name = "modopt";
break;
case ElementType.CModReqd:
name = "modreq";
break;
default:
name = "!!missing";
Errors.Add("OptOrReq must be CModOpt or CModReqd");
break;
};
NodeValue = $"{name} ({Token.NodeValue})";
}
}
[Ecma("II.23.2.7")]
sealed class CustomMods : CodeNode
{
protected override void InnerRead() {
while (true) {
var b = Bytes.Peek<ElementType>();
if (b != ElementType.CModOpt && b != ElementType.CModReqd) break;
var mod = Bytes.ReadClass<CustomMod>();
mod.NodeName = $"{nameof(CustomMods)}[{Children.Count}]";
Children.Add(mod);
}
NodeValue = string.Join(" ", Enumerable.Reverse(Children).Select(c => c.NodeValue));
}
}
[Ecma("II.23.2.8")]
sealed class TypeDefOrRefOrSpecEncoded : CodeNode
{
protected override void InnerRead() {
var encoded = Bytes.ReadClass<UnsignedCompressed>();
var tag = encoded.Value & 0b11;
var index = (encoded.Value >> 2) - 1;
Link = tag switch {
0b00 => Bytes.TildeStream.TypeDefs[index],
0b01 => Bytes.TildeStream.TypeRefs[index],
0b10 => Bytes.TildeStream.TypeSpecs[index],
_ => throw new InvalidOperationException(),
};
NodeValue = Link.NodeValue;
Description = $"Compressed {encoded.Value} has lower 2 bits {tag} specify {Link.GetType().Name}, with row {index}";
}
}
[Ecma("II.23.2.10")]
sealed class ParamSig : CodeNode
{
public CustomMods CustomMods;
public ElementType ByRef;
public TypeSig Type;
protected override void InnerRead() {
AddChild(nameof(CustomMods));
ResizeLastChild();
string val;
if (Bytes.Peek<ElementType>() == ElementType.TypedByRef) {
val = Bytes.Read<ElementType>().S();
} else {
TryAddChild(nameof(ByRef), ElementType.ByRef);
AddChild(nameof(Type));
val = Type.NodeValue + (ByRef != default ? "&" : "");
}
NodeValue = string.Join(" ", new[] {
val,
CustomMods.NodeValue,
}.Where(s => !string.IsNullOrEmpty(s)));
}
}
[Ecma("II.23.2.11")]
sealed class RetType : CodeNode
{
public CustomMods CustomMods;
public ElementType ByRef;
public TypeSig Type;
public ElementType Void;
protected override void InnerRead() {
AddChild(nameof(CustomMods));
ResizeLastChild();
string val;
if (Bytes.Peek<ElementType>() == ElementType.TypedByRef) {
val = Bytes.Read<ElementType>().S();
} else if (TryAddChild(nameof(Void), ElementType.Void)) {
val = "void";
} else {
TryAddChild(nameof(ByRef), ElementType.ByRef);
AddChild(nameof(Type));
val = Type.NodeValue + (ByRef != default ? "&" : "");
}
NodeValue = string.Join(" ", new[] {
val,
CustomMods.NodeValue,
}.Where(s => !string.IsNullOrEmpty(s)));
}
}
[Ecma("II.23.2.12")]
sealed class TypeSig : CodeNode
{
public CustomMods PrefixCustomMods;
//TODO(SpecViolation) CModOpt shouldn't be allowed at start of TypeSpec, but found from i.e. `object modopt ([mscorlib]System.Text.StringBuilder)`
public ElementType Type;
public TypeDefOrRefOrSpecEncoded TypeEncoded;
public UnsignedCompressed VarNumber;
public CustomMods PtrCustomMods;
public TypeSig PtrType;
public ElementType PtrVoid;
public MethodDefRefSig MethodRefSig; // use MethodRefSig as it is the superset type
protected override void InnerRead() {
AddChild(nameof(PrefixCustomMods));
ResizeLastChild();
AddChild(nameof(Type));
switch (Type) {
case ElementType.Boolean:
case ElementType.Char:
case ElementType.Int1:
case ElementType.UInt1:
case ElementType.Int2:
case ElementType.UInt2:
case ElementType.Int4:
case ElementType.UInt4:
case ElementType.Int8:
case ElementType.UInt8:
case ElementType.Real4:
case ElementType.Real8:
case ElementType.IntPtr:
case ElementType.UIntPtr:
case ElementType.Object:
case ElementType.String:
if (Children.Count == 1) Children.Clear();
SetNodeValue(Type.S());
return;
case ElementType.Array:
ReadArray();
return;
case ElementType.Class:
case ElementType.ValueType:
AddChild(nameof(TypeEncoded));
SetNodeValue(Type.S(), TypeEncoded.NodeValue);
return;
case ElementType.Fnptr:
AddChild(nameof(MethodRefSig));
MethodRefSig.EcmaSection = "II.23.2.2";
SetNodeValue($"method {MethodRefSig.NamedValue("*")}");
return;
case ElementType.GenericInst:
ReadGeneric();
return;
case ElementType.MVar:
AddChild(nameof(VarNumber));
SetNodeValue($"!!{VarNumber.Value}");
return;
case ElementType.Ptr:
AddChild(nameof(PtrCustomMods));
ResizeLastChild();
string ptrNodeVal;
if (TryAddChild(nameof(PtrVoid), ElementType.Void)) {
ptrNodeVal = "void";
} else {
AddChild(nameof(PtrType));
ptrNodeVal = PtrType.NodeValue;
}
if (!string.IsNullOrEmpty(PtrCustomMods.NodeValue)) {
SetNodeValue(ptrNodeVal, PtrCustomMods.NodeValue + "*");
} else {
SetNodeValue(ptrNodeVal + "*");
}
return;
case ElementType.SzArray:
ReadSzArray();
return;
case ElementType.Var:
AddChild(nameof(VarNumber));
SetNodeValue($"!{VarNumber.Value}");
return;
default:
throw new InvalidOperationException(Type.GetString());
}
}
void SetNodeValue(params string[] parts) {
parts = parts.Concat(new[] { PrefixCustomMods.NodeValue }).ToArray();
NodeValue = string.Join(" ", parts.Where(s => !string.IsNullOrEmpty(s)));
}
public TypeSig ArrayElementType;
public ArrayShape ArrayShape;
void ReadArray() {
AddChild(nameof(ArrayElementType));
AddChild(nameof(ArrayShape));
SetNodeValue($"{ArrayElementType.NodeValue}[{ArrayShape.NodeValue}]");
}
public CustomMods SzArrayCustomMods;
public TypeSig SzArrayElementType;
void ReadSzArray() {
AddChild(nameof(SzArrayCustomMods));
ResizeLastChild();
AddChild(nameof(SzArrayElementType));
if (!string.IsNullOrEmpty(SzArrayCustomMods.NodeValue)) {
SetNodeValue(SzArrayElementType.NodeValue, SzArrayCustomMods.NodeValue + "[]");
} else {
SetNodeValue(SzArrayElementType.NodeValue + "[]");
}
}
public ElementType GenKind;
public TypeDefOrRefOrSpecEncoded GenType;
public UnsignedCompressed GenArgCount;
public TypeSig[] GenArgTypes;
void ReadGeneric() {
AddChild(nameof(GenKind));
if (GenKind != ElementType.ValueType && GenKind != ElementType.Class) {
Errors.Add("GenKind must be ValueType or Class");
}
AddChild(nameof(GenType));
AddChild(nameof(GenArgCount));
AddChildren(nameof(GenArgTypes), (int)GenArgCount.Value);
SetNodeValue("Generic", GenKind.S(), $"{GenType.NodeValue}<{string.Join(", ", GenArgTypes.Select(t => t.NodeValue))}>");
}
}
[Ecma("II.23.2.13")]
sealed class ArrayShape : CodeNode
{
public UnsignedCompressed Rank;
public UnsignedCompressed NumSizes;
public UnsignedCompressed[] Size;
public UnsignedCompressed NumLoBounds;
public SignedCompressed[] LoBound;
protected override void InnerRead() {
AddChild(nameof(Rank));
AddChild(nameof(NumSizes));
AddChildren(nameof(Size), (int)NumSizes.Value);
AddChild(nameof(NumLoBounds));
AddChildren(nameof(LoBound), (int)NumLoBounds.Value);
var text = new List<string>();
for (int i = 0; i < Rank.Value; ++i) {
int lower = i < NumLoBounds.Value ? LoBound[i].Value : 0;
uint size = i < NumSizes.Value ? Size[i].Value : 0;
if (lower == 0) {
if (size == 0) {
text.Add("");
} else {
text.Add($"{size}");
}
} else {
if (size == 0) {
text.Add($"{lower}...");
} else {
text.Add($"{lower}...{lower + size - 1}");
}
}
}
NodeValue = string.Join(",", text);
}
}
[Ecma("II.23.2.14")]
sealed class TypeSpecSig : CodeNode
{
public TypeSig TypeSig;
protected override void InnerRead() {
// Since TypeSpec is a subset of Type, don't reimplement it!
AddChild(nameof(TypeSig));
Children.Clear();
Children.AddRange(TypeSig.Children);
NodeValue = TypeSig.NodeValue;
Description = TypeSig.Description;
switch (TypeSig.Type) {
case ElementType.GenericInst:
if (TypeSig.GenArgCount.Value == 0) {
Errors.Add("TypeSpec GenArgCount should be non-zero");
}
break;
// case ElementType.Object:
// case ElementType.Int8:
// case ElementType.Class:
// case ElementType.MVar:
//TODO(SpecViolation) i.e. Object, Int8, Class, MVar, etc. aren't allowed in TypeSpec, but assembling i.e. `modreq (object)` creates a TypeSpec for Object
// throw new InvalidOperationException(TypeSig.Type.ToString());
}
}
}
[Ecma("II.23.2.15")]
sealed class MethodSpecSig : CodeNode
{
[Expected(0x0A)]
public byte GenericInst;
[OrderedField]
public UnsignedCompressed GenArgCount;
[OrderedField]
public TypeSig[] Types;
public override string NodeValue => $"<{string.Join(", ", Types.Select(t => t.NodeValue))}>";
protected override int GetCount(string field) => field switch {
nameof(Types) => (int)GenArgCount.Value,
_ => base.GetCount(field),
};
}
// MAYBE implement the errors from II.23.2.16 checking Short form signatures:
// No ElementType.Class then TypeRef of System.String or System.Object
// No ElementType.ValueType then TypeRef to any primitive already in ElementType or TypedByRef