2 // rootcontext.cs: keeps track of our tree representation, and assemblies loaded.
4 // Author: Miguel de Icaza (miguel@ximian.com)
5 // Ravi Pratap (ravi@ximian.com)
7 // Licensed under the terms of the GNU GPL
9 // (C) 2001 Ximian, Inc (http://www.ximian.com)
10 // (C) 2004 Novell, Inc
13 using System.Collections;
14 using System.Reflection;
15 using System.Reflection.Emit;
16 using System.Diagnostics;
18 namespace Mono.CSharp {
20 public enum LanguageVersion
26 public class RootContext {
29 // Contains the parsed tree
31 static RootTypes root;
34 // This hashtable contains all of the #definitions across the source code
35 // it is used by the ConditionalAttribute handler.
37 public static Hashtable AllDefines = new Hashtable ();
40 // Whether we are being linked against the standard libraries.
41 // This is only used to tell whether `System.Object' should
42 // have a base class or not.
44 public static bool StdLib;
47 // This keeps track of the order in which classes were defined
48 // so that we can poulate them in that order.
50 // Order is important, because we need to be able to tell, by
51 // examining the list of methods of the base class, which ones are virtual
52 // or abstract as well as the parent names (to implement new,
55 static ArrayList type_container_resolve_order;
58 // Holds a reference to the Private Implementation Details
61 static ArrayList helper_classes;
63 static TypeBuilder impl_details_class;
65 public static int WarningLevel;
67 public static Target Target;
68 public static string TargetExt;
70 public static bool VerifyClsCompliance = true;
73 /// Holds /optimize option
75 public static bool Optimize = true;
77 public static LanguageVersion Version;
80 // We keep strongname related info here because
81 // it's also used as complier options from CSC 8.x
83 public static string StrongNameKeyFile;
84 public static string StrongNameKeyContainer;
85 public static bool StrongNameDelaySign;
88 // If set, enable XML documentation generation
90 public static Documentation Documentation;
92 static public string MainClass;
102 public static void Reset ()
104 root = new RootTypes ();
105 type_container_resolve_order = new ArrayList ();
111 StrongNameKeyFile = null;
112 StrongNameKeyContainer = null;
113 StrongNameDelaySign = false;
117 Version = LanguageVersion.Default;
118 Documentation = null;
119 impl_details_class = null;
120 helper_classes = null;
123 public static bool NeedsEntryPoint {
124 get { return RootContext.Target == Target.Exe || RootContext.Target == Target.WinExe; }
127 static public RootTypes ToplevelTypes {
131 public static void RegisterOrder (TypeContainer tc)
133 type_container_resolve_order.Add (tc);
137 // The default compiler checked state
139 static public bool Checked;
142 // Whether to allow Unsafe code
144 static public bool Unsafe;
147 // This function is used to resolve the hierarchy tree.
148 // It processes interfaces, structs and classes in that order.
150 // It creates the TypeBuilder's as it processes the user defined
153 static public void ResolveTree ()
156 // Interfaces are processed next, as classes and
157 // structs might inherit from an object or implement
158 // a set of interfaces, we need to be able to tell
159 // them appart by just using the TypeManager.
161 ArrayList ifaces = root.Interfaces;
163 foreach (TypeContainer i in ifaces)
167 foreach (TypeContainer tc in root.Types)
170 if (root.Delegates != null)
171 foreach (Delegate d in root.Delegates)
174 if (root.Enums != null)
175 foreach (Enum e in root.Enums)
179 static void Error_TypeConflict (string name, Location loc)
182 520, loc, "`" + name + "' conflicts with a predefined type");
185 static void Error_TypeConflict (string name)
188 520, "`" + name + "' conflicts with a predefined type");
192 // Resolves a single class during the corlib bootstrap process
194 static TypeBuilder BootstrapCorlib_ResolveClass (TypeContainer root, string name)
196 object o = root.GetDefinition (name);
198 Report.Error (518, "The predefined type `" + name + "' is not defined or imported");
204 DeclSpace d = (DeclSpace) o;
206 Error_TypeConflict (name, d.Location);
208 Error_TypeConflict (name);
213 return ((DeclSpace) o).DefineType ();
217 // Resolves a struct during the corlib bootstrap process
219 static void BootstrapCorlib_ResolveStruct (TypeContainer root, string name)
221 object o = root.GetDefinition (name);
223 Report.Error (518, "The predefined type `" + name + "' is not defined or imported");
229 DeclSpace d = (DeclSpace) o;
231 Error_TypeConflict (name, d.Location);
233 Error_TypeConflict (name);
238 ((DeclSpace) o).DefineType ();
242 // Resolves a struct during the corlib bootstrap process
244 static void BootstrapCorlib_ResolveInterface (TypeContainer root, string name)
246 object o = root.GetDefinition (name);
248 Report.Error (518, "The predefined type `" + name + "' is not defined or imported");
252 if (!(o is Interface)){
254 DeclSpace d = (DeclSpace) o;
256 Error_TypeConflict (name, d.Location);
258 Error_TypeConflict (name);
263 ((DeclSpace) o).DefineType ();
267 // Resolves a delegate during the corlib bootstrap process
269 static void BootstrapCorlib_ResolveDelegate (TypeContainer root, string name)
271 object o = root.GetDefinition (name);
273 Report.Error (518, "The predefined type `" + name + "' is not defined or imported");
277 if (!(o is Delegate)){
278 Error_TypeConflict (name);
282 ((DeclSpace) o).DefineType ();
287 /// Resolves the core types in the compiler when compiling with --nostdlib
289 static public void ResolveCore ()
291 TypeManager.object_type = BootstrapCorlib_ResolveClass (root, "System.Object");
292 TypeManager.system_object_expr.Type = TypeManager.object_type;
293 TypeManager.value_type = BootstrapCorlib_ResolveClass (root, "System.ValueType");
294 TypeManager.system_valuetype_expr.Type = TypeManager.value_type;
295 TypeManager.attribute_type = BootstrapCorlib_ResolveClass (root, "System.Attribute");
296 TypeManager.indexer_name_type = BootstrapCorlib_ResolveClass (root, "System.Runtime.CompilerServices.IndexerNameAttribute");
298 string [] interfaces_first_stage = {
299 "System.IComparable", "System.ICloneable",
300 "System.IConvertible",
302 "System.Collections.IEnumerable",
303 "System.Collections.ICollection",
304 "System.Collections.IEnumerator",
305 "System.Collections.IList",
306 "System.IAsyncResult",
307 "System.IDisposable",
309 "System.Runtime.Serialization.ISerializable",
311 "System.Reflection.IReflect",
312 "System.Reflection.ICustomAttributeProvider"
315 foreach (string iname in interfaces_first_stage)
316 BootstrapCorlib_ResolveInterface (root, iname);
319 // These are the base value types
321 string [] structs_first_stage = {
322 "System.Byte", "System.SByte",
323 "System.Int16", "System.UInt16",
324 "System.Int32", "System.UInt32",
325 "System.Int64", "System.UInt64",
328 foreach (string cname in structs_first_stage)
329 BootstrapCorlib_ResolveStruct (root, cname);
332 // Now, we can load the enumerations, after this point,
335 TypeManager.InitEnumUnderlyingTypes ();
337 string [] structs_second_stage = {
338 "System.Single", "System.Double",
339 "System.Char", "System.Boolean",
340 "System.Decimal", "System.Void",
341 "System.RuntimeFieldHandle",
342 "System.RuntimeArgumentHandle",
343 "System.RuntimeTypeHandle",
345 "System.TypedReference",
349 foreach (string cname in structs_second_stage)
350 BootstrapCorlib_ResolveStruct (root, cname);
353 // These are classes that depends on the core interfaces
355 string [] classes_second_stage = {
359 "System.Reflection.MemberInfo",
364 // These are not really important in the order, but they
365 // are used by the compiler later on (typemanager/CoreLookupType-d)
367 "System.Runtime.CompilerServices.RuntimeHelpers",
368 "System.Reflection.DefaultMemberAttribute",
369 "System.Threading.Monitor",
371 "System.AttributeUsageAttribute",
372 "System.Runtime.InteropServices.DllImportAttribute",
373 "System.Runtime.CompilerServices.MethodImplAttribute",
374 "System.Runtime.InteropServices.MarshalAsAttribute",
375 "System.Diagnostics.ConditionalAttribute",
376 "System.ObsoleteAttribute",
377 "System.ParamArrayAttribute",
378 "System.CLSCompliantAttribute",
379 "System.Security.UnverifiableCodeAttribute",
380 "System.Security.Permissions.SecurityAttribute",
381 "System.Runtime.CompilerServices.DecimalConstantAttribute",
382 "System.Runtime.InteropServices.InAttribute",
383 "System.Runtime.InteropServices.OutAttribute",
384 "System.Runtime.InteropServices.StructLayoutAttribute",
385 "System.Runtime.InteropServices.FieldOffsetAttribute",
386 "System.InvalidOperationException",
387 "System.NotSupportedException",
388 "System.MarshalByRefObject",
389 "System.Security.CodeAccessPermission",
390 "System.Runtime.CompilerServices.RequiredAttributeAttribute",
391 "System.Runtime.InteropServices.GuidAttribute",
392 "System.Reflection.AssemblyCultureAttribute"
395 foreach (string cname in classes_second_stage)
396 BootstrapCorlib_ResolveClass (root, cname);
398 BootstrapCorlib_ResolveDelegate (root, "System.AsyncCallback");
400 // These will be defined indirectly during the previous ResolveDelegate.
401 // However make sure the rest of the checks happen.
402 string [] delegate_types = { "System.Delegate", "System.MulticastDelegate" };
403 foreach (string cname in delegate_types)
404 BootstrapCorlib_ResolveClass (root, cname);
408 // Closes all open types
412 // We usually use TypeBuilder types. When we are done
413 // creating the type (which will happen after we have added
414 // methods, fields, etc) we need to "Define" them before we
415 // can save the Assembly
417 static public void CloseTypes ()
419 if (root.Enums != null)
420 foreach (Enum en in root.Enums)
424 // We do this in two passes, first we close the structs,
425 // then the classes, because it seems the code needs it this
426 // way. If this is really what is going on, we should probably
427 // make sure that we define the structs in order as well.
429 foreach (TypeContainer tc in type_container_resolve_order){
430 if (tc.Kind == Kind.Struct && tc.Parent == root){
435 foreach (TypeContainer tc in type_container_resolve_order){
436 if (!(tc.Kind == Kind.Struct && tc.Parent == root))
440 if (root.Delegates != null)
441 foreach (Delegate d in root.Delegates)
446 // If we have a <PrivateImplementationDetails> class, close it
448 if (helper_classes != null){
449 foreach (TypeBuilder type_builder in helper_classes) {
451 type_builder.SetCustomAttribute (TypeManager.compiler_generated_attr);
453 type_builder.CreateType ();
457 type_container_resolve_order = null;
458 helper_classes = null;
460 TypeManager.CleanUp ();
464 /// Used to register classes that need to be closed after all the
465 /// user defined classes
467 public static void RegisterCompilerGeneratedType (TypeBuilder helper_class)
469 if (helper_classes == null)
470 helper_classes = new ArrayList ();
472 helper_classes.Add (helper_class);
475 static public void PopulateCoreType (TypeContainer root, string name)
477 DeclSpace ds = (DeclSpace) root.GetDefinition (name);
483 static public void BootCorlib_PopulateCoreTypes ()
485 PopulateCoreType (root, "System.Object");
486 PopulateCoreType (root, "System.ValueType");
487 PopulateCoreType (root, "System.Attribute");
488 PopulateCoreType (root, "System.Runtime.CompilerServices.IndexerNameAttribute");
492 // Populates the structs and classes with fields and methods
495 // This is invoked after all interfaces, structs and classes
496 // have been defined through `ResolveTree'
497 static public void PopulateTypes ()
499 if (type_container_resolve_order != null){
500 foreach (TypeContainer tc in type_container_resolve_order)
504 ArrayList delegates = root.Delegates;
505 if (delegates != null){
506 foreach (Delegate d in delegates)
510 ArrayList enums = root.Enums;
512 foreach (Enum en in enums)
517 // Check for cycles in the struct layout
519 if (type_container_resolve_order != null){
520 Hashtable seen = new Hashtable ();
521 foreach (TypeContainer tc in type_container_resolve_order)
522 TypeManager.CheckStructCycles (tc, seen);
527 // A generic hook delegate
529 public delegate void Hook ();
532 // A hook invoked when the code has been generated.
534 public static event Hook EmitCodeHook;
537 // DefineTypes is used to fill in the members of each type.
539 static public void DefineTypes ()
541 ArrayList delegates = root.Delegates;
542 if (delegates != null){
543 foreach (Delegate d in delegates)
547 if (type_container_resolve_order != null){
548 foreach (TypeContainer tc in type_container_resolve_order) {
549 // When compiling corlib, these types have already been
550 // populated from BootCorlib_PopulateCoreTypes ().
551 if (!RootContext.StdLib &&
552 ((tc.Name == "System.Object") ||
553 (tc.Name == "System.Attribute") ||
554 (tc.Name == "System.ValueType") ||
555 (tc.Name == "System.Runtime.CompilerServices.IndexerNameAttribute")))
562 ArrayList enums = root.Enums;
564 foreach (Enum en in enums)
569 static public void EmitCode ()
571 if (root.Enums != null) {
572 foreach (Enum e in root.Enums)
576 if (type_container_resolve_order != null) {
577 foreach (TypeContainer tc in type_container_resolve_order)
580 if (Report.Errors > 0)
583 foreach (TypeContainer tc in type_container_resolve_order)
587 if (root.Delegates != null) {
588 foreach (Delegate d in root.Delegates)
592 // Run any hooks after all the types have been defined.
593 // This is used to create nested auxiliary classes for example
596 if (EmitCodeHook != null)
599 CodeGen.Assembly.Emit (root);
600 CodeGen.Module.Emit (root);
604 // Public Field, used to track which method is the public entry
607 static public MethodInfo EntryPoint;
610 // Track the location of the entry point.
612 static public Location EntryPointLocation;
615 // These are used to generate unique names on the structs and fields.
617 static int field_count;
620 // Makes an initialized struct, returns the field builder that
621 // references the data. Thanks go to Sergey Chaban for researching
622 // how to do this. And coming up with a shorter mechanism than I
623 // was able to figure out.
625 // This works but makes an implicit public struct $ArrayType$SIZE and
626 // makes the fields point to it. We could get more control if we did
629 // 1. DefineNestedType on the impl_details_class with our struct.
631 // 2. Define the field on the impl_details_class
633 static public FieldBuilder MakeStaticData (byte [] data)
637 if (impl_details_class == null){
638 impl_details_class = CodeGen.Module.Builder.DefineType (
639 "<PrivateImplementationDetails>",
640 TypeAttributes.NotPublic,
641 TypeManager.object_type);
643 RegisterCompilerGeneratedType (impl_details_class);
646 fb = impl_details_class.DefineInitializedData (
647 "$$field-" + (field_count++), data,
648 FieldAttributes.Static | FieldAttributes.Assembly);
653 public static void CheckUnsafeOption (Location loc)
656 Report.Error (227, loc,
657 "Unsafe code requires the `unsafe' command line option to be specified");