2003-09-22 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / gmcs / 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 (DeclSpace ds, string name, Location loc)
463                 {
464                         Type t;
465
466                         //
467                         // Try in the current namespace and all its implicit parents
468                         //
469                         for (NamespaceEntry ns = ds.NamespaceEntry; ns != null; ns = ns.ImplicitParent) {
470                                 object result = ns.Lookup (ds, name, loc);
471                                 if (result == null)
472                                         continue;
473
474                                 if (result is Type)
475                                         return (Type) result;
476
477                                 return null;
478                         }
479
480                         return null;
481                 }
482                 
483                 //
484                 // Public function used to locate types, this can only
485                 // be used after the ResolveTree function has been invoked.
486                 //
487                 // Returns: Type or null if they type can not be found.
488                 //
489                 // Come to think of it, this should be a DeclSpace
490                 //
491                 static public Type LookupType (DeclSpace ds, string name, bool silent, Location loc)
492                 {
493                         Type t;
494
495                         if (ds.Cache.Contains (name)){
496                                 t = (Type) ds.Cache [name];
497                                 if (t != null)
498                                         return t;
499                         } else {
500                                 //
501                                 // For the case the type we are looking for is nested within this one
502                                 // or is in any base class
503                                 //
504                                 DeclSpace containing_ds = ds;
505                                 while (containing_ds != null){
506                                         Type current_type = containing_ds.TypeBuilder;
507                                         
508                                         while (current_type != null) {
509                                                 //
510                                                 // nested class
511                                                 //
512                                                 t = TypeManager.LookupType (current_type.FullName + "." + name);
513                                                 if (t != null){
514                                                         ds.Cache [name] = t;
515                                                         return t;
516                                                 }
517                                                 
518                                                 current_type = current_type.BaseType;
519                                         }
520                                         
521                                         containing_ds = containing_ds.Parent;
522                                 }
523                                 
524                                 t = NamespaceLookup (ds, name, loc);
525                                 if (t != null){
526                                         ds.Cache [name] = t;
527                                         return t;
528                                 }
529                         }
530
531                         if (!silent)
532                                 Report.Error (246, loc, "Cannot find type `"+name+"'");
533                         
534                         return null;
535                 }
536
537                 // <summary>
538                 //   This is the silent version of LookupType, you can use this
539                 //   to `probe' for a type
540                 // </summary>
541                 static public Type LookupType (TypeContainer tc, string name, Location loc)
542                 {
543                         return LookupType (tc, name, true, loc);
544                 }
545
546                 static public bool IsNamespace (string name)
547                 {
548                         Namespace ns;
549
550                         if (tree.Namespaces != null){
551                                 ns = (Namespace) tree.Namespaces [name];
552
553                                 if (ns != null)
554                                         return true;
555                         }
556
557                         return false;
558                 }
559
560                 static void Report1530 (Location loc)
561                 {
562                         Report.Error (1530, loc, "Keyword new not allowed for namespace elements");
563                 }
564                 
565                 static public void PopulateCoreType (TypeContainer root, string name)
566                 {
567                         DeclSpace ds = (DeclSpace) root.GetDefinition (name);
568
569                         ds.DefineMembers (root);
570                         ds.Define (root);
571                 }
572                 
573                 static public void BootCorlib_PopulateCoreTypes ()
574                 {
575                         TypeContainer root = tree.Types;
576
577                         PopulateCoreType (root, "System.Object");
578                         PopulateCoreType (root, "System.ValueType");
579                         PopulateCoreType (root, "System.Attribute");
580                 }
581                 
582                 // <summary>
583                 //   Populates the structs and classes with fields and methods
584                 // </summary>
585                 //
586                 // This is invoked after all interfaces, structs and classes
587                 // have been defined through `ResolveTree' 
588                 static public void PopulateTypes ()
589                 {
590                         TypeContainer root = Tree.Types;
591
592                         if (attribute_types != null)
593                                 foreach (TypeContainer tc in attribute_types)
594                                         tc.DefineMembers (root);
595                         
596                         if (interface_resolve_order != null){
597                                 foreach (Interface iface in interface_resolve_order)
598                                         if ((iface.ModFlags & Modifiers.NEW) == 0)
599                                                 iface.DefineMembers (root);
600                                         else
601                                                 Report1530 (iface.Location);
602                         }
603
604
605                         if (type_container_resolve_order != null){
606                                 if (RootContext.StdLib){
607                                         foreach (TypeContainer tc in type_container_resolve_order)
608                                                 tc.DefineMembers (root);
609                                 } else {
610                                         foreach (TypeContainer tc in type_container_resolve_order) {
611                                                 // When compiling corlib, these types have already been
612                                                 // populated from BootCorlib_PopulateCoreTypes ().
613                                                 if (((tc.Name == "System.Object") ||
614                                                      (tc.Name == "System.Attribute") ||
615                                                      (tc.Name == "System.ValueType")))
616                                                 continue;
617
618                                                 tc.DefineMembers (root);
619                                         }
620                                 } 
621                         }
622
623                         ArrayList delegates = root.Delegates;
624                         if (delegates != null){
625                                 foreach (Delegate d in delegates)
626                                         if ((d.ModFlags & Modifiers.NEW) == 0)
627                                                 d.DefineMembers (root);
628                                         else
629                                                 Report1530 (d.Location);
630                         }
631
632                         ArrayList enums = root.Enums;
633                         if (enums != null){
634                                 foreach (Enum en in enums)
635                                         if ((en.ModFlags & Modifiers.NEW) == 0)
636                                                 en.DefineMembers (root);
637                                         else
638                                                 Report1530 (en.Location);
639                         }
640                 }
641
642                 //
643                 // A generic hook delegate
644                 //
645                 public delegate void Hook ();
646
647                 //
648                 // A hook invoked when the code has been generated.
649                 //
650                 public static event Hook EmitCodeHook;
651
652                 //
653                 // DefineTypes is used to fill in the members of each type.
654                 //
655                 static public void DefineTypes ()
656                 {
657                         TypeContainer root = Tree.Types;
658
659                         if (attribute_types != null)
660                                 foreach (TypeContainer tc in attribute_types)
661                                         tc.Define (root);
662                         
663                         if (interface_resolve_order != null){
664                                 foreach (Interface iface in interface_resolve_order)
665                                         if ((iface.ModFlags & Modifiers.NEW) == 0)
666                                                 iface.Define (root);
667                         }
668
669
670                         if (type_container_resolve_order != null){
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 (!RootContext.StdLib &&
675                                             ((tc.Name == "System.Object") ||
676                                              (tc.Name == "System.Attribute") ||
677                                              (tc.Name == "System.ValueType")))
678                                                 continue;
679
680                                         if ((tc.ModFlags & Modifiers.NEW) == 0)
681                                                 tc.Define (root);
682                                 }
683                         }
684
685                         ArrayList delegates = root.Delegates;
686                         if (delegates != null){
687                                 foreach (Delegate d in delegates)
688                                         if ((d.ModFlags & Modifiers.NEW) == 0)
689                                                 d.Define (root);
690                         }
691
692                         ArrayList enums = root.Enums;
693                         if (enums != null){
694                                 foreach (Enum en in enums)
695                                         if ((en.ModFlags & Modifiers.NEW) == 0)
696                                                 en.Define (root);
697                         }
698                 }
699
700                 static public void EmitCode ()
701                 {
702                         //
703                         // Because of the strange way in which we do things, global
704                         // attributes must be processed first.
705                         //
706                         if (global_attributes.Count > 0){
707                                 AssemblyBuilder ab = CodeGen.AssemblyBuilder;
708                                 TypeContainer dummy = new TypeContainer (null, null, "", new Location (-1));
709                                 EmitContext temp_ec = new EmitContext (
710                                         dummy, Mono.CSharp.Location.Null, null, null, 0, false);
711                         
712                                 foreach (DictionaryEntry de in global_attributes){
713                                         NamespaceEntry ns = (NamespaceEntry) de.Key;
714                                         Attributes attrs = (Attributes) de.Value;
715                                         
716                                         dummy.NamespaceEntry = ns;
717                                         Attribute.ApplyAttributes (temp_ec, ab, ab, attrs);
718                                 }
719                         }
720                         
721                         if (attribute_types != null)
722                                 foreach (TypeContainer tc in attribute_types)
723                                         tc.Emit ();
724
725                         if (interface_resolve_order != null){
726                                 foreach (Interface iface in interface_resolve_order)
727                                         iface.Emit (Tree.Types);
728                         }                        
729                         
730                         if (type_container_resolve_order != null) {
731                                 foreach (TypeContainer tc in type_container_resolve_order)
732                                         tc.EmitConstants ();
733                                 
734                                 foreach (TypeContainer tc in type_container_resolve_order)
735                                         tc.Emit ();
736                         }
737                         
738                         //
739                         // Run any hooks after all the types have been defined.
740                         // This is used to create nested auxiliary classes for example
741                         //
742
743                         if (EmitCodeHook != null)
744                                 EmitCodeHook ();
745
746                         
747                         if (Unsafe) {
748                                 if (TypeManager.unverifiable_code_ctor == null) {
749                                         Console.WriteLine ("Internal error ! Cannot set unverifiable code attribute.");
750                                         return;
751                                 }
752                                 
753                                 CustomAttributeBuilder cb = new CustomAttributeBuilder (TypeManager.unverifiable_code_ctor,
754                                                                                         new object [0]);
755                                 CodeGen.ModuleBuilder.SetCustomAttribute (cb);
756                         }
757                 }
758                 
759                 //
760                 // Public Field, used to track which method is the public entry
761                 // point.
762                 //
763                 static public MethodInfo EntryPoint;
764
765                 //
766                 // Track the location of the entry point.
767                 //
768                 static public Location EntryPointLocation;
769
770                 //
771                 // These are used to generate unique names on the structs and fields.
772                 //
773                 static int field_count;
774                 
775                 //
776                 // Makes an initialized struct, returns the field builder that
777                 // references the data.  Thanks go to Sergey Chaban for researching
778                 // how to do this.  And coming up with a shorter mechanism than I
779                 // was able to figure out.
780                 //
781                 // This works but makes an implicit public struct $ArrayType$SIZE and
782                 // makes the fields point to it.  We could get more control if we did
783                 // use instead:
784                 //
785                 // 1. DefineNestedType on the impl_details_class with our struct.
786                 //
787                 // 2. Define the field on the impl_details_class
788                 //
789                 static public FieldBuilder MakeStaticData (byte [] data)
790                 {
791                         FieldBuilder fb;
792                         int size = data.Length;
793                         
794                         if (impl_details_class == null){
795                                 impl_details_class = CodeGen.ModuleBuilder.DefineType (
796                                         "<PrivateImplementationDetails>",
797                                         TypeAttributes.NotPublic,
798                                         TypeManager.object_type);
799                                 
800                                 RegisterHelperClass (impl_details_class);
801                         }
802
803                         fb = impl_details_class.DefineInitializedData (
804                                 "$$field-" + (field_count++), data,
805                                 FieldAttributes.Static | FieldAttributes.Assembly);
806                         
807                         return fb;
808                 }
809
810                 //
811                 // Adds a global attribute that was declared in `container', 
812                 // the attribute is in `attr', and it was defined at `loc'
813                 //
814                 static public void AddGlobalAttributeSection (TypeContainer container, AttributeSection attr)
815                 {
816                         NamespaceEntry ns = container.NamespaceEntry;
817                         Attributes a = (Attributes) global_attributes [ns];
818
819                         if (a == null)
820                                 global_attributes [ns] = new Attributes (attr);
821                         else
822                                 a.AddAttributeSection (attr);
823                 }
824         }
825 }
826               
827