Move
[mono.git] / mcs / class / Mono.Cecil / Mono.Cecil.Metadata / MetadataRowReader.cs
1 //
2 // MetadataRowReader.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@gmail.com)
6 //
7 // Generated by /CodeGen/cecil-gen.rb do not edit
8 // Thu Feb 22 14:39:38 CET 2007
9 //
10 // (C) 2005 Jb Evain
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 namespace Mono.Cecil.Metadata {
33
34         using System.Collections;
35         using System.IO;
36
37         using Mono.Cecil.Binary;
38
39         class MetadataRowReader : BaseMetadataRowVisitor {
40
41                 MetadataTableReader m_mtrv;
42                 BinaryReader m_binaryReader;
43                 MetadataRoot m_metadataRoot;
44                 IDictionary m_ciCache;
45
46                 int m_blobHeapIdxSz;
47                 int m_stringsHeapIdxSz;
48                 int m_guidHeapIdxSz;
49
50                 public MetadataRowReader (MetadataTableReader mtrv)
51                 {
52                         m_mtrv = mtrv;
53                         m_binaryReader = mtrv.GetReader ();
54                         m_metadataRoot = mtrv.GetMetadataRoot ();
55                         m_ciCache = new Hashtable ();
56                 }
57
58                 int GetIndexSize (int rid)
59                 {
60                         return m_mtrv.GetNumberOfRows (rid) < (1 << 16) ? 2 : 4;
61                 }
62
63                 int GetCodedIndexSize (CodedIndex ci)
64                 {
65                         return Utilities.GetCodedIndexSize (ci,
66                                 new Utilities.TableRowCounter (m_mtrv.GetNumberOfRows), m_ciCache);
67                 }
68
69                 uint ReadByIndexSize (int size)
70                 {
71                         if (size == 2) {
72                                 return (uint) m_binaryReader.ReadUInt16 ();
73                         } else if (size == 4) {
74                                 return m_binaryReader.ReadUInt32 ();
75                         } else {
76                                 throw new MetadataFormatException ("Non valid size for indexing");
77                         }
78                 }
79
80                 public override void VisitRowCollection (RowCollection coll)
81                 {
82                         m_blobHeapIdxSz = m_metadataRoot.Streams.BlobHeap != null ?
83                                 m_metadataRoot.Streams.BlobHeap.IndexSize : 2;
84                         m_stringsHeapIdxSz = m_metadataRoot.Streams.StringsHeap != null ?
85                                 m_metadataRoot.Streams.StringsHeap.IndexSize : 2;
86                         m_guidHeapIdxSz = m_metadataRoot.Streams.GuidHeap != null ?
87                                 m_metadataRoot.Streams.GuidHeap.IndexSize : 2;
88                 }
89
90                 public override void VisitAssemblyRow (AssemblyRow row)
91                 {
92                         row.HashAlgId = (Mono.Cecil.AssemblyHashAlgorithm) m_binaryReader.ReadUInt32 ();
93                         row.MajorVersion = m_binaryReader.ReadUInt16 ();
94                         row.MinorVersion = m_binaryReader.ReadUInt16 ();
95                         row.BuildNumber = m_binaryReader.ReadUInt16 ();
96                         row.RevisionNumber = m_binaryReader.ReadUInt16 ();
97                         row.Flags = (Mono.Cecil.AssemblyFlags) m_binaryReader.ReadUInt32 ();
98                         row.PublicKey = ReadByIndexSize (m_blobHeapIdxSz);
99                         row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
100                         row.Culture = ReadByIndexSize (m_stringsHeapIdxSz);
101                 }
102                 public override void VisitAssemblyOSRow (AssemblyOSRow row)
103                 {
104                         row.OSPlatformID = m_binaryReader.ReadUInt32 ();
105                         row.OSMajorVersion = m_binaryReader.ReadUInt32 ();
106                         row.OSMinorVersion = m_binaryReader.ReadUInt32 ();
107                 }
108                 public override void VisitAssemblyProcessorRow (AssemblyProcessorRow row)
109                 {
110                         row.Processor = m_binaryReader.ReadUInt32 ();
111                 }
112                 public override void VisitAssemblyRefRow (AssemblyRefRow row)
113                 {
114                         row.MajorVersion = m_binaryReader.ReadUInt16 ();
115                         row.MinorVersion = m_binaryReader.ReadUInt16 ();
116                         row.BuildNumber = m_binaryReader.ReadUInt16 ();
117                         row.RevisionNumber = m_binaryReader.ReadUInt16 ();
118                         row.Flags = (Mono.Cecil.AssemblyFlags) m_binaryReader.ReadUInt32 ();
119                         row.PublicKeyOrToken = ReadByIndexSize (m_blobHeapIdxSz);
120                         row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
121                         row.Culture = ReadByIndexSize (m_stringsHeapIdxSz);
122                         row.HashValue = ReadByIndexSize (m_blobHeapIdxSz);
123                 }
124                 public override void VisitAssemblyRefOSRow (AssemblyRefOSRow row)
125                 {
126                         row.OSPlatformID = m_binaryReader.ReadUInt32 ();
127                         row.OSMajorVersion = m_binaryReader.ReadUInt32 ();
128                         row.OSMinorVersion = m_binaryReader.ReadUInt32 ();
129                         row.AssemblyRef = ReadByIndexSize (GetIndexSize (AssemblyRefTable.RId));
130                 }
131                 public override void VisitAssemblyRefProcessorRow (AssemblyRefProcessorRow row)
132                 {
133                         row.Processor = m_binaryReader.ReadUInt32 ();
134                         row.AssemblyRef = ReadByIndexSize (GetIndexSize (AssemblyRefTable.RId));
135                 }
136                 public override void VisitClassLayoutRow (ClassLayoutRow row)
137                 {
138                         row.PackingSize = m_binaryReader.ReadUInt16 ();
139                         row.ClassSize = m_binaryReader.ReadUInt32 ();
140                         row.Parent = ReadByIndexSize (GetIndexSize (TypeDefTable.RId));
141                 }
142                 public override void VisitConstantRow (ConstantRow row)
143                 {
144                         row.Type = (Mono.Cecil.Metadata.ElementType) m_binaryReader.ReadUInt16 ();
145                         row.Parent = Utilities.GetMetadataToken (CodedIndex.HasConstant,
146                                 ReadByIndexSize (GetCodedIndexSize (CodedIndex.HasConstant)));
147                         row.Value = ReadByIndexSize (m_blobHeapIdxSz);
148                 }
149                 public override void VisitCustomAttributeRow (CustomAttributeRow row)
150                 {
151                         row.Parent = Utilities.GetMetadataToken (CodedIndex.HasCustomAttribute,
152                                 ReadByIndexSize (GetCodedIndexSize (CodedIndex.HasCustomAttribute)));
153                         row.Type = Utilities.GetMetadataToken (CodedIndex.CustomAttributeType,
154                                 ReadByIndexSize (GetCodedIndexSize (CodedIndex.CustomAttributeType)));
155                         row.Value = ReadByIndexSize (m_blobHeapIdxSz);
156                 }
157                 public override void VisitDeclSecurityRow (DeclSecurityRow row)
158                 {
159                         row.Action = (Mono.Cecil.SecurityAction) m_binaryReader.ReadInt16 ();
160                         row.Parent = Utilities.GetMetadataToken (CodedIndex.HasDeclSecurity,
161                                 ReadByIndexSize (GetCodedIndexSize (CodedIndex.HasDeclSecurity)));
162                         row.PermissionSet = ReadByIndexSize (m_blobHeapIdxSz);
163                 }
164                 public override void VisitEventRow (EventRow row)
165                 {
166                         row.EventFlags = (Mono.Cecil.EventAttributes) m_binaryReader.ReadUInt16 ();
167                         row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
168                         row.EventType = Utilities.GetMetadataToken (CodedIndex.TypeDefOrRef,
169                                 ReadByIndexSize (GetCodedIndexSize (CodedIndex.TypeDefOrRef)));
170                 }
171                 public override void VisitEventMapRow (EventMapRow row)
172                 {
173                         row.Parent = ReadByIndexSize (GetIndexSize (TypeDefTable.RId));
174                         row.EventList = ReadByIndexSize (GetIndexSize (EventTable.RId));
175                 }
176                 public override void VisitEventPtrRow (EventPtrRow row)
177                 {
178                         row.Event = ReadByIndexSize (GetIndexSize (EventTable.RId));
179                 }
180                 public override void VisitExportedTypeRow (ExportedTypeRow row)
181                 {
182                         row.Flags = (Mono.Cecil.TypeAttributes) m_binaryReader.ReadUInt32 ();
183                         row.TypeDefId = m_binaryReader.ReadUInt32 ();
184                         row.TypeName = ReadByIndexSize (m_stringsHeapIdxSz);
185                         row.TypeNamespace = ReadByIndexSize (m_stringsHeapIdxSz);
186                         row.Implementation = Utilities.GetMetadataToken (CodedIndex.Implementation,
187                                 ReadByIndexSize (GetCodedIndexSize (CodedIndex.Implementation)));
188                 }
189                 public override void VisitFieldRow (FieldRow row)
190                 {
191                         row.Flags = (Mono.Cecil.FieldAttributes) m_binaryReader.ReadUInt16 ();
192                         row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
193                         row.Signature = ReadByIndexSize (m_blobHeapIdxSz);
194                 }
195                 public override void VisitFieldLayoutRow (FieldLayoutRow row)
196                 {
197                         row.Offset = m_binaryReader.ReadUInt32 ();
198                         row.Field = ReadByIndexSize (GetIndexSize (FieldTable.RId));
199                 }
200                 public override void VisitFieldMarshalRow (FieldMarshalRow row)
201                 {
202                         row.Parent = Utilities.GetMetadataToken (CodedIndex.HasFieldMarshal,
203                                 ReadByIndexSize (GetCodedIndexSize (CodedIndex.HasFieldMarshal)));
204                         row.NativeType = ReadByIndexSize (m_blobHeapIdxSz);
205                 }
206                 public override void VisitFieldPtrRow (FieldPtrRow row)
207                 {
208                         row.Field = ReadByIndexSize (GetIndexSize (FieldTable.RId));
209                 }
210                 public override void VisitFieldRVARow (FieldRVARow row)
211                 {
212                         row.RVA = new RVA (m_binaryReader.ReadUInt32 ());
213                         row.Field = ReadByIndexSize (GetIndexSize (FieldTable.RId));
214                 }
215                 public override void VisitFileRow (FileRow row)
216                 {
217                         row.Flags = (Mono.Cecil.FileAttributes) m_binaryReader.ReadUInt32 ();
218                         row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
219                         row.HashValue = ReadByIndexSize (m_blobHeapIdxSz);
220                 }
221                 public override void VisitGenericParamRow (GenericParamRow row)
222                 {
223                         row.Number = m_binaryReader.ReadUInt16 ();
224                         row.Flags = (Mono.Cecil.GenericParameterAttributes) m_binaryReader.ReadUInt16 ();
225                         row.Owner = Utilities.GetMetadataToken (CodedIndex.TypeOrMethodDef,
226                                 ReadByIndexSize (GetCodedIndexSize (CodedIndex.TypeOrMethodDef)));
227                         row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
228                 }
229                 public override void VisitGenericParamConstraintRow (GenericParamConstraintRow row)
230                 {
231                         row.Owner = ReadByIndexSize (GetIndexSize (GenericParamTable.RId));
232                         row.Constraint = Utilities.GetMetadataToken (CodedIndex.TypeDefOrRef,
233                                 ReadByIndexSize (GetCodedIndexSize (CodedIndex.TypeDefOrRef)));
234                 }
235                 public override void VisitImplMapRow (ImplMapRow row)
236                 {
237                         row.MappingFlags = (Mono.Cecil.PInvokeAttributes) m_binaryReader.ReadUInt16 ();
238                         row.MemberForwarded = Utilities.GetMetadataToken (CodedIndex.MemberForwarded,
239                                 ReadByIndexSize (GetCodedIndexSize (CodedIndex.MemberForwarded)));
240                         row.ImportName = ReadByIndexSize (m_stringsHeapIdxSz);
241                         row.ImportScope = ReadByIndexSize (GetIndexSize (ModuleRefTable.RId));
242                 }
243                 public override void VisitInterfaceImplRow (InterfaceImplRow row)
244                 {
245                         row.Class = ReadByIndexSize (GetIndexSize (TypeDefTable.RId));
246                         row.Interface = Utilities.GetMetadataToken (CodedIndex.TypeDefOrRef,
247                                 ReadByIndexSize (GetCodedIndexSize (CodedIndex.TypeDefOrRef)));
248                 }
249                 public override void VisitManifestResourceRow (ManifestResourceRow row)
250                 {
251                         row.Offset = m_binaryReader.ReadUInt32 ();
252                         row.Flags = (Mono.Cecil.ManifestResourceAttributes) m_binaryReader.ReadUInt32 ();
253                         row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
254                         row.Implementation = Utilities.GetMetadataToken (CodedIndex.Implementation,
255                                 ReadByIndexSize (GetCodedIndexSize (CodedIndex.Implementation)));
256                 }
257                 public override void VisitMemberRefRow (MemberRefRow row)
258                 {
259                         row.Class = Utilities.GetMetadataToken (CodedIndex.MemberRefParent,
260                                 ReadByIndexSize (GetCodedIndexSize (CodedIndex.MemberRefParent)));
261                         row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
262                         row.Signature = ReadByIndexSize (m_blobHeapIdxSz);
263                 }
264                 public override void VisitMethodRow (MethodRow row)
265                 {
266                         row.RVA = new RVA (m_binaryReader.ReadUInt32 ());
267                         row.ImplFlags = (Mono.Cecil.MethodImplAttributes) m_binaryReader.ReadUInt16 ();
268                         row.Flags = (Mono.Cecil.MethodAttributes) m_binaryReader.ReadUInt16 ();
269                         row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
270                         row.Signature = ReadByIndexSize (m_blobHeapIdxSz);
271                         row.ParamList = ReadByIndexSize (GetIndexSize (ParamTable.RId));
272                 }
273                 public override void VisitMethodImplRow (MethodImplRow row)
274                 {
275                         row.Class = ReadByIndexSize (GetIndexSize (TypeDefTable.RId));
276                         row.MethodBody = Utilities.GetMetadataToken (CodedIndex.MethodDefOrRef,
277                                 ReadByIndexSize (GetCodedIndexSize (CodedIndex.MethodDefOrRef)));
278                         row.MethodDeclaration = Utilities.GetMetadataToken (CodedIndex.MethodDefOrRef,
279                                 ReadByIndexSize (GetCodedIndexSize (CodedIndex.MethodDefOrRef)));
280                 }
281                 public override void VisitMethodPtrRow (MethodPtrRow row)
282                 {
283                         row.Method = ReadByIndexSize (GetIndexSize (MethodTable.RId));
284                 }
285                 public override void VisitMethodSemanticsRow (MethodSemanticsRow row)
286                 {
287                         row.Semantics = (Mono.Cecil.MethodSemanticsAttributes) m_binaryReader.ReadUInt16 ();
288                         row.Method = ReadByIndexSize (GetIndexSize (MethodTable.RId));
289                         row.Association = Utilities.GetMetadataToken (CodedIndex.HasSemantics,
290                                 ReadByIndexSize (GetCodedIndexSize (CodedIndex.HasSemantics)));
291                 }
292                 public override void VisitMethodSpecRow (MethodSpecRow row)
293                 {
294                         row.Method = Utilities.GetMetadataToken (CodedIndex.MethodDefOrRef,
295                                 ReadByIndexSize (GetCodedIndexSize (CodedIndex.MethodDefOrRef)));
296                         row.Instantiation = ReadByIndexSize (m_blobHeapIdxSz);
297                 }
298                 public override void VisitModuleRow (ModuleRow row)
299                 {
300                         row.Generation = m_binaryReader.ReadUInt16 ();
301                         row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
302                         row.Mvid = ReadByIndexSize (m_guidHeapIdxSz);
303                         row.EncId = ReadByIndexSize (m_guidHeapIdxSz);
304                         row.EncBaseId = ReadByIndexSize (m_guidHeapIdxSz);
305                 }
306                 public override void VisitModuleRefRow (ModuleRefRow row)
307                 {
308                         row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
309                 }
310                 public override void VisitNestedClassRow (NestedClassRow row)
311                 {
312                         row.NestedClass = ReadByIndexSize (GetIndexSize (TypeDefTable.RId));
313                         row.EnclosingClass = ReadByIndexSize (GetIndexSize (TypeDefTable.RId));
314                 }
315                 public override void VisitParamRow (ParamRow row)
316                 {
317                         row.Flags = (Mono.Cecil.ParameterAttributes) m_binaryReader.ReadUInt16 ();
318                         row.Sequence = m_binaryReader.ReadUInt16 ();
319                         row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
320                 }
321                 public override void VisitParamPtrRow (ParamPtrRow row)
322                 {
323                         row.Param = ReadByIndexSize (GetIndexSize (ParamTable.RId));
324                 }
325                 public override void VisitPropertyRow (PropertyRow row)
326                 {
327                         row.Flags = (Mono.Cecil.PropertyAttributes) m_binaryReader.ReadUInt16 ();
328                         row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
329                         row.Type = ReadByIndexSize (m_blobHeapIdxSz);
330                 }
331                 public override void VisitPropertyMapRow (PropertyMapRow row)
332                 {
333                         row.Parent = ReadByIndexSize (GetIndexSize (TypeDefTable.RId));
334                         row.PropertyList = ReadByIndexSize (GetIndexSize (PropertyTable.RId));
335                 }
336                 public override void VisitPropertyPtrRow (PropertyPtrRow row)
337                 {
338                         row.Property = ReadByIndexSize (GetIndexSize (PropertyTable.RId));
339                 }
340                 public override void VisitStandAloneSigRow (StandAloneSigRow row)
341                 {
342                         row.Signature = ReadByIndexSize (m_blobHeapIdxSz);
343                 }
344                 public override void VisitTypeDefRow (TypeDefRow row)
345                 {
346                         row.Flags = (Mono.Cecil.TypeAttributes) m_binaryReader.ReadUInt32 ();
347                         row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
348                         row.Namespace = ReadByIndexSize (m_stringsHeapIdxSz);
349                         row.Extends = Utilities.GetMetadataToken (CodedIndex.TypeDefOrRef,
350                                 ReadByIndexSize (GetCodedIndexSize (CodedIndex.TypeDefOrRef)));
351                         row.FieldList = ReadByIndexSize (GetIndexSize (FieldTable.RId));
352                         row.MethodList = ReadByIndexSize (GetIndexSize (MethodTable.RId));
353                 }
354                 public override void VisitTypeRefRow (TypeRefRow row)
355                 {
356                         row.ResolutionScope = Utilities.GetMetadataToken (CodedIndex.ResolutionScope,
357                                 ReadByIndexSize (GetCodedIndexSize (CodedIndex.ResolutionScope)));
358                         row.Name = ReadByIndexSize (m_stringsHeapIdxSz);
359                         row.Namespace = ReadByIndexSize (m_stringsHeapIdxSz);
360                 }
361                 public override void VisitTypeSpecRow (TypeSpecRow row)
362                 {
363                         row.Signature = ReadByIndexSize (m_blobHeapIdxSz);
364                 }
365         }
366 }