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