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