**** Merged from MCS ****
[mono.git] / mcs / gmcs / typemanager.cs
1 //
2 // typemanager.cs: C# type manager
3 //
4 // Author: Miguel de Icaza (miguel@gnu.org)
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 //
11 //
12
13 //
14 // We will eventually remove the SIMPLE_SPEEDUP, and should never change 
15 // the behavior of the compilation.  This can be removed if we rework
16 // the code to get a list of namespaces available.
17 //
18 #define SIMPLE_SPEEDUP
19
20 using System;
21 using System.IO;
22 using System.Globalization;
23 using System.Collections;
24 using System.Reflection;
25 using System.Reflection.Emit;
26 using System.Text;
27 using System.Text.RegularExpressions;
28 using System.Runtime.CompilerServices;
29 using System.Diagnostics;
30
31 namespace Mono.CSharp {
32
33 public class TypeManager {
34         //
35         // A list of core types that the compiler requires or uses
36         //
37         static public Type object_type;
38         static public Type value_type;
39         static public Type string_type;
40         static public Type int32_type;
41         static public Type uint32_type;
42         static public Type int64_type;
43         static public Type uint64_type;
44         static public Type float_type;
45         static public Type double_type;
46         static public Type char_type;
47         static public Type char_ptr_type;
48         static public Type short_type;
49         static public Type decimal_type;
50         static public Type bool_type;
51         static public Type sbyte_type;
52         static public Type byte_type;
53         static public Type ushort_type;
54         static public Type enum_type;
55         static public Type delegate_type;
56         static public Type multicast_delegate_type;
57         static public Type void_type;
58         static public Type enumeration_type;
59         static public Type array_type;
60         static public Type runtime_handle_type;
61         static public Type icloneable_type;
62         static public Type type_type;
63         static public Type ienumerator_type;
64         static public Type ienumerable_type;
65         static public Type idisposable_type;
66         static public Type iconvertible_type;
67         static public Type default_member_type;
68         static public Type iasyncresult_type;
69         static public Type asynccallback_type;
70         static public Type intptr_type;
71         static public Type monitor_type;
72         static public Type runtime_field_handle_type;
73         static public Type runtime_argument_handle_type;
74         static public Type attribute_type;
75         static public Type attribute_usage_type;
76         static public Type dllimport_type;
77         static public Type unverifiable_code_type;
78         static public Type methodimpl_attr_type;
79         static public Type marshal_as_attr_type;
80         static public Type new_constraint_attr_type;
81         static public Type param_array_type;
82         static public Type guid_attr_type;
83         static public Type void_ptr_type;
84         static public Type indexer_name_type;
85         static public Type exception_type;
86         static public Type activator_type;
87         static public Type invalid_operation_exception_type;
88         static public Type not_supported_exception_type;
89         static public Type obsolete_attribute_type;
90         static public Type conditional_attribute_type;
91         static public Type in_attribute_type;
92         static public Type cls_compliant_attribute_type;
93         static public Type typed_reference_type;
94         static public Type arg_iterator_type;
95         static public Type mbr_type;
96         static public Type struct_layout_attribute_type;
97         static public Type field_offset_attribute_type;
98
99         static public Type generic_ienumerator_type;
100         static public Type generic_ienumerable_type;
101
102         //
103         // An empty array of types
104         //
105         static public Type [] NoTypes;
106         static public TypeExpr [] NoTypeExprs;
107
108
109         // 
110         // Expressions representing the internal types.  Used during declaration
111         // definition.
112         //
113         static public TypeExpr system_object_expr, system_string_expr; 
114         static public TypeExpr system_boolean_expr, system_decimal_expr;
115         static public TypeExpr system_single_expr, system_double_expr;
116         static public TypeExpr system_sbyte_expr, system_byte_expr;
117         static public TypeExpr system_int16_expr, system_uint16_expr;
118         static public TypeExpr system_int32_expr, system_uint32_expr;
119         static public TypeExpr system_int64_expr, system_uint64_expr;
120         static public TypeExpr system_char_expr, system_void_expr;
121         static public TypeExpr system_asynccallback_expr;
122         static public TypeExpr system_iasyncresult_expr;
123         static public TypeExpr system_valuetype_expr;
124
125         //
126         // This is only used when compiling corlib
127         //
128         static public Type system_int32_type;
129         static public Type system_array_type;
130         static public Type system_type_type;
131         static public Type system_assemblybuilder_type;
132         static public MethodInfo system_int_array_get_length;
133         static public MethodInfo system_int_array_get_rank;
134         static public MethodInfo system_object_array_clone;
135         static public MethodInfo system_int_array_get_length_int;
136         static public MethodInfo system_int_array_get_lower_bound_int;
137         static public MethodInfo system_int_array_get_upper_bound_int;
138         static public MethodInfo system_void_array_copyto_array_int;
139
140         
141         //
142         // Internal, not really used outside
143         //
144         static Type runtime_helpers_type;
145         
146         //
147         // These methods are called by code generated by the compiler
148         //
149         static public MethodInfo string_concat_string_string;
150         static public MethodInfo string_concat_string_string_string;
151         static public MethodInfo string_concat_string_string_string_string;
152         static public MethodInfo string_concat_string_dot_dot_dot;
153         static public MethodInfo string_concat_object_object;
154         static public MethodInfo string_concat_object_object_object;
155         static public MethodInfo string_concat_object_dot_dot_dot;
156         static public MethodInfo string_isinterneted_string;
157         static public MethodInfo system_type_get_type_from_handle;
158         static public MethodInfo object_getcurrent_void;
159         static public MethodInfo bool_movenext_void;
160         static public MethodInfo ienumerable_getenumerator_void;
161         static public MethodInfo void_reset_void;
162         static public MethodInfo void_dispose_void;
163         static public MethodInfo void_monitor_enter_object;
164         static public MethodInfo void_monitor_exit_object;
165         static public MethodInfo void_initializearray_array_fieldhandle;
166         static public MethodInfo int_getlength_int;
167         static public MethodInfo delegate_combine_delegate_delegate;
168         static public MethodInfo delegate_remove_delegate_delegate;
169         static public MethodInfo int_get_offset_to_string_data;
170         static public MethodInfo int_array_get_length;
171         static public MethodInfo int_array_get_rank;
172         static public MethodInfo object_array_clone;
173         static public MethodInfo int_array_get_length_int;
174         static public MethodInfo int_array_get_lower_bound_int;
175         static public MethodInfo int_array_get_upper_bound_int;
176         static public MethodInfo void_array_copyto_array_int;
177         static public MethodInfo activator_create_instance;
178         
179         //
180         // The attribute constructors.
181         //
182         static public ConstructorInfo object_ctor;
183         static public ConstructorInfo cons_param_array_attribute;
184         static public ConstructorInfo void_decimal_ctor_five_args;
185         static public ConstructorInfo unverifiable_code_ctor;
186         static public ConstructorInfo invalid_operation_ctor;
187         static public ConstructorInfo default_member_ctor;
188         
189         // <remarks>
190         //   Holds the Array of Assemblies that have been loaded
191         //   (either because it is the default or the user used the
192         //   -r command line option)
193         // </remarks>
194         static Assembly [] assemblies;
195
196         // <remarks>
197         //  Keeps a list of modules. We used this to do lookups
198         //  on the module using GetType -- needed for arrays
199         // </remarks>
200         static Module [] modules;
201
202         // <remarks>
203         //   This is the type_cache from the assemblies to avoid
204         //   hitting System.Reflection on every lookup.
205         // </summary>
206         static Hashtable types;
207
208         // <remarks>
209         //  This is used to hotld the corresponding TypeContainer objects
210         //  since we need this in FindMembers
211         // </remarks>
212         static Hashtable typecontainers;
213
214         // <remarks>
215         //   Keeps track of those types that are defined by the
216         //   user's program
217         // </remarks>
218         static ArrayList user_types;
219
220         static PtrHashtable builder_to_declspace;
221
222         // <remarks>
223         //   Tracks the interfaces implemented by typebuilders.  We only
224         //   enter those who do implement or or more interfaces
225         // </remarks>
226         static PtrHashtable builder_to_ifaces;
227
228         // <remarks>
229         //   Tracks the generic parameters.
230         // </remarks>
231         static PtrHashtable builder_to_type_param;
232
233         // <remarks>
234         //   Maps MethodBase.RuntimeTypeHandle to a Type array that contains
235         //   the arguments to the method
236         // </remarks>
237         static Hashtable method_arguments;
238
239         // <remarks>
240         //   Maps PropertyBuilder to a Type array that contains
241         //   the arguments to the indexer
242         // </remarks>
243         static Hashtable indexer_arguments;
244
245         // <remarks>
246         //   Maybe `method_arguments' should be replaced and only
247         //   method_internal_params should be kept?
248         // <remarks>
249         static Hashtable method_internal_params;
250
251         // <remarks>
252         //  Keeps track of methods
253         // </remarks>
254
255         static Hashtable builder_to_method;
256
257         // <remarks>
258         //  Contains all public types from referenced assemblies.
259         //  This member is used only if CLS Compliance verification is required.
260         // </remarks>
261         public static Hashtable all_imported_types;
262
263         struct Signature {
264                 public string name;
265                 public Type [] args;
266         }
267
268         public static void CleanUp ()
269         {
270                 // Lets get everything clean so that we can collect before generating code
271                 assemblies = null;
272                 modules = null;
273                 types = null;
274                 typecontainers = null;
275                 user_types = null;
276                 builder_to_declspace = null;
277                 builder_to_ifaces = null;
278                 method_arguments = null;
279                 indexer_arguments = null;
280                 method_internal_params = null;
281                 builder_to_method = null;
282                 builder_to_type_param = null;
283                 
284                 fields = null;
285                 references = null;
286                 negative_hits = null;
287                 builder_to_constant = null;
288                 fieldbuilders_to_fields = null;
289                 events = null;
290                 priv_fields_events = null;
291                 properties = null;
292                 
293                 TypeHandle.CleanUp ();
294         }
295
296         /// <summary>
297         ///   A filter for Findmembers that uses the Signature object to
298         ///   extract objects
299         /// </summary>
300         static bool SignatureFilter (MemberInfo mi, object criteria)
301         {
302                 Signature sig = (Signature) criteria;
303
304                 if (!(mi is MethodBase))
305                         return false;
306                 
307                 if (mi.Name != sig.name)
308                         return false;
309
310                 int count = sig.args.Length;
311                 
312                 if (mi is MethodBuilder || mi is ConstructorBuilder){
313                         Type [] candidate_args = GetArgumentTypes ((MethodBase) mi);
314
315                         if (candidate_args.Length != count)
316                                 return false;
317                         
318                         for (int i = 0; i < count; i++)
319                                 if (candidate_args [i] != sig.args [i])
320                                         return false;
321                         
322                         return true;
323                 } else {
324                         ParameterInfo [] pars = ((MethodBase) mi).GetParameters ();
325
326                         if (pars.Length != count)
327                                 return false;
328
329                         for (int i = 0; i < count; i++)
330                                 if (pars [i].ParameterType != sig.args [i])
331                                         return false;
332                         return true;
333                 }
334         }
335
336         // A delegate that points to the filter above.
337         static MemberFilter signature_filter;
338
339         //
340         // These are expressions that represent some of the internal data types, used
341         // elsewhere
342         //
343         static void InitExpressionTypes ()
344         {
345                 system_object_expr  = new TypeLookupExpression ("System.Object");
346                 system_string_expr  = new TypeLookupExpression ("System.String");
347                 system_boolean_expr = new TypeLookupExpression ("System.Boolean");
348                 system_decimal_expr = new TypeLookupExpression ("System.Decimal");
349                 system_single_expr  = new TypeLookupExpression ("System.Single");
350                 system_double_expr  = new TypeLookupExpression ("System.Double");
351                 system_sbyte_expr   = new TypeLookupExpression ("System.SByte");
352                 system_byte_expr    = new TypeLookupExpression ("System.Byte");
353                 system_int16_expr   = new TypeLookupExpression ("System.Int16");
354                 system_uint16_expr  = new TypeLookupExpression ("System.UInt16");
355                 system_int32_expr   = new TypeLookupExpression ("System.Int32");
356                 system_uint32_expr  = new TypeLookupExpression ("System.UInt32");
357                 system_int64_expr   = new TypeLookupExpression ("System.Int64");
358                 system_uint64_expr  = new TypeLookupExpression ("System.UInt64");
359                 system_char_expr    = new TypeLookupExpression ("System.Char");
360                 system_void_expr    = new TypeLookupExpression ("System.Void");
361                 system_asynccallback_expr = new TypeLookupExpression ("System.AsyncCallback");
362                 system_iasyncresult_expr = new TypeLookupExpression ("System.IAsyncResult");
363                 system_valuetype_expr  = new TypeLookupExpression ("System.ValueType");
364         }
365
366         static TypeManager ()
367         {
368                 assemblies = new Assembly [0];
369                 modules = null;
370                 user_types = new ArrayList ();
371                 
372                 types = new Hashtable ();
373                 typecontainers = new Hashtable ();
374                 
375                 builder_to_declspace = new PtrHashtable ();
376                 builder_to_method = new PtrHashtable ();
377                 method_arguments = new PtrHashtable ();
378                 method_internal_params = new PtrHashtable ();
379                 indexer_arguments = new PtrHashtable ();
380                 builder_to_ifaces = new PtrHashtable ();
381                 builder_to_type_param = new PtrHashtable ();
382                 
383                 NoTypes = new Type [0];
384                 NoTypeExprs = new TypeExpr [0];
385
386                 signature_filter = new MemberFilter (SignatureFilter);
387                 InitExpressionTypes ();
388         }
389
390         public static void HandleDuplicate (string name, Type t)
391         {
392                 Type prev = (Type) types [name];
393                 TypeContainer tc = builder_to_declspace [prev] as TypeContainer;
394                 
395                 if (tc != null){
396                         //
397                         // This probably never happens, as we catch this before
398                         //
399                         Report.Error (-17, "The type `" + name + "' has already been defined.");
400                         return;
401                 }
402                 
403                 tc = builder_to_declspace [t] as TypeContainer;
404                 if (tc != null){
405                         Report.Warning (
406                                         1595, "The type `" + name + "' is defined in an existing assembly;"+
407                                         " Using the new definition from: " + tc.Location);
408                 } else {
409                         Report.Warning (
410                                         1595, "The type `" + name + "' is defined in an existing assembly;");
411                 }
412                 
413                 Report.Warning (1595, "Previously defined in: " + prev.Assembly.FullName);
414                 
415                 types.Remove (name);
416                 types.Add (name, t);
417         }
418         
419         public static void AddUserType (string name, TypeBuilder t)
420         {
421                 try {
422                         types.Add (name, t);
423                 } catch {
424                         HandleDuplicate (name, t); 
425                 }
426                 user_types.Add (t);
427         }
428
429         //
430         // This entry point is used by types that we define under the covers
431         // 
432         public static void RegisterBuilder (Type tb, Type [] ifaces)
433         {
434                 if (ifaces != null)
435                         builder_to_ifaces [tb] = ifaces;
436         }
437         
438         public static void AddUserType (string name, TypeBuilder t, TypeContainer tc)
439         {
440                 builder_to_declspace.Add (t, tc);
441                 typecontainers.Add (name, tc);
442                 AddUserType (name, t);
443         }
444
445         public static void AddDelegateType (string name, TypeBuilder t, Delegate del)
446         {
447                 try {
448                         types.Add (name, t);
449                 } catch {
450                         HandleDuplicate (name, t);
451                 }
452                 
453                 builder_to_declspace.Add (t, del);
454         }
455         
456         public static void AddEnumType (string name, TypeBuilder t, Enum en)
457         {
458                 try {
459                         types.Add (name, t);
460                 } catch {
461                         HandleDuplicate (name, t);
462                 }
463                 builder_to_declspace.Add (t, en);
464         }
465
466         public static void AddMethod (MethodBase builder, IMethodData method)
467         {
468                 builder_to_method.Add (builder, method);
469         }
470
471         public static IMethodData GetMethod (MethodBase builder)
472         {
473                 return (IMethodData) builder_to_method [builder];
474         }
475
476         public static void AddTypeParameter (Type t, TypeParameter tparam)
477         {
478                 if (!builder_to_type_param.Contains (t))
479                         builder_to_type_param.Add (t, tparam);
480         }
481
482         /// <summary>
483         ///   Returns the DeclSpace whose Type is `t' or null if there is no
484         ///   DeclSpace for `t' (ie, the Type comes from a library)
485         /// </summary>
486         public static DeclSpace LookupDeclSpace (Type t)
487         {
488                 return builder_to_declspace [t] as DeclSpace;
489         }
490
491         /// <summary>
492         ///   Returns the TypeContainer whose Type is `t' or null if there is no
493         ///   TypeContainer for `t' (ie, the Type comes from a library)
494         /// </summary>
495         public static TypeContainer LookupTypeContainer (Type t)
496         {
497                 return builder_to_declspace [t] as TypeContainer;
498         }
499         
500         public static IMemberContainer LookupMemberContainer (Type t)
501         {
502                 if (t is TypeBuilder) {
503                         IMemberContainer container = builder_to_declspace [t] as IMemberContainer;
504                         if (container != null)
505                                 return container;
506                 }
507
508                 if (t is GenericTypeParameterBuilder) {
509                         IMemberContainer container = builder_to_type_param [t] as IMemberContainer;
510
511                         if (container != null)
512                                 return container;
513                 }
514
515                 return TypeHandle.GetTypeHandle (t);
516         }
517
518         public static TypeContainer LookupInterface (Type t)
519         {
520                 TypeContainer tc = (TypeContainer) builder_to_declspace [t];
521                 if ((tc == null) || (tc.Kind != Kind.Interface))
522                         return null;
523
524                 return tc;
525         }
526
527         public static Delegate LookupDelegate (Type t)
528         {
529                 return builder_to_declspace [t] as Delegate;
530         }
531
532         public static Enum LookupEnum (Type t)
533         {
534                 return builder_to_declspace [t] as Enum;
535         }
536         
537         public static Class LookupClass (Type t)
538         {
539                 return (Class) builder_to_declspace [t];
540         }
541
542         public static TypeParameter LookupTypeParameter (Type t)
543         {
544                 return (TypeParameter) builder_to_type_param [t];
545         }
546
547         public static bool HasConstructorConstraint (Type t)
548         {
549                 if (!t.IsGenericParameter)
550                         throw new InvalidOperationException ();
551
552                 TypeParameter tparam = LookupTypeParameter (t);
553                 if (tparam != null)
554                         return tparam.HasConstructorConstraint;
555                 else {
556                         object[] attrs = t.GetCustomAttributes (
557                                 TypeManager.new_constraint_attr_type, false);
558
559                         return attrs.Length > 0;
560                 }
561         }
562         
563         /// <summary>
564         ///   Registers an assembly to load types from.
565         /// </summary>
566         public static void AddAssembly (Assembly a)
567         {
568                 foreach (Assembly assembly in assemblies) {
569                         if (a == assembly)
570                                 return;
571                 }
572
573                 int top = assemblies.Length;
574                 Assembly [] n = new Assembly [top + 1];
575
576                 assemblies.CopyTo (n, 0);
577                 
578                 n [top] = a;
579                 assemblies = n;
580         }
581
582         public static Assembly [] GetAssemblies ()
583         {
584                 return assemblies;
585         }
586
587         /// <summary>
588         ///  Registers a module builder to lookup types from
589         /// </summary>
590         public static void AddModule (Module mb)
591         {
592                 int top = modules != null ? modules.Length : 0;
593                 Module [] n = new Module [top + 1];
594
595                 if (modules != null)
596                         modules.CopyTo (n, 0);
597                 n [top] = mb;
598                 modules = n;
599         }
600
601         public static Module[] Modules {
602                 get {
603                         return modules;
604                 }
605         }
606
607         static Hashtable references = new Hashtable ();
608         
609         //
610         // Gets the reference to T version of the Type (T&)
611         //
612         public static Type GetReferenceType (Type t)
613         {
614                 return t.MakeByRefType ();
615         }
616
617         static Hashtable pointers = new Hashtable ();
618
619         //
620         // Gets the pointer to T version of the Type  (T*)
621         //
622         public static Type GetPointerType (Type t)
623         {
624                 string tname = t.FullName + "*";
625                 
626                 Type ret = t.Assembly.GetType (tname);
627                 
628                 //
629                 // If the type comes from the assembly we are building
630                 // We need the Hashtable, because .NET 1.1 will return different instance types
631                 // every time we call ModuleBuilder.GetType.
632                 //
633                 if (ret == null){
634                         if (pointers [t] == null)
635                                 pointers [t] = CodeGen.Module.Builder.GetType (tname);
636                         
637                         ret = (Type) pointers [t];
638                 }
639
640                 return ret;
641         }
642         
643         //
644         // Low-level lookup, cache-less
645         //
646         static Type LookupTypeReflection (string name)
647         {
648                 Type t;
649
650                 foreach (Assembly a in assemblies){
651                         t = a.GetType (name);
652                         if (t == null)
653                                 continue;
654
655                         do {
656                                 TypeAttributes ta = t.Attributes & TypeAttributes.VisibilityMask;
657                                 if (ta == TypeAttributes.NotPublic ||
658                                     ta == TypeAttributes.NestedPrivate ||
659                                     ta == TypeAttributes.NestedAssembly ||
660                                     ta == TypeAttributes.NestedFamANDAssem){
661                                         
662                                         //
663                                         // In .NET pointers turn out to be private, even if their
664                                         // element type is not
665                                         //
666                                         if (t.IsPointer){
667                                                 t = t.GetElementType ();
668                                                 continue;
669                                         } else
670                                                 t = null;
671                                 } else {
672                                         return t;
673                                 }
674                         } while (t != null);
675                 }
676
677                 foreach (Module mb in modules) {
678                         t = mb.GetType (name);
679                         if (t != null) 
680                                 return t;
681                 }
682                         
683                 return null;
684         }
685
686         static Hashtable negative_hits = new Hashtable ();
687         
688         //
689         // This function is used when you want to avoid the lookups, and want to go
690         // directly to the source.  This will use the cache.
691         //
692         // Notice that bypassing the cache is bad, because on Microsoft.NET runtime
693         // GetType ("DynamicType[]") != GetType ("DynamicType[]"), and there is no
694         // way to test things other than doing a fullname compare
695         //
696         public static Type LookupTypeDirect (string name)
697         {
698                 Type t = (Type) types [name];
699                 if (t != null)
700                         return t;
701
702                 t = LookupTypeReflection (name);
703                 if (t == null)
704                         return null;
705
706                 types [name] = t;
707                 return t;
708         }
709
710         static readonly char [] dot_array = { '.' };
711
712         /// <summary>
713         ///   Returns the Type associated with @name, takes care of the fact that
714         ///   reflection expects nested types to be separated from the main type
715         ///   with a "+" instead of a "."
716         /// </summary>
717         public static Type LookupType (string name)
718         {
719                 Type t;
720
721                 //
722                 // First lookup in user defined and cached values
723                 //
724
725                 t = (Type) types [name];
726                 if (t != null)
727                         return t;
728
729                 // Two thirds of the failures are caught here.
730                 if (negative_hits.Contains (name))
731                         return null;
732
733                 // Sadly, split takes a param array, so this ends up allocating *EVERY TIME*
734                 string [] elements = name.Split (dot_array);
735                 int count = elements.Length;
736
737                 for (int n = 1; n <= count; n++){
738                         string top_level_type = String.Join (".", elements, 0, n);
739
740                         // One third of the failures are caught here.
741                         if (negative_hits.Contains (top_level_type))
742                                 continue;
743                         
744                         t = (Type) types [top_level_type];
745                         if (t == null){
746                                 t = LookupTypeReflection (top_level_type);
747                                 if (t == null){
748                                         negative_hits [top_level_type] = null;
749                                         continue;
750                                 }
751                         }
752                         
753                         if (count == n){
754                                 types [name] = t;
755                                 return t;
756                         } 
757
758                         //
759                         // We know that System.Object does not have children, and since its the parent of 
760                         // all the objects, it always gets probbed for inner classes. 
761                         //
762                         if (top_level_type == "System.Object")
763                                 return null;
764                         
765                         string newt = top_level_type + "+" + String.Join ("+", elements, n, count - n);
766                         //Console.WriteLine ("Looking up: " + newt + " " + name);
767                         t = LookupTypeReflection (newt);
768                         if (t == null)
769                                 negative_hits [name] = null;
770                         else
771                                 types [name] = t;
772                         return t;
773                 }
774                 negative_hits [name] = null;
775                 return null;
776         }
777
778         /// <summary>
779         ///   Computes the namespaces that we import from the assemblies we reference.
780         /// </summary>
781         public static void ComputeNamespaces ()
782         {
783                 MethodInfo assembly_get_namespaces = typeof (Assembly).GetMethod ("GetNamespaces", BindingFlags.Instance|BindingFlags.NonPublic);
784
785                 //
786                 // First add the assembly namespaces
787                 //
788                 if (assembly_get_namespaces != null){
789                         int count = assemblies.Length;
790
791                         for (int i = 0; i < count; i++){
792                                 Assembly a = assemblies [i];
793                                 string [] namespaces = (string []) assembly_get_namespaces.Invoke (a, null);
794                                 foreach (string ns in namespaces){
795                                         if (ns == "")
796                                                 continue;
797                                         Namespace.LookupNamespace (ns, true);
798                                 }
799                         }
800                 } else {
801                         Hashtable cache = new Hashtable ();
802                         cache.Add ("", null);
803                         foreach (Assembly a in assemblies) {
804                                 foreach (Type t in a.GetExportedTypes ()) {
805                                         string ns = t.Namespace;
806                                         if (ns == null || cache.Contains (ns))
807                                                 continue;
808
809                                         Namespace.LookupNamespace (ns, true);
810                                         cache.Add (ns, null);
811                                 }
812                         }
813                 }
814         }
815
816         /// <summary>
817         /// Fills static table with exported types from all referenced assemblies.
818         /// This information is required for CLS Compliance tests.
819         /// </summary>
820         public static void LoadAllImportedTypes ()
821         {
822                 all_imported_types = new Hashtable ();
823                 foreach (Assembly a in assemblies) {
824                         foreach (Type t in a.GetExportedTypes ()) {
825                                 all_imported_types [t.FullName] = t;
826                         }
827                 }
828         }
829
830         public static bool NamespaceClash (string name, Location loc)
831         {
832                 if (Namespace.LookupNamespace (name, false) == null)
833                         return false;
834
835                 Report.Error (519, loc, String.Format ("`{0}' clashes with a predefined namespace", name));
836                 return true;
837         }
838
839         /// <summary>
840         ///   Returns the C# name of a type if possible, or the full type name otherwise
841         /// </summary>
842         static public string CSharpName (Type t)
843         {
844                 if (t.FullName == null)
845                         return t.Name;
846
847                 return Regex.Replace (t.FullName, 
848                         @"^System\." +
849                         @"(Int32|UInt32|Int16|UInt16|Int64|UInt64|" +
850                         @"Single|Double|Char|Decimal|Byte|SByte|Object|" +
851                         @"Boolean|String|Void|Null)" +
852                         @"(\W+|\b)", 
853                         new MatchEvaluator (CSharpNameMatch));
854         }       
855         
856         static String CSharpNameMatch (Match match) 
857         {
858                 string s = match.Groups [1].Captures [0].Value;
859                 return s.ToLower ().
860                 Replace ("int32", "int").
861                 Replace ("uint32", "uint").
862                 Replace ("int16", "short").
863                 Replace ("uint16", "ushort").
864                 Replace ("int64", "long").
865                 Replace ("uint64", "ulong").
866                 Replace ("single", "float").
867                 Replace ("boolean", "bool")
868                 + match.Groups [2].Captures [0].Value;
869         }
870
871         /// <summary>
872         ///  Returns the signature of the method with full namespace classification
873         /// </summary>
874         static public string GetFullNameSignature (MemberInfo mi)
875         {
876                 return mi.DeclaringType.FullName.Replace ('+', '.') + '.' + mi.Name;
877         }
878                 
879         static public string GetFullNameSignature (MethodBase mb)
880         {
881                 string name = mb.Name;
882                 if (name == ".ctor")
883                         name = mb.DeclaringType.Name;
884
885                 if (mb.IsSpecialName) {
886                         if (name.StartsWith ("get_") || name.StartsWith ("set_")) {
887                                 name = name.Remove (0, 4);
888                         }
889
890                         if (name == "Item")
891                                 name = "this";
892                 }
893
894                 return mb.DeclaringType.FullName.Replace ('+', '.') + '.' + name;
895         }
896
897         static public string GetFullName (Type t)
898         {
899                 if (t.FullName == null)
900                         return t.Name;
901
902                 string name = t.FullName.Replace ('+', '.');
903
904                 DeclSpace tc = LookupDeclSpace (t);
905                 if ((tc != null) && tc.IsGeneric) {
906                         TypeParameter[] tparam = tc.TypeParameters;
907
908                         StringBuilder sb = new StringBuilder (name);
909                         sb.Append ("<");
910                         for (int i = 0; i < tparam.Length; i++) {
911                                 if (i > 0)
912                                         sb.Append (",");
913                                 sb.Append (tparam [i].Name);
914                         }
915                         sb.Append (">");
916                         return sb.ToString ();
917                 } else if (t.HasGenericArguments && !t.IsGenericInstance) {
918                         Type[] tparam = t.GetGenericArguments ();
919
920                         StringBuilder sb = new StringBuilder (name);
921                         sb.Append ("<");
922                         for (int i = 0; i < tparam.Length; i++) {
923                                 if (i > 0)
924                                         sb.Append (",");
925                                 sb.Append (tparam [i].Name);
926                         }
927                         sb.Append (">");
928                         return sb.ToString ();
929                 }
930
931                 return name;
932         }
933
934         /// <summary>
935         ///   Returns the signature of the property and indexer
936         /// </summary>
937         static public string CSharpSignature (PropertyBuilder pb, bool is_indexer) 
938         {
939                 if (!is_indexer) {
940                         return GetFullNameSignature (pb);
941                 }
942
943                 MethodBase mb = pb.GetSetMethod (true) != null ? pb.GetSetMethod (true) : pb.GetGetMethod (true);
944                 string signature = GetFullNameSignature (mb);
945                 string arg = TypeManager.LookupParametersByBuilder (mb).ParameterDesc (0);
946                 return String.Format ("{0}.this[{1}]", signature.Substring (0, signature.LastIndexOf ('.')), arg);
947         }
948
949         /// <summary>
950         ///   Returns the signature of the method
951         /// </summary>
952         static public string CSharpSignature (MethodBase mb)
953         {
954                 StringBuilder sig = new StringBuilder ("(");
955
956                 //
957                 // FIXME: We should really have a single function to do
958                 // everything instead of the following 5 line pattern
959                 //
960                 ParameterData iparams = LookupParametersByBuilder (mb);
961
962                 if (iparams == null)
963                         iparams = new ReflectionParameters (mb);
964                 
965                 // Is property
966                 if (mb.IsSpecialName && iparams.Count == 0 && !mb.IsConstructor)
967                         return GetFullNameSignature (mb);
968                 
969                 for (int i = 0; i < iparams.Count; i++) {
970                         if (i > 0) {
971                                 sig.Append (", ");
972                         }
973                         sig.Append (iparams.ParameterDesc (i));
974                 }
975                 sig.Append (")");
976
977                 // Is indexer
978                 if (mb.IsSpecialName && iparams.Count == 1 && !mb.IsConstructor) {
979                         sig.Replace ('(', '[');
980                         sig.Replace (')', ']');
981                 }
982
983                 return GetFullNameSignature (mb) + sig.ToString ();
984         }
985
986         /// <summary>
987         ///   Looks up a type, and aborts if it is not found.  This is used
988         ///   by types required by the compiler
989         /// </summary>
990         static Type CoreLookupType (string name)
991         {
992                 Type t = LookupTypeDirect (name);
993
994                 if (t == null){
995                         Report.Error (518, "The predefined type `" + name + "' is not defined or imported");
996                         Environment.Exit (1);
997                 }
998
999                 return t;
1000         }
1001
1002         /// <summary>
1003         ///   Returns the MethodInfo for a method named `name' defined
1004         ///   in type `t' which takes arguments of types `args'
1005         /// </summary>
1006         static MethodInfo GetMethod (Type t, string name, Type [] args, bool is_private, bool report_errors)
1007         {
1008                 MemberList list;
1009                 Signature sig;
1010                 BindingFlags flags = instance_and_static | BindingFlags.Public;
1011
1012                 sig.name = name;
1013                 sig.args = args;
1014                 
1015                 if (is_private)
1016                         flags |= BindingFlags.NonPublic;
1017
1018                 list = FindMembers (t, MemberTypes.Method, flags, signature_filter, sig);
1019                 if (list.Count == 0) {
1020                         if (report_errors)
1021                                 Report.Error (-19, "Can not find the core function `" + name + "'");
1022                         return null;
1023                 }
1024
1025                 MethodInfo mi = list [0] as MethodInfo;
1026                 if (mi == null) {
1027                         if (report_errors)
1028                                 Report.Error (-19, "Can not find the core function `" + name + "'");
1029                         return null;
1030                 }
1031
1032                 return mi;
1033         }
1034
1035         static MethodInfo GetMethod (Type t, string name, Type [] args, bool report_errors)
1036         {
1037                 return GetMethod (t, name, args, false, report_errors);
1038         }
1039
1040         static MethodInfo GetMethod (Type t, string name, Type [] args)
1041         {
1042                 return GetMethod (t, name, args, true);
1043         }
1044
1045
1046         /// <summary>
1047         ///    Returns the ConstructorInfo for "args"
1048         /// </summary>
1049         static ConstructorInfo GetConstructor (Type t, Type [] args)
1050         {
1051                 MemberList list;
1052                 Signature sig;
1053
1054                 sig.name = ".ctor";
1055                 sig.args = args;
1056                 
1057                 list = FindMembers (t, MemberTypes.Constructor,
1058                                     instance_and_static | BindingFlags.Public | BindingFlags.DeclaredOnly,
1059                                     signature_filter, sig);
1060                 if (list.Count == 0){
1061                         Report.Error (-19, "Can not find the core constructor for type `" + t.Name + "'");
1062                         return null;
1063                 }
1064
1065                 ConstructorInfo ci = list [0] as ConstructorInfo;
1066                 if (ci == null){
1067                         Report.Error (-19, "Can not find the core constructor for type `" + t.Name + "'");
1068                         return null;
1069                 }
1070
1071                 return ci;
1072         }
1073
1074         public static void InitEnumUnderlyingTypes ()
1075         {
1076
1077                 int32_type    = CoreLookupType ("System.Int32");
1078                 int64_type    = CoreLookupType ("System.Int64");
1079                 uint32_type   = CoreLookupType ("System.UInt32"); 
1080                 uint64_type   = CoreLookupType ("System.UInt64"); 
1081                 byte_type     = CoreLookupType ("System.Byte");
1082                 sbyte_type    = CoreLookupType ("System.SByte");
1083                 short_type    = CoreLookupType ("System.Int16");
1084                 ushort_type   = CoreLookupType ("System.UInt16");
1085         }
1086         
1087         /// <remarks>
1088         ///   The types have to be initialized after the initial
1089         ///   population of the type has happened (for example, to
1090         ///   bootstrap the corlib.dll
1091         /// </remarks>
1092         public static void InitCoreTypes ()
1093         {
1094                 object_type   = CoreLookupType ("System.Object");
1095                 value_type    = CoreLookupType ("System.ValueType");
1096
1097                 InitEnumUnderlyingTypes ();
1098
1099                 char_type     = CoreLookupType ("System.Char");
1100                 string_type   = CoreLookupType ("System.String");
1101                 float_type    = CoreLookupType ("System.Single");
1102                 double_type   = CoreLookupType ("System.Double");
1103                 char_ptr_type = CoreLookupType ("System.Char*");
1104                 decimal_type  = CoreLookupType ("System.Decimal");
1105                 bool_type     = CoreLookupType ("System.Boolean");
1106                 enum_type     = CoreLookupType ("System.Enum");
1107
1108                 multicast_delegate_type = CoreLookupType ("System.MulticastDelegate");
1109                 delegate_type           = CoreLookupType ("System.Delegate");
1110
1111                 array_type    = CoreLookupType ("System.Array");
1112                 void_type     = CoreLookupType ("System.Void");
1113                 type_type     = CoreLookupType ("System.Type");
1114
1115                 runtime_field_handle_type = CoreLookupType ("System.RuntimeFieldHandle");
1116                 runtime_argument_handle_type = CoreLookupType ("System.RuntimeArgumentHandle");
1117                 runtime_helpers_type = CoreLookupType ("System.Runtime.CompilerServices.RuntimeHelpers");
1118                 default_member_type  = CoreLookupType ("System.Reflection.DefaultMemberAttribute");
1119                 runtime_handle_type  = CoreLookupType ("System.RuntimeTypeHandle");
1120                 asynccallback_type   = CoreLookupType ("System.AsyncCallback");
1121                 iasyncresult_type    = CoreLookupType ("System.IAsyncResult");
1122                 ienumerator_type     = CoreLookupType ("System.Collections.IEnumerator");
1123                 ienumerable_type     = CoreLookupType ("System.Collections.IEnumerable");
1124                 idisposable_type     = CoreLookupType ("System.IDisposable");
1125                 icloneable_type      = CoreLookupType ("System.ICloneable");
1126                 iconvertible_type    = CoreLookupType ("System.IConvertible");
1127                 monitor_type         = CoreLookupType ("System.Threading.Monitor");
1128                 intptr_type          = CoreLookupType ("System.IntPtr");
1129
1130                 attribute_type       = CoreLookupType ("System.Attribute");
1131                 attribute_usage_type = CoreLookupType ("System.AttributeUsageAttribute");
1132                 dllimport_type       = CoreLookupType ("System.Runtime.InteropServices.DllImportAttribute");
1133                 methodimpl_attr_type = CoreLookupType ("System.Runtime.CompilerServices.MethodImplAttribute");
1134                 marshal_as_attr_type = CoreLookupType ("System.Runtime.InteropServices.MarshalAsAttribute");
1135                 new_constraint_attr_type = CoreLookupType ("System.Runtime.CompilerServices.NewConstraintAttribute");
1136                 param_array_type     = CoreLookupType ("System.ParamArrayAttribute");
1137                 in_attribute_type    = CoreLookupType ("System.Runtime.InteropServices.InAttribute");
1138                 typed_reference_type = CoreLookupType ("System.TypedReference");
1139                 arg_iterator_type    = CoreLookupType ("System.ArgIterator");
1140                 mbr_type             = CoreLookupType ("System.MarshalByRefObject");
1141
1142                 //
1143                 // Sigh. Remove this before the release.  Wonder what versions of Mono
1144                 // people are running.
1145                 //
1146                 guid_attr_type        = LookupType ("System.Runtime.InteropServices.GuidAttribute");
1147
1148                 unverifiable_code_type= CoreLookupType ("System.Security.UnverifiableCodeAttribute");
1149
1150                 void_ptr_type         = CoreLookupType ("System.Void*");
1151
1152                 indexer_name_type     = CoreLookupType ("System.Runtime.CompilerServices.IndexerNameAttribute");
1153
1154                 exception_type        = CoreLookupType ("System.Exception");
1155                 activator_type        = CoreLookupType ("System.Activator");
1156                 invalid_operation_exception_type = CoreLookupType ("System.InvalidOperationException");
1157                 not_supported_exception_type = CoreLookupType ("System.NotSupportedException");
1158
1159                 //
1160                 // Attribute types
1161                 //
1162                 obsolete_attribute_type = CoreLookupType ("System.ObsoleteAttribute");
1163                 conditional_attribute_type = CoreLookupType ("System.Diagnostics.ConditionalAttribute");
1164                 cls_compliant_attribute_type = CoreLookupType ("System.CLSCompliantAttribute");
1165                 struct_layout_attribute_type = CoreLookupType ("System.Runtime.InteropServices.StructLayoutAttribute");
1166                 field_offset_attribute_type = CoreLookupType ("System.Runtime.InteropServices.FieldOffsetAttribute");
1167
1168                 //
1169                 // Generic types
1170                 //
1171                 generic_ienumerator_type     = CoreLookupType (MemberName.MakeName ("System.Collections.Generic.IEnumerator", 1));
1172                 generic_ienumerable_type     = CoreLookupType (MemberName.MakeName ("System.Collections.Generic.IEnumerable", 1));
1173
1174
1175                 //
1176                 // When compiling corlib, store the "real" types here.
1177                 //
1178                 if (!RootContext.StdLib) {
1179                         system_int32_type = typeof (System.Int32);
1180                         system_array_type = typeof (System.Array);
1181                         system_type_type = typeof (System.Type);
1182                         system_assemblybuilder_type = typeof (System.Reflection.Emit.AssemblyBuilder);
1183
1184                         Type [] void_arg = {  };
1185                         system_int_array_get_length = GetMethod (
1186                                 system_array_type, "get_Length", void_arg);
1187                         system_int_array_get_rank = GetMethod (
1188                                 system_array_type, "get_Rank", void_arg);
1189                         system_object_array_clone = GetMethod (
1190                                 system_array_type, "Clone", void_arg);
1191
1192                         Type [] system_int_arg = { system_int32_type };
1193                         system_int_array_get_length_int = GetMethod (
1194                                 system_array_type, "GetLength", system_int_arg);
1195                         system_int_array_get_upper_bound_int = GetMethod (
1196                                 system_array_type, "GetUpperBound", system_int_arg);
1197                         system_int_array_get_lower_bound_int = GetMethod (
1198                                 system_array_type, "GetLowerBound", system_int_arg);
1199
1200                         Type [] system_array_int_arg = { system_array_type, system_int32_type };
1201                         system_void_array_copyto_array_int = GetMethod (
1202                                 system_array_type, "CopyTo", system_array_int_arg);
1203
1204                         Type [] system_3_type_arg = {
1205                                 system_type_type, system_type_type, system_type_type };
1206                         Type [] system_4_type_arg = {
1207                                 system_type_type, system_type_type, system_type_type, system_type_type };
1208
1209                         MethodInfo set_corlib_type_builders = GetMethod (
1210                                 system_assemblybuilder_type, "SetCorlibTypeBuilders",
1211                                 system_4_type_arg, true, false);
1212
1213                         if (set_corlib_type_builders != null) {
1214                                 object[] args = new object [4];
1215                                 args [0] = object_type;
1216                                 args [1] = value_type;
1217                                 args [2] = enum_type;
1218                                 args [3] = void_type;
1219                                 
1220                                 set_corlib_type_builders.Invoke (CodeGen.Assembly.Builder, args);
1221                         } else {
1222                                 // Compatibility for an older version of the class libs.
1223                                 set_corlib_type_builders = GetMethod (
1224                                         system_assemblybuilder_type, "SetCorlibTypeBuilders",
1225                                         system_3_type_arg, true, true);
1226
1227                                 if (set_corlib_type_builders == null) {
1228                                         Report.Error (-26, "Corlib compilation is not supported in Microsoft.NET due to bugs in it");
1229                                         return;
1230                                 }
1231
1232                                 object[] args = new object [3];
1233                                 args [0] = object_type;
1234                                 args [1] = value_type;
1235                                 args [2] = enum_type;
1236                                 
1237                                 set_corlib_type_builders.Invoke (CodeGen.Assembly.Builder, args);
1238                         }
1239                 }
1240
1241                 system_object_expr.Type = object_type;
1242                 system_string_expr.Type = string_type;
1243                 system_boolean_expr.Type = bool_type;
1244                 system_decimal_expr.Type = decimal_type;
1245                 system_single_expr.Type = float_type;
1246                 system_double_expr.Type = double_type;
1247                 system_sbyte_expr.Type = sbyte_type;
1248                 system_byte_expr.Type = byte_type;
1249                 system_int16_expr.Type = short_type;
1250                 system_uint16_expr.Type = ushort_type;
1251                 system_int32_expr.Type = int32_type;
1252                 system_uint32_expr.Type = uint32_type;
1253                 system_int64_expr.Type = int64_type;
1254                 system_uint64_expr.Type = uint64_type;
1255                 system_char_expr.Type = char_type;
1256                 system_void_expr.Type = void_type;
1257                 system_asynccallback_expr.Type = asynccallback_type;
1258                 system_iasyncresult_expr.Type = iasyncresult_type;
1259                 system_valuetype_expr.Type = value_type;
1260         }
1261
1262         //
1263         // The helper methods that are used by the compiler
1264         //
1265         public static void InitCodeHelpers ()
1266         {
1267                 //
1268                 // Now load the default methods that we use.
1269                 //
1270                 Type [] string_string = { string_type, string_type };
1271                 string_concat_string_string = GetMethod (
1272                         string_type, "Concat", string_string);
1273                 Type [] string_string_string = { string_type, string_type, string_type };
1274                 string_concat_string_string_string = GetMethod (
1275                         string_type, "Concat", string_string_string);
1276                 Type [] string_string_string_string = { string_type, string_type, string_type, string_type };
1277                 string_concat_string_string_string_string = GetMethod (
1278                         string_type, "Concat", string_string_string_string);
1279                 Type[] params_string = { TypeManager.LookupType ("System.String[]") };
1280                 string_concat_string_dot_dot_dot = GetMethod (
1281                         string_type, "Concat", params_string);
1282
1283                 Type [] object_object = { object_type, object_type };
1284                 string_concat_object_object = GetMethod (
1285                         string_type, "Concat", object_object);
1286                 Type [] object_object_object = { object_type, object_type, object_type };
1287                 string_concat_object_object_object = GetMethod (
1288                         string_type, "Concat", object_object_object);
1289                 Type[] params_object = { TypeManager.LookupType ("System.Object[]") };
1290                 string_concat_object_dot_dot_dot = GetMethod (
1291                         string_type, "Concat", params_object);
1292
1293                 Type [] string_ = { string_type };
1294                 string_isinterneted_string = GetMethod (
1295                         string_type, "IsInterned", string_);
1296                 
1297                 Type [] runtime_type_handle = { runtime_handle_type };
1298                 system_type_get_type_from_handle = GetMethod (
1299                         type_type, "GetTypeFromHandle", runtime_type_handle);
1300
1301                 Type [] delegate_delegate = { delegate_type, delegate_type };
1302                 delegate_combine_delegate_delegate = GetMethod (
1303                                 delegate_type, "Combine", delegate_delegate);
1304
1305                 delegate_remove_delegate_delegate = GetMethod (
1306                                 delegate_type, "Remove", delegate_delegate);
1307
1308                 //
1309                 // Void arguments
1310                 //
1311                 Type [] void_arg = {  };
1312                 object_getcurrent_void = GetMethod (
1313                         ienumerator_type, "get_Current", void_arg);
1314                 bool_movenext_void = GetMethod (
1315                         ienumerator_type, "MoveNext", void_arg);
1316                 void_reset_void = GetMethod (
1317                         ienumerator_type, "Reset", void_arg);
1318                 void_dispose_void = GetMethod (
1319                         idisposable_type, "Dispose", void_arg);
1320                 int_get_offset_to_string_data = GetMethod (
1321                         runtime_helpers_type, "get_OffsetToStringData", void_arg);
1322                 int_array_get_length = GetMethod (
1323                         array_type, "get_Length", void_arg);
1324                 int_array_get_rank = GetMethod (
1325                         array_type, "get_Rank", void_arg);
1326                 ienumerable_getenumerator_void = GetMethod (
1327                         ienumerable_type, "GetEnumerator", void_arg);
1328                 
1329                 //
1330                 // Int32 arguments
1331                 //
1332                 Type [] int_arg = { int32_type };
1333                 int_array_get_length_int = GetMethod (
1334                         array_type, "GetLength", int_arg);
1335                 int_array_get_upper_bound_int = GetMethod (
1336                         array_type, "GetUpperBound", int_arg);
1337                 int_array_get_lower_bound_int = GetMethod (
1338                         array_type, "GetLowerBound", int_arg);
1339
1340                 //
1341                 // System.Array methods
1342                 //
1343                 object_array_clone = GetMethod (
1344                         array_type, "Clone", void_arg);
1345                 Type [] array_int_arg = { array_type, int32_type };
1346                 void_array_copyto_array_int = GetMethod (
1347                         array_type, "CopyTo", array_int_arg);
1348                 
1349                 //
1350                 // object arguments
1351                 //
1352                 Type [] object_arg = { object_type };
1353                 void_monitor_enter_object = GetMethod (
1354                         monitor_type, "Enter", object_arg);
1355                 void_monitor_exit_object = GetMethod (
1356                         monitor_type, "Exit", object_arg);
1357
1358                 Type [] array_field_handle_arg = { array_type, runtime_field_handle_type };
1359                 
1360                 void_initializearray_array_fieldhandle = GetMethod (
1361                         runtime_helpers_type, "InitializeArray", array_field_handle_arg);
1362
1363                 //
1364                 // Array functions
1365                 //
1366                 int_getlength_int = GetMethod (
1367                         array_type, "GetLength", int_arg);
1368
1369                 //
1370                 // Decimal constructors
1371                 //
1372                 Type [] dec_arg = { int32_type, int32_type, int32_type, bool_type, byte_type };
1373                 void_decimal_ctor_five_args = GetConstructor (
1374                         decimal_type, dec_arg);
1375                 
1376                 //
1377                 // Attributes
1378                 //
1379                 cons_param_array_attribute = GetConstructor (
1380                         param_array_type, void_arg);
1381
1382                 unverifiable_code_ctor = GetConstructor (
1383                         unverifiable_code_type, void_arg);
1384
1385                 default_member_ctor = GetConstructor (default_member_type, string_);
1386
1387                 //
1388                 // InvalidOperationException
1389                 //
1390                 invalid_operation_ctor = GetConstructor (
1391                         invalid_operation_exception_type, void_arg);
1392
1393
1394                 // Object
1395                 object_ctor = GetConstructor (object_type, void_arg);
1396
1397                 // Activator
1398                 Type [] type_arg = { type_type };
1399                 activator_create_instance = GetMethod (
1400                         activator_type, "CreateInstance", type_arg);
1401         }
1402
1403         const BindingFlags instance_and_static = BindingFlags.Static | BindingFlags.Instance;
1404
1405         /// <remarks>
1406         ///   This is the "old", non-cache based FindMembers() function.  We cannot use
1407         ///   the cache here because there is no member name argument.
1408         /// </remarks>
1409         public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1410                                               MemberFilter filter, object criteria)
1411         {
1412                 DeclSpace decl = (DeclSpace) builder_to_declspace [t];
1413
1414                 //
1415                 // `builder_to_declspace' contains all dynamic types.
1416                 //
1417                 if (decl != null) {
1418                         MemberList list;
1419                         Timer.StartTimer (TimerType.FindMembers);
1420                         list = decl.FindMembers (mt, bf, filter, criteria);
1421                         Timer.StopTimer (TimerType.FindMembers);
1422                         return list;
1423                 }
1424
1425                 //
1426                 // We have to take care of arrays specially, because GetType on
1427                 // a TypeBuilder array will return a Type, not a TypeBuilder,
1428                 // and we can not call FindMembers on this type.
1429                 //
1430                 if (t.IsSubclassOf (TypeManager.array_type))
1431                         return new MemberList (TypeManager.array_type.FindMembers (mt, bf, filter, criteria));
1432
1433                 if (t is GenericTypeParameterBuilder) {
1434                         TypeParameter tparam = (TypeParameter) builder_to_type_param [t];
1435
1436                         Timer.StartTimer (TimerType.FindMembers);
1437                         MemberList list = tparam.FindMembers (
1438                                 mt, bf | BindingFlags.DeclaredOnly, filter, criteria);
1439                         Timer.StopTimer (TimerType.FindMembers);
1440                         return list;
1441                 }
1442
1443                 //
1444                 // Since FindMembers will not lookup both static and instance
1445                 // members, we emulate this behaviour here.
1446                 //
1447                 if ((bf & instance_and_static) == instance_and_static){
1448                         MemberInfo [] i_members = t.FindMembers (
1449                                 mt, bf & ~BindingFlags.Static, filter, criteria);
1450
1451                         int i_len = i_members.Length;
1452                         if (i_len == 1){
1453                                 MemberInfo one = i_members [0];
1454
1455                                 //
1456                                 // If any of these are present, we are done!
1457                                 //
1458                                 if ((one is Type) || (one is EventInfo) || (one is FieldInfo))
1459                                         return new MemberList (i_members);
1460                         }
1461                                 
1462                         MemberInfo [] s_members = t.FindMembers (
1463                                 mt, bf & ~BindingFlags.Instance, filter, criteria);
1464
1465                         int s_len = s_members.Length;
1466                         if (i_len > 0 || s_len > 0)
1467                                 return new MemberList (i_members, s_members);
1468                         else {
1469                                 if (i_len > 0)
1470                                         return new MemberList (i_members);
1471                                 else
1472                                         return new MemberList (s_members);
1473                         }
1474                 }
1475
1476                 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
1477         }
1478
1479
1480         /// <summary>
1481         ///   This method is only called from within MemberLookup.  It tries to use the member
1482         ///   cache if possible and falls back to the normal FindMembers if not.  The `used_cache'
1483         ///   flag tells the caller whether we used the cache or not.  If we used the cache, then
1484         ///   our return value will already contain all inherited members and the caller don't need
1485         ///   to check base classes and interfaces anymore.
1486         /// </summary>
1487         private static MemberInfo [] MemberLookup_FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1488                                                             string name, out bool used_cache)
1489         {
1490                 //
1491                 // We have to take care of arrays specially, because GetType on
1492                 // a TypeBuilder array will return a Type, not a TypeBuilder,
1493                 // and we can not call FindMembers on this type.
1494                 //
1495                 if (t == TypeManager.array_type || t.IsSubclassOf (TypeManager.array_type)) {
1496                         used_cache = true;
1497                         return TypeHandle.ArrayType.MemberCache.FindMembers (
1498                                 mt, bf, name, FilterWithClosure_delegate, null);
1499                 }
1500
1501                 //
1502                 // If this is a dynamic type, it's always in the `builder_to_declspace' hash table
1503                 // and we can ask the DeclSpace for the MemberCache.
1504                 //
1505                 if (t is TypeBuilder) {
1506                         DeclSpace decl = (DeclSpace) builder_to_declspace [t];
1507                         MemberCache cache = decl.MemberCache;
1508
1509                         //
1510                         // If this DeclSpace has a MemberCache, use it.
1511                         //
1512
1513                         if (cache != null) {
1514                                 used_cache = true;
1515                                 return cache.FindMembers (
1516                                         mt, bf, name, FilterWithClosure_delegate, null);
1517                         }
1518
1519                         // If there is no MemberCache, we need to use the "normal" FindMembers.
1520                         // Note, this is a VERY uncommon route!
1521
1522                         MemberList list;
1523                         Timer.StartTimer (TimerType.FindMembers);
1524                         list = decl.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
1525                                                  FilterWithClosure_delegate, name);
1526                         Timer.StopTimer (TimerType.FindMembers);
1527                         used_cache = false;
1528                         return (MemberInfo []) list;
1529                 }
1530
1531                 if (t is GenericTypeParameterBuilder) {
1532                         TypeParameter tparam = (TypeParameter) builder_to_type_param [t];
1533
1534                         MemberList list;
1535                         Timer.StartTimer (TimerType.FindMembers);
1536                         list = tparam.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
1537                                                    FilterWithClosure_delegate, name);
1538                         Timer.StopTimer (TimerType.FindMembers);
1539                         used_cache = false;
1540                         return (MemberInfo []) list;
1541                 }
1542
1543                 //
1544                 // This call will always succeed.  There is exactly one TypeHandle instance per
1545                 // type, TypeHandle.GetTypeHandle() will either return it or create a new one
1546                 // if it didn't already exist.
1547                 //
1548                 TypeHandle handle = TypeHandle.GetTypeHandle (t);
1549
1550                 used_cache = true;
1551                 return handle.MemberCache.FindMembers (mt, bf, name, FilterWithClosure_delegate, null);
1552         }
1553
1554         public static bool IsBuiltinType (Type t)
1555         {
1556                 if (t == object_type || t == string_type || t == int32_type || t == uint32_type ||
1557                     t == int64_type || t == uint64_type || t == float_type || t == double_type ||
1558                     t == char_type || t == short_type || t == decimal_type || t == bool_type ||
1559                     t == sbyte_type || t == byte_type || t == ushort_type || t == void_type)
1560                         return true;
1561                 else
1562                         return false;
1563         }
1564
1565         public static bool IsBuiltinType (TypeContainer tc)
1566         {
1567                 return IsBuiltinType (tc.TypeBuilder);
1568         }
1569
1570         //
1571         // This is like IsBuiltinType, but lacks decimal_type, we should also clean up
1572         // the pieces in the code where we use IsBuiltinType and special case decimal_type.
1573         // 
1574         public static bool IsCLRType (Type t)
1575         {
1576                 if (t == object_type || t == int32_type || t == uint32_type ||
1577                     t == int64_type || t == uint64_type || t == float_type || t == double_type ||
1578                     t == char_type || t == short_type || t == bool_type ||
1579                     t == sbyte_type || t == byte_type || t == ushort_type)
1580                         return true;
1581                 else
1582                         return false;
1583         }
1584
1585         public static bool IsDelegateType (Type t)
1586         {
1587                 if (t.IsGenericInstance)
1588                         t = t.GetGenericTypeDefinition ();
1589
1590                 if (t.IsSubclassOf (TypeManager.delegate_type))
1591                         return true;
1592                 else
1593                         return false;
1594         }
1595         
1596         public static bool IsEnumType (Type t)
1597         {
1598                 if (t.IsSubclassOf (TypeManager.enum_type))
1599                         return true;
1600                 else
1601                         return false;
1602         }
1603         public static bool IsBuiltinOrEnum (Type t)
1604         {
1605                 if (IsBuiltinType (t))
1606                         return true;
1607                 
1608                 if (IsEnumType (t))
1609                         return true;
1610
1611                 return false;
1612         }
1613
1614         //
1615         // Only a quick hack to get things moving, while real runtime support appears
1616         //
1617         public static bool IsGeneric (Type t)
1618         {
1619                 DeclSpace ds = (DeclSpace) builder_to_declspace [t];
1620
1621                 return ds.IsGeneric;
1622         }
1623
1624         public static bool HasGenericArguments (Type t)
1625         {
1626                 return GetNumberOfTypeArguments (t) > 0;
1627         }
1628
1629         public static int GetNumberOfTypeArguments (Type t)
1630         {
1631                 DeclSpace tc = LookupDeclSpace (t);
1632                 if (tc != null)
1633                         return tc.IsGeneric ? tc.CountTypeParameters : 0;
1634                 else
1635                         return t.HasGenericArguments ? t.GetGenericArguments ().Length : 0;
1636         }
1637
1638         public static Type[] GetTypeArguments (Type t)
1639         {
1640                 DeclSpace tc = LookupDeclSpace (t);
1641                 if (tc != null) {
1642                         if (!tc.IsGeneric)
1643                                 throw new InvalidOperationException ();
1644
1645                         TypeParameter[] tparam = tc.TypeParameters;
1646                         Type[] ret = new Type [tparam.Length];
1647                         for (int i = 0; i < tparam.Length; i++) {
1648                                 ret [i] = tparam [i].Type;
1649                                 if (ret [i] == null)
1650                                         throw new InternalErrorException ();
1651                         }
1652
1653                         return ret;
1654                 } else
1655                         return t.GetGenericArguments ();
1656         }
1657         
1658         //
1659         // Whether a type is unmanaged.  This is used by the unsafe code (25.2)
1660         //
1661         public static bool IsUnmanagedType (Type t)
1662         {
1663                 if (IsBuiltinType (t) && t != TypeManager.string_type)
1664                         return true;
1665
1666                 if (IsEnumType (t))
1667                         return true;
1668
1669                 if (t.IsPointer)
1670                         return true;
1671
1672                 if (IsValueType (t)){
1673                         if (t is TypeBuilder){
1674                                 TypeContainer tc = LookupTypeContainer (t);
1675
1676                                 if (tc.Fields != null){
1677                                 foreach (Field f in tc.Fields){
1678                                         if (f.FieldBuilder.IsStatic)
1679                                                 continue;
1680                                         if (!IsUnmanagedType (f.FieldBuilder.FieldType))
1681                                                 return false;
1682                                 }
1683                                 } else
1684                                         return true;
1685                         } else {
1686                                 FieldInfo [] fields = t.GetFields ();
1687
1688                                 foreach (FieldInfo f in fields){
1689                                         if (f.IsStatic)
1690                                                 continue;
1691                                         if (!IsUnmanagedType (f.FieldType))
1692                                                 return false;
1693                                 }
1694                         }
1695                         return true;
1696                 }
1697
1698                 return false;
1699         }
1700                 
1701         public static bool IsValueType (Type t)
1702         {
1703                 return t.IsGenericParameter || t.IsValueType;
1704         }
1705         
1706         public static bool IsInterfaceType (Type t)
1707         {
1708                 TypeContainer tc = (TypeContainer) builder_to_declspace [t];
1709                 if (tc == null)
1710                         return false;
1711
1712                 return tc.Kind == Kind.Interface;
1713         }
1714
1715         public static bool IsEqualGenericType (Type a, Type b)
1716         {
1717                 if ((a is TypeBuilder) && a.IsGenericTypeDefinition && b.IsGenericInstance) {
1718                         //
1719                         // `a' is a generic type definition's TypeBuilder and `b' is a
1720                         // generic instance of the same type.
1721                         //
1722                         // Example:
1723                         //
1724                         // class Stack<T>
1725                         // {
1726                         //     void Test (Stack<T> stack) { }
1727                         // }
1728                         //
1729                         // The first argument of `Test' will be the generic instance
1730                         // "Stack<!0>" - which is the same type than the "Stack" TypeBuilder.
1731                         //
1732                         if (a != b.GetGenericTypeDefinition ())
1733                                 return false;
1734
1735                         Type[] aparams = a.GetGenericArguments ();
1736                         Type[] bparams = b.GetGenericArguments ();
1737
1738                         if (aparams.Length != bparams.Length)
1739                                 return false;
1740
1741                         for (int i = 0; i < aparams.Length; i++)
1742                                 if (!aparams [i].Equals (bparams [i]))
1743                                         return false;
1744
1745                         return true;
1746                 }
1747
1748                 return false;
1749         }
1750
1751         public static bool IsEqual (Type a, Type b)
1752         {
1753                 if (a.Equals (b))
1754                         return true;
1755                 else
1756                         return IsEqualGenericType (a, b);
1757         }
1758
1759         public static bool MayBecomeEqualGenericTypes (Type a, Type b)
1760         {
1761                 if (a.IsGenericParameter) {
1762                         //
1763                         // If a is an array of a's type, they may never
1764                         // become equal.
1765                         //
1766                         while (b.IsArray) {
1767                                 b = b.GetElementType ();
1768                                 if (a.Equals (b))
1769                                         return false;
1770                         }
1771
1772                         //
1773                         // If b is a generic parameter or an actual type,
1774                         // they may become equal:
1775                         //
1776                         //    class X<T,U> : I<T>, I<U>
1777                         //    class X<T> : I<T>, I<float>
1778                         // 
1779                         if (b.IsGenericParameter || !b.IsGenericInstance)
1780                                 return true;
1781
1782                         //
1783                         // We're now comparing a type parameter with a
1784                         // generic instance.  They may become equal unless
1785                         // the type parameter appears anywhere in the
1786                         // generic instance:
1787                         //
1788                         //    class X<T,U> : I<T>, I<X<U>>
1789                         //        -> error because you could instanciate it as
1790                         //           X<X<int>,int>
1791                         //
1792                         //    class X<T> : I<T>, I<X<T>> -> ok
1793                         //
1794
1795                         Type[] bargs = GetTypeArguments (b);
1796                         for (int i = 0; i < bargs.Length; i++) {
1797                                 if (a.Equals (bargs [i]))
1798                                         return false;
1799                         }
1800
1801                         return true;
1802                 }
1803
1804                 if (b.IsGenericParameter)
1805                         return MayBecomeEqualGenericTypes (b, a);
1806
1807                 //
1808                 // At this point, neither a nor b are a type parameter.
1809                 //
1810                 // If one of them is a generic instance, let
1811                 // MayBecomeEqualGenericInstances() compare them (if the
1812                 // other one is not a generic instance, they can never
1813                 // become equal).
1814                 //
1815
1816                 if (a.IsGenericInstance || b.IsGenericInstance)
1817                         return MayBecomeEqualGenericInstances (a, b);
1818
1819                 //
1820                 // If both of them are arrays.
1821                 //
1822
1823                 if (a.IsArray && b.IsArray) {
1824                         if (a.GetArrayRank () != b.GetArrayRank ())
1825                                 return false;
1826                         
1827                         a = a.GetElementType ();
1828                         b = b.GetElementType ();
1829
1830                         return MayBecomeEqualGenericTypes (a, b);
1831                 }
1832
1833                 //
1834                 // Ok, two ordinary types.
1835                 //
1836
1837                 return a.Equals (b);
1838         }
1839
1840         //
1841         // Checks whether two generic instances may become equal for some
1842         // particular instantiation (26.3.1).
1843         //
1844         public static bool MayBecomeEqualGenericInstances (Type a, Type b)
1845         {
1846                 if (!a.IsGenericInstance || !b.IsGenericInstance)
1847                         return false;
1848                 if (a.GetGenericTypeDefinition () != b.GetGenericTypeDefinition ())
1849                         return false;
1850
1851                 Type[] aargs = GetTypeArguments (a);
1852                 Type[] bargs = GetTypeArguments (b);
1853
1854                 if (aargs.Length != bargs.Length)
1855                         return false;
1856
1857                 for (int i = 0; i < aargs.Length; i++) {
1858                         if (MayBecomeEqualGenericTypes (aargs [i], bargs [i]))
1859                                 return true;
1860                 }
1861
1862                 return false;
1863         }
1864
1865         public static bool IsEqualGenericInstance (Type type, Type parent)
1866         {
1867                 int tcount = GetNumberOfTypeArguments (type);
1868                 int pcount = GetNumberOfTypeArguments (parent);
1869
1870                 if (type.IsGenericInstance)
1871                         type = type.GetGenericTypeDefinition ();
1872                 if (parent.IsGenericInstance)
1873                         parent = parent.GetGenericTypeDefinition ();
1874
1875                 if (tcount != pcount)
1876                         return false;
1877
1878                 return type.Equals (parent);
1879         }
1880
1881         public static bool IsSubclassOf (Type type, Type parent)
1882         {
1883                 TypeParameter tparam = LookupTypeParameter (type);
1884                 TypeParameter pparam = LookupTypeParameter (parent);
1885
1886                 if ((tparam != null) && (pparam != null)) {
1887                         if (tparam == pparam)
1888                                 return true;
1889
1890                         return tparam.IsSubclassOf (parent);
1891                 }
1892
1893                 do {
1894                         if (type.Equals (parent))
1895                                 return true;
1896
1897                         type = type.BaseType;
1898                 } while (type != null);
1899
1900                 return false;
1901         }
1902
1903         public static bool IsFamilyAccessible (Type type, Type parent)
1904         {
1905                 TypeParameter tparam = LookupTypeParameter (type);
1906                 TypeParameter pparam = LookupTypeParameter (parent);
1907
1908                 if ((tparam != null) && (pparam != null)) {
1909                         if (tparam == pparam)
1910                                 return true;
1911
1912                         return tparam.IsSubclassOf (parent);
1913                 }
1914
1915                 do {
1916                         if (IsEqualGenericInstance (type, parent))
1917                                 return true;
1918
1919                         type = type.BaseType;
1920                 } while (type != null);
1921
1922                 return false;
1923         }
1924
1925         //
1926         // Checks whether `type' is a subclass or nested child of `parent'.
1927         //
1928         public static bool IsNestedFamilyAccessible (Type type, Type parent)
1929         {
1930                 do {
1931                         if (IsFamilyAccessible (type, parent))
1932                                 return true;
1933
1934                         // Handle nested types.
1935                         type = type.DeclaringType;
1936                 } while (type != null);
1937
1938                 return false;
1939         }
1940
1941         //
1942         // Checks whether `type' is a nested child of `parent'.
1943         //
1944         public static bool IsNestedChildOf (Type type, Type parent)
1945         {
1946                 if (IsEqual (type, parent))
1947                         return false;
1948
1949                 type = type.DeclaringType;
1950                 while (type != null) {
1951                         if (IsEqual (type, parent))
1952                                 return true;
1953
1954                         type = type.DeclaringType;
1955                 }
1956
1957                 return false;
1958         }
1959
1960         //
1961         // Do the right thing when returning the element type of an
1962         // array type based on whether we are compiling corlib or not
1963         //
1964         public static Type GetElementType (Type t)
1965         {
1966                 if (RootContext.StdLib)
1967                         return t.GetElementType ();
1968                 else
1969                         return TypeToCoreType (t.GetElementType ());
1970         }
1971
1972         /// <summary>
1973         ///   Returns the User Defined Types
1974         /// </summary>
1975         public static ArrayList UserTypes {
1976                 get {
1977                         return user_types;
1978                 }
1979         }
1980
1981         public static Hashtable TypeContainers {
1982                 get {
1983                         return typecontainers;
1984                 }
1985         }
1986
1987         static Hashtable builder_to_constant;
1988
1989         public static void RegisterConstant (FieldBuilder fb, Const c)
1990         {
1991                 if (builder_to_constant == null)
1992                         builder_to_constant = new PtrHashtable ();
1993
1994                 if (builder_to_constant.Contains (fb))
1995                         return;
1996
1997                 builder_to_constant.Add (fb, c);
1998         }
1999
2000         public static Const LookupConstant (FieldBuilder fb)
2001         {
2002                 if (builder_to_constant == null)
2003                         return null;
2004                 
2005                 return (Const) builder_to_constant [fb];
2006         }
2007         
2008         /// <summary>
2009         ///   Gigantic work around for missing features in System.Reflection.Emit follows.
2010         /// </summary>
2011         ///
2012         /// <remarks>
2013         ///   Since System.Reflection.Emit can not return MethodBase.GetParameters
2014         ///   for anything which is dynamic, and we need this in a number of places,
2015         ///   we register this information here, and use it afterwards.
2016         /// </remarks>
2017         static public void RegisterMethod (MethodBase mb, InternalParameters ip, Type [] args)
2018         {
2019                 if (args == null)
2020                         args = NoTypes;
2021                                 
2022                 method_arguments.Add (mb, args);
2023                 method_internal_params.Add (mb, ip);
2024         }
2025         
2026         static public InternalParameters LookupParametersByBuilder (MethodBase mb)
2027         {
2028                 if (! (mb is ConstructorBuilder || mb is MethodBuilder))
2029                         return null;
2030                 
2031                 if (method_internal_params.Contains (mb))
2032                         return (InternalParameters) method_internal_params [mb];
2033                 else
2034                         throw new Exception ("Argument for Method not registered" + mb);
2035         }
2036
2037         /// <summary>
2038         ///    Returns the argument types for a method based on its methodbase
2039         ///
2040         ///    For dynamic methods, we use the compiler provided types, for
2041         ///    methods from existing assemblies we load them from GetParameters,
2042         ///    and insert them into the cache
2043         /// </summary>
2044         static public Type [] GetArgumentTypes (MethodBase mb)
2045         {
2046                 object t = method_arguments [mb];
2047                 if (t != null)
2048                         return (Type []) t;
2049
2050                         ParameterInfo [] pi = mb.GetParameters ();
2051                         int c = pi.Length;
2052                 Type [] types;
2053                         
2054                 if (c == 0) {
2055                         types = NoTypes;
2056                 } else {
2057                         types = new Type [c];
2058                         for (int i = 0; i < c; i++)
2059                                 types [i] = pi [i].ParameterType;
2060                 }
2061                         method_arguments.Add (mb, types);
2062                         return types;
2063                 }
2064
2065         /// <summary>
2066         ///    Returns the argument types for an indexer based on its PropertyInfo
2067         ///
2068         ///    For dynamic indexers, we use the compiler provided types, for
2069         ///    indexers from existing assemblies we load them from GetParameters,
2070         ///    and insert them into the cache
2071         /// </summary>
2072         static public Type [] GetArgumentTypes (PropertyInfo indexer)
2073         {
2074                 if (indexer_arguments.Contains (indexer))
2075                         return (Type []) indexer_arguments [indexer];
2076                 else if (indexer is PropertyBuilder)
2077                         // If we're a PropertyBuilder and not in the
2078                         // `indexer_arguments' hash, then we're a property and
2079                         // not an indexer.
2080                         return NoTypes;
2081                 else {
2082                         ParameterInfo [] pi = indexer.GetIndexParameters ();
2083                         // Property, not an indexer.
2084                         if (pi == null)
2085                                 return NoTypes;
2086                         int c = pi.Length;
2087                         Type [] types = new Type [c];
2088                         
2089                         for (int i = 0; i < c; i++)
2090                                 types [i] = pi [i].ParameterType;
2091
2092                         indexer_arguments.Add (indexer, types);
2093                         return types;
2094                 }
2095         }
2096         
2097         // <remarks>
2098         //  This is a workaround the fact that GetValue is not
2099         //  supported for dynamic types
2100         // </remarks>
2101         static Hashtable fields = new Hashtable ();
2102         static public bool RegisterFieldValue (FieldBuilder fb, object value)
2103         {
2104                 if (fields.Contains (fb))
2105                         return false;
2106
2107                 fields.Add (fb, value);
2108
2109                 return true;
2110         }
2111
2112         static public object GetValue (FieldBuilder fb)
2113         {
2114                 return fields [fb];
2115         }
2116
2117         static Hashtable fieldbuilders_to_fields = new Hashtable ();
2118         static public bool RegisterFieldBase (FieldBuilder fb, FieldBase f)
2119         {
2120                 if (fieldbuilders_to_fields.Contains (fb))
2121                         return false;
2122
2123                 fieldbuilders_to_fields.Add (fb, f);
2124                 return true;
2125         }
2126
2127         //
2128         // The return value can be null;  This will be the case for
2129         // auxiliary FieldBuilders created by the compiler that have no
2130         // real field being declared on the source code
2131         //
2132         static public FieldBase GetField (FieldInfo fb)
2133         {
2134                 return (FieldBase) fieldbuilders_to_fields [fb];
2135         }
2136         
2137         static Hashtable events;
2138
2139         static public void RegisterEvent (MyEventBuilder eb, MethodBase add, MethodBase remove)
2140         {
2141                 if (events == null)
2142                         events = new Hashtable ();
2143
2144                 if (!events.Contains (eb)) {
2145                 events.Add (eb, new Pair (add, remove));
2146                 }
2147         }
2148
2149         static public MethodInfo GetAddMethod (EventInfo ei)
2150         {
2151                 if (ei is MyEventBuilder) {
2152                         Pair pair = (Pair) events [ei];
2153
2154                         return (MethodInfo) pair.First;
2155                 }
2156                 return ei.GetAddMethod (true);
2157         }
2158
2159         static public MethodInfo GetRemoveMethod (EventInfo ei)
2160         {
2161                 if (ei is MyEventBuilder) {
2162                         Pair pair = (Pair) events [ei];
2163
2164                         return (MethodInfo) pair.Second;
2165                 }
2166                 return ei.GetRemoveMethod (true);
2167         }
2168
2169         static Hashtable priv_fields_events;
2170
2171         static public bool RegisterPrivateFieldOfEvent (EventInfo einfo, FieldBuilder builder)
2172         {
2173                 if (priv_fields_events == null)
2174                         priv_fields_events = new Hashtable ();
2175
2176                 if (priv_fields_events.Contains (einfo))
2177                         return false;
2178
2179                 priv_fields_events.Add (einfo, builder);
2180
2181                 return true;
2182         }
2183
2184         static public MemberInfo GetPrivateFieldOfEvent (EventInfo ei)
2185         {
2186                 if (priv_fields_events == null)
2187                         return null;
2188                 else
2189                         return (MemberInfo) priv_fields_events [ei];
2190         }
2191                 
2192         static Hashtable properties;
2193         
2194         static public bool RegisterProperty (PropertyBuilder pb, MethodBase get, MethodBase set)
2195         {
2196                 if (properties == null)
2197                         properties = new Hashtable ();
2198
2199                 if (properties.Contains (pb))
2200                         return false;
2201
2202                 properties.Add (pb, new Pair (get, set));
2203
2204                 return true;
2205         }
2206
2207         static public bool RegisterIndexer (PropertyBuilder pb, MethodBase get,
2208                                             MethodBase set, Type[] args)
2209         {
2210                 if (!RegisterProperty (pb, get,set))
2211                         return false;
2212
2213                 indexer_arguments.Add (pb, args);
2214
2215                 return true;
2216         }
2217
2218         public static bool CheckStructCycles (TypeContainer tc, Hashtable seen)
2219         {
2220                 Hashtable hash = new Hashtable ();
2221                 return CheckStructCycles (tc, seen, hash);
2222         }
2223
2224         public static bool CheckStructCycles (TypeContainer tc, Hashtable seen,
2225                                               Hashtable hash)
2226         {
2227                 if ((tc.Kind != Kind.Struct) || IsBuiltinType (tc))
2228                         return true;
2229
2230                 //
2231                 // `seen' contains all types we've already visited.
2232                 //
2233                 if (seen.Contains (tc))
2234                         return true;
2235                 seen.Add (tc, null);
2236
2237                 if (tc.Fields == null)
2238                         return true;
2239
2240                 foreach (Field field in tc.Fields) {
2241                         if (field.FieldBuilder.IsStatic)
2242                                 continue;
2243
2244                         Type ftype = field.FieldBuilder.FieldType;
2245                         TypeContainer ftc = LookupTypeContainer (ftype);
2246                         if (ftc == null)
2247                                 continue;
2248
2249                         if (hash.Contains (ftc)) {
2250                                 Report.Error (523, tc.Location,
2251                                               "Struct member `{0}.{1}' of type `{2}' " +
2252                                               "causes a cycle in the struct layout",
2253                                               tc.Name, field.Name, ftc.Name);
2254                                 return false;
2255                         }
2256
2257                         //
2258                         // `hash' contains all types in the current path.
2259                         //
2260                         hash.Add (tc, null);
2261
2262                         bool ok = CheckStructCycles (ftc, seen, hash);
2263
2264                         hash.Remove (tc);
2265
2266                         if (!ok)
2267                                 return false;
2268
2269                         if (!seen.Contains (ftc))
2270                                 seen.Add (ftc, null);
2271                 }
2272
2273                 return true;
2274         }
2275
2276         /// <summary>
2277         ///   Given an array of interface types, expand and eliminate repeated ocurrences
2278         ///   of an interface.  
2279         /// </summary>
2280         ///
2281         /// <remarks>
2282         ///   This expands in context like: IA; IB : IA; IC : IA, IB; the interface "IC" to
2283         ///   be IA, IB, IC.
2284         /// </remarks>
2285         public static Type[] ExpandInterfaces (EmitContext ec, TypeExpr [] base_interfaces)
2286         {
2287                 ArrayList new_ifaces = new ArrayList ();
2288                 
2289                 foreach (TypeExpr iface in base_interfaces){
2290                         Type itype = iface.ResolveType (ec);
2291                         if (itype == null)
2292                                 return null;
2293
2294                         if (!new_ifaces.Contains (itype))
2295                                 new_ifaces.Add (itype);
2296                         
2297                         Type [] implementing = itype.GetInterfaces ();
2298                         
2299                         foreach (Type imp in implementing){
2300                                 if (!new_ifaces.Contains (imp))
2301                                         new_ifaces.Add (imp);
2302                         }
2303                 }
2304                 Type [] ret = new Type [new_ifaces.Count];
2305                 new_ifaces.CopyTo (ret, 0);
2306                 return ret;
2307         }
2308                 
2309         static PtrHashtable iface_cache = new PtrHashtable ();
2310                 
2311         /// <summary>
2312         ///   This function returns the interfaces in the type `t'.  Works with
2313         ///   both types and TypeBuilders.
2314         /// </summary>
2315         public static Type [] GetInterfaces (Type t)
2316         {
2317                 
2318                 Type [] cached = iface_cache [t] as Type [];
2319                 if (cached != null)
2320                         return cached;
2321                 
2322                 //
2323                 // The reason for catching the Array case is that Reflection.Emit
2324                 // will not return a TypeBuilder for Array types of TypeBuilder types,
2325                 // but will still throw an exception if we try to call GetInterfaces
2326                 // on the type.
2327                 //
2328                 // Since the array interfaces are always constant, we return those for
2329                 // the System.Array
2330                 //
2331                 
2332                 if (t.IsArray)
2333                         t = TypeManager.array_type;
2334                 
2335                 if (t is TypeBuilder){
2336                         Type[] parent_ifaces;
2337                         
2338                         if (t.BaseType == null)
2339                                 parent_ifaces = NoTypes;
2340                         else
2341                                 parent_ifaces = GetInterfaces (t.BaseType);
2342                         Type[] type_ifaces = (Type []) builder_to_ifaces [t];
2343                         if (type_ifaces == null)
2344                                 type_ifaces = NoTypes;
2345
2346                         int parent_count = parent_ifaces.Length;
2347                         Type[] result = new Type [parent_count + type_ifaces.Length];
2348                         parent_ifaces.CopyTo (result, 0);
2349                         type_ifaces.CopyTo (result, parent_count);
2350
2351                         iface_cache [t] = result;
2352                         return result;
2353                 } else if (t is GenericTypeParameterBuilder){
2354                         Type[] type_ifaces = (Type []) builder_to_ifaces [t];
2355                         if (type_ifaces == null)
2356                                 type_ifaces = NoTypes;
2357
2358                         iface_cache [t] = type_ifaces;
2359                         return type_ifaces;
2360                 } else {
2361                         Type[] ifaces = t.GetInterfaces ();
2362                         iface_cache [t] = ifaces;
2363                         return ifaces;
2364                 }
2365         }
2366         
2367         //
2368         // gets the interfaces that are declared explicitly on t
2369         //
2370         public static Type [] GetExplicitInterfaces (TypeBuilder t)
2371         {
2372                 return (Type []) builder_to_ifaces [t];
2373         }
2374         
2375         /// <remarks>
2376         ///  The following is used to check if a given type implements an interface.
2377         ///  The cache helps us reduce the expense of hitting Type.GetInterfaces everytime.
2378         /// </remarks>
2379         public static bool ImplementsInterface (Type t, Type iface)
2380         {
2381                 Type [] interfaces;
2382
2383                 //
2384                 // FIXME OPTIMIZATION:
2385                 // as soon as we hit a non-TypeBuiler in the interface
2386                 // chain, we could return, as the `Type.GetInterfaces'
2387                 // will return all the interfaces implement by the type
2388                 // or its parents.
2389                 //
2390                 do {
2391                         interfaces = GetInterfaces (t);
2392
2393                         if (interfaces != null){
2394                                 foreach (Type i in interfaces){
2395                                         if (i == iface)
2396                                                 return true;
2397                                 }
2398                         }
2399                         
2400                         t = t.BaseType;
2401                 } while (t != null);
2402                 
2403                 return false;
2404         }
2405
2406         static NumberFormatInfo nf_provider = CultureInfo.CurrentCulture.NumberFormat;
2407
2408         // This is a custom version of Convert.ChangeType() which works
2409         // with the TypeBuilder defined types when compiling corlib.
2410         public static object ChangeType (object value, Type conversionType, out bool error)
2411         {
2412                 IConvertible convert_value = value as IConvertible;
2413                 
2414                 if (convert_value == null){
2415                         error = true;
2416                         return null;
2417                 }
2418                 
2419                 //
2420                 // We must use Type.Equals() here since `conversionType' is
2421                 // the TypeBuilder created version of a system type and not
2422                 // the system type itself.  You cannot use Type.GetTypeCode()
2423                 // on such a type - it'd always return TypeCode.Object.
2424                 //
2425                 error = false;
2426                 try {
2427                         if (conversionType.Equals (typeof (Boolean)))
2428                                 return (object)(convert_value.ToBoolean (nf_provider));
2429                         else if (conversionType.Equals (typeof (Byte)))
2430                                 return (object)(convert_value.ToByte (nf_provider));
2431                         else if (conversionType.Equals (typeof (Char)))
2432                                 return (object)(convert_value.ToChar (nf_provider));
2433                         else if (conversionType.Equals (typeof (DateTime)))
2434                                 return (object)(convert_value.ToDateTime (nf_provider));
2435                         else if (conversionType.Equals (typeof (Decimal)))
2436                                 return (object)(convert_value.ToDecimal (nf_provider));
2437                         else if (conversionType.Equals (typeof (Double)))
2438                                 return (object)(convert_value.ToDouble (nf_provider));
2439                         else if (conversionType.Equals (typeof (Int16)))
2440                                 return (object)(convert_value.ToInt16 (nf_provider));
2441                         else if (conversionType.Equals (typeof (Int32)))
2442                                 return (object)(convert_value.ToInt32 (nf_provider));
2443                         else if (conversionType.Equals (typeof (Int64)))
2444                                 return (object)(convert_value.ToInt64 (nf_provider));
2445                         else if (conversionType.Equals (typeof (SByte)))
2446                                 return (object)(convert_value.ToSByte (nf_provider));
2447                         else if (conversionType.Equals (typeof (Single)))
2448                                 return (object)(convert_value.ToSingle (nf_provider));
2449                         else if (conversionType.Equals (typeof (String)))
2450                                 return (object)(convert_value.ToString (nf_provider));
2451                         else if (conversionType.Equals (typeof (UInt16)))
2452                                 return (object)(convert_value.ToUInt16 (nf_provider));
2453                         else if (conversionType.Equals (typeof (UInt32)))
2454                                 return (object)(convert_value.ToUInt32 (nf_provider));
2455                         else if (conversionType.Equals (typeof (UInt64)))
2456                                 return (object)(convert_value.ToUInt64 (nf_provider));
2457                         else if (conversionType.Equals (typeof (Object)))
2458                                 return (object)(value);
2459                         else 
2460                                 error = true;
2461                 } catch {
2462                         error = true;
2463                 }
2464                 return null;
2465         }
2466
2467         //
2468         // This is needed, because enumerations from assemblies
2469         // do not report their underlyingtype, but they report
2470         // themselves
2471         //
2472         public static Type EnumToUnderlying (Type t)
2473         {
2474                 if (t == TypeManager.enum_type)
2475                         return t;
2476
2477                 t = t.UnderlyingSystemType;
2478                 if (!TypeManager.IsEnumType (t))
2479                         return t;
2480         
2481                 if (t is TypeBuilder) {
2482                         // slow path needed to compile corlib
2483                         if (t == TypeManager.bool_type ||
2484                                         t == TypeManager.byte_type ||
2485                                         t == TypeManager.sbyte_type ||
2486                                         t == TypeManager.char_type ||
2487                                         t == TypeManager.short_type ||
2488                                         t == TypeManager.ushort_type ||
2489                                         t == TypeManager.int32_type ||
2490                                         t == TypeManager.uint32_type ||
2491                                         t == TypeManager.int64_type ||
2492                                         t == TypeManager.uint64_type)
2493                                 return t;
2494                         throw new Exception ("Unhandled typecode in enum " + " from " + t.AssemblyQualifiedName);
2495                 }
2496                 TypeCode tc = Type.GetTypeCode (t);
2497
2498                 switch (tc){
2499                 case TypeCode.Boolean:
2500                         return TypeManager.bool_type;
2501                 case TypeCode.Byte:
2502                         return TypeManager.byte_type;
2503                 case TypeCode.SByte:
2504                         return TypeManager.sbyte_type;
2505                 case TypeCode.Char:
2506                         return TypeManager.char_type;
2507                 case TypeCode.Int16:
2508                         return TypeManager.short_type;
2509                 case TypeCode.UInt16:
2510                         return TypeManager.ushort_type;
2511                 case TypeCode.Int32:
2512                         return TypeManager.int32_type;
2513                 case TypeCode.UInt32:
2514                         return TypeManager.uint32_type;
2515                 case TypeCode.Int64:
2516                         return TypeManager.int64_type;
2517                 case TypeCode.UInt64:
2518                         return TypeManager.uint64_type;
2519                 }
2520                 throw new Exception ("Unhandled typecode in enum " + tc + " from " + t.AssemblyQualifiedName);
2521         }
2522
2523         //
2524         // When compiling corlib and called with one of the core types, return
2525         // the corresponding typebuilder for that type.
2526         //
2527         public static Type TypeToCoreType (Type t)
2528         {
2529                 if (RootContext.StdLib || (t is TypeBuilder))
2530                         return t;
2531
2532                 TypeCode tc = Type.GetTypeCode (t);
2533
2534                 switch (tc){
2535                 case TypeCode.Boolean:
2536                         return TypeManager.bool_type;
2537                 case TypeCode.Byte:
2538                         return TypeManager.byte_type;
2539                 case TypeCode.SByte:
2540                         return TypeManager.sbyte_type;
2541                 case TypeCode.Char:
2542                         return TypeManager.char_type;
2543                 case TypeCode.Int16:
2544                         return TypeManager.short_type;
2545                 case TypeCode.UInt16:
2546                         return TypeManager.ushort_type;
2547                 case TypeCode.Int32:
2548                         return TypeManager.int32_type;
2549                 case TypeCode.UInt32:
2550                         return TypeManager.uint32_type;
2551                 case TypeCode.Int64:
2552                         return TypeManager.int64_type;
2553                 case TypeCode.UInt64:
2554                         return TypeManager.uint64_type;
2555                 case TypeCode.Single:
2556                         return TypeManager.float_type;
2557                 case TypeCode.Double:
2558                         return TypeManager.double_type;
2559                 case TypeCode.String:
2560                         return TypeManager.string_type;
2561                 default:
2562                         if (t == typeof (void))
2563                                 return TypeManager.void_type;
2564                         if (t == typeof (object))
2565                                 return TypeManager.object_type;
2566                         if (t == typeof (System.Type))
2567                                 return TypeManager.type_type;
2568                         if (t == typeof (System.IntPtr))
2569                                 return TypeManager.intptr_type;
2570                         return t;
2571                 }
2572         }
2573
2574         /// <summary>
2575         ///   Utility function that can be used to probe whether a type
2576         ///   is managed or not.  
2577         /// </summary>
2578         public static bool VerifyUnManaged (Type t, Location loc)
2579         {
2580                 if (t.IsValueType || t.IsPointer){
2581                         //
2582                         // FIXME: this is more complex, we actually need to
2583                         // make sure that the type does not contain any
2584                         // classes itself
2585                         //
2586                         return true;
2587                 }
2588
2589                 if (!RootContext.StdLib && (t == TypeManager.decimal_type))
2590                         // We need this explicit check here to make it work when
2591                         // compiling corlib.
2592                         return true;
2593
2594                 Report.Error (
2595                         208, loc,
2596                         "Cannot take the address or size of a variable of a managed type ('" +
2597                         CSharpName (t) + "')");
2598                 return false;   
2599         }
2600         
2601         /// <summary>
2602         ///   Returns the name of the indexer in a given type.
2603         /// </summary>
2604         /// <remarks>
2605         ///   The default is not always `Item'.  The user can change this behaviour by
2606         ///   using the IndexerNameAttribute in the container.
2607         ///
2608         ///   For example, the String class indexer is named `Chars' not `Item' 
2609         /// </remarks>
2610         public static string IndexerPropertyName (Type t)
2611         {
2612                 if (t.IsGenericInstance)
2613                         t = t.GetGenericTypeDefinition ();
2614
2615                 if (t is TypeBuilder) {
2616                         TypeContainer tc = t.IsInterface ? LookupInterface (t) : LookupTypeContainer (t);
2617                         return tc == null ? TypeContainer.DefaultIndexerName : tc.IndexerName;
2618                 }
2619                 
2620                 System.Attribute attr = System.Attribute.GetCustomAttribute (
2621                         t, TypeManager.default_member_type);
2622                 if (attr != null){
2623                         DefaultMemberAttribute dma = (DefaultMemberAttribute) attr;
2624                         return dma.MemberName;
2625                 }
2626
2627                 return TypeContainer.DefaultIndexerName;
2628         }
2629
2630         static MethodInfo declare_local_method = null;
2631         
2632         public static LocalBuilder DeclareLocalPinned (ILGenerator ig, Type t)
2633         {
2634                 if (declare_local_method == null){
2635                         declare_local_method = typeof (ILGenerator).GetMethod (
2636                                 "DeclareLocal",
2637                                 BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
2638                                 null, 
2639                                 new Type [] { typeof (Type), typeof (bool)},
2640                                 null);
2641                         if (declare_local_method == null){
2642                                 Report.Warning (-24, new Location (-1),
2643                                                 "This version of the runtime does not support making pinned local variables.  " +
2644                                         "This code may cause errors on a runtime with a moving GC");
2645                                 return ig.DeclareLocal (t);
2646                         }
2647                 }
2648                 return (LocalBuilder) declare_local_method.Invoke (ig, new object [] { t, true });
2649         }
2650
2651         //
2652         // Returns whether the array of memberinfos contains the given method
2653         //
2654         public static bool ArrayContainsMethod (MemberInfo [] array, MethodBase new_method)
2655         {
2656                 Type [] new_args = TypeManager.GetArgumentTypes (new_method);
2657                 
2658                 foreach (MethodBase method in array) {
2659                         if (method.Name != new_method.Name)
2660                                 continue;
2661
2662                         if (method is MethodInfo && new_method is MethodInfo)
2663                                 if (((MethodInfo) method).ReturnType != ((MethodInfo) new_method).ReturnType)
2664                                         continue;
2665
2666                         
2667                         Type [] old_args = TypeManager.GetArgumentTypes (method);
2668                         int old_count = old_args.Length;
2669                         int i;
2670                         
2671                         if (new_args.Length != old_count)
2672                                 continue;
2673                         
2674                         for (i = 0; i < old_count; i++){
2675                                 if (old_args [i] != new_args [i])
2676                                         break;
2677                         }
2678                         if (i != old_count)
2679                                 continue;
2680
2681                         return true;
2682                 }
2683                 
2684                 return false;
2685         }
2686         
2687         //
2688         // We copy methods from `new_members' into `target_list' if the signature
2689         // for the method from in the new list does not exist in the target_list
2690         //
2691         // The name is assumed to be the same.
2692         //
2693         public static ArrayList CopyNewMethods (ArrayList target_list, IList new_members)
2694         {
2695                 if (target_list == null){
2696                         target_list = new ArrayList ();
2697
2698                         foreach (MemberInfo mi in new_members){
2699                                 if (mi is MethodBase)
2700                                         target_list.Add (mi);
2701                         }
2702                         return target_list;
2703                 }
2704                 
2705                 MemberInfo [] target_array = new MemberInfo [target_list.Count];
2706                 target_list.CopyTo (target_array, 0);
2707                 
2708                 foreach (MemberInfo mi in new_members){
2709                         MethodBase new_method = (MethodBase) mi;
2710                         
2711                         if (!ArrayContainsMethod (target_array, new_method))
2712                                 target_list.Add (new_method);
2713                 }
2714                 return target_list;
2715         }
2716
2717         static public bool IsGenericMethod (MethodBase mb)
2718         {
2719                 if (mb.DeclaringType is TypeBuilder) {
2720                         IMethodData method = (IMethodData) builder_to_method [mb];
2721                         if (method == null)
2722                                 return false;
2723
2724                         return method.GenericMethod != null;
2725                 }
2726
2727                 return mb.IsGenericMethodDefinition;
2728         }
2729         
2730 #region MemberLookup implementation
2731         
2732         //
2733         // Whether we allow private members in the result (since FindMembers
2734         // uses NonPublic for both protected and private), we need to distinguish.
2735         //
2736
2737         static internal bool FilterNone (MemberInfo m, object filter_criteria)
2738         {
2739                 return true;
2740         }
2741
2742         internal class Closure {
2743                 internal bool     private_ok;
2744
2745                 // Who is invoking us and which type is being queried currently.
2746                 internal Type     invocation_type;
2747                 internal Type     qualifier_type;
2748
2749                 // The assembly that defines the type is that is calling us
2750                 internal Assembly invocation_assembly;
2751                 internal IList almost_match;
2752
2753                 private bool CheckValidFamilyAccess (bool is_static, MemberInfo m)
2754                 {
2755                         if (invocation_type == null)
2756                                 return false;
2757
2758                         if (is_static)
2759                                 return true;
2760                         
2761                         // A nested class has access to all the protected members visible
2762                         // to its parent.
2763                         if (qualifier_type != null
2764                             && TypeManager.IsNestedChildOf (invocation_type, qualifier_type))
2765                                 return true;
2766
2767                         if (invocation_type == m.DeclaringType
2768                             || invocation_type.IsSubclassOf (m.DeclaringType)) {
2769                                 // Although a derived class can access protected members of
2770                                 // its base class it cannot do so through an instance of the
2771                                 // base class (CS1540).
2772                                 // => Ancestry should be: declaring_type ->* invocation_type
2773                                 //      ->*  qualified_type
2774                                 if (qualifier_type == null
2775                                     || qualifier_type == invocation_type
2776                                     || qualifier_type.IsSubclassOf (invocation_type))
2777                                         return true;
2778                         }
2779         
2780                         if (almost_match != null)
2781                                 almost_match.Add (m);
2782                         return false;
2783                 }
2784
2785                 bool Filter (MethodBase mb, object filter_criteria)
2786                 {
2787                         MethodAttributes ma = mb.Attributes & MethodAttributes.MemberAccessMask;
2788
2789                         if (ma == MethodAttributes.Private)
2790                                 return private_ok ||
2791                                         IsEqual (invocation_type, mb.DeclaringType) ||
2792                                         IsNestedChildOf (invocation_type, mb.DeclaringType);
2793
2794                         //
2795                         // FamAndAssem requires that we not only derivate, but we are on the
2796                         // same assembly.  
2797                         //
2798                         if (ma == MethodAttributes.FamANDAssem){
2799                                 if (invocation_assembly != mb.DeclaringType.Assembly)
2800                                         return false;
2801                         }
2802
2803                         // Assembly and FamORAssem succeed if we're in the same assembly.
2804                         if ((ma == MethodAttributes.Assembly) || (ma == MethodAttributes.FamORAssem)){
2805                                 if (invocation_assembly == mb.DeclaringType.Assembly)
2806                                         return true;
2807                         }
2808
2809                         // We already know that we aren't in the same assembly.
2810                         if (ma == MethodAttributes.Assembly)
2811                                 return false;
2812
2813                         // Family and FamANDAssem require that we derive.
2814                         if ((ma == MethodAttributes.Family) || (ma == MethodAttributes.FamANDAssem)){
2815                                 if (invocation_type == null)
2816                                         return false;
2817
2818                                 if (!IsNestedFamilyAccessible (invocation_type, mb.DeclaringType))
2819                                         return false;
2820
2821                                 // Although a derived class can access protected members of its base class
2822                                 // it cannot do so through an instance of the base class (CS1540).
2823                                 if (!mb.IsStatic && (qualifier_type != null) &&
2824                                     !IsEqualGenericInstance (invocation_type, qualifier_type) &&
2825                                     TypeManager.IsFamilyAccessible (invocation_type, qualifier_type) &&
2826                                     !TypeManager.IsNestedChildOf (invocation_type, qualifier_type))
2827                                         return false;
2828
2829                                 return true;
2830                         }
2831
2832                         // Public.
2833                         return true;
2834                 }
2835
2836                 bool Filter (FieldInfo fi, object filter_criteria)
2837                 {
2838                         FieldAttributes fa = fi.Attributes & FieldAttributes.FieldAccessMask;
2839
2840                         if (fa == FieldAttributes.Private)
2841                                 return private_ok ||
2842                                         IsEqual (invocation_type, fi.DeclaringType) ||
2843                                         IsNestedChildOf (invocation_type, fi.DeclaringType);
2844
2845                         //
2846                         // FamAndAssem requires that we not only derivate, but we are on the
2847                         // same assembly.  
2848                         //
2849                         if (fa == FieldAttributes.FamANDAssem){
2850                                 if (invocation_assembly != fi.DeclaringType.Assembly)
2851                                         return false;
2852                         }
2853
2854                         // Assembly and FamORAssem succeed if we're in the same assembly.
2855                         if ((fa == FieldAttributes.Assembly) || (fa == FieldAttributes.FamORAssem)){
2856                                 if (invocation_assembly == fi.DeclaringType.Assembly)
2857                                         return true;
2858                         }
2859
2860                         // We already know that we aren't in the same assembly.
2861                         if (fa == FieldAttributes.Assembly)
2862                                 return false;
2863
2864                         // Family and FamANDAssem require that we derive.
2865                         if ((fa == FieldAttributes.Family) || (fa == FieldAttributes.FamANDAssem)){
2866                                 if (invocation_type == null)
2867                                         return false;
2868
2869                                 if (!IsNestedFamilyAccessible (invocation_type, fi.DeclaringType))
2870                                         return false;
2871
2872                                 // Although a derived class can access protected members of its base class
2873                                 // it cannot do so through an instance of the base class (CS1540).
2874                                 if (!fi.IsStatic && (qualifier_type != null) &&
2875                                     !IsEqualGenericInstance (invocation_type, qualifier_type) &&
2876                                     TypeManager.IsFamilyAccessible (invocation_type, qualifier_type) &&
2877                                     !TypeManager.IsNestedChildOf (invocation_type, qualifier_type))
2878                                         return false;
2879
2880                                 return true;
2881                         }
2882
2883                         // Public.
2884                         return true;
2885                 }
2886                 
2887                 //
2888                 // This filter filters by name + whether it is ok to include private
2889                 // members in the search
2890                 //
2891                 internal bool Filter (MemberInfo m, object filter_criteria)
2892                 {
2893                         //
2894                         // Hack: we know that the filter criteria will always be in the
2895                         // `closure' // fields. 
2896                         //
2897
2898                         if ((filter_criteria != null) && (m.Name != (string) filter_criteria))
2899                                 return false;
2900
2901                         if (((qualifier_type == null) || (qualifier_type == invocation_type)) &&
2902                             (invocation_type != null) &&
2903                             IsEqual (m.DeclaringType, invocation_type))
2904                                 return true;
2905
2906                         //
2907                         // Ugly: we need to find out the type of `m', and depending
2908                         // on this, tell whether we accept or not
2909                         //
2910                         if (m is MethodBase)
2911                                 return Filter ((MethodBase) m, filter_criteria);
2912
2913                         if (m is FieldInfo)
2914                                 return Filter ((FieldInfo) m, filter_criteria);
2915
2916                         //
2917                         // EventInfos and PropertyInfos, return true because they lack
2918                         // permission information, so we need to check later on the methods.
2919                         //
2920                         return true;
2921                 }
2922         }
2923
2924         static Closure closure = new Closure ();
2925         static MemberFilter FilterWithClosure_delegate = new MemberFilter (closure.Filter);
2926
2927         //
2928         // Looks up a member called `name' in the `queried_type'.  This lookup
2929         // is done by code that is contained in the definition for `invocation_type'
2930         // through a qualifier of type `qualifier_type' (or null if there is no qualifier).
2931         //
2932         // `invocation_type' is used to check whether we're allowed to access the requested
2933         // member wrt its protection level.
2934         //
2935         // When called from MemberAccess, `qualifier_type' is the type which is used to access
2936         // the requested member (`class B { A a = new A (); a.foo = 5; }'; here invocation_type
2937         // is B and qualifier_type is A).  This is used to do the CS1540 check.
2938         //
2939         // When resolving a SimpleName, `qualifier_type' is null.
2940         //
2941         // The `qualifier_type' is used for the CS1540 check; it's normally either null or
2942         // the same than `queried_type' - except when we're being called from BaseAccess;
2943         // in this case, `invocation_type' is the current type and `queried_type' the base
2944         // type, so this'd normally trigger a CS1540.
2945         //
2946         // The binding flags are `bf' and the kind of members being looked up are `mt'
2947         //
2948         // The return value always includes private members which code in `invocation_type'
2949         // is allowed to access (using the specified `qualifier_type' if given); only use
2950         // BindingFlags.NonPublic to bypass the permission check.
2951         //
2952         // The 'almost_match' argument is used for reporting error CS1540.
2953         //
2954         // Returns an array of a single element for everything but Methods/Constructors
2955         // that might return multiple matches.
2956         //
2957         public static MemberInfo [] MemberLookup (Type invocation_type, Type qualifier_type,
2958                                                   Type queried_type, MemberTypes mt,
2959                                                   BindingFlags original_bf, string name, IList almost_match)
2960         {
2961                 Timer.StartTimer (TimerType.MemberLookup);
2962
2963                 MemberInfo[] retval = RealMemberLookup (invocation_type, qualifier_type,
2964                                                         queried_type, mt, original_bf, name, almost_match);
2965
2966                 Timer.StopTimer (TimerType.MemberLookup);
2967
2968                 return retval;
2969         }
2970
2971         static MemberInfo [] RealMemberLookup (Type invocation_type, Type qualifier_type,
2972                                                Type queried_type, MemberTypes mt,
2973                                                BindingFlags original_bf, string name, IList almost_match)
2974         {
2975                 BindingFlags bf = original_bf;
2976                 
2977                 ArrayList method_list = null;
2978                 Type current_type = queried_type;
2979                 bool searching = (original_bf & BindingFlags.DeclaredOnly) == 0;
2980                 bool skip_iface_check = true, used_cache = false;
2981                 bool always_ok_flag = false;
2982
2983                 closure.invocation_type = invocation_type;
2984                 closure.invocation_assembly = invocation_type != null ? invocation_type.Assembly : null;
2985                 closure.qualifier_type = qualifier_type;
2986                 closure.almost_match = almost_match;
2987
2988                 //
2989                 // If we are a nested class, we always have access to our container
2990                 // type names
2991                 //
2992                 if (invocation_type != null){
2993                         string invocation_name = invocation_type.FullName;
2994                         if ((invocation_name != null) && (invocation_name.IndexOf ('+') != -1)){
2995                                 string container = queried_type.FullName + "+";
2996                                 int container_length = container.Length;
2997
2998                                 if (invocation_name.Length > container_length){
2999                                         string shared = invocation_name.Substring (0, container_length);
3000                                 
3001                                         if (shared == container)
3002                                                 always_ok_flag = true;
3003                                 }
3004                         }
3005                 }
3006                 
3007                 // This is from the first time we find a method
3008                 // in most cases, we do not actually find a method in the base class
3009                 // so we can just ignore it, and save the arraylist allocation
3010                 MemberInfo [] first_members_list = null;
3011                 bool use_first_members_list = false;
3012                 
3013                 do {
3014                         MemberInfo [] list;
3015
3016                         //
3017                         // `NonPublic' is lame, because it includes both protected and
3018                         // private methods, so we need to control this behavior by
3019                         // explicitly tracking if a private method is ok or not.
3020                         //
3021                         // The possible cases are:
3022                         //    public, private and protected (internal does not come into the
3023                         //    equation)
3024                         //
3025                         if ((invocation_type != null) &&
3026                             ((invocation_type == current_type) ||
3027                              IsNestedChildOf (invocation_type, current_type)) ||
3028                             always_ok_flag)
3029                                 bf = original_bf | BindingFlags.NonPublic;
3030                         else
3031                                 bf = original_bf;
3032
3033                         closure.private_ok = (original_bf & BindingFlags.NonPublic) != 0;
3034
3035                         Timer.StopTimer (TimerType.MemberLookup);
3036
3037                         list = MemberLookup_FindMembers (
3038                                 current_type, mt, bf, name, out used_cache);
3039
3040                         Timer.StartTimer (TimerType.MemberLookup);
3041
3042                         //
3043                         // When queried for an interface type, the cache will automatically check all
3044                         // inherited members, so we don't need to do this here.  However, this only
3045                         // works if we already used the cache in the first iteration of this loop.
3046                         //
3047                         // If we used the cache in any further iteration, we can still terminate the
3048                         // loop since the cache always looks in all parent classes.
3049                         //
3050
3051                         if (used_cache)
3052                                 searching = false;
3053                         else
3054                                 skip_iface_check = false;
3055
3056                         if (current_type == TypeManager.object_type)
3057                                 searching = false;
3058                         else {
3059                                 current_type = current_type.BaseType;
3060                                 
3061                                 //
3062                                 // This happens with interfaces, they have a null
3063                                 // basetype.  Look members up in the Object class.
3064                                 //
3065                                 if (current_type == null)
3066                                         current_type = TypeManager.object_type;
3067                         }
3068                         
3069                         if (list.Length == 0)
3070                                 continue;
3071
3072                         //
3073                         // Events and types are returned by both `static' and `instance'
3074                         // searches, which means that our above FindMembers will
3075                         // return two copies of the same.
3076                         //
3077                         if (list.Length == 1 && !(list [0] is MethodBase)){
3078                                 return list;
3079                         }
3080
3081                         //
3082                         // Multiple properties: we query those just to find out the indexer
3083                         // name
3084                         //
3085                         if (list [0] is PropertyInfo)
3086                                 return list;
3087
3088                         //
3089                         // We found an event: the cache lookup returns both the event and
3090                         // its private field.
3091                         //
3092                         if (list [0] is EventInfo) {
3093                                 if ((list.Length == 2) && (list [1] is FieldInfo))
3094                                         return new MemberInfo [] { list [0] };
3095
3096                                 // Oooops
3097                                 return null;
3098                         }
3099
3100                         //
3101                         // We found methods, turn the search into "method scan"
3102                         // mode.
3103                         //
3104
3105                         if (first_members_list != null) {
3106                                 if (use_first_members_list) {
3107                                         method_list = CopyNewMethods (method_list, first_members_list);
3108                                         use_first_members_list = false;
3109                                 }
3110                                 
3111                                 method_list = CopyNewMethods (method_list, list);
3112                         } else {
3113                                 first_members_list = list;
3114                                 use_first_members_list = true;
3115
3116                                 mt &= (MemberTypes.Method | MemberTypes.Constructor);
3117                         }
3118                 } while (searching);
3119
3120                 if (use_first_members_list) {
3121                         foreach (MemberInfo mi in first_members_list) {
3122                                 if (! (mi is MethodBase)) {
3123                                         method_list = CopyNewMethods (method_list, first_members_list);
3124                                         return (MemberInfo []) method_list.ToArray (typeof (MemberInfo));
3125                                 }
3126                         }
3127                         return (MemberInfo []) first_members_list;
3128                 }
3129
3130                 if (method_list != null && method_list.Count > 0) {
3131                         return (MemberInfo []) method_list.ToArray (typeof (MemberInfo));
3132                 }
3133                 //
3134                 // This happens if we already used the cache in the first iteration, in this case
3135                 // the cache already looked in all interfaces.
3136                 //
3137                 if (skip_iface_check)
3138                         return null;
3139
3140                 //
3141                 // Interfaces do not list members they inherit, so we have to
3142                 // scan those.
3143                 // 
3144                 if (!queried_type.IsInterface)
3145                         return null;
3146
3147                 if (queried_type.IsArray)
3148                         queried_type = TypeManager.array_type;
3149                 
3150                 Type [] ifaces = GetInterfaces (queried_type);
3151                 if (ifaces == null)
3152                         return null;
3153                 
3154                 foreach (Type itype in ifaces){
3155                         MemberInfo [] x;
3156
3157                         x = MemberLookup (null, null, itype, mt, bf, name, null);
3158                         if (x != null)
3159                                 return x;
3160                 }
3161                                         
3162                 return null;
3163         }
3164
3165         // Tests whether external method is really special
3166         public static bool IsSpecialMethod (MethodBase mb)
3167         {
3168                 string name = mb.Name;
3169                 if (name.StartsWith ("get_") || name.StartsWith ("set_"))
3170                         return mb.DeclaringType.GetProperty (name.Substring (4)) != null;
3171                                 
3172                 if (name.StartsWith ("add_"))
3173                         return mb.DeclaringType.GetEvent (name.Substring (4)) != null;
3174
3175                 if (name.StartsWith ("remove_"))
3176                         return mb.DeclaringType.GetEvent (name.Substring (7)) != null;
3177
3178                 if (name.StartsWith ("op_")){
3179                         foreach (string oname in Unary.oper_names) {
3180                                 if (oname == name)
3181                                         return true;
3182                         }
3183
3184                         foreach (string oname in Binary.oper_names) {
3185                                 if (oname == name)
3186                                         return true;
3187                         }
3188                 }
3189                 return false;
3190         }
3191                 
3192 #endregion
3193         
3194 }
3195
3196 /// <summary>
3197 ///   There is exactly one instance of this class per type.
3198 /// </summary>
3199 public sealed class TypeHandle : IMemberContainer {
3200         public readonly TypeHandle BaseType;
3201
3202         readonly int id = ++next_id;
3203         static int next_id = 0;
3204
3205         /// <summary>
3206         ///   Lookup a TypeHandle instance for the given type.  If the type doesn't have
3207         ///   a TypeHandle yet, a new instance of it is created.  This static method
3208         ///   ensures that we'll only have one TypeHandle instance per type.
3209         /// </summary>
3210         public static TypeHandle GetTypeHandle (Type t)
3211         {
3212                 TypeHandle handle = (TypeHandle) type_hash [t];
3213                 if (handle != null)
3214                         return handle;
3215
3216                 handle = new TypeHandle (t);
3217                 type_hash.Add (t, handle);
3218                 return handle;
3219         }
3220         
3221         public static void CleanUp ()
3222         {
3223                 type_hash = null;
3224         }
3225
3226         /// <summary>
3227         ///   Returns the TypeHandle for TypeManager.object_type.
3228         /// </summary>
3229         public static IMemberContainer ObjectType {
3230                 get {
3231                         if (object_type != null)
3232                                 return object_type;
3233
3234                         object_type = GetTypeHandle (TypeManager.object_type);
3235
3236                         return object_type;
3237                 }
3238         }
3239
3240         /// <summary>
3241         ///   Returns the TypeHandle for TypeManager.array_type.
3242         /// </summary>
3243         public static IMemberContainer ArrayType {
3244                 get {
3245                         if (array_type != null)
3246                                 return array_type;
3247
3248                         array_type = GetTypeHandle (TypeManager.array_type);
3249
3250                         return array_type;
3251                 }
3252         }
3253
3254         private static PtrHashtable type_hash = new PtrHashtable ();
3255
3256         private static TypeHandle object_type = null;
3257         private static TypeHandle array_type = null;
3258
3259         private Type type;
3260         private string full_name;
3261         private bool is_interface;
3262         private MemberCache member_cache;
3263
3264         private TypeHandle (Type type)
3265         {
3266                 this.type = type;
3267                 full_name = type.FullName != null ? type.FullName : type.Name;
3268                 if (type.BaseType != null)
3269                         BaseType = GetTypeHandle (type.BaseType);
3270                 this.is_interface = type.IsInterface || type.IsGenericParameter;
3271                 this.member_cache = new MemberCache (this);
3272         }
3273
3274         // IMemberContainer methods
3275
3276         public string Name {
3277                 get {
3278                         return full_name;
3279                 }
3280         }
3281
3282         public Type Type {
3283                 get {
3284                         return type;
3285                 }
3286         }
3287
3288         public IMemberContainer ParentContainer {
3289                 get {
3290                         return BaseType;
3291                 }
3292         }
3293
3294         public bool IsInterface {
3295                 get {
3296                         return is_interface;
3297                 }
3298         }
3299
3300         public MemberList GetMembers (MemberTypes mt, BindingFlags bf)
3301         {
3302                 MemberInfo [] members;
3303                 if (type is GenericTypeParameterBuilder)
3304                         return MemberList.Empty;
3305                 if (mt == MemberTypes.Event)
3306                         members = type.GetEvents (bf | BindingFlags.DeclaredOnly);
3307                 else
3308                         members = type.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
3309                                                     null, null);
3310                 Array.Reverse (members);
3311
3312                 return new MemberList (members);
3313         }
3314
3315         // IMemberFinder methods
3316
3317         public MemberList FindMembers (MemberTypes mt, BindingFlags bf, string name,
3318                                        MemberFilter filter, object criteria)
3319         {
3320                 return new MemberList (member_cache.FindMembers (mt, bf, name, filter, criteria));
3321         }
3322
3323         public MemberCache MemberCache {
3324                 get {
3325                         return member_cache;
3326                 }
3327         }
3328
3329         public override string ToString ()
3330         {
3331                 if (BaseType != null)
3332                         return "TypeHandle (" + id + "," + Name + " : " + BaseType + ")";
3333                 else
3334                         return "TypeHandle (" + id + "," + Name + ")";
3335         }
3336 }
3337
3338 }