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