Wed Feb 24 15:47:16 CET 2010 Paolo Molaro <lupus@ximian.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
271                         //
272                         // If we have a <PrivateImplementationDetails> class, close it
273                         //
274                         if (helper_classes != null){
275                                 foreach (TypeBuilder type_builder in helper_classes) {
276                                         PredefinedAttributes.Get.CompilerGenerated.EmitAttribute (type_builder);
277                                         type_builder.CreateType ();
278                                 }
279                         }
280                         
281                         type_container_resolve_order = null;
282                         helper_classes = null;
283                         //root = null;
284                         TypeManager.CleanUp ();
285                 }
286
287                 /// <summary>
288                 ///   Used to register classes that need to be closed after all the
289                 ///   user defined classes
290                 /// </summary>
291                 public static void RegisterCompilerGeneratedType (TypeBuilder helper_class)
292                 {
293                         if (helper_classes == null)
294                                 helper_classes = new List<TypeBuilder> ();
295
296                         helper_classes.Add (helper_class);
297                 }
298                 
299                 static public DeclSpace PopulateCoreType (TypeContainer root, string name)
300                 {
301                         DeclSpace ds = (DeclSpace) root.GetDefinition (name);
302                         // Core type was imported
303                         if (ds == null)
304                                 return null;
305
306                         ds.Define ();
307                         return ds;
308                 }
309                 
310                 static public void BootCorlib_PopulateCoreTypes ()
311                 {
312                         // Clear -nostdlib flag when object type is imported
313                         if (PopulateCoreType (root, "System.Object") == null)
314                                 RootContext.StdLib = true;
315
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                         var 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                                 var seen = new Dictionary<TypeContainer, object> ();
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 (ToplevelTypes.Compiler.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, Report Report)
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 }