Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Cecil/0.9.5/Mono.Cecil-0.9.5/Mono.Cecil/Mono.Cecil/MetadataSystem.cs @ 13331

Last change on this file since 13331 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 11.3 KB
Line 
1//
2// MetadataSystem.cs
3//
4// Author:
5//   Jb Evain (jbevain@gmail.com)
6//
7// Copyright (c) 2008 - 2011 Jb Evain
8//
9// Permission is hereby granted, free of charge, to any person obtaining
10// a copy of this software and associated documentation files (the
11// "Software"), to deal in the Software without restriction, including
12// without limitation the rights to use, copy, modify, merge, publish,
13// distribute, sublicense, and/or sell copies of the Software, and to
14// permit persons to whom the Software is furnished to do so, subject to
15// the following conditions:
16//
17// The above copyright notice and this permission notice shall be
18// included in all copies or substantial portions of the Software.
19//
20// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27//
28
29using System;
30using System.Collections.Generic;
31
32using Mono.Cecil.Metadata;
33
34namespace Mono.Cecil {
35
36  struct Range {
37    public uint Start;
38    public uint Length;
39
40    public Range (uint index, uint length)
41    {
42      this.Start = index;
43      this.Length = length;
44    }
45  }
46
47  sealed class MetadataSystem {
48
49    internal AssemblyNameReference [] AssemblyReferences;
50    internal ModuleReference [] ModuleReferences;
51
52    internal TypeDefinition [] Types;
53    internal TypeReference [] TypeReferences;
54
55    internal FieldDefinition [] Fields;
56    internal MethodDefinition [] Methods;
57    internal MemberReference [] MemberReferences;
58
59    internal Dictionary<uint, uint []> NestedTypes;
60    internal Dictionary<uint, uint> ReverseNestedTypes;
61    internal Dictionary<uint, MetadataToken []> Interfaces;
62    internal Dictionary<uint, Row<ushort, uint>> ClassLayouts;
63    internal Dictionary<uint, uint> FieldLayouts;
64    internal Dictionary<uint, uint> FieldRVAs;
65    internal Dictionary<MetadataToken, uint> FieldMarshals;
66    internal Dictionary<MetadataToken, Row<ElementType, uint>> Constants;
67    internal Dictionary<uint, MetadataToken []> Overrides;
68    internal Dictionary<MetadataToken, Range> CustomAttributes;
69    internal Dictionary<MetadataToken, Range> SecurityDeclarations;
70    internal Dictionary<uint, Range> Events;
71    internal Dictionary<uint, Range> Properties;
72    internal Dictionary<uint, Row<MethodSemanticsAttributes, MetadataToken>> Semantics;
73    internal Dictionary<uint, Row<PInvokeAttributes, uint, uint>> PInvokes;
74    internal Dictionary<MetadataToken, Range> GenericParameters;
75    internal Dictionary<uint, MetadataToken []> GenericConstraints;
76
77    static Dictionary<string, Row<ElementType, bool>> primitive_value_types;
78
79    static void InitializePrimitives ()
80    {
81      primitive_value_types = new Dictionary<string, Row<ElementType, bool>> (18) {
82        { "Void", new Row<ElementType, bool> (ElementType.Void, false) },
83        { "Boolean", new Row<ElementType, bool> (ElementType.Boolean, true) },
84        { "Char", new Row<ElementType, bool> (ElementType.Char, true) },
85        { "SByte", new Row<ElementType, bool> (ElementType.I1, true) },
86        { "Byte", new Row<ElementType, bool> (ElementType.U1, true) },
87        { "Int16", new Row<ElementType, bool> (ElementType.I2, true) },
88        { "UInt16", new Row<ElementType, bool> (ElementType.U2, true) },
89        { "Int32", new Row<ElementType, bool> (ElementType.I4, true) },
90        { "UInt32", new Row<ElementType, bool> (ElementType.U4, true) },
91        { "Int64", new Row<ElementType, bool> (ElementType.I8, true) },
92        { "UInt64", new Row<ElementType, bool> (ElementType.U8, true) },
93        { "Single", new Row<ElementType, bool> (ElementType.R4, true) },
94        { "Double", new Row<ElementType, bool> (ElementType.R8, true) },
95        { "String", new Row<ElementType, bool> (ElementType.String, false) },
96        { "TypedReference", new Row<ElementType, bool> (ElementType.TypedByRef, false) },
97        { "IntPtr", new Row<ElementType, bool> (ElementType.I, true) },
98        { "UIntPtr", new Row<ElementType, bool> (ElementType.U, true) },
99        { "Object", new Row<ElementType, bool> (ElementType.Object, false) },
100      };
101    }
102
103    public static void TryProcessPrimitiveType (TypeReference type)
104    {
105      var scope = type.scope;
106      if (scope == null)
107        return;
108
109      if (scope.MetadataScopeType != MetadataScopeType.AssemblyNameReference)
110        return;
111
112      if (scope.Name != "mscorlib")
113        return;
114
115      if (type.Namespace != "System")
116        return;
117
118      if (primitive_value_types == null)
119        InitializePrimitives ();
120
121      Row<ElementType, bool> primitive_data;
122      if (!primitive_value_types.TryGetValue (type.Name, out primitive_data))
123        return;
124
125      type.etype = primitive_data.Col1;
126      type.IsValueType = primitive_data.Col2;
127    }
128
129    public void Clear ()
130    {
131      if (NestedTypes != null) NestedTypes.Clear ();
132      if (ReverseNestedTypes != null) ReverseNestedTypes.Clear ();
133      if (Interfaces != null) Interfaces.Clear ();
134      if (ClassLayouts != null) ClassLayouts.Clear ();
135      if (FieldLayouts != null) FieldLayouts.Clear ();
136      if (FieldRVAs != null) FieldRVAs.Clear ();
137      if (FieldMarshals != null) FieldMarshals.Clear ();
138      if (Constants != null) Constants.Clear ();
139      if (Overrides != null) Overrides.Clear ();
140      if (CustomAttributes != null) CustomAttributes.Clear ();
141      if (SecurityDeclarations != null) SecurityDeclarations.Clear ();
142      if (Events != null) Events.Clear ();
143      if (Properties != null) Properties.Clear ();
144      if (Semantics != null) Semantics.Clear ();
145      if (PInvokes != null) PInvokes.Clear ();
146      if (GenericParameters != null) GenericParameters.Clear ();
147      if (GenericConstraints != null) GenericConstraints.Clear ();
148    }
149
150    public TypeDefinition GetTypeDefinition (uint rid)
151    {
152      if (rid < 1 || rid > Types.Length)
153        return null;
154
155      return Types [rid - 1];
156    }
157
158    public void AddTypeDefinition (TypeDefinition type)
159    {
160      Types [type.token.RID - 1] = type;
161    }
162
163    public TypeReference GetTypeReference (uint rid)
164    {
165      if (rid < 1 || rid > TypeReferences.Length)
166        return null;
167
168      return TypeReferences [rid - 1];
169    }
170
171    public void AddTypeReference (TypeReference type)
172    {
173      TypeReferences [type.token.RID - 1] = type;
174    }
175
176    public FieldDefinition GetFieldDefinition (uint rid)
177    {
178      if (rid < 1 || rid > Fields.Length)
179        return null;
180
181      return Fields [rid - 1];
182    }
183
184    public void AddFieldDefinition (FieldDefinition field)
185    {
186      Fields [field.token.RID - 1] = field;
187    }
188
189    public MethodDefinition GetMethodDefinition (uint rid)
190    {
191      if (rid < 1 || rid > Methods.Length)
192        return null;
193
194      return Methods [rid - 1];
195    }
196
197    public void AddMethodDefinition (MethodDefinition method)
198    {
199      Methods [method.token.RID - 1] = method;
200    }
201
202    public MemberReference GetMemberReference (uint rid)
203    {
204      if (rid < 1 || rid > MemberReferences.Length)
205        return null;
206
207      return MemberReferences [rid - 1];
208    }
209
210    public void AddMemberReference (MemberReference member)
211    {
212      MemberReferences [member.token.RID - 1] = member;
213    }
214
215    public bool TryGetNestedTypeMapping (TypeDefinition type, out uint [] mapping)
216    {
217      return NestedTypes.TryGetValue (type.token.RID, out mapping);
218    }
219
220    public void SetNestedTypeMapping (uint type_rid, uint [] mapping)
221    {
222      NestedTypes [type_rid] = mapping;
223    }
224
225    public void RemoveNestedTypeMapping (TypeDefinition type)
226    {
227      NestedTypes.Remove (type.token.RID);
228    }
229
230    public bool TryGetReverseNestedTypeMapping (TypeDefinition type, out uint declaring)
231    {
232      return ReverseNestedTypes.TryGetValue (type.token.RID, out declaring);
233    }
234
235    public void SetReverseNestedTypeMapping (uint nested, uint declaring)
236    {
237      ReverseNestedTypes.Add (nested, declaring);
238    }
239
240    public void RemoveReverseNestedTypeMapping (TypeDefinition type)
241    {
242      ReverseNestedTypes.Remove (type.token.RID);
243    }
244
245    public bool TryGetInterfaceMapping (TypeDefinition type, out MetadataToken [] mapping)
246    {
247      return Interfaces.TryGetValue (type.token.RID, out mapping);
248    }
249
250    public void SetInterfaceMapping (uint type_rid, MetadataToken [] mapping)
251    {
252      Interfaces [type_rid] = mapping;
253    }
254
255    public void RemoveInterfaceMapping (TypeDefinition type)
256    {
257      Interfaces.Remove (type.token.RID);
258    }
259
260    public void AddPropertiesRange (uint type_rid, Range range)
261    {
262      Properties.Add (type_rid, range);
263    }
264
265    public bool TryGetPropertiesRange (TypeDefinition type, out Range range)
266    {
267      return Properties.TryGetValue (type.token.RID, out range);
268    }
269
270    public void RemovePropertiesRange (TypeDefinition type)
271    {
272      Properties.Remove (type.token.RID);
273    }
274
275    public void AddEventsRange (uint type_rid, Range range)
276    {
277      Events.Add (type_rid, range);
278    }
279
280    public bool TryGetEventsRange (TypeDefinition type, out Range range)
281    {
282      return Events.TryGetValue (type.token.RID, out range);
283    }
284
285    public void RemoveEventsRange (TypeDefinition type)
286    {
287      Events.Remove (type.token.RID);
288    }
289
290    public bool TryGetGenericParameterRange (IGenericParameterProvider owner, out Range range)
291    {
292      return GenericParameters.TryGetValue (owner.MetadataToken, out range);
293    }
294
295    public void RemoveGenericParameterRange (IGenericParameterProvider owner)
296    {
297      GenericParameters.Remove (owner.MetadataToken);
298    }
299
300    public bool TryGetCustomAttributeRange (ICustomAttributeProvider owner, out Range range)
301    {
302      return CustomAttributes.TryGetValue (owner.MetadataToken, out range);
303    }
304
305    public void RemoveCustomAttributeRange (ICustomAttributeProvider owner)
306    {
307      CustomAttributes.Remove (owner.MetadataToken);
308    }
309
310    public bool TryGetSecurityDeclarationRange (ISecurityDeclarationProvider owner, out Range range)
311    {
312      return SecurityDeclarations.TryGetValue (owner.MetadataToken, out range);
313    }
314
315    public void RemoveSecurityDeclarationRange (ISecurityDeclarationProvider owner)
316    {
317      SecurityDeclarations.Remove (owner.MetadataToken);
318    }
319
320    public bool TryGetGenericConstraintMapping (GenericParameter generic_parameter, out MetadataToken [] mapping)
321    {
322      return GenericConstraints.TryGetValue (generic_parameter.token.RID, out mapping);
323    }
324
325    public void SetGenericConstraintMapping (uint gp_rid, MetadataToken [] mapping)
326    {
327      GenericConstraints [gp_rid] = mapping;
328    }
329
330    public void RemoveGenericConstraintMapping (GenericParameter generic_parameter)
331    {
332      GenericConstraints.Remove (generic_parameter.token.RID);
333    }
334
335    public bool TryGetOverrideMapping (MethodDefinition method, out MetadataToken [] mapping)
336    {
337      return Overrides.TryGetValue (method.token.RID, out mapping);
338    }
339
340    public void SetOverrideMapping (uint rid, MetadataToken [] mapping)
341    {
342      Overrides [rid] = mapping;
343    }
344
345    public void RemoveOverrideMapping (MethodDefinition method)
346    {
347      Overrides.Remove (method.token.RID);
348    }
349
350    public TypeDefinition GetFieldDeclaringType (uint field_rid)
351    {
352      return BinaryRangeSearch (Types, field_rid, true);
353    }
354
355    public TypeDefinition GetMethodDeclaringType (uint method_rid)
356    {
357      return BinaryRangeSearch (Types, method_rid, false);
358    }
359
360    static TypeDefinition BinaryRangeSearch (TypeDefinition [] types, uint rid, bool field)
361    {
362      int min = 0;
363      int max = types.Length - 1;
364      while (min <= max) {
365        int mid = min + ((max - min) / 2);
366        var type = types [mid];
367        var range = field ? type.fields_range : type.methods_range;
368
369        if (rid < range.Start)
370          max = mid - 1;
371        else if (rid >= range.Start + range.Length)
372          min = mid + 1;
373        else
374          return type;
375      }
376
377      return null;
378    }
379  }
380}
Note: See TracBrowser for help on using the repository browser.