Debian Codegraph @

mcs/tools/cil-strip/Mono.Cecil/ReflectionHelper.cs

raw
1 //
2 // ReflectionHelper.cs
3 //
4 // Author:
5 // Jb Evain ([email protected])
6 //
7 // (C) 2005 Jb Evain
8 // (C) 2006 Evaluant RC S.A.
9 // (C) 2007 Jb Evain
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 namespace Mono.Cecil {
32
33 using System;
34 using System.Collections;
35 using SR = System.Reflection;
36 using System.Text;
37
38 internal sealed class ReflectionHelper {
39
40 ModuleDefinition m_module;
41
42 public ReflectionHelper (ModuleDefinition module)
43 {
44 m_module = module;
45 }
46
47 public AssemblyNameReference ImportAssembly (SR.Assembly asm)
48 {
49 AssemblyNameReference asmRef = GetAssemblyNameReference (asm.GetName ());
50 if (asmRef != null)
51 return asmRef;
52
53 SR.AssemblyName asmName = asm.GetName ();
54 asmRef = new AssemblyNameReference (
55 asmName.Name, asmName.CultureInfo.Name, asmName.Version);
56 asmRef.PublicKeyToken = asmName.GetPublicKeyToken ();
57 asmRef.HashAlgorithm = (AssemblyHashAlgorithm) asmName.HashAlgorithm;
58 asmRef.Culture = asmName.CultureInfo.ToString ();
59 m_module.AssemblyReferences.Add (asmRef);
60 return asmRef;
61 }
62
63 AssemblyNameReference GetAssemblyNameReference (SR.AssemblyName name)
64 {
65 foreach (AssemblyNameReference reference in m_module.AssemblyReferences)
66 if (reference.FullName == name.FullName)
67 return reference;
68
69 return null;
70 }
71
72 public static string GetTypeSignature (Type t)
73 {
74 if (t.HasElementType) {
75 if (t.IsPointer)
76 return string.Concat (GetTypeSignature (t.GetElementType ()), "*");
77 else if (t.IsArray) {
78 int rank = t.GetArrayRank ();
79 if (rank == 1)
80 return string.Concat (GetTypeSignature (t.GetElementType ()), "[]");
81
82 StringBuilder sb = new StringBuilder ();
83 sb.Append ('[');
84 for (int i = 1; i < rank; i++)
85 sb.Append (',');
86 sb.Append (']');
87
88 return string.Concat (GetTypeSignature (t.GetElementType ()), sb.ToString ());
89 } else if (t.IsByRef)
90 return string.Concat(GetTypeSignature(t.GetElementType()), "&");
91 }
92
93 if (IsGenericTypeSpec (t)) {
94 StringBuilder sb = new StringBuilder ();
95 sb.Append (GetTypeSignature (GetGenericTypeDefinition (t)));
96 sb.Append ("<");
97 Type [] genArgs = GetGenericArguments (t);
98 for (int i = 0; i < genArgs.Length; i++) {
99 if (i > 0)
100 sb.Append (",");
101 sb.Append (GetTypeSignature (genArgs [i]));
102 }
103 sb.Append (">");
104 return sb.ToString ();
105 }
106
107 if (IsGenericParameter (t))
108 return t.Name;
109
110 if (t.DeclaringType != null)
111 return string.Concat (t.DeclaringType.FullName, "/", t.Name);
112
113 if (t.Namespace == null || t.Namespace.Length == 0)
114 return t.Name;
115
116 return string.Concat (t.Namespace, ".", t.Name);
117 }
118
119 static bool GetProperty (object o, string prop)
120 {
121 SR.PropertyInfo pi = o.GetType ().GetProperty (prop);
122 if (pi == null)
123 return false;
124
125 return (bool) pi.GetValue (o, null);
126 }
127
128 public static bool IsGenericType (Type t)
129 {
130 return GetProperty (t, "IsGenericType");
131 }
132
133 static bool IsGenericParameter (Type t)
134 {
135 return GetProperty (t, "IsGenericParameter");
136 }
137
138 static bool IsGenericTypeDefinition (Type t)
139 {
140 return GetProperty (t, "IsGenericTypeDefinition");
141 }
142
143 static bool IsGenericTypeSpec (Type t)
144 {
145 return IsGenericType (t) && !IsGenericTypeDefinition (t);
146 }
147
148 static Type GetGenericTypeDefinition (Type t)
149 {
150 return (Type) t.GetType ().GetMethod ("GetGenericTypeDefinition").Invoke (t, null);
151 }
152
153 static Type [] GetGenericArguments (Type t)
154 {
155 return (Type []) t.GetType ().GetMethod ("GetGenericArguments").Invoke (t, null);
156 }
157
158 GenericInstanceType GetGenericType (Type t, TypeReference element, ImportContext context)
159 {
160 GenericInstanceType git = new GenericInstanceType (element);
161 foreach (Type genArg in GetGenericArguments (t))
162 git.GenericArguments.Add (ImportSystemType (genArg, context));
163
164 return git;
165 }
166
167 static bool GenericParameterOfMethod (Type t)
168 {
169 return t.GetType ().GetProperty ("DeclaringMethod").GetValue (t, null) != null;
170 }
171
172 static GenericParameter GetGenericParameter (Type t, ImportContext context)
173 {
174 int pos = (int) t.GetType ().GetProperty ("GenericParameterPosition").GetValue (t, null);
175 IGenericParameterProvider provider;
176 if (GenericParameterOfMethod (t))
177 provider = context.GenericContext.Method;
178 else
179 provider = context.GenericContext.Type;
180
181 if (provider == null)
182 throw new InvalidOperationException ("Invalid context");
183
184 return provider.GenericParameters [pos];
185 }
186
187 TypeReference GetTypeSpec (Type t, ImportContext context)
188 {
189 Stack s = new Stack ();
190 while (t.HasElementType || IsGenericTypeSpec (t)) {
191 s.Push (t);
192 if (t.HasElementType)
193 t = t.GetElementType ();
194 else if (IsGenericTypeSpec (t)) {
195 t = (Type) t.GetType ().GetMethod ("GetGenericTypeDefinition").Invoke (t, null);
196 break;
197 }
198 }
199
200 TypeReference elementType = ImportSystemType (t, context);
201 while (s.Count > 0) {
202 t = (Type) s.Pop ();
203 if (t.IsPointer)
204 elementType = new PointerType (elementType);
205 else if (t.IsArray)
206 elementType = new ArrayType (elementType, t.GetArrayRank ());
207 else if (t.IsByRef)
208 elementType = new ReferenceType (elementType);
209 else if (IsGenericTypeSpec (t))
210 elementType = GetGenericType (t, elementType, context);
211 else
212 throw new ReflectionException ("Unknown element type");
213 }
214
215 return elementType;
216 }
217
218 TypeReference AdjustReference (Type type, TypeReference reference)
219 {
220 if (type.IsValueType && !reference.IsValueType)
221 reference.IsValueType = true;
222
223 if (IsGenericTypeDefinition (type)) {
224 Type [] parameters = GetGenericArguments (type);
225 for (int i = reference.GenericParameters.Count; i < parameters.Length; i++)
226 reference.GenericParameters.Add (new GenericParameter (i, reference));
227 }
228
229 return reference;
230 }
231
232 public TypeReference ImportSystemType (Type t, ImportContext context)
233 {
234 if (t.HasElementType || IsGenericTypeSpec (t))
235 return GetTypeSpec (t, context);
236
237 if (IsGenericParameter (t))
238 return GetGenericParameter (t, context);
239
240 TypeReference type = m_module.TypeReferences [GetTypeSignature (t)];
241 if (type != null)
242 return AdjustReference (t, type);
243
244 AssemblyNameReference asm = ImportAssembly (t.Assembly);
245 if (t.DeclaringType != null) {
246 type = new TypeReference (t.Name, string.Empty, asm, t.IsValueType);
247 type.DeclaringType = ImportSystemType (t.DeclaringType, context);
248 } else
249 type = new TypeReference (t.Name, t.Namespace, asm, t.IsValueType);
250
251 if (IsGenericTypeDefinition (t))
252 foreach (Type genParam in GetGenericArguments (t))
253 type.GenericParameters.Add (new GenericParameter (genParam.Name, type));
254
255 context.GenericContext.Type = type;
256
257 m_module.TypeReferences.Add (type);
258 return type;
259 }
260
261 static string GetMethodBaseSignature (SR.MethodBase meth, Type declaringType, Type retType)
262 {
263 StringBuilder sb = new StringBuilder ();
264 sb.Append (GetTypeSignature (retType));
265 sb.Append (' ');
266 sb.Append (GetTypeSignature (declaringType));
267 sb.Append ("::");
268 sb.Append (meth.Name);
269 if (IsGenericMethodSpec (meth)) {
270 sb.Append ("<");
271 Type [] genArgs = GetGenericArguments (meth as SR.MethodInfo);
272 for (int i = 0; i < genArgs.Length; i++) {
273 if (i > 0)
274 sb.Append (",");
275 sb.Append (GetTypeSignature (genArgs [i]));
276 }
277 sb.Append (">");
278 }
279 sb.Append ("(");
280 SR.ParameterInfo [] parameters = meth.GetParameters ();
281 for (int i = 0; i < parameters.Length; i++) {
282 if (i > 0)
283 sb.Append (",");
284 sb.Append (GetTypeSignature (parameters [i].ParameterType));
285 }
286 sb.Append (")");
287 return sb.ToString ();
288 }
289
290 static bool IsGenericMethod (SR.MethodBase mb)
291 {
292 return GetProperty (mb, "IsGenericMethod");
293 }
294
295 static bool IsGenericMethodDefinition (SR.MethodBase mb)
296 {
297 return GetProperty (mb, "IsGenericMethodDefinition");
298 }
299
300 static bool IsGenericMethodSpec (SR.MethodBase mb)
301 {
302 return IsGenericMethod (mb) && !IsGenericMethodDefinition (mb);
303 }
304
305 static Type [] GetGenericArguments (SR.MethodInfo mi)
306 {
307 return (Type []) mi.GetType ().GetMethod ("GetGenericArguments").Invoke (mi, null);
308 }
309
310 static int GetMetadataToken (SR.MethodInfo mi)
311 {
312 return (int) mi.GetType ().GetProperty ("MetadataToken").GetValue (mi, null);
313 }
314
315 MethodReference ImportGenericInstanceMethod (SR.MethodInfo mi, ImportContext context)
316 {
317 SR.MethodInfo gmd = (SR.MethodInfo) mi.GetType ().GetMethod ("GetGenericMethodDefinition").Invoke (mi, null);
318 GenericInstanceMethod gim = new GenericInstanceMethod (
319 ImportMethodBase (gmd, gmd.ReturnType, context));
320
321 foreach (Type genArg in GetGenericArguments (mi))
322 gim.GenericArguments.Add (ImportSystemType (genArg, context));
323
324 return gim;
325 }
326
327 MethodReference ImportMethodBase (SR.MethodBase mb, Type retType, ImportContext context)
328 {
329 if (IsGenericMethod (mb) && !IsGenericMethodDefinition (mb))
330 return ImportGenericInstanceMethod ((SR.MethodInfo) mb, context);
331
332 Type originalDecType = mb.DeclaringType;
333 Type declaringTypeDef = originalDecType;
334 while (IsGenericTypeSpec (declaringTypeDef))
335 declaringTypeDef = GetGenericTypeDefinition (declaringTypeDef);
336
337 if (mb.DeclaringType != declaringTypeDef && mb is SR.MethodInfo) {
338 int mt = GetMetadataToken (mb as SR.MethodInfo);
339 // hack to get the generic method definition from the constructed method
340 foreach (SR.MethodInfo mi in declaringTypeDef.GetMethods ()) {
341 if (GetMetadataToken (mi) == mt) {
342 mb = mi;
343 retType = mi.ReturnType;
344 break;
345 }
346 }
347 }
348
349 string sig = GetMethodBaseSignature (mb, originalDecType, retType);
350 MethodReference meth = (MethodReference) GetMemberReference (sig);
351 if (meth != null)
352 return meth;
353
354 meth = new MethodReference (
355 mb.Name,
356 (mb.CallingConvention & SR.CallingConventions.HasThis) > 0,
357 (mb.CallingConvention & SR.CallingConventions.ExplicitThis) > 0,
358 MethodCallingConvention.Default); // TODO: get the real callconv
359 meth.DeclaringType = ImportSystemType (originalDecType, context);
360
361 if (IsGenericMethod (mb))
362 foreach (Type genParam in GetGenericArguments (mb as SR.MethodInfo))
363 meth.GenericParameters.Add (new GenericParameter (genParam.Name, meth));
364
365 TypeReference contextType = context.GenericContext.Type;
366 MethodReference contextMethod = context.GenericContext.Method;
367
368 context.GenericContext.Method = meth;
369 context.GenericContext.Type = ImportSystemType (declaringTypeDef, context);
370
371 meth.ReturnType.ReturnType = ImportSystemType (retType, context);
372
373 SR.ParameterInfo [] parameters = mb.GetParameters ();
374 for (int i = 0; i < parameters.Length; i++)
375 meth.Parameters.Add (new ParameterDefinition (
376 ImportSystemType (parameters [i].ParameterType, context)));
377
378 context.GenericContext.Type = contextType;
379 context.GenericContext.Method = contextMethod;
380
381 m_module.MemberReferences.Add (meth);
382 return meth;
383 }
384
385 public MethodReference ImportConstructorInfo (SR.ConstructorInfo ci, ImportContext context)
386 {
387 return ImportMethodBase (ci, typeof (void), context);
388 }
389
390 public MethodReference ImportMethodInfo (SR.MethodInfo mi, ImportContext context)
391 {
392 return ImportMethodBase (mi, mi.ReturnType, context);
393 }
394
395 static string GetFieldSignature (SR.FieldInfo field)
396 {
397 StringBuilder sb = new StringBuilder ();
398 sb.Append (GetTypeSignature (field.FieldType));
399 sb.Append (' ');
400 sb.Append (GetTypeSignature (field.DeclaringType));
401 sb.Append ("::");
402 sb.Append (field.Name);
403 return sb.ToString ();
404 }
405
406 public FieldReference ImportFieldInfo (SR.FieldInfo fi, ImportContext context)
407 {
408 string sig = GetFieldSignature (fi);
409 FieldReference f = (FieldReference) GetMemberReference (sig);
410 if (f != null)
411 return f;
412
413 f = new FieldReference (
414 fi.Name,
415 ImportSystemType (fi.DeclaringType, context),
416 ImportSystemType (fi.FieldType, context));
417
418 m_module.MemberReferences.Add (f);
419 return f;
420 }
421
422 MemberReference GetMemberReference (string signature)
423 {
424 foreach (MemberReference reference in m_module.MemberReferences)
425 if (reference.ToString () == signature)
426 return reference;
427
428 return null;
429 }
430 }
431 }