Nothing to see here
[mono.git] / mcs / mcs / rootcontext.cs
index 46cd399c239adc358e1a59055d38eb5bd395a28c..686e4f400b6a6ce6bc494551d25148d4975019ea 100644 (file)
@@ -6,10 +6,10 @@
 //            Marek Safar  (marek.safar@gmail.com)
 //
 //
-// Licensed under the terms of the GNU GPL
+// Dual licensed under the terms of the MIT X11 or GNU GPL
 //
-// (C) 2001 Ximian, Inc (http://www.ximian.com)
-// (C) 2004 Novell, Inc
+// Copyright 2001 Ximian, Inc (http://www.ximian.com)
+// Copyright 2004-2008 Novell, Inc
 
 using System;
 using System.Collections;
@@ -21,24 +21,68 @@ namespace Mono.CSharp {
 
        public enum LanguageVersion
        {
-               Default = 0,
-               ISO_1   = 1,
-               LINQ
+               ISO_1           = 1,
+               Default_MCS     = 2,
+               ISO_2           = 3,
+               LINQ            = 4,
+
+#if GMCS_SOURCE
+               Default         = LINQ
+#else
+               Default         = Default_MCS
+#endif
        }
 
        public class RootContext {
 
                //
-               // Contains the parsed tree
+               // COMPILER OPTIONS CLASS
                //
-               static RootTypes root;
+               public static Target Target;
+               public static string TargetExt;
+               public static bool VerifyClsCompliance = true;
+               public static bool Optimize = true;
+               public static LanguageVersion Version;
 
                //
-               // This hashtable contains all of the #definitions across the source code
-               // it is used by the ConditionalAttribute handler.
+               // We keep strongname related info here because
+               // it's also used as complier options from CSC 8.x
                //
-               public static Hashtable AllDefines = new Hashtable ();
+               public static string StrongNameKeyFile;
+               public static string StrongNameKeyContainer;
+               public static bool StrongNameDelaySign;
+
+               //
+               // If set, enable XML documentation generation
+               //
+               public static Documentation Documentation;
+
+               static public string MainClass;
+
+               // 
+               // The default compiler checked state
+               //
+               static public bool Checked;
+
+               //
+               // If true, it means that the compiler is executing as
+               // in eval mode so unresolved variables are resolved in
+               // static classes maintained by the eval engine.
+               //
+               static public bool EvalMode;
+
+               //
+               // If true, the compiler is operating in statement mode,
+               // this currently turns local variable declaration into
+               // static variables of a class
+               //
+               static public bool StatementMode;
                
+               //
+               // Whether to allow Unsafe code
+               //
+               static public bool Unsafe;
+
                //
                // Whether we are being linked against the standard libraries.
                // This is only used to tell whether `System.Object' should
@@ -46,6 +90,25 @@ namespace Mono.CSharp {
                //
                public static bool StdLib;
 
+               public static bool NeedsEntryPoint {
+                       get { return Target == Target.Exe || Target == Target.WinExe; }
+               }
+
+               //
+               // COMPILER OPTIONS CLASS END
+               //
+
+               //
+               // Contains the parsed tree
+               //
+               static RootTypes root;
+
+               //
+               // This hashtable contains all of the #definitions across the source code
+               // it is used by the ConditionalAttribute handler.
+               //
+               static ArrayList AllDefines = new ArrayList ();
+               
                //
                // This keeps track of the order in which classes were defined
                // so that we can poulate them in that order.
@@ -65,49 +128,27 @@ namespace Mono.CSharp {
                
                static TypeBuilder impl_details_class;
 
-               public static int WarningLevel;
-
-               public static Target Target;
-               public static string TargetExt;
-
-               public static bool VerifyClsCompliance = true;
-
-               /// <summary>
-               /// Holds /optimize option
-               /// </summary>
-               public static bool Optimize = true;
-
-               public static LanguageVersion Version;
-
-               //
-               // 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;
-
-               //
-               // If set, enable XML documentation generation
-               //
-               public static Documentation Documentation;
-
-               static public string MainClass;
-
                //
                // Constructor
                //
                static RootContext ()
                {
-                       Reset ();
+                       Reset (true);
                }
 
-               public static void Reset ()
+               public static void PartialReset ()
+               {
+                       Reset (false);
+               }
+               
+               public static void Reset (bool full)
                {
-                       root = new RootTypes ();
+                       if (full)
+                               root = new RootTypes ();
+                       
                        type_container_resolve_order = new ArrayList ();
                        EntryPoint = null;
-                       WarningLevel = 3;
+                       Report.WarningLevel = 4;
                        Checked = false;
                        Unsafe = false;
                        StdLib = true;
@@ -121,10 +162,24 @@ namespace Mono.CSharp {
                        Documentation = null;
                        impl_details_class = null;
                        helper_classes = null;
+
+                       //
+                       // Setup default defines
+                       //
+                       RootContext.AllDefines = new ArrayList ();
+                       RootContext.AddConditional ("__MonoCS__");
                }
 
-               public static bool NeedsEntryPoint {
-                       get { return RootContext.Target == Target.Exe || RootContext.Target == Target.WinExe; }
+               public static void AddConditional (string p)
+               {
+                       if (AllDefines.Contains (p))
+                               return;
+                       AllDefines.Add (p);
+               }
+
+               public static bool IsConditionalDefined (string value)
+               {
+                       return AllDefines.Contains (value);
                }
 
                static public RootTypes ToplevelTypes {
@@ -136,16 +191,6 @@ namespace Mono.CSharp {
                        type_container_resolve_order.Add (tc);
                }
                
-               // 
-               // The default compiler checked state
-               //
-               static public bool Checked;
-
-               //
-               // Whether to allow Unsafe code
-               //
-               static public bool Unsafe;
-               
                // <remarks>
                //   This function is used to resolve the hierarchy tree.
                //   It processes interfaces, structs and classes in that order.
@@ -172,239 +217,6 @@ namespace Mono.CSharp {
                                        d.DefineType ();
                }
 
-               delegate bool VerifyBootstrapType (Type t);
-
-               static Type BootstrapCorlib_ResolveType (TypeContainer root, string name, VerifyBootstrapType typeVerifier)
-               {
-                       TypeLookupExpression tle = new TypeLookupExpression (name);
-                       Report.DisableErrors ();
-                       TypeExpr te = tle.ResolveAsTypeTerminal (root, false);
-                       Report.EnableErrors ();
-                       if (te == null) {
-                               Report.Error (518, "The predefined type `{0}' is not defined or imported", name);
-                               return null;
-                       }
-
-                       Type t = te.Type;
-                       if (!typeVerifier (t)) {
-                               MemberCore mc = root.GetDefinition (name);
-                               Location loc = Location.Null;
-                               if (mc != null) {
-                                       name = mc.GetSignatureForError ();
-                                       loc = mc.Location;
-                               }
-
-                               Report.Error (520, loc, "The predefined type `{0}' is not declared correctly", name);
-                               return null;
-                       }
-
-                       AttributeTester.RegisterNonObsoleteType (t);
-                       return t;
-               }
-               //
-               // Resolves a single class during the corlib bootstrap process
-               //
-               static Type BootstrapCorlib_ResolveClass (TypeContainer root, string name)
-               {
-                       return BootstrapCorlib_ResolveType (root, name, IsClass);
-               }
-
-               static bool IsClass (Type t)
-               {
-                       DeclSpace ds = TypeManager.LookupDeclSpace (t);
-                       if (ds != null)
-                               return ds is Class;
-                       return t.IsClass;
-               }
-
-               //
-               // Resolves a struct during the corlib bootstrap process
-               //
-               static void BootstrapCorlib_ResolveStruct (TypeContainer root, string name)
-               {
-                       BootstrapCorlib_ResolveType (root, name, IsStruct);
-               }
-
-               static bool IsStruct (Type t)
-               {
-                       DeclSpace ds = TypeManager.LookupDeclSpace (t);
-                       if (ds != null)
-                               return ds is Struct;
-                       
-                       return TypeManager.IsSubclassOf (t, TypeManager.value_type);                    
-               }
-
-               //
-               // Resolves an interface during the corlib bootstrap process
-               //
-               static void BootstrapCorlib_ResolveInterface (TypeContainer root, string name)
-               {
-                       BootstrapCorlib_ResolveType (root, name, IsInterface);
-               }
-
-               static bool IsInterface (Type t)
-               {
-                       return t.IsInterface;
-               }
-
-               //
-               // Resolves a delegate during the corlib bootstrap process
-               //
-               static void BootstrapCorlib_ResolveDelegate (TypeContainer root, string name)
-               {
-                       BootstrapCorlib_ResolveType (root, name, IsDelegate);
-               }
-
-               static bool IsDelegate (Type t)
-               {
-                       return TypeManager.IsDelegateType (t);
-               }
-
-               /// <summary>
-               ///    Resolves the core types in the compiler when compiling with --nostdlib
-               /// </summary>
-               static public void ResolveCore ()
-               {
-                       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;
-
-                       //
-                       // The core attributes
-                       //
-                       BootstrapCorlib_ResolveInterface (root, "System.Runtime.InteropServices._Attribute");
-                       TypeManager.attribute_type = BootstrapCorlib_ResolveClass (root, "System.Attribute");
-                       TypeManager.obsolete_attribute_type = BootstrapCorlib_ResolveClass (root, "System.ObsoleteAttribute");
-                       TypeManager.indexer_name_type = BootstrapCorlib_ResolveClass (root, "System.Runtime.CompilerServices.IndexerNameAttribute");
-                       
-                       string [] interfaces_first_stage = {
-                               "System.IComparable", "System.ICloneable",
-                               "System.IConvertible",
-                               
-                               "System.Collections.IEnumerable",
-                               "System.Collections.ICollection",
-                               "System.Collections.IEnumerator",
-                               "System.Collections.IList", 
-                               "System.IAsyncResult",
-                               "System.IDisposable",
-                               
-                               "System.Runtime.Serialization.ISerializable",
-
-                               "System.Reflection.IReflect",
-                               "System.Reflection.ICustomAttributeProvider",
-#if GMCS_SOURCE
-                               "System.Runtime.InteropServices._Exception",
-
-                               //
-                               // Generic types
-                               //
-                               "System.Collections.Generic.IEnumerator`1",
-                               "System.Collections.Generic.IEnumerable`1"
-#endif
-                       };
-
-                       foreach (string iname in interfaces_first_stage)
-                               BootstrapCorlib_ResolveInterface (root, iname);
-
-                       //
-                       // These are the base value types
-                       //
-                       string [] structs_first_stage = {
-                               "System.Byte",    "System.SByte",
-                               "System.Int16",   "System.UInt16",
-                               "System.Int32",   "System.UInt32",
-                               "System.Int64",   "System.UInt64",
-                       };
-
-                       foreach (string cname in structs_first_stage)
-                               BootstrapCorlib_ResolveStruct (root, cname);
-
-                       //
-                       // Now, we can load the enumerations, after this point,
-                       // we can use enums.
-                       //
-                       TypeManager.InitEnumUnderlyingTypes ();
-
-                       string [] structs_second_stage = {
-                               "System.Single",  "System.Double",
-                               "System.Char",    "System.Boolean",
-                               "System.Decimal", "System.Void",
-                               "System.RuntimeFieldHandle",
-                               "System.RuntimeArgumentHandle",
-                               "System.RuntimeTypeHandle",
-                               "System.IntPtr",
-                               "System.TypedReference",
-                               "System.ArgIterator"
-                       };
-                       
-                       foreach (string cname in structs_second_stage)
-                               BootstrapCorlib_ResolveStruct (root, cname);
-                       
-                       //
-                       // These are classes that depends on the core interfaces
-                       //
-                       string [] classes_second_stage = {
-                               "System.Enum",
-                               "System.String",
-                               "System.Array",
-                               "System.Reflection.MemberInfo",
-                               "System.Type",
-                               "System.Exception",
-#if GMCS_SOURCE
-                               "System.Activator",
-#endif
-
-                               //
-                               // These are not really important in the order, but they
-                               // are used by the compiler later on (typemanager/CoreLookupType-d)
-                               //
-                               "System.Runtime.CompilerServices.RuntimeHelpers",
-                               "System.Reflection.DefaultMemberAttribute",
-                               "System.Threading.Monitor",
-                               "System.Threading.Interlocked",
-                               
-                               "System.AttributeUsageAttribute",
-                               "System.Runtime.InteropServices.DllImportAttribute",
-                               "System.Runtime.CompilerServices.MethodImplAttribute",
-                               "System.Runtime.InteropServices.MarshalAsAttribute",
-#if GMCS_SOURCE
-                               "System.Runtime.CompilerServices.CompilerGeneratedAttribute",
-                               "System.Runtime.CompilerServices.FixedBufferAttribute",
-#endif
-                               "System.Diagnostics.ConditionalAttribute",
-                               "System.ParamArrayAttribute",
-                               "System.CLSCompliantAttribute",
-                               "System.Security.UnverifiableCodeAttribute",
-                               "System.Security.Permissions.SecurityAttribute",
-                               "System.Runtime.CompilerServices.DecimalConstantAttribute",
-                               "System.Runtime.InteropServices.InAttribute",
-                               "System.Runtime.InteropServices.OutAttribute",
-                               "System.Runtime.InteropServices.StructLayoutAttribute",
-                               "System.Runtime.InteropServices.FieldOffsetAttribute",
-#if GMCS_SOURCE
-                               "System.Runtime.InteropServices.DefaultCharSetAttribute",
-#endif
-                               "System.InvalidOperationException",
-                               "System.NotSupportedException",
-                               "System.MarshalByRefObject",
-                               "System.Security.CodeAccessPermission",
-                               "System.Runtime.CompilerServices.RequiredAttributeAttribute",
-                               "System.Runtime.InteropServices.GuidAttribute",
-                               "System.Reflection.AssemblyCultureAttribute"
-                       };
-
-                       foreach (string cname in classes_second_stage)
-                               BootstrapCorlib_ResolveClass (root, cname);
-#if GMCS_SOURCE
-                       BootstrapCorlib_ResolveStruct (root, "System.Nullable`1");
-#endif
-                       TypeManager.delegate_type = BootstrapCorlib_ResolveClass (root, "System.Delegate");
-                       BootstrapCorlib_ResolveClass (root, "System.MulticastDelegate");
-
-                       BootstrapCorlib_ResolveDelegate (root, "System.AsyncCallback");
-               }
-                       
                // <summary>
                //   Closes all open types
                // </summary>
@@ -445,7 +257,7 @@ namespace Mono.CSharp {
                        if (helper_classes != null){
                                foreach (TypeBuilder type_builder in helper_classes) {
 #if GMCS_SOURCE
-                                       type_builder.SetCustomAttribute (TypeManager.compiler_generated_attr);
+                                       type_builder.SetCustomAttribute (TypeManager.GetCompilerGeneratedAttribute (Location.Null));
 #endif
                                        type_builder.CreateType ();
                                }
@@ -476,7 +288,6 @@ namespace Mono.CSharp {
                        if (ds == null)
                                return;
 
-                       ds.DefineMembers ();
                        ds.Define ();
                }
                
@@ -501,13 +312,13 @@ namespace Mono.CSharp {
                                foreach (TypeContainer tc in type_container_resolve_order)
                                        tc.ResolveType ();
                                foreach (TypeContainer tc in type_container_resolve_order)
-                                       tc.DefineMembers ();
+                                       tc.Define ();
                        }
 
                        ArrayList delegates = root.Delegates;
                        if (delegates != null){
                                foreach (Delegate d in delegates)
-                                       d.DefineMembers ();
+                                       d.Define ();
                        }
 
                        //
@@ -520,43 +331,6 @@ namespace Mono.CSharp {
                        }
                }
 
-               //
-               // A generic hook delegate
-               //
-               public delegate void Hook ();
-
-               //
-               // A hook invoked when the code has been generated.
-               //
-               public static event Hook EmitCodeHook;
-
-               //
-               // 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) {
@@ -574,13 +348,6 @@ namespace Mono.CSharp {
                                foreach (Delegate d in root.Delegates)
                                        d.Emit ();
                        }                       
-                       //
-                       // Run any hooks after all the types have been defined.
-                       // This is used to create nested auxiliary classes for example
-                       //
-
-                       if (EmitCodeHook != null)
-                               EmitCodeHook ();
 
                        CodeGen.Assembly.Emit (root);
                        CodeGen.Module.Emit (root);