Updated with Xamarin copyrights
[mono.git] / mcs / mcs / field.cs
index 3fd32dfe5c27f043c0fedf564778127998a658c0..ec3e050205aebb17a6ada15d3b5adcf288e419f0 100644 (file)
@@ -9,6 +9,7 @@
 //
 // Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
 // Copyright 2004-2008 Novell, Inc
+// Copyright 2011 Xamarin Inc
 //
 
 using System;
@@ -166,7 +167,8 @@ namespace Mono.CSharp
                                return false;
 
                        MemberSpec candidate;
-                       var conflict_symbol = MemberCache.FindBaseMember (this, out candidate);
+                       bool overrides = false;
+                       var conflict_symbol = MemberCache.FindBaseMember (this, out candidate, ref overrides);
                        if (conflict_symbol == null)
                                conflict_symbol = candidate;
 
@@ -194,7 +196,7 @@ namespace Mono.CSharp
 
                public virtual Constant ConvertInitializer (ResolveContext rc, Constant expr)
                {
-                       return expr.ConvertImplicitly (rc, MemberType);
+                       return expr.ConvertImplicitly (MemberType);
                }
 
                protected override void DoMemberTypeDependentChecks ()
@@ -220,7 +222,7 @@ namespace Mono.CSharp
 
                public override void Emit ()
                {
-                       if (member_type == InternalType.Dynamic) {
+                       if (member_type.BuiltinType == BuiltinTypeSpec.Type.Dynamic) {
                                Module.PredefinedAttributes.Dynamic.EmitAttribute (FieldBuilder);
                        } else if (!(Parent is CompilerGeneratedClass) && member_type.HasDynamicElement) {
                                Module.PredefinedAttributes.Dynamic.EmitAttribute (FieldBuilder, member_type, Location);
@@ -237,6 +239,8 @@ namespace Mono.CSharp
                                Report.Error (625, Location, "`{0}': Instance field types marked with StructLayout(LayoutKind.Explicit) must have a FieldOffset attribute", GetSignatureForError ());
                        }
 
+                       ConstraintChecker.Check (this, member_type, type_expr.Location);
+
                        base.Emit ();
                }
 
@@ -380,7 +384,7 @@ namespace Mono.CSharp
 
                public override Constant ConvertInitializer (ResolveContext rc, Constant expr)
                {
-                       return expr.ImplicitConversionRequired (rc, TypeManager.int32_type, Location);
+                       return expr.ImplicitConversionRequired (rc, rc.BuiltinTypes.Int, Location);
                }
 
                public override bool Define ()
@@ -388,7 +392,7 @@ namespace Mono.CSharp
                        if (!base.Define ())
                                return false;
 
-                       if (!BuildinTypeSpec.IsPrimitiveType (MemberType)) {
+                       if (!BuiltinTypeSpec.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",
                                        GetSignatureForError ());
@@ -406,12 +410,14 @@ namespace Mono.CSharp
                        // Create nested fixed buffer container
                        string name = String.Format ("<{0}>__FixedBuffer{1}", Name, GlobalCounter++);
                        fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name,
-                               TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, TypeManager.value_type.GetMetaInfo ());
+                               TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit,
+                               Compiler.BuiltinTypes.ValueType.GetMetaInfo ());
 
-                       fixed_buffer_type.DefineField (FixedElementName, MemberType.GetMetaInfo (), FieldAttributes.Public);
+                       var ffield = fixed_buffer_type.DefineField (FixedElementName, MemberType.GetMetaInfo (), FieldAttributes.Public);
                        
                        FieldBuilder = Parent.TypeBuilder.DefineField (Name, fixed_buffer_type, ModifiersExtensions.FieldAttr (ModFlags));
-                       var element_spec = new FieldSpec (null, this, MemberType, FieldBuilder, ModFlags);
+
+                       var element_spec = new FieldSpec (null, this, MemberType, ffield, ModFlags);
                        spec = new FixedFieldSpec (Parent.Definition, this, FieldBuilder, element_spec, ModFlags);
 
                        Parent.MemberCache.AddMember (spec);
@@ -445,14 +451,6 @@ namespace Mono.CSharp
                                return;
                        }
 
-                       int type_size = BuildinTypeSpec.GetSize (MemberType);
-
-                       if (buffer_size > int.MaxValue / type_size) {
-                               Report.Error (1664, Location, "Fixed size buffer `{0}' of length `{1}' and type `{2}' exceeded 2^31 limit",
-                                       GetSignatureForError (), buffer_size.ToString (), TypeManager.CSharpName (MemberType));
-                               return;
-                       }
-
                        EmitFieldSize (buffer_size);
 
 #if STATIC
