Don't emit reaonly. prefix for reference loads
[mono.git] / mcs / mcs / roottypes.cs
index 5b95c3caf3dced1b9e52a5393b32b8653f38b15c..df7962043c9088089aa24c0c70a8e059e3384662 100644 (file)
@@ -28,7 +28,7 @@ namespace Mono.CSharp
        //
        // Module (top-level type) container
        //
-       public class ModuleContainer : TypeContainer
+       public sealed class ModuleContainer : TypeContainer
        {
 #if STATIC
                //
@@ -36,7 +36,7 @@ namespace Mono.CSharp
                //
                sealed class StaticDataContainer : CompilerGeneratedClass
                {
-                       Dictionary<int, Struct> size_types;
+                       readonly Dictionary<int, Struct> size_types;
                        new int fields;
 
                        public StaticDataContainer (ModuleContainer module)
@@ -63,28 +63,14 @@ namespace Mono.CSharp
                                        // DefineInitializedData because it creates public type,
                                        // and its name is not unique among modules
                                        //
-                                       size_type = new Struct (null, this, new MemberName ("$ArrayType=" + data.Length, Location), Modifiers.PRIVATE | Modifiers.COMPILER_GENERATED, null);
+                                       size_type = new Struct (null, this, new MemberName ("$ArrayType=" + data.Length, loc), Modifiers.PRIVATE | Modifiers.COMPILER_GENERATED, null);
                                        size_type.CreateType ();
                                        size_type.DefineType ();
 
                                        size_types.Add (data.Length, size_type);
 
-                                       var pa = Module.PredefinedAttributes.StructLayout;
-                                       if (pa.Constructor != null || pa.ResolveConstructor (Location, Compiler.BuildinTypes.Short)) {
-                                               var argsEncoded = new AttributeEncoder ();
-                                               argsEncoded.Encode ((short) LayoutKind.Explicit);
-
-                                               var field_size = pa.GetField ("Size", Compiler.BuildinTypes.Int, Location);
-                                               var pack = pa.GetField ("Pack", Compiler.BuildinTypes.Int, Location);
-                                               if (field_size != null) {
-                                                       argsEncoded.EncodeNamedArguments (
-                                                               new[] { field_size, pack },
-                                                               new[] { new IntConstant (Compiler.BuildinTypes, (int) data.Length, Location), new IntConstant (Compiler.BuildinTypes, 1, Location) }
-                                                       );
-                                               }
-
-                                               pa.EmitAttribute (size_type.TypeBuilder, argsEncoded);
-                                       }
+                                       // It has to work even if StructLayoutAttribute does not exist
+                                       size_type.TypeBuilder.__SetLayout (1, data.Length);
                                }
 
                                var name = "$field-" + fields.ToString ("X");
@@ -119,13 +105,19 @@ namespace Mono.CSharp
                public CharSet? DefaultCharSet;
                public TypeAttributes DefaultCharSetType = TypeAttributes.AnsiClass;
 
-               Dictionary<int, List<AnonymousTypeClass>> anonymous_types;
-               Dictionary<ArrayContainer.TypeRankPair, ArrayContainer> arrays;
+               readonly Dictionary<int, List<AnonymousTypeClass>> anonymous_types;
+               readonly Dictionary<ArrayContainer.TypeRankPair, ArrayContainer> array_types;
+               readonly Dictionary<TypeSpec, PointerContainer> pointer_types;
+               readonly Dictionary<TypeSpec, ReferenceContainer> reference_types;
+               readonly Dictionary<TypeSpec, MethodSpec> attrs_cache;
+
+               // Used for unique namespaces/types during parsing
+               Dictionary<MemberName, ITypesContainer> defined_type_containers;
 
                AssemblyDefinition assembly;
                readonly CompilerContext context;
                readonly RootNamespace global_ns;
-               Dictionary<string, RootNamespace> alias_ns;
+               readonly Dictionary<string, RootNamespace> alias_ns;
 
                ModuleBuilder builder;
 
@@ -133,6 +125,7 @@ namespace Mono.CSharp
 
                PredefinedAttributes predefined_attributes;
                PredefinedTypes predefined_types;
+               PredefinedMembers predefined_members;
 
                static readonly string[] attribute_targets = new string[] { "assembly", "module" };
 
@@ -147,14 +140,28 @@ namespace Mono.CSharp
                        anonymous_types = new Dictionary<int, List<AnonymousTypeClass>> ();
                        global_ns = new GlobalRootNamespace ();
                        alias_ns = new Dictionary<string, RootNamespace> ();
-                       arrays = new Dictionary<ArrayContainer.TypeRankPair, ArrayContainer> ();
+                       array_types = new Dictionary<ArrayContainer.TypeRankPair, ArrayContainer> ();
+                       pointer_types = new Dictionary<TypeSpec, PointerContainer> ();
+                       reference_types = new Dictionary<TypeSpec, ReferenceContainer> ();
+                       attrs_cache = new Dictionary<TypeSpec, MethodSpec> ();
+
+                       defined_type_containers = new Dictionary<MemberName, ITypesContainer> ();
                }
 
                #region Properties
 
