New tests.
[mono.git] / mcs / mcs / field.cs
index da3813254da55d9e21c76d28fafaeb16a3700614..e43bf5ba4c53d5db0981fd7f1a96fc28487379f2 100644 (file)
@@ -25,6 +25,7 @@ namespace Mono.CSharp
        abstract public class FieldBase : MemberBase
        {
                public FieldBuilder FieldBuilder;
+               protected FieldSpec spec;
                public Status status;
                protected Expression initializer;
 
@@ -50,7 +51,7 @@ namespace Mono.CSharp
                        }
                }
 
-               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa)
+               public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
                {
                        if (a.Type == pa.FieldOffset) {
                                status |= Status.HAS_OFFSET;
@@ -90,41 +91,55 @@ namespace Mono.CSharp
                                return;
                        }
 
-                       FieldBuilder.SetCustomAttribute (cb);
+                       FieldBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
                }
 
                protected override bool CheckBase ()
                {
                        if (!base.CheckBase ())
                                return false;
-                       MemberInfo conflict_symbol = Parent.PartialContainer.FindBaseMemberWithSameName (Name, false);
+
+                       MemberSpec candidate;
+                       var conflict_symbol = MemberCache.FindBaseMember (this, out candidate);
+                       if (conflict_symbol == null)
+                               conflict_symbol = candidate;
+
                        if (conflict_symbol == null) {
                                if ((ModFlags & Modifiers.NEW) != 0) {
-                                       Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required", GetSignatureForError ());
+                                       Report.Warning (109, 4, Location, "The member `{0}' does not hide an inherited member. The new keyword is not required",
+                                               GetSignatureForError ());
                                }
-                               return true;
-                       }
-                       if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.BACKING_FIELD)) == 0) {
-                               Report.SymbolRelatedToPreviousError (conflict_symbol);
-                               Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
-                                       GetSignatureForError (), TypeManager.GetFullNameSignature (conflict_symbol));
+                       } else {
+                               if ((ModFlags & (Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.BACKING_FIELD)) == 0) {
+                                       Report.SymbolRelatedToPreviousError (conflict_symbol);
+                                       Report.Warning (108, 2, Location, "`{0}' hides inherited member `{1}'. Use the new keyword if hiding was intended",
+                                               GetSignatureForError (), conflict_symbol.GetSignatureForError ());
+                               }
+
+                               if (conflict_symbol.IsAbstract) {
+                                       Report.SymbolRelatedToPreviousError (conflict_symbol);
+                                       Report.Error (533, Location, "`{0}' hides inherited abstract member `{1}'",
+                                               GetSignatureForError (), conflict_symbol.GetSignatureForError ());
+                               }
                        }
  
                        return true;
                }
 
