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