MethodDefinition.cs 20 KB

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