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)
6 // Marek Safar (marek.safar@gmail.com)
9 // Licensed under the terms of the GNU GPL
11 // (C) 2001 Ximian, Inc (http://www.ximian.com)
12 // (C) 2004 Novell, Inc
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
36 public class RootContext {
39 // Contains the parsed tree
41 static RootTypes root;
44 // This hashtable contains all of the #definitions across the source code
45 // it is used by the ConditionalAttribute handler.
47 public static Hashtable AllDefines = new Hashtable ();
50 // Whether we are being linked against the standard libraries.
51 // This is only used to tell whether `System.Object' should
52 // have a base class or not.
54 public static bool StdLib;
57 // This keeps track of the order in which classes were defined
58 // so that we can poulate them in that order.
60 // Order is important, because we need to be able to tell, by
61 // examining the list of methods of the base class, which ones are virtual
62 // or abstract as well as the parent names (to implement new,
65 static ArrayList type_container_resolve_order;
68 // Holds a reference to the Private Implementation Details
71 static ArrayList helper_classes;
73 static TypeBuilder impl_details_class;
75 public static Target Target;
76 public static string TargetExt;
78 public static bool VerifyClsCompliance = true;
81 /// Holds /optimize option
83 public static bool Optimize = true;
85 public static LanguageVersion Version;
88 // We keep strongname related info here because
89 // it's also used as complier options from CSC 8.x
91 public static string StrongNameKeyFile;
92 public static string StrongNameKeyContainer;
93 public static bool StrongNameDelaySign;
96 // If set, enable XML documentation generation
98 public static Documentation Documentation;
100 static public string MainClass;
105 static RootContext ()
110 public static void Reset ()
112 root = new RootTypes ();
113 type_container_resolve_order = new ArrayList ();
115 Report.WarningLevel = 3;
119 StrongNameKeyFile = null;
120 StrongNameKeyContainer = null;
121 StrongNameDelaySign = false;
125 Version = LanguageVersion.Default;
126 Documentation = null;
127 impl_details_class = null;
128 helper_classes = null;
131 public static bool NeedsEntryPoint {
132 get { return RootContext.Target == Target.Exe || RootContext.Target == Target.WinExe; }
135 static public RootTypes ToplevelTypes {
139 public static void RegisterOrder (TypeContainer tc)
141 type_container_resolve_order.Add (tc);
145 // The default compiler checked state
147 static public bool Checked;
150 // Whether to allow Unsafe code
152 static public bool Unsafe;
155 // This function is used to resolve the hierarchy tree.
156 // It processes interfaces, structs and classes in that order.
158 // It creates the TypeBuilder's as it processes the user defined
161 static public void ResolveTree ()
164 // Interfaces are processed next, as classes and
165 // structs might inherit from an object or implement
166 // a set of interfaces, we need to be able to tell
167 // them appart by just using the TypeManager.
169 foreach (TypeContainer tc in root.Types)
172 foreach (TypeContainer tc in root.Types)
175 if (root.Delegates != null)
176 foreach (Delegate d in root.Delegates)
180 delegate bool VerifyBootstrapType (Type t);
182 static Type BootstrapCorlib_ResolveType (TypeContainer root, string name, VerifyBootstrapType typeVerifier)
184 TypeLookupExpression tle = new TypeLookupExpression (name);
185 Report.DisableReporting ();
186 TypeExpr te = tle.ResolveAsTypeTerminal (root, false);
187 Report.EnableReporting ();
189 Report.Error (518, "The predefined type `{0}' is not defined or imported", name);
194 if (!typeVerifier (t)) {
195 MemberCore mc = root.GetDefinition (name);
196 Location loc = Location.Null;
198 name = mc.GetSignatureForError ();
202 Report.Error (520, loc, "The predefined type `{0}' is not declared correctly", name);
206 AttributeTester.RegisterNonObsoleteType (t);
210 // Resolves a single class during the corlib bootstrap process
212 static Type BootstrapCorlib_ResolveClass (TypeContainer root, string name)
214 return BootstrapCorlib_ResolveType (root, name, IsClass);
217 static bool IsClass (Type t)
219 DeclSpace ds = TypeManager.LookupDeclSpace (t);
226 // Resolves a struct during the corlib bootstrap process
228 static Type BootstrapCorlib_ResolveStruct (TypeContainer root, string name)
230 return BootstrapCorlib_ResolveType (root, name, IsStruct);
233 static bool IsStruct (Type t)
235 DeclSpace ds = TypeManager.LookupDeclSpace (t);
239 return TypeManager.IsSubclassOf (t, TypeManager.value_type);
243 // Resolves an interface during the corlib bootstrap process
245 static void BootstrapCorlib_ResolveInterface (TypeContainer root, string name)
247 BootstrapCorlib_ResolveType (root, name, IsInterface);
250 static bool IsInterface (Type t)
252 return t.IsInterface;
256 // Resolves a delegate during the corlib bootstrap process
258 static void BootstrapCorlib_ResolveDelegate (TypeContainer root, string name)
260 BootstrapCorlib_ResolveType (root, name, IsDelegate);
263 static bool IsDelegate (Type t)
265 return TypeManager.IsDelegateType (t);
269 /// Resolves the core types in the compiler when compiling with --nostdlib
271 static public void ResolveCore ()
273 TypeManager.object_type = BootstrapCorlib_ResolveClass (root, "System.Object");
274 TypeManager.system_object_expr.Type = TypeManager.object_type;
275 TypeManager.value_type = BootstrapCorlib_ResolveClass (root, "System.ValueType");
276 TypeManager.system_valuetype_expr.Type = TypeManager.value_type;
279 // The core attributes
281 BootstrapCorlib_ResolveInterface (root, "System.Runtime.InteropServices._Attribute");
282 TypeManager.attribute_type = BootstrapCorlib_ResolveClass (root, "System.Attribute");
283 TypeManager.obsolete_attribute_type = BootstrapCorlib_ResolveClass (root, "System.ObsoleteAttribute");
284 if (TypeManager.obsolete_attribute_type != null) {
285 Class c = TypeManager.LookupClass (TypeManager.obsolete_attribute_type);
289 TypeManager.indexer_name_type = BootstrapCorlib_ResolveClass (root, "System.Runtime.CompilerServices.IndexerNameAttribute");
291 string [] interfaces_first_stage = {
292 "System.IComparable", "System.ICloneable",
293 "System.IConvertible",
295 "System.Collections.IEnumerable",
296 "System.Collections.ICollection",
297 "System.Collections.IEnumerator",
298 "System.Collections.IList",
299 "System.IAsyncResult",
300 "System.IDisposable",
302 "System.Runtime.Serialization.ISerializable",
304 "System.Reflection.IReflect",
305 "System.Reflection.ICustomAttributeProvider",
307 "System.Runtime.InteropServices._Exception",
312 "System.Collections.Generic.IEnumerator`1",
313 "System.Collections.Generic.IEnumerable`1"
317 foreach (string iname in interfaces_first_stage)
318 BootstrapCorlib_ResolveInterface (root, iname);
321 // These are the base value types
323 string [] structs_first_stage = {
324 "System.Byte", "System.SByte",
325 "System.Int16", "System.UInt16",
326 "System.Int32", "System.UInt32",
327 "System.Int64", "System.UInt64",
330 foreach (string cname in structs_first_stage)
331 BootstrapCorlib_ResolveStruct (root, cname);
334 // Now, we can load the enumerations, after this point,
337 TypeManager.InitEnumUnderlyingTypes ();
339 string [] structs_second_stage = {
340 "System.Single", "System.Double",
342 "System.Decimal", "System.Void",
343 "System.RuntimeFieldHandle",
344 "System.RuntimeArgumentHandle",
345 "System.RuntimeTypeHandle",
347 "System.TypedReference",
351 TypeManager.bool_type = BootstrapCorlib_ResolveStruct (root, "System.Boolean");
353 foreach (string cname in structs_second_stage)
354 BootstrapCorlib_ResolveStruct (root, cname);
357 // These are classes that depends on the core interfaces
359 string [] classes_second_stage = {
363 "System.Reflection.MemberInfo",
371 // These are not really important in the order, but they
372 // are used by the compiler later on (typemanager/CoreLookupType-d)
374 "System.Runtime.CompilerServices.RuntimeHelpers",
375 "System.Reflection.DefaultMemberAttribute",
376 "System.Threading.Monitor",
377 "System.Threading.Interlocked",
379 "System.AttributeUsageAttribute",
380 "System.Runtime.InteropServices.DllImportAttribute",
381 "System.Runtime.CompilerServices.MethodImplAttribute",
382 "System.Runtime.InteropServices.MarshalAsAttribute",
384 "System.Runtime.CompilerServices.CompilerGeneratedAttribute",
385 "System.Runtime.CompilerServices.FixedBufferAttribute",
387 "System.Diagnostics.ConditionalAttribute",
388 "System.ParamArrayAttribute",
389 "System.CLSCompliantAttribute",
390 "System.Security.UnverifiableCodeAttribute",
391 "System.Security.Permissions.SecurityAttribute",
392 "System.Runtime.CompilerServices.DecimalConstantAttribute",
393 "System.Runtime.InteropServices.InAttribute",
394 "System.Runtime.InteropServices.OutAttribute",
395 "System.Runtime.InteropServices.StructLayoutAttribute",
396 "System.Runtime.InteropServices.FieldOffsetAttribute",
398 "System.Runtime.InteropServices.DefaultCharSetAttribute",
400 "System.InvalidOperationException",
401 "System.NotSupportedException",
402 "System.MarshalByRefObject",
403 "System.Security.CodeAccessPermission",
404 "System.Runtime.CompilerServices.RequiredAttributeAttribute",
405 "System.Runtime.InteropServices.GuidAttribute",
406 "System.Reflection.AssemblyCultureAttribute"
409 foreach (string cname in classes_second_stage)
410 BootstrapCorlib_ResolveClass (root, cname);
412 BootstrapCorlib_ResolveStruct (root, "System.Nullable`1");
414 TypeManager.delegate_type = BootstrapCorlib_ResolveClass (root, "System.Delegate");
415 BootstrapCorlib_ResolveClass (root, "System.MulticastDelegate");
417 BootstrapCorlib_ResolveDelegate (root, "System.AsyncCallback");
421 // Closes all open types
425 // We usually use TypeBuilder types. When we are done
426 // creating the type (which will happen after we have added
427 // methods, fields, etc) we need to "Define" them before we
428 // can save the Assembly
430 static public void CloseTypes ()
433 // We do this in two passes, first we close the structs,
434 // then the classes, because it seems the code needs it this
435 // way. If this is really what is going on, we should probably
436 // make sure that we define the structs in order as well.
438 foreach (TypeContainer tc in type_container_resolve_order){
439 if (tc.Kind == Kind.Struct && tc.Parent == root){
444 foreach (TypeContainer tc in type_container_resolve_order){
445 if (!(tc.Kind == Kind.Struct && tc.Parent == root))
449 if (root.Delegates != null)
450 foreach (Delegate d in root.Delegates)
455 // If we have a <PrivateImplementationDetails> class, close it
457 if (helper_classes != null){
458 foreach (TypeBuilder type_builder in helper_classes) {
460 type_builder.SetCustomAttribute (TypeManager.compiler_generated_attr);
462 type_builder.CreateType ();
466 type_container_resolve_order = null;
467 helper_classes = null;
469 TypeManager.CleanUp ();
473 /// Used to register classes that need to be closed after all the
474 /// user defined classes
476 public static void RegisterCompilerGeneratedType (TypeBuilder helper_class)
478 if (helper_classes == null)
479 helper_classes = new ArrayList ();
481 helper_classes.Add (helper_class);
484 static public void PopulateCoreType (TypeContainer root, string name)
486 DeclSpace ds = (DeclSpace) root.GetDefinition (name);
487 // Core type was imported
495 static public void BootCorlib_PopulateCoreTypes ()
497 PopulateCoreType (root, "System.Object");
498 PopulateCoreType (root, "System.ValueType");
499 PopulateCoreType (root, "System.Attribute");
500 PopulateCoreType (root, "System.Runtime.CompilerServices.IndexerNameAttribute");
504 // Populates the structs and classes with fields and methods
507 // This is invoked after all interfaces, structs and classes
508 // have been defined through `ResolveTree'
509 static public void PopulateTypes ()
512 if (type_container_resolve_order != null){
513 foreach (TypeContainer tc in type_container_resolve_order)
515 foreach (TypeContainer tc in type_container_resolve_order)
519 ArrayList delegates = root.Delegates;
520 if (delegates != null){
521 foreach (Delegate d in delegates)
526 // Check for cycles in the struct layout
528 if (type_container_resolve_order != null){
529 Hashtable seen = new Hashtable ();
530 foreach (TypeContainer tc in type_container_resolve_order)
531 TypeManager.CheckStructCycles (tc, seen);
536 // A generic hook delegate
538 public delegate void Hook ();
541 // A hook invoked when the code has been generated.
543 public static event Hook EmitCodeHook;
546 // DefineTypes is used to fill in the members of each type.
548 static public void DefineTypes ()
550 ArrayList delegates = root.Delegates;
551 if (delegates != null){
552 foreach (Delegate d in delegates)
556 if (type_container_resolve_order != null){
557 foreach (TypeContainer tc in type_container_resolve_order) {
558 // When compiling corlib, these types have already been
559 // populated from BootCorlib_PopulateCoreTypes ().
560 if (!RootContext.StdLib &&
561 ((tc.Name == "System.Object") ||
562 (tc.Name == "System.Attribute") ||
563 (tc.Name == "System.ValueType") ||
564 (tc.Name == "System.Runtime.CompilerServices.IndexerNameAttribute")))
572 static public void EmitCode ()
574 if (type_container_resolve_order != null) {
575 foreach (TypeContainer tc in type_container_resolve_order)
578 if (Report.Errors > 0)
581 foreach (TypeContainer tc in type_container_resolve_order)
585 if (root.Delegates != null) {
586 foreach (Delegate d in root.Delegates)
590 // Run any hooks after all the types have been defined.
591 // This is used to create nested auxiliary classes for example
594 if (EmitCodeHook != null)
597 CodeGen.Assembly.Emit (root);
598 CodeGen.Module.Emit (root);
602 // Public Field, used to track which method is the public entry
605 static public MethodInfo EntryPoint;
608 // Track the location of the entry point.
610 static public Location EntryPointLocation;
613 // These are used to generate unique names on the structs and fields.
615 static int field_count;
618 // Makes an initialized struct, returns the field builder that
619 // references the data. Thanks go to Sergey Chaban for researching
620 // how to do this. And coming up with a shorter mechanism than I
621 // was able to figure out.
623 // This works but makes an implicit public struct $ArrayType$SIZE and
624 // makes the fields point to it. We could get more control if we did
627 // 1. DefineNestedType on the impl_details_class with our struct.
629 // 2. Define the field on the impl_details_class
631 static public FieldBuilder MakeStaticData (byte [] data)
635 if (impl_details_class == null){
636 impl_details_class = CodeGen.Module.Builder.DefineType (
637 "<PrivateImplementationDetails>",
638 TypeAttributes.NotPublic,
639 TypeManager.object_type);
641 RegisterCompilerGeneratedType (impl_details_class);
644 fb = impl_details_class.DefineInitializedData (
645 "$$field-" + (field_count++), data,
646 FieldAttributes.Static | FieldAttributes.Assembly);
651 public static void CheckUnsafeOption (Location loc)
654 Report.Error (227, loc,
655 "Unsafe code requires the `unsafe' command line option to be specified");