Fixes for Java compilations:
[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 //            Marek Safar  (marek.safar@gmail.com)
7 //
8 //
9 // Licensed under the terms of the GNU GPL
10 //
11 // (C) 2001 Ximian, Inc (http://www.ximian.com)
12 // (C) 2004 Novell, Inc
13
14 using System;
15 using System.Collections;
16 using System.Reflection;
17 using System.Reflection.Emit;
18 using System.Diagnostics;
19
20 namespace Mono.CSharp {
21
22         public enum LanguageVersion
23         {
24                 ISO_1           = 1,
25                 Default_MCS     = 2,
26                 ISO_2           = 3,
27                 LINQ            = 4,
28
29 #if GMCS_SOURCE
30                 Default         = LINQ
31 #else
32                 Default         = Default_MCS
33 #endif
34         }
35
36         public class RootContext {
37
38                 //
39                 // Contains the parsed tree
40                 //
41                 static RootTypes root;
42
43                 //
44                 // This hashtable contains all of the #definitions across the source code
45                 // it is used by the ConditionalAttribute handler.
46                 //
47                 public static Hashtable AllDefines = new Hashtable ();
48                 
49                 //
50                 // Whether we are being linked against the standard libraries.
51                 // This is only used to tell whether `System.Object' should
52                 // have a base class or not.
53                 //
54                 public static bool StdLib;
55
56                 //
57                 // This keeps track of the order in which classes were defined
58                 // so that we can poulate them in that order.
59                 //
60                 // Order is important, because we need to be able to tell, by
61                 // examining the list of methods of the base class, which ones are virtual
62                 // or abstract as well as the parent names (to implement new, 
63                 // override).
64                 //
65                 static ArrayList type_container_resolve_order;
66
67                 //
68                 // Holds a reference to the Private Implementation Details
69                 // class.
70                 //
71                 static ArrayList helper_classes;
72                 
73                 static TypeBuilder impl_details_class;
74
75                 public static Target Target;
76                 public static string TargetExt;
77
78                 public static bool VerifyClsCompliance = true;
79
80                 /// <summary>
81                 /// Holds /optimize option
82                 /// </summary>
83                 public static bool Optimize = true;
84
85                 public static LanguageVersion Version;
86
87                 //
88                 // We keep strongname related info here because
89                 // it's also used as complier options from CSC 8.x
90                 //
91                 public static string StrongNameKeyFile;
92                 public static string StrongNameKeyContainer;
93                 public static bool StrongNameDelaySign;
94
95                 //
96                 // If set, enable XML documentation generation
97                 //
98                 public static Documentation Documentation;
99
100                 static public string MainClass;
101
102                 //
103                 // Constructor
104                 //
105                 static RootContext ()
106                 {
107                         Reset ();
108                 }
109
110                 public static void Reset ()
111                 {
112                         root = new RootTypes ();
113                         type_container_resolve_order = new ArrayList ();
114                         EntryPoint = null;
115                         Report.WarningLevel = 3;
116                         Checked = false;
117                         Unsafe = false;
118                         StdLib = true;
119                         StrongNameKeyFile = null;
120                         StrongNameKeyContainer = null;
121                         StrongNameDelaySign = false;
122                         MainClass = null;
123                         Target = Target.Exe;
124                         TargetExt = ".exe";
125                         Version = LanguageVersion.Default;
126                         Documentation = null;
127                         impl_details_class = null;
128                         helper_classes = null;
129                 }
130
131                 public static bool NeedsEntryPoint {
132                         get { return RootContext.Target == Target.Exe || RootContext.Target == Target.WinExe; }
133                 }
134
135                 static public RootTypes ToplevelTypes {
136                         get { return root; }
137                 }
138
139                 public static void RegisterOrder (TypeContainer tc)
140                 {
141                         type_container_resolve_order.Add (tc);
142                 }
143                 
144                 // 
145                 // The default compiler checked state
146                 //
147                 static public bool Checked;
148
149                 //
150                 // Whether to allow Unsafe code
151                 //
152                 static public bool Unsafe;
153                 
154                 // <remarks>
155                 //   This function is used to resolve the hierarchy tree.
156                 //   It processes interfaces, structs and classes in that order.
157                 //
158                 //   It creates the TypeBuilder's as it processes the user defined
159                 //   types.  
160                 // </remarks>
161                 static public void ResolveTree ()
162                 {
163                         //
164                         // Interfaces are processed next, as classes and
165                         // structs might inherit from an object or implement
166                         // a set of interfaces, we need to be able to tell
167                         // them appart by just using the TypeManager.
168                         //
169                         foreach (TypeContainer tc in root.Types)
170                                 tc.CreateType ();
171
172                         foreach (TypeContainer tc in root.Types)
173                                 tc.DefineType ();
174
175                         if (root.Delegates != null)
176                                 foreach (Delegate d in root.Delegates) 
177                                         d.DefineType ();
178                 }
179
180                 // <summary>
181                 //   Closes all open types
182                 // </summary>
183                 //
184                 // <remarks>
185                 //   We usually use TypeBuilder types.  When we are done
186                 //   creating the type (which will happen after we have added
187                 //   methods, fields, etc) we need to "Define" them before we
188                 //   can save the Assembly
189                 // </remarks>
190                 static public void CloseTypes ()
191                 {
192                         //
193                         // We do this in two passes, first we close the structs,
194                         // then the classes, because it seems the code needs it this
195                         // way.  If this is really what is going on, we should probably
196                         // make sure that we define the structs in order as well.
197                         //
198                         foreach (TypeContainer tc in type_container_resolve_order){
199                                 if (tc.Kind == Kind.Struct && tc.Parent == root){
200                                         tc.CloseType ();
201                                 }
202                         }
203
204                         foreach (TypeContainer tc in type_container_resolve_order){
205                                 if (!(tc.Kind == Kind.Struct && tc.Parent == root))
206                                         tc.CloseType ();                                        
207                         }
208                         
209                         if (root.Delegates != null)
210                                 foreach (Delegate d in root.Delegates)
211                                         d.CloseType ();
212
213
214                         //
215                         // If we have a <PrivateImplementationDetails> class, close it
216                         //
217                         if (helper_classes != null){
218                                 foreach (TypeBuilder type_builder in helper_classes) {
219 #if GMCS_SOURCE
220                                         type_builder.SetCustomAttribute (TypeManager.GetCompilerGeneratedAttribute (Location.Null));
221 #endif
222                                         type_builder.CreateType ();
223                                 }
224                         }
225                         
226                         type_container_resolve_order = null;
227                         helper_classes = null;
228                         //root = null;
229                         TypeManager.CleanUp ();
230                 }
231
232                 /// <summary>
233                 ///   Used to register classes that need to be closed after all the
234                 ///   user defined classes
235                 /// </summary>
236                 public static void RegisterCompilerGeneratedType (TypeBuilder helper_class)
237                 {
238                         if (helper_classes == null)
239                                 helper_classes = new ArrayList ();
240
241                         helper_classes.Add (helper_class);
242                 }
243                 
244                 static public void PopulateCoreType (TypeContainer root, string name)
245                 {
246                         DeclSpace ds = (DeclSpace) root.GetDefinition (name);
247                         // Core type was imported
248                         if (ds == null)
249                                 return;
250
251                         ds.DefineMembers ();
252                         ds.Define ();
253                 }
254                 
255                 static public void BootCorlib_PopulateCoreTypes ()
256                 {
257                         PopulateCoreType (root, "System.Object");
258                         PopulateCoreType (root, "System.ValueType");
259                         PopulateCoreType (root, "System.Attribute");
260                         PopulateCoreType (root, "System.Runtime.CompilerServices.IndexerNameAttribute");
261                 }
262                 
263                 // <summary>
264                 //   Populates the structs and classes with fields and methods
265                 // </summary>
266                 //
267                 // This is invoked after all interfaces, structs and classes
268                 // have been defined through `ResolveTree' 
269                 static public void PopulateTypes ()
270                 {
271
272                         if (type_container_resolve_order != null){
273                                 foreach (TypeContainer tc in type_container_resolve_order)
274                                         tc.ResolveType ();
275                                 foreach (TypeContainer tc in type_container_resolve_order)
276                                         tc.DefineMembers ();
277                         }
278
279                         ArrayList delegates = root.Delegates;
280                         if (delegates != null){
281                                 foreach (Delegate d in delegates)
282                                         d.DefineMembers ();
283                         }
284
285                         //
286                         // Check for cycles in the struct layout
287                         //
288                         if (type_container_resolve_order != null){
289                                 Hashtable seen = new Hashtable ();
290                                 foreach (TypeContainer tc in type_container_resolve_order)
291                                         TypeManager.CheckStructCycles (tc, seen);
292                         }
293                 }
294
295                 //
296                 // A generic hook delegate
297                 //
298                 public delegate void Hook ();
299
300                 //
301                 // A hook invoked when the code has been generated.
302                 //
303                 public static event Hook EmitCodeHook;
304
305                 //
306                 // DefineTypes is used to fill in the members of each type.
307                 //
308                 static public void DefineTypes ()
309                 {
310                         ArrayList delegates = root.Delegates;
311                         if (delegates != null){
312                                 foreach (Delegate d in delegates)
313                                         d.Define ();
314                         }
315
316                         if (type_container_resolve_order != null){
317                                 foreach (TypeContainer tc in type_container_resolve_order) {
318                                         // When compiling corlib, these types have already been
319                                         // populated from BootCorlib_PopulateCoreTypes ().
320                                         if (!RootContext.StdLib &&
321                                             ((tc.Name == "System.Object") ||
322                                              (tc.Name == "System.Attribute") ||
323                                              (tc.Name == "System.ValueType") ||
324                                              (tc.Name == "System.Runtime.CompilerServices.IndexerNameAttribute")))
325                                                 continue;
326
327                                         tc.Define ();
328                                 }
329                         }
330                 }
331
332                 static public void EmitCode ()
333                 {
334                         if (type_container_resolve_order != null) {
335                                 foreach (TypeContainer tc in type_container_resolve_order)
336                                         tc.EmitType ();
337
338                                 if (Report.Errors > 0)
339                                         return;
340
341                                 foreach (TypeContainer tc in type_container_resolve_order)
342                                         tc.VerifyMembers ();
343                         }
344                         
345                         if (root.Delegates != null) {
346                                 foreach (Delegate d in root.Delegates)
347                                         d.Emit ();
348                         }                       
349                         //
350                         // Run any hooks after all the types have been defined.
351                         // This is used to create nested auxiliary classes for example
352                         //
353
354                         if (EmitCodeHook != null)
355                                 EmitCodeHook ();
356
357                         CodeGen.Assembly.Emit (root);
358                         CodeGen.Module.Emit (root);
359                 }
360                 
361                 //
362                 // Public Field, used to track which method is the public entry
363                 // point.
364                 //
365                 static public MethodInfo EntryPoint;
366
367                 //
368                 // Track the location of the entry point.
369                 //
370                 static public Location EntryPointLocation;
371
372                 //
373                 // These are used to generate unique names on the structs and fields.
374                 //
375                 static int field_count;
376                 
377                 //
378                 // Makes an initialized struct, returns the field builder that
379                 // references the data.  Thanks go to Sergey Chaban for researching
380                 // how to do this.  And coming up with a shorter mechanism than I
381                 // was able to figure out.
382                 //
383                 // This works but makes an implicit public struct $ArrayType$SIZE and
384                 // makes the fields point to it.  We could get more control if we did
385                 // use instead:
386                 //
387                 // 1. DefineNestedType on the impl_details_class with our struct.
388                 //
389                 // 2. Define the field on the impl_details_class
390                 //
391                 static public FieldBuilder MakeStaticData (byte [] data)
392                 {
393                         FieldBuilder fb;
394                         
395                         if (impl_details_class == null){
396                                 impl_details_class = CodeGen.Module.Builder.DefineType (
397                                         "<PrivateImplementationDetails>",
398                                         TypeAttributes.NotPublic,
399                                         TypeManager.object_type);
400                                 
401                                 RegisterCompilerGeneratedType (impl_details_class);
402                         }
403
404                         fb = impl_details_class.DefineInitializedData (
405                                 "$$field-" + (field_count++), data,
406                                 FieldAttributes.Static | FieldAttributes.Assembly);
407                         
408                         return fb;
409                 }
410
411                 public static void CheckUnsafeOption (Location loc)
412                 {
413                         if (!Unsafe) {
414                                 Report.Error (227, loc, 
415                                         "Unsafe code requires the `unsafe' command line option to be specified");
416                         }
417                 }
418         }
419 }
420               
421