2 // typemanager.cs: C# type manager
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 // Ravi Pratap (ravi@ximian.com)
6 // Marek Safar (marek.safar@seznam.cz)
8 // Licensed under the terms of the GNU GPL
10 // (C) 2001 Ximian, Inc (http://www.ximian.com)
15 // We will eventually remove the SIMPLE_SPEEDUP, and should never change
16 // the behavior of the compilation. This can be removed if we rework
17 // the code to get a list of namespaces available.
19 #define SIMPLE_SPEEDUP
23 using System.Globalization;
24 using System.Collections;
25 using System.Reflection;
26 using System.Reflection.Emit;
28 using System.Text.RegularExpressions;
29 using System.Runtime.CompilerServices;
30 using System.Diagnostics;
32 namespace Mono.CSharp {
34 public partial class TypeManager {
36 // A list of core types that the compiler requires or uses
38 static public Type object_type;
39 static public Type value_type;
40 static public Type string_type;
41 static public Type int32_type;
42 static public Type uint32_type;
43 static public Type int64_type;
44 static public Type uint64_type;
45 static public Type float_type;
46 static public Type double_type;
47 static public Type char_type;
48 static public Type char_ptr_type;
49 static public Type short_type;
50 static public Type decimal_type;
51 static public Type bool_type;
52 static public Type sbyte_type;
53 static public Type byte_type;
54 static public Type ushort_type;
55 static public Type enum_type;
56 static public Type delegate_type;
57 static public Type multicast_delegate_type;
58 static public Type void_type;
59 static public Type null_type;
60 static public Type enumeration_type;
61 static public Type array_type;
62 static public Type runtime_handle_type;
63 static public Type icloneable_type;
64 static public Type type_type;
65 static public Type ienumerator_type;
66 static public Type ienumerable_type;
67 static public Type idisposable_type;
68 static public Type iconvertible_type;
69 static public Type default_member_type;
70 static public Type iasyncresult_type;
71 static public Type asynccallback_type;
72 static public Type intptr_type;
73 static public Type monitor_type;
74 static public Type runtime_field_handle_type;
75 static public Type runtime_argument_handle_type;
76 static public Type attribute_type;
77 static public Type attribute_usage_type;
78 static public Type decimal_constant_attribute_type;
79 static public Type dllimport_type;
80 static public Type unverifiable_code_type;
81 static public Type methodimpl_attr_type;
82 static public Type marshal_as_attr_type;
83 static public Type param_array_type;
84 static public Type void_ptr_type;
85 static public Type indexer_name_type;
86 static public Type exception_type;
87 static public Type invalid_operation_exception_type;
88 static public Type not_supported_exception_type;
89 static public Type obsolete_attribute_type;
90 static public Type conditional_attribute_type;
91 static public Type in_attribute_type;
92 static public Type out_attribute_type;
93 static public Type anonymous_method_type;
94 static public Type cls_compliant_attribute_type;
95 static public Type typed_reference_type;
96 static public Type arg_iterator_type;
97 static public Type mbr_type;
98 static public Type struct_layout_attribute_type;
99 static public Type field_offset_attribute_type;
100 static public Type security_attr_type;
101 static public Type required_attr_type;
102 static public Type guid_attr_type;
103 static public Type assembly_culture_attribute_type;
104 static public Type coclass_attr_type;
105 static public Type comimport_attr_type;
110 static internal Type runtime_compatibility_attr_type;
111 static internal Type compiler_generated_attr_type;
112 static internal Type fixed_buffer_attr_type;
113 static internal Type default_charset_type;
114 static internal Type internals_visible_attr_type;
117 // An empty array of types
119 static public Type [] NoTypes;
120 static public TypeExpr [] NoTypeExprs;
124 // Expressions representing the internal types. Used during declaration
127 static public TypeExpr system_object_expr, system_string_expr;
128 static public TypeExpr system_boolean_expr, system_decimal_expr;
129 static public TypeExpr system_single_expr, system_double_expr;
130 static public TypeExpr system_sbyte_expr, system_byte_expr;
131 static public TypeExpr system_int16_expr, system_uint16_expr;
132 static public TypeExpr system_int32_expr, system_uint32_expr;
133 static public TypeExpr system_int64_expr, system_uint64_expr;
134 static public TypeExpr system_char_expr, system_void_expr;
135 static public TypeExpr system_asynccallback_expr;
136 static public TypeExpr system_iasyncresult_expr;
137 static public TypeExpr system_valuetype_expr;
138 static public TypeExpr system_intptr_expr;
141 // This is only used when compiling corlib
143 static public Type system_int32_type;
144 static public Type system_array_type;
145 static public Type system_type_type;
146 static public Type system_assemblybuilder_type;
147 static public MethodInfo system_int_array_get_length;
148 static public MethodInfo system_int_array_get_rank;
149 static public MethodInfo system_object_array_clone;
150 static public MethodInfo system_int_array_get_length_int;
151 static public MethodInfo system_int_array_get_lower_bound_int;
152 static public MethodInfo system_int_array_get_upper_bound_int;
153 static public MethodInfo system_void_array_copyto_array_int;
157 // Internal, not really used outside
159 static Type runtime_helpers_type;
162 // These methods are called by code generated by the compiler
164 static public MethodInfo string_concat_string_string;
165 static public MethodInfo string_concat_string_string_string;
166 static public MethodInfo string_concat_string_string_string_string;
167 static public MethodInfo string_concat_string_dot_dot_dot;
168 static public MethodInfo string_concat_object_object;
169 static public MethodInfo string_concat_object_object_object;
170 static public MethodInfo string_concat_object_dot_dot_dot;
171 static public MethodInfo string_isinterneted_string;
172 static public MethodInfo system_type_get_type_from_handle;
173 static public MethodInfo bool_movenext_void;
174 static public MethodInfo ienumerable_getenumerator_void;
175 static public MethodInfo void_reset_void;
176 static public MethodInfo void_dispose_void;
177 static public MethodInfo void_monitor_enter_object;
178 static public MethodInfo void_monitor_exit_object;
179 static public MethodInfo void_initializearray_array_fieldhandle;
180 static public MethodInfo int_getlength_int;
181 static public MethodInfo delegate_combine_delegate_delegate;
182 static public MethodInfo delegate_remove_delegate_delegate;
183 static public MethodInfo int_get_offset_to_string_data;
184 static public MethodInfo int_array_get_length;
185 static public MethodInfo int_array_get_rank;
186 static public MethodInfo object_array_clone;
187 static public MethodInfo int_array_get_length_int;
188 static public MethodInfo int_array_get_lower_bound_int;
189 static public MethodInfo int_array_get_upper_bound_int;
190 static public MethodInfo void_array_copyto_array_int;
191 static public PropertyInfo ienumerator_getcurrent;
194 // The attribute constructors.
196 static public ConstructorInfo object_ctor;
197 static public ConstructorInfo cons_param_array_attribute;
198 static public ConstructorInfo void_decimal_ctor_five_args;
199 static public ConstructorInfo void_decimal_ctor_int_arg;
200 static public ConstructorInfo unverifiable_code_ctor;
201 static public ConstructorInfo default_member_ctor;
202 static public ConstructorInfo decimal_constant_attribute_ctor;
203 static internal ConstructorInfo struct_layout_attribute_ctor;
204 static public ConstructorInfo field_offset_attribute_ctor;
209 static internal CustomAttributeBuilder compiler_generated_attr;
210 static internal ConstructorInfo fixed_buffer_attr_ctor;
212 static PtrHashtable builder_to_declspace;
214 static PtrHashtable builder_to_member_cache;
217 // Tracks the interfaces implemented by typebuilders. We only
218 // enter those who do implement or or more interfaces
220 static PtrHashtable builder_to_ifaces;
223 // Maps PropertyBuilder to a Type array that contains
224 // the arguments to the indexer
226 static Hashtable indexer_arguments;
229 // Maps a MethodBase to its ParameterData (either InternalParameters or ReflectionParameters)
231 static Hashtable method_params;
234 // A hash table from override methods to their base virtual method.
236 static Hashtable method_overrides;
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 AllClsTopLevelTypes;
250 static Hashtable fieldbuilders_to_fields;
251 static Hashtable fields;
253 static PtrHashtable assembly_internals_vis_attrs;
260 public static void CleanUp ()
262 // Lets get everything clean so that we can collect before generating code
263 builder_to_declspace = null;
264 builder_to_member_cache = null;
265 builder_to_ifaces = null;
266 indexer_arguments = null;
267 method_params = null;
268 builder_to_method = null;
271 fieldbuilders_to_fields = null;
273 priv_fields_events = null;
277 assembly_internals_vis_attrs = null;
280 TypeHandle.CleanUp ();
284 /// A filter for Findmembers that uses the Signature object to
287 static bool SignatureFilter (MemberInfo mi, object criteria)
289 Signature sig = (Signature) criteria;
291 if (!(mi is MethodBase))
294 if (mi.Name != sig.name)
297 int count = sig.args.Length;
299 if (mi is MethodBuilder || mi is ConstructorBuilder){
300 Type [] candidate_args = GetParameterData ((MethodBase) mi).Types;
302 if (candidate_args.Length != count)
305 for (int i = 0; i < count; i++)
306 if (candidate_args [i] != sig.args [i])
311 ParameterInfo [] pars = ((MethodBase) mi).GetParameters ();
313 if (pars.Length != count)
316 for (int i = 0; i < count; i++)
317 if (pars [i].ParameterType != sig.args [i])
323 // A delegate that points to the filter above.
324 static MemberFilter signature_filter;
327 // These are expressions that represent some of the internal data types, used
330 static void InitExpressionTypes ()
332 system_object_expr = new TypeLookupExpression ("System.Object");
333 system_string_expr = new TypeLookupExpression ("System.String");
334 system_boolean_expr = new TypeLookupExpression ("System.Boolean");
335 system_decimal_expr = new TypeLookupExpression ("System.Decimal");
336 system_single_expr = new TypeLookupExpression ("System.Single");
337 system_double_expr = new TypeLookupExpression ("System.Double");
338 system_sbyte_expr = new TypeLookupExpression ("System.SByte");
339 system_byte_expr = new TypeLookupExpression ("System.Byte");
340 system_int16_expr = new TypeLookupExpression ("System.Int16");
341 system_uint16_expr = new TypeLookupExpression ("System.UInt16");
342 system_int32_expr = new TypeLookupExpression ("System.Int32");
343 system_uint32_expr = new TypeLookupExpression ("System.UInt32");
344 system_int64_expr = new TypeLookupExpression ("System.Int64");
345 system_uint64_expr = new TypeLookupExpression ("System.UInt64");
346 system_char_expr = new TypeLookupExpression ("System.Char");
347 system_void_expr = new TypeLookupExpression ("System.Void");
348 system_asynccallback_expr = new TypeLookupExpression ("System.AsyncCallback");
349 system_iasyncresult_expr = new TypeLookupExpression ("System.IAsyncResult");
350 system_valuetype_expr = new TypeLookupExpression ("System.ValueType");
351 system_intptr_expr = new TypeLookupExpression ("System.IntPtr");
354 static TypeManager ()
358 signature_filter = new MemberFilter (SignatureFilter);
359 InitExpressionTypes ();
362 static public void Reset ()
364 builder_to_declspace = new PtrHashtable ();
365 builder_to_member_cache = new PtrHashtable ();
366 builder_to_method = new PtrHashtable ();
367 method_params = new PtrHashtable ();
368 method_overrides = new PtrHashtable ();
369 indexer_arguments = new PtrHashtable ();
370 builder_to_ifaces = new PtrHashtable ();
372 NoTypes = new Type [0];
373 NoTypeExprs = new TypeExpr [0];
375 fieldbuilders_to_fields = new Hashtable ();
376 fields = new Hashtable ();
377 type_hash = new DoubleHash ();
379 assembly_internals_vis_attrs = new PtrHashtable ();
384 public static void AddUserType (DeclSpace ds)
386 builder_to_declspace.Add (ds.TypeBuilder, ds);
390 // This entry point is used by types that we define under the covers
392 public static void RegisterBuilder (Type tb, Type [] ifaces)
395 builder_to_ifaces [tb] = ifaces;
398 public static void AddMethod (MethodBase builder, IMethodData method)
400 builder_to_method.Add (builder, method);
401 method_params.Add (builder, method.ParameterInfo);
404 public static IMethodData GetMethod (MethodBase builder)
406 return (IMethodData) builder_to_method [builder];
410 /// Returns the DeclSpace whose Type is `t' or null if there is no
411 /// DeclSpace for `t' (ie, the Type comes from a library)
413 public static DeclSpace LookupDeclSpace (Type t)
415 return builder_to_declspace [t] as DeclSpace;
419 /// Returns the TypeContainer whose Type is `t' or null if there is no
420 /// TypeContainer for `t' (ie, the Type comes from a library)
422 public static TypeContainer LookupTypeContainer (Type t)
424 return builder_to_declspace [t] as TypeContainer;
427 public static MemberCache LookupMemberCache (Type t)
429 if (t is TypeBuilder) {
430 IMemberContainer container = builder_to_declspace [t] as IMemberContainer;
431 if (container != null)
432 return container.MemberCache;
435 if (t is GenericTypeParameterBuilder) {
436 IMemberContainer container = builder_to_type_param [t] as IMemberContainer;
438 if (container != null)
439 return container.MemberCache;
442 return TypeHandle.GetMemberCache (t);
445 public static MemberCache LookupBaseInterfacesCache (Type t)
447 Type [] ifaces = t.GetInterfaces ();
449 if (ifaces != null && ifaces.Length == 1)
450 return LookupMemberCache (ifaces [0]);
452 // TODO: the builder_to_member_cache should be indexed by 'ifaces', not 't'
453 MemberCache cache = builder_to_member_cache [t] as MemberCache;
457 cache = new MemberCache (ifaces);
458 builder_to_member_cache.Add (t, cache);
462 public static TypeContainer LookupInterface (Type t)
464 TypeContainer tc = (TypeContainer) builder_to_declspace [t];
465 if ((tc == null) || (tc.Kind != Kind.Interface))
471 public static Delegate LookupDelegate (Type t)
473 return builder_to_declspace [t] as Delegate;
476 public static Class LookupClass (Type t)
478 return (Class) builder_to_declspace [t];
482 // We use this hash for multiple kinds of constructed types:
484 // (T, "&") Given T, get T &
485 // (T, "*") Given T, get T *
486 // (T, "[]") Given T and a array dimension, get T []
487 // (T, X) Given a type T and a simple name X, get the type T+X
489 // Accessibility tests, if necessary, should be done by the user
491 static DoubleHash type_hash = new DoubleHash ();
494 // Gets the reference to T version of the Type (T&)
496 public static Type GetReferenceType (Type t)
498 return t.MakeByRefType ();
502 // Gets the pointer to T version of the Type (T*)
504 public static Type GetPointerType (Type t)
506 return GetConstructedType (t, "*");
509 public static Type GetConstructedType (Type t, string dim)
512 if (type_hash.Lookup (t, dim, out ret))
515 ret = t.Module.GetType (t.ToString () + dim);
517 type_hash.Insert (t, dim, ret);
522 ret = GetReferenceType (t);
523 type_hash.Insert (t, dim, ret);
527 if (t.IsGenericParameter || t.IsGenericInstance) {
530 while ((pos < dim.Length) && (dim [pos] == '[')) {
533 if (dim [pos] == ']') {
534 result = result.MakeArrayType ();
537 if (pos < dim.Length)
540 type_hash.Insert (t, dim, result);
545 while (dim [pos] == ',') {
549 if ((dim [pos] != ']') || (pos != dim.Length-1))
552 result = result.MakeArrayType (rank + 1);
553 type_hash.Insert (t, dim, result);
558 type_hash.Insert (t, dim, null);
562 public static Type GetNestedType (Type t, string name)
565 if (!type_hash.Lookup (t, name, out ret)) {
566 ret = t.GetNestedType (name,
567 BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
568 type_hash.Insert (t, name, ret);
574 /// Fills static table with exported types from all referenced assemblies.
575 /// This information is required for CLS Compliance tests.
577 public static void LoadAllImportedTypes ()
579 AllClsTopLevelTypes = new Hashtable (1500);
580 foreach (Assembly a in RootNamespace.Global.Assemblies) {
581 foreach (Type t in a.GetExportedTypes ()) {
582 AllClsTopLevelTypes [t.FullName.ToLower (System.Globalization.CultureInfo.InvariantCulture)] = null;
587 public static bool NamespaceClash (string name, Location loc)
589 if (! RootNamespace.Global.IsNamespace (name))
592 Report.Error (519, loc, String.Format ("`{0}' clashes with a predefined namespace", name));
597 /// Returns the C# name of a type if possible, or the full type name otherwise
599 static public string CSharpName (Type t)
601 string name = GetFullName (t);
603 return Regex.Replace (name,
605 @"(Int32|UInt32|Int16|UInt16|Int64|UInt64|" +
606 @"Single|Double|Char|Decimal|Byte|SByte|Object|" +
607 @"Boolean|String|Void|Null)" +
609 new MatchEvaluator (CSharpNameMatch)).Replace ('+', '.');
612 static public string CSharpName (Type[] types)
614 StringBuilder sb = new StringBuilder ();
615 foreach (Type t in types) {
616 sb.Append (CSharpName (t));
619 sb.Remove (sb.Length - 1, 1);
620 return sb.ToString ();
623 static String CSharpNameMatch (Match match)
625 string s = match.Groups [1].Captures [0].Value;
627 Replace ("int32", "int").
628 Replace ("uint32", "uint").
629 Replace ("int16", "short").
630 Replace ("uint16", "ushort").
631 Replace ("int64", "long").
632 Replace ("uint64", "ulong").
633 Replace ("single", "float").
634 Replace ("boolean", "bool")
635 + match.Groups [2].Captures [0].Value;
639 /// Returns the signature of the method with full namespace classification
641 static public string GetFullNameSignature (MemberInfo mi)
643 PropertyInfo pi = mi as PropertyInfo;
645 MethodBase pmi = pi.GetGetMethod ();
647 pmi = pi.GetSetMethod ();
648 if (GetParameterData (pmi).Count > 0)
651 return (mi is MethodBase)
652 ? CSharpSignature (mi as MethodBase)
653 : CSharpName (mi.DeclaringType) + '.' + mi.Name;
656 static string GetFullName (Type t)
658 if (t.IsGenericParameter)
661 string name = t.FullName;
662 if (name == null) // It looks like mono bug
665 StringBuilder sb = new StringBuilder (RemoveGenericArity (name));
667 Type[] this_args = GetTypeArguments (t);
669 if (this_args.Length > 0) {
671 for (int i = 0; i < this_args.Length; i++) {
674 sb.Append (CSharpName (this_args[i]));
679 return sb.ToString ();
682 static string RemoveGenericArity (string from)
684 int i = from.IndexOf ('`');
686 return from.Substring (0, i);
691 /// When we need to report accessors as well
693 static public string CSharpSignature (MethodBase mb)
695 return CSharpSignature (mb, false);
699 /// Returns the signature of the method
701 static public string CSharpSignature (MethodBase mb, bool show_accessor)
703 StringBuilder sig = new StringBuilder (CSharpName (mb.DeclaringType));
706 ParameterData iparams = GetParameterData (mb);
707 string parameters = iparams.GetSignatureForError ();
708 string accessor = "";
711 if (mb.IsSpecialName) {
712 Operator.OpType ot = Operator.GetOperatorType (mb.Name);
713 if (ot != Operator.OpType.TOP) {
714 sig.Append ("operator ");
715 sig.Append (Operator.GetName (ot));
716 sig.Append (parameters);
717 return sig.ToString ();
720 if (mb.Name.StartsWith ("get_") || mb.Name.StartsWith ("set_")) {
721 accessor = mb.Name.Substring (0, 3);
726 if (mb.IsSpecialName && !mb.IsConstructor) {
727 if (iparams.Count > (mb.Name.StartsWith ("get_") ? 0 : 1)) {
728 sig.Append ("this[");
730 sig.Append (parameters.Substring (1, parameters.Length - 2));
733 int before_ret_val = parameters.LastIndexOf (',');
734 if (before_ret_val < 0)
735 sig.Append (parameters.Substring (1, parameters.Length - 2));
737 sig.Append (parameters.Substring (1, before_ret_val - 1));
741 sig.Append (mb.Name.Substring (4));
744 if (mb.Name == ".ctor")
745 sig.Append (mb.DeclaringType.Name);
747 sig.Append (mb.Name);
749 sig.Append (parameters);
752 if (show_accessor && accessor.Length > 0) {
754 sig.Append (accessor);
757 return sig.ToString ();
760 public static string GetMethodName (MethodInfo m)
762 if (!IsGenericMethod (m))
765 return MemberName.MakeName (m.Name, m.GetGenericArguments ().Length);
768 static public string CSharpSignature (EventInfo ei)
770 return CSharpName (ei.DeclaringType) + '.' + ei.Name;
774 /// Looks up a type, and aborts if it is not found. This is used
775 /// by types required by the compiler
777 static Type CoreLookupType (string ns_name, string name)
779 Namespace ns = RootNamespace.Global.GetNamespace (ns_name, true);
780 FullNamedExpression fne = ns.Lookup (RootContext.Tree.Types, name, Location.Null);
781 Type t = fne == null ? null : fne.Type;
783 Report.Error (518, "The predefined type `" + name + "' is not defined or imported");
788 /// Returns the MethodInfo for a method named `name' defined
789 /// in type `t' which takes arguments of types `args'
791 static MethodInfo GetMethod (Type t, string name, Type [] args, bool is_private, bool report_errors)
795 BindingFlags flags = instance_and_static | BindingFlags.Public;
801 flags |= BindingFlags.NonPublic;
803 list = FindMembers (t, MemberTypes.Method, flags, signature_filter, sig);
804 if (list.Count == 0) {
806 Report.Error (-19, "Can not find the core function `" + name + "'");
810 MethodInfo mi = list [0] as MethodInfo;
813 Report.Error (-19, "Can not find the core function `" + name + "'");
820 static MethodInfo GetMethod (Type t, string name, Type [] args, bool report_errors)
822 return GetMethod (t, name, args, false, report_errors);
825 static MethodInfo GetMethod (Type t, string name, Type [] args)
827 return GetMethod (t, name, args, true);
831 /// Returns the PropertyInfo for a property named `name' defined
834 public static PropertyInfo GetProperty (Type t, string name)
836 MemberList list = FindMembers (t, MemberTypes.Property, BindingFlags.Public |
837 BindingFlags.Instance, Type.FilterName, name);
838 if (list.Count == 0) {
839 Report.Error (-19, "Can not find the core property `" + name + "'");
843 PropertyInfo pi = list [0] as PropertyInfo;
845 Report.Error (-19, "Can not find the core function `" + name + "'");
853 /// Returns the ConstructorInfo for "args"
855 public static ConstructorInfo GetConstructor (Type t, Type [] args)
863 list = FindMembers (t, MemberTypes.Constructor,
864 instance_and_static | BindingFlags.Public | BindingFlags.DeclaredOnly,
865 signature_filter, sig);
866 if (list.Count == 0){
867 Report.Error (-19, "Can not find the core constructor for type `" + t.Name + "'");
871 ConstructorInfo ci = list [0] as ConstructorInfo;
873 Report.Error (-19, "Can not find the core constructor for type `" + t.Name + "'");
880 public static void InitEnumUnderlyingTypes ()
883 int32_type = CoreLookupType ("System", "Int32");
884 int64_type = CoreLookupType ("System", "Int64");
885 uint32_type = CoreLookupType ("System", "UInt32");
886 uint64_type = CoreLookupType ("System", "UInt64");
887 byte_type = CoreLookupType ("System", "Byte");
888 sbyte_type = CoreLookupType ("System", "SByte");
889 short_type = CoreLookupType ("System", "Int16");
890 ushort_type = CoreLookupType ("System", "UInt16");
894 /// The types have to be initialized after the initial
895 /// population of the type has happened (for example, to
896 /// bootstrap the corlib.dll
898 public static void InitCoreTypes ()
900 object_type = CoreLookupType ("System", "Object");
901 value_type = CoreLookupType ("System", "ValueType");
903 InitEnumUnderlyingTypes ();
905 char_type = CoreLookupType ("System", "Char");
906 string_type = CoreLookupType ("System", "String");
907 float_type = CoreLookupType ("System", "Single");
908 double_type = CoreLookupType ("System", "Double");
909 char_ptr_type = GetPointerType (char_type);
910 decimal_type = CoreLookupType ("System", "Decimal");
911 bool_type = CoreLookupType ("System", "Boolean");
912 enum_type = CoreLookupType ("System", "Enum");
914 multicast_delegate_type = CoreLookupType ("System", "MulticastDelegate");
915 delegate_type = CoreLookupType ("System", "Delegate");
917 array_type = CoreLookupType ("System", "Array");
918 void_type = CoreLookupType ("System", "Void");
919 type_type = CoreLookupType ("System", "Type");
921 runtime_field_handle_type = CoreLookupType ("System", "RuntimeFieldHandle");
922 runtime_argument_handle_type = CoreLookupType ("System", "RuntimeArgumentHandle");
923 runtime_helpers_type = CoreLookupType ("System.Runtime.CompilerServices", "RuntimeHelpers");
924 default_member_type = CoreLookupType ("System.Reflection", "DefaultMemberAttribute");
925 runtime_handle_type = CoreLookupType ("System", "RuntimeTypeHandle");
926 asynccallback_type = CoreLookupType ("System", "AsyncCallback");
927 iasyncresult_type = CoreLookupType ("System", "IAsyncResult");
928 ienumerator_type = CoreLookupType ("System.Collections", "IEnumerator");
929 ienumerable_type = CoreLookupType ("System.Collections", "IEnumerable");
930 idisposable_type = CoreLookupType ("System", "IDisposable");
931 icloneable_type = CoreLookupType ("System", "ICloneable");
932 iconvertible_type = CoreLookupType ("System", "IConvertible");
933 monitor_type = CoreLookupType ("System.Threading", "Monitor");
934 intptr_type = CoreLookupType ("System", "IntPtr");
936 attribute_type = CoreLookupType ("System", "Attribute");
937 attribute_usage_type = CoreLookupType ("System", "AttributeUsageAttribute");
938 dllimport_type = CoreLookupType ("System.Runtime.InteropServices", "DllImportAttribute");
939 methodimpl_attr_type = CoreLookupType ("System.Runtime.CompilerServices", "MethodImplAttribute");
940 marshal_as_attr_type = CoreLookupType ("System.Runtime.InteropServices", "MarshalAsAttribute");
941 param_array_type = CoreLookupType ("System", "ParamArrayAttribute");
942 in_attribute_type = CoreLookupType ("System.Runtime.InteropServices", "InAttribute");
943 out_attribute_type = CoreLookupType ("System.Runtime.InteropServices", "OutAttribute");
944 typed_reference_type = CoreLookupType ("System", "TypedReference");
945 arg_iterator_type = CoreLookupType ("System", "ArgIterator");
946 mbr_type = CoreLookupType ("System", "MarshalByRefObject");
947 decimal_constant_attribute_type = CoreLookupType ("System.Runtime.CompilerServices", "DecimalConstantAttribute");
949 unverifiable_code_type= CoreLookupType ("System.Security", "UnverifiableCodeAttribute");
951 void_ptr_type = GetPointerType (void_type);
953 indexer_name_type = CoreLookupType ("System.Runtime.CompilerServices", "IndexerNameAttribute");
955 exception_type = CoreLookupType ("System", "Exception");
956 invalid_operation_exception_type = CoreLookupType ("System", "InvalidOperationException");
957 not_supported_exception_type = CoreLookupType ("System", "NotSupportedException");
962 obsolete_attribute_type = CoreLookupType ("System", "ObsoleteAttribute");
963 conditional_attribute_type = CoreLookupType ("System.Diagnostics", "ConditionalAttribute");
964 cls_compliant_attribute_type = CoreLookupType ("System", "CLSCompliantAttribute");
965 struct_layout_attribute_type = CoreLookupType ("System.Runtime.InteropServices", "StructLayoutAttribute");
966 field_offset_attribute_type = CoreLookupType ("System.Runtime.InteropServices", "FieldOffsetAttribute");
967 security_attr_type = CoreLookupType ("System.Security.Permissions", "SecurityAttribute");
968 required_attr_type = CoreLookupType ("System.Runtime.CompilerServices", "RequiredAttributeAttribute");
969 guid_attr_type = CoreLookupType ("System.Runtime.InteropServices", "GuidAttribute");
970 assembly_culture_attribute_type = CoreLookupType ("System.Reflection", "AssemblyCultureAttribute");
971 comimport_attr_type = CoreLookupType ("System.Runtime.InteropServices", "ComImportAttribute");
972 coclass_attr_type = CoreLookupType ("System.Runtime.InteropServices", "CoClassAttribute");
974 InitGenericCoreTypes ();
979 compiler_generated_attr_type = CoreLookupType ("System.Runtime.CompilerServices", "CompilerGeneratedAttribute");
980 fixed_buffer_attr_type = CoreLookupType ("System.Runtime.CompilerServices", "FixedBufferAttribute");
981 default_charset_type = CoreLookupType ("System.Runtime.InteropServices", "DefaultCharSetAttribute");
982 internals_visible_attr_type = CoreLookupType ("System.Runtime.CompilerServices", "InternalsVisibleToAttribute");
983 runtime_compatibility_attr_type = CoreLookupType ("System.Runtime.CompilerServices", "RuntimeCompatibilityAttribute");
985 // When compiling corlib, store the "real" types here.
987 if (!RootContext.StdLib) {
988 system_int32_type = typeof (System.Int32);
989 system_array_type = typeof (System.Array);
990 system_type_type = typeof (System.Type);
991 system_assemblybuilder_type = typeof (System.Reflection.Emit.AssemblyBuilder);
993 Type [] void_arg = { };
994 system_int_array_get_length = GetMethod (
995 system_array_type, "get_Length", void_arg);
996 system_int_array_get_rank = GetMethod (
997 system_array_type, "get_Rank", void_arg);
998 system_object_array_clone = GetMethod (
999 system_array_type, "Clone", void_arg);
1001 Type [] system_int_arg = { system_int32_type };
1002 system_int_array_get_length_int = GetMethod (
1003 system_array_type, "GetLength", system_int_arg);
1004 system_int_array_get_upper_bound_int = GetMethod (
1005 system_array_type, "GetUpperBound", system_int_arg);
1006 system_int_array_get_lower_bound_int = GetMethod (
1007 system_array_type, "GetLowerBound", system_int_arg);
1009 Type [] system_array_int_arg = { system_array_type, system_int32_type };
1010 system_void_array_copyto_array_int = GetMethod (
1011 system_array_type, "CopyTo", system_array_int_arg);
1013 Type [] system_3_type_arg = {
1014 system_type_type, system_type_type, system_type_type };
1015 Type [] system_4_type_arg = {
1016 system_type_type, system_type_type, system_type_type, system_type_type };
1018 MethodInfo set_corlib_type_builders = GetMethod (
1019 system_assemblybuilder_type, "SetCorlibTypeBuilders",
1020 system_4_type_arg, true, false);
1022 if (set_corlib_type_builders != null) {
1023 object[] args = new object [4];
1024 args [0] = object_type;
1025 args [1] = value_type;
1026 args [2] = enum_type;
1027 args [3] = void_type;
1029 set_corlib_type_builders.Invoke (CodeGen.Assembly.Builder, args);
1031 // Compatibility for an older version of the class libs.
1032 set_corlib_type_builders = GetMethod (
1033 system_assemblybuilder_type, "SetCorlibTypeBuilders",
1034 system_3_type_arg, true, true);
1036 if (set_corlib_type_builders == null) {
1037 Report.Error (-26, "Corlib compilation is not supported in Microsoft.NET due to bugs in it");
1041 object[] args = new object [3];
1042 args [0] = object_type;
1043 args [1] = value_type;
1044 args [2] = enum_type;
1046 set_corlib_type_builders.Invoke (CodeGen.Assembly.Builder, args);
1050 system_object_expr.Type = object_type;
1051 system_string_expr.Type = string_type;
1052 system_boolean_expr.Type = bool_type;
1053 system_decimal_expr.Type = decimal_type;
1054 system_single_expr.Type = float_type;
1055 system_double_expr.Type = double_type;
1056 system_sbyte_expr.Type = sbyte_type;
1057 system_byte_expr.Type = byte_type;
1058 system_int16_expr.Type = short_type;
1059 system_uint16_expr.Type = ushort_type;
1060 system_int32_expr.Type = int32_type;
1061 system_uint32_expr.Type = uint32_type;
1062 system_int64_expr.Type = int64_type;
1063 system_uint64_expr.Type = uint64_type;
1064 system_char_expr.Type = char_type;
1065 system_void_expr.Type = void_type;
1066 system_asynccallback_expr.Type = asynccallback_type;
1067 system_iasyncresult_expr.Type = iasyncresult_type;
1068 system_valuetype_expr.Type = value_type;
1071 // These are only used for compare purposes
1073 anonymous_method_type = typeof (AnonymousMethod);
1074 null_type = typeof (NullType);
1078 // The helper methods that are used by the compiler
1080 public static void InitCodeHelpers ()
1083 // Now load the default methods that we use.
1085 Type [] string_string = { string_type, string_type };
1086 string_concat_string_string = GetMethod (
1087 string_type, "Concat", string_string);
1088 Type [] string_string_string = { string_type, string_type, string_type };
1089 string_concat_string_string_string = GetMethod (
1090 string_type, "Concat", string_string_string);
1091 Type [] string_string_string_string = { string_type, string_type, string_type, string_type };
1092 string_concat_string_string_string_string = GetMethod (
1093 string_type, "Concat", string_string_string_string);
1094 Type[] params_string = { GetConstructedType (string_type, "[]") };
1095 string_concat_string_dot_dot_dot = GetMethod (
1096 string_type, "Concat", params_string);
1098 Type [] object_object = { object_type, object_type };
1099 string_concat_object_object = GetMethod (
1100 string_type, "Concat", object_object);
1101 Type [] object_object_object = { object_type, object_type, object_type };
1102 string_concat_object_object_object = GetMethod (
1103 string_type, "Concat", object_object_object);
1104 Type[] params_object = { GetConstructedType (object_type, "[]") };
1105 string_concat_object_dot_dot_dot = GetMethod (
1106 string_type, "Concat", params_object);
1108 Type [] string_ = { string_type };
1109 string_isinterneted_string = GetMethod (
1110 string_type, "IsInterned", string_);
1112 Type [] runtime_type_handle = { runtime_handle_type };
1113 system_type_get_type_from_handle = GetMethod (
1114 type_type, "GetTypeFromHandle", runtime_type_handle);
1116 Type [] delegate_delegate = { delegate_type, delegate_type };
1117 delegate_combine_delegate_delegate = GetMethod (
1118 delegate_type, "Combine", delegate_delegate);
1120 delegate_remove_delegate_delegate = GetMethod (
1121 delegate_type, "Remove", delegate_delegate);
1126 Type [] void_arg = { };
1127 ienumerator_getcurrent = GetProperty (
1128 ienumerator_type, "Current");
1129 bool_movenext_void = GetMethod (
1130 ienumerator_type, "MoveNext", void_arg);
1131 void_reset_void = GetMethod (
1132 ienumerator_type, "Reset", void_arg);
1133 void_dispose_void = GetMethod (
1134 idisposable_type, "Dispose", void_arg);
1135 int_get_offset_to_string_data = GetMethod (
1136 runtime_helpers_type, "get_OffsetToStringData", void_arg);
1137 int_array_get_length = GetMethod (
1138 array_type, "get_Length", void_arg);
1139 int_array_get_rank = GetMethod (
1140 array_type, "get_Rank", void_arg);
1141 ienumerable_getenumerator_void = GetMethod (
1142 ienumerable_type, "GetEnumerator", void_arg);
1147 Type [] int_arg = { int32_type };
1148 int_array_get_length_int = GetMethod (
1149 array_type, "GetLength", int_arg);
1150 int_array_get_upper_bound_int = GetMethod (
1151 array_type, "GetUpperBound", int_arg);
1152 int_array_get_lower_bound_int = GetMethod (
1153 array_type, "GetLowerBound", int_arg);
1156 // System.Array methods
1158 object_array_clone = GetMethod (
1159 array_type, "Clone", void_arg);
1160 Type [] array_int_arg = { array_type, int32_type };
1161 void_array_copyto_array_int = GetMethod (
1162 array_type, "CopyTo", array_int_arg);
1167 Type [] object_arg = { object_type };
1168 void_monitor_enter_object = GetMethod (
1169 monitor_type, "Enter", object_arg);
1170 void_monitor_exit_object = GetMethod (
1171 monitor_type, "Exit", object_arg);
1173 Type [] array_field_handle_arg = { array_type, runtime_field_handle_type };
1175 void_initializearray_array_fieldhandle = GetMethod (
1176 runtime_helpers_type, "InitializeArray", array_field_handle_arg);
1181 int_getlength_int = GetMethod (
1182 array_type, "GetLength", int_arg);
1185 // Decimal constructors
1187 Type [] dec_arg = { int32_type, int32_type, int32_type, bool_type, byte_type };
1188 void_decimal_ctor_five_args = GetConstructor (
1189 decimal_type, dec_arg);
1191 void_decimal_ctor_int_arg = GetConstructor (decimal_type, int_arg);
1196 cons_param_array_attribute = GetConstructor (param_array_type, void_arg);
1197 unverifiable_code_ctor = GetConstructor (unverifiable_code_type, void_arg);
1198 default_member_ctor = GetConstructor (default_member_type, string_);
1200 Type[] short_arg = { short_type };
1201 struct_layout_attribute_ctor = GetConstructor (struct_layout_attribute_type, short_arg);
1203 decimal_constant_attribute_ctor = GetConstructor (decimal_constant_attribute_type, new Type []
1204 { byte_type, byte_type, uint32_type, uint32_type, uint32_type } );
1206 field_offset_attribute_ctor = GetConstructor (field_offset_attribute_type, new Type []
1212 compiler_generated_attr = new CustomAttributeBuilder (
1213 GetConstructor (compiler_generated_attr_type, void_arg), new object[0]);
1215 Type[] type_int_arg = { type_type, int32_type };
1216 fixed_buffer_attr_ctor = GetConstructor (fixed_buffer_attr_type, type_int_arg);
1219 object_ctor = GetConstructor (object_type, void_arg);
1221 InitGenericCodeHelpers ();
1224 const BindingFlags instance_and_static = BindingFlags.Static | BindingFlags.Instance;
1227 /// This is the "old", non-cache based FindMembers() function. We cannot use
1228 /// the cache here because there is no member name argument.
1230 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1231 MemberFilter filter, object criteria)
1233 DeclSpace decl = (DeclSpace) builder_to_declspace [t];
1236 // `builder_to_declspace' contains all dynamic types.
1240 Timer.StartTimer (TimerType.FindMembers);
1241 list = decl.FindMembers (mt, bf, filter, criteria);
1242 Timer.StopTimer (TimerType.FindMembers);
1247 // We have to take care of arrays specially, because GetType on
1248 // a TypeBuilder array will return a Type, not a TypeBuilder,
1249 // and we can not call FindMembers on this type.
1251 if (t.IsSubclassOf (TypeManager.array_type))
1252 return new MemberList (TypeManager.array_type.FindMembers (mt, bf, filter, criteria));
1254 if (t is GenericTypeParameterBuilder) {
1255 TypeParameter tparam = (TypeParameter) builder_to_type_param [t];
1257 Timer.StartTimer (TimerType.FindMembers);
1258 MemberList list = tparam.FindMembers (
1259 mt, bf | BindingFlags.DeclaredOnly, filter, criteria);
1260 Timer.StopTimer (TimerType.FindMembers);
1265 // Since FindMembers will not lookup both static and instance
1266 // members, we emulate this behaviour here.
1268 if ((bf & instance_and_static) == instance_and_static){
1269 MemberInfo [] i_members = t.FindMembers (
1270 mt, bf & ~BindingFlags.Static, filter, criteria);
1272 int i_len = i_members.Length;
1274 MemberInfo one = i_members [0];
1277 // If any of these are present, we are done!
1279 if ((one is Type) || (one is EventInfo) || (one is FieldInfo))
1280 return new MemberList (i_members);
1283 MemberInfo [] s_members = t.FindMembers (
1284 mt, bf & ~BindingFlags.Instance, filter, criteria);
1286 int s_len = s_members.Length;
1287 if (i_len > 0 || s_len > 0)
1288 return new MemberList (i_members, s_members);
1291 return new MemberList (i_members);
1293 return new MemberList (s_members);
1297 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
1302 /// This method is only called from within MemberLookup. It tries to use the member
1303 /// cache if possible and falls back to the normal FindMembers if not. The `used_cache'
1304 /// flag tells the caller whether we used the cache or not. If we used the cache, then
1305 /// our return value will already contain all inherited members and the caller don't need
1306 /// to check base classes and interfaces anymore.
1308 private static MemberInfo [] MemberLookup_FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1309 string name, out bool used_cache)
1314 // If this is a dynamic type, it's always in the `builder_to_declspace' hash table
1315 // and we can ask the DeclSpace for the MemberCache.
1317 if (t is TypeBuilder) {
1318 DeclSpace decl = (DeclSpace) builder_to_declspace [t];
1319 cache = decl.MemberCache;
1322 // If this DeclSpace has a MemberCache, use it.
1325 if (cache != null) {
1327 return cache.FindMembers (
1328 mt, bf, name, FilterWithClosure_delegate, null);
1331 // If there is no MemberCache, we need to use the "normal" FindMembers.
1332 // Note, this is a VERY uncommon route!
1335 Timer.StartTimer (TimerType.FindMembers);
1336 list = decl.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
1337 FilterWithClosure_delegate, name);
1338 Timer.StopTimer (TimerType.FindMembers);
1340 return (MemberInfo []) list;
1344 // We have to take care of arrays specially, because GetType on
1345 // a TypeBuilder array will return a Type, not a TypeBuilder,
1346 // and we can not call FindMembers on this type.
1348 if (t == TypeManager.array_type || t.IsSubclassOf (TypeManager.array_type)) {
1350 return TypeHandle.ArrayType.MemberCache.FindMembers (
1351 mt, bf, name, FilterWithClosure_delegate, null);
1354 if (t is GenericTypeParameterBuilder) {
1355 TypeParameter tparam = (TypeParameter) builder_to_type_param [t];
1358 Timer.StartTimer (TimerType.FindMembers);
1359 list = tparam.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
1360 FilterWithClosure_delegate, name);
1361 Timer.StopTimer (TimerType.FindMembers);
1363 return (MemberInfo []) list;
1366 if (t.IsGenericInstance && (mt == MemberTypes.NestedType)) {
1368 // This happens if we're resolving a class'es base class and interfaces
1369 // in TypeContainer.DefineType(). At this time, the types aren't
1370 // populated yet, so we can't use the cache.
1372 MemberInfo[] info = t.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
1373 FilterWithClosure_delegate, name);
1379 // This call will always succeed. There is exactly one TypeHandle instance per
1380 // type, TypeHandle.GetMemberCache() will, if necessary, create a new one, and return
1381 // the corresponding MemberCache.
1383 cache = TypeHandle.GetMemberCache (t);
1386 return cache.FindMembers (mt, bf, name, FilterWithClosure_delegate, null);
1389 public static bool IsBuiltinType (Type t)
1391 t = TypeToCoreType (t);
1392 if (t == object_type || t == string_type || t == int32_type || t == uint32_type ||
1393 t == int64_type || t == uint64_type || t == float_type || t == double_type ||
1394 t == char_type || t == short_type || t == decimal_type || t == bool_type ||
1395 t == sbyte_type || t == byte_type || t == ushort_type || t == void_type)
1401 public static bool IsBuiltinType (TypeContainer tc)
1403 return IsBuiltinType (tc.TypeBuilder);
1407 // This is like IsBuiltinType, but lacks decimal_type, we should also clean up
1408 // the pieces in the code where we use IsBuiltinType and special case decimal_type.
1410 public static bool IsPrimitiveType (Type t)
1412 return (t == int32_type || t == uint32_type ||
1413 t == int64_type || t == uint64_type || t == float_type || t == double_type ||
1414 t == char_type || t == short_type || t == bool_type ||
1415 t == sbyte_type || t == byte_type || t == ushort_type);
1418 public static bool IsDelegateType (Type t)
1420 if (t.IsGenericInstance)
1421 t = t.GetGenericTypeDefinition ();
1423 if (t.IsSubclassOf (TypeManager.delegate_type))
1429 public static bool IsEnumType (Type t)
1431 if (builder_to_declspace [t] is Enum)
1437 public static bool IsBuiltinOrEnum (Type t)
1439 if (IsBuiltinType (t))
1448 public static bool IsNullType (Type t)
1450 return t == null_type;
1453 public static bool IsAttributeType (Type t)
1455 return t == attribute_type && t.BaseType != null || IsSubclassOf (t, attribute_type);
1458 static Stack unmanaged_enclosing_types = new Stack (4);
1461 // Whether a type is unmanaged. This is used by the unsafe code (25.2)
1463 public static bool IsUnmanagedType (Type t)
1465 // Avoid infloops in the case of: unsafe struct Foo { Foo *x; }
1466 if (unmanaged_enclosing_types.Contains (t))
1469 // builtins that are not unmanaged types
1470 if (t == TypeManager.object_type || t == TypeManager.string_type)
1473 if (IsBuiltinOrEnum (t))
1476 // Someone did the work of checking if the ElementType of t is unmanaged. Let's not repeat it.
1480 // Arrays are disallowed, even if we mark them with [MarshalAs(UnmanagedType.ByValArray, ...)]
1484 if (!IsValueType (t))
1487 for (Type p = t.DeclaringType; p != null; p = p.DeclaringType) {
1488 if (p.IsGenericTypeDefinition)
1492 unmanaged_enclosing_types.Push (t);
1496 if (t is TypeBuilder) {
1497 TypeContainer tc = LookupTypeContainer (t);
1498 if (tc.Fields != null){
1499 foreach (FieldMember f in tc.Fields){
1500 // Avoid using f.FieldBuilder: f.Define () may not yet have been invoked.
1501 if ((f.ModFlags & Modifiers.STATIC) != 0)
1503 if (f.MemberType == null)
1505 if (!IsUnmanagedType (f.MemberType)){
1506 Report.SymbolRelatedToPreviousError (f.Location, CSharpName (t) + "." + f.Name);
1512 FieldInfo [] fields = t.GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
1514 foreach (FieldInfo f in fields){
1515 if (!IsUnmanagedType (f.FieldType)){
1516 Report.SymbolRelatedToPreviousError (f);
1522 unmanaged_enclosing_types.Pop ();
1527 public static bool IsValueType (Type t)
1529 return t.IsGenericParameter || t.IsValueType;
1532 public static bool IsInterfaceType (Type t)
1534 TypeContainer tc = (TypeContainer) builder_to_declspace [t];
1538 return tc.Kind == Kind.Interface;
1541 public static bool IsSubclassOf (Type type, Type base_type)
1543 TypeParameter tparam = LookupTypeParameter (type);
1544 TypeParameter pparam = LookupTypeParameter (base_type);
1546 if ((tparam != null) && (pparam != null)) {
1547 if (tparam == pparam)
1550 return tparam.IsSubclassOf (base_type);
1554 if (IsEqual (type, base_type))
1557 type = type.BaseType;
1558 } while (type != null);
1563 public static bool IsPrivateAccessible (Type type, Type parent)
1565 if (type.Equals (parent))
1568 if ((type is TypeBuilder) && type.IsGenericTypeDefinition && parent.IsGenericInstance) {
1570 // `a' is a generic type definition's TypeBuilder and `b' is a
1571 // generic instance of the same type.
1577 // void Test (Stack<T> stack) { }
1580 // The first argument of `Test' will be the generic instance
1581 // "Stack<!0>" - which is the same type than the "Stack" TypeBuilder.
1584 // We hit this via Closure.Filter() for gen-82.cs.
1586 if (type != parent.GetGenericTypeDefinition ())
1592 if (type.IsGenericInstance && parent.IsGenericInstance) {
1593 if (type.GetGenericTypeDefinition () != parent.GetGenericTypeDefinition ())
1602 public static bool IsFamilyAccessible (Type type, Type parent)
1604 TypeParameter tparam = LookupTypeParameter (type);
1605 TypeParameter pparam = LookupTypeParameter (parent);
1607 if ((tparam != null) && (pparam != null)) {
1608 if (tparam == pparam)
1611 return tparam.IsSubclassOf (parent);
1615 if (IsInstantiationOfSameGenericType (type, parent))
1618 type = type.BaseType;
1619 } while (type != null);
1625 // Checks whether `type' is a subclass or nested child of `base_type'.
1627 public static bool IsNestedFamilyAccessible (Type type, Type base_type)
1630 if (IsFamilyAccessible (type, base_type))
1633 // Handle nested types.
1634 type = type.DeclaringType;
1635 } while (type != null);
1641 // Checks whether `type' is a nested child of `parent'.
1643 public static bool IsNestedChildOf (Type type, Type parent)
1645 if (IsEqual (type, parent))
1648 type = type.DeclaringType;
1649 while (type != null) {
1650 if (IsEqual (type, parent))
1653 type = type.DeclaringType;
1660 // Checks whether `extern_type' is friend of the output assembly
1662 public static bool IsFriendAssembly (Assembly assembly)
1664 if (assembly_internals_vis_attrs.Contains (assembly))
1665 return (bool)(assembly_internals_vis_attrs [assembly]);
1667 object [] attrs = assembly.GetCustomAttributes (internals_visible_attr_type, false);
1668 if (attrs.Length == 0) {
1669 assembly_internals_vis_attrs.Add (assembly, false);
1673 AssemblyName this_name = CodeGen.Assembly.Name;
1674 byte [] this_token = this_name.GetPublicKeyToken ();
1675 bool is_friend = false;
1676 foreach (InternalsVisibleToAttribute attr in attrs) {
1677 if (attr.AssemblyName == null || attr.AssemblyName.Length == 0)
1680 AssemblyName aname = null;
1682 aname = new AssemblyName (attr.AssemblyName);
1683 } catch (FileLoadException) {
1684 } catch (ArgumentException) {
1687 if (aname == null || aname.Name != this_name.Name)
1690 byte [] key_token = aname.GetPublicKeyToken ();
1691 if (key_token != null) {
1692 if (this_token == null) {
1693 // Same name, but key token is null
1694 Error_FriendAccessNameNotMatching (aname.FullName);
1698 if (!CompareKeyTokens (this_token, key_token))
1706 assembly_internals_vis_attrs.Add (assembly, is_friend);
1710 static bool CompareKeyTokens (byte [] token1, byte [] token2)
1712 for (int i = 0; i < token1.Length; i++)
1713 if (token1 [i] != token2 [i])
1719 static void Error_FriendAccessNameNotMatching (string other_name)
1721 Report.Error (281, "Friend access was granted to `" + other_name +
1722 "', but the output assembly is named `" + CodeGen.Assembly.Name.FullName +
1723 "'. Try adding a reference to `" + other_name +
1724 "' or change the output assembly name to match it");
1728 // Do the right thing when returning the element type of an
1729 // array type based on whether we are compiling corlib or not
1731 public static Type GetElementType (Type t)
1733 if (RootContext.StdLib)
1734 return t.GetElementType ();
1736 return TypeToCoreType (t.GetElementType ());
1740 /// This method is not implemented by MS runtime for dynamic types
1742 public static bool HasElementType (Type t)
1744 return t.IsArray || t.IsPointer || t.IsByRef;
1748 /// Gigantic work around for missing features in System.Reflection.Emit follows.
1752 /// Since System.Reflection.Emit can not return MethodBase.GetParameters
1753 /// for anything which is dynamic, and we need this in a number of places,
1754 /// we register this information here, and use it afterwards.
1756 static public void RegisterMethod (MethodBase mb, Parameters ip)
1758 method_params.Add (mb, ip);
1761 static public ParameterData GetParameterData (MethodBase mb)
1763 ParameterData pd = (ParameterData)method_params [mb];
1765 if (mb is MethodBuilder || mb is ConstructorBuilder)
1766 throw new InternalErrorException ("Argument for Method not registered" + mb);
1768 pd = new ReflectionParameters (mb);
1769 method_params.Add (mb, pd);
1774 static public void RegisterOverride (MethodBase override_method, MethodBase base_method)
1776 if (method_overrides.Contains (override_method)) {
1777 if (method_overrides [override_method] != base_method)
1778 throw new InternalErrorException ("Override mismatch: " + override_method);
1781 method_overrides [override_method] = base_method;
1784 static public bool IsOverride (MethodBase m)
1786 return m.IsVirtual &&
1787 (m.Attributes & MethodAttributes.NewSlot) == 0 &&
1788 (m is MethodBuilder || method_overrides.Contains (m));
1792 /// Returns the argument types for an indexer based on its PropertyInfo
1794 /// For dynamic indexers, we use the compiler provided types, for
1795 /// indexers from existing assemblies we load them from GetParameters,
1796 /// and insert them into the cache
1798 static public Type [] GetArgumentTypes (PropertyInfo indexer)
1800 if (indexer_arguments.Contains (indexer))
1801 return (Type []) indexer_arguments [indexer];
1802 else if (indexer is PropertyBuilder)
1803 // If we're a PropertyBuilder and not in the
1804 // `indexer_arguments' hash, then we're a property and
1808 ParameterInfo [] pi = indexer.GetIndexParameters ();
1809 // Property, not an indexer.
1813 Type [] types = new Type [c];
1815 for (int i = 0; i < c; i++)
1816 types [i] = pi [i].ParameterType;
1818 indexer_arguments.Add (indexer, types);
1823 public static void RegisterConstant (FieldInfo fb, IConstant ic)
1825 fields.Add (fb, ic);
1828 public static IConstant GetConstant (FieldInfo fb)
1833 return (IConstant)fields [fb];
1836 static public bool RegisterFieldBase (FieldBuilder fb, FieldBase f)
1838 if (fieldbuilders_to_fields.Contains (fb))
1841 fieldbuilders_to_fields.Add (fb, f);
1846 // The return value can be null; This will be the case for
1847 // auxiliary FieldBuilders created by the compiler that have no
1848 // real field being declared on the source code
1850 static public FieldBase GetField (FieldInfo fb)
1852 if (fb.DeclaringType.IsGenericInstance)
1853 fb = fb.Mono_GetGenericFieldDefinition ();
1855 return (FieldBase) fieldbuilders_to_fields [fb];
1858 static Hashtable events;
1860 static public void RegisterEvent (MyEventBuilder eb, MethodBase add, MethodBase remove)
1863 events = new Hashtable ();
1865 if (!events.Contains (eb)) {
1866 events.Add (eb, new Pair (add, remove));
1870 static public MethodInfo GetAddMethod (EventInfo ei)
1872 if (ei is MyEventBuilder) {
1873 Pair pair = (Pair) events [ei];
1875 return (MethodInfo) pair.First;
1877 return ei.GetAddMethod (true);
1880 static public MethodInfo GetRemoveMethod (EventInfo ei)
1882 if (ei is MyEventBuilder) {
1883 Pair pair = (Pair) events [ei];
1885 return (MethodInfo) pair.Second;
1887 return ei.GetRemoveMethod (true);
1890 static Hashtable priv_fields_events;
1892 static public bool RegisterPrivateFieldOfEvent (EventInfo einfo, FieldBuilder builder)
1894 if (priv_fields_events == null)
1895 priv_fields_events = new Hashtable ();
1897 if (priv_fields_events.Contains (einfo))
1900 priv_fields_events.Add (einfo, builder);
1905 static public MemberInfo GetPrivateFieldOfEvent (EventInfo ei)
1907 if (priv_fields_events == null)
1910 return (MemberInfo) priv_fields_events [ei];
1913 static public bool RegisterIndexer (PropertyBuilder pb, MethodBase get,
1914 MethodBase set, Type[] args)
1916 indexer_arguments.Add (pb, args);
1921 public static bool CheckStructCycles (TypeContainer tc, Hashtable seen)
1923 Hashtable hash = new Hashtable ();
1924 return CheckStructCycles (tc, seen, hash);
1927 public static bool CheckStructCycles (TypeContainer tc, Hashtable seen,
1930 if ((tc.Kind != Kind.Struct) || IsBuiltinType (tc))
1934 // `seen' contains all types we've already visited.
1936 if (seen.Contains (tc))
1938 seen.Add (tc, null);
1940 if (tc.Fields == null)
1943 foreach (FieldMember field in tc.Fields) {
1944 if (field.FieldBuilder == null || field.FieldBuilder.IsStatic)
1947 Type ftype = field.FieldBuilder.FieldType;
1948 TypeContainer ftc = LookupTypeContainer (ftype);
1952 if (hash.Contains (ftc)) {
1953 Report.Error (523, tc.Location,
1954 "Struct member `{0}.{1}' of type `{2}' " +
1955 "causes a cycle in the struct layout",
1956 tc.Name, field.Name, ftc.Name);
1961 // `hash' contains all types in the current path.
1963 hash.Add (tc, null);
1965 bool ok = CheckStructCycles (ftc, seen, hash);
1972 if (!seen.Contains (ftc))
1973 seen.Add (ftc, null);
1980 /// Given an array of interface types, expand and eliminate repeated ocurrences
1981 /// of an interface.
1985 /// This expands in context like: IA; IB : IA; IC : IA, IB; the interface "IC" to
1988 public static Type[] ExpandInterfaces (EmitContext ec, TypeExpr [] base_interfaces)
1990 ArrayList new_ifaces = new ArrayList ();
1992 foreach (TypeExpr iface in base_interfaces){
1993 TypeExpr texpr = iface.ResolveAsTypeTerminal (ec);
1997 Type itype = texpr.ResolveType (ec);
1999 if (new_ifaces.Contains (itype))
2002 new_ifaces.Add (itype);
2004 Type [] implementing = itype.GetInterfaces ();
2006 foreach (Type imp in implementing){
2007 if (!new_ifaces.Contains (imp))
2008 new_ifaces.Add (imp);
2011 Type [] ret = new Type [new_ifaces.Count];
2012 new_ifaces.CopyTo (ret, 0);
2016 public static Type[] ExpandInterfaces (Type [] base_interfaces)
2018 ArrayList new_ifaces = new ArrayList ();
2020 foreach (Type itype in base_interfaces){
2021 if (new_ifaces.Contains (itype))
2024 new_ifaces.Add (itype);
2026 Type [] implementing = itype.GetInterfaces ();
2028 foreach (Type imp in implementing){
2029 if (!new_ifaces.Contains (imp))
2030 new_ifaces.Add (imp);
2033 Type [] ret = new Type [new_ifaces.Count];
2034 new_ifaces.CopyTo (ret, 0);
2038 static PtrHashtable iface_cache = new PtrHashtable ();
2041 /// This function returns the interfaces in the type `t'. Works with
2042 /// both types and TypeBuilders.
2044 public static Type [] GetInterfaces (Type t)
2046 Type [] cached = iface_cache [t] as Type [];
2051 // The reason for catching the Array case is that Reflection.Emit
2052 // will not return a TypeBuilder for Array types of TypeBuilder types,
2053 // but will still throw an exception if we try to call GetInterfaces
2056 // Since the array interfaces are always constant, we return those for
2061 t = TypeManager.array_type;
2063 if ((t is TypeBuilder) || t.IsGenericInstance) {
2064 Type [] base_ifaces;
2066 if (t.BaseType == null)
2067 base_ifaces = NoTypes;
2069 base_ifaces = GetInterfaces (t.BaseType);
2071 if (t.IsGenericInstance)
2072 type_ifaces = t.GetInterfaces ();
2074 type_ifaces = (Type []) builder_to_ifaces [t];
2075 if (type_ifaces == null)
2076 type_ifaces = NoTypes;
2078 int base_count = base_ifaces.Length;
2079 Type [] result = new Type [base_count + type_ifaces.Length];
2080 base_ifaces.CopyTo (result, 0);
2081 type_ifaces.CopyTo (result, base_count);
2083 iface_cache [t] = result;
2085 } else if (t is GenericTypeParameterBuilder){
2086 Type[] type_ifaces = (Type []) builder_to_ifaces [t];
2087 if (type_ifaces == null)
2088 type_ifaces = NoTypes;
2090 iface_cache [t] = type_ifaces;
2093 Type[] ifaces = t.GetInterfaces ();
2094 iface_cache [t] = ifaces;
2100 // gets the interfaces that are declared explicitly on t
2102 public static Type [] GetExplicitInterfaces (TypeBuilder t)
2104 return (Type []) builder_to_ifaces [t];
2108 /// The following is used to check if a given type implements an interface.
2109 /// The cache helps us reduce the expense of hitting Type.GetInterfaces everytime.
2111 public static bool ImplementsInterface (Type t, Type iface)
2116 // FIXME OPTIMIZATION:
2117 // as soon as we hit a non-TypeBuiler in the interface
2118 // chain, we could return, as the `Type.GetInterfaces'
2119 // will return all the interfaces implement by the type
2123 interfaces = GetInterfaces (t);
2125 if (interfaces != null){
2126 foreach (Type i in interfaces){
2133 } while (t != null);
2138 static NumberFormatInfo nf_provider = CultureInfo.CurrentCulture.NumberFormat;
2140 // This is a custom version of Convert.ChangeType() which works
2141 // with the TypeBuilder defined types when compiling corlib.
2142 public static object ChangeType (object value, Type conversionType, out bool error)
2144 IConvertible convert_value = value as IConvertible;
2146 if (convert_value == null){
2152 // We must use Type.Equals() here since `conversionType' is
2153 // the TypeBuilder created version of a system type and not
2154 // the system type itself. You cannot use Type.GetTypeCode()
2155 // on such a type - it'd always return TypeCode.Object.
2159 if (conversionType.Equals (typeof (Boolean)))
2160 return (object)(convert_value.ToBoolean (nf_provider));
2161 else if (conversionType.Equals (typeof (Byte)))
2162 return (object)(convert_value.ToByte (nf_provider));
2163 else if (conversionType.Equals (typeof (Char)))
2164 return (object)(convert_value.ToChar (nf_provider));
2165 else if (conversionType.Equals (typeof (DateTime)))
2166 return (object)(convert_value.ToDateTime (nf_provider));
2167 else if (conversionType.Equals (TypeManager.decimal_type)) // typeof (Decimal)))
2168 return (object)(convert_value.ToDecimal (nf_provider));
2169 else if (conversionType.Equals (typeof (Double)))
2170 return (object)(convert_value.ToDouble (nf_provider));
2171 else if (conversionType.Equals (typeof (Int16)))
2172 return (object)(convert_value.ToInt16 (nf_provider));
2173 else if (conversionType.Equals (typeof (Int32)))
2174 return (object)(convert_value.ToInt32 (nf_provider));
2175 else if (conversionType.Equals (typeof (Int64)))
2176 return (object)(convert_value.ToInt64 (nf_provider));
2177 else if (conversionType.Equals (typeof (SByte)))
2178 return (object)(convert_value.ToSByte (nf_provider));
2179 else if (conversionType.Equals (typeof (Single)))
2180 return (object)(convert_value.ToSingle (nf_provider));
2181 else if (conversionType.Equals (typeof (String)))
2182 return (object)(convert_value.ToString (nf_provider));
2183 else if (conversionType.Equals (typeof (UInt16)))
2184 return (object)(convert_value.ToUInt16 (nf_provider));
2185 else if (conversionType.Equals (typeof (UInt32)))
2186 return (object)(convert_value.ToUInt32 (nf_provider));
2187 else if (conversionType.Equals (typeof (UInt64)))
2188 return (object)(convert_value.ToUInt64 (nf_provider));
2189 else if (conversionType.Equals (typeof (Object)))
2190 return (object)(value);
2200 // This is needed, because enumerations from assemblies
2201 // do not report their underlyingtype, but they report
2204 public static Type EnumToUnderlying (Type t)
2206 if (t == TypeManager.enum_type)
2209 t = t.UnderlyingSystemType;
2210 if (!TypeManager.IsEnumType (t))
2213 if (t is TypeBuilder) {
2214 // slow path needed to compile corlib
2215 if (t == TypeManager.bool_type ||
2216 t == TypeManager.byte_type ||
2217 t == TypeManager.sbyte_type ||
2218 t == TypeManager.char_type ||
2219 t == TypeManager.short_type ||
2220 t == TypeManager.ushort_type ||
2221 t == TypeManager.int32_type ||
2222 t == TypeManager.uint32_type ||
2223 t == TypeManager.int64_type ||
2224 t == TypeManager.uint64_type)
2227 TypeCode tc = Type.GetTypeCode (t);
2230 case TypeCode.Boolean:
2231 return TypeManager.bool_type;
2233 return TypeManager.byte_type;
2234 case TypeCode.SByte:
2235 return TypeManager.sbyte_type;
2237 return TypeManager.char_type;
2238 case TypeCode.Int16:
2239 return TypeManager.short_type;
2240 case TypeCode.UInt16:
2241 return TypeManager.ushort_type;
2242 case TypeCode.Int32:
2243 return TypeManager.int32_type;
2244 case TypeCode.UInt32:
2245 return TypeManager.uint32_type;
2246 case TypeCode.Int64:
2247 return TypeManager.int64_type;
2248 case TypeCode.UInt64:
2249 return TypeManager.uint64_type;
2251 throw new Exception ("Unhandled typecode in enum " + tc + " from " + t.AssemblyQualifiedName);
2255 // When compiling corlib and called with one of the core types, return
2256 // the corresponding typebuilder for that type.
2258 public static Type TypeToCoreType (Type t)
2260 if (RootContext.StdLib || (t is TypeBuilder))
2263 TypeCode tc = Type.GetTypeCode (t);
2266 case TypeCode.Boolean:
2267 return TypeManager.bool_type;
2269 return TypeManager.byte_type;
2270 case TypeCode.SByte:
2271 return TypeManager.sbyte_type;
2273 return TypeManager.char_type;
2274 case TypeCode.Int16:
2275 return TypeManager.short_type;
2276 case TypeCode.UInt16:
2277 return TypeManager.ushort_type;
2278 case TypeCode.Int32:
2279 return TypeManager.int32_type;
2280 case TypeCode.UInt32:
2281 return TypeManager.uint32_type;
2282 case TypeCode.Int64:
2283 return TypeManager.int64_type;
2284 case TypeCode.UInt64:
2285 return TypeManager.uint64_type;
2286 case TypeCode.Single:
2287 return TypeManager.float_type;
2288 case TypeCode.Double:
2289 return TypeManager.double_type;
2290 case TypeCode.String:
2291 return TypeManager.string_type;
2292 case TypeCode.Decimal:
2293 return TypeManager.decimal_type;
2295 if (t == typeof (void))
2296 return TypeManager.void_type;
2297 if (t == typeof (object))
2298 return TypeManager.object_type;
2299 if (t == typeof (System.Type))
2300 return TypeManager.type_type;
2301 if (t == typeof (System.IntPtr))
2302 return TypeManager.intptr_type;
2308 /// Utility function that can be used to probe whether a type
2309 /// is managed or not.
2311 public static bool VerifyUnManaged (Type t, Location loc)
2313 if (IsUnmanagedType (t))
2316 Report.Error (208, loc, "Cannot take the address of, get the size of, or declare a pointer to a managed type `{0}'",
2323 /// Returns the name of the indexer in a given type.
2326 /// The default is not always `Item'. The user can change this behaviour by
2327 /// using the IndexerNameAttribute in the container.
2329 /// For example, the String class indexer is named `Chars' not `Item'
2331 public static string IndexerPropertyName (Type t)
2333 if (t.IsGenericInstance)
2334 t = t.GetGenericTypeDefinition ();
2336 if (t is TypeBuilder) {
2337 TypeContainer tc = t.IsInterface ? LookupInterface (t) : LookupTypeContainer (t);
2338 return tc == null ? TypeContainer.DefaultIndexerName : tc.IndexerName;
2341 System.Attribute attr = System.Attribute.GetCustomAttribute (
2342 t, TypeManager.default_member_type);
2344 DefaultMemberAttribute dma = (DefaultMemberAttribute) attr;
2345 return dma.MemberName;
2348 return TypeContainer.DefaultIndexerName;
2351 static MethodInfo declare_local_method = null;
2353 public static LocalBuilder DeclareLocalPinned (ILGenerator ig, Type t)
2355 if (declare_local_method == null){
2356 declare_local_method = typeof (ILGenerator).GetMethod (
2358 BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
2360 new Type [] { typeof (Type), typeof (bool)},
2362 if (declare_local_method == null){
2363 Report.RuntimeMissingSupport (Location.Null, "pinned local variables");
2364 return ig.DeclareLocal (t);
2367 return (LocalBuilder) declare_local_method.Invoke (ig, new object [] { t, true });
2371 // Returns whether the array of memberinfos contains the given method
2373 public static bool ArrayContainsMethod (MemberInfo [] array, MethodBase new_method)
2375 Type [] new_args = TypeManager.GetParameterData (new_method).Types;
2377 foreach (MethodBase method in array) {
2378 if (method.Name != new_method.Name)
2381 if (method is MethodInfo && new_method is MethodInfo)
2382 if (((MethodInfo) method).ReturnType != ((MethodInfo) new_method).ReturnType)
2386 Type [] old_args = TypeManager.GetParameterData (method).Types;
2387 int old_count = old_args.Length;
2390 if (new_args.Length != old_count)
2393 for (i = 0; i < old_count; i++){
2394 if (old_args [i] != new_args [i])
2407 // We copy methods from `new_members' into `target_list' if the signature
2408 // for the method from in the new list does not exist in the target_list
2410 // The name is assumed to be the same.
2412 public static ArrayList CopyNewMethods (ArrayList target_list, IList new_members)
2414 if (target_list == null){
2415 target_list = new ArrayList ();
2417 foreach (MemberInfo mi in new_members){
2418 if (mi is MethodBase)
2419 target_list.Add (mi);
2424 MemberInfo [] target_array = new MemberInfo [target_list.Count];
2425 target_list.CopyTo (target_array, 0);
2427 foreach (MemberInfo mi in new_members){
2428 MethodBase new_method = (MethodBase) mi;
2430 if (!ArrayContainsMethod (target_array, new_method))
2431 target_list.Add (new_method);
2436 #region MemberLookup implementation
2439 // Whether we allow private members in the result (since FindMembers
2440 // uses NonPublic for both protected and private), we need to distinguish.
2443 static internal bool FilterNone (MemberInfo m, object filter_criteria)
2448 internal class Closure {
2449 internal bool private_ok;
2451 // Who is invoking us and which type is being queried currently.
2452 internal Type invocation_type;
2453 internal Type qualifier_type;
2455 // The assembly that defines the type is that is calling us
2456 internal Assembly invocation_assembly;
2457 internal IList almost_match;
2459 private bool CheckValidFamilyAccess (bool is_static, MemberInfo m)
2461 if (invocation_type == null)
2464 if (is_static && qualifier_type == null)
2465 // It resolved from a simple name, so it should be visible.
2468 // A nested class has access to all the protected members visible to its parent.
2469 if (qualifier_type != null && TypeManager.IsNestedChildOf (invocation_type, qualifier_type))
2472 if (IsNestedFamilyAccessible (invocation_type, m.DeclaringType)) {
2473 // Although a derived class can access protected members of its base class
2474 // it cannot do so through an instance of the base class (CS1540).
2475 // => Ancestry should be: declaring_type ->* invocation_type ->* qualified_type
2477 qualifier_type == null ||
2478 IsInstantiationOfSameGenericType (invocation_type, qualifier_type) ||
2479 !IsFamilyAccessible (invocation_type, qualifier_type) ||
2480 IsNestedChildOf (invocation_type, qualifier_type))
2484 if (almost_match != null)
2485 almost_match.Add (m);
2491 // This filter filters by name + whether it is ok to include private
2492 // members in the search
2494 internal bool Filter (MemberInfo m, object filter_criteria)
2497 // Hack: we know that the filter criteria will always be in the
2498 // `closure' // fields.
2501 if ((filter_criteria != null) && (m.Name != (string) filter_criteria))
2504 if (((qualifier_type == null) || (qualifier_type == invocation_type)) &&
2505 (invocation_type != null) &&
2506 IsPrivateAccessible (m.DeclaringType, invocation_type))
2510 // Ugly: we need to find out the type of `m', and depending
2511 // on this, tell whether we accept or not
2513 if (m is MethodBase){
2514 MethodBase mb = (MethodBase) m;
2515 MethodAttributes ma = mb.Attributes & MethodAttributes.MemberAccessMask;
2517 if (ma == MethodAttributes.Public)
2520 if (ma == MethodAttributes.Private)
2521 return private_ok ||
2522 IsPrivateAccessible (invocation_type, m.DeclaringType) ||
2523 IsNestedChildOf (invocation_type, m.DeclaringType);
2525 if (invocation_assembly == mb.DeclaringType.Assembly ||
2526 TypeManager.IsFriendAssembly (mb.DeclaringType.Assembly)) {
2527 if (ma == MethodAttributes.Assembly || ma == MethodAttributes.FamORAssem)
2530 if (ma == MethodAttributes.Assembly || ma == MethodAttributes.FamANDAssem)
2534 // Family, FamORAssem or FamANDAssem
2535 return CheckValidFamilyAccess (mb.IsStatic, m);
2538 if (m is FieldInfo){
2539 FieldInfo fi = (FieldInfo) m;
2540 FieldAttributes fa = fi.Attributes & FieldAttributes.FieldAccessMask;
2542 if (fa == FieldAttributes.Public)
2545 if (fa == FieldAttributes.Private)
2546 return private_ok ||
2547 IsPrivateAccessible (invocation_type, m.DeclaringType) ||
2548 IsNestedChildOf (invocation_type, m.DeclaringType);
2550 if (invocation_assembly == fi.DeclaringType.Assembly ||
2551 TypeManager.IsFriendAssembly (fi.DeclaringType.Assembly)) {
2552 if (fa == FieldAttributes.Assembly || fa == FieldAttributes.FamORAssem)
2555 if (fa == FieldAttributes.Assembly || fa == FieldAttributes.FamANDAssem)
2559 // Family, FamORAssem or FamANDAssem
2560 return CheckValidFamilyAccess (fi.IsStatic, m);
2564 // EventInfos and PropertyInfos, return true because they lack
2565 // permission information, so we need to check later on the methods.
2571 static Closure closure = new Closure ();
2572 static MemberFilter FilterWithClosure_delegate = new MemberFilter (closure.Filter);
2575 // Looks up a member called `name' in the `queried_type'. This lookup
2576 // is done by code that is contained in the definition for `invocation_type'
2577 // through a qualifier of type `qualifier_type' (or null if there is no qualifier).
2579 // `invocation_type' is used to check whether we're allowed to access the requested
2580 // member wrt its protection level.
2582 // When called from MemberAccess, `qualifier_type' is the type which is used to access
2583 // the requested member (`class B { A a = new A (); a.foo = 5; }'; here invocation_type
2584 // is B and qualifier_type is A). This is used to do the CS1540 check.
2586 // When resolving a SimpleName, `qualifier_type' is null.
2588 // The `qualifier_type' is used for the CS1540 check; it's normally either null or
2589 // the same than `queried_type' - except when we're being called from BaseAccess;
2590 // in this case, `invocation_type' is the current type and `queried_type' the base
2591 // type, so this'd normally trigger a CS1540.
2593 // The binding flags are `bf' and the kind of members being looked up are `mt'
2595 // The return value always includes private members which code in `invocation_type'
2596 // is allowed to access (using the specified `qualifier_type' if given); only use
2597 // BindingFlags.NonPublic to bypass the permission check.
2599 // The 'almost_match' argument is used for reporting error CS1540.
2601 // Returns an array of a single element for everything but Methods/Constructors
2602 // that might return multiple matches.
2604 public static MemberInfo [] MemberLookup (Type invocation_type, Type qualifier_type,
2605 Type queried_type, MemberTypes mt,
2606 BindingFlags original_bf, string name, IList almost_match)
2608 Timer.StartTimer (TimerType.MemberLookup);
2610 MemberInfo[] retval = RealMemberLookup (invocation_type, qualifier_type,
2611 queried_type, mt, original_bf, name, almost_match);
2613 Timer.StopTimer (TimerType.MemberLookup);
2618 static MemberInfo [] RealMemberLookup (Type invocation_type, Type qualifier_type,
2619 Type queried_type, MemberTypes mt,
2620 BindingFlags original_bf, string name, IList almost_match)
2622 BindingFlags bf = original_bf;
2624 ArrayList method_list = null;
2625 Type current_type = queried_type;
2626 bool searching = (original_bf & BindingFlags.DeclaredOnly) == 0;
2627 bool skip_iface_check = true, used_cache = false;
2628 bool always_ok_flag = invocation_type != null && IsNestedChildOf (invocation_type, queried_type);
2630 closure.invocation_type = invocation_type;
2631 closure.invocation_assembly = invocation_type != null ? invocation_type.Assembly : null;
2632 closure.qualifier_type = qualifier_type;
2633 closure.almost_match = almost_match;
2635 // This is from the first time we find a method
2636 // in most cases, we do not actually find a method in the base class
2637 // so we can just ignore it, and save the arraylist allocation
2638 MemberInfo [] first_members_list = null;
2639 bool use_first_members_list = false;
2645 // `NonPublic' is lame, because it includes both protected and
2646 // private methods, so we need to control this behavior by
2647 // explicitly tracking if a private method is ok or not.
2649 // The possible cases are:
2650 // public, private and protected (internal does not come into the
2653 if ((invocation_type != null) &&
2654 ((invocation_type == current_type) ||
2655 IsNestedChildOf (invocation_type, current_type)) ||
2657 bf = original_bf | BindingFlags.NonPublic;
2661 closure.private_ok = (original_bf & BindingFlags.NonPublic) != 0;
2663 Timer.StopTimer (TimerType.MemberLookup);
2665 list = MemberLookup_FindMembers (
2666 current_type, mt, bf, name, out used_cache);
2668 Timer.StartTimer (TimerType.MemberLookup);
2671 // When queried for an interface type, the cache will automatically check all
2672 // inherited members, so we don't need to do this here. However, this only
2673 // works if we already used the cache in the first iteration of this loop.
2675 // If we used the cache in any further iteration, we can still terminate the
2676 // loop since the cache always looks in all base classes.
2682 skip_iface_check = false;
2684 if (current_type == TypeManager.object_type)
2687 current_type = current_type.BaseType;
2690 // This happens with interfaces, they have a null
2691 // basetype. Look members up in the Object class.
2693 if (current_type == null) {
2694 current_type = TypeManager.object_type;
2699 if (list.Length == 0)
2703 // Events and types are returned by both `static' and `instance'
2704 // searches, which means that our above FindMembers will
2705 // return two copies of the same.
2707 if (list.Length == 1 && !(list [0] is MethodBase)){
2712 // Multiple properties: we query those just to find out the indexer
2715 if (list [0] is PropertyInfo)
2719 // We found an event: the cache lookup returns both the event and
2720 // its private field.
2722 if (list [0] is EventInfo) {
2723 if ((list.Length == 2) && (list [1] is FieldInfo))
2724 return new MemberInfo [] { list [0] };
2731 // We found methods, turn the search into "method scan"
2735 if (first_members_list != null) {
2736 if (use_first_members_list) {
2737 method_list = CopyNewMethods (method_list, first_members_list);
2738 use_first_members_list = false;
2741 method_list = CopyNewMethods (method_list, list);
2743 first_members_list = list;
2744 use_first_members_list = true;
2746 mt &= (MemberTypes.Method | MemberTypes.Constructor);
2748 } while (searching);
2750 if (use_first_members_list) {
2751 foreach (MemberInfo mi in first_members_list) {
2752 if (! (mi is MethodBase)) {
2753 method_list = CopyNewMethods (method_list, first_members_list);
2754 return (MemberInfo []) method_list.ToArray (typeof (MemberInfo));
2757 return (MemberInfo []) first_members_list;
2760 if (method_list != null && method_list.Count > 0) {
2761 return (MemberInfo []) method_list.ToArray (typeof (MemberInfo));
2764 // This happens if we already used the cache in the first iteration, in this case
2765 // the cache already looked in all interfaces.
2767 if (skip_iface_check)
2771 // Interfaces do not list members they inherit, so we have to
2774 if (!queried_type.IsInterface)
2777 if (queried_type.IsArray)
2778 queried_type = TypeManager.array_type;
2780 Type [] ifaces = GetInterfaces (queried_type);
2784 foreach (Type itype in ifaces){
2787 x = MemberLookup (null, null, itype, mt, bf, name, null);
2795 // Tests whether external method is really special
2796 public static bool IsSpecialMethod (MethodBase mb)
2798 string name = mb.Name;
2799 if (name.StartsWith ("get_") || name.StartsWith ("set_"))
2800 return mb.DeclaringType.GetProperty (name.Substring (4)) != null;
2802 if (name.StartsWith ("add_"))
2803 return mb.DeclaringType.GetEvent (name.Substring (4)) != null;
2805 if (name.StartsWith ("remove_"))
2806 return mb.DeclaringType.GetEvent (name.Substring (7)) != null;
2808 if (name.StartsWith ("op_")){
2809 foreach (string oname in Unary.oper_names) {
2814 foreach (string oname in Binary.oper_names) {
2827 /// There is exactly one instance of this class per type.
2829 public sealed class TypeHandle : IMemberContainer {
2830 public readonly IMemberContainer BaseType;
2832 readonly int id = ++next_id;
2833 static int next_id = 0;
2835 static TypeHandle ()
2841 /// Lookup a TypeHandle instance for the given type. If the type doesn't have
2842 /// a TypeHandle yet, a new instance of it is created. This static method
2843 /// ensures that we'll only have one TypeHandle instance per type.
2845 private static TypeHandle GetTypeHandle (Type t)
2847 TypeHandle handle = (TypeHandle) type_hash [t];
2851 handle = new TypeHandle (t);
2852 type_hash.Add (t, handle);
2856 public static MemberCache GetMemberCache (Type t)
2858 return GetTypeHandle (t).MemberCache;
2861 public static void CleanUp ()
2866 public static void Reset ()
2868 type_hash = new PtrHashtable ();
2872 /// Returns the TypeHandle for TypeManager.object_type.
2874 public static IMemberContainer ObjectType {
2876 if (object_type != null)
2879 object_type = GetTypeHandle (TypeManager.object_type);
2886 /// Returns the TypeHandle for TypeManager.array_type.
2888 public static IMemberContainer ArrayType {
2890 if (array_type != null)
2893 array_type = GetTypeHandle (TypeManager.array_type);
2899 private static PtrHashtable type_hash;
2901 private static TypeHandle object_type = null;
2902 private static TypeHandle array_type = null;
2905 private string full_name;
2906 private bool is_interface;
2907 private MemberCache member_cache;
2908 private MemberCache base_cache;
2910 private TypeHandle (Type type)
2913 full_name = type.FullName != null ? type.FullName : type.Name;
2914 if (type.BaseType != null) {
2915 base_cache = TypeManager.LookupMemberCache (type.BaseType);
2916 BaseType = base_cache.Container;
2917 } else if (type.IsInterface)
2918 base_cache = TypeManager.LookupBaseInterfacesCache (type);
2919 this.is_interface = type.IsInterface || type.IsGenericParameter;
2920 this.member_cache = new MemberCache (this);
2923 // IMemberContainer methods
2925 public string Name {
2937 public MemberCache BaseCache {
2943 public bool IsInterface {
2945 return is_interface;
2949 public MemberList GetMembers (MemberTypes mt, BindingFlags bf)
2951 MemberInfo [] members;
2952 if (type is GenericTypeParameterBuilder)
2953 return MemberList.Empty;
2954 if (mt == MemberTypes.Event)
2955 members = type.GetEvents (bf | BindingFlags.DeclaredOnly);
2957 members = type.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
2959 Array.Reverse (members);
2961 return new MemberList (members);
2964 // IMemberFinder methods
2966 public MemberList FindMembers (MemberTypes mt, BindingFlags bf, string name,
2967 MemberFilter filter, object criteria)
2969 return new MemberList (member_cache.FindMembers (mt, bf, name, filter, criteria));
2972 public MemberCache MemberCache {
2974 return member_cache;
2978 public override string ToString ()
2980 if (BaseType != null)
2981 return "TypeHandle (" + id + "," + Name + " : " + BaseType + ")";
2983 return "TypeHandle (" + id + "," + Name + ")";