8cca3ed46c42f845a1b9ac42c0b3b86458e0d653
[mono.git] / mcs / mcs / rootcontext.cs
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)
10
11 using System;
12 using System.Collections;
13 using System.Reflection;
14 using System.Reflection.Emit;
15 using System.Diagnostics;
16
17 namespace Mono.CSharp {
18
19         public class RootContext {
20
21                 //
22                 // Contains the parsed tree
23                 //
24                 static Tree tree;
25
26                 //
27                 // This hashtable contains all of the #definitions across the source code
28                 // it is used by the ConditionalAttribute handler.
29                 //
30                 public static Hashtable AllDefines = new Hashtable ();
31                 
32                 //
33                 // The list of global attributes (those that target the assembly)
34                 //
35                 static Hashtable global_attributes = new Hashtable ();
36                 
37                 //
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.
41                 //
42                 public static bool StdLib = true;
43
44                 //
45                 // This keeps track of the order in which classes were defined
46                 // so that we can poulate them in that order.
47                 //
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).
52                 //
53                 static ArrayList type_container_resolve_order;
54                 static ArrayList interface_resolve_order;
55                 static ArrayList attribute_types;
56
57                 //
58                 // Holds a reference to the Private Implementation Details
59                 // class.
60                 //
61                 static ArrayList helper_classes;
62                 
63                 static TypeBuilder impl_details_class;
64
65                 public static int WarningLevel = 2;
66
67                 //
68                 // If set, enable C# version 2 features
69                 //
70                 public static bool V2;
71                 //
72                 // Constructor
73                 //
74                 static RootContext ()
75                 {
76                         tree = new Tree ();
77                         interface_resolve_order = new ArrayList ();
78                         type_container_resolve_order = new ArrayList ();
79                 }
80
81                 static public Tree Tree {
82                         get {
83                                 return tree;
84                         }
85                 }
86
87                 static public string MainClass;
88                 
89                 public static void RegisterOrder (Interface iface)
90                 {
91                         interface_resolve_order.Add (iface);
92                 }
93                 
94                 public static void RegisterOrder (TypeContainer tc)
95                 {
96                         type_container_resolve_order.Add (tc);
97                 }
98
99                 public static void RegisterAttribute (TypeContainer tc)
100                 {
101                         if (attribute_types == null)
102                                 attribute_types = new ArrayList ();
103                         
104                         attribute_types.Add (tc);
105                 }
106                 
107                 // 
108                 // The default compiler checked state
109                 //
110                 static public bool Checked = false;
111
112                 //
113                 // Whether to allow Unsafe code
114                 //
115                 static public bool Unsafe = false;
116                 
117                 static string MakeFQN (string nsn, string name)
118                 {
119                         if (nsn == "")
120                                 return name;
121                         return String.Concat (nsn, ".", name);
122                 }
123
124                 // <remarks>
125                 //   This function is used to resolve the hierarchy tree.
126                 //   It processes interfaces, structs and classes in that order.
127                 //
128                 //   It creates the TypeBuilder's as it processes the user defined
129                 //   types.  
130                 // </remarks>
131                 static public void ResolveTree ()
132                 {
133                         //
134                         // Process the attribute types separately and before anything else
135                         //
136                         if (attribute_types != null)
137                                 foreach (TypeContainer tc in attribute_types)
138                                         tc.DefineType ();
139                         
140                         //
141                         // Interfaces are processed next, as classes and
142                         // structs might inherit from an object or implement
143                         // a set of interfaces, we need to be able to tell
144                         // them appart by just using the TypeManager.
145                         //
146                         TypeContainer root = Tree.Types;
147
148                         ArrayList ifaces = root.Interfaces;
149                         if (ifaces != null){
150                                 foreach (Interface i in ifaces) 
151                                         i.DefineType ();
152                         }
153
154                         foreach (TypeContainer tc in root.Types)
155                                 tc.DefineType ();
156
157                         if (root.Delegates != null)
158                                 foreach (Delegate d in root.Delegates) 
159                                         d.DefineType ();
160
161                         if (root.Enums != null)
162                                 foreach (Enum e in root.Enums)
163                                         e.DefineType ();
164                 }
165
166                 static void Error_TypeConflict (string name, Location loc)
167                 {
168                         Report.Error (
169                                 520, loc, "`" + name + "' conflicts with a predefined type");
170                 }
171
172                 static void Error_TypeConflict (string name)
173                 {
174                         Report.Error (
175                                 520, "`" + name + "' conflicts with a predefined type");
176                 }
177
178                 //
179                 // Resolves a single class during the corlib bootstrap process
180                 //
181                 static TypeBuilder BootstrapCorlib_ResolveClass (TypeContainer root, string name)
182                 {
183                         object o = root.GetDefinition (name);
184                         if (o == null){
185                                 Report.Error (518, "The predefined type `" + name + "' is not defined");
186                                 return null;
187                         }
188
189                         if (!(o is Class)){
190                                 if (o is DeclSpace){
191                                         DeclSpace d = (DeclSpace) o;
192
193                                         Error_TypeConflict (name, d.Location);
194                                 } else
195                                         Error_TypeConflict (name);
196
197                                 return null;
198                         }
199
200                         return ((DeclSpace) o).DefineType ();
201                 }
202
203                 //
204                 // Resolves a struct during the corlib bootstrap process
205                 //
206                 static void BootstrapCorlib_ResolveStruct (TypeContainer root, string name)
207                 {
208                         object o = root.GetDefinition (name);
209                         if (o == null){
210                                 Report.Error (518, "The predefined type `" + name + "' is not defined");
211                                 return;
212                         }
213
214                         if (!(o is Struct)){
215                                 if (o is DeclSpace){
216                                         DeclSpace d = (DeclSpace) o;
217
218                                         Error_TypeConflict (name, d.Location);
219                                 } else
220                                         Error_TypeConflict (name);
221
222                                 return;
223                         }
224
225                         ((DeclSpace) o).DefineType ();
226                 }
227
228                 //
229                 // Resolves a struct during the corlib bootstrap process
230                 //
231                 static void BootstrapCorlib_ResolveInterface (TypeContainer root, string name)
232                 {
233                         object o = root.GetDefinition (name);
234                         if (o == null){
235                                 Report.Error (518, "The predefined type `" + name + "' is not defined");
236                                 return;
237                         }
238
239                         if (!(o is Interface)){
240                                 if (o is DeclSpace){
241                                         DeclSpace d = (DeclSpace) o;
242
243                                         Error_TypeConflict (name, d.Location);
244                                 } else
245                                         Error_TypeConflict (name);
246
247                                 return;
248                         }
249
250                         ((DeclSpace) o).DefineType ();
251                 }
252
253                 //
254                 // Resolves a delegate during the corlib bootstrap process
255                 //
256                 static void BootstrapCorlib_ResolveDelegate (TypeContainer root, string name)
257                 {
258                         object o = root.GetDefinition (name);
259                         if (o == null){
260                                 Report.Error (518, "The predefined type `" + name + "' is not defined");
261                                 Environment.Exit (0);
262                         }
263
264                         if (!(o is Delegate)){
265                                 Error_TypeConflict (name);
266                                 return;
267                         }
268
269                         ((DeclSpace) o).DefineType ();
270                 }
271                 
272
273                 /// <summary>
274                 ///    Resolves the core types in the compiler when compiling with --nostdlib
275                 /// </summary>
276                 static public void ResolveCore ()
277                 {
278                         TypeContainer root = Tree.Types;
279
280                         TypeManager.object_type = BootstrapCorlib_ResolveClass (root, "System.Object");
281                         TypeManager.value_type = BootstrapCorlib_ResolveClass (root, "System.ValueType");
282                         TypeManager.attribute_type = BootstrapCorlib_ResolveClass (root, "System.Attribute");
283                         
284                         string [] interfaces_first_stage = {
285                                 "System.IComparable", "System.ICloneable",
286                                 "System.IConvertible",
287                                 
288                                 "System.Collections.IEnumerable",
289                                 "System.Collections.ICollection",
290                                 "System.Collections.IEnumerator",
291                                 "System.Collections.IList", 
292                                 "System.IAsyncResult",
293                                 "System.IDisposable",
294                                 
295                                 "System.Runtime.Serialization.ISerializable",
296
297                                 "System.Reflection.IReflect",
298                                 "System.Reflection.ICustomAttributeProvider"
299                         };
300
301                         foreach (string iname in interfaces_first_stage)
302                                 BootstrapCorlib_ResolveInterface (root, iname);
303
304                         //
305                         // These are the base value types
306                         //
307                         string [] structs_first_stage = {
308                                 "System.Byte",    "System.SByte",
309                                 "System.Int16",   "System.UInt16",
310                                 "System.Int32",   "System.UInt32",
311                                 "System.Int64",   "System.UInt64",
312                         };
313
314                         foreach (string cname in structs_first_stage)
315                                 BootstrapCorlib_ResolveStruct (root, cname);
316
317                         //
318                         // Now, we can load the enumerations, after this point,
319                         // we can use enums.
320                         //
321                         TypeManager.InitEnumUnderlyingTypes ();
322
323                         string [] structs_second_stage = {
324                                 "System.Single",  "System.Double",
325                                 "System.Char",    "System.Boolean",
326                                 "System.Decimal", "System.Void",
327                                 "System.RuntimeFieldHandle",
328                                 "System.RuntimeTypeHandle",
329                                 "System.IntPtr"
330                         };
331                         
332                         foreach (string cname in structs_second_stage)
333                                 BootstrapCorlib_ResolveStruct (root, cname);
334                         
335                         //
336                         // These are classes that depends on the core interfaces
337                         //
338                         string [] classes_second_stage = {
339                                 "System.Reflection.MemberInfo",
340                                 "System.Type",
341                                 "System.Exception",
342
343                                 //
344                                 // These are not really important in the order, but they
345                                 // are used by the compiler later on (typemanager/CoreLookupType-d)
346                                 //
347                                 "System.Runtime.CompilerServices.RuntimeHelpers",
348                                 "System.Reflection.DefaultMemberAttribute",
349                                 "System.Threading.Monitor",
350                                 
351                                 "System.AttributeUsageAttribute",
352                                 "System.Runtime.InteropServices.DllImportAttribute",
353                                 "System.Runtime.CompilerServices.MethodImplAttribute",
354                                 "System.Runtime.InteropServices.MarshalAsAttribute",
355                                 "System.Diagnostics.ConditionalAttribute",
356                                 "System.ObsoleteAttribute",
357                                 "System.ParamArrayAttribute",
358                                 "System.Security.UnverifiableCodeAttribute",
359                                 "System.Runtime.CompilerServices.IndexerNameAttribute",
360                                 "System.Runtime.InteropServices.InAttribute",
361                                 "System.InvalidOperationException"
362
363                         };
364
365                         // We must store them here before calling BootstrapCorlib_ResolveDelegate.
366                         TypeManager.string_type = BootstrapCorlib_ResolveClass (root, "System.String");
367                         TypeManager.enum_type = BootstrapCorlib_ResolveClass (root, "System.Enum");
368                         TypeManager.array_type = BootstrapCorlib_ResolveClass (root, "System.Array");
369                         TypeManager.multicast_delegate_type = BootstrapCorlib_ResolveClass (root, "System.MulticastDelegate");
370                         TypeManager.delegate_type = BootstrapCorlib_ResolveClass (root, "System.Delegate");
371                         
372                         foreach (string cname in classes_second_stage)
373                                 BootstrapCorlib_ResolveClass (root, cname);
374
375                         BootstrapCorlib_ResolveDelegate (root, "System.AsyncCallback");
376                 }
377                         
378                 // <summary>
379                 //   Closes all open types
380                 // </summary>
381                 //
382                 // <remarks>
383                 //   We usually use TypeBuilder types.  When we are done
384                 //   creating the type (which will happen after we have added
385                 //   methods, fields, etc) we need to "Define" them before we
386                 //   can save the Assembly
387                 // </remarks>
388                 static public void CloseTypes ()
389                 {
390                         TypeContainer root = Tree.Types;
391                         
392                         ArrayList ifaces = root.Interfaces;
393
394                         if (root.Enums != null)
395                                 foreach (Enum en in root.Enums)
396                                         en.CloseType ();
397
398                         if (attribute_types != null)
399                                 foreach (TypeContainer tc in attribute_types)
400                                         tc.CloseType ();
401                         
402                         foreach (Interface iface in interface_resolve_order)
403                                 iface.CloseType ();
404
405                         //
406                         // We do this in two passes, first we close the structs,
407                         // then the classes, because it seems the code needs it this
408                         // way.  If this is really what is going on, we should probably
409                         // make sure that we define the structs in order as well.
410                         //
411                         foreach (TypeContainer tc in type_container_resolve_order){
412                                 if (tc is Struct && tc.Parent == tree.Types){
413                                         tc.CloseType ();
414                                 }
415                         }
416
417                         foreach (TypeContainer tc in type_container_resolve_order){
418                                 if (!(tc is Struct && tc.Parent == tree.Types))
419                                         tc.CloseType ();                                        
420                         }
421                         
422                         if (root.Delegates != null)
423                                 foreach (Delegate d in root.Delegates)
424                                         d.CloseType ();
425
426
427                         //
428                         // If we have a <PrivateImplementationDetails> class, close it
429                         //
430                         if (helper_classes != null){
431                                 foreach (TypeBuilder type_builder in helper_classes)
432                                         type_builder.CreateType ();
433                         }
434                 }
435
436                 /// <summary>
437                 ///   Used to register classes that need to be closed after all the
438                 ///   user defined classes
439                 /// </summary>
440                 public static void RegisterHelperClass (TypeBuilder helper_class)
441                 {
442                         if (helper_classes == null)
443                                 helper_classes = new ArrayList ();
444                         helper_classes.Add (helper_class);
445                 }
446                 
447                 //
448                 // This idea is from Felix Arrese-Igor
449                 //
450                 // Returns : the implicit parent of a composite namespace string
451                 //   eg. Implicit parent of A.B is A
452                 //
453                 static public string ImplicitParent (string ns)
454                 {
455                         int i = ns.LastIndexOf (".");
456                         if (i < 0)
457                                 return null;
458                         
459                         return ns.Substring (0, i);
460                 }
461
462                 static Type NamespaceLookup (NamespaceEntry curr_ns, string name, Location loc)
463                 {
464                         Type t;
465
466                         //
467                         // Try in the current namespace and all its implicit parents
468                         //
469                         for (string ns = curr_ns.Name; ns != null; ns = ImplicitParent (ns)) {
470                                 t = TypeManager.LookupType (MakeFQN (ns, name));
471                                 if (t != null)
472                                         return t;
473                         }
474                         
475                         //
476                         // It's possible that name already is fully qualified. So we do
477                         // a simple direct lookup without adding any namespace names
478                         //
479                         t = TypeManager.LookupType (name); 
480                         if (t != null)
481                                 return t;
482
483                         //
484                         // Try the aliases in the current namespace
485                         //
486                         string alias = curr_ns.LookupAlias (name);
487
488                         if (alias != null) {
489                                 t = TypeManager.LookupType (alias);
490                                 if (t != null)
491                                         return t;
492
493                                 t = TypeManager.LookupType (MakeFQN (alias, name));
494                                 if (t != null)
495                                         return t;
496                         }
497                         
498                         for (NamespaceEntry ns = curr_ns; ns != null; ns = ns.Parent) {
499                                 //
500                                 // Look in the namespace ns
501                                 //
502                                 t = TypeManager.LookupType (MakeFQN (ns.Name, name));
503                                 if (t != null)
504                                         return t;
505                                 
506                                 //
507                                 // Then try with the using clauses
508                                 //
509                                 Type match = null;
510                                 foreach (Namespace using_ns in ns.GetUsingTable ()) {
511                                         string full_name = DeclSpace.MakeFQN (using_ns.Name, name);
512                                         match = TypeManager.LookupType (full_name);
513                                         if (match != null){
514                                                 if (t != null){
515                                                         DeclSpace.Error_AmbiguousTypeReference (loc, name, t, match);
516                                                         return null;
517                                                 }
518
519                                                 t = match;
520                                         }
521                                 }
522
523                                 if (t != null)
524                                         return t;
525
526                                 //
527                                 // Try with aliases
528                                 //
529                                 string a = ns.LookupAlias (name);
530                                 if (a != null) {
531                                         t = TypeManager.LookupType (a);
532                                         if (t != null)
533                                                 return t;
534
535                                         t = TypeManager.LookupType (MakeFQN (a, name));
536                                         if (t != null)
537                                                 return t;
538                                 }
539                         }
540
541                         return null;
542                 }
543                 
544                 //
545                 // Public function used to locate types, this can only
546                 // be used after the ResolveTree function has been invoked.
547                 //
548                 // Returns: Type or null if they type can not be found.
549                 //
550                 // Come to think of it, this should be a DeclSpace
551                 //
552                 static public Type LookupType (DeclSpace ds, string name, bool silent, Location loc)
553                 {
554                         Type t;
555
556                         if (ds.Cache.Contains (name)){
557                                 t = (Type) ds.Cache [name];
558                                 if (t != null)
559                                         return t;
560                         } else {
561                                 //
562                                 // For the case the type we are looking for is nested within this one
563                                 // or is in any base class
564                                 //
565                                 DeclSpace containing_ds = ds;
566                                 while (containing_ds != null){
567                                         Type current_type = containing_ds.TypeBuilder;
568                                         
569                                         while (current_type != null) {
570                                                 //
571                                                 // nested class
572                                                 //
573                                                 t = TypeManager.LookupType (current_type.FullName + "." + name);
574                                                 if (t != null){
575                                                         ds.Cache [name] = t;
576                                                         return t;
577                                                 }
578                                                 
579                                                 current_type = current_type.BaseType;
580                                         }
581                                         
582                                         containing_ds = containing_ds.Parent;
583                                 }
584                                 
585                                 t = NamespaceLookup (ds.Namespace, name, loc);
586                                 if (t != null){
587                                         ds.Cache [name] = t;
588                                         return t;
589                                 }
590                         }
591
592                         if (!silent)
593                                 Report.Error (246, loc, "Cannot find type `"+name+"'");
594                         
595                         return null;
596                 }
597
598                 // <summary>
599                 //   This is the silent version of LookupType, you can use this
600                 //   to `probe' for a type
601                 // </summary>
602                 static public Type LookupType (TypeContainer tc, string name, Location loc)
603                 {
604                         return LookupType (tc, name, true, loc);
605                 }
606
607                 static public bool IsNamespace (string name)
608                 {
609                         Namespace ns;
610
611                         if (tree.Namespaces != null){
612                                 ns = (Namespace) tree.Namespaces [name];
613
614                                 if (ns != null)
615                                         return true;
616                         }
617
618                         return false;
619                 }
620
621                 static void Report1530 (Location loc)
622                 {
623                         Report.Error (1530, loc, "Keyword new not allowed for namespace elements");
624                 }
625                 
626                 static public void PopulateCoreType (TypeContainer root, string name)
627                 {
628                         DeclSpace ds = (DeclSpace) root.GetDefinition (name);
629
630                         ds.DefineMembers (root);
631                         ds.Define (root);
632                 }
633                 
634                 static public void BootCorlib_PopulateCoreTypes ()
635                 {
636                         TypeContainer root = tree.Types;
637
638                         PopulateCoreType (root, "System.Object");
639                         PopulateCoreType (root, "System.ValueType");
640                         PopulateCoreType (root, "System.Attribute");
641                 }
642                 
643                 // <summary>
644                 //   Populates the structs and classes with fields and methods
645                 // </summary>
646                 //
647                 // This is invoked after all interfaces, structs and classes
648                 // have been defined through `ResolveTree' 
649                 static public void PopulateTypes ()
650                 {
651                         TypeContainer root = Tree.Types;
652
653                         if (attribute_types != null)
654                                 foreach (TypeContainer tc in attribute_types)
655                                         tc.DefineMembers (root);
656                         
657                         if (interface_resolve_order != null){
658                                 foreach (Interface iface in interface_resolve_order)
659                                         if ((iface.ModFlags & Modifiers.NEW) == 0)
660                                                 iface.DefineMembers (root);
661                                         else
662                                                 Report1530 (iface.Location);
663                         }
664
665
666                         if (type_container_resolve_order != null){
667                                 if (RootContext.StdLib){
668                                         foreach (TypeContainer tc in type_container_resolve_order)
669                                                 tc.DefineMembers (root);
670                                 } else {
671                                         foreach (TypeContainer tc in type_container_resolve_order) {
672                                                 // When compiling corlib, these types have already been
673                                                 // populated from BootCorlib_PopulateCoreTypes ().
674                                                 if (((tc.Name == "System.Object") ||
675                                                      (tc.Name == "System.Attribute") ||
676                                                      (tc.Name == "System.ValueType")))
677                                                 continue;
678
679                                                 tc.DefineMembers (root);
680                                         }
681                                 } 
682                         }
683
684                         ArrayList delegates = root.Delegates;
685                         if (delegates != null){
686                                 foreach (Delegate d in delegates)
687                                         if ((d.ModFlags & Modifiers.NEW) == 0)
688                                                 d.DefineMembers (root);
689                                         else
690                                                 Report1530 (d.Location);
691                         }
692
693                         ArrayList enums = root.Enums;
694                         if (enums != null){
695                                 foreach (Enum en in enums)
696                                         if ((en.ModFlags & Modifiers.NEW) == 0)
697                                                 en.DefineMembers (root);
698                                         else
699                                                 Report1530 (en.Location);
700                         }
701                 }
702
703                 //
704                 // A generic hook delegate
705                 //
706                 public delegate void Hook ();
707
708                 //
709                 // A hook invoked when the code has been generated.
710                 //
711                 public static event Hook EmitCodeHook;
712
713                 //
714                 // DefineTypes is used to fill in the members of each type.
715                 //
716                 static public void DefineTypes ()
717                 {
718                         TypeContainer root = Tree.Types;
719
720                         if (attribute_types != null)
721                                 foreach (TypeContainer tc in attribute_types)
722                                         tc.Define (root);
723                         
724                         if (interface_resolve_order != null){
725                                 foreach (Interface iface in interface_resolve_order)
726                                         if ((iface.ModFlags & Modifiers.NEW) == 0)
727                                                 iface.Define (root);
728                         }
729
730
731                         if (type_container_resolve_order != null){
732                                 foreach (TypeContainer tc in type_container_resolve_order) {
733                                         // When compiling corlib, these types have already been
734                                         // populated from BootCorlib_PopulateCoreTypes ().
735                                         if (!RootContext.StdLib &&
736                                             ((tc.Name == "System.Object") ||
737                                              (tc.Name == "System.Attribute") ||
738                                              (tc.Name == "System.ValueType")))
739                                                 continue;
740
741                                         if ((tc.ModFlags & Modifiers.NEW) == 0)
742                                                 tc.Define (root);
743                                 }
744                         }
745
746                         ArrayList delegates = root.Delegates;
747                         if (delegates != null){
748                                 foreach (Delegate d in delegates)
749                                         if ((d.ModFlags & Modifiers.NEW) == 0)
750                                                 d.Define (root);
751                         }
752
753                         ArrayList enums = root.Enums;
754                         if (enums != null){
755                                 foreach (Enum en in enums)
756                                         if ((en.ModFlags & Modifiers.NEW) == 0)
757                                                 en.Define (root);
758                         }
759                 }
760
761                 static public void EmitCode ()
762                 {
763                         //
764                         // Because of the strange way in which we do things, global
765                         // attributes must be processed first.
766                         //
767                         if (global_attributes.Count > 0){
768                                 AssemblyBuilder ab = CodeGen.AssemblyBuilder;
769                                 TypeContainer dummy = new TypeContainer (null, "", new Location (-1));
770                                 EmitContext temp_ec = new EmitContext (
771                                         dummy, Mono.CSharp.Location.Null, null, null, 0, false);
772                         
773                                 foreach (DictionaryEntry de in global_attributes){
774                                         NamespaceEntry ns = (NamespaceEntry) de.Key;
775                                         Attributes attrs = (Attributes) de.Value;
776                                         
777                                         dummy.Namespace = ns;
778                                         Attribute.ApplyAttributes (temp_ec, ab, ab, attrs);
779                                 }
780                         }
781
782                         if (attribute_types != null)
783                                 foreach (TypeContainer tc in attribute_types)
784                                         tc.Emit ();
785                         
786                         if (type_container_resolve_order != null) {
787                                 foreach (TypeContainer tc in type_container_resolve_order)
788                                         tc.EmitConstants ();
789                                 
790                                 foreach (TypeContainer tc in type_container_resolve_order)
791                                         tc.Emit ();
792                         }
793                         
794                         //
795                         // Run any hooks after all the types have been defined.
796                         // This is used to create nested auxiliary classes for example
797                         //
798
799                         if (EmitCodeHook != null)
800                                 EmitCodeHook ();
801
802                         
803                         if (Unsafe) {
804                                 if (TypeManager.unverifiable_code_ctor == null) {
805                                         Console.WriteLine ("Internal error ! Cannot set unverifiable code attribute.");
806                                         return;
807                                 }
808                                 
809                                 CustomAttributeBuilder cb = new CustomAttributeBuilder (TypeManager.unverifiable_code_ctor,
810                                                                                         new object [0]);
811                                 CodeGen.ModuleBuilder.SetCustomAttribute (cb);
812                         }
813                 }
814                 
815                 //
816                 // Public Field, used to track which method is the public entry
817                 // point.
818                 //
819                 static public MethodInfo EntryPoint;
820
821                 //
822                 // Track the location of the entry point.
823                 //
824                 static public Location EntryPointLocation;
825
826                 //
827                 // These are used to generate unique names on the structs and fields.
828                 //
829                 static int field_count;
830                 
831                 //
832                 // Makes an initialized struct, returns the field builder that
833                 // references the data.  Thanks go to Sergey Chaban for researching
834                 // how to do this.  And coming up with a shorter mechanism than I
835                 // was able to figure out.
836                 //
837                 // This works but makes an implicit public struct $ArrayType$SIZE and
838                 // makes the fields point to it.  We could get more control if we did
839                 // use instead:
840                 //
841                 // 1. DefineNestedType on the impl_details_class with our struct.
842                 //
843                 // 2. Define the field on the impl_details_class
844                 //
845                 static public FieldBuilder MakeStaticData (byte [] data)
846                 {
847                         FieldBuilder fb;
848                         int size = data.Length;
849                         
850                         if (impl_details_class == null){
851                                 impl_details_class = CodeGen.ModuleBuilder.DefineType (
852                                         "<PrivateImplementationDetails>", TypeAttributes.NotPublic, TypeManager.object_type);
853                                 RegisterHelperClass (impl_details_class);
854                         }
855
856                         fb = impl_details_class.DefineInitializedData (
857                                 "$$field-" + (field_count++), data,
858                                 FieldAttributes.Static | FieldAttributes.Assembly);
859                         
860                         return fb;
861                 }
862
863                 //
864                 // Adds a global attribute that was declared in `container', 
865                 // the attribute is in `attr', and it was defined at `loc'
866                 //
867                 static public void AddGlobalAttributeSection (TypeContainer container, AttributeSection attr)
868                 {
869                         NamespaceEntry ns = container.Namespace;
870                         Attributes a = (Attributes) global_attributes [ns];
871
872                         if (a == null)
873                                 global_attributes [ns] = new Attributes (attr);
874                         else
875                                 a.AddAttributeSection (attr);
876                 }
877         }
878 }
879               
880