2 // typemanager.cs: C# type manager
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 // Ravi Pratap (ravi@ximian.com)
7 // Licensed under the terms of the GNU GPL
9 // (C) 2001 Ximian, Inc (http://www.ximian.com)
14 // We will eventually remove the SIMPLE_SPEEDUP, and should never change
15 // the behavior of the compilation. This can be removed if we rework
16 // the code to get a list of namespaces available.
18 #define SIMPLE_SPEEDUP
22 using System.Globalization;
23 using System.Collections;
24 using System.Reflection;
25 using System.Reflection.Emit;
27 using System.Text.RegularExpressions;
28 using System.Runtime.CompilerServices;
29 using System.Diagnostics;
31 namespace Mono.CSharp {
33 public 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 char_ptr_type;
48 static public Type short_type;
49 static public Type decimal_type;
50 static public Type bool_type;
51 static public Type sbyte_type;
52 static public Type byte_type;
53 static public Type ushort_type;
54 static public Type enum_type;
55 static public Type delegate_type;
56 static public Type multicast_delegate_type;
57 static public Type void_type;
58 static public Type null_type;
59 static public Type enumeration_type;
60 static public Type array_type;
61 static public Type runtime_handle_type;
62 static public Type icloneable_type;
63 static public Type type_type;
64 static public Type ienumerator_type;
65 static public Type ienumerable_type;
66 static public Type idisposable_type;
67 static public Type iconvertible_type;
68 static public Type default_member_type;
69 static public Type iasyncresult_type;
70 static public Type asynccallback_type;
71 static public Type intptr_type;
72 static public Type monitor_type;
73 static public Type runtime_field_handle_type;
74 static public Type runtime_argument_handle_type;
75 static public Type attribute_type;
76 static public Type attribute_usage_type;
77 static public Type decimal_constant_attribute_type;
78 static public Type dllimport_type;
79 static public Type unverifiable_code_type;
80 static public Type methodimpl_attr_type;
81 static public Type marshal_as_attr_type;
82 static public Type param_array_type;
83 static public Type guid_attr_type;
84 static public Type void_ptr_type;
85 static public Type indexer_name_type;
86 static public Type exception_type;
87 static public Type invalid_operation_exception_type;
88 static public Type not_supported_exception_type;
89 static public Type obsolete_attribute_type;
90 static public Type conditional_attribute_type;
91 static public Type in_attribute_type;
92 static public Type anonymous_method_type;
93 static public Type cls_compliant_attribute_type;
94 static public Type typed_reference_type;
95 static public Type arg_iterator_type;
96 static public Type mbr_type;
97 static public Type struct_layout_attribute_type;
98 static public Type field_offset_attribute_type;
99 static public Type security_attr_type;
102 // An empty array of types
104 static public Type [] NoTypes;
105 static public TypeExpr [] NoTypeExprs;
109 // Expressions representing the internal types. Used during declaration
112 static public TypeExpr system_object_expr, system_string_expr;
113 static public TypeExpr system_boolean_expr, system_decimal_expr;
114 static public TypeExpr system_single_expr, system_double_expr;
115 static public TypeExpr system_sbyte_expr, system_byte_expr;
116 static public TypeExpr system_int16_expr, system_uint16_expr;
117 static public TypeExpr system_int32_expr, system_uint32_expr;
118 static public TypeExpr system_int64_expr, system_uint64_expr;
119 static public TypeExpr system_char_expr, system_void_expr;
120 static public TypeExpr system_asynccallback_expr;
121 static public TypeExpr system_iasyncresult_expr;
122 static public TypeExpr system_valuetype_expr;
123 static public TypeExpr system_intptr_expr;
126 // This is only used when compiling corlib
128 static public Type system_int32_type;
129 static public Type system_array_type;
130 static public Type system_type_type;
131 static public Type system_assemblybuilder_type;
132 static public MethodInfo system_int_array_get_length;
133 static public MethodInfo system_int_array_get_rank;
134 static public MethodInfo system_object_array_clone;
135 static public MethodInfo system_int_array_get_length_int;
136 static public MethodInfo system_int_array_get_lower_bound_int;
137 static public MethodInfo system_int_array_get_upper_bound_int;
138 static public MethodInfo system_void_array_copyto_array_int;
142 // Internal, not really used outside
144 static Type runtime_helpers_type;
147 // These methods are called by code generated by the compiler
149 static public MethodInfo string_concat_string_string;
150 static public MethodInfo string_concat_string_string_string;
151 static public MethodInfo string_concat_string_string_string_string;
152 static public MethodInfo string_concat_string_dot_dot_dot;
153 static public MethodInfo string_concat_object_object;
154 static public MethodInfo string_concat_object_object_object;
155 static public MethodInfo string_concat_object_dot_dot_dot;
156 static public MethodInfo string_isinterneted_string;
157 static public MethodInfo system_type_get_type_from_handle;
158 static public MethodInfo object_getcurrent_void;
159 static public MethodInfo bool_movenext_void;
160 static public MethodInfo ienumerable_getenumerator_void;
161 static public MethodInfo void_reset_void;
162 static public MethodInfo void_dispose_void;
163 static public MethodInfo void_monitor_enter_object;
164 static public MethodInfo void_monitor_exit_object;
165 static public MethodInfo void_initializearray_array_fieldhandle;
166 static public MethodInfo int_getlength_int;
167 static public MethodInfo delegate_combine_delegate_delegate;
168 static public MethodInfo delegate_remove_delegate_delegate;
169 static public MethodInfo int_get_offset_to_string_data;
170 static public MethodInfo int_array_get_length;
171 static public MethodInfo int_array_get_rank;
172 static public MethodInfo object_array_clone;
173 static public MethodInfo int_array_get_length_int;
174 static public MethodInfo int_array_get_lower_bound_int;
175 static public MethodInfo int_array_get_upper_bound_int;
176 static public MethodInfo void_array_copyto_array_int;
179 // The attribute constructors.
181 static public ConstructorInfo object_ctor;
182 static public ConstructorInfo cons_param_array_attribute;
183 static public ConstructorInfo void_decimal_ctor_five_args;
184 static public ConstructorInfo void_decimal_ctor_int_arg;
185 static public ConstructorInfo unverifiable_code_ctor;
186 static public ConstructorInfo invalid_operation_ctor;
187 static public ConstructorInfo default_member_ctor;
188 static public ConstructorInfo decimal_constant_attribute_ctor;
191 // Holds the Array of Assemblies that have been loaded
192 // (either because it is the default or the user used the
193 // -r command line option)
195 static Assembly [] assemblies;
198 // Keeps a list of modules. We used this to do lookups
199 // on the module using GetType -- needed for arrays
201 static Module [] modules;
204 // This is the type_cache from the assemblies to avoid
205 // hitting System.Reflection on every lookup.
207 static Hashtable types;
210 // This is used to hotld the corresponding TypeContainer objects
211 // since we need this in FindMembers
213 static Hashtable typecontainers;
216 // Keeps track of those types that are defined by the
219 static ArrayList user_types;
221 static PtrHashtable builder_to_declspace;
223 static PtrHashtable builder_to_member_cache;
226 // Tracks the interfaces implemented by typebuilders. We only
227 // enter those who do implement or or more interfaces
229 static PtrHashtable builder_to_ifaces;
232 // Maps MethodBase.RuntimeTypeHandle to a Type array that contains
233 // the arguments to the method
235 static Hashtable method_arguments;
238 // Maps PropertyBuilder to a Type array that contains
239 // the arguments to the indexer
241 static Hashtable indexer_arguments;
244 // Maybe `method_arguments' should be replaced and only
245 // method_internal_params should be kept?
247 static Hashtable method_internal_params;
250 // Keeps track of methods
253 static Hashtable builder_to_method;
256 // Contains all public types from referenced assemblies.
257 // This member is used only if CLS Compliance verification is required.
259 public static Hashtable all_imported_types;
266 public static void CleanUp ()
268 // Lets get everything clean so that we can collect before generating code
272 typecontainers = null;
274 builder_to_declspace = null;
275 builder_to_member_cache = null;
276 builder_to_ifaces = null;
277 method_arguments = null;
278 indexer_arguments = null;
279 method_internal_params = null;
280 builder_to_method = null;
284 negative_hits = null;
285 builder_to_constant = null;
286 fieldbuilders_to_fields = null;
288 priv_fields_events = null;
292 TypeHandle.CleanUp ();
296 /// A filter for Findmembers that uses the Signature object to
299 static bool SignatureFilter (MemberInfo mi, object criteria)
301 Signature sig = (Signature) criteria;
303 if (!(mi is MethodBase))
306 if (mi.Name != sig.name)
309 int count = sig.args.Length;
311 if (mi is MethodBuilder || mi is ConstructorBuilder){
312 Type [] candidate_args = GetArgumentTypes ((MethodBase) mi);
314 if (candidate_args.Length != count)
317 for (int i = 0; i < count; i++)
318 if (candidate_args [i] != sig.args [i])
323 ParameterInfo [] pars = ((MethodBase) mi).GetParameters ();
325 if (pars.Length != count)
328 for (int i = 0; i < count; i++)
329 if (pars [i].ParameterType != sig.args [i])
335 // A delegate that points to the filter above.
336 static MemberFilter signature_filter;
339 // These are expressions that represent some of the internal data types, used
342 static void InitExpressionTypes ()
344 system_object_expr = new TypeLookupExpression ("System.Object");
345 system_string_expr = new TypeLookupExpression ("System.String");
346 system_boolean_expr = new TypeLookupExpression ("System.Boolean");
347 system_decimal_expr = new TypeLookupExpression ("System.Decimal");
348 system_single_expr = new TypeLookupExpression ("System.Single");
349 system_double_expr = new TypeLookupExpression ("System.Double");
350 system_sbyte_expr = new TypeLookupExpression ("System.SByte");
351 system_byte_expr = new TypeLookupExpression ("System.Byte");
352 system_int16_expr = new TypeLookupExpression ("System.Int16");
353 system_uint16_expr = new TypeLookupExpression ("System.UInt16");
354 system_int32_expr = new TypeLookupExpression ("System.Int32");
355 system_uint32_expr = new TypeLookupExpression ("System.UInt32");
356 system_int64_expr = new TypeLookupExpression ("System.Int64");
357 system_uint64_expr = new TypeLookupExpression ("System.UInt64");
358 system_char_expr = new TypeLookupExpression ("System.Char");
359 system_void_expr = new TypeLookupExpression ("System.Void");
360 system_asynccallback_expr = new TypeLookupExpression ("System.AsyncCallback");
361 system_iasyncresult_expr = new TypeLookupExpression ("System.IAsyncResult");
362 system_valuetype_expr = new TypeLookupExpression ("System.ValueType");
363 system_intptr_expr = new TypeLookupExpression ("System.IntPtr");
366 static TypeManager ()
368 assemblies = new Assembly [0];
370 user_types = new ArrayList ();
372 types = new Hashtable ();
373 typecontainers = new Hashtable ();
375 builder_to_declspace = new PtrHashtable ();
376 builder_to_member_cache = new PtrHashtable ();
377 builder_to_method = new PtrHashtable ();
378 method_arguments = new PtrHashtable ();
379 method_internal_params = new PtrHashtable ();
380 indexer_arguments = new PtrHashtable ();
381 builder_to_ifaces = new PtrHashtable ();
383 NoTypes = new Type [0];
384 NoTypeExprs = new TypeExpr [0];
386 signature_filter = new MemberFilter (SignatureFilter);
388 InitExpressionTypes ();
391 public static void HandleDuplicate (string name, Type t)
393 Type prev = (Type) types [name];
394 TypeContainer tc = builder_to_declspace [prev] as TypeContainer;
398 // This probably never happens, as we catch this before
400 Report.Error (-17, "The type `" + name + "' has already been defined.");
404 tc = builder_to_declspace [t] as TypeContainer;
407 1595, "The type `" + name + "' is defined in an existing assembly;"+
408 " Using the new definition from: " + tc.Location);
411 1595, "The type `" + name + "' is defined in an existing assembly;");
414 Report.Warning (1595, "Previously defined in: " + prev.Assembly.FullName);
420 public static void AddUserType (string name, TypeBuilder t)
425 HandleDuplicate (name, t);
431 // This entry point is used by types that we define under the covers
433 public static void RegisterBuilder (Type tb, Type [] ifaces)
436 builder_to_ifaces [tb] = ifaces;
439 public static void AddUserType (string name, TypeBuilder t, TypeContainer tc)
441 builder_to_declspace.Add (t, tc);
442 typecontainers.Add (name, tc);
443 AddUserType (name, t);
446 public static void AddDelegateType (string name, TypeBuilder t, Delegate del)
451 HandleDuplicate (name, t);
454 builder_to_declspace.Add (t, del);
457 public static void AddEnumType (string name, TypeBuilder t, Enum en)
462 HandleDuplicate (name, t);
464 builder_to_declspace.Add (t, en);
467 public static void AddMethod (MethodBase builder, IMethodData method)
469 builder_to_method.Add (builder, method);
472 public static IMethodData GetMethod (MethodBase builder)
474 return (IMethodData) builder_to_method [builder];
478 /// Returns the DeclSpace whose Type is `t' or null if there is no
479 /// DeclSpace for `t' (ie, the Type comes from a library)
481 public static DeclSpace LookupDeclSpace (Type t)
483 return builder_to_declspace [t] as DeclSpace;
487 /// Returns the TypeContainer whose Type is `t' or null if there is no
488 /// TypeContainer for `t' (ie, the Type comes from a library)
490 public static TypeContainer LookupTypeContainer (Type t)
492 return builder_to_declspace [t] as TypeContainer;
495 public static MemberCache LookupMemberCache (Type t)
497 if (t is TypeBuilder) {
498 IMemberContainer container = builder_to_declspace [t] as IMemberContainer;
499 if (container != null)
500 return container.MemberCache;
503 if (t is GenericTypeParameterBuilder) {
504 IMemberContainer container = builder_to_type_param [t] as IMemberContainer;
506 if (container != null)
507 return container.MemberCache;
510 return TypeHandle.GetMemberCache (t);
513 public static MemberCache LookupParentInterfacesCache (Type t)
515 Type [] ifaces = t.GetInterfaces ();
517 if (ifaces != null && ifaces.Length == 1)
518 return LookupMemberCache (ifaces [0]);
520 // TODO: the builder_to_member_cache should be indexed by 'ifaces', not 't'
521 MemberCache cache = builder_to_member_cache [t] as MemberCache;
525 cache = new MemberCache (ifaces);
526 builder_to_member_cache.Add (t, cache);
530 public static TypeContainer LookupInterface (Type t)
532 TypeContainer tc = (TypeContainer) builder_to_declspace [t];
533 if ((tc == null) || (tc.Kind != Kind.Interface))
539 public static Delegate LookupDelegate (Type t)
541 return builder_to_declspace [t] as Delegate;
544 public static Enum LookupEnum (Type t)
546 return builder_to_declspace [t] as Enum;
549 public static Class LookupClass (Type t)
551 return (Class) builder_to_declspace [t];
555 /// Registers an assembly to load types from.
557 public static void AddAssembly (Assembly a)
559 foreach (Assembly assembly in assemblies) {
564 int top = assemblies.Length;
565 Assembly [] n = new Assembly [top + 1];
567 assemblies.CopyTo (n, 0);
573 public static Assembly [] GetAssemblies ()
579 /// Registers a module builder to lookup types from
581 public static void AddModule (Module mb)
583 int top = modules != null ? modules.Length : 0;
584 Module [] n = new Module [top + 1];
587 modules.CopyTo (n, 0);
592 public static Module[] Modules {
598 static Hashtable references = new Hashtable ();
601 // Gets the reference to T version of the Type (T&)
603 public static Type GetReferenceType (Type t)
605 return t.MakeByRefType ();
608 static Hashtable pointers = new Hashtable ();
611 // Gets the pointer to T version of the Type (T*)
613 public static Type GetPointerType (Type t)
615 string tname = t.FullName + "*";
617 Type ret = t.Assembly.GetType (tname);
620 // If the type comes from the assembly we are building
621 // We need the Hashtable, because .NET 1.1 will return different instance types
622 // every time we call ModuleBuilder.GetType.
625 if (pointers [t] == null)
626 pointers [t] = CodeGen.Module.Builder.GetType (tname);
628 ret = (Type) pointers [t];
635 // Low-level lookup, cache-less
637 static Type LookupTypeReflection (string name)
641 foreach (Assembly a in assemblies){
642 t = a.GetType (name);
647 TypeAttributes ta = t.Attributes & TypeAttributes.VisibilityMask;
648 if (ta == TypeAttributes.NotPublic ||
649 ta == TypeAttributes.NestedPrivate ||
650 ta == TypeAttributes.NestedAssembly ||
651 ta == TypeAttributes.NestedFamANDAssem){
654 // In .NET pointers turn out to be private, even if their
655 // element type is not
658 t = t.GetElementType ();
668 foreach (Module mb in modules) {
669 t = mb.GetType (name);
677 static Hashtable negative_hits = new Hashtable ();
680 // This function is used when you want to avoid the lookups, and want to go
681 // directly to the source. This will use the cache.
683 // Notice that bypassing the cache is bad, because on Microsoft.NET runtime
684 // GetType ("DynamicType[]") != GetType ("DynamicType[]"), and there is no
685 // way to test things other than doing a fullname compare
687 public static Type LookupTypeDirect (string name)
689 Type t = (Type) types [name];
693 t = LookupTypeReflection (name);
701 static readonly char [] dot_array = { '.' };
704 /// Returns the Type associated with @name, takes care of the fact that
705 /// reflection expects nested types to be separated from the main type
706 /// with a "+" instead of a "."
708 public static Type LookupType (string name)
713 // First lookup in user defined and cached values
716 t = (Type) types [name];
720 // Two thirds of the failures are caught here.
721 if (negative_hits.Contains (name))
724 // Sadly, split takes a param array, so this ends up allocating *EVERY TIME*
725 string [] elements = name.Split (dot_array);
726 int count = elements.Length;
728 for (int n = 1; n <= count; n++){
729 string top_level_type = String.Join (".", elements, 0, n);
731 // One third of the failures are caught here.
732 if (negative_hits.Contains (top_level_type))
735 t = (Type) types [top_level_type];
737 t = LookupTypeReflection (top_level_type);
739 negative_hits [top_level_type] = null;
750 // We know that System.Object does not have children, and since its the parent of
751 // all the objects, it always gets probbed for inner classes.
753 if (top_level_type == "System.Object")
756 string newt = top_level_type + "+" + String.Join ("+", elements, n, count - n);
757 //Console.WriteLine ("Looking up: " + newt + " " + name);
758 t = LookupTypeReflection (newt);
760 negative_hits [name] = null;
765 negative_hits [name] = null;
770 /// Computes the namespaces that we import from the assemblies we reference.
772 public static void ComputeNamespaces ()
774 MethodInfo assembly_get_namespaces = typeof (Assembly).GetMethod ("GetNamespaces", BindingFlags.Instance|BindingFlags.NonPublic);
777 // First add the assembly namespaces
779 if (assembly_get_namespaces != null){
780 int count = assemblies.Length;
782 for (int i = 0; i < count; i++){
783 Assembly a = assemblies [i];
784 string [] namespaces = (string []) assembly_get_namespaces.Invoke (a, null);
785 foreach (string ns in namespaces){
788 Namespace.LookupNamespace (ns, true);
792 Hashtable cache = new Hashtable ();
793 cache.Add ("", null);
794 foreach (Assembly a in assemblies) {
795 foreach (Type t in a.GetExportedTypes ()) {
796 string ns = t.Namespace;
797 if (ns == null || cache.Contains (ns))
800 Namespace.LookupNamespace (ns, true);
801 cache.Add (ns, null);
808 /// Fills static table with exported types from all referenced assemblies.
809 /// This information is required for CLS Compliance tests.
811 public static void LoadAllImportedTypes ()
813 all_imported_types = new Hashtable ();
814 foreach (Assembly a in assemblies) {
815 foreach (Type t in a.GetExportedTypes ()) {
816 all_imported_types [t.FullName] = t;
821 public static bool NamespaceClash (string name, Location loc)
823 if (Namespace.LookupNamespace (name, false) == null)
826 Report.Error (519, loc, String.Format ("`{0}' clashes with a predefined namespace", name));
831 /// Returns the C# name of a type if possible, or the full type name otherwise
833 static public string CSharpName (Type t)
835 if (t.FullName == null)
838 return Regex.Replace (t.FullName,
840 @"(Int32|UInt32|Int16|UInt16|Int64|UInt64|" +
841 @"Single|Double|Char|Decimal|Byte|SByte|Object|" +
842 @"Boolean|String|Void|Null)" +
844 new MatchEvaluator (CSharpNameMatch));
847 static String CSharpNameMatch (Match match)
849 string s = match.Groups [1].Captures [0].Value;
851 Replace ("int32", "int").
852 Replace ("uint32", "uint").
853 Replace ("int16", "short").
854 Replace ("uint16", "ushort").
855 Replace ("int64", "long").
856 Replace ("uint64", "ulong").
857 Replace ("single", "float").
858 Replace ("boolean", "bool")
859 + match.Groups [2].Captures [0].Value;
863 /// Returns the signature of the method with full namespace classification
865 static public string GetFullNameSignature (MemberInfo mi)
867 return mi.DeclaringType.FullName.Replace ('+', '.') + '.' + mi.Name;
870 static public string GetFullNameSignature (MethodBase mb)
872 string name = mb.Name;
874 name = mb.DeclaringType.Name;
876 if (mb.IsSpecialName) {
877 if (name.StartsWith ("get_") || name.StartsWith ("set_")) {
878 name = name.Remove (0, 4);
885 return mb.DeclaringType.FullName.Replace ('+', '.') + '.' + name;
888 static public string GetFullName (Type t)
890 if (t.FullName == null)
893 string name = t.FullName.Replace ('+', '.');
895 DeclSpace tc = LookupDeclSpace (t);
896 if ((tc != null) && tc.IsGeneric) {
897 TypeParameter[] tparam = tc.TypeParameters;
899 StringBuilder sb = new StringBuilder (name);
901 for (int i = 0; i < tparam.Length; i++) {
904 sb.Append (tparam [i].Name);
907 return sb.ToString ();
908 } else if (t.HasGenericArguments && !t.IsGenericInstance) {
909 Type[] tparam = t.GetGenericArguments ();
911 StringBuilder sb = new StringBuilder (name);
913 for (int i = 0; i < tparam.Length; i++) {
916 sb.Append (tparam [i].Name);
919 return sb.ToString ();
926 /// Returns the signature of the property and indexer
928 static public string CSharpSignature (PropertyBuilder pb, bool is_indexer)
931 return GetFullNameSignature (pb);
934 MethodBase mb = pb.GetSetMethod (true) != null ? pb.GetSetMethod (true) : pb.GetGetMethod (true);
935 string signature = GetFullNameSignature (mb);
936 string arg = TypeManager.LookupParametersByBuilder (mb).ParameterDesc (0);
937 return String.Format ("{0}.this[{1}]", signature.Substring (0, signature.LastIndexOf ('.')), arg);
941 /// Returns the signature of the method
943 static public string CSharpSignature (MethodBase mb)
945 StringBuilder sig = new StringBuilder ("(");
948 // FIXME: We should really have a single function to do
949 // everything instead of the following 5 line pattern
951 ParameterData iparams = LookupParametersByBuilder (mb);
954 iparams = new ReflectionParameters (mb);
957 if (mb.IsSpecialName && iparams.Count == 0 && !mb.IsConstructor)
958 return GetFullNameSignature (mb);
960 for (int i = 0; i < iparams.Count; i++) {
964 sig.Append (iparams.ParameterDesc (i));
969 if (mb.IsSpecialName && iparams.Count == 1 && !mb.IsConstructor) {
970 sig.Replace ('(', '[');
971 sig.Replace (')', ']');
974 return GetFullNameSignature (mb) + sig.ToString ();
977 public static string GetMethodName (MethodInfo m)
979 if (!IsGenericMethod (m))
982 return MemberName.MakeName (m.Name, m.GetGenericArguments ().Length);
986 /// Looks up a type, and aborts if it is not found. This is used
987 /// by types required by the compiler
989 static Type CoreLookupType (string name)
991 Type t = LookupTypeDirect (name);
994 Report.Error (518, "The predefined type `" + name + "' is not defined or imported");
995 Environment.Exit (1);
1002 /// Returns the MethodInfo for a method named `name' defined
1003 /// in type `t' which takes arguments of types `args'
1005 static MethodInfo GetMethod (Type t, string name, Type [] args, bool is_private, bool report_errors)
1009 BindingFlags flags = instance_and_static | BindingFlags.Public;
1015 flags |= BindingFlags.NonPublic;
1017 list = FindMembers (t, MemberTypes.Method, flags, signature_filter, sig);
1018 if (list.Count == 0) {
1020 Report.Error (-19, "Can not find the core function `" + name + "'");
1024 MethodInfo mi = list [0] as MethodInfo;
1027 Report.Error (-19, "Can not find the core function `" + name + "'");
1034 static MethodInfo GetMethod (Type t, string name, Type [] args, bool report_errors)
1036 return GetMethod (t, name, args, false, report_errors);
1039 static MethodInfo GetMethod (Type t, string name, Type [] args)
1041 return GetMethod (t, name, args, true);
1046 /// Returns the ConstructorInfo for "args"
1048 static ConstructorInfo GetConstructor (Type t, Type [] args)
1056 list = FindMembers (t, MemberTypes.Constructor,
1057 instance_and_static | BindingFlags.Public | BindingFlags.DeclaredOnly,
1058 signature_filter, sig);
1059 if (list.Count == 0){
1060 Report.Error (-19, "Can not find the core constructor for type `" + t.Name + "'");
1064 ConstructorInfo ci = list [0] as ConstructorInfo;
1066 Report.Error (-19, "Can not find the core constructor for type `" + t.Name + "'");
1073 public static void InitEnumUnderlyingTypes ()
1076 int32_type = CoreLookupType ("System.Int32");
1077 int64_type = CoreLookupType ("System.Int64");
1078 uint32_type = CoreLookupType ("System.UInt32");
1079 uint64_type = CoreLookupType ("System.UInt64");
1080 byte_type = CoreLookupType ("System.Byte");
1081 sbyte_type = CoreLookupType ("System.SByte");
1082 short_type = CoreLookupType ("System.Int16");
1083 ushort_type = CoreLookupType ("System.UInt16");
1087 /// The types have to be initialized after the initial
1088 /// population of the type has happened (for example, to
1089 /// bootstrap the corlib.dll
1091 public static void InitCoreTypes ()
1093 object_type = CoreLookupType ("System.Object");
1094 value_type = CoreLookupType ("System.ValueType");
1096 InitEnumUnderlyingTypes ();
1098 char_type = CoreLookupType ("System.Char");
1099 string_type = CoreLookupType ("System.String");
1100 float_type = CoreLookupType ("System.Single");
1101 double_type = CoreLookupType ("System.Double");
1102 char_ptr_type = CoreLookupType ("System.Char*");
1103 decimal_type = CoreLookupType ("System.Decimal");
1104 bool_type = CoreLookupType ("System.Boolean");
1105 enum_type = CoreLookupType ("System.Enum");
1107 multicast_delegate_type = CoreLookupType ("System.MulticastDelegate");
1108 delegate_type = CoreLookupType ("System.Delegate");
1110 array_type = CoreLookupType ("System.Array");
1111 void_type = CoreLookupType ("System.Void");
1112 type_type = CoreLookupType ("System.Type");
1114 runtime_field_handle_type = CoreLookupType ("System.RuntimeFieldHandle");
1115 runtime_argument_handle_type = CoreLookupType ("System.RuntimeArgumentHandle");
1116 runtime_helpers_type = CoreLookupType ("System.Runtime.CompilerServices.RuntimeHelpers");
1117 default_member_type = CoreLookupType ("System.Reflection.DefaultMemberAttribute");
1118 runtime_handle_type = CoreLookupType ("System.RuntimeTypeHandle");
1119 asynccallback_type = CoreLookupType ("System.AsyncCallback");
1120 iasyncresult_type = CoreLookupType ("System.IAsyncResult");
1121 ienumerator_type = CoreLookupType ("System.Collections.IEnumerator");
1122 ienumerable_type = CoreLookupType ("System.Collections.IEnumerable");
1123 idisposable_type = CoreLookupType ("System.IDisposable");
1124 icloneable_type = CoreLookupType ("System.ICloneable");
1125 iconvertible_type = CoreLookupType ("System.IConvertible");
1126 monitor_type = CoreLookupType ("System.Threading.Monitor");
1127 intptr_type = CoreLookupType ("System.IntPtr");
1129 attribute_type = CoreLookupType ("System.Attribute");
1130 attribute_usage_type = CoreLookupType ("System.AttributeUsageAttribute");
1131 dllimport_type = CoreLookupType ("System.Runtime.InteropServices.DllImportAttribute");
1132 methodimpl_attr_type = CoreLookupType ("System.Runtime.CompilerServices.MethodImplAttribute");
1133 marshal_as_attr_type = CoreLookupType ("System.Runtime.InteropServices.MarshalAsAttribute");
1134 param_array_type = CoreLookupType ("System.ParamArrayAttribute");
1135 in_attribute_type = CoreLookupType ("System.Runtime.InteropServices.InAttribute");
1136 typed_reference_type = CoreLookupType ("System.TypedReference");
1137 arg_iterator_type = CoreLookupType ("System.ArgIterator");
1138 mbr_type = CoreLookupType ("System.MarshalByRefObject");
1139 decimal_constant_attribute_type = CoreLookupType ("System.Runtime.CompilerServices.DecimalConstantAttribute");
1142 // Sigh. Remove this before the release. Wonder what versions of Mono
1143 // people are running.
1145 guid_attr_type = LookupType ("System.Runtime.InteropServices.GuidAttribute");
1147 unverifiable_code_type= CoreLookupType ("System.Security.UnverifiableCodeAttribute");
1149 void_ptr_type = CoreLookupType ("System.Void*");
1151 indexer_name_type = CoreLookupType ("System.Runtime.CompilerServices.IndexerNameAttribute");
1153 exception_type = CoreLookupType ("System.Exception");
1154 invalid_operation_exception_type = CoreLookupType ("System.InvalidOperationException");
1155 not_supported_exception_type = CoreLookupType ("System.NotSupportedException");
1160 obsolete_attribute_type = CoreLookupType ("System.ObsoleteAttribute");
1161 conditional_attribute_type = CoreLookupType ("System.Diagnostics.ConditionalAttribute");
1162 cls_compliant_attribute_type = CoreLookupType ("System.CLSCompliantAttribute");
1163 struct_layout_attribute_type = CoreLookupType ("System.Runtime.InteropServices.StructLayoutAttribute");
1164 field_offset_attribute_type = CoreLookupType ("System.Runtime.InteropServices.FieldOffsetAttribute");
1165 security_attr_type = CoreLookupType ("System.Security.Permissions.SecurityAttribute");
1167 InitGenericCoreTypes ();
1170 // When compiling corlib, store the "real" types here.
1172 if (!RootContext.StdLib) {
1173 system_int32_type = typeof (System.Int32);
1174 system_array_type = typeof (System.Array);
1175 system_type_type = typeof (System.Type);
1176 system_assemblybuilder_type = typeof (System.Reflection.Emit.AssemblyBuilder);
1178 Type [] void_arg = { };
1179 system_int_array_get_length = GetMethod (
1180 system_array_type, "get_Length", void_arg);
1181 system_int_array_get_rank = GetMethod (
1182 system_array_type, "get_Rank", void_arg);
1183 system_object_array_clone = GetMethod (
1184 system_array_type, "Clone", void_arg);
1186 Type [] system_int_arg = { system_int32_type };
1187 system_int_array_get_length_int = GetMethod (
1188 system_array_type, "GetLength", system_int_arg);
1189 system_int_array_get_upper_bound_int = GetMethod (
1190 system_array_type, "GetUpperBound", system_int_arg);
1191 system_int_array_get_lower_bound_int = GetMethod (
1192 system_array_type, "GetLowerBound", system_int_arg);
1194 Type [] system_array_int_arg = { system_array_type, system_int32_type };
1195 system_void_array_copyto_array_int = GetMethod (
1196 system_array_type, "CopyTo", system_array_int_arg);
1198 Type [] system_3_type_arg = {
1199 system_type_type, system_type_type, system_type_type };
1200 Type [] system_4_type_arg = {
1201 system_type_type, system_type_type, system_type_type, system_type_type };
1203 MethodInfo set_corlib_type_builders = GetMethod (
1204 system_assemblybuilder_type, "SetCorlibTypeBuilders",
1205 system_4_type_arg, true, false);
1207 if (set_corlib_type_builders != null) {
1208 object[] args = new object [4];
1209 args [0] = object_type;
1210 args [1] = value_type;
1211 args [2] = enum_type;
1212 args [3] = void_type;
1214 set_corlib_type_builders.Invoke (CodeGen.Assembly.Builder, args);
1216 // Compatibility for an older version of the class libs.
1217 set_corlib_type_builders = GetMethod (
1218 system_assemblybuilder_type, "SetCorlibTypeBuilders",
1219 system_3_type_arg, true, true);
1221 if (set_corlib_type_builders == null) {
1222 Report.Error (-26, "Corlib compilation is not supported in Microsoft.NET due to bugs in it");
1226 object[] args = new object [3];
1227 args [0] = object_type;
1228 args [1] = value_type;
1229 args [2] = enum_type;
1231 set_corlib_type_builders.Invoke (CodeGen.Assembly.Builder, args);
1235 system_object_expr.Type = object_type;
1236 system_string_expr.Type = string_type;
1237 system_boolean_expr.Type = bool_type;
1238 system_decimal_expr.Type = decimal_type;
1239 system_single_expr.Type = float_type;
1240 system_double_expr.Type = double_type;
1241 system_sbyte_expr.Type = sbyte_type;
1242 system_byte_expr.Type = byte_type;
1243 system_int16_expr.Type = short_type;
1244 system_uint16_expr.Type = ushort_type;
1245 system_int32_expr.Type = int32_type;
1246 system_uint32_expr.Type = uint32_type;
1247 system_int64_expr.Type = int64_type;
1248 system_uint64_expr.Type = uint64_type;
1249 system_char_expr.Type = char_type;
1250 system_void_expr.Type = void_type;
1251 system_asynccallback_expr.Type = asynccallback_type;
1252 system_iasyncresult_expr.Type = iasyncresult_type;
1253 system_valuetype_expr.Type = value_type;
1256 // These are only used for compare purposes
1258 anonymous_method_type = typeof (AnonymousMethod);
1259 null_type = typeof (NullType);
1263 // The helper methods that are used by the compiler
1265 public static void InitCodeHelpers ()
1268 // Now load the default methods that we use.
1270 Type [] string_string = { string_type, string_type };
1271 string_concat_string_string = GetMethod (
1272 string_type, "Concat", string_string);
1273 Type [] string_string_string = { string_type, string_type, string_type };
1274 string_concat_string_string_string = GetMethod (
1275 string_type, "Concat", string_string_string);
1276 Type [] string_string_string_string = { string_type, string_type, string_type, string_type };
1277 string_concat_string_string_string_string = GetMethod (
1278 string_type, "Concat", string_string_string_string);
1279 Type[] params_string = { TypeManager.LookupType ("System.String[]") };
1280 string_concat_string_dot_dot_dot = GetMethod (
1281 string_type, "Concat", params_string);
1283 Type [] object_object = { object_type, object_type };
1284 string_concat_object_object = GetMethod (
1285 string_type, "Concat", object_object);
1286 Type [] object_object_object = { object_type, object_type, object_type };
1287 string_concat_object_object_object = GetMethod (
1288 string_type, "Concat", object_object_object);
1289 Type[] params_object = { TypeManager.LookupType ("System.Object[]") };
1290 string_concat_object_dot_dot_dot = GetMethod (
1291 string_type, "Concat", params_object);
1293 Type [] string_ = { string_type };
1294 string_isinterneted_string = GetMethod (
1295 string_type, "IsInterned", string_);
1297 Type [] runtime_type_handle = { runtime_handle_type };
1298 system_type_get_type_from_handle = GetMethod (
1299 type_type, "GetTypeFromHandle", runtime_type_handle);
1301 Type [] delegate_delegate = { delegate_type, delegate_type };
1302 delegate_combine_delegate_delegate = GetMethod (
1303 delegate_type, "Combine", delegate_delegate);
1305 delegate_remove_delegate_delegate = GetMethod (
1306 delegate_type, "Remove", delegate_delegate);
1311 Type [] void_arg = { };
1312 object_getcurrent_void = GetMethod (
1313 ienumerator_type, "get_Current", void_arg);
1314 bool_movenext_void = GetMethod (
1315 ienumerator_type, "MoveNext", void_arg);
1316 void_reset_void = GetMethod (
1317 ienumerator_type, "Reset", void_arg);
1318 void_dispose_void = GetMethod (
1319 idisposable_type, "Dispose", void_arg);
1320 int_get_offset_to_string_data = GetMethod (
1321 runtime_helpers_type, "get_OffsetToStringData", void_arg);
1322 int_array_get_length = GetMethod (
1323 array_type, "get_Length", void_arg);
1324 int_array_get_rank = GetMethod (
1325 array_type, "get_Rank", void_arg);
1326 ienumerable_getenumerator_void = GetMethod (
1327 ienumerable_type, "GetEnumerator", void_arg);
1332 Type [] int_arg = { int32_type };
1333 int_array_get_length_int = GetMethod (
1334 array_type, "GetLength", int_arg);
1335 int_array_get_upper_bound_int = GetMethod (
1336 array_type, "GetUpperBound", int_arg);
1337 int_array_get_lower_bound_int = GetMethod (
1338 array_type, "GetLowerBound", int_arg);
1341 // System.Array methods
1343 object_array_clone = GetMethod (
1344 array_type, "Clone", void_arg);
1345 Type [] array_int_arg = { array_type, int32_type };
1346 void_array_copyto_array_int = GetMethod (
1347 array_type, "CopyTo", array_int_arg);
1352 Type [] object_arg = { object_type };
1353 void_monitor_enter_object = GetMethod (
1354 monitor_type, "Enter", object_arg);
1355 void_monitor_exit_object = GetMethod (
1356 monitor_type, "Exit", object_arg);
1358 Type [] array_field_handle_arg = { array_type, runtime_field_handle_type };
1360 void_initializearray_array_fieldhandle = GetMethod (
1361 runtime_helpers_type, "InitializeArray", array_field_handle_arg);
1366 int_getlength_int = GetMethod (
1367 array_type, "GetLength", int_arg);
1370 // Decimal constructors
1372 Type [] dec_arg = { int32_type, int32_type, int32_type, bool_type, byte_type };
1373 void_decimal_ctor_five_args = GetConstructor (
1374 decimal_type, dec_arg);
1376 void_decimal_ctor_int_arg = GetConstructor (decimal_type, int_arg);
1381 cons_param_array_attribute = GetConstructor (
1382 param_array_type, void_arg);
1384 unverifiable_code_ctor = GetConstructor (
1385 unverifiable_code_type, void_arg);
1387 decimal_constant_attribute_ctor = GetConstructor (decimal_constant_attribute_type, new Type []
1388 { byte_type, byte_type, uint32_type, uint32_type, uint32_type } );
1390 default_member_ctor = GetConstructor (default_member_type, string_);
1393 // InvalidOperationException
1395 invalid_operation_ctor = GetConstructor (
1396 invalid_operation_exception_type, void_arg);
1400 object_ctor = GetConstructor (object_type, void_arg);
1402 InitGenericCodeHelpers ();
1405 const BindingFlags instance_and_static = BindingFlags.Static | BindingFlags.Instance;
1408 /// This is the "old", non-cache based FindMembers() function. We cannot use
1409 /// the cache here because there is no member name argument.
1411 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1412 MemberFilter filter, object criteria)
1414 DeclSpace decl = (DeclSpace) builder_to_declspace [t];
1417 // `builder_to_declspace' contains all dynamic types.
1421 Timer.StartTimer (TimerType.FindMembers);
1422 list = decl.FindMembers (mt, bf, filter, criteria);
1423 Timer.StopTimer (TimerType.FindMembers);
1428 // We have to take care of arrays specially, because GetType on
1429 // a TypeBuilder array will return a Type, not a TypeBuilder,
1430 // and we can not call FindMembers on this type.
1432 if (t.IsSubclassOf (TypeManager.array_type))
1433 return new MemberList (TypeManager.array_type.FindMembers (mt, bf, filter, criteria));
1435 if (t is GenericTypeParameterBuilder) {
1436 TypeParameter tparam = (TypeParameter) builder_to_type_param [t];
1438 Timer.StartTimer (TimerType.FindMembers);
1439 MemberList list = tparam.FindMembers (
1440 mt, bf | BindingFlags.DeclaredOnly, filter, criteria);
1441 Timer.StopTimer (TimerType.FindMembers);
1446 // Since FindMembers will not lookup both static and instance
1447 // members, we emulate this behaviour here.
1449 if ((bf & instance_and_static) == instance_and_static){
1450 MemberInfo [] i_members = t.FindMembers (
1451 mt, bf & ~BindingFlags.Static, filter, criteria);
1453 int i_len = i_members.Length;
1455 MemberInfo one = i_members [0];
1458 // If any of these are present, we are done!
1460 if ((one is Type) || (one is EventInfo) || (one is FieldInfo))
1461 return new MemberList (i_members);
1464 MemberInfo [] s_members = t.FindMembers (
1465 mt, bf & ~BindingFlags.Instance, filter, criteria);
1467 int s_len = s_members.Length;
1468 if (i_len > 0 || s_len > 0)
1469 return new MemberList (i_members, s_members);
1472 return new MemberList (i_members);
1474 return new MemberList (s_members);
1478 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
1483 /// This method is only called from within MemberLookup. It tries to use the member
1484 /// cache if possible and falls back to the normal FindMembers if not. The `used_cache'
1485 /// flag tells the caller whether we used the cache or not. If we used the cache, then
1486 /// our return value will already contain all inherited members and the caller don't need
1487 /// to check base classes and interfaces anymore.
1489 private static MemberInfo [] MemberLookup_FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1490 string name, out bool used_cache)
1495 // We have to take care of arrays specially, because GetType on
1496 // a TypeBuilder array will return a Type, not a TypeBuilder,
1497 // and we can not call FindMembers on this type.
1499 if (t == TypeManager.array_type || t.IsSubclassOf (TypeManager.array_type)) {
1501 return TypeHandle.ArrayType.MemberCache.FindMembers (
1502 mt, bf, name, FilterWithClosure_delegate, null);
1506 // If this is a dynamic type, it's always in the `builder_to_declspace' hash table
1507 // and we can ask the DeclSpace for the MemberCache.
1509 if (t is TypeBuilder) {
1510 DeclSpace decl = (DeclSpace) builder_to_declspace [t];
1511 cache = decl.MemberCache;
1514 // If this DeclSpace has a MemberCache, use it.
1517 if (cache != null) {
1519 return cache.FindMembers (
1520 mt, bf, name, FilterWithClosure_delegate, null);
1523 // If there is no MemberCache, we need to use the "normal" FindMembers.
1524 // Note, this is a VERY uncommon route!
1527 Timer.StartTimer (TimerType.FindMembers);
1528 list = decl.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
1529 FilterWithClosure_delegate, name);
1530 Timer.StopTimer (TimerType.FindMembers);
1532 return (MemberInfo []) list;
1535 if (t is GenericTypeParameterBuilder) {
1536 TypeParameter tparam = (TypeParameter) builder_to_type_param [t];
1539 Timer.StartTimer (TimerType.FindMembers);
1540 list = tparam.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
1541 FilterWithClosure_delegate, name);
1542 Timer.StopTimer (TimerType.FindMembers);
1544 return (MemberInfo []) list;
1548 // This call will always succeed. There is exactly one TypeHandle instance per
1549 // type, TypeHandle.GetMemberCache() will, if necessary, create a new one, and return
1550 // the corresponding MemberCache.
1552 cache = TypeHandle.GetMemberCache (t);
1555 return cache.FindMembers (mt, bf, name, FilterWithClosure_delegate, null);
1558 public static bool IsBuiltinType (Type t)
1560 if (t == object_type || t == string_type || t == int32_type || t == uint32_type ||
1561 t == int64_type || t == uint64_type || t == float_type || t == double_type ||
1562 t == char_type || t == short_type || t == decimal_type || t == bool_type ||
1563 t == sbyte_type || t == byte_type || t == ushort_type || t == void_type)
1569 public static bool IsBuiltinType (TypeContainer tc)
1571 return IsBuiltinType (tc.TypeBuilder);
1575 // This is like IsBuiltinType, but lacks decimal_type, we should also clean up
1576 // the pieces in the code where we use IsBuiltinType and special case decimal_type.
1578 public static bool IsCLRType (Type t)
1580 if (t == object_type || t == int32_type || t == uint32_type ||
1581 t == int64_type || t == uint64_type || t == float_type || t == double_type ||
1582 t == char_type || t == short_type || t == bool_type ||
1583 t == sbyte_type || t == byte_type || t == ushort_type)
1589 public static bool IsDelegateType (Type t)
1591 if (t.IsGenericInstance)
1592 t = t.GetGenericTypeDefinition ();
1594 if (t.IsSubclassOf (TypeManager.delegate_type))
1600 public static bool IsEnumType (Type t)
1602 if (t.IsSubclassOf (TypeManager.enum_type))
1607 public static bool IsBuiltinOrEnum (Type t)
1609 if (IsBuiltinType (t))
1618 public static bool IsNullType (Type t)
1620 return t == null_type;
1624 // Whether a type is unmanaged. This is used by the unsafe code (25.2)
1626 public static bool IsUnmanagedType (Type t)
1628 if (IsBuiltinType (t) && t != TypeManager.string_type)
1637 if (IsValueType (t)){
1638 if (t is TypeBuilder){
1639 TypeContainer tc = LookupTypeContainer (t);
1641 if (tc.Fields != null){
1642 foreach (Field f in tc.Fields){
1643 if (f.FieldBuilder.IsStatic)
1645 if (!IsUnmanagedType (f.FieldBuilder.FieldType))
1651 FieldInfo [] fields = t.GetFields ();
1653 foreach (FieldInfo f in fields){
1656 if (!IsUnmanagedType (f.FieldType))
1666 public static bool IsValueType (Type t)
1668 return t.IsGenericParameter || t.IsValueType;
1671 public static bool IsInterfaceType (Type t)
1673 TypeContainer tc = (TypeContainer) builder_to_declspace [t];
1677 return tc.Kind == Kind.Interface;
1680 public static bool IsSubclassOf (Type type, Type parent)
1682 TypeParameter tparam = LookupTypeParameter (type);
1683 TypeParameter pparam = LookupTypeParameter (parent);
1685 if ((tparam != null) && (pparam != null)) {
1686 if (tparam == pparam)
1689 return tparam.IsSubclassOf (parent);
1693 if (type.Equals (parent))
1696 type = type.BaseType;
1697 } while (type != null);
1702 public static bool IsPrivateAccessible (Type type, Type parent)
1704 if (type.Equals (parent))
1707 if ((type is TypeBuilder) && type.IsGenericTypeDefinition && parent.IsGenericInstance) {
1709 // `a' is a generic type definition's TypeBuilder and `b' is a
1710 // generic instance of the same type.
1716 // void Test (Stack<T> stack) { }
1719 // The first argument of `Test' will be the generic instance
1720 // "Stack<!0>" - which is the same type than the "Stack" TypeBuilder.
1723 // We hit this via Closure.Filter() for gen-82.cs.
1725 if (type != parent.GetGenericTypeDefinition ())
1731 if (type.IsGenericInstance && parent.IsGenericInstance) {
1732 Type tdef = type.GetGenericTypeDefinition ();
1733 Type pdef = parent.GetGenericTypeDefinition ();
1735 if (type.GetGenericTypeDefinition () != parent.GetGenericTypeDefinition ())
1744 public static bool IsFamilyAccessible (Type type, Type parent)
1746 TypeParameter tparam = LookupTypeParameter (type);
1747 TypeParameter pparam = LookupTypeParameter (parent);
1749 if ((tparam != null) && (pparam != null)) {
1750 if (tparam == pparam)
1753 return tparam.IsSubclassOf (parent);
1757 if (IsEqualGenericInstance (type, parent))
1760 type = type.BaseType;
1761 } while (type != null);
1767 // Checks whether `type' is a subclass or nested child of `parent'.
1769 public static bool IsNestedFamilyAccessible (Type type, Type parent)
1772 if (IsFamilyAccessible (type, parent))
1775 // Handle nested types.
1776 type = type.DeclaringType;
1777 } while (type != null);
1783 // Checks whether `type' is a nested child of `parent'.
1785 public static bool IsNestedChildOf (Type type, Type parent)
1787 if (IsEqual (type, parent))
1790 type = type.DeclaringType;
1791 while (type != null) {
1792 if (IsEqual (type, parent))
1795 type = type.DeclaringType;
1802 // Do the right thing when returning the element type of an
1803 // array type based on whether we are compiling corlib or not
1805 public static Type GetElementType (Type t)
1807 if (RootContext.StdLib)
1808 return t.GetElementType ();
1810 return TypeToCoreType (t.GetElementType ());
1814 /// Returns the User Defined Types
1816 public static ArrayList UserTypes {
1822 public static Hashtable TypeContainers {
1824 return typecontainers;
1828 static Hashtable builder_to_constant;
1830 public static void RegisterConstant (FieldBuilder fb, Const c)
1832 if (builder_to_constant == null)
1833 builder_to_constant = new PtrHashtable ();
1835 if (builder_to_constant.Contains (fb))
1838 builder_to_constant.Add (fb, c);
1841 public static Const LookupConstant (FieldBuilder fb)
1843 if (builder_to_constant == null)
1846 return (Const) builder_to_constant [fb];
1850 /// Gigantic work around for missing features in System.Reflection.Emit follows.
1854 /// Since System.Reflection.Emit can not return MethodBase.GetParameters
1855 /// for anything which is dynamic, and we need this in a number of places,
1856 /// we register this information here, and use it afterwards.
1858 static public void RegisterMethod (MethodBase mb, InternalParameters ip, Type [] args)
1863 method_arguments.Add (mb, args);
1864 method_internal_params.Add (mb, ip);
1867 static public InternalParameters LookupParametersByBuilder (MethodBase mb)
1869 if (! (mb is ConstructorBuilder || mb is MethodBuilder))
1872 if (method_internal_params.Contains (mb))
1873 return (InternalParameters) method_internal_params [mb];
1875 throw new Exception ("Argument for Method not registered" + mb);
1879 /// Returns the argument types for a method based on its methodbase
1881 /// For dynamic methods, we use the compiler provided types, for
1882 /// methods from existing assemblies we load them from GetParameters,
1883 /// and insert them into the cache
1885 static public Type [] GetArgumentTypes (MethodBase mb)
1887 object t = method_arguments [mb];
1891 ParameterInfo [] pi = mb.GetParameters ();
1898 types = new Type [c];
1899 for (int i = 0; i < c; i++)
1900 types [i] = pi [i].ParameterType;
1902 method_arguments.Add (mb, types);
1907 /// Returns the argument types for an indexer based on its PropertyInfo
1909 /// For dynamic indexers, we use the compiler provided types, for
1910 /// indexers from existing assemblies we load them from GetParameters,
1911 /// and insert them into the cache
1913 static public Type [] GetArgumentTypes (PropertyInfo indexer)
1915 if (indexer_arguments.Contains (indexer))
1916 return (Type []) indexer_arguments [indexer];
1917 else if (indexer is PropertyBuilder)
1918 // If we're a PropertyBuilder and not in the
1919 // `indexer_arguments' hash, then we're a property and
1923 ParameterInfo [] pi = indexer.GetIndexParameters ();
1924 // Property, not an indexer.
1928 Type [] types = new Type [c];
1930 for (int i = 0; i < c; i++)
1931 types [i] = pi [i].ParameterType;
1933 indexer_arguments.Add (indexer, types);
1939 // This is a workaround the fact that GetValue is not
1940 // supported for dynamic types
1942 static Hashtable fields = new Hashtable ();
1943 static public bool RegisterFieldValue (FieldBuilder fb, object value)
1945 if (fields.Contains (fb))
1948 fields.Add (fb, value);
1953 static public object GetValue (FieldBuilder fb)
1958 static Hashtable fieldbuilders_to_fields = new Hashtable ();
1959 static public bool RegisterFieldBase (FieldBuilder fb, FieldBase f)
1961 if (fieldbuilders_to_fields.Contains (fb))
1964 fieldbuilders_to_fields.Add (fb, f);
1969 // The return value can be null; This will be the case for
1970 // auxiliary FieldBuilders created by the compiler that have no
1971 // real field being declared on the source code
1973 static public FieldBase GetField (FieldInfo fb)
1975 return (FieldBase) fieldbuilders_to_fields [fb];
1978 static Hashtable events;
1980 static public void RegisterEvent (MyEventBuilder eb, MethodBase add, MethodBase remove)
1983 events = new Hashtable ();
1985 if (!events.Contains (eb)) {
1986 events.Add (eb, new Pair (add, remove));
1990 static public MethodInfo GetAddMethod (EventInfo ei)
1992 if (ei is MyEventBuilder) {
1993 Pair pair = (Pair) events [ei];
1995 return (MethodInfo) pair.First;
1997 return ei.GetAddMethod (true);
2000 static public MethodInfo GetRemoveMethod (EventInfo ei)
2002 if (ei is MyEventBuilder) {
2003 Pair pair = (Pair) events [ei];
2005 return (MethodInfo) pair.Second;
2007 return ei.GetRemoveMethod (true);
2010 static Hashtable priv_fields_events;
2012 static public bool RegisterPrivateFieldOfEvent (EventInfo einfo, FieldBuilder builder)
2014 if (priv_fields_events == null)
2015 priv_fields_events = new Hashtable ();
2017 if (priv_fields_events.Contains (einfo))
2020 priv_fields_events.Add (einfo, builder);
2025 static public MemberInfo GetPrivateFieldOfEvent (EventInfo ei)
2027 if (priv_fields_events == null)
2030 return (MemberInfo) priv_fields_events [ei];
2033 static Hashtable properties;
2035 static public bool RegisterProperty (PropertyBuilder pb, MethodBase get, MethodBase set)
2037 if (properties == null)
2038 properties = new Hashtable ();
2040 if (properties.Contains (pb))
2043 properties.Add (pb, new Pair (get, set));
2048 static public bool RegisterIndexer (PropertyBuilder pb, MethodBase get,
2049 MethodBase set, Type[] args)
2051 if (!RegisterProperty (pb, get,set))
2054 indexer_arguments.Add (pb, args);
2059 public static bool CheckStructCycles (TypeContainer tc, Hashtable seen)
2061 Hashtable hash = new Hashtable ();
2062 return CheckStructCycles (tc, seen, hash);
2065 public static bool CheckStructCycles (TypeContainer tc, Hashtable seen,
2068 if ((tc.Kind != Kind.Struct) || IsBuiltinType (tc))
2072 // `seen' contains all types we've already visited.
2074 if (seen.Contains (tc))
2076 seen.Add (tc, null);
2078 if (tc.Fields == null)
2081 foreach (Field field in tc.Fields) {
2082 if (field.FieldBuilder.IsStatic)
2085 Type ftype = field.FieldBuilder.FieldType;
2086 TypeContainer ftc = LookupTypeContainer (ftype);
2090 if (hash.Contains (ftc)) {
2091 Report.Error (523, tc.Location,
2092 "Struct member `{0}.{1}' of type `{2}' " +
2093 "causes a cycle in the struct layout",
2094 tc.Name, field.Name, ftc.Name);
2099 // `hash' contains all types in the current path.
2101 hash.Add (tc, null);
2103 bool ok = CheckStructCycles (ftc, seen, hash);
2110 if (!seen.Contains (ftc))
2111 seen.Add (ftc, null);
2118 /// Given an array of interface types, expand and eliminate repeated ocurrences
2119 /// of an interface.
2123 /// This expands in context like: IA; IB : IA; IC : IA, IB; the interface "IC" to
2126 public static Type[] ExpandInterfaces (EmitContext ec, TypeExpr [] base_interfaces)
2128 ArrayList new_ifaces = new ArrayList ();
2130 foreach (TypeExpr iface in base_interfaces){
2131 TypeExpr texpr = iface.ResolveAsTypeTerminal (ec);
2135 if (!new_ifaces.Contains (texpr.Type))
2136 new_ifaces.Add (texpr.Type);
2138 Type [] implementing = texpr.Type.GetInterfaces ();
2140 foreach (Type imp in implementing){
2141 if (!new_ifaces.Contains (imp))
2142 new_ifaces.Add (imp);
2145 Type [] ret = new Type [new_ifaces.Count];
2146 new_ifaces.CopyTo (ret, 0);
2150 static PtrHashtable iface_cache = new PtrHashtable ();
2153 /// This function returns the interfaces in the type `t'. Works with
2154 /// both types and TypeBuilders.
2156 public static Type [] GetInterfaces (Type t)
2159 Type [] cached = iface_cache [t] as Type [];
2164 // The reason for catching the Array case is that Reflection.Emit
2165 // will not return a TypeBuilder for Array types of TypeBuilder types,
2166 // but will still throw an exception if we try to call GetInterfaces
2169 // Since the array interfaces are always constant, we return those for
2174 t = TypeManager.array_type;
2176 if (t is TypeBuilder){
2177 Type[] parent_ifaces;
2179 if (t.BaseType == null)
2180 parent_ifaces = NoTypes;
2182 parent_ifaces = GetInterfaces (t.BaseType);
2183 Type[] type_ifaces = (Type []) builder_to_ifaces [t];
2184 if (type_ifaces == null)
2185 type_ifaces = NoTypes;
2187 int parent_count = parent_ifaces.Length;
2188 Type[] result = new Type [parent_count + type_ifaces.Length];
2189 parent_ifaces.CopyTo (result, 0);
2190 type_ifaces.CopyTo (result, parent_count);
2192 iface_cache [t] = result;
2194 } else if (t is GenericTypeParameterBuilder){
2195 Type[] type_ifaces = (Type []) builder_to_ifaces [t];
2196 if (type_ifaces == null)
2197 type_ifaces = NoTypes;
2199 iface_cache [t] = type_ifaces;
2202 Type[] ifaces = t.GetInterfaces ();
2203 iface_cache [t] = ifaces;
2209 // gets the interfaces that are declared explicitly on t
2211 public static Type [] GetExplicitInterfaces (TypeBuilder t)
2213 return (Type []) builder_to_ifaces [t];
2217 /// The following is used to check if a given type implements an interface.
2218 /// The cache helps us reduce the expense of hitting Type.GetInterfaces everytime.
2220 public static bool ImplementsInterface (Type t, Type iface)
2225 // FIXME OPTIMIZATION:
2226 // as soon as we hit a non-TypeBuiler in the interface
2227 // chain, we could return, as the `Type.GetInterfaces'
2228 // will return all the interfaces implement by the type
2232 interfaces = GetInterfaces (t);
2234 if (interfaces != null){
2235 foreach (Type i in interfaces){
2242 } while (t != null);
2247 static NumberFormatInfo nf_provider = CultureInfo.CurrentCulture.NumberFormat;
2249 // This is a custom version of Convert.ChangeType() which works
2250 // with the TypeBuilder defined types when compiling corlib.
2251 public static object ChangeType (object value, Type conversionType, out bool error)
2253 IConvertible convert_value = value as IConvertible;
2255 if (convert_value == null){
2261 // We must use Type.Equals() here since `conversionType' is
2262 // the TypeBuilder created version of a system type and not
2263 // the system type itself. You cannot use Type.GetTypeCode()
2264 // on such a type - it'd always return TypeCode.Object.
2268 if (conversionType.Equals (typeof (Boolean)))
2269 return (object)(convert_value.ToBoolean (nf_provider));
2270 else if (conversionType.Equals (typeof (Byte)))
2271 return (object)(convert_value.ToByte (nf_provider));
2272 else if (conversionType.Equals (typeof (Char)))
2273 return (object)(convert_value.ToChar (nf_provider));
2274 else if (conversionType.Equals (typeof (DateTime)))
2275 return (object)(convert_value.ToDateTime (nf_provider));
2276 else if (conversionType.Equals (TypeManager.decimal_type)) // typeof (Decimal)))
2277 return (object)(convert_value.ToDecimal (nf_provider));
2278 else if (conversionType.Equals (typeof (Double)))
2279 return (object)(convert_value.ToDouble (nf_provider));
2280 else if (conversionType.Equals (typeof (Int16)))
2281 return (object)(convert_value.ToInt16 (nf_provider));
2282 else if (conversionType.Equals (typeof (Int32)))
2283 return (object)(convert_value.ToInt32 (nf_provider));
2284 else if (conversionType.Equals (typeof (Int64)))
2285 return (object)(convert_value.ToInt64 (nf_provider));
2286 else if (conversionType.Equals (typeof (SByte)))
2287 return (object)(convert_value.ToSByte (nf_provider));
2288 else if (conversionType.Equals (typeof (Single)))
2289 return (object)(convert_value.ToSingle (nf_provider));
2290 else if (conversionType.Equals (typeof (String)))
2291 return (object)(convert_value.ToString (nf_provider));
2292 else if (conversionType.Equals (typeof (UInt16)))
2293 return (object)(convert_value.ToUInt16 (nf_provider));
2294 else if (conversionType.Equals (typeof (UInt32)))
2295 return (object)(convert_value.ToUInt32 (nf_provider));
2296 else if (conversionType.Equals (typeof (UInt64)))
2297 return (object)(convert_value.ToUInt64 (nf_provider));
2298 else if (conversionType.Equals (typeof (Object)))
2299 return (object)(value);
2309 // This is needed, because enumerations from assemblies
2310 // do not report their underlyingtype, but they report
2313 public static Type EnumToUnderlying (Type t)
2315 if (t == TypeManager.enum_type)
2318 t = t.UnderlyingSystemType;
2319 if (!TypeManager.IsEnumType (t))
2322 if (t is TypeBuilder) {
2323 // slow path needed to compile corlib
2324 if (t == TypeManager.bool_type ||
2325 t == TypeManager.byte_type ||
2326 t == TypeManager.sbyte_type ||
2327 t == TypeManager.char_type ||
2328 t == TypeManager.short_type ||
2329 t == TypeManager.ushort_type ||
2330 t == TypeManager.int32_type ||
2331 t == TypeManager.uint32_type ||
2332 t == TypeManager.int64_type ||
2333 t == TypeManager.uint64_type)
2335 throw new Exception ("Unhandled typecode in enum " + " from " + t.AssemblyQualifiedName);
2337 TypeCode tc = Type.GetTypeCode (t);
2340 case TypeCode.Boolean:
2341 return TypeManager.bool_type;
2343 return TypeManager.byte_type;
2344 case TypeCode.SByte:
2345 return TypeManager.sbyte_type;
2347 return TypeManager.char_type;
2348 case TypeCode.Int16:
2349 return TypeManager.short_type;
2350 case TypeCode.UInt16:
2351 return TypeManager.ushort_type;
2352 case TypeCode.Int32:
2353 return TypeManager.int32_type;
2354 case TypeCode.UInt32:
2355 return TypeManager.uint32_type;
2356 case TypeCode.Int64:
2357 return TypeManager.int64_type;
2358 case TypeCode.UInt64:
2359 return TypeManager.uint64_type;
2361 throw new Exception ("Unhandled typecode in enum " + tc + " from " + t.AssemblyQualifiedName);
2365 // When compiling corlib and called with one of the core types, return
2366 // the corresponding typebuilder for that type.
2368 public static Type TypeToCoreType (Type t)
2370 if (RootContext.StdLib || (t is TypeBuilder))
2373 TypeCode tc = Type.GetTypeCode (t);
2376 case TypeCode.Boolean:
2377 return TypeManager.bool_type;
2379 return TypeManager.byte_type;
2380 case TypeCode.SByte:
2381 return TypeManager.sbyte_type;
2383 return TypeManager.char_type;
2384 case TypeCode.Int16:
2385 return TypeManager.short_type;
2386 case TypeCode.UInt16:
2387 return TypeManager.ushort_type;
2388 case TypeCode.Int32:
2389 return TypeManager.int32_type;
2390 case TypeCode.UInt32:
2391 return TypeManager.uint32_type;
2392 case TypeCode.Int64:
2393 return TypeManager.int64_type;
2394 case TypeCode.UInt64:
2395 return TypeManager.uint64_type;
2396 case TypeCode.Single:
2397 return TypeManager.float_type;
2398 case TypeCode.Double:
2399 return TypeManager.double_type;
2400 case TypeCode.String:
2401 return TypeManager.string_type;
2402 case TypeCode.Decimal:
2403 return TypeManager.decimal_type;
2405 if (t == typeof (void))
2406 return TypeManager.void_type;
2407 if (t == typeof (object))
2408 return TypeManager.object_type;
2409 if (t == typeof (System.Type))
2410 return TypeManager.type_type;
2411 if (t == typeof (System.IntPtr))
2412 return TypeManager.intptr_type;
2418 /// Utility function that can be used to probe whether a type
2419 /// is managed or not.
2421 public static bool VerifyUnManaged (Type t, Location loc)
2423 if (t.IsValueType || t.IsPointer){
2425 // FIXME: this is more complex, we actually need to
2426 // make sure that the type does not contain any
2432 if (!RootContext.StdLib && (t == TypeManager.decimal_type))
2433 // We need this explicit check here to make it work when
2434 // compiling corlib.
2439 "Cannot take the address or size of a variable of a managed type ('" +
2440 CSharpName (t) + "')");
2445 /// Returns the name of the indexer in a given type.
2448 /// The default is not always `Item'. The user can change this behaviour by
2449 /// using the IndexerNameAttribute in the container.
2451 /// For example, the String class indexer is named `Chars' not `Item'
2453 public static string IndexerPropertyName (Type t)
2455 if (t.IsGenericInstance)
2456 t = t.GetGenericTypeDefinition ();
2458 if (t is TypeBuilder) {
2459 TypeContainer tc = t.IsInterface ? LookupInterface (t) : LookupTypeContainer (t);
2460 return tc == null ? TypeContainer.DefaultIndexerName : tc.IndexerName;
2463 System.Attribute attr = System.Attribute.GetCustomAttribute (
2464 t, TypeManager.default_member_type);
2466 DefaultMemberAttribute dma = (DefaultMemberAttribute) attr;
2467 return dma.MemberName;
2470 return TypeContainer.DefaultIndexerName;
2473 static MethodInfo declare_local_method = null;
2475 public static LocalBuilder DeclareLocalPinned (ILGenerator ig, Type t)
2477 if (declare_local_method == null){
2478 declare_local_method = typeof (ILGenerator).GetMethod (
2480 BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
2482 new Type [] { typeof (Type), typeof (bool)},
2484 if (declare_local_method == null){
2485 Report.Warning (-24, new Location (-1),
2486 "This version of the runtime does not support making pinned local variables. " +
2487 "This code may cause errors on a runtime with a moving GC");
2488 return ig.DeclareLocal (t);
2491 return (LocalBuilder) declare_local_method.Invoke (ig, new object [] { t, true });
2495 // Returns whether the array of memberinfos contains the given method
2497 public static bool ArrayContainsMethod (MemberInfo [] array, MethodBase new_method)
2499 Type [] new_args = TypeManager.GetArgumentTypes (new_method);
2501 foreach (MethodBase method in array) {
2502 if (method.Name != new_method.Name)
2505 if (method is MethodInfo && new_method is MethodInfo)
2506 if (((MethodInfo) method).ReturnType != ((MethodInfo) new_method).ReturnType)
2510 Type [] old_args = TypeManager.GetArgumentTypes (method);
2511 int old_count = old_args.Length;
2514 if (new_args.Length != old_count)
2517 for (i = 0; i < old_count; i++){
2518 if (old_args [i] != new_args [i])
2531 // We copy methods from `new_members' into `target_list' if the signature
2532 // for the method from in the new list does not exist in the target_list
2534 // The name is assumed to be the same.
2536 public static ArrayList CopyNewMethods (ArrayList target_list, IList new_members)
2538 if (target_list == null){
2539 target_list = new ArrayList ();
2541 foreach (MemberInfo mi in new_members){
2542 if (mi is MethodBase)
2543 target_list.Add (mi);
2548 MemberInfo [] target_array = new MemberInfo [target_list.Count];
2549 target_list.CopyTo (target_array, 0);
2551 foreach (MemberInfo mi in new_members){
2552 MethodBase new_method = (MethodBase) mi;
2554 if (!ArrayContainsMethod (target_array, new_method))
2555 target_list.Add (new_method);
2560 #region MemberLookup implementation
2563 // Whether we allow private members in the result (since FindMembers
2564 // uses NonPublic for both protected and private), we need to distinguish.
2567 static internal bool FilterNone (MemberInfo m, object filter_criteria)
2572 internal class Closure {
2573 internal bool private_ok;
2575 // Who is invoking us and which type is being queried currently.
2576 internal Type invocation_type;
2577 internal Type qualifier_type;
2579 // The assembly that defines the type is that is calling us
2580 internal Assembly invocation_assembly;
2581 internal IList almost_match;
2583 private bool CheckValidFamilyAccess (bool is_static, MemberInfo m)
2585 if (invocation_type == null)
2591 // A nested class has access to all the protected members visible
2593 if (qualifier_type != null
2594 && TypeManager.IsNestedChildOf (invocation_type, qualifier_type))
2597 if (invocation_type == m.DeclaringType
2598 || invocation_type.IsSubclassOf (m.DeclaringType)) {
2599 // Although a derived class can access protected members of
2600 // its base class it cannot do so through an instance of the
2601 // base class (CS1540).
2602 // => Ancestry should be: declaring_type ->* invocation_type
2603 // ->* qualified_type
2604 if (qualifier_type == null
2605 || qualifier_type == invocation_type
2606 || qualifier_type.IsSubclassOf (invocation_type))
2610 if (almost_match != null)
2611 almost_match.Add (m);
2615 bool Filter (MethodBase mb, object filter_criteria)
2617 MethodAttributes ma = mb.Attributes & MethodAttributes.MemberAccessMask;
2619 if (ma == MethodAttributes.Private)
2620 return private_ok ||
2621 IsPrivateAccessible (invocation_type, mb.DeclaringType) ||
2622 IsNestedChildOf (invocation_type, mb.DeclaringType);
2625 // FamAndAssem requires that we not only derivate, but we are on the
2628 if (ma == MethodAttributes.FamANDAssem){
2629 if (invocation_assembly != mb.DeclaringType.Assembly)
2633 // Assembly and FamORAssem succeed if we're in the same assembly.
2634 if ((ma == MethodAttributes.Assembly) || (ma == MethodAttributes.FamORAssem)){
2635 if (invocation_assembly == mb.DeclaringType.Assembly)
2639 // We already know that we aren't in the same assembly.
2640 if (ma == MethodAttributes.Assembly)
2643 // Family and FamANDAssem require that we derive.
2644 if ((ma == MethodAttributes.Family) || (ma == MethodAttributes.FamANDAssem)){
2645 if (invocation_type == null)
2648 if (!IsNestedFamilyAccessible (invocation_type, mb.DeclaringType))
2651 // Although a derived class can access protected members of its base class
2652 // it cannot do so through an instance of the base class (CS1540).
2653 if (!mb.IsStatic && (qualifier_type != null) &&
2654 !IsEqualGenericInstance (invocation_type, qualifier_type) &&
2655 TypeManager.IsFamilyAccessible (invocation_type, qualifier_type) &&
2656 !TypeManager.IsNestedChildOf (invocation_type, qualifier_type))
2666 bool Filter (FieldInfo fi, object filter_criteria)
2668 FieldAttributes fa = fi.Attributes & FieldAttributes.FieldAccessMask;
2670 if (fa == FieldAttributes.Private)
2671 return private_ok ||
2672 IsPrivateAccessible (invocation_type, fi.DeclaringType) ||
2673 IsNestedChildOf (invocation_type, fi.DeclaringType);
2676 // FamAndAssem requires that we not only derivate, but we are on the
2679 if (fa == FieldAttributes.FamANDAssem){
2680 if (invocation_assembly != fi.DeclaringType.Assembly)
2684 // Assembly and FamORAssem succeed if we're in the same assembly.
2685 if ((fa == FieldAttributes.Assembly) || (fa == FieldAttributes.FamORAssem)){
2686 if (invocation_assembly == fi.DeclaringType.Assembly)
2690 // We already know that we aren't in the same assembly.
2691 if (fa == FieldAttributes.Assembly)
2694 // Family and FamANDAssem require that we derive.
2695 if ((fa == FieldAttributes.Family) || (fa == FieldAttributes.FamANDAssem)){
2696 if (invocation_type == null)
2699 if (!IsNestedFamilyAccessible (invocation_type, fi.DeclaringType))
2702 // Although a derived class can access protected members of its base class
2703 // it cannot do so through an instance of the base class (CS1540).
2704 if (!fi.IsStatic && (qualifier_type != null) &&
2705 !IsEqualGenericInstance (invocation_type, qualifier_type) &&
2706 TypeManager.IsFamilyAccessible (invocation_type, qualifier_type) &&
2707 !TypeManager.IsNestedChildOf (invocation_type, qualifier_type))
2718 // This filter filters by name + whether it is ok to include private
2719 // members in the search
2721 internal bool Filter (MemberInfo m, object filter_criteria)
2724 // Hack: we know that the filter criteria will always be in the
2725 // `closure' // fields.
2728 if ((filter_criteria != null) && (m.Name != (string) filter_criteria))
2731 if (((qualifier_type == null) || (qualifier_type == invocation_type)) &&
2732 (invocation_type != null) &&
2733 IsPrivateAccessible (m.DeclaringType, invocation_type))
2737 // Ugly: we need to find out the type of `m', and depending
2738 // on this, tell whether we accept or not
2740 if (m is MethodBase)
2741 return Filter ((MethodBase) m, filter_criteria);
2744 return Filter ((FieldInfo) m, filter_criteria);
2747 // EventInfos and PropertyInfos, return true because they lack
2748 // permission information, so we need to check later on the methods.
2754 static Closure closure = new Closure ();
2755 static MemberFilter FilterWithClosure_delegate = new MemberFilter (closure.Filter);
2758 // Looks up a member called `name' in the `queried_type'. This lookup
2759 // is done by code that is contained in the definition for `invocation_type'
2760 // through a qualifier of type `qualifier_type' (or null if there is no qualifier).
2762 // `invocation_type' is used to check whether we're allowed to access the requested
2763 // member wrt its protection level.
2765 // When called from MemberAccess, `qualifier_type' is the type which is used to access
2766 // the requested member (`class B { A a = new A (); a.foo = 5; }'; here invocation_type
2767 // is B and qualifier_type is A). This is used to do the CS1540 check.
2769 // When resolving a SimpleName, `qualifier_type' is null.
2771 // The `qualifier_type' is used for the CS1540 check; it's normally either null or
2772 // the same than `queried_type' - except when we're being called from BaseAccess;
2773 // in this case, `invocation_type' is the current type and `queried_type' the base
2774 // type, so this'd normally trigger a CS1540.
2776 // The binding flags are `bf' and the kind of members being looked up are `mt'
2778 // The return value always includes private members which code in `invocation_type'
2779 // is allowed to access (using the specified `qualifier_type' if given); only use
2780 // BindingFlags.NonPublic to bypass the permission check.
2782 // The 'almost_match' argument is used for reporting error CS1540.
2784 // Returns an array of a single element for everything but Methods/Constructors
2785 // that might return multiple matches.
2787 public static MemberInfo [] MemberLookup (Type invocation_type, Type qualifier_type,
2788 Type queried_type, MemberTypes mt,
2789 BindingFlags original_bf, string name, IList almost_match)
2791 Timer.StartTimer (TimerType.MemberLookup);
2793 MemberInfo[] retval = RealMemberLookup (invocation_type, qualifier_type,
2794 queried_type, mt, original_bf, name, almost_match);
2796 Timer.StopTimer (TimerType.MemberLookup);
2801 static MemberInfo [] RealMemberLookup (Type invocation_type, Type qualifier_type,
2802 Type queried_type, MemberTypes mt,
2803 BindingFlags original_bf, string name, IList almost_match)
2805 BindingFlags bf = original_bf;
2807 ArrayList method_list = null;
2808 Type current_type = queried_type;
2809 bool searching = (original_bf & BindingFlags.DeclaredOnly) == 0;
2810 bool skip_iface_check = true, used_cache = false;
2811 bool always_ok_flag = false;
2813 closure.invocation_type = invocation_type;
2814 closure.invocation_assembly = invocation_type != null ? invocation_type.Assembly : null;
2815 closure.qualifier_type = qualifier_type;
2816 closure.almost_match = almost_match;
2819 // If we are a nested class, we always have access to our container
2822 if (invocation_type != null){
2823 string invocation_name = invocation_type.FullName;
2824 if ((invocation_name != null) && (invocation_name.IndexOf ('+') != -1)){
2825 string container = queried_type.FullName + "+";
2826 int container_length = container.Length;
2828 if (invocation_name.Length > container_length){
2829 string shared = invocation_name.Substring (0, container_length);
2831 if (shared == container)
2832 always_ok_flag = true;
2837 // This is from the first time we find a method
2838 // in most cases, we do not actually find a method in the base class
2839 // so we can just ignore it, and save the arraylist allocation
2840 MemberInfo [] first_members_list = null;
2841 bool use_first_members_list = false;
2847 // `NonPublic' is lame, because it includes both protected and
2848 // private methods, so we need to control this behavior by
2849 // explicitly tracking if a private method is ok or not.
2851 // The possible cases are:
2852 // public, private and protected (internal does not come into the
2855 if ((invocation_type != null) &&
2856 ((invocation_type == current_type) ||
2857 IsNestedChildOf (invocation_type, current_type)) ||
2859 bf = original_bf | BindingFlags.NonPublic;
2863 closure.private_ok = (original_bf & BindingFlags.NonPublic) != 0;
2865 Timer.StopTimer (TimerType.MemberLookup);
2867 list = MemberLookup_FindMembers (
2868 current_type, mt, bf, name, out used_cache);
2870 Timer.StartTimer (TimerType.MemberLookup);
2873 // When queried for an interface type, the cache will automatically check all
2874 // inherited members, so we don't need to do this here. However, this only
2875 // works if we already used the cache in the first iteration of this loop.
2877 // If we used the cache in any further iteration, we can still terminate the
2878 // loop since the cache always looks in all parent classes.
2884 skip_iface_check = false;
2886 if (current_type == TypeManager.object_type)
2889 current_type = current_type.BaseType;
2892 // This happens with interfaces, they have a null
2893 // basetype. Look members up in the Object class.
2895 if (current_type == null) {
2896 current_type = TypeManager.object_type;
2901 if (list.Length == 0)
2905 // Events and types are returned by both `static' and `instance'
2906 // searches, which means that our above FindMembers will
2907 // return two copies of the same.
2909 if (list.Length == 1 && !(list [0] is MethodBase)){
2914 // Multiple properties: we query those just to find out the indexer
2917 if (list [0] is PropertyInfo)
2921 // We found an event: the cache lookup returns both the event and
2922 // its private field.
2924 if (list [0] is EventInfo) {
2925 if ((list.Length == 2) && (list [1] is FieldInfo))
2926 return new MemberInfo [] { list [0] };
2933 // We found methods, turn the search into "method scan"
2937 if (first_members_list != null) {
2938 if (use_first_members_list) {
2939 method_list = CopyNewMethods (method_list, first_members_list);
2940 use_first_members_list = false;
2943 method_list = CopyNewMethods (method_list, list);
2945 first_members_list = list;
2946 use_first_members_list = true;
2948 mt &= (MemberTypes.Method | MemberTypes.Constructor);
2950 } while (searching);
2952 if (use_first_members_list) {
2953 foreach (MemberInfo mi in first_members_list) {
2954 if (! (mi is MethodBase)) {
2955 method_list = CopyNewMethods (method_list, first_members_list);
2956 return (MemberInfo []) method_list.ToArray (typeof (MemberInfo));
2959 return (MemberInfo []) first_members_list;
2962 if (method_list != null && method_list.Count > 0) {
2963 return (MemberInfo []) method_list.ToArray (typeof (MemberInfo));
2966 // This happens if we already used the cache in the first iteration, in this case
2967 // the cache already looked in all interfaces.
2969 if (skip_iface_check)
2973 // Interfaces do not list members they inherit, so we have to
2976 if (!queried_type.IsInterface)
2979 if (queried_type.IsArray)
2980 queried_type = TypeManager.array_type;
2982 Type [] ifaces = GetInterfaces (queried_type);
2986 foreach (Type itype in ifaces){
2989 x = MemberLookup (null, null, itype, mt, bf, name, null);
2997 // Tests whether external method is really special
2998 public static bool IsSpecialMethod (MethodBase mb)
3000 string name = mb.Name;
3001 if (name.StartsWith ("get_") || name.StartsWith ("set_"))
3002 return mb.DeclaringType.GetProperty (name.Substring (4)) != null;
3004 if (name.StartsWith ("add_"))
3005 return mb.DeclaringType.GetEvent (name.Substring (4)) != null;
3007 if (name.StartsWith ("remove_"))
3008 return mb.DeclaringType.GetEvent (name.Substring (7)) != null;
3010 if (name.StartsWith ("op_")){
3011 foreach (string oname in Unary.oper_names) {
3016 foreach (string oname in Binary.oper_names) {
3029 /// There is exactly one instance of this class per type.
3031 public sealed class TypeHandle : IMemberContainer {
3032 public readonly TypeHandle BaseType;
3034 readonly int id = ++next_id;
3035 static int next_id = 0;
3038 /// Lookup a TypeHandle instance for the given type. If the type doesn't have
3039 /// a TypeHandle yet, a new instance of it is created. This static method
3040 /// ensures that we'll only have one TypeHandle instance per type.
3042 private static TypeHandle GetTypeHandle (Type t)
3044 TypeHandle handle = (TypeHandle) type_hash [t];
3048 handle = new TypeHandle (t);
3049 type_hash.Add (t, handle);
3053 public static MemberCache GetMemberCache (Type t)
3055 return GetTypeHandle (t).MemberCache;
3058 public static void CleanUp ()
3064 /// Returns the TypeHandle for TypeManager.object_type.
3066 public static IMemberContainer ObjectType {
3068 if (object_type != null)
3071 object_type = GetTypeHandle (TypeManager.object_type);
3078 /// Returns the TypeHandle for TypeManager.array_type.
3080 public static IMemberContainer ArrayType {
3082 if (array_type != null)
3085 array_type = GetTypeHandle (TypeManager.array_type);
3091 private static PtrHashtable type_hash = new PtrHashtable ();
3093 private static TypeHandle object_type = null;
3094 private static TypeHandle array_type = null;
3097 private string full_name;
3098 private bool is_interface;
3099 private MemberCache member_cache;
3100 private MemberCache parent_cache;
3102 private TypeHandle (Type type)
3105 full_name = type.FullName != null ? type.FullName : type.Name;
3106 if (type.BaseType != null) {
3107 BaseType = GetTypeHandle (type.BaseType);
3108 parent_cache = BaseType.MemberCache;
3109 } else if (type.IsInterface)
3110 parent_cache = TypeManager.LookupParentInterfacesCache (type);
3111 this.is_interface = type.IsInterface || type.IsGenericParameter;
3112 this.member_cache = new MemberCache (this);
3115 // IMemberContainer methods
3117 public string Name {
3129 public MemberCache ParentCache {
3131 return parent_cache;
3135 public bool IsInterface {
3137 return is_interface;
3141 public MemberList GetMembers (MemberTypes mt, BindingFlags bf)
3143 MemberInfo [] members;
3144 if (type is GenericTypeParameterBuilder)
3145 return MemberList.Empty;
3146 if (mt == MemberTypes.Event)
3147 members = type.GetEvents (bf | BindingFlags.DeclaredOnly);
3149 members = type.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
3151 Array.Reverse (members);
3153 return new MemberList (members);
3156 // IMemberFinder methods
3158 public MemberList FindMembers (MemberTypes mt, BindingFlags bf, string name,
3159 MemberFilter filter, object criteria)
3161 return new MemberList (member_cache.FindMembers (mt, bf, name, filter, criteria));
3164 public MemberCache MemberCache {
3166 return member_cache;
3170 public override string ToString ()
3172 if (BaseType != null)
3173 return "TypeHandle (" + id + "," + Name + " : " + BaseType + ")";
3175 return "TypeHandle (" + id + "," + Name + ")";