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 iconvertible_type;
67 static public Type default_member_type;
68 static public Type iasyncresult_type;
69 static public Type asynccallback_type;
70 static public Type intptr_type;
71 static public Type monitor_type;
72 static public Type runtime_field_handle_type;
73 static public Type runtime_argument_handle_type;
74 static public Type attribute_type;
75 static public Type attribute_usage_type;
76 static public Type dllimport_type;
77 static public Type unverifiable_code_type;
78 static public Type methodimpl_attr_type;
79 static public Type marshal_as_attr_type;
80 static public Type param_array_type;
81 static public Type guid_attr_type;
82 static public Type void_ptr_type;
83 static public Type indexer_name_type;
84 static public Type exception_type;
85 static public Type invalid_operation_exception_type;
86 static public Type obsolete_attribute_type;
87 static public Type conditional_attribute_type;
88 static public Type in_attribute_type;
89 static public Type cls_compliant_attribute_type;
90 static public Type typed_reference_type;
91 static public Type arg_iterator_type;
92 static public Type mbr_type;
93 static public Type struct_layout_attribute_type;
94 static public Type field_offset_attribute_type;
97 // An empty array of types
99 static public Type [] NoTypes;
100 static public TypeExpr [] NoTypeExprs;
104 // Expressions representing the internal types. Used during declaration
107 static public TypeExpr system_object_expr, system_string_expr;
108 static public TypeExpr system_boolean_expr, system_decimal_expr;
109 static public TypeExpr system_single_expr, system_double_expr;
110 static public TypeExpr system_sbyte_expr, system_byte_expr;
111 static public TypeExpr system_int16_expr, system_uint16_expr;
112 static public TypeExpr system_int32_expr, system_uint32_expr;
113 static public TypeExpr system_int64_expr, system_uint64_expr;
114 static public TypeExpr system_char_expr, system_void_expr;
115 static public TypeExpr system_asynccallback_expr;
116 static public TypeExpr system_iasyncresult_expr;
117 static public TypeExpr system_valuetype_expr;
120 // This is only used when compiling corlib
122 static public Type system_int32_type;
123 static public Type system_array_type;
124 static public Type system_type_type;
125 static public Type system_assemblybuilder_type;
126 static public MethodInfo system_int_array_get_length;
127 static public MethodInfo system_int_array_get_rank;
128 static public MethodInfo system_object_array_clone;
129 static public MethodInfo system_int_array_get_length_int;
130 static public MethodInfo system_int_array_get_lower_bound_int;
131 static public MethodInfo system_int_array_get_upper_bound_int;
132 static public MethodInfo system_void_array_copyto_array_int;
136 // Internal, not really used outside
138 static Type runtime_helpers_type;
141 // These methods are called by code generated by the compiler
143 static public MethodInfo string_concat_string_string;
144 static public MethodInfo string_concat_string_string_string;
145 static public MethodInfo string_concat_string_string_string_string;
146 static public MethodInfo string_concat_string_dot_dot_dot;
147 static public MethodInfo string_concat_object_object;
148 static public MethodInfo string_concat_object_object_object;
149 static public MethodInfo string_concat_object_dot_dot_dot;
150 static public MethodInfo string_isinterneted_string;
151 static public MethodInfo system_type_get_type_from_handle;
152 static public MethodInfo object_getcurrent_void;
153 static public MethodInfo bool_movenext_void;
154 static public MethodInfo ienumerable_getenumerator_void;
155 static public MethodInfo void_reset_void;
156 static public MethodInfo void_dispose_void;
157 static public MethodInfo void_monitor_enter_object;
158 static public MethodInfo void_monitor_exit_object;
159 static public MethodInfo void_initializearray_array_fieldhandle;
160 static public MethodInfo int_getlength_int;
161 static public MethodInfo delegate_combine_delegate_delegate;
162 static public MethodInfo delegate_remove_delegate_delegate;
163 static public MethodInfo int_get_offset_to_string_data;
164 static public MethodInfo int_array_get_length;
165 static public MethodInfo int_array_get_rank;
166 static public MethodInfo object_array_clone;
167 static public MethodInfo int_array_get_length_int;
168 static public MethodInfo int_array_get_lower_bound_int;
169 static public MethodInfo int_array_get_upper_bound_int;
170 static public MethodInfo void_array_copyto_array_int;
173 // The attribute constructors.
175 static public ConstructorInfo object_ctor;
176 static public ConstructorInfo cons_param_array_attribute;
177 static public ConstructorInfo void_decimal_ctor_five_args;
178 static public ConstructorInfo unverifiable_code_ctor;
179 static public ConstructorInfo invalid_operation_ctor;
182 // Holds the Array of Assemblies that have been loaded
183 // (either because it is the default or the user used the
184 // -r command line option)
186 static Assembly [] assemblies;
189 // Keeps a list of modules. We used this to do lookups
190 // on the module using GetType -- needed for arrays
192 static Module [] modules;
195 // This is the type_cache from the assemblies to avoid
196 // hitting System.Reflection on every lookup.
198 static Hashtable types;
201 // This is used to hotld the corresponding TypeContainer objects
202 // since we need this in FindMembers
204 static Hashtable typecontainers;
207 // Keeps track of those types that are defined by the
210 static ArrayList user_types;
212 static PtrHashtable builder_to_declspace;
215 // Tracks the interfaces implemented by typebuilders. We only
216 // enter those who do implement or or more interfaces
218 static PtrHashtable builder_to_ifaces;
221 // Maps MethodBase.RuntimeTypeHandle to a Type array that contains
222 // the arguments to the method
224 static Hashtable method_arguments;
227 // Maps PropertyBuilder to a Type array that contains
228 // the arguments to the indexer
230 static Hashtable indexer_arguments;
233 // Maybe `method_arguments' should be replaced and only
234 // method_internal_params should be kept?
236 static Hashtable method_internal_params;
239 // Keeps track of methods
242 static Hashtable builder_to_method;
245 // Contains all public types from referenced assemblies.
246 // This member is used only if CLS Compliance verification is required.
248 public static Hashtable all_imported_types;
255 public static void CleanUp ()
257 // Lets get everything clean so that we can collect before generating code
261 typecontainers = null;
263 builder_to_declspace = null;
264 builder_to_ifaces = null;
265 method_arguments = null;
266 indexer_arguments = null;
267 method_internal_params = null;
268 builder_to_method = null;
272 negative_hits = null;
273 builder_to_constant = null;
274 fieldbuilders_to_fields = null;
276 priv_fields_events = null;
279 TypeHandle.CleanUp ();
283 /// A filter for Findmembers that uses the Signature object to
286 static bool SignatureFilter (MemberInfo mi, object criteria)
288 Signature sig = (Signature) criteria;
290 if (!(mi is MethodBase))
293 if (mi.Name != sig.name)
296 int count = sig.args.Length;
298 if (mi is MethodBuilder || mi is ConstructorBuilder){
299 Type [] candidate_args = GetArgumentTypes ((MethodBase) mi);
301 if (candidate_args.Length != count)
304 for (int i = 0; i < count; i++)
305 if (candidate_args [i] != sig.args [i])
310 ParameterInfo [] pars = ((MethodBase) mi).GetParameters ();
312 if (pars.Length != count)
315 for (int i = 0; i < count; i++)
316 if (pars [i].ParameterType != sig.args [i])
322 // A delegate that points to the filter above.
323 static MemberFilter signature_filter;
326 // These are expressions that represent some of the internal data types, used
329 static void InitExpressionTypes ()
331 system_object_expr = new TypeLookupExpression ("System.Object");
332 system_string_expr = new TypeLookupExpression ("System.String");
333 system_boolean_expr = new TypeLookupExpression ("System.Boolean");
334 system_decimal_expr = new TypeLookupExpression ("System.Decimal");
335 system_single_expr = new TypeLookupExpression ("System.Single");
336 system_double_expr = new TypeLookupExpression ("System.Double");
337 system_sbyte_expr = new TypeLookupExpression ("System.SByte");
338 system_byte_expr = new TypeLookupExpression ("System.Byte");
339 system_int16_expr = new TypeLookupExpression ("System.Int16");
340 system_uint16_expr = new TypeLookupExpression ("System.UInt16");
341 system_int32_expr = new TypeLookupExpression ("System.Int32");
342 system_uint32_expr = new TypeLookupExpression ("System.UInt32");
343 system_int64_expr = new TypeLookupExpression ("System.Int64");
344 system_uint64_expr = new TypeLookupExpression ("System.UInt64");
345 system_char_expr = new TypeLookupExpression ("System.Char");
346 system_void_expr = new TypeLookupExpression ("System.Void");
347 system_asynccallback_expr = new TypeLookupExpression ("System.AsyncCallback");
348 system_iasyncresult_expr = new TypeLookupExpression ("System.IAsyncResult");
349 system_valuetype_expr = new TypeLookupExpression ("System.ValueType");
352 static TypeManager ()
354 assemblies = new Assembly [0];
356 user_types = new ArrayList ();
358 types = new Hashtable ();
359 typecontainers = new Hashtable ();
361 builder_to_declspace = new PtrHashtable ();
362 builder_to_method = new PtrHashtable ();
363 method_arguments = new PtrHashtable ();
364 method_internal_params = new PtrHashtable ();
365 indexer_arguments = new PtrHashtable ();
366 builder_to_ifaces = new PtrHashtable ();
368 NoTypes = new Type [0];
369 NoTypeExprs = new TypeExpr [0];
371 signature_filter = new MemberFilter (SignatureFilter);
372 InitExpressionTypes ();
375 public static void HandleDuplicate (string name, Type t)
377 Type prev = (Type) types [name];
378 TypeContainer tc = builder_to_declspace [prev] as TypeContainer;
382 // This probably never happens, as we catch this before
384 Report.Error (-17, "The type `" + name + "' has already been defined.");
388 tc = builder_to_declspace [t] as TypeContainer;
391 1595, "The type `" + name + "' is defined in an existing assembly;"+
392 " Using the new definition from: " + tc.Location);
395 1595, "The type `" + name + "' is defined in an existing assembly;");
398 Report.Warning (1595, "Previously defined in: " + prev.Assembly.FullName);
404 public static void AddUserType (string name, TypeBuilder t, TypeExpr[] ifaces)
409 HandleDuplicate (name, t);
414 builder_to_ifaces [t] = ifaces;
418 // This entry point is used by types that we define under the covers
420 public static void RegisterBuilder (TypeBuilder tb, TypeExpr [] ifaces)
423 builder_to_ifaces [tb] = ifaces;
426 public static void AddUserType (string name, TypeBuilder t, TypeContainer tc, TypeExpr [] ifaces)
428 builder_to_declspace.Add (t, tc);
429 typecontainers.Add (name, tc);
430 AddUserType (name, t, ifaces);
433 public static void AddDelegateType (string name, TypeBuilder t, Delegate del)
438 HandleDuplicate (name, t);
441 builder_to_declspace.Add (t, del);
444 public static void AddEnumType (string name, TypeBuilder t, Enum en)
449 HandleDuplicate (name, t);
451 builder_to_declspace.Add (t, en);
454 public static void AddUserInterface (string name, TypeBuilder t, Interface i, TypeExpr [] ifaces)
456 AddUserType (name, t, ifaces);
457 builder_to_declspace.Add (t, i);
461 public static void AddMethod (MethodBase builder, IMethodData method)
463 builder_to_method.Add (builder, method);
466 public static IMethodData GetMethod (MethodBase builder)
468 return (IMethodData) builder_to_method [builder];
472 /// Returns the DeclSpace whose Type is `t' or null if there is no
473 /// DeclSpace for `t' (ie, the Type comes from a library)
475 public static DeclSpace LookupDeclSpace (Type t)
477 return builder_to_declspace [t] as DeclSpace;
481 /// Returns the TypeContainer whose Type is `t' or null if there is no
482 /// TypeContainer for `t' (ie, the Type comes from a library)
484 public static TypeContainer LookupTypeContainer (Type t)
486 return builder_to_declspace [t] as TypeContainer;
489 public static IMemberContainer LookupMemberContainer (Type t)
491 if (t is TypeBuilder) {
492 IMemberContainer container = builder_to_declspace [t] as IMemberContainer;
493 if (container != null)
497 return TypeHandle.GetTypeHandle (t);
500 public static Interface LookupInterface (Type t)
502 return builder_to_declspace [t] as Interface;
505 public static Delegate LookupDelegate (Type t)
507 return builder_to_declspace [t] as Delegate;
510 public static Enum LookupEnum (Type t)
512 return builder_to_declspace [t] as Enum;
515 public static Class LookupClass (Type t)
517 return (Class) builder_to_declspace [t];
521 /// Registers an assembly to load types from.
523 public static void AddAssembly (Assembly a)
525 foreach (Assembly assembly in assemblies) {
530 int top = assemblies.Length;
531 Assembly [] n = new Assembly [top + 1];
533 assemblies.CopyTo (n, 0);
539 public static Assembly [] GetAssemblies ()
545 /// Registers a module builder to lookup types from
547 public static void AddModule (Module mb)
549 int top = modules != null ? modules.Length : 0;
550 Module [] n = new Module [top + 1];
553 modules.CopyTo (n, 0);
558 public static Module[] Modules {
564 static Hashtable references = new Hashtable ();
567 // Gets the reference to T version of the Type (T&)
569 public static Type GetReferenceType (Type t)
571 string tname = t.FullName + "&";
573 Type ret = t.Assembly.GetType (tname);
576 // If the type comes from the assembly we are building
577 // We need the Hashtable, because .NET 1.1 will return different instance types
578 // every time we call ModuleBuilder.GetType.
581 if (references [t] == null)
582 references [t] = CodeGen.Module.Builder.GetType (tname);
583 ret = (Type) references [t];
589 static Hashtable pointers = new Hashtable ();
592 // Gets the pointer to T version of the Type (T*)
594 public static Type GetPointerType (Type t)
596 string tname = t.FullName + "*";
598 Type ret = t.Assembly.GetType (tname);
601 // If the type comes from the assembly we are building
602 // We need the Hashtable, because .NET 1.1 will return different instance types
603 // every time we call ModuleBuilder.GetType.
606 if (pointers [t] == null)
607 pointers [t] = CodeGen.Module.Builder.GetType (tname);
609 ret = (Type) pointers [t];
616 // Low-level lookup, cache-less
618 static Type LookupTypeReflection (string name)
622 foreach (Assembly a in assemblies){
623 t = a.GetType (name);
628 TypeAttributes ta = t.Attributes & TypeAttributes.VisibilityMask;
629 if (ta == TypeAttributes.NotPublic ||
630 ta == TypeAttributes.NestedPrivate ||
631 ta == TypeAttributes.NestedAssembly ||
632 ta == TypeAttributes.NestedFamANDAssem){
635 // In .NET pointers turn out to be private, even if their
636 // element type is not
639 t = t.GetElementType ();
649 foreach (Module mb in modules) {
650 t = mb.GetType (name);
658 static Hashtable negative_hits = new Hashtable ();
661 // This function is used when you want to avoid the lookups, and want to go
662 // directly to the source. This will use the cache.
664 // Notice that bypassing the cache is bad, because on Microsoft.NET runtime
665 // GetType ("DynamicType[]") != GetType ("DynamicType[]"), and there is no
666 // way to test things other than doing a fullname compare
668 public static Type LookupTypeDirect (string name)
670 Type t = (Type) types [name];
674 if (negative_hits.Contains (name))
677 t = LookupTypeReflection (name);
680 negative_hits [name] = null;
687 static readonly char [] dot_array = { '.' };
690 /// Returns the Type associated with @name, takes care of the fact that
691 /// reflection expects nested types to be separated from the main type
692 /// with a "+" instead of a "."
694 public static Type LookupType (string name)
699 // First lookup in user defined and cached values
702 t = (Type) types [name];
706 // Two thirds of the failures are caught here.
707 if (negative_hits.Contains (name))
710 // Sadly, split takes a param array, so this ends up allocating *EVERY TIME*
711 string [] elements = name.Split (dot_array);
712 int count = elements.Length;
714 for (int n = 1; n <= count; n++){
715 string top_level_type = String.Join (".", elements, 0, n);
717 // One third of the failures are caught here.
718 if (negative_hits.Contains (top_level_type))
721 t = (Type) types [top_level_type];
723 t = LookupTypeReflection (top_level_type);
725 negative_hits [top_level_type] = null;
736 // We know that System.Object does not have children, and since its the parent of
737 // all the objects, it always gets probbed for inner classes.
739 if (top_level_type == "System.Object")
742 string newt = top_level_type + "+" + String.Join ("+", elements, n, count - n);
743 //Console.WriteLine ("Looking up: " + newt + " " + name);
744 t = LookupTypeReflection (newt);
746 negative_hits [name] = null;
751 negative_hits [name] = null;
756 /// Computes the namespaces that we import from the assemblies we reference.
758 public static void ComputeNamespaces ()
760 MethodInfo assembly_get_namespaces = typeof (Assembly).GetMethod ("GetNamespaces", BindingFlags.Instance|BindingFlags.NonPublic);
763 // First add the assembly namespaces
765 if (assembly_get_namespaces != null){
766 int count = assemblies.Length;
768 for (int i = 0; i < count; i++){
769 Assembly a = assemblies [i];
770 string [] namespaces = (string []) assembly_get_namespaces.Invoke (a, null);
771 foreach (string ns in namespaces){
774 Namespace.LookupNamespace (ns, true);
778 Hashtable cache = new Hashtable ();
779 cache.Add ("", null);
780 foreach (Assembly a in assemblies) {
781 foreach (Type t in a.GetExportedTypes ()) {
782 string ns = t.Namespace;
783 if (ns == null || cache.Contains (ns))
786 Namespace.LookupNamespace (ns, true);
787 cache.Add (ns, null);
794 /// Fills static table with exported types from all referenced assemblies.
795 /// This information is required for CLS Compliance tests.
797 public static void LoadAllImportedTypes ()
799 if (!CodeGen.Assembly.IsClsCompliant)
802 all_imported_types = new Hashtable ();
803 foreach (Assembly a in assemblies) {
804 foreach (Type t in a.GetExportedTypes ()) {
805 all_imported_types [t.FullName] = t;
810 public static bool NamespaceClash (string name, Location loc)
812 if (Namespace.LookupNamespace (name, false) == null)
815 Report.Error (519, loc, String.Format ("`{0}' clashes with a predefined namespace", name));
820 /// Returns the C# name of a type if possible, or the full type name otherwise
822 static public string CSharpName (Type t)
824 return Regex.Replace (t.FullName,
826 @"(Int32|UInt32|Int16|UInt16|Int64|UInt64|" +
827 @"Single|Double|Char|Decimal|Byte|SByte|Object|" +
828 @"Boolean|String|Void)" +
830 new MatchEvaluator (CSharpNameMatch));
833 static String CSharpNameMatch (Match match)
835 string s = match.Groups [1].Captures [0].Value;
837 Replace ("int32", "int").
838 Replace ("uint32", "uint").
839 Replace ("int16", "short").
840 Replace ("uint16", "ushort").
841 Replace ("int64", "long").
842 Replace ("uint64", "ulong").
843 Replace ("single", "float").
844 Replace ("boolean", "bool")
845 + match.Groups [2].Captures [0].Value;
849 /// Returns the signature of the method with full namespace classification
851 static public string GetFullNameSignature (MemberInfo mi)
853 return mi.DeclaringType.FullName.Replace ('+', '.') + '.' + mi.Name;
856 static public string GetFullNameSignature (MethodBase mb)
858 string name = mb.Name;
860 name = mb.DeclaringType.Name;
862 if (mb.IsSpecialName) {
863 if (name.StartsWith ("get_") || name.StartsWith ("set_")) {
864 name = name.Remove (0, 4);
871 return mb.DeclaringType.FullName.Replace ('+', '.') + '.' + name;
875 /// Returns the signature of the property and indexer
877 static public string CSharpSignature (PropertyBuilder pb, bool is_indexer)
880 return GetFullNameSignature (pb);
883 MethodBase mb = pb.GetSetMethod (true) != null ? pb.GetSetMethod (true) : pb.GetGetMethod (true);
884 string signature = GetFullNameSignature (mb);
885 string arg = TypeManager.LookupParametersByBuilder (mb).ParameterDesc (0);
886 return String.Format ("{0}.this[{1}]", signature.Substring (0, signature.LastIndexOf ('.')), arg);
890 /// Returns the signature of the method
892 static public string CSharpSignature (MethodBase mb)
894 StringBuilder sig = new StringBuilder ("(");
897 // FIXME: We should really have a single function to do
898 // everything instead of the following 5 line pattern
900 ParameterData iparams = LookupParametersByBuilder (mb);
903 iparams = new ReflectionParameters (mb);
906 if (mb.IsSpecialName && iparams.Count == 0)
907 return GetFullNameSignature (mb);
909 for (int i = 0; i < iparams.Count; i++) {
913 sig.Append (iparams.ParameterDesc (i));
918 if (mb.IsSpecialName && iparams.Count == 1) {
919 sig.Replace ('(', '[');
920 sig.Replace (')', ']');
923 return GetFullNameSignature (mb) + sig.ToString ();
927 /// Looks up a type, and aborts if it is not found. This is used
928 /// by types required by the compiler
930 static Type CoreLookupType (string name)
932 Type t = LookupTypeDirect (name);
935 Report.Error (518, "The predefined type `" + name + "' is not defined or imported");
936 Environment.Exit (1);
943 /// Returns the MethodInfo for a method named `name' defined
944 /// in type `t' which takes arguments of types `args'
946 static MethodInfo GetMethod (Type t, string name, Type [] args, bool is_private, bool report_errors)
950 BindingFlags flags = instance_and_static | BindingFlags.Public;
956 flags |= BindingFlags.NonPublic;
958 list = FindMembers (t, MemberTypes.Method, flags, signature_filter, sig);
959 if (list.Count == 0) {
961 Report.Error (-19, "Can not find the core function `" + name + "'");
965 MethodInfo mi = list [0] as MethodInfo;
968 Report.Error (-19, "Can not find the core function `" + name + "'");
975 static MethodInfo GetMethod (Type t, string name, Type [] args, bool report_errors)
977 return GetMethod (t, name, args, false, report_errors);
980 static MethodInfo GetMethod (Type t, string name, Type [] args)
982 return GetMethod (t, name, args, true);
987 /// Returns the ConstructorInfo for "args"
989 static ConstructorInfo GetConstructor (Type t, Type [] args)
997 list = FindMembers (t, MemberTypes.Constructor,
998 instance_and_static | BindingFlags.Public | BindingFlags.DeclaredOnly,
999 signature_filter, sig);
1000 if (list.Count == 0){
1001 Report.Error (-19, "Can not find the core constructor for type `" + t.Name + "'");
1005 ConstructorInfo ci = list [0] as ConstructorInfo;
1007 Report.Error (-19, "Can not find the core constructor for type `" + t.Name + "'");
1014 public static void InitEnumUnderlyingTypes ()
1017 int32_type = CoreLookupType ("System.Int32");
1018 int64_type = CoreLookupType ("System.Int64");
1019 uint32_type = CoreLookupType ("System.UInt32");
1020 uint64_type = CoreLookupType ("System.UInt64");
1021 byte_type = CoreLookupType ("System.Byte");
1022 sbyte_type = CoreLookupType ("System.SByte");
1023 short_type = CoreLookupType ("System.Int16");
1024 ushort_type = CoreLookupType ("System.UInt16");
1028 /// The types have to be initialized after the initial
1029 /// population of the type has happened (for example, to
1030 /// bootstrap the corlib.dll
1032 public static void InitCoreTypes ()
1034 object_type = CoreLookupType ("System.Object");
1035 value_type = CoreLookupType ("System.ValueType");
1037 InitEnumUnderlyingTypes ();
1039 char_type = CoreLookupType ("System.Char");
1040 string_type = CoreLookupType ("System.String");
1041 float_type = CoreLookupType ("System.Single");
1042 double_type = CoreLookupType ("System.Double");
1043 char_ptr_type = CoreLookupType ("System.Char*");
1044 decimal_type = CoreLookupType ("System.Decimal");
1045 bool_type = CoreLookupType ("System.Boolean");
1046 enum_type = CoreLookupType ("System.Enum");
1048 multicast_delegate_type = CoreLookupType ("System.MulticastDelegate");
1049 delegate_type = CoreLookupType ("System.Delegate");
1051 array_type = CoreLookupType ("System.Array");
1052 void_type = CoreLookupType ("System.Void");
1053 type_type = CoreLookupType ("System.Type");
1055 runtime_field_handle_type = CoreLookupType ("System.RuntimeFieldHandle");
1056 runtime_argument_handle_type = CoreLookupType ("System.RuntimeArgumentHandle");
1057 runtime_helpers_type = CoreLookupType ("System.Runtime.CompilerServices.RuntimeHelpers");
1058 default_member_type = CoreLookupType ("System.Reflection.DefaultMemberAttribute");
1059 runtime_handle_type = CoreLookupType ("System.RuntimeTypeHandle");
1060 asynccallback_type = CoreLookupType ("System.AsyncCallback");
1061 iasyncresult_type = CoreLookupType ("System.IAsyncResult");
1062 ienumerator_type = CoreLookupType ("System.Collections.IEnumerator");
1063 ienumerable_type = CoreLookupType ("System.Collections.IEnumerable");
1064 idisposable_type = CoreLookupType ("System.IDisposable");
1065 icloneable_type = CoreLookupType ("System.ICloneable");
1066 iconvertible_type = CoreLookupType ("System.IConvertible");
1067 monitor_type = CoreLookupType ("System.Threading.Monitor");
1068 intptr_type = CoreLookupType ("System.IntPtr");
1070 attribute_type = CoreLookupType ("System.Attribute");
1071 attribute_usage_type = CoreLookupType ("System.AttributeUsageAttribute");
1072 dllimport_type = CoreLookupType ("System.Runtime.InteropServices.DllImportAttribute");
1073 methodimpl_attr_type = CoreLookupType ("System.Runtime.CompilerServices.MethodImplAttribute");
1074 marshal_as_attr_type = CoreLookupType ("System.Runtime.InteropServices.MarshalAsAttribute");
1075 param_array_type = CoreLookupType ("System.ParamArrayAttribute");
1076 in_attribute_type = CoreLookupType ("System.Runtime.InteropServices.InAttribute");
1077 typed_reference_type = CoreLookupType ("System.TypedReference");
1078 arg_iterator_type = CoreLookupType ("System.ArgIterator");
1079 mbr_type = CoreLookupType ("System.MarshalByRefObject");
1082 // Sigh. Remove this before the release. Wonder what versions of Mono
1083 // people are running.
1085 guid_attr_type = LookupType ("System.Runtime.InteropServices.GuidAttribute");
1087 unverifiable_code_type= CoreLookupType ("System.Security.UnverifiableCodeAttribute");
1089 void_ptr_type = CoreLookupType ("System.Void*");
1091 indexer_name_type = CoreLookupType ("System.Runtime.CompilerServices.IndexerNameAttribute");
1093 exception_type = CoreLookupType ("System.Exception");
1094 invalid_operation_exception_type = CoreLookupType ("System.InvalidOperationException");
1099 obsolete_attribute_type = CoreLookupType ("System.ObsoleteAttribute");
1100 conditional_attribute_type = CoreLookupType ("System.Diagnostics.ConditionalAttribute");
1101 cls_compliant_attribute_type = CoreLookupType ("System.CLSCompliantAttribute");
1102 struct_layout_attribute_type = CoreLookupType ("System.Runtime.InteropServices.StructLayoutAttribute");
1103 field_offset_attribute_type = CoreLookupType ("System.Runtime.InteropServices.FieldOffsetAttribute");
1106 // When compiling corlib, store the "real" types here.
1108 if (!RootContext.StdLib) {
1109 system_int32_type = typeof (System.Int32);
1110 system_array_type = typeof (System.Array);
1111 system_type_type = typeof (System.Type);
1112 system_assemblybuilder_type = typeof (System.Reflection.Emit.AssemblyBuilder);
1114 Type [] void_arg = { };
1115 system_int_array_get_length = GetMethod (
1116 system_array_type, "get_Length", void_arg);
1117 system_int_array_get_rank = GetMethod (
1118 system_array_type, "get_Rank", void_arg);
1119 system_object_array_clone = GetMethod (
1120 system_array_type, "Clone", void_arg);
1122 Type [] system_int_arg = { system_int32_type };
1123 system_int_array_get_length_int = GetMethod (
1124 system_array_type, "GetLength", system_int_arg);
1125 system_int_array_get_upper_bound_int = GetMethod (
1126 system_array_type, "GetUpperBound", system_int_arg);
1127 system_int_array_get_lower_bound_int = GetMethod (
1128 system_array_type, "GetLowerBound", system_int_arg);
1130 Type [] system_array_int_arg = { system_array_type, system_int32_type };
1131 system_void_array_copyto_array_int = GetMethod (
1132 system_array_type, "CopyTo", system_array_int_arg);
1134 Type [] system_3_type_arg = {
1135 system_type_type, system_type_type, system_type_type };
1136 Type [] system_4_type_arg = {
1137 system_type_type, system_type_type, system_type_type, system_type_type };
1139 MethodInfo set_corlib_type_builders = GetMethod (
1140 system_assemblybuilder_type, "SetCorlibTypeBuilders",
1141 system_4_type_arg, true, false);
1143 if (set_corlib_type_builders != null) {
1144 object[] args = new object [4];
1145 args [0] = object_type;
1146 args [1] = value_type;
1147 args [2] = enum_type;
1148 args [3] = void_type;
1150 set_corlib_type_builders.Invoke (CodeGen.Assembly.Builder, args);
1152 // Compatibility for an older version of the class libs.
1153 set_corlib_type_builders = GetMethod (
1154 system_assemblybuilder_type, "SetCorlibTypeBuilders",
1155 system_3_type_arg, true, true);
1157 if (set_corlib_type_builders == null) {
1158 Report.Error (-26, "Corlib compilation is not supported in Microsoft.NET due to bugs in it");
1162 object[] args = new object [3];
1163 args [0] = object_type;
1164 args [1] = value_type;
1165 args [2] = enum_type;
1167 set_corlib_type_builders.Invoke (CodeGen.Assembly.Builder, args);
1171 system_object_expr.Type = object_type;
1172 system_string_expr.Type = string_type;
1173 system_boolean_expr.Type = bool_type;
1174 system_decimal_expr.Type = decimal_type;
1175 system_single_expr.Type = float_type;
1176 system_double_expr.Type = double_type;
1177 system_sbyte_expr.Type = sbyte_type;
1178 system_byte_expr.Type = byte_type;
1179 system_int16_expr.Type = short_type;
1180 system_uint16_expr.Type = ushort_type;
1181 system_int32_expr.Type = int32_type;
1182 system_uint32_expr.Type = uint32_type;
1183 system_int64_expr.Type = int64_type;
1184 system_uint64_expr.Type = uint64_type;
1185 system_char_expr.Type = char_type;
1186 system_void_expr.Type = void_type;
1187 system_asynccallback_expr.Type = asynccallback_type;
1188 system_iasyncresult_expr.Type = iasyncresult_type;
1189 system_valuetype_expr.Type = value_type;
1193 // The helper methods that are used by the compiler
1195 public static void InitCodeHelpers ()
1198 // Now load the default methods that we use.
1200 Type [] string_string = { string_type, string_type };
1201 string_concat_string_string = GetMethod (
1202 string_type, "Concat", string_string);
1203 Type [] string_string_string = { string_type, string_type, string_type };
1204 string_concat_string_string_string = GetMethod (
1205 string_type, "Concat", string_string_string);
1206 Type [] string_string_string_string = { string_type, string_type, string_type, string_type };
1207 string_concat_string_string_string_string = GetMethod (
1208 string_type, "Concat", string_string_string_string);
1209 Type[] params_string = { TypeManager.LookupType ("System.String[]") };
1210 string_concat_string_dot_dot_dot = GetMethod (
1211 string_type, "Concat", params_string);
1213 Type [] object_object = { object_type, object_type };
1214 string_concat_object_object = GetMethod (
1215 string_type, "Concat", object_object);
1216 Type [] object_object_object = { object_type, object_type, object_type };
1217 string_concat_object_object_object = GetMethod (
1218 string_type, "Concat", object_object_object);
1219 Type[] params_object = { TypeManager.LookupType ("System.Object[]") };
1220 string_concat_object_dot_dot_dot = GetMethod (
1221 string_type, "Concat", params_object);
1223 Type [] string_ = { string_type };
1224 string_isinterneted_string = GetMethod (
1225 string_type, "IsInterned", string_);
1227 Type [] runtime_type_handle = { runtime_handle_type };
1228 system_type_get_type_from_handle = GetMethod (
1229 type_type, "GetTypeFromHandle", runtime_type_handle);
1231 Type [] delegate_delegate = { delegate_type, delegate_type };
1232 delegate_combine_delegate_delegate = GetMethod (
1233 delegate_type, "Combine", delegate_delegate);
1235 delegate_remove_delegate_delegate = GetMethod (
1236 delegate_type, "Remove", delegate_delegate);
1241 Type [] void_arg = { };
1242 object_getcurrent_void = GetMethod (
1243 ienumerator_type, "get_Current", void_arg);
1244 bool_movenext_void = GetMethod (
1245 ienumerator_type, "MoveNext", void_arg);
1246 void_reset_void = GetMethod (
1247 ienumerator_type, "Reset", void_arg);
1248 void_dispose_void = GetMethod (
1249 idisposable_type, "Dispose", void_arg);
1250 int_get_offset_to_string_data = GetMethod (
1251 runtime_helpers_type, "get_OffsetToStringData", void_arg);
1252 int_array_get_length = GetMethod (
1253 array_type, "get_Length", void_arg);
1254 int_array_get_rank = GetMethod (
1255 array_type, "get_Rank", void_arg);
1256 ienumerable_getenumerator_void = GetMethod (
1257 ienumerable_type, "GetEnumerator", void_arg);
1262 Type [] int_arg = { int32_type };
1263 int_array_get_length_int = GetMethod (
1264 array_type, "GetLength", int_arg);
1265 int_array_get_upper_bound_int = GetMethod (
1266 array_type, "GetUpperBound", int_arg);
1267 int_array_get_lower_bound_int = GetMethod (
1268 array_type, "GetLowerBound", int_arg);
1271 // System.Array methods
1273 object_array_clone = GetMethod (
1274 array_type, "Clone", void_arg);
1275 Type [] array_int_arg = { array_type, int32_type };
1276 void_array_copyto_array_int = GetMethod (
1277 array_type, "CopyTo", array_int_arg);
1282 Type [] object_arg = { object_type };
1283 void_monitor_enter_object = GetMethod (
1284 monitor_type, "Enter", object_arg);
1285 void_monitor_exit_object = GetMethod (
1286 monitor_type, "Exit", object_arg);
1288 Type [] array_field_handle_arg = { array_type, runtime_field_handle_type };
1290 void_initializearray_array_fieldhandle = GetMethod (
1291 runtime_helpers_type, "InitializeArray", array_field_handle_arg);
1296 int_getlength_int = GetMethod (
1297 array_type, "GetLength", int_arg);
1300 // Decimal constructors
1302 Type [] dec_arg = { int32_type, int32_type, int32_type, bool_type, byte_type };
1303 void_decimal_ctor_five_args = GetConstructor (
1304 decimal_type, dec_arg);
1309 cons_param_array_attribute = GetConstructor (
1310 param_array_type, void_arg);
1312 unverifiable_code_ctor = GetConstructor (
1313 unverifiable_code_type, void_arg);
1316 // InvalidOperationException
1318 invalid_operation_ctor = GetConstructor (
1319 invalid_operation_exception_type, void_arg);
1323 object_ctor = GetConstructor (object_type, void_arg);
1327 const BindingFlags instance_and_static = BindingFlags.Static | BindingFlags.Instance;
1330 /// This is the "old", non-cache based FindMembers() function. We cannot use
1331 /// the cache here because there is no member name argument.
1333 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1334 MemberFilter filter, object criteria)
1336 DeclSpace decl = (DeclSpace) builder_to_declspace [t];
1339 // `builder_to_declspace' contains all dynamic types.
1343 Timer.StartTimer (TimerType.FindMembers);
1344 list = decl.FindMembers (mt, bf, filter, criteria);
1345 Timer.StopTimer (TimerType.FindMembers);
1350 // We have to take care of arrays specially, because GetType on
1351 // a TypeBuilder array will return a Type, not a TypeBuilder,
1352 // and we can not call FindMembers on this type.
1354 if (t.IsSubclassOf (TypeManager.array_type))
1355 return new MemberList (TypeManager.array_type.FindMembers (mt, bf, filter, criteria));
1358 // Since FindMembers will not lookup both static and instance
1359 // members, we emulate this behaviour here.
1361 if ((bf & instance_and_static) == instance_and_static){
1362 MemberInfo [] i_members = t.FindMembers (
1363 mt, bf & ~BindingFlags.Static, filter, criteria);
1365 int i_len = i_members.Length;
1367 MemberInfo one = i_members [0];
1370 // If any of these are present, we are done!
1372 if ((one is Type) || (one is EventInfo) || (one is FieldInfo))
1373 return new MemberList (i_members);
1376 MemberInfo [] s_members = t.FindMembers (
1377 mt, bf & ~BindingFlags.Instance, filter, criteria);
1379 int s_len = s_members.Length;
1380 if (i_len > 0 || s_len > 0)
1381 return new MemberList (i_members, s_members);
1384 return new MemberList (i_members);
1386 return new MemberList (s_members);
1390 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
1395 /// This method is only called from within MemberLookup. It tries to use the member
1396 /// cache if possible and falls back to the normal FindMembers if not. The `used_cache'
1397 /// flag tells the caller whether we used the cache or not. If we used the cache, then
1398 /// our return value will already contain all inherited members and the caller don't need
1399 /// to check base classes and interfaces anymore.
1401 private static MemberInfo [] MemberLookup_FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1402 string name, out bool used_cache)
1405 // We have to take care of arrays specially, because GetType on
1406 // a TypeBuilder array will return a Type, not a TypeBuilder,
1407 // and we can not call FindMembers on this type.
1409 if (t == TypeManager.array_type || t.IsSubclassOf (TypeManager.array_type)) {
1411 return TypeHandle.ArrayType.MemberCache.FindMembers (
1412 mt, bf, name, FilterWithClosure_delegate, null);
1416 // If this is a dynamic type, it's always in the `builder_to_declspace' hash table
1417 // and we can ask the DeclSpace for the MemberCache.
1419 if (t is TypeBuilder) {
1420 DeclSpace decl = (DeclSpace) builder_to_declspace [t];
1421 MemberCache cache = decl.MemberCache;
1424 // If this DeclSpace has a MemberCache, use it.
1427 if (cache != null) {
1429 return cache.FindMembers (
1430 mt, bf, name, FilterWithClosure_delegate, null);
1433 // If there is no MemberCache, we need to use the "normal" FindMembers.
1434 // Note, this is a VERY uncommon route!
1437 Timer.StartTimer (TimerType.FindMembers);
1438 list = decl.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
1439 FilterWithClosure_delegate, name);
1440 Timer.StopTimer (TimerType.FindMembers);
1443 return (MemberInfo []) list;
1447 // This call will always succeed. There is exactly one TypeHandle instance per
1448 // type, TypeHandle.GetTypeHandle() will either return it or create a new one
1449 // if it didn't already exist.
1451 TypeHandle handle = TypeHandle.GetTypeHandle (t);
1454 return handle.MemberCache.FindMembers (mt, bf, name, FilterWithClosure_delegate, null);
1457 public static bool IsBuiltinType (Type t)
1459 if (t == object_type || t == string_type || t == int32_type || t == uint32_type ||
1460 t == int64_type || t == uint64_type || t == float_type || t == double_type ||
1461 t == char_type || t == short_type || t == decimal_type || t == bool_type ||
1462 t == sbyte_type || t == byte_type || t == ushort_type || t == void_type)
1468 public static bool IsBuiltinType (TypeContainer tc)
1470 return IsBuiltinType (tc.TypeBuilder);
1474 // This is like IsBuiltinType, but lacks decimal_type, we should also clean up
1475 // the pieces in the code where we use IsBuiltinType and special case decimal_type.
1477 public static bool IsCLRType (Type t)
1479 if (t == object_type || t == int32_type || t == uint32_type ||
1480 t == int64_type || t == uint64_type || t == float_type || t == double_type ||
1481 t == char_type || t == short_type || t == bool_type ||
1482 t == sbyte_type || t == byte_type || t == ushort_type)
1488 public static bool IsDelegateType (Type t)
1490 if (t.IsSubclassOf (TypeManager.delegate_type))
1496 public static bool IsEnumType (Type t)
1498 if (t.IsSubclassOf (TypeManager.enum_type))
1503 public static bool IsBuiltinOrEnum (Type t)
1505 if (IsBuiltinType (t))
1515 // Whether a type is unmanaged. This is used by the unsafe code (25.2)
1517 public static bool IsUnmanagedType (Type t)
1519 if (IsBuiltinType (t) && t != TypeManager.string_type)
1528 if (IsValueType (t)){
1529 if (t is TypeBuilder){
1530 TypeContainer tc = LookupTypeContainer (t);
1532 if (tc.Fields != null){
1533 foreach (Field f in tc.Fields){
1534 if (f.FieldBuilder.IsStatic)
1536 if (!IsUnmanagedType (f.FieldBuilder.FieldType))
1542 FieldInfo [] fields = t.GetFields ();
1544 foreach (FieldInfo f in fields){
1547 if (!IsUnmanagedType (f.FieldType))
1557 public static bool IsValueType (Type t)
1559 if (t.IsSubclassOf (TypeManager.value_type) && (t != TypeManager.enum_type))
1565 public static bool IsInterfaceType (Type t)
1567 Interface iface = builder_to_declspace [t] as Interface;
1576 // Checks whether `type' is a subclass or nested child of `parent'.
1578 public static bool IsSubclassOrNestedChildOf (Type type, Type parent)
1581 if ((type == parent) || type.IsSubclassOf (parent))
1584 // Handle nested types.
1585 type = type.DeclaringType;
1586 } while (type != null);
1592 // Checks whether `type' is a nested child of `parent'.
1594 public static bool IsNestedChildOf (Type type, Type parent)
1599 type = type.DeclaringType;
1600 while (type != null) {
1604 type = type.DeclaringType;
1611 // Do the right thing when returning the element type of an
1612 // array type based on whether we are compiling corlib or not
1614 public static Type GetElementType (Type t)
1616 if (RootContext.StdLib)
1617 return t.GetElementType ();
1619 return TypeToCoreType (t.GetElementType ());
1623 /// Returns the User Defined Types
1625 public static ArrayList UserTypes {
1631 public static Hashtable TypeContainers {
1633 return typecontainers;
1637 static Hashtable builder_to_constant;
1639 public static void RegisterConstant (FieldBuilder fb, Const c)
1641 if (builder_to_constant == null)
1642 builder_to_constant = new PtrHashtable ();
1644 if (builder_to_constant.Contains (fb))
1647 builder_to_constant.Add (fb, c);
1650 public static Const LookupConstant (FieldBuilder fb)
1652 if (builder_to_constant == null)
1655 return (Const) builder_to_constant [fb];
1659 /// Gigantic work around for missing features in System.Reflection.Emit follows.
1663 /// Since System.Reflection.Emit can not return MethodBase.GetParameters
1664 /// for anything which is dynamic, and we need this in a number of places,
1665 /// we register this information here, and use it afterwards.
1667 static public bool RegisterMethod (MethodBase mb, InternalParameters ip, Type [] args)
1672 method_arguments.Add (mb, args);
1673 method_internal_params.Add (mb, ip);
1678 static public InternalParameters LookupParametersByBuilder (MethodBase mb)
1680 if (! (mb is ConstructorBuilder || mb is MethodBuilder))
1683 if (method_internal_params.Contains (mb))
1684 return (InternalParameters) method_internal_params [mb];
1686 throw new Exception ("Argument for Method not registered" + mb);
1690 /// Returns the argument types for a method based on its methodbase
1692 /// For dynamic methods, we use the compiler provided types, for
1693 /// methods from existing assemblies we load them from GetParameters,
1694 /// and insert them into the cache
1696 static public Type [] GetArgumentTypes (MethodBase mb)
1698 object t = method_arguments [mb];
1702 ParameterInfo [] pi = mb.GetParameters ();
1709 types = new Type [c];
1710 for (int i = 0; i < c; i++)
1711 types [i] = pi [i].ParameterType;
1713 method_arguments.Add (mb, types);
1718 /// Returns the argument types for an indexer based on its PropertyInfo
1720 /// For dynamic indexers, we use the compiler provided types, for
1721 /// indexers from existing assemblies we load them from GetParameters,
1722 /// and insert them into the cache
1724 static public Type [] GetArgumentTypes (PropertyInfo indexer)
1726 if (indexer_arguments.Contains (indexer))
1727 return (Type []) indexer_arguments [indexer];
1728 else if (indexer is PropertyBuilder)
1729 // If we're a PropertyBuilder and not in the
1730 // `indexer_arguments' hash, then we're a property and
1734 ParameterInfo [] pi = indexer.GetIndexParameters ();
1735 // Property, not an indexer.
1739 Type [] types = new Type [c];
1741 for (int i = 0; i < c; i++)
1742 types [i] = pi [i].ParameterType;
1744 indexer_arguments.Add (indexer, types);
1750 // This is a workaround the fact that GetValue is not
1751 // supported for dynamic types
1753 static Hashtable fields = new Hashtable ();
1754 static public bool RegisterFieldValue (FieldBuilder fb, object value)
1756 if (fields.Contains (fb))
1759 fields.Add (fb, value);
1764 static public object GetValue (FieldBuilder fb)
1769 static Hashtable fieldbuilders_to_fields = new Hashtable ();
1770 static public bool RegisterFieldBase (FieldBuilder fb, FieldBase f)
1772 if (fieldbuilders_to_fields.Contains (fb))
1775 fieldbuilders_to_fields.Add (fb, f);
1780 // The return value can be null; This will be the case for
1781 // auxiliary FieldBuilders created by the compiler that have no
1782 // real field being declared on the source code
1784 static public FieldBase GetField (FieldInfo fb)
1786 return (FieldBase) fieldbuilders_to_fields [fb];
1789 static Hashtable events;
1791 static public bool RegisterEvent (MyEventBuilder eb, MethodBase add, MethodBase remove)
1794 events = new Hashtable ();
1796 if (events.Contains (eb))
1799 events.Add (eb, new Pair (add, remove));
1804 static public MethodInfo GetAddMethod (EventInfo ei)
1806 if (ei is MyEventBuilder) {
1807 Pair pair = (Pair) events [ei];
1809 return (MethodInfo) pair.First;
1811 return ei.GetAddMethod ();
1814 static public MethodInfo GetRemoveMethod (EventInfo ei)
1816 if (ei is MyEventBuilder) {
1817 Pair pair = (Pair) events [ei];
1819 return (MethodInfo) pair.Second;
1821 return ei.GetRemoveMethod ();
1824 static Hashtable priv_fields_events;
1826 static public bool RegisterPrivateFieldOfEvent (EventInfo einfo, FieldBuilder builder)
1828 if (priv_fields_events == null)
1829 priv_fields_events = new Hashtable ();
1831 if (priv_fields_events.Contains (einfo))
1834 priv_fields_events.Add (einfo, builder);
1839 static public MemberInfo GetPrivateFieldOfEvent (EventInfo ei)
1841 if (priv_fields_events == null)
1844 return (MemberInfo) priv_fields_events [ei];
1847 static Hashtable properties;
1849 static public bool RegisterProperty (PropertyBuilder pb, MethodBase get, MethodBase set)
1851 if (properties == null)
1852 properties = new Hashtable ();
1854 if (properties.Contains (pb))
1857 properties.Add (pb, new Pair (get, set));
1862 static public bool RegisterIndexer (PropertyBuilder pb, MethodBase get,
1863 MethodBase set, Type[] args)
1865 if (!RegisterProperty (pb, get,set))
1868 indexer_arguments.Add (pb, args);
1873 public static bool CheckStructCycles (TypeContainer tc, Hashtable seen)
1875 Hashtable hash = new Hashtable ();
1876 return CheckStructCycles (tc, seen, hash);
1879 public static bool CheckStructCycles (TypeContainer tc, Hashtable seen,
1882 if (!(tc is Struct) || IsBuiltinType (tc))
1886 // `seen' contains all types we've already visited.
1888 if (seen.Contains (tc))
1890 seen.Add (tc, null);
1892 if (tc.Fields == null)
1895 foreach (Field field in tc.Fields) {
1896 if (field.FieldBuilder.IsStatic)
1899 Type ftype = field.FieldBuilder.FieldType;
1900 TypeContainer ftc = LookupTypeContainer (ftype);
1904 if (hash.Contains (ftc)) {
1905 Report.Error (523, tc.Location,
1906 "Struct member `{0}.{1}' of type `{2}' " +
1907 "causes a cycle in the struct layout",
1908 tc.Name, field.Name, ftc.Name);
1913 // `hash' contains all types in the current path.
1915 hash.Add (tc, null);
1917 bool ok = CheckStructCycles (ftc, seen, hash);
1924 if (!seen.Contains (ftc))
1925 seen.Add (ftc, null);
1932 /// Given an array of interface types, expand and eliminate repeated ocurrences
1933 /// of an interface.
1937 /// This expands in context like: IA; IB : IA; IC : IA, IB; the interface "IC" to
1940 public static TypeExpr[] ExpandInterfaces (TypeExpr [] base_interfaces)
1942 ArrayList new_ifaces = new ArrayList ();
1944 foreach (TypeExpr iface in base_interfaces){
1945 if (!new_ifaces.Contains (iface))
1946 new_ifaces.Add (iface);
1948 TypeExpr [] implementing = iface.GetInterfaces ();
1950 foreach (TypeExpr imp in implementing){
1951 if (!new_ifaces.Contains (imp))
1952 new_ifaces.Add (imp);
1955 TypeExpr [] ret = new TypeExpr [new_ifaces.Count];
1956 new_ifaces.CopyTo (ret, 0);
1960 static PtrHashtable iface_cache = new PtrHashtable ();
1963 /// This function returns the interfaces in the type `t'. Works with
1964 /// both types and TypeBuilders.
1966 public static TypeExpr [] GetInterfaces (Type t)
1969 TypeExpr [] cached = iface_cache [t] as TypeExpr [];
1974 // The reason for catching the Array case is that Reflection.Emit
1975 // will not return a TypeBuilder for Array types of TypeBuilder types,
1976 // but will still throw an exception if we try to call GetInterfaces
1979 // Since the array interfaces are always constant, we return those for
1984 t = TypeManager.array_type;
1986 if (t is TypeBuilder){
1987 TypeExpr [] parent_ifaces;
1989 if (t.BaseType == null)
1990 parent_ifaces = NoTypeExprs;
1992 parent_ifaces = GetInterfaces (t.BaseType);
1993 TypeExpr [] type_ifaces = (TypeExpr []) builder_to_ifaces [t];
1994 if (type_ifaces == null)
1995 type_ifaces = NoTypeExprs;
1997 int parent_count = parent_ifaces.Length;
1998 TypeExpr [] result = new TypeExpr [parent_count + type_ifaces.Length];
1999 parent_ifaces.CopyTo (result, 0);
2000 type_ifaces.CopyTo (result, parent_count);
2002 iface_cache [t] = result;
2005 Type [] ifaces = t.GetInterfaces ();
2006 if (ifaces.Length == 0)
2009 TypeExpr [] result = new TypeExpr [ifaces.Length];
2010 for (int i = 0; i < ifaces.Length; i++)
2011 result [i] = new TypeExpression (ifaces [i], Location.Null);
2013 iface_cache [t] = result;
2019 // gets the interfaces that are declared explicitly on t
2021 public static TypeExpr [] GetExplicitInterfaces (TypeBuilder t)
2023 return (TypeExpr []) builder_to_ifaces [t];
2027 /// The following is used to check if a given type implements an interface.
2028 /// The cache helps us reduce the expense of hitting Type.GetInterfaces everytime.
2030 public static bool ImplementsInterface (Type t, Type iface)
2032 TypeExpr [] interfaces;
2035 // FIXME OPTIMIZATION:
2036 // as soon as we hit a non-TypeBuiler in the interface
2037 // chain, we could return, as the `Type.GetInterfaces'
2038 // will return all the interfaces implement by the type
2042 interfaces = GetInterfaces (t);
2044 if (interfaces != null){
2045 foreach (TypeExpr i in interfaces){
2046 if (i.Type == iface)
2052 } while (t != null);
2057 static NumberFormatInfo nf_provider = CultureInfo.CurrentCulture.NumberFormat;
2059 // This is a custom version of Convert.ChangeType() which works
2060 // with the TypeBuilder defined types when compiling corlib.
2061 public static object ChangeType (object value, Type conversionType, out bool error)
2063 IConvertible convert_value = value as IConvertible;
2065 if (convert_value == null){
2071 // We must use Type.Equals() here since `conversionType' is
2072 // the TypeBuilder created version of a system type and not
2073 // the system type itself. You cannot use Type.GetTypeCode()
2074 // on such a type - it'd always return TypeCode.Object.
2078 if (conversionType.Equals (typeof (Boolean)))
2079 return (object)(convert_value.ToBoolean (nf_provider));
2080 else if (conversionType.Equals (typeof (Byte)))
2081 return (object)(convert_value.ToByte (nf_provider));
2082 else if (conversionType.Equals (typeof (Char)))
2083 return (object)(convert_value.ToChar (nf_provider));
2084 else if (conversionType.Equals (typeof (DateTime)))
2085 return (object)(convert_value.ToDateTime (nf_provider));
2086 else if (conversionType.Equals (typeof (Decimal)))
2087 return (object)(convert_value.ToDecimal (nf_provider));
2088 else if (conversionType.Equals (typeof (Double)))
2089 return (object)(convert_value.ToDouble (nf_provider));
2090 else if (conversionType.Equals (typeof (Int16)))
2091 return (object)(convert_value.ToInt16 (nf_provider));
2092 else if (conversionType.Equals (typeof (Int32)))
2093 return (object)(convert_value.ToInt32 (nf_provider));
2094 else if (conversionType.Equals (typeof (Int64)))
2095 return (object)(convert_value.ToInt64 (nf_provider));
2096 else if (conversionType.Equals (typeof (SByte)))
2097 return (object)(convert_value.ToSByte (nf_provider));
2098 else if (conversionType.Equals (typeof (Single)))
2099 return (object)(convert_value.ToSingle (nf_provider));
2100 else if (conversionType.Equals (typeof (String)))
2101 return (object)(convert_value.ToString (nf_provider));
2102 else if (conversionType.Equals (typeof (UInt16)))
2103 return (object)(convert_value.ToUInt16 (nf_provider));
2104 else if (conversionType.Equals (typeof (UInt32)))
2105 return (object)(convert_value.ToUInt32 (nf_provider));
2106 else if (conversionType.Equals (typeof (UInt64)))
2107 return (object)(convert_value.ToUInt64 (nf_provider));
2108 else if (conversionType.Equals (typeof (Object)))
2109 return (object)(value);
2119 // This is needed, because enumerations from assemblies
2120 // do not report their underlyingtype, but they report
2123 public static Type EnumToUnderlying (Type t)
2125 if (t == TypeManager.enum_type)
2128 t = t.UnderlyingSystemType;
2129 if (!TypeManager.IsEnumType (t))
2132 if (t is TypeBuilder) {
2133 // slow path needed to compile corlib
2134 if (t == TypeManager.bool_type ||
2135 t == TypeManager.byte_type ||
2136 t == TypeManager.sbyte_type ||
2137 t == TypeManager.char_type ||
2138 t == TypeManager.short_type ||
2139 t == TypeManager.ushort_type ||
2140 t == TypeManager.int32_type ||
2141 t == TypeManager.uint32_type ||
2142 t == TypeManager.int64_type ||
2143 t == TypeManager.uint64_type)
2145 throw new Exception ("Unhandled typecode in enum " + " from " + t.AssemblyQualifiedName);
2147 TypeCode tc = Type.GetTypeCode (t);
2150 case TypeCode.Boolean:
2151 return TypeManager.bool_type;
2153 return TypeManager.byte_type;
2154 case TypeCode.SByte:
2155 return TypeManager.sbyte_type;
2157 return TypeManager.char_type;
2158 case TypeCode.Int16:
2159 return TypeManager.short_type;
2160 case TypeCode.UInt16:
2161 return TypeManager.ushort_type;
2162 case TypeCode.Int32:
2163 return TypeManager.int32_type;
2164 case TypeCode.UInt32:
2165 return TypeManager.uint32_type;
2166 case TypeCode.Int64:
2167 return TypeManager.int64_type;
2168 case TypeCode.UInt64:
2169 return TypeManager.uint64_type;
2171 throw new Exception ("Unhandled typecode in enum " + tc + " from " + t.AssemblyQualifiedName);
2175 // When compiling corlib and called with one of the core types, return
2176 // the corresponding typebuilder for that type.
2178 public static Type TypeToCoreType (Type t)
2180 if (RootContext.StdLib || (t is TypeBuilder))
2183 TypeCode tc = Type.GetTypeCode (t);
2186 case TypeCode.Boolean:
2187 return TypeManager.bool_type;
2189 return TypeManager.byte_type;
2190 case TypeCode.SByte:
2191 return TypeManager.sbyte_type;
2193 return TypeManager.char_type;
2194 case TypeCode.Int16:
2195 return TypeManager.short_type;
2196 case TypeCode.UInt16:
2197 return TypeManager.ushort_type;
2198 case TypeCode.Int32:
2199 return TypeManager.int32_type;
2200 case TypeCode.UInt32:
2201 return TypeManager.uint32_type;
2202 case TypeCode.Int64:
2203 return TypeManager.int64_type;
2204 case TypeCode.UInt64:
2205 return TypeManager.uint64_type;
2206 case TypeCode.Single:
2207 return TypeManager.float_type;
2208 case TypeCode.Double:
2209 return TypeManager.double_type;
2210 case TypeCode.String:
2211 return TypeManager.string_type;
2213 if (t == typeof (void))
2214 return TypeManager.void_type;
2215 if (t == typeof (object))
2216 return TypeManager.object_type;
2217 if (t == typeof (System.Type))
2218 return TypeManager.type_type;
2219 if (t == typeof (System.IntPtr))
2220 return TypeManager.intptr_type;
2226 /// Utility function that can be used to probe whether a type
2227 /// is managed or not.
2229 public static bool VerifyUnManaged (Type t, Location loc)
2231 if (t.IsValueType || t.IsPointer){
2233 // FIXME: this is more complex, we actually need to
2234 // make sure that the type does not contain any
2240 if (!RootContext.StdLib && (t == TypeManager.decimal_type))
2241 // We need this explicit check here to make it work when
2242 // compiling corlib.
2247 "Cannot take the address or size of a variable of a managed type ('" +
2248 CSharpName (t) + "')");
2253 /// Returns the name of the indexer in a given type.
2256 /// The default is not always `Item'. The user can change this behaviour by
2257 /// using the DefaultMemberAttribute in the class.
2259 /// For example, the String class indexer is named `Chars' not `Item'
2261 public static string IndexerPropertyName (Type t)
2263 if (t is TypeBuilder) {
2264 if (t.IsInterface) {
2265 Interface i = LookupInterface (t);
2267 if ((i == null) || (i.IndexerName == null))
2270 return i.IndexerName;
2272 TypeContainer tc = LookupTypeContainer (t);
2274 if ((tc == null) || (tc.IndexerName == null))
2277 return tc.IndexerName;
2281 System.Attribute attr = System.Attribute.GetCustomAttribute (
2282 t, TypeManager.default_member_type);
2284 DefaultMemberAttribute dma = (DefaultMemberAttribute) attr;
2285 return dma.MemberName;
2291 static MethodInfo pinned_method = null;
2292 public static void MakePinned (LocalBuilder builder)
2294 if (pinned_method == null) {
2295 pinned_method = typeof (LocalBuilder).GetMethod ("MakePinned", BindingFlags.Instance | BindingFlags.NonPublic);
2296 if (pinned_method == null) {
2297 Report.Warning (-24, new Location (-1), "Microsoft.NET does not support making pinned variables." +
2298 "This code may cause errors on a runtime with a moving GC");
2304 pinned_method.Invoke (builder, null);
2309 // Returns whether the array of memberinfos contains the given method
2311 public static bool ArrayContainsMethod (MemberInfo [] array, MethodBase new_method)
2313 Type [] new_args = TypeManager.GetArgumentTypes (new_method);
2315 foreach (MethodBase method in array) {
2316 if (method.Name != new_method.Name)
2319 if (method is MethodInfo && new_method is MethodInfo)
2320 if (((MethodInfo) method).ReturnType != ((MethodInfo) new_method).ReturnType)
2324 Type [] old_args = TypeManager.GetArgumentTypes (method);
2325 int old_count = old_args.Length;
2328 if (new_args.Length != old_count)
2331 for (i = 0; i < old_count; i++){
2332 if (old_args [i] != new_args [i])
2345 // We copy methods from `new_members' into `target_list' if the signature
2346 // for the method from in the new list does not exist in the target_list
2348 // The name is assumed to be the same.
2350 public static ArrayList CopyNewMethods (ArrayList target_list, IList new_members)
2352 if (target_list == null){
2353 target_list = new ArrayList ();
2355 foreach (MemberInfo mi in new_members){
2356 if (mi is MethodBase)
2357 target_list.Add (mi);
2362 MemberInfo [] target_array = new MemberInfo [target_list.Count];
2363 target_list.CopyTo (target_array, 0);
2365 foreach (MemberInfo mi in new_members){
2366 MethodBase new_method = (MethodBase) mi;
2368 if (!ArrayContainsMethod (target_array, new_method))
2369 target_list.Add (new_method);
2375 #region MemberLookup implementation
2378 // Whether we allow private members in the result (since FindMembers
2379 // uses NonPublic for both protected and private), we need to distinguish.
2382 static internal bool FilterNone (MemberInfo m, object filter_criteria)
2387 internal class Closure {
2388 internal bool private_ok;
2390 // Who is invoking us and which type is being queried currently.
2391 internal Type invocation_type;
2392 internal Type qualifier_type;
2394 // The assembly that defines the type is that is calling us
2395 internal Assembly invocation_assembly;
2396 internal IList almost_match;
2398 private bool CheckValidFamilyAccess (bool is_static, MemberInfo m)
2400 if (invocation_type == null)
2403 Debug.Assert (IsSubclassOrNestedChildOf (invocation_type, m.DeclaringType));
2408 // A nested class has access to all the protected members visible to its parent.
2409 if (qualifier_type != null
2410 && TypeManager.IsNestedChildOf (invocation_type, qualifier_type))
2413 if (invocation_type == m.DeclaringType
2414 || invocation_type.IsSubclassOf (m.DeclaringType)) {
2415 // Although a derived class can access protected members of its base class
2416 // it cannot do so through an instance of the base class (CS1540).
2417 // => Ancestry should be: declaring_type ->* invocation_type ->* qualified_type
2418 if (qualifier_type == null
2419 || qualifier_type == invocation_type
2420 || qualifier_type.IsSubclassOf (invocation_type))
2424 if (almost_match != null)
2425 almost_match.Add (m);
2430 // This filter filters by name + whether it is ok to include private
2431 // members in the search
2433 internal bool Filter (MemberInfo m, object filter_criteria)
2436 // Hack: we know that the filter criteria will always be in the `closure'
2440 if ((filter_criteria != null) && (m.Name != (string) filter_criteria))
2443 if (((qualifier_type == null) || (qualifier_type == invocation_type)) &&
2444 (m.DeclaringType == invocation_type))
2448 // Ugly: we need to find out the type of `m', and depending
2449 // on this, tell whether we accept or not
2451 if (m is MethodBase){
2452 MethodBase mb = (MethodBase) m;
2453 MethodAttributes ma = mb.Attributes & MethodAttributes.MemberAccessMask;
2455 if (ma == MethodAttributes.Private)
2456 return private_ok || (invocation_type == m.DeclaringType) ||
2457 IsNestedChildOf (invocation_type, m.DeclaringType);
2459 // Assembly succeeds if we're in the same assembly.
2460 if (ma == MethodAttributes.Assembly)
2461 return (invocation_assembly == mb.DeclaringType.Assembly);
2463 // FamAndAssem requires that we not only derive, but we are on the same assembly.
2464 if (ma == MethodAttributes.FamANDAssem){
2465 if (invocation_assembly != mb.DeclaringType.Assembly)
2469 // Family and FamANDAssem require that we derive.
2470 if ((ma == MethodAttributes.Family) || (ma == MethodAttributes.FamANDAssem))
2471 return CheckValidFamilyAccess (mb.IsStatic, m);
2477 if (m is FieldInfo){
2478 FieldInfo fi = (FieldInfo) m;
2479 FieldAttributes fa = fi.Attributes & FieldAttributes.FieldAccessMask;
2481 if (fa == FieldAttributes.Private)
2482 return private_ok || (invocation_type == m.DeclaringType) ||
2483 IsNestedChildOf (invocation_type, m.DeclaringType);
2485 // Assembly succeeds if we're in the same assembly.
2486 if (fa == FieldAttributes.Assembly)
2487 return (invocation_assembly == fi.DeclaringType.Assembly);
2489 // FamAndAssem requires that we not only derive, but we are on the same assembly.
2490 if (fa == FieldAttributes.FamANDAssem){
2491 if (invocation_assembly != fi.DeclaringType.Assembly)
2495 // Family and FamANDAssem require that we derive.
2496 if ((fa == FieldAttributes.Family) || (fa == FieldAttributes.FamANDAssem))
2497 return CheckValidFamilyAccess (fi.IsStatic, m);
2504 // EventInfos and PropertyInfos, return true because they lack permission
2505 // information, so we need to check later on the methods.
2511 static Closure closure = new Closure ();
2512 static MemberFilter FilterWithClosure_delegate = new MemberFilter (closure.Filter);
2513 static MemberFilter FilterNone_delegate = new MemberFilter (FilterNone);
2516 // Looks up a member called `name' in the `queried_type'. This lookup
2517 // is done by code that is contained in the definition for `invocation_type'
2518 // through a qualifier of type `qualifier_type' (or null if there is no qualifier).
2520 // `invocation_type' is used to check whether we're allowed to access the requested
2521 // member wrt its protection level.
2523 // When called from MemberAccess, `qualifier_type' is the type which is used to access
2524 // the requested member (`class B { A a = new A (); a.foo = 5; }'; here invocation_type
2525 // is B and qualifier_type is A). This is used to do the CS1540 check.
2527 // When resolving a SimpleName, `qualifier_type' is null.
2529 // The `qualifier_type' is used for the CS1540 check; it's normally either null or
2530 // the same than `queried_type' - except when we're being called from BaseAccess;
2531 // in this case, `invocation_type' is the current type and `queried_type' the base
2532 // type, so this'd normally trigger a CS1540.
2534 // The binding flags are `bf' and the kind of members being looked up are `mt'
2536 // The return value always includes private members which code in `invocation_type'
2537 // is allowed to access (using the specified `qualifier_type' if given); only use
2538 // BindingFlags.NonPublic to bypass the permission check.
2540 // The 'almost_match' argument is used for reporting error CS1540.
2542 // Returns an array of a single element for everything but Methods/Constructors
2543 // that might return multiple matches.
2545 public static MemberInfo [] MemberLookup (Type invocation_type, Type qualifier_type,
2546 Type queried_type, MemberTypes mt,
2547 BindingFlags original_bf, string name, IList almost_match)
2549 Timer.StartTimer (TimerType.MemberLookup);
2551 MemberInfo[] retval = RealMemberLookup (invocation_type, qualifier_type,
2552 queried_type, mt, original_bf, name, almost_match);
2554 Timer.StopTimer (TimerType.MemberLookup);
2559 static MemberInfo [] RealMemberLookup (Type invocation_type, Type qualifier_type,
2560 Type queried_type, MemberTypes mt,
2561 BindingFlags original_bf, string name, IList almost_match)
2563 BindingFlags bf = original_bf;
2565 ArrayList method_list = null;
2566 Type current_type = queried_type;
2567 bool searching = (original_bf & BindingFlags.DeclaredOnly) == 0;
2568 bool skip_iface_check = true, used_cache = false;
2569 bool always_ok_flag = false;
2571 closure.invocation_type = invocation_type;
2572 closure.invocation_assembly = invocation_type != null ? invocation_type.Assembly : null;
2573 closure.qualifier_type = qualifier_type;
2574 closure.almost_match = almost_match;
2577 // If we are a nested class, we always have access to our container
2580 if (invocation_type != null){
2581 string invocation_name = invocation_type.FullName;
2582 if (invocation_name.IndexOf ('+') != -1){
2583 string container = queried_type.FullName + "+";
2584 int container_length = container.Length;
2586 if (invocation_name.Length > container_length){
2587 string shared = invocation_name.Substring (0, container_length);
2589 if (shared == container)
2590 always_ok_flag = true;
2595 // This is from the first time we find a method
2596 // in most cases, we do not actually find a method in the base class
2597 // so we can just ignore it, and save the arraylist allocation
2598 MemberInfo [] first_members_list = null;
2599 bool use_first_members_list = false;
2605 // `NonPublic' is lame, because it includes both protected and
2606 // private methods, so we need to control this behavior by
2607 // explicitly tracking if a private method is ok or not.
2609 // The possible cases are:
2610 // public, private and protected (internal does not come into the
2613 if ((invocation_type != null) &&
2614 ((invocation_type == current_type) ||
2615 IsNestedChildOf (invocation_type, current_type)) ||
2617 bf = original_bf | BindingFlags.NonPublic;
2621 closure.private_ok = (original_bf & BindingFlags.NonPublic) != 0;
2623 Timer.StopTimer (TimerType.MemberLookup);
2625 list = MemberLookup_FindMembers (current_type, mt, bf, name, out used_cache);
2627 Timer.StartTimer (TimerType.MemberLookup);
2630 // When queried for an interface type, the cache will automatically check all
2631 // inherited members, so we don't need to do this here. However, this only
2632 // works if we already used the cache in the first iteration of this loop.
2634 // If we used the cache in any further iteration, we can still terminate the
2635 // loop since the cache always looks in all parent classes.
2641 skip_iface_check = false;
2643 if (current_type == TypeManager.object_type)
2646 current_type = current_type.BaseType;
2649 // This happens with interfaces, they have a null
2650 // basetype. Look members up in the Object class.
2652 if (current_type == null)
2653 current_type = TypeManager.object_type;
2656 if (list.Length == 0)
2660 // Events and types are returned by both `static' and `instance'
2661 // searches, which means that our above FindMembers will
2662 // return two copies of the same.
2664 if (list.Length == 1 && !(list [0] is MethodBase)){
2669 // Multiple properties: we query those just to find out the indexer
2672 if (list [0] is PropertyInfo)
2676 // We found an event: the cache lookup returns both the event and
2677 // its private field.
2679 if (list [0] is EventInfo) {
2680 if ((list.Length == 2) && (list [1] is FieldInfo))
2681 return new MemberInfo [] { list [0] };
2688 // We found methods, turn the search into "method scan"
2692 if (first_members_list != null) {
2693 if (use_first_members_list) {
2694 method_list = CopyNewMethods (method_list, first_members_list);
2695 use_first_members_list = false;
2698 method_list = CopyNewMethods (method_list, list);
2700 first_members_list = list;
2701 use_first_members_list = true;
2702 mt &= (MemberTypes.Method | MemberTypes.Constructor);
2704 } while (searching);
2706 if (use_first_members_list) {
2707 foreach (MemberInfo mi in first_members_list) {
2708 if (! (mi is MethodBase)) {
2709 method_list = CopyNewMethods (method_list, first_members_list);
2710 return (MemberInfo []) method_list.ToArray (typeof (MemberInfo));
2713 return (MemberInfo []) first_members_list;
2716 if (method_list != null && method_list.Count > 0)
2717 return (MemberInfo []) method_list.ToArray (typeof (MemberInfo));
2720 // This happens if we already used the cache in the first iteration, in this case
2721 // the cache already looked in all interfaces.
2723 if (skip_iface_check)
2727 // Interfaces do not list members they inherit, so we have to
2730 if (!queried_type.IsInterface)
2733 if (queried_type.IsArray)
2734 queried_type = TypeManager.array_type;
2736 TypeExpr [] ifaces = GetInterfaces (queried_type);
2740 foreach (TypeExpr itype in ifaces){
2743 x = MemberLookup (null, null, itype.Type, mt, bf, name, null);
2752 // This is used to extract properties and event declarations from a type
2754 static MemberInfo [] SpecialContainerLookup (Type t, bool is_static)
2756 BindingFlags bf = BindingFlags.DeclaredOnly | (is_static ? BindingFlags.Static : BindingFlags.Instance);
2758 bf |= BindingFlags.Public | BindingFlags.NonPublic;
2760 if (t is TypeBuilder) {
2761 DeclSpace decl = (DeclSpace) builder_to_declspace [t];
2763 return (MemberInfo []) decl.FindMembers (
2764 MemberTypes.Property | MemberTypes.Event,
2765 bf, FilterNone_delegate, null);
2767 return t.FindMembers (MemberTypes.Property | MemberTypes.Event,
2768 bf, FilterNone_delegate, null);
2773 public static bool IsSpecialMethod (MethodBase mb)
2775 Type t = mb.DeclaringType;
2777 MemberInfo [] matches = TypeManager.SpecialContainerLookup (t, mb.IsStatic);
2778 if (matches == null)
2781 foreach (MemberInfo mi in matches){
2782 if (mi is PropertyBuilder){
2783 Pair p = (Pair) properties [mi];
2785 if (p.First == mb || p.Second == mb)
2787 } else if (mi is PropertyInfo){
2788 MethodInfo [] methods = ((PropertyInfo) mi).GetAccessors (true);
2790 foreach (MethodInfo m in methods){
2794 } else if (mi is MyEventBuilder){
2795 Pair p = (Pair) events [mi];
2797 if (p.First == mb || p.Second == mb)
2799 } else if (mi is EventInfo){
2800 EventInfo ei = ((EventInfo) mi);
2802 if (ei.GetAddMethod (true) == mb)
2805 if (ei.GetRemoveMethod (true) == mb)
2808 if (ei.GetRaiseMethod (true) == mb)
2814 // Now check if it is an operator method
2818 if (s.StartsWith ("op_")){
2819 foreach (string name in Unary.oper_names){
2824 foreach (string name in Binary.oper_names){
2838 /// There is exactly one instance of this class per type.
2840 public sealed class TypeHandle : IMemberContainer {
2841 public readonly TypeHandle BaseType;
2843 readonly int id = ++next_id;
2844 static int next_id = 0;
2847 /// Lookup a TypeHandle instance for the given type. If the type doesn't have
2848 /// a TypeHandle yet, a new instance of it is created. This static method
2849 /// ensures that we'll only have one TypeHandle instance per type.
2851 public static TypeHandle GetTypeHandle (Type t)
2853 TypeHandle handle = (TypeHandle) type_hash [t];
2857 handle = new TypeHandle (t);
2858 type_hash.Add (t, handle);
2862 public static void CleanUp ()
2868 /// Returns the TypeHandle for TypeManager.object_type.
2870 public static IMemberContainer ObjectType {
2872 if (object_type != null)
2875 object_type = GetTypeHandle (TypeManager.object_type);
2882 /// Returns the TypeHandle for TypeManager.array_type.
2884 public static IMemberContainer ArrayType {
2886 if (array_type != null)
2889 array_type = GetTypeHandle (TypeManager.array_type);
2895 private static PtrHashtable type_hash = new PtrHashtable ();
2897 private static TypeHandle object_type = null;
2898 private static TypeHandle array_type = null;
2901 private bool is_interface;
2902 private MemberCache member_cache;
2904 private TypeHandle (Type type)
2907 if (type.BaseType != null)
2908 BaseType = GetTypeHandle (type.BaseType);
2909 this.is_interface = type.IsInterface;
2910 this.member_cache = new MemberCache (this);
2913 // IMemberContainer methods
2915 public string Name {
2917 return type.FullName;
2927 public IMemberContainer Parent {
2933 public bool IsInterface {
2935 return is_interface;
2939 public MemberList GetMembers (MemberTypes mt, BindingFlags bf)
2941 MemberInfo [] members;
2942 if (mt == MemberTypes.Event)
2943 members = type.GetEvents (bf | BindingFlags.DeclaredOnly);
2945 members = type.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
2947 Array.Reverse (members);
2949 return new MemberList (members);
2952 // IMemberFinder methods
2954 public MemberList FindMembers (MemberTypes mt, BindingFlags bf, string name,
2955 MemberFilter filter, object criteria)
2957 return new MemberList (member_cache.FindMembers (mt, bf, name, filter, criteria));
2960 public MemberCache MemberCache {
2962 return member_cache;
2966 public override string ToString ()
2968 if (BaseType != null)
2969 return "TypeHandle (" + id + "," + Name + " : " + BaseType + ")";
2971 return "TypeHandle (" + id + "," + Name + ")";