2002-02-11 Ravi Pratap <ravi@ximian.com>
[mono.git] / mcs / mcs / rootcontext.cs
index 03ab357766915e063256bda5fa26499c268a5576..922800caab16fe0e717c86237f48df92c955d9a0 100755 (executable)
@@ -44,7 +44,7 @@ namespace Mono.CSharp {
                // This is only used to tell whether `System.Object' should
                // have a parent or not.
                //
-               static bool stdlib = true;
+               public static bool StdLib = true;
 
                //
                // This keeps track of the order in which classes were defined
@@ -57,14 +57,14 @@ namespace Mono.CSharp {
                //
                static ArrayList type_container_resolve_order;
                static ArrayList interface_resolve_order;
-               
+
                //
                // Holds a reference to the Private Implementation Details
                // class.
                //
                static TypeBuilder impl_details_class;
 
-               public static int WarningLevel = 4;
+               public static int WarningLevel = 2;
                
                //
                // Constructor
@@ -99,6 +99,16 @@ namespace Mono.CSharp {
                        }
                }
 
+               public static void RegisterOrder (Interface iface)
+               {
+                       interface_resolve_order.Add (iface);
+               }
+               
+               public static void RegisterOrder (TypeContainer tc)
+               {
+                       type_container_resolve_order.Add (tc);
+               }
+               
                // 
                // The default compiler checked state
                //
@@ -138,20 +148,14 @@ namespace Mono.CSharp {
                        if (ifaces != null){
                                interface_resolve_order = new ArrayList ();
                                
-                               foreach (Interface i in ifaces) {
-                                       Type t = i.DefineInterface (mb);
-                                       if (t != null)
-                                               interface_resolve_order.Add (i);
-                               }
+                               foreach (Interface i in ifaces) 
+                                       i.DefineInterface (mb);
                        }
                                                
                        type_container_resolve_order = new ArrayList ();
                        
-                       foreach (TypeContainer tc in root.Types) {
-                               Type t = tc.DefineType (mb);
-                               if (t != null)
-                                       type_container_resolve_order.Add (tc);
-                       }
+                       foreach (TypeContainer tc in root.Types) 
+                               tc.DefineType (mb);
 
                        if (root.Delegates != null)
                                foreach (Delegate d in root.Delegates) 
@@ -179,21 +183,38 @@ namespace Mono.CSharp {
                        
                        ArrayList ifaces = root.Interfaces;
 
-                       if (ifaces != null)
-                               foreach (Interface i in ifaces) 
-                                       i.CloseType ();
-                       
-                       foreach (TypeContainer tc in root.Types)
-                               tc.CloseType ();
+                       if (root.Enums != null)
+                               foreach (Enum en in root.Enums)
+                                       en.CloseType ();
 
+                       if (interface_resolve_order != null){
+                               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
+                       // way.  If this is really what is going on, we should probably
+                       // make sure that we define the structs in order as well.
+                       //
+                       if (type_container_resolve_order != null){
+                               foreach (TypeContainer tc in type_container_resolve_order){
+                                       if (tc is Struct && tc.Parent == tree.Types){
+                                               tc.CloseType ();
+                                       }
+                               }
+
+                               foreach (TypeContainer tc in type_container_resolve_order){
+                                       if (!(tc is Struct && tc.Parent == tree.Types))
+                                               tc.CloseType ();                                        
+                               }
+                       }
+                       
                        if (root.Delegates != null)
                                foreach (Delegate d in root.Delegates)
                                        d.CloseDelegate ();
 
-                       if (root.Enums != null)
-                               foreach (Enum en in root.Enums)
-                                       en.CloseEnum ();
-                       
 
                        //
                        // If we have a <PrivateImplementationDetails> class, close it
@@ -209,10 +230,28 @@ namespace Mono.CSharp {
                //
                // Returns: Type or null if they type can not be found.
                //
-               static public Type LookupType (DeclSpace ds, string name, bool silent)
+               static public Type LookupType (DeclSpace ds, string name, bool silent, Location loc)
                {
                        Type t;
 
+                       //
+                       // For the case the type we are looking for is nested within this one
+                       // or any base class
+                       //
+                       DeclSpace containing_ds = ds;
+                       while (containing_ds != null){
+                               Type current_type = containing_ds.TypeBuilder;
+
+                               while (current_type != null) {
+                                       t = TypeManager.LookupType (current_type.FullName + "+" + name);
+                                       if (t != null)
+                                               return t;
+                                       current_type = current_type.BaseType;
+                               }
+
+                               containing_ds = containing_ds.Parent;
+                       }
+
                        t = TypeManager.LookupType (MakeFQN (ds.Namespace.Name, name));
                        if (t != null)
                                return t;
@@ -237,13 +276,8 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       // For the case the type we are looking for is nested within this one.
-                       t = TypeManager.LookupType (ds.Name + "." + name);
-                       if (t != null)
-                               return t;
-                       
                        if (!silent)
-                               Report.Error (246, "Cannot find type `"+name+"'");
+                               Report.Error (246, loc, "Cannot find type `"+name+"'");
                        
                        return null;
                }
@@ -252,9 +286,9 @@ 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)
+               static public Type LookupType (TypeContainer tc, string name, Location loc)
                {
-                       return LookupType (tc, name, true);
+                       return LookupType (tc, name, true, loc);
                }
 
                static public bool IsNamespace (string name)
@@ -271,6 +305,11 @@ namespace Mono.CSharp {
                        return false;
                }
 
+               static void Report1530 (Location loc)
+               {
+                       Report.Error (1530, loc, "Keyword new not allowed for namespace elements");
+               }
+               
                // <summary>
                //   Populates the structs and classes with fields and methods
                // </summary>
@@ -279,48 +318,55 @@ namespace Mono.CSharp {
                // have been defined through `ResolveTree' 
                static public void PopulateTypes ()
                {
-                       if (interface_resolve_order != null)
+                       TypeContainer root = Tree.Types;
+
+                       if (interface_resolve_order != null){
                                foreach (Interface iface in interface_resolve_order)
-                                       iface.Populate ();
+                                       if ((iface.ModFlags & Modifiers.NEW) == 0)
+                                               iface.Define (root);
+                                       else
+                                               Report1530 (iface.Location);
+                       }
 
-                       if (type_container_resolve_order != null)
-                               foreach (TypeContainer tc in type_container_resolve_order)
-                                       tc.Populate ();
 
-                       TypeContainer root = Tree.Types;
+                       if (type_container_resolve_order != null){
+                               foreach (TypeContainer tc in type_container_resolve_order)
+                                       if ((tc.ModFlags & Modifiers.NEW) == 0)
+                                               tc.Define (root);
+                                       else
+                                               Report1530 (tc.Location);
+                       }
 
                        ArrayList delegates = root.Delegates;
-                       if (delegates != null)
+                       if (delegates != null){
                                foreach (Delegate d in delegates)
-                                       d.Populate (root);
+                                       if ((d.ModFlags & Modifiers.NEW) == 0)
+                                               d.Define (root);
+                                       else
+                                               Report1530 (d.Location);
+                       }
 
                        ArrayList enums = root.Enums;
-                       if (enums != null)
+                       if (enums != null){
                                foreach (Enum en in enums)
-                                       en.Populate (root);
-                       
+                                       if ((en.ModFlags & Modifiers.NEW) == 0)
+                                               en.Define (root);
+                                       else
+                                               Report1530 (en.Location);
+                       }
                }
 
                static public void EmitCode ()
                {
-                       if (type_container_resolve_order != null)
+                       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 ();
-               }
-               
-               // <summary>
-               //   Compiling against Standard Libraries property.
-               // </summary>
-               static public bool StdLib {
-                       get {
-                               return stdlib;
-                       }
-
-                       set {
-                               stdlib = value;
                        }
                }
-
+               
                static public ModuleBuilder ModuleBuilder {
                        get {
                                return mb;