Clean up resolving of DefaultCharSetAttribute
authorMarek Safar <marek.safar@gmail.com>
Wed, 19 Jan 2011 09:21:35 +0000 (09:21 +0000)
committerMarek Safar <marek.safar@gmail.com>
Wed, 19 Jan 2011 17:58:05 +0000 (17:58 +0000)
mcs/mcs/attribute.cs
mcs/mcs/class.cs
mcs/mcs/decl.cs
mcs/mcs/field.cs
mcs/mcs/generic.cs
mcs/mcs/roottypes.cs

index e0f5a137549cfc807b16ff732fefe022dba41c59..f950d2102e7f7827d61354ec927fcd608595fe09 100644 (file)
@@ -410,10 +410,6 @@ namespace Mono.CSharp {
                        // Add [module: DefaultCharSet] to all DllImport import attributes
                        //
                        var module = context.Module;
-                       // HACK: Needed for broken ModuleContainer::ResolveGlobalAttributes
-                       if (module.PredefinedAttributes == null)
-                               return ctor;
-
                        if (Type == module.PredefinedAttributes.DllImport && module.HasDefaultCharSet) {
                                AddModuleCharSet (rc);
                        }
@@ -1646,7 +1642,7 @@ namespace Mono.CSharp {
                public readonly PredefinedAttribute UnverifiableCode;
 
                // New in .NET 2.0
-               //public readonly PredefinedAttribute DefaultCharset;
+               public readonly PredefinedAttribute DefaultCharset;
                public readonly PredefinedAttribute TypeForwarder;
                public readonly PredefinedAttribute FixedBuffer;
                public readonly PredefinedAttribute CompilerGenerated;
@@ -1699,7 +1695,7 @@ namespace Mono.CSharp {
                        OptionalParameter = new PredefinedAttribute (module, "System.Runtime.InteropServices", "OptionalAttribute");
                        UnverifiableCode = new PredefinedAttribute (module, "System.Security", "UnverifiableCodeAttribute");
 
-                       //DefaultCharset = new PredefinedAttribute (module, "System.Runtime.InteropServices", "DefaultCharSetAttribute");
+                       DefaultCharset = new PredefinedAttribute (module, "System.Runtime.InteropServices", "DefaultCharSetAttribute");
                        TypeForwarder = new PredefinedAttribute (module, "System.Runtime.CompilerServices", "TypeForwardedToAttribute");
                        FixedBuffer = new PredefinedAttribute (module, "System.Runtime.CompilerServices", "FixedBufferAttribute");
                        CompilerGenerated = new PredefinedAttribute (module, "System.Runtime.CompilerServices", "CompilerGeneratedAttribute");
index 3fd13b5dbc147cb792d019cccdd9fb4182a81f62..d91c433484940e0968fbaf4b4e4538b182997bd6 100644 (file)
@@ -656,7 +656,7 @@ namespace Mono.CSharp {
 
                protected override TypeAttributes TypeAttr {
                        get {
-                               return ModifiersExtensions.TypeAttr (ModFlags, IsTopLevel) | base.TypeAttr;
+                               return ModifiersExtensions.TypeAttr (ModFlags, IsTopLevel);
                        }
                }
 
@@ -1773,6 +1773,11 @@ namespace Mono.CSharp {
                        if ((ModFlags & Modifiers.COMPILER_GENERATED) != 0 && !Parent.IsCompilerGenerated)
                                Module.PredefinedAttributes.CompilerGenerated.EmitAttribute (TypeBuilder);
 
+#if STATIC
+                       if ((TypeBuilder.Attributes & TypeAttributes.StringFormatMask) == 0 && Module.HasDefaultCharSet)
+                               TypeBuilder.__SetAttributes (TypeBuilder.Attributes | Module.DefaultCharSetType);
+#endif
+
                        base.Emit ();
                }
 
index 8e2628f6f3dd20f197c5b41d5ba6203d143688bf..9776b8dd9738e8cd9c53cab1ebcb8b517a98e4dd 100644 (file)
@@ -1339,9 +1339,7 @@ namespace Mono.CSharp {
                        return false;
                }
 
-               protected virtual TypeAttributes TypeAttr {
-                       get { return Module.DefaultCharSetType; }
-               }
+               protected abstract TypeAttributes TypeAttr { get; }
 
                /// <remarks>
                ///  Should be overriten by the appropriate declaration space
index 222f013fb25e66628f26a213d673d1f4ac998b27..641fe206327642d537f3cdff2d51d631c1e04b7c 100644 (file)
@@ -405,7 +405,7 @@ namespace Mono.CSharp
                        
                        // Create nested fixed buffer container
                        string name = String.Format ("<{0}>__FixedBuffer{1}", Name, GlobalCounter++);
-                       fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name, Parent.Module.DefaultCharSetType |
+                       fixed_buffer_type = Parent.TypeBuilder.DefineNestedType (name,
                                TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, TypeManager.value_type.GetMetaInfo ());
 
                        fixed_buffer_type.DefineField (FixedElementName, MemberType.GetMetaInfo (), FieldAttributes.Public);
@@ -455,6 +455,11 @@ namespace Mono.CSharp
 
                        EmitFieldSize (buffer_size);
 
+#if STATIC
+                       if (Module.HasDefaultCharSet)
+                               fixed_buffer_type.__SetAttributes (fixed_buffer_type.Attributes | Module.DefaultCharSetType);
+#endif
+
                        Module.PredefinedAttributes.UnsafeValueType.EmitAttribute (fixed_buffer_type);
                        Module.PredefinedAttributes.CompilerGenerated.EmitAttribute (fixed_buffer_type);
                        fixed_buffer_type.CreateType ();
@@ -480,7 +485,7 @@ namespace Mono.CSharp
                        if (field_size == null || field_charset == null)
                                return;
 
-                       var char_set = CharSet ?? Module.DefaultCharSet;
+                       var char_set = CharSet ?? Module.DefaultCharSet ?? 0;
 
                        encoder = new AttributeEncoder (false);
                        encoder.Encode ((short)LayoutKind.Sequential);
index e11ee481598afcb5b74dbb576a576368dac5f101..21df6c3b2a33f1818d132f4814b71fcc80b75cd5 100644 (file)
@@ -2243,6 +2243,12 @@ namespace Mono.CSharp {
                        }
                }
 
+               protected override TypeAttributes TypeAttr {
+                       get {
+                               throw new NotSupportedException ();
+                       }
+               }
+
                public override void DefineType ()
                {
                        throw new Exception ();
index bea0408a25697387bf478c48682b30b1be474d56..7646a95250594afad7adc9632f1d65db6ccb0e27 100644 (file)
@@ -110,7 +110,7 @@ namespace Mono.CSharp
                        }
                }
 
-               public CharSet DefaultCharSet = CharSet.Ansi;
+               public CharSet? DefaultCharSet;
                public TypeAttributes DefaultCharSetType = TypeAttributes.AnsiClass;
 
                Dictionary<int, List<AnonymousTypeClass>> anonymous_types;
@@ -123,7 +123,6 @@ namespace Mono.CSharp
 
                ModuleBuilder builder;
 
-               bool has_default_charset;
                bool has_extenstion_method;
 
                PredefinedAttributes predefined_attributes;
@@ -172,7 +171,7 @@ namespace Mono.CSharp
 
                public bool HasDefaultCharSet {
                        get {
-                               return has_default_charset;
+                               return DefaultCharSet.HasValue;
                        }
                }
 
@@ -261,7 +260,25 @@ namespace Mono.CSharp
                                return;
                        }
 
-                       if (a.Type == pa.CLSCompliant) {
+                       if (a.Type == pa.DefaultCharset) {
+                               switch (a.GetCharSetValue ()) {
+                               case CharSet.Ansi:
+                               case CharSet.None:
+                                       break;
+                               case CharSet.Auto:
+                                       DefaultCharSet = CharSet.Auto;
+                                       DefaultCharSetType = TypeAttributes.AutoClass;
+                                       break;
+                               case CharSet.Unicode:
+                                       DefaultCharSet = CharSet.Unicode;
+                                       DefaultCharSetType = TypeAttributes.UnicodeClass;
+                                       break;
+                               default:
+                                       Report.Error (1724, a.Location, "Value specified for the argument to `{0}' is not valid",
+                                               a.GetSignatureForError ());
+                                       break;
+                               }
+                       } else if (a.Type == pa.CLSCompliant) {
                                Attribute cls = DeclaringAssembly.CLSCompliantAttribute;
                                if (cls == null) {
                                        Report.Warning (3012, 1, a.Location,
@@ -319,10 +336,6 @@ namespace Mono.CSharp
                        // FIXME: Temporary hack for repl to reset
                        static_data = null;
 
-                       // TODO: It should be done much later when the types are resolved
-                       // but that require DefineType clean-up
-                       ResolveGlobalAttributes ();
-
                        foreach (TypeContainer tc in types)
                                tc.CreateType ();
 
@@ -444,42 +457,6 @@ namespace Mono.CSharp
                        base.RemoveMemberType (ds);
                }
 
-               /// <summary>
-               /// It is called very early therefore can resolve only predefined attributes
-               /// </summary>
-               void ResolveGlobalAttributes ()
-               {
-                       if (OptAttributes == null)
-                               return;
-
-                       if (!OptAttributes.CheckTargets ())
-                               return;
-
-                       // FIXME: Define is wrong as the type may not exist yet
-                       var DefaultCharSet_attr = new PredefinedAttribute (this, "System.Runtime.InteropServices", "DefaultCharSetAttribute");
-                       DefaultCharSet_attr.Define ();
-                       Attribute a = ResolveModuleAttribute (DefaultCharSet_attr);
-                       if (a != null) {
-                               has_default_charset = true;
-                               DefaultCharSet = a.GetCharSetValue ();
-                               switch (DefaultCharSet) {
-                               case CharSet.Ansi:
-                               case CharSet.None:
-                                       break;
-                               case CharSet.Auto:
-                                       DefaultCharSetType = TypeAttributes.AutoClass;
-                                       break;
-                               case CharSet.Unicode:
-                                       DefaultCharSetType = TypeAttributes.UnicodeClass;
-                                       break;
-                               default:
-                                       Report.Error (1724, a.Location, "Value specified for the argument to `{0}' is not valid", 
-                                               DefaultCharSet_attr.GetSignatureForError ());
-                                       break;
-                               }
-                       }
-               }
-
                public Attribute ResolveAssemblyAttribute (PredefinedAttribute a_type)
                {
                        Attribute a = OptAttributes.Search ("assembly", a_type);
@@ -489,15 +466,6 @@ namespace Mono.CSharp
                        return a;
                }
 
-               Attribute ResolveModuleAttribute (PredefinedAttribute a_type)
-               {
-                       Attribute a = OptAttributes.Search ("module", a_type);
-                       if (a != null) {
-                               a.Resolve ();
-                       }
-                       return a;
-               }
-
                public void SetDeclaringAssembly (AssemblyDefinition assembly)
                {
                        // TODO: This setter is quite ugly but I have not found a way around it yet