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 * Setup interface offsets for interfaces. Used by Ref.Emit.
1702 mono_class_setup_interface_offsets (MonoClass *class)
1704 mono_loader_lock ();
1706 setup_interface_offsets (class, 0);
1708 mono_loader_unlock ();
1712 mono_class_setup_vtable (MonoClass *class)
1714 MonoMethod **overrides;
1715 MonoGenericContext *context;
1722 mono_class_setup_methods (class);
1724 if (MONO_CLASS_IS_INTERFACE (class))
1727 mono_loader_lock ();
1729 if (class->vtable) {
1730 mono_loader_unlock ();
1734 if (class->generic_class) {
1735 context = class->generic_class->context;
1736 type_token = class->generic_class->container_class->type_token;
1738 context = (MonoGenericContext *) class->generic_container;
1739 type_token = class->type_token;
1742 if (class->image->dynamic)
1743 mono_reflection_get_dynamic_overrides (class, &overrides, &onum);
1745 mono_class_get_overrides_full (class->image, type_token, &overrides, &onum, context);
1747 mono_class_setup_vtable_general (class, overrides, onum);
1750 mono_loader_unlock ();
1754 * LOCKING: this is supposed to be called with the loader lock held.
1757 mono_class_setup_vtable_general (MonoClass *class, MonoMethod **overrides, int onum)
1760 MonoMethod **vtable;
1761 int i, max_vtsize = 0, max_iid, cur_slot = 0;
1762 GPtrArray *ifaces, *pifaces = NULL;
1763 GHashTable *override_map = NULL;
1764 gboolean security_enabled = mono_is_security_manager_active ();
1769 ifaces = mono_class_get_implemented_interfaces (class);
1771 for (i = 0; i < ifaces->len; i++) {
1772 MonoClass *ic = g_ptr_array_index (ifaces, i);
1773 max_vtsize += ic->method.count;
1775 g_ptr_array_free (ifaces, TRUE);
1778 if (class->parent) {
1779 mono_class_init (class->parent);
1780 mono_class_setup_vtable (class->parent);
1781 max_vtsize += class->parent->vtable_size;
1782 cur_slot = class->parent->vtable_size;
1785 max_vtsize += class->method.count;
1787 vtable = alloca (sizeof (gpointer) * max_vtsize);
1788 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
1790 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
1792 cur_slot = setup_interface_offsets (class, cur_slot);
1793 max_iid = class->max_interface_id;
1795 if (class->parent && class->parent->vtable_size)
1796 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
1798 /* override interface methods */
1799 for (i = 0; i < onum; i++) {
1800 MonoMethod *decl = overrides [i*2];
1801 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
1803 mono_class_setup_methods (decl->klass);
1804 g_assert (decl->slot != -1);
1805 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
1806 vtable [dslot] = overrides [i*2 + 1];
1807 vtable [dslot]->slot = dslot;
1809 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1811 g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
1815 for (k = class; k ; k = k->parent) {
1818 ifaces = mono_class_get_implemented_interfaces (k);
1820 nifaces = ifaces->len;
1821 if (k->generic_class) {
1822 pifaces = mono_class_get_implemented_interfaces (
1823 k->generic_class->container_class);
1824 g_assert (pifaces && (pifaces->len == nifaces));
1827 for (i = 0; i < nifaces; i++) {
1828 MonoClass *pic = NULL;
1831 ic = g_ptr_array_index (ifaces, i);
1833 pic = g_ptr_array_index (pifaces, i);
1834 g_assert (ic->interface_id <= k->max_interface_id);
1835 io = k->interface_offsets [ic->interface_id];
1838 g_assert (io <= max_vtsize);
1841 mono_class_setup_methods (ic);
1842 for (l = 0; l < ic->method.count; l++) {
1843 MonoMethod *im = ic->methods [l];
1845 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1848 for (j = 0; j < class->method.count; ++j) {
1849 MonoMethod *cm = class->methods [j];
1850 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1851 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
1852 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
1854 if (!strcmp(cm->name, im->name) &&
1855 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1857 /* CAS - SecurityAction.InheritanceDemand on interface */
1858 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1859 mono_secman_inheritancedemand_method (cm, im);
1862 g_assert (io + l <= max_vtsize);
1863 vtable [io + l] = cm;
1868 /* already implemented */
1869 if (io >= k->vtable_size)
1873 for (l = 0; l < ic->method.count; l++) {
1874 MonoMethod *im = ic->methods [l];
1877 g_assert (io + l <= max_vtsize);
1879 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1882 for (k1 = class; k1; k1 = k1->parent) {
1883 for (j = 0; j < k1->method.count; ++j) {
1884 MonoMethod *cm = k1->methods [j];
1886 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1887 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
1890 if (!strcmp(cm->name, im->name) &&
1891 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1893 /* CAS - SecurityAction.InheritanceDemand on interface */
1894 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1895 mono_secman_inheritancedemand_method (cm, im);
1898 g_assert (io + l <= max_vtsize);
1899 vtable [io + l] = cm;
1904 g_assert (io + l <= max_vtsize);
1905 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1910 for (l = 0; l < ic->method.count; l++) {
1911 MonoMethod *im = ic->methods [l];
1912 char *qname, *fqname, *cname, *the_cname;
1915 if (vtable [io + l])
1919 the_cname = mono_type_get_name_full (&pic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
1923 cname = (char*)ic->name;
1926 qname = g_strconcat (cname, ".", im->name, NULL);
1927 if (ic->name_space && ic->name_space [0])
1928 fqname = g_strconcat (ic->name_space, ".", cname, ".", im->name, NULL);
1932 for (k1 = class; k1; k1 = k1->parent) {
1933 for (j = 0; j < k1->method.count; ++j) {
1934 MonoMethod *cm = k1->methods [j];
1936 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1939 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
1940 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1942 /* CAS - SecurityAction.InheritanceDemand on interface */
1943 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1944 mono_secman_inheritancedemand_method (cm, im);
1947 g_assert (io + l <= max_vtsize);
1948 vtable [io + l] = cm;
1959 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
1960 for (l = 0; l < ic->method.count; l++) {
1962 MonoMethod *im = ic->methods [l];
1963 if (im->flags & METHOD_ATTRIBUTE_STATIC)
1965 g_assert (io + l <= max_vtsize);
1968 * If one of our parents already implements this interface
1969 * we can inherit the implementation.
1971 if (!(vtable [io + l])) {
1972 MonoClass *parent = class->parent;
1974 if ((ic->interface_id <= parent->max_interface_id) &&
1975 (parent->interface_offsets [ic->interface_id] != -1) &&
1977 vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
1980 if (!(vtable [io + l])) {
1981 for (j = 0; j < onum; ++j) {
1982 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
1983 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
1985 msig = mono_signature_get_desc (mono_method_signature (im), FALSE);
1986 printf ("no implementation for interface method %s::%s(%s) in class %s.%s\n",
1987 mono_type_get_name (&ic->byval_arg), im->name, msig, class->name_space, class->name);
1989 for (j = 0; j < class->method.count; ++j) {
1990 MonoMethod *cm = class->methods [j];
1991 msig = mono_signature_get_desc (mono_method_signature (cm), TRUE);
1993 printf ("METHOD %s(%s)\n", cm->name, msig);
1996 g_assert_not_reached ();
2001 for (l = 0; l < ic->method.count; l++) {
2002 MonoMethod *im = vtable [io + l];
2005 g_assert (io + l <= max_vtsize);
2007 /* FIXME: why do we need this ? */
2009 /* g_assert_not_reached (); */
2015 g_ptr_array_free (ifaces, TRUE);
2018 for (i = 0; i < class->method.count; ++i) {
2021 cm = class->methods [i];
2024 * Non-virtual method have no place in the vtable.
2025 * This also catches static methods (since they are not virtual).
2027 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
2031 * If the method is REUSE_SLOT, we must check in the
2032 * base class for a method to override.
2034 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
2036 for (k = class->parent; k ; k = k->parent) {
2038 for (j = 0; j < k->method.count; ++j) {
2039 MonoMethod *m1 = k->methods [j];
2040 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
2042 if (!strcmp(cm->name, m1->name) &&
2043 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (m1))) {
2045 /* CAS - SecurityAction.InheritanceDemand */
2046 if (security_enabled && (m1->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
2047 mono_secman_inheritancedemand_method (cm, m1);
2050 slot = k->methods [j]->slot;
2051 g_assert (cm->slot < max_vtsize);
2053 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
2054 g_hash_table_insert (override_map, m1, cm);
2066 cm->slot = cur_slot++;
2068 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
2069 vtable [cm->slot] = cm;
2072 /* override non interface methods */
2073 for (i = 0; i < onum; i++) {
2074 MonoMethod *decl = overrides [i*2];
2075 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
2076 g_assert (decl->slot != -1);
2077 vtable [decl->slot] = overrides [i*2 + 1];
2078 overrides [i * 2 + 1]->slot = decl->slot;
2080 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
2081 g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
2086 * If a method occupies more than one place in the vtable, and it is
2087 * overriden, then change the other occurances too.
2090 for (i = 0; i < max_vtsize; ++i)
2092 MonoMethod *cm = g_hash_table_lookup (override_map, vtable [i]);
2097 g_hash_table_destroy (override_map);
2100 if (class->generic_class) {
2101 MonoClass *gklass = class->generic_class->container_class;
2103 mono_class_init (gklass);
2105 class->vtable_size = MAX (gklass->vtable_size, cur_slot);
2107 class->vtable_size = cur_slot;
2109 class->vtable = mono_mempool_alloc0 (class->image->mempool, sizeof (gpointer) * class->vtable_size);
2110 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
2112 if (mono_print_vtable) {
2115 for (i = 0; i <= max_iid; i++)
2116 if (class->interface_offsets [i] != -1)
2119 printf ("VTable %s (vtable entries = %d, interfaces = %d)\n", mono_type_full_name (&class->byval_arg),
2120 class->vtable_size, icount);
2122 for (i = 0; i < class->vtable_size; ++i) {
2127 printf (" slot assigned: %03d, slot index: %03d %s\n", i, cm->slot,
2128 mono_method_full_name (cm, TRUE));
2134 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
2135 class->name, max_iid);
2137 for (i = 0; i < class->interface_count; i++) {
2138 ic = class->interfaces [i];
2139 printf (" slot offset: %03d, method count: %03d, iid: %03d %s\n",
2140 class->interface_offsets [ic->interface_id],
2141 ic->method.count, ic->interface_id, mono_type_full_name (&ic->byval_arg));
2144 for (k = class->parent; k ; k = k->parent) {
2145 for (i = 0; i < k->interface_count; i++) {
2146 ic = k->interfaces [i];
2147 printf (" slot offset: %03d, method count: %03d, iid: %03d %s\n",
2148 class->interface_offsets [ic->interface_id],
2149 ic->method.count, ic->interface_id, mono_type_full_name (&ic->byval_arg));
2156 static MonoMethod *default_ghc = NULL;
2157 static MonoMethod *default_finalize = NULL;
2158 static int finalize_slot = -1;
2159 static int ghc_slot = -1;
2162 initialize_object_slots (MonoClass *class)
2167 if (class == mono_defaults.object_class) {
2168 mono_class_setup_vtable (class);
2169 for (i = 0; i < class->vtable_size; ++i) {
2170 MonoMethod *cm = class->vtable [i];
2172 if (!strcmp (cm->name, "GetHashCode"))
2174 else if (!strcmp (cm->name, "Finalize"))
2178 g_assert (ghc_slot > 0);
2179 default_ghc = class->vtable [ghc_slot];
2181 g_assert (finalize_slot > 0);
2182 default_finalize = class->vtable [finalize_slot];
2187 g_list_prepend_mempool (GList* l, MonoMemPool* mp, gpointer datum)
2189 GList* n = mono_mempool_alloc (mp, sizeof (GList));
2198 * @class: the class to initialize
2200 * compute the instance_size, class_size and other infos that cannot be
2201 * computed at mono_class_get() time. Also compute a generic vtable and
2202 * the method slot numbers. We use this infos later to create a domain
2206 mono_class_init (MonoClass *class)
2209 MonoCachedClassInfo cached_info;
2210 gboolean has_cached_info;
2217 /*g_print ("Init class %s\n", class->name);*/
2219 /* We do everything inside the lock to prevent races */
2220 mono_loader_lock ();
2222 if (class->inited) {
2223 mono_loader_unlock ();
2224 /* Somebody might have gotten in before us */
2228 if (class->init_pending) {
2229 mono_loader_unlock ();
2230 /* this indicates a cyclic dependency */
2231 g_error ("pending init %s.%s\n", class->name_space, class->name);
2234 class->init_pending = 1;
2236 /* CAS - SecurityAction.InheritanceDemand */
2237 if (mono_is_security_manager_active () && class->parent && (class->parent->flags & TYPE_ATTRIBUTE_HAS_SECURITY)) {
2238 mono_secman_inheritancedemand_class (class, class->parent);
2241 if (mono_debugger_start_class_init_func)
2242 mono_debugger_start_class_init_func (class);
2244 mono_stats.initialized_class_count++;
2246 if (class->generic_class && !class->generic_class->is_dynamic) {
2247 MonoInflatedGenericClass *gclass;
2250 gclass = mono_get_inflated_generic_class (class->generic_class);
2251 gklass = gclass->generic_class.container_class;
2253 mono_stats.generic_class_count++;
2255 class->method = gklass->method;
2256 class->field = gklass->field;
2258 mono_class_init (gklass);
2259 mono_class_setup_methods (gklass);
2260 mono_class_setup_properties (gklass);
2262 if (MONO_CLASS_IS_INTERFACE (class))
2263 class->interface_id = mono_get_unique_iid (class);
2265 g_assert (class->method.count == gklass->method.count);
2266 class->methods = g_new0 (MonoMethod *, class->method.count);
2268 for (i = 0; i < class->method.count; i++) {
2269 MonoMethod *inflated = mono_class_inflate_generic_method_full (
2270 gklass->methods [i], class, gclass->generic_class.context);
2272 class->methods [i] = mono_get_inflated_method (inflated);
2275 class->property = gklass->property;
2276 class->properties = g_new0 (MonoProperty, class->property.count);
2278 for (i = 0; i < class->property.count; i++) {
2279 MonoProperty *prop = &class->properties [i];
2281 *prop = gklass->properties [i];
2284 prop->get = mono_class_inflate_generic_method_full (
2285 prop->get, class, gclass->generic_class.context);
2287 prop->set = mono_class_inflate_generic_method_full (
2288 prop->set, class, gclass->generic_class.context);
2290 prop->parent = class;
2293 g_assert (class->interface_count == gklass->interface_count);
2296 if (class->parent && !class->parent->inited)
2297 mono_class_init (class->parent);
2299 has_cached_info = mono_class_get_cached_class_info (class, &cached_info);
2301 if (!class->generic_class && (!has_cached_info || (has_cached_info && cached_info.has_nested_classes))) {
2302 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
2305 guint32 cols [MONO_NESTED_CLASS_SIZE];
2306 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
2307 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
2308 class->nested_classes = g_list_prepend_mempool (class->nested_classes, class->image->mempool, nclass);
2310 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
2315 * Computes the size used by the fields, and their locations
2317 if (has_cached_info) {
2318 class->instance_size = cached_info.instance_size;
2319 class->class_size = cached_info.class_size;
2320 class->packing_size = cached_info.packing_size;
2321 class->min_align = cached_info.min_align;
2322 class->blittable = cached_info.blittable;
2323 class->has_references = cached_info.has_references;
2324 class->has_static_refs = cached_info.has_static_refs;
2327 if (!class->size_inited)
2328 mono_class_setup_fields (class);
2330 /* initialize method pointers */
2333 MonoMethodSignature *sig;
2334 class->method.count = class->rank > 1? 2: 1;
2335 sig = mono_metadata_signature_alloc (class->image, class->rank);
2336 sig->ret = &mono_defaults.void_class->byval_arg;
2337 sig->pinvoke = TRUE;
2338 for (i = 0; i < class->rank; ++i)
2339 sig->params [i] = &mono_defaults.int32_class->byval_arg;
2341 ctor = (MonoMethod *) mono_mempool_alloc0 (class->image->mempool, sizeof (MonoMethodPInvoke));
2342 ctor->klass = class;
2343 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
2344 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
2345 ctor->signature = sig;
2346 ctor->name = ".ctor";
2348 class->methods = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoMethod*) * class->method.count);
2349 class->methods [0] = ctor;
2350 if (class->rank > 1) {
2351 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
2352 sig->ret = &mono_defaults.void_class->byval_arg;
2353 sig->pinvoke = TRUE;
2354 for (i = 0; i < class->rank * 2; ++i)
2355 sig->params [i] = &mono_defaults.int32_class->byval_arg;
2357 ctor = (MonoMethod *) mono_mempool_alloc0 (class->image->mempool, sizeof (MonoMethodPInvoke));
2358 ctor->klass = class;
2359 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
2360 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
2361 ctor->signature = sig;
2362 ctor->name = ".ctor";
2364 class->methods [1] = ctor;
2368 mono_class_setup_supertypes (class);
2371 initialize_object_slots (class);
2374 * If possible, avoid the creation of the generic vtable by requesting
2375 * cached info from the runtime.
2377 if (has_cached_info) {
2378 guint32 cur_slot = 0;
2380 class->vtable_size = cached_info.vtable_size;
2381 class->has_finalize = cached_info.has_finalize;
2382 class->ghcimpl = cached_info.ghcimpl;
2383 class->has_cctor = cached_info.has_cctor;
2385 if (class->parent) {
2386 mono_class_init (class->parent);
2387 cur_slot = class->parent->vtable_size;
2390 setup_interface_offsets (class, cur_slot);
2393 mono_class_setup_vtable (class);
2396 if (class->parent) {
2397 MonoMethod *cmethod = class->vtable [ghc_slot];
2398 if (cmethod->is_inflated)
2399 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
2400 if (cmethod == default_ghc) {
2405 /* Object::Finalize should have empty implemenatation */
2406 class->has_finalize = 0;
2407 if (class->parent) {
2408 MonoMethod *cmethod = class->vtable [finalize_slot];
2409 if (cmethod->is_inflated)
2410 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
2411 if (cmethod != default_finalize) {
2412 class->has_finalize = 1;
2416 for (i = 0; i < class->method.count; ++i) {
2417 MonoMethod *method = class->methods [i];
2418 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2419 (strcmp (".cctor", method->name) == 0)) {
2420 class->has_cctor = 1;
2426 if (MONO_CLASS_IS_INTERFACE (class)) {
2428 * class->interface_offsets is needed for the castclass/isinst code, so
2429 * we have to setup them for interfaces, too.
2431 setup_interface_offsets (class, 0);
2435 class->init_pending = 0;
2437 mono_loader_unlock ();
2439 if (mono_debugger_class_init_func)
2440 mono_debugger_class_init_func (class);
2444 * LOCKING: this assumes the loader lock is held
2447 mono_class_setup_mono_type (MonoClass *class)
2449 const char *name = class->name;
2450 const char *nspace = class->name_space;
2452 class->this_arg.byref = 1;
2453 class->this_arg.data.klass = class;
2454 class->this_arg.type = MONO_TYPE_CLASS;
2455 class->byval_arg.data.klass = class;
2456 class->byval_arg.type = MONO_TYPE_CLASS;
2458 if (!strcmp (nspace, "System")) {
2459 if (!strcmp (name, "ValueType")) {
2461 * do not set the valuetype bit for System.ValueType.
2462 * class->valuetype = 1;
2464 class->blittable = TRUE;
2465 } else if (!strcmp (name, "Enum")) {
2467 * do not set the valuetype bit for System.Enum.
2468 * class->valuetype = 1;
2470 class->valuetype = 0;
2471 class->enumtype = 0;
2472 } else if (!strcmp (name, "Object")) {
2473 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
2474 } else if (!strcmp (name, "String")) {
2475 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
2476 } else if (!strcmp (name, "TypedReference")) {
2477 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
2481 if (class->valuetype) {
2482 int t = MONO_TYPE_VALUETYPE;
2483 if (!strcmp (nspace, "System")) {
2486 if (!strcmp (name, "Boolean")) {
2487 t = MONO_TYPE_BOOLEAN;
2488 } else if (!strcmp(name, "Byte")) {
2490 class->blittable = TRUE;
2494 if (!strcmp (name, "Char")) {
2499 if (!strcmp (name, "Double")) {
2501 class->blittable = TRUE;
2505 if (!strcmp (name, "Int32")) {
2507 class->blittable = TRUE;
2508 } else if (!strcmp(name, "Int16")) {
2510 class->blittable = TRUE;
2511 } else if (!strcmp(name, "Int64")) {
2513 class->blittable = TRUE;
2514 } else if (!strcmp(name, "IntPtr")) {
2516 class->blittable = TRUE;
2520 if (!strcmp (name, "Single")) {
2522 class->blittable = TRUE;
2523 } else if (!strcmp(name, "SByte")) {
2525 class->blittable = TRUE;
2529 if (!strcmp (name, "UInt32")) {
2531 class->blittable = TRUE;
2532 } else if (!strcmp(name, "UInt16")) {
2534 class->blittable = TRUE;
2535 } else if (!strcmp(name, "UInt64")) {
2537 class->blittable = TRUE;
2538 } else if (!strcmp(name, "UIntPtr")) {
2540 class->blittable = TRUE;
2544 if (!strcmp (name, "TypedReference")) {
2545 t = MONO_TYPE_TYPEDBYREF;
2546 class->blittable = TRUE;
2550 if (!strcmp (name, "Void")) {
2558 class->this_arg.type = class->byval_arg.type = t;
2561 if (MONO_CLASS_IS_INTERFACE (class))
2562 class->interface_id = mono_get_unique_iid (class);
2567 * LOCKING: this assumes the loader lock is held
2570 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
2572 gboolean system_namespace;
2574 system_namespace = !strcmp (class->name_space, "System");
2576 /* if root of the hierarchy */
2577 if (system_namespace && !strcmp (class->name, "Object")) {
2578 class->parent = NULL;
2579 class->instance_size = sizeof (MonoObject);
2582 if (!strcmp (class->name, "<Module>")) {
2583 class->parent = NULL;
2584 class->instance_size = 0;
2588 if (!MONO_CLASS_IS_INTERFACE (class)) {
2589 class->parent = parent;
2592 g_assert_not_reached (); /* FIXME */
2594 if (parent->generic_class && !parent->name) {
2596 * If the parent is a generic instance, we may get
2597 * called before it is fully initialized, especially
2598 * before it has its name.
2603 class->marshalbyref = parent->marshalbyref;
2604 class->contextbound = parent->contextbound;
2605 class->delegate = parent->delegate;
2607 if (system_namespace) {
2608 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
2609 class->marshalbyref = 1;
2611 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
2612 class->contextbound = 1;
2614 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
2615 class->delegate = 1;
2618 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
2619 (strcmp (class->parent->name_space, "System") == 0)))
2620 class->valuetype = 1;
2621 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
2622 class->valuetype = class->enumtype = 1;
2624 /*class->enumtype = class->parent->enumtype; */
2625 mono_class_setup_supertypes (class);
2627 class->parent = NULL;
2633 * mono_class_setup_supertypes:
2636 * Build the data structure needed to make fast type checks work.
2637 * This currently sets two fields in @class:
2638 * - idepth: distance between @class and System.Object in the type
2640 * - supertypes: array of classes: each element has a class in the hierarchy
2641 * starting from @class up to System.Object
2643 * LOCKING: this assumes the loader lock is held
2646 mono_class_setup_supertypes (MonoClass *class)
2650 if (class->supertypes)
2653 if (class->parent && !class->parent->supertypes)
2654 mono_class_setup_supertypes (class->parent);
2656 class->idepth = class->parent->idepth + 1;
2660 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
2661 class->supertypes = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoClass *) * ms);
2663 if (class->parent) {
2664 class->supertypes [class->idepth - 1] = class;
2665 memcpy (class->supertypes, class->parent->supertypes, class->parent->idepth * sizeof (gpointer));
2667 class->supertypes [0] = class;
2672 mono_get_shared_generic_inst (MonoGenericContainer *container)
2674 MonoGenericInst *nginst;
2677 nginst = g_new0 (MonoGenericInst, 1);
2678 nginst->type_argc = container->type_argc;
2679 nginst->type_argv = g_new0 (MonoType *, nginst->type_argc);
2680 nginst->is_reference = 1;
2681 nginst->is_open = 1;
2683 for (i = 0; i < nginst->type_argc; i++) {
2684 MonoType *t = g_new0 (MonoType, 1);
2686 t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2687 t->data.generic_param = &container->type_params [i];
2689 nginst->type_argv [i] = t;
2692 return mono_metadata_lookup_generic_inst (nginst);
2696 * In preparation for implementing shared code.
2699 mono_get_shared_generic_class (MonoGenericContainer *container, gboolean is_dynamic)
2701 MonoInflatedGenericClass *igclass;
2702 MonoGenericClass *gclass;
2705 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
2706 igclass = &dgclass->generic_class;
2707 gclass = &igclass->generic_class;
2708 gclass->is_inflated = 1;
2709 gclass->is_dynamic = 1;
2711 igclass = g_new0 (MonoInflatedGenericClass, 1);
2712 gclass = &igclass->generic_class;
2713 gclass->is_inflated = 1;
2716 gclass->context = &container->context;
2717 gclass->container_class = container->klass;
2718 gclass->inst = mono_get_shared_generic_inst (container);
2721 MonoGenericClass *cached = mono_metadata_lookup_generic_class (gclass);
2729 igclass->klass = container->klass;
2735 * mono_class_create_from_typedef:
2736 * @image: image where the token is valid
2737 * @type_token: typedef token
2739 * Create the MonoClass* representing the specified type token.
2740 * @type_token must be a TypeDef token.
2743 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
2745 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
2746 MonoClass *class, *parent = NULL;
2747 guint32 cols [MONO_TYPEDEF_SIZE];
2748 guint32 cols_next [MONO_TYPEDEF_SIZE];
2749 guint tidx = mono_metadata_token_index (type_token);
2750 MonoGenericContext *context = NULL;
2751 const char *name, *nspace;
2753 MonoClass **interfaces;
2754 guint32 field_last, method_last;
2755 guint32 nesting_tokeen;
2757 mono_loader_lock ();
2759 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
2760 mono_loader_unlock ();
2764 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
2766 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
2768 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2769 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2771 class = mono_mempool_alloc0 (image->mempool, sizeof (MonoClass));
2774 class->name_space = nspace;
2776 class->image = image;
2777 class->type_token = type_token;
2778 class->flags = cols [MONO_TYPEDEF_FLAGS];
2780 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
2783 * Check whether we're a generic type definition.
2785 class->generic_container = mono_metadata_load_generic_params (image, class->type_token, NULL);
2786 if (class->generic_container) {
2787 class->generic_container->klass = class;
2788 context = &class->generic_container->context;
2790 context->gclass = mono_get_shared_generic_class (context->container, FALSE);
2793 if (cols [MONO_TYPEDEF_EXTENDS]) {
2794 parent = mono_class_get_full (
2795 image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]), context);
2798 /* do this early so it's available for interfaces in setup_mono_type () */
2799 if ((nesting_tokeen = mono_metadata_nested_in_typedef (image, type_token)))
2800 class->nested_in = mono_class_create_from_typedef (image, nesting_tokeen);
2802 mono_class_setup_parent (class, parent);
2804 /* uses ->valuetype, which is initialized by mono_class_setup_parent above */
2805 mono_class_setup_mono_type (class);
2807 if (!class->enumtype) {
2808 mono_metadata_interfaces_from_typedef_full (image, type_token, &interfaces, &icount, context);
2810 class->interfaces = interfaces;
2811 class->interface_count = icount;
2814 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
2818 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
2822 class->cast_class = class->element_class = class;
2824 /*g_print ("Load class %s\n", name);*/
2827 * Compute the field and method lists
2829 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
2830 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
2832 if (tt->rows > tidx){
2833 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
2834 field_last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
2835 method_last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
2837 field_last = image->tables [MONO_TABLE_FIELD].rows;
2838 method_last = image->tables [MONO_TABLE_METHOD].rows;
2841 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
2842 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
2843 class->field.count = field_last - class->field.first;
2845 class->field.count = 0;
2847 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
2848 class->method.count = method_last - class->method.first;
2850 class->method.count = 0;
2852 /* reserve space to store vector pointer in arrays */
2853 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
2854 class->instance_size += 2 * sizeof (gpointer);
2855 g_assert (class->field.count == 0);
2858 if (class->enumtype) {
2859 class->enum_basetype = mono_class_find_enum_basetype (class);
2860 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
2864 * If we're a generic type definition, load the constraints.
2865 * We must do this after the class has been constructed to make certain recursive scenarios
2868 if (class->generic_container)
2869 mono_metadata_load_generic_param_constraints (
2870 image, type_token, class->generic_container);
2872 mono_loader_unlock ();
2877 /** is klass Nullable<T>? */
2879 mono_class_is_nullable (MonoClass *klass)
2881 return klass->generic_class != NULL &&
2882 klass->generic_class->container_class == mono_defaults.generic_nullable_class;
2886 /** if klass is T? return T */
2888 mono_class_get_nullable_param (MonoClass *klass)
2890 g_assert (mono_class_is_nullable (klass));
2891 return mono_class_from_mono_type (klass->generic_class->inst->type_argv [0]);
2895 * Create the `MonoClass' for an instantiation of a generic type.
2896 * We only do this if we actually need it.
2899 mono_class_create_generic (MonoInflatedGenericClass *gclass)
2901 MonoClass *klass, *gklass;
2904 mono_loader_lock ();
2905 if (gclass->is_initialized) {
2906 mono_loader_unlock ();
2911 gclass->klass = g_malloc0 (sizeof (MonoClass));
2912 klass = gclass->klass;
2914 gklass = gclass->generic_class.container_class;
2916 if (gklass->nested_in) {
2918 * FIXME: the nested type context should include everything the
2919 * nesting context should have, but it may also have additional
2920 * generic parameters...
2922 MonoType *inflated = mono_class_inflate_generic_type (
2923 &gklass->nested_in->byval_arg, gclass->generic_class.context);
2924 klass->nested_in = mono_class_from_mono_type (inflated);
2927 klass->name = gklass->name;
2928 klass->name_space = gklass->name_space;
2929 klass->image = gklass->image;
2930 klass->flags = gklass->flags;
2931 klass->type_token = gklass->type_token;
2933 klass->generic_class = &gclass->generic_class;
2935 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
2936 klass->this_arg.data.generic_class = klass->byval_arg.data.generic_class = &gclass->generic_class;
2937 klass->this_arg.byref = TRUE;
2939 klass->cast_class = klass->element_class = klass;
2941 if (mono_class_is_nullable (klass))
2942 klass->cast_class = klass->element_class = mono_class_get_nullable_param (klass);
2944 if (gclass->generic_class.is_dynamic) {
2945 klass->instance_size = gklass->instance_size;
2946 klass->class_size = gklass->class_size;
2947 klass->size_inited = 1;
2950 klass->valuetype = gklass->valuetype;
2952 mono_class_setup_supertypes (klass);
2955 klass->interface_count = gklass->interface_count;
2956 klass->interfaces = g_new0 (MonoClass *, klass->interface_count);
2957 for (i = 0; i < klass->interface_count; i++) {
2958 MonoType *it = &gklass->interfaces [i]->byval_arg;
2959 MonoType *inflated = mono_class_inflate_generic_type (
2960 it, gclass->generic_class.context);
2961 klass->interfaces [i] = mono_class_from_mono_type (inflated);
2964 i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, 1);
2967 guint32 cols [MONO_NESTED_CLASS_SIZE];
2968 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
2969 nclass = mono_class_create_from_typedef (klass->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
2970 klass->nested_classes = g_list_prepend (klass->nested_classes, nclass);
2972 i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, i + 1);
2975 if (gklass->parent) {
2976 MonoType *inflated = mono_class_inflate_generic_type (
2977 &gklass->parent->byval_arg, gclass->generic_class.context);
2979 klass->parent = mono_class_from_mono_type (inflated);
2983 mono_class_setup_parent (klass, klass->parent);
2985 if (MONO_CLASS_IS_INTERFACE (klass))
2986 setup_interface_offsets (klass, 0);
2987 gclass->is_initialized = TRUE;
2988 mono_loader_unlock ();
2992 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
2994 MonoClass *klass, **ptr;
2998 return param->pklass;
3000 klass = param->pklass = g_new0 (MonoClass, 1);
3002 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
3006 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
3007 klass->parent = param->constraints [0];
3009 } else if (param->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT)
3010 klass->parent = mono_class_from_name (mono_defaults.corlib, "System", "ValueType");
3012 klass->parent = mono_defaults.object_class;
3014 if (count - pos > 0) {
3015 klass->interface_count = count - pos;
3016 klass->interfaces = g_new0 (MonoClass *, count - pos);
3017 for (i = pos; i < count; i++)
3018 klass->interfaces [i - pos] = param->constraints [i];
3022 klass->name = param->name;
3024 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
3026 klass->name_space = "";
3029 klass->image = image;
3030 else if (is_mvar && param->method && param->method->klass)
3031 klass->image = param->method->klass->image;
3032 else if (param->owner && param->owner->klass)
3033 klass->image = param->owner->klass->image;
3035 klass->image = mono_defaults.corlib;
3037 klass->inited = TRUE;
3038 klass->cast_class = klass->element_class = klass;
3039 klass->enum_basetype = &klass->element_class->byval_arg;
3040 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
3042 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
3043 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
3044 klass->this_arg.byref = TRUE;
3046 mono_class_setup_supertypes (klass);
3052 mono_ptr_class_get (MonoType *type)
3055 MonoClass *el_class;
3056 static GHashTable *ptr_hash = NULL;
3058 mono_loader_lock ();
3061 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
3062 el_class = mono_class_from_mono_type (type);
3063 if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
3064 mono_loader_unlock ();
3067 result = g_new0 (MonoClass, 1);
3069 result->parent = NULL; /* no parent for PTR types */
3070 result->name_space = el_class->name_space;
3071 result->name = g_strdup_printf ("%s*", el_class->name);
3072 result->image = el_class->image;
3073 result->inited = TRUE;
3074 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
3075 /* Can pointers get boxed? */
3076 result->instance_size = sizeof (gpointer);
3077 result->cast_class = result->element_class = el_class;
3078 result->enum_basetype = &result->element_class->byval_arg;
3079 result->blittable = TRUE;
3081 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
3082 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
3083 result->this_arg.byref = TRUE;
3085 mono_class_setup_supertypes (result);
3087 g_hash_table_insert (ptr_hash, el_class, result);
3089 mono_loader_unlock ();
3095 mono_fnptr_class_get (MonoMethodSignature *sig)
3098 static GHashTable *ptr_hash = NULL;
3100 mono_loader_lock ();
3103 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
3105 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
3106 mono_loader_unlock ();
3109 result = g_new0 (MonoClass, 1);
3111 result->parent = NULL; /* no parent for PTR types */
3112 result->name_space = "System";
3113 result->name = "MonoFNPtrFakeClass";
3114 result->image = NULL; /* need to fix... */
3115 result->inited = TRUE;
3116 result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
3117 /* Can pointers get boxed? */
3118 result->instance_size = sizeof (gpointer);
3119 result->cast_class = result->element_class = result;
3120 result->blittable = TRUE;
3122 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
3123 result->this_arg.data.method = result->byval_arg.data.method = sig;
3124 result->this_arg.byref = TRUE;
3125 result->enum_basetype = &result->element_class->byval_arg;
3126 result->blittable = TRUE;
3128 mono_class_setup_supertypes (result);
3130 g_hash_table_insert (ptr_hash, sig, result);
3132 mono_loader_unlock ();
3138 mono_class_from_mono_type (MonoType *type)
3140 switch (type->type) {
3141 case MONO_TYPE_OBJECT:
3142 return type->data.klass? type->data.klass: mono_defaults.object_class;
3143 case MONO_TYPE_VOID:
3144 return type->data.klass? type->data.klass: mono_defaults.void_class;
3145 case MONO_TYPE_BOOLEAN:
3146 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
3147 case MONO_TYPE_CHAR:
3148 return type->data.klass? type->data.klass: mono_defaults.char_class;
3150 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
3152 return type->data.klass? type->data.klass: mono_defaults.byte_class;
3154 return type->data.klass? type->data.klass: mono_defaults.int16_class;
3156 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
3158 return type->data.klass? type->data.klass: mono_defaults.int32_class;
3160 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
3162 return type->data.klass? type->data.klass: mono_defaults.int_class;
3164 return type->data.klass? type->data.klass: mono_defaults.uint_class;
3166 return type->data.klass? type->data.klass: mono_defaults.int64_class;
3168 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
3170 return type->data.klass? type->data.klass: mono_defaults.single_class;
3172 return type->data.klass? type->data.klass: mono_defaults.double_class;
3173 case MONO_TYPE_STRING:
3174 return type->data.klass? type->data.klass: mono_defaults.string_class;
3175 case MONO_TYPE_TYPEDBYREF:
3176 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
3177 case MONO_TYPE_ARRAY:
3178 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
3180 return mono_ptr_class_get (type->data.type);
3181 case MONO_TYPE_FNPTR:
3182 return mono_fnptr_class_get (type->data.method);
3183 case MONO_TYPE_SZARRAY:
3184 return mono_array_class_get (type->data.klass, 1);
3185 case MONO_TYPE_CLASS:
3186 case MONO_TYPE_VALUETYPE:
3187 return type->data.klass;
3188 case MONO_TYPE_GENERICINST: {
3189 MonoInflatedGenericClass *gclass;
3190 gclass = mono_get_inflated_generic_class (type->data.generic_class);
3191 g_assert (gclass->klass);
3192 return gclass->klass;
3195 return mono_class_from_generic_parameter (type->data.generic_param, NULL, FALSE);
3196 case MONO_TYPE_MVAR:
3197 return mono_class_from_generic_parameter (type->data.generic_param, NULL, TRUE);
3199 g_warning ("implement me 0x%02x\n", type->type);
3200 g_assert_not_reached ();
3207 * @image: context where the image is created
3208 * @type_spec: typespec token
3211 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
3213 return mono_class_from_mono_type (mono_type_create_from_typespec (image, type_spec));
3217 * mono_bounded_array_class_get:
3218 * @element_class: element class
3219 * @rank: the dimension of the array class
3220 * @bounded: whenever the array has non-zero bounds
3222 * Returns: a class object describing the array with element type @element_type and
3226 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
3230 MonoClass *parent = NULL;
3231 GSList *list, *rootlist;
3234 gboolean corlib_type = FALSE;
3236 g_assert (rank <= 255);
3239 /* bounded only matters for one-dimensional arrays */
3242 image = eclass->image;
3244 mono_loader_lock ();
3246 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
3247 for (; list; list = list->next) {
3249 if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
3250 mono_loader_unlock ();
3256 /* for the building corlib use System.Array from it */
3257 if (image->assembly && image->assembly->dynamic && image->assembly_name && strcmp (image->assembly_name, "mscorlib") == 0) {
3258 parent = mono_class_from_name (image, "System", "Array");
3260 } else if (mono_defaults.generic_array_class) {
3261 MonoType *inflated, **args;
3263 args = g_new0 (MonoType *, 1);
3264 args [0] = &eclass->byval_arg;
3266 inflated = mono_class_bind_generic_parameters (
3267 &mono_defaults.generic_array_class->byval_arg, 1, args);
3268 parent = mono_class_from_mono_type (inflated);
3270 if (!parent->inited)
3271 mono_class_init (parent);
3273 parent = mono_defaults.array_class;
3274 if (!parent->inited)
3275 mono_class_init (parent);
3278 class = g_malloc0 (sizeof (MonoClass));
3280 class->image = image;
3281 class->name_space = eclass->name_space;
3282 nsize = strlen (eclass->name);
3283 name = g_malloc (nsize + 2 + rank);
3284 memcpy (name, eclass->name, nsize);
3287 memset (name + nsize + 1, ',', rank - 1);
3288 name [nsize + rank] = ']';
3289 name [nsize + rank + 1] = 0;
3291 class->type_token = 0;
3292 /* all arrays are marked serializable and sealed, bug #42779 */
3293 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
3294 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
3295 class->parent = parent;
3296 class->instance_size = mono_class_instance_size (class->parent);
3297 class->class_size = 0;
3298 mono_class_setup_supertypes (class);
3299 if (eclass->generic_class)
3300 mono_class_init (eclass);
3301 if (!eclass->size_inited)
3302 mono_class_setup_fields (eclass);
3303 class->has_references = MONO_TYPE_IS_REFERENCE (&eclass->byval_arg) || eclass->has_references? TRUE: FALSE;
3307 if (eclass->enumtype)
3308 class->cast_class = eclass->element_class;
3310 class->cast_class = eclass;
3312 class->element_class = eclass;
3314 if ((rank > 1) || bounded) {
3315 MonoArrayType *at = g_new0 (MonoArrayType, 1);
3316 class->byval_arg.type = MONO_TYPE_ARRAY;
3317 class->byval_arg.data.array = at;
3318 at->eklass = eclass;
3320 /* FIXME: complete.... */
3322 class->byval_arg.type = MONO_TYPE_SZARRAY;
3323 class->byval_arg.data.klass = eclass;
3325 class->this_arg = class->byval_arg;
3326 class->this_arg.byref = 1;
3331 class->generic_container = eclass->generic_container;
3333 list = g_slist_append (rootlist, class);
3334 g_hash_table_insert (image->array_cache, eclass, list);
3336 mono_loader_unlock ();
3342 * mono_array_class_get:
3343 * @element_class: element class
3344 * @rank: the dimension of the array class
3346 * Returns: a class object describing the array with element type @element_type and
3350 mono_array_class_get (MonoClass *eclass, guint32 rank)
3352 return mono_bounded_array_class_get (eclass, rank, FALSE);
3356 * mono_class_instance_size:
3359 * Returns: the size of an object instance
3362 mono_class_instance_size (MonoClass *klass)
3364 if (!klass->size_inited)
3365 mono_class_init (klass);
3367 return klass->instance_size;
3371 * mono_class_min_align:
3374 * Returns: minimm alignment requirements
3377 mono_class_min_align (MonoClass *klass)
3379 if (!klass->size_inited)
3380 mono_class_init (klass);
3382 return klass->min_align;
3386 * mono_class_value_size:
3389 * This function is used for value types, and return the
3390 * space and the alignment to store that kind of value object.
3392 * Returns: the size of a value of kind @klass
3395 mono_class_value_size (MonoClass *klass, guint32 *align)
3399 /* fixme: check disable, because we still have external revereces to
3400 * mscorlib and Dummy Objects
3402 /*g_assert (klass->valuetype);*/
3404 size = mono_class_instance_size (klass) - sizeof (MonoObject);
3407 *align = klass->min_align;
3413 * mono_class_data_size:
3416 * Returns: the size of the static class data
3419 mono_class_data_size (MonoClass *klass)
3422 mono_class_init (klass);
3424 return klass->class_size;
3428 * Auxiliary routine to mono_class_get_field
3430 * Takes a field index instead of a field token.
3432 static MonoClassField *
3433 mono_class_get_field_idx (MonoClass *class, int idx)
3435 mono_class_setup_fields_locking (class);
3438 if (class->field.count) {
3439 if ((idx >= class->field.first) && (idx < class->field.first + class->field.count)){
3440 return &class->fields [idx - class->field.first];
3443 class = class->parent;
3449 * mono_class_get_field:
3450 * @class: the class to lookup the field.
3451 * @field_token: the field token
3453 * Returns: A MonoClassField representing the type and offset of
3454 * the field, or a NULL value if the field does not belong to this
3458 mono_class_get_field (MonoClass *class, guint32 field_token)
3460 int idx = mono_metadata_token_index (field_token);
3462 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
3464 return mono_class_get_field_idx (class, idx - 1);
3468 * mono_class_get_field_from_name:
3469 * @klass: the class to lookup the field.
3470 * @name: the field name
3472 * Search the class @klass and it's parents for a field with the name @name.
3474 * Returns: the MonoClassField pointer of the named field or NULL
3477 mono_class_get_field_from_name (MonoClass *klass, const char *name)
3481 mono_class_setup_fields_locking (klass);
3483 for (i = 0; i < klass->field.count; ++i) {
3484 if (strcmp (name, klass->fields [i].name) == 0)
3485 return &klass->fields [i];
3487 klass = klass->parent;
3493 * mono_class_get_field_token:
3494 * @field: the field we need the token of
3496 * Get the token of a field. Note that the tokesn is only valid for the image
3497 * the field was loaded from. Don't use this function for fields in dynamic types.
3499 * Returns: the token representing the field in the image it was loaded from.
3502 mono_class_get_field_token (MonoClassField *field)
3504 MonoClass *klass = field->parent;
3507 mono_class_setup_fields_locking (klass);
3509 for (i = 0; i < klass->field.count; ++i) {
3510 if (&klass->fields [i] == field)
3511 return mono_metadata_make_token (MONO_TABLE_FIELD, klass->field.first + i + 1);
3513 klass = klass->parent;
3516 g_assert_not_reached ();
3521 mono_class_get_event_token (MonoEvent *event)
3523 MonoClass *klass = event->parent;
3527 for (i = 0; i < klass->event.count; ++i) {
3528 if (&klass->events [i] == event)
3529 return mono_metadata_make_token (MONO_TABLE_EVENT, klass->event.first + i + 1);
3531 klass = klass->parent;
3534 g_assert_not_reached ();
3539 mono_class_get_property_from_name (MonoClass *klass, const char *name)
3543 gpointer iter = NULL;
3544 while ((p = mono_class_get_properties (klass, &iter))) {
3545 if (! strcmp (name, p->name))
3548 klass = klass->parent;
3554 mono_class_get_property_token (MonoProperty *prop)
3556 MonoClass *klass = prop->parent;
3560 gpointer iter = NULL;
3561 while ((p = mono_class_get_properties (klass, &iter))) {
3562 if (&klass->properties [i] == prop)
3563 return mono_metadata_make_token (MONO_TABLE_PROPERTY, klass->property.first + i + 1);
3567 klass = klass->parent;
3570 g_assert_not_reached ();
3575 mono_class_name_from_token (MonoImage *image, guint32 type_token)
3577 const char *name, *nspace;
3579 return g_strdup_printf ("DynamicType 0x%08x", type_token);
3581 switch (type_token & 0xff000000){
3582 case MONO_TOKEN_TYPE_DEF: {
3583 guint32 cols [MONO_TYPEDEF_SIZE];
3584 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
3585 guint tidx = mono_metadata_token_index (type_token);
3587 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
3588 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3589 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3590 if (strlen (nspace) == 0)
3591 return g_strdup_printf ("%s", name);
3593 return g_strdup_printf ("%s.%s", nspace, name);
3596 case MONO_TOKEN_TYPE_REF: {
3597 guint32 cols [MONO_TYPEREF_SIZE];
3598 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
3600 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
3601 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
3602 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
3603 if (strlen (nspace) == 0)
3604 return g_strdup_printf ("%s", name);
3606 return g_strdup_printf ("%s.%s", nspace, name);
3609 case MONO_TOKEN_TYPE_SPEC:
3610 return g_strdup_printf ("Typespec 0x%08x", type_token);
3612 g_assert_not_reached ();
3619 mono_assembly_name_from_token (MonoImage *image, guint32 type_token)
3622 return g_strdup_printf ("DynamicAssembly %s", image->name);
3624 switch (type_token & 0xff000000){
3625 case MONO_TOKEN_TYPE_DEF:
3626 return mono_stringify_assembly_name (&image->assembly->aname);
3628 case MONO_TOKEN_TYPE_REF: {
3629 MonoAssemblyName aname;
3630 guint32 cols [MONO_TYPEREF_SIZE];
3631 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
3634 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
3636 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
3637 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
3638 case MONO_RESOLTION_SCOPE_MODULE:
3640 return g_strdup ("");
3641 case MONO_RESOLTION_SCOPE_MODULEREF:
3643 return g_strdup ("");
3644 case MONO_RESOLTION_SCOPE_TYPEREF:
3646 return g_strdup ("");
3647 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
3648 mono_assembly_get_assemblyref (image, idx - 1, &aname);
3649 return mono_stringify_assembly_name (&aname);
3651 g_assert_not_reached ();
3655 case MONO_TOKEN_TYPE_SPEC:
3657 return g_strdup ("");
3659 g_assert_not_reached ();
3667 * @image: the image where the class resides
3668 * @type_token: the token for the class
3670 * Returns: the MonoClass that represents @type_token in @image
3673 mono_class_get (MonoImage *image, guint32 type_token)
3675 MonoClass *class = NULL;
3678 return mono_lookup_dynamic_token (image, type_token);
3680 switch (type_token & 0xff000000){
3681 case MONO_TOKEN_TYPE_DEF:
3682 class = mono_class_create_from_typedef (image, type_token);
3684 case MONO_TOKEN_TYPE_REF:
3685 class = mono_class_from_typeref (image, type_token);
3687 case MONO_TOKEN_TYPE_SPEC:
3688 class = mono_class_create_from_typespec (image, type_token);
3691 g_warning ("unknown token type %x", type_token & 0xff000000);
3692 g_assert_not_reached ();
3696 char *name = mono_class_name_from_token (image, type_token);
3697 char *assembly = mono_assembly_name_from_token (image, type_token);
3698 mono_loader_set_error_type_load (name, assembly);
3705 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3707 MonoClass *class = mono_class_get (image, type_token);
3709 if (!image->dynamic && ((type_token & 0xff000000) == MONO_TOKEN_TYPE_DEF))
3712 if (class && context && (context->gclass || context->gmethod)) {
3713 MonoType *inflated = inflate_generic_type (&class->byval_arg, context);
3715 class = mono_class_from_mono_type (inflated);
3726 find_nocase (gpointer key, gpointer value, gpointer user_data)
3728 char *name = (char*)key;
3729 FindUserData *data = (FindUserData*)user_data;
3731 if (!data->value && (g_strcasecmp (name, (char*)data->key) == 0))
3732 data->value = value;
3736 * mono_class_from_name_case:
3737 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3738 * @name_space: the type namespace
3739 * @name: the type short name.
3741 * Obtains a MonoClass with a given namespace and a given name which
3742 * is located in the given MonoImage. The namespace and name
3743 * lookups are case insensitive.
3745 * You can also pass @NULL to the image, and that will lookup for
3746 * a type with the given namespace and name in all of the loaded
3747 * assemblies: notice that since there might be a name clash in this
3748 * case, passing @NULL is not encouraged if you need a precise type.
3752 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
3754 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
3755 guint32 cols [MONO_TYPEDEF_SIZE];
3760 if (image->dynamic) {
3762 FindUserData user_data;
3764 mono_loader_lock ();
3766 user_data.key = name_space;
3767 user_data.value = NULL;
3768 g_hash_table_foreach (image->name_cache, find_nocase, &user_data);
3770 if (user_data.value) {
3771 GHashTable *nspace_table = (GHashTable*)user_data.value;
3773 user_data.key = name;
3774 user_data.value = NULL;
3776 g_hash_table_foreach (nspace_table, find_nocase, &user_data);
3778 if (user_data.value)
3779 token = GPOINTER_TO_UINT (user_data.value);
3782 mono_loader_unlock ();
3785 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | token);
3791 /* add a cache if needed */
3792 for (i = 1; i <= t->rows; ++i) {
3793 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
3794 /* nested types are accessed from the nesting name */
3795 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
3796 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
3798 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3799 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3800 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
3801 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
3807 return_nested_in (MonoClass *class, char *nested) {
3809 char *s = strchr (nested, '/');
3816 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
3818 if (strcmp (found->name, nested) == 0) {
3820 return return_nested_in (found, s);
3829 * mono_class_from_name:
3830 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3831 * @name_space: the type namespace
3832 * @name: the type short name.
3834 * Obtains a MonoClass with a given namespace and a given name which
3835 * is located in the given MonoImage.
3837 * You can also pass `NULL' to the image, and that will lookup for
3838 * a type with the given namespace and name in all of the loaded
3839 * assemblies: notice that since there might be a name clash in this
3840 * case, passing NULL is not encouraged if you need a precise type.
3844 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
3846 GHashTable *nspace_table;
3847 MonoImage *loaded_image;
3853 if ((nested = strchr (name, '/'))) {
3854 int pos = nested - name;
3855 int len = strlen (name);
3858 memcpy (buf, name, len + 1);
3860 nested = buf + pos + 1;
3864 mono_loader_lock ();
3866 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
3869 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
3871 mono_loader_unlock ();
3876 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
3877 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
3878 guint32 cols [MONO_EXP_TYPE_SIZE];
3881 idx = mono_metadata_token_index (token);
3883 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
3885 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
3886 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
3887 loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
3890 class = mono_class_from_name (loaded_image, name_space, name);
3892 return return_nested_in (class, nested);
3894 } else if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_ASSEMBLYREF) {
3895 MonoAssembly **references = image->references;
3896 if (!references [idx - 1])
3897 mono_assembly_load_reference (image, idx - 1);
3898 g_assert (references == image->references);
3899 g_assert (references [idx - 1]);
3900 if (references [idx - 1] == (gpointer)-1)
3903 /* FIXME: Cycle detection */
3904 return mono_class_from_name (references [idx - 1]->image, name_space, name);
3906 g_error ("not yet implemented");
3910 token = MONO_TOKEN_TYPE_DEF | token;
3912 class = mono_class_get (image, token);
3914 return return_nested_in (class, nested);
3919 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
3920 gboolean check_interfaces)
3922 g_assert (klassc->idepth > 0);
3923 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
3924 if ((klassc->interface_id <= klass->max_interface_id) &&
3925 (klass->interface_offsets [klassc->interface_id] >= 0))
3927 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
3930 for (i = 0; i < klass->interface_count; i ++) {
3931 MonoClass *ic = klass->interfaces [i];
3936 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
3941 * MS.NET thinks interfaces are a subclass of Object, so we think it as
3944 if (klassc == mono_defaults.object_class)
3951 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
3954 mono_class_init (klass);
3956 if (!oklass->inited)
3957 mono_class_init (oklass);
3959 if (MONO_CLASS_IS_INTERFACE (klass)) {
3960 if ((oklass->byval_arg.type == MONO_TYPE_VAR) || (oklass->byval_arg.type == MONO_TYPE_MVAR))
3963 /* interface_offsets might not be set for dynamic classes */
3964 if (oklass->reflection_info && !oklass->interface_offsets)
3966 * oklass might be a generic type parameter but they have
3967 * interface_offsets set.
3969 return mono_reflection_call_is_assignable_to (oklass, klass);
3971 if ((klass->interface_id <= oklass->max_interface_id) &&
3972 (oklass->interface_offsets [klass->interface_id] != -1))
3974 } else if (klass->rank) {
3975 MonoClass *eclass, *eoclass;
3977 if (oklass->rank != klass->rank)
3980 /* vectors vs. one dimensional arrays */
3981 if (oklass->byval_arg.type != klass->byval_arg.type)
3984 eclass = klass->cast_class;
3985 eoclass = oklass->cast_class;
3988 * a is b does not imply a[] is b[] when a is a valuetype, and
3989 * b is a reference type.
3992 if (eoclass->valuetype) {
3993 if ((eclass == mono_defaults.enum_class) ||
3994 (eclass == mono_defaults.enum_class->parent) ||
3995 (eclass == mono_defaults.object_class))
3999 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
4000 } else if (mono_class_is_nullable (klass))
4001 return (mono_class_is_assignable_from (klass->cast_class, oklass));
4002 else if (klass == mono_defaults.object_class)
4005 return mono_class_has_parent (oklass, klass);
4009 * mono_class_get_cctor:
4011 * Returns the static constructor of @klass if it exists, NULL otherwise.
4014 mono_class_get_cctor (MonoClass *klass)
4016 MonoCachedClassInfo cached_info;
4018 if (!klass->has_cctor)
4021 if (mono_class_get_cached_class_info (klass, &cached_info))
4022 return mono_get_method (klass->image, cached_info.cctor_token, klass);
4024 return mono_class_get_method_from_name_flags (klass, ".cctor", -1, METHOD_ATTRIBUTE_SPECIAL_NAME);
4028 * mono_class_get_finalizer:
4030 * Returns the finalizer method of @klass if it exists, NULL otherwise.
4033 mono_class_get_finalizer (MonoClass *klass)
4035 MonoCachedClassInfo cached_info;
4038 mono_class_init (klass);
4039 if (!klass->has_finalize)
4042 if (mono_class_get_cached_class_info (klass, &cached_info))
4043 return mono_get_method (cached_info.finalize_image, cached_info.finalize_token, NULL);
4045 mono_class_setup_vtable (klass);
4046 return klass->vtable [finalize_slot];
4051 * mono_class_needs_cctor_run:
4053 * Determines whenever the class has a static constructor and whenever it
4054 * needs to be called when executing CALLER.
4057 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
4061 method = mono_class_get_cctor (klass);
4063 return (method == caller) ? FALSE : TRUE;
4069 * mono_class_array_element_size:
4072 * Returns: the number of bytes an element of type @klass
4073 * uses when stored into an array.
4076 mono_class_array_element_size (MonoClass *klass)
4078 MonoType *type = &klass->byval_arg;
4081 switch (type->type) {
4084 case MONO_TYPE_BOOLEAN:
4088 case MONO_TYPE_CHAR:
4097 case MONO_TYPE_CLASS:
4098 case MONO_TYPE_STRING:
4099 case MONO_TYPE_OBJECT:
4100 case MONO_TYPE_SZARRAY:
4101 case MONO_TYPE_ARRAY:
4103 case MONO_TYPE_MVAR:
4104 return sizeof (gpointer);
4109 case MONO_TYPE_VALUETYPE:
4110 if (type->data.klass->enumtype) {
4111 type = type->data.klass->enum_basetype;
4112 klass = klass->element_class;
4115 return mono_class_instance_size (klass) - sizeof (MonoObject);
4116 case MONO_TYPE_GENERICINST:
4117 type = &type->data.generic_class->container_class->byval_arg;
4120 g_error ("unknown type 0x%02x in mono_class_array_element_size", type->type);
4126 * mono_array_element_size:
4127 * @ac: pointer to a #MonoArrayClass
4129 * Returns: the size of single array element.
4132 mono_array_element_size (MonoClass *ac)
4134 return mono_class_array_element_size (ac->element_class);
4138 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
4139 MonoGenericContext *context)
4141 if (image->dynamic) {
4142 MonoClass *tmp_handle_class;
4143 gpointer obj = mono_lookup_dynamic_token_class (image, token, &tmp_handle_class);
4145 g_assert (tmp_handle_class);
4147 *handle_class = tmp_handle_class;
4149 if (tmp_handle_class == mono_defaults.typehandle_class)
4150 return &((MonoClass*)obj)->byval_arg;
4155 switch (token & 0xff000000) {
4156 case MONO_TOKEN_TYPE_DEF:
4157 case MONO_TOKEN_TYPE_REF:
4158 case MONO_TOKEN_TYPE_SPEC: {
4161 *handle_class = mono_defaults.typehandle_class;
4162 class = mono_class_get_full (image, token, context);
4165 mono_class_init (class);
4166 /* We return a MonoType* as handle */
4167 return &class->byval_arg;
4169 case MONO_TOKEN_FIELD_DEF: {
4171 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
4173 *handle_class = mono_defaults.fieldhandle_class;
4174 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
4177 mono_class_init (class);
4178 return mono_class_get_field (class, token);
4180 case MONO_TOKEN_METHOD_DEF: {
4182 meth = mono_get_method_full (image, token, NULL, context);
4184 *handle_class = mono_defaults.methodhandle_class;
4187 case MONO_TOKEN_MEMBER_REF: {
4188 guint32 cols [MONO_MEMBERREF_SIZE];
4190 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
4191 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
4192 mono_metadata_decode_blob_size (sig, &sig);
4193 if (*sig == 0x6) { /* it's a field */
4195 MonoClassField *field;
4196 field = mono_field_from_token (image, token, &klass, context);
4198 *handle_class = mono_defaults.fieldhandle_class;
4202 meth = mono_get_method_full (image, token, NULL, context);
4204 *handle_class = mono_defaults.methodhandle_class;
4209 g_warning ("Unknown token 0x%08x in ldtoken", token);
4216 * This function might need to call runtime functions so it can't be part
4217 * of the metadata library.
4219 static MonoLookupDynamicToken lookup_dynamic = NULL;
4222 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
4224 lookup_dynamic = func;
4228 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
4230 MonoClass *handle_class;
4232 return lookup_dynamic (image, token, &handle_class);
4236 mono_lookup_dynamic_token_class (MonoImage *image, guint32 token, MonoClass **handle_class)
4238 return lookup_dynamic (image, token, handle_class);
4241 static MonoGetCachedClassInfo get_cached_class_info = NULL;
4244 mono_install_get_cached_class_info (MonoGetCachedClassInfo func)
4246 get_cached_class_info = func;
4250 mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
4252 if (!get_cached_class_info)
4255 return get_cached_class_info (klass, res);
4259 mono_class_get_image (MonoClass *klass)
4261 return klass->image;
4265 * mono_class_get_element_class:
4266 * @klass: the MonoClass to act on
4268 * Returns: the element class of an array or an enumeration.
4271 mono_class_get_element_class (MonoClass *klass)
4273 return klass->element_class;
4277 * mono_class_is_valuetype:
4278 * @klass: the MonoClass to act on
4280 * Returns: true if the MonoClass represents a ValueType.
4283 mono_class_is_valuetype (MonoClass *klass)
4285 return klass->valuetype;
4289 * mono_class_is_enum:
4290 * @klass: the MonoClass to act on
4292 * Returns: true if the MonoClass represents an enumeration.
4295 mono_class_is_enum (MonoClass *klass)
4297 return klass->enumtype;
4301 * mono_class_enum_basetype:
4302 * @klass: the MonoClass to act on
4304 * Returns: the underlying type representation for an enumeration.
4307 mono_class_enum_basetype (MonoClass *klass)
4309 return klass->enum_basetype;
4313 * mono_class_get_parent
4314 * @klass: the MonoClass to act on
4316 * Returns: the parent class for this class.
4319 mono_class_get_parent (MonoClass *klass)
4321 return klass->parent;
4325 * mono_class_get_nesting_type;
4326 * @klass: the MonoClass to act on
4328 * Returns: the container type where this type is nested or NULL if this type is not a nested type.
4331 mono_class_get_nesting_type (MonoClass *klass)
4333 return klass->nested_in;
4337 * mono_class_get_rank:
4338 * @klass: the MonoClass to act on
4340 * Returns: the rank for the array (the number of dimensions).
4343 mono_class_get_rank (MonoClass *klass)
4349 * mono_class_get_flags:
4350 * @klass: the MonoClass to act on
4352 * The type flags from the TypeDef table from the metadata.
4353 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
4356 * Returns: the flags from the TypeDef table.
4359 mono_class_get_flags (MonoClass *klass)
4361 return klass->flags;
4365 * mono_class_get_name
4366 * @klass: the MonoClass to act on
4368 * Returns: the name of the class.
4371 mono_class_get_name (MonoClass *klass)
4377 * mono_class_get_namespace:
4378 * @klass: the MonoClass to act on
4380 * Returns: the namespace of the class.
4383 mono_class_get_namespace (MonoClass *klass)
4385 return klass->name_space;
4389 * mono_class_get_type:
4390 * @klass: the MonoClass to act on
4392 * This method returns the internal Type representation for the class.
4394 * Returns: the MonoType from the class.
4397 mono_class_get_type (MonoClass *klass)
4399 return &klass->byval_arg;
4403 * mono_class_get_byref_type:
4404 * @klass: the MonoClass to act on
4409 mono_class_get_byref_type (MonoClass *klass)
4411 return &klass->this_arg;
4415 * mono_class_num_fields:
4416 * @klass: the MonoClass to act on
4418 * Returns: the number of static and instance fields in the class.
4421 mono_class_num_fields (MonoClass *klass)
4423 return klass->field.count;
4427 * mono_class_num_methods:
4428 * @klass: the MonoClass to act on
4430 * Returns: the number of methods in the class.
4433 mono_class_num_methods (MonoClass *klass)
4435 return klass->method.count;
4439 * mono_class_num_properties
4440 * @klass: the MonoClass to act on
4442 * Returns: the number of properties in the class.
4445 mono_class_num_properties (MonoClass *klass)
4447 mono_class_setup_properties (klass);
4449 return klass->property.count;
4453 * mono_class_num_events:
4454 * @klass: the MonoClass to act on
4456 * Returns: the number of events in the class.
4459 mono_class_num_events (MonoClass *klass)
4461 mono_class_setup_events (klass);
4463 return klass->event.count;
4467 * mono_class_get_fields:
4468 * @klass: the MonoClass to act on
4470 * This routine is an iterator routine for retrieving the fields in a class.
4472 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4473 * iterate over all of the elements. When no more values are
4474 * available, the return value is NULL.
4476 * Returns: a @MonoClassField* on each iteration, or NULL when no more fields are available.
4479 mono_class_get_fields (MonoClass* klass, gpointer *iter)
4481 MonoClassField* field;
4484 mono_class_setup_fields_locking (klass);
4486 /* start from the first */
4487 if (klass->field.count) {
4488 return *iter = &klass->fields [0];
4496 if (field < &klass->fields [klass->field.count]) {
4497 return *iter = field;
4503 * mono_class_get_methods
4504 * @klass: the MonoClass to act on
4506 * This routine is an iterator routine for retrieving the fields in a class.
4508 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4509 * iterate over all of the elements. When no more values are
4510 * available, the return value is NULL.
4512 * Returns: a MonoMethod on each iteration or NULL when no more methods are available.
4515 mono_class_get_methods (MonoClass* klass, gpointer *iter)
4517 MonoMethod** method;
4521 mono_class_init (klass);
4523 mono_class_setup_methods (klass);
4524 /* start from the first */
4525 if (klass->method.count) {
4526 *iter = &klass->methods [0];
4527 return klass->methods [0];
4535 if (method < &klass->methods [klass->method.count]) {
4543 * mono_class_get_properties:
4544 * @klass: the MonoClass to act on
4546 * This routine is an iterator routine for retrieving the properties in a class.
4548 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4549 * iterate over all of the elements. When no more values are
4550 * available, the return value is NULL.
4552 * Returns: a @MonoProperty* on each invocation, or NULL when no more are available.
4555 mono_class_get_properties (MonoClass* klass, gpointer *iter)
4557 MonoProperty* property;
4561 mono_class_init (klass);
4563 mono_class_setup_properties (klass);
4564 /* start from the first */
4565 if (klass->property.count) {
4566 return *iter = &klass->properties [0];
4574 if (property < &klass->properties [klass->property.count]) {
4575 return *iter = property;
4581 * mono_class_get_events:
4582 * @klass: the MonoClass to act on
4584 * This routine is an iterator routine for retrieving the properties in a class.
4586 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4587 * iterate over all of the elements. When no more values are
4588 * available, the return value is NULL.
4590 * Returns: a @MonoEvent* on each invocation, or NULL when no more are available.
4593 mono_class_get_events (MonoClass* klass, gpointer *iter)
4599 mono_class_init (klass);
4601 mono_class_setup_events (klass);
4602 /* start from the first */
4603 if (klass->event.count) {
4604 return *iter = &klass->events [0];
4612 if (event < &klass->events [klass->event.count]) {
4613 return *iter = event;
4619 * mono_class_get_interfaces
4620 * @klass: the MonoClass to act on
4622 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
4624 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4625 * iterate over all of the elements. When no more values are
4626 * available, the return value is NULL.
4628 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
4631 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
4637 mono_class_init (klass);
4639 /* start from the first */
4640 if (klass->interface_count) {
4641 *iter = &klass->interfaces [0];
4642 return klass->interfaces [0];
4650 if (iface < &klass->interfaces [klass->interface_count]) {
4658 * mono_class_get_nested_types
4659 * @klass: the MonoClass to act on
4661 * This routine is an iterator routine for retrieving the nested types of a class.
4663 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4664 * iterate over all of the elements. When no more values are
4665 * available, the return value is NULL.
4667 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
4670 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
4676 mono_class_init (klass);
4678 /* start from the first */
4679 if (klass->nested_classes) {
4680 *iter = klass->nested_classes;
4681 return klass->nested_classes->data;
4683 /* no nested types */
4697 * mono_field_get_name:
4698 * @field: the MonoClassField to act on
4700 * Returns: the name of the field.
4703 mono_field_get_name (MonoClassField *field)
4709 * mono_field_get_type:
4710 * @field: the MonoClassField to act on
4712 * Returns: MonoType of the field.
4715 mono_field_get_type (MonoClassField *field)
4721 * mono_field_get_type:
4722 * @field: the MonoClassField to act on
4724 * Returns: MonoClass where the field was defined.
4727 mono_field_get_parent (MonoClassField *field)
4729 return field->parent;
4733 * mono_field_get_flags;
4734 * @field: the MonoClassField to act on
4736 * The metadata flags for a field are encoded using the
4737 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4739 * Returns: the flags for the field.
4742 mono_field_get_flags (MonoClassField *field)
4744 return field->type->attrs;
4748 * mono_property_get_name:
4749 * @prop: the MonoProperty to act on
4751 * Returns: the name of the property
4754 mono_property_get_name (MonoProperty *prop)
4760 * mono_property_get_set_method
4761 * @prop: the MonoProperty to act on.
4763 * Returns: the setter method of the property (A MonoMethod)
4766 mono_property_get_set_method (MonoProperty *prop)
4772 * mono_property_get_get_method
4773 * @prop: the MonoProperty to act on.
4775 * Returns: the setter method of the property (A MonoMethod)
4778 mono_property_get_get_method (MonoProperty *prop)
4784 * mono_property_get_parent:
4785 * @prop: the MonoProperty to act on.
4787 * Returns: the MonoClass where the property was defined.
4790 mono_property_get_parent (MonoProperty *prop)
4792 return prop->parent;
4796 * mono_property_get_flags:
4797 * @prop: the MonoProperty to act on.
4799 * The metadata flags for a property are encoded using the
4800 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4802 * Returns: the flags for the property.
4805 mono_property_get_flags (MonoProperty *prop)
4811 * mono_event_get_name:
4812 * @event: the MonoEvent to act on
4814 * Returns: the name of the event.
4817 mono_event_get_name (MonoEvent *event)
4823 * mono_event_get_add_method:
4824 * @event: The MonoEvent to act on.
4826 * Returns: the @add' method for the event (a MonoMethod).
4829 mono_event_get_add_method (MonoEvent *event)
4835 * mono_event_get_remove_method:
4836 * @event: The MonoEvent to act on.
4838 * Returns: the @remove method for the event (a MonoMethod).
4841 mono_event_get_remove_method (MonoEvent *event)
4843 return event->remove;
4847 * mono_event_get_raise_method:
4848 * @event: The MonoEvent to act on.
4850 * Returns: the @raise method for the event (a MonoMethod).
4853 mono_event_get_raise_method (MonoEvent *event)
4855 return event->raise;
4859 * mono_event_get_parent:
4860 * @event: the MonoEvent to act on.
4862 * Returns: the MonoClass where the event is defined.
4865 mono_event_get_parent (MonoEvent *event)
4867 return event->parent;
4871 * mono_event_get_flags
4872 * @event: the MonoEvent to act on.
4874 * The metadata flags for an event are encoded using the
4875 * EVENT_* constants. See the tabledefs.h file for details.
4877 * Returns: the flags for the event.
4880 mono_event_get_flags (MonoEvent *event)
4882 return event->attrs;
4886 * mono_class_get_method_from_name:
4887 * @klass: where to look for the method
4888 * @name_space: name of the method
4889 * @param_count: number of parameters. -1 for any number.
4891 * Obtains a MonoMethod with a given name and number of parameters.
4892 * It only works if there are no multiple signatures for any given method name.
4895 mono_class_get_method_from_name (MonoClass *klass, const char *name, int param_count)
4897 return mono_class_get_method_from_name_flags (klass, name, param_count, 0);
4901 * mono_class_get_method_from_name_flags:
4902 * @klass: where to look for the method
4903 * @name_space: name of the method
4904 * @param_count: number of parameters. -1 for any number.
4905 * @flags: flags which must be set in the method
4907 * Obtains a MonoMethod with a given name and number of parameters.
4908 * It only works if there are no multiple signatures for any given method name.
4911 mono_class_get_method_from_name_flags (MonoClass *klass, const char *name, int param_count, int flags)
4913 MonoMethod *res = NULL;
4916 mono_class_init (klass);
4918 if (klass->methods) {
4919 mono_class_setup_methods (klass);
4920 for (i = 0; i < klass->method.count; ++i) {
4921 MonoMethod *method = klass->methods [i];
4923 if (method->name[0] == name [0] &&
4924 !strcmp (name, method->name) &&
4925 (param_count == -1 || mono_method_signature (method)->param_count == param_count) &&
4926 ((method->flags & flags) == flags)) {
4933 /* Search directly in the metadata to avoid calling setup_methods () */
4934 for (i = 0; i < klass->method.count; ++i) {
4935 guint32 cols [MONO_METHOD_SIZE];
4938 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_METHOD], klass->method.first + i, cols, MONO_METHOD_SIZE);
4940 if (!strcmp (mono_metadata_string_heap (klass->image, cols [MONO_METHOD_NAME]), name)) {
4941 method = mono_get_method (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass);
4942 if ((param_count == -1) || mono_method_signature (method)->param_count == param_count) {
4954 * mono_class_set_failure:
4955 * @klass: class in which the failure was detected
4956 * @ex_type: the kind of exception/error to be thrown (later)
4957 * @ex_data: exception data (specific to each type of exception/error)
4959 * Keep a detected failure informations in the class for later processing.
4960 * Note that only the first failure is kept.
4963 mono_class_set_failure (MonoClass *klass, guint32 ex_type, void *ex_data)
4965 if (klass->exception_type)
4967 klass->exception_type = ex_type;
4968 klass->exception_data = ex_data;
4973 * mono_classes_init:
4975 * Initialize the resources used by this module.
4978 mono_classes_init (void)
4983 * mono_classes_cleanup:
4985 * Free the resources used by this module.
4988 mono_classes_cleanup (void)
4990 IOffsetInfo *cached_info, *next;
4992 if (global_interface_bitset)
4993 mono_bitset_free (global_interface_bitset);
4995 for (cached_info = cached_offset_info; cached_info;) {
4996 next = cached_info->next;
4998 g_free (cached_info);
5004 * mono_class_get_exception_for_failure:
5005 * @klass: class in which the failure was detected
5007 * Return a constructed MonoException than the caller can then throw
5008 * using mono_raise_exception - or NULL if no failure is present (or
5009 * doesn't result in an exception).
5012 mono_class_get_exception_for_failure (MonoClass *klass)
5014 switch (klass->exception_type) {
5015 case MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND: {
5016 MonoDomain *domain = mono_domain_get ();
5017 MonoSecurityManager* secman = mono_security_manager_get_methods ();
5018 MonoMethod *method = klass->exception_data;
5019 guint32 error = (method) ? MONO_METADATA_INHERITANCEDEMAND_METHOD : MONO_METADATA_INHERITANCEDEMAND_CLASS;
5020 MonoObject *exc = NULL;
5024 args [1] = mono_assembly_get_object (domain, mono_image_get_assembly (klass->image));
5025 args [2] = mono_type_get_object (domain, &klass->byval_arg);
5026 args [3] = (method) ? mono_method_get_object (domain, method, NULL) : NULL;
5028 mono_runtime_invoke (secman->inheritsecurityexception, NULL, args, &exc);
5029 return (MonoException*) exc;
5031 case MONO_EXCEPTION_TYPE_LOAD:
5032 return mono_exception_from_name (mono_defaults.corlib, "System", "TypeLoadException");
5033 /* TODO - handle other class related failures */