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)
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
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,
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
61 static TypeBuilder impl_details_class;
63 public static int WarningLevel = 2;
71 interface_resolve_order = new ArrayList ();
72 type_container_resolve_order = new ArrayList ();
75 static public Tree 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;
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
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)
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;
162 foreach (Interface 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)
180 static void Error_TypeConflict (string name, Location loc)
183 520, loc, "`" + name + "' conflicts with a predefined type");
186 static void Error_TypeConflict (string name)
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);
199 Report.Error (518, "The predefined type `" + name + "' is not defined");
205 DeclSpace d = (DeclSpace) o;
207 Error_TypeConflict (name, d.Location);
209 Error_TypeConflict (name);
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);
224 Report.Error (518, "The predefined type `" + name + "' is not defined");
230 DeclSpace d = (DeclSpace) o;
232 Error_TypeConflict (name, d.Location);
234 Error_TypeConflict (name);
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);
249 Report.Error (518, "The predefined type `" + name + "' is not defined");
253 if (!(o is Interface)){
255 DeclSpace d = (DeclSpace) o;
257 Error_TypeConflict (name, d.Location);
259 Error_TypeConflict (name);
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);
274 Report.Error (518, "The predefined type `" + name + "' is not defined");
275 Environment.Exit (0);
278 if (!(o is Delegate)){
279 Error_TypeConflict (name);
283 ((DeclSpace) o).DefineType ();
288 /// Resolves the core types in the compiler when compiling with --nostdlib
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,
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",
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",
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");
390 // Closes all open types
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
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)
409 if (attribute_types != null)
410 foreach (TypeContainer tc in attribute_types)
413 foreach (Interface iface in interface_resolve_order)
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){
428 foreach (TypeContainer tc in type_container_resolve_order){
429 if (!(tc is Struct && tc.Parent == tree.Types))
433 if (root.Delegates != null)
434 foreach (Delegate d in root.Delegates)
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 (".");
458 return ns.Substring (0, i);
461 static Type NamespaceLookup (Namespace curr_ns, string name, Location loc)
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));
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);
483 // Try the aliases in the current namespace
485 string alias = sourceBeingCompiled.LookupAlias (name);
488 t = TypeManager.LookupType (alias);
492 t = TypeManager.LookupType (MakeFQN (alias, name));
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));
506 // Then try with the using clauses
509 ICollection imports_list = sourceBeingCompiled.ImportsTable;
511 if (imports_list == null)
515 foreach (SourceBeingCompiled.ImportsEntry ue in imports_list) {
516 //TODO: deal with standard modules
517 match = TypeManager.LookupType (MakeFQN (ue.Name, name));
520 DeclSpace.Error_AmbiguousTypeReference (loc, name, t, match);
534 string a = sourceBeingCompiled.LookupAlias (name);
536 t = TypeManager.LookupType (a);
540 t = TypeManager.LookupType (MakeFQN (a, name));
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)
561 if (ds.Cache.Contains (name)){
562 t = (Type) ds.Cache [name];
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) {
578 t = TypeManager.LookupType (current_type.FullName + "." + name);
584 current_type = current_type.BaseType;
587 containing_ds = containing_ds.Parent;
590 t = NamespaceLookup (ds.Namespace, name, loc);
598 Report.Error (30002, loc, "Cannot find type `"+name+"'");
604 // This is the silent version of LookupType, you can use this
605 // to `probe' for a type
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)
616 if (tree.Namespaces != null){
617 ns = (Namespace) tree.Namespaces [name];
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);
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");
649 // Populates the structs and classes with fields and methods
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);
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")))
681 if ((tc.ModFlags & Modifiers.NEW) == 0)
682 tc.DefineMembers (root);
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);
694 Report1530 (d.Location);
697 ArrayList enums = root.Enums;
699 foreach (Enum en in enums)
700 if ((en.ModFlags & Modifiers.NEW) == 0)
701 en.DefineMembers (root);
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)
715 if (interface_resolve_order != null){
716 foreach (Interface iface in interface_resolve_order)
717 if ((iface.ModFlags & Modifiers.NEW) == 0)
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")))
732 if ((tc.ModFlags & Modifiers.NEW) == 0)
737 ArrayList delegates = root.Delegates;
738 if (delegates != null){
739 foreach (Delegate d in delegates)
740 if ((d.ModFlags & Modifiers.NEW) == 0)
744 ArrayList enums = root.Enums;
746 foreach (Enum en in enums)
747 if ((en.ModFlags & Modifiers.NEW) == 0)
752 static public void EmitCode ()
754 if (attribute_types != null)
755 foreach (TypeContainer tc in attribute_types)
758 CodeGen.EmitGlobalAttributes ();
760 if (type_container_resolve_order != null) {
761 foreach (TypeContainer tc in type_container_resolve_order)
764 foreach (TypeContainer tc in type_container_resolve_order)
769 if (TypeManager.unverifiable_code_ctor == null) {
770 Console.WriteLine ("Internal error ! Cannot set unverifiable code attribute.");
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
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
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)
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);