MetadataSystem.cs 13 KB

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