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