2009-02-19 Jb Evain <jbevain@novell.com>
[mcs.git] / mcs / rootcontext.cs
blob5962a33ee5f2dd7b86bb6cea94b5e1cbc0d4033b
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 RootTypes 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 = new RootTypes ();
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 RootContext.AllDefines = new ArrayList ();
171 RootContext.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 RootTypes ToplevelTypes {
187 get { return root; }
190 public static void RegisterOrder (TypeContainer tc)
192 type_container_resolve_order.Add (tc);
195 // <remarks>
196 // This function is used to resolve the hierarchy tree.
197 // It processes interfaces, structs and classes in that order.
199 // It creates the TypeBuilder's as it processes the user defined
200 // types.
201 // </remarks>
202 static public void ResolveTree ()
205 // Interfaces are processed next, as classes and
206 // structs might inherit from an object or implement
207 // a set of interfaces, we need to be able to tell
208 // them appart by just using the TypeManager.
210 foreach (TypeContainer tc in root.Types)
211 tc.CreateType ();
213 foreach (TypeContainer tc in root.Types)
214 tc.DefineType ();
216 if (root.Delegates != null)
217 foreach (Delegate d in root.Delegates)
218 d.DefineType ();
221 // <summary>
222 // Closes all open types
223 // </summary>
225 // <remarks>
226 // We usually use TypeBuilder types. When we are done
227 // creating the type (which will happen after we have added
228 // methods, fields, etc) we need to "Define" them before we
229 // can save the Assembly
230 // </remarks>
231 static public void CloseTypes ()
234 // We do this in two passes, first we close the structs,
235 // then the classes, because it seems the code needs it this
236 // way. If this is really what is going on, we should probably
237 // make sure that we define the structs in order as well.
239 foreach (TypeContainer tc in type_container_resolve_order){
240 if (tc.Kind == Kind.Struct && tc.Parent == root){
241 tc.CloseType ();
245 foreach (TypeContainer tc in type_container_resolve_order){
246 if (!(tc.Kind == Kind.Struct && tc.Parent == root))
247 tc.CloseType ();
250 if (root.Delegates != null)
251 foreach (Delegate d in root.Delegates)
252 d.CloseType ();
256 // If we have a <PrivateImplementationDetails> class, close it
258 if (helper_classes != null){
259 foreach (TypeBuilder type_builder in helper_classes) {
260 #if GMCS_SOURCE
261 type_builder.SetCustomAttribute (TypeManager.GetCompilerGeneratedAttribute (Location.Null));
262 #endif
263 type_builder.CreateType ();
267 type_container_resolve_order = null;
268 helper_classes = null;
269 //root = null;
270 TypeManager.CleanUp ();
273 /// <summary>
274 /// Used to register classes that need to be closed after all the
275 /// user defined classes
276 /// </summary>
277 public static void RegisterCompilerGeneratedType (TypeBuilder helper_class)
279 if (helper_classes == null)
280 helper_classes = new ArrayList ();
282 helper_classes.Add (helper_class);
285 static public void PopulateCoreType (TypeContainer root, string name)
287 DeclSpace ds = (DeclSpace) root.GetDefinition (name);
288 // Core type was imported
289 if (ds == null)
290 return;
292 ds.Define ();
295 static public void BootCorlib_PopulateCoreTypes ()
297 PopulateCoreType (root, "System.Object");
298 PopulateCoreType (root, "System.ValueType");
299 PopulateCoreType (root, "System.Attribute");
300 PopulateCoreType (root, "System.Runtime.CompilerServices.IndexerNameAttribute");
303 // <summary>
304 // Populates the structs and classes with fields and methods
305 // </summary>
307 // This is invoked after all interfaces, structs and classes
308 // have been defined through `ResolveTree'
309 static public void PopulateTypes ()
312 if (type_container_resolve_order != null){
313 foreach (TypeContainer tc in type_container_resolve_order)
314 tc.ResolveType ();
315 foreach (TypeContainer tc in type_container_resolve_order)
316 tc.Define ();
319 ArrayList delegates = root.Delegates;
320 if (delegates != null){
321 foreach (Delegate d in delegates)
322 d.Define ();
326 // Check for cycles in the struct layout
328 if (type_container_resolve_order != null){
329 Hashtable seen = new Hashtable ();
330 foreach (TypeContainer tc in type_container_resolve_order)
331 TypeManager.CheckStructCycles (tc, seen);
335 static public void EmitCode ()
337 if (type_container_resolve_order != null) {
338 foreach (TypeContainer tc in type_container_resolve_order)
339 tc.EmitType ();
341 if (Report.Errors > 0)
342 return;
344 foreach (TypeContainer tc in type_container_resolve_order)
345 tc.VerifyMembers ();
348 if (root.Delegates != null) {
349 foreach (Delegate d in root.Delegates)
350 d.Emit ();
353 CodeGen.Assembly.Emit (root);
354 CodeGen.Module.Emit (root);
358 // Public Field, used to track which method is the public entry
359 // point.
361 static public Method EntryPoint;
364 // These are used to generate unique names on the structs and fields.
366 static int field_count;
369 // Makes an initialized struct, returns the field builder that
370 // references the data. Thanks go to Sergey Chaban for researching
371 // how to do this. And coming up with a shorter mechanism than I
372 // was able to figure out.
374 // This works but makes an implicit public struct $ArrayType$SIZE and
375 // makes the fields point to it. We could get more control if we did
376 // use instead:
378 // 1. DefineNestedType on the impl_details_class with our struct.
380 // 2. Define the field on the impl_details_class
382 static public FieldBuilder MakeStaticData (byte [] data)
384 FieldBuilder fb;
386 if (impl_details_class == null){
387 impl_details_class = CodeGen.Module.Builder.DefineType (
388 "<PrivateImplementationDetails>",
389 TypeAttributes.NotPublic,
390 TypeManager.object_type);
392 RegisterCompilerGeneratedType (impl_details_class);
395 fb = impl_details_class.DefineInitializedData (
396 "$$field-" + (field_count++), data,
397 FieldAttributes.Static | FieldAttributes.Assembly);
399 return fb;
402 public static void CheckUnsafeOption (Location loc)
404 if (!Unsafe) {
405 Report.Error (227, loc,
406 "Unsafe code requires the `unsafe' command line option to be specified");