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