update cecil from upstream
[mono.git] / mcs / class / Mono.Cecil / Mono.Cecil / MetadataSystem.cs
1 //
2 // MetadataSystem.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@gmail.com)
6 //
7 // Copyright (c) 2008 - 2010 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
29 using System;
30 using System.Collections.Generic;
31
32 using Mono.Cecil.Metadata;
33
34 namespace 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 TypeDefinition [] Types;
50                 internal TypeReference [] TypeReferences;
51
52                 internal FieldDefinition [] Fields;
53                 internal MethodDefinition [] Methods;
54                 internal MemberReference [] MemberReferences;
55
56                 internal Dictionary<uint, uint []> NestedTypes;
57                 internal Dictionary<uint, uint> ReverseNestedTypes;
58                 internal Dictionary<uint, MetadataToken []> Interfaces;
59                 internal Dictionary<uint, Row<ushort, uint>> ClassLayouts;
60                 internal Dictionary<uint, uint> FieldLayouts;
61                 internal Dictionary<uint, uint> FieldRVAs;
62                 internal Dictionary<MetadataToken, uint> FieldMarshals;
63                 internal Dictionary<MetadataToken, Row<ElementType, uint>> Constants;
64                 internal Dictionary<uint, MetadataToken []> Overrides;
65                 internal Dictionary<MetadataToken, Range> CustomAttributes;
66                 internal Dictionary<MetadataToken, Range> SecurityDeclarations;
67                 internal Dictionary<uint, Range> Events;
68                 internal Dictionary<uint, Range> Properties;
69                 internal Dictionary<uint, Row<MethodSemanticsAttributes, MetadataToken>> Semantics;
70                 internal Dictionary<uint, Row<PInvokeAttributes, uint, uint>> PInvokes;
71                 internal Dictionary<MetadataToken, Range> GenericParameters;
72                 internal Dictionary<uint, MetadataToken []> GenericConstraints;
73
74                 static Dictionary<string, Row<ElementType, bool>> primitive_value_types;
75
76                 static void InitializePrimitives ()
77                 {
78                         primitive_value_types = new Dictionary<string, Row<ElementType, bool>> (18) {
79                                 { "Void", new Row<ElementType, bool> (ElementType.Void, false) },
80                                 { "Boolean", new Row<ElementType, bool> (ElementType.Boolean, true) },
81                                 { "Char", new Row<ElementType, bool> (ElementType.Char, true) },
82                                 { "SByte", new Row<ElementType, bool> (ElementType.I1, true) },
83                                 { "Byte", new Row<ElementType, bool> (ElementType.U1, true) },
84                                 { "Int16", new Row<ElementType, bool> (ElementType.I2, true) },
85                                 { "UInt16", new Row<ElementType, bool> (ElementType.U2, true) },
86                                 { "Int32", new Row<ElementType, bool> (ElementType.I4, true) },
87                                 { "UInt32", new Row<ElementType, bool> (ElementType.U4, true) },
88                                 { "Int64", new Row<ElementType, bool> (ElementType.I8, true) },
89                                 { "UInt64", new Row<ElementType, bool> (ElementType.U8, true) },
90                                 { "Single", new Row<ElementType, bool> (ElementType.R4, true) },
91                                 { "Double", new Row<ElementType, bool> (ElementType.R8, true) },
92                                 { "String", new Row<ElementType, bool> (ElementType.String, false) },
93                                 { "TypedReference", new Row<ElementType, bool> (ElementType.TypedByRef, false) },
94                                 { "IntPtr", new Row<ElementType, bool> (ElementType.I, true) },
95                                 { "UIntPtr", new Row<ElementType, bool> (ElementType.U, true) },
96                                 { "Object", new Row<ElementType, bool> (ElementType.Object, false) },
97                         };
98                 }
99
100                 public static void TryProcessPrimitiveType (TypeReference type)
101                 {
102                         var scope = type.scope;
103                         if (scope == null)
104                                 return;
105
106                         if (scope.MetadataScopeType != MetadataScopeType.AssemblyNameReference)
107                                 return;
108
109                         if (scope.Name != "mscorlib")
110                                 return;
111
112                         if (type.Namespace != "System")
113                                 return;
114
115                         if (primitive_value_types == null)
116                                 InitializePrimitives ();
117
118                         Row<ElementType, bool> primitive_data;
119                         if (!primitive_value_types.TryGetValue (type.Name, out primitive_data))
120                                 return;
121
122                         type.etype = primitive_data.Col1;
123                         type.IsValueType = primitive_data.Col2;
124                 }
125
126                 public void Clear ()
127                 {
128                         if (NestedTypes != null) NestedTypes.Clear ();
129                         if (ReverseNestedTypes != null) ReverseNestedTypes.Clear ();
130                         if (Interfaces != null) Interfaces.Clear ();
131                         if (ClassLayouts != null) ClassLayouts.Clear ();
132                         if (FieldLayouts != null) FieldLayouts.Clear ();
133                         if (FieldRVAs != null) FieldRVAs.Clear ();
134                         if (FieldMarshals != null) FieldMarshals.Clear ();
135                         if (Constants != null) Constants.Clear ();
136                         if (Overrides != null) Overrides.Clear ();
137                         if (CustomAttributes != null) CustomAttributes.Clear ();
138                         if (SecurityDeclarations != null) SecurityDeclarations.Clear ();
139                         if (Events != null) Events.Clear ();
140                         if (Properties != null) Properties.Clear ();
141                         if (Semantics != null) Semantics.Clear ();
142                         if (PInvokes != null) PInvokes.Clear ();
143                         if (GenericParameters != null) GenericParameters.Clear ();
144                         if (GenericConstraints != null) GenericConstraints.Clear ();
145                 }
146
147                 public TypeDefinition GetTypeDefinition (uint rid)
148                 {
149                         if (rid < 1 || rid > Types.Length)
150                                 return null;
151
152                         return Types [rid - 1];
153                 }
154
155                 public void AddTypeDefinition (TypeDefinition type)
156                 {
157                         Types [type.token.RID - 1] = type;
158                 }
159
160                 public TypeReference GetTypeReference (uint rid)
161                 {
162                         if (rid < 1 || rid > TypeReferences.Length)
163                                 return null;
164
165                         return TypeReferences [rid - 1];
166                 }
167
168                 public void AddTypeReference (TypeReference type)
169                 {
170                         TypeReferences [type.token.RID - 1] = type;
171                 }
172
173                 public FieldDefinition GetFieldDefinition (uint rid)
174                 {
175                         if (rid < 1 || rid > Fields.Length)
176                                 return null;
177
178                         return Fields [rid - 1];
179                 }
180
181                 public void AddFieldDefinition (FieldDefinition field)
182                 {
183                         Fields [field.token.RID - 1] = field;
184                 }
185
186                 public MethodDefinition GetMethodDefinition (uint rid)
187                 {
188                         if (rid < 1 || rid > Methods.Length)
189                                 return null;
190
191                         return Methods [rid - 1];
192                 }
193
194                 public void AddMethodDefinition (MethodDefinition method)
195                 {
196                         Methods [method.token.RID - 1] = method;
197                 }
198
199                 public MemberReference GetMemberReference (uint rid)
200                 {
201                         if (rid < 1 || rid > MemberReferences.Length)
202                                 return null;
203
204                         return MemberReferences [rid - 1];
205                 }
206
207                 public void AddMemberReference (MemberReference member)
208                 {
209                         MemberReferences [member.token.RID - 1] = member;
210                 }
211
212                 public bool TryGetNestedTypeMapping (TypeDefinition type, out uint [] mapping)
213                 {
214                         return NestedTypes.TryGetValue (type.token.RID, out mapping);
215                 }
216
217                 public void SetNestedTypeMapping (uint type_rid, uint [] mapping)
218                 {
219                         NestedTypes [type_rid] = mapping;
220                 }
221
222                 public void RemoveNestedTypeMapping (TypeDefinition type)
223                 {
224                         NestedTypes.Remove (type.token.RID);
225                 }
226
227                 public bool TryGetReverseNestedTypeMapping (TypeDefinition type, out uint declaring)
228                 {
229                         return ReverseNestedTypes.TryGetValue (type.token.RID, out declaring);
230                 }
231
232                 public void SetReverseNestedTypeMapping (uint nested, uint declaring)
233                 {
234                         ReverseNestedTypes.Add (nested, declaring);
235                 }
236
237                 public void RemoveReverseNestedTypeMapping (TypeDefinition type)
238                 {
239                         ReverseNestedTypes.Remove (type.token.RID);
240                 }
241
242                 public bool TryGetInterfaceMapping (TypeDefinition type, out MetadataToken [] mapping)
243                 {
244                         return Interfaces.TryGetValue (type.token.RID, out mapping);
245                 }
246
247                 public void SetInterfaceMapping (uint type_rid, MetadataToken [] mapping)
248                 {
249                         Interfaces [type_rid] = mapping;
250                 }
251
252                 public void RemoveInterfaceMapping (TypeDefinition type)
253                 {
254                         Interfaces.Remove (type.token.RID);
255                 }
256
257                 public void AddPropertiesRange (uint type_rid, Range range)
258                 {
259                         Properties.Add (type_rid, range);
260                 }
261
262                 public bool TryGetPropertiesRange (TypeDefinition type, out Range range)
263                 {
264                         return Properties.TryGetValue (type.token.RID, out range);
265                 }
266
267                 public void RemovePropertiesRange (TypeDefinition type)
268                 {
269                         Properties.Remove (type.token.RID);
270                 }
271
272                 public void AddEventsRange (uint type_rid, Range range)
273                 {
274                         Events.Add (type_rid, range);
275                 }
276
277                 public bool TryGetEventsRange (TypeDefinition type, out Range range)
278                 {
279                         return Events.TryGetValue (type.token.RID, out range);
280                 }
281
282                 public void RemoveEventsRange (TypeDefinition type)
283                 {
284                         Events.Remove (type.token.RID);
285                 }
286
287                 public bool TryGetGenericParameterRange (IGenericParameterProvider owner, out Range range)
288                 {
289                         return GenericParameters.TryGetValue (owner.MetadataToken, out range);
290                 }
291
292                 public void RemoveGenericParameterRange (IGenericParameterProvider owner)
293                 {
294                         GenericParameters.Remove (owner.MetadataToken);
295                 }
296
297                 public bool TryGetCustomAttributeRange (ICustomAttributeProvider owner, out Range range)
298                 {
299                         return CustomAttributes.TryGetValue (owner.MetadataToken, out range);
300                 }
301
302                 public void RemoveCustomAttributeRange (ICustomAttributeProvider owner)
303                 {
304                         CustomAttributes.Remove (owner.MetadataToken);
305                 }
306
307                 public bool TryGetSecurityDeclarationRange (ISecurityDeclarationProvider owner, out Range range)
308                 {
309                         return SecurityDeclarations.TryGetValue (owner.MetadataToken, out range);
310                 }
311
312                 public void RemoveSecurityDeclarationRange (ISecurityDeclarationProvider owner)
313                 {
314                         SecurityDeclarations.Remove (owner.MetadataToken);
315                 }
316
317                 public bool TryGetGenericConstraintMapping (GenericParameter generic_parameter, out MetadataToken [] mapping)
318                 {
319                         return GenericConstraints.TryGetValue (generic_parameter.token.RID, out mapping);
320                 }
321
322                 public void SetGenericConstraintMapping (uint gp_rid, MetadataToken [] mapping)
323                 {
324                         GenericConstraints [gp_rid] = mapping;
325                 }
326
327                 public void RemoveGenericConstraintMapping (GenericParameter generic_parameter)
328                 {
329                         GenericConstraints.Remove (generic_parameter.token.RID);
330                 }
331
332                 public bool TryGetOverrideMapping (MethodDefinition method, out MetadataToken [] mapping)
333                 {
334                         return Overrides.TryGetValue (method.token.RID, out mapping);
335                 }
336
337                 public void SetOverrideMapping (uint rid, MetadataToken [] mapping)
338                 {
339                         Overrides [rid] = mapping;
340                 }
341
342                 public void RemoveOverrideMapping (MethodDefinition method)
343                 {
344                         Overrides.Remove (method.token.RID);
345                 }
346
347                 public TypeDefinition GetFieldDeclaringType (uint field_rid)
348                 {
349                         return BinaryRangeSearch (Types, field_rid, true);
350                 }
351
352                 public TypeDefinition GetMethodDeclaringType (uint method_rid)
353                 {
354                         return BinaryRangeSearch (Types, method_rid, false);
355                 }
356
357                 static TypeDefinition BinaryRangeSearch (TypeDefinition [] types, uint rid, bool field)
358                 {
359                         int min = 0;
360                         int max = types.Length - 1;
361                         while (min <= max) {
362                                 int mid = min + ((max - min) / 2);
363                                 var type = types [mid];
364                                 var range = field ? type.fields_range : type.methods_range;
365
366                                 if (rid < range.Start)
367                                         max = mid - 1;
368                                 else if (rid >= range.Start + range.Length)
369                                         min = mid + 1;
370                                 else
371                                         return type;
372                         }
373
374                         return null;
375                 }
376         }
377 }