2010-02-03 Miguel de Icaza <miguel@novell.com>
[mono.git] / mcs / mcs / rootcontext.cs
index 22c784d62b7ab3baa085304bc20a1439d7a1e03b..90ebd1aa5a3bbc817c841b90e54a28e5c571d577 100644 (file)
@@ -12,7 +12,7 @@
 // Copyright 2004-2008 Novell, Inc
 
 using System;
-using System.Collections;
+using System.Collections.Generic;
 using System.Reflection;
 using System.Reflection.Emit;
 using System.Diagnostics;
@@ -22,15 +22,19 @@ namespace Mono.CSharp {
        public enum LanguageVersion
        {
                ISO_1           = 1,
-               Default_MCS     = 2,
-               ISO_2           = 3,
-               LINQ            = 4,
+               ISO_2           = 2,
+               V_3             = 3,
+               V_4             = 4,
+               Future          = 100,
 
-#if GMCS_SOURCE
-               Default         = LINQ
-#else
-               Default         = Default_MCS
-#endif
+               Default         = LanguageVersion.V_4,
+       }
+
+       public enum MetadataVersion
+       {
+               v1,
+               v2,
+               v4
        }
 
        public class RootContext {
@@ -39,10 +43,14 @@ namespace Mono.CSharp {
                // COMPILER OPTIONS CLASS
                //
                public static Target Target;
+               public static Platform Platform;
                public static string TargetExt;
                public static bool VerifyClsCompliance = true;
                public static bool Optimize = true;
                public static LanguageVersion Version;
+               public static bool EnhancedWarnings;
+
+               public static MetadataVersion MetadataCompatibilityVersion;
 
                //
                // We keep strongname related info here because
@@ -101,13 +109,13 @@ namespace Mono.CSharp {
                //
                // Contains the parsed tree
                //
-               static RootTypes root;
+               static ModuleCompiled root;
 
                //
                // This hashtable contains all of the #definitions across the source code
                // it is used by the ConditionalAttribute handler.
                //
-               static ArrayList AllDefines = new ArrayList ();
+               static List<string> AllDefines;
                
                //
                // This keeps track of the order in which classes were defined
@@ -118,13 +126,13 @@ namespace Mono.CSharp {
                // or abstract as well as the parent names (to implement new, 
                // override).
                //
-               static ArrayList type_container_resolve_order;
+               static List<TypeContainer> type_container_resolve_order;
 
                //
                // Holds a reference to the Private Implementation Details
                // class.
                //
-               static ArrayList helper_classes;
+               static List<TypeBuilder> helper_classes;
                
                static TypeBuilder impl_details_class;
 
@@ -144,11 +152,10 @@ namespace Mono.CSharp {
                public static void Reset (bool full)
                {
                        if (full)
-                               root = new RootTypes ();
+                               root = null;
                        
-                       type_container_resolve_order = new ArrayList ();
+                       type_container_resolve_order = new List<TypeContainer> ();
                        EntryPoint = null;
-                       Report.WarningLevel = 4;
                        Checked = false;
                        Unsafe = false;
                        StdLib = true;
@@ -158,16 +165,23 @@ namespace Mono.CSharp {
                        MainClass = null;
                        Target = Target.Exe;
                        TargetExt = ".exe";
+                       Platform = Platform.AnyCPU;
                        Version = LanguageVersion.Default;
                        Documentation = null;
                        impl_details_class = null;
                        helper_classes = null;
 
+#if NET_4_0
+                       MetadataCompatibilityVersion = MetadataVersion.v4;
+#else
+                       MetadataCompatibilityVersion = MetadataVersion.v2;
+#endif
+
                        //
                        // Setup default defines
                        //
-                       RootContext.AllDefines = new ArrayList ();
-                       RootContext.AddConditional ("__MonoCS__");
+                       AllDefines = new List<string> ();
+                       AddConditional ("__MonoCS__");
                }
 
                public static void AddConditional (string p)
@@ -182,8 +196,9 @@ namespace Mono.CSharp {
                        return AllDefines.Contains (value);
                }
 
-               static public RootTypes ToplevelTypes {
+               static public ModuleCompiled ToplevelTypes {
                        get { return root; }
+                       set { root = value; }
                }
 
                public static void RegisterOrder (TypeContainer tc)
@@ -200,6 +215,8 @@ namespace Mono.CSharp {
                // </remarks>
                static public void ResolveTree ()
                {
+                       root.Resolve ();
+
                        //
                        // Interfaces are processed next, as classes and
                        // structs might inherit from an object or implement
@@ -236,13 +253,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.Kind == Kind.Struct && tc.Parent == root){
+                               if (tc.Kind == MemberKind.Struct && tc.Parent == root){
                                        tc.CloseType ();
                                }
                        }
 
                        foreach (TypeContainer tc in type_container_resolve_order){
-                               if (!(tc.Kind == Kind.Struct && tc.Parent == root))
+                               if (!(tc.Kind == MemberKind.Struct && tc.Parent == root))
                                        tc.CloseType ();                                        
                        }
                        
@@ -256,9 +273,7 @@ namespace Mono.CSharp {
                        //
                        if (helper_classes != null){
                                foreach (TypeBuilder type_builder in helper_classes) {
-#if GMCS_SOURCE
-                                       type_builder.SetCustomAttribute (TypeManager.GetCompilerGeneratedAttribute (Location.Null));
-#endif
+                                       PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (type_builder);
                                        type_builder.CreateType ();
                                }
                        }
@@ -276,25 +291,28 @@ namespace Mono.CSharp {
                public static void RegisterCompilerGeneratedType (TypeBuilder helper_class)
                {
                        if (helper_classes == null)
-                               helper_classes = new ArrayList ();
+                               helper_classes = new List<TypeBuilder> ();
 
                        helper_classes.Add (helper_class);
                }
                
-               static public void PopulateCoreType (TypeContainer root, string name)
+               static public DeclSpace PopulateCoreType (TypeContainer root, string name)
                {
                        DeclSpace ds = (DeclSpace) root.GetDefinition (name);
                        // Core type was imported
                        if (ds == null)
-                               return;
+                               return null;
 
-                       ds.DefineMembers ();
                        ds.Define ();
+                       return ds;
                }
                
                static public void BootCorlib_PopulateCoreTypes ()
                {
-                       PopulateCoreType (root, "System.Object");
+                       // Clear -nostdlib flag when object type is imported
+                       if (PopulateCoreType (root, "System.Object") == null)
+                               RootContext.StdLib = true;
+
                        PopulateCoreType (root, "System.ValueType");
                        PopulateCoreType (root, "System.Attribute");
                        PopulateCoreType (root, "System.Runtime.CompilerServices.IndexerNameAttribute");
@@ -312,60 +330,38 @@ namespace Mono.CSharp {
                        if (type_container_resolve_order != null){
                                foreach (TypeContainer tc in type_container_resolve_order)
                                        tc.ResolveType ();
-                               foreach (TypeContainer tc in type_container_resolve_order)
-                                       tc.DefineMembers ();
+                               foreach (TypeContainer tc in type_container_resolve_order) {
+                                       try {
+                                               tc.Define ();
+                                       } catch (Exception e) {
+                                               throw new InternalErrorException (tc, e);
+                                       }
+                               }
                        }
 
-                       ArrayList delegates = root.Delegates;
+                       var delegates = root.Delegates;
                        if (delegates != null){
                                foreach (Delegate d in delegates)
-                                       d.DefineMembers ();
+                                       d.Define ();
                        }
 
                        //
                        // Check for cycles in the struct layout
                        //
                        if (type_container_resolve_order != null){
-                               Hashtable seen = new Hashtable ();
+                               var seen = new Dictionary<TypeContainer, object> ();
                                foreach (TypeContainer tc in type_container_resolve_order)
                                        TypeManager.CheckStructCycles (tc, seen);
                        }
                }
 
-               //
-               // DefineTypes is used to fill in the members of each type.
-               //
-               static public void DefineTypes ()
-               {
-                       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) {
-                                       // 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") ||
-                                            (tc.Name == "System.Runtime.CompilerServices.IndexerNameAttribute")))
-                                               continue;
-
-                                       tc.Define ();
-                               }
-                       }
-               }
-
                static public void EmitCode ()
                {
                        if (type_container_resolve_order != null) {
                                foreach (TypeContainer tc in type_container_resolve_order)
                                        tc.EmitType ();
 
-                               if (Report.Errors > 0)
+                               if (ToplevelTypes.Compiler.Report.Errors > 0)
                                        return;
 
                                foreach (TypeContainer tc in type_container_resolve_order)
@@ -378,19 +374,14 @@ namespace Mono.CSharp {
                        }                       
 
                        CodeGen.Assembly.Emit (root);
-                       CodeGen.Module.Emit (root);
+                       root.Emit ();
                }
                
                //
                // Public Field, used to track which method is the public entry
                // point.
                //
-               static public MethodInfo EntryPoint;
-
-                //
-                // Track the location of the entry point.
-                //
-                static public Location EntryPointLocation;
+               static public Method EntryPoint;
 
                //
                // These are used to generate unique names on the structs and fields.
@@ -416,7 +407,7 @@ namespace Mono.CSharp {
                        FieldBuilder fb;
                        
                        if (impl_details_class == null){
-                               impl_details_class = CodeGen.Module.Builder.DefineType (
+                               impl_details_class = ToplevelTypes.Builder.DefineType (
                                        "<PrivateImplementationDetails>",
                                         TypeAttributes.NotPublic,
                                         TypeManager.object_type);
@@ -431,7 +422,7 @@ namespace Mono.CSharp {
                        return fb;
                }
 
-               public static void CheckUnsafeOption (Location loc)
+               public static void CheckUnsafeOption (Location loc, Report Report)
                {
                        if (!Unsafe) {
                                Report.Error (227, loc, 
@@ -440,5 +431,3 @@ namespace Mono.CSharp {
                }
        }
 }
-             
-