FieldDefinition.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //
  2. // FieldDefinition.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.Collections.Generic;
  29. namespace Mono.Cecil {
  30. public sealed class FieldDefinition : FieldReference, IMemberDefinition, IConstantProvider, IMarshalInfoProvider {
  31. ushort attributes;
  32. Collection<CustomAttribute> custom_attributes;
  33. int offset = Mixin.NotResolvedMarker;
  34. internal int rva = Mixin.NotResolvedMarker;
  35. byte [] initial_value;
  36. object constant = Mixin.NotResolved;
  37. MarshalInfo marshal_info;
  38. void ResolveLayout ()
  39. {
  40. if (offset != Mixin.NotResolvedMarker)
  41. return;
  42. if (!HasImage) {
  43. offset = Mixin.NoDataMarker;
  44. return;
  45. }
  46. offset = Module.Read (this, (field, reader) => reader.ReadFieldLayout (field));
  47. }
  48. public bool HasLayoutInfo {
  49. get {
  50. if (offset >= 0)
  51. return true;
  52. ResolveLayout ();
  53. return offset >= 0;
  54. }
  55. }
  56. public int Offset {
  57. get {
  58. if (offset >= 0)
  59. return offset;
  60. ResolveLayout ();
  61. return offset >= 0 ? offset : -1;
  62. }
  63. set { offset = value; }
  64. }
  65. void ResolveRVA ()
  66. {
  67. if (rva != Mixin.NotResolvedMarker)
  68. return;
  69. if (!HasImage)
  70. return;
  71. rva = Module.Read (this, (field, reader) => reader.ReadFieldRVA (field));
  72. }
  73. public int RVA {
  74. get {
  75. if (rva > 0)
  76. return rva;
  77. ResolveRVA ();
  78. return rva > 0 ? rva : 0;
  79. }
  80. }
  81. public byte [] InitialValue {
  82. get {
  83. if (initial_value != null)
  84. return initial_value;
  85. ResolveRVA ();
  86. if (initial_value == null)
  87. initial_value = Empty<byte>.Array;
  88. return initial_value;
  89. }
  90. set { initial_value = value; }
  91. }
  92. public FieldAttributes Attributes {
  93. get { return (FieldAttributes) attributes; }
  94. set { attributes = (ushort) value; }
  95. }
  96. public bool HasConstant {
  97. get {
  98. Mixin.ResolveConstant(this, ref constant, Module);
  99. return constant != Mixin.NoValue;
  100. }
  101. set { if (!value) constant = Mixin.NoValue; }
  102. }
  103. public object Constant {
  104. get { return HasConstant ? constant : null; }
  105. set { constant = value; }
  106. }
  107. public bool HasCustomAttributes {
  108. get {
  109. if (custom_attributes != null)
  110. return custom_attributes.Count > 0;
  111. return Mixin.GetHasCustomAttributes(this, Module);
  112. }
  113. }
  114. public Collection<CustomAttribute> CustomAttributes {
  115. get { return custom_attributes ?? (Mixin.GetCustomAttributes(this, ref custom_attributes, Module)); }
  116. }
  117. public bool HasMarshalInfo {
  118. get {
  119. if (marshal_info != null)
  120. return true;
  121. return Mixin.GetHasMarshalInfo(this, Module);
  122. }
  123. }
  124. public MarshalInfo MarshalInfo {
  125. get { return marshal_info ?? (Mixin.GetMarshalInfo(this, ref marshal_info, Module)); }
  126. set { marshal_info = value; }
  127. }
  128. #region FieldAttributes
  129. public bool IsCompilerControlled {
  130. get { return Mixin.GetMaskedAttributes(attributes,(ushort) FieldAttributes.FieldAccessMask, (ushort) FieldAttributes.CompilerControlled); }
  131. set { attributes = Mixin.SetMaskedAttributes(attributes,(ushort) FieldAttributes.FieldAccessMask, (ushort) FieldAttributes.CompilerControlled, value); }
  132. }
  133. public bool IsPrivate {
  134. get { return Mixin.GetMaskedAttributes(attributes,(ushort) FieldAttributes.FieldAccessMask, (ushort) FieldAttributes.Private); }
  135. set { attributes = Mixin.SetMaskedAttributes(attributes,(ushort) FieldAttributes.FieldAccessMask, (ushort) FieldAttributes.Private, value); }
  136. }
  137. public bool IsFamilyAndAssembly {
  138. get { return Mixin.GetMaskedAttributes(attributes,(ushort) FieldAttributes.FieldAccessMask, (ushort) FieldAttributes.FamANDAssem); }
  139. set { attributes = Mixin.SetMaskedAttributes(attributes,(ushort) FieldAttributes.FieldAccessMask, (ushort) FieldAttributes.FamANDAssem, value); }
  140. }
  141. public bool IsAssembly {
  142. get { return Mixin.GetMaskedAttributes(attributes,(ushort) FieldAttributes.FieldAccessMask, (ushort) FieldAttributes.Assembly); }
  143. set { attributes = Mixin.SetMaskedAttributes(attributes,(ushort) FieldAttributes.FieldAccessMask, (ushort) FieldAttributes.Assembly, value); }
  144. }
  145. public bool IsFamily {
  146. get { return Mixin.GetMaskedAttributes(attributes,(ushort) FieldAttributes.FieldAccessMask, (ushort) FieldAttributes.Family); }
  147. set { attributes = Mixin.SetMaskedAttributes(attributes,(ushort) FieldAttributes.FieldAccessMask, (ushort) FieldAttributes.Family, value); }
  148. }
  149. public bool IsFamilyOrAssembly {
  150. get { return Mixin.GetMaskedAttributes(attributes,(ushort) FieldAttributes.FieldAccessMask, (ushort) FieldAttributes.FamORAssem); }
  151. set { attributes = Mixin.SetMaskedAttributes(attributes,(ushort) FieldAttributes.FieldAccessMask, (ushort) FieldAttributes.FamORAssem, value); }
  152. }
  153. public bool IsPublic {
  154. get { return Mixin.GetMaskedAttributes(attributes,(ushort) FieldAttributes.FieldAccessMask, (ushort) FieldAttributes.Public); }
  155. set { attributes = Mixin.SetMaskedAttributes(attributes,(ushort) FieldAttributes.FieldAccessMask, (ushort) FieldAttributes.Public, value); }
  156. }
  157. public bool IsStatic {
  158. get { return Mixin.GetAttributes(attributes,(ushort) FieldAttributes.Static); }
  159. set { attributes =Mixin.SetAttributes(attributes,(ushort) FieldAttributes.Static, value); }
  160. }
  161. public bool IsInitOnly {
  162. get { return Mixin.GetAttributes(attributes,(ushort) FieldAttributes.InitOnly); }
  163. set { attributes =Mixin.SetAttributes(attributes,(ushort) FieldAttributes.InitOnly, value); }
  164. }
  165. public bool IsLiteral {
  166. get { return Mixin.GetAttributes(attributes,(ushort) FieldAttributes.Literal); }
  167. set { attributes =Mixin.SetAttributes(attributes,(ushort) FieldAttributes.Literal, value); }
  168. }
  169. public bool IsNotSerialized {
  170. get { return Mixin.GetAttributes(attributes,(ushort) FieldAttributes.NotSerialized); }
  171. set { attributes =Mixin.SetAttributes(attributes,(ushort) FieldAttributes.NotSerialized, value); }
  172. }
  173. public bool IsSpecialName {
  174. get { return Mixin.GetAttributes(attributes,(ushort) FieldAttributes.SpecialName); }
  175. set { attributes =Mixin.SetAttributes(attributes,(ushort) FieldAttributes.SpecialName, value); }
  176. }
  177. public bool IsPInvokeImpl {
  178. get { return Mixin.GetAttributes(attributes,(ushort) FieldAttributes.PInvokeImpl); }
  179. set { attributes =Mixin.SetAttributes(attributes,(ushort) FieldAttributes.PInvokeImpl, value); }
  180. }
  181. public bool IsRuntimeSpecialName {
  182. get { return Mixin.GetAttributes(attributes,(ushort) FieldAttributes.RTSpecialName); }
  183. set { attributes =Mixin.SetAttributes(attributes,(ushort) FieldAttributes.RTSpecialName, value); }
  184. }
  185. public bool HasDefault {
  186. get { return Mixin.GetAttributes(attributes,(ushort) FieldAttributes.HasDefault); }
  187. set { attributes =Mixin.SetAttributes(attributes,(ushort) FieldAttributes.HasDefault, value); }
  188. }
  189. #endregion
  190. public override bool IsDefinition {
  191. get { return true; }
  192. }
  193. public new TypeDefinition DeclaringType {
  194. get { return (TypeDefinition) base.DeclaringType; }
  195. set { base.DeclaringType = value; }
  196. }
  197. public FieldDefinition (string name, FieldAttributes attributes, TypeReference fieldType)
  198. : base (name, fieldType)
  199. {
  200. this.attributes = (ushort) attributes;
  201. }
  202. public override FieldDefinition Resolve ()
  203. {
  204. return this;
  205. }
  206. }
  207. static partial class Mixin {
  208. public const int NotResolvedMarker = -2;
  209. public const int NoDataMarker = -1;
  210. }
  211. }