-               public Dictionary<ArrayContainer.TypeRankPair, ArrayContainer> ArraysCache {
+               internal Dictionary<ArrayContainer.TypeRankPair, ArrayContainer> ArrayTypesCache {
                        get {
-                               return arrays;
+                               return array_types;
+                       }
+               }
+
+               //
+               // Cache for parameter-less attributes
+               //
+               internal Dictionary<TypeSpec, MethodSpec> AttributeConstructorCache {
+                       get {
+                               return attrs_cache;
                        }
                }
 
@@ -182,6 +189,10 @@ namespace Mono.CSharp
                        }
                }
 
+               internal DocumentationBuilder DocumentationBuilder {
+                       get; set;
+               }
+
                public Evaluator Evaluator {
                        get; set;
                }
@@ -201,6 +212,10 @@ namespace Mono.CSharp
                        }
                }
 
+               public bool HasTypesFullyDefined {
+                       get; set;
+               }
+
                //
                // Returns module global:: namespace
                //
@@ -216,18 +231,36 @@ namespace Mono.CSharp
                        }
                }
 
+               internal Dictionary<TypeSpec, PointerContainer> PointerTypesCache {
+                       get {
+                               return pointer_types;
+                       }
+               }
+
                internal PredefinedAttributes PredefinedAttributes {
                        get {
                                return predefined_attributes;
                        }
                }
 
+               internal PredefinedMembers PredefinedMembers {
+                       get {
+                               return predefined_members;
+                       }
+               }
+
                internal PredefinedTypes PredefinedTypes {
                        get {
                                return predefined_types;
                        }
                }
 
+               internal Dictionary<TypeSpec, ReferenceContainer> ReferenceTypesCache {
+                       get {
+                               return reference_types;
+                       }
+               }
+
                public override string[] ValidAttributeTargets {
                        get {
                                return attribute_targets;
@@ -253,21 +286,16 @@ namespace Mono.CSharp
                        existing.Add (type);
                }
 
-               public void AddAttributes (List<Attribute> attrs)
+               public void AddAttribute (Attribute attr, IMemberContext context)
                {
-                       AddAttributes (attrs, this);
-               }
-
-               public void AddAttributes (List<Attribute> attrs, IMemberContext context)
-               {
-                       foreach (Attribute a in attrs)
-                               a.AttachTo (this, context);
+                       attr.AttachTo (this, context);
 
                        if (attributes == null) {
-                               attributes = new Attributes (attrs);
+                               attributes = new Attributes (attr);
                                return;
                        }
-                       attributes.AddAttributes (attrs);
+
+                       attributes.AddAttribute (attr);
                }
 
                public override TypeContainer AddPartial (TypeContainer nextPart)
@@ -338,7 +366,7 @@ namespace Mono.CSharp
                public RootNamespace CreateRootNamespace (string alias)
                {
                        if (alias == global_ns.Alias) {
-                               NamespaceEntry.Error_GlobalNamespaceRedefined (Location.Null, Report);
+                               NamespaceContainer.Error_GlobalNamespaceRedefined (Location.Null, Report);
                                return global_ns;
                        }
 
@@ -359,14 +387,18 @@ namespace Mono.CSharp
 
                public new void CreateType ()
                {
+                       // Release cache used by parser only
+                       if (Evaluator == null)
+                               defined_type_containers = null;
+                       else
+                               defined_type_containers.Clear ();
+
                        foreach (TypeContainer tc in types)
                                tc.CreateType ();
                }
 
                public new void Define ()
                {
-                       InitializePredefinedTypes ();
-
                        foreach (TypeContainer tc in types)
                                tc.DefineType ();
 
@@ -380,6 +412,8 @@ namespace Mono.CSharp
                                        throw new InternalErrorException (tc, e);
                                }
                        }
+
+                       HasTypesFullyDefined = true;
                }
 
                public override void Emit ()
