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 // Dual licensed under the terms of the MIT X11 or GNU GPL
10 // Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
11 // Copyright 2003-2008 Novell, Inc.
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.Generic;
25 using System.Reflection;
26 using System.Reflection.Emit;
28 using System.Runtime.CompilerServices;
29 using System.Diagnostics;
31 namespace Mono.CSharp {
33 partial class TypeManager {
35 // A list of core types that the compiler requires or uses
37 static public Type object_type;
38 static public Type value_type;
39 static public Type string_type;
40 static public Type int32_type;
41 static public Type uint32_type;
42 static public Type int64_type;
43 static public Type uint64_type;
44 static public Type float_type;
45 static public Type double_type;
46 static public Type char_type;
47 static public Type short_type;
48 static public Type decimal_type;
49 static public Type bool_type;
50 static public Type sbyte_type;
51 static public Type byte_type;
52 static public Type ushort_type;
53 static public Type enum_type;
54 static public Type delegate_type;
55 static public Type multicast_delegate_type;
56 static public Type void_type;
57 static public Type null_type;
58 static public Type array_type;
59 static public Type runtime_handle_type;
60 static public Type type_type;
61 static public Type ienumerator_type;
62 static public Type ienumerable_type;
63 static public Type idisposable_type;
64 static public Type iasyncresult_type;
65 static public Type asynccallback_type;
66 static public Type intptr_type;
67 static public Type uintptr_type;
68 static public Type runtime_field_handle_type;
69 static public Type runtime_argument_handle_type;
70 static public Type attribute_type;
71 static public Type void_ptr_type;
72 static public Type exception_type;
74 static public Type typed_reference_type;
75 static public Type arg_iterator_type;
76 static public Type mbr_type;
77 public static Type runtime_helpers_type;
82 static internal Type isvolatile_type;
83 static public Type generic_ilist_type;
84 static public Type generic_icollection_type;
85 static public Type generic_ienumerator_type;
86 static public Type generic_ienumerable_type;
87 static public Type generic_nullable_type;
92 static internal Type expression_type;
93 public static Type parameter_expression_type;
94 public static Type fieldinfo_type;
95 public static Type methodinfo_type;
96 public static Type ctorinfo_type;
101 public static Type call_site_type;
102 public static Type generic_call_site_type;
103 public static TypeExpr binder_type;
104 public static Type binder_flags;
107 // Expressions representing the internal types. Used during declaration
110 static public TypeExpr system_object_expr, system_string_expr;
111 static public TypeExpr system_boolean_expr, system_decimal_expr;
112 static public TypeExpr system_single_expr, system_double_expr;
113 static public TypeExpr system_sbyte_expr, system_byte_expr;
114 static public TypeExpr system_int16_expr, system_uint16_expr;
115 static public TypeExpr system_int32_expr, system_uint32_expr;
116 static public TypeExpr system_int64_expr, system_uint64_expr;
117 static public TypeExpr system_char_expr, system_void_expr;
118 static public TypeExpr system_valuetype_expr;
119 static public TypeExpr system_intptr_expr;
120 public static TypeExpr expression_type_expr;
124 // These methods are called by code generated by the compiler
126 static public FieldInfo string_empty;
127 static public MethodSpec system_type_get_type_from_handle;
128 static public MethodSpec bool_movenext_void;
129 static public MethodSpec void_dispose_void;
130 static public MethodSpec void_monitor_enter_object;
131 static public MethodSpec void_monitor_exit_object;
132 static public MethodSpec void_initializearray_array_fieldhandle;
133 static public MethodSpec delegate_combine_delegate_delegate;
134 static public MethodSpec delegate_remove_delegate_delegate;
135 static public PropertyInfo int_get_offset_to_string_data;
136 static public MethodSpec int_interlocked_compare_exchange;
137 static public PropertyInfo ienumerator_getcurrent;
138 public static MethodSpec methodbase_get_type_from_handle;
139 public static MethodSpec methodbase_get_type_from_handle_generic;
140 public static MethodSpec fieldinfo_get_field_from_handle;
141 public static MethodSpec fieldinfo_get_field_from_handle_generic;
142 static public MethodSpec activator_create_instance;
147 static public ConstructorInfo void_decimal_ctor_five_args;
148 static public ConstructorInfo void_decimal_ctor_int_arg;
149 public static ConstructorInfo void_decimal_ctor_long_arg;
151 static Dictionary<TypeBuilder, DeclSpace> builder_to_declspace;
153 static Dictionary<Type, MemberCache> builder_to_member_cache;
156 // Tracks the interfaces implemented by typebuilders. We only
157 // enter those who do implement or or more interfaces
159 static Dictionary<Type, Type[]> builder_to_ifaces;
162 // Maps a MethodBase to its ParameterData (either InternalParameters or ReflectionParameters)
164 static Dictionary<MemberInfo, AParametersCollection> method_params;
167 // A hash table from override methods to their base virtual method.
169 static Dictionary<MethodBase, MethodBase> method_overrides;
172 // Keeps track of methods
175 static Dictionary<MethodBase, IMethodData> builder_to_method;
178 // Contains all public types from referenced assemblies.
179 // This member is used only if CLS Compliance verification is required.
181 public static Dictionary<string, object> AllClsTopLevelTypes;
183 static Dictionary<FieldInfo, FieldBase> fieldbuilders_to_fields;
184 static Dictionary<PropertyInfo, PropertyBase> propertybuilder_to_property;
185 static Dictionary<FieldInfo, ConstSpec> fields;
186 static Dictionary<EventInfo, EventField> events;
187 static Dictionary<Assembly, bool> assembly_internals_vis_attrs;
188 static Dictionary<GenericTypeParameterBuilder, TypeParameter> builder_to_type_param;
189 static Dictionary<Type, Type[]> iface_cache;
191 public static void CleanUp ()
193 // Lets get everything clean so that we can collect before generating code
194 builder_to_declspace = null;
195 builder_to_member_cache = null;
196 builder_to_ifaces = null;
197 builder_to_type_param = null;
198 method_params = null;
199 builder_to_method = null;
205 propertybuilder_to_property = null;
207 TypeHandle.CleanUp ();
211 // These are expressions that represent some of the internal data types, used
214 static void InitExpressionTypes ()
216 system_object_expr = new TypeLookupExpression ("System", "Object");
217 system_string_expr = new TypeLookupExpression ("System", "String");
218 system_boolean_expr = new TypeLookupExpression ("System", "Boolean");
219 system_decimal_expr = new TypeLookupExpression ("System", "Decimal");
220 system_single_expr = new TypeLookupExpression ("System", "Single");
221 system_double_expr = new TypeLookupExpression ("System", "Double");
222 system_sbyte_expr = new TypeLookupExpression ("System", "SByte");
223 system_byte_expr = new TypeLookupExpression ("System", "Byte");
224 system_int16_expr = new TypeLookupExpression ("System", "Int16");
225 system_uint16_expr = new TypeLookupExpression ("System", "UInt16");
226 system_int32_expr = new TypeLookupExpression ("System", "Int32");
227 system_uint32_expr = new TypeLookupExpression ("System", "UInt32");
228 system_int64_expr = new TypeLookupExpression ("System", "Int64");
229 system_uint64_expr = new TypeLookupExpression ("System", "UInt64");
230 system_char_expr = new TypeLookupExpression ("System", "Char");
231 system_void_expr = new TypeLookupExpression ("System", "Void");
232 system_valuetype_expr = new TypeLookupExpression ("System", "ValueType");
233 system_intptr_expr = new TypeLookupExpression ("System", "IntPtr");
236 static TypeManager ()
241 static public void Reset ()
245 InitExpressionTypes ();
247 builder_to_declspace = new Dictionary<TypeBuilder, DeclSpace> (ReferenceEquality<TypeBuilder>.Default);
248 builder_to_member_cache = new Dictionary<Type, MemberCache> (ReferenceEquality<Type>.Default);
249 builder_to_method = new Dictionary<MethodBase, IMethodData> (ReferenceEquality<MethodBase>.Default);
250 builder_to_type_param = new Dictionary<GenericTypeParameterBuilder, TypeParameter> (ReferenceEquality<GenericTypeParameterBuilder>.Default);
251 method_params = new Dictionary<MemberInfo, AParametersCollection> (ReferenceEquality<MemberInfo>.Default);
252 method_overrides = new Dictionary<MethodBase, MethodBase> (ReferenceEquality<MethodBase>.Default);
253 builder_to_ifaces = new Dictionary<Type, Type[]> (ReferenceEquality<Type>.Default);
255 fieldbuilders_to_fields = new Dictionary<FieldInfo, FieldBase> (ReferenceEquality<FieldInfo>.Default);
256 propertybuilder_to_property = new Dictionary<PropertyInfo, PropertyBase> (ReferenceEquality<PropertyInfo>.Default);
257 fields = new Dictionary<FieldInfo, ConstSpec> (ReferenceEquality<FieldInfo>.Default);
258 type_hash = new DoubleHash ();
259 assembly_internals_vis_attrs = new Dictionary<Assembly, bool> ();
260 iface_cache = new Dictionary<Type, Type[]> (ReferenceEquality<Type>.Default);
262 closure = new Closure ();
263 FilterWithClosure_delegate = new MemberFilter (closure.Filter);
265 // TODO: I am really bored by all this static stuff
266 system_type_get_type_from_handle =
269 void_monitor_enter_object =
270 void_monitor_exit_object =
271 void_initializearray_array_fieldhandle =
272 int_interlocked_compare_exchange =
273 methodbase_get_type_from_handle =
274 methodbase_get_type_from_handle_generic =
275 fieldinfo_get_field_from_handle =
276 fieldinfo_get_field_from_handle_generic =
277 activator_create_instance =
278 delegate_combine_delegate_delegate =
279 delegate_remove_delegate_delegate = null;
281 int_get_offset_to_string_data =
282 ienumerator_getcurrent = null;
284 void_decimal_ctor_five_args =
285 void_decimal_ctor_int_arg =
286 void_decimal_ctor_long_arg = null;
288 isvolatile_type = null;
291 generic_call_site_type =
296 // to uncover regressions
297 AllClsTopLevelTypes = null;
300 public static void AddUserType (DeclSpace ds)
302 builder_to_declspace.Add (ds.TypeBuilder, ds);
306 // This entry point is used by types that we define under the covers
308 public static void RegisterBuilder (Type tb, Type [] ifaces)
311 builder_to_ifaces [tb] = ifaces;
314 public static void AddMethod (MethodBase builder, IMethodData method)
316 builder_to_method.Add (builder, method);
317 method_params.Add (builder, method.ParameterInfo);
320 public static IMethodData GetMethod (MethodBase builder)
323 if (builder_to_method.TryGetValue (builder, out md))
329 /// Returns the DeclSpace whose Type is `t' or null if there is no
330 /// DeclSpace for `t' (ie, the Type comes from a library)
332 public static DeclSpace LookupDeclSpace (Type t)
335 var tb = t as TypeBuilder;
336 if (tb != null && builder_to_declspace.TryGetValue (tb, out ds))
343 /// Returns the TypeContainer whose Type is `t' or null if there is no
344 /// TypeContainer for `t' (ie, the Type comes from a library)
346 public static TypeContainer LookupTypeContainer (Type t)
348 return LookupDeclSpace (t) as TypeContainer;
351 public static MemberCache LookupMemberCache (Type t)
353 if (IsBeingCompiled (t)) {
354 DeclSpace container = LookupDeclSpace (t);
355 if (container != null)
356 return container.MemberCache;
359 if (t is GenericTypeParameterBuilder) {
360 TypeParameter container;
361 if (builder_to_type_param.TryGetValue ((GenericTypeParameterBuilder) t, out container))
362 return container.MemberCache;
365 return TypeHandle.GetMemberCache (t);
368 public static MemberCache LookupBaseInterfacesCache (Type t)
370 Type [] ifaces = GetInterfaces (t);
372 if (ifaces != null && ifaces.Length == 1)
373 return LookupMemberCache (ifaces [0]);
375 // TODO: the builder_to_member_cache should be indexed by 'ifaces', not 't'
377 if (builder_to_member_cache.TryGetValue (t, out cache))
380 cache = new MemberCache (ifaces);
381 builder_to_member_cache.Add (t, cache);
385 public static TypeContainer LookupInterface (Type t)
387 TypeContainer tc = LookupTypeContainer (t);
388 if ((tc == null) || (tc.Kind != Kind.Interface))
394 public static Delegate LookupDelegate (Type t)
396 return LookupDeclSpace (t) as Delegate;
399 public static Class LookupClass (Type t)
401 return LookupDeclSpace (t) as Class;
405 // We use this hash for multiple kinds of constructed types:
407 // (T, "&") Given T, get T &
408 // (T, "*") Given T, get T *
409 // (T, "[]") Given T and a array dimension, get T []
410 // (T, X) Given a type T and a simple name X, get the type T+X
412 // Accessibility tests, if necessary, should be done by the user
414 static DoubleHash type_hash = new DoubleHash ();
417 // Gets the reference to T version of the Type (T&)
419 public static Type GetReferenceType (Type t)
421 return t.MakeByRefType ();
425 // Gets the pointer to T version of the Type (T*)
427 public static Type GetPointerType (Type t)
429 return GetConstructedType (t, "*");
432 public static Type GetConstructedType (Type t, string dim)
435 if (type_hash.Lookup (t, dim, out ret))
438 if (IsDynamicType (t)) {
439 ret = new DynamicArrayType (dim.Length - 1);
440 type_hash.Insert (t, dim, ret);
444 ret = t.Module.GetType (t.ToString () + dim);
446 type_hash.Insert (t, dim, ret);
451 ret = GetReferenceType (t);
452 type_hash.Insert (t, dim, ret);
456 if (t.IsGenericParameter || t.IsGenericType) {
459 while ((pos < dim.Length) && (dim [pos] == '[')) {
462 if (dim [pos] == ']') {
463 result = result.MakeArrayType ();
466 if (pos < dim.Length)
469 type_hash.Insert (t, dim, result);
474 while (dim [pos] == ',') {
478 if ((dim [pos] != ']') || (pos != dim.Length-1))
481 result = result.MakeArrayType (rank + 1);
482 type_hash.Insert (t, dim, result);
487 type_hash.Insert (t, dim, null);
491 public static Type GetNestedType (Type t, string name)
494 if (!type_hash.Lookup (t, name, out ret)) {
495 ret = t.GetNestedType (name,
496 BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
497 type_hash.Insert (t, name, ret);
503 /// Fills static table with exported types from all referenced assemblies.
504 /// This information is required for CLS Compliance tests.
506 public static void LoadAllImportedTypes ()
508 AllClsTopLevelTypes = new Dictionary<string, object> (1500);
509 foreach (Assembly a in GlobalRootNamespace.Instance.Assemblies) {
510 foreach (Type t in a.GetExportedTypes ()) {
511 AllClsTopLevelTypes [t.FullName.ToLower (System.Globalization.CultureInfo.InvariantCulture)] = null;
517 /// Returns the C# name of a type if possible, or the full type name otherwise
519 static public string CSharpName (Type t)
524 if (t == typeof (ArglistAccess))
527 if (t == typeof (AnonymousMethodBody))
528 return "anonymous method";
530 if (t == typeof (MethodGroupExpr))
531 return "method group";
533 if (IsDynamicType (t))
537 return "internal error";
539 return CSharpName (GetFullName (t), t);
542 static readonly char [] elements = new char [] { '*', '[' };
544 public static string CSharpName (string name, Type type)
546 if (name.Length > 10) {
549 case "System.Int32": s = "int"; break;
550 case "System.Int64": s = "long"; break;
551 case "System.String": s = "string"; break;
552 case "System.Boolean": s = "bool"; break;
553 case "System.Void": s = "void"; break;
554 case "System.Object": s = "object"; break;
555 case "System.UInt32": s = "uint"; break;
556 case "System.Int16": s = "short"; break;
557 case "System.UInt16": s = "ushort"; break;
558 case "System.UInt64": s = "ulong"; break;
559 case "System.Single": s = "float"; break;
560 case "System.Double": s = "double"; break;
561 case "System.Decimal": s = "decimal"; break;
562 case "System.Char": s = "char"; break;
563 case "System.Byte": s = "byte"; break;
564 case "System.SByte": s = "sbyte"; break;
565 default: s = null; break;
570 // Predefined names can come from mscorlib only
572 if (type == null || type.Module.Name == "mscorlib.dll" || !RootContext.StdLib)
578 if (name [0] == AnonymousTypeClass.ClassNamePrefix [0] && name.StartsWith (AnonymousTypeClass.ClassNamePrefix))
579 return AnonymousTypeClass.SignatureForError;
581 int idx = name.IndexOfAny (elements, 10);
583 return CSharpName (name.Substring (0, idx), type) + name.Substring (idx);
586 return name.Replace ('+', '.');
589 static public string CSharpName (Type[] types)
591 if (types.Length == 0)
594 StringBuilder sb = new StringBuilder ();
595 for (int i = 0; i < types.Length; ++i) {
599 sb.Append (CSharpName (types [i]));
601 return sb.ToString ();
605 /// Returns the signature of the method with full namespace classification
607 static public string GetFullNameSignature (MemberInfo mi)
609 PropertyInfo pi = mi as PropertyInfo;
611 MethodBase pmi = pi.GetGetMethod (true);
613 pmi = pi.GetSetMethod (true);
614 if (GetParameterData (pmi).Count > 0)
617 return (mi is MethodBase)
618 ? CSharpSignature (mi as MethodBase)
619 : CSharpName (mi.DeclaringType) + '.' + mi.Name;
622 private static int GetFullName (Type t, StringBuilder sb)
626 if (!t.IsGenericType) {
627 sb.Append (t.FullName);
631 if (t.DeclaringType != null) {
632 pos = GetFullName (t.DeclaringType, sb);
634 } else if (t.Namespace != null && t.Namespace.Length != 0) {
635 sb.Append (t.Namespace);
638 sb.Append (RemoveGenericArity (t.Name));
640 Type[] this_args = GetTypeArguments (t);
642 if (this_args.Length < pos)
643 throw new InternalErrorException (
644 "Enclosing class " + t.DeclaringType + " has more type arguments than " + t);
645 if (this_args.Length == pos)
650 sb.Append (CSharpName (this_args [pos++]));
651 if (pos == this_args.Length)
659 static string GetFullName (Type t)
662 string dimension = t.Name.Substring (t.Name.LastIndexOf ('['));
663 return GetFullName (GetElementType (t)) + dimension;
666 if (IsNullableType (t) && !t.IsGenericTypeDefinition) {
667 t = TypeToCoreType (GetTypeArguments (t)[0]);
668 return CSharpName (t) + "?";
671 if (t.IsGenericParameter)
673 if (!t.IsGenericType)
676 StringBuilder sb = new StringBuilder ();
677 int pos = GetFullName (t, sb);
679 throw new InternalErrorException ("Generic Type " + t + " doesn't have type arguments");
680 return sb.ToString ();
683 public static string RemoveGenericArity (string from)
685 int i = from.IndexOf ('`');
687 return from.Substring (0, i);
692 /// When we need to report accessors as well
694 static public string CSharpSignature (MethodBase mb)
696 return CSharpSignature (mb, false);
699 static public string CSharpSignature (MethodSpec ms)
701 return CSharpSignature (ms.MetaInfo);
705 /// Returns the signature of the method
707 static public string CSharpSignature (MethodBase mb, bool show_accessor)
709 StringBuilder sig = new StringBuilder (CSharpName (mb.DeclaringType));
712 AParametersCollection iparams = GetParameterData (mb);
713 string parameters = iparams.GetSignatureForError ();
714 int accessor_end = 0;
716 if (!mb.IsConstructor && TypeManager.IsSpecialMethod (mb)) {
717 string op_name = Operator.GetName (mb.Name);
718 if (op_name != null) {
719 if (op_name == "explicit" || op_name == "implicit") {
720 sig.Append (op_name);
721 sig.Append (" operator ");
722 sig.Append (CSharpName (((MethodInfo)mb).ReturnType));
724 sig.Append ("operator ");
725 sig.Append (op_name);
727 sig.Append (parameters);
728 return sig.ToString ();
731 bool is_getter = mb.Name.StartsWith ("get_");
732 bool is_setter = mb.Name.StartsWith ("set_");
733 if (is_getter || is_setter || mb.Name.StartsWith ("add_")) {
735 } else if (mb.Name.StartsWith ("remove_")) {
740 if (iparams.Count > (is_getter ? 0 : 1)) {
741 sig.Append ("this[");
743 sig.Append (parameters.Substring (1, parameters.Length - 2));
745 sig.Append (parameters.Substring (1, parameters.LastIndexOf (',') - 1));
748 sig.Append (mb.Name.Substring (accessor_end + 1));
751 if (mb.Name == ".ctor")
752 sig.Append (RemoveGenericArity (mb.DeclaringType.Name));
754 sig.Append (mb.Name);
756 if (IsGenericMethod (mb)) {
757 Type[] args = GetGenericArguments (mb);
759 for (int i = 0; i < args.Length; i++) {
762 sig.Append (CSharpName (args [i]));
768 sig.Append (parameters);
771 if (show_accessor && accessor_end > 0) {
773 sig.Append (mb.Name.Substring (0, accessor_end));
776 return sig.ToString ();
779 public static string GetMethodName (MethodInfo m)
781 if (!IsGenericMethodDefinition (m) && !IsGenericMethod (m))
784 return MemberName.MakeName (m.Name, TypeManager.GetGenericArguments (m).Length);
787 static public string CSharpSignature (EventInfo ei)
789 return CSharpName (ei.DeclaringType) + "." + ei.Name;
793 // Looks up a type, and aborts if it is not found. This is used
794 // by predefined types required by the compiler
796 public static Type CoreLookupType (CompilerContext ctx, string ns_name, string name, Kind type_kind, bool required)
798 Namespace ns = GlobalRootNamespace.Instance.GetNamespace (ns_name, true);
799 Expression expr = ns.Lookup (ctx, name, Location.Null);
803 ctx.Report.Error (518, "The predefined type `{0}.{1}' is not defined or imported",
810 if (RootContext.StdLib || t == null || !required)
813 // TODO: All predefined imported types have to have correct signature
814 if (!IsBeingCompiled (t))
817 DeclSpace ds = (DeclSpace)RootContext.ToplevelTypes.GetDefinition (t.FullName);
818 if (ds is Delegate) {
819 if (type_kind == Kind.Delegate)
822 TypeContainer tc = (TypeContainer)ds;
823 if (tc.Kind == type_kind)
827 ctx.Report.Error (520, ds.Location, "The predefined type `{0}.{1}' is not declared correctly",
832 static MemberInfo GetPredefinedMember (Type t, string name, MemberTypes mt, Location loc, params Type [] args)
834 const BindingFlags flags = instance_and_static | BindingFlags.Public | BindingFlags.DeclaredOnly;
836 MemberInfo [] members = MemberLookup (null, null, t, mt, flags, name, null);
837 if (members != null) {
838 for (int i = 0; i < members.Length; ++i) {
839 MemberInfo member = members [i];
840 if (mt == MemberTypes.Method || mt == MemberTypes.Constructor) {
841 MethodBase mb = member as MethodBase;
845 AParametersCollection pd = TypeManager.GetParameterData (mb);
846 if (IsEqual (pd.Types, args))
849 if (mt == MemberTypes.Field) {
850 FieldInfo fi = member as FieldInfo;
854 if (args.Length >= 1 && !IsEqual (TypeToCoreType (fi.FieldType), args [0]))
860 if (mt == MemberTypes.Property) {
861 PropertyInfo pi = member as PropertyInfo;
865 if (args.Length >= 1 && !IsEqual (TypeToCoreType (pi.PropertyType), args [0]))
873 string method_args = null;
874 if (mt == MemberTypes.Method || mt == MemberTypes.Constructor)
875 method_args = "(" + TypeManager.CSharpName (args) + ")";
877 RootContext.ToplevelTypes.Compiler.Report.Error (656, loc, "The compiler required member `{0}.{1}{2}' could not be found or is inaccessible",
878 TypeManager.CSharpName (t), name, method_args);
884 // Returns the ConstructorInfo for "args"
886 public static ConstructorInfo GetPredefinedConstructor (Type t, Location loc, params Type [] args)
888 return (ConstructorInfo) GetPredefinedMember (t, ConstructorInfo.ConstructorName, MemberTypes.Constructor, loc, args);
892 // Returns the MethodInfo for a method named `name' defined
893 // in type `t' which takes arguments of types `args'
895 public static MethodSpec GetPredefinedMethod (Type t, string name, Location loc, params Type [] args)
897 var m = GetPredefinedMember (t, name, MemberTypes.Method, loc, args) as MethodBase;
901 return Import.CreateMethod (m);
904 public static FieldInfo GetPredefinedField (Type t, string name, Location loc, params Type [] args)
906 return (FieldInfo) GetPredefinedMember (t, name, MemberTypes.Field, loc, args);
909 public static PropertyInfo GetPredefinedProperty (Type t, string name, Location loc, params Type [] args)
911 return (PropertyInfo) GetPredefinedMember (t, name, MemberTypes.Property, loc, args);
915 /// The types have to be initialized after the initial
916 /// population of the type has happened (for example, to
917 /// bootstrap the corlib.dll
919 public static bool InitCoreTypes (CompilerContext ctx)
921 object_type = CoreLookupType (ctx, "System", "Object", Kind.Class, true);
922 system_object_expr.Type = object_type;
923 value_type = CoreLookupType (ctx, "System", "ValueType", Kind.Class, true);
924 system_valuetype_expr.Type = value_type;
925 attribute_type = CoreLookupType (ctx, "System", "Attribute", Kind.Class, true);
927 int32_type = CoreLookupType (ctx, "System", "Int32", Kind.Struct, true);
928 system_int32_expr.Type = int32_type;
929 int64_type = CoreLookupType (ctx, "System", "Int64", Kind.Struct, true);
930 system_int64_expr.Type = int64_type;
931 uint32_type = CoreLookupType (ctx, "System", "UInt32", Kind.Struct, true);
932 system_uint32_expr.Type = uint32_type;
933 uint64_type = CoreLookupType (ctx, "System", "UInt64", Kind.Struct, true);
934 system_uint64_expr.Type = uint64_type;
935 byte_type = CoreLookupType (ctx, "System", "Byte", Kind.Struct, true);
936 system_byte_expr.Type = byte_type;
937 sbyte_type = CoreLookupType (ctx, "System", "SByte", Kind.Struct, true);
938 system_sbyte_expr.Type = sbyte_type;
939 short_type = CoreLookupType (ctx, "System", "Int16", Kind.Struct, true);
940 system_int16_expr.Type = short_type;
941 ushort_type = CoreLookupType (ctx, "System", "UInt16", Kind.Struct, true);
942 system_uint16_expr.Type = ushort_type;
944 ienumerator_type = CoreLookupType (ctx, "System.Collections", "IEnumerator", Kind.Interface, true);
945 ienumerable_type = CoreLookupType (ctx, "System.Collections", "IEnumerable", Kind.Interface, true);
946 idisposable_type = CoreLookupType (ctx, "System", "IDisposable", Kind.Interface, true);
948 // HACK: DefineType immediately resolves iterators (very wrong)
949 generic_ienumerator_type = CoreLookupType (ctx, "System.Collections.Generic", "IEnumerator`1", Kind.Interface, false);
951 char_type = CoreLookupType (ctx, "System", "Char", Kind.Struct, true);
952 system_char_expr.Type = char_type;
953 string_type = CoreLookupType (ctx, "System", "String", Kind.Class, true);
954 system_string_expr.Type = string_type;
955 float_type = CoreLookupType (ctx, "System", "Single", Kind.Struct, true);
956 system_single_expr.Type = float_type;
957 double_type = CoreLookupType (ctx, "System", "Double", Kind.Struct, true);
958 system_double_expr.Type = double_type;
959 decimal_type = CoreLookupType (ctx, "System", "Decimal", Kind.Struct, true);
960 system_decimal_expr.Type = decimal_type;
961 bool_type = CoreLookupType (ctx, "System", "Boolean", Kind.Struct, true);
962 system_boolean_expr.Type = bool_type;
963 intptr_type = CoreLookupType (ctx, "System", "IntPtr", Kind.Struct, true);
964 system_intptr_expr.Type = intptr_type;
965 uintptr_type = CoreLookupType (ctx, "System", "UIntPtr", Kind.Struct, true);
967 multicast_delegate_type = CoreLookupType (ctx, "System", "MulticastDelegate", Kind.Class, true);
968 delegate_type = CoreLookupType (ctx, "System", "Delegate", Kind.Class, true);
970 enum_type = CoreLookupType (ctx, "System", "Enum", Kind.Class, true);
971 array_type = CoreLookupType (ctx, "System", "Array", Kind.Class, true);
972 void_type = CoreLookupType (ctx, "System", "Void", Kind.Struct, true);
973 system_void_expr.Type = void_type;
974 type_type = CoreLookupType (ctx, "System", "Type", Kind.Class, true);
975 exception_type = CoreLookupType (ctx, "System", "Exception", Kind.Class, true);
977 runtime_field_handle_type = CoreLookupType (ctx, "System", "RuntimeFieldHandle", Kind.Struct, true);
978 runtime_handle_type = CoreLookupType (ctx, "System", "RuntimeTypeHandle", Kind.Struct, true);
980 PredefinedAttributes.Get.ParamArray.Initialize (ctx, false);
981 PredefinedAttributes.Get.Out.Initialize (ctx, false);
983 return ctx.Report.Errors == 0;
987 // Initializes optional core types
989 public static void InitOptionalCoreTypes (CompilerContext ctx)
992 // These are only used for compare purposes
994 null_type = typeof (NullLiteral);
996 void_ptr_type = GetPointerType (void_type);
999 // Initialize InternalsVisibleTo as the very first optional type. Otherwise we would populate
1000 // types cache with incorrect accessiblity when any of optional types is internal.
1002 PredefinedAttributes.Get.Initialize (ctx);
1004 runtime_argument_handle_type = CoreLookupType (ctx, "System", "RuntimeArgumentHandle", Kind.Struct, false);
1005 asynccallback_type = CoreLookupType (ctx, "System", "AsyncCallback", Kind.Delegate, false);
1006 iasyncresult_type = CoreLookupType (ctx, "System", "IAsyncResult", Kind.Interface, false);
1007 typed_reference_type = CoreLookupType (ctx, "System", "TypedReference", Kind.Struct, false);
1008 arg_iterator_type = CoreLookupType (ctx, "System", "ArgIterator", Kind.Struct, false);
1009 mbr_type = CoreLookupType (ctx, "System", "MarshalByRefObject", Kind.Class, false);
1012 // Optional attributes, used for error reporting only
1014 //if (PredefinedAttributes.Get.Obsolete.IsDefined) {
1015 // Class c = TypeManager.LookupClass (PredefinedAttributes.Get.Obsolete.Type);
1020 generic_ilist_type = CoreLookupType (ctx, "System.Collections.Generic", "IList`1", Kind.Interface, false);
1021 generic_icollection_type = CoreLookupType (ctx, "System.Collections.Generic", "ICollection`1", Kind.Interface, false);
1022 generic_ienumerable_type = CoreLookupType (ctx, "System.Collections.Generic", "IEnumerable`1", Kind.Interface, false);
1023 generic_nullable_type = CoreLookupType (ctx, "System", "Nullable`1", Kind.Struct, false);
1026 // Optional types which are used as types and for member lookup
1028 runtime_helpers_type = CoreLookupType (ctx, "System.Runtime.CompilerServices", "RuntimeHelpers", Kind.Class, false);
1031 // Note: extension_attribute_type is already loaded
1032 expression_type = CoreLookupType (ctx, "System.Linq.Expressions", "Expression`1", Kind.Class, false);
1034 if (!RootContext.StdLib) {
1036 // HACK: When building Mono corlib mcs uses loaded mscorlib which
1037 // has different predefined types and this method sets mscorlib types
1038 // to be same to avoid any type check errors.
1041 Type type = typeof (Type);
1042 Type [] system_4_type_arg = { type, type, type, type };
1044 MethodInfo set_corlib_type_builders =
1045 typeof (System.Reflection.Emit.AssemblyBuilder).GetMethod (
1046 "SetCorlibTypeBuilders", BindingFlags.NonPublic | BindingFlags.Instance, null,
1047 system_4_type_arg, null);
1049 if (set_corlib_type_builders != null) {
1050 object[] args = new object [4];
1051 args [0] = object_type;
1052 args [1] = value_type;
1053 args [2] = enum_type;
1054 args [3] = void_type;
1056 set_corlib_type_builders.Invoke (CodeGen.Assembly.Builder, args);
1058 ctx.Report.Warning (-26, 3, "The compilation may fail due to missing `{0}.SetCorlibTypeBuilders({1})' method",
1059 TypeManager.CSharpName (typeof (System.Reflection.Emit.AssemblyBuilder)),
1060 TypeManager.CSharpName (system_4_type_arg));
1065 const BindingFlags instance_and_static = BindingFlags.Static | BindingFlags.Instance;
1068 /// This is the "old", non-cache based FindMembers() function. We cannot use
1069 /// the cache here because there is no member name argument.
1071 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1072 MemberFilter filter, object criteria)
1075 if (t.IsGenericType)
1076 t = t.GetGenericTypeDefinition ();
1079 DeclSpace decl = LookupDeclSpace (t);
1082 // `builder_to_declspace' contains all dynamic types.
1086 Timer.StartTimer (TimerType.FindMembers);
1087 list = decl.FindMembers (mt, bf, filter, criteria);
1088 Timer.StopTimer (TimerType.FindMembers);
1093 // We have to take care of arrays specially, because GetType on
1094 // a TypeBuilder array will return a Type, not a TypeBuilder,
1095 // and we can not call FindMembers on this type.
1101 t.IsSubclassOf (TypeManager.array_type))
1102 return new MemberList (TypeManager.array_type.FindMembers (mt, bf, filter, criteria));
1104 if (t is GenericTypeParameterBuilder) {
1105 TypeParameter tparam = builder_to_type_param [(GenericTypeParameterBuilder) t];
1107 Timer.StartTimer (TimerType.FindMembers);
1108 MemberList list = tparam.FindMembers (
1109 mt, bf | BindingFlags.DeclaredOnly, filter, criteria);
1110 Timer.StopTimer (TimerType.FindMembers);
1115 // Since FindMembers will not lookup both static and instance
1116 // members, we emulate this behaviour here.
1118 if ((bf & instance_and_static) == instance_and_static){
1119 MemberInfo [] i_members = t.FindMembers (
1120 mt, bf & ~BindingFlags.Static, filter, criteria);
1122 int i_len = i_members.Length;
1124 MemberInfo one = i_members [0];
1127 // If any of these are present, we are done!
1129 if ((one is Type) || (one is EventInfo) || (one is FieldInfo))
1130 return new MemberList (i_members);
1133 MemberInfo [] s_members = t.FindMembers (
1134 mt, bf & ~BindingFlags.Instance, filter, criteria);
1136 int s_len = s_members.Length;
1137 if (i_len > 0 || s_len > 0)
1138 return new MemberList (i_members, s_members);
1141 return new MemberList (i_members);
1143 return new MemberList (s_members);
1147 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
1152 /// This method is only called from within MemberLookup. It tries to use the member
1153 /// cache if possible and falls back to the normal FindMembers if not. The `used_cache'
1154 /// flag tells the caller whether we used the cache or not. If we used the cache, then
1155 /// our return value will already contain all inherited members and the caller don't need
1156 /// to check base classes and interfaces anymore.
1158 private static MemberInfo [] MemberLookup_FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1159 string name, out bool used_cache)
1164 // If this is a dynamic type, it's always in the `builder_to_declspace' hash table
1165 // and we can ask the DeclSpace for the MemberCache.
1168 if (t.Assembly == CodeGen.Assembly.Builder) {
1169 if (t.IsGenericParameter) {
1170 TypeParameter tparam = builder_to_type_param [(GenericTypeParameterBuilder) t];
1173 if (tparam.MemberCache == null)
1174 return new MemberInfo[0];
1176 return tparam.MemberCache.FindMembers (
1177 mt, bf, name, FilterWithClosure_delegate, null);
1181 // We have to take care of arrays specially, because GetType on
1182 // a TypeBuilder array will return a Type, not a TypeBuilder,
1183 // and we can not call FindMembers on this type.
1187 return TypeHandle.ArrayType.MemberCache.FindMembers (
1188 mt, bf, name, FilterWithClosure_delegate, null);
1191 if (t.IsGenericType && !t.IsGenericTypeDefinition)
1192 t = t.GetGenericTypeDefinition ();
1194 if (t is TypeBuilder) {
1196 DeclSpace decl = LookupDeclSpace (t);
1197 cache = decl.MemberCache;
1200 // If this DeclSpace has a MemberCache, use it.
1203 if (cache != null) {
1205 return cache.FindMembers (
1206 mt, bf, name, FilterWithClosure_delegate, null);
1209 // If there is no MemberCache, we need to use the "normal" FindMembers.
1210 // Note, this is a VERY uncommon route!
1213 Timer.StartTimer (TimerType.FindMembers);
1214 list = decl.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
1215 FilterWithClosure_delegate, name);
1216 Timer.StopTimer (TimerType.FindMembers);
1218 return (MemberInfo []) list;
1222 // We have to take care of arrays specially, because GetType on
1223 // a TypeBuilder array will return a Type, not a TypeBuilder,
1224 // and we can not call FindMembers on this type.
1228 return TypeHandle.ArrayType.MemberCache.FindMembers (
1229 mt, bf, name, FilterWithClosure_delegate, null);
1232 if (t is GenericTypeParameterBuilder) {
1233 TypeParameter tparam = builder_to_type_param [(GenericTypeParameterBuilder) t];
1236 if (tparam.MemberCache == null)
1237 return new MemberInfo [0];
1239 return tparam.MemberCache.FindMembers (
1240 mt, bf, name, FilterWithClosure_delegate, null);
1243 if (IsGenericType (t) && (mt == MemberTypes.NestedType)) {
1245 // This happens if we're resolving a class'es base class and interfaces
1246 // in TypeContainer.DefineType(). At this time, the types aren't
1247 // populated yet, so we can't use the cache.
1249 MemberInfo[] info = t.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
1250 FilterWithClosure_delegate, name);
1256 // This call will always succeed. There is exactly one TypeHandle instance per
1257 // type, TypeHandle.GetMemberCache() will, if necessary, create a new one, and return
1258 // the corresponding MemberCache.
1260 cache = TypeHandle.GetMemberCache (t);
1263 return cache.FindMembers (mt, bf, name, FilterWithClosure_delegate, null);
1267 // Return true for SRE dynamic/unclosed members
1269 public static bool IsBeingCompiled (MemberInfo mi)
1271 return mi.Module.Assembly == CodeGen.Assembly.Builder;
1274 public static bool IsBuiltinType (Type t)
1276 t = TypeToCoreType (t);
1277 if (t == object_type || t == string_type || t == int32_type || t == uint32_type ||
1278 t == int64_type || t == uint64_type || t == float_type || t == double_type ||
1279 t == char_type || t == short_type || t == decimal_type || t == bool_type ||
1280 t == sbyte_type || t == byte_type || t == ushort_type || t == void_type)
1287 // This is like IsBuiltinType, but lacks decimal_type, we should also clean up
1288 // the pieces in the code where we use IsBuiltinType and special case decimal_type.
1290 public static bool IsPrimitiveType (Type t)
1292 return (t == int32_type || t == uint32_type ||
1293 t == int64_type || t == uint64_type || t == float_type || t == double_type ||
1294 t == char_type || t == short_type || t == bool_type ||
1295 t == sbyte_type || t == byte_type || t == ushort_type);
1298 public static bool IsDelegateType (Type t)
1300 if (TypeManager.IsGenericParameter (t))
1303 if (t == TypeManager.delegate_type || t == TypeManager.multicast_delegate_type)
1306 t = DropGenericTypeArguments (t);
1307 return IsSubclassOf (t, TypeManager.delegate_type);
1311 // Is a type of dynamic type
1313 public static bool IsDynamicType (Type t)
1315 if (object.ReferenceEquals (InternalType.Dynamic, t))
1318 if (t != object_type)
1321 if (IsBeingCompiled (t))
1324 PredefinedAttribute pa = PredefinedAttributes.Get.Dynamic;
1328 object[] res = t.GetCustomAttributes (pa.Type, false);
1329 return res != null && res.Length != 0;
1333 // When any element of the type is a dynamic type
1335 // This method builds a transformation array for dynamic types
1336 // used in places where DynamicAttribute cannnot be applied to.
1337 // It uses bool flag when type is of dynamic type and each
1338 // section always starts with "false" for some reason.
1340 // LAMESPEC: This should be part of C# specification !
1342 // Example: Func<dynamic, int, dynamic[]>
1343 // Transformation: { false, true, false, false, true }
1345 public static bool[] HasDynamicTypeUsed (Type t)
1347 if (t is DynamicArrayType)
1348 return new bool[] { false, true };
1353 if (IsGenericType (t)) {
1354 List<bool> transform = null;
1355 var targs = GetTypeArguments (t);
1356 for (int i = 0; i < targs.Length; ++i) {
1357 var element = HasDynamicTypeUsed (targs [i]);
1358 if (element != null) {
1359 if (transform == null) {
1360 transform = new List<bool> ();
1361 for (int ii = 0; ii <= i; ++ii)
1362 transform.Add (false);
1365 transform.AddRange (element);
1366 } else if (transform != null) {
1367 transform.Add (false);
1371 if (transform != null)
1372 return transform.ToArray ();
1375 if (object.ReferenceEquals (InternalType.Dynamic, t))
1376 return new bool [] { true };
1381 public static bool IsEnumType (Type t)
1383 t = DropGenericTypeArguments (t);
1384 return t.BaseType == TypeManager.enum_type;
1387 public static bool IsBuiltinOrEnum (Type t)
1389 if (IsBuiltinType (t))
1398 public static bool IsAttributeType (Type t)
1400 return t == attribute_type && t.BaseType != null || IsSubclassOf (t, attribute_type);
1404 // Whether a type is unmanaged. This is used by the unsafe code (25.2)
1406 // mcs4: delete, DeclSpace.IsUnmanagedType is replacement
1407 public static bool IsUnmanagedType (Type t)
1409 DeclSpace ds = TypeManager.LookupDeclSpace (t);
1411 return ds.IsUnmanagedType ();
1413 // builtins that are not unmanaged types
1414 if (t == TypeManager.object_type || t == TypeManager.string_type)
1417 if (IsGenericType (t) || IsGenericParameter (t))
1420 if (IsBuiltinOrEnum (t))
1423 // Someone did the work of checking if the ElementType of t is unmanaged. Let's not repeat it.
1425 return IsUnmanagedType (GetElementType (t));
1427 // Arrays are disallowed, even if we mark them with [MarshalAs(UnmanagedType.ByValArray, ...)]
1431 if (!IsValueType (t))
1434 for (Type p = t.DeclaringType; p != null; p = p.DeclaringType) {
1435 if (IsGenericTypeDefinition (p))
1441 FieldInfo [] fields = t.GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
1443 foreach (FieldInfo f in fields){
1444 if (!IsUnmanagedType (f.FieldType)){
1454 // Null is considered to be a reference type
1456 public static bool IsReferenceType (Type t)
1458 if (TypeManager.IsGenericParameter (t)) {
1459 GenericConstraints constraints = TypeManager.GetTypeParameterConstraints (t);
1460 if (constraints == null)
1463 return constraints.IsReferenceType;
1466 return !IsStruct (t) && !IsEnumType (t);
1469 public static bool IsValueType (Type t)
1471 if (TypeManager.IsGenericParameter (t)) {
1472 GenericConstraints constraints = TypeManager.GetTypeParameterConstraints (t);
1473 if (constraints == null)
1476 return constraints.IsValueType;
1479 return IsStruct (t) || IsEnumType (t);
1482 public static bool IsStruct (Type t)
1484 return t.BaseType == value_type && t != enum_type && t.IsSealed;
1487 public static bool IsInterfaceType (Type t)
1489 TypeContainer tc = LookupTypeContainer (t);
1493 return tc.Kind == Kind.Interface;
1496 public static bool IsSubclassOf (Type type, Type base_type)
1498 TypeParameter tparam = LookupTypeParameter (type);
1499 TypeParameter pparam = LookupTypeParameter (base_type);
1501 if ((tparam != null) && (pparam != null)) {
1502 if (tparam == pparam)
1505 return tparam.IsSubclassOf (base_type);
1509 if (tparam != pparam)
1512 if (type.IsGenericType)
1513 type = type.GetGenericTypeDefinition ();
1516 if (type.IsSubclassOf (base_type))
1520 if (IsEqual (type, base_type))
1523 type = type.BaseType;
1524 } while (type != null);
1529 public static bool IsPrivateAccessible (Type type, Type parent)
1534 if (type.Equals (parent))
1537 return DropGenericTypeArguments (type) == DropGenericTypeArguments (parent);
1540 public static bool IsFamilyAccessible (Type type, Type parent)
1542 TypeParameter tparam = LookupTypeParameter (type);
1543 TypeParameter pparam = LookupTypeParameter (parent);
1545 if ((tparam != null) && (pparam != null)) {
1546 if (tparam == pparam)
1549 return tparam.IsSubclassOf (parent);
1553 if (IsInstantiationOfSameGenericType (type, parent))
1556 type = type.BaseType;
1557 } while (type != null);
1563 // Checks whether `type' is a subclass or nested child of `base_type'.
1565 public static bool IsNestedFamilyAccessible (Type type, Type base_type)
1568 if (IsFamilyAccessible (type, base_type))
1571 // Handle nested types.
1572 type = type.DeclaringType;
1573 } while (type != null);
1579 // Checks whether `type' is a nested child of `parent'.
1581 public static bool IsNestedChildOf (Type type, Type parent)
1586 type = DropGenericTypeArguments (type);
1587 parent = DropGenericTypeArguments (parent);
1589 if (IsEqual (type, parent))
1592 type = type.DeclaringType;
1593 while (type != null) {
1594 if (IsEqual (type, parent))
1597 type = type.DeclaringType;
1603 public static bool IsSpecialType (Type t)
1605 return t == arg_iterator_type || t == typed_reference_type;
1609 // Checks whether `invocationAssembly' is same or a friend of the assembly
1611 public static bool IsThisOrFriendAssembly (Assembly invocationAssembly, Assembly assembly)
1613 if (assembly == null)
1614 throw new ArgumentNullException ("assembly");
1616 // TODO: This can happen for constants used at assembly level and
1617 // predefined members
1618 // But there is no way to test for it for now, so it could be abused
1620 if (invocationAssembly == null)
1621 invocationAssembly = CodeGen.Assembly.Builder;
1623 if (invocationAssembly == assembly)
1627 if (assembly_internals_vis_attrs.TryGetValue (assembly, out value))
1630 PredefinedAttribute pa = PredefinedAttributes.Get.InternalsVisibleTo;
1631 // HACK: Do very early resolve of SRE type checking
1632 if (pa.Type == null)
1638 object [] attrs = assembly.GetCustomAttributes (pa.Type, false);
1639 if (attrs.Length == 0) {
1640 assembly_internals_vis_attrs.Add (assembly, false);
1644 bool is_friend = false;
1646 AssemblyName this_name = CodeGen.Assembly.Name;
1647 byte [] this_token = this_name.GetPublicKeyToken ();
1648 foreach (InternalsVisibleToAttribute attr in attrs) {
1649 if (attr.AssemblyName == null || attr.AssemblyName.Length == 0)
1652 AssemblyName aname = null;
1654 aname = new AssemblyName (attr.AssemblyName);
1655 } catch (FileLoadException) {
1656 } catch (ArgumentException) {
1659 if (aname == null || aname.Name != this_name.Name)
1662 byte [] key_token = aname.GetPublicKeyToken ();
1663 if (key_token != null) {
1664 if (this_token.Length == 0) {
1665 // Same name, but assembly is not strongnamed
1666 Error_FriendAccessNameNotMatching (aname.FullName, RootContext.ToplevelTypes.Compiler.Report);
1670 if (!CompareKeyTokens (this_token, key_token))
1678 assembly_internals_vis_attrs.Add (assembly, is_friend);
1682 static bool CompareKeyTokens (byte [] token1, byte [] token2)
1684 for (int i = 0; i < token1.Length; i++)
1685 if (token1 [i] != token2 [i])
1691 static void Error_FriendAccessNameNotMatching (string other_name, Report Report)
1694 "Friend access was granted to `{0}', but the output assembly is named `{1}'. Try adding a reference to `{0}' or change the output assembly name to match it",
1695 other_name, CodeGen.Assembly.Name.FullName);
1699 // Do the right thing when returning the element type of an
1700 // array type based on whether we are compiling corlib or not
1702 public static Type GetElementType (Type t)
1704 if (RootContext.StdLib)
1705 return t.GetElementType ();
1707 return TypeToCoreType (t.GetElementType ());
1711 /// This method is not implemented by MS runtime for dynamic types
1713 public static bool HasElementType (Type t)
1715 return t.IsArray || t.IsPointer || t.IsByRef;
1718 public static Type GetEnumUnderlyingType (Type t)
1720 t = DropGenericTypeArguments (t);
1721 Enum e = LookupTypeContainer (t) as Enum;
1723 return e.UnderlyingType;
1726 FieldInfo fi = GetPredefinedField (t, Enum.UnderlyingValueField, Location.Null, Type.EmptyTypes);
1728 return TypeManager.int32_type;
1730 return TypeToCoreType (fi.FieldType);
1734 /// Gigantic work around for missing features in System.Reflection.Emit follows.
1738 /// Since System.Reflection.Emit can not return MethodBase.GetParameters
1739 /// for anything which is dynamic, and we need this in a number of places,
1740 /// we register this information here, and use it afterwards.
1742 static public void RegisterMethod (MethodBase mb, AParametersCollection ip)
1744 method_params.Add (mb, ip);
1747 static public void RegisterIndexer (PropertyBuilder pb, AParametersCollection p)
1749 method_params.Add (pb, p);
1752 static public AParametersCollection GetParameterData (MethodBase mb)
1754 AParametersCollection pd;
1755 if (!method_params.TryGetValue (mb, out pd)) {
1757 if (mb.IsGenericMethod && !mb.IsGenericMethodDefinition) {
1758 MethodInfo mi = ((MethodInfo) mb).GetGenericMethodDefinition ();
1759 pd = GetParameterData (mi);
1761 if (mi.IsGenericMethod)
1762 pd = pd.InflateTypes (mi.GetGenericArguments (), mb.GetGenericArguments ());
1764 pd = pd.InflateTypes (mi.DeclaringType.GetGenericArguments (), mb.GetGenericArguments ());
1766 method_params.Add (mb, pd);
1770 if (mb.DeclaringType.Assembly == CodeGen.Assembly.Builder) {
1771 throw new InternalErrorException ("Parameters are not registered for method `{0}'",
1772 TypeManager.CSharpName (mb.DeclaringType) + "." + mb.Name);
1775 pd = ParametersImported.Create (mb);
1777 MethodBase generic = TypeManager.DropGenericMethodArguments (mb);
1778 if (generic != mb) {
1779 pd = TypeManager.GetParameterData (generic);
1780 pd = ParametersImported.Create (pd, mb);
1782 pd = ParametersImported.Create (mb);
1785 method_params.Add (mb, pd);
1790 public static AParametersCollection GetParameterData (PropertyInfo pi)
1792 AParametersCollection pd;
1793 if (!method_params.TryGetValue (pi, out pd)) {
1794 if (pi is PropertyBuilder)
1795 return ParametersCompiled.EmptyReadOnlyParameters;
1797 ParameterInfo [] p = pi.GetIndexParameters ();
1799 return ParametersCompiled.EmptyReadOnlyParameters;
1801 pd = ParametersImported.Create (p, null);
1802 method_params.Add (pi, pd);
1808 public static AParametersCollection GetDelegateParameters (ResolveContext ec, Type t)
1810 Delegate d = LookupDelegate (t);
1812 return d.Parameters;
1814 var invoke_mb = Delegate.GetInvokeMethod (ec.Compiler, t, t);
1815 return invoke_mb.Parameters;
1818 static public void RegisterOverride (MethodBase override_method, MethodBase base_method)
1820 if (!method_overrides.ContainsKey (override_method))
1821 method_overrides [override_method] = base_method;
1822 if (method_overrides [override_method] != base_method)
1823 throw new InternalErrorException ("Override mismatch: " + override_method);
1826 static public bool IsOverride (MethodSpec ms)
1828 MethodBase m = ms.MetaInfo;
1829 m = DropGenericMethodArguments (m);
1831 return m.IsVirtual &&
1832 (m.Attributes & MethodAttributes.NewSlot) == 0 &&
1833 (m is MethodBuilder || method_overrides.ContainsKey (m));
1836 static public MethodBase TryGetBaseDefinition (MethodBase m)
1838 m = DropGenericMethodArguments (m);
1840 if (method_overrides.TryGetValue (m, out mb))
1846 public static void RegisterConstant (FieldInfo fb, ConstSpec ic)
1848 fields.Add (fb, ic);
1851 public static ConstSpec GetConstant (FieldInfo fb)
1857 if (fields.TryGetValue (fb, out ic))
1863 public static void RegisterProperty (PropertyInfo pi, PropertyBase pb)
1865 propertybuilder_to_property.Add (pi, pb);
1868 public static PropertyBase GetProperty (PropertyInfo pi)
1871 if (propertybuilder_to_property.TryGetValue (pi, out pb))
1877 static public void RegisterFieldBase (FieldBuilder fb, FieldBase f)
1879 fieldbuilders_to_fields.Add (fb, f);
1883 // The return value can be null; This will be the case for
1884 // auxiliary FieldBuilders created by the compiler that have no
1885 // real field being declared on the source code
1887 static public FieldBase GetField (FieldInfo fb)
1889 return GetFieldCore (GetGenericFieldDefinition (fb));
1892 static public FieldBase GetFieldCore (FieldInfo fb)
1895 if (fieldbuilders_to_fields.TryGetValue (fb, out f))
1901 static public MethodInfo GetAddMethod (EventInfo ei)
1903 if (ei is MyEventBuilder) {
1904 return ((MyEventBuilder)ei).GetAddMethod (true);
1906 return ei.GetAddMethod (true);
1909 static public MethodInfo GetRemoveMethod (EventInfo ei)
1911 if (ei is MyEventBuilder) {
1912 return ((MyEventBuilder)ei).GetRemoveMethod (true);
1914 return ei.GetRemoveMethod (true);
1917 static public void RegisterEventField (EventInfo einfo, EventField e)
1920 events = new Dictionary<EventInfo, EventField> (ReferenceEquality<EventInfo>.Default);
1922 events.Add (einfo, e);
1925 static public EventField GetEventField (EventInfo ei)
1931 if (events.TryGetValue (ei, out value))
1937 public static bool CheckStructCycles (TypeContainer tc, Dictionary<TypeContainer, object> seen)
1939 var hash = new Dictionary<TypeContainer, object> ();
1940 return CheckStructCycles (tc, seen, hash);
1943 public static bool CheckStructCycles (TypeContainer tc, Dictionary<TypeContainer, object> seen,
1944 Dictionary<TypeContainer, object> hash)
1946 if ((tc.Kind != Kind.Struct) || IsBuiltinType (tc.TypeBuilder))
1950 // `seen' contains all types we've already visited.
1952 if (seen.ContainsKey (tc))
1954 seen.Add (tc, null);
1956 if (tc.Fields == null)
1959 foreach (FieldBase field in tc.Fields) {
1960 if (field.Spec == null || field.Spec.IsStatic)
1963 Type ftype = field.Spec.FieldType;
1964 TypeContainer ftc = LookupTypeContainer (ftype);
1968 if (hash.ContainsKey (ftc)) {
1969 tc.Compiler.Report.Error (523, tc.Location,
1970 "Struct member `{0}.{1}' of type `{2}' " +
1971 "causes a cycle in the struct layout",
1972 tc.Name, field.Name, ftc.Name);
1977 // `hash' contains all types in the current path.
1979 hash.Add (tc, null);
1981 bool ok = CheckStructCycles (ftc, seen, hash);
1988 if (!seen.ContainsKey (ftc))
1989 seen.Add (ftc, null);
1996 /// Given an array of interface types, expand and eliminate repeated ocurrences
1997 /// of an interface.
2001 /// This expands in context like: IA; IB : IA; IC : IA, IB; the interface "IC" to
2004 public static Type[] ExpandInterfaces (TypeExpr [] base_interfaces)
2006 var new_ifaces = new List<Type> ();
2008 foreach (TypeExpr iface in base_interfaces){
2009 Type itype = iface.Type;
2011 if (new_ifaces.Contains (itype))
2014 new_ifaces.Add (itype);
2016 Type [] implementing = GetInterfaces (itype);
2018 foreach (Type imp in implementing){
2019 if (!new_ifaces.Contains (imp))
2020 new_ifaces.Add (imp);
2024 return new_ifaces.ToArray ();
2027 public static Type[] ExpandInterfaces (Type [] base_interfaces)
2029 var new_ifaces = new List<Type> ();
2031 foreach (Type itype in base_interfaces){
2032 if (new_ifaces.Contains (itype))
2035 new_ifaces.Add (itype);
2037 Type [] implementing = GetInterfaces (itype);
2039 foreach (Type imp in implementing){
2040 if (!new_ifaces.Contains (imp))
2041 new_ifaces.Add (imp);
2045 return new_ifaces.ToArray ();
2049 /// This function returns the interfaces in the type `t'. Works with
2050 /// both types and TypeBuilders.
2052 public static Type [] GetInterfaces (Type t)
2055 if (iface_cache.TryGetValue (t, out cached))
2060 // The reason for catching the Array case is that Reflection.Emit
2061 // will not return a TypeBuilder for Array types of TypeBuilder types,
2062 // but will still throw an exception if we try to call GetInterfaces
2065 // Since the array interfaces are always constant, we return those for
2070 t = TypeManager.array_type;
2072 if ((t is TypeBuilder) || IsGenericType (t)) {
2073 Type [] base_ifaces;
2075 if (t.BaseType == null)
2076 base_ifaces = Type.EmptyTypes;
2078 base_ifaces = GetInterfaces (t.BaseType);
2080 if (IsGenericType (t))
2082 type_ifaces = t.GetGenericTypeDefinition().GetInterfaces ();
2084 type_ifaces = t.GetInterfaces ();
2087 type_ifaces = GetExplicitInterfaces (t);
2088 if (type_ifaces == null || type_ifaces.Length == 0)
2089 type_ifaces = Type.EmptyTypes;
2091 int base_count = base_ifaces.Length;
2092 Type [] result = new Type [base_count + type_ifaces.Length];
2093 base_ifaces.CopyTo (result, 0);
2094 type_ifaces.CopyTo (result, base_count);
2096 iface_cache [t] = result;
2098 } else if (t is GenericTypeParameterBuilder){
2099 Type[] type_ifaces = GetExplicitInterfaces (t);
2100 if (type_ifaces == null || type_ifaces.Length == 0)
2101 type_ifaces = Type.EmptyTypes;
2103 iface_cache [t] = type_ifaces;
2106 Type[] ifaces = t.GetInterfaces ();
2107 iface_cache [t] = ifaces;
2113 // gets the interfaces that are declared explicitly on t
2115 public static Type[] GetExplicitInterfaces (Type t)
2118 if (builder_to_ifaces.TryGetValue (t, out ifaces))
2125 /// The following is used to check if a given type implements an interface.
2126 /// The cache helps us reduce the expense of hitting Type.GetInterfaces everytime.
2128 public static bool ImplementsInterface (Type t, Type iface)
2133 // FIXME OPTIMIZATION:
2134 // as soon as we hit a non-TypeBuiler in the interface
2135 // chain, we could return, as the `Type.GetInterfaces'
2136 // will return all the interfaces implement by the type
2140 interfaces = GetInterfaces (t);
2142 if (interfaces != null){
2143 foreach (Type i in interfaces){
2144 if (i == iface || IsVariantOf (i, iface))
2150 } while (t != null);
2155 static NumberFormatInfo nf_provider = CultureInfo.CurrentCulture.NumberFormat;
2157 // This is a custom version of Convert.ChangeType() which works
2158 // with the TypeBuilder defined types when compiling corlib.
2159 public static object ChangeType (object value, Type conversionType, out bool error)
2161 IConvertible convert_value = value as IConvertible;
2163 if (convert_value == null){
2170 // We must use Type.Equals() here since `conversionType' is
2171 // the TypeBuilder created version of a system type and not
2172 // the system type itself. You cannot use Type.GetTypeCode()
2173 // on such a type - it'd always return TypeCode.Object.
2176 // We cannot rely on build-in type conversions as they are
2177 // more limited than what C# supports.
2178 // See char -> float/decimal/double conversion
2183 if (conversionType.Equals (typeof (Boolean)))
2184 return (object)(convert_value.ToBoolean (nf_provider));
2185 if (conversionType.Equals (typeof (Byte)))
2186 return (object)(convert_value.ToByte (nf_provider));
2187 if (conversionType.Equals (typeof (Char)))
2188 return (object)(convert_value.ToChar (nf_provider));
2189 if (conversionType.Equals (typeof (DateTime)))
2190 return (object)(convert_value.ToDateTime (nf_provider));
2192 if (conversionType.Equals (decimal_type)) {
2193 if (convert_value.GetType () == TypeManager.char_type)
2194 return (decimal)convert_value.ToInt32 (nf_provider);
2195 return convert_value.ToDecimal (nf_provider);
2198 if (conversionType.Equals (typeof (Double))) {
2199 if (convert_value.GetType () == TypeManager.char_type)
2200 return (double)convert_value.ToInt32 (nf_provider);
2201 return convert_value.ToDouble (nf_provider);
2204 if (conversionType.Equals (typeof (Int16)))
2205 return (object)(convert_value.ToInt16 (nf_provider));
2206 if (conversionType.Equals (int32_type))
2207 return (object)(convert_value.ToInt32 (nf_provider));
2208 if (conversionType.Equals (int64_type))
2209 return (object)(convert_value.ToInt64 (nf_provider));
2210 if (conversionType.Equals (typeof (SByte)))
2211 return (object)(convert_value.ToSByte (nf_provider));
2213 if (conversionType.Equals (typeof (Single))) {
2214 if (convert_value.GetType () == TypeManager.char_type)
2215 return (float)convert_value.ToInt32 (nf_provider);
2216 return convert_value.ToSingle (nf_provider);
2219 if (conversionType.Equals (typeof (String)))
2220 return (object)(convert_value.ToString (nf_provider));
2221 if (conversionType.Equals (typeof (UInt16)))
2222 return (object)(convert_value.ToUInt16 (nf_provider));
2223 if (conversionType.Equals (typeof (UInt32)))
2224 return (object)(convert_value.ToUInt32 (nf_provider));
2225 if (conversionType.Equals (typeof (UInt64)))
2226 return (object)(convert_value.ToUInt64 (nf_provider));
2227 if (conversionType.Equals (typeof (Object)))
2228 return (object)(value);
2238 // When compiling with -nostdlib and the type is imported from an external assembly
2239 // SRE uses "wrong" type and we have to convert it to the right compiler instance.
2241 public static Type TypeToCoreType (Type t)
2243 if (RootContext.StdLib || t.Module != typeof (object).Module)
2246 // TODO: GetTypeCode returns underlying type for enums !!
2247 TypeCode tc = Type.GetTypeCode (t);
2250 case TypeCode.Boolean:
2251 return TypeManager.bool_type;
2253 return TypeManager.byte_type;
2254 case TypeCode.SByte:
2255 return TypeManager.sbyte_type;
2257 return TypeManager.char_type;
2258 case TypeCode.Int16:
2259 return TypeManager.short_type;
2260 case TypeCode.UInt16:
2261 return TypeManager.ushort_type;
2262 case TypeCode.Int32:
2263 return TypeManager.int32_type;
2264 case TypeCode.UInt32:
2265 return TypeManager.uint32_type;
2266 case TypeCode.Int64:
2267 return TypeManager.int64_type;
2268 case TypeCode.UInt64:
2269 return TypeManager.uint64_type;
2270 case TypeCode.Single:
2271 return TypeManager.float_type;
2272 case TypeCode.Double:
2273 return TypeManager.double_type;
2274 case TypeCode.String:
2275 return TypeManager.string_type;
2276 case TypeCode.Decimal:
2277 return TypeManager.decimal_type;
2280 if (t == typeof (void))
2281 return TypeManager.void_type;
2282 if (t == typeof (object))
2283 return TypeManager.object_type;
2284 if (t == typeof (System.Type))
2285 return TypeManager.type_type;
2286 if (t == typeof (System.IntPtr))
2287 return TypeManager.intptr_type;
2288 if (t == typeof (System.UIntPtr))
2289 return TypeManager.uintptr_type;
2292 int dim = t.GetArrayRank ();
2293 t = GetElementType (t);
2294 return t.MakeArrayType (dim);
2297 t = GetElementType (t);
2298 return t.MakeByRefType ();
2301 t = GetElementType (t);
2302 return t.MakePointerType ();
2309 // Converts any type to reflection supported type
2311 public static Type TypeToReflectionType (Type type)
2313 // TODO: Very lame and painful, GetReference () is enough for mcs-cecil
2314 if (IsDynamicType (type))
2317 if (type is DynamicArrayType)
2318 return type.UnderlyingSystemType;
2324 /// Utility function that can be used to probe whether a type
2325 /// is managed or not.
2327 public static bool VerifyUnmanaged (CompilerContext ctx, Type t, Location loc)
2329 if (IsUnmanagedType (t))
2333 t = GetElementType (t);
2335 ctx.Report.SymbolRelatedToPreviousError (t);
2336 ctx.Report.Error (208, loc,
2337 "Cannot take the address of, get the size of, or declare a pointer to a managed type `{0}'",
2344 /// Returns the name of the indexer in a given type.
2347 /// The default is not always `Item'. The user can change this behaviour by
2348 /// using the IndexerNameAttribute in the container.
2349 /// For example, the String class indexer is named `Chars' not `Item'
2351 public static string IndexerPropertyName (Type t)
2353 t = DropGenericTypeArguments (t);
2354 if (t is TypeBuilder) {
2355 TypeContainer tc = t.IsInterface ? LookupInterface (t) : LookupTypeContainer (t);
2356 return tc == null ? TypeContainer.DefaultIndexerName : tc.IndexerName;
2359 PredefinedAttribute pa = PredefinedAttributes.Get.DefaultMember;
2361 System.Attribute attr = System.Attribute.GetCustomAttribute (
2364 DefaultMemberAttribute dma = (DefaultMemberAttribute) attr;
2365 return dma.MemberName;
2369 return TypeContainer.DefaultIndexerName;
2372 private static bool IsSignatureEqual (Type a, Type b)
2375 /// Consider the following example (bug #77674):
2377 /// public abstract class A
2379 /// public abstract T Foo<T> ();
2382 /// public abstract class B : A
2384 /// public override U Foo<T> ()
2385 /// { return default (U); }
2388 /// Here, `T' and `U' are method type parameters from different methods
2389 /// (A.Foo and B.Foo), so both `==' and Equals() will fail.
2391 /// However, since we're determining whether B.Foo() overrides A.Foo(),
2392 /// we need to do a signature based comparision and consider them equal.
2397 if (a.IsGenericParameter && b.IsGenericParameter &&
2398 (a.DeclaringMethod != null) && (b.DeclaringMethod != null)) {
2399 return a.GenericParameterPosition == b.GenericParameterPosition;
2402 if (a.IsArray && b.IsArray) {
2403 if (a.GetArrayRank () != b.GetArrayRank ())
2406 return IsSignatureEqual (GetElementType (a), GetElementType (b));
2409 if (a.IsByRef && b.IsByRef)
2410 return IsSignatureEqual (GetElementType (a), GetElementType (b));
2412 if (IsGenericType (a) && IsGenericType (b)) {
2413 if (DropGenericTypeArguments (a) != DropGenericTypeArguments (b))
2416 Type[] aargs = GetTypeArguments (a);
2417 Type[] bargs = GetTypeArguments (b);
2419 if (aargs.Length != bargs.Length)
2422 for (int i = 0; i < aargs.Length; i++) {
2423 if (!IsSignatureEqual (aargs [i], bargs [i]))
2434 // Returns whether the array of memberinfos contains the given method
2436 public static bool ArrayContainsMethod (MethodBase [] array, MethodBase new_method, bool ignoreDeclType)
2438 Type [] new_args = TypeManager.GetParameterData (new_method).Types;
2440 foreach (MethodBase method in array) {
2441 if (!ignoreDeclType && method.DeclaringType != new_method.DeclaringType)
2444 if (method.Name != new_method.Name)
2447 if (method is MethodInfo && new_method is MethodInfo &&
2449 TypeToCoreType (((MethodInfo) method).ReturnType),
2450 TypeToCoreType (((MethodInfo) new_method).ReturnType)))
2453 Type [] old_args = TypeManager.GetParameterData (method).Types;
2454 int old_count = old_args.Length;
2457 if (new_args.Length != old_count)
2460 for (i = 0; i < old_count; i++){
2461 if (!IsSignatureEqual (old_args [i], new_args [i]))
2474 // We copy methods from `new_members' into `target_list' if the signature
2475 // for the method from in the new list does not exist in the target_list
2477 // The name is assumed to be the same.
2479 public static List<MethodBase> CopyNewMethods (List<MethodBase> target_list, IList<MemberInfo> new_members)
2481 if (target_list == null){
2482 target_list = new List<MethodBase> ();
2484 foreach (MemberInfo mi in new_members){
2485 if (mi is MethodBase)
2486 target_list.Add ((MethodBase) mi);
2491 MethodBase[] target_array = new MethodBase[target_list.Count];
2492 target_list.CopyTo (target_array, 0);
2494 foreach (MemberInfo mi in new_members){
2495 MethodBase new_method = (MethodBase) mi;
2497 if (!ArrayContainsMethod (target_array, new_method, true))
2498 target_list.Add (new_method);
2505 // Tracks the generic parameters.
2508 public static void AddTypeParameter (GenericTypeParameterBuilder t, TypeParameter tparam)
2510 builder_to_type_param [t] = tparam;
2513 public static TypeParameter LookupTypeParameter (Type t)
2516 var gtp = t as GenericTypeParameterBuilder;
2517 if (gtp != null && builder_to_type_param.TryGetValue (gtp, out tp))
2523 // This method always return false for non-generic compiler,
2524 // while Type.IsGenericParameter is returned if it is supported.
2525 public static bool IsGenericParameter (Type type)
2527 return type.IsGenericParameter;
2530 public static int GenericParameterPosition (Type type)
2532 return type.GenericParameterPosition;
2535 public static bool IsGenericType (Type type)
2537 return type.IsGenericType;
2540 public static bool IsGenericTypeDefinition (Type type)
2542 return type.IsGenericTypeDefinition;
2545 public static bool ContainsGenericParameters (Type type)
2547 return type.ContainsGenericParameters;
2550 public static FieldInfo GetGenericFieldDefinition (FieldInfo fi)
2552 if (fi.DeclaringType.IsGenericTypeDefinition ||
2553 !fi.DeclaringType.IsGenericType)
2556 Type t = fi.DeclaringType.GetGenericTypeDefinition ();
2557 BindingFlags bf = BindingFlags.Public | BindingFlags.NonPublic |
2558 BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
2560 // TODO: use CodeGen.Module.Builder.ResolveField (fi.MetadataToken);
2561 foreach (FieldInfo f in t.GetFields (bf))
2562 if (f.MetadataToken == fi.MetadataToken)
2568 public static bool IsEqual (Type a, Type b)
2571 // MS BCL returns true even if enum types are different
2572 if (a.BaseType == TypeManager.enum_type || b.BaseType == TypeManager.enum_type)
2573 return a.FullName == b.FullName;
2575 // Some types are never equal
2576 if (a == TypeManager.null_type || a == InternalType.AnonymousMethod)
2582 if (IsGenericParameter (a) && IsGenericParameter (b)) {
2583 // TODO: needs more testing before cleaning up
2584 //if (a.DeclaringMethod != b.DeclaringMethod &&
2585 // (a.DeclaringMethod == null || b.DeclaringMethod == null))
2587 return a.GenericParameterPosition == b.GenericParameterPosition;
2590 if (a.IsArray && b.IsArray) {
2591 if (a.GetArrayRank () != b.GetArrayRank ())
2593 return IsEqual (GetElementType (a), GetElementType (b));
2596 if (a.IsByRef && b.IsByRef)
2597 return IsEqual (a.GetElementType (), b.GetElementType ());
2599 if (IsGenericType (a) && IsGenericType (b)) {
2600 Type adef = DropGenericTypeArguments (a);
2601 Type bdef = DropGenericTypeArguments (b);
2606 if (adef.IsEnum && bdef.IsEnum)
2609 Type[] aargs = GetTypeArguments (a);
2610 Type[] bargs = GetTypeArguments (b);
2612 if (aargs.Length != bargs.Length)
2615 for (int i = 0; i < aargs.Length; i++) {
2616 if (!IsEqual (aargs [i], bargs [i]))
2626 public static bool IsEqual (Type[] a, Type[] b)
2628 if (a == null || b == null || a.Length != b.Length)
2631 for (int i = 0; i < a.Length; ++i) {
2632 if (a [i] == null || b [i] == null) {
2639 if (!IsEqual (a [i], b [i]))
2646 public static Type DropGenericTypeArguments (Type t)
2648 if (!t.IsGenericType)
2650 // Micro-optimization: a generic typebuilder is always a generic type definition
2651 if (t is TypeBuilder)
2653 return t.GetGenericTypeDefinition ();
2656 public static MethodBase DropGenericMethodArguments (MethodSpec m)
2658 return DropGenericMethodArguments (m.MetaInfo);
2661 public static MethodBase DropGenericMethodArguments (MethodBase m)
2663 if (m.IsGenericMethod)
2664 m = ((MethodInfo) m).GetGenericMethodDefinition ();
2666 Type t = m.DeclaringType;
2667 if (!t.IsGenericType || t.IsGenericTypeDefinition)
2670 t = t.GetGenericTypeDefinition ();
2671 BindingFlags bf = BindingFlags.Public | BindingFlags.NonPublic |
2672 BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly;
2675 // TODO: use CodeGen.Module.Builder.ResolveMethod ()
2679 if (m is ConstructorInfo) {
2680 foreach (ConstructorInfo c in t.GetConstructors (bf))
2681 if (c.MetadataToken == m.MetadataToken)
2684 foreach (MethodBase mb in t.GetMethods (bf))
2685 if (mb.MetadataToken == m.MetadataToken)
2692 public static Type[] GetGenericArguments (MethodBase mi)
2694 return mi.GetGenericArguments () ?? Type.EmptyTypes;
2697 public static Type[] GetTypeArguments (Type t)
2699 DeclSpace tc = LookupDeclSpace (t);
2702 return Type.EmptyTypes;
2704 TypeParameter[] tparam = tc.TypeParameters;
2705 Type[] ret = new Type [tparam.Length];
2706 for (int i = 0; i < tparam.Length; i++) {
2707 ret [i] = tparam [i].Type;
2708 if (ret [i] == null)
2709 throw new InternalErrorException ();
2714 return t.GetGenericArguments ();
2717 public static GenericConstraints GetTypeParameterConstraints (Type t)
2719 if (!t.IsGenericParameter)
2720 throw new InvalidOperationException ();
2722 TypeParameter tparam = LookupTypeParameter (t);
2724 return tparam.GenericConstraints;
2726 return ReflectionConstraints.GetConstraints (t);
2729 public static bool HasGenericArguments (Type t)
2731 return GetNumberOfTypeArguments (t) > 0;
2734 public static int GetNumberOfTypeArguments (Type t)
2736 if (t.IsGenericParameter)
2738 DeclSpace tc = LookupDeclSpace (t);
2740 return tc.IsGeneric ? tc.CountTypeParameters : 0;
2742 return t.IsGenericType ? t.GetGenericArguments ().Length : 0;
2746 /// Check whether `type' and `parent' are both instantiations of the same
2747 /// generic type. Note that we do not check the type parameters here.
2749 public static bool IsInstantiationOfSameGenericType (Type type, Type parent)
2751 int tcount = GetNumberOfTypeArguments (type);
2752 int pcount = GetNumberOfTypeArguments (parent);
2754 if (tcount != pcount)
2757 type = DropGenericTypeArguments (type);
2758 parent = DropGenericTypeArguments (parent);
2760 return type.Equals (parent);
2764 /// Whether `mb' is a generic method definition.
2766 public static bool IsGenericMethodDefinition (MethodBase mb)
2768 if (mb.DeclaringType is TypeBuilder) {
2769 IMethodData method = GetMethod (mb);
2773 return method.GenericMethod != null;
2776 return mb.IsGenericMethodDefinition;
2780 /// Whether `mb' is a generic method.
2782 public static bool IsGenericMethod (MethodBase mb)
2784 return mb.IsGenericMethod;
2787 public static bool IsNullableType (Type t)
2789 return generic_nullable_type == DropGenericTypeArguments (t);
2792 public static MethodInfo MakeGenericMethod (MethodInfo gmd, Type[] methodArguments)
2794 if (!gmd.IsGenericMethodDefinition)
2795 gmd = gmd.GetGenericMethodDefinition ();
2796 return gmd.MakeGenericMethod (methodArguments);
2800 #region MemberLookup implementation
2803 // Whether we allow private members in the result (since FindMembers
2804 // uses NonPublic for both protected and private), we need to distinguish.
2807 internal class Closure {
2808 internal bool private_ok;
2810 // Who is invoking us and which type is being queried currently.
2811 internal Type invocation_type;
2812 internal Type qualifier_type;
2814 // The assembly that defines the type is that is calling us
2815 internal Assembly invocation_assembly;
2816 internal IList<MemberInfo> almost_match;
2818 private bool CheckValidFamilyAccess (bool is_static, MemberInfo m)
2820 if (invocation_type == null)
2823 if (is_static && qualifier_type == null)
2824 // It resolved from a simple name, so it should be visible.
2827 if (IsNestedChildOf (invocation_type, m.DeclaringType))
2830 for (Type t = invocation_type; t != null; t = t.DeclaringType) {
2831 if (!IsFamilyAccessible (t, m.DeclaringType))
2834 // Although a derived class can access protected members of its base class
2835 // it cannot do so through an instance of the base class (CS1540).
2836 // => Ancestry should be: declaring_type ->* invocation_type ->* qualified_type
2837 if (is_static || qualifier_type == null ||
2838 IsInstantiationOfSameGenericType (t, qualifier_type) ||
2839 IsFamilyAccessible (qualifier_type, t))
2843 if (almost_match != null)
2844 almost_match.Add (m);
2850 // This filter filters by name + whether it is ok to include private
2851 // members in the search
2853 internal bool Filter (MemberInfo m, object filter_criteria)
2856 // Hack: we know that the filter criteria will always be in the
2857 // `closure' // fields.
2860 if ((filter_criteria != null) && (m.Name != (string) filter_criteria))
2863 if (((qualifier_type == null) || (qualifier_type == invocation_type)) &&
2864 (invocation_type != null) &&
2865 IsPrivateAccessible (m.DeclaringType, invocation_type))
2869 // Ugly: we need to find out the type of `m', and depending
2870 // on this, tell whether we accept or not
2872 if (m is MethodBase){
2873 MethodBase mb = (MethodBase) m;
2874 MethodAttributes ma = mb.Attributes & MethodAttributes.MemberAccessMask;
2876 if (ma == MethodAttributes.Public)
2879 if (ma == MethodAttributes.PrivateScope)
2882 if (ma == MethodAttributes.Private)
2883 return private_ok ||
2884 IsPrivateAccessible (invocation_type, m.DeclaringType) ||
2885 IsNestedChildOf (invocation_type, m.DeclaringType);
2887 if (TypeManager.IsThisOrFriendAssembly (invocation_assembly, mb.DeclaringType.Assembly)) {
2888 if (ma == MethodAttributes.Assembly || ma == MethodAttributes.FamORAssem)
2891 if (ma == MethodAttributes.Assembly || ma == MethodAttributes.FamANDAssem)
2895 // Family, FamORAssem or FamANDAssem
2896 return CheckValidFamilyAccess (mb.IsStatic, m);
2899 if (m is FieldInfo){
2900 FieldInfo fi = (FieldInfo) m;
2901 FieldAttributes fa = fi.Attributes & FieldAttributes.FieldAccessMask;
2903 if (fa == FieldAttributes.Public)
2906 if (fa == FieldAttributes.PrivateScope)
2909 if (fa == FieldAttributes.Private)
2910 return private_ok ||
2911 IsPrivateAccessible (invocation_type, m.DeclaringType) ||
2912 IsNestedChildOf (invocation_type, m.DeclaringType);
2914 if (TypeManager.IsThisOrFriendAssembly (invocation_assembly, fi.DeclaringType.Assembly)) {
2915 if ((fa == FieldAttributes.Assembly) ||
2916 (fa == FieldAttributes.FamORAssem))
2919 if ((fa == FieldAttributes.Assembly) ||
2920 (fa == FieldAttributes.FamANDAssem))
2924 // Family, FamORAssem or FamANDAssem
2925 return CheckValidFamilyAccess (fi.IsStatic, m);
2929 // EventInfos and PropertyInfos, return true because they lack
2930 // permission information, so we need to check later on the methods.
2936 static Closure closure;
2937 static MemberFilter FilterWithClosure_delegate;
2940 // Looks up a member called `name' in the `queried_type'. This lookup
2941 // is done by code that is contained in the definition for `invocation_type'
2942 // through a qualifier of type `qualifier_type' (or null if there is no qualifier).
2944 // `invocation_type' is used to check whether we're allowed to access the requested
2945 // member wrt its protection level.
2947 // When called from MemberAccess, `qualifier_type' is the type which is used to access
2948 // the requested member (`class B { A a = new A (); a.foo = 5; }'; here invocation_type
2949 // is B and qualifier_type is A). This is used to do the CS1540 check.
2951 // When resolving a SimpleName, `qualifier_type' is null.
2953 // The `qualifier_type' is used for the CS1540 check; it's normally either null or
2954 // the same than `queried_type' - except when we're being called from BaseAccess;
2955 // in this case, `invocation_type' is the current type and `queried_type' the base
2956 // type, so this'd normally trigger a CS1540.
2958 // The binding flags are `bf' and the kind of members being looked up are `mt'
2960 // The return value always includes private members which code in `invocation_type'
2961 // is allowed to access (using the specified `qualifier_type' if given); only use
2962 // BindingFlags.NonPublic to bypass the permission check.
2964 // The 'almost_match' argument is used for reporting error CS1540.
2966 // Returns an array of a single element for everything but Methods/Constructors
2967 // that might return multiple matches.
2969 public static MemberInfo [] MemberLookup (Type invocation_type, Type qualifier_type,
2970 Type queried_type, MemberTypes mt,
2971 BindingFlags original_bf, string name, IList<MemberInfo> almost_match)
2973 Timer.StartTimer (TimerType.MemberLookup);
2975 MemberInfo[] retval = RealMemberLookup (invocation_type, qualifier_type,
2976 queried_type, mt, original_bf, name, almost_match);
2978 Timer.StopTimer (TimerType.MemberLookup);
2983 static MemberInfo [] RealMemberLookup (Type invocation_type, Type qualifier_type,
2984 Type queried_type, MemberTypes mt,
2985 BindingFlags original_bf, string name, IList<MemberInfo> almost_match)
2987 BindingFlags bf = original_bf;
2989 List<MethodBase> method_list = null;
2990 Type current_type = queried_type;
2991 bool searching = (original_bf & BindingFlags.DeclaredOnly) == 0;
2992 bool skip_iface_check = true, used_cache = false;
2993 bool always_ok_flag = invocation_type != null && IsNestedChildOf (invocation_type, queried_type);
2995 closure.invocation_type = invocation_type;
2996 closure.invocation_assembly = invocation_type != null ? invocation_type.Assembly : null;
2997 closure.qualifier_type = qualifier_type;
2998 closure.almost_match = almost_match;
3000 // This is from the first time we find a method
3001 // in most cases, we do not actually find a method in the base class
3002 // so we can just ignore it, and save the arraylist allocation
3003 MemberInfo [] first_members_list = null;
3004 bool use_first_members_list = false;
3010 // `NonPublic' is lame, because it includes both protected and
3011 // private methods, so we need to control this behavior by
3012 // explicitly tracking if a private method is ok or not.
3014 // The possible cases are:
3015 // public, private and protected (internal does not come into the
3018 if ((invocation_type != null) &&
3019 ((invocation_type == current_type) ||
3020 IsNestedChildOf (invocation_type, current_type)) ||
3022 bf = original_bf | BindingFlags.NonPublic;
3026 closure.private_ok = (original_bf & BindingFlags.NonPublic) != 0;
3028 Timer.StopTimer (TimerType.MemberLookup);
3030 list = MemberLookup_FindMembers (current_type, mt, bf, name, out used_cache);
3032 Timer.StartTimer (TimerType.MemberLookup);
3035 // When queried for an interface type, the cache will automatically check all
3036 // inherited members, so we don't need to do this here. However, this only
3037 // works if we already used the cache in the first iteration of this loop.
3039 // If we used the cache in any further iteration, we can still terminate the
3040 // loop since the cache always looks in all base classes.
3046 skip_iface_check = false;
3048 if (current_type == TypeManager.object_type)
3051 current_type = current_type.BaseType;
3054 // This happens with interfaces, they have a null
3055 // basetype. Look members up in the Object class.
3057 if (current_type == null) {
3058 current_type = TypeManager.object_type;
3063 if (list.Length == 0)
3067 // Events and types are returned by both `static' and `instance'
3068 // searches, which means that our above FindMembers will
3069 // return two copies of the same.
3071 if (list.Length == 1 && !(list [0] is MethodBase)){
3076 // Multiple properties: we query those just to find out the indexer
3079 if (list [0] is PropertyInfo)
3083 // We found an event: the cache lookup returns both the event and
3084 // its private field.
3086 if (list [0] is EventInfo) {
3087 if ((list.Length == 2) && (list [1] is FieldInfo))
3088 return new MemberInfo [] { list [0] };
3094 // We found methods, turn the search into "method scan"
3098 if (first_members_list != null) {
3099 if (use_first_members_list) {
3100 method_list = CopyNewMethods (method_list, first_members_list);
3101 use_first_members_list = false;
3104 method_list = CopyNewMethods (method_list, list);
3106 first_members_list = list;
3107 use_first_members_list = true;
3108 mt &= (MemberTypes.Method | MemberTypes.Constructor);
3110 } while (searching);
3112 if (use_first_members_list)
3113 return first_members_list;
3115 if (method_list != null && method_list.Count > 0) {
3116 return method_list.ToArray ();
3119 // This happens if we already used the cache in the first iteration, in this case
3120 // the cache already looked in all interfaces.
3122 if (skip_iface_check)
3126 // Interfaces do not list members they inherit, so we have to
3129 if (!queried_type.IsInterface)
3132 if (queried_type.IsArray)
3133 queried_type = TypeManager.array_type;
3135 Type [] ifaces = GetInterfaces (queried_type);
3139 foreach (Type itype in ifaces){
3142 x = MemberLookup (null, null, itype, mt, bf, name, null);
3150 public const BindingFlags AllMembers = BindingFlags.Public | BindingFlags.NonPublic |
3151 BindingFlags.Static | BindingFlags.Instance |
3152 BindingFlags.DeclaredOnly;
3154 // Currently is designed to work with external types only
3155 public static PropertyInfo GetPropertyFromAccessor (MethodBase mb)
3157 if (!mb.IsSpecialName)
3160 string name = mb.Name;
3161 if (name.Length < 5)
3164 if (name [3] != '_')
3167 if (name.StartsWith ("get") || name.StartsWith ("set")) {
3168 MemberInfo[] pi = mb.DeclaringType.FindMembers (MemberTypes.Property, AllMembers,
3169 Type.FilterName, name.Substring (4));
3174 // This can happen when property is indexer (it can have same name but different parameters)
3175 foreach (PropertyInfo p in pi) {
3176 foreach (MethodInfo p_mi in p.GetAccessors (true)) {
3177 if (p_mi == mb || TypeManager.GetParameterData (p_mi).Equals (TypeManager.GetParameterData (mb)))
3186 // Currently is designed to work with external types only
3187 public static MemberInfo GetEventFromAccessor (MethodBase mb)
3189 if (!mb.IsSpecialName)
3192 string name = mb.Name;
3193 if (name.Length < 5)
3196 if (name.StartsWith ("add_"))
3197 return mb.DeclaringType.GetEvent (name.Substring (4), AllMembers);
3199 if (name.StartsWith ("remove_"))
3200 return mb.DeclaringType.GetEvent (name.Substring (7), AllMembers);
3205 // Tests whether external method is really special
3206 public static bool IsSpecialMethod (MethodBase mb)
3208 if (!mb.IsSpecialName)
3211 IMethodData md = TypeManager.GetMethod (mb);
3213 return (md is AbstractPropertyEventMethod || md is Operator);
3215 PropertyInfo pi = GetPropertyFromAccessor (mb);
3217 return IsValidProperty (pi);
3219 if (GetEventFromAccessor (mb) != null)
3222 string name = mb.Name;
3223 if (name.StartsWith ("op_"))
3224 return Operator.GetName (name) != null;
3229 // Tests whether imported property is valid C# property.
3230 // TODO: It seems to me that we should do a lot of sanity tests before
3231 // we accept property as C# property
3232 static bool IsValidProperty (PropertyInfo pi)
3234 MethodInfo get_method = pi.GetGetMethod (true);
3235 MethodInfo set_method = pi.GetSetMethod (true);
3238 if (get_method != null && set_method != null) {
3239 g_count = get_method.GetParameters ().Length;
3240 s_count = set_method.GetParameters ().Length;
3241 if (g_count + 1 != s_count)
3243 } else if (get_method != null) {
3244 g_count = get_method.GetParameters ().Length;
3245 } else if (set_method != null) {
3246 s_count = set_method.GetParameters ().Length;
3250 // DefaultMemberName and indexer name has to match to identify valid C# indexer
3252 PredefinedAttribute pa = PredefinedAttributes.Get.DefaultMember;
3253 if ((s_count > 1 || g_count > 0) && pa.IsDefined) {
3254 object[] o = pi.DeclaringType.GetCustomAttributes (pa.Type, false);
3258 DefaultMemberAttribute dma = (DefaultMemberAttribute) o [0];
3259 if (dma.MemberName != pi.Name)
3261 if (get_method != null && "get_" + dma.MemberName != get_method.Name)
3263 if (set_method != null && "set_" + dma.MemberName != set_method.Name)
3276 public static readonly Type AnonymousMethod = typeof (AnonymousMethodBody);
3277 public static readonly Type Arglist = typeof (ArglistAccess);
3278 public static readonly Type Dynamic = new DynamicType ();
3279 public static readonly Type MethodGroup = typeof (MethodGroupExpr);
3283 /// There is exactly one instance of this class per type.
3285 sealed class TypeHandle : IMemberContainer {
3286 public readonly IMemberContainer BaseType;
3288 readonly int id = ++next_id;
3289 static int next_id = 0;
3291 static TypeHandle ()
3297 /// Lookup a TypeHandle instance for the given type. If the type doesn't have
3298 /// a TypeHandle yet, a new instance of it is created. This static method
3299 /// ensures that we'll only have one TypeHandle instance per type.
3301 private static TypeHandle GetTypeHandle (Type t)
3304 if (type_hash.TryGetValue (t, out handle))
3307 handle = new TypeHandle (t);
3308 type_hash.Add (t, handle);
3312 public static MemberCache GetMemberCache (Type t)
3314 return GetTypeHandle (t).MemberCache;
3317 public static void CleanUp ()
3322 public static void Reset ()
3324 type_hash = new Dictionary<Type, TypeHandle> (ReferenceEquality<Type>.Default);
3328 /// Returns the TypeHandle for TypeManager.object_type.
3330 public static IMemberContainer ObjectType {
3332 if (object_type != null)
3335 object_type = GetTypeHandle (TypeManager.object_type);
3342 /// Returns the TypeHandle for TypeManager.array_type.
3344 public static TypeHandle ArrayType {
3346 if (array_type != null)
3349 array_type = GetTypeHandle (TypeManager.array_type);
3355 static Dictionary<Type, TypeHandle> type_hash;
3357 private static TypeHandle object_type;
3358 private static TypeHandle array_type;
3361 private string full_name;
3362 private bool is_interface;
3363 private MemberCache member_cache;
3364 private MemberCache base_cache;
3366 private TypeHandle (Type type)
3369 full_name = type.FullName != null ? type.FullName : type.Name;
3370 if (type.BaseType != null) {
3371 base_cache = TypeManager.LookupMemberCache (type.BaseType);
3372 BaseType = base_cache.Container;
3373 } else if (type.IsInterface)
3374 base_cache = TypeManager.LookupBaseInterfacesCache (type);
3375 this.is_interface = type.IsInterface || TypeManager.IsGenericParameter (type);
3376 this.member_cache = new MemberCache (this);
3379 // IMemberContainer methods
3381 public string Name {
3393 public MemberCache BaseCache {
3399 public bool IsInterface {
3401 return is_interface;
3405 public MemberList GetMembers (MemberTypes mt, BindingFlags bf)
3407 MemberInfo [] members;
3409 if (type is GenericTypeParameterBuilder)
3410 return MemberList.Empty;
3413 type = TypeManager.DropGenericTypeArguments (type);
3415 if (mt == MemberTypes.Event)
3416 members = type.GetEvents (bf | BindingFlags.DeclaredOnly);
3418 members = type.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
3421 if (members.Length == 0)
3422 return MemberList.Empty;
3424 Array.Reverse (members);
3425 return new MemberList (members);
3428 // IMemberFinder methods
3430 public MemberList FindMembers (MemberTypes mt, BindingFlags bf, string name,
3431 MemberFilter filter, object criteria)
3433 return new MemberList (member_cache.FindMembers (mt, bf, name, filter, criteria));
3436 public MemberCache MemberCache {
3438 return member_cache;
3442 public override string ToString ()
3444 if (BaseType != null)
3445 return "TypeHandle (" + id + "," + Name + " : " + BaseType + ")";
3447 return "TypeHandle (" + id + "," + Name + ")";