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