MethodDefinition.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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.Threading;
  12. using ILRuntime.Mono.Cecil.Cil;
  13. using ILRuntime.Mono.Collections.Generic;
  14. using RVA = System.UInt32;
  15. namespace ILRuntime.Mono.Cecil {
  16. public sealed class MethodDefinition : MethodReference, IMemberDefinition, ISecurityDeclarationProvider, ICustomDebugInformationProvider {
  17. ushort attributes;
  18. ushort impl_attributes;
  19. internal volatile bool sem_attrs_ready;
  20. internal MethodSemanticsAttributes sem_attrs;
  21. Collection<CustomAttribute> custom_attributes;
  22. Collection<SecurityDeclaration> security_declarations;
  23. internal RVA rva;
  24. internal PInvokeInfo pinvoke;
  25. Collection<MethodReference> overrides;
  26. internal MethodBody body;
  27. internal MethodDebugInformation debug_info;
  28. internal Collection<CustomDebugInformation> custom_infos;
  29. public override string Name {
  30. get { return base.Name; }
  31. set {
  32. if (IsWindowsRuntimeProjection && value != base.Name)
  33. throw new InvalidOperationException ();
  34. base.Name = value;
  35. }
  36. }
  37. public MethodAttributes Attributes {
  38. get { return (MethodAttributes) attributes; }
  39. set {
  40. if (IsWindowsRuntimeProjection && (ushort) value != attributes)
  41. throw new InvalidOperationException ();
  42. attributes = (ushort) value;
  43. }
  44. }
  45. public MethodImplAttributes ImplAttributes {
  46. get { return (MethodImplAttributes) impl_attributes; }
  47. set {
  48. if (IsWindowsRuntimeProjection && (ushort) value != impl_attributes)
  49. throw new InvalidOperationException ();
  50. impl_attributes = (ushort) value;
  51. }
  52. }
  53. public MethodSemanticsAttributes SemanticsAttributes {
  54. get {
  55. if (sem_attrs_ready)
  56. return sem_attrs;
  57. if (HasImage) {
  58. ReadSemantics ();
  59. return sem_attrs;
  60. }
  61. sem_attrs = MethodSemanticsAttributes.None;
  62. sem_attrs_ready = true;
  63. return sem_attrs;
  64. }
  65. set { sem_attrs = value; }
  66. }
  67. internal MethodDefinitionProjection WindowsRuntimeProjection {
  68. get { return (MethodDefinitionProjection) projection; }
  69. set { projection = value; }
  70. }
  71. internal void ReadSemantics ()
  72. {
  73. if (sem_attrs_ready)
  74. return;
  75. var module = this.Module;
  76. if (module == null)
  77. return;
  78. if (!module.HasImage)
  79. return;
  80. lock (module.SyncRoot) {
  81. if (sem_attrs_ready)
  82. return;
  83. module.Read (this, (method, reader) => reader.ReadAllSemantics (method));
  84. }
  85. }
  86. public bool HasSecurityDeclarations {
  87. get {
  88. if (security_declarations != null)
  89. return security_declarations.Count > 0;
  90. return this.GetHasSecurityDeclarations (Module);
  91. }
  92. }
  93. public Collection<SecurityDeclaration> SecurityDeclarations {
  94. get { return security_declarations ?? (this.GetSecurityDeclarations (ref security_declarations, Module)); }
  95. }
  96. public bool HasCustomAttributes {
  97. get {
  98. if (custom_attributes != null)
  99. return custom_attributes.Count > 0;
  100. return this.GetHasCustomAttributes (Module);
  101. }
  102. }
  103. public Collection<CustomAttribute> CustomAttributes {
  104. get { return custom_attributes ?? (this.GetCustomAttributes (ref custom_attributes, Module)); }
  105. }
  106. public int RVA {
  107. get { return (int) rva; }
  108. }
  109. public bool HasBody {
  110. get {
  111. return (attributes & (ushort) MethodAttributes.Abstract) == 0 &&
  112. (attributes & (ushort) MethodAttributes.PInvokeImpl) == 0 &&
  113. (impl_attributes & (ushort) MethodImplAttributes.InternalCall) == 0 &&
  114. (impl_attributes & (ushort) MethodImplAttributes.Native) == 0 &&
  115. (impl_attributes & (ushort) MethodImplAttributes.Unmanaged) == 0 &&
  116. (impl_attributes & (ushort) MethodImplAttributes.Runtime) == 0;
  117. }
  118. }
  119. public MethodBody Body {
  120. get {
  121. var local = this.body;
  122. if (local != null)
  123. return local;
  124. if (!HasBody)
  125. return null;
  126. if (HasImage && rva != 0)
  127. return Module.Read (ref body, this, (method, reader) => reader.ReadMethodBody (method));
  128. Interlocked.CompareExchange (ref body, new MethodBody (this) , null);
  129. return body;
  130. }
  131. set {
  132. var module = this.Module;
  133. if (module == null) {
  134. body = value;
  135. return;
  136. }
  137. // we reset Body to null in ILSpy to save memory; so we need that operation to be thread-safe
  138. lock (module.SyncRoot) {
  139. body = value;
  140. if (value == null)
  141. this.debug_info = null;
  142. }
  143. }
  144. }
  145. public MethodDebugInformation DebugInformation {
  146. get {
  147. Mixin.Read (Body);
  148. if (debug_info == null)
  149. {
  150. Interlocked.CompareExchange(ref debug_info, new MethodDebugInformation(this), null);
  151. }
  152. return debug_info;
  153. }
  154. set {
  155. debug_info = value;
  156. }
  157. }
  158. public bool HasPInvokeInfo {
  159. get {
  160. if (pinvoke != null)
  161. return true;
  162. return IsPInvokeImpl;
  163. }
  164. }
  165. public PInvokeInfo PInvokeInfo {
  166. get {
  167. if (pinvoke != null)
  168. return pinvoke;
  169. if (HasImage && IsPInvokeImpl)
  170. return Module.Read (ref pinvoke, this, (method, reader) => reader.ReadPInvokeInfo (method));
  171. return null;
  172. }
  173. set {
  174. IsPInvokeImpl = true;
  175. pinvoke = value;
  176. }
  177. }
  178. public bool HasOverrides {
  179. get {
  180. if (overrides != null)
  181. return overrides.Count > 0;
  182. return HasImage && Module.Read (this, (method, reader) => reader.HasOverrides (method));
  183. }
  184. }
  185. public Collection<MethodReference> Overrides {
  186. get {
  187. if (overrides != null)
  188. return overrides;
  189. if (HasImage)
  190. return Module.Read (ref overrides, this, (method, reader) => reader.ReadOverrides (method));
  191. Interlocked.CompareExchange (ref overrides, new Collection<MethodReference> (), null);
  192. return overrides;
  193. }
  194. }
  195. public override bool HasGenericParameters {
  196. get {
  197. if (generic_parameters != null)
  198. return generic_parameters.Count > 0;
  199. return this.GetHasGenericParameters (Module);
  200. }
  201. }
  202. public override Collection<GenericParameter> GenericParameters {
  203. get { return generic_parameters ?? (this.GetGenericParameters (ref generic_parameters, Module)); }
  204. }
  205. public bool HasCustomDebugInformations {
  206. get {
  207. Mixin.Read (Body);
  208. return !custom_infos.IsNullOrEmpty ();
  209. }
  210. }
  211. public Collection<CustomDebugInformation> CustomDebugInformations {
  212. get {
  213. Mixin.Read (Body);
  214. if (custom_infos == null)
  215. Interlocked.CompareExchange (ref custom_infos, new Collection<CustomDebugInformation> (), null);
  216. return custom_infos;
  217. }
  218. }
  219. #region MethodAttributes
  220. public bool IsCompilerControlled {
  221. get { return attributes.GetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.CompilerControlled); }
  222. set { attributes = attributes.SetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.CompilerControlled, value); }
  223. }
  224. public bool IsPrivate {
  225. get { return attributes.GetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.Private); }
  226. set { attributes = attributes.SetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.Private, value); }
  227. }
  228. public bool IsFamilyAndAssembly {
  229. get { return attributes.GetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.FamANDAssem); }
  230. set { attributes = attributes.SetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.FamANDAssem, value); }
  231. }
  232. public bool IsAssembly {
  233. get { return attributes.GetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.Assembly); }
  234. set { attributes = attributes.SetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.Assembly, value); }
  235. }
  236. public bool IsFamily {
  237. get { return attributes.GetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.Family); }
  238. set { attributes = attributes.SetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.Family, value); }
  239. }
  240. public bool IsFamilyOrAssembly {
  241. get { return attributes.GetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.FamORAssem); }
  242. set { attributes = attributes.SetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.FamORAssem, value); }
  243. }
  244. public bool IsPublic {
  245. get { return attributes.GetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.Public); }
  246. set { attributes = attributes.SetMaskedAttributes ((ushort) MethodAttributes.MemberAccessMask, (ushort) MethodAttributes.Public, value); }
  247. }
  248. public bool IsStatic {
  249. get { return attributes.GetAttributes ((ushort) MethodAttributes.Static); }
  250. set { attributes = attributes.SetAttributes ((ushort) MethodAttributes.Static, value); }
  251. }
  252. public bool IsFinal {
  253. get { return attributes.GetAttributes ((ushort) MethodAttributes.Final); }
  254. set { attributes = attributes.SetAttributes ((ushort) MethodAttributes.Final, value); }
  255. }
  256. public bool IsVirtual {
  257. get { return attributes.GetAttributes ((ushort) MethodAttributes.Virtual); }
  258. set { attributes = attributes.SetAttributes ((ushort) MethodAttributes.Virtual, value); }
  259. }
  260. public bool IsHideBySig {
  261. get { return attributes.GetAttributes ((ushort) MethodAttributes.HideBySig); }
  262. set { attributes = attributes.SetAttributes ((ushort) MethodAttributes.HideBySig, value); }
  263. }
  264. public bool IsReuseSlot {
  265. get { return attributes.GetMaskedAttributes ((ushort) MethodAttributes.VtableLayoutMask, (ushort) MethodAttributes.ReuseSlot); }
  266. set { attributes = attributes.SetMaskedAttributes ((ushort) MethodAttributes.VtableLayoutMask, (ushort) MethodAttributes.ReuseSlot, value); }
  267. }
  268. public bool IsNewSlot {
  269. get { return attributes.GetMaskedAttributes ((ushort) MethodAttributes.VtableLayoutMask, (ushort) MethodAttributes.NewSlot); }
  270. set { attributes = attributes.SetMaskedAttributes ((ushort) MethodAttributes.VtableLayoutMask, (ushort) MethodAttributes.NewSlot, value); }
  271. }
  272. public bool IsCheckAccessOnOverride {
  273. get { return attributes.GetAttributes ((ushort) MethodAttributes.CheckAccessOnOverride); }
  274. set { attributes = attributes.SetAttributes ((ushort) MethodAttributes.CheckAccessOnOverride, value); }
  275. }
  276. public bool IsAbstract {
  277. get { return attributes.GetAttributes ((ushort) MethodAttributes.Abstract); }
  278. set { attributes = attributes.SetAttributes ((ushort) MethodAttributes.Abstract, value); }
  279. }
  280. public bool IsSpecialName {
  281. get { return attributes.GetAttributes ((ushort) MethodAttributes.SpecialName); }
  282. set { attributes = attributes.SetAttributes ((ushort) MethodAttributes.SpecialName, value); }
  283. }
  284. public bool IsPInvokeImpl {
  285. get { return attributes.GetAttributes ((ushort) MethodAttributes.PInvokeImpl); }
  286. set { attributes = attributes.SetAttributes ((ushort) MethodAttributes.PInvokeImpl, value); }
  287. }
  288. public bool IsUnmanagedExport {
  289. get { return attributes.GetAttributes ((ushort) MethodAttributes.UnmanagedExport); }
  290. set { attributes = attributes.SetAttributes ((ushort) MethodAttributes.UnmanagedExport, value); }
  291. }
  292. public bool IsRuntimeSpecialName {
  293. get { return attributes.GetAttributes ((ushort) MethodAttributes.RTSpecialName); }
  294. set { attributes = attributes.SetAttributes ((ushort) MethodAttributes.RTSpecialName, value); }
  295. }
  296. public bool HasSecurity {
  297. get { return attributes.GetAttributes ((ushort) MethodAttributes.HasSecurity); }
  298. set { attributes = attributes.SetAttributes ((ushort) MethodAttributes.HasSecurity, value); }
  299. }
  300. #endregion
  301. #region MethodImplAttributes
  302. public bool IsIL {
  303. get { return impl_attributes.GetMaskedAttributes ((ushort) MethodImplAttributes.CodeTypeMask, (ushort) MethodImplAttributes.IL); }
  304. set { impl_attributes = impl_attributes.SetMaskedAttributes ((ushort) MethodImplAttributes.CodeTypeMask, (ushort) MethodImplAttributes.IL, value); }
  305. }
  306. public bool IsNative {
  307. get { return impl_attributes.GetMaskedAttributes ((ushort) MethodImplAttributes.CodeTypeMask, (ushort) MethodImplAttributes.Native); }
  308. set { impl_attributes = impl_attributes.SetMaskedAttributes ((ushort) MethodImplAttributes.CodeTypeMask, (ushort) MethodImplAttributes.Native, value); }
  309. }
  310. public bool IsRuntime {
  311. get { return impl_attributes.GetMaskedAttributes ((ushort) MethodImplAttributes.CodeTypeMask, (ushort) MethodImplAttributes.Runtime); }
  312. set { impl_attributes = impl_attributes.SetMaskedAttributes ((ushort) MethodImplAttributes.CodeTypeMask, (ushort) MethodImplAttributes.Runtime, value); }
  313. }
  314. public bool IsUnmanaged {
  315. get { return impl_attributes.GetMaskedAttributes ((ushort) MethodImplAttributes.ManagedMask, (ushort) MethodImplAttributes.Unmanaged); }
  316. set { impl_attributes = impl_attributes.SetMaskedAttributes ((ushort) MethodImplAttributes.ManagedMask, (ushort) MethodImplAttributes.Unmanaged, value); }
  317. }
  318. public bool IsManaged {
  319. get { return impl_attributes.GetMaskedAttributes ((ushort) MethodImplAttributes.ManagedMask, (ushort) MethodImplAttributes.Managed); }
  320. set { impl_attributes = impl_attributes.SetMaskedAttributes ((ushort) MethodImplAttributes.ManagedMask, (ushort) MethodImplAttributes.Managed, value); }
  321. }
  322. public bool IsForwardRef {
  323. get { return impl_attributes.GetAttributes ((ushort) MethodImplAttributes.ForwardRef); }
  324. set { impl_attributes = impl_attributes.SetAttributes ((ushort) MethodImplAttributes.ForwardRef, value); }
  325. }
  326. public bool IsPreserveSig {
  327. get { return impl_attributes.GetAttributes ((ushort) MethodImplAttributes.PreserveSig); }
  328. set { impl_attributes = impl_attributes.SetAttributes ((ushort) MethodImplAttributes.PreserveSig, value); }
  329. }
  330. public bool IsInternalCall {
  331. get { return impl_attributes.GetAttributes ((ushort) MethodImplAttributes.InternalCall); }
  332. set { impl_attributes = impl_attributes.SetAttributes ((ushort) MethodImplAttributes.InternalCall, value); }
  333. }
  334. public bool IsSynchronized {
  335. get { return impl_attributes.GetAttributes ((ushort) MethodImplAttributes.Synchronized); }
  336. set { impl_attributes = impl_attributes.SetAttributes ((ushort) MethodImplAttributes.Synchronized, value); }
  337. }
  338. public bool NoInlining {
  339. get { return impl_attributes.GetAttributes ((ushort) MethodImplAttributes.NoInlining); }
  340. set { impl_attributes = impl_attributes.SetAttributes ((ushort) MethodImplAttributes.NoInlining, value); }
  341. }
  342. public bool NoOptimization {
  343. get { return impl_attributes.GetAttributes ((ushort) MethodImplAttributes.NoOptimization); }
  344. set { impl_attributes = impl_attributes.SetAttributes ((ushort) MethodImplAttributes.NoOptimization, value); }
  345. }
  346. public bool AggressiveInlining {
  347. get { return impl_attributes.GetAttributes ((ushort) MethodImplAttributes.AggressiveInlining); }
  348. set { impl_attributes = impl_attributes.SetAttributes ((ushort) MethodImplAttributes.AggressiveInlining, value); }
  349. }
  350. #endregion
  351. #region MethodSemanticsAttributes
  352. public bool IsSetter {
  353. get { return this.GetSemantics (MethodSemanticsAttributes.Setter); }
  354. set { this.SetSemantics (MethodSemanticsAttributes.Setter, value); }
  355. }
  356. public bool IsGetter {
  357. get { return this.GetSemantics (MethodSemanticsAttributes.Getter); }
  358. set { this.SetSemantics (MethodSemanticsAttributes.Getter, value); }
  359. }
  360. public bool IsOther {
  361. get { return this.GetSemantics (MethodSemanticsAttributes.Other); }
  362. set { this.SetSemantics (MethodSemanticsAttributes.Other, value); }
  363. }
  364. public bool IsAddOn {
  365. get { return this.GetSemantics (MethodSemanticsAttributes.AddOn); }
  366. set { this.SetSemantics (MethodSemanticsAttributes.AddOn, value); }
  367. }
  368. public bool IsRemoveOn {
  369. get { return this.GetSemantics (MethodSemanticsAttributes.RemoveOn); }
  370. set { this.SetSemantics (MethodSemanticsAttributes.RemoveOn, value); }
  371. }
  372. public bool IsFire {
  373. get { return this.GetSemantics (MethodSemanticsAttributes.Fire); }
  374. set { this.SetSemantics (MethodSemanticsAttributes.Fire, value); }
  375. }
  376. #endregion
  377. public new TypeDefinition DeclaringType {
  378. get { return (TypeDefinition) base.DeclaringType; }
  379. set { base.DeclaringType = value; }
  380. }
  381. public bool IsConstructor {
  382. get {
  383. return this.IsRuntimeSpecialName
  384. && this.IsSpecialName
  385. && (this.Name == ".cctor" || this.Name == ".ctor");
  386. }
  387. }
  388. public override bool IsDefinition {
  389. get { return true; }
  390. }
  391. internal MethodDefinition ()
  392. {
  393. this.token = new MetadataToken (TokenType.Method);
  394. }
  395. public MethodDefinition (string name, MethodAttributes attributes, TypeReference returnType)
  396. : base (name, returnType)
  397. {
  398. this.attributes = (ushort) attributes;
  399. this.HasThis = !this.IsStatic;
  400. this.token = new MetadataToken (TokenType.Method);
  401. }
  402. public override MethodDefinition Resolve ()
  403. {
  404. return this;
  405. }
  406. }
  407. static partial class Mixin {
  408. public static ParameterDefinition GetParameter (this MethodBody self, int index)
  409. {
  410. var method = self.method;
  411. if (method.HasThis) {
  412. if (index == 0)
  413. return self.ThisParameter;
  414. index--;
  415. }
  416. var parameters = method.Parameters;
  417. if (index < 0 || index >= parameters.size)
  418. return null;
  419. return parameters [index];
  420. }
  421. public static VariableDefinition GetVariable (this MethodBody self, int index)
  422. {
  423. var variables = self.Variables;
  424. if (index < 0 || index >= variables.size)
  425. return null;
  426. return variables [index];
  427. }
  428. public static bool GetSemantics (this MethodDefinition self, MethodSemanticsAttributes semantics)
  429. {
  430. return (self.SemanticsAttributes & semantics) != 0;
  431. }
  432. public static void SetSemantics (this MethodDefinition self, MethodSemanticsAttributes semantics, bool value)
  433. {
  434. if (value)
  435. self.SemanticsAttributes |= semantics;
  436. else
  437. self.SemanticsAttributes &= ~semantics;
  438. }
  439. }
  440. }