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>
42 #include <mono/utils/mono-string.h>
43 #include <mono/utils/mono-error-internals.h>
47 gboolean mono_print_vtable
= FALSE
;
50 guint32 inflated_classes
, inflated_classes_size
, inflated_methods_size
;
51 guint32 classes_size
, class_ext_size
;
53 /* Function supplied by the runtime to find classes by name using information from the AOT file */
54 static MonoGetClassFromName get_class_from_name
= NULL
;
56 static MonoClass
* mono_class_create_from_typedef (MonoImage
*image
, guint32 type_token
);
57 static gboolean
mono_class_get_cached_class_info (MonoClass
*klass
, MonoCachedClassInfo
*res
);
58 static gboolean
can_access_type (MonoClass
*access_klass
, MonoClass
*member_klass
);
59 static MonoMethod
* find_method_in_metadata (MonoClass
*klass
, const char *name
, int param_count
, int flags
);
60 static int generic_array_methods (MonoClass
*class);
61 static void setup_generic_array_ifaces (MonoClass
*class, MonoClass
*iface
, MonoMethod
**methods
, int pos
);
63 static MonoMethod
* mono_class_get_virtual_methods (MonoClass
* klass
, gpointer
*iter
);
64 static char* mono_assembly_name_from_token (MonoImage
*image
, guint32 type_token
);
65 static gboolean
mono_class_is_variant_compatible (MonoClass
*klass
, MonoClass
*oklass
);
68 void (*mono_debugger_class_init_func
) (MonoClass
*klass
) = NULL
;
69 void (*mono_debugger_class_loaded_methods_func
) (MonoClass
*klass
) = NULL
;
72 * mono_class_from_typeref:
74 * @type_token: a TypeRef token
76 * Creates the MonoClass* structure representing the type defined by
77 * the typeref token valid inside @image.
78 * Returns: the MonoClass* representing the typeref token, NULL ifcould
82 mono_class_from_typeref (MonoImage
*image
, guint32 type_token
)
84 guint32 cols
[MONO_TYPEREF_SIZE
];
85 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_TYPEREF
];
87 const char *name
, *nspace
;
91 mono_metadata_decode_row (t
, (type_token
&0xffffff)-1, cols
, MONO_TYPEREF_SIZE
);
93 name
= mono_metadata_string_heap (image
, cols
[MONO_TYPEREF_NAME
]);
94 nspace
= mono_metadata_string_heap (image
, cols
[MONO_TYPEREF_NAMESPACE
]);
96 idx
= cols
[MONO_TYPEREF_SCOPE
] >> MONO_RESOLTION_SCOPE_BITS
;
97 switch (cols
[MONO_TYPEREF_SCOPE
] & MONO_RESOLTION_SCOPE_MASK
) {
98 case MONO_RESOLTION_SCOPE_MODULE
:
100 g_error ("null ResolutionScope not yet handled");
101 /* a typedef in disguise */
102 return mono_class_from_name (image
, nspace
, name
);
103 case MONO_RESOLTION_SCOPE_MODULEREF
:
104 module
= mono_image_load_module (image
, idx
);
106 return mono_class_from_name (module
, nspace
, name
);
108 char *msg
= g_strdup_printf ("%s%s%s", nspace
, nspace
[0] ? "." : "", name
);
111 human_name
= mono_stringify_assembly_name (&image
->assembly
->aname
);
112 mono_loader_set_error_type_load (msg
, human_name
);
118 case MONO_RESOLTION_SCOPE_TYPEREF
: {
119 MonoClass
*enclosing
;
122 if (idx
== mono_metadata_token_index (type_token
)) {
123 mono_loader_set_error_bad_image (g_strdup_printf ("Image %s with self-referencing typeref token %08x.", image
->name
, type_token
));
127 enclosing
= mono_class_from_typeref (image
, MONO_TOKEN_TYPE_REF
| idx
);
131 if (enclosing
->nested_classes_inited
&& enclosing
->ext
) {
132 /* Micro-optimization: don't scan the metadata tables if enclosing is already inited */
133 for (tmp
= enclosing
->ext
->nested_classes
; tmp
; tmp
= tmp
->next
) {
135 if (strcmp (res
->name
, name
) == 0)
139 /* Don't call mono_class_init as we might've been called by it recursively */
140 int i
= mono_metadata_nesting_typedef (enclosing
->image
, enclosing
->type_token
, 1);
142 guint32 class_nested
= mono_metadata_decode_row_col (&enclosing
->image
->tables
[MONO_TABLE_NESTEDCLASS
], i
- 1, MONO_NESTED_CLASS_NESTED
);
143 guint32 string_offset
= mono_metadata_decode_row_col (&enclosing
->image
->tables
[MONO_TABLE_TYPEDEF
], class_nested
- 1, MONO_TYPEDEF_NAME
);
144 const char *nname
= mono_metadata_string_heap (enclosing
->image
, string_offset
);
146 if (strcmp (nname
, name
) == 0)
147 return mono_class_create_from_typedef (enclosing
->image
, MONO_TOKEN_TYPE_DEF
| class_nested
);
149 i
= mono_metadata_nesting_typedef (enclosing
->image
, enclosing
->type_token
, i
+ 1);
152 g_warning ("TypeRef ResolutionScope not yet handled (%d) for %s.%s in image %s", idx
, nspace
, name
, image
->name
);
155 case MONO_RESOLTION_SCOPE_ASSEMBLYREF
:
159 if (idx
> image
->tables
[MONO_TABLE_ASSEMBLYREF
].rows
) {
160 mono_loader_set_error_bad_image (g_strdup_printf ("Image %s with invalid assemblyref token %08x.", image
->name
, idx
));
164 if (!image
->references
|| !image
->references
[idx
- 1])
165 mono_assembly_load_reference (image
, idx
- 1);
166 g_assert (image
->references
[idx
- 1]);
168 /* If the assembly did not load, register this as a type load exception */
169 if (image
->references
[idx
- 1] == REFERENCE_MISSING
){
170 MonoAssemblyName aname
;
173 mono_assembly_get_assemblyref (image
, idx
- 1, &aname
);
174 human_name
= mono_stringify_assembly_name (&aname
);
175 mono_loader_set_error_assembly_load (human_name
, image
->assembly
->ref_only
);
181 return mono_class_from_name (image
->references
[idx
- 1]->image
, nspace
, name
);
186 mono_image_memdup (MonoImage
*image
, void *data
, guint size
)
188 void *res
= mono_image_alloc (image
, size
);
189 memcpy (res
, data
, size
);
193 /* Copy everything mono_metadata_free_array free. */
195 mono_dup_array_type (MonoImage
*image
, MonoArrayType
*a
)
198 a
= mono_image_memdup (image
, a
, sizeof (MonoArrayType
));
200 a
->sizes
= mono_image_memdup (image
, a
->sizes
, a
->numsizes
* sizeof (int));
202 a
->lobounds
= mono_image_memdup (image
, a
->lobounds
, a
->numlobounds
* sizeof (int));
204 a
= g_memdup (a
, sizeof (MonoArrayType
));
206 a
->sizes
= g_memdup (a
->sizes
, a
->numsizes
* sizeof (int));
208 a
->lobounds
= g_memdup (a
->lobounds
, a
->numlobounds
* sizeof (int));
213 /* Copy everything mono_metadata_free_method_signature free. */
215 mono_metadata_signature_deep_dup (MonoImage
*image
, MonoMethodSignature
*sig
)
219 sig
= mono_metadata_signature_dup_full (image
, sig
);
221 sig
->ret
= mono_metadata_type_dup (image
, sig
->ret
);
222 for (i
= 0; i
< sig
->param_count
; ++i
)
223 sig
->params
[i
] = mono_metadata_type_dup (image
, sig
->params
[i
]);
229 _mono_type_get_assembly_name (MonoClass
*klass
, GString
*str
)
231 MonoAssembly
*ta
= klass
->image
->assembly
;
233 g_string_append_printf (
234 str
, ", %s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s%s",
236 ta
->aname
.major
, ta
->aname
.minor
, ta
->aname
.build
, ta
->aname
.revision
,
237 ta
->aname
.culture
&& *ta
->aname
.culture
? ta
->aname
.culture
: "neutral",
238 ta
->aname
.public_key_token
[0] ? (char *)ta
->aname
.public_key_token
: "null",
239 (ta
->aname
.flags
& ASSEMBLYREF_RETARGETABLE_FLAG
) ? ", Retargetable=Yes" : "");
243 mono_type_name_check_byref (MonoType
*type
, GString
*str
)
246 g_string_append_c (str
, '&');
250 mono_type_get_name_recurse (MonoType
*type
, GString
*str
, gboolean is_recursed
,
251 MonoTypeNameFormat format
)
255 switch (type
->type
) {
256 case MONO_TYPE_ARRAY
: {
257 int i
, rank
= type
->data
.array
->rank
;
258 MonoTypeNameFormat nested_format
;
260 nested_format
= format
== MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
?
261 MONO_TYPE_NAME_FORMAT_FULL_NAME
: format
;
263 mono_type_get_name_recurse (
264 &type
->data
.array
->eklass
->byval_arg
, str
, FALSE
, nested_format
);
265 g_string_append_c (str
, '[');
267 g_string_append_c (str
, '*');
268 for (i
= 1; i
< rank
; i
++)
269 g_string_append_c (str
, ',');
270 g_string_append_c (str
, ']');
272 mono_type_name_check_byref (type
, str
);
274 if (format
== MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
)
275 _mono_type_get_assembly_name (type
->data
.array
->eklass
, str
);
278 case MONO_TYPE_SZARRAY
: {
279 MonoTypeNameFormat nested_format
;
281 nested_format
= format
== MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
?
282 MONO_TYPE_NAME_FORMAT_FULL_NAME
: format
;
284 mono_type_get_name_recurse (
285 &type
->data
.klass
->byval_arg
, str
, FALSE
, nested_format
);
286 g_string_append (str
, "[]");
288 mono_type_name_check_byref (type
, str
);
290 if (format
== MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
)
291 _mono_type_get_assembly_name (type
->data
.klass
, str
);
294 case MONO_TYPE_PTR
: {
295 MonoTypeNameFormat nested_format
;
297 nested_format
= format
== MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
?
298 MONO_TYPE_NAME_FORMAT_FULL_NAME
: format
;
300 mono_type_get_name_recurse (
301 type
->data
.type
, str
, FALSE
, nested_format
);
302 g_string_append_c (str
, '*');
304 mono_type_name_check_byref (type
, str
);
306 if (format
== MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
)
307 _mono_type_get_assembly_name (mono_class_from_mono_type (type
->data
.type
), str
);
312 if (!mono_generic_param_info (type
->data
.generic_param
))
313 g_string_append_printf (str
, "%s%d", type
->type
== MONO_TYPE_VAR
? "!" : "!!", type
->data
.generic_param
->num
);
315 g_string_append (str
, mono_generic_param_info (type
->data
.generic_param
)->name
);
317 mono_type_name_check_byref (type
, str
);
321 klass
= mono_class_from_mono_type (type
);
322 if (klass
->nested_in
) {
323 mono_type_get_name_recurse (
324 &klass
->nested_in
->byval_arg
, str
, TRUE
, format
);
325 if (format
== MONO_TYPE_NAME_FORMAT_IL
)
326 g_string_append_c (str
, '.');
328 g_string_append_c (str
, '+');
329 } else if (*klass
->name_space
) {
330 g_string_append (str
, klass
->name_space
);
331 g_string_append_c (str
, '.');
333 if (format
== MONO_TYPE_NAME_FORMAT_IL
) {
334 char *s
= strchr (klass
->name
, '`');
335 int len
= s
? s
- klass
->name
: strlen (klass
->name
);
337 g_string_append_len (str
, klass
->name
, len
);
339 g_string_append (str
, klass
->name
);
342 if (klass
->generic_class
) {
343 MonoGenericClass
*gclass
= klass
->generic_class
;
344 MonoGenericInst
*inst
= gclass
->context
.class_inst
;
345 MonoTypeNameFormat nested_format
;
348 nested_format
= format
== MONO_TYPE_NAME_FORMAT_FULL_NAME
?
349 MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
: format
;
351 if (format
== MONO_TYPE_NAME_FORMAT_IL
)
352 g_string_append_c (str
, '<');
354 g_string_append_c (str
, '[');
355 for (i
= 0; i
< inst
->type_argc
; i
++) {
356 MonoType
*t
= inst
->type_argv
[i
];
359 g_string_append_c (str
, ',');
360 if ((nested_format
== MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
) &&
361 (t
->type
!= MONO_TYPE_VAR
) && (type
->type
!= MONO_TYPE_MVAR
))
362 g_string_append_c (str
, '[');
363 mono_type_get_name_recurse (inst
->type_argv
[i
], str
, FALSE
, nested_format
);
364 if ((nested_format
== MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
) &&
365 (t
->type
!= MONO_TYPE_VAR
) && (type
->type
!= MONO_TYPE_MVAR
))
366 g_string_append_c (str
, ']');
368 if (format
== MONO_TYPE_NAME_FORMAT_IL
)
369 g_string_append_c (str
, '>');
371 g_string_append_c (str
, ']');
372 } else if (klass
->generic_container
&&
373 (format
!= MONO_TYPE_NAME_FORMAT_FULL_NAME
) &&
374 (format
!= MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
)) {
377 if (format
== MONO_TYPE_NAME_FORMAT_IL
)
378 g_string_append_c (str
, '<');
380 g_string_append_c (str
, '[');
381 for (i
= 0; i
< klass
->generic_container
->type_argc
; i
++) {
383 g_string_append_c (str
, ',');
384 g_string_append (str
, mono_generic_container_get_param_info (klass
->generic_container
, i
)->name
);
386 if (format
== MONO_TYPE_NAME_FORMAT_IL
)
387 g_string_append_c (str
, '>');
389 g_string_append_c (str
, ']');
392 mono_type_name_check_byref (type
, str
);
394 if ((format
== MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
) &&
395 (type
->type
!= MONO_TYPE_VAR
) && (type
->type
!= MONO_TYPE_MVAR
))
396 _mono_type_get_assembly_name (klass
, str
);
402 * mono_type_get_name_full:
404 * @format: the format for the return string.
407 * Returns: the string representation in a number of formats:
409 * if format is MONO_TYPE_NAME_FORMAT_REFLECTION, the return string is
410 * returned in the formatrequired by System.Reflection, this is the
411 * inverse of mono_reflection_parse_type ().
413 * if format is MONO_TYPE_NAME_FORMAT_IL, it returns a syntax that can
414 * be used by the IL assembler.
416 * if format is MONO_TYPE_NAME_FORMAT_FULL_NAME
418 * if format is MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
421 mono_type_get_name_full (MonoType
*type
, MonoTypeNameFormat format
)
425 result
= g_string_new ("");
427 mono_type_get_name_recurse (type
, result
, FALSE
, format
);
429 return g_string_free (result
, FALSE
);
433 * mono_type_get_full_name:
436 * Returns: the string representation for type as required by System.Reflection.
437 * The inverse of mono_reflection_parse_type ().
440 mono_type_get_full_name (MonoClass
*class)
442 return mono_type_get_name_full (mono_class_get_type (class), MONO_TYPE_NAME_FORMAT_REFLECTION
);
446 * mono_type_get_name:
449 * Returns: the string representation for type as it would be represented in IL code.
452 mono_type_get_name (MonoType
*type
)
454 return mono_type_get_name_full (type
, MONO_TYPE_NAME_FORMAT_IL
);
458 * mono_type_get_underlying_type:
461 * Returns: the MonoType for the underlying integer type if @type
462 * is an enum and byref is false, otherwise the type itself.
465 mono_type_get_underlying_type (MonoType
*type
)
467 if (type
->type
== MONO_TYPE_VALUETYPE
&& type
->data
.klass
->enumtype
&& !type
->byref
)
468 return mono_class_enum_basetype (type
->data
.klass
);
469 if (type
->type
== MONO_TYPE_GENERICINST
&& type
->data
.generic_class
->container_class
->enumtype
&& !type
->byref
)
470 return mono_class_enum_basetype (type
->data
.generic_class
->container_class
);
475 * mono_class_is_open_constructed_type:
478 * Returns TRUE if type represents a generics open constructed type
479 * (not all the type parameters required for the instantiation have
483 mono_class_is_open_constructed_type (MonoType
*t
)
489 case MONO_TYPE_SZARRAY
:
490 return mono_class_is_open_constructed_type (&t
->data
.klass
->byval_arg
);
491 case MONO_TYPE_ARRAY
:
492 return mono_class_is_open_constructed_type (&t
->data
.array
->eklass
->byval_arg
);
494 return mono_class_is_open_constructed_type (t
->data
.type
);
495 case MONO_TYPE_GENERICINST
:
496 return t
->data
.generic_class
->context
.class_inst
->is_open
;
503 inflate_generic_type (MonoImage
*image
, MonoType
*type
, MonoGenericContext
*context
, MonoError
*error
)
505 mono_error_init (error
);
507 switch (type
->type
) {
508 case MONO_TYPE_MVAR
: {
510 int num
= mono_type_get_generic_param_num (type
);
511 MonoGenericInst
*inst
= context
->method_inst
;
512 if (!inst
|| !inst
->type_argv
)
514 if (num
>= inst
->type_argc
) {
515 MonoGenericParamInfo
*info
= mono_generic_param_info (type
->data
.generic_param
);
516 mono_error_set_bad_image (error
, image
, "MVAR %d (%s) cannot be expanded in this context with %d instantiations",
517 num
, info
? info
->name
: "", inst
->type_argc
);
522 * Note that the VAR/MVAR cases are different from the rest. The other cases duplicate @type,
523 * while the VAR/MVAR duplicates a type from the context. So, we need to ensure that the
524 * ->byref and ->attrs from @type are propagated to the returned type.
526 nt
= mono_metadata_type_dup (image
, inst
->type_argv
[num
]);
527 nt
->byref
= type
->byref
;
528 nt
->attrs
= type
->attrs
;
531 case MONO_TYPE_VAR
: {
533 int num
= mono_type_get_generic_param_num (type
);
534 MonoGenericInst
*inst
= context
->class_inst
;
537 if (num
>= inst
->type_argc
) {
538 MonoGenericParamInfo
*info
= mono_generic_param_info (type
->data
.generic_param
);
539 mono_error_set_bad_image (error
, image
, "VAR %d (%s) cannot be expanded in this context with %d instantiations",
540 num
, info
? info
->name
: "", inst
->type_argc
);
543 nt
= mono_metadata_type_dup (image
, inst
->type_argv
[num
]);
544 nt
->byref
= type
->byref
;
545 nt
->attrs
= type
->attrs
;
548 case MONO_TYPE_SZARRAY
: {
549 MonoClass
*eclass
= type
->data
.klass
;
550 MonoType
*nt
, *inflated
= inflate_generic_type (NULL
, &eclass
->byval_arg
, context
, error
);
551 if (!inflated
|| !mono_error_ok (error
))
553 nt
= mono_metadata_type_dup (image
, type
);
554 nt
->data
.klass
= mono_class_from_mono_type (inflated
);
555 mono_metadata_free_type (inflated
);
558 case MONO_TYPE_ARRAY
: {
559 MonoClass
*eclass
= type
->data
.array
->eklass
;
560 MonoType
*nt
, *inflated
= inflate_generic_type (NULL
, &eclass
->byval_arg
, context
, error
);
561 if (!inflated
|| !mono_error_ok (error
))
563 nt
= mono_metadata_type_dup (image
, type
);
564 nt
->data
.array
= g_memdup (nt
->data
.array
, sizeof (MonoArrayType
));
565 nt
->data
.array
->eklass
= mono_class_from_mono_type (inflated
);
566 mono_metadata_free_type (inflated
);
569 case MONO_TYPE_GENERICINST
: {
570 MonoGenericClass
*gclass
= type
->data
.generic_class
;
571 MonoGenericInst
*inst
;
573 if (!gclass
->context
.class_inst
->is_open
)
576 inst
= mono_metadata_inflate_generic_inst (gclass
->context
.class_inst
, context
, error
);
577 if (!mono_error_ok (error
))
579 if (inst
!= gclass
->context
.class_inst
)
580 gclass
= mono_metadata_lookup_generic_class (gclass
->container_class
, inst
, gclass
->is_dynamic
);
582 if (gclass
== type
->data
.generic_class
)
585 nt
= mono_metadata_type_dup (image
, type
);
586 nt
->data
.generic_class
= gclass
;
589 case MONO_TYPE_CLASS
:
590 case MONO_TYPE_VALUETYPE
: {
591 MonoClass
*klass
= type
->data
.klass
;
592 MonoGenericContainer
*container
= klass
->generic_container
;
593 MonoGenericInst
*inst
;
594 MonoGenericClass
*gclass
= NULL
;
600 /* We can't use context->class_inst directly, since it can have more elements */
601 inst
= mono_metadata_inflate_generic_inst (container
->context
.class_inst
, context
, error
);
602 if (!mono_error_ok (error
))
604 if (inst
== container
->context
.class_inst
)
607 gclass
= mono_metadata_lookup_generic_class (klass
, inst
, klass
->image
->dynamic
);
609 nt
= mono_metadata_type_dup (image
, type
);
610 nt
->type
= MONO_TYPE_GENERICINST
;
611 nt
->data
.generic_class
= gclass
;
621 mono_generic_class_get_context (MonoGenericClass
*gclass
)
623 return &gclass
->context
;
627 mono_class_get_context (MonoClass
*class)
629 return class->generic_class
? mono_generic_class_get_context (class->generic_class
) : NULL
;
633 * mono_class_get_generic_container:
635 * Return the generic container of KLASS which should be a generic type definition.
637 MonoGenericContainer
*
638 mono_class_get_generic_container (MonoClass
*klass
)
640 g_assert (klass
->is_generic
);
642 return klass
->generic_container
;
646 * mono_class_get_generic_class:
648 * Return the MonoGenericClass of KLASS, which should be a generic instance.
651 mono_class_get_generic_class (MonoClass
*klass
)
653 g_assert (klass
->is_inflated
);
655 return klass
->generic_class
;
659 * mono_class_inflate_generic_type_with_mempool:
660 * @mempool: a mempool
662 * @context: a generics context
663 * @error: error context
665 * The same as mono_class_inflate_generic_type, but allocates the MonoType
666 * from mempool if it is non-NULL. If it is NULL, the MonoType is
667 * allocated on the heap and is owned by the caller.
668 * The returned type can potentially be the same as TYPE, so it should not be
669 * modified by the caller, and it should be freed using mono_metadata_free_type ().
672 mono_class_inflate_generic_type_with_mempool (MonoImage
*image
, MonoType
*type
, MonoGenericContext
*context
, MonoError
*error
)
674 MonoType
*inflated
= NULL
;
675 mono_error_init (error
);
678 inflated
= inflate_generic_type (image
, type
, context
, error
);
679 if (!mono_error_ok (error
))
683 MonoType
*shared
= mono_metadata_get_shared_type (type
);
688 return mono_metadata_type_dup (image
, type
);
692 mono_stats
.inflated_type_count
++;
697 * mono_class_inflate_generic_type:
699 * @context: a generics context
701 * If @type is a generic type and @context is not NULL, instantiate it using the
702 * generics context @context.
704 * Returns: the instantiated type or a copy of @type. The returned MonoType is allocated
705 * on the heap and is owned by the caller. Returns NULL on error.
707 * @deprecated Please use mono_class_inflate_generic_type_checked instead
710 mono_class_inflate_generic_type (MonoType
*type
, MonoGenericContext
*context
)
714 result
= mono_class_inflate_generic_type_checked (type
, context
, &error
);
716 if (!mono_error_ok (&error
)) {
717 mono_error_cleanup (&error
);
724 * mono_class_inflate_generic_type:
726 * @context: a generics context
727 * @error: error context to use
729 * If @type is a generic type and @context is not NULL, instantiate it using the
730 * generics context @context.
732 * Returns: the instantiated type or a copy of @type. The returned MonoType is allocated
733 * on the heap and is owned by the caller.
736 mono_class_inflate_generic_type_checked (MonoType
*type
, MonoGenericContext
*context
, MonoError
*error
)
738 return mono_class_inflate_generic_type_with_mempool (NULL
, type
, context
, error
);
742 * mono_class_inflate_generic_type_no_copy:
744 * Same as inflate_generic_type_with_mempool, but return TYPE if no inflation
748 mono_class_inflate_generic_type_no_copy (MonoImage
*image
, MonoType
*type
, MonoGenericContext
*context
)
751 MonoType
*inflated
= NULL
;
754 inflated
= inflate_generic_type (image
, type
, context
, &error
);
755 g_assert (mono_error_ok (&error
)); /*FIXME proper error handling*/
761 mono_stats
.inflated_type_count
++;
766 mono_class_inflate_generic_class_checked (MonoClass
*gklass
, MonoGenericContext
*context
, MonoError
*error
)
771 inflated
= mono_class_inflate_generic_type_checked (&gklass
->byval_arg
, context
, error
);
772 if (!mono_error_ok (error
))
775 res
= mono_class_from_mono_type (inflated
);
776 mono_metadata_free_type (inflated
);
781 * mono_class_inflate_generic_class:
783 * Inflate the class GKLASS with CONTEXT.
786 mono_class_inflate_generic_class (MonoClass
*gklass
, MonoGenericContext
*context
)
791 res
= mono_class_inflate_generic_class_checked (gklass
, context
, &error
);
792 g_assert (mono_error_ok (&error
)); /*FIXME proper error handling*/
799 static MonoGenericContext
800 inflate_generic_context (MonoGenericContext
*context
, MonoGenericContext
*inflate_with
, MonoError
*error
)
802 MonoGenericInst
*class_inst
= NULL
;
803 MonoGenericInst
*method_inst
= NULL
;
804 MonoGenericContext res
= { NULL
, NULL
};
806 mono_error_init (error
);
808 if (context
->class_inst
) {
809 class_inst
= mono_metadata_inflate_generic_inst (context
->class_inst
, inflate_with
, error
);
810 if (!mono_error_ok (error
))
814 if (context
->method_inst
) {
815 method_inst
= mono_metadata_inflate_generic_inst (context
->method_inst
, inflate_with
, error
);
816 if (!mono_error_ok (error
))
820 res
.class_inst
= class_inst
;
821 res
.method_inst
= method_inst
;
827 * mono_class_inflate_generic_method:
828 * @method: a generic method
829 * @context: a generics context
831 * Instantiate the generic method @method using the generics context @context.
833 * Returns: the new instantiated method
836 mono_class_inflate_generic_method (MonoMethod
*method
, MonoGenericContext
*context
)
838 return mono_class_inflate_generic_method_full (method
, NULL
, context
);
842 * mono_class_inflate_generic_method_full:
844 * Instantiate method @method with the generic context @context.
845 * BEWARE: All non-trivial fields are invalid, including klass, signature, and header.
846 * Use mono_method_signature () and mono_method_get_header () to get the correct values.
849 mono_class_inflate_generic_method_full (MonoMethod
*method
, MonoClass
*klass_hint
, MonoGenericContext
*context
)
852 MonoMethod
*res
= mono_class_inflate_generic_method_full_checked (method
, klass_hint
, context
, &error
);
853 if (!mono_error_ok (&error
))
854 /*FIXME do proper error handling - on this case, kill this function. */
855 g_error ("Could not inflate generic method due to %s", mono_error_get_message (&error
));
861 * mono_class_inflate_generic_method_full_checked:
862 * Same as mono_class_inflate_generic_method_full but return failure using @error.
865 mono_class_inflate_generic_method_full_checked (MonoMethod
*method
, MonoClass
*klass_hint
, MonoGenericContext
*context
, MonoError
*error
)
868 MonoMethodInflated
*iresult
, *cached
;
869 MonoMethodSignature
*sig
;
870 MonoGenericContext tmp_context
;
871 gboolean is_mb_open
= FALSE
;
873 mono_error_init (error
);
875 /* The `method' has already been instantiated before => we need to peel out the instantiation and create a new context */
876 while (method
->is_inflated
) {
877 MonoGenericContext
*method_context
= mono_method_get_context (method
);
878 MonoMethodInflated
*imethod
= (MonoMethodInflated
*) method
;
880 tmp_context
= inflate_generic_context (method_context
, context
, error
);
881 if (!mono_error_ok (error
))
883 context
= &tmp_context
;
885 if (mono_metadata_generic_context_equal (method_context
, context
))
888 method
= imethod
->declaring
;
891 if (!method
->is_generic
&& !method
->klass
->generic_container
)
895 * The reason for this hack is to fix the behavior of inflating generic methods that come from a MethodBuilder.
896 * What happens is that instantiating a generic MethodBuilder with its own arguments should create a diferent object.
897 * This is opposite to the way non-SRE MethodInfos behave.
899 * This happens, for example, when we want to emit a recursive generic method. Given the following C# code:
901 * void Example<T> () {
905 * In Example, the method token must be encoded as: "void Example<!!0>()"
907 * The reference to the first generic argument, "!!0", must be explicit otherwise it won't be inflated
908 * properly. To get that we need to inflate the MethodBuilder with its own arguments.
910 * On the other hand, inflating a non-SRE generic method with its own arguments should
911 * return itself. For example:
913 * MethodInfo m = ... //m is a generic method definition
914 * MethodInfo res = m.MakeGenericMethod (m.GetGenericArguments ());
917 * To allow such scenarios we must allow inflation of MethodBuilder to happen in a diferent way than
918 * what happens with regular methods.
920 * There is one last touch to this madness, once a TypeBuilder is finished, IOW CreateType() is called,
921 * everything should behave like a regular type or method.
924 is_mb_open
= method
->is_generic
&&
925 method
->klass
->image
->dynamic
&& !method
->klass
->wastypebuilder
&& /* that is a MethodBuilder from an unfinished TypeBuilder */
926 context
->method_inst
== mono_method_get_generic_container (method
)->context
.method_inst
; /* and it's been instantiated with its own arguments. */
928 iresult
= g_new0 (MonoMethodInflated
, 1);
929 iresult
->context
= *context
;
930 iresult
->declaring
= method
;
931 iresult
->method
.method
.is_mb_open
= is_mb_open
;
933 if (!context
->method_inst
&& method
->is_generic
)
934 iresult
->context
.method_inst
= mono_method_get_generic_container (method
)->context
.method_inst
;
936 if (!context
->class_inst
) {
937 g_assert (!iresult
->declaring
->klass
->generic_class
);
938 if (iresult
->declaring
->klass
->generic_container
)
939 iresult
->context
.class_inst
= iresult
->declaring
->klass
->generic_container
->context
.class_inst
;
940 else if (iresult
->declaring
->klass
->generic_class
)
941 iresult
->context
.class_inst
= iresult
->declaring
->klass
->generic_class
->context
.class_inst
;
945 cached
= mono_method_inflated_lookup (iresult
, FALSE
);
947 mono_loader_unlock ();
949 return (MonoMethod
*)cached
;
952 mono_stats
.inflated_method_count
++;
954 inflated_methods_size
+= sizeof (MonoMethodInflated
);
956 sig
= mono_method_signature (method
);
958 char *name
= mono_type_get_full_name (method
->klass
);
959 mono_error_set_bad_image (error
, method
->klass
->image
, "Could not resolve signature of method %s:%s", name
, method
->name
);
965 memcpy (&iresult
->method
.pinvoke
, method
, sizeof (MonoMethodPInvoke
));
967 memcpy (&iresult
->method
.normal
, method
, sizeof (MonoMethodNormal
));
968 iresult
->method
.normal
.header
= NULL
;
971 result
= (MonoMethod
*) iresult
;
972 result
->is_inflated
= TRUE
;
973 result
->is_generic
= FALSE
;
974 result
->signature
= NULL
;
975 result
->is_mb_open
= is_mb_open
;
977 if (!context
->method_inst
) {
978 /* Set the generic_container of the result to the generic_container of method */
979 MonoGenericContainer
*generic_container
= mono_method_get_generic_container (method
);
981 if (generic_container
) {
982 result
->is_generic
= 1;
983 mono_method_set_generic_container (result
, generic_container
);
987 if (!klass_hint
|| !klass_hint
->generic_class
||
988 klass_hint
->generic_class
->container_class
!= method
->klass
||
989 klass_hint
->generic_class
->context
.class_inst
!= context
->class_inst
)
992 if (method
->klass
->generic_container
)
993 result
->klass
= klass_hint
;
995 if (!result
->klass
) {
996 MonoType
*inflated
= inflate_generic_type (NULL
, &method
->klass
->byval_arg
, context
, error
);
997 if (!mono_error_ok (error
))
1000 result
->klass
= inflated
? mono_class_from_mono_type (inflated
) : method
->klass
;
1002 mono_metadata_free_type (inflated
);
1005 mono_method_inflated_lookup (iresult
, TRUE
);
1006 mono_loader_unlock ();
1010 mono_loader_unlock ();
1016 * mono_get_inflated_method:
1018 * Obsolete. We keep it around since it's mentioned in the public API.
1021 mono_get_inflated_method (MonoMethod
*method
)
1027 * mono_method_get_context_general:
1029 * @uninflated: handle uninflated methods?
1031 * Returns the generic context of a method or NULL if it doesn't have
1032 * one. For an inflated method that's the context stored in the
1033 * method. Otherwise it's in the method's generic container or in the
1034 * generic container of the method's class.
1037 mono_method_get_context_general (MonoMethod
*method
, gboolean uninflated
)
1039 if (method
->is_inflated
) {
1040 MonoMethodInflated
*imethod
= (MonoMethodInflated
*) method
;
1041 return &imethod
->context
;
1045 if (method
->is_generic
)
1046 return &(mono_method_get_generic_container (method
)->context
);
1047 if (method
->klass
->generic_container
)
1048 return &method
->klass
->generic_container
->context
;
1053 * mono_method_get_context:
1056 * Returns the generic context for method if it's inflated, otherwise
1060 mono_method_get_context (MonoMethod
*method
)
1062 return mono_method_get_context_general (method
, FALSE
);
1066 * mono_method_get_generic_container:
1068 * Returns the generic container of METHOD, which should be a generic method definition.
1069 * Returns NULL if METHOD is not a generic method definition.
1070 * LOCKING: Acquires the loader lock.
1072 MonoGenericContainer
*
1073 mono_method_get_generic_container (MonoMethod
*method
)
1075 MonoGenericContainer
*container
;
1077 if (!method
->is_generic
)
1080 container
= mono_image_property_lookup (method
->klass
->image
, method
, MONO_METHOD_PROP_GENERIC_CONTAINER
);
1081 g_assert (container
);
1087 * mono_method_set_generic_container:
1089 * Sets the generic container of METHOD to CONTAINER.
1090 * LOCKING: Acquires the loader lock.
1093 mono_method_set_generic_container (MonoMethod
*method
, MonoGenericContainer
* container
)
1095 g_assert (method
->is_generic
);
1097 mono_image_property_insert (method
->klass
->image
, method
, MONO_METHOD_PROP_GENERIC_CONTAINER
, container
);
1101 * mono_class_find_enum_basetype:
1102 * @class: The enum class
1104 * Determine the basetype of an enum by iterating through its fields. We do this
1105 * in a separate function since it is cheaper than calling mono_class_setup_fields.
1108 mono_class_find_enum_basetype (MonoClass
*class)
1110 MonoGenericContainer
*container
= NULL
;
1111 MonoImage
*m
= class->image
;
1112 const int top
= class->field
.count
;
1115 g_assert (class->enumtype
);
1117 if (class->generic_container
)
1118 container
= class->generic_container
;
1119 else if (class->generic_class
) {
1120 MonoClass
*gklass
= class->generic_class
->container_class
;
1122 container
= gklass
->generic_container
;
1123 g_assert (container
);
1127 * Fetch all the field information.
1129 for (i
= 0; i
< top
; i
++){
1131 guint32 cols
[MONO_FIELD_SIZE
];
1132 int idx
= class->field
.first
+ i
;
1135 /* class->field.first and idx points into the fieldptr table */
1136 mono_metadata_decode_table_row (m
, MONO_TABLE_FIELD
, idx
, cols
, MONO_FIELD_SIZE
);
1138 if (cols
[MONO_FIELD_FLAGS
] & FIELD_ATTRIBUTE_STATIC
) //no need to decode static fields
1141 if (!mono_verifier_verify_field_signature (class->image
, cols
[MONO_FIELD_SIGNATURE
], NULL
))
1144 sig
= mono_metadata_blob_heap (m
, cols
[MONO_FIELD_SIGNATURE
]);
1145 mono_metadata_decode_value (sig
, &sig
);
1146 /* FIELD signature == 0x06 */
1150 ftype
= mono_metadata_parse_type_full (m
, container
, MONO_PARSE_FIELD
, cols
[MONO_FIELD_FLAGS
], sig
+ 1, &sig
);
1153 if (class->generic_class
) {
1154 //FIXME do we leak here?
1155 ftype
= mono_class_inflate_generic_type (ftype
, mono_class_get_context (class));
1156 ftype
->attrs
= cols
[MONO_FIELD_FLAGS
];
1166 * Checks for MonoClass::exception_type without resolving all MonoType's into MonoClass'es
1169 mono_type_has_exceptions (MonoType
*type
)
1171 switch (type
->type
) {
1172 case MONO_TYPE_CLASS
:
1173 case MONO_TYPE_VALUETYPE
:
1174 case MONO_TYPE_SZARRAY
:
1175 return type
->data
.klass
->exception_type
;
1176 case MONO_TYPE_ARRAY
:
1177 return type
->data
.array
->eklass
->exception_type
;
1178 case MONO_TYPE_GENERICINST
:
1179 return mono_generic_class_get_class (type
->data
.generic_class
)->exception_type
;
1185 * mono_class_setup_fields:
1186 * @class: The class to initialize
1188 * Initializes the class->fields.
1189 * LOCKING: Assumes the loader lock is held.
1192 mono_class_setup_fields (MonoClass
*class)
1194 MonoImage
*m
= class->image
;
1195 int top
= class->field
.count
;
1196 guint32 layout
= class->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
;
1197 int i
, blittable
= TRUE
;
1198 guint32 real_size
= 0;
1199 guint32 packing_size
= 0;
1200 gboolean explicit_size
;
1201 MonoClassField
*field
;
1202 MonoGenericContainer
*container
= NULL
;
1203 MonoClass
*gtd
= class->generic_class
? mono_class_get_generic_type_definition (class) : NULL
;
1205 if (class->size_inited
)
1208 if (class->generic_class
&& class->generic_class
->container_class
->image
->dynamic
&& !class->generic_class
->container_class
->wastypebuilder
) {
1210 * This happens when a generic instance of an unfinished generic typebuilder
1211 * is used as an element type for creating an array type. We can't initialize
1212 * the fields of this class using the fields of gklass, since gklass is not
1213 * finished yet, fields could be added to it later.
1219 mono_class_setup_fields (gtd
);
1220 if (gtd
->exception_type
) {
1221 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
1225 top
= gtd
->field
.count
;
1226 class->field
.first
= gtd
->field
.first
;
1227 class->field
.count
= gtd
->field
.count
;
1230 class->instance_size
= 0;
1232 class->sizes
.class_size
= 0;
1234 if (class->parent
) {
1235 /* For generic instances, class->parent might not have been initialized */
1236 mono_class_init (class->parent
);
1237 if (!class->parent
->size_inited
) {
1238 mono_class_setup_fields (class->parent
);
1239 if (class->parent
->exception_type
) {
1240 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
1244 class->instance_size
+= class->parent
->instance_size
;
1245 class->min_align
= class->parent
->min_align
;
1246 /* we use |= since it may have been set already */
1247 class->has_references
|= class->parent
->has_references
;
1248 blittable
= class->parent
->blittable
;
1250 class->instance_size
= sizeof (MonoObject
);
1251 class->min_align
= 1;
1254 /* We can't really enable 16 bytes alignment until the GC supports it.
1255 The whole layout/instance size code must be reviewed because we do alignment calculation in terms of the
1256 boxed instance, which leads to unexplainable holes at the beginning of an object embedding a simd type.
1257 Bug #506144 is an example of this issue.
1259 if (class->simd_type)
1260 class->min_align = 16;
1262 /* Get the real size */
1263 explicit_size
= mono_metadata_packing_from_typedef (class->image
, class->type_token
, &packing_size
, &real_size
);
1265 if (explicit_size
) {
1266 g_assert ((packing_size
& 0xfffffff0) == 0);
1267 class->packing_size
= packing_size
;
1268 real_size
+= class->instance_size
;
1272 if (explicit_size
&& real_size
) {
1273 class->instance_size
= MAX (real_size
, class->instance_size
);
1275 class->size_inited
= 1;
1276 class->blittable
= blittable
;
1280 if (layout
== TYPE_ATTRIBUTE_AUTO_LAYOUT
)
1283 /* Prevent infinite loops if the class references itself */
1284 class->size_inited
= 1;
1286 class->fields
= mono_image_alloc0 (class->image
, sizeof (MonoClassField
) * top
);
1288 if (class->generic_container
) {
1289 container
= class->generic_container
;
1291 container
= gtd
->generic_container
;
1292 g_assert (container
);
1296 * Fetch all the field information.
1298 for (i
= 0; i
< top
; i
++){
1299 int idx
= class->field
.first
+ i
;
1300 field
= &class->fields
[i
];
1302 field
->parent
= class;
1305 MonoClassField
*gfield
= >d
->fields
[i
];
1307 field
->name
= mono_field_get_name (gfield
);
1308 /*This memory must come from the image mempool as we don't have a chance to free it.*/
1309 field
->type
= mono_class_inflate_generic_type_no_copy (class->image
, gfield
->type
, mono_class_get_context (class));
1310 g_assert (field
->type
->attrs
== gfield
->type
->attrs
);
1311 if (mono_field_is_deleted (field
))
1313 field
->offset
= gfield
->offset
;
1316 guint32 cols
[MONO_FIELD_SIZE
];
1318 /* class->field.first and idx points into the fieldptr table */
1319 mono_metadata_decode_table_row (m
, MONO_TABLE_FIELD
, idx
, cols
, MONO_FIELD_SIZE
);
1320 /* The name is needed for fieldrefs */
1321 field
->name
= mono_metadata_string_heap (m
, cols
[MONO_FIELD_NAME
]);
1322 if (!mono_verifier_verify_field_signature (class->image
, cols
[MONO_FIELD_SIGNATURE
], NULL
)) {
1323 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
1326 sig
= mono_metadata_blob_heap (m
, cols
[MONO_FIELD_SIGNATURE
]);
1327 mono_metadata_decode_value (sig
, &sig
);
1328 /* FIELD signature == 0x06 */
1329 g_assert (*sig
== 0x06);
1330 field
->type
= mono_metadata_parse_type_full (m
, container
, MONO_PARSE_FIELD
, cols
[MONO_FIELD_FLAGS
], sig
+ 1, &sig
);
1332 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
1335 if (mono_field_is_deleted (field
))
1337 if (layout
== TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) {
1339 mono_metadata_field_info (m
, idx
, &offset
, NULL
, NULL
);
1340 field
->offset
= offset
;
1342 if (field
->offset
== (guint32
)-1 && !(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)) {
1343 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup_printf ("Missing field layout info for %s", field
->name
));
1346 if (field
->offset
< -1) { /*-1 is used to encode special static fields */
1347 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup_printf ("Invalid negative field offset %d for %s", field
->offset
, field
->name
));
1353 /* Only do these checks if we still think this type is blittable */
1354 if (blittable
&& !(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)) {
1355 if (field
->type
->byref
|| MONO_TYPE_IS_REFERENCE (field
->type
)) {
1358 MonoClass
*field_class
= mono_class_from_mono_type (field
->type
);
1360 mono_class_setup_fields (field_class
);
1361 if (field_class
->exception_type
) {
1362 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
1366 if (!field_class
|| !field_class
->blittable
)
1371 if (class->enumtype
&& !(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)) {
1372 class->cast_class
= class->element_class
= mono_class_from_mono_type (field
->type
);
1373 blittable
= class->element_class
->blittable
;
1376 if (mono_type_has_exceptions (field
->type
)) {
1377 char *class_name
= mono_type_get_full_name (class);
1378 char *type_name
= mono_type_full_name (field
->type
);
1380 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
1381 g_warning ("Invalid type %s for instance field %s:%s", type_name
, class_name
, field
->name
);
1382 g_free (class_name
);
1386 /* The def_value of fields is compute lazily during vtable creation */
1389 if (class == mono_defaults
.string_class
)
1392 class->blittable
= blittable
;
1394 if (class->enumtype
&& !mono_class_enum_basetype (class)) {
1395 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
1398 if (explicit_size
&& real_size
) {
1399 class->instance_size
= MAX (real_size
, class->instance_size
);
1402 if (class->exception_type
)
1404 mono_class_layout_fields (class);
1406 /*valuetypes can't be neither bigger than 1Mb or empty. */
1407 if (class->valuetype
&& (class->instance_size
<= 0 || class->instance_size
> (0x100000 + sizeof (MonoObject
))))
1408 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
1412 * mono_class_setup_fields_locking:
1413 * @class: The class to initialize
1415 * Initializes the class->fields array of fields.
1416 * Aquires the loader lock.
1419 mono_class_setup_fields_locking (MonoClass
*class)
1421 mono_loader_lock ();
1422 mono_class_setup_fields (class);
1423 mono_loader_unlock ();
1427 * mono_class_has_references:
1429 * Returns whenever @klass->has_references is set, initializing it if needed.
1430 * Aquires the loader lock.
1433 mono_class_has_references (MonoClass
*klass
)
1435 if (klass
->init_pending
) {
1436 /* Be conservative */
1439 mono_class_init (klass
);
1441 return klass
->has_references
;
1445 /* useful until we keep track of gc-references in corlib etc. */
1447 #define IS_GC_REFERENCE(t) FALSE
1449 #define IS_GC_REFERENCE(t) ((t)->type == MONO_TYPE_U && class->image == mono_defaults.corlib)
1453 * mono_type_get_basic_type_from_generic:
1456 * Returns a closed type corresponding to the possibly open type
1460 mono_type_get_basic_type_from_generic (MonoType
*type
)
1462 /* When we do generic sharing we let type variables stand for reference types. */
1463 if (!type
->byref
&& (type
->type
== MONO_TYPE_VAR
|| type
->type
== MONO_TYPE_MVAR
))
1464 return &mono_defaults
.object_class
->byval_arg
;
1469 * mono_class_layout_fields:
1472 * Compute the placement of fields inside an object or struct, according to
1473 * the layout rules and set the following fields in @class:
1474 * - has_references (if the class contains instance references firled or structs that contain references)
1475 * - has_static_refs (same, but for static fields)
1476 * - instance_size (size of the object in memory)
1477 * - class_size (size needed for the static fields)
1478 * - size_inited (flag set when the instance_size is set)
1480 * LOCKING: this is supposed to be called with the loader lock held.
1483 mono_class_layout_fields (MonoClass
*class)
1486 const int top
= class->field
.count
;
1487 guint32 layout
= class->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
;
1488 guint32 pass
, passes
, real_size
;
1489 gboolean gc_aware_layout
= FALSE
;
1490 MonoClassField
*field
;
1493 * When we do generic sharing we need to have layout
1494 * information for open generic classes (either with a generic
1495 * context containing type variables or with a generic
1496 * container), so we don't return in that case anymore.
1500 * Enable GC aware auto layout: in this mode, reference
1501 * fields are grouped together inside objects, increasing collector
1503 * Requires that all classes whose layout is known to native code be annotated
1504 * with [StructLayout (LayoutKind.Sequential)]
1505 * Value types have gc_aware_layout disabled by default, as per
1506 * what the default is for other runtimes.
1508 /* corlib is missing [StructLayout] directives in many places */
1509 if (layout
== TYPE_ATTRIBUTE_AUTO_LAYOUT
) {
1510 if (class->image
!= mono_defaults
.corlib
&&
1511 class->byval_arg
.type
!= MONO_TYPE_VALUETYPE
)
1512 gc_aware_layout
= TRUE
;
1513 /* from System.dll, used in metadata/process.h */
1514 if (strcmp (class->name
, "ProcessStartInfo") == 0)
1515 gc_aware_layout
= FALSE
;
1518 /* Compute klass->has_references */
1520 * Process non-static fields first, since static fields might recursively
1521 * refer to the class itself.
1523 for (i
= 0; i
< top
; i
++) {
1526 field
= &class->fields
[i
];
1528 if (!(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)) {
1529 ftype
= mono_type_get_underlying_type (field
->type
);
1530 ftype
= mono_type_get_basic_type_from_generic (ftype
);
1531 if (MONO_TYPE_IS_REFERENCE (ftype
) || IS_GC_REFERENCE (ftype
) || ((MONO_TYPE_ISSTRUCT (ftype
) && mono_class_has_references (mono_class_from_mono_type (ftype
)))))
1532 class->has_references
= TRUE
;
1536 for (i
= 0; i
< top
; i
++) {
1539 field
= &class->fields
[i
];
1541 if (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
) {
1542 ftype
= mono_type_get_underlying_type (field
->type
);
1543 ftype
= mono_type_get_basic_type_from_generic (ftype
);
1544 if (MONO_TYPE_IS_REFERENCE (ftype
) || IS_GC_REFERENCE (ftype
) || ((MONO_TYPE_ISSTRUCT (ftype
) && mono_class_has_references (mono_class_from_mono_type (ftype
)))))
1545 class->has_static_refs
= TRUE
;
1549 for (i
= 0; i
< top
; i
++) {
1552 field
= &class->fields
[i
];
1554 ftype
= mono_type_get_underlying_type (field
->type
);
1555 ftype
= mono_type_get_basic_type_from_generic (ftype
);
1556 if (MONO_TYPE_IS_REFERENCE (ftype
) || IS_GC_REFERENCE (ftype
) || ((MONO_TYPE_ISSTRUCT (ftype
) && mono_class_has_references (mono_class_from_mono_type (ftype
))))) {
1557 if (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
1558 class->has_static_refs
= TRUE
;
1560 class->has_references
= TRUE
;
1565 * Compute field layout and total size (not considering static fields)
1569 case TYPE_ATTRIBUTE_AUTO_LAYOUT
:
1570 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT
:
1572 if (gc_aware_layout
)
1577 if (layout
!= TYPE_ATTRIBUTE_AUTO_LAYOUT
)
1581 real_size
= class->parent
->instance_size
;
1583 real_size
= sizeof (MonoObject
);
1585 for (pass
= 0; pass
< passes
; ++pass
) {
1586 for (i
= 0; i
< top
; i
++){
1591 field
= &class->fields
[i
];
1593 if (mono_field_is_deleted (field
))
1595 if (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
1598 ftype
= mono_type_get_underlying_type (field
->type
);
1599 ftype
= mono_type_get_basic_type_from_generic (ftype
);
1600 if (gc_aware_layout
) {
1601 if (MONO_TYPE_IS_REFERENCE (ftype
) || IS_GC_REFERENCE (ftype
) || ((MONO_TYPE_ISSTRUCT (ftype
) && mono_class_has_references (mono_class_from_mono_type (ftype
))))) {
1610 if ((top
== 1) && (class->instance_size
== sizeof (MonoObject
)) &&
1611 (strcmp (mono_field_get_name (field
), "$PRIVATE$") == 0)) {
1612 /* This field is a hack inserted by MCS to empty structures */
1616 size
= mono_type_size (field
->type
, &align
);
1618 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
1619 align
= class->packing_size
? MIN (class->packing_size
, align
): align
;
1620 /* if the field has managed references, we need to force-align it
1623 if (MONO_TYPE_IS_REFERENCE (ftype
) || IS_GC_REFERENCE (ftype
) || ((MONO_TYPE_ISSTRUCT (ftype
) && mono_class_has_references (mono_class_from_mono_type (ftype
)))))
1624 align
= MAX (align
, sizeof (gpointer
));
1626 class->min_align
= MAX (align
, class->min_align
);
1627 field
->offset
= real_size
;
1628 field
->offset
+= align
- 1;
1629 field
->offset
&= ~(align
- 1);
1630 real_size
= field
->offset
+ size
;
1633 class->instance_size
= MAX (real_size
, class->instance_size
);
1635 if (class->instance_size
& (class->min_align
- 1)) {
1636 class->instance_size
+= class->min_align
- 1;
1637 class->instance_size
&= ~(class->min_align
- 1);
1641 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
:
1643 for (i
= 0; i
< top
; i
++) {
1648 field
= &class->fields
[i
];
1651 * There must be info about all the fields in a type if it
1652 * uses explicit layout.
1655 if (mono_field_is_deleted (field
))
1657 if (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
1660 size
= mono_type_size (field
->type
, &align
);
1661 class->min_align
= MAX (align
, class->min_align
);
1664 * When we get here, field->offset is already set by the
1665 * loader (for either runtime fields or fields loaded from metadata).
1666 * The offset is from the start of the object: this works for both
1667 * classes and valuetypes.
1669 field
->offset
+= sizeof (MonoObject
);
1670 ftype
= mono_type_get_underlying_type (field
->type
);
1671 ftype
= mono_type_get_basic_type_from_generic (ftype
);
1672 if (MONO_TYPE_IS_REFERENCE (ftype
) || ((MONO_TYPE_ISSTRUCT (ftype
) && mono_class_has_references (mono_class_from_mono_type (ftype
))))) {
1673 if (field
->offset
% sizeof (gpointer
)) {
1674 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
1681 real_size
= MAX (real_size
, size
+ field
->offset
);
1683 class->instance_size
= MAX (real_size
, class->instance_size
);
1687 if (layout
!= TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) {
1689 * For small structs, set min_align to at least the struct size to improve
1690 * performance, and since the JIT memset/memcpy code assumes this and generates
1691 * unaligned accesses otherwise. See #78990 for a testcase.
1693 if (class->instance_size
<= sizeof (MonoObject
) + sizeof (gpointer
))
1694 class->min_align
= MAX (class->min_align
, class->instance_size
- sizeof (MonoObject
));
1697 class->size_inited
= 1;
1700 * Compute static field layout and size
1702 for (i
= 0; i
< top
; i
++){
1706 field
= &class->fields
[i
];
1708 if (!(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
) || field
->type
->attrs
& FIELD_ATTRIBUTE_LITERAL
)
1710 if (mono_field_is_deleted (field
))
1713 if (mono_type_has_exceptions (field
->type
)) {
1714 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
1718 size
= mono_type_size (field
->type
, &align
);
1719 field
->offset
= class->sizes
.class_size
;
1720 field
->offset
+= align
- 1;
1721 field
->offset
&= ~(align
- 1);
1722 class->sizes
.class_size
= field
->offset
+ size
;
1727 create_array_method (MonoClass
*class, const char *name
, MonoMethodSignature
*sig
)
1731 method
= (MonoMethod
*) mono_image_alloc0 (class->image
, sizeof (MonoMethodPInvoke
));
1732 method
->klass
= class;
1733 method
->flags
= METHOD_ATTRIBUTE_PUBLIC
;
1734 method
->iflags
= METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
;
1735 method
->signature
= sig
;
1736 method
->name
= name
;
1739 if (name
[0] == '.') {
1740 method
->flags
|= METHOD_ATTRIBUTE_RT_SPECIAL_NAME
| METHOD_ATTRIBUTE_SPECIAL_NAME
;
1742 method
->iflags
|= METHOD_IMPL_ATTRIBUTE_RUNTIME
;
1748 * mono_class_setup_methods:
1751 * Initializes the 'methods' array in the klass.
1752 * Calling this method should be avoided if possible since it allocates a lot
1753 * of long-living MonoMethod structures.
1754 * Methods belonging to an interface are assigned a sequential slot starting
1757 * On failure this function sets class->exception_type
1760 mono_class_setup_methods (MonoClass
*class)
1763 MonoMethod
**methods
;
1768 mono_loader_lock ();
1770 if (class->methods
) {
1771 mono_loader_unlock ();
1775 if (class->generic_class
) {
1777 MonoClass
*gklass
= class->generic_class
->container_class
;
1779 mono_class_init (gklass
);
1780 if (!gklass
->exception_type
)
1781 mono_class_setup_methods (gklass
);
1782 if (gklass
->exception_type
) {
1783 /*FIXME make exception_data less opaque so it's possible to dup it here*/
1784 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Generic type definition failed to load"));
1785 mono_loader_unlock ();
1789 /* The + 1 makes this always non-NULL to pass the check in mono_class_setup_methods () */
1790 class->method
.count
= gklass
->method
.count
;
1791 methods
= g_new0 (MonoMethod
*, class->method
.count
+ 1);
1793 for (i
= 0; i
< class->method
.count
; i
++) {
1794 methods
[i
] = mono_class_inflate_generic_method_full_checked (
1795 gklass
->methods
[i
], class, mono_class_get_context (class), &error
);
1796 if (!mono_error_ok (&error
)) {
1797 char *method
= mono_method_full_name (gklass
->methods
[i
], TRUE
);
1798 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup_printf ("Could not inflate method %s due to %s", method
, mono_error_get_message (&error
)));
1801 mono_error_cleanup (&error
);
1802 mono_loader_unlock ();
1806 } else if (class->rank
) {
1808 MonoMethod
*amethod
;
1809 MonoMethodSignature
*sig
;
1810 int count_generic
= 0, first_generic
= 0;
1813 class->method
.count
= 3 + (class->rank
> 1? 2: 1);
1815 mono_class_setup_interfaces (class, &error
);
1816 g_assert (mono_error_ok (&error
)); /*FIXME can this fail for array types?*/
1818 if (class->interface_count
) {
1819 count_generic
= generic_array_methods (class);
1820 first_generic
= class->method
.count
;
1821 class->method
.count
+= class->interface_count
* count_generic
;
1824 methods
= mono_image_alloc0 (class->image
, sizeof (MonoMethod
*) * class->method
.count
);
1826 sig
= mono_metadata_signature_alloc (class->image
, class->rank
);
1827 sig
->ret
= &mono_defaults
.void_class
->byval_arg
;
1828 sig
->pinvoke
= TRUE
;
1829 sig
->hasthis
= TRUE
;
1830 for (i
= 0; i
< class->rank
; ++i
)
1831 sig
->params
[i
] = &mono_defaults
.int32_class
->byval_arg
;
1833 amethod
= create_array_method (class, ".ctor", sig
);
1834 methods
[method_num
++] = amethod
;
1835 if (class->rank
> 1) {
1836 sig
= mono_metadata_signature_alloc (class->image
, class->rank
* 2);
1837 sig
->ret
= &mono_defaults
.void_class
->byval_arg
;
1838 sig
->pinvoke
= TRUE
;
1839 sig
->hasthis
= TRUE
;
1840 for (i
= 0; i
< class->rank
* 2; ++i
)
1841 sig
->params
[i
] = &mono_defaults
.int32_class
->byval_arg
;
1843 amethod
= create_array_method (class, ".ctor", sig
);
1844 methods
[method_num
++] = amethod
;
1846 /* element Get (idx11, [idx2, ...]) */
1847 sig
= mono_metadata_signature_alloc (class->image
, class->rank
);
1848 sig
->ret
= &class->element_class
->byval_arg
;
1849 sig
->pinvoke
= TRUE
;
1850 sig
->hasthis
= TRUE
;
1851 for (i
= 0; i
< class->rank
; ++i
)
1852 sig
->params
[i
] = &mono_defaults
.int32_class
->byval_arg
;
1853 amethod
= create_array_method (class, "Get", sig
);
1854 methods
[method_num
++] = amethod
;
1855 /* element& Address (idx11, [idx2, ...]) */
1856 sig
= mono_metadata_signature_alloc (class->image
, class->rank
);
1857 sig
->ret
= &class->element_class
->this_arg
;
1858 sig
->pinvoke
= TRUE
;
1859 sig
->hasthis
= TRUE
;
1860 for (i
= 0; i
< class->rank
; ++i
)
1861 sig
->params
[i
] = &mono_defaults
.int32_class
->byval_arg
;
1862 amethod
= create_array_method (class, "Address", sig
);
1863 methods
[method_num
++] = amethod
;
1864 /* void Set (idx11, [idx2, ...], element) */
1865 sig
= mono_metadata_signature_alloc (class->image
, class->rank
+ 1);
1866 sig
->ret
= &mono_defaults
.void_class
->byval_arg
;
1867 sig
->pinvoke
= TRUE
;
1868 sig
->hasthis
= TRUE
;
1869 for (i
= 0; i
< class->rank
; ++i
)
1870 sig
->params
[i
] = &mono_defaults
.int32_class
->byval_arg
;
1871 sig
->params
[i
] = &class->element_class
->byval_arg
;
1872 amethod
= create_array_method (class, "Set", sig
);
1873 methods
[method_num
++] = amethod
;
1875 for (i
= 0; i
< class->interface_count
; i
++)
1876 setup_generic_array_ifaces (class, class->interfaces
[i
], methods
, first_generic
+ i
* count_generic
);
1878 methods
= mono_image_alloc (class->image
, sizeof (MonoMethod
*) * class->method
.count
);
1879 for (i
= 0; i
< class->method
.count
; ++i
) {
1880 int idx
= mono_metadata_translate_token_index (class->image
, MONO_TABLE_METHOD
, class->method
.first
+ i
+ 1);
1881 methods
[i
] = mono_get_method (class->image
, MONO_TOKEN_METHOD_DEF
| idx
, class);
1885 if (MONO_CLASS_IS_INTERFACE (class)) {
1887 /*Only assign slots to virtual methods as interfaces are allowed to have static methods.*/
1888 for (i
= 0; i
< class->method
.count
; ++i
) {
1889 if (methods
[i
]->flags
& METHOD_ATTRIBUTE_VIRTUAL
)
1890 methods
[i
]->slot
= slot
++;
1894 /* Needed because of the double-checking locking pattern */
1895 mono_memory_barrier ();
1897 class->methods
= methods
;
1899 if (mono_debugger_class_loaded_methods_func
)
1900 mono_debugger_class_loaded_methods_func (class);
1902 mono_loader_unlock ();
1906 * mono_class_get_method_by_index:
1908 * Returns class->methods [index], initializing class->methods if neccesary.
1910 * LOCKING: Acquires the loader lock.
1913 mono_class_get_method_by_index (MonoClass
*class, int index
)
1915 /* Avoid calling setup_methods () if possible */
1916 if (class->generic_class
&& !class->methods
) {
1917 MonoClass
*gklass
= class->generic_class
->container_class
;
1920 m
= mono_class_inflate_generic_method_full (
1921 gklass
->methods
[index
], class, mono_class_get_context (class));
1923 * If setup_methods () is called later for this class, no duplicates are created,
1924 * since inflate_generic_method guarantees that only one instance of a method
1925 * is created for each context.
1928 mono_class_setup_methods (class);
1929 g_assert (m == class->methods [index]);
1933 mono_class_setup_methods (class);
1934 g_assert (!class->exception_type
); /*FIXME do proper error handling*/
1935 g_assert (index
>= 0 && index
< class->method
.count
);
1936 return class->methods
[index
];
1941 * mono_class_get_inflated_method:
1943 * Given an inflated class CLASS and a method METHOD which should be a method of
1944 * CLASS's generic definition, return the inflated method corresponding to METHOD.
1947 mono_class_get_inflated_method (MonoClass
*class, MonoMethod
*method
)
1949 MonoClass
*gklass
= class->generic_class
->container_class
;
1952 g_assert (method
->klass
== gklass
);
1954 mono_class_setup_methods (gklass
);
1955 g_assert (!gklass
->exception_type
); /*FIXME do proper error handling*/
1957 for (i
= 0; i
< gklass
->method
.count
; ++i
) {
1958 if (gklass
->methods
[i
] == method
) {
1960 return class->methods
[i
];
1962 return mono_class_inflate_generic_method_full (gklass
->methods
[i
], class, mono_class_get_context (class));
1970 * mono_class_get_vtable_entry:
1972 * Returns class->vtable [offset], computing it if neccesary.
1973 * LOCKING: Acquires the loader lock.
1976 mono_class_get_vtable_entry (MonoClass
*class, int offset
)
1980 if (class->rank
== 1) {
1982 * szarrays do not overwrite any methods of Array, so we can avoid
1983 * initializing their vtables in some cases.
1985 mono_class_setup_vtable (class->parent
);
1986 if (offset
< class->parent
->vtable_size
)
1987 return class->parent
->vtable
[offset
];
1990 if (class->generic_class
) {
1991 MonoClass
*gklass
= class->generic_class
->container_class
;
1992 mono_class_setup_vtable (gklass
);
1993 m
= gklass
->vtable
[offset
];
1995 m
= mono_class_inflate_generic_method_full (m
, class, mono_class_get_context (class));
1997 mono_class_setup_vtable (class);
1998 m
= class->vtable
[offset
];
2005 * mono_class_get_vtable_size:
2007 * Return the vtable size for KLASS.
2010 mono_class_get_vtable_size (MonoClass
*klass
)
2012 mono_class_setup_vtable (klass
);
2014 return klass
->vtable_size
;
2017 /*This method can fail the class.*/
2019 mono_class_setup_properties (MonoClass
*class)
2021 guint startm
, endm
, i
, j
;
2022 guint32 cols
[MONO_PROPERTY_SIZE
];
2023 MonoTableInfo
*msemt
= &class->image
->tables
[MONO_TABLE_METHODSEMANTICS
];
2024 MonoProperty
*properties
;
2027 if (class->ext
&& class->ext
->properties
)
2030 mono_loader_lock ();
2032 if (class->ext
&& class->ext
->properties
) {
2033 mono_loader_unlock ();
2037 mono_class_alloc_ext (class);
2039 if (class->generic_class
) {
2040 MonoClass
*gklass
= class->generic_class
->container_class
;
2042 mono_class_init (gklass
);
2043 mono_class_setup_properties (gklass
);
2044 if (gklass
->exception_type
) {
2045 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Generic type definition failed to load"));
2046 mono_loader_unlock ();
2050 class->ext
->property
= gklass
->ext
->property
;
2052 properties
= g_new0 (MonoProperty
, class->ext
->property
.count
+ 1);
2054 for (i
= 0; i
< class->ext
->property
.count
; i
++) {
2055 MonoProperty
*prop
= &properties
[i
];
2057 *prop
= gklass
->ext
->properties
[i
];
2060 prop
->get
= mono_class_inflate_generic_method_full (
2061 prop
->get
, class, mono_class_get_context (class));
2063 prop
->set
= mono_class_inflate_generic_method_full (
2064 prop
->set
, class, mono_class_get_context (class));
2066 prop
->parent
= class;
2069 int first
= mono_metadata_properties_from_typedef (class->image
, mono_metadata_token_index (class->type_token
) - 1, &last
);
2070 int count
= last
- first
;
2073 mono_class_setup_methods (class);
2074 if (class->exception_type
) {
2075 mono_loader_unlock ();
2080 class->ext
->property
.first
= first
;
2081 class->ext
->property
.count
= count
;
2082 properties
= mono_image_alloc0 (class->image
, sizeof (MonoProperty
) * count
);
2083 for (i
= first
; i
< last
; ++i
) {
2084 mono_metadata_decode_table_row (class->image
, MONO_TABLE_PROPERTY
, i
, cols
, MONO_PROPERTY_SIZE
);
2085 properties
[i
- first
].parent
= class;
2086 properties
[i
- first
].attrs
= cols
[MONO_PROPERTY_FLAGS
];
2087 properties
[i
- first
].name
= mono_metadata_string_heap (class->image
, cols
[MONO_PROPERTY_NAME
]);
2089 startm
= mono_metadata_methods_from_property (class->image
, i
, &endm
);
2090 for (j
= startm
; j
< endm
; ++j
) {
2093 mono_metadata_decode_row (msemt
, j
, cols
, MONO_METHOD_SEMA_SIZE
);
2095 if (class->image
->uncompressed_metadata
)
2096 /* It seems like the MONO_METHOD_SEMA_METHOD column needs no remapping */
2097 method
= mono_get_method (class->image
, MONO_TOKEN_METHOD_DEF
| cols
[MONO_METHOD_SEMA_METHOD
], class);
2099 method
= class->methods
[cols
[MONO_METHOD_SEMA_METHOD
] - 1 - class->method
.first
];
2101 switch (cols
[MONO_METHOD_SEMA_SEMANTICS
]) {
2102 case METHOD_SEMANTIC_SETTER
:
2103 properties
[i
- first
].set
= method
;
2105 case METHOD_SEMANTIC_GETTER
:
2106 properties
[i
- first
].get
= method
;
2114 /*Flush any pending writes as we do double checked locking on class->properties */
2115 mono_memory_barrier ();
2117 /* Leave this assignment as the last op in the function */
2118 class->ext
->properties
= properties
;
2120 mono_loader_unlock ();
2124 inflate_method_listz (MonoMethod
**methods
, MonoClass
*class, MonoGenericContext
*context
)
2126 MonoMethod
**om
, **retval
;
2129 for (om
= methods
, count
= 0; *om
; ++om
, ++count
)
2132 retval
= g_new0 (MonoMethod
*, count
+ 1);
2134 for (om
= methods
, count
= 0; *om
; ++om
, ++count
)
2135 retval
[count
] = mono_class_inflate_generic_method_full (*om
, class, context
);
2140 /*This method can fail the class.*/
2142 mono_class_setup_events (MonoClass
*class)
2145 guint startm
, endm
, i
, j
;
2146 guint32 cols
[MONO_EVENT_SIZE
];
2147 MonoTableInfo
*msemt
= &class->image
->tables
[MONO_TABLE_METHODSEMANTICS
];
2151 if (class->ext
&& class->ext
->events
)
2154 mono_loader_lock ();
2156 if (class->ext
&& class->ext
->events
) {
2157 mono_loader_unlock ();
2161 mono_class_alloc_ext (class);
2163 if (class->generic_class
) {
2164 MonoClass
*gklass
= class->generic_class
->container_class
;
2165 MonoGenericContext
*context
;
2167 mono_class_setup_events (gklass
);
2168 if (gklass
->exception_type
) {
2169 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Generic type definition failed to load"));
2170 mono_loader_unlock ();
2174 class->ext
->event
= gklass
->ext
->event
;
2175 class->ext
->events
= g_new0 (MonoEvent
, class->ext
->event
.count
);
2177 if (class->ext
->event
.count
)
2178 context
= mono_class_get_context (class);
2180 for (i
= 0; i
< class->ext
->event
.count
; i
++) {
2181 MonoEvent
*event
= &class->ext
->events
[i
];
2182 MonoEvent
*gevent
= &gklass
->ext
->events
[i
];
2184 event
->parent
= class;
2185 event
->name
= gevent
->name
;
2186 event
->add
= gevent
->add
? mono_class_inflate_generic_method_full (gevent
->add
, class, context
) : NULL
;
2187 event
->remove
= gevent
->remove
? mono_class_inflate_generic_method_full (gevent
->remove
, class, context
) : NULL
;
2188 event
->raise
= gevent
->raise
? mono_class_inflate_generic_method_full (gevent
->raise
, class, context
) : NULL
;
2189 event
->other
= gevent
->other
? inflate_method_listz (gevent
->other
, class, context
) : NULL
;
2190 event
->attrs
= gevent
->attrs
;
2193 mono_loader_unlock ();
2197 first
= mono_metadata_events_from_typedef (class->image
, mono_metadata_token_index (class->type_token
) - 1, &last
);
2198 count
= last
- first
;
2201 mono_class_setup_methods (class);
2202 if (class->exception_type
) {
2203 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Generic type definition failed to load"));
2204 mono_loader_unlock ();
2208 class->ext
->event
.first
= first
;
2209 class->ext
->event
.count
= count
;
2210 events
= mono_image_alloc0 (class->image
, sizeof (MonoEvent
) * class->ext
->event
.count
);
2211 for (i
= first
; i
< last
; ++i
) {
2212 MonoEvent
*event
= &events
[i
- first
];
2214 mono_metadata_decode_table_row (class->image
, MONO_TABLE_EVENT
, i
, cols
, MONO_EVENT_SIZE
);
2215 event
->parent
= class;
2216 event
->attrs
= cols
[MONO_EVENT_FLAGS
];
2217 event
->name
= mono_metadata_string_heap (class->image
, cols
[MONO_EVENT_NAME
]);
2219 startm
= mono_metadata_methods_from_event (class->image
, i
, &endm
);
2220 for (j
= startm
; j
< endm
; ++j
) {
2223 mono_metadata_decode_row (msemt
, j
, cols
, MONO_METHOD_SEMA_SIZE
);
2225 if (class->image
->uncompressed_metadata
)
2226 /* It seems like the MONO_METHOD_SEMA_METHOD column needs no remapping */
2227 method
= mono_get_method (class->image
, MONO_TOKEN_METHOD_DEF
| cols
[MONO_METHOD_SEMA_METHOD
], class);
2229 method
= class->methods
[cols
[MONO_METHOD_SEMA_METHOD
] - 1 - class->method
.first
];
2231 switch (cols
[MONO_METHOD_SEMA_SEMANTICS
]) {
2232 case METHOD_SEMANTIC_ADD_ON
:
2233 event
->add
= method
;
2235 case METHOD_SEMANTIC_REMOVE_ON
:
2236 event
->remove
= method
;
2238 case METHOD_SEMANTIC_FIRE
:
2239 event
->raise
= method
;
2241 case METHOD_SEMANTIC_OTHER
: {
2244 if (event
->other
== NULL
) {
2245 event
->other
= g_new0 (MonoMethod
*, 2);
2247 while (event
->other
[n
])
2249 event
->other
= g_realloc (event
->other
, (n
+ 2) * sizeof (MonoMethod
*));
2251 event
->other
[n
] = method
;
2252 /* NULL terminated */
2253 event
->other
[n
+ 1] = NULL
;
2261 /*Flush any pending writes as we do double checked locking on class->properties */
2262 mono_memory_barrier ();
2264 /* Leave this assignment as the last op in the function */
2265 class->ext
->events
= events
;
2267 mono_loader_unlock ();
2271 * Global pool of interface IDs, represented as a bitset.
2272 * LOCKING: this is supposed to be accessed with the loader lock held.
2274 static MonoBitSet
*global_interface_bitset
= NULL
;
2277 * mono_unload_interface_ids:
2278 * @bitset: bit set of interface IDs
2280 * When an image is unloaded, the interface IDs associated with
2281 * the image are put back in the global pool of IDs so the numbers
2285 mono_unload_interface_ids (MonoBitSet
*bitset
)
2287 mono_loader_lock ();
2288 mono_bitset_sub (global_interface_bitset
, bitset
);
2289 mono_loader_unlock ();
2293 * mono_get_unique_iid:
2296 * Assign a unique integer ID to the interface represented by @class.
2297 * The ID will positive and as small as possible.
2298 * LOCKING: this is supposed to be called with the loader lock held.
2299 * Returns: the new ID.
2302 mono_get_unique_iid (MonoClass
*class)
2306 g_assert (MONO_CLASS_IS_INTERFACE (class));
2308 if (!global_interface_bitset
) {
2309 global_interface_bitset
= mono_bitset_new (128, 0);
2312 iid
= mono_bitset_find_first_unset (global_interface_bitset
, -1);
2314 int old_size
= mono_bitset_size (global_interface_bitset
);
2315 MonoBitSet
*new_set
= mono_bitset_clone (global_interface_bitset
, old_size
* 2);
2316 mono_bitset_free (global_interface_bitset
);
2317 global_interface_bitset
= new_set
;
2320 mono_bitset_set (global_interface_bitset
, iid
);
2321 /* set the bit also in the per-image set */
2322 if (class->image
->interface_bitset
) {
2323 if (iid
>= mono_bitset_size (class->image
->interface_bitset
)) {
2324 MonoBitSet
*new_set
= mono_bitset_clone (class->image
->interface_bitset
, iid
+ 1);
2325 mono_bitset_free (class->image
->interface_bitset
);
2326 class->image
->interface_bitset
= new_set
;
2329 class->image
->interface_bitset
= mono_bitset_new (iid
+ 1, 0);
2331 mono_bitset_set (class->image
->interface_bitset
, iid
);
2333 if (mono_print_vtable
) {
2335 char *type_name
= mono_type_full_name (&class->byval_arg
);
2336 if (class->generic_class
&& !class->generic_class
->context
.class_inst
->is_open
) {
2337 generic_id
= class->generic_class
->context
.class_inst
->id
;
2338 g_assert (generic_id
!= 0);
2342 printf ("Interface: assigned id %d to %s|%s|%d\n", iid
, class->image
->name
, type_name
, generic_id
);
2346 g_assert (iid
<= 65535);
2351 collect_implemented_interfaces_aux (MonoClass
*klass
, GPtrArray
**res
, MonoError
*error
)
2356 mono_class_setup_interfaces (klass
, error
);
2357 if (!mono_error_ok (error
))
2360 for (i
= 0; i
< klass
->interface_count
; i
++) {
2361 ic
= klass
->interfaces
[i
];
2364 *res
= g_ptr_array_new ();
2365 g_ptr_array_add (*res
, ic
);
2366 mono_class_init (ic
);
2368 collect_implemented_interfaces_aux (ic
, res
, error
);
2369 if (!mono_error_ok (error
))
2375 mono_class_get_implemented_interfaces (MonoClass
*klass
, MonoError
*error
)
2377 GPtrArray
*res
= NULL
;
2379 collect_implemented_interfaces_aux (klass
, &res
, error
);
2380 if (!mono_error_ok (error
)) {
2382 g_ptr_array_free (res
, TRUE
);
2389 compare_interface_ids (const void *p_key
, const void *p_element
) {
2390 const MonoClass
*key
= p_key
;
2391 const MonoClass
*element
= *(MonoClass
**) p_element
;
2393 return (key
->interface_id
- element
->interface_id
);
2396 /*FIXME verify all callers if they should switch to mono_class_interface_offset_with_variance*/
2398 mono_class_interface_offset (MonoClass
*klass
, MonoClass
*itf
) {
2399 MonoClass
**result
= bsearch (
2401 klass
->interfaces_packed
,
2402 klass
->interface_offsets_count
,
2403 sizeof (MonoClass
*),
2404 compare_interface_ids
);
2406 return klass
->interface_offsets_packed
[result
- (klass
->interfaces_packed
)];
2413 * mono_class_interface_offset_with_variance:
2415 * Return the interface offset of @itf in @klass. Sets @non_exact_match to TRUE if the match required variance check
2416 * If @itf is an interface with generic variant arguments, try to find the compatible one.
2418 * Note that this function is responsible for resolving ambiguities. Right now we use whatever ordering interfaces_packed gives us.
2420 * FIXME figure out MS disambiguation rules and fix this function.
2423 mono_class_interface_offset_with_variance (MonoClass
*klass
, MonoClass
*itf
, gboolean
*non_exact_match
) {
2424 int i
= mono_class_interface_offset (klass
, itf
);
2425 *non_exact_match
= FALSE
;
2429 if (!mono_class_has_variant_generic_params (itf
))
2432 for (i
= 0; i
< klass
->interface_offsets_count
; i
++) {
2433 if (mono_class_is_variant_compatible (itf
, klass
->interfaces_packed
[i
])) {
2434 *non_exact_match
= TRUE
;
2435 return klass
->interface_offsets_packed
[i
];
2443 print_implemented_interfaces (MonoClass
*klass
) {
2445 GPtrArray
*ifaces
= NULL
;
2447 int ancestor_level
= 0;
2449 printf ("Packed interface table for class %s has size %d\n", klass
->name
, klass
->interface_offsets_count
);
2450 for (i
= 0; i
< klass
->interface_offsets_count
; i
++)
2451 printf (" [%03d][UUID %03d][SLOT %03d][SIZE %03d] interface %s.%s\n", i
,
2452 klass
->interfaces_packed
[i
]->interface_id
,
2453 klass
->interface_offsets_packed
[i
],
2454 klass
->interfaces_packed
[i
]->method
.count
,
2455 klass
->interfaces_packed
[i
]->name_space
,
2456 klass
->interfaces_packed
[i
]->name
);
2457 printf ("Interface flags: ");
2458 for (i
= 0; i
<= klass
->max_interface_id
; i
++)
2459 if (MONO_CLASS_IMPLEMENTS_INTERFACE (klass
, i
))
2460 printf ("(%d,T)", i
);
2462 printf ("(%d,F)", i
);
2464 printf ("Dump interface flags:");
2465 for (i
= 0; i
< ((((klass
->max_interface_id
+ 1) >> 3)) + (((klass
->max_interface_id
+ 1) & 7)? 1 :0)); i
++)
2466 printf (" %02X", klass
->interface_bitmap
[i
]);
2468 while (klass
!= NULL
) {
2469 printf ("[LEVEL %d] Implemented interfaces by class %s:\n", ancestor_level
, klass
->name
);
2470 ifaces
= mono_class_get_implemented_interfaces (klass
, &error
);
2471 if (!mono_error_ok (&error
)) {
2472 printf (" Type failed due to %s\n", mono_error_get_message (&error
));
2473 mono_error_cleanup (&error
);
2474 } else if (ifaces
) {
2475 for (i
= 0; i
< ifaces
->len
; i
++) {
2476 MonoClass
*ic
= g_ptr_array_index (ifaces
, i
);
2477 printf (" [UIID %d] interface %s\n", ic
->interface_id
, ic
->name
);
2478 printf (" [%03d][UUID %03d][SLOT %03d][SIZE %03d] interface %s.%s\n", i
,
2480 mono_class_interface_offset (klass
, ic
),
2485 g_ptr_array_free (ifaces
, TRUE
);
2488 klass
= klass
->parent
;
2493 inflate_class_one_arg (MonoClass
*gtype
, MonoClass
*arg0
)
2496 args
[0] = &arg0
->byval_arg
;
2498 return mono_class_bind_generic_parameters (gtype
, 1, args
, FALSE
);
2502 array_class_get_if_rank (MonoClass
*class, guint rank
)
2504 return rank
? mono_array_class_get (class, rank
) : class;
2508 fill_valuetype_array_derived_types (MonoClass
**valuetype_types
, MonoClass
*eclass
, int rank
)
2510 valuetype_types
[0] = eclass
;
2511 if (eclass
== mono_defaults
.int16_class
)
2512 valuetype_types
[1] = mono_defaults
.uint16_class
;
2513 else if (eclass
== mono_defaults
.uint16_class
)
2514 valuetype_types
[1] = mono_defaults
.int16_class
;
2515 else if (eclass
== mono_defaults
.int32_class
)
2516 valuetype_types
[1] = mono_defaults
.uint32_class
;
2517 else if (eclass
== mono_defaults
.uint32_class
)
2518 valuetype_types
[1] = mono_defaults
.int32_class
;
2519 else if (eclass
== mono_defaults
.int64_class
)
2520 valuetype_types
[1] = mono_defaults
.uint64_class
;
2521 else if (eclass
== mono_defaults
.uint64_class
)
2522 valuetype_types
[1] = mono_defaults
.int64_class
;
2523 else if (eclass
== mono_defaults
.byte_class
)
2524 valuetype_types
[1] = mono_defaults
.sbyte_class
;
2525 else if (eclass
== mono_defaults
.sbyte_class
)
2526 valuetype_types
[1] = mono_defaults
.byte_class
;
2527 else if (eclass
->enumtype
&& mono_class_enum_basetype (eclass
))
2528 valuetype_types
[1] = mono_class_from_mono_type (mono_class_enum_basetype (eclass
));
2531 /* this won't be needed once bug #325495 is completely fixed
2532 * though we'll need something similar to know which interfaces to allow
2533 * in arrays when they'll be lazyly created
2535 * FIXME: System.Array/InternalEnumerator don't need all this interface fabrication machinery.
2536 * MS returns diferrent types based on which instance is called. For example:
2537 * object obj = new byte[10][];
2538 * Type a = ((IEnumerable<byte[]>)obj).GetEnumerator ().GetType ();
2539 * Type b = ((IEnumerable<IList<byte>>)obj).GetEnumerator ().GetType ();
2542 * Fixing this should kill quite some code, save some bits and improve compatibility.
2545 get_implicit_generic_array_interfaces (MonoClass
*class, int *num
, int *is_enumerator
)
2547 MonoClass
*eclass
= class->element_class
;
2548 static MonoClass
* generic_icollection_class
= NULL
;
2549 static MonoClass
* generic_ienumerable_class
= NULL
;
2550 static MonoClass
* generic_ienumerator_class
= NULL
;
2551 MonoClass
*valuetype_types
[2] = { NULL
, NULL
};
2552 MonoClass
**interfaces
= NULL
;
2553 int i
, interface_count
, real_count
, original_rank
;
2555 gboolean internal_enumerator
;
2556 gboolean eclass_is_valuetype
;
2558 if (!mono_defaults
.generic_ilist_class
) {
2562 internal_enumerator
= FALSE
;
2563 eclass_is_valuetype
= FALSE
;
2564 original_rank
= eclass
->rank
;
2565 if (class->byval_arg
.type
!= MONO_TYPE_SZARRAY
) {
2566 if (class->generic_class
&& class->nested_in
== mono_defaults
.array_class
&& strcmp (class->name
, "InternalEnumerator`1") == 0) {
2568 * For a Enumerator<T[]> we need to get the list of interfaces for T.
2570 eclass
= mono_class_from_mono_type (class->generic_class
->context
.class_inst
->type_argv
[0]);
2571 original_rank
= eclass
->rank
;
2572 eclass
= eclass
->element_class
;
2573 internal_enumerator
= TRUE
;
2574 *is_enumerator
= TRUE
;
2582 * with this non-lazy impl we can't implement all the interfaces so we do just the minimal stuff
2583 * for deep levels of arrays of arrays (string[][] has all the interfaces, string[][][] doesn't)
2585 all_interfaces
= eclass
->rank
&& eclass
->element_class
->rank
? FALSE
: TRUE
;
2587 if (!generic_icollection_class
) {
2588 generic_icollection_class
= mono_class_from_name (mono_defaults
.corlib
,
2589 "System.Collections.Generic", "ICollection`1");
2590 generic_ienumerable_class
= mono_class_from_name (mono_defaults
.corlib
,
2591 "System.Collections.Generic", "IEnumerable`1");
2592 generic_ienumerator_class
= mono_class_from_name (mono_defaults
.corlib
,
2593 "System.Collections.Generic", "IEnumerator`1");
2596 mono_class_init (eclass
);
2599 * Arrays in 2.0 need to implement a number of generic interfaces
2600 * (IList`1, ICollection`1, IEnumerable`1 for a number of types depending
2601 * on the element class). We collect the types needed to build the
2602 * instantiations in interfaces at intervals of 3, because 3 are
2603 * the generic interfaces needed to implement.
2605 if (eclass
->valuetype
) {
2606 fill_valuetype_array_derived_types (valuetype_types
, eclass
, original_rank
);
2608 /* IList, ICollection, IEnumerable */
2609 real_count
= interface_count
= valuetype_types
[1] ? 6 : 3;
2610 if (internal_enumerator
) {
2612 if (valuetype_types
[1])
2616 interfaces
= g_malloc0 (sizeof (MonoClass
*) * real_count
);
2617 interfaces
[0] = valuetype_types
[0];
2618 if (valuetype_types
[1])
2619 interfaces
[3] = valuetype_types
[1];
2621 eclass_is_valuetype
= TRUE
;
2624 int idepth
= eclass
->idepth
;
2625 if (!internal_enumerator
)
2628 // FIXME: This doesn't seem to work/required for generic params
2629 if (!(eclass
->this_arg
.type
== MONO_TYPE_VAR
|| eclass
->this_arg
.type
== MONO_TYPE_MVAR
|| (eclass
->image
->dynamic
&& !eclass
->wastypebuilder
)))
2630 mono_class_setup_interface_offsets (eclass
);
2632 interface_count
= all_interfaces
? eclass
->interface_offsets_count
: eclass
->interface_count
;
2633 /* we add object for interfaces and the supertypes for the other
2634 * types. The last of the supertypes is the element class itself which we
2635 * already created the explicit interfaces for (so we include it for IEnumerator
2636 * and exclude it for arrays).
2638 if (MONO_CLASS_IS_INTERFACE (eclass
))
2641 interface_count
+= idepth
;
2642 if (eclass
->rank
&& eclass
->element_class
->valuetype
) {
2643 fill_valuetype_array_derived_types (valuetype_types
, eclass
->element_class
, original_rank
);
2644 if (valuetype_types
[1])
2647 /* IList, ICollection, IEnumerable */
2648 interface_count
*= 3;
2649 real_count
= interface_count
;
2650 if (internal_enumerator
) {
2651 real_count
+= (MONO_CLASS_IS_INTERFACE (eclass
) ? 1 : idepth
) + eclass
->interface_offsets_count
;
2652 if (valuetype_types
[1])
2655 interfaces
= g_malloc0 (sizeof (MonoClass
*) * real_count
);
2656 if (MONO_CLASS_IS_INTERFACE (eclass
)) {
2657 interfaces
[0] = mono_defaults
.object_class
;
2661 for (i
= 0; i
< idepth
; i
++) {
2662 mono_class_init (eclass
->supertypes
[i
]);
2663 interfaces
[j
] = eclass
->supertypes
[i
];
2667 if (all_interfaces
) {
2668 for (i
= 0; i
< eclass
->interface_offsets_count
; i
++) {
2669 interfaces
[j
] = eclass
->interfaces_packed
[i
];
2673 for (i
= 0; i
< eclass
->interface_count
; i
++) {
2674 interfaces
[j
] = eclass
->interfaces
[i
];
2678 if (valuetype_types
[1]) {
2679 interfaces
[j
] = array_class_get_if_rank (valuetype_types
[1], original_rank
);
2684 /* instantiate the generic interfaces */
2685 for (i
= 0; i
< interface_count
; i
+= 3) {
2686 MonoClass
*iface
= interfaces
[i
];
2688 interfaces
[i
+ 0] = inflate_class_one_arg (mono_defaults
.generic_ilist_class
, iface
);
2689 interfaces
[i
+ 1] = inflate_class_one_arg (generic_icollection_class
, iface
);
2690 interfaces
[i
+ 2] = inflate_class_one_arg (generic_ienumerable_class
, iface
);
2692 if (internal_enumerator
) {
2694 /* instantiate IEnumerator<iface> */
2695 for (i
= 0; i
< interface_count
; i
++) {
2696 interfaces
[i
] = inflate_class_one_arg (generic_ienumerator_class
, interfaces
[i
]);
2698 j
= interface_count
;
2699 if (!eclass_is_valuetype
) {
2700 if (MONO_CLASS_IS_INTERFACE (eclass
)) {
2701 interfaces
[j
] = inflate_class_one_arg (generic_ienumerator_class
, mono_defaults
.object_class
);
2704 for (i
= 0; i
< eclass
->idepth
; i
++) {
2705 interfaces
[j
] = inflate_class_one_arg (generic_ienumerator_class
, eclass
->supertypes
[i
]);
2709 for (i
= 0; i
< eclass
->interface_offsets_count
; i
++) {
2710 interfaces
[j
] = inflate_class_one_arg (generic_ienumerator_class
, eclass
->interfaces_packed
[i
]);
2714 interfaces
[j
++] = inflate_class_one_arg (generic_ienumerator_class
, array_class_get_if_rank (valuetype_types
[0], original_rank
));
2716 if (valuetype_types
[1])
2717 interfaces
[j
] = inflate_class_one_arg (generic_ienumerator_class
, array_class_get_if_rank (valuetype_types
[1], original_rank
));
2721 char *type_name
= mono_type_get_name_full (&class->byval_arg
, 0);
2722 for (i
= 0; i
< real_count
; ++i
) {
2723 char *name
= mono_type_get_name_full (&interfaces
[i
]->byval_arg
, 0);
2724 g_print ("%s implements %s\n", type_name
, name
);
2735 find_array_interface (MonoClass
*klass
, const char *name
)
2738 for (i
= 0; i
< klass
->interface_count
; ++i
) {
2739 if (strcmp (klass
->interfaces
[i
]->name
, name
) == 0)
2746 * Return the number of virtual methods.
2747 * Even for interfaces we can't simply return the number of methods as all CLR types are allowed to have static methods.
2748 * Return -1 on failure.
2749 * FIXME It would be nice if this information could be cached somewhere.
2752 count_virtual_methods (MonoClass
*class)
2756 class = mono_class_get_generic_type_definition (class); /*We can find this information by looking at the GTD*/
2758 if (class->methods
|| !MONO_CLASS_HAS_STATIC_METADATA (class)) {
2759 mono_class_setup_methods (class);
2760 if (class->exception_type
)
2763 for (i
= 0; i
< class->method
.count
; ++i
) {
2764 flags
= class->methods
[i
]->flags
;
2765 if (flags
& METHOD_ATTRIBUTE_VIRTUAL
)
2769 for (i
= 0; i
< class->method
.count
; ++i
) {
2770 flags
= mono_metadata_decode_table_row_col (class->image
, MONO_TABLE_METHOD
, class->method
.first
+ i
, MONO_METHOD_FLAGS
);
2772 if (flags
& METHOD_ATTRIBUTE_VIRTUAL
)
2780 * LOCKING: this is supposed to be called with the loader lock held.
2781 * Return -1 on failure and set exception_type
2784 setup_interface_offsets (MonoClass
*class, int cur_slot
)
2789 MonoClass
**interfaces_full
= NULL
;
2790 int *interface_offsets_full
= NULL
;
2792 int interface_offsets_count
;
2793 MonoClass
**array_interfaces
= NULL
;
2794 int num_array_interfaces
;
2795 int is_enumerator
= FALSE
;
2798 * get the implicit generic interfaces for either the arrays or for System.Array/InternalEnumerator<T>
2799 * implicit interfaces have the property that they are assigned the same slot in the
2800 * vtables for compatible interfaces
2802 array_interfaces
= get_implicit_generic_array_interfaces (class, &num_array_interfaces
, &is_enumerator
);
2804 /* compute maximum number of slots and maximum interface id */
2806 for (k
= class; k
; k
= k
->parent
) {
2807 for (i
= 0; i
< k
->interface_count
; i
++) {
2808 ic
= k
->interfaces
[i
];
2811 mono_class_init (ic
);
2813 if (max_iid
< ic
->interface_id
)
2814 max_iid
= ic
->interface_id
;
2816 ifaces
= mono_class_get_implemented_interfaces (k
, &error
);
2817 if (!mono_error_ok (&error
)) {
2818 char *name
= mono_type_get_full_name (k
);
2819 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup_printf ("Error getting the interfaces of %s due to %s", name
, mono_error_get_message (&error
)));
2821 mono_error_cleanup (&error
);
2825 for (i
= 0; i
< ifaces
->len
; ++i
) {
2826 ic
= g_ptr_array_index (ifaces
, i
);
2827 if (max_iid
< ic
->interface_id
)
2828 max_iid
= ic
->interface_id
;
2830 g_ptr_array_free (ifaces
, TRUE
);
2833 for (i
= 0; i
< num_array_interfaces
; ++i
) {
2834 ic
= array_interfaces
[i
];
2835 mono_class_init (ic
);
2836 if (max_iid
< ic
->interface_id
)
2837 max_iid
= ic
->interface_id
;
2840 if (MONO_CLASS_IS_INTERFACE (class)) {
2841 if (max_iid
< class->interface_id
)
2842 max_iid
= class->interface_id
;
2844 class->max_interface_id
= max_iid
;
2845 /* compute vtable offset for interfaces */
2846 interfaces_full
= g_malloc (sizeof (MonoClass
*) * (max_iid
+ 1));
2847 interface_offsets_full
= g_malloc (sizeof (int) * (max_iid
+ 1));
2849 for (i
= 0; i
<= max_iid
; i
++) {
2850 interfaces_full
[i
] = NULL
;
2851 interface_offsets_full
[i
] = -1;
2854 for (k
= class->parent
; k
; k
= k
->parent
) {
2855 ifaces
= mono_class_get_implemented_interfaces (k
, &error
);
2856 g_assert (mono_error_ok (&error
));/*FIXME we perform the same thing above, so not failing there but here is VERY wrong.*/
2859 for (i
= 0; i
< ifaces
->len
; ++i
) {
2861 ic
= g_ptr_array_index (ifaces
, i
);
2863 /*Force the sharing of interface offsets between parent and subtypes.*/
2864 io
= mono_class_interface_offset (k
, ic
);
2866 interfaces_full
[ic
->interface_id
] = ic
;
2867 interface_offsets_full
[ic
->interface_id
] = io
;
2869 g_ptr_array_free (ifaces
, TRUE
);
2874 ifaces
= mono_class_get_implemented_interfaces (class, &error
);
2875 if (!mono_error_ok (&error
)) {
2876 char *name
= mono_type_get_full_name (class);
2877 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup_printf ("Error getting the interfaces of %s due to %s", name
, mono_error_get_message (&error
)));
2879 mono_error_cleanup (&error
);
2881 } else if (ifaces
) {
2882 for (i
= 0; i
< ifaces
->len
; ++i
) {
2884 ic
= g_ptr_array_index (ifaces
, i
);
2885 if (interfaces_full
[ic
->interface_id
] != NULL
)
2887 interfaces_full
[ic
->interface_id
] = ic
;
2888 interface_offsets_full
[ic
->interface_id
] = cur_slot
;
2889 count
= count_virtual_methods (ic
);
2891 char *name
= mono_type_get_full_name (ic
);
2892 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup_printf ("Error calculating interface offset of %s", name
));
2898 g_ptr_array_free (ifaces
, TRUE
);
2901 if (MONO_CLASS_IS_INTERFACE (class)) {
2902 interfaces_full
[class->interface_id
] = class;
2903 interface_offsets_full
[class->interface_id
] = cur_slot
;
2906 if (num_array_interfaces
) {
2907 if (is_enumerator
) {
2908 int ienumerator_offset
;
2909 int ienumerator_idx
= find_array_interface (class, "IEnumerator`1");
2910 ienumerator_offset
= interface_offsets_full
[class->interfaces
[ienumerator_idx
]->interface_id
];
2911 for (i
= 0; i
< num_array_interfaces
; ++i
) {
2912 ic
= array_interfaces
[i
];
2913 interfaces_full
[ic
->interface_id
] = ic
;
2914 if (strcmp (ic
->name
, "IEnumerator`1") == 0)
2915 interface_offsets_full
[ic
->interface_id
] = ienumerator_offset
;
2917 g_assert_not_reached ();
2918 /*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);*/
2921 int ilist_offset
, icollection_offset
, ienumerable_offset
;
2922 int ilist_iface_idx
= find_array_interface (class, "IList`1");
2923 int icollection_iface_idx
= find_array_interface (class->interfaces
[ilist_iface_idx
], "ICollection`1");
2924 int ienumerable_iface_idx
= find_array_interface (class->interfaces
[ilist_iface_idx
], "IEnumerable`1");
2925 ilist_offset
= interface_offsets_full
[class->interfaces
[ilist_iface_idx
]->interface_id
];
2926 icollection_offset
= interface_offsets_full
[class->interfaces
[ilist_iface_idx
]->interfaces
[icollection_iface_idx
]->interface_id
];
2927 ienumerable_offset
= interface_offsets_full
[class->interfaces
[ilist_iface_idx
]->interfaces
[ienumerable_iface_idx
]->interface_id
];
2928 g_assert (ilist_offset
>= 0 && icollection_offset
>= 0 && ienumerable_offset
>= 0);
2929 for (i
= 0; i
< num_array_interfaces
; ++i
) {
2930 ic
= array_interfaces
[i
];
2931 interfaces_full
[ic
->interface_id
] = ic
;
2932 if (ic
->generic_class
->container_class
== mono_defaults
.generic_ilist_class
)
2933 interface_offsets_full
[ic
->interface_id
] = ilist_offset
;
2934 else if (strcmp (ic
->name
, "ICollection`1") == 0)
2935 interface_offsets_full
[ic
->interface_id
] = icollection_offset
;
2936 else if (strcmp (ic
->name
, "IEnumerable`1") == 0)
2937 interface_offsets_full
[ic
->interface_id
] = ienumerable_offset
;
2939 g_assert_not_reached ();
2940 /*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);*/
2945 for (interface_offsets_count
= 0, i
= 0; i
<= max_iid
; i
++) {
2946 if (interface_offsets_full
[i
] != -1) {
2947 interface_offsets_count
++;
2952 * We might get called twice: once from mono_class_init () then once from
2953 * mono_class_setup_vtable ().
2955 if (class->interfaces_packed
) {
2956 g_assert (class->interface_offsets_count
== interface_offsets_count
);
2958 class->interface_offsets_count
= interface_offsets_count
;
2959 class->interfaces_packed
= mono_image_alloc (class->image
, sizeof (MonoClass
*) * interface_offsets_count
);
2960 class->interface_offsets_packed
= mono_image_alloc (class->image
, sizeof (guint16
) * interface_offsets_count
);
2961 class->interface_bitmap
= mono_image_alloc0 (class->image
, (sizeof (guint8
) * ((max_iid
+ 1) >> 3)) + (((max_iid
+ 1) & 7)? 1 :0));
2962 for (interface_offsets_count
= 0, i
= 0; i
<= max_iid
; i
++) {
2963 if (interface_offsets_full
[i
] != -1) {
2964 class->interface_bitmap
[i
>> 3] |= (1 << (i
& 7));
2965 class->interfaces_packed
[interface_offsets_count
] = interfaces_full
[i
];
2966 class->interface_offsets_packed
[interface_offsets_count
] = interface_offsets_full
[i
];
2967 /*if (num_array_interfaces)
2968 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]);*/
2969 interface_offsets_count
++;
2974 g_free (interfaces_full
);
2975 g_free (interface_offsets_full
);
2976 g_free (array_interfaces
);
2978 //printf ("JUST DONE: ");
2979 //print_implemented_interfaces (class);
2983 g_free (interfaces_full
);
2984 g_free (interface_offsets_full
);
2985 g_free (array_interfaces
);
2990 * Setup interface offsets for interfaces.
2992 * - class->max_interface_id
2993 * - class->interface_offsets_count
2994 * - class->interfaces_packed
2995 * - class->interface_offsets_packed
2996 * - class->interface_bitmap
2998 * This function can fail @class.
3001 mono_class_setup_interface_offsets (MonoClass
*class)
3003 mono_loader_lock ();
3005 setup_interface_offsets (class, 0);
3007 mono_loader_unlock ();
3011 * mono_class_setup_vtable:
3013 * Creates the generic vtable of CLASS.
3014 * Initializes the following fields in MonoClass:
3017 * Plus all the fields initialized by setup_interface_offsets ().
3018 * If there is an error during vtable construction, class->exception_type is set.
3020 * LOCKING: Acquires the loader lock.
3023 mono_class_setup_vtable (MonoClass
*class)
3025 MonoMethod
**overrides
;
3026 MonoGenericContext
*context
;
3035 if (mono_debug_using_mono_debugger ())
3036 /* The debugger currently depends on this */
3037 mono_class_setup_methods (class);
3039 if (MONO_CLASS_IS_INTERFACE (class)) {
3040 /* This sets method->slot for all methods if this is an interface */
3041 mono_class_setup_methods (class);
3045 if (class->exception_type
)
3048 mono_loader_lock ();
3050 if (class->vtable
) {
3051 mono_loader_unlock ();
3055 mono_stats
.generic_vtable_count
++;
3057 if (class->generic_class
) {
3058 context
= mono_class_get_context (class);
3059 type_token
= class->generic_class
->container_class
->type_token
;
3061 context
= (MonoGenericContext
*) class->generic_container
;
3062 type_token
= class->type_token
;
3065 if (class->image
->dynamic
) {
3066 if (class->generic_class
) {
3067 MonoClass
*gklass
= class->generic_class
->container_class
;
3069 mono_reflection_get_dynamic_overrides (gklass
, &overrides
, &onum
);
3070 for (i
= 0; i
< onum
; ++i
) {
3071 MonoMethod
*override
= overrides
[(i
* 2) + 1];
3072 MonoMethod
*inflated
= NULL
;
3075 for (j
= 0; j
< class->method
.count
; ++j
) {
3076 if (gklass
->methods
[j
] == override
) {
3077 inflated
= class->methods
[j
];
3081 g_assert (inflated
);
3083 overrides
[(i
* 2) + 1] = inflated
;
3086 mono_reflection_get_dynamic_overrides (class, &overrides
, &onum
);
3089 /* The following call fails if there are missing methods in the type */
3090 ok
= mono_class_get_overrides_full (class->image
, type_token
, &overrides
, &onum
, context
);
3094 mono_class_setup_vtable_general (class, overrides
, onum
);
3098 mono_loader_unlock ();
3103 #define DEBUG_INTERFACE_VTABLE_CODE 0
3104 #define TRACE_INTERFACE_VTABLE_CODE 0
3105 #define VERIFY_INTERFACE_VTABLE_CODE 0
3106 #define VTABLE_SELECTOR (1)
3108 #if (TRACE_INTERFACE_VTABLE_CODE|DEBUG_INTERFACE_VTABLE_CODE)
3109 #define DEBUG_INTERFACE_VTABLE(stmt) do {\
3110 if (!(VTABLE_SELECTOR)) break; \
3114 #define DEBUG_INTERFACE_VTABLE(stmt)
3117 #if TRACE_INTERFACE_VTABLE_CODE
3118 #define TRACE_INTERFACE_VTABLE(stmt) do {\
3119 if (!(VTABLE_SELECTOR)) break; \
3123 #define TRACE_INTERFACE_VTABLE(stmt)
3126 #if VERIFY_INTERFACE_VTABLE_CODE
3127 #define VERIFY_INTERFACE_VTABLE(stmt) do {\
3128 if (!(VTABLE_SELECTOR)) break; \
3132 #define VERIFY_INTERFACE_VTABLE(stmt)
3136 #if (TRACE_INTERFACE_VTABLE_CODE|DEBUG_INTERFACE_VTABLE_CODE)
3138 mono_signature_get_full_desc (MonoMethodSignature
*sig
, gboolean include_namespace
)
3142 GString
*res
= g_string_new ("");
3144 g_string_append_c (res
, '(');
3145 for (i
= 0; i
< sig
->param_count
; ++i
) {
3147 g_string_append_c (res
, ',');
3148 mono_type_get_desc (res
, sig
->params
[i
], include_namespace
);
3150 g_string_append (res
, ")=>");
3151 if (sig
->ret
!= NULL
) {
3152 mono_type_get_desc (res
, sig
->ret
, include_namespace
);
3154 g_string_append (res
, "NULL");
3157 g_string_free (res
, FALSE
);
3161 print_method_signatures (MonoMethod
*im
, MonoMethod
*cm
) {
3162 char *im_sig
= mono_signature_get_full_desc (mono_method_signature (im
), TRUE
);
3163 char *cm_sig
= mono_signature_get_full_desc (mono_method_signature (cm
), TRUE
);
3164 printf ("(IM \"%s\", CM \"%s\")", im_sig
, cm_sig
);
3172 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
) {
3173 MonoMethodSignature
*cmsig
, *imsig
;
3174 if (strcmp (im
->name
, cm
->name
) == 0) {
3175 if (! (cm
->flags
& METHOD_ATTRIBUTE_PUBLIC
)) {
3176 TRACE_INTERFACE_VTABLE (printf ("[PUBLIC CHECK FAILED]"));
3179 if (! slot_is_empty
) {
3180 if (require_newslot
) {
3181 if (! interface_is_explicitly_implemented_by_class
) {
3182 TRACE_INTERFACE_VTABLE (printf ("[NOT EXPLICIT IMPLEMENTATION IN FULL SLOT REFUSED]"));
3185 if (! (cm
->flags
& METHOD_ATTRIBUTE_NEW_SLOT
)) {
3186 TRACE_INTERFACE_VTABLE (printf ("[NEWSLOT CHECK FAILED]"));
3190 TRACE_INTERFACE_VTABLE (printf ("[FULL SLOT REFUSED]"));
3193 cmsig
= mono_method_signature (cm
);
3194 imsig
= mono_method_signature (im
);
3195 if (!cmsig
|| !imsig
) {
3196 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Could not resolve the signature of a virtual method"));
3200 if (! mono_metadata_signature_equal (cmsig
, imsig
)) {
3201 TRACE_INTERFACE_VTABLE (printf ("[SIGNATURE CHECK FAILED "));
3202 TRACE_INTERFACE_VTABLE (print_method_signatures (im
, cm
));
3203 TRACE_INTERFACE_VTABLE (printf ("]"));
3206 TRACE_INTERFACE_VTABLE (printf ("[SECURITY CHECKS]"));
3207 /* CAS - SecurityAction.InheritanceDemand on interface */
3208 if (security_enabled
&& (im
->flags
& METHOD_ATTRIBUTE_HAS_SECURITY
)) {
3209 mono_secman_inheritancedemand_method (cm
, im
);
3212 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR
)
3213 mono_security_core_clr_check_override (class, cm
, im
);
3214 TRACE_INTERFACE_VTABLE (printf ("[NAME CHECK OK]"));
3217 MonoClass
*ic
= im
->klass
;
3218 const char *ic_name_space
= ic
->name_space
;
3219 const char *ic_name
= ic
->name
;
3222 if (! require_newslot
) {
3223 TRACE_INTERFACE_VTABLE (printf ("[INJECTED METHOD REFUSED]"));
3226 if (cm
->klass
->rank
== 0) {
3227 TRACE_INTERFACE_VTABLE (printf ("[RANK CHECK FAILED]"));
3230 if (! mono_metadata_signature_equal (mono_method_signature (cm
), mono_method_signature (im
))) {
3231 TRACE_INTERFACE_VTABLE (printf ("[(INJECTED) SIGNATURE CHECK FAILED "));
3232 TRACE_INTERFACE_VTABLE (print_method_signatures (im
, cm
));
3233 TRACE_INTERFACE_VTABLE (printf ("]"));
3236 if (mono_class_get_image (ic
) != mono_defaults
.corlib
) {
3237 TRACE_INTERFACE_VTABLE (printf ("[INTERFACE CORLIB CHECK FAILED]"));
3240 if ((ic_name_space
== NULL
) || (strcmp (ic_name_space
, "System.Collections.Generic") != 0)) {
3241 TRACE_INTERFACE_VTABLE (printf ("[INTERFACE NAMESPACE CHECK FAILED]"));
3244 if ((ic_name
== NULL
) || ((strcmp (ic_name
, "IEnumerable`1") != 0) && (strcmp (ic_name
, "ICollection`1") != 0) && (strcmp (ic_name
, "IList`1") != 0))) {
3245 TRACE_INTERFACE_VTABLE (printf ("[INTERFACE NAME CHECK FAILED]"));
3249 subname
= strstr (cm
->name
, ic_name_space
);
3250 if (subname
!= cm
->name
) {
3251 TRACE_INTERFACE_VTABLE (printf ("[ACTUAL NAMESPACE CHECK FAILED]"));
3254 subname
+= strlen (ic_name_space
);
3255 if (subname
[0] != '.') {
3256 TRACE_INTERFACE_VTABLE (printf ("[FIRST DOT CHECK FAILED]"));
3260 if (strstr (subname
, ic_name
) != subname
) {
3261 TRACE_INTERFACE_VTABLE (printf ("[ACTUAL CLASS NAME CHECK FAILED]"));
3264 subname
+= strlen (ic_name
);
3265 if (subname
[0] != '.') {
3266 TRACE_INTERFACE_VTABLE (printf ("[SECOND DOT CHECK FAILED]"));
3270 if (strcmp (subname
, im
->name
) != 0) {
3271 TRACE_INTERFACE_VTABLE (printf ("[METHOD NAME CHECK FAILED]"));
3275 TRACE_INTERFACE_VTABLE (printf ("[SECURITY CHECKS (INJECTED CASE)]"));
3276 /* CAS - SecurityAction.InheritanceDemand on interface */
3277 if (security_enabled
&& (im
->flags
& METHOD_ATTRIBUTE_HAS_SECURITY
)) {
3278 mono_secman_inheritancedemand_method (cm
, im
);
3281 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR
)
3282 mono_security_core_clr_check_override (class, cm
, im
);
3284 TRACE_INTERFACE_VTABLE (printf ("[INJECTED INTERFACE CHECK OK]"));
3289 #if (TRACE_INTERFACE_VTABLE_CODE|DEBUG_INTERFACE_VTABLE_CODE)
3291 foreach_override (gpointer key
, gpointer value
, gpointer user_data
) {
3292 MonoMethod
*method
= key
;
3293 MonoMethod
*override
= value
;
3294 MonoClass
*method_class
= mono_method_get_class (method
);
3295 MonoClass
*override_class
= mono_method_get_class (override
);
3297 printf (" Method '%s.%s:%s' has override '%s.%s:%s'\n",
3298 mono_class_get_namespace (method_class
), mono_class_get_name (method_class
), mono_method_get_name (method
),
3299 mono_class_get_namespace (override_class
), mono_class_get_name (override_class
), mono_method_get_name (override
));
3302 print_overrides (GHashTable
*override_map
, const char *message
) {
3304 printf ("Override map \"%s\" START:\n", message
);
3305 g_hash_table_foreach (override_map
, foreach_override
, NULL
);
3306 printf ("Override map \"%s\" END.\n", message
);
3308 printf ("Override map \"%s\" EMPTY.\n", message
);
3312 print_vtable_full (MonoClass
*class, MonoMethod
** vtable
, int size
, int first_non_interface_slot
, const char *message
, gboolean print_interfaces
) {
3313 char *full_name
= mono_type_full_name (&class->byval_arg
);
3317 printf ("*** Vtable for class '%s' at \"%s\" (size %d)\n", full_name
, message
, size
);
3319 if (print_interfaces
) {
3320 print_implemented_interfaces (class);
3321 printf ("* Interfaces for class '%s' done.\nStarting vtable (size %d):\n", full_name
, size
);
3324 if (class->parent
) {
3325 parent_size
= class->parent
->vtable_size
;
3329 for (i
= 0; i
< size
; ++i
) {
3330 MonoMethod
*cm
= vtable
[i
];
3332 char *cm_name
= mono_method_full_name (cm
, TRUE
);
3333 char newness
= (i
< parent_size
) ? 'O' : ((i
< first_non_interface_slot
) ? 'I' : 'N');
3334 printf (" [%c][%03d][INDEX %03d] %s\n", newness
, i
, cm
->slot
, cm_name
);
3343 #if VERIFY_INTERFACE_VTABLE_CODE
3345 mono_method_try_get_vtable_index (MonoMethod
*method
)
3347 if (method
->is_inflated
&& (method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
)) {
3348 MonoMethodInflated
*imethod
= (MonoMethodInflated
*)method
;
3349 if (imethod
->declaring
->is_generic
)
3350 return imethod
->declaring
->slot
;
3352 return method
->slot
;
3356 mono_class_verify_vtable (MonoClass
*class)
3359 char *full_name
= mono_type_full_name (&class->byval_arg
);
3361 printf ("*** Verifying VTable of class '%s' \n", full_name
);
3365 if (!class->methods
)
3368 for (i
= 0; i
< class->method
.count
; ++i
) {
3369 MonoMethod
*cm
= class->methods
[i
];
3372 if (!(cm
->flags
& METHOD_ATTRIBUTE_VIRTUAL
))
3376 full_name
= mono_method_full_name (cm
, TRUE
);
3378 slot
= mono_method_try_get_vtable_index (cm
);
3380 if (slot
>= class->vtable_size
) {
3381 printf ("\tInvalid method %s at index %d with vtable of length %d\n", full_name
, slot
, class->vtable_size
);
3385 if (slot
>= 0 && class->vtable
[slot
] != cm
&& (class->vtable
[slot
])) {
3386 char *other_name
= class->vtable
[slot
] ? mono_method_full_name (class->vtable
[slot
], TRUE
) : g_strdup ("[null value]");
3387 printf ("\tMethod %s has slot %d but vtable has %s on it\n", full_name
, slot
, other_name
);
3388 g_free (other_name
);
3391 printf ("\tVirtual method %s does n't have an assigned slot\n", full_name
);
3398 print_unimplemented_interface_method_info (MonoClass
*class, MonoClass
*ic
, MonoMethod
*im
, int im_slot
, MonoMethod
**overrides
, int onum
) {
3400 char *method_signature
;
3403 for (index
= 0; index
< onum
; ++index
) {
3404 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", im_slot
, overrides
[index
*2+1]->name
,
3405 overrides
[index
*2+1]->slot
, overrides
[index
*2]->name
, overrides
[index
*2]->slot
);
3407 method_signature
= mono_signature_get_desc (mono_method_signature (im
), FALSE
);
3408 type_name
= mono_type_full_name (&class->byval_arg
);
3409 printf ("no implementation for interface method %s::%s(%s) in class %s\n",
3410 mono_type_get_name (&ic
->byval_arg
), im
->name
, method_signature
, type_name
);
3411 g_free (method_signature
);
3413 mono_class_setup_methods (class);
3414 if (class->exception_type
) {
3415 char *name
= mono_type_get_full_name (class);
3416 printf ("CLASS %s failed to resolve methods\n", name
);
3420 for (index
= 0; index
< class->method
.count
; ++index
) {
3421 MonoMethod
*cm
= class->methods
[index
];
3422 method_signature
= mono_signature_get_desc (mono_method_signature (cm
), TRUE
);
3424 printf ("METHOD %s(%s)\n", cm
->name
, method_signature
);
3425 g_free (method_signature
);
3430 verify_class_overrides (MonoClass
*class, MonoMethod
**overrides
, int onum
)
3434 for (i
= 0; i
< onum
; ++i
) {
3435 MonoMethod
*decl
= overrides
[i
* 2];
3436 MonoMethod
*body
= overrides
[i
* 2 + 1];
3438 if (mono_class_get_generic_type_definition (body
->klass
) != mono_class_get_generic_type_definition (class)) {
3439 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Method belongs to a different class than the declared one"));
3443 if (!(body
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) || (body
->flags
& METHOD_ATTRIBUTE_STATIC
)) {
3444 if (body
->flags
& METHOD_ATTRIBUTE_STATIC
)
3445 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Method must not be static to override a base type"));
3447 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Method must be virtual to override a base type"));
3451 if (!(decl
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) || (decl
->flags
& METHOD_ATTRIBUTE_STATIC
)) {
3452 if (body
->flags
& METHOD_ATTRIBUTE_STATIC
)
3453 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Cannot override a static method in a base type"));
3455 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Cannot override a non virtual method in a base type"));
3459 if (!mono_class_is_assignable_from_slow (decl
->klass
, class)) {
3460 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Method overrides a class or interface that extended or implemented by this type"));
3467 * LOCKING: this is supposed to be called with the loader lock held.
3470 mono_class_setup_vtable_general (MonoClass
*class, MonoMethod
**overrides
, int onum
)
3474 MonoMethod
**vtable
;
3475 int i
, max_vtsize
= 0, max_iid
, cur_slot
= 0;
3476 GPtrArray
*ifaces
= NULL
;
3477 GHashTable
*override_map
= NULL
;
3478 gboolean security_enabled
= mono_is_security_manager_active ();
3480 gpointer class_iter
;
3481 #if (DEBUG_INTERFACE_VTABLE_CODE|TRACE_INTERFACE_VTABLE_CODE)
3482 int first_non_interface_slot
;
3484 GSList
*virt_methods
= NULL
, *l
;
3489 if (overrides
&& !verify_class_overrides (class, overrides
, onum
))
3492 ifaces
= mono_class_get_implemented_interfaces (class, &error
);
3493 if (!mono_error_ok (&error
)) {
3494 char *name
= mono_type_get_full_name (class);
3495 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup_printf ("Could not resolve %s interfaces due to %s", name
, mono_error_get_message (&error
)));
3497 mono_error_cleanup (&error
);
3499 } else if (ifaces
) {
3500 for (i
= 0; i
< ifaces
->len
; i
++) {
3501 MonoClass
*ic
= g_ptr_array_index (ifaces
, i
);
3502 max_vtsize
+= ic
->method
.count
;
3504 g_ptr_array_free (ifaces
, TRUE
);
3508 if (class->parent
) {
3509 mono_class_init (class->parent
);
3510 mono_class_setup_vtable (class->parent
);
3512 if (class->parent
->exception_type
) {
3513 char *name
= mono_type_get_full_name (class->parent
);
3514 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup_printf ("Parent %s failed to load", name
));
3519 max_vtsize
+= class->parent
->vtable_size
;
3520 cur_slot
= class->parent
->vtable_size
;
3523 max_vtsize
+= class->method
.count
;
3525 vtable
= alloca (sizeof (gpointer
) * max_vtsize
);
3526 memset (vtable
, 0, sizeof (gpointer
) * max_vtsize
);
3528 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
3530 cur_slot
= setup_interface_offsets (class, cur_slot
);
3531 if (cur_slot
== -1) /*setup_interface_offsets fails the type.*/
3534 max_iid
= class->max_interface_id
;
3535 DEBUG_INTERFACE_VTABLE (first_non_interface_slot
= cur_slot
);
3537 /* Optimized version for generic instances */
3538 if (class->generic_class
) {
3539 MonoClass
*gklass
= class->generic_class
->container_class
;
3542 mono_class_setup_vtable (gklass
);
3543 if (gklass
->exception_type
!= MONO_EXCEPTION_NONE
) {
3544 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
3548 tmp
= mono_image_alloc0 (class->image
, sizeof (gpointer
) * gklass
->vtable_size
);
3549 class->vtable_size
= gklass
->vtable_size
;
3550 for (i
= 0; i
< gklass
->vtable_size
; ++i
)
3551 if (gklass
->vtable
[i
]) {
3552 tmp
[i
] = mono_class_inflate_generic_method_full (gklass
->vtable
[i
], class, mono_class_get_context (class));
3553 tmp
[i
]->slot
= gklass
->vtable
[i
]->slot
;
3555 mono_memory_barrier ();
3556 class->vtable
= tmp
;
3558 /* Have to set method->slot for abstract virtual methods */
3559 if (class->methods
&& gklass
->methods
) {
3560 for (i
= 0; i
< class->method
.count
; ++i
)
3561 if (class->methods
[i
]->slot
== -1)
3562 class->methods
[i
]->slot
= gklass
->methods
[i
]->slot
;
3568 if (class->parent
&& class->parent
->vtable_size
) {
3569 MonoClass
*parent
= class->parent
;
3572 memcpy (vtable
, parent
->vtable
, sizeof (gpointer
) * parent
->vtable_size
);
3574 // Also inherit parent interface vtables, just as a starting point.
3575 // This is needed otherwise bug-77127.exe fails when the property methods
3576 // have different names in the iterface and the class, because for child
3577 // classes the ".override" information is not used anymore.
3578 for (i
= 0; i
< parent
->interface_offsets_count
; i
++) {
3579 MonoClass
*parent_interface
= parent
->interfaces_packed
[i
];
3580 int interface_offset
= mono_class_interface_offset (class, parent_interface
);
3581 /*FIXME this is now dead code as this condition will never hold true.
3582 Since interface offsets are inherited then the offset of an interface implemented
3583 by a parent will never be the out of it's vtable boundary.
3585 if (interface_offset
>= parent
->vtable_size
) {
3586 int parent_interface_offset
= mono_class_interface_offset (parent
, parent_interface
);
3589 mono_class_setup_methods (parent_interface
); /*FIXME Just kill this whole chunk of dead code*/
3590 TRACE_INTERFACE_VTABLE (printf (" +++ Inheriting interface %s.%s\n", parent_interface
->name_space
, parent_interface
->name
));
3591 for (j
= 0; j
< parent_interface
->method
.count
&& !class->exception_type
; j
++) {
3592 vtable
[interface_offset
+ j
] = parent
->vtable
[parent_interface_offset
+ j
];
3593 TRACE_INTERFACE_VTABLE (printf (" --- Inheriting: [%03d][(%03d)+(%03d)] => [%03d][(%03d)+(%03d)]\n",
3594 parent_interface_offset
+ j
, parent_interface_offset
, j
,
3595 interface_offset
+ j
, interface_offset
, j
));
3602 TRACE_INTERFACE_VTABLE (print_vtable_full (class, vtable
, cur_slot
, first_non_interface_slot
, "AFTER INHERITING PARENT VTABLE", TRUE
));
3603 /* override interface methods */
3604 for (i
= 0; i
< onum
; i
++) {
3605 MonoMethod
*decl
= overrides
[i
*2];
3606 if (MONO_CLASS_IS_INTERFACE (decl
->klass
)) {
3608 dslot
= mono_method_get_vtable_slot (decl
);
3610 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
3614 dslot
+= mono_class_interface_offset (class, decl
->klass
);
3615 vtable
[dslot
] = overrides
[i
*2 + 1];
3616 vtable
[dslot
]->slot
= dslot
;
3618 override_map
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
3620 g_hash_table_insert (override_map
, overrides
[i
* 2], overrides
[i
* 2 + 1]);
3622 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR
)
3623 mono_security_core_clr_check_override (class, vtable
[dslot
], decl
);
3626 TRACE_INTERFACE_VTABLE (print_overrides (override_map
, "AFTER OVERRIDING INTERFACE METHODS"));
3627 TRACE_INTERFACE_VTABLE (print_vtable_full (class, vtable
, cur_slot
, first_non_interface_slot
, "AFTER OVERRIDING INTERFACE METHODS", FALSE
));
3630 * Create a list of virtual methods to avoid calling
3631 * mono_class_get_virtual_methods () which is slow because of the metadata
3635 gpointer iter
= NULL
;
3638 virt_methods
= NULL
;
3639 while ((cm
= mono_class_get_virtual_methods (class, &iter
))) {
3640 virt_methods
= g_slist_prepend (virt_methods
, cm
);
3642 if (class->exception_type
)
3646 // Loop on all implemented interfaces...
3647 for (i
= 0; i
< class->interface_offsets_count
; i
++) {
3648 MonoClass
*parent
= class->parent
;
3650 gboolean interface_is_explicitly_implemented_by_class
;
3653 ic
= class->interfaces_packed
[i
];
3654 ic_offset
= mono_class_interface_offset (class, ic
);
3656 mono_class_setup_methods (ic
);
3657 if (ic
->exception_type
)
3660 // Check if this interface is explicitly implemented (instead of just inherited)
3661 if (parent
!= NULL
) {
3662 int implemented_interfaces_index
;
3663 interface_is_explicitly_implemented_by_class
= FALSE
;
3664 for (implemented_interfaces_index
= 0; implemented_interfaces_index
< class->interface_count
; implemented_interfaces_index
++) {
3665 if (ic
== class->interfaces
[implemented_interfaces_index
]) {
3666 interface_is_explicitly_implemented_by_class
= TRUE
;
3671 interface_is_explicitly_implemented_by_class
= TRUE
;
3674 // Loop on all interface methods...
3675 for (im_index
= 0; im_index
< ic
->method
.count
; im_index
++) {
3676 MonoMethod
*im
= ic
->methods
[im_index
];
3677 int im_slot
= ic_offset
+ im
->slot
;
3678 MonoMethod
*override_im
= (override_map
!= NULL
) ? g_hash_table_lookup (override_map
, im
) : NULL
;
3680 if (im
->flags
& METHOD_ATTRIBUTE_STATIC
)
3683 // If there is an explicit implementation, just use it right away,
3684 // otherwise look for a matching method
3685 if (override_im
== NULL
) {
3690 // First look for a suitable method among the class methods
3692 for (l
= virt_methods
; l
; l
= l
->next
) {
3694 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
)));
3695 if (check_interface_method_override (class, im
, cm
, TRUE
, interface_is_explicitly_implemented_by_class
, (vtable
[im_slot
] == NULL
), security_enabled
)) {
3696 TRACE_INTERFACE_VTABLE (printf ("[check ok]: ASSIGNING"));
3697 vtable
[im_slot
] = cm
;
3698 /* Why do we need this? */
3703 TRACE_INTERFACE_VTABLE (printf ("\n"));
3704 if (class->exception_type
) /*Might be set by check_interface_method_override*/
3708 // If the slot is still empty, look in all the inherited virtual methods...
3709 if ((vtable
[im_slot
] == NULL
) && class->parent
!= NULL
) {
3710 MonoClass
*parent
= class->parent
;
3711 // Reverse order, so that last added methods are preferred
3712 for (cm_index
= parent
->vtable_size
- 1; cm_index
>= 0; cm_index
--) {
3713 MonoMethod
*cm
= parent
->vtable
[cm_index
];
3715 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
));
3716 if ((cm
!= NULL
) && check_interface_method_override (class, im
, cm
, FALSE
, FALSE
, TRUE
, security_enabled
)) {
3717 TRACE_INTERFACE_VTABLE (printf ("[everything ok]: ASSIGNING"));
3718 vtable
[im_slot
] = cm
;
3719 /* Why do we need this? */
3725 if (class->exception_type
) /*Might be set by check_interface_method_override*/
3727 TRACE_INTERFACE_VTABLE ((cm
!= NULL
) && printf ("\n"));
3731 g_assert (vtable
[im_slot
] == override_im
);
3736 // If the class is not abstract, check that all its interface slots are full.
3737 // The check is done here and not directly at the end of the loop above because
3738 // it can happen (for injected generic array interfaces) that the same slot is
3739 // processed multiple times (those interfaces have overlapping slots), and it
3740 // will not always be the first pass the one that fills the slot.
3741 if (! (class->flags
& TYPE_ATTRIBUTE_ABSTRACT
)) {
3742 for (i
= 0; i
< class->interface_offsets_count
; i
++) {
3746 ic
= class->interfaces_packed
[i
];
3747 ic_offset
= mono_class_interface_offset (class, ic
);
3749 for (im_index
= 0; im_index
< ic
->method
.count
; im_index
++) {
3750 MonoMethod
*im
= ic
->methods
[im_index
];
3751 int im_slot
= ic_offset
+ im
->slot
;
3753 if (im
->flags
& METHOD_ATTRIBUTE_STATIC
)
3756 TRACE_INTERFACE_VTABLE (printf (" [class is not abstract, checking slot %d for interface '%s'.'%s', method %s, slot check is %d]\n",
3757 im_slot
, ic
->name_space
, ic
->name
, im
->name
, (vtable
[im_slot
] == NULL
)));
3758 if (vtable
[im_slot
] == NULL
) {
3759 print_unimplemented_interface_method_info (class, ic
, im
, im_slot
, overrides
, onum
);
3766 TRACE_INTERFACE_VTABLE (print_vtable_full (class, vtable
, cur_slot
, first_non_interface_slot
, "AFTER SETTING UP INTERFACE METHODS", FALSE
));
3768 for (l
= virt_methods
; l
; l
= l
->next
) {
3771 * If the method is REUSE_SLOT, we must check in the
3772 * base class for a method to override.
3774 if (!(cm
->flags
& METHOD_ATTRIBUTE_NEW_SLOT
)) {
3776 for (k
= class->parent
; k
; k
= k
->parent
) {
3781 while ((m1
= mono_class_get_virtual_methods (k
, &k_iter
))) {
3782 MonoMethodSignature
*cmsig
, *m1sig
;
3784 cmsig
= mono_method_signature (cm
);
3785 m1sig
= mono_method_signature (m1
);
3787 if (!cmsig
|| !m1sig
) {
3788 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
3792 if (!strcmp(cm
->name
, m1
->name
) &&
3793 mono_metadata_signature_equal (cmsig
, m1sig
)) {
3795 /* CAS - SecurityAction.InheritanceDemand */
3796 if (security_enabled
&& (m1
->flags
& METHOD_ATTRIBUTE_HAS_SECURITY
)) {
3797 mono_secman_inheritancedemand_method (cm
, m1
);
3800 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR
)
3801 mono_security_core_clr_check_override (class, cm
, m1
);
3803 slot
= mono_method_get_vtable_slot (m1
);
3807 g_assert (cm
->slot
< max_vtsize
);
3809 override_map
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
3810 g_hash_table_insert (override_map
, m1
, cm
);
3814 if (k
->exception_type
)
3824 /*Non final newslot methods must be given a non-interface vtable slot*/
3825 if ((cm
->flags
& METHOD_ATTRIBUTE_NEW_SLOT
) && !(cm
->flags
& METHOD_ATTRIBUTE_FINAL
) && cm
->slot
>= 0)
3829 cm
->slot
= cur_slot
++;
3831 if (!(cm
->flags
& METHOD_ATTRIBUTE_ABSTRACT
))
3832 vtable
[cm
->slot
] = cm
;
3835 /* override non interface methods */
3836 for (i
= 0; i
< onum
; i
++) {
3837 MonoMethod
*decl
= overrides
[i
*2];
3838 if (!MONO_CLASS_IS_INTERFACE (decl
->klass
)) {
3839 g_assert (decl
->slot
!= -1);
3840 vtable
[decl
->slot
] = overrides
[i
*2 + 1];
3841 overrides
[i
* 2 + 1]->slot
= decl
->slot
;
3843 override_map
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
3844 g_hash_table_insert (override_map
, decl
, overrides
[i
* 2 + 1]);
3846 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR
)
3847 mono_security_core_clr_check_override (class, vtable
[decl
->slot
], decl
);
3852 * If a method occupies more than one place in the vtable, and it is
3853 * overriden, then change the other occurances too.
3856 for (i
= 0; i
< max_vtsize
; ++i
)
3858 MonoMethod
*cm
= g_hash_table_lookup (override_map
, vtable
[i
]);
3863 g_hash_table_destroy (override_map
);
3864 override_map
= NULL
;
3867 g_slist_free (virt_methods
);
3868 virt_methods
= NULL
;
3870 /* Ensure that all vtable slots are filled with concrete instance methods */
3871 if (!(class->flags
& TYPE_ATTRIBUTE_ABSTRACT
)) {
3872 for (i
= 0; i
< cur_slot
; ++i
) {
3873 if (vtable
[i
] == NULL
|| (vtable
[i
]->flags
& (METHOD_ATTRIBUTE_ABSTRACT
| METHOD_ATTRIBUTE_STATIC
))) {
3874 char *type_name
= mono_type_get_full_name (class);
3875 char *method_name
= vtable
[i
] ? mono_method_full_name (vtable
[i
], TRUE
) : g_strdup ("none");
3876 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup_printf ("Type %s has invalid vtable method slot %d with method %s", type_name
, i
, method_name
));
3878 g_free (method_name
);
3884 if (class->generic_class
) {
3885 MonoClass
*gklass
= class->generic_class
->container_class
;
3887 mono_class_init (gklass
);
3889 class->vtable_size
= MAX (gklass
->vtable_size
, cur_slot
);
3891 /* Check that the vtable_size value computed in mono_class_init () is correct */
3892 if (class->vtable_size
)
3893 g_assert (cur_slot
== class->vtable_size
);
3894 class->vtable_size
= cur_slot
;
3897 /* Try to share the vtable with our parent. */
3898 if (class->parent
&& (class->parent
->vtable_size
== class->vtable_size
) && (memcmp (class->parent
->vtable
, vtable
, sizeof (gpointer
) * class->vtable_size
) == 0)) {
3899 mono_memory_barrier ();
3900 class->vtable
= class->parent
->vtable
;
3902 MonoMethod
**tmp
= mono_image_alloc0 (class->image
, sizeof (gpointer
) * class->vtable_size
);
3903 memcpy (tmp
, vtable
, sizeof (gpointer
) * class->vtable_size
);
3904 mono_memory_barrier ();
3905 class->vtable
= tmp
;
3908 DEBUG_INTERFACE_VTABLE (print_vtable_full (class, class->vtable
, class->vtable_size
, first_non_interface_slot
, "FINALLY", FALSE
));
3909 if (mono_print_vtable
) {
3912 print_implemented_interfaces (class);
3914 for (i
= 0; i
<= max_iid
; i
++)
3915 if (MONO_CLASS_IMPLEMENTS_INTERFACE (class, i
))
3918 printf ("VTable %s (vtable entries = %d, interfaces = %d)\n", mono_type_full_name (&class->byval_arg
),
3919 class->vtable_size
, icount
);
3921 for (i
= 0; i
< cur_slot
; ++i
) {
3926 printf (" slot assigned: %03d, slot index: %03d %s\n", i
, cm
->slot
,
3927 mono_method_full_name (cm
, TRUE
));
3933 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space
,
3934 class->name
, max_iid
);
3936 for (i
= 0; i
< class->interface_count
; i
++) {
3937 ic
= class->interfaces
[i
];
3938 printf (" slot offset: %03d, method count: %03d, iid: %03d %s\n",
3939 mono_class_interface_offset (class, ic
),
3940 count_virtual_methods (ic
), ic
->interface_id
, mono_type_full_name (&ic
->byval_arg
));
3943 for (k
= class->parent
; k
; k
= k
->parent
) {
3944 for (i
= 0; i
< k
->interface_count
; i
++) {
3945 ic
= k
->interfaces
[i
];
3946 printf (" parent slot offset: %03d, method count: %03d, iid: %03d %s\n",
3947 mono_class_interface_offset (class, ic
),
3948 count_virtual_methods (ic
), ic
->interface_id
, mono_type_full_name (&ic
->byval_arg
));
3954 VERIFY_INTERFACE_VTABLE (mono_class_verify_vtable (class));
3959 char *name
= mono_type_get_full_name (class);
3960 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup_printf ("VTable setup of type %s failed", name
));
3963 g_hash_table_destroy (override_map
);
3965 g_slist_free (virt_methods
);
3970 * mono_method_get_vtable_slot:
3972 * Returns method->slot, computing it if neccesary. Return -1 on failure.
3973 * LOCKING: Acquires the loader lock.
3975 * FIXME Use proper MonoError machinery here.
3978 mono_method_get_vtable_slot (MonoMethod
*method
)
3980 if (method
->slot
== -1) {
3981 mono_class_setup_vtable (method
->klass
);
3982 if (method
->klass
->exception_type
)
3984 g_assert (method
->slot
!= -1);
3986 return method
->slot
;
3990 * mono_method_get_vtable_index:
3993 * Returns the index into the runtime vtable to access the method or,
3994 * in the case of a virtual generic method, the virtual generic method
3995 * thunk. Returns -1 on failure.
3997 * FIXME Use proper MonoError machinery here.
4000 mono_method_get_vtable_index (MonoMethod
*method
)
4002 if (method
->is_inflated
&& (method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
)) {
4003 MonoMethodInflated
*imethod
= (MonoMethodInflated
*)method
;
4004 if (imethod
->declaring
->is_generic
)
4005 return mono_method_get_vtable_slot (imethod
->declaring
);
4007 return mono_method_get_vtable_slot (method
);
4010 static MonoMethod
*default_ghc
= NULL
;
4011 static MonoMethod
*default_finalize
= NULL
;
4012 static int finalize_slot
= -1;
4013 static int ghc_slot
= -1;
4016 initialize_object_slots (MonoClass
*class)
4021 if (class == mono_defaults
.object_class
) {
4022 mono_class_setup_vtable (class);
4023 for (i
= 0; i
< class->vtable_size
; ++i
) {
4024 MonoMethod
*cm
= class->vtable
[i
];
4026 if (!strcmp (cm
->name
, "GetHashCode"))
4028 else if (!strcmp (cm
->name
, "Finalize"))
4032 g_assert (ghc_slot
> 0);
4033 default_ghc
= class->vtable
[ghc_slot
];
4035 g_assert (finalize_slot
> 0);
4036 default_finalize
= class->vtable
[finalize_slot
];
4041 MonoMethod
*array_method
;
4043 } GenericArrayMethodInfo
;
4045 static int generic_array_method_num
= 0;
4046 static GenericArrayMethodInfo
*generic_array_method_info
= NULL
;
4049 generic_array_methods (MonoClass
*class)
4051 int i
, count_generic
= 0;
4052 GList
*list
= NULL
, *tmp
;
4053 if (generic_array_method_num
)
4054 return generic_array_method_num
;
4055 mono_class_setup_methods (class->parent
); /*This is setting up System.Array*/
4056 g_assert (!class->parent
->exception_type
); /*So hitting this assert is a huge problem*/
4057 for (i
= 0; i
< class->parent
->method
.count
; i
++) {
4058 MonoMethod
*m
= class->parent
->methods
[i
];
4059 if (!strncmp (m
->name
, "InternalArray__", 15)) {
4061 list
= g_list_prepend (list
, m
);
4064 list
= g_list_reverse (list
);
4065 generic_array_method_info
= g_malloc (sizeof (GenericArrayMethodInfo
) * count_generic
);
4067 for (tmp
= list
; tmp
; tmp
= tmp
->next
) {
4068 const char *mname
, *iname
;
4070 MonoMethod
*m
= tmp
->data
;
4071 generic_array_method_info
[i
].array_method
= m
;
4072 if (!strncmp (m
->name
, "InternalArray__ICollection_", 27)) {
4073 iname
= "System.Collections.Generic.ICollection`1.";
4074 mname
= m
->name
+ 27;
4075 } else if (!strncmp (m
->name
, "InternalArray__IEnumerable_", 27)) {
4076 iname
= "System.Collections.Generic.IEnumerable`1.";
4077 mname
= m
->name
+ 27;
4078 } else if (!strncmp (m
->name
, "InternalArray__", 15)) {
4079 iname
= "System.Collections.Generic.IList`1.";
4080 mname
= m
->name
+ 15;
4082 g_assert_not_reached ();
4085 name
= mono_image_alloc (mono_defaults
.corlib
, strlen (iname
) + strlen (mname
) + 1);
4086 strcpy (name
, iname
);
4087 strcpy (name
+ strlen (iname
), mname
);
4088 generic_array_method_info
[i
].name
= name
;
4091 /*g_print ("array generic methods: %d\n", count_generic);*/
4093 generic_array_method_num
= count_generic
;
4095 return generic_array_method_num
;
4099 setup_generic_array_ifaces (MonoClass
*class, MonoClass
*iface
, MonoMethod
**methods
, int pos
)
4101 MonoGenericContext tmp_context
;
4104 tmp_context
.class_inst
= NULL
;
4105 tmp_context
.method_inst
= iface
->generic_class
->context
.class_inst
;
4106 //g_print ("setting up array interface: %s\n", mono_type_get_name_full (&iface->byval_arg, 0));
4108 for (i
= 0; i
< generic_array_method_num
; i
++) {
4109 MonoMethod
*m
= generic_array_method_info
[i
].array_method
;
4110 MonoMethod
*inflated
;
4112 inflated
= mono_class_inflate_generic_method (m
, &tmp_context
);
4113 methods
[pos
++] = mono_marshal_get_generic_array_helper (class, iface
, generic_array_method_info
[i
].name
, inflated
);
4118 concat_two_strings_with_zero (MonoImage
*image
, const char *s1
, const char *s2
)
4120 int len
= strlen (s1
) + strlen (s2
) + 2;
4121 char *s
= mono_image_alloc (image
, len
);
4124 result
= g_snprintf (s
, len
, "%s%c%s", s1
, '\0', s2
);
4125 g_assert (result
== len
- 1);
4131 set_failure_from_loader_error (MonoClass
*class, MonoLoaderError
*error
)
4133 gpointer exception_data
= NULL
;
4135 switch (error
->exception_type
) {
4136 case MONO_EXCEPTION_TYPE_LOAD
:
4137 exception_data
= concat_two_strings_with_zero (class->image
, error
->class_name
, error
->assembly_name
);
4140 case MONO_EXCEPTION_MISSING_METHOD
:
4141 exception_data
= concat_two_strings_with_zero (class->image
, error
->class_name
, error
->member_name
);
4144 case MONO_EXCEPTION_MISSING_FIELD
: {
4145 const char *name_space
= error
->klass
->name_space
? error
->klass
->name_space
: NULL
;
4146 const char *class_name
;
4149 class_name
= g_strdup_printf ("%s.%s", name_space
, error
->klass
->name
);
4151 class_name
= error
->klass
->name
;
4153 exception_data
= concat_two_strings_with_zero (class->image
, class_name
, error
->member_name
);
4156 g_free ((void*)class_name
);
4160 case MONO_EXCEPTION_FILE_NOT_FOUND
: {
4163 if (error
->ref_only
)
4164 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.";
4166 msg
= "Could not load file or assembly '%s' or one of its dependencies.";
4168 exception_data
= concat_two_strings_with_zero (class->image
, msg
, error
->assembly_name
);
4172 case MONO_EXCEPTION_BAD_IMAGE
:
4173 exception_data
= error
->msg
;
4177 g_assert_not_reached ();
4180 mono_class_set_failure (class, error
->exception_type
, exception_data
);
4185 * @class: the class to initialize
4187 * Compute the instance_size, class_size and other infos that cannot be
4188 * computed at mono_class_get() time. Also compute vtable_size if possible.
4189 * Returns TRUE on success or FALSE if there was a problem in loading
4190 * the type (incorrect assemblies, missing assemblies, methods, etc).
4192 * LOCKING: Acquires the loader lock.
4195 mono_class_init (MonoClass
*class)
4198 MonoCachedClassInfo cached_info
;
4199 gboolean has_cached_info
;
4203 /* Double-checking locking pattern */
4205 return class->exception_type
== MONO_EXCEPTION_NONE
;
4207 /*g_print ("Init class %s\n", class->name);*/
4209 /* We do everything inside the lock to prevent races */
4210 mono_loader_lock ();
4212 if (class->inited
) {
4213 mono_loader_unlock ();
4214 /* Somebody might have gotten in before us */
4215 return class->exception_type
== MONO_EXCEPTION_NONE
;
4218 if (class->init_pending
) {
4219 mono_loader_unlock ();
4220 /* this indicates a cyclic dependency */
4221 g_error ("pending init %s.%s\n", class->name_space
, class->name
);
4224 class->init_pending
= 1;
4226 if (mono_verifier_is_enabled_for_class (class) && !mono_verifier_verify_class (class)) {
4227 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, concat_two_strings_with_zero (class->image
, class->name
, class->image
->assembly_name
));
4232 if (class->byval_arg
.type
== MONO_TYPE_ARRAY
|| class->byval_arg
.type
== MONO_TYPE_SZARRAY
) {
4233 MonoClass
*element_class
= class->element_class
;
4234 if (!element_class
->inited
)
4235 mono_class_init (element_class
);
4236 if (element_class
->exception_type
!= MONO_EXCEPTION_NONE
) {
4237 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
4242 /* CAS - SecurityAction.InheritanceDemand */
4243 if (mono_is_security_manager_active () && class->parent
&& (class->parent
->flags
& TYPE_ATTRIBUTE_HAS_SECURITY
)) {
4244 mono_secman_inheritancedemand_class (class, class->parent
);
4247 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR
)
4248 mono_security_core_clr_check_inheritance (class);
4250 mono_stats
.initialized_class_count
++;
4252 if (class->generic_class
&& !class->generic_class
->is_dynamic
) {
4253 MonoClass
*gklass
= class->generic_class
->container_class
;
4255 mono_stats
.generic_class_count
++;
4257 class->method
= gklass
->method
;
4258 class->field
= gklass
->field
;
4260 mono_class_init (gklass
);
4261 // FIXME: Why is this needed ?
4262 if (!gklass
->exception_type
)
4263 mono_class_setup_methods (gklass
);
4264 if (gklass
->exception_type
) {
4265 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup_printf ("Generic Type Defintion failed to init"));
4269 if (MONO_CLASS_IS_INTERFACE (class))
4270 class->interface_id
= mono_get_unique_iid (class);
4273 if (class->parent
&& !class->parent
->inited
)
4274 mono_class_init (class->parent
);
4276 has_cached_info
= mono_class_get_cached_class_info (class, &cached_info
);
4278 if (class->generic_class
|| class->image
->dynamic
|| !class->type_token
|| (has_cached_info
&& !cached_info
.has_nested_classes
))
4279 class->nested_classes_inited
= TRUE
;
4282 * Computes the size used by the fields, and their locations
4284 if (has_cached_info
) {
4285 class->instance_size
= cached_info
.instance_size
;
4286 class->sizes
.class_size
= cached_info
.class_size
;
4287 class->packing_size
= cached_info
.packing_size
;
4288 class->min_align
= cached_info
.min_align
;
4289 class->blittable
= cached_info
.blittable
;
4290 class->has_references
= cached_info
.has_references
;
4291 class->has_static_refs
= cached_info
.has_static_refs
;
4292 class->no_special_static_fields
= cached_info
.no_special_static_fields
;
4295 if (!class->size_inited
){
4296 mono_class_setup_fields (class);
4297 if (class->exception_type
|| mono_loader_get_last_error ())
4301 /* Initialize arrays */
4303 class->method
.count
= 3 + (class->rank
> 1? 2: 1);
4305 if (class->interface_count
) {
4306 int count_generic
= generic_array_methods (class);
4307 class->method
.count
+= class->interface_count
* count_generic
;
4311 mono_class_setup_supertypes (class);
4314 initialize_object_slots (class);
4317 * Initialize the rest of the data without creating a generic vtable if possible.
4318 * If possible, also compute vtable_size, so mono_class_create_runtime_vtable () can
4319 * also avoid computing a generic vtable.
4321 if (has_cached_info
) {
4323 class->vtable_size
= cached_info
.vtable_size
;
4324 class->has_finalize
= cached_info
.has_finalize
;
4325 class->ghcimpl
= cached_info
.ghcimpl
;
4326 class->has_cctor
= cached_info
.has_cctor
;
4327 } else if (class->rank
== 1 && class->byval_arg
.type
== MONO_TYPE_SZARRAY
) {
4328 static int szarray_vtable_size
= 0;
4331 if (!szarray_vtable_size
) {
4332 mono_class_setup_vtable (class);
4333 szarray_vtable_size
= class->vtable_size
;
4335 class->vtable_size
= szarray_vtable_size
;
4337 } else if (class->generic_class
&& !MONO_CLASS_IS_INTERFACE (class)) {
4338 MonoClass
*gklass
= class->generic_class
->container_class
;
4340 /* Generic instance case */
4341 class->ghcimpl
= gklass
->ghcimpl
;
4342 class->has_finalize
= gklass
->has_finalize
;
4343 class->has_cctor
= gklass
->has_cctor
;
4345 mono_class_setup_vtable (gklass
);
4346 if (gklass
->exception_type
) {
4347 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
4351 class->vtable_size
= gklass
->vtable_size
;
4355 /* ghcimpl is not currently used
4357 if (class->parent) {
4358 MonoMethod *cmethod = class->vtable [ghc_slot];
4359 if (cmethod->is_inflated)
4360 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
4361 if (cmethod == default_ghc) {
4367 /* Interfaces and valuetypes are not supposed to have finalizers */
4368 if (!(MONO_CLASS_IS_INTERFACE (class) || class->valuetype
)) {
4369 MonoMethod
*cmethod
= NULL
;
4371 if (class->parent
&& class->parent
->has_finalize
) {
4372 class->has_finalize
= 1;
4374 if (class->type_token
) {
4375 cmethod
= find_method_in_metadata (class, "Finalize", 0, METHOD_ATTRIBUTE_VIRTUAL
);
4376 } else if (class->parent
) {
4377 /* FIXME: Optimize this */
4378 mono_class_setup_vtable (class);
4379 if (class->exception_type
|| mono_loader_get_last_error ())
4381 cmethod
= class->vtable
[finalize_slot
];
4385 /* Check that this is really the finalizer method */
4386 mono_class_setup_vtable (class);
4387 if (class->exception_type
|| mono_loader_get_last_error ())
4390 g_assert (class->vtable_size
> finalize_slot
);
4392 class->has_finalize
= 0;
4393 if (class->parent
) {
4394 cmethod
= class->vtable
[finalize_slot
];
4396 if (cmethod
->is_inflated
)
4397 cmethod
= ((MonoMethodInflated
*)cmethod
)->declaring
;
4398 if (cmethod
!= default_finalize
) {
4399 class->has_finalize
= 1;
4406 /* C# doesn't allow interfaces to have cctors */
4407 if (!MONO_CLASS_IS_INTERFACE (class) || class->image
!= mono_defaults
.corlib
) {
4408 MonoMethod
*cmethod
= NULL
;
4410 if (class->type_token
) {
4411 cmethod
= find_method_in_metadata (class, ".cctor", 0, METHOD_ATTRIBUTE_SPECIAL_NAME
);
4412 /* The find_method function ignores the 'flags' argument */
4413 if (cmethod
&& (cmethod
->flags
& METHOD_ATTRIBUTE_SPECIAL_NAME
))
4414 class->has_cctor
= 1;
4416 mono_class_setup_methods (class);
4417 if (class->exception_type
)
4420 for (i
= 0; i
< class->method
.count
; ++i
) {
4421 MonoMethod
*method
= class->methods
[i
];
4422 if ((method
->flags
& METHOD_ATTRIBUTE_SPECIAL_NAME
) &&
4423 (strcmp (".cctor", method
->name
) == 0)) {
4424 class->has_cctor
= 1;
4432 if (class->parent
) {
4433 /* This will compute class->parent->vtable_size for some classes */
4434 mono_class_init (class->parent
);
4435 if (class->parent
->exception_type
) {
4436 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
4439 if (mono_loader_get_last_error ())
4441 if (!class->parent
->vtable_size
) {
4442 /* FIXME: Get rid of this somehow */
4443 mono_class_setup_vtable (class->parent
);
4444 if (class->parent
->exception_type
) {
4445 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
4448 if (mono_loader_get_last_error ())
4451 setup_interface_offsets (class, class->parent
->vtable_size
);
4453 setup_interface_offsets (class, 0);
4459 /* Because of the double-checking locking pattern */
4460 mono_memory_barrier ();
4462 class->init_pending
= 0;
4464 if (mono_loader_get_last_error ()) {
4465 if (class->exception_type
== MONO_EXCEPTION_NONE
) {
4466 set_failure_from_loader_error (class, mono_loader_get_last_error ());
4468 mono_loader_clear_error ();
4471 mono_loader_unlock ();
4473 if (mono_debugger_class_init_func
)
4474 mono_debugger_class_init_func (class);
4476 return class->exception_type
== MONO_EXCEPTION_NONE
;
4480 is_corlib_image (MonoImage
*image
)
4482 /* FIXME: allow the dynamic case for our compilers and with full trust */
4484 return image
->assembly
&& !strcmp (image
->assembly
->aname
.name
, "mscorlib");
4486 return image
== mono_defaults
.corlib
;
4490 * LOCKING: this assumes the loader lock is held
4493 mono_class_setup_mono_type (MonoClass
*class)
4495 const char *name
= class->name
;
4496 const char *nspace
= class->name_space
;
4497 gboolean is_corlib
= is_corlib_image (class->image
);
4499 class->this_arg
.byref
= 1;
4500 class->this_arg
.data
.klass
= class;
4501 class->this_arg
.type
= MONO_TYPE_CLASS
;
4502 class->byval_arg
.data
.klass
= class;
4503 class->byval_arg
.type
= MONO_TYPE_CLASS
;
4505 if (is_corlib
&& !strcmp (nspace
, "System")) {
4506 if (!strcmp (name
, "ValueType")) {
4508 * do not set the valuetype bit for System.ValueType.
4509 * class->valuetype = 1;
4511 class->blittable
= TRUE
;
4512 } else if (!strcmp (name
, "Enum")) {
4514 * do not set the valuetype bit for System.Enum.
4515 * class->valuetype = 1;
4517 class->valuetype
= 0;
4518 class->enumtype
= 0;
4519 } else if (!strcmp (name
, "Object")) {
4520 class->this_arg
.type
= class->byval_arg
.type
= MONO_TYPE_OBJECT
;
4521 } else if (!strcmp (name
, "String")) {
4522 class->this_arg
.type
= class->byval_arg
.type
= MONO_TYPE_STRING
;
4523 } else if (!strcmp (name
, "TypedReference")) {
4524 class->this_arg
.type
= class->byval_arg
.type
= MONO_TYPE_TYPEDBYREF
;
4528 if (class->valuetype
) {
4529 int t
= MONO_TYPE_VALUETYPE
;
4531 if (is_corlib
&& !strcmp (nspace
, "System")) {
4534 if (!strcmp (name
, "Boolean")) {
4535 t
= MONO_TYPE_BOOLEAN
;
4536 } else if (!strcmp(name
, "Byte")) {
4538 class->blittable
= TRUE
;
4542 if (!strcmp (name
, "Char")) {
4547 if (!strcmp (name
, "Double")) {
4549 class->blittable
= TRUE
;
4553 if (!strcmp (name
, "Int32")) {
4555 class->blittable
= TRUE
;
4556 } else if (!strcmp(name
, "Int16")) {
4558 class->blittable
= TRUE
;
4559 } else if (!strcmp(name
, "Int64")) {
4561 class->blittable
= TRUE
;
4562 } else if (!strcmp(name
, "IntPtr")) {
4564 class->blittable
= TRUE
;
4568 if (!strcmp (name
, "Single")) {
4570 class->blittable
= TRUE
;
4571 } else if (!strcmp(name
, "SByte")) {
4573 class->blittable
= TRUE
;
4577 if (!strcmp (name
, "UInt32")) {
4579 class->blittable
= TRUE
;
4580 } else if (!strcmp(name
, "UInt16")) {
4582 class->blittable
= TRUE
;
4583 } else if (!strcmp(name
, "UInt64")) {
4585 class->blittable
= TRUE
;
4586 } else if (!strcmp(name
, "UIntPtr")) {
4588 class->blittable
= TRUE
;
4592 if (!strcmp (name
, "TypedReference")) {
4593 t
= MONO_TYPE_TYPEDBYREF
;
4594 class->blittable
= TRUE
;
4598 if (!strcmp (name
, "Void")) {
4606 class->this_arg
.type
= class->byval_arg
.type
= t
;
4609 if (MONO_CLASS_IS_INTERFACE (class))
4610 class->interface_id
= mono_get_unique_iid (class);
4615 * COM initialization (using mono_init_com_types) is delayed until needed.
4616 * However when a [ComImport] attribute is present on a type it will trigger
4617 * the initialization. This is not a problem unless the BCL being executed
4618 * lacks the types that COM depends on (e.g. Variant on Silverlight).
4621 init_com_from_comimport (MonoClass
*class)
4623 /* we don't always allow COM initialization under the CoreCLR (e.g. Moonlight does not require it) */
4624 if ((mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR
)) {
4625 /* but some other CoreCLR user could requires it for their platform (i.e. trusted) code */
4626 if (!mono_security_core_clr_determine_platform_image (class->image
)) {
4627 /* but it can not be made available for application (i.e. user code) since all COM calls
4628 * are considered native calls. In this case we fail with a TypeLoadException (just like
4629 * Silverlight 2 does */
4630 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
4634 /* FIXME : we should add an extra checks to ensure COM can be initialized properly before continuing */
4635 mono_init_com_types ();
4639 * LOCKING: this assumes the loader lock is held
4642 mono_class_setup_parent (MonoClass
*class, MonoClass
*parent
)
4644 gboolean system_namespace
;
4645 gboolean is_corlib
= is_corlib_image (class->image
);
4647 system_namespace
= !strcmp (class->name_space
, "System") && is_corlib
;
4649 /* if root of the hierarchy */
4650 if (system_namespace
&& !strcmp (class->name
, "Object")) {
4651 class->parent
= NULL
;
4652 class->instance_size
= sizeof (MonoObject
);
4655 if (!strcmp (class->name
, "<Module>")) {
4656 class->parent
= NULL
;
4657 class->instance_size
= 0;
4661 if (!MONO_CLASS_IS_INTERFACE (class)) {
4662 /* Imported COM Objects always derive from __ComObject. */
4663 if (MONO_CLASS_IS_IMPORT (class)) {
4664 init_com_from_comimport (class);
4665 if (parent
== mono_defaults
.object_class
)
4666 parent
= mono_defaults
.com_object_class
;
4669 /* set the parent to something useful and safe, but mark the type as broken */
4670 parent
= mono_defaults
.object_class
;
4671 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
4674 class->parent
= parent
;
4676 if (parent
->generic_class
&& !parent
->name
) {
4678 * If the parent is a generic instance, we may get
4679 * called before it is fully initialized, especially
4680 * before it has its name.
4685 class->marshalbyref
= parent
->marshalbyref
;
4686 class->contextbound
= parent
->contextbound
;
4687 class->delegate
= parent
->delegate
;
4688 if (MONO_CLASS_IS_IMPORT (class))
4689 class->is_com_object
= 1;
4691 class->is_com_object
= parent
->is_com_object
;
4693 if (system_namespace
) {
4694 if (*class->name
== 'M' && !strcmp (class->name
, "MarshalByRefObject"))
4695 class->marshalbyref
= 1;
4697 if (*class->name
== 'C' && !strcmp (class->name
, "ContextBoundObject"))
4698 class->contextbound
= 1;
4700 if (*class->name
== 'D' && !strcmp (class->name
, "Delegate"))
4701 class->delegate
= 1;
4704 if (class->parent
->enumtype
|| (is_corlib_image (class->parent
->image
) && (strcmp (class->parent
->name
, "ValueType") == 0) &&
4705 (strcmp (class->parent
->name_space
, "System") == 0)))
4706 class->valuetype
= 1;
4707 if (is_corlib_image (class->parent
->image
) && ((strcmp (class->parent
->name
, "Enum") == 0) && (strcmp (class->parent
->name_space
, "System") == 0))) {
4708 class->valuetype
= class->enumtype
= 1;
4710 /*class->enumtype = class->parent->enumtype; */
4711 mono_class_setup_supertypes (class);
4713 /* initialize com types if COM interfaces are present */
4714 if (MONO_CLASS_IS_IMPORT (class))
4715 init_com_from_comimport (class);
4716 class->parent
= NULL
;
4722 * mono_class_setup_supertypes:
4725 * Build the data structure needed to make fast type checks work.
4726 * This currently sets two fields in @class:
4727 * - idepth: distance between @class and System.Object in the type
4729 * - supertypes: array of classes: each element has a class in the hierarchy
4730 * starting from @class up to System.Object
4732 * LOCKING: this assumes the loader lock is held
4735 mono_class_setup_supertypes (MonoClass
*class)
4739 if (class->supertypes
)
4742 if (class->parent
&& !class->parent
->supertypes
)
4743 mono_class_setup_supertypes (class->parent
);
4745 class->idepth
= class->parent
->idepth
+ 1;
4749 ms
= MAX (MONO_DEFAULT_SUPERTABLE_SIZE
, class->idepth
);
4750 class->supertypes
= mono_image_alloc0 (class->image
, sizeof (MonoClass
*) * ms
);
4752 if (class->parent
) {
4753 class->supertypes
[class->idepth
- 1] = class;
4754 memcpy (class->supertypes
, class->parent
->supertypes
, class->parent
->idepth
* sizeof (gpointer
));
4756 class->supertypes
[0] = class;
4761 * mono_class_create_from_typedef:
4762 * @image: image where the token is valid
4763 * @type_token: typedef token
4765 * Create the MonoClass* representing the specified type token.
4766 * @type_token must be a TypeDef token.
4769 mono_class_create_from_typedef (MonoImage
*image
, guint32 type_token
)
4771 MonoTableInfo
*tt
= &image
->tables
[MONO_TABLE_TYPEDEF
];
4772 MonoClass
*class, *parent
= NULL
;
4773 guint32 cols
[MONO_TYPEDEF_SIZE
];
4774 guint32 cols_next
[MONO_TYPEDEF_SIZE
];
4775 guint tidx
= mono_metadata_token_index (type_token
);
4776 MonoGenericContext
*context
= NULL
;
4777 const char *name
, *nspace
;
4779 MonoClass
**interfaces
;
4780 guint32 field_last
, method_last
;
4781 guint32 nesting_tokeen
;
4783 if (mono_metadata_token_table (type_token
) != MONO_TABLE_TYPEDEF
|| tidx
> tt
->rows
)
4786 mono_loader_lock ();
4788 if ((class = mono_internal_hash_table_lookup (&image
->class_cache
, GUINT_TO_POINTER (type_token
)))) {
4789 mono_loader_unlock ();
4793 mono_metadata_decode_row (tt
, tidx
- 1, cols
, MONO_TYPEDEF_SIZE
);
4795 name
= mono_metadata_string_heap (image
, cols
[MONO_TYPEDEF_NAME
]);
4796 nspace
= mono_metadata_string_heap (image
, cols
[MONO_TYPEDEF_NAMESPACE
]);
4798 class = mono_image_alloc0 (image
, sizeof (MonoClass
));
4801 class->name_space
= nspace
;
4803 mono_profiler_class_event (class, MONO_PROFILE_START_LOAD
);
4805 class->image
= image
;
4806 class->type_token
= type_token
;
4807 class->flags
= cols
[MONO_TYPEDEF_FLAGS
];
4809 mono_internal_hash_table_insert (&image
->class_cache
, GUINT_TO_POINTER (type_token
), class);
4811 classes_size
+= sizeof (MonoClass
);
4814 * Check whether we're a generic type definition.
4816 class->generic_container
= mono_metadata_load_generic_params (image
, class->type_token
, NULL
);
4817 if (class->generic_container
) {
4818 class->is_generic
= 1;
4819 class->generic_container
->owner
.klass
= class;
4820 context
= &class->generic_container
->context
;
4823 if (cols
[MONO_TYPEDEF_EXTENDS
]) {
4824 guint32 parent_token
= mono_metadata_token_from_dor (cols
[MONO_TYPEDEF_EXTENDS
]);
4826 if (mono_metadata_token_table (parent_token
) == MONO_TABLE_TYPESPEC
) {
4827 /*WARNING: this must satisfy mono_metadata_type_hash*/
4828 class->this_arg
.byref
= 1;
4829 class->this_arg
.data
.klass
= class;
4830 class->this_arg
.type
= MONO_TYPE_CLASS
;
4831 class->byval_arg
.data
.klass
= class;
4832 class->byval_arg
.type
= MONO_TYPE_CLASS
;
4834 parent
= mono_class_get_full (image
, parent_token
, context
);
4836 if (parent
== NULL
){
4837 mono_internal_hash_table_remove (&image
->class_cache
, GUINT_TO_POINTER (type_token
));
4838 mono_loader_unlock ();
4839 mono_profiler_class_loaded (class, MONO_PROFILE_FAILED
);
4844 /* do this early so it's available for interfaces in setup_mono_type () */
4845 if ((nesting_tokeen
= mono_metadata_nested_in_typedef (image
, type_token
))) {
4846 class->nested_in
= mono_class_create_from_typedef (image
, nesting_tokeen
);
4847 if (!class->nested_in
) {
4848 mono_internal_hash_table_remove (&image
->class_cache
, GUINT_TO_POINTER (type_token
));
4849 mono_loader_unlock ();
4850 mono_profiler_class_loaded (class, MONO_PROFILE_FAILED
);
4855 mono_class_setup_parent (class, parent
);
4857 /* uses ->valuetype, which is initialized by mono_class_setup_parent above */
4858 mono_class_setup_mono_type (class);
4860 if ((class->flags
& TYPE_ATTRIBUTE_STRING_FORMAT_MASK
) == TYPE_ATTRIBUTE_UNICODE_CLASS
)
4864 if ((class->flags
& TYPE_ATTRIBUTE_STRING_FORMAT_MASK
) == TYPE_ATTRIBUTE_AUTO_CLASS
)
4868 class->cast_class
= class->element_class
= class;
4870 if (!class->enumtype
) {
4871 if (!mono_metadata_interfaces_from_typedef_full (
4872 image
, type_token
, &interfaces
, &icount
, FALSE
, context
)){
4873 mono_loader_unlock ();
4874 mono_profiler_class_loaded (class, MONO_PROFILE_FAILED
);
4878 class->interfaces
= interfaces
;
4879 class->interface_count
= icount
;
4880 class->interfaces_inited
= 1;
4883 /*g_print ("Load class %s\n", name);*/
4886 * Compute the field and method lists
4888 class->field
.first
= cols
[MONO_TYPEDEF_FIELD_LIST
] - 1;
4889 class->method
.first
= cols
[MONO_TYPEDEF_METHOD_LIST
] - 1;
4891 if (tt
->rows
> tidx
){
4892 mono_metadata_decode_row (tt
, tidx
, cols_next
, MONO_TYPEDEF_SIZE
);
4893 field_last
= cols_next
[MONO_TYPEDEF_FIELD_LIST
] - 1;
4894 method_last
= cols_next
[MONO_TYPEDEF_METHOD_LIST
] - 1;
4896 field_last
= image
->tables
[MONO_TABLE_FIELD
].rows
;
4897 method_last
= image
->tables
[MONO_TABLE_METHOD
].rows
;
4900 if (cols
[MONO_TYPEDEF_FIELD_LIST
] &&
4901 cols
[MONO_TYPEDEF_FIELD_LIST
] <= image
->tables
[MONO_TABLE_FIELD
].rows
)
4902 class->field
.count
= field_last
- class->field
.first
;
4904 class->field
.count
= 0;
4906 if (cols
[MONO_TYPEDEF_METHOD_LIST
] <= image
->tables
[MONO_TABLE_METHOD
].rows
)
4907 class->method
.count
= method_last
- class->method
.first
;
4909 class->method
.count
= 0;
4911 /* reserve space to store vector pointer in arrays */
4912 if (is_corlib_image (image
) && !strcmp (nspace
, "System") && !strcmp (name
, "Array")) {
4913 class->instance_size
+= 2 * sizeof (gpointer
);
4914 g_assert (class->field
.count
== 0);
4917 if (class->enumtype
) {
4918 MonoType
*enum_basetype
= mono_class_find_enum_basetype (class);
4919 if (!enum_basetype
) {
4920 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
4921 mono_loader_unlock ();
4922 mono_profiler_class_loaded (class, MONO_PROFILE_FAILED
);
4925 class->cast_class
= class->element_class
= mono_class_from_mono_type (enum_basetype
);
4929 * If we're a generic type definition, load the constraints.
4930 * We must do this after the class has been constructed to make certain recursive scenarios
4933 if (class->generic_container
&& !mono_metadata_load_generic_param_constraints_full (image
, type_token
, class->generic_container
)){
4934 char *class_name
= g_strdup_printf("%s.%s", class->name_space
, class->name
);
4935 char *error
= concat_two_strings_with_zero (class->image
, class_name
, class->image
->assembly_name
);
4936 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, error
);
4937 g_free (class_name
);
4938 mono_loader_unlock ();
4939 mono_profiler_class_loaded (class, MONO_PROFILE_FAILED
);
4943 if (class->image
->assembly_name
&& !strcmp (class->image
->assembly_name
, "Mono.Simd") && !strcmp (nspace
, "Mono.Simd")) {
4944 if (!strncmp (name
, "Vector", 6))
4945 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");
4948 mono_loader_unlock ();
4950 mono_profiler_class_loaded (class, MONO_PROFILE_OK
);
4955 /** is klass Nullable<T>? */
4957 mono_class_is_nullable (MonoClass
*klass
)
4959 return klass
->generic_class
!= NULL
&&
4960 klass
->generic_class
->container_class
== mono_defaults
.generic_nullable_class
;
4964 /** if klass is T? return T */
4966 mono_class_get_nullable_param (MonoClass
*klass
)
4968 g_assert (mono_class_is_nullable (klass
));
4969 return mono_class_from_mono_type (klass
->generic_class
->context
.class_inst
->type_argv
[0]);
4973 * Create the `MonoClass' for an instantiation of a generic type.
4974 * We only do this if we actually need it.
4977 mono_generic_class_get_class (MonoGenericClass
*gclass
)
4979 MonoClass
*klass
, *gklass
;
4981 mono_loader_lock ();
4982 if (gclass
->cached_class
) {
4983 mono_loader_unlock ();
4984 return gclass
->cached_class
;
4987 gclass
->cached_class
= g_malloc0 (sizeof (MonoClass
));
4988 klass
= gclass
->cached_class
;
4990 gklass
= gclass
->container_class
;
4992 if (gklass
->nested_in
) {
4994 * FIXME: the nested type context should include everything the
4995 * nesting context should have, but it may also have additional
4996 * generic parameters...
4998 klass
->nested_in
= mono_class_inflate_generic_class (gklass
->nested_in
,
4999 mono_generic_class_get_context (gclass
));
5002 klass
->name
= gklass
->name
;
5003 klass
->name_space
= gklass
->name_space
;
5005 mono_profiler_class_event (klass
, MONO_PROFILE_START_LOAD
);
5007 klass
->image
= gklass
->image
;
5008 klass
->flags
= gklass
->flags
;
5009 klass
->type_token
= gklass
->type_token
;
5010 klass
->field
.count
= gklass
->field
.count
;
5012 klass
->is_inflated
= 1;
5013 klass
->generic_class
= gclass
;
5015 klass
->this_arg
.type
= klass
->byval_arg
.type
= MONO_TYPE_GENERICINST
;
5016 klass
->this_arg
.data
.generic_class
= klass
->byval_arg
.data
.generic_class
= gclass
;
5017 klass
->this_arg
.byref
= TRUE
;
5018 klass
->enumtype
= gklass
->enumtype
;
5019 klass
->valuetype
= gklass
->valuetype
;
5021 klass
->cast_class
= klass
->element_class
= klass
;
5023 if (mono_class_is_nullable (klass
))
5024 klass
->cast_class
= klass
->element_class
= mono_class_get_nullable_param (klass
);
5027 * We're not interested in the nested classes of a generic instance.
5028 * We use the generic type definition to look for nested classes.
5031 if (gklass
->parent
) {
5032 klass
->parent
= mono_class_inflate_generic_class (gklass
->parent
, mono_generic_class_get_context (gclass
));
5036 mono_class_setup_parent (klass
, klass
->parent
);
5038 if (klass
->enumtype
) {
5039 klass
->cast_class
= gklass
->cast_class
;
5040 klass
->element_class
= gklass
->element_class
;
5043 if (gclass
->is_dynamic
) {
5046 mono_class_setup_supertypes (klass
);
5048 if (klass
->enumtype
) {
5050 * For enums, gklass->fields might not been set, but instance_size etc. is
5051 * already set in mono_reflection_create_internal_class (). For non-enums,
5052 * these will be computed normally in mono_class_layout_fields ().
5054 klass
->instance_size
= gklass
->instance_size
;
5055 klass
->sizes
.class_size
= gklass
->sizes
.class_size
;
5056 klass
->size_inited
= 1;
5060 mono_profiler_class_loaded (klass
, MONO_PROFILE_OK
);
5062 inflated_classes
++;
5063 inflated_classes_size
+= sizeof (MonoClass
);
5065 mono_loader_unlock ();
5071 make_generic_param_class (MonoGenericParam
*param
, MonoImage
*image
, gboolean is_mvar
, MonoGenericParamInfo
*pinfo
)
5073 MonoClass
*klass
, **ptr
;
5075 MonoGenericContainer
*container
= mono_generic_param_owner (param
);
5079 image
= mono_defaults
.corlib
;
5081 klass
= mono_image_alloc0 (image
, sizeof (MonoClass
));
5082 classes_size
+= sizeof (MonoClass
);
5085 klass
->name
= pinfo
->name
;
5087 int n
= mono_generic_param_num (param
);
5088 klass
->name
= mono_image_alloc0 (image
, 16);
5089 sprintf ((char*)klass
->name
, "%d", n
);
5094 MonoMethod
*omethod
= container
->owner
.method
;
5095 klass
->name_space
= (omethod
&& omethod
->klass
) ? omethod
->klass
->name_space
: "";
5097 MonoClass
*oklass
= container
->owner
.klass
;
5098 klass
->name_space
= oklass
? oklass
->name_space
: "";
5101 klass
->name_space
= "";
5104 mono_profiler_class_event (klass
, MONO_PROFILE_START_LOAD
);
5108 for (ptr
= pinfo
->constraints
; ptr
&& *ptr
; ptr
++, count
++)
5112 if ((count
> 0) && !MONO_CLASS_IS_INTERFACE (pinfo
->constraints
[0])) {
5113 klass
->parent
= pinfo
->constraints
[0];
5115 } else if (pinfo
&& pinfo
->flags
& GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT
)
5116 klass
->parent
= mono_class_from_name (mono_defaults
.corlib
, "System", "ValueType");
5118 klass
->parent
= mono_defaults
.object_class
;
5121 if (count
- pos
> 0) {
5122 klass
->interface_count
= count
- pos
;
5123 klass
->interfaces
= mono_image_alloc0 (image
, sizeof (MonoClass
*) * (count
- pos
));
5124 klass
->interfaces_inited
= TRUE
;
5125 for (i
= pos
; i
< count
; i
++)
5126 klass
->interfaces
[i
- pos
] = pinfo
->constraints
[i
];
5129 klass
->image
= image
;
5131 klass
->inited
= TRUE
;
5132 klass
->cast_class
= klass
->element_class
= klass
;
5133 klass
->flags
= TYPE_ATTRIBUTE_PUBLIC
;
5135 klass
->this_arg
.type
= klass
->byval_arg
.type
= is_mvar
? MONO_TYPE_MVAR
: MONO_TYPE_VAR
;
5136 klass
->this_arg
.data
.generic_param
= klass
->byval_arg
.data
.generic_param
= param
;
5137 klass
->this_arg
.byref
= TRUE
;
5139 /* FIXME: shouldn't this be ->type_token? */
5140 klass
->sizes
.generic_param_token
= pinfo
? pinfo
->token
: 0;
5142 mono_class_setup_supertypes (klass
);
5144 if (count
- pos
> 0) {
5145 mono_class_setup_vtable (klass
->parent
);
5146 g_assert (!klass
->parent
->exception_type
);
5147 setup_interface_offsets (klass
, klass
->parent
->vtable_size
);
5153 #define FAST_CACHE_SIZE 16
5154 static MonoClass
*var_cache_fast
[FAST_CACHE_SIZE
];
5155 static MonoClass
*mvar_cache_fast
[FAST_CACHE_SIZE
];
5156 static GHashTable
*var_cache_slow
;
5157 static GHashTable
*mvar_cache_slow
;
5160 get_anon_gparam_class (MonoGenericParam
*param
, gboolean is_mvar
)
5162 int n
= mono_generic_param_num (param
);
5165 if (n
< FAST_CACHE_SIZE
)
5166 return (is_mvar
? mvar_cache_fast
: var_cache_fast
) [n
];
5167 ht
= is_mvar
? mvar_cache_slow
: var_cache_slow
;
5168 return ht
? g_hash_table_lookup (ht
, GINT_TO_POINTER (n
)) : NULL
;
5172 set_anon_gparam_class (MonoGenericParam
*param
, gboolean is_mvar
, MonoClass
*klass
)
5174 int n
= mono_generic_param_num (param
);
5177 if (n
< FAST_CACHE_SIZE
) {
5178 (is_mvar
? mvar_cache_fast
: var_cache_fast
) [n
] = klass
;
5181 ht
= is_mvar
? mvar_cache_slow
: var_cache_slow
;
5183 ht
= g_hash_table_new (NULL
, NULL
);
5185 mvar_cache_slow
= ht
;
5187 var_cache_slow
= ht
;
5190 g_hash_table_insert (ht
, GINT_TO_POINTER (n
), klass
);
5194 * LOCKING: Acquires the loader lock.
5197 mono_class_from_generic_parameter (MonoGenericParam
*param
, MonoImage
*image
, gboolean is_mvar
)
5199 MonoGenericContainer
*container
= mono_generic_param_owner (param
);
5200 MonoGenericParamInfo
*pinfo
;
5203 mono_loader_lock ();
5206 pinfo
= mono_generic_param_info (param
);
5207 if (pinfo
->pklass
) {
5208 mono_loader_unlock ();
5209 return pinfo
->pklass
;
5215 klass
= get_anon_gparam_class (param
, is_mvar
);
5217 mono_loader_unlock ();
5222 if (!image
&& container
) {
5224 MonoMethod
*method
= container
->owner
.method
;
5225 image
= (method
&& method
->klass
) ? method
->klass
->image
: NULL
;
5227 MonoClass
*klass
= container
->owner
.klass
;
5228 // FIXME: 'klass' should not be null
5229 // But, monodis creates GenericContainers without associating a owner to it
5230 image
= klass
? klass
->image
: NULL
;
5234 klass
= make_generic_param_class (param
, image
, is_mvar
, pinfo
);
5236 mono_memory_barrier ();
5239 pinfo
->pklass
= klass
;
5241 set_anon_gparam_class (param
, is_mvar
, klass
);
5243 mono_loader_unlock ();
5245 /* FIXME: Should this go inside 'make_generic_param_klass'? */
5246 mono_profiler_class_loaded (klass
, MONO_PROFILE_OK
);
5252 mono_ptr_class_get (MonoType
*type
)
5255 MonoClass
*el_class
;
5259 el_class
= mono_class_from_mono_type (type
);
5260 image
= el_class
->image
;
5262 mono_loader_lock ();
5264 if (!image
->ptr_cache
)
5265 image
->ptr_cache
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5267 if ((result
= g_hash_table_lookup (image
->ptr_cache
, el_class
))) {
5268 mono_loader_unlock ();
5271 result
= mono_image_alloc0 (image
, sizeof (MonoClass
));
5273 classes_size
+= sizeof (MonoClass
);
5275 result
->parent
= NULL
; /* no parent for PTR types */
5276 result
->name_space
= el_class
->name_space
;
5277 name
= g_strdup_printf ("%s*", el_class
->name
);
5278 result
->name
= mono_image_strdup (image
, name
);
5281 mono_profiler_class_event (result
, MONO_PROFILE_START_LOAD
);
5283 result
->image
= el_class
->image
;
5284 result
->inited
= TRUE
;
5285 result
->flags
= TYPE_ATTRIBUTE_CLASS
| (el_class
->flags
& TYPE_ATTRIBUTE_VISIBILITY_MASK
);
5286 /* Can pointers get boxed? */
5287 result
->instance_size
= sizeof (gpointer
);
5288 result
->cast_class
= result
->element_class
= el_class
;
5289 result
->blittable
= TRUE
;
5291 result
->this_arg
.type
= result
->byval_arg
.type
= MONO_TYPE_PTR
;
5292 result
->this_arg
.data
.type
= result
->byval_arg
.data
.type
= &result
->element_class
->byval_arg
;
5293 result
->this_arg
.byref
= TRUE
;
5295 mono_class_setup_supertypes (result
);
5297 g_hash_table_insert (image
->ptr_cache
, el_class
, result
);
5299 mono_loader_unlock ();
5301 mono_profiler_class_loaded (result
, MONO_PROFILE_OK
);
5307 mono_fnptr_class_get (MonoMethodSignature
*sig
)
5310 static GHashTable
*ptr_hash
= NULL
;
5312 /* FIXME: These should be allocate from a mempool as well, but which one ? */
5314 mono_loader_lock ();
5317 ptr_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5319 if ((result
= g_hash_table_lookup (ptr_hash
, sig
))) {
5320 mono_loader_unlock ();
5323 result
= g_new0 (MonoClass
, 1);
5325 result
->parent
= NULL
; /* no parent for PTR types */
5326 result
->name_space
= "System";
5327 result
->name
= "MonoFNPtrFakeClass";
5329 mono_profiler_class_event (result
, MONO_PROFILE_START_LOAD
);
5331 result
->image
= mono_defaults
.corlib
; /* need to fix... */
5332 result
->inited
= TRUE
;
5333 result
->flags
= TYPE_ATTRIBUTE_CLASS
; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
5334 /* Can pointers get boxed? */
5335 result
->instance_size
= sizeof (gpointer
);
5336 result
->cast_class
= result
->element_class
= result
;
5337 result
->blittable
= TRUE
;
5339 result
->this_arg
.type
= result
->byval_arg
.type
= MONO_TYPE_FNPTR
;
5340 result
->this_arg
.data
.method
= result
->byval_arg
.data
.method
= sig
;
5341 result
->this_arg
.byref
= TRUE
;
5342 result
->blittable
= TRUE
;
5344 mono_class_setup_supertypes (result
);
5346 g_hash_table_insert (ptr_hash
, sig
, result
);
5348 mono_loader_unlock ();
5350 mono_profiler_class_loaded (result
, MONO_PROFILE_OK
);
5356 mono_class_from_mono_type (MonoType
*type
)
5358 switch (type
->type
) {
5359 case MONO_TYPE_OBJECT
:
5360 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.object_class
;
5361 case MONO_TYPE_VOID
:
5362 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.void_class
;
5363 case MONO_TYPE_BOOLEAN
:
5364 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.boolean_class
;
5365 case MONO_TYPE_CHAR
:
5366 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.char_class
;
5368 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.sbyte_class
;
5370 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.byte_class
;
5372 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.int16_class
;
5374 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.uint16_class
;
5376 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.int32_class
;
5378 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.uint32_class
;
5380 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.int_class
;
5382 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.uint_class
;
5384 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.int64_class
;
5386 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.uint64_class
;
5388 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.single_class
;
5390 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.double_class
;
5391 case MONO_TYPE_STRING
:
5392 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.string_class
;
5393 case MONO_TYPE_TYPEDBYREF
:
5394 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.typed_reference_class
;
5395 case MONO_TYPE_ARRAY
:
5396 return mono_bounded_array_class_get (type
->data
.array
->eklass
, type
->data
.array
->rank
, TRUE
);
5398 return mono_ptr_class_get (type
->data
.type
);
5399 case MONO_TYPE_FNPTR
:
5400 return mono_fnptr_class_get (type
->data
.method
);
5401 case MONO_TYPE_SZARRAY
:
5402 return mono_array_class_get (type
->data
.klass
, 1);
5403 case MONO_TYPE_CLASS
:
5404 case MONO_TYPE_VALUETYPE
:
5405 return type
->data
.klass
;
5406 case MONO_TYPE_GENERICINST
:
5407 return mono_generic_class_get_class (type
->data
.generic_class
);
5409 return mono_class_from_generic_parameter (type
->data
.generic_param
, NULL
, FALSE
);
5410 case MONO_TYPE_MVAR
:
5411 return mono_class_from_generic_parameter (type
->data
.generic_param
, NULL
, TRUE
);
5413 g_warning ("mono_class_from_mono_type: implement me 0x%02x\n", type
->type
);
5414 g_assert_not_reached ();
5421 * mono_type_retrieve_from_typespec
5422 * @image: context where the image is created
5423 * @type_spec: typespec token
5424 * @context: the generic context used to evaluate generic instantiations in
5427 mono_type_retrieve_from_typespec (MonoImage
*image
, guint32 type_spec
, MonoGenericContext
*context
, gboolean
*did_inflate
, MonoError
*error
)
5429 MonoType
*t
= mono_type_create_from_typespec (image
, type_spec
);
5431 mono_error_init (error
);
5432 *did_inflate
= FALSE
;
5435 char *name
= mono_class_name_from_token (image
, type_spec
);
5436 char *assembly
= mono_assembly_name_from_token (image
, type_spec
);
5437 mono_error_set_type_load_name (error
, name
, assembly
, "Could not resolve typespec token %08x", type_spec
);
5441 if (context
&& (context
->class_inst
|| context
->method_inst
)) {
5442 MonoType
*inflated
= inflate_generic_type (NULL
, t
, context
, error
);
5444 if (!mono_error_ok (error
))
5449 *did_inflate
= TRUE
;
5456 * mono_class_create_from_typespec
5457 * @image: context where the image is created
5458 * @type_spec: typespec token
5459 * @context: the generic context used to evaluate generic instantiations in
5462 mono_class_create_from_typespec (MonoImage
*image
, guint32 type_spec
, MonoGenericContext
*context
, MonoError
*error
)
5465 gboolean inflated
= FALSE
;
5466 MonoType
*t
= mono_type_retrieve_from_typespec (image
, type_spec
, context
, &inflated
, error
);
5467 if (!mono_error_ok (error
))
5469 ret
= mono_class_from_mono_type (t
);
5471 mono_metadata_free_type (t
);
5476 * mono_bounded_array_class_get:
5477 * @element_class: element class
5478 * @rank: the dimension of the array class
5479 * @bounded: whenever the array has non-zero bounds
5481 * Returns: a class object describing the array with element type @element_type and
5485 mono_bounded_array_class_get (MonoClass
*eclass
, guint32 rank
, gboolean bounded
)
5489 MonoClass
*parent
= NULL
;
5490 GSList
*list
, *rootlist
= NULL
;
5493 gboolean corlib_type
= FALSE
;
5495 g_assert (rank
<= 255);
5498 /* bounded only matters for one-dimensional arrays */
5501 image
= eclass
->image
;
5503 if (rank
== 1 && !bounded
) {
5505 * This case is very frequent not just during compilation because of calls
5506 * from mono_class_from_mono_type (), mono_array_new (),
5507 * Array:CreateInstance (), etc, so use a separate cache + a separate lock.
5509 EnterCriticalSection (&image
->szarray_cache_lock
);
5510 if (!image
->szarray_cache
)
5511 image
->szarray_cache
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5512 class = g_hash_table_lookup (image
->szarray_cache
, eclass
);
5513 LeaveCriticalSection (&image
->szarray_cache_lock
);
5517 mono_loader_lock ();
5519 mono_loader_lock ();
5521 if (!image
->array_cache
)
5522 image
->array_cache
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5524 if ((rootlist
= list
= g_hash_table_lookup (image
->array_cache
, eclass
))) {
5525 for (; list
; list
= list
->next
) {
5527 if ((class->rank
== rank
) && (class->byval_arg
.type
== (((rank
> 1) || bounded
) ? MONO_TYPE_ARRAY
: MONO_TYPE_SZARRAY
))) {
5528 mono_loader_unlock ();
5535 /* for the building corlib use System.Array from it */
5536 if (image
->assembly
&& image
->assembly
->dynamic
&& image
->assembly_name
&& strcmp (image
->assembly_name
, "mscorlib") == 0) {
5537 parent
= mono_class_from_name (image
, "System", "Array");
5540 parent
= mono_defaults
.array_class
;
5541 if (!parent
->inited
)
5542 mono_class_init (parent
);
5545 class = mono_image_alloc0 (image
, sizeof (MonoClass
));
5547 class->image
= image
;
5548 class->name_space
= eclass
->name_space
;
5549 nsize
= strlen (eclass
->name
);
5550 name
= g_malloc (nsize
+ 2 + rank
+ 1);
5551 memcpy (name
, eclass
->name
, nsize
);
5554 memset (name
+ nsize
+ 1, ',', rank
- 1);
5556 name
[nsize
+ rank
] = '*';
5557 name
[nsize
+ rank
+ bounded
] = ']';
5558 name
[nsize
+ rank
+ bounded
+ 1] = 0;
5559 class->name
= mono_image_strdup (image
, name
);
5562 mono_profiler_class_event (class, MONO_PROFILE_START_LOAD
);
5564 classes_size
+= sizeof (MonoClass
);
5566 class->type_token
= 0;
5567 /* all arrays are marked serializable and sealed, bug #42779 */
5568 class->flags
= TYPE_ATTRIBUTE_CLASS
| TYPE_ATTRIBUTE_SERIALIZABLE
| TYPE_ATTRIBUTE_SEALED
| TYPE_ATTRIBUTE_PUBLIC
;
5569 class->parent
= parent
;
5570 class->instance_size
= mono_class_instance_size (class->parent
);
5572 if (eclass
->enumtype
&& !mono_class_enum_basetype (eclass
)) {
5573 if (!eclass
->reflection_info
|| eclass
->wastypebuilder
) {
5574 g_warning ("Only incomplete TypeBuilder objects are allowed to be an enum without base_type");
5575 g_assert (eclass
->reflection_info
&& !eclass
->wastypebuilder
);
5577 /* element_size -1 is ok as this is not an instantitable type*/
5578 class->sizes
.element_size
= -1;
5580 class->sizes
.element_size
= mono_class_array_element_size (eclass
);
5582 mono_class_setup_supertypes (class);
5584 if (eclass
->generic_class
)
5585 mono_class_init (eclass
);
5586 if (!eclass
->size_inited
)
5587 mono_class_setup_fields (eclass
);
5588 if (eclass
->exception_type
) /*FIXME we fail the array type, but we have to let other fields be set.*/
5589 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
5591 class->has_references
= MONO_TYPE_IS_REFERENCE (&eclass
->byval_arg
) || eclass
->has_references
? TRUE
: FALSE
;
5595 if (eclass
->enumtype
)
5596 class->cast_class
= eclass
->element_class
;
5598 class->cast_class
= eclass
;
5600 class->element_class
= eclass
;
5602 if ((rank
> 1) || bounded
) {
5603 MonoArrayType
*at
= mono_image_alloc0 (image
, sizeof (MonoArrayType
));
5604 class->byval_arg
.type
= MONO_TYPE_ARRAY
;
5605 class->byval_arg
.data
.array
= at
;
5606 at
->eklass
= eclass
;
5608 /* FIXME: complete.... */
5610 class->byval_arg
.type
= MONO_TYPE_SZARRAY
;
5611 class->byval_arg
.data
.klass
= eclass
;
5613 class->this_arg
= class->byval_arg
;
5614 class->this_arg
.byref
= 1;
5619 class->generic_container
= eclass
->generic_container
;
5621 if (rank
== 1 && !bounded
) {
5622 MonoClass
*prev_class
;
5624 EnterCriticalSection (&image
->szarray_cache_lock
);
5625 prev_class
= g_hash_table_lookup (image
->szarray_cache
, eclass
);
5627 /* Someone got in before us */
5630 g_hash_table_insert (image
->szarray_cache
, eclass
, class);
5631 LeaveCriticalSection (&image
->szarray_cache_lock
);
5633 list
= g_slist_append (rootlist
, class);
5634 g_hash_table_insert (image
->array_cache
, eclass
, list
);
5637 mono_loader_unlock ();
5639 mono_profiler_class_loaded (class, MONO_PROFILE_OK
);
5645 * mono_array_class_get:
5646 * @element_class: element class
5647 * @rank: the dimension of the array class
5649 * Returns: a class object describing the array with element type @element_type and
5653 mono_array_class_get (MonoClass
*eclass
, guint32 rank
)
5655 return mono_bounded_array_class_get (eclass
, rank
, FALSE
);
5659 * mono_class_instance_size:
5662 * Returns: the size of an object instance
5665 mono_class_instance_size (MonoClass
*klass
)
5667 if (!klass
->size_inited
)
5668 mono_class_init (klass
);
5670 return klass
->instance_size
;
5674 * mono_class_min_align:
5677 * Returns: minimm alignment requirements
5680 mono_class_min_align (MonoClass
*klass
)
5682 if (!klass
->size_inited
)
5683 mono_class_init (klass
);
5685 return klass
->min_align
;
5689 * mono_class_value_size:
5692 * This function is used for value types, and return the
5693 * space and the alignment to store that kind of value object.
5695 * Returns: the size of a value of kind @klass
5698 mono_class_value_size (MonoClass
*klass
, guint32
*align
)
5702 /* fixme: check disable, because we still have external revereces to
5703 * mscorlib and Dummy Objects
5705 /*g_assert (klass->valuetype);*/
5707 size
= mono_class_instance_size (klass
) - sizeof (MonoObject
);
5710 *align
= klass
->min_align
;
5716 * mono_class_data_size:
5719 * Returns: the size of the static class data
5722 mono_class_data_size (MonoClass
*klass
)
5725 mono_class_init (klass
);
5727 /* in arrays, sizes.class_size is unioned with element_size
5728 * and arrays have no static fields
5732 return klass
->sizes
.class_size
;
5736 * Auxiliary routine to mono_class_get_field
5738 * Takes a field index instead of a field token.
5740 static MonoClassField
*
5741 mono_class_get_field_idx (MonoClass
*class, int idx
)
5743 mono_class_setup_fields_locking (class);
5744 if (class->exception_type
)
5748 if (class->image
->uncompressed_metadata
) {
5750 * class->field.first points to the FieldPtr table, while idx points into the
5751 * Field table, so we have to do a search.
5753 /*FIXME this is broken for types with multiple fields with the same name.*/
5754 const char *name
= mono_metadata_string_heap (class->image
, mono_metadata_decode_row_col (&class->image
->tables
[MONO_TABLE_FIELD
], idx
, MONO_FIELD_NAME
));
5757 for (i
= 0; i
< class->field
.count
; ++i
)
5758 if (mono_field_get_name (&class->fields
[i
]) == name
)
5759 return &class->fields
[i
];
5760 g_assert_not_reached ();
5762 if (class->field
.count
) {
5763 if ((idx
>= class->field
.first
) && (idx
< class->field
.first
+ class->field
.count
)){
5764 return &class->fields
[idx
- class->field
.first
];
5768 class = class->parent
;
5774 * mono_class_get_field:
5775 * @class: the class to lookup the field.
5776 * @field_token: the field token
5778 * Returns: A MonoClassField representing the type and offset of
5779 * the field, or a NULL value if the field does not belong to this
5783 mono_class_get_field (MonoClass
*class, guint32 field_token
)
5785 int idx
= mono_metadata_token_index (field_token
);
5787 g_assert (mono_metadata_token_code (field_token
) == MONO_TOKEN_FIELD_DEF
);
5789 return mono_class_get_field_idx (class, idx
- 1);
5793 * mono_class_get_field_from_name:
5794 * @klass: the class to lookup the field.
5795 * @name: the field name
5797 * Search the class @klass and it's parents for a field with the name @name.
5799 * Returns: the MonoClassField pointer of the named field or NULL
5802 mono_class_get_field_from_name (MonoClass
*klass
, const char *name
)
5804 return mono_class_get_field_from_name_full (klass
, name
, NULL
);
5808 * mono_class_get_field_from_name_full:
5809 * @klass: the class to lookup the field.
5810 * @name: the field name
5811 * @type: the type of the fields. This optional.
5813 * Search the class @klass and it's parents for a field with the name @name and type @type.
5815 * If @klass is an inflated generic type, the type comparison is done with the equivalent field
5816 * of its generic type definition.
5818 * Returns: the MonoClassField pointer of the named field or NULL
5821 mono_class_get_field_from_name_full (MonoClass
*klass
, const char *name
, MonoType
*type
)
5825 mono_class_setup_fields_locking (klass
);
5826 if (klass
->exception_type
)
5830 for (i
= 0; i
< klass
->field
.count
; ++i
) {
5831 MonoClassField
*field
= &klass
->fields
[i
];
5833 if (strcmp (name
, mono_field_get_name (field
)) != 0)
5837 MonoType
*field_type
= mono_metadata_get_corresponding_field_from_generic_type_definition (field
)->type
;
5838 if (!mono_metadata_type_equal_full (type
, field_type
, TRUE
))
5843 klass
= klass
->parent
;
5849 * mono_class_get_field_token:
5850 * @field: the field we need the token of
5852 * Get the token of a field. Note that the tokesn is only valid for the image
5853 * the field was loaded from. Don't use this function for fields in dynamic types.
5855 * Returns: the token representing the field in the image it was loaded from.
5858 mono_class_get_field_token (MonoClassField
*field
)
5860 MonoClass
*klass
= field
->parent
;
5863 mono_class_setup_fields_locking (klass
);
5864 if (klass
->exception_type
)
5868 for (i
= 0; i
< klass
->field
.count
; ++i
) {
5869 if (&klass
->fields
[i
] == field
) {
5870 int idx
= klass
->field
.first
+ i
+ 1;
5872 if (klass
->image
->uncompressed_metadata
)
5873 idx
= mono_metadata_translate_token_index (klass
->image
, MONO_TABLE_FIELD
, idx
);
5874 return mono_metadata_make_token (MONO_TABLE_FIELD
, idx
);
5877 klass
= klass
->parent
;
5880 g_assert_not_reached ();
5885 mono_field_get_index (MonoClassField
*field
)
5887 int index
= field
- field
->parent
->fields
;
5889 g_assert (index
>= 0 && index
< field
->parent
->field
.count
);
5895 * mono_class_get_field_default_value:
5897 * Return the default value of the field as a pointer into the metadata blob.
5900 mono_class_get_field_default_value (MonoClassField
*field
, MonoTypeEnum
*def_type
)
5903 guint32 constant_cols
[MONO_CONSTANT_SIZE
];
5905 MonoClass
*klass
= field
->parent
;
5907 g_assert (field
->type
->attrs
& FIELD_ATTRIBUTE_HAS_DEFAULT
);
5909 if (!klass
->ext
|| !klass
->ext
->field_def_values
) {
5910 mono_loader_lock ();
5911 mono_class_alloc_ext (klass
);
5912 if (!klass
->ext
->field_def_values
)
5913 klass
->ext
->field_def_values
= mono_image_alloc0 (klass
->image
, sizeof (MonoFieldDefaultValue
) * klass
->field
.count
);
5914 mono_loader_unlock ();
5917 field_index
= mono_field_get_index (field
);
5919 if (!klass
->ext
->field_def_values
[field_index
].data
) {
5920 cindex
= mono_metadata_get_constant_index (field
->parent
->image
, mono_class_get_field_token (field
), 0);
5922 g_assert (!(field
->type
->attrs
& FIELD_ATTRIBUTE_HAS_FIELD_RVA
));
5924 mono_metadata_decode_row (&field
->parent
->image
->tables
[MONO_TABLE_CONSTANT
], cindex
- 1, constant_cols
, MONO_CONSTANT_SIZE
);
5925 klass
->ext
->field_def_values
[field_index
].def_type
= constant_cols
[MONO_CONSTANT_TYPE
];
5926 klass
->ext
->field_def_values
[field_index
].data
= (gpointer
)mono_metadata_blob_heap (field
->parent
->image
, constant_cols
[MONO_CONSTANT_VALUE
]);
5929 *def_type
= klass
->ext
->field_def_values
[field_index
].def_type
;
5930 return klass
->ext
->field_def_values
[field_index
].data
;
5934 * mono_class_get_property_default_value:
5936 * Return the default value of the field as a pointer into the metadata blob.
5939 mono_class_get_property_default_value (MonoProperty
*property
, MonoTypeEnum
*def_type
)
5942 guint32 constant_cols
[MONO_CONSTANT_SIZE
];
5943 MonoClass
*klass
= property
->parent
;
5945 g_assert (property
->attrs
& PROPERTY_ATTRIBUTE_HAS_DEFAULT
);
5946 /*We don't cache here because it is not used by C# so it's quite rare.*/
5948 cindex
= mono_metadata_get_constant_index (klass
->image
, mono_class_get_property_token (property
), 0);
5952 mono_metadata_decode_row (&klass
->image
->tables
[MONO_TABLE_CONSTANT
], cindex
- 1, constant_cols
, MONO_CONSTANT_SIZE
);
5953 *def_type
= constant_cols
[MONO_CONSTANT_TYPE
];
5954 return (gpointer
)mono_metadata_blob_heap (klass
->image
, constant_cols
[MONO_CONSTANT_VALUE
]);
5958 mono_class_get_event_token (MonoEvent
*event
)
5960 MonoClass
*klass
= event
->parent
;
5965 for (i
= 0; i
< klass
->ext
->event
.count
; ++i
) {
5966 if (&klass
->ext
->events
[i
] == event
)
5967 return mono_metadata_make_token (MONO_TABLE_EVENT
, klass
->ext
->event
.first
+ i
+ 1);
5970 klass
= klass
->parent
;
5973 g_assert_not_reached ();
5978 mono_class_get_property_from_name (MonoClass
*klass
, const char *name
)
5982 gpointer iter
= NULL
;
5983 while ((p
= mono_class_get_properties (klass
, &iter
))) {
5984 if (! strcmp (name
, p
->name
))
5987 klass
= klass
->parent
;
5993 mono_class_get_property_token (MonoProperty
*prop
)
5995 MonoClass
*klass
= prop
->parent
;
5999 gpointer iter
= NULL
;
6000 while ((p
= mono_class_get_properties (klass
, &iter
))) {
6001 if (&klass
->ext
->properties
[i
] == prop
)
6002 return mono_metadata_make_token (MONO_TABLE_PROPERTY
, klass
->ext
->property
.first
+ i
+ 1);
6006 klass
= klass
->parent
;
6009 g_assert_not_reached ();
6014 mono_class_name_from_token (MonoImage
*image
, guint32 type_token
)
6016 const char *name
, *nspace
;
6018 return g_strdup_printf ("DynamicType 0x%08x", type_token
);
6020 switch (type_token
& 0xff000000){
6021 case MONO_TOKEN_TYPE_DEF
: {
6022 guint32 cols
[MONO_TYPEDEF_SIZE
];
6023 MonoTableInfo
*tt
= &image
->tables
[MONO_TABLE_TYPEDEF
];
6024 guint tidx
= mono_metadata_token_index (type_token
);
6026 if (tidx
> tt
->rows
)
6027 return g_strdup_printf ("Invalid type token 0x%08x", type_token
);
6029 mono_metadata_decode_row (tt
, tidx
- 1, cols
, MONO_TYPEDEF_SIZE
);
6030 name
= mono_metadata_string_heap (image
, cols
[MONO_TYPEDEF_NAME
]);
6031 nspace
= mono_metadata_string_heap (image
, cols
[MONO_TYPEDEF_NAMESPACE
]);
6032 if (strlen (nspace
) == 0)
6033 return g_strdup_printf ("%s", name
);
6035 return g_strdup_printf ("%s.%s", nspace
, name
);
6038 case MONO_TOKEN_TYPE_REF
: {
6039 guint32 cols
[MONO_TYPEREF_SIZE
];
6040 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_TYPEREF
];
6041 guint tidx
= mono_metadata_token_index (type_token
);
6044 return g_strdup_printf ("Invalid type token 0x%08x", type_token
);
6045 mono_metadata_decode_row (t
, tidx
-1, cols
, MONO_TYPEREF_SIZE
);
6046 name
= mono_metadata_string_heap (image
, cols
[MONO_TYPEREF_NAME
]);
6047 nspace
= mono_metadata_string_heap (image
, cols
[MONO_TYPEREF_NAMESPACE
]);
6048 if (strlen (nspace
) == 0)
6049 return g_strdup_printf ("%s", name
);
6051 return g_strdup_printf ("%s.%s", nspace
, name
);
6054 case MONO_TOKEN_TYPE_SPEC
:
6055 return g_strdup_printf ("Typespec 0x%08x", type_token
);
6057 return g_strdup_printf ("Invalid type token 0x%08x", type_token
);
6062 mono_assembly_name_from_token (MonoImage
*image
, guint32 type_token
)
6065 return g_strdup_printf ("DynamicAssembly %s", image
->name
);
6067 switch (type_token
& 0xff000000){
6068 case MONO_TOKEN_TYPE_DEF
:
6069 return mono_stringify_assembly_name (&image
->assembly
->aname
);
6071 case MONO_TOKEN_TYPE_REF
: {
6072 MonoAssemblyName aname
;
6073 guint32 cols
[MONO_TYPEREF_SIZE
];
6074 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_TYPEREF
];
6075 guint32 idx
= mono_metadata_token_index (type_token
);
6078 return g_strdup_printf ("Invalid type token 0x%08x", type_token
);
6080 mono_metadata_decode_row (t
, idx
-1, cols
, MONO_TYPEREF_SIZE
);
6082 idx
= cols
[MONO_TYPEREF_SCOPE
] >> MONO_RESOLTION_SCOPE_BITS
;
6083 switch (cols
[MONO_TYPEREF_SCOPE
] & MONO_RESOLTION_SCOPE_MASK
) {
6084 case MONO_RESOLTION_SCOPE_MODULE
:
6086 return g_strdup ("");
6087 case MONO_RESOLTION_SCOPE_MODULEREF
:
6089 return g_strdup ("");
6090 case MONO_RESOLTION_SCOPE_TYPEREF
:
6092 return g_strdup ("");
6093 case MONO_RESOLTION_SCOPE_ASSEMBLYREF
:
6094 mono_assembly_get_assemblyref (image
, idx
- 1, &aname
);
6095 return mono_stringify_assembly_name (&aname
);
6097 g_assert_not_reached ();
6101 case MONO_TOKEN_TYPE_SPEC
:
6103 return g_strdup ("");
6105 g_assert_not_reached ();
6112 * mono_class_get_full:
6113 * @image: the image where the class resides
6114 * @type_token: the token for the class
6115 * @context: the generic context used to evaluate generic instantiations in
6117 * Returns: the MonoClass that represents @type_token in @image
6120 mono_class_get_full (MonoImage
*image
, guint32 type_token
, MonoGenericContext
*context
)
6123 MonoClass
*class = NULL
;
6125 if (image
->dynamic
) {
6126 int table
= mono_metadata_token_table (type_token
);
6128 if (table
!= MONO_TABLE_TYPEDEF
&& table
!= MONO_TABLE_TYPEREF
&& table
!= MONO_TABLE_TYPESPEC
) {
6129 mono_loader_set_error_bad_image (g_strdup ("Bad type token."));
6132 return mono_lookup_dynamic_token (image
, type_token
, context
);
6135 switch (type_token
& 0xff000000){
6136 case MONO_TOKEN_TYPE_DEF
:
6137 class = mono_class_create_from_typedef (image
, type_token
);
6139 case MONO_TOKEN_TYPE_REF
:
6140 class = mono_class_from_typeref (image
, type_token
);
6142 case MONO_TOKEN_TYPE_SPEC
:
6143 class = mono_class_create_from_typespec (image
, type_token
, context
, &error
);
6144 if (!mono_error_ok (&error
)) {
6145 /*FIXME don't swallow the error message*/
6146 mono_error_cleanup (&error
);
6150 g_warning ("unknown token type %x", type_token
& 0xff000000);
6151 g_assert_not_reached ();
6155 char *name
= mono_class_name_from_token (image
, type_token
);
6156 char *assembly
= mono_assembly_name_from_token (image
, type_token
);
6157 mono_loader_set_error_type_load (name
, assembly
);
6165 * mono_type_get_full:
6166 * @image: the image where the type resides
6167 * @type_token: the token for the type
6168 * @context: the generic context used to evaluate generic instantiations in
6170 * This functions exists to fullfill the fact that sometimes it's desirable to have access to the
6172 * Returns: the MonoType that represents @type_token in @image
6175 mono_type_get_full (MonoImage
*image
, guint32 type_token
, MonoGenericContext
*context
)
6178 MonoType
*type
= NULL
;
6179 gboolean inflated
= FALSE
;
6181 //FIXME: this will not fix the very issue for which mono_type_get_full exists -but how to do it then?
6183 return mono_class_get_type (mono_lookup_dynamic_token (image
, type_token
, context
));
6185 if ((type_token
& 0xff000000) != MONO_TOKEN_TYPE_SPEC
) {
6186 MonoClass
*class = mono_class_get_full (image
, type_token
, context
);
6187 return class ? mono_class_get_type (class) : NULL
;
6190 type
= mono_type_retrieve_from_typespec (image
, type_token
, context
, &inflated
, &error
);
6192 if (!mono_error_ok (&error
)) {
6193 /*FIXME don't swalloc the error message.*/
6194 char *name
= mono_class_name_from_token (image
, type_token
);
6195 char *assembly
= mono_assembly_name_from_token (image
, type_token
);
6197 g_warning ("Error loading type %s from %s due to %s", name
, assembly
, mono_error_get_message (&error
));
6199 mono_error_cleanup (&error
);
6200 mono_loader_set_error_type_load (name
, assembly
);
6205 MonoType
*tmp
= type
;
6206 type
= mono_class_get_type (mono_class_from_mono_type (type
));
6207 /* FIXME: This is a workaround fo the fact that a typespec token sometimes reference to the generic type definition.
6208 * A MonoClass::byval_arg of a generic type definion has type CLASS.
6209 * Some parts of mono create a GENERICINST to reference a generic type definition and this generates confict with byval_arg.
6211 * The long term solution is to chaise this places and make then set MonoType::type correctly.
6213 if (type
->type
!= tmp
->type
)
6216 mono_metadata_free_type (tmp
);
6223 mono_class_get (MonoImage
*image
, guint32 type_token
)
6225 return mono_class_get_full (image
, type_token
, NULL
);
6229 * mono_image_init_name_cache:
6231 * Initializes the class name cache stored in image->name_cache.
6233 * LOCKING: Acquires the corresponding image lock.
6236 mono_image_init_name_cache (MonoImage
*image
)
6238 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_TYPEDEF
];
6239 guint32 cols
[MONO_TYPEDEF_SIZE
];
6242 guint32 i
, visib
, nspace_index
;
6243 GHashTable
*name_cache2
, *nspace_table
;
6245 mono_image_lock (image
);
6247 if (image
->name_cache
) {
6248 mono_image_unlock (image
);
6252 image
->name_cache
= g_hash_table_new (g_str_hash
, g_str_equal
);
6254 if (image
->dynamic
) {
6255 mono_image_unlock (image
);
6259 /* Temporary hash table to avoid lookups in the nspace_table */
6260 name_cache2
= g_hash_table_new (NULL
, NULL
);
6262 for (i
= 1; i
<= t
->rows
; ++i
) {
6263 mono_metadata_decode_row (t
, i
- 1, cols
, MONO_TYPEDEF_SIZE
);
6264 visib
= cols
[MONO_TYPEDEF_FLAGS
] & TYPE_ATTRIBUTE_VISIBILITY_MASK
;
6266 * Nested types are accessed from the nesting name. We use the fact that nested types use different visibility flags
6267 * than toplevel types, thus avoiding the need to grovel through the NESTED_TYPE table
6269 if (visib
>= TYPE_ATTRIBUTE_NESTED_PUBLIC
&& visib
<= TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM
)
6271 name
= mono_metadata_string_heap (image
, cols
[MONO_TYPEDEF_NAME
]);
6272 nspace
= mono_metadata_string_heap (image
, cols
[MONO_TYPEDEF_NAMESPACE
]);
6274 nspace_index
= cols
[MONO_TYPEDEF_NAMESPACE
];
6275 nspace_table
= g_hash_table_lookup (name_cache2
, GUINT_TO_POINTER (nspace_index
));
6276 if (!nspace_table
) {
6277 nspace_table
= g_hash_table_new (g_str_hash
, g_str_equal
);
6278 g_hash_table_insert (image
->name_cache
, (char*)nspace
, nspace_table
);
6279 g_hash_table_insert (name_cache2
, GUINT_TO_POINTER (nspace_index
),
6282 g_hash_table_insert (nspace_table
, (char *) name
, GUINT_TO_POINTER (i
));
6285 /* Load type names from EXPORTEDTYPES table */
6287 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_EXPORTEDTYPE
];
6288 guint32 cols
[MONO_EXP_TYPE_SIZE
];
6291 for (i
= 0; i
< t
->rows
; ++i
) {
6292 mono_metadata_decode_row (t
, i
, cols
, MONO_EXP_TYPE_SIZE
);
6293 name
= mono_metadata_string_heap (image
, cols
[MONO_EXP_TYPE_NAME
]);
6294 nspace
= mono_metadata_string_heap (image
, cols
[MONO_EXP_TYPE_NAMESPACE
]);
6296 nspace_index
= cols
[MONO_EXP_TYPE_NAMESPACE
];
6297 nspace_table
= g_hash_table_lookup (name_cache2
, GUINT_TO_POINTER (nspace_index
));
6298 if (!nspace_table
) {
6299 nspace_table
= g_hash_table_new (g_str_hash
, g_str_equal
);
6300 g_hash_table_insert (image
->name_cache
, (char*)nspace
, nspace_table
);
6301 g_hash_table_insert (name_cache2
, GUINT_TO_POINTER (nspace_index
),
6304 g_hash_table_insert (nspace_table
, (char *) name
, GUINT_TO_POINTER (mono_metadata_make_token (MONO_TABLE_EXPORTEDTYPE
, i
+ 1)));
6308 g_hash_table_destroy (name_cache2
);
6309 mono_image_unlock (image
);
6312 /*FIXME Only dynamic assemblies should allow this operation.*/
6314 mono_image_add_to_name_cache (MonoImage
*image
, const char *nspace
,
6315 const char *name
, guint32 index
)
6317 GHashTable
*nspace_table
;
6318 GHashTable
*name_cache
;
6321 mono_image_lock (image
);
6323 if (!image
->name_cache
)
6324 mono_image_init_name_cache (image
);
6326 name_cache
= image
->name_cache
;
6327 if (!(nspace_table
= g_hash_table_lookup (name_cache
, nspace
))) {
6328 nspace_table
= g_hash_table_new (g_str_hash
, g_str_equal
);
6329 g_hash_table_insert (name_cache
, (char *)nspace
, (char *)nspace_table
);
6332 if ((old_index
= GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table
, (char*) name
))))
6333 g_error ("overrwritting old token %x on image %s for type %s::%s", old_index
, image
->name
, nspace
, name
);
6335 g_hash_table_insert (nspace_table
, (char *) name
, GUINT_TO_POINTER (index
));
6337 mono_image_unlock (image
);
6346 find_nocase (gpointer key
, gpointer value
, gpointer user_data
)
6348 char *name
= (char*)key
;
6349 FindUserData
*data
= (FindUserData
*)user_data
;
6351 if (!data
->value
&& (mono_utf8_strcasecmp (name
, (char*)data
->key
) == 0))
6352 data
->value
= value
;
6356 * mono_class_from_name_case:
6357 * @image: The MonoImage where the type is looked up in
6358 * @name_space: the type namespace
6359 * @name: the type short name.
6361 * Obtains a MonoClass with a given namespace and a given name which
6362 * is located in the given MonoImage. The namespace and name
6363 * lookups are case insensitive.
6366 mono_class_from_name_case (MonoImage
*image
, const char* name_space
, const char *name
)
6368 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_TYPEDEF
];
6369 guint32 cols
[MONO_TYPEDEF_SIZE
];
6374 if (image
->dynamic
) {
6376 FindUserData user_data
;
6378 mono_image_lock (image
);
6380 if (!image
->name_cache
)
6381 mono_image_init_name_cache (image
);
6383 user_data
.key
= name_space
;
6384 user_data
.value
= NULL
;
6385 g_hash_table_foreach (image
->name_cache
, find_nocase
, &user_data
);
6387 if (user_data
.value
) {
6388 GHashTable
*nspace_table
= (GHashTable
*)user_data
.value
;
6390 user_data
.key
= name
;
6391 user_data
.value
= NULL
;
6393 g_hash_table_foreach (nspace_table
, find_nocase
, &user_data
);
6395 if (user_data
.value
)
6396 token
= GPOINTER_TO_UINT (user_data
.value
);
6399 mono_image_unlock (image
);
6402 return mono_class_get (image
, MONO_TOKEN_TYPE_DEF
| token
);
6408 /* add a cache if needed */
6409 for (i
= 1; i
<= t
->rows
; ++i
) {
6410 mono_metadata_decode_row (t
, i
- 1, cols
, MONO_TYPEDEF_SIZE
);
6411 visib
= cols
[MONO_TYPEDEF_FLAGS
] & TYPE_ATTRIBUTE_VISIBILITY_MASK
;
6413 * Nested types are accessed from the nesting name. We use the fact that nested types use different visibility flags
6414 * than toplevel types, thus avoiding the need to grovel through the NESTED_TYPE table
6416 if (visib
>= TYPE_ATTRIBUTE_NESTED_PUBLIC
&& visib
<= TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM
)
6418 n
= mono_metadata_string_heap (image
, cols
[MONO_TYPEDEF_NAME
]);
6419 nspace
= mono_metadata_string_heap (image
, cols
[MONO_TYPEDEF_NAMESPACE
]);
6420 if (mono_utf8_strcasecmp (n
, name
) == 0 && mono_utf8_strcasecmp (nspace
, name_space
) == 0)
6421 return mono_class_get (image
, MONO_TOKEN_TYPE_DEF
| i
);
6427 return_nested_in (MonoClass
*class, char *nested
)
6430 char *s
= strchr (nested
, '/');
6431 gpointer iter
= NULL
;
6438 while ((found
= mono_class_get_nested_types (class, &iter
))) {
6439 if (strcmp (found
->name
, nested
) == 0) {
6441 return return_nested_in (found
, s
);
6449 search_modules (MonoImage
*image
, const char *name_space
, const char *name
)
6451 MonoTableInfo
*file_table
= &image
->tables
[MONO_TABLE_FILE
];
6452 MonoImage
*file_image
;
6457 * The EXPORTEDTYPES table only contains public types, so have to search the
6459 * Note: image->modules contains the contents of the MODULEREF table, while
6460 * the real module list is in the FILE table.
6462 for (i
= 0; i
< file_table
->rows
; i
++) {
6463 guint32 cols
[MONO_FILE_SIZE
];
6464 mono_metadata_decode_row (file_table
, i
, cols
, MONO_FILE_SIZE
);
6465 if (cols
[MONO_FILE_FLAGS
] == FILE_CONTAINS_NO_METADATA
)
6468 file_image
= mono_image_load_file_for_image (image
, i
+ 1);
6470 class = mono_class_from_name (file_image
, name_space
, name
);
6480 * mono_class_from_name:
6481 * @image: The MonoImage where the type is looked up in
6482 * @name_space: the type namespace
6483 * @name: the type short name.
6485 * Obtains a MonoClass with a given namespace and a given name which
6486 * is located in the given MonoImage.
6489 mono_class_from_name (MonoImage
*image
, const char* name_space
, const char *name
)
6491 GHashTable
*nspace_table
;
6492 MonoImage
*loaded_image
;
6499 if ((nested
= strchr (name
, '/'))) {
6500 int pos
= nested
- name
;
6501 int len
= strlen (name
);
6504 memcpy (buf
, name
, len
+ 1);
6506 nested
= buf
+ pos
+ 1;
6510 if (get_class_from_name
) {
6511 gboolean res
= get_class_from_name (image
, name_space
, name
, &class);
6514 class = search_modules (image
, name_space
, name
);
6516 return class ? return_nested_in (class, nested
) : NULL
;
6522 mono_image_lock (image
);
6524 if (!image
->name_cache
)
6525 mono_image_init_name_cache (image
);
6527 nspace_table
= g_hash_table_lookup (image
->name_cache
, name_space
);
6530 token
= GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table
, name
));
6532 mono_image_unlock (image
);
6534 if (!token
&& image
->dynamic
&& image
->modules
) {
6535 /* Search modules as well */
6536 for (i
= 0; i
< image
->module_count
; ++i
) {
6537 MonoImage
*module
= image
->modules
[i
];
6539 class = mono_class_from_name (module
, name_space
, name
);
6546 class = search_modules (image
, name_space
, name
);
6554 if (mono_metadata_token_table (token
) == MONO_TABLE_EXPORTEDTYPE
) {
6555 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_EXPORTEDTYPE
];
6556 guint32 cols
[MONO_EXP_TYPE_SIZE
];
6559 idx
= mono_metadata_token_index (token
);
6561 mono_metadata_decode_row (t
, idx
- 1, cols
, MONO_EXP_TYPE_SIZE
);
6563 impl
= cols
[MONO_EXP_TYPE_IMPLEMENTATION
];
6564 if ((impl
& MONO_IMPLEMENTATION_MASK
) == MONO_IMPLEMENTATION_FILE
) {
6565 loaded_image
= mono_assembly_load_module (image
->assembly
, impl
>> MONO_IMPLEMENTATION_BITS
);
6568 class = mono_class_from_name (loaded_image
, name_space
, name
);
6570 return return_nested_in (class, nested
);
6572 } else if ((impl
& MONO_IMPLEMENTATION_MASK
) == MONO_IMPLEMENTATION_ASSEMBLYREF
) {
6573 guint32 assembly_idx
;
6575 assembly_idx
= impl
>> MONO_IMPLEMENTATION_BITS
;
6577 mono_assembly_load_reference (image
, assembly_idx
- 1);
6578 g_assert (image
->references
[assembly_idx
- 1]);
6579 if (image
->references
[assembly_idx
- 1] == (gpointer
)-1)
6582 /* FIXME: Cycle detection */
6583 return mono_class_from_name (image
->references
[assembly_idx
- 1]->image
, name_space
, name
);
6585 g_error ("not yet implemented");
6589 token
= MONO_TOKEN_TYPE_DEF
| token
;
6591 class = mono_class_get (image
, token
);
6593 return return_nested_in (class, nested
);
6597 /*FIXME test for interfaces with variant generic arguments*/
6599 mono_class_is_subclass_of (MonoClass
*klass
, MonoClass
*klassc
,
6600 gboolean check_interfaces
)
6602 g_assert (klassc
->idepth
> 0);
6603 if (check_interfaces
&& MONO_CLASS_IS_INTERFACE (klassc
) && !MONO_CLASS_IS_INTERFACE (klass
)) {
6604 if (MONO_CLASS_IMPLEMENTS_INTERFACE (klass
, klassc
->interface_id
))
6606 } else if (check_interfaces
&& MONO_CLASS_IS_INTERFACE (klassc
) && MONO_CLASS_IS_INTERFACE (klass
)) {
6609 for (i
= 0; i
< klass
->interface_count
; i
++) {
6610 MonoClass
*ic
= klass
->interfaces
[i
];
6615 if (!MONO_CLASS_IS_INTERFACE (klass
) && mono_class_has_parent (klass
, klassc
))
6620 * MS.NET thinks interfaces are a subclass of Object, so we think it as
6623 if (klassc
== mono_defaults
.object_class
)
6630 mono_class_has_variant_generic_params (MonoClass
*klass
)
6633 MonoGenericContainer
*container
;
6635 if (!klass
->generic_class
)
6638 container
= klass
->generic_class
->container_class
->generic_container
;
6640 for (i
= 0; i
< container
->type_argc
; ++i
)
6641 if (mono_generic_container_get_param_info (container
, i
)->flags
& (MONO_GEN_PARAM_VARIANT
|MONO_GEN_PARAM_COVARIANT
))
6648 * @container the generic container from the GTD
6649 * @klass: the class to be assigned to
6650 * @oklass: the source class
6652 * Both klass and oklass must be instances of the same generic interface.
6653 * Return true if @klass can be assigned to a @klass variable
6656 mono_class_is_variant_compatible (MonoClass
*klass
, MonoClass
*oklass
)
6659 MonoType
**klass_argv
, **oklass_argv
;
6660 MonoClass
*klass_gtd
= mono_class_get_generic_type_definition (klass
);
6661 MonoGenericContainer
*container
= klass_gtd
->generic_container
;
6663 /*Viable candidates are instances of the same generic interface*/
6664 if (mono_class_get_generic_type_definition (oklass
) != klass_gtd
)
6667 klass_argv
= &klass
->generic_class
->context
.class_inst
->type_argv
[0];
6668 oklass_argv
= &oklass
->generic_class
->context
.class_inst
->type_argv
[0];
6670 for (j
= 0; j
< container
->type_argc
; ++j
) {
6671 MonoClass
*param1_class
= mono_class_from_mono_type (klass_argv
[j
]);
6672 MonoClass
*param2_class
= mono_class_from_mono_type (oklass_argv
[j
]);
6674 if (param1_class
->valuetype
!= param2_class
->valuetype
)
6678 * The _VARIANT and _COVARIANT constants should read _COVARIANT and
6679 * _CONTRAVARIANT, but they are in a public header so we can't fix it.
6681 if (param1_class
!= param2_class
) {
6682 if (mono_generic_container_get_param_info (container
, j
)->flags
& MONO_GEN_PARAM_VARIANT
) {
6683 if (!mono_class_is_assignable_from (param1_class
, param2_class
))
6685 } else if (mono_generic_container_get_param_info (container
, j
)->flags
& MONO_GEN_PARAM_COVARIANT
) {
6686 if (!mono_class_is_assignable_from (param2_class
, param1_class
))
6696 * mono_class_is_assignable_from:
6697 * @klass: the class to be assigned to
6698 * @oklass: the source class
6700 * Return: true if an instance of object oklass can be assigned to an
6701 * instance of object @klass
6704 mono_class_is_assignable_from (MonoClass
*klass
, MonoClass
*oklass
)
6707 mono_class_init (klass
);
6709 if (!oklass
->inited
)
6710 mono_class_init (oklass
);
6712 if ((klass
->byval_arg
.type
== MONO_TYPE_VAR
) || (klass
->byval_arg
.type
== MONO_TYPE_MVAR
))
6713 return klass
== oklass
;
6715 if (MONO_CLASS_IS_INTERFACE (klass
)) {
6716 if ((oklass
->byval_arg
.type
== MONO_TYPE_VAR
) || (oklass
->byval_arg
.type
== MONO_TYPE_MVAR
))
6719 /* interface_offsets might not be set for dynamic classes */
6720 if (oklass
->reflection_info
&& !oklass
->interface_bitmap
)
6722 * oklass might be a generic type parameter but they have
6723 * interface_offsets set.
6725 return mono_reflection_call_is_assignable_to (oklass
, klass
);
6726 if (!oklass
->interface_bitmap
)
6727 /* Happens with generic instances of not-yet created dynamic types */
6729 if (MONO_CLASS_IMPLEMENTS_INTERFACE (oklass
, klass
->interface_id
))
6732 if (mono_class_has_variant_generic_params (klass
)) {
6735 mono_class_setup_interfaces (oklass
, &error
);
6736 if (!mono_error_ok (&error
)) {
6737 mono_error_cleanup (&error
);
6741 /*klass is a generic variant interface, We need to extract from oklass a list of ifaces which are viable candidates.*/
6742 for (i
= 0; i
< oklass
->interface_offsets_count
; ++i
) {
6743 MonoClass
*iface
= oklass
->interfaces_packed
[i
];
6745 if (mono_class_is_variant_compatible (klass
, iface
))
6750 } else if (klass
->delegate
) {
6751 if (mono_class_has_variant_generic_params (klass
) && mono_class_is_variant_compatible (klass
, oklass
))
6753 }else if (klass
->rank
) {
6754 MonoClass
*eclass
, *eoclass
;
6756 if (oklass
->rank
!= klass
->rank
)
6759 /* vectors vs. one dimensional arrays */
6760 if (oklass
->byval_arg
.type
!= klass
->byval_arg
.type
)
6763 eclass
= klass
->cast_class
;
6764 eoclass
= oklass
->cast_class
;
6767 * a is b does not imply a[] is b[] when a is a valuetype, and
6768 * b is a reference type.
6771 if (eoclass
->valuetype
) {
6772 if ((eclass
== mono_defaults
.enum_class
) ||
6773 (eclass
== mono_defaults
.enum_class
->parent
) ||
6774 (eclass
== mono_defaults
.object_class
))
6778 return mono_class_is_assignable_from (klass
->cast_class
, oklass
->cast_class
);
6779 } else if (mono_class_is_nullable (klass
)) {
6780 if (mono_class_is_nullable (oklass
))
6781 return mono_class_is_assignable_from (klass
->cast_class
, oklass
->cast_class
);
6783 return mono_class_is_assignable_from (klass
->cast_class
, oklass
);
6784 } else if (klass
== mono_defaults
.object_class
)
6787 return mono_class_has_parent (oklass
, klass
);
6790 /*Check if @oklass is variant compatible with @klass.*/
6792 mono_class_is_variant_compatible_slow (MonoClass
*klass
, MonoClass
*oklass
)
6795 MonoType
**klass_argv
, **oklass_argv
;
6796 MonoClass
*klass_gtd
= mono_class_get_generic_type_definition (klass
);
6797 MonoGenericContainer
*container
= klass_gtd
->generic_container
;
6799 /*Viable candidates are instances of the same generic interface*/
6800 if (mono_class_get_generic_type_definition (oklass
) != klass_gtd
)
6803 klass_argv
= &klass
->generic_class
->context
.class_inst
->type_argv
[0];
6804 oklass_argv
= &oklass
->generic_class
->context
.class_inst
->type_argv
[0];
6806 for (j
= 0; j
< container
->type_argc
; ++j
) {
6807 MonoClass
*param1_class
= mono_class_from_mono_type (klass_argv
[j
]);
6808 MonoClass
*param2_class
= mono_class_from_mono_type (oklass_argv
[j
]);
6810 if (param1_class
->valuetype
!= param2_class
->valuetype
)
6814 * The _VARIANT and _COVARIANT constants should read _COVARIANT and
6815 * _CONTRAVARIANT, but they are in a public header so we can't fix it.
6817 if (param1_class
!= param2_class
) {
6818 if (mono_generic_container_get_param_info (container
, j
)->flags
& MONO_GEN_PARAM_VARIANT
) {
6819 if (!mono_class_is_assignable_from_slow (param1_class
, param2_class
))
6821 } else if (mono_generic_container_get_param_info (container
, j
)->flags
& MONO_GEN_PARAM_COVARIANT
) {
6822 if (!mono_class_is_assignable_from_slow (param2_class
, param1_class
))
6830 /*Check if @candidate implements the interface @target*/
6832 mono_class_implement_interface_slow (MonoClass
*target
, MonoClass
*candidate
)
6836 gboolean is_variant
= mono_class_has_variant_generic_params (target
);
6838 if (is_variant
&& MONO_CLASS_IS_INTERFACE (candidate
)) {
6839 if (mono_class_is_variant_compatible_slow (target
, candidate
))
6844 if (candidate
== target
)
6847 /*A TypeBuilder can have more interfaces on tb->interfaces than on candidate->interfaces*/
6848 if (candidate
->image
->dynamic
&& !candidate
->wastypebuilder
) {
6849 MonoReflectionTypeBuilder
*tb
= candidate
->reflection_info
;
6851 if (tb
->interfaces
) {
6852 for (j
= mono_array_length (tb
->interfaces
) - 1; j
>= 0; --j
) {
6853 MonoReflectionType
*iface
= mono_array_get (tb
->interfaces
, MonoReflectionType
*, j
);
6854 MonoClass
*iface_class
= mono_class_from_mono_type (iface
->type
);
6855 if (iface_class
== target
)
6857 if (is_variant
&& mono_class_is_variant_compatible_slow (target
, iface_class
))
6859 if (mono_class_implement_interface_slow (target
, iface_class
))
6864 /*setup_interfaces don't mono_class_init anything*/
6865 mono_class_setup_interfaces (candidate
, &error
);
6866 if (!mono_error_ok (&error
)) {
6867 mono_error_cleanup (&error
);
6871 for (i
= 0; i
< candidate
->interface_count
; ++i
) {
6872 if (candidate
->interfaces
[i
] == target
)
6875 if (is_variant
&& mono_class_is_variant_compatible_slow (target
, candidate
->interfaces
[i
]))
6878 if (mono_class_implement_interface_slow (target
, candidate
->interfaces
[i
]))
6882 candidate
= candidate
->parent
;
6883 } while (candidate
);
6889 * Check if @oklass can be assigned to @klass.
6890 * This function does the same as mono_class_is_assignable_from but is safe to be used from mono_class_init context.
6893 mono_class_is_assignable_from_slow (MonoClass
*target
, MonoClass
*candidate
)
6895 if (candidate
== target
)
6897 if (target
== mono_defaults
.object_class
)
6900 /*setup_supertypes don't mono_class_init anything */
6901 mono_class_setup_supertypes (candidate
);
6902 mono_class_setup_supertypes (target
);
6904 if (mono_class_has_parent (candidate
, target
))
6907 /*If target is not an interface there is no need to check them.*/
6908 if (MONO_CLASS_IS_INTERFACE (target
))
6909 return mono_class_implement_interface_slow (target
, candidate
);
6911 if (target
->delegate
&& mono_class_has_variant_generic_params (target
))
6912 return mono_class_is_variant_compatible (target
, candidate
);
6914 /*FIXME properly handle nullables and arrays */
6920 * mono_class_get_cctor:
6921 * @klass: A MonoClass pointer
6923 * Returns: the static constructor of @klass if it exists, NULL otherwise.
6926 mono_class_get_cctor (MonoClass
*klass
)
6928 MonoCachedClassInfo cached_info
;
6930 if (klass
->image
->dynamic
) {
6932 * has_cctor is not set for these classes because mono_class_init () is
6935 return mono_class_get_method_from_name_flags (klass
, ".cctor", -1, METHOD_ATTRIBUTE_SPECIAL_NAME
);
6938 if (!klass
->has_cctor
)
6941 if (mono_class_get_cached_class_info (klass
, &cached_info
))
6942 return mono_get_method (klass
->image
, cached_info
.cctor_token
, klass
);
6944 if (klass
->generic_class
&& !klass
->methods
)
6945 return mono_class_get_inflated_method (klass
, mono_class_get_cctor (klass
->generic_class
->container_class
));
6947 return mono_class_get_method_from_name_flags (klass
, ".cctor", -1, METHOD_ATTRIBUTE_SPECIAL_NAME
);
6951 * mono_class_get_finalizer:
6952 * @klass: The MonoClass pointer
6954 * Returns: the finalizer method of @klass if it exists, NULL otherwise.
6957 mono_class_get_finalizer (MonoClass
*klass
)
6959 MonoCachedClassInfo cached_info
;
6962 mono_class_init (klass
);
6963 if (!klass
->has_finalize
)
6966 if (mono_class_get_cached_class_info (klass
, &cached_info
))
6967 return mono_get_method (cached_info
.finalize_image
, cached_info
.finalize_token
, NULL
);
6969 mono_class_setup_vtable (klass
);
6970 return klass
->vtable
[finalize_slot
];
6975 * mono_class_needs_cctor_run:
6976 * @klass: the MonoClass pointer
6977 * @caller: a MonoMethod describing the caller
6979 * Determines whenever the class has a static constructor and whenever it
6980 * needs to be called when executing CALLER.
6983 mono_class_needs_cctor_run (MonoClass
*klass
, MonoMethod
*caller
)
6987 method
= mono_class_get_cctor (klass
);
6989 return (method
== caller
) ? FALSE
: TRUE
;
6995 * mono_class_array_element_size:
6998 * Returns: the number of bytes an element of type @klass
6999 * uses when stored into an array.
7002 mono_class_array_element_size (MonoClass
*klass
)
7004 MonoType
*type
= &klass
->byval_arg
;
7007 switch (type
->type
) {
7010 case MONO_TYPE_BOOLEAN
:
7014 case MONO_TYPE_CHAR
:
7023 case MONO_TYPE_CLASS
:
7024 case MONO_TYPE_STRING
:
7025 case MONO_TYPE_OBJECT
:
7026 case MONO_TYPE_SZARRAY
:
7027 case MONO_TYPE_ARRAY
:
7029 case MONO_TYPE_MVAR
:
7030 return sizeof (gpointer
);
7035 case MONO_TYPE_VALUETYPE
:
7036 if (type
->data
.klass
->enumtype
) {
7037 type
= mono_class_enum_basetype (type
->data
.klass
);
7038 klass
= klass
->element_class
;
7041 return mono_class_instance_size (klass
) - sizeof (MonoObject
);
7042 case MONO_TYPE_GENERICINST
:
7043 type
= &type
->data
.generic_class
->container_class
->byval_arg
;
7046 case MONO_TYPE_VOID
:
7050 g_error ("unknown type 0x%02x in mono_class_array_element_size", type
->type
);
7056 * mono_array_element_size:
7057 * @ac: pointer to a #MonoArrayClass
7059 * Returns: the size of single array element.
7062 mono_array_element_size (MonoClass
*ac
)
7064 g_assert (ac
->rank
);
7065 return ac
->sizes
.element_size
;
7069 mono_ldtoken (MonoImage
*image
, guint32 token
, MonoClass
**handle_class
,
7070 MonoGenericContext
*context
)
7072 if (image
->dynamic
) {
7073 MonoClass
*tmp_handle_class
;
7074 gpointer obj
= mono_lookup_dynamic_token_class (image
, token
, TRUE
, &tmp_handle_class
, context
);
7076 g_assert (tmp_handle_class
);
7078 *handle_class
= tmp_handle_class
;
7080 if (tmp_handle_class
== mono_defaults
.typehandle_class
)
7081 return &((MonoClass
*)obj
)->byval_arg
;
7086 switch (token
& 0xff000000) {
7087 case MONO_TOKEN_TYPE_DEF
:
7088 case MONO_TOKEN_TYPE_REF
:
7089 case MONO_TOKEN_TYPE_SPEC
: {
7092 *handle_class
= mono_defaults
.typehandle_class
;
7093 type
= mono_type_get_full (image
, token
, context
);
7096 mono_class_init (mono_class_from_mono_type (type
));
7097 /* We return a MonoType* as handle */
7100 case MONO_TOKEN_FIELD_DEF
: {
7102 guint32 type
= mono_metadata_typedef_from_field (image
, mono_metadata_token_index (token
));
7106 *handle_class
= mono_defaults
.fieldhandle_class
;
7107 class = mono_class_get_full (image
, MONO_TOKEN_TYPE_DEF
| type
, context
);
7110 mono_class_init (class);
7111 return mono_class_get_field (class, token
);
7113 case MONO_TOKEN_METHOD_DEF
:
7114 case MONO_TOKEN_METHOD_SPEC
: {
7116 meth
= mono_get_method_full (image
, token
, NULL
, context
);
7118 *handle_class
= mono_defaults
.methodhandle_class
;
7121 case MONO_TOKEN_MEMBER_REF
: {
7122 guint32 cols
[MONO_MEMBERREF_SIZE
];
7124 mono_metadata_decode_row (&image
->tables
[MONO_TABLE_MEMBERREF
], mono_metadata_token_index (token
) - 1, cols
, MONO_MEMBERREF_SIZE
);
7125 sig
= mono_metadata_blob_heap (image
, cols
[MONO_MEMBERREF_SIGNATURE
]);
7126 mono_metadata_decode_blob_size (sig
, &sig
);
7127 if (*sig
== 0x6) { /* it's a field */
7129 MonoClassField
*field
;
7130 field
= mono_field_from_token (image
, token
, &klass
, context
);
7132 *handle_class
= mono_defaults
.fieldhandle_class
;
7136 meth
= mono_get_method_full (image
, token
, NULL
, context
);
7138 *handle_class
= mono_defaults
.methodhandle_class
;
7143 g_warning ("Unknown token 0x%08x in ldtoken", token
);
7150 * This function might need to call runtime functions so it can't be part
7151 * of the metadata library.
7153 static MonoLookupDynamicToken lookup_dynamic
= NULL
;
7156 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func
)
7158 lookup_dynamic
= func
;
7162 mono_lookup_dynamic_token (MonoImage
*image
, guint32 token
, MonoGenericContext
*context
)
7164 MonoClass
*handle_class
;
7166 return lookup_dynamic (image
, token
, TRUE
, &handle_class
, context
);
7170 mono_lookup_dynamic_token_class (MonoImage
*image
, guint32 token
, gboolean valid_token
, MonoClass
**handle_class
, MonoGenericContext
*context
)
7172 return lookup_dynamic (image
, token
, valid_token
, handle_class
, context
);
7175 static MonoGetCachedClassInfo get_cached_class_info
= NULL
;
7178 mono_install_get_cached_class_info (MonoGetCachedClassInfo func
)
7180 get_cached_class_info
= func
;
7184 mono_class_get_cached_class_info (MonoClass
*klass
, MonoCachedClassInfo
*res
)
7186 if (!get_cached_class_info
)
7189 return get_cached_class_info (klass
, res
);
7193 mono_install_get_class_from_name (MonoGetClassFromName func
)
7195 get_class_from_name
= func
;
7199 mono_class_get_image (MonoClass
*klass
)
7201 return klass
->image
;
7205 * mono_class_get_element_class:
7206 * @klass: the MonoClass to act on
7208 * Returns: the element class of an array or an enumeration.
7211 mono_class_get_element_class (MonoClass
*klass
)
7213 return klass
->element_class
;
7217 * mono_class_is_valuetype:
7218 * @klass: the MonoClass to act on
7220 * Returns: true if the MonoClass represents a ValueType.
7223 mono_class_is_valuetype (MonoClass
*klass
)
7225 return klass
->valuetype
;
7229 * mono_class_is_enum:
7230 * @klass: the MonoClass to act on
7232 * Returns: true if the MonoClass represents an enumeration.
7235 mono_class_is_enum (MonoClass
*klass
)
7237 return klass
->enumtype
;
7241 * mono_class_enum_basetype:
7242 * @klass: the MonoClass to act on
7244 * Returns: the underlying type representation for an enumeration.
7247 mono_class_enum_basetype (MonoClass
*klass
)
7249 if (klass
->element_class
== klass
)
7250 /* SRE or broken types */
7253 return &klass
->element_class
->byval_arg
;
7257 * mono_class_get_parent
7258 * @klass: the MonoClass to act on
7260 * Returns: the parent class for this class.
7263 mono_class_get_parent (MonoClass
*klass
)
7265 return klass
->parent
;
7269 * mono_class_get_nesting_type;
7270 * @klass: the MonoClass to act on
7272 * Returns: the container type where this type is nested or NULL if this type is not a nested type.
7275 mono_class_get_nesting_type (MonoClass
*klass
)
7277 return klass
->nested_in
;
7281 * mono_class_get_rank:
7282 * @klass: the MonoClass to act on
7284 * Returns: the rank for the array (the number of dimensions).
7287 mono_class_get_rank (MonoClass
*klass
)
7293 * mono_class_get_flags:
7294 * @klass: the MonoClass to act on
7296 * The type flags from the TypeDef table from the metadata.
7297 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
7300 * Returns: the flags from the TypeDef table.
7303 mono_class_get_flags (MonoClass
*klass
)
7305 return klass
->flags
;
7309 * mono_class_get_name
7310 * @klass: the MonoClass to act on
7312 * Returns: the name of the class.
7315 mono_class_get_name (MonoClass
*klass
)
7321 * mono_class_get_namespace:
7322 * @klass: the MonoClass to act on
7324 * Returns: the namespace of the class.
7327 mono_class_get_namespace (MonoClass
*klass
)
7329 return klass
->name_space
;
7333 * mono_class_get_type:
7334 * @klass: the MonoClass to act on
7336 * This method returns the internal Type representation for the class.
7338 * Returns: the MonoType from the class.
7341 mono_class_get_type (MonoClass
*klass
)
7343 return &klass
->byval_arg
;
7347 * mono_class_get_type_token
7348 * @klass: the MonoClass to act on
7350 * This method returns type token for the class.
7352 * Returns: the type token for the class.
7355 mono_class_get_type_token (MonoClass
*klass
)
7357 return klass
->type_token
;
7361 * mono_class_get_byref_type:
7362 * @klass: the MonoClass to act on
7367 mono_class_get_byref_type (MonoClass
*klass
)
7369 return &klass
->this_arg
;
7373 * mono_class_num_fields:
7374 * @klass: the MonoClass to act on
7376 * Returns: the number of static and instance fields in the class.
7379 mono_class_num_fields (MonoClass
*klass
)
7381 return klass
->field
.count
;
7385 * mono_class_num_methods:
7386 * @klass: the MonoClass to act on
7388 * Returns: the number of methods in the class.
7391 mono_class_num_methods (MonoClass
*klass
)
7393 return klass
->method
.count
;
7397 * mono_class_num_properties
7398 * @klass: the MonoClass to act on
7400 * Returns: the number of properties in the class.
7403 mono_class_num_properties (MonoClass
*klass
)
7405 mono_class_setup_properties (klass
);
7407 return klass
->ext
->property
.count
;
7411 * mono_class_num_events:
7412 * @klass: the MonoClass to act on
7414 * Returns: the number of events in the class.
7417 mono_class_num_events (MonoClass
*klass
)
7419 mono_class_setup_events (klass
);
7421 return klass
->ext
->event
.count
;
7425 * mono_class_get_fields:
7426 * @klass: the MonoClass to act on
7428 * This routine is an iterator routine for retrieving the fields in a class.
7430 * You must pass a gpointer that points to zero and is treated as an opaque handle to
7431 * iterate over all of the elements. When no more values are
7432 * available, the return value is NULL.
7434 * Returns: a @MonoClassField* on each iteration, or NULL when no more fields are available.
7437 mono_class_get_fields (MonoClass
* klass
, gpointer
*iter
)
7439 MonoClassField
* field
;
7443 mono_class_setup_fields_locking (klass
);
7444 if (klass
->exception_type
)
7446 /* start from the first */
7447 if (klass
->field
.count
) {
7448 return *iter
= &klass
->fields
[0];
7456 if (field
< &klass
->fields
[klass
->field
.count
]) {
7457 return *iter
= field
;
7463 * mono_class_get_methods
7464 * @klass: the MonoClass to act on
7466 * This routine is an iterator routine for retrieving the fields in a class.
7468 * You must pass a gpointer that points to zero and is treated as an opaque handle to
7469 * iterate over all of the elements. When no more values are
7470 * available, the return value is NULL.
7472 * Returns: a MonoMethod on each iteration or NULL when no more methods are available.
7475 mono_class_get_methods (MonoClass
* klass
, gpointer
*iter
)
7477 MonoMethod
** method
;
7481 mono_class_init (klass
);
7483 mono_class_setup_methods (klass
);
7486 * We can't fail lookup of methods otherwise the runtime will burst in flames on all sort of places.
7487 * FIXME we should better report this error to the caller
7489 if (!klass
->methods
)
7491 /* start from the first */
7492 if (klass
->method
.count
) {
7493 *iter
= &klass
->methods
[0];
7494 return klass
->methods
[0];
7502 if (method
< &klass
->methods
[klass
->method
.count
]) {
7510 * mono_class_get_virtual_methods:
7512 * Iterate over the virtual methods of KLASS.
7514 * LOCKING: Assumes the loader lock is held (because of the klass->methods check).
7517 mono_class_get_virtual_methods (MonoClass
* klass
, gpointer
*iter
)
7519 MonoMethod
** method
;
7522 if (klass
->methods
|| !MONO_CLASS_HAS_STATIC_METADATA (klass
) || mono_debug_using_mono_debugger ()) {
7524 mono_class_setup_methods (klass
);
7526 * We can't fail lookup of methods otherwise the runtime will burst in flames on all sort of places.
7527 * FIXME we should better report this error to the caller
7529 if (!klass
->methods
)
7531 /* start from the first */
7532 method
= &klass
->methods
[0];
7537 while (method
< &klass
->methods
[klass
->method
.count
]) {
7538 if (((*method
)->flags
& METHOD_ATTRIBUTE_VIRTUAL
))
7542 if (method
< &klass
->methods
[klass
->method
.count
]) {
7549 /* Search directly in metadata to avoid calling setup_methods () */
7550 MonoMethod
*res
= NULL
;
7556 start_index
= GPOINTER_TO_UINT (*iter
);
7559 for (i
= start_index
; i
< klass
->method
.count
; ++i
) {
7562 /* class->method.first points into the methodptr table */
7563 flags
= mono_metadata_decode_table_row_col (klass
->image
, MONO_TABLE_METHOD
, klass
->method
.first
+ i
, MONO_METHOD_FLAGS
);
7565 if (flags
& METHOD_ATTRIBUTE_VIRTUAL
)
7569 if (i
< klass
->method
.count
) {
7570 res
= mono_get_method (klass
->image
, MONO_TOKEN_METHOD_DEF
| (klass
->method
.first
+ i
+ 1), klass
);
7571 /* Add 1 here so the if (*iter) check fails */
7572 *iter
= GUINT_TO_POINTER (i
+ 1);
7581 * mono_class_get_properties:
7582 * @klass: the MonoClass to act on
7584 * This routine is an iterator routine for retrieving the properties in a class.
7586 * You must pass a gpointer that points to zero and is treated as an opaque handle to
7587 * iterate over all of the elements. When no more values are
7588 * available, the return value is NULL.
7590 * Returns: a @MonoProperty* on each invocation, or NULL when no more are available.
7593 mono_class_get_properties (MonoClass
* klass
, gpointer
*iter
)
7595 MonoProperty
* property
;
7599 mono_class_init (klass
);
7601 mono_class_setup_properties (klass
);
7602 /* start from the first */
7603 if (klass
->ext
->property
.count
) {
7604 return *iter
= &klass
->ext
->properties
[0];
7612 if (property
< &klass
->ext
->properties
[klass
->ext
->property
.count
]) {
7613 return *iter
= property
;
7619 * mono_class_get_events:
7620 * @klass: the MonoClass to act on
7622 * This routine is an iterator routine for retrieving the properties in a class.
7624 * You must pass a gpointer that points to zero and is treated as an opaque handle to
7625 * iterate over all of the elements. When no more values are
7626 * available, the return value is NULL.
7628 * Returns: a @MonoEvent* on each invocation, or NULL when no more are available.
7631 mono_class_get_events (MonoClass
* klass
, gpointer
*iter
)
7637 mono_class_init (klass
);
7639 mono_class_setup_events (klass
);
7640 /* start from the first */
7641 if (klass
->ext
->event
.count
) {
7642 return *iter
= &klass
->ext
->events
[0];
7650 if (event
< &klass
->ext
->events
[klass
->ext
->event
.count
]) {
7651 return *iter
= event
;
7657 * mono_class_get_interfaces
7658 * @klass: the MonoClass to act on
7660 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
7662 * You must pass a gpointer that points to zero and is treated as an opaque handle to
7663 * iterate over all of the elements. When no more values are
7664 * available, the return value is NULL.
7666 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
7669 mono_class_get_interfaces (MonoClass
* klass
, gpointer
*iter
)
7677 mono_class_init (klass
);
7678 if (!klass
->interfaces_inited
) {
7679 mono_class_setup_interfaces (klass
, &error
);
7680 if (!mono_error_ok (&error
)) {
7681 mono_error_cleanup (&error
);
7685 /* start from the first */
7686 if (klass
->interface_count
) {
7687 *iter
= &klass
->interfaces
[0];
7688 return klass
->interfaces
[0];
7696 if (iface
< &klass
->interfaces
[klass
->interface_count
]) {
7704 * mono_class_get_nested_types
7705 * @klass: the MonoClass to act on
7707 * This routine is an iterator routine for retrieving the nested types of a class.
7708 * This works only if @klass is non-generic, or a generic type definition.
7710 * You must pass a gpointer that points to zero and is treated as an opaque handle to
7711 * iterate over all of the elements. When no more values are
7712 * available, the return value is NULL.
7714 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
7717 mono_class_get_nested_types (MonoClass
* klass
, gpointer
*iter
)
7725 mono_class_init (klass
);
7726 if (!klass
->nested_classes_inited
) {
7727 if (!klass
->type_token
)
7728 klass
->nested_classes_inited
= TRUE
;
7729 mono_loader_lock ();
7730 if (!klass
->nested_classes_inited
) {
7731 i
= mono_metadata_nesting_typedef (klass
->image
, klass
->type_token
, 1);
7734 guint32 cols
[MONO_NESTED_CLASS_SIZE
];
7735 mono_metadata_decode_row (&klass
->image
->tables
[MONO_TABLE_NESTEDCLASS
], i
- 1, cols
, MONO_NESTED_CLASS_SIZE
);
7736 nclass
= mono_class_create_from_typedef (klass
->image
, MONO_TOKEN_TYPE_DEF
| cols
[MONO_NESTED_CLASS_NESTED
]);
7739 mono_class_alloc_ext (klass
);
7740 klass
->ext
->nested_classes
= g_list_prepend_image (klass
->image
, klass
->ext
->nested_classes
, nclass
);
7742 i
= mono_metadata_nesting_typedef (klass
->image
, klass
->type_token
, i
+ 1);
7745 mono_memory_barrier ();
7746 klass
->nested_classes_inited
= TRUE
;
7747 mono_loader_unlock ();
7751 /* start from the first */
7752 if (klass
->ext
&& klass
->ext
->nested_classes
) {
7753 *iter
= klass
->ext
->nested_classes
;
7754 return klass
->ext
->nested_classes
->data
;
7756 /* no nested types */
7770 * mono_field_get_name:
7771 * @field: the MonoClassField to act on
7773 * Returns: the name of the field.
7776 mono_field_get_name (MonoClassField
*field
)
7782 * mono_field_get_type:
7783 * @field: the MonoClassField to act on
7785 * Returns: MonoType of the field.
7788 mono_field_get_type (MonoClassField
*field
)
7794 * mono_field_get_parent:
7795 * @field: the MonoClassField to act on
7797 * Returns: MonoClass where the field was defined.
7800 mono_field_get_parent (MonoClassField
*field
)
7802 return field
->parent
;
7806 * mono_field_get_flags;
7807 * @field: the MonoClassField to act on
7809 * The metadata flags for a field are encoded using the
7810 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
7812 * Returns: the flags for the field.
7815 mono_field_get_flags (MonoClassField
*field
)
7817 return field
->type
->attrs
;
7821 * mono_field_get_offset;
7822 * @field: the MonoClassField to act on
7824 * Returns: the field offset.
7827 mono_field_get_offset (MonoClassField
*field
)
7829 return field
->offset
;
7833 mono_field_get_rva (MonoClassField
*field
)
7837 MonoClass
*klass
= field
->parent
;
7839 g_assert (field
->type
->attrs
& FIELD_ATTRIBUTE_HAS_FIELD_RVA
);
7841 if (!klass
->ext
|| !klass
->ext
->field_def_values
) {
7842 mono_loader_lock ();
7843 mono_class_alloc_ext (klass
);
7844 if (!klass
->ext
->field_def_values
)
7845 klass
->ext
->field_def_values
= mono_image_alloc0 (klass
->image
, sizeof (MonoFieldDefaultValue
) * klass
->field
.count
);
7846 mono_loader_unlock ();
7849 field_index
= mono_field_get_index (field
);
7851 if (!klass
->ext
->field_def_values
[field_index
].data
&& !klass
->image
->dynamic
) {
7852 mono_metadata_field_info (field
->parent
->image
, klass
->field
.first
+ field_index
, NULL
, &rva
, NULL
);
7854 g_warning ("field %s in %s should have RVA data, but hasn't", mono_field_get_name (field
), field
->parent
->name
);
7855 klass
->ext
->field_def_values
[field_index
].data
= mono_image_rva_map (field
->parent
->image
, rva
);
7858 return klass
->ext
->field_def_values
[field_index
].data
;
7862 * mono_field_get_data;
7863 * @field: the MonoClassField to act on
7865 * Returns: pointer to the metadata constant value or to the field
7866 * data if it has an RVA flag.
7869 mono_field_get_data (MonoClassField
*field
)
7871 if (field
->type
->attrs
& FIELD_ATTRIBUTE_HAS_DEFAULT
) {
7872 MonoTypeEnum def_type
;
7874 return mono_class_get_field_default_value (field
, &def_type
);
7875 } else if (field
->type
->attrs
& FIELD_ATTRIBUTE_HAS_FIELD_RVA
) {
7876 return mono_field_get_rva (field
);
7883 * mono_property_get_name:
7884 * @prop: the MonoProperty to act on
7886 * Returns: the name of the property
7889 mono_property_get_name (MonoProperty
*prop
)
7895 * mono_property_get_set_method
7896 * @prop: the MonoProperty to act on.
7898 * Returns: the setter method of the property (A MonoMethod)
7901 mono_property_get_set_method (MonoProperty
*prop
)
7907 * mono_property_get_get_method
7908 * @prop: the MonoProperty to act on.
7910 * Returns: the setter method of the property (A MonoMethod)
7913 mono_property_get_get_method (MonoProperty
*prop
)
7919 * mono_property_get_parent:
7920 * @prop: the MonoProperty to act on.
7922 * Returns: the MonoClass where the property was defined.
7925 mono_property_get_parent (MonoProperty
*prop
)
7927 return prop
->parent
;
7931 * mono_property_get_flags:
7932 * @prop: the MonoProperty to act on.
7934 * The metadata flags for a property are encoded using the
7935 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
7937 * Returns: the flags for the property.
7940 mono_property_get_flags (MonoProperty
*prop
)
7946 * mono_event_get_name:
7947 * @event: the MonoEvent to act on
7949 * Returns: the name of the event.
7952 mono_event_get_name (MonoEvent
*event
)
7958 * mono_event_get_add_method:
7959 * @event: The MonoEvent to act on.
7961 * Returns: the @add' method for the event (a MonoMethod).
7964 mono_event_get_add_method (MonoEvent
*event
)
7970 * mono_event_get_remove_method:
7971 * @event: The MonoEvent to act on.
7973 * Returns: the @remove method for the event (a MonoMethod).
7976 mono_event_get_remove_method (MonoEvent
*event
)
7978 return event
->remove
;
7982 * mono_event_get_raise_method:
7983 * @event: The MonoEvent to act on.
7985 * Returns: the @raise method for the event (a MonoMethod).
7988 mono_event_get_raise_method (MonoEvent
*event
)
7990 return event
->raise
;
7994 * mono_event_get_parent:
7995 * @event: the MonoEvent to act on.
7997 * Returns: the MonoClass where the event is defined.
8000 mono_event_get_parent (MonoEvent
*event
)
8002 return event
->parent
;
8006 * mono_event_get_flags
8007 * @event: the MonoEvent to act on.
8009 * The metadata flags for an event are encoded using the
8010 * EVENT_* constants. See the tabledefs.h file for details.
8012 * Returns: the flags for the event.
8015 mono_event_get_flags (MonoEvent
*event
)
8017 return event
->attrs
;
8021 * mono_class_get_method_from_name:
8022 * @klass: where to look for the method
8023 * @name_space: name of the method
8024 * @param_count: number of parameters. -1 for any number.
8026 * Obtains a MonoMethod with a given name and number of parameters.
8027 * It only works if there are no multiple signatures for any given method name.
8030 mono_class_get_method_from_name (MonoClass
*klass
, const char *name
, int param_count
)
8032 return mono_class_get_method_from_name_flags (klass
, name
, param_count
, 0);
8036 find_method_in_metadata (MonoClass
*klass
, const char *name
, int param_count
, int flags
)
8038 MonoMethod
*res
= NULL
;
8041 /* Search directly in the metadata to avoid calling setup_methods () */
8042 for (i
= 0; i
< klass
->method
.count
; ++i
) {
8043 guint32 cols
[MONO_METHOD_SIZE
];
8046 /* class->method.first points into the methodptr table */
8047 mono_metadata_decode_table_row (klass
->image
, MONO_TABLE_METHOD
, klass
->method
.first
+ i
, cols
, MONO_METHOD_SIZE
);
8049 if (!strcmp (mono_metadata_string_heap (klass
->image
, cols
[MONO_METHOD_NAME
]), name
)) {
8050 method
= mono_get_method (klass
->image
, MONO_TOKEN_METHOD_DEF
| (klass
->method
.first
+ i
+ 1), klass
);
8051 if ((param_count
== -1) || mono_method_signature (method
)->param_count
== param_count
) {
8062 * mono_class_get_method_from_name_flags:
8063 * @klass: where to look for the method
8064 * @name_space: name of the method
8065 * @param_count: number of parameters. -1 for any number.
8066 * @flags: flags which must be set in the method
8068 * Obtains a MonoMethod with a given name and number of parameters.
8069 * It only works if there are no multiple signatures for any given method name.
8072 mono_class_get_method_from_name_flags (MonoClass
*klass
, const char *name
, int param_count
, int flags
)
8074 MonoMethod
*res
= NULL
;
8077 mono_class_init (klass
);
8079 if (klass
->generic_class
&& !klass
->methods
) {
8080 res
= mono_class_get_method_from_name_flags (klass
->generic_class
->container_class
, name
, param_count
, flags
);
8082 res
= mono_class_inflate_generic_method_full (res
, klass
, mono_class_get_context (klass
));
8086 if (klass
->methods
|| !MONO_CLASS_HAS_STATIC_METADATA (klass
)) {
8087 mono_class_setup_methods (klass
);
8089 We can't fail lookup of methods otherwise the runtime will burst in flames on all sort of places.
8090 See mono/tests/array_load_exception.il
8091 FIXME we should better report this error to the caller
8093 if (!klass
->methods
)
8095 for (i
= 0; i
< klass
->method
.count
; ++i
) {
8096 MonoMethod
*method
= klass
->methods
[i
];
8098 if (method
->name
[0] == name
[0] &&
8099 !strcmp (name
, method
->name
) &&
8100 (param_count
== -1 || mono_method_signature (method
)->param_count
== param_count
) &&
8101 ((method
->flags
& flags
) == flags
)) {
8108 res
= find_method_in_metadata (klass
, name
, param_count
, flags
);
8115 * mono_class_set_failure:
8116 * @klass: class in which the failure was detected
8117 * @ex_type: the kind of exception/error to be thrown (later)
8118 * @ex_data: exception data (specific to each type of exception/error)
8120 * Keep a detected failure informations in the class for later processing.
8121 * Note that only the first failure is kept.
8123 * LOCKING: Acquires the loader lock.
8126 mono_class_set_failure (MonoClass
*klass
, guint32 ex_type
, void *ex_data
)
8128 if (klass
->exception_type
)
8131 mono_loader_lock ();
8132 klass
->exception_type
= ex_type
;
8134 mono_image_property_insert (klass
->image
, klass
, MONO_CLASS_PROP_EXCEPTION_DATA
, ex_data
);
8135 mono_loader_unlock ();
8141 * mono_class_get_exception_data:
8143 * Return the exception_data property of KLASS.
8145 * LOCKING: Acquires the loader lock.
8148 mono_class_get_exception_data (MonoClass
*klass
)
8150 return mono_image_property_lookup (klass
->image
, klass
, MONO_CLASS_PROP_EXCEPTION_DATA
);
8154 * mono_classes_init:
8156 * Initialize the resources used by this module.
8159 mono_classes_init (void)
8161 mono_counters_register ("Inflated methods size",
8162 MONO_COUNTER_GENERICS
| MONO_COUNTER_INT
, &inflated_methods_size
);
8163 mono_counters_register ("Inflated classes",
8164 MONO_COUNTER_GENERICS
| MONO_COUNTER_INT
, &inflated_classes
);
8165 mono_counters_register ("Inflated classes size",
8166 MONO_COUNTER_GENERICS
| MONO_COUNTER_INT
, &inflated_classes_size
);
8167 mono_counters_register ("MonoClass size",
8168 MONO_COUNTER_METADATA
| MONO_COUNTER_INT
, &classes_size
);
8169 mono_counters_register ("MonoClassExt size",
8170 MONO_COUNTER_METADATA
| MONO_COUNTER_INT
, &class_ext_size
);
8174 * mono_classes_cleanup:
8176 * Free the resources used by this module.
8179 mono_classes_cleanup (void)
8181 if (global_interface_bitset
)
8182 mono_bitset_free (global_interface_bitset
);
8186 * mono_class_get_exception_for_failure:
8187 * @klass: class in which the failure was detected
8189 * Return a constructed MonoException than the caller can then throw
8190 * using mono_raise_exception - or NULL if no failure is present (or
8191 * doesn't result in an exception).
8194 mono_class_get_exception_for_failure (MonoClass
*klass
)
8196 gpointer exception_data
= mono_class_get_exception_data (klass
);
8198 switch (klass
->exception_type
) {
8199 case MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND
: {
8200 MonoDomain
*domain
= mono_domain_get ();
8201 MonoSecurityManager
* secman
= mono_security_manager_get_methods ();
8202 MonoMethod
*method
= exception_data
;
8203 guint32 error
= (method
) ? MONO_METADATA_INHERITANCEDEMAND_METHOD
: MONO_METADATA_INHERITANCEDEMAND_CLASS
;
8204 MonoObject
*exc
= NULL
;
8208 args
[1] = mono_assembly_get_object (domain
, mono_image_get_assembly (klass
->image
));
8209 args
[2] = mono_type_get_object (domain
, &klass
->byval_arg
);
8210 args
[3] = (method
) ? mono_method_get_object (domain
, method
, NULL
) : NULL
;
8212 mono_runtime_invoke (secman
->inheritsecurityexception
, NULL
, args
, &exc
);
8213 return (MonoException
*) exc
;
8215 case MONO_EXCEPTION_TYPE_LOAD
: {
8218 char *str
= mono_type_get_full_name (klass
);
8219 char *astr
= klass
->image
->assembly
? mono_stringify_assembly_name (&klass
->image
->assembly
->aname
): NULL
;
8220 name
= mono_string_new (mono_domain_get (), str
);
8222 ex
= mono_get_exception_type_load (name
, astr
);
8226 case MONO_EXCEPTION_MISSING_METHOD
: {
8227 char *class_name
= exception_data
;
8228 char *assembly_name
= class_name
+ strlen (class_name
) + 1;
8230 return mono_get_exception_missing_method (class_name
, assembly_name
);
8232 case MONO_EXCEPTION_MISSING_FIELD
: {
8233 char *class_name
= exception_data
;
8234 char *member_name
= class_name
+ strlen (class_name
) + 1;
8236 return mono_get_exception_missing_field (class_name
, member_name
);
8238 case MONO_EXCEPTION_FILE_NOT_FOUND
: {
8239 char *msg_format
= exception_data
;
8240 char *assembly_name
= msg_format
+ strlen (msg_format
) + 1;
8241 char *msg
= g_strdup_printf (msg_format
, assembly_name
);
8244 ex
= mono_get_exception_file_not_found2 (msg
, mono_string_new (mono_domain_get (), assembly_name
));
8250 case MONO_EXCEPTION_BAD_IMAGE
: {
8251 return mono_get_exception_bad_image_format (exception_data
);
8254 MonoLoaderError
*error
;
8257 error
= mono_loader_get_last_error ();
8259 ex
= mono_loader_error_prepare_exception (error
);
8263 /* TODO - handle other class related failures */
8270 is_nesting_type (MonoClass
*outer_klass
, MonoClass
*inner_klass
)
8272 outer_klass
= mono_class_get_generic_type_definition (outer_klass
);
8273 inner_klass
= mono_class_get_generic_type_definition (inner_klass
);
8275 if (outer_klass
== inner_klass
)
8277 inner_klass
= inner_klass
->nested_in
;
8278 } while (inner_klass
);
8283 mono_class_get_generic_type_definition (MonoClass
*klass
)
8285 return klass
->generic_class
? klass
->generic_class
->container_class
: klass
;
8289 * Check if @klass is a subtype of @parent ignoring generic instantiations.
8291 * Generic instantiations are ignored for all super types of @klass.
8293 * Visibility checks ignoring generic instantiations.
8296 mono_class_has_parent_and_ignore_generics (MonoClass
*klass
, MonoClass
*parent
)
8299 klass
= mono_class_get_generic_type_definition (klass
);
8300 parent
= mono_class_get_generic_type_definition (parent
);
8302 for (i
= 0; i
< klass
->idepth
; ++i
) {
8303 if (parent
== mono_class_get_generic_type_definition (klass
->supertypes
[i
]))
8309 * Subtype can only access parent members with family protection if the site object
8310 * is subclass of Subtype. For example:
8311 * class A { protected int x; }
8313 * void valid_access () {
8317 * void invalid_access () {
8324 is_valid_family_access (MonoClass
*access_klass
, MonoClass
*member_klass
, MonoClass
*context_klass
)
8326 if (!mono_class_has_parent_and_ignore_generics (access_klass
, member_klass
))
8329 if (context_klass
== NULL
)
8331 /*if access_klass is not member_klass context_klass must be type compat*/
8332 if (access_klass
!= member_klass
&& !mono_class_has_parent_and_ignore_generics (context_klass
, access_klass
))
8338 can_access_internals (MonoAssembly
*accessing
, MonoAssembly
* accessed
)
8341 if (accessing
== accessed
)
8343 if (!accessed
|| !accessing
)
8346 /* extra safety under CoreCLR - the runtime does not verify the strongname signatures
8347 * anywhere so untrusted friends are not safe to access platform's code internals */
8348 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR
) {
8349 if (!mono_security_core_clr_can_access_internals (accessing
->image
, accessed
->image
))
8353 mono_assembly_load_friends (accessed
);
8354 for (tmp
= accessed
->friend_assembly_names
; tmp
; tmp
= tmp
->next
) {
8355 MonoAssemblyName
*friend = tmp
->data
;
8356 /* Be conservative with checks */
8359 if (strcmp (accessing
->aname
.name
, friend->name
))
8361 if (friend->public_key_token
[0]) {
8362 if (!accessing
->aname
.public_key_token
[0])
8364 if (!mono_public_tokens_are_equal (friend->public_key_token
, accessing
->aname
.public_key_token
))
8373 * If klass is a generic type or if it is derived from a generic type, return the
8374 * MonoClass of the generic definition
8375 * Returns NULL if not found
8378 get_generic_definition_class (MonoClass
*klass
)
8381 if (klass
->generic_class
&& klass
->generic_class
->container_class
)
8382 return klass
->generic_class
->container_class
;
8383 klass
= klass
->parent
;
8389 can_access_instantiation (MonoClass
*access_klass
, MonoGenericInst
*ginst
)
8392 for (i
= 0; i
< ginst
->type_argc
; ++i
) {
8393 MonoType
*type
= ginst
->type_argv
[i
];
8394 switch (type
->type
) {
8395 case MONO_TYPE_SZARRAY
:
8396 if (!can_access_type (access_klass
, type
->data
.klass
))
8399 case MONO_TYPE_ARRAY
:
8400 if (!can_access_type (access_klass
, type
->data
.array
->eklass
))
8404 if (!can_access_type (access_klass
, mono_class_from_mono_type (type
->data
.type
)))
8407 case MONO_TYPE_CLASS
:
8408 case MONO_TYPE_VALUETYPE
:
8409 case MONO_TYPE_GENERICINST
:
8410 if (!can_access_type (access_klass
, mono_class_from_mono_type (type
)))
8418 can_access_type (MonoClass
*access_klass
, MonoClass
*member_klass
)
8422 if (access_klass
->element_class
&& !access_klass
->enumtype
)
8423 access_klass
= access_klass
->element_class
;
8425 if (member_klass
->element_class
&& !member_klass
->enumtype
)
8426 member_klass
= member_klass
->element_class
;
8428 access_level
= member_klass
->flags
& TYPE_ATTRIBUTE_VISIBILITY_MASK
;
8430 if (member_klass
->byval_arg
.type
== MONO_TYPE_VAR
|| member_klass
->byval_arg
.type
== MONO_TYPE_MVAR
)
8433 if (member_klass
->generic_class
&& !can_access_instantiation (access_klass
, member_klass
->generic_class
->context
.class_inst
))
8436 if (is_nesting_type (access_klass
, member_klass
) || (access_klass
->nested_in
&& is_nesting_type (access_klass
->nested_in
, member_klass
)))
8439 if (member_klass
->nested_in
&& !can_access_type (access_klass
, member_klass
->nested_in
))
8442 /*Non nested type with nested visibility. We just fail it.*/
8443 if (access_level
>= TYPE_ATTRIBUTE_NESTED_PRIVATE
&& access_level
<= TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM
&& member_klass
->nested_in
== NULL
)
8446 switch (access_level
) {
8447 case TYPE_ATTRIBUTE_NOT_PUBLIC
:
8448 return can_access_internals (access_klass
->image
->assembly
, member_klass
->image
->assembly
);
8450 case TYPE_ATTRIBUTE_PUBLIC
:
8453 case TYPE_ATTRIBUTE_NESTED_PUBLIC
:
8456 case TYPE_ATTRIBUTE_NESTED_PRIVATE
:
8457 return is_nesting_type (member_klass
, access_klass
);
8459 case TYPE_ATTRIBUTE_NESTED_FAMILY
:
8460 return mono_class_has_parent_and_ignore_generics (access_klass
, member_klass
->nested_in
);
8462 case TYPE_ATTRIBUTE_NESTED_ASSEMBLY
:
8463 return can_access_internals (access_klass
->image
->assembly
, member_klass
->image
->assembly
);
8465 case TYPE_ATTRIBUTE_NESTED_FAM_AND_ASSEM
:
8466 return can_access_internals (access_klass
->image
->assembly
, member_klass
->nested_in
->image
->assembly
) &&
8467 mono_class_has_parent_and_ignore_generics (access_klass
, member_klass
->nested_in
);
8469 case TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM
:
8470 return can_access_internals (access_klass
->image
->assembly
, member_klass
->nested_in
->image
->assembly
) ||
8471 mono_class_has_parent_and_ignore_generics (access_klass
, member_klass
->nested_in
);
8476 /* FIXME: check visibility of type, too */
8478 can_access_member (MonoClass
*access_klass
, MonoClass
*member_klass
, MonoClass
* context_klass
, int access_level
)
8480 MonoClass
*member_generic_def
;
8481 if (((access_klass
->generic_class
&& access_klass
->generic_class
->container_class
) ||
8482 access_klass
->generic_container
) &&
8483 (member_generic_def
= get_generic_definition_class (member_klass
))) {
8484 MonoClass
*access_container
;
8486 if (access_klass
->generic_container
)
8487 access_container
= access_klass
;
8489 access_container
= access_klass
->generic_class
->container_class
;
8491 if (can_access_member (access_container
, member_generic_def
, context_klass
, access_level
))
8495 /* Partition I 8.5.3.2 */
8496 /* the access level values are the same for fields and methods */
8497 switch (access_level
) {
8498 case FIELD_ATTRIBUTE_COMPILER_CONTROLLED
:
8499 /* same compilation unit */
8500 return access_klass
->image
== member_klass
->image
;
8501 case FIELD_ATTRIBUTE_PRIVATE
:
8502 return access_klass
== member_klass
;
8503 case FIELD_ATTRIBUTE_FAM_AND_ASSEM
:
8504 if (is_valid_family_access (access_klass
, member_klass
, context_klass
) &&
8505 can_access_internals (access_klass
->image
->assembly
, member_klass
->image
->assembly
))
8508 case FIELD_ATTRIBUTE_ASSEMBLY
:
8509 return can_access_internals (access_klass
->image
->assembly
, member_klass
->image
->assembly
);
8510 case FIELD_ATTRIBUTE_FAMILY
:
8511 if (is_valid_family_access (access_klass
, member_klass
, context_klass
))
8514 case FIELD_ATTRIBUTE_FAM_OR_ASSEM
:
8515 if (is_valid_family_access (access_klass
, member_klass
, context_klass
))
8517 return can_access_internals (access_klass
->image
->assembly
, member_klass
->image
->assembly
);
8518 case FIELD_ATTRIBUTE_PUBLIC
:
8525 mono_method_can_access_field (MonoMethod
*method
, MonoClassField
*field
)
8527 /* FIXME: check all overlapping fields */
8528 int can
= can_access_member (method
->klass
, field
->parent
, NULL
, field
->type
->attrs
& FIELD_ATTRIBUTE_FIELD_ACCESS_MASK
);
8530 MonoClass
*nested
= method
->klass
->nested_in
;
8532 can
= can_access_member (nested
, field
->parent
, NULL
, field
->type
->attrs
& FIELD_ATTRIBUTE_FIELD_ACCESS_MASK
);
8535 nested
= nested
->nested_in
;
8542 mono_method_can_access_method (MonoMethod
*method
, MonoMethod
*called
)
8544 int can
= can_access_member (method
->klass
, called
->klass
, NULL
, called
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
);
8546 MonoClass
*nested
= method
->klass
->nested_in
;
8548 can
= can_access_member (nested
, called
->klass
, NULL
, called
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
);
8551 nested
= nested
->nested_in
;
8556 * with generics calls to explicit interface implementations can be expressed
8557 * directly: the method is private, but we must allow it. This may be opening
8558 * a hole or the generics code should handle this differently.
8559 * Maybe just ensure the interface type is public.
8561 if ((called
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && (called
->flags
& METHOD_ATTRIBUTE_FINAL
))
8567 * mono_method_can_access_method_full:
8568 * @method: The caller method
8569 * @called: The called method
8570 * @context_klass: The static type on stack of the owner @called object used
8572 * This function must be used with instance calls, as they have more strict family accessibility.
8573 * It can be used with static methods, but context_klass should be NULL.
8575 * Returns: TRUE if caller have proper visibility and acessibility to @called
8578 mono_method_can_access_method_full (MonoMethod
*method
, MonoMethod
*called
, MonoClass
*context_klass
)
8580 MonoClass
*access_class
= method
->klass
;
8581 MonoClass
*member_class
= called
->klass
;
8582 int can
= can_access_member (access_class
, member_class
, context_klass
, called
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
);
8584 MonoClass
*nested
= access_class
->nested_in
;
8586 can
= can_access_member (nested
, member_class
, context_klass
, called
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
);
8589 nested
= nested
->nested_in
;
8596 if (!can_access_type (access_class
, member_class
) && (!access_class
->nested_in
|| !can_access_type (access_class
->nested_in
, member_class
)))
8599 if (called
->is_inflated
) {
8600 MonoMethodInflated
* infl
= (MonoMethodInflated
*)called
;
8601 if (infl
->context
.method_inst
&& !can_access_instantiation (access_class
, infl
->context
.method_inst
))
8610 * mono_method_can_access_field_full:
8611 * @method: The caller method
8612 * @field: The accessed field
8613 * @context_klass: The static type on stack of the owner @field object used
8615 * This function must be used with instance fields, as they have more strict family accessibility.
8616 * It can be used with static fields, but context_klass should be NULL.
8618 * Returns: TRUE if caller have proper visibility and acessibility to @field
8621 mono_method_can_access_field_full (MonoMethod
*method
, MonoClassField
*field
, MonoClass
*context_klass
)
8623 MonoClass
*access_class
= method
->klass
;
8624 MonoClass
*member_class
= field
->parent
;
8625 /* FIXME: check all overlapping fields */
8626 int can
= can_access_member (access_class
, member_class
, context_klass
, field
->type
->attrs
& FIELD_ATTRIBUTE_FIELD_ACCESS_MASK
);
8628 MonoClass
*nested
= access_class
->nested_in
;
8630 can
= can_access_member (nested
, member_class
, context_klass
, field
->type
->attrs
& FIELD_ATTRIBUTE_FIELD_ACCESS_MASK
);
8633 nested
= nested
->nested_in
;
8640 if (!can_access_type (access_class
, member_class
) && (!access_class
->nested_in
|| !can_access_type (access_class
->nested_in
, member_class
)))
8646 * mono_type_is_valid_enum_basetype:
8647 * @type: The MonoType to check
8649 * Returns: TRUE if the type can be used as the basetype of an enum
8651 gboolean
mono_type_is_valid_enum_basetype (MonoType
* type
) {
8652 switch (type
->type
) {
8655 case MONO_TYPE_BOOLEAN
:
8658 case MONO_TYPE_CHAR
:
8671 * mono_class_is_valid_enum:
8672 * @klass: An enum class to be validated
8674 * This method verify the required properties an enum should have.
8676 * Returns: TRUE if the informed enum class is valid
8678 * FIXME: TypeBuilder enums are allowed to implement interfaces, but since they cannot have methods, only empty interfaces are possible
8679 * FIXME: enum types are not allowed to have a cctor, but mono_reflection_create_runtime_class sets has_cctor to 1 for all types
8680 * FIXME: TypeBuilder enums can have any kind of static fields, but the spec is very explicit about that (P II 14.3)
8682 gboolean
mono_class_is_valid_enum (MonoClass
*klass
) {
8683 MonoClassField
* field
;
8684 gpointer iter
= NULL
;
8685 gboolean found_base_field
= FALSE
;
8687 g_assert (klass
->enumtype
);
8688 /* we cannot test against mono_defaults.enum_class, or mcs won't be able to compile the System namespace*/
8689 if (!klass
->parent
|| strcmp (klass
->parent
->name
, "Enum") || strcmp (klass
->parent
->name_space
, "System") ) {
8693 if ((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) != TYPE_ATTRIBUTE_AUTO_LAYOUT
)
8696 while ((field
= mono_class_get_fields (klass
, &iter
))) {
8697 if (!(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)) {
8698 if (found_base_field
)
8700 found_base_field
= TRUE
;
8701 if (!mono_type_is_valid_enum_basetype (field
->type
))
8706 if (!found_base_field
)
8709 if (klass
->method
.count
> 0)
8716 mono_generic_class_is_generic_type_definition (MonoGenericClass
*gklass
)
8718 return gklass
->context
.class_inst
== gklass
->container_class
->generic_container
->context
.class_inst
;
8722 * mono_class_setup_interface_id:
8724 * Initializes MonoClass::interface_id if required.
8726 * LOCKING: Acquires the loader lock.
8729 mono_class_setup_interface_id (MonoClass
*class)
8731 mono_loader_lock ();
8732 if (MONO_CLASS_IS_INTERFACE (class) && !class->interface_id
)
8733 class->interface_id
= mono_get_unique_iid (class);
8734 mono_loader_unlock ();
8738 * mono_class_alloc_ext:
8740 * Allocate klass->ext if not already done.
8741 * LOCKING: Assumes the loader lock is held.
8744 mono_class_alloc_ext (MonoClass
*klass
)
8747 if (klass
->generic_class
) {
8748 klass
->ext
= g_new0 (MonoClassExt
, 1);
8750 klass
->ext
= mono_image_alloc0 (klass
->image
, sizeof (MonoClassExt
));
8752 class_ext_size
+= sizeof (MonoClassExt
);
8757 * mono_class_setup_interfaces:
8759 * Initialize class->interfaces/interfaces_count.
8760 * LOCKING: Acquires the loader lock.
8761 * This function can fail the type.
8764 mono_class_setup_interfaces (MonoClass
*klass
, MonoError
*error
)
8768 mono_error_init (error
);
8770 if (klass
->interfaces_inited
)
8773 mono_loader_lock ();
8775 if (klass
->interfaces_inited
) {
8776 mono_loader_unlock ();
8780 if (klass
->rank
== 1 && klass
->byval_arg
.type
!= MONO_TYPE_ARRAY
&& mono_defaults
.generic_ilist_class
) {
8783 /* generic IList, ICollection, IEnumerable */
8784 klass
->interface_count
= 1;
8785 klass
->interfaces
= mono_image_alloc0 (klass
->image
, sizeof (MonoClass
*) * klass
->interface_count
);
8787 args
[0] = &klass
->element_class
->byval_arg
;
8788 klass
->interfaces
[0] = mono_class_bind_generic_parameters (
8789 mono_defaults
.generic_ilist_class
, 1, args
, FALSE
);
8790 } else if (klass
->generic_class
) {
8791 MonoClass
*gklass
= klass
->generic_class
->container_class
;
8793 klass
->interface_count
= gklass
->interface_count
;
8794 klass
->interfaces
= g_new0 (MonoClass
*, klass
->interface_count
);
8795 for (i
= 0; i
< klass
->interface_count
; i
++) {
8796 klass
->interfaces
[i
] = mono_class_inflate_generic_class_checked (gklass
->interfaces
[i
], mono_generic_class_get_context (klass
->generic_class
), error
);
8797 if (!mono_error_ok (error
)) {
8798 mono_class_set_failure (klass
, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Could not setup the interfaces"));
8799 g_free (klass
->interfaces
);
8800 klass
->interfaces
= NULL
;
8806 mono_memory_barrier ();
8808 klass
->interfaces_inited
= TRUE
;
8810 mono_loader_unlock ();