@@ -410,6 +444,12 @@ namespace Mono.CSharp
                                        c.EmitType ();
                }
 
+               internal override void GenerateDocComment (DocumentationBuilder builder)
+               {
+                       foreach (var tc in types)
+                               tc.GenerateDocComment (builder);
+               }
+
                public AnonymousTypeClass GetAnonymousType (IList<AnonymousTypeParameter> parameters)
                {
                        List<AnonymousTypeClass> candidates;
@@ -446,6 +486,7 @@ namespace Mono.CSharp
                {
                        predefined_attributes = new PredefinedAttributes (this);
                        predefined_types = new PredefinedTypes (this);
+                       predefined_members = new PredefinedMembers (this);
                }
 
                public override bool IsClsComplianceRequired ()
@@ -453,16 +494,58 @@ namespace Mono.CSharp
                        return DeclaringAssembly.IsCLSCompliant;
                }
 
-               protected override bool AddMemberType (TypeContainer ds)
+               protected override bool AddMemberType (TypeContainer tc)
+               {
+                       if (AddTypesContainer (tc)) {
+                               if ((tc.ModFlags & Modifiers.PARTIAL) != 0)
+                                       defined_names.Add (tc.Name, tc);
+
+                               tc.NamespaceEntry.NS.AddType (this, tc.Definition);
+                               return true;
+                       }
+
+                       return false;
+               }
+
+               public bool AddTypesContainer (ITypesContainer container)
                {
-                       if (!AddToContainer (ds, ds.Name))
-                               return false;
-                       ds.NamespaceEntry.NS.AddType (ds.Definition);
-                       return true;
+                       var mn = container.MemberName;
+                       ITypesContainer found;
+                       if (!defined_type_containers.TryGetValue (mn, out found)) {
+                               defined_type_containers.Add (mn, container);
+                               return true;
+                       }
+
+                       if (container is NamespaceContainer && found is NamespaceContainer)
+                               return true;
+
+                       var container_tc = container as TypeContainer;
+                       var found_tc = found as TypeContainer;
+                       if (container_tc != null && found_tc != null && container_tc.Kind == found_tc.Kind) {
+                               if ((found_tc.ModFlags & container_tc.ModFlags & Modifiers.PARTIAL) != 0) {
+                                       return false;
+                               }
+
+                               if (((found_tc.ModFlags | container_tc.ModFlags) & Modifiers.PARTIAL) != 0) {
+                                       Report.SymbolRelatedToPreviousError (found_tc);
+                                       Error_MissingPartialModifier (container_tc);
+                                       return false;
+                               }
+                       }
+
+                       string ns = mn.Left != null ? mn.Left.GetSignatureForError () : Module.GlobalRootNamespace.GetSignatureForError ();
+                       mn = new MemberName (mn.Name, mn.TypeArguments, mn.Location);
+
+                       Report.SymbolRelatedToPreviousError (found.Location, "");
+                       Report.Error (101, container.Location,
+                               "The namespace `{0}' already contains a definition for `{1}'",
+                               ns, mn.GetSignatureForError ());
+                       return false;
                }
 
-               protected override void RemoveMemberType (DeclSpace ds)
+               protected override void RemoveMemberType (TypeContainer ds)
                {
+                       defined_type_containers.Remove (ds.MemberName);
                        ds.NamespaceEntry.NS.RemoveDeclSpace (ds.Basename);
                        base.RemoveMemberType (ds);
                }
@@ -484,7 +567,7 @@ namespace Mono.CSharp
        }
 
        sealed class RootDeclSpace : TypeContainer {
-               public RootDeclSpace (ModuleContainer module, NamespaceEntry ns)
+               public RootDeclSpace (ModuleContainer module, NamespaceContainer ns)
                        : base (ns, null, MemberName.Null, null, 0)
                {
                        PartialContainer = module;
@@ -525,7 +608,7 @@ namespace Mono.CSharp
                        return PartialContainer.IsClsComplianceRequired ();
                }
 
-               public override IList<MethodSpec> LookupExtensionMethod (TypeSpec extensionType, string name, int arity, ref NamespaceEntry scope)
+               public override IList<MethodSpec> LookupExtensionMethod (TypeSpec extensionType, string name, int arity, ref NamespaceContainer scope)
                {
                        return null;
                }