5
// Jb Evain (jbevain@gmail.com)
7
// Copyright (c) 2008 - 2010 Jb Evain
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:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
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.
30
using System.Collections.Generic;
32
using Mono.Cecil.Metadata;
34
namespace Mono.Cecil {
40
public Range (uint index, uint length)
47
sealed class MetadataSystem {
49
internal TypeDefinition [] Types;
50
internal TypeReference [] TypeReferences;
52
internal FieldDefinition [] Fields;
53
internal MethodDefinition [] Methods;
54
internal MemberReference [] MemberReferences;
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;
74
static Dictionary<string, Row<ElementType, bool>> primitive_value_types;
76
static void InitializePrimitives ()
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) },
100
public static void TryProcessPrimitiveType (TypeReference type)
102
var scope = type.scope;
106
if (scope.MetadataScopeType != MetadataScopeType.AssemblyNameReference)
109
if (scope.Name != "mscorlib")
112
if (type.Namespace != "System")
115
if (primitive_value_types == null)
116
InitializePrimitives ();
118
Row<ElementType, bool> primitive_data;
119
if (!primitive_value_types.TryGetValue (type.Name, out primitive_data))
122
type.etype = primitive_data.Col1;
123
type.IsValueType = primitive_data.Col2;
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 ();
147
public TypeDefinition GetTypeDefinition (uint rid)
149
if (rid < 1 || rid > Types.Length)
152
return Types [rid - 1];
155
public void AddTypeDefinition (TypeDefinition type)
157
Types [type.token.RID - 1] = type;
160
public TypeReference GetTypeReference (uint rid)
162
if (rid < 1 || rid > TypeReferences.Length)
165
return TypeReferences [rid - 1];
168
public void AddTypeReference (TypeReference type)
170
TypeReferences [type.token.RID - 1] = type;
173
public FieldDefinition GetFieldDefinition (uint rid)
175
if (rid < 1 || rid > Fields.Length)
178
return Fields [rid - 1];
181
public void AddFieldDefinition (FieldDefinition field)
183
Fields [field.token.RID - 1] = field;
186
public MethodDefinition GetMethodDefinition (uint rid)
188
if (rid < 1 || rid > Methods.Length)
191
return Methods [rid - 1];
194
public void AddMethodDefinition (MethodDefinition method)
196
Methods [method.token.RID - 1] = method;
199
public MemberReference GetMemberReference (uint rid)
201
if (rid < 1 || rid > MemberReferences.Length)
204
return MemberReferences [rid - 1];
207
public void AddMemberReference (MemberReference member)
209
MemberReferences [member.token.RID - 1] = member;
212
public bool TryGetNestedTypeMapping (TypeDefinition type, out uint [] mapping)
214
return NestedTypes.TryGetValue (type.token.RID, out mapping);
217
public void SetNestedTypeMapping (uint type_rid, uint [] mapping)
219
NestedTypes [type_rid] = mapping;
222
public void RemoveNestedTypeMapping (TypeDefinition type)
224
NestedTypes.Remove (type.token.RID);
227
public bool TryGetReverseNestedTypeMapping (TypeDefinition type, out uint declaring)
229
return ReverseNestedTypes.TryGetValue (type.token.RID, out declaring);
232
public void SetReverseNestedTypeMapping (uint nested, uint declaring)
234
ReverseNestedTypes.Add (nested, declaring);
237
public void RemoveReverseNestedTypeMapping (TypeDefinition type)
239
ReverseNestedTypes.Remove (type.token.RID);
242
public bool TryGetInterfaceMapping (TypeDefinition type, out MetadataToken [] mapping)
244
return Interfaces.TryGetValue (type.token.RID, out mapping);
247
public void SetInterfaceMapping (uint type_rid, MetadataToken [] mapping)
249
Interfaces [type_rid] = mapping;
252
public void RemoveInterfaceMapping (TypeDefinition type)
254
Interfaces.Remove (type.token.RID);
257
public void AddPropertiesRange (uint type_rid, Range range)
259
Properties.Add (type_rid, range);
262
public bool TryGetPropertiesRange (TypeDefinition type, out Range range)
264
return Properties.TryGetValue (type.token.RID, out range);
267
public void RemovePropertiesRange (TypeDefinition type)
269
Properties.Remove (type.token.RID);
272
public void AddEventsRange (uint type_rid, Range range)
274
Events.Add (type_rid, range);
277
public bool TryGetEventsRange (TypeDefinition type, out Range range)
279
return Events.TryGetValue (type.token.RID, out range);
282
public void RemoveEventsRange (TypeDefinition type)
284
Events.Remove (type.token.RID);
287
public bool TryGetGenericParameterRange (IGenericParameterProvider owner, out Range range)
289
return GenericParameters.TryGetValue (owner.MetadataToken, out range);
292
public void RemoveGenericParameterRange (IGenericParameterProvider owner)
294
GenericParameters.Remove (owner.MetadataToken);
297
public bool TryGetCustomAttributeRange (ICustomAttributeProvider owner, out Range range)
299
return CustomAttributes.TryGetValue (owner.MetadataToken, out range);
302
public void RemoveCustomAttributeRange (ICustomAttributeProvider owner)
304
CustomAttributes.Remove (owner.MetadataToken);
307
public bool TryGetSecurityDeclarationRange (ISecurityDeclarationProvider owner, out Range range)
309
return SecurityDeclarations.TryGetValue (owner.MetadataToken, out range);
312
public void RemoveSecurityDeclarationRange (ISecurityDeclarationProvider owner)
314
SecurityDeclarations.Remove (owner.MetadataToken);
317
public bool TryGetGenericConstraintMapping (GenericParameter generic_parameter, out MetadataToken [] mapping)
319
return GenericConstraints.TryGetValue (generic_parameter.token.RID, out mapping);
322
public void SetGenericConstraintMapping (uint gp_rid, MetadataToken [] mapping)
324
GenericConstraints [gp_rid] = mapping;
327
public void RemoveGenericConstraintMapping (GenericParameter generic_parameter)
329
GenericConstraints.Remove (generic_parameter.token.RID);
332
public bool TryGetOverrideMapping (MethodDefinition method, out MetadataToken [] mapping)
334
return Overrides.TryGetValue (method.token.RID, out mapping);
337
public void SetOverrideMapping (uint rid, MetadataToken [] mapping)
339
Overrides [rid] = mapping;
342
public void RemoveOverrideMapping (MethodDefinition method)
344
Overrides.Remove (method.token.RID);
347
public TypeDefinition GetFieldDeclaringType (uint field_rid)
349
return BinaryRangeSearch (Types, field_rid, true);
352
public TypeDefinition GetMethodDeclaringType (uint method_rid)
354
return BinaryRangeSearch (Types, method_rid, false);
357
static TypeDefinition BinaryRangeSearch (TypeDefinition [] types, uint rid, bool field)
360
int max = types.Length - 1;
362
int mid = min + ((max - min) / 2);
363
var type = types [mid];
364
var range = field ? type.fields_range : type.methods_range;
366
if (rid < range.Start)
368
else if (rid >= range.Start + range.Length)