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 using Microsoft
.VisualBasic
;
20 namespace Mono
.CSharp
{
22 public enum LanguageVersion
28 public class RootContext
{
31 // Contains the parsed tree
36 // This hashtable contains all of the #definitions across the source code
37 // it is used by the ConditionalAttribute handler.
39 public static Hashtable AllDefines
= new Hashtable ();
42 // Whether we are being linked against the standard libraries.
43 // This is only used to tell whether `System.Object' should
44 // have a base class or not.
46 public static bool StdLib
= true;
49 // This keeps track of the order in which classes were defined
50 // so that we can poulate them in that order.
52 // Order is important, because we need to be able to tell, by
53 // examining the list of methods of the base class, which ones are virtual
54 // or abstract as well as the parent names (to implement new,
57 static ArrayList type_container_resolve_order
;
58 static ArrayList attribute_types
;
61 // Holds a reference to the Private Implementation Details
64 static ArrayList helper_classes
;
66 static TypeBuilder impl_details_class
;
68 public static int WarningLevel
= 3;
70 public static Target Target
= Target
.Exe
;
71 public static string TargetExt
= ".exe";
73 public static bool VerifyClsCompliance
= true;
75 public static LanguageVersion Version
= LanguageVersion
.Default
;
78 // We keep strongname related info here because
79 // it's also used as complier options from CSC 8.x
81 public static string StrongNameKeyFile
;
82 public static string StrongNameKeyContainer
;
83 public static bool StrongNameDelaySign
= false;
86 // If set, enable XML documentation generation
88 public static Documentation Documentation
;
96 type_container_resolve_order
= new ArrayList ();
99 public static bool NeedsEntryPoint
{
101 return RootContext
.Target
== Target
.Exe
|| RootContext
.Target
== Target
.WinExe
;
105 static public Tree Tree
{
111 static public string MainClass
;
113 public static void RegisterOrder (TypeContainer tc
)
115 type_container_resolve_order
.Add (tc
);
118 public static void RegisterAttribute (TypeContainer tc
)
120 if (attribute_types
== null)
121 attribute_types
= new ArrayList ();
123 attribute_types
.Add (tc
);
127 // The default compiler checked state. It's "On" in case of VB.NET compiler.
129 static public bool Checked
= true;
133 // Whether to allow Unsafe code
135 static public bool Unsafe
= false;
138 // VB.NET specific compiler options
142 // The default type checking state
144 static public bool StricterTypeChecking
= false;
147 // The default character comparison mode
150 static public CompareMethod StringComparisonMode
= CompareMethod
.Binary
;
152 static string MakeFQN (string nsn
, string name
)
156 return String
.Concat (nsn
, ".", name
);
160 // This function is used to resolve the hierarchy tree.
161 // It processes interfaces, structs and classes in that order.
163 // It creates the TypeBuilder's as it processes the user defined
166 static public void ResolveTree ()
169 // Process the attribute types separately and before anything else
171 if (attribute_types
!= null)
172 foreach (TypeContainer tc
in attribute_types
)
176 // Interfaces are processed next, as classes and
177 // structs might inherit from an object or implement
178 // a set of interfaces, we need to be able to tell
179 // them appart by just using the TypeManager.
181 TypeContainer root
= Tree
.Types
;
183 ArrayList ifaces
= root
.Interfaces
;
185 foreach (Interface i
in ifaces
)
189 foreach (TypeContainer tc
in root
.Types
)
192 if (root
.Delegates
!= null)
193 foreach (Delegate d
in root
.Delegates
)
196 if (root
.Enums
!= null)
197 foreach (Enum e
in root
.Enums
)
201 static void Error_TypeConflict (string name
, Location loc
)
204 520, loc
, "`" + name
+ "' conflicts with a predefined type");
207 static void Error_TypeConflict (string name
)
210 520, "`" + name
+ "' conflicts with a predefined type");
214 // Resolves a single class during the corlib bootstrap process
216 static TypeBuilder
BootstrapCorlib_ResolveClass (TypeContainer root
, string name
)
218 object o
= root
.GetDefinition (name
);
220 Report
.Error (518, "The predefined type `" + name
+ "' is not defined");
226 DeclSpace d
= (DeclSpace
) o
;
228 Error_TypeConflict (name
, d
.Location
);
230 Error_TypeConflict (name
);
235 return ((DeclSpace
) o
).DefineType ();
239 // Resolves a struct during the corlib bootstrap process
241 static void BootstrapCorlib_ResolveStruct (TypeContainer root
, string name
)
243 object o
= root
.GetDefinition (name
);
245 Report
.Error (518, "The predefined type `" + name
+ "' is not defined");
251 DeclSpace d
= (DeclSpace
) o
;
253 Error_TypeConflict (name
, d
.Location
);
255 Error_TypeConflict (name
);
260 ((DeclSpace
) o
).DefineType ();
264 // Resolves a struct during the corlib bootstrap process
266 static void BootstrapCorlib_ResolveInterface (TypeContainer root
, string name
)
268 object o
= root
.GetDefinition (name
);
270 Report
.Error (518, "The predefined type `" + name
+ "' is not defined");
274 if (!(o
is Interface
)){
276 DeclSpace d
= (DeclSpace
) o
;
278 Error_TypeConflict (name
, d
.Location
);
280 Error_TypeConflict (name
);
285 ((DeclSpace
) o
).DefineType ();
289 // Resolves a delegate during the corlib bootstrap process
291 static void BootstrapCorlib_ResolveDelegate (TypeContainer root
, string name
)
293 object o
= root
.GetDefinition (name
);
295 Report
.Error (518, "The predefined type `" + name
+ "' is not defined");
296 Environment
.Exit (1);
299 if (!(o
is Delegate
)){
300 Error_TypeConflict (name
);
304 ((DeclSpace
) o
).DefineType ();
309 /// Resolves the core types in the compiler when compiling with --nostdlib
311 static public void ResolveCore ()
313 TypeContainer root
= Tree
.Types
;
315 TypeManager
.object_type
= BootstrapCorlib_ResolveClass (root
, "System.Object");
316 TypeManager
.value_type
= BootstrapCorlib_ResolveClass (root
, "System.ValueType");
317 TypeManager
.attribute_type
= BootstrapCorlib_ResolveClass (root
, "System.Attribute");
319 string [] interfaces_first_stage
= {
320 "System.IComparable", "System.ICloneable",
321 "System.IConvertible",
323 "System.Collections.IEnumerable",
324 "System.Collections.ICollection",
325 "System.Collections.IEnumerator",
326 "System.Collections.IList",
327 "System.IAsyncResult",
328 "System.IDisposable",
330 "System.Runtime.Serialization.ISerializable",
331 "System.Runtime.InteropServices._Exception",
333 "System.Reflection.IReflect",
334 "System.Reflection.ICustomAttributeProvider",
339 "System.Collections.Generic.IEnumerator`1",
340 "System.Collections.Generic.IEnumerable`1",
343 foreach (string iname
in interfaces_first_stage
)
344 BootstrapCorlib_ResolveInterface (root
, iname
);
347 // These are the base value types
349 string [] structs_first_stage
= {
350 "System.Byte", "System.SByte",
351 "System.Int16", "System.UInt16",
352 "System.Int32", "System.UInt32",
353 "System.Int64", "System.UInt64",
356 foreach (string cname
in structs_first_stage
)
357 BootstrapCorlib_ResolveStruct (root
, cname
);
360 // Now, we can load the enumerations, after this point,
363 TypeManager
.InitEnumUnderlyingTypes ();
365 string [] structs_second_stage
= {
366 "System.Single", "System.Double",
367 "System.Char", "System.Boolean",
368 "System.Decimal", "System.Void",
369 "System.RuntimeFieldHandle",
370 "System.RuntimeArgumentHandle",
371 "System.RuntimeTypeHandle",
373 "System.TypedReference",
377 foreach (string cname
in structs_second_stage
)
378 BootstrapCorlib_ResolveStruct (root
, cname
);
381 // These are classes that depends on the core interfaces
383 string [] classes_second_stage
= {
387 "System.Reflection.MemberInfo",
393 // These are not really important in the order, but they
394 // are used by the compiler later on (typemanager/CoreLookupType-d)
396 "System.Runtime.CompilerServices.RuntimeHelpers",
397 "System.Reflection.DefaultMemberAttribute",
398 "System.Threading.Monitor",
400 "System.AttributeUsageAttribute",
401 "System.Runtime.InteropServices.DllImportAttribute",
402 "System.Runtime.CompilerServices.MethodImplAttribute",
403 "System.Runtime.InteropServices.MarshalAsAttribute",
404 "System.Runtime.CompilerServices.NewConstraintAttribute",
405 "System.Diagnostics.ConditionalAttribute",
406 "System.ObsoleteAttribute",
407 "System.ParamArrayAttribute",
408 "System.CLSCompliantAttribute",
409 "System.Security.UnverifiableCodeAttribute",
410 "System.Security.Permissions.SecurityAttribute",
411 "System.Runtime.CompilerServices.IndexerNameAttribute",
412 "System.Runtime.CompilerServices.DecimalConstantAttribute",
413 "System.Runtime.InteropServices.InAttribute",
414 "System.Runtime.InteropServices.OutAttribute",
415 "System.Runtime.InteropServices.StructLayoutAttribute",
416 "System.Runtime.InteropServices.FieldOffsetAttribute",
417 "System.InvalidOperationException",
418 "System.NotSupportedException",
419 "System.MarshalByRefObject",
420 "System.Security.CodeAccessPermission"
423 foreach (string cname
in classes_second_stage
)
424 BootstrapCorlib_ResolveClass (root
, cname
);
426 BootstrapCorlib_ResolveStruct (root
, "System.Nullable`1");
428 BootstrapCorlib_ResolveDelegate (root
, "System.AsyncCallback");
430 // These will be defined indirectly during the previous ResolveDelegate.
431 // However make sure the rest of the checks happen.
432 string [] delegate_types
= { "System.Delegate", "System.MulticastDelegate" }
;
433 foreach (string cname
in delegate_types
)
434 BootstrapCorlib_ResolveClass (root
, cname
);
438 // Closes all open types
442 // We usually use TypeBuilder types. When we are done
443 // creating the type (which will happen after we have added
444 // methods, fields, etc) we need to "Define" them before we
445 // can save the Assembly
447 static public void CloseTypes ()
449 TypeContainer root
= Tree
.Types
;
451 if (root
.Enums
!= null)
452 foreach (Enum en
in root
.Enums
)
455 if (attribute_types
!= null)
456 foreach (TypeContainer tc
in attribute_types
)
460 // We do this in two passes, first we close the structs,
461 // then the classes, because it seems the code needs it this
462 // way. If this is really what is going on, we should probably
463 // make sure that we define the structs in order as well.
465 foreach (TypeContainer tc
in type_container_resolve_order
){
466 if (tc
.Kind
== Kind
.Struct
&& tc
.Parent
== tree
.Types
){
471 foreach (TypeContainer tc
in type_container_resolve_order
){
472 if (!(tc
.Kind
== Kind
.Struct
&& tc
.Parent
== tree
.Types
))
476 if (root
.Delegates
!= null)
477 foreach (Delegate d
in root
.Delegates
)
482 // If we have a <PrivateImplementationDetails> class, close it
484 if (helper_classes
!= null){
485 foreach (TypeBuilder type_builder
in helper_classes
)
486 type_builder
.CreateType ();
489 attribute_types
= null;
490 type_container_resolve_order
= null;
491 helper_classes
= null;
493 TypeManager
.CleanUp ();
497 /// Used to register classes that need to be closed after all the
498 /// user defined classes
500 public static void RegisterHelperClass (TypeBuilder helper_class
)
502 if (helper_classes
== null)
503 helper_classes
= new ArrayList ();
504 helper_classes
.Add (helper_class
);
507 static void Report1530 (Location loc
)
509 Report
.Error (1530, loc
, "Keyword new not allowed for namespace elements");
512 static public void PopulateCoreType (TypeContainer root
, string name
)
514 DeclSpace ds
= (DeclSpace
) root
.GetDefinition (name
);
516 ds
.DefineMembers (root
);
520 static public void BootCorlib_PopulateCoreTypes ()
522 TypeContainer root
= tree
.Types
;
524 PopulateCoreType (root
, "System.Object");
525 PopulateCoreType (root
, "System.ValueType");
526 PopulateCoreType (root
, "System.Attribute");
530 // Populates the structs and classes with fields and methods
533 // This is invoked after all interfaces, structs and classes
534 // have been defined through `ResolveTree'
535 static public void PopulateTypes ()
537 TypeContainer root
= Tree
.Types
;
539 if (attribute_types
!= null)
540 foreach (TypeContainer tc
in attribute_types
)
541 tc
.DefineMembers (root
);
543 if (type_container_resolve_order
!= null){
544 if (RootContext
.StdLib
){
545 foreach (TypeContainer tc
in type_container_resolve_order
)
546 tc
.DefineMembers (root
);
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 (((tc
.Name
== "System.Object") ||
552 (tc
.Name
== "System.Attribute") ||
553 (tc
.Name
== "System.ValueType")))
556 tc
.DefineMembers (root
);
561 ArrayList delegates
= root
.Delegates
;
562 if (delegates
!= null){
563 foreach (Delegate d
in delegates
)
564 if ((d
.ModFlags
& Modifiers
.NEW
) == 0)
565 d
.DefineMembers (root
);
567 Report1530 (d
.Location
);
570 ArrayList enums
= root
.Enums
;
572 foreach (Enum en
in enums
)
573 if ((en
.ModFlags
& Modifiers
.NEW
) == 0)
574 en
.DefineMembers (root
);
576 Report1530 (en
.Location
);
580 // Check for cycles in the struct layout
582 if (type_container_resolve_order
!= null){
583 Hashtable seen
= new Hashtable ();
584 foreach (TypeContainer tc
in type_container_resolve_order
)
585 TypeManager
.CheckStructCycles (tc
, seen
);
590 // A generic hook delegate
592 public delegate void Hook ();
595 // A hook invoked when the code has been generated.
597 public static event Hook EmitCodeHook
;
600 // DefineTypes is used to fill in the members of each type.
602 static public void DefineTypes ()
604 TypeContainer root
= Tree
.Types
;
606 if (attribute_types
!= null)
607 foreach (TypeContainer tc
in attribute_types
)
610 if (type_container_resolve_order
!= null){
611 foreach (TypeContainer tc
in type_container_resolve_order
) {
612 // When compiling corlib, these types have already been
613 // populated from BootCorlib_PopulateCoreTypes ().
614 if (!RootContext
.StdLib
&&
615 ((tc
.Name
== "System.Object") ||
616 (tc
.Name
== "System.Attribute") ||
617 (tc
.Name
== "System.ValueType")))
620 if ((tc
.ModFlags
& Modifiers
.NEW
) == 0)
625 ArrayList delegates
= root
.Delegates
;
626 if (delegates
!= null){
627 foreach (Delegate d
in delegates
)
628 if ((d
.ModFlags
& Modifiers
.NEW
) == 0)
632 ArrayList enums
= root
.Enums
;
634 foreach (Enum en
in enums
)
635 if ((en
.ModFlags
& Modifiers
.NEW
) == 0)
640 static public void EmitCode ()
642 if (attribute_types
!= null)
643 foreach (TypeContainer tc
in attribute_types
)
646 CodeGen
.Assembly
.Emit (Tree
.Types
);
647 CodeGen
.Module
.Emit (Tree
.Types
);
649 if (Tree
.Types
.Enums
!= null) {
650 foreach (Enum e
in Tree
.Types
.Enums
)
654 if (type_container_resolve_order
!= null) {
655 foreach (TypeContainer tc
in type_container_resolve_order
)
659 if (Tree
.Types
.Delegates
!= null) {
660 foreach (Delegate d
in Tree
.Types
.Delegates
)
664 // Run any hooks after all the types have been defined.
665 // This is used to create nested auxiliary classes for example
668 if (EmitCodeHook
!= null)
673 // Public Field, used to track which method is the public entry
676 static public MethodInfo EntryPoint
;
679 // Track the location of the entry point.
681 static public Location EntryPointLocation
;
684 // These are used to generate unique names on the structs and fields.
686 static int field_count
;
689 // Makes an initialized struct, returns the field builder that
690 // references the data. Thanks go to Sergey Chaban for researching
691 // how to do this. And coming up with a shorter mechanism than I
692 // was able to figure out.
694 // This works but makes an implicit public struct $ArrayType$SIZE and
695 // makes the fields point to it. We could get more control if we did
698 // 1. DefineNestedType on the impl_details_class with our struct.
700 // 2. Define the field on the impl_details_class
702 static public FieldBuilder
MakeStaticData (byte [] data
)
706 if (impl_details_class
== null){
707 impl_details_class
= CodeGen
.Module
.Builder
.DefineType (
708 "<PrivateImplementationDetails>",
709 TypeAttributes
.NotPublic
,
710 TypeManager
.object_type
);
712 RegisterHelperClass (impl_details_class
);
715 fb
= impl_details_class
.DefineInitializedData (
716 "$$field-" + (field_count
++), data
,
717 FieldAttributes
.Static
| FieldAttributes
.Assembly
);