Update
[mono.git] / mcs / mcs / field.cs
index e43bf5ba4c53d5db0981fd7f1a96fc28487379f2..49dbb3d400d0bd43e587908e41c6ff097a4431bd 100644 (file)
@@ -19,15 +19,32 @@ using System.Runtime.InteropServices;
 
 namespace Mono.CSharp
 {
+       public class FieldDeclarator
+       {
+               public FieldDeclarator (SimpleMemberName name, Expression initializer)
+               {
+                       this.Name = name;
+                       this.Initializer = initializer;
+               }
+
+               #region Properties
+
+               public SimpleMemberName Name { get; private set; }
+               public Expression Initializer { get; private set; }
+
+               #endregion
+       }
+
        //
        // Abstract class for all fields
        //
        abstract public class FieldBase : MemberBase
        {
-               public FieldBuilder FieldBuilder;
+               protected FieldBuilder FieldBuilder;
                protected FieldSpec spec;
                public Status status;
                protected Expression initializer;
+               protected List<FieldDeclarator> declarators;
 
                [Flags]
                public enum Status : byte {
@@ -45,12 +62,48 @@ namespace Mono.CSharp
                                Report.Error (681, Location, "The modifier 'abstract' is not valid on fields. Try using a property instead");
                }
 
+               #region Properties
+
                public override AttributeTargets AttributeTargets {
                        get {
                                return AttributeTargets.Field;
                        }
                }
 
+               public Expression Initializer {
+                       get {
+                               return initializer;
+                       }
+                       set {
+                               this.initializer = value;
+                       }
+               }
+
+               public FieldSpec Spec {
+                       get {
+                               return spec;
+                       }
+               }
+
+               public override string[] ValidAttributeTargets  {
+                       get {
+                               return attribute_targets;
+                       }
+               }
+
+               #endregion
+
+               public void AddDeclarator (FieldDeclarator declarator)
+               {
+                       if (declarators == null)
+                               declarators = new List<FieldDeclarator> (2);
+
+                       declarators.Add (declarator);
+
+                       // TODO: This will probably break
+                       Parent.AddMember (this, declarator.Name.Value);
+               }
+
                public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
                {
                        if (a.Type == pa.FieldOffset) {
@@ -155,19 +208,13 @@ namespace Mono.CSharp
                public override void Emit ()
                {
                        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, ArrayContainer.MakeType (TypeManager.bool_type, 1))) {
-                                               FieldBuilder.SetCustomAttribute (new CustomAttributeBuilder (pa.Constructor, new object[] { trans_flags }));
-                                       }
-                               }
+                               Compiler.PredefinedAttributes.Dynamic.EmitAttribute (FieldBuilder);
+                       } else if (member_type.HasDynamicElement) {
+                               Compiler.PredefinedAttributes.Dynamic.EmitAttribute (FieldBuilder, member_type);
                        }
 
                        if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
-                               PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (FieldBuilder);
+                               Compiler.PredefinedAttributes.CompilerGenerated.EmitAttribute (FieldBuilder);
 
                        if (OptAttributes != null) {
                                OptAttributes.Emit ();
@@ -187,28 +234,6 @@ namespace Mono.CSharp
                                variable_name);
                }
 
-               public Expression Initializer {
-                       get {
-                               return initializer;
-                       }
-                       set {
-                               if (value != null) {
-                                       this.initializer = value;
-                               }
-                       }
-               }
-
-               public FieldSpec Spec {
-                       get { return spec; }
-               }
-
-               public override string[] ValidAttributeTargets 
-               {
-                       get {
-                               return attribute_targets;
-                       }
-               }
-
                protected override bool VerifyClsCompliance ()
                {
                        if (!base.VerifyClsCompliance ())
@@ -261,7 +286,7 @@ namespace Mono.CSharp
                                        metaInfo = TypeBuilder.GetField (decl_meta, metaInfo);
                                } else {
                                        var orig_token = metaInfo.MetadataToken;
-                                       metaInfo = decl_meta.GetField (Name);
+                                       metaInfo = decl_meta.GetField (Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
                                        if (metaInfo.MetadataToken != orig_token)
                                                throw new NotImplementedException ("Resolved to wrong meta token");
 
@@ -296,7 +321,7 @@ namespace Mono.CSharp
                        fs.state |= StateFlags.PendingMetaInflate;
 
                        // Gets back FieldInfo in case of metaInfo was inflated
-                       fs.metaInfo = MemberCache.GetMember (DeclaringType.GetDefinition (), this).metaInfo;
+                       fs.metaInfo = MemberCache.GetMember (TypeParameterMutator.GetMemberDeclaringType (DeclaringType), this).metaInfo;
                        return fs;
                }
        }
@@ -321,11 +346,9 @@ namespace Mono.CSharp
                        Modifiers.PRIVATE |
                        Modifiers.UNSAFE;
 
-               public FixedField (DeclSpace parent, FullNamedExpression type, Modifiers mod, string name,
-                       Expression size_expr, Attributes attrs, Location loc):
-                       base (parent, type, mod, AllowedModifiers, new MemberName (name, loc), attrs)
+               public FixedField (DeclSpace parent, FullNamedExpression type, Modifiers mod, MemberName name, Attributes attrs)
+                       : base (parent, type, mod, AllowedModifiers, name, attrs)
                {
-                       initializer = new ConstInitializer (this, size_expr);
                }
 
                public override Constant ConvertInitializer (ResolveContext rc, Constant expr)
