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 * Controls whenever mono_class_init () constructs a generic vtable. This is TRUE by
51 * default to avoid breaking embedding apps, but set to FALSE by the runtime executable
54 gboolean mono_setup_vtable_in_class_init
= TRUE
;
57 guint32 inflated_classes
, inflated_classes_size
, inflated_methods_size
;
58 guint32 classes_size
, class_ext_size
;
60 /* Function supplied by the runtime to find classes by name using information from the AOT file */
61 static MonoGetClassFromName get_class_from_name
= NULL
;
63 static MonoClass
* mono_class_create_from_typedef (MonoImage
*image
, guint32 type_token
);
64 static gboolean
mono_class_get_cached_class_info (MonoClass
*klass
, MonoCachedClassInfo
*res
);
65 static gboolean
can_access_type (MonoClass
*access_klass
, MonoClass
*member_klass
);
66 static MonoMethod
* find_method_in_metadata (MonoClass
*klass
, const char *name
, int param_count
, int flags
);
67 static int generic_array_methods (MonoClass
*class);
68 static void setup_generic_array_ifaces (MonoClass
*class, MonoClass
*iface
, MonoMethod
**methods
, int pos
);
70 static MonoMethod
* mono_class_get_virtual_methods (MonoClass
* klass
, gpointer
*iter
);
71 static char* mono_assembly_name_from_token (MonoImage
*image
, guint32 type_token
);
72 static gboolean
mono_class_is_variant_compatible (MonoClass
*klass
, MonoClass
*oklass
);
75 void (*mono_debugger_class_init_func
) (MonoClass
*klass
) = NULL
;
76 void (*mono_debugger_class_loaded_methods_func
) (MonoClass
*klass
) = NULL
;
79 * mono_class_from_typeref:
81 * @type_token: a TypeRef token
83 * Creates the MonoClass* structure representing the type defined by
84 * the typeref token valid inside @image.
85 * Returns: the MonoClass* representing the typeref token, NULL ifcould
89 mono_class_from_typeref (MonoImage
*image
, guint32 type_token
)
91 guint32 cols
[MONO_TYPEREF_SIZE
];
92 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_TYPEREF
];
94 const char *name
, *nspace
;
98 mono_metadata_decode_row (t
, (type_token
&0xffffff)-1, cols
, MONO_TYPEREF_SIZE
);
100 name
= mono_metadata_string_heap (image
, cols
[MONO_TYPEREF_NAME
]);
101 nspace
= mono_metadata_string_heap (image
, cols
[MONO_TYPEREF_NAMESPACE
]);
103 idx
= cols
[MONO_TYPEREF_SCOPE
] >> MONO_RESOLTION_SCOPE_BITS
;
104 switch (cols
[MONO_TYPEREF_SCOPE
] & MONO_RESOLTION_SCOPE_MASK
) {
105 case MONO_RESOLTION_SCOPE_MODULE
:
107 g_error ("null ResolutionScope not yet handled");
108 /* a typedef in disguise */
109 return mono_class_from_name (image
, nspace
, name
);
110 case MONO_RESOLTION_SCOPE_MODULEREF
:
111 module
= mono_image_load_module (image
, idx
);
113 return mono_class_from_name (module
, nspace
, name
);
115 char *msg
= g_strdup_printf ("%s%s%s", nspace
, nspace
[0] ? "." : "", name
);
118 human_name
= mono_stringify_assembly_name (&image
->assembly
->aname
);
119 mono_loader_set_error_type_load (msg
, human_name
);
125 case MONO_RESOLTION_SCOPE_TYPEREF
: {
126 MonoClass
*enclosing
= mono_class_from_typeref (image
, MONO_TOKEN_TYPE_REF
| idx
);
132 if (enclosing
->nested_classes_inited
&& enclosing
->ext
) {
133 /* Micro-optimization: don't scan the metadata tables if enclosing is already inited */
134 for (tmp
= enclosing
->ext
->nested_classes
; tmp
; tmp
= tmp
->next
) {
136 if (strcmp (res
->name
, name
) == 0)
140 /* Don't call mono_class_init as we might've been called by it recursively */
141 int i
= mono_metadata_nesting_typedef (enclosing
->image
, enclosing
->type_token
, 1);
143 guint32 class_nested
= mono_metadata_decode_row_col (&enclosing
->image
->tables
[MONO_TABLE_NESTEDCLASS
], i
- 1, MONO_NESTED_CLASS_NESTED
);
144 guint32 string_offset
= mono_metadata_decode_row_col (&enclosing
->image
->tables
[MONO_TABLE_TYPEDEF
], class_nested
- 1, MONO_TYPEDEF_NAME
);
145 const char *nname
= mono_metadata_string_heap (enclosing
->image
, string_offset
);
147 if (strcmp (nname
, name
) == 0)
148 return mono_class_create_from_typedef (enclosing
->image
, MONO_TOKEN_TYPE_DEF
| class_nested
);
150 i
= mono_metadata_nesting_typedef (enclosing
->image
, enclosing
->type_token
, i
+ 1);
153 g_warning ("TypeRef ResolutionScope not yet handled (%d) for %s.%s in image %s", idx
, nspace
, name
, image
->name
);
156 case MONO_RESOLTION_SCOPE_ASSEMBLYREF
:
160 if (idx
> image
->tables
[MONO_TABLE_ASSEMBLYREF
].rows
) {
161 mono_loader_set_error_bad_image (g_strdup_printf ("Image %s with invalid assemblyref token %08x.", image
->name
, idx
));
165 if (!image
->references
|| !image
->references
[idx
- 1])
166 mono_assembly_load_reference (image
, idx
- 1);
167 g_assert (image
->references
[idx
- 1]);
169 /* If the assembly did not load, register this as a type load exception */
170 if (image
->references
[idx
- 1] == REFERENCE_MISSING
){
171 MonoAssemblyName aname
;
174 mono_assembly_get_assemblyref (image
, idx
- 1, &aname
);
175 human_name
= mono_stringify_assembly_name (&aname
);
176 mono_loader_set_error_assembly_load (human_name
, image
->assembly
->ref_only
);
182 return mono_class_from_name (image
->references
[idx
- 1]->image
, nspace
, name
);
187 mono_image_memdup (MonoImage
*image
, void *data
, guint size
)
189 void *res
= mono_image_alloc (image
, size
);
190 memcpy (res
, data
, size
);
194 /* Copy everything mono_metadata_free_array free. */
196 mono_dup_array_type (MonoImage
*image
, MonoArrayType
*a
)
199 a
= mono_image_memdup (image
, a
, sizeof (MonoArrayType
));
201 a
->sizes
= mono_image_memdup (image
, a
->sizes
, a
->numsizes
* sizeof (int));
203 a
->lobounds
= mono_image_memdup (image
, a
->lobounds
, a
->numlobounds
* sizeof (int));
205 a
= g_memdup (a
, sizeof (MonoArrayType
));
207 a
->sizes
= g_memdup (a
->sizes
, a
->numsizes
* sizeof (int));
209 a
->lobounds
= g_memdup (a
->lobounds
, a
->numlobounds
* sizeof (int));
214 /* Copy everything mono_metadata_free_method_signature free. */
216 mono_metadata_signature_deep_dup (MonoImage
*image
, MonoMethodSignature
*sig
)
220 sig
= mono_metadata_signature_dup_full (image
, sig
);
222 sig
->ret
= mono_metadata_type_dup (image
, sig
->ret
);
223 for (i
= 0; i
< sig
->param_count
; ++i
)
224 sig
->params
[i
] = mono_metadata_type_dup (image
, sig
->params
[i
]);
230 _mono_type_get_assembly_name (MonoClass
*klass
, GString
*str
)
232 MonoAssembly
*ta
= klass
->image
->assembly
;
234 g_string_append_printf (
235 str
, ", %s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s%s",
237 ta
->aname
.major
, ta
->aname
.minor
, ta
->aname
.build
, ta
->aname
.revision
,
238 ta
->aname
.culture
&& *ta
->aname
.culture
? ta
->aname
.culture
: "neutral",
239 ta
->aname
.public_key_token
[0] ? (char *)ta
->aname
.public_key_token
: "null",
240 (ta
->aname
.flags
& ASSEMBLYREF_RETARGETABLE_FLAG
) ? ", Retargetable=Yes" : "");
244 mono_type_name_check_byref (MonoType
*type
, GString
*str
)
247 g_string_append_c (str
, '&');
251 mono_type_get_name_recurse (MonoType
*type
, GString
*str
, gboolean is_recursed
,
252 MonoTypeNameFormat format
)
256 switch (type
->type
) {
257 case MONO_TYPE_ARRAY
: {
258 int i
, rank
= type
->data
.array
->rank
;
259 MonoTypeNameFormat nested_format
;
261 nested_format
= format
== MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
?
262 MONO_TYPE_NAME_FORMAT_FULL_NAME
: format
;
264 mono_type_get_name_recurse (
265 &type
->data
.array
->eklass
->byval_arg
, str
, FALSE
, nested_format
);
266 g_string_append_c (str
, '[');
268 g_string_append_c (str
, '*');
269 for (i
= 1; i
< rank
; i
++)
270 g_string_append_c (str
, ',');
271 g_string_append_c (str
, ']');
273 mono_type_name_check_byref (type
, str
);
275 if (format
== MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
)
276 _mono_type_get_assembly_name (type
->data
.array
->eklass
, str
);
279 case MONO_TYPE_SZARRAY
: {
280 MonoTypeNameFormat nested_format
;
282 nested_format
= format
== MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
?
283 MONO_TYPE_NAME_FORMAT_FULL_NAME
: format
;
285 mono_type_get_name_recurse (
286 &type
->data
.klass
->byval_arg
, str
, FALSE
, nested_format
);
287 g_string_append (str
, "[]");
289 mono_type_name_check_byref (type
, str
);
291 if (format
== MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
)
292 _mono_type_get_assembly_name (type
->data
.klass
, str
);
295 case MONO_TYPE_PTR
: {
296 MonoTypeNameFormat nested_format
;
298 nested_format
= format
== MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
?
299 MONO_TYPE_NAME_FORMAT_FULL_NAME
: format
;
301 mono_type_get_name_recurse (
302 type
->data
.type
, str
, FALSE
, nested_format
);
303 g_string_append_c (str
, '*');
305 mono_type_name_check_byref (type
, str
);
307 if (format
== MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
)
308 _mono_type_get_assembly_name (mono_class_from_mono_type (type
->data
.type
), str
);
313 if (!mono_generic_param_info (type
->data
.generic_param
))
314 g_string_append_printf (str
, "%s%d", type
->type
== MONO_TYPE_VAR
? "!" : "!!", type
->data
.generic_param
->num
);
316 g_string_append (str
, mono_generic_param_info (type
->data
.generic_param
)->name
);
318 mono_type_name_check_byref (type
, str
);
322 klass
= mono_class_from_mono_type (type
);
323 if (klass
->nested_in
) {
324 mono_type_get_name_recurse (
325 &klass
->nested_in
->byval_arg
, str
, TRUE
, format
);
326 if (format
== MONO_TYPE_NAME_FORMAT_IL
)
327 g_string_append_c (str
, '.');
329 g_string_append_c (str
, '+');
330 } else if (*klass
->name_space
) {
331 g_string_append (str
, klass
->name_space
);
332 g_string_append_c (str
, '.');
334 if (format
== MONO_TYPE_NAME_FORMAT_IL
) {
335 char *s
= strchr (klass
->name
, '`');
336 int len
= s
? s
- klass
->name
: strlen (klass
->name
);
338 g_string_append_len (str
, klass
->name
, len
);
340 g_string_append (str
, klass
->name
);
343 if (klass
->generic_class
) {
344 MonoGenericClass
*gclass
= klass
->generic_class
;
345 MonoGenericInst
*inst
= gclass
->context
.class_inst
;
346 MonoTypeNameFormat nested_format
;
349 nested_format
= format
== MONO_TYPE_NAME_FORMAT_FULL_NAME
?
350 MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
: format
;
352 if (format
== MONO_TYPE_NAME_FORMAT_IL
)
353 g_string_append_c (str
, '<');
355 g_string_append_c (str
, '[');
356 for (i
= 0; i
< inst
->type_argc
; i
++) {
357 MonoType
*t
= inst
->type_argv
[i
];
360 g_string_append_c (str
, ',');
361 if ((nested_format
== MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
) &&
362 (t
->type
!= MONO_TYPE_VAR
) && (type
->type
!= MONO_TYPE_MVAR
))
363 g_string_append_c (str
, '[');
364 mono_type_get_name_recurse (inst
->type_argv
[i
], str
, FALSE
, nested_format
);
365 if ((nested_format
== MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
) &&
366 (t
->type
!= MONO_TYPE_VAR
) && (type
->type
!= MONO_TYPE_MVAR
))
367 g_string_append_c (str
, ']');
369 if (format
== MONO_TYPE_NAME_FORMAT_IL
)
370 g_string_append_c (str
, '>');
372 g_string_append_c (str
, ']');
373 } else if (klass
->generic_container
&&
374 (format
!= MONO_TYPE_NAME_FORMAT_FULL_NAME
) &&
375 (format
!= MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
)) {
378 if (format
== MONO_TYPE_NAME_FORMAT_IL
)
379 g_string_append_c (str
, '<');
381 g_string_append_c (str
, '[');
382 for (i
= 0; i
< klass
->generic_container
->type_argc
; i
++) {
384 g_string_append_c (str
, ',');
385 g_string_append (str
, mono_generic_container_get_param_info (klass
->generic_container
, i
)->name
);
387 if (format
== MONO_TYPE_NAME_FORMAT_IL
)
388 g_string_append_c (str
, '>');
390 g_string_append_c (str
, ']');
393 mono_type_name_check_byref (type
, str
);
395 if ((format
== MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
) &&
396 (type
->type
!= MONO_TYPE_VAR
) && (type
->type
!= MONO_TYPE_MVAR
))
397 _mono_type_get_assembly_name (klass
, str
);
403 * mono_type_get_name_full:
405 * @format: the format for the return string.
408 * Returns: the string representation in a number of formats:
410 * if format is MONO_TYPE_NAME_FORMAT_REFLECTION, the return string is
411 * returned in the formatrequired by System.Reflection, this is the
412 * inverse of mono_reflection_parse_type ().
414 * if format is MONO_TYPE_NAME_FORMAT_IL, it returns a syntax that can
415 * be used by the IL assembler.
417 * if format is MONO_TYPE_NAME_FORMAT_FULL_NAME
419 * if format is MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
422 mono_type_get_name_full (MonoType
*type
, MonoTypeNameFormat format
)
426 result
= g_string_new ("");
428 mono_type_get_name_recurse (type
, result
, FALSE
, format
);
430 return g_string_free (result
, FALSE
);
434 * mono_type_get_full_name:
437 * Returns: the string representation for type as required by System.Reflection.
438 * The inverse of mono_reflection_parse_type ().
441 mono_type_get_full_name (MonoClass
*class)
443 return mono_type_get_name_full (mono_class_get_type (class), MONO_TYPE_NAME_FORMAT_REFLECTION
);
447 * mono_type_get_name:
450 * Returns: the string representation for type as it would be represented in IL code.
453 mono_type_get_name (MonoType
*type
)
455 return mono_type_get_name_full (type
, MONO_TYPE_NAME_FORMAT_IL
);
459 * mono_type_get_underlying_type:
462 * Returns: the MonoType for the underlying integer type if @type
463 * is an enum and byref is false, otherwise the type itself.
466 mono_type_get_underlying_type (MonoType
*type
)
468 if (type
->type
== MONO_TYPE_VALUETYPE
&& type
->data
.klass
->enumtype
&& !type
->byref
)
469 return mono_class_enum_basetype (type
->data
.klass
);
470 if (type
->type
== MONO_TYPE_GENERICINST
&& type
->data
.generic_class
->container_class
->enumtype
&& !type
->byref
)
471 return mono_class_enum_basetype (type
->data
.generic_class
->container_class
);
476 * mono_class_is_open_constructed_type:
479 * Returns TRUE if type represents a generics open constructed type
480 * (not all the type parameters required for the instantiation have
484 mono_class_is_open_constructed_type (MonoType
*t
)
490 case MONO_TYPE_SZARRAY
:
491 return mono_class_is_open_constructed_type (&t
->data
.klass
->byval_arg
);
492 case MONO_TYPE_ARRAY
:
493 return mono_class_is_open_constructed_type (&t
->data
.array
->eklass
->byval_arg
);
495 return mono_class_is_open_constructed_type (t
->data
.type
);
496 case MONO_TYPE_GENERICINST
:
497 return t
->data
.generic_class
->context
.class_inst
->is_open
;
504 inflate_generic_type (MonoImage
*image
, MonoType
*type
, MonoGenericContext
*context
, MonoError
*error
)
506 mono_error_init (error
);
508 switch (type
->type
) {
509 case MONO_TYPE_MVAR
: {
511 int num
= mono_type_get_generic_param_num (type
);
512 MonoGenericInst
*inst
= context
->method_inst
;
513 if (!inst
|| !inst
->type_argv
)
515 if (num
>= inst
->type_argc
) {
516 MonoGenericParamInfo
*info
= mono_generic_param_info (type
->data
.generic_param
);
517 mono_error_set_bad_image (error
, image
, "MVAR %d (%s) cannot be expanded in this context with %d instantiations",
518 num
, info
? info
->name
: "", inst
->type_argc
);
523 * Note that the VAR/MVAR cases are different from the rest. The other cases duplicate @type,
524 * while the VAR/MVAR duplicates a type from the context. So, we need to ensure that the
525 * ->byref and ->attrs from @type are propagated to the returned type.
527 nt
= mono_metadata_type_dup (image
, inst
->type_argv
[num
]);
528 nt
->byref
= type
->byref
;
529 nt
->attrs
= type
->attrs
;
532 case MONO_TYPE_VAR
: {
534 int num
= mono_type_get_generic_param_num (type
);
535 MonoGenericInst
*inst
= context
->class_inst
;
538 if (num
>= inst
->type_argc
) {
539 MonoGenericParamInfo
*info
= mono_generic_param_info (type
->data
.generic_param
);
540 mono_error_set_bad_image (error
, image
, "VAR %d (%s) cannot be expanded in this context with %d instantiations",
541 num
, info
? info
->name
: "", inst
->type_argc
);
544 nt
= mono_metadata_type_dup (image
, inst
->type_argv
[num
]);
545 nt
->byref
= type
->byref
;
546 nt
->attrs
= type
->attrs
;
549 case MONO_TYPE_SZARRAY
: {
550 MonoClass
*eclass
= type
->data
.klass
;
551 MonoType
*nt
, *inflated
= inflate_generic_type (NULL
, &eclass
->byval_arg
, context
, error
);
552 if (!inflated
|| !mono_error_ok (error
))
554 nt
= mono_metadata_type_dup (image
, type
);
555 nt
->data
.klass
= mono_class_from_mono_type (inflated
);
556 mono_metadata_free_type (inflated
);
559 case MONO_TYPE_ARRAY
: {
560 MonoClass
*eclass
= type
->data
.array
->eklass
;
561 MonoType
*nt
, *inflated
= inflate_generic_type (NULL
, &eclass
->byval_arg
, context
, error
);
562 if (!inflated
|| !mono_error_ok (error
))
564 nt
= mono_metadata_type_dup (image
, type
);
565 nt
->data
.array
= g_memdup (nt
->data
.array
, sizeof (MonoArrayType
));
566 nt
->data
.array
->eklass
= mono_class_from_mono_type (inflated
);
567 mono_metadata_free_type (inflated
);
570 case MONO_TYPE_GENERICINST
: {
571 MonoGenericClass
*gclass
= type
->data
.generic_class
;
572 MonoGenericInst
*inst
;
574 if (!gclass
->context
.class_inst
->is_open
)
577 inst
= mono_metadata_inflate_generic_inst (gclass
->context
.class_inst
, context
, error
);
578 if (!mono_error_ok (error
))
580 if (inst
!= gclass
->context
.class_inst
)
581 gclass
= mono_metadata_lookup_generic_class (gclass
->container_class
, inst
, gclass
->is_dynamic
);
583 if (gclass
== type
->data
.generic_class
)
586 nt
= mono_metadata_type_dup (image
, type
);
587 nt
->data
.generic_class
= gclass
;
590 case MONO_TYPE_CLASS
:
591 case MONO_TYPE_VALUETYPE
: {
592 MonoClass
*klass
= type
->data
.klass
;
593 MonoGenericContainer
*container
= klass
->generic_container
;
594 MonoGenericInst
*inst
;
595 MonoGenericClass
*gclass
= NULL
;
601 /* We can't use context->class_inst directly, since it can have more elements */
602 inst
= mono_metadata_inflate_generic_inst (container
->context
.class_inst
, context
, error
);
603 if (!mono_error_ok (error
))
605 if (inst
== container
->context
.class_inst
)
608 gclass
= mono_metadata_lookup_generic_class (klass
, inst
, klass
->image
->dynamic
);
610 nt
= mono_metadata_type_dup (image
, type
);
611 nt
->type
= MONO_TYPE_GENERICINST
;
612 nt
->data
.generic_class
= gclass
;
622 mono_generic_class_get_context (MonoGenericClass
*gclass
)
624 return &gclass
->context
;
628 mono_class_get_context (MonoClass
*class)
630 return class->generic_class
? mono_generic_class_get_context (class->generic_class
) : NULL
;
634 * mono_class_get_generic_container:
636 * Return the generic container of KLASS which should be a generic type definition.
638 MonoGenericContainer
*
639 mono_class_get_generic_container (MonoClass
*klass
)
641 g_assert (klass
->is_generic
);
643 return klass
->generic_container
;
647 * mono_class_get_generic_class:
649 * Return the MonoGenericClass of KLASS, which should be a generic instance.
652 mono_class_get_generic_class (MonoClass
*klass
)
654 g_assert (klass
->is_inflated
);
656 return klass
->generic_class
;
660 * mono_class_inflate_generic_type_with_mempool:
661 * @mempool: a mempool
663 * @context: a generics context
664 * @error: error context
666 * The same as mono_class_inflate_generic_type, but allocates the MonoType
667 * from mempool if it is non-NULL. If it is NULL, the MonoType is
668 * allocated on the heap and is owned by the caller.
669 * The returned type can potentially be the same as TYPE, so it should not be
670 * modified by the caller, and it should be freed using mono_metadata_free_type ().
673 mono_class_inflate_generic_type_with_mempool (MonoImage
*image
, MonoType
*type
, MonoGenericContext
*context
, MonoError
*error
)
675 MonoType
*inflated
= NULL
;
676 mono_error_init (error
);
679 inflated
= inflate_generic_type (image
, type
, context
, error
);
680 if (!mono_error_ok (error
))
684 MonoType
*shared
= mono_metadata_get_shared_type (type
);
689 return mono_metadata_type_dup (image
, type
);
693 mono_stats
.inflated_type_count
++;
698 * mono_class_inflate_generic_type:
700 * @context: a generics context
702 * If @type is a generic type and @context is not NULL, instantiate it using the
703 * generics context @context.
705 * Returns: the instantiated type or a copy of @type. The returned MonoType is allocated
706 * on the heap and is owned by the caller. Returns NULL on error.
708 * @deprecated Please use mono_class_inflate_generic_type_checked instead
711 mono_class_inflate_generic_type (MonoType
*type
, MonoGenericContext
*context
)
715 result
= mono_class_inflate_generic_type_checked (type
, context
, &error
);
717 if (!mono_error_ok (&error
)) {
718 mono_error_cleanup (&error
);
725 * mono_class_inflate_generic_type:
727 * @context: a generics context
728 * @error: error context to use
730 * If @type is a generic type and @context is not NULL, instantiate it using the
731 * generics context @context.
733 * Returns: the instantiated type or a copy of @type. The returned MonoType is allocated
734 * on the heap and is owned by the caller.
737 mono_class_inflate_generic_type_checked (MonoType
*type
, MonoGenericContext
*context
, MonoError
*error
)
739 return mono_class_inflate_generic_type_with_mempool (NULL
, type
, context
, error
);
743 * mono_class_inflate_generic_type_no_copy:
745 * Same as inflate_generic_type_with_mempool, but return TYPE if no inflation
749 mono_class_inflate_generic_type_no_copy (MonoImage
*image
, MonoType
*type
, MonoGenericContext
*context
)
752 MonoType
*inflated
= NULL
;
755 inflated
= inflate_generic_type (image
, type
, context
, &error
);
756 g_assert (mono_error_ok (&error
)); /*FIXME proper error handling*/
762 mono_stats
.inflated_type_count
++;
767 * mono_class_inflate_generic_class:
769 * Inflate the class GKLASS with CONTEXT.
772 mono_class_inflate_generic_class (MonoClass
*gklass
, MonoGenericContext
*context
)
778 inflated
= mono_class_inflate_generic_type_checked (&gklass
->byval_arg
, context
, &error
);
779 g_assert (mono_error_ok (&error
)); /*FIXME proper error handling*/
781 res
= mono_class_from_mono_type (inflated
);
782 mono_metadata_free_type (inflated
);
787 static MonoGenericContext
788 inflate_generic_context (MonoGenericContext
*context
, MonoGenericContext
*inflate_with
, MonoError
*error
)
790 MonoGenericInst
*class_inst
= NULL
;
791 MonoGenericInst
*method_inst
= NULL
;
792 MonoGenericContext res
= { NULL
, NULL
};
794 mono_error_init (error
);
796 if (context
->class_inst
) {
797 class_inst
= mono_metadata_inflate_generic_inst (context
->class_inst
, inflate_with
, error
);
798 if (!mono_error_ok (error
))
802 if (context
->method_inst
) {
803 method_inst
= mono_metadata_inflate_generic_inst (context
->method_inst
, inflate_with
, error
);
804 if (!mono_error_ok (error
))
808 res
.class_inst
= class_inst
;
809 res
.method_inst
= method_inst
;
815 * mono_class_inflate_generic_method:
816 * @method: a generic method
817 * @context: a generics context
819 * Instantiate the generic method @method using the generics context @context.
821 * Returns: the new instantiated method
824 mono_class_inflate_generic_method (MonoMethod
*method
, MonoGenericContext
*context
)
826 return mono_class_inflate_generic_method_full (method
, NULL
, context
);
830 * mono_class_inflate_generic_method_full:
832 * Instantiate method @method with the generic context @context.
833 * BEWARE: All non-trivial fields are invalid, including klass, signature, and header.
834 * Use mono_method_signature () and mono_method_get_header () to get the correct values.
837 mono_class_inflate_generic_method_full (MonoMethod
*method
, MonoClass
*klass_hint
, MonoGenericContext
*context
)
840 MonoMethod
*res
= mono_class_inflate_generic_method_full_checked (method
, klass_hint
, context
, &error
);
841 if (!mono_error_ok (&error
))
842 /*FIXME do proper error handling - on this case, kill this function. */
843 g_error ("Could not inflate generic method due to %s", mono_error_get_message (&error
));
849 * mono_class_inflate_generic_method_full_checked:
850 * Same as mono_class_inflate_generic_method_full but return failure using @error.
853 mono_class_inflate_generic_method_full_checked (MonoMethod
*method
, MonoClass
*klass_hint
, MonoGenericContext
*context
, MonoError
*error
)
856 MonoMethodInflated
*iresult
, *cached
;
857 MonoMethodSignature
*sig
;
858 MonoGenericContext tmp_context
;
859 gboolean is_mb_open
= FALSE
;
861 mono_error_init (error
);
863 /* The `method' has already been instantiated before => we need to peel out the instantiation and create a new context */
864 while (method
->is_inflated
) {
865 MonoGenericContext
*method_context
= mono_method_get_context (method
);
866 MonoMethodInflated
*imethod
= (MonoMethodInflated
*) method
;
868 tmp_context
= inflate_generic_context (method_context
, context
, error
);
869 if (!mono_error_ok (error
))
871 context
= &tmp_context
;
873 if (mono_metadata_generic_context_equal (method_context
, context
))
876 method
= imethod
->declaring
;
879 if (!method
->is_generic
&& !method
->klass
->generic_container
)
883 * The reason for this hack is to fix the behavior of inflating generic methods that come from a MethodBuilder.
884 * What happens is that instantiating a generic MethodBuilder with its own arguments should create a diferent object.
885 * This is opposite to the way non-SRE MethodInfos behave.
887 * This happens, for example, when we want to emit a recursive generic method. Given the following C# code:
889 * void Example<T> () {
893 * In Example, the method token must be encoded as: "void Example<!!0>()"
895 * The reference to the first generic argument, "!!0", must be explicit otherwise it won't be inflated
896 * properly. To get that we need to inflate the MethodBuilder with its own arguments.
898 * On the other hand, inflating a non-SRE generic method with its own arguments should
899 * return itself. For example:
901 * MethodInfo m = ... //m is a generic method definition
902 * MethodInfo res = m.MakeGenericMethod (m.GetGenericArguments ());
905 * To allow such scenarios we must allow inflation of MethodBuilder to happen in a diferent way than
906 * what happens with regular methods.
908 * There is one last touch to this madness, once a TypeBuilder is finished, IOW CreateType() is called,
909 * everything should behave like a regular type or method.
912 is_mb_open
= method
->is_generic
&&
913 method
->klass
->image
->dynamic
&& !method
->klass
->wastypebuilder
&& /* that is a MethodBuilder from an unfinished TypeBuilder */
914 context
->method_inst
== mono_method_get_generic_container (method
)->context
.method_inst
; /* and it's been instantiated with its own arguments. */
916 iresult
= g_new0 (MonoMethodInflated
, 1);
917 iresult
->context
= *context
;
918 iresult
->declaring
= method
;
919 iresult
->method
.method
.is_mb_open
= is_mb_open
;
921 if (!context
->method_inst
&& method
->is_generic
)
922 iresult
->context
.method_inst
= mono_method_get_generic_container (method
)->context
.method_inst
;
924 if (!context
->class_inst
) {
925 g_assert (!iresult
->declaring
->klass
->generic_class
);
926 if (iresult
->declaring
->klass
->generic_container
)
927 iresult
->context
.class_inst
= iresult
->declaring
->klass
->generic_container
->context
.class_inst
;
928 else if (iresult
->declaring
->klass
->generic_class
)
929 iresult
->context
.class_inst
= iresult
->declaring
->klass
->generic_class
->context
.class_inst
;
933 cached
= mono_method_inflated_lookup (iresult
, FALSE
);
935 mono_loader_unlock ();
937 return (MonoMethod
*)cached
;
940 mono_stats
.inflated_method_count
++;
942 inflated_methods_size
+= sizeof (MonoMethodInflated
);
944 sig
= mono_method_signature (method
);
946 char *name
= mono_type_get_full_name (method
->klass
);
947 mono_error_set_bad_image (error
, method
->klass
->image
, "Could not resolve signature of method %s:%s", name
, method
->name
);
953 memcpy (&iresult
->method
.pinvoke
, method
, sizeof (MonoMethodPInvoke
));
955 memcpy (&iresult
->method
.normal
, method
, sizeof (MonoMethodNormal
));
956 iresult
->method
.normal
.header
= NULL
;
959 result
= (MonoMethod
*) iresult
;
960 result
->is_inflated
= TRUE
;
961 result
->is_generic
= FALSE
;
962 result
->signature
= NULL
;
963 result
->is_mb_open
= is_mb_open
;
965 if (!context
->method_inst
) {
966 /* Set the generic_container of the result to the generic_container of method */
967 MonoGenericContainer
*generic_container
= mono_method_get_generic_container (method
);
969 if (generic_container
) {
970 result
->is_generic
= 1;
971 mono_method_set_generic_container (result
, generic_container
);
975 if (!klass_hint
|| !klass_hint
->generic_class
||
976 klass_hint
->generic_class
->container_class
!= method
->klass
||
977 klass_hint
->generic_class
->context
.class_inst
!= context
->class_inst
)
980 if (method
->klass
->generic_container
)
981 result
->klass
= klass_hint
;
983 if (!result
->klass
) {
984 MonoType
*inflated
= inflate_generic_type (NULL
, &method
->klass
->byval_arg
, context
, error
);
985 if (!mono_error_ok (error
))
988 result
->klass
= inflated
? mono_class_from_mono_type (inflated
) : method
->klass
;
990 mono_metadata_free_type (inflated
);
993 mono_method_inflated_lookup (iresult
, TRUE
);
994 mono_loader_unlock ();
998 mono_loader_unlock ();
1004 * mono_get_inflated_method:
1006 * Obsolete. We keep it around since it's mentioned in the public API.
1009 mono_get_inflated_method (MonoMethod
*method
)
1015 * mono_method_get_context_general:
1017 * @uninflated: handle uninflated methods?
1019 * Returns the generic context of a method or NULL if it doesn't have
1020 * one. For an inflated method that's the context stored in the
1021 * method. Otherwise it's in the method's generic container or in the
1022 * generic container of the method's class.
1025 mono_method_get_context_general (MonoMethod
*method
, gboolean uninflated
)
1027 if (method
->is_inflated
) {
1028 MonoMethodInflated
*imethod
= (MonoMethodInflated
*) method
;
1029 return &imethod
->context
;
1033 if (method
->is_generic
)
1034 return &(mono_method_get_generic_container (method
)->context
);
1035 if (method
->klass
->generic_container
)
1036 return &method
->klass
->generic_container
->context
;
1041 * mono_method_get_context:
1044 * Returns the generic context for method if it's inflated, otherwise
1048 mono_method_get_context (MonoMethod
*method
)
1050 return mono_method_get_context_general (method
, FALSE
);
1054 * mono_method_get_generic_container:
1056 * Returns the generic container of METHOD, which should be a generic method definition.
1057 * Returns NULL if METHOD is not a generic method definition.
1058 * LOCKING: Acquires the loader lock.
1060 MonoGenericContainer
*
1061 mono_method_get_generic_container (MonoMethod
*method
)
1063 MonoGenericContainer
*container
;
1065 if (!method
->is_generic
)
1068 container
= mono_image_property_lookup (method
->klass
->image
, method
, MONO_METHOD_PROP_GENERIC_CONTAINER
);
1069 g_assert (container
);
1075 * mono_method_set_generic_container:
1077 * Sets the generic container of METHOD to CONTAINER.
1078 * LOCKING: Acquires the loader lock.
1081 mono_method_set_generic_container (MonoMethod
*method
, MonoGenericContainer
* container
)
1083 g_assert (method
->is_generic
);
1085 mono_image_property_insert (method
->klass
->image
, method
, MONO_METHOD_PROP_GENERIC_CONTAINER
, container
);
1089 * mono_class_find_enum_basetype:
1090 * @class: The enum class
1092 * Determine the basetype of an enum by iterating through its fields. We do this
1093 * in a separate function since it is cheaper than calling mono_class_setup_fields.
1096 mono_class_find_enum_basetype (MonoClass
*class)
1098 MonoGenericContainer
*container
= NULL
;
1099 MonoImage
*m
= class->image
;
1100 const int top
= class->field
.count
;
1103 g_assert (class->enumtype
);
1105 if (class->generic_container
)
1106 container
= class->generic_container
;
1107 else if (class->generic_class
) {
1108 MonoClass
*gklass
= class->generic_class
->container_class
;
1110 container
= gklass
->generic_container
;
1111 g_assert (container
);
1115 * Fetch all the field information.
1117 for (i
= 0; i
< top
; i
++){
1119 guint32 cols
[MONO_FIELD_SIZE
];
1120 int idx
= class->field
.first
+ i
;
1123 /* class->field.first and idx points into the fieldptr table */
1124 mono_metadata_decode_table_row (m
, MONO_TABLE_FIELD
, idx
, cols
, MONO_FIELD_SIZE
);
1126 if (cols
[MONO_FIELD_FLAGS
] & FIELD_ATTRIBUTE_STATIC
) //no need to decode static fields
1129 if (!mono_verifier_verify_field_signature (class->image
, cols
[MONO_FIELD_SIGNATURE
], NULL
))
1132 sig
= mono_metadata_blob_heap (m
, cols
[MONO_FIELD_SIGNATURE
]);
1133 mono_metadata_decode_value (sig
, &sig
);
1134 /* FIELD signature == 0x06 */
1138 ftype
= mono_metadata_parse_type_full (m
, container
, MONO_PARSE_FIELD
, cols
[MONO_FIELD_FLAGS
], sig
+ 1, &sig
);
1141 if (class->generic_class
) {
1142 //FIXME do we leak here?
1143 ftype
= mono_class_inflate_generic_type (ftype
, mono_class_get_context (class));
1144 ftype
->attrs
= cols
[MONO_FIELD_FLAGS
];
1154 * Checks for MonoClass::exception_type without resolving all MonoType's into MonoClass'es
1155 * It doesn't resolve generic instances, only check the GTD.
1158 mono_type_has_exceptions (MonoType
*type
)
1160 switch (type
->type
) {
1161 case MONO_TYPE_CLASS
:
1162 case MONO_TYPE_VALUETYPE
:
1163 case MONO_TYPE_SZARRAY
:
1164 return type
->data
.klass
->exception_type
;
1165 case MONO_TYPE_ARRAY
:
1166 return type
->data
.array
->eklass
->exception_type
;
1167 case MONO_TYPE_GENERICINST
:
1168 return type
->data
.generic_class
->container_class
->exception_type
;
1174 * mono_class_setup_fields:
1175 * @class: The class to initialize
1177 * Initializes the class->fields.
1178 * LOCKING: Assumes the loader lock is held.
1181 mono_class_setup_fields (MonoClass
*class)
1183 MonoImage
*m
= class->image
;
1184 int top
= class->field
.count
;
1185 guint32 layout
= class->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
;
1186 int i
, blittable
= TRUE
;
1187 guint32 real_size
= 0;
1188 guint32 packing_size
= 0;
1189 gboolean explicit_size
;
1190 MonoClassField
*field
;
1191 MonoGenericContainer
*container
= NULL
;
1192 MonoClass
*gtd
= class->generic_class
? mono_class_get_generic_type_definition (class) : NULL
;
1194 if (class->size_inited
)
1197 if (class->generic_class
&& class->generic_class
->container_class
->image
->dynamic
&& !class->generic_class
->container_class
->wastypebuilder
) {
1199 * This happens when a generic instance of an unfinished generic typebuilder
1200 * is used as an element type for creating an array type. We can't initialize
1201 * the fields of this class using the fields of gklass, since gklass is not
1202 * finished yet, fields could be added to it later.
1208 mono_class_setup_fields (gtd
);
1209 if (gtd
->exception_type
) {
1210 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
1214 top
= gtd
->field
.count
;
1215 class->field
.first
= gtd
->field
.first
;
1216 class->field
.count
= gtd
->field
.count
;
1219 class->instance_size
= 0;
1221 class->sizes
.class_size
= 0;
1223 if (class->parent
) {
1224 /* For generic instances, class->parent might not have been initialized */
1225 mono_class_init (class->parent
);
1226 if (!class->parent
->size_inited
) {
1227 mono_class_setup_fields (class->parent
);
1228 if (class->parent
->exception_type
) {
1229 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
1233 class->instance_size
+= class->parent
->instance_size
;
1234 class->min_align
= class->parent
->min_align
;
1235 /* we use |= since it may have been set already */
1236 class->has_references
|= class->parent
->has_references
;
1237 blittable
= class->parent
->blittable
;
1239 class->instance_size
= sizeof (MonoObject
);
1240 class->min_align
= 1;
1243 /* We can't really enable 16 bytes alignment until the GC supports it.
1244 The whole layout/instance size code must be reviewed because we do alignment calculation in terms of the
1245 boxed instance, which leads to unexplainable holes at the beginning of an object embedding a simd type.
1246 Bug #506144 is an example of this issue.
1248 if (class->simd_type)
1249 class->min_align = 16;
1251 /* Get the real size */
1252 explicit_size
= mono_metadata_packing_from_typedef (class->image
, class->type_token
, &packing_size
, &real_size
);
1254 if (explicit_size
) {
1255 g_assert ((packing_size
& 0xfffffff0) == 0);
1256 class->packing_size
= packing_size
;
1257 real_size
+= class->instance_size
;
1261 if (explicit_size
&& real_size
) {
1262 class->instance_size
= MAX (real_size
, class->instance_size
);
1264 class->size_inited
= 1;
1265 class->blittable
= blittable
;
1269 if (layout
== TYPE_ATTRIBUTE_AUTO_LAYOUT
)
1272 /* Prevent infinite loops if the class references itself */
1273 class->size_inited
= 1;
1275 class->fields
= mono_image_alloc0 (class->image
, sizeof (MonoClassField
) * top
);
1277 if (class->generic_container
) {
1278 container
= class->generic_container
;
1280 container
= gtd
->generic_container
;
1281 g_assert (container
);
1285 * Fetch all the field information.
1287 for (i
= 0; i
< top
; i
++){
1288 int idx
= class->field
.first
+ i
;
1289 field
= &class->fields
[i
];
1291 field
->parent
= class;
1294 MonoClassField
*gfield
= >d
->fields
[i
];
1296 field
->name
= mono_field_get_name (gfield
);
1297 /*This memory must come from the image mempool as we don't have a chance to free it.*/
1298 field
->type
= mono_class_inflate_generic_type_no_copy (class->image
, gfield
->type
, mono_class_get_context (class));
1299 g_assert (field
->type
->attrs
== gfield
->type
->attrs
);
1300 if (mono_field_is_deleted (field
))
1302 field
->offset
= gfield
->offset
;
1305 guint32 cols
[MONO_FIELD_SIZE
];
1307 /* class->field.first and idx points into the fieldptr table */
1308 mono_metadata_decode_table_row (m
, MONO_TABLE_FIELD
, idx
, cols
, MONO_FIELD_SIZE
);
1309 /* The name is needed for fieldrefs */
1310 field
->name
= mono_metadata_string_heap (m
, cols
[MONO_FIELD_NAME
]);
1311 if (!mono_verifier_verify_field_signature (class->image
, cols
[MONO_FIELD_SIGNATURE
], NULL
)) {
1312 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
1315 sig
= mono_metadata_blob_heap (m
, cols
[MONO_FIELD_SIGNATURE
]);
1316 mono_metadata_decode_value (sig
, &sig
);
1317 /* FIELD signature == 0x06 */
1318 g_assert (*sig
== 0x06);
1319 field
->type
= mono_metadata_parse_type_full (m
, container
, MONO_PARSE_FIELD
, cols
[MONO_FIELD_FLAGS
], sig
+ 1, &sig
);
1321 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
1324 if (mono_field_is_deleted (field
))
1326 if (layout
== TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) {
1328 mono_metadata_field_info (m
, idx
, &offset
, NULL
, NULL
);
1329 field
->offset
= offset
;
1330 if (field
->offset
== (guint32
)-1 && !(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
))
1331 g_warning ("%s not initialized correctly (missing field layout info for %s)",
1332 class->name
, mono_field_get_name (field
));
1336 /* Only do these checks if we still think this type is blittable */
1337 if (blittable
&& !(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)) {
1338 if (field
->type
->byref
|| MONO_TYPE_IS_REFERENCE (field
->type
)) {
1341 MonoClass
*field_class
= mono_class_from_mono_type (field
->type
);
1343 mono_class_setup_fields (field_class
);
1344 if (field_class
->exception_type
) {
1345 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
1349 if (!field_class
|| !field_class
->blittable
)
1354 if (class->enumtype
&& !(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)) {
1355 class->cast_class
= class->element_class
= mono_class_from_mono_type (field
->type
);
1356 blittable
= class->element_class
->blittable
;
1359 if (mono_type_has_exceptions (field
->type
)) {
1360 char *class_name
= mono_type_get_full_name (class);
1361 char *type_name
= mono_type_full_name (field
->type
);
1363 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
1364 g_warning ("Invalid type %s for instance field %s:%s", type_name
, class_name
, field
->name
);
1365 g_free (class_name
);
1369 /* The def_value of fields is compute lazily during vtable creation */
1372 if (class == mono_defaults
.string_class
)
1375 class->blittable
= blittable
;
1377 if (class->enumtype
&& !mono_class_enum_basetype (class)) {
1378 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
1381 if (explicit_size
&& real_size
) {
1382 class->instance_size
= MAX (real_size
, class->instance_size
);
1385 if (class->exception_type
)
1387 mono_class_layout_fields (class);
1389 /*valuetypes can't be bigger than 1Mb.*/
1390 if (class->valuetype
&& class->instance_size
> 0x100000)
1391 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
1395 * mono_class_setup_fields_locking:
1396 * @class: The class to initialize
1398 * Initializes the class->fields array of fields.
1399 * Aquires the loader lock.
1402 mono_class_setup_fields_locking (MonoClass
*class)
1404 mono_loader_lock ();
1405 mono_class_setup_fields (class);
1406 mono_loader_unlock ();
1410 * mono_class_has_references:
1412 * Returns whenever @klass->has_references is set, initializing it if needed.
1413 * Aquires the loader lock.
1416 mono_class_has_references (MonoClass
*klass
)
1418 if (klass
->init_pending
) {
1419 /* Be conservative */
1422 mono_class_init (klass
);
1424 return klass
->has_references
;
1428 /* useful until we keep track of gc-references in corlib etc. */
1430 #define IS_GC_REFERENCE(t) FALSE
1432 #define IS_GC_REFERENCE(t) ((t)->type == MONO_TYPE_U && class->image == mono_defaults.corlib)
1436 * mono_type_get_basic_type_from_generic:
1439 * Returns a closed type corresponding to the possibly open type
1443 mono_type_get_basic_type_from_generic (MonoType
*type
)
1445 /* When we do generic sharing we let type variables stand for reference types. */
1446 if (!type
->byref
&& (type
->type
== MONO_TYPE_VAR
|| type
->type
== MONO_TYPE_MVAR
))
1447 return &mono_defaults
.object_class
->byval_arg
;
1452 * mono_class_layout_fields:
1455 * Compute the placement of fields inside an object or struct, according to
1456 * the layout rules and set the following fields in @class:
1457 * - has_references (if the class contains instance references firled or structs that contain references)
1458 * - has_static_refs (same, but for static fields)
1459 * - instance_size (size of the object in memory)
1460 * - class_size (size needed for the static fields)
1461 * - size_inited (flag set when the instance_size is set)
1463 * LOCKING: this is supposed to be called with the loader lock held.
1466 mono_class_layout_fields (MonoClass
*class)
1469 const int top
= class->field
.count
;
1470 guint32 layout
= class->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
;
1471 guint32 pass
, passes
, real_size
;
1472 gboolean gc_aware_layout
= FALSE
;
1473 MonoClassField
*field
;
1476 * When we do generic sharing we need to have layout
1477 * information for open generic classes (either with a generic
1478 * context containing type variables or with a generic
1479 * container), so we don't return in that case anymore.
1483 * Enable GC aware auto layout: in this mode, reference
1484 * fields are grouped together inside objects, increasing collector
1486 * Requires that all classes whose layout is known to native code be annotated
1487 * with [StructLayout (LayoutKind.Sequential)]
1488 * Value types have gc_aware_layout disabled by default, as per
1489 * what the default is for other runtimes.
1491 /* corlib is missing [StructLayout] directives in many places */
1492 if (layout
== TYPE_ATTRIBUTE_AUTO_LAYOUT
) {
1493 if (class->image
!= mono_defaults
.corlib
&&
1494 class->byval_arg
.type
!= MONO_TYPE_VALUETYPE
)
1495 gc_aware_layout
= TRUE
;
1496 /* from System.dll, used in metadata/process.h */
1497 if (strcmp (class->name
, "ProcessStartInfo") == 0)
1498 gc_aware_layout
= FALSE
;
1501 /* Compute klass->has_references */
1503 * Process non-static fields first, since static fields might recursively
1504 * refer to the class itself.
1506 for (i
= 0; i
< top
; i
++) {
1509 field
= &class->fields
[i
];
1511 if (!(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)) {
1512 ftype
= mono_type_get_underlying_type (field
->type
);
1513 ftype
= mono_type_get_basic_type_from_generic (ftype
);
1514 if (MONO_TYPE_IS_REFERENCE (ftype
) || IS_GC_REFERENCE (ftype
) || ((MONO_TYPE_ISSTRUCT (ftype
) && mono_class_has_references (mono_class_from_mono_type (ftype
)))))
1515 class->has_references
= TRUE
;
1519 for (i
= 0; i
< top
; i
++) {
1522 field
= &class->fields
[i
];
1524 if (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
) {
1525 ftype
= mono_type_get_underlying_type (field
->type
);
1526 ftype
= mono_type_get_basic_type_from_generic (ftype
);
1527 if (MONO_TYPE_IS_REFERENCE (ftype
) || IS_GC_REFERENCE (ftype
) || ((MONO_TYPE_ISSTRUCT (ftype
) && mono_class_has_references (mono_class_from_mono_type (ftype
)))))
1528 class->has_static_refs
= TRUE
;
1532 for (i
= 0; i
< top
; i
++) {
1535 field
= &class->fields
[i
];
1537 ftype
= mono_type_get_underlying_type (field
->type
);
1538 ftype
= mono_type_get_basic_type_from_generic (ftype
);
1539 if (MONO_TYPE_IS_REFERENCE (ftype
) || IS_GC_REFERENCE (ftype
) || ((MONO_TYPE_ISSTRUCT (ftype
) && mono_class_has_references (mono_class_from_mono_type (ftype
))))) {
1540 if (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
1541 class->has_static_refs
= TRUE
;
1543 class->has_references
= TRUE
;
1548 * Compute field layout and total size (not considering static fields)
1552 case TYPE_ATTRIBUTE_AUTO_LAYOUT
:
1553 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT
:
1555 if (gc_aware_layout
)
1560 if (layout
!= TYPE_ATTRIBUTE_AUTO_LAYOUT
)
1564 real_size
= class->parent
->instance_size
;
1566 real_size
= sizeof (MonoObject
);
1568 for (pass
= 0; pass
< passes
; ++pass
) {
1569 for (i
= 0; i
< top
; i
++){
1574 field
= &class->fields
[i
];
1576 if (mono_field_is_deleted (field
))
1578 if (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
1581 ftype
= mono_type_get_underlying_type (field
->type
);
1582 ftype
= mono_type_get_basic_type_from_generic (ftype
);
1583 if (gc_aware_layout
) {
1584 if (MONO_TYPE_IS_REFERENCE (ftype
) || IS_GC_REFERENCE (ftype
) || ((MONO_TYPE_ISSTRUCT (ftype
) && mono_class_has_references (mono_class_from_mono_type (ftype
))))) {
1593 if ((top
== 1) && (class->instance_size
== sizeof (MonoObject
)) &&
1594 (strcmp (mono_field_get_name (field
), "$PRIVATE$") == 0)) {
1595 /* This field is a hack inserted by MCS to empty structures */
1599 size
= mono_type_size (field
->type
, &align
);
1601 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
1602 align
= class->packing_size
? MIN (class->packing_size
, align
): align
;
1603 /* if the field has managed references, we need to force-align it
1606 if (MONO_TYPE_IS_REFERENCE (ftype
) || IS_GC_REFERENCE (ftype
) || ((MONO_TYPE_ISSTRUCT (ftype
) && mono_class_has_references (mono_class_from_mono_type (ftype
)))))
1607 align
= MAX (align
, sizeof (gpointer
));
1609 class->min_align
= MAX (align
, class->min_align
);
1610 field
->offset
= real_size
;
1611 field
->offset
+= align
- 1;
1612 field
->offset
&= ~(align
- 1);
1613 real_size
= field
->offset
+ size
;
1616 class->instance_size
= MAX (real_size
, class->instance_size
);
1618 if (class->instance_size
& (class->min_align
- 1)) {
1619 class->instance_size
+= class->min_align
- 1;
1620 class->instance_size
&= ~(class->min_align
- 1);
1624 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
:
1626 for (i
= 0; i
< top
; i
++) {
1631 field
= &class->fields
[i
];
1634 * There must be info about all the fields in a type if it
1635 * uses explicit layout.
1638 if (mono_field_is_deleted (field
))
1640 if (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
1643 size
= mono_type_size (field
->type
, &align
);
1644 class->min_align
= MAX (align
, class->min_align
);
1647 * When we get here, field->offset is already set by the
1648 * loader (for either runtime fields or fields loaded from metadata).
1649 * The offset is from the start of the object: this works for both
1650 * classes and valuetypes.
1652 field
->offset
+= sizeof (MonoObject
);
1653 ftype
= mono_type_get_underlying_type (field
->type
);
1654 ftype
= mono_type_get_basic_type_from_generic (ftype
);
1655 if (MONO_TYPE_IS_REFERENCE (ftype
) || ((MONO_TYPE_ISSTRUCT (ftype
) && mono_class_has_references (mono_class_from_mono_type (ftype
))))) {
1656 if (field
->offset
% sizeof (gpointer
)) {
1657 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
1664 real_size
= MAX (real_size
, size
+ field
->offset
);
1666 class->instance_size
= MAX (real_size
, class->instance_size
);
1670 if (layout
!= TYPE_ATTRIBUTE_EXPLICIT_LAYOUT
) {
1672 * For small structs, set min_align to at least the struct size to improve
1673 * performance, and since the JIT memset/memcpy code assumes this and generates
1674 * unaligned accesses otherwise. See #78990 for a testcase.
1676 if (class->instance_size
<= sizeof (MonoObject
) + sizeof (gpointer
))
1677 class->min_align
= MAX (class->min_align
, class->instance_size
- sizeof (MonoObject
));
1680 class->size_inited
= 1;
1683 * Compute static field layout and size
1685 for (i
= 0; i
< top
; i
++){
1689 field
= &class->fields
[i
];
1691 if (!(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
) || field
->type
->attrs
& FIELD_ATTRIBUTE_LITERAL
)
1693 if (mono_field_is_deleted (field
))
1696 size
= mono_type_size (field
->type
, &align
);
1697 field
->offset
= class->sizes
.class_size
;
1698 field
->offset
+= align
- 1;
1699 field
->offset
&= ~(align
- 1);
1700 class->sizes
.class_size
= field
->offset
+ size
;
1705 create_array_method (MonoClass
*class, const char *name
, MonoMethodSignature
*sig
)
1709 method
= (MonoMethod
*) mono_image_alloc0 (class->image
, sizeof (MonoMethodPInvoke
));
1710 method
->klass
= class;
1711 method
->flags
= METHOD_ATTRIBUTE_PUBLIC
;
1712 method
->iflags
= METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
;
1713 method
->signature
= sig
;
1714 method
->name
= name
;
1717 if (name
[0] == '.') {
1718 method
->flags
|= METHOD_ATTRIBUTE_RT_SPECIAL_NAME
| METHOD_ATTRIBUTE_SPECIAL_NAME
;
1720 method
->iflags
|= METHOD_IMPL_ATTRIBUTE_RUNTIME
;
1726 * mono_class_setup_methods:
1729 * Initializes the 'methods' array in the klass.
1730 * Calling this method should be avoided if possible since it allocates a lot
1731 * of long-living MonoMethod structures.
1732 * Methods belonging to an interface are assigned a sequential slot starting
1735 * On failure this function sets class->exception_type
1738 mono_class_setup_methods (MonoClass
*class)
1741 MonoMethod
**methods
;
1746 mono_loader_lock ();
1748 if (class->methods
) {
1749 mono_loader_unlock ();
1753 if (class->generic_class
) {
1755 MonoClass
*gklass
= class->generic_class
->container_class
;
1757 mono_class_init (gklass
);
1758 if (!gklass
->exception_type
)
1759 mono_class_setup_methods (gklass
);
1760 if (gklass
->exception_type
) {
1761 /*FIXME make exception_data less opaque so it's possible to dup it here*/
1762 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Generic type definition failed to load"));
1763 mono_loader_unlock ();
1767 /* The + 1 makes this always non-NULL to pass the check in mono_class_setup_methods () */
1768 class->method
.count
= gklass
->method
.count
;
1769 methods
= g_new0 (MonoMethod
*, class->method
.count
+ 1);
1771 for (i
= 0; i
< class->method
.count
; i
++) {
1772 methods
[i
] = mono_class_inflate_generic_method_full_checked (
1773 gklass
->methods
[i
], class, mono_class_get_context (class), &error
);
1774 if (!mono_error_ok (&error
)) {
1775 char *method
= mono_method_full_name (gklass
->methods
[i
], TRUE
);
1776 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
)));
1779 mono_error_cleanup (&error
);
1780 mono_loader_unlock ();
1784 } else if (class->rank
) {
1785 MonoMethod
*amethod
;
1786 MonoMethodSignature
*sig
;
1787 int count_generic
= 0, first_generic
= 0;
1790 class->method
.count
= 3 + (class->rank
> 1? 2: 1);
1792 mono_class_setup_interfaces (class);
1794 if (class->interface_count
) {
1795 count_generic
= generic_array_methods (class);
1796 first_generic
= class->method
.count
;
1797 class->method
.count
+= class->interface_count
* count_generic
;
1800 methods
= mono_image_alloc0 (class->image
, sizeof (MonoMethod
*) * class->method
.count
);
1802 sig
= mono_metadata_signature_alloc (class->image
, class->rank
);
1803 sig
->ret
= &mono_defaults
.void_class
->byval_arg
;
1804 sig
->pinvoke
= TRUE
;
1805 sig
->hasthis
= TRUE
;
1806 for (i
= 0; i
< class->rank
; ++i
)
1807 sig
->params
[i
] = &mono_defaults
.int32_class
->byval_arg
;
1809 amethod
= create_array_method (class, ".ctor", sig
);
1810 methods
[method_num
++] = amethod
;
1811 if (class->rank
> 1) {
1812 sig
= mono_metadata_signature_alloc (class->image
, class->rank
* 2);
1813 sig
->ret
= &mono_defaults
.void_class
->byval_arg
;
1814 sig
->pinvoke
= TRUE
;
1815 sig
->hasthis
= TRUE
;
1816 for (i
= 0; i
< class->rank
* 2; ++i
)
1817 sig
->params
[i
] = &mono_defaults
.int32_class
->byval_arg
;
1819 amethod
= create_array_method (class, ".ctor", sig
);
1820 methods
[method_num
++] = amethod
;
1822 /* element Get (idx11, [idx2, ...]) */
1823 sig
= mono_metadata_signature_alloc (class->image
, class->rank
);
1824 sig
->ret
= &class->element_class
->byval_arg
;
1825 sig
->pinvoke
= TRUE
;
1826 sig
->hasthis
= TRUE
;
1827 for (i
= 0; i
< class->rank
; ++i
)
1828 sig
->params
[i
] = &mono_defaults
.int32_class
->byval_arg
;
1829 amethod
= create_array_method (class, "Get", sig
);
1830 methods
[method_num
++] = amethod
;
1831 /* element& Address (idx11, [idx2, ...]) */
1832 sig
= mono_metadata_signature_alloc (class->image
, class->rank
);
1833 sig
->ret
= &class->element_class
->this_arg
;
1834 sig
->pinvoke
= TRUE
;
1835 sig
->hasthis
= TRUE
;
1836 for (i
= 0; i
< class->rank
; ++i
)
1837 sig
->params
[i
] = &mono_defaults
.int32_class
->byval_arg
;
1838 amethod
= create_array_method (class, "Address", sig
);
1839 methods
[method_num
++] = amethod
;
1840 /* void Set (idx11, [idx2, ...], element) */
1841 sig
= mono_metadata_signature_alloc (class->image
, class->rank
+ 1);
1842 sig
->ret
= &mono_defaults
.void_class
->byval_arg
;
1843 sig
->pinvoke
= TRUE
;
1844 sig
->hasthis
= TRUE
;
1845 for (i
= 0; i
< class->rank
; ++i
)
1846 sig
->params
[i
] = &mono_defaults
.int32_class
->byval_arg
;
1847 sig
->params
[i
] = &class->element_class
->byval_arg
;
1848 amethod
= create_array_method (class, "Set", sig
);
1849 methods
[method_num
++] = amethod
;
1851 for (i
= 0; i
< class->interface_count
; i
++)
1852 setup_generic_array_ifaces (class, class->interfaces
[i
], methods
, first_generic
+ i
* count_generic
);
1854 methods
= mono_image_alloc (class->image
, sizeof (MonoMethod
*) * class->method
.count
);
1855 for (i
= 0; i
< class->method
.count
; ++i
) {
1856 int idx
= mono_metadata_translate_token_index (class->image
, MONO_TABLE_METHOD
, class->method
.first
+ i
+ 1);
1857 methods
[i
] = mono_get_method (class->image
, MONO_TOKEN_METHOD_DEF
| idx
, class);
1861 if (MONO_CLASS_IS_INTERFACE (class)) {
1863 /*Only assign slots to virtual methods as interfaces are allowed to have static methods.*/
1864 for (i
= 0; i
< class->method
.count
; ++i
) {
1865 if (methods
[i
]->flags
& METHOD_ATTRIBUTE_VIRTUAL
)
1866 methods
[i
]->slot
= slot
++;
1870 /* Needed because of the double-checking locking pattern */
1871 mono_memory_barrier ();
1873 class->methods
= methods
;
1875 if (mono_debugger_class_loaded_methods_func
)
1876 mono_debugger_class_loaded_methods_func (class);
1878 mono_loader_unlock ();
1882 * mono_class_get_method_by_index:
1884 * Returns class->methods [index], initializing class->methods if neccesary.
1886 * LOCKING: Acquires the loader lock.
1889 mono_class_get_method_by_index (MonoClass
*class, int index
)
1891 /* Avoid calling setup_methods () if possible */
1892 if (class->generic_class
&& !class->methods
) {
1893 MonoClass
*gklass
= class->generic_class
->container_class
;
1896 m
= mono_class_inflate_generic_method_full (
1897 gklass
->methods
[index
], class, mono_class_get_context (class));
1899 * If setup_methods () is called later for this class, no duplicates are created,
1900 * since inflate_generic_method guarantees that only one instance of a method
1901 * is created for each context.
1904 mono_class_setup_methods (class);
1905 g_assert (m == class->methods [index]);
1909 mono_class_setup_methods (class);
1910 g_assert (!class->exception_type
); /*FIXME do proper error handling*/
1911 g_assert (index
>= 0 && index
< class->method
.count
);
1912 return class->methods
[index
];
1917 * mono_class_get_inflated_method:
1919 * Given an inflated class CLASS and a method METHOD which should be a method of
1920 * CLASS's generic definition, return the inflated method corresponding to METHOD.
1923 mono_class_get_inflated_method (MonoClass
*class, MonoMethod
*method
)
1925 MonoClass
*gklass
= class->generic_class
->container_class
;
1928 g_assert (method
->klass
== gklass
);
1930 mono_class_setup_methods (gklass
);
1931 g_assert (!gklass
->exception_type
); /*FIXME do proper error handling*/
1933 for (i
= 0; i
< gklass
->method
.count
; ++i
) {
1934 if (gklass
->methods
[i
] == method
) {
1936 return class->methods
[i
];
1938 return mono_class_inflate_generic_method_full (gklass
->methods
[i
], class, mono_class_get_context (class));
1946 * mono_class_get_vtable_entry:
1948 * Returns class->vtable [offset], computing it if neccesary.
1949 * LOCKING: Acquires the loader lock.
1952 mono_class_get_vtable_entry (MonoClass
*class, int offset
)
1956 if (class->rank
== 1) {
1958 * szarrays do not overwrite any methods of Array, so we can avoid
1959 * initializing their vtables in some cases.
1961 mono_class_setup_vtable (class->parent
);
1962 if (offset
< class->parent
->vtable_size
)
1963 return class->parent
->vtable
[offset
];
1966 if (class->generic_class
) {
1967 MonoClass
*gklass
= class->generic_class
->container_class
;
1968 mono_class_setup_vtable (gklass
);
1969 m
= gklass
->vtable
[offset
];
1971 m
= mono_class_inflate_generic_method_full (m
, class, mono_class_get_context (class));
1973 mono_class_setup_vtable (class);
1974 m
= class->vtable
[offset
];
1981 * mono_class_get_vtable_size:
1983 * Return the vtable size for KLASS.
1986 mono_class_get_vtable_size (MonoClass
*klass
)
1988 mono_class_setup_vtable (klass
);
1990 return klass
->vtable_size
;
1993 /*This method can fail the class.*/
1995 mono_class_setup_properties (MonoClass
*class)
1997 guint startm
, endm
, i
, j
;
1998 guint32 cols
[MONO_PROPERTY_SIZE
];
1999 MonoTableInfo
*msemt
= &class->image
->tables
[MONO_TABLE_METHODSEMANTICS
];
2000 MonoProperty
*properties
;
2003 if (class->ext
&& class->ext
->properties
)
2006 mono_loader_lock ();
2008 if (class->ext
&& class->ext
->properties
) {
2009 mono_loader_unlock ();
2013 mono_class_alloc_ext (class);
2015 if (class->generic_class
) {
2016 MonoClass
*gklass
= class->generic_class
->container_class
;
2018 mono_class_init (gklass
);
2019 mono_class_setup_properties (gklass
);
2020 if (gklass
->exception_type
) {
2021 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Generic type definition failed to load"));
2022 mono_loader_unlock ();
2026 class->ext
->property
= gklass
->ext
->property
;
2028 properties
= g_new0 (MonoProperty
, class->ext
->property
.count
+ 1);
2030 for (i
= 0; i
< class->ext
->property
.count
; i
++) {
2031 MonoProperty
*prop
= &properties
[i
];
2033 *prop
= gklass
->ext
->properties
[i
];
2036 prop
->get
= mono_class_inflate_generic_method_full (
2037 prop
->get
, class, mono_class_get_context (class));
2039 prop
->set
= mono_class_inflate_generic_method_full (
2040 prop
->set
, class, mono_class_get_context (class));
2042 prop
->parent
= class;
2045 int first
= mono_metadata_properties_from_typedef (class->image
, mono_metadata_token_index (class->type_token
) - 1, &last
);
2046 int count
= last
- first
;
2049 mono_class_setup_methods (class);
2050 if (class->exception_type
) {
2051 mono_loader_unlock ();
2056 class->ext
->property
.first
= first
;
2057 class->ext
->property
.count
= count
;
2058 properties
= mono_image_alloc0 (class->image
, sizeof (MonoProperty
) * count
);
2059 for (i
= first
; i
< last
; ++i
) {
2060 mono_metadata_decode_table_row (class->image
, MONO_TABLE_PROPERTY
, i
, cols
, MONO_PROPERTY_SIZE
);
2061 properties
[i
- first
].parent
= class;
2062 properties
[i
- first
].attrs
= cols
[MONO_PROPERTY_FLAGS
];
2063 properties
[i
- first
].name
= mono_metadata_string_heap (class->image
, cols
[MONO_PROPERTY_NAME
]);
2065 startm
= mono_metadata_methods_from_property (class->image
, i
, &endm
);
2066 for (j
= startm
; j
< endm
; ++j
) {
2069 mono_metadata_decode_row (msemt
, j
, cols
, MONO_METHOD_SEMA_SIZE
);
2071 if (class->image
->uncompressed_metadata
)
2072 /* It seems like the MONO_METHOD_SEMA_METHOD column needs no remapping */
2073 method
= mono_get_method (class->image
, MONO_TOKEN_METHOD_DEF
| cols
[MONO_METHOD_SEMA_METHOD
], class);
2075 method
= class->methods
[cols
[MONO_METHOD_SEMA_METHOD
] - 1 - class->method
.first
];
2077 switch (cols
[MONO_METHOD_SEMA_SEMANTICS
]) {
2078 case METHOD_SEMANTIC_SETTER
:
2079 properties
[i
- first
].set
= method
;
2081 case METHOD_SEMANTIC_GETTER
:
2082 properties
[i
- first
].get
= method
;
2090 /*Flush any pending writes as we do double checked locking on class->properties */
2091 mono_memory_barrier ();
2093 /* Leave this assignment as the last op in the function */
2094 class->ext
->properties
= properties
;
2096 mono_loader_unlock ();
2100 inflate_method_listz (MonoMethod
**methods
, MonoClass
*class, MonoGenericContext
*context
)
2102 MonoMethod
**om
, **retval
;
2105 for (om
= methods
, count
= 0; *om
; ++om
, ++count
)
2108 retval
= g_new0 (MonoMethod
*, count
+ 1);
2110 for (om
= methods
, count
= 0; *om
; ++om
, ++count
)
2111 retval
[count
] = mono_class_inflate_generic_method_full (*om
, class, context
);
2116 /*This method can fail the class.*/
2118 mono_class_setup_events (MonoClass
*class)
2121 guint startm
, endm
, i
, j
;
2122 guint32 cols
[MONO_EVENT_SIZE
];
2123 MonoTableInfo
*msemt
= &class->image
->tables
[MONO_TABLE_METHODSEMANTICS
];
2127 if (class->ext
&& class->ext
->events
)
2130 mono_loader_lock ();
2132 if (class->ext
&& class->ext
->events
) {
2133 mono_loader_unlock ();
2137 mono_class_alloc_ext (class);
2139 if (class->generic_class
) {
2140 MonoClass
*gklass
= class->generic_class
->container_class
;
2141 MonoGenericContext
*context
;
2143 mono_class_setup_events (gklass
);
2144 if (gklass
->exception_type
) {
2145 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Generic type definition failed to load"));
2146 mono_loader_unlock ();
2150 class->ext
->event
= gklass
->ext
->event
;
2151 class->ext
->events
= g_new0 (MonoEvent
, class->ext
->event
.count
);
2153 if (class->ext
->event
.count
)
2154 context
= mono_class_get_context (class);
2156 for (i
= 0; i
< class->ext
->event
.count
; i
++) {
2157 MonoEvent
*event
= &class->ext
->events
[i
];
2158 MonoEvent
*gevent
= &gklass
->ext
->events
[i
];
2160 event
->parent
= class;
2161 event
->name
= gevent
->name
;
2162 event
->add
= gevent
->add
? mono_class_inflate_generic_method_full (gevent
->add
, class, context
) : NULL
;
2163 event
->remove
= gevent
->remove
? mono_class_inflate_generic_method_full (gevent
->remove
, class, context
) : NULL
;
2164 event
->raise
= gevent
->raise
? mono_class_inflate_generic_method_full (gevent
->raise
, class, context
) : NULL
;
2165 event
->other
= gevent
->other
? inflate_method_listz (gevent
->other
, class, context
) : NULL
;
2166 event
->attrs
= gevent
->attrs
;
2169 mono_loader_unlock ();
2173 first
= mono_metadata_events_from_typedef (class->image
, mono_metadata_token_index (class->type_token
) - 1, &last
);
2174 count
= last
- first
;
2177 mono_class_setup_methods (class);
2178 if (class->exception_type
) {
2179 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Generic type definition failed to load"));
2180 mono_loader_unlock ();
2184 class->ext
->event
.first
= first
;
2185 class->ext
->event
.count
= count
;
2186 events
= mono_image_alloc0 (class->image
, sizeof (MonoEvent
) * class->ext
->event
.count
);
2187 for (i
= first
; i
< last
; ++i
) {
2188 MonoEvent
*event
= &events
[i
- first
];
2190 mono_metadata_decode_table_row (class->image
, MONO_TABLE_EVENT
, i
, cols
, MONO_EVENT_SIZE
);
2191 event
->parent
= class;
2192 event
->attrs
= cols
[MONO_EVENT_FLAGS
];
2193 event
->name
= mono_metadata_string_heap (class->image
, cols
[MONO_EVENT_NAME
]);
2195 startm
= mono_metadata_methods_from_event (class->image
, i
, &endm
);
2196 for (j
= startm
; j
< endm
; ++j
) {
2199 mono_metadata_decode_row (msemt
, j
, cols
, MONO_METHOD_SEMA_SIZE
);
2201 if (class->image
->uncompressed_metadata
)
2202 /* It seems like the MONO_METHOD_SEMA_METHOD column needs no remapping */
2203 method
= mono_get_method (class->image
, MONO_TOKEN_METHOD_DEF
| cols
[MONO_METHOD_SEMA_METHOD
], class);
2205 method
= class->methods
[cols
[MONO_METHOD_SEMA_METHOD
] - 1 - class->method
.first
];
2207 switch (cols
[MONO_METHOD_SEMA_SEMANTICS
]) {
2208 case METHOD_SEMANTIC_ADD_ON
:
2209 event
->add
= method
;
2211 case METHOD_SEMANTIC_REMOVE_ON
:
2212 event
->remove
= method
;
2214 case METHOD_SEMANTIC_FIRE
:
2215 event
->raise
= method
;
2217 case METHOD_SEMANTIC_OTHER
: {
2220 if (event
->other
== NULL
) {
2221 event
->other
= g_new0 (MonoMethod
*, 2);
2223 while (event
->other
[n
])
2225 event
->other
= g_realloc (event
->other
, (n
+ 2) * sizeof (MonoMethod
*));
2227 event
->other
[n
] = method
;
2228 /* NULL terminated */
2229 event
->other
[n
+ 1] = NULL
;
2237 /*Flush any pending writes as we do double checked locking on class->properties */
2238 mono_memory_barrier ();
2240 /* Leave this assignment as the last op in the function */
2241 class->ext
->events
= events
;
2243 mono_loader_unlock ();
2247 * Global pool of interface IDs, represented as a bitset.
2248 * LOCKING: this is supposed to be accessed with the loader lock held.
2250 static MonoBitSet
*global_interface_bitset
= NULL
;
2253 * mono_unload_interface_ids:
2254 * @bitset: bit set of interface IDs
2256 * When an image is unloaded, the interface IDs associated with
2257 * the image are put back in the global pool of IDs so the numbers
2261 mono_unload_interface_ids (MonoBitSet
*bitset
)
2263 mono_loader_lock ();
2264 mono_bitset_sub (global_interface_bitset
, bitset
);
2265 mono_loader_unlock ();
2269 * mono_get_unique_iid:
2272 * Assign a unique integer ID to the interface represented by @class.
2273 * The ID will positive and as small as possible.
2274 * LOCKING: this is supposed to be called with the loader lock held.
2275 * Returns: the new ID.
2278 mono_get_unique_iid (MonoClass
*class)
2282 g_assert (MONO_CLASS_IS_INTERFACE (class));
2284 if (!global_interface_bitset
) {
2285 global_interface_bitset
= mono_bitset_new (128, 0);
2288 iid
= mono_bitset_find_first_unset (global_interface_bitset
, -1);
2290 int old_size
= mono_bitset_size (global_interface_bitset
);
2291 MonoBitSet
*new_set
= mono_bitset_clone (global_interface_bitset
, old_size
* 2);
2292 mono_bitset_free (global_interface_bitset
);
2293 global_interface_bitset
= new_set
;
2296 mono_bitset_set (global_interface_bitset
, iid
);
2297 /* set the bit also in the per-image set */
2298 if (class->image
->interface_bitset
) {
2299 if (iid
>= mono_bitset_size (class->image
->interface_bitset
)) {
2300 MonoBitSet
*new_set
= mono_bitset_clone (class->image
->interface_bitset
, iid
+ 1);
2301 mono_bitset_free (class->image
->interface_bitset
);
2302 class->image
->interface_bitset
= new_set
;
2305 class->image
->interface_bitset
= mono_bitset_new (iid
+ 1, 0);
2307 mono_bitset_set (class->image
->interface_bitset
, iid
);
2309 if (mono_print_vtable
) {
2311 char *type_name
= mono_type_full_name (&class->byval_arg
);
2312 if (class->generic_class
&& !class->generic_class
->context
.class_inst
->is_open
) {
2313 generic_id
= class->generic_class
->context
.class_inst
->id
;
2314 g_assert (generic_id
!= 0);
2318 printf ("Interface: assigned id %d to %s|%s|%d\n", iid
, class->image
->name
, type_name
, generic_id
);
2322 g_assert (iid
<= 65535);
2327 collect_implemented_interfaces_aux (MonoClass
*klass
, GPtrArray
**res
)
2332 mono_class_setup_interfaces (klass
);
2334 for (i
= 0; i
< klass
->interface_count
; i
++) {
2335 ic
= klass
->interfaces
[i
];
2338 *res
= g_ptr_array_new ();
2339 g_ptr_array_add (*res
, ic
);
2340 mono_class_init (ic
);
2342 collect_implemented_interfaces_aux (ic
, res
);
2347 mono_class_get_implemented_interfaces (MonoClass
*klass
)
2349 GPtrArray
*res
= NULL
;
2351 collect_implemented_interfaces_aux (klass
, &res
);
2356 compare_interface_ids (const void *p_key
, const void *p_element
) {
2357 const MonoClass
*key
= p_key
;
2358 const MonoClass
*element
= *(MonoClass
**) p_element
;
2360 return (key
->interface_id
- element
->interface_id
);
2363 /*FIXME verify all callers if they should switch to mono_class_interface_offset_with_variance*/
2365 mono_class_interface_offset (MonoClass
*klass
, MonoClass
*itf
) {
2366 MonoClass
**result
= bsearch (
2368 klass
->interfaces_packed
,
2369 klass
->interface_offsets_count
,
2370 sizeof (MonoClass
*),
2371 compare_interface_ids
);
2373 return klass
->interface_offsets_packed
[result
- (klass
->interfaces_packed
)];
2380 * mono_class_interface_offset_with_variance:
2382 * Return the interface offset of @itf in @klass. Sets @non_exact_match to TRUE if the match required variance check
2383 * If @itf is an interface with generic variant arguments, try to find the compatible one.
2385 * Note that this function is responsible for resolving ambiguities. Right now we use whatever ordering interfaces_packed gives us.
2387 * FIXME figure out MS disambiguation rules and fix this function.
2390 mono_class_interface_offset_with_variance (MonoClass
*klass
, MonoClass
*itf
, gboolean
*non_exact_match
) {
2391 int i
= mono_class_interface_offset (klass
, itf
);
2392 *non_exact_match
= FALSE
;
2396 if (!mono_class_has_variant_generic_params (itf
))
2399 for (i
= 0; i
< klass
->interface_offsets_count
; i
++) {
2400 if (mono_class_is_variant_compatible (itf
, klass
->interfaces_packed
[i
])) {
2401 *non_exact_match
= TRUE
;
2402 return klass
->interface_offsets_packed
[i
];
2410 print_implemented_interfaces (MonoClass
*klass
) {
2411 GPtrArray
*ifaces
= NULL
;
2413 int ancestor_level
= 0;
2415 printf ("Packed interface table for class %s has size %d\n", klass
->name
, klass
->interface_offsets_count
);
2416 for (i
= 0; i
< klass
->interface_offsets_count
; i
++)
2417 printf (" [%03d][UUID %03d][SLOT %03d][SIZE %03d] interface %s.%s\n", i
,
2418 klass
->interfaces_packed
[i
]->interface_id
,
2419 klass
->interface_offsets_packed
[i
],
2420 klass
->interfaces_packed
[i
]->method
.count
,
2421 klass
->interfaces_packed
[i
]->name_space
,
2422 klass
->interfaces_packed
[i
]->name
);
2423 printf ("Interface flags: ");
2424 for (i
= 0; i
<= klass
->max_interface_id
; i
++)
2425 if (MONO_CLASS_IMPLEMENTS_INTERFACE (klass
, i
))
2426 printf ("(%d,T)", i
);
2428 printf ("(%d,F)", i
);
2430 printf ("Dump interface flags:");
2431 for (i
= 0; i
< ((((klass
->max_interface_id
+ 1) >> 3)) + (((klass
->max_interface_id
+ 1) & 7)? 1 :0)); i
++)
2432 printf (" %02X", klass
->interface_bitmap
[i
]);
2434 while (klass
!= NULL
) {
2435 printf ("[LEVEL %d] Implemented interfaces by class %s:\n", ancestor_level
, klass
->name
);
2436 ifaces
= mono_class_get_implemented_interfaces (klass
);
2438 for (i
= 0; i
< ifaces
->len
; i
++) {
2439 MonoClass
*ic
= g_ptr_array_index (ifaces
, i
);
2440 printf (" [UIID %d] interface %s\n", ic
->interface_id
, ic
->name
);
2441 printf (" [%03d][UUID %03d][SLOT %03d][SIZE %03d] interface %s.%s\n", i
,
2443 mono_class_interface_offset (klass
, ic
),
2448 g_ptr_array_free (ifaces
, TRUE
);
2451 klass
= klass
->parent
;
2456 inflate_class_one_arg (MonoClass
*gtype
, MonoClass
*arg0
)
2459 args
[0] = &arg0
->byval_arg
;
2461 return mono_class_bind_generic_parameters (gtype
, 1, args
, FALSE
);
2465 array_class_get_if_rank (MonoClass
*class, guint rank
)
2467 return rank
? mono_array_class_get (class, rank
) : class;
2471 fill_valuetype_array_derived_types (MonoClass
**valuetype_types
, MonoClass
*eclass
, int rank
)
2473 valuetype_types
[0] = eclass
;
2474 if (eclass
== mono_defaults
.int16_class
)
2475 valuetype_types
[1] = mono_defaults
.uint16_class
;
2476 else if (eclass
== mono_defaults
.uint16_class
)
2477 valuetype_types
[1] = mono_defaults
.int16_class
;
2478 else if (eclass
== mono_defaults
.int32_class
)
2479 valuetype_types
[1] = mono_defaults
.uint32_class
;
2480 else if (eclass
== mono_defaults
.uint32_class
)
2481 valuetype_types
[1] = mono_defaults
.int32_class
;
2482 else if (eclass
== mono_defaults
.int64_class
)
2483 valuetype_types
[1] = mono_defaults
.uint64_class
;
2484 else if (eclass
== mono_defaults
.uint64_class
)
2485 valuetype_types
[1] = mono_defaults
.int64_class
;
2486 else if (eclass
== mono_defaults
.byte_class
)
2487 valuetype_types
[1] = mono_defaults
.sbyte_class
;
2488 else if (eclass
== mono_defaults
.sbyte_class
)
2489 valuetype_types
[1] = mono_defaults
.byte_class
;
2490 else if (eclass
->enumtype
&& mono_class_enum_basetype (eclass
))
2491 valuetype_types
[1] = mono_class_from_mono_type (mono_class_enum_basetype (eclass
));
2494 /* this won't be needed once bug #325495 is completely fixed
2495 * though we'll need something similar to know which interfaces to allow
2496 * in arrays when they'll be lazyly created
2498 * FIXME: System.Array/InternalEnumerator don't need all this interface fabrication machinery.
2499 * MS returns diferrent types based on which instance is called. For example:
2500 * object obj = new byte[10][];
2501 * Type a = ((IEnumerable<byte[]>)obj).GetEnumerator ().GetType ();
2502 * Type b = ((IEnumerable<IList<byte>>)obj).GetEnumerator ().GetType ();
2505 * Fixing this should kill quite some code, save some bits and improve compatibility.
2508 get_implicit_generic_array_interfaces (MonoClass
*class, int *num
, int *is_enumerator
)
2510 MonoClass
*eclass
= class->element_class
;
2511 static MonoClass
* generic_icollection_class
= NULL
;
2512 static MonoClass
* generic_ienumerable_class
= NULL
;
2513 static MonoClass
* generic_ienumerator_class
= NULL
;
2514 MonoClass
*valuetype_types
[2] = { NULL
, NULL
};
2515 MonoClass
**interfaces
= NULL
;
2516 int i
, interface_count
, real_count
, original_rank
;
2518 gboolean internal_enumerator
;
2519 gboolean eclass_is_valuetype
;
2521 if (!mono_defaults
.generic_ilist_class
) {
2525 internal_enumerator
= FALSE
;
2526 eclass_is_valuetype
= FALSE
;
2527 original_rank
= eclass
->rank
;
2528 if (class->byval_arg
.type
!= MONO_TYPE_SZARRAY
) {
2529 if (class->generic_class
&& class->nested_in
== mono_defaults
.array_class
&& strcmp (class->name
, "InternalEnumerator`1") == 0) {
2531 * For a Enumerator<T[]> we need to get the list of interfaces for T.
2533 eclass
= mono_class_from_mono_type (class->generic_class
->context
.class_inst
->type_argv
[0]);
2534 original_rank
= eclass
->rank
;
2535 eclass
= eclass
->element_class
;
2536 internal_enumerator
= TRUE
;
2537 *is_enumerator
= TRUE
;
2545 * with this non-lazy impl we can't implement all the interfaces so we do just the minimal stuff
2546 * for deep levels of arrays of arrays (string[][] has all the interfaces, string[][][] doesn't)
2548 all_interfaces
= eclass
->rank
&& eclass
->element_class
->rank
? FALSE
: TRUE
;
2550 if (!generic_icollection_class
) {
2551 generic_icollection_class
= mono_class_from_name (mono_defaults
.corlib
,
2552 "System.Collections.Generic", "ICollection`1");
2553 generic_ienumerable_class
= mono_class_from_name (mono_defaults
.corlib
,
2554 "System.Collections.Generic", "IEnumerable`1");
2555 generic_ienumerator_class
= mono_class_from_name (mono_defaults
.corlib
,
2556 "System.Collections.Generic", "IEnumerator`1");
2559 mono_class_init (eclass
);
2562 * Arrays in 2.0 need to implement a number of generic interfaces
2563 * (IList`1, ICollection`1, IEnumerable`1 for a number of types depending
2564 * on the element class). We collect the types needed to build the
2565 * instantiations in interfaces at intervals of 3, because 3 are
2566 * the generic interfaces needed to implement.
2568 if (eclass
->valuetype
) {
2569 fill_valuetype_array_derived_types (valuetype_types
, eclass
, original_rank
);
2571 /* IList, ICollection, IEnumerable */
2572 real_count
= interface_count
= valuetype_types
[1] ? 6 : 3;
2573 if (internal_enumerator
) {
2575 if (valuetype_types
[1])
2579 interfaces
= g_malloc0 (sizeof (MonoClass
*) * real_count
);
2580 interfaces
[0] = valuetype_types
[0];
2581 if (valuetype_types
[1])
2582 interfaces
[3] = valuetype_types
[1];
2584 eclass_is_valuetype
= TRUE
;
2587 int idepth
= eclass
->idepth
;
2588 if (!internal_enumerator
)
2590 interface_count
= all_interfaces
? eclass
->interface_offsets_count
: eclass
->interface_count
;
2591 /* we add object for interfaces and the supertypes for the other
2592 * types. The last of the supertypes is the element class itself which we
2593 * already created the explicit interfaces for (so we include it for IEnumerator
2594 * and exclude it for arrays).
2596 if (MONO_CLASS_IS_INTERFACE (eclass
))
2599 interface_count
+= idepth
;
2600 if (eclass
->rank
&& eclass
->element_class
->valuetype
) {
2601 fill_valuetype_array_derived_types (valuetype_types
, eclass
->element_class
, original_rank
);
2602 if (valuetype_types
[1])
2605 /* IList, ICollection, IEnumerable */
2606 interface_count
*= 3;
2607 real_count
= interface_count
;
2608 if (internal_enumerator
) {
2609 real_count
+= (MONO_CLASS_IS_INTERFACE (eclass
) ? 1 : idepth
) + eclass
->interface_offsets_count
;
2610 if (valuetype_types
[1])
2613 interfaces
= g_malloc0 (sizeof (MonoClass
*) * real_count
);
2614 if (MONO_CLASS_IS_INTERFACE (eclass
)) {
2615 interfaces
[0] = mono_defaults
.object_class
;
2619 for (i
= 0; i
< idepth
; i
++) {
2620 mono_class_init (eclass
->supertypes
[i
]);
2621 interfaces
[j
] = eclass
->supertypes
[i
];
2625 if (all_interfaces
) {
2626 for (i
= 0; i
< eclass
->interface_offsets_count
; i
++) {
2627 interfaces
[j
] = eclass
->interfaces_packed
[i
];
2631 for (i
= 0; i
< eclass
->interface_count
; i
++) {
2632 interfaces
[j
] = eclass
->interfaces
[i
];
2636 if (valuetype_types
[1]) {
2637 interfaces
[j
] = array_class_get_if_rank (valuetype_types
[1], original_rank
);
2642 /* instantiate the generic interfaces */
2643 for (i
= 0; i
< interface_count
; i
+= 3) {
2644 MonoClass
*iface
= interfaces
[i
];
2646 interfaces
[i
+ 0] = inflate_class_one_arg (mono_defaults
.generic_ilist_class
, iface
);
2647 interfaces
[i
+ 1] = inflate_class_one_arg (generic_icollection_class
, iface
);
2648 interfaces
[i
+ 2] = inflate_class_one_arg (generic_ienumerable_class
, iface
);
2650 if (internal_enumerator
) {
2652 /* instantiate IEnumerator<iface> */
2653 for (i
= 0; i
< interface_count
; i
++) {
2654 interfaces
[i
] = inflate_class_one_arg (generic_ienumerator_class
, interfaces
[i
]);
2656 j
= interface_count
;
2657 if (!eclass_is_valuetype
) {
2658 if (MONO_CLASS_IS_INTERFACE (eclass
)) {
2659 interfaces
[j
] = inflate_class_one_arg (generic_ienumerator_class
, mono_defaults
.object_class
);
2662 for (i
= 0; i
< eclass
->idepth
; i
++) {
2663 interfaces
[j
] = inflate_class_one_arg (generic_ienumerator_class
, eclass
->supertypes
[i
]);
2667 for (i
= 0; i
< eclass
->interface_offsets_count
; i
++) {
2668 interfaces
[j
] = inflate_class_one_arg (generic_ienumerator_class
, eclass
->interfaces_packed
[i
]);
2672 interfaces
[j
++] = inflate_class_one_arg (generic_ienumerator_class
, array_class_get_if_rank (valuetype_types
[0], original_rank
));
2674 if (valuetype_types
[1])
2675 interfaces
[j
] = inflate_class_one_arg (generic_ienumerator_class
, array_class_get_if_rank (valuetype_types
[1], original_rank
));
2679 char *type_name
= mono_type_get_name_full (&class->byval_arg
, 0);
2680 for (i
= 0; i
< real_count
; ++i
) {
2681 char *name
= mono_type_get_name_full (&interfaces
[i
]->byval_arg
, 0);
2682 g_print ("%s implements %s\n", type_name
, name
);
2693 find_array_interface (MonoClass
*klass
, const char *name
)
2696 for (i
= 0; i
< klass
->interface_count
; ++i
) {
2697 if (strcmp (klass
->interfaces
[i
]->name
, name
) == 0)
2704 * Return the number of virtual methods.
2705 * Even for interfaces we can't simply return the number of methods as all CLR types are allowed to have static methods.
2706 * Return -1 on failure.
2707 * FIXME It would be nice if this information could be cached somewhere.
2710 count_virtual_methods (MonoClass
*class)
2714 class = mono_class_get_generic_type_definition (class); /*We can find this information by looking at the GTD*/
2716 if (class->methods
|| !MONO_CLASS_HAS_STATIC_METADATA (class)) {
2717 mono_class_setup_methods (class);
2718 if (class->exception_type
)
2721 for (i
= 0; i
< class->method
.count
; ++i
) {
2722 flags
= class->methods
[i
]->flags
;
2723 if (flags
& METHOD_ATTRIBUTE_VIRTUAL
)
2727 for (i
= 0; i
< class->method
.count
; ++i
) {
2728 flags
= mono_metadata_decode_table_row_col (class->image
, MONO_TABLE_METHOD
, class->method
.first
+ i
, MONO_METHOD_FLAGS
);
2730 if (flags
& METHOD_ATTRIBUTE_VIRTUAL
)
2738 * LOCKING: this is supposed to be called with the loader lock held.
2739 * Return -1 on failure and set exception_type
2742 setup_interface_offsets (MonoClass
*class, int cur_slot
)
2746 MonoClass
**interfaces_full
;
2747 int *interface_offsets_full
;
2749 int interface_offsets_count
;
2750 MonoClass
**array_interfaces
;
2751 int num_array_interfaces
;
2752 int is_enumerator
= FALSE
;
2755 * get the implicit generic interfaces for either the arrays or for System.Array/InternalEnumerator<T>
2756 * implicit interfaces have the property that they are assigned the same slot in the
2757 * vtables for compatible interfaces
2759 array_interfaces
= get_implicit_generic_array_interfaces (class, &num_array_interfaces
, &is_enumerator
);
2761 /* compute maximum number of slots and maximum interface id */
2763 for (k
= class; k
; k
= k
->parent
) {
2764 for (i
= 0; i
< k
->interface_count
; i
++) {
2765 ic
= k
->interfaces
[i
];
2768 mono_class_init (ic
);
2770 if (max_iid
< ic
->interface_id
)
2771 max_iid
= ic
->interface_id
;
2773 ifaces
= mono_class_get_implemented_interfaces (k
);
2775 for (i
= 0; i
< ifaces
->len
; ++i
) {
2776 ic
= g_ptr_array_index (ifaces
, i
);
2777 if (max_iid
< ic
->interface_id
)
2778 max_iid
= ic
->interface_id
;
2780 g_ptr_array_free (ifaces
, TRUE
);
2783 for (i
= 0; i
< num_array_interfaces
; ++i
) {
2784 ic
= array_interfaces
[i
];
2785 mono_class_init (ic
);
2786 if (max_iid
< ic
->interface_id
)
2787 max_iid
= ic
->interface_id
;
2790 if (MONO_CLASS_IS_INTERFACE (class)) {
2791 if (max_iid
< class->interface_id
)
2792 max_iid
= class->interface_id
;
2794 class->max_interface_id
= max_iid
;
2795 /* compute vtable offset for interfaces */
2796 interfaces_full
= g_malloc (sizeof (MonoClass
*) * (max_iid
+ 1));
2797 interface_offsets_full
= g_malloc (sizeof (int) * (max_iid
+ 1));
2799 for (i
= 0; i
<= max_iid
; i
++) {
2800 interfaces_full
[i
] = NULL
;
2801 interface_offsets_full
[i
] = -1;
2804 for (k
= class->parent
; k
; k
= k
->parent
) {
2805 ifaces
= mono_class_get_implemented_interfaces (k
);
2807 for (i
= 0; i
< ifaces
->len
; ++i
) {
2809 ic
= g_ptr_array_index (ifaces
, i
);
2811 /*Force the sharing of interface offsets between parent and subtypes.*/
2812 io
= mono_class_interface_offset (k
, ic
);
2814 interfaces_full
[ic
->interface_id
] = ic
;
2815 interface_offsets_full
[ic
->interface_id
] = io
;
2817 g_ptr_array_free (ifaces
, TRUE
);
2822 ifaces
= mono_class_get_implemented_interfaces (class);
2824 for (i
= 0; i
< ifaces
->len
; ++i
) {
2826 ic
= g_ptr_array_index (ifaces
, i
);
2827 if (interfaces_full
[ic
->interface_id
] != NULL
)
2829 interfaces_full
[ic
->interface_id
] = ic
;
2830 interface_offsets_full
[ic
->interface_id
] = cur_slot
;
2831 count
= count_virtual_methods (ic
);
2833 char *name
= mono_type_get_full_name (ic
);
2834 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup_printf ("Error calculating interface offset of %s", name
));
2840 g_ptr_array_free (ifaces
, TRUE
);
2843 if (MONO_CLASS_IS_INTERFACE (class)) {
2844 interfaces_full
[class->interface_id
] = class;
2845 interface_offsets_full
[class->interface_id
] = cur_slot
;
2848 if (num_array_interfaces
) {
2849 if (is_enumerator
) {
2850 int ienumerator_offset
;
2851 int ienumerator_idx
= find_array_interface (class, "IEnumerator`1");
2852 ienumerator_offset
= interface_offsets_full
[class->interfaces
[ienumerator_idx
]->interface_id
];
2853 for (i
= 0; i
< num_array_interfaces
; ++i
) {
2854 ic
= array_interfaces
[i
];
2855 interfaces_full
[ic
->interface_id
] = ic
;
2856 if (strcmp (ic
->name
, "IEnumerator`1") == 0)
2857 interface_offsets_full
[ic
->interface_id
] = ienumerator_offset
;
2859 g_assert_not_reached ();
2860 /*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);*/
2863 int ilist_offset
, icollection_offset
, ienumerable_offset
;
2864 int ilist_iface_idx
= find_array_interface (class, "IList`1");
2865 int icollection_iface_idx
= find_array_interface (class->interfaces
[ilist_iface_idx
], "ICollection`1");
2866 int ienumerable_iface_idx
= find_array_interface (class->interfaces
[ilist_iface_idx
], "IEnumerable`1");
2867 ilist_offset
= interface_offsets_full
[class->interfaces
[ilist_iface_idx
]->interface_id
];
2868 icollection_offset
= interface_offsets_full
[class->interfaces
[ilist_iface_idx
]->interfaces
[icollection_iface_idx
]->interface_id
];
2869 ienumerable_offset
= interface_offsets_full
[class->interfaces
[ilist_iface_idx
]->interfaces
[ienumerable_iface_idx
]->interface_id
];
2870 g_assert (ilist_offset
>= 0 && icollection_offset
>= 0 && ienumerable_offset
>= 0);
2871 for (i
= 0; i
< num_array_interfaces
; ++i
) {
2872 ic
= array_interfaces
[i
];
2873 interfaces_full
[ic
->interface_id
] = ic
;
2874 if (ic
->generic_class
->container_class
== mono_defaults
.generic_ilist_class
)
2875 interface_offsets_full
[ic
->interface_id
] = ilist_offset
;
2876 else if (strcmp (ic
->name
, "ICollection`1") == 0)
2877 interface_offsets_full
[ic
->interface_id
] = icollection_offset
;
2878 else if (strcmp (ic
->name
, "IEnumerable`1") == 0)
2879 interface_offsets_full
[ic
->interface_id
] = ienumerable_offset
;
2881 g_assert_not_reached ();
2882 /*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);*/
2887 for (interface_offsets_count
= 0, i
= 0; i
<= max_iid
; i
++) {
2888 if (interface_offsets_full
[i
] != -1) {
2889 interface_offsets_count
++;
2894 * We might get called twice: once from mono_class_init () then once from
2895 * mono_class_setup_vtable ().
2897 if (class->interfaces_packed
) {
2898 g_assert (class->interface_offsets_count
== interface_offsets_count
);
2900 class->interface_offsets_count
= interface_offsets_count
;
2901 class->interfaces_packed
= mono_image_alloc (class->image
, sizeof (MonoClass
*) * interface_offsets_count
);
2902 class->interface_offsets_packed
= mono_image_alloc (class->image
, sizeof (guint16
) * interface_offsets_count
);
2903 class->interface_bitmap
= mono_image_alloc0 (class->image
, (sizeof (guint8
) * ((max_iid
+ 1) >> 3)) + (((max_iid
+ 1) & 7)? 1 :0));
2904 for (interface_offsets_count
= 0, i
= 0; i
<= max_iid
; i
++) {
2905 if (interface_offsets_full
[i
] != -1) {
2906 class->interface_bitmap
[i
>> 3] |= (1 << (i
& 7));
2907 class->interfaces_packed
[interface_offsets_count
] = interfaces_full
[i
];
2908 class->interface_offsets_packed
[interface_offsets_count
] = interface_offsets_full
[i
];
2909 /*if (num_array_interfaces)
2910 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]);*/
2911 interface_offsets_count
++;
2916 g_free (interfaces_full
);
2917 g_free (interface_offsets_full
);
2918 g_free (array_interfaces
);
2920 //printf ("JUST DONE: ");
2921 //print_implemented_interfaces (class);
2925 g_free (interfaces_full
);
2926 g_free (interface_offsets_full
);
2927 g_free (array_interfaces
);
2932 * Setup interface offsets for interfaces.
2934 * - class->max_interface_id
2935 * - class->interface_offsets_count
2936 * - class->interfaces_packed
2937 * - class->interface_offsets_packed
2938 * - class->interface_bitmap
2940 * This function can fail @class.
2943 mono_class_setup_interface_offsets (MonoClass
*class)
2945 mono_loader_lock ();
2947 setup_interface_offsets (class, 0);
2949 mono_loader_unlock ();
2953 * mono_class_setup_vtable:
2955 * Creates the generic vtable of CLASS.
2956 * Initializes the following fields in MonoClass:
2959 * Plus all the fields initialized by setup_interface_offsets ().
2960 * If there is an error during vtable construction, class->exception_type is set.
2962 * LOCKING: Acquires the loader lock.
2965 mono_class_setup_vtable (MonoClass
*class)
2967 MonoMethod
**overrides
;
2968 MonoGenericContext
*context
;
2977 if (mono_debug_using_mono_debugger ())
2978 /* The debugger currently depends on this */
2979 mono_class_setup_methods (class);
2981 if (MONO_CLASS_IS_INTERFACE (class)) {
2982 /* This sets method->slot for all methods if this is an interface */
2983 mono_class_setup_methods (class);
2987 if (class->exception_type
)
2990 mono_loader_lock ();
2992 if (class->vtable
) {
2993 mono_loader_unlock ();
2997 mono_stats
.generic_vtable_count
++;
2999 if (class->generic_class
) {
3000 context
= mono_class_get_context (class);
3001 type_token
= class->generic_class
->container_class
->type_token
;
3003 context
= (MonoGenericContext
*) class->generic_container
;
3004 type_token
= class->type_token
;
3007 if (class->image
->dynamic
) {
3008 if (class->generic_class
) {
3009 MonoClass
*gklass
= class->generic_class
->container_class
;
3011 mono_reflection_get_dynamic_overrides (gklass
, &overrides
, &onum
);
3012 for (i
= 0; i
< onum
; ++i
) {
3013 MonoMethod
*override
= overrides
[(i
* 2) + 1];
3014 MonoMethod
*inflated
= NULL
;
3017 for (j
= 0; j
< class->method
.count
; ++j
) {
3018 if (gklass
->methods
[j
] == override
) {
3019 inflated
= class->methods
[j
];
3023 g_assert (inflated
);
3025 overrides
[(i
* 2) + 1] = inflated
;
3028 mono_reflection_get_dynamic_overrides (class, &overrides
, &onum
);
3031 /* The following call fails if there are missing methods in the type */
3032 ok
= mono_class_get_overrides_full (class->image
, type_token
, &overrides
, &onum
, context
);
3036 mono_class_setup_vtable_general (class, overrides
, onum
);
3040 mono_loader_unlock ();
3045 #define DEBUG_INTERFACE_VTABLE_CODE 0
3046 #define TRACE_INTERFACE_VTABLE_CODE 0
3047 #define VERIFY_INTERFACE_VTABLE_CODE 0
3048 #define VTABLE_SELECTOR (1)
3050 #if (TRACE_INTERFACE_VTABLE_CODE|DEBUG_INTERFACE_VTABLE_CODE)
3051 #define DEBUG_INTERFACE_VTABLE(stmt) do {\
3052 if (!(VTABLE_SELECTOR)) break; \
3056 #define DEBUG_INTERFACE_VTABLE(stmt)
3059 #if TRACE_INTERFACE_VTABLE_CODE
3060 #define TRACE_INTERFACE_VTABLE(stmt) do {\
3061 if (!(VTABLE_SELECTOR)) break; \
3065 #define TRACE_INTERFACE_VTABLE(stmt)
3068 #if VERIFY_INTERFACE_VTABLE_CODE
3069 #define VERIFY_INTERFACE_VTABLE(stmt) do {\
3070 if (!(VTABLE_SELECTOR)) break; \
3074 #define VERIFY_INTERFACE_VTABLE(stmt)
3078 #if (TRACE_INTERFACE_VTABLE_CODE|DEBUG_INTERFACE_VTABLE_CODE)
3080 mono_signature_get_full_desc (MonoMethodSignature
*sig
, gboolean include_namespace
)
3084 GString
*res
= g_string_new ("");
3086 g_string_append_c (res
, '(');
3087 for (i
= 0; i
< sig
->param_count
; ++i
) {
3089 g_string_append_c (res
, ',');
3090 mono_type_get_desc (res
, sig
->params
[i
], include_namespace
);
3092 g_string_append (res
, ")=>");
3093 if (sig
->ret
!= NULL
) {
3094 mono_type_get_desc (res
, sig
->ret
, include_namespace
);
3096 g_string_append (res
, "NULL");
3099 g_string_free (res
, FALSE
);
3103 print_method_signatures (MonoMethod
*im
, MonoMethod
*cm
) {
3104 char *im_sig
= mono_signature_get_full_desc (mono_method_signature (im
), TRUE
);
3105 char *cm_sig
= mono_signature_get_full_desc (mono_method_signature (cm
), TRUE
);
3106 printf ("(IM \"%s\", CM \"%s\")", im_sig
, cm_sig
);
3114 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
) {
3115 MonoMethodSignature
*cmsig
, *imsig
;
3116 if (strcmp (im
->name
, cm
->name
) == 0) {
3117 if (! (cm
->flags
& METHOD_ATTRIBUTE_PUBLIC
)) {
3118 TRACE_INTERFACE_VTABLE (printf ("[PUBLIC CHECK FAILED]"));
3121 if (! slot_is_empty
) {
3122 if (require_newslot
) {
3123 if (! interface_is_explicitly_implemented_by_class
) {
3124 TRACE_INTERFACE_VTABLE (printf ("[NOT EXPLICIT IMPLEMENTATION IN FULL SLOT REFUSED]"));
3127 if (! (cm
->flags
& METHOD_ATTRIBUTE_NEW_SLOT
)) {
3128 TRACE_INTERFACE_VTABLE (printf ("[NEWSLOT CHECK FAILED]"));
3132 TRACE_INTERFACE_VTABLE (printf ("[FULL SLOT REFUSED]"));
3135 cmsig
= mono_method_signature (cm
);
3136 imsig
= mono_method_signature (im
);
3137 if (!cmsig
|| !imsig
) {
3138 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Could not resolve the signature of a virtual method"));
3142 if (! mono_metadata_signature_equal (cmsig
, imsig
)) {
3143 TRACE_INTERFACE_VTABLE (printf ("[SIGNATURE CHECK FAILED "));
3144 TRACE_INTERFACE_VTABLE (print_method_signatures (im
, cm
));
3145 TRACE_INTERFACE_VTABLE (printf ("]"));
3148 TRACE_INTERFACE_VTABLE (printf ("[SECURITY CHECKS]"));
3149 /* CAS - SecurityAction.InheritanceDemand on interface */
3150 if (security_enabled
&& (im
->flags
& METHOD_ATTRIBUTE_HAS_SECURITY
)) {
3151 mono_secman_inheritancedemand_method (cm
, im
);
3154 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR
)
3155 mono_security_core_clr_check_override (class, cm
, im
);
3156 TRACE_INTERFACE_VTABLE (printf ("[NAME CHECK OK]"));
3159 MonoClass
*ic
= im
->klass
;
3160 const char *ic_name_space
= ic
->name_space
;
3161 const char *ic_name
= ic
->name
;
3164 if (! require_newslot
) {
3165 TRACE_INTERFACE_VTABLE (printf ("[INJECTED METHOD REFUSED]"));
3168 if (cm
->klass
->rank
== 0) {
3169 TRACE_INTERFACE_VTABLE (printf ("[RANK CHECK FAILED]"));
3172 if (! mono_metadata_signature_equal (mono_method_signature (cm
), mono_method_signature (im
))) {
3173 TRACE_INTERFACE_VTABLE (printf ("[(INJECTED) SIGNATURE CHECK FAILED "));
3174 TRACE_INTERFACE_VTABLE (print_method_signatures (im
, cm
));
3175 TRACE_INTERFACE_VTABLE (printf ("]"));
3178 if (mono_class_get_image (ic
) != mono_defaults
.corlib
) {
3179 TRACE_INTERFACE_VTABLE (printf ("[INTERFACE CORLIB CHECK FAILED]"));
3182 if ((ic_name_space
== NULL
) || (strcmp (ic_name_space
, "System.Collections.Generic") != 0)) {
3183 TRACE_INTERFACE_VTABLE (printf ("[INTERFACE NAMESPACE CHECK FAILED]"));
3186 if ((ic_name
== NULL
) || ((strcmp (ic_name
, "IEnumerable`1") != 0) && (strcmp (ic_name
, "ICollection`1") != 0) && (strcmp (ic_name
, "IList`1") != 0))) {
3187 TRACE_INTERFACE_VTABLE (printf ("[INTERFACE NAME CHECK FAILED]"));
3191 subname
= strstr (cm
->name
, ic_name_space
);
3192 if (subname
!= cm
->name
) {
3193 TRACE_INTERFACE_VTABLE (printf ("[ACTUAL NAMESPACE CHECK FAILED]"));
3196 subname
+= strlen (ic_name_space
);
3197 if (subname
[0] != '.') {
3198 TRACE_INTERFACE_VTABLE (printf ("[FIRST DOT CHECK FAILED]"));
3202 if (strstr (subname
, ic_name
) != subname
) {
3203 TRACE_INTERFACE_VTABLE (printf ("[ACTUAL CLASS NAME CHECK FAILED]"));
3206 subname
+= strlen (ic_name
);
3207 if (subname
[0] != '.') {
3208 TRACE_INTERFACE_VTABLE (printf ("[SECOND DOT CHECK FAILED]"));
3212 if (strcmp (subname
, im
->name
) != 0) {
3213 TRACE_INTERFACE_VTABLE (printf ("[METHOD NAME CHECK FAILED]"));
3217 TRACE_INTERFACE_VTABLE (printf ("[SECURITY CHECKS (INJECTED CASE)]"));
3218 /* CAS - SecurityAction.InheritanceDemand on interface */
3219 if (security_enabled
&& (im
->flags
& METHOD_ATTRIBUTE_HAS_SECURITY
)) {
3220 mono_secman_inheritancedemand_method (cm
, im
);
3223 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR
)
3224 mono_security_core_clr_check_override (class, cm
, im
);
3226 TRACE_INTERFACE_VTABLE (printf ("[INJECTED INTERFACE CHECK OK]"));
3231 #if (TRACE_INTERFACE_VTABLE_CODE|DEBUG_INTERFACE_VTABLE_CODE)
3233 foreach_override (gpointer key
, gpointer value
, gpointer user_data
) {
3234 MonoMethod
*method
= key
;
3235 MonoMethod
*override
= value
;
3236 MonoClass
*method_class
= mono_method_get_class (method
);
3237 MonoClass
*override_class
= mono_method_get_class (override
);
3239 printf (" Method '%s.%s:%s' has override '%s.%s:%s'\n",
3240 mono_class_get_namespace (method_class
), mono_class_get_name (method_class
), mono_method_get_name (method
),
3241 mono_class_get_namespace (override_class
), mono_class_get_name (override_class
), mono_method_get_name (override
));
3244 print_overrides (GHashTable
*override_map
, const char *message
) {
3246 printf ("Override map \"%s\" START:\n", message
);
3247 g_hash_table_foreach (override_map
, foreach_override
, NULL
);
3248 printf ("Override map \"%s\" END.\n", message
);
3250 printf ("Override map \"%s\" EMPTY.\n", message
);
3254 print_vtable_full (MonoClass
*class, MonoMethod
** vtable
, int size
, int first_non_interface_slot
, const char *message
, gboolean print_interfaces
) {
3255 char *full_name
= mono_type_full_name (&class->byval_arg
);
3259 printf ("*** Vtable for class '%s' at \"%s\" (size %d)\n", full_name
, message
, size
);
3261 if (print_interfaces
) {
3262 print_implemented_interfaces (class);
3263 printf ("* Interfaces for class '%s' done.\nStarting vtable (size %d):\n", full_name
, size
);
3266 if (class->parent
) {
3267 parent_size
= class->parent
->vtable_size
;
3271 for (i
= 0; i
< size
; ++i
) {
3272 MonoMethod
*cm
= vtable
[i
];
3274 char *cm_name
= mono_method_full_name (cm
, TRUE
);
3275 char newness
= (i
< parent_size
) ? 'O' : ((i
< first_non_interface_slot
) ? 'I' : 'N');
3276 printf (" [%c][%03d][INDEX %03d] %s\n", newness
, i
, cm
->slot
, cm_name
);
3285 #if VERIFY_INTERFACE_VTABLE_CODE
3287 mono_method_try_get_vtable_index (MonoMethod
*method
)
3289 if (method
->is_inflated
&& (method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
)) {
3290 MonoMethodInflated
*imethod
= (MonoMethodInflated
*)method
;
3291 if (imethod
->declaring
->is_generic
)
3292 return imethod
->declaring
->slot
;
3294 return method
->slot
;
3298 mono_class_verify_vtable (MonoClass
*class)
3301 char *full_name
= mono_type_full_name (&class->byval_arg
);
3303 printf ("*** Verifying VTable of class '%s' \n", full_name
);
3307 if (!class->methods
)
3310 for (i
= 0; i
< class->method
.count
; ++i
) {
3311 MonoMethod
*cm
= class->methods
[i
];
3314 if (!(cm
->flags
& METHOD_ATTRIBUTE_VIRTUAL
))
3318 full_name
= mono_method_full_name (cm
, TRUE
);
3320 slot
= mono_method_try_get_vtable_index (cm
);
3322 if (slot
>= class->vtable_size
) {
3323 printf ("\tInvalid method %s at index %d with vtable of length %d\n", full_name
, slot
, class->vtable_size
);
3327 if (slot
>= 0 && class->vtable
[slot
] != cm
&& (class->vtable
[slot
])) {
3328 char *other_name
= class->vtable
[slot
] ? mono_method_full_name (class->vtable
[slot
], TRUE
) : g_strdup ("[null value]");
3329 printf ("\tMethod %s has slot %d but vtable has %s on it\n", full_name
, slot
, other_name
);
3330 g_free (other_name
);
3333 printf ("\tVirtual method %s does n't have an assigned slot\n", full_name
);
3340 print_unimplemented_interface_method_info (MonoClass
*class, MonoClass
*ic
, MonoMethod
*im
, int im_slot
, MonoMethod
**overrides
, int onum
) {
3342 char *method_signature
;
3345 for (index
= 0; index
< onum
; ++index
) {
3346 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", im_slot
, overrides
[index
*2+1]->name
,
3347 overrides
[index
*2+1]->slot
, overrides
[index
*2]->name
, overrides
[index
*2]->slot
);
3349 method_signature
= mono_signature_get_desc (mono_method_signature (im
), FALSE
);
3350 type_name
= mono_type_full_name (&class->byval_arg
);
3351 printf ("no implementation for interface method %s::%s(%s) in class %s\n",
3352 mono_type_get_name (&ic
->byval_arg
), im
->name
, method_signature
, type_name
);
3353 g_free (method_signature
);
3355 mono_class_setup_methods (class);
3356 if (class->exception_type
) {
3357 char *name
= mono_type_get_full_name (class);
3358 printf ("CLASS %s failed to resolve methods\n", name
);
3362 for (index
= 0; index
< class->method
.count
; ++index
) {
3363 MonoMethod
*cm
= class->methods
[index
];
3364 method_signature
= mono_signature_get_desc (mono_method_signature (cm
), TRUE
);
3366 printf ("METHOD %s(%s)\n", cm
->name
, method_signature
);
3367 g_free (method_signature
);
3372 verify_class_overrides (MonoClass
*class, MonoMethod
**overrides
, int onum
)
3376 for (i
= 0; i
< onum
; ++i
) {
3377 MonoMethod
*decl
= overrides
[i
* 2];
3378 MonoMethod
*body
= overrides
[i
* 2 + 1];
3380 if (mono_class_get_generic_type_definition (body
->klass
) != mono_class_get_generic_type_definition (class)) {
3381 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Method belongs to a different class than the declared one"));
3385 if (!(body
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) || (body
->flags
& METHOD_ATTRIBUTE_STATIC
)) {
3386 if (body
->flags
& METHOD_ATTRIBUTE_STATIC
)
3387 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Method must not be static to override a base type"));
3389 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Method must be virtual to override a base type"));
3393 if (!(decl
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) || (decl
->flags
& METHOD_ATTRIBUTE_STATIC
)) {
3394 if (body
->flags
& METHOD_ATTRIBUTE_STATIC
)
3395 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Cannot override a static method in a base type"));
3397 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Cannot override a non virtual method in a base type"));
3401 if (!mono_class_is_assignable_from_slow (decl
->klass
, class)) {
3402 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup ("Method overrides a class or interface that extended or implemented by this type"));
3409 * LOCKING: this is supposed to be called with the loader lock held.
3412 mono_class_setup_vtable_general (MonoClass
*class, MonoMethod
**overrides
, int onum
)
3415 MonoMethod
**vtable
;
3416 int i
, max_vtsize
= 0, max_iid
, cur_slot
= 0;
3417 GPtrArray
*ifaces
= NULL
;
3418 GHashTable
*override_map
= NULL
;
3419 gboolean security_enabled
= mono_is_security_manager_active ();
3421 gpointer class_iter
;
3422 #if (DEBUG_INTERFACE_VTABLE_CODE|TRACE_INTERFACE_VTABLE_CODE)
3423 int first_non_interface_slot
;
3425 GSList
*virt_methods
= NULL
, *l
;
3430 if (overrides
&& !verify_class_overrides (class, overrides
, onum
))
3433 ifaces
= mono_class_get_implemented_interfaces (class);
3436 for (i
= 0; i
< ifaces
->len
; i
++) {
3437 MonoClass
*ic
= g_ptr_array_index (ifaces
, i
);
3438 max_vtsize
+= ic
->method
.count
;
3440 g_ptr_array_free (ifaces
, TRUE
);
3444 if (class->parent
) {
3445 mono_class_init (class->parent
);
3446 mono_class_setup_vtable (class->parent
);
3448 if (class->parent
->exception_type
) {
3449 char *name
= mono_type_get_full_name (class->parent
);
3450 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup_printf ("Parent %s failed to load", name
));
3455 max_vtsize
+= class->parent
->vtable_size
;
3456 cur_slot
= class->parent
->vtable_size
;
3459 max_vtsize
+= class->method
.count
;
3461 vtable
= alloca (sizeof (gpointer
) * max_vtsize
);
3462 memset (vtable
, 0, sizeof (gpointer
) * max_vtsize
);
3464 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
3466 cur_slot
= setup_interface_offsets (class, cur_slot
);
3467 if (cur_slot
== -1) /*setup_interface_offsets fails the type.*/
3470 max_iid
= class->max_interface_id
;
3471 DEBUG_INTERFACE_VTABLE (first_non_interface_slot
= cur_slot
);
3473 /* Optimized version for generic instances */
3474 if (class->generic_class
) {
3475 MonoClass
*gklass
= class->generic_class
->container_class
;
3478 mono_class_setup_vtable (gklass
);
3479 if (gklass
->exception_type
!= MONO_EXCEPTION_NONE
) {
3480 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
3484 tmp
= mono_image_alloc0 (class->image
, sizeof (gpointer
) * gklass
->vtable_size
);
3485 class->vtable_size
= gklass
->vtable_size
;
3486 for (i
= 0; i
< gklass
->vtable_size
; ++i
)
3487 if (gklass
->vtable
[i
]) {
3488 tmp
[i
] = mono_class_inflate_generic_method_full (gklass
->vtable
[i
], class, mono_class_get_context (class));
3489 tmp
[i
]->slot
= gklass
->vtable
[i
]->slot
;
3491 mono_memory_barrier ();
3492 class->vtable
= tmp
;
3494 /* Have to set method->slot for abstract virtual methods */
3495 if (class->methods
&& gklass
->methods
) {
3496 for (i
= 0; i
< class->method
.count
; ++i
)
3497 if (class->methods
[i
]->slot
== -1)
3498 class->methods
[i
]->slot
= gklass
->methods
[i
]->slot
;
3504 if (class->parent
&& class->parent
->vtable_size
) {
3505 MonoClass
*parent
= class->parent
;
3508 memcpy (vtable
, parent
->vtable
, sizeof (gpointer
) * parent
->vtable_size
);
3510 // Also inherit parent interface vtables, just as a starting point.
3511 // This is needed otherwise bug-77127.exe fails when the property methods
3512 // have different names in the iterface and the class, because for child
3513 // classes the ".override" information is not used anymore.
3514 for (i
= 0; i
< parent
->interface_offsets_count
; i
++) {
3515 MonoClass
*parent_interface
= parent
->interfaces_packed
[i
];
3516 int interface_offset
= mono_class_interface_offset (class, parent_interface
);
3517 /*FIXME this is now dead code as this condition will never hold true.
3518 Since interface offsets are inherited then the offset of an interface implemented
3519 by a parent will never be the out of it's vtable boundary.
3521 if (interface_offset
>= parent
->vtable_size
) {
3522 int parent_interface_offset
= mono_class_interface_offset (parent
, parent_interface
);
3525 mono_class_setup_methods (parent_interface
); /*FIXME Just kill this whole chunk of dead code*/
3526 TRACE_INTERFACE_VTABLE (printf (" +++ Inheriting interface %s.%s\n", parent_interface
->name_space
, parent_interface
->name
));
3527 for (j
= 0; j
< parent_interface
->method
.count
&& !class->exception_type
; j
++) {
3528 vtable
[interface_offset
+ j
] = parent
->vtable
[parent_interface_offset
+ j
];
3529 TRACE_INTERFACE_VTABLE (printf (" --- Inheriting: [%03d][(%03d)+(%03d)] => [%03d][(%03d)+(%03d)]\n",
3530 parent_interface_offset
+ j
, parent_interface_offset
, j
,
3531 interface_offset
+ j
, interface_offset
, j
));
3538 TRACE_INTERFACE_VTABLE (print_vtable_full (class, vtable
, cur_slot
, first_non_interface_slot
, "AFTER INHERITING PARENT VTABLE", TRUE
));
3539 /* override interface methods */
3540 for (i
= 0; i
< onum
; i
++) {
3541 MonoMethod
*decl
= overrides
[i
*2];
3542 if (MONO_CLASS_IS_INTERFACE (decl
->klass
)) {
3544 dslot
= mono_method_get_vtable_slot (decl
);
3546 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
3550 dslot
+= mono_class_interface_offset (class, decl
->klass
);
3551 vtable
[dslot
] = overrides
[i
*2 + 1];
3552 vtable
[dslot
]->slot
= dslot
;
3554 override_map
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
3556 g_hash_table_insert (override_map
, overrides
[i
* 2], overrides
[i
* 2 + 1]);
3558 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR
)
3559 mono_security_core_clr_check_override (class, vtable
[dslot
], decl
);
3562 TRACE_INTERFACE_VTABLE (print_overrides (override_map
, "AFTER OVERRIDING INTERFACE METHODS"));
3563 TRACE_INTERFACE_VTABLE (print_vtable_full (class, vtable
, cur_slot
, first_non_interface_slot
, "AFTER OVERRIDING INTERFACE METHODS", FALSE
));
3566 * Create a list of virtual methods to avoid calling
3567 * mono_class_get_virtual_methods () which is slow because of the metadata
3571 gpointer iter
= NULL
;
3574 virt_methods
= NULL
;
3575 while ((cm
= mono_class_get_virtual_methods (class, &iter
))) {
3576 virt_methods
= g_slist_prepend (virt_methods
, cm
);
3578 if (class->exception_type
)
3582 // Loop on all implemented interfaces...
3583 for (i
= 0; i
< class->interface_offsets_count
; i
++) {
3584 MonoClass
*parent
= class->parent
;
3586 gboolean interface_is_explicitly_implemented_by_class
;
3589 ic
= class->interfaces_packed
[i
];
3590 ic_offset
= mono_class_interface_offset (class, ic
);
3592 mono_class_setup_methods (ic
);
3593 if (ic
->exception_type
)
3596 // Check if this interface is explicitly implemented (instead of just inherited)
3597 if (parent
!= NULL
) {
3598 int implemented_interfaces_index
;
3599 interface_is_explicitly_implemented_by_class
= FALSE
;
3600 for (implemented_interfaces_index
= 0; implemented_interfaces_index
< class->interface_count
; implemented_interfaces_index
++) {
3601 if (ic
== class->interfaces
[implemented_interfaces_index
]) {
3602 interface_is_explicitly_implemented_by_class
= TRUE
;
3607 interface_is_explicitly_implemented_by_class
= TRUE
;
3610 // Loop on all interface methods...
3611 for (im_index
= 0; im_index
< ic
->method
.count
; im_index
++) {
3612 MonoMethod
*im
= ic
->methods
[im_index
];
3613 int im_slot
= ic_offset
+ im
->slot
;
3614 MonoMethod
*override_im
= (override_map
!= NULL
) ? g_hash_table_lookup (override_map
, im
) : NULL
;
3616 if (im
->flags
& METHOD_ATTRIBUTE_STATIC
)
3619 // If there is an explicit implementation, just use it right away,
3620 // otherwise look for a matching method
3621 if (override_im
== NULL
) {
3626 // First look for a suitable method among the class methods
3628 for (l
= virt_methods
; l
; l
= l
->next
) {
3630 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
)));
3631 if (check_interface_method_override (class, im
, cm
, TRUE
, interface_is_explicitly_implemented_by_class
, (vtable
[im_slot
] == NULL
), security_enabled
)) {
3632 TRACE_INTERFACE_VTABLE (printf ("[check ok]: ASSIGNING"));
3633 vtable
[im_slot
] = cm
;
3634 /* Why do we need this? */
3639 TRACE_INTERFACE_VTABLE (printf ("\n"));
3640 if (class->exception_type
) /*Might be set by check_interface_method_override*/
3644 // If the slot is still empty, look in all the inherited virtual methods...
3645 if ((vtable
[im_slot
] == NULL
) && class->parent
!= NULL
) {
3646 MonoClass
*parent
= class->parent
;
3647 // Reverse order, so that last added methods are preferred
3648 for (cm_index
= parent
->vtable_size
- 1; cm_index
>= 0; cm_index
--) {
3649 MonoMethod
*cm
= parent
->vtable
[cm_index
];
3651 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
));
3652 if ((cm
!= NULL
) && check_interface_method_override (class, im
, cm
, FALSE
, FALSE
, TRUE
, security_enabled
)) {
3653 TRACE_INTERFACE_VTABLE (printf ("[everything ok]: ASSIGNING"));
3654 vtable
[im_slot
] = cm
;
3655 /* Why do we need this? */
3661 if (class->exception_type
) /*Might be set by check_interface_method_override*/
3663 TRACE_INTERFACE_VTABLE ((cm
!= NULL
) && printf ("\n"));
3667 g_assert (vtable
[im_slot
] == override_im
);
3672 // If the class is not abstract, check that all its interface slots are full.
3673 // The check is done here and not directly at the end of the loop above because
3674 // it can happen (for injected generic array interfaces) that the same slot is
3675 // processed multiple times (those interfaces have overlapping slots), and it
3676 // will not always be the first pass the one that fills the slot.
3677 if (! (class->flags
& TYPE_ATTRIBUTE_ABSTRACT
)) {
3678 for (i
= 0; i
< class->interface_offsets_count
; i
++) {
3682 ic
= class->interfaces_packed
[i
];
3683 ic_offset
= mono_class_interface_offset (class, ic
);
3685 for (im_index
= 0; im_index
< ic
->method
.count
; im_index
++) {
3686 MonoMethod
*im
= ic
->methods
[im_index
];
3687 int im_slot
= ic_offset
+ im
->slot
;
3689 if (im
->flags
& METHOD_ATTRIBUTE_STATIC
)
3692 TRACE_INTERFACE_VTABLE (printf (" [class is not abstract, checking slot %d for interface '%s'.'%s', method %s, slot check is %d]\n",
3693 im_slot
, ic
->name_space
, ic
->name
, im
->name
, (vtable
[im_slot
] == NULL
)));
3694 if (vtable
[im_slot
] == NULL
) {
3695 print_unimplemented_interface_method_info (class, ic
, im
, im_slot
, overrides
, onum
);
3702 TRACE_INTERFACE_VTABLE (print_vtable_full (class, vtable
, cur_slot
, first_non_interface_slot
, "AFTER SETTING UP INTERFACE METHODS", FALSE
));
3704 for (l
= virt_methods
; l
; l
= l
->next
) {
3707 * If the method is REUSE_SLOT, we must check in the
3708 * base class for a method to override.
3710 if (!(cm
->flags
& METHOD_ATTRIBUTE_NEW_SLOT
)) {
3712 for (k
= class->parent
; k
; k
= k
->parent
) {
3717 while ((m1
= mono_class_get_virtual_methods (k
, &k_iter
))) {
3718 MonoMethodSignature
*cmsig
, *m1sig
;
3720 cmsig
= mono_method_signature (cm
);
3721 m1sig
= mono_method_signature (m1
);
3723 if (!cmsig
|| !m1sig
) {
3724 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
3728 if (!strcmp(cm
->name
, m1
->name
) &&
3729 mono_metadata_signature_equal (cmsig
, m1sig
)) {
3731 /* CAS - SecurityAction.InheritanceDemand */
3732 if (security_enabled
&& (m1
->flags
& METHOD_ATTRIBUTE_HAS_SECURITY
)) {
3733 mono_secman_inheritancedemand_method (cm
, m1
);
3736 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR
)
3737 mono_security_core_clr_check_override (class, cm
, m1
);
3739 slot
= mono_method_get_vtable_slot (m1
);
3743 g_assert (cm
->slot
< max_vtsize
);
3745 override_map
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
3746 g_hash_table_insert (override_map
, m1
, cm
);
3750 if (k
->exception_type
)
3760 /*Non final newslot methods must be given a non-interface vtable slot*/
3761 if ((cm
->flags
& METHOD_ATTRIBUTE_NEW_SLOT
) && !(cm
->flags
& METHOD_ATTRIBUTE_FINAL
) && cm
->slot
>= 0)
3765 cm
->slot
= cur_slot
++;
3767 if (!(cm
->flags
& METHOD_ATTRIBUTE_ABSTRACT
))
3768 vtable
[cm
->slot
] = cm
;
3771 /* override non interface methods */
3772 for (i
= 0; i
< onum
; i
++) {
3773 MonoMethod
*decl
= overrides
[i
*2];
3774 if (!MONO_CLASS_IS_INTERFACE (decl
->klass
)) {
3775 g_assert (decl
->slot
!= -1);
3776 vtable
[decl
->slot
] = overrides
[i
*2 + 1];
3777 overrides
[i
* 2 + 1]->slot
= decl
->slot
;
3779 override_map
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
3780 g_hash_table_insert (override_map
, decl
, overrides
[i
* 2 + 1]);
3782 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR
)
3783 mono_security_core_clr_check_override (class, vtable
[decl
->slot
], decl
);
3788 * If a method occupies more than one place in the vtable, and it is
3789 * overriden, then change the other occurances too.
3792 for (i
= 0; i
< max_vtsize
; ++i
)
3794 MonoMethod
*cm
= g_hash_table_lookup (override_map
, vtable
[i
]);
3799 g_hash_table_destroy (override_map
);
3800 override_map
= NULL
;
3803 g_slist_free (virt_methods
);
3804 virt_methods
= NULL
;
3806 /* Ensure that all vtable slots are filled with concrete instance methods */
3807 if (!(class->flags
& TYPE_ATTRIBUTE_ABSTRACT
)) {
3808 for (i
= 0; i
< cur_slot
; ++i
) {
3809 if (vtable
[i
] == NULL
|| (vtable
[i
]->flags
& (METHOD_ATTRIBUTE_ABSTRACT
| METHOD_ATTRIBUTE_STATIC
))) {
3810 char *type_name
= mono_type_get_full_name (class);
3811 char *method_name
= vtable
[i
] ? mono_method_full_name (vtable
[i
], TRUE
) : g_strdup ("none");
3812 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
));
3814 g_free (method_name
);
3820 if (class->generic_class
) {
3821 MonoClass
*gklass
= class->generic_class
->container_class
;
3823 mono_class_init (gklass
);
3825 class->vtable_size
= MAX (gklass
->vtable_size
, cur_slot
);
3827 /* Check that the vtable_size value computed in mono_class_init () is correct */
3828 if (class->vtable_size
)
3829 g_assert (cur_slot
== class->vtable_size
);
3830 class->vtable_size
= cur_slot
;
3833 /* Try to share the vtable with our parent. */
3834 if (class->parent
&& (class->parent
->vtable_size
== class->vtable_size
) && (memcmp (class->parent
->vtable
, vtable
, sizeof (gpointer
) * class->vtable_size
) == 0)) {
3835 mono_memory_barrier ();
3836 class->vtable
= class->parent
->vtable
;
3838 MonoMethod
**tmp
= mono_image_alloc0 (class->image
, sizeof (gpointer
) * class->vtable_size
);
3839 memcpy (tmp
, vtable
, sizeof (gpointer
) * class->vtable_size
);
3840 mono_memory_barrier ();
3841 class->vtable
= tmp
;
3844 DEBUG_INTERFACE_VTABLE (print_vtable_full (class, class->vtable
, class->vtable_size
, first_non_interface_slot
, "FINALLY", FALSE
));
3845 if (mono_print_vtable
) {
3848 print_implemented_interfaces (class);
3850 for (i
= 0; i
<= max_iid
; i
++)
3851 if (MONO_CLASS_IMPLEMENTS_INTERFACE (class, i
))
3854 printf ("VTable %s (vtable entries = %d, interfaces = %d)\n", mono_type_full_name (&class->byval_arg
),
3855 class->vtable_size
, icount
);
3857 for (i
= 0; i
< cur_slot
; ++i
) {
3862 printf (" slot assigned: %03d, slot index: %03d %s\n", i
, cm
->slot
,
3863 mono_method_full_name (cm
, TRUE
));
3869 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space
,
3870 class->name
, max_iid
);
3872 for (i
= 0; i
< class->interface_count
; i
++) {
3873 ic
= class->interfaces
[i
];
3874 printf (" slot offset: %03d, method count: %03d, iid: %03d %s\n",
3875 mono_class_interface_offset (class, ic
),
3876 count_virtual_methods (ic
), ic
->interface_id
, mono_type_full_name (&ic
->byval_arg
));
3879 for (k
= class->parent
; k
; k
= k
->parent
) {
3880 for (i
= 0; i
< k
->interface_count
; i
++) {
3881 ic
= k
->interfaces
[i
];
3882 printf (" parent slot offset: %03d, method count: %03d, iid: %03d %s\n",
3883 mono_class_interface_offset (class, ic
),
3884 count_virtual_methods (ic
), ic
->interface_id
, mono_type_full_name (&ic
->byval_arg
));
3890 VERIFY_INTERFACE_VTABLE (mono_class_verify_vtable (class));
3895 char *name
= mono_type_get_full_name (class);
3896 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup_printf ("VTable setup of type %s failed", name
));
3899 g_hash_table_destroy (override_map
);
3901 g_slist_free (virt_methods
);
3906 * mono_method_get_vtable_slot:
3908 * Returns method->slot, computing it if neccesary. Return -1 on failure.
3909 * LOCKING: Acquires the loader lock.
3911 * FIXME Use proper MonoError machinery here.
3914 mono_method_get_vtable_slot (MonoMethod
*method
)
3916 if (method
->slot
== -1) {
3917 mono_class_setup_vtable (method
->klass
);
3918 if (method
->klass
->exception_type
)
3920 g_assert (method
->slot
!= -1);
3922 return method
->slot
;
3926 * mono_method_get_vtable_index:
3929 * Returns the index into the runtime vtable to access the method or,
3930 * in the case of a virtual generic method, the virtual generic method
3931 * thunk. Returns -1 on failure.
3933 * FIXME Use proper MonoError machinery here.
3936 mono_method_get_vtable_index (MonoMethod
*method
)
3938 if (method
->is_inflated
&& (method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
)) {
3939 MonoMethodInflated
*imethod
= (MonoMethodInflated
*)method
;
3940 if (imethod
->declaring
->is_generic
)
3941 return mono_method_get_vtable_slot (imethod
->declaring
);
3943 return mono_method_get_vtable_slot (method
);
3946 static MonoMethod
*default_ghc
= NULL
;
3947 static MonoMethod
*default_finalize
= NULL
;
3948 static int finalize_slot
= -1;
3949 static int ghc_slot
= -1;
3952 initialize_object_slots (MonoClass
*class)
3957 if (class == mono_defaults
.object_class
) {
3958 mono_class_setup_vtable (class);
3959 for (i
= 0; i
< class->vtable_size
; ++i
) {
3960 MonoMethod
*cm
= class->vtable
[i
];
3962 if (!strcmp (cm
->name
, "GetHashCode"))
3964 else if (!strcmp (cm
->name
, "Finalize"))
3968 g_assert (ghc_slot
> 0);
3969 default_ghc
= class->vtable
[ghc_slot
];
3971 g_assert (finalize_slot
> 0);
3972 default_finalize
= class->vtable
[finalize_slot
];
3977 MonoMethod
*array_method
;
3979 } GenericArrayMethodInfo
;
3981 static int generic_array_method_num
= 0;
3982 static GenericArrayMethodInfo
*generic_array_method_info
= NULL
;
3985 generic_array_methods (MonoClass
*class)
3987 int i
, count_generic
= 0;
3988 GList
*list
= NULL
, *tmp
;
3989 if (generic_array_method_num
)
3990 return generic_array_method_num
;
3991 mono_class_setup_methods (class->parent
); /*This is setting up System.Array*/
3992 g_assert (!class->parent
->exception_type
); /*So hitting this assert is a huge problem*/
3993 for (i
= 0; i
< class->parent
->method
.count
; i
++) {
3994 MonoMethod
*m
= class->parent
->methods
[i
];
3995 if (!strncmp (m
->name
, "InternalArray__", 15)) {
3997 list
= g_list_prepend (list
, m
);
4000 list
= g_list_reverse (list
);
4001 generic_array_method_info
= g_malloc (sizeof (GenericArrayMethodInfo
) * count_generic
);
4003 for (tmp
= list
; tmp
; tmp
= tmp
->next
) {
4004 const char *mname
, *iname
;
4006 MonoMethod
*m
= tmp
->data
;
4007 generic_array_method_info
[i
].array_method
= m
;
4008 if (!strncmp (m
->name
, "InternalArray__ICollection_", 27)) {
4009 iname
= "System.Collections.Generic.ICollection`1.";
4010 mname
= m
->name
+ 27;
4011 } else if (!strncmp (m
->name
, "InternalArray__IEnumerable_", 27)) {
4012 iname
= "System.Collections.Generic.IEnumerable`1.";
4013 mname
= m
->name
+ 27;
4014 } else if (!strncmp (m
->name
, "InternalArray__", 15)) {
4015 iname
= "System.Collections.Generic.IList`1.";
4016 mname
= m
->name
+ 15;
4018 g_assert_not_reached ();
4021 name
= mono_image_alloc (mono_defaults
.corlib
, strlen (iname
) + strlen (mname
) + 1);
4022 strcpy (name
, iname
);
4023 strcpy (name
+ strlen (iname
), mname
);
4024 generic_array_method_info
[i
].name
= name
;
4027 /*g_print ("array generic methods: %d\n", count_generic);*/
4029 generic_array_method_num
= count_generic
;
4031 return generic_array_method_num
;
4035 setup_generic_array_ifaces (MonoClass
*class, MonoClass
*iface
, MonoMethod
**methods
, int pos
)
4037 MonoGenericContext tmp_context
;
4040 tmp_context
.class_inst
= NULL
;
4041 tmp_context
.method_inst
= iface
->generic_class
->context
.class_inst
;
4042 //g_print ("setting up array interface: %s\n", mono_type_get_name_full (&iface->byval_arg, 0));
4044 for (i
= 0; i
< generic_array_method_num
; i
++) {
4045 MonoMethod
*m
= generic_array_method_info
[i
].array_method
;
4046 MonoMethod
*inflated
;
4048 inflated
= mono_class_inflate_generic_method (m
, &tmp_context
);
4049 methods
[pos
++] = mono_marshal_get_generic_array_helper (class, iface
, generic_array_method_info
[i
].name
, inflated
);
4054 concat_two_strings_with_zero (MonoImage
*image
, const char *s1
, const char *s2
)
4056 int len
= strlen (s1
) + strlen (s2
) + 2;
4057 char *s
= mono_image_alloc (image
, len
);
4060 result
= g_snprintf (s
, len
, "%s%c%s", s1
, '\0', s2
);
4061 g_assert (result
== len
- 1);
4067 set_failure_from_loader_error (MonoClass
*class, MonoLoaderError
*error
)
4069 gpointer exception_data
= NULL
;
4071 switch (error
->exception_type
) {
4072 case MONO_EXCEPTION_TYPE_LOAD
:
4073 exception_data
= concat_two_strings_with_zero (class->image
, error
->class_name
, error
->assembly_name
);
4076 case MONO_EXCEPTION_MISSING_METHOD
:
4077 exception_data
= concat_two_strings_with_zero (class->image
, error
->class_name
, error
->member_name
);
4080 case MONO_EXCEPTION_MISSING_FIELD
: {
4081 const char *name_space
= error
->klass
->name_space
? error
->klass
->name_space
: NULL
;
4082 const char *class_name
;
4085 class_name
= g_strdup_printf ("%s.%s", name_space
, error
->klass
->name
);
4087 class_name
= error
->klass
->name
;
4089 exception_data
= concat_two_strings_with_zero (class->image
, class_name
, error
->member_name
);
4092 g_free ((void*)class_name
);
4096 case MONO_EXCEPTION_FILE_NOT_FOUND
: {
4099 if (error
->ref_only
)
4100 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.";
4102 msg
= "Could not load file or assembly '%s' or one of its dependencies.";
4104 exception_data
= concat_two_strings_with_zero (class->image
, msg
, error
->assembly_name
);
4108 case MONO_EXCEPTION_BAD_IMAGE
:
4109 exception_data
= error
->msg
;
4113 g_assert_not_reached ();
4116 mono_class_set_failure (class, error
->exception_type
, exception_data
);
4121 * @class: the class to initialize
4123 * Compute the instance_size, class_size and other infos that cannot be
4124 * computed at mono_class_get() time. Also compute vtable_size if possible.
4125 * Returns TRUE on success or FALSE if there was a problem in loading
4126 * the type (incorrect assemblies, missing assemblies, methods, etc).
4128 * LOCKING: Acquires the loader lock.
4131 mono_class_init (MonoClass
*class)
4134 MonoCachedClassInfo cached_info
;
4135 gboolean has_cached_info
;
4139 /* Double-checking locking pattern */
4141 return class->exception_type
== MONO_EXCEPTION_NONE
;
4143 /*g_print ("Init class %s\n", class->name);*/
4145 /* We do everything inside the lock to prevent races */
4146 mono_loader_lock ();
4148 if (class->inited
) {
4149 mono_loader_unlock ();
4150 /* Somebody might have gotten in before us */
4151 return class->exception_type
== MONO_EXCEPTION_NONE
;
4154 if (class->init_pending
) {
4155 mono_loader_unlock ();
4156 /* this indicates a cyclic dependency */
4157 g_error ("pending init %s.%s\n", class->name_space
, class->name
);
4160 class->init_pending
= 1;
4162 if (mono_verifier_is_enabled_for_class (class) && !mono_verifier_verify_class (class)) {
4163 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, concat_two_strings_with_zero (class->image
, class->name
, class->image
->assembly_name
));
4168 if (class->byval_arg
.type
== MONO_TYPE_ARRAY
|| class->byval_arg
.type
== MONO_TYPE_SZARRAY
) {
4169 MonoClass
*element_class
= class->element_class
;
4170 if (!element_class
->inited
)
4171 mono_class_init (element_class
);
4172 if (element_class
->exception_type
!= MONO_EXCEPTION_NONE
) {
4173 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
4178 /* CAS - SecurityAction.InheritanceDemand */
4179 if (mono_is_security_manager_active () && class->parent
&& (class->parent
->flags
& TYPE_ATTRIBUTE_HAS_SECURITY
)) {
4180 mono_secman_inheritancedemand_class (class, class->parent
);
4183 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR
)
4184 mono_security_core_clr_check_inheritance (class);
4186 mono_stats
.initialized_class_count
++;
4188 if (class->generic_class
&& !class->generic_class
->is_dynamic
) {
4189 MonoClass
*gklass
= class->generic_class
->container_class
;
4191 mono_stats
.generic_class_count
++;
4193 class->method
= gklass
->method
;
4194 class->field
= gklass
->field
;
4196 mono_class_init (gklass
);
4197 // FIXME: Why is this needed ?
4198 if (!gklass
->exception_type
)
4199 mono_class_setup_methods (gklass
);
4200 if (gklass
->exception_type
) {
4201 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, g_strdup_printf ("Generic Type Defintion failed to init"));
4205 if (MONO_CLASS_IS_INTERFACE (class))
4206 class->interface_id
= mono_get_unique_iid (class);
4209 if (class->parent
&& !class->parent
->inited
)
4210 mono_class_init (class->parent
);
4212 has_cached_info
= mono_class_get_cached_class_info (class, &cached_info
);
4214 if (class->generic_class
|| class->image
->dynamic
|| !class->type_token
|| (has_cached_info
&& !cached_info
.has_nested_classes
))
4215 class->nested_classes_inited
= TRUE
;
4218 * Computes the size used by the fields, and their locations
4220 if (has_cached_info
) {
4221 class->instance_size
= cached_info
.instance_size
;
4222 class->sizes
.class_size
= cached_info
.class_size
;
4223 class->packing_size
= cached_info
.packing_size
;
4224 class->min_align
= cached_info
.min_align
;
4225 class->blittable
= cached_info
.blittable
;
4226 class->has_references
= cached_info
.has_references
;
4227 class->has_static_refs
= cached_info
.has_static_refs
;
4228 class->no_special_static_fields
= cached_info
.no_special_static_fields
;
4231 if (!class->size_inited
){
4232 mono_class_setup_fields (class);
4233 if (class->exception_type
|| mono_loader_get_last_error ())
4237 /* Initialize arrays */
4239 class->method
.count
= 3 + (class->rank
> 1? 2: 1);
4241 if (class->interface_count
) {
4242 int count_generic
= generic_array_methods (class);
4243 class->method
.count
+= class->interface_count
* count_generic
;
4247 mono_class_setup_supertypes (class);
4250 initialize_object_slots (class);
4253 * Initialize the rest of the data without creating a generic vtable if possible.
4254 * If possible, also compute vtable_size, so mono_class_create_runtime_vtable () can
4255 * also avoid computing a generic vtable.
4257 if (has_cached_info
) {
4259 class->vtable_size
= cached_info
.vtable_size
;
4260 class->has_finalize
= cached_info
.has_finalize
;
4261 class->ghcimpl
= cached_info
.ghcimpl
;
4262 class->has_cctor
= cached_info
.has_cctor
;
4263 } else if (class->rank
== 1 && class->byval_arg
.type
== MONO_TYPE_SZARRAY
) {
4264 static int szarray_vtable_size
= 0;
4267 if (!szarray_vtable_size
) {
4268 mono_class_setup_vtable (class);
4269 szarray_vtable_size
= class->vtable_size
;
4271 class->vtable_size
= szarray_vtable_size
;
4273 } else if (class->generic_class
&& !MONO_CLASS_IS_INTERFACE (class)) {
4274 MonoClass
*gklass
= class->generic_class
->container_class
;
4276 /* Generic instance case */
4277 class->ghcimpl
= gklass
->ghcimpl
;
4278 class->has_finalize
= gklass
->has_finalize
;
4279 class->has_cctor
= gklass
->has_cctor
;
4281 mono_class_setup_vtable (gklass
);
4282 if (gklass
->exception_type
) {
4283 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
4287 class->vtable_size
= gklass
->vtable_size
;
4291 /* ghcimpl is not currently used
4293 if (class->parent) {
4294 MonoMethod *cmethod = class->vtable [ghc_slot];
4295 if (cmethod->is_inflated)
4296 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
4297 if (cmethod == default_ghc) {
4303 /* Interfaces and valuetypes are not supposed to have finalizers */
4304 if (!(MONO_CLASS_IS_INTERFACE (class) || class->valuetype
)) {
4305 MonoMethod
*cmethod
= NULL
;
4307 if (class->parent
&& class->parent
->has_finalize
) {
4308 class->has_finalize
= 1;
4310 if (class->type_token
) {
4311 cmethod
= find_method_in_metadata (class, "Finalize", 0, METHOD_ATTRIBUTE_VIRTUAL
);
4312 } else if (class->parent
) {
4313 /* FIXME: Optimize this */
4314 mono_class_setup_vtable (class);
4315 if (class->exception_type
|| mono_loader_get_last_error ())
4317 cmethod
= class->vtable
[finalize_slot
];
4321 /* Check that this is really the finalizer method */
4322 mono_class_setup_vtable (class);
4323 if (class->exception_type
|| mono_loader_get_last_error ())
4326 g_assert (class->vtable_size
> finalize_slot
);
4328 class->has_finalize
= 0;
4329 if (class->parent
) {
4330 cmethod
= class->vtable
[finalize_slot
];
4332 if (cmethod
->is_inflated
)
4333 cmethod
= ((MonoMethodInflated
*)cmethod
)->declaring
;
4334 if (cmethod
!= default_finalize
) {
4335 class->has_finalize
= 1;
4342 /* C# doesn't allow interfaces to have cctors */
4343 if (!MONO_CLASS_IS_INTERFACE (class) || class->image
!= mono_defaults
.corlib
) {
4344 MonoMethod
*cmethod
= NULL
;
4346 if (class->type_token
) {
4347 cmethod
= find_method_in_metadata (class, ".cctor", 0, METHOD_ATTRIBUTE_SPECIAL_NAME
);
4348 /* The find_method function ignores the 'flags' argument */
4349 if (cmethod
&& (cmethod
->flags
& METHOD_ATTRIBUTE_SPECIAL_NAME
))
4350 class->has_cctor
= 1;
4352 mono_class_setup_methods (class);
4353 if (class->exception_type
)
4356 for (i
= 0; i
< class->method
.count
; ++i
) {
4357 MonoMethod
*method
= class->methods
[i
];
4358 if ((method
->flags
& METHOD_ATTRIBUTE_SPECIAL_NAME
) &&
4359 (strcmp (".cctor", method
->name
) == 0)) {
4360 class->has_cctor
= 1;
4368 if (!mono_setup_vtable_in_class_init
) {
4370 * This is an embedding API break, since the caller might assume that
4371 * mono_class_init () constructs a generic vtable, so vtable construction errors
4372 * are visible right after the mono_class_init (), and not after
4373 * mono_class_vtable ().
4375 if (class->parent
) {
4376 /* This will compute class->parent->vtable_size for some classes */
4377 mono_class_init (class->parent
);
4378 if (class->parent
->exception_type
) {
4379 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
4382 if (mono_loader_get_last_error ())
4384 if (!class->parent
->vtable_size
) {
4385 /* FIXME: Get rid of this somehow */
4386 mono_class_setup_vtable (class->parent
);
4387 if (class->parent
->exception_type
) {
4388 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
4391 if (mono_loader_get_last_error ())
4394 setup_interface_offsets (class, class->parent
->vtable_size
);
4396 setup_interface_offsets (class, 0);
4399 mono_class_setup_vtable (class);
4401 if (MONO_CLASS_IS_INTERFACE (class))
4402 setup_interface_offsets (class, 0);
4408 /* Because of the double-checking locking pattern */
4409 mono_memory_barrier ();
4411 class->init_pending
= 0;
4413 if (mono_loader_get_last_error ()) {
4414 if (class->exception_type
== MONO_EXCEPTION_NONE
) {
4415 set_failure_from_loader_error (class, mono_loader_get_last_error ());
4417 mono_loader_clear_error ();
4420 mono_loader_unlock ();
4422 if (mono_debugger_class_init_func
)
4423 mono_debugger_class_init_func (class);
4425 return class->exception_type
== MONO_EXCEPTION_NONE
;
4429 is_corlib_image (MonoImage
*image
)
4431 /* FIXME: allow the dynamic case for our compilers and with full trust */
4433 return image
->assembly
&& !strcmp (image
->assembly
->aname
.name
, "mscorlib");
4435 return image
== mono_defaults
.corlib
;
4439 * LOCKING: this assumes the loader lock is held
4442 mono_class_setup_mono_type (MonoClass
*class)
4444 const char *name
= class->name
;
4445 const char *nspace
= class->name_space
;
4446 gboolean is_corlib
= is_corlib_image (class->image
);
4448 class->this_arg
.byref
= 1;
4449 class->this_arg
.data
.klass
= class;
4450 class->this_arg
.type
= MONO_TYPE_CLASS
;
4451 class->byval_arg
.data
.klass
= class;
4452 class->byval_arg
.type
= MONO_TYPE_CLASS
;
4454 if (is_corlib
&& !strcmp (nspace
, "System")) {
4455 if (!strcmp (name
, "ValueType")) {
4457 * do not set the valuetype bit for System.ValueType.
4458 * class->valuetype = 1;
4460 class->blittable
= TRUE
;
4461 } else if (!strcmp (name
, "Enum")) {
4463 * do not set the valuetype bit for System.Enum.
4464 * class->valuetype = 1;
4466 class->valuetype
= 0;
4467 class->enumtype
= 0;
4468 } else if (!strcmp (name
, "Object")) {
4469 class->this_arg
.type
= class->byval_arg
.type
= MONO_TYPE_OBJECT
;
4470 } else if (!strcmp (name
, "String")) {
4471 class->this_arg
.type
= class->byval_arg
.type
= MONO_TYPE_STRING
;
4472 } else if (!strcmp (name
, "TypedReference")) {
4473 class->this_arg
.type
= class->byval_arg
.type
= MONO_TYPE_TYPEDBYREF
;
4477 if (class->valuetype
) {
4478 int t
= MONO_TYPE_VALUETYPE
;
4480 if (is_corlib
&& !strcmp (nspace
, "System")) {
4483 if (!strcmp (name
, "Boolean")) {
4484 t
= MONO_TYPE_BOOLEAN
;
4485 } else if (!strcmp(name
, "Byte")) {
4487 class->blittable
= TRUE
;
4491 if (!strcmp (name
, "Char")) {
4496 if (!strcmp (name
, "Double")) {
4498 class->blittable
= TRUE
;
4502 if (!strcmp (name
, "Int32")) {
4504 class->blittable
= TRUE
;
4505 } else if (!strcmp(name
, "Int16")) {
4507 class->blittable
= TRUE
;
4508 } else if (!strcmp(name
, "Int64")) {
4510 class->blittable
= TRUE
;
4511 } else if (!strcmp(name
, "IntPtr")) {
4513 class->blittable
= TRUE
;
4517 if (!strcmp (name
, "Single")) {
4519 class->blittable
= TRUE
;
4520 } else if (!strcmp(name
, "SByte")) {
4522 class->blittable
= TRUE
;
4526 if (!strcmp (name
, "UInt32")) {
4528 class->blittable
= TRUE
;
4529 } else if (!strcmp(name
, "UInt16")) {
4531 class->blittable
= TRUE
;
4532 } else if (!strcmp(name
, "UInt64")) {
4534 class->blittable
= TRUE
;
4535 } else if (!strcmp(name
, "UIntPtr")) {
4537 class->blittable
= TRUE
;
4541 if (!strcmp (name
, "TypedReference")) {
4542 t
= MONO_TYPE_TYPEDBYREF
;
4543 class->blittable
= TRUE
;
4547 if (!strcmp (name
, "Void")) {
4555 class->this_arg
.type
= class->byval_arg
.type
= t
;
4558 if (MONO_CLASS_IS_INTERFACE (class))
4559 class->interface_id
= mono_get_unique_iid (class);
4564 * COM initialization (using mono_init_com_types) is delayed until needed.
4565 * However when a [ComImport] attribute is present on a type it will trigger
4566 * the initialization. This is not a problem unless the BCL being executed
4567 * lacks the types that COM depends on (e.g. Variant on Silverlight).
4570 init_com_from_comimport (MonoClass
*class)
4572 /* we don't always allow COM initialization under the CoreCLR (e.g. Moonlight does not require it) */
4573 if ((mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR
)) {
4574 /* but some other CoreCLR user could requires it for their platform (i.e. trusted) code */
4575 if (!mono_security_core_clr_determine_platform_image (class->image
)) {
4576 /* but it can not be made available for application (i.e. user code) since all COM calls
4577 * are considered native calls. In this case we fail with a TypeLoadException (just like
4578 * Silverlight 2 does */
4579 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
4583 /* FIXME : we should add an extra checks to ensure COM can be initialized properly before continuing */
4584 mono_init_com_types ();
4588 * LOCKING: this assumes the loader lock is held
4591 mono_class_setup_parent (MonoClass
*class, MonoClass
*parent
)
4593 gboolean system_namespace
;
4594 gboolean is_corlib
= is_corlib_image (class->image
);
4596 system_namespace
= !strcmp (class->name_space
, "System") && is_corlib
;
4598 /* if root of the hierarchy */
4599 if (system_namespace
&& !strcmp (class->name
, "Object")) {
4600 class->parent
= NULL
;
4601 class->instance_size
= sizeof (MonoObject
);
4604 if (!strcmp (class->name
, "<Module>")) {
4605 class->parent
= NULL
;
4606 class->instance_size
= 0;
4610 if (!MONO_CLASS_IS_INTERFACE (class)) {
4611 /* Imported COM Objects always derive from __ComObject. */
4612 if (MONO_CLASS_IS_IMPORT (class)) {
4613 init_com_from_comimport (class);
4614 if (parent
== mono_defaults
.object_class
)
4615 parent
= mono_defaults
.com_object_class
;
4618 /* set the parent to something useful and safe, but mark the type as broken */
4619 parent
= mono_defaults
.object_class
;
4620 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
4623 class->parent
= parent
;
4625 if (parent
->generic_class
&& !parent
->name
) {
4627 * If the parent is a generic instance, we may get
4628 * called before it is fully initialized, especially
4629 * before it has its name.
4634 class->marshalbyref
= parent
->marshalbyref
;
4635 class->contextbound
= parent
->contextbound
;
4636 class->delegate
= parent
->delegate
;
4637 if (MONO_CLASS_IS_IMPORT (class))
4638 class->is_com_object
= 1;
4640 class->is_com_object
= parent
->is_com_object
;
4642 if (system_namespace
) {
4643 if (*class->name
== 'M' && !strcmp (class->name
, "MarshalByRefObject"))
4644 class->marshalbyref
= 1;
4646 if (*class->name
== 'C' && !strcmp (class->name
, "ContextBoundObject"))
4647 class->contextbound
= 1;
4649 if (*class->name
== 'D' && !strcmp (class->name
, "Delegate"))
4650 class->delegate
= 1;
4653 if (class->parent
->enumtype
|| (is_corlib_image (class->parent
->image
) && (strcmp (class->parent
->name
, "ValueType") == 0) &&
4654 (strcmp (class->parent
->name_space
, "System") == 0)))
4655 class->valuetype
= 1;
4656 if (is_corlib_image (class->parent
->image
) && ((strcmp (class->parent
->name
, "Enum") == 0) && (strcmp (class->parent
->name_space
, "System") == 0))) {
4657 class->valuetype
= class->enumtype
= 1;
4659 /*class->enumtype = class->parent->enumtype; */
4660 mono_class_setup_supertypes (class);
4662 /* initialize com types if COM interfaces are present */
4663 if (MONO_CLASS_IS_IMPORT (class))
4664 init_com_from_comimport (class);
4665 class->parent
= NULL
;
4671 * mono_class_setup_supertypes:
4674 * Build the data structure needed to make fast type checks work.
4675 * This currently sets two fields in @class:
4676 * - idepth: distance between @class and System.Object in the type
4678 * - supertypes: array of classes: each element has a class in the hierarchy
4679 * starting from @class up to System.Object
4681 * LOCKING: this assumes the loader lock is held
4684 mono_class_setup_supertypes (MonoClass
*class)
4688 if (class->supertypes
)
4691 if (class->parent
&& !class->parent
->supertypes
)
4692 mono_class_setup_supertypes (class->parent
);
4694 class->idepth
= class->parent
->idepth
+ 1;
4698 ms
= MAX (MONO_DEFAULT_SUPERTABLE_SIZE
, class->idepth
);
4699 class->supertypes
= mono_image_alloc0 (class->image
, sizeof (MonoClass
*) * ms
);
4701 if (class->parent
) {
4702 class->supertypes
[class->idepth
- 1] = class;
4703 memcpy (class->supertypes
, class->parent
->supertypes
, class->parent
->idepth
* sizeof (gpointer
));
4705 class->supertypes
[0] = class;
4710 * mono_class_create_from_typedef:
4711 * @image: image where the token is valid
4712 * @type_token: typedef token
4714 * Create the MonoClass* representing the specified type token.
4715 * @type_token must be a TypeDef token.
4718 mono_class_create_from_typedef (MonoImage
*image
, guint32 type_token
)
4720 MonoTableInfo
*tt
= &image
->tables
[MONO_TABLE_TYPEDEF
];
4721 MonoClass
*class, *parent
= NULL
;
4722 guint32 cols
[MONO_TYPEDEF_SIZE
];
4723 guint32 cols_next
[MONO_TYPEDEF_SIZE
];
4724 guint tidx
= mono_metadata_token_index (type_token
);
4725 MonoGenericContext
*context
= NULL
;
4726 const char *name
, *nspace
;
4728 MonoClass
**interfaces
;
4729 guint32 field_last
, method_last
;
4730 guint32 nesting_tokeen
;
4732 if (mono_metadata_token_table (type_token
) != MONO_TABLE_TYPEDEF
|| tidx
> tt
->rows
)
4735 mono_loader_lock ();
4737 if ((class = mono_internal_hash_table_lookup (&image
->class_cache
, GUINT_TO_POINTER (type_token
)))) {
4738 mono_loader_unlock ();
4742 mono_metadata_decode_row (tt
, tidx
- 1, cols
, MONO_TYPEDEF_SIZE
);
4744 name
= mono_metadata_string_heap (image
, cols
[MONO_TYPEDEF_NAME
]);
4745 nspace
= mono_metadata_string_heap (image
, cols
[MONO_TYPEDEF_NAMESPACE
]);
4747 class = mono_image_alloc0 (image
, sizeof (MonoClass
));
4750 class->name_space
= nspace
;
4752 mono_profiler_class_event (class, MONO_PROFILE_START_LOAD
);
4754 class->image
= image
;
4755 class->type_token
= type_token
;
4756 class->flags
= cols
[MONO_TYPEDEF_FLAGS
];
4758 mono_internal_hash_table_insert (&image
->class_cache
, GUINT_TO_POINTER (type_token
), class);
4760 classes_size
+= sizeof (MonoClass
);
4763 * Check whether we're a generic type definition.
4765 class->generic_container
= mono_metadata_load_generic_params (image
, class->type_token
, NULL
);
4766 if (class->generic_container
) {
4767 class->is_generic
= 1;
4768 class->generic_container
->owner
.klass
= class;
4769 context
= &class->generic_container
->context
;
4772 if (cols
[MONO_TYPEDEF_EXTENDS
]) {
4773 guint32 parent_token
= mono_metadata_token_from_dor (cols
[MONO_TYPEDEF_EXTENDS
]);
4775 if (mono_metadata_token_table (parent_token
) == MONO_TABLE_TYPESPEC
) {
4776 /*WARNING: this must satisfy mono_metadata_type_hash*/
4777 class->this_arg
.byref
= 1;
4778 class->this_arg
.data
.klass
= class;
4779 class->this_arg
.type
= MONO_TYPE_CLASS
;
4780 class->byval_arg
.data
.klass
= class;
4781 class->byval_arg
.type
= MONO_TYPE_CLASS
;
4783 parent
= mono_class_get_full (image
, parent_token
, context
);
4785 if (parent
== NULL
){
4786 mono_internal_hash_table_remove (&image
->class_cache
, GUINT_TO_POINTER (type_token
));
4787 mono_loader_unlock ();
4788 mono_profiler_class_loaded (class, MONO_PROFILE_FAILED
);
4793 /* do this early so it's available for interfaces in setup_mono_type () */
4794 if ((nesting_tokeen
= mono_metadata_nested_in_typedef (image
, type_token
))) {
4795 class->nested_in
= mono_class_create_from_typedef (image
, nesting_tokeen
);
4796 if (!class->nested_in
) {
4797 mono_internal_hash_table_remove (&image
->class_cache
, GUINT_TO_POINTER (type_token
));
4798 mono_loader_unlock ();
4799 mono_profiler_class_loaded (class, MONO_PROFILE_FAILED
);
4804 mono_class_setup_parent (class, parent
);
4806 /* uses ->valuetype, which is initialized by mono_class_setup_parent above */
4807 mono_class_setup_mono_type (class);
4809 if ((class->flags
& TYPE_ATTRIBUTE_STRING_FORMAT_MASK
) == TYPE_ATTRIBUTE_UNICODE_CLASS
)
4813 if ((class->flags
& TYPE_ATTRIBUTE_STRING_FORMAT_MASK
) == TYPE_ATTRIBUTE_AUTO_CLASS
)
4817 class->cast_class
= class->element_class
= class;
4819 if (!class->enumtype
) {
4820 if (!mono_metadata_interfaces_from_typedef_full (
4821 image
, type_token
, &interfaces
, &icount
, FALSE
, context
)){
4822 mono_loader_unlock ();
4823 mono_profiler_class_loaded (class, MONO_PROFILE_FAILED
);
4827 class->interfaces
= interfaces
;
4828 class->interface_count
= icount
;
4829 class->interfaces_inited
= 1;
4832 /*g_print ("Load class %s\n", name);*/
4835 * Compute the field and method lists
4837 class->field
.first
= cols
[MONO_TYPEDEF_FIELD_LIST
] - 1;
4838 class->method
.first
= cols
[MONO_TYPEDEF_METHOD_LIST
] - 1;
4840 if (tt
->rows
> tidx
){
4841 mono_metadata_decode_row (tt
, tidx
, cols_next
, MONO_TYPEDEF_SIZE
);
4842 field_last
= cols_next
[MONO_TYPEDEF_FIELD_LIST
] - 1;
4843 method_last
= cols_next
[MONO_TYPEDEF_METHOD_LIST
] - 1;
4845 field_last
= image
->tables
[MONO_TABLE_FIELD
].rows
;
4846 method_last
= image
->tables
[MONO_TABLE_METHOD
].rows
;
4849 if (cols
[MONO_TYPEDEF_FIELD_LIST
] &&
4850 cols
[MONO_TYPEDEF_FIELD_LIST
] <= image
->tables
[MONO_TABLE_FIELD
].rows
)
4851 class->field
.count
= field_last
- class->field
.first
;
4853 class->field
.count
= 0;
4855 if (cols
[MONO_TYPEDEF_METHOD_LIST
] <= image
->tables
[MONO_TABLE_METHOD
].rows
)
4856 class->method
.count
= method_last
- class->method
.first
;
4858 class->method
.count
= 0;
4860 /* reserve space to store vector pointer in arrays */
4861 if (is_corlib_image (image
) && !strcmp (nspace
, "System") && !strcmp (name
, "Array")) {
4862 class->instance_size
+= 2 * sizeof (gpointer
);
4863 g_assert (class->field
.count
== 0);
4866 if (class->enumtype
) {
4867 MonoType
*enum_basetype
= mono_class_find_enum_basetype (class);
4868 if (!enum_basetype
) {
4869 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
4870 mono_loader_unlock ();
4871 mono_profiler_class_loaded (class, MONO_PROFILE_FAILED
);
4874 class->cast_class
= class->element_class
= mono_class_from_mono_type (enum_basetype
);
4878 * If we're a generic type definition, load the constraints.
4879 * We must do this after the class has been constructed to make certain recursive scenarios
4882 if (class->generic_container
&& !mono_metadata_load_generic_param_constraints_full (image
, type_token
, class->generic_container
)){
4883 char *class_name
= g_strdup_printf("%s.%s", class->name_space
, class->name
);
4884 char *error
= concat_two_strings_with_zero (class->image
, class_name
, class->image
->assembly_name
);
4885 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, error
);
4886 g_free (class_name
);
4887 mono_loader_unlock ();
4888 mono_profiler_class_loaded (class, MONO_PROFILE_FAILED
);
4892 if (class->image
->assembly_name
&& !strcmp (class->image
->assembly_name
, "Mono.Simd") && !strcmp (nspace
, "Mono.Simd")) {
4893 if (!strncmp (name
, "Vector", 6))
4894 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");
4897 mono_loader_unlock ();
4899 mono_profiler_class_loaded (class, MONO_PROFILE_OK
);
4904 /** is klass Nullable<T>? */
4906 mono_class_is_nullable (MonoClass
*klass
)
4908 return klass
->generic_class
!= NULL
&&
4909 klass
->generic_class
->container_class
== mono_defaults
.generic_nullable_class
;
4913 /** if klass is T? return T */
4915 mono_class_get_nullable_param (MonoClass
*klass
)
4917 g_assert (mono_class_is_nullable (klass
));
4918 return mono_class_from_mono_type (klass
->generic_class
->context
.class_inst
->type_argv
[0]);
4922 * Create the `MonoClass' for an instantiation of a generic type.
4923 * We only do this if we actually need it.
4926 mono_generic_class_get_class (MonoGenericClass
*gclass
)
4928 MonoClass
*klass
, *gklass
;
4930 mono_loader_lock ();
4931 if (gclass
->cached_class
) {
4932 mono_loader_unlock ();
4933 return gclass
->cached_class
;
4936 gclass
->cached_class
= g_malloc0 (sizeof (MonoClass
));
4937 klass
= gclass
->cached_class
;
4939 gklass
= gclass
->container_class
;
4941 if (gklass
->nested_in
) {
4943 * FIXME: the nested type context should include everything the
4944 * nesting context should have, but it may also have additional
4945 * generic parameters...
4947 klass
->nested_in
= mono_class_inflate_generic_class (gklass
->nested_in
,
4948 mono_generic_class_get_context (gclass
));
4951 klass
->name
= gklass
->name
;
4952 klass
->name_space
= gklass
->name_space
;
4954 mono_profiler_class_event (klass
, MONO_PROFILE_START_LOAD
);
4956 klass
->image
= gklass
->image
;
4957 klass
->flags
= gklass
->flags
;
4958 klass
->type_token
= gklass
->type_token
;
4959 klass
->field
.count
= gklass
->field
.count
;
4961 klass
->is_inflated
= 1;
4962 klass
->generic_class
= gclass
;
4964 klass
->this_arg
.type
= klass
->byval_arg
.type
= MONO_TYPE_GENERICINST
;
4965 klass
->this_arg
.data
.generic_class
= klass
->byval_arg
.data
.generic_class
= gclass
;
4966 klass
->this_arg
.byref
= TRUE
;
4967 klass
->enumtype
= gklass
->enumtype
;
4968 klass
->valuetype
= gklass
->valuetype
;
4970 klass
->cast_class
= klass
->element_class
= klass
;
4972 if (mono_class_is_nullable (klass
))
4973 klass
->cast_class
= klass
->element_class
= mono_class_get_nullable_param (klass
);
4976 * We're not interested in the nested classes of a generic instance.
4977 * We use the generic type definition to look for nested classes.
4980 if (gklass
->parent
) {
4981 klass
->parent
= mono_class_inflate_generic_class (gklass
->parent
, mono_generic_class_get_context (gclass
));
4985 mono_class_setup_parent (klass
, klass
->parent
);
4987 if (klass
->enumtype
) {
4988 klass
->cast_class
= gklass
->cast_class
;
4989 klass
->element_class
= gklass
->element_class
;
4992 if (gclass
->is_dynamic
) {
4995 mono_class_setup_supertypes (klass
);
4997 if (klass
->enumtype
) {
4999 * For enums, gklass->fields might not been set, but instance_size etc. is
5000 * already set in mono_reflection_create_internal_class (). For non-enums,
5001 * these will be computed normally in mono_class_layout_fields ().
5003 klass
->instance_size
= gklass
->instance_size
;
5004 klass
->sizes
.class_size
= gklass
->sizes
.class_size
;
5005 klass
->size_inited
= 1;
5009 mono_profiler_class_loaded (klass
, MONO_PROFILE_OK
);
5011 inflated_classes
++;
5012 inflated_classes_size
+= sizeof (MonoClass
);
5014 mono_loader_unlock ();
5020 make_generic_param_class (MonoGenericParam
*param
, MonoImage
*image
, gboolean is_mvar
, MonoGenericParamInfo
*pinfo
)
5022 MonoClass
*klass
, **ptr
;
5024 MonoGenericContainer
*container
= mono_generic_param_owner (param
);
5028 image
= mono_defaults
.corlib
;
5030 klass
= mono_image_alloc0 (image
, sizeof (MonoClass
));
5031 classes_size
+= sizeof (MonoClass
);
5034 klass
->name
= pinfo
->name
;
5036 int n
= mono_generic_param_num (param
);
5037 klass
->name
= mono_image_alloc0 (image
, 16);
5038 sprintf ((char*)klass
->name
, "%d", n
);
5043 MonoMethod
*omethod
= container
->owner
.method
;
5044 klass
->name_space
= (omethod
&& omethod
->klass
) ? omethod
->klass
->name_space
: "";
5046 MonoClass
*oklass
= container
->owner
.klass
;
5047 klass
->name_space
= oklass
? oklass
->name_space
: "";
5050 klass
->name_space
= "";
5053 mono_profiler_class_event (klass
, MONO_PROFILE_START_LOAD
);
5057 for (ptr
= pinfo
->constraints
; ptr
&& *ptr
; ptr
++, count
++)
5061 if ((count
> 0) && !MONO_CLASS_IS_INTERFACE (pinfo
->constraints
[0])) {
5062 klass
->parent
= pinfo
->constraints
[0];
5064 } else if (pinfo
&& pinfo
->flags
& GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT
)
5065 klass
->parent
= mono_class_from_name (mono_defaults
.corlib
, "System", "ValueType");
5067 klass
->parent
= mono_defaults
.object_class
;
5070 if (count
- pos
> 0) {
5071 klass
->interface_count
= count
- pos
;
5072 klass
->interfaces
= mono_image_alloc0 (image
, sizeof (MonoClass
*) * (count
- pos
));
5073 for (i
= pos
; i
< count
; i
++)
5074 klass
->interfaces
[i
- pos
] = pinfo
->constraints
[i
];
5077 klass
->image
= image
;
5079 klass
->inited
= TRUE
;
5080 klass
->cast_class
= klass
->element_class
= klass
;
5081 klass
->flags
= TYPE_ATTRIBUTE_PUBLIC
;
5083 klass
->this_arg
.type
= klass
->byval_arg
.type
= is_mvar
? MONO_TYPE_MVAR
: MONO_TYPE_VAR
;
5084 klass
->this_arg
.data
.generic_param
= klass
->byval_arg
.data
.generic_param
= param
;
5085 klass
->this_arg
.byref
= TRUE
;
5087 /* FIXME: shouldn't this be ->type_token? */
5088 klass
->sizes
.generic_param_token
= pinfo
? pinfo
->token
: 0;
5090 mono_class_setup_supertypes (klass
);
5095 #define FAST_CACHE_SIZE 1024
5096 static MonoClass
*var_cache_fast
[FAST_CACHE_SIZE
];
5097 static MonoClass
*mvar_cache_fast
[FAST_CACHE_SIZE
];
5098 static GHashTable
*var_cache_slow
;
5099 static GHashTable
*mvar_cache_slow
;
5102 get_anon_gparam_class (MonoGenericParam
*param
, gboolean is_mvar
)
5104 int n
= mono_generic_param_num (param
);
5107 if (n
< FAST_CACHE_SIZE
)
5108 return (is_mvar
? mvar_cache_fast
: var_cache_fast
) [n
];
5109 ht
= is_mvar
? mvar_cache_slow
: var_cache_slow
;
5110 return ht
? g_hash_table_lookup (ht
, GINT_TO_POINTER (n
)) : NULL
;
5114 set_anon_gparam_class (MonoGenericParam
*param
, gboolean is_mvar
, MonoClass
*klass
)
5116 int n
= mono_generic_param_num (param
);
5119 if (n
< FAST_CACHE_SIZE
) {
5120 (is_mvar
? mvar_cache_fast
: var_cache_fast
) [n
] = klass
;
5123 ht
= is_mvar
? mvar_cache_slow
: var_cache_slow
;
5125 ht
= g_hash_table_new (NULL
, NULL
);
5127 mvar_cache_slow
= ht
;
5129 var_cache_slow
= ht
;
5132 g_hash_table_insert (ht
, GINT_TO_POINTER (n
), klass
);
5136 * LOCKING: Acquires the loader lock.
5139 mono_class_from_generic_parameter (MonoGenericParam
*param
, MonoImage
*image
, gboolean is_mvar
)
5141 MonoGenericContainer
*container
= mono_generic_param_owner (param
);
5142 MonoGenericParamInfo
*pinfo
;
5145 mono_loader_lock ();
5148 pinfo
= mono_generic_param_info (param
);
5149 if (pinfo
->pklass
) {
5150 mono_loader_unlock ();
5151 return pinfo
->pklass
;
5157 klass
= get_anon_gparam_class (param
, is_mvar
);
5159 mono_loader_unlock ();
5164 if (!image
&& container
) {
5166 MonoMethod
*method
= container
->owner
.method
;
5167 image
= (method
&& method
->klass
) ? method
->klass
->image
: NULL
;
5169 MonoClass
*klass
= container
->owner
.klass
;
5170 // FIXME: 'klass' should not be null
5171 // But, monodis creates GenericContainers without associating a owner to it
5172 image
= klass
? klass
->image
: NULL
;
5176 klass
= make_generic_param_class (param
, image
, is_mvar
, pinfo
);
5178 mono_memory_barrier ();
5181 pinfo
->pklass
= klass
;
5183 set_anon_gparam_class (param
, is_mvar
, klass
);
5185 mono_loader_unlock ();
5187 /* FIXME: Should this go inside 'make_generic_param_klass'? */
5188 mono_profiler_class_loaded (klass
, MONO_PROFILE_OK
);
5194 mono_ptr_class_get (MonoType
*type
)
5197 MonoClass
*el_class
;
5201 el_class
= mono_class_from_mono_type (type
);
5202 image
= el_class
->image
;
5204 mono_loader_lock ();
5206 if (!image
->ptr_cache
)
5207 image
->ptr_cache
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5209 if ((result
= g_hash_table_lookup (image
->ptr_cache
, el_class
))) {
5210 mono_loader_unlock ();
5213 result
= mono_image_alloc0 (image
, sizeof (MonoClass
));
5215 classes_size
+= sizeof (MonoClass
);
5217 result
->parent
= NULL
; /* no parent for PTR types */
5218 result
->name_space
= el_class
->name_space
;
5219 name
= g_strdup_printf ("%s*", el_class
->name
);
5220 result
->name
= mono_image_strdup (image
, name
);
5223 mono_profiler_class_event (result
, MONO_PROFILE_START_LOAD
);
5225 result
->image
= el_class
->image
;
5226 result
->inited
= TRUE
;
5227 result
->flags
= TYPE_ATTRIBUTE_CLASS
| (el_class
->flags
& TYPE_ATTRIBUTE_VISIBILITY_MASK
);
5228 /* Can pointers get boxed? */
5229 result
->instance_size
= sizeof (gpointer
);
5230 result
->cast_class
= result
->element_class
= el_class
;
5231 result
->blittable
= TRUE
;
5233 result
->this_arg
.type
= result
->byval_arg
.type
= MONO_TYPE_PTR
;
5234 result
->this_arg
.data
.type
= result
->byval_arg
.data
.type
= &result
->element_class
->byval_arg
;
5235 result
->this_arg
.byref
= TRUE
;
5237 mono_class_setup_supertypes (result
);
5239 g_hash_table_insert (image
->ptr_cache
, el_class
, result
);
5241 mono_loader_unlock ();
5243 mono_profiler_class_loaded (result
, MONO_PROFILE_OK
);
5249 mono_fnptr_class_get (MonoMethodSignature
*sig
)
5252 static GHashTable
*ptr_hash
= NULL
;
5254 /* FIXME: These should be allocate from a mempool as well, but which one ? */
5256 mono_loader_lock ();
5259 ptr_hash
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5261 if ((result
= g_hash_table_lookup (ptr_hash
, sig
))) {
5262 mono_loader_unlock ();
5265 result
= g_new0 (MonoClass
, 1);
5267 result
->parent
= NULL
; /* no parent for PTR types */
5268 result
->name_space
= "System";
5269 result
->name
= "MonoFNPtrFakeClass";
5271 mono_profiler_class_event (result
, MONO_PROFILE_START_LOAD
);
5273 result
->image
= mono_defaults
.corlib
; /* need to fix... */
5274 result
->inited
= TRUE
;
5275 result
->flags
= TYPE_ATTRIBUTE_CLASS
; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
5276 /* Can pointers get boxed? */
5277 result
->instance_size
= sizeof (gpointer
);
5278 result
->cast_class
= result
->element_class
= result
;
5279 result
->blittable
= TRUE
;
5281 result
->this_arg
.type
= result
->byval_arg
.type
= MONO_TYPE_FNPTR
;
5282 result
->this_arg
.data
.method
= result
->byval_arg
.data
.method
= sig
;
5283 result
->this_arg
.byref
= TRUE
;
5284 result
->blittable
= TRUE
;
5286 mono_class_setup_supertypes (result
);
5288 g_hash_table_insert (ptr_hash
, sig
, result
);
5290 mono_loader_unlock ();
5292 mono_profiler_class_loaded (result
, MONO_PROFILE_OK
);
5298 mono_class_from_mono_type (MonoType
*type
)
5300 switch (type
->type
) {
5301 case MONO_TYPE_OBJECT
:
5302 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.object_class
;
5303 case MONO_TYPE_VOID
:
5304 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.void_class
;
5305 case MONO_TYPE_BOOLEAN
:
5306 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.boolean_class
;
5307 case MONO_TYPE_CHAR
:
5308 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.char_class
;
5310 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.sbyte_class
;
5312 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.byte_class
;
5314 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.int16_class
;
5316 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.uint16_class
;
5318 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.int32_class
;
5320 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.uint32_class
;
5322 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.int_class
;
5324 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.uint_class
;
5326 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.int64_class
;
5328 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.uint64_class
;
5330 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.single_class
;
5332 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.double_class
;
5333 case MONO_TYPE_STRING
:
5334 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.string_class
;
5335 case MONO_TYPE_TYPEDBYREF
:
5336 return type
->data
.klass
? type
->data
.klass
: mono_defaults
.typed_reference_class
;
5337 case MONO_TYPE_ARRAY
:
5338 return mono_bounded_array_class_get (type
->data
.array
->eklass
, type
->data
.array
->rank
, TRUE
);
5340 return mono_ptr_class_get (type
->data
.type
);
5341 case MONO_TYPE_FNPTR
:
5342 return mono_fnptr_class_get (type
->data
.method
);
5343 case MONO_TYPE_SZARRAY
:
5344 return mono_array_class_get (type
->data
.klass
, 1);
5345 case MONO_TYPE_CLASS
:
5346 case MONO_TYPE_VALUETYPE
:
5347 return type
->data
.klass
;
5348 case MONO_TYPE_GENERICINST
:
5349 return mono_generic_class_get_class (type
->data
.generic_class
);
5351 return mono_class_from_generic_parameter (type
->data
.generic_param
, NULL
, FALSE
);
5352 case MONO_TYPE_MVAR
:
5353 return mono_class_from_generic_parameter (type
->data
.generic_param
, NULL
, TRUE
);
5355 g_warning ("mono_class_from_mono_type: implement me 0x%02x\n", type
->type
);
5356 g_assert_not_reached ();
5363 * mono_type_retrieve_from_typespec
5364 * @image: context where the image is created
5365 * @type_spec: typespec token
5366 * @context: the generic context used to evaluate generic instantiations in
5369 mono_type_retrieve_from_typespec (MonoImage
*image
, guint32 type_spec
, MonoGenericContext
*context
, gboolean
*did_inflate
, MonoError
*error
)
5371 MonoType
*t
= mono_type_create_from_typespec (image
, type_spec
);
5373 mono_error_init (error
);
5374 *did_inflate
= FALSE
;
5377 char *name
= mono_class_name_from_token (image
, type_spec
);
5378 char *assembly
= mono_assembly_name_from_token (image
, type_spec
);
5379 mono_error_set_type_load_name (error
, name
, assembly
, "Could not resolve typespec token %08x", type_spec
);
5383 if (context
&& (context
->class_inst
|| context
->method_inst
)) {
5384 MonoType
*inflated
= inflate_generic_type (NULL
, t
, context
, error
);
5386 if (!mono_error_ok (error
))
5391 *did_inflate
= TRUE
;
5398 * mono_class_create_from_typespec
5399 * @image: context where the image is created
5400 * @type_spec: typespec token
5401 * @context: the generic context used to evaluate generic instantiations in
5404 mono_class_create_from_typespec (MonoImage
*image
, guint32 type_spec
, MonoGenericContext
*context
, MonoError
*error
)
5407 gboolean inflated
= FALSE
;
5408 MonoType
*t
= mono_type_retrieve_from_typespec (image
, type_spec
, context
, &inflated
, error
);
5409 if (!mono_error_ok (error
))
5411 ret
= mono_class_from_mono_type (t
);
5413 mono_metadata_free_type (t
);
5418 * mono_bounded_array_class_get:
5419 * @element_class: element class
5420 * @rank: the dimension of the array class
5421 * @bounded: whenever the array has non-zero bounds
5423 * Returns: a class object describing the array with element type @element_type and
5427 mono_bounded_array_class_get (MonoClass
*eclass
, guint32 rank
, gboolean bounded
)
5431 MonoClass
*parent
= NULL
;
5432 GSList
*list
, *rootlist
= NULL
;
5435 gboolean corlib_type
= FALSE
;
5437 g_assert (rank
<= 255);
5440 /* bounded only matters for one-dimensional arrays */
5443 image
= eclass
->image
;
5445 if (rank
== 1 && !bounded
) {
5447 * This case is very frequent not just during compilation because of calls
5448 * from mono_class_from_mono_type (), mono_array_new (),
5449 * Array:CreateInstance (), etc, so use a separate cache + a separate lock.
5451 EnterCriticalSection (&image
->szarray_cache_lock
);
5452 if (!image
->szarray_cache
)
5453 image
->szarray_cache
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5454 class = g_hash_table_lookup (image
->szarray_cache
, eclass
);
5455 LeaveCriticalSection (&image
->szarray_cache_lock
);
5459 mono_loader_lock ();
5461 mono_loader_lock ();
5463 if (!image
->array_cache
)
5464 image
->array_cache
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
5466 if ((rootlist
= list
= g_hash_table_lookup (image
->array_cache
, eclass
))) {
5467 for (; list
; list
= list
->next
) {
5469 if ((class->rank
== rank
) && (class->byval_arg
.type
== (((rank
> 1) || bounded
) ? MONO_TYPE_ARRAY
: MONO_TYPE_SZARRAY
))) {
5470 mono_loader_unlock ();
5477 /* for the building corlib use System.Array from it */
5478 if (image
->assembly
&& image
->assembly
->dynamic
&& image
->assembly_name
&& strcmp (image
->assembly_name
, "mscorlib") == 0) {
5479 parent
= mono_class_from_name (image
, "System", "Array");
5482 parent
= mono_defaults
.array_class
;
5483 if (!parent
->inited
)
5484 mono_class_init (parent
);
5487 class = mono_image_alloc0 (image
, sizeof (MonoClass
));
5489 class->image
= image
;
5490 class->name_space
= eclass
->name_space
;
5491 nsize
= strlen (eclass
->name
);
5492 name
= g_malloc (nsize
+ 2 + rank
+ 1);
5493 memcpy (name
, eclass
->name
, nsize
);
5496 memset (name
+ nsize
+ 1, ',', rank
- 1);
5498 name
[nsize
+ rank
] = '*';
5499 name
[nsize
+ rank
+ bounded
] = ']';
5500 name
[nsize
+ rank
+ bounded
+ 1] = 0;
5501 class->name
= mono_image_strdup (image
, name
);
5504 mono_profiler_class_event (class, MONO_PROFILE_START_LOAD
);
5506 classes_size
+= sizeof (MonoClass
);
5508 class->type_token
= 0;
5509 /* all arrays are marked serializable and sealed, bug #42779 */
5510 class->flags
= TYPE_ATTRIBUTE_CLASS
| TYPE_ATTRIBUTE_SERIALIZABLE
| TYPE_ATTRIBUTE_SEALED
| TYPE_ATTRIBUTE_PUBLIC
;
5511 class->parent
= parent
;
5512 class->instance_size
= mono_class_instance_size (class->parent
);
5514 if (eclass
->enumtype
&& !mono_class_enum_basetype (eclass
)) {
5515 if (!eclass
->reflection_info
|| eclass
->wastypebuilder
) {
5516 g_warning ("Only incomplete TypeBuilder objects are allowed to be an enum without base_type");
5517 g_assert (eclass
->reflection_info
&& !eclass
->wastypebuilder
);
5519 /* element_size -1 is ok as this is not an instantitable type*/
5520 class->sizes
.element_size
= -1;
5522 class->sizes
.element_size
= mono_class_array_element_size (eclass
);
5524 mono_class_setup_supertypes (class);
5526 if (eclass
->generic_class
)
5527 mono_class_init (eclass
);
5528 if (!eclass
->size_inited
)
5529 mono_class_setup_fields (eclass
);
5530 if (eclass
->exception_type
) /*FIXME we fail the array type, but we have to let other fields be set.*/
5531 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD
, NULL
);
5533 class->has_references
= MONO_TYPE_IS_REFERENCE (&eclass
->byval_arg
) || eclass
->has_references
? TRUE
: FALSE
;
5537 if (eclass
->enumtype
)
5538 class->cast_class
= eclass
->element_class
;
5540 class->cast_class
= eclass
;
5542 class->element_class
= eclass
;
5544 if ((rank
> 1) || bounded
) {
5545 MonoArrayType
*at
= mono_image_alloc0 (image
, sizeof (MonoArrayType
));
5546 class->byval_arg
.type
= MONO_TYPE_ARRAY
;
5547 class->byval_arg
.data
.array
= at
;
5548 at
->eklass
= eclass
;
5550 /* FIXME: complete.... */
5552 class->byval_arg
.type
= MONO_TYPE_SZARRAY
;
5553 class->byval_arg
.data
.klass
= eclass
;
5555 class->this_arg
= class->byval_arg
;
5556 class->this_arg
.byref
= 1;
5561 class->generic_container
= eclass
->generic_container
;
5563 if (rank
== 1 && !bounded
) {
5564 MonoClass
*prev_class
;
5566 EnterCriticalSection (&image
->szarray_cache_lock
);
5567 prev_class
= g_hash_table_lookup (image
->szarray_cache
, eclass
);
5569 /* Someone got in before us */
5572 g_hash_table_insert (image
->szarray_cache
, eclass
, class);
5573 LeaveCriticalSection (&image
->szarray_cache_lock
);
5575 list
= g_slist_append (rootlist
, class);
5576 g_hash_table_insert (image
->array_cache
, eclass
, list
);
5579 mono_loader_unlock ();
5581 mono_profiler_class_loaded (class, MONO_PROFILE_OK
);
5587 * mono_array_class_get:
5588 * @element_class: element class
5589 * @rank: the dimension of the array class
5591 * Returns: a class object describing the array with element type @element_type and
5595 mono_array_class_get (MonoClass
*eclass
, guint32 rank
)
5597 return mono_bounded_array_class_get (eclass
, rank
, FALSE
);
5601 * mono_class_instance_size:
5604 * Returns: the size of an object instance
5607 mono_class_instance_size (MonoClass
*klass
)
5609 if (!klass
->size_inited
)
5610 mono_class_init (klass
);
5612 return klass
->instance_size
;
5616 * mono_class_min_align:
5619 * Returns: minimm alignment requirements
5622 mono_class_min_align (MonoClass
*klass
)
5624 if (!klass
->size_inited
)
5625 mono_class_init (klass
);
5627 return klass
->min_align
;
5631 * mono_class_value_size:
5634 * This function is used for value types, and return the
5635 * space and the alignment to store that kind of value object.
5637 * Returns: the size of a value of kind @klass
5640 mono_class_value_size (MonoClass
*klass
, guint32
*align
)
5644 /* fixme: check disable, because we still have external revereces to
5645 * mscorlib and Dummy Objects
5647 /*g_assert (klass->valuetype);*/
5649 size
= mono_class_instance_size (klass
) - sizeof (MonoObject
);
5652 *align
= klass
->min_align
;
5658 * mono_class_data_size:
5661 * Returns: the size of the static class data
5664 mono_class_data_size (MonoClass
*klass
)
5667 mono_class_init (klass
);
5669 /* in arrays, sizes.class_size is unioned with element_size
5670 * and arrays have no static fields
5674 return klass
->sizes
.class_size
;
5678 * Auxiliary routine to mono_class_get_field
5680 * Takes a field index instead of a field token.
5682 static MonoClassField
*
5683 mono_class_get_field_idx (MonoClass
*class, int idx
)
5685 mono_class_setup_fields_locking (class);
5686 if (class->exception_type
)
5690 if (class->image
->uncompressed_metadata
) {
5692 * class->field.first points to the FieldPtr table, while idx points into the
5693 * Field table, so we have to do a search.
5695 /*FIXME this is broken for types with multiple fields with the same name.*/
5696 const char *name
= mono_metadata_string_heap (class->image
, mono_metadata_decode_row_col (&class->image
->tables
[MONO_TABLE_FIELD
], idx
, MONO_FIELD_NAME
));
5699 for (i
= 0; i
< class->field
.count
; ++i
)
5700 if (mono_field_get_name (&class->fields
[i
]) == name
)
5701 return &class->fields
[i
];
5702 g_assert_not_reached ();
5704 if (class->field
.count
) {
5705 if ((idx
>= class->field
.first
) && (idx
< class->field
.first
+ class->field
.count
)){
5706 return &class->fields
[idx
- class->field
.first
];
5710 class = class->parent
;
5716 * mono_class_get_field:
5717 * @class: the class to lookup the field.
5718 * @field_token: the field token
5720 * Returns: A MonoClassField representing the type and offset of
5721 * the field, or a NULL value if the field does not belong to this
5725 mono_class_get_field (MonoClass
*class, guint32 field_token
)
5727 int idx
= mono_metadata_token_index (field_token
);
5729 g_assert (mono_metadata_token_code (field_token
) == MONO_TOKEN_FIELD_DEF
);
5731 return mono_class_get_field_idx (class, idx
- 1);
5735 * mono_class_get_field_from_name:
5736 * @klass: the class to lookup the field.
5737 * @name: the field name
5739 * Search the class @klass and it's parents for a field with the name @name.
5741 * Returns: the MonoClassField pointer of the named field or NULL
5744 mono_class_get_field_from_name (MonoClass
*klass
, const char *name
)
5746 return mono_class_get_field_from_name_full (klass
, name
, NULL
);
5750 * mono_class_get_field_from_name_full:
5751 * @klass: the class to lookup the field.
5752 * @name: the field name
5753 * @type: the type of the fields. This optional.
5755 * Search the class @klass and it's parents for a field with the name @name and type @type.
5757 * If @klass is an inflated generic type, the type comparison is done with the equivalent field
5758 * of its generic type definition.
5760 * Returns: the MonoClassField pointer of the named field or NULL
5763 mono_class_get_field_from_name_full (MonoClass
*klass
, const char *name
, MonoType
*type
)
5767 mono_class_setup_fields_locking (klass
);
5768 if (klass
->exception_type
)
5772 for (i
= 0; i
< klass
->field
.count
; ++i
) {
5773 MonoClassField
*field
= &klass
->fields
[i
];
5775 if (strcmp (name
, mono_field_get_name (field
)) != 0)
5779 MonoType
*field_type
= mono_metadata_get_corresponding_field_from_generic_type_definition (field
)->type
;
5780 if (!mono_metadata_type_equal_full (type
, field_type
, TRUE
))
5785 klass
= klass
->parent
;
5791 * mono_class_get_field_token:
5792 * @field: the field we need the token of
5794 * Get the token of a field. Note that the tokesn is only valid for the image
5795 * the field was loaded from. Don't use this function for fields in dynamic types.
5797 * Returns: the token representing the field in the image it was loaded from.
5800 mono_class_get_field_token (MonoClassField
*field
)
5802 MonoClass
*klass
= field
->parent
;
5805 mono_class_setup_fields_locking (klass
);
5806 if (klass
->exception_type
)
5810 for (i
= 0; i
< klass
->field
.count
; ++i
) {
5811 if (&klass
->fields
[i
] == field
) {
5812 int idx
= klass
->field
.first
+ i
+ 1;
5814 if (klass
->image
->uncompressed_metadata
)
5815 idx
= mono_metadata_translate_token_index (klass
->image
, MONO_TABLE_FIELD
, idx
);
5816 return mono_metadata_make_token (MONO_TABLE_FIELD
, idx
);
5819 klass
= klass
->parent
;
5822 g_assert_not_reached ();
5827 mono_field_get_index (MonoClassField
*field
)
5829 int index
= field
- field
->parent
->fields
;
5831 g_assert (index
>= 0 && index
< field
->parent
->field
.count
);
5837 * mono_class_get_field_default_value:
5839 * Return the default value of the field as a pointer into the metadata blob.
5842 mono_class_get_field_default_value (MonoClassField
*field
, MonoTypeEnum
*def_type
)
5845 guint32 constant_cols
[MONO_CONSTANT_SIZE
];
5847 MonoClass
*klass
= field
->parent
;
5849 g_assert (field
->type
->attrs
& FIELD_ATTRIBUTE_HAS_DEFAULT
);
5851 if (!klass
->ext
|| !klass
->ext
->field_def_values
) {
5852 mono_loader_lock ();
5853 mono_class_alloc_ext (klass
);
5854 if (!klass
->ext
->field_def_values
)
5855 klass
->ext
->field_def_values
= mono_image_alloc0 (klass
->image
, sizeof (MonoFieldDefaultValue
) * klass
->field
.count
);
5856 mono_loader_unlock ();
5859 field_index
= mono_field_get_index (field
);
5861 if (!klass
->ext
->field_def_values
[field_index
].data
) {
5862 cindex
= mono_metadata_get_constant_index (field
->parent
->image
, mono_class_get_field_token (field
), 0);
5864 g_assert (!(field
->type
->attrs
& FIELD_ATTRIBUTE_HAS_FIELD_RVA
));
5866 mono_metadata_decode_row (&field
->parent
->image
->tables
[MONO_TABLE_CONSTANT
], cindex
- 1, constant_cols
, MONO_CONSTANT_SIZE
);
5867 klass
->ext
->field_def_values
[field_index
].def_type
= constant_cols
[MONO_CONSTANT_TYPE
];
5868 klass
->ext
->field_def_values
[field_index
].data
= (gpointer
)mono_metadata_blob_heap (field
->parent
->image
, constant_cols
[MONO_CONSTANT_VALUE
]);
5871 *def_type
= klass
->ext
->field_def_values
[field_index
].def_type
;
5872 return klass
->ext
->field_def_values
[field_index
].data
;
5876 mono_class_get_event_token (MonoEvent
*event
)
5878 MonoClass
*klass
= event
->parent
;
5883 for (i
= 0; i
< klass
->ext
->event
.count
; ++i
) {
5884 if (&klass
->ext
->events
[i
] == event
)
5885 return mono_metadata_make_token (MONO_TABLE_EVENT
, klass
->ext
->event
.first
+ i
+ 1);
5888 klass
= klass
->parent
;
5891 g_assert_not_reached ();
5896 mono_class_get_property_from_name (MonoClass
*klass
, const char *name
)
5900 gpointer iter
= NULL
;
5901 while ((p
= mono_class_get_properties (klass
, &iter
))) {
5902 if (! strcmp (name
, p
->name
))
5905 klass
= klass
->parent
;
5911 mono_class_get_property_token (MonoProperty
*prop
)
5913 MonoClass
*klass
= prop
->parent
;
5917 gpointer iter
= NULL
;
5918 while ((p
= mono_class_get_properties (klass
, &iter
))) {
5919 if (&klass
->ext
->properties
[i
] == prop
)
5920 return mono_metadata_make_token (MONO_TABLE_PROPERTY
, klass
->ext
->property
.first
+ i
+ 1);
5924 klass
= klass
->parent
;
5927 g_assert_not_reached ();
5932 mono_class_name_from_token (MonoImage
*image
, guint32 type_token
)
5934 const char *name
, *nspace
;
5936 return g_strdup_printf ("DynamicType 0x%08x", type_token
);
5938 switch (type_token
& 0xff000000){
5939 case MONO_TOKEN_TYPE_DEF
: {
5940 guint32 cols
[MONO_TYPEDEF_SIZE
];
5941 MonoTableInfo
*tt
= &image
->tables
[MONO_TABLE_TYPEDEF
];
5942 guint tidx
= mono_metadata_token_index (type_token
);
5944 if (tidx
> tt
->rows
)
5945 return g_strdup_printf ("Invalid type token 0x%08x", type_token
);
5947 mono_metadata_decode_row (tt
, tidx
- 1, cols
, MONO_TYPEDEF_SIZE
);
5948 name
= mono_metadata_string_heap (image
, cols
[MONO_TYPEDEF_NAME
]);
5949 nspace
= mono_metadata_string_heap (image
, cols
[MONO_TYPEDEF_NAMESPACE
]);
5950 if (strlen (nspace
) == 0)
5951 return g_strdup_printf ("%s", name
);
5953 return g_strdup_printf ("%s.%s", nspace
, name
);
5956 case MONO_TOKEN_TYPE_REF
: {
5957 guint32 cols
[MONO_TYPEREF_SIZE
];
5958 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_TYPEREF
];
5959 guint tidx
= mono_metadata_token_index (type_token
);
5962 return g_strdup_printf ("Invalid type token 0x%08x", type_token
);
5963 mono_metadata_decode_row (t
, tidx
-1, cols
, MONO_TYPEREF_SIZE
);
5964 name
= mono_metadata_string_heap (image
, cols
[MONO_TYPEREF_NAME
]);
5965 nspace
= mono_metadata_string_heap (image
, cols
[MONO_TYPEREF_NAMESPACE
]);
5966 if (strlen (nspace
) == 0)
5967 return g_strdup_printf ("%s", name
);
5969 return g_strdup_printf ("%s.%s", nspace
, name
);
5972 case MONO_TOKEN_TYPE_SPEC
:
5973 return g_strdup_printf ("Typespec 0x%08x", type_token
);
5975 return g_strdup_printf ("Invalid type token 0x%08x", type_token
);
5980 mono_assembly_name_from_token (MonoImage
*image
, guint32 type_token
)
5983 return g_strdup_printf ("DynamicAssembly %s", image
->name
);
5985 switch (type_token
& 0xff000000){
5986 case MONO_TOKEN_TYPE_DEF
:
5987 return mono_stringify_assembly_name (&image
->assembly
->aname
);
5989 case MONO_TOKEN_TYPE_REF
: {
5990 MonoAssemblyName aname
;
5991 guint32 cols
[MONO_TYPEREF_SIZE
];
5992 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_TYPEREF
];
5993 guint32 idx
= mono_metadata_token_index (type_token
);
5996 return g_strdup_printf ("Invalid type token 0x%08x", type_token
);
5998 mono_metadata_decode_row (t
, idx
-1, cols
, MONO_TYPEREF_SIZE
);
6000 idx
= cols
[MONO_TYPEREF_SCOPE
] >> MONO_RESOLTION_SCOPE_BITS
;
6001 switch (cols
[MONO_TYPEREF_SCOPE
] & MONO_RESOLTION_SCOPE_MASK
) {
6002 case MONO_RESOLTION_SCOPE_MODULE
:
6004 return g_strdup ("");
6005 case MONO_RESOLTION_SCOPE_MODULEREF
:
6007 return g_strdup ("");
6008 case MONO_RESOLTION_SCOPE_TYPEREF
:
6010 return g_strdup ("");
6011 case MONO_RESOLTION_SCOPE_ASSEMBLYREF
:
6012 mono_assembly_get_assemblyref (image
, idx
- 1, &aname
);
6013 return mono_stringify_assembly_name (&aname
);
6015 g_assert_not_reached ();
6019 case MONO_TOKEN_TYPE_SPEC
:
6021 return g_strdup ("");
6023 g_assert_not_reached ();
6030 * mono_class_get_full:
6031 * @image: the image where the class resides
6032 * @type_token: the token for the class
6033 * @context: the generic context used to evaluate generic instantiations in
6035 * Returns: the MonoClass that represents @type_token in @image
6038 mono_class_get_full (MonoImage
*image
, guint32 type_token
, MonoGenericContext
*context
)
6041 MonoClass
*class = NULL
;
6043 if (image
->dynamic
) {
6044 int table
= mono_metadata_token_table (type_token
);
6046 if (table
!= MONO_TABLE_TYPEDEF
&& table
!= MONO_TABLE_TYPEREF
&& table
!= MONO_TABLE_TYPESPEC
) {
6047 mono_loader_set_error_bad_image (g_strdup ("Bad type token."));
6050 return mono_lookup_dynamic_token (image
, type_token
, context
);
6053 switch (type_token
& 0xff000000){
6054 case MONO_TOKEN_TYPE_DEF
:
6055 class = mono_class_create_from_typedef (image
, type_token
);
6057 case MONO_TOKEN_TYPE_REF
:
6058 class = mono_class_from_typeref (image
, type_token
);
6060 case MONO_TOKEN_TYPE_SPEC
:
6061 class = mono_class_create_from_typespec (image
, type_token
, context
, &error
);
6062 if (!mono_error_ok (&error
)) {
6063 /*FIXME don't swallow the error message*/
6064 mono_error_cleanup (&error
);
6068 g_warning ("unknown token type %x", type_token
& 0xff000000);
6069 g_assert_not_reached ();
6073 char *name
= mono_class_name_from_token (image
, type_token
);
6074 char *assembly
= mono_assembly_name_from_token (image
, type_token
);
6075 mono_loader_set_error_type_load (name
, assembly
);
6083 * mono_type_get_full:
6084 * @image: the image where the type resides
6085 * @type_token: the token for the type
6086 * @context: the generic context used to evaluate generic instantiations in
6088 * This functions exists to fullfill the fact that sometimes it's desirable to have access to the
6090 * Returns: the MonoType that represents @type_token in @image
6093 mono_type_get_full (MonoImage
*image
, guint32 type_token
, MonoGenericContext
*context
)
6096 MonoType
*type
= NULL
;
6097 gboolean inflated
= FALSE
;
6099 //FIXME: this will not fix the very issue for which mono_type_get_full exists -but how to do it then?
6101 return mono_class_get_type (mono_lookup_dynamic_token (image
, type_token
, context
));
6103 if ((type_token
& 0xff000000) != MONO_TOKEN_TYPE_SPEC
) {
6104 MonoClass
*class = mono_class_get_full (image
, type_token
, context
);
6105 return class ? mono_class_get_type (class) : NULL
;
6108 type
= mono_type_retrieve_from_typespec (image
, type_token
, context
, &inflated
, &error
);
6110 if (!mono_error_ok (&error
)) {
6111 /*FIXME don't swalloc the error message.*/
6112 char *name
= mono_class_name_from_token (image
, type_token
);
6113 char *assembly
= mono_assembly_name_from_token (image
, type_token
);
6115 g_warning ("Error loading type %s from %s due to %s", name
, assembly
, mono_error_get_message (&error
));
6117 mono_error_cleanup (&error
);
6118 mono_loader_set_error_type_load (name
, assembly
);
6123 MonoType
*tmp
= type
;
6124 type
= mono_class_get_type (mono_class_from_mono_type (type
));
6125 /* FIXME: This is a workaround fo the fact that a typespec token sometimes reference to the generic type definition.
6126 * A MonoClass::byval_arg of a generic type definion has type CLASS.
6127 * Some parts of mono create a GENERICINST to reference a generic type definition and this generates confict with byval_arg.
6129 * The long term solution is to chaise this places and make then set MonoType::type correctly.
6131 if (type
->type
!= tmp
->type
)
6134 mono_metadata_free_type (tmp
);
6141 mono_class_get (MonoImage
*image
, guint32 type_token
)
6143 return mono_class_get_full (image
, type_token
, NULL
);
6147 * mono_image_init_name_cache:
6149 * Initializes the class name cache stored in image->name_cache.
6151 * LOCKING: Acquires the corresponding image lock.
6154 mono_image_init_name_cache (MonoImage
*image
)
6156 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_TYPEDEF
];
6157 guint32 cols
[MONO_TYPEDEF_SIZE
];
6160 guint32 i
, visib
, nspace_index
;
6161 GHashTable
*name_cache2
, *nspace_table
;
6163 mono_image_lock (image
);
6165 if (image
->name_cache
) {
6166 mono_image_unlock (image
);
6170 image
->name_cache
= g_hash_table_new (g_str_hash
, g_str_equal
);
6172 if (image
->dynamic
) {
6173 mono_image_unlock (image
);
6177 /* Temporary hash table to avoid lookups in the nspace_table */
6178 name_cache2
= g_hash_table_new (NULL
, NULL
);
6180 for (i
= 1; i
<= t
->rows
; ++i
) {
6181 mono_metadata_decode_row (t
, i
- 1, cols
, MONO_TYPEDEF_SIZE
);
6182 visib
= cols
[MONO_TYPEDEF_FLAGS
] & TYPE_ATTRIBUTE_VISIBILITY_MASK
;
6184 * Nested types are accessed from the nesting name. We use the fact that nested types use different visibility flags
6185 * than toplevel types, thus avoiding the need to grovel through the NESTED_TYPE table
6187 if (visib
>= TYPE_ATTRIBUTE_NESTED_PUBLIC
&& visib
<= TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM
)
6189 name
= mono_metadata_string_heap (image
, cols
[MONO_TYPEDEF_NAME
]);
6190 nspace
= mono_metadata_string_heap (image
, cols
[MONO_TYPEDEF_NAMESPACE
]);
6192 nspace_index
= cols
[MONO_TYPEDEF_NAMESPACE
];
6193 nspace_table
= g_hash_table_lookup (name_cache2
, GUINT_TO_POINTER (nspace_index
));
6194 if (!nspace_table
) {
6195 nspace_table
= g_hash_table_new (g_str_hash
, g_str_equal
);
6196 g_hash_table_insert (image
->name_cache
, (char*)nspace
, nspace_table
);
6197 g_hash_table_insert (name_cache2
, GUINT_TO_POINTER (nspace_index
),
6200 g_hash_table_insert (nspace_table
, (char *) name
, GUINT_TO_POINTER (i
));
6203 /* Load type names from EXPORTEDTYPES table */
6205 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_EXPORTEDTYPE
];
6206 guint32 cols
[MONO_EXP_TYPE_SIZE
];
6209 for (i
= 0; i
< t
->rows
; ++i
) {
6210 mono_metadata_decode_row (t
, i
, cols
, MONO_EXP_TYPE_SIZE
);
6211 name
= mono_metadata_string_heap (image
, cols
[MONO_EXP_TYPE_NAME
]);
6212 nspace
= mono_metadata_string_heap (image
, cols
[MONO_EXP_TYPE_NAMESPACE
]);
6214 nspace_index
= cols
[MONO_EXP_TYPE_NAMESPACE
];
6215 nspace_table
= g_hash_table_lookup (name_cache2
, GUINT_TO_POINTER (nspace_index
));
6216 if (!nspace_table
) {
6217 nspace_table
= g_hash_table_new (g_str_hash
, g_str_equal
);
6218 g_hash_table_insert (image
->name_cache
, (char*)nspace
, nspace_table
);
6219 g_hash_table_insert (name_cache2
, GUINT_TO_POINTER (nspace_index
),
6222 g_hash_table_insert (nspace_table
, (char *) name
, GUINT_TO_POINTER (mono_metadata_make_token (MONO_TABLE_EXPORTEDTYPE
, i
+ 1)));
6226 g_hash_table_destroy (name_cache2
);
6227 mono_image_unlock (image
);
6230 /*FIXME Only dynamic assemblies should allow this operation.*/
6232 mono_image_add_to_name_cache (MonoImage
*image
, const char *nspace
,
6233 const char *name
, guint32 index
)
6235 GHashTable
*nspace_table
;
6236 GHashTable
*name_cache
;
6239 mono_image_lock (image
);
6241 if (!image
->name_cache
)
6242 mono_image_init_name_cache (image
);
6244 name_cache
= image
->name_cache
;
6245 if (!(nspace_table
= g_hash_table_lookup (name_cache
, nspace
))) {
6246 nspace_table
= g_hash_table_new (g_str_hash
, g_str_equal
);
6247 g_hash_table_insert (name_cache
, (char *)nspace
, (char *)nspace_table
);
6250 if ((old_index
= GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table
, (char*) name
))))
6251 g_error ("overrwritting old token %x on image %s for type %s::%s", old_index
, image
->name
, nspace
, name
);
6253 g_hash_table_insert (nspace_table
, (char *) name
, GUINT_TO_POINTER (index
));
6255 mono_image_unlock (image
);
6264 find_nocase (gpointer key
, gpointer value
, gpointer user_data
)
6266 char *name
= (char*)key
;
6267 FindUserData
*data
= (FindUserData
*)user_data
;
6269 if (!data
->value
&& (mono_utf8_strcasecmp (name
, (char*)data
->key
) == 0))
6270 data
->value
= value
;
6274 * mono_class_from_name_case:
6275 * @image: The MonoImage where the type is looked up in
6276 * @name_space: the type namespace
6277 * @name: the type short name.
6279 * Obtains a MonoClass with a given namespace and a given name which
6280 * is located in the given MonoImage. The namespace and name
6281 * lookups are case insensitive.
6284 mono_class_from_name_case (MonoImage
*image
, const char* name_space
, const char *name
)
6286 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_TYPEDEF
];
6287 guint32 cols
[MONO_TYPEDEF_SIZE
];
6292 if (image
->dynamic
) {
6294 FindUserData user_data
;
6296 mono_image_lock (image
);
6298 if (!image
->name_cache
)
6299 mono_image_init_name_cache (image
);
6301 user_data
.key
= name_space
;
6302 user_data
.value
= NULL
;
6303 g_hash_table_foreach (image
->name_cache
, find_nocase
, &user_data
);
6305 if (user_data
.value
) {
6306 GHashTable
*nspace_table
= (GHashTable
*)user_data
.value
;
6308 user_data
.key
= name
;
6309 user_data
.value
= NULL
;
6311 g_hash_table_foreach (nspace_table
, find_nocase
, &user_data
);
6313 if (user_data
.value
)
6314 token
= GPOINTER_TO_UINT (user_data
.value
);
6317 mono_image_unlock (image
);
6320 return mono_class_get (image
, MONO_TOKEN_TYPE_DEF
| token
);
6326 /* add a cache if needed */
6327 for (i
= 1; i
<= t
->rows
; ++i
) {
6328 mono_metadata_decode_row (t
, i
- 1, cols
, MONO_TYPEDEF_SIZE
);
6329 visib
= cols
[MONO_TYPEDEF_FLAGS
] & TYPE_ATTRIBUTE_VISIBILITY_MASK
;
6331 * Nested types are accessed from the nesting name. We use the fact that nested types use different visibility flags
6332 * than toplevel types, thus avoiding the need to grovel through the NESTED_TYPE table
6334 if (visib
>= TYPE_ATTRIBUTE_NESTED_PUBLIC
&& visib
<= TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM
)
6336 n
= mono_metadata_string_heap (image
, cols
[MONO_TYPEDEF_NAME
]);
6337 nspace
= mono_metadata_string_heap (image
, cols
[MONO_TYPEDEF_NAMESPACE
]);
6338 if (mono_utf8_strcasecmp (n
, name
) == 0 && mono_utf8_strcasecmp (nspace
, name_space
) == 0)
6339 return mono_class_get (image
, MONO_TOKEN_TYPE_DEF
| i
);
6345 return_nested_in (MonoClass
*class, char *nested
)
6348 char *s
= strchr (nested
, '/');
6349 gpointer iter
= NULL
;
6356 while ((found
= mono_class_get_nested_types (class, &iter
))) {
6357 if (strcmp (found
->name
, nested
) == 0) {
6359 return return_nested_in (found
, s
);
6367 search_modules (MonoImage
*image
, const char *name_space
, const char *name
)
6369 MonoTableInfo
*file_table
= &image
->tables
[MONO_TABLE_FILE
];
6370 MonoImage
*file_image
;
6375 * The EXPORTEDTYPES table only contains public types, so have to search the
6377 * Note: image->modules contains the contents of the MODULEREF table, while
6378 * the real module list is in the FILE table.
6380 for (i
= 0; i
< file_table
->rows
; i
++) {
6381 guint32 cols
[MONO_FILE_SIZE
];
6382 mono_metadata_decode_row (file_table
, i
, cols
, MONO_FILE_SIZE
);
6383 if (cols
[MONO_FILE_FLAGS
] == FILE_CONTAINS_NO_METADATA
)
6386 file_image
= mono_image_load_file_for_image (image
, i
+ 1);
6388 class = mono_class_from_name (file_image
, name_space
, name
);
6398 * mono_class_from_name:
6399 * @image: The MonoImage where the type is looked up in
6400 * @name_space: the type namespace
6401 * @name: the type short name.
6403 * Obtains a MonoClass with a given namespace and a given name which
6404 * is located in the given MonoImage.
6407 mono_class_from_name (MonoImage
*image
, const char* name_space
, const char *name
)
6409 GHashTable
*nspace_table
;
6410 MonoImage
*loaded_image
;
6417 if ((nested
= strchr (name
, '/'))) {
6418 int pos
= nested
- name
;
6419 int len
= strlen (name
);
6422 memcpy (buf
, name
, len
+ 1);
6424 nested
= buf
+ pos
+ 1;
6428 if (get_class_from_name
) {
6429 gboolean res
= get_class_from_name (image
, name_space
, name
, &class);
6432 class = search_modules (image
, name_space
, name
);
6434 return class ? return_nested_in (class, nested
) : NULL
;
6440 mono_image_lock (image
);
6442 if (!image
->name_cache
)
6443 mono_image_init_name_cache (image
);
6445 nspace_table
= g_hash_table_lookup (image
->name_cache
, name_space
);
6448 token
= GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table
, name
));
6450 mono_image_unlock (image
);
6452 if (!token
&& image
->dynamic
&& image
->modules
) {
6453 /* Search modules as well */
6454 for (i
= 0; i
< image
->module_count
; ++i
) {
6455 MonoImage
*module
= image
->modules
[i
];
6457 class = mono_class_from_name (module
, name_space
, name
);
6464 class = search_modules (image
, name_space
, name
);
6472 if (mono_metadata_token_table (token
) == MONO_TABLE_EXPORTEDTYPE
) {
6473 MonoTableInfo
*t
= &image
->tables
[MONO_TABLE_EXPORTEDTYPE
];
6474 guint32 cols
[MONO_EXP_TYPE_SIZE
];
6477 idx
= mono_metadata_token_index (token
);
6479 mono_metadata_decode_row (t
, idx
- 1, cols
, MONO_EXP_TYPE_SIZE
);
6481 impl
= cols
[MONO_EXP_TYPE_IMPLEMENTATION
];
6482 if ((impl
& MONO_IMPLEMENTATION_MASK
) == MONO_IMPLEMENTATION_FILE
) {
6483 loaded_image
= mono_assembly_load_module (image
->assembly
, impl
>> MONO_IMPLEMENTATION_BITS
);
6486 class = mono_class_from_name (loaded_image
, name_space
, name
);
6488 return return_nested_in (class, nested
);
6490 } else if ((impl
& MONO_IMPLEMENTATION_MASK
) == MONO_IMPLEMENTATION_ASSEMBLYREF
) {
6491 guint32 assembly_idx
;
6493 assembly_idx
= impl
>> MONO_IMPLEMENTATION_BITS
;
6495 mono_assembly_load_reference (image
, assembly_idx
- 1);
6496 g_assert (image
->references
[assembly_idx
- 1]);
6497 if (image
->references
[assembly_idx
- 1] == (gpointer
)-1)
6500 /* FIXME: Cycle detection */
6501 return mono_class_from_name (image
->references
[assembly_idx
- 1]->image
, name_space
, name
);
6503 g_error ("not yet implemented");
6507 token
= MONO_TOKEN_TYPE_DEF
| token
;
6509 class = mono_class_get (image
, token
);
6511 return return_nested_in (class, nested
);
6515 /*FIXME test for interfaces with variant generic arguments*/
6517 mono_class_is_subclass_of (MonoClass
*klass
, MonoClass
*klassc
,
6518 gboolean check_interfaces
)
6520 g_assert (klassc
->idepth
> 0);
6521 if (check_interfaces
&& MONO_CLASS_IS_INTERFACE (klassc
) && !MONO_CLASS_IS_INTERFACE (klass
)) {
6522 if (MONO_CLASS_IMPLEMENTS_INTERFACE (klass
, klassc
->interface_id
))
6524 } else if (check_interfaces
&& MONO_CLASS_IS_INTERFACE (klassc
) && MONO_CLASS_IS_INTERFACE (klass
)) {
6527 for (i
= 0; i
< klass
->interface_count
; i
++) {
6528 MonoClass
*ic
= klass
->interfaces
[i
];
6533 if (!MONO_CLASS_IS_INTERFACE (klass
) && mono_class_has_parent (klass
, klassc
))
6538 * MS.NET thinks interfaces are a subclass of Object, so we think it as
6541 if (klassc
== mono_defaults
.object_class
)
6548 mono_class_has_variant_generic_params (MonoClass
*klass
)
6551 MonoGenericContainer
*container
;
6553 if (!klass
->generic_class
)
6556 container
= klass
->generic_class
->container_class
->generic_container
;
6558 for (i
= 0; i
< container
->type_argc
; ++i
)
6559 if (mono_generic_container_get_param_info (container
, i
)->flags
& (MONO_GEN_PARAM_VARIANT
|MONO_GEN_PARAM_COVARIANT
))
6566 * @container the generic container from the GTD
6567 * @klass: the class to be assigned to
6568 * @oklass: the source class
6570 * Both klass and oklass must be instances of the same generic interface.
6571 * Return true if @klass can be assigned to a @klass variable
6574 mono_class_is_variant_compatible (MonoClass
*klass
, MonoClass
*oklass
)
6577 MonoType
**klass_argv
, **oklass_argv
;
6578 MonoClass
*klass_gtd
= mono_class_get_generic_type_definition (klass
);
6579 MonoGenericContainer
*container
= klass_gtd
->generic_container
;
6581 /*Viable candidates are instances of the same generic interface*/
6582 if (mono_class_get_generic_type_definition (oklass
) != klass_gtd
)
6585 klass_argv
= &klass
->generic_class
->context
.class_inst
->type_argv
[0];
6586 oklass_argv
= &oklass
->generic_class
->context
.class_inst
->type_argv
[0];
6588 for (j
= 0; j
< container
->type_argc
; ++j
) {
6589 MonoClass
*param1_class
= mono_class_from_mono_type (klass_argv
[j
]);
6590 MonoClass
*param2_class
= mono_class_from_mono_type (oklass_argv
[j
]);
6592 if (param1_class
->valuetype
!= param2_class
->valuetype
)
6596 * The _VARIANT and _COVARIANT constants should read _COVARIANT and
6597 * _CONTRAVARIANT, but they are in a public header so we can't fix it.
6599 if (param1_class
!= param2_class
) {
6600 if (mono_generic_container_get_param_info (container
, j
)->flags
& MONO_GEN_PARAM_VARIANT
) {
6601 if (!mono_class_is_assignable_from (param1_class
, param2_class
))
6603 } else if (mono_generic_container_get_param_info (container
, j
)->flags
& MONO_GEN_PARAM_COVARIANT
) {
6604 if (!mono_class_is_assignable_from (param2_class
, param1_class
))
6614 * mono_class_is_assignable_from:
6615 * @klass: the class to be assigned to
6616 * @oklass: the source class
6618 * Return: true if an instance of object oklass can be assigned to an
6619 * instance of object @klass
6622 mono_class_is_assignable_from (MonoClass
*klass
, MonoClass
*oklass
)
6625 mono_class_init (klass
);
6627 if (!oklass
->inited
)
6628 mono_class_init (oklass
);
6630 if ((klass
->byval_arg
.type
== MONO_TYPE_VAR
) || (klass
->byval_arg
.type
== MONO_TYPE_MVAR
))
6631 return klass
== oklass
;
6633 if (MONO_CLASS_IS_INTERFACE (klass
)) {
6634 if ((oklass
->byval_arg
.type
== MONO_TYPE_VAR
) || (oklass
->byval_arg
.type
== MONO_TYPE_MVAR
))
6637 /* interface_offsets might not be set for dynamic classes */
6638 if (oklass
->reflection_info
&& !oklass
->interface_bitmap
)
6640 * oklass might be a generic type parameter but they have
6641 * interface_offsets set.
6643 return mono_reflection_call_is_assignable_to (oklass
, klass
);
6644 if (!oklass
->interface_bitmap
)
6645 /* Happens with generic instances of not-yet created dynamic types */
6647 if (MONO_CLASS_IMPLEMENTS_INTERFACE (oklass
, klass
->interface_id
))
6650 if (mono_class_has_variant_generic_params (klass
)) {
6652 mono_class_setup_interfaces (oklass
);
6654 /*klass is a generic variant interface, We need to extract from oklass a list of ifaces which are viable candidates.*/
6655 for (i
= 0; i
< oklass
->interface_offsets_count
; ++i
) {
6656 MonoClass
*iface
= oklass
->interfaces_packed
[i
];
6658 if (mono_class_is_variant_compatible (klass
, iface
))
6663 } else if (klass
->delegate
) {
6664 if (mono_class_has_variant_generic_params (klass
) && mono_class_is_variant_compatible (klass
, oklass
))
6666 }else if (klass
->rank
) {
6667 MonoClass
*eclass
, *eoclass
;
6669 if (oklass
->rank
!= klass
->rank
)
6672 /* vectors vs. one dimensional arrays */
6673 if (oklass
->byval_arg
.type
!= klass
->byval_arg
.type
)
6676 eclass
= klass
->cast_class
;
6677 eoclass
= oklass
->cast_class
;
6680 * a is b does not imply a[] is b[] when a is a valuetype, and
6681 * b is a reference type.
6684 if (eoclass
->valuetype
) {
6685 if ((eclass
== mono_defaults
.enum_class
) ||
6686 (eclass
== mono_defaults
.enum_class
->parent
) ||
6687 (eclass
== mono_defaults
.object_class
))
6691 return mono_class_is_assignable_from (klass
->cast_class
, oklass
->cast_class
);
6692 } else if (mono_class_is_nullable (klass
)) {
6693 if (mono_class_is_nullable (oklass
))
6694 return mono_class_is_assignable_from (klass
->cast_class
, oklass
->cast_class
);
6696 return mono_class_is_assignable_from (klass
->cast_class
, oklass
);
6697 } else if (klass
== mono_defaults
.object_class
)
6700 return mono_class_has_parent (oklass
, klass
);
6703 /*Check if @oklass is variant compatible with @klass.*/
6705 mono_class_is_variant_compatible_slow (MonoClass
*klass
, MonoClass
*oklass
)
6708 MonoType
**klass_argv
, **oklass_argv
;
6709 MonoClass
*klass_gtd
= mono_class_get_generic_type_definition (klass
);
6710 MonoGenericContainer
*container
= klass_gtd
->generic_container
;
6712 /*Viable candidates are instances of the same generic interface*/
6713 if (mono_class_get_generic_type_definition (oklass
) != klass_gtd
)
6716 klass_argv
= &klass
->generic_class
->context
.class_inst
->type_argv
[0];
6717 oklass_argv
= &oklass
->generic_class
->context
.class_inst
->type_argv
[0];
6719 for (j
= 0; j
< container
->type_argc
; ++j
) {
6720 MonoClass
*param1_class
= mono_class_from_mono_type (klass_argv
[j
]);
6721 MonoClass
*param2_class
= mono_class_from_mono_type (oklass_argv
[j
]);
6723 if (param1_class
->valuetype
!= param2_class
->valuetype
)
6727 * The _VARIANT and _COVARIANT constants should read _COVARIANT and
6728 * _CONTRAVARIANT, but they are in a public header so we can't fix it.
6730 if (param1_class
!= param2_class
) {
6731 if (mono_generic_container_get_param_info (container
, j
)->flags
& MONO_GEN_PARAM_VARIANT
) {
6732 if (!mono_class_is_assignable_from_slow (param1_class
, param2_class
))
6734 } else if (mono_generic_container_get_param_info (container
, j
)->flags
& MONO_GEN_PARAM_COVARIANT
) {
6735 if (!mono_class_is_assignable_from_slow (param2_class
, param1_class
))
6743 /*Check if @candidate implements the interface @target*/
6745 mono_class_implement_interface_slow (MonoClass
*target
, MonoClass
*candidate
)
6748 gboolean is_variant
= mono_class_has_variant_generic_params (target
);
6750 if (is_variant
&& MONO_CLASS_IS_INTERFACE (candidate
)) {
6751 if (mono_class_is_variant_compatible_slow (target
, candidate
))
6756 if (candidate
== target
)
6759 /*A TypeBuilder can have more interfaces on tb->interfaces than on candidate->interfaces*/
6760 if (candidate
->image
->dynamic
&& !candidate
->wastypebuilder
) {
6761 MonoReflectionTypeBuilder
*tb
= candidate
->reflection_info
;
6763 if (tb
->interfaces
) {
6764 for (j
= mono_array_length (tb
->interfaces
) - 1; j
>= 0; --j
) {
6765 MonoReflectionType
*iface
= mono_array_get (tb
->interfaces
, MonoReflectionType
*, j
);
6766 MonoClass
*iface_class
= mono_class_from_mono_type (iface
->type
);
6767 if (iface_class
== target
)
6769 if (is_variant
&& mono_class_is_variant_compatible_slow (target
, iface_class
))
6771 if (mono_class_implement_interface_slow (target
, iface_class
))
6776 /*setup_interfaces don't mono_class_init anything*/
6777 mono_class_setup_interfaces (candidate
);
6778 for (i
= 0; i
< candidate
->interface_count
; ++i
) {
6779 if (candidate
->interfaces
[i
] == target
)
6782 if (is_variant
&& mono_class_is_variant_compatible_slow (target
, candidate
->interfaces
[i
]))
6785 if (mono_class_implement_interface_slow (target
, candidate
->interfaces
[i
]))
6789 candidate
= candidate
->parent
;
6790 } while (candidate
);
6796 * Check if @oklass can be assigned to @klass.
6797 * This function does the same as mono_class_is_assignable_from but is safe to be used from mono_class_init context.
6800 mono_class_is_assignable_from_slow (MonoClass
*target
, MonoClass
*candidate
)
6802 if (candidate
== target
)
6804 if (target
== mono_defaults
.object_class
)
6807 /*setup_supertypes don't mono_class_init anything */
6808 mono_class_setup_supertypes (candidate
);
6809 mono_class_setup_supertypes (target
);
6811 if (mono_class_has_parent (candidate
, target
))
6814 /*If target is not an interface there is no need to check them.*/
6815 if (MONO_CLASS_IS_INTERFACE (target
))
6816 return mono_class_implement_interface_slow (target
, candidate
);
6818 if (target
->delegate
&& mono_class_has_variant_generic_params (target
))
6819 return mono_class_is_variant_compatible (target
, candidate
);
6821 /*FIXME properly handle nullables and arrays */
6827 * mono_class_get_cctor:
6828 * @klass: A MonoClass pointer
6830 * Returns: the static constructor of @klass if it exists, NULL otherwise.
6833 mono_class_get_cctor (MonoClass
*klass
)
6835 MonoCachedClassInfo cached_info
;
6837 if (!klass
->has_cctor
)
6840 if (mono_class_get_cached_class_info (klass
, &cached_info
))
6841 return mono_get_method (klass
->image
, cached_info
.cctor_token
, klass
);
6843 if (klass
->generic_class
&& !klass
->methods
)
6844 return mono_class_get_inflated_method (klass
, mono_class_get_cctor (klass
->generic_class
->container_class
));
6846 return mono_class_get_method_from_name_flags (klass
, ".cctor", -1, METHOD_ATTRIBUTE_SPECIAL_NAME
);
6850 * mono_class_get_finalizer:
6851 * @klass: The MonoClass pointer
6853 * Returns: the finalizer method of @klass if it exists, NULL otherwise.
6856 mono_class_get_finalizer (MonoClass
*klass
)
6858 MonoCachedClassInfo cached_info
;
6861 mono_class_init (klass
);
6862 if (!klass
->has_finalize
)
6865 if (mono_class_get_cached_class_info (klass
, &cached_info
))
6866 return mono_get_method (cached_info
.finalize_image
, cached_info
.finalize_token
, NULL
);
6868 mono_class_setup_vtable (klass
);
6869 return klass
->vtable
[finalize_slot
];
6874 * mono_class_needs_cctor_run:
6875 * @klass: the MonoClass pointer
6876 * @caller: a MonoMethod describing the caller
6878 * Determines whenever the class has a static constructor and whenever it
6879 * needs to be called when executing CALLER.
6882 mono_class_needs_cctor_run (MonoClass
*klass
, MonoMethod
*caller
)
6886 method
= mono_class_get_cctor (klass
);
6888 return (method
== caller
) ? FALSE
: TRUE
;
6894 * mono_class_array_element_size:
6897 * Returns: the number of bytes an element of type @klass
6898 * uses when stored into an array.
6901 mono_class_array_element_size (MonoClass
*klass
)
6903 MonoType
*type
= &klass
->byval_arg
;
6906 switch (type
->type
) {
6909 case MONO_TYPE_BOOLEAN
:
6913 case MONO_TYPE_CHAR
:
6922 case MONO_TYPE_CLASS
:
6923 case MONO_TYPE_STRING
:
6924 case MONO_TYPE_OBJECT
:
6925 case MONO_TYPE_SZARRAY
:
6926 case MONO_TYPE_ARRAY
:
6928 case MONO_TYPE_MVAR
:
6929 return sizeof (gpointer
);
6934 case MONO_TYPE_VALUETYPE
:
6935 if (type
->data
.klass
->enumtype
) {
6936 type
= mono_class_enum_basetype (type
->data
.klass
);
6937 klass
= klass
->element_class
;
6940 return mono_class_instance_size (klass
) - sizeof (MonoObject
);
6941 case MONO_TYPE_GENERICINST
:
6942 type
= &type
->data
.generic_class
->container_class
->byval_arg
;
6945 case MONO_TYPE_VOID
:
6949 g_error ("unknown type 0x%02x in mono_class_array_element_size", type
->type
);
6955 * mono_array_element_size:
6956 * @ac: pointer to a #MonoArrayClass
6958 * Returns: the size of single array element.
6961 mono_array_element_size (MonoClass
*ac
)
6963 g_assert (ac
->rank
);
6964 return ac
->sizes
.element_size
;
6968 mono_ldtoken (MonoImage
*image
, guint32 token
, MonoClass
**handle_class
,
6969 MonoGenericContext
*context
)
6971 if (image
->dynamic
) {
6972 MonoClass
*tmp_handle_class
;
6973 gpointer obj
= mono_lookup_dynamic_token_class (image
, token
, TRUE
, &tmp_handle_class
, context
);
6975 g_assert (tmp_handle_class
);
6977 *handle_class
= tmp_handle_class
;
6979 if (tmp_handle_class
== mono_defaults
.typehandle_class
)
6980 return &((MonoClass
*)obj
)->byval_arg
;
6985 switch (token
& 0xff000000) {
6986 case MONO_TOKEN_TYPE_DEF
:
6987 case MONO_TOKEN_TYPE_REF
:
6988 case MONO_TOKEN_TYPE_SPEC
: {
6991 *handle_class
= mono_defaults
.typehandle_class
;
6992 type
= mono_type_get_full (image
, token
, context
);
6995 mono_class_init (mono_class_from_mono_type (type
));
6996 /* We return a MonoType* as handle */
6999 case MONO_TOKEN_FIELD_DEF
: {
7001 guint32 type
= mono_metadata_typedef_from_field (image
, mono_metadata_token_index (token
));
7005 *handle_class
= mono_defaults
.fieldhandle_class
;
7006 class = mono_class_get_full (image
, MONO_TOKEN_TYPE_DEF
| type
, context
);
7009 mono_class_init (class);
7010 return mono_class_get_field (class, token
);
7012 case MONO_TOKEN_METHOD_DEF
:
7013 case MONO_TOKEN_METHOD_SPEC
: {
7015 meth
= mono_get_method_full (image
, token
, NULL
, context
);
7017 *handle_class
= mono_defaults
.methodhandle_class
;
7020 case MONO_TOKEN_MEMBER_REF
: {
7021 guint32 cols
[MONO_MEMBERREF_SIZE
];
7023 mono_metadata_decode_row (&image
->tables
[MONO_TABLE_MEMBERREF
], mono_metadata_token_index (token
) - 1, cols
, MONO_MEMBERREF_SIZE
);
7024 sig
= mono_metadata_blob_heap (image
, cols
[MONO_MEMBERREF_SIGNATURE
]);
7025 mono_metadata_decode_blob_size (sig
, &sig
);
7026 if (*sig
== 0x6) { /* it's a field */
7028 MonoClassField
*field
;
7029 field
= mono_field_from_token (image
, token
, &klass
, context
);
7031 *handle_class
= mono_defaults
.fieldhandle_class
;
7035 meth
= mono_get_method_full (image
, token
, NULL
, context
);
7037 *handle_class
= mono_defaults
.methodhandle_class
;
7042 g_warning ("Unknown token 0x%08x in ldtoken", token
);
7049 * This function might need to call runtime functions so it can't be part
7050 * of the metadata library.
7052 static MonoLookupDynamicToken lookup_dynamic
= NULL
;
7055 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func
)
7057 lookup_dynamic
= func
;
7061 mono_lookup_dynamic_token (MonoImage
*image
, guint32 token
, MonoGenericContext
*context
)
7063 MonoClass
*handle_class
;
7065 return lookup_dynamic (image
, token
, TRUE
, &handle_class
, context
);
7069 mono_lookup_dynamic_token_class (MonoImage
*image
, guint32 token
, gboolean valid_token
, MonoClass
**handle_class
, MonoGenericContext
*context
)
7071 return lookup_dynamic (image
, token
, valid_token
, handle_class
, context
);
7074 static MonoGetCachedClassInfo get_cached_class_info
= NULL
;
7077 mono_install_get_cached_class_info (MonoGetCachedClassInfo func
)
7079 get_cached_class_info
= func
;
7083 mono_class_get_cached_class_info (MonoClass
*klass
, MonoCachedClassInfo
*res
)
7085 if (!get_cached_class_info
)
7088 return get_cached_class_info (klass
, res
);
7092 mono_install_get_class_from_name (MonoGetClassFromName func
)
7094 get_class_from_name
= func
;
7098 mono_class_get_image (MonoClass
*klass
)
7100 return klass
->image
;
7104 * mono_class_get_element_class:
7105 * @klass: the MonoClass to act on
7107 * Returns: the element class of an array or an enumeration.
7110 mono_class_get_element_class (MonoClass
*klass
)
7112 return klass
->element_class
;
7116 * mono_class_is_valuetype:
7117 * @klass: the MonoClass to act on
7119 * Returns: true if the MonoClass represents a ValueType.
7122 mono_class_is_valuetype (MonoClass
*klass
)
7124 return klass
->valuetype
;
7128 * mono_class_is_enum:
7129 * @klass: the MonoClass to act on
7131 * Returns: true if the MonoClass represents an enumeration.
7134 mono_class_is_enum (MonoClass
*klass
)
7136 return klass
->enumtype
;
7140 * mono_class_enum_basetype:
7141 * @klass: the MonoClass to act on
7143 * Returns: the underlying type representation for an enumeration.
7146 mono_class_enum_basetype (MonoClass
*klass
)
7148 if (klass
->element_class
== klass
)
7149 /* SRE or broken types */
7152 return &klass
->element_class
->byval_arg
;
7156 * mono_class_get_parent
7157 * @klass: the MonoClass to act on
7159 * Returns: the parent class for this class.
7162 mono_class_get_parent (MonoClass
*klass
)
7164 return klass
->parent
;
7168 * mono_class_get_nesting_type;
7169 * @klass: the MonoClass to act on
7171 * Returns: the container type where this type is nested or NULL if this type is not a nested type.
7174 mono_class_get_nesting_type (MonoClass
*klass
)
7176 return klass
->nested_in
;
7180 * mono_class_get_rank:
7181 * @klass: the MonoClass to act on
7183 * Returns: the rank for the array (the number of dimensions).
7186 mono_class_get_rank (MonoClass
*klass
)
7192 * mono_class_get_flags:
7193 * @klass: the MonoClass to act on
7195 * The type flags from the TypeDef table from the metadata.
7196 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
7199 * Returns: the flags from the TypeDef table.
7202 mono_class_get_flags (MonoClass
*klass
)
7204 return klass
->flags
;
7208 * mono_class_get_name
7209 * @klass: the MonoClass to act on
7211 * Returns: the name of the class.
7214 mono_class_get_name (MonoClass
*klass
)
7220 * mono_class_get_namespace:
7221 * @klass: the MonoClass to act on
7223 * Returns: the namespace of the class.
7226 mono_class_get_namespace (MonoClass
*klass
)
7228 return klass
->name_space
;
7232 * mono_class_get_type:
7233 * @klass: the MonoClass to act on
7235 * This method returns the internal Type representation for the class.
7237 * Returns: the MonoType from the class.
7240 mono_class_get_type (MonoClass
*klass
)
7242 return &klass
->byval_arg
;
7246 * mono_class_get_type_token
7247 * @klass: the MonoClass to act on
7249 * This method returns type token for the class.
7251 * Returns: the type token for the class.
7254 mono_class_get_type_token (MonoClass
*klass
)
7256 return klass
->type_token
;
7260 * mono_class_get_byref_type:
7261 * @klass: the MonoClass to act on
7266 mono_class_get_byref_type (MonoClass
*klass
)
7268 return &klass
->this_arg
;
7272 * mono_class_num_fields:
7273 * @klass: the MonoClass to act on
7275 * Returns: the number of static and instance fields in the class.
7278 mono_class_num_fields (MonoClass
*klass
)
7280 return klass
->field
.count
;
7284 * mono_class_num_methods:
7285 * @klass: the MonoClass to act on
7287 * Returns: the number of methods in the class.
7290 mono_class_num_methods (MonoClass
*klass
)
7292 return klass
->method
.count
;
7296 * mono_class_num_properties
7297 * @klass: the MonoClass to act on
7299 * Returns: the number of properties in the class.
7302 mono_class_num_properties (MonoClass
*klass
)
7304 mono_class_setup_properties (klass
);
7306 return klass
->ext
->property
.count
;
7310 * mono_class_num_events:
7311 * @klass: the MonoClass to act on
7313 * Returns: the number of events in the class.
7316 mono_class_num_events (MonoClass
*klass
)
7318 mono_class_setup_events (klass
);
7320 return klass
->ext
->event
.count
;
7324 * mono_class_get_fields:
7325 * @klass: the MonoClass to act on
7327 * This routine is an iterator routine for retrieving the fields in a class.
7329 * You must pass a gpointer that points to zero and is treated as an opaque handle to
7330 * iterate over all of the elements. When no more values are
7331 * available, the return value is NULL.
7333 * Returns: a @MonoClassField* on each iteration, or NULL when no more fields are available.
7336 mono_class_get_fields (MonoClass
* klass
, gpointer
*iter
)
7338 MonoClassField
* field
;
7342 mono_class_setup_fields_locking (klass
);
7343 if (klass
->exception_type
)
7345 /* start from the first */
7346 if (klass
->field
.count
) {
7347 return *iter
= &klass
->fields
[0];
7355 if (field
< &klass
->fields
[klass
->field
.count
]) {
7356 return *iter
= field
;
7362 * mono_class_get_methods
7363 * @klass: the MonoClass to act on
7365 * This routine is an iterator routine for retrieving the fields in a class.
7367 * You must pass a gpointer that points to zero and is treated as an opaque handle to
7368 * iterate over all of the elements. When no more values are
7369 * available, the return value is NULL.
7371 * Returns: a MonoMethod on each iteration or NULL when no more methods are available.
7374 mono_class_get_methods (MonoClass
* klass
, gpointer
*iter
)
7376 MonoMethod
** method
;
7380 mono_class_init (klass
);
7382 mono_class_setup_methods (klass
);
7385 * We can't fail lookup of methods otherwise the runtime will burst in flames on all sort of places.
7386 * FIXME we should better report this error to the caller
7388 if (!klass
->methods
)
7390 /* start from the first */
7391 if (klass
->method
.count
) {
7392 *iter
= &klass
->methods
[0];
7393 return klass
->methods
[0];
7401 if (method
< &klass
->methods
[klass
->method
.count
]) {
7409 * mono_class_get_virtual_methods:
7411 * Iterate over the virtual methods of KLASS.
7413 * LOCKING: Assumes the loader lock is held (because of the klass->methods check).
7416 mono_class_get_virtual_methods (MonoClass
* klass
, gpointer
*iter
)
7418 MonoMethod
** method
;
7421 if (klass
->methods
|| !MONO_CLASS_HAS_STATIC_METADATA (klass
) || mono_debug_using_mono_debugger ()) {
7423 mono_class_setup_methods (klass
);
7425 * We can't fail lookup of methods otherwise the runtime will burst in flames on all sort of places.
7426 * FIXME we should better report this error to the caller
7428 if (!klass
->methods
)
7430 /* start from the first */
7431 method
= &klass
->methods
[0];
7436 while (method
< &klass
->methods
[klass
->method
.count
]) {
7437 if (((*method
)->flags
& METHOD_ATTRIBUTE_VIRTUAL
))
7441 if (method
< &klass
->methods
[klass
->method
.count
]) {
7448 /* Search directly in metadata to avoid calling setup_methods () */
7449 MonoMethod
*res
= NULL
;
7455 start_index
= GPOINTER_TO_UINT (*iter
);
7458 for (i
= start_index
; i
< klass
->method
.count
; ++i
) {
7461 /* class->method.first points into the methodptr table */
7462 flags
= mono_metadata_decode_table_row_col (klass
->image
, MONO_TABLE_METHOD
, klass
->method
.first
+ i
, MONO_METHOD_FLAGS
);
7464 if (flags
& METHOD_ATTRIBUTE_VIRTUAL
)
7468 if (i
< klass
->method
.count
) {
7469 res
= mono_get_method (klass
->image
, MONO_TOKEN_METHOD_DEF
| (klass
->method
.first
+ i
+ 1), klass
);
7470 /* Add 1 here so the if (*iter) check fails */
7471 *iter
= GUINT_TO_POINTER (i
+ 1);
7480 * mono_class_get_properties:
7481 * @klass: the MonoClass to act on
7483 * This routine is an iterator routine for retrieving the properties in a class.
7485 * You must pass a gpointer that points to zero and is treated as an opaque handle to
7486 * iterate over all of the elements. When no more values are
7487 * available, the return value is NULL.
7489 * Returns: a @MonoProperty* on each invocation, or NULL when no more are available.
7492 mono_class_get_properties (MonoClass
* klass
, gpointer
*iter
)
7494 MonoProperty
* property
;
7498 mono_class_init (klass
);
7500 mono_class_setup_properties (klass
);
7501 /* start from the first */
7502 if (klass
->ext
->property
.count
) {
7503 return *iter
= &klass
->ext
->properties
[0];
7511 if (property
< &klass
->ext
->properties
[klass
->ext
->property
.count
]) {
7512 return *iter
= property
;
7518 * mono_class_get_events:
7519 * @klass: the MonoClass to act on
7521 * This routine is an iterator routine for retrieving the properties in a class.
7523 * You must pass a gpointer that points to zero and is treated as an opaque handle to
7524 * iterate over all of the elements. When no more values are
7525 * available, the return value is NULL.
7527 * Returns: a @MonoEvent* on each invocation, or NULL when no more are available.
7530 mono_class_get_events (MonoClass
* klass
, gpointer
*iter
)
7536 mono_class_init (klass
);
7538 mono_class_setup_events (klass
);
7539 /* start from the first */
7540 if (klass
->ext
->event
.count
) {
7541 return *iter
= &klass
->ext
->events
[0];
7549 if (event
< &klass
->ext
->events
[klass
->ext
->event
.count
]) {
7550 return *iter
= event
;
7556 * mono_class_get_interfaces
7557 * @klass: the MonoClass to act on
7559 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
7561 * You must pass a gpointer that points to zero and is treated as an opaque handle to
7562 * iterate over all of the elements. When no more values are
7563 * available, the return value is NULL.
7565 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
7568 mono_class_get_interfaces (MonoClass
* klass
, gpointer
*iter
)
7575 mono_class_init (klass
);
7576 if (!klass
->interfaces_inited
)
7577 mono_class_setup_interfaces (klass
);
7578 /* start from the first */
7579 if (klass
->interface_count
) {
7580 *iter
= &klass
->interfaces
[0];
7581 return klass
->interfaces
[0];
7589 if (iface
< &klass
->interfaces
[klass
->interface_count
]) {
7597 * mono_class_get_nested_types
7598 * @klass: the MonoClass to act on
7600 * This routine is an iterator routine for retrieving the nested types of a class.
7601 * This works only if @klass is non-generic, or a generic type definition.
7603 * You must pass a gpointer that points to zero and is treated as an opaque handle to
7604 * iterate over all of the elements. When no more values are
7605 * available, the return value is NULL.
7607 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
7610 mono_class_get_nested_types (MonoClass
* klass
, gpointer
*iter
)
7618 mono_class_init (klass
);
7619 if (!klass
->nested_classes_inited
) {
7620 if (!klass
->type_token
)
7621 klass
->nested_classes_inited
= TRUE
;
7622 mono_loader_lock ();
7623 if (!klass
->nested_classes_inited
) {
7624 i
= mono_metadata_nesting_typedef (klass
->image
, klass
->type_token
, 1);
7627 guint32 cols
[MONO_NESTED_CLASS_SIZE
];
7628 mono_metadata_decode_row (&klass
->image
->tables
[MONO_TABLE_NESTEDCLASS
], i
- 1, cols
, MONO_NESTED_CLASS_SIZE
);
7629 nclass
= mono_class_create_from_typedef (klass
->image
, MONO_TOKEN_TYPE_DEF
| cols
[MONO_NESTED_CLASS_NESTED
]);
7632 mono_class_alloc_ext (klass
);
7633 klass
->ext
->nested_classes
= g_list_prepend_image (klass
->image
, klass
->ext
->nested_classes
, nclass
);
7635 i
= mono_metadata_nesting_typedef (klass
->image
, klass
->type_token
, i
+ 1);
7638 mono_memory_barrier ();
7639 klass
->nested_classes_inited
= TRUE
;
7640 mono_loader_unlock ();
7644 /* start from the first */
7645 if (klass
->ext
&& klass
->ext
->nested_classes
) {
7646 *iter
= klass
->ext
->nested_classes
;
7647 return klass
->ext
->nested_classes
->data
;
7649 /* no nested types */
7663 * mono_field_get_name:
7664 * @field: the MonoClassField to act on
7666 * Returns: the name of the field.
7669 mono_field_get_name (MonoClassField
*field
)
7675 * mono_field_get_type:
7676 * @field: the MonoClassField to act on
7678 * Returns: MonoType of the field.
7681 mono_field_get_type (MonoClassField
*field
)
7687 * mono_field_get_parent:
7688 * @field: the MonoClassField to act on
7690 * Returns: MonoClass where the field was defined.
7693 mono_field_get_parent (MonoClassField
*field
)
7695 return field
->parent
;
7699 * mono_field_get_flags;
7700 * @field: the MonoClassField to act on
7702 * The metadata flags for a field are encoded using the
7703 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
7705 * Returns: the flags for the field.
7708 mono_field_get_flags (MonoClassField
*field
)
7710 return field
->type
->attrs
;
7714 * mono_field_get_offset;
7715 * @field: the MonoClassField to act on
7717 * Returns: the field offset.
7720 mono_field_get_offset (MonoClassField
*field
)
7722 return field
->offset
;
7726 mono_field_get_rva (MonoClassField
*field
)
7730 MonoClass
*klass
= field
->parent
;
7732 g_assert (field
->type
->attrs
& FIELD_ATTRIBUTE_HAS_FIELD_RVA
);
7734 if (!klass
->ext
|| !klass
->ext
->field_def_values
) {
7735 mono_loader_lock ();
7736 mono_class_alloc_ext (klass
);
7737 if (!klass
->ext
->field_def_values
)
7738 klass
->ext
->field_def_values
= mono_image_alloc0 (klass
->image
, sizeof (MonoFieldDefaultValue
) * klass
->field
.count
);
7739 mono_loader_unlock ();
7742 field_index
= mono_field_get_index (field
);
7744 if (!klass
->ext
->field_def_values
[field_index
].data
&& !klass
->image
->dynamic
) {
7745 mono_metadata_field_info (field
->parent
->image
, klass
->field
.first
+ field_index
, NULL
, &rva
, NULL
);
7747 g_warning ("field %s in %s should have RVA data, but hasn't", mono_field_get_name (field
), field
->parent
->name
);
7748 klass
->ext
->field_def_values
[field_index
].data
= mono_image_rva_map (field
->parent
->image
, rva
);
7751 return klass
->ext
->field_def_values
[field_index
].data
;
7755 * mono_field_get_data;
7756 * @field: the MonoClassField to act on
7758 * Returns: pointer to the metadata constant value or to the field
7759 * data if it has an RVA flag.
7762 mono_field_get_data (MonoClassField
*field
)
7764 if (field
->type
->attrs
& FIELD_ATTRIBUTE_HAS_DEFAULT
) {
7765 MonoTypeEnum def_type
;
7767 return mono_class_get_field_default_value (field
, &def_type
);
7768 } else if (field
->type
->attrs
& FIELD_ATTRIBUTE_HAS_FIELD_RVA
) {
7769 return mono_field_get_rva (field
);
7776 * mono_property_get_name:
7777 * @prop: the MonoProperty to act on
7779 * Returns: the name of the property
7782 mono_property_get_name (MonoProperty
*prop
)
7788 * mono_property_get_set_method
7789 * @prop: the MonoProperty to act on.
7791 * Returns: the setter method of the property (A MonoMethod)
7794 mono_property_get_set_method (MonoProperty
*prop
)
7800 * mono_property_get_get_method
7801 * @prop: the MonoProperty to act on.
7803 * Returns: the setter method of the property (A MonoMethod)
7806 mono_property_get_get_method (MonoProperty
*prop
)
7812 * mono_property_get_parent:
7813 * @prop: the MonoProperty to act on.
7815 * Returns: the MonoClass where the property was defined.
7818 mono_property_get_parent (MonoProperty
*prop
)
7820 return prop
->parent
;
7824 * mono_property_get_flags:
7825 * @prop: the MonoProperty to act on.
7827 * The metadata flags for a property are encoded using the
7828 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
7830 * Returns: the flags for the property.
7833 mono_property_get_flags (MonoProperty
*prop
)
7839 * mono_event_get_name:
7840 * @event: the MonoEvent to act on
7842 * Returns: the name of the event.
7845 mono_event_get_name (MonoEvent
*event
)
7851 * mono_event_get_add_method:
7852 * @event: The MonoEvent to act on.
7854 * Returns: the @add' method for the event (a MonoMethod).
7857 mono_event_get_add_method (MonoEvent
*event
)
7863 * mono_event_get_remove_method:
7864 * @event: The MonoEvent to act on.
7866 * Returns: the @remove method for the event (a MonoMethod).
7869 mono_event_get_remove_method (MonoEvent
*event
)
7871 return event
->remove
;
7875 * mono_event_get_raise_method:
7876 * @event: The MonoEvent to act on.
7878 * Returns: the @raise method for the event (a MonoMethod).
7881 mono_event_get_raise_method (MonoEvent
*event
)
7883 return event
->raise
;
7887 * mono_event_get_parent:
7888 * @event: the MonoEvent to act on.
7890 * Returns: the MonoClass where the event is defined.
7893 mono_event_get_parent (MonoEvent
*event
)
7895 return event
->parent
;
7899 * mono_event_get_flags
7900 * @event: the MonoEvent to act on.
7902 * The metadata flags for an event are encoded using the
7903 * EVENT_* constants. See the tabledefs.h file for details.
7905 * Returns: the flags for the event.
7908 mono_event_get_flags (MonoEvent
*event
)
7910 return event
->attrs
;
7914 * mono_class_get_method_from_name:
7915 * @klass: where to look for the method
7916 * @name_space: name of the method
7917 * @param_count: number of parameters. -1 for any number.
7919 * Obtains a MonoMethod with a given name and number of parameters.
7920 * It only works if there are no multiple signatures for any given method name.
7923 mono_class_get_method_from_name (MonoClass
*klass
, const char *name
, int param_count
)
7925 return mono_class_get_method_from_name_flags (klass
, name
, param_count
, 0);
7929 find_method_in_metadata (MonoClass
*klass
, const char *name
, int param_count
, int flags
)
7931 MonoMethod
*res
= NULL
;
7934 /* Search directly in the metadata to avoid calling setup_methods () */
7935 for (i
= 0; i
< klass
->method
.count
; ++i
) {
7936 guint32 cols
[MONO_METHOD_SIZE
];
7939 /* class->method.first points into the methodptr table */
7940 mono_metadata_decode_table_row (klass
->image
, MONO_TABLE_METHOD
, klass
->method
.first
+ i
, cols
, MONO_METHOD_SIZE
);
7942 if (!strcmp (mono_metadata_string_heap (klass
->image
, cols
[MONO_METHOD_NAME
]), name
)) {
7943 method
= mono_get_method (klass
->image
, MONO_TOKEN_METHOD_DEF
| (klass
->method
.first
+ i
+ 1), klass
);
7944 if ((param_count
== -1) || mono_method_signature (method
)->param_count
== param_count
) {
7955 * mono_class_get_method_from_name_flags:
7956 * @klass: where to look for the method
7957 * @name_space: name of the method
7958 * @param_count: number of parameters. -1 for any number.
7959 * @flags: flags which must be set in the method
7961 * Obtains a MonoMethod with a given name and number of parameters.
7962 * It only works if there are no multiple signatures for any given method name.
7965 mono_class_get_method_from_name_flags (MonoClass
*klass
, const char *name
, int param_count
, int flags
)
7967 MonoMethod
*res
= NULL
;
7970 mono_class_init (klass
);
7972 if (klass
->generic_class
&& !klass
->methods
) {
7973 res
= mono_class_get_method_from_name_flags (klass
->generic_class
->container_class
, name
, param_count
, flags
);
7975 res
= mono_class_inflate_generic_method_full (res
, klass
, mono_class_get_context (klass
));
7979 if (klass
->methods
|| !MONO_CLASS_HAS_STATIC_METADATA (klass
)) {
7980 mono_class_setup_methods (klass
);
7982 We can't fail lookup of methods otherwise the runtime will burst in flames on all sort of places.
7983 See mono/tests/array_load_exception.il
7984 FIXME we should better report this error to the caller
7986 if (!klass
->methods
)
7988 for (i
= 0; i
< klass
->method
.count
; ++i
) {
7989 MonoMethod
*method
= klass
->methods
[i
];
7991 if (method
->name
[0] == name
[0] &&
7992 !strcmp (name
, method
->name
) &&
7993 (param_count
== -1 || mono_method_signature (method
)->param_count
== param_count
) &&
7994 ((method
->flags
& flags
) == flags
)) {
8001 res
= find_method_in_metadata (klass
, name
, param_count
, flags
);
8008 * mono_class_set_failure:
8009 * @klass: class in which the failure was detected
8010 * @ex_type: the kind of exception/error to be thrown (later)
8011 * @ex_data: exception data (specific to each type of exception/error)
8013 * Keep a detected failure informations in the class for later processing.
8014 * Note that only the first failure is kept.
8016 * LOCKING: Acquires the loader lock.
8019 mono_class_set_failure (MonoClass
*klass
, guint32 ex_type
, void *ex_data
)
8021 if (klass
->exception_type
)
8024 mono_loader_lock ();
8025 klass
->exception_type
= ex_type
;
8027 mono_image_property_insert (klass
->image
, klass
, MONO_CLASS_PROP_EXCEPTION_DATA
, ex_data
);
8028 mono_loader_unlock ();
8034 * mono_class_get_exception_data:
8036 * Return the exception_data property of KLASS.
8038 * LOCKING: Acquires the loader lock.
8041 mono_class_get_exception_data (MonoClass
*klass
)
8043 return mono_image_property_lookup (klass
->image
, klass
, MONO_CLASS_PROP_EXCEPTION_DATA
);
8047 * mono_classes_init:
8049 * Initialize the resources used by this module.
8052 mono_classes_init (void)
8054 mono_counters_register ("Inflated methods size",
8055 MONO_COUNTER_GENERICS
| MONO_COUNTER_INT
, &inflated_methods_size
);
8056 mono_counters_register ("Inflated classes",
8057 MONO_COUNTER_GENERICS
| MONO_COUNTER_INT
, &inflated_classes
);
8058 mono_counters_register ("Inflated classes size",
8059 MONO_COUNTER_GENERICS
| MONO_COUNTER_INT
, &inflated_classes_size
);
8060 mono_counters_register ("MonoClass size",
8061 MONO_COUNTER_METADATA
| MONO_COUNTER_INT
, &classes_size
);
8062 mono_counters_register ("MonoClassExt size",
8063 MONO_COUNTER_METADATA
| MONO_COUNTER_INT
, &class_ext_size
);
8067 * mono_classes_cleanup:
8069 * Free the resources used by this module.
8072 mono_classes_cleanup (void)
8074 if (global_interface_bitset
)
8075 mono_bitset_free (global_interface_bitset
);
8079 * mono_class_get_exception_for_failure:
8080 * @klass: class in which the failure was detected
8082 * Return a constructed MonoException than the caller can then throw
8083 * using mono_raise_exception - or NULL if no failure is present (or
8084 * doesn't result in an exception).
8087 mono_class_get_exception_for_failure (MonoClass
*klass
)
8089 gpointer exception_data
= mono_class_get_exception_data (klass
);
8091 switch (klass
->exception_type
) {
8092 case MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND
: {
8093 MonoDomain
*domain
= mono_domain_get ();
8094 MonoSecurityManager
* secman
= mono_security_manager_get_methods ();
8095 MonoMethod
*method
= exception_data
;
8096 guint32 error
= (method
) ? MONO_METADATA_INHERITANCEDEMAND_METHOD
: MONO_METADATA_INHERITANCEDEMAND_CLASS
;
8097 MonoObject
*exc
= NULL
;
8101 args
[1] = mono_assembly_get_object (domain
, mono_image_get_assembly (klass
->image
));
8102 args
[2] = mono_type_get_object (domain
, &klass
->byval_arg
);
8103 args
[3] = (method
) ? mono_method_get_object (domain
, method
, NULL
) : NULL
;
8105 mono_runtime_invoke (secman
->inheritsecurityexception
, NULL
, args
, &exc
);
8106 return (MonoException
*) exc
;
8108 case MONO_EXCEPTION_TYPE_LOAD
: {
8111 char *str
= mono_type_get_full_name (klass
);
8112 char *astr
= klass
->image
->assembly
? mono_stringify_assembly_name (&klass
->image
->assembly
->aname
): NULL
;
8113 name
= mono_string_new (mono_domain_get (), str
);
8115 ex
= mono_get_exception_type_load (name
, astr
);
8119 case MONO_EXCEPTION_MISSING_METHOD
: {
8120 char *class_name
= exception_data
;
8121 char *assembly_name
= class_name
+ strlen (class_name
) + 1;
8123 return mono_get_exception_missing_method (class_name
, assembly_name
);
8125 case MONO_EXCEPTION_MISSING_FIELD
: {
8126 char *class_name
= exception_data
;
8127 char *member_name
= class_name
+ strlen (class_name
) + 1;
8129 return mono_get_exception_missing_field (class_name
, member_name
);
8131 case MONO_EXCEPTION_FILE_NOT_FOUND
: {
8132 char *msg_format
= exception_data
;
8133 char *assembly_name
= msg_format
+ strlen (msg_format
) + 1;
8134 char *msg
= g_strdup_printf (msg_format
, assembly_name
);
8137 ex
= mono_get_exception_file_not_found2 (msg
, mono_string_new (mono_domain_get (), assembly_name
));
8143 case MONO_EXCEPTION_BAD_IMAGE
: {
8144 return mono_get_exception_bad_image_format (exception_data
);
8147 MonoLoaderError
*error
;
8150 error
= mono_loader_get_last_error ();
8152 ex
= mono_loader_error_prepare_exception (error
);
8156 /* TODO - handle other class related failures */
8163 is_nesting_type (MonoClass
*outer_klass
, MonoClass
*inner_klass
)
8165 outer_klass
= mono_class_get_generic_type_definition (outer_klass
);
8166 inner_klass
= mono_class_get_generic_type_definition (inner_klass
);
8168 if (outer_klass
== inner_klass
)
8170 inner_klass
= inner_klass
->nested_in
;
8171 } while (inner_klass
);
8176 mono_class_get_generic_type_definition (MonoClass
*klass
)
8178 return klass
->generic_class
? klass
->generic_class
->container_class
: klass
;
8182 * Check if @klass is a subtype of @parent ignoring generic instantiations.
8184 * Generic instantiations are ignored for all super types of @klass.
8186 * Visibility checks ignoring generic instantiations.
8189 mono_class_has_parent_and_ignore_generics (MonoClass
*klass
, MonoClass
*parent
)
8192 klass
= mono_class_get_generic_type_definition (klass
);
8193 parent
= mono_class_get_generic_type_definition (parent
);
8195 for (i
= 0; i
< klass
->idepth
; ++i
) {
8196 if (parent
== mono_class_get_generic_type_definition (klass
->supertypes
[i
]))
8202 * Subtype can only access parent members with family protection if the site object
8203 * is subclass of Subtype. For example:
8204 * class A { protected int x; }
8206 * void valid_access () {
8210 * void invalid_access () {
8217 is_valid_family_access (MonoClass
*access_klass
, MonoClass
*member_klass
, MonoClass
*context_klass
)
8219 if (!mono_class_has_parent_and_ignore_generics (access_klass
, member_klass
))
8222 if (context_klass
== NULL
)
8224 /*if access_klass is not member_klass context_klass must be type compat*/
8225 if (access_klass
!= member_klass
&& !mono_class_has_parent_and_ignore_generics (context_klass
, access_klass
))
8231 can_access_internals (MonoAssembly
*accessing
, MonoAssembly
* accessed
)
8234 if (accessing
== accessed
)
8236 if (!accessed
|| !accessing
)
8239 /* extra safety under CoreCLR - the runtime does not verify the strongname signatures
8240 * anywhere so untrusted friends are not safe to access platform's code internals */
8241 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR
) {
8242 if (!mono_security_core_clr_can_access_internals (accessing
->image
, accessed
->image
))
8246 mono_assembly_load_friends (accessed
);
8247 for (tmp
= accessed
->friend_assembly_names
; tmp
; tmp
= tmp
->next
) {
8248 MonoAssemblyName
*friend = tmp
->data
;
8249 /* Be conservative with checks */
8252 if (strcmp (accessing
->aname
.name
, friend->name
))
8254 if (friend->public_key_token
[0]) {
8255 if (!accessing
->aname
.public_key_token
[0])
8257 if (!mono_public_tokens_are_equal (friend->public_key_token
, accessing
->aname
.public_key_token
))
8266 * If klass is a generic type or if it is derived from a generic type, return the
8267 * MonoClass of the generic definition
8268 * Returns NULL if not found
8271 get_generic_definition_class (MonoClass
*klass
)
8274 if (klass
->generic_class
&& klass
->generic_class
->container_class
)
8275 return klass
->generic_class
->container_class
;
8276 klass
= klass
->parent
;
8282 can_access_instantiation (MonoClass
*access_klass
, MonoGenericInst
*ginst
)
8285 for (i
= 0; i
< ginst
->type_argc
; ++i
) {
8286 MonoType
*type
= ginst
->type_argv
[i
];
8287 switch (type
->type
) {
8288 case MONO_TYPE_SZARRAY
:
8289 if (!can_access_type (access_klass
, type
->data
.klass
))
8292 case MONO_TYPE_ARRAY
:
8293 if (!can_access_type (access_klass
, type
->data
.array
->eklass
))
8297 if (!can_access_type (access_klass
, mono_class_from_mono_type (type
->data
.type
)))
8300 case MONO_TYPE_CLASS
:
8301 case MONO_TYPE_VALUETYPE
:
8302 case MONO_TYPE_GENERICINST
:
8303 if (!can_access_type (access_klass
, mono_class_from_mono_type (type
)))
8311 can_access_type (MonoClass
*access_klass
, MonoClass
*member_klass
)
8315 if (access_klass
->element_class
&& !access_klass
->enumtype
)
8316 access_klass
= access_klass
->element_class
;
8318 if (member_klass
->element_class
&& !member_klass
->enumtype
)
8319 member_klass
= member_klass
->element_class
;
8321 access_level
= member_klass
->flags
& TYPE_ATTRIBUTE_VISIBILITY_MASK
;
8323 if (member_klass
->byval_arg
.type
== MONO_TYPE_VAR
|| member_klass
->byval_arg
.type
== MONO_TYPE_MVAR
)
8326 if (member_klass
->generic_class
&& !can_access_instantiation (access_klass
, member_klass
->generic_class
->context
.class_inst
))
8329 if (is_nesting_type (access_klass
, member_klass
) || (access_klass
->nested_in
&& is_nesting_type (access_klass
->nested_in
, member_klass
)))
8332 if (member_klass
->nested_in
&& !can_access_type (access_klass
, member_klass
->nested_in
))
8335 /*Non nested type with nested visibility. We just fail it.*/
8336 if (access_level
>= TYPE_ATTRIBUTE_NESTED_PRIVATE
&& access_level
<= TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM
&& member_klass
->nested_in
== NULL
)
8339 switch (access_level
) {
8340 case TYPE_ATTRIBUTE_NOT_PUBLIC
:
8341 return can_access_internals (access_klass
->image
->assembly
, member_klass
->image
->assembly
);
8343 case TYPE_ATTRIBUTE_PUBLIC
:
8346 case TYPE_ATTRIBUTE_NESTED_PUBLIC
:
8349 case TYPE_ATTRIBUTE_NESTED_PRIVATE
:
8350 return is_nesting_type (member_klass
, access_klass
);
8352 case TYPE_ATTRIBUTE_NESTED_FAMILY
:
8353 return mono_class_has_parent_and_ignore_generics (access_klass
, member_klass
->nested_in
);
8355 case TYPE_ATTRIBUTE_NESTED_ASSEMBLY
:
8356 return can_access_internals (access_klass
->image
->assembly
, member_klass
->image
->assembly
);
8358 case TYPE_ATTRIBUTE_NESTED_FAM_AND_ASSEM
:
8359 return can_access_internals (access_klass
->image
->assembly
, member_klass
->nested_in
->image
->assembly
) &&
8360 mono_class_has_parent_and_ignore_generics (access_klass
, member_klass
->nested_in
);
8362 case TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM
:
8363 return can_access_internals (access_klass
->image
->assembly
, member_klass
->nested_in
->image
->assembly
) ||
8364 mono_class_has_parent_and_ignore_generics (access_klass
, member_klass
->nested_in
);
8369 /* FIXME: check visibility of type, too */
8371 can_access_member (MonoClass
*access_klass
, MonoClass
*member_klass
, MonoClass
* context_klass
, int access_level
)
8373 MonoClass
*member_generic_def
;
8374 if (((access_klass
->generic_class
&& access_klass
->generic_class
->container_class
) ||
8375 access_klass
->generic_container
) &&
8376 (member_generic_def
= get_generic_definition_class (member_klass
))) {
8377 MonoClass
*access_container
;
8379 if (access_klass
->generic_container
)
8380 access_container
= access_klass
;
8382 access_container
= access_klass
->generic_class
->container_class
;
8384 if (can_access_member (access_container
, member_generic_def
, context_klass
, access_level
))
8388 /* Partition I 8.5.3.2 */
8389 /* the access level values are the same for fields and methods */
8390 switch (access_level
) {
8391 case FIELD_ATTRIBUTE_COMPILER_CONTROLLED
:
8392 /* same compilation unit */
8393 return access_klass
->image
== member_klass
->image
;
8394 case FIELD_ATTRIBUTE_PRIVATE
:
8395 return access_klass
== member_klass
;
8396 case FIELD_ATTRIBUTE_FAM_AND_ASSEM
:
8397 if (is_valid_family_access (access_klass
, member_klass
, context_klass
) &&
8398 can_access_internals (access_klass
->image
->assembly
, member_klass
->image
->assembly
))
8401 case FIELD_ATTRIBUTE_ASSEMBLY
:
8402 return can_access_internals (access_klass
->image
->assembly
, member_klass
->image
->assembly
);
8403 case FIELD_ATTRIBUTE_FAMILY
:
8404 if (is_valid_family_access (access_klass
, member_klass
, context_klass
))
8407 case FIELD_ATTRIBUTE_FAM_OR_ASSEM
:
8408 if (is_valid_family_access (access_klass
, member_klass
, context_klass
))
8410 return can_access_internals (access_klass
->image
->assembly
, member_klass
->image
->assembly
);
8411 case FIELD_ATTRIBUTE_PUBLIC
:
8418 mono_method_can_access_field (MonoMethod
*method
, MonoClassField
*field
)
8420 /* FIXME: check all overlapping fields */
8421 int can
= can_access_member (method
->klass
, field
->parent
, NULL
, field
->type
->attrs
& FIELD_ATTRIBUTE_FIELD_ACCESS_MASK
);
8423 MonoClass
*nested
= method
->klass
->nested_in
;
8425 can
= can_access_member (nested
, field
->parent
, NULL
, field
->type
->attrs
& FIELD_ATTRIBUTE_FIELD_ACCESS_MASK
);
8428 nested
= nested
->nested_in
;
8435 mono_method_can_access_method (MonoMethod
*method
, MonoMethod
*called
)
8437 int can
= can_access_member (method
->klass
, called
->klass
, NULL
, called
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
);
8439 MonoClass
*nested
= method
->klass
->nested_in
;
8441 can
= can_access_member (nested
, called
->klass
, NULL
, called
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
);
8444 nested
= nested
->nested_in
;
8449 * with generics calls to explicit interface implementations can be expressed
8450 * directly: the method is private, but we must allow it. This may be opening
8451 * a hole or the generics code should handle this differently.
8452 * Maybe just ensure the interface type is public.
8454 if ((called
->flags
& METHOD_ATTRIBUTE_VIRTUAL
) && (called
->flags
& METHOD_ATTRIBUTE_FINAL
))
8460 * mono_method_can_access_method_full:
8461 * @method: The caller method
8462 * @called: The called method
8463 * @context_klass: The static type on stack of the owner @called object used
8465 * This function must be used with instance calls, as they have more strict family accessibility.
8466 * It can be used with static methods, but context_klass should be NULL.
8468 * Returns: TRUE if caller have proper visibility and acessibility to @called
8471 mono_method_can_access_method_full (MonoMethod
*method
, MonoMethod
*called
, MonoClass
*context_klass
)
8473 MonoClass
*access_class
= method
->klass
;
8474 MonoClass
*member_class
= called
->klass
;
8475 int can
= can_access_member (access_class
, member_class
, context_klass
, called
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
);
8477 MonoClass
*nested
= access_class
->nested_in
;
8479 can
= can_access_member (nested
, member_class
, context_klass
, called
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
);
8482 nested
= nested
->nested_in
;
8489 if (!can_access_type (access_class
, member_class
) && (!access_class
->nested_in
|| !can_access_type (access_class
->nested_in
, member_class
)))
8492 if (called
->is_inflated
) {
8493 MonoMethodInflated
* infl
= (MonoMethodInflated
*)called
;
8494 if (infl
->context
.method_inst
&& !can_access_instantiation (access_class
, infl
->context
.method_inst
))
8503 * mono_method_can_access_field_full:
8504 * @method: The caller method
8505 * @field: The accessed field
8506 * @context_klass: The static type on stack of the owner @field object used
8508 * This function must be used with instance fields, as they have more strict family accessibility.
8509 * It can be used with static fields, but context_klass should be NULL.
8511 * Returns: TRUE if caller have proper visibility and acessibility to @field
8514 mono_method_can_access_field_full (MonoMethod
*method
, MonoClassField
*field
, MonoClass
*context_klass
)
8516 MonoClass
*access_class
= method
->klass
;
8517 MonoClass
*member_class
= field
->parent
;
8518 /* FIXME: check all overlapping fields */
8519 int can
= can_access_member (access_class
, member_class
, context_klass
, field
->type
->attrs
& FIELD_ATTRIBUTE_FIELD_ACCESS_MASK
);
8521 MonoClass
*nested
= access_class
->nested_in
;
8523 can
= can_access_member (nested
, member_class
, context_klass
, field
->type
->attrs
& FIELD_ATTRIBUTE_FIELD_ACCESS_MASK
);
8526 nested
= nested
->nested_in
;
8533 if (!can_access_type (access_class
, member_class
) && (!access_class
->nested_in
|| !can_access_type (access_class
->nested_in
, member_class
)))
8539 * mono_type_is_valid_enum_basetype:
8540 * @type: The MonoType to check
8542 * Returns: TRUE if the type can be used as the basetype of an enum
8544 gboolean
mono_type_is_valid_enum_basetype (MonoType
* type
) {
8545 switch (type
->type
) {
8548 case MONO_TYPE_BOOLEAN
:
8551 case MONO_TYPE_CHAR
:
8564 * mono_class_is_valid_enum:
8565 * @klass: An enum class to be validated
8567 * This method verify the required properties an enum should have.
8569 * Returns: TRUE if the informed enum class is valid
8571 * FIXME: TypeBuilder enums are allowed to implement interfaces, but since they cannot have methods, only empty interfaces are possible
8572 * FIXME: enum types are not allowed to have a cctor, but mono_reflection_create_runtime_class sets has_cctor to 1 for all types
8573 * FIXME: TypeBuilder enums can have any kind of static fields, but the spec is very explicit about that (P II 14.3)
8575 gboolean
mono_class_is_valid_enum (MonoClass
*klass
) {
8576 MonoClassField
* field
;
8577 gpointer iter
= NULL
;
8578 gboolean found_base_field
= FALSE
;
8580 g_assert (klass
->enumtype
);
8581 /* we cannot test against mono_defaults.enum_class, or mcs won't be able to compile the System namespace*/
8582 if (!klass
->parent
|| strcmp (klass
->parent
->name
, "Enum") || strcmp (klass
->parent
->name_space
, "System") ) {
8586 if ((klass
->flags
& TYPE_ATTRIBUTE_LAYOUT_MASK
) != TYPE_ATTRIBUTE_AUTO_LAYOUT
)
8589 while ((field
= mono_class_get_fields (klass
, &iter
))) {
8590 if (!(field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)) {
8591 if (found_base_field
)
8593 found_base_field
= TRUE
;
8594 if (!mono_type_is_valid_enum_basetype (field
->type
))
8599 if (!found_base_field
)
8602 if (klass
->method
.count
> 0)
8609 mono_generic_class_is_generic_type_definition (MonoGenericClass
*gklass
)
8611 return gklass
->context
.class_inst
== gklass
->container_class
->generic_container
->context
.class_inst
;
8614 static gboolean gshared_supported
;
8617 mono_set_generic_sharing_supported (gboolean supported
)
8619 gshared_supported
= supported
;
8623 * mono_class_generic_sharing_enabled:
8626 * Returns whether generic sharing is enabled for class.
8628 * This is a stop-gap measure to slowly introduce generic sharing
8629 * until we have all the issues sorted out, at which time this
8630 * function will disappear and generic sharing will always be enabled.
8633 mono_class_generic_sharing_enabled (MonoClass
*class)
8635 static int generic_sharing
= MONO_GENERIC_SHARING_NONE
;
8636 static gboolean inited
= FALSE
;
8641 if (gshared_supported
)
8642 generic_sharing
= MONO_GENERIC_SHARING_ALL
;
8644 generic_sharing
= MONO_GENERIC_SHARING_NONE
;
8646 if ((option
= g_getenv ("MONO_GENERIC_SHARING"))) {
8647 if (strcmp (option
, "corlib") == 0)
8648 generic_sharing
= MONO_GENERIC_SHARING_CORLIB
;
8649 else if (strcmp (option
, "collections") == 0)
8650 generic_sharing
= MONO_GENERIC_SHARING_COLLECTIONS
;
8651 else if (strcmp (option
, "all") == 0)
8652 generic_sharing
= MONO_GENERIC_SHARING_ALL
;
8653 else if (strcmp (option
, "none") == 0)
8654 generic_sharing
= MONO_GENERIC_SHARING_NONE
;
8656 g_warning ("Unknown generic sharing option `%s'.", option
);
8659 if (!gshared_supported
)
8660 generic_sharing
= MONO_GENERIC_SHARING_NONE
;
8665 switch (generic_sharing
) {
8666 case MONO_GENERIC_SHARING_NONE
:
8668 case MONO_GENERIC_SHARING_ALL
:
8670 case MONO_GENERIC_SHARING_CORLIB
:
8671 return class->image
== mono_defaults
.corlib
;
8672 case MONO_GENERIC_SHARING_COLLECTIONS
:
8673 if (class->image
!= mono_defaults
.corlib
)
8675 while (class->nested_in
)
8676 class = class->nested_in
;
8677 return g_str_has_prefix (class->name_space
, "System.Collections.Generic");
8679 g_assert_not_reached ();
8685 * mono_class_setup_interface_id:
8687 * Initializes MonoClass::interface_id if required.
8689 * LOCKING: Acquires the loader lock.
8692 mono_class_setup_interface_id (MonoClass
*class)
8694 mono_loader_lock ();
8695 if (MONO_CLASS_IS_INTERFACE (class) && !class->interface_id
)
8696 class->interface_id
= mono_get_unique_iid (class);
8697 mono_loader_unlock ();
8701 * mono_class_alloc_ext:
8703 * Allocate klass->ext if not already done.
8704 * LOCKING: Assumes the loader lock is held.
8707 mono_class_alloc_ext (MonoClass
*klass
)
8710 if (klass
->generic_class
) {
8711 klass
->ext
= g_new0 (MonoClassExt
, 1);
8713 klass
->ext
= mono_image_alloc0 (klass
->image
, sizeof (MonoClassExt
));
8715 class_ext_size
+= sizeof (MonoClassExt
);
8720 * mono_class_setup_interfaces:
8722 * Initialize class->interfaces/interfaces_count.
8723 * LOCKING: Acquires the loader lock.
8726 mono_class_setup_interfaces (MonoClass
*klass
)
8730 if (klass
->interfaces_inited
)
8733 mono_loader_lock ();
8735 if (klass
->interfaces_inited
) {
8736 mono_loader_unlock ();
8740 if (klass
->rank
== 1 && klass
->byval_arg
.type
!= MONO_TYPE_ARRAY
&& mono_defaults
.generic_ilist_class
) {
8743 /* generic IList, ICollection, IEnumerable */
8744 klass
->interface_count
= 1;
8745 klass
->interfaces
= mono_image_alloc0 (klass
->image
, sizeof (MonoClass
*) * klass
->interface_count
);
8747 args
[0] = &klass
->element_class
->byval_arg
;
8748 klass
->interfaces
[0] = mono_class_bind_generic_parameters (
8749 mono_defaults
.generic_ilist_class
, 1, args
, FALSE
);
8750 } else if (klass
->generic_class
) {
8751 MonoClass
*gklass
= klass
->generic_class
->container_class
;
8753 klass
->interface_count
= gklass
->interface_count
;
8754 klass
->interfaces
= g_new0 (MonoClass
*, klass
->interface_count
);
8755 for (i
= 0; i
< klass
->interface_count
; i
++)
8756 klass
->interfaces
[i
] = mono_class_inflate_generic_class (gklass
->interfaces
[i
], mono_generic_class_get_context (klass
->generic_class
));
8759 mono_memory_barrier ();
8761 klass
->interfaces_inited
= TRUE
;
8763 mono_loader_unlock ();