2 // typegen.cs: type generation
4 // Author: Miguel de Icaza (miguel@gnu.org)
6 // Licensed under the terms of the GNU GPL
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
13 using System.Collections;
14 using System.Reflection;
15 using System.Reflection.Emit;
19 public class TypeManager {
21 // A list of core types that the compiler requires or uses
23 static public Type object_type;
24 static public Type value_type;
25 static public Type string_type;
26 static public Type int32_type;
27 static public Type uint32_type;
28 static public Type int64_type;
29 static public Type uint64_type;
30 static public Type float_type;
31 static public Type double_type;
32 static public Type char_type;
33 static public Type short_type;
34 static public Type decimal_type;
35 static public Type bool_type;
36 static public Type sbyte_type;
37 static public Type byte_type;
38 static public Type ushort_type;
39 static public Type enum_type;
40 static public Type delegate_type;
41 static public Type void_type;
42 static public Type enumeration_type;
43 static public Type array_type;
44 static public Type runtime_handle_type;
45 static public Type icloneable_type;
46 static public Type type_type;
47 static public Type ienumerator_type;
48 static public Type idisposable_type;
49 static public Type default_member_type;
50 static public Type iasyncresult_type;
51 static public Type asynccallback_type;
52 static public Type intptr_type;
53 static public Type monitor_type;
54 static public Type runtime_field_handle_type;
55 static public Type attribute_usage_type;
56 static public Type param_array_type;
59 // Internal, not really used outside
61 Type runtime_helpers_type;
64 // These methods are called by code generated by the compiler
66 static public MethodInfo string_concat_string_string;
67 static public MethodInfo string_concat_object_object;
68 static public MethodInfo system_type_get_type_from_handle;
69 static public MethodInfo object_getcurrent_void;
70 static public MethodInfo bool_movenext_void;
71 static public MethodInfo void_dispose_void;
72 static public MethodInfo void_monitor_enter_object;
73 static public MethodInfo void_monitor_exit_object;
74 static public MethodInfo void_initializearray_array_fieldhandle;
75 static public MethodInfo int_getlength_int;
78 // The attribute constructors.
80 static public ConstructorInfo cons_param_array_attribute;
83 // Holds the Array of Assemblies that have been loaded
84 // (either because it is the default or the user used the
85 // -r command line option)
90 // Keeps a list of module builders. We used this to do lookups
91 // on the modulebuilder using GetType -- needed for arrays
96 // This is the type_cache from the assemblies to avoid
97 // hitting System.Reflection on every lookup.
102 // This is used to hotld the corresponding TypeContainer objects
103 // since we need this in FindMembers
105 Hashtable typecontainers;
108 // Keeps track of those types that are defined by the
111 ArrayList user_types;
114 // Keeps a mapping between TypeBuilders and their TypeContainers
116 static Hashtable builder_to_container;
119 // Maps MethodBase.RuntimeTypeHandle to a Type array that contains
120 // the arguments to the method
122 static Hashtable method_arguments;
124 static Hashtable builder_to_interface;
127 // Keeps track of delegate types
130 static Hashtable builder_to_delegate;
133 // Keeps track of enum types
136 static Hashtable builder_to_enum;
139 // Keeps track of attribute types
142 static Hashtable builder_to_attr;
144 public TypeManager ()
146 assemblies = new ArrayList ();
147 modules = new ArrayList ();
148 user_types = new ArrayList ();
149 types = new Hashtable ();
150 typecontainers = new Hashtable ();
151 builder_to_interface = new Hashtable ();
152 builder_to_delegate = new Hashtable ();
153 builder_to_enum = new Hashtable ();
154 builder_to_attr = new Hashtable ();
157 static TypeManager ()
159 method_arguments = new Hashtable ();
160 builder_to_container = new Hashtable ();
161 type_interface_cache = new Hashtable ();
164 public void AddUserType (string name, TypeBuilder t)
170 public void AddUserType (string name, TypeBuilder t, TypeContainer tc)
172 AddUserType (name, t);
173 builder_to_container.Add (t, tc);
174 typecontainers.Add (name, tc);
177 public void AddDelegateType (string name, TypeBuilder t, Delegate del)
180 builder_to_delegate.Add (t, del);
183 public void AddEnumType (string name, TypeBuilder t, Enum en)
186 builder_to_enum.Add (t, en);
189 public void AddUserInterface (string name, TypeBuilder t, Interface i)
191 AddUserType (name, t);
192 builder_to_interface.Add (t, i);
195 public void RegisterAttrType (Type t, TypeContainer tc)
197 builder_to_attr.Add (t, tc);
201 // Returns the TypeContainer whose Type is `t' or null if there is no
202 // TypeContainer for `t' (ie, the Type comes from a library)
204 public static TypeContainer LookupTypeContainer (Type t)
206 return (TypeContainer) builder_to_container [t];
209 public Interface LookupInterface (Type t)
211 return (Interface) builder_to_interface [t];
214 public static Delegate LookupDelegate (Type t)
216 return (Delegate) builder_to_delegate [t];
219 public static TypeContainer LookupAttr (Type t)
221 return (TypeContainer) builder_to_attr [t];
225 // Registers an assembly to load types from.
227 public void AddAssembly (Assembly a)
233 // Registers a module builder to lookup types from
235 public void AddModule (ModuleBuilder mb)
241 // Returns the Type associated with @name
243 public Type LookupType (string name)
248 // First lookup in user defined and cached values
251 t = (Type) types [name];
255 foreach (Assembly a in assemblies){
256 t = a.GetType (name);
264 foreach (ModuleBuilder mb in modules) {
265 t = mb.GetType (name);
276 // Returns the C# name of a type if possible, or the full type name otherwise
278 static public string CSharpName (Type t)
282 else if (t == uint32_type)
284 else if (t == int64_type)
286 else if (t == uint64_type)
288 else if (t == float_type)
290 else if (t == double_type)
292 else if (t == char_type)
294 else if (t == short_type)
296 else if (t == decimal_type)
298 else if (t == bool_type)
300 else if (t == sbyte_type)
302 else if (t == byte_type)
304 else if (t == short_type)
306 else if (t == ushort_type)
308 else if (t == string_type)
310 else if (t == object_type)
317 // Looks up a type, and aborts if it is not found. This is used
318 // by types required by the compiler
320 Type CoreLookupType (string name)
322 Type t = LookupType (name);
325 throw new Exception ("Can not find core type " + name);
331 // Returns the MethodInfo for a method named `name' defined
332 // in type `t' which takes arguments of types `args'
334 MethodInfo GetMethod (Type t, string name, Type [] args)
336 MethodInfo mi = t.GetMethod (name, args);
339 throw new Exception ("Can not find the core function `" + name + "'");
344 ConstructorInfo GetConstructor (Type t, Type [] args)
346 ConstructorInfo ci = t.GetConstructor (args);
349 throw new Exception ("Can not find the core constructor for `" + t.FullName + "'");
355 // The types have to be initialized after the initial
356 // population of the type has happened (for example, to
357 // bootstrap the corlib.dll
359 public void InitCoreTypes ()
361 object_type = CoreLookupType ("System.Object");
362 value_type = CoreLookupType ("System.ValueType");
363 string_type = CoreLookupType ("System.String");
364 int32_type = CoreLookupType ("System.Int32");
365 int64_type = CoreLookupType ("System.Int64");
366 uint32_type = CoreLookupType ("System.UInt32");
367 uint64_type = CoreLookupType ("System.UInt64");
368 float_type = CoreLookupType ("System.Single");
369 double_type = CoreLookupType ("System.Double");
370 byte_type = CoreLookupType ("System.Byte");
371 sbyte_type = CoreLookupType ("System.SByte");
372 char_type = CoreLookupType ("System.Char");
373 short_type = CoreLookupType ("System.Int16");
374 ushort_type = CoreLookupType ("System.UInt16");
375 decimal_type = CoreLookupType ("System.Decimal");
376 bool_type = CoreLookupType ("System.Boolean");
377 enum_type = CoreLookupType ("System.Enum");
378 delegate_type = CoreLookupType ("System.MulticastDelegate");
379 array_type = CoreLookupType ("System.Array");
380 void_type = CoreLookupType ("System.Void");
381 type_type = CoreLookupType ("System.Type");
383 runtime_field_handle_type = CoreLookupType ("System.RuntimeFieldHandle");
384 runtime_helpers_type = CoreLookupType ("System.Runtime.CompilerServices.RuntimeHelpers");
385 default_member_type = CoreLookupType ("System.Reflection.DefaultMemberAttribute");
386 runtime_handle_type = CoreLookupType ("System.RuntimeTypeHandle");
387 asynccallback_type = CoreLookupType ("System.AsyncCallback");
388 iasyncresult_type = CoreLookupType ("System.IAsyncResult");
389 ienumerator_type = CoreLookupType ("System.Collections.IEnumerator");
390 idisposable_type = CoreLookupType ("System.IDisposable");
391 icloneable_type = CoreLookupType ("System.ICloneable");
392 monitor_type = CoreLookupType ("System.Threading.Monitor");
393 intptr_type = CoreLookupType ("System.IntPtr");
395 attribute_usage_type = CoreLookupType ("System.AttributeUsageAttribute");
396 param_array_type = CoreLookupType ("System.ParamArrayAttribute");
399 // Now load the default methods that we use.
401 Type [] string_string = { string_type, string_type };
402 string_concat_string_string = GetMethod (
403 string_type, "Concat", string_string);
405 Type [] object_object = { object_type, object_type };
406 string_concat_object_object = GetMethod (
407 string_type, "Concat", object_object);
409 Type [] runtime_type_handle = { runtime_handle_type };
410 system_type_get_type_from_handle = GetMethod (
411 type_type, "GetTypeFromHandle", runtime_type_handle);
416 Type [] void_arg = { };
417 object_getcurrent_void = GetMethod (
418 ienumerator_type, "get_Current", void_arg);
419 bool_movenext_void = GetMethod (
420 ienumerator_type, "MoveNext", void_arg);
421 void_dispose_void = GetMethod (
422 idisposable_type, "Dispose", void_arg);
427 Type [] object_arg = { object_type };
428 void_monitor_enter_object = GetMethod (
429 monitor_type, "Enter", object_arg);
430 void_monitor_exit_object = GetMethod (
431 monitor_type, "Exit", object_arg);
433 Type [] array_field_handle_arg = { array_type, runtime_field_handle_type };
435 void_initializearray_array_fieldhandle = GetMethod (
436 runtime_helpers_type, "InitializeArray", array_field_handle_arg);
441 Type [] int_arg = { int32_type };
442 int_getlength_int = GetMethod (
443 array_type, "GetLength", int_arg);
448 cons_param_array_attribute = GetConstructor (
449 param_array_type, void_arg);
453 public MemberInfo [] FindMembers (Type t, MemberTypes mt, BindingFlags bf, MemberFilter filter, object criteria)
455 if (!(t is TypeBuilder))
456 return t.FindMembers (mt, bf, filter, criteria);
458 Enum e = (Enum) builder_to_enum [t];
461 return e.FindMembers (mt, bf, filter, criteria);
463 Delegate del = (Delegate) builder_to_delegate [t];
466 return del.FindMembers (mt, bf, filter, criteria);
468 Interface iface = (Interface) builder_to_interface [t];
471 return iface.FindMembers (mt, bf, filter, criteria);
473 TypeContainer tc = (TypeContainer) builder_to_container [t];
476 return tc.FindMembers (mt, bf, filter, criteria);
481 public static bool IsBuiltinType (Type t)
483 if (t == object_type || t == string_type || t == int32_type || t == uint32_type ||
484 t == int64_type || t == uint64_type || t == float_type || t == double_type ||
485 t == char_type || t == short_type || t == decimal_type || t == bool_type ||
486 t == sbyte_type || t == byte_type || t == ushort_type)
492 public static bool IsDelegateType (Type t)
494 Delegate del = (Delegate) builder_to_delegate [t];
502 public static bool IsEnumType (Type t)
504 Enum en = (Enum) builder_to_enum [t];
512 public static bool IsInterfaceType (Type t)
514 Interface iface = (Interface) builder_to_interface [t];
523 // Returns the User Defined Types
525 public ArrayList UserTypes {
531 public Hashtable TypeContainers {
533 return typecontainers;
537 static string GetSig (MethodBase mb)
539 if (mb is MethodBuilder || mb is ConstructorBuilder)
540 return mb.ReflectedType.FullName + ":" + mb;
542 return mb.MethodHandle.ToString ();
546 // Gigantic work around for stupidity in System.Reflection.Emit follows
548 // Since System.Reflection.Emit can not return MethodBase.GetParameters
549 // for anything which is dynamic, and we need this in a number of places,
550 // we register this information here, and use it afterwards.
552 static public bool RegisterMethod (MethodBase mb, Type [] args)
558 if (method_arguments.Contains (s))
561 method_arguments.Add (s, args);
566 // Returns the argument types for a method based on its methodbase
568 // For dynamic methods, we use the compiler provided types, for
569 // methods from existing assemblies we load them from GetParameters,
570 // and insert them into the cache
572 static public Type [] GetArgumentTypes (MethodBase mb)
574 string sig = GetSig (mb);
575 object o = method_arguments [sig];
577 if (method_arguments.Contains (sig))
578 return (Type []) method_arguments [sig];
580 ParameterInfo [] pi = mb.GetParameters ();
582 Type [] types = new Type [c];
584 for (int i = 0; i < c; i++)
585 types [i] = pi [i].ParameterType;
587 method_arguments.Add (sig, types);
593 // This is a workaround the fact that GetValue is not
594 // supported for dynamic types
596 static Hashtable fields;
598 static public bool RegisterField (FieldBuilder fb, object value)
601 fields = new Hashtable ();
603 if (fields.Contains (fb))
606 fields.Add (fb, value);
611 static public object GetValue (FieldBuilder fb)
617 static Hashtable properties;
619 static public bool RegisterProperty (PropertyBuilder pb, MethodBase get, MethodBase set)
621 if (properties == null)
622 properties = new Hashtable ();
624 if (properties.Contains (pb))
627 properties.Add (pb, new DictionaryEntry (get, set));
632 static public MethodInfo [] GetAccessors (PropertyInfo pi)
636 if (pi is PropertyBuilder){
637 DictionaryEntry de = (DictionaryEntry) properties [pi];
639 ret = new MethodInfo [2];
640 ret [0] = (MethodInfo) de.Key;
641 ret [1] = (MethodInfo) de.Value;
645 return pi.GetAccessors ();
649 // The following is used to check if a given type implements an interface.
650 // The cache helps us reduce the expense of hitting Type.GetInterfaces everytime.
653 static Hashtable type_interface_cache;
655 public static bool ImplementsInterface (Type t, Type iface)
657 Type [] interfaces = (Type []) type_interface_cache [t];
659 if (interfaces == null) {
660 if (type_interface_cache.Contains (t))
663 interfaces = t.GetInterfaces ();
665 type_interface_cache [t] = interfaces;
668 if (interfaces == null)
671 for (int i = interfaces.Length; i > 0; ) {
673 if (interfaces [i] == iface)
682 // Returns the name of the indexer in a given type. The default
683 // is not always `Item'. The user can change this behaviour by
684 // using the DefaultMemberAttribute in the class.
686 // For example, the String class indexer is named `Chars' not `Item'
688 public static string IndexerPropertyName (Type t)
692 // FIXME: Replace with something that works around S.R.E failure
695 System.Attribute attr;
697 attr = System.Attribute.GetCustomAttribute (t, TypeManager.default_member_type);
701 DefaultMemberAttribute dma = (DefaultMemberAttribute) attr;
703 return dma.MemberName;