2 // typemanager.cs: C# type manager
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 // Ravi Pratap (ravi@ximian.com)
6 // Marek Safar (marek.safar@seznam.cz)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001 Ximian, Inc (http://www.ximian.com)
15 // We will eventually remove the SIMPLE_SPEEDUP, and should never change
16 // the behavior of the compilation. This can be removed if we rework
17 // the code to get a list of namespaces available.
19 #define SIMPLE_SPEEDUP
23 using System.Globalization;
24 using System.Collections;
25 using System.Reflection;
26 using System.Reflection.Emit;
28 using System.Text.RegularExpressions;
29 using System.Runtime.CompilerServices;
30 using System.Diagnostics;
32 namespace Mono.CSharp {
39 // A list of core types that the compiler requires or uses
41 static public Type object_type;
42 static public Type value_type;
43 static public Type string_type;
44 static public Type int32_type;
45 static public Type uint32_type;
46 static public Type int64_type;
47 static public Type uint64_type;
48 static public Type float_type;
49 static public Type double_type;
50 static public Type char_type;
51 static public Type char_ptr_type;
52 static public Type short_type;
53 static public Type decimal_type;
54 static public Type bool_type;
55 static public Type sbyte_type;
56 static public Type byte_type;
57 static public Type ushort_type;
58 static public Type enum_type;
59 static public Type delegate_type;
60 static public Type multicast_delegate_type;
61 static public Type void_type;
62 static public Type null_type;
63 static public Type array_type;
64 static public Type runtime_handle_type;
65 static public Type icloneable_type;
66 static public Type type_type;
67 static public Type ienumerator_type;
68 static public Type ienumerable_type;
69 static public Type idisposable_type;
70 static public Type iconvertible_type;
71 static public Type default_member_type;
72 static public Type iasyncresult_type;
73 static public Type asynccallback_type;
74 static public Type intptr_type;
75 static public Type uintptr_type;
76 static public Type monitor_type;
77 static public Type interlocked_type;
78 static public Type runtime_field_handle_type;
79 static public Type runtime_argument_handle_type;
80 static public Type attribute_type;
81 static public Type attribute_usage_type;
82 static public Type decimal_constant_attribute_type;
83 static public Type dllimport_type;
84 static public Type unverifiable_code_type;
85 static public Type methodimpl_attr_type;
86 static public Type marshal_as_attr_type;
87 static public Type param_array_type;
88 static public Type void_ptr_type;
89 static public Type indexer_name_type;
90 static public Type exception_type;
91 static public Type invalid_operation_exception_type;
92 static public Type not_supported_exception_type;
93 static public Type obsolete_attribute_type;
94 static public Type conditional_attribute_type;
95 static public Type in_attribute_type;
96 static public Type out_attribute_type;
97 static public Type extension_attribute_type;
98 static public Type default_parameter_value_attribute_type;
100 static public Type anonymous_method_type;
101 static public Type cls_compliant_attribute_type;
102 static public Type typed_reference_type;
103 static public Type arg_iterator_type;
104 static public Type mbr_type;
105 static public Type struct_layout_attribute_type;
106 static public Type field_offset_attribute_type;
107 static public Type security_attr_type;
108 static public Type required_attr_type;
109 static public Type guid_attr_type;
110 static public Type assembly_culture_attribute_type;
111 static public Type coclass_attr_type;
112 static public Type comimport_attr_type;
118 static internal Type runtime_compatibility_attr_type;
119 static internal Type compiler_generated_attr_type;
120 static internal Type fixed_buffer_attr_type;
121 static internal Type default_charset_type;
122 static internal Type internals_visible_attr_type;
123 static internal Type type_forwarder_attr_type;
127 // Expressions representing the internal types. Used during declaration
130 static public TypeExpr system_object_expr, system_string_expr;
131 static public TypeExpr system_boolean_expr, system_decimal_expr;
132 static public TypeExpr system_single_expr, system_double_expr;
133 static public TypeExpr system_sbyte_expr, system_byte_expr;
134 static public TypeExpr system_int16_expr, system_uint16_expr;
135 static public TypeExpr system_int32_expr, system_uint32_expr;
136 static public TypeExpr system_int64_expr, system_uint64_expr;
137 static public TypeExpr system_char_expr, system_void_expr;
138 static public TypeExpr system_asynccallback_expr;
139 static public TypeExpr system_iasyncresult_expr;
140 static public TypeExpr system_valuetype_expr;
141 static public TypeExpr system_intptr_expr;
144 // This is only used when compiling corlib
146 static public Type system_int32_type;
147 static public Type system_array_type;
148 static public Type system_type_type;
149 static public Type system_assemblybuilder_type;
150 static public MethodInfo system_int_array_get_length;
151 static public MethodInfo system_int_array_get_rank;
152 static public MethodInfo system_object_array_clone;
153 static public MethodInfo system_int_array_get_length_int;
154 static public MethodInfo system_int_array_get_lower_bound_int;
155 static public MethodInfo system_int_array_get_upper_bound_int;
156 static public MethodInfo system_void_array_copyto_array_int;
160 // Internal, not really used outside
162 static Type runtime_helpers_type;
165 // These methods are called by code generated by the compiler
167 static public MethodInfo string_concat_string_string;
168 static public MethodInfo string_concat_string_string_string;
169 static public MethodInfo string_concat_string_string_string_string;
170 static public MethodInfo string_concat_string_dot_dot_dot;
171 static public MethodInfo string_concat_object_object;
172 static public MethodInfo string_concat_object_object_object;
173 static public MethodInfo string_concat_object_dot_dot_dot;
174 static public MethodInfo string_isinterned_string;
175 static public MethodInfo system_type_get_type_from_handle;
176 static public MethodInfo bool_movenext_void;
177 static public MethodInfo ienumerable_getenumerator_void;
178 static public MethodInfo void_reset_void;
179 static public MethodInfo void_dispose_void;
180 static public MethodInfo void_monitor_enter_object;
181 static public MethodInfo void_monitor_exit_object;
182 static public MethodInfo void_initializearray_array_fieldhandle;
183 static public MethodInfo int_getlength_int;
184 static public MethodInfo delegate_combine_delegate_delegate;
185 static public MethodInfo delegate_remove_delegate_delegate;
186 static public MethodInfo int_get_offset_to_string_data;
187 static public MethodInfo int_array_get_length;
188 static public MethodInfo int_array_get_rank;
189 static public MethodInfo object_array_clone;
190 static public MethodInfo int_array_get_length_int;
191 static public MethodInfo int_array_get_lower_bound_int;
192 static public MethodInfo int_array_get_upper_bound_int;
193 static public MethodInfo void_array_copyto_array_int;
194 static public MethodInfo int_interlocked_compare_exchange;
195 static public PropertyInfo ienumerator_getcurrent;
198 // The attribute constructors.
200 static public ConstructorInfo object_ctor;
201 static public ConstructorInfo cons_param_array_attribute;
202 static public ConstructorInfo void_decimal_ctor_five_args;
203 static public ConstructorInfo void_decimal_ctor_int_arg;
204 static public ConstructorInfo unverifiable_code_ctor;
205 static public ConstructorInfo default_member_ctor;
206 static public ConstructorInfo decimal_constant_attribute_ctor;
207 static internal ConstructorInfo struct_layout_attribute_ctor;
208 static public ConstructorInfo field_offset_attribute_ctor;
212 static internal CustomAttributeBuilder compiler_generated_attr;
213 static internal ConstructorInfo fixed_buffer_attr_ctor;
217 static internal CustomAttributeBuilder extension_attribute_attr;
219 static PtrHashtable builder_to_declspace;
221 static PtrHashtable builder_to_member_cache;
224 // Tracks the interfaces implemented by typebuilders. We only
225 // enter those who do implement or or more interfaces
227 static PtrHashtable builder_to_ifaces;
230 // Maps PropertyBuilder to a Type array that contains
231 // the arguments to the indexer
233 static Hashtable indexer_arguments;
236 // Maps a MethodBase to its ParameterData (either InternalParameters or ReflectionParameters)
238 static Hashtable method_params;
241 // A hash table from override methods to their base virtual method.
243 static Hashtable method_overrides;
246 // Keeps track of methods
249 static Hashtable builder_to_method;
252 // Contains all public types from referenced assemblies.
253 // This member is used only if CLS Compliance verification is required.
255 public static Hashtable AllClsTopLevelTypes;
257 static Hashtable fieldbuilders_to_fields;
258 static Hashtable propertybuilder_to_property;
259 static Hashtable fields;
260 static Hashtable events;
263 static PtrHashtable assembly_internals_vis_attrs;
271 public static void CleanUp ()
273 // Lets get everything clean so that we can collect before generating code
274 builder_to_declspace = null;
275 builder_to_member_cache = null;
276 builder_to_ifaces = null;
277 builder_to_type_param = null;
278 indexer_arguments = null;
279 method_params = null;
280 builder_to_method = null;
285 propertybuilder_to_property = null;
288 assembly_internals_vis_attrs = null;
291 TypeHandle.CleanUp ();
295 /// A filter for Findmembers that uses the Signature object to
298 static bool SignatureFilter (MemberInfo mi, object criteria)
300 Signature sig = (Signature) criteria;
302 if (!(mi is MethodBase))
305 if (mi.Name != sig.name)
308 int count = sig.args.Length;
310 if (mi is MethodBuilder || mi is ConstructorBuilder){
311 Type [] candidate_args = GetParameterData ((MethodBase) mi).Types;
313 if (candidate_args.Length != count)
316 for (int i = 0; i < count; i++)
317 if (candidate_args [i] != sig.args [i])
322 ParameterInfo [] pars = ((MethodBase) mi).GetParameters ();
324 if (pars.Length != count)
327 for (int i = 0; i < count; i++)
328 if (pars [i].ParameterType != sig.args [i])
334 // A delegate that points to the filter above.
335 static MemberFilter signature_filter;
338 // These are expressions that represent some of the internal data types, used
341 static void InitExpressionTypes ()
343 system_object_expr = new TypeLookupExpression ("System.Object");
344 system_string_expr = new TypeLookupExpression ("System.String");
345 system_boolean_expr = new TypeLookupExpression ("System.Boolean");
346 system_decimal_expr = new TypeLookupExpression ("System.Decimal");
347 system_single_expr = new TypeLookupExpression ("System.Single");
348 system_double_expr = new TypeLookupExpression ("System.Double");
349 system_sbyte_expr = new TypeLookupExpression ("System.SByte");
350 system_byte_expr = new TypeLookupExpression ("System.Byte");
351 system_int16_expr = new TypeLookupExpression ("System.Int16");
352 system_uint16_expr = new TypeLookupExpression ("System.UInt16");
353 system_int32_expr = new TypeLookupExpression ("System.Int32");
354 system_uint32_expr = new TypeLookupExpression ("System.UInt32");
355 system_int64_expr = new TypeLookupExpression ("System.Int64");
356 system_uint64_expr = new TypeLookupExpression ("System.UInt64");
357 system_char_expr = new TypeLookupExpression ("System.Char");
358 system_void_expr = new TypeLookupExpression ("System.Void");
359 system_asynccallback_expr = new TypeLookupExpression ("System.AsyncCallback");
360 system_iasyncresult_expr = new TypeLookupExpression ("System.IAsyncResult");
361 system_valuetype_expr = new TypeLookupExpression ("System.ValueType");
362 system_intptr_expr = new TypeLookupExpression ("System.IntPtr");
365 static TypeManager ()
369 signature_filter = new MemberFilter (SignatureFilter);
370 InitExpressionTypes ();
373 static public void Reset ()
375 builder_to_declspace = new PtrHashtable ();
376 builder_to_member_cache = new PtrHashtable ();
377 builder_to_method = new PtrHashtable ();
378 builder_to_type_param = new PtrHashtable ();
379 method_params = new PtrHashtable ();
380 method_overrides = new PtrHashtable ();
381 indexer_arguments = new PtrHashtable ();
382 builder_to_ifaces = new PtrHashtable ();
384 fieldbuilders_to_fields = new Hashtable ();
385 propertybuilder_to_property = new Hashtable ();
386 fields = new Hashtable ();
387 type_hash = new DoubleHash ();
390 assembly_internals_vis_attrs = new PtrHashtable ();
393 // to uncover regressions
394 AllClsTopLevelTypes = null;
397 public static void AddUserType (DeclSpace ds)
399 builder_to_declspace.Add (ds.TypeBuilder, ds);
403 // This entry point is used by types that we define under the covers
405 public static void RegisterBuilder (Type tb, Type [] ifaces)
408 builder_to_ifaces [tb] = ifaces;
411 public static void AddMethod (MethodBase builder, IMethodData method)
413 builder_to_method.Add (builder, method);
414 method_params.Add (builder, method.ParameterInfo);
417 public static IMethodData GetMethod (MethodBase builder)
419 return (IMethodData) builder_to_method [builder];
423 /// Returns the DeclSpace whose Type is `t' or null if there is no
424 /// DeclSpace for `t' (ie, the Type comes from a library)
426 public static DeclSpace LookupDeclSpace (Type t)
428 return builder_to_declspace [t] as DeclSpace;
432 /// Returns the TypeContainer whose Type is `t' or null if there is no
433 /// TypeContainer for `t' (ie, the Type comes from a library)
435 public static TypeContainer LookupTypeContainer (Type t)
437 return builder_to_declspace [t] as TypeContainer;
440 public static MemberCache LookupMemberCache (Type t)
442 if (t is TypeBuilder) {
443 IMemberContainer container = builder_to_declspace [t] as IMemberContainer;
444 if (container != null)
445 return container.MemberCache;
449 if (t is GenericTypeParameterBuilder) {
450 IMemberContainer container = builder_to_type_param [t] as IMemberContainer;
452 if (container != null)
453 return container.MemberCache;
457 return TypeHandle.GetMemberCache (t);
460 public static MemberCache LookupBaseInterfacesCache (Type t)
462 Type [] ifaces = GetInterfaces (t);
464 if (ifaces != null && ifaces.Length == 1)
465 return LookupMemberCache (ifaces [0]);
467 // TODO: the builder_to_member_cache should be indexed by 'ifaces', not 't'
468 MemberCache cache = builder_to_member_cache [t] as MemberCache;
472 cache = new MemberCache (ifaces);
473 builder_to_member_cache.Add (t, cache);
477 public static TypeContainer LookupInterface (Type t)
479 TypeContainer tc = (TypeContainer) builder_to_declspace [t];
480 if ((tc == null) || (tc.Kind != Kind.Interface))
486 public static Delegate LookupDelegate (Type t)
488 return builder_to_declspace [t] as Delegate;
491 public static Class LookupClass (Type t)
493 return (Class) builder_to_declspace [t];
497 // We use this hash for multiple kinds of constructed types:
499 // (T, "&") Given T, get T &
500 // (T, "*") Given T, get T *
501 // (T, "[]") Given T and a array dimension, get T []
502 // (T, X) Given a type T and a simple name X, get the type T+X
504 // Accessibility tests, if necessary, should be done by the user
506 static DoubleHash type_hash = new DoubleHash ();
509 // Gets the reference to T version of the Type (T&)
511 public static Type GetReferenceType (Type t)
514 return t.MakeByRefType ();
516 return GetConstructedType (t, "&");
521 // Gets the pointer to T version of the Type (T*)
523 public static Type GetPointerType (Type t)
525 return GetConstructedType (t, "*");
528 public static Type GetConstructedType (Type t, string dim)
531 if (type_hash.Lookup (t, dim, out ret))
534 ret = t.Module.GetType (t.ToString () + dim);
536 type_hash.Insert (t, dim, ret);
541 ret = GetReferenceType (t);
542 type_hash.Insert (t, dim, ret);
547 if (t.IsGenericParameter || t.IsGenericType) {
550 while ((pos < dim.Length) && (dim [pos] == '[')) {
553 if (dim [pos] == ']') {
554 result = result.MakeArrayType ();
557 if (pos < dim.Length)
560 type_hash.Insert (t, dim, result);
565 while (dim [pos] == ',') {
569 if ((dim [pos] != ']') || (pos != dim.Length-1))
572 result = result.MakeArrayType (rank + 1);
573 type_hash.Insert (t, dim, result);
579 type_hash.Insert (t, dim, null);
583 public static Type GetNestedType (Type t, string name)
586 if (!type_hash.Lookup (t, name, out ret)) {
587 ret = t.GetNestedType (name,
588 BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
589 type_hash.Insert (t, name, ret);
595 /// Fills static table with exported types from all referenced assemblies.
596 /// This information is required for CLS Compliance tests.
598 public static void LoadAllImportedTypes ()
600 AllClsTopLevelTypes = new Hashtable (1500);
601 foreach (Assembly a in RootNamespace.Global.Assemblies) {
602 foreach (Type t in a.GetExportedTypes ()) {
603 AllClsTopLevelTypes [t.FullName.ToLower (System.Globalization.CultureInfo.InvariantCulture)] = null;
608 public static bool NamespaceClash (string name, Location loc)
610 if (! RootNamespace.Global.IsNamespace (name))
613 Report.Error (519, loc, String.Format ("`{0}' clashes with a predefined namespace", name));
618 /// Returns the C# name of a type if possible, or the full type name otherwise
620 static public string CSharpName (Type t)
622 if (t == typeof(NullType))
625 string name = GetFullName (t);
627 return Regex.Replace (name,
629 @"(Int32|UInt32|Int16|UInt16|Int64|UInt64|" +
630 @"Single|Double|Char|Decimal|Byte|SByte|Object|" +
631 @"Boolean|String|Void|Null)" +
633 new MatchEvaluator (CSharpNameMatch)).Replace ('+', '.');
636 static public string CSharpName (Type[] types)
638 StringBuilder sb = new StringBuilder ();
639 foreach (Type t in types) {
640 sb.Append (CSharpName (t));
643 sb.Remove (sb.Length - 1, 1);
644 return sb.ToString ();
647 static String CSharpNameMatch (Match match)
649 string s = match.Groups [1].Captures [0].Value;
651 Replace ("int32", "int").
652 Replace ("uint32", "uint").
653 Replace ("int16", "short").
654 Replace ("uint16", "ushort").
655 Replace ("int64", "long").
656 Replace ("uint64", "ulong").
657 Replace ("single", "float").
658 Replace ("boolean", "bool")
659 + match.Groups [2].Captures [0].Value;
662 // Used for error reporting to show symbolic name instead of underlying value
663 public static string CSharpEnumValue (Type t, object value)
665 t = DropGenericTypeArguments (t);
666 Enum e = LookupDeclSpace (t) as Enum;
668 return System.Enum.GetName (t, value);
670 return e.GetDefinition (value).GetSignatureForError ();
674 /// Returns the signature of the method with full namespace classification
676 static public string GetFullNameSignature (MemberInfo mi)
678 PropertyInfo pi = mi as PropertyInfo;
680 MethodBase pmi = pi.GetGetMethod (true);
682 pmi = pi.GetSetMethod (true);
683 if (GetParameterData (pmi).Count > 0)
686 return (mi is MethodBase)
687 ? CSharpSignature (mi as MethodBase)
688 : CSharpName (mi.DeclaringType) + '.' + mi.Name;
692 private static int GetFullName (Type t, StringBuilder sb)
696 if (!t.IsGenericType) {
697 sb.Append (t.FullName);
701 if (t.DeclaringType != null) {
702 pos = GetFullName (t.DeclaringType, sb);
704 } else if (t.Namespace != null && t.Namespace.Length != 0) {
705 sb.Append (t.Namespace);
708 sb.Append (RemoveGenericArity (t.Name));
710 Type[] this_args = GetTypeArguments (t);
712 if (this_args.Length < pos)
713 throw new InternalErrorException (
714 "Enclosing class " + t.DeclaringType + " has more type arguments than " + t);
715 if (this_args.Length == pos)
720 sb.Append (CSharpName (this_args [pos++]));
721 if (pos == this_args.Length)
729 static string GetFullName (Type t)
732 string dimension = t.Name.Substring (t.Name.LastIndexOf ('['));
733 return GetFullName (t.GetElementType ()) + dimension;
736 if (IsNullableType (t) && !t.IsGenericTypeDefinition) {
737 t = GetTypeArguments (t)[0];
738 return CSharpName (t) + "?";
741 if (t.IsGenericParameter)
743 if (!t.IsGenericType)
746 StringBuilder sb = new StringBuilder ();
747 int pos = GetFullName (t, sb);
749 throw new InternalErrorException ("Generic Type " + t + " doesn't have type arguments");
750 return sb.ToString ();
753 public static string GetFullName (Type t)
759 public static string RemoveGenericArity (string from)
761 int i = from.IndexOf ('`');
763 return from.Substring (0, i);
768 /// When we need to report accessors as well
770 static public string CSharpSignature (MethodBase mb)
772 return CSharpSignature (mb, false);
776 /// Returns the signature of the method
778 static public string CSharpSignature (MethodBase mb, bool show_accessor)
780 StringBuilder sig = new StringBuilder (CSharpName (mb.DeclaringType));
783 ParameterData iparams = GetParameterData (mb);
784 string parameters = iparams.GetSignatureForError ();
785 int accessor_end = 0;
787 if (!mb.IsConstructor && TypeManager.IsSpecialMethod (mb)) {
788 Operator.OpType ot = Operator.GetOperatorType (mb.Name);
789 if (ot != Operator.OpType.TOP) {
790 sig.Append ("operator ");
791 sig.Append (Operator.GetName (ot));
792 sig.Append (parameters);
793 return sig.ToString ();
796 bool is_getter = mb.Name.StartsWith ("get_");
797 bool is_setter = mb.Name.StartsWith ("set_");
798 if (is_getter || is_setter || mb.Name.StartsWith ("add_")) {
800 } else if (mb.Name.StartsWith ("remove_")) {
805 if (iparams.Count > (is_getter ? 0 : 1)) {
806 sig.Append ("this[");
808 sig.Append (parameters.Substring (1, parameters.Length - 2));
810 sig.Append (parameters.Substring (1, parameters.LastIndexOf (',') - 1));
813 sig.Append (mb.Name.Substring (accessor_end + 1));
816 if (mb.Name == ".ctor")
817 sig.Append (RemoveGenericArity (mb.DeclaringType.Name));
819 sig.Append (mb.Name);
822 if (TypeManager.IsGenericMethod (mb)) {
823 Type[] args = mb.GetGenericArguments ();
825 for (int i = 0; i < args.Length; i++) {
828 sig.Append (args [i].Name);
835 sig.Append (parameters);
838 if (show_accessor && accessor_end > 0) {
840 sig.Append (mb.Name.Substring (0, accessor_end));
843 return sig.ToString ();
846 public static string GetMethodName (MethodInfo m)
849 if (!IsGenericMethodDefinition (m) && !IsGenericMethod (m))
852 return MemberName.MakeName (m.Name, m.GetGenericArguments ().Length);
858 static public string CSharpSignature (EventInfo ei)
860 return CSharpName (ei.DeclaringType) + "." + ei.Name;
864 /// Looks up a type, and aborts if it is not found. This is used
865 /// by types required by the compiler
867 static Type CoreLookupType (string namespaceName, string name)
869 return CoreLookupType (namespaceName, name, false);
872 static Type CoreLookupType (string ns_name, string name, bool mayFail)
874 Namespace ns = RootNamespace.Global.GetNamespace (ns_name, true);
875 FullNamedExpression fne = ns.Lookup (RootContext.ToplevelTypes, name, Location.Null);
876 Type t = fne == null ? null : fne.Type;
879 Report.Error (518, "The predefined type `" + name + "' is not defined or imported");
883 AttributeTester.RegisterNonObsoleteType (t);
888 /// Returns the MethodInfo for a method named `name' defined
889 /// in type `t' which takes arguments of types `args'
891 static MethodInfo GetMethod (Type t, string name, Type [] args, bool is_private, bool report_errors)
895 BindingFlags flags = instance_and_static | BindingFlags.Public;
901 flags |= BindingFlags.NonPublic;
903 list = FindMembers (t, MemberTypes.Method, flags, signature_filter, sig);
904 if (list.Count == 0) {
906 Report.Error (-19, "Can not find the core function `" + name + "'");
910 MethodInfo mi = list [0] as MethodInfo;
913 Report.Error (-19, "Can not find the core function `" + name + "'");
920 static MethodInfo GetMethod (Type t, string name, Type [] args, bool report_errors)
922 return GetMethod (t, name, args, false, report_errors);
925 public static MethodInfo GetMethod (Type t, string name, Type [] args)
927 return GetMethod (t, name, args, true);
931 /// Returns the PropertyInfo for a property named `name' defined
934 public static PropertyInfo GetProperty (Type t, string name)
936 MemberList list = FindMembers (t, MemberTypes.Property, BindingFlags.Public |
937 BindingFlags.Instance, Type.FilterName, name);
938 if (list.Count == 0) {
939 Report.Error (-19, "Can not find the core property `" + name + "'");
943 PropertyInfo pi = list [0] as PropertyInfo;
945 Report.Error (-19, "Can not find the core function `" + name + "'");
953 /// Returns the ConstructorInfo for "args"
955 public static ConstructorInfo GetConstructor (Type t, Type [] args)
964 throw new InternalErrorException ("Core types haven't been initialized yet?");
966 list = FindMembers (t, MemberTypes.Constructor,
967 instance_and_static | BindingFlags.Public | BindingFlags.DeclaredOnly,
968 signature_filter, sig);
969 if (list.Count == 0){
970 Report.Error (-19, "Can not find the core constructor for type `" + t.Name + "'");
974 ConstructorInfo ci = list [0] as ConstructorInfo;
976 Report.Error (-19, "Can not find the core constructor for type `" + t.Name + "'");
983 public static void InitEnumUnderlyingTypes ()
985 int32_type = CoreLookupType ("System", "Int32");
986 int64_type = CoreLookupType ("System", "Int64");
987 uint32_type = CoreLookupType ("System", "UInt32");
988 uint64_type = CoreLookupType ("System", "UInt64");
989 byte_type = CoreLookupType ("System", "Byte");
990 sbyte_type = CoreLookupType ("System", "SByte");
991 short_type = CoreLookupType ("System", "Int16");
992 ushort_type = CoreLookupType ("System", "UInt16");
994 ienumerator_type = CoreLookupType ("System.Collections", "IEnumerator");
995 ienumerable_type = CoreLookupType ("System.Collections", "IEnumerable");
997 idisposable_type = CoreLookupType ("System", "IDisposable");
1000 InitGenericCoreTypes ();
1005 /// The types have to be initialized after the initial
1006 /// population of the type has happened (for example, to
1007 /// bootstrap the corlib.dll
1009 public static void InitCoreTypes ()
1011 object_type = CoreLookupType ("System", "Object");
1012 system_object_expr.Type = object_type;
1013 value_type = CoreLookupType ("System", "ValueType");
1014 system_valuetype_expr.Type = value_type;
1016 InitEnumUnderlyingTypes ();
1018 char_type = CoreLookupType ("System", "Char");
1019 string_type = CoreLookupType ("System", "String");
1020 float_type = CoreLookupType ("System", "Single");
1021 double_type = CoreLookupType ("System", "Double");
1022 char_ptr_type = GetPointerType (char_type);
1023 decimal_type = CoreLookupType ("System", "Decimal");
1024 bool_type = CoreLookupType ("System", "Boolean");
1025 enum_type = CoreLookupType ("System", "Enum");
1027 multicast_delegate_type = CoreLookupType ("System", "MulticastDelegate");
1028 delegate_type = CoreLookupType ("System", "Delegate");
1030 array_type = CoreLookupType ("System", "Array");
1031 void_type = CoreLookupType ("System", "Void");
1032 type_type = CoreLookupType ("System", "Type");
1034 runtime_field_handle_type = CoreLookupType ("System", "RuntimeFieldHandle");
1035 runtime_argument_handle_type = CoreLookupType ("System", "RuntimeArgumentHandle");
1036 runtime_helpers_type = CoreLookupType ("System.Runtime.CompilerServices", "RuntimeHelpers");
1037 default_member_type = CoreLookupType ("System.Reflection", "DefaultMemberAttribute");
1038 runtime_handle_type = CoreLookupType ("System", "RuntimeTypeHandle");
1039 asynccallback_type = CoreLookupType ("System", "AsyncCallback");
1040 iasyncresult_type = CoreLookupType ("System", "IAsyncResult");
1041 icloneable_type = CoreLookupType ("System", "ICloneable");
1042 iconvertible_type = CoreLookupType ("System", "IConvertible");
1043 interlocked_type = CoreLookupType ("System.Threading", "Interlocked");
1044 monitor_type = CoreLookupType ("System.Threading", "Monitor");
1045 intptr_type = CoreLookupType ("System", "IntPtr");
1046 uintptr_type = CoreLookupType ("System", "UIntPtr");
1048 attribute_type = CoreLookupType ("System", "Attribute");
1049 attribute_usage_type = CoreLookupType ("System", "AttributeUsageAttribute");
1050 dllimport_type = CoreLookupType ("System.Runtime.InteropServices", "DllImportAttribute");
1051 methodimpl_attr_type = CoreLookupType ("System.Runtime.CompilerServices", "MethodImplAttribute");
1052 marshal_as_attr_type = CoreLookupType ("System.Runtime.InteropServices", "MarshalAsAttribute");
1053 param_array_type = CoreLookupType ("System", "ParamArrayAttribute");
1054 in_attribute_type = CoreLookupType ("System.Runtime.InteropServices", "InAttribute");
1055 out_attribute_type = CoreLookupType ("System.Runtime.InteropServices", "OutAttribute");
1057 // this can fail if the user doesn't have an -r:System.dll
1058 default_parameter_value_attribute_type = CoreLookupType ("System.Runtime.InteropServices", "DefaultParameterValueAttribute", true);
1060 typed_reference_type = CoreLookupType ("System", "TypedReference");
1061 arg_iterator_type = CoreLookupType ("System", "ArgIterator");
1062 mbr_type = CoreLookupType ("System", "MarshalByRefObject");
1063 decimal_constant_attribute_type = CoreLookupType ("System.Runtime.CompilerServices", "DecimalConstantAttribute");
1065 unverifiable_code_type= CoreLookupType ("System.Security", "UnverifiableCodeAttribute");
1067 void_ptr_type = GetPointerType (void_type);
1069 indexer_name_type = CoreLookupType ("System.Runtime.CompilerServices", "IndexerNameAttribute");
1071 exception_type = CoreLookupType ("System", "Exception");
1072 invalid_operation_exception_type = CoreLookupType ("System", "InvalidOperationException");
1073 not_supported_exception_type = CoreLookupType ("System", "NotSupportedException");
1078 obsolete_attribute_type = CoreLookupType ("System", "ObsoleteAttribute");
1079 conditional_attribute_type = CoreLookupType ("System.Diagnostics", "ConditionalAttribute");
1080 cls_compliant_attribute_type = CoreLookupType ("System", "CLSCompliantAttribute");
1081 struct_layout_attribute_type = CoreLookupType ("System.Runtime.InteropServices", "StructLayoutAttribute");
1082 field_offset_attribute_type = CoreLookupType ("System.Runtime.InteropServices", "FieldOffsetAttribute");
1083 security_attr_type = CoreLookupType ("System.Security.Permissions", "SecurityAttribute");
1084 required_attr_type = CoreLookupType ("System.Runtime.CompilerServices", "RequiredAttributeAttribute");
1085 guid_attr_type = CoreLookupType ("System.Runtime.InteropServices", "GuidAttribute");
1086 assembly_culture_attribute_type = CoreLookupType ("System.Reflection", "AssemblyCultureAttribute");
1087 comimport_attr_type = CoreLookupType ("System.Runtime.InteropServices", "ComImportAttribute");
1088 coclass_attr_type = CoreLookupType ("System.Runtime.InteropServices", "CoClassAttribute");
1094 compiler_generated_attr_type = CoreLookupType ("System.Runtime.CompilerServices", "CompilerGeneratedAttribute");
1095 fixed_buffer_attr_type = CoreLookupType ("System.Runtime.CompilerServices", "FixedBufferAttribute");
1096 default_charset_type = CoreLookupType ("System.Runtime.InteropServices", "DefaultCharSetAttribute");
1097 internals_visible_attr_type = CoreLookupType ("System.Runtime.CompilerServices", "InternalsVisibleToAttribute");
1098 runtime_compatibility_attr_type = CoreLookupType ("System.Runtime.CompilerServices", "RuntimeCompatibilityAttribute");
1099 type_forwarder_attr_type = CoreLookupType ("System.Runtime.CompilerServices", "TypeForwardedToAttribute");
1104 extension_attribute_type = CoreLookupType("System.Runtime.CompilerServices", "ExtensionAttribute", true);
1108 // When compiling corlib, store the "real" types here.
1110 if (!RootContext.StdLib) {
1111 system_int32_type = typeof (System.Int32);
1112 system_array_type = typeof (System.Array);
1113 system_type_type = typeof (System.Type);
1114 system_assemblybuilder_type = typeof (System.Reflection.Emit.AssemblyBuilder);
1116 system_int_array_get_length = GetMethod (
1117 system_array_type, "get_Length", Type.EmptyTypes);
1118 system_int_array_get_rank = GetMethod (
1119 system_array_type, "get_Rank", Type.EmptyTypes);
1120 system_object_array_clone = GetMethod (
1121 system_array_type, "Clone", Type.EmptyTypes);
1123 Type [] system_int_arg = { system_int32_type };
1124 system_int_array_get_length_int = GetMethod (
1125 system_array_type, "GetLength", system_int_arg);
1126 system_int_array_get_upper_bound_int = GetMethod (
1127 system_array_type, "GetUpperBound", system_int_arg);
1128 system_int_array_get_lower_bound_int = GetMethod (
1129 system_array_type, "GetLowerBound", system_int_arg);
1131 Type [] system_array_int_arg = { system_array_type, system_int32_type };
1132 system_void_array_copyto_array_int = GetMethod (
1133 system_array_type, "CopyTo", system_array_int_arg);
1135 Type [] system_3_type_arg = {
1136 system_type_type, system_type_type, system_type_type };
1137 Type [] system_4_type_arg = {
1138 system_type_type, system_type_type, system_type_type, system_type_type };
1140 MethodInfo set_corlib_type_builders = GetMethod (
1141 system_assemblybuilder_type, "SetCorlibTypeBuilders",
1142 system_4_type_arg, true, false);
1144 if (set_corlib_type_builders != null) {
1145 object[] args = new object [4];
1146 args [0] = object_type;
1147 args [1] = value_type;
1148 args [2] = enum_type;
1149 args [3] = void_type;
1151 set_corlib_type_builders.Invoke (CodeGen.Assembly.Builder, args);
1153 // Compatibility for an older version of the class libs.
1154 set_corlib_type_builders = GetMethod (
1155 system_assemblybuilder_type, "SetCorlibTypeBuilders",
1156 system_3_type_arg, true, true);
1158 if (set_corlib_type_builders == null) {
1159 Report.Error (-26, "Corlib compilation is not supported in Microsoft.NET due to bugs in it");
1163 object[] args = new object [3];
1164 args [0] = object_type;
1165 args [1] = value_type;
1166 args [2] = enum_type;
1168 set_corlib_type_builders.Invoke (CodeGen.Assembly.Builder, args);
1172 system_string_expr.Type = string_type;
1173 system_boolean_expr.Type = bool_type;
1174 system_decimal_expr.Type = decimal_type;
1175 system_single_expr.Type = float_type;
1176 system_double_expr.Type = double_type;
1177 system_sbyte_expr.Type = sbyte_type;
1178 system_byte_expr.Type = byte_type;
1179 system_int16_expr.Type = short_type;
1180 system_uint16_expr.Type = ushort_type;
1181 system_int32_expr.Type = int32_type;
1182 system_uint32_expr.Type = uint32_type;
1183 system_int64_expr.Type = int64_type;
1184 system_uint64_expr.Type = uint64_type;
1185 system_char_expr.Type = char_type;
1186 system_void_expr.Type = void_type;
1187 system_asynccallback_expr.Type = asynccallback_type;
1188 system_iasyncresult_expr.Type = iasyncresult_type;
1191 // These are only used for compare purposes
1193 anonymous_method_type = typeof (AnonymousMethod);
1194 null_type = typeof (NullType);
1198 // The helper methods that are used by the compiler
1200 public static void InitCodeHelpers ()
1203 // Now load the default methods that we use.
1205 Type [] string_string = { string_type, string_type };
1206 string_concat_string_string = GetMethod (
1207 string_type, "Concat", string_string);
1208 Type [] string_string_string = { string_type, string_type, string_type };
1209 string_concat_string_string_string = GetMethod (
1210 string_type, "Concat", string_string_string);
1211 Type [] string_string_string_string = { string_type, string_type, string_type, string_type };
1212 string_concat_string_string_string_string = GetMethod (
1213 string_type, "Concat", string_string_string_string);
1214 Type[] params_string = { GetConstructedType (string_type, "[]") };
1215 string_concat_string_dot_dot_dot = GetMethod (
1216 string_type, "Concat", params_string);
1218 Type [] object_object = { object_type, object_type };
1219 string_concat_object_object = GetMethod (
1220 string_type, "Concat", object_object);
1221 Type [] object_object_object = { object_type, object_type, object_type };
1222 string_concat_object_object_object = GetMethod (
1223 string_type, "Concat", object_object_object);
1224 Type[] params_object = { GetConstructedType (object_type, "[]") };
1225 string_concat_object_dot_dot_dot = GetMethod (
1226 string_type, "Concat", params_object);
1228 Type [] string_ = { string_type };
1229 string_isinterned_string = GetMethod (
1230 string_type, "IsInterned", string_);
1232 Type [] runtime_type_handle = { runtime_handle_type };
1233 system_type_get_type_from_handle = GetMethod (
1234 type_type, "GetTypeFromHandle", runtime_type_handle);
1236 Type [] delegate_delegate = { delegate_type, delegate_type };
1237 delegate_combine_delegate_delegate = GetMethod (
1238 delegate_type, "Combine", delegate_delegate);
1240 delegate_remove_delegate_delegate = GetMethod (
1241 delegate_type, "Remove", delegate_delegate);
1246 ienumerator_getcurrent = GetProperty (
1247 ienumerator_type, "Current");
1248 bool_movenext_void = GetMethod (
1249 ienumerator_type, "MoveNext", Type.EmptyTypes);
1250 void_reset_void = GetMethod (
1251 ienumerator_type, "Reset", Type.EmptyTypes);
1252 void_dispose_void = GetMethod (
1253 idisposable_type, "Dispose", Type.EmptyTypes);
1254 int_get_offset_to_string_data = GetMethod (
1255 runtime_helpers_type, "get_OffsetToStringData", Type.EmptyTypes);
1256 int_array_get_length = GetMethod (
1257 array_type, "get_Length", Type.EmptyTypes);
1258 int_array_get_rank = GetMethod (
1259 array_type, "get_Rank", Type.EmptyTypes);
1260 ienumerable_getenumerator_void = GetMethod (
1261 ienumerable_type, "GetEnumerator", Type.EmptyTypes);
1266 Type [] int_arg = { int32_type };
1267 int_array_get_length_int = GetMethod (
1268 array_type, "GetLength", int_arg);
1269 int_array_get_upper_bound_int = GetMethod (
1270 array_type, "GetUpperBound", int_arg);
1271 int_array_get_lower_bound_int = GetMethod (
1272 array_type, "GetLowerBound", int_arg);
1275 // System.Array methods
1277 object_array_clone = GetMethod (
1278 array_type, "Clone", Type.EmptyTypes);
1279 Type [] array_int_arg = { array_type, int32_type };
1280 void_array_copyto_array_int = GetMethod (
1281 array_type, "CopyTo", array_int_arg);
1286 Type [] object_arg = { object_type };
1287 void_monitor_enter_object = GetMethod (
1288 monitor_type, "Enter", object_arg);
1289 void_monitor_exit_object = GetMethod (
1290 monitor_type, "Exit", object_arg);
1292 Type [] array_field_handle_arg = { array_type, runtime_field_handle_type };
1294 void_initializearray_array_fieldhandle = GetMethod (
1295 runtime_helpers_type, "InitializeArray", array_field_handle_arg);
1300 int_getlength_int = GetMethod (
1301 array_type, "GetLength", int_arg);
1304 // Decimal constructors
1306 Type [] dec_arg = { int32_type, int32_type, int32_type, bool_type, byte_type };
1307 void_decimal_ctor_five_args = GetConstructor (
1308 decimal_type, dec_arg);
1310 void_decimal_ctor_int_arg = GetConstructor (decimal_type, int_arg);
1315 unverifiable_code_ctor = GetConstructor (unverifiable_code_type, Type.EmptyTypes);
1316 default_member_ctor = GetConstructor (default_member_type, string_);
1317 cons_param_array_attribute = GetConstructor (param_array_type, Type.EmptyTypes);
1319 Type[] short_arg = { short_type };
1320 struct_layout_attribute_ctor = GetConstructor (struct_layout_attribute_type, short_arg);
1322 decimal_constant_attribute_ctor = GetConstructor (decimal_constant_attribute_type, new Type []
1323 { byte_type, byte_type, uint32_type, uint32_type, uint32_type } );
1325 field_offset_attribute_ctor = GetConstructor (field_offset_attribute_type, new Type []
1329 // System.Threading.CompareExchange
1331 Type[] compare_exchange_types = {
1332 GetReferenceType (int32_type), int32_type, int32_type };
1333 int_interlocked_compare_exchange = GetMethod (
1334 interlocked_type, "CompareExchange", compare_exchange_types);
1340 compiler_generated_attr = new CustomAttributeBuilder (
1341 GetConstructor (compiler_generated_attr_type, Type.EmptyTypes), new object[0]);
1343 Type[] type_int_arg = { type_type, int32_type };
1344 fixed_buffer_attr_ctor = GetConstructor (fixed_buffer_attr_type, type_int_arg);
1351 object_ctor = GetConstructor (object_type, Type.EmptyTypes);
1354 InitGenericCodeHelpers ();
1359 public static MethodInfo enumerable_select;
1361 static void InitSystemCore ()
1363 if (RootContext.Version != LanguageVersion.LINQ)
1366 if (extension_attribute_type != null)
1367 extension_attribute_attr = new CustomAttributeBuilder (
1368 GetConstructor (extension_attribute_type, Type.EmptyTypes), new object[0]);
1370 Type t = CoreLookupType ("System.Linq", "Enumerable");
1371 MemberList select = TypeManager.FindMembers (t, MemberTypes.Method, BindingFlags.Static | BindingFlags.Public,
1372 Type.FilterName, "Select");
1374 // TODO: implement correct selection
1375 enumerable_select = ((MethodInfo)select[0]);
1379 const BindingFlags instance_and_static = BindingFlags.Static | BindingFlags.Instance;
1382 /// This is the "old", non-cache based FindMembers() function. We cannot use
1383 /// the cache here because there is no member name argument.
1385 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1386 MemberFilter filter, object criteria)
1388 #if MS_COMPATIBLE && GMCS_SOURCE
1389 if (t.IsGenericType)
1390 t = t.GetGenericTypeDefinition ();
1393 DeclSpace decl = (DeclSpace) builder_to_declspace [t];
1396 // `builder_to_declspace' contains all dynamic types.
1400 Timer.StartTimer (TimerType.FindMembers);
1401 list = decl.FindMembers (mt, bf, filter, criteria);
1402 Timer.StopTimer (TimerType.FindMembers);
1407 // We have to take care of arrays specially, because GetType on
1408 // a TypeBuilder array will return a Type, not a TypeBuilder,
1409 // and we can not call FindMembers on this type.
1412 #if MS_COMPATIBLE && GMCS_SOURCE
1415 t.IsSubclassOf (TypeManager.array_type))
1416 return new MemberList (TypeManager.array_type.FindMembers (mt, bf, filter, criteria));
1419 if (t is GenericTypeParameterBuilder) {
1420 TypeParameter tparam = (TypeParameter) builder_to_type_param [t];
1422 Timer.StartTimer (TimerType.FindMembers);
1423 MemberList list = tparam.FindMembers (
1424 mt, bf | BindingFlags.DeclaredOnly, filter, criteria);
1425 Timer.StopTimer (TimerType.FindMembers);
1431 // Since FindMembers will not lookup both static and instance
1432 // members, we emulate this behaviour here.
1434 if ((bf & instance_and_static) == instance_and_static){
1435 MemberInfo [] i_members = t.FindMembers (
1436 mt, bf & ~BindingFlags.Static, filter, criteria);
1438 int i_len = i_members.Length;
1440 MemberInfo one = i_members [0];
1443 // If any of these are present, we are done!
1445 if ((one is Type) || (one is EventInfo) || (one is FieldInfo))
1446 return new MemberList (i_members);
1449 MemberInfo [] s_members = t.FindMembers (
1450 mt, bf & ~BindingFlags.Instance, filter, criteria);
1452 int s_len = s_members.Length;
1453 if (i_len > 0 || s_len > 0)
1454 return new MemberList (i_members, s_members);
1457 return new MemberList (i_members);
1459 return new MemberList (s_members);
1463 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
1468 /// This method is only called from within MemberLookup. It tries to use the member
1469 /// cache if possible and falls back to the normal FindMembers if not. The `used_cache'
1470 /// flag tells the caller whether we used the cache or not. If we used the cache, then
1471 /// our return value will already contain all inherited members and the caller don't need
1472 /// to check base classes and interfaces anymore.
1474 private static MemberInfo [] MemberLookup_FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1475 string name, out bool used_cache)
1479 #if GMCS_SOURCE && MS_COMPATIBLE
1480 if (t.IsGenericType && !t.IsGenericTypeDefinition)
1481 t = t.GetGenericTypeDefinition();
1485 // If this is a dynamic type, it's always in the `builder_to_declspace' hash table
1486 // and we can ask the DeclSpace for the MemberCache.
1488 if (t is TypeBuilder) {
1489 DeclSpace decl = (DeclSpace) builder_to_declspace [t];
1490 cache = decl.MemberCache;
1493 // If this DeclSpace has a MemberCache, use it.
1496 if (cache != null) {
1498 return cache.FindMembers (
1499 mt, bf, name, FilterWithClosure_delegate, null);
1502 // If there is no MemberCache, we need to use the "normal" FindMembers.
1503 // Note, this is a VERY uncommon route!
1506 Timer.StartTimer (TimerType.FindMembers);
1507 list = decl.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
1508 FilterWithClosure_delegate, name);
1509 Timer.StopTimer (TimerType.FindMembers);
1511 return (MemberInfo []) list;
1515 // We have to take care of arrays specially, because GetType on
1516 // a TypeBuilder array will return a Type, not a TypeBuilder,
1517 // and we can not call FindMembers on this type.
1519 if (t.IsArray) { // == TypeManager.array_type || t.IsSubclassOf (TypeManager.array_type)) {
1521 return TypeHandle.ArrayType.MemberCache.FindMembers (
1522 mt, bf, name, FilterWithClosure_delegate, null);
1526 if (t is GenericTypeParameterBuilder) {
1527 TypeParameter tparam = (TypeParameter) builder_to_type_param [t];
1530 if (tparam.MemberCache == null)
1531 return new MemberInfo [0];
1533 return tparam.MemberCache.FindMembers (
1534 mt, bf, name, FilterWithClosure_delegate, null);
1538 if (IsGenericType (t) && (mt == MemberTypes.NestedType)) {
1540 // This happens if we're resolving a class'es base class and interfaces
1541 // in TypeContainer.DefineType(). At this time, the types aren't
1542 // populated yet, so we can't use the cache.
1544 MemberInfo[] info = t.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
1545 FilterWithClosure_delegate, name);
1551 // This call will always succeed. There is exactly one TypeHandle instance per
1552 // type, TypeHandle.GetMemberCache() will, if necessary, create a new one, and return
1553 // the corresponding MemberCache.
1555 cache = TypeHandle.GetMemberCache (t);
1558 return cache.FindMembers (mt, bf, name, FilterWithClosure_delegate, null);
1561 public static bool IsBuiltinType (Type t)
1563 t = TypeToCoreType (t);
1564 if (t == object_type || t == string_type || t == int32_type || t == uint32_type ||
1565 t == int64_type || t == uint64_type || t == float_type || t == double_type ||
1566 t == char_type || t == short_type || t == decimal_type || t == bool_type ||
1567 t == sbyte_type || t == byte_type || t == ushort_type || t == void_type)
1573 public static bool IsBuiltinType (TypeContainer tc)
1575 return IsBuiltinType (tc.TypeBuilder);
1579 // This is like IsBuiltinType, but lacks decimal_type, we should also clean up
1580 // the pieces in the code where we use IsBuiltinType and special case decimal_type.
1582 public static bool IsPrimitiveType (Type t)
1584 return (t == int32_type || t == uint32_type ||
1585 t == int64_type || t == uint64_type || t == float_type || t == double_type ||
1586 t == char_type || t == short_type || t == bool_type ||
1587 t == sbyte_type || t == byte_type || t == ushort_type);
1590 public static bool IsDelegateType (Type t)
1592 t = DropGenericTypeArguments (t);
1593 if (t.IsSubclassOf (TypeManager.delegate_type))
1599 public static bool IsEnumType (Type t)
1601 t = DropGenericTypeArguments (t);
1602 if (builder_to_declspace [t] is Enum)
1605 #if MS_COMPATIBLE && GMCS_SOURCE
1606 if (t.IsGenericParameter || t.IsGenericType)
1612 public static bool IsBuiltinOrEnum (Type t)
1614 if (IsBuiltinType (t))
1623 public static bool IsNullType (Type t)
1625 return t == null_type;
1628 public static bool IsAttributeType (Type t)
1630 return t == attribute_type && t.BaseType != null || IsSubclassOf (t, attribute_type);
1633 static Stack unmanaged_enclosing_types = new Stack (4);
1636 // Whether a type is unmanaged. This is used by the unsafe code (25.2)
1638 public static bool IsUnmanagedType (Type t)
1640 // Avoid infloops in the case of: unsafe struct Foo { Foo *x; }
1641 if (unmanaged_enclosing_types.Contains (t))
1644 // builtins that are not unmanaged types
1645 if (t == TypeManager.object_type || t == TypeManager.string_type)
1648 if (IsGenericType (t) || IsGenericParameter (t))
1651 if (IsBuiltinOrEnum (t))
1654 // Someone did the work of checking if the ElementType of t is unmanaged. Let's not repeat it.
1658 // Arrays are disallowed, even if we mark them with [MarshalAs(UnmanagedType.ByValArray, ...)]
1662 if (!IsValueType (t))
1666 for (Type p = t.DeclaringType; p != null; p = p.DeclaringType) {
1667 if (p.IsGenericTypeDefinition)
1672 unmanaged_enclosing_types.Push (t);
1676 if (t is TypeBuilder) {
1677 TypeContainer tc = LookupTypeContainer (t);
1678 if (tc.Fields != null){
1679 foreach (FieldBase f in tc.Fields){
1680 // Avoid using f.FieldBuilder: f.Define () may not yet have been invoked.
1681 if ((f.ModFlags & Modifiers.STATIC) != 0)
1683 if (f.MemberType == null)
1685 if (!IsUnmanagedType (f.MemberType)){
1686 Report.SymbolRelatedToPreviousError (f.Location, CSharpName (t) + "." + f.Name);
1692 FieldInfo [] fields = t.GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
1694 foreach (FieldInfo f in fields){
1695 if (!IsUnmanagedType (f.FieldType)){
1696 Report.SymbolRelatedToPreviousError (f);
1702 unmanaged_enclosing_types.Pop ();
1707 public static bool IsValueType (Type t)
1709 return t.IsValueType || IsGenericParameter (t);
1712 public static bool IsInterfaceType (Type t)
1714 TypeContainer tc = (TypeContainer) builder_to_declspace [t];
1718 return tc.Kind == Kind.Interface;
1721 public static bool IsSubclassOf (Type type, Type base_type)
1723 TypeParameter tparam = LookupTypeParameter (type);
1724 TypeParameter pparam = LookupTypeParameter (base_type);
1726 if ((tparam != null) && (pparam != null)) {
1727 if (tparam == pparam)
1730 return tparam.IsSubclassOf (base_type);
1733 #if MS_COMPATIBLE && GMCS_SOURCE
1734 if (type.IsGenericType)
1735 type = type.GetGenericTypeDefinition ();
1738 if (type.IsSubclassOf (base_type))
1742 if (IsEqual (type, base_type))
1745 type = type.BaseType;
1746 } while (type != null);
1751 public static bool IsPrivateAccessible (Type type, Type parent)
1756 if (type.Equals (parent))
1759 return DropGenericTypeArguments (type) == DropGenericTypeArguments (parent);
1762 public static bool IsFamilyAccessible (Type type, Type parent)
1764 TypeParameter tparam = LookupTypeParameter (type);
1765 TypeParameter pparam = LookupTypeParameter (parent);
1767 if ((tparam != null) && (pparam != null)) {
1768 if (tparam == pparam)
1771 return tparam.IsSubclassOf (parent);
1775 if (IsInstantiationOfSameGenericType (type, parent))
1778 type = type.BaseType;
1779 } while (type != null);
1785 // Checks whether `type' is a subclass or nested child of `base_type'.
1787 public static bool IsNestedFamilyAccessible (Type type, Type base_type)
1790 if (IsFamilyAccessible (type, base_type))
1793 // Handle nested types.
1794 type = type.DeclaringType;
1795 } while (type != null);
1801 // Checks whether `type' is a nested child of `parent'.
1803 public static bool IsNestedChildOf (Type type, Type parent)
1808 type = DropGenericTypeArguments (type);
1809 parent = DropGenericTypeArguments (parent);
1811 if (IsEqual (type, parent))
1814 type = type.DeclaringType;
1815 while (type != null) {
1816 if (IsEqual (type, parent))
1819 type = type.DeclaringType;
1827 // Checks whether `extern_type' is friend of the output assembly
1829 public static bool IsFriendAssembly (Assembly assembly)
1831 // FIXME: This should not be reached
1832 if (assembly == CodeGen.Assembly.Builder)
1835 if (assembly_internals_vis_attrs.Contains (assembly))
1836 return (bool)(assembly_internals_vis_attrs [assembly]);
1838 object [] attrs = assembly.GetCustomAttributes (internals_visible_attr_type, false);
1839 if (attrs.Length == 0) {
1840 assembly_internals_vis_attrs.Add (assembly, false);
1844 AssemblyName this_name = CodeGen.Assembly.Name;
1845 byte [] this_token = this_name.GetPublicKeyToken ();
1846 bool is_friend = false;
1847 foreach (InternalsVisibleToAttribute attr in attrs) {
1848 if (attr.AssemblyName == null || attr.AssemblyName.Length == 0)
1851 AssemblyName aname = null;
1853 aname = new AssemblyName (attr.AssemblyName);
1854 } catch (FileLoadException) {
1855 } catch (ArgumentException) {
1858 if (aname == null || aname.Name != this_name.Name)
1861 byte [] key_token = aname.GetPublicKeyToken ();
1862 if (key_token != null) {
1863 if (this_token == null) {
1864 // Same name, but key token is null
1865 Error_FriendAccessNameNotMatching (aname.FullName);
1869 if (!CompareKeyTokens (this_token, key_token))
1877 assembly_internals_vis_attrs.Add (assembly, is_friend);
1881 static bool CompareKeyTokens (byte [] token1, byte [] token2)
1883 for (int i = 0; i < token1.Length; i++)
1884 if (token1 [i] != token2 [i])
1890 static void Error_FriendAccessNameNotMatching (string other_name)
1892 Report.Error (281, "Friend access was granted to `" + other_name +
1893 "', but the output assembly is named `" + CodeGen.Assembly.Name.FullName +
1894 "'. Try adding a reference to `" + other_name +
1895 "' or change the output assembly name to match it");
1898 public static bool IsFriendAssembly (Assembly assembly)
1905 // Do the right thing when returning the element type of an
1906 // array type based on whether we are compiling corlib or not
1908 public static Type GetElementType (Type t)
1910 if (RootContext.StdLib)
1911 return t.GetElementType ();
1913 return TypeToCoreType (t.GetElementType ());
1917 /// This method is not implemented by MS runtime for dynamic types
1919 public static bool HasElementType (Type t)
1921 return t.IsArray || t.IsPointer || t.IsByRef;
1925 /// Gigantic work around for missing features in System.Reflection.Emit follows.
1929 /// Since System.Reflection.Emit can not return MethodBase.GetParameters
1930 /// for anything which is dynamic, and we need this in a number of places,
1931 /// we register this information here, and use it afterwards.
1933 static public void RegisterMethod (MethodBase mb, Parameters ip)
1935 method_params.Add (mb, ip);
1938 static public ParameterData GetParameterData (MethodBase mb)
1940 ParameterData pd = (ParameterData)method_params [mb];
1942 if (mb is MethodBuilder || mb is ConstructorBuilder)
1943 throw new InternalErrorException ("Argument for Method not registered" + mb);
1946 if (mb.IsGenericMethod && !mb.IsGenericMethodDefinition)
1947 return GetParameterData (((MethodInfo)mb).GetGenericMethodDefinition ());
1949 pd = new ReflectionParameters (mb);
1950 method_params.Add (mb, pd);
1955 static public void RegisterOverride (MethodBase override_method, MethodBase base_method)
1957 if (!method_overrides.Contains (override_method))
1958 method_overrides [override_method] = base_method;
1959 if (method_overrides [override_method] != base_method)
1960 throw new InternalErrorException ("Override mismatch: " + override_method);
1963 static public bool IsOverride (MethodBase m)
1965 m = DropGenericMethodArguments (m);
1967 return m.IsVirtual &&
1968 (m.Attributes & MethodAttributes.NewSlot) == 0 &&
1969 (m is MethodBuilder || method_overrides.Contains (m));
1972 static public MethodBase TryGetBaseDefinition (MethodBase m)
1974 m = DropGenericMethodArguments (m);
1976 return (MethodBase) method_overrides [m];
1980 /// Returns the argument types for an indexer based on its PropertyInfo
1982 /// For dynamic indexers, we use the compiler provided types, for
1983 /// indexers from existing assemblies we load them from GetParameters,
1984 /// and insert them into the cache
1986 static public Type [] GetArgumentTypes (PropertyInfo indexer)
1988 if (indexer_arguments.Contains (indexer))
1989 return (Type []) indexer_arguments [indexer];
1990 else if (indexer is PropertyBuilder)
1991 // If we're a PropertyBuilder and not in the
1992 // `indexer_arguments' hash, then we're a property and
1994 return Type.EmptyTypes;
1996 ParameterInfo [] pi = indexer.GetIndexParameters ();
1997 // Property, not an indexer.
1999 return Type.EmptyTypes;
2001 Type [] types = new Type [c];
2003 for (int i = 0; i < c; i++)
2004 types [i] = pi [i].ParameterType;
2006 indexer_arguments.Add (indexer, types);
2011 public static void RegisterConstant (FieldInfo fb, IConstant ic)
2013 fields.Add (fb, ic);
2016 public static IConstant GetConstant (FieldInfo fb)
2021 return (IConstant)fields [fb];
2024 public static void RegisterProperty (PropertyInfo pi, PropertyBase pb)
2026 propertybuilder_to_property.Add (pi, pb);
2029 public static PropertyBase GetProperty (PropertyInfo pi)
2031 return (PropertyBase)propertybuilder_to_property [pi];
2034 static public void RegisterFieldBase (FieldBuilder fb, FieldBase f)
2036 fieldbuilders_to_fields.Add (fb, f);
2040 // The return value can be null; This will be the case for
2041 // auxiliary FieldBuilders created by the compiler that have no
2042 // real field being declared on the source code
2044 static public FieldBase GetField (FieldInfo fb)
2047 fb = GetGenericFieldDefinition (fb);
2049 return (FieldBase) fieldbuilders_to_fields [fb];
2052 static public MethodInfo GetAddMethod (EventInfo ei)
2054 if (ei is MyEventBuilder) {
2055 return ((MyEventBuilder)ei).GetAddMethod (true);
2057 return ei.GetAddMethod (true);
2060 static public MethodInfo GetRemoveMethod (EventInfo ei)
2062 if (ei is MyEventBuilder) {
2063 return ((MyEventBuilder)ei).GetRemoveMethod (true);
2065 return ei.GetRemoveMethod (true);
2068 static public void RegisterEventField (EventInfo einfo, EventField e)
2071 events = new Hashtable ();
2073 events.Add (einfo, e);
2076 static public EventField GetEventField (EventInfo ei)
2081 return (EventField) events [ei];
2084 static public bool RegisterIndexer (PropertyBuilder pb, MethodBase get,
2085 MethodBase set, Type[] args)
2087 indexer_arguments.Add (pb, args);
2092 public static bool CheckStructCycles (TypeContainer tc, Hashtable seen)
2094 Hashtable hash = new Hashtable ();
2095 return CheckStructCycles (tc, seen, hash);
2098 public static bool CheckStructCycles (TypeContainer tc, Hashtable seen,
2101 if ((tc.Kind != Kind.Struct) || IsBuiltinType (tc))
2105 // `seen' contains all types we've already visited.
2107 if (seen.Contains (tc))
2109 seen.Add (tc, null);
2111 if (tc.Fields == null)
2114 foreach (FieldBase field in tc.Fields) {
2115 if (field.FieldBuilder == null || field.FieldBuilder.IsStatic)
2118 Type ftype = field.FieldBuilder.FieldType;
2119 TypeContainer ftc = LookupTypeContainer (ftype);
2123 if (hash.Contains (ftc)) {
2124 Report.Error (523, tc.Location,
2125 "Struct member `{0}.{1}' of type `{2}' " +
2126 "causes a cycle in the struct layout",
2127 tc.Name, field.Name, ftc.Name);
2132 // `hash' contains all types in the current path.
2134 hash.Add (tc, null);
2136 bool ok = CheckStructCycles (ftc, seen, hash);
2143 if (!seen.Contains (ftc))
2144 seen.Add (ftc, null);
2151 /// Given an array of interface types, expand and eliminate repeated ocurrences
2152 /// of an interface.
2156 /// This expands in context like: IA; IB : IA; IC : IA, IB; the interface "IC" to
2159 public static Type[] ExpandInterfaces (TypeExpr [] base_interfaces)
2161 ArrayList new_ifaces = new ArrayList ();
2163 foreach (TypeExpr iface in base_interfaces){
2164 Type itype = iface.Type;
2166 if (new_ifaces.Contains (itype))
2169 new_ifaces.Add (itype);
2171 Type [] implementing = GetInterfaces (itype);
2173 foreach (Type imp in implementing){
2174 if (!new_ifaces.Contains (imp))
2175 new_ifaces.Add (imp);
2178 Type [] ret = new Type [new_ifaces.Count];
2179 new_ifaces.CopyTo (ret, 0);
2183 public static Type[] ExpandInterfaces (Type [] base_interfaces)
2185 ArrayList new_ifaces = new ArrayList ();
2187 foreach (Type itype in base_interfaces){
2188 if (new_ifaces.Contains (itype))
2191 new_ifaces.Add (itype);
2193 Type [] implementing = GetInterfaces (itype);
2195 foreach (Type imp in implementing){
2196 if (!new_ifaces.Contains (imp))
2197 new_ifaces.Add (imp);
2200 Type [] ret = new Type [new_ifaces.Count];
2201 new_ifaces.CopyTo (ret, 0);
2205 static PtrHashtable iface_cache = new PtrHashtable ();
2208 /// This function returns the interfaces in the type `t'. Works with
2209 /// both types and TypeBuilders.
2211 public static Type [] GetInterfaces (Type t)
2213 Type [] cached = iface_cache [t] as Type [];
2218 // The reason for catching the Array case is that Reflection.Emit
2219 // will not return a TypeBuilder for Array types of TypeBuilder types,
2220 // but will still throw an exception if we try to call GetInterfaces
2223 // Since the array interfaces are always constant, we return those for
2228 t = TypeManager.array_type;
2230 if ((t is TypeBuilder) || IsGenericType (t)) {
2231 Type [] base_ifaces;
2233 if (t.BaseType == null)
2234 base_ifaces = Type.EmptyTypes;
2236 base_ifaces = GetInterfaces (t.BaseType);
2238 if (IsGenericType (t))
2239 #if MS_COMPATIBLE && GMCS_SOURCE
2240 type_ifaces = t.GetGenericTypeDefinition().GetInterfaces ();
2242 type_ifaces = t.GetInterfaces ();
2245 type_ifaces = (Type []) builder_to_ifaces [t];
2246 if (type_ifaces == null || type_ifaces.Length == 0)
2247 type_ifaces = Type.EmptyTypes;
2249 int base_count = base_ifaces.Length;
2250 Type [] result = new Type [base_count + type_ifaces.Length];
2251 base_ifaces.CopyTo (result, 0);
2252 type_ifaces.CopyTo (result, base_count);
2254 iface_cache [t] = result;
2257 } else if (t is GenericTypeParameterBuilder){
2258 Type[] type_ifaces = (Type []) builder_to_ifaces [t];
2259 if (type_ifaces == null || type_ifaces.Length == 0)
2260 type_ifaces = Type.EmptyTypes;
2262 iface_cache [t] = type_ifaces;
2266 Type[] ifaces = t.GetInterfaces ();
2267 iface_cache [t] = ifaces;
2273 // gets the interfaces that are declared explicitly on t
2275 public static Type [] GetExplicitInterfaces (TypeBuilder t)
2277 return (Type []) builder_to_ifaces [t];
2281 /// The following is used to check if a given type implements an interface.
2282 /// The cache helps us reduce the expense of hitting Type.GetInterfaces everytime.
2284 public static bool ImplementsInterface (Type t, Type iface)
2289 // FIXME OPTIMIZATION:
2290 // as soon as we hit a non-TypeBuiler in the interface
2291 // chain, we could return, as the `Type.GetInterfaces'
2292 // will return all the interfaces implement by the type
2296 interfaces = GetInterfaces (t);
2298 if (interfaces != null){
2299 foreach (Type i in interfaces){
2306 } while (t != null);
2311 static NumberFormatInfo nf_provider = CultureInfo.CurrentCulture.NumberFormat;
2313 // This is a custom version of Convert.ChangeType() which works
2314 // with the TypeBuilder defined types when compiling corlib.
2315 public static object ChangeType (object value, Type conversionType, out bool error)
2317 IConvertible convert_value = value as IConvertible;
2319 if (convert_value == null){
2325 // We must use Type.Equals() here since `conversionType' is
2326 // the TypeBuilder created version of a system type and not
2327 // the system type itself. You cannot use Type.GetTypeCode()
2328 // on such a type - it'd always return TypeCode.Object.
2332 if (conversionType.Equals (typeof (Boolean)))
2333 return (object)(convert_value.ToBoolean (nf_provider));
2334 else if (conversionType.Equals (typeof (Byte)))
2335 return (object)(convert_value.ToByte (nf_provider));
2336 else if (conversionType.Equals (typeof (Char)))
2337 return (object)(convert_value.ToChar (nf_provider));
2338 else if (conversionType.Equals (typeof (DateTime)))
2339 return (object)(convert_value.ToDateTime (nf_provider));
2340 else if (conversionType.Equals (TypeManager.decimal_type)) // typeof (Decimal)))
2341 return (object)(convert_value.ToDecimal (nf_provider));
2342 else if (conversionType.Equals (typeof (Double)))
2343 return (object)(convert_value.ToDouble (nf_provider));
2344 else if (conversionType.Equals (typeof (Int16)))
2345 return (object)(convert_value.ToInt16 (nf_provider));
2346 else if (conversionType.Equals (typeof (Int32)))
2347 return (object)(convert_value.ToInt32 (nf_provider));
2348 else if (conversionType.Equals (typeof (Int64)))
2349 return (object)(convert_value.ToInt64 (nf_provider));
2350 else if (conversionType.Equals (typeof (SByte)))
2351 return (object)(convert_value.ToSByte (nf_provider));
2352 else if (conversionType.Equals (typeof (Single)))
2353 return (object)(convert_value.ToSingle (nf_provider));
2354 else if (conversionType.Equals (typeof (String)))
2355 return (object)(convert_value.ToString (nf_provider));
2356 else if (conversionType.Equals (typeof (UInt16)))
2357 return (object)(convert_value.ToUInt16 (nf_provider));
2358 else if (conversionType.Equals (typeof (UInt32)))
2359 return (object)(convert_value.ToUInt32 (nf_provider));
2360 else if (conversionType.Equals (typeof (UInt64)))
2361 return (object)(convert_value.ToUInt64 (nf_provider));
2362 else if (conversionType.Equals (typeof (Object)))
2363 return (object)(value);
2373 // This is needed, because enumerations from assemblies
2374 // do not report their underlyingtype, but they report
2377 public static Type EnumToUnderlying (Type t)
2379 t = DropGenericTypeArguments (t);
2380 if (t == TypeManager.enum_type)
2383 t = t.UnderlyingSystemType;
2384 if (!TypeManager.IsEnumType (t))
2387 if (t is TypeBuilder) {
2388 // slow path needed to compile corlib
2389 if (t == TypeManager.bool_type ||
2390 t == TypeManager.byte_type ||
2391 t == TypeManager.sbyte_type ||
2392 t == TypeManager.char_type ||
2393 t == TypeManager.short_type ||
2394 t == TypeManager.ushort_type ||
2395 t == TypeManager.int32_type ||
2396 t == TypeManager.uint32_type ||
2397 t == TypeManager.int64_type ||
2398 t == TypeManager.uint64_type)
2401 TypeCode tc = Type.GetTypeCode (t);
2404 case TypeCode.Boolean:
2405 return TypeManager.bool_type;
2407 return TypeManager.byte_type;
2408 case TypeCode.SByte:
2409 return TypeManager.sbyte_type;
2411 return TypeManager.char_type;
2412 case TypeCode.Int16:
2413 return TypeManager.short_type;
2414 case TypeCode.UInt16:
2415 return TypeManager.ushort_type;
2416 case TypeCode.Int32:
2417 return TypeManager.int32_type;
2418 case TypeCode.UInt32:
2419 return TypeManager.uint32_type;
2420 case TypeCode.Int64:
2421 return TypeManager.int64_type;
2422 case TypeCode.UInt64:
2423 return TypeManager.uint64_type;
2425 throw new Exception ("Unhandled typecode in enum " + tc + " from " + t.AssemblyQualifiedName);
2429 // When compiling corlib and called with one of the core types, return
2430 // the corresponding typebuilder for that type.
2432 public static Type TypeToCoreType (Type t)
2434 if (RootContext.StdLib || (t is TypeBuilder))
2437 TypeCode tc = Type.GetTypeCode (t);
2440 case TypeCode.Boolean:
2441 return TypeManager.bool_type;
2443 return TypeManager.byte_type;
2444 case TypeCode.SByte:
2445 return TypeManager.sbyte_type;
2447 return TypeManager.char_type;
2448 case TypeCode.Int16:
2449 return TypeManager.short_type;
2450 case TypeCode.UInt16:
2451 return TypeManager.ushort_type;
2452 case TypeCode.Int32:
2453 return TypeManager.int32_type;
2454 case TypeCode.UInt32:
2455 return TypeManager.uint32_type;
2456 case TypeCode.Int64:
2457 return TypeManager.int64_type;
2458 case TypeCode.UInt64:
2459 return TypeManager.uint64_type;
2460 case TypeCode.Single:
2461 return TypeManager.float_type;
2462 case TypeCode.Double:
2463 return TypeManager.double_type;
2464 case TypeCode.String:
2465 return TypeManager.string_type;
2466 case TypeCode.Decimal:
2467 return TypeManager.decimal_type;
2469 if (t == typeof (void))
2470 return TypeManager.void_type;
2471 if (t == typeof (object))
2472 return TypeManager.object_type;
2473 if (t == typeof (System.Type))
2474 return TypeManager.type_type;
2475 if (t == typeof (System.IntPtr))
2476 return TypeManager.intptr_type;
2482 /// Utility function that can be used to probe whether a type
2483 /// is managed or not.
2485 public static bool VerifyUnManaged (Type t, Location loc)
2487 if (IsUnmanagedType (t))
2490 Report.Error (208, loc, "Cannot take the address of, get the size of, or declare a pointer to a managed type `{0}'",
2497 /// Returns the name of the indexer in a given type.
2500 /// The default is not always `Item'. The user can change this behaviour by
2501 /// using the IndexerNameAttribute in the container.
2502 /// For example, the String class indexer is named `Chars' not `Item'
2504 public static string IndexerPropertyName (Type t)
2506 t = DropGenericTypeArguments (t);
2507 if (t is TypeBuilder) {
2508 TypeContainer tc = t.IsInterface ? LookupInterface (t) : LookupTypeContainer (t);
2509 return tc == null ? TypeContainer.DefaultIndexerName : tc.IndexerName;
2512 System.Attribute attr = System.Attribute.GetCustomAttribute (
2513 t, TypeManager.default_member_type);
2515 DefaultMemberAttribute dma = (DefaultMemberAttribute) attr;
2516 return dma.MemberName;
2519 return TypeContainer.DefaultIndexerName;
2522 static MethodInfo declare_local_method = null;
2524 public static LocalBuilder DeclareLocalPinned (ILGenerator ig, Type t)
2526 if (declare_local_method == null){
2527 declare_local_method = typeof (ILGenerator).GetMethod (
2529 BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
2531 new Type [] { typeof (Type), typeof (bool)},
2533 if (declare_local_method == null){
2534 Report.RuntimeMissingSupport (Location.Null, "pinned local variables");
2535 return ig.DeclareLocal (t);
2538 return (LocalBuilder) declare_local_method.Invoke (ig, new object [] { t, true });
2541 private static bool IsSignatureEqual (Type a, Type b)
2544 /// Consider the following example (bug #77674):
2546 /// public abstract class A
2548 /// public abstract T Foo<T> ();
2551 /// public abstract class B : A
2553 /// public override U Foo<T> ()
2554 /// { return default (U); }
2557 /// Here, `T' and `U' are method type parameters from different methods
2558 /// (A.Foo and B.Foo), so both `==' and Equals() will fail.
2560 /// However, since we're determining whether B.Foo() overrides A.Foo(),
2561 /// we need to do a signature based comparision and consider them equal.
2567 if (a.IsGenericParameter && b.IsGenericParameter &&
2568 (a.DeclaringMethod != null) && (b.DeclaringMethod != null)) {
2569 return a.GenericParameterPosition == b.GenericParameterPosition;
2573 if (a.IsArray && b.IsArray) {
2574 if (a.GetArrayRank () != b.GetArrayRank ())
2577 return IsSignatureEqual (a.GetElementType (), b.GetElementType ());
2580 if (a.IsByRef && b.IsByRef)
2581 return IsSignatureEqual (a.GetElementType (), b.GetElementType ());
2584 if (a.IsGenericType && b.IsGenericType) {
2585 if (a.GetGenericTypeDefinition () != b.GetGenericTypeDefinition ())
2588 Type[] aargs = a.GetGenericArguments ();
2589 Type[] bargs = b.GetGenericArguments ();
2591 if (aargs.Length != bargs.Length)
2594 for (int i = 0; i < aargs.Length; i++) {
2595 if (!IsSignatureEqual (aargs [i], bargs [i]))
2607 // Returns whether the array of memberinfos contains the given method
2609 public static bool ArrayContainsMethod (MemberInfo [] array, MethodBase new_method)
2611 Type [] new_args = TypeManager.GetParameterData (new_method).Types;
2613 foreach (MethodBase method in array) {
2614 if (method.Name != new_method.Name)
2617 if (method is MethodInfo && new_method is MethodInfo)
2618 if (!IsSignatureEqual (((MethodInfo) method).ReturnType,
2619 ((MethodInfo) new_method).ReturnType))
2623 Type [] old_args = TypeManager.GetParameterData (method).Types;
2624 int old_count = old_args.Length;
2627 if (new_args.Length != old_count)
2630 for (i = 0; i < old_count; i++){
2631 if (!IsSignatureEqual (old_args [i], new_args [i]))
2644 // We copy methods from `new_members' into `target_list' if the signature
2645 // for the method from in the new list does not exist in the target_list
2647 // The name is assumed to be the same.
2649 public static ArrayList CopyNewMethods (ArrayList target_list, IList new_members)
2651 if (target_list == null){
2652 target_list = new ArrayList ();
2654 foreach (MemberInfo mi in new_members){
2655 if (mi is MethodBase)
2656 target_list.Add (mi);
2661 MemberInfo [] target_array = new MemberInfo [target_list.Count];
2662 target_list.CopyTo (target_array, 0);
2664 foreach (MemberInfo mi in new_members){
2665 MethodBase new_method = (MethodBase) mi;
2667 if (!ArrayContainsMethod (target_array, new_method))
2668 target_list.Add (new_method);
2675 // Tracks the generic parameters.
2677 static PtrHashtable builder_to_type_param;
2679 public static void AddTypeParameter (Type t, TypeParameter tparam)
2681 if (!builder_to_type_param.Contains (t))
2682 builder_to_type_param.Add (t, tparam);
2685 public static TypeParameter LookupTypeParameter (Type t)
2687 return (TypeParameter) builder_to_type_param [t];
2690 // This method always return false for non-generic compiler,
2691 // while Type.IsGenericParameter is returned if it is supported.
2692 public static bool IsGenericParameter (Type type)
2695 return type.IsGenericParameter;
2701 public static int GenericParameterPosition (Type type)
2704 return type.GenericParameterPosition;
2706 throw new InternalErrorException ("should not be called");
2710 public static bool IsGenericType (Type type)
2713 return type.IsGenericType;
2719 public static bool IsGenericTypeDefinition (Type type)
2722 return type.IsGenericTypeDefinition;
2728 public static bool ContainsGenericParameters (Type type)
2731 return type.ContainsGenericParameters;
2737 public static FieldInfo GetGenericFieldDefinition (FieldInfo fi)
2740 if (fi.DeclaringType.IsGenericTypeDefinition ||
2741 !fi.DeclaringType.IsGenericType)
2744 Type t = fi.DeclaringType.GetGenericTypeDefinition ();
2745 BindingFlags bf = BindingFlags.Public | BindingFlags.NonPublic |
2746 BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
2748 // TODO: use CodeGen.Module.Builder.ResolveField (fi.MetadataToken);
2749 foreach (FieldInfo f in t.GetFields (bf))
2750 if (f.MetadataToken == fi.MetadataToken)
2757 public static bool IsEqual (Type a, Type b)
2760 // MS BCL returns true even if enum types are different
2761 if (a.BaseType == TypeManager.enum_type || b.BaseType == TypeManager.enum_type)
2762 return a.FullName == b.FullName;
2768 if (a.IsGenericParameter && b.IsGenericParameter) {
2769 if (a.DeclaringMethod != b.DeclaringMethod &&
2770 (a.DeclaringMethod == null || b.DeclaringMethod == null))
2772 return a.GenericParameterPosition == b.GenericParameterPosition;
2775 if (a.IsArray && b.IsArray) {
2776 if (a.GetArrayRank () != b.GetArrayRank ())
2778 return IsEqual (a.GetElementType (), b.GetElementType ());
2781 if (a.IsByRef && b.IsByRef)
2782 return IsEqual (a.GetElementType (), b.GetElementType ());
2784 if (a.IsGenericType && b.IsGenericType) {
2785 Type adef = a.GetGenericTypeDefinition ();
2786 Type bdef = b.GetGenericTypeDefinition ();
2791 if (adef.IsEnum && bdef.IsEnum)
2794 Type[] aargs = a.GetGenericArguments ();
2795 Type[] bargs = b.GetGenericArguments ();
2797 if (aargs.Length != bargs.Length)
2800 for (int i = 0; i < aargs.Length; i++) {
2801 if (!IsEqual (aargs [i], bargs [i]))
2812 public static Type DropGenericTypeArguments (Type t)
2815 if (!t.IsGenericType)
2817 // Micro-optimization: a generic typebuilder is always a generic type definition
2818 if (t is TypeBuilder)
2820 return t.GetGenericTypeDefinition ();
2826 public static MethodBase DropGenericMethodArguments (MethodBase m)
2829 if (m.IsGenericMethodDefinition)
2831 if (m.IsGenericMethod)
2832 return ((MethodInfo) m).GetGenericMethodDefinition ();
2833 if (!m.DeclaringType.IsGenericType)
2836 Type t = m.DeclaringType.GetGenericTypeDefinition ();
2837 BindingFlags bf = BindingFlags.Public | BindingFlags.NonPublic |
2838 BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
2841 // TODO: use CodeGen.Module.Builder.ResolveMethod ()
2845 if (m is ConstructorInfo) {
2846 foreach (ConstructorInfo c in t.GetConstructors (bf))
2847 if (c.MetadataToken == m.MetadataToken)
2850 foreach (MethodBase mb in t.GetMethods (bf))
2851 if (mb.MetadataToken == m.MetadataToken)
2859 public static Type[] GetGenericArguments (MethodInfo mi)
2862 return mi.GetGenericArguments ();
2864 return Type.EmptyTypes;
2868 public static Type[] GetTypeArguments (Type t)
2871 DeclSpace tc = LookupDeclSpace (t);
2874 return Type.EmptyTypes;
2876 TypeParameter[] tparam = tc.TypeParameters;
2877 Type[] ret = new Type [tparam.Length];
2878 for (int i = 0; i < tparam.Length; i++) {
2879 ret [i] = tparam [i].Type;
2880 if (ret [i] == null)
2881 throw new InternalErrorException ();
2886 return t.GetGenericArguments ();
2888 throw new InternalErrorException ();
2892 public static bool HasGenericArguments (Type t)
2894 return GetNumberOfTypeArguments (t) > 0;
2897 public static int GetNumberOfTypeArguments (Type t)
2900 if (t.IsGenericParameter)
2902 DeclSpace tc = LookupDeclSpace (t);
2904 return tc.IsGeneric ? tc.CountTypeParameters : 0;
2906 return t.IsGenericType ? t.GetGenericArguments ().Length : 0;
2913 /// Check whether `type' and `parent' are both instantiations of the same
2914 /// generic type. Note that we do not check the type parameters here.
2916 public static bool IsInstantiationOfSameGenericType (Type type, Type parent)
2918 int tcount = GetNumberOfTypeArguments (type);
2919 int pcount = GetNumberOfTypeArguments (parent);
2921 if (tcount != pcount)
2924 type = DropGenericTypeArguments (type);
2925 parent = DropGenericTypeArguments (parent);
2927 return type.Equals (parent);
2931 /// Whether `mb' is a generic method definition.
2933 public static bool IsGenericMethodDefinition (MethodBase mb)
2936 if (mb.DeclaringType is TypeBuilder) {
2937 IMethodData method = (IMethodData) builder_to_method [mb];
2941 return method.GenericMethod != null;
2944 return mb.IsGenericMethodDefinition;
2951 /// Whether `mb' is a generic method.
2953 public static bool IsGenericMethod (MethodBase mb)
2956 return mb.IsGenericMethod;
2962 public static bool IsNullableType (Type t)
2965 return generic_nullable_type == DropGenericTypeArguments (t);
2971 public static bool IsNullableTypeOf (Type t, Type nullable)
2974 if (!IsNullableType (t))
2977 return GetTypeArguments (t) [0] == nullable;
2983 public static bool IsNullableValueType (Type t)
2986 if (!IsNullableType (t))
2989 return GetTypeArguments (t) [0].IsValueType;
2996 #region MemberLookup implementation
2999 // Whether we allow private members in the result (since FindMembers
3000 // uses NonPublic for both protected and private), we need to distinguish.
3003 internal class Closure {
3004 internal bool private_ok;
3006 // Who is invoking us and which type is being queried currently.
3007 internal Type invocation_type;
3008 internal Type qualifier_type;
3010 // The assembly that defines the type is that is calling us
3011 internal Assembly invocation_assembly;
3012 internal IList almost_match;
3014 private bool CheckValidFamilyAccess (bool is_static, MemberInfo m)
3016 if (invocation_type == null)
3019 if (is_static && qualifier_type == null)
3020 // It resolved from a simple name, so it should be visible.
3023 if (IsNestedChildOf (invocation_type, m.DeclaringType))
3026 for (Type t = invocation_type; t != null; t = t.DeclaringType) {
3027 if (!IsFamilyAccessible (t, m.DeclaringType))
3030 // Although a derived class can access protected members of its base class
3031 // it cannot do so through an instance of the base class (CS1540).
3032 // => Ancestry should be: declaring_type ->* invocation_type ->* qualified_type
3033 if (is_static || qualifier_type == null ||
3034 IsInstantiationOfSameGenericType (t, qualifier_type) ||
3035 IsFamilyAccessible (qualifier_type, t))
3039 if (almost_match != null)
3040 almost_match.Add (m);
3046 // This filter filters by name + whether it is ok to include private
3047 // members in the search
3049 internal bool Filter (MemberInfo m, object filter_criteria)
3052 // Hack: we know that the filter criteria will always be in the
3053 // `closure' // fields.
3056 if ((filter_criteria != null) && (m.Name != (string) filter_criteria))
3059 if (((qualifier_type == null) || (qualifier_type == invocation_type)) &&
3060 (invocation_type != null) &&
3061 IsPrivateAccessible (m.DeclaringType, invocation_type))
3065 // Ugly: we need to find out the type of `m', and depending
3066 // on this, tell whether we accept or not
3068 if (m is MethodBase){
3069 MethodBase mb = (MethodBase) m;
3070 MethodAttributes ma = mb.Attributes & MethodAttributes.MemberAccessMask;
3072 if (ma == MethodAttributes.Public)
3075 if (ma == MethodAttributes.PrivateScope)
3078 if (ma == MethodAttributes.Private)
3079 return private_ok ||
3080 IsPrivateAccessible (invocation_type, m.DeclaringType) ||
3081 IsNestedChildOf (invocation_type, m.DeclaringType);
3083 if (invocation_assembly == mb.DeclaringType.Assembly ||
3084 TypeManager.IsFriendAssembly (mb.DeclaringType.Assembly)) {
3085 if (ma == MethodAttributes.Assembly || ma == MethodAttributes.FamORAssem)
3088 if (ma == MethodAttributes.Assembly || ma == MethodAttributes.FamANDAssem)
3092 // Family, FamORAssem or FamANDAssem
3093 return CheckValidFamilyAccess (mb.IsStatic, m);
3096 if (m is FieldInfo){
3097 FieldInfo fi = (FieldInfo) m;
3098 FieldAttributes fa = fi.Attributes & FieldAttributes.FieldAccessMask;
3100 if (fa == FieldAttributes.Public)
3103 if (fa == FieldAttributes.PrivateScope)
3106 if (fa == FieldAttributes.Private)
3107 return private_ok ||
3108 IsPrivateAccessible (invocation_type, m.DeclaringType) ||
3109 IsNestedChildOf (invocation_type, m.DeclaringType);
3111 if ((invocation_assembly == fi.DeclaringType.Assembly) ||
3112 (invocation_assembly == null) ||
3113 TypeManager.IsFriendAssembly (fi.DeclaringType.Assembly)) {
3114 if ((fa == FieldAttributes.Assembly) ||
3115 (fa == FieldAttributes.FamORAssem))
3118 if ((fa == FieldAttributes.Assembly) ||
3119 (fa == FieldAttributes.FamANDAssem))
3123 // Family, FamORAssem or FamANDAssem
3124 return CheckValidFamilyAccess (fi.IsStatic, m);
3128 // EventInfos and PropertyInfos, return true because they lack
3129 // permission information, so we need to check later on the methods.
3135 static Closure closure = new Closure ();
3136 static MemberFilter FilterWithClosure_delegate = new MemberFilter (closure.Filter);
3139 // Looks up a member called `name' in the `queried_type'. This lookup
3140 // is done by code that is contained in the definition for `invocation_type'
3141 // through a qualifier of type `qualifier_type' (or null if there is no qualifier).
3143 // `invocation_type' is used to check whether we're allowed to access the requested
3144 // member wrt its protection level.
3146 // When called from MemberAccess, `qualifier_type' is the type which is used to access
3147 // the requested member (`class B { A a = new A (); a.foo = 5; }'; here invocation_type
3148 // is B and qualifier_type is A). This is used to do the CS1540 check.
3150 // When resolving a SimpleName, `qualifier_type' is null.
3152 // The `qualifier_type' is used for the CS1540 check; it's normally either null or
3153 // the same than `queried_type' - except when we're being called from BaseAccess;
3154 // in this case, `invocation_type' is the current type and `queried_type' the base
3155 // type, so this'd normally trigger a CS1540.
3157 // The binding flags are `bf' and the kind of members being looked up are `mt'
3159 // The return value always includes private members which code in `invocation_type'
3160 // is allowed to access (using the specified `qualifier_type' if given); only use
3161 // BindingFlags.NonPublic to bypass the permission check.
3163 // The 'almost_match' argument is used for reporting error CS1540.
3165 // Returns an array of a single element for everything but Methods/Constructors
3166 // that might return multiple matches.
3168 public static MemberInfo [] MemberLookup (Type invocation_type, Type qualifier_type,
3169 Type queried_type, MemberTypes mt,
3170 BindingFlags original_bf, string name, IList almost_match)
3172 Timer.StartTimer (TimerType.MemberLookup);
3174 MemberInfo[] retval = RealMemberLookup (invocation_type, qualifier_type,
3175 queried_type, mt, original_bf, name, almost_match);
3177 Timer.StopTimer (TimerType.MemberLookup);
3182 static MemberInfo [] RealMemberLookup (Type invocation_type, Type qualifier_type,
3183 Type queried_type, MemberTypes mt,
3184 BindingFlags original_bf, string name, IList almost_match)
3186 BindingFlags bf = original_bf;
3188 ArrayList method_list = null;
3189 Type current_type = queried_type;
3190 bool searching = (original_bf & BindingFlags.DeclaredOnly) == 0;
3191 bool skip_iface_check = true, used_cache = false;
3192 bool always_ok_flag = invocation_type != null && IsNestedChildOf (invocation_type, queried_type);
3194 closure.invocation_type = invocation_type;
3195 closure.invocation_assembly = invocation_type != null ? invocation_type.Assembly : null;
3196 closure.qualifier_type = qualifier_type;
3197 closure.almost_match = almost_match;
3199 // This is from the first time we find a method
3200 // in most cases, we do not actually find a method in the base class
3201 // so we can just ignore it, and save the arraylist allocation
3202 MemberInfo [] first_members_list = null;
3203 bool use_first_members_list = false;
3209 // `NonPublic' is lame, because it includes both protected and
3210 // private methods, so we need to control this behavior by
3211 // explicitly tracking if a private method is ok or not.
3213 // The possible cases are:
3214 // public, private and protected (internal does not come into the
3217 if ((invocation_type != null) &&
3218 ((invocation_type == current_type) ||
3219 IsNestedChildOf (invocation_type, current_type)) ||
3221 bf = original_bf | BindingFlags.NonPublic;
3225 closure.private_ok = (original_bf & BindingFlags.NonPublic) != 0;
3227 Timer.StopTimer (TimerType.MemberLookup);
3229 list = MemberLookup_FindMembers (current_type, mt, bf, name, out used_cache);
3231 Timer.StartTimer (TimerType.MemberLookup);
3234 // When queried for an interface type, the cache will automatically check all
3235 // inherited members, so we don't need to do this here. However, this only
3236 // works if we already used the cache in the first iteration of this loop.
3238 // If we used the cache in any further iteration, we can still terminate the
3239 // loop since the cache always looks in all base classes.
3245 skip_iface_check = false;
3247 if (current_type == TypeManager.object_type)
3250 current_type = current_type.BaseType;
3253 // This happens with interfaces, they have a null
3254 // basetype. Look members up in the Object class.
3256 if (current_type == null) {
3257 current_type = TypeManager.object_type;
3262 if (list.Length == 0)
3266 // Events and types are returned by both `static' and `instance'
3267 // searches, which means that our above FindMembers will
3268 // return two copies of the same.
3270 if (list.Length == 1 && !(list [0] is MethodBase)){
3275 // Multiple properties: we query those just to find out the indexer
3278 if (list [0] is PropertyInfo)
3282 // We found an event: the cache lookup returns both the event and
3283 // its private field.
3285 if (list [0] is EventInfo) {
3286 if ((list.Length == 2) && (list [1] is FieldInfo))
3287 return new MemberInfo [] { list [0] };
3293 // We found methods, turn the search into "method scan"
3297 if (first_members_list != null) {
3298 if (use_first_members_list) {
3299 method_list = CopyNewMethods (method_list, first_members_list);
3300 use_first_members_list = false;
3303 method_list = CopyNewMethods (method_list, list);
3305 first_members_list = list;
3306 use_first_members_list = true;
3307 mt &= (MemberTypes.Method | MemberTypes.Constructor);
3309 } while (searching);
3311 if (use_first_members_list)
3312 return first_members_list;
3314 if (method_list != null && method_list.Count > 0) {
3315 return (MemberInfo []) method_list.ToArray (typeof (MemberInfo));
3318 // This happens if we already used the cache in the first iteration, in this case
3319 // the cache already looked in all interfaces.
3321 if (skip_iface_check)
3325 // Interfaces do not list members they inherit, so we have to
3328 if (!queried_type.IsInterface)
3331 if (queried_type.IsArray)
3332 queried_type = TypeManager.array_type;
3334 Type [] ifaces = GetInterfaces (queried_type);
3338 foreach (Type itype in ifaces){
3341 x = MemberLookup (null, null, itype, mt, bf, name, null);
3349 const BindingFlags AllMembers = BindingFlags.Public | BindingFlags.NonPublic |
3350 BindingFlags.Static | BindingFlags.Instance |
3351 BindingFlags.DeclaredOnly;
3353 // Currently is designed to work with external types only
3354 public static PropertyInfo GetPropertyFromAccessor (MethodBase mb)
3356 if (!mb.IsSpecialName)
3359 string name = mb.Name;
3360 if (name.Length < 5)
3363 if (name [3] != '_')
3366 if (name.StartsWith ("get") || name.StartsWith ("set")) {
3367 MemberInfo[] pi = mb.DeclaringType.FindMembers (MemberTypes.Property, AllMembers,
3368 Type.FilterName, name.Substring (4));
3373 // This can happen when property is indexer (it can have same name but different parameters)
3374 foreach (PropertyInfo p in pi) {
3375 foreach (MethodInfo p_mi in p.GetAccessors (true)) {
3376 if (p_mi == mb || TypeManager.GetParameterData (p_mi).Equals (TypeManager.GetParameterData (mb)))
3385 // Currently is designed to work with external types only
3386 public static MemberInfo GetEventFromAccessor (MethodBase mb)
3388 if (!mb.IsSpecialName)
3391 string name = mb.Name;
3392 if (name.Length < 5)
3395 if (name.StartsWith ("add_"))
3396 return mb.DeclaringType.GetEvent (name.Substring (4), AllMembers);
3398 if (name.StartsWith ("remove_"))
3399 return mb.DeclaringType.GetEvent (name.Substring (7), AllMembers);
3404 // Tests whether external method is really special
3405 public static bool IsSpecialMethod (MethodBase mb)
3407 if (!mb.IsSpecialName)
3410 IMethodData md = TypeManager.GetMethod (mb);
3412 return (md is AbstractPropertyEventMethod || md is Operator);
3414 PropertyInfo pi = GetPropertyFromAccessor (mb);
3416 return IsValidProperty (pi);
3418 if (GetEventFromAccessor (mb) != null)
3421 string name = mb.Name;
3422 if (name.StartsWith ("op_")){
3423 foreach (string oname in Unary.oper_names) {
3428 foreach (string oname in Binary.oper_names) {
3436 // Tests whether imported property is valid C# property.
3437 // TODO: It seems to me that we should do a lot of sanity tests before
3438 // we accept property as C# property
3439 static bool IsValidProperty (PropertyInfo pi)
3441 MethodInfo get_method = pi.GetGetMethod (true);
3442 MethodInfo set_method = pi.GetSetMethod (true);
3443 if (get_method != null && set_method != null) {
3444 int g_count = get_method.GetParameters ().Length;
3445 int s_count = set_method.GetParameters ().Length;
3446 if (g_count + 1 != s_count)
3457 /// There is exactly one instance of this class per type.
3459 public sealed class TypeHandle : IMemberContainer {
3460 public readonly IMemberContainer BaseType;
3462 readonly int id = ++next_id;
3463 static int next_id = 0;
3465 static TypeHandle ()
3471 /// Lookup a TypeHandle instance for the given type. If the type doesn't have
3472 /// a TypeHandle yet, a new instance of it is created. This static method
3473 /// ensures that we'll only have one TypeHandle instance per type.
3475 private static TypeHandle GetTypeHandle (Type t)
3477 TypeHandle handle = (TypeHandle) type_hash [t];
3481 handle = new TypeHandle (t);
3482 type_hash.Add (t, handle);
3486 public static MemberCache GetMemberCache (Type t)
3488 return GetTypeHandle (t).MemberCache;
3491 public static void CleanUp ()
3496 public static void Reset ()
3498 type_hash = new PtrHashtable ();
3502 /// Returns the TypeHandle for TypeManager.object_type.
3504 public static IMemberContainer ObjectType {
3506 if (object_type != null)
3509 object_type = GetTypeHandle (TypeManager.object_type);
3516 /// Returns the TypeHandle for TypeManager.array_type.
3518 public static IMemberContainer ArrayType {
3520 if (array_type != null)
3523 array_type = GetTypeHandle (TypeManager.array_type);
3529 private static PtrHashtable type_hash;
3531 private static TypeHandle object_type = null;
3532 private static TypeHandle array_type = null;
3535 private string full_name;
3536 private bool is_interface;
3537 private MemberCache member_cache;
3538 private MemberCache base_cache;
3540 private TypeHandle (Type type)
3543 #if MS_COMPATIBLE && GMCS_SOURCE
3544 if (type.IsGenericType)
3545 this.type = this.type.GetGenericTypeDefinition ();
3547 full_name = type.FullName != null ? type.FullName : type.Name;
3548 if (type.BaseType != null) {
3549 base_cache = TypeManager.LookupMemberCache (type.BaseType);
3550 BaseType = base_cache.Container;
3551 } else if (type.IsInterface)
3552 base_cache = TypeManager.LookupBaseInterfacesCache (type);
3553 this.is_interface = type.IsInterface || TypeManager.IsGenericParameter (type);
3554 this.member_cache = new MemberCache (this);
3557 // IMemberContainer methods
3559 public string Name {
3571 public MemberCache BaseCache {
3577 public bool IsInterface {
3579 return is_interface;
3583 public MemberList GetMembers (MemberTypes mt, BindingFlags bf)
3585 MemberInfo [] members;
3588 if (type is GenericTypeParameterBuilder)
3589 return MemberList.Empty;
3592 if (mt == MemberTypes.Event)
3593 members = type.GetEvents (bf | BindingFlags.DeclaredOnly);
3595 members = type.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
3597 Array.Reverse (members);
3599 return new MemberList (members);
3602 // IMemberFinder methods
3604 public MemberList FindMembers (MemberTypes mt, BindingFlags bf, string name,
3605 MemberFilter filter, object criteria)
3607 return new MemberList (member_cache.FindMembers (mt, bf, name, filter, criteria));
3610 public MemberCache MemberCache {
3612 return member_cache;
3616 public override string ToString ()
3618 if (BaseType != null)
3619 return "TypeHandle (" + id + "," + Name + " : " + BaseType + ")";
3621 return "TypeHandle (" + id + "," + Name + ")";