PageRenderTime 22ms CodeModel.GetById 1ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

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