2009-03-25 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / mcs / rootcontext.cs
blob85f176fcab686aee257a901e5df174fb7955def2
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 #if GMCS_SOURCE
264 PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (type_builder);
265 #endif
266 type_builder.CreateType ();
270 type_container_resolve_order = null;
271 helper_classes = null;
272 //root = null;
273 TypeManager.CleanUp ();
276 /// <summary>
277 /// Used to register classes that need to be closed after all the
278 /// user defined classes
279 /// </summary>
280 public static void RegisterCompilerGeneratedType (TypeBuilder helper_class)
282 if (helper_classes == null)
283 helper_classes = new ArrayList ();
285 helper_classes.Add (helper_class);
288 static public void PopulateCoreType (TypeContainer root, string name)
290 DeclSpace ds = (DeclSpace) root.GetDefinition (name);
291 // Core type was imported
292 if (ds == null)
293 return;
295 ds.Define ();
298 static public void BootCorlib_PopulateCoreTypes ()
300 PopulateCoreType (root, "System.Object");
301 PopulateCoreType (root, "System.ValueType");
302 PopulateCoreType (root, "System.Attribute");
303 PopulateCoreType (root, "System.Runtime.CompilerServices.IndexerNameAttribute");
306 // <summary>
307 // Populates the structs and classes with fields and methods
308 // </summary>
310 // This is invoked after all interfaces, structs and classes
311 // have been defined through `ResolveTree'
312 static public void PopulateTypes ()
315 if (type_container_resolve_order != null){
316 foreach (TypeContainer tc in type_container_resolve_order)
317 tc.ResolveType ();
318 foreach (TypeContainer tc in type_container_resolve_order) {
319 try {
320 tc.Define ();
321 } catch (Exception e) {
322 throw new InternalErrorException (tc, e);
327 ArrayList delegates = root.Delegates;
328 if (delegates != null){
329 foreach (Delegate d in delegates)
330 d.Define ();
334 // Check for cycles in the struct layout
336 if (type_container_resolve_order != null){
337 Hashtable seen = new Hashtable ();
338 foreach (TypeContainer tc in type_container_resolve_order)
339 TypeManager.CheckStructCycles (tc, seen);
343 static public void EmitCode ()
345 if (type_container_resolve_order != null) {
346 foreach (TypeContainer tc in type_container_resolve_order)
347 tc.EmitType ();
349 if (Report.Errors > 0)
350 return;
352 foreach (TypeContainer tc in type_container_resolve_order)
353 tc.VerifyMembers ();
356 if (root.Delegates != null) {
357 foreach (Delegate d in root.Delegates)
358 d.Emit ();
361 CodeGen.Assembly.Emit (root);
362 root.Emit ();
366 // Public Field, used to track which method is the public entry
367 // point.
369 static public Method EntryPoint;
372 // These are used to generate unique names on the structs and fields.
374 static int field_count;
377 // Makes an initialized struct, returns the field builder that
378 // references the data. Thanks go to Sergey Chaban for researching
379 // how to do this. And coming up with a shorter mechanism than I
380 // was able to figure out.
382 // This works but makes an implicit public struct $ArrayType$SIZE and
383 // makes the fields point to it. We could get more control if we did
384 // use instead:
386 // 1. DefineNestedType on the impl_details_class with our struct.
388 // 2. Define the field on the impl_details_class
390 static public FieldBuilder MakeStaticData (byte [] data)
392 FieldBuilder fb;
394 if (impl_details_class == null){
395 impl_details_class = ToplevelTypes.Builder.DefineType (
396 "<PrivateImplementationDetails>",
397 TypeAttributes.NotPublic,
398 TypeManager.object_type);
400 RegisterCompilerGeneratedType (impl_details_class);
403 fb = impl_details_class.DefineInitializedData (
404 "$$field-" + (field_count++), data,
405 FieldAttributes.Static | FieldAttributes.Assembly);
407 return fb;
410 public static void CheckUnsafeOption (Location loc)
412 if (!Unsafe) {
413 Report.Error (227, loc,
414 "Unsafe code requires the `unsafe' command line option to be specified");