2 * class.c: Class management for the Mono runtime
5 * Miguel de Icaza (miguel@ximian.com)
7 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
8 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
9 * Copyright 2012 Xamarin Inc (http://www.xamarin.com)
19 #include <mono/metadata/image.h>
20 #include <mono/metadata/assembly.h>
21 #include <mono/metadata/metadata.h>
22 #include <mono/metadata/metadata-internals.h>
23 #include <mono/metadata/profiler-private.h>
24 #include <mono/metadata/tabledefs.h>
25 #include <mono/metadata/tokentype.h>
26 #include <mono/metadata/class-internals.h>
27 #include <mono/metadata/object.h>
28 #include <mono/metadata/appdomain.h>
29 #include <mono/metadata/mono-endian.h>
30 #include <mono/metadata/debug-helpers.h>
31 #include <mono/metadata/reflection.h>
32 #include <mono/metadata/exception.h>
33 #include <mono/metadata/security-manager.h>
34 #include <mono/metadata/security-core-clr.h>
35 #include <mono/metadata/attrdefs.h>
36 #include <mono/metadata/gc-internal.h>
37 #include <mono/metadata/verify-internals.h>
38 #include <mono/metadata/mono-debug.h>
39 #include <mono/utils/mono-counters.h>
40 #include <mono/utils/mono-string.h>
41 #include <mono/utils/mono-error-internals.h>
42 #include <mono/utils/mono-logger-internal.h>
43 #include <mono/utils/mono-memory-model.h>
44 #include <mono/utils/atomic.h>
45 #include <mono/utils/bsearch.h>
49 gboolean mono_print_vtable = FALSE;
50 gboolean mono_align_small_structs = FALSE;
53 guint32 inflated_classes, inflated_classes_size, inflated_methods_size;
54 guint32 classes_size, class_ext_size;
56 /* Low level lock which protects data structures in this module */
57 static mono_mutex_t classes_mutex;
59 /* Function supplied by the runtime to find classes by name using information from the AOT file */
60 static MonoGetClassFromName get_class_from_name = NULL;
62 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token, MonoError *error);
63 static gboolean mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res);
64 static gboolean can_access_type (MonoClass *access_klass, MonoClass *member_klass);
65 static MonoMethod* find_method_in_metadata (MonoClass *klass, const char *name, int param_count, int flags);
66 static int generic_array_methods (MonoClass *class);
67 static void setup_generic_array_ifaces (MonoClass *class, MonoClass *iface, MonoMethod **methods, int pos);
69 static MonoMethod* mono_class_get_virtual_methods (MonoClass* klass, gpointer *iter);
70 static char* mono_assembly_name_from_token (MonoImage *image, guint32 type_token);
71 static void mono_field_resolve_type (MonoClassField *field, MonoError *error);
72 static guint32 mono_field_resolve_flags (MonoClassField *field);
73 static void mono_class_setup_vtable_full (MonoClass *class, GList *in_setup);
74 static void mono_generic_class_setup_parent (MonoClass *klass, MonoClass *gklass);
77 We use gclass recording to allow recursive system f types to be referenced by a parent.
79 Given the following type hierarchy:
81 class TextBox : TextBoxBase<TextBox> {}
82 class TextBoxBase<T> : TextInput<TextBox> where T : TextBoxBase<T> {}
83 class TextInput<T> : Input<T> where T: TextInput<T> {}
86 The runtime tries to load TextBoxBase<>.
87 To load TextBoxBase<> to do so it must resolve the parent which is TextInput<TextBox>.
88 To instantiate TextInput<TextBox> it must resolve TextInput<> and TextBox.
89 To load TextBox it must resolve the parent which is TextBoxBase<TextBox>.
91 At this point the runtime must instantiate TextBoxBase<TextBox>. Both types are partially loaded
92 at this point, iow, both are registered in the type map and both and a NULL parent. This means
93 that the resulting generic instance will have a NULL parent, which is wrong and will cause breakage.
95 To fix that what we do is to record all generic instantes created while resolving the parent of
96 any generic type definition and, after resolved, correct the parent field if needed.
99 static int record_gclass_instantiation;
100 static GSList *gclass_recorded_list;
101 typedef gboolean (*gclass_record_func) (MonoClass*, void*);
106 mono_locks_acquire (&classes_mutex, ClassesLock);
110 classes_unlock (void)
112 mono_locks_release (&classes_mutex, ClassesLock);
116 * LOCKING: loader lock must be held until pairing disable_gclass_recording is called.
119 enable_gclass_recording (void)
121 ++record_gclass_instantiation;
125 * LOCKING: loader lock must be held since pairing enable_gclass_recording was called.
128 disable_gclass_recording (gclass_record_func func, void *user_data)
130 GSList **head = &gclass_recorded_list;
132 g_assert (record_gclass_instantiation > 0);
133 --record_gclass_instantiation;
136 GSList *node = *head;
137 if (func ((MonoClass*)node->data, user_data)) {
139 g_slist_free_1 (node);
145 /* We automatically discard all recorded gclasses when disabled. */
146 if (!record_gclass_instantiation && gclass_recorded_list) {
147 g_slist_free (gclass_recorded_list);
148 gclass_recorded_list = NULL;
153 * mono_class_from_typeref:
154 * @image: a MonoImage
155 * @type_token: a TypeRef token
157 * Creates the MonoClass* structure representing the type defined by
158 * the typeref token valid inside @image.
159 * Returns: the MonoClass* representing the typeref token, NULL ifcould
163 mono_class_from_typeref (MonoImage *image, guint32 type_token)
166 MonoClass *class = mono_class_from_typeref_checked (image, type_token, &error);
167 g_assert (mono_error_ok (&error)); /*FIXME proper error handling*/
172 mono_class_from_typeref_checked (MonoImage *image, guint32 type_token, MonoError *error)
174 guint32 cols [MONO_TYPEREF_SIZE];
175 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
177 const char *name, *nspace;
178 MonoClass *res = NULL;
181 mono_error_init (error);
183 if (!mono_verifier_verify_typeref_row (image, (type_token & 0xffffff) - 1, error))
186 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
188 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
189 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
191 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLUTION_SCOPE_BITS;
192 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLUTION_SCOPE_MASK) {
193 case MONO_RESOLUTION_SCOPE_MODULE:
195 LAMESPEC The spec says that a null module resolution scope should go through the exported type table.
196 This is not the observed behavior of existing implementations.
197 The defacto behavior is that it's just a typedef in disguise.
199 /* a typedef in disguise */
200 res = mono_class_from_name_checked (image, nspace, name, error);
203 case MONO_RESOLUTION_SCOPE_MODULEREF:
204 module = mono_image_load_module (image, idx);
206 res = mono_class_from_name_checked (module, nspace, name, error);
209 case MONO_RESOLUTION_SCOPE_TYPEREF: {
210 MonoClass *enclosing;
213 if (idx == mono_metadata_token_index (type_token)) {
214 mono_error_set_bad_image (error, image, "Image with self-referencing typeref token %08x.", type_token);
218 enclosing = mono_class_from_typeref_checked (image, MONO_TOKEN_TYPE_REF | idx, error);
219 if (!mono_error_ok (error))
222 if (enclosing->nested_classes_inited && enclosing->ext) {
223 /* Micro-optimization: don't scan the metadata tables if enclosing is already inited */
224 for (tmp = enclosing->ext->nested_classes; tmp; tmp = tmp->next) {
226 if (strcmp (res->name, name) == 0)
230 /* Don't call mono_class_init as we might've been called by it recursively */
231 int i = mono_metadata_nesting_typedef (enclosing->image, enclosing->type_token, 1);
233 guint32 class_nested = mono_metadata_decode_row_col (&enclosing->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, MONO_NESTED_CLASS_NESTED);
234 guint32 string_offset = mono_metadata_decode_row_col (&enclosing->image->tables [MONO_TABLE_TYPEDEF], class_nested - 1, MONO_TYPEDEF_NAME);
235 const char *nname = mono_metadata_string_heap (enclosing->image, string_offset);
237 if (strcmp (nname, name) == 0)
238 return mono_class_create_from_typedef (enclosing->image, MONO_TOKEN_TYPE_DEF | class_nested, error);
240 i = mono_metadata_nesting_typedef (enclosing->image, enclosing->type_token, i + 1);
243 g_warning ("TypeRef ResolutionScope not yet handled (%d) for %s.%s in image %s", idx, nspace, name, image->name);
246 case MONO_RESOLUTION_SCOPE_ASSEMBLYREF:
250 if (idx > image->tables [MONO_TABLE_ASSEMBLYREF].rows) {
251 mono_error_set_bad_image (error, image, "Image with invalid assemblyref token %08x.", idx);
255 if (!image->references || !image->references [idx - 1])
256 mono_assembly_load_reference (image, idx - 1);
257 g_assert (image->references [idx - 1]);
259 /* If the assembly did not load, register this as a type load exception */
260 if (image->references [idx - 1] == REFERENCE_MISSING){
261 MonoAssemblyName aname;
264 mono_assembly_get_assemblyref (image, idx - 1, &aname);
265 human_name = mono_stringify_assembly_name (&aname);
266 mono_error_set_assembly_load_simple (error, human_name, image->assembly ? image->assembly->ref_only : FALSE);
270 res = mono_class_from_name_checked (image->references [idx - 1]->image, nspace, name, error);
273 /* Generic case, should be avoided for when a better error is possible. */
274 if (!res && mono_error_ok (error)) {
275 if (mono_loader_get_last_error ()) { /*FIXME plug the above to not leak errors*/
276 mono_error_set_from_loader_error (error);
278 char *name = mono_class_name_from_token (image, type_token);
279 char *assembly = mono_assembly_name_from_token (image, type_token);
280 mono_error_set_type_load_name (error, name, assembly, "Could not resolve type with token %08x", type_token);
283 mono_loader_assert_no_error ();
289 mono_image_memdup (MonoImage *image, void *data, guint size)
291 void *res = mono_image_alloc (image, size);
292 memcpy (res, data, size);
296 /* Copy everything mono_metadata_free_array free. */
298 mono_dup_array_type (MonoImage *image, MonoArrayType *a)
301 a = mono_image_memdup (image, a, sizeof (MonoArrayType));
303 a->sizes = mono_image_memdup (image, a->sizes, a->numsizes * sizeof (int));
305 a->lobounds = mono_image_memdup (image, a->lobounds, a->numlobounds * sizeof (int));
307 a = g_memdup (a, sizeof (MonoArrayType));
309 a->sizes = g_memdup (a->sizes, a->numsizes * sizeof (int));
311 a->lobounds = g_memdup (a->lobounds, a->numlobounds * sizeof (int));
316 /* Copy everything mono_metadata_free_method_signature free. */
318 mono_metadata_signature_deep_dup (MonoImage *image, MonoMethodSignature *sig)
322 sig = mono_metadata_signature_dup_full (image, sig);
324 sig->ret = mono_metadata_type_dup (image, sig->ret);
325 for (i = 0; i < sig->param_count; ++i)
326 sig->params [i] = mono_metadata_type_dup (image, sig->params [i]);
332 _mono_type_get_assembly_name (MonoClass *klass, GString *str)
334 MonoAssembly *ta = klass->image->assembly;
337 name = mono_stringify_assembly_name (&ta->aname);
338 g_string_append_printf (str, ", %s", name);
343 mono_type_name_check_byref (MonoType *type, GString *str)
346 g_string_append_c (str, '&');
350 * mono_identifier_escape_type_name_chars:
351 * @str: a destination string
352 * @identifier: an IDENTIFIER in internal form
356 * The displayed form of the identifier is appended to str.
358 * The displayed form of an identifier has the characters ,+&*[]\
359 * that have special meaning in type names escaped with a preceeding
360 * backslash (\) character.
363 mono_identifier_escape_type_name_chars (GString* str, const char* identifier)
369 // reserve space for common case: there will be no escaped characters.
370 g_string_set_size(str, n + strlen(identifier));
371 g_string_set_size(str, n);
373 for (const char* s = identifier; *s != 0 ; s++) {
382 g_string_append_c (str, '\\');
383 g_string_append_c (str, *s);
386 g_string_append_c (str, *s);
394 mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed,
395 MonoTypeNameFormat format)
399 switch (type->type) {
400 case MONO_TYPE_ARRAY: {
401 int i, rank = type->data.array->rank;
402 MonoTypeNameFormat nested_format;
404 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
405 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
407 mono_type_get_name_recurse (
408 &type->data.array->eklass->byval_arg, str, FALSE, nested_format);
409 g_string_append_c (str, '[');
411 g_string_append_c (str, '*');
412 for (i = 1; i < rank; i++)
413 g_string_append_c (str, ',');
414 g_string_append_c (str, ']');
416 mono_type_name_check_byref (type, str);
418 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
419 _mono_type_get_assembly_name (type->data.array->eklass, str);
422 case MONO_TYPE_SZARRAY: {
423 MonoTypeNameFormat nested_format;
425 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
426 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
428 mono_type_get_name_recurse (
429 &type->data.klass->byval_arg, str, FALSE, nested_format);
430 g_string_append (str, "[]");
432 mono_type_name_check_byref (type, str);
434 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
435 _mono_type_get_assembly_name (type->data.klass, str);
438 case MONO_TYPE_PTR: {
439 MonoTypeNameFormat nested_format;
441 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
442 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
444 mono_type_get_name_recurse (
445 type->data.type, str, FALSE, nested_format);
446 g_string_append_c (str, '*');
448 mono_type_name_check_byref (type, str);
450 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
451 _mono_type_get_assembly_name (mono_class_from_mono_type (type->data.type), str);
456 if (!mono_generic_param_info (type->data.generic_param))
457 g_string_append_printf (str, "%s%d", type->type == MONO_TYPE_VAR ? "!" : "!!", type->data.generic_param->num);
459 g_string_append (str, mono_generic_param_info (type->data.generic_param)->name);
461 mono_type_name_check_byref (type, str);
465 klass = mono_class_from_mono_type (type);
466 if (klass->nested_in) {
467 mono_type_get_name_recurse (
468 &klass->nested_in->byval_arg, str, TRUE, format);
469 if (format == MONO_TYPE_NAME_FORMAT_IL)
470 g_string_append_c (str, '.');
472 g_string_append_c (str, '+');
473 } else if (*klass->name_space) {
474 if (format == MONO_TYPE_NAME_FORMAT_IL)
475 g_string_append (str, klass->name_space);
477 mono_identifier_escape_type_name_chars (str, klass->name_space);
478 g_string_append_c (str, '.');
480 if (format == MONO_TYPE_NAME_FORMAT_IL) {
481 char *s = strchr (klass->name, '`');
482 int len = s ? s - klass->name : strlen (klass->name);
483 g_string_append_len (str, klass->name, len);
485 mono_identifier_escape_type_name_chars (str, klass->name);
489 if (klass->generic_class) {
490 MonoGenericClass *gclass = klass->generic_class;
491 MonoGenericInst *inst = gclass->context.class_inst;
492 MonoTypeNameFormat nested_format;
495 nested_format = format == MONO_TYPE_NAME_FORMAT_FULL_NAME ?
496 MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED : format;
498 if (format == MONO_TYPE_NAME_FORMAT_IL)
499 g_string_append_c (str, '<');
501 g_string_append_c (str, '[');
502 for (i = 0; i < inst->type_argc; i++) {
503 MonoType *t = inst->type_argv [i];
506 g_string_append_c (str, ',');
507 if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
508 (t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
509 g_string_append_c (str, '[');
510 mono_type_get_name_recurse (inst->type_argv [i], str, FALSE, nested_format);
511 if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
512 (t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
513 g_string_append_c (str, ']');
515 if (format == MONO_TYPE_NAME_FORMAT_IL)
516 g_string_append_c (str, '>');
518 g_string_append_c (str, ']');
519 } else if (klass->generic_container &&
520 (format != MONO_TYPE_NAME_FORMAT_FULL_NAME) &&
521 (format != MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)) {
524 if (format == MONO_TYPE_NAME_FORMAT_IL)
525 g_string_append_c (str, '<');
527 g_string_append_c (str, '[');
528 for (i = 0; i < klass->generic_container->type_argc; i++) {
530 g_string_append_c (str, ',');
531 g_string_append (str, mono_generic_container_get_param_info (klass->generic_container, i)->name);
533 if (format == MONO_TYPE_NAME_FORMAT_IL)
534 g_string_append_c (str, '>');
536 g_string_append_c (str, ']');
539 mono_type_name_check_byref (type, str);
541 if ((format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
542 (type->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
543 _mono_type_get_assembly_name (klass, str);
549 * mono_type_get_name_full:
551 * @format: the format for the return string.
554 * Returns: the string representation in a number of formats:
556 * if format is MONO_TYPE_NAME_FORMAT_REFLECTION, the return string is
557 * returned in the formatrequired by System.Reflection, this is the
558 * inverse of mono_reflection_parse_type ().
560 * if format is MONO_TYPE_NAME_FORMAT_IL, it returns a syntax that can
561 * be used by the IL assembler.
563 * if format is MONO_TYPE_NAME_FORMAT_FULL_NAME
565 * if format is MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
568 mono_type_get_name_full (MonoType *type, MonoTypeNameFormat format)
572 result = g_string_new ("");
574 mono_type_get_name_recurse (type, result, FALSE, format);
576 return g_string_free (result, FALSE);
580 * mono_type_get_full_name:
583 * Returns: the string representation for type as required by System.Reflection.
584 * The inverse of mono_reflection_parse_type ().
587 mono_type_get_full_name (MonoClass *class)
589 return mono_type_get_name_full (mono_class_get_type (class), MONO_TYPE_NAME_FORMAT_REFLECTION);
593 * mono_type_get_name:
596 * Returns: the string representation for type as it would be represented in IL code.
599 mono_type_get_name (MonoType *type)
601 return mono_type_get_name_full (type, MONO_TYPE_NAME_FORMAT_IL);
605 * mono_type_get_underlying_type:
608 * Returns: the MonoType for the underlying integer type if @type
609 * is an enum and byref is false, otherwise the type itself.
612 mono_type_get_underlying_type (MonoType *type)
614 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype && !type->byref)
615 return mono_class_enum_basetype (type->data.klass);
616 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype && !type->byref)
617 return mono_class_enum_basetype (type->data.generic_class->container_class);
622 * mono_class_is_open_constructed_type:
625 * Returns TRUE if type represents a generics open constructed type.
626 * IOW, not all type parameters required for the instantiation have
627 * been provided or it's a generic type definition.
629 * An open constructed type means it's a non realizable type. Not to
630 * be mixed up with an abstract type - we can't cast or dispatch to
631 * an open type, for example.
634 mono_class_is_open_constructed_type (MonoType *t)
640 case MONO_TYPE_SZARRAY:
641 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
642 case MONO_TYPE_ARRAY:
643 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
645 return mono_class_is_open_constructed_type (t->data.type);
646 case MONO_TYPE_GENERICINST:
647 return t->data.generic_class->context.class_inst->is_open;
648 case MONO_TYPE_CLASS:
649 case MONO_TYPE_VALUETYPE:
650 return t->data.klass->generic_container != NULL;
657 This is a simple function to catch the most common bad instances of generic types.
658 Specially those that might lead to further failures in the runtime.
661 is_valid_generic_argument (MonoType *type)
663 switch (type->type) {
665 //case MONO_TYPE_TYPEDBYREF:
673 inflate_generic_type (MonoImage *image, MonoType *type, MonoGenericContext *context, MonoError *error)
675 mono_error_init (error);
677 switch (type->type) {
678 case MONO_TYPE_MVAR: {
680 int num = mono_type_get_generic_param_num (type);
681 MonoGenericInst *inst = context->method_inst;
684 if (num >= inst->type_argc) {
685 MonoGenericParamInfo *info = mono_generic_param_info (type->data.generic_param);
686 mono_error_set_bad_image (error, image, "MVAR %d (%s) cannot be expanded in this context with %d instantiations",
687 num, info ? info->name : "", inst->type_argc);
691 if (!is_valid_generic_argument (inst->type_argv [num])) {
692 MonoGenericParamInfo *info = mono_generic_param_info (type->data.generic_param);
693 mono_error_set_bad_image (error, image, "MVAR %d (%s) cannot be expanded with type 0x%x",
694 num, info ? info->name : "", inst->type_argv [num]->type);
698 * Note that the VAR/MVAR cases are different from the rest. The other cases duplicate @type,
699 * while the VAR/MVAR duplicates a type from the context. So, we need to ensure that the
700 * ->byref and ->attrs from @type are propagated to the returned type.
702 nt = mono_metadata_type_dup (image, inst->type_argv [num]);
703 nt->byref = type->byref;
704 nt->attrs = type->attrs;
707 case MONO_TYPE_VAR: {
709 int num = mono_type_get_generic_param_num (type);
710 MonoGenericInst *inst = context->class_inst;
713 if (num >= inst->type_argc) {
714 MonoGenericParamInfo *info = mono_generic_param_info (type->data.generic_param);
715 mono_error_set_bad_image (error, image, "VAR %d (%s) cannot be expanded in this context with %d instantiations",
716 num, info ? info->name : "", inst->type_argc);
719 if (!is_valid_generic_argument (inst->type_argv [num])) {
720 MonoGenericParamInfo *info = mono_generic_param_info (type->data.generic_param);
721 mono_error_set_bad_image (error, image, "VAR %d (%s) cannot be expanded with type 0x%x",
722 num, info ? info->name : "", inst->type_argv [num]->type);
725 nt = mono_metadata_type_dup (image, inst->type_argv [num]);
726 nt->byref = type->byref;
727 nt->attrs = type->attrs;
730 case MONO_TYPE_SZARRAY: {
731 MonoClass *eclass = type->data.klass;
732 MonoType *nt, *inflated = inflate_generic_type (NULL, &eclass->byval_arg, context, error);
733 if (!inflated || !mono_error_ok (error))
735 nt = mono_metadata_type_dup (image, type);
736 nt->data.klass = mono_class_from_mono_type (inflated);
737 mono_metadata_free_type (inflated);
740 case MONO_TYPE_ARRAY: {
741 MonoClass *eclass = type->data.array->eklass;
742 MonoType *nt, *inflated = inflate_generic_type (NULL, &eclass->byval_arg, context, error);
743 if (!inflated || !mono_error_ok (error))
745 nt = mono_metadata_type_dup (image, type);
746 nt->data.array->eklass = mono_class_from_mono_type (inflated);
747 mono_metadata_free_type (inflated);
750 case MONO_TYPE_GENERICINST: {
751 MonoGenericClass *gclass = type->data.generic_class;
752 MonoGenericInst *inst;
754 if (!gclass->context.class_inst->is_open)
757 inst = mono_metadata_inflate_generic_inst (gclass->context.class_inst, context, error);
758 if (!mono_error_ok (error))
760 if (inst != gclass->context.class_inst)
761 gclass = mono_metadata_lookup_generic_class (gclass->container_class, inst, gclass->is_dynamic);
763 if (gclass == type->data.generic_class)
766 nt = mono_metadata_type_dup (image, type);
767 nt->data.generic_class = gclass;
770 case MONO_TYPE_CLASS:
771 case MONO_TYPE_VALUETYPE: {
772 MonoClass *klass = type->data.klass;
773 MonoGenericContainer *container = klass->generic_container;
774 MonoGenericInst *inst;
775 MonoGenericClass *gclass = NULL;
781 /* We can't use context->class_inst directly, since it can have more elements */
782 inst = mono_metadata_inflate_generic_inst (container->context.class_inst, context, error);
783 if (!mono_error_ok (error))
785 if (inst == container->context.class_inst)
788 gclass = mono_metadata_lookup_generic_class (klass, inst, image_is_dynamic (klass->image));
790 nt = mono_metadata_type_dup (image, type);
791 nt->type = MONO_TYPE_GENERICINST;
792 nt->data.generic_class = gclass;
802 mono_generic_class_get_context (MonoGenericClass *gclass)
804 return &gclass->context;
808 mono_class_get_context (MonoClass *class)
810 return class->generic_class ? mono_generic_class_get_context (class->generic_class) : NULL;
814 * mono_class_get_generic_container:
816 * Return the generic container of KLASS which should be a generic type definition.
818 MonoGenericContainer*
819 mono_class_get_generic_container (MonoClass *klass)
821 g_assert (klass->is_generic);
823 return klass->generic_container;
827 * mono_class_get_generic_class:
829 * Return the MonoGenericClass of KLASS, which should be a generic instance.
832 mono_class_get_generic_class (MonoClass *klass)
834 g_assert (klass->is_inflated);
836 return klass->generic_class;
840 * mono_class_inflate_generic_type_with_mempool:
841 * @mempool: a mempool
843 * @context: a generics context
844 * @error: error context
846 * The same as mono_class_inflate_generic_type, but allocates the MonoType
847 * from mempool if it is non-NULL. If it is NULL, the MonoType is
848 * allocated on the heap and is owned by the caller.
849 * The returned type can potentially be the same as TYPE, so it should not be
850 * modified by the caller, and it should be freed using mono_metadata_free_type ().
853 mono_class_inflate_generic_type_with_mempool (MonoImage *image, MonoType *type, MonoGenericContext *context, MonoError *error)
855 MonoType *inflated = NULL;
856 mono_error_init (error);
859 inflated = inflate_generic_type (image, type, context, error);
860 if (!mono_error_ok (error))
864 MonoType *shared = mono_metadata_get_shared_type (type);
869 return mono_metadata_type_dup (image, type);
873 mono_stats.inflated_type_count++;
878 * mono_class_inflate_generic_type:
880 * @context: a generics context
882 * If @type is a generic type and @context is not NULL, instantiate it using the
883 * generics context @context.
885 * Returns: the instantiated type or a copy of @type. The returned MonoType is allocated
886 * on the heap and is owned by the caller. Returns NULL on error.
888 * @deprecated Please use mono_class_inflate_generic_type_checked instead
891 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
895 result = mono_class_inflate_generic_type_checked (type, context, &error);
897 if (!mono_error_ok (&error)) {
898 mono_error_cleanup (&error);
905 * mono_class_inflate_generic_type:
907 * @context: a generics context
908 * @error: error context to use
910 * If @type is a generic type and @context is not NULL, instantiate it using the
911 * generics context @context.
913 * Returns: the instantiated type or a copy of @type. The returned MonoType is allocated
914 * on the heap and is owned by the caller.
917 mono_class_inflate_generic_type_checked (MonoType *type, MonoGenericContext *context, MonoError *error)
919 return mono_class_inflate_generic_type_with_mempool (NULL, type, context, error);
923 * mono_class_inflate_generic_type_no_copy:
925 * Same as inflate_generic_type_with_mempool, but return TYPE if no inflation
929 mono_class_inflate_generic_type_no_copy (MonoImage *image, MonoType *type, MonoGenericContext *context, MonoError *error)
931 MonoType *inflated = NULL;
933 mono_error_init (error);
935 inflated = inflate_generic_type (image, type, context, error);
936 if (!mono_error_ok (error))
943 mono_stats.inflated_type_count++;
948 mono_class_inflate_generic_class_checked (MonoClass *gklass, MonoGenericContext *context, MonoError *error)
953 inflated = mono_class_inflate_generic_type_checked (&gklass->byval_arg, context, error);
954 if (!mono_error_ok (error))
957 res = mono_class_from_mono_type (inflated);
958 mono_metadata_free_type (inflated);
963 * mono_class_inflate_generic_class:
965 * Inflate the class GKLASS with CONTEXT.
968 mono_class_inflate_generic_class (MonoClass *gklass, MonoGenericContext *context)
973 res = mono_class_inflate_generic_class_checked (gklass, context, &error);
974 g_assert (mono_error_ok (&error)); /*FIXME proper error handling*/
981 static MonoGenericContext
982 inflate_generic_context (MonoGenericContext *context, MonoGenericContext *inflate_with, MonoError *error)
984 MonoGenericInst *class_inst = NULL;
985 MonoGenericInst *method_inst = NULL;
986 MonoGenericContext res = { NULL, NULL };
988 mono_error_init (error);
990 if (context->class_inst) {
991 class_inst = mono_metadata_inflate_generic_inst (context->class_inst, inflate_with, error);
992 if (!mono_error_ok (error))
996 if (context->method_inst) {
997 method_inst = mono_metadata_inflate_generic_inst (context->method_inst, inflate_with, error);
998 if (!mono_error_ok (error))
1002 res.class_inst = class_inst;
1003 res.method_inst = method_inst;
1009 * mono_class_inflate_generic_method:
1010 * @method: a generic method
1011 * @context: a generics context
1013 * Instantiate the generic method @method using the generics context @context.
1015 * Returns: the new instantiated method
1018 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context)
1020 return mono_class_inflate_generic_method_full (method, NULL, context);
1024 mono_class_inflate_generic_method_checked (MonoMethod *method, MonoGenericContext *context, MonoError *error)
1026 return mono_class_inflate_generic_method_full_checked (method, NULL, context, error);
1030 * mono_class_inflate_generic_method_full:
1032 * Instantiate method @method with the generic context @context.
1033 * BEWARE: All non-trivial fields are invalid, including klass, signature, and header.
1034 * Use mono_method_signature () and mono_method_get_header () to get the correct values.
1037 mono_class_inflate_generic_method_full (MonoMethod *method, MonoClass *klass_hint, MonoGenericContext *context)
1040 MonoMethod *res = mono_class_inflate_generic_method_full_checked (method, klass_hint, context, &error);
1041 if (!mono_error_ok (&error))
1042 /*FIXME do proper error handling - on this case, kill this function. */
1043 g_error ("Could not inflate generic method due to %s", mono_error_get_message (&error));
1049 * mono_class_inflate_generic_method_full_checked:
1050 * Same as mono_class_inflate_generic_method_full but return failure using @error.
1053 mono_class_inflate_generic_method_full_checked (MonoMethod *method, MonoClass *klass_hint, MonoGenericContext *context, MonoError *error)
1056 MonoMethodInflated *iresult, *cached;
1057 MonoMethodSignature *sig;
1058 MonoGenericContext tmp_context;
1060 mono_error_init (error);
1062 /* The `method' has already been instantiated before => we need to peel out the instantiation and create a new context */
1063 while (method->is_inflated) {
1064 MonoGenericContext *method_context = mono_method_get_context (method);
1065 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
1067 tmp_context = inflate_generic_context (method_context, context, error);
1068 if (!mono_error_ok (error))
1070 context = &tmp_context;
1072 if (mono_metadata_generic_context_equal (method_context, context))
1075 method = imethod->declaring;
1079 * A method only needs to be inflated if the context has argument for which it is
1082 * class Foo<T> { void Bar(); } - doesn't need to be inflated if only mvars' are supplied
1083 * class Foo { void Bar<T> (); } - doesn't need to be if only vars' are supplied
1086 if (!((method->is_generic && context->method_inst) ||
1087 (method->klass->generic_container && context->class_inst)))
1090 iresult = g_new0 (MonoMethodInflated, 1);
1091 iresult->context = *context;
1092 iresult->declaring = method;
1094 if (!context->method_inst && method->is_generic)
1095 iresult->context.method_inst = mono_method_get_generic_container (method)->context.method_inst;
1097 if (!context->class_inst) {
1098 g_assert (!iresult->declaring->klass->generic_class);
1099 if (iresult->declaring->klass->generic_container)
1100 iresult->context.class_inst = iresult->declaring->klass->generic_container->context.class_inst;
1101 else if (iresult->declaring->klass->generic_class)
1102 iresult->context.class_inst = iresult->declaring->klass->generic_class->context.class_inst;
1104 /* This can happen with some callers like mono_object_get_virtual_method () */
1105 if (!iresult->declaring->klass->generic_container && !iresult->declaring->klass->generic_class)
1106 iresult->context.class_inst = NULL;
1108 MonoImageSet *set = mono_metadata_get_image_set_for_method (iresult);
1111 mono_image_set_lock (set);
1112 cached = g_hash_table_lookup (set->gmethod_cache, iresult);
1113 mono_image_set_unlock (set);
1117 return (MonoMethod*)cached;
1120 mono_stats.inflated_method_count++;
1122 inflated_methods_size += sizeof (MonoMethodInflated);
1124 sig = mono_method_signature (method);
1126 char *name = mono_type_get_full_name (method->klass);
1127 mono_error_set_bad_image (error, method->klass->image, "Could not resolve signature of method %s:%s", name, method->name);
1133 memcpy (&iresult->method.pinvoke, method, sizeof (MonoMethodPInvoke));
1135 memcpy (&iresult->method.method, method, sizeof (MonoMethod));
1138 result = (MonoMethod *) iresult;
1139 result->is_inflated = TRUE;
1140 result->is_generic = FALSE;
1141 result->sre_method = FALSE;
1142 result->signature = NULL;
1144 if (!context->method_inst) {
1145 /* Set the generic_container of the result to the generic_container of method */
1146 MonoGenericContainer *generic_container = mono_method_get_generic_container (method);
1148 if (generic_container) {
1149 result->is_generic = 1;
1150 mono_method_set_generic_container (result, generic_container);
1154 if (!klass_hint || !klass_hint->generic_class ||
1155 klass_hint->generic_class->container_class != method->klass ||
1156 klass_hint->generic_class->context.class_inst != context->class_inst)
1159 if (method->klass->generic_container)
1160 result->klass = klass_hint;
1162 if (!result->klass) {
1163 MonoType *inflated = inflate_generic_type (NULL, &method->klass->byval_arg, context, error);
1164 if (!mono_error_ok (error))
1167 result->klass = inflated ? mono_class_from_mono_type (inflated) : method->klass;
1169 mono_metadata_free_type (inflated);
1173 * FIXME: This should hold, but it doesn't:
1175 * if (result->is_inflated && mono_method_get_context (result)->method_inst &&
1176 * mono_method_get_context (result)->method_inst == mono_method_get_generic_container (((MonoMethodInflated*)result)->declaring)->context.method_inst) {
1177 * g_assert (result->is_generic);
1180 * Fixing this here causes other things to break, hence a very
1181 * ugly hack in mini-trampolines.c - see
1182 * is_generic_method_definition().
1186 mono_image_set_lock (set);
1187 cached = g_hash_table_lookup (set->gmethod_cache, iresult);
1189 g_hash_table_insert (set->gmethod_cache, iresult, iresult);
1190 iresult->owner = set;
1193 mono_image_set_unlock (set);
1195 return (MonoMethod*)cached;
1203 * mono_get_inflated_method:
1205 * Obsolete. We keep it around since it's mentioned in the public API.
1208 mono_get_inflated_method (MonoMethod *method)
1214 * mono_method_get_context_general:
1216 * @uninflated: handle uninflated methods?
1218 * Returns the generic context of a method or NULL if it doesn't have
1219 * one. For an inflated method that's the context stored in the
1220 * method. Otherwise it's in the method's generic container or in the
1221 * generic container of the method's class.
1224 mono_method_get_context_general (MonoMethod *method, gboolean uninflated)
1226 if (method->is_inflated) {
1227 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
1228 return &imethod->context;
1232 if (method->is_generic)
1233 return &(mono_method_get_generic_container (method)->context);
1234 if (method->klass->generic_container)
1235 return &method->klass->generic_container->context;
1240 * mono_method_get_context:
1243 * Returns the generic context for method if it's inflated, otherwise
1247 mono_method_get_context (MonoMethod *method)
1249 return mono_method_get_context_general (method, FALSE);
1253 * mono_method_get_generic_container:
1255 * Returns the generic container of METHOD, which should be a generic method definition.
1256 * Returns NULL if METHOD is not a generic method definition.
1257 * LOCKING: Acquires the loader lock.
1259 MonoGenericContainer*
1260 mono_method_get_generic_container (MonoMethod *method)
1262 MonoGenericContainer *container;
1264 if (!method->is_generic)
1267 container = mono_image_property_lookup (method->klass->image, method, MONO_METHOD_PROP_GENERIC_CONTAINER);
1268 g_assert (container);
1274 * mono_method_set_generic_container:
1276 * Sets the generic container of METHOD to CONTAINER.
1277 * LOCKING: Acquires the image lock.
1280 mono_method_set_generic_container (MonoMethod *method, MonoGenericContainer* container)
1282 g_assert (method->is_generic);
1284 mono_image_property_insert (method->klass->image, method, MONO_METHOD_PROP_GENERIC_CONTAINER, container);
1288 * mono_class_find_enum_basetype:
1289 * @class: The enum class
1291 * Determine the basetype of an enum by iterating through its fields. We do this
1292 * in a separate function since it is cheaper than calling mono_class_setup_fields.
1295 mono_class_find_enum_basetype (MonoClass *class, MonoError *error)
1297 MonoGenericContainer *container = NULL;
1298 MonoImage *m = class->image;
1299 const int top = class->field.count;
1302 g_assert (class->enumtype);
1304 mono_error_init (error);
1306 if (class->generic_container)
1307 container = class->generic_container;
1308 else if (class->generic_class) {
1309 MonoClass *gklass = class->generic_class->container_class;
1311 container = gklass->generic_container;
1312 g_assert (container);
1316 * Fetch all the field information.
1318 for (i = 0; i < top; i++){
1320 guint32 cols [MONO_FIELD_SIZE];
1321 int idx = class->field.first + i;
1324 /* class->field.first and idx points into the fieldptr table */
1325 mono_metadata_decode_table_row (m, MONO_TABLE_FIELD, idx, cols, MONO_FIELD_SIZE);
1327 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC) //no need to decode static fields
1330 if (!mono_verifier_verify_field_signature (class->image, cols [MONO_FIELD_SIGNATURE], NULL)) {
1331 mono_error_set_bad_image (error, class->image, "Invalid field signature %x", cols [MONO_FIELD_SIGNATURE]);
1335 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
1336 mono_metadata_decode_value (sig, &sig);
1337 /* FIELD signature == 0x06 */
1339 mono_error_set_bad_image (error, class->image, "Invalid field signature %x, expected 0x6 but got %x", cols [MONO_FIELD_SIGNATURE], *sig);
1343 ftype = mono_metadata_parse_type_full (m, container, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
1345 if (mono_loader_get_last_error ()) /*FIXME plug the above to not leak errors*/
1346 mono_error_set_from_loader_error (error);
1348 mono_error_set_bad_image (error, class->image, "Could not parse type for field signature %x", cols [MONO_FIELD_SIGNATURE]);
1351 if (class->generic_class) {
1352 //FIXME do we leak here?
1353 ftype = mono_class_inflate_generic_type_checked (ftype, mono_class_get_context (class), error);
1354 if (!mono_error_ok (error))
1356 ftype->attrs = cols [MONO_FIELD_FLAGS];
1361 mono_error_set_type_load_class (error, class, "Could not find base type");
1364 mono_loader_assert_no_error ();
1369 * Checks for MonoClass::exception_type without resolving all MonoType's into MonoClass'es
1372 mono_type_has_exceptions (MonoType *type)
1374 switch (type->type) {
1375 case MONO_TYPE_CLASS:
1376 case MONO_TYPE_VALUETYPE:
1377 case MONO_TYPE_SZARRAY:
1378 return type->data.klass->exception_type;
1379 case MONO_TYPE_ARRAY:
1380 return type->data.array->eklass->exception_type;
1381 case MONO_TYPE_GENERICINST:
1382 return mono_generic_class_get_class (type->data.generic_class)->exception_type;
1391 * Allocate memory for some data belonging to CLASS, either from its image's mempool,
1395 mono_class_alloc (MonoClass *class, int size)
1397 if (class->generic_class)
1398 return mono_image_set_alloc (class->generic_class->owner, size);
1400 return mono_image_alloc (class->image, size);
1404 mono_class_alloc0 (MonoClass *class, int size)
1408 res = mono_class_alloc (class, size);
1409 memset (res, 0, size);
1413 #define mono_class_new0(class,struct_type, n_structs) \
1414 ((struct_type *) mono_class_alloc0 ((class), ((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
1417 * mono_class_setup_basic_field_info:
1418 * @class: The class to initialize
1420 * Initializes the class->fields.
1421 * LOCKING: Assumes the loader lock is held.
1424 mono_class_setup_basic_field_info (MonoClass *class)
1426 MonoClassField *field;
1434 gtd = class->generic_class ? mono_class_get_generic_type_definition (class) : NULL;
1435 image = class->image;
1436 top = class->field.count;
1438 if (class->generic_class && image_is_dynamic (class->generic_class->container_class->image) && !class->generic_class->container_class->wastypebuilder) {
1440 * This happens when a generic instance of an unfinished generic typebuilder
1441 * is used as an element type for creating an array type. We can't initialize
1442 * the fields of this class using the fields of gklass, since gklass is not
1443 * finished yet, fields could be added to it later.
1449 mono_class_setup_basic_field_info (gtd);
1451 top = gtd->field.count;
1452 class->field.first = gtd->field.first;
1453 class->field.count = gtd->field.count;
1456 class->fields = mono_class_alloc0 (class, sizeof (MonoClassField) * top);
1459 * Fetch all the field information.
1461 for (i = 0; i < top; i++){
1462 field = &class->fields [i];
1463 field->parent = class;
1466 field->name = mono_field_get_name (>d->fields [i]);
1468 int idx = class->field.first + i;
1469 /* class->field.first and idx points into the fieldptr table */
1470 guint32 name_idx = mono_metadata_decode_table_row_col (image, MONO_TABLE_FIELD, idx, MONO_FIELD_NAME);
1471 /* The name is needed for fieldrefs */
1472 field->name = mono_metadata_string_heap (image, name_idx);
1478 * mono_class_setup_fields:
1479 * @class: The class to initialize
1481 * Initializes the class->fields.
1482 * LOCKING: Assumes the loader lock is held.
1485 mono_class_setup_fields (MonoClass *class)
1488 MonoImage *m = class->image;
1490 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
1491 int i, blittable = TRUE;
1492 guint32 real_size = 0;
1493 guint32 packing_size = 0;
1494 gboolean explicit_size;
1495 MonoClassField *field;
1496 MonoGenericContainer *container = NULL;
1497 MonoClass *gtd = class->generic_class ? mono_class_get_generic_type_definition (class) : NULL;
1500 * FIXME: We have a race condition here. It's possible that this function returns
1501 * to its caller with `instance_size` set to `0` instead of the actual size. This
1502 * is not a problem when the function is called recursively on the same class,
1503 * because the size will be initialized by the outer invocation. What follows is a
1504 * description of how it can occur in other cases, too. There it is a problem,
1505 * because it can lead to the GC being asked to allocate an object of size `0`,
1506 * which SGen chokes on. The race condition is triggered infrequently by
1507 * `tests/sgen-suspend.cs`.
1509 * This function is called for a class whenever one of its subclasses is inited.
1510 * For example, it's called for every subclass of Object. What it does is this:
1512 * if (class->setup_fields_called)
1515 * class->instance_size = 0;
1517 * class->setup_fields_called = 1;
1518 * ... critical point
1519 * class->instance_size = actual_instance_size;
1521 * The last two steps are sometimes reversed, but that only changes the way in which
1522 * the race condition works.
1524 * Assume thread A goes through this function and makes it to the critical point.
1525 * Now thread B runs the function and, since `setup_fields_called` is set, returns
1526 * immediately, but `instance_size` is incorrect.
1528 * The other case looks like this:
1530 * if (class->setup_fields_called)
1532 * ... critical point X
1533 * class->instance_size = 0;
1534 * ... critical point Y
1535 * class->instance_size = actual_instance_size;
1537 * class->setup_fields_called = 1;
1539 * Assume thread A goes through the function and makes it to critical point X. Now
1540 * thread B runs through the whole of the function, returning, assuming
1541 * `instance_size` is set. At that point thread A gets to run and makes it to
1542 * critical point Y, at which time `instance_size` is `0` again, invalidating thread
1545 if (class->setup_fields_called)
1548 if (class->generic_class && image_is_dynamic (class->generic_class->container_class->image) && !class->generic_class->container_class->wastypebuilder) {
1550 * This happens when a generic instance of an unfinished generic typebuilder
1551 * is used as an element type for creating an array type. We can't initialize
1552 * the fields of this class using the fields of gklass, since gklass is not
1553 * finished yet, fields could be added to it later.
1558 mono_class_setup_basic_field_info (class);
1559 top = class->field.count;
1562 mono_class_setup_fields (gtd);
1563 if (gtd->exception_type) {
1564 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
1569 class->instance_size = 0;
1571 class->sizes.class_size = 0;
1573 if (class->parent) {
1574 /* For generic instances, class->parent might not have been initialized */
1575 mono_class_init (class->parent);
1576 if (!class->parent->size_inited) {
1577 mono_class_setup_fields (class->parent);
1578 if (class->parent->exception_type) {
1579 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
1583 class->instance_size += class->parent->instance_size;
1584 class->min_align = class->parent->min_align;
1585 /* we use |= since it may have been set already */
1586 class->has_references |= class->parent->has_references;
1587 blittable = class->parent->blittable;
1589 class->instance_size = sizeof (MonoObject);
1590 class->min_align = 1;
1593 /* We can't really enable 16 bytes alignment until the GC supports it.
1594 The whole layout/instance size code must be reviewed because we do alignment calculation in terms of the
1595 boxed instance, which leads to unexplainable holes at the beginning of an object embedding a simd type.
1596 Bug #506144 is an example of this issue.
1598 if (class->simd_type)
1599 class->min_align = 16;
1601 /* Get the real size */
1602 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
1604 if (explicit_size) {
1605 if ((packing_size & 0xffffff00) != 0) {
1606 char *err_msg = g_strdup_printf ("Could not load struct '%s' with packing size %d >= 256", class->name, packing_size);
1607 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, err_msg);
1610 class->packing_size = packing_size;
1611 real_size += class->instance_size;
1615 if (explicit_size && real_size) {
1616 class->instance_size = MAX (real_size, class->instance_size);
1618 class->blittable = blittable;
1619 mono_memory_barrier ();
1620 class->size_inited = 1;
1621 class->fields_inited = 1;
1622 class->setup_fields_called = 1;
1626 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT && !(mono_is_corlib_image (class->image) && !strcmp (class->name_space, "System") && !strcmp (class->name, "ValueType")))
1629 /* Prevent infinite loops if the class references itself */
1630 class->setup_fields_called = 1;
1632 if (class->generic_container) {
1633 container = class->generic_container;
1635 container = gtd->generic_container;
1636 g_assert (container);
1640 * Fetch all the field information.
1642 for (i = 0; i < top; i++){
1643 int idx = class->field.first + i;
1644 field = &class->fields [i];
1646 field->parent = class;
1649 mono_field_resolve_type (field, &error);
1650 if (!mono_error_ok (&error)) {
1651 /*mono_field_resolve_type already failed class*/
1652 mono_error_cleanup (&error);
1656 g_error ("could not resolve %s:%s\n", mono_type_get_full_name(class), field->name);
1657 g_assert (field->type);
1660 if (mono_field_is_deleted (field))
1663 MonoClassField *gfield = >d->fields [i];
1664 field->offset = gfield->offset;
1666 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
1668 mono_metadata_field_info (m, idx, &offset, NULL, NULL);
1669 field->offset = offset;
1671 if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
1672 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup_printf ("Missing field layout info for %s", field->name));
1675 if (field->offset < -1) { /*-1 is used to encode special static fields */
1676 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup_printf ("Invalid negative field offset %d for %s", field->offset, field->name));
1679 if (class->generic_container) {
1680 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup_printf ("Generic class cannot have explicit layout."));
1686 /* Only do these checks if we still think this type is blittable */
1687 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
1688 if (field->type->byref || MONO_TYPE_IS_REFERENCE (field->type)) {
1691 MonoClass *field_class = mono_class_from_mono_type (field->type);
1693 mono_class_setup_fields (field_class);
1694 if (field_class->exception_type) {
1695 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
1699 if (!field_class || !field_class->blittable)
1704 if (class->enumtype && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
1705 class->cast_class = class->element_class = mono_class_from_mono_type (field->type);
1706 blittable = class->element_class->blittable;
1709 if (mono_type_has_exceptions (field->type)) {
1710 char *class_name = mono_type_get_full_name (class);
1711 char *type_name = mono_type_full_name (field->type);
1713 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
1714 g_warning ("Invalid type %s for instance field %s:%s", type_name, class_name, field->name);
1715 g_free (class_name);
1719 /* The def_value of fields is compute lazily during vtable creation */
1722 if (class == mono_defaults.string_class)
1725 class->blittable = blittable;
1727 if (class->enumtype && !mono_class_enum_basetype (class)) {
1728 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
1731 if (explicit_size && real_size) {
1732 class->instance_size = MAX (real_size, class->instance_size);
1735 if (class->exception_type)
1737 mono_class_layout_fields (class);
1739 /*valuetypes can't be neither bigger than 1Mb or empty. */
1740 if (class->valuetype && (class->instance_size <= 0 || class->instance_size > (0x100000 + sizeof (MonoObject))))
1741 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
1743 mono_memory_barrier ();
1744 class->fields_inited = 1;
1748 * mono_class_setup_fields_locking:
1749 * @class: The class to initialize
1751 * Initializes the class->fields array of fields.
1752 * Aquires the loader lock.
1755 mono_class_setup_fields_locking (MonoClass *class)
1757 /* This can be checked without locks */
1758 if (class->fields_inited)
1760 mono_loader_lock ();
1761 mono_class_setup_fields (class);
1762 mono_loader_unlock ();
1766 * mono_class_has_references:
1768 * Returns whenever @klass->has_references is set, initializing it if needed.
1769 * Aquires the loader lock.
1772 mono_class_has_references (MonoClass *klass)
1774 if (klass->init_pending) {
1775 /* Be conservative */
1778 mono_class_init (klass);
1780 return klass->has_references;
1785 * mono_type_get_basic_type_from_generic:
1788 * Returns a closed type corresponding to the possibly open type
1792 mono_type_get_basic_type_from_generic (MonoType *type)
1794 /* When we do generic sharing we let type variables stand for reference/primitive types. */
1795 if (!type->byref && (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR) &&
1796 (!type->data.generic_param->gshared_constraint || type->data.generic_param->gshared_constraint->type == MONO_TYPE_OBJECT))
1797 return &mono_defaults.object_class->byval_arg;
1802 type_has_references (MonoClass *klass, MonoType *ftype)
1804 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (klass, ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype)))))
1806 if (!ftype->byref && (ftype->type == MONO_TYPE_VAR || ftype->type == MONO_TYPE_MVAR)) {
1807 MonoGenericParam *gparam = ftype->data.generic_param;
1809 if (gparam->gshared_constraint)
1810 return mono_class_has_references (mono_class_from_mono_type (gparam->gshared_constraint));
1816 * mono_class_layout_fields:
1819 * Compute the placement of fields inside an object or struct, according to
1820 * the layout rules and set the following fields in @class:
1821 * - has_references (if the class contains instance references firled or structs that contain references)
1822 * - has_static_refs (same, but for static fields)
1823 * - instance_size (size of the object in memory)
1824 * - class_size (size needed for the static fields)
1825 * - size_inited (flag set when the instance_size is set)
1827 * LOCKING: this is supposed to be called with the loader lock held.
1830 mono_class_layout_fields (MonoClass *class)
1833 const int top = class->field.count;
1834 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
1835 guint32 pass, passes, real_size;
1836 gboolean gc_aware_layout = FALSE;
1837 gboolean has_static_fields = FALSE;
1838 MonoClassField *field;
1841 * When we do generic sharing we need to have layout
1842 * information for open generic classes (either with a generic
1843 * context containing type variables or with a generic
1844 * container), so we don't return in that case anymore.
1848 * Enable GC aware auto layout: in this mode, reference
1849 * fields are grouped together inside objects, increasing collector
1851 * Requires that all classes whose layout is known to native code be annotated
1852 * with [StructLayout (LayoutKind.Sequential)]
1853 * Value types have gc_aware_layout disabled by default, as per
1854 * what the default is for other runtimes.
1856 /* corlib is missing [StructLayout] directives in many places */
1857 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
1858 if (!class->valuetype)
1859 gc_aware_layout = TRUE;
1862 /* Compute klass->has_references */
1864 * Process non-static fields first, since static fields might recursively
1865 * refer to the class itself.
1867 for (i = 0; i < top; i++) {
1870 field = &class->fields [i];
1872 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
1873 ftype = mono_type_get_underlying_type (field->type);
1874 ftype = mono_type_get_basic_type_from_generic (ftype);
1875 if (type_has_references (class, ftype))
1876 class->has_references = TRUE;
1880 for (i = 0; i < top; i++) {
1883 field = &class->fields [i];
1885 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC) {
1886 ftype = mono_type_get_underlying_type (field->type);
1887 ftype = mono_type_get_basic_type_from_generic (ftype);
1888 if (type_has_references (class, ftype))
1889 class->has_static_refs = TRUE;
1893 for (i = 0; i < top; i++) {
1896 field = &class->fields [i];
1898 ftype = mono_type_get_underlying_type (field->type);
1899 ftype = mono_type_get_basic_type_from_generic (ftype);
1900 if (type_has_references (class, ftype)) {
1901 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1902 class->has_static_refs = TRUE;
1904 class->has_references = TRUE;
1909 * Compute field layout and total size (not considering static fields)
1913 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
1914 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
1916 if (gc_aware_layout)
1921 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
1924 if (class->parent) {
1925 mono_class_setup_fields (class->parent);
1926 if (class->parent->exception_type) {
1927 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
1930 real_size = class->parent->instance_size;
1932 real_size = sizeof (MonoObject);
1935 for (pass = 0; pass < passes; ++pass) {
1936 for (i = 0; i < top; i++){
1941 field = &class->fields [i];
1943 if (mono_field_is_deleted (field))
1945 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1948 ftype = mono_type_get_underlying_type (field->type);
1949 ftype = mono_type_get_basic_type_from_generic (ftype);
1950 if (gc_aware_layout) {
1951 if (type_has_references (class, ftype)) {
1960 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
1961 (strcmp (mono_field_get_name (field), "$PRIVATE$") == 0)) {
1962 /* This field is a hack inserted by MCS to empty structures */
1966 size = mono_type_size (field->type, &align);
1968 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
1969 align = class->packing_size ? MIN (class->packing_size, align): align;
1970 /* if the field has managed references, we need to force-align it
1973 if (type_has_references (class, ftype))
1974 align = MAX (align, sizeof (gpointer));
1976 class->min_align = MAX (align, class->min_align);
1977 field->offset = real_size;
1979 field->offset += align - 1;
1980 field->offset &= ~(align - 1);
1982 /*TypeBuilders produce all sort of weird things*/
1983 g_assert (image_is_dynamic (class->image) || field->offset > 0);
1984 real_size = field->offset + size;
1987 class->instance_size = MAX (real_size, class->instance_size);
1989 if (class->instance_size & (class->min_align - 1)) {
1990 class->instance_size += class->min_align - 1;
1991 class->instance_size &= ~(class->min_align - 1);
1995 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT: {
1999 for (i = 0; i < top; i++) {
2004 field = &class->fields [i];
2007 * There must be info about all the fields in a type if it
2008 * uses explicit layout.
2010 if (mono_field_is_deleted (field))
2012 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
2015 size = mono_type_size (field->type, &align);
2016 align = class->packing_size ? MIN (class->packing_size, align): align;
2017 class->min_align = MAX (align, class->min_align);
2020 * When we get here, field->offset is already set by the
2021 * loader (for either runtime fields or fields loaded from metadata).
2022 * The offset is from the start of the object: this works for both
2023 * classes and valuetypes.
2025 field->offset += sizeof (MonoObject);
2026 ftype = mono_type_get_underlying_type (field->type);
2027 ftype = mono_type_get_basic_type_from_generic (ftype);
2028 if (type_has_references (class, ftype)) {
2029 if (field->offset % sizeof (gpointer)) {
2030 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
2037 real_size = MAX (real_size, size + field->offset);
2040 if (class->has_references) {
2041 ref_bitmap = g_new0 (guint8, real_size / sizeof (gpointer));
2043 /* Check for overlapping reference and non-reference fields */
2044 for (i = 0; i < top; i++) {
2047 field = &class->fields [i];
2049 if (mono_field_is_deleted (field))
2051 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
2053 ftype = mono_type_get_underlying_type (field->type);
2054 if (MONO_TYPE_IS_REFERENCE (ftype))
2055 ref_bitmap [field->offset / sizeof (gpointer)] = 1;
2057 for (i = 0; i < top; i++) {
2058 field = &class->fields [i];
2060 if (mono_field_is_deleted (field))
2062 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
2065 // FIXME: Too much code does this
2067 if (!MONO_TYPE_IS_REFERENCE (field->type) && ref_bitmap [field->offset / sizeof (gpointer)]) {
2068 char *err_msg = g_strdup_printf ("Could not load type '%s' because it contains an object field at offset %d that is incorrectly aligned or overlapped by a non-object field.", class->name, field->offset);
2069 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, err_msg);
2073 g_free (ref_bitmap);
2076 class->instance_size = MAX (real_size, class->instance_size);
2077 if (class->instance_size & (class->min_align - 1)) {
2078 class->instance_size += class->min_align - 1;
2079 class->instance_size &= ~(class->min_align - 1);
2085 if (layout != TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
2087 * This leads to all kinds of problems with nested structs, so only
2088 * enable it when a MONO_DEBUG property is set.
2090 * For small structs, set min_align to at least the struct size to improve
2091 * performance, and since the JIT memset/memcpy code assumes this and generates
2092 * unaligned accesses otherwise. See #78990 for a testcase.
2094 if (mono_align_small_structs) {
2095 if (class->instance_size <= sizeof (MonoObject) + sizeof (gpointer))
2096 class->min_align = MAX (class->min_align, class->instance_size - sizeof (MonoObject));
2100 mono_memory_barrier ();
2101 class->size_inited = 1;
2104 * Compute static field layout and size
2106 for (i = 0; i < top; i++){
2110 field = &class->fields [i];
2112 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
2114 if (mono_field_is_deleted (field))
2117 if (mono_type_has_exceptions (field->type)) {
2118 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
2122 has_static_fields = TRUE;
2124 size = mono_type_size (field->type, &align);
2125 field->offset = class->sizes.class_size;
2126 /*align is always non-zero here*/
2127 field->offset += align - 1;
2128 field->offset &= ~(align - 1);
2129 class->sizes.class_size = field->offset + size;
2132 if (has_static_fields && class->sizes.class_size == 0)
2133 /* Simplify code which depends on class_size != 0 if the class has static fields */
2134 class->sizes.class_size = 8;
2138 create_array_method (MonoClass *class, const char *name, MonoMethodSignature *sig)
2142 method = (MonoMethod *) mono_image_alloc0 (class->image, sizeof (MonoMethodPInvoke));
2143 method->klass = class;
2144 method->flags = METHOD_ATTRIBUTE_PUBLIC;
2145 method->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
2146 method->signature = sig;
2147 method->name = name;
2150 if (name [0] == '.') {
2151 method->flags |= METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
2153 method->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
2159 * mono_class_setup_methods:
2162 * Initializes the 'methods' array in CLASS.
2163 * Calling this method should be avoided if possible since it allocates a lot
2164 * of long-living MonoMethod structures.
2165 * Methods belonging to an interface are assigned a sequential slot starting
2168 * On failure this function sets class->exception_type
2171 mono_class_setup_methods (MonoClass *class)
2174 MonoMethod **methods;
2179 if (class->generic_class) {
2181 MonoClass *gklass = class->generic_class->container_class;
2183 mono_class_init (gklass);
2184 if (!gklass->exception_type)
2185 mono_class_setup_methods (gklass);
2186 if (gklass->exception_type) {
2187 /* FIXME make exception_data less opaque so it's possible to dup it here */
2188 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Generic type definition failed to load"));
2192 /* The + 1 makes this always non-NULL to pass the check in mono_class_setup_methods () */
2193 count = gklass->method.count;
2194 methods = mono_class_alloc0 (class, sizeof (MonoMethod*) * (count + 1));
2196 for (i = 0; i < count; i++) {
2197 methods [i] = mono_class_inflate_generic_method_full_checked (
2198 gklass->methods [i], class, mono_class_get_context (class), &error);
2199 if (!mono_error_ok (&error)) {
2200 char *method = mono_method_full_name (gklass->methods [i], TRUE);
2201 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup_printf ("Could not inflate method %s due to %s", method, mono_error_get_message (&error)));
2204 mono_error_cleanup (&error);
2208 } else if (class->rank) {
2210 MonoMethod *amethod;
2211 MonoMethodSignature *sig;
2212 int count_generic = 0, first_generic = 0;
2214 gboolean jagged_ctor = FALSE;
2216 count = 3 + (class->rank > 1? 2: 1);
2218 mono_class_setup_interfaces (class, &error);
2219 g_assert (mono_error_ok (&error)); /*FIXME can this fail for array types?*/
2221 if (class->rank == 1 && class->element_class->rank) {
2223 class->method.count ++;
2226 if (class->interface_count) {
2227 count_generic = generic_array_methods (class);
2228 first_generic = count;
2229 count += class->interface_count * count_generic;
2232 methods = mono_class_alloc0 (class, sizeof (MonoMethod*) * count);
2234 sig = mono_metadata_signature_alloc (class->image, class->rank);
2235 sig->ret = &mono_defaults.void_class->byval_arg;
2236 sig->pinvoke = TRUE;
2237 sig->hasthis = TRUE;
2238 for (i = 0; i < class->rank; ++i)
2239 sig->params [i] = &mono_defaults.int32_class->byval_arg;
2241 amethod = create_array_method (class, ".ctor", sig);
2242 methods [method_num++] = amethod;
2243 if (class->rank > 1) {
2244 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
2245 sig->ret = &mono_defaults.void_class->byval_arg;
2246 sig->pinvoke = TRUE;
2247 sig->hasthis = TRUE;
2248 for (i = 0; i < class->rank * 2; ++i)
2249 sig->params [i] = &mono_defaults.int32_class->byval_arg;
2251 amethod = create_array_method (class, ".ctor", sig);
2252 methods [method_num++] = amethod;
2256 /* Jagged arrays have an extra ctor in .net which creates an array of arrays */
2257 sig = mono_metadata_signature_alloc (class->image, class->rank + 1);
2258 sig->ret = &mono_defaults.void_class->byval_arg;
2259 sig->pinvoke = TRUE;
2260 sig->hasthis = TRUE;
2261 for (i = 0; i < class->rank + 1; ++i)
2262 sig->params [i] = &mono_defaults.int32_class->byval_arg;
2263 amethod = create_array_method (class, ".ctor", sig);
2264 methods [method_num++] = amethod;
2267 /* element Get (idx11, [idx2, ...]) */
2268 sig = mono_metadata_signature_alloc (class->image, class->rank);
2269 sig->ret = &class->element_class->byval_arg;
2270 sig->pinvoke = TRUE;
2271 sig->hasthis = TRUE;
2272 for (i = 0; i < class->rank; ++i)
2273 sig->params [i] = &mono_defaults.int32_class->byval_arg;
2274 amethod = create_array_method (class, "Get", sig);
2275 methods [method_num++] = amethod;
2276 /* element& Address (idx11, [idx2, ...]) */
2277 sig = mono_metadata_signature_alloc (class->image, class->rank);
2278 sig->ret = &class->element_class->this_arg;
2279 sig->pinvoke = TRUE;
2280 sig->hasthis = TRUE;
2281 for (i = 0; i < class->rank; ++i)
2282 sig->params [i] = &mono_defaults.int32_class->byval_arg;
2283 amethod = create_array_method (class, "Address", sig);
2284 methods [method_num++] = amethod;
2285 /* void Set (idx11, [idx2, ...], element) */
2286 sig = mono_metadata_signature_alloc (class->image, class->rank + 1);
2287 sig->ret = &mono_defaults.void_class->byval_arg;
2288 sig->pinvoke = TRUE;
2289 sig->hasthis = TRUE;
2290 for (i = 0; i < class->rank; ++i)
2291 sig->params [i] = &mono_defaults.int32_class->byval_arg;
2292 sig->params [i] = &class->element_class->byval_arg;
2293 amethod = create_array_method (class, "Set", sig);
2294 methods [method_num++] = amethod;
2296 for (i = 0; i < class->interface_count; i++)
2297 setup_generic_array_ifaces (class, class->interfaces [i], methods, first_generic + i * count_generic);
2301 count = class->method.count;
2302 methods = mono_class_alloc (class, sizeof (MonoMethod*) * count);
2303 for (i = 0; i < count; ++i) {
2304 int idx = mono_metadata_translate_token_index (class->image, MONO_TABLE_METHOD, class->method.first + i + 1);
2305 methods [i] = mono_get_method_checked (class->image, MONO_TOKEN_METHOD_DEF | idx, class, NULL, &error);
2307 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup_printf ("Could not load method %d due to %s", i, mono_error_get_message (&error)));
2308 mono_error_cleanup (&error);
2313 if (MONO_CLASS_IS_INTERFACE (class)) {
2315 /*Only assign slots to virtual methods as interfaces are allowed to have static methods.*/
2316 for (i = 0; i < count; ++i) {
2317 if (methods [i]->flags & METHOD_ATTRIBUTE_VIRTUAL)
2318 methods [i]->slot = slot++;
2322 mono_image_lock (class->image);
2324 if (!class->methods) {
2325 class->method.count = count;
2327 /* Needed because of the double-checking locking pattern */
2328 mono_memory_barrier ();
2330 class->methods = methods;
2333 mono_image_unlock (class->image);
2337 * mono_class_get_method_by_index:
2339 * Returns class->methods [index], initializing class->methods if neccesary.
2341 * LOCKING: Acquires the loader lock.
2344 mono_class_get_method_by_index (MonoClass *class, int index)
2347 /* Avoid calling setup_methods () if possible */
2348 if (class->generic_class && !class->methods) {
2349 MonoClass *gklass = class->generic_class->container_class;
2352 m = mono_class_inflate_generic_method_full_checked (
2353 gklass->methods [index], class, mono_class_get_context (class), &error);
2354 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
2356 * If setup_methods () is called later for this class, no duplicates are created,
2357 * since inflate_generic_method guarantees that only one instance of a method
2358 * is created for each context.
2361 mono_class_setup_methods (class);
2362 g_assert (m == class->methods [index]);
2366 mono_class_setup_methods (class);
2367 if (class->exception_type) /*FIXME do proper error handling*/
2369 g_assert (index >= 0 && index < class->method.count);
2370 return class->methods [index];
2375 * mono_class_get_inflated_method:
2377 * Given an inflated class CLASS and a method METHOD which should be a method of
2378 * CLASS's generic definition, return the inflated method corresponding to METHOD.
2381 mono_class_get_inflated_method (MonoClass *class, MonoMethod *method)
2383 MonoClass *gklass = class->generic_class->container_class;
2386 g_assert (method->klass == gklass);
2388 mono_class_setup_methods (gklass);
2389 g_assert (!gklass->exception_type); /*FIXME do proper error handling*/
2391 for (i = 0; i < gklass->method.count; ++i) {
2392 if (gklass->methods [i] == method) {
2393 if (class->methods) {
2394 return class->methods [i];
2397 MonoMethod *result = mono_class_inflate_generic_method_full_checked (gklass->methods [i], class, mono_class_get_context (class), &error);
2398 g_assert (mono_error_ok (&error)); /* FIXME don't swallow this error */
2408 * mono_class_get_vtable_entry:
2410 * Returns class->vtable [offset], computing it if neccesary. Returns NULL on failure.
2411 * LOCKING: Acquires the loader lock.
2414 mono_class_get_vtable_entry (MonoClass *class, int offset)
2418 if (class->rank == 1) {
2420 * szarrays do not overwrite any methods of Array, so we can avoid
2421 * initializing their vtables in some cases.
2423 mono_class_setup_vtable (class->parent);
2424 if (offset < class->parent->vtable_size)
2425 return class->parent->vtable [offset];
2428 if (class->generic_class) {
2430 MonoClass *gklass = class->generic_class->container_class;
2431 mono_class_setup_vtable (gklass);
2432 m = gklass->vtable [offset];
2434 m = mono_class_inflate_generic_method_full_checked (m, class, mono_class_get_context (class), &error);
2435 g_assert (mono_error_ok (&error)); /* FIXME don't swallow this error */
2437 mono_class_setup_vtable (class);
2438 if (class->exception_type)
2440 m = class->vtable [offset];
2447 * mono_class_get_vtable_size:
2449 * Return the vtable size for KLASS.
2452 mono_class_get_vtable_size (MonoClass *klass)
2454 mono_class_setup_vtable (klass);
2456 return klass->vtable_size;
2460 * mono_class_setup_properties:
2462 * Initialize class->ext.property and class->ext.properties.
2464 * This method can fail the class.
2467 mono_class_setup_properties (MonoClass *class)
2469 guint startm, endm, i, j;
2470 guint32 cols [MONO_PROPERTY_SIZE];
2471 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
2472 MonoProperty *properties;
2476 if (class->ext && class->ext->properties)
2479 if (class->generic_class) {
2480 MonoClass *gklass = class->generic_class->container_class;
2482 mono_class_init (gklass);
2483 mono_class_setup_properties (gklass);
2484 if (gklass->exception_type) {
2485 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Generic type definition failed to load"));
2489 properties = mono_class_new0 (class, MonoProperty, gklass->ext->property.count + 1);
2491 for (i = 0; i < gklass->ext->property.count; i++) {
2493 MonoProperty *prop = &properties [i];
2495 *prop = gklass->ext->properties [i];
2498 prop->get = mono_class_inflate_generic_method_full_checked (
2499 prop->get, class, mono_class_get_context (class), &error);
2501 prop->set = mono_class_inflate_generic_method_full_checked (
2502 prop->set, class, mono_class_get_context (class), &error);
2504 g_assert (mono_error_ok (&error)); /*FIXME proper error handling*/
2505 prop->parent = class;
2508 first = gklass->ext->property.first;
2509 count = gklass->ext->property.count;
2511 first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &last);
2512 count = last - first;
2515 mono_class_setup_methods (class);
2516 if (class->exception_type)
2520 properties = mono_class_alloc0 (class, sizeof (MonoProperty) * count);
2521 for (i = first; i < last; ++i) {
2522 mono_metadata_decode_table_row (class->image, MONO_TABLE_PROPERTY, i, cols, MONO_PROPERTY_SIZE);
2523 properties [i - first].parent = class;
2524 properties [i - first].attrs = cols [MONO_PROPERTY_FLAGS];
2525 properties [i - first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
2527 startm = mono_metadata_methods_from_property (class->image, i, &endm);
2528 for (j = startm; j < endm; ++j) {
2531 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
2533 if (class->image->uncompressed_metadata) {
2535 /* It seems like the MONO_METHOD_SEMA_METHOD column needs no remapping */
2536 method = mono_get_method_checked (class->image, MONO_TOKEN_METHOD_DEF | cols [MONO_METHOD_SEMA_METHOD], class, NULL, &error);
2537 mono_error_cleanup (&error); /* FIXME don't swallow this error */
2539 method = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
2542 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
2543 case METHOD_SEMANTIC_SETTER:
2544 properties [i - first].set = method;
2546 case METHOD_SEMANTIC_GETTER:
2547 properties [i - first].get = method;
2556 mono_class_alloc_ext (class);
2558 mono_image_lock (class->image);
2560 if (class->ext->properties) {
2561 /* We leak 'properties' which was allocated from the image mempool */
2562 mono_image_unlock (class->image);
2566 class->ext->property.first = first;
2567 class->ext->property.count = count;
2569 /* Flush any pending writes as we do double checked locking on class->ext->properties */
2570 mono_memory_barrier ();
2572 /* Leave this assignment as the last op in the function */
2573 class->ext->properties = properties;
2575 mono_image_unlock (class->image);
2579 inflate_method_listz (MonoMethod **methods, MonoClass *class, MonoGenericContext *context)
2581 MonoMethod **om, **retval;
2584 for (om = methods, count = 0; *om; ++om, ++count)
2587 retval = g_new0 (MonoMethod*, count + 1);
2589 for (om = methods, count = 0; *om; ++om, ++count) {
2591 retval [count] = mono_class_inflate_generic_method_full_checked (*om, class, context, &error);
2592 g_assert (mono_error_ok (&error)); /*FIXME proper error handling*/
2598 /*This method can fail the class.*/
2600 mono_class_setup_events (MonoClass *class)
2603 guint startm, endm, i, j;
2604 guint32 cols [MONO_EVENT_SIZE];
2605 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
2609 if (class->ext && class->ext->events)
2612 if (class->generic_class) {
2613 MonoClass *gklass = class->generic_class->container_class;
2614 MonoGenericContext *context = NULL;
2616 mono_class_setup_events (gklass);
2617 if (gklass->exception_type) {
2618 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Generic type definition failed to load"));
2622 first = gklass->ext->event.first;
2623 count = gklass->ext->event.count;
2625 events = mono_class_new0 (class, MonoEvent, count);
2628 context = mono_class_get_context (class);
2630 for (i = 0; i < count; i++) {
2632 MonoEvent *event = &events [i];
2633 MonoEvent *gevent = &gklass->ext->events [i];
2635 mono_error_init (&error); //since we do conditional calls, we must ensure the default value is ok
2637 event->parent = class;
2638 event->name = gevent->name;
2639 event->add = gevent->add ? mono_class_inflate_generic_method_full_checked (gevent->add, class, context, &error) : NULL;
2640 g_assert (mono_error_ok (&error)); /*FIXME proper error handling*/
2641 event->remove = gevent->remove ? mono_class_inflate_generic_method_full_checked (gevent->remove, class, context, &error) : NULL;
2642 g_assert (mono_error_ok (&error)); /*FIXME proper error handling*/
2643 event->raise = gevent->raise ? mono_class_inflate_generic_method_full_checked (gevent->raise, class, context, &error) : NULL;
2644 g_assert (mono_error_ok (&error)); /*FIXME proper error handling*/
2646 #ifndef MONO_SMALL_CONFIG
2647 event->other = gevent->other ? inflate_method_listz (gevent->other, class, context) : NULL;
2649 event->attrs = gevent->attrs;
2652 first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &last);
2653 count = last - first;
2656 mono_class_setup_methods (class);
2657 if (class->exception_type) {
2658 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Generic type definition failed to load"));
2663 events = mono_class_alloc0 (class, sizeof (MonoEvent) * count);
2664 for (i = first; i < last; ++i) {
2665 MonoEvent *event = &events [i - first];
2667 mono_metadata_decode_table_row (class->image, MONO_TABLE_EVENT, i, cols, MONO_EVENT_SIZE);
2668 event->parent = class;
2669 event->attrs = cols [MONO_EVENT_FLAGS];
2670 event->name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
2672 startm = mono_metadata_methods_from_event (class->image, i, &endm);
2673 for (j = startm; j < endm; ++j) {
2676 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
2678 if (class->image->uncompressed_metadata) {
2680 /* It seems like the MONO_METHOD_SEMA_METHOD column needs no remapping */
2681 method = mono_get_method_checked (class->image, MONO_TOKEN_METHOD_DEF | cols [MONO_METHOD_SEMA_METHOD], class, NULL, &error);
2682 mono_error_cleanup (&error); /* FIXME don't swallow this error */
2684 method = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
2687 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
2688 case METHOD_SEMANTIC_ADD_ON:
2689 event->add = method;
2691 case METHOD_SEMANTIC_REMOVE_ON:
2692 event->remove = method;
2694 case METHOD_SEMANTIC_FIRE:
2695 event->raise = method;
2697 case METHOD_SEMANTIC_OTHER: {
2698 #ifndef MONO_SMALL_CONFIG
2701 if (event->other == NULL) {
2702 event->other = g_new0 (MonoMethod*, 2);
2704 while (event->other [n])
2706 event->other = g_realloc (event->other, (n + 2) * sizeof (MonoMethod*));
2708 event->other [n] = method;
2709 /* NULL terminated */
2710 event->other [n + 1] = NULL;
2721 mono_class_alloc_ext (class);
2723 mono_image_lock (class->image);
2725 if (class->ext->events) {
2726 mono_image_unlock (class->image);
2730 class->ext->event.first = first;
2731 class->ext->event.count = count;
2733 /* Flush any pending writes as we do double checked locking on class->ext.events */
2734 mono_memory_barrier ();
2736 /* Leave this assignment as the last op in the function */
2737 class->ext->events = events;
2739 mono_image_unlock (class->image);
2743 * Global pool of interface IDs, represented as a bitset.
2744 * LOCKING: Protected by the classes lock.
2746 static MonoBitSet *global_interface_bitset = NULL;
2749 * mono_unload_interface_ids:
2750 * @bitset: bit set of interface IDs
2752 * When an image is unloaded, the interface IDs associated with
2753 * the image are put back in the global pool of IDs so the numbers
2757 mono_unload_interface_ids (MonoBitSet *bitset)
2760 mono_bitset_sub (global_interface_bitset, bitset);
2765 mono_unload_interface_id (MonoClass *class)
2767 if (global_interface_bitset && class->interface_id) {
2769 mono_bitset_clear (global_interface_bitset, class->interface_id);
2775 * mono_get_unique_iid:
2778 * Assign a unique integer ID to the interface represented by @class.
2779 * The ID will positive and as small as possible.
2780 * LOCKING: Acquires the classes lock.
2781 * Returns: the new ID.
2784 mono_get_unique_iid (MonoClass *class)
2788 g_assert (MONO_CLASS_IS_INTERFACE (class));
2792 if (!global_interface_bitset) {
2793 global_interface_bitset = mono_bitset_new (128, 0);
2796 iid = mono_bitset_find_first_unset (global_interface_bitset, -1);
2798 int old_size = mono_bitset_size (global_interface_bitset);
2799 MonoBitSet *new_set = mono_bitset_clone (global_interface_bitset, old_size * 2);
2800 mono_bitset_free (global_interface_bitset);
2801 global_interface_bitset = new_set;
2804 mono_bitset_set (global_interface_bitset, iid);
2805 /* set the bit also in the per-image set */
2806 if (!class->generic_class) {
2807 if (class->image->interface_bitset) {
2808 if (iid >= mono_bitset_size (class->image->interface_bitset)) {
2809 MonoBitSet *new_set = mono_bitset_clone (class->image->interface_bitset, iid + 1);
2810 mono_bitset_free (class->image->interface_bitset);
2811 class->image->interface_bitset = new_set;
2814 class->image->interface_bitset = mono_bitset_new (iid + 1, 0);
2816 mono_bitset_set (class->image->interface_bitset, iid);
2821 #ifndef MONO_SMALL_CONFIG
2822 if (mono_print_vtable) {
2824 char *type_name = mono_type_full_name (&class->byval_arg);
2825 if (class->generic_class && !class->generic_class->context.class_inst->is_open) {
2826 generic_id = class->generic_class->context.class_inst->id;
2827 g_assert (generic_id != 0);
2831 printf ("Interface: assigned id %d to %s|%s|%d\n", iid, class->image->name, type_name, generic_id);
2836 g_assert (iid <= 65535);
2841 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res, MonoError *error)
2846 mono_class_setup_interfaces (klass, error);
2847 if (!mono_error_ok (error))
2850 for (i = 0; i < klass->interface_count; i++) {
2851 ic = klass->interfaces [i];
2854 *res = g_ptr_array_new ();
2855 g_ptr_array_add (*res, ic);
2856 mono_class_init (ic);
2857 if (ic->exception_type) {
2858 mono_error_set_type_load_class (error, ic, "Error Loading class");
2862 collect_implemented_interfaces_aux (ic, res, error);
2863 if (!mono_error_ok (error))
2869 mono_class_get_implemented_interfaces (MonoClass *klass, MonoError *error)
2871 GPtrArray *res = NULL;
2873 collect_implemented_interfaces_aux (klass, &res, error);
2874 if (!mono_error_ok (error)) {
2876 g_ptr_array_free (res, TRUE);
2883 compare_interface_ids (const void *p_key, const void *p_element) {
2884 const MonoClass *key = p_key;
2885 const MonoClass *element = *(MonoClass**) p_element;
2887 return (key->interface_id - element->interface_id);
2890 /*FIXME verify all callers if they should switch to mono_class_interface_offset_with_variance*/
2892 mono_class_interface_offset (MonoClass *klass, MonoClass *itf) {
2893 MonoClass **result = mono_binary_search (
2895 klass->interfaces_packed,
2896 klass->interface_offsets_count,
2897 sizeof (MonoClass *),
2898 compare_interface_ids);
2900 return klass->interface_offsets_packed [result - (klass->interfaces_packed)];
2907 * mono_class_interface_offset_with_variance:
2909 * Return the interface offset of @itf in @klass. Sets @non_exact_match to TRUE if the match required variance check
2910 * If @itf is an interface with generic variant arguments, try to find the compatible one.
2912 * Note that this function is responsible for resolving ambiguities. Right now we use whatever ordering interfaces_packed gives us.
2914 * FIXME figure out MS disambiguation rules and fix this function.
2917 mono_class_interface_offset_with_variance (MonoClass *klass, MonoClass *itf, gboolean *non_exact_match) {
2918 int i = mono_class_interface_offset (klass, itf);
2919 *non_exact_match = FALSE;
2923 if (!mono_class_has_variant_generic_params (itf))
2926 for (i = 0; i < klass->interface_offsets_count; i++) {
2927 if (mono_class_is_variant_compatible (itf, klass->interfaces_packed [i], FALSE)) {
2928 *non_exact_match = TRUE;
2929 return klass->interface_offsets_packed [i];
2937 print_implemented_interfaces (MonoClass *klass) {
2940 GPtrArray *ifaces = NULL;
2942 int ancestor_level = 0;
2944 name = mono_type_get_full_name (klass);
2945 printf ("Packed interface table for class %s has size %d\n", name, klass->interface_offsets_count);
2948 for (i = 0; i < klass->interface_offsets_count; i++)
2949 printf (" [%03d][UUID %03d][SLOT %03d][SIZE %03d] interface %s.%s\n", i,
2950 klass->interfaces_packed [i]->interface_id,
2951 klass->interface_offsets_packed [i],
2952 klass->interfaces_packed [i]->method.count,
2953 klass->interfaces_packed [i]->name_space,
2954 klass->interfaces_packed [i]->name );
2955 printf ("Interface flags: ");
2956 for (i = 0; i <= klass->max_interface_id; i++)
2957 if (MONO_CLASS_IMPLEMENTS_INTERFACE (klass, i))
2958 printf ("(%d,T)", i);
2960 printf ("(%d,F)", i);
2962 printf ("Dump interface flags:");
2963 #ifdef COMPRESSED_INTERFACE_BITMAP
2965 const uint8_t* p = klass->interface_bitmap;
2966 i = klass->max_interface_id;
2968 printf (" %d x 00 %02X", p [0], p [1]);
2974 for (i = 0; i < ((((klass->max_interface_id + 1) >> 3)) + (((klass->max_interface_id + 1) & 7)? 1 :0)); i++)
2975 printf (" %02X", klass->interface_bitmap [i]);
2978 while (klass != NULL) {
2979 printf ("[LEVEL %d] Implemented interfaces by class %s:\n", ancestor_level, klass->name);
2980 ifaces = mono_class_get_implemented_interfaces (klass, &error);
2981 if (!mono_error_ok (&error)) {
2982 printf (" Type failed due to %s\n", mono_error_get_message (&error));
2983 mono_error_cleanup (&error);
2984 } else if (ifaces) {
2985 for (i = 0; i < ifaces->len; i++) {
2986 MonoClass *ic = g_ptr_array_index (ifaces, i);
2987 printf (" [UIID %d] interface %s\n", ic->interface_id, ic->name);
2988 printf (" [%03d][UUID %03d][SLOT %03d][SIZE %03d] interface %s.%s\n", i,
2990 mono_class_interface_offset (klass, ic),
2995 g_ptr_array_free (ifaces, TRUE);
2998 klass = klass->parent;
3003 inflate_class_one_arg (MonoClass *gtype, MonoClass *arg0)
3006 args [0] = &arg0->byval_arg;
3008 return mono_class_bind_generic_parameters (gtype, 1, args, FALSE);
3012 array_class_get_if_rank (MonoClass *class, guint rank)
3014 return rank ? mono_array_class_get (class, rank) : class;
3018 fill_valuetype_array_derived_types (MonoClass **valuetype_types, MonoClass *eclass, int rank)
3020 valuetype_types [0] = eclass;
3021 if (eclass == mono_defaults.int16_class)
3022 valuetype_types [1] = mono_defaults.uint16_class;
3023 else if (eclass == mono_defaults.uint16_class)
3024 valuetype_types [1] = mono_defaults.int16_class;
3025 else if (eclass == mono_defaults.int32_class)
3026 valuetype_types [1] = mono_defaults.uint32_class;
3027 else if (eclass == mono_defaults.uint32_class)
3028 valuetype_types [1] = mono_defaults.int32_class;
3029 else if (eclass == mono_defaults.int64_class)
3030 valuetype_types [1] = mono_defaults.uint64_class;
3031 else if (eclass == mono_defaults.uint64_class)
3032 valuetype_types [1] = mono_defaults.int64_class;
3033 else if (eclass == mono_defaults.byte_class)
3034 valuetype_types [1] = mono_defaults.sbyte_class;
3035 else if (eclass == mono_defaults.sbyte_class)
3036 valuetype_types [1] = mono_defaults.byte_class;
3037 else if (eclass->enumtype && mono_class_enum_basetype (eclass))
3038 valuetype_types [1] = mono_class_from_mono_type (mono_class_enum_basetype (eclass));
3041 /* this won't be needed once bug #325495 is completely fixed
3042 * though we'll need something similar to know which interfaces to allow
3043 * in arrays when they'll be lazyly created
3045 * FIXME: System.Array/InternalEnumerator don't need all this interface fabrication machinery.
3046 * MS returns diferrent types based on which instance is called. For example:
3047 * object obj = new byte[10][];
3048 * Type a = ((IEnumerable<byte[]>)obj).GetEnumerator ().GetType ();
3049 * Type b = ((IEnumerable<IList<byte>>)obj).GetEnumerator ().GetType ();
3052 * Fixing this should kill quite some code, save some bits and improve compatibility.
3055 get_implicit_generic_array_interfaces (MonoClass *class, int *num, int *is_enumerator)
3057 MonoClass *eclass = class->element_class;
3058 static MonoClass* generic_icollection_class = NULL;
3059 static MonoClass* generic_ienumerable_class = NULL;
3060 static MonoClass* generic_ienumerator_class = NULL;
3061 static MonoClass* generic_ireadonlylist_class = NULL;
3062 static MonoClass* generic_ireadonlycollection_class = NULL;
3063 MonoClass *valuetype_types[2] = { NULL, NULL };
3064 MonoClass **interfaces = NULL;
3065 int i, nifaces, interface_count, real_count, original_rank;
3067 gboolean internal_enumerator;
3068 gboolean eclass_is_valuetype;
3070 if (!mono_defaults.generic_ilist_class) {
3074 internal_enumerator = FALSE;
3075 eclass_is_valuetype = FALSE;
3076 original_rank = eclass->rank;
3077 if (class->byval_arg.type != MONO_TYPE_SZARRAY) {
3078 if (class->generic_class && class->nested_in == mono_defaults.array_class && strcmp (class->name, "InternalEnumerator`1") == 0) {
3080 * For a Enumerator<T[]> we need to get the list of interfaces for T.
3082 eclass = mono_class_from_mono_type (class->generic_class->context.class_inst->type_argv [0]);
3083 original_rank = eclass->rank;
3085 eclass = eclass->element_class;
3086 internal_enumerator = TRUE;
3087 *is_enumerator = TRUE;
3095 * with this non-lazy impl we can't implement all the interfaces so we do just the minimal stuff
3096 * for deep levels of arrays of arrays (string[][] has all the interfaces, string[][][] doesn't)
3098 all_interfaces = eclass->rank && eclass->element_class->rank? FALSE: TRUE;
3100 if (!generic_icollection_class) {
3101 generic_icollection_class = mono_class_from_name (mono_defaults.corlib,
3102 "System.Collections.Generic", "ICollection`1");
3103 generic_ienumerable_class = mono_class_from_name (mono_defaults.corlib,
3104 "System.Collections.Generic", "IEnumerable`1");
3105 generic_ienumerator_class = mono_class_from_name (mono_defaults.corlib,
3106 "System.Collections.Generic", "IEnumerator`1");
3107 generic_ireadonlylist_class = mono_class_from_name (mono_defaults.corlib,
3108 "System.Collections.Generic", "IReadOnlyList`1");
3109 generic_ireadonlycollection_class = mono_class_from_name (mono_defaults.corlib,
3110 "System.Collections.Generic", "IReadOnlyCollection`1");
3113 mono_class_init (eclass);
3116 * Arrays in 2.0 need to implement a number of generic interfaces
3117 * (IList`1, ICollection`1, IEnumerable`1 for a number of types depending
3118 * on the element class). For net 4.5, we also need to implement IReadOnlyList`1/IReadOnlyCollection`1.
3119 * We collect the types needed to build the
3120 * instantiations in interfaces at intervals of 3/5, because 3/5 are
3121 * the generic interfaces needed to implement.
3123 * On 4.5, as an optimization, we don't expand ref classes for the variant generic interfaces
3124 * (IEnumerator, IReadOnlyList and IReadOnlyColleciton). The regular dispatch code can handle those cases.
3126 if (eclass->valuetype) {
3127 nifaces = generic_ireadonlylist_class ? 5 : 3;
3128 fill_valuetype_array_derived_types (valuetype_types, eclass, original_rank);
3130 /* IList, ICollection, IEnumerable, IReadOnlyList`1 */
3131 real_count = interface_count = valuetype_types [1] ? (nifaces * 2) : nifaces;
3132 if (internal_enumerator) {
3134 if (valuetype_types [1])
3138 interfaces = g_malloc0 (sizeof (MonoClass*) * real_count);
3139 interfaces [0] = valuetype_types [0];
3140 if (valuetype_types [1])
3141 interfaces [nifaces] = valuetype_types [1];
3143 eclass_is_valuetype = TRUE;
3146 int idepth = eclass->idepth;
3147 if (!internal_enumerator)
3149 nifaces = generic_ireadonlylist_class ? 2 : 3;
3151 // FIXME: This doesn't seem to work/required for generic params
3152 if (!(eclass->this_arg.type == MONO_TYPE_VAR || eclass->this_arg.type == MONO_TYPE_MVAR || (image_is_dynamic (eclass->image) && !eclass->wastypebuilder)))
3153 mono_class_setup_interface_offsets (eclass);
3155 interface_count = all_interfaces? eclass->interface_offsets_count: eclass->interface_count;
3156 /* we add object for interfaces and the supertypes for the other
3157 * types. The last of the supertypes is the element class itself which we
3158 * already created the explicit interfaces for (so we include it for IEnumerator
3159 * and exclude it for arrays).
3161 if (MONO_CLASS_IS_INTERFACE (eclass))
3164 interface_count += idepth;
3165 if (eclass->rank && eclass->element_class->valuetype) {
3166 fill_valuetype_array_derived_types (valuetype_types, eclass->element_class, original_rank);
3167 if (valuetype_types [1])
3170 /* IList, ICollection, IEnumerable, IReadOnlyList */
3171 interface_count *= nifaces;
3172 real_count = interface_count;
3173 if (internal_enumerator) {
3174 real_count += (MONO_CLASS_IS_INTERFACE (eclass) ? 1 : idepth) + eclass->interface_offsets_count;
3175 if (valuetype_types [1])
3178 interfaces = g_malloc0 (sizeof (MonoClass*) * real_count);
3179 if (MONO_CLASS_IS_INTERFACE (eclass)) {
3180 interfaces [0] = mono_defaults.object_class;
3184 for (i = 0; i < idepth; i++) {
3185 mono_class_init (eclass->supertypes [i]);
3186 interfaces [j] = eclass->supertypes [i];
3190 if (all_interfaces) {
3191 for (i = 0; i < eclass->interface_offsets_count; i++) {
3192 interfaces [j] = eclass->interfaces_packed [i];
3196 for (i = 0; i < eclass->interface_count; i++) {
3197 interfaces [j] = eclass->interfaces [i];
3201 if (valuetype_types [1]) {
3202 interfaces [j] = array_class_get_if_rank (valuetype_types [1], original_rank);
3207 /* instantiate the generic interfaces */
3208 for (i = 0; i < interface_count; i += nifaces) {
3209 MonoClass *iface = interfaces [i];
3211 interfaces [i + 0] = inflate_class_one_arg (mono_defaults.generic_ilist_class, iface);
3212 interfaces [i + 1] = inflate_class_one_arg (generic_icollection_class, iface);
3214 if (eclass->valuetype) {
3215 interfaces [i + 2] = inflate_class_one_arg (generic_ienumerable_class, iface);
3216 if (generic_ireadonlylist_class) {
3217 interfaces [i + 3] = inflate_class_one_arg (generic_ireadonlylist_class, iface);
3218 interfaces [i + 4] = inflate_class_one_arg (generic_ireadonlycollection_class, iface);
3221 if (!generic_ireadonlylist_class)
3222 interfaces [i + 2] = inflate_class_one_arg (generic_ienumerable_class, iface);
3225 if (internal_enumerator) {
3227 /* instantiate IEnumerator<iface> */
3228 for (i = 0; i < interface_count; i++) {
3229 interfaces [i] = inflate_class_one_arg (generic_ienumerator_class, interfaces [i]);
3231 j = interface_count;
3232 if (!eclass_is_valuetype) {
3233 if (MONO_CLASS_IS_INTERFACE (eclass)) {
3234 interfaces [j] = inflate_class_one_arg (generic_ienumerator_class, mono_defaults.object_class);
3237 for (i = 0; i < eclass->idepth; i++) {
3238 interfaces [j] = inflate_class_one_arg (generic_ienumerator_class, eclass->supertypes [i]);
3242 for (i = 0; i < eclass->interface_offsets_count; i++) {
3243 interfaces [j] = inflate_class_one_arg (generic_ienumerator_class, eclass->interfaces_packed [i]);
3247 interfaces [j++] = inflate_class_one_arg (generic_ienumerator_class, array_class_get_if_rank (valuetype_types [0], original_rank));
3249 if (valuetype_types [1])
3250 interfaces [j] = inflate_class_one_arg (generic_ienumerator_class, array_class_get_if_rank (valuetype_types [1], original_rank));
3254 char *type_name = mono_type_get_name_full (&class->byval_arg, 0);
3255 for (i = 0; i < real_count; ++i) {
3256 char *name = mono_type_get_name_full (&interfaces [i]->byval_arg, 0);
3257 g_print ("%s implements %s\n", type_name, name);
3268 find_array_interface (MonoClass *klass, const char *name)
3271 for (i = 0; i < klass->interface_count; ++i) {
3272 if (strcmp (klass->interfaces [i]->name, name) == 0)
3279 * Return the number of virtual methods.
3280 * Even for interfaces we can't simply return the number of methods as all CLR types are allowed to have static methods.
3281 * Return -1 on failure.
3282 * FIXME It would be nice if this information could be cached somewhere.
3285 count_virtual_methods (MonoClass *class)
3289 class = mono_class_get_generic_type_definition (class); /*We can find this information by looking at the GTD*/
3291 if (class->methods || !MONO_CLASS_HAS_STATIC_METADATA (class)) {
3292 mono_class_setup_methods (class);
3293 if (class->exception_type)
3296 for (i = 0; i < class->method.count; ++i) {
3297 flags = class->methods [i]->flags;
3298 if (flags & METHOD_ATTRIBUTE_VIRTUAL)
3302 for (i = 0; i < class->method.count; ++i) {
3303 flags = mono_metadata_decode_table_row_col (class->image, MONO_TABLE_METHOD, class->method.first + i, MONO_METHOD_FLAGS);
3305 if (flags & METHOD_ATTRIBUTE_VIRTUAL)
3313 find_interface (int num_ifaces, MonoClass **interfaces_full, MonoClass *ic)
3321 m = (l + num_ifaces) / 2;
3322 if (interfaces_full [m] == ic)
3324 if (l == num_ifaces)
3326 if (!interfaces_full [m] || interfaces_full [m]->interface_id > ic->interface_id) {
3335 find_interface_offset (int num_ifaces, MonoClass **interfaces_full, int *interface_offsets_full, MonoClass *ic)
3337 int i = find_interface (num_ifaces, interfaces_full, ic);
3339 return interface_offsets_full [i];
3344 set_interface_and_offset (int num_ifaces, MonoClass **interfaces_full, int *interface_offsets_full, MonoClass *ic, int offset, mono_bool force_set)
3346 int i = find_interface (num_ifaces, interfaces_full, ic);
3350 interface_offsets_full [i] = offset;
3353 for (i = 0; i < num_ifaces; ++i) {
3354 if (interfaces_full [i]) {
3356 if (interfaces_full [i]->interface_id < ic->interface_id)
3359 while (end < num_ifaces && interfaces_full [end]) end++;
3360 memmove (interfaces_full + i + 1, interfaces_full + i, sizeof (MonoClass*) * (end - i));
3361 memmove (interface_offsets_full + i + 1, interface_offsets_full + i, sizeof (int) * (end - i));
3363 interfaces_full [i] = ic;
3364 interface_offsets_full [i] = offset;
3370 #ifdef COMPRESSED_INTERFACE_BITMAP
3373 * Compressed interface bitmap design.
3375 * Interface bitmaps take a large amount of memory, because their size is
3376 * linear with the maximum interface id assigned in the process (each interface
3377 * is assigned a unique id as it is loaded). The number of interface classes
3378 * is high because of the many implicit interfaces implemented by arrays (we'll
3379 * need to lazy-load them in the future).
3380 * Most classes implement a very small number of interfaces, so the bitmap is
3381 * sparse. This bitmap needs to be checked by interface casts, so access to the
3382 * needed bit must be fast and doable with few jit instructions.
3384 * The current compression format is as follows:
3385 * *) it is a sequence of one or more two-byte elements
3386 * *) the first byte in the element is the count of empty bitmap bytes
3387 * at the current bitmap position
3388 * *) the second byte in the element is an actual bitmap byte at the current
3391 * As an example, the following compressed bitmap bytes:
3392 * 0x07 0x01 0x00 0x7
3393 * correspond to the following bitmap:
3394 * 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x01 0x07
3396 * Each two-byte element can represent up to 2048 bitmap bits, but as few as a single
3397 * bitmap byte for non-sparse sequences. In practice the interface bitmaps created
3398 * during a gmcs bootstrap are reduced to less tha 5% of the original size.
3402 * mono_compress_bitmap:
3403 * @dest: destination buffer
3404 * @bitmap: bitmap buffer
3405 * @size: size of @bitmap in bytes
3407 * This is a mono internal function.
3408 * The @bitmap data is compressed into a format that is small but
3409 * still searchable in few instructions by the JIT and runtime.
3410 * The compressed data is stored in the buffer pointed to by the
3411 * @dest array. Passing a #NULL value for @dest allows to just compute
3412 * the size of the buffer.
3413 * This compression algorithm assumes the bits set in the bitmap are
3414 * few and far between, like in interface bitmaps.
3415 * Returns: the size of the compressed bitmap in bytes.
3418 mono_compress_bitmap (uint8_t *dest, const uint8_t *bitmap, int size)
3422 const uint8_t *end = bitmap + size;
3423 while (bitmap < end) {
3424 if (*bitmap || numz == 255) {
3448 * mono_class_interface_match:
3449 * @bitmap: a compressed bitmap buffer
3450 * @id: the index to check in the bitmap
3452 * This is a mono internal function.
3453 * Checks if a bit is set in a compressed interface bitmap. @id must
3454 * be already checked for being smaller than the maximum id encoded in the
3457 * Returns: a non-zero value if bit @id is set in the bitmap @bitmap,
3461 mono_class_interface_match (const uint8_t *bitmap, int id)
3464 id -= bitmap [0] * 8;
3468 return bitmap [1] & (1 << id);
3477 * LOCKING: this is supposed to be called with the loader lock held.
3478 * Return -1 on failure and set exception_type
3481 setup_interface_offsets (MonoClass *class, int cur_slot, gboolean overwrite)
3485 int i, j, max_iid, num_ifaces;
3486 MonoClass **interfaces_full = NULL;
3487 int *interface_offsets_full = NULL;
3489 GPtrArray **ifaces_array = NULL;
3490 int interface_offsets_count;
3491 MonoClass **array_interfaces = NULL;
3492 int num_array_interfaces;
3493 int is_enumerator = FALSE;
3495 mono_class_setup_supertypes (class);
3497 * get the implicit generic interfaces for either the arrays or for System.Array/InternalEnumerator<T>
3498 * implicit interfaces have the property that they are assigned the same slot in the
3499 * vtables for compatible interfaces
3501 array_interfaces = get_implicit_generic_array_interfaces (class, &num_array_interfaces, &is_enumerator);
3503 /* compute maximum number of slots and maximum interface id */
3505 num_ifaces = num_array_interfaces; /* this can include duplicated ones */
3506 ifaces_array = g_new0 (GPtrArray *, class->idepth);
3507 for (j = 0; j < class->idepth; j++) {
3508 k = class->supertypes [j];
3509 num_ifaces += k->interface_count;
3510 for (i = 0; i < k->interface_count; i++) {
3511 ic = k->interfaces [i];
3514 mono_class_init (ic);
3516 if (max_iid < ic->interface_id)
3517 max_iid = ic->interface_id;
3519 ifaces = mono_class_get_implemented_interfaces (k, &error);
3520 if (!mono_error_ok (&error)) {
3521 char *name = mono_type_get_full_name (k);
3522 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup_printf ("Error getting the interfaces of %s due to %s", name, mono_error_get_message (&error)));
3524 mono_error_cleanup (&error);
3529 num_ifaces += ifaces->len;
3530 for (i = 0; i < ifaces->len; ++i) {
3531 ic = g_ptr_array_index (ifaces, i);
3532 if (max_iid < ic->interface_id)
3533 max_iid = ic->interface_id;
3535 ifaces_array [j] = ifaces;
3539 for (i = 0; i < num_array_interfaces; ++i) {
3540 ic = array_interfaces [i];
3541 mono_class_init (ic);
3542 if (max_iid < ic->interface_id)
3543 max_iid = ic->interface_id;
3546 if (MONO_CLASS_IS_INTERFACE (class)) {
3548 if (max_iid < class->interface_id)
3549 max_iid = class->interface_id;
3551 class->max_interface_id = max_iid;
3552 /* compute vtable offset for interfaces */
3553 interfaces_full = g_malloc0 (sizeof (MonoClass*) * num_ifaces);
3554 interface_offsets_full = g_malloc (sizeof (int) * num_ifaces);
3556 for (i = 0; i < num_ifaces; i++) {
3557 interface_offsets_full [i] = -1;
3560 /* skip the current class */
3561 for (j = 0; j < class->idepth - 1; j++) {
3562 k = class->supertypes [j];
3563 ifaces = ifaces_array [j];
3566 for (i = 0; i < ifaces->len; ++i) {
3568 ic = g_ptr_array_index (ifaces, i);
3570 /*Force the sharing of interface offsets between parent and subtypes.*/
3571 io = mono_class_interface_offset (k, ic);
3573 set_interface_and_offset (num_ifaces, interfaces_full, interface_offsets_full, ic, io, TRUE);
3578 g_assert (class == class->supertypes [class->idepth - 1]);
3579 ifaces = ifaces_array [class->idepth - 1];
3581 for (i = 0; i < ifaces->len; ++i) {
3583 ic = g_ptr_array_index (ifaces, i);
3584 if (set_interface_and_offset (num_ifaces, interfaces_full, interface_offsets_full, ic, cur_slot, FALSE))
3586 count = count_virtual_methods (ic);
3588 char *name = mono_type_get_full_name (ic);
3589 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup_printf ("Error calculating interface offset of %s", name));
3598 if (MONO_CLASS_IS_INTERFACE (class))
3599 set_interface_and_offset (num_ifaces, interfaces_full, interface_offsets_full, class, cur_slot, TRUE);
3601 if (num_array_interfaces) {
3602 if (is_enumerator) {
3603 int ienumerator_idx = find_array_interface (class, "IEnumerator`1");
3604 int ienumerator_offset = find_interface_offset (num_ifaces, interfaces_full, interface_offsets_full, class->interfaces [ienumerator_idx]);
3605 g_assert (ienumerator_offset >= 0);
3606 for (i = 0; i < num_array_interfaces; ++i) {
3607 ic = array_interfaces [i];
3608 if (strcmp (ic->name, "IEnumerator`1") == 0)
3609 set_interface_and_offset (num_ifaces, interfaces_full, interface_offsets_full, ic, ienumerator_offset, TRUE);
3611 g_assert_not_reached ();
3612 /*g_print ("type %s has %s offset at %d (%s)\n", class->name, ic->name, interface_offsets_full [ic->interface_id], class->interfaces [0]->name);*/
3615 int ilist_offset, icollection_offset, ienumerable_offset, ireadonlylist_offset, ireadonlycollection_offset;
3616 int ilist_iface_idx = find_array_interface (class, "IList`1");
3617 MonoClass* ilist_class = class->interfaces [ilist_iface_idx];
3618 int ireadonlylist_iface_idx = find_array_interface (class, "IReadOnlyList`1");
3619 MonoClass* ireadonlylist_class = ireadonlylist_iface_idx != -1 ? class->interfaces [ireadonlylist_iface_idx] : NULL;
3620 int icollection_iface_idx = find_array_interface (ilist_class, "ICollection`1");
3621 int ienumerable_iface_idx = find_array_interface (ilist_class, "IEnumerable`1");
3622 int ireadonlycollection_iface_idx = ireadonlylist_iface_idx != -1 ? find_array_interface (ireadonlylist_class, "IReadOnlyCollection`1") : -1;
3623 ilist_offset = find_interface_offset (num_ifaces, interfaces_full, interface_offsets_full, class->interfaces [ilist_iface_idx]);
3624 icollection_offset = find_interface_offset (num_ifaces, interfaces_full, interface_offsets_full, ilist_class->interfaces [icollection_iface_idx]);
3625 ienumerable_offset = find_interface_offset (num_ifaces, interfaces_full, interface_offsets_full, ilist_class->interfaces [ienumerable_iface_idx]);
3626 ireadonlylist_offset = ireadonlylist_iface_idx != -1 ? find_interface_offset (num_ifaces, interfaces_full, interface_offsets_full, class->interfaces [ireadonlylist_iface_idx]) : -1;
3627 ireadonlycollection_offset = ireadonlycollection_iface_idx != -1 ? find_interface_offset (num_ifaces, interfaces_full, interface_offsets_full, ireadonlylist_class->interfaces [ireadonlycollection_iface_idx]) : -1;
3628 g_assert (ilist_offset >= 0 && icollection_offset >= 0 && ienumerable_offset >= 0);
3629 for (i = 0; i < num_array_interfaces; ++i) {
3631 ic = array_interfaces [i];
3632 if (ic->generic_class->container_class == mono_defaults.generic_ilist_class)
3633 offset = ilist_offset;
3634 else if (strcmp (ic->name, "ICollection`1") == 0)
3635 offset = icollection_offset;
3636 else if (strcmp (ic->name, "IEnumerable`1") == 0)
3637 offset = ienumerable_offset;
3638 else if (strcmp (ic->name, "IReadOnlyList`1") == 0)
3639 offset = ireadonlylist_offset;
3640 else if (strcmp (ic->name, "IReadOnlyCollection`1") == 0)
3641 offset = ireadonlycollection_offset;
3643 g_assert_not_reached ();
3644 set_interface_and_offset (num_ifaces, interfaces_full, interface_offsets_full, ic, offset, TRUE);
3645 /*g_print ("type %s has %s offset at %d (%s)\n", class->name, ic->name, offset, class->interfaces [0]->name);*/
3650 for (interface_offsets_count = 0, i = 0; i < num_ifaces; i++) {
3651 if (interface_offsets_full [i] != -1) {
3652 interface_offsets_count ++;
3657 * We might get called multiple times:
3658 * - mono_class_init ()
3659 * - mono_class_setup_vtable ().
3660 * - mono_class_setup_interface_offsets ().
3661 * mono_class_setup_interface_offsets () passes 0 as CUR_SLOT, so the computed interface offsets will be invalid. This
3662 * means we have to overwrite those when called from other places (#4440).
3664 if (class->interfaces_packed) {
3666 g_assert (class->interface_offsets_count == interface_offsets_count);
3670 class->interface_offsets_count = interface_offsets_count;
3671 class->interfaces_packed = mono_class_alloc (class, sizeof (MonoClass*) * interface_offsets_count);
3672 class->interface_offsets_packed = mono_class_alloc (class, sizeof (guint16) * interface_offsets_count);
3673 bsize = (sizeof (guint8) * ((max_iid + 1) >> 3)) + (((max_iid + 1) & 7)? 1 :0);
3674 #ifdef COMPRESSED_INTERFACE_BITMAP
3675 bitmap = g_malloc0 (bsize);
3677 bitmap = mono_class_alloc0 (class, bsize);
3679 for (i = 0; i < interface_offsets_count; i++) {
3680 int id = interfaces_full [i]->interface_id;
3681 bitmap [id >> 3] |= (1 << (id & 7));
3682 class->interfaces_packed [i] = interfaces_full [i];
3683 class->interface_offsets_packed [i] = interface_offsets_full [i];
3684 /*if (num_array_interfaces)
3685 g_print ("type %s has %s offset at %d\n", mono_type_get_name_full (&class->byval_arg, 0), mono_type_get_name_full (&interfaces_full [i]->byval_arg, 0), interface_offsets_full [i]);*/
3687 #ifdef COMPRESSED_INTERFACE_BITMAP
3688 i = mono_compress_bitmap (NULL, bitmap, bsize);
3689 class->interface_bitmap = mono_class_alloc0 (class, i);
3690 mono_compress_bitmap (class->interface_bitmap, bitmap, bsize);
3693 class->interface_bitmap = bitmap;
3698 g_free (interfaces_full);
3699 g_free (interface_offsets_full);
3700 g_free (array_interfaces);
3701 for (i = 0; i < class->idepth; i++) {
3702 ifaces = ifaces_array [i];
3704 g_ptr_array_free (ifaces, TRUE);
3706 g_free (ifaces_array);
3708 //printf ("JUST DONE: ");
3709 //print_implemented_interfaces (class);
3715 * Setup interface offsets for interfaces.
3717 * - class->max_interface_id
3718 * - class->interface_offsets_count
3719 * - class->interfaces_packed
3720 * - class->interface_offsets_packed
3721 * - class->interface_bitmap
3723 * This function can fail @class.
3726 mono_class_setup_interface_offsets (MonoClass *class)
3728 mono_loader_lock ();
3730 setup_interface_offsets (class, 0, FALSE);
3732 mono_loader_unlock ();
3735 /*Checks if @klass has @parent as one of it's parents type gtd
3739 * Bar<T> : Foo<Bar<Bar<T>>>
3743 mono_class_has_gtd_parent (MonoClass *klass, MonoClass *parent)
3745 klass = mono_class_get_generic_type_definition (klass);
3746 parent = mono_class_get_generic_type_definition (parent);
3747 mono_class_setup_supertypes (klass);
3748 mono_class_setup_supertypes (parent);
3750 return klass->idepth >= parent->idepth &&
3751 mono_class_get_generic_type_definition (klass->supertypes [parent->idepth - 1]) == parent;
3755 mono_class_check_vtable_constraints (MonoClass *class, GList *in_setup)
3757 MonoGenericInst *ginst;
3759 if (!class->generic_class) {
3760 mono_class_setup_vtable_full (class, in_setup);
3761 return class->exception_type == 0;
3764 mono_class_setup_vtable_full (mono_class_get_generic_type_definition (class), in_setup);
3765 if (class->generic_class->container_class->exception_type) {
3766 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Failed to load generic definition vtable"));
3770 ginst = class->generic_class->context.class_inst;
3771 for (i = 0; i < ginst->type_argc; ++i) {
3773 if (ginst->type_argv [i]->type != MONO_TYPE_GENERICINST)
3775 arg = mono_class_from_mono_type (ginst->type_argv [i]);
3776 /*Those 2 will be checked by mono_class_setup_vtable itself*/
3777 if (mono_class_has_gtd_parent (class, arg) || mono_class_has_gtd_parent (arg, class))
3779 if (!mono_class_check_vtable_constraints (arg, in_setup)) {
3780 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup_printf ("Failed to load generic parameter %d", i));
3788 * mono_class_setup_vtable:
3790 * Creates the generic vtable of CLASS.
3791 * Initializes the following fields in MonoClass:
3794 * Plus all the fields initialized by setup_interface_offsets ().
3795 * If there is an error during vtable construction, class->exception_type is set.
3797 * LOCKING: Acquires the loader lock.
3800 mono_class_setup_vtable (MonoClass *class)
3802 mono_class_setup_vtable_full (class, NULL);
3806 mono_class_setup_vtable_full (MonoClass *class, GList *in_setup)
3808 MonoMethod **overrides;
3809 MonoGenericContext *context;
3817 if (MONO_CLASS_IS_INTERFACE (class)) {
3818 /* This sets method->slot for all methods if this is an interface */
3819 mono_class_setup_methods (class);
3823 if (class->exception_type)
3826 if (g_list_find (in_setup, class))
3829 mono_loader_lock ();
3831 if (class->vtable) {
3832 mono_loader_unlock ();
3836 mono_stats.generic_vtable_count ++;
3837 in_setup = g_list_prepend (in_setup, class);
3839 if (class->generic_class) {
3840 if (!mono_class_check_vtable_constraints (class, in_setup)) {
3841 mono_loader_unlock ();
3842 g_list_remove (in_setup, class);
3846 context = mono_class_get_context (class);
3847 type_token = class->generic_class->container_class->type_token;
3849 context = (MonoGenericContext *) class->generic_container;
3850 type_token = class->type_token;
3853 if (image_is_dynamic (class->image)) {
3854 /* Generic instances can have zero method overrides without causing any harm.
3855 * This is true since we don't do layout all over again for them, we simply inflate
3856 * the layout of the parent.
3858 mono_reflection_get_dynamic_overrides (class, &overrides, &onum);
3860 /* The following call fails if there are missing methods in the type */
3861 /* FIXME it's probably a good idea to avoid this for generic instances. */
3862 ok = mono_class_get_overrides_full (class->image, type_token, &overrides, &onum, context);
3866 mono_class_setup_vtable_general (class, overrides, onum, in_setup);
3868 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Could not load list of method overrides"));
3872 mono_loader_unlock ();
3873 g_list_remove (in_setup, class);
3878 #define DEBUG_INTERFACE_VTABLE_CODE 0
3879 #define TRACE_INTERFACE_VTABLE_CODE 0
3880 #define VERIFY_INTERFACE_VTABLE_CODE 0
3881 #define VTABLE_SELECTOR (1)
3883 #if (TRACE_INTERFACE_VTABLE_CODE|DEBUG_INTERFACE_VTABLE_CODE)
3884 #define DEBUG_INTERFACE_VTABLE(stmt) do {\
3885 if (!(VTABLE_SELECTOR)) break; \
3889 #define DEBUG_INTERFACE_VTABLE(stmt)
3892 #if TRACE_INTERFACE_VTABLE_CODE
3893 #define TRACE_INTERFACE_VTABLE(stmt) do {\
3894 if (!(VTABLE_SELECTOR)) break; \
3898 #define TRACE_INTERFACE_VTABLE(stmt)
3901 #if VERIFY_INTERFACE_VTABLE_CODE
3902 #define VERIFY_INTERFACE_VTABLE(stmt) do {\
3903 if (!(VTABLE_SELECTOR)) break; \
3907 #define VERIFY_INTERFACE_VTABLE(stmt)
3911 #if (TRACE_INTERFACE_VTABLE_CODE|DEBUG_INTERFACE_VTABLE_CODE)
3913 mono_signature_get_full_desc (MonoMethodSignature *sig, gboolean include_namespace)
3917 GString *res = g_string_new ("");
3919 g_string_append_c (res, '(');
3920 for (i = 0; i < sig->param_count; ++i) {
3922 g_string_append_c (res, ',');
3923 mono_type_get_desc (res, sig->params [i], include_namespace);
3925 g_string_append (res, ")=>");
3926 if (sig->ret != NULL) {
3927 mono_type_get_desc (res, sig->ret, include_namespace);
3929 g_string_append (res, "NULL");
3932 g_string_free (res, FALSE);
3936 print_method_signatures (MonoMethod *im, MonoMethod *cm) {
3937 char *im_sig = mono_signature_get_full_desc (mono_method_signature (im), TRUE);
3938 char *cm_sig = mono_signature_get_full_desc (mono_method_signature (cm), TRUE);
3939 printf ("(IM \"%s\", CM \"%s\")", im_sig, cm_sig);
3947 is_wcf_hack_disabled (void)
3949 static gboolean disabled;
3950 static gboolean inited = FALSE;
3952 disabled = g_getenv ("MONO_DISABLE_WCF_HACK") != NULL;
3959 check_interface_method_override (MonoClass *class, MonoMethod *im, MonoMethod *cm, gboolean require_newslot, gboolean interface_is_explicitly_implemented_by_class, gboolean slot_is_empty)
3961 MonoMethodSignature *cmsig, *imsig;
3962 if (strcmp (im->name, cm->name) == 0) {
3963 if (! (cm->flags & METHOD_ATTRIBUTE_PUBLIC)) {
3964 TRACE_INTERFACE_VTABLE (printf ("[PUBLIC CHECK FAILED]"));
3967 if (! slot_is_empty) {
3968 if (require_newslot) {
3969 if (! interface_is_explicitly_implemented_by_class) {
3970 TRACE_INTERFACE_VTABLE (printf ("[NOT EXPLICIT IMPLEMENTATION IN FULL SLOT REFUSED]"));
3973 if (! (cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
3974 TRACE_INTERFACE_VTABLE (printf ("[NEWSLOT CHECK FAILED]"));
3978 TRACE_INTERFACE_VTABLE (printf ("[FULL SLOT REFUSED]"));
3981 cmsig = mono_method_signature (cm);
3982 imsig = mono_method_signature (im);
3983 if (!cmsig || !imsig) {
3984 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Could not resolve the signature of a virtual method"));
3988 if (! mono_metadata_signature_equal (cmsig, imsig)) {
3989 TRACE_INTERFACE_VTABLE (printf ("[SIGNATURE CHECK FAILED "));
3990 TRACE_INTERFACE_VTABLE (print_method_signatures (im, cm));
3991 TRACE_INTERFACE_VTABLE (printf ("]"));
3994 TRACE_INTERFACE_VTABLE (printf ("[SECURITY CHECKS]"));
3995 if (mono_security_core_clr_enabled ())
3996 mono_security_core_clr_check_override (class, cm, im);
3998 TRACE_INTERFACE_VTABLE (printf ("[NAME CHECK OK]"));
3999 if (is_wcf_hack_disabled () && !mono_method_can_access_method_full (cm, im, NULL)) {
4000 char *body_name = mono_method_full_name (cm, TRUE);
4001 char *decl_name = mono_method_full_name (im, TRUE);
4002 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup_printf ("Method %s overrides method '%s' which is not accessible", body_name, decl_name));
4010 MonoClass *ic = im->klass;
4011 const char *ic_name_space = ic->name_space;
4012 const char *ic_name = ic->name;
4015 if (! require_newslot) {
4016 TRACE_INTERFACE_VTABLE (printf ("[INJECTED METHOD REFUSED]"));
4019 if (cm->klass->rank == 0) {
4020 TRACE_INTERFACE_VTABLE (printf ("[RANK CHECK FAILED]"));
4023 cmsig = mono_method_signature (cm);
4024 imsig = mono_method_signature (im);
4025 if (!cmsig || !imsig) {
4026 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Could not resolve the signature of a virtual method"));
4030 if (! mono_metadata_signature_equal (cmsig, imsig)) {
4031 TRACE_INTERFACE_VTABLE (printf ("[(INJECTED) SIGNATURE CHECK FAILED "));
4032 TRACE_INTERFACE_VTABLE (print_method_signatures (im, cm));
4033 TRACE_INTERFACE_VTABLE (printf ("]"));
4036 if (mono_class_get_image (ic) != mono_defaults.corlib) {
4037 TRACE_INTERFACE_VTABLE (printf ("[INTERFACE CORLIB CHECK FAILED]"));
4040 if ((ic_name_space == NULL) || (strcmp (ic_name_space, "System.Collections.Generic") != 0)) {
4041 TRACE_INTERFACE_VTABLE (printf ("[INTERFACE NAMESPACE CHECK FAILED]"));
4044 if ((ic_name == NULL) || ((strcmp (ic_name, "IEnumerable`1") != 0) && (strcmp (ic_name, "ICollection`1") != 0) && (strcmp (ic_name, "IList`1") != 0) && (strcmp (ic_name, "IReadOnlyList`1") != 0) && (strcmp (ic_name, "IReadOnlyCollection`1") != 0))) {
4045 TRACE_INTERFACE_VTABLE (printf ("[INTERFACE NAME CHECK FAILED]"));
4049 subname = strstr (cm->name, ic_name_space);
4050 if (subname != cm->name) {
4051 TRACE_INTERFACE_VTABLE (printf ("[ACTUAL NAMESPACE CHECK FAILED]"));
4054 subname += strlen (ic_name_space);
4055 if (subname [0] != '.') {
4056 TRACE_INTERFACE_VTABLE (printf ("[FIRST DOT CHECK FAILED]"));
4060 if (strstr (subname, ic_name) != subname) {
4061 TRACE_INTERFACE_VTABLE (printf ("[ACTUAL CLASS NAME CHECK FAILED]"));
4064 subname += strlen (ic_name);
4065 if (subname [0] != '.') {
4066 TRACE_INTERFACE_VTABLE (printf ("[SECOND DOT CHECK FAILED]"));
4070 if (strcmp (subname, im->name) != 0) {
4071 TRACE_INTERFACE_VTABLE (printf ("[METHOD NAME CHECK FAILED]"));
4075 TRACE_INTERFACE_VTABLE (printf ("[SECURITY CHECKS (INJECTED CASE)]"));
4076 if (mono_security_core_clr_enabled ())
4077 mono_security_core_clr_check_override (class, cm, im);
4079 TRACE_INTERFACE_VTABLE (printf ("[INJECTED INTERFACE CHECK OK]"));
4080 if (is_wcf_hack_disabled () && !mono_method_can_access_method_full (cm, im, NULL)) {
4081 char *body_name = mono_method_full_name (cm, TRUE);
4082 char *decl_name = mono_method_full_name (im, TRUE);
4083 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup_printf ("Method %s overrides method '%s' which is not accessible", body_name, decl_name));
4093 #if (TRACE_INTERFACE_VTABLE_CODE|DEBUG_INTERFACE_VTABLE_CODE)
4095 foreach_override (gpointer key, gpointer value, gpointer user_data) {
4096 MonoMethod *method = key;
4097 MonoMethod *override = value;
4098 MonoClass *method_class = mono_method_get_class (method);
4099 MonoClass *override_class = mono_method_get_class (override);
4101 printf (" Method '%s.%s:%s' has override '%s.%s:%s'\n",
4102 mono_class_get_namespace (method_class), mono_class_get_name (method_class), mono_method_get_name (method),
4103 mono_class_get_namespace (override_class), mono_class_get_name (override_class), mono_method_get_name (override));
4106 print_overrides (GHashTable *override_map, const char *message) {
4108 printf ("Override map \"%s\" START:\n", message);
4109 g_hash_table_foreach (override_map, foreach_override, NULL);
4110 printf ("Override map \"%s\" END.\n", message);
4112 printf ("Override map \"%s\" EMPTY.\n", message);
4116 print_vtable_full (MonoClass *class, MonoMethod** vtable, int size, int first_non_interface_slot, const char *message, gboolean print_interfaces) {
4117 char *full_name = mono_type_full_name (&class->byval_arg);
4121 printf ("*** Vtable for class '%s' at \"%s\" (size %d)\n", full_name, message, size);
4123 if (print_interfaces) {
4124 print_implemented_interfaces (class);
4125 printf ("* Interfaces for class '%s' done.\nStarting vtable (size %d):\n", full_name, size);
4128 if (class->parent) {
4129 parent_size = class->parent->vtable_size;
4133 for (i = 0; i < size; ++i) {
4134 MonoMethod *cm = vtable [i];
4135 char *cm_name = cm ? mono_method_full_name (cm, TRUE) : g_strdup ("nil");
4136 char newness = (i < parent_size) ? 'O' : ((i < first_non_interface_slot) ? 'I' : 'N');
4138 printf (" [%c][%03d][INDEX %03d] %s [%p]\n", newness, i, cm ? cm->slot : - 1, cm_name, cm);
4146 #if VERIFY_INTERFACE_VTABLE_CODE
4148 mono_method_try_get_vtable_index (MonoMethod *method)
4150 if (method->is_inflated && (method->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
4151 MonoMethodInflated *imethod = (MonoMethodInflated*)method;
4152 if (imethod->declaring->is_generic)
4153 return imethod->declaring->slot;
4155 return method->slot;
4159 mono_class_verify_vtable (MonoClass *class)
4162 char *full_name = mono_type_full_name (&class->byval_arg);
4164 printf ("*** Verifying VTable of class '%s' \n", full_name);
4168 if (!class->methods)
4171 for (i = 0; i < class->method.count; ++i) {
4172 MonoMethod *cm = class->methods [i];
4175 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
4179 full_name = mono_method_full_name (cm, TRUE);
4181 slot = mono_method_try_get_vtable_index (cm);
4183 if (slot >= class->vtable_size) {
4184 printf ("\tInvalid method %s at index %d with vtable of length %d\n", full_name, slot, class->vtable_size);
4188 if (slot >= 0 && class->vtable [slot] != cm && (class->vtable [slot])) {
4189 char *other_name = class->vtable [slot] ? mono_method_full_name (class->vtable [slot], TRUE) : g_strdup ("[null value]");
4190 printf ("\tMethod %s has slot %d but vtable has %s on it\n", full_name, slot, other_name);
4191 g_free (other_name);
4194 printf ("\tVirtual method %s does n't have an assigned slot\n", full_name);
4201 print_unimplemented_interface_method_info (MonoClass *class, MonoClass *ic, MonoMethod *im, int im_slot, MonoMethod **overrides, int onum) {
4203 char *method_signature;
4206 for (index = 0; index < onum; ++index) {
4207 mono_trace_warning (MONO_TRACE_TYPE, " at slot %d: %s (%d) overrides %s (%d)\n", im_slot, overrides [index*2+1]->name,
4208 overrides [index*2+1]->slot, overrides [index*2]->name, overrides [index*2]->slot);
4210 method_signature = mono_signature_get_desc (mono_method_signature (im), FALSE);
4211 type_name = mono_type_full_name (&class->byval_arg);
4212 mono_trace_warning (MONO_TRACE_TYPE, "no implementation for interface method %s::%s(%s) in class %s\n",
4213 mono_type_get_name (&ic->byval_arg), im->name, method_signature, type_name);
4214 g_free (method_signature);
4216 mono_class_setup_methods (class);
4217 if (class->exception_type) {
4218 char *name = mono_type_get_full_name (class);
4219 mono_trace_warning (MONO_TRACE_TYPE, "CLASS %s failed to resolve methods\n", name);
4223 for (index = 0; index < class->method.count; ++index) {
4224 MonoMethod *cm = class->methods [index];
4225 method_signature = mono_signature_get_desc (mono_method_signature (cm), TRUE);
4227 mono_trace_warning (MONO_TRACE_TYPE, "METHOD %s(%s)\n", cm->name, method_signature);
4228 g_free (method_signature);
4233 mono_method_get_method_definition (MonoMethod *method)
4235 while (method->is_inflated)
4236 method = ((MonoMethodInflated*)method)->declaring;
4241 verify_class_overrides (MonoClass *class, MonoMethod **overrides, int onum)
4245 for (i = 0; i < onum; ++i) {
4246 MonoMethod *decl = overrides [i * 2];
4247 MonoMethod *body = overrides [i * 2 + 1];
4249 if (mono_class_get_generic_type_definition (body->klass) != mono_class_get_generic_type_definition (class)) {
4250 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Method belongs to a different class than the declared one"));
4254 if (!(body->flags & METHOD_ATTRIBUTE_VIRTUAL) || (body->flags & METHOD_ATTRIBUTE_STATIC)) {
4255 if (body->flags & METHOD_ATTRIBUTE_STATIC)
4256 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Method must not be static to override a base type"));
4258 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Method must be virtual to override a base type"));
4262 if (!(decl->flags & METHOD_ATTRIBUTE_VIRTUAL) || (decl->flags & METHOD_ATTRIBUTE_STATIC)) {
4263 if (body->flags & METHOD_ATTRIBUTE_STATIC)
4264 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Cannot override a static method in a base type"));
4266 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Cannot override a non virtual method in a base type"));
4270 if (!mono_class_is_assignable_from_slow (decl->klass, class)) {
4271 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Method overrides a class or interface that extended or implemented by this type"));
4275 body = mono_method_get_method_definition (body);
4276 decl = mono_method_get_method_definition (decl);
4278 if (is_wcf_hack_disabled () && !mono_method_can_access_method_full (body, decl, NULL)) {
4279 char *body_name = mono_method_full_name (body, TRUE);
4280 char *decl_name = mono_method_full_name (decl, TRUE);
4281 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup_printf ("Method %s overrides method '%s' which is not accessible", body_name, decl_name));
4291 mono_class_need_stelemref_method (MonoClass *class)
4293 return class->rank == 1 && MONO_TYPE_IS_REFERENCE (&class->element_class->byval_arg);
4297 * LOCKING: this is supposed to be called with the loader lock held.
4300 mono_class_setup_vtable_general (MonoClass *class, MonoMethod **overrides, int onum, GList *in_setup)
4304 MonoMethod **vtable;
4305 int i, max_vtsize = 0, max_iid, cur_slot = 0;
4306 GPtrArray *ifaces = NULL;
4307 GHashTable *override_map = NULL;
4309 #if (DEBUG_INTERFACE_VTABLE_CODE|TRACE_INTERFACE_VTABLE_CODE)
4310 int first_non_interface_slot;
4312 GSList *virt_methods = NULL, *l;
4313 int stelemref_slot = 0;
4318 if (overrides && !verify_class_overrides (class, overrides, onum))
4321 ifaces = mono_class_get_implemented_interfaces (class, &error);
4322 if (!mono_error_ok (&error)) {
4323 char *name = mono_type_get_full_name (class);
4324 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup_printf ("Could not resolve %s interfaces due to %s", name, mono_error_get_message (&error)));
4326 mono_error_cleanup (&error);
4328 } else if (ifaces) {
4329 for (i = 0; i < ifaces->len; i++) {
4330 MonoClass *ic = g_ptr_array_index (ifaces, i);
4331 max_vtsize += ic->method.count;
4333 g_ptr_array_free (ifaces, TRUE);
4337 if (class->parent) {
4338 mono_class_init (class->parent);
4339 mono_class_setup_vtable_full (class->parent, in_setup);
4341 if (class->parent->exception_type) {
4342 char *name = mono_type_get_full_name (class->parent);
4343 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup_printf ("Parent %s failed to load", name));
4348 max_vtsize += class->parent->vtable_size;
4349 cur_slot = class->parent->vtable_size;
4352 max_vtsize += class->method.count;
4354 /*Array have a slot for stelemref*/
4355 if (mono_class_need_stelemref_method (class)) {
4356 stelemref_slot = cur_slot;
4361 vtable = alloca (sizeof (gpointer) * max_vtsize);
4362 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
4364 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
4366 cur_slot = setup_interface_offsets (class, cur_slot, TRUE);
4367 if (cur_slot == -1) /*setup_interface_offsets fails the type.*/
4370 max_iid = class->max_interface_id;
4371 DEBUG_INTERFACE_VTABLE (first_non_interface_slot = cur_slot);
4373 /* Optimized version for generic instances */
4374 if (class->generic_class) {
4376 MonoClass *gklass = class->generic_class->container_class;
4379 mono_class_setup_vtable_full (gklass, in_setup);
4380 if (gklass->exception_type != MONO_EXCEPTION_NONE) {
4381 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
4385 tmp = mono_class_alloc0 (class, sizeof (gpointer) * gklass->vtable_size);
4386 class->vtable_size = gklass->vtable_size;
4387 for (i = 0; i < gklass->vtable_size; ++i)
4388 if (gklass->vtable [i]) {
4389 MonoMethod *inflated = mono_class_inflate_generic_method_full_checked (gklass->vtable [i], class, mono_class_get_context (class), &error);
4390 if (!mono_error_ok (&error)) {
4391 char *err_msg = g_strdup_printf ("Could not inflate method due to %s", mono_error_get_message (&error));
4392 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, err_msg);
4394 mono_error_cleanup (&error);
4398 tmp [i]->slot = gklass->vtable [i]->slot;
4400 mono_memory_barrier ();
4401 class->vtable = tmp;
4403 /* Have to set method->slot for abstract virtual methods */
4404 if (class->methods && gklass->methods) {
4405 for (i = 0; i < class->method.count; ++i)
4406 if (class->methods [i]->slot == -1)
4407 class->methods [i]->slot = gklass->methods [i]->slot;
4413 if (class->parent && class->parent->vtable_size) {
4414 MonoClass *parent = class->parent;
4417 memcpy (vtable, parent->vtable, sizeof (gpointer) * parent->vtable_size);
4419 // Also inherit parent interface vtables, just as a starting point.
4420 // This is needed otherwise bug-77127.exe fails when the property methods
4421 // have different names in the iterface and the class, because for child
4422 // classes the ".override" information is not used anymore.
4423 for (i = 0; i < parent->interface_offsets_count; i++) {
4424 MonoClass *parent_interface = parent->interfaces_packed [i];
4425 int interface_offset = mono_class_interface_offset (class, parent_interface);
4426 /*FIXME this is now dead code as this condition will never hold true.
4427 Since interface offsets are inherited then the offset of an interface implemented
4428 by a parent will never be the out of it's vtable boundary.
4430 if (interface_offset >= parent->vtable_size) {
4431 int parent_interface_offset = mono_class_interface_offset (parent, parent_interface);
4434 mono_class_setup_methods (parent_interface); /*FIXME Just kill this whole chunk of dead code*/
4435 TRACE_INTERFACE_VTABLE (printf (" +++ Inheriting interface %s.%s\n", parent_interface->name_space, parent_interface->name));
4436 for (j = 0; j < parent_interface->method.count && !class->exception_type; j++) {
4437 vtable [interface_offset + j] = parent->vtable [parent_interface_offset + j];
4438 TRACE_INTERFACE_VTABLE (printf (" --- Inheriting: [%03d][(%03d)+(%03d)] => [%03d][(%03d)+(%03d)]\n",
4439 parent_interface_offset + j, parent_interface_offset, j,
4440 interface_offset + j, interface_offset, j));
4447 /*Array have a slot for stelemref*/
4448 if (mono_class_need_stelemref_method (class)) {
4449 MonoMethod *method = mono_marshal_get_virtual_stelemref (class);
4451 method->slot = stelemref_slot;
4453 g_assert (method->slot == stelemref_slot);
4455 vtable [stelemref_slot] = method;
4458 TRACE_INTERFACE_VTABLE (print_vtable_full (class, vtable, cur_slot, first_non_interface_slot, "AFTER INHERITING PARENT VTABLE", TRUE));
4459 /* override interface methods */
4460 for (i = 0; i < onum; i++) {
4461 MonoMethod *decl = overrides [i*2];
4462 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
4464 dslot = mono_method_get_vtable_slot (decl);
4466 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
4470 dslot += mono_class_interface_offset (class, decl->klass);
4471 vtable [dslot] = overrides [i*2 + 1];
4472 vtable [dslot]->slot = dslot;
4474 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
4476 g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
4478 if (mono_security_core_clr_enabled ())
4479 mono_security_core_clr_check_override (class, vtable [dslot], decl);
4482 TRACE_INTERFACE_VTABLE (print_overrides (override_map, "AFTER OVERRIDING INTERFACE METHODS"));
4483 TRACE_INTERFACE_VTABLE (print_vtable_full (class, vtable, cur_slot, first_non_interface_slot, "AFTER OVERRIDING INTERFACE METHODS", FALSE));
4486 * Create a list of virtual methods to avoid calling
4487 * mono_class_get_virtual_methods () which is slow because of the metadata
4491 gpointer iter = NULL;
4494 virt_methods = NULL;
4495 while ((cm = mono_class_get_virtual_methods (class, &iter))) {
4496 virt_methods = g_slist_prepend (virt_methods, cm);
4498 if (class->exception_type)
4502 // Loop on all implemented interfaces...
4503 for (i = 0; i < class->interface_offsets_count; i++) {
4504 MonoClass *parent = class->parent;
4506 gboolean interface_is_explicitly_implemented_by_class;
4509 ic = class->interfaces_packed [i];
4510 ic_offset = mono_class_interface_offset (class, ic);
4512 mono_class_setup_methods (ic);
4513 if (ic->exception_type)
4516 // Check if this interface is explicitly implemented (instead of just inherited)
4517 if (parent != NULL) {
4518 int implemented_interfaces_index;
4519 interface_is_explicitly_implemented_by_class = FALSE;
4520 for (implemented_interfaces_index = 0; implemented_interfaces_index < class->interface_count; implemented_interfaces_index++) {
4521 if (ic == class->interfaces [implemented_interfaces_index]) {
4522 interface_is_explicitly_implemented_by_class = TRUE;
4527 interface_is_explicitly_implemented_by_class = TRUE;
4530 // Loop on all interface methods...
4531 for (im_index = 0; im_index < ic->method.count; im_index++) {
4532 MonoMethod *im = ic->methods [im_index];
4533 int im_slot = ic_offset + im->slot;
4534 MonoMethod *override_im = (override_map != NULL) ? g_hash_table_lookup (override_map, im) : NULL;
4536 if (im->flags & METHOD_ATTRIBUTE_STATIC)
4539 TRACE_INTERFACE_VTABLE (printf ("\tchecking iface method %s\n", mono_method_full_name (im,1)));
4541 // If there is an explicit implementation, just use it right away,
4542 // otherwise look for a matching method
4543 if (override_im == NULL) {
4547 // First look for a suitable method among the class methods
4548 for (l = virt_methods; l; l = l->next) {
4550 TRACE_INTERFACE_VTABLE (printf (" For slot %d ('%s'.'%s':'%s'), trying method '%s'.'%s':'%s'... [EXPLICIT IMPLEMENTATION = %d][SLOT IS NULL = %d]", im_slot, ic->name_space, ic->name, im->name, cm->klass->name_space, cm->klass->name, cm->name, interface_is_explicitly_implemented_by_class, (vtable [im_slot] == NULL)));
4551 if (check_interface_method_override (class, im, cm, TRUE, interface_is_explicitly_implemented_by_class, (vtable [im_slot] == NULL))) {
4552 TRACE_INTERFACE_VTABLE (printf ("[check ok]: ASSIGNING"));
4553 vtable [im_slot] = cm;
4554 /* Why do we need this? */
4559 TRACE_INTERFACE_VTABLE (printf ("\n"));
4560 if (class->exception_type) /*Might be set by check_interface_method_override*/
4564 // If the slot is still empty, look in all the inherited virtual methods...
4565 if ((vtable [im_slot] == NULL) && class->parent != NULL) {
4566 MonoClass *parent = class->parent;
4567 // Reverse order, so that last added methods are preferred
4568 for (cm_index = parent->vtable_size - 1; cm_index >= 0; cm_index--) {
4569 MonoMethod *cm = parent->vtable [cm_index];
4571 TRACE_INTERFACE_VTABLE ((cm != NULL) && printf (" For slot %d ('%s'.'%s':'%s'), trying (ancestor) method '%s'.'%s':'%s'... ", im_slot, ic->name_space, ic->name, im->name, cm->klass->name_space, cm->klass->name, cm->name));
4572 if ((cm != NULL) && check_interface_method_override (class, im, cm, FALSE, FALSE, TRUE)) {
4573 TRACE_INTERFACE_VTABLE (printf ("[everything ok]: ASSIGNING"));
4574 vtable [im_slot] = cm;
4575 /* Why do we need this? */
4581 if (class->exception_type) /*Might be set by check_interface_method_override*/
4583 TRACE_INTERFACE_VTABLE ((cm != NULL) && printf ("\n"));
4587 g_assert (vtable [im_slot] == override_im);
4592 // If the class is not abstract, check that all its interface slots are full.
4593 // The check is done here and not directly at the end of the loop above because
4594 // it can happen (for injected generic array interfaces) that the same slot is
4595 // processed multiple times (those interfaces have overlapping slots), and it
4596 // will not always be the first pass the one that fills the slot.
4597 if (! (class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
4598 for (i = 0; i < class->interface_offsets_count; i++) {
4602 ic = class->interfaces_packed [i];
4603 ic_offset = mono_class_interface_offset (class, ic);
4605 for (im_index = 0; im_index < ic->method.count; im_index++) {
4606 MonoMethod *im = ic->methods [im_index];
4607 int im_slot = ic_offset + im->slot;
4609 if (im->flags & METHOD_ATTRIBUTE_STATIC)
4612 TRACE_INTERFACE_VTABLE (printf (" [class is not abstract, checking slot %d for interface '%s'.'%s', method %s, slot check is %d]\n",
4613 im_slot, ic->name_space, ic->name, im->name, (vtable [im_slot] == NULL)));
4614 if (vtable [im_slot] == NULL) {
4615 print_unimplemented_interface_method_info (class, ic, im, im_slot, overrides, onum);
4622 TRACE_INTERFACE_VTABLE (print_vtable_full (class, vtable, cur_slot, first_non_interface_slot, "AFTER SETTING UP INTERFACE METHODS", FALSE));
4623 for (l = virt_methods; l; l = l->next) {
4626 * If the method is REUSE_SLOT, we must check in the
4627 * base class for a method to override.
4629 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
4631 for (k = class->parent; k ; k = k->parent) {
4636 while ((m1 = mono_class_get_virtual_methods (k, &k_iter))) {
4637 MonoMethodSignature *cmsig, *m1sig;
4639 cmsig = mono_method_signature (cm);
4640 m1sig = mono_method_signature (m1);
4642 if (!cmsig || !m1sig) {
4643 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
4647 if (!strcmp(cm->name, m1->name) &&
4648 mono_metadata_signature_equal (cmsig, m1sig)) {
4650 if (mono_security_core_clr_enabled ())
4651 mono_security_core_clr_check_override (class, cm, m1);
4653 slot = mono_method_get_vtable_slot (m1);
4657 if (is_wcf_hack_disabled () && !mono_method_can_access_method_full (cm, m1, NULL)) {
4658 char *body_name = mono_method_full_name (cm, TRUE);
4659 char *decl_name = mono_method_full_name (m1, TRUE);
4660 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup_printf ("Method %s overrides method '%s' which is not accessible", body_name, decl_name));
4666 g_assert (cm->slot < max_vtsize);
4668 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
4669 TRACE_INTERFACE_VTABLE (printf ("adding iface override from %s [%p] to %s [%p]\n",
4670 mono_method_full_name (m1, 1), m1,
4671 mono_method_full_name (cm, 1), cm));
4672 g_hash_table_insert (override_map, m1, cm);
4676 if (k->exception_type)
4686 /*Non final newslot methods must be given a non-interface vtable slot*/
4687 if ((cm->flags & METHOD_ATTRIBUTE_NEW_SLOT) && !(cm->flags & METHOD_ATTRIBUTE_FINAL) && cm->slot >= 0)
4691 cm->slot = cur_slot++;
4693 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
4694 vtable [cm->slot] = cm;
4697 /* override non interface methods */
4698 for (i = 0; i < onum; i++) {
4699 MonoMethod *decl = overrides [i*2];
4700 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
4701 g_assert (decl->slot != -1);
4702 vtable [decl->slot] = overrides [i*2 + 1];
4703 overrides [i * 2 + 1]->slot = decl->slot;
4705 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
4706 TRACE_INTERFACE_VTABLE (printf ("adding explicit override from %s [%p] to %s [%p]\n",
4707 mono_method_full_name (decl, 1), decl,
4708 mono_method_full_name (overrides [i * 2 + 1], 1), overrides [i * 2 + 1]));
4709 g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
4711 if (mono_security_core_clr_enabled ())
4712 mono_security_core_clr_check_override (class, vtable [decl->slot], decl);
4717 * If a method occupies more than one place in the vtable, and it is
4718 * overriden, then change the other occurances too.
4723 for (i = 0; i < max_vtsize; ++i)
4725 TRACE_INTERFACE_VTABLE (printf ("checking slot %d method %s[%p] for overrides\n", i, mono_method_full_name (vtable [i], 1), vtable [i]));
4727 cm = g_hash_table_lookup (override_map, vtable [i]);
4732 g_hash_table_destroy (override_map);
4733 override_map = NULL;
4736 g_slist_free (virt_methods);
4737 virt_methods = NULL;
4739 /* Ensure that all vtable slots are filled with concrete instance methods */
4740 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
4741 for (i = 0; i < cur_slot; ++i) {
4742 if (vtable [i] == NULL || (vtable [i]->flags & (METHOD_ATTRIBUTE_ABSTRACT | METHOD_ATTRIBUTE_STATIC))) {
4743 char *type_name = mono_type_get_full_name (class);
4744 char *method_name = vtable [i] ? mono_method_full_name (vtable [i], TRUE) : g_strdup ("none");
4745 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup_printf ("Type %s has invalid vtable method slot %d with method %s", type_name, i, method_name));
4747 g_free (method_name);
4753 if (class->generic_class) {
4754 MonoClass *gklass = class->generic_class->container_class;
4756 mono_class_init (gklass);
4758 class->vtable_size = MAX (gklass->vtable_size, cur_slot);
4760 /* Check that the vtable_size value computed in mono_class_init () is correct */
4761 if (class->vtable_size)
4762 g_assert (cur_slot == class->vtable_size);
4763 class->vtable_size = cur_slot;
4766 /* Try to share the vtable with our parent. */
4767 if (class->parent && (class->parent->vtable_size == class->vtable_size) && (memcmp (class->parent->vtable, vtable, sizeof (gpointer) * class->vtable_size) == 0)) {
4768 mono_memory_barrier ();
4769 class->vtable = class->parent->vtable;
4771 MonoMethod **tmp = mono_class_alloc0 (class, sizeof (gpointer) * class->vtable_size);
4772 memcpy (tmp, vtable, sizeof (gpointer) * class->vtable_size);
4773 mono_memory_barrier ();
4774 class->vtable = tmp;
4777 DEBUG_INTERFACE_VTABLE (print_vtable_full (class, class->vtable, class->vtable_size, first_non_interface_slot, "FINALLY", FALSE));
4778 if (mono_print_vtable) {
4781 print_implemented_interfaces (class);
4783 for (i = 0; i <= max_iid; i++)
4784 if (MONO_CLASS_IMPLEMENTS_INTERFACE (class, i))
4787 printf ("VTable %s (vtable entries = %d, interfaces = %d)\n", mono_type_full_name (&class->byval_arg),
4788 class->vtable_size, icount);
4790 for (i = 0; i < cur_slot; ++i) {
4795 printf (" slot assigned: %03d, slot index: %03d %s\n", i, cm->slot,
4796 mono_method_full_name (cm, TRUE));
4802 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
4803 class->name, max_iid);
4805 for (i = 0; i < class->interface_count; i++) {
4806 ic = class->interfaces [i];
4807 printf (" slot offset: %03d, method count: %03d, iid: %03d %s\n",
4808 mono_class_interface_offset (class, ic),
4809 count_virtual_methods (ic), ic->interface_id, mono_type_full_name (&ic->byval_arg));
4812 for (k = class->parent; k ; k = k->parent) {
4813 for (i = 0; i < k->interface_count; i++) {
4814 ic = k->interfaces [i];
4815 printf (" parent slot offset: %03d, method count: %03d, iid: %03d %s\n",
4816 mono_class_interface_offset (class, ic),
4817 count_virtual_methods (ic), ic->interface_id, mono_type_full_name (&ic->byval_arg));
4823 VERIFY_INTERFACE_VTABLE (mono_class_verify_vtable (class));
4828 char *name = mono_type_get_full_name (class);
4829 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup_printf ("VTable setup of type %s failed", name));
4832 g_hash_table_destroy (override_map);
4834 g_slist_free (virt_methods);
4839 * mono_method_get_vtable_slot:
4841 * Returns method->slot, computing it if neccesary. Return -1 on failure.
4842 * LOCKING: Acquires the loader lock.
4844 * FIXME Use proper MonoError machinery here.
4847 mono_method_get_vtable_slot (MonoMethod *method)
4849 if (method->slot == -1) {
4850 mono_class_setup_vtable (method->klass);
4851 if (method->klass->exception_type)
4853 if (method->slot == -1) {
4857 if (!method->klass->generic_class) {
4858 g_assert (method->is_inflated);
4859 return mono_method_get_vtable_slot (((MonoMethodInflated*)method)->declaring);
4862 /* This can happen for abstract methods of generic instances due to the shortcut code in mono_class_setup_vtable_general (). */
4863 g_assert (method->klass->generic_class);
4864 gklass = method->klass->generic_class->container_class;
4865 mono_class_setup_methods (method->klass);
4866 g_assert (method->klass->methods);
4867 for (i = 0; i < method->klass->method.count; ++i) {
4868 if (method->klass->methods [i] == method)
4871 g_assert (i < method->klass->method.count);
4872 g_assert (gklass->methods);
4873 method->slot = gklass->methods [i]->slot;
4875 g_assert (method->slot != -1);
4877 return method->slot;
4881 * mono_method_get_vtable_index:
4884 * Returns the index into the runtime vtable to access the method or,
4885 * in the case of a virtual generic method, the virtual generic method
4886 * thunk. Returns -1 on failure.
4888 * FIXME Use proper MonoError machinery here.
4891 mono_method_get_vtable_index (MonoMethod *method)
4893 if (method->is_inflated && (method->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
4894 MonoMethodInflated *imethod = (MonoMethodInflated*)method;
4895 if (imethod->declaring->is_generic)
4896 return mono_method_get_vtable_slot (imethod->declaring);
4898 return mono_method_get_vtable_slot (method);
4901 static MonoMethod *default_ghc = NULL;
4902 static MonoMethod *default_finalize = NULL;
4903 static int finalize_slot = -1;
4904 static int ghc_slot = -1;
4907 initialize_object_slots (MonoClass *class)
4912 if (class == mono_defaults.object_class) {
4913 mono_class_setup_vtable (class);
4914 for (i = 0; i < class->vtable_size; ++i) {
4915 MonoMethod *cm = class->vtable [i];
4917 if (!strcmp (cm->name, "GetHashCode"))
4919 else if (!strcmp (cm->name, "Finalize"))
4923 g_assert (ghc_slot > 0);
4924 default_ghc = class->vtable [ghc_slot];
4926 g_assert (finalize_slot > 0);
4927 default_finalize = class->vtable [finalize_slot];
4932 MonoMethod *array_method;
4934 } GenericArrayMethodInfo;
4936 static int generic_array_method_num = 0;
4937 static GenericArrayMethodInfo *generic_array_method_info = NULL;
4940 generic_array_methods (MonoClass *class)
4942 int i, count_generic = 0;
4943 GList *list = NULL, *tmp;
4944 if (generic_array_method_num)
4945 return generic_array_method_num;
4946 mono_class_setup_methods (class->parent); /*This is setting up System.Array*/
4947 g_assert (!class->parent->exception_type); /*So hitting this assert is a huge problem*/
4948 for (i = 0; i < class->parent->method.count; i++) {
4949 MonoMethod *m = class->parent->methods [i];
4950 if (!strncmp (m->name, "InternalArray__", 15)) {
4952 list = g_list_prepend (list, m);
4955 list = g_list_reverse (list);
4956 generic_array_method_info = mono_image_alloc (mono_defaults.corlib, sizeof (GenericArrayMethodInfo) * count_generic);
4958 for (tmp = list; tmp; tmp = tmp->next) {
4959 const char *mname, *iname;
4961 MonoMethod *m = tmp->data;
4962 const char *ireadonlylist_prefix = "InternalArray__IReadOnlyList_";
4963 const char *ireadonlycollection_prefix = "InternalArray__IReadOnlyCollection_";
4965 generic_array_method_info [i].array_method = m;
4966 if (!strncmp (m->name, "InternalArray__ICollection_", 27)) {
4967 iname = "System.Collections.Generic.ICollection`1.";
4968 mname = m->name + 27;
4969 } else if (!strncmp (m->name, "InternalArray__IEnumerable_", 27)) {
4970 iname = "System.Collections.Generic.IEnumerable`1.";
4971 mname = m->name + 27;
4972 } else if (!strncmp (m->name, ireadonlylist_prefix, strlen (ireadonlylist_prefix))) {
4973 iname = "System.Collections.Generic.IReadOnlyList`1.";
4974 mname = m->name + strlen (ireadonlylist_prefix);
4975 } else if (!strncmp (m->name, ireadonlycollection_prefix, strlen (ireadonlycollection_prefix))) {
4976 iname = "System.Collections.Generic.IReadOnlyCollection`1.";
4977 mname = m->name + strlen (ireadonlycollection_prefix);
4978 } else if (!strncmp (m->name, "InternalArray__", 15)) {
4979 iname = "System.Collections.Generic.IList`1.";
4980 mname = m->name + 15;
4982 g_assert_not_reached ();
4985 name = mono_image_alloc (mono_defaults.corlib, strlen (iname) + strlen (mname) + 1);
4986 strcpy (name, iname);
4987 strcpy (name + strlen (iname), mname);
4988 generic_array_method_info [i].name = name;
4991 /*g_print ("array generic methods: %d\n", count_generic);*/
4993 generic_array_method_num = count_generic;
4995 return generic_array_method_num;
4999 setup_generic_array_ifaces (MonoClass *class, MonoClass *iface, MonoMethod **methods, int pos)
5001 MonoGenericContext tmp_context;
5004 tmp_context.class_inst = NULL;
5005 tmp_context.method_inst = iface->generic_class->context.class_inst;
5006 //g_print ("setting up array interface: %s\n", mono_type_get_name_full (&iface->byval_arg, 0));
5008 for (i = 0; i < generic_array_method_num; i++) {
5010 MonoMethod *m = generic_array_method_info [i].array_method;
5011 MonoMethod *inflated;
5013 inflated = mono_class_inflate_generic_method_checked (m, &tmp_context, &error);
5014 g_assert (mono_error_ok (&error)); /*FIXME proper error handling*/
5015 methods [pos++] = mono_marshal_get_generic_array_helper (class, iface, generic_array_method_info [i].name, inflated);
5020 concat_two_strings_with_zero (MonoImage *image, const char *s1, const char *s2)
5022 int null_length = strlen ("(null)");
5023 int len = (s1 ? strlen (s1) : null_length) + (s2 ? strlen (s2) : null_length) + 2;
5024 char *s = mono_image_alloc (image, len);
5027 result = g_snprintf (s, len, "%s%c%s", s1 ? s1 : "(null)", '\0', s2 ? s2 : "(null)");
5028 g_assert (result == len - 1);
5034 set_failure_from_loader_error (MonoClass *class, MonoLoaderError *error)
5036 gpointer exception_data = NULL;
5038 switch (error->exception_type) {
5039 case MONO_EXCEPTION_TYPE_LOAD:
5040 exception_data = concat_two_strings_with_zero (class->image, error->class_name, error->assembly_name);
5043 case MONO_EXCEPTION_MISSING_METHOD:
5044 exception_data = concat_two_strings_with_zero (class->image, error->class_name, error->member_name);
5047 case MONO_EXCEPTION_MISSING_FIELD: {
5048 const char *name_space = error->klass->name_space ? error->klass->name_space : NULL;
5049 const char *class_name;
5052 class_name = g_strdup_printf ("%s.%s", name_space, error->klass->name);
5054 class_name = error->klass->name;
5056 exception_data = concat_two_strings_with_zero (class->image, class_name, error->member_name);
5059 g_free ((void*)class_name);
5063 case MONO_EXCEPTION_FILE_NOT_FOUND: {
5066 if (error->ref_only)
5067 msg = "Cannot resolve dependency to assembly '%s' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.";
5069 msg = "Could not load file or assembly '%s' or one of its dependencies.";
5071 exception_data = concat_two_strings_with_zero (class->image, msg, error->assembly_name);
5075 case MONO_EXCEPTION_BAD_IMAGE:
5076 exception_data = error->msg;
5080 g_assert_not_reached ();
5083 mono_class_set_failure (class, error->exception_type, exception_data);
5088 * @class: the class to initialize
5090 * Compute the instance_size, class_size and other infos that cannot be
5091 * computed at mono_class_get() time. Also compute vtable_size if possible.
5092 * Returns TRUE on success or FALSE if there was a problem in loading
5093 * the type (incorrect assemblies, missing assemblies, methods, etc).
5095 * LOCKING: Acquires the loader lock.
5098 mono_class_init (MonoClass *class)
5101 MonoCachedClassInfo cached_info;
5102 gboolean has_cached_info;
5106 /* Double-checking locking pattern */
5107 if (class->inited || class->exception_type)
5108 return class->exception_type == MONO_EXCEPTION_NONE;
5110 /*g_print ("Init class %s\n", mono_type_get_full_name (class));*/
5112 /* We do everything inside the lock to prevent races */
5113 mono_loader_lock ();
5115 if (class->inited || class->exception_type) {
5116 mono_loader_unlock ();
5117 /* Somebody might have gotten in before us */
5118 return class->exception_type == MONO_EXCEPTION_NONE;
5121 if (class->init_pending) {
5122 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Recursive type definition detected"));
5126 class->init_pending = 1;
5128 if (mono_verifier_is_enabled_for_class (class) && !mono_verifier_verify_class (class)) {
5129 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, concat_two_strings_with_zero (class->image, class->name, class->image->assembly_name));
5134 if (class->byval_arg.type == MONO_TYPE_ARRAY || class->byval_arg.type == MONO_TYPE_SZARRAY) {
5135 MonoClass *element_class = class->element_class;
5136 if (!element_class->inited)
5137 mono_class_init (element_class);
5138 if (element_class->exception_type != MONO_EXCEPTION_NONE) {
5139 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
5144 mono_stats.initialized_class_count++;
5146 if (class->generic_class && !class->generic_class->is_dynamic) {
5147 MonoClass *gklass = class->generic_class->container_class;
5149 mono_stats.generic_class_count++;
5151 class->method = gklass->method;
5152 class->field = gklass->field;
5154 mono_class_init (gklass);
5155 // FIXME: Why is this needed ?
5156 if (!gklass->exception_type)
5157 mono_class_setup_methods (gklass);
5158 if (gklass->exception_type) {
5159 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup_printf ("Generic Type Defintion failed to init"));
5163 if (MONO_CLASS_IS_INTERFACE (class))
5164 class->interface_id = mono_get_unique_iid (class);
5167 if (class->parent && !class->parent->inited)
5168 mono_class_init (class->parent);
5170 has_cached_info = mono_class_get_cached_class_info (class, &cached_info);
5172 if (class->generic_class || image_is_dynamic (class->image) || !class->type_token || (has_cached_info && !cached_info.has_nested_classes))
5173 class->nested_classes_inited = TRUE;
5176 * Computes the size used by the fields, and their locations
5178 if (has_cached_info) {
5179 class->instance_size = cached_info.instance_size;
5180 class->sizes.class_size = cached_info.class_size;
5181 class->packing_size = cached_info.packing_size;
5182 class->min_align = cached_info.min_align;
5183 class->blittable = cached_info.blittable;
5184 class->has_references = cached_info.has_references;
5185 class->has_static_refs = cached_info.has_static_refs;
5186 class->no_special_static_fields = cached_info.no_special_static_fields;
5189 if (!class->size_inited){
5190 mono_class_setup_fields (class);
5191 if (class->exception_type || mono_loader_get_last_error ())
5195 /* Initialize arrays */
5197 class->method.count = 3 + (class->rank > 1? 2: 1);
5199 if (class->interface_count) {
5200 int count_generic = generic_array_methods (class);
5201 class->method.count += class->interface_count * count_generic;
5205 mono_class_setup_supertypes (class);
5208 initialize_object_slots (class);
5211 * Initialize the rest of the data without creating a generic vtable if possible.
5212 * If possible, also compute vtable_size, so mono_class_create_runtime_vtable () can
5213 * also avoid computing a generic vtable.
5215 if (has_cached_info) {
5217 class->vtable_size = cached_info.vtable_size;
5218 class->has_finalize = cached_info.has_finalize;
5219 class->has_finalize_inited = TRUE;
5220 class->ghcimpl = cached_info.ghcimpl;
5221 class->has_cctor = cached_info.has_cctor;
5222 } else if (class->rank == 1 && class->byval_arg.type == MONO_TYPE_SZARRAY) {
5223 /* SZARRAY can have 2 vtable layouts, with and without the stelemref method.
5224 * The first slot if for array with.
5226 static int szarray_vtable_size[2] = { 0 };
5228 int slot = MONO_TYPE_IS_REFERENCE (&class->element_class->byval_arg) ? 0 : 1;
5231 if (!szarray_vtable_size [slot]) {
5232 mono_class_setup_vtable (class);
5233 szarray_vtable_size [slot] = class->vtable_size;
5235 class->vtable_size = szarray_vtable_size[slot];
5237 } else if (class->generic_class && !MONO_CLASS_IS_INTERFACE (class)) {
5238 MonoClass *gklass = class->generic_class->container_class;
5240 /* Generic instance case */
5241 class->ghcimpl = gklass->ghcimpl;
5242 class->has_cctor = gklass->has_cctor;
5244 mono_class_setup_vtable (gklass);
5245 if (gklass->exception_type) {
5246 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
5250 class->vtable_size = gklass->vtable_size;
5254 /* ghcimpl is not currently used
5256 if (class->parent) {
5257 MonoMethod *cmethod = class->vtable [ghc_slot];
5258 if (cmethod->is_inflated)
5259 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
5260 if (cmethod == default_ghc) {
5266 /* C# doesn't allow interfaces to have cctors */
5267 if (!MONO_CLASS_IS_INTERFACE (class) || class->image != mono_defaults.corlib) {
5268 MonoMethod *cmethod = NULL;
5270 if (class->type_token) {
5271 cmethod = find_method_in_metadata (class, ".cctor", 0, METHOD_ATTRIBUTE_SPECIAL_NAME);
5272 /* The find_method function ignores the 'flags' argument */
5273 if (cmethod && (cmethod->flags & METHOD_ATTRIBUTE_SPECIAL_NAME))
5274 class->has_cctor = 1;
5276 mono_class_setup_methods (class);
5277 if (class->exception_type)
5280 for (i = 0; i < class->method.count; ++i) {
5281 MonoMethod *method = class->methods [i];
5282 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
5283 (strcmp (".cctor", method->name) == 0)) {
5284 class->has_cctor = 1;
5292 if (class->parent) {
5293 int first_iface_slot;
5294 /* This will compute class->parent->vtable_size for some classes */
5295 mono_class_init (class->parent);
5296 if (class->parent->exception_type) {
5297 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
5300 if (mono_loader_get_last_error ())
5302 if (!class->parent->vtable_size) {
5303 /* FIXME: Get rid of this somehow */
5304 mono_class_setup_vtable (class->parent);
5305 if (class->parent->exception_type) {
5306 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
5309 if (mono_loader_get_last_error ())
5312 first_iface_slot = class->parent->vtable_size;
5313 if (mono_class_need_stelemref_method (class))
5315 setup_interface_offsets (class, first_iface_slot, TRUE);
5317 setup_interface_offsets (class, 0, TRUE);
5320 if (mono_security_core_clr_enabled ())
5321 mono_security_core_clr_check_inheritance (class);
5323 if (mono_loader_get_last_error ()) {
5324 if (class->exception_type == MONO_EXCEPTION_NONE) {
5325 set_failure_from_loader_error (class, mono_loader_get_last_error ());
5327 mono_loader_clear_error ();
5330 if (class->generic_class && !mono_verifier_class_is_valid_generic_instantiation (class))
5331 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Invalid generic instantiation"));
5336 /* Because of the double-checking locking pattern */
5337 mono_memory_barrier ();
5339 class->init_pending = 0;
5341 mono_loader_unlock ();
5343 return class->exception_type == MONO_EXCEPTION_NONE;
5347 * mono_class_has_finalizer:
5349 * Return whenever KLASS has a finalizer, initializing klass->has_finalizer in the
5353 mono_class_has_finalizer (MonoClass *klass)
5355 MonoClass *class = klass;
5356 gboolean has_finalize = FALSE;
5358 if (klass->has_finalize_inited)
5359 return klass->has_finalize;
5361 /* Interfaces and valuetypes are not supposed to have finalizers */
5362 if (!(MONO_CLASS_IS_INTERFACE (class) || class->valuetype)) {
5363 MonoMethod *cmethod = NULL;
5365 if (class->rank == 1 && class->byval_arg.type == MONO_TYPE_SZARRAY) {
5366 } else if (class->generic_class) {
5367 MonoClass *gklass = class->generic_class->container_class;
5369 has_finalize = mono_class_has_finalizer (gklass);
5370 } else if (class->parent && class->parent->has_finalize) {
5371 has_finalize = TRUE;
5373 if (class->parent) {
5375 * Can't search in metadata for a method named Finalize, because that
5376 * ignores overrides.
5378 mono_class_setup_vtable (class);
5379 if (class->exception_type || mono_loader_get_last_error ())
5382 cmethod = class->vtable [finalize_slot];
5386 g_assert (class->vtable_size > finalize_slot);
5388 if (class->parent) {
5389 if (cmethod->is_inflated)
5390 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
5391 if (cmethod != default_finalize)
5392 has_finalize = TRUE;
5398 mono_image_lock (klass->image);
5400 if (!klass->has_finalize_inited) {
5401 klass->has_finalize = has_finalize ? 1 : 0;
5403 mono_memory_barrier ();
5404 klass->has_finalize_inited = TRUE;
5407 mono_image_unlock (klass->image);
5409 return klass->has_finalize;
5413 mono_is_corlib_image (MonoImage *image)
5415 /* FIXME: allow the dynamic case for our compilers and with full trust */
5416 if (image_is_dynamic (image))
5417 return image->assembly && !strcmp (image->assembly->aname.name, "mscorlib");
5419 return image == mono_defaults.corlib;
5423 * LOCKING: this assumes the loader lock is held
5426 mono_class_setup_mono_type (MonoClass *class)
5428 const char *name = class->name;
5429 const char *nspace = class->name_space;
5430 gboolean is_corlib = mono_is_corlib_image (class->image);
5432 class->this_arg.byref = 1;
5433 class->this_arg.data.klass = class;
5434 class->this_arg.type = MONO_TYPE_CLASS;
5435 class->byval_arg.data.klass = class;
5436 class->byval_arg.type = MONO_TYPE_CLASS;
5438 if (is_corlib && !strcmp (nspace, "System")) {
5439 if (!strcmp (name, "ValueType")) {
5441 * do not set the valuetype bit for System.ValueType.
5442 * class->valuetype = 1;
5444 class->blittable = TRUE;
5445 } else if (!strcmp (name, "Enum")) {
5447 * do not set the valuetype bit for System.Enum.
5448 * class->valuetype = 1;
5450 class->valuetype = 0;
5451 class->enumtype = 0;
5452 } else if (!strcmp (name, "Object")) {
5453 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
5454 } else if (!strcmp (name, "String")) {
5455 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
5456 } else if (!strcmp (name, "TypedReference")) {
5457 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
5461 if (class->valuetype) {
5462 int t = MONO_TYPE_VALUETYPE;
5464 if (is_corlib && !strcmp (nspace, "System")) {
5467 if (!strcmp (name, "Boolean")) {
5468 t = MONO_TYPE_BOOLEAN;
5469 } else if (!strcmp(name, "Byte")) {
5471 class->blittable = TRUE;
5475 if (!strcmp (name, "Char")) {
5480 if (!strcmp (name, "Double")) {
5482 class->blittable = TRUE;
5486 if (!strcmp (name, "Int32")) {
5488 class->blittable = TRUE;
5489 } else if (!strcmp(name, "Int16")) {
5491 class->blittable = TRUE;
5492 } else if (!strcmp(name, "Int64")) {
5494 class->blittable = TRUE;
5495 } else if (!strcmp(name, "IntPtr")) {
5497 class->blittable = TRUE;
5501 if (!strcmp (name, "Single")) {
5503 class->blittable = TRUE;
5504 } else if (!strcmp(name, "SByte")) {
5506 class->blittable = TRUE;
5510 if (!strcmp (name, "UInt32")) {
5512 class->blittable = TRUE;
5513 } else if (!strcmp(name, "UInt16")) {
5515 class->blittable = TRUE;
5516 } else if (!strcmp(name, "UInt64")) {
5518 class->blittable = TRUE;
5519 } else if (!strcmp(name, "UIntPtr")) {
5521 class->blittable = TRUE;
5525 if (!strcmp (name, "TypedReference")) {
5526 t = MONO_TYPE_TYPEDBYREF;
5527 class->blittable = TRUE;
5531 if (!strcmp (name, "Void")) {
5539 class->this_arg.type = class->byval_arg.type = t;
5542 if (MONO_CLASS_IS_INTERFACE (class))
5543 class->interface_id = mono_get_unique_iid (class);
5549 * COM initialization is delayed until needed.
5550 * However when a [ComImport] attribute is present on a type it will trigger
5551 * the initialization. This is not a problem unless the BCL being executed
5552 * lacks the types that COM depends on (e.g. Variant on Silverlight).
5555 init_com_from_comimport (MonoClass *class)
5557 /* we don't always allow COM initialization under the CoreCLR (e.g. Moonlight does not require it) */
5558 if (mono_security_core_clr_enabled ()) {
5559 /* but some other CoreCLR user could requires it for their platform (i.e. trusted) code */
5560 if (!mono_security_core_clr_determine_platform_image (class->image)) {
5561 /* but it can not be made available for application (i.e. user code) since all COM calls
5562 * are considered native calls. In this case we fail with a TypeLoadException (just like
5563 * Silverlight 2 does */
5564 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
5569 /* FIXME : we should add an extra checks to ensure COM can be initialized properly before continuing */
5571 #endif /*DISABLE_COM*/
5574 * LOCKING: this assumes the loader lock is held
5577 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
5579 gboolean system_namespace;
5580 gboolean is_corlib = mono_is_corlib_image (class->image);
5582 system_namespace = !strcmp (class->name_space, "System") && is_corlib;
5584 /* if root of the hierarchy */
5585 if (system_namespace && !strcmp (class->name, "Object")) {
5586 class->parent = NULL;
5587 class->instance_size = sizeof (MonoObject);
5590 if (!strcmp (class->name, "<Module>")) {
5591 class->parent = NULL;
5592 class->instance_size = 0;
5596 if (!MONO_CLASS_IS_INTERFACE (class)) {
5597 /* Imported COM Objects always derive from __ComObject. */
5599 if (MONO_CLASS_IS_IMPORT (class)) {
5600 init_com_from_comimport (class);
5601 if (parent == mono_defaults.object_class)
5602 parent = mono_class_get_com_object_class ();
5606 /* set the parent to something useful and safe, but mark the type as broken */
5607 parent = mono_defaults.object_class;
5608 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
5611 class->parent = parent;
5613 if (parent->generic_class && !parent->name) {
5615 * If the parent is a generic instance, we may get
5616 * called before it is fully initialized, especially
5617 * before it has its name.
5622 #ifndef DISABLE_REMOTING
5623 class->marshalbyref = parent->marshalbyref;
5624 class->contextbound = parent->contextbound;
5627 class->delegate = parent->delegate;
5629 if (MONO_CLASS_IS_IMPORT (class) || mono_class_is_com_object (parent))
5630 mono_class_set_is_com_object (class);
5632 if (system_namespace) {
5633 #ifndef DISABLE_REMOTING
5634 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
5635 class->marshalbyref = 1;
5637 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
5638 class->contextbound = 1;
5640 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
5641 class->delegate = 1;
5644 if (class->parent->enumtype || (mono_is_corlib_image (class->parent->image) && (strcmp (class->parent->name, "ValueType") == 0) &&
5645 (strcmp (class->parent->name_space, "System") == 0)))
5646 class->valuetype = 1;
5647 if (mono_is_corlib_image (class->parent->image) && ((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
5648 class->valuetype = class->enumtype = 1;
5650 /*class->enumtype = class->parent->enumtype; */
5652 /* initialize com types if COM interfaces are present */
5654 if (MONO_CLASS_IS_IMPORT (class))
5655 init_com_from_comimport (class);
5657 class->parent = NULL;
5663 * mono_class_setup_supertypes:
5666 * Build the data structure needed to make fast type checks work.
5667 * This currently sets two fields in @class:
5668 * - idepth: distance between @class and System.Object in the type
5670 * - supertypes: array of classes: each element has a class in the hierarchy
5671 * starting from @class up to System.Object
5673 * LOCKING: This function is atomic, in case of contention we waste memory.
5676 mono_class_setup_supertypes (MonoClass *class)
5679 MonoClass **supertypes;
5681 mono_atomic_load_acquire (supertypes, void*, &class->supertypes);
5685 if (class->parent && !class->parent->supertypes)
5686 mono_class_setup_supertypes (class->parent);
5688 class->idepth = class->parent->idepth + 1;
5692 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
5693 supertypes = mono_class_alloc0 (class, sizeof (MonoClass *) * ms);
5695 if (class->parent) {
5696 supertypes [class->idepth - 1] = class;
5697 memcpy (supertypes, class->parent->supertypes, class->parent->idepth * sizeof (gpointer));
5699 supertypes [0] = class;
5702 mono_atomic_store_release (&class->supertypes, supertypes);
5706 fix_gclass_incomplete_instantiation (MonoClass *gclass, void *user_data)
5708 MonoClass *gtd = (MonoClass*)user_data;
5709 /* Only try to fix generic instances of @gtd */
5710 if (gclass->generic_class->container_class != gtd)
5713 /* Check if the generic instance has no parent. */
5714 if (gtd->parent && !gclass->parent)
5715 mono_generic_class_setup_parent (gclass, gtd);
5721 mono_class_set_failure_and_error (MonoClass *class, MonoError *error, const char *msg)
5723 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup (msg));
5724 mono_error_set_type_load_class (error, class, msg);
5728 mono_class_set_failure_from_loader_error (MonoClass *class, MonoError *error, char *msg)
5730 MonoLoaderError *lerror = mono_loader_get_last_error ();
5733 set_failure_from_loader_error (class, lerror);
5734 mono_error_set_from_loader_error (error);
5738 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, msg);
5739 mono_error_set_type_load_class (error, class, msg);
5744 * mono_class_create_from_typedef:
5745 * @image: image where the token is valid
5746 * @type_token: typedef token
5747 * @error: used to return any error found while creating the type
5749 * Create the MonoClass* representing the specified type token.
5750 * @type_token must be a TypeDef token.
5752 * FIXME: don't return NULL on failure, just the the caller figure it out.
5755 mono_class_create_from_typedef (MonoImage *image, guint32 type_token, MonoError *error)
5757 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
5758 MonoClass *class, *parent = NULL;
5759 guint32 cols [MONO_TYPEDEF_SIZE];
5760 guint32 cols_next [MONO_TYPEDEF_SIZE];
5761 guint tidx = mono_metadata_token_index (type_token);
5762 MonoGenericContext *context = NULL;
5763 const char *name, *nspace;
5765 MonoClass **interfaces;
5766 guint32 field_last, method_last;
5767 guint32 nesting_tokeen;
5769 mono_error_init (error);
5771 if (mono_metadata_token_table (type_token) != MONO_TABLE_TYPEDEF || tidx > tt->rows) {
5772 mono_error_set_bad_image (error, image, "Invalid typedef token %x", type_token);
5773 mono_loader_assert_no_error ();
5777 mono_loader_lock ();
5779 if ((class = mono_internal_hash_table_lookup (&image->class_cache, GUINT_TO_POINTER (type_token)))) {
5780 mono_loader_unlock ();
5781 mono_loader_assert_no_error ();
5785 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
5787 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
5788 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
5790 class = mono_image_alloc0 (image, sizeof (MonoClass));
5793 class->name_space = nspace;
5795 mono_profiler_class_event (class, MONO_PROFILE_START_LOAD);
5797 class->image = image;
5798 class->type_token = type_token;
5799 class->flags = cols [MONO_TYPEDEF_FLAGS];
5801 mono_internal_hash_table_insert (&image->class_cache, GUINT_TO_POINTER (type_token), class);
5803 classes_size += sizeof (MonoClass);
5806 * Check whether we're a generic type definition.
5808 class->generic_container = mono_metadata_load_generic_params (image, class->type_token, NULL);
5809 if (class->generic_container) {
5810 class->is_generic = 1;
5811 class->generic_container->owner.klass = class;
5812 context = &class->generic_container->context;
5815 if (class->generic_container)
5816 enable_gclass_recording ();
5818 if (cols [MONO_TYPEDEF_EXTENDS]) {
5820 guint32 parent_token = mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]);
5822 if (mono_metadata_token_table (parent_token) == MONO_TABLE_TYPESPEC) {
5823 /*WARNING: this must satisfy mono_metadata_type_hash*/
5824 class->this_arg.byref = 1;
5825 class->this_arg.data.klass = class;
5826 class->this_arg.type = MONO_TYPE_CLASS;
5827 class->byval_arg.data.klass = class;
5828 class->byval_arg.type = MONO_TYPE_CLASS;
5830 parent = mono_class_get_checked (image, parent_token, error);
5831 if (parent && context) /* Always inflate */
5832 parent = mono_class_inflate_generic_class_checked (parent, context, error);
5834 if (parent == NULL) {
5835 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup (mono_error_get_message (error)));
5836 goto parent_failure;
5839 for (tmp = parent; tmp; tmp = tmp->parent) {
5841 mono_class_set_failure_and_error (class, error, "Cycle found while resolving parent");
5842 goto parent_failure;
5844 if (class->generic_container && tmp->generic_class && tmp->generic_class->container_class == class) {
5845 mono_class_set_failure_and_error (class, error, "Parent extends generic instance of this type");
5846 goto parent_failure;
5851 mono_class_setup_parent (class, parent);
5853 /* uses ->valuetype, which is initialized by mono_class_setup_parent above */
5854 mono_class_setup_mono_type (class);
5856 if (class->generic_container)
5857 disable_gclass_recording (fix_gclass_incomplete_instantiation, class);
5860 * This might access class->byval_arg for recursion generated by generic constraints,
5861 * so it has to come after setup_mono_type ().
5863 if ((nesting_tokeen = mono_metadata_nested_in_typedef (image, type_token))) {
5864 class->nested_in = mono_class_create_from_typedef (image, nesting_tokeen, error);
5865 if (!mono_error_ok (error)) {
5866 /*FIXME implement a mono_class_set_failure_from_mono_error */
5867 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup (mono_error_get_message (error)));
5868 mono_loader_unlock ();
5869 mono_profiler_class_loaded (class, MONO_PROFILE_FAILED);
5870 mono_loader_assert_no_error ();
5875 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
5879 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
5883 class->cast_class = class->element_class = class;
5885 if (!class->enumtype) {
5886 if (!mono_metadata_interfaces_from_typedef_full (
5887 image, type_token, &interfaces, &icount, FALSE, context, error)){
5889 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup (mono_error_get_message (error)));
5890 mono_loader_unlock ();
5891 mono_profiler_class_loaded (class, MONO_PROFILE_FAILED);
5895 class->interfaces = interfaces;
5896 class->interface_count = icount;
5897 class->interfaces_inited = 1;
5900 /*g_print ("Load class %s\n", name);*/
5903 * Compute the field and method lists
5905 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
5906 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
5908 if (tt->rows > tidx){
5909 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
5910 field_last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
5911 method_last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
5913 field_last = image->tables [MONO_TABLE_FIELD].rows;
5914 method_last = image->tables [MONO_TABLE_METHOD].rows;
5917 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
5918 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
5919 class->field.count = field_last - class->field.first;
5921 class->field.count = 0;
5923 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
5924 class->method.count = method_last - class->method.first;
5926 class->method.count = 0;
5928 /* reserve space to store vector pointer in arrays */
5929 if (mono_is_corlib_image (image) && !strcmp (nspace, "System") && !strcmp (name, "Array")) {
5930 class->instance_size += 2 * sizeof (gpointer);
5931 g_assert (class->field.count == 0);
5934 if (class->enumtype) {
5935 MonoType *enum_basetype = mono_class_find_enum_basetype (class, error);
5936 if (!enum_basetype) {
5937 /*set it to a default value as the whole runtime can't handle this to be null*/
5938 class->cast_class = class->element_class = mono_defaults.int32_class;
5939 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup (mono_error_get_message (error)));
5940 mono_loader_unlock ();
5941 mono_profiler_class_loaded (class, MONO_PROFILE_FAILED);
5942 mono_loader_assert_no_error ();
5945 class->cast_class = class->element_class = mono_class_from_mono_type (enum_basetype);
5949 * If we're a generic type definition, load the constraints.
5950 * We must do this after the class has been constructed to make certain recursive scenarios
5953 if (class->generic_container && !mono_metadata_load_generic_param_constraints_checked (image, type_token, class->generic_container, error)) {
5954 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, g_strdup_printf ("Could not load generic parameter constrains due to %s", mono_error_get_message (error)));
5955 mono_loader_unlock ();
5956 mono_profiler_class_loaded (class, MONO_PROFILE_FAILED);
5957 mono_loader_assert_no_error ();
5961 if (class->image->assembly_name && !strcmp (class->image->assembly_name, "Mono.Simd") && !strcmp (nspace, "Mono.Simd")) {
5962 if (!strncmp (name, "Vector", 6))
5963 class->simd_type = !strcmp (name + 6, "2d") || !strcmp (name + 6, "2ul") || !strcmp (name + 6, "2l") || !strcmp (name + 6, "4f") || !strcmp (name + 6, "4ui") || !strcmp (name + 6, "4i") || !strcmp (name + 6, "8s") || !strcmp (name + 6, "8us") || !strcmp (name + 6, "16b") || !strcmp (name + 6, "16sb");
5966 mono_loader_unlock ();
5968 mono_profiler_class_loaded (class, MONO_PROFILE_OK);
5969 mono_loader_assert_no_error ();
5974 mono_class_setup_mono_type (class);
5975 mono_loader_unlock ();
5976 mono_profiler_class_loaded (class, MONO_PROFILE_FAILED);
5977 mono_loader_assert_no_error ();
5981 /** is klass Nullable<T>? */
5983 mono_class_is_nullable (MonoClass *klass)
5985 return klass->generic_class != NULL &&
5986 klass->generic_class->container_class == mono_defaults.generic_nullable_class;
5990 /** if klass is T? return T */
5992 mono_class_get_nullable_param (MonoClass *klass)
5994 g_assert (mono_class_is_nullable (klass));
5995 return mono_class_from_mono_type (klass->generic_class->context.class_inst->type_argv [0]);
5999 mono_generic_class_setup_parent (MonoClass *klass, MonoClass *gtd)
6003 MonoGenericClass *gclass = klass->generic_class;
6005 klass->parent = mono_class_inflate_generic_class_checked (gtd->parent, mono_generic_class_get_context (gclass), &error);
6006 if (!mono_error_ok (&error)) {
6007 /*Set parent to something safe as the runtime doesn't handle well this kind of failure.*/
6008 klass->parent = mono_defaults.object_class;
6009 mono_class_set_failure (klass, MONO_EXCEPTION_TYPE_LOAD, NULL);
6010 mono_error_cleanup (&error);
6014 mono_class_setup_parent (klass, klass->parent);
6016 if (klass->enumtype) {
6017 klass->cast_class = gtd->cast_class;
6018 klass->element_class = gtd->element_class;
6024 * Create the `MonoClass' for an instantiation of a generic type.
6025 * We only do this if we actually need it.
6028 mono_generic_class_get_class (MonoGenericClass *gclass)
6030 MonoClass *klass, *gklass;
6032 if (gclass->cached_class)
6033 return gclass->cached_class;
6035 mono_loader_lock ();
6036 if (gclass->cached_class) {
6037 mono_loader_unlock ();
6038 return gclass->cached_class;
6041 klass = mono_image_set_alloc0 (gclass->owner, sizeof (MonoClass));
6043 gklass = gclass->container_class;
6045 if (record_gclass_instantiation > 0)
6046 gclass_recorded_list = g_slist_append (gclass_recorded_list, klass);
6048 if (gklass->nested_in) {
6049 /* The nested_in type should not be inflated since it's possible to produce a nested type with less generic arguments*/
6050 klass->nested_in = gklass->nested_in;
6053 klass->name = gklass->name;
6054 klass->name_space = gklass->name_space;
6056 mono_profiler_class_event (klass, MONO_PROFILE_START_LOAD);
6058 klass->image = gklass->image;
6059 klass->flags = gklass->flags;
6060 klass->type_token = gklass->type_token;
6061 klass->field.count = gklass->field.count;
6063 klass->is_inflated = 1;
6064 klass->generic_class = gclass;
6066 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
6067 klass->this_arg.data.generic_class = klass->byval_arg.data.generic_class = gclass;
6068 klass->this_arg.byref = TRUE;
6069 klass->enumtype = gklass->enumtype;
6070 klass->valuetype = gklass->valuetype;
6072 klass->cast_class = klass->element_class = klass;
6074 if (mono_class_is_nullable (klass))
6075 klass->cast_class = klass->element_class = mono_class_get_nullable_param (klass);
6078 * We're not interested in the nested classes of a generic instance.
6079 * We use the generic type definition to look for nested classes.
6082 mono_generic_class_setup_parent (klass, gklass);
6084 if (gclass->is_dynamic) {
6086 * We don't need to do any init workf with unbaked typebuilders. Generic instances created at this point will be later unregistered and/or fixed.
6087 * This is to avoid work that would probably give wrong results as fields change as we build the TypeBuilder.
6088 * See remove_instantiations_of_and_ensure_contents in reflection.c and its usage in reflection.c to understand the fixup stage of SRE banking.
6090 if (!gklass->wastypebuilder)
6093 mono_class_setup_supertypes (klass);
6095 if (klass->enumtype) {
6097 * For enums, gklass->fields might not been set, but instance_size etc. is
6098 * already set in mono_reflection_create_internal_class (). For non-enums,
6099 * these will be computed normally in mono_class_layout_fields ().
6101 klass->instance_size = gklass->instance_size;
6102 klass->sizes.class_size = gklass->sizes.class_size;
6103 mono_memory_barrier ();
6104 klass->size_inited = 1;
6108 mono_memory_barrier ();
6109 gclass->cached_class = klass;
6111 mono_profiler_class_loaded (klass, MONO_PROFILE_OK);
6113 inflated_classes ++;
6114 inflated_classes_size += sizeof (MonoClass);
6116 mono_loader_unlock ();
6122 make_generic_param_class (MonoGenericParam *param, MonoImage *image, gboolean is_mvar, MonoGenericParamInfo *pinfo)
6124 MonoClass *klass, **ptr;
6126 MonoGenericContainer *container = mono_generic_param_owner (param);
6130 image = mono_defaults.corlib;
6132 klass = mono_image_alloc0 (image, sizeof (MonoClass));
6133 classes_size += sizeof (MonoClass);
6136 klass->name = pinfo->name;
6138 int n = mono_generic_param_num (param);
6139 klass->name = mono_image_alloc0 (image, 16);
6140 sprintf ((char*)klass->name, "%d", n);
6145 MonoMethod *omethod = container->owner.method;
6146 klass->name_space = (omethod && omethod->klass) ? omethod->klass->name_space : "";
6148 MonoClass *oklass = container->owner.klass;
6149 klass->name_space = oklass ? oklass->name_space : "";
6152 klass->name_space = "";
6155 mono_profiler_class_event (klass, MONO_PROFILE_START_LOAD);
6159 for (ptr = pinfo->constraints; ptr && *ptr; ptr++, count++)
6163 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (pinfo->constraints [0])) {
6164 klass->parent = pinfo->constraints [0];
6166 } else if (pinfo && pinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT)
6167 klass->parent = mono_class_from_name (mono_defaults.corlib, "System", "ValueType");
6169 klass->parent = mono_defaults.object_class;
6172 if (count - pos > 0) {
6173 klass->interface_count = count - pos;
6174 klass->interfaces = mono_image_alloc0 (image, sizeof (MonoClass *) * (count - pos));
6175 klass->interfaces_inited = TRUE;
6176 for (i = pos; i < count; i++)
6177 klass->interfaces [i - pos] = pinfo->constraints [i];
6180 klass->image = image;
6182 klass->inited = TRUE;
6183 klass->cast_class = klass->element_class = klass;
6184 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
6186 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
6187 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
6188 klass->this_arg.byref = TRUE;
6190 /* We don't use type_token for VAR since only classes can use it (not arrays, pointer, VARs, etc) */
6191 klass->sizes.generic_param_token = pinfo ? pinfo->token : 0;
6193 /*Init these fields to sane values*/
6194 klass->min_align = 1;
6196 * This makes sure the the value size of this class is equal to the size of the types the gparam is
6197 * constrained to, the JIT depends on this.
6199 klass->instance_size = sizeof (MonoObject) + mono_type_stack_size_internal (&klass->byval_arg, NULL, TRUE);
6200 mono_memory_barrier ();
6201 klass->size_inited = 1;
6202 klass->setup_fields_called = 1;
6204 mono_class_setup_supertypes (klass);
6206 if (count - pos > 0) {
6207 mono_class_setup_vtable (klass->parent);
6208 if (klass->parent->exception_type)
6209 mono_class_set_failure (klass, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Failed to setup parent interfaces"));
6211 setup_interface_offsets (klass, klass->parent->vtable_size, TRUE);
6217 #define FAST_CACHE_SIZE 16
6220 * LOCKING: Takes the image lock depending on @take_lock.
6223 get_anon_gparam_class (MonoGenericParam *param, gboolean is_mvar, gboolean take_lock)
6225 int n = mono_generic_param_num (param);
6226 MonoImage *image = param->image;
6227 MonoClass *klass = NULL;
6232 if (param->gshared_constraint) {
6233 ht = is_mvar ? image->mvar_cache_constrained : image->var_cache_constrained;
6236 mono_image_lock (image);
6237 klass = g_hash_table_lookup (ht, param);
6239 mono_image_unlock (image);
6244 if (n < FAST_CACHE_SIZE) {
6246 return image->mvar_cache_fast ? image->mvar_cache_fast [n] : NULL;
6248 return image->var_cache_fast ? image->var_cache_fast [n] : NULL;
6250 ht = is_mvar ? image->mvar_cache_slow : image->var_cache_slow;
6253 mono_image_lock (image);
6254 klass = g_hash_table_lookup (ht, GINT_TO_POINTER (n));
6256 mono_image_unlock (image);
6263 * LOCKING: Image lock (param->image) must be held
6266 set_anon_gparam_class (MonoGenericParam *param, gboolean is_mvar, MonoClass *klass)
6268 int n = mono_generic_param_num (param);
6269 MonoImage *image = param->image;
6273 if (param->gshared_constraint) {
6274 GHashTable *ht = is_mvar ? image->mvar_cache_constrained : image->var_cache_constrained;
6276 ht = g_hash_table_new ((GHashFunc)mono_metadata_generic_param_hash, (GEqualFunc)mono_metadata_generic_param_equal);
6277 mono_memory_barrier ();
6279 image->mvar_cache_constrained = ht;
6281 image->var_cache_constrained = ht;
6283 g_hash_table_insert (ht, param, klass);
6284 } else if (n < FAST_CACHE_SIZE) {
6286 /* Requires locking to avoid droping an already published class */
6287 if (!image->mvar_cache_fast)
6288 image->mvar_cache_fast = mono_image_alloc0 (image, sizeof (MonoClass*) * FAST_CACHE_SIZE);
6289 image->mvar_cache_fast [n] = klass;
6291 if (!image->var_cache_fast)
6292 image->var_cache_fast = mono_image_alloc0 (image, sizeof (MonoClass*) * FAST_CACHE_SIZE);
6293 image->var_cache_fast [n] = klass;
6296 GHashTable *ht = is_mvar ? image->mvar_cache_slow : image->var_cache_slow;
6298 ht = is_mvar ? image->mvar_cache_slow : image->var_cache_slow;
6300 ht = g_hash_table_new (NULL, NULL);
6301 mono_memory_barrier ();
6303 image->mvar_cache_slow = ht;
6305 image->var_cache_slow = ht;
6308 g_hash_table_insert (ht, GINT_TO_POINTER (n), klass);
6313 * LOCKING: Acquires the image lock (@image).
6316 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
6318 MonoGenericContainer *container = mono_generic_param_owner (param);
6319 MonoGenericParamInfo *pinfo = NULL;
6320 MonoClass *klass, *klass2;
6323 pinfo = mono_generic_param_info (param);
6324 klass = pinfo->pklass;
6327 klass = get_anon_gparam_class (param, is_mvar, TRUE);
6332 if (!image && container) {
6334 MonoMethod *method = container->owner.method;
6335 image = (method && method->klass) ? method->klass->image : NULL;
6337 MonoClass *klass = container->owner.klass;
6338 // FIXME: 'klass' should not be null
6339 // But, monodis creates GenericContainers without associating a owner to it
6340 image = klass ? klass->image : NULL;
6344 klass = make_generic_param_class (param, image, is_mvar, pinfo);
6346 mono_memory_barrier ();
6348 if (!image) //FIXME is this only needed by monodis? Can't we fix monodis instead of having this hack?
6349 image = mono_defaults.corlib;
6351 mono_image_lock (image);
6353 klass2 = pinfo->pklass;
6355 klass2 = get_anon_gparam_class (param, is_mvar, FALSE);
6361 pinfo->pklass = klass;
6363 set_anon_gparam_class (param, is_mvar, klass);
6365 mono_image_unlock (image);
6367 /* FIXME: Should this go inside 'make_generic_param_klass'? */
6369 mono_profiler_class_loaded (klass2, MONO_PROFILE_FAILED);
6371 mono_profiler_class_loaded (klass, MONO_PROFILE_OK);
6377 mono_ptr_class_get (MonoType *type)
6380 MonoClass *el_class;
6384 el_class = mono_class_from_mono_type (type);
6385 image = el_class->image;
6387 mono_image_lock (image);
6388 if (image->ptr_cache) {
6389 if ((result = g_hash_table_lookup (image->ptr_cache, el_class))) {
6390 mono_image_unlock (image);
6394 mono_image_unlock (image);
6396 result = mono_image_alloc0 (image, sizeof (MonoClass));
6398 classes_size += sizeof (MonoClass);
6400 result->parent = NULL; /* no parent for PTR types */
6401 result->name_space = el_class->name_space;
6402 name = g_strdup_printf ("%s*", el_class->name);
6403 result->name = mono_image_strdup (image, name);
6406 mono_profiler_class_event (result, MONO_PROFILE_START_LOAD);
6408 result->image = el_class->image;
6409 result->inited = TRUE;
6410 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
6411 /* Can pointers get boxed? */
6412 result->instance_size = sizeof (gpointer);
6413 result->cast_class = result->element_class = el_class;
6414 result->blittable = TRUE;
6416 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
6417 result->this_arg.data.type = result->byval_arg.data.type = &result->element_class->byval_arg;
6418 result->this_arg.byref = TRUE;
6420 mono_class_setup_supertypes (result);
6422 mono_image_lock (image);
6423 if (image->ptr_cache) {
6425 if ((result2 = g_hash_table_lookup (image->ptr_cache, el_class))) {
6426 mono_image_unlock (image);
6427 mono_profiler_class_loaded (result, MONO_PROFILE_FAILED);
6431 image->ptr_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
6433 g_hash_table_insert (image->ptr_cache, el_class, result);
6434 mono_image_unlock (image);
6436 mono_profiler_class_loaded (result, MONO_PROFILE_OK);
6442 mono_fnptr_class_get (MonoMethodSignature *sig)
6445 static GHashTable *ptr_hash = NULL;
6447 /* FIXME: These should be allocate from a mempool as well, but which one ? */
6449 mono_loader_lock ();
6452 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
6454 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
6455 mono_loader_unlock ();
6458 result = g_new0 (MonoClass, 1);
6460 result->parent = NULL; /* no parent for PTR types */
6461 result->name_space = "System";
6462 result->name = "MonoFNPtrFakeClass";
6464 mono_profiler_class_event (result, MONO_PROFILE_START_LOAD);
6466 result->image = mono_defaults.corlib; /* need to fix... */
6467 result->inited = TRUE;
6468 result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
6469 /* Can pointers get boxed? */
6470 result->instance_size = sizeof (gpointer);
6471 result->cast_class = result->element_class = result;
6472 result->blittable = TRUE;
6474 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
6475 result->this_arg.data.method = result->byval_arg.data.method = sig;
6476 result->this_arg.byref = TRUE;
6477 result->blittable = TRUE;
6479 mono_class_setup_supertypes (result);
6481 g_hash_table_insert (ptr_hash, sig, result);
6483 mono_loader_unlock ();
6485 mono_profiler_class_loaded (result, MONO_PROFILE_OK);
6491 mono_class_from_mono_type (MonoType *type)
6493 switch (type->type) {
6494 case MONO_TYPE_OBJECT:
6495 return type->data.klass? type->data.klass: mono_defaults.object_class;
6496 case MONO_TYPE_VOID:
6497 return type->data.klass? type->data.klass: mono_defaults.void_class;
6498 case MONO_TYPE_BOOLEAN:
6499 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
6500 case MONO_TYPE_CHAR:
6501 return type->data.klass? type->data.klass: mono_defaults.char_class;
6503 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
6505 return type->data.klass? type->data.klass: mono_defaults.byte_class;
6507 return type->data.klass? type->data.klass: mono_defaults.int16_class;
6509 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
6511 return type->data.klass? type->data.klass: mono_defaults.int32_class;
6513 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
6515 return type->data.klass? type->data.klass: mono_defaults.int_class;
6517 return type->data.klass? type->data.klass: mono_defaults.uint_class;
6519 return type->data.klass? type->data.klass: mono_defaults.int64_class;
6521 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
6523 return type->data.klass? type->data.klass: mono_defaults.single_class;
6525 return type->data.klass? type->data.klass: mono_defaults.double_class;
6526 case MONO_TYPE_STRING:
6527 return type->data.klass? type->data.klass: mono_defaults.string_class;
6528 case MONO_TYPE_TYPEDBYREF:
6529 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
6530 case MONO_TYPE_ARRAY:
6531 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
6533 return mono_ptr_class_get (type->data.type);
6534 case MONO_TYPE_FNPTR:
6535 return mono_fnptr_class_get (type->data.method);
6536 case MONO_TYPE_SZARRAY:
6537 return mono_array_class_get (type->data.klass, 1);
6538 case MONO_TYPE_CLASS:
6539 case MONO_TYPE_VALUETYPE:
6540 return type->data.klass;
6541 case MONO_TYPE_GENERICINST:
6542 return mono_generic_class_get_class (type->data.generic_class);
6544 return mono_class_from_generic_parameter (type->data.generic_param, NULL, FALSE);
6545 case MONO_TYPE_MVAR:
6546 return mono_class_from_generic_parameter (type->data.generic_param, NULL, TRUE);
6548 g_warning ("mono_class_from_mono_type: implement me 0x%02x\n", type->type);
6549 g_assert_not_reached ();
6556 * mono_type_retrieve_from_typespec
6557 * @image: context where the image is created
6558 * @type_spec: typespec token
6559 * @context: the generic context used to evaluate generic instantiations in
6562 mono_type_retrieve_from_typespec (MonoImage *image, guint32 type_spec, MonoGenericContext *context, gboolean *did_inflate, MonoError *error)
6564 MonoType *t = mono_type_create_from_typespec_checked (image, type_spec, error);
6566 *did_inflate = FALSE;
6571 if (context && (context->class_inst || context->method_inst)) {
6572 MonoType *inflated = inflate_generic_type (NULL, t, context, error);
6574 if (!mono_error_ok (error)) {
6575 mono_loader_assert_no_error ();
6581 *did_inflate = TRUE;
6588 * mono_class_create_from_typespec
6589 * @image: context where the image is created
6590 * @type_spec: typespec token
6591 * @context: the generic context used to evaluate generic instantiations in
6594 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec, MonoGenericContext *context, MonoError *error)
6597 gboolean inflated = FALSE;
6598 MonoType *t = mono_type_retrieve_from_typespec (image, type_spec, context, &inflated, error);
6599 if (!mono_error_ok (error))
6601 ret = mono_class_from_mono_type (t);
6603 mono_metadata_free_type (t);
6608 * mono_bounded_array_class_get:
6609 * @element_class: element class
6610 * @rank: the dimension of the array class
6611 * @bounded: whenever the array has non-zero bounds
6613 * Returns: a class object describing the array with element type @element_type and
6617 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
6621 MonoClass *parent = NULL;
6622 GSList *list, *rootlist = NULL;
6625 gboolean corlib_type = FALSE;
6627 g_assert (rank <= 255);
6630 /* bounded only matters for one-dimensional arrays */
6633 image = eclass->image;
6635 if (rank == 1 && !bounded) {
6637 * This case is very frequent not just during compilation because of calls
6638 * from mono_class_from_mono_type (), mono_array_new (),
6639 * Array:CreateInstance (), etc, so use a separate cache + a separate lock.
6641 mono_mutex_lock (&image->szarray_cache_lock);
6642 if (!image->szarray_cache)
6643 image->szarray_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
6644 class = g_hash_table_lookup (image->szarray_cache, eclass);
6645 mono_mutex_unlock (&image->szarray_cache_lock);
6649 mono_loader_lock ();
6651 mono_loader_lock ();
6653 if (!image->array_cache)
6654 image->array_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
6656 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
6657 for (; list; list = list->next) {
6659 if ((class->rank == rank) && (class->byval_arg.type == (((rank > 1) || bounded) ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
6660 mono_loader_unlock ();
6667 /* for the building corlib use System.Array from it */
6668 if (image->assembly && assembly_is_dynamic (image->assembly) && image->assembly_name && strcmp (image->assembly_name, "mscorlib") == 0) {
6669 parent = mono_class_from_name (image, "System", "Array");
6672 parent = mono_defaults.array_class;
6673 if (!parent->inited)
6674 mono_class_init (parent);
6677 class = mono_image_alloc0 (image, sizeof (MonoClass));
6679 class->image = image;
6680 class->name_space = eclass->name_space;
6681 nsize = strlen (eclass->name);
6682 name = g_malloc (nsize + 2 + rank + 1);
6683 memcpy (name, eclass->name, nsize);
6686 memset (name + nsize + 1, ',', rank - 1);
6688 name [nsize + rank] = '*';
6689 name [nsize + rank + bounded] = ']';
6690 name [nsize + rank + bounded + 1] = 0;
6691 class->name = mono_image_strdup (image, name);
6694 mono_profiler_class_event (class, MONO_PROFILE_START_LOAD);
6696 classes_size += sizeof (MonoClass);
6698 class->type_token = 0;
6699 /* all arrays are marked serializable and sealed, bug #42779 */
6700 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED | TYPE_ATTRIBUTE_PUBLIC;
6701 class->parent = parent;
6702 class->instance_size = mono_class_instance_size (class->parent);
6704 if (eclass->byval_arg.type == MONO_TYPE_TYPEDBYREF || eclass->byval_arg.type == MONO_TYPE_VOID) {
6705 /*Arrays of those two types are invalid.*/
6706 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
6707 } else if (eclass->enumtype && !mono_class_enum_basetype (eclass)) {
6708 if (!eclass->ref_info_handle || eclass->wastypebuilder) {
6709 g_warning ("Only incomplete TypeBuilder objects are allowed to be an enum without base_type");
6710 g_assert (eclass->ref_info_handle && !eclass->wastypebuilder);
6712 /* element_size -1 is ok as this is not an instantitable type*/
6713 class->sizes.element_size = -1;
6715 class->sizes.element_size = mono_class_array_element_size (eclass);
6717 mono_class_setup_supertypes (class);
6719 if (eclass->generic_class)
6720 mono_class_init (eclass);
6721 if (!eclass->size_inited)
6722 mono_class_setup_fields (eclass);
6723 if (eclass->exception_type) /*FIXME we fail the array type, but we have to let other fields be set.*/
6724 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
6726 class->has_references = MONO_TYPE_IS_REFERENCE (&eclass->byval_arg) || eclass->has_references? TRUE: FALSE;
6730 if (eclass->enumtype)
6731 class->cast_class = eclass->element_class;
6733 class->cast_class = eclass;
6735 switch (class->cast_class->byval_arg.type) {
6737 class->cast_class = mono_defaults.byte_class;
6740 class->cast_class = mono_defaults.int16_class;
6743 #if SIZEOF_VOID_P == 4
6747 class->cast_class = mono_defaults.int32_class;
6750 #if SIZEOF_VOID_P == 8
6754 class->cast_class = mono_defaults.int64_class;
6760 class->element_class = eclass;
6762 if ((rank > 1) || bounded) {
6763 MonoArrayType *at = mono_image_alloc0 (image, sizeof (MonoArrayType));
6764 class->byval_arg.type = MONO_TYPE_ARRAY;
6765 class->byval_arg.data.array = at;
6766 at->eklass = eclass;
6768 /* FIXME: complete.... */
6770 class->byval_arg.type = MONO_TYPE_SZARRAY;
6771 class->byval_arg.data.klass = eclass;
6773 class->this_arg = class->byval_arg;
6774 class->this_arg.byref = 1;
6779 class->generic_container = eclass->generic_container;
6781 if (rank == 1 && !bounded) {
6782 MonoClass *prev_class;
6784 mono_mutex_lock (&image->szarray_cache_lock);
6785 prev_class = g_hash_table_lookup (image->szarray_cache, eclass);
6787 /* Someone got in before us */
6790 g_hash_table_insert (image->szarray_cache, eclass, class);
6791 mono_mutex_unlock (&image->szarray_cache_lock);
6793 list = g_slist_append (rootlist, class);
6794 g_hash_table_insert (image->array_cache, eclass, list);
6797 mono_loader_unlock ();
6799 mono_profiler_class_loaded (class, MONO_PROFILE_OK);
6805 * mono_array_class_get:
6806 * @element_class: element class
6807 * @rank: the dimension of the array class
6809 * Returns: a class object describing the array with element type @element_type and
6813 mono_array_class_get (MonoClass *eclass, guint32 rank)
6815 return mono_bounded_array_class_get (eclass, rank, FALSE);
6819 * mono_class_instance_size:
6822 * Returns: the size of an object instance
6825 mono_class_instance_size (MonoClass *klass)
6827 if (!klass->size_inited)
6828 mono_class_init (klass);
6830 return klass->instance_size;
6834 * mono_class_min_align:
6837 * Returns: minimm alignment requirements
6840 mono_class_min_align (MonoClass *klass)
6842 if (!klass->size_inited)
6843 mono_class_init (klass);
6845 return klass->min_align;
6849 * mono_class_value_size:
6852 * This function is used for value types, and return the
6853 * space and the alignment to store that kind of value object.
6855 * Returns: the size of a value of kind @klass
6858 mono_class_value_size (MonoClass *klass, guint32 *align)
6862 /* fixme: check disable, because we still have external revereces to
6863 * mscorlib and Dummy Objects
6865 /*g_assert (klass->valuetype);*/
6867 size = mono_class_instance_size (klass) - sizeof (MonoObject);
6870 *align = klass->min_align;
6876 * mono_class_data_size:
6879 * Returns: the size of the static class data
6882 mono_class_data_size (MonoClass *klass)
6885 mono_class_init (klass);
6886 /* This can happen with dynamically created types */
6887 if (!klass->fields_inited)
6888 mono_class_setup_fields_locking (klass);
6890 /* in arrays, sizes.class_size is unioned with element_size
6891 * and arrays have no static fields
6895 return klass->sizes.class_size;
6899 * Auxiliary routine to mono_class_get_field
6901 * Takes a field index instead of a field token.
6903 static MonoClassField *
6904 mono_class_get_field_idx (MonoClass *class, int idx)
6906 mono_class_setup_fields_locking (class);
6907 if (class->exception_type)
6911 if (class->image->uncompressed_metadata) {
6913 * class->field.first points to the FieldPtr table, while idx points into the
6914 * Field table, so we have to do a search.
6916 /*FIXME this is broken for types with multiple fields with the same name.*/
6917 const char *name = mono_metadata_string_heap (class->image, mono_metadata_decode_row_col (&class->image->tables [MONO_TABLE_FIELD], idx, MONO_FIELD_NAME));
6920 for (i = 0; i < class->field.count; ++i)
6921 if (mono_field_get_name (&class->fields [i]) == name)
6922 return &class->fields [i];
6923 g_assert_not_reached ();
6925 if (class->field.count) {
6926 if ((idx >= class->field.first) && (idx < class->field.first + class->field.count)){
6927 return &class->fields [idx - class->field.first];
6931 class = class->parent;
6937 * mono_class_get_field:
6938 * @class: the class to lookup the field.
6939 * @field_token: the field token
6941 * Returns: A MonoClassField representing the type and offset of
6942 * the field, or a NULL value if the field does not belong to this
6946 mono_class_get_field (MonoClass *class, guint32 field_token)
6948 int idx = mono_metadata_token_index (field_token);
6950 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
6952 return mono_class_get_field_idx (class, idx - 1);
6956 * mono_class_get_field_from_name:
6957 * @klass: the class to lookup the field.
6958 * @name: the field name
6960 * Search the class @klass and it's parents for a field with the name @name.
6962 * Returns: the MonoClassField pointer of the named field or NULL
6965 mono_class_get_field_from_name (MonoClass *klass, const char *name)
6967 return mono_class_get_field_from_name_full (klass, name, NULL);
6971 * mono_class_get_field_from_name_full:
6972 * @klass: the class to lookup the field.
6973 * @name: the field name
6974 * @type: the type of the fields. This optional.
6976 * Search the class @klass and it's parents for a field with the name @name and type @type.
6978 * If @klass is an inflated generic type, the type comparison is done with the equivalent field
6979 * of its generic type definition.
6981 * Returns: the MonoClassField pointer of the named field or NULL
6984 mono_class_get_field_from_name_full (MonoClass *klass, const char *name, MonoType *type)
6988 mono_class_setup_fields_locking (klass);
6989 if (klass->exception_type)
6993 for (i = 0; i < klass->field.count; ++i) {
6994 MonoClassField *field = &klass->fields [i];
6996 if (strcmp (name, mono_field_get_name (field)) != 0)
7000 MonoType *field_type = mono_metadata_get_corresponding_field_from_generic_type_definition (field)->type;
7001 if (!mono_metadata_type_equal_full (type, field_type, TRUE))
7006 klass = klass->parent;
7012 * mono_class_get_field_token:
7013 * @field: the field we need the token of
7015 * Get the token of a field. Note that the tokesn is only valid for the image
7016 * the field was loaded from. Don't use this function for fields in dynamic types.
7018 * Returns: the token representing the field in the image it was loaded from.
7021 mono_class_get_field_token (MonoClassField *field)
7023 MonoClass *klass = field->parent;
7026 mono_class_setup_fields_locking (klass);
7031 for (i = 0; i < klass->field.count; ++i) {
7032 if (&klass->fields [i] == field) {
7033 int idx = klass->field.first + i + 1;
7035 if (klass->image->uncompressed_metadata)
7036 idx = mono_metadata_translate_token_index (klass->image, MONO_TABLE_FIELD, idx);
7037 return mono_metadata_make_token (MONO_TABLE_FIELD, idx);
7040 klass = klass->parent;
7043 g_assert_not_reached ();
7048 mono_field_get_index (MonoClassField *field)
7050 int index = field - field->parent->fields;
7052 g_assert (index >= 0 && index < field->parent->field.count);
7058 * mono_class_get_field_default_value:
7060 * Return the default value of the field as a pointer into the metadata blob.
7063 mono_class_get_field_default_value (MonoClassField *field, MonoTypeEnum *def_type)
7066 guint32 constant_cols [MONO_CONSTANT_SIZE];
7068 MonoClass *klass = field->parent;
7070 g_assert (field->type->attrs & FIELD_ATTRIBUTE_HAS_DEFAULT);
7072 if (!klass->ext || !klass->ext->field_def_values) {
7073 MonoFieldDefaultValue *def_values;
7075 mono_class_alloc_ext (klass);
7077 def_values = mono_class_alloc0 (klass, sizeof (MonoFieldDefaultValue) * klass->field.count);
7079 mono_image_lock (klass->image);
7080 mono_memory_barrier ();
7081 if (!klass->ext->field_def_values)
7082 klass->ext->field_def_values = def_values;
7083 mono_image_unlock (klass->image);
7086 field_index = mono_field_get_index (field);
7088 if (!klass->ext->field_def_values [field_index].data) {
7089 cindex = mono_metadata_get_constant_index (field->parent->image, mono_class_get_field_token (field), 0);
7093 g_assert (!(field->type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_RVA));
7095 mono_metadata_decode_row (&field->parent->image->tables [MONO_TABLE_CONSTANT], cindex - 1, constant_cols, MONO_CONSTANT_SIZE);
7096 klass->ext->field_def_values [field_index].def_type = constant_cols [MONO_CONSTANT_TYPE];
7097 klass->ext->field_def_values [field_index].data = (gpointer)mono_metadata_blob_heap (field->parent->image, constant_cols [MONO_CONSTANT_VALUE]);
7100 *def_type = klass->ext->field_def_values [field_index].def_type;
7101 return klass->ext->field_def_values [field_index].data;
7105 mono_property_get_index (MonoProperty *prop)
7107 int index = prop - prop->parent->ext->properties;
7109 g_assert (index >= 0 && index < prop->parent->ext->property.count);
7115 * mono_class_get_property_default_value:
7117 * Return the default value of the field as a pointer into the metadata blob.
7120 mono_class_get_property_default_value (MonoProperty *property, MonoTypeEnum *def_type)
7123 guint32 constant_cols [MONO_CONSTANT_SIZE];
7124 MonoClass *klass = property->parent;
7126 g_assert (property->attrs & PROPERTY_ATTRIBUTE_HAS_DEFAULT);
7128 * We don't cache here because it is not used by C# so it's quite rare, but
7129 * we still do the lookup in klass->ext because that is where the data
7130 * is stored for dynamic assemblies.
7133 if (image_is_dynamic (klass->image)) {
7134 int prop_index = mono_property_get_index (property);
7135 if (klass->ext->prop_def_values && klass->ext->prop_def_values [prop_index].data) {
7136 *def_type = klass->ext->prop_def_values [prop_index].def_type;
7137 return klass->ext->prop_def_values [prop_index].data;
7141 cindex = mono_metadata_get_constant_index (klass->image, mono_class_get_property_token (property), 0);
7145 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_CONSTANT], cindex - 1, constant_cols, MONO_CONSTANT_SIZE);
7146 *def_type = constant_cols [MONO_CONSTANT_TYPE];
7147 return (gpointer)mono_metadata_blob_heap (klass->image, constant_cols [MONO_CONSTANT_VALUE]);
7151 mono_class_get_event_token (MonoEvent *event)
7153 MonoClass *klass = event->parent;
7158 for (i = 0; i < klass->ext->event.count; ++i) {
7159 if (&klass->ext->events [i] == event)
7160 return mono_metadata_make_token (MONO_TABLE_EVENT, klass->ext->event.first + i + 1);
7163 klass = klass->parent;
7166 g_assert_not_reached ();
7171 mono_class_get_property_from_name (MonoClass *klass, const char *name)
7175 gpointer iter = NULL;
7176 while ((p = mono_class_get_properties (klass, &iter))) {
7177 if (! strcmp (name, p->name))
7180 klass = klass->parent;
7186 mono_class_get_property_token (MonoProperty *prop)
7188 MonoClass *klass = prop->parent;
7192 gpointer iter = NULL;
7193 while ((p = mono_class_get_properties (klass, &iter))) {
7194 if (&klass->ext->properties [i] == prop)
7195 return mono_metadata_make_token (MONO_TABLE_PROPERTY, klass->ext->property.first + i + 1);
7199 klass = klass->parent;
7202 g_assert_not_reached ();
7207 mono_class_name_from_token (MonoImage *image, guint32 type_token)
7209 const char *name, *nspace;
7210 if (image_is_dynamic (image))
7211 return g_strdup_printf ("DynamicType 0x%08x", type_token);
7213 switch (type_token & 0xff000000){
7214 case MONO_TOKEN_TYPE_DEF: {
7215 guint32 cols [MONO_TYPEDEF_SIZE];
7216 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
7217 guint tidx = mono_metadata_token_index (type_token);
7219 if (tidx > tt->rows)
7220 return g_strdup_printf ("Invalid type token 0x%08x", type_token);
7222 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
7223 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
7224 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
7225 if (strlen (nspace) == 0)
7226 return g_strdup_printf ("%s", name);
7228 return g_strdup_printf ("%s.%s", nspace, name);
7231 case MONO_TOKEN_TYPE_REF: {
7233 guint32 cols [MONO_TYPEREF_SIZE];
7234 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
7235 guint tidx = mono_metadata_token_index (type_token);
7238 return g_strdup_printf ("Invalid type token 0x%08x", type_token);
7240 if (!mono_verifier_verify_typeref_row (image, tidx - 1, &error)) {
7241 char *msg = g_strdup_printf ("Invalid type token 0x%08x due to '%s'", type_token, mono_error_get_message (&error));
7242 mono_error_cleanup (&error);
7246 mono_metadata_decode_row (t, tidx-1, cols, MONO_TYPEREF_SIZE);
7247 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
7248 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
7249 if (strlen (nspace) == 0)
7250 return g_strdup_printf ("%s", name);
7252 return g_strdup_printf ("%s.%s", nspace, name);
7255 case MONO_TOKEN_TYPE_SPEC:
7256 return g_strdup_printf ("Typespec 0x%08x", type_token);
7258 return g_strdup_printf ("Invalid type token 0x%08x", type_token);
7263 mono_assembly_name_from_token (MonoImage *image, guint32 type_token)
7265 if (image_is_dynamic (image))
7266 return g_strdup_printf ("DynamicAssembly %s", image->name);
7268 switch (type_token & 0xff000000){
7269 case MONO_TOKEN_TYPE_DEF:
7270 if (image->assembly)
7271 return mono_stringify_assembly_name (&image->assembly->aname);
7272 else if (image->assembly_name)
7273 return g_strdup (image->assembly_name);
7274 return g_strdup_printf ("%s", image->name ? image->name : "[Could not resolve assembly name");
7275 case MONO_TOKEN_TYPE_REF: {
7277 MonoAssemblyName aname;
7278 guint32 cols [MONO_TYPEREF_SIZE];
7279 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
7280 guint32 idx = mono_metadata_token_index (type_token);
7283 return g_strdup_printf ("Invalid type token 0x%08x", type_token);
7285 if (!mono_verifier_verify_typeref_row (image, idx - 1, &error)) {
7286 char *msg = g_strdup_printf ("Invalid type token 0x%08x due to '%s'", type_token, mono_error_get_message (&error));
7287 mono_error_cleanup (&error);
7290 mono_metadata_decode_row (t, idx-1, cols, MONO_TYPEREF_SIZE);
7292 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLUTION_SCOPE_BITS;
7293 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLUTION_SCOPE_MASK) {
7294 case MONO_RESOLUTION_SCOPE_MODULE:
7296 return g_strdup ("");
7297 case MONO_RESOLUTION_SCOPE_MODULEREF:
7299 return g_strdup ("");
7300 case MONO_RESOLUTION_SCOPE_TYPEREF:
7302 return g_strdup ("");
7303 case MONO_RESOLUTION_SCOPE_ASSEMBLYREF:
7304 mono_assembly_get_assemblyref (image, idx - 1, &aname);
7305 return mono_stringify_assembly_name (&aname);
7307 g_assert_not_reached ();
7311 case MONO_TOKEN_TYPE_SPEC:
7313 return g_strdup ("");
7315 g_assert_not_reached ();
7322 * mono_class_get_full:
7323 * @image: the image where the class resides
7324 * @type_token: the token for the class
7325 * @context: the generic context used to evaluate generic instantiations in
7326 * @deprecated: Functions that expose MonoGenericContext are going away in mono 4.0
7328 * Returns: the MonoClass that represents @type_token in @image
7331 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
7335 class = mono_class_get_checked (image, type_token, &error);
7337 if (class && context && mono_metadata_token_table (type_token) == MONO_TABLE_TYPESPEC)
7338 class = mono_class_inflate_generic_class_checked (class, context, &error);
7340 g_assert (mono_error_ok (&error)); /* FIXME deprecate this function and forbit the runtime from using it. */
7346 mono_class_get_and_inflate_typespec_checked (MonoImage *image, guint32 type_token, MonoGenericContext *context, MonoError *error)
7350 mono_error_init (error);
7351 class = mono_class_get_checked (image, type_token, error);
7353 if (class && context && mono_metadata_token_table (type_token) == MONO_TABLE_TYPESPEC)
7354 class = mono_class_inflate_generic_class_checked (class, context, error);
7359 * mono_class_get_checked:
7360 * @image: the image where the class resides
7361 * @type_token: the token for the class
7362 * @error: error object to return any error
7364 * Returns: the MonoClass that represents @type_token in @image
7367 mono_class_get_checked (MonoImage *image, guint32 type_token, MonoError *error)
7369 MonoClass *class = NULL;
7371 mono_error_init (error);
7373 if (image_is_dynamic (image)) {
7374 int table = mono_metadata_token_table (type_token);
7376 if (table != MONO_TABLE_TYPEDEF && table != MONO_TABLE_TYPEREF && table != MONO_TABLE_TYPESPEC) {
7377 mono_error_set_bad_image (error, image,"Bad token table for dynamic image: %x", table);
7380 class = mono_lookup_dynamic_token (image, type_token, NULL); /*FIXME proper error handling*/
7384 switch (type_token & 0xff000000){
7385 case MONO_TOKEN_TYPE_DEF:
7386 class = mono_class_create_from_typedef (image, type_token, error);
7388 case MONO_TOKEN_TYPE_REF:
7389 class = mono_class_from_typeref_checked (image, type_token, error);
7391 case MONO_TOKEN_TYPE_SPEC:
7392 class = mono_class_create_from_typespec (image, type_token, NULL, error);
7395 mono_error_set_bad_image (error, image, "Unknown type token %x", type_token & 0xff000000);
7399 /* Generic case, should be avoided for when a better error is possible. */
7400 if (!class && mono_error_ok (error)) {
7401 char *name = mono_class_name_from_token (image, type_token);
7402 char *assembly = mono_assembly_name_from_token (image, type_token);
7403 mono_error_set_type_load_name (error, name, assembly, "Could not resolve type with token %08x", type_token);
7411 * mono_type_get_checked:
7412 * @image: the image where the type resides
7413 * @type_token: the token for the type
7414 * @context: the generic context used to evaluate generic instantiations in
7415 * @error: Error handling context
7417 * This functions exists to fullfill the fact that sometimes it's desirable to have access to the
7419 * Returns: the MonoType that represents @type_token in @image
7422 mono_type_get_checked (MonoImage *image, guint32 type_token, MonoGenericContext *context, MonoError *error)
7424 MonoType *type = NULL;
7425 gboolean inflated = FALSE;
7427 mono_error_init (error);
7429 //FIXME: this will not fix the very issue for which mono_type_get_full exists -but how to do it then?
7430 if (image_is_dynamic (image))
7431 return mono_class_get_type (mono_lookup_dynamic_token (image, type_token, context));
7433 if ((type_token & 0xff000000) != MONO_TOKEN_TYPE_SPEC) {
7434 MonoClass *class = mono_class_get_checked (image, type_token, error);
7437 mono_loader_assert_no_error ();
7442 return mono_class_get_type (class);
7445 type = mono_type_retrieve_from_typespec (image, type_token, context, &inflated, error);
7448 mono_loader_assert_no_error ();
7453 MonoType *tmp = type;
7454 type = mono_class_get_type (mono_class_from_mono_type (type));
7455 /* FIXME: This is a workaround fo the fact that a typespec token sometimes reference to the generic type definition.
7456 * A MonoClass::byval_arg of a generic type definion has type CLASS.
7457 * Some parts of mono create a GENERICINST to reference a generic type definition and this generates confict with byval_arg.
7459 * The long term solution is to chaise this places and make then set MonoType::type correctly.
7461 if (type->type != tmp->type)
7464 mono_metadata_free_type (tmp);
7471 mono_class_get (MonoImage *image, guint32 type_token)
7473 return mono_class_get_full (image, type_token, NULL);
7477 * mono_image_init_name_cache:
7479 * Initializes the class name cache stored in image->name_cache.
7481 * LOCKING: Acquires the corresponding image lock.
7484 mono_image_init_name_cache (MonoImage *image)
7486 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
7487 guint32 cols [MONO_TYPEDEF_SIZE];
7490 guint32 i, visib, nspace_index;
7491 GHashTable *name_cache2, *nspace_table, *the_name_cache;
7493 if (image->name_cache)
7496 the_name_cache = g_hash_table_new (g_str_hash, g_str_equal);
7498 if (image_is_dynamic (image)) {
7499 mono_image_lock (image);
7500 if (image->name_cache) {
7501 /* Somebody initialized it before us */
7502 g_hash_table_destroy (the_name_cache);
7504 mono_atomic_store_release (&image->name_cache, the_name_cache);
7506 mono_image_unlock (image);
7510 /* Temporary hash table to avoid lookups in the nspace_table */
7511 name_cache2 = g_hash_table_new (NULL, NULL);
7513 for (i = 1; i <= t->rows; ++i) {
7514 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
7515 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
7517 * Nested types are accessed from the nesting name. We use the fact that nested types use different visibility flags
7518 * than toplevel types, thus avoiding the need to grovel through the NESTED_TYPE table
7520 if (visib >= TYPE_ATTRIBUTE_NESTED_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM)
7522 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
7523 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
7525 nspace_index = cols [MONO_TYPEDEF_NAMESPACE];
7526 nspace_table = g_hash_table_lookup (name_cache2, GUINT_TO_POINTER (nspace_index));
7527 if (!nspace_table) {
7528 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
7529 g_hash_table_insert (the_name_cache, (char*)nspace, nspace_table);
7530 g_hash_table_insert (name_cache2, GUINT_TO_POINTER (nspace_index),
7533 g_hash_table_insert (nspace_table, (char *) name, GUINT_TO_POINTER (i));
7536 /* Load type names from EXPORTEDTYPES table */
7538 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
7539 guint32 cols [MONO_EXP_TYPE_SIZE];
7542 for (i = 0; i < t->rows; ++i) {
7543 mono_metadata_decode_row (t, i, cols, MONO_EXP_TYPE_SIZE);
7544 name = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAME]);
7545 nspace = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAMESPACE]);
7547 nspace_index = cols [MONO_EXP_TYPE_NAMESPACE];
7548 nspace_table = g_hash_table_lookup (name_cache2, GUINT_TO_POINTER (nspace_index));
7549 if (!nspace_table) {
7550 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
7551 g_hash_table_insert (the_name_cache, (char*)nspace, nspace_table);
7552 g_hash_table_insert (name_cache2, GUINT_TO_POINTER (nspace_index),
7555 g_hash_table_insert (nspace_table, (char *) name, GUINT_TO_POINTER (mono_metadata_make_token (MONO_TABLE_EXPORTEDTYPE, i + 1)));
7559 g_hash_table_destroy (name_cache2);
7561 mono_image_lock (image);
7562 if (image->name_cache) {
7563 /* Somebody initialized it before us */
7564 g_hash_table_destroy (the_name_cache);
7566 mono_atomic_store_release (&image->name_cache, the_name_cache);
7568 mono_image_unlock (image);
7571 /*FIXME Only dynamic assemblies should allow this operation.*/
7573 mono_image_add_to_name_cache (MonoImage *image, const char *nspace,
7574 const char *name, guint32 index)
7576 GHashTable *nspace_table;
7577 GHashTable *name_cache;
7580 mono_image_init_name_cache (image);
7581 mono_image_lock (image);
7583 name_cache = image->name_cache;
7584 if (!(nspace_table = g_hash_table_lookup (name_cache, nspace))) {
7585 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
7586 g_hash_table_insert (name_cache, (char *)nspace, (char *)nspace_table);
7589 if ((old_index = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, (char*) name))))
7590 g_error ("overrwritting old token %x on image %s for type %s::%s", old_index, image->name, nspace, name);
7592 g_hash_table_insert (nspace_table, (char *) name, GUINT_TO_POINTER (index));
7594 mono_image_unlock (image);
7603 find_nocase (gpointer key, gpointer value, gpointer user_data)
7605 char *name = (char*)key;
7606 FindUserData *data = (FindUserData*)user_data;
7608 if (!data->value && (mono_utf8_strcasecmp (name, (char*)data->key) == 0))
7609 data->value = value;
7613 * mono_class_from_name_case:
7614 * @image: The MonoImage where the type is looked up in
7615 * @name_space: the type namespace
7616 * @name: the type short name.
7617 * @deprecated: use the _checked variant
7619 * Obtains a MonoClass with a given namespace and a given name which
7620 * is located in the given MonoImage. The namespace and name
7621 * lookups are case insensitive.
7624 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
7627 MonoClass *res = mono_class_from_name_case_checked (image, name_space, name, &error);
7628 g_assert (!mono_error_ok (&error));
7633 mono_class_from_name_case_checked (MonoImage *image, const char* name_space, const char *name, MonoError *error)
7635 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
7636 guint32 cols [MONO_TYPEDEF_SIZE];
7641 mono_error_init (error);
7643 if (image_is_dynamic (image)) {
7645 FindUserData user_data;
7647 mono_image_init_name_cache (image);
7648 mono_image_lock (image);
7650 user_data.key = name_space;
7651 user_data.value = NULL;
7652 g_hash_table_foreach (image->name_cache, find_nocase, &user_data);
7654 if (user_data.value) {
7655 GHashTable *nspace_table = (GHashTable*)user_data.value;
7657 user_data.key = name;
7658 user_data.value = NULL;
7660 g_hash_table_foreach (nspace_table, find_nocase, &user_data);
7662 if (user_data.value)
7663 token = GPOINTER_TO_UINT (user_data.value);
7666 mono_image_unlock (image);
7669 return mono_class_get_checked (image, MONO_TOKEN_TYPE_DEF | token, error);
7675 /* add a cache if needed */
7676 for (i = 1; i <= t->rows; ++i) {
7677 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
7678 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
7680 * Nested types are accessed from the nesting name. We use the fact that nested types use different visibility flags
7681 * than toplevel types, thus avoiding the need to grovel through the NESTED_TYPE table
7683 if (visib >= TYPE_ATTRIBUTE_NESTED_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM)
7685 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
7686 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
7687 if (mono_utf8_strcasecmp (n, name) == 0 && mono_utf8_strcasecmp (nspace, name_space) == 0)
7688 return mono_class_get_checked (image, MONO_TOKEN_TYPE_DEF | i, error);
7694 return_nested_in (MonoClass *class, char *nested)
7697 char *s = strchr (nested, '/');
7698 gpointer iter = NULL;
7705 while ((found = mono_class_get_nested_types (class, &iter))) {
7706 if (strcmp (found->name, nested) == 0) {
7708 return return_nested_in (found, s);
7716 search_modules (MonoImage *image, const char *name_space, const char *name)
7718 MonoTableInfo *file_table = &image->tables [MONO_TABLE_FILE];
7719 MonoImage *file_image;
7724 * The EXPORTEDTYPES table only contains public types, so have to search the
7726 * Note: image->modules contains the contents of the MODULEREF table, while
7727 * the real module list is in the FILE table.
7729 for (i = 0; i < file_table->rows; i++) {
7730 guint32 cols [MONO_FILE_SIZE];
7731 mono_metadata_decode_row (file_table, i, cols, MONO_FILE_SIZE);
7732 if (cols [MONO_FILE_FLAGS] == FILE_CONTAINS_NO_METADATA)
7735 file_image = mono_image_load_file_for_image (image, i + 1);
7737 class = mono_class_from_name (file_image, name_space, name);
7747 mono_class_from_name_checked_aux (MonoImage *image, const char* name_space, const char *name, MonoError *error, GHashTable* visited_images)
7749 GHashTable *nspace_table;
7750 MonoImage *loaded_image;
7757 mono_error_init (error);
7759 // Checking visited images avoids stack overflows when cyclic references exist.
7760 if (g_hash_table_lookup (visited_images, image))
7763 g_hash_table_insert (visited_images, image, GUINT_TO_POINTER(1));
7765 if ((nested = strchr (name, '/'))) {
7766 int pos = nested - name;
7767 int len = strlen (name);
7770 memcpy (buf, name, len + 1);
7772 nested = buf + pos + 1;
7776 /* FIXME: get_class_from_name () can't handle types in the EXPORTEDTYPE table */
7777 if (get_class_from_name && image->tables [MONO_TABLE_EXPORTEDTYPE].rows == 0) {
7778 gboolean res = get_class_from_name (image, name_space, name, &class);
7781 class = search_modules (image, name_space, name);
7783 return class ? return_nested_in (class, nested) : NULL;
7789 mono_image_init_name_cache (image);
7790 mono_image_lock (image);
7792 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
7795 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
7797 mono_image_unlock (image);
7799 if (!token && image_is_dynamic (image) && image->modules) {
7800 /* Search modules as well */
7801 for (i = 0; i < image->module_count; ++i) {
7802 MonoImage *module = image->modules [i];
7804 class = mono_class_from_name (module, name_space, name);
7811 class = search_modules (image, name_space, name);
7819 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
7820 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
7821 guint32 cols [MONO_EXP_TYPE_SIZE];
7824 idx = mono_metadata_token_index (token);
7826 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
7828 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
7829 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
7830 loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
7833 class = mono_class_from_name_checked_aux (loaded_image, name_space, name, error, visited_images);
7835 return return_nested_in (class, nested);
7837 } else if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_ASSEMBLYREF) {
7838 guint32 assembly_idx;
7840 assembly_idx = impl >> MONO_IMPLEMENTATION_BITS;
7842 mono_assembly_load_reference (image, assembly_idx - 1);
7843 g_assert (image->references [assembly_idx - 1]);
7844 if (image->references [assembly_idx - 1] == (gpointer)-1)
7847 return mono_class_from_name_checked_aux (image->references [assembly_idx - 1]->image, name_space, name, error, visited_images);
7849 g_error ("not yet implemented");
7853 token = MONO_TOKEN_TYPE_DEF | token;
7855 class = mono_class_get_checked (image, token, error);
7857 return return_nested_in (class, nested);
7862 mono_class_from_name_checked (MonoImage *image, const char* name_space, const char *name, MonoError *error)
7865 GHashTable *visited_images;
7867 visited_images = g_hash_table_new (g_direct_hash, g_direct_equal);
7869 klass = mono_class_from_name_checked_aux (image, name_space, name, error, visited_images);
7871 g_hash_table_destroy (visited_images);
7877 * mono_class_from_name:
7878 * @image: The MonoImage where the type is looked up in
7879 * @name_space: the type namespace
7880 * @name: the type short name.
7882 * Obtains a MonoClass with a given namespace and a given name which
7883 * is located in the given MonoImage.
7885 * To reference nested classes, use the "/" character as a separator.
7886 * For example use "Foo/Bar" to reference the class Bar that is nested
7887 * inside Foo, like this: "class Foo { class Bar {} }".
7890 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
7895 klass = mono_class_from_name_checked (image, name_space, name, &error);
7896 if (!mono_error_ok (&error)) {
7897 mono_loader_set_error_from_mono_error (&error);
7898 mono_error_cleanup (&error); /* FIXME Don't swallow the error */
7904 * mono_class_is_subclass_of:
7905 * @klass: class to probe if it is a subclass of another one
7906 * @klassc: the class we suspect is the base class
7907 * @check_interfaces: whether we should perform interface checks
7909 * This method determines whether @klass is a subclass of @klassc.
7911 * If the @check_interfaces flag is set, then if @klassc is an interface
7912 * this method return true if the @klass implements the interface or
7913 * if @klass is an interface, if one of its base classes is @klass.
7915 * If @check_interfaces is false then, then if @klass is not an interface
7916 * then it returns true if the @klass is a subclass of @klassc.
7918 * if @klass is an interface and @klassc is System.Object, then this function
7923 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
7924 gboolean check_interfaces)
7926 /*FIXME test for interfaces with variant generic arguments*/
7928 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
7929 if (MONO_CLASS_IMPLEMENTS_INTERFACE (klass, klassc->interface_id))
7931 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
7934 for (i = 0; i < klass->interface_count; i ++) {
7935 MonoClass *ic = klass->interfaces [i];
7940 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
7945 * MS.NET thinks interfaces are a subclass of Object, so we think it as
7948 if (klassc == mono_defaults.object_class)
7955 mono_type_is_generic_argument (MonoType *type)
7957 return type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR;
7961 mono_class_has_variant_generic_params (MonoClass *klass)
7964 MonoGenericContainer *container;
7966 if (!klass->generic_class)
7969 container = klass->generic_class->container_class->generic_container;
7971 for (i = 0; i < container->type_argc; ++i)
7972 if (mono_generic_container_get_param_info (container, i)->flags & (MONO_GEN_PARAM_VARIANT|MONO_GEN_PARAM_COVARIANT))
7979 mono_gparam_is_reference_conversible (MonoClass *target, MonoClass *candidate, gboolean check_for_reference_conv)
7981 if (target == candidate)
7984 if (check_for_reference_conv &&
7985 mono_type_is_generic_argument (&target->byval_arg) &&
7986 mono_type_is_generic_argument (&candidate->byval_arg)) {
7987 MonoGenericParam *gparam = candidate->byval_arg.data.generic_param;
7988 MonoGenericParamInfo *pinfo = mono_generic_param_info (gparam);
7990 if (!pinfo || (pinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) == 0)
7993 if (!mono_class_is_assignable_from (target, candidate))
7999 * @container the generic container from the GTD
8000 * @klass: the class to be assigned to
8001 * @oklass: the source class
8003 * Both klass and oklass must be instances of the same generic interface.
8004 * Return true if @klass can be assigned to a @klass variable
8007 mono_class_is_variant_compatible (MonoClass *klass, MonoClass *oklass, gboolean check_for_reference_conv)
8010 MonoType **klass_argv, **oklass_argv;
8011 MonoClass *klass_gtd = mono_class_get_generic_type_definition (klass);
8012 MonoGenericContainer *container = klass_gtd->generic_container;
8014 if (klass == oklass)
8017 /*Viable candidates are instances of the same generic interface*/
8018 if (mono_class_get_generic_type_definition (oklass) != klass_gtd || oklass == klass_gtd)
8021 klass_argv = &klass->generic_class->context.class_inst->type_argv [0];
8022 oklass_argv = &oklass->generic_class->context.class_inst->type_argv [0];
8024 for (j = 0; j < container->type_argc; ++j) {
8025 MonoClass *param1_class = mono_class_from_mono_type (klass_argv [j]);
8026 MonoClass *param2_class = mono_class_from_mono_type (oklass_argv [j]);
8028 if (param1_class->valuetype != param2_class->valuetype || (param1_class->valuetype && param1_class != param2_class))
8032 * The _VARIANT and _COVARIANT constants should read _COVARIANT and
8033 * _CONTRAVARIANT, but they are in a public header so we can't fix it.
8035 if (param1_class != param2_class) {
8036 if (mono_generic_container_get_param_info (container, j)->flags & MONO_GEN_PARAM_VARIANT) {
8037 if (!mono_gparam_is_reference_conversible (param1_class, param2_class, check_for_reference_conv))
8039 } else if (mono_generic_container_get_param_info (container, j)->flags & MONO_GEN_PARAM_COVARIANT) {
8040 if (!mono_gparam_is_reference_conversible (param2_class, param1_class, check_for_reference_conv))
8050 mono_gparam_is_assignable_from (MonoClass *target, MonoClass *candidate)
8052 MonoGenericParam *gparam, *ogparam;
8053 MonoGenericParamInfo *tinfo, *cinfo;
8054 MonoClass **candidate_class;
8055 gboolean class_constraint_satisfied, valuetype_constraint_satisfied;
8058 if (target == candidate)
8060 if (target->byval_arg.type != candidate->byval_arg.type)
8063 gparam = target->byval_arg.data.generic_param;
8064 ogparam = candidate->byval_arg.data.generic_param;
8065 tinfo = mono_generic_param_info (gparam);
8066 cinfo = mono_generic_param_info (ogparam);
8068 class_constraint_satisfied = FALSE;
8069 valuetype_constraint_satisfied = FALSE;
8071 /*candidate must have a super set of target's special constraints*/
8072 tmask = tinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
8073 cmask = cinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_SPECIAL_CONSTRAINTS_MASK;
8075 if (cinfo->constraints) {
8076 for (candidate_class = cinfo->constraints; *candidate_class; ++candidate_class) {
8077 MonoClass *cc = *candidate_class;
8079 if (mono_type_is_reference (&cc->byval_arg) && !MONO_CLASS_IS_INTERFACE (cc))
8080 class_constraint_satisfied = TRUE;
8081 else if (!mono_type_is_reference (&cc->byval_arg) && !MONO_CLASS_IS_INTERFACE (cc))
8082 valuetype_constraint_satisfied = TRUE;
8085 class_constraint_satisfied |= (cmask & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) != 0;
8086 valuetype_constraint_satisfied |= (cmask & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) != 0;
8088 if ((tmask & GENERIC_PARAMETER_ATTRIBUTE_REFERENCE_TYPE_CONSTRAINT) && !class_constraint_satisfied)
8090 if ((tmask & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT) && !valuetype_constraint_satisfied)
8092 if ((tmask & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) && !((cmask & GENERIC_PARAMETER_ATTRIBUTE_CONSTRUCTOR_CONSTRAINT) ||
8093 valuetype_constraint_satisfied)) {
8098 /*candidate type constraints must be a superset of target's*/
8099 if (tinfo->constraints) {
8100 MonoClass **target_class;
8101 for (target_class = tinfo->constraints; *target_class; ++target_class) {
8102 MonoClass *tc = *target_class;
8105 * A constraint from @target might inflate into @candidate itself and in that case we don't need
8106 * check it's constraints since it satisfy the constraint by itself.
8108 if (mono_metadata_type_equal (&tc->byval_arg, &candidate->byval_arg))
8111 if (!cinfo->constraints)
8114 for (candidate_class = cinfo->constraints; *candidate_class; ++candidate_class) {
8115 MonoClass *cc = *candidate_class;
8117 if (mono_class_is_assignable_from (tc, cc))
8121 * This happens when we have the following:
8123 * Bar<K> where K : IFace
8124 * Foo<T, U> where T : U where U : IFace
8126 * Bar<T> <- T here satisfy K constraint transitively through to U's constraint
8129 if (mono_type_is_generic_argument (&cc->byval_arg)) {
8130 if (mono_gparam_is_assignable_from (target, cc))
8134 if (!*candidate_class)
8139 /*candidate itself must have a constraint that satisfy target*/
8140 if (cinfo->constraints) {
8141 for (candidate_class = cinfo->constraints; *candidate_class; ++candidate_class) {
8142 MonoClass *cc = *candidate_class;
8143 if (mono_class_is_assignable_from (target, cc))
8151 * mono_class_is_assignable_from:
8152 * @klass: the class to be assigned to
8153 * @oklass: the source class
8155 * Return: true if an instance of object oklass can be assigned to an
8156 * instance of object @klass
8159 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
8161 /*FIXME this will cause a lot of irrelevant stuff to be loaded.*/
8163 mono_class_init (klass);
8165 if (!oklass->inited)
8166 mono_class_init (oklass);
8168 if (klass->exception_type || oklass->exception_type)
8171 if (mono_type_is_generic_argument (&klass->byval_arg)) {
8172 if (!mono_type_is_generic_argument (&oklass->byval_arg))
8174 return mono_gparam_is_assignable_from (klass, oklass);
8177 if (MONO_CLASS_IS_INTERFACE (klass)) {
8178 if ((oklass->byval_arg.type == MONO_TYPE_VAR) || (oklass->byval_arg.type == MONO_TYPE_MVAR)) {
8179 MonoGenericParam *gparam = oklass->byval_arg.data.generic_param;
8180 MonoClass **constraints = mono_generic_container_get_param_info (gparam->owner, gparam->num)->constraints;
8184 for (i = 0; constraints [i]; ++i) {
8185 if (mono_class_is_assignable_from (klass, constraints [i]))
8193 /* interface_offsets might not be set for dynamic classes */
8194 if (oklass->ref_info_handle && !oklass->interface_bitmap)
8196 * oklass might be a generic type parameter but they have
8197 * interface_offsets set.
8199 return mono_reflection_call_is_assignable_to (oklass, klass);
8200 if (!oklass->interface_bitmap)
8201 /* Happens with generic instances of not-yet created dynamic types */
8203 if (MONO_CLASS_IMPLEMENTS_INTERFACE (oklass, klass->interface_id))
8206 if (mono_class_has_variant_generic_params (klass)) {
8209 mono_class_setup_interfaces (oklass, &error);
8210 if (!mono_error_ok (&error)) {
8211 mono_error_cleanup (&error);
8215 /*klass is a generic variant interface, We need to extract from oklass a list of ifaces which are viable candidates.*/
8216 for (i = 0; i < oklass->interface_offsets_count; ++i) {
8217 MonoClass *iface = oklass->interfaces_packed [i];
8219 if (mono_class_is_variant_compatible (klass, iface, FALSE))
8224 } else if (klass->delegate) {
8225 if (mono_class_has_variant_generic_params (klass) && mono_class_is_variant_compatible (klass, oklass, FALSE))
8227 }else if (klass->rank) {
8228 MonoClass *eclass, *eoclass;
8230 if (oklass->rank != klass->rank)
8233 /* vectors vs. one dimensional arrays */
8234 if (oklass->byval_arg.type != klass->byval_arg.type)
8237 eclass = klass->cast_class;
8238 eoclass = oklass->cast_class;
8241 * a is b does not imply a[] is b[] when a is a valuetype, and
8242 * b is a reference type.
8245 if (eoclass->valuetype) {
8246 if ((eclass == mono_defaults.enum_class) ||
8247 (eclass == mono_defaults.enum_class->parent) ||
8248 (eclass == mono_defaults.object_class))
8252 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
8253 } else if (mono_class_is_nullable (klass)) {
8254 if (mono_class_is_nullable (oklass))
8255 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
8257 return mono_class_is_assignable_from (klass->cast_class, oklass);
8258 } else if (klass == mono_defaults.object_class)
8261 return mono_class_has_parent (oklass, klass);
8264 /*Check if @oklass is variant compatible with @klass.*/
8266 mono_class_is_variant_compatible_slow (MonoClass *klass, MonoClass *oklass)
8269 MonoType **klass_argv, **oklass_argv;
8270 MonoClass *klass_gtd = mono_class_get_generic_type_definition (klass);
8271 MonoGenericContainer *container = klass_gtd->generic_container;
8273 /*Viable candidates are instances of the same generic interface*/
8274 if (mono_class_get_generic_type_definition (oklass) != klass_gtd || oklass == klass_gtd)
8277 klass_argv = &klass->generic_class->context.class_inst->type_argv [0];
8278 oklass_argv = &oklass->generic_class->context.class_inst->type_argv [0];
8280 for (j = 0; j < container->type_argc; ++j) {
8281 MonoClass *param1_class = mono_class_from_mono_type (klass_argv [j]);
8282 MonoClass *param2_class = mono_class_from_mono_type (oklass_argv [j]);
8284 if (param1_class->valuetype != param2_class->valuetype)
8288 * The _VARIANT and _COVARIANT constants should read _COVARIANT and
8289 * _CONTRAVARIANT, but they are in a public header so we can't fix it.
8291 if (param1_class != param2_class) {
8292 if (mono_generic_container_get_param_info (container, j)->flags & MONO_GEN_PARAM_VARIANT) {
8293 if (!mono_class_is_assignable_from_slow (param1_class, param2_class))
8295 } else if (mono_generic_container_get_param_info (container, j)->flags & MONO_GEN_PARAM_COVARIANT) {
8296 if (!mono_class_is_assignable_from_slow (param2_class, param1_class))
8304 /*Check if @candidate implements the interface @target*/
8306 mono_class_implement_interface_slow (MonoClass *target, MonoClass *candidate)
8310 gboolean is_variant = mono_class_has_variant_generic_params (target);
8312 if (is_variant && MONO_CLASS_IS_INTERFACE (candidate)) {
8313 if (mono_class_is_variant_compatible_slow (target, candidate))
8318 if (candidate == target)
8321 /*A TypeBuilder can have more interfaces on tb->interfaces than on candidate->interfaces*/
8322 if (image_is_dynamic (candidate->image) && !candidate->wastypebuilder) {
8323 MonoReflectionTypeBuilder *tb = mono_class_get_ref_info (candidate);
8325 if (tb && tb->interfaces) {
8326 for (j = mono_array_length (tb->interfaces) - 1; j >= 0; --j) {
8327 MonoReflectionType *iface = mono_array_get (tb->interfaces, MonoReflectionType*, j);
8328 MonoClass *iface_class;
8330 /* we can't realize the type here since it can do pretty much anything. */
8333 iface_class = mono_class_from_mono_type (iface->type);
8334 if (iface_class == target)
8336 if (is_variant && mono_class_is_variant_compatible_slow (target, iface_class))
8338 if (mono_class_implement_interface_slow (target, iface_class))
8343 /*setup_interfaces don't mono_class_init anything*/
8344 /*FIXME this doesn't handle primitive type arrays.
8345 ICollection<sbyte> x byte [] won't work because candidate->interfaces, for byte[], won't have IList<sbyte>.
8346 A possible way to fix this would be to move that to setup_interfaces from setup_interface_offsets.
8348 mono_class_setup_interfaces (candidate, &error);
8349 if (!mono_error_ok (&error)) {
8350 mono_error_cleanup (&error);
8354 for (i = 0; i < candidate->interface_count; ++i) {
8355 if (candidate->interfaces [i] == target)
8358 if (is_variant && mono_class_is_variant_compatible_slow (target, candidate->interfaces [i]))
8361 if (mono_class_implement_interface_slow (target, candidate->interfaces [i]))
8365 candidate = candidate->parent;
8366 } while (candidate);
8372 * Check if @oklass can be assigned to @klass.
8373 * This function does the same as mono_class_is_assignable_from but is safe to be used from mono_class_init context.
8376 mono_class_is_assignable_from_slow (MonoClass *target, MonoClass *candidate)
8378 if (candidate == target)
8380 if (target == mono_defaults.object_class)
8383 if (mono_class_has_parent (candidate, target))
8386 /*If target is not an interface there is no need to check them.*/
8387 if (MONO_CLASS_IS_INTERFACE (target))
8388 return mono_class_implement_interface_slow (target, candidate);
8390 if (target->delegate && mono_class_has_variant_generic_params (target))
8391 return mono_class_is_variant_compatible (target, candidate, FALSE);
8394 MonoClass *eclass, *eoclass;
8396 if (target->rank != candidate->rank)
8399 /* vectors vs. one dimensional arrays */
8400 if (target->byval_arg.type != candidate->byval_arg.type)
8403 eclass = target->cast_class;
8404 eoclass = candidate->cast_class;
8407 * a is b does not imply a[] is b[] when a is a valuetype, and
8408 * b is a reference type.
8411 if (eoclass->valuetype) {
8412 if ((eclass == mono_defaults.enum_class) ||
8413 (eclass == mono_defaults.enum_class->parent) ||
8414 (eclass == mono_defaults.object_class))
8418 return mono_class_is_assignable_from_slow (target->cast_class, candidate->cast_class);
8420 /*FIXME properly handle nullables */
8421 /*FIXME properly handle (M)VAR */
8426 * mono_class_get_cctor:
8427 * @klass: A MonoClass pointer
8429 * Returns: the static constructor of @klass if it exists, NULL otherwise.
8432 mono_class_get_cctor (MonoClass *klass)
8434 MonoCachedClassInfo cached_info;
8436 if (image_is_dynamic (klass->image)) {
8438 * has_cctor is not set for these classes because mono_class_init () is
8441 return mono_class_get_method_from_name_flags (klass, ".cctor", -1, METHOD_ATTRIBUTE_SPECIAL_NAME);
8444 if (!klass->has_cctor)
8447 if (mono_class_get_cached_class_info (klass, &cached_info)) {
8449 MonoMethod *result = mono_get_method_checked (klass->image, cached_info.cctor_token, klass, NULL, &error);
8450 if (!mono_error_ok (&error))
8451 g_error ("Could not lookup class cctor from cached metadata due to %s", mono_error_get_message (&error));
8455 if (klass->generic_class && !klass->methods)
8456 return mono_class_get_inflated_method (klass, mono_class_get_cctor (klass->generic_class->container_class));
8458 return mono_class_get_method_from_name_flags (klass, ".cctor", -1, METHOD_ATTRIBUTE_SPECIAL_NAME);
8462 * mono_class_get_finalizer:
8463 * @klass: The MonoClass pointer
8465 * Returns: the finalizer method of @klass if it exists, NULL otherwise.
8468 mono_class_get_finalizer (MonoClass *klass)
8470 MonoCachedClassInfo cached_info;
8473 mono_class_init (klass);
8474 if (!mono_class_has_finalizer (klass))
8477 if (mono_class_get_cached_class_info (klass, &cached_info)) {
8479 MonoMethod *result = mono_get_method_checked (cached_info.finalize_image, cached_info.finalize_token, NULL, NULL, &error);
8480 if (!mono_error_ok (&error))
8481 g_error ("Could not lookup finalizer from cached metadata due to %s", mono_error_get_message (&error));
8484 mono_class_setup_vtable (klass);
8485 return klass->vtable [finalize_slot];
8490 * mono_class_needs_cctor_run:
8491 * @klass: the MonoClass pointer
8492 * @caller: a MonoMethod describing the caller
8494 * Determines whenever the class has a static constructor and whenever it
8495 * needs to be called when executing CALLER.
8498 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
8502 method = mono_class_get_cctor (klass);
8504 return (method == caller) ? FALSE : TRUE;
8510 * mono_class_array_element_size:
8513 * Returns: the number of bytes an element of type @klass
8514 * uses when stored into an array.
8517 mono_class_array_element_size (MonoClass *klass)
8519 MonoType *type = &klass->byval_arg;
8522 switch (type->type) {
8525 case MONO_TYPE_BOOLEAN:
8529 case MONO_TYPE_CHAR:
8538 case MONO_TYPE_CLASS:
8539 case MONO_TYPE_STRING:
8540 case MONO_TYPE_OBJECT:
8541 case MONO_TYPE_SZARRAY:
8542 case MONO_TYPE_ARRAY:
8543 return sizeof (gpointer);
8548 case MONO_TYPE_VALUETYPE:
8549 if (type->data.klass->enumtype) {
8550 type = mono_class_enum_basetype (type->data.klass);
8551 klass = klass->element_class;
8554 return mono_class_instance_size (klass) - sizeof (MonoObject);
8555 case MONO_TYPE_GENERICINST:
8556 type = &type->data.generic_class->container_class->byval_arg;
8559 case MONO_TYPE_MVAR: {
8562 return mono_type_size (type, &align);
8564 case MONO_TYPE_VOID:
8568 g_error ("unknown type 0x%02x in mono_class_array_element_size", type->type);
8574 * mono_array_element_size:
8575 * @ac: pointer to a #MonoArrayClass
8577 * Returns: the size of single array element.
8580 mono_array_element_size (MonoClass *ac)
8582 g_assert (ac->rank);
8583 return ac->sizes.element_size;
8587 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
8588 MonoGenericContext *context)
8591 gpointer res = mono_ldtoken_checked (image, token, handle_class, context, &error);
8592 g_assert (mono_error_ok (&error));
8597 mono_ldtoken_checked (MonoImage *image, guint32 token, MonoClass **handle_class,
8598 MonoGenericContext *context, MonoError *error)
8600 mono_error_init (error);
8602 if (image_is_dynamic (image)) {
8603 MonoClass *tmp_handle_class;
8604 gpointer obj = mono_lookup_dynamic_token_class (image, token, TRUE, &tmp_handle_class, context);
8606 g_assert (tmp_handle_class);
8608 *handle_class = tmp_handle_class;
8610 if (tmp_handle_class == mono_defaults.typehandle_class)
8611 return &((MonoClass*)obj)->byval_arg;
8616 switch (token & 0xff000000) {
8617 case MONO_TOKEN_TYPE_DEF:
8618 case MONO_TOKEN_TYPE_REF:
8619 case MONO_TOKEN_TYPE_SPEC: {
8622 *handle_class = mono_defaults.typehandle_class;
8623 type = mono_type_get_checked (image, token, context, error);
8627 mono_class_init (mono_class_from_mono_type (type));
8628 /* We return a MonoType* as handle */
8631 case MONO_TOKEN_FIELD_DEF: {
8633 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
8635 mono_error_set_bad_image (error, image, "Bad ldtoken %x", token);
8639 *handle_class = mono_defaults.fieldhandle_class;
8640 class = mono_class_get_and_inflate_typespec_checked (image, MONO_TOKEN_TYPE_DEF | type, context, error);
8644 mono_class_init (class);
8645 return mono_class_get_field (class, token);
8647 case MONO_TOKEN_METHOD_DEF:
8648 case MONO_TOKEN_METHOD_SPEC: {
8650 meth = mono_get_method_checked (image, token, NULL, context, error);
8652 *handle_class = mono_defaults.methodhandle_class;
8658 case MONO_TOKEN_MEMBER_REF: {
8659 guint32 cols [MONO_MEMBERREF_SIZE];
8661 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
8662 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
8663 mono_metadata_decode_blob_size (sig, &sig);
8664 if (*sig == 0x6) { /* it's a field */
8666 MonoClassField *field;
8667 field = mono_field_from_token_checked (image, token, &klass, context, error);
8669 *handle_class = mono_defaults.fieldhandle_class;
8673 meth = mono_get_method_checked (image, token, NULL, context, error);
8675 *handle_class = mono_defaults.methodhandle_class;
8680 mono_error_set_bad_image (error, image, "Bad ldtoken %x", token);
8686 * This function might need to call runtime functions so it can't be part
8687 * of the metadata library.
8689 static MonoLookupDynamicToken lookup_dynamic = NULL;
8692 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
8694 lookup_dynamic = func;
8698 mono_lookup_dynamic_token (MonoImage *image, guint32 token, MonoGenericContext *context)
8700 MonoClass *handle_class;
8702 return lookup_dynamic (image, token, TRUE, &handle_class, context);
8706 mono_lookup_dynamic_token_class (MonoImage *image, guint32 token, gboolean valid_token, MonoClass **handle_class, MonoGenericContext *context)
8708 return lookup_dynamic (image, token, valid_token, handle_class, context);
8711 static MonoGetCachedClassInfo get_cached_class_info = NULL;
8714 mono_install_get_cached_class_info (MonoGetCachedClassInfo func)
8716 get_cached_class_info = func;
8720 mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
8722 if (!get_cached_class_info)
8725 return get_cached_class_info (klass, res);
8729 mono_install_get_class_from_name (MonoGetClassFromName func)
8731 get_class_from_name = func;
8735 mono_class_get_image (MonoClass *klass)
8737 return klass->image;
8741 * mono_class_get_element_class:
8742 * @klass: the MonoClass to act on
8744 * Returns: the element class of an array or an enumeration.
8747 mono_class_get_element_class (MonoClass *klass)
8749 return klass->element_class;
8753 * mono_class_is_valuetype:
8754 * @klass: the MonoClass to act on
8756 * Returns: true if the MonoClass represents a ValueType.
8759 mono_class_is_valuetype (MonoClass *klass)
8761 return klass->valuetype;
8765 * mono_class_is_enum:
8766 * @klass: the MonoClass to act on
8768 * Returns: true if the MonoClass represents an enumeration.
8771 mono_class_is_enum (MonoClass *klass)
8773 return klass->enumtype;
8777 * mono_class_enum_basetype:
8778 * @klass: the MonoClass to act on
8780 * Returns: the underlying type representation for an enumeration.
8783 mono_class_enum_basetype (MonoClass *klass)
8785 if (klass->element_class == klass)
8786 /* SRE or broken types */
8789 return &klass->element_class->byval_arg;
8793 * mono_class_get_parent
8794 * @klass: the MonoClass to act on
8796 * Returns: the parent class for this class.
8799 mono_class_get_parent (MonoClass *klass)
8801 return klass->parent;
8805 * mono_class_get_nesting_type;
8806 * @klass: the MonoClass to act on
8808 * Returns: the container type where this type is nested or NULL if this type is not a nested type.
8811 mono_class_get_nesting_type (MonoClass *klass)
8813 return klass->nested_in;
8817 * mono_class_get_rank:
8818 * @klass: the MonoClass to act on
8820 * Returns: the rank for the array (the number of dimensions).
8823 mono_class_get_rank (MonoClass *klass)
8829 * mono_class_get_flags:
8830 * @klass: the MonoClass to act on
8832 * The type flags from the TypeDef table from the metadata.
8833 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
8836 * Returns: the flags from the TypeDef table.
8839 mono_class_get_flags (MonoClass *klass)
8841 return klass->flags;
8845 * mono_class_get_name
8846 * @klass: the MonoClass to act on
8848 * Returns: the name of the class.
8851 mono_class_get_name (MonoClass *klass)
8857 * mono_class_get_namespace:
8858 * @klass: the MonoClass to act on
8860 * Returns: the namespace of the class.
8863 mono_class_get_namespace (MonoClass *klass)
8865 return klass->name_space;
8869 * mono_class_get_type:
8870 * @klass: the MonoClass to act on
8872 * This method returns the internal Type representation for the class.
8874 * Returns: the MonoType from the class.
8877 mono_class_get_type (MonoClass *klass)
8879 return &klass->byval_arg;
8883 * mono_class_get_type_token
8884 * @klass: the MonoClass to act on
8886 * This method returns type token for the class.
8888 * Returns: the type token for the class.
8891 mono_class_get_type_token (MonoClass *klass)
8893 return klass->type_token;
8897 * mono_class_get_byref_type:
8898 * @klass: the MonoClass to act on
8903 mono_class_get_byref_type (MonoClass *klass)
8905 return &klass->this_arg;
8909 * mono_class_num_fields:
8910 * @klass: the MonoClass to act on
8912 * Returns: the number of static and instance fields in the class.
8915 mono_class_num_fields (MonoClass *klass)
8917 return klass->field.count;
8921 * mono_class_num_methods:
8922 * @klass: the MonoClass to act on
8924 * Returns: the number of methods in the class.
8927 mono_class_num_methods (MonoClass *klass)
8929 return klass->method.count;
8933 * mono_class_num_properties
8934 * @klass: the MonoClass to act on
8936 * Returns: the number of properties in the class.
8939 mono_class_num_properties (MonoClass *klass)
8941 mono_class_setup_properties (klass);
8943 return klass->ext->property.count;
8947 * mono_class_num_events:
8948 * @klass: the MonoClass to act on
8950 * Returns: the number of events in the class.
8953 mono_class_num_events (MonoClass *klass)
8955 mono_class_setup_events (klass);
8957 return klass->ext->event.count;
8961 * mono_class_get_fields:
8962 * @klass: the MonoClass to act on
8964 * This routine is an iterator routine for retrieving the fields in a class.
8966 * You must pass a gpointer that points to zero and is treated as an opaque handle to
8967 * iterate over all of the elements. When no more values are
8968 * available, the return value is NULL.
8970 * Returns: a @MonoClassField* on each iteration, or NULL when no more fields are available.
8973 mono_class_get_fields (MonoClass* klass, gpointer *iter)
8975 MonoClassField* field;
8979 mono_class_setup_fields_locking (klass);
8980 if (klass->exception_type)
8982 /* start from the first */
8983 if (klass->field.count) {
8984 return *iter = &klass->fields [0];
8992 if (field < &klass->fields [klass->field.count]) {
8993 return *iter = field;
8999 * mono_class_get_methods
9000 * @klass: the MonoClass to act on
9002 * This routine is an iterator routine for retrieving the fields in a class.
9004 * You must pass a gpointer that points to zero and is treated as an opaque handle to
9005 * iterate over all of the elements. When no more values are
9006 * available, the return value is NULL.
9008 * Returns: a MonoMethod on each iteration or NULL when no more methods are available.
9011 mono_class_get_methods (MonoClass* klass, gpointer *iter)
9013 MonoMethod** method;
9017 mono_class_setup_methods (klass);
9020 * We can't fail lookup of methods otherwise the runtime will burst in flames on all sort of places.
9021 * FIXME we should better report this error to the caller
9023 if (!klass->methods)
9025 /* start from the first */
9026 if (klass->method.count) {
9027 *iter = &klass->methods [0];
9028 return klass->methods [0];
9036 if (method < &klass->methods [klass->method.count]) {
9044 * mono_class_get_virtual_methods:
9046 * Iterate over the virtual methods of KLASS.
9048 * LOCKING: Assumes the loader lock is held (because of the klass->methods check).
9051 mono_class_get_virtual_methods (MonoClass* klass, gpointer *iter)
9053 MonoMethod** method;
9056 if (klass->methods || !MONO_CLASS_HAS_STATIC_METADATA (klass)) {
9058 mono_class_setup_methods (klass);
9060 * We can't fail lookup of methods otherwise the runtime will burst in flames on all sort of places.
9061 * FIXME we should better report this error to the caller
9063 if (!klass->methods)
9065 /* start from the first */
9066 method = &klass->methods [0];
9071 while (method < &klass->methods [klass->method.count]) {
9072 if (*method && ((*method)->flags & METHOD_ATTRIBUTE_VIRTUAL))
9076 if (method < &klass->methods [klass->method.count]) {
9083 /* Search directly in metadata to avoid calling setup_methods () */
9084 MonoMethod *res = NULL;
9090 start_index = GPOINTER_TO_UINT (*iter);
9093 for (i = start_index; i < klass->method.count; ++i) {
9096 /* class->method.first points into the methodptr table */
9097 flags = mono_metadata_decode_table_row_col (klass->image, MONO_TABLE_METHOD, klass->method.first + i, MONO_METHOD_FLAGS);
9099 if (flags & METHOD_ATTRIBUTE_VIRTUAL)
9103 if (i < klass->method.count) {
9105 res = mono_get_method_checked (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass, NULL, &error);
9106 mono_error_cleanup (&error); /* FIXME don't swallow the error */
9108 /* Add 1 here so the if (*iter) check fails */
9109 *iter = GUINT_TO_POINTER (i + 1);
9118 * mono_class_get_properties:
9119 * @klass: the MonoClass to act on
9121 * This routine is an iterator routine for retrieving the properties in a class.
9123 * You must pass a gpointer that points to zero and is treated as an opaque handle to
9124 * iterate over all of the elements. When no more values are
9125 * available, the return value is NULL.
9127 * Returns: a @MonoProperty* on each invocation, or NULL when no more are available.
9130 mono_class_get_properties (MonoClass* klass, gpointer *iter)
9132 MonoProperty* property;
9136 mono_class_setup_properties (klass);
9137 /* start from the first */
9138 if (klass->ext->property.count) {
9139 return *iter = &klass->ext->properties [0];
9147 if (property < &klass->ext->properties [klass->ext->property.count]) {
9148 return *iter = property;
9154 * mono_class_get_events:
9155 * @klass: the MonoClass to act on
9157 * This routine is an iterator routine for retrieving the properties in a class.
9159 * You must pass a gpointer that points to zero and is treated as an opaque handle to
9160 * iterate over all of the elements. When no more values are
9161 * available, the return value is NULL.
9163 * Returns: a @MonoEvent* on each invocation, or NULL when no more are available.
9166 mono_class_get_events (MonoClass* klass, gpointer *iter)
9172 mono_class_setup_events (klass);
9173 /* start from the first */
9174 if (klass->ext->event.count) {
9175 return *iter = &klass->ext->events [0];
9183 if (event < &klass->ext->events [klass->ext->event.count]) {
9184 return *iter = event;
9190 * mono_class_get_interfaces
9191 * @klass: the MonoClass to act on
9193 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
9195 * You must pass a gpointer that points to zero and is treated as an opaque handle to
9196 * iterate over all of the elements. When no more values are
9197 * available, the return value is NULL.
9199 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
9202 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
9210 mono_class_init (klass);
9211 if (!klass->interfaces_inited) {
9212 mono_class_setup_interfaces (klass, &error);
9213 if (!mono_error_ok (&error)) {
9214 mono_error_cleanup (&error);
9218 /* start from the first */
9219 if (klass->interface_count) {
9220 *iter = &klass->interfaces [0];
9221 return klass->interfaces [0];
9229 if (iface < &klass->interfaces [klass->interface_count]) {
9237 setup_nested_types (MonoClass *klass)
9240 GList *classes, *nested_classes, *l;
9243 if (klass->nested_classes_inited)
9246 if (!klass->type_token)
9247 klass->nested_classes_inited = TRUE;
9249 i = mono_metadata_nesting_typedef (klass->image, klass->type_token, 1);
9253 guint32 cols [MONO_NESTED_CLASS_SIZE];
9254 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
9255 nclass = mono_class_create_from_typedef (klass->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED], &error);
9256 if (!mono_error_ok (&error)) {
9257 /*FIXME don't swallow the error message*/
9258 mono_error_cleanup (&error);
9260 i = mono_metadata_nesting_typedef (klass->image, klass->type_token, i + 1);
9264 classes = g_list_prepend (classes, nclass);
9266 i = mono_metadata_nesting_typedef (klass->image, klass->type_token, i + 1);
9269 mono_class_alloc_ext (klass);
9271 nested_classes = NULL;
9272 for (l = classes; l; l = l->next)
9273 nested_classes = g_list_prepend_image (klass->image, nested_classes, l->data);
9274 g_list_free (classes);
9276 mono_image_lock (klass->image);
9278 mono_memory_barrier ();
9279 if (!klass->nested_classes_inited) {
9280 klass->ext->nested_classes = nested_classes;
9281 mono_memory_barrier ();
9282 klass->nested_classes_inited = TRUE;
9285 mono_image_unlock (klass->image);
9289 * mono_class_get_nested_types
9290 * @klass: the MonoClass to act on
9292 * This routine is an iterator routine for retrieving the nested types of a class.
9293 * This works only if @klass is non-generic, or a generic type definition.
9295 * You must pass a gpointer that points to zero and is treated as an opaque handle to
9296 * iterate over all of the elements. When no more values are
9297 * available, the return value is NULL.
9299 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
9302 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
9308 if (!klass->nested_classes_inited)
9309 setup_nested_types (klass);
9312 /* start from the first */
9313 if (klass->ext && klass->ext->nested_classes) {
9314 *iter = klass->ext->nested_classes;
9315 return klass->ext->nested_classes->data;
9317 /* no nested types */
9332 * mono_class_is_delegate
9333 * @klass: the MonoClass to act on
9335 * Returns: true if the MonoClass represents a System.Delegate.
9338 mono_class_is_delegate (MonoClass *klass)
9340 return klass->delegate;
9344 * mono_class_implements_interface
9345 * @klass: The MonoClass to act on
9346 * @interface: The interface to check if @klass implements.
9348 * Returns: true if @klass implements @interface.
9351 mono_class_implements_interface (MonoClass* klass, MonoClass* iface)
9353 return mono_class_is_assignable_from (iface, klass);
9357 * mono_field_get_name:
9358 * @field: the MonoClassField to act on
9360 * Returns: the name of the field.
9363 mono_field_get_name (MonoClassField *field)
9369 * mono_field_get_type:
9370 * @field: the MonoClassField to act on
9372 * Returns: MonoType of the field.
9375 mono_field_get_type (MonoClassField *field)
9378 MonoType *type = mono_field_get_type_checked (field, &error);
9379 if (!mono_error_ok (&error)) {
9380 mono_trace_warning (MONO_TRACE_TYPE, "Could not load field's type due to %s", mono_error_get_message (&error));
9381 mono_error_cleanup (&error);
9388 * mono_field_get_type_checked:
9389 * @field: the MonoClassField to act on
9390 * @error: used to return any erro found while retrieving @field type
9392 * Returns: MonoType of the field.
9395 mono_field_get_type_checked (MonoClassField *field, MonoError *error)
9397 mono_error_init (error);
9399 mono_field_resolve_type (field, error);
9404 * mono_field_get_parent:
9405 * @field: the MonoClassField to act on
9407 * Returns: MonoClass where the field was defined.
9410 mono_field_get_parent (MonoClassField *field)
9412 return field->parent;
9416 * mono_field_get_flags;
9417 * @field: the MonoClassField to act on
9419 * The metadata flags for a field are encoded using the
9420 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
9422 * Returns: the flags for the field.
9425 mono_field_get_flags (MonoClassField *field)
9428 return mono_field_resolve_flags (field);
9429 return field->type->attrs;
9433 * mono_field_get_offset;
9434 * @field: the MonoClassField to act on
9436 * Returns: the field offset.
9439 mono_field_get_offset (MonoClassField *field)
9441 return field->offset;
9445 mono_field_get_rva (MonoClassField *field)
9449 MonoClass *klass = field->parent;
9450 MonoFieldDefaultValue *field_def_values;
9452 g_assert (field->type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_RVA);
9454 if (!klass->ext || !klass->ext->field_def_values) {
9455 mono_class_alloc_ext (klass);
9457 field_def_values = mono_class_alloc0 (klass, sizeof (MonoFieldDefaultValue) * klass->field.count);
9459 mono_image_lock (klass->image);
9460 if (!klass->ext->field_def_values)
9461 klass->ext->field_def_values = field_def_values;
9462 mono_image_unlock (klass->image);
9465 field_index = mono_field_get_index (field);
9467 if (!klass->ext->field_def_values [field_index].data && !image_is_dynamic (klass->image)) {
9468 mono_metadata_field_info (field->parent->image, klass->field.first + field_index, NULL, &rva, NULL);
9470 g_warning ("field %s in %s should have RVA data, but hasn't", mono_field_get_name (field), field->parent->name);
9471 klass->ext->field_def_values [field_index].data = mono_image_rva_map (field->parent->image, rva);
9474 return klass->ext->field_def_values [field_index].data;
9478 * mono_field_get_data;
9479 * @field: the MonoClassField to act on
9481 * Returns: pointer to the metadata constant value or to the field
9482 * data if it has an RVA flag.
9485 mono_field_get_data (MonoClassField *field)
9487 if (field->type->attrs & FIELD_ATTRIBUTE_HAS_DEFAULT) {
9488 MonoTypeEnum def_type;
9490 return mono_class_get_field_default_value (field, &def_type);
9491 } else if (field->type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
9492 return mono_field_get_rva (field);
9499 * mono_property_get_name:
9500 * @prop: the MonoProperty to act on
9502 * Returns: the name of the property
9505 mono_property_get_name (MonoProperty *prop)
9511 * mono_property_get_set_method
9512 * @prop: the MonoProperty to act on.
9514 * Returns: the setter method of the property (A MonoMethod)
9517 mono_property_get_set_method (MonoProperty *prop)
9523 * mono_property_get_get_method
9524 * @prop: the MonoProperty to act on.
9526 * Returns: the setter method of the property (A MonoMethod)
9529 mono_property_get_get_method (MonoProperty *prop)
9535 * mono_property_get_parent:
9536 * @prop: the MonoProperty to act on.
9538 * Returns: the MonoClass where the property was defined.
9541 mono_property_get_parent (MonoProperty *prop)
9543 return prop->parent;
9547 * mono_property_get_flags:
9548 * @prop: the MonoProperty to act on.
9550 * The metadata flags for a property are encoded using the
9551 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
9553 * Returns: the flags for the property.
9556 mono_property_get_flags (MonoProperty *prop)
9562 * mono_event_get_name:
9563 * @event: the MonoEvent to act on
9565 * Returns: the name of the event.
9568 mono_event_get_name (MonoEvent *event)
9574 * mono_event_get_add_method:
9575 * @event: The MonoEvent to act on.
9577 * Returns: the @add' method for the event (a MonoMethod).
9580 mono_event_get_add_method (MonoEvent *event)
9586 * mono_event_get_remove_method:
9587 * @event: The MonoEvent to act on.
9589 * Returns: the @remove method for the event (a MonoMethod).
9592 mono_event_get_remove_method (MonoEvent *event)
9594 return event->remove;
9598 * mono_event_get_raise_method:
9599 * @event: The MonoEvent to act on.
9601 * Returns: the @raise method for the event (a MonoMethod).
9604 mono_event_get_raise_method (MonoEvent *event)
9606 return event->raise;
9610 * mono_event_get_parent:
9611 * @event: the MonoEvent to act on.
9613 * Returns: the MonoClass where the event is defined.
9616 mono_event_get_parent (MonoEvent *event)
9618 return event->parent;
9622 * mono_event_get_flags
9623 * @event: the MonoEvent to act on.
9625 * The metadata flags for an event are encoded using the
9626 * EVENT_* constants. See the tabledefs.h file for details.
9628 * Returns: the flags for the event.
9631 mono_event_get_flags (MonoEvent *event)
9633 return event->attrs;
9637 * mono_class_get_method_from_name:
9638 * @klass: where to look for the method
9639 * @name: name of the method
9640 * @param_count: number of parameters. -1 for any number.
9642 * Obtains a MonoMethod with a given name and number of parameters.
9643 * It only works if there are no multiple signatures for any given method name.
9646 mono_class_get_method_from_name (MonoClass *klass, const char *name, int param_count)
9648 return mono_class_get_method_from_name_flags (klass, name, param_count, 0);
9652 find_method_in_metadata (MonoClass *klass, const char *name, int param_count, int flags)
9654 MonoMethod *res = NULL;
9657 /* Search directly in the metadata to avoid calling setup_methods () */
9658 for (i = 0; i < klass->method.count; ++i) {
9660 guint32 cols [MONO_METHOD_SIZE];
9662 MonoMethodSignature *sig;
9664 /* class->method.first points into the methodptr table */
9665 mono_metadata_decode_table_row (klass->image, MONO_TABLE_METHOD, klass->method.first + i, cols, MONO_METHOD_SIZE);
9667 if (!strcmp (mono_metadata_string_heap (klass->image, cols [MONO_METHOD_NAME]), name)) {
9668 method = mono_get_method_checked (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass, NULL, &error);
9670 mono_error_cleanup (&error); /* FIXME don't swallow the error */
9673 if (param_count == -1) {
9677 sig = mono_method_signature_checked (method, &error);
9679 mono_error_cleanup (&error); /* FIXME don't swallow the error */
9682 if (sig->param_count == param_count) {
9693 * mono_class_get_method_from_name_flags:
9694 * @klass: where to look for the method
9695 * @name_space: name of the method
9696 * @param_count: number of parameters. -1 for any number.
9697 * @flags: flags which must be set in the method
9699 * Obtains a MonoMethod with a given name and number of parameters.
9700 * It only works if there are no multiple signatures for any given method name.
9703 mono_class_get_method_from_name_flags (MonoClass *klass, const char *name, int param_count, int flags)
9705 MonoMethod *res = NULL;
9708 mono_class_init (klass);
9710 if (klass->generic_class && !klass->methods) {
9711 res = mono_class_get_method_from_name_flags (klass->generic_class->container_class, name, param_count, flags);
9714 res = mono_class_inflate_generic_method_full_checked (res, klass, mono_class_get_context (klass), &error);
9715 g_assert (mono_error_ok (&error)); /*FIXME proper error handling*/
9720 if (klass->methods || !MONO_CLASS_HAS_STATIC_METADATA (klass)) {
9721 mono_class_setup_methods (klass);
9723 We can't fail lookup of methods otherwise the runtime will burst in flames on all sort of places.
9724 See mono/tests/array_load_exception.il
9725 FIXME we should better report this error to the caller
9727 if (!klass->methods)
9729 for (i = 0; i < klass->method.count; ++i) {
9730 MonoMethod *method = klass->methods [i];
9732 if (method->name[0] == name [0] &&
9733 !strcmp (name, method->name) &&
9734 (param_count == -1 || mono_method_signature (method)->param_count == param_count) &&
9735 ((method->flags & flags) == flags)) {
9742 res = find_method_in_metadata (klass, name, param_count, flags);
9749 * mono_class_set_failure:
9750 * @klass: class in which the failure was detected
9751 * @ex_type: the kind of exception/error to be thrown (later)
9752 * @ex_data: exception data (specific to each type of exception/error)
9754 * Keep a detected failure informations in the class for later processing.
9755 * Note that only the first failure is kept.
9757 * LOCKING: Acquires the loader lock.
9760 mono_class_set_failure (MonoClass *klass, guint32 ex_type, void *ex_data)
9762 if (klass->exception_type)
9765 mono_loader_lock ();
9766 klass->exception_type = ex_type;
9768 mono_image_property_insert (klass->image, klass, MONO_CLASS_PROP_EXCEPTION_DATA, ex_data);
9769 mono_loader_unlock ();
9775 * mono_class_get_exception_data:
9777 * Return the exception_data property of KLASS.
9779 * LOCKING: Acquires the loader lock.
9782 mono_class_get_exception_data (MonoClass *klass)
9784 return mono_image_property_lookup (klass->image, klass, MONO_CLASS_PROP_EXCEPTION_DATA);
9788 * mono_classes_init:
9790 * Initialize the resources used by this module.
9793 mono_classes_init (void)
9795 mono_mutex_init (&classes_mutex);
9797 mono_counters_register ("Inflated methods size",
9798 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &inflated_methods_size);
9799 mono_counters_register ("Inflated classes",
9800 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &inflated_classes);
9801 mono_counters_register ("Inflated classes size",
9802 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &inflated_classes_size);
9803 mono_counters_register ("MonoClass size",
9804 MONO_COUNTER_METADATA | MONO_COUNTER_INT, &classes_size);
9805 mono_counters_register ("MonoClassExt size",
9806 MONO_COUNTER_METADATA | MONO_COUNTER_INT, &class_ext_size);
9810 * mono_classes_cleanup:
9812 * Free the resources used by this module.
9815 mono_classes_cleanup (void)
9817 if (global_interface_bitset)
9818 mono_bitset_free (global_interface_bitset);
9819 global_interface_bitset = NULL;
9820 mono_mutex_destroy (&classes_mutex);
9824 * mono_class_get_exception_for_failure:
9825 * @klass: class in which the failure was detected
9827 * Return a constructed MonoException than the caller can then throw
9828 * using mono_raise_exception - or NULL if no failure is present (or
9829 * doesn't result in an exception).
9832 mono_class_get_exception_for_failure (MonoClass *klass)
9834 gpointer exception_data = mono_class_get_exception_data (klass);
9836 switch (klass->exception_type) {
9837 case MONO_EXCEPTION_TYPE_LOAD: {
9840 char *str = mono_type_get_full_name (klass);
9841 char *astr = klass->image->assembly? mono_stringify_assembly_name (&klass->image->assembly->aname): NULL;
9842 name = mono_string_new (mono_domain_get (), str);
9844 ex = mono_get_exception_type_load (name, astr);
9848 case MONO_EXCEPTION_MISSING_METHOD: {
9849 char *class_name = exception_data;
9850 char *assembly_name = class_name + strlen (class_name) + 1;
9852 return mono_get_exception_missing_method (class_name, assembly_name);
9854 case MONO_EXCEPTION_MISSING_FIELD: {
9855 char *class_name = exception_data;
9856 char *member_name = class_name + strlen (class_name) + 1;
9858 return mono_get_exception_missing_field (class_name, member_name);
9860 case MONO_EXCEPTION_FILE_NOT_FOUND: {
9861 char *msg_format = exception_data;
9862 char *assembly_name = msg_format + strlen (msg_format) + 1;
9863 char *msg = g_strdup_printf (msg_format, assembly_name);
9866 ex = mono_get_exception_file_not_found2 (msg, mono_string_new (mono_domain_get (), assembly_name));
9872 case MONO_EXCEPTION_BAD_IMAGE: {
9873 return mono_get_exception_bad_image_format (exception_data);
9876 MonoLoaderError *error;
9879 error = mono_loader_get_last_error ();
9881 ex = mono_loader_error_prepare_exception (error);
9885 /* TODO - handle other class related failures */
9892 is_nesting_type (MonoClass *outer_klass, MonoClass *inner_klass)
9894 outer_klass = mono_class_get_generic_type_definition (outer_klass);
9895 inner_klass = mono_class_get_generic_type_definition (inner_klass);
9897 if (outer_klass == inner_klass)
9899 inner_klass = inner_klass->nested_in;
9900 } while (inner_klass);
9905 mono_class_get_generic_type_definition (MonoClass *klass)
9907 return klass->generic_class ? klass->generic_class->container_class : klass;
9911 * Check if @klass is a subtype of @parent ignoring generic instantiations.
9913 * Generic instantiations are ignored for all super types of @klass.
9915 * Visibility checks ignoring generic instantiations.
9918 mono_class_has_parent_and_ignore_generics (MonoClass *klass, MonoClass *parent)
9921 klass = mono_class_get_generic_type_definition (klass);
9922 parent = mono_class_get_generic_type_definition (parent);
9923 mono_class_setup_supertypes (klass);
9925 for (i = 0; i < klass->idepth; ++i) {
9926 if (parent == mono_class_get_generic_type_definition (klass->supertypes [i]))
9932 * Subtype can only access parent members with family protection if the site object
9933 * is subclass of Subtype. For example:
9934 * class A { protected int x; }
9936 * void valid_access () {
9940 * void invalid_access () {
9947 is_valid_family_access (MonoClass *access_klass, MonoClass *member_klass, MonoClass *context_klass)
9949 if (!mono_class_has_parent_and_ignore_generics (access_klass, member_klass))
9952 if (context_klass == NULL)
9954 /*if access_klass is not member_klass context_klass must be type compat*/
9955 if (access_klass != member_klass && !mono_class_has_parent_and_ignore_generics (context_klass, access_klass))
9961 can_access_internals (MonoAssembly *accessing, MonoAssembly* accessed)
9964 if (accessing == accessed)
9966 if (!accessed || !accessing)
9969 /* extra safety under CoreCLR - the runtime does not verify the strongname signatures
9970 * anywhere so untrusted friends are not safe to access platform's code internals */
9971 if (mono_security_core_clr_enabled ()) {
9972 if (!mono_security_core_clr_can_access_internals (accessing->image, accessed->image))
9976 mono_assembly_load_friends (accessed);
9977 for (tmp = accessed->friend_assembly_names; tmp; tmp = tmp->next) {
9978 MonoAssemblyName *friend = tmp->data;
9979 /* Be conservative with checks */
9982 if (strcmp (accessing->aname.name, friend->name))
9984 if (friend->public_key_token [0]) {
9985 if (!accessing->aname.public_key_token [0])
9987 if (!mono_public_tokens_are_equal (friend->public_key_token, accessing->aname.public_key_token))
9996 * If klass is a generic type or if it is derived from a generic type, return the
9997 * MonoClass of the generic definition
9998 * Returns NULL if not found
10001 get_generic_definition_class (MonoClass *klass)
10004 if (klass->generic_class && klass->generic_class->container_class)
10005 return klass->generic_class->container_class;
10006 klass = klass->parent;
10012 can_access_instantiation (MonoClass *access_klass, MonoGenericInst *ginst)
10015 for (i = 0; i < ginst->type_argc; ++i) {
10016 MonoType *type = ginst->type_argv[i];
10017 switch (type->type) {
10018 case MONO_TYPE_SZARRAY:
10019 if (!can_access_type (access_klass, type->data.klass))
10022 case MONO_TYPE_ARRAY:
10023 if (!can_access_type (access_klass, type->data.array->eklass))
10026 case MONO_TYPE_PTR:
10027 if (!can_access_type (access_klass, mono_class_from_mono_type (type->data.type)))
10030 case MONO_TYPE_CLASS:
10031 case MONO_TYPE_VALUETYPE:
10032 case MONO_TYPE_GENERICINST:
10033 if (!can_access_type (access_klass, mono_class_from_mono_type (type)))
10043 can_access_type (MonoClass *access_klass, MonoClass *member_klass)
10047 if (access_klass->image->assembly && access_klass->image->assembly->corlib_internal)
10050 if (access_klass->element_class && !access_klass->enumtype)
10051 access_klass = access_klass->element_class;
10053 if (member_klass->element_class && !member_klass->enumtype)
10054 member_klass = member_klass->element_class;
10056 access_level = member_klass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK;
10058 if (member_klass->byval_arg.type == MONO_TYPE_VAR || member_klass->byval_arg.type == MONO_TYPE_MVAR)
10061 if (member_klass->generic_class && !can_access_instantiation (access_klass, member_klass->generic_class->context.class_inst))
10064 if (is_nesting_type (access_klass, member_klass) || (access_klass->nested_in && is_nesting_type (access_klass->nested_in, member_klass)))
10067 if (member_klass->nested_in && !can_access_type (access_klass, member_klass->nested_in))
10070 /*Non nested type with nested visibility. We just fail it.*/
10071 if (access_level >= TYPE_ATTRIBUTE_NESTED_PRIVATE && access_level <= TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM && member_klass->nested_in == NULL)
10074 switch (access_level) {
10075 case TYPE_ATTRIBUTE_NOT_PUBLIC:
10076 return can_access_internals (access_klass->image->assembly, member_klass->image->assembly);
10078 case TYPE_ATTRIBUTE_PUBLIC:
10081 case TYPE_ATTRIBUTE_NESTED_PUBLIC:
10084 case TYPE_ATTRIBUTE_NESTED_PRIVATE:
10085 return is_nesting_type (member_klass, access_klass);
10087 case TYPE_ATTRIBUTE_NESTED_FAMILY:
10088 return mono_class_has_parent_and_ignore_generics (access_klass, member_klass->nested_in);
10090 case TYPE_ATTRIBUTE_NESTED_ASSEMBLY:
10091 return can_access_internals (access_klass->image->assembly, member_klass->image->assembly);
10093 case TYPE_ATTRIBUTE_NESTED_FAM_AND_ASSEM:
10094 return can_access_internals (access_klass->image->assembly, member_klass->nested_in->image->assembly) &&
10095 mono_class_has_parent_and_ignore_generics (access_klass, member_klass->nested_in);
10097 case TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM:
10098 return can_access_internals (access_klass->image->assembly, member_klass->nested_in->image->assembly) ||
10099 mono_class_has_parent_and_ignore_generics (access_klass, member_klass->nested_in);
10104 /* FIXME: check visibility of type, too */
10106 can_access_member (MonoClass *access_klass, MonoClass *member_klass, MonoClass* context_klass, int access_level)
10108 MonoClass *member_generic_def;
10109 if (access_klass->image->assembly && access_klass->image->assembly->corlib_internal)
10112 if (((access_klass->generic_class && access_klass->generic_class->container_class) ||
10113 access_klass->generic_container) &&
10114 (member_generic_def = get_generic_definition_class (member_klass))) {
10115 MonoClass *access_container;
10117 if (access_klass->generic_container)
10118 access_container = access_klass;
10120 access_container = access_klass->generic_class->container_class;
10122 if (can_access_member (access_container, member_generic_def, context_klass, access_level))
10126 /* Partition I 8.5.3.2 */
10127 /* the access level values are the same for fields and methods */
10128 switch (access_level) {
10129 case FIELD_ATTRIBUTE_COMPILER_CONTROLLED:
10130 /* same compilation unit */
10131 return access_klass->image == member_klass->image;
10132 case FIELD_ATTRIBUTE_PRIVATE:
10133 return access_klass == member_klass;
10134 case FIELD_ATTRIBUTE_FAM_AND_ASSEM:
10135 if (is_valid_family_access (access_klass, member_klass, context_klass) &&
10136 can_access_internals (access_klass->image->assembly, member_klass->image->assembly))
10139 case FIELD_ATTRIBUTE_ASSEMBLY:
10140 return can_access_internals (access_klass->image->assembly, member_klass->image->assembly);
10141 case FIELD_ATTRIBUTE_FAMILY:
10142 if (is_valid_family_access (access_klass, member_klass, context_klass))
10145 case FIELD_ATTRIBUTE_FAM_OR_ASSEM:
10146 if (is_valid_family_access (access_klass, member_klass, context_klass))
10148 return can_access_internals (access_klass->image->assembly, member_klass->image->assembly);
10149 case FIELD_ATTRIBUTE_PUBLIC:
10156 mono_method_can_access_field (MonoMethod *method, MonoClassField *field)
10158 /* FIXME: check all overlapping fields */
10159 int can = can_access_member (method->klass, field->parent, NULL, mono_field_get_type (field)->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK);
10161 MonoClass *nested = method->klass->nested_in;
10163 can = can_access_member (nested, field->parent, NULL, mono_field_get_type (field)->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK);
10166 nested = nested->nested_in;
10173 mono_method_can_access_method (MonoMethod *method, MonoMethod *called)
10175 int can = can_access_member (method->klass, called->klass, NULL, called->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK);
10177 MonoClass *nested = method->klass->nested_in;
10179 can = can_access_member (nested, called->klass, NULL, called->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK);
10182 nested = nested->nested_in;
10187 * with generics calls to explicit interface implementations can be expressed
10188 * directly: the method is private, but we must allow it. This may be opening
10189 * a hole or the generics code should handle this differently.
10190 * Maybe just ensure the interface type is public.
10192 if ((called->flags & METHOD_ATTRIBUTE_VIRTUAL) && (called->flags & METHOD_ATTRIBUTE_FINAL))
10198 * mono_method_can_access_method_full:
10199 * @method: The caller method
10200 * @called: The called method
10201 * @context_klass: The static type on stack of the owner @called object used
10203 * This function must be used with instance calls, as they have more strict family accessibility.
10204 * It can be used with static methods, but context_klass should be NULL.
10206 * Returns: TRUE if caller have proper visibility and acessibility to @called
10209 mono_method_can_access_method_full (MonoMethod *method, MonoMethod *called, MonoClass *context_klass)
10211 MonoClass *access_class = method->klass;
10212 MonoClass *member_class = called->klass;
10213 int can = can_access_member (access_class, member_class, context_klass, called->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK);
10215 MonoClass *nested = access_class->nested_in;
10217 can = can_access_member (nested, member_class, context_klass, called->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK);
10220 nested = nested->nested_in;
10227 can = can_access_type (access_class, member_class);
10229 MonoClass *nested = access_class->nested_in;
10231 can = can_access_type (nested, member_class);
10234 nested = nested->nested_in;
10241 if (called->is_inflated) {
10242 MonoMethodInflated * infl = (MonoMethodInflated*)called;
10243 if (infl->context.method_inst && !can_access_instantiation (access_class, infl->context.method_inst))
10252 * mono_method_can_access_field_full:
10253 * @method: The caller method
10254 * @field: The accessed field
10255 * @context_klass: The static type on stack of the owner @field object used
10257 * This function must be used with instance fields, as they have more strict family accessibility.
10258 * It can be used with static fields, but context_klass should be NULL.
10260 * Returns: TRUE if caller have proper visibility and acessibility to @field
10263 mono_method_can_access_field_full (MonoMethod *method, MonoClassField *field, MonoClass *context_klass)
10265 MonoClass *access_class = method->klass;
10266 MonoClass *member_class = field->parent;
10267 /* FIXME: check all overlapping fields */
10268 int can = can_access_member (access_class, member_class, context_klass, field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK);
10270 MonoClass *nested = access_class->nested_in;
10272 can = can_access_member (nested, member_class, context_klass, field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK);
10275 nested = nested->nested_in;
10282 can = can_access_type (access_class, member_class);
10284 MonoClass *nested = access_class->nested_in;
10286 can = can_access_type (nested, member_class);
10289 nested = nested->nested_in;
10299 * mono_class_can_access_class:
10300 * @source_class: The source class
10301 * @target_class: The accessed class
10303 * This function returns is @target_class is visible to @source_class
10305 * Returns: TRUE if source have proper visibility and acessibility to target
10308 mono_class_can_access_class (MonoClass *source_class, MonoClass *target_class)
10310 return can_access_type (source_class, target_class);
10314 * mono_type_is_valid_enum_basetype:
10315 * @type: The MonoType to check
10317 * Returns: TRUE if the type can be used as the basetype of an enum
10319 gboolean mono_type_is_valid_enum_basetype (MonoType * type) {
10320 switch (type->type) {
10323 case MONO_TYPE_BOOLEAN:
10326 case MONO_TYPE_CHAR:
10340 * mono_class_is_valid_enum:
10341 * @klass: An enum class to be validated
10343 * This method verify the required properties an enum should have.
10345 * Returns: TRUE if the informed enum class is valid
10347 * FIXME: TypeBuilder enums are allowed to implement interfaces, but since they cannot have methods, only empty interfaces are possible
10348 * FIXME: enum types are not allowed to have a cctor, but mono_reflection_create_runtime_class sets has_cctor to 1 for all types
10349 * FIXME: TypeBuilder enums can have any kind of static fields, but the spec is very explicit about that (P II 14.3)
10351 gboolean mono_class_is_valid_enum (MonoClass *klass) {
10352 MonoClassField * field;
10353 gpointer iter = NULL;
10354 gboolean found_base_field = FALSE;
10356 g_assert (klass->enumtype);
10357 /* we cannot test against mono_defaults.enum_class, or mcs won't be able to compile the System namespace*/
10358 if (!klass->parent || strcmp (klass->parent->name, "Enum") || strcmp (klass->parent->name_space, "System") ) {
10362 if ((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) != TYPE_ATTRIBUTE_AUTO_LAYOUT)
10365 while ((field = mono_class_get_fields (klass, &iter))) {
10366 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
10367 if (found_base_field)
10369 found_base_field = TRUE;
10370 if (!mono_type_is_valid_enum_basetype (field->type))
10375 if (!found_base_field)
10378 if (klass->method.count > 0)
10385 mono_generic_class_is_generic_type_definition (MonoGenericClass *gklass)
10387 return gklass->context.class_inst == gklass->container_class->generic_container->context.class_inst;
10391 * mono_class_setup_interface_id:
10393 * Initializes MonoClass::interface_id if required.
10395 * LOCKING: Acquires the loader lock.
10398 mono_class_setup_interface_id (MonoClass *class)
10400 mono_loader_lock ();
10401 if (MONO_CLASS_IS_INTERFACE (class) && !class->interface_id)
10402 class->interface_id = mono_get_unique_iid (class);
10403 mono_loader_unlock ();
10407 * mono_class_alloc_ext:
10409 * Allocate klass->ext if not already done.
10412 mono_class_alloc_ext (MonoClass *klass)
10419 ext = mono_class_alloc0 (klass, sizeof (MonoClassExt));
10420 mono_image_lock (klass->image);
10421 mono_memory_barrier ();
10424 class_ext_size += sizeof (MonoClassExt);
10425 mono_image_unlock (klass->image);
10429 * mono_class_setup_interfaces:
10431 * Initialize class->interfaces/interfaces_count.
10432 * LOCKING: Acquires the loader lock.
10433 * This function can fail the type.
10436 mono_class_setup_interfaces (MonoClass *klass, MonoError *error)
10438 int i, interface_count;
10439 MonoClass **interfaces;
10441 mono_error_init (error);
10443 if (klass->interfaces_inited)
10446 if (klass->rank == 1 && klass->byval_arg.type != MONO_TYPE_ARRAY) {
10447 MonoType *args [1];
10449 /* generic IList, ICollection, IEnumerable */
10450 interface_count = mono_defaults.generic_ireadonlylist_class ? 2 : 1;
10451 interfaces = mono_image_alloc0 (klass->image, sizeof (MonoClass*) * interface_count);
10453 args [0] = &klass->element_class->byval_arg;
10454 interfaces [0] = mono_class_bind_generic_parameters (
10455 mono_defaults.generic_ilist_class, 1, args, FALSE);
10456 if (interface_count > 1)
10457 interfaces [1] = mono_class_bind_generic_parameters (
10458 mono_defaults.generic_ireadonlylist_class, 1, args, FALSE);
10459 } else if (klass->generic_class) {
10460 MonoClass *gklass = klass->generic_class->container_class;
10462 mono_class_setup_interfaces (gklass, error);
10463 if (!mono_error_ok (error)) {
10464 mono_class_set_failure (klass, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Could not setup the interfaces"));
10468 interface_count = gklass->interface_count;
10469 interfaces = mono_class_new0 (klass, MonoClass *, interface_count);
10470 for (i = 0; i < interface_count; i++) {
10471 interfaces [i] = mono_class_inflate_generic_class_checked (gklass->interfaces [i], mono_generic_class_get_context (klass->generic_class), error);
10472 if (!mono_error_ok (error)) {
10473 mono_class_set_failure (klass, MONO_EXCEPTION_TYPE_LOAD, g_strdup ("Could not setup the interfaces"));
10478 interface_count = 0;
10482 mono_image_lock (klass->image);
10484 if (!klass->interfaces_inited) {
10485 klass->interface_count = interface_count;
10486 klass->interfaces = interfaces;
10488 mono_memory_barrier ();
10490 klass->interfaces_inited = TRUE;
10493 mono_image_unlock (klass->image);
10497 mono_field_resolve_type (MonoClassField *field, MonoError *error)
10499 MonoClass *class = field->parent;
10500 MonoImage *image = class->image;
10501 MonoClass *gtd = class->generic_class ? mono_class_get_generic_type_definition (class) : NULL;
10502 int field_idx = field - class->fields;
10504 mono_error_init (error);
10507 MonoClassField *gfield = >d->fields [field_idx];
10508 MonoType *gtype = mono_field_get_type_checked (gfield, error);
10509 if (!mono_error_ok (error)) {
10510 char *err_msg = g_strdup_printf ("Could not load field %d type due to: %s", field_idx, mono_error_get_message (error));
10511 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, err_msg);
10515 field->type = mono_class_inflate_generic_type_no_copy (image, gtype, mono_class_get_context (class), error);
10516 if (!mono_error_ok (error)) {
10517 char *err_msg = g_strdup_printf ("Could not load field %d type due to: %s", field_idx, mono_error_get_message (error));
10518 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, err_msg);
10523 guint32 cols [MONO_FIELD_SIZE];
10524 MonoGenericContainer *container = NULL;
10525 int idx = class->field.first + field_idx;
10527 /*FIXME, in theory we do not lazy load SRE fields*/
10528 g_assert (!image_is_dynamic (image));
10530 if (class->generic_container) {
10531 container = class->generic_container;
10533 container = gtd->generic_container;
10534 g_assert (container);
10537 /* class->field.first and idx points into the fieldptr table */
10538 mono_metadata_decode_table_row (image, MONO_TABLE_FIELD, idx, cols, MONO_FIELD_SIZE);
10540 if (!mono_verifier_verify_field_signature (image, cols [MONO_FIELD_SIGNATURE], NULL)) {
10541 mono_error_set_type_load_class (error, class, "Could not verify field %s signature", field->name);
10542 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
10546 sig = mono_metadata_blob_heap (image, cols [MONO_FIELD_SIGNATURE]);
10548 mono_metadata_decode_value (sig, &sig);
10549 /* FIELD signature == 0x06 */
10550 g_assert (*sig == 0x06);
10551 field->type = mono_metadata_parse_type_full (image, container, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
10553 mono_class_set_failure_from_loader_error (class, error, g_strdup_printf ("Could not load field %s type", field->name));
10558 mono_field_resolve_flags (MonoClassField *field)
10560 MonoClass *class = field->parent;
10561 MonoImage *image = class->image;
10562 MonoClass *gtd = class->generic_class ? mono_class_get_generic_type_definition (class) : NULL;
10563 int field_idx = field - class->fields;
10567 MonoClassField *gfield = >d->fields [field_idx];
10568 return mono_field_get_flags (gfield);
10570 int idx = class->field.first + field_idx;
10572 /*FIXME, in theory we do not lazy load SRE fields*/
10573 g_assert (!image_is_dynamic (image));
10575 return mono_metadata_decode_table_row_col (image, MONO_TABLE_FIELD, idx, MONO_FIELD_FLAGS);
10580 * mono_class_setup_basic_field_info:
10581 * @class: The class to initialize
10583 * Initializes the class->fields array of fields.
10584 * Aquires the loader lock.
10587 mono_class_setup_basic_field_info_locking (MonoClass *class)
10589 mono_loader_lock ();
10590 mono_class_setup_basic_field_info (class);
10591 mono_loader_unlock ();
10595 * mono_class_get_fields_lazy:
10596 * @klass: the MonoClass to act on
10598 * This routine is an iterator routine for retrieving the fields in a class.
10599 * Only minimal information about fields are loaded. Accessors must be used
10600 * for all MonoClassField returned.
10602 * You must pass a gpointer that points to zero and is treated as an opaque handle to
10603 * iterate over all of the elements. When no more values are
10604 * available, the return value is NULL.
10606 * Returns: a @MonoClassField* on each iteration, or NULL when no more fields are available.
10609 mono_class_get_fields_lazy (MonoClass* klass, gpointer *iter)
10611 MonoClassField* field;
10615 mono_class_setup_basic_field_info_locking (klass);
10616 if (!klass->fields)
10618 /* start from the first */
10619 if (klass->field.count) {
10620 return *iter = &klass->fields [0];
10628 if (field < &klass->fields [klass->field.count]) {
10629 return *iter = field;
10635 mono_class_full_name (MonoClass *klass)
10637 return mono_type_full_name (&klass->byval_arg);