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)
19 #include <mono/io-layer/atomic.h>
21 #include <mono/metadata/image.h>
22 #include <mono/metadata/assembly.h>
23 #include <mono/metadata/metadata.h>
24 #include <mono/metadata/metadata-internals.h>
25 #include <mono/metadata/profiler-private.h>
26 #include <mono/metadata/tabledefs.h>
27 #include <mono/metadata/tokentype.h>
28 #include <mono/metadata/class-internals.h>
29 #include <mono/metadata/object.h>
30 #include <mono/metadata/appdomain.h>
31 #include <mono/metadata/mono-endian.h>
32 #include <mono/metadata/debug-helpers.h>
33 #include <mono/metadata/reflection.h>
34 #include <mono/metadata/exception.h>
35 #include <mono/metadata/security-manager.h>
36 #include <mono/metadata/security-core-clr.h>
37 #include <mono/metadata/attrdefs.h>
38 #include <mono/metadata/gc-internal.h>
39 #include <mono/metadata/verify-internals.h>
40 #include <mono/metadata/mono-debug.h>
41 #include <mono/utils/mono-counters.h>
45 gboolean mono_print_vtable = FALSE;
48 * Controls whenever mono_class_init () constructs a generic vtable. This is TRUE by
49 * default to avoid breaking embedding apps, but set to FALSE by the runtime executable
52 gboolean mono_setup_vtable_in_class_init = TRUE;
55 guint32 inflated_classes, inflated_classes_size, inflated_methods_size;
56 guint32 classes_size, class_ext_size;
58 /* Function supplied by the runtime to find classes by name using information from the AOT file */
59 static MonoGetClassFromName get_class_from_name = NULL;
61 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
62 static gboolean mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res);
63 static gboolean can_access_type (MonoClass *access_klass, MonoClass *member_klass);
64 static MonoMethod* find_method_in_metadata (MonoClass *klass, const char *name, int param_count, int flags);
65 static int generic_array_methods (MonoClass *class);
66 static void setup_generic_array_ifaces (MonoClass *class, MonoClass *iface, MonoMethod **methods, int pos);
68 static MonoMethod* mono_class_get_virtual_methods (MonoClass* klass, gpointer *iter);
70 void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
71 void (*mono_debugger_class_loaded_methods_func) (MonoClass *klass) = NULL;
74 * mono_class_from_typeref:
76 * @type_token: a TypeRef token
78 * Creates the MonoClass* structure representing the type defined by
79 * the typeref token valid inside @image.
80 * Returns: the MonoClass* representing the typeref token, NULL ifcould
84 mono_class_from_typeref (MonoImage *image, guint32 type_token)
86 guint32 cols [MONO_TYPEREF_SIZE];
87 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
89 const char *name, *nspace;
93 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
95 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
96 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
98 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
99 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
100 case MONO_RESOLTION_SCOPE_MODULE:
102 g_error ("null ResolutionScope not yet handled");
103 /* a typedef in disguise */
104 return mono_class_from_name (image, nspace, name);
105 case MONO_RESOLTION_SCOPE_MODULEREF:
106 module = mono_image_load_module (image, idx);
108 return mono_class_from_name (module, nspace, name);
110 char *msg = g_strdup_printf ("%s%s%s", nspace, nspace [0] ? "." : "", name);
113 human_name = mono_stringify_assembly_name (&image->assembly->aname);
114 mono_loader_set_error_type_load (msg, human_name);
120 case MONO_RESOLTION_SCOPE_TYPEREF: {
121 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
124 if (enclosing->nested_classes_inited && enclosing->ext) {
125 /* Micro-optimization: don't scan the metadata tables if enclosing is already inited */
126 for (tmp = enclosing->ext->nested_classes; tmp; tmp = tmp->next) {
128 if (strcmp (res->name, name) == 0)
132 /* Don't call mono_class_init as we might've been called by it recursively */
133 int i = mono_metadata_nesting_typedef (enclosing->image, enclosing->type_token, 1);
135 guint32 class_nested = mono_metadata_decode_row_col (&enclosing->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, MONO_NESTED_CLASS_NESTED);
136 guint32 string_offset = mono_metadata_decode_row_col (&enclosing->image->tables [MONO_TABLE_TYPEDEF], class_nested - 1, MONO_TYPEDEF_NAME);
137 const char *nname = mono_metadata_string_heap (enclosing->image, string_offset);
139 if (strcmp (nname, name) == 0)
140 return mono_class_create_from_typedef (enclosing->image, MONO_TOKEN_TYPE_DEF | class_nested);
142 i = mono_metadata_nesting_typedef (enclosing->image, enclosing->type_token, i + 1);
145 g_warning ("TypeRef ResolutionScope not yet handled (%d) for %s.%s in image %s", idx, nspace, name, image->name);
148 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
152 if (!image->references || !image->references [idx - 1])
153 mono_assembly_load_reference (image, idx - 1);
154 g_assert (image->references [idx - 1]);
156 /* If the assembly did not load, register this as a type load exception */
157 if (image->references [idx - 1] == REFERENCE_MISSING){
158 MonoAssemblyName aname;
161 mono_assembly_get_assemblyref (image, idx - 1, &aname);
162 human_name = mono_stringify_assembly_name (&aname);
163 mono_loader_set_error_assembly_load (human_name, image->assembly->ref_only);
169 return mono_class_from_name (image->references [idx - 1]->image, nspace, name);
174 mono_image_memdup (MonoImage *image, void *data, guint size)
176 void *res = mono_image_alloc (image, size);
177 memcpy (res, data, size);
181 /* Copy everything mono_metadata_free_array free. */
183 mono_dup_array_type (MonoImage *image, MonoArrayType *a)
186 a = mono_image_memdup (image, a, sizeof (MonoArrayType));
188 a->sizes = mono_image_memdup (image, a->sizes, a->numsizes * sizeof (int));
190 a->lobounds = mono_image_memdup (image, a->lobounds, a->numlobounds * sizeof (int));
192 a = g_memdup (a, sizeof (MonoArrayType));
194 a->sizes = g_memdup (a->sizes, a->numsizes * sizeof (int));
196 a->lobounds = g_memdup (a->lobounds, a->numlobounds * sizeof (int));
201 /* Copy everything mono_metadata_free_method_signature free. */
203 mono_metadata_signature_deep_dup (MonoImage *image, MonoMethodSignature *sig)
207 sig = mono_metadata_signature_dup_full (image, sig);
209 sig->ret = mono_metadata_type_dup (image, sig->ret);
210 for (i = 0; i < sig->param_count; ++i)
211 sig->params [i] = mono_metadata_type_dup (image, sig->params [i]);
217 _mono_type_get_assembly_name (MonoClass *klass, GString *str)
219 MonoAssembly *ta = klass->image->assembly;
221 g_string_append_printf (
222 str, ", %s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s%s",
224 ta->aname.major, ta->aname.minor, ta->aname.build, ta->aname.revision,
225 ta->aname.culture && *ta->aname.culture? ta->aname.culture: "neutral",
226 ta->aname.public_key_token [0] ? (char *)ta->aname.public_key_token : "null",
227 (ta->aname.flags & ASSEMBLYREF_RETARGETABLE_FLAG) ? ", Retargetable=Yes" : "");
231 mono_type_name_check_byref (MonoType *type, GString *str)
234 g_string_append_c (str, '&');
238 mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed,
239 MonoTypeNameFormat format)
243 switch (type->type) {
244 case MONO_TYPE_ARRAY: {
245 int i, rank = type->data.array->rank;
246 MonoTypeNameFormat nested_format;
248 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
249 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
251 mono_type_get_name_recurse (
252 &type->data.array->eklass->byval_arg, str, FALSE, nested_format);
253 g_string_append_c (str, '[');
255 g_string_append_c (str, '*');
256 for (i = 1; i < rank; i++)
257 g_string_append_c (str, ',');
258 g_string_append_c (str, ']');
260 mono_type_name_check_byref (type, str);
262 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
263 _mono_type_get_assembly_name (type->data.array->eklass, str);
266 case MONO_TYPE_SZARRAY: {
267 MonoTypeNameFormat nested_format;
269 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
270 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
272 mono_type_get_name_recurse (
273 &type->data.klass->byval_arg, str, FALSE, nested_format);
274 g_string_append (str, "[]");
276 mono_type_name_check_byref (type, str);
278 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
279 _mono_type_get_assembly_name (type->data.klass, str);
282 case MONO_TYPE_PTR: {
283 MonoTypeNameFormat nested_format;
285 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
286 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
288 mono_type_get_name_recurse (
289 type->data.type, str, FALSE, nested_format);
290 g_string_append_c (str, '*');
292 mono_type_name_check_byref (type, str);
294 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
295 _mono_type_get_assembly_name (mono_class_from_mono_type (type->data.type), str);
300 if (!mono_generic_param_info (type->data.generic_param))
301 g_string_append_printf (str, "%s%d", type->type == MONO_TYPE_VAR ? "!" : "!!", type->data.generic_param->num);
303 g_string_append (str, mono_generic_param_info (type->data.generic_param)->name);
305 mono_type_name_check_byref (type, str);
309 klass = mono_class_from_mono_type (type);
310 if (klass->nested_in) {
311 mono_type_get_name_recurse (
312 &klass->nested_in->byval_arg, str, TRUE, format);
313 if (format == MONO_TYPE_NAME_FORMAT_IL)
314 g_string_append_c (str, '.');
316 g_string_append_c (str, '+');
317 } else if (*klass->name_space) {
318 g_string_append (str, klass->name_space);
319 g_string_append_c (str, '.');
321 if (format == MONO_TYPE_NAME_FORMAT_IL) {
322 char *s = strchr (klass->name, '`');
323 int len = s ? s - klass->name : strlen (klass->name);
325 g_string_append_len (str, klass->name, len);
327 g_string_append (str, klass->name);
330 if (klass->generic_class) {
331 MonoGenericClass *gclass = klass->generic_class;
332 MonoGenericInst *inst = gclass->context.class_inst;
333 MonoTypeNameFormat nested_format;
336 nested_format = format == MONO_TYPE_NAME_FORMAT_FULL_NAME ?
337 MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED : format;
339 if (format == MONO_TYPE_NAME_FORMAT_IL)
340 g_string_append_c (str, '<');
342 g_string_append_c (str, '[');
343 for (i = 0; i < inst->type_argc; i++) {
344 MonoType *t = inst->type_argv [i];
347 g_string_append_c (str, ',');
348 if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
349 (t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
350 g_string_append_c (str, '[');
351 mono_type_get_name_recurse (inst->type_argv [i], str, FALSE, nested_format);
352 if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
353 (t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
354 g_string_append_c (str, ']');
356 if (format == MONO_TYPE_NAME_FORMAT_IL)
357 g_string_append_c (str, '>');
359 g_string_append_c (str, ']');
360 } else if (klass->generic_container &&
361 (format != MONO_TYPE_NAME_FORMAT_FULL_NAME) &&
362 (format != MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)) {
365 if (format == MONO_TYPE_NAME_FORMAT_IL)
366 g_string_append_c (str, '<');
368 g_string_append_c (str, '[');
369 for (i = 0; i < klass->generic_container->type_argc; i++) {
371 g_string_append_c (str, ',');
372 g_string_append (str, mono_generic_container_get_param_info (klass->generic_container, i)->name);
374 if (format == MONO_TYPE_NAME_FORMAT_IL)
375 g_string_append_c (str, '>');
377 g_string_append_c (str, ']');
380 mono_type_name_check_byref (type, str);
382 if ((format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
383 (type->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
384 _mono_type_get_assembly_name (klass, str);
390 * mono_type_get_name:
392 * @format: the format for the return string.
395 * Returns: the string representation in a number of formats:
397 * if format is MONO_TYPE_NAME_FORMAT_REFLECTION, the return string is
398 * returned in the formatrequired by System.Reflection, this is the
399 * inverse of mono_reflection_parse_type ().
401 * if format is MONO_TYPE_NAME_FORMAT_IL, it returns a syntax that can
402 * be used by the IL assembler.
404 * if format is MONO_TYPE_NAME_FORMAT_FULL_NAME
406 * if format is MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
409 mono_type_get_name_full (MonoType *type, MonoTypeNameFormat format)
413 result = g_string_new ("");
415 mono_type_get_name_recurse (type, result, FALSE, format);
417 return g_string_free (result, FALSE);
421 * mono_type_get_full_name:
424 * Returns: the string representation for type as required by System.Reflection.
425 * The inverse of mono_reflection_parse_type ().
428 mono_type_get_full_name (MonoClass *class)
430 return mono_type_get_name_full (mono_class_get_type (class), MONO_TYPE_NAME_FORMAT_REFLECTION);
434 * mono_type_get_name:
437 * Returns: the string representation for type as it would be represented in IL code.
440 mono_type_get_name (MonoType *type)
442 return mono_type_get_name_full (type, MONO_TYPE_NAME_FORMAT_IL);
446 * mono_type_get_underlying_type:
449 * Returns: the MonoType for the underlying integer type if @type
450 * is an enum and byref is false, otherwise the type itself.
453 mono_type_get_underlying_type (MonoType *type)
455 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype && !type->byref)
456 return mono_class_enum_basetype (type->data.klass);
457 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype && !type->byref)
458 return mono_class_enum_basetype (type->data.generic_class->container_class);
463 * mono_class_is_open_constructed_type:
466 * Returns TRUE if type represents a generics open constructed type
467 * (not all the type parameters required for the instantiation have
471 mono_class_is_open_constructed_type (MonoType *t)
477 case MONO_TYPE_SZARRAY:
478 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
479 case MONO_TYPE_ARRAY:
480 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
482 return mono_class_is_open_constructed_type (t->data.type);
483 case MONO_TYPE_GENERICINST:
484 return t->data.generic_class->context.class_inst->is_open;
491 inflate_generic_type (MonoImage *image, MonoType *type, MonoGenericContext *context)
493 switch (type->type) {
494 case MONO_TYPE_MVAR: {
496 int num = mono_type_get_generic_param_num (type);
497 MonoGenericInst *inst = context->method_inst;
498 if (!inst || !inst->type_argv)
500 if (num >= inst->type_argc)
501 g_error ("MVAR %d (%s) cannot be expanded in this context with %d instantiations",
502 num, mono_generic_param_info (type->data.generic_param)->name, inst->type_argc);
505 * Note that the VAR/MVAR cases are different from the rest. The other cases duplicate @type,
506 * while the VAR/MVAR duplicates a type from the context. So, we need to ensure that the
507 * ->byref and ->attrs from @type are propagated to the returned type.
509 nt = mono_metadata_type_dup (image, inst->type_argv [num]);
510 nt->byref = type->byref;
511 nt->attrs = type->attrs;
514 case MONO_TYPE_VAR: {
516 int num = mono_type_get_generic_param_num (type);
517 MonoGenericInst *inst = context->class_inst;
520 if (num >= inst->type_argc)
521 g_error ("VAR %d (%s) cannot be expanded in this context with %d instantiations",
522 num, mono_generic_param_info (type->data.generic_param)->name, inst->type_argc);
523 nt = mono_metadata_type_dup (image, inst->type_argv [num]);
524 nt->byref = type->byref;
525 nt->attrs = type->attrs;
528 case MONO_TYPE_SZARRAY: {
529 MonoClass *eclass = type->data.klass;
530 MonoType *nt, *inflated = inflate_generic_type (NULL, &eclass->byval_arg, context);
533 nt = mono_metadata_type_dup (image, type);
534 nt->data.klass = mono_class_from_mono_type (inflated);
535 mono_metadata_free_type (inflated);
538 case MONO_TYPE_ARRAY: {
539 MonoClass *eclass = type->data.array->eklass;
540 MonoType *nt, *inflated = inflate_generic_type (NULL, &eclass->byval_arg, context);
543 nt = mono_metadata_type_dup (image, type);
544 nt->data.array = g_memdup (nt->data.array, sizeof (MonoArrayType));
545 nt->data.array->eklass = mono_class_from_mono_type (inflated);
546 mono_metadata_free_type (inflated);
549 case MONO_TYPE_GENERICINST: {
550 MonoGenericClass *gclass = type->data.generic_class;
551 MonoGenericInst *inst;
553 if (!gclass->context.class_inst->is_open)
556 inst = mono_metadata_inflate_generic_inst (gclass->context.class_inst, context);
557 if (inst != gclass->context.class_inst)
558 gclass = mono_metadata_lookup_generic_class (gclass->container_class, inst, gclass->is_dynamic);
560 if (gclass == type->data.generic_class)
563 nt = mono_metadata_type_dup (image, type);
564 nt->data.generic_class = gclass;
567 case MONO_TYPE_CLASS:
568 case MONO_TYPE_VALUETYPE: {
569 MonoClass *klass = type->data.klass;
570 MonoGenericContainer *container = klass->generic_container;
571 MonoGenericInst *inst;
572 MonoGenericClass *gclass = NULL;
578 /* We can't use context->class_inst directly, since it can have more elements */
579 inst = mono_metadata_inflate_generic_inst (container->context.class_inst, context);
580 if (inst == container->context.class_inst)
583 gclass = mono_metadata_lookup_generic_class (klass, inst, klass->image->dynamic);
585 nt = mono_metadata_type_dup (image, type);
586 nt->type = MONO_TYPE_GENERICINST;
587 nt->data.generic_class = gclass;
597 mono_generic_class_get_context (MonoGenericClass *gclass)
599 return &gclass->context;
603 mono_class_get_context (MonoClass *class)
605 return class->generic_class ? mono_generic_class_get_context (class->generic_class) : NULL;
609 * mono_class_get_generic_container:
611 * Return the generic container of KLASS which should be a generic type definition.
613 MonoGenericContainer*
614 mono_class_get_generic_container (MonoClass *klass)
616 g_assert (klass->is_generic);
618 return klass->generic_container;
622 * mono_class_get_generic_class:
624 * Return the MonoGenericClass of KLASS, which should be a generic instance.
627 mono_class_get_generic_class (MonoClass *klass)
629 g_assert (klass->is_inflated);
631 return klass->generic_class;
635 * mono_class_inflate_generic_type_with_mempool:
636 * @mempool: a mempool
638 * @context: a generics context
640 * The same as mono_class_inflate_generic_type, but allocates the MonoType
641 * from mempool if it is non-NULL. If it is NULL, the MonoType is
642 * allocated on the heap and is owned by the caller.
643 * The returned type can potentially be the same as TYPE, so it should not be
644 * modified by the caller, and it should be freed using mono_metadata_free_type ().
647 mono_class_inflate_generic_type_with_mempool (MonoImage *image, MonoType *type, MonoGenericContext *context)
649 MonoType *inflated = NULL;
652 inflated = inflate_generic_type (image, type, context);
655 MonoType *shared = mono_metadata_get_shared_type (type);
660 return mono_metadata_type_dup (image, type);
664 mono_stats.inflated_type_count++;
669 * mono_class_inflate_generic_type:
671 * @context: a generics context
673 * If @type is a generic type and @context is not NULL, instantiate it using the
674 * generics context @context.
676 * Returns: the instantiated type or a copy of @type. The returned MonoType is allocated
677 * on the heap and is owned by the caller.
680 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
682 return mono_class_inflate_generic_type_with_mempool (NULL, type, context);
686 * mono_class_inflate_generic_type_no_copy:
688 * Same as inflate_generic_type_with_mempool, but return TYPE if no inflation
692 mono_class_inflate_generic_type_no_copy (MonoImage *image, MonoType *type, MonoGenericContext *context)
694 MonoType *inflated = NULL;
697 inflated = inflate_generic_type (image, type, context);
702 mono_stats.inflated_type_count++;
707 * mono_class_inflate_generic_class:
709 * Inflate the class GKLASS with CONTEXT.
712 mono_class_inflate_generic_class (MonoClass *gklass, MonoGenericContext *context)
717 inflated = mono_class_inflate_generic_type (&gklass->byval_arg, context);
719 res = mono_class_from_mono_type (inflated);
720 mono_metadata_free_type (inflated);
725 static MonoGenericContext
726 inflate_generic_context (MonoGenericContext *context, MonoGenericContext *inflate_with)
728 MonoGenericInst *class_inst = NULL;
729 MonoGenericInst *method_inst = NULL;
730 MonoGenericContext res;
732 if (context->class_inst)
733 class_inst = mono_metadata_inflate_generic_inst (context->class_inst, inflate_with);
735 if (context->method_inst)
736 method_inst = mono_metadata_inflate_generic_inst (context->method_inst, inflate_with);
738 res.class_inst = class_inst;
739 res.method_inst = method_inst;
745 * mono_class_inflate_generic_method:
746 * @method: a generic method
747 * @context: a generics context
749 * Instantiate the generic method @method using the generics context @context.
751 * Returns: the new instantiated method
754 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context)
756 return mono_class_inflate_generic_method_full (method, NULL, context);
760 * mono_class_inflate_generic_method:
762 * Instantiate method @method with the generic context @context.
763 * BEWARE: All non-trivial fields are invalid, including klass, signature, and header.
764 * Use mono_method_signature () and mono_method_get_header () to get the correct values.
767 mono_class_inflate_generic_method_full (MonoMethod *method, MonoClass *klass_hint, MonoGenericContext *context)
770 MonoMethodInflated *iresult, *cached;
771 MonoMethodSignature *sig;
772 MonoGenericContext tmp_context;
773 gboolean is_mb_open = FALSE;
775 /* The `method' has already been instantiated before => we need to peel out the instantiation and create a new context */
776 while (method->is_inflated) {
777 MonoGenericContext *method_context = mono_method_get_context (method);
778 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
780 tmp_context = inflate_generic_context (method_context, context);
781 context = &tmp_context;
783 if (mono_metadata_generic_context_equal (method_context, context))
786 method = imethod->declaring;
789 if (!method->is_generic && !method->klass->generic_container)
793 * The reason for this hack is to fix the behavior of inflating generic methods that come from a MethodBuilder.
794 * What happens is that instantiating a generic MethodBuilder with its own arguments should create a diferent object.
795 * This is opposite to the way non-SRE MethodInfos behave.
797 * This happens, for example, when we want to emit a recursive generic method. Given the following C# code:
799 * void Example<T> () {
803 * In Example, the method token must be encoded as: "void Example<!!0>()"
805 * The reference to the first generic argument, "!!0", must be explicit otherwise it won't be inflated
806 * properly. To get that we need to inflate the MethodBuilder with its own arguments.
808 * On the other hand, inflating a non-SRE generic method with its own arguments should
809 * return itself. For example:
811 * MethodInfo m = ... //m is a generic method definition
812 * MethodInfo res = m.MakeGenericMethod (m.GetGenericArguments ());
815 * To allow such scenarios we must allow inflation of MethodBuilder to happen in a diferent way than
816 * what happens with regular methods.
818 * There is one last touch to this madness, once a TypeBuilder is finished, IOW CreateType() is called,
819 * everything should behave like a regular type or method.
822 is_mb_open = method->is_generic &&
823 method->klass->image->dynamic && !method->klass->wastypebuilder && /* that is a MethodBuilder from an unfinished TypeBuilder */
824 context->method_inst == mono_method_get_generic_container (method)->context.method_inst; /* and it's been instantiated with its own arguments. */
826 iresult = g_new0 (MonoMethodInflated, 1);
827 iresult->context = *context;
828 iresult->declaring = method;
829 iresult->method.method.is_mb_open = is_mb_open;
831 if (!context->method_inst && method->is_generic)
832 iresult->context.method_inst = mono_method_get_generic_container (method)->context.method_inst;
834 if (!context->class_inst) {
835 g_assert (!iresult->declaring->klass->generic_class);
836 if (iresult->declaring->klass->generic_container)
837 iresult->context.class_inst = iresult->declaring->klass->generic_container->context.class_inst;
838 else if (iresult->declaring->klass->generic_class)
839 iresult->context.class_inst = iresult->declaring->klass->generic_class->context.class_inst;
843 cached = mono_method_inflated_lookup (iresult, FALSE);
845 mono_loader_unlock ();
847 return (MonoMethod*)cached;
850 mono_stats.inflated_method_count++;
852 inflated_methods_size += sizeof (MonoMethodInflated);
854 sig = mono_method_signature (method);
856 memcpy (&iresult->method.pinvoke, method, sizeof (MonoMethodPInvoke));
858 memcpy (&iresult->method.normal, method, sizeof (MonoMethodNormal));
859 iresult->method.normal.header = NULL;
862 result = (MonoMethod *) iresult;
863 result->is_inflated = TRUE;
864 result->is_generic = FALSE;
865 result->signature = NULL;
866 result->is_mb_open = is_mb_open;
868 if (!context->method_inst) {
869 /* Set the generic_container of the result to the generic_container of method */
870 MonoGenericContainer *generic_container = mono_method_get_generic_container (method);
872 if (generic_container) {
873 result->is_generic = 1;
874 mono_method_set_generic_container (result, generic_container);
878 if (!klass_hint || !klass_hint->generic_class ||
879 klass_hint->generic_class->container_class != method->klass ||
880 klass_hint->generic_class->context.class_inst != context->class_inst)
883 if (method->klass->generic_container)
884 result->klass = klass_hint;
886 if (!result->klass) {
887 MonoType *inflated = inflate_generic_type (NULL, &method->klass->byval_arg, context);
888 result->klass = inflated ? mono_class_from_mono_type (inflated) : method->klass;
890 mono_metadata_free_type (inflated);
893 mono_method_inflated_lookup (iresult, TRUE);
894 mono_loader_unlock ();
899 * mono_get_inflated_method:
901 * Obsolete. We keep it around since it's mentioned in the public API.
904 mono_get_inflated_method (MonoMethod *method)
910 * mono_method_get_context_general:
912 * @uninflated: handle uninflated methods?
914 * Returns the generic context of a method or NULL if it doesn't have
915 * one. For an inflated method that's the context stored in the
916 * method. Otherwise it's in the method's generic container or in the
917 * generic container of the method's class.
920 mono_method_get_context_general (MonoMethod *method, gboolean uninflated)
922 if (method->is_inflated) {
923 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
924 return &imethod->context;
928 if (method->is_generic)
929 return &(mono_method_get_generic_container (method)->context);
930 if (method->klass->generic_container)
931 return &method->klass->generic_container->context;
936 * mono_method_get_context:
939 * Returns the generic context for method if it's inflated, otherwise
943 mono_method_get_context (MonoMethod *method)
945 return mono_method_get_context_general (method, FALSE);
949 * mono_method_get_generic_container:
951 * Returns the generic container of METHOD, which should be a generic method definition.
952 * Returns NULL if METHOD is not a generic method definition.
953 * LOCKING: Acquires the loader lock.
955 MonoGenericContainer*
956 mono_method_get_generic_container (MonoMethod *method)
958 MonoGenericContainer *container;
960 if (!method->is_generic)
963 container = mono_image_property_lookup (method->klass->image, method, MONO_METHOD_PROP_GENERIC_CONTAINER);
964 g_assert (container);
970 * mono_method_set_generic_container:
972 * Sets the generic container of METHOD to CONTAINER.
973 * LOCKING: Acquires the loader lock.
976 mono_method_set_generic_container (MonoMethod *method, MonoGenericContainer* container)
978 g_assert (method->is_generic);
980 mono_image_property_insert (method->klass->image, method, MONO_METHOD_PROP_GENERIC_CONTAINER, container);
984 * mono_class_find_enum_basetype:
985 * @class: The enum class
987 * Determine the basetype of an enum by iterating through its fields. We do this
988 * in a separate function since it is cheaper than calling mono_class_setup_fields.
991 mono_class_find_enum_basetype (MonoClass *class)
993 MonoImage *m = class->image;
994 const int top = class->field.count;
997 g_assert (class->enumtype);
1000 * Fetch all the field information.
1002 for (i = 0; i < top; i++){
1004 guint32 cols [MONO_FIELD_SIZE];
1005 int idx = class->field.first + i;
1006 MonoGenericContainer *container = NULL;
1009 /* class->field.first and idx points into the fieldptr table */
1010 mono_metadata_decode_table_row (m, MONO_TABLE_FIELD, idx, cols, MONO_FIELD_SIZE);
1011 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
1012 mono_metadata_decode_value (sig, &sig);
1013 /* FIELD signature == 0x06 */
1014 g_assert (*sig == 0x06);
1015 if (class->generic_container)
1016 container = class->generic_container;
1017 else if (class->generic_class) {
1018 MonoClass *gklass = class->generic_class->container_class;
1020 container = gklass->generic_container;
1021 g_assert (container);
1023 ftype = mono_metadata_parse_type_full (m, container, MONO_PARSE_FIELD, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
1026 if (class->generic_class) {
1027 //FIXME do we leak here?
1028 ftype = mono_class_inflate_generic_type (ftype, mono_class_get_context (class));
1029 ftype->attrs = cols [MONO_FIELD_FLAGS];
1032 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC))
1040 * mono_class_setup_fields:
1041 * @class: The class to initialize
1043 * Initializes the class->fields.
1044 * LOCKING: Assumes the loader lock is held.
1047 mono_class_setup_fields (MonoClass *class)
1049 MonoImage *m = class->image;
1050 int top = class->field.count;
1051 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
1052 int i, blittable = TRUE;
1053 guint32 real_size = 0;
1054 guint32 packing_size = 0;
1055 gboolean explicit_size;
1056 MonoClassField *field;
1057 MonoGenericContainer *container = NULL;
1058 MonoClass *gklass = NULL;
1060 if (class->size_inited)
1063 if (class->generic_class && class->generic_class->container_class->image->dynamic && !class->generic_class->container_class->wastypebuilder) {
1065 * This happens when a generic instance of an unfinished generic typebuilder
1066 * is used as an element type for creating an array type. We can't initialize
1067 * the fields of this class using the fields of gklass, since gklass is not
1068 * finished yet, fields could be added to it later.
1073 if (class->generic_class) {
1074 MonoClass *gklass = class->generic_class->container_class;
1075 mono_class_setup_fields (gklass);
1076 top = gklass->field.count;
1077 class->field.first = gklass->field.first;
1078 class->field.count = gklass->field.count;
1081 class->instance_size = 0;
1083 class->sizes.class_size = 0;
1085 if (class->parent) {
1086 /* For generic instances, class->parent might not have been initialized */
1087 mono_class_init (class->parent);
1088 if (!class->parent->size_inited)
1089 mono_class_setup_fields (class->parent);
1090 class->instance_size += class->parent->instance_size;
1091 class->min_align = class->parent->min_align;
1092 /* we use |= since it may have been set already */
1093 class->has_references |= class->parent->has_references;
1094 blittable = class->parent->blittable;
1096 class->instance_size = sizeof (MonoObject);
1097 class->min_align = 1;
1100 /* We can't really enable 16 bytes alignment until the GC supports it.
1101 The whole layout/instance size code must be reviewed because we do alignment calculation in terms of the
1102 boxed instance, which leads to unexplainable holes at the beginning of an object embedding a simd type.
1103 Bug #506144 is an example of this issue.
1105 if (class->simd_type)
1106 class->min_align = 16;
1108 /* Get the real size */
1109 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
1111 if (explicit_size) {
1112 g_assert ((packing_size & 0xfffffff0) == 0);
1113 class->packing_size = packing_size;
1114 real_size += class->instance_size;
1118 if (explicit_size && real_size) {
1119 class->instance_size = MAX (real_size, class->instance_size);
1121 class->size_inited = 1;
1122 class->blittable = blittable;
1126 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT)
1129 /* Prevent infinite loops if the class references itself */
1130 class->size_inited = 1;
1132 class->fields = mono_image_alloc0 (class->image, sizeof (MonoClassField) * top);
1134 if (class->generic_container) {
1135 container = class->generic_container;
1136 } else if (class->generic_class) {
1137 gklass = class->generic_class->container_class;
1138 container = gklass->generic_container;
1139 g_assert (container);
1141 mono_class_setup_fields (gklass);
1143 if (gklass->exception_type) {
1144 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
1150 * Fetch all the field information.
1152 for (i = 0; i < top; i++){
1153 int idx = class->field.first + i;
1154 field = &class->fields [i];
1156 field->parent = class;
1158 if (class->generic_class) {
1159 MonoClassField *gfield = &gklass->fields [i];
1161 field->name = mono_field_get_name (gfield);
1162 /*This memory must come from the image mempool as we don't have a chance to free it.*/
1163 field->type = mono_class_inflate_generic_type_no_copy (class->image, gfield->type, mono_class_get_context (class));
1164 g_assert (field->type->attrs == gfield->type->attrs);
1165 if (mono_field_is_deleted (field))
1167 field->offset = gfield->offset;
1170 guint32 cols [MONO_FIELD_SIZE];
1172 /* class->field.first and idx points into the fieldptr table */
1173 mono_metadata_decode_table_row (m, MONO_TABLE_FIELD, idx, cols, MONO_FIELD_SIZE);
1174 /* The name is needed for fieldrefs */
1175 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
1176 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
1177 mono_metadata_decode_value (sig, &sig);
1178 /* FIELD signature == 0x06 */
1179 g_assert (*sig == 0x06);
1180 field->type = mono_metadata_parse_type_full (m, container, MONO_PARSE_FIELD, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
1182 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
1185 if (mono_field_is_deleted (field))
1187 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
1189 mono_metadata_field_info (m, idx, &offset, NULL, NULL);
1190 field->offset = offset;
1191 if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
1192 g_warning ("%s not initialized correctly (missing field layout info for %s)",
1193 class->name, mono_field_get_name (field));
1197 /* Only do these checks if we still think this type is blittable */
1198 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
1199 if (field->type->byref || MONO_TYPE_IS_REFERENCE (field->type)) {
1202 MonoClass *field_class = mono_class_from_mono_type (field->type);
1204 mono_class_setup_fields (field_class);
1205 if (!field_class || !field_class->blittable)
1210 if (class->enumtype && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
1211 class->cast_class = class->element_class = mono_class_from_mono_type (field->type);
1212 blittable = class->element_class->blittable;
1215 /* The def_value of fields is compute lazily during vtable creation */
1218 if (class == mono_defaults.string_class)
1221 class->blittable = blittable;
1223 if (class->enumtype && !mono_class_enum_basetype (class)) {
1224 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
1227 if (explicit_size && real_size) {
1228 class->instance_size = MAX (real_size, class->instance_size);
1231 if (class->exception_type)
1233 mono_class_layout_fields (class);
1237 * mono_class_setup_fields_locking:
1238 * @class: The class to initialize
1240 * Initializes the class->fields array of fields.
1241 * Aquires the loader lock.
1244 mono_class_setup_fields_locking (MonoClass *class)
1246 mono_loader_lock ();
1247 mono_class_setup_fields (class);
1248 mono_loader_unlock ();
1252 * mono_class_has_references:
1254 * Returns whenever @klass->has_references is set, initializing it if needed.
1255 * Aquires the loader lock.
1258 mono_class_has_references (MonoClass *klass)
1260 if (klass->init_pending) {
1261 /* Be conservative */
1264 mono_class_init (klass);
1266 return klass->has_references;
1270 /* useful until we keep track of gc-references in corlib etc. */
1272 #define IS_GC_REFERENCE(t) FALSE
1274 #define IS_GC_REFERENCE(t) ((t)->type == MONO_TYPE_U && class->image == mono_defaults.corlib)
1278 * mono_type_get_basic_type_from_generic:
1281 * Returns a closed type corresponding to the possibly open type
1285 mono_type_get_basic_type_from_generic (MonoType *type)
1287 /* When we do generic sharing we let type variables stand for reference types. */
1288 if (!type->byref && (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR))
1289 return &mono_defaults.object_class->byval_arg;
1294 * mono_class_layout_fields:
1297 * Compute the placement of fields inside an object or struct, according to
1298 * the layout rules and set the following fields in @class:
1299 * - has_references (if the class contains instance references firled or structs that contain references)
1300 * - has_static_refs (same, but for static fields)
1301 * - instance_size (size of the object in memory)
1302 * - class_size (size needed for the static fields)
1303 * - size_inited (flag set when the instance_size is set)
1305 * LOCKING: this is supposed to be called with the loader lock held.
1308 mono_class_layout_fields (MonoClass *class)
1311 const int top = class->field.count;
1312 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
1313 guint32 pass, passes, real_size;
1314 gboolean gc_aware_layout = FALSE;
1315 MonoClassField *field;
1318 * When we do generic sharing we need to have layout
1319 * information for open generic classes (either with a generic
1320 * context containing type variables or with a generic
1321 * container), so we don't return in that case anymore.
1325 * Enable GC aware auto layout: in this mode, reference
1326 * fields are grouped together inside objects, increasing collector
1328 * Requires that all classes whose layout is known to native code be annotated
1329 * with [StructLayout (LayoutKind.Sequential)]
1330 * Value types have gc_aware_layout disabled by default, as per
1331 * what the default is for other runtimes.
1333 /* corlib is missing [StructLayout] directives in many places */
1334 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
1335 if (class->image != mono_defaults.corlib &&
1336 class->byval_arg.type != MONO_TYPE_VALUETYPE)
1337 gc_aware_layout = TRUE;
1338 /* from System.dll, used in metadata/process.h */
1339 if (strcmp (class->name, "ProcessStartInfo") == 0)
1340 gc_aware_layout = FALSE;
1343 /* Compute klass->has_references */
1345 * Process non-static fields first, since static fields might recursively
1346 * refer to the class itself.
1348 for (i = 0; i < top; i++) {
1351 field = &class->fields [i];
1353 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
1354 ftype = mono_type_get_underlying_type (field->type);
1355 ftype = mono_type_get_basic_type_from_generic (ftype);
1356 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype)))))
1357 class->has_references = TRUE;
1361 for (i = 0; i < top; i++) {
1364 field = &class->fields [i];
1366 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC) {
1367 ftype = mono_type_get_underlying_type (field->type);
1368 ftype = mono_type_get_basic_type_from_generic (ftype);
1369 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype)))))
1370 class->has_static_refs = TRUE;
1374 for (i = 0; i < top; i++) {
1377 field = &class->fields [i];
1379 ftype = mono_type_get_underlying_type (field->type);
1380 ftype = mono_type_get_basic_type_from_generic (ftype);
1381 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype))))) {
1382 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1383 class->has_static_refs = TRUE;
1385 class->has_references = TRUE;
1390 * Compute field layout and total size (not considering static fields)
1394 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
1395 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
1397 if (gc_aware_layout)
1402 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
1406 real_size = class->parent->instance_size;
1408 real_size = sizeof (MonoObject);
1410 for (pass = 0; pass < passes; ++pass) {
1411 for (i = 0; i < top; i++){
1416 field = &class->fields [i];
1418 if (mono_field_is_deleted (field))
1420 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1423 ftype = mono_type_get_underlying_type (field->type);
1424 ftype = mono_type_get_basic_type_from_generic (ftype);
1425 if (gc_aware_layout) {
1426 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype))))) {
1435 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
1436 (strcmp (mono_field_get_name (field), "$PRIVATE$") == 0)) {
1437 /* This field is a hack inserted by MCS to empty structures */
1441 size = mono_type_size (field->type, &align);
1443 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
1444 align = class->packing_size ? MIN (class->packing_size, align): align;
1445 /* if the field has managed references, we need to force-align it
1448 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype)))))
1449 align = MAX (align, sizeof (gpointer));
1451 class->min_align = MAX (align, class->min_align);
1452 field->offset = real_size;
1453 field->offset += align - 1;
1454 field->offset &= ~(align - 1);
1455 real_size = field->offset + size;
1458 class->instance_size = MAX (real_size, class->instance_size);
1460 if (class->instance_size & (class->min_align - 1)) {
1461 class->instance_size += class->min_align - 1;
1462 class->instance_size &= ~(class->min_align - 1);
1466 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
1468 for (i = 0; i < top; i++) {
1473 field = &class->fields [i];
1476 * There must be info about all the fields in a type if it
1477 * uses explicit layout.
1480 if (mono_field_is_deleted (field))
1482 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1485 size = mono_type_size (field->type, &align);
1486 class->min_align = MAX (align, class->min_align);
1489 * When we get here, field->offset is already set by the
1490 * loader (for either runtime fields or fields loaded from metadata).
1491 * The offset is from the start of the object: this works for both
1492 * classes and valuetypes.
1494 field->offset += sizeof (MonoObject);
1495 ftype = mono_type_get_underlying_type (field->type);
1496 ftype = mono_type_get_basic_type_from_generic (ftype);
1497 if (MONO_TYPE_IS_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype))))) {
1498 if (field->offset % sizeof (gpointer)) {
1499 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
1506 real_size = MAX (real_size, size + field->offset);
1508 class->instance_size = MAX (real_size, class->instance_size);
1512 if (layout != TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
1514 * For small structs, set min_align to at least the struct size to improve
1515 * performance, and since the JIT memset/memcpy code assumes this and generates
1516 * unaligned accesses otherwise. See #78990 for a testcase.
1518 if (class->instance_size <= sizeof (MonoObject) + sizeof (gpointer))
1519 class->min_align = MAX (class->min_align, class->instance_size - sizeof (MonoObject));
1522 class->size_inited = 1;
1525 * Compute static field layout and size
1527 for (i = 0; i < top; i++){
1531 field = &class->fields [i];
1533 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
1535 if (mono_field_is_deleted (field))
1538 size = mono_type_size (field->type, &align);
1539 field->offset = class->sizes.class_size;
1540 field->offset += align - 1;
1541 field->offset &= ~(align - 1);
1542 class->sizes.class_size = field->offset + size;
1547 create_array_method (MonoClass *class, const char *name, MonoMethodSignature *sig)
1551 method = (MonoMethod *) mono_image_alloc0 (class->image, sizeof (MonoMethodPInvoke));
1552 method->klass = class;
1553 method->flags = METHOD_ATTRIBUTE_PUBLIC;
1554 method->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1555 method->signature = sig;
1556 method->name = name;
1559 if (name [0] == '.') {
1560 method->flags |= METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1562 method->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
1568 * mono_class_setup_methods:
1571 * Initializes the 'methods' array in the klass.
1572 * Calling this method should be avoided if possible since it allocates a lot
1573 * of long-living MonoMethod structures.
1574 * Methods belonging to an interface are assigned a sequential slot starting
1578 mono_class_setup_methods (MonoClass *class)
1581 MonoMethod **methods;
1586 mono_loader_lock ();
1588 if (class->methods) {
1589 mono_loader_unlock ();
1593 if (class->generic_class) {
1594 MonoClass *gklass = class->generic_class->container_class;
1596 mono_class_init (gklass);
1597 mono_class_setup_methods (gklass);
1599 /* The + 1 makes this always non-NULL to pass the check in mono_class_setup_methods () */
1600 class->method.count = gklass->method.count;
1601 methods = g_new0 (MonoMethod *, class->method.count + 1);
1603 for (i = 0; i < class->method.count; i++) {
1604 methods [i] = mono_class_inflate_generic_method_full (
1605 gklass->methods [i], class, mono_class_get_context (class));
1607 } else if (class->rank) {
1608 MonoMethod *amethod;
1609 MonoMethodSignature *sig;
1610 int count_generic = 0, first_generic = 0;
1613 class->method.count = 3 + (class->rank > 1? 2: 1);
1615 mono_class_setup_interfaces (class);
1617 if (class->interface_count) {
1618 count_generic = generic_array_methods (class);
1619 first_generic = class->method.count;
1620 class->method.count += class->interface_count * count_generic;
1623 methods = mono_image_alloc0 (class->image, sizeof (MonoMethod*) * class->method.count);
1625 sig = mono_metadata_signature_alloc (class->image, class->rank);
1626 sig->ret = &mono_defaults.void_class->byval_arg;
1627 sig->pinvoke = TRUE;
1628 sig->hasthis = TRUE;
1629 for (i = 0; i < class->rank; ++i)
1630 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1632 amethod = create_array_method (class, ".ctor", sig);
1633 methods [method_num++] = amethod;
1634 if (class->rank > 1) {
1635 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
1636 sig->ret = &mono_defaults.void_class->byval_arg;
1637 sig->pinvoke = TRUE;
1638 sig->hasthis = TRUE;
1639 for (i = 0; i < class->rank * 2; ++i)
1640 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1642 amethod = create_array_method (class, ".ctor", sig);
1643 methods [method_num++] = amethod;
1645 /* element Get (idx11, [idx2, ...]) */
1646 sig = mono_metadata_signature_alloc (class->image, class->rank);
1647 sig->ret = &class->element_class->byval_arg;
1648 sig->pinvoke = TRUE;
1649 sig->hasthis = TRUE;
1650 for (i = 0; i < class->rank; ++i)
1651 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1652 amethod = create_array_method (class, "Get", sig);
1653 methods [method_num++] = amethod;
1654 /* element& Address (idx11, [idx2, ...]) */
1655 sig = mono_metadata_signature_alloc (class->image, class->rank);
1656 sig->ret = &class->element_class->this_arg;
1657 sig->pinvoke = TRUE;
1658 sig->hasthis = TRUE;
1659 for (i = 0; i < class->rank; ++i)
1660 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1661 amethod = create_array_method (class, "Address", sig);
1662 methods [method_num++] = amethod;
1663 /* void Set (idx11, [idx2, ...], element) */
1664 sig = mono_metadata_signature_alloc (class->image, class->rank + 1);
1665 sig->ret = &mono_defaults.void_class->byval_arg;
1666 sig->pinvoke = TRUE;
1667 sig->hasthis = TRUE;
1668 for (i = 0; i < class->rank; ++i)
1669 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1670 sig->params [i] = &class->element_class->byval_arg;
1671 amethod = create_array_method (class, "Set", sig);
1672 methods [method_num++] = amethod;
1674 for (i = 0; i < class->interface_count; i++)
1675 setup_generic_array_ifaces (class, class->interfaces [i], methods, first_generic + i * count_generic);
1677 methods = mono_image_alloc (class->image, sizeof (MonoMethod*) * class->method.count);
1678 for (i = 0; i < class->method.count; ++i) {
1679 int idx = mono_metadata_translate_token_index (class->image, MONO_TABLE_METHOD, class->method.first + i + 1);
1680 methods [i] = mono_get_method (class->image, MONO_TOKEN_METHOD_DEF | idx, class);
1684 if (MONO_CLASS_IS_INTERFACE (class))
1685 for (i = 0; i < class->method.count; ++i)
1686 methods [i]->slot = i;
1688 /* Needed because of the double-checking locking pattern */
1689 mono_memory_barrier ();
1691 class->methods = methods;
1693 if (mono_debugger_class_loaded_methods_func)
1694 mono_debugger_class_loaded_methods_func (class);
1696 mono_loader_unlock ();
1700 * mono_class_get_method_by_index:
1702 * Returns class->methods [index], initializing class->methods if neccesary.
1704 * LOCKING: Acquires the loader lock.
1707 mono_class_get_method_by_index (MonoClass *class, int index)
1709 /* Avoid calling setup_methods () if possible */
1710 if (class->generic_class && !class->methods) {
1711 MonoClass *gklass = class->generic_class->container_class;
1714 m = mono_class_inflate_generic_method_full (
1715 gklass->methods [index], class, mono_class_get_context (class));
1717 * If setup_methods () is called later for this class, no duplicates are created,
1718 * since inflate_generic_method guarantees that only one instance of a method
1719 * is created for each context.
1722 mono_class_setup_methods (class);
1723 g_assert (m == class->methods [index]);
1727 mono_class_setup_methods (class);
1728 g_assert (index >= 0 && index < class->method.count);
1729 return class->methods [index];
1734 * mono_class_get_inflated_method:
1736 * Given an inflated class CLASS and a method METHOD which should be a method of
1737 * CLASS's generic definition, return the inflated method corresponding to METHOD.
1740 mono_class_get_inflated_method (MonoClass *class, MonoMethod *method)
1742 MonoClass *gklass = class->generic_class->container_class;
1745 mono_class_setup_methods (gklass);
1746 for (i = 0; i < gklass->method.count; ++i) {
1747 if (gklass->methods [i] == method) {
1749 return class->methods [i];
1751 return mono_class_inflate_generic_method_full (gklass->methods [i], class, mono_class_get_context (class));
1759 * mono_class_get_vtable_entry:
1761 * Returns class->vtable [offset], computing it if neccesary.
1762 * LOCKING: Acquires the loader lock.
1765 mono_class_get_vtable_entry (MonoClass *class, int offset)
1769 if (class->rank == 1) {
1771 * szarrays do not overwrite any methods of Array, so we can avoid
1772 * initializing their vtables in some cases.
1774 mono_class_setup_vtable (class->parent);
1775 if (offset < class->parent->vtable_size)
1776 return class->parent->vtable [offset];
1779 if (class->generic_class) {
1780 MonoClass *gklass = class->generic_class->container_class;
1781 mono_class_setup_vtable (gklass);
1782 m = gklass->vtable [offset];
1784 m = mono_class_inflate_generic_method_full (m, class, mono_class_get_context (class));
1786 mono_class_setup_vtable (class);
1787 m = class->vtable [offset];
1794 mono_class_setup_properties (MonoClass *class)
1796 guint startm, endm, i, j;
1797 guint32 cols [MONO_PROPERTY_SIZE];
1798 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
1799 MonoProperty *properties;
1802 if (class->ext && class->ext->properties)
1805 mono_loader_lock ();
1807 if (class->ext && class->ext->properties) {
1808 mono_loader_unlock ();
1812 mono_class_alloc_ext (class);
1814 if (class->generic_class) {
1815 MonoClass *gklass = class->generic_class->container_class;
1817 mono_class_init (gklass);
1818 mono_class_setup_properties (gklass);
1820 class->ext->property = gklass->ext->property;
1822 properties = g_new0 (MonoProperty, class->ext->property.count + 1);
1824 for (i = 0; i < class->ext->property.count; i++) {
1825 MonoProperty *prop = &properties [i];
1827 *prop = gklass->ext->properties [i];
1830 prop->get = mono_class_inflate_generic_method_full (
1831 prop->get, class, mono_class_get_context (class));
1833 prop->set = mono_class_inflate_generic_method_full (
1834 prop->set, class, mono_class_get_context (class));
1836 prop->parent = class;
1839 class->ext->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &last);
1840 class->ext->property.count = last - class->ext->property.first;
1842 if (class->ext->property.count)
1843 mono_class_setup_methods (class);
1845 properties = mono_image_alloc0 (class->image, sizeof (MonoProperty) * class->ext->property.count);
1846 for (i = class->ext->property.first; i < last; ++i) {
1847 mono_metadata_decode_table_row (class->image, MONO_TABLE_PROPERTY, i, cols, MONO_PROPERTY_SIZE);
1848 properties [i - class->ext->property.first].parent = class;
1849 properties [i - class->ext->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
1850 properties [i - class->ext->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
1852 startm = mono_metadata_methods_from_property (class->image, i, &endm);
1853 for (j = startm; j < endm; ++j) {
1856 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
1858 if (class->image->uncompressed_metadata)
1859 /* It seems like the MONO_METHOD_SEMA_METHOD column needs no remapping */
1860 method = mono_get_method (class->image, MONO_TOKEN_METHOD_DEF | cols [MONO_METHOD_SEMA_METHOD], class);
1862 method = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1864 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1865 case METHOD_SEMANTIC_SETTER:
1866 properties [i - class->ext->property.first].set = method;
1868 case METHOD_SEMANTIC_GETTER:
1869 properties [i - class->ext->property.first].get = method;
1877 /*Flush any pending writes as we do double checked locking on class->properties */
1878 mono_memory_barrier ();
1880 /* Leave this assignment as the last op in the function */
1881 class->ext->properties = properties;
1883 mono_loader_unlock ();
1887 inflate_method_listz (MonoMethod **methods, MonoClass *class, MonoGenericContext *context)
1889 MonoMethod **om, **retval;
1892 for (om = methods, count = 0; *om; ++om, ++count)
1895 retval = g_new0 (MonoMethod*, count + 1);
1897 for (om = methods, count = 0; *om; ++om, ++count)
1898 retval [count] = mono_class_inflate_generic_method_full (*om, class, context);
1904 mono_class_setup_events (MonoClass *class)
1906 guint startm, endm, i, j;
1907 guint32 cols [MONO_EVENT_SIZE];
1908 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
1912 if (class->ext && class->ext->events)
1915 mono_loader_lock ();
1917 if (class->ext && class->ext->events) {
1918 mono_loader_unlock ();
1922 mono_class_alloc_ext (class);
1924 if (class->generic_class) {
1925 MonoClass *gklass = class->generic_class->container_class;
1926 MonoGenericContext *context;
1928 mono_class_setup_events (gklass);
1929 class->ext->event = gklass->ext->event;
1931 class->ext->events = g_new0 (MonoEvent, class->ext->event.count);
1933 if (class->ext->event.count)
1934 context = mono_class_get_context (class);
1936 for (i = 0; i < class->ext->event.count; i++) {
1937 MonoEvent *event = &class->ext->events [i];
1938 MonoEvent *gevent = &gklass->ext->events [i];
1940 event->parent = class;
1941 event->name = gevent->name;
1942 event->add = gevent->add ? mono_class_inflate_generic_method_full (gevent->add, class, context) : NULL;
1943 event->remove = gevent->remove ? mono_class_inflate_generic_method_full (gevent->remove, class, context) : NULL;
1944 event->raise = gevent->raise ? mono_class_inflate_generic_method_full (gevent->raise, class, context) : NULL;
1945 event->other = gevent->other ? inflate_method_listz (gevent->other, class, context) : NULL;
1946 event->attrs = gevent->attrs;
1949 mono_loader_unlock ();
1953 class->ext->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &last);
1954 class->ext->event.count = last - class->ext->event.first;
1956 if (class->ext->event.count)
1957 mono_class_setup_methods (class);
1959 events = mono_image_alloc0 (class->image, sizeof (MonoEvent) * class->ext->event.count);
1960 for (i = class->ext->event.first; i < last; ++i) {
1961 MonoEvent *event = &events [i - class->ext->event.first];
1963 mono_metadata_decode_table_row (class->image, MONO_TABLE_EVENT, i, cols, MONO_EVENT_SIZE);
1964 event->parent = class;
1965 event->attrs = cols [MONO_EVENT_FLAGS];
1966 event->name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
1968 startm = mono_metadata_methods_from_event (class->image, i, &endm);
1969 for (j = startm; j < endm; ++j) {
1972 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
1974 if (class->image->uncompressed_metadata)
1975 /* It seems like the MONO_METHOD_SEMA_METHOD column needs no remapping */
1976 method = mono_get_method (class->image, MONO_TOKEN_METHOD_DEF | cols [MONO_METHOD_SEMA_METHOD], class);
1978 method = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1980 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1981 case METHOD_SEMANTIC_ADD_ON:
1982 event->add = method;
1984 case METHOD_SEMANTIC_REMOVE_ON:
1985 event->remove = method;
1987 case METHOD_SEMANTIC_FIRE:
1988 event->raise = method;
1990 case METHOD_SEMANTIC_OTHER: {
1993 if (event->other == NULL) {
1994 event->other = g_new0 (MonoMethod*, 2);
1996 while (event->other [n])
1998 event->other = g_realloc (event->other, (n + 2) * sizeof (MonoMethod*));
2000 event->other [n] = method;
2001 /* NULL terminated */
2002 event->other [n + 1] = NULL;
2010 /*Flush any pending writes as we do double checked locking on class->properties */
2011 mono_memory_barrier ();
2013 /* Leave this assignment as the last op in the function */
2014 class->ext->events = events;
2016 mono_loader_unlock ();
2020 * Global pool of interface IDs, represented as a bitset.
2021 * LOCKING: this is supposed to be accessed with the loader lock held.
2023 static MonoBitSet *global_interface_bitset = NULL;
2026 * mono_unload_interface_ids:
2027 * @bitset: bit set of interface IDs
2029 * When an image is unloaded, the interface IDs associated with
2030 * the image are put back in the global pool of IDs so the numbers
2034 mono_unload_interface_ids (MonoBitSet *bitset)
2036 mono_loader_lock ();
2037 mono_bitset_sub (global_interface_bitset, bitset);
2038 mono_loader_unlock ();
2042 * mono_get_unique_iid:
2045 * Assign a unique integer ID to the interface represented by @class.
2046 * The ID will positive and as small as possible.
2047 * LOCKING: this is supposed to be called with the loader lock held.
2048 * Returns: the new ID.
2051 mono_get_unique_iid (MonoClass *class)
2055 g_assert (MONO_CLASS_IS_INTERFACE (class));
2057 if (!global_interface_bitset) {
2058 global_interface_bitset = mono_bitset_new (128, 0);
2061 iid = mono_bitset_find_first_unset (global_interface_bitset, -1);
2063 int old_size = mono_bitset_size (global_interface_bitset);
2064 MonoBitSet *new_set = mono_bitset_clone (global_interface_bitset, old_size * 2);
2065 mono_bitset_free (global_interface_bitset);
2066 global_interface_bitset = new_set;
2069 mono_bitset_set (global_interface_bitset, iid);
2070 /* set the bit also in the per-image set */
2071 if (class->image->interface_bitset) {
2072 if (iid >= mono_bitset_size (class->image->interface_bitset)) {
2073 MonoBitSet *new_set = mono_bitset_clone (class->image->interface_bitset, iid + 1);
2074 mono_bitset_free (class->image->interface_bitset);
2075 class->image->interface_bitset = new_set;
2078 class->image->interface_bitset = mono_bitset_new (iid + 1, 0);
2080 mono_bitset_set (class->image->interface_bitset, iid);
2082 if (mono_print_vtable) {
2084 char *type_name = mono_type_full_name (&class->byval_arg);
2085 if (class->generic_class && !class->generic_class->context.class_inst->is_open) {
2086 generic_id = class->generic_class->context.class_inst->id;
2087 g_assert (generic_id != 0);
2091 printf ("Interface: assigned id %d to %s|%s|%d\n", iid, class->image->name, type_name, generic_id);
2095 g_assert (iid <= 65535);
2100 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
2105 mono_class_setup_interfaces (klass);
2107 for (i = 0; i < klass->interface_count; i++) {
2108 ic = klass->interfaces [i];
2111 *res = g_ptr_array_new ();
2112 g_ptr_array_add (*res, ic);
2113 mono_class_init (ic);
2115 collect_implemented_interfaces_aux (ic, res);
2120 mono_class_get_implemented_interfaces (MonoClass *klass)
2122 GPtrArray *res = NULL;
2124 collect_implemented_interfaces_aux (klass, &res);
2129 compare_interface_ids (const void *p_key, const void *p_element) {
2130 const MonoClass *key = p_key;
2131 const MonoClass *element = *(MonoClass**) p_element;
2133 return (key->interface_id - element->interface_id);
2137 mono_class_interface_offset (MonoClass *klass, MonoClass *itf) {
2138 MonoClass **result = bsearch (
2140 klass->interfaces_packed,
2141 klass->interface_offsets_count,
2142 sizeof (MonoClass *),
2143 compare_interface_ids);
2145 return klass->interface_offsets_packed [result - (klass->interfaces_packed)];
2152 print_implemented_interfaces (MonoClass *klass) {
2153 GPtrArray *ifaces = NULL;
2155 int ancestor_level = 0;
2157 printf ("Packed interface table for class %s has size %d\n", klass->name, klass->interface_offsets_count);
2158 for (i = 0; i < klass->interface_offsets_count; i++)
2159 printf (" [%03d][UUID %03d][SLOT %03d][SIZE %03d] interface %s.%s\n", i,
2160 klass->interfaces_packed [i]->interface_id,
2161 klass->interface_offsets_packed [i],
2162 klass->interfaces_packed [i]->method.count,
2163 klass->interfaces_packed [i]->name_space,
2164 klass->interfaces_packed [i]->name );
2165 printf ("Interface flags: ");
2166 for (i = 0; i <= klass->max_interface_id; i++)
2167 if (MONO_CLASS_IMPLEMENTS_INTERFACE (klass, i))
2168 printf ("(%d,T)", i);
2170 printf ("(%d,F)", i);
2172 printf ("Dump interface flags:");
2173 for (i = 0; i < ((((klass->max_interface_id + 1) >> 3)) + (((klass->max_interface_id + 1) & 7)? 1 :0)); i++)
2174 printf (" %02X", klass->interface_bitmap [i]);
2176 while (klass != NULL) {
2177 printf ("[LEVEL %d] Implemented interfaces by class %s:\n", ancestor_level, klass->name);
2178 ifaces = mono_class_get_implemented_interfaces (klass);
2180 for (i = 0; i < ifaces->len; i++) {
2181 MonoClass *ic = g_ptr_array_index (ifaces, i);
2182 printf (" [UIID %d] interface %s\n", ic->interface_id, ic->name);
2183 printf (" [%03d][UUID %03d][SLOT %03d][SIZE %03d] interface %s.%s\n", i,
2185 mono_class_interface_offset (klass, ic),
2190 g_ptr_array_free (ifaces, TRUE);
2193 klass = klass->parent;
2198 inflate_class_one_arg (MonoClass *gtype, MonoClass *arg0)
2201 args [0] = &arg0->byval_arg;
2203 return mono_class_bind_generic_parameters (gtype, 1, args, FALSE);
2207 array_class_get_if_rank (MonoClass *class, guint rank)
2209 return rank ? mono_array_class_get (class, rank) : class;
2213 fill_valuetype_array_derived_types (MonoClass **valuetype_types, MonoClass *eclass, int rank)
2215 valuetype_types [0] = eclass;
2216 if (eclass == mono_defaults.int16_class)
2217 valuetype_types [1] = mono_defaults.uint16_class;
2218 else if (eclass == mono_defaults.uint16_class)
2219 valuetype_types [1] = mono_defaults.int16_class;
2220 else if (eclass == mono_defaults.int32_class)
2221 valuetype_types [1] = mono_defaults.uint32_class;
2222 else if (eclass == mono_defaults.uint32_class)
2223 valuetype_types [1] = mono_defaults.int32_class;
2224 else if (eclass == mono_defaults.int64_class)
2225 valuetype_types [1] = mono_defaults.uint64_class;
2226 else if (eclass == mono_defaults.uint64_class)
2227 valuetype_types [1] = mono_defaults.int64_class;
2228 else if (eclass == mono_defaults.byte_class)
2229 valuetype_types [1] = mono_defaults.sbyte_class;
2230 else if (eclass == mono_defaults.sbyte_class)
2231 valuetype_types [1] = mono_defaults.byte_class;
2232 else if (eclass->enumtype && mono_class_enum_basetype (eclass))
2233 valuetype_types [1] = mono_class_from_mono_type (mono_class_enum_basetype (eclass));
2236 /* this won't be needed once bug #325495 is completely fixed
2237 * though we'll need something similar to know which interfaces to allow
2238 * in arrays when they'll be lazyly created
2240 * FIXME: System.Array/InternalEnumerator don't need all this interface fabrication machinery.
2241 * MS returns diferrent types based on which instance is called. For example:
2242 * object obj = new byte[10][];
2243 * Type a = ((IEnumerable<byte[]>)obj).GetEnumerator ().GetType ();
2244 * Type b = ((IEnumerable<IList<byte>>)obj).GetEnumerator ().GetType ();
2247 * Fixing this should kill quite some code, save some bits and improve compatbility.
2250 get_implicit_generic_array_interfaces (MonoClass *class, int *num, int *is_enumerator)
2252 MonoClass *eclass = class->element_class;
2253 static MonoClass* generic_icollection_class = NULL;
2254 static MonoClass* generic_ienumerable_class = NULL;
2255 static MonoClass* generic_ienumerator_class = NULL;
2256 MonoClass *valuetype_types[2] = { NULL, NULL };
2257 MonoClass **interfaces = NULL;
2258 int i, interface_count, real_count, original_rank;
2260 gboolean internal_enumerator;
2261 gboolean eclass_is_valuetype;
2263 if (!mono_defaults.generic_ilist_class) {
2267 internal_enumerator = FALSE;
2268 eclass_is_valuetype = FALSE;
2269 original_rank = eclass->rank;
2270 if (class->byval_arg.type != MONO_TYPE_SZARRAY) {
2271 if (class->generic_class && class->nested_in == mono_defaults.array_class && strcmp (class->name, "InternalEnumerator`1") == 0) {
2273 * For a Enumerator<T[]> we need to get the list of interfaces for T.
2275 eclass = mono_class_from_mono_type (class->generic_class->context.class_inst->type_argv [0]);
2276 original_rank = eclass->rank;
2277 eclass = eclass->element_class;
2278 internal_enumerator = TRUE;
2279 *is_enumerator = TRUE;
2287 * with this non-lazy impl we can't implement all the interfaces so we do just the minimal stuff
2288 * for deep levels of arrays of arrays (string[][] has all the interfaces, string[][][] doesn't)
2290 all_interfaces = eclass->rank && eclass->element_class->rank? FALSE: TRUE;
2292 if (!generic_icollection_class) {
2293 generic_icollection_class = mono_class_from_name (mono_defaults.corlib,
2294 "System.Collections.Generic", "ICollection`1");
2295 generic_ienumerable_class = mono_class_from_name (mono_defaults.corlib,
2296 "System.Collections.Generic", "IEnumerable`1");
2297 generic_ienumerator_class = mono_class_from_name (mono_defaults.corlib,
2298 "System.Collections.Generic", "IEnumerator`1");
2301 mono_class_init (eclass);
2304 * Arrays in 2.0 need to implement a number of generic interfaces
2305 * (IList`1, ICollection`1, IEnumerable`1 for a number of types depending
2306 * on the element class). We collect the types needed to build the
2307 * instantiations in interfaces at intervals of 3, because 3 are
2308 * the generic interfaces needed to implement.
2310 if (eclass->valuetype) {
2311 fill_valuetype_array_derived_types (valuetype_types, eclass, original_rank);
2313 /* IList, ICollection, IEnumerable */
2314 real_count = interface_count = valuetype_types [1] ? 6 : 3;
2315 if (internal_enumerator) {
2317 if (valuetype_types [1])
2321 interfaces = g_malloc0 (sizeof (MonoClass*) * real_count);
2322 interfaces [0] = valuetype_types [0];
2323 if (valuetype_types [1])
2324 interfaces [3] = valuetype_types [1];
2326 eclass_is_valuetype = TRUE;
2329 int idepth = eclass->idepth;
2330 if (!internal_enumerator)
2332 interface_count = all_interfaces? eclass->interface_offsets_count: eclass->interface_count;
2333 /* we add object for interfaces and the supertypes for the other
2334 * types. The last of the supertypes is the element class itself which we
2335 * already created the explicit interfaces for (so we include it for IEnumerator
2336 * and exclude it for arrays).
2338 if (MONO_CLASS_IS_INTERFACE (eclass))
2341 interface_count += idepth;
2342 if (eclass->rank && eclass->element_class->valuetype) {
2343 fill_valuetype_array_derived_types (valuetype_types, eclass->element_class, original_rank);
2344 if (valuetype_types [1])
2347 /* IList, ICollection, IEnumerable */
2348 interface_count *= 3;
2349 real_count = interface_count;
2350 if (internal_enumerator) {
2351 real_count += (MONO_CLASS_IS_INTERFACE (eclass) ? 1 : idepth) + eclass->interface_offsets_count;
2352 if (valuetype_types [1])
2355 interfaces = g_malloc0 (sizeof (MonoClass*) * real_count);
2356 if (MONO_CLASS_IS_INTERFACE (eclass)) {
2357 interfaces [0] = mono_defaults.object_class;
2361 for (i = 0; i < idepth; i++) {
2362 mono_class_init (eclass->supertypes [i]);
2363 interfaces [j] = eclass->supertypes [i];
2367 if (all_interfaces) {
2368 for (i = 0; i < eclass->interface_offsets_count; i++) {
2369 interfaces [j] = eclass->interfaces_packed [i];
2373 for (i = 0; i < eclass->interface_count; i++) {
2374 interfaces [j] = eclass->interfaces [i];
2378 if (valuetype_types [1]) {
2379 interfaces [j] = array_class_get_if_rank (valuetype_types [1], original_rank);
2384 /* instantiate the generic interfaces */
2385 for (i = 0; i < interface_count; i += 3) {
2386 MonoClass *iface = interfaces [i];
2388 interfaces [i + 0] = inflate_class_one_arg (mono_defaults.generic_ilist_class, iface);
2389 interfaces [i + 1] = inflate_class_one_arg (generic_icollection_class, iface);
2390 interfaces [i + 2] = inflate_class_one_arg (generic_ienumerable_class, iface);
2392 if (internal_enumerator) {
2394 /* instantiate IEnumerator<iface> */
2395 for (i = 0; i < interface_count; i++) {
2396 interfaces [i] = inflate_class_one_arg (generic_ienumerator_class, interfaces [i]);
2398 j = interface_count;
2399 if (!eclass_is_valuetype) {
2400 if (MONO_CLASS_IS_INTERFACE (eclass)) {
2401 interfaces [j] = inflate_class_one_arg (generic_ienumerator_class, mono_defaults.object_class);
2404 for (i = 0; i < eclass->idepth; i++) {
2405 interfaces [j] = inflate_class_one_arg (generic_ienumerator_class, eclass->supertypes [i]);
2409 for (i = 0; i < eclass->interface_offsets_count; i++) {
2410 interfaces [j] = inflate_class_one_arg (generic_ienumerator_class, eclass->interfaces_packed [i]);
2414 interfaces [j++] = inflate_class_one_arg (generic_ienumerator_class, array_class_get_if_rank (valuetype_types [0], original_rank));
2416 if (valuetype_types [1])
2417 interfaces [j] = inflate_class_one_arg (generic_ienumerator_class, array_class_get_if_rank (valuetype_types [1], original_rank));
2421 char *type_name = mono_type_get_name_full (&class->byval_arg, 0);
2422 for (i = 0; i < real_count; ++i) {
2423 char *name = mono_type_get_name_full (&interfaces [i]->byval_arg, 0);
2424 g_print ("%s implements %s\n", type_name, name);
2435 find_array_interface (MonoClass *klass, const char *name)
2438 for (i = 0; i < klass->interface_count; ++i) {
2439 if (strcmp (klass->interfaces [i]->name, name) == 0)
2446 * LOCKING: this is supposed to be called with the loader lock held.
2449 setup_interface_offsets (MonoClass *class, int cur_slot)
2453 MonoClass **interfaces_full;
2454 int *interface_offsets_full;
2456 int interface_offsets_count;
2457 MonoClass **array_interfaces;
2458 int num_array_interfaces;
2459 int is_enumerator = FALSE;
2462 * get the implicit generic interfaces for either the arrays or for System.Array/InternalEnumerator<T>
2463 * implicit interfaces have the property that they are assigned the same slot in the
2464 * vtables for compatible interfaces
2466 array_interfaces = get_implicit_generic_array_interfaces (class, &num_array_interfaces, &is_enumerator);
2468 /* compute maximum number of slots and maximum interface id */
2470 for (k = class; k ; k = k->parent) {
2471 for (i = 0; i < k->interface_count; i++) {
2472 ic = k->interfaces [i];
2475 mono_class_init (ic);
2477 if (max_iid < ic->interface_id)
2478 max_iid = ic->interface_id;
2480 ifaces = mono_class_get_implemented_interfaces (k);
2482 for (i = 0; i < ifaces->len; ++i) {
2483 ic = g_ptr_array_index (ifaces, i);
2484 if (max_iid < ic->interface_id)
2485 max_iid = ic->interface_id;
2487 g_ptr_array_free (ifaces, TRUE);
2490 for (i = 0; i < num_array_interfaces; ++i) {
2491 ic = array_interfaces [i];
2492 mono_class_init (ic);
2493 if (max_iid < ic->interface_id)
2494 max_iid = ic->interface_id;
2497 if (MONO_CLASS_IS_INTERFACE (class)) {
2498 if (max_iid < class->interface_id)
2499 max_iid = class->interface_id;
2501 class->max_interface_id = max_iid;
2502 /* compute vtable offset for interfaces */
2503 interfaces_full = g_malloc (sizeof (MonoClass*) * (max_iid + 1));
2504 interface_offsets_full = g_malloc (sizeof (int) * (max_iid + 1));
2506 for (i = 0; i <= max_iid; i++) {
2507 interfaces_full [i] = NULL;
2508 interface_offsets_full [i] = -1;
2511 ifaces = mono_class_get_implemented_interfaces (class);
2513 for (i = 0; i < ifaces->len; ++i) {
2514 ic = g_ptr_array_index (ifaces, i);
2515 interfaces_full [ic->interface_id] = ic;
2516 interface_offsets_full [ic->interface_id] = cur_slot;
2517 cur_slot += ic->method.count;
2519 g_ptr_array_free (ifaces, TRUE);
2522 for (k = class->parent; k ; k = k->parent) {
2523 ifaces = mono_class_get_implemented_interfaces (k);
2525 for (i = 0; i < ifaces->len; ++i) {
2526 ic = g_ptr_array_index (ifaces, i);
2528 if (interface_offsets_full [ic->interface_id] == -1) {
2529 int io = mono_class_interface_offset (k, ic);
2533 interfaces_full [ic->interface_id] = ic;
2534 interface_offsets_full [ic->interface_id] = io;
2537 g_ptr_array_free (ifaces, TRUE);
2541 if (MONO_CLASS_IS_INTERFACE (class)) {
2542 interfaces_full [class->interface_id] = class;
2543 interface_offsets_full [class->interface_id] = cur_slot;
2546 if (num_array_interfaces) {
2547 if (is_enumerator) {
2548 int ienumerator_offset;
2549 int ienumerator_idx = find_array_interface (class, "IEnumerator`1");
2550 ienumerator_offset = interface_offsets_full [class->interfaces [ienumerator_idx]->interface_id];
2551 for (i = 0; i < num_array_interfaces; ++i) {
2552 ic = array_interfaces [i];
2553 interfaces_full [ic->interface_id] = ic;
2554 if (strcmp (ic->name, "IEnumerator`1") == 0)
2555 interface_offsets_full [ic->interface_id] = ienumerator_offset;
2557 g_assert_not_reached ();
2558 /*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);*/
2561 int ilist_offset, icollection_offset, ienumerable_offset;
2562 int ilist_iface_idx = find_array_interface (class, "IList`1");
2563 int icollection_iface_idx = find_array_interface (class->interfaces [ilist_iface_idx], "ICollection`1");
2564 int ienumerable_iface_idx = find_array_interface (class->interfaces [ilist_iface_idx], "IEnumerable`1");
2565 ilist_offset = interface_offsets_full [class->interfaces [ilist_iface_idx]->interface_id];
2566 icollection_offset = interface_offsets_full [class->interfaces [ilist_iface_idx]->interfaces [icollection_iface_idx]->interface_id];
2567 ienumerable_offset = interface_offsets_full [class->interfaces [ilist_iface_idx]->interfaces [ienumerable_iface_idx]->interface_id];
2568 g_assert (ilist_offset >= 0 && icollection_offset >= 0 && ienumerable_offset >= 0);
2569 for (i = 0; i < num_array_interfaces; ++i) {
2570 ic = array_interfaces [i];
2571 interfaces_full [ic->interface_id] = ic;
2572 if (ic->generic_class->container_class == mono_defaults.generic_ilist_class)
2573 interface_offsets_full [ic->interface_id] = ilist_offset;
2574 else if (strcmp (ic->name, "ICollection`1") == 0)
2575 interface_offsets_full [ic->interface_id] = icollection_offset;
2576 else if (strcmp (ic->name, "IEnumerable`1") == 0)
2577 interface_offsets_full [ic->interface_id] = ienumerable_offset;
2579 g_assert_not_reached ();
2580 /*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);*/
2585 for (interface_offsets_count = 0, i = 0; i <= max_iid; i++) {
2586 if (interface_offsets_full [i] != -1) {
2587 interface_offsets_count ++;
2592 * We might get called twice: once from mono_class_init () then once from
2593 * mono_class_setup_vtable ().
2595 if (class->interfaces_packed) {
2596 g_assert (class->interface_offsets_count == interface_offsets_count);
2598 class->interface_offsets_count = interface_offsets_count;
2599 class->interfaces_packed = mono_image_alloc (class->image, sizeof (MonoClass*) * interface_offsets_count);
2600 class->interface_offsets_packed = mono_image_alloc (class->image, sizeof (guint16) * interface_offsets_count);
2601 class->interface_bitmap = mono_image_alloc0 (class->image, (sizeof (guint8) * ((max_iid + 1) >> 3)) + (((max_iid + 1) & 7)? 1 :0));
2602 for (interface_offsets_count = 0, i = 0; i <= max_iid; i++) {
2603 if (interface_offsets_full [i] != -1) {
2604 class->interface_bitmap [i >> 3] |= (1 << (i & 7));
2605 class->interfaces_packed [interface_offsets_count] = interfaces_full [i];
2606 class->interface_offsets_packed [interface_offsets_count] = interface_offsets_full [i];
2607 /*if (num_array_interfaces)
2608 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]);*/
2609 interface_offsets_count ++;
2614 g_free (interfaces_full);
2615 g_free (interface_offsets_full);
2616 g_free (array_interfaces);
2618 //printf ("JUST DONE: ");
2619 //print_implemented_interfaces (class);
2625 * Setup interface offsets for interfaces.
2627 * - class->max_interface_id
2628 * - class->interface_offsets_count
2629 * - class->interfaces_packed
2630 * - class->interface_offsets_packed
2631 * - class->interface_bitmap
2634 mono_class_setup_interface_offsets (MonoClass *class)
2636 mono_loader_lock ();
2638 setup_interface_offsets (class, 0);
2640 mono_loader_unlock ();
2644 * mono_class_setup_vtable:
2646 * Creates the generic vtable of CLASS.
2647 * Initializes the following fields in MonoClass:
2650 * Plus all the fields initialized by setup_interface_offsets ().
2651 * If there is an error during vtable construction, class->exception_type is set.
2653 * LOCKING: Acquires the loader lock.
2656 mono_class_setup_vtable (MonoClass *class)
2658 MonoMethod **overrides;
2659 MonoGenericContext *context;
2668 if (mono_debug_using_mono_debugger ())
2669 /* The debugger currently depends on this */
2670 mono_class_setup_methods (class);
2672 if (MONO_CLASS_IS_INTERFACE (class)) {
2673 /* This sets method->slot for all methods if this is an interface */
2674 mono_class_setup_methods (class);
2678 mono_loader_lock ();
2680 if (class->vtable) {
2681 mono_loader_unlock ();
2685 mono_stats.generic_vtable_count ++;
2687 if (class->generic_class) {
2688 context = mono_class_get_context (class);
2689 type_token = class->generic_class->container_class->type_token;
2691 context = (MonoGenericContext *) class->generic_container;
2692 type_token = class->type_token;
2695 if (class->image->dynamic) {
2696 if (class->generic_class) {
2697 MonoClass *gklass = class->generic_class->container_class;
2699 mono_reflection_get_dynamic_overrides (gklass, &overrides, &onum);
2700 for (i = 0; i < onum; ++i) {
2701 MonoMethod *override = overrides [(i * 2) + 1];
2702 MonoMethod *inflated = NULL;
2705 for (j = 0; j < class->method.count; ++j) {
2706 if (gklass->methods [j] == override) {
2707 inflated = class->methods [j];
2711 g_assert (inflated);
2713 overrides [(i * 2) + 1] = inflated;
2716 mono_reflection_get_dynamic_overrides (class, &overrides, &onum);
2719 /* The following call fails if there are missing methods in the type */
2720 ok = mono_class_get_overrides_full (class->image, type_token, &overrides, &onum, context);
2724 mono_class_setup_vtable_general (class, overrides, onum);
2728 mono_loader_unlock ();
2733 #define DEBUG_INTERFACE_VTABLE_CODE 0
2734 #define TRACE_INTERFACE_VTABLE_CODE 0
2735 #define VERIFY_INTERFACE_VTABLE_CODE 0
2737 #if (TRACE_INTERFACE_VTABLE_CODE|DEBUG_INTERFACE_VTABLE_CODE)
2738 #define DEBUG_INTERFACE_VTABLE(stmt) do {\
2742 #define DEBUG_INTERFACE_VTABLE(stmt)
2745 #if TRACE_INTERFACE_VTABLE_CODE
2746 #define TRACE_INTERFACE_VTABLE(stmt) do {\
2750 #define TRACE_INTERFACE_VTABLE(stmt)
2753 #if VERIFY_INTERFACE_VTABLE_CODE
2754 #define VERIFY_INTERFACE_VTABLE(stmt) do {\
2758 #define VERIFY_INTERFACE_VTABLE(stmt)
2762 #if (TRACE_INTERFACE_VTABLE_CODE|DEBUG_INTERFACE_VTABLE_CODE)
2764 mono_signature_get_full_desc (MonoMethodSignature *sig, gboolean include_namespace)
2768 GString *res = g_string_new ("");
2770 g_string_append_c (res, '(');
2771 for (i = 0; i < sig->param_count; ++i) {
2773 g_string_append_c (res, ',');
2774 mono_type_get_desc (res, sig->params [i], include_namespace);
2776 g_string_append (res, ")=>");
2777 if (sig->ret != NULL) {
2778 mono_type_get_desc (res, sig->ret, include_namespace);
2780 g_string_append (res, "NULL");
2783 g_string_free (res, FALSE);
2787 print_method_signatures (MonoMethod *im, MonoMethod *cm) {
2788 char *im_sig = mono_signature_get_full_desc (mono_method_signature (im), TRUE);
2789 char *cm_sig = mono_signature_get_full_desc (mono_method_signature (cm), TRUE);
2790 printf ("(IM \"%s\", CM \"%s\")", im_sig, cm_sig);
2798 check_interface_method_override (MonoClass *class, MonoMethod *im, MonoMethod *cm, gboolean require_newslot, gboolean interface_is_explicitly_implemented_by_class, gboolean slot_is_empty, gboolean security_enabled) {
2799 if (strcmp (im->name, cm->name) == 0) {
2800 if (! (cm->flags & METHOD_ATTRIBUTE_PUBLIC)) {
2801 TRACE_INTERFACE_VTABLE (printf ("[PUBLIC CHECK FAILED]"));
2804 if (! slot_is_empty) {
2805 if (require_newslot) {
2806 if (! interface_is_explicitly_implemented_by_class) {
2807 TRACE_INTERFACE_VTABLE (printf ("[NOT EXPLICIT IMPLEMENTATION IN FULL SLOT REFUSED]"));
2810 if (! (cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
2811 TRACE_INTERFACE_VTABLE (printf ("[NEWSLOT CHECK FAILED]"));
2815 TRACE_INTERFACE_VTABLE (printf ("[FULL SLOT REFUSED]"));
2818 if (! mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
2819 TRACE_INTERFACE_VTABLE (printf ("[SIGNATURE CHECK FAILED "));
2820 TRACE_INTERFACE_VTABLE (print_method_signatures (im, cm));
2821 TRACE_INTERFACE_VTABLE (printf ("]"));
2824 TRACE_INTERFACE_VTABLE (printf ("[SECURITY CHECKS]"));
2825 /* CAS - SecurityAction.InheritanceDemand on interface */
2826 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
2827 mono_secman_inheritancedemand_method (cm, im);
2830 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR)
2831 mono_security_core_clr_check_override (class, cm, im);
2832 TRACE_INTERFACE_VTABLE (printf ("[NAME CHECK OK]"));
2835 MonoClass *ic = im->klass;
2836 const char *ic_name_space = ic->name_space;
2837 const char *ic_name = ic->name;
2840 if (! require_newslot) {
2841 TRACE_INTERFACE_VTABLE (printf ("[INJECTED METHOD REFUSED]"));
2844 if (cm->klass->rank == 0) {
2845 TRACE_INTERFACE_VTABLE (printf ("[RANK CHECK FAILED]"));
2848 if (! mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
2849 TRACE_INTERFACE_VTABLE (printf ("[(INJECTED) SIGNATURE CHECK FAILED "));
2850 TRACE_INTERFACE_VTABLE (print_method_signatures (im, cm));
2851 TRACE_INTERFACE_VTABLE (printf ("]"));
2854 if (mono_class_get_image (ic) != mono_defaults.corlib) {
2855 TRACE_INTERFACE_VTABLE (printf ("[INTERFACE CORLIB CHECK FAILED]"));
2858 if ((ic_name_space == NULL) || (strcmp (ic_name_space, "System.Collections.Generic") != 0)) {
2859 TRACE_INTERFACE_VTABLE (printf ("[INTERFACE NAMESPACE CHECK FAILED]"));
2862 if ((ic_name == NULL) || ((strcmp (ic_name, "IEnumerable`1") != 0) && (strcmp (ic_name, "ICollection`1") != 0) && (strcmp (ic_name, "IList`1") != 0))) {
2863 TRACE_INTERFACE_VTABLE (printf ("[INTERFACE NAME CHECK FAILED]"));
2867 subname = strstr (cm->name, ic_name_space);
2868 if (subname != cm->name) {
2869 TRACE_INTERFACE_VTABLE (printf ("[ACTUAL NAMESPACE CHECK FAILED]"));
2872 subname += strlen (ic_name_space);
2873 if (subname [0] != '.') {
2874 TRACE_INTERFACE_VTABLE (printf ("[FIRST DOT CHECK FAILED]"));
2878 if (strstr (subname, ic_name) != subname) {
2879 TRACE_INTERFACE_VTABLE (printf ("[ACTUAL CLASS NAME CHECK FAILED]"));
2882 subname += strlen (ic_name);
2883 if (subname [0] != '.') {
2884 TRACE_INTERFACE_VTABLE (printf ("[SECOND DOT CHECK FAILED]"));
2888 if (strcmp (subname, im->name) != 0) {
2889 TRACE_INTERFACE_VTABLE (printf ("[METHOD NAME CHECK FAILED]"));
2893 TRACE_INTERFACE_VTABLE (printf ("[SECURITY CHECKS (INJECTED CASE)]"));
2894 /* CAS - SecurityAction.InheritanceDemand on interface */
2895 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
2896 mono_secman_inheritancedemand_method (cm, im);
2899 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR)
2900 mono_security_core_clr_check_override (class, cm, im);
2902 TRACE_INTERFACE_VTABLE (printf ("[INJECTED INTERFACE CHECK OK]"));
2907 #if (TRACE_INTERFACE_VTABLE_CODE|DEBUG_INTERFACE_VTABLE_CODE)
2909 foreach_override (gpointer key, gpointer value, gpointer user_data) {
2910 MonoMethod *method = key;
2911 MonoMethod *override = value;
2912 MonoClass *method_class = mono_method_get_class (method);
2913 MonoClass *override_class = mono_method_get_class (override);
2915 printf (" Method '%s.%s:%s' has override '%s.%s:%s'\n",
2916 mono_class_get_namespace (method_class), mono_class_get_name (method_class), mono_method_get_name (method),
2917 mono_class_get_namespace (override_class), mono_class_get_name (override_class), mono_method_get_name (override));
2920 print_overrides (GHashTable *override_map, const char *message) {
2922 printf ("Override map \"%s\" START:\n", message);
2923 g_hash_table_foreach (override_map, foreach_override, NULL);
2924 printf ("Override map \"%s\" END.\n", message);
2926 printf ("Override map \"%s\" EMPTY.\n", message);
2930 print_vtable_full (MonoClass *class, MonoMethod** vtable, int size, int first_non_interface_slot, const char *message, gboolean print_interfaces) {
2931 char *full_name = mono_type_full_name (&class->byval_arg);
2935 printf ("*** Vtable for class '%s' at \"%s\" (size %d)\n", full_name, message, size);
2937 if (print_interfaces) {
2938 print_implemented_interfaces (class);
2939 printf ("* Interfaces for class '%s' done.\nStarting vtable (size %d):\n", full_name, size);
2942 if (class->parent) {
2943 parent_size = class->parent->vtable_size;
2947 for (i = 0; i < size; ++i) {
2948 MonoMethod *cm = vtable [i];
2950 char *cm_name = mono_method_full_name (cm, TRUE);
2951 char newness = (i < parent_size) ? 'O' : ((i < first_non_interface_slot) ? 'I' : 'N');
2952 printf (" [%c][%03d][INDEX %03d] %s\n", newness, i, cm->slot, cm_name);
2961 #if VERIFY_INTERFACE_VTABLE_CODE
2963 mono_method_try_get_vtable_index (MonoMethod *method)
2965 if (method->is_inflated && (method->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
2966 MonoMethodInflated *imethod = (MonoMethodInflated*)method;
2967 if (imethod->declaring->is_generic)
2968 return imethod->declaring->slot;
2970 return method->slot;
2974 mono_class_verify_vtable (MonoClass *class)
2977 char *full_name = mono_type_full_name (&class->byval_arg);
2979 printf ("*** Verifying VTable of class '%s' \n", full_name);
2983 if (!class->methods)
2986 for (i = 0; i < class->method.count; ++i) {
2987 MonoMethod *cm = class->methods [i];
2990 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
2994 full_name = mono_method_full_name (cm, TRUE);
2996 slot = mono_method_try_get_vtable_index (cm);
2998 if (slot >= class->vtable_size) {
2999 printf ("\tInvalid method %s at index %d with vtable of length %d\n", full_name, slot, class->vtable_size);
3003 if (slot >= 0 && class->vtable [slot] != cm && (class->vtable [slot])) {
3004 char *other_name = class->vtable [slot] ? mono_method_full_name (class->vtable [slot], TRUE) : g_strdup ("[null value]");
3005 printf ("\tMethod %s has slot %d but vtable has %s on it\n", full_name, slot, other_name);
3006 g_free (other_name);
3009 printf ("\tVirtual method %s does n't have an assigned slot\n", full_name);
3016 print_unimplemented_interface_method_info (MonoClass *class, MonoClass *ic, MonoMethod *im, int im_slot, MonoMethod **overrides, int onum) {
3018 char *method_signature;
3020 for (index = 0; index < onum; ++index) {
3021 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", im_slot, overrides [index*2+1]->name,
3022 overrides [index*2+1]->slot, overrides [index*2]->name, overrides [index*2]->slot);
3024 method_signature = mono_signature_get_desc (mono_method_signature (im), FALSE);
3025 printf ("no implementation for interface method %s::%s(%s) in class %s.%s\n",
3026 mono_type_get_name (&ic->byval_arg), im->name, method_signature, class->name_space, class->name);
3027 g_free (method_signature);
3028 mono_class_setup_methods (class);
3029 for (index = 0; index < class->method.count; ++index) {
3030 MonoMethod *cm = class->methods [index];
3031 method_signature = mono_signature_get_desc (mono_method_signature (cm), TRUE);
3033 printf ("METHOD %s(%s)\n", cm->name, method_signature);
3034 g_free (method_signature);
3039 * LOCKING: this is supposed to be called with the loader lock held.
3042 mono_class_setup_vtable_general (MonoClass *class, MonoMethod **overrides, int onum)
3045 MonoMethod **vtable;
3046 int i, max_vtsize = 0, max_iid, cur_slot = 0;
3047 GPtrArray *ifaces = NULL;
3048 GHashTable *override_map = NULL;
3049 gboolean security_enabled = mono_is_security_manager_active ();
3051 gpointer class_iter;
3052 #if (DEBUG_INTERFACE_VTABLE_CODE|TRACE_INTERFACE_VTABLE_CODE)
3053 int first_non_interface_slot;
3059 ifaces = mono_class_get_implemented_interfaces (class);
3061 for (i = 0; i < ifaces->len; i++) {
3062 MonoClass *ic = g_ptr_array_index (ifaces, i);
3063 max_vtsize += ic->method.count;
3065 g_ptr_array_free (ifaces, TRUE);
3069 if (class->parent) {
3070 mono_class_init (class->parent);
3071 mono_class_setup_vtable (class->parent);
3072 max_vtsize += class->parent->vtable_size;
3073 cur_slot = class->parent->vtable_size;
3076 max_vtsize += class->method.count;
3078 vtable = alloca (sizeof (gpointer) * max_vtsize);
3079 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
3081 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
3083 cur_slot = setup_interface_offsets (class, cur_slot);
3084 max_iid = class->max_interface_id;
3085 DEBUG_INTERFACE_VTABLE (first_non_interface_slot = cur_slot);
3087 /* Optimized version for generic instances */
3088 if (class->generic_class) {
3089 MonoClass *gklass = class->generic_class->container_class;
3092 mono_class_setup_vtable (gklass);
3094 tmp = mono_image_alloc0 (class->image, sizeof (gpointer) * gklass->vtable_size);
3095 class->vtable_size = gklass->vtable_size;
3096 for (i = 0; i < gklass->vtable_size; ++i)
3097 if (gklass->vtable [i]) {
3098 tmp [i] = mono_class_inflate_generic_method_full (gklass->vtable [i], class, mono_class_get_context (class));
3099 tmp [i]->slot = gklass->vtable [i]->slot;
3101 mono_memory_barrier ();
3102 class->vtable = tmp;
3104 /* Have to set method->slot for abstract virtual methods */
3105 if (class->methods && gklass->methods) {
3106 for (i = 0; i < class->method.count; ++i)
3107 if (class->methods [i]->slot == -1)
3108 class->methods [i]->slot = gklass->methods [i]->slot;
3114 if (class->parent && class->parent->vtable_size) {
3115 MonoClass *parent = class->parent;
3118 memcpy (vtable, parent->vtable, sizeof (gpointer) * parent->vtable_size);
3120 // Also inherit parent interface vtables, just as a starting point.
3121 // This is needed otherwise bug-77127.exe fails when the property methods
3122 // have different names in the iterface and the class, because for child
3123 // classes the ".override" information is not used anymore.
3124 for (i = 0; i < parent->interface_offsets_count; i++) {
3125 MonoClass *parent_interface = parent->interfaces_packed [i];
3126 int interface_offset = mono_class_interface_offset (class, parent_interface);
3128 if (interface_offset >= parent->vtable_size) {
3129 int parent_interface_offset = mono_class_interface_offset (parent, parent_interface);
3132 mono_class_setup_methods (parent_interface);
3133 TRACE_INTERFACE_VTABLE (printf (" +++ Inheriting interface %s.%s\n", parent_interface->name_space, parent_interface->name));
3134 for (j = 0; j < parent_interface->method.count; j++) {
3135 vtable [interface_offset + j] = parent->vtable [parent_interface_offset + j];
3136 TRACE_INTERFACE_VTABLE (printf (" --- Inheriting: [%03d][(%03d)+(%03d)] => [%03d][(%03d)+(%03d)]\n",
3137 parent_interface_offset + j, parent_interface_offset, j,
3138 interface_offset + j, interface_offset, j));
3145 TRACE_INTERFACE_VTABLE (print_vtable_full (class, vtable, cur_slot, first_non_interface_slot, "AFTER INHERITING PARENT VTABLE", TRUE));
3146 /* override interface methods */
3147 for (i = 0; i < onum; i++) {
3148 MonoMethod *decl = overrides [i*2];
3149 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
3151 dslot = mono_method_get_vtable_slot (decl) + mono_class_interface_offset (class, decl->klass);
3152 vtable [dslot] = overrides [i*2 + 1];
3153 vtable [dslot]->slot = dslot;
3155 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
3157 g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
3159 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR)
3160 mono_security_core_clr_check_override (class, vtable [dslot], decl);
3163 TRACE_INTERFACE_VTABLE (print_overrides (override_map, "AFTER OVERRIDING INTERFACE METHODS"));
3164 TRACE_INTERFACE_VTABLE (print_vtable_full (class, vtable, cur_slot, first_non_interface_slot, "AFTER OVERRIDING INTERFACE METHODS", FALSE));
3166 // Loop on all implemented interfaces...
3167 for (i = 0; i < class->interface_offsets_count; i++) {
3168 MonoClass *parent = class->parent;
3170 gboolean interface_is_explicitly_implemented_by_class;
3173 ic = class->interfaces_packed [i];
3174 ic_offset = mono_class_interface_offset (class, ic);
3176 mono_class_setup_methods (ic);
3178 // Check if this interface is explicitly implemented (instead of just inherited)
3179 if (parent != NULL) {
3180 int implemented_interfaces_index;
3181 interface_is_explicitly_implemented_by_class = FALSE;
3182 for (implemented_interfaces_index = 0; implemented_interfaces_index < class->interface_count; implemented_interfaces_index++) {
3183 if (ic == class->interfaces [implemented_interfaces_index]) {
3184 interface_is_explicitly_implemented_by_class = TRUE;
3189 interface_is_explicitly_implemented_by_class = TRUE;
3192 // Loop on all interface methods...
3193 for (im_index = 0; im_index < ic->method.count; im_index++) {
3194 MonoMethod *im = ic->methods [im_index];
3195 int im_slot = ic_offset + im->slot;
3196 MonoMethod *override_im = (override_map != NULL) ? g_hash_table_lookup (override_map, im) : NULL;
3198 if (im->flags & METHOD_ATTRIBUTE_STATIC)
3201 // If there is an explicit implementation, just use it right away,
3202 // otherwise look for a matching method
3203 if (override_im == NULL) {
3208 // First look for a suitable method among the class methods
3210 while ((cm = mono_class_get_virtual_methods (class, &iter))) {
3211 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)));
3212 if (check_interface_method_override (class, im, cm, TRUE, interface_is_explicitly_implemented_by_class, (vtable [im_slot] == NULL), security_enabled)) {
3213 TRACE_INTERFACE_VTABLE (printf ("[check ok]: ASSIGNING"));
3214 vtable [im_slot] = cm;
3215 /* Why do we need this? */
3220 TRACE_INTERFACE_VTABLE (printf ("\n"));
3223 // If the slot is still empty, look in all the inherited virtual methods...
3224 if ((vtable [im_slot] == NULL) && class->parent != NULL) {
3225 MonoClass *parent = class->parent;
3226 // Reverse order, so that last added methods are preferred
3227 for (cm_index = parent->vtable_size - 1; cm_index >= 0; cm_index--) {
3228 MonoMethod *cm = parent->vtable [cm_index];
3230 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));
3231 if ((cm != NULL) && check_interface_method_override (class, im, cm, FALSE, FALSE, TRUE, security_enabled)) {
3232 TRACE_INTERFACE_VTABLE (printf ("[everything ok]: ASSIGNING"));
3233 vtable [im_slot] = cm;
3234 /* Why do we need this? */
3240 TRACE_INTERFACE_VTABLE ((cm != NULL) && printf ("\n"));
3244 g_assert (vtable [im_slot] == override_im);
3249 // If the class is not abstract, check that all its interface slots are full.
3250 // The check is done here and not directly at the end of the loop above because
3251 // it can happen (for injected generic array interfaces) that the same slot is
3252 // processed multiple times (those interfaces have overlapping slots), and it
3253 // will not always be the first pass the one that fills the slot.
3254 if (! (class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
3255 for (i = 0; i < class->interface_offsets_count; i++) {
3259 ic = class->interfaces_packed [i];
3260 ic_offset = mono_class_interface_offset (class, ic);
3262 for (im_index = 0; im_index < ic->method.count; im_index++) {
3263 MonoMethod *im = ic->methods [im_index];
3264 int im_slot = ic_offset + im->slot;
3266 if (im->flags & METHOD_ATTRIBUTE_STATIC)
3269 TRACE_INTERFACE_VTABLE (printf (" [class is not abstract, checking slot %d for interface '%s'.'%s', method %s, slot check is %d]\n",
3270 im_slot, ic->name_space, ic->name, im->name, (vtable [im_slot] == NULL)));
3271 if (vtable [im_slot] == NULL) {
3272 print_unimplemented_interface_method_info (class, ic, im, im_slot, overrides, onum);
3273 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
3275 g_hash_table_destroy (override_map);
3282 TRACE_INTERFACE_VTABLE (print_vtable_full (class, vtable, cur_slot, first_non_interface_slot, "AFTER SETTING UP INTERFACE METHODS", FALSE));
3284 while ((cm = mono_class_get_virtual_methods (class, &class_iter))) {
3286 * If the method is REUSE_SLOT, we must check in the
3287 * base class for a method to override.
3289 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
3291 for (k = class->parent; k ; k = k->parent) {
3296 while ((m1 = mono_class_get_virtual_methods (k, &k_iter))) {
3297 MonoMethodSignature *cmsig, *m1sig;
3299 cmsig = mono_method_signature (cm);
3300 m1sig = mono_method_signature (m1);
3302 if (!cmsig || !m1sig) {
3303 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
3307 if (!strcmp(cm->name, m1->name) &&
3308 mono_metadata_signature_equal (cmsig, m1sig)) {
3310 /* CAS - SecurityAction.InheritanceDemand */
3311 if (security_enabled && (m1->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
3312 mono_secman_inheritancedemand_method (cm, m1);
3315 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR)
3316 mono_security_core_clr_check_override (class, cm, m1);
3318 slot = mono_method_get_vtable_slot (m1);
3319 g_assert (cm->slot < max_vtsize);
3321 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
3322 g_hash_table_insert (override_map, m1, cm);
3334 cm->slot = cur_slot++;
3336 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
3337 vtable [cm->slot] = cm;
3340 /* override non interface methods */
3341 for (i = 0; i < onum; i++) {
3342 MonoMethod *decl = overrides [i*2];
3343 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
3344 g_assert (decl->slot != -1);
3345 vtable [decl->slot] = overrides [i*2 + 1];
3346 overrides [i * 2 + 1]->slot = decl->slot;
3348 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
3349 g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
3351 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR)
3352 mono_security_core_clr_check_override (class, vtable [decl->slot], decl);
3357 * If a method occupies more than one place in the vtable, and it is
3358 * overriden, then change the other occurances too.
3361 for (i = 0; i < max_vtsize; ++i)
3363 MonoMethod *cm = g_hash_table_lookup (override_map, vtable [i]);
3368 g_hash_table_destroy (override_map);
3371 if (class->generic_class) {
3372 MonoClass *gklass = class->generic_class->container_class;
3374 mono_class_init (gklass);
3376 class->vtable_size = MAX (gklass->vtable_size, cur_slot);
3378 /* Check that the vtable_size value computed in mono_class_init () is correct */
3379 if (class->vtable_size)
3380 g_assert (cur_slot == class->vtable_size);
3381 class->vtable_size = cur_slot;
3384 /* Try to share the vtable with our parent. */
3385 if (class->parent && (class->parent->vtable_size == class->vtable_size) && (memcmp (class->parent->vtable, vtable, sizeof (gpointer) * class->vtable_size) == 0)) {
3386 mono_memory_barrier ();
3387 class->vtable = class->parent->vtable;
3389 MonoMethod **tmp = mono_image_alloc0 (class->image, sizeof (gpointer) * class->vtable_size);
3390 memcpy (tmp, vtable, sizeof (gpointer) * class->vtable_size);
3391 mono_memory_barrier ();
3392 class->vtable = tmp;
3395 DEBUG_INTERFACE_VTABLE (print_vtable_full (class, class->vtable, class->vtable_size, first_non_interface_slot, "FINALLY", FALSE));
3396 if (mono_print_vtable) {
3399 print_implemented_interfaces (class);
3401 for (i = 0; i <= max_iid; i++)
3402 if (MONO_CLASS_IMPLEMENTS_INTERFACE (class, i))
3405 printf ("VTable %s (vtable entries = %d, interfaces = %d)\n", mono_type_full_name (&class->byval_arg),
3406 class->vtable_size, icount);
3408 for (i = 0; i < class->vtable_size; ++i) {
3413 printf (" slot assigned: %03d, slot index: %03d %s\n", i, cm->slot,
3414 mono_method_full_name (cm, TRUE));
3420 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
3421 class->name, max_iid);
3423 for (i = 0; i < class->interface_count; i++) {
3424 ic = class->interfaces [i];
3425 printf (" slot offset: %03d, method count: %03d, iid: %03d %s\n",
3426 mono_class_interface_offset (class, ic),
3427 ic->method.count, ic->interface_id, mono_type_full_name (&ic->byval_arg));
3430 for (k = class->parent; k ; k = k->parent) {
3431 for (i = 0; i < k->interface_count; i++) {
3432 ic = k->interfaces [i];
3433 printf (" slot offset: %03d, method count: %03d, iid: %03d %s\n",
3434 mono_class_interface_offset (class, ic),
3435 ic->method.count, ic->interface_id, mono_type_full_name (&ic->byval_arg));
3441 VERIFY_INTERFACE_VTABLE (mono_class_verify_vtable (class));
3445 * mono_method_get_vtable_slot:
3447 * Returns method->slot, computing it if neccesary.
3448 * LOCKING: Acquires the loader lock.
3451 mono_method_get_vtable_slot (MonoMethod *method)
3453 if (method->slot == -1) {
3454 mono_class_setup_vtable (method->klass);
3455 g_assert (method->slot != -1);
3457 return method->slot;
3461 * mono_method_get_vtable_index:
3464 * Returns the index into the runtime vtable to access the method or,
3465 * in the case of a virtual generic method, the virtual generic method
3469 mono_method_get_vtable_index (MonoMethod *method)
3471 if (method->is_inflated && (method->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
3472 MonoMethodInflated *imethod = (MonoMethodInflated*)method;
3473 if (imethod->declaring->is_generic)
3474 return mono_method_get_vtable_slot (imethod->declaring);
3476 return mono_method_get_vtable_slot (method);
3479 static MonoMethod *default_ghc = NULL;
3480 static MonoMethod *default_finalize = NULL;
3481 static int finalize_slot = -1;
3482 static int ghc_slot = -1;
3485 initialize_object_slots (MonoClass *class)
3490 if (class == mono_defaults.object_class) {
3491 mono_class_setup_vtable (class);
3492 for (i = 0; i < class->vtable_size; ++i) {
3493 MonoMethod *cm = class->vtable [i];
3495 if (!strcmp (cm->name, "GetHashCode"))
3497 else if (!strcmp (cm->name, "Finalize"))
3501 g_assert (ghc_slot > 0);
3502 default_ghc = class->vtable [ghc_slot];
3504 g_assert (finalize_slot > 0);
3505 default_finalize = class->vtable [finalize_slot];
3510 MonoMethod *array_method;
3512 } GenericArrayMethodInfo;
3514 static int generic_array_method_num = 0;
3515 static GenericArrayMethodInfo *generic_array_method_info = NULL;
3518 generic_array_methods (MonoClass *class)
3520 int i, count_generic = 0;
3521 GList *list = NULL, *tmp;
3522 if (generic_array_method_num)
3523 return generic_array_method_num;
3524 mono_class_setup_methods (class->parent);
3525 for (i = 0; i < class->parent->method.count; i++) {
3526 MonoMethod *m = class->parent->methods [i];
3527 if (!strncmp (m->name, "InternalArray__", 15)) {
3529 list = g_list_prepend (list, m);
3532 list = g_list_reverse (list);
3533 generic_array_method_info = g_malloc (sizeof (GenericArrayMethodInfo) * count_generic);
3535 for (tmp = list; tmp; tmp = tmp->next) {
3536 const char *mname, *iname;
3538 MonoMethod *m = tmp->data;
3539 generic_array_method_info [i].array_method = m;
3540 if (!strncmp (m->name, "InternalArray__ICollection_", 27)) {
3541 iname = "System.Collections.Generic.ICollection`1.";
3542 mname = m->name + 27;
3543 } else if (!strncmp (m->name, "InternalArray__IEnumerable_", 27)) {
3544 iname = "System.Collections.Generic.IEnumerable`1.";
3545 mname = m->name + 27;
3546 } else if (!strncmp (m->name, "InternalArray__", 15)) {
3547 iname = "System.Collections.Generic.IList`1.";
3548 mname = m->name + 15;
3550 g_assert_not_reached ();
3553 name = mono_image_alloc (mono_defaults.corlib, strlen (iname) + strlen (mname) + 1);
3554 strcpy (name, iname);
3555 strcpy (name + strlen (iname), mname);
3556 generic_array_method_info [i].name = name;
3559 /*g_print ("array generic methods: %d\n", count_generic);*/
3561 generic_array_method_num = count_generic;
3563 return generic_array_method_num;
3567 setup_generic_array_ifaces (MonoClass *class, MonoClass *iface, MonoMethod **methods, int pos)
3569 MonoGenericContext tmp_context;
3572 tmp_context.class_inst = NULL;
3573 tmp_context.method_inst = iface->generic_class->context.class_inst;
3574 //g_print ("setting up array interface: %s\n", mono_type_get_name_full (&iface->byval_arg, 0));
3576 for (i = 0; i < generic_array_method_num; i++) {
3577 MonoMethod *m = generic_array_method_info [i].array_method;
3578 MonoMethod *inflated;
3580 inflated = mono_class_inflate_generic_method (m, &tmp_context);
3581 methods [pos++] = mono_marshal_get_generic_array_helper (class, iface, generic_array_method_info [i].name, inflated);
3586 concat_two_strings_with_zero (MonoImage *image, const char *s1, const char *s2)
3588 int len = strlen (s1) + strlen (s2) + 2;
3589 char *s = mono_image_alloc (image, len);
3592 result = g_snprintf (s, len, "%s%c%s", s1, '\0', s2);
3593 g_assert (result == len - 1);
3599 set_failure_from_loader_error (MonoClass *class, MonoLoaderError *error)
3601 gpointer exception_data = NULL;
3603 switch (error->exception_type) {
3604 case MONO_EXCEPTION_TYPE_LOAD:
3605 exception_data = concat_two_strings_with_zero (class->image, error->class_name, error->assembly_name);
3608 case MONO_EXCEPTION_MISSING_METHOD:
3609 exception_data = concat_two_strings_with_zero (class->image, error->class_name, error->member_name);
3612 case MONO_EXCEPTION_MISSING_FIELD: {
3613 const char *name_space = error->klass->name_space ? error->klass->name_space : NULL;
3614 const char *class_name;
3617 class_name = g_strdup_printf ("%s.%s", name_space, error->klass->name);
3619 class_name = error->klass->name;
3621 exception_data = concat_two_strings_with_zero (class->image, class_name, error->member_name);
3624 g_free ((void*)class_name);
3628 case MONO_EXCEPTION_FILE_NOT_FOUND: {
3631 if (error->ref_only)
3632 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.";
3634 msg = "Could not load file or assembly '%s' or one of its dependencies.";
3636 exception_data = concat_two_strings_with_zero (class->image, msg, error->assembly_name);
3640 case MONO_EXCEPTION_BAD_IMAGE:
3641 exception_data = error->msg;
3645 g_assert_not_reached ();
3648 mono_class_set_failure (class, error->exception_type, exception_data);
3653 * @class: the class to initialize
3655 * Compute the instance_size, class_size and other infos that cannot be
3656 * computed at mono_class_get() time. Also compute vtable_size if possible.
3657 * Returns TRUE on success or FALSE if there was a problem in loading
3658 * the type (incorrect assemblies, missing assemblies, methods, etc).
3660 * LOCKING: Acquires the loader lock.
3663 mono_class_init (MonoClass *class)
3666 MonoCachedClassInfo cached_info;
3667 gboolean has_cached_info;
3668 int class_init_ok = TRUE;
3672 /* Double-checking locking pattern */
3674 return class->exception_type == MONO_EXCEPTION_NONE;
3676 /*g_print ("Init class %s\n", class->name);*/
3678 /* We do everything inside the lock to prevent races */
3679 mono_loader_lock ();
3681 if (class->inited) {
3682 mono_loader_unlock ();
3683 /* Somebody might have gotten in before us */
3684 return class->exception_type == MONO_EXCEPTION_NONE;
3687 if (class->init_pending) {
3688 mono_loader_unlock ();
3689 /* this indicates a cyclic dependency */
3690 g_error ("pending init %s.%s\n", class->name_space, class->name);
3693 class->init_pending = 1;
3695 /* CAS - SecurityAction.InheritanceDemand */
3696 if (mono_is_security_manager_active () && class->parent && (class->parent->flags & TYPE_ATTRIBUTE_HAS_SECURITY)) {
3697 mono_secman_inheritancedemand_class (class, class->parent);
3700 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR)
3701 mono_security_core_clr_check_inheritance (class);
3703 mono_stats.initialized_class_count++;
3705 if (class->generic_class && !class->generic_class->is_dynamic) {
3706 MonoClass *gklass = class->generic_class->container_class;
3708 mono_stats.generic_class_count++;
3710 class->method = gklass->method;
3711 class->field = gklass->field;
3713 mono_class_init (gklass);
3714 // FIXME: Why is this needed ?
3715 mono_class_setup_methods (gklass);
3717 if (MONO_CLASS_IS_INTERFACE (class))
3718 class->interface_id = mono_get_unique_iid (class);
3721 if (class->parent && !class->parent->inited)
3722 mono_class_init (class->parent);
3724 has_cached_info = mono_class_get_cached_class_info (class, &cached_info);
3726 if (class->generic_class || class->image->dynamic || !class->type_token || (has_cached_info && !cached_info.has_nested_classes))
3727 class->nested_classes_inited = TRUE;
3730 * Computes the size used by the fields, and their locations
3732 if (has_cached_info) {
3733 class->instance_size = cached_info.instance_size;
3734 class->sizes.class_size = cached_info.class_size;
3735 class->packing_size = cached_info.packing_size;
3736 class->min_align = cached_info.min_align;
3737 class->blittable = cached_info.blittable;
3738 class->has_references = cached_info.has_references;
3739 class->has_static_refs = cached_info.has_static_refs;
3740 class->no_special_static_fields = cached_info.no_special_static_fields;
3743 if (!class->size_inited){
3744 mono_class_setup_fields (class);
3745 if (class->exception_type || mono_loader_get_last_error ()){
3746 class_init_ok = FALSE;
3751 /* Initialize arrays */
3753 class->method.count = 3 + (class->rank > 1? 2: 1);
3755 if (class->interface_count) {
3756 int count_generic = generic_array_methods (class);
3757 class->method.count += class->interface_count * count_generic;
3761 mono_class_setup_supertypes (class);
3764 initialize_object_slots (class);
3767 * Initialize the rest of the data without creating a generic vtable if possible.
3768 * If possible, also compute vtable_size, so mono_class_create_runtime_vtable () can
3769 * also avoid computing a generic vtable.
3771 if (has_cached_info) {
3773 class->vtable_size = cached_info.vtable_size;
3774 class->has_finalize = cached_info.has_finalize;
3775 class->ghcimpl = cached_info.ghcimpl;
3776 class->has_cctor = cached_info.has_cctor;
3777 } else if (class->rank == 1 && class->byval_arg.type == MONO_TYPE_SZARRAY) {
3778 static int szarray_vtable_size = 0;
3781 if (!szarray_vtable_size) {
3782 mono_class_setup_vtable (class);
3783 szarray_vtable_size = class->vtable_size;
3785 class->vtable_size = szarray_vtable_size;
3787 } else if (class->generic_class && !MONO_CLASS_IS_INTERFACE (class)) {
3788 MonoClass *gklass = class->generic_class->container_class;
3790 /* Generic instance case */
3791 class->ghcimpl = gklass->ghcimpl;
3792 class->has_finalize = gklass->has_finalize;
3793 class->has_cctor = gklass->has_cctor;
3795 mono_class_setup_vtable (gklass);
3796 if (gklass->exception_type)
3799 class->vtable_size = gklass->vtable_size;
3803 /* ghcimpl is not currently used
3805 if (class->parent) {
3806 MonoMethod *cmethod = class->vtable [ghc_slot];
3807 if (cmethod->is_inflated)
3808 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
3809 if (cmethod == default_ghc) {
3815 /* Interfaces and valuetypes are not supposed to have finalizers */
3816 if (!(MONO_CLASS_IS_INTERFACE (class) || class->valuetype)) {
3817 MonoMethod *cmethod = NULL;
3819 if (class->parent && class->parent->has_finalize) {
3820 class->has_finalize = 1;
3822 if (class->type_token) {
3823 cmethod = find_method_in_metadata (class, "Finalize", 0, METHOD_ATTRIBUTE_VIRTUAL);
3824 } else if (class->parent) {
3825 /* FIXME: Optimize this */
3826 mono_class_setup_vtable (class);
3827 if (class->exception_type || mono_loader_get_last_error ())
3829 cmethod = class->vtable [finalize_slot];
3833 /* Check that this is really the finalizer method */
3834 mono_class_setup_vtable (class);
3835 if (class->exception_type || mono_loader_get_last_error ())
3838 class->has_finalize = 0;
3839 if (class->parent) {
3840 cmethod = class->vtable [finalize_slot];
3841 if (cmethod->is_inflated)
3842 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
3843 if (cmethod != default_finalize) {
3844 class->has_finalize = 1;
3851 /* C# doesn't allow interfaces to have cctors */
3852 if (!MONO_CLASS_IS_INTERFACE (class) || class->image != mono_defaults.corlib) {
3853 MonoMethod *cmethod = NULL;
3855 if (class->type_token) {
3856 cmethod = find_method_in_metadata (class, ".cctor", 0, METHOD_ATTRIBUTE_SPECIAL_NAME);
3857 /* The find_method function ignores the 'flags' argument */
3858 if (cmethod && (cmethod->flags & METHOD_ATTRIBUTE_SPECIAL_NAME))
3859 class->has_cctor = 1;
3861 mono_class_setup_methods (class);
3863 for (i = 0; i < class->method.count; ++i) {
3864 MonoMethod *method = class->methods [i];
3865 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
3866 (strcmp (".cctor", method->name) == 0)) {
3867 class->has_cctor = 1;
3875 if (!mono_setup_vtable_in_class_init) {
3877 * This is an embedding API break, since the caller might assume that
3878 * mono_class_init () constructs a generic vtable, so vtable construction errors
3879 * are visible right after the mono_class_init (), and not after
3880 * mono_class_vtable ().
3882 if (class->parent) {
3883 /* This will compute class->parent->vtable_size for some classes */
3884 mono_class_init (class->parent);
3885 if (class->parent->exception_type || mono_loader_get_last_error ())
3887 if (!class->parent->vtable_size) {
3888 /* FIXME: Get rid of this somehow */
3889 mono_class_setup_vtable (class->parent);
3890 if (class->parent->exception_type || mono_loader_get_last_error ())
3893 setup_interface_offsets (class, class->parent->vtable_size);
3895 setup_interface_offsets (class, 0);
3898 mono_class_setup_vtable (class);
3900 if (MONO_CLASS_IS_INTERFACE (class))
3901 setup_interface_offsets (class, 0);
3904 if (mono_verifier_is_enabled_for_class (class) && !mono_verifier_verify_class (class)) {
3905 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, concat_two_strings_with_zero (class->image, class->name, class->image->assembly_name));
3906 class_init_ok = FALSE;
3912 class_init_ok = FALSE;
3915 /* Because of the double-checking locking pattern */
3916 mono_memory_barrier ();
3918 class->init_pending = 0;
3920 if (mono_loader_get_last_error ()) {
3921 if (class->exception_type == MONO_EXCEPTION_NONE)
3922 set_failure_from_loader_error (class, mono_loader_get_last_error ());
3924 mono_loader_clear_error ();
3927 mono_loader_unlock ();
3929 if (mono_debugger_class_init_func)
3930 mono_debugger_class_init_func (class);
3932 return class_init_ok;
3936 is_corlib_image (MonoImage *image)
3938 /* FIXME: allow the dynamic case for our compilers and with full trust */
3940 return image->assembly && !strcmp (image->assembly->aname.name, "mscorlib");
3942 return image == mono_defaults.corlib;
3946 * LOCKING: this assumes the loader lock is held
3949 mono_class_setup_mono_type (MonoClass *class)
3951 const char *name = class->name;
3952 const char *nspace = class->name_space;
3953 gboolean is_corlib = is_corlib_image (class->image);
3955 class->this_arg.byref = 1;
3956 class->this_arg.data.klass = class;
3957 class->this_arg.type = MONO_TYPE_CLASS;
3958 class->byval_arg.data.klass = class;
3959 class->byval_arg.type = MONO_TYPE_CLASS;
3961 if (is_corlib && !strcmp (nspace, "System")) {
3962 if (!strcmp (name, "ValueType")) {
3964 * do not set the valuetype bit for System.ValueType.
3965 * class->valuetype = 1;
3967 class->blittable = TRUE;
3968 } else if (!strcmp (name, "Enum")) {
3970 * do not set the valuetype bit for System.Enum.
3971 * class->valuetype = 1;
3973 class->valuetype = 0;
3974 class->enumtype = 0;
3975 } else if (!strcmp (name, "Object")) {
3976 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
3977 } else if (!strcmp (name, "String")) {
3978 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
3979 } else if (!strcmp (name, "TypedReference")) {
3980 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
3984 if (class->valuetype) {
3985 int t = MONO_TYPE_VALUETYPE;
3987 if (is_corlib && !strcmp (nspace, "System")) {
3990 if (!strcmp (name, "Boolean")) {
3991 t = MONO_TYPE_BOOLEAN;
3992 } else if (!strcmp(name, "Byte")) {
3994 class->blittable = TRUE;
3998 if (!strcmp (name, "Char")) {
4003 if (!strcmp (name, "Double")) {
4005 class->blittable = TRUE;
4009 if (!strcmp (name, "Int32")) {
4011 class->blittable = TRUE;
4012 } else if (!strcmp(name, "Int16")) {
4014 class->blittable = TRUE;
4015 } else if (!strcmp(name, "Int64")) {
4017 class->blittable = TRUE;
4018 } else if (!strcmp(name, "IntPtr")) {
4020 class->blittable = TRUE;
4024 if (!strcmp (name, "Single")) {
4026 class->blittable = TRUE;
4027 } else if (!strcmp(name, "SByte")) {
4029 class->blittable = TRUE;
4033 if (!strcmp (name, "UInt32")) {
4035 class->blittable = TRUE;
4036 } else if (!strcmp(name, "UInt16")) {
4038 class->blittable = TRUE;
4039 } else if (!strcmp(name, "UInt64")) {
4041 class->blittable = TRUE;
4042 } else if (!strcmp(name, "UIntPtr")) {
4044 class->blittable = TRUE;
4048 if (!strcmp (name, "TypedReference")) {
4049 t = MONO_TYPE_TYPEDBYREF;
4050 class->blittable = TRUE;
4054 if (!strcmp (name, "Void")) {
4062 class->this_arg.type = class->byval_arg.type = t;
4065 if (MONO_CLASS_IS_INTERFACE (class))
4066 class->interface_id = mono_get_unique_iid (class);
4071 * LOCKING: this assumes the loader lock is held
4074 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
4076 gboolean system_namespace;
4077 gboolean is_corlib = is_corlib_image (class->image);
4079 system_namespace = !strcmp (class->name_space, "System") && is_corlib;
4081 /* if root of the hierarchy */
4082 if (system_namespace && !strcmp (class->name, "Object")) {
4083 class->parent = NULL;
4084 class->instance_size = sizeof (MonoObject);
4087 if (!strcmp (class->name, "<Module>")) {
4088 class->parent = NULL;
4089 class->instance_size = 0;
4093 if (!MONO_CLASS_IS_INTERFACE (class)) {
4094 /* Imported COM Objects always derive from __ComObject. */
4095 if (MONO_CLASS_IS_IMPORT (class)) {
4096 mono_init_com_types ();
4097 if (parent == mono_defaults.object_class)
4098 parent = mono_defaults.com_object_class;
4101 /* set the parent to something useful and safe, but mark the type as broken */
4102 parent = mono_defaults.object_class;
4103 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
4106 class->parent = parent;
4108 if (parent->generic_class && !parent->name) {
4110 * If the parent is a generic instance, we may get
4111 * called before it is fully initialized, especially
4112 * before it has its name.
4117 class->marshalbyref = parent->marshalbyref;
4118 class->contextbound = parent->contextbound;
4119 class->delegate = parent->delegate;
4120 if (MONO_CLASS_IS_IMPORT (class))
4121 class->is_com_object = 1;
4123 class->is_com_object = parent->is_com_object;
4125 if (system_namespace) {
4126 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
4127 class->marshalbyref = 1;
4129 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
4130 class->contextbound = 1;
4132 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
4133 class->delegate = 1;
4136 if (class->parent->enumtype || (is_corlib_image (class->parent->image) && (strcmp (class->parent->name, "ValueType") == 0) &&
4137 (strcmp (class->parent->name_space, "System") == 0)))
4138 class->valuetype = 1;
4139 if (is_corlib_image (class->parent->image) && ((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
4140 class->valuetype = class->enumtype = 1;
4142 /*class->enumtype = class->parent->enumtype; */
4143 mono_class_setup_supertypes (class);
4145 /* initialize com types if COM interfaces are present */
4146 if (MONO_CLASS_IS_IMPORT (class))
4147 mono_init_com_types ();
4148 class->parent = NULL;
4154 * mono_class_setup_supertypes:
4157 * Build the data structure needed to make fast type checks work.
4158 * This currently sets two fields in @class:
4159 * - idepth: distance between @class and System.Object in the type
4161 * - supertypes: array of classes: each element has a class in the hierarchy
4162 * starting from @class up to System.Object
4164 * LOCKING: this assumes the loader lock is held
4167 mono_class_setup_supertypes (MonoClass *class)
4171 if (class->supertypes)
4174 if (class->parent && !class->parent->supertypes)
4175 mono_class_setup_supertypes (class->parent);
4177 class->idepth = class->parent->idepth + 1;
4181 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
4182 class->supertypes = mono_image_alloc0 (class->image, sizeof (MonoClass *) * ms);
4184 if (class->parent) {
4185 class->supertypes [class->idepth - 1] = class;
4186 memcpy (class->supertypes, class->parent->supertypes, class->parent->idepth * sizeof (gpointer));
4188 class->supertypes [0] = class;
4193 * mono_class_create_from_typedef:
4194 * @image: image where the token is valid
4195 * @type_token: typedef token
4197 * Create the MonoClass* representing the specified type token.
4198 * @type_token must be a TypeDef token.
4201 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
4203 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
4204 MonoClass *class, *parent = NULL;
4205 guint32 cols [MONO_TYPEDEF_SIZE];
4206 guint32 cols_next [MONO_TYPEDEF_SIZE];
4207 guint tidx = mono_metadata_token_index (type_token);
4208 MonoGenericContext *context = NULL;
4209 const char *name, *nspace;
4211 MonoClass **interfaces;
4212 guint32 field_last, method_last;
4213 guint32 nesting_tokeen;
4215 mono_loader_lock ();
4217 if ((class = mono_internal_hash_table_lookup (&image->class_cache, GUINT_TO_POINTER (type_token)))) {
4218 mono_loader_unlock ();
4222 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
4224 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
4226 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
4227 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
4229 class = mono_image_alloc0 (image, sizeof (MonoClass));
4232 class->name_space = nspace;
4234 mono_profiler_class_event (class, MONO_PROFILE_START_LOAD);
4236 class->image = image;
4237 class->type_token = type_token;
4238 class->flags = cols [MONO_TYPEDEF_FLAGS];
4240 mono_internal_hash_table_insert (&image->class_cache, GUINT_TO_POINTER (type_token), class);
4242 classes_size += sizeof (MonoClass);
4245 * Check whether we're a generic type definition.
4247 class->generic_container = mono_metadata_load_generic_params (image, class->type_token, NULL);
4248 if (class->generic_container) {
4249 class->is_generic = 1;
4250 class->generic_container->owner.klass = class;
4251 context = &class->generic_container->context;
4254 if (cols [MONO_TYPEDEF_EXTENDS]) {
4255 guint32 parent_token = mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]);
4257 if (mono_metadata_token_table (parent_token) == MONO_TABLE_TYPESPEC) {
4258 /*WARNING: this must satisfy mono_metadata_type_hash*/
4259 class->this_arg.byref = 1;
4260 class->this_arg.data.klass = class;
4261 class->this_arg.type = MONO_TYPE_CLASS;
4262 class->byval_arg.data.klass = class;
4263 class->byval_arg.type = MONO_TYPE_CLASS;
4265 parent = mono_class_get_full (image, parent_token, context);
4267 if (parent == NULL){
4268 mono_internal_hash_table_remove (&image->class_cache, GUINT_TO_POINTER (type_token));
4269 mono_loader_unlock ();
4270 mono_profiler_class_loaded (class, MONO_PROFILE_FAILED);
4275 /* do this early so it's available for interfaces in setup_mono_type () */
4276 if ((nesting_tokeen = mono_metadata_nested_in_typedef (image, type_token)))
4277 class->nested_in = mono_class_create_from_typedef (image, nesting_tokeen);
4279 mono_class_setup_parent (class, parent);
4281 /* uses ->valuetype, which is initialized by mono_class_setup_parent above */
4282 mono_class_setup_mono_type (class);
4284 if (!class->enumtype) {
4285 if (!mono_metadata_interfaces_from_typedef_full (
4286 image, type_token, &interfaces, &icount, context)){
4287 mono_loader_unlock ();
4288 mono_profiler_class_loaded (class, MONO_PROFILE_FAILED);
4292 class->interfaces = interfaces;
4293 class->interface_count = icount;
4294 class->interfaces_inited = 1;
4297 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
4301 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
4305 class->cast_class = class->element_class = class;
4307 /*g_print ("Load class %s\n", name);*/
4310 * Compute the field and method lists
4312 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
4313 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
4315 if (tt->rows > tidx){
4316 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
4317 field_last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
4318 method_last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
4320 field_last = image->tables [MONO_TABLE_FIELD].rows;
4321 method_last = image->tables [MONO_TABLE_METHOD].rows;
4324 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
4325 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
4326 class->field.count = field_last - class->field.first;
4328 class->field.count = 0;
4330 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
4331 class->method.count = method_last - class->method.first;
4333 class->method.count = 0;
4335 /* reserve space to store vector pointer in arrays */
4336 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
4337 class->instance_size += 2 * sizeof (gpointer);
4338 g_assert (class->field.count == 0);
4341 if (class->enumtype) {
4342 MonoType *enum_basetype = mono_class_find_enum_basetype (class);
4343 if (!enum_basetype) {
4344 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
4345 mono_loader_unlock ();
4348 class->cast_class = class->element_class = mono_class_from_mono_type (enum_basetype);
4352 * If we're a generic type definition, load the constraints.
4353 * We must do this after the class has been constructed to make certain recursive scenarios
4356 if (class->generic_container)
4357 mono_metadata_load_generic_param_constraints (
4358 image, type_token, class->generic_container);
4360 if (class->image->assembly_name && !strcmp (class->image->assembly_name, "Mono.Simd") && !strcmp (nspace, "Mono.Simd")) {
4361 if (!strncmp (name, "Vector", 6))
4362 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");
4365 mono_loader_unlock ();
4367 mono_profiler_class_loaded (class, MONO_PROFILE_OK);
4372 /** is klass Nullable<T>? */
4374 mono_class_is_nullable (MonoClass *klass)
4376 return klass->generic_class != NULL &&
4377 klass->generic_class->container_class == mono_defaults.generic_nullable_class;
4381 /** if klass is T? return T */
4383 mono_class_get_nullable_param (MonoClass *klass)
4385 g_assert (mono_class_is_nullable (klass));
4386 return mono_class_from_mono_type (klass->generic_class->context.class_inst->type_argv [0]);
4390 * Create the `MonoClass' for an instantiation of a generic type.
4391 * We only do this if we actually need it.
4394 mono_generic_class_get_class (MonoGenericClass *gclass)
4396 MonoClass *klass, *gklass;
4398 mono_loader_lock ();
4399 if (gclass->cached_class) {
4400 mono_loader_unlock ();
4401 return gclass->cached_class;
4404 gclass->cached_class = g_malloc0 (sizeof (MonoClass));
4405 klass = gclass->cached_class;
4407 gklass = gclass->container_class;
4409 if (gklass->nested_in) {
4411 * FIXME: the nested type context should include everything the
4412 * nesting context should have, but it may also have additional
4413 * generic parameters...
4415 klass->nested_in = mono_class_inflate_generic_class (gklass->nested_in,
4416 mono_generic_class_get_context (gclass));
4419 klass->name = gklass->name;
4420 klass->name_space = gklass->name_space;
4422 mono_profiler_class_event (klass, MONO_PROFILE_START_LOAD);
4424 klass->image = gklass->image;
4425 klass->flags = gklass->flags;
4426 klass->type_token = gklass->type_token;
4427 klass->field.count = gklass->field.count;
4429 klass->is_inflated = 1;
4430 klass->generic_class = gclass;
4432 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
4433 klass->this_arg.data.generic_class = klass->byval_arg.data.generic_class = gclass;
4434 klass->this_arg.byref = TRUE;
4435 klass->enumtype = gklass->enumtype;
4436 klass->valuetype = gklass->valuetype;
4438 klass->cast_class = klass->element_class = klass;
4440 if (mono_class_is_nullable (klass))
4441 klass->cast_class = klass->element_class = mono_class_get_nullable_param (klass);
4444 * We're not interested in the nested classes of a generic instance.
4445 * We use the generic type definition to look for nested classes.
4448 if (gklass->parent) {
4449 klass->parent = mono_class_inflate_generic_class (gklass->parent, mono_generic_class_get_context (gclass));
4453 mono_class_setup_parent (klass, klass->parent);
4455 if (klass->enumtype) {
4456 klass->cast_class = gklass->cast_class;
4457 klass->element_class = gklass->element_class;
4460 if (gclass->is_dynamic) {
4463 mono_class_setup_supertypes (klass);
4465 if (klass->enumtype) {
4467 * For enums, gklass->fields might not been set, but instance_size etc. is
4468 * already set in mono_reflection_create_internal_class (). For non-enums,
4469 * these will be computed normally in mono_class_layout_fields ().
4471 klass->instance_size = gklass->instance_size;
4472 klass->sizes.class_size = gklass->sizes.class_size;
4473 klass->size_inited = 1;
4477 mono_profiler_class_loaded (klass, MONO_PROFILE_OK);
4479 inflated_classes ++;
4480 inflated_classes_size += sizeof (MonoClass);
4482 mono_loader_unlock ();
4488 make_generic_param_class (MonoGenericParam *param, MonoImage *image, gboolean is_mvar, MonoGenericParamInfo *pinfo)
4490 MonoClass *klass, **ptr;
4495 image = mono_defaults.corlib;
4497 klass = mono_image_alloc0 (image, sizeof (MonoClass));
4498 classes_size += sizeof (MonoClass);
4501 klass->name = pinfo->name;
4503 int n = mono_generic_param_num (param);
4504 klass->name = mono_image_alloc0 (image, 16);
4505 sprintf ((char*)klass->name, "%d", n);
4508 klass->name_space = "";
4509 mono_profiler_class_event (klass, MONO_PROFILE_START_LOAD);
4513 for (ptr = pinfo->constraints; ptr && *ptr; ptr++, count++)
4517 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (pinfo->constraints [0])) {
4518 klass->parent = pinfo->constraints [0];
4520 } else if (pinfo && pinfo->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT)
4521 klass->parent = mono_class_from_name (mono_defaults.corlib, "System", "ValueType");
4523 klass->parent = mono_defaults.object_class;
4526 if (count - pos > 0) {
4527 klass->interface_count = count - pos;
4528 klass->interfaces = mono_image_alloc0 (image, sizeof (MonoClass *) * (count - pos));
4529 for (i = pos; i < count; i++)
4530 klass->interfaces [i - pos] = pinfo->constraints [i];
4533 klass->image = image;
4535 klass->inited = TRUE;
4536 klass->cast_class = klass->element_class = klass;
4537 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
4539 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
4540 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
4541 klass->this_arg.byref = TRUE;
4543 /* FIXME: shouldn't this be ->type_token? */
4544 klass->sizes.generic_param_token = pinfo ? pinfo->token : 0;
4546 mono_class_setup_supertypes (klass);
4551 #define FAST_CACHE_SIZE 1024
4552 static MonoClass *var_cache_fast [FAST_CACHE_SIZE];
4553 static MonoClass *mvar_cache_fast [FAST_CACHE_SIZE];
4554 static GHashTable *var_cache_slow;
4555 static GHashTable *mvar_cache_slow;
4558 get_anon_gparam_class (MonoGenericParam *param, gboolean is_mvar)
4560 int n = mono_generic_param_num (param);
4563 if (n < FAST_CACHE_SIZE)
4564 return (is_mvar ? mvar_cache_fast : var_cache_fast) [n];
4565 ht = is_mvar ? mvar_cache_slow : var_cache_slow;
4566 return ht ? g_hash_table_lookup (ht, GINT_TO_POINTER (n)) : NULL;
4570 set_anon_gparam_class (MonoGenericParam *param, gboolean is_mvar, MonoClass *klass)
4572 int n = mono_generic_param_num (param);
4575 if (n < FAST_CACHE_SIZE) {
4576 (is_mvar ? mvar_cache_fast : var_cache_fast) [n] = klass;
4579 ht = is_mvar ? mvar_cache_slow : var_cache_slow;
4581 ht = g_hash_table_new (NULL, NULL);
4583 mvar_cache_slow = ht;
4585 var_cache_slow = ht;
4588 g_hash_table_insert (ht, GINT_TO_POINTER (n), klass);
4592 * LOCKING: Acquires the loader lock.
4595 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
4597 MonoGenericContainer *container = mono_generic_param_owner (param);
4598 MonoGenericParamInfo *pinfo;
4601 mono_loader_lock ();
4604 pinfo = mono_generic_param_info (param);
4605 if (pinfo->pklass) {
4606 mono_loader_unlock ();
4607 return pinfo->pklass;
4613 klass = get_anon_gparam_class (param, is_mvar);
4615 mono_loader_unlock ();
4620 if (!image && container) {
4622 MonoMethod *method = container->owner.method;
4623 image = (method && method->klass) ? method->klass->image : NULL;
4625 MonoClass *klass = container->owner.klass;
4626 // FIXME: 'klass' should not be null
4627 // But, monodis creates GenericContainers without associating a owner to it
4628 image = klass ? klass->image : NULL;
4632 klass = make_generic_param_class (param, image, is_mvar, pinfo);
4634 mono_memory_barrier ();
4637 pinfo->pklass = klass;
4639 set_anon_gparam_class (param, is_mvar, klass);
4641 mono_loader_unlock ();
4643 /* FIXME: Should this go inside 'make_generic_param_klass'? */
4644 mono_profiler_class_loaded (klass, MONO_PROFILE_OK);
4650 mono_ptr_class_get (MonoType *type)
4653 MonoClass *el_class;
4657 el_class = mono_class_from_mono_type (type);
4658 image = el_class->image;
4660 mono_loader_lock ();
4662 if (!image->ptr_cache)
4663 image->ptr_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
4665 if ((result = g_hash_table_lookup (image->ptr_cache, el_class))) {
4666 mono_loader_unlock ();
4669 result = mono_image_alloc0 (image, sizeof (MonoClass));
4671 classes_size += sizeof (MonoClass);
4673 result->parent = NULL; /* no parent for PTR types */
4674 result->name_space = el_class->name_space;
4675 name = g_strdup_printf ("%s*", el_class->name);
4676 result->name = mono_image_strdup (image, name);
4679 mono_profiler_class_event (result, MONO_PROFILE_START_LOAD);
4681 result->image = el_class->image;
4682 result->inited = TRUE;
4683 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
4684 /* Can pointers get boxed? */
4685 result->instance_size = sizeof (gpointer);
4686 result->cast_class = result->element_class = el_class;
4687 result->blittable = TRUE;
4689 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
4690 result->this_arg.data.type = result->byval_arg.data.type = &result->element_class->byval_arg;
4691 result->this_arg.byref = TRUE;
4693 mono_class_setup_supertypes (result);
4695 g_hash_table_insert (image->ptr_cache, el_class, result);
4697 mono_loader_unlock ();
4699 mono_profiler_class_loaded (result, MONO_PROFILE_OK);
4705 mono_fnptr_class_get (MonoMethodSignature *sig)
4708 static GHashTable *ptr_hash = NULL;
4710 /* FIXME: These should be allocate from a mempool as well, but which one ? */
4712 mono_loader_lock ();
4715 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
4717 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
4718 mono_loader_unlock ();
4721 result = g_new0 (MonoClass, 1);
4723 result->parent = NULL; /* no parent for PTR types */
4724 result->name_space = "System";
4725 result->name = "MonoFNPtrFakeClass";
4727 mono_profiler_class_event (result, MONO_PROFILE_START_LOAD);
4729 result->image = mono_defaults.corlib; /* need to fix... */
4730 result->inited = TRUE;
4731 result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
4732 /* Can pointers get boxed? */
4733 result->instance_size = sizeof (gpointer);
4734 result->cast_class = result->element_class = result;
4735 result->blittable = TRUE;
4737 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
4738 result->this_arg.data.method = result->byval_arg.data.method = sig;
4739 result->this_arg.byref = TRUE;
4740 result->blittable = TRUE;
4742 mono_class_setup_supertypes (result);
4744 g_hash_table_insert (ptr_hash, sig, result);
4746 mono_loader_unlock ();
4748 mono_profiler_class_loaded (result, MONO_PROFILE_OK);
4754 mono_class_from_mono_type (MonoType *type)
4756 switch (type->type) {
4757 case MONO_TYPE_OBJECT:
4758 return type->data.klass? type->data.klass: mono_defaults.object_class;
4759 case MONO_TYPE_VOID:
4760 return type->data.klass? type->data.klass: mono_defaults.void_class;
4761 case MONO_TYPE_BOOLEAN:
4762 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
4763 case MONO_TYPE_CHAR:
4764 return type->data.klass? type->data.klass: mono_defaults.char_class;
4766 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
4768 return type->data.klass? type->data.klass: mono_defaults.byte_class;
4770 return type->data.klass? type->data.klass: mono_defaults.int16_class;
4772 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
4774 return type->data.klass? type->data.klass: mono_defaults.int32_class;
4776 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
4778 return type->data.klass? type->data.klass: mono_defaults.int_class;
4780 return type->data.klass? type->data.klass: mono_defaults.uint_class;
4782 return type->data.klass? type->data.klass: mono_defaults.int64_class;
4784 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
4786 return type->data.klass? type->data.klass: mono_defaults.single_class;
4788 return type->data.klass? type->data.klass: mono_defaults.double_class;
4789 case MONO_TYPE_STRING:
4790 return type->data.klass? type->data.klass: mono_defaults.string_class;
4791 case MONO_TYPE_TYPEDBYREF:
4792 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
4793 case MONO_TYPE_ARRAY:
4794 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
4796 return mono_ptr_class_get (type->data.type);
4797 case MONO_TYPE_FNPTR:
4798 return mono_fnptr_class_get (type->data.method);
4799 case MONO_TYPE_SZARRAY:
4800 return mono_array_class_get (type->data.klass, 1);
4801 case MONO_TYPE_CLASS:
4802 case MONO_TYPE_VALUETYPE:
4803 return type->data.klass;
4804 case MONO_TYPE_GENERICINST:
4805 return mono_generic_class_get_class (type->data.generic_class);
4807 return mono_class_from_generic_parameter (type->data.generic_param, NULL, FALSE);
4808 case MONO_TYPE_MVAR:
4809 return mono_class_from_generic_parameter (type->data.generic_param, NULL, TRUE);
4811 g_warning ("mono_class_from_mono_type: implement me 0x%02x\n", type->type);
4812 g_assert_not_reached ();
4819 * mono_type_retrieve_from_typespec
4820 * @image: context where the image is created
4821 * @type_spec: typespec token
4822 * @context: the generic context used to evaluate generic instantiations in
4825 mono_type_retrieve_from_typespec (MonoImage *image, guint32 type_spec, MonoGenericContext *context, gboolean *did_inflate)
4827 MonoType *t = mono_type_create_from_typespec (image, type_spec);
4830 if (context && (context->class_inst || context->method_inst)) {
4831 MonoType *inflated = inflate_generic_type (NULL, t, context);
4834 *did_inflate = TRUE;
4841 * mono_class_create_from_typespec
4842 * @image: context where the image is created
4843 * @type_spec: typespec token
4844 * @context: the generic context used to evaluate generic instantiations in
4847 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec, MonoGenericContext *context)
4850 gboolean inflated = FALSE;
4851 MonoType *t = mono_type_retrieve_from_typespec (image, type_spec, context, &inflated);
4854 ret = mono_class_from_mono_type (t);
4856 mono_metadata_free_type (t);
4861 * mono_bounded_array_class_get:
4862 * @element_class: element class
4863 * @rank: the dimension of the array class
4864 * @bounded: whenever the array has non-zero bounds
4866 * Returns: a class object describing the array with element type @element_type and
4870 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
4874 MonoClass *parent = NULL;
4875 GSList *list, *rootlist;
4878 gboolean corlib_type = FALSE;
4880 g_assert (rank <= 255);
4883 /* bounded only matters for one-dimensional arrays */
4886 image = eclass->image;
4888 if (rank == 1 && !bounded) {
4890 * This case is very frequent not just during compilation because of calls
4891 * from mono_class_from_mono_type (), mono_array_new (),
4892 * Array:CreateInstance (), etc, so use a separate cache + a separate lock.
4894 EnterCriticalSection (&image->szarray_cache_lock);
4895 if (!image->szarray_cache)
4896 image->szarray_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
4897 class = g_hash_table_lookup (image->szarray_cache, eclass);
4898 LeaveCriticalSection (&image->szarray_cache_lock);
4902 mono_loader_lock ();
4904 mono_loader_lock ();
4906 if (!image->array_cache)
4907 image->array_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
4909 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
4910 for (; list; list = list->next) {
4912 if ((class->rank == rank) && (class->byval_arg.type == (((rank > 1) || bounded) ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
4913 mono_loader_unlock ();
4920 /* for the building corlib use System.Array from it */
4921 if (image->assembly && image->assembly->dynamic && image->assembly_name && strcmp (image->assembly_name, "mscorlib") == 0) {
4922 parent = mono_class_from_name (image, "System", "Array");
4925 parent = mono_defaults.array_class;
4926 if (!parent->inited)
4927 mono_class_init (parent);
4930 class = mono_image_alloc0 (image, sizeof (MonoClass));
4932 class->image = image;
4933 class->name_space = eclass->name_space;
4934 nsize = strlen (eclass->name);
4935 name = g_malloc (nsize + 2 + rank + 1);
4936 memcpy (name, eclass->name, nsize);
4939 memset (name + nsize + 1, ',', rank - 1);
4941 name [nsize + rank] = '*';
4942 name [nsize + rank + bounded] = ']';
4943 name [nsize + rank + bounded + 1] = 0;
4944 class->name = mono_image_strdup (image, name);
4947 mono_profiler_class_event (class, MONO_PROFILE_START_LOAD);
4949 classes_size += sizeof (MonoClass);
4951 class->type_token = 0;
4952 /* all arrays are marked serializable and sealed, bug #42779 */
4953 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
4954 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
4955 class->parent = parent;
4956 class->instance_size = mono_class_instance_size (class->parent);
4958 if (eclass->enumtype && !mono_class_enum_basetype (eclass)) {
4959 if (!eclass->reflection_info || eclass->wastypebuilder) {
4960 g_warning ("Only incomplete TypeBuilder objects are allowed to be an enum without base_type");
4961 g_assert (eclass->reflection_info && !eclass->wastypebuilder);
4963 /* element_size -1 is ok as this is not an instantitable type*/
4964 class->sizes.element_size = -1;
4966 class->sizes.element_size = mono_class_array_element_size (eclass);
4968 mono_class_setup_supertypes (class);
4970 if (eclass->generic_class)
4971 mono_class_init (eclass);
4972 if (!eclass->size_inited)
4973 mono_class_setup_fields (eclass);
4974 class->has_references = MONO_TYPE_IS_REFERENCE (&eclass->byval_arg) || eclass->has_references? TRUE: FALSE;
4978 if (eclass->enumtype)
4979 class->cast_class = eclass->element_class;
4981 class->cast_class = eclass;
4983 class->element_class = eclass;
4985 if ((rank > 1) || bounded) {
4986 MonoArrayType *at = mono_image_alloc0 (image, sizeof (MonoArrayType));
4987 class->byval_arg.type = MONO_TYPE_ARRAY;
4988 class->byval_arg.data.array = at;
4989 at->eklass = eclass;
4991 /* FIXME: complete.... */
4993 class->byval_arg.type = MONO_TYPE_SZARRAY;
4994 class->byval_arg.data.klass = eclass;
4996 class->this_arg = class->byval_arg;
4997 class->this_arg.byref = 1;
5002 class->generic_container = eclass->generic_container;
5004 if (rank == 1 && !bounded) {
5005 MonoClass *prev_class;
5007 EnterCriticalSection (&image->szarray_cache_lock);
5008 prev_class = g_hash_table_lookup (image->szarray_cache, eclass);
5010 /* Someone got in before us */
5013 g_hash_table_insert (image->szarray_cache, eclass, class);
5014 LeaveCriticalSection (&image->szarray_cache_lock);
5016 list = g_slist_append (rootlist, class);
5017 g_hash_table_insert (image->array_cache, eclass, list);
5020 mono_loader_unlock ();
5022 mono_profiler_class_loaded (class, MONO_PROFILE_OK);
5028 * mono_array_class_get:
5029 * @element_class: element class
5030 * @rank: the dimension of the array class
5032 * Returns: a class object describing the array with element type @element_type and
5036 mono_array_class_get (MonoClass *eclass, guint32 rank)
5038 return mono_bounded_array_class_get (eclass, rank, FALSE);
5042 * mono_class_instance_size:
5045 * Returns: the size of an object instance
5048 mono_class_instance_size (MonoClass *klass)
5050 if (!klass->size_inited)
5051 mono_class_init (klass);
5053 return klass->instance_size;
5057 * mono_class_min_align:
5060 * Returns: minimm alignment requirements
5063 mono_class_min_align (MonoClass *klass)
5065 if (!klass->size_inited)
5066 mono_class_init (klass);
5068 return klass->min_align;
5072 * mono_class_value_size:
5075 * This function is used for value types, and return the
5076 * space and the alignment to store that kind of value object.
5078 * Returns: the size of a value of kind @klass
5081 mono_class_value_size (MonoClass *klass, guint32 *align)
5085 /* fixme: check disable, because we still have external revereces to
5086 * mscorlib and Dummy Objects
5088 /*g_assert (klass->valuetype);*/
5090 size = mono_class_instance_size (klass) - sizeof (MonoObject);
5093 *align = klass->min_align;
5099 * mono_class_data_size:
5102 * Returns: the size of the static class data
5105 mono_class_data_size (MonoClass *klass)
5108 mono_class_init (klass);
5110 /* in arrays, sizes.class_size is unioned with element_size
5111 * and arrays have no static fields
5115 return klass->sizes.class_size;
5119 * Auxiliary routine to mono_class_get_field
5121 * Takes a field index instead of a field token.
5123 static MonoClassField *
5124 mono_class_get_field_idx (MonoClass *class, int idx)
5126 mono_class_setup_fields_locking (class);
5129 if (class->image->uncompressed_metadata) {
5131 * class->field.first points to the FieldPtr table, while idx points into the
5132 * Field table, so we have to do a search.
5134 const char *name = mono_metadata_string_heap (class->image, mono_metadata_decode_row_col (&class->image->tables [MONO_TABLE_FIELD], idx, MONO_FIELD_NAME));
5137 for (i = 0; i < class->field.count; ++i)
5138 if (mono_field_get_name (&class->fields [i]) == name)
5139 return &class->fields [i];
5140 g_assert_not_reached ();
5142 if (class->field.count) {
5143 if ((idx >= class->field.first) && (idx < class->field.first + class->field.count)){
5144 return &class->fields [idx - class->field.first];
5148 class = class->parent;
5154 * mono_class_get_field:
5155 * @class: the class to lookup the field.
5156 * @field_token: the field token
5158 * Returns: A MonoClassField representing the type and offset of
5159 * the field, or a NULL value if the field does not belong to this
5163 mono_class_get_field (MonoClass *class, guint32 field_token)
5165 int idx = mono_metadata_token_index (field_token);
5167 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
5169 return mono_class_get_field_idx (class, idx - 1);
5173 * mono_class_get_field_from_name:
5174 * @klass: the class to lookup the field.
5175 * @name: the field name
5177 * Search the class @klass and it's parents for a field with the name @name.
5179 * Returns: the MonoClassField pointer of the named field or NULL
5182 mono_class_get_field_from_name (MonoClass *klass, const char *name)
5186 mono_class_setup_fields_locking (klass);
5188 for (i = 0; i < klass->field.count; ++i) {
5189 if (strcmp (name, mono_field_get_name (&klass->fields [i])) == 0)
5190 return &klass->fields [i];
5192 klass = klass->parent;
5198 * mono_class_get_field_token:
5199 * @field: the field we need the token of
5201 * Get the token of a field. Note that the tokesn is only valid for the image
5202 * the field was loaded from. Don't use this function for fields in dynamic types.
5204 * Returns: the token representing the field in the image it was loaded from.
5207 mono_class_get_field_token (MonoClassField *field)
5209 MonoClass *klass = field->parent;
5212 mono_class_setup_fields_locking (klass);
5214 for (i = 0; i < klass->field.count; ++i) {
5215 if (&klass->fields [i] == field) {
5216 int idx = klass->field.first + i + 1;
5218 if (klass->image->uncompressed_metadata)
5219 idx = mono_metadata_translate_token_index (klass->image, MONO_TABLE_FIELD, idx);
5220 return mono_metadata_make_token (MONO_TABLE_FIELD, idx);
5223 klass = klass->parent;
5226 g_assert_not_reached ();
5231 mono_field_get_index (MonoClassField *field)
5233 int index = field - field->parent->fields;
5235 g_assert (index >= 0 && index < field->parent->field.count);
5241 * mono_class_get_field_default_value:
5243 * Return the default value of the field as a pointer into the metadata blob.
5246 mono_class_get_field_default_value (MonoClassField *field, MonoTypeEnum *def_type)
5249 guint32 constant_cols [MONO_CONSTANT_SIZE];
5251 MonoClass *klass = field->parent;
5253 g_assert (field->type->attrs & FIELD_ATTRIBUTE_HAS_DEFAULT);
5255 if (!klass->ext || !klass->ext->field_def_values) {
5256 mono_loader_lock ();
5257 mono_class_alloc_ext (klass);
5258 if (!klass->ext->field_def_values)
5259 klass->ext->field_def_values = mono_image_alloc0 (klass->image, sizeof (MonoFieldDefaultValue) * klass->field.count);
5260 mono_loader_unlock ();
5263 field_index = mono_field_get_index (field);
5265 if (!klass->ext->field_def_values [field_index].data) {
5266 cindex = mono_metadata_get_constant_index (field->parent->image, mono_class_get_field_token (field), 0);
5268 g_assert (!(field->type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_RVA));
5270 mono_metadata_decode_row (&field->parent->image->tables [MONO_TABLE_CONSTANT], cindex - 1, constant_cols, MONO_CONSTANT_SIZE);
5271 klass->ext->field_def_values [field_index].def_type = constant_cols [MONO_CONSTANT_TYPE];
5272 klass->ext->field_def_values [field_index].data = (gpointer)mono_metadata_blob_heap (field->parent->image, constant_cols [MONO_CONSTANT_VALUE]);
5275 *def_type = klass->ext->field_def_values [field_index].def_type;
5276 return klass->ext->field_def_values [field_index].data;
5280 mono_class_get_event_token (MonoEvent *event)
5282 MonoClass *klass = event->parent;
5287 for (i = 0; i < klass->ext->event.count; ++i) {
5288 if (&klass->ext->events [i] == event)
5289 return mono_metadata_make_token (MONO_TABLE_EVENT, klass->ext->event.first + i + 1);
5292 klass = klass->parent;
5295 g_assert_not_reached ();
5300 mono_class_get_property_from_name (MonoClass *klass, const char *name)
5304 gpointer iter = NULL;
5305 while ((p = mono_class_get_properties (klass, &iter))) {
5306 if (! strcmp (name, p->name))
5309 klass = klass->parent;
5315 mono_class_get_property_token (MonoProperty *prop)
5317 MonoClass *klass = prop->parent;
5321 gpointer iter = NULL;
5322 while ((p = mono_class_get_properties (klass, &iter))) {
5323 if (&klass->ext->properties [i] == prop)
5324 return mono_metadata_make_token (MONO_TABLE_PROPERTY, klass->ext->property.first + i + 1);
5328 klass = klass->parent;
5331 g_assert_not_reached ();
5336 mono_class_name_from_token (MonoImage *image, guint32 type_token)
5338 const char *name, *nspace;
5340 return g_strdup_printf ("DynamicType 0x%08x", type_token);
5342 switch (type_token & 0xff000000){
5343 case MONO_TOKEN_TYPE_DEF: {
5344 guint32 cols [MONO_TYPEDEF_SIZE];
5345 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
5346 guint tidx = mono_metadata_token_index (type_token);
5348 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
5349 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
5350 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
5351 if (strlen (nspace) == 0)
5352 return g_strdup_printf ("%s", name);
5354 return g_strdup_printf ("%s.%s", nspace, name);
5357 case MONO_TOKEN_TYPE_REF: {
5358 guint32 cols [MONO_TYPEREF_SIZE];
5359 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
5361 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
5362 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
5363 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
5364 if (strlen (nspace) == 0)
5365 return g_strdup_printf ("%s", name);
5367 return g_strdup_printf ("%s.%s", nspace, name);
5370 case MONO_TOKEN_TYPE_SPEC:
5371 return g_strdup_printf ("Typespec 0x%08x", type_token);
5373 g_assert_not_reached ();
5380 mono_assembly_name_from_token (MonoImage *image, guint32 type_token)
5383 return g_strdup_printf ("DynamicAssembly %s", image->name);
5385 switch (type_token & 0xff000000){
5386 case MONO_TOKEN_TYPE_DEF:
5387 return mono_stringify_assembly_name (&image->assembly->aname);
5389 case MONO_TOKEN_TYPE_REF: {
5390 MonoAssemblyName aname;
5391 guint32 cols [MONO_TYPEREF_SIZE];
5392 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
5395 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
5397 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
5398 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
5399 case MONO_RESOLTION_SCOPE_MODULE:
5401 return g_strdup ("");
5402 case MONO_RESOLTION_SCOPE_MODULEREF:
5404 return g_strdup ("");
5405 case MONO_RESOLTION_SCOPE_TYPEREF:
5407 return g_strdup ("");
5408 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
5409 mono_assembly_get_assemblyref (image, idx - 1, &aname);
5410 return mono_stringify_assembly_name (&aname);
5412 g_assert_not_reached ();
5416 case MONO_TOKEN_TYPE_SPEC:
5418 return g_strdup ("");
5420 g_assert_not_reached ();
5427 * mono_class_get_full:
5428 * @image: the image where the class resides
5429 * @type_token: the token for the class
5430 * @context: the generic context used to evaluate generic instantiations in
5432 * Returns: the MonoClass that represents @type_token in @image
5435 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
5437 MonoClass *class = NULL;
5439 if (image->dynamic) {
5440 int table = mono_metadata_token_table (type_token);
5442 if (table != MONO_TABLE_TYPEDEF && table != MONO_TABLE_TYPEREF && table != MONO_TABLE_TYPESPEC) {
5443 mono_loader_set_error_bad_image (g_strdup ("Bad type token."));
5446 return mono_lookup_dynamic_token (image, type_token, context);
5449 switch (type_token & 0xff000000){
5450 case MONO_TOKEN_TYPE_DEF:
5451 class = mono_class_create_from_typedef (image, type_token);
5453 case MONO_TOKEN_TYPE_REF:
5454 class = mono_class_from_typeref (image, type_token);
5456 case MONO_TOKEN_TYPE_SPEC:
5457 class = mono_class_create_from_typespec (image, type_token, context);
5460 g_warning ("unknown token type %x", type_token & 0xff000000);
5461 g_assert_not_reached ();
5465 char *name = mono_class_name_from_token (image, type_token);
5466 char *assembly = mono_assembly_name_from_token (image, type_token);
5467 mono_loader_set_error_type_load (name, assembly);
5475 * mono_type_get_full:
5476 * @image: the image where the type resides
5477 * @type_token: the token for the type
5478 * @context: the generic context used to evaluate generic instantiations in
5480 * This functions exists to fullfill the fact that sometimes it's desirable to have access to the
5482 * Returns: the MonoType that represents @type_token in @image
5485 mono_type_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
5487 MonoType *type = NULL;
5488 gboolean inflated = FALSE;
5490 //FIXME: this will not fix the very issue for which mono_type_get_full exists -but how to do it then?
5492 return mono_class_get_type (mono_lookup_dynamic_token (image, type_token, context));
5494 if ((type_token & 0xff000000) != MONO_TOKEN_TYPE_SPEC) {
5495 MonoClass *class = mono_class_get_full (image, type_token, context);
5496 return class ? mono_class_get_type (class) : NULL;
5499 type = mono_type_retrieve_from_typespec (image, type_token, context, &inflated);
5502 char *name = mono_class_name_from_token (image, type_token);
5503 char *assembly = mono_assembly_name_from_token (image, type_token);
5505 mono_metadata_free_type (type);
5506 mono_loader_set_error_type_load (name, assembly);
5510 MonoType *tmp = type;
5511 type = mono_class_get_type (mono_class_from_mono_type (type));
5512 /* FIXME: This is a workaround fo the fact that a typespec token sometimes reference to the generic type definition.
5513 * A MonoClass::byval_arg of a generic type definion has type CLASS.
5514 * Some parts of mono create a GENERICINST to reference a generic type definition and this generates confict with byval_arg.
5516 * The long term solution is to chaise this places and make then set MonoType::type correctly.
5518 if (type->type != tmp->type)
5521 mono_metadata_free_type (tmp);
5528 mono_class_get (MonoImage *image, guint32 type_token)
5530 return mono_class_get_full (image, type_token, NULL);
5534 * mono_image_init_name_cache:
5536 * Initializes the class name cache stored in image->name_cache.
5538 * LOCKING: Acquires the corresponding image lock.
5541 mono_image_init_name_cache (MonoImage *image)
5543 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
5544 guint32 cols [MONO_TYPEDEF_SIZE];
5547 guint32 i, visib, nspace_index;
5548 GHashTable *name_cache2, *nspace_table;
5550 mono_image_lock (image);
5552 if (image->name_cache) {
5553 mono_image_unlock (image);
5557 image->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
5559 if (image->dynamic) {
5560 mono_image_unlock (image);
5564 /* Temporary hash table to avoid lookups in the nspace_table */
5565 name_cache2 = g_hash_table_new (NULL, NULL);
5567 for (i = 1; i <= t->rows; ++i) {
5568 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
5569 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
5571 * Nested types are accessed from the nesting name. We use the fact that nested types use different visibility flags
5572 * than toplevel types, thus avoiding the need to grovel through the NESTED_TYPE table
5574 if (visib >= TYPE_ATTRIBUTE_NESTED_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM)
5576 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
5577 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
5579 nspace_index = cols [MONO_TYPEDEF_NAMESPACE];
5580 nspace_table = g_hash_table_lookup (name_cache2, GUINT_TO_POINTER (nspace_index));
5581 if (!nspace_table) {
5582 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
5583 g_hash_table_insert (image->name_cache, (char*)nspace, nspace_table);
5584 g_hash_table_insert (name_cache2, GUINT_TO_POINTER (nspace_index),
5587 g_hash_table_insert (nspace_table, (char *) name, GUINT_TO_POINTER (i));
5590 /* Load type names from EXPORTEDTYPES table */
5592 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
5593 guint32 cols [MONO_EXP_TYPE_SIZE];
5596 for (i = 0; i < t->rows; ++i) {
5597 mono_metadata_decode_row (t, i, cols, MONO_EXP_TYPE_SIZE);
5598 name = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAME]);
5599 nspace = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAMESPACE]);
5601 nspace_index = cols [MONO_EXP_TYPE_NAMESPACE];
5602 nspace_table = g_hash_table_lookup (name_cache2, GUINT_TO_POINTER (nspace_index));
5603 if (!nspace_table) {
5604 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
5605 g_hash_table_insert (image->name_cache, (char*)nspace, nspace_table);
5606 g_hash_table_insert (name_cache2, GUINT_TO_POINTER (nspace_index),
5609 g_hash_table_insert (nspace_table, (char *) name, GUINT_TO_POINTER (mono_metadata_make_token (MONO_TABLE_EXPORTEDTYPE, i + 1)));
5613 g_hash_table_destroy (name_cache2);
5614 mono_image_unlock (image);
5617 /*FIXME Only dynamic assemblies should allow this operation.*/
5619 mono_image_add_to_name_cache (MonoImage *image, const char *nspace,
5620 const char *name, guint32 index)
5622 GHashTable *nspace_table;
5623 GHashTable *name_cache;
5625 mono_image_lock (image);
5627 if (!image->name_cache)
5628 mono_image_init_name_cache (image);
5630 name_cache = image->name_cache;
5631 if (!(nspace_table = g_hash_table_lookup (name_cache, nspace))) {
5632 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
5633 g_hash_table_insert (name_cache, (char *)nspace, (char *)nspace_table);
5635 g_hash_table_insert (nspace_table, (char *) name, GUINT_TO_POINTER (index));
5637 mono_image_unlock (image);
5646 find_nocase (gpointer key, gpointer value, gpointer user_data)
5648 char *name = (char*)key;
5649 FindUserData *data = (FindUserData*)user_data;
5651 if (!data->value && (g_strcasecmp (name, (char*)data->key) == 0))
5652 data->value = value;
5656 * mono_class_from_name_case:
5657 * @image: The MonoImage where the type is looked up in
5658 * @name_space: the type namespace
5659 * @name: the type short name.
5661 * Obtains a MonoClass with a given namespace and a given name which
5662 * is located in the given MonoImage. The namespace and name
5663 * lookups are case insensitive.
5666 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
5668 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
5669 guint32 cols [MONO_TYPEDEF_SIZE];
5674 if (image->dynamic) {
5676 FindUserData user_data;
5678 mono_image_lock (image);
5680 if (!image->name_cache)
5681 mono_image_init_name_cache (image);
5683 user_data.key = name_space;
5684 user_data.value = NULL;
5685 g_hash_table_foreach (image->name_cache, find_nocase, &user_data);
5687 if (user_data.value) {
5688 GHashTable *nspace_table = (GHashTable*)user_data.value;
5690 user_data.key = name;
5691 user_data.value = NULL;
5693 g_hash_table_foreach (nspace_table, find_nocase, &user_data);
5695 if (user_data.value)
5696 token = GPOINTER_TO_UINT (user_data.value);
5699 mono_image_unlock (image);
5702 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | token);
5708 /* add a cache if needed */
5709 for (i = 1; i <= t->rows; ++i) {
5710 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
5711 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
5713 * Nested types are accessed from the nesting name. We use the fact that nested types use different visibility flags
5714 * than toplevel types, thus avoiding the need to grovel through the NESTED_TYPE table
5716 if (visib >= TYPE_ATTRIBUTE_NESTED_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM)
5718 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
5719 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
5720 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
5721 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
5727 return_nested_in (MonoClass *class, char *nested)
5730 char *s = strchr (nested, '/');
5731 gpointer iter = NULL;
5738 while ((found = mono_class_get_nested_types (class, &iter))) {
5739 if (strcmp (found->name, nested) == 0) {
5741 return return_nested_in (found, s);
5749 search_modules (MonoImage *image, const char *name_space, const char *name)
5751 MonoTableInfo *file_table = &image->tables [MONO_TABLE_FILE];
5752 MonoImage *file_image;
5757 * The EXPORTEDTYPES table only contains public types, so have to search the
5759 * Note: image->modules contains the contents of the MODULEREF table, while
5760 * the real module list is in the FILE table.
5762 for (i = 0; i < file_table->rows; i++) {
5763 guint32 cols [MONO_FILE_SIZE];
5764 mono_metadata_decode_row (file_table, i, cols, MONO_FILE_SIZE);
5765 if (cols [MONO_FILE_FLAGS] == FILE_CONTAINS_NO_METADATA)
5768 file_image = mono_image_load_file_for_image (image, i + 1);
5770 class = mono_class_from_name (file_image, name_space, name);
5780 * mono_class_from_name:
5781 * @image: The MonoImage where the type is looked up in
5782 * @name_space: the type namespace
5783 * @name: the type short name.
5785 * Obtains a MonoClass with a given namespace and a given name which
5786 * is located in the given MonoImage.
5789 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
5791 GHashTable *nspace_table;
5792 MonoImage *loaded_image;
5799 if ((nested = strchr (name, '/'))) {
5800 int pos = nested - name;
5801 int len = strlen (name);
5804 memcpy (buf, name, len + 1);
5806 nested = buf + pos + 1;
5810 if (get_class_from_name) {
5811 gboolean res = get_class_from_name (image, name_space, name, &class);
5814 class = search_modules (image, name_space, name);
5816 return class ? return_nested_in (class, nested) : NULL;
5822 mono_image_lock (image);
5824 if (!image->name_cache)
5825 mono_image_init_name_cache (image);
5827 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
5830 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
5832 mono_image_unlock (image);
5834 if (!token && image->dynamic && image->modules) {
5835 /* Search modules as well */
5836 for (i = 0; i < image->module_count; ++i) {
5837 MonoImage *module = image->modules [i];
5839 class = mono_class_from_name (module, name_space, name);
5846 class = search_modules (image, name_space, name);
5854 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
5855 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
5856 guint32 cols [MONO_EXP_TYPE_SIZE];
5859 idx = mono_metadata_token_index (token);
5861 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
5863 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
5864 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
5865 loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
5868 class = mono_class_from_name (loaded_image, name_space, name);
5870 return return_nested_in (class, nested);
5872 } else if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_ASSEMBLYREF) {
5873 MonoAssembly **references = image->references;
5874 guint32 assembly_idx;
5876 assembly_idx = impl >> MONO_IMPLEMENTATION_BITS;
5878 if (!references [assembly_idx - 1])
5879 mono_assembly_load_reference (image, assembly_idx - 1);
5880 g_assert (references == image->references);
5881 g_assert (references [assembly_idx - 1]);
5882 if (references [assembly_idx - 1] == (gpointer)-1)
5885 /* FIXME: Cycle detection */
5886 return mono_class_from_name (references [assembly_idx - 1]->image, name_space, name);
5888 g_error ("not yet implemented");
5892 token = MONO_TOKEN_TYPE_DEF | token;
5894 class = mono_class_get (image, token);
5896 return return_nested_in (class, nested);
5901 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
5902 gboolean check_interfaces)
5904 g_assert (klassc->idepth > 0);
5905 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
5906 if (MONO_CLASS_IMPLEMENTS_INTERFACE (klass, klassc->interface_id))
5908 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
5911 for (i = 0; i < klass->interface_count; i ++) {
5912 MonoClass *ic = klass->interfaces [i];
5917 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
5922 * MS.NET thinks interfaces are a subclass of Object, so we think it as
5925 if (klassc == mono_defaults.object_class)
5932 mono_class_has_variant_generic_params (MonoClass *klass)
5935 MonoGenericContainer *container;
5937 if (!klass->generic_class)
5940 container = klass->generic_class->container_class->generic_container;
5942 for (i = 0; i < container->type_argc; ++i)
5943 if (mono_generic_container_get_param_info (container, i)->flags & (MONO_GEN_PARAM_VARIANT|MONO_GEN_PARAM_COVARIANT))
5950 * mono_class_is_assignable_from:
5951 * @klass: the class to be assigned to
5952 * @oklass: the source class
5954 * Return: true if an instance of object oklass can be assigned to an
5955 * instance of object @klass
5958 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
5961 mono_class_init (klass);
5963 if (!oklass->inited)
5964 mono_class_init (oklass);
5966 if ((klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR))
5967 return klass == oklass;
5969 if (MONO_CLASS_IS_INTERFACE (klass)) {
5970 if ((oklass->byval_arg.type == MONO_TYPE_VAR) || (oklass->byval_arg.type == MONO_TYPE_MVAR))
5973 /* interface_offsets might not be set for dynamic classes */
5974 if (oklass->reflection_info && !oklass->interface_bitmap)
5976 * oklass might be a generic type parameter but they have
5977 * interface_offsets set.
5979 return mono_reflection_call_is_assignable_to (oklass, klass);
5980 if (!oklass->interface_bitmap)
5981 /* Happens with generic instances of not-yet created dynamic types */
5983 if (MONO_CLASS_IMPLEMENTS_INTERFACE (oklass, klass->interface_id))
5986 if (mono_class_has_variant_generic_params (klass)) {
5987 if (oklass->generic_class) {
5989 gboolean match = FALSE;
5990 MonoClass *container_class1 = klass->generic_class->container_class;
5991 MonoClass *container_class2 = oklass->generic_class->container_class;
5994 * Check whenever the generic definition of oklass implements the
5995 * generic definition of klass. The IMPLEMENTS_INTERFACE stuff is not usable
5996 * here since the relevant tables are not set up.
5998 for (i = 0; i < container_class2->interface_offsets_count; ++i)
5999 if ((container_class2->interfaces_packed [i] == container_class1) || (container_class2->interfaces_packed [i]->generic_class && (container_class2->interfaces_packed [i]->generic_class->container_class == container_class1)))
6003 MonoGenericContainer *container;
6005 container = klass->generic_class->container_class->generic_container;
6008 for (i = 0; i < container->type_argc; ++i) {
6009 MonoClass *param1_class = mono_class_from_mono_type (klass->generic_class->context.class_inst->type_argv [i]);
6010 MonoClass *param2_class = mono_class_from_mono_type (oklass->generic_class->context.class_inst->type_argv [i]);
6012 if (param1_class->valuetype != param2_class->valuetype) {
6017 * The _VARIANT and _COVARIANT constants should read _COVARIANT and
6018 * _CONTRAVARIANT, but they are in a public header so we can't fix it.
6020 if (param1_class != param2_class) {
6021 if ((mono_generic_container_get_param_info (container, i)->flags & MONO_GEN_PARAM_VARIANT) && mono_class_is_assignable_from (param1_class, param2_class))
6023 else if (((mono_generic_container_get_param_info (container, i)->flags & MONO_GEN_PARAM_COVARIANT) && mono_class_is_assignable_from (param2_class, param1_class)))
6037 } else if (klass->rank) {
6038 MonoClass *eclass, *eoclass;
6040 if (oklass->rank != klass->rank)
6043 /* vectors vs. one dimensional arrays */
6044 if (oklass->byval_arg.type != klass->byval_arg.type)
6047 eclass = klass->cast_class;
6048 eoclass = oklass->cast_class;
6051 * a is b does not imply a[] is b[] when a is a valuetype, and
6052 * b is a reference type.
6055 if (eoclass->valuetype) {
6056 if ((eclass == mono_defaults.enum_class) ||
6057 (eclass == mono_defaults.enum_class->parent) ||
6058 (eclass == mono_defaults.object_class))
6062 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
6063 } else if (mono_class_is_nullable (klass)) {
6064 if (mono_class_is_nullable (oklass))
6065 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
6067 return mono_class_is_assignable_from (klass->cast_class, oklass);
6068 } else if (klass == mono_defaults.object_class)
6071 return mono_class_has_parent (oklass, klass);
6075 * mono_class_get_cctor:
6076 * @klass: A MonoClass pointer
6078 * Returns: the static constructor of @klass if it exists, NULL otherwise.
6081 mono_class_get_cctor (MonoClass *klass)
6083 MonoCachedClassInfo cached_info;
6085 if (!klass->has_cctor)
6088 if (mono_class_get_cached_class_info (klass, &cached_info))
6089 return mono_get_method (klass->image, cached_info.cctor_token, klass);
6091 if (klass->generic_class && !klass->methods)
6092 return mono_class_get_inflated_method (klass, mono_class_get_cctor (klass->generic_class->container_class));
6094 return mono_class_get_method_from_name_flags (klass, ".cctor", -1, METHOD_ATTRIBUTE_SPECIAL_NAME);
6098 * mono_class_get_finalizer:
6099 * @klass: The MonoClass pointer
6101 * Returns: the finalizer method of @klass if it exists, NULL otherwise.
6104 mono_class_get_finalizer (MonoClass *klass)
6106 MonoCachedClassInfo cached_info;
6109 mono_class_init (klass);
6110 if (!klass->has_finalize)
6113 if (mono_class_get_cached_class_info (klass, &cached_info))
6114 return mono_get_method (cached_info.finalize_image, cached_info.finalize_token, NULL);
6116 mono_class_setup_vtable (klass);
6117 return klass->vtable [finalize_slot];
6122 * mono_class_needs_cctor_run:
6123 * @klass: the MonoClass pointer
6124 * @caller: a MonoMethod describing the caller
6126 * Determines whenever the class has a static constructor and whenever it
6127 * needs to be called when executing CALLER.
6130 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
6134 method = mono_class_get_cctor (klass);
6136 return (method == caller) ? FALSE : TRUE;
6142 * mono_class_array_element_size:
6145 * Returns: the number of bytes an element of type @klass
6146 * uses when stored into an array.
6149 mono_class_array_element_size (MonoClass *klass)
6151 MonoType *type = &klass->byval_arg;
6154 switch (type->type) {
6157 case MONO_TYPE_BOOLEAN:
6161 case MONO_TYPE_CHAR:
6170 case MONO_TYPE_CLASS:
6171 case MONO_TYPE_STRING:
6172 case MONO_TYPE_OBJECT:
6173 case MONO_TYPE_SZARRAY:
6174 case MONO_TYPE_ARRAY:
6176 case MONO_TYPE_MVAR:
6177 return sizeof (gpointer);
6182 case MONO_TYPE_VALUETYPE:
6183 if (type->data.klass->enumtype) {
6184 type = mono_class_enum_basetype (type->data.klass);
6185 klass = klass->element_class;
6188 return mono_class_instance_size (klass) - sizeof (MonoObject);
6189 case MONO_TYPE_GENERICINST:
6190 type = &type->data.generic_class->container_class->byval_arg;
6193 g_error ("unknown type 0x%02x in mono_class_array_element_size", type->type);
6199 * mono_array_element_size:
6200 * @ac: pointer to a #MonoArrayClass
6202 * Returns: the size of single array element.
6205 mono_array_element_size (MonoClass *ac)
6207 g_assert (ac->rank);
6208 return ac->sizes.element_size;
6212 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
6213 MonoGenericContext *context)
6215 if (image->dynamic) {
6216 MonoClass *tmp_handle_class;
6217 gpointer obj = mono_lookup_dynamic_token_class (image, token, TRUE, &tmp_handle_class, context);
6219 g_assert (tmp_handle_class);
6221 *handle_class = tmp_handle_class;
6223 if (tmp_handle_class == mono_defaults.typehandle_class)
6224 return &((MonoClass*)obj)->byval_arg;
6229 switch (token & 0xff000000) {
6230 case MONO_TOKEN_TYPE_DEF:
6231 case MONO_TOKEN_TYPE_REF:
6232 case MONO_TOKEN_TYPE_SPEC: {
6235 *handle_class = mono_defaults.typehandle_class;
6236 type = mono_type_get_full (image, token, context);
6239 mono_class_init (mono_class_from_mono_type (type));
6240 /* We return a MonoType* as handle */
6243 case MONO_TOKEN_FIELD_DEF: {
6245 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
6247 *handle_class = mono_defaults.fieldhandle_class;
6248 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
6251 mono_class_init (class);
6252 return mono_class_get_field (class, token);
6254 case MONO_TOKEN_METHOD_DEF:
6255 case MONO_TOKEN_METHOD_SPEC: {
6257 meth = mono_get_method_full (image, token, NULL, context);
6259 *handle_class = mono_defaults.methodhandle_class;
6262 case MONO_TOKEN_MEMBER_REF: {
6263 guint32 cols [MONO_MEMBERREF_SIZE];
6265 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
6266 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
6267 mono_metadata_decode_blob_size (sig, &sig);
6268 if (*sig == 0x6) { /* it's a field */
6270 MonoClassField *field;
6271 field = mono_field_from_token (image, token, &klass, context);
6273 *handle_class = mono_defaults.fieldhandle_class;
6277 meth = mono_get_method_full (image, token, NULL, context);
6279 *handle_class = mono_defaults.methodhandle_class;
6284 g_warning ("Unknown token 0x%08x in ldtoken", token);
6291 * This function might need to call runtime functions so it can't be part
6292 * of the metadata library.
6294 static MonoLookupDynamicToken lookup_dynamic = NULL;
6297 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
6299 lookup_dynamic = func;
6303 mono_lookup_dynamic_token (MonoImage *image, guint32 token, MonoGenericContext *context)
6305 MonoClass *handle_class;
6307 return lookup_dynamic (image, token, TRUE, &handle_class, context);
6311 mono_lookup_dynamic_token_class (MonoImage *image, guint32 token, gboolean valid_token, MonoClass **handle_class, MonoGenericContext *context)
6313 return lookup_dynamic (image, token, valid_token, handle_class, context);
6316 static MonoGetCachedClassInfo get_cached_class_info = NULL;
6319 mono_install_get_cached_class_info (MonoGetCachedClassInfo func)
6321 get_cached_class_info = func;
6325 mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
6327 if (!get_cached_class_info)
6330 return get_cached_class_info (klass, res);
6334 mono_install_get_class_from_name (MonoGetClassFromName func)
6336 get_class_from_name = func;
6340 mono_class_get_image (MonoClass *klass)
6342 return klass->image;
6346 * mono_class_get_element_class:
6347 * @klass: the MonoClass to act on
6349 * Returns: the element class of an array or an enumeration.
6352 mono_class_get_element_class (MonoClass *klass)
6354 return klass->element_class;
6358 * mono_class_is_valuetype:
6359 * @klass: the MonoClass to act on
6361 * Returns: true if the MonoClass represents a ValueType.
6364 mono_class_is_valuetype (MonoClass *klass)
6366 return klass->valuetype;
6370 * mono_class_is_enum:
6371 * @klass: the MonoClass to act on
6373 * Returns: true if the MonoClass represents an enumeration.
6376 mono_class_is_enum (MonoClass *klass)
6378 return klass->enumtype;
6382 * mono_class_enum_basetype:
6383 * @klass: the MonoClass to act on
6385 * Returns: the underlying type representation for an enumeration.
6388 mono_class_enum_basetype (MonoClass *klass)
6390 if (klass->element_class == klass)
6394 return &klass->element_class->byval_arg;
6398 * mono_class_get_parent
6399 * @klass: the MonoClass to act on
6401 * Returns: the parent class for this class.
6404 mono_class_get_parent (MonoClass *klass)
6406 return klass->parent;
6410 * mono_class_get_nesting_type;
6411 * @klass: the MonoClass to act on
6413 * Returns: the container type where this type is nested or NULL if this type is not a nested type.
6416 mono_class_get_nesting_type (MonoClass *klass)
6418 return klass->nested_in;
6422 * mono_class_get_rank:
6423 * @klass: the MonoClass to act on
6425 * Returns: the rank for the array (the number of dimensions).
6428 mono_class_get_rank (MonoClass *klass)
6434 * mono_class_get_flags:
6435 * @klass: the MonoClass to act on
6437 * The type flags from the TypeDef table from the metadata.
6438 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
6441 * Returns: the flags from the TypeDef table.
6444 mono_class_get_flags (MonoClass *klass)
6446 return klass->flags;
6450 * mono_class_get_name
6451 * @klass: the MonoClass to act on
6453 * Returns: the name of the class.
6456 mono_class_get_name (MonoClass *klass)
6462 * mono_class_get_namespace:
6463 * @klass: the MonoClass to act on
6465 * Returns: the namespace of the class.
6468 mono_class_get_namespace (MonoClass *klass)
6470 return klass->name_space;
6474 * mono_class_get_type:
6475 * @klass: the MonoClass to act on
6477 * This method returns the internal Type representation for the class.
6479 * Returns: the MonoType from the class.
6482 mono_class_get_type (MonoClass *klass)
6484 return &klass->byval_arg;
6488 * mono_class_get_type_token
6489 * @klass: the MonoClass to act on
6491 * This method returns type token for the class.
6493 * Returns: the type token for the class.
6496 mono_class_get_type_token (MonoClass *klass)
6498 return klass->type_token;
6502 * mono_class_get_byref_type:
6503 * @klass: the MonoClass to act on
6508 mono_class_get_byref_type (MonoClass *klass)
6510 return &klass->this_arg;
6514 * mono_class_num_fields:
6515 * @klass: the MonoClass to act on
6517 * Returns: the number of static and instance fields in the class.
6520 mono_class_num_fields (MonoClass *klass)
6522 return klass->field.count;
6526 * mono_class_num_methods:
6527 * @klass: the MonoClass to act on
6529 * Returns: the number of methods in the class.
6532 mono_class_num_methods (MonoClass *klass)
6534 return klass->method.count;
6538 * mono_class_num_properties
6539 * @klass: the MonoClass to act on
6541 * Returns: the number of properties in the class.
6544 mono_class_num_properties (MonoClass *klass)
6546 mono_class_setup_properties (klass);
6548 return klass->ext->property.count;
6552 * mono_class_num_events:
6553 * @klass: the MonoClass to act on
6555 * Returns: the number of events in the class.
6558 mono_class_num_events (MonoClass *klass)
6560 mono_class_setup_events (klass);
6562 return klass->ext->event.count;
6566 * mono_class_get_fields:
6567 * @klass: the MonoClass to act on
6569 * This routine is an iterator routine for retrieving the fields in a class.
6571 * You must pass a gpointer that points to zero and is treated as an opaque handle to
6572 * iterate over all of the elements. When no more values are
6573 * available, the return value is NULL.
6575 * Returns: a @MonoClassField* on each iteration, or NULL when no more fields are available.
6578 mono_class_get_fields (MonoClass* klass, gpointer *iter)
6580 MonoClassField* field;
6584 mono_class_setup_fields_locking (klass);
6585 if (klass->exception_type)
6587 /* start from the first */
6588 if (klass->field.count) {
6589 return *iter = &klass->fields [0];
6597 if (field < &klass->fields [klass->field.count]) {
6598 return *iter = field;
6604 * mono_class_get_methods
6605 * @klass: the MonoClass to act on
6607 * This routine is an iterator routine for retrieving the fields in a class.
6609 * You must pass a gpointer that points to zero and is treated as an opaque handle to
6610 * iterate over all of the elements. When no more values are
6611 * available, the return value is NULL.
6613 * Returns: a MonoMethod on each iteration or NULL when no more methods are available.
6616 mono_class_get_methods (MonoClass* klass, gpointer *iter)
6618 MonoMethod** method;
6622 mono_class_init (klass);
6624 mono_class_setup_methods (klass);
6625 /* start from the first */
6626 if (klass->method.count) {
6627 *iter = &klass->methods [0];
6628 return klass->methods [0];
6636 if (method < &klass->methods [klass->method.count]) {
6644 * mono_class_get_virtual_methods:
6646 * Iterate over the virtual methods of KLASS.
6648 * LOCKING: Assumes the loader lock is held (because of the klass->methods check).
6651 mono_class_get_virtual_methods (MonoClass* klass, gpointer *iter)
6653 MonoMethod** method;
6656 if (klass->methods || !MONO_CLASS_HAS_STATIC_METADATA (klass) || mono_debug_using_mono_debugger ()) {
6658 mono_class_setup_methods (klass);
6659 /* start from the first */
6660 method = &klass->methods [0];
6665 while (method < &klass->methods [klass->method.count]) {
6666 if (((*method)->flags & METHOD_ATTRIBUTE_VIRTUAL))
6670 if (method < &klass->methods [klass->method.count]) {
6677 /* Search directly in metadata to avoid calling setup_methods () */
6678 MonoMethod *res = NULL;
6684 start_index = GPOINTER_TO_UINT (*iter);
6687 for (i = start_index; i < klass->method.count; ++i) {
6688 guint32 cols [MONO_METHOD_SIZE];
6690 /* class->method.first points into the methodptr table */
6691 mono_metadata_decode_table_row (klass->image, MONO_TABLE_METHOD, klass->method.first + i, cols, MONO_METHOD_SIZE);
6693 if (cols [MONO_METHOD_FLAGS] & METHOD_ATTRIBUTE_VIRTUAL)
6697 if (i < klass->method.count) {
6698 res = mono_get_method (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass);
6699 /* Add 1 here so the if (*iter) check fails */
6700 *iter = GUINT_TO_POINTER (i + 1);
6709 * mono_class_get_properties:
6710 * @klass: the MonoClass to act on
6712 * This routine is an iterator routine for retrieving the properties in a class.
6714 * You must pass a gpointer that points to zero and is treated as an opaque handle to
6715 * iterate over all of the elements. When no more values are
6716 * available, the return value is NULL.
6718 * Returns: a @MonoProperty* on each invocation, or NULL when no more are available.
6721 mono_class_get_properties (MonoClass* klass, gpointer *iter)
6723 MonoProperty* property;
6727 mono_class_init (klass);
6729 mono_class_setup_properties (klass);
6730 /* start from the first */
6731 if (klass->ext->property.count) {
6732 return *iter = &klass->ext->properties [0];
6740 if (property < &klass->ext->properties [klass->ext->property.count]) {
6741 return *iter = property;
6747 * mono_class_get_events:
6748 * @klass: the MonoClass to act on
6750 * This routine is an iterator routine for retrieving the properties in a class.
6752 * You must pass a gpointer that points to zero and is treated as an opaque handle to
6753 * iterate over all of the elements. When no more values are
6754 * available, the return value is NULL.
6756 * Returns: a @MonoEvent* on each invocation, or NULL when no more are available.
6759 mono_class_get_events (MonoClass* klass, gpointer *iter)
6765 mono_class_init (klass);
6767 mono_class_setup_events (klass);
6768 /* start from the first */
6769 if (klass->ext->event.count) {
6770 return *iter = &klass->ext->events [0];
6778 if (event < &klass->ext->events [klass->ext->event.count]) {
6779 return *iter = event;
6785 * mono_class_get_interfaces
6786 * @klass: the MonoClass to act on
6788 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
6790 * You must pass a gpointer that points to zero and is treated as an opaque handle to
6791 * iterate over all of the elements. When no more values are
6792 * available, the return value is NULL.
6794 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
6797 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
6804 mono_class_init (klass);
6805 if (!klass->interfaces_inited)
6806 mono_class_setup_interfaces (klass);
6807 /* start from the first */
6808 if (klass->interface_count) {
6809 *iter = &klass->interfaces [0];
6810 return klass->interfaces [0];
6818 if (iface < &klass->interfaces [klass->interface_count]) {
6826 * mono_class_get_nested_types
6827 * @klass: the MonoClass to act on
6829 * This routine is an iterator routine for retrieving the nested types of a class.
6830 * This works only if @klass is non-generic, or a generic type definition.
6832 * You must pass a gpointer that points to zero and is treated as an opaque handle to
6833 * iterate over all of the elements. When no more values are
6834 * available, the return value is NULL.
6836 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
6839 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
6847 mono_class_init (klass);
6848 if (!klass->nested_classes_inited) {
6849 if (!klass->type_token)
6850 klass->nested_classes_inited = TRUE;
6851 mono_loader_lock ();
6852 if (!klass->nested_classes_inited) {
6853 i = mono_metadata_nesting_typedef (klass->image, klass->type_token, 1);
6856 guint32 cols [MONO_NESTED_CLASS_SIZE];
6857 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
6858 nclass = mono_class_create_from_typedef (klass->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
6859 mono_class_alloc_ext (klass);
6860 klass->ext->nested_classes = g_list_prepend_image (klass->image, klass->ext->nested_classes, nclass);
6862 i = mono_metadata_nesting_typedef (klass->image, klass->type_token, i + 1);
6865 mono_memory_barrier ();
6866 klass->nested_classes_inited = TRUE;
6867 mono_loader_unlock ();
6871 /* start from the first */
6872 if (klass->ext && klass->ext->nested_classes) {
6873 *iter = klass->ext->nested_classes;
6874 return klass->ext->nested_classes->data;
6876 /* no nested types */
6890 * mono_field_get_name:
6891 * @field: the MonoClassField to act on
6893 * Returns: the name of the field.
6896 mono_field_get_name (MonoClassField *field)
6902 * mono_field_get_type:
6903 * @field: the MonoClassField to act on
6905 * Returns: MonoType of the field.
6908 mono_field_get_type (MonoClassField *field)
6914 * mono_field_get_type:
6915 * @field: the MonoClassField to act on
6917 * Returns: MonoClass where the field was defined.
6920 mono_field_get_parent (MonoClassField *field)
6922 return field->parent;
6926 * mono_field_get_flags;
6927 * @field: the MonoClassField to act on
6929 * The metadata flags for a field are encoded using the
6930 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
6932 * Returns: the flags for the field.
6935 mono_field_get_flags (MonoClassField *field)
6937 return field->type->attrs;
6941 * mono_field_get_offset;
6942 * @field: the MonoClassField to act on
6944 * Returns: the field offset.
6947 mono_field_get_offset (MonoClassField *field)
6949 return field->offset;
6953 mono_field_get_rva (MonoClassField *field)
6957 MonoClass *klass = field->parent;
6959 g_assert (field->type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_RVA);
6961 if (!klass->ext || !klass->ext->field_def_values) {
6962 mono_loader_lock ();
6963 mono_class_alloc_ext (klass);
6964 if (!klass->ext->field_def_values)
6965 klass->ext->field_def_values = mono_image_alloc0 (klass->image, sizeof (MonoFieldDefaultValue) * klass->field.count);
6966 mono_loader_unlock ();
6969 field_index = mono_field_get_index (field);
6971 if (!klass->ext->field_def_values [field_index].data && !klass->image->dynamic) {
6972 mono_metadata_field_info (field->parent->image, klass->field.first + field_index, NULL, &rva, NULL);
6974 g_warning ("field %s in %s should have RVA data, but hasn't", mono_field_get_name (field), field->parent->name);
6975 klass->ext->field_def_values [field_index].data = mono_image_rva_map (field->parent->image, rva);
6978 return klass->ext->field_def_values [field_index].data;
6982 * mono_field_get_data;
6983 * @field: the MonoClassField to act on
6985 * Returns: pointer to the metadata constant value or to the field
6986 * data if it has an RVA flag.
6989 mono_field_get_data (MonoClassField *field)
6991 if (field->type->attrs & FIELD_ATTRIBUTE_HAS_DEFAULT) {
6992 MonoTypeEnum def_type;
6994 return mono_class_get_field_default_value (field, &def_type);
6995 } else if (field->type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
6996 return mono_field_get_rva (field);
7003 * mono_property_get_name:
7004 * @prop: the MonoProperty to act on
7006 * Returns: the name of the property
7009 mono_property_get_name (MonoProperty *prop)
7015 * mono_property_get_set_method
7016 * @prop: the MonoProperty to act on.
7018 * Returns: the setter method of the property (A MonoMethod)
7021 mono_property_get_set_method (MonoProperty *prop)
7027 * mono_property_get_get_method
7028 * @prop: the MonoProperty to act on.
7030 * Returns: the setter method of the property (A MonoMethod)
7033 mono_property_get_get_method (MonoProperty *prop)
7039 * mono_property_get_parent:
7040 * @prop: the MonoProperty to act on.
7042 * Returns: the MonoClass where the property was defined.
7045 mono_property_get_parent (MonoProperty *prop)
7047 return prop->parent;
7051 * mono_property_get_flags:
7052 * @prop: the MonoProperty to act on.
7054 * The metadata flags for a property are encoded using the
7055 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
7057 * Returns: the flags for the property.
7060 mono_property_get_flags (MonoProperty *prop)
7066 * mono_event_get_name:
7067 * @event: the MonoEvent to act on
7069 * Returns: the name of the event.
7072 mono_event_get_name (MonoEvent *event)
7078 * mono_event_get_add_method:
7079 * @event: The MonoEvent to act on.
7081 * Returns: the @add' method for the event (a MonoMethod).
7084 mono_event_get_add_method (MonoEvent *event)
7090 * mono_event_get_remove_method:
7091 * @event: The MonoEvent to act on.
7093 * Returns: the @remove method for the event (a MonoMethod).
7096 mono_event_get_remove_method (MonoEvent *event)
7098 return event->remove;
7102 * mono_event_get_raise_method:
7103 * @event: The MonoEvent to act on.
7105 * Returns: the @raise method for the event (a MonoMethod).
7108 mono_event_get_raise_method (MonoEvent *event)
7110 return event->raise;
7114 * mono_event_get_parent:
7115 * @event: the MonoEvent to act on.
7117 * Returns: the MonoClass where the event is defined.
7120 mono_event_get_parent (MonoEvent *event)
7122 return event->parent;
7126 * mono_event_get_flags
7127 * @event: the MonoEvent to act on.
7129 * The metadata flags for an event are encoded using the
7130 * EVENT_* constants. See the tabledefs.h file for details.
7132 * Returns: the flags for the event.
7135 mono_event_get_flags (MonoEvent *event)
7137 return event->attrs;
7141 * mono_class_get_method_from_name:
7142 * @klass: where to look for the method
7143 * @name_space: name of the method
7144 * @param_count: number of parameters. -1 for any number.
7146 * Obtains a MonoMethod with a given name and number of parameters.
7147 * It only works if there are no multiple signatures for any given method name.
7150 mono_class_get_method_from_name (MonoClass *klass, const char *name, int param_count)
7152 return mono_class_get_method_from_name_flags (klass, name, param_count, 0);
7156 find_method_in_metadata (MonoClass *klass, const char *name, int param_count, int flags)
7158 MonoMethod *res = NULL;
7161 /* Search directly in the metadata to avoid calling setup_methods () */
7162 for (i = 0; i < klass->method.count; ++i) {
7163 guint32 cols [MONO_METHOD_SIZE];
7166 /* class->method.first points into the methodptr table */
7167 mono_metadata_decode_table_row (klass->image, MONO_TABLE_METHOD, klass->method.first + i, cols, MONO_METHOD_SIZE);
7169 if (!strcmp (mono_metadata_string_heap (klass->image, cols [MONO_METHOD_NAME]), name)) {
7170 method = mono_get_method (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass);
7171 if ((param_count == -1) || mono_method_signature (method)->param_count == param_count) {
7182 * mono_class_get_method_from_name_flags:
7183 * @klass: where to look for the method
7184 * @name_space: name of the method
7185 * @param_count: number of parameters. -1 for any number.
7186 * @flags: flags which must be set in the method
7188 * Obtains a MonoMethod with a given name and number of parameters.
7189 * It only works if there are no multiple signatures for any given method name.
7192 mono_class_get_method_from_name_flags (MonoClass *klass, const char *name, int param_count, int flags)
7194 MonoMethod *res = NULL;
7197 mono_class_init (klass);
7199 if (klass->generic_class && !klass->methods) {
7200 res = mono_class_get_method_from_name_flags (klass->generic_class->container_class, name, param_count, flags);
7202 res = mono_class_inflate_generic_method_full (res, klass, mono_class_get_context (klass));
7206 if (klass->methods || !MONO_CLASS_HAS_STATIC_METADATA (klass)) {
7207 mono_class_setup_methods (klass);
7208 for (i = 0; i < klass->method.count; ++i) {
7209 MonoMethod *method = klass->methods [i];
7211 if (method->name[0] == name [0] &&
7212 !strcmp (name, method->name) &&
7213 (param_count == -1 || mono_method_signature (method)->param_count == param_count) &&
7214 ((method->flags & flags) == flags)) {
7221 res = find_method_in_metadata (klass, name, param_count, flags);
7228 * mono_class_set_failure:
7229 * @klass: class in which the failure was detected
7230 * @ex_type: the kind of exception/error to be thrown (later)
7231 * @ex_data: exception data (specific to each type of exception/error)
7233 * Keep a detected failure informations in the class for later processing.
7234 * Note that only the first failure is kept.
7236 * LOCKING: Acquires the loader lock.
7239 mono_class_set_failure (MonoClass *klass, guint32 ex_type, void *ex_data)
7241 if (klass->exception_type)
7244 mono_loader_lock ();
7245 klass->exception_type = ex_type;
7247 mono_image_property_insert (klass->image, klass, MONO_CLASS_PROP_EXCEPTION_DATA, ex_data);
7248 mono_loader_unlock ();
7254 * mono_class_get_exception_data:
7256 * Return the exception_data property of KLASS.
7258 * LOCKING: Acquires the loader lock.
7261 mono_class_get_exception_data (MonoClass *klass)
7263 return mono_image_property_lookup (klass->image, klass, MONO_CLASS_PROP_EXCEPTION_DATA);
7267 * mono_classes_init:
7269 * Initialize the resources used by this module.
7272 mono_classes_init (void)
7274 mono_counters_register ("Inflated methods size",
7275 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &inflated_methods_size);
7276 mono_counters_register ("Inflated classes",
7277 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &inflated_classes);
7278 mono_counters_register ("Inflated classes size",
7279 MONO_COUNTER_GENERICS | MONO_COUNTER_INT, &inflated_classes_size);
7280 mono_counters_register ("MonoClass size",
7281 MONO_COUNTER_METADATA | MONO_COUNTER_INT, &classes_size);
7282 mono_counters_register ("MonoClassExt size",
7283 MONO_COUNTER_METADATA | MONO_COUNTER_INT, &class_ext_size);
7287 * mono_classes_cleanup:
7289 * Free the resources used by this module.
7292 mono_classes_cleanup (void)
7294 if (global_interface_bitset)
7295 mono_bitset_free (global_interface_bitset);
7299 * mono_class_get_exception_for_failure:
7300 * @klass: class in which the failure was detected
7302 * Return a constructed MonoException than the caller can then throw
7303 * using mono_raise_exception - or NULL if no failure is present (or
7304 * doesn't result in an exception).
7307 mono_class_get_exception_for_failure (MonoClass *klass)
7309 gpointer exception_data = mono_class_get_exception_data (klass);
7311 switch (klass->exception_type) {
7312 case MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND: {
7313 MonoDomain *domain = mono_domain_get ();
7314 MonoSecurityManager* secman = mono_security_manager_get_methods ();
7315 MonoMethod *method = exception_data;
7316 guint32 error = (method) ? MONO_METADATA_INHERITANCEDEMAND_METHOD : MONO_METADATA_INHERITANCEDEMAND_CLASS;
7317 MonoObject *exc = NULL;
7321 args [1] = mono_assembly_get_object (domain, mono_image_get_assembly (klass->image));
7322 args [2] = mono_type_get_object (domain, &klass->byval_arg);
7323 args [3] = (method) ? mono_method_get_object (domain, method, NULL) : NULL;
7325 mono_runtime_invoke (secman->inheritsecurityexception, NULL, args, &exc);
7326 return (MonoException*) exc;
7328 case MONO_EXCEPTION_TYPE_LOAD: {
7331 char *str = mono_type_get_full_name (klass);
7332 char *astr = klass->image->assembly? mono_stringify_assembly_name (&klass->image->assembly->aname): NULL;
7333 name = mono_string_new (mono_domain_get (), str);
7335 ex = mono_get_exception_type_load (name, astr);
7339 case MONO_EXCEPTION_MISSING_METHOD: {
7340 char *class_name = exception_data;
7341 char *assembly_name = class_name + strlen (class_name) + 1;
7343 return mono_get_exception_missing_method (class_name, assembly_name);
7345 case MONO_EXCEPTION_MISSING_FIELD: {
7346 char *class_name = exception_data;
7347 char *member_name = class_name + strlen (class_name) + 1;
7349 return mono_get_exception_missing_field (class_name, member_name);
7351 case MONO_EXCEPTION_FILE_NOT_FOUND: {
7352 char *msg_format = exception_data;
7353 char *assembly_name = msg_format + strlen (msg_format) + 1;
7354 char *msg = g_strdup_printf (msg_format, assembly_name);
7357 ex = mono_get_exception_file_not_found2 (msg, mono_string_new (mono_domain_get (), assembly_name));
7363 case MONO_EXCEPTION_BAD_IMAGE: {
7364 return mono_get_exception_bad_image_format (exception_data);
7367 MonoLoaderError *error;
7370 error = mono_loader_get_last_error ();
7372 ex = mono_loader_error_prepare_exception (error);
7376 /* TODO - handle other class related failures */
7383 is_nesting_type (MonoClass *outer_klass, MonoClass *inner_klass)
7385 outer_klass = mono_class_get_generic_type_definition (outer_klass);
7386 inner_klass = mono_class_get_generic_type_definition (inner_klass);
7388 if (outer_klass == inner_klass)
7390 inner_klass = inner_klass->nested_in;
7391 } while (inner_klass);
7396 mono_class_get_generic_type_definition (MonoClass *klass)
7398 return klass->generic_class ? klass->generic_class->container_class : klass;
7402 * Check if @klass is a subtype of @parent ignoring generic instantiations.
7404 * Generic instantiations are ignored for all super types of @klass.
7406 * Visibility checks ignoring generic instantiations.
7409 mono_class_has_parent_and_ignore_generics (MonoClass *klass, MonoClass *parent)
7412 klass = mono_class_get_generic_type_definition (klass);
7413 parent = mono_class_get_generic_type_definition (parent);
7415 for (i = 0; i < klass->idepth; ++i) {
7416 if (parent == mono_class_get_generic_type_definition (klass->supertypes [i]))
7422 * Subtype can only access parent members with family protection if the site object
7423 * is subclass of Subtype. For example:
7424 * class A { protected int x; }
7426 * void valid_access () {
7430 * void invalid_access () {
7437 is_valid_family_access (MonoClass *access_klass, MonoClass *member_klass, MonoClass *context_klass)
7439 if (!mono_class_has_parent_and_ignore_generics (access_klass, member_klass))
7442 if (context_klass == NULL)
7444 /*if access_klass is not member_klass context_klass must be type compat*/
7445 if (access_klass != member_klass && !mono_class_has_parent_and_ignore_generics (context_klass, access_klass))
7451 can_access_internals (MonoAssembly *accessing, MonoAssembly* accessed)
7454 if (accessing == accessed)
7456 if (!accessed || !accessing)
7458 mono_assembly_load_friends (accessed);
7459 for (tmp = accessed->friend_assembly_names; tmp; tmp = tmp->next) {
7460 MonoAssemblyName *friend = tmp->data;
7461 /* Be conservative with checks */
7464 if (strcmp (accessing->aname.name, friend->name))
7466 if (friend->public_key_token [0]) {
7467 if (!accessing->aname.public_key_token [0])
7469 if (!mono_public_tokens_are_equal (friend->public_key_token, accessing->aname.public_key_token))
7478 * If klass is a generic type or if it is derived from a generic type, return the
7479 * MonoClass of the generic definition
7480 * Returns NULL if not found
7483 get_generic_definition_class (MonoClass *klass)
7486 if (klass->generic_class && klass->generic_class->container_class)
7487 return klass->generic_class->container_class;
7488 klass = klass->parent;
7494 can_access_instantiation (MonoClass *access_klass, MonoGenericInst *ginst)
7497 for (i = 0; i < ginst->type_argc; ++i) {
7498 MonoType *type = ginst->type_argv[i];
7499 switch (type->type) {
7500 case MONO_TYPE_SZARRAY:
7501 if (!can_access_type (access_klass, type->data.klass))
7504 case MONO_TYPE_ARRAY:
7505 if (!can_access_type (access_klass, type->data.array->eklass))
7509 if (!can_access_type (access_klass, mono_class_from_mono_type (type->data.type)))
7512 case MONO_TYPE_CLASS:
7513 case MONO_TYPE_VALUETYPE:
7514 case MONO_TYPE_GENERICINST:
7515 if (!can_access_type (access_klass, mono_class_from_mono_type (type)))
7523 can_access_type (MonoClass *access_klass, MonoClass *member_klass)
7527 if (access_klass->element_class && !access_klass->enumtype)
7528 access_klass = access_klass->element_class;
7530 if (member_klass->element_class && !member_klass->enumtype)
7531 member_klass = member_klass->element_class;
7533 access_level = member_klass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK;
7535 if (member_klass->byval_arg.type == MONO_TYPE_VAR || member_klass->byval_arg.type == MONO_TYPE_MVAR)
7538 if (member_klass->generic_class && !can_access_instantiation (access_klass, member_klass->generic_class->context.class_inst))
7541 if (is_nesting_type (access_klass, member_klass) || (access_klass->nested_in && is_nesting_type (access_klass->nested_in, member_klass)))
7544 if (member_klass->nested_in && !can_access_type (access_klass, member_klass->nested_in))
7547 switch (access_level) {
7548 case TYPE_ATTRIBUTE_NOT_PUBLIC:
7549 return can_access_internals (access_klass->image->assembly, member_klass->image->assembly);
7551 case TYPE_ATTRIBUTE_PUBLIC:
7554 case TYPE_ATTRIBUTE_NESTED_PUBLIC:
7557 case TYPE_ATTRIBUTE_NESTED_PRIVATE:
7558 return is_nesting_type (member_klass, access_klass);
7560 case TYPE_ATTRIBUTE_NESTED_FAMILY:
7561 return mono_class_has_parent_and_ignore_generics (access_klass, member_klass->nested_in);
7563 case TYPE_ATTRIBUTE_NESTED_ASSEMBLY:
7564 return can_access_internals (access_klass->image->assembly, member_klass->image->assembly);
7566 case TYPE_ATTRIBUTE_NESTED_FAM_AND_ASSEM:
7567 return can_access_internals (access_klass->image->assembly, member_klass->nested_in->image->assembly) &&
7568 mono_class_has_parent_and_ignore_generics (access_klass, member_klass->nested_in);
7570 case TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM:
7571 return can_access_internals (access_klass->image->assembly, member_klass->nested_in->image->assembly) ||
7572 mono_class_has_parent_and_ignore_generics (access_klass, member_klass->nested_in);
7577 /* FIXME: check visibility of type, too */
7579 can_access_member (MonoClass *access_klass, MonoClass *member_klass, MonoClass* context_klass, int access_level)
7581 MonoClass *member_generic_def;
7582 if (((access_klass->generic_class && access_klass->generic_class->container_class) ||
7583 access_klass->generic_container) &&
7584 (member_generic_def = get_generic_definition_class (member_klass))) {
7585 MonoClass *access_container;
7587 if (access_klass->generic_container)
7588 access_container = access_klass;
7590 access_container = access_klass->generic_class->container_class;
7592 if (can_access_member (access_container, member_generic_def, context_klass, access_level))
7596 /* Partition I 8.5.3.2 */
7597 /* the access level values are the same for fields and methods */
7598 switch (access_level) {
7599 case FIELD_ATTRIBUTE_COMPILER_CONTROLLED:
7600 /* same compilation unit */
7601 return access_klass->image == member_klass->image;
7602 case FIELD_ATTRIBUTE_PRIVATE:
7603 return access_klass == member_klass;
7604 case FIELD_ATTRIBUTE_FAM_AND_ASSEM:
7605 if (is_valid_family_access (access_klass, member_klass, context_klass) &&
7606 can_access_internals (access_klass->image->assembly, member_klass->image->assembly))
7609 case FIELD_ATTRIBUTE_ASSEMBLY:
7610 return can_access_internals (access_klass->image->assembly, member_klass->image->assembly);
7611 case FIELD_ATTRIBUTE_FAMILY:
7612 if (is_valid_family_access (access_klass, member_klass, context_klass))
7615 case FIELD_ATTRIBUTE_FAM_OR_ASSEM:
7616 if (is_valid_family_access (access_klass, member_klass, context_klass))
7618 return can_access_internals (access_klass->image->assembly, member_klass->image->assembly);
7619 case FIELD_ATTRIBUTE_PUBLIC:
7626 mono_method_can_access_field (MonoMethod *method, MonoClassField *field)
7628 /* FIXME: check all overlapping fields */
7629 int can = can_access_member (method->klass, field->parent, NULL, field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK);
7631 MonoClass *nested = method->klass->nested_in;
7633 can = can_access_member (nested, field->parent, NULL, field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK);
7636 nested = nested->nested_in;
7643 mono_method_can_access_method (MonoMethod *method, MonoMethod *called)
7645 int can = can_access_member (method->klass, called->klass, NULL, called->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK);
7647 MonoClass *nested = method->klass->nested_in;
7649 can = can_access_member (nested, called->klass, NULL, called->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK);
7652 nested = nested->nested_in;
7657 * with generics calls to explicit interface implementations can be expressed
7658 * directly: the method is private, but we must allow it. This may be opening
7659 * a hole or the generics code should handle this differently.
7660 * Maybe just ensure the interface type is public.
7662 if ((called->flags & METHOD_ATTRIBUTE_VIRTUAL) && (called->flags & METHOD_ATTRIBUTE_FINAL))
7668 * mono_method_can_access_method_full:
7669 * @method: The caller method
7670 * @called: The called method
7671 * @context_klass: The static type on stack of the owner @called object used
7673 * This function must be used with instance calls, as they have more strict family accessibility.
7674 * It can be used with static methods, but context_klass should be NULL.
7676 * Returns: TRUE if caller have proper visibility and acessibility to @called
7679 mono_method_can_access_method_full (MonoMethod *method, MonoMethod *called, MonoClass *context_klass)
7681 MonoClass *access_class = method->klass;
7682 MonoClass *member_class = called->klass;
7683 int can = can_access_member (access_class, member_class, context_klass, called->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK);
7685 MonoClass *nested = access_class->nested_in;
7687 can = can_access_member (nested, member_class, context_klass, called->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK);
7690 nested = nested->nested_in;
7697 if (!can_access_type (access_class, member_class) && (!access_class->nested_in || !can_access_type (access_class->nested_in, member_class)))
7700 if (called->is_inflated) {
7701 MonoMethodInflated * infl = (MonoMethodInflated*)called;
7702 if (infl->context.method_inst && !can_access_instantiation (access_class, infl->context.method_inst))
7711 * mono_method_can_access_method_with_context:
7712 * @method: The caller method
7713 * @field: The accessed field
7714 * @context_klass: The static type on stack of the owner @field object used
7716 * This function must be used with instance fields, as they have more strict family accessibility.
7717 * It can be used with static fields, but context_klass should be NULL.
7719 * Returns: TRUE if caller have proper visibility and acessibility to @field
7722 mono_method_can_access_field_full (MonoMethod *method, MonoClassField *field, MonoClass *context_klass)
7724 MonoClass *access_class = method->klass;
7725 MonoClass *member_class = field->parent;
7726 /* FIXME: check all overlapping fields */
7727 int can = can_access_member (access_class, member_class, context_klass, field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK);
7729 MonoClass *nested = access_class->nested_in;
7731 can = can_access_member (nested, member_class, context_klass, field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK);
7734 nested = nested->nested_in;
7741 if (!can_access_type (access_class, member_class) && (!access_class->nested_in || !can_access_type (access_class->nested_in, member_class)))
7747 * mono_type_is_valid_enum_basetype:
7748 * @type: The MonoType to check
7750 * Returns: TRUE if the type can be used as the basetype of an enum
7752 gboolean mono_type_is_valid_enum_basetype (MonoType * type) {
7753 switch (type->type) {
7756 case MONO_TYPE_BOOLEAN:
7759 case MONO_TYPE_CHAR:
7772 * mono_class_is_valid_enum:
7773 * @klass: An enum class to be validated
7775 * This method verify the required properties an enum should have.
7777 * Returns: TRUE if the informed enum class is valid
7779 * FIXME: TypeBuilder enums are allowed to implement interfaces, but since they cannot have methods, only empty interfaces are possible
7780 * FIXME: enum types are not allowed to have a cctor, but mono_reflection_create_runtime_class sets has_cctor to 1 for all types
7781 * FIXME: TypeBuilder enums can have any kind of static fields, but the spec is very explicit about that (P II 14.3)
7783 gboolean mono_class_is_valid_enum (MonoClass *klass) {
7784 MonoClassField * field;
7785 gpointer iter = NULL;
7786 gboolean found_base_field = FALSE;
7788 g_assert (klass->enumtype);
7789 /* we cannot test against mono_defaults.enum_class, or mcs won't be able to compile the System namespace*/
7790 if (!klass->parent || strcmp (klass->parent->name, "Enum") || strcmp (klass->parent->name_space, "System") ) {
7794 if ((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) != TYPE_ATTRIBUTE_AUTO_LAYOUT)
7797 while ((field = mono_class_get_fields (klass, &iter))) {
7798 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
7799 if (found_base_field)
7801 found_base_field = TRUE;
7802 if (!mono_type_is_valid_enum_basetype (field->type))
7807 if (!found_base_field)
7810 if (klass->method.count > 0)
7817 mono_generic_class_is_generic_type_definition (MonoGenericClass *gklass)
7819 return gklass->context.class_inst == gklass->container_class->generic_container->context.class_inst;
7822 static gboolean gshared_supported;
7825 mono_set_generic_sharing_supported (gboolean supported)
7827 gshared_supported = supported;
7831 * mono_class_generic_sharing_enabled:
7834 * Returns whether generic sharing is enabled for class.
7836 * This is a stop-gap measure to slowly introduce generic sharing
7837 * until we have all the issues sorted out, at which time this
7838 * function will disappear and generic sharing will always be enabled.
7841 mono_class_generic_sharing_enabled (MonoClass *class)
7843 static int generic_sharing = MONO_GENERIC_SHARING_NONE;
7844 static gboolean inited = FALSE;
7849 if (gshared_supported)
7850 generic_sharing = MONO_GENERIC_SHARING_ALL;
7852 generic_sharing = MONO_GENERIC_SHARING_NONE;
7854 if ((option = g_getenv ("MONO_GENERIC_SHARING"))) {
7855 if (strcmp (option, "corlib") == 0)
7856 generic_sharing = MONO_GENERIC_SHARING_CORLIB;
7857 else if (strcmp (option, "collections") == 0)
7858 generic_sharing = MONO_GENERIC_SHARING_COLLECTIONS;
7859 else if (strcmp (option, "all") == 0)
7860 generic_sharing = MONO_GENERIC_SHARING_ALL;
7861 else if (strcmp (option, "none") == 0)
7862 generic_sharing = MONO_GENERIC_SHARING_NONE;
7864 g_warning ("Unknown generic sharing option `%s'.", option);
7867 if (!gshared_supported)
7868 generic_sharing = MONO_GENERIC_SHARING_NONE;
7873 switch (generic_sharing) {
7874 case MONO_GENERIC_SHARING_NONE:
7876 case MONO_GENERIC_SHARING_ALL:
7878 case MONO_GENERIC_SHARING_CORLIB :
7879 return class->image == mono_defaults.corlib;
7880 case MONO_GENERIC_SHARING_COLLECTIONS:
7881 if (class->image != mono_defaults.corlib)
7883 while (class->nested_in)
7884 class = class->nested_in;
7885 return g_str_has_prefix (class->name_space, "System.Collections.Generic");
7887 g_assert_not_reached ();
7892 * mono_class_setup_interface_id:
7894 * Initializes MonoClass::interface_id if required.
7896 * LOCKING: Acquires the loader lock.
7899 mono_class_setup_interface_id (MonoClass *class)
7901 mono_loader_lock ();
7902 if (MONO_CLASS_IS_INTERFACE (class) && !class->interface_id)
7903 class->interface_id = mono_get_unique_iid (class);
7904 mono_loader_unlock ();
7908 * mono_class_alloc_ext:
7910 * Allocate klass->ext if not already done.
7911 * LOCKING: Assumes the loader lock is held.
7914 mono_class_alloc_ext (MonoClass *klass)
7917 if (klass->generic_class) {
7918 klass->ext = g_new0 (MonoClassExt, 1);
7920 klass->ext = mono_image_alloc0 (klass->image, sizeof (MonoClassExt));
7922 class_ext_size += sizeof (MonoClassExt);
7927 * mono_class_setup_interfaces:
7929 * Initialize class->interfaces/interfaces_count.
7930 * LOCKING: Acquires the loader lock.
7933 mono_class_setup_interfaces (MonoClass *klass)
7937 if (klass->interfaces_inited)
7940 mono_loader_lock ();
7942 if (klass->interfaces_inited) {
7943 mono_loader_unlock ();
7947 if (klass->rank == 1 && klass->byval_arg.type != MONO_TYPE_ARRAY && mono_defaults.generic_ilist_class) {
7950 /* generic IList, ICollection, IEnumerable */
7951 klass->interface_count = 1;
7952 klass->interfaces = mono_image_alloc0 (klass->image, sizeof (MonoClass*) * klass->interface_count);
7954 args [0] = &klass->element_class->byval_arg;
7955 klass->interfaces [0] = mono_class_bind_generic_parameters (
7956 mono_defaults.generic_ilist_class, 1, args, FALSE);
7957 } else if (klass->generic_class) {
7958 MonoClass *gklass = klass->generic_class->container_class;
7960 klass->interface_count = gklass->interface_count;
7961 klass->interfaces = g_new0 (MonoClass *, klass->interface_count);
7962 for (i = 0; i < klass->interface_count; i++)
7963 klass->interfaces [i] = mono_class_inflate_generic_class (gklass->interfaces [i], mono_generic_class_get_context (klass->generic_class));
7966 mono_memory_barrier ();
7968 klass->interfaces_inited = TRUE;
7970 mono_loader_unlock ();