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