2004-08-06 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / gmcs / rootcontext.cs
index e3c1e323ab8cd9fa28e7fe68ea6714c6dcf4f5f0..8239ea423f62ddf47cf6c36d71a962b28488cf28 100755 (executable)
@@ -29,11 +29,6 @@ namespace Mono.CSharp {
                //
                public static Hashtable AllDefines = new Hashtable ();
                
-               //
-               // The list of global attributes (those that target the assembly)
-               //
-               static Hashtable global_attributes = new Hashtable ();
-               
                //
                // Whether we are being linked against the standard libraries.
                // This is only used to tell whether `System.Object' should
@@ -51,7 +46,6 @@ namespace Mono.CSharp {
                // override).
                //
                static ArrayList type_container_resolve_order;
-               static ArrayList interface_resolve_order;
                static ArrayList attribute_types;
 
                //
@@ -67,17 +61,27 @@ namespace Mono.CSharp {
                public static Target Target = Target.Exe;
                public static string TargetExt = ".exe";
 
+               public static bool VerifyClsCompliance = true;
+
                //
                // If set, enable C# version 2 features
                //
-               public static bool V2;
+               public static bool V2 = true;
+
+               //
+               // We keep strongname related info here because
+               // it's also used as complier options from CSC 8.x
+               //
+               public static string StrongNameKeyFile;
+               public static string StrongNameKeyContainer;
+               public static bool StrongNameDelaySign = false;
+
                //
                // Constructor
                //
                static RootContext ()
                {
                        tree = new Tree ();
-                       interface_resolve_order = new ArrayList ();
                        type_container_resolve_order = new ArrayList ();
                }
 
@@ -95,11 +99,6 @@ namespace Mono.CSharp {
 
                static public string MainClass;
                
-               public static void RegisterOrder (Interface iface)
-               {
-                       interface_resolve_order.Add (iface);
-               }
-               
                public static void RegisterOrder (TypeContainer tc)
                {
                        type_container_resolve_order.Add (tc);
@@ -267,7 +266,7 @@ namespace Mono.CSharp {
                        object o = root.GetDefinition (name);
                        if (o == null){
                                Report.Error (518, "The predefined type `" + name + "' is not defined");
-                               Environment.Exit (0);
+                               Environment.Exit (1);
                        }
 
                        if (!(o is Delegate)){
@@ -304,7 +303,13 @@ namespace Mono.CSharp {
                                "System.Runtime.Serialization.ISerializable",
 
                                "System.Reflection.IReflect",
-                               "System.Reflection.ICustomAttributeProvider"
+                               "System.Reflection.ICustomAttributeProvider",
+
+                               //
+                               // Generic types
+                               //
+                               "System.Collections.Generic.IEnumerator`1",
+                               "System.Collections.Generic.IEnumerable`1"
                        };
 
                        foreach (string iname in interfaces_first_stage)
@@ -334,8 +339,11 @@ namespace Mono.CSharp {
                                "System.Char",    "System.Boolean",
                                "System.Decimal", "System.Void",
                                "System.RuntimeFieldHandle",
+                               "System.RuntimeArgumentHandle",
                                "System.RuntimeTypeHandle",
-                               "System.IntPtr"
+                               "System.IntPtr",
+                               "System.TypedReference",
+                               "System.ArgIterator"
                        };
                        
                        foreach (string cname in structs_second_stage)
@@ -348,6 +356,7 @@ namespace Mono.CSharp {
                                "System.Reflection.MemberInfo",
                                "System.Type",
                                "System.Exception",
+                               "System.Activator",
 
                                //
                                // These are not really important in the order, but they
@@ -361,14 +370,19 @@ namespace Mono.CSharp {
                                "System.Runtime.InteropServices.DllImportAttribute",
                                "System.Runtime.CompilerServices.MethodImplAttribute",
                                "System.Runtime.InteropServices.MarshalAsAttribute",
+                               "System.Runtime.CompilerServices.NewConstraintAttribute",
                                "System.Diagnostics.ConditionalAttribute",
                                "System.ObsoleteAttribute",
                                "System.ParamArrayAttribute",
+                               "System.CLSCompliantAttribute",
                                "System.Security.UnverifiableCodeAttribute",
                                "System.Runtime.CompilerServices.IndexerNameAttribute",
                                "System.Runtime.InteropServices.InAttribute",
-                               "System.InvalidOperationException"
-
+                               "System.Runtime.InteropServices.StructLayoutAttribute",
+                               "System.Runtime.InteropServices.FieldOffsetAttribute",
+                               "System.InvalidOperationException",
+                               "System.NotSupportedException",
+                               "System.MarshalByRefObject"
                        };
 
                        // We must store them here before calling BootstrapCorlib_ResolveDelegate.
@@ -406,9 +420,6 @@ namespace Mono.CSharp {
                                foreach (TypeContainer tc in attribute_types)
                                        tc.CloseType ();
                        
-                       foreach (Interface iface in interface_resolve_order)
-                               iface.CloseType ();
-
                        //
                        // We do this in two passes, first we close the structs,
                        // then the classes, because it seems the code needs it this
@@ -416,13 +427,13 @@ namespace Mono.CSharp {
                        // make sure that we define the structs in order as well.
                        //
                        foreach (TypeContainer tc in type_container_resolve_order){
-                               if (tc is Struct && tc.Parent == tree.Types){
+                               if (tc.Kind == Kind.Struct && tc.Parent == tree.Types){
                                        tc.CloseType ();
                                }
                        }
 
                        foreach (TypeContainer tc in type_container_resolve_order){
-                               if (!(tc is Struct && tc.Parent == tree.Types))
+                               if (!(tc.Kind == Kind.Struct && tc.Parent == tree.Types))
                                        tc.CloseType ();                                        
                        }
                        
@@ -440,10 +451,9 @@ namespace Mono.CSharp {
                        }
                        
                        attribute_types = null;
-                       interface_resolve_order = null;
                        type_container_resolve_order = null;
                        helper_classes = null;
-                       tree = null;
+                       //tree = null;
                        TypeManager.CleanUp ();
                }
 
@@ -473,25 +483,33 @@ namespace Mono.CSharp {
                        return ns.Substring (0, i);
                }
 
-               static Type NamespaceLookup (DeclSpace ds, string name, Location loc)
+               static TypeExpr NamespaceLookup (DeclSpace ds, string name,
+                                                int num_type_args, bool silent, Location loc)
                {
                        //
                        // Try in the current namespace and all its implicit parents
                        //
                        for (NamespaceEntry ns = ds.NamespaceEntry; ns != null; ns = ns.ImplicitParent) {
-                               object result = ns.Lookup (ds, name, loc);
+                               IAlias result = ns.Lookup (ds, name, num_type_args, silent, loc);
+
                                if (result == null)
                                        continue;
 
-                               if (result is Type)
-                                       return (Type) result;
+                               if (!result.IsType)
+                                       return null;
 
-                               return null;
+                               return result.Type;
                        }
 
                        return null;
                }
                
+               static public TypeExpr LookupType (DeclSpace ds, string name, bool silent,
+                                                  Location loc)
+               {
+                       return LookupType (ds, name, silent, 0, loc);
+               }
+
                //
                // Public function used to locate types, this can only
                // be used after the ResolveTree function has been invoked.
@@ -500,12 +518,13 @@ namespace Mono.CSharp {
                //
                // Come to think of it, this should be a DeclSpace
                //
-               static public Type LookupType (DeclSpace ds, string name, bool silent, Location loc)
+               static public TypeExpr LookupType (DeclSpace ds, string name, bool silent,
+                                                  int num_type_params, Location loc)
                {
-                       Type t;
+                       TypeExpr t;
 
                        if (ds.Cache.Contains (name)){
-                               t = (Type) ds.Cache [name];
+                               t = (TypeExpr) ds.Cache [name];
                                if (t != null)
                                        return t;
                        } else {
@@ -521,8 +540,9 @@ namespace Mono.CSharp {
                                                //
                                                // nested class
                                                //
-                                               t = TypeManager.LookupType (current_type.FullName + "." + name);
-                                               if (t != null){
+                                               Type type = TypeManager.LookupType (current_type.FullName + "." + name);
+                                               if (type != null){
+                                                       t = new TypeExpression (type, loc);
                                                        ds.Cache [name] = t;
                                                        return t;
                                                }
@@ -533,7 +553,7 @@ namespace Mono.CSharp {
                                        containing_ds = containing_ds.Parent;
                                }
                                
-                               t = NamespaceLookup (ds, name, loc);
+                               t = NamespaceLookup (ds, name, num_type_params, silent, loc);
                                if (t != null){
                                        ds.Cache [name] = t;
                                        return t;
@@ -550,25 +570,11 @@ namespace Mono.CSharp {
                //   This is the silent version of LookupType, you can use this
                //   to `probe' for a type
                // </summary>
-               static public Type LookupType (TypeContainer tc, string name, Location loc)
+               static public TypeExpr LookupType (TypeContainer tc, string name, Location loc)
                {
                        return LookupType (tc, name, true, loc);
                }
 
-               static public bool IsNamespace (string name)
-               {
-                       Namespace ns;
-
-                       if (tree.Namespaces != null){
-                               ns = (Namespace) tree.Namespaces [name];
-
-                               if (ns != null)
-                                       return true;
-                       }
-
-                       return false;
-               }
-
                static void Report1530 (Location loc)
                {
                        Report.Error (1530, loc, "Keyword new not allowed for namespace elements");
@@ -579,7 +585,7 @@ namespace Mono.CSharp {
                        DeclSpace ds = (DeclSpace) root.GetDefinition (name);
 
                        ds.DefineMembers (root);
-                       ds.Define (root);
+                       ds.Define ();
                }
                
                static public void BootCorlib_PopulateCoreTypes ()
@@ -604,15 +610,6 @@ namespace Mono.CSharp {
                        if (attribute_types != null)
                                foreach (TypeContainer tc in attribute_types)
                                        tc.DefineMembers (root);
-                       
-                       if (interface_resolve_order != null){
-                               foreach (Interface iface in interface_resolve_order)
-                                       if ((iface.ModFlags & Modifiers.NEW) == 0)
-                                               iface.DefineMembers (root);
-                                       else
-                                               Report1530 (iface.Location);
-                       }
-
 
                        if (type_container_resolve_order != null){
                                if (RootContext.StdLib){
@@ -649,6 +646,15 @@ namespace Mono.CSharp {
                                        else
                                                Report1530 (en.Location);
                        }
+
+                       //
+                       // Check for cycles in the struct layout
+                       //
+                       if (type_container_resolve_order != null){
+                               Hashtable seen = new Hashtable ();
+                               foreach (TypeContainer tc in type_container_resolve_order)
+                                       TypeManager.CheckStructCycles (tc, seen);
+                       }
                }
 
                //
@@ -670,14 +676,7 @@ namespace Mono.CSharp {
 
                        if (attribute_types != null)
                                foreach (TypeContainer tc in attribute_types)
-                                       tc.Define (root);
-                       
-                       if (interface_resolve_order != null){
-                               foreach (Interface iface in interface_resolve_order)
-                                       if ((iface.ModFlags & Modifiers.NEW) == 0)
-                                               iface.Define (root);
-                       }
-
+                                       tc.Define ();
 
                        if (type_container_resolve_order != null){
                                foreach (TypeContainer tc in type_container_resolve_order) {
@@ -690,7 +689,7 @@ namespace Mono.CSharp {
                                                continue;
 
                                        if ((tc.ModFlags & Modifiers.NEW) == 0)
-                                               tc.Define (root);
+                                               tc.Define ();
                                }
                        }
 
@@ -698,55 +697,43 @@ namespace Mono.CSharp {
                        if (delegates != null){
                                foreach (Delegate d in delegates)
                                        if ((d.ModFlags & Modifiers.NEW) == 0)
-                                               d.Define (root);
+                                               d.Define ();
                        }
 
                        ArrayList enums = root.Enums;
                        if (enums != null){
                                foreach (Enum en in enums)
                                        if ((en.ModFlags & Modifiers.NEW) == 0)
-                                               en.Define (root);
+                                               en.Define ();
                        }
                }
 
                static public void EmitCode ()
                {
-                       //
-                       // Because of the strange way in which we do things, global
-                       // attributes must be processed first.
-                       //
-                       if (global_attributes.Count > 0){
-                               AssemblyBuilder ab = CodeGen.AssemblyBuilder;
-                               TypeContainer dummy = new TypeContainer ();
-                               EmitContext temp_ec = new EmitContext (
-                                       dummy, Mono.CSharp.Location.Null, null, null, 0, false);
-                       
-                               foreach (DictionaryEntry de in global_attributes){
-                                       NamespaceEntry ns = (NamespaceEntry) de.Key;
-                                       Attributes attrs = (Attributes) de.Value;
-                                       
-                                       dummy.NamespaceEntry = ns;
-                                       Attribute.ApplyAttributes (temp_ec, ab, ab, attrs);
-                               }
-                       }
-                        
                        if (attribute_types != null)
                                foreach (TypeContainer tc in attribute_types)
-                                       tc.Emit ();
+                                       tc.EmitType ();
+
+                       CodeGen.Assembly.Emit (Tree.Types);
+                       CodeGen.Module.Emit (Tree.Types);
+                        
+                       if (Tree.Types.Enums != null) {
+                               foreach (Enum e in Tree.Types.Enums)
+                                       e.Emit ();
+                       }
 
-                        if (interface_resolve_order != null){
-                               foreach (Interface iface in interface_resolve_order)
-                                        iface.Emit (Tree.Types);
-                       }                        
-                       
                        if (type_container_resolve_order != null) {
                                foreach (TypeContainer tc in type_container_resolve_order)
                                        tc.EmitConstants ();
                                
                                foreach (TypeContainer tc in type_container_resolve_order)
-                                       tc.Emit ();
+                                       tc.EmitType ();
                        }
                        
+                       if (Tree.Types.Delegates != null) {
+                               foreach (Delegate d in Tree.Types.Delegates)
+                                       d.Emit ();
+                       }                       
                        //
                        // Run any hooks after all the types have been defined.
                        // This is used to create nested auxiliary classes for example
@@ -754,18 +741,6 @@ namespace Mono.CSharp {
 
                        if (EmitCodeHook != null)
                                EmitCodeHook ();
-
-                       
-                       if (Unsafe) {
-                               if (TypeManager.unverifiable_code_ctor == null) {
-                                       Console.WriteLine ("Internal error ! Cannot set unverifiable code attribute.");
-                                       return;
-                               }
-                               
-                               CustomAttributeBuilder cb = new CustomAttributeBuilder (TypeManager.unverifiable_code_ctor,
-                                                                                       new object [0]);
-                               CodeGen.ModuleBuilder.SetCustomAttribute (cb);
-                       }
                }
                
                //
@@ -803,7 +778,7 @@ namespace Mono.CSharp {
                        FieldBuilder fb;
                        
                        if (impl_details_class == null){
-                               impl_details_class = CodeGen.ModuleBuilder.DefineType (
+                               impl_details_class = CodeGen.Module.Builder.DefineType (
                                        "<PrivateImplementationDetails>",
                                         TypeAttributes.NotPublic,
                                         TypeManager.object_type);
@@ -817,21 +792,6 @@ namespace Mono.CSharp {
                        
                        return fb;
                }
-
-               //
-               // Adds a global attribute that was declared in `container', 
-               // the attribute is in `attr', and it was defined at `loc'
-               //
-               static public void AddGlobalAttributeSection (TypeContainer container, AttributeSection attr)
-               {
-                       NamespaceEntry ns = container.NamespaceEntry;
-                       Attributes a = (Attributes) global_attributes [ns];
-
-                       if (a == null)
-                               global_attributes [ns] = new Attributes (attr);
-                       else
-                               a.AddAttributeSection (attr);
-               }
        }
 }