Use handle macros to avoid leaking result of mono_type_get_object_handle
[mono.git] / mono / metadata / reflection.c
1 /**
2  * \file
3  * System.Type icalls and related reflection queries.
4  * 
5  * Author:
6  *   Paolo Molaro (lupus@ximian.com)
7  *
8  * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
9  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
10  * Copyright 2011 Rodrigo Kumpera
11  * Copyright 2016 Microsoft
12  *
13  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
14  */
15 #include <config.h>
16 #include "mono/utils/mono-membar.h"
17 #include "mono/metadata/reflection-internals.h"
18 #include "mono/metadata/tabledefs.h"
19 #include "mono/metadata/metadata-internals.h"
20 #include <mono/metadata/profiler-private.h>
21 #include "mono/metadata/class-internals.h"
22 #include "mono/metadata/gc-internals.h"
23 #include "mono/metadata/domain-internals.h"
24 #include "mono/metadata/opcodes.h"
25 #include "mono/metadata/assembly.h"
26 #include "mono/metadata/object-internals.h"
27 #include <mono/metadata/exception.h>
28 #include <mono/metadata/marshal.h>
29 #include <mono/metadata/security-manager.h>
30 #include <mono/metadata/reflection-cache.h>
31 #include <mono/metadata/sre-internals.h>
32 #include <stdio.h>
33 #include <glib.h>
34 #include <errno.h>
35 #include <time.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include "image.h"
39 #include "cil-coff.h"
40 #include "mono-endian.h"
41 #include <mono/metadata/gc-internals.h>
42 #include <mono/metadata/mempool-internals.h>
43 #include <mono/metadata/security-core-clr.h>
44 #include <mono/metadata/debug-helpers.h>
45 #include <mono/metadata/verify-internals.h>
46 #include <mono/metadata/mono-ptr-array.h>
47 #include <mono/utils/mono-string.h>
48 #include <mono/utils/mono-error-internals.h>
49 #include <mono/utils/checked-build.h>
50 #include <mono/utils/mono-counters.h>
51
52 static void get_default_param_value_blobs (MonoMethod *method, char **blobs, guint32 *types);
53 static MonoType* mono_reflection_get_type_with_rootimage (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve, MonoError *error);
54
55 /* Class lazy loading functions */
56 static GENERATE_GET_CLASS_WITH_CACHE (mono_assembly, "System.Reflection", "MonoAssembly")
57 static GENERATE_GET_CLASS_WITH_CACHE (mono_module, "System.Reflection", "MonoModule")
58 static GENERATE_GET_CLASS_WITH_CACHE (mono_method, "System.Reflection", "MonoMethod");
59 static GENERATE_GET_CLASS_WITH_CACHE (mono_cmethod, "System.Reflection", "MonoCMethod");
60 static GENERATE_GET_CLASS_WITH_CACHE (mono_field, "System.Reflection", "MonoField");
61 static GENERATE_GET_CLASS_WITH_CACHE (mono_event, "System.Reflection", "MonoEvent");
62 static GENERATE_GET_CLASS_WITH_CACHE (mono_property, "System.Reflection", "MonoProperty");
63 static GENERATE_GET_CLASS_WITH_CACHE (mono_parameter_info, "System.Reflection", "MonoParameterInfo");
64 static GENERATE_GET_CLASS_WITH_CACHE (missing, "System.Reflection", "Missing");
65 static GENERATE_GET_CLASS_WITH_CACHE (method_body, "System.Reflection", "MethodBody");
66 static GENERATE_GET_CLASS_WITH_CACHE (local_variable_info, "System.Reflection", "LocalVariableInfo");
67 static GENERATE_GET_CLASS_WITH_CACHE (exception_handling_clause, "System.Reflection", "ExceptionHandlingClause");
68 static GENERATE_GET_CLASS_WITH_CACHE (type_builder, "System.Reflection.Emit", "TypeBuilder");
69 static GENERATE_GET_CLASS_WITH_CACHE (dbnull, "System", "DBNull");
70
71
72 static int class_ref_info_handle_count;
73
74 void
75 mono_reflection_init (void)
76 {
77         mono_reflection_emit_init ();
78
79         mono_counters_register ("MonoClass::ref_info_handle count",
80                                                         MONO_COUNTER_METADATA | MONO_COUNTER_INT, &class_ref_info_handle_count);
81
82 }
83
84 /*
85  * mono_class_get_ref_info:
86  *
87  *   Return the type builder/generic param builder corresponding to KLASS, if it exists.
88  */
89 MonoObjectHandle
90 mono_class_get_ref_info (MonoClass *klass)
91 {
92         MONO_REQ_GC_UNSAFE_MODE;
93         guint32 ref_info_handle = mono_class_get_ref_info_handle (klass);
94
95         if (ref_info_handle == 0)
96                 return MONO_HANDLE_NEW (MonoObject, NULL);
97         else
98                 return mono_gchandle_get_target_handle (ref_info_handle);
99 }
100
101 gboolean
102 mono_class_has_ref_info (MonoClass *klass)
103 {
104         MONO_REQ_GC_UNSAFE_MODE;
105         return 0 != mono_class_get_ref_info_handle (klass);
106 }
107
108 MonoObject*
109 mono_class_get_ref_info_raw (MonoClass *klass)
110 {
111         /* FIXME callers of mono_class_get_ref_info_raw should use handles */
112         MONO_REQ_GC_UNSAFE_MODE;
113         guint32 ref_info_handle = mono_class_get_ref_info_handle (klass);
114
115         if (ref_info_handle == 0)
116                 return NULL;
117         else
118                 return mono_gchandle_get_target (ref_info_handle);
119 }
120
121 void
122 mono_class_set_ref_info (MonoClass *klass, MonoObjectHandle obj)
123 {
124         MONO_REQ_GC_UNSAFE_MODE;
125
126         guint32 candidate = mono_gchandle_from_handle (obj, FALSE);
127         guint32 handle = mono_class_set_ref_info_handle (klass, candidate);
128         ++class_ref_info_handle_count;
129
130         if (handle != candidate)
131                 mono_gchandle_free (candidate);
132 }
133
134 void
135 mono_class_free_ref_info (MonoClass *klass)
136 {
137         MONO_REQ_GC_NEUTRAL_MODE;
138         guint32 handle = mono_class_get_ref_info_handle (klass);
139
140         if (handle) {
141                 mono_gchandle_free (handle);
142                 mono_class_set_ref_info_handle (klass, 0);
143         }
144 }
145
146
147 /**
148  * mono_custom_attrs_free:
149  */
150 void
151 mono_custom_attrs_free (MonoCustomAttrInfo *ainfo)
152 {
153         MONO_REQ_GC_NEUTRAL_MODE;
154
155         if (ainfo && !ainfo->cached)
156                 g_free (ainfo);
157 }
158
159
160 gboolean
161 reflected_equal (gconstpointer a, gconstpointer b)
162 {
163         const ReflectedEntry *ea = (const ReflectedEntry *)a;
164         const ReflectedEntry *eb = (const ReflectedEntry *)b;
165
166         return (ea->item == eb->item) && (ea->refclass == eb->refclass);
167 }
168
169 guint
170 reflected_hash (gconstpointer a) {
171         const ReflectedEntry *ea = (const ReflectedEntry *)a;
172         /* Combine hashes for item and refclass. Identical to boost's hash_combine */
173         guint seed = mono_aligned_addr_hash (ea->item) + 0x9e3779b9;
174         seed ^= mono_aligned_addr_hash (ea->refclass) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
175         return seed;
176 }
177
178
179 static void
180 clear_cached_object (MonoDomain *domain, gpointer o, MonoClass *klass)
181 {
182         mono_domain_lock (domain);
183         if (domain->refobject_hash) {
184         ReflectedEntry pe;
185                 gpointer orig_pe, orig_value;
186
187                 pe.item = o;
188                 pe.refclass = klass;
189                 if (mono_g_hash_table_lookup_extended (domain->refobject_hash, &pe, &orig_pe, &orig_value)) {
190                         mono_g_hash_table_remove (domain->refobject_hash, &pe);
191                         FREE_REFENTRY (orig_pe);
192                 }
193         }
194         mono_domain_unlock (domain);
195 }
196
197 #ifdef REFENTRY_REQUIRES_CLEANUP
198 static void
199 cleanup_refobject_hash (gpointer key, gpointer value, gpointer user_data)
200 {
201         FREE_REFENTRY (key);
202 }
203 #endif
204
205 void
206 mono_reflection_cleanup_domain (MonoDomain *domain)
207 {
208         if (domain->refobject_hash) {
209 /*let's avoid scanning the whole hashtable if not needed*/
210 #ifdef REFENTRY_REQUIRES_CLEANUP
211                 mono_g_hash_table_foreach (domain->refobject_hash, cleanup_refobject_hash, NULL);
212 #endif
213                 mono_g_hash_table_destroy (domain->refobject_hash);
214                 domain->refobject_hash = NULL;
215         }
216 }
217
218
219 /**
220  * mono_assembly_get_object:
221  * \param domain an app domain
222  * \param assembly an assembly
223  * \returns a \c System.Reflection.Assembly object representing the \c MonoAssembly \p assembly.
224  */
225 MonoReflectionAssembly*
226 mono_assembly_get_object (MonoDomain *domain, MonoAssembly *assembly)
227 {
228         HANDLE_FUNCTION_ENTER ();
229         MonoError error;
230         MonoReflectionAssemblyHandle result = mono_assembly_get_object_handle (domain, assembly, &error);
231         mono_error_cleanup (&error); /* FIXME new API that doesn't swallow the error */
232         HANDLE_FUNCTION_RETURN_OBJ (result);
233 }
234
235 static MonoReflectionAssemblyHandle
236 assembly_object_construct (MonoDomain *domain, MonoClass *unused_klass, MonoAssembly *assembly, gpointer user_data, MonoError *error)
237 {
238         error_init (error);
239         MonoReflectionAssemblyHandle res = MONO_HANDLE_NEW (MonoReflectionAssembly, mono_object_new_checked (domain, mono_class_get_mono_assembly_class (), error));
240         return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionAssembly, NULL_HANDLE));
241         MONO_HANDLE_SETVAL (res, assembly, MonoAssembly*, assembly);
242         return res;
243 }
244
245 /*
246  * mono_assembly_get_object_handle:
247  * @domain: an app domain
248  * @assembly: an assembly
249  *
250  * Return an System.Reflection.Assembly object representing the MonoAssembly @assembly.
251  */
252 MonoReflectionAssemblyHandle
253 mono_assembly_get_object_handle (MonoDomain *domain, MonoAssembly *assembly, MonoError *error)
254 {
255         error_init (error);
256         return CHECK_OR_CONSTRUCT_HANDLE (MonoReflectionAssemblyHandle, assembly, NULL, assembly_object_construct, NULL);
257 }
258
259 /**
260  * mono_module_get_object:
261  */
262 MonoReflectionModule*   
263 mono_module_get_object   (MonoDomain *domain, MonoImage *image)
264 {
265         HANDLE_FUNCTION_ENTER ();
266         MonoError error;
267         MonoReflectionModuleHandle result = mono_module_get_object_handle (domain, image, &error);
268         mono_error_cleanup (&error);
269         HANDLE_FUNCTION_RETURN_OBJ (result);
270 }
271
272 static MonoReflectionModuleHandle
273 module_object_construct (MonoDomain *domain, MonoClass *unused_klass, MonoImage *image, gpointer user_data, MonoError *error)
274 {
275         char* basename;
276         
277         error_init (error);
278         MonoReflectionModuleHandle res = MONO_HANDLE_NEW (MonoReflectionModule, mono_object_new_checked (domain, mono_class_get_mono_module_class (), error));
279         if (!is_ok (error))
280                 goto fail;
281
282         MONO_HANDLE_SETVAL (res, image, MonoImage *, image);
283         MonoReflectionAssemblyHandle assm_obj = mono_assembly_get_object_handle (domain, image->assembly, error);
284         if (!is_ok (error))
285                 goto fail;
286         MONO_HANDLE_SET (res, assembly, assm_obj);
287
288         MONO_HANDLE_SET (res, fqname, mono_string_new_handle (domain, image->name, error));
289         if (!is_ok (error))
290                 goto fail;
291         basename = g_path_get_basename (image->name);
292         MONO_HANDLE_SET (res, name, mono_string_new_handle (domain, basename, error));
293         if (!is_ok (error))
294                 goto fail;
295         MONO_HANDLE_SET (res, scopename, mono_string_new_handle (domain, image->module_name, error));
296         if (!is_ok (error))
297                 goto fail;
298
299         g_free (basename);
300
301         guint32 token = 0;
302         if (image->assembly->image == image) {
303                 token  = mono_metadata_make_token (MONO_TABLE_MODULE, 1);
304         } else {
305                 int i;
306                 if (image->assembly->image->modules) {
307                         for (i = 0; i < image->assembly->image->module_count; i++) {
308                                 if (image->assembly->image->modules [i] == image)
309                                         token = mono_metadata_make_token (MONO_TABLE_MODULEREF, i + 1);
310                         }
311                         g_assert (token != 0);
312                 }
313         }
314         MONO_HANDLE_SETVAL (res, token, guint32, token);
315
316         return res;
317 fail:
318         return MONO_HANDLE_CAST (MonoReflectionModule, NULL_HANDLE);
319 }
320
321 MonoReflectionModuleHandle
322 mono_module_get_object_handle (MonoDomain *domain, MonoImage *image, MonoError *error)
323 {
324         error_init (error);
325         return CHECK_OR_CONSTRUCT_HANDLE (MonoReflectionModuleHandle, image, NULL, module_object_construct, NULL);
326 }
327
328 /**
329  * mono_module_file_get_object:
330  */
331 MonoReflectionModule*
332 mono_module_file_get_object (MonoDomain *domain, MonoImage *image, int table_index)
333 {
334         HANDLE_FUNCTION_ENTER ();
335         MonoError error;
336         MonoReflectionModuleHandle result = mono_module_file_get_object_handle (domain, image, table_index, &error);
337         mono_error_cleanup (&error);
338         HANDLE_FUNCTION_RETURN_OBJ (result);
339 }
340
341 MonoReflectionModuleHandle
342 mono_module_file_get_object_handle (MonoDomain *domain, MonoImage *image, int table_index, MonoError *error)
343 {
344         MonoTableInfo *table;
345         guint32 cols [MONO_FILE_SIZE];
346         const char *name;
347         guint32 i, name_idx;
348         const char *val;
349         
350         error_init (error);
351
352         MonoReflectionModuleHandle res = MONO_HANDLE_NEW (MonoReflectionModule, mono_object_new_checked (domain, mono_class_get_mono_module_class (), error));
353         if (!is_ok (error))
354                 goto fail;
355
356         table = &image->tables [MONO_TABLE_FILE];
357         g_assert (table_index < table->rows);
358         mono_metadata_decode_row (table, table_index, cols, MONO_FILE_SIZE);
359
360         MONO_HANDLE_SETVAL (res, image, MonoImage*, NULL);
361         MonoReflectionAssemblyHandle assm_obj = mono_assembly_get_object_handle (domain, image->assembly, error);
362         if (!is_ok (error))
363                 goto fail;
364         MONO_HANDLE_SET (res, assembly, assm_obj);
365         name = mono_metadata_string_heap (image, cols [MONO_FILE_NAME]);
366
367         /* Check whenever the row has a corresponding row in the moduleref table */
368         table = &image->tables [MONO_TABLE_MODULEREF];
369         for (i = 0; i < table->rows; ++i) {
370                 name_idx = mono_metadata_decode_row_col (table, i, MONO_MODULEREF_NAME);
371                 val = mono_metadata_string_heap (image, name_idx);
372                 if (strcmp (val, name) == 0)
373                         MONO_HANDLE_SETVAL (res, image, MonoImage*, image->modules [i]);
374         }
375
376         MONO_HANDLE_SET (res, fqname, mono_string_new_handle (domain, name, error));
377         if (!is_ok (error))
378                 goto fail;
379         MONO_HANDLE_SET (res, name, mono_string_new_handle (domain, name, error));
380         if (!is_ok (error))
381                 goto fail;
382         MONO_HANDLE_SET (res, scopename, mono_string_new_handle (domain, name, error));
383         if (!is_ok (error))
384                 goto fail;
385         MONO_HANDLE_SETVAL (res, is_resource, MonoBoolean, cols [MONO_FILE_FLAGS] & FILE_CONTAINS_NO_METADATA);
386         MONO_HANDLE_SETVAL (res, token, guint32, mono_metadata_make_token (MONO_TABLE_FILE, table_index + 1));
387
388         return res;
389 fail:
390         return MONO_HANDLE_CAST (MonoReflectionModule, NULL_HANDLE);
391 }
392
393 static MonoType*
394 mono_type_normalize (MonoType *type)
395 {
396         int i;
397         MonoGenericClass *gclass;
398         MonoGenericInst *ginst;
399         MonoClass *gtd;
400         MonoGenericContainer *gcontainer;
401         MonoType **argv = NULL;
402         gboolean is_denorm_gtd = TRUE, requires_rebind = FALSE;
403
404         if (type->type != MONO_TYPE_GENERICINST)
405                 return type;
406
407         gclass = type->data.generic_class;
408         ginst = gclass->context.class_inst;
409         if (!ginst->is_open)
410                 return type;
411
412         gtd = gclass->container_class;
413         gcontainer = mono_class_get_generic_container (gtd);
414         argv = g_newa (MonoType*, ginst->type_argc);
415
416         for (i = 0; i < ginst->type_argc; ++i) {
417                 MonoType *t = ginst->type_argv [i], *norm;
418                 if (t->type != MONO_TYPE_VAR || t->data.generic_param->num != i || t->data.generic_param->owner != gcontainer)
419                         is_denorm_gtd = FALSE;
420                 norm = mono_type_normalize (t);
421                 argv [i] = norm;
422                 if (norm != t)
423                         requires_rebind = TRUE;
424         }
425
426         if (is_denorm_gtd)
427                 return type->byref == gtd->byval_arg.byref ? &gtd->byval_arg : &gtd->this_arg;
428
429         if (requires_rebind) {
430                 MonoClass *klass = mono_class_bind_generic_parameters (gtd, ginst->type_argc, argv, gclass->is_dynamic);
431                 return type->byref == klass->byval_arg.byref ? &klass->byval_arg : &klass->this_arg;
432         }
433
434         return type;
435 }
436
437 /**
438  * mono_type_get_object:
439  * \param domain an app domain
440  * \param type a type
441  * \returns A \c System.MonoType object representing the type \p type.
442  */
443 MonoReflectionType*
444 mono_type_get_object (MonoDomain *domain, MonoType *type)
445 {
446         MonoError error;
447         MonoReflectionType *ret = mono_type_get_object_checked (domain, type, &error);
448         mono_error_cleanup (&error);
449
450         return ret;
451 }
452
453 MonoReflectionType*
454 mono_type_get_object_checked (MonoDomain *domain, MonoType *type, MonoError *error)
455 {
456         MonoType *norm_type;
457         MonoReflectionType *res;
458         MonoClass *klass;
459
460         error_init (error);
461
462         g_assert (type != NULL);
463         klass = mono_class_from_mono_type (type);
464
465         /*we must avoid using @type as it might have come
466          * from a mono_metadata_type_dup and the caller
467          * expects that is can be freed.
468          * Using the right type from 
469          */
470         type = klass->byval_arg.byref == type->byref ? &klass->byval_arg : &klass->this_arg;
471
472         /* void is very common */
473         if (type->type == MONO_TYPE_VOID && domain->typeof_void)
474                 return (MonoReflectionType*)domain->typeof_void;
475
476         /*
477          * If the vtable of the given class was already created, we can use
478          * the MonoType from there and avoid all locking and hash table lookups.
479          * 
480          * We cannot do this for TypeBuilders as mono_reflection_create_runtime_class expects
481          * that the resulting object is different.   
482          */
483         if (type == &klass->byval_arg && !image_is_dynamic (klass->image)) {
484                 MonoVTable *vtable = mono_class_try_get_vtable (domain, klass);
485                 if (vtable && vtable->type)
486                         return (MonoReflectionType *)vtable->type;
487         }
488
489         mono_loader_lock (); /*FIXME mono_class_init and mono_class_vtable acquire it*/
490         mono_domain_lock (domain);
491         if (!domain->type_hash)
492                 domain->type_hash = mono_g_hash_table_new_type ((GHashFunc)mono_metadata_type_hash, 
493                                 (GCompareFunc)mono_metadata_type_equal, MONO_HASH_VALUE_GC, MONO_ROOT_SOURCE_DOMAIN, "domain reflection types table");
494         if ((res = (MonoReflectionType *)mono_g_hash_table_lookup (domain->type_hash, type))) {
495                 mono_domain_unlock (domain);
496                 mono_loader_unlock ();
497                 return res;
498         }
499
500         /*Types must be normalized so a generic instance of the GTD get's the same inner type.
501          * For example in: Foo<A,B>; Bar<A> : Foo<A, Bar<A>>
502          * The second Bar will be encoded a generic instance of Bar with <A> as parameter.
503          * On all other places, Bar<A> will be encoded as the GTD itself. This is an implementation
504          * artifact of how generics are encoded and should be transparent to managed code so we
505          * need to weed out this diference when retrieving managed System.Type objects.
506          */
507         norm_type = mono_type_normalize (type);
508         if (norm_type != type) {
509                 res = mono_type_get_object_checked (domain, norm_type, error);
510                 if (!mono_error_ok (error))
511                         return NULL;
512                 mono_g_hash_table_insert (domain->type_hash, type, res);
513                 mono_domain_unlock (domain);
514                 mono_loader_unlock ();
515                 return res;
516         }
517
518         if ((type->type == MONO_TYPE_GENERICINST) && type->data.generic_class->is_dynamic && !type->data.generic_class->container_class->wastypebuilder) {
519                 /* This can happen if a TypeBuilder for a generic class K<T,U>
520                  * had reflection_create_generic_class) called on it, but not
521                  * ves_icall_TypeBuilder_create_runtime_class.  This can happen
522                  * if the K`2 is refernced from a generic instantiation
523                  * (e.g. K<int,string>) that appears as type argument
524                  * (e.g. Dict<string,K<int,string>>), field (e.g. K<int,string>
525                  * Foo) or method signature, parent class or any of the above
526                  * in a nested class of some other TypeBuilder.  Such an
527                  * occurrence caused mono_reflection_type_get_handle to be
528                  * called on the sre generic instance (K<int,string>) which
529                  * required the container_class for the generic class K`2 to be
530                  * set up, but the remainder of class construction for K`2 has
531                  * not been done. */
532                 char * full_name = mono_type_get_full_name (klass);
533                 /* I would have expected ReflectionTypeLoadException, but evidently .NET throws TLE in this case. */
534                 mono_error_set_type_load_class (error, klass, "TypeBuilder.CreateType() not called for generic class %s", full_name);
535                 g_free (full_name);
536                 mono_domain_unlock (domain);
537                 mono_loader_unlock ();
538                 return NULL;
539         }
540
541         if (mono_class_has_ref_info (klass) && !klass->wastypebuilder && !type->byref) {
542                 mono_domain_unlock (domain);
543                 mono_loader_unlock ();
544                 return (MonoReflectionType *)mono_class_get_ref_info_raw (klass); /* FIXME use handles */
545         }
546         /* This is stored in vtables/JITted code so it has to be pinned */
547         res = (MonoReflectionType *)mono_object_new_pinned (domain, mono_defaults.runtimetype_class, error);
548         if (!mono_error_ok (error))
549                 return NULL;
550
551         res->type = type;
552         mono_g_hash_table_insert (domain->type_hash, type, res);
553
554         if (type->type == MONO_TYPE_VOID)
555                 domain->typeof_void = (MonoObject*)res;
556
557         mono_domain_unlock (domain);
558         mono_loader_unlock ();
559         return res;
560 }
561
562 MonoReflectionTypeHandle
563 mono_type_get_object_handle (MonoDomain *domain, MonoType *type, MonoError *error)
564 {
565         /* NOTE: We happen to know that mono_type_get_object_checked returns
566          * pinned objects, so we can just wrap its return value in a handle for
567          * uniformity.  If it ever starts returning unpinned, objects, this
568          * implementation would need to change!
569          */
570         return MONO_HANDLE_NEW (MonoReflectionType, mono_type_get_object_checked (domain, type, error));
571 }
572
573 /**
574  * mono_method_get_object:
575  * \param domain an app domain
576  * \param method a method
577  * \param refclass the reflected type (can be NULL)
578  * \returns A \c System.Reflection.MonoMethod object representing the method \p method.
579  */
580 MonoReflectionMethod*
581 mono_method_get_object (MonoDomain *domain, MonoMethod *method, MonoClass *refclass)
582 {
583         HANDLE_FUNCTION_ENTER ();
584         MonoError error;
585         MonoReflectionMethodHandle ret = mono_method_get_object_handle (domain, method, refclass, &error);
586         mono_error_cleanup (&error);
587         HANDLE_FUNCTION_RETURN_OBJ (ret);
588 }
589
590 static MonoReflectionMethodHandle
591 method_object_construct (MonoDomain *domain, MonoClass *refclass, MonoMethod *method, gpointer user_data, MonoError *error)
592 {
593         error_init (error);
594         g_assert (refclass != NULL);
595         /*
596          * We use the same C representation for methods and constructors, but the type 
597          * name in C# is different.
598          */
599         MonoClass *klass;
600
601         error_init (error);
602
603         if (*method->name == '.' && (strcmp (method->name, ".ctor") == 0 || strcmp (method->name, ".cctor") == 0)) {
604                 klass = mono_class_get_mono_cmethod_class ();
605         }
606         else {
607                 klass = mono_class_get_mono_method_class ();
608         }
609         MonoReflectionMethodHandle ret = MONO_HANDLE_NEW (MonoReflectionMethod, mono_object_new_checked (domain, klass, error));
610         if (!is_ok (error))
611                 goto fail;
612         MONO_HANDLE_SETVAL (ret, method, MonoMethod*, method);
613
614         MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, &refclass->byval_arg, error);
615         if (!is_ok (error))
616                 goto fail;
617
618         MONO_HANDLE_SET (ret, reftype, rt);
619
620         return ret;
621
622 fail:
623         return MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE);
624 }
625
626 /*
627  * mono_method_get_object_handle:
628  * @domain: an app domain
629  * @method: a method
630  * @refclass: the reflected type (can be NULL)
631  * @error: set on error.
632  *
633  * Return an System.Reflection.MonoMethod object representing the method @method.
634  * Returns NULL and sets @error on error.
635  */
636 MonoReflectionMethodHandle
637 mono_method_get_object_handle (MonoDomain *domain, MonoMethod *method, MonoClass *refclass, MonoError *error)
638 {
639         error_init (error);
640         if (!refclass)
641                 refclass = method->klass;
642
643         return CHECK_OR_CONSTRUCT_HANDLE (MonoReflectionMethodHandle, method, refclass, method_object_construct, NULL);
644 }
645 /*
646  * mono_method_get_object_checked:
647  * @domain: an app domain
648  * @method: a method
649  * @refclass: the reflected type (can be NULL)
650  * @error: set on error.
651  *
652  * Return an System.Reflection.MonoMethod object representing the method @method.
653  * Returns NULL and sets @error on error.
654  */
655 MonoReflectionMethod*
656 mono_method_get_object_checked (MonoDomain *domain, MonoMethod *method, MonoClass *refclass, MonoError *error)
657 {
658         HANDLE_FUNCTION_ENTER ();
659         MonoReflectionMethodHandle result = mono_method_get_object_handle (domain, method, refclass, error);
660         HANDLE_FUNCTION_RETURN_OBJ (result);
661 }
662
663 /*
664  * mono_method_clear_object:
665  *
666  *   Clear the cached reflection objects for the dynamic method METHOD.
667  */
668 void
669 mono_method_clear_object (MonoDomain *domain, MonoMethod *method)
670 {
671         MonoClass *klass;
672         g_assert (method_is_dynamic (method));
673
674         klass = method->klass;
675         while (klass) {
676                 clear_cached_object (domain, method, klass);
677                 klass = klass->parent;
678         }
679         /* Added by mono_param_get_objects () */
680         clear_cached_object (domain, &(method->signature), NULL);
681         klass = method->klass;
682         while (klass) {
683                 clear_cached_object (domain, &(method->signature), klass);
684                 klass = klass->parent;
685         }
686 }
687
688 /**
689  * mono_field_get_object:
690  * \param domain an app domain
691  * \param klass a type
692  * \param field a field
693  * \returns A \c System.Reflection.MonoField object representing the field \p field
694  * in class \p klass.
695  */
696 MonoReflectionField*
697 mono_field_get_object (MonoDomain *domain, MonoClass *klass, MonoClassField *field)
698 {
699         HANDLE_FUNCTION_ENTER ();
700         MonoError error;
701         MonoReflectionFieldHandle result = mono_field_get_object_handle (domain, klass, field, &error);
702         mono_error_cleanup (&error);
703         HANDLE_FUNCTION_RETURN_OBJ (result);
704 }
705
706 static MonoReflectionFieldHandle
707 field_object_construct (MonoDomain *domain, MonoClass *klass, MonoClassField *field, gpointer user_data, MonoError *error)
708 {
709         error_init (error);
710
711         MonoReflectionFieldHandle res = MONO_HANDLE_NEW (MonoReflectionField, mono_object_new_checked (domain, mono_class_get_mono_field_class (), error));
712         if (!is_ok (error))
713                 goto fail;
714         MONO_HANDLE_SETVAL (res, klass, MonoClass *, klass);
715         MONO_HANDLE_SETVAL (res, field, MonoClassField *, field);
716         MonoStringHandle name = mono_string_new_handle (domain, mono_field_get_name (field), error);
717         if (!is_ok (error))
718                 goto fail;
719         MONO_HANDLE_SET (res, name, name);
720
721         if (field->type) {
722                 MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, field->type, error);
723                 if (!is_ok (error))
724                         goto fail;
725
726                 MONO_HANDLE_SET (res, type, rt);
727         }
728         MONO_HANDLE_SETVAL (res, attrs, guint32, mono_field_get_flags (field));
729         return res;
730 fail:
731         return MONO_HANDLE_CAST (MonoReflectionField, NULL_HANDLE);
732 }
733
734 /*
735  * mono_field_get_object_handle:
736  * @domain: an app domain
737  * @klass: a type
738  * @field: a field
739  * @error: set on error
740  *
741  * Return an System.Reflection.MonoField object representing the field @field
742  * in class @klass. On error, returns NULL and sets @error.
743  */
744 MonoReflectionFieldHandle
745 mono_field_get_object_handle (MonoDomain *domain, MonoClass *klass, MonoClassField *field, MonoError *error)
746 {
747         error_init (error);
748         return CHECK_OR_CONSTRUCT_HANDLE (MonoReflectionFieldHandle, field, klass, field_object_construct, NULL);
749 }
750
751
752 /*
753  * mono_field_get_object_checked:
754  * @domain: an app domain
755  * @klass: a type
756  * @field: a field
757  * @error: set on error
758  *
759  * Return an System.Reflection.MonoField object representing the field @field
760  * in class @klass. On error, returns NULL and sets @error.
761  */
762 MonoReflectionField*
763 mono_field_get_object_checked (MonoDomain *domain, MonoClass *klass, MonoClassField *field, MonoError *error)
764 {
765         HANDLE_FUNCTION_ENTER ();
766         MonoReflectionFieldHandle result = mono_field_get_object_handle (domain, klass, field, error);
767         HANDLE_FUNCTION_RETURN_OBJ (result);
768 }
769
770 /*
771  * mono_property_get_object:
772  * @domain: an app domain
773  * @klass: a type
774  * @property: a property
775  *
776  * Return an System.Reflection.MonoProperty object representing the property @property
777  * in class @klass.
778  */
779 MonoReflectionProperty*
780 mono_property_get_object (MonoDomain *domain, MonoClass *klass, MonoProperty *property)
781 {
782         HANDLE_FUNCTION_ENTER ();
783         MonoError error;
784         MonoReflectionPropertyHandle result = mono_property_get_object_handle (domain, klass, property, &error);
785         mono_error_cleanup (&error);
786         HANDLE_FUNCTION_RETURN_OBJ (result);
787 }
788
789 static MonoReflectionPropertyHandle
790 property_object_construct (MonoDomain *domain, MonoClass *klass, MonoProperty *property, gpointer user_data, MonoError *error)
791 {
792         error_init (error);
793
794         MonoReflectionPropertyHandle res = MONO_HANDLE_NEW (MonoReflectionProperty, mono_object_new_checked (domain, mono_class_get_mono_property_class (), error));
795         if (!is_ok (error))
796                 goto fail;
797         MONO_HANDLE_SETVAL (res, klass, MonoClass *, klass);
798         MONO_HANDLE_SETVAL (res, property, MonoProperty *, property);
799         return res;
800 fail:
801         return MONO_HANDLE_CAST (MonoReflectionProperty, NULL_HANDLE);
802 }
803
804 /**
805  * mono_property_get_object_handle:
806  * \param domain an app domain
807  * \param klass a type
808  * \param property a property
809  * \param error set on error
810  *
811  * \returns A \c System.Reflection.MonoProperty object representing the property \p property
812  * in class \p klass.  On error returns NULL and sets \p error.
813  */
814 MonoReflectionPropertyHandle
815 mono_property_get_object_handle (MonoDomain *domain, MonoClass *klass, MonoProperty *property, MonoError *error)
816 {
817         return CHECK_OR_CONSTRUCT_HANDLE (MonoReflectionPropertyHandle, property, klass, property_object_construct, NULL);
818 }
819
820 /**
821  * mono_property_get_object:
822  * \param domain an app domain
823  * \param klass a type
824  * \param property a property
825  * \param error set on error
826  * \returns a \c System.Reflection.MonoProperty object representing the property \p property
827  * in class \p klass.  On error returns NULL and sets \p error.
828  */
829 MonoReflectionProperty*
830 mono_property_get_object_checked (MonoDomain *domain, MonoClass *klass, MonoProperty *property, MonoError *error)
831 {
832         HANDLE_FUNCTION_ENTER ();
833         MonoReflectionPropertyHandle res = mono_property_get_object_handle (domain, klass, property, error);
834         HANDLE_FUNCTION_RETURN_OBJ (res);
835 }
836
837 /**
838  * mono_event_get_object:
839  * \param domain an app domain
840  * \param klass a type
841  * \param event a event
842  * \returns A \c System.Reflection.MonoEvent object representing the event \p event
843  * in class \p klass.
844  */
845 MonoReflectionEvent*
846 mono_event_get_object (MonoDomain *domain, MonoClass *klass, MonoEvent *event)
847 {
848         HANDLE_FUNCTION_ENTER ();
849         MonoError error;
850         MonoReflectionEventHandle result = mono_event_get_object_handle (domain, klass, event, &error);
851         mono_error_cleanup (&error);
852         HANDLE_FUNCTION_RETURN_OBJ (result);
853 }
854
855 static MonoReflectionEventHandle
856 event_object_construct (MonoDomain *domain, MonoClass *klass, MonoEvent *event, gpointer user_data, MonoError *error)
857 {
858
859         error_init (error);
860         MonoReflectionMonoEventHandle mono_event = MONO_HANDLE_NEW (MonoReflectionMonoEvent, mono_object_new_checked (domain, mono_class_get_mono_event_class (), error));
861         if (!is_ok (error))
862                 return MONO_HANDLE_CAST (MonoReflectionEvent, NULL_HANDLE);
863         MONO_HANDLE_SETVAL (mono_event, klass, MonoClass* , klass);
864         MONO_HANDLE_SETVAL (mono_event, event, MonoEvent* , event);
865         return MONO_HANDLE_CAST (MonoReflectionEvent, mono_event);
866 }
867
868 /**
869  * mono_event_get_object_handle:
870  * \param domain an app domain
871  * \param klass a type
872  * \param event a event
873  * \param error set on error
874  * \returns a \c System.Reflection.MonoEvent object representing the event \p event
875  * in class \p klass. On failure sets \p error and returns NULL
876  */
877 MonoReflectionEventHandle
878 mono_event_get_object_handle (MonoDomain *domain, MonoClass *klass, MonoEvent *event, MonoError *error)
879 {
880         error_init (error);
881         return CHECK_OR_CONSTRUCT_HANDLE (MonoReflectionEventHandle, event, klass, event_object_construct, NULL);
882 }
883
884
885 /**
886  * mono_get_reflection_missing_object:
887  * \param domain Domain where the object lives
888  *
889  * \returns the \c System.Reflection.Missing.Value singleton object
890  * (of type \c System.Reflection.Missing).
891  *
892  * Used as the value for \c ParameterInfo.DefaultValue when Optional
893  * is present
894  */
895 static MonoObjectHandle
896 mono_get_reflection_missing_object (MonoDomain *domain)
897 {
898         MonoError error;
899         static MonoClassField *missing_value_field = NULL;
900         
901         if (!missing_value_field) {
902                 MonoClass *missing_klass;
903                 missing_klass = mono_class_get_missing_class ();
904                 mono_class_init (missing_klass);
905                 missing_value_field = mono_class_get_field_from_name (missing_klass, "Value");
906                 g_assert (missing_value_field);
907         }
908         /* FIXME change mono_field_get_value_object_checked to return a handle */
909         MonoObjectHandle obj = MONO_HANDLE_NEW (MonoObject, mono_field_get_value_object_checked (domain, missing_value_field, NULL, &error));
910         mono_error_assert_ok (&error);
911         return obj;
912 }
913
914 static MonoObjectHandle
915 get_dbnull_object (MonoDomain *domain, MonoError *error)
916 {
917         static MonoClassField *dbnull_value_field = NULL;
918
919         error_init (error);
920
921         if (!dbnull_value_field) {
922                 MonoClass *dbnull_klass;
923                 dbnull_klass = mono_class_get_dbnull_class ();
924                 dbnull_value_field = mono_class_get_field_from_name (dbnull_klass, "Value");
925                 g_assert (dbnull_value_field);
926         }
927         /* FIXME change mono_field_get_value_object_checked to return a handle */
928         MonoObjectHandle obj = MONO_HANDLE_NEW (MonoObject, mono_field_get_value_object_checked (domain, dbnull_value_field, NULL, error));
929         return obj;
930 }
931
932 static MonoObjectHandle
933 get_dbnull (MonoDomain *domain, MonoObjectHandle dbnull, MonoError *error)
934 {
935         error_init (error);
936         if (MONO_HANDLE_IS_NULL (dbnull))
937                 MONO_HANDLE_ASSIGN (dbnull, get_dbnull_object (domain, error));
938         return dbnull;
939 }
940
941 static MonoObjectHandle
942 get_reflection_missing (MonoDomain *domain, MonoObjectHandleOut reflection_missing)
943 {
944         if (MONO_HANDLE_IS_NULL (reflection_missing))
945                 MONO_HANDLE_ASSIGN (reflection_missing, mono_get_reflection_missing_object (domain));
946         return reflection_missing;
947 }
948
949 static gboolean
950 add_parameter_object_to_array (MonoDomain *domain, MonoMethod *method, MonoObjectHandle member, int idx, const char *name, MonoType *sig_param, guint32 blob_type_enum, const char *blob, MonoMarshalSpec *mspec, MonoObjectHandle missing, MonoObjectHandle dbnull, MonoArrayHandle dest,  MonoError *error)
951 {
952         HANDLE_FUNCTION_ENTER ();
953         error_init (error);
954         MonoReflectionParameterHandle param = MONO_HANDLE_NEW (MonoReflectionParameter, mono_object_new_checked (domain, mono_class_get_mono_parameter_info_class (), error));
955         if (!is_ok (error))
956                 goto leave;
957
958         MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, sig_param, error);
959         if (!is_ok (error))
960                 goto leave;
961
962         MONO_HANDLE_SET (param, ClassImpl, rt);
963
964         MONO_HANDLE_SET (param, MemberImpl, member);
965
966         MonoStringHandle name_str = mono_string_new_handle (domain, name, error);
967         if (!is_ok (error))
968                 goto leave;
969
970         MONO_HANDLE_SET (param, NameImpl, name_str);
971
972         MONO_HANDLE_SETVAL (param, PositionImpl, gint32, idx);
973
974         MONO_HANDLE_SETVAL (param, AttrsImpl, guint32, sig_param->attrs);
975
976         if (!(sig_param->attrs & PARAM_ATTRIBUTE_HAS_DEFAULT)) {
977                 if (sig_param->attrs & PARAM_ATTRIBUTE_OPTIONAL)
978                         MONO_HANDLE_SET (param, DefaultValueImpl, get_reflection_missing (domain, missing));
979                 else
980                         MONO_HANDLE_SET (param, DefaultValueImpl, get_dbnull (domain, dbnull, error));
981                 if (!is_ok (error))
982                         goto leave;
983         } else {
984
985                 MonoType blob_type;
986
987                 blob_type.type = (MonoTypeEnum)blob_type_enum;
988                 blob_type.data.klass = NULL;
989                 if (blob_type_enum == MONO_TYPE_CLASS)
990                         blob_type.data.klass = mono_defaults.object_class;
991                 else if ((sig_param->type == MONO_TYPE_VALUETYPE) && sig_param->data.klass->enumtype) {
992                         /* For enums, types [i] contains the base type */
993
994                         blob_type.type = MONO_TYPE_VALUETYPE;
995                         blob_type.data.klass = mono_class_from_mono_type (sig_param);
996                 } else
997                         blob_type.data.klass = mono_class_from_mono_type (&blob_type);
998
999                 MonoObjectHandle default_val_obj = MONO_HANDLE_NEW (MonoObject, mono_get_object_from_blob (domain, &blob_type, blob, error)); /* FIXME make mono_get_object_from_blob return a handle */
1000                 if (!is_ok (error))
1001                         goto leave;
1002                 MONO_HANDLE_SET (param, DefaultValueImpl, default_val_obj);
1003
1004                 /* Type in the Constant table is MONO_TYPE_CLASS for nulls */
1005                 if (blob_type_enum != MONO_TYPE_CLASS && MONO_HANDLE_IS_NULL(default_val_obj)) {
1006                         if (sig_param->attrs & PARAM_ATTRIBUTE_OPTIONAL)
1007                                 MONO_HANDLE_SET (param, DefaultValueImpl, get_reflection_missing (domain, missing));
1008                         else
1009                                 MONO_HANDLE_SET (param, DefaultValueImpl, get_dbnull (domain, dbnull, error));
1010                         if (!is_ok (error))
1011                                 goto leave;
1012                 }
1013         }
1014
1015         if (mspec) {
1016                 MonoReflectionMarshalAsAttributeHandle mobj = mono_reflection_marshal_as_attribute_from_marshal_spec (domain, method->klass, mspec, error);
1017                 if (!is_ok (error))
1018                         goto leave;
1019                 MONO_HANDLE_SET (param, MarshalAsImpl, mobj);
1020         }
1021
1022         MONO_HANDLE_ARRAY_SETREF (dest, idx, param);
1023
1024 leave:
1025         HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
1026 }
1027
1028 static MonoArrayHandle
1029 param_objects_construct (MonoDomain *domain, MonoClass *refclass, MonoMethodSignature **addr_of_sig, gpointer user_data, MonoError *error)
1030 {
1031         MonoMethod *method = (MonoMethod*)user_data;
1032         MonoMethodSignature *sig = *addr_of_sig; /* see note in mono_param_get_objects_internal */
1033
1034         MonoArrayHandle res = MONO_HANDLE_NEW (MonoArray, NULL);
1035         char **names = NULL, **blobs = NULL;
1036         guint32 *types = NULL;
1037         MonoMarshalSpec **mspecs = NULL;
1038         int i;
1039
1040         error_init (error);
1041         
1042         MonoReflectionMethodHandle member = mono_method_get_object_handle (domain, method, refclass, error);
1043         if (!is_ok (error))
1044                 goto leave;
1045         names = g_new (char *, sig->param_count);
1046         mono_method_get_param_names (method, (const char **) names);
1047
1048         mspecs = g_new (MonoMarshalSpec*, sig->param_count + 1);
1049         mono_method_get_marshal_info (method, mspecs);
1050
1051         res = mono_array_new_handle (domain, mono_class_get_mono_parameter_info_class (), sig->param_count, error);
1052         if (!res)
1053                 goto leave;
1054
1055         gboolean any_default_value = FALSE;
1056         for (i = 0; i < sig->param_count; ++i) {
1057                 if ((sig->params [i]->attrs & PARAM_ATTRIBUTE_HAS_DEFAULT) != 0) {
1058                         any_default_value = TRUE;
1059                         break;
1060                 }
1061         }
1062         if (any_default_value) {
1063                 blobs = g_new0 (char *, sig->param_count);
1064                 types = g_new0 (guint32, sig->param_count);
1065                 get_default_param_value_blobs (method, blobs, types);
1066         }
1067
1068         /* Handles missing and dbnull are assigned in add_parameter_object_to_array when needed */
1069         MonoObjectHandle missing = MONO_HANDLE_NEW (MonoObject, NULL);
1070         MonoObjectHandle dbnull = MONO_HANDLE_NEW (MonoObject, NULL);
1071         for (i = 0; i < sig->param_count; ++i) {
1072                 if (!add_parameter_object_to_array (domain, method, MONO_HANDLE_CAST(MonoObject, member), i, names[i], sig->params[i], types ? types[i] : 0, blobs ? blobs[i] : NULL, mspecs [i + 1], missing, dbnull, res, error))
1073                         goto leave;
1074         }
1075
1076 leave:
1077         g_free (names);
1078         g_free (blobs);
1079         g_free (types);
1080
1081         if (sig && mspecs) {
1082                 for (i = sig->param_count; i >= 0; i--) {
1083                         if (mspecs [i])
1084                                 mono_metadata_free_marshal_spec (mspecs [i]);
1085                 }
1086         }
1087         g_free (mspecs);
1088
1089         if (!is_ok (error))
1090                 return NULL;
1091         
1092         return res;
1093 }
1094
1095 /*
1096  * mono_param_get_objects:
1097  * @domain: an app domain
1098  * @method: a method
1099  *
1100  * Return an System.Reflection.ParameterInfo array object representing the parameters
1101  * in the method @method.
1102  */
1103 MonoArrayHandle
1104 mono_param_get_objects_internal (MonoDomain *domain, MonoMethod *method, MonoClass *refclass, MonoError *error)
1105 {
1106         error_init (error);
1107
1108         /* side-effect: sets method->signature non-NULL on success */
1109         MonoMethodSignature *sig = mono_method_signature_checked (method, error);
1110         if (!is_ok (error))
1111                 goto fail;
1112
1113         if (!sig->param_count) {
1114                 MonoArrayHandle res = mono_array_new_handle (domain, mono_class_get_mono_parameter_info_class (), 0, error);
1115                 if (!is_ok (error))
1116                         goto fail;
1117
1118                 return res;
1119         }
1120
1121         /* Note: the cache is based on the address of the signature into the method
1122          * since we already cache MethodInfos with the method as keys.
1123          */
1124         return CHECK_OR_CONSTRUCT_HANDLE (MonoArrayHandle, &method->signature, refclass, param_objects_construct, method);
1125 fail:
1126         return MONO_HANDLE_NEW (MonoArray, NULL_HANDLE);
1127 }
1128
1129 /**
1130  * mono_param_get_objects:
1131  */
1132 MonoArray*
1133 mono_param_get_objects (MonoDomain *domain, MonoMethod *method)
1134 {
1135         HANDLE_FUNCTION_ENTER ();
1136         MonoError error;
1137         MonoArrayHandle result = mono_param_get_objects_internal (domain, method, NULL, &error);
1138         mono_error_assert_ok (&error);
1139         HANDLE_FUNCTION_RETURN_OBJ (result);
1140 }
1141
1142 static gboolean
1143 add_local_var_info_to_array (MonoDomain *domain, MonoMethodHeader *header, int idx, MonoArrayHandle dest, MonoError *error)
1144 {
1145         HANDLE_FUNCTION_ENTER ();
1146         error_init (error);
1147         MonoReflectionLocalVariableInfoHandle info = MONO_HANDLE_NEW (MonoReflectionLocalVariableInfo, mono_object_new_checked (domain, mono_class_get_local_variable_info_class (), error));
1148         if (!is_ok (error))
1149                 goto leave;
1150
1151         MonoReflectionTypeHandle rt = mono_type_get_object_handle (domain, header->locals [idx], error);
1152         if (!is_ok (error))
1153                 goto leave;
1154
1155         MONO_HANDLE_SET (info, local_type, rt);
1156
1157         MONO_HANDLE_SETVAL (info, is_pinned, MonoBoolean, header->locals [idx]->pinned);
1158         MONO_HANDLE_SETVAL (info, local_index, guint16, idx);
1159
1160         MONO_HANDLE_ARRAY_SETREF (dest, idx, info);
1161
1162 leave:
1163         HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
1164 }
1165
1166 static gboolean
1167 add_exception_handling_clause_to_array (MonoDomain *domain, MonoMethodHeader *header, int idx, MonoArrayHandle dest, MonoError *error)
1168 {
1169         HANDLE_FUNCTION_ENTER ();
1170         error_init (error);
1171         MonoReflectionExceptionHandlingClauseHandle info = MONO_HANDLE_NEW (MonoReflectionExceptionHandlingClause, mono_object_new_checked (domain, mono_class_get_exception_handling_clause_class (), error));
1172         if (!is_ok (error))
1173                 goto leave;
1174         MonoExceptionClause *clause = &header->clauses [idx];
1175
1176         MONO_HANDLE_SETVAL (info, flags, gint32, clause->flags);
1177         MONO_HANDLE_SETVAL (info, try_offset, gint32, clause->try_offset);
1178         MONO_HANDLE_SETVAL (info, try_length, gint32, clause->try_len);
1179         MONO_HANDLE_SETVAL (info, handler_offset, gint32, clause->handler_offset);
1180         MONO_HANDLE_SETVAL (info, handler_length, gint32, clause->handler_len);
1181         if (clause->flags == MONO_EXCEPTION_CLAUSE_FILTER)
1182                 MONO_HANDLE_SETVAL (info, filter_offset, gint32, clause->data.filter_offset);
1183         else if (clause->data.catch_class) {
1184                 MonoReflectionTypeHandle rt = mono_type_get_object_handle (mono_domain_get (), &clause->data.catch_class->byval_arg, error);
1185                 if (!is_ok (error))
1186                         goto leave;
1187
1188                 MONO_HANDLE_SET (info, catch_type, rt);
1189         }
1190
1191         MONO_HANDLE_ARRAY_SETREF (dest, idx, info);
1192 leave:
1193         HANDLE_FUNCTION_RETURN_VAL (is_ok (error));
1194 }
1195
1196 /**
1197  * mono_method_body_get_object:
1198  * \param domain an app domain
1199  * \param method a method
1200  * \return A \c System.Reflection.MethodBody object representing the method \p method.
1201  */
1202 MonoReflectionMethodBody*
1203 mono_method_body_get_object (MonoDomain *domain, MonoMethod *method)
1204 {
1205         HANDLE_FUNCTION_ENTER ();
1206         MonoError error;
1207         MonoReflectionMethodBodyHandle result = mono_method_body_get_object_handle (domain, method, &error);
1208         mono_error_cleanup (&error);
1209         HANDLE_FUNCTION_RETURN_OBJ (result);
1210 }
1211
1212 static MonoReflectionMethodBodyHandle
1213 method_body_object_construct (MonoDomain *domain, MonoClass *unused_class, MonoMethod *method, gpointer user_data, MonoError *error)
1214 {
1215         MonoMethodHeader *header = NULL;
1216         MonoImage *image;
1217         guint32 method_rva, local_var_sig_token;
1218         char *ptr;
1219         unsigned char format, flags;
1220         int i;
1221
1222         error_init (error);
1223
1224         /* for compatibility with .net */
1225         if (method_is_dynamic (method)) {
1226                 mono_error_set_generic_error (error, "System", "InvalidOperationException", "");
1227                 goto fail;
1228         }
1229
1230         if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1231                 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
1232             (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1233                 (method->klass->image->raw_data && method->klass->image->raw_data [1] != 'Z') ||
1234             (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME))
1235                 return MONO_HANDLE_CAST (MonoReflectionMethodBody, NULL_HANDLE);
1236
1237         image = method->klass->image;
1238         header = mono_method_get_header_checked (method, error);
1239         if (!is_ok (error))
1240                 goto fail;
1241
1242         if (!image_is_dynamic (image)) {
1243                 /* Obtain local vars signature token */
1244                 method_rva = mono_metadata_decode_row_col (&image->tables [MONO_TABLE_METHOD], mono_metadata_token_index (method->token) - 1, MONO_METHOD_RVA);
1245                 ptr = mono_image_rva_map (image, method_rva);
1246                 flags = *(const unsigned char *) ptr;
1247                 format = flags & METHOD_HEADER_FORMAT_MASK;
1248                 switch (format){
1249                 case METHOD_HEADER_TINY_FORMAT:
1250                         local_var_sig_token = 0;
1251                         break;
1252                 case METHOD_HEADER_FAT_FORMAT:
1253                         ptr += 2;
1254                         ptr += 2;
1255                         ptr += 4;
1256                         local_var_sig_token = read32 (ptr);
1257                         break;
1258                 default:
1259                         g_assert_not_reached ();
1260                 }
1261         } else
1262                 local_var_sig_token = 0; //FIXME
1263
1264         MonoReflectionMethodBodyHandle ret = MONO_HANDLE_NEW (MonoReflectionMethodBody, mono_object_new_checked (domain, mono_class_get_method_body_class (), error));
1265         if (!is_ok (error))
1266                 goto fail;
1267
1268         MONO_HANDLE_SETVAL (ret, init_locals, MonoBoolean, header->init_locals);
1269         MONO_HANDLE_SETVAL (ret, max_stack, guint32, header->max_stack);
1270         MONO_HANDLE_SETVAL (ret, local_var_sig_token, guint32, local_var_sig_token);
1271         MonoArrayHandle il_arr = mono_array_new_handle (domain, mono_defaults.byte_class, header->code_size, error);
1272         if (!is_ok (error))
1273                 goto fail;
1274         MONO_HANDLE_SET (ret, il, il_arr);
1275         uint32_t il_gchandle;
1276         guint8* il_data = MONO_ARRAY_HANDLE_PIN (il_arr, guint8, 0, &il_gchandle);
1277         memcpy (il_data, header->code, header->code_size);
1278         mono_gchandle_free (il_gchandle);
1279
1280         /* Locals */
1281         MonoArrayHandle locals_arr = mono_array_new_handle (domain, mono_class_get_local_variable_info_class (), header->num_locals, error);
1282         if (!is_ok (error))
1283                 goto fail;
1284         MONO_HANDLE_SET (ret, locals, locals_arr);
1285         for (i = 0; i < header->num_locals; ++i) {
1286                 if (!add_local_var_info_to_array (domain, header, i, locals_arr, error))
1287                         goto fail;
1288         }
1289
1290         /* Exceptions */
1291         MonoArrayHandle exn_clauses = mono_array_new_handle (domain, mono_class_get_exception_handling_clause_class (), header->num_clauses, error);
1292         if (!is_ok (error))
1293                 goto fail;
1294         MONO_HANDLE_SET (ret, clauses, exn_clauses);
1295         for (i = 0; i < header->num_clauses; ++i) {
1296                 if (!add_exception_handling_clause_to_array (domain, header, i, exn_clauses, error))
1297                         goto fail;
1298         }
1299
1300         mono_metadata_free_mh (header);
1301         return ret;
1302 fail:
1303         if (header)
1304                 mono_metadata_free_mh (header);
1305         return NULL;
1306 }
1307
1308 /**
1309  * mono_method_body_get_object_handle:
1310  * \param domain an app domain
1311  * \param method a method
1312  * \param error set on error
1313  * \returns a \c System.Reflection.MethodBody object representing the
1314  * method \p method.  On failure, returns NULL and sets \p error.
1315  */
1316 MonoReflectionMethodBodyHandle
1317 mono_method_body_get_object_handle (MonoDomain *domain, MonoMethod *method, MonoError *error)
1318 {
1319         error_init (error);
1320         return CHECK_OR_CONSTRUCT_HANDLE (MonoReflectionMethodBodyHandle, method, NULL, method_body_object_construct, NULL);
1321 }
1322
1323
1324 /**
1325  * mono_get_dbnull_object:
1326  * \param domain Domain where the object lives
1327  * Used as the value for \c ParameterInfo.DefaultValue
1328  * \returns the \c System.DBNull.Value singleton object
1329  */
1330 MonoObject *
1331 mono_get_dbnull_object (MonoDomain *domain)
1332 {
1333         HANDLE_FUNCTION_ENTER ();
1334         MonoError error;
1335         MonoObjectHandle obj = get_dbnull_object (domain, &error);
1336         mono_error_assert_ok (&error);
1337         HANDLE_FUNCTION_RETURN_OBJ (obj);
1338 }
1339
1340 static void
1341 get_default_param_value_blobs (MonoMethod *method, char **blobs, guint32 *types)
1342 {
1343         guint32 param_index, i, lastp, crow = 0;
1344         guint32 param_cols [MONO_PARAM_SIZE], const_cols [MONO_CONSTANT_SIZE];
1345         gint32 idx;
1346
1347         MonoClass *klass = method->klass;
1348         MonoImage *image = klass->image;
1349         MonoMethodSignature *methodsig = mono_method_signature (method);
1350
1351         MonoTableInfo *constt;
1352         MonoTableInfo *methodt;
1353         MonoTableInfo *paramt;
1354
1355         if (!methodsig->param_count)
1356                 return;
1357
1358         mono_class_init (klass);
1359
1360         if (image_is_dynamic (klass->image)) {
1361                 MonoReflectionMethodAux *aux;
1362                 if (method->is_inflated)
1363                         method = ((MonoMethodInflated*)method)->declaring;
1364                 aux = (MonoReflectionMethodAux *)g_hash_table_lookup (((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
1365                 if (aux && aux->param_defaults) {
1366                         memcpy (blobs, &(aux->param_defaults [1]), methodsig->param_count * sizeof (char*));
1367                         memcpy (types, &(aux->param_default_types [1]), methodsig->param_count * sizeof (guint32));
1368                 }
1369                 return;
1370         }
1371
1372         methodt = &klass->image->tables [MONO_TABLE_METHOD];
1373         paramt = &klass->image->tables [MONO_TABLE_PARAM];
1374         constt = &image->tables [MONO_TABLE_CONSTANT];
1375
1376         idx = mono_method_get_index (method) - 1;
1377         g_assert (idx != -1);
1378
1379         param_index = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
1380         if (idx + 1 < methodt->rows)
1381                 lastp = mono_metadata_decode_row_col (methodt, idx + 1, MONO_METHOD_PARAMLIST);
1382         else
1383                 lastp = paramt->rows + 1;
1384
1385         for (i = param_index; i < lastp; ++i) {
1386                 guint32 paramseq;
1387
1388                 mono_metadata_decode_row (paramt, i - 1, param_cols, MONO_PARAM_SIZE);
1389                 paramseq = param_cols [MONO_PARAM_SEQUENCE];
1390
1391                 if (!(param_cols [MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_DEFAULT))
1392                         continue;
1393
1394                 crow = mono_metadata_get_constant_index (image, MONO_TOKEN_PARAM_DEF | i, crow + 1);
1395                 if (!crow) {
1396                         continue;
1397                 }
1398         
1399                 mono_metadata_decode_row (constt, crow - 1, const_cols, MONO_CONSTANT_SIZE);
1400                 blobs [paramseq - 1] = (char *)mono_metadata_blob_heap (image, const_cols [MONO_CONSTANT_VALUE]);
1401                 types [paramseq - 1] = const_cols [MONO_CONSTANT_TYPE];
1402         }
1403
1404         return;
1405 }
1406
1407 MonoObject *
1408 mono_get_object_from_blob (MonoDomain *domain, MonoType *type, const char *blob, MonoError *error)
1409 {
1410         void *retval;
1411         MonoClass *klass;
1412         MonoObject *object;
1413         MonoType *basetype = type;
1414
1415         error_init (error);
1416
1417         if (!blob)
1418                 return NULL;
1419         
1420         klass = mono_class_from_mono_type (type);
1421         if (klass->valuetype) {
1422                 object = mono_object_new_checked (domain, klass, error);
1423                 return_val_if_nok (error, NULL);
1424                 retval = ((gchar *) object + sizeof (MonoObject));
1425                 if (klass->enumtype)
1426                         basetype = mono_class_enum_basetype (klass);
1427         } else {
1428                 retval = &object;
1429         }
1430                         
1431         if (!mono_get_constant_value_from_blob (domain, basetype->type,  blob, retval, error))
1432                 return object;
1433         else
1434                 return NULL;
1435 }
1436
1437 static int
1438 assembly_name_to_aname (MonoAssemblyName *assembly, char *p) {
1439         int found_sep;
1440         char *s;
1441         gboolean quoted = FALSE;
1442
1443         memset (assembly, 0, sizeof (MonoAssemblyName));
1444         assembly->culture = "";
1445         memset (assembly->public_key_token, 0, MONO_PUBLIC_KEY_TOKEN_LENGTH);
1446
1447         if (*p == '"') {
1448                 quoted = TRUE;
1449                 p++;
1450         }
1451         assembly->name = p;
1452         while (*p && (isalnum (*p) || *p == '.' || *p == '-' || *p == '_' || *p == '$' || *p == '@' || g_ascii_isspace (*p)))
1453                 p++;
1454         if (quoted) {
1455                 if (*p != '"')
1456                         return 1;
1457                 *p = 0;
1458                 p++;
1459         }
1460         if (*p != ',')
1461                 return 1;
1462         *p = 0;
1463         /* Remove trailing whitespace */
1464         s = p - 1;
1465         while (*s && g_ascii_isspace (*s))
1466                 *s-- = 0;
1467         p ++;
1468         while (g_ascii_isspace (*p))
1469                 p++;
1470         while (*p) {
1471                 if (*p == 'V' && g_ascii_strncasecmp (p, "Version=", 8) == 0) {
1472                         p += 8;
1473                         assembly->major = strtoul (p, &s, 10);
1474                         if (s == p || *s != '.')
1475                                 return 1;
1476                         p = ++s;
1477                         assembly->minor = strtoul (p, &s, 10);
1478                         if (s == p || *s != '.')
1479                                 return 1;
1480                         p = ++s;
1481                         assembly->build = strtoul (p, &s, 10);
1482                         if (s == p || *s != '.')
1483                                 return 1;
1484                         p = ++s;
1485                         assembly->revision = strtoul (p, &s, 10);
1486                         if (s == p)
1487                                 return 1;
1488                         p = s;
1489                 } else if (*p == 'C' && g_ascii_strncasecmp (p, "Culture=", 8) == 0) {
1490                         p += 8;
1491                         if (g_ascii_strncasecmp (p, "neutral", 7) == 0) {
1492                                 assembly->culture = "";
1493                                 p += 7;
1494                         } else {
1495                                 assembly->culture = p;
1496                                 while (*p && *p != ',') {
1497                                         p++;
1498                                 }
1499                         }
1500                 } else if (*p == 'P' && g_ascii_strncasecmp (p, "PublicKeyToken=", 15) == 0) {
1501                         p += 15;
1502                         if (strncmp (p, "null", 4) == 0) {
1503                                 p += 4;
1504                         } else {
1505                                 int len;
1506                                 gchar *start = p;
1507                                 while (*p && *p != ',') {
1508                                         p++;
1509                                 }
1510                                 len = (p - start + 1);
1511                                 if (len > MONO_PUBLIC_KEY_TOKEN_LENGTH)
1512                                         len = MONO_PUBLIC_KEY_TOKEN_LENGTH;
1513                                 g_strlcpy ((char*)assembly->public_key_token, start, len);
1514                         }
1515                 } else {
1516                         while (*p && *p != ',')
1517                                 p++;
1518                 }
1519                 found_sep = 0;
1520                 while (g_ascii_isspace (*p) || *p == ',') {
1521                         *p++ = 0;
1522                         found_sep = 1;
1523                         continue;
1524                 }
1525                 /* failed */
1526                 if (!found_sep)
1527                         return 1;
1528         }
1529
1530         return 0;
1531 }
1532
1533 /*
1534  * mono_reflection_parse_type:
1535  * @name: type name
1536  *
1537  * Parse a type name as accepted by the GetType () method and output the info
1538  * extracted in the info structure.
1539  * the name param will be mangled, so, make a copy before passing it to this function.
1540  * The fields in info will be valid until the memory pointed to by name is valid.
1541  *
1542  * See also mono_type_get_name () below.
1543  *
1544  * Returns: 0 on parse error.
1545  */
1546 static int
1547 _mono_reflection_parse_type (char *name, char **endptr, gboolean is_recursed,
1548                              MonoTypeNameParse *info)
1549 {
1550         char *start, *p, *w, *last_point, *startn;
1551         int in_modifiers = 0;
1552         int isbyref = 0, rank = 0, isptr = 0;
1553
1554         start = p = w = name;
1555
1556         //FIXME could we just zero the whole struct? memset (&info, 0, sizeof (MonoTypeNameParse))
1557         memset (&info->assembly, 0, sizeof (MonoAssemblyName));
1558         info->name = info->name_space = NULL;
1559         info->nested = NULL;
1560         info->modifiers = NULL;
1561         info->type_arguments = NULL;
1562
1563         /* last_point separates the namespace from the name */
1564         last_point = NULL;
1565         /* Skips spaces */
1566         while (*p == ' ') p++, start++, w++, name++;
1567
1568         while (*p) {
1569                 switch (*p) {
1570                 case '+':
1571                         *p = 0; /* NULL terminate the name */
1572                         startn = p + 1;
1573                         info->nested = g_list_append (info->nested, startn);
1574                         /* we have parsed the nesting namespace + name */
1575                         if (info->name)
1576                                 break;
1577                         if (last_point) {
1578                                 info->name_space = start;
1579                                 *last_point = 0;
1580                                 info->name = last_point + 1;
1581                         } else {
1582                                 info->name_space = (char *)"";
1583                                 info->name = start;
1584                         }
1585                         break;
1586                 case '.':
1587                         last_point = p;
1588                         break;
1589                 case '\\':
1590                         ++p;
1591                         break;
1592                 case '&':
1593                 case '*':
1594                 case '[':
1595                 case ',':
1596                 case ']':
1597                         in_modifiers = 1;
1598                         break;
1599                 default:
1600                         break;
1601                 }
1602                 if (in_modifiers)
1603                         break;
1604                 // *w++ = *p++;
1605                 p++;
1606         }
1607         
1608         if (!info->name) {
1609                 if (last_point) {
1610                         info->name_space = start;
1611                         *last_point = 0;
1612                         info->name = last_point + 1;
1613                 } else {
1614                         info->name_space = (char *)"";
1615                         info->name = start;
1616                 }
1617         }
1618         while (*p) {
1619                 switch (*p) {
1620                 case '&':
1621                         if (isbyref) /* only one level allowed by the spec */
1622                                 return 0;
1623                         isbyref = 1;
1624                         isptr = 0;
1625                         info->modifiers = g_list_append (info->modifiers, GUINT_TO_POINTER (0));
1626                         *p++ = 0;
1627                         break;
1628                 case '*':
1629                         if (isbyref) /* pointer to ref not okay */
1630                                 return 0;
1631                         info->modifiers = g_list_append (info->modifiers, GUINT_TO_POINTER (-1));
1632                         isptr = 1;
1633                         *p++ = 0;
1634                         break;
1635                 case '[':
1636                         if (isbyref) /* array of ref and generic ref are not okay */
1637                                 return 0;
1638                         //Decide if it's an array of a generic argument list
1639                         *p++ = 0;
1640
1641                         if (!*p) //XXX test
1642                                 return 0;
1643                         if (*p  == ',' || *p == '*' || *p == ']') { //array
1644                                 isptr = 0;
1645                                 rank = 1;
1646                                 while (*p) {
1647                                         if (*p == ']')
1648                                                 break;
1649                                         if (*p == ',')
1650                                                 rank++;
1651                                         else if (*p == '*') /* '*' means unknown lower bound */
1652                                                 info->modifiers = g_list_append (info->modifiers, GUINT_TO_POINTER (-2));
1653                                         else
1654                                                 return 0;
1655                                         ++p;
1656                                 }
1657                                 if (*p++ != ']')
1658                                         return 0;
1659                                 info->modifiers = g_list_append (info->modifiers, GUINT_TO_POINTER (rank));
1660                         } else {
1661                                 if (rank || isptr) /* generic args after array spec or ptr*/ //XXX test
1662                                         return 0;
1663                                 isptr = 0;
1664                                 info->type_arguments = g_ptr_array_new ();
1665                                 while (*p) {
1666                                         MonoTypeNameParse *subinfo = g_new0 (MonoTypeNameParse, 1);
1667                                         gboolean fqname = FALSE;
1668
1669                                         g_ptr_array_add (info->type_arguments, subinfo);
1670
1671                                         while (*p == ' ') p++;
1672                                         if (*p == '[') {
1673                                                 p++;
1674                                                 fqname = TRUE;
1675                                         }
1676
1677                                         if (!_mono_reflection_parse_type (p, &p, TRUE, subinfo))
1678                                                 return 0;
1679
1680                                         /*MS is lenient on [] delimited parameters that aren't fqn - and F# uses them.*/
1681                                         if (fqname && (*p != ']')) {
1682                                                 char *aname;
1683
1684                                                 if (*p != ',')
1685                                                         return 0;
1686                                                 *p++ = 0;
1687
1688                                                 aname = p;
1689                                                 while (*p && (*p != ']'))
1690                                                         p++;
1691
1692                                                 if (*p != ']')
1693                                                         return 0;
1694
1695                                                 *p++ = 0;
1696                                                 while (*aname) {
1697                                                         if (g_ascii_isspace (*aname)) {
1698                                                                 ++aname;
1699                                                                 continue;
1700                                                         }
1701                                                         break;
1702                                                 }
1703                                                 if (!*aname ||
1704                                                     !assembly_name_to_aname (&subinfo->assembly, aname))
1705                                                         return 0;
1706                                         } else if (fqname && (*p == ']')) {
1707                                                 *p++ = 0;
1708                                         }
1709                                         if (*p == ']') {
1710                                                 *p++ = 0;
1711                                                 break;
1712                                         } else if (!*p) {
1713                                                 return 0;
1714                                         }
1715                                         *p++ = 0;
1716                                 }
1717                         }
1718                         break;
1719                 case ']':
1720                         if (is_recursed)
1721                                 goto end;
1722                         return 0;
1723                 case ',':
1724                         if (is_recursed)
1725                                 goto end;
1726                         *p++ = 0;
1727                         while (*p) {
1728                                 if (g_ascii_isspace (*p)) {
1729                                         ++p;
1730                                         continue;
1731                                 }
1732                                 break;
1733                         }
1734                         if (!*p)
1735                                 return 0; /* missing assembly name */
1736                         if (!assembly_name_to_aname (&info->assembly, p))
1737                                 return 0;
1738                         break;
1739                 default:
1740                         return 0;
1741                 }
1742                 if (info->assembly.name)
1743                         break;
1744         }
1745         // *w = 0; /* terminate class name */
1746  end:
1747         if (!info->name || !*info->name)
1748                 return 0;
1749         if (endptr)
1750                 *endptr = p;
1751         /* add other consistency checks */
1752         return 1;
1753 }
1754
1755
1756 /**
1757  * mono_identifier_unescape_type_name_chars:
1758  * \param identifier the display name of a mono type
1759  *
1760  * \returns The name in internal form, that is without escaping backslashes.
1761  *
1762  * The string is modified in place!
1763  */
1764 char*
1765 mono_identifier_unescape_type_name_chars(char* identifier)
1766 {
1767         char *w, *r;
1768         if (!identifier)
1769                 return NULL;
1770         for (w = r = identifier; *r != 0; r++)
1771         {
1772                 char c = *r;
1773                 if (c == '\\') {
1774                         r++;
1775                         if (*r == 0)
1776                                 break;
1777                         c = *r;
1778                 }
1779                 *w = c;
1780                 w++;
1781         }
1782         if (w != r)
1783                 *w = 0;
1784         return identifier;
1785 }
1786
1787 void
1788 mono_identifier_unescape_info (MonoTypeNameParse* info);
1789
1790 static void
1791 unescape_each_type_argument(void* data, void* user_data)
1792 {
1793         MonoTypeNameParse* info = (MonoTypeNameParse*)data;
1794         mono_identifier_unescape_info (info);
1795 }
1796
1797 static void
1798 unescape_each_nested_name (void* data, void* user_data)
1799 {
1800         char* nested_name = (char*) data;
1801         mono_identifier_unescape_type_name_chars(nested_name);
1802 }
1803
1804 /**
1805  * mono_identifier_unescape_info:
1806  *
1807  * \param info a parsed display form of an (optionally assembly qualified) full type name.
1808  *
1809  * Destructively updates the info by unescaping the identifiers that
1810  * comprise the type namespace, name, nested types (if any) and
1811  * generic type arguments (if any).
1812  *
1813  * The resulting info has the names in internal form.
1814  *
1815  */
1816 void
1817 mono_identifier_unescape_info (MonoTypeNameParse *info)
1818 {
1819         if (!info)
1820                 return;
1821         mono_identifier_unescape_type_name_chars(info->name_space);
1822         mono_identifier_unescape_type_name_chars(info->name);
1823         // but don't escape info->assembly
1824         if (info->type_arguments)
1825                 g_ptr_array_foreach(info->type_arguments, &unescape_each_type_argument, NULL);
1826         if (info->nested)
1827                 g_list_foreach(info->nested, &unescape_each_nested_name, NULL);
1828 }
1829
1830 /**
1831  * mono_reflection_parse_type:
1832  */
1833 int
1834 mono_reflection_parse_type (char *name, MonoTypeNameParse *info)
1835 {
1836         int ok = _mono_reflection_parse_type (name, NULL, FALSE, info);
1837         if (ok) {
1838                 mono_identifier_unescape_info (info);
1839         }
1840         return ok;
1841 }
1842
1843 static MonoType*
1844 _mono_reflection_get_type_from_info (MonoTypeNameParse *info, MonoImage *image, gboolean ignorecase, MonoError *error)
1845 {
1846         gboolean type_resolve = FALSE;
1847         MonoType *type;
1848         MonoImage *rootimage = image;
1849
1850         error_init (error);
1851
1852         if (info->assembly.name) {
1853                 MonoAssembly *assembly = mono_assembly_loaded (&info->assembly);
1854                 if (!assembly && image && image->assembly && mono_assembly_names_equal (&info->assembly, &image->assembly->aname))
1855                         /* 
1856                          * This could happen in the AOT compiler case when the search hook is not
1857                          * installed.
1858                          */
1859                         assembly = image->assembly;
1860                 if (!assembly) {
1861                         /* then we must load the assembly ourselve - see #60439 */
1862                         assembly = mono_assembly_load (&info->assembly, image->assembly->basedir, NULL);
1863                         if (!assembly)
1864                                 return NULL;
1865                 }
1866                 image = assembly->image;
1867         } else if (!image) {
1868                 image = mono_defaults.corlib;
1869         }
1870
1871         type = mono_reflection_get_type_with_rootimage (rootimage, image, info, ignorecase, &type_resolve, error);
1872         if (type == NULL && !info->assembly.name && image != mono_defaults.corlib) {
1873                 /* ignore the error and try again */
1874                 mono_error_cleanup (error);
1875                 error_init (error);
1876                 image = mono_defaults.corlib;
1877                 type = mono_reflection_get_type_with_rootimage (rootimage, image, info, ignorecase, &type_resolve, error);
1878         }
1879
1880         return type;
1881 }
1882
1883 /**
1884  * mono_reflection_get_type_internal:
1885  *
1886  * Returns: may return NULL on success, sets error on failure.
1887  */
1888 static MonoType*
1889 mono_reflection_get_type_internal (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, MonoError *error)
1890 {
1891         HANDLE_FUNCTION_ENTER ();
1892         MonoClass *klass;
1893         GList *mod;
1894         int modval;
1895         gboolean bounded = FALSE;
1896         MonoType* type = NULL;
1897         
1898         error_init (error);
1899         if (!image)
1900                 image = mono_defaults.corlib;
1901
1902         if (!rootimage)
1903                 rootimage = mono_defaults.corlib;
1904
1905         if (ignorecase)
1906                 klass = mono_class_from_name_case_checked (image, info->name_space, info->name, error);
1907         else
1908                 klass = mono_class_from_name_checked (image, info->name_space, info->name, error);
1909
1910         if (!klass)
1911                 goto leave;
1912
1913         for (mod = info->nested; mod; mod = mod->next) {
1914                 gpointer iter = NULL;
1915                 MonoClass *parent;
1916
1917                 parent = klass;
1918                 mono_class_init (parent);
1919
1920                 while ((klass = mono_class_get_nested_types (parent, &iter))) {
1921                         char *lastp;
1922                         char *nested_name, *nested_nspace;
1923                         gboolean match = TRUE;
1924
1925                         lastp = strrchr ((const char *)mod->data, '.');
1926                         if (lastp) {
1927                                 /* Nested classes can have namespaces */
1928                                 int nspace_len;
1929
1930                                 nested_name = g_strdup (lastp + 1);
1931                                 nspace_len = lastp - (char*)mod->data;
1932                                 nested_nspace = (char *)g_malloc (nspace_len + 1);
1933                                 memcpy (nested_nspace, mod->data, nspace_len);
1934                                 nested_nspace [nspace_len] = '\0';
1935
1936                         } else {
1937                                 nested_name = (char *)mod->data;
1938                                 nested_nspace = NULL;
1939                         }
1940
1941                         if (nested_nspace) {
1942                                 if (ignorecase) {
1943                                         if (!(klass->name_space && mono_utf8_strcasecmp (klass->name_space, nested_nspace) == 0))
1944                                                 match = FALSE;
1945                                 } else {
1946                                         if (!(klass->name_space && strcmp (klass->name_space, nested_nspace) == 0))
1947                                                 match = FALSE;
1948                                 }
1949                         }
1950                         if (match) {
1951                                 if (ignorecase) {
1952                                         if (mono_utf8_strcasecmp (klass->name, nested_name) != 0)
1953                                                 match = FALSE;
1954                                 } else {
1955                                         if (strcmp (klass->name, nested_name) != 0)
1956                                                 match = FALSE;
1957                                 }
1958                         }
1959                         if (lastp) {
1960                                 g_free (nested_name);
1961                                 g_free (nested_nspace);
1962                         }
1963                         if (match)
1964                                 break;
1965                 }
1966
1967                 if (!klass)
1968                         break;
1969         }
1970         if (!klass)
1971                 goto leave;
1972
1973         if (info->type_arguments) {
1974                 MonoType **type_args = g_new0 (MonoType *, info->type_arguments->len);
1975                 MonoReflectionTypeHandle the_type;
1976                 MonoType *instance;
1977                 int i;
1978
1979                 for (i = 0; i < info->type_arguments->len; i++) {
1980                         MonoTypeNameParse *subinfo = (MonoTypeNameParse *)g_ptr_array_index (info->type_arguments, i);
1981
1982                         type_args [i] = _mono_reflection_get_type_from_info (subinfo, rootimage, ignorecase, error);
1983                         if (!type_args [i]) {
1984                                 g_free (type_args);
1985                                 goto leave;
1986                         }
1987                 }
1988
1989                 the_type = mono_type_get_object_handle (mono_domain_get (), &klass->byval_arg, error);
1990                 if (!is_ok (error) || MONO_HANDLE_IS_NULL (the_type))
1991                         goto leave;
1992
1993                 instance = mono_reflection_bind_generic_parameters (
1994                         the_type, info->type_arguments->len, type_args, error);
1995
1996                 g_free (type_args);
1997                 if (!instance)
1998                         goto leave;
1999
2000                 klass = mono_class_from_mono_type (instance);
2001         }
2002
2003         for (mod = info->modifiers; mod; mod = mod->next) {
2004                 modval = GPOINTER_TO_UINT (mod->data);
2005                 if (!modval) { /* byref: must be last modifier */
2006                         type = &klass->this_arg;
2007                         goto leave;
2008                 } else if (modval == -1) {
2009                         klass = mono_ptr_class_get (&klass->byval_arg);
2010                 } else if (modval == -2) {
2011                         bounded = TRUE;
2012                 } else { /* array rank */
2013                         klass = mono_bounded_array_class_get (klass, modval, bounded);
2014                 }
2015         }
2016
2017         type = &klass->byval_arg;
2018
2019 leave:
2020         HANDLE_FUNCTION_RETURN_VAL (type);
2021 }
2022
2023 /**
2024  * mono_reflection_get_type:
2025  * \param image a metadata context
2026  * \param info type description structure
2027  * \param ignorecase flag for case-insensitive string compares
2028  * \param type_resolve whenever type resolve was already tried
2029  *
2030  * Build a MonoType from the type description in \p info.
2031  * 
2032  */
2033 MonoType*
2034 mono_reflection_get_type (MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve) {
2035         MonoError error;
2036         MonoType *result = mono_reflection_get_type_with_rootimage (image, image, info, ignorecase, type_resolve, &error);
2037         mono_error_cleanup (&error);
2038         return result;
2039 }
2040
2041 /**
2042  * mono_reflection_get_type_checked:
2043  * \param rootimage the image of the currently active managed caller
2044  * \param image a metadata context
2045  * \param info type description structure
2046  * \param ignorecase flag for case-insensitive string compares
2047  * \param type_resolve whenever type resolve was already tried
2048  * \param error set on error.
2049  * Build a \c MonoType from the type description in \p info. On failure returns NULL and sets \p error.
2050  */
2051 MonoType*
2052 mono_reflection_get_type_checked (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve, MonoError *error) {
2053         error_init (error);
2054         return mono_reflection_get_type_with_rootimage (rootimage, image, info, ignorecase, type_resolve, error);
2055 }
2056
2057
2058 static MonoType*
2059 module_builder_array_get_type (MonoArrayHandle module_builders, int i, MonoImage *rootimage, MonoTypeNameParse *info, gboolean ignorecase, MonoError *error)
2060 {
2061         HANDLE_FUNCTION_ENTER ();
2062         error_init (error);
2063         MonoType *type = NULL;
2064         MonoReflectionModuleBuilderHandle mb = MONO_HANDLE_NEW (MonoReflectionModuleBuilder, NULL);
2065         MONO_HANDLE_ARRAY_GETREF (mb, module_builders, i);
2066         MonoDynamicImage *dynamic_image = MONO_HANDLE_GETVAL (mb, dynamic_image);
2067         type = mono_reflection_get_type_internal (rootimage, &dynamic_image->image, info, ignorecase, error);
2068         HANDLE_FUNCTION_RETURN_VAL (type);
2069 }
2070
2071 static MonoType*
2072 module_array_get_type (MonoArrayHandle modules, int i, MonoImage *rootimage, MonoTypeNameParse *info, gboolean ignorecase, MonoError *error)
2073 {
2074         HANDLE_FUNCTION_ENTER ();
2075         error_init (error);
2076         MonoType *type = NULL;
2077         MonoReflectionModuleHandle mod = MONO_HANDLE_NEW (MonoReflectionModule, NULL);
2078         MONO_HANDLE_ARRAY_GETREF (mod, modules, i);
2079         MonoImage *image = MONO_HANDLE_GETVAL (mod, image);
2080         type = mono_reflection_get_type_internal (rootimage, image, info, ignorecase, error);
2081         HANDLE_FUNCTION_RETURN_VAL (type);
2082 }
2083
2084 static MonoType*
2085 mono_reflection_get_type_internal_dynamic (MonoImage *rootimage, MonoAssembly *assembly, MonoTypeNameParse *info, gboolean ignorecase, MonoError *error)
2086 {
2087         HANDLE_FUNCTION_ENTER ();
2088         MonoType *type = NULL;
2089         int i;
2090
2091         error_init (error);
2092         g_assert (assembly_is_dynamic (assembly));
2093         MonoReflectionAssemblyBuilderHandle abuilder = MONO_HANDLE_CAST (MonoReflectionAssemblyBuilder, mono_assembly_get_object_handle (((MonoDynamicAssembly*)assembly)->domain, assembly, error));
2094         if (!is_ok (error))
2095                 goto leave;
2096
2097         /* Enumerate all modules */
2098
2099         MonoArrayHandle modules = MONO_HANDLE_NEW (MonoArray, NULL);
2100         MONO_HANDLE_GET (modules, abuilder, modules);
2101         if (!MONO_HANDLE_IS_NULL (modules)) {
2102                 int n = mono_array_handle_length (modules);
2103                 for (i = 0; i < n; ++i) {
2104                         type = module_builder_array_get_type (modules, i, rootimage, info, ignorecase, error);
2105                         if (type)
2106                                 break;
2107                         if (!is_ok (error))
2108                                 goto leave;
2109                 }
2110         }
2111
2112         MonoArrayHandle loaded_modules = MONO_HANDLE_NEW (MonoArray, NULL);
2113         MONO_HANDLE_GET (loaded_modules, abuilder, loaded_modules);
2114         if (!type && !MONO_HANDLE_IS_NULL(loaded_modules)) {
2115                 int n = mono_array_handle_length (loaded_modules);
2116                 for (i = 0; i < n; ++i) {
2117                         type = module_array_get_type (loaded_modules, i, rootimage, info, ignorecase, error);
2118                         if (type)
2119                                 break;
2120                         if (!is_ok (error))
2121                                 goto leave;
2122                 }
2123         }
2124
2125 leave:
2126         HANDLE_FUNCTION_RETURN_VAL (type);
2127 }
2128         
2129 MonoType*
2130 mono_reflection_get_type_with_rootimage (MonoImage *rootimage, MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve, MonoError *error)
2131 {
2132         MonoType *type;
2133         MonoReflectionAssembly *assembly;
2134         GString *fullName;
2135         GList *mod;
2136
2137         error_init (error);
2138
2139         if (image && image_is_dynamic (image))
2140                 type = mono_reflection_get_type_internal_dynamic (rootimage, image->assembly, info, ignorecase, error);
2141         else {
2142                 type = mono_reflection_get_type_internal (rootimage, image, info, ignorecase, error);
2143         }
2144         return_val_if_nok (error, NULL);
2145
2146         if (type)
2147                 return type;
2148         if (!mono_domain_has_type_resolve (mono_domain_get ()))
2149                 return NULL;
2150
2151         if (type_resolve) {
2152                 if (*type_resolve) 
2153                         return NULL;
2154                 else
2155                         *type_resolve = TRUE;
2156         }
2157         
2158         /* Reconstruct the type name */
2159         fullName = g_string_new ("");
2160         if (info->name_space && (info->name_space [0] != '\0'))
2161                 g_string_printf (fullName, "%s.%s", info->name_space, info->name);
2162         else
2163                 g_string_printf (fullName, "%s", info->name);
2164         for (mod = info->nested; mod; mod = mod->next)
2165                 g_string_append_printf (fullName, "+%s", (char*)mod->data);
2166
2167         assembly = mono_domain_try_type_resolve_checked ( mono_domain_get (), fullName->str, NULL, error);
2168         if (!is_ok (error)) {
2169                 g_string_free (fullName, TRUE);
2170                 return NULL;
2171         }
2172
2173         if (assembly) {
2174                 if (assembly_is_dynamic (assembly->assembly))
2175                         type = mono_reflection_get_type_internal_dynamic (rootimage, assembly->assembly,
2176                                                                           info, ignorecase, error);
2177                 else
2178                         type = mono_reflection_get_type_internal (rootimage, assembly->assembly->image, 
2179                                                                   info, ignorecase, error);
2180         }
2181         g_string_free (fullName, TRUE);
2182         return_val_if_nok (error, NULL);
2183         return type;
2184 }
2185
2186 /**
2187  * mono_reflection_free_type_info:
2188  */
2189 void
2190 mono_reflection_free_type_info (MonoTypeNameParse *info)
2191 {
2192         g_list_free (info->modifiers);
2193         g_list_free (info->nested);
2194
2195         if (info->type_arguments) {
2196                 int i;
2197
2198                 for (i = 0; i < info->type_arguments->len; i++) {
2199                         MonoTypeNameParse *subinfo = (MonoTypeNameParse *)g_ptr_array_index (info->type_arguments, i);
2200
2201                         mono_reflection_free_type_info (subinfo);
2202                         /*We free the subinfo since it is allocated by _mono_reflection_parse_type*/
2203                         g_free (subinfo);
2204                 }
2205
2206                 g_ptr_array_free (info->type_arguments, TRUE);
2207         }
2208 }
2209
2210 /**
2211  * mono_reflection_type_from_name:
2212  * \param name type name.
2213  * \param image a metadata context (can be NULL).
2214  *
2215  * Retrieves a \c MonoType from its \p name. If the name is not fully qualified,
2216  * it defaults to get the type from \p image or, if \p image is NULL or loading
2217  * from it fails, uses corlib.
2218  * 
2219  */
2220 MonoType*
2221 mono_reflection_type_from_name (char *name, MonoImage *image)
2222 {
2223         MonoError error;
2224         MonoType  *result = mono_reflection_type_from_name_checked (name, image, &error);
2225         mono_error_cleanup (&error);
2226         return result;
2227 }
2228
2229 /**
2230  * mono_reflection_type_from_name_checked:
2231  * \param name type name.
2232  * \param image a metadata context (can be NULL).
2233  * \param error set on errror.
2234  * Retrieves a MonoType from its \p name. If the name is not fully qualified,
2235  * it defaults to get the type from \p image or, if \p image is NULL or loading
2236  * from it fails, uses corlib.  On failure returns NULL and sets \p error.
2237  */
2238 MonoType*
2239 mono_reflection_type_from_name_checked (char *name, MonoImage *image, MonoError *error)
2240 {
2241         MonoType *type = NULL;
2242         MonoTypeNameParse info;
2243         char *tmp;
2244
2245         error_init (error);
2246         /* Make a copy since parse_type modifies its argument */
2247         tmp = g_strdup (name);
2248         
2249         /*g_print ("requested type %s\n", str);*/
2250         if (mono_reflection_parse_type (tmp, &info)) {
2251                 type = _mono_reflection_get_type_from_info (&info, image, FALSE, error);
2252                 if (!is_ok (error)) {
2253                         g_free (tmp);
2254                         mono_reflection_free_type_info (&info);
2255                         return NULL;
2256                 }
2257         }
2258
2259         g_free (tmp);
2260         mono_reflection_free_type_info (&info);
2261         return type;
2262 }
2263
2264 /**
2265  * mono_reflection_get_token:
2266  * \returns the metadata token of \p obj which should be an object
2267  * representing a metadata element.
2268  */
2269 guint32
2270 mono_reflection_get_token (MonoObject *obj_raw)
2271 {
2272         HANDLE_FUNCTION_ENTER ();
2273         MONO_HANDLE_DCL (MonoObject, obj);
2274         MonoError error;
2275         guint32 result = mono_reflection_get_token_checked (obj, &error);
2276         mono_error_assert_ok (&error);
2277         HANDLE_FUNCTION_RETURN_VAL (result);
2278 }
2279
2280 /**
2281  * mono_reflection_get_token_checked:
2282  * \param obj the object
2283  * \param error set on error
2284  * \returns the metadata token of \p obj which should be an object
2285  * representing a metadata element.  On failure sets \p error.
2286  */
2287 guint32
2288 mono_reflection_get_token_checked (MonoObjectHandle obj, MonoError *error)
2289 {
2290         guint32 token = 0;
2291
2292         error_init (error);
2293
2294         MonoClass *klass = mono_handle_class (obj);
2295
2296         if (strcmp (klass->name, "MethodBuilder") == 0) {
2297                 MonoReflectionMethodBuilderHandle mb = MONO_HANDLE_CAST (MonoReflectionMethodBuilder, obj);
2298
2299                 token = MONO_HANDLE_GETVAL (mb, table_idx) | MONO_TOKEN_METHOD_DEF;
2300         } else if (strcmp (klass->name, "ConstructorBuilder") == 0) {
2301                 MonoReflectionCtorBuilderHandle mb = MONO_HANDLE_CAST (MonoReflectionCtorBuilder, obj);
2302
2303                 token = MONO_HANDLE_GETVAL (mb, table_idx) | MONO_TOKEN_METHOD_DEF;
2304         } else if (strcmp (klass->name, "FieldBuilder") == 0) {
2305                 MonoReflectionFieldBuilderHandle fb = MONO_HANDLE_CAST (MonoReflectionFieldBuilder, obj);
2306
2307                 token = MONO_HANDLE_GETVAL (fb, table_idx) | MONO_TOKEN_FIELD_DEF;
2308         } else if (strcmp (klass->name, "TypeBuilder") == 0) {
2309                 MonoReflectionTypeBuilderHandle tb = MONO_HANDLE_CAST (MonoReflectionTypeBuilder, obj);
2310                 token = MONO_HANDLE_GETVAL (tb, table_idx) | MONO_TOKEN_TYPE_DEF;
2311         } else if (strcmp (klass->name, "RuntimeType") == 0) {
2312                 MonoType *type = mono_reflection_type_handle_mono_type (MONO_HANDLE_CAST (MonoReflectionType, obj), error);
2313                 return_val_if_nok (error, 0);
2314                 MonoClass *mc = mono_class_from_mono_type (type);
2315                 if (!mono_class_init (mc)) {
2316                         mono_error_set_for_class_failure (error, mc);
2317                         return 0;
2318                 }
2319
2320                 token = mc->type_token;
2321         } else if (strcmp (klass->name, "MonoCMethod") == 0 ||
2322                            strcmp (klass->name, "MonoMethod") == 0) {
2323                 MonoReflectionMethodHandle m = MONO_HANDLE_CAST (MonoReflectionMethod, obj);
2324                 MonoMethod *method = MONO_HANDLE_GETVAL (m, method);
2325                 if (method->is_inflated) {
2326                         MonoMethodInflated *inflated = (MonoMethodInflated *) method;
2327                         return inflated->declaring->token;
2328                 } else {
2329                         token = method->token;
2330                 }
2331         } else if (strcmp (klass->name, "MonoField") == 0) {
2332                 MonoReflectionFieldHandle f = MONO_HANDLE_CAST (MonoReflectionField, obj);
2333
2334                 token = mono_class_get_field_token (MONO_HANDLE_GETVAL (f, field));
2335         } else if (strcmp (klass->name, "MonoProperty") == 0) {
2336                 MonoReflectionPropertyHandle p = MONO_HANDLE_CAST (MonoReflectionProperty, obj);
2337
2338                 token = mono_class_get_property_token (MONO_HANDLE_GETVAL (p, property));
2339         } else if (strcmp (klass->name, "MonoEvent") == 0) {
2340                 MonoReflectionMonoEventHandle p = MONO_HANDLE_CAST (MonoReflectionMonoEvent, obj);
2341
2342                 token = mono_class_get_event_token (MONO_HANDLE_GETVAL (p, event));
2343         } else if (strcmp (klass->name, "ParameterInfo") == 0 || strcmp (klass->name, "MonoParameterInfo") == 0) {
2344                 MonoReflectionParameterHandle p = MONO_HANDLE_CAST (MonoReflectionParameter, obj);
2345                 MonoObjectHandle member_impl = MONO_HANDLE_NEW (MonoObject, NULL);
2346                 MONO_HANDLE_GET (member_impl, p, MemberImpl);
2347                 MonoClass *member_class = mono_handle_class (member_impl);
2348                 g_assert (mono_class_is_reflection_method_or_constructor (member_class));
2349                 MonoMethod *method = MONO_HANDLE_GETVAL (MONO_HANDLE_CAST (MonoReflectionMethod, member_impl), method);
2350
2351                 token = mono_method_get_param_token (method, MONO_HANDLE_GETVAL (p, PositionImpl));
2352         } else if (strcmp (klass->name, "Module") == 0 || strcmp (klass->name, "MonoModule") == 0) {
2353                 MonoReflectionModuleHandle m = MONO_HANDLE_CAST (MonoReflectionModule, obj);
2354
2355                 token = MONO_HANDLE_GETVAL (m, token);
2356         } else if (strcmp (klass->name, "Assembly") == 0 || strcmp (klass->name, "MonoAssembly") == 0) {
2357                 token = mono_metadata_make_token (MONO_TABLE_ASSEMBLY, 1);
2358         } else {
2359                 mono_error_set_not_implemented (error, "MetadataToken is not supported for type '%s.%s'",
2360                                                 klass->name_space, klass->name);
2361                 return 0;
2362         }
2363
2364         return token;
2365 }
2366
2367
2368 gboolean
2369 mono_reflection_is_usertype (MonoReflectionTypeHandle ref)
2370 {
2371         MonoClass *klass = mono_handle_class (ref);
2372         return klass->image != mono_defaults.corlib || strcmp ("TypeDelegator", klass->name) == 0;
2373 }
2374
2375 /**
2376  * mono_reflection_bind_generic_parameters:
2377  * \param type a managed type object (which should be some kind of generic (instance? definition?))
2378  * \param type_args the number of type arguments to bind
2379  * \param types array of type arguments
2380  * \param error set on error
2381  * Given a managed type object for a generic type instance, binds each of its arguments to the specified types.
2382  * \returns the \c MonoType* for the resulting type instantiation.  On failure returns NULL and sets \p error.
2383  */
2384 MonoType*
2385 mono_reflection_bind_generic_parameters (MonoReflectionTypeHandle reftype, int type_argc, MonoType **types, MonoError *error)
2386 {
2387         gboolean is_dynamic = FALSE;
2388         MonoClass *geninst;
2389
2390         error_init (error);
2391         
2392         mono_loader_lock ();
2393
2394         MonoClass *klass = mono_handle_class (reftype);
2395         if (mono_is_sre_type_builder (klass)) {
2396                 is_dynamic = TRUE;
2397         } else if (mono_is_sre_generic_instance (klass)) {
2398                 /* Does this ever make sense?  what does instantiating a generic instance even mean? */
2399                 g_assert_not_reached ();
2400                 MonoReflectionGenericClassHandle rgi = MONO_HANDLE_CAST (MonoReflectionGenericClass, reftype);
2401                 MonoReflectionTypeHandle gtd = MONO_HANDLE_NEW_GET (MonoReflectionType, rgi, generic_type);
2402
2403                 if (mono_is_sre_type_builder (mono_handle_class (gtd)))
2404                         is_dynamic = TRUE;
2405         }
2406
2407         MonoType *t = mono_reflection_type_handle_mono_type (reftype, error);
2408         if (!is_ok (error)) {
2409                 mono_loader_unlock ();
2410                 return NULL;
2411         }
2412
2413         klass = mono_class_from_mono_type (t);
2414         if (!mono_class_is_gtd (klass)) {
2415                 mono_loader_unlock ();
2416                 mono_error_set_type_load_class (error, klass, "Cannot bind generic parameters of a non-generic type");
2417                 return NULL;
2418         }
2419
2420         guint gtd_type_argc = mono_class_get_generic_container (klass)->type_argc;
2421         if (gtd_type_argc != type_argc) {
2422                 mono_loader_unlock ();
2423                 mono_error_set_argument (error, "types", "The generic type definition needs %d type arguments, but was instantiated with %d ", gtd_type_argc, type_argc);
2424                 return NULL;
2425         }
2426
2427
2428         if (klass->wastypebuilder)
2429                 is_dynamic = TRUE;
2430
2431         mono_loader_unlock ();
2432
2433         geninst = mono_class_bind_generic_parameters (klass, type_argc, types, is_dynamic);
2434
2435         return &geninst->byval_arg;
2436 }
2437
2438 MonoClass*
2439 mono_class_bind_generic_parameters (MonoClass *klass, int type_argc, MonoType **types, gboolean is_dynamic)
2440 {
2441         MonoGenericClass *gclass;
2442         MonoGenericInst *inst;
2443
2444         g_assert (mono_class_is_gtd (klass));
2445
2446         inst = mono_metadata_get_generic_inst (type_argc, types);
2447         gclass = mono_metadata_lookup_generic_class (klass, inst, is_dynamic);
2448
2449         return mono_generic_class_get_class (gclass);
2450 }
2451
2452 static MonoGenericInst*
2453 generic_inst_from_type_array_handle (MonoArrayHandle types, MonoError *error)
2454 {
2455         HANDLE_FUNCTION_ENTER ();
2456         error_init (error);
2457         MonoGenericInst *ginst = NULL;
2458         int count = mono_array_handle_length (types);
2459         MonoType **type_argv = g_new0 (MonoType *, count);
2460         MonoReflectionTypeHandle garg = MONO_HANDLE_NEW (MonoReflectionType, NULL);
2461         for (int i = 0; i < count; i++) {
2462                 MONO_HANDLE_ARRAY_GETREF (garg, types, i);
2463                 type_argv [i] = mono_reflection_type_handle_mono_type (garg, error);
2464                 if (!is_ok (error))
2465                         goto leave;
2466
2467         }
2468         ginst = mono_metadata_get_generic_inst (count, type_argv);
2469 leave:
2470         g_free (type_argv);
2471         HANDLE_FUNCTION_RETURN_VAL (ginst);
2472 }
2473
2474 static MonoMethod*
2475 reflection_bind_generic_method_parameters (MonoMethod *method, MonoArrayHandle types, MonoError *error)
2476 {
2477         MonoClass *klass;
2478         MonoMethod *inflated;
2479         MonoGenericContext tmp_context;
2480
2481         error_init (error);
2482
2483         klass = method->klass;
2484
2485         if (method->is_inflated)
2486                 method = ((MonoMethodInflated *) method)->declaring;
2487
2488         int count = mono_method_signature (method)->generic_param_count;
2489         if (count != mono_array_handle_length (types)) {
2490                 mono_error_set_argument (error, "typeArguments", "Incorrect number of generic arguments");
2491                 return NULL;
2492         }
2493
2494         MonoGenericInst *ginst = generic_inst_from_type_array_handle (types, error);
2495         return_val_if_nok (error, NULL);
2496
2497         tmp_context.class_inst = mono_class_is_ginst (klass) ? mono_class_get_generic_class (klass)->context.class_inst : NULL;
2498         tmp_context.method_inst = ginst;
2499
2500         inflated = mono_class_inflate_generic_method_checked (method, &tmp_context, error);
2501         mono_error_assert_ok (error);
2502
2503         if (!mono_verifier_is_method_valid_generic_instantiation (inflated)) {
2504                 mono_error_set_argument (error, "typeArguments", "Invalid generic arguments");
2505                 return NULL;
2506         }
2507
2508         return inflated;
2509 }
2510
2511 MonoReflectionMethodHandle
2512 ves_icall_MonoMethod_MakeGenericMethod_impl (MonoReflectionMethodHandle rmethod, MonoArrayHandle types, MonoError *error)
2513 {
2514         error_init (error);
2515         g_assert (0 != strcmp (mono_handle_class (rmethod)->name, "MethodBuilder"));
2516
2517         MonoMethod *method = MONO_HANDLE_GETVAL (rmethod, method);
2518         MonoMethod *imethod = reflection_bind_generic_method_parameters (method, types, error);
2519         return_val_if_nok (error, MONO_HANDLE_CAST (MonoReflectionMethod, NULL_HANDLE));
2520
2521         /*FIXME but I think this is no longer necessary*/
2522         if (image_is_dynamic (method->klass->image)) {
2523                 MonoDynamicImage *image = (MonoDynamicImage*)method->klass->image;
2524                 /*
2525                  * This table maps metadata structures representing inflated methods/fields
2526                  * to the reflection objects representing their generic definitions.
2527                  */
2528                 mono_image_lock ((MonoImage*)image);
2529                 mono_g_hash_table_insert (image->generic_def_objects, imethod, MONO_HANDLE_RAW (rmethod));
2530                 mono_image_unlock ((MonoImage*)image);
2531         }
2532
2533         return mono_method_get_object_handle (MONO_HANDLE_DOMAIN (rmethod), imethod, NULL, error);
2534 }
2535
2536
2537 /* SECURITY_ACTION_* are defined in mono/metadata/tabledefs.h */
2538 const static guint32 declsec_flags_map[] = {
2539         0x00000000,                                     /* empty */
2540         MONO_DECLSEC_FLAG_REQUEST,                      /* SECURITY_ACTION_REQUEST                      (x01) */
2541         MONO_DECLSEC_FLAG_DEMAND,                       /* SECURITY_ACTION_DEMAND                       (x02) */
2542         MONO_DECLSEC_FLAG_ASSERT,                       /* SECURITY_ACTION_ASSERT                       (x03) */
2543         MONO_DECLSEC_FLAG_DENY,                         /* SECURITY_ACTION_DENY                         (x04) */
2544         MONO_DECLSEC_FLAG_PERMITONLY,                   /* SECURITY_ACTION_PERMITONLY                   (x05) */
2545         MONO_DECLSEC_FLAG_LINKDEMAND,                   /* SECURITY_ACTION_LINKDEMAND                   (x06) */
2546         MONO_DECLSEC_FLAG_INHERITANCEDEMAND,            /* SECURITY_ACTION_INHERITANCEDEMAND            (x07) */
2547         MONO_DECLSEC_FLAG_REQUEST_MINIMUM,              /* SECURITY_ACTION_REQUEST_MINIMUM              (x08) */
2548         MONO_DECLSEC_FLAG_REQUEST_OPTIONAL,             /* SECURITY_ACTION_REQUEST_OPTIONAL             (x09) */
2549         MONO_DECLSEC_FLAG_REQUEST_REFUSE,               /* SECURITY_ACTION_REQUEST_REFUSE               (x0A) */
2550         MONO_DECLSEC_FLAG_PREJIT_GRANT,                 /* SECURITY_ACTION_PREJIT_GRANT                 (x0B) */
2551         MONO_DECLSEC_FLAG_PREJIT_DENY,                  /* SECURITY_ACTION_PREJIT_DENY                  (x0C) */
2552         MONO_DECLSEC_FLAG_NONCAS_DEMAND,                /* SECURITY_ACTION_NONCAS_DEMAND                (x0D) */
2553         MONO_DECLSEC_FLAG_NONCAS_LINKDEMAND,            /* SECURITY_ACTION_NONCAS_LINKDEMAND            (x0E) */
2554         MONO_DECLSEC_FLAG_NONCAS_INHERITANCEDEMAND,     /* SECURITY_ACTION_NONCAS_INHERITANCEDEMAND     (x0F) */
2555         MONO_DECLSEC_FLAG_LINKDEMAND_CHOICE,            /* SECURITY_ACTION_LINKDEMAND_CHOICE            (x10) */
2556         MONO_DECLSEC_FLAG_INHERITANCEDEMAND_CHOICE,     /* SECURITY_ACTION_INHERITANCEDEMAND_CHOICE     (x11) */
2557         MONO_DECLSEC_FLAG_DEMAND_CHOICE,                /* SECURITY_ACTION_DEMAND_CHOICE                (x12) */
2558 };
2559
2560 /*
2561  * Returns flags that includes all available security action associated to the handle.
2562  * @token: metadata token (either for a class or a method)
2563  * @image: image where resides the metadata.
2564  */
2565 static guint32
2566 mono_declsec_get_flags (MonoImage *image, guint32 token)
2567 {
2568         int index = mono_metadata_declsec_from_index (image, token);
2569         MonoTableInfo *t = &image->tables [MONO_TABLE_DECLSECURITY];
2570         guint32 result = 0;
2571         guint32 action;
2572         int i;
2573
2574         /* HasSecurity can be present for other, not specially encoded, attributes,
2575            e.g. SuppressUnmanagedCodeSecurityAttribute */
2576         if (index < 0)
2577                 return 0;
2578
2579         for (i = index; i < t->rows; i++) {
2580                 guint32 cols [MONO_DECL_SECURITY_SIZE];
2581
2582                 mono_metadata_decode_row (t, i, cols, MONO_DECL_SECURITY_SIZE);
2583                 if (cols [MONO_DECL_SECURITY_PARENT] != token)
2584                         break;
2585
2586                 action = cols [MONO_DECL_SECURITY_ACTION];
2587                 if ((action >= MONO_DECLSEC_ACTION_MIN) && (action <= MONO_DECLSEC_ACTION_MAX)) {
2588                         result |= declsec_flags_map [action];
2589                 } else {
2590                         g_assert_not_reached ();
2591                 }
2592         }
2593         return result;
2594 }
2595
2596 /**
2597  * mono_declsec_flags_from_method:
2598  * \param method The method for which we want the declarative security flags.
2599  * Get the security actions (in the form of flags) associated with the specified method.
2600  * To keep \c MonoMethod size down we do not cache the declarative security flags
2601  * (except for the stack modifiers which are kept in the MonoJitInfo structure)
2602  * \returns the declarative security flags for the method (only).
2603  */
2604 guint32
2605 mono_declsec_flags_from_method (MonoMethod *method)
2606 {
2607         if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2608                 /* FIXME: No cache (for the moment) */
2609                 guint32 idx = mono_method_get_index (method);
2610                 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2611                 idx |= MONO_HAS_DECL_SECURITY_METHODDEF;
2612                 return mono_declsec_get_flags (method->klass->image, idx);
2613         }
2614         return 0;
2615 }
2616
2617 /**
2618  * mono_declsec_flags_from_class:
2619  * \param klass The class for which we want the declarative security flags.
2620  * Get the security actions (in the form of flags) associated with the specified class.
2621  * We cache the flags inside the \c MonoClass structure as this will get 
2622  * called very often (at least for each method).
2623  * \returns the declarative security flags for the class.
2624  */
2625 guint32
2626 mono_declsec_flags_from_class (MonoClass *klass)
2627 {
2628         if (mono_class_get_flags (klass) & TYPE_ATTRIBUTE_HAS_SECURITY) {
2629                 guint32 flags = mono_class_get_declsec_flags (klass);
2630
2631                 if (!flags) {
2632                         guint32 idx;
2633
2634                         idx = mono_metadata_token_index (klass->type_token);
2635                         idx <<= MONO_HAS_DECL_SECURITY_BITS;
2636                         idx |= MONO_HAS_DECL_SECURITY_TYPEDEF;
2637                         flags = mono_declsec_get_flags (klass->image, idx);
2638                         /* we cache the flags on classes */
2639                         mono_class_set_declsec_flags (klass, flags);
2640                 }
2641                 return flags;
2642         }
2643         return 0;
2644 }
2645
2646 /**
2647  * mono_declsec_flags_from_assembly:
2648  * \param assembly The assembly for which we want the declarative security flags.
2649  * Get the security actions (in the form of flags) associated with the specified assembly.
2650  * \returns the declarative security flags for the assembly.
2651  */
2652 guint32
2653 mono_declsec_flags_from_assembly (MonoAssembly *assembly)
2654 {
2655         guint32 idx = 1; /* there is only one assembly */
2656         idx <<= MONO_HAS_DECL_SECURITY_BITS;
2657         idx |= MONO_HAS_DECL_SECURITY_ASSEMBLY;
2658         return mono_declsec_get_flags (assembly->image, idx);
2659 }
2660
2661
2662 /*
2663  * Fill actions for the specific index (which may either be an encoded class token or
2664  * an encoded method token) from the metadata image.
2665  * Returns TRUE if some actions requiring code generation are present, FALSE otherwise.
2666  */
2667 static MonoBoolean
2668 fill_actions_from_index (MonoImage *image, guint32 token, MonoDeclSecurityActions* actions,
2669         guint32 id_std, guint32 id_noncas, guint32 id_choice)
2670 {
2671         MonoBoolean result = FALSE;
2672         MonoTableInfo *t;
2673         guint32 cols [MONO_DECL_SECURITY_SIZE];
2674         int index = mono_metadata_declsec_from_index (image, token);
2675         int i;
2676
2677         t  = &image->tables [MONO_TABLE_DECLSECURITY];
2678         for (i = index; i < t->rows; i++) {
2679                 mono_metadata_decode_row (t, i, cols, MONO_DECL_SECURITY_SIZE);
2680
2681                 if (cols [MONO_DECL_SECURITY_PARENT] != token)
2682                         return result;
2683
2684                 /* if present only replace (class) permissions with method permissions */
2685                 /* if empty accept either class or method permissions */
2686                 if (cols [MONO_DECL_SECURITY_ACTION] == id_std) {
2687                         if (!actions->demand.blob) {
2688                                 const char *blob = mono_metadata_blob_heap (image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
2689                                 actions->demand.index = cols [MONO_DECL_SECURITY_PERMISSIONSET];
2690                                 actions->demand.blob = (char*) (blob + 2);
2691                                 actions->demand.size = mono_metadata_decode_blob_size (blob, &blob);
2692                                 result = TRUE;
2693                         }
2694                 } else if (cols [MONO_DECL_SECURITY_ACTION] == id_noncas) {
2695                         if (!actions->noncasdemand.blob) {
2696                                 const char *blob = mono_metadata_blob_heap (image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
2697                                 actions->noncasdemand.index = cols [MONO_DECL_SECURITY_PERMISSIONSET];
2698                                 actions->noncasdemand.blob = (char*) (blob + 2);
2699                                 actions->noncasdemand.size = mono_metadata_decode_blob_size (blob, &blob);
2700                                 result = TRUE;
2701                         }
2702                 } else if (cols [MONO_DECL_SECURITY_ACTION] == id_choice) {
2703                         if (!actions->demandchoice.blob) {
2704                                 const char *blob = mono_metadata_blob_heap (image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
2705                                 actions->demandchoice.index = cols [MONO_DECL_SECURITY_PERMISSIONSET];
2706                                 actions->demandchoice.blob = (char*) (blob + 2);
2707                                 actions->demandchoice.size = mono_metadata_decode_blob_size (blob, &blob);
2708                                 result = TRUE;
2709                         }
2710                 }
2711         }
2712
2713         return result;
2714 }
2715
2716 static MonoBoolean
2717 mono_declsec_get_class_demands_params (MonoClass *klass, MonoDeclSecurityActions* demands, 
2718         guint32 id_std, guint32 id_noncas, guint32 id_choice)
2719 {
2720         guint32 idx = mono_metadata_token_index (klass->type_token);
2721         idx <<= MONO_HAS_DECL_SECURITY_BITS;
2722         idx |= MONO_HAS_DECL_SECURITY_TYPEDEF;
2723         return fill_actions_from_index (klass->image, idx, demands, id_std, id_noncas, id_choice);
2724 }
2725
2726 static MonoBoolean
2727 mono_declsec_get_method_demands_params (MonoMethod *method, MonoDeclSecurityActions* demands, 
2728         guint32 id_std, guint32 id_noncas, guint32 id_choice)
2729 {
2730         guint32 idx = mono_method_get_index (method);
2731         idx <<= MONO_HAS_DECL_SECURITY_BITS;
2732         idx |= MONO_HAS_DECL_SECURITY_METHODDEF;
2733         return fill_actions_from_index (method->klass->image, idx, demands, id_std, id_noncas, id_choice);
2734 }
2735
2736 /**
2737  * mono_declsec_get_demands:
2738  * Collect all actions (that requires to generate code in mini) assigned for
2739  * the specified method.
2740  * Don't use the content of actions if the function return FALSE.
2741  */
2742 MonoBoolean
2743 mono_declsec_get_demands (MonoMethod *method, MonoDeclSecurityActions* demands)
2744 {
2745         guint32 mask = MONO_DECLSEC_FLAG_DEMAND | MONO_DECLSEC_FLAG_NONCAS_DEMAND | 
2746                 MONO_DECLSEC_FLAG_DEMAND_CHOICE;
2747         MonoBoolean result = FALSE;
2748         guint32 flags;
2749
2750         /* quick exit if no declarative security is present in the metadata */
2751         if (!method->klass->image->tables [MONO_TABLE_DECLSECURITY].rows)
2752                 return FALSE;
2753
2754         /* we want the original as the wrapper is "free" of the security informations */
2755         if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE || method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
2756                 method = mono_marshal_method_from_wrapper (method);
2757                 if (!method)
2758                         return FALSE;
2759         }
2760
2761         /* First we look for method-level attributes */
2762         if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2763                 mono_class_init (method->klass);
2764                 memset (demands, 0, sizeof (MonoDeclSecurityActions));
2765
2766                 result = mono_declsec_get_method_demands_params (method, demands, 
2767                         SECURITY_ACTION_DEMAND, SECURITY_ACTION_NONCASDEMAND, SECURITY_ACTION_DEMANDCHOICE);
2768         }
2769
2770         /* Here we use (or create) the class declarative cache to look for demands */
2771         flags = mono_declsec_flags_from_class (method->klass);
2772         if (flags & mask) {
2773                 if (!result) {
2774                         mono_class_init (method->klass);
2775                         memset (demands, 0, sizeof (MonoDeclSecurityActions));
2776                 }
2777                 result |= mono_declsec_get_class_demands_params (method->klass, demands, 
2778                         SECURITY_ACTION_DEMAND, SECURITY_ACTION_NONCASDEMAND, SECURITY_ACTION_DEMANDCHOICE);
2779         }
2780
2781         /* The boolean return value is used as a shortcut in case nothing needs to
2782            be generated (e.g. LinkDemand[Choice] and InheritanceDemand[Choice]) */
2783         return result;
2784 }
2785
2786
2787 /**
2788  * mono_declsec_get_linkdemands:
2789  * Collect all Link actions: \c LinkDemand, \c NonCasLinkDemand and \c LinkDemandChoice (2.0).
2790  * Don't use the content of actions if the function return FALSE.
2791  */
2792 MonoBoolean
2793 mono_declsec_get_linkdemands (MonoMethod *method, MonoDeclSecurityActions* klass, MonoDeclSecurityActions *cmethod)
2794 {
2795         MonoBoolean result = FALSE;
2796         guint32 flags;
2797
2798         /* quick exit if no declarative security is present in the metadata */
2799         if (!method->klass->image->tables [MONO_TABLE_DECLSECURITY].rows)
2800                 return FALSE;
2801
2802         /* we want the original as the wrapper is "free" of the security informations */
2803         if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE || method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
2804                 method = mono_marshal_method_from_wrapper (method);
2805                 if (!method)
2806                         return FALSE;
2807         }
2808
2809         /* results are independant - zeroize both */
2810         memset (cmethod, 0, sizeof (MonoDeclSecurityActions));
2811         memset (klass, 0, sizeof (MonoDeclSecurityActions));
2812
2813         /* First we look for method-level attributes */
2814         if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2815                 mono_class_init (method->klass);
2816
2817                 result = mono_declsec_get_method_demands_params (method, cmethod, 
2818                         SECURITY_ACTION_LINKDEMAND, SECURITY_ACTION_NONCASLINKDEMAND, SECURITY_ACTION_LINKDEMANDCHOICE);
2819         }
2820
2821         /* Here we use (or create) the class declarative cache to look for demands */
2822         flags = mono_declsec_flags_from_class (method->klass);
2823         if (flags & (MONO_DECLSEC_FLAG_LINKDEMAND | MONO_DECLSEC_FLAG_NONCAS_LINKDEMAND | MONO_DECLSEC_FLAG_LINKDEMAND_CHOICE)) {
2824                 mono_class_init (method->klass);
2825
2826                 result |= mono_declsec_get_class_demands_params (method->klass, klass, 
2827                         SECURITY_ACTION_LINKDEMAND, SECURITY_ACTION_NONCASLINKDEMAND, SECURITY_ACTION_LINKDEMANDCHOICE);
2828         }
2829
2830         return result;
2831 }
2832
2833 /**
2834  * mono_declsec_get_inheritdemands_class:
2835  * \param klass The inherited class - this is the class that provides the security check (attributes)
2836  * \param demands
2837  * Collect all Inherit actions - \c InheritanceDemand, \c NonCasInheritanceDemand and \c InheritanceDemandChoice (2.0).
2838  * Don't use the content of actions if the function return FALSE.
2839  * \returns TRUE if inheritance demands (any kind) are present, FALSE otherwise.
2840  */
2841 MonoBoolean
2842 mono_declsec_get_inheritdemands_class (MonoClass *klass, MonoDeclSecurityActions* demands)
2843 {
2844         MonoBoolean result = FALSE;
2845         guint32 flags;
2846
2847         /* quick exit if no declarative security is present in the metadata */
2848         if (!klass->image->tables [MONO_TABLE_DECLSECURITY].rows)
2849                 return FALSE;
2850
2851         /* Here we use (or create) the class declarative cache to look for demands */
2852         flags = mono_declsec_flags_from_class (klass);
2853         if (flags & (MONO_DECLSEC_FLAG_INHERITANCEDEMAND | MONO_DECLSEC_FLAG_NONCAS_INHERITANCEDEMAND | MONO_DECLSEC_FLAG_INHERITANCEDEMAND_CHOICE)) {
2854                 mono_class_init (klass);
2855                 memset (demands, 0, sizeof (MonoDeclSecurityActions));
2856
2857                 result |= mono_declsec_get_class_demands_params (klass, demands, 
2858                         SECURITY_ACTION_INHERITDEMAND, SECURITY_ACTION_NONCASINHERITANCE, SECURITY_ACTION_INHERITDEMANDCHOICE);
2859         }
2860
2861         return result;
2862 }
2863
2864 /**
2865  * mono_declsec_get_inheritdemands_method:
2866  * Collect all Inherit actions: \c InheritanceDemand, \c NonCasInheritanceDemand and \c InheritanceDemandChoice (2.0).
2867  * Don't use the content of actions if the function return FALSE.
2868  */
2869 MonoBoolean
2870 mono_declsec_get_inheritdemands_method (MonoMethod *method, MonoDeclSecurityActions* demands)
2871 {
2872         /* quick exit if no declarative security is present in the metadata */
2873         if (!method->klass->image->tables [MONO_TABLE_DECLSECURITY].rows)
2874                 return FALSE;
2875
2876         /* we want the original as the wrapper is "free" of the security informations */
2877         if (method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE || method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
2878                 method = mono_marshal_method_from_wrapper (method);
2879                 if (!method)
2880                         return FALSE;
2881         }
2882
2883         if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2884                 mono_class_init (method->klass);
2885                 memset (demands, 0, sizeof (MonoDeclSecurityActions));
2886
2887                 return mono_declsec_get_method_demands_params (method, demands, 
2888                         SECURITY_ACTION_INHERITDEMAND, SECURITY_ACTION_NONCASINHERITANCE, SECURITY_ACTION_INHERITDEMANDCHOICE);
2889         }
2890         return FALSE;
2891 }
2892
2893
2894 static MonoBoolean
2895 get_declsec_action (MonoImage *image, guint32 token, guint32 action, MonoDeclSecurityEntry *entry)
2896 {
2897         guint32 cols [MONO_DECL_SECURITY_SIZE];
2898         MonoTableInfo *t;
2899         int i;
2900
2901         int index = mono_metadata_declsec_from_index (image, token);
2902         if (index == -1)
2903                 return FALSE;
2904
2905         t =  &image->tables [MONO_TABLE_DECLSECURITY];
2906         for (i = index; i < t->rows; i++) {
2907                 mono_metadata_decode_row (t, i, cols, MONO_DECL_SECURITY_SIZE);
2908
2909                 /* shortcut - index are ordered */
2910                 if (token != cols [MONO_DECL_SECURITY_PARENT])
2911                         return FALSE;
2912
2913                 if (cols [MONO_DECL_SECURITY_ACTION] == action) {
2914                         const char *metadata = mono_metadata_blob_heap (image, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
2915                         entry->blob = (char*) (metadata + 2);
2916                         entry->size = mono_metadata_decode_blob_size (metadata, &metadata);
2917                         return TRUE;
2918                 }
2919         }
2920
2921         return FALSE;
2922 }
2923
2924 MonoBoolean
2925 mono_declsec_get_method_action (MonoMethod *method, guint32 action, MonoDeclSecurityEntry *entry)
2926 {
2927         if (method->flags & METHOD_ATTRIBUTE_HAS_SECURITY) {
2928                 guint32 idx = mono_method_get_index (method);
2929                 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2930                 idx |= MONO_HAS_DECL_SECURITY_METHODDEF;
2931                 return get_declsec_action (method->klass->image, idx, action, entry);
2932         }
2933         return FALSE;
2934 }
2935
2936 /**
2937  * mono_declsec_get_class_action:
2938  */
2939 MonoBoolean
2940 mono_declsec_get_class_action (MonoClass *klass, guint32 action, MonoDeclSecurityEntry *entry)
2941 {
2942         /* use cache */
2943         guint32 flags = mono_declsec_flags_from_class (klass);
2944         if (declsec_flags_map [action] & flags) {
2945                 guint32 idx = mono_metadata_token_index (klass->type_token);
2946                 idx <<= MONO_HAS_DECL_SECURITY_BITS;
2947                 idx |= MONO_HAS_DECL_SECURITY_TYPEDEF;
2948                 return get_declsec_action (klass->image, idx, action, entry);
2949         }
2950         return FALSE;
2951 }
2952
2953 /**
2954  * mono_declsec_get_assembly_action:
2955  */
2956 MonoBoolean
2957 mono_declsec_get_assembly_action (MonoAssembly *assembly, guint32 action, MonoDeclSecurityEntry *entry)
2958 {
2959         guint32 idx = 1; /* there is only one assembly */
2960         idx <<= MONO_HAS_DECL_SECURITY_BITS;
2961         idx |= MONO_HAS_DECL_SECURITY_ASSEMBLY;
2962
2963         return get_declsec_action (assembly->image, idx, action, entry);
2964 }
2965
2966 gboolean
2967 mono_reflection_call_is_assignable_to (MonoClass *klass, MonoClass *oklass, MonoError *error)
2968 {
2969         MonoObject *res, *exc;
2970         void *params [1];
2971         static MonoMethod *method = NULL;
2972
2973         error_init (error);
2974
2975         if (method == NULL) {
2976                 method = mono_class_get_method_from_name (mono_class_get_type_builder_class (), "IsAssignableTo", 1);
2977                 g_assert (method);
2978         }
2979
2980         /* 
2981          * The result of mono_type_get_object_checked () might be a System.MonoType but we
2982          * need a TypeBuilder so use mono_class_get_ref_info (klass).
2983          */
2984         g_assert (mono_class_has_ref_info (klass));
2985         g_assert (!strcmp (mono_object_class (mono_class_get_ref_info_raw (klass))->name, "TypeBuilder")); /* FIXME use handles */
2986
2987         params [0] = mono_type_get_object_checked (mono_domain_get (), &oklass->byval_arg, error);
2988         return_val_if_nok (error, FALSE);
2989
2990         MonoError inner_error;
2991         res = mono_runtime_try_invoke (method, mono_class_get_ref_info_raw (klass), params, &exc, &inner_error); /* FIXME use handles */
2992
2993         if (exc || !is_ok (&inner_error)) {
2994                 mono_error_cleanup (&inner_error);
2995                 return FALSE;
2996         } else
2997                 return *(MonoBoolean*)mono_object_unbox (res);
2998 }
2999
3000 /**
3001  * mono_reflection_type_get_type:
3002  * \param reftype the \c System.Type object
3003  * \returns the \c MonoType* associated with the C# \c System.Type object \p reftype.
3004  */
3005 MonoType*
3006 mono_reflection_type_get_type (MonoReflectionType *reftype)
3007 {
3008         g_assert (reftype);
3009
3010         MonoError error;
3011         MonoType *result = mono_reflection_type_get_handle (reftype, &error);
3012         mono_error_assert_ok (&error);
3013         return result;
3014 }
3015
3016 /**
3017  * mono_reflection_assembly_get_assembly:
3018  * \param refassembly the \c System.Reflection.Assembly object
3019  * \returns the \c MonoAssembly* associated with the C# \c System.Reflection.Assembly object \p refassembly.
3020  */
3021 MonoAssembly*
3022 mono_reflection_assembly_get_assembly (MonoReflectionAssembly *refassembly)
3023 {
3024         g_assert (refassembly);
3025
3026         return refassembly->assembly;
3027 }
3028
3029 /**
3030  * mono_class_from_mono_type_handle:
3031  * \param reftype the \c System.Type handle
3032  * \returns the \c MonoClass* corresponding to the given type.
3033  */
3034 MonoClass*
3035 mono_class_from_mono_type_handle (MonoReflectionTypeHandle reftype)
3036 {
3037         return mono_class_from_mono_type (MONO_HANDLE_RAW (reftype)->type);
3038 }