2 * class.c: Class management for the Mono runtime
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001-2006 Novell, Inc.
16 #include <mono/metadata/image.h>
17 #include <mono/metadata/assembly.h>
18 #include <mono/metadata/cil-coff.h>
19 #include <mono/metadata/metadata.h>
20 #include <mono/metadata/metadata-internals.h>
21 #include <mono/metadata/tabledefs.h>
22 #include <mono/metadata/tokentype.h>
23 #include <mono/metadata/class-internals.h>
24 #include <mono/metadata/object.h>
25 #include <mono/metadata/appdomain.h>
26 #include <mono/metadata/mono-endian.h>
27 #include <mono/metadata/debug-helpers.h>
28 #include <mono/metadata/reflection.h>
29 #include <mono/metadata/exception.h>
30 #include <mono/metadata/security-manager.h>
31 #include <mono/os/gc_wrapper.h>
32 #include <mono/utils/mono-counters.h>
36 gboolean mono_print_vtable = FALSE;
38 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
39 static void mono_class_create_generic (MonoInflatedGenericClass *gclass);
40 static gboolean mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res);
42 void (*mono_debugger_start_class_init_func) (MonoClass *klass) = NULL;
43 void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
46 * mono_class_from_typeref:
48 * @type_token: a TypeRef token
50 * Creates the MonoClass* structure representing the type defined by
51 * the typeref token valid inside @image.
52 * Returns: the MonoClass* representing the typeref token, NULL ifcould
56 mono_class_from_typeref (MonoImage *image, guint32 type_token)
58 guint32 cols [MONO_TYPEREF_SIZE];
59 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
61 const char *name, *nspace;
63 MonoAssembly **references;
65 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
67 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
68 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
70 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
71 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
72 case MONO_RESOLTION_SCOPE_MODULE:
74 g_error ("null ResolutionScope not yet handled");
75 /* a typedef in disguise */
76 return mono_class_from_name (image, nspace, name);
77 case MONO_RESOLTION_SCOPE_MODULEREF:
78 return mono_class_from_name (image->modules [idx - 1], nspace, name);
79 case MONO_RESOLTION_SCOPE_TYPEREF: {
80 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
83 if (enclosing->inited) {
84 /* Micro-optimization: don't scan the metadata tables if enclosing is already inited */
85 for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
87 if (strcmp (res->name, name) == 0)
91 /* Don't call mono_class_init as we might've been called by it recursively */
92 int i = mono_metadata_nesting_typedef (enclosing->image, enclosing->type_token, 1);
94 guint32 class_nested = mono_metadata_decode_row_col (&enclosing->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, MONO_NESTED_CLASS_NESTED);
95 guint32 string_offset = mono_metadata_decode_row_col (&enclosing->image->tables [MONO_TABLE_TYPEDEF], class_nested - 1, MONO_TYPEDEF_NAME);
96 const char *nname = mono_metadata_string_heap (enclosing->image, string_offset);
98 if (strcmp (nname, name) == 0)
99 return mono_class_create_from_typedef (enclosing->image, MONO_TOKEN_TYPE_DEF | class_nested);
101 i = mono_metadata_nesting_typedef (enclosing->image, enclosing->type_token, i + 1);
104 g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
107 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
111 references = image->references;
112 if (!references [idx - 1])
113 mono_assembly_load_reference (image, idx - 1);
114 /* If this assert fails, it probably means that you haven't installed an assembly load/search hook */
115 g_assert (references == image->references);
116 g_assert (references [idx - 1]);
117 if (references [idx - 1] == (gpointer)-1)
120 return mono_class_from_name (references [idx - 1]->image, nspace, name);
123 static inline MonoType*
124 dup_type (MonoType* t, const MonoType *original)
126 MonoType *r = g_new0 (MonoType, 1);
128 r->attrs = original->attrs;
129 r->byref = original->byref;
130 if (t->type == MONO_TYPE_PTR)
131 t->data.type = dup_type (t->data.type, original->data.type);
132 else if (t->type == MONO_TYPE_ARRAY)
133 t->data.array = mono_dup_array_type (t->data.array);
134 else if (t->type == MONO_TYPE_FNPTR)
135 t->data.method = mono_metadata_signature_deep_dup (t->data.method);
136 mono_stats.generics_metadata_size += sizeof (MonoType);
140 /* Copy everything mono_metadata_free_array free. */
142 mono_dup_array_type (MonoArrayType *a)
144 a = g_memdup (a, sizeof (MonoArrayType));
146 a->sizes = g_memdup (a->sizes, a->numsizes * sizeof (int));
148 a->lobounds = g_memdup (a->lobounds, a->numlobounds * sizeof (int));
152 /* Copy everything mono_metadata_free_method_signature free. */
154 mono_metadata_signature_deep_dup (MonoMethodSignature *sig)
158 sig = mono_metadata_signature_dup (sig);
160 sig->ret = dup_type (sig->ret, sig->ret);
161 for (i = 0; i < sig->param_count; ++i)
162 sig->params [i] = dup_type (sig->params [i], sig->params [i]);
168 _mono_type_get_assembly_name (MonoClass *klass, GString *str)
170 MonoAssembly *ta = klass->image->assembly;
172 g_string_append_printf (
173 str, ", %s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s",
175 ta->aname.major, ta->aname.minor, ta->aname.build, ta->aname.revision,
176 ta->aname.culture && *ta->aname.culture? ta->aname.culture: "neutral",
177 ta->aname.public_key_token [0] ? (char *)ta->aname.public_key_token : "null");
181 mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed,
182 MonoTypeNameFormat format)
186 switch (type->type) {
187 case MONO_TYPE_ARRAY: {
188 int i, rank = type->data.array->rank;
189 MonoTypeNameFormat nested_format;
191 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
192 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
194 mono_type_get_name_recurse (
195 &type->data.array->eklass->byval_arg, str, FALSE, nested_format);
196 g_string_append_c (str, '[');
197 for (i = 1; i < rank; i++)
198 g_string_append_c (str, ',');
199 g_string_append_c (str, ']');
200 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
201 _mono_type_get_assembly_name (type->data.array->eklass, str);
204 case MONO_TYPE_SZARRAY: {
205 MonoTypeNameFormat nested_format;
207 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
208 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
210 mono_type_get_name_recurse (
211 &type->data.klass->byval_arg, str, FALSE, nested_format);
212 g_string_append (str, "[]");
213 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
214 _mono_type_get_assembly_name (type->data.klass, str);
217 case MONO_TYPE_PTR: {
218 MonoTypeNameFormat nested_format;
220 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
221 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
223 mono_type_get_name_recurse (
224 type->data.type, str, FALSE, nested_format);
225 g_string_append (str, "*");
226 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
227 _mono_type_get_assembly_name (type->data.klass, str);
232 g_assert (type->data.generic_param->name);
233 g_string_append (str, type->data.generic_param->name);
236 klass = mono_class_from_mono_type (type);
237 if (klass->nested_in) {
238 mono_type_get_name_recurse (
239 &klass->nested_in->byval_arg, str, TRUE, format);
240 if (format == MONO_TYPE_NAME_FORMAT_IL)
241 g_string_append_c (str, '.');
243 g_string_append_c (str, '+');
244 } else if (*klass->name_space) {
245 g_string_append (str, klass->name_space);
246 g_string_append_c (str, '.');
248 if (format == MONO_TYPE_NAME_FORMAT_IL) {
249 char *s = strchr (klass->name, '`');
250 int len = s ? s - klass->name : strlen (klass->name);
252 g_string_append_len (str, klass->name, len);
254 g_string_append (str, klass->name);
257 if (klass->generic_class) {
258 MonoGenericClass *gclass = klass->generic_class;
259 MonoTypeNameFormat nested_format;
262 nested_format = format == MONO_TYPE_NAME_FORMAT_FULL_NAME ?
263 MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED : format;
265 if (format == MONO_TYPE_NAME_FORMAT_IL)
266 g_string_append_c (str, '<');
268 g_string_append_c (str, '[');
269 for (i = 0; i < gclass->inst->type_argc; i++) {
270 MonoType *t = gclass->inst->type_argv [i];
273 g_string_append_c (str, ',');
274 if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
275 (t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
276 g_string_append_c (str, '[');
277 mono_type_get_name_recurse (
278 gclass->inst->type_argv [i], str, FALSE, nested_format);
279 if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
280 (t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
281 g_string_append_c (str, ']');
283 if (format == MONO_TYPE_NAME_FORMAT_IL)
284 g_string_append_c (str, '>');
286 g_string_append_c (str, ']');
287 } else if (klass->generic_container &&
288 (format != MONO_TYPE_NAME_FORMAT_FULL_NAME) &&
289 (format != MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)) {
292 if (format == MONO_TYPE_NAME_FORMAT_IL)
293 g_string_append_c (str, '<');
295 g_string_append_c (str, '[');
296 for (i = 0; i < klass->generic_container->type_argc; i++) {
298 g_string_append_c (str, ',');
299 g_string_append (str, klass->generic_container->type_params [i].name);
301 if (format == MONO_TYPE_NAME_FORMAT_IL)
302 g_string_append_c (str, '>');
304 g_string_append_c (str, ']');
306 if ((format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
307 (type->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
308 _mono_type_get_assembly_name (klass, str);
314 * mono_type_get_name:
316 * @format: the format for the return string.
319 * Returns: the string representation in a number of formats:
321 * if format is MONO_TYPE_NAME_FORMAT_REFLECTION, the return string is
322 * returned in the formatrequired by System.Reflection, this is the
323 * inverse of mono_reflection_parse_type ().
325 * if format is MONO_TYPE_NAME_FORMAT_IL, it returns a syntax that can
326 * be used by the IL assembler.
328 * if format is MONO_TYPE_NAME_FORMAT_FULL_NAME
330 * if format is MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
333 mono_type_get_name_full (MonoType *type, MonoTypeNameFormat format)
337 result = g_string_new ("");
339 mono_type_get_name_recurse (type, result, FALSE, format);
342 g_string_append_c (result, '&');
344 return g_string_free (result, FALSE);
348 * mono_type_get_full_name:
351 * Returns: the string representation for type as required by System.Reflection.
352 * The inverse of mono_reflection_parse_type ().
355 mono_type_get_full_name (MonoClass *class)
357 return mono_type_get_name_full (mono_class_get_type (class), MONO_TYPE_NAME_FORMAT_REFLECTION);
361 * mono_type_get_name:
364 * Returns: the string representation for type as it would be represented in IL code.
367 mono_type_get_name (MonoType *type)
369 return mono_type_get_name_full (type, MONO_TYPE_NAME_FORMAT_IL);
373 * mono_type_get_underlying_type:
376 * Returns: the MonoType for the underlying interger type if @type
377 * is an enum, otherwise the type itself.
380 mono_type_get_underlying_type (MonoType *type)
382 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
383 return type->data.klass->enum_basetype;
388 * mono_class_is_open_constructed_type:
391 * Returns TRUE if type represents a generics open constructed type
392 * (not all the type parameters required for the instantiation have
396 mono_class_is_open_constructed_type (MonoType *t)
402 case MONO_TYPE_SZARRAY:
403 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
404 case MONO_TYPE_ARRAY:
405 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
407 return mono_class_is_open_constructed_type (t->data.type);
408 case MONO_TYPE_GENERICINST: {
409 MonoGenericClass *gclass = t->data.generic_class;
412 if (mono_class_is_open_constructed_type (&gclass->container_class->byval_arg))
414 for (i = 0; i < gclass->inst->type_argc; i++)
415 if (mono_class_is_open_constructed_type (gclass->inst->type_argv [i]))
424 static MonoGenericClass *
425 inflate_generic_class (MonoGenericClass *ogclass, MonoGenericContext *context)
427 MonoInflatedGenericClass *igclass;
428 MonoGenericClass *ngclass, *cached;
429 MonoGenericInst *ninst;
431 ninst = mono_metadata_inflate_generic_inst (ogclass->inst, context);
432 if (ninst == ogclass->inst)
435 if (ogclass->is_dynamic) {
436 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
437 igclass = &dgclass->generic_class;
438 ngclass = &igclass->generic_class;
439 ngclass->is_inflated = 1;
440 ngclass->is_dynamic = 1;
442 igclass = g_new0 (MonoInflatedGenericClass, 1);
443 ngclass = &igclass->generic_class;
444 ngclass->is_inflated = 1;
449 ngclass->inst = ninst;
451 igclass->klass = NULL;
453 ngclass->context = g_new0 (MonoGenericContext, 1);
454 ngclass->context->container = ngclass->container_class->generic_container;
455 ngclass->context->gclass = ngclass;
458 cached = mono_metadata_lookup_generic_class (ngclass);
459 mono_loader_unlock ();
461 g_free (ngclass->context);
470 inflate_generic_type (MonoType *type, MonoGenericContext *context)
472 switch (type->type) {
473 case MONO_TYPE_MVAR: {
474 int num = type->data.generic_param->num;
475 MonoGenericInst *inst = context->gmethod ? context->gmethod->inst : NULL;
476 if (!inst || !inst->type_argv)
478 if (num >= inst->type_argc)
479 g_error ("MVAR %d (%s) cannot be expanded in this context with %d instantiations", num, type->data.generic_param->name, inst->type_argc);
480 return dup_type (inst->type_argv [num], type);
482 case MONO_TYPE_VAR: {
483 int num = type->data.generic_param->num;
484 MonoGenericInst *inst = context->gclass ? context->gclass->inst : NULL;
487 if (num >= inst->type_argc)
488 g_error ("VAR %d (%s) cannot be expanded in this context with %d instantiations", num, type->data.generic_param->name, inst->type_argc);
489 return dup_type (inst->type_argv [num], type);
491 case MONO_TYPE_SZARRAY: {
492 MonoClass *eclass = type->data.klass;
493 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
496 nt = dup_type (type, type);
497 nt->data.klass = mono_class_from_mono_type (inflated);
500 case MONO_TYPE_ARRAY: {
501 MonoClass *eclass = type->data.array->eklass;
502 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
505 nt = dup_type (type, type);
506 nt->data.array = g_memdup (nt->data.array, sizeof (MonoArrayType));
507 nt->data.array->eklass = mono_class_from_mono_type (inflated);
510 case MONO_TYPE_GENERICINST: {
511 MonoGenericClass *gclass = type->data.generic_class;
513 if (!gclass->inst->is_open)
515 gclass = inflate_generic_class (gclass, context);
516 if (gclass == type->data.generic_class)
518 nt = dup_type (type, type);
519 nt->data.generic_class = gclass;
522 case MONO_TYPE_CLASS:
523 case MONO_TYPE_VALUETYPE: {
524 MonoClass *klass = type->data.klass;
525 MonoGenericClass *gclass;
528 if (!klass->generic_container)
530 gclass = inflate_generic_class (klass->generic_container->context.gclass, context);
531 if (gclass == klass->generic_container->context.gclass)
533 nt = dup_type (type, type);
534 nt->type = MONO_TYPE_GENERICINST;
535 nt->data.generic_class = gclass;
544 MonoInflatedGenericClass*
545 mono_get_inflated_generic_class (MonoGenericClass *gclass)
547 g_assert (gclass->is_inflated);
548 mono_class_create_generic ((MonoInflatedGenericClass *) gclass);
549 return (MonoInflatedGenericClass *) gclass;
553 * mono_class_inflate_generic_type:
555 * @context: a generics context
557 * Instantiate the generic type @type, using the generics context @context.
559 * Returns: the instantiated type
562 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
564 MonoType *inflated = inflate_generic_type (type, context);
567 return dup_type (type, type);
569 mono_stats.inflated_type_count++;
573 static MonoGenericContext *
574 inflate_generic_context (MonoGenericContext *context, MonoGenericContext *inflate_with)
576 MonoGenericClass *gclass = NULL;
577 MonoGenericMethod *gmethod = NULL;
578 MonoGenericContext *res;
581 gclass = inflate_generic_class (context->gclass, inflate_with);
583 if (context->gmethod) {
584 MonoGenericInst *ninst = mono_metadata_inflate_generic_inst (context->gmethod->inst, inflate_with);
585 if (gclass == context->gclass && ninst == context->gmethod->inst) {
586 gmethod = context->gmethod;
588 gmethod = g_new0 (MonoGenericMethod, 1);
589 gmethod->generic_class = gclass;
590 gmethod->container = context->container;
591 gmethod->inst = ninst;
595 if (gclass == context->gclass && gmethod == context->gmethod)
598 res = g_new0 (MonoGenericContext, 1);
600 res->container = gmethod ? gmethod->container : context->container;
601 res->gclass = gclass;
602 res->gmethod = gmethod;
608 * mono_class_inflate_generic_method:
609 * @method: a generic method
610 * @context: a generics context
612 * Instantiate the generic method @method using the generics context @context.
614 * Returns: the new instantiated method
617 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context)
619 return mono_class_inflate_generic_method_full (method, NULL, context);
623 * mono_class_inflate_generic_method:
625 * Instantiate method @method with the generic context @context.
626 * BEWARE: All non-trivial fields are invalid, including klass, signature, and header.
627 * Use mono_get_inflated_method (), mono_method_signature () and mono_method_get_header () to get the correct values.
630 mono_class_inflate_generic_method_full (MonoMethod *method, MonoClass *klass_hint, MonoGenericContext *context)
633 MonoMethodInflated *iresult;
634 MonoMethodSignature *sig;
636 /* The `method' has already been instantiated before -> we need to create a new context. */
637 while (method->is_inflated) {
638 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
639 context = inflate_generic_context (imethod->context, context);
640 if (context == imethod->context)
642 method = imethod->declaring;
645 if (!method->generic_container && !method->klass->generic_container)
648 mono_stats.inflated_method_count++;
649 iresult = g_new0 (MonoMethodInflated, 1);
651 sig = mono_method_signature (method);
653 iresult->method.pinvoke = *(MonoMethodPInvoke*)method;
655 iresult->method.normal = *(MonoMethodNormal*)method;
656 iresult->method.normal.header = NULL;
659 result = (MonoMethod *) iresult;
660 result->is_inflated = 1;
661 result->signature = NULL;
662 iresult->context = context;
663 iresult->declaring = method;
665 if (!klass_hint || !klass_hint->generic_class ||
666 klass_hint->generic_class->container_class != method->klass ||
667 klass_hint->generic_class->inst != context->gclass->inst)
670 if (method->klass->generic_container)
671 result->klass = klass_hint;
673 if (!result->klass) {
674 MonoType *inflated = inflate_generic_type (&method->klass->byval_arg, context);
675 result->klass = inflated ? mono_class_from_mono_type (inflated) : method->klass;
678 if (method->generic_container && !context->gmethod) {
679 MonoGenericMethod *gmethod = g_memdup (method->generic_container->context.gmethod, sizeof (*gmethod));
680 gmethod->generic_class = result->klass->generic_class;
682 context = g_new0 (MonoGenericContext, 1);
683 context->container = method->generic_container;
684 context->gclass = result->klass->generic_class;
685 context->gmethod = gmethod;
687 iresult->context = context;
694 * mono_get_inflated_method:
696 * For performance reasons, mono_class_inflate_generic_method() does not actually instantiate the
697 * method, it just "prepares" it for that. If you really need to fully instantiate the method
698 * (including its signature and header), call this method.
699 * FIXME: Martin? this description looks completely wrong.
702 mono_get_inflated_method (MonoMethod *method)
708 * mono_class_find_enum_basetype:
709 * @class: The enum class
711 * Determine the basetype of an enum by iterating through its fields. We do this
712 * in a separate function since it is cheaper than calling mono_class_setup_fields.
715 mono_class_find_enum_basetype (MonoClass *class)
717 MonoImage *m = class->image;
718 const int top = class->field.count;
719 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
722 g_assert (class->enumtype);
725 * Fetch all the field information.
727 for (i = 0; i < top; i++){
729 guint32 cols [MONO_FIELD_SIZE];
730 int idx = class->field.first + i;
731 MonoGenericContainer *container = NULL;
734 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
735 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
736 mono_metadata_decode_value (sig, &sig);
737 /* FIELD signature == 0x06 */
738 g_assert (*sig == 0x06);
739 if (class->generic_container)
740 container = class->generic_container;
741 else if (class->generic_class) {
742 MonoClass *gklass = class->generic_class->container_class;
744 container = gklass->generic_container;
745 g_assert (container);
747 ftype = mono_metadata_parse_type_full (m, container, MONO_PARSE_FIELD, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
750 if (class->generic_class) {
751 ftype = mono_class_inflate_generic_type (ftype, class->generic_class->context);
752 ftype->attrs = cols [MONO_FIELD_FLAGS];
755 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC))
763 * mono_class_setup_fields:
764 * @class: The class to initialize
766 * Initializes the class->fields.
767 * LOCKING: Assumes the loader lock is held.
770 mono_class_setup_fields (MonoClass *class)
772 MonoImage *m = class->image;
773 int top = class->field.count;
774 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
775 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
776 int i, blittable = TRUE;
777 guint32 real_size = 0;
778 guint32 packing_size = 0;
779 gboolean explicit_size;
780 MonoClassField *field;
781 MonoGenericContainer *container = NULL;
782 MonoClass *gklass = NULL;
784 if (class->size_inited)
787 if (class->generic_class) {
788 MonoClass *gklass = class->generic_class->container_class;
789 mono_class_setup_fields (gklass);
790 top = gklass->field.count;
793 class->instance_size = 0;
794 class->class_size = 0;
797 if (!class->parent->size_inited)
798 mono_class_setup_fields (class->parent);
799 class->instance_size += class->parent->instance_size;
800 class->min_align = class->parent->min_align;
801 /* we use |= since it may have been set already */
802 class->has_references |= class->parent->has_references;
803 blittable = class->parent->blittable;
805 class->instance_size = sizeof (MonoObject);
806 class->min_align = 1;
809 /* Get the real size */
810 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
813 g_assert ((packing_size & 0xfffffff0) == 0);
814 class->packing_size = packing_size;
815 real_size += class->instance_size;
819 if (explicit_size && real_size) {
820 class->instance_size = MAX (real_size, class->instance_size);
822 class->size_inited = 1;
823 class->blittable = blittable;
827 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT)
830 /* Prevent infinite loops if the class references itself */
831 class->size_inited = 1;
833 class->fields = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoClassField) * top);
835 if (class->generic_container) {
836 container = class->generic_container;
837 } else if (class->generic_class) {
838 gklass = class->generic_class->container_class;
839 container = gklass->generic_container;
840 g_assert (container);
842 mono_class_setup_fields (gklass);
846 * Fetch all the field information.
848 for (i = 0; i < top; i++){
849 int idx = class->field.first + i;
850 field = &class->fields [i];
852 field->parent = class;
854 if (class->generic_class) {
855 MonoClassField *gfield = &gklass->fields [i];
856 MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
858 ifield->generic_type = gfield->type;
859 field->name = gfield->name;
860 field->generic_info = ifield;
861 field->type = mono_class_inflate_generic_type (gfield->type, class->generic_class->context);
862 field->type->attrs = gfield->type->attrs;
863 if (mono_field_is_deleted (field))
865 field->offset = gfield->offset;
866 field->data = gfield->data;
870 guint32 cols [MONO_FIELD_SIZE];
872 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
873 /* The name is needed for fieldrefs */
874 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
875 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
876 mono_metadata_decode_value (sig, &sig);
877 /* FIELD signature == 0x06 */
878 g_assert (*sig == 0x06);
879 field->type = mono_metadata_parse_type_full (m, container, MONO_PARSE_FIELD, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
881 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
884 if (mono_field_is_deleted (field))
886 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
888 mono_metadata_field_info (m, idx, &offset, NULL, NULL);
889 field->offset = offset;
890 if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
891 g_warning ("%s not initialized correctly (missing field layout info for %s)",
892 class->name, field->name);
895 if (field->type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
896 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
898 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
899 field->data = mono_image_rva_map (class->image, rva);
903 /* Only do these checks if we still think this type is blittable */
904 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
905 if (field->type->byref || MONO_TYPE_IS_REFERENCE (field->type)) {
908 MonoClass *field_class = mono_class_from_mono_type (field->type);
909 if (!field_class || !field_class->blittable)
914 if (class->enumtype && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
915 class->enum_basetype = field->type;
916 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
917 blittable = class->element_class->blittable;
920 /* The def_value of fields is compute lazily during vtable creation */
923 if (class == mono_defaults.string_class)
926 class->blittable = blittable;
928 if (class->enumtype && !class->enum_basetype) {
929 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
932 if (explicit_size && real_size) {
933 class->instance_size = MAX (real_size, class->instance_size);
936 mono_class_layout_fields (class);
940 * mono_class_setup_fields_locking:
941 * @class: The class to initialize
943 * Initializes the class->fields array of fields.
944 * Aquires the loader lock.
947 mono_class_setup_fields_locking (MonoClass *class)
950 mono_class_setup_fields (class);
951 mono_loader_unlock ();
955 * mono_class_has_references:
957 * Returns whenever @klass->has_references is set, initializing it if needed.
958 * Aquires the loader lock.
961 mono_class_has_references (MonoClass *klass)
963 if (klass->init_pending) {
964 /* Be conservative */
967 mono_class_init (klass);
969 return klass->has_references;
973 /* useful until we keep track of gc-references in corlib etc. */
974 #define IS_GC_REFERENCE(t) ((t)->type == MONO_TYPE_U || (t)->type == MONO_TYPE_I || (t)->type == MONO_TYPE_PTR)
977 * mono_class_layout_fields:
980 * Compute the placement of fields inside an object or struct, according to
981 * the layout rules and set the following fields in @class:
982 * - has_references (if the class contains instance references firled or structs that contain references)
983 * - has_static_refs (same, but for static fields)
984 * - instance_size (size of the object in memory)
985 * - class_size (size needed for the static fields)
986 * - size_inited (flag set when the instance_size is set)
988 * LOCKING: this is supposed to be called with the loader lock held.
991 mono_class_layout_fields (MonoClass *class)
994 const int top = class->field.count;
995 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
996 guint32 pass, passes, real_size;
997 gboolean gc_aware_layout = FALSE;
998 MonoClassField *field;
1000 if (class->generic_container ||
1001 (class->generic_class && class->generic_class->inst->is_open))
1005 * Enable GC aware auto layout: in this mode, reference
1006 * fields are grouped together inside objects, increasing collector
1008 * Requires that all classes whose layout is known to native code be annotated
1009 * with [StructLayout (LayoutKind.Sequential)]
1010 * Value types have gc_aware_layout disabled by default, as per
1011 * what the default is for other runtimes.
1013 /* corlib is missing [StructLayout] directives in many places */
1014 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
1015 if (class->image != mono_defaults.corlib &&
1016 class->byval_arg.type != MONO_TYPE_VALUETYPE)
1017 gc_aware_layout = TRUE;
1020 /* Compute klass->has_references */
1022 * Process non-static fields first, since static fields might recursively
1023 * refer to the class itself.
1025 for (i = 0; i < top; i++) {
1028 field = &class->fields [i];
1030 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
1031 ftype = mono_type_get_underlying_type (field->type);
1032 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype)))))
1033 class->has_references = TRUE;
1037 for (i = 0; i < top; i++) {
1040 field = &class->fields [i];
1042 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC) {
1043 ftype = mono_type_get_underlying_type (field->type);
1044 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype)))))
1045 class->has_static_refs = TRUE;
1049 for (i = 0; i < top; i++) {
1052 field = &class->fields [i];
1054 ftype = mono_type_get_underlying_type (field->type);
1055 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype))))) {
1056 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1057 class->has_static_refs = TRUE;
1059 class->has_references = TRUE;
1064 * Compute field layout and total size (not considering static fields)
1068 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
1069 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
1071 if (gc_aware_layout)
1076 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
1080 real_size = class->parent->instance_size;
1082 real_size = sizeof (MonoObject);
1084 for (pass = 0; pass < passes; ++pass) {
1085 for (i = 0; i < top; i++){
1088 field = &class->fields [i];
1090 if (mono_field_is_deleted (field))
1092 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1095 if (gc_aware_layout) {
1097 * We process fields with reference type in the first pass,
1098 * and fields with non-reference type in the second pass.
1099 * We use IS_POINTER instead of IS_REFERENCE because in
1100 * some internal structures, we store GC_MALLOCed memory
1101 * in IntPtr fields...
1103 if (MONO_TYPE_IS_POINTER (field->type)) {
1112 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
1113 (strcmp (field->name, "$PRIVATE$") == 0)) {
1114 /* This field is a hack inserted by MCS to empty structures */
1118 size = mono_type_size (field->type, &align);
1120 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
1121 align = class->packing_size ? MIN (class->packing_size, align): align;
1122 class->min_align = MAX (align, class->min_align);
1123 field->offset = real_size;
1124 field->offset += align - 1;
1125 field->offset &= ~(align - 1);
1126 real_size = field->offset + size;
1129 class->instance_size = MAX (real_size, class->instance_size);
1131 if (class->instance_size & (class->min_align - 1)) {
1132 class->instance_size += class->min_align - 1;
1133 class->instance_size &= ~(class->min_align - 1);
1137 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
1139 for (i = 0; i < top; i++) {
1142 field = &class->fields [i];
1145 * There must be info about all the fields in a type if it
1146 * uses explicit layout.
1149 if (mono_field_is_deleted (field))
1151 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1154 size = mono_type_size (field->type, &align);
1157 * When we get here, field->offset is already set by the
1158 * loader (for either runtime fields or fields loaded from metadata).
1159 * The offset is from the start of the object: this works for both
1160 * classes and valuetypes.
1162 field->offset += sizeof (MonoObject);
1167 real_size = MAX (real_size, size + field->offset);
1169 class->instance_size = MAX (real_size, class->instance_size);
1173 class->size_inited = 1;
1176 * Compute static field layout and size
1178 for (i = 0; i < top; i++){
1181 field = &class->fields [i];
1183 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
1185 if (mono_field_is_deleted (field))
1188 size = mono_type_size (field->type, &align);
1189 field->offset = class->class_size;
1190 field->offset += align - 1;
1191 field->offset &= ~(align - 1);
1192 class->class_size = field->offset + size;
1197 * mono_class_setup_methods:
1200 * Initializes the 'methods' array in the klass.
1201 * Calling this method should be avoided if possible since it allocates a lot
1202 * of long-living MonoMethod structures.
1203 * Methods belonging to an interface are assigned a sequential slot starting
1207 mono_class_setup_methods (MonoClass *class)
1210 MonoMethod **methods;
1215 mono_loader_lock ();
1217 if (class->methods) {
1218 mono_loader_unlock ();
1222 //printf ("INIT: %s.%s\n", class->name_space, class->name);
1224 if (!class->methods) {
1225 methods = mono_mempool_alloc (class->image->mempool, sizeof (MonoMethod*) * class->method.count);
1226 for (i = 0; i < class->method.count; ++i) {
1227 methods [i] = mono_get_method (class->image, MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
1231 if (MONO_CLASS_IS_INTERFACE (class))
1232 for (i = 0; i < class->method.count; ++i)
1233 methods [i]->slot = i;
1235 /* Leave this assignment as the last op in this function */
1236 class->methods = methods;
1238 mono_loader_unlock ();
1243 mono_class_setup_properties (MonoClass *class)
1245 guint startm, endm, i, j;
1246 guint32 cols [MONO_PROPERTY_SIZE];
1247 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
1248 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
1249 MonoProperty *properties;
1252 if (class->properties)
1255 mono_loader_lock ();
1257 if (class->properties) {
1258 mono_loader_unlock ();
1262 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &last);
1263 class->property.count = last - class->property.first;
1265 if (class->property.count)
1266 mono_class_setup_methods (class);
1268 properties = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoProperty) * class->property.count);
1269 for (i = class->property.first; i < last; ++i) {
1270 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
1271 properties [i - class->property.first].parent = class;
1272 properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
1273 properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
1275 startm = mono_metadata_methods_from_property (class->image, i, &endm);
1276 for (j = startm; j < endm; ++j) {
1277 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
1278 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1279 case METHOD_SEMANTIC_SETTER:
1280 properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1282 case METHOD_SEMANTIC_GETTER:
1283 properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1291 /* Leave this assignment as the last op in the function */
1292 class->properties = properties;
1294 mono_loader_unlock ();
1298 * LOCKING: assumes the loader lock is held.
1301 inflate_event (MonoClass *class, MonoEvent *event, MonoInflatedGenericClass *gclass)
1303 event->parent = class;
1306 MonoMethod *inflated = mono_class_inflate_generic_method_full (
1307 event->add, class, gclass->generic_class.context);
1309 event->add = mono_get_inflated_method (inflated);
1312 if (event->remove) {
1313 MonoMethod *inflated = mono_class_inflate_generic_method_full (
1314 event->remove, class, gclass->generic_class.context);
1316 event->remove = mono_get_inflated_method (inflated);
1320 MonoMethod *inflated = mono_class_inflate_generic_method_full (
1321 event->raise, class, gclass->generic_class.context);
1323 event->raise = mono_get_inflated_method (inflated);
1327 MonoMethod **om = event->other;
1334 event->other = g_new0 (MonoMethod*, count + 1);
1337 MonoMethod *inflated = mono_class_inflate_generic_method_full (
1338 *om, class, gclass->generic_class.context);
1340 event->other [count++] = mono_get_inflated_method (inflated);
1347 mono_class_setup_events (MonoClass *class)
1349 guint startm, endm, i, j;
1350 guint32 cols [MONO_EVENT_SIZE];
1351 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
1352 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
1359 mono_loader_lock ();
1361 if (class->events) {
1362 mono_loader_unlock ();
1366 if (class->generic_class) {
1367 MonoInflatedGenericClass *gclass;
1370 gclass = mono_get_inflated_generic_class (class->generic_class);
1371 gklass = gclass->generic_class.container_class;
1373 mono_class_setup_events (gklass);
1374 class->event = gklass->event;
1376 class->events = g_new0 (MonoEvent, class->event.count);
1378 for (i = 0; i < class->event.count; i++) {
1379 class->events [i] = gklass->events [i];
1380 inflate_event (class, &class->events [i], gclass);
1383 mono_loader_unlock ();
1387 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &last);
1388 class->event.count = last - class->event.first;
1390 if (class->event.count)
1391 mono_class_setup_methods (class);
1393 events = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoEvent) * class->event.count);
1394 for (i = class->event.first; i < last; ++i) {
1395 MonoEvent *event = &events [i - class->event.first];
1397 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
1398 event->parent = class;
1399 event->attrs = cols [MONO_EVENT_FLAGS];
1400 event->name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
1402 startm = mono_metadata_methods_from_event (class->image, i, &endm);
1403 for (j = startm; j < endm; ++j) {
1404 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
1405 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1406 case METHOD_SEMANTIC_ADD_ON:
1407 event->add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1409 case METHOD_SEMANTIC_REMOVE_ON:
1410 event->remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1412 case METHOD_SEMANTIC_FIRE:
1413 event->raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1415 case METHOD_SEMANTIC_OTHER: {
1418 if (event->other == NULL) {
1419 event->other = g_new0 (MonoMethod*, 2);
1421 while (event->other [n])
1423 event->other = g_realloc (event->other, (n + 2) * sizeof (MonoMethod*));
1425 event->other [n] = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1426 /* NULL terminated */
1427 event->other [n + 1] = NULL;
1435 /* Leave this assignment as the last op in the function */
1436 class->events = events;
1438 mono_loader_unlock ();
1442 * Global pool of interface IDs, represented as a bitset.
1443 * LOCKING: this is supposed to be accessed with the loader lock held.
1445 static MonoBitSet *global_interface_bitset = NULL;
1448 * mono_unload_interface_ids:
1449 * @bitset: bit set of interface IDs
1451 * When an image is unloaded, the interface IDs associated with
1452 * the image are put back in the global pool of IDs so the numbers
1456 mono_unload_interface_ids (MonoBitSet *bitset)
1458 mono_loader_lock ();
1459 mono_bitset_sub (global_interface_bitset, bitset);
1460 mono_loader_unlock ();
1464 * mono_get_unique_iid:
1467 * Assign a unique integer ID to the interface represented by @class.
1468 * The ID will positive and as small as possible.
1469 * LOCKING: this is supposed to be called with the loader lock held.
1470 * Returns: the new ID.
1473 mono_get_unique_iid (MonoClass *class)
1477 g_assert (MONO_CLASS_IS_INTERFACE (class));
1479 if (!global_interface_bitset) {
1480 global_interface_bitset = mono_bitset_new (128, 0);
1483 iid = mono_bitset_find_first_unset (global_interface_bitset, -1);
1485 int old_size = mono_bitset_size (global_interface_bitset);
1486 MonoBitSet *new_set = mono_bitset_clone (global_interface_bitset, old_size * 2);
1487 mono_bitset_free (global_interface_bitset);
1488 global_interface_bitset = new_set;
1491 mono_bitset_set (global_interface_bitset, iid);
1492 /* set the bit also in the per-image set */
1493 if (class->image->interface_bitset) {
1494 if (iid >= mono_bitset_size (class->image->interface_bitset)) {
1495 MonoBitSet *new_set = mono_bitset_clone (class->image->interface_bitset, iid + 1);
1496 mono_bitset_free (class->image->interface_bitset);
1497 class->image->interface_bitset = new_set;
1500 class->image->interface_bitset = mono_bitset_new (iid + 1, 0);
1502 mono_bitset_set (class->image->interface_bitset, iid);
1504 if (mono_print_vtable) {
1506 char *type_name = mono_type_full_name (&class->byval_arg);
1507 if (class->generic_class && !class->generic_class->inst->is_open) {
1508 generic_id = class->generic_class->inst->id;
1509 g_assert (generic_id != 0);
1513 printf ("Interface: assigned id %d to %s|%s|%d\n", iid, class->image->name, type_name, generic_id);
1517 g_assert (iid <= 65535);
1522 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
1527 for (i = 0; i < klass->interface_count; i++) {
1528 ic = klass->interfaces [i];
1531 *res = g_ptr_array_new ();
1532 g_ptr_array_add (*res, ic);
1533 mono_class_init (ic);
1535 collect_implemented_interfaces_aux (ic, res);
1540 mono_class_get_implemented_interfaces (MonoClass *klass)
1542 GPtrArray *res = NULL;
1544 collect_implemented_interfaces_aux (klass, &res);
1548 typedef struct _IOffsetInfo IOffsetInfo;
1549 struct _IOffsetInfo {
1553 int data [MONO_ZERO_LEN_ARRAY];
1556 static IOffsetInfo *cached_offset_info = NULL;
1557 static int next_offset_info_size = 128;
1560 cache_interface_offsets (int max_iid, int *data)
1562 IOffsetInfo *cached_info;
1565 for (cached_info = cached_offset_info; cached_info; cached_info = cached_info->next) {
1566 cached = cached_info->data;
1567 while (cached < cached_info->data + cached_info->size && *cached) {
1568 if (*cached == max_iid) {
1569 int i, matched = TRUE;
1571 for (i = 0; i < max_iid; ++i) {
1572 if (cached [i] != data [i]) {
1581 cached += *cached + 1;
1585 /* find a free slot */
1586 for (cached_info = cached_offset_info; cached_info; cached_info = cached_info->next) {
1587 if (cached_info->size - cached_info->next_free >= max_iid + 1) {
1588 cached = &cached_info->data [cached_info->next_free];
1589 *cached++ = max_iid;
1590 memcpy (cached, data, max_iid * sizeof (int));
1591 cached_info->next_free += max_iid + 1;
1595 /* allocate a new chunk */
1596 if (max_iid + 1 < next_offset_info_size) {
1597 new_size = next_offset_info_size;
1598 if (next_offset_info_size < 4096)
1599 next_offset_info_size += next_offset_info_size >> 2;
1601 new_size = max_iid + 1;
1603 cached_info = g_malloc0 (sizeof (IOffsetInfo) + sizeof (int) * new_size);
1604 cached_info->size = new_size;
1605 /*g_print ("allocated %d offset entries at %p (total: %d)\n", new_size, cached_info->data, offset_info_total_size);*/
1606 cached = &cached_info->data [0];
1607 *cached++ = max_iid;
1608 memcpy (cached, data, max_iid * sizeof (int));
1609 cached_info->next_free += max_iid + 1;
1610 cached_info->next = cached_offset_info;
1611 cached_offset_info = cached_info;
1616 * LOCKING: this is supposed to be called with the loader lock held.
1619 setup_interface_offsets (MonoClass *class, int cur_slot)
1626 /* compute maximum number of slots and maximum interface id */
1628 for (k = class; k ; k = k->parent) {
1629 for (i = 0; i < k->interface_count; i++) {
1630 ic = k->interfaces [i];
1633 mono_class_init (ic);
1635 if (max_iid < ic->interface_id)
1636 max_iid = ic->interface_id;
1638 ifaces = mono_class_get_implemented_interfaces (k);
1640 for (i = 0; i < ifaces->len; ++i) {
1641 ic = g_ptr_array_index (ifaces, i);
1642 if (max_iid < ic->interface_id)
1643 max_iid = ic->interface_id;
1645 g_ptr_array_free (ifaces, TRUE);
1649 if (MONO_CLASS_IS_INTERFACE (class)) {
1650 if (max_iid < class->interface_id)
1651 max_iid = class->interface_id;
1653 class->max_interface_id = max_iid;
1654 /* compute vtable offset for interfaces */
1655 class->interface_offsets = g_malloc (sizeof (int) * (max_iid + 1));
1657 for (i = 0; i <= max_iid; i++)
1658 class->interface_offsets [i] = -1;
1660 ifaces = mono_class_get_implemented_interfaces (class);
1662 for (i = 0; i < ifaces->len; ++i) {
1663 ic = g_ptr_array_index (ifaces, i);
1664 class->interface_offsets [ic->interface_id] = cur_slot;
1665 cur_slot += ic->method.count;
1667 g_ptr_array_free (ifaces, TRUE);
1670 for (k = class->parent; k ; k = k->parent) {
1671 ifaces = mono_class_get_implemented_interfaces (k);
1673 for (i = 0; i < ifaces->len; ++i) {
1674 ic = g_ptr_array_index (ifaces, i);
1676 if (class->interface_offsets [ic->interface_id] == -1) {
1677 int io = k->interface_offsets [ic->interface_id];
1681 class->interface_offsets [ic->interface_id] = io;
1684 g_ptr_array_free (ifaces, TRUE);
1688 if (MONO_CLASS_IS_INTERFACE (class))
1689 class->interface_offsets [class->interface_id] = cur_slot;
1691 cached_data = cache_interface_offsets (max_iid + 1, class->interface_offsets);
1692 g_free (class->interface_offsets);
1693 class->interface_offsets = cached_data;
1699 mono_class_setup_vtable (MonoClass *class)
1701 MonoMethod **overrides;
1702 MonoGenericContext *context;
1709 mono_class_setup_methods (class);
1711 if (MONO_CLASS_IS_INTERFACE (class))
1714 mono_loader_lock ();
1716 if (class->vtable) {
1717 mono_loader_unlock ();
1721 if (class->generic_class) {
1722 context = class->generic_class->context;
1723 type_token = class->generic_class->container_class->type_token;
1725 context = (MonoGenericContext *) class->generic_container;
1726 type_token = class->type_token;
1729 if (class->image->dynamic)
1730 mono_reflection_get_dynamic_overrides (class, &overrides, &onum);
1732 mono_class_get_overrides_full (class->image, type_token, &overrides, &onum, context);
1734 mono_class_setup_vtable_general (class, overrides, onum);
1737 mono_loader_unlock ();
1741 * LOCKING: this is supposed to be called with the loader lock held.
1744 mono_class_setup_vtable_general (MonoClass *class, MonoMethod **overrides, int onum)
1747 MonoMethod **vtable;
1748 int i, max_vtsize = 0, max_iid, cur_slot = 0;
1749 GPtrArray *ifaces, *pifaces = NULL;
1750 GHashTable *override_map = NULL;
1751 gboolean security_enabled = mono_is_security_manager_active ();
1756 ifaces = mono_class_get_implemented_interfaces (class);
1758 for (i = 0; i < ifaces->len; i++) {
1759 MonoClass *ic = g_ptr_array_index (ifaces, i);
1760 max_vtsize += ic->method.count;
1762 g_ptr_array_free (ifaces, TRUE);
1765 if (class->parent) {
1766 mono_class_init (class->parent);
1767 mono_class_setup_vtable (class->parent);
1768 max_vtsize += class->parent->vtable_size;
1769 cur_slot = class->parent->vtable_size;
1772 max_vtsize += class->method.count;
1774 vtable = alloca (sizeof (gpointer) * max_vtsize);
1775 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
1777 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
1779 cur_slot = setup_interface_offsets (class, cur_slot);
1780 max_iid = class->max_interface_id;
1782 if (class->parent && class->parent->vtable_size)
1783 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
1785 /* override interface methods */
1786 for (i = 0; i < onum; i++) {
1787 MonoMethod *decl = overrides [i*2];
1788 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
1790 mono_class_setup_methods (decl->klass);
1791 g_assert (decl->slot != -1);
1792 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
1793 vtable [dslot] = overrides [i*2 + 1];
1794 vtable [dslot]->slot = dslot;
1796 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1798 g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
1802 for (k = class; k ; k = k->parent) {
1805 ifaces = mono_class_get_implemented_interfaces (k);
1807 nifaces = ifaces->len;
1808 if (k->generic_class) {
1809 pifaces = mono_class_get_implemented_interfaces (
1810 k->generic_class->container_class);
1811 g_assert (pifaces && (pifaces->len == nifaces));
1814 for (i = 0; i < nifaces; i++) {
1815 MonoClass *pic = NULL;
1818 ic = g_ptr_array_index (ifaces, i);
1820 pic = g_ptr_array_index (pifaces, i);
1821 g_assert (ic->interface_id <= k->max_interface_id);
1822 io = k->interface_offsets [ic->interface_id];
1825 g_assert (io <= max_vtsize);
1828 mono_class_setup_methods (ic);
1829 for (l = 0; l < ic->method.count; l++) {
1830 MonoMethod *im = ic->methods [l];
1832 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1835 for (j = 0; j < class->method.count; ++j) {
1836 MonoMethod *cm = class->methods [j];
1837 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1838 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
1839 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
1841 if (!strcmp(cm->name, im->name) &&
1842 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1844 /* CAS - SecurityAction.InheritanceDemand on interface */
1845 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1846 mono_secman_inheritancedemand_method (cm, im);
1849 g_assert (io + l <= max_vtsize);
1850 vtable [io + l] = cm;
1855 /* already implemented */
1856 if (io >= k->vtable_size)
1860 for (l = 0; l < ic->method.count; l++) {
1861 MonoMethod *im = ic->methods [l];
1864 g_assert (io + l <= max_vtsize);
1866 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1869 for (k1 = class; k1; k1 = k1->parent) {
1870 for (j = 0; j < k1->method.count; ++j) {
1871 MonoMethod *cm = k1->methods [j];
1873 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1874 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
1877 if (!strcmp(cm->name, im->name) &&
1878 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1880 /* CAS - SecurityAction.InheritanceDemand on interface */
1881 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1882 mono_secman_inheritancedemand_method (cm, im);
1885 g_assert (io + l <= max_vtsize);
1886 vtable [io + l] = cm;
1891 g_assert (io + l <= max_vtsize);
1892 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1897 for (l = 0; l < ic->method.count; l++) {
1898 MonoMethod *im = ic->methods [l];
1899 char *qname, *fqname, *cname, *the_cname;
1902 if (vtable [io + l])
1906 the_cname = mono_type_get_name_full (&pic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
1910 cname = (char*)ic->name;
1913 qname = g_strconcat (cname, ".", im->name, NULL);
1914 if (ic->name_space && ic->name_space [0])
1915 fqname = g_strconcat (ic->name_space, ".", cname, ".", im->name, NULL);
1919 for (k1 = class; k1; k1 = k1->parent) {
1920 for (j = 0; j < k1->method.count; ++j) {
1921 MonoMethod *cm = k1->methods [j];
1923 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1926 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
1927 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1929 /* CAS - SecurityAction.InheritanceDemand on interface */
1930 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1931 mono_secman_inheritancedemand_method (cm, im);
1934 g_assert (io + l <= max_vtsize);
1935 vtable [io + l] = cm;
1946 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
1947 for (l = 0; l < ic->method.count; l++) {
1949 MonoMethod *im = ic->methods [l];
1950 if (im->flags & METHOD_ATTRIBUTE_STATIC)
1952 g_assert (io + l <= max_vtsize);
1955 * If one of our parents already implements this interface
1956 * we can inherit the implementation.
1958 if (!(vtable [io + l])) {
1959 MonoClass *parent = class->parent;
1961 if ((ic->interface_id <= parent->max_interface_id) &&
1962 (parent->interface_offsets [ic->interface_id] != -1) &&
1964 vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
1967 if (!(vtable [io + l])) {
1968 for (j = 0; j < onum; ++j) {
1969 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
1970 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
1972 msig = mono_signature_get_desc (mono_method_signature (im), FALSE);
1973 printf ("no implementation for interface method %s::%s(%s) in class %s.%s\n",
1974 mono_type_get_name (&ic->byval_arg), im->name, msig, class->name_space, class->name);
1976 for (j = 0; j < class->method.count; ++j) {
1977 MonoMethod *cm = class->methods [j];
1978 msig = mono_signature_get_desc (mono_method_signature (cm), TRUE);
1980 printf ("METHOD %s(%s)\n", cm->name, msig);
1983 g_assert_not_reached ();
1988 for (l = 0; l < ic->method.count; l++) {
1989 MonoMethod *im = vtable [io + l];
1992 g_assert (io + l <= max_vtsize);
1994 /* FIXME: why do we need this ? */
1996 /* g_assert_not_reached (); */
2002 g_ptr_array_free (ifaces, TRUE);
2005 for (i = 0; i < class->method.count; ++i) {
2008 cm = class->methods [i];
2011 * Non-virtual method have no place in the vtable.
2012 * This also catches static methods (since they are not virtual).
2014 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
2018 * If the method is REUSE_SLOT, we must check in the
2019 * base class for a method to override.
2021 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
2023 for (k = class->parent; k ; k = k->parent) {
2025 for (j = 0; j < k->method.count; ++j) {
2026 MonoMethod *m1 = k->methods [j];
2027 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
2029 if (!strcmp(cm->name, m1->name) &&
2030 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (m1))) {
2032 /* CAS - SecurityAction.InheritanceDemand */
2033 if (security_enabled && (m1->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
2034 mono_secman_inheritancedemand_method (cm, m1);
2037 slot = k->methods [j]->slot;
2038 g_assert (cm->slot < max_vtsize);
2040 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
2041 g_hash_table_insert (override_map, m1, cm);
2053 cm->slot = cur_slot++;
2055 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
2056 vtable [cm->slot] = cm;
2059 /* override non interface methods */
2060 for (i = 0; i < onum; i++) {
2061 MonoMethod *decl = overrides [i*2];
2062 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
2063 g_assert (decl->slot != -1);
2064 vtable [decl->slot] = overrides [i*2 + 1];
2065 overrides [i * 2 + 1]->slot = decl->slot;
2067 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
2068 g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
2073 * If a method occupies more than one place in the vtable, and it is
2074 * overriden, then change the other occurances too.
2077 for (i = 0; i < max_vtsize; ++i)
2079 MonoMethod *cm = g_hash_table_lookup (override_map, vtable [i]);
2084 g_hash_table_destroy (override_map);
2087 if (class->generic_class) {
2088 MonoClass *gklass = class->generic_class->container_class;
2090 mono_class_init (gklass);
2092 class->vtable_size = MAX (gklass->vtable_size, cur_slot);
2094 class->vtable_size = cur_slot;
2096 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
2097 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
2099 if (mono_print_vtable) {
2102 for (i = 0; i <= max_iid; i++)
2103 if (class->interface_offsets [i] != -1)
2106 printf ("VTable %s (vtable entries = %d, interfaces = %d)\n", mono_type_full_name (&class->byval_arg),
2107 class->vtable_size, icount);
2109 for (i = 0; i < class->vtable_size; ++i) {
2114 printf (" slot assigned: %03d, slot index: %03d %s\n", i, cm->slot,
2115 mono_method_full_name (cm, TRUE));
2121 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
2122 class->name, max_iid);
2124 for (i = 0; i < class->interface_count; i++) {
2125 ic = class->interfaces [i];
2126 printf (" slot offset: %03d, method count: %03d, iid: %03d %s\n",
2127 class->interface_offsets [ic->interface_id],
2128 ic->method.count, ic->interface_id, mono_type_full_name (&ic->byval_arg));
2131 for (k = class->parent; k ; k = k->parent) {
2132 for (i = 0; i < k->interface_count; i++) {
2133 ic = k->interfaces [i];
2134 printf (" slot offset: %03d, method count: %03d, iid: %03d %s\n",
2135 class->interface_offsets [ic->interface_id],
2136 ic->method.count, ic->interface_id, mono_type_full_name (&ic->byval_arg));
2143 static MonoMethod *default_ghc = NULL;
2144 static MonoMethod *default_finalize = NULL;
2145 static int finalize_slot = -1;
2146 static int ghc_slot = -1;
2149 initialize_object_slots (MonoClass *class)
2154 if (class == mono_defaults.object_class) {
2155 mono_class_setup_vtable (class);
2156 for (i = 0; i < class->vtable_size; ++i) {
2157 MonoMethod *cm = class->vtable [i];
2159 if (!strcmp (cm->name, "GetHashCode"))
2161 else if (!strcmp (cm->name, "Finalize"))
2165 g_assert (ghc_slot > 0);
2166 default_ghc = class->vtable [ghc_slot];
2168 g_assert (finalize_slot > 0);
2169 default_finalize = class->vtable [finalize_slot];
2175 * @class: the class to initialize
2177 * compute the instance_size, class_size and other infos that cannot be
2178 * computed at mono_class_get() time. Also compute a generic vtable and
2179 * the method slot numbers. We use this infos later to create a domain
2183 mono_class_init (MonoClass *class)
2186 MonoCachedClassInfo cached_info;
2187 gboolean has_cached_info;
2194 /*g_print ("Init class %s\n", class->name);*/
2196 /* We do everything inside the lock to prevent races */
2197 mono_loader_lock ();
2199 if (class->inited) {
2200 mono_loader_unlock ();
2201 /* Somebody might have gotten in before us */
2205 if (class->init_pending) {
2206 mono_loader_unlock ();
2207 /* this indicates a cyclic dependency */
2208 g_error ("pending init %s.%s\n", class->name_space, class->name);
2211 class->init_pending = 1;
2213 /* CAS - SecurityAction.InheritanceDemand */
2214 if (mono_is_security_manager_active () && class->parent && (class->parent->flags & TYPE_ATTRIBUTE_HAS_SECURITY)) {
2215 mono_secman_inheritancedemand_class (class, class->parent);
2218 if (mono_debugger_start_class_init_func)
2219 mono_debugger_start_class_init_func (class);
2221 mono_stats.initialized_class_count++;
2223 if (class->generic_class && !class->generic_class->is_dynamic) {
2224 MonoInflatedGenericClass *gclass;
2227 gclass = mono_get_inflated_generic_class (class->generic_class);
2228 gklass = gclass->generic_class.container_class;
2230 mono_stats.generic_class_count++;
2232 class->method = gklass->method;
2233 class->field = gklass->field;
2235 mono_class_init (gklass);
2236 mono_class_setup_methods (gklass);
2237 mono_class_setup_properties (gklass);
2239 if (MONO_CLASS_IS_INTERFACE (class))
2240 class->interface_id = mono_get_unique_iid (class);
2242 g_assert (class->method.count == gklass->method.count);
2243 class->methods = g_new0 (MonoMethod *, class->method.count);
2245 for (i = 0; i < class->method.count; i++) {
2246 MonoMethod *inflated = mono_class_inflate_generic_method_full (
2247 gklass->methods [i], class, gclass->generic_class.context);
2249 class->methods [i] = mono_get_inflated_method (inflated);
2252 class->property = gklass->property;
2253 class->properties = g_new0 (MonoProperty, class->property.count);
2255 for (i = 0; i < class->property.count; i++) {
2256 MonoProperty *prop = &class->properties [i];
2258 *prop = gklass->properties [i];
2261 prop->get = mono_class_inflate_generic_method_full (
2262 prop->get, class, gclass->generic_class.context);
2264 prop->set = mono_class_inflate_generic_method_full (
2265 prop->set, class, gclass->generic_class.context);
2267 prop->parent = class;
2270 g_assert (class->interface_count == gklass->interface_count);
2273 if (class->parent && !class->parent->inited)
2274 mono_class_init (class->parent);
2276 has_cached_info = mono_class_get_cached_class_info (class, &cached_info);
2278 if (!class->generic_class && (!has_cached_info || (has_cached_info && cached_info.has_nested_classes))) {
2279 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
2282 guint32 cols [MONO_NESTED_CLASS_SIZE];
2283 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
2284 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
2285 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
2287 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
2292 * Computes the size used by the fields, and their locations
2294 if (has_cached_info) {
2295 class->instance_size = cached_info.instance_size;
2296 class->class_size = cached_info.class_size;
2297 class->packing_size = cached_info.packing_size;
2298 class->min_align = cached_info.min_align;
2299 class->blittable = cached_info.blittable;
2300 class->has_references = cached_info.has_references;
2301 class->has_static_refs = cached_info.has_static_refs;
2304 if (!class->size_inited)
2305 mono_class_setup_fields (class);
2307 /* initialize method pointers */
2310 MonoMethodSignature *sig;
2311 class->method.count = class->rank > 1? 2: 1;
2312 sig = mono_metadata_signature_alloc (class->image, class->rank);
2313 sig->ret = &mono_defaults.void_class->byval_arg;
2314 sig->pinvoke = TRUE;
2315 for (i = 0; i < class->rank; ++i)
2316 sig->params [i] = &mono_defaults.int32_class->byval_arg;
2318 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
2319 ctor->klass = class;
2320 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
2321 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
2322 ctor->signature = sig;
2323 ctor->name = ".ctor";
2325 class->methods = g_new (MonoMethod*, class->method.count);
2326 class->methods [0] = ctor;
2327 if (class->rank > 1) {
2328 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
2329 sig->ret = &mono_defaults.void_class->byval_arg;
2330 sig->pinvoke = TRUE;
2331 for (i = 0; i < class->rank * 2; ++i)
2332 sig->params [i] = &mono_defaults.int32_class->byval_arg;
2334 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
2335 ctor->klass = class;
2336 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
2337 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
2338 ctor->signature = sig;
2339 ctor->name = ".ctor";
2341 class->methods [1] = ctor;
2345 mono_class_setup_supertypes (class);
2348 initialize_object_slots (class);
2351 * If possible, avoid the creation of the generic vtable by requesting
2352 * cached info from the runtime.
2354 if (has_cached_info) {
2355 guint32 cur_slot = 0;
2357 class->vtable_size = cached_info.vtable_size;
2358 class->has_finalize = cached_info.has_finalize;
2359 class->ghcimpl = cached_info.ghcimpl;
2360 class->has_cctor = cached_info.has_cctor;
2362 if (class->parent) {
2363 mono_class_init (class->parent);
2364 cur_slot = class->parent->vtable_size;
2367 setup_interface_offsets (class, cur_slot);
2370 mono_class_setup_vtable (class);
2373 if (class->parent) {
2374 MonoMethod *cmethod = class->vtable [ghc_slot];
2375 if (cmethod->is_inflated)
2376 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
2377 if (cmethod == default_ghc) {
2382 /* Object::Finalize should have empty implemenatation */
2383 class->has_finalize = 0;
2384 if (class->parent) {
2385 MonoMethod *cmethod = class->vtable [finalize_slot];
2386 if (cmethod->is_inflated)
2387 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
2388 if (cmethod != default_finalize) {
2389 class->has_finalize = 1;
2393 for (i = 0; i < class->method.count; ++i) {
2394 MonoMethod *method = class->methods [i];
2395 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2396 (strcmp (".cctor", method->name) == 0)) {
2397 class->has_cctor = 1;
2403 if (MONO_CLASS_IS_INTERFACE (class)) {
2405 * class->interface_offsets is needed for the castclass/isinst code, so
2406 * we have to setup them for interfaces, too.
2408 setup_interface_offsets (class, 0);
2412 class->init_pending = 0;
2414 mono_loader_unlock ();
2416 if (mono_debugger_class_init_func)
2417 mono_debugger_class_init_func (class);
2421 * LOCKING: this assumes the loader lock is held
2424 mono_class_setup_mono_type (MonoClass *class)
2426 const char *name = class->name;
2427 const char *nspace = class->name_space;
2429 class->this_arg.byref = 1;
2430 class->this_arg.data.klass = class;
2431 class->this_arg.type = MONO_TYPE_CLASS;
2432 class->byval_arg.data.klass = class;
2433 class->byval_arg.type = MONO_TYPE_CLASS;
2435 if (!strcmp (nspace, "System")) {
2436 if (!strcmp (name, "ValueType")) {
2438 * do not set the valuetype bit for System.ValueType.
2439 * class->valuetype = 1;
2441 class->blittable = TRUE;
2442 } else if (!strcmp (name, "Enum")) {
2444 * do not set the valuetype bit for System.Enum.
2445 * class->valuetype = 1;
2447 class->valuetype = 0;
2448 class->enumtype = 0;
2449 } else if (!strcmp (name, "Object")) {
2450 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
2451 } else if (!strcmp (name, "String")) {
2452 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
2453 } else if (!strcmp (name, "TypedReference")) {
2454 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
2458 if (class->valuetype) {
2459 int t = MONO_TYPE_VALUETYPE;
2460 if (!strcmp (nspace, "System")) {
2463 if (!strcmp (name, "Boolean")) {
2464 t = MONO_TYPE_BOOLEAN;
2465 } else if (!strcmp(name, "Byte")) {
2467 class->blittable = TRUE;
2471 if (!strcmp (name, "Char")) {
2476 if (!strcmp (name, "Double")) {
2478 class->blittable = TRUE;
2482 if (!strcmp (name, "Int32")) {
2484 class->blittable = TRUE;
2485 } else if (!strcmp(name, "Int16")) {
2487 class->blittable = TRUE;
2488 } else if (!strcmp(name, "Int64")) {
2490 class->blittable = TRUE;
2491 } else if (!strcmp(name, "IntPtr")) {
2493 class->blittable = TRUE;
2497 if (!strcmp (name, "Single")) {
2499 class->blittable = TRUE;
2500 } else if (!strcmp(name, "SByte")) {
2502 class->blittable = TRUE;
2506 if (!strcmp (name, "UInt32")) {
2508 class->blittable = TRUE;
2509 } else if (!strcmp(name, "UInt16")) {
2511 class->blittable = TRUE;
2512 } else if (!strcmp(name, "UInt64")) {
2514 class->blittable = TRUE;
2515 } else if (!strcmp(name, "UIntPtr")) {
2517 class->blittable = TRUE;
2521 if (!strcmp (name, "TypedReference")) {
2522 t = MONO_TYPE_TYPEDBYREF;
2523 class->blittable = TRUE;
2527 if (!strcmp (name, "Void")) {
2535 class->this_arg.type = class->byval_arg.type = t;
2538 if (MONO_CLASS_IS_INTERFACE (class))
2539 class->interface_id = mono_get_unique_iid (class);
2544 * LOCKING: this assumes the loader lock is held
2547 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
2549 gboolean system_namespace;
2551 system_namespace = !strcmp (class->name_space, "System");
2553 /* if root of the hierarchy */
2554 if (system_namespace && !strcmp (class->name, "Object")) {
2555 class->parent = NULL;
2556 class->instance_size = sizeof (MonoObject);
2559 if (!strcmp (class->name, "<Module>")) {
2560 class->parent = NULL;
2561 class->instance_size = 0;
2565 if (!MONO_CLASS_IS_INTERFACE (class)) {
2566 class->parent = parent;
2569 g_assert_not_reached (); /* FIXME */
2571 if (parent->generic_class && !parent->name) {
2573 * If the parent is a generic instance, we may get
2574 * called before it is fully initialized, especially
2575 * before it has its name.
2580 class->marshalbyref = parent->marshalbyref;
2581 class->contextbound = parent->contextbound;
2582 class->delegate = parent->delegate;
2584 if (system_namespace) {
2585 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
2586 class->marshalbyref = 1;
2588 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
2589 class->contextbound = 1;
2591 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
2592 class->delegate = 1;
2595 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
2596 (strcmp (class->parent->name_space, "System") == 0)))
2597 class->valuetype = 1;
2598 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
2599 class->valuetype = class->enumtype = 1;
2601 /*class->enumtype = class->parent->enumtype; */
2602 mono_class_setup_supertypes (class);
2604 class->parent = NULL;
2610 * mono_class_setup_supertypes:
2613 * Build the data structure needed to make fast type checks work.
2614 * This currently sets two fields in @class:
2615 * - idepth: distance between @class and System.Object in the type
2617 * - supertypes: array of classes: each element has a class in the hierarchy
2618 * starting from @class up to System.Object
2620 * LOCKING: this assumes the loader lock is held
2623 mono_class_setup_supertypes (MonoClass *class)
2627 if (class->supertypes)
2630 if (class->parent && !class->parent->supertypes)
2631 mono_class_setup_supertypes (class->parent);
2633 class->idepth = class->parent->idepth + 1;
2637 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
2638 class->supertypes = g_new0 (MonoClass *, ms);
2640 if (class->parent) {
2641 class->supertypes [class->idepth - 1] = class;
2642 memcpy (class->supertypes, class->parent->supertypes, class->parent->idepth * sizeof (gpointer));
2644 class->supertypes [0] = class;
2649 mono_get_shared_generic_inst (MonoGenericContainer *container)
2651 MonoGenericInst *nginst;
2654 nginst = g_new0 (MonoGenericInst, 1);
2655 nginst->type_argc = container->type_argc;
2656 nginst->type_argv = g_new0 (MonoType *, nginst->type_argc);
2657 nginst->is_reference = 1;
2658 nginst->is_open = 1;
2660 for (i = 0; i < nginst->type_argc; i++) {
2661 MonoType *t = g_new0 (MonoType, 1);
2663 t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2664 t->data.generic_param = &container->type_params [i];
2666 nginst->type_argv [i] = t;
2669 return mono_metadata_lookup_generic_inst (nginst);
2673 * In preparation for implementing shared code.
2676 mono_get_shared_generic_class (MonoGenericContainer *container, gboolean is_dynamic)
2678 MonoInflatedGenericClass *igclass;
2679 MonoGenericClass *gclass;
2682 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
2683 igclass = &dgclass->generic_class;
2684 gclass = &igclass->generic_class;
2685 gclass->is_inflated = 1;
2686 gclass->is_dynamic = 1;
2688 igclass = g_new0 (MonoInflatedGenericClass, 1);
2689 gclass = &igclass->generic_class;
2690 gclass->is_inflated = 1;
2693 gclass->context = &container->context;
2694 gclass->container_class = container->klass;
2695 gclass->inst = mono_get_shared_generic_inst (container);
2698 MonoGenericClass *cached = mono_metadata_lookup_generic_class (gclass);
2706 igclass->klass = container->klass;
2712 * mono_class_create_from_typedef:
2713 * @image: image where the token is valid
2714 * @type_token: typedef token
2716 * Create the MonoClass* representing the specified type token.
2717 * @type_token must be a TypeDef token.
2720 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
2722 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
2723 MonoClass *class, *parent = NULL;
2724 guint32 cols [MONO_TYPEDEF_SIZE];
2725 guint32 cols_next [MONO_TYPEDEF_SIZE];
2726 guint tidx = mono_metadata_token_index (type_token);
2727 MonoGenericContext *context = NULL;
2728 const char *name, *nspace;
2730 MonoClass **interfaces;
2731 guint32 field_last, method_last;
2732 guint32 nesting_tokeen;
2734 mono_loader_lock ();
2736 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
2737 mono_loader_unlock ();
2741 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
2743 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
2745 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2746 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2748 class = g_malloc0 (sizeof (MonoClass));
2751 class->name_space = nspace;
2753 class->image = image;
2754 class->type_token = type_token;
2755 class->flags = cols [MONO_TYPEDEF_FLAGS];
2757 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
2760 * Check whether we're a generic type definition.
2762 class->generic_container = mono_metadata_load_generic_params (image, class->type_token, NULL);
2763 if (class->generic_container) {
2764 class->generic_container->klass = class;
2765 context = &class->generic_container->context;
2767 context->gclass = mono_get_shared_generic_class (context->container, FALSE);
2770 if (cols [MONO_TYPEDEF_EXTENDS]) {
2771 parent = mono_class_get_full (
2772 image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]), context);
2775 /* do this early so it's available for interfaces in setup_mono_type () */
2776 if ((nesting_tokeen = mono_metadata_nested_in_typedef (image, type_token)))
2777 class->nested_in = mono_class_create_from_typedef (image, nesting_tokeen);
2779 mono_class_setup_parent (class, parent);
2781 /* uses ->valuetype, which is initialized by mono_class_setup_parent above */
2782 mono_class_setup_mono_type (class);
2784 if (!class->enumtype) {
2785 mono_metadata_interfaces_from_typedef_full (image, type_token, &interfaces, &icount, context);
2787 class->interfaces = interfaces;
2788 class->interface_count = icount;
2791 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
2795 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
2799 class->cast_class = class->element_class = class;
2801 /*g_print ("Load class %s\n", name);*/
2804 * Compute the field and method lists
2806 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
2807 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
2809 if (tt->rows > tidx){
2810 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
2811 field_last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
2812 method_last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
2814 field_last = image->tables [MONO_TABLE_FIELD].rows;
2815 method_last = image->tables [MONO_TABLE_METHOD].rows;
2818 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
2819 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
2820 class->field.count = field_last - class->field.first;
2822 class->field.count = 0;
2824 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
2825 class->method.count = method_last - class->method.first;
2827 class->method.count = 0;
2829 /* reserve space to store vector pointer in arrays */
2830 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
2831 class->instance_size += 2 * sizeof (gpointer);
2832 g_assert (class->field.count == 0);
2835 if (class->enumtype) {
2836 class->enum_basetype = mono_class_find_enum_basetype (class);
2837 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
2841 * If we're a generic type definition, load the constraints.
2842 * We must do this after the class has been constructed to make certain recursive scenarios
2845 if (class->generic_container)
2846 mono_metadata_load_generic_param_constraints (
2847 image, type_token, class->generic_container);
2849 mono_loader_unlock ();
2854 /** is klass Nullable<T>? */
2856 mono_class_is_nullable (MonoClass *klass)
2858 return klass->generic_class != NULL &&
2859 klass->generic_class->container_class == mono_defaults.generic_nullable_class;
2863 /** if klass is T? return T */
2865 mono_class_get_nullable_param (MonoClass *klass)
2867 g_assert (mono_class_is_nullable (klass));
2868 return mono_class_from_mono_type (klass->generic_class->inst->type_argv [0]);
2872 * Create the `MonoClass' for an instantiation of a generic type.
2873 * We only do this if we actually need it.
2876 mono_class_create_generic (MonoInflatedGenericClass *gclass)
2878 MonoClass *klass, *gklass;
2881 mono_loader_lock ();
2882 if (gclass->is_initialized) {
2883 mono_loader_unlock ();
2888 gclass->klass = g_malloc0 (sizeof (MonoClass));
2889 klass = gclass->klass;
2891 gklass = gclass->generic_class.container_class;
2893 if (gklass->nested_in) {
2895 * FIXME: the nested type context should include everything the
2896 * nesting context should have, but it may also have additional
2897 * generic parameters...
2899 MonoType *inflated = mono_class_inflate_generic_type (
2900 &gklass->nested_in->byval_arg, gclass->generic_class.context);
2901 klass->nested_in = mono_class_from_mono_type (inflated);
2904 klass->name = gklass->name;
2905 klass->name_space = gklass->name_space;
2906 klass->image = gklass->image;
2907 klass->flags = gklass->flags;
2908 klass->type_token = gklass->type_token;
2910 klass->generic_class = &gclass->generic_class;
2912 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
2913 klass->this_arg.data.generic_class = klass->byval_arg.data.generic_class = &gclass->generic_class;
2914 klass->this_arg.byref = TRUE;
2916 klass->cast_class = klass->element_class = klass;
2918 if (mono_class_is_nullable (klass))
2919 klass->cast_class = klass->element_class = mono_class_get_nullable_param (klass);
2921 if (gclass->generic_class.is_dynamic) {
2922 klass->instance_size = gklass->instance_size;
2923 klass->class_size = gklass->class_size;
2924 klass->size_inited = 1;
2927 klass->valuetype = gklass->valuetype;
2929 mono_class_setup_supertypes (klass);
2932 klass->interface_count = gklass->interface_count;
2933 klass->interfaces = g_new0 (MonoClass *, klass->interface_count);
2934 for (i = 0; i < klass->interface_count; i++) {
2935 MonoType *it = &gklass->interfaces [i]->byval_arg;
2936 MonoType *inflated = mono_class_inflate_generic_type (
2937 it, gclass->generic_class.context);
2938 klass->interfaces [i] = mono_class_from_mono_type (inflated);
2941 i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, 1);
2944 guint32 cols [MONO_NESTED_CLASS_SIZE];
2945 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
2946 nclass = mono_class_create_from_typedef (klass->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
2947 klass->nested_classes = g_list_prepend (klass->nested_classes, nclass);
2949 i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, i + 1);
2952 if (gklass->parent) {
2953 MonoType *inflated = mono_class_inflate_generic_type (
2954 &gklass->parent->byval_arg, gclass->generic_class.context);
2956 klass->parent = mono_class_from_mono_type (inflated);
2960 mono_class_setup_parent (klass, klass->parent);
2962 if (MONO_CLASS_IS_INTERFACE (klass))
2963 setup_interface_offsets (klass, 0);
2964 gclass->is_initialized = TRUE;
2965 mono_loader_unlock ();
2969 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
2971 MonoClass *klass, **ptr;
2975 return param->pklass;
2977 klass = param->pklass = g_new0 (MonoClass, 1);
2979 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
2983 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
2984 klass->parent = param->constraints [0];
2986 } else if (param->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT)
2987 klass->parent = mono_class_from_name (mono_defaults.corlib, "System", "ValueType");
2989 klass->parent = mono_defaults.object_class;
2991 if (count - pos > 0) {
2992 klass->interface_count = count - pos;
2993 klass->interfaces = g_new0 (MonoClass *, count - pos);
2994 for (i = pos; i < count; i++)
2995 klass->interfaces [i - pos] = param->constraints [i];
2999 klass->name = param->name;
3001 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
3003 klass->name_space = "";
3006 klass->image = image;
3007 else if (is_mvar && param->method && param->method->klass)
3008 klass->image = param->method->klass->image;
3009 else if (param->owner && param->owner->klass)
3010 klass->image = param->owner->klass->image;
3012 klass->image = mono_defaults.corlib;
3014 klass->inited = TRUE;
3015 klass->cast_class = klass->element_class = klass;
3016 klass->enum_basetype = &klass->element_class->byval_arg;
3017 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
3019 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
3020 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
3021 klass->this_arg.byref = TRUE;
3023 mono_class_setup_supertypes (klass);
3029 mono_ptr_class_get (MonoType *type)
3032 MonoClass *el_class;
3033 static GHashTable *ptr_hash = NULL;
3035 mono_loader_lock ();
3038 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
3039 el_class = mono_class_from_mono_type (type);
3040 if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
3041 mono_loader_unlock ();
3044 result = g_new0 (MonoClass, 1);
3046 result->parent = NULL; /* no parent for PTR types */
3047 result->name_space = el_class->name_space;
3048 result->name = g_strdup_printf ("%s*", el_class->name);
3049 result->image = el_class->image;
3050 result->inited = TRUE;
3051 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
3052 /* Can pointers get boxed? */
3053 result->instance_size = sizeof (gpointer);
3054 result->cast_class = result->element_class = el_class;
3055 result->enum_basetype = &result->element_class->byval_arg;
3056 result->blittable = TRUE;
3058 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
3059 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
3060 result->this_arg.byref = TRUE;
3062 mono_class_setup_supertypes (result);
3064 g_hash_table_insert (ptr_hash, el_class, result);
3066 mono_loader_unlock ();
3072 mono_fnptr_class_get (MonoMethodSignature *sig)
3075 static GHashTable *ptr_hash = NULL;
3077 mono_loader_lock ();
3080 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
3082 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
3083 mono_loader_unlock ();
3086 result = g_new0 (MonoClass, 1);
3088 result->parent = NULL; /* no parent for PTR types */
3089 result->name_space = "System";
3090 result->name = "MonoFNPtrFakeClass";
3091 result->image = NULL; /* need to fix... */
3092 result->inited = TRUE;
3093 result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
3094 /* Can pointers get boxed? */
3095 result->instance_size = sizeof (gpointer);
3096 result->cast_class = result->element_class = result;
3097 result->blittable = TRUE;
3099 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
3100 result->this_arg.data.method = result->byval_arg.data.method = sig;
3101 result->this_arg.byref = TRUE;
3102 result->enum_basetype = &result->element_class->byval_arg;
3103 result->blittable = TRUE;
3105 mono_class_setup_supertypes (result);
3107 g_hash_table_insert (ptr_hash, sig, result);
3109 mono_loader_unlock ();
3115 mono_class_from_mono_type (MonoType *type)
3117 switch (type->type) {
3118 case MONO_TYPE_OBJECT:
3119 return type->data.klass? type->data.klass: mono_defaults.object_class;
3120 case MONO_TYPE_VOID:
3121 return type->data.klass? type->data.klass: mono_defaults.void_class;
3122 case MONO_TYPE_BOOLEAN:
3123 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
3124 case MONO_TYPE_CHAR:
3125 return type->data.klass? type->data.klass: mono_defaults.char_class;
3127 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
3129 return type->data.klass? type->data.klass: mono_defaults.byte_class;
3131 return type->data.klass? type->data.klass: mono_defaults.int16_class;
3133 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
3135 return type->data.klass? type->data.klass: mono_defaults.int32_class;
3137 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
3139 return type->data.klass? type->data.klass: mono_defaults.int_class;
3141 return type->data.klass? type->data.klass: mono_defaults.uint_class;
3143 return type->data.klass? type->data.klass: mono_defaults.int64_class;
3145 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
3147 return type->data.klass? type->data.klass: mono_defaults.single_class;
3149 return type->data.klass? type->data.klass: mono_defaults.double_class;
3150 case MONO_TYPE_STRING:
3151 return type->data.klass? type->data.klass: mono_defaults.string_class;
3152 case MONO_TYPE_TYPEDBYREF:
3153 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
3154 case MONO_TYPE_ARRAY:
3155 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
3157 return mono_ptr_class_get (type->data.type);
3158 case MONO_TYPE_FNPTR:
3159 return mono_fnptr_class_get (type->data.method);
3160 case MONO_TYPE_SZARRAY:
3161 return mono_array_class_get (type->data.klass, 1);
3162 case MONO_TYPE_CLASS:
3163 case MONO_TYPE_VALUETYPE:
3164 return type->data.klass;
3165 case MONO_TYPE_GENERICINST: {
3166 MonoInflatedGenericClass *gclass;
3167 gclass = mono_get_inflated_generic_class (type->data.generic_class);
3168 g_assert (gclass->klass);
3169 return gclass->klass;
3172 return mono_class_from_generic_parameter (type->data.generic_param, NULL, FALSE);
3173 case MONO_TYPE_MVAR:
3174 return mono_class_from_generic_parameter (type->data.generic_param, NULL, TRUE);
3176 g_warning ("implement me 0x%02x\n", type->type);
3177 g_assert_not_reached ();
3184 * @image: context where the image is created
3185 * @type_spec: typespec token
3188 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
3190 return mono_class_from_mono_type (mono_type_create_from_typespec (image, type_spec));
3194 * mono_bounded_array_class_get:
3195 * @element_class: element class
3196 * @rank: the dimension of the array class
3197 * @bounded: whenever the array has non-zero bounds
3199 * Returns: a class object describing the array with element type @element_type and
3203 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
3207 MonoClass *parent = NULL;
3208 GSList *list, *rootlist;
3211 gboolean corlib_type = FALSE;
3213 g_assert (rank <= 255);
3216 /* bounded only matters for one-dimensional arrays */
3219 image = eclass->image;
3221 mono_loader_lock ();
3223 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
3224 for (; list; list = list->next) {
3226 if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
3227 mono_loader_unlock ();
3233 /* for the building corlib use System.Array from it */
3234 if (image->assembly && image->assembly->dynamic && image->assembly_name && strcmp (image->assembly_name, "mscorlib") == 0) {
3235 parent = mono_class_from_name (image, "System", "Array");
3237 } else if (mono_defaults.generic_array_class) {
3238 MonoType *inflated, **args;
3240 args = g_new0 (MonoType *, 1);
3241 args [0] = &eclass->byval_arg;
3243 inflated = mono_class_bind_generic_parameters (
3244 &mono_defaults.generic_array_class->byval_arg, 1, args);
3245 parent = mono_class_from_mono_type (inflated);
3247 if (!parent->inited)
3248 mono_class_init (parent);
3250 parent = mono_defaults.array_class;
3251 if (!parent->inited)
3252 mono_class_init (parent);
3255 class = g_malloc0 (sizeof (MonoClass));
3257 class->image = image;
3258 class->name_space = eclass->name_space;
3259 nsize = strlen (eclass->name);
3260 name = g_malloc (nsize + 2 + rank);
3261 memcpy (name, eclass->name, nsize);
3264 memset (name + nsize + 1, ',', rank - 1);
3265 name [nsize + rank] = ']';
3266 name [nsize + rank + 1] = 0;
3268 class->type_token = 0;
3269 /* all arrays are marked serializable and sealed, bug #42779 */
3270 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
3271 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
3272 class->parent = parent;
3273 class->instance_size = mono_class_instance_size (class->parent);
3274 class->class_size = 0;
3275 mono_class_setup_supertypes (class);
3276 if (eclass->generic_class)
3277 mono_class_init (eclass);
3278 if (!eclass->size_inited)
3279 mono_class_setup_fields (eclass);
3280 class->has_references = MONO_TYPE_IS_REFERENCE (&eclass->byval_arg) || eclass->has_references? TRUE: FALSE;
3284 if (eclass->enumtype)
3285 class->cast_class = eclass->element_class;
3287 class->cast_class = eclass;
3289 class->element_class = eclass;
3291 if ((rank > 1) || bounded) {
3292 MonoArrayType *at = g_new0 (MonoArrayType, 1);
3293 class->byval_arg.type = MONO_TYPE_ARRAY;
3294 class->byval_arg.data.array = at;
3295 at->eklass = eclass;
3297 /* FIXME: complete.... */
3299 class->byval_arg.type = MONO_TYPE_SZARRAY;
3300 class->byval_arg.data.klass = eclass;
3302 class->this_arg = class->byval_arg;
3303 class->this_arg.byref = 1;
3308 class->generic_container = eclass->generic_container;
3310 list = g_slist_append (rootlist, class);
3311 g_hash_table_insert (image->array_cache, eclass, list);
3313 mono_loader_unlock ();
3319 * mono_array_class_get:
3320 * @element_class: element class
3321 * @rank: the dimension of the array class
3323 * Returns: a class object describing the array with element type @element_type and
3327 mono_array_class_get (MonoClass *eclass, guint32 rank)
3329 return mono_bounded_array_class_get (eclass, rank, FALSE);
3333 * mono_class_instance_size:
3336 * Returns: the size of an object instance
3339 mono_class_instance_size (MonoClass *klass)
3341 if (!klass->size_inited)
3342 mono_class_init (klass);
3344 return klass->instance_size;
3348 * mono_class_min_align:
3351 * Returns: minimm alignment requirements
3354 mono_class_min_align (MonoClass *klass)
3356 if (!klass->size_inited)
3357 mono_class_init (klass);
3359 return klass->min_align;
3363 * mono_class_value_size:
3366 * This function is used for value types, and return the
3367 * space and the alignment to store that kind of value object.
3369 * Returns: the size of a value of kind @klass
3372 mono_class_value_size (MonoClass *klass, guint32 *align)
3376 /* fixme: check disable, because we still have external revereces to
3377 * mscorlib and Dummy Objects
3379 /*g_assert (klass->valuetype);*/
3381 size = mono_class_instance_size (klass) - sizeof (MonoObject);
3384 *align = klass->min_align;
3390 * mono_class_data_size:
3393 * Returns: the size of the static class data
3396 mono_class_data_size (MonoClass *klass)
3399 mono_class_init (klass);
3401 return klass->class_size;
3405 * Auxiliary routine to mono_class_get_field
3407 * Takes a field index instead of a field token.
3409 static MonoClassField *
3410 mono_class_get_field_idx (MonoClass *class, int idx)
3412 mono_class_setup_fields_locking (class);
3415 if (class->field.count) {
3416 if ((idx >= class->field.first) && (idx < class->field.first + class->field.count)){
3417 return &class->fields [idx - class->field.first];
3420 class = class->parent;
3426 * mono_class_get_field:
3427 * @class: the class to lookup the field.
3428 * @field_token: the field token
3430 * Returns: A MonoClassField representing the type and offset of
3431 * the field, or a NULL value if the field does not belong to this
3435 mono_class_get_field (MonoClass *class, guint32 field_token)
3437 int idx = mono_metadata_token_index (field_token);
3439 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
3441 return mono_class_get_field_idx (class, idx - 1);
3445 * mono_class_get_field_from_name:
3446 * @klass: the class to lookup the field.
3447 * @name: the field name
3449 * Search the class @klass and it's parents for a field with the name @name.
3451 * Returns: the MonoClassField pointer of the named field or NULL
3454 mono_class_get_field_from_name (MonoClass *klass, const char *name)
3458 mono_class_setup_fields_locking (klass);
3460 for (i = 0; i < klass->field.count; ++i) {
3461 if (strcmp (name, klass->fields [i].name) == 0)
3462 return &klass->fields [i];
3464 klass = klass->parent;
3470 * mono_class_get_field_token:
3471 * @field: the field we need the token of
3473 * Get the token of a field. Note that the tokesn is only valid for the image
3474 * the field was loaded from. Don't use this function for fields in dynamic types.
3476 * Returns: the token representing the field in the image it was loaded from.
3479 mono_class_get_field_token (MonoClassField *field)
3481 MonoClass *klass = field->parent;
3484 mono_class_setup_fields_locking (klass);
3486 for (i = 0; i < klass->field.count; ++i) {
3487 if (&klass->fields [i] == field)
3488 return mono_metadata_make_token (MONO_TABLE_FIELD, klass->field.first + i + 1);
3490 klass = klass->parent;
3493 g_assert_not_reached ();
3498 mono_class_get_event_token (MonoEvent *event)
3500 MonoClass *klass = event->parent;
3504 for (i = 0; i < klass->event.count; ++i) {
3505 if (&klass->events [i] == event)
3506 return mono_metadata_make_token (MONO_TABLE_EVENT, klass->event.first + i + 1);
3508 klass = klass->parent;
3511 g_assert_not_reached ();
3516 mono_class_get_property_from_name (MonoClass *klass, const char *name)
3520 gpointer iter = NULL;
3521 while ((p = mono_class_get_properties (klass, &iter))) {
3522 if (! strcmp (name, p->name))
3525 klass = klass->parent;
3531 mono_class_get_property_token (MonoProperty *prop)
3533 MonoClass *klass = prop->parent;
3537 gpointer iter = NULL;
3538 while ((p = mono_class_get_properties (klass, &iter))) {
3539 if (&klass->properties [i] == prop)
3540 return mono_metadata_make_token (MONO_TABLE_PROPERTY, klass->property.first + i + 1);
3544 klass = klass->parent;
3547 g_assert_not_reached ();
3552 mono_class_name_from_token (MonoImage *image, guint32 type_token)
3554 const char *name, *nspace;
3556 return g_strdup_printf ("DynamicType 0x%08x", type_token);
3558 switch (type_token & 0xff000000){
3559 case MONO_TOKEN_TYPE_DEF: {
3560 guint32 cols [MONO_TYPEDEF_SIZE];
3561 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
3562 guint tidx = mono_metadata_token_index (type_token);
3564 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
3565 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3566 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3567 if (strlen (nspace) == 0)
3568 return g_strdup_printf ("%s", name);
3570 return g_strdup_printf ("%s.%s", nspace, name);
3573 case MONO_TOKEN_TYPE_REF: {
3574 guint32 cols [MONO_TYPEREF_SIZE];
3575 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
3577 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
3578 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
3579 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
3580 if (strlen (nspace) == 0)
3581 return g_strdup_printf ("%s", name);
3583 return g_strdup_printf ("%s.%s", nspace, name);
3586 case MONO_TOKEN_TYPE_SPEC:
3587 return g_strdup_printf ("Typespec 0x%08x", type_token);
3589 g_assert_not_reached ();
3596 mono_assembly_name_from_token (MonoImage *image, guint32 type_token)
3599 return g_strdup_printf ("DynamicAssembly %s", image->name);
3601 switch (type_token & 0xff000000){
3602 case MONO_TOKEN_TYPE_DEF:
3603 return mono_stringify_assembly_name (&image->assembly->aname);
3605 case MONO_TOKEN_TYPE_REF: {
3606 MonoAssemblyName aname;
3607 guint32 cols [MONO_TYPEREF_SIZE];
3608 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
3611 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
3613 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
3614 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
3615 case MONO_RESOLTION_SCOPE_MODULE:
3617 return g_strdup ("");
3618 case MONO_RESOLTION_SCOPE_MODULEREF:
3620 return g_strdup ("");
3621 case MONO_RESOLTION_SCOPE_TYPEREF:
3623 return g_strdup ("");
3624 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
3625 mono_assembly_get_assemblyref (image, idx - 1, &aname);
3626 return mono_stringify_assembly_name (&aname);
3628 g_assert_not_reached ();
3632 case MONO_TOKEN_TYPE_SPEC:
3634 return g_strdup ("");
3636 g_assert_not_reached ();
3644 * @image: the image where the class resides
3645 * @type_token: the token for the class
3647 * Returns: the MonoClass that represents @type_token in @image
3650 mono_class_get (MonoImage *image, guint32 type_token)
3652 MonoClass *class = NULL;
3655 return mono_lookup_dynamic_token (image, type_token);
3657 switch (type_token & 0xff000000){
3658 case MONO_TOKEN_TYPE_DEF:
3659 class = mono_class_create_from_typedef (image, type_token);
3661 case MONO_TOKEN_TYPE_REF:
3662 class = mono_class_from_typeref (image, type_token);
3664 case MONO_TOKEN_TYPE_SPEC:
3665 class = mono_class_create_from_typespec (image, type_token);
3668 g_warning ("unknown token type %x", type_token & 0xff000000);
3669 g_assert_not_reached ();
3673 char *name = mono_class_name_from_token (image, type_token);
3674 char *assembly = mono_assembly_name_from_token (image, type_token);
3675 mono_loader_set_error_type_load (name, assembly);
3682 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3684 MonoClass *class = mono_class_get (image, type_token);
3686 if (!image->dynamic && ((type_token & 0xff000000) == MONO_TOKEN_TYPE_DEF))
3689 if (class && context && (context->gclass || context->gmethod)) {
3690 MonoType *inflated = inflate_generic_type (&class->byval_arg, context);
3692 class = mono_class_from_mono_type (inflated);
3703 find_nocase (gpointer key, gpointer value, gpointer user_data)
3705 char *name = (char*)key;
3706 FindUserData *data = (FindUserData*)user_data;
3708 if (!data->value && (g_strcasecmp (name, (char*)data->key) == 0))
3709 data->value = value;
3713 * mono_class_from_name_case:
3714 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3715 * @name_space: the type namespace
3716 * @name: the type short name.
3718 * Obtains a MonoClass with a given namespace and a given name which
3719 * is located in the given MonoImage. The namespace and name
3720 * lookups are case insensitive.
3722 * You can also pass @NULL to the image, and that will lookup for
3723 * a type with the given namespace and name in all of the loaded
3724 * assemblies: notice that since there might be a name clash in this
3725 * case, passing @NULL is not encouraged if you need a precise type.
3729 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
3731 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
3732 guint32 cols [MONO_TYPEDEF_SIZE];
3737 if (image->dynamic) {
3739 FindUserData user_data;
3741 mono_loader_lock ();
3743 user_data.key = name_space;
3744 user_data.value = NULL;
3745 g_hash_table_foreach (image->name_cache, find_nocase, &user_data);
3747 if (user_data.value) {
3748 GHashTable *nspace_table = (GHashTable*)user_data.value;
3750 user_data.key = name;
3751 user_data.value = NULL;
3753 g_hash_table_foreach (nspace_table, find_nocase, &user_data);
3755 if (user_data.value)
3756 token = GPOINTER_TO_UINT (user_data.value);
3759 mono_loader_unlock ();
3762 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | token);
3768 /* add a cache if needed */
3769 for (i = 1; i <= t->rows; ++i) {
3770 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
3771 /* nested types are accessed from the nesting name */
3772 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
3773 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
3775 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3776 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3777 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
3778 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
3784 return_nested_in (MonoClass *class, char *nested) {
3786 char *s = strchr (nested, '/');
3793 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
3795 if (strcmp (found->name, nested) == 0) {
3797 return return_nested_in (found, s);
3806 * mono_class_from_name:
3807 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3808 * @name_space: the type namespace
3809 * @name: the type short name.
3811 * Obtains a MonoClass with a given namespace and a given name which
3812 * is located in the given MonoImage.
3814 * You can also pass `NULL' to the image, and that will lookup for
3815 * a type with the given namespace and name in all of the loaded
3816 * assemblies: notice that since there might be a name clash in this
3817 * case, passing NULL is not encouraged if you need a precise type.
3821 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
3823 GHashTable *nspace_table;
3824 MonoImage *loaded_image;
3830 if ((nested = strchr (name, '/'))) {
3831 int pos = nested - name;
3832 int len = strlen (name);
3835 memcpy (buf, name, len + 1);
3837 nested = buf + pos + 1;
3841 mono_loader_lock ();
3843 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
3846 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
3848 mono_loader_unlock ();
3853 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
3854 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
3855 guint32 cols [MONO_EXP_TYPE_SIZE];
3858 idx = mono_metadata_token_index (token);
3860 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
3862 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
3863 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
3864 loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
3867 class = mono_class_from_name (loaded_image, name_space, name);
3869 return return_nested_in (class, nested);
3871 } else if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_ASSEMBLYREF) {
3872 MonoAssembly **references = image->references;
3873 if (!references [idx - 1])
3874 mono_assembly_load_reference (image, idx - 1);
3875 g_assert (references == image->references);
3876 g_assert (references [idx - 1]);
3877 if (references [idx - 1] == (gpointer)-1)
3880 /* FIXME: Cycle detection */
3881 return mono_class_from_name (references [idx - 1]->image, name_space, name);
3883 g_error ("not yet implemented");
3887 token = MONO_TOKEN_TYPE_DEF | token;
3889 class = mono_class_get (image, token);
3891 return return_nested_in (class, nested);
3896 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
3897 gboolean check_interfaces)
3899 g_assert (klassc->idepth > 0);
3900 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
3901 if ((klassc->interface_id <= klass->max_interface_id) &&
3902 (klass->interface_offsets [klassc->interface_id] >= 0))
3904 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
3907 for (i = 0; i < klass->interface_count; i ++) {
3908 MonoClass *ic = klass->interfaces [i];
3913 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
3918 * MS.NET thinks interfaces are a subclass of Object, so we think it as
3921 if (klassc == mono_defaults.object_class)
3928 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
3931 mono_class_init (klass);
3933 if (!oklass->inited)
3934 mono_class_init (oklass);
3936 if (MONO_CLASS_IS_INTERFACE (klass)) {
3937 if ((oklass->byval_arg.type == MONO_TYPE_VAR) || (oklass->byval_arg.type == MONO_TYPE_MVAR))
3940 /* interface_offsets might not be set for dynamic classes */
3941 if (oklass->reflection_info && !oklass->interface_offsets)
3943 * oklass might be a generic type parameter but they have
3944 * interface_offsets set.
3946 return mono_reflection_call_is_assignable_to (oklass, klass);
3948 if ((klass->interface_id <= oklass->max_interface_id) &&
3949 (oklass->interface_offsets [klass->interface_id] != -1))
3951 } else if (klass->rank) {
3952 MonoClass *eclass, *eoclass;
3954 if (oklass->rank != klass->rank)
3957 /* vectors vs. one dimensional arrays */
3958 if (oklass->byval_arg.type != klass->byval_arg.type)
3961 eclass = klass->cast_class;
3962 eoclass = oklass->cast_class;
3965 * a is b does not imply a[] is b[] when a is a valuetype, and
3966 * b is a reference type.
3969 if (eoclass->valuetype) {
3970 if ((eclass == mono_defaults.enum_class) ||
3971 (eclass == mono_defaults.enum_class->parent) ||
3972 (eclass == mono_defaults.object_class))
3976 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
3977 } else if (mono_class_is_nullable (klass))
3978 return (mono_class_is_assignable_from (klass->cast_class, oklass));
3979 else if (klass == mono_defaults.object_class)
3982 return mono_class_has_parent (oklass, klass);
3986 * mono_class_get_cctor:
3988 * Returns the static constructor of @klass if it exists, NULL otherwise.
3991 mono_class_get_cctor (MonoClass *klass)
3993 MonoCachedClassInfo cached_info;
3995 if (!klass->has_cctor)
3998 if (mono_class_get_cached_class_info (klass, &cached_info))
3999 return mono_get_method (klass->image, cached_info.cctor_token, klass);
4001 return mono_class_get_method_from_name_flags (klass, ".cctor", -1, METHOD_ATTRIBUTE_SPECIAL_NAME);
4005 * mono_class_get_finalizer:
4007 * Returns the finalizer method of @klass if it exists, NULL otherwise.
4010 mono_class_get_finalizer (MonoClass *klass)
4012 MonoCachedClassInfo cached_info;
4015 mono_class_init (klass);
4016 if (!klass->has_finalize)
4019 if (mono_class_get_cached_class_info (klass, &cached_info))
4020 return mono_get_method (cached_info.finalize_image, cached_info.finalize_token, NULL);
4022 mono_class_setup_vtable (klass);
4023 return klass->vtable [finalize_slot];
4028 * mono_class_needs_cctor_run:
4030 * Determines whenever the class has a static constructor and whenever it
4031 * needs to be called when executing CALLER.
4034 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
4038 method = mono_class_get_cctor (klass);
4040 return (method == caller) ? FALSE : TRUE;
4046 * mono_class_array_element_size:
4049 * Returns: the number of bytes an element of type @klass
4050 * uses when stored into an array.
4053 mono_class_array_element_size (MonoClass *klass)
4055 MonoType *type = &klass->byval_arg;
4058 switch (type->type) {
4061 case MONO_TYPE_BOOLEAN:
4065 case MONO_TYPE_CHAR:
4074 case MONO_TYPE_CLASS:
4075 case MONO_TYPE_STRING:
4076 case MONO_TYPE_OBJECT:
4077 case MONO_TYPE_SZARRAY:
4078 case MONO_TYPE_ARRAY:
4080 case MONO_TYPE_MVAR:
4081 return sizeof (gpointer);
4086 case MONO_TYPE_VALUETYPE:
4087 if (type->data.klass->enumtype) {
4088 type = type->data.klass->enum_basetype;
4089 klass = klass->element_class;
4092 return mono_class_instance_size (klass) - sizeof (MonoObject);
4093 case MONO_TYPE_GENERICINST:
4094 type = &type->data.generic_class->container_class->byval_arg;
4097 g_error ("unknown type 0x%02x in mono_class_array_element_size", type->type);
4103 * mono_array_element_size:
4104 * @ac: pointer to a #MonoArrayClass
4106 * Returns: the size of single array element.
4109 mono_array_element_size (MonoClass *ac)
4111 return mono_class_array_element_size (ac->element_class);
4115 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
4116 MonoGenericContext *context)
4118 if (image->dynamic) {
4119 MonoClass *tmp_handle_class;
4120 gpointer obj = mono_lookup_dynamic_token_class (image, token, &tmp_handle_class);
4122 g_assert (tmp_handle_class);
4124 *handle_class = tmp_handle_class;
4126 if (tmp_handle_class == mono_defaults.typehandle_class)
4127 return &((MonoClass*)obj)->byval_arg;
4132 switch (token & 0xff000000) {
4133 case MONO_TOKEN_TYPE_DEF:
4134 case MONO_TOKEN_TYPE_REF:
4135 case MONO_TOKEN_TYPE_SPEC: {
4138 *handle_class = mono_defaults.typehandle_class;
4139 class = mono_class_get_full (image, token, context);
4142 mono_class_init (class);
4143 /* We return a MonoType* as handle */
4144 return &class->byval_arg;
4146 case MONO_TOKEN_FIELD_DEF: {
4148 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
4150 *handle_class = mono_defaults.fieldhandle_class;
4151 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
4154 mono_class_init (class);
4155 return mono_class_get_field (class, token);
4157 case MONO_TOKEN_METHOD_DEF: {
4159 meth = mono_get_method_full (image, token, NULL, context);
4161 *handle_class = mono_defaults.methodhandle_class;
4164 case MONO_TOKEN_MEMBER_REF: {
4165 guint32 cols [MONO_MEMBERREF_SIZE];
4167 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
4168 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
4169 mono_metadata_decode_blob_size (sig, &sig);
4170 if (*sig == 0x6) { /* it's a field */
4172 MonoClassField *field;
4173 field = mono_field_from_token (image, token, &klass, context);
4175 *handle_class = mono_defaults.fieldhandle_class;
4179 meth = mono_get_method_full (image, token, NULL, context);
4181 *handle_class = mono_defaults.methodhandle_class;
4186 g_warning ("Unknown token 0x%08x in ldtoken", token);
4193 * This function might need to call runtime functions so it can't be part
4194 * of the metadata library.
4196 static MonoLookupDynamicToken lookup_dynamic = NULL;
4199 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
4201 lookup_dynamic = func;
4205 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
4207 MonoClass *handle_class;
4209 return lookup_dynamic (image, token, &handle_class);
4213 mono_lookup_dynamic_token_class (MonoImage *image, guint32 token, MonoClass **handle_class)
4215 return lookup_dynamic (image, token, handle_class);
4218 static MonoGetCachedClassInfo get_cached_class_info = NULL;
4221 mono_install_get_cached_class_info (MonoGetCachedClassInfo func)
4223 get_cached_class_info = func;
4227 mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
4229 if (!get_cached_class_info)
4232 return get_cached_class_info (klass, res);
4236 mono_class_get_image (MonoClass *klass)
4238 return klass->image;
4242 * mono_class_get_element_class:
4243 * @klass: the MonoClass to act on
4245 * Returns: the element class of an array or an enumeration.
4248 mono_class_get_element_class (MonoClass *klass)
4250 return klass->element_class;
4254 * mono_class_is_valuetype:
4255 * @klass: the MonoClass to act on
4257 * Returns: true if the MonoClass represents a ValueType.
4260 mono_class_is_valuetype (MonoClass *klass)
4262 return klass->valuetype;
4266 * mono_class_is_enum:
4267 * @klass: the MonoClass to act on
4269 * Returns: true if the MonoClass represents an enumeration.
4272 mono_class_is_enum (MonoClass *klass)
4274 return klass->enumtype;
4278 * mono_class_enum_basetype:
4279 * @klass: the MonoClass to act on
4281 * Returns: the underlying type representation for an enumeration.
4284 mono_class_enum_basetype (MonoClass *klass)
4286 return klass->enum_basetype;
4290 * mono_class_get_parent
4291 * @klass: the MonoClass to act on
4293 * Returns: the parent class for this class.
4296 mono_class_get_parent (MonoClass *klass)
4298 return klass->parent;
4302 * mono_class_get_nesting_type;
4303 * @klass: the MonoClass to act on
4305 * Returns: the container type where this type is nested or NULL if this type is not a nested type.
4308 mono_class_get_nesting_type (MonoClass *klass)
4310 return klass->nested_in;
4314 * mono_class_get_rank:
4315 * @klass: the MonoClass to act on
4317 * Returns: the rank for the array (the number of dimensions).
4320 mono_class_get_rank (MonoClass *klass)
4326 * mono_class_get_flags:
4327 * @klass: the MonoClass to act on
4329 * The type flags from the TypeDef table from the metadata.
4330 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
4333 * Returns: the flags from the TypeDef table.
4336 mono_class_get_flags (MonoClass *klass)
4338 return klass->flags;
4342 * mono_class_get_name
4343 * @klass: the MonoClass to act on
4345 * Returns: the name of the class.
4348 mono_class_get_name (MonoClass *klass)
4354 * mono_class_get_namespace:
4355 * @klass: the MonoClass to act on
4357 * Returns: the namespace of the class.
4360 mono_class_get_namespace (MonoClass *klass)
4362 return klass->name_space;
4366 * mono_class_get_type:
4367 * @klass: the MonoClass to act on
4369 * This method returns the internal Type representation for the class.
4371 * Returns: the MonoType from the class.
4374 mono_class_get_type (MonoClass *klass)
4376 return &klass->byval_arg;
4380 * mono_class_get_byref_type:
4381 * @klass: the MonoClass to act on
4386 mono_class_get_byref_type (MonoClass *klass)
4388 return &klass->this_arg;
4392 * mono_class_num_fields:
4393 * @klass: the MonoClass to act on
4395 * Returns: the number of static and instance fields in the class.
4398 mono_class_num_fields (MonoClass *klass)
4400 return klass->field.count;
4404 * mono_class_num_methods:
4405 * @klass: the MonoClass to act on
4407 * Returns: the number of methods in the class.
4410 mono_class_num_methods (MonoClass *klass)
4412 return klass->method.count;
4416 * mono_class_num_properties
4417 * @klass: the MonoClass to act on
4419 * Returns: the number of properties in the class.
4422 mono_class_num_properties (MonoClass *klass)
4424 mono_class_setup_properties (klass);
4426 return klass->property.count;
4430 * mono_class_num_events:
4431 * @klass: the MonoClass to act on
4433 * Returns: the number of events in the class.
4436 mono_class_num_events (MonoClass *klass)
4438 mono_class_setup_events (klass);
4440 return klass->event.count;
4444 * mono_class_get_fields:
4445 * @klass: the MonoClass to act on
4447 * This routine is an iterator routine for retrieving the fields in a class.
4449 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4450 * iterate over all of the elements. When no more values are
4451 * available, the return value is NULL.
4453 * Returns: a @MonoClassField* on each iteration, or NULL when no more fields are available.
4456 mono_class_get_fields (MonoClass* klass, gpointer *iter)
4458 MonoClassField* field;
4461 mono_class_setup_fields_locking (klass);
4463 /* start from the first */
4464 if (klass->field.count) {
4465 return *iter = &klass->fields [0];
4473 if (field < &klass->fields [klass->field.count]) {
4474 return *iter = field;
4480 * mono_class_get_methods
4481 * @klass: the MonoClass to act on
4483 * This routine is an iterator routine for retrieving the fields in a class.
4485 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4486 * iterate over all of the elements. When no more values are
4487 * available, the return value is NULL.
4489 * Returns: a MonoMethod on each iteration or NULL when no more methods are available.
4492 mono_class_get_methods (MonoClass* klass, gpointer *iter)
4494 MonoMethod** method;
4498 mono_class_init (klass);
4500 mono_class_setup_methods (klass);
4501 /* start from the first */
4502 if (klass->method.count) {
4503 *iter = &klass->methods [0];
4504 return klass->methods [0];
4512 if (method < &klass->methods [klass->method.count]) {
4520 * mono_class_get_properties:
4521 * @klass: the MonoClass to act on
4523 * This routine is an iterator routine for retrieving the properties in a class.
4525 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4526 * iterate over all of the elements. When no more values are
4527 * available, the return value is NULL.
4529 * Returns: a @MonoProperty* on each invocation, or NULL when no more are available.
4532 mono_class_get_properties (MonoClass* klass, gpointer *iter)
4534 MonoProperty* property;
4538 mono_class_init (klass);
4540 mono_class_setup_properties (klass);
4541 /* start from the first */
4542 if (klass->property.count) {
4543 return *iter = &klass->properties [0];
4551 if (property < &klass->properties [klass->property.count]) {
4552 return *iter = property;
4558 * mono_class_get_events:
4559 * @klass: the MonoClass to act on
4561 * This routine is an iterator routine for retrieving the properties in a class.
4563 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4564 * iterate over all of the elements. When no more values are
4565 * available, the return value is NULL.
4567 * Returns: a @MonoEvent* on each invocation, or NULL when no more are available.
4570 mono_class_get_events (MonoClass* klass, gpointer *iter)
4576 mono_class_init (klass);
4578 mono_class_setup_events (klass);
4579 /* start from the first */
4580 if (klass->event.count) {
4581 return *iter = &klass->events [0];
4589 if (event < &klass->events [klass->event.count]) {
4590 return *iter = event;
4596 * mono_class_get_interfaces
4597 * @klass: the MonoClass to act on
4599 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
4601 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4602 * iterate over all of the elements. When no more values are
4603 * available, the return value is NULL.
4605 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
4608 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
4614 mono_class_init (klass);
4616 /* start from the first */
4617 if (klass->interface_count) {
4618 *iter = &klass->interfaces [0];
4619 return klass->interfaces [0];
4627 if (iface < &klass->interfaces [klass->interface_count]) {
4635 * mono_class_get_nested_types
4636 * @klass: the MonoClass to act on
4638 * This routine is an iterator routine for retrieving the nested types of a class.
4640 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4641 * iterate over all of the elements. When no more values are
4642 * available, the return value is NULL.
4644 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
4647 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
4653 mono_class_init (klass);
4655 /* start from the first */
4656 if (klass->nested_classes) {
4657 *iter = klass->nested_classes;
4658 return klass->nested_classes->data;
4660 /* no nested types */
4674 * mono_field_get_name:
4675 * @field: the MonoClassField to act on
4677 * Returns: the name of the field.
4680 mono_field_get_name (MonoClassField *field)
4686 * mono_field_get_type:
4687 * @field: the MonoClassField to act on
4689 * Returns: MonoType of the field.
4692 mono_field_get_type (MonoClassField *field)
4698 * mono_field_get_type:
4699 * @field: the MonoClassField to act on
4701 * Returns: MonoClass where the field was defined.
4704 mono_field_get_parent (MonoClassField *field)
4706 return field->parent;
4710 * mono_field_get_flags;
4711 * @field: the MonoClassField to act on
4713 * The metadata flags for a field are encoded using the
4714 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4716 * Returns: the flags for the field.
4719 mono_field_get_flags (MonoClassField *field)
4721 return field->type->attrs;
4725 * mono_property_get_name:
4726 * @prop: the MonoProperty to act on
4728 * Returns: the name of the property
4731 mono_property_get_name (MonoProperty *prop)
4737 * mono_property_get_set_method
4738 * @prop: the MonoProperty to act on.
4740 * Returns: the setter method of the property (A MonoMethod)
4743 mono_property_get_set_method (MonoProperty *prop)
4749 * mono_property_get_get_method
4750 * @prop: the MonoProperty to act on.
4752 * Returns: the setter method of the property (A MonoMethod)
4755 mono_property_get_get_method (MonoProperty *prop)
4761 * mono_property_get_parent:
4762 * @prop: the MonoProperty to act on.
4764 * Returns: the MonoClass where the property was defined.
4767 mono_property_get_parent (MonoProperty *prop)
4769 return prop->parent;
4773 * mono_property_get_flags:
4774 * @prop: the MonoProperty to act on.
4776 * The metadata flags for a property are encoded using the
4777 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4779 * Returns: the flags for the property.
4782 mono_property_get_flags (MonoProperty *prop)
4788 * mono_event_get_name:
4789 * @event: the MonoEvent to act on
4791 * Returns: the name of the event.
4794 mono_event_get_name (MonoEvent *event)
4800 * mono_event_get_add_method:
4801 * @event: The MonoEvent to act on.
4803 * Returns: the @add' method for the event (a MonoMethod).
4806 mono_event_get_add_method (MonoEvent *event)
4812 * mono_event_get_remove_method:
4813 * @event: The MonoEvent to act on.
4815 * Returns: the @remove method for the event (a MonoMethod).
4818 mono_event_get_remove_method (MonoEvent *event)
4820 return event->remove;
4824 * mono_event_get_raise_method:
4825 * @event: The MonoEvent to act on.
4827 * Returns: the @raise method for the event (a MonoMethod).
4830 mono_event_get_raise_method (MonoEvent *event)
4832 return event->raise;
4836 * mono_event_get_parent:
4837 * @event: the MonoEvent to act on.
4839 * Returns: the MonoClass where the event is defined.
4842 mono_event_get_parent (MonoEvent *event)
4844 return event->parent;
4848 * mono_event_get_flags
4849 * @event: the MonoEvent to act on.
4851 * The metadata flags for an event are encoded using the
4852 * EVENT_* constants. See the tabledefs.h file for details.
4854 * Returns: the flags for the event.
4857 mono_event_get_flags (MonoEvent *event)
4859 return event->attrs;
4863 * mono_class_get_method_from_name:
4864 * @klass: where to look for the method
4865 * @name_space: name of the method
4866 * @param_count: number of parameters. -1 for any number.
4868 * Obtains a MonoMethod with a given name and number of parameters.
4869 * It only works if there are no multiple signatures for any given method name.
4872 mono_class_get_method_from_name (MonoClass *klass, const char *name, int param_count)
4874 return mono_class_get_method_from_name_flags (klass, name, param_count, 0);
4878 * mono_class_get_method_from_name_flags:
4879 * @klass: where to look for the method
4880 * @name_space: name of the method
4881 * @param_count: number of parameters. -1 for any number.
4882 * @flags: flags which must be set in the method
4884 * Obtains a MonoMethod with a given name and number of parameters.
4885 * It only works if there are no multiple signatures for any given method name.
4888 mono_class_get_method_from_name_flags (MonoClass *klass, const char *name, int param_count, int flags)
4890 MonoMethod *res = NULL;
4893 mono_class_init (klass);
4895 if (klass->methods) {
4896 mono_class_setup_methods (klass);
4897 for (i = 0; i < klass->method.count; ++i) {
4898 MonoMethod *method = klass->methods [i];
4900 if (method->name[0] == name [0] &&
4901 !strcmp (name, method->name) &&
4902 (param_count == -1 || mono_method_signature (method)->param_count == param_count) &&
4903 ((method->flags & flags) == flags)) {
4910 /* Search directly in the metadata to avoid calling setup_methods () */
4911 for (i = 0; i < klass->method.count; ++i) {
4912 guint32 cols [MONO_METHOD_SIZE];
4915 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_METHOD], klass->method.first + i, cols, MONO_METHOD_SIZE);
4917 if (!strcmp (mono_metadata_string_heap (klass->image, cols [MONO_METHOD_NAME]), name)) {
4918 method = mono_get_method (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass);
4919 if ((param_count == -1) || mono_method_signature (method)->param_count == param_count) {
4931 * mono_class_set_failure:
4932 * @klass: class in which the failure was detected
4933 * @ex_type: the kind of exception/error to be thrown (later)
4934 * @ex_data: exception data (specific to each type of exception/error)
4936 * Keep a detected failure informations in the class for later processing.
4937 * Note that only the first failure is kept.
4940 mono_class_set_failure (MonoClass *klass, guint32 ex_type, void *ex_data)
4942 if (klass->exception_type)
4944 klass->exception_type = ex_type;
4945 klass->exception_data = ex_data;
4950 * mono_class_get_exception_for_failure:
4951 * @klass: class in which the failure was detected
4953 * Return a constructed MonoException than the caller can then throw
4954 * using mono_raise_exception - or NULL if no failure is present (or
4955 * doesn't result in an exception).
4958 mono_class_get_exception_for_failure (MonoClass *klass)
4960 switch (klass->exception_type) {
4961 case MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND: {
4962 MonoDomain *domain = mono_domain_get ();
4963 MonoSecurityManager* secman = mono_security_manager_get_methods ();
4964 MonoMethod *method = klass->exception_data;
4965 guint32 error = (method) ? MONO_METADATA_INHERITANCEDEMAND_METHOD : MONO_METADATA_INHERITANCEDEMAND_CLASS;
4966 MonoObject *exc = NULL;
4970 args [1] = mono_assembly_get_object (domain, mono_image_get_assembly (klass->image));
4971 args [2] = mono_type_get_object (domain, &klass->byval_arg);
4972 args [3] = (method) ? mono_method_get_object (domain, method, NULL) : NULL;
4974 mono_runtime_invoke (secman->inheritsecurityexception, NULL, args, &exc);
4975 return (MonoException*) exc;
4977 case MONO_EXCEPTION_TYPE_LOAD:
4978 return mono_exception_from_name (mono_defaults.corlib, "System", "TypeLoadException");
4979 /* TODO - handle other class related failures */