* statement.cs (ToplevelBlock.ResolveMeta): Move CS0136 checks ...
[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 // (C) 2004 Novell, Inc
11
12 using System;
13 using System.Collections;
14 using System.Reflection;
15 using System.Reflection.Emit;
16 using System.Diagnostics;
17
18 namespace Mono.CSharp {
19
20         public enum LanguageVersion
21         {
22                 Default = 0,
23                 ISO_1   = 1,
24                 LINQ
25         }
26
27         public class RootContext {
28
29                 //
30                 // Contains the parsed tree
31                 //
32                 static RootTypes root;
33
34                 //
35                 // This hashtable contains all of the #definitions across the source code
36                 // it is used by the ConditionalAttribute handler.
37                 //
38                 public static Hashtable AllDefines = new Hashtable ();
39                 
40                 //
41                 // Whether we are being linked against the standard libraries.
42                 // This is only used to tell whether `System.Object' should
43                 // have a base class or not.
44                 //
45                 public static bool StdLib;
46
47                 //
48                 // This keeps track of the order in which classes were defined
49                 // so that we can poulate them in that order.
50                 //
51                 // Order is important, because we need to be able to tell, by
52                 // examining the list of methods of the base class, which ones are virtual
53                 // or abstract as well as the parent names (to implement new, 
54                 // override).
55                 //
56                 static ArrayList type_container_resolve_order;
57
58                 //
59                 // Holds a reference to the Private Implementation Details
60                 // class.
61                 //
62                 static ArrayList helper_classes;
63                 
64                 static TypeBuilder impl_details_class;
65
66                 public static int WarningLevel;
67
68                 public static Target Target;
69                 public static string TargetExt;
70
71                 public static bool VerifyClsCompliance = true;
72
73                 /// <summary>
74                 /// Holds /optimize option
75                 /// </summary>
76                 public static bool Optimize = true;
77
78                 public static LanguageVersion Version;
79
80                 //
81                 // We keep strongname related info here because
82                 // it's also used as complier options from CSC 8.x
83                 //
84                 public static string StrongNameKeyFile;
85                 public static string StrongNameKeyContainer;
86                 public static bool StrongNameDelaySign;
87
88                 //
89                 // If set, enable XML documentation generation
90                 //
91                 public static Documentation Documentation;
92
93                 static public string MainClass;
94
95                 //
96                 // Constructor
97                 //
98                 static RootContext ()
99                 {
100                         Reset ();
101                 }
102
103                 public static void Reset ()
104                 {
105                         root = new RootTypes ();
106                         type_container_resolve_order = new ArrayList ();
107                         EntryPoint = null;
108                         WarningLevel = 3;
109                         Checked = false;
110                         Unsafe = false;
111                         StdLib = true;
112                         StrongNameKeyFile = null;
113                         StrongNameKeyContainer = null;
114                         StrongNameDelaySign = false;
115                         MainClass = null;
116                         Target = Target.Exe;
117                         TargetExt = ".exe";
118                         Version = LanguageVersion.Default;
119                         Documentation = null;
120                         impl_details_class = null;
121                         helper_classes = null;
122                 }
123
124                 public static bool NeedsEntryPoint {
125                         get { return RootContext.Target == Target.Exe || RootContext.Target == Target.WinExe; }
126                 }
127
128                 static public RootTypes ToplevelTypes {
129                         get { return root; }
130                 }
131
132                 public static void RegisterOrder (TypeContainer tc)
133                 {
134                         type_container_resolve_order.Add (tc);
135                 }
136                 
137                 // 
138                 // The default compiler checked state
139                 //
140                 static public bool Checked;
141
142                 //
143                 // Whether to allow Unsafe code
144                 //
145                 static public bool Unsafe;
146                 
147                 // <remarks>
148                 //   This function is used to resolve the hierarchy tree.
149                 //   It processes interfaces, structs and classes in that order.
150                 //
151                 //   It creates the TypeBuilder's as it processes the user defined
152                 //   types.  
153                 // </remarks>
154                 static public void ResolveTree ()
155                 {
156                         //
157                         // Interfaces are processed next, as classes and
158                         // structs might inherit from an object or implement
159                         // a set of interfaces, we need to be able to tell
160                         // them appart by just using the TypeManager.
161                         //
162                         foreach (TypeContainer tc in root.Types)
163                                 tc.CreateType ();
164
165                         foreach (TypeContainer tc in root.Types)
166                                 tc.DefineType ();
167
168                         if (root.Delegates != null)
169                                 foreach (Delegate d in root.Delegates) 
170                                         d.DefineType ();
171                 }
172
173                 static void Error_TypeConflict (string name, Location loc)
174                 {
175                         Report.Error (
176                                 520, loc, "`" + name + "' conflicts with a predefined type");
177                 }
178
179                 static void Error_TypeConflict (string name)
180                 {
181                         Report.Error (
182                                 520, "`" + name + "' conflicts with a predefined type");
183                 }
184
185                 //
186                 // Resolves a single class during the corlib bootstrap process
187                 //
188                 static Type BootstrapCorlib_ResolveClass (TypeContainer root, string name)
189                 {
190                         object o = root.GetDefinition (name);
191                         if (o == null){
192                                 Report.Error (518, "The predefined type `" + name + "' is not defined or imported");
193                                 return null;
194                         }
195
196                         if (!(o is Class)){
197                                 if (o is DeclSpace){
198                                         DeclSpace d = (DeclSpace) o;
199
200                                         Error_TypeConflict (name, d.Location);
201                                 } else
202                                         Error_TypeConflict (name);
203
204                                 return null;
205                         }
206
207                         Type t = ((DeclSpace) o).DefineType ();
208                         if (t != null)
209                                 AttributeTester.RegisterNonObsoleteType (t);
210                         return t;
211                 }
212
213                 //
214                 // Resolves a struct during the corlib bootstrap process
215                 //
216                 static void BootstrapCorlib_ResolveStruct (TypeContainer root, string name)
217                 {
218                         object o = root.GetDefinition (name);
219                         if (o == null){
220                                 Report.Error (518, "The predefined type `" + name + "' is not defined or imported");
221                                 return;
222                         }
223
224                         if (!(o is Struct)){
225                                 if (o is DeclSpace){
226                                         DeclSpace d = (DeclSpace) o;
227
228                                         Error_TypeConflict (name, d.Location);
229                                 } else
230                                         Error_TypeConflict (name);
231
232                                 return;
233                         }
234
235                         ((DeclSpace) o).DefineType ();
236                 }
237
238                 //
239                 // Resolves a struct during the corlib bootstrap process
240                 //
241                 static void BootstrapCorlib_ResolveInterface (TypeContainer root, string name)
242                 {
243                         object o = root.GetDefinition (name);
244                         if (o == null){
245                                 Report.Error (518, "The predefined type `" + name + "' is not defined or imported");
246                                 return;
247                         }
248
249                         if (!(o is Interface)){
250                                 if (o is DeclSpace){
251                                         DeclSpace d = (DeclSpace) o;
252
253                                         Error_TypeConflict (name, d.Location);
254                                 } else
255                                         Error_TypeConflict (name);
256
257                                 return;
258                         }
259
260                         Type t = ((DeclSpace) o).DefineType ();
261                         if (t != null)
262                                 AttributeTester.RegisterNonObsoleteType (t);
263                 }
264
265                 //
266                 // Resolves a delegate during the corlib bootstrap process
267                 //
268                 static void BootstrapCorlib_ResolveDelegate (TypeContainer root, string name)
269                 {
270                         object o = root.GetDefinition (name);
271                         if (o == null){
272                                 Report.Error (518, "The predefined type `" + name + "' is not defined or imported");
273                                 return;
274                         }
275
276                         if (!(o is Delegate)){
277                                 Error_TypeConflict (name);
278                                 return;
279                         }
280
281                         ((DeclSpace) o).DefineType ();
282                 }
283                 
284
285                 /// <summary>
286                 ///    Resolves the core types in the compiler when compiling with --nostdlib
287                 /// </summary>
288                 static public void ResolveCore ()
289                 {
290                         TypeManager.object_type = BootstrapCorlib_ResolveClass (root, "System.Object");
291                         TypeManager.system_object_expr.Type = TypeManager.object_type;
292                         TypeManager.value_type = BootstrapCorlib_ResolveClass (root, "System.ValueType");
293                         TypeManager.system_valuetype_expr.Type = TypeManager.value_type;
294
295                         //
296                         // The core attributes
297                         //
298                         BootstrapCorlib_ResolveInterface (root, "System.Runtime.InteropServices._Attribute");
299                         TypeManager.attribute_type = BootstrapCorlib_ResolveClass (root, "System.Attribute");
300                         TypeManager.obsolete_attribute_type = BootstrapCorlib_ResolveClass (root, "System.ObsoleteAttribute");
301                         TypeManager.indexer_name_type = BootstrapCorlib_ResolveClass (root, "System.Runtime.CompilerServices.IndexerNameAttribute");
302                         
303                         string [] interfaces_first_stage = {
304                                 "System.IComparable", "System.ICloneable",
305                                 "System.IConvertible",
306                                 
307                                 "System.Collections.IEnumerable",
308                                 "System.Collections.ICollection",
309                                 "System.Collections.IEnumerator",
310                                 "System.Collections.IList", 
311                                 "System.IAsyncResult",
312                                 "System.IDisposable",
313                                 
314                                 "System.Runtime.Serialization.ISerializable",
315
316                                 "System.Reflection.IReflect",
317                                 "System.Reflection.ICustomAttributeProvider",
318 #if GMCS_SOURCE
319                                 "System.Runtime.InteropServices._Exception",
320
321                                 //
322                                 // Generic types
323                                 //
324                                 "System.Collections.Generic.IEnumerator`1",
325                                 "System.Collections.Generic.IEnumerable`1"
326 #endif
327                         };
328
329                         foreach (string iname in interfaces_first_stage)
330                                 BootstrapCorlib_ResolveInterface (root, iname);
331
332                         //
333                         // These are the base value types
334                         //
335                         string [] structs_first_stage = {
336                                 "System.Byte",    "System.SByte",
337                                 "System.Int16",   "System.UInt16",
338                                 "System.Int32",   "System.UInt32",
339                                 "System.Int64",   "System.UInt64",
340                         };
341
342                         foreach (string cname in structs_first_stage)
343                                 BootstrapCorlib_ResolveStruct (root, cname);
344
345                         //
346                         // Now, we can load the enumerations, after this point,
347                         // we can use enums.
348                         //
349                         TypeManager.InitEnumUnderlyingTypes ();
350
351                         string [] structs_second_stage = {
352                                 "System.Single",  "System.Double",
353                                 "System.Char",    "System.Boolean",
354                                 "System.Decimal", "System.Void",
355                                 "System.RuntimeFieldHandle",
356                                 "System.RuntimeArgumentHandle",
357                                 "System.RuntimeTypeHandle",
358                                 "System.IntPtr",
359                                 "System.TypedReference",
360                                 "System.ArgIterator"
361                         };
362                         
363                         foreach (string cname in structs_second_stage)
364                                 BootstrapCorlib_ResolveStruct (root, cname);
365                         
366                         //
367                         // These are classes that depends on the core interfaces
368                         //
369                         string [] classes_second_stage = {
370                                 "System.Enum",
371                                 "System.String",
372                                 "System.Array",
373                                 "System.Reflection.MemberInfo",
374                                 "System.Type",
375                                 "System.Exception",
376 #if GMCS_SOURCE
377                                 "System.Activator",
378 #endif
379
380                                 //
381                                 // These are not really important in the order, but they
382                                 // are used by the compiler later on (typemanager/CoreLookupType-d)
383                                 //
384                                 "System.Runtime.CompilerServices.RuntimeHelpers",
385                                 "System.Reflection.DefaultMemberAttribute",
386                                 "System.Threading.Monitor",
387                                 "System.Threading.Interlocked",
388                                 
389                                 "System.AttributeUsageAttribute",
390                                 "System.Runtime.InteropServices.DllImportAttribute",
391                                 "System.Runtime.CompilerServices.MethodImplAttribute",
392                                 "System.Runtime.InteropServices.MarshalAsAttribute",
393 #if GMCS_SOURCE
394                                 "System.Runtime.CompilerServices.CompilerGeneratedAttribute",
395                                 "System.Runtime.CompilerServices.FixedBufferAttribute",
396 #endif
397                                 "System.Diagnostics.ConditionalAttribute",
398                                 "System.ParamArrayAttribute",
399                                 "System.CLSCompliantAttribute",
400                                 "System.Security.UnverifiableCodeAttribute",
401                                 "System.Security.Permissions.SecurityAttribute",
402                                 "System.Runtime.CompilerServices.DecimalConstantAttribute",
403                                 "System.Runtime.InteropServices.InAttribute",
404                                 "System.Runtime.InteropServices.OutAttribute",
405                                 "System.Runtime.InteropServices.StructLayoutAttribute",
406                                 "System.Runtime.InteropServices.FieldOffsetAttribute",
407 #if GMCS_SOURCE
408                                 "System.Runtime.InteropServices.DefaultCharSetAttribute",
409 #endif
410                                 "System.InvalidOperationException",
411                                 "System.NotSupportedException",
412                                 "System.MarshalByRefObject",
413                                 "System.Security.CodeAccessPermission",
414                                 "System.Runtime.CompilerServices.RequiredAttributeAttribute",
415                                 "System.Runtime.InteropServices.GuidAttribute",
416                                 "System.Reflection.AssemblyCultureAttribute"
417                         };
418
419                         foreach (string cname in classes_second_stage)
420                                 BootstrapCorlib_ResolveClass (root, cname);
421 #if GMCS_SOURCE
422                         BootstrapCorlib_ResolveStruct (root, "System.Nullable`1");
423 #endif
424                         BootstrapCorlib_ResolveDelegate (root, "System.AsyncCallback");
425
426                         // These will be defined indirectly during the previous ResolveDelegate.
427                         // However make sure the rest of the checks happen.
428                         string [] delegate_types = { "System.Delegate", "System.MulticastDelegate" };
429                         foreach (string cname in delegate_types)
430                                 BootstrapCorlib_ResolveClass (root, cname);
431                 }
432                         
433                 // <summary>
434                 //   Closes all open types
435                 // </summary>
436                 //
437                 // <remarks>
438                 //   We usually use TypeBuilder types.  When we are done
439                 //   creating the type (which will happen after we have added
440                 //   methods, fields, etc) we need to "Define" them before we
441                 //   can save the Assembly
442                 // </remarks>
443                 static public void CloseTypes ()
444                 {
445                         //
446                         // We do this in two passes, first we close the structs,
447                         // then the classes, because it seems the code needs it this
448                         // way.  If this is really what is going on, we should probably
449                         // make sure that we define the structs in order as well.
450                         //
451                         foreach (TypeContainer tc in type_container_resolve_order){
452                                 if (tc.Kind == Kind.Struct && tc.Parent == root){
453                                         tc.CloseType ();
454                                 }
455                         }
456
457                         foreach (TypeContainer tc in type_container_resolve_order){
458                                 if (!(tc.Kind == Kind.Struct && tc.Parent == root))
459                                         tc.CloseType ();                                        
460                         }
461                         
462                         if (root.Delegates != null)
463                                 foreach (Delegate d in root.Delegates)
464                                         d.CloseType ();
465
466
467                         //
468                         // If we have a <PrivateImplementationDetails> class, close it
469                         //
470                         if (helper_classes != null){
471                                 foreach (TypeBuilder type_builder in helper_classes) {
472 #if GMCS_SOURCE
473                                         type_builder.SetCustomAttribute (TypeManager.compiler_generated_attr);
474 #endif
475                                         type_builder.CreateType ();
476                                 }
477                         }
478                         
479                         type_container_resolve_order = null;
480                         helper_classes = null;
481                         //root = null;
482                         TypeManager.CleanUp ();
483                 }
484
485                 /// <summary>
486                 ///   Used to register classes that need to be closed after all the
487                 ///   user defined classes
488                 /// </summary>
489                 public static void RegisterCompilerGeneratedType (TypeBuilder helper_class)
490                 {
491                         if (helper_classes == null)
492                                 helper_classes = new ArrayList ();
493
494                         helper_classes.Add (helper_class);
495                 }
496                 
497                 static public void PopulateCoreType (TypeContainer root, string name)
498                 {
499                         DeclSpace ds = (DeclSpace) root.GetDefinition (name);
500
501                         ds.DefineMembers ();
502                         ds.Define ();
503                 }
504                 
505                 static public void BootCorlib_PopulateCoreTypes ()
506                 {
507                         PopulateCoreType (root, "System.Object");
508                         PopulateCoreType (root, "System.ValueType");
509                         PopulateCoreType (root, "System.Attribute");
510                         PopulateCoreType (root, "System.Runtime.CompilerServices.IndexerNameAttribute");
511                 }
512                 
513                 // <summary>
514                 //   Populates the structs and classes with fields and methods
515                 // </summary>
516                 //
517                 // This is invoked after all interfaces, structs and classes
518                 // have been defined through `ResolveTree' 
519                 static public void PopulateTypes ()
520                 {
521
522                         if (type_container_resolve_order != null){
523                                 foreach (TypeContainer tc in type_container_resolve_order)
524                                         tc.ResolveType ();
525                                 foreach (TypeContainer tc in type_container_resolve_order)
526                                         tc.DefineMembers ();
527                         }
528
529                         ArrayList delegates = root.Delegates;
530                         if (delegates != null){
531                                 foreach (Delegate d in delegates)
532                                         d.DefineMembers ();
533                         }
534
535                         //
536                         // Check for cycles in the struct layout
537                         //
538                         if (type_container_resolve_order != null){
539                                 Hashtable seen = new Hashtable ();
540                                 foreach (TypeContainer tc in type_container_resolve_order)
541                                         TypeManager.CheckStructCycles (tc, seen);
542                         }
543                 }
544
545                 //
546                 // A generic hook delegate
547                 //
548                 public delegate void Hook ();
549
550                 //
551                 // A hook invoked when the code has been generated.
552                 //
553                 public static event Hook EmitCodeHook;
554
555                 //
556                 // DefineTypes is used to fill in the members of each type.
557                 //
558                 static public void DefineTypes ()
559                 {
560                         ArrayList delegates = root.Delegates;
561                         if (delegates != null){
562                                 foreach (Delegate d in delegates)
563                                         d.Define ();
564                         }
565
566                         if (type_container_resolve_order != null){
567                                 foreach (TypeContainer tc in type_container_resolve_order) {
568                                         // When compiling corlib, these types have already been
569                                         // populated from BootCorlib_PopulateCoreTypes ().
570                                         if (!RootContext.StdLib &&
571                                             ((tc.Name == "System.Object") ||
572                                              (tc.Name == "System.Attribute") ||
573                                              (tc.Name == "System.ValueType") ||
574                                              (tc.Name == "System.Runtime.CompilerServices.IndexerNameAttribute")))
575                                                 continue;
576
577                                         tc.Define ();
578                                 }
579                         }
580                 }
581
582                 static public void EmitCode ()
583                 {
584                         if (type_container_resolve_order != null) {
585                                 foreach (TypeContainer tc in type_container_resolve_order)
586                                         tc.EmitType ();
587
588                                 if (Report.Errors > 0)
589                                         return;
590
591                                 foreach (TypeContainer tc in type_container_resolve_order)
592                                         tc.VerifyMembers ();
593                         }
594                         
595                         if (root.Delegates != null) {
596                                 foreach (Delegate d in root.Delegates)
597                                         d.Emit ();
598                         }                       
599                         //
600                         // Run any hooks after all the types have been defined.
601                         // This is used to create nested auxiliary classes for example
602                         //
603
604                         if (EmitCodeHook != null)
605                                 EmitCodeHook ();
606
607                         CodeGen.Assembly.Emit (root);
608                         CodeGen.Module.Emit (root);
609                 }
610                 
611                 //
612                 // Public Field, used to track which method is the public entry
613                 // point.
614                 //
615                 static public MethodInfo EntryPoint;
616
617                 //
618                 // Track the location of the entry point.
619                 //
620                 static public Location EntryPointLocation;
621
622                 //
623                 // These are used to generate unique names on the structs and fields.
624                 //
625                 static int field_count;
626                 
627                 //
628                 // Makes an initialized struct, returns the field builder that
629                 // references the data.  Thanks go to Sergey Chaban for researching
630                 // how to do this.  And coming up with a shorter mechanism than I
631                 // was able to figure out.
632                 //
633                 // This works but makes an implicit public struct $ArrayType$SIZE and
634                 // makes the fields point to it.  We could get more control if we did
635                 // use instead:
636                 //
637                 // 1. DefineNestedType on the impl_details_class with our struct.
638                 //
639                 // 2. Define the field on the impl_details_class
640                 //
641                 static public FieldBuilder MakeStaticData (byte [] data)
642                 {
643                         FieldBuilder fb;
644                         
645                         if (impl_details_class == null){
646                                 impl_details_class = CodeGen.Module.Builder.DefineType (
647                                         "<PrivateImplementationDetails>",
648                                         TypeAttributes.NotPublic,
649                                         TypeManager.object_type);
650                                 
651                                 RegisterCompilerGeneratedType (impl_details_class);
652                         }
653
654                         fb = impl_details_class.DefineInitializedData (
655                                 "$$field-" + (field_count++), data,
656                                 FieldAttributes.Static | FieldAttributes.Assembly);
657                         
658                         return fb;
659                 }
660
661                 public static void CheckUnsafeOption (Location loc)
662                 {
663                         if (!Unsafe) {
664                                 Report.Error (227, loc, 
665                                         "Unsafe code requires the `unsafe' command line option to be specified");
666                         }
667                 }
668         }
669 }
670               
671