2010-04-07 Jb Evain <jbevain@novell.com>
[mcs.git] / mcs / rootcontext.cs
blob5ef66ceeaa0dc4fee365f8e24388e9515fcad017
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.Generic;
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 ISO_2 = 2,
26 V_3 = 3,
27 V_4 = 4,
28 Future = 100,
30 Default = LanguageVersion.V_4,
33 public enum MetadataVersion
35 v1,
36 v2,
40 public class RootContext {
43 // COMPILER OPTIONS CLASS
45 public static Target Target;
46 public static Platform Platform;
47 public static string TargetExt;
48 public static bool VerifyClsCompliance = true;
49 public static bool Optimize = true;
50 public static LanguageVersion Version;
51 public static bool EnhancedWarnings;
53 public static MetadataVersion MetadataCompatibilityVersion;
56 // We keep strongname related info here because
57 // it's also used as complier options from CSC 8.x
59 public static string StrongNameKeyFile;
60 public static string StrongNameKeyContainer;
61 public static bool StrongNameDelaySign;
64 // If set, enable XML documentation generation
66 public static Documentation Documentation;
68 static public string MainClass;
70 //
71 // The default compiler checked state
73 static public bool Checked;
76 // If true, it means that the compiler is executing as
77 // in eval mode so unresolved variables are resolved in
78 // static classes maintained by the eval engine.
80 static public bool EvalMode;
83 // If true, the compiler is operating in statement mode,
84 // this currently turns local variable declaration into
85 // static variables of a class
87 static public bool StatementMode;
90 // Whether to allow Unsafe code
92 static public bool Unsafe;
95 // Whether we are being linked against the standard libraries.
96 // This is only used to tell whether `System.Object' should
97 // have a base class or not.
99 public static bool StdLib;
101 public static bool NeedsEntryPoint {
102 get { return Target == Target.Exe || Target == Target.WinExe; }
106 // COMPILER OPTIONS CLASS END
110 // Contains the parsed tree
112 static ModuleCompiled root;
115 // This hashtable contains all of the #definitions across the source code
116 // it is used by the ConditionalAttribute handler.
118 static List<string> AllDefines;
121 // This keeps track of the order in which classes were defined
122 // so that we can poulate them in that order.
124 // Order is important, because we need to be able to tell, by
125 // examining the list of methods of the base class, which ones are virtual
126 // or abstract as well as the parent names (to implement new,
127 // override).
129 static List<TypeContainer> type_container_resolve_order;
132 // Holds a reference to the Private Implementation Details
133 // class.
135 static List<TypeBuilder> helper_classes;
137 static TypeBuilder impl_details_class;
140 // Constructor
142 static RootContext ()
144 Reset (true);
147 public static void PartialReset ()
149 Reset (false);
152 public static void Reset (bool full)
154 if (full)
155 root = null;
157 type_container_resolve_order = new List<TypeContainer> ();
158 EntryPoint = null;
159 Checked = false;
160 Unsafe = false;
161 StdLib = true;
162 StrongNameKeyFile = null;
163 StrongNameKeyContainer = null;
164 StrongNameDelaySign = false;
165 MainClass = null;
166 Target = Target.Exe;
167 TargetExt = ".exe";
168 Platform = Platform.AnyCPU;
169 Version = LanguageVersion.Default;
170 Documentation = null;
171 impl_details_class = null;
172 helper_classes = null;
174 #if NET_4_0
175 MetadataCompatibilityVersion = MetadataVersion.v4;
176 #else
177 MetadataCompatibilityVersion = MetadataVersion.v2;
178 #endif
181 // Setup default defines
183 AllDefines = new List<string> ();
184 AddConditional ("__MonoCS__");
187 public static void AddConditional (string p)
189 if (AllDefines.Contains (p))
190 return;
191 AllDefines.Add (p);
194 public static bool IsConditionalDefined (string value)
196 return AllDefines.Contains (value);
199 static public ModuleCompiled ToplevelTypes {
200 get { return root; }
201 set { root = value; }
204 public static void RegisterOrder (TypeContainer tc)
206 type_container_resolve_order.Add (tc);
209 // <remarks>
210 // This function is used to resolve the hierarchy tree.
211 // It processes interfaces, structs and classes in that order.
213 // It creates the TypeBuilder's as it processes the user defined
214 // types.
215 // </remarks>
216 static public void ResolveTree ()
218 root.Resolve ();
221 // Interfaces are processed next, as classes and
222 // structs might inherit from an object or implement
223 // a set of interfaces, we need to be able to tell
224 // them appart by just using the TypeManager.
226 foreach (TypeContainer tc in root.Types)
227 tc.CreateType ();
229 foreach (TypeContainer tc in root.Types)
230 tc.DefineType ();
232 if (root.Delegates != null)
233 foreach (Delegate d in root.Delegates)
234 d.DefineType ();
237 // <summary>
238 // Closes all open types
239 // </summary>
241 // <remarks>
242 // We usually use TypeBuilder types. When we are done
243 // creating the type (which will happen after we have added
244 // methods, fields, etc) we need to "Define" them before we
245 // can save the Assembly
246 // </remarks>
247 static public void CloseTypes ()
250 // We do this in two passes, first we close the structs,
251 // then the classes, because it seems the code needs it this
252 // way. If this is really what is going on, we should probably
253 // make sure that we define the structs in order as well.
255 foreach (TypeContainer tc in type_container_resolve_order){
256 if (tc.Kind == MemberKind.Struct && tc.Parent == root){
257 tc.CloseType ();
261 foreach (TypeContainer tc in type_container_resolve_order){
262 if (!(tc.Kind == MemberKind.Struct && tc.Parent == root))
263 tc.CloseType ();
266 if (root.Delegates != null)
267 foreach (Delegate d in root.Delegates)
268 d.CloseType ();
270 if (root.CompilerGeneratedClasses != null)
271 foreach (CompilerGeneratedClass c in root.CompilerGeneratedClasses)
272 c.CloseType ();
275 // If we have a <PrivateImplementationDetails> class, close it
277 if (helper_classes != null){
278 foreach (TypeBuilder type_builder in helper_classes) {
279 PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (type_builder);
280 type_builder.CreateType ();
284 type_container_resolve_order = null;
285 helper_classes = null;
286 //root = null;
287 TypeManager.CleanUp ();
290 /// <summary>
291 /// Used to register classes that need to be closed after all the
292 /// user defined classes
293 /// </summary>
294 public static void RegisterCompilerGeneratedType (TypeBuilder helper_class)
296 if (helper_classes == null)
297 helper_classes = new List<TypeBuilder> ();
299 helper_classes.Add (helper_class);
302 static public DeclSpace PopulateCoreType (TypeContainer root, string name)
304 DeclSpace ds = (DeclSpace) root.GetDefinition (name);
305 // Core type was imported
306 if (ds == null)
307 return null;
309 ds.Define ();
310 return ds;
313 static public void BootCorlib_PopulateCoreTypes ()
315 // Clear -nostdlib flag when object type is imported
316 if (PopulateCoreType (root, "System.Object") == null)
317 RootContext.StdLib = true;
319 PopulateCoreType (root, "System.ValueType");
320 PopulateCoreType (root, "System.Attribute");
321 PopulateCoreType (root, "System.Runtime.CompilerServices.IndexerNameAttribute");
324 // <summary>
325 // Populates the structs and classes with fields and methods
326 // </summary>
328 // This is invoked after all interfaces, structs and classes
329 // have been defined through `ResolveTree'
330 static public void PopulateTypes ()
333 if (type_container_resolve_order != null){
334 foreach (TypeContainer tc in type_container_resolve_order)
335 tc.ResolveType ();
336 foreach (TypeContainer tc in type_container_resolve_order) {
337 try {
338 tc.Define ();
339 } catch (Exception e) {
340 throw new InternalErrorException (tc, e);
345 var delegates = root.Delegates;
346 if (delegates != null){
347 foreach (Delegate d in delegates)
348 d.Define ();
352 // Check for cycles in the struct layout
354 if (type_container_resolve_order != null){
355 var seen = new Dictionary<TypeContainer, object> ();
356 foreach (TypeContainer tc in type_container_resolve_order)
357 TypeManager.CheckStructCycles (tc, seen);
361 static public void EmitCode ()
363 if (type_container_resolve_order != null) {
364 foreach (TypeContainer tc in type_container_resolve_order)
365 tc.EmitType ();
367 if (ToplevelTypes.Compiler.Report.Errors > 0)
368 return;
370 foreach (TypeContainer tc in type_container_resolve_order)
371 tc.VerifyMembers ();
374 if (root.Delegates != null) {
375 foreach (Delegate d in root.Delegates)
376 d.Emit ();
379 if (root.CompilerGeneratedClasses != null)
380 foreach (CompilerGeneratedClass c in root.CompilerGeneratedClasses)
381 c.EmitType ();
383 CodeGen.Assembly.Emit (root);
384 root.Emit ();
388 // Public Field, used to track which method is the public entry
389 // point.
391 static public Method EntryPoint;
394 // These are used to generate unique names on the structs and fields.
396 static int field_count;
399 // Makes an initialized struct, returns the field builder that
400 // references the data. Thanks go to Sergey Chaban for researching
401 // how to do this. And coming up with a shorter mechanism than I
402 // was able to figure out.
404 // This works but makes an implicit public struct $ArrayType$SIZE and
405 // makes the fields point to it. We could get more control if we did
406 // use instead:
408 // 1. DefineNestedType on the impl_details_class with our struct.
410 // 2. Define the field on the impl_details_class
412 static public FieldBuilder MakeStaticData (byte [] data)
414 FieldBuilder fb;
416 if (impl_details_class == null){
417 impl_details_class = ToplevelTypes.Builder.DefineType (
418 "<PrivateImplementationDetails>",
419 TypeAttributes.NotPublic,
420 TypeManager.object_type);
422 RegisterCompilerGeneratedType (impl_details_class);
425 fb = impl_details_class.DefineInitializedData (
426 "$$field-" + (field_count++), data,
427 FieldAttributes.Static | FieldAttributes.Assembly);
429 return fb;
432 public static void CheckUnsafeOption (Location loc, Report Report)
434 if (!Unsafe) {
435 Report.Error (227, loc,
436 "Unsafe code requires the `unsafe' command line option to be specified");