2001-09-27 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / mcs / typemanager.cs
1 //
2 // typegen.cs: type generation 
3 //
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 //
6 // Licensed under the terms of the GNU GPL
7 //
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
9 //
10 //
11
12 using System;
13 using System.Collections;
14 using System.Reflection;
15 using System.Reflection.Emit;
16
17 namespace CIR {
18
19 public class TypeManager {
20         static public Type object_type;
21         static public Type value_type;
22         static public Type string_type;
23         static public Type int32_type;
24         static public Type uint32_type;
25         static public Type int64_type;
26         static public Type uint64_type;
27         static public Type float_type;
28         static public Type double_type;
29         static public Type char_type;
30         static public Type short_type;
31         static public Type decimal_type;
32         static public Type bool_type;
33         static public Type sbyte_type;
34         static public Type byte_type;
35         static public Type ushort_type;
36         static public Type enum_type;
37         static public Type delegate_type;
38         static public Type void_type;
39         
40         // <remarks>
41         //   Holds the Array of Assemblies that have been loaded
42         //   (either because it is the default or the user used the
43         //   -r command line option)
44         // </remarks>
45         ArrayList assemblies;
46
47         // <remarks>
48         //   This is the type_cache from the assemblies to avoid
49         //   hitting System.Reflection on every lookup.
50         // </summary>
51         Hashtable types;
52
53         // <remarks>
54         //  This is used to hotld the corresponding TypeContainer objects
55         //  since we need this in FindMembers
56         // </remarks>
57         Hashtable typecontainers;
58
59         // <remarks>
60         //   Keeps track of those types that are defined by the
61         //   user's program
62         // </remarks>
63         ArrayList user_types;
64
65         public TypeManager ()
66         {
67                 assemblies = new ArrayList ();
68                 user_types = new ArrayList ();
69                 types = new Hashtable ();
70                 typecontainers = new Hashtable ();
71         }
72
73         public void AddUserType (string name, TypeBuilder t, TypeContainer tc)
74         {
75                 types.Add (t.FullName, t);
76                 user_types.Add (t);
77                 typecontainers.Add (t.FullName, tc);
78         }
79
80         public void AddUserType (string name, TypeBuilder t)
81         {
82                 this.AddUserType (name, t, null);
83         }
84         
85         // <summary>
86         //   Registers an assembly to load types from.
87         // </summary>
88         public void AddAssembly (Assembly a)
89         {
90                 assemblies.Add (a);
91         }
92
93         // <summary>
94         //   Returns the Type associated with @name
95         // </summary>
96         public Type LookupType (string name)
97         {
98                 Type t;
99
100                 //
101                 // First lookup in user defined and cached values
102                 //
103
104                 t = (Type) types [name];
105                 if (t != null)
106                         return t;
107
108                 foreach (Assembly a in assemblies){
109                         t = a.GetType (name);
110                         if (t != null){
111                                 types [name] = t;
112
113                                 return t;
114                         }
115                 }
116
117                 return null;
118         }
119
120         // <summary>
121         //   Returns the C# name of a type if possible, or the full type name otherwise
122         // </summary>
123         static public string CSharpName (Type t)
124         {
125                 if (t == int32_type)
126                         return "int";
127                 else if (t == uint32_type)
128                         return "uint";
129                 else if (t == int64_type)
130                         return "long";
131                 else if (t == uint64_type)
132                         return "ulong";
133                 else if (t == float_type)
134                         return "float";
135                 else if (t == double_type)
136                         return "double";
137                 else if (t == char_type)
138                         return "char";
139                 else if (t == short_type)
140                         return "short";
141                 else if (t == decimal_type)
142                         return "decimal";
143                 else if (t == bool_type)
144                         return "bool";
145                 else if (t == sbyte_type)
146                         return "sbyte";
147                 else if (t == byte_type)
148                         return "byte";
149                 else if (t == short_type)
150                         return "short";
151                 else if (t == ushort_type)
152                         return "ushort";
153                 else if (t == System.Type.GetType ("System.String"))
154                         return "string";
155                 else
156                         return t.FullName;
157         }
158
159         Type CoreLookupType (string name)
160         {
161                 Type t = LookupType (name);
162
163                 if (t == null)
164                         throw new Exception ("Can not find core type " + name);
165
166                 return t;
167         }
168                
169         // <remarks>
170         //   The types have to be initialized after the initial
171         //   population of the type has happened (for example, to
172         //   bootstrap the corlib.dll
173         // </remarks>
174         public void InitCoreTypes ()
175         {
176                 object_type   = CoreLookupType ("System.Object");
177                 value_type    = CoreLookupType ("System.ValueType");
178                 string_type   = CoreLookupType ("System.String");
179                 int32_type    = CoreLookupType ("System.Int32");
180                 int64_type    = CoreLookupType ("System.Int64");
181                 uint32_type   = CoreLookupType ("System.UInt32"); 
182                 uint64_type   = CoreLookupType ("System.UInt64"); 
183                 float_type    = CoreLookupType ("System.Single");
184                 double_type   = CoreLookupType ("System.Double");
185                 byte_type     = CoreLookupType ("System.Byte");
186                 sbyte_type    = CoreLookupType ("System.SByte");
187                 char_type     = CoreLookupType ("System.Char");
188                 short_type    = CoreLookupType ("System.Int16");
189                 ushort_type   = CoreLookupType ("System.UInt16");
190                 decimal_type  = CoreLookupType ("System.Decimal");
191                 bool_type     = CoreLookupType ("System.Boolean");
192                 enum_type     = CoreLookupType ("System.Enum");
193                 delegate_type = CoreLookupType ("System.Delegate");
194                 void_type     = CoreLookupType ("System.Void");
195         }
196         
197         public MemberInfo [] FindMembers (Type t, MemberTypes mt, BindingFlags bf, MemberFilter filter, object criteria)
198         {
199                 TypeContainer tc;
200                 
201                 tc = (TypeContainer) typecontainers [t.FullName];
202
203                 if (tc == null)
204                         return t.FindMembers (mt, bf, filter, criteria);
205                 else 
206                         return tc.FindMembers (mt, bf, filter, criteria);
207                 
208         }
209
210         // <summary>
211         //   Returns the User Defined Types
212         // </summary>
213         public ArrayList UserTypes {
214                 get {
215                         return user_types;
216                 }
217         }
218
219         public Hashtable TypeContainers {
220                 get {
221                         return typecontainers;
222                 }
223         }
224
225 }
226
227 }