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