+               public virtual Constant ConvertInitializer (ResolveContext rc, Constant expr)
+               {
+                       return expr.ConvertImplicitly (rc, MemberType);
+               }
+
                protected override void DoMemberTypeDependentChecks ()
                {
                        base.DoMemberTypeDependentChecks ();
 
-                       if (TypeManager.IsGenericParameter (MemberType))
+                       if (MemberType.IsGenericParameter)
                                return;
 
-                       if (MemberType.IsSealed && MemberType.IsAbstract) {
+                       if (MemberType.IsStatic)
                                Error_VariableOfStaticClass (Location, GetSignatureForError (), MemberType, Report);
-                       }
 
                        CheckBase ();
                        IsTypePermitted ();
@@ -139,13 +154,13 @@ namespace Mono.CSharp
 
                public override void Emit ()
                {
-                       if (TypeManager.IsDynamicType (member_type)) {
+                       if (member_type == InternalType.Dynamic) {
                                PredefinedAttributes.Get.Dynamic.EmitAttribute (FieldBuilder);
                        } else {
                                var trans_flags = TypeManager.HasDynamicTypeUsed (member_type);
                                if (trans_flags != null) {
                                        var pa = PredefinedAttributes.Get.DynamicTransform;
-                                       if (pa.Constructor != null || pa.ResolveConstructor (Location, TypeManager.bool_type.MakeArrayType ())) {
+                                       if (pa.Constructor != null || pa.ResolveConstructor (Location, ArrayContainer.MakeType (TypeManager.bool_type, 1))) {
                                                FieldBuilder.SetCustomAttribute (new CustomAttributeBuilder (pa.Constructor, new object[] { trans_flags }));
                                        }
                                }
@@ -165,7 +180,7 @@ namespace Mono.CSharp
                        base.Emit ();
                }
 
-               public static void Error_VariableOfStaticClass (Location loc, string variable_name, Type static_class, Report Report)
+               public static void Error_VariableOfStaticClass (Location loc, string variable_name, TypeSpec static_class, Report Report)
                {
                        Report.SymbolRelatedToPreviousError (static_class);
                        Report.Error (723, loc, "`{0}': cannot declare variables of static types",
@@ -173,6 +188,9 @@ namespace Mono.CSharp
                }
 
                public Expression Initializer {
+                       get {
+                               return initializer;
+                       }
                        set {
                                if (value != null) {
                                        this.initializer = value;
@@ -180,13 +198,8 @@ namespace Mono.CSharp
                        }
                }
 
-               protected virtual bool IsFieldClsCompliant {
-                       get {
-                               if (FieldBuilder == null)
-                                       return true;
-
-                               return AttributeTester.IsClsCompliant (FieldBuilder.FieldType);
-                       }
+               public FieldSpec Spec {
+                       get { return spec; }
                }
 
                public override string[] ValidAttributeTargets 
@@ -201,55 +214,97 @@ namespace Mono.CSharp
                        if (!base.VerifyClsCompliance ())
                                return false;
 
-                       if (!IsFieldClsCompliant) {
+                       if (!MemberType.IsCLSCompliant () || this is FixedField) {
                                Report.Warning (3003, 1, Location, "Type of `{0}' is not CLS-compliant",
                                        GetSignatureForError ());
                        }
                        return true;
                }
-
-               public void SetAssigned ()
-               {
-                       caching_flags |= Flags.IsAssigned;
-               }
        }
 
-       interface IFixedBuffer
-       {
-               FieldInfo Element { get; }
-               Type ElementType { get; }
-       }
-
-       public class FixedFieldExternal: IFixedBuffer
+       //
+       // Field specification
+       //
+       public class FieldSpec : MemberSpec, IInterfaceMemberSpec
        {
-               FieldInfo element_field;
+               FieldInfo metaInfo;
+               TypeSpec memberType;
 
-               public FixedFieldExternal (FieldInfo fi)
+               public FieldSpec (TypeSpec declaringType, IMemberDefinition definition, TypeSpec memberType, FieldInfo info, Modifiers modifiers)
+                       : base (MemberKind.Field, declaringType, definition, modifiers)
                {
-                       element_field = fi.FieldType.GetField (FixedField.FixedElementName);
+                       this.metaInfo = info;
+                       this.memberType = memberType;
                }
 
-               #region IFixedField Members
+#region Properties
 
-               public FieldInfo Element {
+               public bool IsReadOnly {
                        get {
-                               return element_field;
+                               return (Modifiers & Modifiers.READONLY) != 0;
                        }
                }
 
-               public Type ElementType {
+               public TypeSpec MemberType {
                        get {
-                               return element_field.FieldType;
+                               return memberType;
+                       }
+               }
+
+#endregion
+
+               public FieldInfo GetMetaInfo ()
+               {
+                       if ((state & StateFlags.PendingMetaInflate) != 0) {
+                               var decl_meta = DeclaringType.GetMetaInfo ();
+                               if (DeclaringType.IsTypeBuilder) {
+                                       metaInfo = TypeBuilder.GetField (decl_meta, metaInfo);
+                               } else {
+                                       var orig_token = metaInfo.MetadataToken;
+                                       metaInfo = decl_meta.GetField (Name);
+                                       if (metaInfo.MetadataToken != orig_token)
+                                               throw new NotImplementedException ("Resolved to wrong meta token");
+
+                                       // What a stupid API, does not work because field handle is imported
+                                       // metaInfo = FieldInfo.GetFieldFromHandle (metaInfo.FieldHandle, DeclaringType.MetaInfo.TypeHandle);
+                               }
+
+                               state &= ~StateFlags.PendingMetaInflate;
                        }
+
+                       return metaInfo;
+               }
+
+               public override MemberSpec InflateMember (TypeParameterInflator inflator)
+               {
+                       var fs = (FieldSpec) base.InflateMember (inflator);
+                       fs.memberType = inflator.Inflate (memberType);
+                       return fs;
                }
 
-               #endregion
+               public FieldSpec Mutate (TypeParameterMutator mutator)
+               {
+                       var decl = DeclaringType;
+                       if (DeclaringType.IsGenericOrParentIsGeneric)
+                               decl = mutator.Mutate (decl);
+
+                       if (decl == DeclaringType)
+                               return this;
+
+                       var fs = (FieldSpec) MemberwiseClone ();
+                       fs.declaringType = decl;
+                       fs.state |= StateFlags.PendingMetaInflate;
+
+                       // Gets back FieldInfo in case of metaInfo was inflated
+                       fs.metaInfo = MemberCache.GetMember (DeclaringType.GetDefinition (), this).metaInfo;
+                       return fs;
+               }
        }
 
        /// <summary>
        /// Fixed buffer implementation
        /// </summary>
-       public class FixedField : FieldBase, IFixedBuffer
+       public class FixedField : FieldBase
        {
                public const string FixedElementName = "FixedElementField";
                static int GlobalCounter = 0;
@@ -257,8 +312,6 @@ namespace Mono.CSharp
                static FieldInfo[] fi;
 
                TypeBuilder fixed_buffer_type;
-               FieldBuilder element;
-               Expression size_expr;
 
                const Modifiers AllowedModifiers =
                        Modifiers.NEW |
@@ -272,34 +325,38 @@ namespace Mono.CSharp
                        Expression size_expr, Attributes attrs, Location loc):
                        base (parent, type, mod, AllowedModifiers, new MemberName (name, loc), attrs)
                {
-                       if (RootContext.Version < LanguageVersion.ISO_2)
-                               Report.FeatureIsNotAvailable (loc, "fixed size buffers");
+                       initializer = new ConstInitializer (this, size_expr);
+               }
 
-                       this.size_expr = size_expr;
+               public override Constant ConvertInitializer (ResolveContext rc, Constant expr)
+               {
+                       return expr.ImplicitConversionRequired (rc, TypeManager.int32_type, Location);
                }
 
-               public override bool Define()
+               public override bool Define ()
                {
                        if (!base.Define ())
                                return false;
 
                        if (!TypeManager.IsPrimitiveType (MemberType)) {
-                               Report.Error (1663, Location, "`{0}': Fixed size buffers type must be one of the following: bool, byte, short, int, long, char, sbyte, ushort, uint, ulong, float or double",
+                               Report.Error (1663, Location,
+                                       "`{0}': Fixed size buffers type must be one of the following: bool, byte, short, int, long, char, sbyte, ushort, uint, ulong, float or double",
                                        GetSignatureForError ());
                        }                       
                        
                        // Create nested fixed buffer container
                        string name = String.Format ("<{0}>__FixedBuffer{1}", Name, GlobalCounter++);
                        fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name, Parent.Module.DefaultCharSetType |
-                               TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, TypeManager.value_type);
-                       
-                       element = fixed_buffer_type.DefineField (FixedElementName, MemberType, FieldAttributes.Public);
+                               TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, TypeManager.value_type.GetMetaInfo ());
+
+                       fixed_buffer_type.DefineField (FixedElementName, MemberType.GetMetaInfo (), FieldAttributes.Public);
                        RootContext.RegisterCompilerGeneratedType (fixed_buffer_type);
                        
                        FieldBuilder = Parent.TypeBuilder.DefineField (Name, fixed_buffer_type, ModifiersExtensions.FieldAttr (ModFlags));
-                       Parent.MemberCache.AddMember (FieldBuilder, this);
-                       TypeManager.RegisterFieldBase (FieldBuilder, this);
+                       var element_spec = new FieldSpec (null, this, MemberType, FieldBuilder, ModFlags);
+                       spec = new FixedFieldSpec (Parent.Definition, this, FieldBuilder, element_spec, ModFlags);
 
+                       Parent.MemberCache.AddMember (spec);
                        return true;
                }
 
@@ -310,7 +367,7 @@ namespace Mono.CSharp
                        if (!IsUnsafe)
                                Expression.UnsafeError (Report, Location);
 
-                       if (Parent.PartialContainer.Kind != Kind.Struct) {
+                       if (Parent.PartialContainer.Kind != MemberKind.Struct) {
                                Report.Error (1642, Location, "`{0}': Fixed size buffer fields may only be members of structs",
                                        GetSignatureForError ());
                        }
@@ -319,11 +376,7 @@ namespace Mono.CSharp
                public override void Emit()
                {
                        ResolveContext rc = new ResolveContext (this);
-                       Constant c = size_expr.ResolveAsConstant (rc, this);
-                       if (c == null)
-                               return;
-                       
-                       IntConstant buffer_size_const = c.ImplicitConversionRequired (rc, TypeManager.int32_type, Location) as IntConstant;
+                       IntConstant buffer_size_const = initializer.Resolve (rc) as IntConstant;
                        if (buffer_size_const == null)
                                return;
 
@@ -361,8 +414,10 @@ namespace Mono.CSharp
                                        return;
 
                        // TODO: It's not cleared
-                       if (fi == null)
-                               fi = new FieldInfo[] { pa.Type.GetField ("Size") };
+                       if (fi == null) {
+                               var field = (FieldSpec) MemberCache.FindMember (pa.Type, MemberFilter.Field ("Size", null), BindingRestriction.DeclaredOnly);
+                               fi = new FieldInfo[] { field.GetMetaInfo () };
+                       }
 
                        object[] fi_val = new object[] { buffer_size };
                        cab = new CustomAttributeBuilder (pa.Constructor,
@@ -380,16 +435,10 @@ namespace Mono.CSharp
                                !pa.ResolveConstructor (Location, TypeManager.type_type, TypeManager.int32_type))
                                return;
 
-                       cab = new CustomAttributeBuilder (pa.Constructor, new object[] { MemberType, buffer_size });
+                       cab = new CustomAttributeBuilder (pa.Constructor, new object[] { MemberType.GetMetaInfo (), buffer_size });
                        FieldBuilder.SetCustomAttribute (cab);
                }
 
-               protected override bool IsFieldClsCompliant {
-                       get {
-                               return false;
-                       }
-               }
-
                public void SetCharSet (TypeAttributes ta)
                {
                        TypeAttributes cta = fixed_buffer_type.Attributes;
@@ -410,22 +459,32 @@ namespace Mono.CSharp
                                mi.Invoke (fixed_buffer_type, new object [] { ta });
                        }
                }
+       }
 
-               #region IFixedField Members
+       class FixedFieldSpec : FieldSpec
+       {
+               readonly FieldSpec element;
+
+               public FixedFieldSpec (TypeSpec declaringType, IMemberDefinition definition, FieldInfo info, FieldSpec element, Modifiers modifiers)
+                       : base (declaringType, definition, element.MemberType, info, modifiers)
+               {
+                       this.element = element;
+
+                       // It's never CLS-Compliant
+                       state &= ~StateFlags.CLSCompliant_Undetected;
+               }
 
-               public FieldInfo Element {
+               public FieldSpec Element {
                        get {
                                return element;
                        }
                }
 
-               public Type ElementType {
+               public TypeSpec ElementType {
                        get {
                                return MemberType;
                        }
                }
-
-               #endregion
        }
 
        //
@@ -465,39 +524,12 @@ namespace Mono.CSharp
                                MemberType == TypeManager.intptr_type || MemberType == TypeManager.uintptr_type)
                                return true;
 
-                       if (TypeManager.IsEnumType (MemberType))
+                       if (MemberType.IsEnum)
                                return true;
 
                        return false;
                }
 
-               bool CheckStructLayout (Type type, bool isStatic)
-               {
-                       if (TypeManager.IsBuiltinType (type))
-                               return true;
-
-                       if (isStatic) {
-                               if (!TypeManager.IsValueType (type) || TypeManager.IsEqual (type, Parent.TypeBuilder))
-                                       return true;
-                       }
-
-                       if (!TypeManager.IsEqual (TypeManager.DropGenericTypeArguments (type), Parent.TypeBuilder)) {
-                               if (!TypeManager.IsGenericType (type))
-                                       return true;
-
-                               foreach (Type t in TypeManager.GetTypeArguments (type)) {
-                                       if (!CheckStructLayout (t, false))
-                                               return false;
-                               }
-                               return true;
-                       }
-                       
-                       Report.Error (523, Location,
-                               "Struct member `{0}' of type `{1}' causes a cycle in the struct layout",
-                               GetSignatureForError (), TypeManager.CSharpName (MemberType));
-                       return false;
-               }
-
                public override bool Define ()
                {
                        if (!base.Define ())
@@ -508,21 +540,21 @@ namespace Mono.CSharp
                                if ((ModFlags & Modifiers.VOLATILE) != 0) {
                                        if (TypeManager.isvolatile_type == null)
                                                TypeManager.isvolatile_type = TypeManager.CoreLookupType (Compiler,
-                                                       "System.Runtime.CompilerServices", "IsVolatile", Kind.Class, true);
+                                                       "System.Runtime.CompilerServices", "IsVolatile", MemberKind.Class, true);
 
                                        if (TypeManager.isvolatile_type != null)
-                                               required_modifier = new Type [] { TypeManager.isvolatile_type };
+                                               required_modifier = new Type[] { TypeManager.isvolatile_type.GetMetaInfo () };
                                }
 
                                FieldBuilder = Parent.TypeBuilder.DefineField (
-                                       Name, MemberType, required_modifier, null, ModifiersExtensions.FieldAttr (ModFlags));
+                                       Name, member_type.GetMetaInfo (), required_modifier, null, ModifiersExtensions.FieldAttr (ModFlags));
+
+                               spec = new FieldSpec (Parent.Definition, this, MemberType, FieldBuilder, ModFlags);
 
                                // Don't cache inaccessible fields
                                if ((ModFlags & Modifiers.BACKING_FIELD) == 0) {
-                                       Parent.MemberCache.AddMember (FieldBuilder, this);
+                                       Parent.MemberCache.AddMember (spec);
                                }
-
-                               TypeManager.RegisterFieldBase (FieldBuilder, this);
                        }
                        catch (ArgumentException) {
                                Report.RuntimeMissingSupport (Location, "`void' or `void*' field type");
@@ -531,10 +563,7 @@ namespace Mono.CSharp
 
                        if (initializer != null) {
                                ((TypeContainer) Parent).RegisterFieldForInitialization (this,
-                                       new FieldInitializer (FieldBuilder, initializer, this));
-                       } else {
-                               if (Parent.PartialContainer.Kind == Kind.Struct)
-                                       CheckStructLayout (member_type, (ModFlags & Modifiers.STATIC) != 0);
+                                       new FieldInitializer (this, initializer, this));
                        }
 
                        return true;
@@ -542,6 +571,9 @@ namespace Mono.CSharp
 
                protected override void DoMemberTypeDependentChecks ()
                {
+                       if ((ModFlags & Modifiers.BACKING_FIELD) != 0)
+                               return;
+
                        base.DoMemberTypeDependentChecks ();
 
                        if ((ModFlags & Modifiers.VOLATILE) != 0) {