@@ -342,7 +365,16 @@ namespace Mono.CSharp
                                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 ());
-                       }                       
+                       } else if (declarators != null) {
+                               var t = new TypeExpression (MemberType, TypeExpression.Location);
+                               int index = Parent.PartialContainer.Fields.IndexOf (this);
+                               foreach (var d in declarators) {
+                                       var f = new FixedField (Parent, t, ModFlags, new MemberName (d.Name.Value, d.Name.Location), OptAttributes);
+                                       f.initializer = d.Initializer;
+                                       ((ConstInitializer) f.initializer).Name = d.Name.Value;
+                                       Parent.PartialContainer.Fields.Insert (++index, f);
+                               }
+                       }
                        
                        // Create nested fixed buffer container
                        string name = String.Format ("<{0}>__FixedBuffer{1}", Name, GlobalCounter++);
@@ -398,7 +430,7 @@ namespace Mono.CSharp
                        buffer_size *= type_size;
                        EmitFieldSize (buffer_size);
 
-                       PredefinedAttributes.Get.UnsafeValueType.EmitAttribute (fixed_buffer_type);
+                       Compiler.PredefinedAttributes.UnsafeValueType.EmitAttribute (fixed_buffer_type);
 
                        base.Emit ();
                }
@@ -408,7 +440,7 @@ namespace Mono.CSharp
                        CustomAttributeBuilder cab;
                        PredefinedAttribute pa;
 
-                       pa = PredefinedAttributes.Get.StructLayout;
+                       pa = Compiler.PredefinedAttributes.StructLayout;
                        if (pa.Constructor == null &&
                                !pa.ResolveConstructor (Location, TypeManager.short_type))
                                        return;
@@ -430,7 +462,7 @@ namespace Mono.CSharp
                        if ((ModFlags & Modifiers.PRIVATE) != 0)
                                return;
 
-                       pa = PredefinedAttributes.Get.FixedBuffer;
+                       pa = Compiler.PredefinedAttributes.FixedBuffer;
                        if (pa.Constructor == null &&
                                !pa.ResolveConstructor (Location, TypeManager.type_type, TypeManager.int32_type))
                                return;
@@ -535,35 +567,41 @@ namespace Mono.CSharp
                        if (!base.Define ())
                                return false;
 
-                       try {
-                               Type[] required_modifier = null;
-                               if ((ModFlags & Modifiers.VOLATILE) != 0) {
-                                       if (TypeManager.isvolatile_type == null)
-                                               TypeManager.isvolatile_type = TypeManager.CoreLookupType (Compiler,
-                                                       "System.Runtime.CompilerServices", "IsVolatile", MemberKind.Class, true);
+                       Type[] required_modifier = null;
+                       if ((ModFlags & Modifiers.VOLATILE) != 0) {
+                               if (TypeManager.isvolatile_type == null)
+                                       TypeManager.isvolatile_type = TypeManager.CoreLookupType (Compiler,
+                                               "System.Runtime.CompilerServices", "IsVolatile", MemberKind.Class, true);
 
-                                       if (TypeManager.isvolatile_type != null)
-                                               required_modifier = new Type[] { TypeManager.isvolatile_type.GetMetaInfo () };
-                               }
+                               if (TypeManager.isvolatile_type != null)
+                                       required_modifier = new Type[] { TypeManager.isvolatile_type.GetMetaInfo () };
+                       }
 
-                               FieldBuilder = Parent.TypeBuilder.DefineField (
-                                       Name, member_type.GetMetaInfo (), required_modifier, null, ModifiersExtensions.FieldAttr (ModFlags));
+                       FieldBuilder = Parent.TypeBuilder.DefineField (
+                               Name, member_type.GetMetaInfo (), required_modifier, null, ModifiersExtensions.FieldAttr (ModFlags));
 
-                               spec = new FieldSpec (Parent.Definition, this, MemberType, FieldBuilder, ModFlags);
+                       spec = new FieldSpec (Parent.Definition, this, MemberType, FieldBuilder, ModFlags);
 
-                               // Don't cache inaccessible fields
-                               if ((ModFlags & Modifiers.BACKING_FIELD) == 0) {
-                                       Parent.MemberCache.AddMember (spec);
-                               }
-                       }
-                       catch (ArgumentException) {
-                               Report.RuntimeMissingSupport (Location, "`void' or `void*' field type");
-                               return false;
+                       // Don't cache inaccessible fields
+                       if ((ModFlags & Modifiers.BACKING_FIELD) == 0) {
+                               Parent.MemberCache.AddMember (spec);
                        }
 
                        if (initializer != null) {
                                ((TypeContainer) Parent).RegisterFieldForInitialization (this,
-                                       new FieldInitializer (this, initializer, this));
+                                       new FieldInitializer (spec, initializer, this));
+                       }
+
+                       if (declarators != null) {
+                               var t = new TypeExpression (MemberType, TypeExpression.Location);
+                               int index = Parent.PartialContainer.Fields.IndexOf (this);
+                               foreach (var d in declarators) {
+                                       var f = new Field (Parent, t, ModFlags, new MemberName (d.Name.Value, d.Name.Location), OptAttributes);
+                                       if (d.Initializer != null)
+                                               f.initializer = d.Initializer;
+
+                                       Parent.PartialContainer.Fields.Insert (++index, f);
+                               }
                        }
 
                        return true;