@@ -469,19 +467,22 @@ namespace Mono.CSharp
 
                void EmitFieldSize (int buffer_size)
                {
-                       PredefinedAttribute pa;
-                       AttributeEncoder encoder;
+                       int type_size = BuiltinTypeSpec.GetSize (MemberType);
 
-                       pa = Module.PredefinedAttributes.StructLayout;
-                       if (pa.Constructor == null && !pa.ResolveConstructor (Location, TypeManager.short_type))
+                       if (buffer_size > int.MaxValue / type_size) {
+                               Report.Error (1664, Location, "Fixed size buffer `{0}' of length `{1}' and type `{2}' exceeded 2^31 limit",
+                                       GetSignatureForError (), buffer_size.ToString (), TypeManager.CSharpName (MemberType));
                                return;
+                       }
+
+                       AttributeEncoder encoder;
 
-                       var char_set_type = Module.PredefinedTypes.CharSet.Resolve (Location);
-                       if (char_set_type == null)
+                       var ctor = Module.PredefinedMembers.StructLayoutAttributeCtor.Resolve (Location);
+                       if (ctor == null)
                                return;
 
-                       var field_size = pa.GetField ("Size", TypeManager.int32_type, Location);
-                       var field_charset = pa.GetField ("CharSet", char_set_type, Location);
+                       var field_size = Module.PredefinedMembers.StructLayoutSize.Resolve (Location);
+                       var field_charset = Module.PredefinedMembers.StructLayoutCharSet.Resolve (Location);
                        if (field_size == null || field_charset == null)
                                return;
 
@@ -491,10 +492,13 @@ namespace Mono.CSharp
                        encoder.Encode ((short)LayoutKind.Sequential);
                        encoder.EncodeNamedArguments (
                                new [] { field_size, field_charset },
-                               new Constant [] { new IntConstant (buffer_size, Location), new IntConstant ((int) char_set, Location) }
+                               new Constant [] { 
+                                       new IntConstant (Compiler.BuiltinTypes, buffer_size * type_size, Location),
+                                       new IntConstant (Compiler.BuiltinTypes, (int) char_set, Location)
+                               }
                        );
 
-                       pa.EmitAttribute (fixed_buffer_type, encoder);
+                       fixed_buffer_type.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), encoder.ToArray ());
 
                        //
                        // Don't emit FixedBufferAttribute attribute for private types
@@ -502,8 +506,8 @@ namespace Mono.CSharp
                        if ((ModFlags & Modifiers.PRIVATE) != 0)
                                return;
 
-                       pa = Module.PredefinedAttributes.FixedBuffer;
-                       if (pa.Constructor == null && !pa.ResolveConstructor (Location, Compiler.BuildinTypes.Type, TypeManager.int32_type))
+                       ctor = Module.PredefinedMembers.FixedBufferAttributeCtor.Resolve (Location);
+                       if (ctor == null)
                                return;
 
                        encoder = new AttributeEncoder ();
@@ -511,7 +515,7 @@ namespace Mono.CSharp
                        encoder.Encode (buffer_size);
                        encoder.EncodeEmptyNamedArguments ();
 
-                       pa.EmitAttribute (FieldBuilder, encoder);
+                       FieldBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), encoder.ToArray ());
                }
        }
 
@@ -567,23 +571,23 @@ namespace Mono.CSharp
 
                bool CanBeVolatile ()
                {
-                       if (TypeManager.IsReferenceType (MemberType))
+                       switch (MemberType.BuiltinType) {
+                       case BuiltinTypeSpec.Type.Bool:
+                       case BuiltinTypeSpec.Type.Char:
+                       case BuiltinTypeSpec.Type.SByte:
+                       case BuiltinTypeSpec.Type.Byte:
+                       case BuiltinTypeSpec.Type.Short:
+                       case BuiltinTypeSpec.Type.UShort:
+                       case BuiltinTypeSpec.Type.Int:
+                       case BuiltinTypeSpec.Type.UInt:
+                       case BuiltinTypeSpec.Type.Float:
+                       case BuiltinTypeSpec.Type.UIntPtr:
+                       case BuiltinTypeSpec.Type.IntPtr:
                                return true;
+                       }
 
-                       switch (MemberType.BuildinType) {
-                       case BuildinTypeSpec.Type.Bool:
-                       case BuildinTypeSpec.Type.Char:
-                       case BuildinTypeSpec.Type.SByte:
-                       case BuildinTypeSpec.Type.Byte:
-                       case BuildinTypeSpec.Type.Short:
-                       case BuildinTypeSpec.Type.UShort:
-                       case BuildinTypeSpec.Type.Int:
-                       case BuildinTypeSpec.Type.UInt:
-                       case BuildinTypeSpec.Type.Float:
-                       case BuildinTypeSpec.Type.UIntPtr:
-                       case BuildinTypeSpec.Type.IntPtr:
+                       if (TypeSpec.IsReferenceType (MemberType))
                                return true;
-                       }
 
                        if (MemberType.IsEnum)
                                return true;
@@ -598,7 +602,7 @@ namespace Mono.CSharp
 
                        MetaType[] required_modifier = null;
                        if ((ModFlags & Modifiers.VOLATILE) != 0) {
-                               var mod = Module.PredefinedTypes.IsVolatile.Resolve (Location);
+                               var mod = Module.PredefinedTypes.IsVolatile.Resolve ();
                                if (mod != null)
                                        required_modifier = new MetaType[] { mod.GetMetaInfo () };
                        }
@@ -608,8 +612,11 @@ namespace Mono.CSharp
 
                        spec = new FieldSpec (Parent.Definition, this, MemberType, FieldBuilder, ModFlags);
 
-                       // Don't cache inaccessible fields
-                       if ((ModFlags & Modifiers.BACKING_FIELD) == 0) {
+                       //
+                       // Don't cache inaccessible fields except for struct where we
+                       // need them for definitive assignment checks
+                       //
+                       if ((ModFlags & Modifiers.BACKING_FIELD) == 0 || Parent.Kind == MemberKind.Struct) {
                                Parent.MemberCache.AddMember (spec);
                        }