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 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 enumeration_type;
59 static public Type array_type;
60 static public Type runtime_handle_type;
61 static public Type icloneable_type;
62 static public Type type_type;
63 static public Type ienumerator_type;
64 static public Type ienumerable_type;
65 static public Type idisposable_type;
66 static public Type default_member_type;
67 static public Type iasyncresult_type;
68 static public Type asynccallback_type;
69 static public Type intptr_type;
70 static public Type monitor_type;
71 static public Type runtime_field_handle_type;
72 static public Type attribute_type;
73 static public Type attribute_usage_type;
74 static public Type dllimport_type;
75 static public Type unverifiable_code_type;
76 static public Type methodimpl_attr_type;
77 static public Type marshal_as_attr_type;
78 static public Type param_array_type;
79 static public Type guid_attr_type;
80 static public Type void_ptr_type;
81 static public Type indexer_name_type;
82 static public Type exception_type;
83 static public Type invalid_operation_exception_type;
84 static public object obsolete_attribute_type;
85 static public object conditional_attribute_type;
86 static public Type in_attribute_type;
87 static public Type cls_compliant_attribute_type;
88 static public Type typed_reference_type;
89 static public Type arg_iterator_type;
90 static public Type mbr_type;
91 static public Type struct_layout_attribute_type;
92 static public Type field_offset_attribute_type;
95 // An empty array of types
97 static public Type [] NoTypes;
98 static public TypeExpr [] NoTypeExprs;
102 // Expressions representing the internal types. Used during declaration
105 static public TypeExpr system_object_expr, system_string_expr;
106 static public TypeExpr system_boolean_expr, system_decimal_expr;
107 static public TypeExpr system_single_expr, system_double_expr;
108 static public TypeExpr system_sbyte_expr, system_byte_expr;
109 static public TypeExpr system_int16_expr, system_uint16_expr;
110 static public TypeExpr system_int32_expr, system_uint32_expr;
111 static public TypeExpr system_int64_expr, system_uint64_expr;
112 static public TypeExpr system_char_expr, system_void_expr;
113 static public TypeExpr system_asynccallback_expr;
114 static public TypeExpr system_iasyncresult_expr;
115 static public TypeExpr system_valuetype_expr;
118 // This is only used when compiling corlib
120 static public Type system_int32_type;
121 static public Type system_array_type;
122 static public Type system_type_type;
123 static public Type system_assemblybuilder_type;
124 static public MethodInfo system_int_array_get_length;
125 static public MethodInfo system_int_array_get_rank;
126 static public MethodInfo system_object_array_clone;
127 static public MethodInfo system_int_array_get_length_int;
128 static public MethodInfo system_int_array_get_lower_bound_int;
129 static public MethodInfo system_int_array_get_upper_bound_int;
130 static public MethodInfo system_void_array_copyto_array_int;
134 // Internal, not really used outside
136 static Type runtime_helpers_type;
139 // These methods are called by code generated by the compiler
141 static public MethodInfo string_concat_string_string;
142 static public MethodInfo string_concat_string_string_string;
143 static public MethodInfo string_concat_string_string_string_string;
144 static public MethodInfo string_concat_string_dot_dot_dot;
145 static public MethodInfo string_concat_object_object;
146 static public MethodInfo string_concat_object_object_object;
147 static public MethodInfo string_concat_object_dot_dot_dot;
148 static public MethodInfo string_isinterneted_string;
149 static public MethodInfo system_type_get_type_from_handle;
150 static public MethodInfo object_getcurrent_void;
151 static public MethodInfo bool_movenext_void;
152 static public MethodInfo ienumerable_getenumerator_void;
153 static public MethodInfo void_reset_void;
154 static public MethodInfo void_dispose_void;
155 static public MethodInfo void_monitor_enter_object;
156 static public MethodInfo void_monitor_exit_object;
157 static public MethodInfo void_initializearray_array_fieldhandle;
158 static public MethodInfo int_getlength_int;
159 static public MethodInfo delegate_combine_delegate_delegate;
160 static public MethodInfo delegate_remove_delegate_delegate;
161 static public MethodInfo int_get_offset_to_string_data;
162 static public MethodInfo int_array_get_length;
163 static public MethodInfo int_array_get_rank;
164 static public MethodInfo object_array_clone;
165 static public MethodInfo int_array_get_length_int;
166 static public MethodInfo int_array_get_lower_bound_int;
167 static public MethodInfo int_array_get_upper_bound_int;
168 static public MethodInfo void_array_copyto_array_int;
171 // The attribute constructors.
173 static public ConstructorInfo object_ctor;
174 static public ConstructorInfo cons_param_array_attribute;
175 static public ConstructorInfo void_decimal_ctor_five_args;
176 static public ConstructorInfo unverifiable_code_ctor;
177 static public ConstructorInfo invalid_operation_ctor;
180 // Holds the Array of Assemblies that have been loaded
181 // (either because it is the default or the user used the
182 // -r command line option)
184 static Assembly [] assemblies;
187 // Keeps a list of modules. We used this to do lookups
188 // on the module using GetType -- needed for arrays
190 static Module [] modules;
193 // This is the type_cache from the assemblies to avoid
194 // hitting System.Reflection on every lookup.
196 static Hashtable types;
199 // This is used to hotld the corresponding TypeContainer objects
200 // since we need this in FindMembers
202 static Hashtable typecontainers;
205 // Keeps track of those types that are defined by the
208 static ArrayList user_types;
210 static PtrHashtable builder_to_declspace;
213 // Tracks the interfaces implemented by typebuilders. We only
214 // enter those who do implement or or more interfaces
216 static PtrHashtable builder_to_ifaces;
219 // Maps MethodBase.RuntimeTypeHandle to a Type array that contains
220 // the arguments to the method
222 static Hashtable method_arguments;
225 // Maps PropertyBuilder to a Type array that contains
226 // the arguments to the indexer
228 static Hashtable indexer_arguments;
231 // Maybe `method_arguments' should be replaced and only
232 // method_internal_params should be kept?
234 static Hashtable method_internal_params;
237 // Keeps track of attribute types
240 static Hashtable builder_to_attr;
243 // Keeps track of methods
246 static Hashtable builder_to_method;
249 // Contains all public types from referenced assemblies.
250 // This member is used only if CLS Compliance verification is required.
252 public static Hashtable all_imported_types;
259 public static void CleanUp ()
261 // Lets get everything clean so that we can collect before generating code
265 typecontainers = null;
267 builder_to_declspace = null;
268 builder_to_ifaces = null;
269 method_arguments = null;
270 indexer_arguments = null;
271 method_internal_params = null;
272 builder_to_attr = null;
273 builder_to_method = null;
277 negative_hits = null;
278 attr_to_allowmult = null;
279 builder_to_constant = null;
280 fieldbuilders_to_fields = null;
282 priv_fields_events = null;
285 TypeHandle.CleanUp ();
289 /// A filter for Findmembers that uses the Signature object to
292 static bool SignatureFilter (MemberInfo mi, object criteria)
294 Signature sig = (Signature) criteria;
296 if (!(mi is MethodBase))
299 if (mi.Name != sig.name)
302 int count = sig.args.Length;
304 if (mi is MethodBuilder || mi is ConstructorBuilder){
305 Type [] candidate_args = GetArgumentTypes ((MethodBase) mi);
307 if (candidate_args.Length != count)
310 for (int i = 0; i < count; i++)
311 if (candidate_args [i] != sig.args [i])
316 ParameterInfo [] pars = ((MethodBase) mi).GetParameters ();
318 if (pars.Length != count)
321 for (int i = 0; i < count; i++)
322 if (pars [i].ParameterType != sig.args [i])
328 // A delegate that points to the filter above.
329 static MemberFilter signature_filter;
332 // These are expressions that represent some of the internal data types, used
335 static void InitExpressionTypes ()
337 system_object_expr = new TypeLookupExpression ("System.Object");
338 system_string_expr = new TypeLookupExpression ("System.String");
339 system_boolean_expr = new TypeLookupExpression ("System.Boolean");
340 system_decimal_expr = new TypeLookupExpression ("System.Decimal");
341 system_single_expr = new TypeLookupExpression ("System.Single");
342 system_double_expr = new TypeLookupExpression ("System.Double");
343 system_sbyte_expr = new TypeLookupExpression ("System.SByte");
344 system_byte_expr = new TypeLookupExpression ("System.Byte");
345 system_int16_expr = new TypeLookupExpression ("System.Int16");
346 system_uint16_expr = new TypeLookupExpression ("System.UInt16");
347 system_int32_expr = new TypeLookupExpression ("System.Int32");
348 system_uint32_expr = new TypeLookupExpression ("System.UInt32");
349 system_int64_expr = new TypeLookupExpression ("System.Int64");
350 system_uint64_expr = new TypeLookupExpression ("System.UInt64");
351 system_char_expr = new TypeLookupExpression ("System.Char");
352 system_void_expr = new TypeLookupExpression ("System.Void");
353 system_asynccallback_expr = new TypeLookupExpression ("System.AsyncCallback");
354 system_iasyncresult_expr = new TypeLookupExpression ("System.IAsyncResult");
355 system_valuetype_expr = new TypeLookupExpression ("System.ValueType");
358 static TypeManager ()
360 assemblies = new Assembly [0];
362 user_types = new ArrayList ();
364 types = new Hashtable ();
365 typecontainers = new Hashtable ();
367 builder_to_declspace = new PtrHashtable ();
368 builder_to_attr = new PtrHashtable ();
369 builder_to_method = new PtrHashtable ();
370 method_arguments = new PtrHashtable ();
371 method_internal_params = new PtrHashtable ();
372 indexer_arguments = new PtrHashtable ();
373 builder_to_ifaces = new PtrHashtable ();
375 NoTypes = new Type [0];
376 NoTypeExprs = new TypeExpr [0];
378 signature_filter = new MemberFilter (SignatureFilter);
379 InitExpressionTypes ();
382 public static void HandleDuplicate (string name, Type t)
384 Type prev = (Type) types [name];
385 TypeContainer tc = builder_to_declspace [prev] as TypeContainer;
389 // This probably never happens, as we catch this before
391 Report.Error (-17, "The type `" + name + "' has already been defined.");
395 tc = builder_to_declspace [t] as TypeContainer;
398 1595, "The type `" + name + "' is defined in an existing assembly;"+
399 " Using the new definition from: " + tc.Location);
402 1595, "The type `" + name + "' is defined in an existing assembly;");
405 Report.Warning (1595, "Previously defined in: " + prev.Assembly.FullName);
411 public static void AddUserType (string name, TypeBuilder t, TypeExpr[] ifaces)
416 HandleDuplicate (name, t);
421 builder_to_ifaces [t] = ifaces;
425 // This entry point is used by types that we define under the covers
427 public static void RegisterBuilder (TypeBuilder tb, TypeExpr [] ifaces)
430 builder_to_ifaces [tb] = ifaces;
433 public static void AddUserType (string name, TypeBuilder t, TypeContainer tc, TypeExpr [] ifaces)
435 builder_to_declspace.Add (t, tc);
436 typecontainers.Add (name, tc);
437 AddUserType (name, t, ifaces);
440 public static void AddDelegateType (string name, TypeBuilder t, Delegate del)
445 HandleDuplicate (name, t);
448 builder_to_declspace.Add (t, del);
451 public static void AddEnumType (string name, TypeBuilder t, Enum en)
456 HandleDuplicate (name, t);
458 builder_to_declspace.Add (t, en);
461 public static void AddUserInterface (string name, TypeBuilder t, Interface i, TypeExpr [] ifaces)
463 AddUserType (name, t, ifaces);
464 builder_to_declspace.Add (t, i);
467 public static void AddMethod (MethodBuilder builder, MethodData method)
469 builder_to_method.Add (builder, method);
472 public static void RegisterAttrType (Type t, TypeContainer tc)
474 builder_to_attr.Add (t, tc);
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 IMemberContainer LookupMemberContainer (Type t)
497 if (t is TypeBuilder) {
498 IMemberContainer container = builder_to_declspace [t] as IMemberContainer;
499 if (container != null)
503 return TypeHandle.GetTypeHandle (t);
506 public static Interface LookupInterface (Type t)
508 return builder_to_declspace [t] as Interface;
511 public static Delegate LookupDelegate (Type t)
513 return builder_to_declspace [t] as Delegate;
516 public static Enum LookupEnum (Type t)
518 return builder_to_declspace [t] as Enum;
521 public static TypeContainer LookupAttr (Type t)
523 return (TypeContainer) builder_to_attr [t];
527 /// Registers an assembly to load types from.
529 public static void AddAssembly (Assembly a)
531 foreach (Assembly assembly in assemblies) {
536 int top = assemblies.Length;
537 Assembly [] n = new Assembly [top + 1];
539 assemblies.CopyTo (n, 0);
546 /// Registers a module builder to lookup types from
548 public static void AddModule (Module mb)
550 int top = modules != null ? modules.Length : 0;
551 Module [] n = new Module [top + 1];
554 modules.CopyTo (n, 0);
559 public static Module[] Modules {
565 static Hashtable references = new Hashtable ();
568 // Gets the reference to T version of the Type (T&)
570 public static Type GetReferenceType (Type t)
572 string tname = t.FullName + "&";
574 Type ret = t.Assembly.GetType (tname);
577 // If the type comes from the assembly we are building
578 // We need the Hashtable, because .NET 1.1 will return different instance types
579 // every time we call ModuleBuilder.GetType.
582 if (references [t] == null)
583 references [t] = CodeGen.Module.Builder.GetType (tname);
584 ret = (Type) references [t];
590 static Hashtable pointers = new Hashtable ();
593 // Gets the pointer to T version of the Type (T*)
595 public static Type GetPointerType (Type t)
597 string tname = t.FullName + "*";
599 Type ret = t.Assembly.GetType (tname);
602 // If the type comes from the assembly we are building
603 // We need the Hashtable, because .NET 1.1 will return different instance types
604 // every time we call ModuleBuilder.GetType.
607 if (pointers [t] == null)
608 pointers [t] = CodeGen.Module.Builder.GetType (tname);
610 ret = (Type) pointers [t];
617 // Low-level lookup, cache-less
619 static Type LookupTypeReflection (string name)
623 foreach (Assembly a in assemblies){
624 t = a.GetType (name);
629 TypeAttributes ta = t.Attributes & TypeAttributes.VisibilityMask;
630 if (ta == TypeAttributes.NotPublic ||
631 ta == TypeAttributes.NestedPrivate ||
632 ta == TypeAttributes.NestedAssembly ||
633 ta == TypeAttributes.NestedFamANDAssem){
636 // In .NET pointers turn out to be private, even if their
637 // element type is not
640 t = t.GetElementType ();
650 foreach (Module mb in modules) {
651 t = mb.GetType (name);
659 static Hashtable negative_hits = new Hashtable ();
662 // This function is used when you want to avoid the lookups, and want to go
663 // directly to the source. This will use the cache.
665 // Notice that bypassing the cache is bad, because on Microsoft.NET runtime
666 // GetType ("DynamicType[]") != GetType ("DynamicType[]"), and there is no
667 // way to test things other than doing a fullname compare
669 public static Type LookupTypeDirect (string name)
671 Type t = (Type) types [name];
675 t = LookupTypeReflection (name);
683 static readonly char [] dot_array = { '.' };
686 /// Returns the Type associated with @name, takes care of the fact that
687 /// reflection expects nested types to be separated from the main type
688 /// with a "+" instead of a "."
690 public static Type LookupType (string name)
695 // First lookup in user defined and cached values
698 t = (Type) types [name];
702 // Two thirds of the failures are caught here.
703 if (negative_hits.Contains (name))
706 // Sadly, split takes a param array, so this ends up allocating *EVERY TIME*
707 string [] elements = name.Split (dot_array);
708 int count = elements.Length;
710 for (int n = 1; n <= count; n++){
711 string top_level_type = String.Join (".", elements, 0, n);
713 // One third of the failures are caught here.
714 if (negative_hits.Contains (top_level_type))
717 t = (Type) types [top_level_type];
719 t = LookupTypeReflection (top_level_type);
721 negative_hits [top_level_type] = null;
732 // We know that System.Object does not have children, and since its the parent of
733 // all the objects, it always gets probbed for inner classes.
735 if (top_level_type == "System.Object")
738 string newt = top_level_type + "+" + String.Join ("+", elements, n, count - n);
739 //Console.WriteLine ("Looking up: " + newt + " " + name);
740 t = LookupTypeReflection (newt);
742 negative_hits [name] = null;
747 negative_hits [name] = null;
752 /// Computes the namespaces that we import from the assemblies we reference.
754 public static void ComputeNamespaces ()
756 MethodInfo assembly_get_namespaces = typeof (Assembly).GetMethod ("GetNamespaces", BindingFlags.Instance|BindingFlags.NonPublic);
759 // First add the assembly namespaces
761 if (assembly_get_namespaces != null){
762 int count = assemblies.Length;
764 for (int i = 0; i < count; i++){
765 Assembly a = assemblies [i];
766 string [] namespaces = (string []) assembly_get_namespaces.Invoke (a, null);
767 foreach (string ns in namespaces){
770 Namespace.LookupNamespace (ns, true);
774 Hashtable cache = new Hashtable ();
775 cache.Add ("", null);
776 foreach (Assembly a in assemblies) {
777 foreach (Type t in a.GetExportedTypes ()) {
778 string ns = t.Namespace;
779 if (ns == null || cache.Contains (ns))
782 Namespace.LookupNamespace (ns, true);
783 cache.Add (ns, null);
790 /// Fills static table with exported types from all referenced assemblies.
791 /// This information is required for CLS Compliance tests.
793 public static void LoadAllImportedTypes ()
795 if (!CodeGen.Assembly.IsClsCompliant)
798 all_imported_types = new Hashtable ();
799 foreach (Assembly a in assemblies) {
800 foreach (Type t in a.GetExportedTypes ()) {
801 all_imported_types [t.FullName] = t;
806 public static bool NamespaceClash (string name, Location loc)
808 if (Namespace.LookupNamespace (name, false) == null)
811 Report.Error (519, loc, String.Format ("`{0}' clashes with a predefined namespace", name));
816 /// Returns the C# name of a type if possible, or the full type name otherwise
818 static public string CSharpName (Type t)
820 return Regex.Replace (t.FullName,
822 @"(Int32|UInt32|Int16|UInt16|Int64|UInt64|" +
823 @"Single|Double|Char|Decimal|Byte|SByte|Object|" +
824 @"Boolean|String|Void)" +
826 new MatchEvaluator (CSharpNameMatch));
829 static String CSharpNameMatch (Match match)
831 string s = match.Groups [1].Captures [0].Value;
833 Replace ("int32", "int").
834 Replace ("uint32", "uint").
835 Replace ("int16", "short").
836 Replace ("uint16", "ushort").
837 Replace ("int64", "long").
838 Replace ("uint64", "ulong").
839 Replace ("single", "float").
840 Replace ("boolean", "bool")
841 + match.Groups [2].Captures [0].Value;
845 /// Returns the signature of the method with full namespace classification
847 static public string GetFullNameSignature (MemberInfo mi)
851 n = mi.DeclaringType.Name;
853 return mi.DeclaringType.FullName.Replace ('+', '.') + '.' + n;
857 /// Returns the signature of the property and indexer
859 static public string CSharpSignature (PropertyBuilder pb, bool is_indexer)
862 return GetFullNameSignature (pb);
865 MethodBase mb = pb.GetSetMethod (true) != null ? pb.GetSetMethod (true) : pb.GetGetMethod (true);
866 string signature = GetFullNameSignature (mb);
867 string arg = TypeManager.LookupParametersByBuilder (mb).ParameterDesc (0);
868 return String.Format ("{0}.this[{1}]", signature.Substring (0, signature.LastIndexOf ('.')), arg);
872 /// Returns the signature of the method
874 static public string CSharpSignature (MethodBase mb)
879 // FIXME: We should really have a single function to do
880 // everything instead of the following 5 line pattern
882 ParameterData iparams = LookupParametersByBuilder (mb);
884 if (iparams == null){
885 ParameterInfo [] pi = mb.GetParameters ();
886 iparams = new ReflectionParameters (pi);
889 for (int i = 0; i < iparams.Count; i++) {
893 sig += iparams.ParameterDesc(i);
897 return GetFullNameSignature (mb) + sig;
901 /// Looks up a type, and aborts if it is not found. This is used
902 /// by types required by the compiler
904 static Type CoreLookupType (string name)
906 Type t = LookupTypeDirect (name);
909 Report.Error (518, "The predefined type `" + name + "' is not defined or imported");
910 Environment.Exit (1);
917 /// Returns the MethodInfo for a method named `name' defined
918 /// in type `t' which takes arguments of types `args'
920 static MethodInfo GetMethod (Type t, string name, Type [] args, bool is_private, bool report_errors)
924 BindingFlags flags = instance_and_static | BindingFlags.Public;
930 flags |= BindingFlags.NonPublic;
932 list = FindMembers (t, MemberTypes.Method, flags, signature_filter, sig);
933 if (list.Count == 0) {
935 Report.Error (-19, "Can not find the core function `" + name + "'");
939 MethodInfo mi = list [0] as MethodInfo;
942 Report.Error (-19, "Can not find the core function `" + name + "'");
949 static MethodInfo GetMethod (Type t, string name, Type [] args, bool report_errors)
951 return GetMethod (t, name, args, false, report_errors);
954 static MethodInfo GetMethod (Type t, string name, Type [] args)
956 return GetMethod (t, name, args, true);
961 /// Returns the ConstructorInfo for "args"
963 static ConstructorInfo GetConstructor (Type t, Type [] args)
971 list = FindMembers (t, MemberTypes.Constructor,
972 instance_and_static | BindingFlags.Public | BindingFlags.DeclaredOnly,
973 signature_filter, sig);
974 if (list.Count == 0){
975 Report.Error (-19, "Can not find the core constructor for type `" + t.Name + "'");
979 ConstructorInfo ci = list [0] as ConstructorInfo;
981 Report.Error (-19, "Can not find the core constructor for type `" + t.Name + "'");
988 public static void InitEnumUnderlyingTypes ()
991 int32_type = CoreLookupType ("System.Int32");
992 int64_type = CoreLookupType ("System.Int64");
993 uint32_type = CoreLookupType ("System.UInt32");
994 uint64_type = CoreLookupType ("System.UInt64");
995 byte_type = CoreLookupType ("System.Byte");
996 sbyte_type = CoreLookupType ("System.SByte");
997 short_type = CoreLookupType ("System.Int16");
998 ushort_type = CoreLookupType ("System.UInt16");
1002 /// The types have to be initialized after the initial
1003 /// population of the type has happened (for example, to
1004 /// bootstrap the corlib.dll
1006 public static void InitCoreTypes ()
1008 object_type = CoreLookupType ("System.Object");
1009 value_type = CoreLookupType ("System.ValueType");
1011 InitEnumUnderlyingTypes ();
1013 char_type = CoreLookupType ("System.Char");
1014 string_type = CoreLookupType ("System.String");
1015 float_type = CoreLookupType ("System.Single");
1016 double_type = CoreLookupType ("System.Double");
1017 char_ptr_type = CoreLookupType ("System.Char*");
1018 decimal_type = CoreLookupType ("System.Decimal");
1019 bool_type = CoreLookupType ("System.Boolean");
1020 enum_type = CoreLookupType ("System.Enum");
1022 multicast_delegate_type = CoreLookupType ("System.MulticastDelegate");
1023 delegate_type = CoreLookupType ("System.Delegate");
1025 array_type = CoreLookupType ("System.Array");
1026 void_type = CoreLookupType ("System.Void");
1027 type_type = CoreLookupType ("System.Type");
1029 runtime_field_handle_type = CoreLookupType ("System.RuntimeFieldHandle");
1030 runtime_helpers_type = CoreLookupType ("System.Runtime.CompilerServices.RuntimeHelpers");
1031 default_member_type = CoreLookupType ("System.Reflection.DefaultMemberAttribute");
1032 runtime_handle_type = CoreLookupType ("System.RuntimeTypeHandle");
1033 asynccallback_type = CoreLookupType ("System.AsyncCallback");
1034 iasyncresult_type = CoreLookupType ("System.IAsyncResult");
1035 ienumerator_type = CoreLookupType ("System.Collections.IEnumerator");
1036 ienumerable_type = CoreLookupType ("System.Collections.IEnumerable");
1037 idisposable_type = CoreLookupType ("System.IDisposable");
1038 icloneable_type = CoreLookupType ("System.ICloneable");
1039 monitor_type = CoreLookupType ("System.Threading.Monitor");
1040 intptr_type = CoreLookupType ("System.IntPtr");
1042 attribute_type = CoreLookupType ("System.Attribute");
1043 attribute_usage_type = CoreLookupType ("System.AttributeUsageAttribute");
1044 dllimport_type = CoreLookupType ("System.Runtime.InteropServices.DllImportAttribute");
1045 methodimpl_attr_type = CoreLookupType ("System.Runtime.CompilerServices.MethodImplAttribute");
1046 marshal_as_attr_type = CoreLookupType ("System.Runtime.InteropServices.MarshalAsAttribute");
1047 param_array_type = CoreLookupType ("System.ParamArrayAttribute");
1048 in_attribute_type = CoreLookupType ("System.Runtime.InteropServices.InAttribute");
1049 typed_reference_type = CoreLookupType ("System.TypedReference");
1050 arg_iterator_type = CoreLookupType ("System.ArgIterator");
1051 mbr_type = CoreLookupType ("System.MarshalByRefObject");
1054 // Sigh. Remove this before the release. Wonder what versions of Mono
1055 // people are running.
1057 guid_attr_type = LookupType ("System.Runtime.InteropServices.GuidAttribute");
1059 unverifiable_code_type= CoreLookupType ("System.Security.UnverifiableCodeAttribute");
1061 void_ptr_type = CoreLookupType ("System.Void*");
1063 indexer_name_type = CoreLookupType ("System.Runtime.CompilerServices.IndexerNameAttribute");
1065 exception_type = CoreLookupType ("System.Exception");
1066 invalid_operation_exception_type = CoreLookupType ("System.InvalidOperationException");
1071 obsolete_attribute_type = CoreLookupType ("System.ObsoleteAttribute");
1072 conditional_attribute_type = CoreLookupType ("System.Diagnostics.ConditionalAttribute");
1073 cls_compliant_attribute_type = CoreLookupType ("System.CLSCompliantAttribute");
1074 struct_layout_attribute_type = CoreLookupType ("System.Runtime.InteropServices.StructLayoutAttribute");
1075 field_offset_attribute_type = CoreLookupType ("System.Runtime.InteropServices.FieldOffsetAttribute");
1078 // When compiling corlib, store the "real" types here.
1080 if (!RootContext.StdLib) {
1081 system_int32_type = typeof (System.Int32);
1082 system_array_type = typeof (System.Array);
1083 system_type_type = typeof (System.Type);
1084 system_assemblybuilder_type = typeof (System.Reflection.Emit.AssemblyBuilder);
1086 Type [] void_arg = { };
1087 system_int_array_get_length = GetMethod (
1088 system_array_type, "get_Length", void_arg);
1089 system_int_array_get_rank = GetMethod (
1090 system_array_type, "get_Rank", void_arg);
1091 system_object_array_clone = GetMethod (
1092 system_array_type, "Clone", void_arg);
1094 Type [] system_int_arg = { system_int32_type };
1095 system_int_array_get_length_int = GetMethod (
1096 system_array_type, "GetLength", system_int_arg);
1097 system_int_array_get_upper_bound_int = GetMethod (
1098 system_array_type, "GetUpperBound", system_int_arg);
1099 system_int_array_get_lower_bound_int = GetMethod (
1100 system_array_type, "GetLowerBound", system_int_arg);
1102 Type [] system_array_int_arg = { system_array_type, system_int32_type };
1103 system_void_array_copyto_array_int = GetMethod (
1104 system_array_type, "CopyTo", system_array_int_arg);
1106 Type [] system_3_type_arg = {
1107 system_type_type, system_type_type, system_type_type };
1108 Type [] system_4_type_arg = {
1109 system_type_type, system_type_type, system_type_type, system_type_type };
1111 MethodInfo set_corlib_type_builders = GetMethod (
1112 system_assemblybuilder_type, "SetCorlibTypeBuilders",
1113 system_4_type_arg, true, false);
1115 if (set_corlib_type_builders != null) {
1116 object[] args = new object [4];
1117 args [0] = object_type;
1118 args [1] = value_type;
1119 args [2] = enum_type;
1120 args [3] = void_type;
1122 set_corlib_type_builders.Invoke (CodeGen.Assembly.Builder, args);
1124 // Compatibility for an older version of the class libs.
1125 set_corlib_type_builders = GetMethod (
1126 system_assemblybuilder_type, "SetCorlibTypeBuilders",
1127 system_3_type_arg, true, true);
1129 if (set_corlib_type_builders == null) {
1130 Report.Error (-26, "Corlib compilation is not supported in Microsoft.NET due to bugs in it");
1134 object[] args = new object [3];
1135 args [0] = object_type;
1136 args [1] = value_type;
1137 args [2] = enum_type;
1139 set_corlib_type_builders.Invoke (CodeGen.Assembly.Builder, args);
1143 system_object_expr.Type = object_type;
1144 system_string_expr.Type = string_type;
1145 system_boolean_expr.Type = bool_type;
1146 system_decimal_expr.Type = decimal_type;
1147 system_single_expr.Type = float_type;
1148 system_double_expr.Type = double_type;
1149 system_sbyte_expr.Type = sbyte_type;
1150 system_byte_expr.Type = byte_type;
1151 system_int16_expr.Type = short_type;
1152 system_uint16_expr.Type = ushort_type;
1153 system_int32_expr.Type = int32_type;
1154 system_uint32_expr.Type = uint32_type;
1155 system_int64_expr.Type = int64_type;
1156 system_uint64_expr.Type = uint64_type;
1157 system_char_expr.Type = char_type;
1158 system_void_expr.Type = void_type;
1159 system_asynccallback_expr.Type = asynccallback_type;
1160 system_iasyncresult_expr.Type = iasyncresult_type;
1161 system_valuetype_expr.Type = value_type;
1165 // The helper methods that are used by the compiler
1167 public static void InitCodeHelpers ()
1170 // Now load the default methods that we use.
1172 Type [] string_string = { string_type, string_type };
1173 string_concat_string_string = GetMethod (
1174 string_type, "Concat", string_string);
1175 Type [] string_string_string = { string_type, string_type, string_type };
1176 string_concat_string_string_string = GetMethod (
1177 string_type, "Concat", string_string_string);
1178 Type [] string_string_string_string = { string_type, string_type, string_type, string_type };
1179 string_concat_string_string_string_string = GetMethod (
1180 string_type, "Concat", string_string_string_string);
1181 Type[] params_string = { TypeManager.LookupType ("System.String[]") };
1182 string_concat_string_dot_dot_dot = GetMethod (
1183 string_type, "Concat", params_string);
1185 Type [] object_object = { object_type, object_type };
1186 string_concat_object_object = GetMethod (
1187 string_type, "Concat", object_object);
1188 Type [] object_object_object = { object_type, object_type, object_type };
1189 string_concat_object_object_object = GetMethod (
1190 string_type, "Concat", object_object_object);
1191 Type[] params_object = { TypeManager.LookupType ("System.Object[]") };
1192 string_concat_object_dot_dot_dot = GetMethod (
1193 string_type, "Concat", params_object);
1195 Type [] string_ = { string_type };
1196 string_isinterneted_string = GetMethod (
1197 string_type, "IsInterned", string_);
1199 Type [] runtime_type_handle = { runtime_handle_type };
1200 system_type_get_type_from_handle = GetMethod (
1201 type_type, "GetTypeFromHandle", runtime_type_handle);
1203 Type [] delegate_delegate = { delegate_type, delegate_type };
1204 delegate_combine_delegate_delegate = GetMethod (
1205 delegate_type, "Combine", delegate_delegate);
1207 delegate_remove_delegate_delegate = GetMethod (
1208 delegate_type, "Remove", delegate_delegate);
1213 Type [] void_arg = { };
1214 object_getcurrent_void = GetMethod (
1215 ienumerator_type, "get_Current", void_arg);
1216 bool_movenext_void = GetMethod (
1217 ienumerator_type, "MoveNext", void_arg);
1218 void_reset_void = GetMethod (
1219 ienumerator_type, "Reset", void_arg);
1220 void_dispose_void = GetMethod (
1221 idisposable_type, "Dispose", void_arg);
1222 int_get_offset_to_string_data = GetMethod (
1223 runtime_helpers_type, "get_OffsetToStringData", void_arg);
1224 int_array_get_length = GetMethod (
1225 array_type, "get_Length", void_arg);
1226 int_array_get_rank = GetMethod (
1227 array_type, "get_Rank", void_arg);
1228 ienumerable_getenumerator_void = GetMethod (
1229 ienumerable_type, "GetEnumerator", void_arg);
1234 Type [] int_arg = { int32_type };
1235 int_array_get_length_int = GetMethod (
1236 array_type, "GetLength", int_arg);
1237 int_array_get_upper_bound_int = GetMethod (
1238 array_type, "GetUpperBound", int_arg);
1239 int_array_get_lower_bound_int = GetMethod (
1240 array_type, "GetLowerBound", int_arg);
1243 // System.Array methods
1245 object_array_clone = GetMethod (
1246 array_type, "Clone", void_arg);
1247 Type [] array_int_arg = { array_type, int32_type };
1248 void_array_copyto_array_int = GetMethod (
1249 array_type, "CopyTo", array_int_arg);
1254 Type [] object_arg = { object_type };
1255 void_monitor_enter_object = GetMethod (
1256 monitor_type, "Enter", object_arg);
1257 void_monitor_exit_object = GetMethod (
1258 monitor_type, "Exit", object_arg);
1260 Type [] array_field_handle_arg = { array_type, runtime_field_handle_type };
1262 void_initializearray_array_fieldhandle = GetMethod (
1263 runtime_helpers_type, "InitializeArray", array_field_handle_arg);
1268 int_getlength_int = GetMethod (
1269 array_type, "GetLength", int_arg);
1272 // Decimal constructors
1274 Type [] dec_arg = { int32_type, int32_type, int32_type, bool_type, byte_type };
1275 void_decimal_ctor_five_args = GetConstructor (
1276 decimal_type, dec_arg);
1281 cons_param_array_attribute = GetConstructor (
1282 param_array_type, void_arg);
1284 unverifiable_code_ctor = GetConstructor (
1285 unverifiable_code_type, void_arg);
1288 // InvalidOperationException
1290 invalid_operation_ctor = GetConstructor (
1291 invalid_operation_exception_type, void_arg);
1295 object_ctor = GetConstructor (object_type, void_arg);
1299 const BindingFlags instance_and_static = BindingFlags.Static | BindingFlags.Instance;
1302 /// This is the "old", non-cache based FindMembers() function. We cannot use
1303 /// the cache here because there is no member name argument.
1305 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1306 MemberFilter filter, object criteria)
1308 DeclSpace decl = (DeclSpace) builder_to_declspace [t];
1311 // `builder_to_declspace' contains all dynamic types.
1315 Timer.StartTimer (TimerType.FindMembers);
1316 list = decl.FindMembers (mt, bf, filter, criteria);
1317 Timer.StopTimer (TimerType.FindMembers);
1322 // We have to take care of arrays specially, because GetType on
1323 // a TypeBuilder array will return a Type, not a TypeBuilder,
1324 // and we can not call FindMembers on this type.
1326 if (t.IsSubclassOf (TypeManager.array_type))
1327 return new MemberList (TypeManager.array_type.FindMembers (mt, bf, filter, criteria));
1330 // Since FindMembers will not lookup both static and instance
1331 // members, we emulate this behaviour here.
1333 if ((bf & instance_and_static) == instance_and_static){
1334 MemberInfo [] i_members = t.FindMembers (
1335 mt, bf & ~BindingFlags.Static, filter, criteria);
1337 int i_len = i_members.Length;
1339 MemberInfo one = i_members [0];
1342 // If any of these are present, we are done!
1344 if ((one is Type) || (one is EventInfo) || (one is FieldInfo))
1345 return new MemberList (i_members);
1348 MemberInfo [] s_members = t.FindMembers (
1349 mt, bf & ~BindingFlags.Instance, filter, criteria);
1351 int s_len = s_members.Length;
1352 if (i_len > 0 || s_len > 0)
1353 return new MemberList (i_members, s_members);
1356 return new MemberList (i_members);
1358 return new MemberList (s_members);
1362 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
1367 /// This method is only called from within MemberLookup. It tries to use the member
1368 /// cache if possible and falls back to the normal FindMembers if not. The `used_cache'
1369 /// flag tells the caller whether we used the cache or not. If we used the cache, then
1370 /// our return value will already contain all inherited members and the caller don't need
1371 /// to check base classes and interfaces anymore.
1373 private static MemberInfo [] MemberLookup_FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1374 string name, out bool used_cache)
1377 // We have to take care of arrays specially, because GetType on
1378 // a TypeBuilder array will return a Type, not a TypeBuilder,
1379 // and we can not call FindMembers on this type.
1381 if (t == TypeManager.array_type || t.IsSubclassOf (TypeManager.array_type)) {
1383 return TypeHandle.ArrayType.MemberCache.FindMembers (
1384 mt, bf, name, FilterWithClosure_delegate, null);
1388 // If this is a dynamic type, it's always in the `builder_to_declspace' hash table
1389 // and we can ask the DeclSpace for the MemberCache.
1391 if (t is TypeBuilder) {
1392 DeclSpace decl = (DeclSpace) builder_to_declspace [t];
1393 MemberCache cache = decl.MemberCache;
1396 // If this DeclSpace has a MemberCache, use it.
1399 if (cache != null) {
1401 return cache.FindMembers (
1402 mt, bf, name, FilterWithClosure_delegate, null);
1405 // If there is no MemberCache, we need to use the "normal" FindMembers.
1406 // Note, this is a VERY uncommon route!
1409 Timer.StartTimer (TimerType.FindMembers);
1410 list = decl.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
1411 FilterWithClosure_delegate, name);
1412 Timer.StopTimer (TimerType.FindMembers);
1415 return (MemberInfo []) list;
1419 // This call will always succeed. There is exactly one TypeHandle instance per
1420 // type, TypeHandle.GetTypeHandle() will either return it or create a new one
1421 // if it didn't already exist.
1423 TypeHandle handle = TypeHandle.GetTypeHandle (t);
1426 return handle.MemberCache.FindMembers (mt, bf, name, FilterWithClosure_delegate, null);
1429 public static bool IsBuiltinType (Type t)
1431 if (t == object_type || t == string_type || t == int32_type || t == uint32_type ||
1432 t == int64_type || t == uint64_type || t == float_type || t == double_type ||
1433 t == char_type || t == short_type || t == decimal_type || t == bool_type ||
1434 t == sbyte_type || t == byte_type || t == ushort_type || t == void_type)
1440 public static bool IsBuiltinType (TypeContainer tc)
1442 return IsBuiltinType (tc.TypeBuilder);
1446 // This is like IsBuiltinType, but lacks decimal_type, we should also clean up
1447 // the pieces in the code where we use IsBuiltinType and special case decimal_type.
1449 public static bool IsCLRType (Type t)
1451 if (t == object_type || t == int32_type || t == uint32_type ||
1452 t == int64_type || t == uint64_type || t == float_type || t == double_type ||
1453 t == char_type || t == short_type || t == bool_type ||
1454 t == sbyte_type || t == byte_type || t == ushort_type)
1460 public static bool IsDelegateType (Type t)
1462 if (t.IsSubclassOf (TypeManager.delegate_type))
1468 public static bool IsEnumType (Type t)
1470 if (t == TypeManager.enum_type || t.IsSubclassOf (TypeManager.enum_type))
1475 public static bool IsBuiltinOrEnum (Type t)
1477 if (IsBuiltinType (t))
1487 // Whether a type is unmanaged. This is used by the unsafe code (25.2)
1489 public static bool IsUnmanagedType (Type t)
1491 if (IsBuiltinType (t) && t != TypeManager.string_type)
1500 if (IsValueType (t)){
1501 if (t is TypeBuilder){
1502 TypeContainer tc = LookupTypeContainer (t);
1504 if (tc.Fields != null){
1505 foreach (Field f in tc.Fields){
1506 if (f.FieldBuilder.IsStatic)
1508 if (!IsUnmanagedType (f.FieldBuilder.FieldType))
1514 FieldInfo [] fields = t.GetFields ();
1516 foreach (FieldInfo f in fields){
1519 if (!IsUnmanagedType (f.FieldType))
1529 public static bool IsValueType (Type t)
1531 if (t.IsSubclassOf (TypeManager.value_type) && (t != TypeManager.enum_type))
1537 public static bool IsInterfaceType (Type t)
1539 Interface iface = builder_to_declspace [t] as Interface;
1548 // Checks whether `type' is a subclass or nested child of `parent'.
1550 public static bool IsSubclassOrNestedChildOf (Type type, Type parent)
1553 if ((type == parent) || type.IsSubclassOf (parent))
1556 // Handle nested types.
1557 type = type.DeclaringType;
1558 } while (type != null);
1564 // Checks whether `type' is a nested child of `parent'.
1566 public static bool IsNestedChildOf (Type type, Type parent)
1571 type = type.DeclaringType;
1572 while (type != null) {
1576 type = type.DeclaringType;
1583 // Do the right thing when returning the element type of an
1584 // array type based on whether we are compiling corlib or not
1586 public static Type GetElementType (Type t)
1588 if (RootContext.StdLib)
1589 return t.GetElementType ();
1591 return TypeToCoreType (t.GetElementType ());
1595 /// Returns the User Defined Types
1597 public static ArrayList UserTypes {
1603 public static Hashtable TypeContainers {
1605 return typecontainers;
1609 static Hashtable attr_to_allowmult;
1611 public static void RegisterAttributeAllowMultiple (Type attr_type, bool allow)
1613 if (attr_to_allowmult == null)
1614 attr_to_allowmult = new PtrHashtable ();
1616 if (attr_to_allowmult.Contains (attr_type))
1619 attr_to_allowmult.Add (attr_type, allow);
1623 public static bool AreMultipleAllowed (Type attr_type)
1625 if (!(attr_type is TypeBuilder)) {
1626 System.Attribute [] attrs = System.Attribute.GetCustomAttributes (attr_type);
1628 foreach (System.Attribute tmp in attrs)
1629 if (tmp is AttributeUsageAttribute) {
1630 return ((AttributeUsageAttribute) tmp).AllowMultiple;
1636 if (attr_to_allowmult == null)
1639 return (bool) attr_to_allowmult [attr_type];
1642 static Hashtable builder_to_constant;
1644 public static void RegisterConstant (FieldBuilder fb, Const c)
1646 if (builder_to_constant == null)
1647 builder_to_constant = new PtrHashtable ();
1649 if (builder_to_constant.Contains (fb))
1652 builder_to_constant.Add (fb, c);
1655 public static Const LookupConstant (FieldBuilder fb)
1657 if (builder_to_constant == null)
1660 return (Const) builder_to_constant [fb];
1664 /// Gigantic work around for missing features in System.Reflection.Emit follows.
1668 /// Since System.Reflection.Emit can not return MethodBase.GetParameters
1669 /// for anything which is dynamic, and we need this in a number of places,
1670 /// we register this information here, and use it afterwards.
1672 static public bool RegisterMethod (MethodBase mb, InternalParameters ip, Type [] args)
1677 method_arguments.Add (mb, args);
1678 method_internal_params.Add (mb, ip);
1683 static public InternalParameters LookupParametersByBuilder (MethodBase mb)
1685 if (! (mb is ConstructorBuilder || mb is MethodBuilder))
1688 if (method_internal_params.Contains (mb))
1689 return (InternalParameters) method_internal_params [mb];
1691 throw new Exception ("Argument for Method not registered" + mb);
1695 /// Returns the argument types for a method based on its methodbase
1697 /// For dynamic methods, we use the compiler provided types, for
1698 /// methods from existing assemblies we load them from GetParameters,
1699 /// and insert them into the cache
1701 static public Type [] GetArgumentTypes (MethodBase mb)
1703 if (method_arguments.Contains (mb))
1704 return (Type []) method_arguments [mb];
1706 ParameterInfo [] pi = mb.GetParameters ();
1708 Type [] types = new Type [c];
1710 for (int i = 0; i < c; i++)
1711 types [i] = pi [i].ParameterType;
1713 method_arguments.Add (mb, types);
1719 /// Returns the argument types for an indexer based on its PropertyInfo
1721 /// For dynamic indexers, we use the compiler provided types, for
1722 /// indexers from existing assemblies we load them from GetParameters,
1723 /// and insert them into the cache
1725 static public Type [] GetArgumentTypes (PropertyInfo indexer)
1727 if (indexer_arguments.Contains (indexer))
1728 return (Type []) indexer_arguments [indexer];
1729 else if (indexer is PropertyBuilder)
1730 // If we're a PropertyBuilder and not in the
1731 // `indexer_arguments' hash, then we're a property and
1735 ParameterInfo [] pi = indexer.GetIndexParameters ();
1736 // Property, not an indexer.
1740 Type [] types = new Type [c];
1742 for (int i = 0; i < c; i++)
1743 types [i] = pi [i].ParameterType;
1745 indexer_arguments.Add (indexer, types);
1751 // This is a workaround the fact that GetValue is not
1752 // supported for dynamic types
1754 static Hashtable fields = new Hashtable ();
1755 static public bool RegisterFieldValue (FieldBuilder fb, object value)
1757 if (fields.Contains (fb))
1760 fields.Add (fb, value);
1765 static public object GetValue (FieldBuilder fb)
1770 static Hashtable fieldbuilders_to_fields = new Hashtable ();
1771 static public bool RegisterFieldBase (FieldBuilder fb, FieldBase f)
1773 if (fieldbuilders_to_fields.Contains (fb))
1776 fieldbuilders_to_fields.Add (fb, f);
1781 // The return value can be null; This will be the case for
1782 // auxiliary FieldBuilders created by the compiler that have no
1783 // real field being declared on the source code
1785 static public FieldBase GetField (FieldInfo fb)
1787 return (FieldBase) fieldbuilders_to_fields [fb];
1790 static Hashtable events;
1792 static public bool RegisterEvent (MyEventBuilder eb, MethodBase add, MethodBase remove)
1795 events = new Hashtable ();
1797 if (events.Contains (eb))
1800 events.Add (eb, new Pair (add, remove));
1805 static public MethodInfo GetAddMethod (EventInfo ei)
1807 if (ei is MyEventBuilder) {
1808 Pair pair = (Pair) events [ei];
1810 return (MethodInfo) pair.First;
1812 return ei.GetAddMethod ();
1815 static public MethodInfo GetRemoveMethod (EventInfo ei)
1817 if (ei is MyEventBuilder) {
1818 Pair pair = (Pair) events [ei];
1820 return (MethodInfo) pair.Second;
1822 return ei.GetRemoveMethod ();
1825 static Hashtable priv_fields_events;
1827 static public bool RegisterPrivateFieldOfEvent (EventInfo einfo, FieldBuilder builder)
1829 if (priv_fields_events == null)
1830 priv_fields_events = new Hashtable ();
1832 if (priv_fields_events.Contains (einfo))
1835 priv_fields_events.Add (einfo, builder);
1840 static public MemberInfo GetPrivateFieldOfEvent (EventInfo ei)
1842 if (priv_fields_events == null)
1845 return (MemberInfo) priv_fields_events [ei];
1848 static Hashtable properties;
1850 static public bool RegisterProperty (PropertyBuilder pb, MethodBase get, MethodBase set)
1852 if (properties == null)
1853 properties = new Hashtable ();
1855 if (properties.Contains (pb))
1858 properties.Add (pb, new Pair (get, set));
1863 static public bool RegisterIndexer (PropertyBuilder pb, MethodBase get,
1864 MethodBase set, Type[] args)
1866 if (!RegisterProperty (pb, get,set))
1869 indexer_arguments.Add (pb, args);
1874 public static bool CheckStructCycles (TypeContainer tc, Hashtable seen)
1876 Hashtable hash = new Hashtable ();
1877 return CheckStructCycles (tc, seen, hash);
1880 public static bool CheckStructCycles (TypeContainer tc, Hashtable seen,
1883 if (!(tc is Struct) || IsBuiltinType (tc))
1887 // `seen' contains all types we've already visited.
1889 if (seen.Contains (tc))
1891 seen.Add (tc, null);
1893 if (tc.Fields == null)
1896 foreach (Field field in tc.Fields) {
1897 if (field.FieldBuilder.IsStatic)
1900 Type ftype = field.FieldBuilder.FieldType;
1901 TypeContainer ftc = LookupTypeContainer (ftype);
1905 if (hash.Contains (ftc)) {
1906 Report.Error (523, tc.Location,
1907 "Struct member `{0}.{1}' of type `{2}' " +
1908 "causes a cycle in the struct layout",
1909 tc.Name, field.Name, ftc.Name);
1914 // `hash' contains all types in the current path.
1916 hash.Add (tc, null);
1918 bool ok = CheckStructCycles (ftc, seen, hash);
1925 if (!seen.Contains (ftc))
1926 seen.Add (ftc, null);
1933 /// Given an array of interface types, expand and eliminate repeated ocurrences
1934 /// of an interface.
1938 /// This expands in context like: IA; IB : IA; IC : IA, IB; the interface "IC" to
1941 public static TypeExpr[] ExpandInterfaces (TypeExpr [] base_interfaces)
1943 ArrayList new_ifaces = new ArrayList ();
1945 foreach (TypeExpr iface in base_interfaces){
1946 if (!new_ifaces.Contains (iface))
1947 new_ifaces.Add (iface);
1949 TypeExpr [] implementing = iface.GetInterfaces ();
1951 foreach (TypeExpr imp in implementing){
1952 if (!new_ifaces.Contains (imp))
1953 new_ifaces.Add (imp);
1956 TypeExpr [] ret = new TypeExpr [new_ifaces.Count];
1957 new_ifaces.CopyTo (ret, 0);
1961 static PtrHashtable iface_cache = new PtrHashtable ();
1964 /// This function returns the interfaces in the type `t'. Works with
1965 /// both types and TypeBuilders.
1967 public static TypeExpr [] GetInterfaces (Type t)
1970 TypeExpr [] cached = iface_cache [t] as TypeExpr [];
1975 // The reason for catching the Array case is that Reflection.Emit
1976 // will not return a TypeBuilder for Array types of TypeBuilder types,
1977 // but will still throw an exception if we try to call GetInterfaces
1980 // Since the array interfaces are always constant, we return those for
1985 t = TypeManager.array_type;
1987 if (t is TypeBuilder){
1988 TypeExpr [] parent_ifaces;
1990 if (t.BaseType == null)
1991 parent_ifaces = NoTypeExprs;
1993 parent_ifaces = GetInterfaces (t.BaseType);
1994 TypeExpr [] type_ifaces = (TypeExpr []) builder_to_ifaces [t];
1995 if (type_ifaces == null)
1996 type_ifaces = NoTypeExprs;
1998 int parent_count = parent_ifaces.Length;
1999 TypeExpr [] result = new TypeExpr [parent_count + type_ifaces.Length];
2000 parent_ifaces.CopyTo (result, 0);
2001 type_ifaces.CopyTo (result, parent_count);
2003 iface_cache [t] = result;
2006 Type [] ifaces = t.GetInterfaces ();
2007 if (ifaces.Length == 0)
2010 TypeExpr [] result = new TypeExpr [ifaces.Length];
2011 for (int i = 0; i < ifaces.Length; i++)
2012 result [i] = new TypeExpression (ifaces [i], Location.Null);
2014 iface_cache [t] = result;
2020 // gets the interfaces that are declared explicitly on t
2022 public static TypeExpr [] GetExplicitInterfaces (TypeBuilder t)
2024 return (TypeExpr []) builder_to_ifaces [t];
2028 /// The following is used to check if a given type implements an interface.
2029 /// The cache helps us reduce the expense of hitting Type.GetInterfaces everytime.
2031 public static bool ImplementsInterface (Type t, Type iface)
2033 TypeExpr [] interfaces;
2036 // FIXME OPTIMIZATION:
2037 // as soon as we hit a non-TypeBuiler in the interface
2038 // chain, we could return, as the `Type.GetInterfaces'
2039 // will return all the interfaces implement by the type
2043 interfaces = GetInterfaces (t);
2045 if (interfaces != null){
2046 foreach (TypeExpr i in interfaces){
2047 if (i.Type == iface)
2053 } while (t != null);
2058 static NumberFormatInfo nf_provider = CultureInfo.CurrentCulture.NumberFormat;
2060 // This is a custom version of Convert.ChangeType() which works
2061 // with the TypeBuilder defined types when compiling corlib.
2062 public static object ChangeType (object value, Type conversionType, out bool error)
2064 IConvertible convert_value = value as IConvertible;
2066 if (convert_value == null){
2072 // We must use Type.Equals() here since `conversionType' is
2073 // the TypeBuilder created version of a system type and not
2074 // the system type itself. You cannot use Type.GetTypeCode()
2075 // on such a type - it'd always return TypeCode.Object.
2079 if (conversionType.Equals (typeof (Boolean)))
2080 return (object)(convert_value.ToBoolean (nf_provider));
2081 else if (conversionType.Equals (typeof (Byte)))
2082 return (object)(convert_value.ToByte (nf_provider));
2083 else if (conversionType.Equals (typeof (Char)))
2084 return (object)(convert_value.ToChar (nf_provider));
2085 else if (conversionType.Equals (typeof (DateTime)))
2086 return (object)(convert_value.ToDateTime (nf_provider));
2087 else if (conversionType.Equals (typeof (Decimal)))
2088 return (object)(convert_value.ToDecimal (nf_provider));
2089 else if (conversionType.Equals (typeof (Double)))
2090 return (object)(convert_value.ToDouble (nf_provider));
2091 else if (conversionType.Equals (typeof (Int16)))
2092 return (object)(convert_value.ToInt16 (nf_provider));
2093 else if (conversionType.Equals (typeof (Int32)))
2094 return (object)(convert_value.ToInt32 (nf_provider));
2095 else if (conversionType.Equals (typeof (Int64)))
2096 return (object)(convert_value.ToInt64 (nf_provider));
2097 else if (conversionType.Equals (typeof (SByte)))
2098 return (object)(convert_value.ToSByte (nf_provider));
2099 else if (conversionType.Equals (typeof (Single)))
2100 return (object)(convert_value.ToSingle (nf_provider));
2101 else if (conversionType.Equals (typeof (String)))
2102 return (object)(convert_value.ToString (nf_provider));
2103 else if (conversionType.Equals (typeof (UInt16)))
2104 return (object)(convert_value.ToUInt16 (nf_provider));
2105 else if (conversionType.Equals (typeof (UInt32)))
2106 return (object)(convert_value.ToUInt32 (nf_provider));
2107 else if (conversionType.Equals (typeof (UInt64)))
2108 return (object)(convert_value.ToUInt64 (nf_provider));
2109 else if (conversionType.Equals (typeof (Object)))
2110 return (object)(value);
2120 // This is needed, because enumerations from assemblies
2121 // do not report their underlyingtype, but they report
2124 public static Type EnumToUnderlying (Type t)
2126 if (t == TypeManager.enum_type)
2129 t = t.UnderlyingSystemType;
2130 if (!TypeManager.IsEnumType (t))
2133 if (t is TypeBuilder) {
2134 // slow path needed to compile corlib
2135 if (t == TypeManager.bool_type ||
2136 t == TypeManager.byte_type ||
2137 t == TypeManager.sbyte_type ||
2138 t == TypeManager.char_type ||
2139 t == TypeManager.short_type ||
2140 t == TypeManager.ushort_type ||
2141 t == TypeManager.int32_type ||
2142 t == TypeManager.uint32_type ||
2143 t == TypeManager.int64_type ||
2144 t == TypeManager.uint64_type)
2146 throw new Exception ("Unhandled typecode in enum " + " from " + t.AssemblyQualifiedName);
2148 TypeCode tc = Type.GetTypeCode (t);
2151 case TypeCode.Boolean:
2152 return TypeManager.bool_type;
2154 return TypeManager.byte_type;
2155 case TypeCode.SByte:
2156 return TypeManager.sbyte_type;
2158 return TypeManager.char_type;
2159 case TypeCode.Int16:
2160 return TypeManager.short_type;
2161 case TypeCode.UInt16:
2162 return TypeManager.ushort_type;
2163 case TypeCode.Int32:
2164 return TypeManager.int32_type;
2165 case TypeCode.UInt32:
2166 return TypeManager.uint32_type;
2167 case TypeCode.Int64:
2168 return TypeManager.int64_type;
2169 case TypeCode.UInt64:
2170 return TypeManager.uint64_type;
2172 throw new Exception ("Unhandled typecode in enum " + tc + " from " + t.AssemblyQualifiedName);
2176 // When compiling corlib and called with one of the core types, return
2177 // the corresponding typebuilder for that type.
2179 public static Type TypeToCoreType (Type t)
2181 if (RootContext.StdLib || (t is TypeBuilder))
2184 TypeCode tc = Type.GetTypeCode (t);
2187 case TypeCode.Boolean:
2188 return TypeManager.bool_type;
2190 return TypeManager.byte_type;
2191 case TypeCode.SByte:
2192 return TypeManager.sbyte_type;
2194 return TypeManager.char_type;
2195 case TypeCode.Int16:
2196 return TypeManager.short_type;
2197 case TypeCode.UInt16:
2198 return TypeManager.ushort_type;
2199 case TypeCode.Int32:
2200 return TypeManager.int32_type;
2201 case TypeCode.UInt32:
2202 return TypeManager.uint32_type;
2203 case TypeCode.Int64:
2204 return TypeManager.int64_type;
2205 case TypeCode.UInt64:
2206 return TypeManager.uint64_type;
2207 case TypeCode.Single:
2208 return TypeManager.float_type;
2209 case TypeCode.Double:
2210 return TypeManager.double_type;
2211 case TypeCode.String:
2212 return TypeManager.string_type;
2214 if (t == typeof (void))
2215 return TypeManager.void_type;
2216 if (t == typeof (object))
2217 return TypeManager.object_type;
2218 if (t == typeof (System.Type))
2219 return TypeManager.type_type;
2220 if (t == typeof (System.IntPtr))
2221 return TypeManager.intptr_type;
2227 /// Utility function that can be used to probe whether a type
2228 /// is managed or not.
2230 public static bool VerifyUnManaged (Type t, Location loc)
2232 if (t.IsValueType || t.IsPointer){
2234 // FIXME: this is more complex, we actually need to
2235 // make sure that the type does not contain any
2241 if (!RootContext.StdLib && (t == TypeManager.decimal_type))
2242 // We need this explicit check here to make it work when
2243 // compiling corlib.
2248 "Cannot take the address or size of a variable of a managed type ('" +
2249 CSharpName (t) + "')");
2254 /// Returns the name of the indexer in a given type.
2257 /// The default is not always `Item'. The user can change this behaviour by
2258 /// using the DefaultMemberAttribute in the class.
2260 /// For example, the String class indexer is named `Chars' not `Item'
2262 public static string IndexerPropertyName (Type t)
2264 if (t is TypeBuilder) {
2265 if (t.IsInterface) {
2266 Interface i = LookupInterface (t);
2268 if ((i == null) || (i.IndexerName == null))
2271 return i.IndexerName;
2273 TypeContainer tc = LookupTypeContainer (t);
2275 if ((tc == null) || (tc.IndexerName == null))
2278 return tc.IndexerName;
2282 System.Attribute attr = System.Attribute.GetCustomAttribute (
2283 t, TypeManager.default_member_type);
2285 DefaultMemberAttribute dma = (DefaultMemberAttribute) attr;
2286 return dma.MemberName;
2292 static MethodInfo pinned_method = null;
2293 public static void MakePinned (LocalBuilder builder)
2295 if (pinned_method == null) {
2296 pinned_method = typeof (LocalBuilder).GetMethod ("MakePinned", BindingFlags.Instance | BindingFlags.NonPublic);
2297 if (pinned_method == null) {
2298 Report.Warning (-24, new Location (-1), "Microsoft.NET does not support making pinned variables." +
2299 "This code may cause errors on a runtime with a moving GC");
2305 pinned_method.Invoke (builder, null);
2310 // Returns whether the array of memberinfos contains the given method
2312 public static bool ArrayContainsMethod (MemberInfo [] array, MethodBase new_method)
2314 Type [] new_args = TypeManager.GetArgumentTypes (new_method);
2316 foreach (MethodBase method in array) {
2317 if (method.Name != new_method.Name)
2320 if (method is MethodInfo && new_method is MethodInfo)
2321 if (((MethodInfo) method).ReturnType != ((MethodInfo) new_method).ReturnType)
2325 Type [] old_args = TypeManager.GetArgumentTypes (method);
2326 int old_count = old_args.Length;
2329 if (new_args.Length != old_count)
2332 for (i = 0; i < old_count; i++){
2333 if (old_args [i] != new_args [i])
2346 // We copy methods from `new_members' into `target_list' if the signature
2347 // for the method from in the new list does not exist in the target_list
2349 // The name is assumed to be the same.
2351 public static ArrayList CopyNewMethods (ArrayList target_list, IList new_members)
2353 if (target_list == null){
2354 target_list = new ArrayList ();
2356 foreach (MemberInfo mi in new_members){
2357 if (mi is MethodBase)
2358 target_list.Add (mi);
2363 MemberInfo [] target_array = new MemberInfo [target_list.Count];
2364 target_list.CopyTo (target_array, 0);
2366 foreach (MemberInfo mi in new_members){
2367 MethodBase new_method = (MethodBase) mi;
2369 if (!ArrayContainsMethod (target_array, new_method))
2370 target_list.Add (new_method);
2376 public enum MethodFlags {
2378 IsObsoleteError = 1 << 1,
2379 ShouldIgnore = 1 << 2
2383 // Returns the TypeManager.MethodFlags for this method.
2384 // This emits an error 619 / warning 618 if the method is obsolete.
2385 // In the former case, TypeManager.MethodFlags.IsObsoleteError is returned.
2387 static public MethodFlags GetMethodFlags (MethodBase mb, Location loc)
2389 MethodFlags flags = 0;
2391 if (mb.DeclaringType is TypeBuilder){
2392 MethodData method = (MethodData) builder_to_method [mb];
2393 if (method == null) {
2394 // FIXME: implement Obsolete attribute on Property,
2395 // Indexer and Event.
2399 return method.GetMethodFlags (loc);
2402 object [] attrs = mb.GetCustomAttributes (true);
2403 foreach (object ta in attrs){
2404 if (!(ta is System.Attribute)){
2405 Console.WriteLine ("Unknown type in GetMethodFlags: " + ta);
2408 System.Attribute a = (System.Attribute) ta;
2409 if (a.TypeId == TypeManager.obsolete_attribute_type){
2410 ObsoleteAttribute oa = (ObsoleteAttribute) a;
2412 string method_desc = TypeManager.CSharpSignature (mb);
2415 Report.Error (619, loc, "Method `" + method_desc +
2416 "' is obsolete: `" + oa.Message + "'");
2417 return MethodFlags.IsObsoleteError;
2419 Report.Warning (618, loc, "Method `" + method_desc +
2420 "' is obsolete: `" + oa.Message + "'");
2422 flags |= MethodFlags.IsObsolete;
2428 // Skip over conditional code.
2430 if (a.TypeId == TypeManager.conditional_attribute_type){
2431 ConditionalAttribute ca = (ConditionalAttribute) a;
2433 if (RootContext.AllDefines [ca.ConditionString] == null)
2434 flags |= MethodFlags.ShouldIgnore;
2441 #region MemberLookup implementation
2444 // Whether we allow private members in the result (since FindMembers
2445 // uses NonPublic for both protected and private), we need to distinguish.
2447 static bool closure_private_ok;
2450 // Who is invoking us and which type is being queried currently.
2452 static Type closure_invocation_type;
2453 static Type closure_qualifier_type;
2456 // The assembly that defines the type is that is calling us
2458 static Assembly closure_invocation_assembly;
2460 static internal bool FilterNone (MemberInfo m, object filter_criteria)
2466 // This filter filters by name + whether it is ok to include private
2467 // members in the search
2469 static internal bool FilterWithClosure (MemberInfo m, object filter_criteria)
2472 // Hack: we know that the filter criteria will always be in the `closure'
2476 if ((filter_criteria != null) && (m.Name != (string) filter_criteria))
2479 if (((closure_qualifier_type == null) || (closure_qualifier_type == closure_invocation_type)) &&
2480 (m.DeclaringType == closure_invocation_type))
2484 // Ugly: we need to find out the type of `m', and depending
2485 // on this, tell whether we accept or not
2487 if (m is MethodBase){
2488 MethodBase mb = (MethodBase) m;
2489 MethodAttributes ma = mb.Attributes & MethodAttributes.MemberAccessMask;
2491 if (ma == MethodAttributes.Private)
2492 return closure_private_ok || (closure_invocation_type == m.DeclaringType) ||
2493 IsNestedChildOf (closure_invocation_type, m.DeclaringType);
2496 // FamAndAssem requires that we not only derivate, but we are on the
2499 if (ma == MethodAttributes.FamANDAssem){
2500 if (closure_invocation_assembly != mb.DeclaringType.Assembly)
2504 // Assembly and FamORAssem succeed if we're in the same assembly.
2505 if ((ma == MethodAttributes.Assembly) || (ma == MethodAttributes.FamORAssem)){
2506 if (closure_invocation_assembly == mb.DeclaringType.Assembly)
2510 // We already know that we aren't in the same assembly.
2511 if (ma == MethodAttributes.Assembly)
2514 // Family and FamANDAssem require that we derive.
2515 if ((ma == MethodAttributes.Family) || (ma == MethodAttributes.FamANDAssem)){
2516 if (closure_invocation_type == null)
2519 if (!IsSubclassOrNestedChildOf (closure_invocation_type, mb.DeclaringType))
2522 // Although a derived class can access protected members of its base class
2523 // it cannot do so through an instance of the base class (CS1540).
2524 if (!mb.IsStatic && (closure_invocation_type != closure_qualifier_type) &&
2525 (closure_qualifier_type != null) &&
2526 closure_invocation_type.IsSubclassOf (closure_qualifier_type) &&
2527 !TypeManager.IsNestedChildOf (closure_invocation_type, closure_qualifier_type))
2537 if (m is FieldInfo){
2538 FieldInfo fi = (FieldInfo) m;
2539 FieldAttributes fa = fi.Attributes & FieldAttributes.FieldAccessMask;
2541 if (fa == FieldAttributes.Private)
2542 return closure_private_ok || (closure_invocation_type == m.DeclaringType) ||
2543 IsNestedChildOf (closure_invocation_type, m.DeclaringType);
2546 // FamAndAssem requires that we not only derivate, but we are on the
2549 if (fa == FieldAttributes.FamANDAssem){
2550 if (closure_invocation_assembly != fi.DeclaringType.Assembly)
2554 // Assembly and FamORAssem succeed if we're in the same assembly.
2555 if ((fa == FieldAttributes.Assembly) || (fa == FieldAttributes.FamORAssem)){
2556 if (closure_invocation_assembly == fi.DeclaringType.Assembly)
2560 // We already know that we aren't in the same assembly.
2561 if (fa == FieldAttributes.Assembly)
2564 // Family and FamANDAssem require that we derive.
2565 if ((fa == FieldAttributes.Family) || (fa == FieldAttributes.FamANDAssem)){
2566 if (closure_invocation_type == null)
2569 if (!IsSubclassOrNestedChildOf (closure_invocation_type, fi.DeclaringType))
2572 // Although a derived class can access protected members of its base class
2573 // it cannot do so through an instance of the base class (CS1540).
2574 if (!fi.IsStatic && (closure_invocation_type != closure_qualifier_type) &&
2575 (closure_qualifier_type != null) &&
2576 closure_invocation_type.IsSubclassOf (closure_qualifier_type) &&
2577 !TypeManager.IsNestedChildOf (closure_invocation_type, closure_qualifier_type))
2588 // EventInfos and PropertyInfos, return true because they lack permission
2589 // informaiton, so we need to check later on the methods.
2594 static MemberFilter FilterWithClosure_delegate = new MemberFilter (FilterWithClosure);
2595 static MemberFilter FilterNone_delegate = new MemberFilter (FilterNone);
2598 // Looks up a member called `name' in the `queried_type'. This lookup
2599 // is done by code that is contained in the definition for `invocation_type'
2600 // through a qualifier of type `qualifier_type' (or null if there is no qualifier).
2602 // `invocation_type' is used to check whether we're allowed to access the requested
2603 // member wrt its protection level.
2605 // When called from MemberAccess, `qualifier_type' is the type which is used to access
2606 // the requested member (`class B { A a = new A (); a.foo = 5; }'; here invocation_type
2607 // is B and qualifier_type is A). This is used to do the CS1540 check.
2609 // When resolving a SimpleName, `qualifier_type' is null.
2611 // The `qualifier_type' is used for the CS1540 check; it's normally either null or
2612 // the same than `queried_type' - except when we're being called from BaseAccess;
2613 // in this case, `invocation_type' is the current type and `queried_type' the base
2614 // type, so this'd normally trigger a CS1540.
2616 // The binding flags are `bf' and the kind of members being looked up are `mt'
2618 // The return value always includes private members which code in `invocation_type'
2619 // is allowed to access (using the specified `qualifier_type' if given); only use
2620 // BindingFlags.NonPublic to bypass the permission check.
2622 // Returns an array of a single element for everything but Methods/Constructors
2623 // that might return multiple matches.
2625 public static MemberInfo [] MemberLookup (Type invocation_type, Type qualifier_type,
2626 Type queried_type, MemberTypes mt,
2627 BindingFlags original_bf, string name)
2629 Timer.StartTimer (TimerType.MemberLookup);
2631 MemberInfo[] retval = RealMemberLookup (invocation_type, qualifier_type,
2632 queried_type, mt, original_bf, name);
2634 Timer.StopTimer (TimerType.MemberLookup);
2639 static MemberInfo [] RealMemberLookup (Type invocation_type, Type qualifier_type,
2640 Type queried_type, MemberTypes mt,
2641 BindingFlags original_bf, string name)
2643 BindingFlags bf = original_bf;
2645 ArrayList method_list = null;
2646 Type current_type = queried_type;
2647 bool searching = (original_bf & BindingFlags.DeclaredOnly) == 0;
2648 bool skip_iface_check = true, used_cache = false;
2649 bool always_ok_flag = false;
2651 closure_invocation_type = invocation_type;
2652 closure_invocation_assembly = invocation_type != null ? invocation_type.Assembly : null;
2653 closure_qualifier_type = qualifier_type;
2656 // If we are a nested class, we always have access to our container
2659 if (invocation_type != null){
2660 string invocation_name = invocation_type.FullName;
2661 if (invocation_name.IndexOf ('+') != -1){
2662 string container = queried_type.FullName + "+";
2663 int container_length = container.Length;
2665 if (invocation_name.Length > container_length){
2666 string shared = invocation_name.Substring (0, container_length);
2668 if (shared == container)
2669 always_ok_flag = true;
2674 // This is from the first time we find a method
2675 // in most cases, we do not actually find a method in the base class
2676 // so we can just ignore it, and save the arraylist allocation
2677 MemberInfo [] first_members_list = null;
2678 bool use_first_members_list = false;
2684 // `NonPublic' is lame, because it includes both protected and
2685 // private methods, so we need to control this behavior by
2686 // explicitly tracking if a private method is ok or not.
2688 // The possible cases are:
2689 // public, private and protected (internal does not come into the
2692 if ((invocation_type != null) &&
2693 ((invocation_type == current_type) ||
2694 IsNestedChildOf (invocation_type, current_type)) ||
2696 bf = original_bf | BindingFlags.NonPublic;
2700 closure_private_ok = (original_bf & BindingFlags.NonPublic) != 0;
2702 Timer.StopTimer (TimerType.MemberLookup);
2704 list = MemberLookup_FindMembers (current_type, mt, bf, name, out used_cache);
2706 Timer.StartTimer (TimerType.MemberLookup);
2709 // When queried for an interface type, the cache will automatically check all
2710 // inherited members, so we don't need to do this here. However, this only
2711 // works if we already used the cache in the first iteration of this loop.
2713 // If we used the cache in any further iteration, we can still terminate the
2714 // loop since the cache always looks in all parent classes.
2720 skip_iface_check = false;
2722 if (current_type == TypeManager.object_type)
2725 current_type = current_type.BaseType;
2728 // This happens with interfaces, they have a null
2729 // basetype. Look members up in the Object class.
2731 if (current_type == null)
2732 current_type = TypeManager.object_type;
2735 if (list.Length == 0)
2739 // Events and types are returned by both `static' and `instance'
2740 // searches, which means that our above FindMembers will
2741 // return two copies of the same.
2743 if (list.Length == 1 && !(list [0] is MethodBase)){
2748 // Multiple properties: we query those just to find out the indexer
2751 if (list [0] is PropertyInfo)
2755 // We found an event: the cache lookup returns both the event and
2756 // its private field.
2758 if (list [0] is EventInfo) {
2759 if ((list.Length == 2) && (list [1] is FieldInfo))
2760 return new MemberInfo [] { list [0] };
2767 // We found methods, turn the search into "method scan"
2771 if (first_members_list != null) {
2772 if (use_first_members_list) {
2773 method_list = CopyNewMethods (method_list, first_members_list);
2774 use_first_members_list = false;
2777 method_list = CopyNewMethods (method_list, list);
2779 first_members_list = list;
2780 use_first_members_list = true;
2781 mt &= (MemberTypes.Method | MemberTypes.Constructor);
2783 } while (searching);
2785 if (use_first_members_list) {
2786 foreach (MemberInfo mi in first_members_list) {
2787 if (! (mi is MethodBase)) {
2788 method_list = CopyNewMethods (method_list, first_members_list);
2789 return (MemberInfo []) method_list.ToArray (typeof (MemberInfo));
2792 return (MemberInfo []) first_members_list;
2795 if (method_list != null && method_list.Count > 0)
2796 return (MemberInfo []) method_list.ToArray (typeof (MemberInfo));
2799 // This happens if we already used the cache in the first iteration, in this case
2800 // the cache already looked in all interfaces.
2802 if (skip_iface_check)
2806 // Interfaces do not list members they inherit, so we have to
2809 if (!queried_type.IsInterface)
2812 if (queried_type.IsArray)
2813 queried_type = TypeManager.array_type;
2815 TypeExpr [] ifaces = GetInterfaces (queried_type);
2819 foreach (TypeExpr itype in ifaces){
2822 x = MemberLookup (null, null, itype.Type, mt, bf, name);
2831 // This is used to extract properties and event declarations from a type
2833 static MemberInfo [] SpecialContainerLookup (Type t, bool is_static)
2835 BindingFlags bf = BindingFlags.DeclaredOnly | (is_static ? BindingFlags.Static : BindingFlags.Instance);
2837 bf |= BindingFlags.Public | BindingFlags.NonPublic;
2839 if (t is TypeBuilder) {
2840 DeclSpace decl = (DeclSpace) builder_to_declspace [t];
2842 return (MemberInfo []) decl.FindMembers (
2843 MemberTypes.Property | MemberTypes.Event,
2844 bf, FilterNone_delegate, null);
2846 return t.FindMembers (MemberTypes.Property | MemberTypes.Event,
2847 bf, FilterNone_delegate, null);
2852 public static bool IsSpecialMethod (MethodBase mb)
2854 Type t = mb.DeclaringType;
2856 MemberInfo [] matches = TypeManager.SpecialContainerLookup (t, mb.IsStatic);
2857 if (matches == null)
2860 foreach (MemberInfo mi in matches){
2861 if (mi is PropertyBuilder){
2862 Pair p = (Pair) properties [mi];
2864 if (p.First == mb || p.Second == mb)
2866 } else if (mi is PropertyInfo){
2867 MethodInfo [] methods = ((PropertyInfo) mi).GetAccessors (true);
2869 foreach (MethodInfo m in methods){
2873 } else if (mi is MyEventBuilder){
2874 Pair p = (Pair) events [mi];
2876 if (p.First == mb || p.Second == mb)
2878 } else if (mi is EventInfo){
2879 EventInfo ei = ((EventInfo) mi);
2881 if (ei.GetAddMethod (true) == mb)
2884 if (ei.GetRemoveMethod (true) == mb)
2887 if (ei.GetRaiseMethod (true) == mb)
2893 // Now check if it is an operator method
2897 if (s.StartsWith ("op_")){
2898 foreach (string name in Unary.oper_names){
2903 foreach (string name in Binary.oper_names){
2917 /// There is exactly one instance of this class per type.
2919 public sealed class TypeHandle : IMemberContainer {
2920 public readonly TypeHandle BaseType;
2922 readonly int id = ++next_id;
2923 static int next_id = 0;
2926 /// Lookup a TypeHandle instance for the given type. If the type doesn't have
2927 /// a TypeHandle yet, a new instance of it is created. This static method
2928 /// ensures that we'll only have one TypeHandle instance per type.
2930 public static TypeHandle GetTypeHandle (Type t)
2932 TypeHandle handle = (TypeHandle) type_hash [t];
2936 handle = new TypeHandle (t);
2937 type_hash.Add (t, handle);
2941 public static void CleanUp ()
2947 /// Returns the TypeHandle for TypeManager.object_type.
2949 public static IMemberContainer ObjectType {
2951 if (object_type != null)
2954 object_type = GetTypeHandle (TypeManager.object_type);
2961 /// Returns the TypeHandle for TypeManager.array_type.
2963 public static IMemberContainer ArrayType {
2965 if (array_type != null)
2968 array_type = GetTypeHandle (TypeManager.array_type);
2974 private static PtrHashtable type_hash = new PtrHashtable ();
2976 private static TypeHandle object_type = null;
2977 private static TypeHandle array_type = null;
2980 private bool is_interface;
2981 private MemberCache member_cache;
2983 private TypeHandle (Type type)
2986 if (type.BaseType != null)
2987 BaseType = GetTypeHandle (type.BaseType);
2988 this.is_interface = type.IsInterface;
2989 this.member_cache = new MemberCache (this);
2992 // IMemberContainer methods
2994 public string Name {
2996 return type.FullName;
3006 public IMemberContainer Parent {
3012 public bool IsInterface {
3014 return is_interface;
3018 public MemberList GetMembers (MemberTypes mt, BindingFlags bf)
3020 MemberInfo [] members;
3021 if (mt == MemberTypes.Event)
3022 members = type.GetEvents (bf | BindingFlags.DeclaredOnly);
3024 members = type.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
3026 Array.Reverse (members);
3028 return new MemberList (members);
3031 // IMemberFinder methods
3033 public MemberList FindMembers (MemberTypes mt, BindingFlags bf, string name,
3034 MemberFilter filter, object criteria)
3036 return new MemberList (member_cache.FindMembers (mt, bf, name, filter, criteria));
3039 public MemberCache MemberCache {
3041 return member_cache;
3045 public override string ToString ()
3047 if (BaseType != null)
3048 return "TypeHandle (" + id + "," + Name + " : " + BaseType + ")";
3050 return "TypeHandle (" + id + "," + Name + ")";