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