2006-05-01 Rafael Teixeira <rafaelteixeirabr@hotmail.com>
[mono.git] / mcs / gmcs / rootcontext.cs
index 2c08c454e11e4e22cf783fecf18959343f245d7f..f5ad0c98a628e98b442f466f84a483d464fe13c3 100644 (file)
@@ -42,7 +42,7 @@ namespace Mono.CSharp {
                // This is only used to tell whether `System.Object' should
                // have a base class or not.
                //
-               public static bool StdLib = true;
+               public static bool StdLib;
 
                //
                // This keeps track of the order in which classes were defined
@@ -54,7 +54,6 @@ namespace Mono.CSharp {
                // override).
                //
                static ArrayList type_container_resolve_order;
-               static ArrayList attribute_types;
 
                //
                // Holds a reference to the Private Implementation Details
@@ -64,14 +63,19 @@ namespace Mono.CSharp {
                
                static TypeBuilder impl_details_class;
 
-               public static int WarningLevel = 3;
+               public static int WarningLevel;
 
-               public static Target Target = Target.Exe;
-               public static string TargetExt = ".exe";
+               public static Target Target;
+               public static string TargetExt;
 
                public static bool VerifyClsCompliance = true;
 
-               public static LanguageVersion Version = LanguageVersion.Default;
+               /// <summary>
+               /// Holds /optimize option
+               /// </summary>
+               public static bool Optimize = true;
+
+               public static LanguageVersion Version;
 
                //
                // We keep strongname related info here because
@@ -79,20 +83,42 @@ namespace Mono.CSharp {
                //
                public static string StrongNameKeyFile;
                public static string StrongNameKeyContainer;
-               public static bool StrongNameDelaySign = false;
+               public static bool StrongNameDelaySign;
 
+               public static bool BrokenCircularDeps;
                //
                // If set, enable XML documentation generation
                //
                public static Documentation Documentation;
 
+               static public string MainClass;
+
                //
                // Constructor
                //
                static RootContext ()
+               {
+                       Reset ();
+               }
+
+               public static void Reset ()
                {
                        tree = new Tree ();
                        type_container_resolve_order = new ArrayList ();
+                       EntryPoint = null;
+                       WarningLevel = 3;
+                       Checked = false;
+                       Unsafe = false;
+                       StdLib = true;
+                       StrongNameKeyFile = null;
+                       StrongNameKeyContainer = null;
+                       StrongNameDelaySign = false;
+                       MainClass = null;
+                       Target = Target.Exe;
+                       TargetExt = ".exe";
+                       Version = LanguageVersion.Default;
+                       Documentation = null;
+                       impl_details_class = null;
                }
 
                public static bool NeedsEntryPoint {
@@ -107,38 +133,21 @@ namespace Mono.CSharp {
                        }
                }
 
-               static public string MainClass;
-               
                public static void RegisterOrder (TypeContainer tc)
                {
                        type_container_resolve_order.Add (tc);
                }
-
-               public static void RegisterAttribute (TypeContainer tc)
-               {
-                       if (attribute_types == null)
-                               attribute_types = new ArrayList ();
-                       
-                       attribute_types.Add (tc);
-               }
                
                // 
                // The default compiler checked state
                //
-               static public bool Checked = false;
+               static public bool Checked;
 
                //
                // Whether to allow Unsafe code
                //
-               static public bool Unsafe = false;
+               static public bool Unsafe;
                
-               static string MakeFQN (string nsn, string name)
-               {
-                       if (nsn == "")
-                               return name;
-                       return String.Concat (nsn, ".", name);
-               }
-
                // <remarks>
                //   This function is used to resolve the hierarchy tree.
                //   It processes interfaces, structs and classes in that order.
@@ -148,13 +157,6 @@ namespace Mono.CSharp {
                // </remarks>
                static public void ResolveTree ()
                {
-                       //
-                       // Process the attribute types separately and before anything else
-                       //
-                       if (attribute_types != null)
-                               foreach (TypeContainer tc in attribute_types)
-                                       tc.DefineType ();
-                       
                        //
                        // Interfaces are processed next, as classes and
                        // structs might inherit from an object or implement
@@ -165,7 +167,7 @@ namespace Mono.CSharp {
 
                        ArrayList ifaces = root.Interfaces;
                        if (ifaces != null){
-                               foreach (Interface i in ifaces) 
+                               foreach (TypeContainer i in ifaces) 
                                        i.DefineType ();
                        }
 
@@ -200,7 +202,7 @@ namespace Mono.CSharp {
                {
                        object o = root.GetDefinition (name);
                        if (o == null){
-                               Report.Error (518, "The predefined type `" + name + "' is not defined");
+                               Report.Error (518, "The predefined type `" + name + "' is not defined or imported");
                                return null;
                        }
 
@@ -225,7 +227,7 @@ namespace Mono.CSharp {
                {
                        object o = root.GetDefinition (name);
                        if (o == null){
-                               Report.Error (518, "The predefined type `" + name + "' is not defined");
+                               Report.Error (518, "The predefined type `" + name + "' is not defined or imported");
                                return;
                        }
 
@@ -250,7 +252,7 @@ namespace Mono.CSharp {
                {
                        object o = root.GetDefinition (name);
                        if (o == null){
-                               Report.Error (518, "The predefined type `" + name + "' is not defined");
+                               Report.Error (518, "The predefined type `" + name + "' is not defined or imported");
                                return;
                        }
 
@@ -275,8 +277,8 @@ namespace Mono.CSharp {
                {
                        object o = root.GetDefinition (name);
                        if (o == null){
-                               Report.Error (518, "The predefined type `" + name + "' is not defined");
-                               Environment.Exit (1);
+                               Report.Error (518, "The predefined type `" + name + "' is not defined or imported");
+                               return;
                        }
 
                        if (!(o is Delegate)){
@@ -296,8 +298,11 @@ namespace Mono.CSharp {
                        TypeContainer root = Tree.Types;
 
                        TypeManager.object_type = BootstrapCorlib_ResolveClass (root, "System.Object");
+                       TypeManager.system_object_expr.Type = TypeManager.object_type;
                        TypeManager.value_type = BootstrapCorlib_ResolveClass (root, "System.ValueType");
+                       TypeManager.system_valuetype_expr.Type = TypeManager.value_type;
                        TypeManager.attribute_type = BootstrapCorlib_ResolveClass (root, "System.Attribute");
+                       TypeManager.indexer_name_type = BootstrapCorlib_ResolveClass (root, "System.Runtime.CompilerServices.IndexerNameAttribute");
                        
                        string [] interfaces_first_stage = {
                                "System.IComparable", "System.ICloneable",
@@ -320,7 +325,7 @@ namespace Mono.CSharp {
                                // Generic types
                                //
                                "System.Collections.Generic.IEnumerator`1",
-                               "System.Collections.Generic.IEnumerable`1",
+                               "System.Collections.Generic.IEnumerable`1"
                        };
 
                        foreach (string iname in interfaces_first_stage)
@@ -384,7 +389,6 @@ namespace Mono.CSharp {
                                "System.Runtime.InteropServices.DllImportAttribute",
                                "System.Runtime.CompilerServices.MethodImplAttribute",
                                "System.Runtime.InteropServices.MarshalAsAttribute",
-                               "System.Runtime.CompilerServices.NewConstraintAttribute",
                                "System.Runtime.CompilerServices.CompilerGeneratedAttribute",
                                "System.Runtime.CompilerServices.FixedBufferAttribute",
                                "System.Diagnostics.ConditionalAttribute",
@@ -393,16 +397,19 @@ namespace Mono.CSharp {
                                "System.CLSCompliantAttribute",
                                "System.Security.UnverifiableCodeAttribute",
                                "System.Security.Permissions.SecurityAttribute",
-                               "System.Runtime.CompilerServices.IndexerNameAttribute",
                                "System.Runtime.CompilerServices.DecimalConstantAttribute",
                                "System.Runtime.InteropServices.InAttribute",
                                "System.Runtime.InteropServices.OutAttribute",
                                "System.Runtime.InteropServices.StructLayoutAttribute",
                                "System.Runtime.InteropServices.FieldOffsetAttribute",
+                               "System.Runtime.InteropServices.DefaultCharSetAttribute",
                                "System.InvalidOperationException",
                                "System.NotSupportedException",
                                "System.MarshalByRefObject",
-                               "System.Security.CodeAccessPermission"
+                               "System.Security.CodeAccessPermission",
+                               "System.Runtime.CompilerServices.RequiredAttributeAttribute",
+                               "System.Runtime.InteropServices.GuidAttribute",
+                               "System.Reflection.AssemblyCultureAttribute"
                        };
 
                        foreach (string cname in classes_second_stage)
@@ -437,10 +444,6 @@ namespace Mono.CSharp {
                                foreach (Enum en in root.Enums)
                                        en.CloseType ();
 
-                       if (attribute_types != null)
-                               foreach (TypeContainer tc in attribute_types)
-                                       tc.CloseType ();
-                       
                        //
                        // We do this in two passes, first we close the structs,
                        // then the classes, because it seems the code needs it this
@@ -473,7 +476,6 @@ namespace Mono.CSharp {
                                }
                        }
                        
-                       attribute_types = null;
                        type_container_resolve_order = null;
                        helper_classes = null;
                        //tree = null;
@@ -492,16 +494,11 @@ namespace Mono.CSharp {
                        helper_classes.Add (helper_class);
                }
                
-               static void Report1530 (Location loc)
-               {
-                       Report.Error (1530, loc, "Keyword new not allowed for namespace elements");
-               }
-               
                static public void PopulateCoreType (TypeContainer root, string name)
                {
                        DeclSpace ds = (DeclSpace) root.GetDefinition (name);
 
-                       ds.DefineMembers (root);
+                       ds.DefineMembers ();
                        ds.Define ();
                }
                
@@ -512,6 +509,7 @@ namespace Mono.CSharp {
                        PopulateCoreType (root, "System.Object");
                        PopulateCoreType (root, "System.ValueType");
                        PopulateCoreType (root, "System.Attribute");
+                       PopulateCoreType (root, "System.Runtime.CompilerServices.IndexerNameAttribute");
                }
                
                // <summary>
@@ -524,44 +522,23 @@ namespace Mono.CSharp {
                {
                        TypeContainer root = Tree.Types;
 
-                       if (attribute_types != null)
-                               foreach (TypeContainer tc in attribute_types)
-                                       tc.DefineMembers (root);
-
                        if (type_container_resolve_order != null){
-                               if (RootContext.StdLib){
-                                       foreach (TypeContainer tc in type_container_resolve_order)
-                                               tc.DefineMembers (root);
-                               } else {
-                                       foreach (TypeContainer tc in type_container_resolve_order) {
-                                               // When compiling corlib, these types have already been
-                                               // populated from BootCorlib_PopulateCoreTypes ().
-                                               if (((tc.Name == "System.Object") ||
-                                                    (tc.Name == "System.Attribute") ||
-                                                    (tc.Name == "System.ValueType")))
-                                               continue;
-
-                                               tc.DefineMembers (root);
-                                       }
-                               } 
+                               foreach (TypeContainer tc in type_container_resolve_order)
+                                       tc.ResolveType ();
+                               foreach (TypeContainer tc in type_container_resolve_order)
+                                       tc.DefineMembers ();
                        }
 
                        ArrayList delegates = root.Delegates;
                        if (delegates != null){
                                foreach (Delegate d in delegates)
-                                       if ((d.ModFlags & Modifiers.NEW) == 0)
-                                               d.DefineMembers (root);
-                                       else
-                                               Report1530 (d.Location);
+                                       d.DefineMembers ();
                        }
 
                        ArrayList enums = root.Enums;
                        if (enums != null){
                                foreach (Enum en in enums)
-                                       if ((en.ModFlags & Modifiers.NEW) == 0)
-                                               en.DefineMembers (root);
-                                       else
-                                               Report1530 (en.Location);
+                                       en.DefineMembers ();
                        }
 
                        //
@@ -591,9 +568,11 @@ namespace Mono.CSharp {
                {
                        TypeContainer root = Tree.Types;
 
-                       if (attribute_types != null)
-                               foreach (TypeContainer tc in attribute_types)
-                                       tc.Define ();
+                       ArrayList delegates = root.Delegates;
+                       if (delegates != null){
+                               foreach (Delegate d in delegates)
+                                       d.Define ();
+                       }
 
                        if (type_container_resolve_order != null){
                                foreach (TypeContainer tc in type_container_resolve_order) {
@@ -602,38 +581,23 @@ namespace Mono.CSharp {
                                        if (!RootContext.StdLib &&
                                            ((tc.Name == "System.Object") ||
                                             (tc.Name == "System.Attribute") ||
-                                            (tc.Name == "System.ValueType")))
+                                            (tc.Name == "System.ValueType") ||
+                                            (tc.Name == "System.Runtime.CompilerServices.IndexerNameAttribute")))
                                                continue;
 
-                                       if ((tc.ModFlags & Modifiers.NEW) == 0)
-                                               tc.Define ();
+                                       tc.Define ();
                                }
                        }
 
-                       ArrayList delegates = root.Delegates;
-                       if (delegates != null){
-                               foreach (Delegate d in delegates)
-                                       if ((d.ModFlags & Modifiers.NEW) == 0)
-                                               d.Define ();
-                       }
-
                        ArrayList enums = root.Enums;
                        if (enums != null){
                                foreach (Enum en in enums)
-                                       if ((en.ModFlags & Modifiers.NEW) == 0)
-                                               en.Define ();
+                                       en.Define ();
                        }
                }
 
                static public void EmitCode ()
                {
-                       if (attribute_types != null)
-                               foreach (TypeContainer tc in attribute_types)
-                                       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 ();
@@ -642,6 +606,12 @@ namespace Mono.CSharp {
                        if (type_container_resolve_order != null) {
                                foreach (TypeContainer tc in type_container_resolve_order)
                                        tc.EmitType ();
+
+                               if (Report.Errors > 0)
+                                       return;
+
+                               foreach (TypeContainer tc in type_container_resolve_order)
+                                       tc.VerifyMembers ();
                        }
                        
                        if (Tree.Types.Delegates != null) {
@@ -655,6 +625,9 @@ namespace Mono.CSharp {
 
                        if (EmitCodeHook != null)
                                EmitCodeHook ();
+
+                       CodeGen.Assembly.Emit (Tree.Types);
+                       CodeGen.Module.Emit (Tree.Types);
                }
                
                //
@@ -706,6 +679,14 @@ namespace Mono.CSharp {
                        
                        return fb;
                }
+
+               public static void CheckUnsafeOption (Location loc)
+               {
+                       if (!Unsafe) {
+                               Report.Error (227, loc, 
+                                       "Unsafe code requires the `unsafe' command line option to be specified");
+                       }
+               }
        }
 }