(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / mbas / rootcontext.cs
blob63bd527b07cf5a1939ad548e8772ad9247fdddbc
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 //
7 // Licensed under the terms of the GNU GPL
8 //
9 // (C) 2001 Ximian, Inc (http://www.ximian.com)
11 using System;
12 using System.Collections;
13 using System.Reflection;
14 using System.Reflection.Emit;
15 using System.Diagnostics;
17 namespace Mono.MonoBASIC {
19 public class RootContext {
22 // Contains the parsed tree
24 static Tree tree;
27 // Tracks Imported namespaces
29 static SourceBeingCompiled sourceBeingCompiled = new SourceBeingCompiled();
32 // This hashtable contains all of the #definitions across the source code
33 // it is used by the ConditionalAttribute handler.
35 public static Hashtable AllDefines = new Hashtable ();
38 // Whether we are being linked against the standard libraries.
39 // This is only used to tell whether `System.Object' should
40 // have a parent or not.
42 public static bool StdLib = true;
45 // This keeps track of the order in which classes were defined
46 // so that we can poulate them in that order.
48 // Order is important, because we need to be able to tell by
49 // examining the parent's list of methods which ones are virtual
50 // or abstract as well as the parent names (to implement new,
51 // override).
53 static ArrayList type_container_resolve_order;
54 static ArrayList interface_resolve_order;
55 static ArrayList attribute_types;
58 // Holds a reference to the Private Implementation Details
59 // class.
61 static TypeBuilder impl_details_class;
63 public static int WarningLevel = 2;
66 // Constructor
68 static RootContext ()
70 tree = new Tree ();
71 interface_resolve_order = new ArrayList ();
72 type_container_resolve_order = new ArrayList ();
75 static public Tree Tree {
76 get {
77 return tree;
81 static public string MainClass;
83 public static void RegisterOrder (Interface iface)
85 interface_resolve_order.Add (iface);
88 public static void RegisterOrder (TypeContainer tc)
90 type_container_resolve_order.Add (tc);
93 public static void RegisterAttribute (TypeContainer tc)
95 if (attribute_types == null)
96 attribute_types = new ArrayList ();
98 attribute_types.Add (tc);
101 public static void InitializeImports(ArrayList ImportsList)
103 sourceBeingCompiled.InitializeImports (ImportsList);
106 public static SourceBeingCompiled SourceBeingCompiled
108 get { return sourceBeingCompiled; }
111 public static void VerifyImports()
113 sourceBeingCompiled.VerifyImports();
117 // The default compiler checked state
119 static public bool Checked = false;
122 // Whether to allow Unsafe code
124 static public bool Unsafe = false;
126 // Root namespace name (implicit namespace)
127 static public string RootNamespace = "";
129 static string MakeFQN (string nsn, string name)
131 string prefix = (nsn == "" ? "" : nsn + ".");
133 return prefix + name;
136 // <remarks>
137 // This function is used to resolve the hierarchy tree.
138 // It processes interfaces, structs and classes in that order.
140 // It creates the TypeBuilder's as it processes the user defined
141 // types.
142 // </remarks>
143 static public void ResolveTree ()
146 // Process the attribute types separately and before anything else
148 if (attribute_types != null)
149 foreach (TypeContainer tc in attribute_types)
150 tc.DefineType ();
153 // Interfaces are processed next, as classes and
154 // structs might inherit from an object or implement
155 // a set of interfaces, we need to be able to tell
156 // them appart by just using the TypeManager.
158 TypeContainer root = Tree.Types;
160 ArrayList ifaces = root.Interfaces;
161 if (ifaces != null){
162 foreach (Interface i in ifaces)
163 i.DefineType ();
167 foreach (TypeContainer tc in root.Types)
168 tc.DefineType ();
170 if (root.Delegates != null)
171 foreach (Delegate d in root.Delegates)
172 d.DefineType ();
174 if (root.Enums != null)
175 foreach (Enum e in root.Enums)
176 e.DefineType ();
180 static void Error_TypeConflict (string name, Location loc)
182 Report.Error (
183 520, loc, "`" + name + "' conflicts with a predefined type");
186 static void Error_TypeConflict (string name)
188 Report.Error (
189 520, "`" + name + "' conflicts with a predefined type");
193 // Resolves a single class during the corlib bootstrap process
195 static TypeBuilder BootstrapCorlib_ResolveClass (TypeContainer root, string name)
197 object o = root.GetDefinition (name);
198 if (o == null){
199 Report.Error (518, "The predefined type `" + name + "' is not defined");
200 return null;
203 if (!(o is Class)){
204 if (o is DeclSpace){
205 DeclSpace d = (DeclSpace) o;
207 Error_TypeConflict (name, d.Location);
208 } else
209 Error_TypeConflict (name);
211 return null;
214 return ((DeclSpace) o).DefineType ();
218 // Resolves a struct during the corlib bootstrap process
220 static void BootstrapCorlib_ResolveStruct (TypeContainer root, string name)
222 object o = root.GetDefinition (name);
223 if (o == null){
224 Report.Error (518, "The predefined type `" + name + "' is not defined");
225 return;
228 if (!(o is Struct)){
229 if (o is DeclSpace){
230 DeclSpace d = (DeclSpace) o;
232 Error_TypeConflict (name, d.Location);
233 } else
234 Error_TypeConflict (name);
236 return;
239 ((DeclSpace) o).DefineType ();
243 // Resolves a struct during the corlib bootstrap process
245 static void BootstrapCorlib_ResolveInterface (TypeContainer root, string name)
247 object o = root.GetDefinition (name);
248 if (o == null){
249 Report.Error (518, "The predefined type `" + name + "' is not defined");
250 return;
253 if (!(o is Interface)){
254 if (o is DeclSpace){
255 DeclSpace d = (DeclSpace) o;
257 Error_TypeConflict (name, d.Location);
258 } else
259 Error_TypeConflict (name);
261 return;
264 ((DeclSpace) o).DefineType ();
268 // Resolves a delegate during the corlib bootstrap process
270 static void BootstrapCorlib_ResolveDelegate (TypeContainer root, string name)
272 object o = root.GetDefinition (name);
273 if (o == null){
274 Report.Error (518, "The predefined type `" + name + "' is not defined");
275 Environment.Exit (0);
278 if (!(o is Delegate)){
279 Error_TypeConflict (name);
280 return;
283 ((DeclSpace) o).DefineType ();
287 /// <summary>
288 /// Resolves the core types in the compiler when compiling with --nostdlib
289 /// </summary>
290 static public void ResolveCore ()
292 TypeContainer root = Tree.Types;
294 TypeManager.object_type = BootstrapCorlib_ResolveClass (root, "System.Object");
295 TypeManager.value_type = BootstrapCorlib_ResolveClass (root, "System.ValueType");
296 TypeManager.attribute_type = BootstrapCorlib_ResolveClass (root, "System.Attribute");
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,
333 // we can use enums.
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.RuntimeTypeHandle",
343 "System.IntPtr"
346 foreach (string cname in structs_second_stage)
347 BootstrapCorlib_ResolveStruct (root, cname);
350 // These are classes that depends on the core interfaces
352 string [] classes_second_stage = {
353 "System.Reflection.MemberInfo",
354 "System.Type",
355 "System.Exception",
358 // These are not really important in the order, but they
359 // are used by the compiler later on (typemanager/CoreLookupType-d)
361 "System.Runtime.CompilerServices.RuntimeHelpers",
362 "System.Reflection.DefaultMemberAttribute",
363 "System.Threading.Monitor",
365 "System.AttributeUsageAttribute",
366 "System.Runtime.InteropServices.DllImportAttribute",
367 "System.Runtime.CompilerServices.MethodImplAttribute",
368 "System.Runtime.InteropServices.MarshalAsAttribute",
369 "System.Diagnostics.ConditionalAttribute",
370 "System.ObsoleteAttribute",
371 "System.ParamArrayAttribute",
372 "System.Security.UnverifiableCodeAttribute",
373 "System.Runtime.CompilerServices.IndexerNameAttribute",
376 // We must store them here before calling BootstrapCorlib_ResolveDelegate.
377 TypeManager.string_type = BootstrapCorlib_ResolveClass (root, "System.String");
378 TypeManager.enum_type = BootstrapCorlib_ResolveClass (root, "System.Enum");
379 TypeManager.array_type = BootstrapCorlib_ResolveClass (root, "System.Array");
380 TypeManager.multicast_delegate_type = BootstrapCorlib_ResolveClass (root, "System.MulticastDelegate");
381 TypeManager.delegate_type = BootstrapCorlib_ResolveClass (root, "System.Delegate");
383 foreach (string cname in classes_second_stage)
384 BootstrapCorlib_ResolveClass (root, cname);
386 BootstrapCorlib_ResolveDelegate (root, "System.AsyncCallback");
389 // <summary>
390 // Closes all open types
391 // </summary>
393 // <remarks>
394 // We usually use TypeBuilder types. When we are done
395 // creating the type (which will happen after we have added
396 // methods, fields, etc) we need to "Define" them before we
397 // can save the Assembly
398 // </remarks>
399 static public void CloseTypes ()
401 TypeContainer root = Tree.Types;
403 ArrayList ifaces = root.Interfaces;
405 if (root.Enums != null)
406 foreach (Enum en in root.Enums)
407 en.CloseType ();
409 if (attribute_types != null)
410 foreach (TypeContainer tc in attribute_types)
411 tc.CloseType ();
413 foreach (Interface iface in interface_resolve_order)
414 iface.CloseType ();
417 // We do this in two passes, first we close the structs,
418 // then the classes, because it seems the code needs it this
419 // way. If this is really what is going on, we should probably
420 // make sure that we define the structs in order as well.
422 foreach (TypeContainer tc in type_container_resolve_order){
423 if (tc is Struct && tc.Parent == tree.Types){
424 tc.CloseType ();
428 foreach (TypeContainer tc in type_container_resolve_order){
429 if (!(tc is Struct && tc.Parent == tree.Types))
430 tc.CloseType ();
433 if (root.Delegates != null)
434 foreach (Delegate d in root.Delegates)
435 d.CloseType ();
439 // If we have a <PrivateImplementationDetails> class, close it
441 if (impl_details_class != null){
442 impl_details_class.CreateType ();
447 // This idea is from Felix Arrese-Igor
449 // Returns : the implicit parent of a composite namespace string
450 // eg. Implicit parent of A.B is A
452 static public string ImplicitParent (string ns)
454 int i = ns.LastIndexOf (".");
455 if (i < 0)
456 return null;
458 return ns.Substring (0, i);
461 static Type NamespaceLookup (Namespace curr_ns, string name, Location loc)
463 Type t;
466 // Try in the current namespace and all its implicit parents
468 for (string ns = curr_ns.Name; ns != null; ns = ImplicitParent (ns)) {
469 t = TypeManager.LookupType (MakeFQN (ns, name));
470 if (t != null)
471 return t;
475 // It's possible that name already is fully qualified. So we do
476 // a simple direct lookup without adding any namespace names
478 t = TypeManager.LookupType (name);
479 if (t != null)
480 return t;
483 // Try the aliases in the current namespace
485 string alias = sourceBeingCompiled.LookupAlias (name);
487 if (alias != null) {
488 t = TypeManager.LookupType (alias);
489 if (t != null)
490 return t;
492 t = TypeManager.LookupType (MakeFQN (alias, name));
493 if (t != null)
494 return t;
497 for (Namespace ns = curr_ns; ns != null; ns = ns.Parent) {
499 // Look in the namespace ns
501 t = TypeManager.LookupType (MakeFQN (ns.Name, name));
502 if (t != null)
503 return t;
506 // Then try with the using clauses
509 ICollection imports_list = sourceBeingCompiled.ImportsTable;
511 if (imports_list == null)
512 continue;
514 Type match = null;
515 foreach (SourceBeingCompiled.ImportsEntry ue in imports_list) {
516 //TODO: deal with standard modules
517 match = TypeManager.LookupType (MakeFQN (ue.Name, name));
518 if (match != null){
519 if (t != null){
520 DeclSpace.Error_AmbiguousTypeReference (loc, name, t, match);
521 return null;
524 t = match;
525 ue.Used = true;
528 if (t != null)
529 return t;
532 // Try with aliases
534 string a = sourceBeingCompiled.LookupAlias (name);
535 if (a != null) {
536 t = TypeManager.LookupType (a);
537 if (t != null)
538 return t;
540 t = TypeManager.LookupType (MakeFQN (a, name));
541 if (t != null)
542 return t;
546 return null;
550 // Public function used to locate types, this can only
551 // be used after the ResolveTree function has been invoked.
553 // Returns: Type or null if they type can not be found.
555 // Come to think of it, this should be a DeclSpace
557 static public Type LookupType (DeclSpace ds, string name, bool silent, Location loc)
559 Type t;
561 if (ds.Cache.Contains (name)){
562 t = (Type) ds.Cache [name];
563 if (t != null)
564 return t;
565 } else {
567 // For the case the type we are looking for is nested within this one
568 // or is in any base class
570 DeclSpace containing_ds = ds;
571 while (containing_ds != null){
572 Type current_type = containing_ds.TypeBuilder;
574 while (current_type != null) {
576 // nested class
578 t = TypeManager.LookupType (current_type.FullName + "." + name);
579 if (t != null){
580 ds.Cache [name] = t;
581 return t;
584 current_type = current_type.BaseType;
587 containing_ds = containing_ds.Parent;
590 t = NamespaceLookup (ds.Namespace, name, loc);
591 if (t != null){
592 ds.Cache [name] = t;
593 return t;
597 if (!silent)
598 Report.Error (30002, loc, "Cannot find type `"+name+"'");
600 return null;
603 // <summary>
604 // This is the silent version of LookupType, you can use this
605 // to `probe' for a type
606 // </summary>
607 static public Type LookupType (TypeContainer tc, string name, Location loc)
609 return LookupType (tc, name, true, loc);
612 static public bool IsNamespace (string name)
614 Namespace ns;
616 if (tree.Namespaces != null){
617 ns = (Namespace) tree.Namespaces [name];
619 if (ns != null)
620 return true;
623 return false;
626 static void Report1530 (Location loc)
628 Report.Error (1530, loc, "Keyword new not allowed for namespace elements");
631 static public void PopulateCoreType (TypeContainer root, string name)
633 DeclSpace ds = (DeclSpace) root.GetDefinition (name);
635 ds.DefineMembers (root);
636 ds.Define (root);
639 static public void BootCorlib_PopulateCoreTypes ()
641 TypeContainer root = tree.Types;
643 PopulateCoreType (root, "System.Object");
644 PopulateCoreType (root, "System.ValueType");
645 PopulateCoreType (root, "System.Attribute");
648 // <summary>
649 // Populates the structs and classes with fields and methods
650 // </summary>
652 // This is invoked after all interfaces, structs and classes
653 // have been defined through `ResolveTree'
654 static public void PopulateTypes ()
656 TypeContainer root = Tree.Types;
658 if (attribute_types != null)
659 foreach (TypeContainer tc in attribute_types)
660 tc.DefineMembers (root);
662 if (interface_resolve_order != null){
663 foreach (Interface iface in interface_resolve_order)
664 if ((iface.ModFlags & Modifiers.NEW) == 0)
665 iface.DefineMembers (root);
666 else
667 Report1530 (iface.Location);
671 if (type_container_resolve_order != null){
672 foreach (TypeContainer tc in type_container_resolve_order) {
673 // When compiling corlib, these types have already been
674 // populated from BootCorlib_PopulateCoreTypes ().
675 if (!RootContext.StdLib &&
676 ((tc.Name == "System.Object") ||
677 (tc.Name == "System.Attribute") ||
678 (tc.Name == "System.ValueType")))
679 continue;
681 if ((tc.ModFlags & Modifiers.NEW) == 0)
682 tc.DefineMembers (root);
683 else
684 Report1530 (tc.Location);
688 ArrayList delegates = root.Delegates;
689 if (delegates != null){
690 foreach (Delegate d in delegates)
691 if ((d.ModFlags & Modifiers.NEW) == 0)
692 d.DefineMembers (root);
693 else
694 Report1530 (d.Location);
697 ArrayList enums = root.Enums;
698 if (enums != null){
699 foreach (Enum en in enums)
700 if ((en.ModFlags & Modifiers.NEW) == 0)
701 en.DefineMembers (root);
702 else
703 Report1530 (en.Location);
707 static public void DefineTypes ()
709 TypeContainer root = Tree.Types;
711 if (attribute_types != null)
712 foreach (TypeContainer tc in attribute_types)
713 tc.Define (root);
715 if (interface_resolve_order != null){
716 foreach (Interface iface in interface_resolve_order)
717 if ((iface.ModFlags & Modifiers.NEW) == 0)
718 iface.Define (root);
722 if (type_container_resolve_order != null){
723 foreach (TypeContainer tc in type_container_resolve_order) {
724 // When compiling corlib, these types have already been
725 // populated from BootCorlib_PopulateCoreTypes ().
726 if (!RootContext.StdLib &&
727 ((tc.Name == "System.Object") ||
728 (tc.Name == "System.Attribute") ||
729 (tc.Name == "System.ValueType")))
730 continue;
732 if ((tc.ModFlags & Modifiers.NEW) == 0)
733 tc.Define (root);
737 ArrayList delegates = root.Delegates;
738 if (delegates != null){
739 foreach (Delegate d in delegates)
740 if ((d.ModFlags & Modifiers.NEW) == 0)
741 d.Define (root);
744 ArrayList enums = root.Enums;
745 if (enums != null){
746 foreach (Enum en in enums)
747 if ((en.ModFlags & Modifiers.NEW) == 0)
748 en.Define (root);
752 static public void EmitCode ()
754 if (attribute_types != null)
755 foreach (TypeContainer tc in attribute_types)
756 tc.Emit ();
758 CodeGen.EmitGlobalAttributes ();
760 if (type_container_resolve_order != null) {
761 foreach (TypeContainer tc in type_container_resolve_order)
762 tc.EmitConstants ();
764 foreach (TypeContainer tc in type_container_resolve_order)
765 tc.Emit ();
768 if (Unsafe) {
769 if (TypeManager.unverifiable_code_ctor == null) {
770 Console.WriteLine ("Internal error ! Cannot set unverifiable code attribute.");
771 return;
774 CustomAttributeBuilder cb = new CustomAttributeBuilder (TypeManager.unverifiable_code_ctor, new object [0]);
775 CodeGen.ModuleBuilder.SetCustomAttribute (cb);
780 // Public Field, used to track which method is the public entry
781 // point.
783 static public MethodInfo EntryPoint;
786 // Track the location of the entry point.
788 static public Location EntryPointLocation;
791 // These are used to generate unique names on the structs and fields.
793 static int field_count;
796 // Makes an initialized struct, returns the field builder that
797 // references the data. Thanks go to Sergey Chaban for researching
798 // how to do this. And coming up with a shorter mechanism than I
799 // was able to figure out.
801 // This works but makes an implicit public struct $ArrayType$SIZE and
802 // makes the fields point to it. We could get more control if we did
803 // use instead:
805 // 1. DefineNestedType on the impl_details_class with our struct.
807 // 2. Define the field on the impl_details_class
809 static public FieldBuilder MakeStaticData (byte [] data)
811 FieldBuilder fb;
812 int size = data.Length;
814 if (impl_details_class == null)
815 impl_details_class = CodeGen.ModuleBuilder.DefineType (
816 "<PrivateImplementationDetails>", TypeAttributes.NotPublic, TypeManager.object_type);
818 fb = impl_details_class.DefineInitializedData (
819 "$$field-" + (field_count++), data,
820 FieldAttributes.Static | FieldAttributes.Assembly);
822 return fb;