2009-05-22 Zoltan Varga <vargaz@gmail.com>
[mcs.git] / mcs / rootcontext.cs
blobe6c2c4b2f1dc5ea543ad89cab34d2add27f13dc4
1 //
2 // rootcontext.cs: keeps track of our tree representation, and assemblies loaded.
3 //
4 // Author: Miguel de Icaza (miguel@ximian.com)
5 // Ravi Pratap (ravi@ximian.com)
6 // Marek Safar (marek.safar@gmail.com)
7 //
8 //
9 // Dual licensed under the terms of the MIT X11 or GNU GPL
11 // Copyright 2001 Ximian, Inc (http://www.ximian.com)
12 // Copyright 2004-2008 Novell, Inc
14 using System;
15 using System.Collections;
16 using System.Reflection;
17 using System.Reflection.Emit;
18 using System.Diagnostics;
20 namespace Mono.CSharp {
22 public enum LanguageVersion
24 ISO_1 = 1,
25 Default_MCS = 2,
26 ISO_2 = 3,
27 LINQ = 4,
28 Future = 5,
30 #if GMCS_SOURCE
31 Default = LINQ
32 #else
33 Default = Default_MCS
34 #endif
37 public class RootContext {
40 // COMPILER OPTIONS CLASS
42 public static Target Target;
43 public static string TargetExt;
44 public static bool VerifyClsCompliance = true;
45 public static bool Optimize = true;
46 public static LanguageVersion Version;
49 // We keep strongname related info here because
50 // it's also used as complier options from CSC 8.x
52 public static string StrongNameKeyFile;
53 public static string StrongNameKeyContainer;
54 public static bool StrongNameDelaySign;
57 // If set, enable XML documentation generation
59 public static Documentation Documentation;
61 static public string MainClass;
63 //
64 // The default compiler checked state
66 static public bool Checked;
69 // If true, it means that the compiler is executing as
70 // in eval mode so unresolved variables are resolved in
71 // static classes maintained by the eval engine.
73 static public bool EvalMode;
76 // If true, the compiler is operating in statement mode,
77 // this currently turns local variable declaration into
78 // static variables of a class
80 static public bool StatementMode;
83 // Whether to allow Unsafe code
85 static public bool Unsafe;
88 // Whether we are being linked against the standard libraries.
89 // This is only used to tell whether `System.Object' should
90 // have a base class or not.
92 public static bool StdLib;
94 public static bool NeedsEntryPoint {
95 get { return Target == Target.Exe || Target == Target.WinExe; }
99 // COMPILER OPTIONS CLASS END
103 // Contains the parsed tree
105 static ModuleContainer root;
108 // This hashtable contains all of the #definitions across the source code
109 // it is used by the ConditionalAttribute handler.
111 static ArrayList AllDefines;
114 // This keeps track of the order in which classes were defined
115 // so that we can poulate them in that order.
117 // Order is important, because we need to be able to tell, by
118 // examining the list of methods of the base class, which ones are virtual
119 // or abstract as well as the parent names (to implement new,
120 // override).
122 static ArrayList type_container_resolve_order;
125 // Holds a reference to the Private Implementation Details
126 // class.
128 static ArrayList helper_classes;
130 static TypeBuilder impl_details_class;
133 // Constructor
135 static RootContext ()
137 Reset (true);
140 public static void PartialReset ()
142 Reset (false);
145 public static void Reset (bool full)
147 if (full)
148 root = null;
150 type_container_resolve_order = new ArrayList ();
151 EntryPoint = null;
152 Report.WarningLevel = 4;
153 Checked = false;
154 Unsafe = false;
155 StdLib = true;
156 StrongNameKeyFile = null;
157 StrongNameKeyContainer = null;
158 StrongNameDelaySign = false;
159 MainClass = null;
160 Target = Target.Exe;
161 TargetExt = ".exe";
162 Version = LanguageVersion.Default;
163 Documentation = null;
164 impl_details_class = null;
165 helper_classes = null;
168 // Setup default defines
170 AllDefines = new ArrayList ();
171 AddConditional ("__MonoCS__");
174 public static void AddConditional (string p)
176 if (AllDefines.Contains (p))
177 return;
178 AllDefines.Add (p);
181 public static bool IsConditionalDefined (string value)
183 return AllDefines.Contains (value);
186 static public ModuleContainer ToplevelTypes {
187 get { return root; }
188 set { root = value; }
191 public static void RegisterOrder (TypeContainer tc)
193 type_container_resolve_order.Add (tc);
196 // <remarks>
197 // This function is used to resolve the hierarchy tree.
198 // It processes interfaces, structs and classes in that order.
200 // It creates the TypeBuilder's as it processes the user defined
201 // types.
202 // </remarks>
203 static public void ResolveTree ()
205 root.Resolve ();
208 // Interfaces are processed next, as classes and
209 // structs might inherit from an object or implement
210 // a set of interfaces, we need to be able to tell
211 // them appart by just using the TypeManager.
213 foreach (TypeContainer tc in root.Types)
214 tc.CreateType ();
216 foreach (TypeContainer tc in root.Types)
217 tc.DefineType ();
219 if (root.Delegates != null)
220 foreach (Delegate d in root.Delegates)
221 d.DefineType ();
224 // <summary>
225 // Closes all open types
226 // </summary>
228 // <remarks>
229 // We usually use TypeBuilder types. When we are done
230 // creating the type (which will happen after we have added
231 // methods, fields, etc) we need to "Define" them before we
232 // can save the Assembly
233 // </remarks>
234 static public void CloseTypes ()
237 // We do this in two passes, first we close the structs,
238 // then the classes, because it seems the code needs it this
239 // way. If this is really what is going on, we should probably
240 // make sure that we define the structs in order as well.
242 foreach (TypeContainer tc in type_container_resolve_order){
243 if (tc.Kind == Kind.Struct && tc.Parent == root){
244 tc.CloseType ();
248 foreach (TypeContainer tc in type_container_resolve_order){
249 if (!(tc.Kind == Kind.Struct && tc.Parent == root))
250 tc.CloseType ();
253 if (root.Delegates != null)
254 foreach (Delegate d in root.Delegates)
255 d.CloseType ();
259 // If we have a <PrivateImplementationDetails> class, close it
261 if (helper_classes != null){
262 foreach (TypeBuilder type_builder in helper_classes) {
263 PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (type_builder);
264 type_builder.CreateType ();
268 type_container_resolve_order = null;
269 helper_classes = null;
270 //root = null;
271 TypeManager.CleanUp ();
274 /// <summary>
275 /// Used to register classes that need to be closed after all the
276 /// user defined classes
277 /// </summary>
278 public static void RegisterCompilerGeneratedType (TypeBuilder helper_class)
280 if (helper_classes == null)
281 helper_classes = new ArrayList ();
283 helper_classes.Add (helper_class);
286 static public void PopulateCoreType (TypeContainer root, string name)
288 DeclSpace ds = (DeclSpace) root.GetDefinition (name);
289 // Core type was imported
290 if (ds == null)
291 return;
293 ds.Define ();
296 static public void BootCorlib_PopulateCoreTypes ()
298 PopulateCoreType (root, "System.Object");
299 PopulateCoreType (root, "System.ValueType");
300 PopulateCoreType (root, "System.Attribute");
301 PopulateCoreType (root, "System.Runtime.CompilerServices.IndexerNameAttribute");
304 // <summary>
305 // Populates the structs and classes with fields and methods
306 // </summary>
308 // This is invoked after all interfaces, structs and classes
309 // have been defined through `ResolveTree'
310 static public void PopulateTypes ()
313 if (type_container_resolve_order != null){
314 foreach (TypeContainer tc in type_container_resolve_order)
315 tc.ResolveType ();
316 foreach (TypeContainer tc in type_container_resolve_order) {
317 try {
318 tc.Define ();
319 } catch (Exception e) {
320 throw new InternalErrorException (tc, e);
325 ArrayList delegates = root.Delegates;
326 if (delegates != null){
327 foreach (Delegate d in delegates)
328 d.Define ();
332 // Check for cycles in the struct layout
334 if (type_container_resolve_order != null){
335 Hashtable seen = new Hashtable ();
336 foreach (TypeContainer tc in type_container_resolve_order)
337 TypeManager.CheckStructCycles (tc, seen);
341 static public void EmitCode ()
343 if (type_container_resolve_order != null) {
344 foreach (TypeContainer tc in type_container_resolve_order)
345 tc.EmitType ();
347 if (Report.Errors > 0)
348 return;
350 foreach (TypeContainer tc in type_container_resolve_order)
351 tc.VerifyMembers ();
354 if (root.Delegates != null) {
355 foreach (Delegate d in root.Delegates)
356 d.Emit ();
359 CodeGen.Assembly.Emit (root);
360 root.Emit ();
364 // Public Field, used to track which method is the public entry
365 // point.
367 static public Method EntryPoint;
370 // These are used to generate unique names on the structs and fields.
372 static int field_count;
375 // Makes an initialized struct, returns the field builder that
376 // references the data. Thanks go to Sergey Chaban for researching
377 // how to do this. And coming up with a shorter mechanism than I
378 // was able to figure out.
380 // This works but makes an implicit public struct $ArrayType$SIZE and
381 // makes the fields point to it. We could get more control if we did
382 // use instead:
384 // 1. DefineNestedType on the impl_details_class with our struct.
386 // 2. Define the field on the impl_details_class
388 static public FieldBuilder MakeStaticData (byte [] data)
390 FieldBuilder fb;
392 if (impl_details_class == null){
393 impl_details_class = ToplevelTypes.Builder.DefineType (
394 "<PrivateImplementationDetails>",
395 TypeAttributes.NotPublic,
396 TypeManager.object_type);
398 RegisterCompilerGeneratedType (impl_details_class);
401 fb = impl_details_class.DefineInitializedData (
402 "$$field-" + (field_count++), data,
403 FieldAttributes.Static | FieldAttributes.Assembly);
405 return fb;
408 public static void CheckUnsafeOption (Location loc)
410 if (!Unsafe) {
411 Report.Error (227, loc,
412 "Unsafe code requires the `unsafe' command line option to be specified");