5 // Jb Evain (jbevain@gmail.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 namespace Mono.Cecil {
32 using Mono.Cecil.Binary;
34 internal sealed class FieldDefinition : FieldReference, IMemberDefinition,
35 ICustomAttributeProvider, IHasMarshalSpec, IHasConstant {
37 FieldAttributes m_attributes;
39 CustomAttributeCollection m_customAttrs;
50 MarshalSpec m_marshalDesc;
52 public bool HasLayoutInfo {
53 get { return m_hasInfo; }
57 get { return m_offset; }
66 set { m_rva = value; }
69 public byte [] InitialValue {
70 get { return m_initVal; }
71 set { m_initVal = value; }
74 public FieldAttributes Attributes {
75 get { return m_attributes; }
76 set { m_attributes = value; }
79 public bool HasConstant {
80 get { return m_hasConstant; }
83 public object Constant {
84 get { return m_const; }
91 public bool HasCustomAttributes {
92 get { return (m_customAttrs == null) ? false : (m_customAttrs.Count > 0); }
95 public CustomAttributeCollection CustomAttributes {
97 if (m_customAttrs == null)
98 m_customAttrs = new CustomAttributeCollection (this);
100 return m_customAttrs;
104 public MarshalSpec MarshalSpec {
105 get { return m_marshalDesc; }
107 m_marshalDesc = value;
109 m_attributes |= FieldAttributes.HasFieldMarshal;
111 m_attributes &= FieldAttributes.HasFieldMarshal;
115 #region FieldAttributes
117 public bool IsCompilerControlled {
118 get { return (m_attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Compilercontrolled; }
121 m_attributes &= ~FieldAttributes.FieldAccessMask;
122 m_attributes |= FieldAttributes.Compilercontrolled;
124 m_attributes &= ~(FieldAttributes.FieldAccessMask & FieldAttributes.Compilercontrolled);
128 public bool IsPrivate {
129 get { return (m_attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private; }
132 m_attributes &= ~FieldAttributes.FieldAccessMask;
133 m_attributes |= FieldAttributes.Private;
135 m_attributes &= ~(FieldAttributes.FieldAccessMask & FieldAttributes.Private);
139 public bool IsFamilyAndAssembly {
140 get { return (m_attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamANDAssem; }
143 m_attributes &= ~FieldAttributes.FieldAccessMask;
144 m_attributes |= FieldAttributes.FamANDAssem;
146 m_attributes &= ~(FieldAttributes.FieldAccessMask & FieldAttributes.FamANDAssem);
150 public bool IsAssembly {
151 get { return (m_attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly; }
154 m_attributes &= ~FieldAttributes.FieldAccessMask;
155 m_attributes |= FieldAttributes.Assembly;
157 m_attributes &= ~(FieldAttributes.FieldAccessMask & FieldAttributes.Assembly);
161 public bool IsFamily {
162 get { return (m_attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family; }
165 m_attributes &= ~FieldAttributes.FieldAccessMask;
166 m_attributes |= FieldAttributes.Family;
168 m_attributes &= ~(FieldAttributes.FieldAccessMask & FieldAttributes.Family);
172 public bool IsFamilyOrAssembly {
173 get { return (m_attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem; }
176 m_attributes &= ~FieldAttributes.FieldAccessMask;
177 m_attributes |= FieldAttributes.FamORAssem;
179 m_attributes &= ~(FieldAttributes.FieldAccessMask & FieldAttributes.FamORAssem);
183 public bool IsPublic {
184 get { return (m_attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public; }
187 m_attributes &= ~FieldAttributes.FieldAccessMask;
188 m_attributes |= FieldAttributes.Public;
190 m_attributes &= ~(FieldAttributes.FieldAccessMask & FieldAttributes.Public);
194 public bool IsStatic {
195 get { return (m_attributes & FieldAttributes.Static) != 0; }
198 m_attributes |= FieldAttributes.Static;
200 m_attributes &= ~FieldAttributes.Static;
204 public bool IsInitOnly {
205 get { return (m_attributes & FieldAttributes.InitOnly) != 0; }
208 m_attributes |= FieldAttributes.InitOnly;
210 m_attributes &= ~FieldAttributes.InitOnly;
214 public bool IsLiteral {
215 get { return (m_attributes & FieldAttributes.Literal) != 0; }
218 m_attributes |= FieldAttributes.Literal;
220 m_attributes &= ~FieldAttributes.Literal;
224 public bool IsNotSerialized {
225 get { return (m_attributes & FieldAttributes.NotSerialized) != 0; }
228 m_attributes |= FieldAttributes.NotSerialized;
230 m_attributes &= ~FieldAttributes.NotSerialized;
234 public bool IsSpecialName {
235 get { return (m_attributes & FieldAttributes.SpecialName) != 0; }
238 m_attributes |= FieldAttributes.SpecialName;
240 m_attributes &= ~FieldAttributes.SpecialName;
244 public bool IsPInvokeImpl {
245 get { return (m_attributes & FieldAttributes.PInvokeImpl) != 0; }
248 m_attributes |= FieldAttributes.PInvokeImpl;
250 m_attributes &= ~FieldAttributes.PInvokeImpl;
254 public bool IsRuntimeSpecialName {
255 get { return (m_attributes & FieldAttributes.RTSpecialName) != 0; }
258 m_attributes |= FieldAttributes.RTSpecialName;
260 m_attributes &= ~FieldAttributes.RTSpecialName;
264 public bool HasDefault {
265 get { return (m_attributes & FieldAttributes.HasDefault) != 0; }
268 m_attributes |= FieldAttributes.HasDefault;
270 m_attributes &= ~FieldAttributes.HasDefault;
276 public new TypeDefinition DeclaringType {
277 get { return (TypeDefinition) base.DeclaringType; }
278 set { base.DeclaringType = value; }
281 public FieldDefinition (string name, TypeReference fieldType,
282 FieldAttributes attrs) : base (name, fieldType)
284 m_attributes = attrs;
287 public override FieldDefinition Resolve ()
292 public FieldDefinition Clone ()
294 return Clone (this, new ImportContext (NullReferenceImporter.Instance, this.DeclaringType));
297 internal static FieldDefinition Clone (FieldDefinition field, ImportContext context)
299 FieldDefinition nf = new FieldDefinition (
301 context.Import (field.FieldType),
304 if (field.HasConstant)
305 nf.Constant = field.Constant;
306 if (field.MarshalSpec != null)
307 nf.MarshalSpec = field.MarshalSpec.CloneInto (nf);
308 if (field.RVA != RVA.Zero)
309 nf.InitialValue = field.InitialValue;
311 nf.InitialValue = new byte [0];
312 if (field.HasLayoutInfo)
313 nf.Offset = field.Offset;
315 foreach (CustomAttribute ca in field.CustomAttributes)
316 nf.CustomAttributes.Add (CustomAttribute.Clone (ca, context));
321 public override void Accept (IReflectionVisitor visitor)
323 visitor.VisitFieldDefinition (this);
325 if (this.MarshalSpec != null)
326 this.MarshalSpec.Accept (visitor);
328 this.CustomAttributes.Accept (visitor);