2 // field.cs: All field handlers
4 // Authors: Miguel de Icaza (miguel@gnu.org)
5 // Martin Baulig (martin@ximian.com)
6 // Marek Safar (marek.safar@seznam.cz)
8 // Dual licensed under the terms of the MIT X11 or GNU GPL
10 // Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
11 // Copyright 2004-2008 Novell, Inc
12 // Copyright 2011 Xamarin Inc
16 using System.Collections.Generic;
17 using System.Runtime.InteropServices;
20 using MetaType = IKVM.Reflection.Type;
21 using IKVM.Reflection;
22 using IKVM.Reflection.Emit;
24 using MetaType = System.Type;
25 using System.Reflection;
26 using System.Reflection.Emit;
31 public class FieldDeclarator
33 public FieldDeclarator (SimpleMemberName name, Expression initializer)
36 this.Initializer = initializer;
41 public SimpleMemberName Name { get; private set; }
42 public Expression Initializer { get; private set; }
48 // Abstract class for all fields
50 abstract public class FieldBase : MemberBase
52 protected FieldBuilder FieldBuilder;
53 protected FieldSpec spec;
55 protected Expression initializer;
56 protected List<FieldDeclarator> declarators;
59 public enum Status : byte {
60 HAS_OFFSET = 4 // Used by FieldMember.
63 static readonly string[] attribute_targets = new string [] { "field" };
65 protected FieldBase (TypeDefinition parent, FullNamedExpression type, Modifiers mod, Modifiers allowed_mod, MemberName name, Attributes attrs)
66 : base (parent, type, mod, allowed_mod | Modifiers.ABSTRACT, Modifiers.PRIVATE, name, attrs)
68 if ((mod & Modifiers.ABSTRACT) != 0)
69 Report.Error (681, Location, "The modifier 'abstract' is not valid on fields. Try using a property instead");
74 public override AttributeTargets AttributeTargets {
76 return AttributeTargets.Field;
80 public List<FieldDeclarator> Declarators {
82 return this.declarators;
86 public Expression Initializer {
91 this.initializer = value;
97 return MemberName.Name;
101 public FieldSpec Spec {
107 public override string[] ValidAttributeTargets {
109 return attribute_targets;
115 public void AddDeclarator (FieldDeclarator declarator)
117 if (declarators == null)
118 declarators = new List<FieldDeclarator> (2);
120 declarators.Add (declarator);
122 Parent.AddNameToContainer (this, declarator.Name.Value);
125 public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
127 if (a.Type == pa.FieldOffset) {
128 status |= Status.HAS_OFFSET;
130 if (!Parent.PartialContainer.HasExplicitLayout) {
131 Report.Error (636, Location, "The FieldOffset attribute can only be placed on members of types marked with the StructLayout(LayoutKind.Explicit)");
135 if ((ModFlags & Modifiers.STATIC) != 0 || this is Const) {
136 Report.Error (637, Location, "The FieldOffset attribute is not allowed on static or const fields");
141 if (a.Type == pa.FixedBuffer) {
142 Report.Error (1716, Location, "Do not use 'System.Runtime.CompilerServices.FixedBuffer' attribute. Use the 'fixed' field modifier instead");
147 if (a.Type == pa.MarshalAs) {
148 UnmanagedMarshal marshal = a.GetMarshal (this);
149 if (marshal != null) {
150 FieldBuilder.SetMarshal (marshal);
155 if ((a.HasSecurityAttribute)) {
156 a.Error_InvalidSecurityParent ();
160 if (a.Type == pa.Dynamic) {
161 a.Error_MisusedDynamicAttribute ();
165 FieldBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
168 public void SetCustomAttribute (MethodSpec ctor, byte[] data)
170 FieldBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), data);
173 protected override bool CheckBase ()
175 if (!base.CheckBase ())
178 MemberSpec candidate;
179 bool overrides = false;
180 var conflict_symbol = MemberCache.FindBaseMember (this, out candidate, ref overrides);
181 if (conflict_symbol == null)
182 conflict_symbol = candidate;
184 if (conflict_symbol == null) {
185 if ((ModFlags & Modifiers.NEW) != 0) {
186 Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required",
187 GetSignatureForError ());
190 if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.BACKING_FIELD)) == 0) {
191 Report.SymbolRelatedToPreviousError (conflict_symbol);
192 Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
193 GetSignatureForError (), conflict_symbol.GetSignatureForError ());
196 if (conflict_symbol.IsAbstract) {
197 Report.SymbolRelatedToPreviousError (conflict_symbol);
198 Report.Error (533, Location, "`{0}' hides inherited abstract member `{1}'",
199 GetSignatureForError (), conflict_symbol.GetSignatureForError ());
206 public virtual Constant ConvertInitializer (ResolveContext rc, Constant expr)
208 return expr.ConvertImplicitly (MemberType);
211 protected override void DoMemberTypeDependentChecks ()
213 base.DoMemberTypeDependentChecks ();
215 if (MemberType.IsGenericParameter)
218 if (MemberType.IsStatic)
219 Error_VariableOfStaticClass (Location, GetSignatureForError (), MemberType, Report);
226 // Represents header string for documentation comment.
228 public override string DocCommentHeader {
232 public override void Emit ()
234 if (member_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
235 Module.PredefinedAttributes.Dynamic.EmitAttribute (FieldBuilder);
236 } else if (!Parent.IsCompilerGenerated && member_type.HasDynamicElement) {
237 Module.PredefinedAttributes.Dynamic.EmitAttribute (FieldBuilder, member_type, Location);
240 if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
241 Module.PredefinedAttributes.CompilerGenerated.EmitAttribute (FieldBuilder);
242 if ((ModFlags & Modifiers.DEBUGGER_HIDDEN) != 0)
243 Module.PredefinedAttributes.DebuggerBrowsable.EmitAttribute (FieldBuilder, System.Diagnostics.DebuggerBrowsableState.Never);
245 if (OptAttributes != null) {
246 OptAttributes.Emit ();
249 if (((status & Status.HAS_OFFSET) == 0) && (ModFlags & (Modifiers.STATIC | Modifiers.BACKING_FIELD)) == 0 && Parent.PartialContainer.HasExplicitLayout) {
250 Report.Error (625, Location, "`{0}': Instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute", GetSignatureForError ());
253 ConstraintChecker.Check (this, member_type, type_expr.Location);
258 public static void Error_VariableOfStaticClass (Location loc, string variable_name, TypeSpec static_class, Report Report)
260 Report.SymbolRelatedToPreviousError (static_class);
261 Report.Error (723, loc, "`{0}': cannot declare variables of static types",
265 protected override bool VerifyClsCompliance ()
267 if (!base.VerifyClsCompliance ())
270 if (!MemberType.IsCLSCompliant () || this is FixedField) {
271 Report.Warning (3003, 1, Location, "Type of `{0}' is not CLS-compliant",
272 GetSignatureForError ());
279 // Field specification
281 public class FieldSpec : MemberSpec, IInterfaceMemberSpec
286 public FieldSpec (TypeSpec declaringType, IMemberDefinition definition, TypeSpec memberType, FieldInfo info, Modifiers modifiers)
287 : base (MemberKind.Field, declaringType, definition, modifiers)
289 this.metaInfo = info;
290 this.memberType = memberType;
295 public bool IsReadOnly {
297 return (Modifiers & Modifiers.READONLY) != 0;
301 public TypeSpec MemberType {
309 public FieldInfo GetMetaInfo ()
311 if ((state & StateFlags.PendingMetaInflate) != 0) {
312 var decl_meta = DeclaringType.GetMetaInfo ();
313 if (DeclaringType.IsTypeBuilder) {
314 metaInfo = TypeBuilder.GetField (decl_meta, metaInfo);
316 var orig_token = metaInfo.MetadataToken;
317 metaInfo = decl_meta.GetField (Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
318 if (metaInfo.MetadataToken != orig_token)
319 throw new NotImplementedException ("Resolved to wrong meta token");
321 // What a stupid API, does not work because field handle is imported
322 // metaInfo = FieldInfo.GetFieldFromHandle (metaInfo.FieldHandle, DeclaringType.MetaInfo.TypeHandle);
325 state &= ~StateFlags.PendingMetaInflate;
331 public override MemberSpec InflateMember (TypeParameterInflator inflator)
333 var fs = (FieldSpec) base.InflateMember (inflator);
334 fs.memberType = inflator.Inflate (memberType);
338 public FieldSpec Mutate (TypeParameterMutator mutator)
340 var decl = DeclaringType;
341 if (DeclaringType.IsGenericOrParentIsGeneric)
342 decl = mutator.Mutate (decl);
344 if (decl == DeclaringType)
347 var fs = (FieldSpec) MemberwiseClone ();
348 fs.declaringType = decl;
349 fs.state |= StateFlags.PendingMetaInflate;
351 // Gets back FieldInfo in case of metaInfo was inflated
352 fs.metaInfo = MemberCache.GetMember (TypeParameterMutator.GetMemberDeclaringType (DeclaringType), this).metaInfo;
356 public override List<TypeSpec> ResolveMissingDependencies ()
358 return memberType.ResolveMissingDependencies ();
363 /// Fixed buffer implementation
365 public class FixedField : FieldBase
367 public const string FixedElementName = "FixedElementField";
368 static int GlobalCounter = 0;
370 TypeBuilder fixed_buffer_type;
372 const Modifiers AllowedModifiers =
375 Modifiers.PROTECTED |
380 public FixedField (TypeDefinition parent, FullNamedExpression type, Modifiers mod, MemberName name, Attributes attrs)
381 : base (parent, type, mod, AllowedModifiers, name, attrs)
388 // Explicit struct layout set by parent
390 public CharSet? CharSet {
396 public override Constant ConvertInitializer (ResolveContext rc, Constant expr)
398 return expr.ImplicitConversionRequired (rc, rc.BuiltinTypes.Int, Location);
401 public override bool Define ()
406 if (!BuiltinTypeSpec.IsPrimitiveType (MemberType)) {
407 Report.Error (1663, Location,
408 "`{0}': Fixed size buffers type must be one of the following: bool, byte, short, int, long, char, sbyte, ushort, uint, ulong, float or double",
409 GetSignatureForError ());
410 } else if (declarators != null) {
411 var t = new TypeExpression (MemberType, TypeExpression.Location);
412 foreach (var d in declarators) {
413 var f = new FixedField (Parent, t, ModFlags, new MemberName (d.Name.Value, d.Name.Location), OptAttributes);
414 f.initializer = d.Initializer;
415 ((ConstInitializer) f.initializer).Name = d.Name.Value;
417 Parent.PartialContainer.Members.Add (f);
421 // Create nested fixed buffer container
422 string name = String.Format ("<{0}>__FixedBuffer{1}", Name, GlobalCounter++);
423 fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name,
424 TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit,
425 Compiler.BuiltinTypes.ValueType.GetMetaInfo ());
427 var ffield = fixed_buffer_type.DefineField (FixedElementName, MemberType.GetMetaInfo (), FieldAttributes.Public);
429 FieldBuilder = Parent.TypeBuilder.DefineField (Name, fixed_buffer_type, ModifiersExtensions.FieldAttr (ModFlags));
431 var element_spec = new FieldSpec (null, this, MemberType, ffield, ModFlags);
432 spec = new FixedFieldSpec (Module, Parent.Definition, this, FieldBuilder, element_spec, ModFlags);
434 Parent.MemberCache.AddMember (spec);
438 protected override void DoMemberTypeIndependentChecks ()
440 base.DoMemberTypeIndependentChecks ();
443 Expression.UnsafeError (Report, Location);
445 if (Parent.PartialContainer.Kind != MemberKind.Struct) {
446 Report.Error (1642, Location, "`{0}': Fixed size buffer fields may only be members of structs",
447 GetSignatureForError ());
451 public override void Emit()
453 ResolveContext rc = new ResolveContext (this);
454 IntConstant buffer_size_const = initializer.Resolve (rc) as IntConstant;
455 if (buffer_size_const == null)
458 int buffer_size = buffer_size_const.Value;
460 if (buffer_size <= 0) {
461 Report.Error (1665, Location, "`{0}': Fixed size buffers must have a length greater than zero", GetSignatureForError ());
465 EmitFieldSize (buffer_size);
468 if (Module.HasDefaultCharSet)
469 fixed_buffer_type.__SetAttributes (fixed_buffer_type.Attributes | Module.DefaultCharSetType);
472 Module.PredefinedAttributes.UnsafeValueType.EmitAttribute (fixed_buffer_type);
473 Module.PredefinedAttributes.CompilerGenerated.EmitAttribute (fixed_buffer_type);
474 fixed_buffer_type.CreateType ();
479 void EmitFieldSize (int buffer_size)
481 int type_size = BuiltinTypeSpec.GetSize (MemberType);
483 if (buffer_size > int.MaxValue / type_size) {
484 Report.Error (1664, Location, "Fixed size buffer `{0}' of length `{1}' and type `{2}' exceeded 2^31 limit",
485 GetSignatureForError (), buffer_size.ToString (), MemberType.GetSignatureForError ());
489 AttributeEncoder encoder;
491 var ctor = Module.PredefinedMembers.StructLayoutAttributeCtor.Resolve (Location);
495 var field_size = Module.PredefinedMembers.StructLayoutSize.Resolve (Location);
496 var field_charset = Module.PredefinedMembers.StructLayoutCharSet.Resolve (Location);
497 if (field_size == null || field_charset == null)
500 var char_set = CharSet ?? Module.DefaultCharSet ?? 0;
502 encoder = new AttributeEncoder ();
503 encoder.Encode ((short)LayoutKind.Sequential);
504 encoder.EncodeNamedArguments (
505 new [] { field_size, field_charset },
507 new IntConstant (Compiler.BuiltinTypes, buffer_size * type_size, Location),
508 new IntConstant (Compiler.BuiltinTypes, (int) char_set, Location)
512 fixed_buffer_type.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), encoder.ToArray ());
515 // Don't emit FixedBufferAttribute attribute for private types
517 if ((ModFlags & Modifiers.PRIVATE) != 0)
520 ctor = Module.PredefinedMembers.FixedBufferAttributeCtor.Resolve (Location);
524 encoder = new AttributeEncoder ();
525 encoder.EncodeTypeName (MemberType);
526 encoder.Encode (buffer_size);
527 encoder.EncodeEmptyNamedArguments ();
529 FieldBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), encoder.ToArray ());
533 class FixedFieldSpec : FieldSpec
535 readonly FieldSpec element;
537 public FixedFieldSpec (ModuleContainer module, TypeSpec declaringType, IMemberDefinition definition, FieldInfo info, FieldSpec element, Modifiers modifiers)
538 : base (declaringType, definition, PointerContainer.MakeType (module, element.MemberType), info, modifiers)
540 this.element = element;
542 // It's never CLS-Compliant
543 state &= ~StateFlags.CLSCompliant_Undetected;
546 public FieldSpec Element {
552 public TypeSpec ElementType {
554 return element.MemberType;
560 // The Field class is used to represents class/struct fields during parsing.
562 public class Field : FieldBase {
564 // Modifiers allowed in a class declaration
566 const Modifiers AllowedModifiers =
569 Modifiers.PROTECTED |
577 public Field (TypeDefinition parent, FullNamedExpression type, Modifiers mod, MemberName name, Attributes attrs)
578 : base (parent, type, mod, AllowedModifiers, name, attrs)
582 bool CanBeVolatile ()
584 switch (MemberType.BuiltinType) {
585 case BuiltinTypeSpec.Type.Bool:
586 case BuiltinTypeSpec.Type.Char:
587 case BuiltinTypeSpec.Type.SByte:
588 case BuiltinTypeSpec.Type.Byte:
589 case BuiltinTypeSpec.Type.Short:
590 case BuiltinTypeSpec.Type.UShort:
591 case BuiltinTypeSpec.Type.Int:
592 case BuiltinTypeSpec.Type.UInt:
593 case BuiltinTypeSpec.Type.Float:
594 case BuiltinTypeSpec.Type.UIntPtr:
595 case BuiltinTypeSpec.Type.IntPtr:
599 if (TypeSpec.IsReferenceType (MemberType))
602 if (MemberType.IsEnum)
608 public override void Accept (StructuralVisitor visitor)
610 visitor.Visit (this);
613 public override bool Define ()
618 MetaType[] required_modifier = null;
619 if ((ModFlags & Modifiers.VOLATILE) != 0) {
620 var mod = Module.PredefinedTypes.IsVolatile.Resolve ();
622 required_modifier = new MetaType[] { mod.GetMetaInfo () };
625 FieldBuilder = Parent.TypeBuilder.DefineField (
626 Name, member_type.GetMetaInfo (), required_modifier, null, ModifiersExtensions.FieldAttr (ModFlags));
628 spec = new FieldSpec (Parent.Definition, this, MemberType, FieldBuilder, ModFlags);
631 // Don't cache inaccessible fields except for struct where we
632 // need them for definitive assignment checks
634 if ((ModFlags & Modifiers.BACKING_FIELD) == 0 || Parent.Kind == MemberKind.Struct) {
635 Parent.MemberCache.AddMember (spec);
638 if (initializer != null) {
639 Parent.RegisterFieldForInitialization (this, new FieldInitializer (this, initializer, TypeExpression.Location));
642 if (declarators != null) {
643 foreach (var d in declarators) {
644 var t = new TypeExpression (MemberType, d.Name.Location);
645 var f = new Field (Parent, t, ModFlags, new MemberName (d.Name.Value, d.Name.Location), OptAttributes);
646 if (d.Initializer != null)
647 f.initializer = d.Initializer;
650 Parent.PartialContainer.Members.Add (f);
657 protected override void DoMemberTypeDependentChecks ()
659 if ((ModFlags & Modifiers.BACKING_FIELD) != 0)
662 base.DoMemberTypeDependentChecks ();
664 if ((ModFlags & Modifiers.VOLATILE) != 0) {
665 if (!CanBeVolatile ()) {
666 Report.Error (677, Location, "`{0}': A volatile field cannot be of the type `{1}'",
667 GetSignatureForError (), MemberType.GetSignatureForError ());
670 if ((ModFlags & Modifiers.READONLY) != 0) {
671 Report.Error (678, Location, "`{0}': A field cannot be both volatile and readonly",
672 GetSignatureForError ());
677 protected override bool VerifyClsCompliance ()
679 if (!base.VerifyClsCompliance ())
682 if ((ModFlags & Modifiers.VOLATILE) != 0) {
683 Report.Warning (3026, 1, Location, "CLS-compliant field `{0}' cannot be volatile", GetSignatureForError ());