b429b974de11d5c2ee4ab09df266c47ff387ce9b
[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                                 "System.Runtime.InteropServices.InAttribute"
357                         };
358
359                         // We must store them here before calling BootstrapCorlib_ResolveDelegate.
360                         TypeManager.string_type = BootstrapCorlib_ResolveClass (root, "System.String");
361                         TypeManager.enum_type = BootstrapCorlib_ResolveClass (root, "System.Enum");
362                         TypeManager.array_type = BootstrapCorlib_ResolveClass (root, "System.Array");
363                         TypeManager.multicast_delegate_type = BootstrapCorlib_ResolveClass (root, "System.MulticastDelegate");
364                         TypeManager.delegate_type = BootstrapCorlib_ResolveClass (root, "System.Delegate");
365                         
366                         foreach (string cname in classes_second_stage)
367                                 BootstrapCorlib_ResolveClass (root, cname);
368
369                         BootstrapCorlib_ResolveDelegate (root, "System.AsyncCallback");
370                 }
371                         
372                 // <summary>
373                 //   Closes all open types
374                 // </summary>
375                 //
376                 // <remarks>
377                 //   We usually use TypeBuilder types.  When we are done
378                 //   creating the type (which will happen after we have added
379                 //   methods, fields, etc) we need to "Define" them before we
380                 //   can save the Assembly
381                 // </remarks>
382                 static public void CloseTypes ()
383                 {
384                         TypeContainer root = Tree.Types;
385                         
386                         ArrayList ifaces = root.Interfaces;
387
388                         if (root.Enums != null)
389                                 foreach (Enum en in root.Enums)
390                                         en.CloseType ();
391
392                         if (attribute_types != null)
393                                 foreach (TypeContainer tc in attribute_types)
394                                         tc.CloseType ();
395                         
396                         foreach (Interface iface in interface_resolve_order)
397                                 iface.CloseType ();
398
399                         //
400                         // We do this in two passes, first we close the structs,
401                         // then the classes, because it seems the code needs it this
402                         // way.  If this is really what is going on, we should probably
403                         // make sure that we define the structs in order as well.
404                         //
405                         foreach (TypeContainer tc in type_container_resolve_order){
406                                 if (tc is Struct && tc.Parent == tree.Types){
407                                         tc.CloseType ();
408                                 }
409                         }
410
411                         foreach (TypeContainer tc in type_container_resolve_order){
412                                 if (!(tc is Struct && tc.Parent == tree.Types))
413                                         tc.CloseType ();                                        
414                         }
415                         
416                         if (root.Delegates != null)
417                                 foreach (Delegate d in root.Delegates)
418                                         d.CloseType ();
419
420
421                         //
422                         // If we have a <PrivateImplementationDetails> class, close it
423                         //
424                         if (impl_details_class != null){
425                                 impl_details_class.CreateType ();
426                         }
427                 }
428
429                 //
430                 // This idea is from Felix Arrese-Igor
431                 //
432                 // Returns : the implicit parent of a composite namespace string
433                 //   eg. Implicit parent of A.B is A
434                 //
435                 static public string ImplicitParent (string ns)
436                 {
437                         int i = ns.LastIndexOf (".");
438                         if (i < 0)
439                                 return null;
440                         
441                         return ns.Substring (0, i);
442                 }
443
444                 static Type NamespaceLookup (Namespace curr_ns, string name, Location loc)
445                 {
446                         Type t;
447                         
448                         //
449                         // Try in the current namespace and all its implicit parents
450                         //
451                         for (string ns = curr_ns.Name; ns != null; ns = ImplicitParent (ns)) {
452                                 t = TypeManager.LookupType (MakeFQN (ns, name));
453                                 if (t != null)
454                                         return t;
455                         }
456                         
457                         //
458                         // It's possible that name already is fully qualified. So we do
459                         // a simple direct lookup without adding any namespace names
460                         //
461                         t = TypeManager.LookupType (name); 
462                         if (t != null)
463                                 return t;
464
465                         //
466                         // Try the aliases in the current namespace
467                         //
468                         string alias = curr_ns.LookupAlias (name);
469
470                         if (alias != null) {
471                                 t = TypeManager.LookupType (alias);
472                                 if (t != null)
473                                         return t;
474
475                                 t = TypeManager.LookupType (MakeFQN (alias, name));
476                                 if (t != null)
477                                         return t;
478                         }
479                         
480                         for (Namespace ns = curr_ns; ns != null; ns = ns.Parent) {
481                                 //
482                                 // Look in the namespace ns
483                                 //
484                                 t = TypeManager.LookupType (MakeFQN (ns.Name, name));
485                                 if (t != null)
486                                         return t;
487                                 
488                                 //
489                                 // Then try with the using clauses
490                                 //
491                                 ArrayList using_list = ns.UsingTable;
492
493                                 if (using_list == null)
494                                         continue;
495
496                                 Type match = null;
497                                 foreach (Namespace.UsingEntry ue in using_list) {
498                                         match = TypeManager.LookupType (MakeFQN (ue.Name, name));
499                                         if (match != null){
500                                                 if (t != null){
501                                                         DeclSpace.Error_AmbiguousTypeReference (loc, name, t, match);
502                                                         return null;
503                                                 }
504                                                 
505                                                 t = match;
506                                                 ue.Used = true;
507                                         }
508                                 }
509                                 if (t != null)
510                                         return t;
511
512                                 //
513                                 // Try with aliases
514                                 //
515                                 string a = ns.LookupAlias (name);
516                                 if (a != null) {
517                                         t = TypeManager.LookupType (a);
518                                         if (t != null)
519                                                 return t;
520
521                                         t = TypeManager.LookupType (MakeFQN (a, name));
522                                         if (t != null)
523                                                 return t;
524                                 }
525                         }
526
527                         return null;
528                 }
529                 
530                 //
531                 // Public function used to locate types, this can only
532                 // be used after the ResolveTree function has been invoked.
533                 //
534                 // Returns: Type or null if they type can not be found.
535                 //
536                 // Come to think of it, this should be a DeclSpace
537                 //
538                 static public Type LookupType (DeclSpace ds, string name, bool silent, 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                                                 t = TypeManager.LookupType (current_type.FullName + "." + name);
560                                                 if (t != null){
561                                                         ds.Cache [name] = t;
562                                                         return t;
563                                                 }
564                                                 
565                                                 current_type = current_type.BaseType;
566                                         }
567                                         
568                                         containing_ds = containing_ds.Parent;
569                                 }
570                                 
571                                 t = NamespaceLookup (ds.Namespace, name, loc);
572                                 if (t != null){
573                                         ds.Cache [name] = t;
574                                         return t;
575                                 }
576                         }
577
578                         if (!silent)
579                                 Report.Error (246, loc, "Cannot find type `"+name+"'");
580                         
581                         return null;
582                 }
583
584                 // <summary>
585                 //   This is the silent version of LookupType, you can use this
586                 //   to `probe' for a type
587                 // </summary>
588                 static public Type LookupType (TypeContainer tc, string name, Location loc)
589                 {
590                         return LookupType (tc, name, true, loc);
591                 }
592
593                 static public bool IsNamespace (string name)
594                 {
595                         Namespace ns;
596
597                         if (tree.Namespaces != null){
598                                 ns = (Namespace) tree.Namespaces [name];
599
600                                 if (ns != null)
601                                         return true;
602                         }
603
604                         return false;
605                 }
606
607                 static void Report1530 (Location loc)
608                 {
609                         Report.Error (1530, loc, "Keyword new not allowed for namespace elements");
610                 }
611                 
612                 static public void PopulateCoreType (TypeContainer root, string name)
613                 {
614                         DeclSpace ds = (DeclSpace) root.GetDefinition (name);
615
616                         ds.DefineMembers (root);
617                         ds.Define (root);
618                 }
619                 
620                 static public void BootCorlib_PopulateCoreTypes ()
621                 {
622                         TypeContainer root = tree.Types;
623
624                         PopulateCoreType (root, "System.Object");
625                         PopulateCoreType (root, "System.ValueType");
626                         PopulateCoreType (root, "System.Attribute");
627                 }
628                 
629                 // <summary>
630                 //   Populates the structs and classes with fields and methods
631                 // </summary>
632                 //
633                 // This is invoked after all interfaces, structs and classes
634                 // have been defined through `ResolveTree' 
635                 static public void PopulateTypes ()
636                 {
637                         TypeContainer root = Tree.Types;
638
639                         if (attribute_types != null)
640                                 foreach (TypeContainer tc in attribute_types)
641                                         tc.DefineMembers (root);
642                         
643                         if (interface_resolve_order != null){
644                                 foreach (Interface iface in interface_resolve_order)
645                                         if ((iface.ModFlags & Modifiers.NEW) == 0)
646                                                 iface.DefineMembers (root);
647                                         else
648                                                 Report1530 (iface.Location);
649                         }
650
651
652                         if (type_container_resolve_order != null){
653                                 if (RootContext.StdLib){
654                                         foreach (TypeContainer tc in type_container_resolve_order) {
655                                                 if ((tc.ModFlags & Modifiers.NEW) == 0)
656                                                         tc.DefineMembers (root);
657                                                 else
658                                                         Report1530 (tc.Location);
659                                         }
660                                 } else {
661                                         foreach (TypeContainer tc in type_container_resolve_order) {
662                                                 // When compiling corlib, these types have already been
663                                                 // populated from BootCorlib_PopulateCoreTypes ().
664                                                 if (((tc.Name == "System.Object") ||
665                                                      (tc.Name == "System.Attribute") ||
666                                                      (tc.Name == "System.ValueType")))
667                                                 continue;
668
669                                                 if ((tc.ModFlags & Modifiers.NEW) == 0)
670                                                         tc.DefineMembers (root);
671                                                 else
672                                                         Report1530 (tc.Location);
673                                         }
674                                 } 
675                         }
676
677                         ArrayList delegates = root.Delegates;
678                         if (delegates != null){
679                                 foreach (Delegate d in delegates)
680                                         if ((d.ModFlags & Modifiers.NEW) == 0)
681                                                 d.DefineMembers (root);
682                                         else
683                                                 Report1530 (d.Location);
684                         }
685
686                         ArrayList enums = root.Enums;
687                         if (enums != null){
688                                 foreach (Enum en in enums)
689                                         if ((en.ModFlags & Modifiers.NEW) == 0)
690                                                 en.DefineMembers (root);
691                                         else
692                                                 Report1530 (en.Location);
693                         }
694                 }
695
696                 static public void DefineTypes ()
697                 {
698                         TypeContainer root = Tree.Types;
699
700                         if (attribute_types != null)
701                                 foreach (TypeContainer tc in attribute_types)
702                                         tc.Define (root);
703                         
704                         if (interface_resolve_order != null){
705                                 foreach (Interface iface in interface_resolve_order)
706                                         if ((iface.ModFlags & Modifiers.NEW) == 0)
707                                                 iface.Define (root);
708                         }
709
710
711                         if (type_container_resolve_order != null){
712                                 foreach (TypeContainer tc in type_container_resolve_order) {
713                                         // When compiling corlib, these types have already been
714                                         // populated from BootCorlib_PopulateCoreTypes ().
715                                         if (!RootContext.StdLib &&
716                                             ((tc.Name == "System.Object") ||
717                                              (tc.Name == "System.Attribute") ||
718                                              (tc.Name == "System.ValueType")))
719                                                 continue;
720
721                                         if ((tc.ModFlags & Modifiers.NEW) == 0)
722                                                 tc.Define (root);
723                                 }
724                         }
725
726                         ArrayList delegates = root.Delegates;
727                         if (delegates != null){
728                                 foreach (Delegate d in delegates)
729                                         if ((d.ModFlags & Modifiers.NEW) == 0)
730                                                 d.Define (root);
731                         }
732
733                         ArrayList enums = root.Enums;
734                         if (enums != null){
735                                 foreach (Enum en in enums)
736                                         if ((en.ModFlags & Modifiers.NEW) == 0)
737                                                 en.Define (root);
738                         }
739                 }
740
741                 static public void EmitCode ()
742                 {
743                         //
744                         // Because of the strange way in which we do things, global
745                         // attributes must be processed first.
746                         //
747                         if (global_attributes.Count > 0){
748                                 AssemblyBuilder ab = CodeGen.AssemblyBuilder;
749                                 TypeContainer dummy = new TypeContainer (null, "", new Location (-1));
750                                 EmitContext temp_ec = new EmitContext (
751                                         dummy, Mono.CSharp.Location.Null, null, null, 0, false);
752                         
753                                 foreach (DictionaryEntry de in global_attributes){
754                                         Namespace ns = (Namespace) de.Key;
755                                         Attributes attrs = (Attributes) de.Value;
756                                         
757                                         dummy.Namespace = ns;
758                                         Attribute.ApplyAttributes (temp_ec, ab, ab, attrs);
759                                 }
760                         }
761
762                         if (attribute_types != null)
763                                 foreach (TypeContainer tc in attribute_types)
764                                         tc.Emit ();
765                         
766                         if (type_container_resolve_order != null) {
767                                 foreach (TypeContainer tc in type_container_resolve_order)
768                                         tc.EmitConstants ();
769                                 
770                                 foreach (TypeContainer tc in type_container_resolve_order)
771                                         tc.Emit ();
772                         }
773                         
774                         if (Unsafe) {
775                                 if (TypeManager.unverifiable_code_ctor == null) {
776                                         Console.WriteLine ("Internal error ! Cannot set unverifiable code attribute.");
777                                         return;
778                                 }
779                                 
780                                 CustomAttributeBuilder cb = new CustomAttributeBuilder (TypeManager.unverifiable_code_ctor,
781                                                                                         new object [0]);
782                                 CodeGen.ModuleBuilder.SetCustomAttribute (cb);
783                         }
784                 }
785                 
786                 //
787                 // Public Field, used to track which method is the public entry
788                 // point.
789                 //
790                 static public MethodInfo EntryPoint;
791
792                 //
793                 // Track the location of the entry point.
794                 //
795                 static public Location EntryPointLocation;
796
797                 //
798                 // These are used to generate unique names on the structs and fields.
799                 //
800                 static int field_count;
801                 
802                 //
803                 // Makes an initialized struct, returns the field builder that
804                 // references the data.  Thanks go to Sergey Chaban for researching
805                 // how to do this.  And coming up with a shorter mechanism than I
806                 // was able to figure out.
807                 //
808                 // This works but makes an implicit public struct $ArrayType$SIZE and
809                 // makes the fields point to it.  We could get more control if we did
810                 // use instead:
811                 //
812                 // 1. DefineNestedType on the impl_details_class with our struct.
813                 //
814                 // 2. Define the field on the impl_details_class
815                 //
816                 static public FieldBuilder MakeStaticData (byte [] data)
817                 {
818                         FieldBuilder fb;
819                         int size = data.Length;
820                         
821                         if (impl_details_class == null)
822                                 impl_details_class = CodeGen.ModuleBuilder.DefineType (
823                                         "<PrivateImplementationDetails>", TypeAttributes.NotPublic, TypeManager.object_type);
824
825                         fb = impl_details_class.DefineInitializedData (
826                                 "$$field-" + (field_count++), data,
827                                 FieldAttributes.Static | FieldAttributes.Assembly);
828                         
829                         return fb;
830                 }
831
832                 //
833                 // Adds a global attribute that was declared in `container', 
834                 // the attribute is in `attr', and it was defined at `loc'
835                 //
836                 static public void AddGlobalAttributeSection (TypeContainer container, AttributeSection attr)
837                 {
838                         Namespace ns = container.Namespace;
839                         Attributes a = (Attributes) global_attributes [ns];
840
841                         if (a == null)
842                                 global_attributes [ns] = new Attributes (attr);
843                         else
844                                 a.AddAttributeSection (attr);
845                 }
846         }
847 }
848               
849