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