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