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