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 (CSharpName (args [i]));
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 public 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 static void InitSystemCore ()
1361 if (RootContext.Version != LanguageVersion.LINQ)
1364 if (extension_attribute_type != null)
1365 extension_attribute_attr = new CustomAttributeBuilder (
1366 GetConstructor (extension_attribute_type, Type.EmptyTypes), new object[0]);
1370 const BindingFlags instance_and_static = BindingFlags.Static | BindingFlags.Instance;
1373 /// This is the "old", non-cache based FindMembers() function. We cannot use
1374 /// the cache here because there is no member name argument.
1376 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1377 MemberFilter filter, object criteria)
1379 #if MS_COMPATIBLE && GMCS_SOURCE
1380 if (t.IsGenericType)
1381 t = t.GetGenericTypeDefinition ();
1384 DeclSpace decl = (DeclSpace) builder_to_declspace [t];
1387 // `builder_to_declspace' contains all dynamic types.
1391 Timer.StartTimer (TimerType.FindMembers);
1392 list = decl.FindMembers (mt, bf, filter, criteria);
1393 Timer.StopTimer (TimerType.FindMembers);
1398 // We have to take care of arrays specially, because GetType on
1399 // a TypeBuilder array will return a Type, not a TypeBuilder,
1400 // and we can not call FindMembers on this type.
1403 #if MS_COMPATIBLE && GMCS_SOURCE
1406 t.IsSubclassOf (TypeManager.array_type))
1407 return new MemberList (TypeManager.array_type.FindMembers (mt, bf, filter, criteria));
1410 if (t is GenericTypeParameterBuilder) {
1411 TypeParameter tparam = (TypeParameter) builder_to_type_param [t];
1413 Timer.StartTimer (TimerType.FindMembers);
1414 MemberList list = tparam.FindMembers (
1415 mt, bf | BindingFlags.DeclaredOnly, filter, criteria);
1416 Timer.StopTimer (TimerType.FindMembers);
1422 // Since FindMembers will not lookup both static and instance
1423 // members, we emulate this behaviour here.
1425 if ((bf & instance_and_static) == instance_and_static){
1426 MemberInfo [] i_members = t.FindMembers (
1427 mt, bf & ~BindingFlags.Static, filter, criteria);
1429 int i_len = i_members.Length;
1431 MemberInfo one = i_members [0];
1434 // If any of these are present, we are done!
1436 if ((one is Type) || (one is EventInfo) || (one is FieldInfo))
1437 return new MemberList (i_members);
1440 MemberInfo [] s_members = t.FindMembers (
1441 mt, bf & ~BindingFlags.Instance, filter, criteria);
1443 int s_len = s_members.Length;
1444 if (i_len > 0 || s_len > 0)
1445 return new MemberList (i_members, s_members);
1448 return new MemberList (i_members);
1450 return new MemberList (s_members);
1454 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
1459 /// This method is only called from within MemberLookup. It tries to use the member
1460 /// cache if possible and falls back to the normal FindMembers if not. The `used_cache'
1461 /// flag tells the caller whether we used the cache or not. If we used the cache, then
1462 /// our return value will already contain all inherited members and the caller don't need
1463 /// to check base classes and interfaces anymore.
1465 private static MemberInfo [] MemberLookup_FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1466 string name, out bool used_cache)
1471 // If this is a dynamic type, it's always in the `builder_to_declspace' hash table
1472 // and we can ask the DeclSpace for the MemberCache.
1474 if (t is TypeBuilder) {
1475 #if GMCS_SOURCE && MS_COMPATIBLE
1476 if (t.IsGenericType && !t.IsGenericTypeDefinition)
1477 t = t.GetGenericTypeDefinition ();
1479 DeclSpace decl = (DeclSpace) builder_to_declspace [t];
1480 cache = decl.MemberCache;
1483 // If this DeclSpace has a MemberCache, use it.
1486 if (cache != null) {
1488 return cache.FindMembers (
1489 mt, bf, name, FilterWithClosure_delegate, null);
1492 // If there is no MemberCache, we need to use the "normal" FindMembers.
1493 // Note, this is a VERY uncommon route!
1496 Timer.StartTimer (TimerType.FindMembers);
1497 list = decl.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
1498 FilterWithClosure_delegate, name);
1499 Timer.StopTimer (TimerType.FindMembers);
1501 return (MemberInfo []) list;
1505 // We have to take care of arrays specially, because GetType on
1506 // a TypeBuilder array will return a Type, not a TypeBuilder,
1507 // and we can not call FindMembers on this type.
1509 if (t.IsArray) { // == TypeManager.array_type || t.IsSubclassOf (TypeManager.array_type)) {
1511 return TypeHandle.ArrayType.MemberCache.FindMembers (
1512 mt, bf, name, FilterWithClosure_delegate, null);
1516 if (t is GenericTypeParameterBuilder) {
1517 TypeParameter tparam = (TypeParameter) builder_to_type_param [t];
1520 if (tparam.MemberCache == null)
1521 return new MemberInfo [0];
1523 return tparam.MemberCache.FindMembers (
1524 mt, bf, name, FilterWithClosure_delegate, null);
1528 if (IsGenericType (t) && (mt == MemberTypes.NestedType)) {
1530 // This happens if we're resolving a class'es base class and interfaces
1531 // in TypeContainer.DefineType(). At this time, the types aren't
1532 // populated yet, so we can't use the cache.
1534 MemberInfo[] info = t.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
1535 FilterWithClosure_delegate, name);
1541 // This call will always succeed. There is exactly one TypeHandle instance per
1542 // type, TypeHandle.GetMemberCache() will, if necessary, create a new one, and return
1543 // the corresponding MemberCache.
1545 cache = TypeHandle.GetMemberCache (t);
1548 return cache.FindMembers (mt, bf, name, FilterWithClosure_delegate, null);
1551 public static bool IsBuiltinType (Type t)
1553 t = TypeToCoreType (t);
1554 if (t == object_type || t == string_type || t == int32_type || t == uint32_type ||
1555 t == int64_type || t == uint64_type || t == float_type || t == double_type ||
1556 t == char_type || t == short_type || t == decimal_type || t == bool_type ||
1557 t == sbyte_type || t == byte_type || t == ushort_type || t == void_type)
1563 public static bool IsBuiltinType (TypeContainer tc)
1565 return IsBuiltinType (tc.TypeBuilder);
1569 // This is like IsBuiltinType, but lacks decimal_type, we should also clean up
1570 // the pieces in the code where we use IsBuiltinType and special case decimal_type.
1572 public static bool IsPrimitiveType (Type t)
1574 return (t == int32_type || t == uint32_type ||
1575 t == int64_type || t == uint64_type || t == float_type || t == double_type ||
1576 t == char_type || t == short_type || t == bool_type ||
1577 t == sbyte_type || t == byte_type || t == ushort_type);
1580 public static bool IsDelegateType (Type t)
1582 t = DropGenericTypeArguments (t);
1583 if (t.IsSubclassOf (TypeManager.delegate_type))
1589 public static bool IsEnumType (Type t)
1591 t = DropGenericTypeArguments (t);
1592 if (builder_to_declspace [t] is Enum)
1595 #if MS_COMPATIBLE && GMCS_SOURCE
1596 if (t.IsGenericParameter || t.IsGenericType)
1602 public static bool IsBuiltinOrEnum (Type t)
1604 if (IsBuiltinType (t))
1613 public static bool IsNullType (Type t)
1615 return t == null_type;
1618 public static bool IsAttributeType (Type t)
1620 return t == attribute_type && t.BaseType != null || IsSubclassOf (t, attribute_type);
1623 static Stack unmanaged_enclosing_types = new Stack (4);
1626 // Whether a type is unmanaged. This is used by the unsafe code (25.2)
1628 public static bool IsUnmanagedType (Type t)
1630 // Avoid infloops in the case of: unsafe struct Foo { Foo *x; }
1631 if (unmanaged_enclosing_types.Contains (t))
1634 // builtins that are not unmanaged types
1635 if (t == TypeManager.object_type || t == TypeManager.string_type)
1638 if (IsGenericType (t) || IsGenericParameter (t))
1641 if (IsBuiltinOrEnum (t))
1644 // Someone did the work of checking if the ElementType of t is unmanaged. Let's not repeat it.
1648 // Arrays are disallowed, even if we mark them with [MarshalAs(UnmanagedType.ByValArray, ...)]
1652 if (!IsValueType (t))
1656 for (Type p = t.DeclaringType; p != null; p = p.DeclaringType) {
1657 if (p.IsGenericTypeDefinition)
1662 unmanaged_enclosing_types.Push (t);
1666 if (t is TypeBuilder) {
1667 TypeContainer tc = LookupTypeContainer (t);
1668 if (tc.Fields != null){
1669 foreach (FieldBase f in tc.Fields){
1670 // Avoid using f.FieldBuilder: f.Define () may not yet have been invoked.
1671 if ((f.ModFlags & Modifiers.STATIC) != 0)
1673 if (f.MemberType == null)
1675 if (!IsUnmanagedType (f.MemberType)){
1676 Report.SymbolRelatedToPreviousError (f.Location, CSharpName (t) + "." + f.Name);
1682 FieldInfo [] fields = t.GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
1684 foreach (FieldInfo f in fields){
1685 if (!IsUnmanagedType (f.FieldType)){
1686 Report.SymbolRelatedToPreviousError (f);
1692 unmanaged_enclosing_types.Pop ();
1697 public static bool IsValueType (Type t)
1699 return t.IsValueType || IsGenericParameter (t);
1702 public static bool IsInterfaceType (Type t)
1704 TypeContainer tc = (TypeContainer) builder_to_declspace [t];
1708 return tc.Kind == Kind.Interface;
1711 public static bool IsSubclassOf (Type type, Type base_type)
1713 TypeParameter tparam = LookupTypeParameter (type);
1714 TypeParameter pparam = LookupTypeParameter (base_type);
1716 if ((tparam != null) && (pparam != null)) {
1717 if (tparam == pparam)
1720 return tparam.IsSubclassOf (base_type);
1723 #if MS_COMPATIBLE && GMCS_SOURCE
1724 if (type.IsGenericType)
1725 type = type.GetGenericTypeDefinition ();
1728 if (type.IsSubclassOf (base_type))
1732 if (IsEqual (type, base_type))
1735 type = type.BaseType;
1736 } while (type != null);
1741 public static bool IsPrivateAccessible (Type type, Type parent)
1746 if (type.Equals (parent))
1749 return DropGenericTypeArguments (type) == DropGenericTypeArguments (parent);
1752 public static bool IsFamilyAccessible (Type type, Type parent)
1754 TypeParameter tparam = LookupTypeParameter (type);
1755 TypeParameter pparam = LookupTypeParameter (parent);
1757 if ((tparam != null) && (pparam != null)) {
1758 if (tparam == pparam)
1761 return tparam.IsSubclassOf (parent);
1765 if (IsInstantiationOfSameGenericType (type, parent))
1768 type = type.BaseType;
1769 } while (type != null);
1775 // Checks whether `type' is a subclass or nested child of `base_type'.
1777 public static bool IsNestedFamilyAccessible (Type type, Type base_type)
1780 if (IsFamilyAccessible (type, base_type))
1783 // Handle nested types.
1784 type = type.DeclaringType;
1785 } while (type != null);
1791 // Checks whether `type' is a nested child of `parent'.
1793 public static bool IsNestedChildOf (Type type, Type parent)
1798 type = DropGenericTypeArguments (type);
1799 parent = DropGenericTypeArguments (parent);
1801 if (IsEqual (type, parent))
1804 type = type.DeclaringType;
1805 while (type != null) {
1806 if (IsEqual (type, parent))
1809 type = type.DeclaringType;
1817 // Checks whether `extern_type' is friend of the output assembly
1819 public static bool IsFriendAssembly (Assembly assembly)
1821 // FIXME: This should not be reached
1822 if (assembly == CodeGen.Assembly.Builder)
1825 if (assembly_internals_vis_attrs.Contains (assembly))
1826 return (bool)(assembly_internals_vis_attrs [assembly]);
1828 object [] attrs = assembly.GetCustomAttributes (internals_visible_attr_type, false);
1829 if (attrs.Length == 0) {
1830 assembly_internals_vis_attrs.Add (assembly, false);
1834 AssemblyName this_name = CodeGen.Assembly.Name;
1835 byte [] this_token = this_name.GetPublicKeyToken ();
1836 bool is_friend = false;
1837 foreach (InternalsVisibleToAttribute attr in attrs) {
1838 if (attr.AssemblyName == null || attr.AssemblyName.Length == 0)
1841 AssemblyName aname = null;
1843 aname = new AssemblyName (attr.AssemblyName);
1844 } catch (FileLoadException) {
1845 } catch (ArgumentException) {
1848 if (aname == null || aname.Name != this_name.Name)
1851 byte [] key_token = aname.GetPublicKeyToken ();
1852 if (key_token != null) {
1853 if (this_token == null) {
1854 // Same name, but key token is null
1855 Error_FriendAccessNameNotMatching (aname.FullName);
1859 if (!CompareKeyTokens (this_token, key_token))
1867 assembly_internals_vis_attrs.Add (assembly, is_friend);
1871 static bool CompareKeyTokens (byte [] token1, byte [] token2)
1873 for (int i = 0; i < token1.Length; i++)
1874 if (token1 [i] != token2 [i])
1880 static void Error_FriendAccessNameNotMatching (string other_name)
1882 Report.Error (281, "Friend access was granted to `" + other_name +
1883 "', but the output assembly is named `" + CodeGen.Assembly.Name.FullName +
1884 "'. Try adding a reference to `" + other_name +
1885 "' or change the output assembly name to match it");
1888 public static bool IsFriendAssembly (Assembly assembly)
1895 // Do the right thing when returning the element type of an
1896 // array type based on whether we are compiling corlib or not
1898 public static Type GetElementType (Type t)
1900 if (RootContext.StdLib)
1901 return t.GetElementType ();
1903 return TypeToCoreType (t.GetElementType ());
1907 /// This method is not implemented by MS runtime for dynamic types
1909 public static bool HasElementType (Type t)
1911 return t.IsArray || t.IsPointer || t.IsByRef;
1915 /// Gigantic work around for missing features in System.Reflection.Emit follows.
1919 /// Since System.Reflection.Emit can not return MethodBase.GetParameters
1920 /// for anything which is dynamic, and we need this in a number of places,
1921 /// we register this information here, and use it afterwards.
1923 static public void RegisterMethod (MethodBase mb, Parameters ip)
1925 method_params.Add (mb, ip);
1928 static public ParameterData GetParameterData (MethodBase mb)
1930 ParameterData pd = (ParameterData)method_params [mb];
1932 if (mb is MethodBuilder || mb is ConstructorBuilder)
1933 throw new InternalErrorException ("Argument for Method not registered" + mb);
1936 if (mb.IsGenericMethod && !mb.IsGenericMethodDefinition)
1937 return GetParameterData (((MethodInfo)mb).GetGenericMethodDefinition ());
1939 pd = new ReflectionParameters (mb);
1940 method_params.Add (mb, pd);
1945 static public void RegisterOverride (MethodBase override_method, MethodBase base_method)
1947 if (!method_overrides.Contains (override_method))
1948 method_overrides [override_method] = base_method;
1949 if (method_overrides [override_method] != base_method)
1950 throw new InternalErrorException ("Override mismatch: " + override_method);
1953 static public bool IsOverride (MethodBase m)
1955 m = DropGenericMethodArguments (m);
1957 return m.IsVirtual &&
1958 (m.Attributes & MethodAttributes.NewSlot) == 0 &&
1959 (m is MethodBuilder || method_overrides.Contains (m));
1962 static public MethodBase TryGetBaseDefinition (MethodBase m)
1964 m = DropGenericMethodArguments (m);
1966 return (MethodBase) method_overrides [m];
1970 /// Returns the argument types for an indexer based on its PropertyInfo
1972 /// For dynamic indexers, we use the compiler provided types, for
1973 /// indexers from existing assemblies we load them from GetParameters,
1974 /// and insert them into the cache
1976 static public Type [] GetArgumentTypes (PropertyInfo indexer)
1978 if (indexer_arguments.Contains (indexer))
1979 return (Type []) indexer_arguments [indexer];
1980 else if (indexer is PropertyBuilder)
1981 // If we're a PropertyBuilder and not in the
1982 // `indexer_arguments' hash, then we're a property and
1984 return Type.EmptyTypes;
1986 ParameterInfo [] pi = indexer.GetIndexParameters ();
1987 // Property, not an indexer.
1989 return Type.EmptyTypes;
1991 Type [] types = new Type [c];
1993 for (int i = 0; i < c; i++)
1994 types [i] = pi [i].ParameterType;
1996 indexer_arguments.Add (indexer, types);
2001 public static void RegisterConstant (FieldInfo fb, IConstant ic)
2003 fields.Add (fb, ic);
2006 public static IConstant GetConstant (FieldInfo fb)
2011 return (IConstant)fields [fb];
2014 public static void RegisterProperty (PropertyInfo pi, PropertyBase pb)
2016 propertybuilder_to_property.Add (pi, pb);
2019 public static PropertyBase GetProperty (PropertyInfo pi)
2021 return (PropertyBase)propertybuilder_to_property [pi];
2024 static public void RegisterFieldBase (FieldBuilder fb, FieldBase f)
2026 fieldbuilders_to_fields.Add (fb, f);
2030 // The return value can be null; This will be the case for
2031 // auxiliary FieldBuilders created by the compiler that have no
2032 // real field being declared on the source code
2034 static public FieldBase GetField (FieldInfo fb)
2037 fb = GetGenericFieldDefinition (fb);
2039 return (FieldBase) fieldbuilders_to_fields [fb];
2042 static public MethodInfo GetAddMethod (EventInfo ei)
2044 if (ei is MyEventBuilder) {
2045 return ((MyEventBuilder)ei).GetAddMethod (true);
2047 return ei.GetAddMethod (true);
2050 static public MethodInfo GetRemoveMethod (EventInfo ei)
2052 if (ei is MyEventBuilder) {
2053 return ((MyEventBuilder)ei).GetRemoveMethod (true);
2055 return ei.GetRemoveMethod (true);
2058 static public void RegisterEventField (EventInfo einfo, EventField e)
2061 events = new Hashtable ();
2063 events.Add (einfo, e);
2066 static public EventField GetEventField (EventInfo ei)
2071 return (EventField) events [ei];
2074 static public bool RegisterIndexer (PropertyBuilder pb, MethodBase get,
2075 MethodBase set, Type[] args)
2077 indexer_arguments.Add (pb, args);
2082 public static bool CheckStructCycles (TypeContainer tc, Hashtable seen)
2084 Hashtable hash = new Hashtable ();
2085 return CheckStructCycles (tc, seen, hash);
2088 public static bool CheckStructCycles (TypeContainer tc, Hashtable seen,
2091 if ((tc.Kind != Kind.Struct) || IsBuiltinType (tc))
2095 // `seen' contains all types we've already visited.
2097 if (seen.Contains (tc))
2099 seen.Add (tc, null);
2101 if (tc.Fields == null)
2104 foreach (FieldBase field in tc.Fields) {
2105 if (field.FieldBuilder == null || field.FieldBuilder.IsStatic)
2108 Type ftype = field.FieldBuilder.FieldType;
2109 TypeContainer ftc = LookupTypeContainer (ftype);
2113 if (hash.Contains (ftc)) {
2114 Report.Error (523, tc.Location,
2115 "Struct member `{0}.{1}' of type `{2}' " +
2116 "causes a cycle in the struct layout",
2117 tc.Name, field.Name, ftc.Name);
2122 // `hash' contains all types in the current path.
2124 hash.Add (tc, null);
2126 bool ok = CheckStructCycles (ftc, seen, hash);
2133 if (!seen.Contains (ftc))
2134 seen.Add (ftc, null);
2141 /// Given an array of interface types, expand and eliminate repeated ocurrences
2142 /// of an interface.
2146 /// This expands in context like: IA; IB : IA; IC : IA, IB; the interface "IC" to
2149 public static Type[] ExpandInterfaces (TypeExpr [] base_interfaces)
2151 ArrayList new_ifaces = new ArrayList ();
2153 foreach (TypeExpr iface in base_interfaces){
2154 Type itype = iface.Type;
2156 if (new_ifaces.Contains (itype))
2159 new_ifaces.Add (itype);
2161 Type [] implementing = GetInterfaces (itype);
2163 foreach (Type imp in implementing){
2164 if (!new_ifaces.Contains (imp))
2165 new_ifaces.Add (imp);
2168 Type [] ret = new Type [new_ifaces.Count];
2169 new_ifaces.CopyTo (ret, 0);
2173 public static Type[] ExpandInterfaces (Type [] base_interfaces)
2175 ArrayList new_ifaces = new ArrayList ();
2177 foreach (Type itype in base_interfaces){
2178 if (new_ifaces.Contains (itype))
2181 new_ifaces.Add (itype);
2183 Type [] implementing = GetInterfaces (itype);
2185 foreach (Type imp in implementing){
2186 if (!new_ifaces.Contains (imp))
2187 new_ifaces.Add (imp);
2190 Type [] ret = new Type [new_ifaces.Count];
2191 new_ifaces.CopyTo (ret, 0);
2195 static PtrHashtable iface_cache = new PtrHashtable ();
2198 /// This function returns the interfaces in the type `t'. Works with
2199 /// both types and TypeBuilders.
2201 public static Type [] GetInterfaces (Type t)
2203 Type [] cached = iface_cache [t] as Type [];
2208 // The reason for catching the Array case is that Reflection.Emit
2209 // will not return a TypeBuilder for Array types of TypeBuilder types,
2210 // but will still throw an exception if we try to call GetInterfaces
2213 // Since the array interfaces are always constant, we return those for
2218 t = TypeManager.array_type;
2220 if ((t is TypeBuilder) || IsGenericType (t)) {
2221 Type [] base_ifaces;
2223 if (t.BaseType == null)
2224 base_ifaces = Type.EmptyTypes;
2226 base_ifaces = GetInterfaces (t.BaseType);
2228 if (IsGenericType (t))
2229 #if MS_COMPATIBLE && GMCS_SOURCE
2230 type_ifaces = t.GetGenericTypeDefinition().GetInterfaces ();
2232 type_ifaces = t.GetInterfaces ();
2235 type_ifaces = (Type []) builder_to_ifaces [t];
2236 if (type_ifaces == null || type_ifaces.Length == 0)
2237 type_ifaces = Type.EmptyTypes;
2239 int base_count = base_ifaces.Length;
2240 Type [] result = new Type [base_count + type_ifaces.Length];
2241 base_ifaces.CopyTo (result, 0);
2242 type_ifaces.CopyTo (result, base_count);
2244 iface_cache [t] = result;
2247 } else if (t is GenericTypeParameterBuilder){
2248 Type[] type_ifaces = (Type []) builder_to_ifaces [t];
2249 if (type_ifaces == null || type_ifaces.Length == 0)
2250 type_ifaces = Type.EmptyTypes;
2252 iface_cache [t] = type_ifaces;
2256 Type[] ifaces = t.GetInterfaces ();
2257 iface_cache [t] = ifaces;
2263 // gets the interfaces that are declared explicitly on t
2265 public static Type [] GetExplicitInterfaces (TypeBuilder t)
2267 return (Type []) builder_to_ifaces [t];
2271 /// The following is used to check if a given type implements an interface.
2272 /// The cache helps us reduce the expense of hitting Type.GetInterfaces everytime.
2274 public static bool ImplementsInterface (Type t, Type iface)
2279 // FIXME OPTIMIZATION:
2280 // as soon as we hit a non-TypeBuiler in the interface
2281 // chain, we could return, as the `Type.GetInterfaces'
2282 // will return all the interfaces implement by the type
2286 interfaces = GetInterfaces (t);
2288 if (interfaces != null){
2289 foreach (Type i in interfaces){
2296 } while (t != null);
2301 static NumberFormatInfo nf_provider = CultureInfo.CurrentCulture.NumberFormat;
2303 // This is a custom version of Convert.ChangeType() which works
2304 // with the TypeBuilder defined types when compiling corlib.
2305 public static object ChangeType (object value, Type conversionType, out bool error)
2307 IConvertible convert_value = value as IConvertible;
2309 if (convert_value == null){
2316 // We must use Type.Equals() here since `conversionType' is
2317 // the TypeBuilder created version of a system type and not
2318 // the system type itself. You cannot use Type.GetTypeCode()
2319 // on such a type - it'd always return TypeCode.Object.
2322 // We cannot rely on build-in type conversions as they are
2323 // more limited than what C# supports.
2324 // See char -> float/decimal/double conversion
2329 if (conversionType.Equals (typeof (Boolean)))
2330 return (object)(convert_value.ToBoolean (nf_provider));
2331 if (conversionType.Equals (typeof (Byte)))
2332 return (object)(convert_value.ToByte (nf_provider));
2333 if (conversionType.Equals (typeof (Char)))
2334 return (object)(convert_value.ToChar (nf_provider));
2335 if (conversionType.Equals (typeof (DateTime)))
2336 return (object)(convert_value.ToDateTime (nf_provider));
2338 if (conversionType.Equals (TypeManager.decimal_type)) {
2339 if (convert_value.GetType () == TypeManager.char_type)
2340 return (decimal)convert_value.ToInt32 (nf_provider);
2341 return convert_value.ToDecimal (nf_provider);
2344 if (conversionType.Equals (typeof (Double))) {
2345 if (convert_value.GetType () == TypeManager.char_type)
2346 return (double)convert_value.ToInt32 (nf_provider);
2347 return convert_value.ToDouble (nf_provider);
2350 if (conversionType.Equals (typeof (Int16)))
2351 return (object)(convert_value.ToInt16 (nf_provider));
2352 if (conversionType.Equals (typeof (Int32)))
2353 return (object)(convert_value.ToInt32 (nf_provider));
2354 if (conversionType.Equals (typeof (Int64)))
2355 return (object)(convert_value.ToInt64 (nf_provider));
2356 if (conversionType.Equals (typeof (SByte)))
2357 return (object)(convert_value.ToSByte (nf_provider));
2359 if (conversionType.Equals (typeof (Single))) {
2360 if (convert_value.GetType () == TypeManager.char_type)
2361 return (float)convert_value.ToInt32 (nf_provider);
2362 return convert_value.ToSingle (nf_provider);
2365 if (conversionType.Equals (typeof (String)))
2366 return (object)(convert_value.ToString (nf_provider));
2367 if (conversionType.Equals (typeof (UInt16)))
2368 return (object)(convert_value.ToUInt16 (nf_provider));
2369 if (conversionType.Equals (typeof (UInt32)))
2370 return (object)(convert_value.ToUInt32 (nf_provider));
2371 if (conversionType.Equals (typeof (UInt64)))
2372 return (object)(convert_value.ToUInt64 (nf_provider));
2373 if (conversionType.Equals (typeof (Object)))
2374 return (object)(value);
2384 // This is needed, because enumerations from assemblies
2385 // do not report their underlyingtype, but they report
2388 public static Type EnumToUnderlying (Type t)
2390 t = DropGenericTypeArguments (t);
2391 if (t == TypeManager.enum_type)
2394 t = t.UnderlyingSystemType;
2395 if (!TypeManager.IsEnumType (t))
2398 if (t is TypeBuilder) {
2399 // slow path needed to compile corlib
2400 if (t == TypeManager.bool_type ||
2401 t == TypeManager.byte_type ||
2402 t == TypeManager.sbyte_type ||
2403 t == TypeManager.char_type ||
2404 t == TypeManager.short_type ||
2405 t == TypeManager.ushort_type ||
2406 t == TypeManager.int32_type ||
2407 t == TypeManager.uint32_type ||
2408 t == TypeManager.int64_type ||
2409 t == TypeManager.uint64_type)
2412 TypeCode tc = Type.GetTypeCode (t);
2415 case TypeCode.Boolean:
2416 return TypeManager.bool_type;
2418 return TypeManager.byte_type;
2419 case TypeCode.SByte:
2420 return TypeManager.sbyte_type;
2422 return TypeManager.char_type;
2423 case TypeCode.Int16:
2424 return TypeManager.short_type;
2425 case TypeCode.UInt16:
2426 return TypeManager.ushort_type;
2427 case TypeCode.Int32:
2428 return TypeManager.int32_type;
2429 case TypeCode.UInt32:
2430 return TypeManager.uint32_type;
2431 case TypeCode.Int64:
2432 return TypeManager.int64_type;
2433 case TypeCode.UInt64:
2434 return TypeManager.uint64_type;
2436 throw new Exception ("Unhandled typecode in enum " + tc + " from " + t.AssemblyQualifiedName);
2440 // When compiling corlib and called with one of the core types, return
2441 // the corresponding typebuilder for that type.
2443 public static Type TypeToCoreType (Type t)
2445 if (RootContext.StdLib || (t is TypeBuilder))
2448 TypeCode tc = Type.GetTypeCode (t);
2451 case TypeCode.Boolean:
2452 return TypeManager.bool_type;
2454 return TypeManager.byte_type;
2455 case TypeCode.SByte:
2456 return TypeManager.sbyte_type;
2458 return TypeManager.char_type;
2459 case TypeCode.Int16:
2460 return TypeManager.short_type;
2461 case TypeCode.UInt16:
2462 return TypeManager.ushort_type;
2463 case TypeCode.Int32:
2464 return TypeManager.int32_type;
2465 case TypeCode.UInt32:
2466 return TypeManager.uint32_type;
2467 case TypeCode.Int64:
2468 return TypeManager.int64_type;
2469 case TypeCode.UInt64:
2470 return TypeManager.uint64_type;
2471 case TypeCode.Single:
2472 return TypeManager.float_type;
2473 case TypeCode.Double:
2474 return TypeManager.double_type;
2475 case TypeCode.String:
2476 return TypeManager.string_type;
2477 case TypeCode.Decimal:
2478 return TypeManager.decimal_type;
2480 if (t == typeof (void))
2481 return TypeManager.void_type;
2482 if (t == typeof (object))
2483 return TypeManager.object_type;
2484 if (t == typeof (System.Type))
2485 return TypeManager.type_type;
2486 if (t == typeof (System.IntPtr))
2487 return TypeManager.intptr_type;
2493 /// Utility function that can be used to probe whether a type
2494 /// is managed or not.
2496 public static bool VerifyUnManaged (Type t, Location loc)
2498 if (IsUnmanagedType (t))
2501 Report.Error (208, loc, "Cannot take the address of, get the size of, or declare a pointer to a managed type `{0}'",
2508 /// Returns the name of the indexer in a given type.
2511 /// The default is not always `Item'. The user can change this behaviour by
2512 /// using the IndexerNameAttribute in the container.
2513 /// For example, the String class indexer is named `Chars' not `Item'
2515 public static string IndexerPropertyName (Type t)
2517 t = DropGenericTypeArguments (t);
2518 if (t is TypeBuilder) {
2519 TypeContainer tc = t.IsInterface ? LookupInterface (t) : LookupTypeContainer (t);
2520 return tc == null ? TypeContainer.DefaultIndexerName : tc.IndexerName;
2523 System.Attribute attr = System.Attribute.GetCustomAttribute (
2524 t, TypeManager.default_member_type);
2526 DefaultMemberAttribute dma = (DefaultMemberAttribute) attr;
2527 return dma.MemberName;
2530 return TypeContainer.DefaultIndexerName;
2533 static MethodInfo declare_local_method = null;
2535 public static LocalBuilder DeclareLocalPinned (ILGenerator ig, Type t)
2537 if (declare_local_method == null){
2538 declare_local_method = typeof (ILGenerator).GetMethod (
2540 BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
2542 new Type [] { typeof (Type), typeof (bool)},
2544 if (declare_local_method == null){
2545 Report.RuntimeMissingSupport (Location.Null, "pinned local variables");
2546 return ig.DeclareLocal (t);
2549 return (LocalBuilder) declare_local_method.Invoke (ig, new object [] { t, true });
2552 private static bool IsSignatureEqual (Type a, Type b)
2555 /// Consider the following example (bug #77674):
2557 /// public abstract class A
2559 /// public abstract T Foo<T> ();
2562 /// public abstract class B : A
2564 /// public override U Foo<T> ()
2565 /// { return default (U); }
2568 /// Here, `T' and `U' are method type parameters from different methods
2569 /// (A.Foo and B.Foo), so both `==' and Equals() will fail.
2571 /// However, since we're determining whether B.Foo() overrides A.Foo(),
2572 /// we need to do a signature based comparision and consider them equal.
2578 if (a.IsGenericParameter && b.IsGenericParameter &&
2579 (a.DeclaringMethod != null) && (b.DeclaringMethod != null)) {
2580 return a.GenericParameterPosition == b.GenericParameterPosition;
2584 if (a.IsArray && b.IsArray) {
2585 if (a.GetArrayRank () != b.GetArrayRank ())
2588 return IsSignatureEqual (a.GetElementType (), b.GetElementType ());
2591 if (a.IsByRef && b.IsByRef)
2592 return IsSignatureEqual (a.GetElementType (), b.GetElementType ());
2595 if (a.IsGenericType && b.IsGenericType) {
2596 if (a.GetGenericTypeDefinition () != b.GetGenericTypeDefinition ())
2599 Type[] aargs = a.GetGenericArguments ();
2600 Type[] bargs = b.GetGenericArguments ();
2602 if (aargs.Length != bargs.Length)
2605 for (int i = 0; i < aargs.Length; i++) {
2606 if (!IsSignatureEqual (aargs [i], bargs [i]))
2618 // Returns whether the array of memberinfos contains the given method
2620 public static bool ArrayContainsMethod (MemberInfo [] array, MethodBase new_method)
2622 Type [] new_args = TypeManager.GetParameterData (new_method).Types;
2624 foreach (MethodBase method in array) {
2625 if (method.Name != new_method.Name)
2628 if (method is MethodInfo && new_method is MethodInfo)
2629 if (!IsSignatureEqual (((MethodInfo) method).ReturnType,
2630 ((MethodInfo) new_method).ReturnType))
2634 Type [] old_args = TypeManager.GetParameterData (method).Types;
2635 int old_count = old_args.Length;
2638 if (new_args.Length != old_count)
2641 for (i = 0; i < old_count; i++){
2642 if (!IsSignatureEqual (old_args [i], new_args [i]))
2655 // We copy methods from `new_members' into `target_list' if the signature
2656 // for the method from in the new list does not exist in the target_list
2658 // The name is assumed to be the same.
2660 public static ArrayList CopyNewMethods (ArrayList target_list, IList new_members)
2662 if (target_list == null){
2663 target_list = new ArrayList ();
2665 foreach (MemberInfo mi in new_members){
2666 if (mi is MethodBase)
2667 target_list.Add (mi);
2672 MemberInfo [] target_array = new MemberInfo [target_list.Count];
2673 target_list.CopyTo (target_array, 0);
2675 foreach (MemberInfo mi in new_members){
2676 MethodBase new_method = (MethodBase) mi;
2678 if (!ArrayContainsMethod (target_array, new_method))
2679 target_list.Add (new_method);
2686 // Tracks the generic parameters.
2688 static PtrHashtable builder_to_type_param;
2690 public static void AddTypeParameter (Type t, TypeParameter tparam)
2692 if (!builder_to_type_param.Contains (t))
2693 builder_to_type_param.Add (t, tparam);
2696 public static TypeParameter LookupTypeParameter (Type t)
2698 return (TypeParameter) builder_to_type_param [t];
2701 // This method always return false for non-generic compiler,
2702 // while Type.IsGenericParameter is returned if it is supported.
2703 public static bool IsGenericParameter (Type type)
2706 return type.IsGenericParameter;
2712 public static int GenericParameterPosition (Type type)
2715 return type.GenericParameterPosition;
2717 throw new InternalErrorException ("should not be called");
2721 public static bool IsGenericType (Type type)
2724 return type.IsGenericType;
2730 public static bool IsGenericTypeDefinition (Type type)
2733 return type.IsGenericTypeDefinition;
2739 public static bool ContainsGenericParameters (Type type)
2742 return type.ContainsGenericParameters;
2748 public static FieldInfo GetGenericFieldDefinition (FieldInfo fi)
2751 if (fi.DeclaringType.IsGenericTypeDefinition ||
2752 !fi.DeclaringType.IsGenericType)
2755 Type t = fi.DeclaringType.GetGenericTypeDefinition ();
2756 BindingFlags bf = BindingFlags.Public | BindingFlags.NonPublic |
2757 BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
2759 // TODO: use CodeGen.Module.Builder.ResolveField (fi.MetadataToken);
2760 foreach (FieldInfo f in t.GetFields (bf))
2761 if (f.MetadataToken == fi.MetadataToken)
2768 public static bool IsEqual (Type a, Type b)
2771 // MS BCL returns true even if enum types are different
2772 if (a.BaseType == TypeManager.enum_type || b.BaseType == TypeManager.enum_type)
2773 return a.FullName == b.FullName;
2779 if (a.IsGenericParameter && b.IsGenericParameter) {
2780 if (a.DeclaringMethod != b.DeclaringMethod &&
2781 (a.DeclaringMethod == null || b.DeclaringMethod == null))
2783 return a.GenericParameterPosition == b.GenericParameterPosition;
2786 if (a.IsArray && b.IsArray) {
2787 if (a.GetArrayRank () != b.GetArrayRank ())
2789 return IsEqual (a.GetElementType (), b.GetElementType ());
2792 if (a.IsByRef && b.IsByRef)
2793 return IsEqual (a.GetElementType (), b.GetElementType ());
2795 if (a.IsGenericType && b.IsGenericType) {
2796 Type adef = a.GetGenericTypeDefinition ();
2797 Type bdef = b.GetGenericTypeDefinition ();
2802 if (adef.IsEnum && bdef.IsEnum)
2805 Type[] aargs = a.GetGenericArguments ();
2806 Type[] bargs = b.GetGenericArguments ();
2808 if (aargs.Length != bargs.Length)
2811 for (int i = 0; i < aargs.Length; i++) {
2812 if (!IsEqual (aargs [i], bargs [i]))
2823 public static Type DropGenericTypeArguments (Type t)
2826 if (!t.IsGenericType)
2828 // Micro-optimization: a generic typebuilder is always a generic type definition
2829 if (t is TypeBuilder)
2831 return t.GetGenericTypeDefinition ();
2837 public static MethodBase DropGenericMethodArguments (MethodBase m)
2840 if (m.IsGenericMethodDefinition)
2842 if (m.IsGenericMethod)
2843 return ((MethodInfo) m).GetGenericMethodDefinition ();
2844 if (!m.DeclaringType.IsGenericType)
2847 Type t = m.DeclaringType.GetGenericTypeDefinition ();
2848 BindingFlags bf = BindingFlags.Public | BindingFlags.NonPublic |
2849 BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
2852 // TODO: use CodeGen.Module.Builder.ResolveMethod ()
2856 if (m is ConstructorInfo) {
2857 foreach (ConstructorInfo c in t.GetConstructors (bf))
2858 if (c.MetadataToken == m.MetadataToken)
2861 foreach (MethodBase mb in t.GetMethods (bf))
2862 if (mb.MetadataToken == m.MetadataToken)
2870 public static Type[] GetGenericArguments (MethodInfo mi)
2873 return mi.GetGenericArguments ();
2875 return Type.EmptyTypes;
2879 public static Type[] GetTypeArguments (Type t)
2882 DeclSpace tc = LookupDeclSpace (t);
2885 return Type.EmptyTypes;
2887 TypeParameter[] tparam = tc.TypeParameters;
2888 Type[] ret = new Type [tparam.Length];
2889 for (int i = 0; i < tparam.Length; i++) {
2890 ret [i] = tparam [i].Type;
2891 if (ret [i] == null)
2892 throw new InternalErrorException ();
2897 return t.GetGenericArguments ();
2899 throw new InternalErrorException ();
2903 public static bool HasGenericArguments (Type t)
2905 return GetNumberOfTypeArguments (t) > 0;
2908 public static int GetNumberOfTypeArguments (Type t)
2911 if (t.IsGenericParameter)
2913 DeclSpace tc = LookupDeclSpace (t);
2915 return tc.IsGeneric ? tc.CountTypeParameters : 0;
2917 return t.IsGenericType ? t.GetGenericArguments ().Length : 0;
2924 /// Check whether `type' and `parent' are both instantiations of the same
2925 /// generic type. Note that we do not check the type parameters here.
2927 public static bool IsInstantiationOfSameGenericType (Type type, Type parent)
2929 int tcount = GetNumberOfTypeArguments (type);
2930 int pcount = GetNumberOfTypeArguments (parent);
2932 if (tcount != pcount)
2935 type = DropGenericTypeArguments (type);
2936 parent = DropGenericTypeArguments (parent);
2938 return type.Equals (parent);
2942 /// Whether `mb' is a generic method definition.
2944 public static bool IsGenericMethodDefinition (MethodBase mb)
2947 if (mb.DeclaringType is TypeBuilder) {
2948 IMethodData method = (IMethodData) builder_to_method [mb];
2952 return method.GenericMethod != null;
2955 return mb.IsGenericMethodDefinition;
2962 /// Whether `mb' is a generic method.
2964 public static bool IsGenericMethod (MethodBase mb)
2967 return mb.IsGenericMethod;
2973 public static bool IsNullableType (Type t)
2976 return generic_nullable_type == DropGenericTypeArguments (t);
2982 public static bool IsNullableTypeOf (Type t, Type nullable)
2985 if (!IsNullableType (t))
2988 return GetTypeArguments (t) [0] == nullable;
2994 public static bool IsNullableValueType (Type t)
2997 if (!IsNullableType (t))
3000 return GetTypeArguments (t) [0].IsValueType;
3007 #region MemberLookup implementation
3010 // Whether we allow private members in the result (since FindMembers
3011 // uses NonPublic for both protected and private), we need to distinguish.
3014 internal class Closure {
3015 internal bool private_ok;
3017 // Who is invoking us and which type is being queried currently.
3018 internal Type invocation_type;
3019 internal Type qualifier_type;
3021 // The assembly that defines the type is that is calling us
3022 internal Assembly invocation_assembly;
3023 internal IList almost_match;
3025 private bool CheckValidFamilyAccess (bool is_static, MemberInfo m)
3027 if (invocation_type == null)
3030 if (is_static && qualifier_type == null)
3031 // It resolved from a simple name, so it should be visible.
3034 if (IsNestedChildOf (invocation_type, m.DeclaringType))
3037 for (Type t = invocation_type; t != null; t = t.DeclaringType) {
3038 if (!IsFamilyAccessible (t, m.DeclaringType))
3041 // Although a derived class can access protected members of its base class
3042 // it cannot do so through an instance of the base class (CS1540).
3043 // => Ancestry should be: declaring_type ->* invocation_type ->* qualified_type
3044 if (is_static || qualifier_type == null ||
3045 IsInstantiationOfSameGenericType (t, qualifier_type) ||
3046 IsFamilyAccessible (qualifier_type, t))
3050 if (almost_match != null)
3051 almost_match.Add (m);
3057 // This filter filters by name + whether it is ok to include private
3058 // members in the search
3060 internal bool Filter (MemberInfo m, object filter_criteria)
3063 // Hack: we know that the filter criteria will always be in the
3064 // `closure' // fields.
3067 if ((filter_criteria != null) && (m.Name != (string) filter_criteria))
3070 if (((qualifier_type == null) || (qualifier_type == invocation_type)) &&
3071 (invocation_type != null) &&
3072 IsPrivateAccessible (m.DeclaringType, invocation_type))
3076 // Ugly: we need to find out the type of `m', and depending
3077 // on this, tell whether we accept or not
3079 if (m is MethodBase){
3080 MethodBase mb = (MethodBase) m;
3081 MethodAttributes ma = mb.Attributes & MethodAttributes.MemberAccessMask;
3083 if (ma == MethodAttributes.Public)
3086 if (ma == MethodAttributes.PrivateScope)
3089 if (ma == MethodAttributes.Private)
3090 return private_ok ||
3091 IsPrivateAccessible (invocation_type, m.DeclaringType) ||
3092 IsNestedChildOf (invocation_type, m.DeclaringType);
3094 if (invocation_assembly == mb.DeclaringType.Assembly ||
3095 TypeManager.IsFriendAssembly (mb.DeclaringType.Assembly)) {
3096 if (ma == MethodAttributes.Assembly || ma == MethodAttributes.FamORAssem)
3099 if (ma == MethodAttributes.Assembly || ma == MethodAttributes.FamANDAssem)
3103 // Family, FamORAssem or FamANDAssem
3104 return CheckValidFamilyAccess (mb.IsStatic, m);
3107 if (m is FieldInfo){
3108 FieldInfo fi = (FieldInfo) m;
3109 FieldAttributes fa = fi.Attributes & FieldAttributes.FieldAccessMask;
3111 if (fa == FieldAttributes.Public)
3114 if (fa == FieldAttributes.PrivateScope)
3117 if (fa == FieldAttributes.Private)
3118 return private_ok ||
3119 IsPrivateAccessible (invocation_type, m.DeclaringType) ||
3120 IsNestedChildOf (invocation_type, m.DeclaringType);
3122 if ((invocation_assembly == fi.DeclaringType.Assembly) ||
3123 (invocation_assembly == null) ||
3124 TypeManager.IsFriendAssembly (fi.DeclaringType.Assembly)) {
3125 if ((fa == FieldAttributes.Assembly) ||
3126 (fa == FieldAttributes.FamORAssem))
3129 if ((fa == FieldAttributes.Assembly) ||
3130 (fa == FieldAttributes.FamANDAssem))
3134 // Family, FamORAssem or FamANDAssem
3135 return CheckValidFamilyAccess (fi.IsStatic, m);
3139 // EventInfos and PropertyInfos, return true because they lack
3140 // permission information, so we need to check later on the methods.
3146 static Closure closure = new Closure ();
3147 static MemberFilter FilterWithClosure_delegate = new MemberFilter (closure.Filter);
3150 // Looks up a member called `name' in the `queried_type'. This lookup
3151 // is done by code that is contained in the definition for `invocation_type'
3152 // through a qualifier of type `qualifier_type' (or null if there is no qualifier).
3154 // `invocation_type' is used to check whether we're allowed to access the requested
3155 // member wrt its protection level.
3157 // When called from MemberAccess, `qualifier_type' is the type which is used to access
3158 // the requested member (`class B { A a = new A (); a.foo = 5; }'; here invocation_type
3159 // is B and qualifier_type is A). This is used to do the CS1540 check.
3161 // When resolving a SimpleName, `qualifier_type' is null.
3163 // The `qualifier_type' is used for the CS1540 check; it's normally either null or
3164 // the same than `queried_type' - except when we're being called from BaseAccess;
3165 // in this case, `invocation_type' is the current type and `queried_type' the base
3166 // type, so this'd normally trigger a CS1540.
3168 // The binding flags are `bf' and the kind of members being looked up are `mt'
3170 // The return value always includes private members which code in `invocation_type'
3171 // is allowed to access (using the specified `qualifier_type' if given); only use
3172 // BindingFlags.NonPublic to bypass the permission check.
3174 // The 'almost_match' argument is used for reporting error CS1540.
3176 // Returns an array of a single element for everything but Methods/Constructors
3177 // that might return multiple matches.
3179 public static MemberInfo [] MemberLookup (Type invocation_type, Type qualifier_type,
3180 Type queried_type, MemberTypes mt,
3181 BindingFlags original_bf, string name, IList almost_match)
3183 Timer.StartTimer (TimerType.MemberLookup);
3185 MemberInfo[] retval = RealMemberLookup (invocation_type, qualifier_type,
3186 queried_type, mt, original_bf, name, almost_match);
3188 Timer.StopTimer (TimerType.MemberLookup);
3193 static MemberInfo [] RealMemberLookup (Type invocation_type, Type qualifier_type,
3194 Type queried_type, MemberTypes mt,
3195 BindingFlags original_bf, string name, IList almost_match)
3197 BindingFlags bf = original_bf;
3199 ArrayList method_list = null;
3200 Type current_type = queried_type;
3201 bool searching = (original_bf & BindingFlags.DeclaredOnly) == 0;
3202 bool skip_iface_check = true, used_cache = false;
3203 bool always_ok_flag = invocation_type != null && IsNestedChildOf (invocation_type, queried_type);
3205 closure.invocation_type = invocation_type;
3206 closure.invocation_assembly = invocation_type != null ? invocation_type.Assembly : null;
3207 closure.qualifier_type = qualifier_type;
3208 closure.almost_match = almost_match;
3210 // This is from the first time we find a method
3211 // in most cases, we do not actually find a method in the base class
3212 // so we can just ignore it, and save the arraylist allocation
3213 MemberInfo [] first_members_list = null;
3214 bool use_first_members_list = false;
3220 // `NonPublic' is lame, because it includes both protected and
3221 // private methods, so we need to control this behavior by
3222 // explicitly tracking if a private method is ok or not.
3224 // The possible cases are:
3225 // public, private and protected (internal does not come into the
3228 if ((invocation_type != null) &&
3229 ((invocation_type == current_type) ||
3230 IsNestedChildOf (invocation_type, current_type)) ||
3232 bf = original_bf | BindingFlags.NonPublic;
3236 closure.private_ok = (original_bf & BindingFlags.NonPublic) != 0;
3238 Timer.StopTimer (TimerType.MemberLookup);
3240 list = MemberLookup_FindMembers (current_type, mt, bf, name, out used_cache);
3242 Timer.StartTimer (TimerType.MemberLookup);
3245 // When queried for an interface type, the cache will automatically check all
3246 // inherited members, so we don't need to do this here. However, this only
3247 // works if we already used the cache in the first iteration of this loop.
3249 // If we used the cache in any further iteration, we can still terminate the
3250 // loop since the cache always looks in all base classes.
3256 skip_iface_check = false;
3258 if (current_type == TypeManager.object_type)
3261 current_type = current_type.BaseType;
3264 // This happens with interfaces, they have a null
3265 // basetype. Look members up in the Object class.
3267 if (current_type == null) {
3268 current_type = TypeManager.object_type;
3273 if (list.Length == 0)
3277 // Events and types are returned by both `static' and `instance'
3278 // searches, which means that our above FindMembers will
3279 // return two copies of the same.
3281 if (list.Length == 1 && !(list [0] is MethodBase)){
3286 // Multiple properties: we query those just to find out the indexer
3289 if (list [0] is PropertyInfo)
3293 // We found an event: the cache lookup returns both the event and
3294 // its private field.
3296 if (list [0] is EventInfo) {
3297 if ((list.Length == 2) && (list [1] is FieldInfo))
3298 return new MemberInfo [] { list [0] };
3304 // We found methods, turn the search into "method scan"
3308 if (first_members_list != null) {
3309 if (use_first_members_list) {
3310 method_list = CopyNewMethods (method_list, first_members_list);
3311 use_first_members_list = false;
3314 method_list = CopyNewMethods (method_list, list);
3316 first_members_list = list;
3317 use_first_members_list = true;
3318 mt &= (MemberTypes.Method | MemberTypes.Constructor);
3320 } while (searching);
3322 if (use_first_members_list)
3323 return first_members_list;
3325 if (method_list != null && method_list.Count > 0) {
3326 return (MemberInfo []) method_list.ToArray (typeof (MemberInfo));
3329 // This happens if we already used the cache in the first iteration, in this case
3330 // the cache already looked in all interfaces.
3332 if (skip_iface_check)
3336 // Interfaces do not list members they inherit, so we have to
3339 if (!queried_type.IsInterface)
3342 if (queried_type.IsArray)
3343 queried_type = TypeManager.array_type;
3345 Type [] ifaces = GetInterfaces (queried_type);
3349 foreach (Type itype in ifaces){
3352 x = MemberLookup (null, null, itype, mt, bf, name, null);
3360 const BindingFlags AllMembers = BindingFlags.Public | BindingFlags.NonPublic |
3361 BindingFlags.Static | BindingFlags.Instance |
3362 BindingFlags.DeclaredOnly;
3364 // Currently is designed to work with external types only
3365 public static PropertyInfo GetPropertyFromAccessor (MethodBase mb)
3367 if (!mb.IsSpecialName)
3370 string name = mb.Name;
3371 if (name.Length < 5)
3374 if (name [3] != '_')
3377 if (name.StartsWith ("get") || name.StartsWith ("set")) {
3378 MemberInfo[] pi = mb.DeclaringType.FindMembers (MemberTypes.Property, AllMembers,
3379 Type.FilterName, name.Substring (4));
3384 // This can happen when property is indexer (it can have same name but different parameters)
3385 foreach (PropertyInfo p in pi) {
3386 foreach (MethodInfo p_mi in p.GetAccessors (true)) {
3387 if (p_mi == mb || TypeManager.GetParameterData (p_mi).Equals (TypeManager.GetParameterData (mb)))
3396 // Currently is designed to work with external types only
3397 public static MemberInfo GetEventFromAccessor (MethodBase mb)
3399 if (!mb.IsSpecialName)
3402 string name = mb.Name;
3403 if (name.Length < 5)
3406 if (name.StartsWith ("add_"))
3407 return mb.DeclaringType.GetEvent (name.Substring (4), AllMembers);
3409 if (name.StartsWith ("remove_"))
3410 return mb.DeclaringType.GetEvent (name.Substring (7), AllMembers);
3415 // Tests whether external method is really special
3416 public static bool IsSpecialMethod (MethodBase mb)
3418 if (!mb.IsSpecialName)
3421 IMethodData md = TypeManager.GetMethod (mb);
3423 return (md is AbstractPropertyEventMethod || md is Operator);
3425 PropertyInfo pi = GetPropertyFromAccessor (mb);
3427 return IsValidProperty (pi);
3429 if (GetEventFromAccessor (mb) != null)
3432 string name = mb.Name;
3433 if (name.StartsWith ("op_")){
3434 foreach (string oname in Unary.oper_names) {
3439 foreach (string oname in Binary.oper_names) {
3447 // Tests whether imported property is valid C# property.
3448 // TODO: It seems to me that we should do a lot of sanity tests before
3449 // we accept property as C# property
3450 static bool IsValidProperty (PropertyInfo pi)
3452 MethodInfo get_method = pi.GetGetMethod (true);
3453 MethodInfo set_method = pi.GetSetMethod (true);
3454 if (get_method != null && set_method != null) {
3455 int g_count = get_method.GetParameters ().Length;
3456 int s_count = set_method.GetParameters ().Length;
3457 if (g_count + 1 != s_count)
3468 /// There is exactly one instance of this class per type.
3470 public sealed class TypeHandle : IMemberContainer {
3471 public readonly IMemberContainer BaseType;
3473 readonly int id = ++next_id;
3474 static int next_id = 0;
3476 static TypeHandle ()
3482 /// Lookup a TypeHandle instance for the given type. If the type doesn't have
3483 /// a TypeHandle yet, a new instance of it is created. This static method
3484 /// ensures that we'll only have one TypeHandle instance per type.
3486 private static TypeHandle GetTypeHandle (Type t)
3488 TypeHandle handle = (TypeHandle) type_hash [t];
3492 handle = new TypeHandle (t);
3493 type_hash.Add (t, handle);
3497 public static MemberCache GetMemberCache (Type t)
3499 return GetTypeHandle (t).MemberCache;
3502 public static void CleanUp ()
3507 public static void Reset ()
3509 type_hash = new PtrHashtable ();
3513 /// Returns the TypeHandle for TypeManager.object_type.
3515 public static IMemberContainer ObjectType {
3517 if (object_type != null)
3520 object_type = GetTypeHandle (TypeManager.object_type);
3527 /// Returns the TypeHandle for TypeManager.array_type.
3529 public static IMemberContainer ArrayType {
3531 if (array_type != null)
3534 array_type = GetTypeHandle (TypeManager.array_type);
3540 private static PtrHashtable type_hash;
3542 private static TypeHandle object_type = null;
3543 private static TypeHandle array_type = null;
3546 private string full_name;
3547 private bool is_interface;
3548 private MemberCache member_cache;
3549 private MemberCache base_cache;
3551 private TypeHandle (Type type)
3554 #if MS_COMPATIBLE && GMCS_SOURCE
3555 if (type.IsGenericType && type is TypeBuilder)
3556 this.type = this.type.GetGenericTypeDefinition ();
3558 full_name = type.FullName != null ? type.FullName : type.Name;
3559 if (type.BaseType != null) {
3560 base_cache = TypeManager.LookupMemberCache (type.BaseType);
3561 BaseType = base_cache.Container;
3562 } else if (type.IsInterface)
3563 base_cache = TypeManager.LookupBaseInterfacesCache (type);
3564 this.is_interface = type.IsInterface || TypeManager.IsGenericParameter (type);
3565 this.member_cache = new MemberCache (this);
3568 // IMemberContainer methods
3570 public string Name {
3582 public MemberCache BaseCache {
3588 public bool IsInterface {
3590 return is_interface;
3594 public MemberList GetMembers (MemberTypes mt, BindingFlags bf)
3596 MemberInfo [] members;
3599 if (type is GenericTypeParameterBuilder)
3600 return MemberList.Empty;
3603 if (mt == MemberTypes.Event)
3604 members = type.GetEvents (bf | BindingFlags.DeclaredOnly);
3606 members = type.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
3608 Array.Reverse (members);
3610 return new MemberList (members);
3613 // IMemberFinder methods
3615 public MemberList FindMembers (MemberTypes mt, BindingFlags bf, string name,
3616 MemberFilter filter, object criteria)
3618 return new MemberList (member_cache.FindMembers (mt, bf, name, filter, criteria));
3621 public MemberCache MemberCache {
3623 return member_cache;
3627 public override string ToString ()
3629 if (BaseType != null)
3630 return "TypeHandle (" + id + "," + Name + " : " + BaseType + ")";
3632 return "TypeHandle (" + id + "," + Name + ")";