| 1 |
// |
| 2 |
// FieldDefinition.cs |
| 3 |
// |
| 4 |
// Author: |
| 5 |
// Jb Evain ([email protected]) |
| 6 |
// |
| 7 |
// (C) 2005 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 |
|
| 29 |
namespace Mono.Cecil { |
| 30 |
|
| 31 |
using Mono.Cecil; |
| 32 |
using Mono.Cecil.Binary; |
| 33 |
|
| 34 |
internal sealed class FieldDefinition : FieldReference, IMemberDefinition, |
| 35 |
ICustomAttributeProvider, IHasMarshalSpec, IHasConstant { |
| 36 |
|
| 37 |
FieldAttributes m_attributes; |
| 38 |
|
| 39 |
CustomAttributeCollection m_customAttrs; |
| 40 |
|
| 41 |
bool m_hasInfo; |
| 42 |
uint m_offset; |
| 43 |
|
| 44 |
RVA m_rva; |
| 45 |
byte [] m_initVal; |
| 46 |
|
| 47 |
bool m_hasConstant; |
| 48 |
object m_const; |
| 49 |
|
| 50 |
MarshalSpec m_marshalDesc; |
| 51 |
|
| 52 |
public bool HasLayoutInfo { |
| 53 |
get { return m_hasInfo; } |
| 54 |
} |
| 55 |
|
| 56 |
public uint Offset { |
| 57 |
get { return m_offset; } |
| 58 |
set { |
| 59 |
m_hasInfo = true; |
| 60 |
m_offset = value; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
public RVA RVA { |
| 65 |
get { return m_rva; } |
| 66 |
set { m_rva = value; } |
| 67 |
} |
| 68 |
|
| 69 |
public byte [] InitialValue { |
| 70 |
get { return m_initVal; } |
| 71 |
set { m_initVal = value; } |
| 72 |
} |
| 73 |
|
| 74 |
public FieldAttributes Attributes { |
| 75 |
get { return m_attributes; } |
| 76 |
set { m_attributes = value; } |
| 77 |
} |
| 78 |
|
| 79 |
public bool HasConstant { |
| 80 |
get { return m_hasConstant; } |
| 81 |
} |
| 82 |
|
| 83 |
public object Constant { |
| 84 |
get { return m_const; } |
| 85 |
set { |
| 86 |
m_hasConstant = true; |
| 87 |
m_const = value; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
public bool HasCustomAttributes { |
| 92 |
get { return (m_customAttrs == null) ? false : (m_customAttrs.Count > 0); } |
| 93 |
} |
| 94 |
|
| 95 |
public CustomAttributeCollection CustomAttributes { |
| 96 |
get { |
| 97 |
if (m_customAttrs == null) |
| 98 |
m_customAttrs = new CustomAttributeCollection (this); |
| 99 |
|
| 100 |
return m_customAttrs; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
public MarshalSpec MarshalSpec { |
| 105 |
get { return m_marshalDesc; } |
| 106 |
set { |
| 107 |
m_marshalDesc = value; |
| 108 |
if (value != null) |
| 109 |
m_attributes |= FieldAttributes.HasFieldMarshal; |
| 110 |
else |
| 111 |
m_attributes &= FieldAttributes.HasFieldMarshal; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
#region FieldAttributes |
| 116 |
|
| 117 |
public bool IsCompilerControlled { |
| 118 |
get { return (m_attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Compilercontrolled; } |
| 119 |
set { |
| 120 |
if (value) { |
| 121 |
m_attributes &= ~FieldAttributes.FieldAccessMask; |
| 122 |
m_attributes |= FieldAttributes.Compilercontrolled; |
| 123 |
} else |
| 124 |
m_attributes &= ~(FieldAttributes.FieldAccessMask & FieldAttributes.Compilercontrolled); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
public bool IsPrivate { |
| 129 |
get { return (m_attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private; } |
| 130 |
set { |
| 131 |
if (value) { |
| 132 |
m_attributes &= ~FieldAttributes.FieldAccessMask; |
| 133 |
m_attributes |= FieldAttributes.Private; |
| 134 |
} else |
| 135 |
m_attributes &= ~(FieldAttributes.FieldAccessMask & FieldAttributes.Private); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
public bool IsFamilyAndAssembly { |
| 140 |
get { return (m_attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamANDAssem; } |
| 141 |
set { |
| 142 |
if (value) { |
| 143 |
m_attributes &= ~FieldAttributes.FieldAccessMask; |
| 144 |
m_attributes |= FieldAttributes.FamANDAssem; |
| 145 |
} else |
| 146 |
m_attributes &= ~(FieldAttributes.FieldAccessMask & FieldAttributes.FamANDAssem); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
public bool IsAssembly { |
| 151 |
get { return (m_attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly; } |
| 152 |
set { |
| 153 |
if (value) { |
| 154 |
m_attributes &= ~FieldAttributes.FieldAccessMask; |
| 155 |
m_attributes |= FieldAttributes.Assembly; |
| 156 |
} else |
| 157 |
m_attributes &= ~(FieldAttributes.FieldAccessMask & FieldAttributes.Assembly); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
public bool IsFamily { |
| 162 |
get { return (m_attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family; } |
| 163 |
set { |
| 164 |
if (value) { |
| 165 |
m_attributes &= ~FieldAttributes.FieldAccessMask; |
| 166 |
m_attributes |= FieldAttributes.Family; |
| 167 |
} else |
| 168 |
m_attributes &= ~(FieldAttributes.FieldAccessMask & FieldAttributes.Family); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
public bool IsFamilyOrAssembly { |
| 173 |
get { return (m_attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem; } |
| 174 |
set { |
| 175 |
if (value) { |
| 176 |
m_attributes &= ~FieldAttributes.FieldAccessMask; |
| 177 |
m_attributes |= FieldAttributes.FamORAssem; |
| 178 |
} else |
| 179 |
m_attributes &= ~(FieldAttributes.FieldAccessMask & FieldAttributes.FamORAssem); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
public bool IsPublic { |
| 184 |
get { return (m_attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public; } |
| 185 |
set { |
| 186 |
if (value) { |
| 187 |
m_attributes &= ~FieldAttributes.FieldAccessMask; |
| 188 |
m_attributes |= FieldAttributes.Public; |
| 189 |
} else |
| 190 |
m_attributes &= ~(FieldAttributes.FieldAccessMask & FieldAttributes.Public); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
public bool IsStatic { |
| 195 |
get { return (m_attributes & FieldAttributes.Static) != 0; } |
| 196 |
set { |
| 197 |
if (value) |
| 198 |
m_attributes |= FieldAttributes.Static; |
| 199 |
else |
| 200 |
m_attributes &= ~FieldAttributes.Static; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
public bool IsInitOnly { |
| 205 |
get { return (m_attributes & FieldAttributes.InitOnly) != 0; } |
| 206 |
set { |
| 207 |
if (value) |
| 208 |
m_attributes |= FieldAttributes.InitOnly; |
| 209 |
else |
| 210 |
m_attributes &= ~FieldAttributes.InitOnly; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
public bool IsLiteral { |
| 215 |
get { return (m_attributes & FieldAttributes.Literal) != 0; } |
| 216 |
set { |
| 217 |
if (value) |
| 218 |
m_attributes |= FieldAttributes.Literal; |
| 219 |
else |
| 220 |
m_attributes &= ~FieldAttributes.Literal; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
public bool IsNotSerialized { |
| 225 |
get { return (m_attributes & FieldAttributes.NotSerialized) != 0; } |
| 226 |
set { |
| 227 |
if (value) |
| 228 |
m_attributes |= FieldAttributes.NotSerialized; |
| 229 |
else |
| 230 |
m_attributes &= ~FieldAttributes.NotSerialized; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
public bool IsSpecialName { |
| 235 |
get { return (m_attributes & FieldAttributes.SpecialName) != 0; } |
| 236 |
set { |
| 237 |
if (value) |
| 238 |
m_attributes |= FieldAttributes.SpecialName; |
| 239 |
else |
| 240 |
m_attributes &= ~FieldAttributes.SpecialName; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
public bool IsPInvokeImpl { |
| 245 |
get { return (m_attributes & FieldAttributes.PInvokeImpl) != 0; } |
| 246 |
set { |
| 247 |
if (value) |
| 248 |
m_attributes |= FieldAttributes.PInvokeImpl; |
| 249 |
else |
| 250 |
m_attributes &= ~FieldAttributes.PInvokeImpl; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
public bool IsRuntimeSpecialName { |
| 255 |
get { return (m_attributes & FieldAttributes.RTSpecialName) != 0; } |
| 256 |
set { |
| 257 |
if (value) |
| 258 |
m_attributes |= FieldAttributes.RTSpecialName; |
| 259 |
else |
| 260 |
m_attributes &= ~FieldAttributes.RTSpecialName; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
public bool HasDefault { |
| 265 |
get { return (m_attributes & FieldAttributes.HasDefault) != 0; } |
| 266 |
set { |
| 267 |
if (value) |
| 268 |
m_attributes |= FieldAttributes.HasDefault; |
| 269 |
else |
| 270 |
m_attributes &= ~FieldAttributes.HasDefault; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
#endregion |
| 275 |
|
| 276 |
public new TypeDefinition DeclaringType { |
| 277 |
get { return (TypeDefinition) base.DeclaringType; } |
| 278 |
set { base.DeclaringType = value; } |
| 279 |
} |
| 280 |
|
| 281 |
public FieldDefinition (string name, TypeReference fieldType, |
| 282 |
FieldAttributes attrs) : base (name, fieldType) |
| 283 |
{ |
| 284 |
m_attributes = attrs; |
| 285 |
} |
| 286 |
|
| 287 |
public override FieldDefinition Resolve () |
| 288 |
{ |
| 289 |
return this; |
| 290 |
} |
| 291 |
|
| 292 |
public FieldDefinition Clone () |
| 293 |
{ |
| 294 |
return Clone (this, new ImportContext (NullReferenceImporter.Instance, this.DeclaringType)); |
| 295 |
} |
| 296 |
|
| 297 |
internal static FieldDefinition Clone (FieldDefinition field, ImportContext context) |
| 298 |
{ |
| 299 |
FieldDefinition nf = new FieldDefinition ( |
| 300 |
field.Name, |
| 301 |
context.Import (field.FieldType), |
| 302 |
field.Attributes); |
| 303 |
|
| 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; |
| 310 |
else |
| 311 |
nf.InitialValue = new byte [0]; |
| 312 |
if (field.HasLayoutInfo) |
| 313 |
nf.Offset = field.Offset; |
| 314 |
|
| 315 |
foreach (CustomAttribute ca in field.CustomAttributes) |
| 316 |
nf.CustomAttributes.Add (CustomAttribute.Clone (ca, context)); |
| 317 |
|
| 318 |
return nf; |
| 319 |
} |
| 320 |
|
| 321 |
public override void Accept (IReflectionVisitor visitor) |
| 322 |
{ |
| 323 |
visitor.VisitFieldDefinition (this); |
| 324 |
|
| 325 |
if (this.MarshalSpec != null) |
| 326 |
this.MarshalSpec.Accept (visitor); |
| 327 |
|
| 328 |
this.CustomAttributes.Accept (visitor); |
| 329 |
} |
| 330 |
} |
| 331 |
} |