2005-07-13 Maverson Eduardo Schulze Rosa <maverson@gmail.com>
[mono.git] / mcs / mbas / rootcontext.cs
index 89b3de5e0eeba99121e61f96c0aa8955ce90b5d2..086973b55347e4905dfae2ce858482987e3044f3 100644 (file)
@@ -14,7 +14,7 @@ using System.Reflection;
 using System.Reflection.Emit;
 using System.Diagnostics;
 
-namespace Mono.CSharp {
+namespace Mono.MonoBASIC {
 
        public class RootContext {
 
@@ -23,22 +23,16 @@ namespace Mono.CSharp {
                //
                static Tree tree;
 
-               static public bool Optimize;
-
                //
-               // If this value is `true', then calls to the Trace class are disabled
+               // Tracks Imported namespaces
                //
-               static public bool DisableTrace = true;
+               static SourceBeingCompiled sourceBeingCompiled = new SourceBeingCompiled();
 
                //
-               // If this value is `true', then calls to the Debug class are disabled
-               //
-               static public bool DisableDebug = true;
-               
-               //
-               // The list of global attributes (those that target the assembly)
+               // This hashtable contains all of the #definitions across the source code
+               // it is used by the ConditionalAttribute handler.
                //
-               static Hashtable global_attributes = new Hashtable ();
+               public static Hashtable AllDefines = new Hashtable ();
                
                //
                // Whether we are being linked against the standard libraries.
@@ -104,6 +98,21 @@ namespace Mono.CSharp {
                        attribute_types.Add (tc);
                }
                
+               public static void InitializeImports(ArrayList ImportsList)
+               {
+                       sourceBeingCompiled.InitializeImports (ImportsList);
+               }
+
+               public static SourceBeingCompiled SourceBeingCompiled
+               {
+                       get { return sourceBeingCompiled; }
+               }
+
+               public static void VerifyImports()
+               {
+                       sourceBeingCompiled.VerifyImports();
+               }
+
                // 
                // The default compiler checked state
                //
@@ -114,6 +123,9 @@ namespace Mono.CSharp {
                //
                static public bool Unsafe = false;
                
+               // Root namespace name (implicit namespace)
+               static public string RootNamespace = "";
+               
                static string MakeFQN (string nsn, string name)
                {
                        string prefix = (nsn == "" ? "" : nsn + ".");
@@ -338,12 +350,9 @@ namespace Mono.CSharp {
                        // These are classes that depends on the core interfaces
                        //
                        string [] classes_second_stage = {
-                               "System.String", "System.Enum",
-                               "System.Array",  "System.MulticastDelegate",
-                               "System.Delegate",
-
                                "System.Reflection.MemberInfo",
                                "System.Type",
+                               "System.Exception",
 
                                //
                                // These are not really important in the order, but they
@@ -357,10 +366,19 @@ namespace Mono.CSharp {
                                "System.Runtime.InteropServices.DllImportAttribute",
                                "System.Runtime.CompilerServices.MethodImplAttribute",
                                "System.Runtime.InteropServices.MarshalAsAttribute",
+                               "System.Diagnostics.ConditionalAttribute",
+                               "System.ObsoleteAttribute",
                                "System.ParamArrayAttribute",
                                "System.Security.UnverifiableCodeAttribute",
                                "System.Runtime.CompilerServices.IndexerNameAttribute",
                        };
+
+                       // We must store them here before calling BootstrapCorlib_ResolveDelegate.
+                       TypeManager.string_type = BootstrapCorlib_ResolveClass (root, "System.String");
+                       TypeManager.enum_type = BootstrapCorlib_ResolveClass (root, "System.Enum");
+                       TypeManager.array_type = BootstrapCorlib_ResolveClass (root, "System.Array");
+                       TypeManager.multicast_delegate_type = BootstrapCorlib_ResolveClass (root, "System.MulticastDelegate");
+                       TypeManager.delegate_type = BootstrapCorlib_ResolveClass (root, "System.Delegate");
                        
                        foreach (string cname in classes_second_stage)
                                BootstrapCorlib_ResolveClass (root, cname);
@@ -382,7 +400,7 @@ namespace Mono.CSharp {
                {
                        TypeContainer root = Tree.Types;
                        
-                       ArrayList ifaces = root.Interfaces;
+                       //ArrayList ifaces = root.Interfaces;
 
                        if (root.Enums != null)
                                foreach (Enum en in root.Enums)
@@ -414,7 +432,7 @@ namespace Mono.CSharp {
                        
                        if (root.Delegates != null)
                                foreach (Delegate d in root.Delegates)
-                                       d.CloseDelegate ();
+                                       d.CloseType ();
 
 
                        //
@@ -440,7 +458,7 @@ namespace Mono.CSharp {
                        return ns.Substring (0, i);
                }
 
-               static Type NamespaceLookup (Namespace curr_ns, string name)
+               static Type NamespaceLookup (Namespace curr_ns, string name, Location loc)
                {
                        Type t;
                        
@@ -464,7 +482,7 @@ namespace Mono.CSharp {
                        //
                        // Try the aliases in the current namespace
                        //
-                       string alias = curr_ns.LookupAlias (name);
+                       string alias = sourceBeingCompiled.LookupAlias (name);
 
                        if (alias != null) {
                                t = TypeManager.LookupType (alias);
@@ -487,21 +505,33 @@ namespace Mono.CSharp {
                                //
                                // Then try with the using clauses
                                //
-                               ArrayList using_list = ns.UsingTable;
 
-                               if (using_list == null)
+                               ICollection imports_list = sourceBeingCompiled.ImportsTable;
+
+                               if (imports_list == null)
                                        continue;
 
-                               foreach (string n in using_list) {
-                                       t = TypeManager.LookupType (MakeFQN (n, name));
-                                       if (t != null)
-                                               return t;
+                               Type match = null;
+                               foreach (SourceBeingCompiled.ImportsEntry ue in imports_list) {
+                               //TODO: deal with standard modules
+                                       match = TypeManager.LookupType (MakeFQN (ue.Name, name));
+                                       if (match != null){
+                                               if (t != null){
+                                                       DeclSpace.Error_AmbiguousTypeReference (loc, name, t, match);
+                                                       return null;
+                                               }
+                                               
+                                               t = match;
+                                               ue.Used = true;
+                                       }
                                }
+                               if (t != null)
+                                       return t;
 
                                //
                                // Try with aliases
                                //
-                               string a = ns.LookupAlias (name);
+                               string a = sourceBeingCompiled.LookupAlias (name);
                                if (a != null) {
                                        t = TypeManager.LookupType (a);
                                        if (t != null)
@@ -545,7 +575,7 @@ namespace Mono.CSharp {
                                                //
                                                // nested class
                                                //
-                                               t = TypeManager.LookupType (current_type.FullName + "+" + name);
+                                               t = TypeManager.LookupType (current_type.FullName + "." + name);
                                                if (t != null){
                                                        ds.Cache [name] = t;
                                                        return t;
@@ -557,7 +587,7 @@ namespace Mono.CSharp {
                                        containing_ds = containing_ds.Parent;
                                }
                                
-                               t = NamespaceLookup (ds.Namespace, name);
+                               t = NamespaceLookup (ds.Namespace, name, loc);
                                if (t != null){
                                        ds.Cache [name] = t;
                                        return t;
@@ -565,7 +595,7 @@ namespace Mono.CSharp {
                        }
 
                        if (!silent)
-                               Report.Error (246, loc, "Cannot find type `"+name+"'");
+                               Report.Error (30002, loc, "Cannot find type `"+name+"'");
                        
                        return null;
                }
@@ -602,6 +632,7 @@ namespace Mono.CSharp {
                {
                        DeclSpace ds = (DeclSpace) root.GetDefinition (name);
 
+                       ds.DefineMembers (root);
                        ds.Define (root);
                }
                
@@ -626,30 +657,39 @@ namespace Mono.CSharp {
 
                        if (attribute_types != null)
                                foreach (TypeContainer tc in attribute_types)
-                                       tc.Define (root);
+                                       tc.DefineMembers (root);
                        
                        if (interface_resolve_order != null){
                                foreach (Interface iface in interface_resolve_order)
                                        if ((iface.ModFlags & Modifiers.NEW) == 0)
-                                               iface.Define (root);
+                                               iface.DefineMembers (root);
                                        else
                                                Report1530 (iface.Location);
                        }
 
 
                        if (type_container_resolve_order != null){
-                               foreach (TypeContainer tc in type_container_resolve_order)
+                               foreach (TypeContainer tc in type_container_resolve_order) {
+                                       // When compiling corlib, these types have already been
+                                       // populated from BootCorlib_PopulateCoreTypes ().
+                                       if (!RootContext.StdLib &&
+                                           ((tc.Name == "System.Object") ||
+                                            (tc.Name == "System.Attribute") ||
+                                            (tc.Name == "System.ValueType")))
+                                               continue;
+
                                        if ((tc.ModFlags & Modifiers.NEW) == 0)
-                                               tc.Define (root);
+                                               tc.DefineMembers (root);
                                        else
                                                Report1530 (tc.Location);
+                               }
                        }
 
                        ArrayList delegates = root.Delegates;
                        if (delegates != null){
                                foreach (Delegate d in delegates)
                                        if ((d.ModFlags & Modifiers.NEW) == 0)
-                                               d.Define (root);
+                                               d.DefineMembers (root);
                                        else
                                                Report1530 (d.Location);
                        }
@@ -658,36 +698,64 @@ namespace Mono.CSharp {
                        if (enums != null){
                                foreach (Enum en in enums)
                                        if ((en.ModFlags & Modifiers.NEW) == 0)
-                                               en.Define (root);
+                                               en.DefineMembers (root);
                                        else
                                                Report1530 (en.Location);
                        }
                }
 
-               static public void EmitCode ()
+               static public void DefineTypes ()
                {
-                       //
-                       // 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 (null, "", new Location (-1));
-                               EmitContext temp_ec = new EmitContext (
-                                       dummy, Mono.CSharp.Location.Null, null, null, 0, false);
+                       TypeContainer root = Tree.Types;
+
+                       if (attribute_types != null)
+                               foreach (TypeContainer tc in attribute_types)
+                                       tc.Define (root);
                        
-                               foreach (DictionaryEntry de in global_attributes){
-                                       Namespace ns = (Namespace) de.Key;
-                                       Attributes attrs = (Attributes) de.Value;
-                                       
-                                       dummy.Namespace = ns;
-                                       Attribute.ApplyAttributes (temp_ec, ab, ab, attrs, attrs.Location);
+                       if (interface_resolve_order != null){
+                               foreach (Interface iface in interface_resolve_order)
+                                       if ((iface.ModFlags & Modifiers.NEW) == 0)
+                                               iface.Define (root);
+                       }
+
+
+                       if (type_container_resolve_order != null){
+                               foreach (TypeContainer tc in type_container_resolve_order) {
+                                       // When compiling corlib, these types have already been
+                                       // populated from BootCorlib_PopulateCoreTypes ().
+                                       if (!RootContext.StdLib &&
+                                           ((tc.Name == "System.Object") ||
+                                            (tc.Name == "System.Attribute") ||
+                                            (tc.Name == "System.ValueType")))
+                                               continue;
+
+                                       if ((tc.ModFlags & Modifiers.NEW) == 0)
+                                               tc.Define (root);
                                }
                        }
-                       
+
+                       ArrayList delegates = root.Delegates;
+                       if (delegates != null){
+                               foreach (Delegate d in delegates)
+                                       if ((d.ModFlags & Modifiers.NEW) == 0)
+                                               d.Define (root);
+                       }
+
+                       ArrayList enums = root.Enums;
+                       if (enums != null){
+                               foreach (Enum en in enums)
+                                       if ((en.ModFlags & Modifiers.NEW) == 0)
+                                               en.Define (root);
+                       }
+               }
+
+               static public void EmitCode ()
+               {
                        if (attribute_types != null)
                                foreach (TypeContainer tc in attribute_types)
                                        tc.Emit ();
+
+                       CodeGen.EmitGlobalAttributes ();
                        
                        if (type_container_resolve_order != null) {
                                foreach (TypeContainer tc in type_container_resolve_order)
@@ -698,14 +766,12 @@ namespace Mono.CSharp {
                        }
                        
                        if (Unsafe) {
-                               ConstructorInfo ci = TypeManager.unverifiable_code_type.GetConstructor (new Type [0]);
-                                       
-                               if (ci == null) {
-                                       Console.WriteLine ("Internal error !");
+                               if (TypeManager.unverifiable_code_ctor == null) {
+                                       Console.WriteLine ("Internal error ! Cannot set unverifiable code attribute.");
                                        return;
                                }
                                
-                               CustomAttributeBuilder cb = new CustomAttributeBuilder (ci, new object [0]);
+                               CustomAttributeBuilder cb = new CustomAttributeBuilder (TypeManager.unverifiable_code_ctor, new object [0]);
                                CodeGen.ModuleBuilder.SetCustomAttribute (cb);
                        }
                }
@@ -743,11 +809,11 @@ namespace Mono.CSharp {
                static public FieldBuilder MakeStaticData (byte [] data)
                {
                        FieldBuilder fb;
-                       int size = data.Length;
+                       //int size = data.Length;
                        
                        if (impl_details_class == null)
                                impl_details_class = CodeGen.ModuleBuilder.DefineType (
-                                       "<PrivateImplementationDetails>", TypeAttributes.NotPublic);
+                                       "<PrivateImplementationDetails>", TypeAttributes.NotPublic, TypeManager.object_type);
 
                        fb = impl_details_class.DefineInitializedData (
                                "$$field-" + (field_count++), data,
@@ -756,22 +822,5 @@ 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 AddGlobalAttribute (TypeContainer container,
-                                                      AttributeSection attr, Location loc)
-               {
-                       Namespace ns = container.Namespace;
-                       Attributes a = (Attributes) global_attributes [ns];
-
-                       if (a == null)
-                               global_attributes [ns] = new Attributes (attr, loc);
-                       else
-                               a.AddAttribute (attr);
-               }
        }
 }
-             
-