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