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