2 * class.c: Class management for the Mono runtime
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001-2006 Novell, Inc.
17 #include <mono/io-layer/atomic.h>
19 #include <mono/metadata/image.h>
20 #include <mono/metadata/assembly.h>
21 #include <mono/metadata/metadata.h>
22 #include <mono/metadata/metadata-internals.h>
23 #include <mono/metadata/profiler-private.h>
24 #include <mono/metadata/tabledefs.h>
25 #include <mono/metadata/tokentype.h>
26 #include <mono/metadata/class-internals.h>
27 #include <mono/metadata/object.h>
28 #include <mono/metadata/appdomain.h>
29 #include <mono/metadata/mono-endian.h>
30 #include <mono/metadata/debug-helpers.h>
31 #include <mono/metadata/reflection.h>
32 #include <mono/metadata/exception.h>
33 #include <mono/metadata/security-manager.h>
34 #include <mono/metadata/security-core-clr.h>
35 #include <mono/metadata/attrdefs.h>
36 #include <mono/os/gc_wrapper.h>
37 #include <mono/utils/mono-counters.h>
41 gboolean mono_print_vtable = FALSE;
43 /* Function supplied by the runtime to find classes by name using information from the AOT file */
44 static MonoGetClassFromName get_class_from_name = NULL;
46 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
47 static gboolean mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res);
49 void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
50 void (*mono_debugger_class_loaded_methods_func) (MonoClass *klass) = NULL;
53 * mono_class_from_typeref:
55 * @type_token: a TypeRef token
57 * Creates the MonoClass* structure representing the type defined by
58 * the typeref token valid inside @image.
59 * Returns: the MonoClass* representing the typeref token, NULL ifcould
63 mono_class_from_typeref (MonoImage *image, guint32 type_token)
65 guint32 cols [MONO_TYPEREF_SIZE];
66 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
68 const char *name, *nspace;
72 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
74 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
75 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
77 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
78 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
79 case MONO_RESOLTION_SCOPE_MODULE:
81 g_error ("null ResolutionScope not yet handled");
82 /* a typedef in disguise */
83 return mono_class_from_name (image, nspace, name);
84 case MONO_RESOLTION_SCOPE_MODULEREF:
85 module = mono_image_load_module (image, idx);
87 return mono_class_from_name (module, nspace, name);
89 char *msg = g_strdup_printf ("%s%s%s", nspace, nspace [0] ? "." : "", name);
92 human_name = mono_stringify_assembly_name (&image->assembly->aname);
93 mono_loader_set_error_type_load (msg, human_name);
99 case MONO_RESOLTION_SCOPE_TYPEREF: {
100 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
103 if (enclosing->inited) {
104 /* Micro-optimization: don't scan the metadata tables if enclosing is already inited */
105 for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
107 if (strcmp (res->name, name) == 0)
111 /* Don't call mono_class_init as we might've been called by it recursively */
112 int i = mono_metadata_nesting_typedef (enclosing->image, enclosing->type_token, 1);
114 guint32 class_nested = mono_metadata_decode_row_col (&enclosing->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, MONO_NESTED_CLASS_NESTED);
115 guint32 string_offset = mono_metadata_decode_row_col (&enclosing->image->tables [MONO_TABLE_TYPEDEF], class_nested - 1, MONO_TYPEDEF_NAME);
116 const char *nname = mono_metadata_string_heap (enclosing->image, string_offset);
118 if (strcmp (nname, name) == 0)
119 return mono_class_create_from_typedef (enclosing->image, MONO_TOKEN_TYPE_DEF | class_nested);
121 i = mono_metadata_nesting_typedef (enclosing->image, enclosing->type_token, i + 1);
124 g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
127 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
131 if (!image->references || !image->references [idx - 1])
132 mono_assembly_load_reference (image, idx - 1);
133 g_assert (image->references [idx - 1]);
135 /* If the assembly did not load, register this as a type load exception */
136 if (image->references [idx - 1] == REFERENCE_MISSING){
137 MonoAssemblyName aname;
140 mono_assembly_get_assemblyref (image, idx - 1, &aname);
141 human_name = mono_stringify_assembly_name (&aname);
142 mono_loader_set_error_assembly_load (human_name, image->assembly->ref_only);
148 return mono_class_from_name (image->references [idx - 1]->image, nspace, name);
151 /* Copy everything mono_metadata_free_array free. */
153 mono_dup_array_type (MonoArrayType *a)
155 a = g_memdup (a, sizeof (MonoArrayType));
157 a->sizes = g_memdup (a->sizes, a->numsizes * sizeof (int));
159 a->lobounds = g_memdup (a->lobounds, a->numlobounds * sizeof (int));
163 /* Copy everything mono_metadata_free_method_signature free. */
165 mono_metadata_signature_deep_dup (MonoMethodSignature *sig)
169 sig = mono_metadata_signature_dup (sig);
171 sig->ret = mono_metadata_type_dup (NULL, sig->ret);
172 for (i = 0; i < sig->param_count; ++i)
173 sig->params [i] = mono_metadata_type_dup (NULL, sig->params [i]);
179 _mono_type_get_assembly_name (MonoClass *klass, GString *str)
181 MonoAssembly *ta = klass->image->assembly;
183 g_string_append_printf (
184 str, ", %s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s",
186 ta->aname.major, ta->aname.minor, ta->aname.build, ta->aname.revision,
187 ta->aname.culture && *ta->aname.culture? ta->aname.culture: "neutral",
188 ta->aname.public_key_token [0] ? (char *)ta->aname.public_key_token : "null");
192 mono_type_name_check_byref (MonoType *type, GString *str)
195 g_string_append_c (str, '&');
199 mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed,
200 MonoTypeNameFormat format)
204 switch (type->type) {
205 case MONO_TYPE_ARRAY: {
206 int i, rank = type->data.array->rank;
207 MonoTypeNameFormat nested_format;
209 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
210 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
212 mono_type_get_name_recurse (
213 &type->data.array->eklass->byval_arg, str, FALSE, nested_format);
214 g_string_append_c (str, '[');
216 g_string_append_c (str, '*');
217 for (i = 1; i < rank; i++)
218 g_string_append_c (str, ',');
219 g_string_append_c (str, ']');
221 mono_type_name_check_byref (type, str);
223 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
224 _mono_type_get_assembly_name (type->data.array->eklass, str);
227 case MONO_TYPE_SZARRAY: {
228 MonoTypeNameFormat nested_format;
230 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
231 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
233 mono_type_get_name_recurse (
234 &type->data.klass->byval_arg, str, FALSE, nested_format);
235 g_string_append (str, "[]");
237 mono_type_name_check_byref (type, str);
239 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
240 _mono_type_get_assembly_name (type->data.klass, str);
243 case MONO_TYPE_PTR: {
244 MonoTypeNameFormat nested_format;
246 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
247 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
249 mono_type_get_name_recurse (
250 type->data.type, str, FALSE, nested_format);
251 g_string_append_c (str, '*');
253 mono_type_name_check_byref (type, str);
255 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
256 _mono_type_get_assembly_name (mono_class_from_mono_type (type->data.type), str);
261 g_assert (type->data.generic_param->name);
262 g_string_append (str, type->data.generic_param->name);
264 mono_type_name_check_byref (type, str);
268 klass = mono_class_from_mono_type (type);
269 if (klass->nested_in) {
270 mono_type_get_name_recurse (
271 &klass->nested_in->byval_arg, str, TRUE, format);
272 if (format == MONO_TYPE_NAME_FORMAT_IL)
273 g_string_append_c (str, '.');
275 g_string_append_c (str, '+');
276 } else if (*klass->name_space) {
277 g_string_append (str, klass->name_space);
278 g_string_append_c (str, '.');
280 if (format == MONO_TYPE_NAME_FORMAT_IL) {
281 char *s = strchr (klass->name, '`');
282 int len = s ? s - klass->name : strlen (klass->name);
284 g_string_append_len (str, klass->name, len);
286 g_string_append (str, klass->name);
289 if (klass->generic_class) {
290 MonoGenericClass *gclass = klass->generic_class;
291 MonoGenericInst *inst = gclass->context.class_inst;
292 MonoTypeNameFormat nested_format;
295 nested_format = format == MONO_TYPE_NAME_FORMAT_FULL_NAME ?
296 MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED : format;
298 if (format == MONO_TYPE_NAME_FORMAT_IL)
299 g_string_append_c (str, '<');
301 g_string_append_c (str, '[');
302 for (i = 0; i < inst->type_argc; i++) {
303 MonoType *t = inst->type_argv [i];
306 g_string_append_c (str, ',');
307 if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
308 (t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
309 g_string_append_c (str, '[');
310 mono_type_get_name_recurse (inst->type_argv [i], str, FALSE, nested_format);
311 if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
312 (t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
313 g_string_append_c (str, ']');
315 if (format == MONO_TYPE_NAME_FORMAT_IL)
316 g_string_append_c (str, '>');
318 g_string_append_c (str, ']');
319 } else if (klass->generic_container &&
320 (format != MONO_TYPE_NAME_FORMAT_FULL_NAME) &&
321 (format != MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)) {
324 if (format == MONO_TYPE_NAME_FORMAT_IL)
325 g_string_append_c (str, '<');
327 g_string_append_c (str, '[');
328 for (i = 0; i < klass->generic_container->type_argc; i++) {
330 g_string_append_c (str, ',');
331 g_string_append (str, klass->generic_container->type_params [i].name);
333 if (format == MONO_TYPE_NAME_FORMAT_IL)
334 g_string_append_c (str, '>');
336 g_string_append_c (str, ']');
339 mono_type_name_check_byref (type, str);
341 if ((format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
342 (type->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
343 _mono_type_get_assembly_name (klass, str);
349 * mono_type_get_name:
351 * @format: the format for the return string.
354 * Returns: the string representation in a number of formats:
356 * if format is MONO_TYPE_NAME_FORMAT_REFLECTION, the return string is
357 * returned in the formatrequired by System.Reflection, this is the
358 * inverse of mono_reflection_parse_type ().
360 * if format is MONO_TYPE_NAME_FORMAT_IL, it returns a syntax that can
361 * be used by the IL assembler.
363 * if format is MONO_TYPE_NAME_FORMAT_FULL_NAME
365 * if format is MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
368 mono_type_get_name_full (MonoType *type, MonoTypeNameFormat format)
372 result = g_string_new ("");
374 mono_type_get_name_recurse (type, result, FALSE, format);
376 return g_string_free (result, FALSE);
380 * mono_type_get_full_name:
383 * Returns: the string representation for type as required by System.Reflection.
384 * The inverse of mono_reflection_parse_type ().
387 mono_type_get_full_name (MonoClass *class)
389 return mono_type_get_name_full (mono_class_get_type (class), MONO_TYPE_NAME_FORMAT_REFLECTION);
393 * mono_type_get_name:
396 * Returns: the string representation for type as it would be represented in IL code.
399 mono_type_get_name (MonoType *type)
401 return mono_type_get_name_full (type, MONO_TYPE_NAME_FORMAT_IL);
405 * mono_type_get_underlying_type:
408 * Returns: the MonoType for the underlying integer type if @type
409 * is an enum and byref is false, otherwise the type itself.
412 mono_type_get_underlying_type (MonoType *type)
414 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype && !type->byref)
415 return type->data.klass->enum_basetype;
416 if (type->type == MONO_TYPE_GENERICINST && type->data.generic_class->container_class->enumtype && !type->byref)
417 return type->data.generic_class->container_class->enum_basetype;
422 * mono_class_is_open_constructed_type:
425 * Returns TRUE if type represents a generics open constructed type
426 * (not all the type parameters required for the instantiation have
430 mono_class_is_open_constructed_type (MonoType *t)
436 case MONO_TYPE_SZARRAY:
437 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
438 case MONO_TYPE_ARRAY:
439 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
441 return mono_class_is_open_constructed_type (t->data.type);
442 case MONO_TYPE_GENERICINST:
443 return t->data.generic_class->context.class_inst->is_open;
450 inflate_generic_type (MonoType *type, MonoGenericContext *context)
452 switch (type->type) {
453 case MONO_TYPE_MVAR: {
455 int num = type->data.generic_param->num;
456 MonoGenericInst *inst = context->method_inst;
457 if (!inst || !inst->type_argv)
459 if (num >= inst->type_argc)
460 g_error ("MVAR %d (%s) cannot be expanded in this context with %d instantiations", num, type->data.generic_param->name, inst->type_argc);
463 * Note that the VAR/MVAR cases are different from the rest. The other cases duplicate @type,
464 * while the VAR/MVAR duplicates a type from the context. So, we need to ensure that the
465 * ->byref and ->attrs from @type are propagated to the returned type.
467 nt = mono_metadata_type_dup (NULL, inst->type_argv [num]);
468 nt->byref = type->byref;
469 nt->attrs = type->attrs;
472 case MONO_TYPE_VAR: {
474 int num = type->data.generic_param->num;
475 MonoGenericInst *inst = context->class_inst;
478 if (num >= inst->type_argc)
479 g_error ("VAR %d (%s) cannot be expanded in this context with %d instantiations", num, type->data.generic_param->name, inst->type_argc);
480 nt = mono_metadata_type_dup (NULL, inst->type_argv [num]);
481 nt->byref = type->byref;
482 nt->attrs = type->attrs;
485 case MONO_TYPE_SZARRAY: {
486 MonoClass *eclass = type->data.klass;
487 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
490 nt = mono_metadata_type_dup (NULL, type);
491 nt->data.klass = mono_class_from_mono_type (inflated);
492 mono_metadata_free_type (inflated);
495 case MONO_TYPE_ARRAY: {
496 MonoClass *eclass = type->data.array->eklass;
497 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
500 nt = mono_metadata_type_dup (NULL, type);
501 nt->data.array = g_memdup (nt->data.array, sizeof (MonoArrayType));
502 nt->data.array->eklass = mono_class_from_mono_type (inflated);
503 mono_metadata_free_type (inflated);
506 case MONO_TYPE_GENERICINST: {
507 MonoGenericClass *gclass = type->data.generic_class;
508 MonoGenericInst *inst;
510 if (!gclass->context.class_inst->is_open)
513 inst = mono_metadata_inflate_generic_inst (gclass->context.class_inst, context);
514 if (inst != gclass->context.class_inst)
515 gclass = mono_metadata_lookup_generic_class (gclass->container_class, inst, gclass->is_dynamic);
517 if (gclass == type->data.generic_class)
520 nt = mono_metadata_type_dup (NULL, type);
521 nt->data.generic_class = gclass;
524 case MONO_TYPE_CLASS:
525 case MONO_TYPE_VALUETYPE: {
526 MonoClass *klass = type->data.klass;
527 MonoGenericContainer *container = klass->generic_container;
528 MonoGenericInst *inst;
529 MonoGenericClass *gclass = NULL;
535 /* We can't use context->class_inst directly, since it can have more elements */
536 inst = mono_metadata_inflate_generic_inst (container->context.class_inst, context);
537 if (inst == container->context.class_inst)
540 gclass = mono_metadata_lookup_generic_class (klass, inst, klass->image->dynamic);
542 nt = mono_metadata_type_dup (NULL, type);
543 nt->type = MONO_TYPE_GENERICINST;
544 nt->data.generic_class = gclass;
554 mono_generic_class_get_context (MonoGenericClass *gclass)
556 return &gclass->context;
560 mono_class_get_context (MonoClass *class)
562 return class->generic_class ? mono_generic_class_get_context (class->generic_class) : NULL;
566 * mono_class_inflate_generic_type:
568 * @context: a generics context
570 * Instantiate the generic type @type, using the generics context @context.
572 * Returns: the instantiated type. The returned MonoType is allocated on the heap and is
573 * owned by the caller.
576 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
578 MonoType *inflated = inflate_generic_type (type, context);
581 return mono_metadata_type_dup (NULL, type);
583 mono_stats.inflated_type_count++;
587 static MonoGenericContext
588 inflate_generic_context (MonoGenericContext *context, MonoGenericContext *inflate_with)
590 MonoGenericInst *class_inst = NULL;
591 MonoGenericInst *method_inst = NULL;
592 MonoGenericContext res;
594 if (context->class_inst)
595 class_inst = mono_metadata_inflate_generic_inst (context->class_inst, inflate_with);
597 if (context->method_inst)
598 method_inst = mono_metadata_inflate_generic_inst (context->method_inst, inflate_with);
600 res.class_inst = class_inst;
601 res.method_inst = method_inst;
607 * mono_class_inflate_generic_method:
608 * @method: a generic method
609 * @context: a generics context
611 * Instantiate the generic method @method using the generics context @context.
613 * Returns: the new instantiated method
616 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context)
618 return mono_class_inflate_generic_method_full (method, NULL, context);
622 * mono_class_inflate_generic_method:
624 * Instantiate method @method with the generic context @context.
625 * BEWARE: All non-trivial fields are invalid, including klass, signature, and header.
626 * Use mono_method_signature () and mono_method_get_header () to get the correct values.
629 mono_class_inflate_generic_method_full (MonoMethod *method, MonoClass *klass_hint, MonoGenericContext *context)
632 MonoMethodInflated *iresult, *cached;
633 MonoMethodSignature *sig;
634 MonoGenericContext tmp_context;
636 /* The `method' has already been instantiated before => we need to peel out the instantiation and create a new context */
637 while (method->is_inflated) {
638 MonoGenericContext *method_context = mono_method_get_context (method);
639 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
641 tmp_context = inflate_generic_context (method_context, context);
642 context = &tmp_context;
644 if (mono_metadata_generic_context_equal (method_context, context))
647 method = imethod->declaring;
650 if (!method->generic_container && !method->klass->generic_container)
653 mono_stats.inflated_method_count++;
654 iresult = g_new0 (MonoMethodInflated, 1);
655 iresult->context = *context;
656 iresult->declaring = method;
659 cached = mono_method_inflated_lookup (iresult, FALSE);
661 mono_loader_unlock ();
663 return (MonoMethod*)cached;
666 sig = mono_method_signature (method);
668 iresult->method.pinvoke = *(MonoMethodPInvoke*)method;
670 iresult->method.normal = *(MonoMethodNormal*)method;
671 iresult->method.normal.header = NULL;
674 result = (MonoMethod *) iresult;
675 result->is_inflated = 1;
676 /* result->generic_container = NULL; */
677 result->signature = NULL;
679 if (!klass_hint || !klass_hint->generic_class ||
680 klass_hint->generic_class->container_class != method->klass ||
681 klass_hint->generic_class->context.class_inst != context->class_inst)
684 if (method->klass->generic_container)
685 result->klass = klass_hint;
687 if (!result->klass) {
688 MonoType *inflated = inflate_generic_type (&method->klass->byval_arg, context);
689 result->klass = inflated ? mono_class_from_mono_type (inflated) : method->klass;
691 mono_metadata_free_type (inflated);
694 if (context->method_inst)
695 result->generic_container = NULL;
696 else if (method->generic_container)
697 iresult->context.method_inst = method->generic_container->context.method_inst;
699 mono_method_inflated_lookup (iresult, TRUE);
700 mono_loader_unlock ();
705 * mono_get_inflated_method:
707 * Obsolete. We keep it around since it's mentioned in the public API.
710 mono_get_inflated_method (MonoMethod *method)
716 mono_method_get_context (MonoMethod *method)
718 MonoMethodInflated *imethod;
719 if (!method->is_inflated)
721 imethod = (MonoMethodInflated *) method;
722 return &imethod->context;
726 * mono_class_find_enum_basetype:
727 * @class: The enum class
729 * Determine the basetype of an enum by iterating through its fields. We do this
730 * in a separate function since it is cheaper than calling mono_class_setup_fields.
733 mono_class_find_enum_basetype (MonoClass *class)
735 MonoImage *m = class->image;
736 const int top = class->field.count;
739 g_assert (class->enumtype);
742 * Fetch all the field information.
744 for (i = 0; i < top; i++){
746 guint32 cols [MONO_FIELD_SIZE];
747 int idx = class->field.first + i;
748 MonoGenericContainer *container = NULL;
751 /* class->field.first and idx points into the fieldptr table */
752 mono_metadata_decode_table_row (m, MONO_TABLE_FIELD, idx, cols, MONO_FIELD_SIZE);
753 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
754 mono_metadata_decode_value (sig, &sig);
755 /* FIELD signature == 0x06 */
756 g_assert (*sig == 0x06);
757 if (class->generic_container)
758 container = class->generic_container;
759 else if (class->generic_class) {
760 MonoClass *gklass = class->generic_class->container_class;
762 container = gklass->generic_container;
763 g_assert (container);
765 ftype = mono_metadata_parse_type_full (m, container, MONO_PARSE_FIELD, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
768 if (class->generic_class) {
769 ftype = mono_class_inflate_generic_type (ftype, mono_class_get_context (class));
770 ftype->attrs = cols [MONO_FIELD_FLAGS];
773 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC))
781 * mono_class_setup_fields:
782 * @class: The class to initialize
784 * Initializes the class->fields.
785 * LOCKING: Assumes the loader lock is held.
788 mono_class_setup_fields (MonoClass *class)
790 MonoImage *m = class->image;
791 int top = class->field.count;
792 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
793 int i, blittable = TRUE;
794 guint32 real_size = 0;
795 guint32 packing_size = 0;
796 gboolean explicit_size;
797 MonoClassField *field;
798 MonoGenericContainer *container = NULL;
799 MonoClass *gklass = NULL;
801 if (class->size_inited)
804 if (class->generic_class) {
805 MonoClass *gklass = class->generic_class->container_class;
806 mono_class_setup_fields (gklass);
807 top = gklass->field.count;
810 class->instance_size = 0;
812 class->sizes.class_size = 0;
815 /* For generic instances, class->parent might not have been initialized */
816 mono_class_init (class->parent);
817 if (!class->parent->size_inited)
818 mono_class_setup_fields (class->parent);
819 class->instance_size += class->parent->instance_size;
820 class->min_align = class->parent->min_align;
821 /* we use |= since it may have been set already */
822 class->has_references |= class->parent->has_references;
823 blittable = class->parent->blittable;
825 class->instance_size = sizeof (MonoObject);
826 class->min_align = 1;
829 /* Get the real size */
830 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
833 g_assert ((packing_size & 0xfffffff0) == 0);
834 class->packing_size = packing_size;
835 real_size += class->instance_size;
839 if (explicit_size && real_size) {
840 class->instance_size = MAX (real_size, class->instance_size);
842 class->size_inited = 1;
843 class->blittable = blittable;
847 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT)
850 /* Prevent infinite loops if the class references itself */
851 class->size_inited = 1;
853 class->fields = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoClassField) * top);
855 if (class->generic_container) {
856 container = class->generic_container;
857 } else if (class->generic_class) {
858 gklass = class->generic_class->container_class;
859 container = gklass->generic_container;
860 g_assert (container);
862 mono_class_setup_fields (gklass);
866 * Fetch all the field information.
868 for (i = 0; i < top; i++){
869 int idx = class->field.first + i;
870 field = &class->fields [i];
872 field->parent = class;
874 if (class->generic_class) {
875 MonoClassField *gfield = &gklass->fields [i];
876 MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
878 ifield->generic_type = gfield->type;
879 field->name = gfield->name;
880 field->generic_info = ifield;
881 field->type = mono_class_inflate_generic_type (gfield->type, mono_class_get_context (class));
882 field->type->attrs = gfield->type->attrs;
883 if (mono_field_is_deleted (field))
885 field->offset = gfield->offset;
886 field->data = gfield->data;
890 guint32 cols [MONO_FIELD_SIZE];
892 /* class->field.first and idx points into the fieldptr table */
893 mono_metadata_decode_table_row (m, MONO_TABLE_FIELD, idx, cols, MONO_FIELD_SIZE);
894 /* The name is needed for fieldrefs */
895 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
896 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
897 mono_metadata_decode_value (sig, &sig);
898 /* FIELD signature == 0x06 */
899 g_assert (*sig == 0x06);
900 field->type = mono_metadata_parse_type_full (m, container, MONO_PARSE_FIELD, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
902 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
905 if (mono_field_is_deleted (field))
907 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
909 mono_metadata_field_info (m, idx, &offset, NULL, NULL);
910 field->offset = offset;
911 if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
912 g_warning ("%s not initialized correctly (missing field layout info for %s)",
913 class->name, field->name);
916 if (field->type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
917 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
919 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
920 field->data = mono_image_rva_map (class->image, rva);
924 /* Only do these checks if we still think this type is blittable */
925 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
926 if (field->type->byref || MONO_TYPE_IS_REFERENCE (field->type)) {
929 MonoClass *field_class = mono_class_from_mono_type (field->type);
930 if (!field_class || !field_class->blittable)
935 if (class->enumtype && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
936 class->enum_basetype = field->type;
937 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
938 blittable = class->element_class->blittable;
941 /* The def_value of fields is compute lazily during vtable creation */
944 if (class == mono_defaults.string_class)
947 class->blittable = blittable;
949 if (class->enumtype && !class->enum_basetype) {
950 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
953 if (explicit_size && real_size) {
954 class->instance_size = MAX (real_size, class->instance_size);
957 if (class->exception_type)
959 mono_class_layout_fields (class);
963 * mono_class_setup_fields_locking:
964 * @class: The class to initialize
966 * Initializes the class->fields array of fields.
967 * Aquires the loader lock.
970 mono_class_setup_fields_locking (MonoClass *class)
973 mono_class_setup_fields (class);
974 mono_loader_unlock ();
978 * mono_class_has_references:
980 * Returns whenever @klass->has_references is set, initializing it if needed.
981 * Aquires the loader lock.
984 mono_class_has_references (MonoClass *klass)
986 if (klass->init_pending) {
987 /* Be conservative */
990 mono_class_init (klass);
992 return klass->has_references;
996 /* useful until we keep track of gc-references in corlib etc. */
998 #define IS_GC_REFERENCE(t) FALSE
1000 #define IS_GC_REFERENCE(t) ((t)->type == MONO_TYPE_U && class->image == mono_defaults.corlib)
1004 * mono_type_get_basic_type_from_generic:
1007 * Returns a closed type corresponding to the possibly open type
1011 mono_type_get_basic_type_from_generic (MonoType *type)
1013 /* When we do generic sharing we let type variables stand for reference types. */
1014 if (!type->byref && (type->type == MONO_TYPE_VAR || type->type == MONO_TYPE_MVAR))
1015 return &mono_defaults.object_class->byval_arg;
1020 * mono_class_layout_fields:
1023 * Compute the placement of fields inside an object or struct, according to
1024 * the layout rules and set the following fields in @class:
1025 * - has_references (if the class contains instance references firled or structs that contain references)
1026 * - has_static_refs (same, but for static fields)
1027 * - instance_size (size of the object in memory)
1028 * - class_size (size needed for the static fields)
1029 * - size_inited (flag set when the instance_size is set)
1031 * LOCKING: this is supposed to be called with the loader lock held.
1034 mono_class_layout_fields (MonoClass *class)
1037 const int top = class->field.count;
1038 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
1039 guint32 pass, passes, real_size;
1040 gboolean gc_aware_layout = FALSE;
1041 MonoClassField *field;
1044 * When we do generic sharing we need to have layout
1045 * information for open generic classes (either with a generic
1046 * context containing type variables or with a generic
1047 * container), so we don't return in that case anymore.
1051 * Enable GC aware auto layout: in this mode, reference
1052 * fields are grouped together inside objects, increasing collector
1054 * Requires that all classes whose layout is known to native code be annotated
1055 * with [StructLayout (LayoutKind.Sequential)]
1056 * Value types have gc_aware_layout disabled by default, as per
1057 * what the default is for other runtimes.
1059 /* corlib is missing [StructLayout] directives in many places */
1060 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
1061 if (class->image != mono_defaults.corlib &&
1062 class->byval_arg.type != MONO_TYPE_VALUETYPE)
1063 gc_aware_layout = TRUE;
1064 /* from System.dll, used in metadata/process.h */
1065 if (strcmp (class->name, "ProcessStartInfo") == 0)
1066 gc_aware_layout = FALSE;
1069 /* Compute klass->has_references */
1071 * Process non-static fields first, since static fields might recursively
1072 * refer to the class itself.
1074 for (i = 0; i < top; i++) {
1077 field = &class->fields [i];
1079 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
1080 ftype = mono_type_get_underlying_type (field->type);
1081 ftype = mono_type_get_basic_type_from_generic (ftype);
1082 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype)))))
1083 class->has_references = TRUE;
1087 for (i = 0; i < top; i++) {
1090 field = &class->fields [i];
1092 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC) {
1093 ftype = mono_type_get_underlying_type (field->type);
1094 ftype = mono_type_get_basic_type_from_generic (ftype);
1095 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype)))))
1096 class->has_static_refs = TRUE;
1100 for (i = 0; i < top; i++) {
1103 field = &class->fields [i];
1105 ftype = mono_type_get_underlying_type (field->type);
1106 ftype = mono_type_get_basic_type_from_generic (ftype);
1107 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype))))) {
1108 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1109 class->has_static_refs = TRUE;
1111 class->has_references = TRUE;
1116 * Compute field layout and total size (not considering static fields)
1120 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
1121 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
1123 if (gc_aware_layout)
1128 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
1132 real_size = class->parent->instance_size;
1134 real_size = sizeof (MonoObject);
1136 for (pass = 0; pass < passes; ++pass) {
1137 for (i = 0; i < top; i++){
1142 field = &class->fields [i];
1144 if (mono_field_is_deleted (field))
1146 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1149 ftype = mono_type_get_underlying_type (field->type);
1150 ftype = mono_type_get_basic_type_from_generic (ftype);
1151 if (gc_aware_layout) {
1152 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype))))) {
1161 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
1162 (strcmp (field->name, "$PRIVATE$") == 0)) {
1163 /* This field is a hack inserted by MCS to empty structures */
1167 size = mono_type_size (field->type, &align);
1169 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
1170 align = class->packing_size ? MIN (class->packing_size, align): align;
1171 /* if the field has managed references, we need to force-align it
1174 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype)))))
1175 align = MAX (align, sizeof (gpointer));
1177 class->min_align = MAX (align, class->min_align);
1178 field->offset = real_size;
1179 field->offset += align - 1;
1180 field->offset &= ~(align - 1);
1181 real_size = field->offset + size;
1184 class->instance_size = MAX (real_size, class->instance_size);
1186 if (class->instance_size & (class->min_align - 1)) {
1187 class->instance_size += class->min_align - 1;
1188 class->instance_size &= ~(class->min_align - 1);
1192 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
1194 for (i = 0; i < top; i++) {
1199 field = &class->fields [i];
1202 * There must be info about all the fields in a type if it
1203 * uses explicit layout.
1206 if (mono_field_is_deleted (field))
1208 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1211 size = mono_type_size (field->type, &align);
1214 * When we get here, field->offset is already set by the
1215 * loader (for either runtime fields or fields loaded from metadata).
1216 * The offset is from the start of the object: this works for both
1217 * classes and valuetypes.
1219 field->offset += sizeof (MonoObject);
1220 ftype = mono_type_get_underlying_type (field->type);
1221 ftype = mono_type_get_basic_type_from_generic (ftype);
1222 if (MONO_TYPE_IS_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype))))) {
1223 if (field->offset % sizeof (gpointer)) {
1224 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
1231 real_size = MAX (real_size, size + field->offset);
1233 class->instance_size = MAX (real_size, class->instance_size);
1237 #if NO_UNALIGNED_ACCESS
1238 if (layout != TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
1240 * For small structs, set min_align to at least the struct size, since the
1241 * JIT memset/memcpy code assumes this and generates unaligned accesses
1242 * otherwise. See #78990 for a testcase.
1243 * FIXME: Fix the memset/memcpy code instead.
1245 if (class->instance_size <= sizeof (MonoObject) + sizeof (gpointer))
1246 class->min_align = MAX (class->min_align, class->instance_size - sizeof (MonoObject));
1250 class->size_inited = 1;
1253 * Compute static field layout and size
1255 for (i = 0; i < top; i++){
1259 field = &class->fields [i];
1261 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
1263 if (mono_field_is_deleted (field))
1266 size = mono_type_size (field->type, &align);
1267 field->offset = class->sizes.class_size;
1268 field->offset += align - 1;
1269 field->offset &= ~(align - 1);
1270 class->sizes.class_size = field->offset + size;
1275 * mono_class_setup_methods:
1278 * Initializes the 'methods' array in the klass.
1279 * Calling this method should be avoided if possible since it allocates a lot
1280 * of long-living MonoMethod structures.
1281 * Methods belonging to an interface are assigned a sequential slot starting
1285 mono_class_setup_methods (MonoClass *class)
1288 MonoMethod **methods;
1293 mono_loader_lock ();
1295 if (class->methods) {
1296 mono_loader_unlock ();
1300 if (class->generic_class) {
1301 MonoClass *gklass = class->generic_class->container_class;
1303 mono_class_init (gklass);
1304 mono_class_setup_methods (gklass);
1306 /* The + 1 makes this always non-NULL to pass the check in mono_class_setup_methods () */
1307 class->method.count = gklass->method.count;
1308 methods = g_new0 (MonoMethod *, class->method.count + 1);
1310 for (i = 0; i < class->method.count; i++) {
1311 methods [i] = mono_class_inflate_generic_method_full (
1312 gklass->methods [i], class, mono_class_get_context (class));
1315 methods = mono_mempool_alloc (class->image->mempool, sizeof (MonoMethod*) * class->method.count);
1316 for (i = 0; i < class->method.count; ++i) {
1317 int idx = mono_metadata_translate_token_index (class->image, MONO_TABLE_METHOD, class->method.first + i + 1);
1318 methods [i] = mono_get_method (class->image, MONO_TOKEN_METHOD_DEF | idx, class);
1319 if (class->valuetype && methods [i]->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED && !(methods [i]->flags & MONO_METHOD_ATTR_STATIC))
1320 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
1324 if (MONO_CLASS_IS_INTERFACE (class))
1325 for (i = 0; i < class->method.count; ++i)
1326 methods [i]->slot = i;
1328 /* Leave this assignment as the last op in this function */
1329 class->methods = methods;
1331 if (mono_debugger_class_loaded_methods_func)
1332 mono_debugger_class_loaded_methods_func (class);
1334 mono_loader_unlock ();
1339 mono_class_setup_properties (MonoClass *class)
1341 guint startm, endm, i, j;
1342 guint32 cols [MONO_PROPERTY_SIZE];
1343 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
1344 MonoProperty *properties;
1347 if (class->properties)
1350 mono_loader_lock ();
1352 if (class->properties) {
1353 mono_loader_unlock ();
1357 if (class->generic_class) {
1358 MonoClass *gklass = class->generic_class->container_class;
1360 class->property = gklass->property;
1362 mono_class_init (gklass);
1363 mono_class_setup_properties (gklass);
1365 properties = g_new0 (MonoProperty, class->property.count + 1);
1367 for (i = 0; i < class->property.count; i++) {
1368 MonoProperty *prop = &properties [i];
1370 *prop = gklass->properties [i];
1373 prop->get = mono_class_inflate_generic_method_full (
1374 prop->get, class, mono_class_get_context (class));
1376 prop->set = mono_class_inflate_generic_method_full (
1377 prop->set, class, mono_class_get_context (class));
1379 prop->parent = class;
1382 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &last);
1383 class->property.count = last - class->property.first;
1385 if (class->property.count)
1386 mono_class_setup_methods (class);
1388 properties = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoProperty) * class->property.count);
1389 for (i = class->property.first; i < last; ++i) {
1390 mono_metadata_decode_table_row (class->image, MONO_TABLE_PROPERTY, i, cols, MONO_PROPERTY_SIZE);
1391 properties [i - class->property.first].parent = class;
1392 properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
1393 properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
1395 startm = mono_metadata_methods_from_property (class->image, i, &endm);
1396 for (j = startm; j < endm; ++j) {
1399 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
1401 if (class->image->uncompressed_metadata)
1402 /* It seems like the MONO_METHOD_SEMA_METHOD column needs no remapping */
1403 method = mono_get_method (class->image, MONO_TOKEN_METHOD_DEF | cols [MONO_METHOD_SEMA_METHOD], class);
1405 method = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1407 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1408 case METHOD_SEMANTIC_SETTER:
1409 properties [i - class->property.first].set = method;
1411 case METHOD_SEMANTIC_GETTER:
1412 properties [i - class->property.first].get = method;
1421 /* Leave this assignment as the last op in the function */
1422 class->properties = properties;
1424 mono_loader_unlock ();
1428 inflate_method_listz (MonoMethod **methods, MonoClass *class, MonoGenericContext *context)
1430 MonoMethod **om, **retval;
1433 for (om = methods, count = 0; *om; ++om, ++count)
1436 retval = g_new0 (MonoMethod*, count + 1);
1438 for (om = methods, count = 0; *om; ++om, ++count)
1439 retval [count] = mono_class_inflate_generic_method_full (*om, class, context);
1445 mono_class_setup_events (MonoClass *class)
1447 guint startm, endm, i, j;
1448 guint32 cols [MONO_EVENT_SIZE];
1449 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
1456 mono_loader_lock ();
1458 if (class->events) {
1459 mono_loader_unlock ();
1463 if (class->generic_class) {
1464 MonoClass *gklass = class->generic_class->container_class;
1465 MonoGenericContext *context;
1467 mono_class_setup_events (gklass);
1468 class->event = gklass->event;
1470 class->events = g_new0 (MonoEvent, class->event.count);
1472 if (class->event.count)
1473 context = mono_class_get_context (class);
1475 for (i = 0; i < class->event.count; i++) {
1476 MonoEvent *event = &class->events [i];
1477 MonoEvent *gevent = &gklass->events [i];
1479 event->parent = class;
1480 event->name = gevent->name;
1481 event->add = gevent->add ? mono_class_inflate_generic_method_full (gevent->add, class, context) : NULL;
1482 event->remove = gevent->remove ? mono_class_inflate_generic_method_full (gevent->remove, class, context) : NULL;
1483 event->raise = gevent->raise ? mono_class_inflate_generic_method_full (gevent->raise, class, context) : NULL;
1484 event->other = gevent->other ? inflate_method_listz (gevent->other, class, context) : NULL;
1485 event->attrs = gevent->attrs;
1488 mono_loader_unlock ();
1492 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &last);
1493 class->event.count = last - class->event.first;
1495 if (class->event.count)
1496 mono_class_setup_methods (class);
1498 events = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoEvent) * class->event.count);
1499 for (i = class->event.first; i < last; ++i) {
1500 MonoEvent *event = &events [i - class->event.first];
1502 mono_metadata_decode_table_row (class->image, MONO_TABLE_EVENT, i, cols, MONO_EVENT_SIZE);
1503 event->parent = class;
1504 event->attrs = cols [MONO_EVENT_FLAGS];
1505 event->name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
1507 startm = mono_metadata_methods_from_event (class->image, i, &endm);
1508 for (j = startm; j < endm; ++j) {
1511 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
1513 if (class->image->uncompressed_metadata)
1514 /* It seems like the MONO_METHOD_SEMA_METHOD column needs no remapping */
1515 method = mono_get_method (class->image, MONO_TOKEN_METHOD_DEF | cols [MONO_METHOD_SEMA_METHOD], class);
1517 method = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1519 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1520 case METHOD_SEMANTIC_ADD_ON:
1521 event->add = method;
1523 case METHOD_SEMANTIC_REMOVE_ON:
1524 event->remove = method;
1526 case METHOD_SEMANTIC_FIRE:
1527 event->raise = method;
1529 case METHOD_SEMANTIC_OTHER: {
1532 if (event->other == NULL) {
1533 event->other = g_new0 (MonoMethod*, 2);
1535 while (event->other [n])
1537 event->other = g_realloc (event->other, (n + 2) * sizeof (MonoMethod*));
1539 event->other [n] = method;
1540 /* NULL terminated */
1541 event->other [n + 1] = NULL;
1549 /* Leave this assignment as the last op in the function */
1550 class->events = events;
1552 mono_loader_unlock ();
1556 * Global pool of interface IDs, represented as a bitset.
1557 * LOCKING: this is supposed to be accessed with the loader lock held.
1559 static MonoBitSet *global_interface_bitset = NULL;
1562 * mono_unload_interface_ids:
1563 * @bitset: bit set of interface IDs
1565 * When an image is unloaded, the interface IDs associated with
1566 * the image are put back in the global pool of IDs so the numbers
1570 mono_unload_interface_ids (MonoBitSet *bitset)
1572 mono_loader_lock ();
1573 mono_bitset_sub (global_interface_bitset, bitset);
1574 mono_loader_unlock ();
1578 * mono_get_unique_iid:
1581 * Assign a unique integer ID to the interface represented by @class.
1582 * The ID will positive and as small as possible.
1583 * LOCKING: this is supposed to be called with the loader lock held.
1584 * Returns: the new ID.
1587 mono_get_unique_iid (MonoClass *class)
1591 g_assert (MONO_CLASS_IS_INTERFACE (class));
1593 if (!global_interface_bitset) {
1594 global_interface_bitset = mono_bitset_new (128, 0);
1597 iid = mono_bitset_find_first_unset (global_interface_bitset, -1);
1599 int old_size = mono_bitset_size (global_interface_bitset);
1600 MonoBitSet *new_set = mono_bitset_clone (global_interface_bitset, old_size * 2);
1601 mono_bitset_free (global_interface_bitset);
1602 global_interface_bitset = new_set;
1605 mono_bitset_set (global_interface_bitset, iid);
1606 /* set the bit also in the per-image set */
1607 if (class->image->interface_bitset) {
1608 if (iid >= mono_bitset_size (class->image->interface_bitset)) {
1609 MonoBitSet *new_set = mono_bitset_clone (class->image->interface_bitset, iid + 1);
1610 mono_bitset_free (class->image->interface_bitset);
1611 class->image->interface_bitset = new_set;
1614 class->image->interface_bitset = mono_bitset_new (iid + 1, 0);
1616 mono_bitset_set (class->image->interface_bitset, iid);
1618 if (mono_print_vtable) {
1620 char *type_name = mono_type_full_name (&class->byval_arg);
1621 if (class->generic_class && !class->generic_class->context.class_inst->is_open) {
1622 generic_id = class->generic_class->context.class_inst->id;
1623 g_assert (generic_id != 0);
1627 printf ("Interface: assigned id %d to %s|%s|%d\n", iid, class->image->name, type_name, generic_id);
1631 g_assert (iid <= 65535);
1636 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
1641 for (i = 0; i < klass->interface_count; i++) {
1642 ic = klass->interfaces [i];
1645 *res = g_ptr_array_new ();
1646 g_ptr_array_add (*res, ic);
1647 mono_class_init (ic);
1649 collect_implemented_interfaces_aux (ic, res);
1654 mono_class_get_implemented_interfaces (MonoClass *klass)
1656 GPtrArray *res = NULL;
1658 collect_implemented_interfaces_aux (klass, &res);
1662 typedef struct _IOffsetInfo IOffsetInfo;
1663 struct _IOffsetInfo {
1667 int data [MONO_ZERO_LEN_ARRAY];
1670 static IOffsetInfo *cached_offset_info = NULL;
1671 static int next_offset_info_size = 128;
1674 cache_interface_offsets (int max_iid, int *data)
1676 IOffsetInfo *cached_info;
1679 for (cached_info = cached_offset_info; cached_info; cached_info = cached_info->next) {
1680 cached = cached_info->data;
1681 while (cached < cached_info->data + cached_info->size && *cached) {
1682 if (*cached == max_iid) {
1683 int i, matched = TRUE;
1685 for (i = 0; i < max_iid; ++i) {
1686 if (cached [i] != data [i]) {
1695 cached += *cached + 1;
1699 /* find a free slot */
1700 for (cached_info = cached_offset_info; cached_info; cached_info = cached_info->next) {
1701 if (cached_info->size - cached_info->next_free >= max_iid + 1) {
1702 cached = &cached_info->data [cached_info->next_free];
1703 *cached++ = max_iid;
1704 memcpy (cached, data, max_iid * sizeof (int));
1705 cached_info->next_free += max_iid + 1;
1709 /* allocate a new chunk */
1710 if (max_iid + 1 < next_offset_info_size) {
1711 new_size = next_offset_info_size;
1712 if (next_offset_info_size < 4096)
1713 next_offset_info_size += next_offset_info_size >> 2;
1715 new_size = max_iid + 1;
1717 cached_info = g_malloc0 (sizeof (IOffsetInfo) + sizeof (int) * new_size);
1718 cached_info->size = new_size;
1719 /*g_print ("allocated %d offset entries at %p (total: %d)\n", new_size, cached_info->data, offset_info_total_size);*/
1720 cached = &cached_info->data [0];
1721 *cached++ = max_iid;
1722 memcpy (cached, data, max_iid * sizeof (int));
1723 cached_info->next_free += max_iid + 1;
1724 cached_info->next = cached_offset_info;
1725 cached_offset_info = cached_info;
1730 compare_interface_ids (const void *p_key, const void *p_element) {
1731 const MonoClass *key = p_key;
1732 const MonoClass *element = *(MonoClass**) p_element;
1734 return (key->interface_id - element->interface_id);
1738 mono_class_interface_offset (MonoClass *klass, MonoClass *itf) {
1739 MonoClass **result = bsearch (
1741 klass->interfaces_packed,
1742 klass->interface_offsets_count,
1743 sizeof (MonoClass *),
1744 compare_interface_ids);
1746 return klass->interface_offsets_packed [result - (klass->interfaces_packed)];
1753 print_implemented_interfaces (MonoClass *klass) {
1754 GPtrArray *ifaces = NULL;
1756 int ancestor_level = 0;
1758 printf ("Packed interface table for class %s has size %d\n", klass->name, klass->interface_offsets_count);
1759 for (i = 0; i < klass->interface_offsets_count; i++)
1760 printf (" [%d][UUID %d][SLOT %d] interface %s\n", i,
1761 klass->interfaces_packed [i]->interface_id,
1762 klass->interface_offsets_packed [i],
1763 klass->interfaces_packed [i]->name );
1764 printf ("Interface flags: ");
1765 for (i = 0; i <= klass->max_interface_id; i++)
1766 if (MONO_CLASS_IMPLEMENTS_INTERFACE (klass, i))
1767 printf ("(%d,T)", i);
1769 printf ("(%d,F)", i);
1771 printf ("Dump interface flags:");
1772 for (i = 0; i < ((((klass->max_interface_id + 1) >> 3)) + (((klass->max_interface_id + 1) & 7)? 1 :0)); i++)
1773 printf (" %02X", klass->interface_bitmap [i]);
1775 while (klass != NULL) {
1776 printf ("[LEVEL %d] Implemented interfaces by class %s:\n", ancestor_level, klass->name);
1777 ifaces = mono_class_get_implemented_interfaces (klass);
1779 for (i = 0; i < ifaces->len; i++) {
1780 MonoClass *ic = g_ptr_array_index (ifaces, i);
1781 printf (" [UIID %d] interface %s\n", ic->interface_id, ic->name);
1783 g_ptr_array_free (ifaces, TRUE);
1786 klass = klass->parent;
1790 /* this won't be needed once bug #325495 is completely fixed
1791 * though we'll need something similar to know which interfaces to allow
1792 * in arrays when they'll be lazyly created
1795 get_implicit_generic_array_interfaces (MonoClass *class, int *num, int *is_enumerator)
1797 MonoClass *eclass = class->element_class;
1798 static MonoClass* generic_icollection_class = NULL;
1799 static MonoClass* generic_ienumerable_class = NULL;
1800 static MonoClass* generic_ienumerator_class = NULL;
1801 MonoClass *fclass = NULL;
1802 MonoClass **interfaces = NULL;
1803 int i, interface_count, real_count;
1805 gboolean internal_enumerator;
1806 gboolean eclass_is_valuetype;
1808 if (!mono_defaults.generic_ilist_class) {
1812 internal_enumerator = FALSE;
1813 eclass_is_valuetype = FALSE;
1814 if (class->byval_arg.type != MONO_TYPE_SZARRAY) {
1815 if (class->generic_class && class->nested_in == mono_defaults.array_class && strcmp (class->name, "InternalEnumerator`1") == 0) {
1817 * For a Enumerator<T[]> we need to get the list of interfaces for T.
1819 eclass = mono_class_from_mono_type (class->generic_class->context.class_inst->type_argv [0]);
1820 eclass = eclass->element_class;
1821 internal_enumerator = TRUE;
1822 *is_enumerator = TRUE;
1830 * with this non-lazy impl we can't implement all the interfaces so we do just the minimal stuff
1831 * for deep levels of arrays of arrays (string[][] has all the interfaces, string[][][] doesn't)
1833 all_interfaces = eclass->rank && eclass->element_class->rank? FALSE: TRUE;
1835 if (!generic_icollection_class) {
1836 generic_icollection_class = mono_class_from_name (mono_defaults.corlib,
1837 "System.Collections.Generic", "ICollection`1");
1838 generic_ienumerable_class = mono_class_from_name (mono_defaults.corlib,
1839 "System.Collections.Generic", "IEnumerable`1");
1840 generic_ienumerator_class = mono_class_from_name (mono_defaults.corlib,
1841 "System.Collections.Generic", "IEnumerator`1");
1845 * Arrays in 2.0 need to implement a number of generic interfaces
1846 * (IList`1, ICollection`1, IEnumerable`1 for a number of types depending
1847 * on the element class). We collect the types needed to build the
1848 * instantiations in interfaces at intervals of 3, because 3 are
1849 * the generic interfaces needed to implement.
1851 if (eclass->valuetype) {
1852 if (eclass == mono_defaults.int16_class)
1853 fclass = mono_defaults.uint16_class;
1854 else if (eclass == mono_defaults.uint16_class)
1855 fclass = mono_defaults.int16_class;
1856 else if (eclass == mono_defaults.int32_class)
1857 fclass = mono_defaults.uint32_class;
1858 else if (eclass == mono_defaults.uint32_class)
1859 fclass = mono_defaults.int32_class;
1860 else if (eclass == mono_defaults.int64_class)
1861 fclass = mono_defaults.uint64_class;
1862 else if (eclass == mono_defaults.uint64_class)
1863 fclass = mono_defaults.int64_class;
1864 else if (eclass == mono_defaults.byte_class)
1865 fclass = mono_defaults.sbyte_class;
1866 else if (eclass == mono_defaults.sbyte_class)
1867 fclass = mono_defaults.byte_class;
1869 /* No additional interfaces for other value types */
1874 /* IList, ICollection, IEnumerable */
1875 real_count = interface_count = 3;
1876 interfaces = g_malloc0 (sizeof (MonoClass*) * interface_count);
1877 interfaces [0] = fclass;
1878 eclass_is_valuetype = TRUE;
1881 int idepth = eclass->idepth;
1882 if (!internal_enumerator)
1884 interface_count = all_interfaces? eclass->interface_offsets_count: eclass->interface_count;
1885 /* we add object for interfaces and the supertypes for the other
1886 * types. The last of the supertypes is the element class itself which we
1887 * already created the explicit interfaces for (so we include it for IEnumerator
1888 * and exclude it for arrays).
1890 if (MONO_CLASS_IS_INTERFACE (eclass))
1893 interface_count += idepth;
1894 /* IList, ICollection, IEnumerable */
1895 interface_count *= 3;
1896 real_count = interface_count;
1897 if (internal_enumerator)
1898 real_count += idepth + eclass->interface_offsets_count;
1899 interfaces = g_malloc0 (sizeof (MonoClass*) * real_count);
1900 if (MONO_CLASS_IS_INTERFACE (eclass)) {
1901 interfaces [0] = mono_defaults.object_class;
1905 for (i = 0; i < idepth; i++) {
1906 mono_class_init (eclass->supertypes [i]);
1907 interfaces [j] = eclass->supertypes [i];
1911 if (all_interfaces) {
1912 for (i = 0; i < eclass->interface_offsets_count; i++) {
1913 interfaces [j] = eclass->interfaces_packed [i];
1917 for (i = 0; i < eclass->interface_count; i++) {
1918 interfaces [j] = eclass->interfaces [i];
1924 /* instantiate the generic interfaces */
1925 for (i = 0; i < interface_count; i += 3) {
1927 MonoClass *iface = interfaces [i];
1929 args [0] = &iface->byval_arg;
1930 interfaces [i] = mono_class_bind_generic_parameters (
1931 mono_defaults.generic_ilist_class, 1, args, FALSE);
1932 //g_print ("%s implements %s\n", class->name, mono_type_get_name_full (&interfaces [i]->byval_arg, 0));
1933 args [0] = &iface->byval_arg;
1934 interfaces [i + 1] = mono_class_bind_generic_parameters (
1935 generic_icollection_class, 1, args, FALSE);
1936 args [0] = &iface->byval_arg;
1937 interfaces [i + 2] = mono_class_bind_generic_parameters (
1938 generic_ienumerable_class, 1, args, FALSE);
1939 //g_print ("%s implements %s\n", class->name, mono_type_get_name_full (&interfaces [i + 1]->byval_arg, 0));
1940 //g_print ("%s implements %s\n", class->name, mono_type_get_name_full (&interfaces [i + 2]->byval_arg, 0));
1942 if (internal_enumerator) {
1944 /* instantiate IEnumerator<iface> */
1945 for (i = 0; i < interface_count; i++) {
1947 MonoClass *iface = interfaces [i];
1949 args [0] = &iface->byval_arg;
1950 interfaces [i] = mono_class_bind_generic_parameters (
1951 generic_ienumerator_class, 1, args, FALSE);
1952 /*g_print ("%s implements %s\n", class->name, mono_type_get_name_full (&interfaces [i]->byval_arg, 0));*/
1954 if (!eclass_is_valuetype) {
1955 j = interface_count;
1956 for (i = 0; i < eclass->idepth; i++) {
1958 args [0] = &eclass->supertypes [i]->byval_arg;
1959 interfaces [j] = mono_class_bind_generic_parameters (
1960 generic_ienumerator_class, 1, args, FALSE);
1961 /*g_print ("%s implements %s\n", class->name, mono_type_get_name_full (&interfaces [i]->byval_arg, 0));*/
1964 for (i = 0; i < eclass->interface_offsets_count; i++) {
1965 MonoClass *iface = eclass->interfaces_packed [i];
1967 args [0] = &iface->byval_arg;
1968 interfaces [j] = mono_class_bind_generic_parameters (
1969 generic_ienumerator_class, 1, args, FALSE);
1970 /*g_print ("%s implements %s\n", class->name, mono_type_get_name_full (&interfaces [i]->byval_arg, 0));*/
1980 * LOCKING: this is supposed to be called with the loader lock held.
1983 setup_interface_offsets (MonoClass *class, int cur_slot)
1987 MonoClass **interfaces_full;
1988 int *interface_offsets_full;
1990 int interface_offsets_count;
1991 MonoClass **array_interfaces;
1992 int num_array_interfaces;
1993 int is_enumerator = FALSE;
1996 * get the implicit generic interfaces for either the arrays or for System.Array/InternalEnumerator<T>
1997 * implicit innterfaces have the property that they are assigned the same slot in the vtables
1998 * for compatible interfaces
2000 array_interfaces = get_implicit_generic_array_interfaces (class, &num_array_interfaces, &is_enumerator);
2002 /* compute maximum number of slots and maximum interface id */
2004 for (k = class; k ; k = k->parent) {
2005 for (i = 0; i < k->interface_count; i++) {
2006 ic = k->interfaces [i];
2009 mono_class_init (ic);
2011 if (max_iid < ic->interface_id)
2012 max_iid = ic->interface_id;
2014 ifaces = mono_class_get_implemented_interfaces (k);
2016 for (i = 0; i < ifaces->len; ++i) {
2017 ic = g_ptr_array_index (ifaces, i);
2018 if (max_iid < ic->interface_id)
2019 max_iid = ic->interface_id;
2021 g_ptr_array_free (ifaces, TRUE);
2024 for (i = 0; i < num_array_interfaces; ++i) {
2025 ic = array_interfaces [i];
2026 mono_class_init (ic);
2027 if (max_iid < ic->interface_id)
2028 max_iid = ic->interface_id;
2031 if (MONO_CLASS_IS_INTERFACE (class)) {
2032 if (max_iid < class->interface_id)
2033 max_iid = class->interface_id;
2035 class->max_interface_id = max_iid;
2036 /* compute vtable offset for interfaces */
2037 interfaces_full = g_malloc (sizeof (MonoClass*) * (max_iid + 1));
2038 interface_offsets_full = g_malloc (sizeof (int) * (max_iid + 1));
2040 for (i = 0; i <= max_iid; i++) {
2041 interfaces_full [i] = NULL;
2042 interface_offsets_full [i] = -1;
2045 ifaces = mono_class_get_implemented_interfaces (class);
2047 for (i = 0; i < ifaces->len; ++i) {
2048 ic = g_ptr_array_index (ifaces, i);
2049 interfaces_full [ic->interface_id] = ic;
2050 interface_offsets_full [ic->interface_id] = cur_slot;
2051 cur_slot += ic->method.count;
2053 g_ptr_array_free (ifaces, TRUE);
2056 for (k = class->parent; k ; k = k->parent) {
2057 ifaces = mono_class_get_implemented_interfaces (k);
2059 for (i = 0; i < ifaces->len; ++i) {
2060 ic = g_ptr_array_index (ifaces, i);
2062 if (interface_offsets_full [ic->interface_id] == -1) {
2063 int io = mono_class_interface_offset (k, ic);
2067 interfaces_full [ic->interface_id] = ic;
2068 interface_offsets_full [ic->interface_id] = io;
2071 g_ptr_array_free (ifaces, TRUE);
2075 if (MONO_CLASS_IS_INTERFACE (class)) {
2076 interfaces_full [class->interface_id] = class;
2077 interface_offsets_full [class->interface_id] = cur_slot;
2080 if (num_array_interfaces) {
2081 if (is_enumerator) {
2082 int ienumerator_offset;
2083 g_assert (strcmp (class->interfaces [0]->name, "IEnumerator`1") == 0);
2084 ienumerator_offset = interface_offsets_full [class->interfaces [0]->interface_id];
2085 for (i = 0; i < num_array_interfaces; ++i) {
2086 ic = array_interfaces [i];
2087 interfaces_full [ic->interface_id] = ic;
2088 if (strcmp (ic->name, "IEnumerator`1") == 0)
2089 interface_offsets_full [ic->interface_id] = ienumerator_offset;
2091 g_assert_not_reached ();
2092 /*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);*/
2095 int ilist_offset, icollection_offset, ienumerable_offset;
2096 g_assert (strcmp (class->interfaces [0]->name, "IList`1") == 0);
2097 g_assert (strcmp (class->interfaces [0]->interfaces [0]->name, "ICollection`1") == 0);
2098 g_assert (strcmp (class->interfaces [0]->interfaces [1]->name, "IEnumerable`1") == 0);
2099 ilist_offset = interface_offsets_full [class->interfaces [0]->interface_id];
2100 icollection_offset = interface_offsets_full [class->interfaces [0]->interfaces [0]->interface_id];
2101 ienumerable_offset = interface_offsets_full [class->interfaces [0]->interfaces [1]->interface_id];
2102 g_assert (ilist_offset >= 0 && icollection_offset >= 0 && ienumerable_offset >= 0);
2103 for (i = 0; i < num_array_interfaces; ++i) {
2104 ic = array_interfaces [i];
2105 interfaces_full [ic->interface_id] = ic;
2106 if (ic->generic_class->container_class == mono_defaults.generic_ilist_class)
2107 interface_offsets_full [ic->interface_id] = ilist_offset;
2108 else if (strcmp (ic->name, "ICollection`1") == 0)
2109 interface_offsets_full [ic->interface_id] = icollection_offset;
2110 else if (strcmp (ic->name, "IEnumerable`1") == 0)
2111 interface_offsets_full [ic->interface_id] = ienumerable_offset;
2113 g_assert_not_reached ();
2114 /*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);*/
2119 for (interface_offsets_count = 0, i = 0; i <= max_iid; i++) {
2120 if (interface_offsets_full [i] != -1) {
2121 interface_offsets_count ++;
2124 class->interface_offsets_count = interface_offsets_count;
2125 class->interfaces_packed = mono_mempool_alloc (class->image->mempool, sizeof (MonoClass*) * interface_offsets_count);
2126 class->interface_offsets_packed = mono_mempool_alloc (class->image->mempool, sizeof (int) * interface_offsets_count);
2127 class->interface_bitmap = mono_mempool_alloc0 (class->image->mempool, (sizeof (guint8) * ((max_iid + 1) >> 3)) + (((max_iid + 1) & 7)? 1 :0));
2128 for (interface_offsets_count = 0, i = 0; i <= max_iid; i++) {
2129 if (interface_offsets_full [i] != -1) {
2130 class->interface_bitmap [i >> 3] |= (1 << (i & 7));
2131 class->interfaces_packed [interface_offsets_count] = interfaces_full [i];
2132 class->interface_offsets_packed [interface_offsets_count] = interface_offsets_full [i];
2133 /*if (num_array_interfaces)
2134 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]);*/
2135 interface_offsets_count ++;
2139 g_free (interfaces_full);
2140 g_free (interface_offsets_full);
2141 g_free (array_interfaces);
2143 //printf ("JUST DONE: ");
2144 //print_implemented_interfaces (class);
2150 * Setup interface offsets for interfaces. Used by Ref.Emit.
2153 mono_class_setup_interface_offsets (MonoClass *class)
2155 mono_loader_lock ();
2157 setup_interface_offsets (class, 0);
2159 mono_loader_unlock ();
2163 mono_class_setup_vtable (MonoClass *class)
2165 MonoMethod **overrides;
2166 MonoGenericContext *context;
2175 mono_class_setup_methods (class);
2177 if (MONO_CLASS_IS_INTERFACE (class))
2180 mono_loader_lock ();
2182 if (class->vtable) {
2183 mono_loader_unlock ();
2187 mono_stats.generic_vtable_count ++;
2189 if (class->generic_class) {
2190 context = mono_class_get_context (class);
2191 type_token = class->generic_class->container_class->type_token;
2193 context = (MonoGenericContext *) class->generic_container;
2194 type_token = class->type_token;
2197 if (class->image->dynamic) {
2198 if (class->generic_class) {
2199 MonoClass *gklass = class->generic_class->container_class;
2201 mono_reflection_get_dynamic_overrides (gklass, &overrides, &onum);
2202 for (i = 0; i < onum; ++i) {
2203 MonoMethod *override = overrides [(i * 2) + 1];
2204 MonoMethod *inflated = NULL;
2207 for (j = 0; j < class->method.count; ++j) {
2208 if (gklass->methods [j] == override) {
2209 inflated = class->methods [j];
2213 g_assert (inflated);
2215 overrides [(i * 2) + 1] = inflated;
2218 mono_reflection_get_dynamic_overrides (class, &overrides, &onum);
2221 /* The following call fails if there are missing methods in the type */
2222 ok = mono_class_get_overrides_full (class->image, type_token, &overrides, &onum, context);
2226 mono_class_setup_vtable_general (class, overrides, onum);
2230 mono_loader_unlock ();
2236 check_core_clr_override_method (MonoClass *class, MonoMethod *override, MonoMethod *base)
2238 MonoSecurityCoreCLRLevel override_level = mono_security_core_clr_method_level (override, FALSE);
2239 MonoSecurityCoreCLRLevel base_level = mono_security_core_clr_method_level (base, FALSE);
2241 if (override_level != base_level && base_level == MONO_SECURITY_CORE_CLR_CRITICAL) {
2242 class->exception_type = MONO_EXCEPTION_TYPE_LOAD;
2243 class->exception_data = NULL;
2248 * LOCKING: this is supposed to be called with the loader lock held.
2251 mono_class_setup_vtable_general (MonoClass *class, MonoMethod **overrides, int onum)
2254 MonoMethod **vtable;
2255 int i, max_vtsize = 0, max_iid, cur_slot = 0;
2256 GPtrArray *ifaces, *pifaces = NULL;
2257 GHashTable *override_map = NULL;
2258 gboolean security_enabled = mono_is_security_manager_active ();
2263 ifaces = mono_class_get_implemented_interfaces (class);
2265 for (i = 0; i < ifaces->len; i++) {
2266 MonoClass *ic = g_ptr_array_index (ifaces, i);
2267 max_vtsize += ic->method.count;
2269 g_ptr_array_free (ifaces, TRUE);
2272 if (class->parent) {
2273 mono_class_init (class->parent);
2274 mono_class_setup_vtable (class->parent);
2275 max_vtsize += class->parent->vtable_size;
2276 cur_slot = class->parent->vtable_size;
2279 max_vtsize += class->method.count;
2281 vtable = alloca (sizeof (gpointer) * max_vtsize);
2282 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
2284 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
2286 cur_slot = setup_interface_offsets (class, cur_slot);
2287 max_iid = class->max_interface_id;
2289 if (class->parent && class->parent->vtable_size)
2290 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
2292 /* override interface methods */
2293 for (i = 0; i < onum; i++) {
2294 MonoMethod *decl = overrides [i*2];
2295 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
2297 mono_class_setup_methods (decl->klass);
2298 g_assert (decl->slot != -1);
2299 dslot = decl->slot + mono_class_interface_offset (class, decl->klass);
2300 vtable [dslot] = overrides [i*2 + 1];
2301 vtable [dslot]->slot = dslot;
2303 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
2305 g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
2307 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR)
2308 check_core_clr_override_method (class, vtable [dslot], decl);
2312 for (k = class; k ; k = k->parent) {
2315 ifaces = mono_class_get_implemented_interfaces (k);
2317 nifaces = ifaces->len;
2318 if (k->generic_class) {
2319 pifaces = mono_class_get_implemented_interfaces (
2320 k->generic_class->container_class);
2321 g_assert (pifaces && (pifaces->len == nifaces));
2324 for (i = 0; i < nifaces; i++) {
2325 MonoClass *pic = NULL;
2328 ic = g_ptr_array_index (ifaces, i);
2330 pic = g_ptr_array_index (pifaces, i);
2331 g_assert (ic->interface_id <= k->max_interface_id);
2332 io = mono_class_interface_offset (k, ic);
2335 g_assert (io <= max_vtsize);
2338 mono_class_setup_methods (ic);
2339 for (l = 0; l < ic->method.count; l++) {
2340 MonoMethod *im = ic->methods [l];
2342 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
2345 for (j = 0; j < class->method.count; ++j) {
2346 MonoMethod *cm = class->methods [j];
2347 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
2348 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
2349 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
2351 if (!strcmp(cm->name, im->name) &&
2352 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
2354 /* CAS - SecurityAction.InheritanceDemand on interface */
2355 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
2356 mono_secman_inheritancedemand_method (cm, im);
2359 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR)
2360 check_core_clr_override_method (class, cm, im);
2362 g_assert (io + l <= max_vtsize);
2363 vtable [io + l] = cm;
2368 /* already implemented */
2369 if (io >= k->vtable_size)
2373 // Override methods with the same fully qualified name
2374 for (l = 0; l < ic->method.count; l++) {
2375 MonoMethod *im = ic->methods [l];
2376 char *qname, *fqname, *cname, *the_cname;
2379 if (vtable [io + l])
2383 the_cname = mono_type_get_name_full (&pic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
2387 cname = (char*)ic->name;
2390 qname = g_strconcat (cname, ".", im->name, NULL);
2391 if (ic->name_space && ic->name_space [0])
2392 fqname = g_strconcat (ic->name_space, ".", cname, ".", im->name, NULL);
2396 for (k1 = class; k1; k1 = k1->parent) {
2397 for (j = 0; j < k1->method.count; ++j) {
2398 MonoMethod *cm = k1->methods [j];
2400 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
2403 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
2404 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im)) &&
2405 ((vtable [io + l] == NULL) || mono_class_is_subclass_of (cm->klass, vtable [io + l]->klass, FALSE))) {
2407 /* CAS - SecurityAction.InheritanceDemand on interface */
2408 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
2409 mono_secman_inheritancedemand_method (cm, im);
2412 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR)
2413 check_core_clr_override_method (class, cm, im);
2415 g_assert (io + l <= max_vtsize);
2416 vtable [io + l] = cm;
2426 // Override methods with the same name
2427 for (l = 0; l < ic->method.count; l++) {
2428 MonoMethod *im = ic->methods [l];
2431 g_assert (io + l <= max_vtsize);
2433 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
2436 for (k1 = class; k1; k1 = k1->parent) {
2437 for (j = 0; j < k1->method.count; ++j) {
2438 MonoMethod *cm = k1->methods [j];
2440 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
2441 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
2444 if (!strcmp(cm->name, im->name) &&
2445 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
2447 /* CAS - SecurityAction.InheritanceDemand on interface */
2448 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
2449 mono_secman_inheritancedemand_method (cm, im);
2452 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR)
2453 check_core_clr_override_method (class, cm, im);
2455 g_assert (io + l <= max_vtsize);
2456 vtable [io + l] = cm;
2461 g_assert (io + l <= max_vtsize);
2462 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
2467 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
2468 for (l = 0; l < ic->method.count; l++) {
2470 MonoMethod *im = ic->methods [l];
2471 if (im->flags & METHOD_ATTRIBUTE_STATIC)
2473 g_assert (io + l <= max_vtsize);
2476 * If one of our parents already implements this interface
2477 * we can inherit the implementation.
2479 if (!(vtable [io + l])) {
2480 MonoClass *parent = class->parent;
2482 for (; parent; parent = parent->parent) {
2483 if (MONO_CLASS_IMPLEMENTS_INTERFACE (parent, ic->interface_id) &&
2485 vtable [io + l] = parent->vtable [mono_class_interface_offset (parent, ic) + l];
2490 if (!(vtable [io + l])) {
2491 for (j = 0; j < onum; ++j) {
2492 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
2493 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
2495 msig = mono_signature_get_desc (mono_method_signature (im), FALSE);
2496 printf ("no implementation for interface method %s::%s(%s) in class %s.%s\n",
2497 mono_type_get_name (&ic->byval_arg), im->name, msig, class->name_space, class->name);
2499 for (j = 0; j < class->method.count; ++j) {
2500 MonoMethod *cm = class->methods [j];
2501 msig = mono_signature_get_desc (mono_method_signature (cm), TRUE);
2503 printf ("METHOD %s(%s)\n", cm->name, msig);
2507 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
2510 g_ptr_array_free (ifaces, TRUE);
2512 g_hash_table_destroy (override_map);
2519 for (l = 0; l < ic->method.count; l++) {
2520 MonoMethod *im = vtable [io + l];
2523 g_assert (io + l <= max_vtsize);
2525 /* FIXME: why do we need this ? */
2527 /* g_assert_not_reached (); */
2533 g_ptr_array_free (ifaces, TRUE);
2536 for (i = 0; i < class->method.count; ++i) {
2539 cm = class->methods [i];
2542 * Non-virtual method have no place in the vtable.
2543 * This also catches static methods (since they are not virtual).
2545 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
2549 * If the method is REUSE_SLOT, we must check in the
2550 * base class for a method to override.
2552 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
2554 for (k = class->parent; k ; k = k->parent) {
2556 for (j = 0; j < k->method.count; ++j) {
2557 MonoMethod *m1 = k->methods [j];
2558 MonoMethodSignature *cmsig, *m1sig;
2560 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
2563 cmsig = mono_method_signature (cm);
2564 m1sig = mono_method_signature (m1);
2566 if (!cmsig || !m1sig) {
2567 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
2571 if (!strcmp(cm->name, m1->name) &&
2572 mono_metadata_signature_equal (cmsig, m1sig)) {
2574 /* CAS - SecurityAction.InheritanceDemand */
2575 if (security_enabled && (m1->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
2576 mono_secman_inheritancedemand_method (cm, m1);
2579 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR)
2580 check_core_clr_override_method (class, cm, m1);
2582 slot = k->methods [j]->slot;
2583 g_assert (cm->slot < max_vtsize);
2585 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
2586 g_hash_table_insert (override_map, m1, cm);
2598 cm->slot = cur_slot++;
2600 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
2601 vtable [cm->slot] = cm;
2604 /* override non interface methods */
2605 for (i = 0; i < onum; i++) {
2606 MonoMethod *decl = overrides [i*2];
2607 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
2608 g_assert (decl->slot != -1);
2609 vtable [decl->slot] = overrides [i*2 + 1];
2610 overrides [i * 2 + 1]->slot = decl->slot;
2612 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
2613 g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
2615 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR)
2616 check_core_clr_override_method (class, vtable [decl->slot], decl);
2621 * If a method occupies more than one place in the vtable, and it is
2622 * overriden, then change the other occurances too.
2625 for (i = 0; i < max_vtsize; ++i)
2627 MonoMethod *cm = g_hash_table_lookup (override_map, vtable [i]);
2632 g_hash_table_destroy (override_map);
2635 if (class->generic_class) {
2636 MonoClass *gklass = class->generic_class->container_class;
2638 mono_class_init (gklass);
2640 class->vtable_size = MAX (gklass->vtable_size, cur_slot);
2642 class->vtable_size = cur_slot;
2644 /* Try to share the vtable with our parent. */
2645 if (class->parent && (class->parent->vtable_size == class->vtable_size) && (memcmp (class->parent->vtable, vtable, sizeof (gpointer) * class->vtable_size) == 0)) {
2646 class->vtable = class->parent->vtable;
2648 class->vtable = mono_mempool_alloc0 (class->image->mempool, sizeof (gpointer) * class->vtable_size);
2649 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
2652 if (mono_print_vtable) {
2655 print_implemented_interfaces (class);
2657 for (i = 0; i <= max_iid; i++)
2658 if (MONO_CLASS_IMPLEMENTS_INTERFACE (class, i))
2661 printf ("VTable %s (vtable entries = %d, interfaces = %d)\n", mono_type_full_name (&class->byval_arg),
2662 class->vtable_size, icount);
2664 for (i = 0; i < class->vtable_size; ++i) {
2669 printf (" slot assigned: %03d, slot index: %03d %s\n", i, cm->slot,
2670 mono_method_full_name (cm, TRUE));
2676 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
2677 class->name, max_iid);
2679 for (i = 0; i < class->interface_count; i++) {
2680 ic = class->interfaces [i];
2681 printf (" slot offset: %03d, method count: %03d, iid: %03d %s\n",
2682 mono_class_interface_offset (class, ic),
2683 ic->method.count, ic->interface_id, mono_type_full_name (&ic->byval_arg));
2686 for (k = class->parent; k ; k = k->parent) {
2687 for (i = 0; i < k->interface_count; i++) {
2688 ic = k->interfaces [i];
2689 printf (" slot offset: %03d, method count: %03d, iid: %03d %s\n",
2690 mono_class_interface_offset (class, ic),
2691 ic->method.count, ic->interface_id, mono_type_full_name (&ic->byval_arg));
2698 static MonoMethod *default_ghc = NULL;
2699 static MonoMethod *default_finalize = NULL;
2700 static int finalize_slot = -1;
2701 static int ghc_slot = -1;
2704 initialize_object_slots (MonoClass *class)
2709 if (class == mono_defaults.object_class) {
2710 mono_class_setup_vtable (class);
2711 for (i = 0; i < class->vtable_size; ++i) {
2712 MonoMethod *cm = class->vtable [i];
2714 if (!strcmp (cm->name, "GetHashCode"))
2716 else if (!strcmp (cm->name, "Finalize"))
2720 g_assert (ghc_slot > 0);
2721 default_ghc = class->vtable [ghc_slot];
2723 g_assert (finalize_slot > 0);
2724 default_finalize = class->vtable [finalize_slot];
2729 g_list_prepend_mempool (GList* l, MonoMemPool* mp, gpointer datum)
2731 GList* n = mono_mempool_alloc (mp, sizeof (GList));
2739 MonoMethod *array_method;
2741 } GenericArrayMethodInfo;
2743 static int generic_array_method_num = 0;
2744 static GenericArrayMethodInfo *generic_array_method_info = NULL;
2747 generic_array_methods (MonoClass *class)
2749 int i, count_generic = 0;
2750 GList *list = NULL, *tmp;
2751 if (generic_array_method_num)
2752 return generic_array_method_num;
2753 mono_class_setup_methods (class->parent);
2754 for (i = 0; i < class->parent->method.count; i++) {
2755 MonoMethod *m = class->parent->methods [i];
2756 if (!strncmp (m->name, "InternalArray__", 15)) {
2758 list = g_list_prepend (list, m);
2761 list = g_list_reverse (list);
2762 generic_array_method_info = g_malloc (sizeof (GenericArrayMethodInfo) * count_generic);
2764 for (tmp = list; tmp; tmp = tmp->next) {
2765 const char *mname, *iname;
2767 MonoMethod *m = tmp->data;
2768 generic_array_method_info [i].array_method = m;
2769 if (!strncmp (m->name, "InternalArray__ICollection_", 27)) {
2770 iname = "System.Collections.Generic.ICollection`1.";
2771 mname = m->name + 27;
2772 } else if (!strncmp (m->name, "InternalArray__IEnumerable_", 27)) {
2773 iname = "System.Collections.Generic.IEnumerable`1.";
2774 mname = m->name + 27;
2775 } else if (!strncmp (m->name, "InternalArray__", 15)) {
2776 iname = "System.Collections.Generic.IList`1.";
2777 mname = m->name + 15;
2779 g_assert_not_reached ();
2782 name = mono_mempool_alloc (mono_defaults.corlib->mempool, strlen (iname) + strlen (mname) + 1);
2783 strcpy (name, iname);
2784 strcpy (name + strlen (iname), mname);
2785 generic_array_method_info [i].name = name;
2788 /*g_print ("array generic methods: %d\n", count_generic);*/
2790 generic_array_method_num = count_generic;
2791 return generic_array_method_num;
2795 setup_generic_array_ifaces (MonoClass *class, MonoClass *iface, int pos)
2797 MonoGenericContext tmp_context;
2800 tmp_context.class_inst = NULL;
2801 tmp_context.method_inst = iface->generic_class->context.class_inst;
2802 //g_print ("setting up array interface: %s\n", mono_type_get_name_full (&iface->byval_arg, 0));
2804 for (i = 0; i < generic_array_method_num; i++) {
2805 MonoMethod *m = generic_array_method_info [i].array_method;
2806 MonoMethod *inflated;
2808 inflated = mono_class_inflate_generic_method (m, &tmp_context);
2809 class->methods [pos++] = mono_marshal_get_generic_array_helper (class, iface, generic_array_method_info [i].name, inflated);
2814 create_array_method (MonoClass *class, const char *name, MonoMethodSignature *sig)
2818 method = (MonoMethod *) mono_mempool_alloc0 (class->image->mempool, sizeof (MonoMethodPInvoke));
2819 method->klass = class;
2820 method->flags = METHOD_ATTRIBUTE_PUBLIC;
2821 method->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
2822 method->signature = sig;
2823 method->name = name;
2826 if (name [0] == '.') {
2827 method->flags |= METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
2829 method->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
2835 concat_two_strings_with_zero (MonoMemPool *pool, const char *s1, const char *s2)
2837 int len = strlen (s1) + strlen (s2) + 2;
2838 char *s = mono_mempool_alloc (pool, len);
2841 result = g_snprintf (s, len, "%s%c%s", s1, '\0', s2);
2842 g_assert (result == len - 1);
2848 set_failure_from_loader_error (MonoClass *class, MonoLoaderError *error)
2850 class->exception_type = error->exception_type;
2852 switch (error->exception_type) {
2853 case MONO_EXCEPTION_TYPE_LOAD:
2854 class->exception_data = concat_two_strings_with_zero (class->image->mempool, error->class_name, error->assembly_name);
2857 case MONO_EXCEPTION_MISSING_METHOD:
2858 class->exception_data = concat_two_strings_with_zero (class->image->mempool, error->class_name, error->member_name);
2861 case MONO_EXCEPTION_MISSING_FIELD: {
2862 const char *name_space = error->klass->name_space ? error->klass->name_space : NULL;
2863 const char *class_name;
2866 class_name = g_strdup_printf ("%s.%s", name_space, error->klass->name);
2868 class_name = error->klass->name;
2870 class->exception_data = concat_two_strings_with_zero (class->image->mempool, class_name, error->member_name);
2873 g_free ((void*)class_name);
2877 case MONO_EXCEPTION_FILE_NOT_FOUND: {
2880 if (error->ref_only)
2881 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.";
2883 msg = "Could not load file or assembly '%s' or one of its dependencies.";
2885 class->exception_data = concat_two_strings_with_zero (class->image->mempool, msg, error->assembly_name);
2890 g_assert_not_reached ();
2895 check_core_clr_inheritance (MonoClass *class)
2897 MonoSecurityCoreCLRLevel class_level, parent_level;
2898 MonoClass *parent = class->parent;
2903 class_level = mono_security_core_clr_class_level (class);
2904 parent_level = mono_security_core_clr_class_level (parent);
2906 if (class_level < parent_level) {
2907 class->exception_type = MONO_EXCEPTION_TYPE_LOAD;
2908 class->exception_data = NULL;
2914 * @class: the class to initialize
2916 * compute the instance_size, class_size and other infos that cannot be
2917 * computed at mono_class_get() time. Also compute a generic vtable and
2918 * the method slot numbers. We use this infos later to create a domain
2921 * Returns TRUE on success or FALSE if there was a problem in loading
2922 * the type (incorrect assemblies, missing assemblies, methods, etc).
2925 mono_class_init (MonoClass *class)
2928 MonoCachedClassInfo cached_info;
2929 gboolean has_cached_info;
2930 int class_init_ok = TRUE;
2935 return class->exception_type == MONO_EXCEPTION_NONE;
2937 /*g_print ("Init class %s\n", class->name);*/
2939 /* We do everything inside the lock to prevent races */
2940 mono_loader_lock ();
2942 if (class->inited) {
2943 mono_loader_unlock ();
2944 /* Somebody might have gotten in before us */
2945 return class->exception_type == MONO_EXCEPTION_NONE;
2948 if (class->init_pending) {
2949 mono_loader_unlock ();
2950 /* this indicates a cyclic dependency */
2951 g_error ("pending init %s.%s\n", class->name_space, class->name);
2954 class->init_pending = 1;
2956 /* CAS - SecurityAction.InheritanceDemand */
2957 if (mono_is_security_manager_active () && class->parent && (class->parent->flags & TYPE_ATTRIBUTE_HAS_SECURITY)) {
2958 mono_secman_inheritancedemand_class (class, class->parent);
2961 if (mono_security_get_mode () == MONO_SECURITY_MODE_CORE_CLR)
2962 check_core_clr_inheritance (class);
2964 mono_stats.initialized_class_count++;
2966 if (class->generic_class && !class->generic_class->is_dynamic) {
2967 MonoClass *gklass = class->generic_class->container_class;
2969 mono_stats.generic_class_count++;
2971 class->method = gklass->method;
2972 class->field = gklass->field;
2974 mono_class_init (gklass);
2975 mono_class_setup_methods (gklass);
2976 mono_class_setup_properties (gklass);
2978 if (MONO_CLASS_IS_INTERFACE (class))
2979 class->interface_id = mono_get_unique_iid (class);
2981 g_assert (class->interface_count == gklass->interface_count);
2984 if (class->parent && !class->parent->inited)
2985 mono_class_init (class->parent);
2987 has_cached_info = mono_class_get_cached_class_info (class, &cached_info);
2989 if (!class->generic_class && !class->image->dynamic && (!has_cached_info || (has_cached_info && cached_info.has_nested_classes))) {
2990 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
2993 guint32 cols [MONO_NESTED_CLASS_SIZE];
2994 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
2995 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
2996 class->nested_classes = g_list_prepend_mempool (class->nested_classes, class->image->mempool, nclass);
2998 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
3003 * Computes the size used by the fields, and their locations
3005 if (has_cached_info) {
3006 class->instance_size = cached_info.instance_size;
3007 class->sizes.class_size = cached_info.class_size;
3008 class->packing_size = cached_info.packing_size;
3009 class->min_align = cached_info.min_align;
3010 class->blittable = cached_info.blittable;
3011 class->has_references = cached_info.has_references;
3012 class->has_static_refs = cached_info.has_static_refs;
3013 class->no_special_static_fields = cached_info.no_special_static_fields;
3016 if (!class->size_inited){
3017 mono_class_setup_fields (class);
3018 if (class->exception_type || mono_loader_get_last_error ()){
3019 class_init_ok = FALSE;
3025 /* initialize method pointers */
3027 MonoMethod *amethod;
3028 MonoMethodSignature *sig;
3029 int count_generic = 0, first_generic = 0;
3032 class->method.count = 3 + (class->rank > 1? 2: 1);
3034 if (class->interface_count) {
3035 count_generic = generic_array_methods (class);
3036 first_generic = class->method.count;
3037 class->method.count += class->interface_count * count_generic;
3040 sig = mono_metadata_signature_alloc (class->image, class->rank);
3041 sig->ret = &mono_defaults.void_class->byval_arg;
3042 sig->pinvoke = TRUE;
3043 sig->hasthis = TRUE;
3044 for (i = 0; i < class->rank; ++i)
3045 sig->params [i] = &mono_defaults.int32_class->byval_arg;
3047 amethod = create_array_method (class, ".ctor", sig);
3048 class->methods = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoMethod*) * class->method.count);
3049 class->methods [method_num++] = amethod;
3050 if (class->rank > 1) {
3051 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
3052 sig->ret = &mono_defaults.void_class->byval_arg;
3053 sig->pinvoke = TRUE;
3054 sig->hasthis = TRUE;
3055 for (i = 0; i < class->rank * 2; ++i)
3056 sig->params [i] = &mono_defaults.int32_class->byval_arg;
3058 amethod = create_array_method (class, ".ctor", sig);
3059 class->methods [method_num++] = amethod;
3061 /* element Get (idx11, [idx2, ...]) */
3062 sig = mono_metadata_signature_alloc (class->image, class->rank);
3063 sig->ret = &class->element_class->byval_arg;
3064 sig->pinvoke = TRUE;
3065 sig->hasthis = TRUE;
3066 for (i = 0; i < class->rank; ++i)
3067 sig->params [i] = &mono_defaults.int32_class->byval_arg;
3068 amethod = create_array_method (class, "Get", sig);
3069 class->methods [method_num++] = amethod;
3070 /* element& Address (idx11, [idx2, ...]) */
3071 sig = mono_metadata_signature_alloc (class->image, class->rank);
3072 sig->ret = &class->element_class->this_arg;
3073 sig->pinvoke = TRUE;
3074 sig->hasthis = TRUE;
3075 for (i = 0; i < class->rank; ++i)
3076 sig->params [i] = &mono_defaults.int32_class->byval_arg;
3077 amethod = create_array_method (class, "Address", sig);
3078 class->methods [method_num++] = amethod;
3079 /* void Set (idx11, [idx2, ...], element) */
3080 sig = mono_metadata_signature_alloc (class->image, class->rank + 1);
3081 sig->ret = &mono_defaults.void_class->byval_arg;
3082 sig->pinvoke = TRUE;
3083 sig->hasthis = TRUE;
3084 for (i = 0; i < class->rank; ++i)
3085 sig->params [i] = &mono_defaults.int32_class->byval_arg;
3086 sig->params [i] = &class->element_class->byval_arg;
3087 amethod = create_array_method (class, "Set", sig);
3088 class->methods [method_num++] = amethod;
3090 for (i = 0; i < class->interface_count; i++)
3091 setup_generic_array_ifaces (class, class->interfaces [i], first_generic + i * count_generic);
3094 mono_class_setup_supertypes (class);
3097 initialize_object_slots (class);
3100 * If possible, avoid the creation of the generic vtable by requesting
3101 * cached info from the runtime.
3103 if (has_cached_info) {
3104 guint32 cur_slot = 0;
3106 class->vtable_size = cached_info.vtable_size;
3107 class->has_finalize = cached_info.has_finalize;
3108 class->ghcimpl = cached_info.ghcimpl;
3109 class->has_cctor = cached_info.has_cctor;
3111 if (class->parent) {
3112 mono_class_init (class->parent);
3113 cur_slot = class->parent->vtable_size;
3116 setup_interface_offsets (class, cur_slot);
3119 mono_class_setup_vtable (class);
3121 if (class->exception_type || mono_loader_get_last_error ()){
3122 class_init_ok = FALSE;
3127 if (class->parent) {
3128 MonoMethod *cmethod = class->vtable [ghc_slot];
3129 if (cmethod->is_inflated)
3130 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
3131 if (cmethod == default_ghc) {
3136 /* Object::Finalize should have empty implemenatation */
3137 class->has_finalize = 0;
3138 if (class->parent) {
3139 MonoMethod *cmethod = class->vtable [finalize_slot];
3140 if (cmethod->is_inflated)
3141 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
3142 if (cmethod != default_finalize) {
3143 class->has_finalize = 1;
3147 for (i = 0; i < class->method.count; ++i) {
3148 MonoMethod *method = class->methods [i];
3149 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
3150 (strcmp (".cctor", method->name) == 0)) {
3151 class->has_cctor = 1;
3157 if (MONO_CLASS_IS_INTERFACE (class)) {
3159 * knowledge of interface offsets is needed for the castclass/isinst code, so
3160 * we have to setup them for interfaces, too.
3162 setup_interface_offsets (class, 0);
3167 class->init_pending = 0;
3169 if (mono_loader_get_last_error ()) {
3170 if (class->exception_type == MONO_EXCEPTION_NONE)
3171 set_failure_from_loader_error (class, mono_loader_get_last_error ());
3173 mono_loader_clear_error ();
3176 mono_loader_unlock ();
3178 if (mono_debugger_class_init_func)
3179 mono_debugger_class_init_func (class);
3181 return class_init_ok;
3185 * LOCKING: this assumes the loader lock is held
3188 mono_class_setup_mono_type (MonoClass *class)
3190 const char *name = class->name;
3191 const char *nspace = class->name_space;
3193 class->this_arg.byref = 1;
3194 class->this_arg.data.klass = class;
3195 class->this_arg.type = MONO_TYPE_CLASS;
3196 class->byval_arg.data.klass = class;
3197 class->byval_arg.type = MONO_TYPE_CLASS;
3199 if (!strcmp (nspace, "System")) {
3200 if (!strcmp (name, "ValueType")) {
3202 * do not set the valuetype bit for System.ValueType.
3203 * class->valuetype = 1;
3205 class->blittable = TRUE;
3206 } else if (!strcmp (name, "Enum")) {
3208 * do not set the valuetype bit for System.Enum.
3209 * class->valuetype = 1;
3211 class->valuetype = 0;
3212 class->enumtype = 0;
3213 } else if (!strcmp (name, "Object")) {
3214 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
3215 } else if (!strcmp (name, "String")) {
3216 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
3217 } else if (!strcmp (name, "TypedReference")) {
3218 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
3222 if (class->valuetype) {
3223 int t = MONO_TYPE_VALUETYPE;
3224 if (!strcmp (nspace, "System")) {
3227 if (!strcmp (name, "Boolean")) {
3228 t = MONO_TYPE_BOOLEAN;
3229 } else if (!strcmp(name, "Byte")) {
3231 class->blittable = TRUE;
3235 if (!strcmp (name, "Char")) {
3240 if (!strcmp (name, "Double")) {
3242 class->blittable = TRUE;
3246 if (!strcmp (name, "Int32")) {
3248 class->blittable = TRUE;
3249 } else if (!strcmp(name, "Int16")) {
3251 class->blittable = TRUE;
3252 } else if (!strcmp(name, "Int64")) {
3254 class->blittable = TRUE;
3255 } else if (!strcmp(name, "IntPtr")) {
3257 class->blittable = TRUE;
3261 if (!strcmp (name, "Single")) {
3263 class->blittable = TRUE;
3264 } else if (!strcmp(name, "SByte")) {
3266 class->blittable = TRUE;
3270 if (!strcmp (name, "UInt32")) {
3272 class->blittable = TRUE;
3273 } else if (!strcmp(name, "UInt16")) {
3275 class->blittable = TRUE;
3276 } else if (!strcmp(name, "UInt64")) {
3278 class->blittable = TRUE;
3279 } else if (!strcmp(name, "UIntPtr")) {
3281 class->blittable = TRUE;
3285 if (!strcmp (name, "TypedReference")) {
3286 t = MONO_TYPE_TYPEDBYREF;
3287 class->blittable = TRUE;
3291 if (!strcmp (name, "Void")) {
3299 class->this_arg.type = class->byval_arg.type = t;
3302 if (MONO_CLASS_IS_INTERFACE (class))
3303 class->interface_id = mono_get_unique_iid (class);
3308 * LOCKING: this assumes the loader lock is held
3311 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
3313 gboolean system_namespace;
3315 system_namespace = !strcmp (class->name_space, "System");
3317 /* if root of the hierarchy */
3318 if (system_namespace && !strcmp (class->name, "Object")) {
3319 class->parent = NULL;
3320 class->instance_size = sizeof (MonoObject);
3323 if (!strcmp (class->name, "<Module>")) {
3324 class->parent = NULL;
3325 class->instance_size = 0;
3329 if (!MONO_CLASS_IS_INTERFACE (class)) {
3330 /* Imported COM Objects always derive from __ComObject. */
3331 if (MONO_CLASS_IS_IMPORT (class)) {
3332 mono_init_com_types ();
3333 if (parent == mono_defaults.object_class)
3334 parent = mono_defaults.com_object_class;
3336 class->parent = parent;
3340 g_assert_not_reached (); /* FIXME */
3342 if (parent->generic_class && !parent->name) {
3344 * If the parent is a generic instance, we may get
3345 * called before it is fully initialized, especially
3346 * before it has its name.
3351 class->marshalbyref = parent->marshalbyref;
3352 class->contextbound = parent->contextbound;
3353 class->delegate = parent->delegate;
3354 if (MONO_CLASS_IS_IMPORT (class))
3355 class->is_com_object = 1;
3357 class->is_com_object = parent->is_com_object;
3359 if (system_namespace) {
3360 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
3361 class->marshalbyref = 1;
3363 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
3364 class->contextbound = 1;
3366 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
3367 class->delegate = 1;
3370 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
3371 (strcmp (class->parent->name_space, "System") == 0)))
3372 class->valuetype = 1;
3373 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
3374 class->valuetype = class->enumtype = 1;
3376 /*class->enumtype = class->parent->enumtype; */
3377 mono_class_setup_supertypes (class);
3379 /* initialize com types if COM interfaces are present */
3380 if (MONO_CLASS_IS_IMPORT (class))
3381 mono_init_com_types ();
3382 class->parent = NULL;
3388 * mono_class_setup_supertypes:
3391 * Build the data structure needed to make fast type checks work.
3392 * This currently sets two fields in @class:
3393 * - idepth: distance between @class and System.Object in the type
3395 * - supertypes: array of classes: each element has a class in the hierarchy
3396 * starting from @class up to System.Object
3398 * LOCKING: this assumes the loader lock is held
3401 mono_class_setup_supertypes (MonoClass *class)
3405 if (class->supertypes)
3408 if (class->parent && !class->parent->supertypes)
3409 mono_class_setup_supertypes (class->parent);
3411 class->idepth = class->parent->idepth + 1;
3415 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
3416 class->supertypes = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoClass *) * ms);
3418 if (class->parent) {
3419 class->supertypes [class->idepth - 1] = class;
3420 memcpy (class->supertypes, class->parent->supertypes, class->parent->idepth * sizeof (gpointer));
3422 class->supertypes [0] = class;
3427 * mono_class_create_from_typedef:
3428 * @image: image where the token is valid
3429 * @type_token: typedef token
3431 * Create the MonoClass* representing the specified type token.
3432 * @type_token must be a TypeDef token.
3435 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
3437 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
3438 MonoClass *class, *parent = NULL;
3439 guint32 cols [MONO_TYPEDEF_SIZE];
3440 guint32 cols_next [MONO_TYPEDEF_SIZE];
3441 guint tidx = mono_metadata_token_index (type_token);
3442 MonoGenericContext *context = NULL;
3443 const char *name, *nspace;
3445 MonoClass **interfaces;
3446 guint32 field_last, method_last;
3447 guint32 nesting_tokeen;
3449 mono_loader_lock ();
3451 if ((class = mono_internal_hash_table_lookup (&image->class_cache, GUINT_TO_POINTER (type_token)))) {
3452 mono_loader_unlock ();
3456 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
3458 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
3460 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3461 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3463 class = mono_mempool_alloc0 (image->mempool, sizeof (MonoClass));
3466 class->name_space = nspace;
3468 mono_profiler_class_event (class, MONO_PROFILE_START_LOAD);
3470 class->image = image;
3471 class->type_token = type_token;
3472 class->flags = cols [MONO_TYPEDEF_FLAGS];
3474 mono_internal_hash_table_insert (&image->class_cache, GUINT_TO_POINTER (type_token), class);
3477 * Check whether we're a generic type definition.
3479 class->generic_container = mono_metadata_load_generic_params (image, class->type_token, NULL);
3480 if (class->generic_container) {
3481 class->generic_container->owner.klass = class;
3482 context = &class->generic_container->context;
3485 if (cols [MONO_TYPEDEF_EXTENDS]) {
3486 parent = mono_class_get_full (
3487 image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]), context);
3488 if (parent == NULL){
3489 mono_internal_hash_table_remove (&image->class_cache, GUINT_TO_POINTER (type_token));
3490 mono_loader_unlock ();
3491 mono_profiler_class_loaded (class, MONO_PROFILE_FAILED);
3496 /* do this early so it's available for interfaces in setup_mono_type () */
3497 if ((nesting_tokeen = mono_metadata_nested_in_typedef (image, type_token)))
3498 class->nested_in = mono_class_create_from_typedef (image, nesting_tokeen);
3500 mono_class_setup_parent (class, parent);
3502 /* uses ->valuetype, which is initialized by mono_class_setup_parent above */
3503 mono_class_setup_mono_type (class);
3505 if (!class->enumtype) {
3506 if (!mono_metadata_interfaces_from_typedef_full (
3507 image, type_token, &interfaces, &icount, context)){
3508 mono_loader_unlock ();
3509 mono_profiler_class_loaded (class, MONO_PROFILE_FAILED);
3513 class->interfaces = interfaces;
3514 class->interface_count = icount;
3517 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
3521 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
3525 class->cast_class = class->element_class = class;
3527 /*g_print ("Load class %s\n", name);*/
3530 * Compute the field and method lists
3532 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
3533 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
3535 if (tt->rows > tidx){
3536 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
3537 field_last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
3538 method_last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
3540 field_last = image->tables [MONO_TABLE_FIELD].rows;
3541 method_last = image->tables [MONO_TABLE_METHOD].rows;
3544 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
3545 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
3546 class->field.count = field_last - class->field.first;
3548 class->field.count = 0;
3550 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
3551 class->method.count = method_last - class->method.first;
3553 class->method.count = 0;
3555 /* reserve space to store vector pointer in arrays */
3556 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
3557 class->instance_size += 2 * sizeof (gpointer);
3558 g_assert (class->field.count == 0);
3561 if (class->enumtype) {
3562 class->enum_basetype = mono_class_find_enum_basetype (class);
3563 if (!class->enum_basetype) {
3564 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
3565 mono_loader_unlock ();
3568 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
3572 * If we're a generic type definition, load the constraints.
3573 * We must do this after the class has been constructed to make certain recursive scenarios
3576 if (class->generic_container)
3577 mono_metadata_load_generic_param_constraints (
3578 image, type_token, class->generic_container);
3580 mono_loader_unlock ();
3582 mono_profiler_class_loaded (class, MONO_PROFILE_OK);
3587 /** is klass Nullable<T>? */
3589 mono_class_is_nullable (MonoClass *klass)
3591 return klass->generic_class != NULL &&
3592 klass->generic_class->container_class == mono_defaults.generic_nullable_class;
3596 /** if klass is T? return T */
3598 mono_class_get_nullable_param (MonoClass *klass)
3600 g_assert (mono_class_is_nullable (klass));
3601 return mono_class_from_mono_type (klass->generic_class->context.class_inst->type_argv [0]);
3605 * Create the `MonoClass' for an instantiation of a generic type.
3606 * We only do this if we actually need it.
3609 mono_generic_class_get_class (MonoGenericClass *gclass)
3611 MonoClass *klass, *gklass;
3614 mono_loader_lock ();
3615 if (gclass->cached_class) {
3616 mono_loader_unlock ();
3617 return gclass->cached_class;
3620 gclass->cached_class = g_malloc0 (sizeof (MonoClass));
3621 klass = gclass->cached_class;
3623 gklass = gclass->container_class;
3625 if (gklass->nested_in) {
3627 * FIXME: the nested type context should include everything the
3628 * nesting context should have, but it may also have additional
3629 * generic parameters...
3631 MonoType *inflated = mono_class_inflate_generic_type (
3632 &gklass->nested_in->byval_arg, mono_generic_class_get_context (gclass));
3633 klass->nested_in = mono_class_from_mono_type (inflated);
3634 mono_metadata_free_type (inflated);
3637 klass->name = gklass->name;
3638 klass->name_space = gklass->name_space;
3639 klass->image = gklass->image;
3640 klass->flags = gklass->flags;
3641 klass->type_token = gklass->type_token;
3642 klass->field.count = gklass->field.count;
3643 klass->property.count = gklass->property.count;
3645 klass->generic_class = gclass;
3647 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
3648 klass->this_arg.data.generic_class = klass->byval_arg.data.generic_class = gclass;
3649 klass->this_arg.byref = TRUE;
3650 klass->enumtype = gklass->enumtype;
3651 klass->valuetype = gklass->valuetype;
3653 klass->cast_class = klass->element_class = klass;
3655 if (mono_class_is_nullable (klass))
3656 klass->cast_class = klass->element_class = mono_class_get_nullable_param (klass);
3658 klass->interface_count = gklass->interface_count;
3659 klass->interfaces = g_new0 (MonoClass *, klass->interface_count);
3660 for (i = 0; i < klass->interface_count; i++) {
3661 MonoType *it = &gklass->interfaces [i]->byval_arg;
3662 MonoType *inflated = mono_class_inflate_generic_type (it, mono_generic_class_get_context (gclass));
3663 klass->interfaces [i] = mono_class_from_mono_type (inflated);
3664 mono_metadata_free_type (inflated);
3668 * We're not interested in the nested classes of a generic instance.
3669 * We use the generic type definition to look for nested classes.
3671 klass->nested_classes = NULL;
3673 if (gklass->parent) {
3674 MonoType *inflated = mono_class_inflate_generic_type (
3675 &gklass->parent->byval_arg, mono_generic_class_get_context (gclass));
3677 klass->parent = mono_class_from_mono_type (inflated);
3678 mono_metadata_free_type (inflated);
3682 mono_class_setup_parent (klass, klass->parent);
3684 if (klass->enumtype) {
3685 klass->enum_basetype = gklass->enum_basetype;
3686 klass->cast_class = gklass->cast_class;
3689 if (gclass->is_dynamic) {
3692 mono_class_setup_supertypes (klass);
3694 if (klass->enumtype) {
3696 * For enums, gklass->fields might not been set, but instance_size etc. is
3697 * already set in mono_reflection_create_internal_class (). For non-enums,
3698 * these will be computed normally in mono_class_layout_fields ().
3700 klass->instance_size = gklass->instance_size;
3701 klass->sizes.class_size = gklass->sizes.class_size;
3702 klass->size_inited = 1;
3706 if (MONO_CLASS_IS_INTERFACE (klass))
3707 setup_interface_offsets (klass, 0);
3709 mono_loader_unlock ();
3715 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
3717 MonoClass *klass, **ptr;
3720 mono_loader_lock ();
3722 if (param->pklass) {
3723 mono_loader_unlock ();
3724 return param->pklass;
3727 if (!image && param->owner) {
3729 MonoMethod *method = param->owner->owner.method;
3730 image = (method && method->klass) ? method->klass->image : NULL;
3732 MonoClass *klass = param->owner->owner.klass;
3733 // FIXME: 'klass' should not be null
3734 // But, monodis creates GenericContainers without associating a owner to it
3735 image = klass ? klass->image : NULL;
3740 image = mono_defaults.corlib;
3742 klass = param->pklass = mono_mempool_alloc0 (image->mempool, sizeof (MonoClass));
3745 klass->name = param->name;
3747 klass->name = mono_mempool_alloc0 (image->mempool, 16);
3748 sprintf ((char*)klass->name, is_mvar ? "!!%d" : "!%d", param->num);
3750 klass->name_space = "";
3751 mono_profiler_class_event (klass, MONO_PROFILE_START_LOAD);
3753 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
3757 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
3758 klass->parent = param->constraints [0];
3760 } else if (param->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT)
3761 klass->parent = mono_class_from_name (mono_defaults.corlib, "System", "ValueType");
3763 klass->parent = mono_defaults.object_class;
3765 if (count - pos > 0) {
3766 klass->interface_count = count - pos;
3767 klass->interfaces = mono_mempool_alloc0 (image->mempool, sizeof (MonoClass *) * (count - pos));
3768 for (i = pos; i < count; i++)
3769 klass->interfaces [i - pos] = param->constraints [i];
3773 image = mono_defaults.corlib;
3775 klass->image = image;
3777 klass->inited = TRUE;
3778 klass->cast_class = klass->element_class = klass;
3779 klass->enum_basetype = &klass->element_class->byval_arg;
3780 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
3782 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
3783 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
3784 klass->this_arg.byref = TRUE;
3786 mono_class_setup_supertypes (klass);
3788 mono_loader_unlock ();
3790 mono_profiler_class_loaded (klass, MONO_PROFILE_OK);
3796 mono_ptr_class_get (MonoType *type)
3799 MonoClass *el_class;
3803 el_class = mono_class_from_mono_type (type);
3804 image = el_class->image;
3806 mono_loader_lock ();
3808 if (!image->ptr_cache)
3809 image->ptr_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
3811 if ((result = g_hash_table_lookup (image->ptr_cache, el_class))) {
3812 mono_loader_unlock ();
3815 result = mono_mempool_alloc0 (image->mempool, sizeof (MonoClass));
3817 result->parent = NULL; /* no parent for PTR types */
3818 result->name_space = el_class->name_space;
3819 name = g_strdup_printf ("%s*", el_class->name);
3820 result->name = mono_mempool_strdup (image->mempool, name);
3823 mono_profiler_class_event (result, MONO_PROFILE_START_LOAD);
3825 result->image = el_class->image;
3826 result->inited = TRUE;
3827 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
3828 /* Can pointers get boxed? */
3829 result->instance_size = sizeof (gpointer);
3830 result->cast_class = result->element_class = el_class;
3831 result->enum_basetype = &result->element_class->byval_arg;
3832 result->blittable = TRUE;
3834 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
3835 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
3836 result->this_arg.byref = TRUE;
3838 mono_class_setup_supertypes (result);
3840 g_hash_table_insert (image->ptr_cache, el_class, result);
3842 mono_loader_unlock ();
3844 mono_profiler_class_loaded (result, MONO_PROFILE_OK);
3850 mono_fnptr_class_get (MonoMethodSignature *sig)
3853 static GHashTable *ptr_hash = NULL;
3855 /* FIXME: These should be allocate from a mempool as well, but which one ? */
3857 mono_loader_lock ();
3860 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
3862 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
3863 mono_loader_unlock ();
3866 result = g_new0 (MonoClass, 1);
3868 result->parent = NULL; /* no parent for PTR types */
3869 result->name_space = "System";
3870 result->name = "MonoFNPtrFakeClass";
3872 mono_profiler_class_event (result, MONO_PROFILE_START_LOAD);
3874 result->image = mono_defaults.corlib; /* need to fix... */
3875 result->inited = TRUE;
3876 result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
3877 /* Can pointers get boxed? */
3878 result->instance_size = sizeof (gpointer);
3879 result->cast_class = result->element_class = result;
3880 result->blittable = TRUE;
3882 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
3883 result->this_arg.data.method = result->byval_arg.data.method = sig;
3884 result->this_arg.byref = TRUE;
3885 result->enum_basetype = &result->element_class->byval_arg;
3886 result->blittable = TRUE;
3888 mono_class_setup_supertypes (result);
3890 g_hash_table_insert (ptr_hash, sig, result);
3892 mono_loader_unlock ();
3894 mono_profiler_class_loaded (result, MONO_PROFILE_OK);
3900 mono_class_from_mono_type (MonoType *type)
3902 switch (type->type) {
3903 case MONO_TYPE_OBJECT:
3904 return type->data.klass? type->data.klass: mono_defaults.object_class;
3905 case MONO_TYPE_VOID:
3906 return type->data.klass? type->data.klass: mono_defaults.void_class;
3907 case MONO_TYPE_BOOLEAN:
3908 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
3909 case MONO_TYPE_CHAR:
3910 return type->data.klass? type->data.klass: mono_defaults.char_class;
3912 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
3914 return type->data.klass? type->data.klass: mono_defaults.byte_class;
3916 return type->data.klass? type->data.klass: mono_defaults.int16_class;
3918 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
3920 return type->data.klass? type->data.klass: mono_defaults.int32_class;
3922 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
3924 return type->data.klass? type->data.klass: mono_defaults.int_class;
3926 return type->data.klass? type->data.klass: mono_defaults.uint_class;
3928 return type->data.klass? type->data.klass: mono_defaults.int64_class;
3930 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
3932 return type->data.klass? type->data.klass: mono_defaults.single_class;
3934 return type->data.klass? type->data.klass: mono_defaults.double_class;
3935 case MONO_TYPE_STRING:
3936 return type->data.klass? type->data.klass: mono_defaults.string_class;
3937 case MONO_TYPE_TYPEDBYREF:
3938 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
3939 case MONO_TYPE_ARRAY:
3940 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
3942 return mono_ptr_class_get (type->data.type);
3943 case MONO_TYPE_FNPTR:
3944 return mono_fnptr_class_get (type->data.method);
3945 case MONO_TYPE_SZARRAY:
3946 return mono_array_class_get (type->data.klass, 1);
3947 case MONO_TYPE_CLASS:
3948 case MONO_TYPE_VALUETYPE:
3949 return type->data.klass;
3950 case MONO_TYPE_GENERICINST:
3951 return mono_generic_class_get_class (type->data.generic_class);
3953 return mono_class_from_generic_parameter (type->data.generic_param, NULL, FALSE);
3954 case MONO_TYPE_MVAR:
3955 return mono_class_from_generic_parameter (type->data.generic_param, NULL, TRUE);
3957 g_warning ("mono_class_from_mono_type: implement me 0x%02x\n", type->type);
3958 g_assert_not_reached ();
3965 * mono_type_retrieve_from_typespec
3966 * @image: context where the image is created
3967 * @type_spec: typespec token
3968 * @context: the generic context used to evaluate generic instantiations in
3971 mono_type_retrieve_from_typespec (MonoImage *image, guint32 type_spec, MonoGenericContext *context)
3973 MonoType *t = mono_type_create_from_typespec (image, type_spec);
3976 if (context && (context->class_inst || context->method_inst)) {
3977 MonoType *inflated = inflate_generic_type (t, context);
3985 * mono_class_create_from_typespec
3986 * @image: context where the image is created
3987 * @type_spec: typespec token
3988 * @context: the generic context used to evaluate generic instantiations in
3991 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec, MonoGenericContext *context)
3993 MonoType *t = mono_type_retrieve_from_typespec (image, type_spec, context);
3996 return mono_class_from_mono_type (t);
4000 * mono_bounded_array_class_get:
4001 * @element_class: element class
4002 * @rank: the dimension of the array class
4003 * @bounded: whenever the array has non-zero bounds
4005 * Returns: a class object describing the array with element type @element_type and
4009 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
4013 MonoClass *parent = NULL;
4014 GSList *list, *rootlist;
4017 gboolean corlib_type = FALSE;
4019 g_assert (rank <= 255);
4022 /* bounded only matters for one-dimensional arrays */
4025 image = eclass->image;
4027 mono_loader_lock ();
4029 if (!image->array_cache)
4030 image->array_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
4032 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
4033 for (; list; list = list->next) {
4035 if ((class->rank == rank) && (class->byval_arg.type == (((rank > 1) || bounded) ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
4036 mono_loader_unlock ();
4042 /* for the building corlib use System.Array from it */
4043 if (image->assembly && image->assembly->dynamic && image->assembly_name && strcmp (image->assembly_name, "mscorlib") == 0) {
4044 parent = mono_class_from_name (image, "System", "Array");
4047 parent = mono_defaults.array_class;
4048 if (!parent->inited)
4049 mono_class_init (parent);
4052 class = mono_mempool_alloc0 (image->mempool, sizeof (MonoClass));
4054 class->image = image;
4055 class->name_space = eclass->name_space;
4056 nsize = strlen (eclass->name);
4057 name = g_malloc (nsize + 2 + rank);
4058 memcpy (name, eclass->name, nsize);
4061 memset (name + nsize + 1, ',', rank - 1);
4062 name [nsize + rank] = ']';
4063 name [nsize + rank + 1] = 0;
4064 class->name = mono_mempool_strdup (image->mempool, name);
4067 mono_profiler_class_event (class, MONO_PROFILE_START_LOAD);
4069 class->type_token = 0;
4070 /* all arrays are marked serializable and sealed, bug #42779 */
4071 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
4072 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
4073 class->parent = parent;
4074 class->instance_size = mono_class_instance_size (class->parent);
4076 if (eclass->enumtype && !eclass->enum_basetype) {
4077 if (!eclass->reflection_info || eclass->wastypebuilder) {
4078 g_warning ("Only incomplete TypeBuilder objects are allowed to be an enum without base_type");
4079 g_assert (eclass->reflection_info && !eclass->wastypebuilder);
4081 /* element_size -1 is ok as this is not an instantitable type*/
4082 class->sizes.element_size = -1;
4084 class->sizes.element_size = mono_class_array_element_size (eclass);
4086 mono_class_setup_supertypes (class);
4088 if (mono_defaults.generic_ilist_class && !bounded && rank == 1) {
4091 /* generic IList, ICollection, IEnumerable */
4092 class->interface_count = 1;
4093 class->interfaces = mono_mempool_alloc0 (image->mempool, sizeof (MonoClass*) * class->interface_count);
4095 args [0] = &eclass->byval_arg;
4096 class->interfaces [0] = mono_class_bind_generic_parameters (
4097 mono_defaults.generic_ilist_class, 1, args, FALSE);
4100 if (eclass->generic_class)
4101 mono_class_init (eclass);
4102 if (!eclass->size_inited)
4103 mono_class_setup_fields (eclass);
4104 class->has_references = MONO_TYPE_IS_REFERENCE (&eclass->byval_arg) || eclass->has_references? TRUE: FALSE;
4108 if (eclass->enumtype)
4109 class->cast_class = eclass->element_class;
4111 class->cast_class = eclass;
4113 class->element_class = eclass;
4115 if ((rank > 1) || bounded) {
4116 MonoArrayType *at = mono_mempool_alloc0 (image->mempool, sizeof (MonoArrayType));
4117 class->byval_arg.type = MONO_TYPE_ARRAY;
4118 class->byval_arg.data.array = at;
4119 at->eklass = eclass;
4121 /* FIXME: complete.... */
4123 class->byval_arg.type = MONO_TYPE_SZARRAY;
4124 class->byval_arg.data.klass = eclass;
4126 class->this_arg = class->byval_arg;
4127 class->this_arg.byref = 1;
4132 class->generic_container = eclass->generic_container;
4134 list = g_slist_append (rootlist, class);
4135 g_hash_table_insert (image->array_cache, eclass, list);
4137 mono_loader_unlock ();
4139 mono_profiler_class_loaded (class, MONO_PROFILE_OK);
4145 * mono_array_class_get:
4146 * @element_class: element class
4147 * @rank: the dimension of the array class
4149 * Returns: a class object describing the array with element type @element_type and
4153 mono_array_class_get (MonoClass *eclass, guint32 rank)
4155 return mono_bounded_array_class_get (eclass, rank, FALSE);
4159 * mono_class_instance_size:
4162 * Returns: the size of an object instance
4165 mono_class_instance_size (MonoClass *klass)
4167 if (!klass->size_inited)
4168 mono_class_init (klass);
4170 return klass->instance_size;
4174 * mono_class_min_align:
4177 * Returns: minimm alignment requirements
4180 mono_class_min_align (MonoClass *klass)
4182 if (!klass->size_inited)
4183 mono_class_init (klass);
4185 return klass->min_align;
4189 * mono_class_value_size:
4192 * This function is used for value types, and return the
4193 * space and the alignment to store that kind of value object.
4195 * Returns: the size of a value of kind @klass
4198 mono_class_value_size (MonoClass *klass, guint32 *align)
4202 /* fixme: check disable, because we still have external revereces to
4203 * mscorlib and Dummy Objects
4205 /*g_assert (klass->valuetype);*/
4207 size = mono_class_instance_size (klass) - sizeof (MonoObject);
4210 *align = klass->min_align;
4216 * mono_class_data_size:
4219 * Returns: the size of the static class data
4222 mono_class_data_size (MonoClass *klass)
4225 mono_class_init (klass);
4227 /* in arrays, sizes.class_size is unioned with element_size
4228 * and arrays have no static fields
4232 return klass->sizes.class_size;
4236 * Auxiliary routine to mono_class_get_field
4238 * Takes a field index instead of a field token.
4240 static MonoClassField *
4241 mono_class_get_field_idx (MonoClass *class, int idx)
4243 mono_class_setup_fields_locking (class);
4246 if (class->image->uncompressed_metadata) {
4248 * class->field.first points to the FieldPtr table, while idx points into the
4249 * Field table, so we have to do a search.
4251 const char *name = mono_metadata_string_heap (class->image, mono_metadata_decode_row_col (&class->image->tables [MONO_TABLE_FIELD], idx, MONO_FIELD_NAME));
4254 for (i = 0; i < class->field.count; ++i)
4255 if (class->fields [i].name == name)
4256 return &class->fields [i];
4257 g_assert_not_reached ();
4259 if (class->field.count) {
4260 if ((idx >= class->field.first) && (idx < class->field.first + class->field.count)){
4261 return &class->fields [idx - class->field.first];
4265 class = class->parent;
4271 * mono_class_get_field:
4272 * @class: the class to lookup the field.
4273 * @field_token: the field token
4275 * Returns: A MonoClassField representing the type and offset of
4276 * the field, or a NULL value if the field does not belong to this
4280 mono_class_get_field (MonoClass *class, guint32 field_token)
4282 int idx = mono_metadata_token_index (field_token);
4284 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
4286 return mono_class_get_field_idx (class, idx - 1);
4290 * mono_class_get_field_from_name:
4291 * @klass: the class to lookup the field.
4292 * @name: the field name
4294 * Search the class @klass and it's parents for a field with the name @name.
4296 * Returns: the MonoClassField pointer of the named field or NULL
4299 mono_class_get_field_from_name (MonoClass *klass, const char *name)
4303 mono_class_setup_fields_locking (klass);
4305 for (i = 0; i < klass->field.count; ++i) {
4306 if (strcmp (name, klass->fields [i].name) == 0)
4307 return &klass->fields [i];
4309 klass = klass->parent;
4315 * mono_class_get_field_token:
4316 * @field: the field we need the token of
4318 * Get the token of a field. Note that the tokesn is only valid for the image
4319 * the field was loaded from. Don't use this function for fields in dynamic types.
4321 * Returns: the token representing the field in the image it was loaded from.
4324 mono_class_get_field_token (MonoClassField *field)
4326 MonoClass *klass = field->parent;
4329 mono_class_setup_fields_locking (klass);
4331 for (i = 0; i < klass->field.count; ++i) {
4332 if (&klass->fields [i] == field) {
4333 int idx = klass->field.first + i + 1;
4335 if (klass->image->uncompressed_metadata)
4336 idx = mono_metadata_translate_token_index (klass->image, MONO_TABLE_FIELD, idx);
4337 return mono_metadata_make_token (MONO_TABLE_FIELD, idx);
4340 klass = klass->parent;
4343 g_assert_not_reached ();
4348 mono_class_get_event_token (MonoEvent *event)
4350 MonoClass *klass = event->parent;
4354 for (i = 0; i < klass->event.count; ++i) {
4355 if (&klass->events [i] == event)
4356 return mono_metadata_make_token (MONO_TABLE_EVENT, klass->event.first + i + 1);
4358 klass = klass->parent;
4361 g_assert_not_reached ();
4366 mono_class_get_property_from_name (MonoClass *klass, const char *name)
4370 gpointer iter = NULL;
4371 while ((p = mono_class_get_properties (klass, &iter))) {
4372 if (! strcmp (name, p->name))
4375 klass = klass->parent;
4381 mono_class_get_property_token (MonoProperty *prop)
4383 MonoClass *klass = prop->parent;
4387 gpointer iter = NULL;
4388 while ((p = mono_class_get_properties (klass, &iter))) {
4389 if (&klass->properties [i] == prop)
4390 return mono_metadata_make_token (MONO_TABLE_PROPERTY, klass->property.first + i + 1);
4394 klass = klass->parent;
4397 g_assert_not_reached ();
4402 mono_class_name_from_token (MonoImage *image, guint32 type_token)
4404 const char *name, *nspace;
4406 return g_strdup_printf ("DynamicType 0x%08x", type_token);
4408 switch (type_token & 0xff000000){
4409 case MONO_TOKEN_TYPE_DEF: {
4410 guint32 cols [MONO_TYPEDEF_SIZE];
4411 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
4412 guint tidx = mono_metadata_token_index (type_token);
4414 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
4415 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
4416 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
4417 if (strlen (nspace) == 0)
4418 return g_strdup_printf ("%s", name);
4420 return g_strdup_printf ("%s.%s", nspace, name);
4423 case MONO_TOKEN_TYPE_REF: {
4424 guint32 cols [MONO_TYPEREF_SIZE];
4425 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
4427 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
4428 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
4429 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
4430 if (strlen (nspace) == 0)
4431 return g_strdup_printf ("%s", name);
4433 return g_strdup_printf ("%s.%s", nspace, name);
4436 case MONO_TOKEN_TYPE_SPEC:
4437 return g_strdup_printf ("Typespec 0x%08x", type_token);
4439 g_assert_not_reached ();
4446 mono_assembly_name_from_token (MonoImage *image, guint32 type_token)
4449 return g_strdup_printf ("DynamicAssembly %s", image->name);
4451 switch (type_token & 0xff000000){
4452 case MONO_TOKEN_TYPE_DEF:
4453 return mono_stringify_assembly_name (&image->assembly->aname);
4455 case MONO_TOKEN_TYPE_REF: {
4456 MonoAssemblyName aname;
4457 guint32 cols [MONO_TYPEREF_SIZE];
4458 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
4461 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
4463 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
4464 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
4465 case MONO_RESOLTION_SCOPE_MODULE:
4467 return g_strdup ("");
4468 case MONO_RESOLTION_SCOPE_MODULEREF:
4470 return g_strdup ("");
4471 case MONO_RESOLTION_SCOPE_TYPEREF:
4473 return g_strdup ("");
4474 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
4475 mono_assembly_get_assemblyref (image, idx - 1, &aname);
4476 return mono_stringify_assembly_name (&aname);
4478 g_assert_not_reached ();
4482 case MONO_TOKEN_TYPE_SPEC:
4484 return g_strdup ("");
4486 g_assert_not_reached ();
4493 * mono_class_get_full:
4494 * @image: the image where the class resides
4495 * @type_token: the token for the class
4496 * @context: the generic context used to evaluate generic instantiations in
4498 * Returns: the MonoClass that represents @type_token in @image
4501 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
4503 MonoClass *class = NULL;
4506 return mono_lookup_dynamic_token (image, type_token, context);
4508 switch (type_token & 0xff000000){
4509 case MONO_TOKEN_TYPE_DEF:
4510 class = mono_class_create_from_typedef (image, type_token);
4512 case MONO_TOKEN_TYPE_REF:
4513 class = mono_class_from_typeref (image, type_token);
4515 case MONO_TOKEN_TYPE_SPEC:
4516 class = mono_class_create_from_typespec (image, type_token, context);
4519 g_warning ("unknown token type %x", type_token & 0xff000000);
4520 g_assert_not_reached ();
4524 char *name = mono_class_name_from_token (image, type_token);
4525 char *assembly = mono_assembly_name_from_token (image, type_token);
4526 mono_loader_set_error_type_load (name, assembly);
4534 * mono_type_get_full:
4535 * @image: the image where the type resides
4536 * @type_token: the token for the type
4537 * @context: the generic context used to evaluate generic instantiations in
4539 * This functions exists to fullfill the fact that sometimes it's desirable to have access to the
4541 * Returns: the MonoType that represents @type_token in @image
4544 mono_type_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
4546 MonoType *type = NULL;
4548 //FIXME: this will not fix the very issue for which mono_type_get_full exists -but how to do it then?
4550 return mono_class_get_type (mono_lookup_dynamic_token (image, type_token, context));
4552 if ((type_token & 0xff000000) != MONO_TOKEN_TYPE_SPEC) {
4553 MonoClass *class = mono_class_get_full (image, type_token, context);
4554 return class ? mono_class_get_type (class) : NULL;
4557 type = mono_type_retrieve_from_typespec (image, type_token, context);
4560 char *name = mono_class_name_from_token (image, type_token);
4561 char *assembly = mono_assembly_name_from_token (image, type_token);
4562 mono_loader_set_error_type_load (name, assembly);
4570 mono_class_get (MonoImage *image, guint32 type_token)
4572 return mono_class_get_full (image, type_token, NULL);
4576 * mono_image_init_name_cache:
4578 * Initializes the class name cache stored in image->name_cache.
4580 * LOCKING: Acquires the loader lock.
4583 mono_image_init_name_cache (MonoImage *image)
4585 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
4586 guint32 cols [MONO_TYPEDEF_SIZE];
4589 guint32 i, visib, nspace_index;
4590 GHashTable *name_cache2, *nspace_table;
4592 mono_loader_lock ();
4594 image->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
4596 if (image->dynamic) {
4597 mono_loader_unlock ();
4601 /* Temporary hash table to avoid lookups in the nspace_table */
4602 name_cache2 = g_hash_table_new (NULL, NULL);
4604 for (i = 1; i <= t->rows; ++i) {
4605 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
4606 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
4608 * Nested types are accessed from the nesting name. We use the fact that nested types use different visibility flags
4609 * than toplevel types, thus avoiding the need to grovel through the NESTED_TYPE table
4611 if (visib >= TYPE_ATTRIBUTE_NESTED_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM)
4613 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
4614 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
4616 nspace_index = cols [MONO_TYPEDEF_NAMESPACE];
4617 nspace_table = g_hash_table_lookup (name_cache2, GUINT_TO_POINTER (nspace_index));
4618 if (!nspace_table) {
4619 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
4620 g_hash_table_insert (image->name_cache, (char*)nspace, nspace_table);
4621 g_hash_table_insert (name_cache2, GUINT_TO_POINTER (nspace_index),
4624 g_hash_table_insert (nspace_table, (char *) name, GUINT_TO_POINTER (i));
4627 /* Load type names from EXPORTEDTYPES table */
4629 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
4630 guint32 cols [MONO_EXP_TYPE_SIZE];
4633 for (i = 0; i < t->rows; ++i) {
4634 mono_metadata_decode_row (t, i, cols, MONO_EXP_TYPE_SIZE);
4635 name = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAME]);
4636 nspace = mono_metadata_string_heap (image, cols [MONO_EXP_TYPE_NAMESPACE]);
4638 nspace_index = cols [MONO_EXP_TYPE_NAMESPACE];
4639 nspace_table = g_hash_table_lookup (name_cache2, GUINT_TO_POINTER (nspace_index));
4640 if (!nspace_table) {
4641 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
4642 g_hash_table_insert (image->name_cache, (char*)nspace, nspace_table);
4643 g_hash_table_insert (name_cache2, GUINT_TO_POINTER (nspace_index),
4646 g_hash_table_insert (nspace_table, (char *) name, GUINT_TO_POINTER (mono_metadata_make_token (MONO_TABLE_EXPORTEDTYPE, i + 1)));
4650 g_hash_table_destroy (name_cache2);
4652 mono_loader_unlock ();
4656 mono_image_add_to_name_cache (MonoImage *image, const char *nspace,
4657 const char *name, guint32 index)
4659 GHashTable *nspace_table;
4660 GHashTable *name_cache;
4662 mono_loader_lock ();
4664 if (!image->name_cache)
4665 mono_image_init_name_cache (image);
4667 name_cache = image->name_cache;
4668 if (!(nspace_table = g_hash_table_lookup (name_cache, nspace))) {
4669 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
4670 g_hash_table_insert (name_cache, (char *)nspace, (char *)nspace_table);
4672 g_hash_table_insert (nspace_table, (char *) name, GUINT_TO_POINTER (index));
4674 mono_loader_unlock ();
4683 find_nocase (gpointer key, gpointer value, gpointer user_data)
4685 char *name = (char*)key;
4686 FindUserData *data = (FindUserData*)user_data;
4688 if (!data->value && (g_strcasecmp (name, (char*)data->key) == 0))
4689 data->value = value;
4693 * mono_class_from_name_case:
4694 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
4695 * @name_space: the type namespace
4696 * @name: the type short name.
4698 * Obtains a MonoClass with a given namespace and a given name which
4699 * is located in the given MonoImage. The namespace and name
4700 * lookups are case insensitive.
4703 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
4705 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
4706 guint32 cols [MONO_TYPEDEF_SIZE];
4711 if (image->dynamic) {
4713 FindUserData user_data;
4715 mono_loader_lock ();
4717 if (!image->name_cache)
4718 mono_image_init_name_cache (image);
4720 user_data.key = name_space;
4721 user_data.value = NULL;
4722 g_hash_table_foreach (image->name_cache, find_nocase, &user_data);
4724 if (user_data.value) {
4725 GHashTable *nspace_table = (GHashTable*)user_data.value;
4727 user_data.key = name;
4728 user_data.value = NULL;
4730 g_hash_table_foreach (nspace_table, find_nocase, &user_data);
4732 if (user_data.value)
4733 token = GPOINTER_TO_UINT (user_data.value);
4736 mono_loader_unlock ();
4739 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | token);
4745 /* add a cache if needed */
4746 for (i = 1; i <= t->rows; ++i) {
4747 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
4748 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
4750 * Nested types are accessed from the nesting name. We use the fact that nested types use different visibility flags
4751 * than toplevel types, thus avoiding the need to grovel through the NESTED_TYPE table
4753 if (visib >= TYPE_ATTRIBUTE_NESTED_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM)
4755 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
4756 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
4757 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
4758 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
4764 return_nested_in (MonoClass *class, char *nested) {
4766 char *s = strchr (nested, '/');
4773 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
4775 if (strcmp (found->name, nested) == 0) {
4777 return return_nested_in (found, s);
4786 * mono_class_from_name:
4787 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
4788 * @name_space: the type namespace
4789 * @name: the type short name.
4791 * Obtains a MonoClass with a given namespace and a given name which
4792 * is located in the given MonoImage.
4795 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
4797 GHashTable *nspace_table;
4798 MonoImage *loaded_image;
4805 if ((nested = strchr (name, '/'))) {
4806 int pos = nested - name;
4807 int len = strlen (name);
4810 memcpy (buf, name, len + 1);
4812 nested = buf + pos + 1;
4816 if (get_class_from_name) {
4817 gboolean res = get_class_from_name (image, name_space, name, &class);
4820 return class ? return_nested_in (class, nested) : NULL;
4826 mono_loader_lock ();
4828 if (!image->name_cache)
4829 mono_image_init_name_cache (image);
4831 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
4834 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
4836 mono_loader_unlock ();
4838 if (!token && image->dynamic && image->modules) {
4839 /* Search modules as well */
4840 for (i = 0; i < image->module_count; ++i) {
4841 MonoImage *module = image->modules [i];
4843 class = mono_class_from_name (module, name_space, name);
4852 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
4853 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
4854 guint32 cols [MONO_EXP_TYPE_SIZE];
4857 idx = mono_metadata_token_index (token);
4859 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
4861 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
4862 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
4863 loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
4866 class = mono_class_from_name (loaded_image, name_space, name);
4868 return return_nested_in (class, nested);
4870 } else if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_ASSEMBLYREF) {
4871 MonoAssembly **references = image->references;
4872 if (!references [idx - 1])
4873 mono_assembly_load_reference (image, idx - 1);
4874 g_assert (references == image->references);
4875 g_assert (references [idx - 1]);
4876 if (references [idx - 1] == (gpointer)-1)
4879 /* FIXME: Cycle detection */
4880 return mono_class_from_name (references [idx - 1]->image, name_space, name);
4882 g_error ("not yet implemented");
4886 token = MONO_TOKEN_TYPE_DEF | token;
4888 class = mono_class_get (image, token);
4890 return return_nested_in (class, nested);
4895 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
4896 gboolean check_interfaces)
4898 g_assert (klassc->idepth > 0);
4899 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
4900 if (MONO_CLASS_IMPLEMENTS_INTERFACE (klass, klassc->interface_id))
4902 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
4905 for (i = 0; i < klass->interface_count; i ++) {
4906 MonoClass *ic = klass->interfaces [i];
4911 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
4916 * MS.NET thinks interfaces are a subclass of Object, so we think it as
4919 if (klassc == mono_defaults.object_class)
4926 mono_class_has_variant_generic_params (MonoClass *klass)
4929 MonoGenericContainer *container;
4931 if (!klass->generic_class)
4934 container = klass->generic_class->container_class->generic_container;
4936 for (i = 0; i < container->type_argc; ++i)
4937 if (container->type_params [i].flags & (MONO_GEN_PARAM_VARIANT|MONO_GEN_PARAM_COVARIANT))
4944 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
4947 mono_class_init (klass);
4949 if (!oklass->inited)
4950 mono_class_init (oklass);
4952 if ((klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR))
4953 return klass == oklass;
4955 if (MONO_CLASS_IS_INTERFACE (klass)) {
4956 if ((oklass->byval_arg.type == MONO_TYPE_VAR) || (oklass->byval_arg.type == MONO_TYPE_MVAR))
4959 /* interface_offsets might not be set for dynamic classes */
4960 if (oklass->reflection_info && !oklass->interface_bitmap)
4962 * oklass might be a generic type parameter but they have
4963 * interface_offsets set.
4965 return mono_reflection_call_is_assignable_to (oklass, klass);
4967 if (MONO_CLASS_IMPLEMENTS_INTERFACE (oklass, klass->interface_id))
4970 if (mono_class_has_variant_generic_params (klass)) {
4971 if (oklass->generic_class) {
4973 gboolean match = FALSE;
4974 MonoClass *container_class1 = klass->generic_class->container_class;
4975 MonoClass *container_class2 = oklass->generic_class->container_class;
4978 * Check whenever the generic definition of oklass implements the
4979 * generic definition of klass. The IMPLEMENTS_INTERFACE stuff is not usable
4980 * here since the relevant tables are not set up.
4982 for (i = 0; i < container_class2->interface_offsets_count; ++i)
4983 if ((container_class2->interfaces_packed [i] == container_class1) || (container_class2->interfaces_packed [i]->generic_class && (container_class2->interfaces_packed [i]->generic_class->container_class == container_class1)))
4987 MonoGenericContainer *container;
4989 container = klass->generic_class->container_class->generic_container;
4992 for (i = 0; i < container->type_argc; ++i) {
4993 MonoClass *param1_class = mono_class_from_mono_type (klass->generic_class->context.class_inst->type_argv [i]);
4994 MonoClass *param2_class = mono_class_from_mono_type (oklass->generic_class->context.class_inst->type_argv [i]);
4997 * The _VARIANT and _COVARIANT constants should read _COVARIANT and
4998 * _CONTRAVARIANT, but they are in a public header so we can't fix it.
5000 if (param1_class != param2_class) {
5001 if ((container->type_params [i].flags & MONO_GEN_PARAM_VARIANT) && mono_class_is_assignable_from (param1_class, param2_class))
5003 else if (((container->type_params [i].flags & MONO_GEN_PARAM_COVARIANT) && mono_class_is_assignable_from (param2_class, param1_class)))
5015 } else if (klass->rank) {
5016 MonoClass *eclass, *eoclass;
5018 if (oklass->rank != klass->rank)
5021 /* vectors vs. one dimensional arrays */
5022 if (oklass->byval_arg.type != klass->byval_arg.type)
5025 eclass = klass->cast_class;
5026 eoclass = oklass->cast_class;
5029 * a is b does not imply a[] is b[] when a is a valuetype, and
5030 * b is a reference type.
5033 if (eoclass->valuetype) {
5034 if ((eclass == mono_defaults.enum_class) ||
5035 (eclass == mono_defaults.enum_class->parent) ||
5036 (eclass == mono_defaults.object_class))
5040 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
5041 } else if (mono_class_is_nullable (klass))
5042 return (mono_class_is_assignable_from (klass->cast_class, oklass));
5043 else if (klass == mono_defaults.object_class)
5046 return mono_class_has_parent (oklass, klass);
5050 * mono_class_get_cctor:
5052 * Returns the static constructor of @klass if it exists, NULL otherwise.
5055 mono_class_get_cctor (MonoClass *klass)
5057 MonoCachedClassInfo cached_info;
5059 if (!klass->has_cctor)
5062 if (mono_class_get_cached_class_info (klass, &cached_info))
5063 return mono_get_method (klass->image, cached_info.cctor_token, klass);
5065 return mono_class_get_method_from_name_flags (klass, ".cctor", -1, METHOD_ATTRIBUTE_SPECIAL_NAME);
5069 * mono_class_get_finalizer:
5071 * Returns the finalizer method of @klass if it exists, NULL otherwise.
5074 mono_class_get_finalizer (MonoClass *klass)
5076 MonoCachedClassInfo cached_info;
5079 mono_class_init (klass);
5080 if (!klass->has_finalize)
5083 if (mono_class_get_cached_class_info (klass, &cached_info))
5084 return mono_get_method (cached_info.finalize_image, cached_info.finalize_token, NULL);
5086 mono_class_setup_vtable (klass);
5087 return klass->vtable [finalize_slot];
5092 * mono_class_needs_cctor_run:
5094 * Determines whenever the class has a static constructor and whenever it
5095 * needs to be called when executing CALLER.
5098 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
5102 method = mono_class_get_cctor (klass);
5104 return (method == caller) ? FALSE : TRUE;
5110 * mono_class_array_element_size:
5113 * Returns: the number of bytes an element of type @klass
5114 * uses when stored into an array.
5117 mono_class_array_element_size (MonoClass *klass)
5119 MonoType *type = &klass->byval_arg;
5122 switch (type->type) {
5125 case MONO_TYPE_BOOLEAN:
5129 case MONO_TYPE_CHAR:
5138 case MONO_TYPE_CLASS:
5139 case MONO_TYPE_STRING:
5140 case MONO_TYPE_OBJECT:
5141 case MONO_TYPE_SZARRAY:
5142 case MONO_TYPE_ARRAY:
5144 case MONO_TYPE_MVAR:
5145 return sizeof (gpointer);
5150 case MONO_TYPE_VALUETYPE:
5151 if (type->data.klass->enumtype) {
5152 type = type->data.klass->enum_basetype;
5153 klass = klass->element_class;
5156 return mono_class_instance_size (klass) - sizeof (MonoObject);
5157 case MONO_TYPE_GENERICINST:
5158 type = &type->data.generic_class->container_class->byval_arg;
5161 g_error ("unknown type 0x%02x in mono_class_array_element_size", type->type);
5167 * mono_array_element_size:
5168 * @ac: pointer to a #MonoArrayClass
5170 * Returns: the size of single array element.
5173 mono_array_element_size (MonoClass *ac)
5175 g_assert (ac->rank);
5176 return ac->sizes.element_size;
5180 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
5181 MonoGenericContext *context)
5183 if (image->dynamic) {
5184 MonoClass *tmp_handle_class;
5185 gpointer obj = mono_lookup_dynamic_token_class (image, token, &tmp_handle_class, context);
5187 g_assert (tmp_handle_class);
5189 *handle_class = tmp_handle_class;
5191 if (tmp_handle_class == mono_defaults.typehandle_class)
5192 return &((MonoClass*)obj)->byval_arg;
5197 switch (token & 0xff000000) {
5198 case MONO_TOKEN_TYPE_DEF:
5199 case MONO_TOKEN_TYPE_REF:
5200 case MONO_TOKEN_TYPE_SPEC: {
5203 *handle_class = mono_defaults.typehandle_class;
5204 type = mono_type_get_full (image, token, context);
5207 mono_class_init (mono_class_from_mono_type (type));
5208 /* We return a MonoType* as handle */
5211 case MONO_TOKEN_FIELD_DEF: {
5213 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
5215 *handle_class = mono_defaults.fieldhandle_class;
5216 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
5219 mono_class_init (class);
5220 return mono_class_get_field (class, token);
5222 case MONO_TOKEN_METHOD_DEF:
5223 case MONO_TOKEN_METHOD_SPEC: {
5225 meth = mono_get_method_full (image, token, NULL, context);
5227 *handle_class = mono_defaults.methodhandle_class;
5230 case MONO_TOKEN_MEMBER_REF: {
5231 guint32 cols [MONO_MEMBERREF_SIZE];
5233 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
5234 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
5235 mono_metadata_decode_blob_size (sig, &sig);
5236 if (*sig == 0x6) { /* it's a field */
5238 MonoClassField *field;
5239 field = mono_field_from_token (image, token, &klass, context);
5241 *handle_class = mono_defaults.fieldhandle_class;
5245 meth = mono_get_method_full (image, token, NULL, context);
5247 *handle_class = mono_defaults.methodhandle_class;
5252 g_warning ("Unknown token 0x%08x in ldtoken", token);
5259 * This function might need to call runtime functions so it can't be part
5260 * of the metadata library.
5262 static MonoLookupDynamicToken lookup_dynamic = NULL;
5265 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
5267 lookup_dynamic = func;
5271 mono_lookup_dynamic_token (MonoImage *image, guint32 token, MonoGenericContext *context)
5273 MonoClass *handle_class;
5275 return lookup_dynamic (image, token, &handle_class, context);
5279 mono_lookup_dynamic_token_class (MonoImage *image, guint32 token, MonoClass **handle_class, MonoGenericContext *context)
5281 return lookup_dynamic (image, token, handle_class, context);
5284 static MonoGetCachedClassInfo get_cached_class_info = NULL;
5287 mono_install_get_cached_class_info (MonoGetCachedClassInfo func)
5289 get_cached_class_info = func;
5293 mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
5295 if (!get_cached_class_info)
5298 return get_cached_class_info (klass, res);
5302 mono_install_get_class_from_name (MonoGetClassFromName func)
5304 get_class_from_name = func;
5308 mono_class_get_image (MonoClass *klass)
5310 return klass->image;
5314 * mono_class_get_element_class:
5315 * @klass: the MonoClass to act on
5317 * Returns: the element class of an array or an enumeration.
5320 mono_class_get_element_class (MonoClass *klass)
5322 return klass->element_class;
5326 * mono_class_is_valuetype:
5327 * @klass: the MonoClass to act on
5329 * Returns: true if the MonoClass represents a ValueType.
5332 mono_class_is_valuetype (MonoClass *klass)
5334 return klass->valuetype;
5338 * mono_class_is_enum:
5339 * @klass: the MonoClass to act on
5341 * Returns: true if the MonoClass represents an enumeration.
5344 mono_class_is_enum (MonoClass *klass)
5346 return klass->enumtype;
5350 * mono_class_enum_basetype:
5351 * @klass: the MonoClass to act on
5353 * Returns: the underlying type representation for an enumeration.
5356 mono_class_enum_basetype (MonoClass *klass)
5358 return klass->enum_basetype;
5362 * mono_class_get_parent
5363 * @klass: the MonoClass to act on
5365 * Returns: the parent class for this class.
5368 mono_class_get_parent (MonoClass *klass)
5370 return klass->parent;
5374 * mono_class_get_nesting_type;
5375 * @klass: the MonoClass to act on
5377 * Returns: the container type where this type is nested or NULL if this type is not a nested type.
5380 mono_class_get_nesting_type (MonoClass *klass)
5382 return klass->nested_in;
5386 * mono_class_get_rank:
5387 * @klass: the MonoClass to act on
5389 * Returns: the rank for the array (the number of dimensions).
5392 mono_class_get_rank (MonoClass *klass)
5398 * mono_class_get_flags:
5399 * @klass: the MonoClass to act on
5401 * The type flags from the TypeDef table from the metadata.
5402 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
5405 * Returns: the flags from the TypeDef table.
5408 mono_class_get_flags (MonoClass *klass)
5410 return klass->flags;
5414 * mono_class_get_name
5415 * @klass: the MonoClass to act on
5417 * Returns: the name of the class.
5420 mono_class_get_name (MonoClass *klass)
5426 * mono_class_get_namespace:
5427 * @klass: the MonoClass to act on
5429 * Returns: the namespace of the class.
5432 mono_class_get_namespace (MonoClass *klass)
5434 return klass->name_space;
5438 * mono_class_get_type:
5439 * @klass: the MonoClass to act on
5441 * This method returns the internal Type representation for the class.
5443 * Returns: the MonoType from the class.
5446 mono_class_get_type (MonoClass *klass)
5448 return &klass->byval_arg;
5452 * mono_class_get_type_token
5453 * @klass: the MonoClass to act on
5455 * This method returns type token for the class.
5457 * Returns: the type token for the class.
5460 mono_class_get_type_token (MonoClass *klass)
5462 return klass->type_token;
5466 * mono_class_get_byref_type:
5467 * @klass: the MonoClass to act on
5472 mono_class_get_byref_type (MonoClass *klass)
5474 return &klass->this_arg;
5478 * mono_class_num_fields:
5479 * @klass: the MonoClass to act on
5481 * Returns: the number of static and instance fields in the class.
5484 mono_class_num_fields (MonoClass *klass)
5486 return klass->field.count;
5490 * mono_class_num_methods:
5491 * @klass: the MonoClass to act on
5493 * Returns: the number of methods in the class.
5496 mono_class_num_methods (MonoClass *klass)
5498 return klass->method.count;
5502 * mono_class_num_properties
5503 * @klass: the MonoClass to act on
5505 * Returns: the number of properties in the class.
5508 mono_class_num_properties (MonoClass *klass)
5510 mono_class_setup_properties (klass);
5512 return klass->property.count;
5516 * mono_class_num_events:
5517 * @klass: the MonoClass to act on
5519 * Returns: the number of events in the class.
5522 mono_class_num_events (MonoClass *klass)
5524 mono_class_setup_events (klass);
5526 return klass->event.count;
5530 * mono_class_get_fields:
5531 * @klass: the MonoClass to act on
5533 * This routine is an iterator routine for retrieving the fields in a class.
5535 * You must pass a gpointer that points to zero and is treated as an opaque handle to
5536 * iterate over all of the elements. When no more values are
5537 * available, the return value is NULL.
5539 * Returns: a @MonoClassField* on each iteration, or NULL when no more fields are available.
5542 mono_class_get_fields (MonoClass* klass, gpointer *iter)
5544 MonoClassField* field;
5547 mono_class_setup_fields_locking (klass);
5549 /* start from the first */
5550 if (klass->field.count) {
5551 return *iter = &klass->fields [0];
5559 if (field < &klass->fields [klass->field.count]) {
5560 return *iter = field;
5566 * mono_class_get_methods
5567 * @klass: the MonoClass to act on
5569 * This routine is an iterator routine for retrieving the fields in a class.
5571 * You must pass a gpointer that points to zero and is treated as an opaque handle to
5572 * iterate over all of the elements. When no more values are
5573 * available, the return value is NULL.
5575 * Returns: a MonoMethod on each iteration or NULL when no more methods are available.
5578 mono_class_get_methods (MonoClass* klass, gpointer *iter)
5580 MonoMethod** method;
5584 mono_class_init (klass);
5586 mono_class_setup_methods (klass);
5587 /* start from the first */
5588 if (klass->method.count) {
5589 *iter = &klass->methods [0];
5590 return klass->methods [0];
5598 if (method < &klass->methods [klass->method.count]) {
5606 * mono_class_get_properties:
5607 * @klass: the MonoClass to act on
5609 * This routine is an iterator routine for retrieving the properties in a class.
5611 * You must pass a gpointer that points to zero and is treated as an opaque handle to
5612 * iterate over all of the elements. When no more values are
5613 * available, the return value is NULL.
5615 * Returns: a @MonoProperty* on each invocation, or NULL when no more are available.
5618 mono_class_get_properties (MonoClass* klass, gpointer *iter)
5620 MonoProperty* property;
5624 mono_class_init (klass);
5626 mono_class_setup_properties (klass);
5627 /* start from the first */
5628 if (klass->property.count) {
5629 return *iter = &klass->properties [0];
5637 if (property < &klass->properties [klass->property.count]) {
5638 return *iter = property;
5644 * mono_class_get_events:
5645 * @klass: the MonoClass to act on
5647 * This routine is an iterator routine for retrieving the properties in a class.
5649 * You must pass a gpointer that points to zero and is treated as an opaque handle to
5650 * iterate over all of the elements. When no more values are
5651 * available, the return value is NULL.
5653 * Returns: a @MonoEvent* on each invocation, or NULL when no more are available.
5656 mono_class_get_events (MonoClass* klass, gpointer *iter)
5662 mono_class_init (klass);
5664 mono_class_setup_events (klass);
5665 /* start from the first */
5666 if (klass->event.count) {
5667 return *iter = &klass->events [0];
5675 if (event < &klass->events [klass->event.count]) {
5676 return *iter = event;
5682 * mono_class_get_interfaces
5683 * @klass: the MonoClass to act on
5685 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
5687 * You must pass a gpointer that points to zero and is treated as an opaque handle to
5688 * iterate over all of the elements. When no more values are
5689 * available, the return value is NULL.
5691 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
5694 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
5700 mono_class_init (klass);
5702 /* start from the first */
5703 if (klass->interface_count) {
5704 *iter = &klass->interfaces [0];
5705 return klass->interfaces [0];
5713 if (iface < &klass->interfaces [klass->interface_count]) {
5721 * mono_class_get_nested_types
5722 * @klass: the MonoClass to act on
5724 * This routine is an iterator routine for retrieving the nested types of a class.
5725 * This works only if @klass is non-generic, or a generic type definition.
5727 * You must pass a gpointer that points to zero and is treated as an opaque handle to
5728 * iterate over all of the elements. When no more values are
5729 * available, the return value is NULL.
5731 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
5734 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
5740 mono_class_init (klass);
5742 /* start from the first */
5743 if (klass->nested_classes) {
5744 *iter = klass->nested_classes;
5745 return klass->nested_classes->data;
5747 /* no nested types */
5761 * mono_field_get_name:
5762 * @field: the MonoClassField to act on
5764 * Returns: the name of the field.
5767 mono_field_get_name (MonoClassField *field)
5773 * mono_field_get_type:
5774 * @field: the MonoClassField to act on
5776 * Returns: MonoType of the field.
5779 mono_field_get_type (MonoClassField *field)
5785 * mono_field_get_type:
5786 * @field: the MonoClassField to act on
5788 * Returns: MonoClass where the field was defined.
5791 mono_field_get_parent (MonoClassField *field)
5793 return field->parent;
5797 * mono_field_get_flags;
5798 * @field: the MonoClassField to act on
5800 * The metadata flags for a field are encoded using the
5801 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
5803 * Returns: the flags for the field.
5806 mono_field_get_flags (MonoClassField *field)
5808 return field->type->attrs;
5812 * mono_field_get_data;
5813 * @field: the MonoClassField to act on
5815 * Returns: pointer to the metadata constant value or to the field
5816 * data if it has an RVA flag.
5819 mono_field_get_data (MonoClassField *field)
5825 * mono_property_get_name:
5826 * @prop: the MonoProperty to act on
5828 * Returns: the name of the property
5831 mono_property_get_name (MonoProperty *prop)
5837 * mono_property_get_set_method
5838 * @prop: the MonoProperty to act on.
5840 * Returns: the setter method of the property (A MonoMethod)
5843 mono_property_get_set_method (MonoProperty *prop)
5849 * mono_property_get_get_method
5850 * @prop: the MonoProperty to act on.
5852 * Returns: the setter method of the property (A MonoMethod)
5855 mono_property_get_get_method (MonoProperty *prop)
5861 * mono_property_get_parent:
5862 * @prop: the MonoProperty to act on.
5864 * Returns: the MonoClass where the property was defined.
5867 mono_property_get_parent (MonoProperty *prop)
5869 return prop->parent;
5873 * mono_property_get_flags:
5874 * @prop: the MonoProperty to act on.
5876 * The metadata flags for a property are encoded using the
5877 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
5879 * Returns: the flags for the property.
5882 mono_property_get_flags (MonoProperty *prop)
5888 * mono_event_get_name:
5889 * @event: the MonoEvent to act on
5891 * Returns: the name of the event.
5894 mono_event_get_name (MonoEvent *event)
5900 * mono_event_get_add_method:
5901 * @event: The MonoEvent to act on.
5903 * Returns: the @add' method for the event (a MonoMethod).
5906 mono_event_get_add_method (MonoEvent *event)
5912 * mono_event_get_remove_method:
5913 * @event: The MonoEvent to act on.
5915 * Returns: the @remove method for the event (a MonoMethod).
5918 mono_event_get_remove_method (MonoEvent *event)
5920 return event->remove;
5924 * mono_event_get_raise_method:
5925 * @event: The MonoEvent to act on.
5927 * Returns: the @raise method for the event (a MonoMethod).
5930 mono_event_get_raise_method (MonoEvent *event)
5932 return event->raise;
5936 * mono_event_get_parent:
5937 * @event: the MonoEvent to act on.
5939 * Returns: the MonoClass where the event is defined.
5942 mono_event_get_parent (MonoEvent *event)
5944 return event->parent;
5948 * mono_event_get_flags
5949 * @event: the MonoEvent to act on.
5951 * The metadata flags for an event are encoded using the
5952 * EVENT_* constants. See the tabledefs.h file for details.
5954 * Returns: the flags for the event.
5957 mono_event_get_flags (MonoEvent *event)
5959 return event->attrs;
5963 * mono_class_get_method_from_name:
5964 * @klass: where to look for the method
5965 * @name_space: name of the method
5966 * @param_count: number of parameters. -1 for any number.
5968 * Obtains a MonoMethod with a given name and number of parameters.
5969 * It only works if there are no multiple signatures for any given method name.
5972 mono_class_get_method_from_name (MonoClass *klass, const char *name, int param_count)
5974 return mono_class_get_method_from_name_flags (klass, name, param_count, 0);
5978 * mono_class_get_method_from_name_flags:
5979 * @klass: where to look for the method
5980 * @name_space: name of the method
5981 * @param_count: number of parameters. -1 for any number.
5982 * @flags: flags which must be set in the method
5984 * Obtains a MonoMethod with a given name and number of parameters.
5985 * It only works if there are no multiple signatures for any given method name.
5988 mono_class_get_method_from_name_flags (MonoClass *klass, const char *name, int param_count, int flags)
5990 MonoMethod *res = NULL;
5993 mono_class_init (klass);
5995 if (klass->methods) {
5996 mono_class_setup_methods (klass);
5997 for (i = 0; i < klass->method.count; ++i) {
5998 MonoMethod *method = klass->methods [i];
6000 if (method->name[0] == name [0] &&
6001 !strcmp (name, method->name) &&
6002 (param_count == -1 || mono_method_signature (method)->param_count == param_count) &&
6003 ((method->flags & flags) == flags)) {
6010 /* Search directly in the metadata to avoid calling setup_methods () */
6011 for (i = 0; i < klass->method.count; ++i) {
6012 guint32 cols [MONO_METHOD_SIZE];
6015 /* class->method.first points into the methodptr table */
6016 mono_metadata_decode_table_row (klass->image, MONO_TABLE_METHOD, klass->method.first + i, cols, MONO_METHOD_SIZE);
6018 if (!strcmp (mono_metadata_string_heap (klass->image, cols [MONO_METHOD_NAME]), name)) {
6019 method = mono_get_method (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass);
6020 if ((param_count == -1) || mono_method_signature (method)->param_count == param_count) {
6032 * mono_class_set_failure:
6033 * @klass: class in which the failure was detected
6034 * @ex_type: the kind of exception/error to be thrown (later)
6035 * @ex_data: exception data (specific to each type of exception/error)
6037 * Keep a detected failure informations in the class for later processing.
6038 * Note that only the first failure is kept.
6041 mono_class_set_failure (MonoClass *klass, guint32 ex_type, void *ex_data)
6043 if (klass->exception_type)
6045 klass->exception_type = ex_type;
6046 klass->exception_data = ex_data;
6051 * mono_classes_init:
6053 * Initialize the resources used by this module.
6056 mono_classes_init (void)
6061 * mono_classes_cleanup:
6063 * Free the resources used by this module.
6066 mono_classes_cleanup (void)
6068 IOffsetInfo *cached_info, *next;
6070 if (global_interface_bitset)
6071 mono_bitset_free (global_interface_bitset);
6073 for (cached_info = cached_offset_info; cached_info;) {
6074 next = cached_info->next;
6076 g_free (cached_info);
6082 * mono_class_get_exception_for_failure:
6083 * @klass: class in which the failure was detected
6085 * Return a constructed MonoException than the caller can then throw
6086 * using mono_raise_exception - or NULL if no failure is present (or
6087 * doesn't result in an exception).
6090 mono_class_get_exception_for_failure (MonoClass *klass)
6092 switch (klass->exception_type) {
6093 case MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND: {
6094 MonoDomain *domain = mono_domain_get ();
6095 MonoSecurityManager* secman = mono_security_manager_get_methods ();
6096 MonoMethod *method = klass->exception_data;
6097 guint32 error = (method) ? MONO_METADATA_INHERITANCEDEMAND_METHOD : MONO_METADATA_INHERITANCEDEMAND_CLASS;
6098 MonoObject *exc = NULL;
6102 args [1] = mono_assembly_get_object (domain, mono_image_get_assembly (klass->image));
6103 args [2] = mono_type_get_object (domain, &klass->byval_arg);
6104 args [3] = (method) ? mono_method_get_object (domain, method, NULL) : NULL;
6106 mono_runtime_invoke (secman->inheritsecurityexception, NULL, args, &exc);
6107 return (MonoException*) exc;
6109 case MONO_EXCEPTION_TYPE_LOAD: {
6112 char *str = mono_type_get_full_name (klass);
6113 char *astr = klass->image->assembly? mono_stringify_assembly_name (&klass->image->assembly->aname): NULL;
6114 name = mono_string_new (mono_domain_get (), str);
6116 ex = mono_get_exception_type_load (name, astr);
6120 case MONO_EXCEPTION_MISSING_METHOD: {
6121 char *class_name = klass->exception_data;
6122 char *assembly_name = class_name + strlen (class_name) + 1;
6124 return mono_get_exception_missing_method (class_name, assembly_name);
6126 case MONO_EXCEPTION_MISSING_FIELD: {
6127 char *class_name = klass->exception_data;
6128 char *member_name = class_name + strlen (class_name) + 1;
6130 return mono_get_exception_missing_field (class_name, member_name);
6132 case MONO_EXCEPTION_FILE_NOT_FOUND: {
6133 char *msg_format = klass->exception_data;
6134 char *assembly_name = msg_format + strlen (msg_format) + 1;
6135 char *msg = g_strdup_printf (msg_format, assembly_name);
6138 ex = mono_get_exception_file_not_found2 (msg, mono_string_new (mono_domain_get (), assembly_name));
6145 MonoLoaderError *error;
6148 error = mono_loader_get_last_error ();
6150 ex = mono_loader_error_prepare_exception (error);
6154 /* TODO - handle other class related failures */
6161 can_access_internals (MonoAssembly *accessing, MonoAssembly* accessed)
6164 if (accessing == accessed)
6166 if (!accessed || !accessing)
6168 for (tmp = accessed->friend_assembly_names; tmp; tmp = tmp->next) {
6169 MonoAssemblyName *friend = tmp->data;
6170 /* Be conservative with checks */
6173 if (strcmp (accessing->aname.name, friend->name))
6175 if (friend->public_key_token [0]) {
6176 if (!accessing->aname.public_key_token [0])
6178 if (strcmp ((char*)friend->public_key_token, (char*)accessing->aname.public_key_token))
6187 * If klass is a generic type or if it is derived from a generic type, return the
6188 * MonoClass of the generic definition
6189 * Returns NULL if not found
6192 get_generic_definition_class (MonoClass *klass)
6195 if (klass->generic_class && klass->generic_class->container_class)
6196 return klass->generic_class->container_class;
6197 klass = klass->parent;
6202 /* FIXME: check visibility of type, too */
6204 can_access_member (MonoClass *access_klass, MonoClass *member_klass, int access_level)
6206 MonoClass *member_generic_def;
6207 if (((access_klass->generic_class && access_klass->generic_class->container_class) ||
6208 access_klass->generic_container) &&
6209 (member_generic_def = get_generic_definition_class (member_klass))) {
6210 MonoClass *access_container;
6212 if (access_klass->generic_container)
6213 access_container = access_klass;
6215 access_container = access_klass->generic_class->container_class;
6217 if (can_access_member (access_container, member_generic_def, access_level))
6221 /* Partition I 8.5.3.2 */
6222 /* the access level values are the same for fields and methods */
6223 switch (access_level) {
6224 case FIELD_ATTRIBUTE_COMPILER_CONTROLLED:
6225 /* same compilation unit */
6226 return access_klass->image == member_klass->image;
6227 case FIELD_ATTRIBUTE_PRIVATE:
6228 return access_klass == member_klass;
6229 case FIELD_ATTRIBUTE_FAM_AND_ASSEM:
6230 if (mono_class_has_parent (access_klass, member_klass) &&
6231 can_access_internals (access_klass->image->assembly, member_klass->image->assembly))
6234 case FIELD_ATTRIBUTE_ASSEMBLY:
6235 return can_access_internals (access_klass->image->assembly, member_klass->image->assembly);
6236 case FIELD_ATTRIBUTE_FAMILY:
6237 if (mono_class_has_parent (access_klass, member_klass))
6240 case FIELD_ATTRIBUTE_FAM_OR_ASSEM:
6241 if (mono_class_has_parent (access_klass, member_klass))
6243 return can_access_internals (access_klass->image->assembly, member_klass->image->assembly);
6244 case FIELD_ATTRIBUTE_PUBLIC:
6251 mono_method_can_access_field (MonoMethod *method, MonoClassField *field)
6253 /* FIXME: check all overlapping fields */
6254 int can = can_access_member (method->klass, field->parent, field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK);
6256 MonoClass *nested = method->klass->nested_in;
6258 can = can_access_member (nested, field->parent, field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK);
6261 nested = nested->nested_in;
6268 mono_method_can_access_method (MonoMethod *method, MonoMethod *called)
6270 int can = can_access_member (method->klass, called->klass, called->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK);
6272 MonoClass *nested = method->klass->nested_in;
6274 can = can_access_member (nested, called->klass, called->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK);
6277 nested = nested->nested_in;
6282 * with generics calls to explicit interface implementations can be expressed
6283 * directly: the method is private, but we must allow it. This may be opening
6284 * a hole or the generics code should handle this differently.
6285 * Maybe just ensure the interface type is public.
6287 if ((called->flags & METHOD_ATTRIBUTE_VIRTUAL) && (called->flags & METHOD_ATTRIBUTE_FINAL))
6293 * mono_type_is_valid_enum_basetype:
6294 * @type: The MonoType to check
6296 * Returns: TRUE if the type can be used as the basetype of an enum
6298 gboolean mono_type_is_valid_enum_basetype (MonoType * type) {
6299 switch (type->type) {
6302 case MONO_TYPE_BOOLEAN:
6305 case MONO_TYPE_CHAR:
6318 * mono_class_is_valid_enum:
6319 * @klass: An enum class to be validated
6321 * This method verify the required properties an enum should have.
6323 * Returns: TRUE if the informed enum class is valid
6325 * FIXME: TypeBuilder enums are allowed to implement interfaces, but since they cannot have methods, only empty interfaces are possible
6326 * FIXME: enum types are not allowed to have a cctor, but mono_reflection_create_runtime_class sets has_cctor to 1 for all types
6327 * FIXME: TypeBuilder enums can have any kind of static fields, but the spec is very explicit about that (P II 14.3)
6329 gboolean mono_class_is_valid_enum (MonoClass *klass) {
6330 MonoClassField * field;
6331 gpointer iter = NULL;
6332 gboolean found_base_field = FALSE;
6334 g_assert (klass->enumtype);
6335 /* we cannot test against mono_defaults.enum_class, or mcs won't be able to compile the System namespace*/
6336 if (!klass->parent || strcmp (klass->parent->name, "Enum") || strcmp (klass->parent->name_space, "System") ) {
6340 if ((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) != TYPE_ATTRIBUTE_AUTO_LAYOUT)
6343 while ((field = mono_class_get_fields (klass, &iter))) {
6344 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
6345 if (found_base_field)
6347 found_base_field = TRUE;
6348 if (!mono_type_is_valid_enum_basetype (field->type))
6353 if (!found_base_field)
6356 if (klass->method.count > 0)
6363 mono_generic_class_is_generic_type_definition (MonoGenericClass *gklass)
6365 return gklass->context.class_inst == gklass->container_class->generic_container->context.class_inst;