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