2 // typemanager.cs: C# type manager
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 // Ravi Pratap (ravi@ximian.com)
7 // Licensed under the terms of the GNU GPL
9 // (C) 2001 Ximian, Inc (http://www.ximian.com)
14 // We will eventually remove the SIMPLE_SPEEDUP, and should never change
15 // the behavior of the compilation. This can be removed if we rework
16 // the code to get a list of namespaces available.
18 #define SIMPLE_SPEEDUP
22 using System.Globalization;
23 using System.Collections;
24 using System.Reflection;
25 using System.Reflection.Emit;
27 using System.Text.RegularExpressions;
28 using System.Runtime.CompilerServices;
29 using System.Diagnostics;
31 namespace Mono.CSharp {
33 public class TypeManager {
35 // A list of core types that the compiler requires or uses
37 static public Type object_type;
38 static public Type value_type;
39 static public Type string_type;
40 static public Type int32_type;
41 static public Type uint32_type;
42 static public Type int64_type;
43 static public Type uint64_type;
44 static public Type float_type;
45 static public Type double_type;
46 static public Type char_type;
47 static public Type char_ptr_type;
48 static public Type short_type;
49 static public Type decimal_type;
50 static public Type bool_type;
51 static public Type sbyte_type;
52 static public Type byte_type;
53 static public Type ushort_type;
54 static public Type enum_type;
55 static public Type delegate_type;
56 static public Type multicast_delegate_type;
57 static public Type void_type;
58 static public Type enumeration_type;
59 static public Type array_type;
60 static public Type runtime_handle_type;
61 static public Type icloneable_type;
62 static public Type type_type;
63 static public Type ienumerator_type;
64 static public Type ienumerable_type;
65 static public Type idisposable_type;
66 static public Type default_member_type;
67 static public Type iasyncresult_type;
68 static public Type asynccallback_type;
69 static public Type intptr_type;
70 static public Type monitor_type;
71 static public Type runtime_field_handle_type;
72 static public Type attribute_type;
73 static public Type attribute_usage_type;
74 static public Type dllimport_type;
75 static public Type unverifiable_code_type;
76 static public Type methodimpl_attr_type;
77 static public Type marshal_as_attr_type;
78 static public Type param_array_type;
79 static public Type guid_attr_type;
80 static public Type void_ptr_type;
81 static public Type indexer_name_type;
82 static public Type exception_type;
83 static public Type invalid_operation_exception_type;
84 static public object obsolete_attribute_type;
85 static public object conditional_attribute_type;
86 static public Type in_attribute_type;
89 // An empty array of types
91 static public Type [] NoTypes;
95 // Expressions representing the internal types. Used during declaration
98 static public Expression system_object_expr, system_string_expr;
99 static public Expression system_boolean_expr, system_decimal_expr;
100 static public Expression system_single_expr, system_double_expr;
101 static public Expression system_sbyte_expr, system_byte_expr;
102 static public Expression system_int16_expr, system_uint16_expr;
103 static public Expression system_int32_expr, system_uint32_expr;
104 static public Expression system_int64_expr, system_uint64_expr;
105 static public Expression system_char_expr, system_void_expr;
106 static public Expression system_asynccallback_expr;
107 static public Expression system_iasyncresult_expr;
110 // This is only used when compiling corlib
112 static public Type system_int32_type;
113 static public Type system_array_type;
114 static public Type system_type_type;
115 static public Type system_assemblybuilder_type;
116 static public MethodInfo system_int_array_get_length;
117 static public MethodInfo system_int_array_get_rank;
118 static public MethodInfo system_object_array_clone;
119 static public MethodInfo system_int_array_get_length_int;
120 static public MethodInfo system_int_array_get_lower_bound_int;
121 static public MethodInfo system_int_array_get_upper_bound_int;
122 static public MethodInfo system_void_array_copyto_array_int;
126 // Internal, not really used outside
128 static Type runtime_helpers_type;
131 // These methods are called by code generated by the compiler
133 static public MethodInfo string_concat_string_string;
134 static public MethodInfo string_concat_string_string_string;
135 static public MethodInfo string_concat_string_string_string_string;
136 static public MethodInfo string_concat_object_object;
137 static public MethodInfo string_isinterneted_string;
138 static public MethodInfo system_type_get_type_from_handle;
139 static public MethodInfo object_getcurrent_void;
140 static public MethodInfo bool_movenext_void;
141 static public MethodInfo ienumerable_getenumerator_void;
142 static public MethodInfo void_reset_void;
143 static public MethodInfo void_dispose_void;
144 static public MethodInfo void_monitor_enter_object;
145 static public MethodInfo void_monitor_exit_object;
146 static public MethodInfo void_initializearray_array_fieldhandle;
147 static public MethodInfo int_getlength_int;
148 static public MethodInfo delegate_combine_delegate_delegate;
149 static public MethodInfo delegate_remove_delegate_delegate;
150 static public MethodInfo int_get_offset_to_string_data;
151 static public MethodInfo int_array_get_length;
152 static public MethodInfo int_array_get_rank;
153 static public MethodInfo object_array_clone;
154 static public MethodInfo int_array_get_length_int;
155 static public MethodInfo int_array_get_lower_bound_int;
156 static public MethodInfo int_array_get_upper_bound_int;
157 static public MethodInfo void_array_copyto_array_int;
160 // The attribute constructors.
162 static public ConstructorInfo object_ctor;
163 static public ConstructorInfo cons_param_array_attribute;
164 static public ConstructorInfo void_decimal_ctor_five_args;
165 static public ConstructorInfo unverifiable_code_ctor;
166 static public ConstructorInfo invalid_operation_ctor;
169 // Holds the Array of Assemblies that have been loaded
170 // (either because it is the default or the user used the
171 // -r command line option)
173 static Assembly [] assemblies;
176 // Keeps a list of module builders. We used this to do lookups
177 // on the modulebuilder using GetType -- needed for arrays
179 static ModuleBuilder [] modules;
182 // This is the type_cache from the assemblies to avoid
183 // hitting System.Reflection on every lookup.
185 static Hashtable types;
188 // This is used to hotld the corresponding TypeContainer objects
189 // since we need this in FindMembers
191 static Hashtable typecontainers;
194 // Keeps track of those types that are defined by the
197 static ArrayList user_types;
199 static PtrHashtable builder_to_declspace;
202 // Tracks the interfaces implemented by typebuilders. We only
203 // enter those who do implement or or more interfaces
205 static PtrHashtable builder_to_ifaces;
208 // Maps MethodBase.RuntimeTypeHandle to a Type array that contains
209 // the arguments to the method
211 static Hashtable method_arguments;
214 // Maps PropertyBuilder to a Type array that contains
215 // the arguments to the indexer
217 static Hashtable indexer_arguments;
220 // Maybe `method_arguments' should be replaced and only
221 // method_internal_params should be kept?
223 static Hashtable method_internal_params;
226 // Keeps track of attribute types
229 static Hashtable builder_to_attr;
232 // Keeps track of methods
235 static Hashtable builder_to_method;
243 /// A filter for Findmembers that uses the Signature object to
246 static bool SignatureFilter (MemberInfo mi, object criteria)
248 Signature sig = (Signature) criteria;
250 if (!(mi is MethodBase))
253 if (mi.Name != sig.name)
256 int count = sig.args.Length;
258 if (mi is MethodBuilder || mi is ConstructorBuilder){
259 Type [] candidate_args = GetArgumentTypes ((MethodBase) mi);
261 if (candidate_args.Length != count)
264 for (int i = 0; i < count; i++)
265 if (candidate_args [i] != sig.args [i])
270 ParameterInfo [] pars = ((MethodBase) mi).GetParameters ();
272 if (pars.Length != count)
275 for (int i = 0; i < count; i++)
276 if (pars [i].ParameterType != sig.args [i])
282 // A delegate that points to the filter above.
283 static MemberFilter signature_filter;
286 // These are expressions that represent some of the internal data types, used
289 static void InitExpressionTypes ()
291 system_object_expr = new TypeLookupExpression ("System.Object");
292 system_string_expr = new TypeLookupExpression ("System.String");
293 system_boolean_expr = new TypeLookupExpression ("System.Boolean");
294 system_decimal_expr = new TypeLookupExpression ("System.Decimal");
295 system_single_expr = new TypeLookupExpression ("System.Single");
296 system_double_expr = new TypeLookupExpression ("System.Double");
297 system_sbyte_expr = new TypeLookupExpression ("System.SByte");
298 system_byte_expr = new TypeLookupExpression ("System.Byte");
299 system_int16_expr = new TypeLookupExpression ("System.Int16");
300 system_uint16_expr = new TypeLookupExpression ("System.UInt16");
301 system_int32_expr = new TypeLookupExpression ("System.Int32");
302 system_uint32_expr = new TypeLookupExpression ("System.UInt32");
303 system_int64_expr = new TypeLookupExpression ("System.Int64");
304 system_uint64_expr = new TypeLookupExpression ("System.UInt64");
305 system_char_expr = new TypeLookupExpression ("System.Char");
306 system_void_expr = new TypeLookupExpression ("System.Void");
307 system_asynccallback_expr = new TypeLookupExpression ("System.AsyncCallback");
308 system_iasyncresult_expr = new TypeLookupExpression ("System.IAsyncResult");
311 static TypeManager ()
313 assemblies = new Assembly [0];
315 user_types = new ArrayList ();
317 types = new Hashtable ();
318 typecontainers = new Hashtable ();
320 builder_to_declspace = new PtrHashtable ();
321 builder_to_attr = new PtrHashtable ();
322 builder_to_method = new PtrHashtable ();
323 method_arguments = new PtrHashtable ();
324 method_internal_params = new PtrHashtable ();
325 indexer_arguments = new PtrHashtable ();
326 builder_to_ifaces = new PtrHashtable ();
328 NoTypes = new Type [0];
330 signature_filter = new MemberFilter (SignatureFilter);
331 InitExpressionTypes ();
334 public static void HandleDuplicate (string name, Type t)
336 Type prev = (Type) types [name];
337 TypeContainer tc = builder_to_declspace [prev] as TypeContainer;
341 // This probably never happens, as we catch this before
343 Report.Error (-17, "The type `" + name + "' has already been defined.");
348 tc = builder_to_declspace [t] as TypeContainer;
351 1595, "The type `" + name + "' is defined in an existing assembly;"+
352 " Using the new definition from: " + tc.Location);
355 1595, "The type `" + name + "' is defined in an existing assembly;");
358 Report.Warning (1595, "Previously defined in: " + prev.Assembly.FullName);
364 public static void AddUserType (string name, TypeBuilder t, Type [] ifaces)
369 HandleDuplicate (name, t);
374 builder_to_ifaces [t] = ifaces;
378 // This entry point is used by types that we define under the covers
380 public static void RegisterBuilder (TypeBuilder tb, Type [] ifaces)
383 builder_to_ifaces [tb] = ifaces;
386 public static void AddUserType (string name, TypeBuilder t, TypeContainer tc, Type [] ifaces)
388 builder_to_declspace.Add (t, tc);
389 typecontainers.Add (name, tc);
390 AddUserType (name, t, ifaces);
393 public static void AddDelegateType (string name, TypeBuilder t, Delegate del)
398 HandleDuplicate (name, t);
401 builder_to_declspace.Add (t, del);
404 public static void AddEnumType (string name, TypeBuilder t, Enum en)
409 HandleDuplicate (name, t);
411 builder_to_declspace.Add (t, en);
414 public static void AddUserInterface (string name, TypeBuilder t, Interface i, Type [] ifaces)
416 AddUserType (name, t, ifaces);
417 builder_to_declspace.Add (t, i);
420 public static void AddMethod (MethodBuilder builder, MethodData method)
422 builder_to_method.Add (builder, method);
425 public static void RegisterAttrType (Type t, TypeContainer tc)
427 builder_to_attr.Add (t, tc);
431 /// Returns the DeclSpace whose Type is `t' or null if there is no
432 /// DeclSpace for `t' (ie, the Type comes from a library)
434 public static DeclSpace LookupDeclSpace (Type t)
436 return builder_to_declspace [t] as DeclSpace;
440 /// Returns the TypeContainer whose Type is `t' or null if there is no
441 /// TypeContainer for `t' (ie, the Type comes from a library)
443 public static TypeContainer LookupTypeContainer (Type t)
445 return builder_to_declspace [t] as TypeContainer;
448 public static IMemberContainer LookupMemberContainer (Type t)
450 if (t is TypeBuilder) {
451 IMemberContainer container = builder_to_declspace [t] as IMemberContainer;
452 if (container != null)
456 return TypeHandle.GetTypeHandle (t);
459 public static Interface LookupInterface (Type t)
461 return builder_to_declspace [t] as Interface;
464 public static Delegate LookupDelegate (Type t)
466 return builder_to_declspace [t] as Delegate;
469 public static Enum LookupEnum (Type t)
471 return builder_to_declspace [t] as Enum;
474 public static TypeContainer LookupAttr (Type t)
476 return (TypeContainer) builder_to_attr [t];
480 /// Registers an assembly to load types from.
482 public static void AddAssembly (Assembly a)
484 int top = assemblies.Length;
485 Assembly [] n = new Assembly [top + 1];
487 assemblies.CopyTo (n, 0);
494 /// Registers a module builder to lookup types from
496 public static void AddModule (ModuleBuilder mb)
498 int top = modules != null ? modules.Length : 0;
499 ModuleBuilder [] n = new ModuleBuilder [top + 1];
502 modules.CopyTo (n, 0);
507 static Hashtable references = new Hashtable ();
510 // Gets the reference to T version of the Type (T&)
512 public static Type GetReferenceType (Type t)
514 string tname = t.FullName + "&";
516 Type ret = t.Assembly.GetType (tname);
519 // If the type comes from the assembly we are building
520 // We need the Hashtable, because .NET 1.1 will return different instance types
521 // every time we call ModuleBuilder.GetType.
524 if (references [t] == null)
525 references [t] = CodeGen.ModuleBuilder.GetType (tname);
526 ret = (Type) references [t];
532 static Hashtable pointers = new Hashtable ();
535 // Gets the pointer to T version of the Type (T*)
537 public static Type GetPointerType (Type t)
539 string tname = t.FullName + "*";
541 Type ret = t.Assembly.GetType (tname);
544 // If the type comes from the assembly we are building
545 // We need the Hashtable, because .NET 1.1 will return different instance types
546 // every time we call ModuleBuilder.GetType.
549 if (pointers [t] == null)
550 pointers [t] = CodeGen.ModuleBuilder.GetType (tname);
552 ret = (Type) pointers [t];
559 // Low-level lookup, cache-less
561 static Type LookupTypeReflection (string name)
565 foreach (Assembly a in assemblies){
566 t = a.GetType (name);
570 TypeAttributes ta = t.Attributes & TypeAttributes.VisibilityMask;
571 if (ta == TypeAttributes.NotPublic ||
572 ta == TypeAttributes.NestedPrivate ||
573 ta == TypeAttributes.NestedAssembly ||
574 ta == TypeAttributes.NestedFamANDAssem)
579 foreach (ModuleBuilder mb in modules) {
580 t = mb.GetType (name);
588 static Hashtable negative_hits = new Hashtable ();
591 // This function is used when you want to avoid the lookups, and want to go
592 // directly to the source. This will use the cache.
594 // Notice that bypassing the cache is bad, because on Microsoft.NET runtime
595 // GetType ("DynamicType[]") != GetType ("DynamicType[]"), and there is no
596 // way to test things other than doing a fullname compare
598 public static Type LookupTypeDirect (string name)
600 Type t = (Type) types [name];
604 t = LookupTypeReflection (name);
613 /// Returns the Type associated with @name, takes care of the fact that
614 /// reflection expects nested types to be separated from the main type
615 /// with a "+" instead of a "."
617 public static Type LookupType (string name)
622 // First lookup in user defined and cached values
625 t = (Type) types [name];
629 // Two thirds of the failures are caught here.
630 if (negative_hits.Contains (name))
633 string [] elements = name.Split ('.');
634 int count = elements.Length;
636 for (int n = 1; n <= count; n++){
637 string top_level_type = String.Join (".", elements, 0, n);
639 // One third of the failures are caught here.
640 if (negative_hits.Contains (top_level_type))
643 t = (Type) types [top_level_type];
645 t = LookupTypeReflection (top_level_type);
647 negative_hits [top_level_type] = true;
658 // We know that System.Object does not have children, and since its the parent of
659 // all the objects, it always gets probbed for inner classes.
661 if (top_level_type == "System.Object")
664 string newt = top_level_type + "+" + String.Join ("+", elements, n, count - n);
665 //Console.WriteLine ("Looking up: " + newt + " " + name);
666 t = LookupTypeReflection (newt);
668 negative_hits [name] = true;
673 negative_hits [name] = true;
678 /// Computes the namespaces that we import from the assemblies we reference.
680 public static void ComputeNamespaces ()
682 MethodInfo assembly_get_namespaces = typeof (Assembly).GetMethod ("GetNamespaces");
685 // First add the assembly namespaces
687 if (assembly_get_namespaces != null){
688 int count = assemblies.Length;
691 for (int i = 0; i < count; i++){
692 Assembly a = assemblies [i];
693 string [] namespaces = (string []) assembly_get_namespaces.Invoke (a, null);
694 foreach (string ns in namespaces){
697 Namespace.LookupNamespace (ns, true);
701 foreach (Assembly a in assemblies){
702 foreach (Type t in a.GetTypes ()){
703 string ns = t.Namespace;
705 // t.Namespace returns null for <PrivateImplDetails>
706 if (ns == ""|| ns == null)
708 Namespace.LookupNamespace (ns, true);
714 public static bool NamespaceClash (string name, Location loc)
716 if (Namespace.LookupNamespace (name, false) == null)
719 Report.Error (519, loc, String.Format ("`{0}' clashes with a predefined namespace", name));
724 /// Returns the C# name of a type if possible, or the full type name otherwise
726 static public string CSharpName (Type t)
728 return Regex.Replace (t.FullName,
730 @"(Int32|UInt32|Int16|UInt16|Int64|UInt64|" +
731 @"Single|Double|Char|Decimal|Byte|SByte|Object|" +
732 @"Boolean|String|Void)" +
734 new MatchEvaluator (CSharpNameMatch));
737 static String CSharpNameMatch (Match match)
739 string s = match.Groups [1].Captures [0].Value;
741 Replace ("int32", "int").
742 Replace ("uint32", "uint").
743 Replace ("int16", "short").
744 Replace ("uint16", "ushort").
745 Replace ("int64", "long").
746 Replace ("uint64", "ulong").
747 Replace ("single", "float").
748 Replace ("boolean", "bool")
749 + match.Groups [2].Captures [0].Value;
753 /// Returns the signature of the method
755 static public string CSharpSignature (MethodBase mb)
760 // FIXME: We should really have a single function to do
761 // everything instead of the following 5 line pattern
763 ParameterData iparams = LookupParametersByBuilder (mb);
765 if (iparams == null){
766 ParameterInfo [] pi = mb.GetParameters ();
767 iparams = new ReflectionParameters (pi);
770 for (int i = 0; i < iparams.Count; i++) {
774 sig += iparams.ParameterDesc(i);
778 return mb.DeclaringType.Name + "." + mb.Name + sig;
782 /// Looks up a type, and aborts if it is not found. This is used
783 /// by types required by the compiler
785 static Type CoreLookupType (string name)
787 Type t = LookupTypeDirect (name);
790 Report.Error (518, "The predefined type `" + name + "' is not defined or imported");
791 Environment.Exit (0);
798 /// Returns the MethodInfo for a method named `name' defined
799 /// in type `t' which takes arguments of types `args'
801 static MethodInfo GetMethod (Type t, string name, Type [] args, bool report_errors)
809 list = FindMembers (t, MemberTypes.Method, instance_and_static | BindingFlags.Public,
810 signature_filter, sig);
811 if (list.Count == 0) {
813 Report.Error (-19, "Can not find the core function `" + name + "'");
817 MethodInfo mi = list [0] as MethodInfo;
820 Report.Error (-19, "Can not find the core function `" + name + "'");
827 static MethodInfo GetMethod (Type t, string name, Type [] args)
829 return GetMethod (t, name, args, true);
834 /// Returns the ConstructorInfo for "args"
836 static ConstructorInfo GetConstructor (Type t, Type [] args)
844 list = FindMembers (t, MemberTypes.Constructor,
845 instance_and_static | BindingFlags.Public | BindingFlags.DeclaredOnly,
846 signature_filter, sig);
847 if (list.Count == 0){
848 Report.Error (-19, "Can not find the core constructor for type `" + t.Name + "'");
852 ConstructorInfo ci = list [0] as ConstructorInfo;
854 Report.Error (-19, "Can not find the core constructor for type `" + t.Name + "'");
861 public static void InitEnumUnderlyingTypes ()
864 int32_type = CoreLookupType ("System.Int32");
865 int64_type = CoreLookupType ("System.Int64");
866 uint32_type = CoreLookupType ("System.UInt32");
867 uint64_type = CoreLookupType ("System.UInt64");
868 byte_type = CoreLookupType ("System.Byte");
869 sbyte_type = CoreLookupType ("System.SByte");
870 short_type = CoreLookupType ("System.Int16");
871 ushort_type = CoreLookupType ("System.UInt16");
875 /// The types have to be initialized after the initial
876 /// population of the type has happened (for example, to
877 /// bootstrap the corlib.dll
879 public static void InitCoreTypes ()
881 object_type = CoreLookupType ("System.Object");
882 value_type = CoreLookupType ("System.ValueType");
884 InitEnumUnderlyingTypes ();
886 char_type = CoreLookupType ("System.Char");
887 string_type = CoreLookupType ("System.String");
888 float_type = CoreLookupType ("System.Single");
889 double_type = CoreLookupType ("System.Double");
890 char_ptr_type = CoreLookupType ("System.Char*");
891 decimal_type = CoreLookupType ("System.Decimal");
892 bool_type = CoreLookupType ("System.Boolean");
893 enum_type = CoreLookupType ("System.Enum");
895 multicast_delegate_type = CoreLookupType ("System.MulticastDelegate");
896 delegate_type = CoreLookupType ("System.Delegate");
898 array_type = CoreLookupType ("System.Array");
899 void_type = CoreLookupType ("System.Void");
900 type_type = CoreLookupType ("System.Type");
902 runtime_field_handle_type = CoreLookupType ("System.RuntimeFieldHandle");
903 runtime_helpers_type = CoreLookupType ("System.Runtime.CompilerServices.RuntimeHelpers");
904 default_member_type = CoreLookupType ("System.Reflection.DefaultMemberAttribute");
905 runtime_handle_type = CoreLookupType ("System.RuntimeTypeHandle");
906 asynccallback_type = CoreLookupType ("System.AsyncCallback");
907 iasyncresult_type = CoreLookupType ("System.IAsyncResult");
908 ienumerator_type = CoreLookupType ("System.Collections.IEnumerator");
909 ienumerable_type = CoreLookupType ("System.Collections.IEnumerable");
910 idisposable_type = CoreLookupType ("System.IDisposable");
911 icloneable_type = CoreLookupType ("System.ICloneable");
912 monitor_type = CoreLookupType ("System.Threading.Monitor");
913 intptr_type = CoreLookupType ("System.IntPtr");
915 attribute_type = CoreLookupType ("System.Attribute");
916 attribute_usage_type = CoreLookupType ("System.AttributeUsageAttribute");
917 dllimport_type = CoreLookupType ("System.Runtime.InteropServices.DllImportAttribute");
918 methodimpl_attr_type = CoreLookupType ("System.Runtime.CompilerServices.MethodImplAttribute");
919 marshal_as_attr_type = CoreLookupType ("System.Runtime.InteropServices.MarshalAsAttribute");
920 param_array_type = CoreLookupType ("System.ParamArrayAttribute");
921 in_attribute_type = CoreLookupType ("System.Runtime.InteropServices.InAttribute");
924 // Sigh. Remove this before the release. Wonder what versions of Mono
925 // people are running.
927 guid_attr_type = LookupType ("System.Runtime.InteropServices.GuidAttribute");
929 unverifiable_code_type= CoreLookupType ("System.Security.UnverifiableCodeAttribute");
931 void_ptr_type = CoreLookupType ("System.Void*");
933 indexer_name_type = CoreLookupType ("System.Runtime.CompilerServices.IndexerNameAttribute");
935 exception_type = CoreLookupType ("System.Exception");
936 invalid_operation_exception_type = CoreLookupType ("System.InvalidOperationException");
941 obsolete_attribute_type = CoreLookupType ("System.ObsoleteAttribute");
942 conditional_attribute_type = CoreLookupType ("System.Diagnostics.ConditionalAttribute");
945 // When compiling corlib, store the "real" types here.
947 if (!RootContext.StdLib) {
948 system_int32_type = typeof (System.Int32);
949 system_array_type = typeof (System.Array);
950 system_type_type = typeof (System.Type);
951 system_assemblybuilder_type = typeof (System.Reflection.Emit.AssemblyBuilder);
953 Type [] void_arg = { };
954 system_int_array_get_length = GetMethod (
955 system_array_type, "get_Length", void_arg);
956 system_int_array_get_rank = GetMethod (
957 system_array_type, "get_Rank", void_arg);
958 system_object_array_clone = GetMethod (
959 system_array_type, "Clone", void_arg);
961 Type [] system_int_arg = { system_int32_type };
962 system_int_array_get_length_int = GetMethod (
963 system_array_type, "GetLength", system_int_arg);
964 system_int_array_get_upper_bound_int = GetMethod (
965 system_array_type, "GetUpperBound", system_int_arg);
966 system_int_array_get_lower_bound_int = GetMethod (
967 system_array_type, "GetLowerBound", system_int_arg);
969 Type [] system_array_int_arg = { system_array_type, system_int32_type };
970 system_void_array_copyto_array_int = GetMethod (
971 system_array_type, "CopyTo", system_array_int_arg);
973 Type [] system_3_type_arg = {
974 system_type_type, system_type_type, system_type_type };
975 Type [] system_4_type_arg = {
976 system_type_type, system_type_type, system_type_type, system_type_type };
978 MethodInfo set_corlib_type_builders = GetMethod (
979 system_assemblybuilder_type, "SetCorlibTypeBuilders",
980 system_4_type_arg, false);
982 if (set_corlib_type_builders != null) {
983 object[] args = new object [4];
984 args [0] = object_type;
985 args [1] = value_type;
986 args [2] = enum_type;
987 args [3] = void_type;
989 set_corlib_type_builders.Invoke (CodeGen.AssemblyBuilder, args);
991 // Compatibility for an older version of the class libs.
992 set_corlib_type_builders = GetMethod (
993 system_assemblybuilder_type, "SetCorlibTypeBuilders",
994 system_3_type_arg, true);
996 if (set_corlib_type_builders == null) {
997 Report.Error (-26, "Corlib compilation is not supported in Microsoft.NET due to bugs in it");
1001 object[] args = new object [3];
1002 args [0] = object_type;
1003 args [1] = value_type;
1004 args [2] = enum_type;
1006 set_corlib_type_builders.Invoke (CodeGen.AssemblyBuilder, args);
1012 // The helper methods that are used by the compiler
1014 public static void InitCodeHelpers ()
1017 // Now load the default methods that we use.
1019 Type [] string_string = { string_type, string_type };
1020 string_concat_string_string = GetMethod (
1021 string_type, "Concat", string_string);
1022 Type [] string_string_string = { string_type, string_type, string_type };
1023 string_concat_string_string_string = GetMethod (
1024 string_type, "Concat", string_string_string);
1025 Type [] string_string_string_string = { string_type, string_type, string_type, string_type };
1026 string_concat_string_string_string_string = GetMethod (
1027 string_type, "Concat", string_string_string_string);
1029 Type [] object_object = { object_type, object_type };
1030 string_concat_object_object = GetMethod (
1031 string_type, "Concat", object_object);
1033 Type [] string_ = { string_type };
1034 string_isinterneted_string = GetMethod (
1035 string_type, "IsInterned", string_);
1037 Type [] runtime_type_handle = { runtime_handle_type };
1038 system_type_get_type_from_handle = GetMethod (
1039 type_type, "GetTypeFromHandle", runtime_type_handle);
1041 Type [] delegate_delegate = { delegate_type, delegate_type };
1042 delegate_combine_delegate_delegate = GetMethod (
1043 delegate_type, "Combine", delegate_delegate);
1045 delegate_remove_delegate_delegate = GetMethod (
1046 delegate_type, "Remove", delegate_delegate);
1051 Type [] void_arg = { };
1052 object_getcurrent_void = GetMethod (
1053 ienumerator_type, "get_Current", void_arg);
1054 bool_movenext_void = GetMethod (
1055 ienumerator_type, "MoveNext", void_arg);
1056 void_reset_void = GetMethod (
1057 ienumerator_type, "Reset", void_arg);
1058 void_dispose_void = GetMethod (
1059 idisposable_type, "Dispose", void_arg);
1060 int_get_offset_to_string_data = GetMethod (
1061 runtime_helpers_type, "get_OffsetToStringData", void_arg);
1062 int_array_get_length = GetMethod (
1063 array_type, "get_Length", void_arg);
1064 int_array_get_rank = GetMethod (
1065 array_type, "get_Rank", void_arg);
1066 ienumerable_getenumerator_void = GetMethod (
1067 ienumerable_type, "GetEnumerator", void_arg);
1072 Type [] int_arg = { int32_type };
1073 int_array_get_length_int = GetMethod (
1074 array_type, "GetLength", int_arg);
1075 int_array_get_upper_bound_int = GetMethod (
1076 array_type, "GetUpperBound", int_arg);
1077 int_array_get_lower_bound_int = GetMethod (
1078 array_type, "GetLowerBound", int_arg);
1081 // System.Array methods
1083 object_array_clone = GetMethod (
1084 array_type, "Clone", void_arg);
1085 Type [] array_int_arg = { array_type, int32_type };
1086 void_array_copyto_array_int = GetMethod (
1087 array_type, "CopyTo", array_int_arg);
1092 Type [] object_arg = { object_type };
1093 void_monitor_enter_object = GetMethod (
1094 monitor_type, "Enter", object_arg);
1095 void_monitor_exit_object = GetMethod (
1096 monitor_type, "Exit", object_arg);
1098 Type [] array_field_handle_arg = { array_type, runtime_field_handle_type };
1100 void_initializearray_array_fieldhandle = GetMethod (
1101 runtime_helpers_type, "InitializeArray", array_field_handle_arg);
1106 int_getlength_int = GetMethod (
1107 array_type, "GetLength", int_arg);
1110 // Decimal constructors
1112 Type [] dec_arg = { int32_type, int32_type, int32_type, bool_type, byte_type };
1113 void_decimal_ctor_five_args = GetConstructor (
1114 decimal_type, dec_arg);
1119 cons_param_array_attribute = GetConstructor (
1120 param_array_type, void_arg);
1122 unverifiable_code_ctor = GetConstructor (
1123 unverifiable_code_type, void_arg);
1126 // InvalidOperationException
1128 invalid_operation_ctor = GetConstructor (
1129 invalid_operation_exception_type, void_arg);
1133 object_ctor = GetConstructor (object_type, void_arg);
1137 const BindingFlags instance_and_static = BindingFlags.Static | BindingFlags.Instance;
1139 static Hashtable type_hash = new Hashtable ();
1142 /// This is the "old", non-cache based FindMembers() function. We cannot use
1143 /// the cache here because there is no member name argument.
1145 public static MemberList FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1146 MemberFilter filter, object criteria)
1148 DeclSpace decl = (DeclSpace) builder_to_declspace [t];
1151 // `builder_to_declspace' contains all dynamic types.
1155 Timer.StartTimer (TimerType.FindMembers);
1156 list = decl.FindMembers (mt, bf, filter, criteria);
1157 Timer.StopTimer (TimerType.FindMembers);
1162 // We have to take care of arrays specially, because GetType on
1163 // a TypeBuilder array will return a Type, not a TypeBuilder,
1164 // and we can not call FindMembers on this type.
1166 if (t.IsSubclassOf (TypeManager.array_type))
1167 return new MemberList (TypeManager.array_type.FindMembers (mt, bf, filter, criteria));
1170 // Since FindMembers will not lookup both static and instance
1171 // members, we emulate this behaviour here.
1173 if ((bf & instance_and_static) == instance_and_static){
1174 MemberInfo [] i_members = t.FindMembers (
1175 mt, bf & ~BindingFlags.Static, filter, criteria);
1177 int i_len = i_members.Length;
1179 MemberInfo one = i_members [0];
1182 // If any of these are present, we are done!
1184 if ((one is Type) || (one is EventInfo) || (one is FieldInfo))
1185 return new MemberList (i_members);
1188 MemberInfo [] s_members = t.FindMembers (
1189 mt, bf & ~BindingFlags.Instance, filter, criteria);
1191 int s_len = s_members.Length;
1192 if (i_len > 0 || s_len > 0)
1193 return new MemberList (i_members, s_members);
1196 return new MemberList (i_members);
1198 return new MemberList (s_members);
1202 return new MemberList (t.FindMembers (mt, bf, filter, criteria));
1207 /// This method is only called from within MemberLookup. It tries to use the member
1208 /// cache if possible and falls back to the normal FindMembers if not. The `used_cache'
1209 /// flag tells the caller whether we used the cache or not. If we used the cache, then
1210 /// our return value will already contain all inherited members and the caller don't need
1211 /// to check base classes and interfaces anymore.
1213 private static MemberList MemberLookup_FindMembers (Type t, MemberTypes mt, BindingFlags bf,
1214 string name, out bool used_cache)
1216 bool not_loaded_corlib = (t.Assembly == CodeGen.AssemblyBuilder);
1219 // We have to take care of arrays specially, because GetType on
1220 // a TypeBuilder array will return a Type, not a TypeBuilder,
1221 // and we can not call FindMembers on this type.
1223 if (t == TypeManager.array_type || t.IsSubclassOf (TypeManager.array_type)) {
1225 return TypeHandle.ArrayType.MemberCache.FindMembers (
1226 mt, bf, name, FilterWithClosure_delegate, null);
1230 // If this is a dynamic type, it's always in the `builder_to_declspace' hash table
1231 // and we can ask the DeclSpace for the MemberCache.
1233 if (t is TypeBuilder) {
1234 DeclSpace decl = (DeclSpace) builder_to_declspace [t];
1235 MemberCache cache = decl.MemberCache;
1238 // If this DeclSpace has a MemberCache, use it.
1241 if (cache != null) {
1243 return cache.FindMembers (
1244 mt, bf, name, FilterWithClosure_delegate, null);
1247 // If there is no MemberCache, we need to use the "normal" FindMembers.
1250 Timer.StartTimer (TimerType.FindMembers);
1251 list = decl.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
1252 FilterWithClosure_delegate, name);
1253 Timer.StopTimer (TimerType.FindMembers);
1260 // This call will always succeed. There is exactly one TypeHandle instance per
1261 // type, TypeHandle.GetTypeHandle() will either return it or create a new one
1262 // if it didn't already exist.
1264 TypeHandle handle = TypeHandle.GetTypeHandle (t);
1267 return handle.MemberCache.FindMembers (mt, bf, name, FilterWithClosure_delegate, null);
1270 public static bool IsBuiltinType (Type t)
1272 if (t == object_type || t == string_type || t == int32_type || t == uint32_type ||
1273 t == int64_type || t == uint64_type || t == float_type || t == double_type ||
1274 t == char_type || t == short_type || t == decimal_type || t == bool_type ||
1275 t == sbyte_type || t == byte_type || t == ushort_type || t == void_type)
1282 // This is like IsBuiltinType, but lacks decimal_type, we should also clean up
1283 // the pieces in the code where we use IsBuiltinType and special case decimal_type.
1285 public static bool IsCLRType (Type t)
1287 if (t == object_type || t == int32_type || t == uint32_type ||
1288 t == int64_type || t == uint64_type || t == float_type || t == double_type ||
1289 t == char_type || t == short_type || t == bool_type ||
1290 t == sbyte_type || t == byte_type || t == ushort_type)
1296 public static bool IsDelegateType (Type t)
1298 if (t.IsSubclassOf (TypeManager.delegate_type))
1304 public static bool IsEnumType (Type t)
1306 if (t == TypeManager.enum_type || t.IsSubclassOf (TypeManager.enum_type))
1313 // Whether a type is unmanaged. This is used by the unsafe code (25.2)
1315 public static bool IsUnmanagedType (Type t)
1317 if (IsBuiltinType (t) && t != TypeManager.string_type)
1326 if (IsValueType (t)){
1327 if (t is TypeBuilder){
1328 TypeContainer tc = LookupTypeContainer (t);
1330 foreach (Field f in tc.Fields){
1331 if (f.FieldBuilder.IsStatic)
1333 if (!IsUnmanagedType (f.FieldBuilder.FieldType))
1337 FieldInfo [] fields = t.GetFields ();
1339 foreach (FieldInfo f in fields){
1342 if (!IsUnmanagedType (f.FieldType))
1352 public static bool IsValueType (Type t)
1354 if (t.IsSubclassOf (TypeManager.value_type) && (t != TypeManager.enum_type))
1360 public static bool IsInterfaceType (Type t)
1362 Interface iface = builder_to_declspace [t] as Interface;
1371 // Checks whether `type' is a subclass or nested child of `parent'.
1373 public static bool IsSubclassOrNestedChildOf (Type type, Type parent)
1376 if ((type == parent) || type.IsSubclassOf (parent))
1379 // Handle nested types.
1380 type = type.DeclaringType;
1381 } while (type != null);
1387 // Checks whether `type' is a nested child of `parent'.
1389 public static bool IsNestedChildOf (Type type, Type parent)
1394 type = type.DeclaringType;
1395 while (type != null) {
1399 type = type.DeclaringType;
1406 // Do the right thing when returning the element type of
1407 // an array type based on whether we
1409 public static Type GetElementType (Type t)
1411 if (RootContext.StdLib)
1412 return t.GetElementType ();
1414 return TypeToCoreType (t.GetElementType ());
1418 /// Returns the User Defined Types
1420 public static ArrayList UserTypes {
1426 public static Hashtable TypeContainers {
1428 return typecontainers;
1432 static Hashtable attr_to_allowmult;
1434 public static void RegisterAttributeAllowMultiple (Type attr_type, bool allow)
1436 if (attr_to_allowmult == null)
1437 attr_to_allowmult = new PtrHashtable ();
1439 if (attr_to_allowmult.Contains (attr_type))
1442 attr_to_allowmult.Add (attr_type, allow);
1446 public static bool AreMultipleAllowed (Type attr_type)
1448 if (!(attr_type is TypeBuilder)) {
1449 System.Attribute [] attrs = System.Attribute.GetCustomAttributes (attr_type);
1451 foreach (System.Attribute tmp in attrs)
1452 if (tmp is AttributeUsageAttribute) {
1453 return ((AttributeUsageAttribute) tmp).AllowMultiple;
1459 if (attr_to_allowmult == null)
1462 return (bool) attr_to_allowmult [attr_type];
1465 static Hashtable builder_to_constant;
1467 public static void RegisterConstant (FieldBuilder fb, Const c)
1469 if (builder_to_constant == null)
1470 builder_to_constant = new PtrHashtable ();
1472 if (builder_to_constant.Contains (fb))
1475 builder_to_constant.Add (fb, c);
1478 public static Const LookupConstant (FieldBuilder fb)
1480 if (builder_to_constant == null)
1483 return (Const) builder_to_constant [fb];
1487 /// Gigantic work around for missing features in System.Reflection.Emit follows.
1491 /// Since System.Reflection.Emit can not return MethodBase.GetParameters
1492 /// for anything which is dynamic, and we need this in a number of places,
1493 /// we register this information here, and use it afterwards.
1495 static public bool RegisterMethod (MethodBase mb, InternalParameters ip, Type [] args)
1500 method_arguments.Add (mb, args);
1501 method_internal_params.Add (mb, ip);
1506 static public InternalParameters LookupParametersByBuilder (MethodBase mb)
1508 if (! (mb is ConstructorBuilder || mb is MethodBuilder))
1511 if (method_internal_params.Contains (mb))
1512 return (InternalParameters) method_internal_params [mb];
1514 throw new Exception ("Argument for Method not registered" + mb);
1518 /// Returns the argument types for a method based on its methodbase
1520 /// For dynamic methods, we use the compiler provided types, for
1521 /// methods from existing assemblies we load them from GetParameters,
1522 /// and insert them into the cache
1524 static public Type [] GetArgumentTypes (MethodBase mb)
1526 if (method_arguments.Contains (mb))
1527 return (Type []) method_arguments [mb];
1529 ParameterInfo [] pi = mb.GetParameters ();
1531 Type [] types = new Type [c];
1533 for (int i = 0; i < c; i++)
1534 types [i] = pi [i].ParameterType;
1536 method_arguments.Add (mb, types);
1542 /// Returns the argument types for an indexer based on its PropertyInfo
1544 /// For dynamic indexers, we use the compiler provided types, for
1545 /// indexers from existing assemblies we load them from GetParameters,
1546 /// and insert them into the cache
1548 static public Type [] GetArgumentTypes (PropertyInfo indexer)
1550 if (indexer_arguments.Contains (indexer))
1551 return (Type []) indexer_arguments [indexer];
1552 else if (indexer is PropertyBuilder)
1553 // If we're a PropertyBuilder and not in the
1554 // `indexer_arguments' hash, then we're a property and
1558 ParameterInfo [] pi = indexer.GetIndexParameters ();
1559 // Property, not an indexer.
1563 Type [] types = new Type [c];
1565 for (int i = 0; i < c; i++)
1566 types [i] = pi [i].ParameterType;
1568 indexer_arguments.Add (indexer, types);
1574 // This is a workaround the fact that GetValue is not
1575 // supported for dynamic types
1577 static Hashtable fields = new Hashtable ();
1578 static public bool RegisterFieldValue (FieldBuilder fb, object value)
1580 if (fields.Contains (fb))
1583 fields.Add (fb, value);
1588 static public object GetValue (FieldBuilder fb)
1593 static Hashtable fieldbuilders_to_fields = new Hashtable ();
1594 static public bool RegisterFieldBase (FieldBuilder fb, FieldBase f)
1596 if (fieldbuilders_to_fields.Contains (fb))
1599 fieldbuilders_to_fields.Add (fb, f);
1603 static public FieldBase GetField (FieldInfo fb)
1605 return (FieldBase) fieldbuilders_to_fields [fb];
1608 static Hashtable events;
1610 static public bool RegisterEvent (MyEventBuilder eb, MethodBase add, MethodBase remove)
1613 events = new Hashtable ();
1615 if (events.Contains (eb))
1618 events.Add (eb, new Pair (add, remove));
1623 static public MethodInfo GetAddMethod (EventInfo ei)
1625 if (ei is MyEventBuilder) {
1626 Pair pair = (Pair) events [ei];
1628 return (MethodInfo) pair.First;
1630 return ei.GetAddMethod ();
1633 static public MethodInfo GetRemoveMethod (EventInfo ei)
1635 if (ei is MyEventBuilder) {
1636 Pair pair = (Pair) events [ei];
1638 return (MethodInfo) pair.Second;
1640 return ei.GetRemoveMethod ();
1643 static Hashtable priv_fields_events;
1645 static public bool RegisterPrivateFieldOfEvent (EventInfo einfo, FieldBuilder builder)
1647 if (priv_fields_events == null)
1648 priv_fields_events = new Hashtable ();
1650 if (priv_fields_events.Contains (einfo))
1653 priv_fields_events.Add (einfo, builder);
1658 static public MemberInfo GetPrivateFieldOfEvent (EventInfo ei)
1660 if (priv_fields_events == null)
1663 return (MemberInfo) priv_fields_events [ei];
1666 static Hashtable properties;
1668 static public bool RegisterProperty (PropertyBuilder pb, MethodBase get, MethodBase set)
1670 if (properties == null)
1671 properties = new Hashtable ();
1673 if (properties.Contains (pb))
1676 properties.Add (pb, new Pair (get, set));
1681 static public bool RegisterIndexer (PropertyBuilder pb, MethodBase get,
1682 MethodBase set, Type[] args)
1684 if (!RegisterProperty (pb, get,set))
1687 indexer_arguments.Add (pb, args);
1693 /// Given an array of interface types, expand and eliminate repeated ocurrences
1694 /// of an interface.
1698 /// This expands in context like: IA; IB : IA; IC : IA, IB; the interface "IC" to
1701 public static Type [] ExpandInterfaces (Type [] base_interfaces)
1703 ArrayList new_ifaces = new ArrayList ();
1705 foreach (Type iface in base_interfaces){
1706 if (!new_ifaces.Contains (iface))
1707 new_ifaces.Add (iface);
1709 Type [] implementing = TypeManager.GetInterfaces (iface);
1711 foreach (Type imp in implementing){
1712 if (!new_ifaces.Contains (imp))
1713 new_ifaces.Add (imp);
1716 Type [] ret = new Type [new_ifaces.Count];
1717 new_ifaces.CopyTo (ret, 0);
1722 /// This function returns the interfaces in the type `t'. Works with
1723 /// both types and TypeBuilders.
1725 public static Type [] GetInterfaces (Type t)
1728 // The reason for catching the Array case is that Reflection.Emit
1729 // will not return a TypeBuilder for Array types of TypeBuilder types,
1730 // but will still throw an exception if we try to call GetInterfaces
1733 // Since the array interfaces are always constant, we return those for
1738 t = TypeManager.array_type;
1740 if (t is TypeBuilder){
1741 Type [] parent_ifaces;
1743 if (t.BaseType == null)
1744 parent_ifaces = NoTypes;
1746 parent_ifaces = GetInterfaces (t.BaseType);
1747 Type [] type_ifaces = (Type []) builder_to_ifaces [t];
1748 if (type_ifaces == null)
1749 type_ifaces = NoTypes;
1751 int parent_count = parent_ifaces.Length;
1752 Type [] result = new Type [parent_count + type_ifaces.Length];
1753 parent_ifaces.CopyTo (result, 0);
1754 type_ifaces.CopyTo (result, parent_count);
1758 return t.GetInterfaces ();
1762 /// The following is used to check if a given type implements an interface.
1763 /// The cache helps us reduce the expense of hitting Type.GetInterfaces everytime.
1765 public static bool ImplementsInterface (Type t, Type iface)
1770 // FIXME OPTIMIZATION:
1771 // as soon as we hit a non-TypeBuiler in the interface
1772 // chain, we could return, as the `Type.GetInterfaces'
1773 // will return all the interfaces implement by the type
1777 interfaces = GetInterfaces (t);
1779 if (interfaces != null){
1780 foreach (Type i in interfaces){
1787 } while (t != null);
1792 // This is a custom version of Convert.ChangeType() which works
1793 // with the TypeBuilder defined types when compiling corlib.
1794 public static object ChangeType (object value, Type conversionType, out bool error)
1796 if (!(value is IConvertible)){
1801 IConvertible convertValue = (IConvertible) value;
1802 CultureInfo ci = CultureInfo.CurrentCulture;
1803 NumberFormatInfo provider = ci.NumberFormat;
1806 // We must use Type.Equals() here since `conversionType' is
1807 // the TypeBuilder created version of a system type and not
1808 // the system type itself. You cannot use Type.GetTypeCode()
1809 // on such a type - it'd always return TypeCode.Object.
1813 if (conversionType.Equals (typeof (Boolean)))
1814 return (object)(convertValue.ToBoolean (provider));
1815 else if (conversionType.Equals (typeof (Byte)))
1816 return (object)(convertValue.ToByte (provider));
1817 else if (conversionType.Equals (typeof (Char)))
1818 return (object)(convertValue.ToChar (provider));
1819 else if (conversionType.Equals (typeof (DateTime)))
1820 return (object)(convertValue.ToDateTime (provider));
1821 else if (conversionType.Equals (typeof (Decimal)))
1822 return (object)(convertValue.ToDecimal (provider));
1823 else if (conversionType.Equals (typeof (Double)))
1824 return (object)(convertValue.ToDouble (provider));
1825 else if (conversionType.Equals (typeof (Int16)))
1826 return (object)(convertValue.ToInt16 (provider));
1827 else if (conversionType.Equals (typeof (Int32)))
1828 return (object)(convertValue.ToInt32 (provider));
1829 else if (conversionType.Equals (typeof (Int64)))
1830 return (object)(convertValue.ToInt64 (provider));
1831 else if (conversionType.Equals (typeof (SByte)))
1832 return (object)(convertValue.ToSByte (provider));
1833 else if (conversionType.Equals (typeof (Single)))
1834 return (object)(convertValue.ToSingle (provider));
1835 else if (conversionType.Equals (typeof (String)))
1836 return (object)(convertValue.ToString (provider));
1837 else if (conversionType.Equals (typeof (UInt16)))
1838 return (object)(convertValue.ToUInt16 (provider));
1839 else if (conversionType.Equals (typeof (UInt32)))
1840 return (object)(convertValue.ToUInt32 (provider));
1841 else if (conversionType.Equals (typeof (UInt64)))
1842 return (object)(convertValue.ToUInt64 (provider));
1843 else if (conversionType.Equals (typeof (Object)))
1844 return (object)(value);
1854 // This is needed, because enumerations from assemblies
1855 // do not report their underlyingtype, but they report
1858 public static Type EnumToUnderlying (Type t)
1860 if (t == TypeManager.enum_type)
1863 t = t.UnderlyingSystemType;
1864 if (!TypeManager.IsEnumType (t))
1867 if (t is TypeBuilder) {
1868 // slow path needed to compile corlib
1869 if (t == TypeManager.bool_type ||
1870 t == TypeManager.byte_type ||
1871 t == TypeManager.sbyte_type ||
1872 t == TypeManager.char_type ||
1873 t == TypeManager.short_type ||
1874 t == TypeManager.ushort_type ||
1875 t == TypeManager.int32_type ||
1876 t == TypeManager.uint32_type ||
1877 t == TypeManager.int64_type ||
1878 t == TypeManager.uint64_type)
1880 throw new Exception ("Unhandled typecode in enum " + " from " + t.AssemblyQualifiedName);
1882 TypeCode tc = Type.GetTypeCode (t);
1885 case TypeCode.Boolean:
1886 return TypeManager.bool_type;
1888 return TypeManager.byte_type;
1889 case TypeCode.SByte:
1890 return TypeManager.sbyte_type;
1892 return TypeManager.char_type;
1893 case TypeCode.Int16:
1894 return TypeManager.short_type;
1895 case TypeCode.UInt16:
1896 return TypeManager.ushort_type;
1897 case TypeCode.Int32:
1898 return TypeManager.int32_type;
1899 case TypeCode.UInt32:
1900 return TypeManager.uint32_type;
1901 case TypeCode.Int64:
1902 return TypeManager.int64_type;
1903 case TypeCode.UInt64:
1904 return TypeManager.uint64_type;
1906 throw new Exception ("Unhandled typecode in enum " + tc + " from " + t.AssemblyQualifiedName);
1910 // When compiling corlib and called with one of the core types, return
1911 // the corresponding typebuilder for that type.
1913 public static Type TypeToCoreType (Type t)
1915 if (RootContext.StdLib || (t is TypeBuilder))
1918 TypeCode tc = Type.GetTypeCode (t);
1921 case TypeCode.Boolean:
1922 return TypeManager.bool_type;
1924 return TypeManager.byte_type;
1925 case TypeCode.SByte:
1926 return TypeManager.sbyte_type;
1928 return TypeManager.char_type;
1929 case TypeCode.Int16:
1930 return TypeManager.short_type;
1931 case TypeCode.UInt16:
1932 return TypeManager.ushort_type;
1933 case TypeCode.Int32:
1934 return TypeManager.int32_type;
1935 case TypeCode.UInt32:
1936 return TypeManager.uint32_type;
1937 case TypeCode.Int64:
1938 return TypeManager.int64_type;
1939 case TypeCode.UInt64:
1940 return TypeManager.uint64_type;
1941 case TypeCode.String:
1942 return TypeManager.string_type;
1944 if (t == typeof (void))
1945 return TypeManager.void_type;
1946 if (t == typeof (object))
1947 return TypeManager.object_type;
1948 if (t == typeof (System.Type))
1949 return TypeManager.type_type;
1955 /// Utility function that can be used to probe whether a type
1956 /// is managed or not.
1958 public static bool VerifyUnManaged (Type t, Location loc)
1960 if (t.IsValueType || t.IsPointer){
1962 // FIXME: this is more complex, we actually need to
1963 // make sure that the type does not contain any
1969 if (!RootContext.StdLib && (t == TypeManager.decimal_type))
1970 // We need this explicit check here to make it work when
1971 // compiling corlib.
1976 "Cannot take the address or size of a variable of a managed type ('" +
1977 CSharpName (t) + "')");
1982 /// Returns the name of the indexer in a given type.
1985 /// The default is not always `Item'. The user can change this behaviour by
1986 /// using the DefaultMemberAttribute in the class.
1988 /// For example, the String class indexer is named `Chars' not `Item'
1990 public static string IndexerPropertyName (Type t)
1992 if (t is TypeBuilder) {
1993 if (t.IsInterface) {
1994 Interface i = LookupInterface (t);
1996 if ((i == null) || (i.IndexerName == null))
1999 return i.IndexerName;
2001 TypeContainer tc = LookupTypeContainer (t);
2003 if ((tc == null) || (tc.IndexerName == null))
2006 return tc.IndexerName;
2010 System.Attribute attr = System.Attribute.GetCustomAttribute (
2011 t, TypeManager.default_member_type);
2013 DefaultMemberAttribute dma = (DefaultMemberAttribute) attr;
2014 return dma.MemberName;
2020 public static void MakePinned (LocalBuilder builder)
2023 // FIXME: Flag the "LocalBuilder" type as being
2024 // pinned. Figure out API.
2030 // Returns whether the array of memberinfos contains the given method
2032 static bool ArrayContainsMethod (MemberInfo [] array, MethodBase new_method)
2034 Type [] new_args = TypeManager.GetArgumentTypes (new_method);
2036 foreach (MethodBase method in array){
2037 if (method.Name != new_method.Name)
2040 Type [] old_args = TypeManager.GetArgumentTypes (method);
2041 int old_count = old_args.Length;
2044 if (new_args.Length != old_count)
2047 for (i = 0; i < old_count; i++){
2048 if (old_args [i] != new_args [i])
2060 // We copy methods from `new_members' into `target_list' if the signature
2061 // for the method from in the new list does not exist in the target_list
2063 // The name is assumed to be the same.
2065 public static ArrayList CopyNewMethods (ArrayList target_list, MemberList new_members)
2067 if (target_list == null){
2068 target_list = new ArrayList ();
2070 foreach (MemberInfo mi in new_members){
2071 if (mi is MethodBase)
2072 target_list.Add (mi);
2077 MemberInfo [] target_array = new MemberInfo [target_list.Count];
2078 target_list.CopyTo (target_array, 0);
2080 foreach (MemberInfo mi in new_members){
2081 MethodBase new_method = (MethodBase) mi;
2083 if (!ArrayContainsMethod (target_array, new_method))
2084 target_list.Add (new_method);
2090 public enum MethodFlags {
2092 IsObsoleteError = 2,
2097 // Returns the TypeManager.MethodFlags for this method.
2098 // This emits an error 619 / warning 618 if the method is obsolete.
2099 // In the former case, TypeManager.MethodFlags.IsObsoleteError is returned.
2101 static public MethodFlags GetMethodFlags (MethodBase mb, Location loc)
2103 MethodFlags flags = 0;
2105 if (mb.DeclaringType is TypeBuilder){
2106 MethodData method = (MethodData) builder_to_method [mb];
2107 if (method == null) {
2108 // FIXME: implement Obsolete attribute on Property,
2109 // Indexer and Event.
2113 return method.GetMethodFlags (loc);
2116 object [] attrs = mb.GetCustomAttributes (true);
2117 foreach (object ta in attrs){
2118 if (!(ta is System.Attribute)){
2119 Console.WriteLine ("Unknown type in GetMethodFlags: " + ta);
2122 System.Attribute a = (System.Attribute) ta;
2123 if (a.TypeId == TypeManager.obsolete_attribute_type){
2124 ObsoleteAttribute oa = (ObsoleteAttribute) a;
2126 string method_desc = TypeManager.CSharpSignature (mb);
2129 Report.Error (619, loc, "Method `" + method_desc +
2130 "' is obsolete: `" + oa.Message + "'");
2131 return MethodFlags.IsObsoleteError;
2133 Report.Warning (618, loc, "Method `" + method_desc +
2134 "' is obsolete: `" + oa.Message + "'");
2136 flags |= MethodFlags.IsObsolete;
2142 // Skip over conditional code.
2144 if (a.TypeId == TypeManager.conditional_attribute_type){
2145 ConditionalAttribute ca = (ConditionalAttribute) a;
2147 if (RootContext.AllDefines [ca.ConditionString] == null)
2148 flags |= MethodFlags.ShouldIgnore;
2155 #region MemberLookup implementation
2158 // Name of the member
2160 static string closure_name;
2163 // Whether we allow private members in the result (since FindMembers
2164 // uses NonPublic for both protected and private), we need to distinguish.
2166 static bool closure_private_ok;
2169 // Who is invoking us and which type is being queried currently.
2171 static Type closure_invocation_type;
2172 static Type closure_queried_type;
2173 static Type closure_qualifier_type;
2176 // The assembly that defines the type is that is calling us
2178 static Assembly closure_invocation_assembly;
2180 static internal bool FilterNone (MemberInfo m, object filter_criteria)
2186 // This filter filters by name + whether it is ok to include private
2187 // members in the search
2189 static internal bool FilterWithClosure (MemberInfo m, object filter_criteria)
2192 // Hack: we know that the filter criteria will always be in the `closure'
2196 if ((filter_criteria != null) && (m.Name != (string) filter_criteria))
2199 if (((closure_qualifier_type == null) || (closure_qualifier_type == closure_invocation_type)) &&
2200 (m.DeclaringType == closure_invocation_type))
2204 // Ugly: we need to find out the type of `m', and depending
2205 // on this, tell whether we accept or not
2207 if (m is MethodBase){
2208 MethodBase mb = (MethodBase) m;
2209 MethodAttributes ma = mb.Attributes & MethodAttributes.MemberAccessMask;
2211 if (ma == MethodAttributes.Private)
2212 return closure_private_ok || (closure_invocation_type == m.DeclaringType) ||
2213 IsNestedChildOf (closure_invocation_type, m.DeclaringType);
2216 // FamAndAssem requires that we not only derivate, but we are on the
2219 if (ma == MethodAttributes.FamANDAssem){
2220 if (closure_invocation_assembly != mb.DeclaringType.Assembly)
2224 // Assembly and FamORAssem succeed if we're in the same assembly.
2225 if ((ma == MethodAttributes.Assembly) || (ma == MethodAttributes.FamORAssem)){
2226 if (closure_invocation_assembly == mb.DeclaringType.Assembly)
2230 // We already know that we aren't in the same assembly.
2231 if (ma == MethodAttributes.Assembly)
2234 // Family and FamANDAssem require that we derive.
2235 if ((ma == MethodAttributes.Family) || (ma == MethodAttributes.FamANDAssem)){
2236 if (closure_invocation_type == null)
2239 if (!IsSubclassOrNestedChildOf (closure_invocation_type, mb.DeclaringType))
2242 // Although a derived class can access protected members of its base class
2243 // it cannot do so through an instance of the base class (CS1540).
2244 if (!mb.IsStatic && (closure_invocation_type != closure_qualifier_type) &&
2245 (closure_qualifier_type != null) &&
2246 closure_invocation_type.IsSubclassOf (closure_qualifier_type))
2256 if (m is FieldInfo){
2257 FieldInfo fi = (FieldInfo) m;
2258 FieldAttributes fa = fi.Attributes & FieldAttributes.FieldAccessMask;
2260 if (fa == FieldAttributes.Private)
2261 return closure_private_ok || (closure_invocation_type == m.DeclaringType) ||
2262 IsNestedChildOf (closure_invocation_type, m.DeclaringType);
2265 // FamAndAssem requires that we not only derivate, but we are on the
2268 if (fa == FieldAttributes.FamANDAssem){
2269 if (closure_invocation_assembly != fi.DeclaringType.Assembly)
2273 // Assembly and FamORAssem succeed if we're in the same assembly.
2274 if ((fa == FieldAttributes.Assembly) || (fa == FieldAttributes.FamORAssem)){
2275 if (closure_invocation_assembly == fi.DeclaringType.Assembly)
2279 // We already know that we aren't in the same assembly.
2280 if (fa == FieldAttributes.Assembly)
2283 // Family and FamANDAssem require that we derive.
2284 if ((fa == FieldAttributes.Family) || (fa == FieldAttributes.FamANDAssem)){
2285 if (closure_invocation_type == null)
2288 if (!IsSubclassOrNestedChildOf (closure_invocation_type, fi.DeclaringType))
2291 // Although a derived class can access protected members of its base class
2292 // it cannot do so through an instance of the base class (CS1540).
2293 if (!fi.IsStatic && (closure_invocation_type != closure_qualifier_type) &&
2294 (closure_qualifier_type != null) &&
2295 closure_invocation_type.IsSubclassOf (closure_qualifier_type))
2306 // EventInfos and PropertyInfos, return true because they lack permission
2307 // informaiton, so we need to check later on the methods.
2312 static MemberFilter FilterWithClosure_delegate = new MemberFilter (FilterWithClosure);
2313 static MemberFilter FilterNone_delegate = new MemberFilter (FilterNone);
2316 // Looks up a member called `name' in the `queried_type'. This lookup
2317 // is done by code that is contained in the definition for `invocation_type'
2318 // through a qualifier of type `qualifier_type' (or null if there is no qualifier).
2320 // `invocation_type' is used to check whether we're allowed to access the requested
2321 // member wrt its protection level.
2323 // When called from MemberAccess, `qualifier_type' is the type which is used to access
2324 // the requested member (`class B { A a = new A (); a.foo = 5; }'; here invocation_type
2325 // is B and qualifier_type is A). This is used to do the CS1540 check.
2327 // When resolving a SimpleName, `qualifier_type' is null.
2329 // The `qualifier_type' is used for the CS1540 check; it's normally either null or
2330 // the same than `queried_type' - except when we're being called from BaseAccess;
2331 // in this case, `invocation_type' is the current type and `queried_type' the base
2332 // type, so this'd normally trigger a CS1540.
2334 // The binding flags are `bf' and the kind of members being looked up are `mt'
2336 // The return value always includes private members which code in `invocation_type'
2337 // is allowed to access (using the specified `qualifier_type' if given); only use
2338 // BindingFlags.NonPublic to bypass the permission check.
2340 // Returns an array of a single element for everything but Methods/Constructors
2341 // that might return multiple matches.
2343 public static MemberInfo [] MemberLookup (Type invocation_type, Type qualifier_type,
2344 Type queried_type, MemberTypes mt,
2345 BindingFlags original_bf, string name)
2347 Timer.StartTimer (TimerType.MemberLookup);
2349 MemberInfo[] retval = RealMemberLookup (invocation_type, qualifier_type,
2350 queried_type, mt, original_bf, name);
2352 Timer.StopTimer (TimerType.MemberLookup);
2357 static MemberInfo [] RealMemberLookup (Type invocation_type, Type qualifier_type,
2358 Type queried_type, MemberTypes mt,
2359 BindingFlags original_bf, string name)
2361 BindingFlags bf = original_bf;
2363 ArrayList method_list = null;
2364 Type current_type = queried_type;
2365 bool searching = (original_bf & BindingFlags.DeclaredOnly) == 0;
2366 bool skip_iface_check = true, used_cache = false;
2367 bool always_ok_flag = false;
2369 closure_name = name;
2370 closure_invocation_type = invocation_type;
2371 closure_invocation_assembly = invocation_type != null ? invocation_type.Assembly : null;
2372 closure_qualifier_type = qualifier_type;
2375 // If we are a nested class, we always have access to our container
2378 if (invocation_type != null){
2379 string invocation_name = invocation_type.FullName;
2380 if (invocation_name.IndexOf ('+') != -1){
2381 string container = queried_type.FullName + "+";
2382 int container_length = container.Length;
2384 if (invocation_name.Length > container_length){
2385 string shared = invocation_name.Substring (0, container_length);
2387 if (shared == container)
2388 always_ok_flag = true;
2397 // `NonPublic' is lame, because it includes both protected and
2398 // private methods, so we need to control this behavior by
2399 // explicitly tracking if a private method is ok or not.
2401 // The possible cases are:
2402 // public, private and protected (internal does not come into the
2405 if ((invocation_type != null) &&
2406 ((invocation_type == current_type) ||
2407 IsNestedChildOf (invocation_type, current_type)) ||
2409 bf = original_bf | BindingFlags.NonPublic;
2413 closure_private_ok = (original_bf & BindingFlags.NonPublic) != 0;
2414 closure_queried_type = current_type;
2416 Timer.StopTimer (TimerType.MemberLookup);
2418 list = MemberLookup_FindMembers (current_type, mt, bf, name, out used_cache);
2420 Timer.StartTimer (TimerType.MemberLookup);
2423 // When queried for an interface type, the cache will automatically check all
2424 // inherited members, so we don't need to do this here. However, this only
2425 // works if we already used the cache in the first iteration of this loop.
2427 // If we used the cache in any further iteration, we can still terminate the
2428 // loop since the cache always looks in all parent classes.
2434 skip_iface_check = false;
2436 if (current_type == TypeManager.object_type)
2439 current_type = current_type.BaseType;
2442 // This happens with interfaces, they have a null
2443 // basetype. Look members up in the Object class.
2445 if (current_type == null)
2446 current_type = TypeManager.object_type;
2449 if (list.Count == 0)
2453 // Events and types are returned by both `static' and `instance'
2454 // searches, which means that our above FindMembers will
2455 // return two copies of the same.
2457 if (list.Count == 1 && !(list [0] is MethodBase)){
2458 return (MemberInfo []) list;
2462 // Multiple properties: we query those just to find out the indexer
2465 if (list [0] is PropertyInfo)
2466 return (MemberInfo []) list;
2469 // We found an event: the cache lookup returns both the event and
2470 // its private field.
2472 if (list [0] is EventInfo) {
2473 if ((list.Count == 2) && (list [1] is FieldInfo))
2474 return new MemberInfo [] { list [0] };
2481 // We found methods, turn the search into "method scan"
2485 method_list = CopyNewMethods (method_list, list);
2486 mt &= (MemberTypes.Method | MemberTypes.Constructor);
2487 } while (searching);
2489 if (method_list != null && method_list.Count > 0)
2490 return (MemberInfo []) method_list.ToArray (typeof (MemberInfo));
2493 // This happens if we already used the cache in the first iteration, in this case
2494 // the cache already looked in all interfaces.
2496 if (skip_iface_check)
2500 // Interfaces do not list members they inherit, so we have to
2503 if (!queried_type.IsInterface)
2506 if (queried_type.IsArray)
2507 queried_type = TypeManager.array_type;
2509 Type [] ifaces = GetInterfaces (queried_type);
2513 foreach (Type itype in ifaces){
2516 x = MemberLookup (null, null, itype, mt, bf, name);
2525 // This is used to extract properties and event declarations from a type
2527 static MemberInfo [] SpecialContainerLookup (Type t, bool is_static)
2529 BindingFlags bf = BindingFlags.DeclaredOnly | (is_static ? BindingFlags.Static : BindingFlags.Instance);
2531 bf |= BindingFlags.Public | BindingFlags.NonPublic;
2533 if (t is TypeBuilder) {
2534 DeclSpace decl = (DeclSpace) builder_to_declspace [t];
2536 return (MemberInfo []) decl.FindMembers (
2537 MemberTypes.Property | MemberTypes.Event,
2538 bf, FilterNone_delegate, null);
2540 return t.FindMembers (MemberTypes.Property | MemberTypes.Event,
2541 bf, FilterNone_delegate, null);
2546 public static bool IsSpecialMethod (MethodBase mb)
2548 Type t = mb.DeclaringType;
2550 MemberInfo [] matches = TypeManager.SpecialContainerLookup (t, mb.IsStatic);
2551 if (matches == null)
2554 foreach (MemberInfo mi in matches){
2555 if (mi is PropertyBuilder){
2556 Pair p = (Pair) properties [mi];
2558 if (p.First == mb || p.Second == mb)
2560 } else if (mi is PropertyInfo){
2561 MethodInfo [] methods = ((PropertyInfo) mi).GetAccessors (true);
2563 foreach (MethodInfo m in methods){
2567 } else if (mi is MyEventBuilder){
2568 Pair p = (Pair) events [mi];
2570 if (p.First == mb || p.Second == mb)
2572 } else if (mi is EventInfo){
2573 EventInfo ei = ((EventInfo) mi);
2575 if (ei.GetAddMethod (true) == mb)
2578 if (ei.GetRemoveMethod (true) == mb)
2581 if (ei.GetRaiseMethod (true) == mb)
2587 // Now check if it is an operator method
2591 if (s.StartsWith ("op_")){
2592 foreach (string name in Unary.oper_names){
2597 foreach (string name in Binary.oper_names){
2611 /// There is exactly one instance of this class per type.
2613 public sealed class TypeHandle : IMemberContainer {
2614 public readonly TypeHandle BaseType;
2616 readonly int id = ++next_id;
2617 static int next_id = 0;
2620 /// Lookup a TypeHandle instance for the given type. If the type doesn't have
2621 /// a TypeHandle yet, a new instance of it is created. This static method
2622 /// ensures that we'll only have one TypeHandle instance per type.
2624 public static TypeHandle GetTypeHandle (Type t)
2626 TypeHandle handle = (TypeHandle) type_hash [t];
2630 handle = new TypeHandle (t);
2631 type_hash.Add (t, handle);
2636 /// Returns the TypeHandle for TypeManager.object_type.
2638 public static IMemberContainer ObjectType {
2640 if (object_type != null)
2643 object_type = GetTypeHandle (TypeManager.object_type);
2650 /// Returns the TypeHandle for TypeManager.array_type.
2652 public static IMemberContainer ArrayType {
2654 if (array_type != null)
2657 array_type = GetTypeHandle (TypeManager.array_type);
2663 private static PtrHashtable type_hash = new PtrHashtable ();
2665 private static TypeHandle object_type = null;
2666 private static TypeHandle array_type = null;
2669 private bool is_interface;
2670 private MemberCache member_cache;
2672 private TypeHandle (Type type)
2675 if (type.BaseType != null)
2676 BaseType = GetTypeHandle (type.BaseType);
2677 else if ((type != TypeManager.object_type) && (type != typeof (object)))
2678 is_interface = true;
2679 this.member_cache = new MemberCache (this);
2682 // IMemberContainer methods
2684 public string Name {
2686 return type.FullName;
2696 public IMemberContainer Parent {
2702 public bool IsInterface {
2704 return is_interface;
2708 public MemberList GetMembers (MemberTypes mt, BindingFlags bf)
2710 MemberInfo [] members;
2711 if (mt == MemberTypes.Event)
2712 members = type.GetEvents (bf | BindingFlags.DeclaredOnly);
2714 members = type.FindMembers (mt, bf | BindingFlags.DeclaredOnly,
2716 Array.Reverse (members);
2718 return new MemberList (members);
2721 // IMemberFinder methods
2723 public MemberList FindMembers (MemberTypes mt, BindingFlags bf, string name,
2724 MemberFilter filter, object criteria)
2726 return member_cache.FindMembers (mt, bf, name, filter, criteria);
2729 public MemberCache MemberCache {
2731 return member_cache;
2735 public override string ToString ()
2737 if (BaseType != null)
2738 return "TypeHandle (" + id + "," + Name + " : " + BaseType + ")";
2740 return "TypeHandle (" + id + "," + Name + ")";