MetadataSystem.cs 12 KB

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