/Mono.Cecil/FieldDefinition.cs

http://github.com/jbevain/cecil · C# · 276 lines · 205 code · 62 blank · 9 comment · 26 complexity · 2e5e2141f3aa9fcb89dfa5e0728eba00 MD5 · raw file

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