/
MetadataBuilder.Tables.cs
2410 lines (2152 loc) · 114 KB
/
MetadataBuilder.Tables.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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Reflection.Internal;
namespace System.Reflection.Metadata.Ecma335
{
public partial class MetadataBuilder
{
private const byte MetadataFormatMajorVersion = 2;
private const byte MetadataFormatMinorVersion = 0;
// type system table rows:
private struct AssemblyRefTableRow { public Version Version; public BlobHandle PublicKeyToken; public StringHandle Name; public StringHandle Culture; public uint Flags; public BlobHandle HashValue; }
private struct ModuleRow { public ushort Generation; public StringHandle Name; public GuidHandle ModuleVersionId; public GuidHandle EncId; public GuidHandle EncBaseId; }
private struct AssemblyRow { public uint HashAlgorithm; public Version Version; public ushort Flags; public BlobHandle AssemblyKey; public StringHandle AssemblyName; public StringHandle AssemblyCulture; }
private struct ClassLayoutRow { public ushort PackingSize; public uint ClassSize; public int Parent; }
private struct ConstantRow { public byte Type; public int Parent; public BlobHandle Value; }
private struct CustomAttributeRow { public int Parent; public int Type; public BlobHandle Value; }
private struct DeclSecurityRow { public ushort Action; public int Parent; public BlobHandle PermissionSet; }
private struct EncLogRow { public int Token; public byte FuncCode; }
private struct EncMapRow { public int Token; }
private struct EventRow { public ushort EventFlags; public StringHandle Name; public int EventType; }
private struct EventMapRow { public int Parent; public int EventList; }
private struct ExportedTypeRow { public uint Flags; public int TypeDefId; public StringHandle TypeName; public StringHandle TypeNamespace; public int Implementation; }
private struct FieldLayoutRow { public int Offset; public int Field; }
private struct FieldMarshalRow { public int Parent; public BlobHandle NativeType; }
private struct FieldRvaRow { public int Offset; public int Field; }
private struct FieldDefRow { public ushort Flags; public StringHandle Name; public BlobHandle Signature; }
private struct FileTableRow { public uint Flags; public StringHandle FileName; public BlobHandle HashValue; }
private struct GenericParamConstraintRow { public int Owner; public int Constraint; }
private struct GenericParamRow { public ushort Number; public ushort Flags; public int Owner; public StringHandle Name; }
private struct ImplMapRow { public ushort MappingFlags; public int MemberForwarded; public StringHandle ImportName; public int ImportScope; }
private struct InterfaceImplRow { public int Class; public int Interface; }
private struct ManifestResourceRow { public uint Offset; public uint Flags; public StringHandle Name; public int Implementation; }
private struct MemberRefRow { public int Class; public StringHandle Name; public BlobHandle Signature; }
private struct MethodImplRow { public int Class; public int MethodBody; public int MethodDecl; }
private struct MethodSemanticsRow { public ushort Semantic; public int Method; public int Association; }
private struct MethodSpecRow { public int Method; public BlobHandle Instantiation; }
private struct MethodRow { public int BodyOffset; public ushort ImplFlags; public ushort Flags; public StringHandle Name; public BlobHandle Signature; public int ParamList; }
private struct ModuleRefRow { public StringHandle Name; }
private struct NestedClassRow { public int NestedClass; public int EnclosingClass; }
private struct ParamRow { public ushort Flags; public ushort Sequence; public StringHandle Name; }
private struct PropertyMapRow { public int Parent; public int PropertyList; }
private struct PropertyRow { public ushort PropFlags; public StringHandle Name; public BlobHandle Type; }
private struct TypeDefRow { public uint Flags; public StringHandle Name; public StringHandle Namespace; public int Extends; public int FieldList; public int MethodList; }
private struct TypeRefRow { public int ResolutionScope; public StringHandle Name; public StringHandle Namespace; }
private struct TypeSpecRow { public BlobHandle Signature; }
private struct StandaloneSigRow { public BlobHandle Signature; }
// debug table rows:
private struct DocumentRow { public BlobHandle Name; public GuidHandle HashAlgorithm; public BlobHandle Hash; public GuidHandle Language; }
private struct MethodDebugInformationRow { public int Document; public BlobHandle SequencePoints; }
private struct LocalScopeRow { public int Method; public int ImportScope; public int VariableList; public int ConstantList; public int StartOffset; public int Length; }
private struct LocalVariableRow { public ushort Attributes; public ushort Index; public StringHandle Name; }
private struct LocalConstantRow { public StringHandle Name; public BlobHandle Signature; }
private struct ImportScopeRow { public int Parent; public BlobHandle Imports; }
private struct StateMachineMethodRow { public int MoveNextMethod; public int KickoffMethod; }
private struct CustomDebugInformationRow { public int Parent; public GuidHandle Kind; public BlobHandle Value; }
// type system tables:
private ModuleRow? _moduleRow;
private AssemblyRow? _assemblyRow;
private readonly List<ClassLayoutRow> _classLayoutTable = new List<ClassLayoutRow>();
private readonly List<ConstantRow> _constantTable = new List<ConstantRow>();
private int _constantTableLastParent;
private bool _constantTableNeedsSorting;
private readonly List<CustomAttributeRow> _customAttributeTable = new List<CustomAttributeRow>();
private int _customAttributeTableLastParent;
private bool _customAttributeTableNeedsSorting;
private readonly List<DeclSecurityRow> _declSecurityTable = new List<DeclSecurityRow>();
private int _declSecurityTableLastParent;
private bool _declSecurityTableNeedsSorting;
private readonly List<EncLogRow> _encLogTable = new List<EncLogRow>();
private readonly List<EncMapRow> _encMapTable = new List<EncMapRow>();
private readonly List<EventRow> _eventTable = new List<EventRow>();
private readonly List<EventMapRow> _eventMapTable = new List<EventMapRow>();
private readonly List<ExportedTypeRow> _exportedTypeTable = new List<ExportedTypeRow>();
private readonly List<FieldLayoutRow> _fieldLayoutTable = new List<FieldLayoutRow>();
private readonly List<FieldMarshalRow> _fieldMarshalTable = new List<FieldMarshalRow>();
private int _fieldMarshalTableLastParent;
private bool _fieldMarshalTableNeedsSorting;
private readonly List<FieldRvaRow> _fieldRvaTable = new List<FieldRvaRow>();
private readonly List<FieldDefRow> _fieldTable = new List<FieldDefRow>();
private readonly List<FileTableRow> _fileTable = new List<FileTableRow>();
private readonly List<GenericParamConstraintRow> _genericParamConstraintTable = new List<GenericParamConstraintRow>();
private readonly List<GenericParamRow> _genericParamTable = new List<GenericParamRow>();
private readonly List<ImplMapRow> _implMapTable = new List<ImplMapRow>();
private readonly List<InterfaceImplRow> _interfaceImplTable = new List<InterfaceImplRow>();
private readonly List<ManifestResourceRow> _manifestResourceTable = new List<ManifestResourceRow>();
private readonly List<MemberRefRow> _memberRefTable = new List<MemberRefRow>();
private readonly List<MethodImplRow> _methodImplTable = new List<MethodImplRow>();
private readonly List<MethodSemanticsRow> _methodSemanticsTable = new List<MethodSemanticsRow>();
private int _methodSemanticsTableLastAssociation;
private bool _methodSemanticsTableNeedsSorting;
private readonly List<MethodSpecRow> _methodSpecTable = new List<MethodSpecRow>();
private readonly List<MethodRow> _methodDefTable = new List<MethodRow>();
private readonly List<ModuleRefRow> _moduleRefTable = new List<ModuleRefRow>();
private readonly List<NestedClassRow> _nestedClassTable = new List<NestedClassRow>();
private readonly List<ParamRow> _paramTable = new List<ParamRow>();
private readonly List<PropertyMapRow> _propertyMapTable = new List<PropertyMapRow>();
private readonly List<PropertyRow> _propertyTable = new List<PropertyRow>();
private readonly List<TypeDefRow> _typeDefTable = new List<TypeDefRow>();
private readonly List<TypeRefRow> _typeRefTable = new List<TypeRefRow>();
private readonly List<TypeSpecRow> _typeSpecTable = new List<TypeSpecRow>();
private readonly List<AssemblyRefTableRow> _assemblyRefTable = new List<AssemblyRefTableRow>();
private readonly List<StandaloneSigRow> _standAloneSigTable = new List<StandaloneSigRow>();
// debug tables:
private readonly List<DocumentRow> _documentTable = new List<DocumentRow>();
private readonly List<MethodDebugInformationRow> _methodDebugInformationTable = new List<MethodDebugInformationRow>();
private readonly List<LocalScopeRow> _localScopeTable = new List<LocalScopeRow>();
private readonly List<LocalVariableRow> _localVariableTable = new List<LocalVariableRow>();
private readonly List<LocalConstantRow> _localConstantTable = new List<LocalConstantRow>();
private readonly List<ImportScopeRow> _importScopeTable = new List<ImportScopeRow>();
private readonly List<StateMachineMethodRow> _stateMachineMethodTable = new List<StateMachineMethodRow>();
private readonly List<CustomDebugInformationRow> _customDebugInformationTable = new List<CustomDebugInformationRow>();
/// <summary>
/// Sets the capacity of the specified table.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="table"/> is not a valid table index.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="rowCount"/> is negative.</exception>
/// <remarks>
/// Use to reduce allocations if the approximate number of rows is known ahead of time.
/// </remarks>
public void SetCapacity(TableIndex table, int rowCount)
{
if (rowCount < 0)
{
Throw.ArgumentOutOfRange(nameof(rowCount));
}
switch (table)
{
case TableIndex.Module: break; // no-op, max row count is 1
case TableIndex.TypeRef: SetTableCapacity(_typeRefTable, rowCount); break;
case TableIndex.TypeDef: SetTableCapacity(_typeDefTable, rowCount); break;
case TableIndex.Field: SetTableCapacity(_fieldTable, rowCount); break;
case TableIndex.MethodDef: SetTableCapacity(_methodDefTable, rowCount); break;
case TableIndex.Param: SetTableCapacity(_paramTable, rowCount); break;
case TableIndex.InterfaceImpl: SetTableCapacity(_interfaceImplTable, rowCount); break;
case TableIndex.MemberRef: SetTableCapacity(_memberRefTable, rowCount); break;
case TableIndex.Constant: SetTableCapacity(_constantTable, rowCount); break;
case TableIndex.CustomAttribute: SetTableCapacity(_customAttributeTable, rowCount); break;
case TableIndex.FieldMarshal: SetTableCapacity(_fieldMarshalTable, rowCount); break;
case TableIndex.DeclSecurity: SetTableCapacity(_declSecurityTable, rowCount); break;
case TableIndex.ClassLayout: SetTableCapacity(_classLayoutTable, rowCount); break;
case TableIndex.FieldLayout: SetTableCapacity(_fieldLayoutTable, rowCount); break;
case TableIndex.StandAloneSig: SetTableCapacity(_standAloneSigTable, rowCount); break;
case TableIndex.EventMap: SetTableCapacity(_eventMapTable, rowCount); break;
case TableIndex.Event: SetTableCapacity(_eventTable, rowCount); break;
case TableIndex.PropertyMap: SetTableCapacity(_propertyMapTable, rowCount); break;
case TableIndex.Property: SetTableCapacity(_propertyTable, rowCount); break;
case TableIndex.MethodSemantics: SetTableCapacity(_methodSemanticsTable, rowCount); break;
case TableIndex.MethodImpl: SetTableCapacity(_methodImplTable, rowCount); break;
case TableIndex.ModuleRef: SetTableCapacity(_moduleRefTable, rowCount); break;
case TableIndex.TypeSpec: SetTableCapacity(_typeSpecTable, rowCount); break;
case TableIndex.ImplMap: SetTableCapacity(_implMapTable, rowCount); break;
case TableIndex.FieldRva: SetTableCapacity(_fieldRvaTable, rowCount); break;
case TableIndex.EncLog: SetTableCapacity(_encLogTable, rowCount); break;
case TableIndex.EncMap: SetTableCapacity(_encMapTable, rowCount); break;
case TableIndex.Assembly: break; // no-op, max row count is 1
case TableIndex.AssemblyRef: SetTableCapacity(_assemblyRefTable, rowCount); break;
case TableIndex.File: SetTableCapacity(_fileTable, rowCount); break;
case TableIndex.ExportedType: SetTableCapacity(_exportedTypeTable, rowCount); break;
case TableIndex.ManifestResource: SetTableCapacity(_manifestResourceTable, rowCount); break;
case TableIndex.NestedClass: SetTableCapacity(_nestedClassTable, rowCount); break;
case TableIndex.GenericParam: SetTableCapacity(_genericParamTable, rowCount); break;
case TableIndex.MethodSpec: SetTableCapacity(_methodSpecTable, rowCount); break;
case TableIndex.GenericParamConstraint: SetTableCapacity(_genericParamConstraintTable, rowCount); break;
case TableIndex.Document: SetTableCapacity(_documentTable, rowCount); break;
case TableIndex.MethodDebugInformation: SetTableCapacity(_methodDebugInformationTable, rowCount); break;
case TableIndex.LocalScope: SetTableCapacity(_localScopeTable, rowCount); break;
case TableIndex.LocalVariable: SetTableCapacity(_localVariableTable, rowCount); break;
case TableIndex.LocalConstant: SetTableCapacity(_localConstantTable, rowCount); break;
case TableIndex.ImportScope: SetTableCapacity(_importScopeTable, rowCount); break;
case TableIndex.StateMachineMethod: SetTableCapacity(_stateMachineMethodTable, rowCount); break;
case TableIndex.CustomDebugInformation: SetTableCapacity(_customDebugInformationTable, rowCount); break;
case TableIndex.AssemblyOS:
case TableIndex.AssemblyProcessor:
case TableIndex.AssemblyRefOS:
case TableIndex.AssemblyRefProcessor:
case TableIndex.EventPtr:
case TableIndex.FieldPtr:
case TableIndex.MethodPtr:
case TableIndex.ParamPtr:
case TableIndex.PropertyPtr:
// these tables are currently not serialized
break;
default:
throw new ArgumentOutOfRangeException(nameof(table));
}
}
private static void SetTableCapacity<T>(List<T> table, int rowCount)
{
if (rowCount > table.Count)
{
table.Capacity = rowCount;
}
}
/// <summary>
/// Returns the current number of entries in the specified table.
/// </summary>
/// <param name="table">Table index.</param>
/// <returns>The number of entries in the table.</returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="table"/> is not a valid table index.</exception>
public int GetRowCount(TableIndex table)
{
switch (table)
{
case TableIndex.Assembly : return _assemblyRow.HasValue ? 1 : 0;
case TableIndex.AssemblyRef : return _assemblyRefTable.Count;
case TableIndex.ClassLayout : return _classLayoutTable.Count;
case TableIndex.Constant : return _constantTable.Count;
case TableIndex.CustomAttribute : return _customAttributeTable.Count;
case TableIndex.DeclSecurity : return _declSecurityTable.Count;
case TableIndex.EncLog : return _encLogTable.Count;
case TableIndex.EncMap : return _encMapTable.Count;
case TableIndex.EventMap : return _eventMapTable.Count;
case TableIndex.Event : return _eventTable.Count;
case TableIndex.ExportedType : return _exportedTypeTable.Count;
case TableIndex.FieldLayout : return _fieldLayoutTable.Count;
case TableIndex.FieldMarshal : return _fieldMarshalTable.Count;
case TableIndex.FieldRva : return _fieldRvaTable.Count;
case TableIndex.Field : return _fieldTable.Count;
case TableIndex.File : return _fileTable.Count;
case TableIndex.GenericParamConstraint : return _genericParamConstraintTable.Count;
case TableIndex.GenericParam : return _genericParamTable.Count;
case TableIndex.ImplMap : return _implMapTable.Count;
case TableIndex.InterfaceImpl : return _interfaceImplTable.Count;
case TableIndex.ManifestResource : return _manifestResourceTable.Count;
case TableIndex.MemberRef : return _memberRefTable.Count;
case TableIndex.MethodImpl : return _methodImplTable.Count;
case TableIndex.MethodSemantics : return _methodSemanticsTable.Count;
case TableIndex.MethodSpec : return _methodSpecTable.Count;
case TableIndex.MethodDef : return _methodDefTable.Count;
case TableIndex.ModuleRef : return _moduleRefTable.Count;
case TableIndex.Module : return _moduleRow.HasValue ? 1 : 0;
case TableIndex.NestedClass : return _nestedClassTable.Count;
case TableIndex.Param : return _paramTable.Count;
case TableIndex.PropertyMap : return _propertyMapTable.Count;
case TableIndex.Property : return _propertyTable.Count;
case TableIndex.StandAloneSig : return _standAloneSigTable.Count;
case TableIndex.TypeDef : return _typeDefTable.Count;
case TableIndex.TypeRef : return _typeRefTable.Count;
case TableIndex.TypeSpec : return _typeSpecTable.Count;
case TableIndex.Document : return _documentTable.Count;
case TableIndex.MethodDebugInformation : return _methodDebugInformationTable.Count;
case TableIndex.LocalScope : return _localScopeTable.Count;
case TableIndex.LocalVariable : return _localVariableTable.Count;
case TableIndex.LocalConstant : return _localConstantTable.Count;
case TableIndex.StateMachineMethod : return _stateMachineMethodTable.Count;
case TableIndex.ImportScope : return _importScopeTable.Count;
case TableIndex.CustomDebugInformation : return _customDebugInformationTable.Count;
case TableIndex.AssemblyOS:
case TableIndex.AssemblyProcessor:
case TableIndex.AssemblyRefOS:
case TableIndex.AssemblyRefProcessor:
case TableIndex.EventPtr:
case TableIndex.FieldPtr:
case TableIndex.MethodPtr:
case TableIndex.ParamPtr:
case TableIndex.PropertyPtr:
return 0;
default:
throw new ArgumentOutOfRangeException(nameof(table));
}
}
/// <summary>
/// Returns the current number of entries in each table.
/// </summary>
/// <returns>
/// An array of size <see cref="MetadataTokens.TableCount"/> with each item filled with the current row count of the corresponding table.
/// </returns>
public ImmutableArray<int> GetRowCounts()
{
var rowCounts = ImmutableArray.CreateBuilder<int>(MetadataTokens.TableCount);
rowCounts.Count = MetadataTokens.TableCount;
rowCounts[(int)TableIndex.Assembly] = _assemblyRow.HasValue ? 1 : 0;
rowCounts[(int)TableIndex.AssemblyRef] = _assemblyRefTable.Count;
rowCounts[(int)TableIndex.ClassLayout] = _classLayoutTable.Count;
rowCounts[(int)TableIndex.Constant] = _constantTable.Count;
rowCounts[(int)TableIndex.CustomAttribute] = _customAttributeTable.Count;
rowCounts[(int)TableIndex.DeclSecurity] = _declSecurityTable.Count;
rowCounts[(int)TableIndex.EncLog] = _encLogTable.Count;
rowCounts[(int)TableIndex.EncMap] = _encMapTable.Count;
rowCounts[(int)TableIndex.EventMap] = _eventMapTable.Count;
rowCounts[(int)TableIndex.Event] = _eventTable.Count;
rowCounts[(int)TableIndex.ExportedType] = _exportedTypeTable.Count;
rowCounts[(int)TableIndex.FieldLayout] = _fieldLayoutTable.Count;
rowCounts[(int)TableIndex.FieldMarshal] = _fieldMarshalTable.Count;
rowCounts[(int)TableIndex.FieldRva] = _fieldRvaTable.Count;
rowCounts[(int)TableIndex.Field] = _fieldTable.Count;
rowCounts[(int)TableIndex.File] = _fileTable.Count;
rowCounts[(int)TableIndex.GenericParamConstraint] = _genericParamConstraintTable.Count;
rowCounts[(int)TableIndex.GenericParam] = _genericParamTable.Count;
rowCounts[(int)TableIndex.ImplMap] = _implMapTable.Count;
rowCounts[(int)TableIndex.InterfaceImpl] = _interfaceImplTable.Count;
rowCounts[(int)TableIndex.ManifestResource] = _manifestResourceTable.Count;
rowCounts[(int)TableIndex.MemberRef] = _memberRefTable.Count;
rowCounts[(int)TableIndex.MethodImpl] = _methodImplTable.Count;
rowCounts[(int)TableIndex.MethodSemantics] = _methodSemanticsTable.Count;
rowCounts[(int)TableIndex.MethodSpec] = _methodSpecTable.Count;
rowCounts[(int)TableIndex.MethodDef] = _methodDefTable.Count;
rowCounts[(int)TableIndex.ModuleRef] = _moduleRefTable.Count;
rowCounts[(int)TableIndex.Module] = _moduleRow.HasValue ? 1 : 0;
rowCounts[(int)TableIndex.NestedClass] = _nestedClassTable.Count;
rowCounts[(int)TableIndex.Param] = _paramTable.Count;
rowCounts[(int)TableIndex.PropertyMap] = _propertyMapTable.Count;
rowCounts[(int)TableIndex.Property] = _propertyTable.Count;
rowCounts[(int)TableIndex.StandAloneSig] = _standAloneSigTable.Count;
rowCounts[(int)TableIndex.TypeDef] = _typeDefTable.Count;
rowCounts[(int)TableIndex.TypeRef] = _typeRefTable.Count;
rowCounts[(int)TableIndex.TypeSpec] = _typeSpecTable.Count;
rowCounts[(int)TableIndex.Document] = _documentTable.Count;
rowCounts[(int)TableIndex.MethodDebugInformation] = _methodDebugInformationTable.Count;
rowCounts[(int)TableIndex.LocalScope] = _localScopeTable.Count;
rowCounts[(int)TableIndex.LocalVariable] = _localVariableTable.Count;
rowCounts[(int)TableIndex.LocalConstant] = _localConstantTable.Count;
rowCounts[(int)TableIndex.StateMachineMethod] = _stateMachineMethodTable.Count;
rowCounts[(int)TableIndex.ImportScope] = _importScopeTable.Count;
rowCounts[(int)TableIndex.CustomDebugInformation] = _customDebugInformationTable.Count;
return rowCounts.MoveToImmutable();
}
#region Building
// Note on argument value checking:
//
// To avoid perf overhead we do minimal argument checking.
// We only check integer parameter bounds if we perform a narrowing conversion,
// so that we don't mangle values that match the Add method signature but are too big to represent in the metadata.
// We do not validate ranges of enum values, we just ignore high bits when converting to smaller integral type.
// Adding such checks would unlikely catch bugs in reasonably written code.
public ModuleDefinitionHandle AddModule(
int generation,
StringHandle moduleName,
GuidHandle mvid,
GuidHandle encId,
GuidHandle encBaseId)
{
if (unchecked((uint)generation) > ushort.MaxValue)
{
Throw.ArgumentOutOfRange(nameof(generation));
}
if (_moduleRow.HasValue)
{
Throw.InvalidOperation(SR.ModuleAlreadyAdded);
}
_moduleRow = new ModuleRow
{
Generation = (ushort)generation,
Name = moduleName,
ModuleVersionId = mvid,
EncId = encId,
EncBaseId = encBaseId,
};
return EntityHandle.ModuleDefinition;
}
public AssemblyDefinitionHandle AddAssembly(
StringHandle name,
Version version,
StringHandle culture,
BlobHandle publicKey,
AssemblyFlags flags,
AssemblyHashAlgorithm hashAlgorithm)
{
if (version is null)
{
Throw.ArgumentNull(nameof(version));
}
if (_assemblyRow.HasValue)
{
Throw.InvalidOperation(SR.AssemblyAlreadyAdded);
}
_assemblyRow = new AssemblyRow
{
Flags = unchecked((ushort)flags),
HashAlgorithm = unchecked((uint)hashAlgorithm),
Version = version,
AssemblyKey = publicKey,
AssemblyName = name,
AssemblyCulture = culture
};
return EntityHandle.AssemblyDefinition;
}
public AssemblyReferenceHandle AddAssemblyReference(
StringHandle name,
Version version,
StringHandle culture,
BlobHandle publicKeyOrToken,
AssemblyFlags flags,
BlobHandle hashValue)
{
if (version is null)
{
Throw.ArgumentNull(nameof(version));
}
_assemblyRefTable.Add(new AssemblyRefTableRow
{
Name = name,
Version = version,
Culture = culture,
PublicKeyToken = publicKeyOrToken,
Flags = unchecked((uint)flags),
HashValue = hashValue
});
return AssemblyReferenceHandle.FromRowId(_assemblyRefTable.Count);
}
/// <summary>
/// Adds a type definition.
/// </summary>
/// <param name="attributes">Attributes</param>
/// <param name="namespace">Namespace</param>
/// <param name="name">Type name</param>
/// <param name="baseType"><see cref="TypeDefinitionHandle"/>, <see cref="TypeReferenceHandle"/>, <see cref="TypeSpecificationHandle"/> or nil.</param>
/// <param name="fieldList">
/// If the type declares fields the handle of the first one, otherwise the handle of the first field declared by the next type definition.
/// If no type defines any fields in the module, <see cref="MetadataTokens.FieldDefinitionHandle(int)"/>(1).
/// </param>
/// <param name="methodList">
/// If the type declares methods the handle of the first one, otherwise the handle of the first method declared by the next type definition.
/// If no type defines any methods in the module, <see cref="MetadataTokens.MethodDefinitionHandle(int)"/>(1).
/// </param>
/// <exception cref="ArgumentException"><paramref name="baseType"/> doesn't have the expected handle kind.</exception>
public TypeDefinitionHandle AddTypeDefinition(
TypeAttributes attributes,
StringHandle @namespace,
StringHandle name,
EntityHandle baseType,
FieldDefinitionHandle fieldList,
MethodDefinitionHandle methodList)
{
_typeDefTable.Add(new TypeDefRow
{
Flags = unchecked((uint)attributes),
Name = name,
Namespace = @namespace,
Extends = baseType.IsNil ? 0 : CodedIndex.TypeDefOrRefOrSpec(baseType),
FieldList = fieldList.RowId,
MethodList = methodList.RowId
});
return TypeDefinitionHandle.FromRowId(_typeDefTable.Count);
}
/// <summary>
/// Defines a type layout of a type definition.
/// </summary>
/// <param name="type">Type definition.</param>
/// <param name="packingSize">
/// Specifies that fields should be placed within the type instance at byte addresses which are a multiple of the value,
/// or at natural alignment for that field type, whichever is smaller. Shall be one of the following: 0, 1, 2, 4, 8, 16, 32, 64, or 128.
/// A value of zero indicates that the packing size used should match the default for the current platform.
/// </param>
/// <param name="size">
/// Indicates a minimum size of the type instance, and is intended to allow for padding.
/// The amount of memory allocated is the maximum of the size calculated from the layout and <paramref name="size"/>.
/// Note that if this directive applies to a value type, then the size shall be less than 1 MB.
/// </param>
/// <remarks>
/// Entries must be added in the same order as the corresponding type definitions.
/// </remarks>
public void AddTypeLayout(
TypeDefinitionHandle type,
ushort packingSize,
uint size)
{
_classLayoutTable.Add(new ClassLayoutRow
{
Parent = type.RowId,
PackingSize = packingSize,
ClassSize = size
});
}
/// <summary>
/// Adds an interface implementation to a type.
/// </summary>
/// <param name="type">The type implementing the interface.</param>
/// <param name="implementedInterface">
/// The interface being implemented:
/// <see cref="TypeDefinitionHandle"/>, <see cref="TypeReferenceHandle"/> or <see cref="TypeSpecificationHandle"/>.
/// </param>
/// <remarks>
/// Interface implementations must be added in the same order as the corresponding type definitions implementing the interface.
/// If a type implements multiple interfaces the corresponding entries must be added in the order determined by their coded indices (<see cref="CodedIndex.TypeDefOrRefOrSpec"/>).
/// </remarks>
/// <exception cref="ArgumentException"><paramref name="implementedInterface"/> doesn't have the expected handle kind.</exception>
public InterfaceImplementationHandle AddInterfaceImplementation(
TypeDefinitionHandle type,
EntityHandle implementedInterface)
{
_interfaceImplTable.Add(new InterfaceImplRow
{
Class = type.RowId,
Interface = CodedIndex.TypeDefOrRefOrSpec(implementedInterface)
});
return InterfaceImplementationHandle.FromRowId(_interfaceImplTable.Count);
}
/// <summary>
/// Defines a nesting relationship to specified type definitions.
/// </summary>
/// <param name="type">The nested type definition handle.</param>
/// <param name="enclosingType">The enclosing type definition handle.</param>
/// <remarks>
/// Entries must be added in the same order as the corresponding nested type definitions.
/// </remarks>
public void AddNestedType(
TypeDefinitionHandle type,
TypeDefinitionHandle enclosingType)
{
_nestedClassTable.Add(new NestedClassRow
{
NestedClass = type.RowId,
EnclosingClass = enclosingType.RowId
});
}
/// <summary>
/// Add a type reference.
/// </summary>
/// <param name="resolutionScope">
/// The entity declaring the target type:
/// <see cref="ModuleDefinitionHandle"/>, <see cref="ModuleReferenceHandle"/>, <see cref="AssemblyReferenceHandle"/>, <see cref="TypeReferenceHandle"/>, or nil.
/// </param>
/// <param name="namespace">Namespace.</param>
/// <param name="name">Type name.</param>
/// <exception cref="ArgumentException"><paramref name="resolutionScope"/> doesn't have the expected handle kind.</exception>
public TypeReferenceHandle AddTypeReference(
EntityHandle resolutionScope,
StringHandle @namespace,
StringHandle name)
{
_typeRefTable.Add(new TypeRefRow
{
ResolutionScope = resolutionScope.IsNil ? 0 : CodedIndex.ResolutionScope(resolutionScope),
Name = name,
Namespace = @namespace
});
return TypeReferenceHandle.FromRowId(_typeRefTable.Count);
}
public TypeSpecificationHandle AddTypeSpecification(BlobHandle signature)
{
_typeSpecTable.Add(new TypeSpecRow
{
Signature = signature
});
return TypeSpecificationHandle.FromRowId(_typeSpecTable.Count);
}
public StandaloneSignatureHandle AddStandaloneSignature(BlobHandle signature)
{
_standAloneSigTable.Add(new StandaloneSigRow
{
Signature = signature
});
return StandaloneSignatureHandle.FromRowId(_standAloneSigTable.Count);
}
/// <summary>
/// Adds a property definition.
/// </summary>
/// <param name="attributes">Attributes</param>
/// <param name="name">Name</param>
/// <param name="signature">Signature of the property.</param>
public PropertyDefinitionHandle AddProperty(PropertyAttributes attributes, StringHandle name, BlobHandle signature)
{
_propertyTable.Add(new PropertyRow
{
PropFlags = unchecked((ushort)attributes),
Name = name,
Type = signature
});
return PropertyDefinitionHandle.FromRowId(_propertyTable.Count);
}
public void AddPropertyMap(TypeDefinitionHandle declaringType, PropertyDefinitionHandle propertyList)
{
_propertyMapTable.Add(new PropertyMapRow
{
Parent = declaringType.RowId,
PropertyList = propertyList.RowId
});
}
/// <summary>
/// Adds an event definition.
/// </summary>
/// <param name="attributes">Attributes</param>
/// <param name="name">Name</param>
/// <param name="type">Type of the event: <see cref="TypeDefinitionHandle"/>, <see cref="TypeReferenceHandle"/>, or <see cref="TypeSpecificationHandle"/></param>
/// <exception cref="ArgumentException"><paramref name="type"/> doesn't have the expected handle kind.</exception>
public EventDefinitionHandle AddEvent(EventAttributes attributes, StringHandle name, EntityHandle type)
{
_eventTable.Add(new EventRow
{
EventFlags = unchecked((ushort)attributes),
Name = name,
EventType = CodedIndex.TypeDefOrRefOrSpec(type)
});
return EventDefinitionHandle.FromRowId(_eventTable.Count);
}
public void AddEventMap(TypeDefinitionHandle declaringType, EventDefinitionHandle eventList)
{
_eventMapTable.Add(new EventMapRow
{
Parent = declaringType.RowId,
EventList = eventList.RowId
});
}
/// <summary>
/// Adds a default value for a parameter, field or property.
/// </summary>
/// <param name="parent"><see cref="ParameterHandle"/>, <see cref="FieldDefinitionHandle"/>, or <see cref="PropertyDefinitionHandle"/></param>
/// <param name="value">The constant value.</param>
/// <remarks>
/// Entries may be added in any order. The table is automatically sorted when serialized.
/// </remarks>
/// <exception cref="ArgumentException"><paramref name="parent"/> doesn't have the expected handle kind.</exception>
public ConstantHandle AddConstant(EntityHandle parent, object? value)
{
int parentCodedIndex = CodedIndex.HasConstant(parent);
// the table is required to be sorted by Parent:
_constantTableNeedsSorting |= parentCodedIndex < _constantTableLastParent;
_constantTableLastParent = parentCodedIndex;
_constantTable.Add(new ConstantRow
{
Type = (byte)MetadataWriterUtilities.GetConstantTypeCode(value),
Parent = parentCodedIndex,
Value = GetOrAddConstantBlob(value)
});
return ConstantHandle.FromRowId(_constantTable.Count);
}
/// <summary>
/// Associates a method (a getter, a setter, an adder, etc.) with a property or an event.
/// </summary>
/// <param name="association"><see cref="EventDefinitionHandle"/> or <see cref="PropertyDefinitionHandle"/>.</param>
/// <param name="semantics">Semantics.</param>
/// <param name="methodDefinition">Method definition.</param>
/// <exception cref="ArgumentException"><paramref name="association"/> doesn't have the expected handle kind.</exception>
/// <remarks>
/// Entries may be added in any order. The table is automatically sorted when serialized.
/// </remarks>
public void AddMethodSemantics(EntityHandle association, MethodSemanticsAttributes semantics, MethodDefinitionHandle methodDefinition)
{
int associationCodedIndex = CodedIndex.HasSemantics(association);
// the table is required to be sorted by Association:
_methodSemanticsTableNeedsSorting |= associationCodedIndex < _methodSemanticsTableLastAssociation;
_methodSemanticsTableLastAssociation = associationCodedIndex;
_methodSemanticsTable.Add(new MethodSemanticsRow
{
Association = associationCodedIndex,
Method = methodDefinition.RowId,
Semantic = unchecked((ushort)semantics)
});
}
/// <summary>
/// Add a custom attribute.
/// </summary>
/// <param name="parent">
/// An entity to attach the custom attribute to:
/// <see cref="MethodDefinitionHandle"/>,
/// <see cref="FieldDefinitionHandle"/>,
/// <see cref="TypeReferenceHandle"/>,
/// <see cref="TypeDefinitionHandle"/>,
/// <see cref="ParameterHandle"/>,
/// <see cref="InterfaceImplementationHandle"/>,
/// <see cref="MemberReferenceHandle"/>,
/// <see cref="ModuleDefinitionHandle"/>,
/// <see cref="DeclarativeSecurityAttributeHandle"/>,
/// <see cref="PropertyDefinitionHandle"/>,
/// <see cref="EventDefinitionHandle"/>,
/// <see cref="StandaloneSignatureHandle"/>,
/// <see cref="ModuleReferenceHandle"/>,
/// <see cref="TypeSpecificationHandle"/>,
/// <see cref="AssemblyDefinitionHandle"/>,
/// <see cref="AssemblyReferenceHandle"/>,
/// <see cref="AssemblyFileHandle"/>,
/// <see cref="ExportedTypeHandle"/>,
/// <see cref="ManifestResourceHandle"/>,
/// <see cref="GenericParameterHandle"/>,
/// <see cref="GenericParameterConstraintHandle"/> or
/// <see cref="MethodSpecificationHandle"/>.
/// </param>
/// <param name="constructor">
/// Custom attribute constructor: <see cref="MethodDefinitionHandle"/> or <see cref="MemberReferenceHandle"/>
/// </param>
/// <param name="value">
/// Custom attribute value blob.
/// </param>
/// <remarks>
/// Entries may be added in any order. The table is automatically sorted when serialized.
/// </remarks>
/// <exception cref="ArgumentException"><paramref name="parent"/> doesn't have the expected handle kind.</exception>
public CustomAttributeHandle AddCustomAttribute(EntityHandle parent, EntityHandle constructor, BlobHandle value)
{
int parentCodedIndex = CodedIndex.HasCustomAttribute(parent);
// the table is required to be sorted by Parent:
_customAttributeTableNeedsSorting |= parentCodedIndex < _customAttributeTableLastParent;
_customAttributeTableLastParent = parentCodedIndex;
_customAttributeTable.Add(new CustomAttributeRow
{
Parent = parentCodedIndex,
Type = CodedIndex.CustomAttributeType(constructor),
Value = value
});
return CustomAttributeHandle.FromRowId(_customAttributeTable.Count);
}
/// <summary>
/// Adds a method specification (instantiation).
/// </summary>
/// <param name="method">Generic method: <see cref="MethodDefinitionHandle"/> or <see cref="MemberReferenceHandle"/></param>
/// <param name="instantiation">Instantiation blob encoding the generic arguments of the method.</param>
/// <exception cref="ArgumentException"><paramref name="method"/> doesn't have the expected handle kind.</exception>
public MethodSpecificationHandle AddMethodSpecification(EntityHandle method, BlobHandle instantiation)
{
_methodSpecTable.Add(new MethodSpecRow
{
Method = CodedIndex.MethodDefOrRef(method),
Instantiation = instantiation
});
return MethodSpecificationHandle.FromRowId(_methodSpecTable.Count);
}
public ModuleReferenceHandle AddModuleReference(StringHandle moduleName)
{
_moduleRefTable.Add(new ModuleRefRow
{
Name = moduleName
});
return ModuleReferenceHandle.FromRowId(_moduleRefTable.Count);
}
/// <summary>
/// Adds a parameter definition.
/// </summary>
/// <param name="attributes"><see cref="ParameterAttributes"/></param>
/// <param name="name">Parameter name (optional).</param>
/// <param name="sequenceNumber">Sequence number of the parameter. Value of 0 refers to the owner method's return type; its parameters are then numbered from 1 onwards.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="sequenceNumber"/> is greater than <see cref="ushort.MaxValue"/>.</exception>
public ParameterHandle AddParameter(ParameterAttributes attributes, StringHandle name, int sequenceNumber)
{
if (unchecked((uint)sequenceNumber) > ushort.MaxValue)
{
Throw.ArgumentOutOfRange(nameof(sequenceNumber));
}
_paramTable.Add(new ParamRow
{
Flags = unchecked((ushort)attributes),
Name = name,
Sequence = (ushort)sequenceNumber
});
return ParameterHandle.FromRowId(_paramTable.Count);
}
/// <summary>
/// Adds a generic parameter definition.
/// </summary>
/// <param name="parent">Parent entity handle: <see cref="TypeDefinitionHandle"/> or <see cref="MethodDefinitionHandle"/></param>
/// <param name="attributes">Attributes.</param>
/// <param name="name">Parameter name.</param>
/// <param name="index">Zero-based parameter index.</param>
/// <remarks>
/// Generic parameters must be added in an order determined by the coded index of their parent entity (<see cref="CodedIndex.TypeOrMethodDef"/>).
/// Generic parameters with the same parent must be ordered by their <paramref name="index"/>.
/// </remarks>
/// <exception cref="ArgumentException"><paramref name="parent"/> doesn't have the expected handle kind.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is greater than <see cref="ushort.MaxValue"/>.</exception>
public GenericParameterHandle AddGenericParameter(
EntityHandle parent,
GenericParameterAttributes attributes,
StringHandle name,
int index)
{
if (unchecked((uint)index) > ushort.MaxValue)
{
Throw.ArgumentOutOfRange(nameof(index));
}
_genericParamTable.Add(new GenericParamRow
{
Flags = unchecked((ushort)attributes),
Name = name,
Number = (ushort)index,
Owner = CodedIndex.TypeOrMethodDef(parent)
});
return GenericParameterHandle.FromRowId(_genericParamTable.Count);
}
/// <summary>
/// Adds a type constraint to a generic parameter.
/// </summary>
/// <param name="genericParameter">Generic parameter to constrain.</param>
/// <param name="constraint">Type constraint: <see cref="TypeDefinitionHandle"/>, <see cref="TypeReferenceHandle"/> or <see cref="TypeSpecificationHandle"/></param>
/// <exception cref="ArgumentException"><paramref name="genericParameter"/> doesn't have the expected handle kind.</exception>
/// <remarks>
/// Constraints must be added in the same order as the corresponding generic parameters.
/// </remarks>
public GenericParameterConstraintHandle AddGenericParameterConstraint(
GenericParameterHandle genericParameter,
EntityHandle constraint)
{
_genericParamConstraintTable.Add(new GenericParamConstraintRow
{
Owner = genericParameter.RowId,
Constraint = CodedIndex.TypeDefOrRefOrSpec(constraint),
});
return GenericParameterConstraintHandle.FromRowId(_genericParamConstraintTable.Count);
}
/// <summary>
/// Adds a field definition.
/// </summary>
/// <param name="attributes">Field attributes.</param>
/// <param name="name">Field name.</param>
/// <param name="signature">Field signature. Use <see cref="BlobEncoder.FieldSignature"/> to construct the blob.</param>
public FieldDefinitionHandle AddFieldDefinition(
FieldAttributes attributes,
StringHandle name,
BlobHandle signature)
{
_fieldTable.Add(new FieldDefRow
{
Flags = unchecked((ushort)attributes),
Name = name,
Signature = signature
});
return FieldDefinitionHandle.FromRowId(_fieldTable.Count);
}
/// <summary>
/// Defines a field layout of a field definition.
/// </summary>
/// <param name="field">Field definition.</param>
/// <param name="offset">The byte offset of the field within the declaring type instance.</param>
/// <remarks>
/// Entries must be added in the same order as the corresponding field definitions.
/// </remarks>
public void AddFieldLayout(
FieldDefinitionHandle field,
int offset)
{
_fieldLayoutTable.Add(new FieldLayoutRow
{
Field = field.RowId,
Offset = offset
});
}
/// <summary>
/// Add marshalling information to a field or a parameter.
/// </summary>
/// <param name="parent"><see cref="ParameterHandle"/> or <see cref="FieldDefinitionHandle"/>.</param>
/// <param name="descriptor">Descriptor blob.</param>
/// <exception cref="ArgumentException"><paramref name="parent"/> doesn't have the expected handle kind.</exception>
/// <remarks>
/// Entries may be added in any order. The table is automatically sorted when serialized.
/// </remarks>
public void AddMarshallingDescriptor(
EntityHandle parent,
BlobHandle descriptor)
{
int codedIndex = CodedIndex.HasFieldMarshal(parent);
// the table is required to be sorted by Parent:
_fieldMarshalTableNeedsSorting |= codedIndex < _fieldMarshalTableLastParent;
_fieldMarshalTableLastParent = codedIndex;
_fieldMarshalTable.Add(new FieldMarshalRow
{
Parent = codedIndex,
NativeType = descriptor
});
}
/// <summary>
/// Adds a mapping from a field to its initial value stored in the PE image.
/// </summary>
/// <param name="field">Field definition handle.</param>
/// <param name="offset">
/// Offset within the block in the PE image that stores initial values of mapped fields (usually in .text section).
/// The final relative virtual address stored in the metadata is calculated when the metadata is serialized
/// by adding the offset to the virtual address of the block start.
/// </param>
/// <remarks>
/// Entries must be added in the same order as the corresponding field definitions.
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> is negative.</exception>
public void AddFieldRelativeVirtualAddress(FieldDefinitionHandle field, int offset)
{
if (offset < 0)
{
Throw.ArgumentOutOfRange(nameof(offset));
}
_fieldRvaTable.Add(new FieldRvaRow
{
Field = field.RowId,
Offset = offset
});
}
/// <summary>
/// Adds a method definition.
/// </summary>
/// <param name="attributes"><see cref="MethodAttributes"/></param>
/// <param name="implAttributes"><see cref="MethodImplAttributes"/></param>
/// <param name="name">Method name/</param>
/// <param name="signature">Method signature.</param>
/// <param name="bodyOffset">
/// Offset within the block in the PE image that stores method bodies (IL stream),
/// or -1 if the method doesn't have a body.
///
/// The final relative virtual address stored in the metadata is calculated when the metadata is serialized
/// by adding the offset to the virtual address of the beginning of the block.
/// </param>
/// <param name="parameterList">
/// If the method declares parameters in Params table the handle of the first one, otherwise the handle of the first parameter declared by the next method definition.
/// If no parameters are declared in the module, <see cref="MetadataTokens.ParameterHandle(int)"/>(1).
/// </param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="bodyOffset"/> is less than -1.</exception>
public MethodDefinitionHandle AddMethodDefinition(
MethodAttributes attributes,
MethodImplAttributes implAttributes,
StringHandle name,
BlobHandle signature,
int bodyOffset,
ParameterHandle parameterList)
{
if (bodyOffset < -1)
{
Throw.ArgumentOutOfRange(nameof(bodyOffset));
}
_methodDefTable.Add(new MethodRow
{
Flags = unchecked((ushort)attributes),