2 * class.c: Class management for the Mono runtime
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
9 * Possible Optimizations:
10 * in mono_class_create, do not allocate the class right away,
11 * but wait until you know the size of the FieldMap, so that
12 * the class embeds directly the FieldMap after the vtable.
22 #include <mono/metadata/image.h>
23 #include <mono/metadata/assembly.h>
24 #include <mono/metadata/cil-coff.h>
25 #include <mono/metadata/metadata.h>
26 #include <mono/metadata/metadata-internals.h>
27 #include <mono/metadata/tabledefs.h>
28 #include <mono/metadata/tokentype.h>
29 #include <mono/metadata/class-internals.h>
30 #include <mono/metadata/object.h>
31 #include <mono/metadata/appdomain.h>
32 #include <mono/metadata/mono-endian.h>
33 #include <mono/metadata/debug-helpers.h>
34 #include <mono/metadata/reflection.h>
35 #include <mono/os/gc_wrapper.h>
39 gboolean mono_print_vtable = FALSE;
41 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
43 void (*mono_debugger_start_class_init_func) (MonoClass *klass) = NULL;
44 void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
47 mono_class_from_typeref (MonoImage *image, guint32 type_token)
49 guint32 cols [MONO_TYPEREF_SIZE];
50 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
52 const char *name, *nspace;
54 MonoAssembly **references;
56 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
58 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
59 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
61 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
62 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
63 case MONO_RESOLTION_SCOPE_MODULE:
65 g_error ("null ResolutionScope not yet handled");
66 /* a typedef in disguise */
67 return mono_class_from_name (image, nspace, name);
68 case MONO_RESOLTION_SCOPE_MODULEREF:
69 return mono_class_from_name (image->modules [idx - 1], nspace, name);
70 case MONO_RESOLTION_SCOPE_TYPEREF: {
71 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
73 mono_class_init (enclosing);
74 for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
76 if (strcmp (res->name, name) == 0)
79 g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
82 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
86 references = image->references;
87 if (!references || !references [idx-1]) {
89 * detected a reference to mscorlib, we simply return a reference to a dummy
90 * until we have a better solution.
92 * once a better solution is in place, the System.MonoDummy
93 * class should be removed from CVS.
95 fprintf(stderr, "Sending dummy where %s.%s expected\n", mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]), mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]));
97 res = mono_class_from_name (image, "System", "MonoDummy");
98 /* prevent method loading */
100 /* some storage if the type is used - very ugly hack */
101 res->instance_size = 2*sizeof (gpointer);
105 /* load referenced assembly */
106 image = references [idx-1]->image;
108 return mono_class_from_name (image, nspace, name);
112 dup_type (MonoType* t, const MonoType *original)
114 MonoType *r = g_new0 (MonoType, 1);
116 r->attrs = original->attrs;
117 mono_stats.generics_metadata_size += sizeof (MonoType);
122 mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed)
126 switch (type->type) {
127 case MONO_TYPE_ARRAY: {
128 int i, rank = type->data.array->rank;
130 mono_type_get_name_recurse (&type->data.array->eklass->byval_arg, str, FALSE);
131 g_string_append_c (str, '[');
132 for (i = 1; i < rank; i++)
133 g_string_append_c (str, ',');
134 g_string_append_c (str, ']');
137 case MONO_TYPE_SZARRAY:
138 mono_type_get_name_recurse (&type->data.klass->byval_arg, str, FALSE);
139 g_string_append (str, "[]");
142 mono_type_get_name_recurse (type->data.type, str, FALSE);
143 g_string_append_c (str, '*');
146 klass = mono_class_from_mono_type (type);
147 if (klass->nested_in) {
148 mono_type_get_name_recurse (&klass->nested_in->byval_arg, str, TRUE);
149 g_string_append_c (str, '+');
151 if (*klass->name_space) {
152 g_string_append (str, klass->name_space);
153 g_string_append_c (str, '.');
155 g_string_append (str, klass->name);
158 if (klass->generic_inst) {
159 MonoGenericInst *ginst = klass->generic_inst;
162 g_string_append_c (str, '[');
163 for (i = 0; i < ginst->type_argc; i++) {
165 g_string_append_c (str, ',');
166 mono_type_get_name_recurse (ginst->type_argv [i], str, FALSE);
168 g_string_append_c (str, ']');
169 } else if (klass->gen_params) {
172 g_string_append_c (str, '[');
173 for (i = 0; i < klass->num_gen_params; i++) {
175 g_string_append_c (str, ',');
176 g_string_append (str, klass->gen_params [i].name);
178 g_string_append_c (str, ']');
185 * mono_type_get_name:
188 * Returns the string representation for type as required by System.Reflection.
189 * The inverse of mono_reflection_parse_type ().
192 mono_type_get_name (MonoType *type)
194 GString* result = g_string_new ("");
195 mono_type_get_name_recurse (type, result, FALSE);
198 g_string_append_c (result, '&');
200 return g_string_free (result, FALSE);
204 mono_class_is_open_constructed_type (MonoType *t)
210 case MONO_TYPE_SZARRAY:
211 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
212 case MONO_TYPE_ARRAY:
213 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
215 return mono_class_is_open_constructed_type (t->data.type);
216 case MONO_TYPE_GENERICINST: {
217 MonoGenericInst *ginst = t->data.generic_inst;
220 if (mono_class_is_open_constructed_type (ginst->generic_type))
222 for (i = 0; i < ginst->type_argc; i++)
223 if (mono_class_is_open_constructed_type (ginst->type_argv [i]))
233 inflate_generic_type (MonoType *type, MonoGenericContext *context)
235 switch (type->type) {
237 if (context->gmethod && context->gmethod->mtype_argv)
239 context->gmethod->mtype_argv [type->data.generic_param->num],
246 context->ginst->type_argv [type->data.generic_param->num],
250 case MONO_TYPE_SZARRAY: {
251 MonoClass *eclass = type->data.klass;
252 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
255 nt = dup_type (type, type);
256 nt->data.klass = mono_class_from_mono_type (inflated);
259 case MONO_TYPE_GENERICINST: {
260 MonoGenericInst *oginst = type->data.generic_inst;
261 MonoGenericInst *nginst;
265 nginst = g_new0 (MonoGenericInst, 1);
268 nginst->is_open = FALSE;
270 nginst->type_argv = g_new0 (MonoType *, oginst->type_argc);
272 for (i = 0; i < oginst->type_argc; i++) {
273 MonoType *t = oginst->type_argv [i];
274 nginst->type_argv [i] = mono_class_inflate_generic_type (t, context);
276 if (!nginst->is_open)
277 nginst->is_open = mono_class_is_open_constructed_type (nginst->type_argv [i]);
280 nginst->klass = NULL;
282 nginst->context = g_new0 (MonoGenericContext, 1);
283 nginst->context->ginst = nginst;
286 nt = g_hash_table_lookup (oginst->klass->image->generic_inst_cache, nginst);
289 g_free (nginst->type_argv);
291 mono_loader_unlock ();
295 nginst->dynamic_info = NULL;
296 nginst->initialized = FALSE;
298 mono_class_create_generic (nginst);
300 mono_stats.generic_instance_count++;
301 mono_stats.generics_metadata_size += sizeof (MonoGenericInst) +
302 sizeof (MonoGenericContext) +
303 nginst->type_argc * sizeof (MonoType);
305 nt = dup_type (type, type);
306 nt->data.generic_inst = nginst;
307 g_hash_table_insert (oginst->klass->image->generic_inst_cache, nginst, nt);
308 mono_loader_unlock ();
318 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
320 MonoType *inflated = inflate_generic_type (type, context);
325 mono_stats.inflated_type_count++;
329 static MonoMethodSignature*
330 inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig,
331 MonoGenericContext *context)
333 MonoMethodSignature *res;
335 res = mono_metadata_signature_alloc (image, sig->param_count);
336 res->ret = mono_class_inflate_generic_type (sig->ret, context);
337 for (i = 0; i < sig->param_count; ++i)
338 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
339 res->hasthis = sig->hasthis;
340 res->explicit_this = sig->explicit_this;
341 res->call_convention = sig->call_convention;
342 res->generic_param_count = sig->generic_param_count;
343 res->is_inflated = 1;
347 static MonoMethodHeader*
348 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
350 MonoMethodHeader *res;
352 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
353 res->code = header->code;
354 res->code_size = header->code_size;
355 res->max_stack = header->max_stack;
356 res->num_clauses = header->num_clauses;
357 res->init_locals = header->init_locals;
358 res->num_locals = header->num_locals;
359 res->clauses = header->clauses;
360 res->gen_params = header->gen_params;
361 for (i = 0; i < header->num_locals; ++i)
362 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
367 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context,
370 MonoMethodInflated *result;
372 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
373 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
376 mono_stats.inflated_method_count++;
377 mono_stats.generics_metadata_size +=
378 sizeof (MonoMethodInflated) - sizeof (MonoMethodNormal);
380 result = g_new0 (MonoMethodInflated, 1);
381 result->nmethod = *(MonoMethodNormal*)method;
383 if (result->nmethod.header)
384 result->nmethod.header = inflate_generic_header (
385 result->nmethod.header, context);
388 result->nmethod.method.klass = klass;
390 MonoType *declaring = mono_class_inflate_generic_type (
391 &method->klass->byval_arg, context);
392 result->nmethod.method.klass = mono_class_from_mono_type (declaring);
395 result->nmethod.method.signature = inflate_generic_signature (
396 method->klass->image, method->signature, context);
398 if (context->gmethod) {
399 result->context = g_new0 (MonoGenericContext, 1);
400 result->context->gmethod = context->gmethod;
401 result->context->ginst = result->nmethod.method.klass->generic_inst;
403 mono_stats.generics_metadata_size += sizeof (MonoGenericContext);
405 result->context = result->nmethod.method.klass->generic_inst->context;
407 if (method->signature->is_inflated)
408 result->declaring = ((MonoMethodInflated *) method)->declaring;
410 result->declaring = method;
412 return (MonoMethod *) result;
416 * class_compute_field_layout:
417 * @m: pointer to the metadata.
418 * @class: The class to initialize
420 * Initializes the class->fields.
422 * Currently we only support AUTO_LAYOUT, and do not even try to do
423 * a good job at it. This is temporary to get the code for Paolo.
426 class_compute_field_layout (MonoClass *class)
428 MonoImage *m = class->image;
429 const int top = class->field.count;
430 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
431 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
432 int i, blittable = TRUE, real_size = 0;
434 guint32 packing_size = 0;
435 gboolean explicit_size;
436 MonoClassField *field;
438 if (class->size_inited)
442 if (!class->parent->size_inited)
443 class_compute_field_layout (class->parent);
444 class->instance_size += class->parent->instance_size;
445 class->min_align = class->parent->min_align;
446 blittable = class->parent->blittable;
448 class->instance_size = sizeof (MonoObject);
449 class->min_align = 1;
452 /* Get the real size */
453 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
456 g_assert ((packing_size & 0xfffffff0) == 0);
457 class->packing_size = packing_size;
458 real_size += class->instance_size;
462 if (explicit_size && real_size) {
463 class->instance_size = MAX (real_size, class->instance_size);
465 class->size_inited = 1;
469 class->fields = g_new0 (MonoClassField, top);
472 * Fetch all the field information.
474 for (i = 0; i < top; i++){
476 guint32 cols [MONO_FIELD_SIZE];
477 int idx = class->field.first + i;
479 field = &class->fields [i];
480 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
481 /* The name is needed for fieldrefs */
482 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
483 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
484 mono_metadata_decode_value (sig, &sig);
485 /* FIELD signature == 0x06 */
486 g_assert (*sig == 0x06);
487 field->type = mono_metadata_parse_field_type (
488 m, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
489 if (mono_field_is_deleted (field))
491 if (class->generic_inst) {
492 field->type = mono_class_inflate_generic_type (
493 field->type, class->generic_inst->context);
494 field->type->attrs = cols [MONO_FIELD_FLAGS];
497 field->parent = class;
499 /* Only do these checks if we still think this type is blittable */
500 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
501 if (field->type->byref) {
504 MonoClass *field_class = mono_class_from_mono_type (field->type);
505 if (!field_class || !field_class->blittable)
510 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
511 mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
512 if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
513 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, field->name);
516 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
517 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
519 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
520 field->data = mono_image_rva_map (class->image, rva);
523 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
524 class->enum_basetype = field->type;
525 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
526 blittable = class->element_class->blittable;
529 /* The def_value of fields is compute lazily during vtable creation */
532 if (class == mono_defaults.string_class)
535 class->blittable = blittable;
537 if (class->enumtype && !class->enum_basetype) {
538 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
541 if (explicit_size && real_size) {
542 class->instance_size = MAX (real_size, class->instance_size);
545 if (class->gen_params)
548 mono_class_layout_fields (class);
552 mono_class_layout_fields (MonoClass *class)
555 const int top = class->field.count;
556 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
557 guint32 pass, passes, real_size;
558 gboolean gc_aware_layout = FALSE;
559 MonoClassField *field;
562 * Enable GC aware auto layout: in this mode, reference
563 * fields are grouped together inside objects, increasing collector
565 * Requires that all classes whose layout is known to native code be annotated
566 * with [StructLayout (LayoutKind.Sequential)]
567 * Value types have gc_aware_layout disabled by default, as per
568 * what the default is for other runtimes.
570 /* corlib is missing [StructLayout] directives in many places */
571 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
572 if (class->image != mono_defaults.corlib &&
573 class->byval_arg.type != MONO_TYPE_VALUETYPE)
574 gc_aware_layout = TRUE;
578 * Compute field layout and total size (not considering static fields)
582 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
583 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
590 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
594 real_size = class->parent->instance_size;
596 real_size = sizeof (MonoObject);
598 for (pass = 0; pass < passes; ++pass) {
599 for (i = 0; i < top; i++){
601 field = &class->fields [i];
603 if (mono_field_is_deleted (field))
605 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
608 if (gc_aware_layout) {
610 * We process fields with reference type in the first pass,
611 * and fields with non-reference type in the second pass.
612 * We use IS_POINTER instead of IS_REFERENCE because in
613 * some internal structures, we store GC_MALLOCed memory
614 * in IntPtr fields...
616 if (MONO_TYPE_IS_POINTER (field->type)) {
625 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
626 (strcmp (field->name, "$PRIVATE$") == 0)) {
627 /* This field is a hack inserted by MCS to empty structures */
631 size = mono_type_size (field->type, &align);
633 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
634 align = class->packing_size ? MIN (class->packing_size, align): align;
635 class->min_align = MAX (align, class->min_align);
636 field->offset = real_size;
637 field->offset += align - 1;
638 field->offset &= ~(align - 1);
639 real_size = field->offset + size;
642 class->instance_size = MAX (real_size, class->instance_size);
644 if (class->instance_size & (class->min_align - 1)) {
645 class->instance_size += class->min_align - 1;
646 class->instance_size &= ~(class->min_align - 1);
650 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
652 for (i = 0; i < top; i++) {
654 field = &class->fields [i];
657 * There must be info about all the fields in a type if it
658 * uses explicit layout.
661 if (mono_field_is_deleted (field))
663 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
666 size = mono_type_size (field->type, &align);
669 * When we get here, field->offset is already set by the
670 * loader (for either runtime fields or fields loaded from metadata).
671 * The offset is from the start of the object: this works for both
672 * classes and valuetypes.
674 field->offset += sizeof (MonoObject);
679 real_size = MAX (real_size, size + field->offset);
681 class->instance_size = MAX (real_size, class->instance_size);
685 class->size_inited = 1;
688 * Compute static field layout and size
690 for (i = 0; i < top; i++){
692 field = &class->fields [i];
694 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
696 if (mono_field_is_deleted (field))
699 size = mono_type_size (field->type, &align);
700 field->offset = class->class_size;
701 field->offset += align - 1;
702 field->offset &= ~(align - 1);
703 class->class_size = field->offset + size;
708 init_properties (MonoClass *class)
710 guint startm, endm, i, j;
711 guint32 cols [MONO_PROPERTY_SIZE];
712 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
713 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
715 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
716 class->property.count = class->property.last - class->property.first;
718 class->properties = g_new0 (MonoProperty, class->property.count);
719 for (i = class->property.first; i < class->property.last; ++i) {
720 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
721 class->properties [i - class->property.first].parent = class;
722 class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
723 class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
725 startm = mono_metadata_methods_from_property (class->image, i, &endm);
726 for (j = startm; j < endm; ++j) {
727 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
728 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
729 case METHOD_SEMANTIC_SETTER:
730 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
732 case METHOD_SEMANTIC_GETTER:
733 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
743 init_events (MonoClass *class)
745 guint startm, endm, i, j;
746 guint32 cols [MONO_EVENT_SIZE];
747 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
748 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
750 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
751 class->event.count = class->event.last - class->event.first;
753 class->events = g_new0 (MonoEvent, class->event.count);
754 for (i = class->event.first; i < class->event.last; ++i) {
755 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
756 class->events [i - class->event.first].parent = class;
757 class->events [i - class->event.first].attrs = cols [MONO_EVENT_FLAGS];
758 class->events [i - class->event.first].name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
760 startm = mono_metadata_methods_from_event (class->image, i, &endm);
761 for (j = startm; j < endm; ++j) {
762 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
763 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
764 case METHOD_SEMANTIC_ADD_ON:
765 class->events [i - class->event.first].add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
767 case METHOD_SEMANTIC_REMOVE_ON:
768 class->events [i - class->event.first].remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
770 case METHOD_SEMANTIC_FIRE:
771 class->events [i - class->event.first].raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
773 case METHOD_SEMANTIC_OTHER: /* don't care for now */
782 mono_get_unique_iid (MonoClass *class)
784 static GHashTable *iid_hash = NULL;
785 static guint iid = 0;
790 g_assert (MONO_CLASS_IS_INTERFACE (class));
795 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
797 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
799 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
800 mono_loader_unlock ();
804 g_hash_table_insert (iid_hash, str, (gpointer)iid);
808 mono_loader_unlock ();
814 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
819 for (i = 0; i < klass->interface_count; i++) {
820 ic = klass->interfaces [i];
823 *res = g_ptr_array_new ();
824 g_ptr_array_add (*res, ic);
826 collect_implemented_interfaces_aux (ic, res);
830 static inline GPtrArray*
831 collect_implemented_interfaces (MonoClass *klass)
833 GPtrArray *res = NULL;
835 collect_implemented_interfaces_aux (klass, &res);
840 setup_interface_offsets (MonoClass *class, int cur_slot)
846 /* compute maximum number of slots and maximum interface id */
848 for (k = class; k ; k = k->parent) {
849 for (i = 0; i < k->interface_count; i++) {
850 ic = k->interfaces [i];
853 mono_class_init (ic);
855 if (max_iid < ic->interface_id)
856 max_iid = ic->interface_id;
860 if (MONO_CLASS_IS_INTERFACE (class)) {
861 if (max_iid < class->interface_id)
862 max_iid = class->interface_id;
864 class->max_interface_id = max_iid;
865 /* compute vtable offset for interfaces */
866 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
868 for (i = 0; i <= max_iid; i++)
869 class->interface_offsets [i] = -1;
871 ifaces = collect_implemented_interfaces (class);
873 for (i = 0; i < ifaces->len; ++i) {
874 ic = g_ptr_array_index (ifaces, i);
875 class->interface_offsets [ic->interface_id] = cur_slot;
876 cur_slot += ic->method.count;
878 g_ptr_array_free (ifaces, TRUE);
881 for (k = class->parent; k ; k = k->parent) {
882 ifaces = collect_implemented_interfaces (k);
884 for (i = 0; i < ifaces->len; ++i) {
885 ic = g_ptr_array_index (ifaces, i);
887 if (class->interface_offsets [ic->interface_id] == -1) {
888 int io = k->interface_offsets [ic->interface_id];
892 class->interface_offsets [ic->interface_id] = io;
895 g_ptr_array_free (ifaces, TRUE);
899 if (MONO_CLASS_IS_INTERFACE (class))
900 class->interface_offsets [class->interface_id] = cur_slot;
906 mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
910 int i, max_vtsize = 0, max_iid, cur_slot = 0;
912 MonoGHashTable *override_map = NULL;
914 /* setup_vtable() must be called only once on the type */
915 if (class->interface_offsets) {
916 g_warning ("vtable already computed in %s.%s", class->name_space, class->name);
920 ifaces = collect_implemented_interfaces (class);
922 for (i = 0; i < ifaces->len; i++) {
923 MonoClass *ic = g_ptr_array_index (ifaces, i);
924 max_vtsize += ic->method.count;
926 g_ptr_array_free (ifaces, TRUE);
930 max_vtsize += class->parent->vtable_size;
931 cur_slot = class->parent->vtable_size;
934 max_vtsize += class->method.count;
936 vtable = alloca (sizeof (gpointer) * max_vtsize);
937 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
939 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
941 cur_slot = setup_interface_offsets (class, cur_slot);
942 max_iid = class->max_interface_id;
944 if (class->parent && class->parent->vtable_size)
945 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
947 /* override interface methods */
948 for (i = 0; i < onum; i++) {
949 MonoMethod *decl = overrides [i*2];
950 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
952 g_assert (decl->slot != -1);
953 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
954 vtable [dslot] = overrides [i*2 + 1];
955 vtable [dslot]->slot = dslot;
957 override_map = mono_g_hash_table_new (NULL, NULL);
959 mono_g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
963 for (k = class; k ; k = k->parent) {
965 ifaces = collect_implemented_interfaces (k);
967 nifaces = ifaces->len;
968 for (i = 0; i < nifaces; i++) {
971 ic = g_ptr_array_index (ifaces, i);
972 io = k->interface_offsets [ic->interface_id];
975 g_assert (io <= max_vtsize);
978 for (l = 0; l < ic->method.count; l++) {
979 MonoMethod *im = ic->methods [l];
981 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
984 for (j = 0; j < class->method.count; ++j) {
985 MonoMethod *cm = class->methods [j];
986 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
987 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
988 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
990 if (!strcmp(cm->name, im->name) &&
991 mono_metadata_signature_equal (cm->signature, im->signature)) {
992 g_assert (io + l <= max_vtsize);
993 vtable [io + l] = cm;
998 /* already implemented */
999 if (io >= k->vtable_size)
1003 for (l = 0; l < ic->method.count; l++) {
1004 MonoMethod *im = ic->methods [l];
1007 g_assert (io + l <= max_vtsize);
1009 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1012 for (k1 = class; k1; k1 = k1->parent) {
1013 for (j = 0; j < k1->method.count; ++j) {
1014 MonoMethod *cm = k1->methods [j];
1016 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1017 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
1020 if (!strcmp(cm->name, im->name) &&
1021 mono_metadata_signature_equal (cm->signature, im->signature)) {
1022 g_assert (io + l <= max_vtsize);
1023 vtable [io + l] = cm;
1028 g_assert (io + l <= max_vtsize);
1029 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1034 for (l = 0; l < ic->method.count; l++) {
1035 MonoMethod *im = ic->methods [l];
1036 char *qname, *fqname;
1039 if (vtable [io + l])
1042 qname = g_strconcat (ic->name, ".", im->name, NULL);
1043 if (ic->name_space && ic->name_space [0])
1044 fqname = g_strconcat (ic->name_space, ".", ic->name, ".", im->name, NULL);
1048 for (k1 = class; k1; k1 = k1->parent) {
1049 for (j = 0; j < k1->method.count; ++j) {
1050 MonoMethod *cm = k1->methods [j];
1052 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1055 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
1056 mono_metadata_signature_equal (cm->signature, im->signature)) {
1057 g_assert (io + l <= max_vtsize);
1058 vtable [io + l] = cm;
1068 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
1069 for (l = 0; l < ic->method.count; l++) {
1071 MonoMethod *im = ic->methods [l];
1072 if (im->flags & METHOD_ATTRIBUTE_STATIC)
1074 g_assert (io + l <= max_vtsize);
1077 * If one of our parents already implements this interface
1078 * we can inherit the implementation.
1080 if (!(vtable [io + l])) {
1081 MonoClass *parent = class->parent;
1083 if ((ic->interface_id <= parent->max_interface_id) &&
1084 (parent->interface_offsets [ic->interface_id]) &&
1086 vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
1089 if (!(vtable [io + l])) {
1090 for (j = 0; j < onum; ++j) {
1091 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
1092 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
1094 msig = mono_signature_get_desc (im->signature, FALSE);
1095 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
1096 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
1098 for (j = 0; j < class->method.count; ++j) {
1099 MonoMethod *cm = class->methods [j];
1100 msig = mono_signature_get_desc (cm->signature, FALSE);
1102 printf ("METHOD %s(%s)\n", cm->name, msig);
1105 g_assert_not_reached ();
1110 for (l = 0; l < ic->method.count; l++) {
1111 MonoMethod *im = vtable [io + l];
1114 g_assert (io + l <= max_vtsize);
1116 /* FIXME: why do we need this ? */
1118 /* g_assert_not_reached (); */
1124 g_ptr_array_free (ifaces, TRUE);
1127 for (i = 0; i < class->method.count; ++i) {
1130 cm = class->methods [i];
1133 * Non-virtual method have no place in the vtable.
1134 * This also catches static methods (since they are not virtual).
1136 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1140 * If the method is REUSE_SLOT, we must check in the
1141 * base class for a method to override.
1143 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
1145 for (k = class->parent; k ; k = k->parent) {
1147 for (j = 0; j < k->method.count; ++j) {
1148 MonoMethod *m1 = k->methods [j];
1149 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
1151 if (!strcmp(cm->name, m1->name) &&
1152 mono_metadata_signature_equal (cm->signature, m1->signature)) {
1153 slot = k->methods [j]->slot;
1154 g_assert (cm->slot < max_vtsize);
1156 override_map = mono_g_hash_table_new (NULL, NULL);
1157 mono_g_hash_table_insert (override_map, m1, cm);
1169 cm->slot = cur_slot++;
1171 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT) && !cm->signature->generic_param_count)
1172 vtable [cm->slot] = cm;
1175 /* override non interface methods */
1176 for (i = 0; i < onum; i++) {
1177 MonoMethod *decl = overrides [i*2];
1178 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
1179 g_assert (decl->slot != -1);
1180 vtable [decl->slot] = overrides [i*2 + 1];
1181 overrides [i * 2 + 1]->slot = decl->slot;
1183 override_map = mono_g_hash_table_new (NULL, NULL);
1184 mono_g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
1189 * If a method occupies more than one place in the vtable, and it is
1190 * overriden, then change the other occurances too.
1193 for (i = 0; i < max_vtsize; ++i)
1195 MonoMethod *cm = mono_g_hash_table_lookup (override_map, vtable [i]);
1200 mono_g_hash_table_destroy (override_map);
1203 if (class->generic_inst) {
1204 MonoClass *gklass = mono_class_from_mono_type (class->generic_inst->generic_type);
1206 mono_class_init (gklass);
1207 class->vtable_size = gklass->vtable_size;
1209 class->vtable_size = cur_slot;
1211 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
1212 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
1214 if (mono_print_vtable) {
1217 for (i = 0; i <= max_iid; i++)
1218 if (class->interface_offsets [i] != -1)
1221 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
1222 class->name, class->vtable_size, icount);
1224 for (i = 0; i < class->vtable_size; ++i) {
1229 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
1230 cm->klass->name_space, cm->klass->name,
1237 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
1238 class->name, max_iid);
1240 for (i = 0; i < class->interface_count; i++) {
1241 ic = class->interfaces [i];
1242 printf (" slot %03d(%03d) %s.%s\n",
1243 class->interface_offsets [ic->interface_id],
1244 ic->method.count, ic->name_space, ic->name);
1247 for (k = class->parent; k ; k = k->parent) {
1248 for (i = 0; i < k->interface_count; i++) {
1249 ic = k->interfaces [i];
1250 printf (" slot %03d(%03d) %s.%s\n",
1251 class->interface_offsets [ic->interface_id],
1252 ic->method.count, ic->name_space, ic->name);
1261 * @class: the class to initialize
1263 * compute the instance_size, class_size and other infos that cannot be
1264 * computed at mono_class_get() time. Also compute a generic vtable and
1265 * the method slot numbers. We use this infos later to create a domain
1269 mono_class_init (MonoClass *class)
1272 static MonoMethod *default_ghc = NULL;
1273 static MonoMethod *default_finalize = NULL;
1274 static int finalize_slot = -1;
1275 static int ghc_slot = -1;
1276 MonoMethod **overrides;
1284 /*g_print ("Init class %s\n", class->name);*/
1286 /* We do everything inside the lock to prevent races */
1287 mono_loader_lock ();
1289 if (class->inited) {
1290 mono_loader_unlock ();
1291 /* Somebody might have gotten in before us */
1295 if (class->init_pending) {
1296 /* this indicates a cyclic dependency */
1297 g_error ("pending init %s.%s\n", class->name_space, class->name);
1300 class->init_pending = 1;
1302 if (mono_debugger_start_class_init_func)
1303 mono_debugger_start_class_init_func (class);
1305 mono_stats.initialized_class_count++;
1307 if (class->generic_inst && !class->generic_inst->is_dynamic) {
1308 MonoGenericInst *ginst = class->generic_inst;
1312 gklass = mono_class_from_mono_type (ginst->generic_type);
1313 mono_class_init (gklass);
1316 class->parent = mono_class_from_mono_type (ginst->parent);
1318 class->parent = gklass->parent;
1320 mono_class_setup_parent (class, class->parent);
1322 if (MONO_CLASS_IS_INTERFACE (class))
1323 class->interface_id = mono_get_unique_iid (class);
1325 class->method = gklass->method;
1326 class->methods = g_new0 (MonoMethod *, class->method.count);
1328 for (i = 0; i < class->method.count; i++)
1329 class->methods [i] = mono_class_inflate_generic_method (
1330 gklass->methods [i], ginst->context, ginst->klass);
1332 class->field = gklass->field;
1333 class->fields = g_new0 (MonoClassField, class->field.count);
1335 for (i = 0; i < class->field.count; i++) {
1336 MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
1337 ifield->generic_type = gklass->fields [i].type;
1339 class->fields [i] = gklass->fields [i];
1340 class->fields [i].generic_info = ifield;
1341 class->fields [i].parent = class;
1342 class->fields [i].type = mono_class_inflate_generic_type (
1343 class->fields [i].type, ginst->context);
1346 class->property = gklass->property;
1347 class->properties = g_new0 (MonoProperty, class->property.count);
1349 for (i = 0; i < class->property.count; i++) {
1350 MonoProperty *prop = &class->properties [i];
1352 *prop = gklass->properties [i];
1355 prop->get = mono_class_inflate_generic_method (
1356 prop->get, ginst->context, ginst->klass);
1358 prop->set = mono_class_inflate_generic_method (
1359 prop->set, ginst->context, ginst->klass);
1361 prop->parent = class;
1364 class->interface_count = gklass->interface_count;
1365 class->interfaces = g_new0 (MonoClass *, class->interface_count);
1366 for (i = 0; i < class->interface_count; i++) {
1367 MonoType *it = &gklass->interfaces [i]->byval_arg;
1368 MonoType *inflated = mono_class_inflate_generic_type (
1369 it, ginst->context);
1370 class->interfaces [i] = mono_class_from_mono_type (inflated);
1371 mono_class_init (class->interfaces [i]);
1374 for (list = gklass->nested_classes; list; list = list->next)
1375 class->nested_classes = g_list_append (
1376 class->nested_classes, list->data);
1379 if (class->parent && !class->parent->inited)
1380 mono_class_init (class->parent);
1383 * Computes the size used by the fields, and their locations
1385 if (!class->size_inited)
1386 class_compute_field_layout (class);
1388 /* initialize method pointers */
1391 MonoMethodSignature *sig;
1392 class->method.count = class->rank > 1? 2: 1;
1393 sig = mono_metadata_signature_alloc (class->image, class->rank);
1394 sig->ret = &mono_defaults.void_class->byval_arg;
1395 sig->pinvoke = TRUE;
1396 for (i = 0; i < class->rank; ++i)
1397 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1399 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1400 ctor->klass = class;
1401 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1402 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1403 ctor->signature = sig;
1404 ctor->name = ".ctor";
1406 class->methods = g_new (MonoMethod*, class->method.count);
1407 class->methods [0] = ctor;
1408 if (class->rank > 1) {
1409 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
1410 sig->ret = &mono_defaults.void_class->byval_arg;
1411 sig->pinvoke = TRUE;
1412 for (i = 0; i < class->rank * 2; ++i)
1413 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1415 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1416 ctor->klass = class;
1417 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1418 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1419 ctor->signature = sig;
1420 ctor->name = ".ctor";
1422 class->methods [1] = ctor;
1425 if (!class->generic_inst && !class->methods) {
1426 class->methods = g_new (MonoMethod*, class->method.count);
1427 for (i = 0; i < class->method.count; ++i) {
1428 class->methods [i] = mono_get_method (class->image,
1429 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
1434 if (!class->generic_inst) {
1435 init_properties (class);
1436 init_events (class);
1438 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
1441 guint32 cols [MONO_NESTED_CLASS_SIZE];
1442 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
1443 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
1444 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
1446 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
1450 mono_class_setup_supertypes (class);
1452 if (MONO_CLASS_IS_INTERFACE (class)) {
1453 for (i = 0; i < class->method.count; ++i)
1454 class->methods [i]->slot = i;
1455 class->init_pending = 0;
1458 * class->interface_offsets is needed for the castclass/isinst code, so
1459 * we have to setup them for interfaces, too.
1461 setup_interface_offsets (class, 0);
1462 mono_loader_unlock ();
1464 if (mono_debugger_class_init_func)
1465 mono_debugger_class_init_func (class);
1470 overrides = mono_class_get_overrides (class->image, class->type_token, &onum);
1471 mono_class_setup_vtable (class, overrides, onum);
1475 class->init_pending = 0;
1478 if (class == mono_defaults.object_class) {
1480 for (i = 0; i < class->vtable_size; ++i) {
1481 MonoMethod *cm = class->vtable [i];
1483 if (!strcmp (cm->name, "GetHashCode")) {
1489 g_assert (ghc_slot > 0);
1491 default_ghc = class->vtable [ghc_slot];
1496 if (class->parent) {
1498 if (class->vtable [ghc_slot] == default_ghc) {
1503 if (!default_finalize) {
1504 if (class == mono_defaults.object_class) {
1506 for (i = 0; i < class->vtable_size; ++i) {
1507 MonoMethod *cm = class->vtable [i];
1509 if (!strcmp (cm->name, "Finalize")) {
1515 g_assert (finalize_slot > 0);
1517 default_finalize = class->vtable [finalize_slot];
1521 /* Object::Finalize should have empty implemenatation */
1522 class->has_finalize = 0;
1523 if (class->parent) {
1524 if (class->vtable [finalize_slot] != default_finalize)
1525 class->has_finalize = 1;
1528 mono_loader_unlock ();
1530 if (mono_debugger_class_init_func)
1531 mono_debugger_class_init_func (class);
1535 mono_class_setup_mono_type (MonoClass *class)
1537 const char *name = class->name;
1538 const char *nspace = class->name_space;
1540 if (MONO_CLASS_IS_INTERFACE (class))
1541 class->interface_id = mono_get_unique_iid (class);
1543 class->this_arg.byref = 1;
1544 class->this_arg.data.klass = class;
1545 class->this_arg.type = MONO_TYPE_CLASS;
1546 class->byval_arg.data.klass = class;
1547 class->byval_arg.type = MONO_TYPE_CLASS;
1549 if (!strcmp (nspace, "System")) {
1550 if (!strcmp (name, "ValueType")) {
1552 * do not set the valuetype bit for System.ValueType.
1553 * class->valuetype = 1;
1555 class->blittable = TRUE;
1556 } else if (!strcmp (name, "Enum")) {
1558 * do not set the valuetype bit for System.Enum.
1559 * class->valuetype = 1;
1561 class->valuetype = 0;
1562 class->enumtype = 0;
1563 } else if (!strcmp (name, "Object")) {
1564 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
1565 } else if (!strcmp (name, "String")) {
1566 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
1567 } else if (!strcmp (name, "TypedReference")) {
1568 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
1572 if (class->valuetype) {
1573 int t = MONO_TYPE_VALUETYPE;
1574 if (!strcmp (nspace, "System")) {
1577 if (!strcmp (name, "Boolean")) {
1578 t = MONO_TYPE_BOOLEAN;
1579 } else if (!strcmp(name, "Byte")) {
1581 class->blittable = TRUE;
1585 if (!strcmp (name, "Char")) {
1590 if (!strcmp (name, "Double")) {
1592 class->blittable = TRUE;
1596 if (!strcmp (name, "Int32")) {
1598 class->blittable = TRUE;
1599 } else if (!strcmp(name, "Int16")) {
1601 class->blittable = TRUE;
1602 } else if (!strcmp(name, "Int64")) {
1604 class->blittable = TRUE;
1605 } else if (!strcmp(name, "IntPtr")) {
1607 class->blittable = TRUE;
1611 if (!strcmp (name, "Single")) {
1613 class->blittable = TRUE;
1614 } else if (!strcmp(name, "SByte")) {
1616 class->blittable = TRUE;
1620 if (!strcmp (name, "UInt32")) {
1622 class->blittable = TRUE;
1623 } else if (!strcmp(name, "UInt16")) {
1625 class->blittable = TRUE;
1626 } else if (!strcmp(name, "UInt64")) {
1628 class->blittable = TRUE;
1629 } else if (!strcmp(name, "UIntPtr")) {
1631 class->blittable = TRUE;
1635 if (!strcmp (name, "TypedReference")) {
1636 t = MONO_TYPE_TYPEDBYREF;
1637 class->blittable = TRUE;
1641 if (!strcmp (name, "Void")) {
1649 class->this_arg.type = class->byval_arg.type = t;
1654 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
1656 gboolean system_namespace;
1658 system_namespace = !strcmp (class->name_space, "System");
1660 /* if root of the hierarchy */
1661 if (system_namespace && !strcmp (class->name, "Object")) {
1662 class->parent = NULL;
1663 class->instance_size = sizeof (MonoObject);
1666 if (!strcmp (class->name, "<Module>")) {
1667 class->parent = NULL;
1668 class->instance_size = 0;
1672 if (!MONO_CLASS_IS_INTERFACE (class)) {
1673 class->parent = parent;
1676 g_assert_not_reached (); /* FIXME */
1678 if (parent->generic_inst && !parent->name) {
1680 * If the parent is a generic instance, we may get
1681 * called before it is fully initialized, especially
1682 * before it has its name.
1687 class->marshalbyref = parent->marshalbyref;
1688 class->contextbound = parent->contextbound;
1689 class->delegate = parent->delegate;
1691 if (system_namespace) {
1692 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
1693 class->marshalbyref = 1;
1695 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
1696 class->contextbound = 1;
1698 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
1699 class->delegate = 1;
1702 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
1703 (strcmp (class->parent->name_space, "System") == 0)))
1704 class->valuetype = 1;
1705 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
1706 class->valuetype = class->enumtype = 1;
1708 /*class->enumtype = class->parent->enumtype; */
1709 mono_class_setup_supertypes (class);
1711 class->parent = NULL;
1717 mono_class_setup_supertypes (MonoClass *class)
1722 if (class->supertypes)
1726 for (k = class; k ; k = k->parent) {
1730 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
1731 class->supertypes = g_new0 (MonoClass *, ms);
1733 if (class->parent) {
1734 for (i = class->idepth, k = class; k ; k = k->parent)
1735 class->supertypes [--i] = k;
1737 class->supertypes [0] = class;
1742 * @image: context where the image is created
1743 * @type_token: typedef token
1746 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
1748 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
1749 MonoClass *class, *parent = NULL;
1750 guint32 cols [MONO_TYPEDEF_SIZE];
1751 guint32 cols_next [MONO_TYPEDEF_SIZE];
1752 guint tidx = mono_metadata_token_index (type_token);
1753 const char *name, *nspace;
1755 MonoClass **interfaces;
1757 mono_loader_lock ();
1759 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
1760 mono_loader_unlock ();
1764 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
1766 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
1768 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1769 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1771 class = g_malloc0 (sizeof (MonoClass));
1774 class->name_space = nspace;
1776 class->image = image;
1777 class->type_token = type_token;
1778 class->flags = cols [MONO_TYPEDEF_FLAGS];
1780 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
1782 if (cols [MONO_TYPEDEF_EXTENDS])
1783 parent = mono_class_get (image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]));
1784 interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
1786 class->interfaces = interfaces;
1787 class->interface_count = icount;
1789 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
1791 /* fixme: maybe we must set this on windows
1792 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
1796 class->cast_class = class->element_class = class;
1798 /*g_print ("Load class %s\n", name);*/
1800 mono_class_setup_parent (class, parent);
1802 mono_class_setup_mono_type (class);
1805 * Compute the field and method lists
1807 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
1808 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
1810 if (tt->rows > tidx){
1811 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
1812 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1813 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1815 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
1816 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
1819 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
1820 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
1821 class->field.count = class->field.last - class->field.first;
1823 class->field.count = 0;
1825 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
1826 class->method.count = class->method.last - class->method.first;
1828 class->method.count = 0;
1830 /* reserve space to store vector pointer in arrays */
1831 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
1832 class->instance_size += 2 * sizeof (gpointer);
1833 g_assert (class->field.count == 0);
1836 if (class->enumtype)
1837 class_compute_field_layout (class);
1839 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
1840 class->nested_in = mono_class_create_from_typedef (image, type_token);
1842 class->gen_params = mono_metadata_load_generic_params (image, class->type_token, &icount);
1843 class->num_gen_params = icount;
1845 mono_loader_unlock ();
1851 mono_class_create_generic (MonoGenericInst *ginst)
1853 MonoClass *klass, *gklass;
1856 ginst->klass = g_malloc0 (sizeof (MonoClass));
1857 klass = ginst->klass;
1859 gklass = mono_class_from_mono_type (ginst->generic_type);
1861 klass->nested_in = gklass->nested_in;
1863 klass->name = gklass->name;
1864 klass->name_space = gklass->name_space;
1865 klass->image = gklass->image;
1866 klass->flags = gklass->flags;
1868 klass->generic_inst = ginst;
1870 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
1871 klass->this_arg.data.generic_inst = klass->byval_arg.data.generic_inst = ginst;
1872 klass->this_arg.byref = TRUE;
1874 klass->cast_class = klass->element_class = klass;
1876 if (ginst->is_dynamic) {
1877 klass->instance_size = gklass->instance_size;
1878 klass->class_size = gklass->class_size;
1879 klass->size_inited = 1;
1881 klass->valuetype = gklass->valuetype;
1888 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
1890 MonoClass *klass, **ptr;
1894 return param->pklass;
1896 klass = param->pklass = g_new0 (MonoClass, 1);
1898 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
1902 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
1903 klass->parent = param->constraints [0];
1907 if (count - pos > 0) {
1908 klass->interface_count = count - pos;
1909 klass->interfaces = g_new0 (MonoClass *, count - pos);
1910 for (i = pos; i < count; i++)
1911 klass->interfaces [i - pos] = param->constraints [i];
1914 g_assert (param->name);
1916 klass->name = param->name;
1917 klass->name_space = "";
1918 klass->image = image;
1919 klass->cast_class = klass->element_class = klass;
1920 klass->enum_basetype = &klass->element_class->byval_arg;
1921 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
1923 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
1924 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
1925 klass->this_arg.byref = TRUE;
1927 mono_class_init (klass);
1933 my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
1938 return param->pklass;
1940 klass = g_new0 (MonoClass, 1);
1943 klass->name = param->name;
1945 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
1946 klass->name_space = "";
1947 klass->image = mono_defaults.corlib;
1948 klass->cast_class = klass->element_class = klass;
1949 klass->enum_basetype = &klass->element_class->byval_arg;
1950 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
1952 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
1953 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
1954 klass->this_arg.byref = TRUE;
1956 mono_class_init (klass);
1962 mono_ptr_class_get (MonoType *type)
1965 MonoClass *el_class;
1966 static GHashTable *ptr_hash = NULL;
1968 mono_loader_lock ();
1971 ptr_hash = g_hash_table_new (NULL, NULL);
1972 el_class = mono_class_from_mono_type (type);
1973 if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
1974 mono_loader_unlock ();
1977 result = g_new0 (MonoClass, 1);
1979 result->parent = NULL; /* no parent for PTR types */
1980 result->name = "System";
1981 result->name_space = "MonoPtrFakeClass";
1982 result->image = el_class->image;
1983 result->inited = TRUE;
1984 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
1985 /* Can pointers get boxed? */
1986 result->instance_size = sizeof (gpointer);
1987 result->cast_class = result->element_class = el_class;
1988 result->enum_basetype = &result->element_class->byval_arg;
1989 result->blittable = TRUE;
1991 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
1992 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
1993 result->this_arg.byref = TRUE;
1995 mono_class_setup_supertypes (result);
1997 g_hash_table_insert (ptr_hash, el_class, result);
1999 mono_loader_unlock ();
2005 mono_fnptr_class_get (MonoMethodSignature *sig)
2008 static GHashTable *ptr_hash = NULL;
2010 mono_loader_lock ();
2013 ptr_hash = g_hash_table_new (NULL, NULL);
2015 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
2016 mono_loader_unlock ();
2019 result = g_new0 (MonoClass, 1);
2021 result->parent = NULL; /* no parent for PTR types */
2022 result->name = "System";
2023 result->name_space = "MonoFNPtrFakeClass";
2024 result->image = NULL; /* need to fix... */
2025 result->inited = TRUE;
2026 result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
2027 /* Can pointers get boxed? */
2028 result->instance_size = sizeof (gpointer);
2029 result->cast_class = result->element_class = result;
2030 result->blittable = TRUE;
2032 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
2033 result->this_arg.data.method = result->byval_arg.data.method = sig;
2034 result->this_arg.byref = TRUE;
2035 result->enum_basetype = &result->element_class->byval_arg;
2036 result->blittable = TRUE;
2038 mono_class_setup_supertypes (result);
2040 g_hash_table_insert (ptr_hash, sig, result);
2042 mono_loader_unlock ();
2048 mono_class_from_mono_type (MonoType *type)
2050 switch (type->type) {
2051 case MONO_TYPE_OBJECT:
2052 return type->data.klass? type->data.klass: mono_defaults.object_class;
2053 case MONO_TYPE_VOID:
2054 return type->data.klass? type->data.klass: mono_defaults.void_class;
2055 case MONO_TYPE_BOOLEAN:
2056 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
2057 case MONO_TYPE_CHAR:
2058 return type->data.klass? type->data.klass: mono_defaults.char_class;
2060 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
2062 return type->data.klass? type->data.klass: mono_defaults.byte_class;
2064 return type->data.klass? type->data.klass: mono_defaults.int16_class;
2066 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
2068 return type->data.klass? type->data.klass: mono_defaults.int32_class;
2070 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
2072 return type->data.klass? type->data.klass: mono_defaults.int_class;
2074 return type->data.klass? type->data.klass: mono_defaults.uint_class;
2076 return type->data.klass? type->data.klass: mono_defaults.int64_class;
2078 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
2080 return type->data.klass? type->data.klass: mono_defaults.single_class;
2082 return type->data.klass? type->data.klass: mono_defaults.double_class;
2083 case MONO_TYPE_STRING:
2084 return type->data.klass? type->data.klass: mono_defaults.string_class;
2085 case MONO_TYPE_TYPEDBYREF:
2086 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
2087 case MONO_TYPE_ARRAY:
2088 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
2090 return mono_ptr_class_get (type->data.type);
2091 case MONO_TYPE_FNPTR:
2092 return mono_fnptr_class_get (type->data.method);
2093 case MONO_TYPE_SZARRAY:
2094 return mono_array_class_get (type->data.klass, 1);
2095 case MONO_TYPE_CLASS:
2096 case MONO_TYPE_VALUETYPE:
2097 return type->data.klass;
2098 case MONO_TYPE_GENERICINST:
2099 g_assert (type->data.generic_inst->klass);
2100 return type->data.generic_inst->klass;
2102 return my_mono_class_from_generic_parameter (type->data.generic_param, FALSE);
2103 case MONO_TYPE_MVAR:
2104 return my_mono_class_from_generic_parameter (type->data.generic_param, TRUE);
2106 g_warning ("implement me 0x%02x\n", type->type);
2107 g_assert_not_reached ();
2114 * @image: context where the image is created
2115 * @type_spec: typespec token
2118 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec,
2119 MonoGenericContext *context)
2121 MonoType *type, *inflated;
2124 type = mono_type_create_from_typespec (image, type_spec);
2126 switch (type->type) {
2127 case MONO_TYPE_ARRAY:
2128 class = mono_array_class_get (type->data.array->eklass, type->data.array->rank);
2130 case MONO_TYPE_SZARRAY:
2131 class = mono_array_class_get (type->data.klass, 1);
2134 class = mono_ptr_class_get (type->data.type);
2136 case MONO_TYPE_GENERICINST:
2137 g_assert (type->data.generic_inst->klass);
2138 class = type->data.generic_inst->klass;
2141 /* it seems any type can be stored in TypeSpec as well */
2142 class = mono_class_from_mono_type (type);
2146 if (!class || !context)
2149 inflated = mono_class_inflate_generic_type (&class->byval_arg, context);
2151 return mono_class_from_mono_type (inflated);
2155 * mono_bounded_array_class_get:
2156 * @element_class: element class
2157 * @rank: the dimension of the array class
2158 * @bounded: whenever the array has non-zero bounds
2160 * Returns: a class object describing the array with element type @element_type and
2164 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
2168 MonoClass *parent = NULL;
2169 GSList *list, *rootlist;
2172 gboolean corlib_type = FALSE;
2174 g_assert (rank <= 255);
2177 /* bounded only matters for one-dimensional arrays */
2180 image = eclass->image;
2182 mono_loader_lock ();
2184 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
2185 for (; list; list = list->next) {
2187 if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
2188 mono_loader_unlock ();
2194 /* for the building corlib use System.Array from it */
2195 if (image->assembly && image->assembly->dynamic && strcmp (image->assembly_name, "mscorlib") == 0) {
2196 parent = mono_class_from_name (image, "System", "Array");
2199 parent = mono_defaults.array_class;
2200 if (!parent->inited)
2201 mono_class_init (parent);
2204 class = g_malloc0 (sizeof (MonoClass));
2206 class->image = image;
2207 class->name_space = eclass->name_space;
2208 nsize = strlen (eclass->name);
2209 name = g_malloc (nsize + 2 + rank);
2210 memcpy (name, eclass->name, nsize);
2213 memset (name + nsize + 1, ',', rank - 1);
2214 name [nsize + rank] = ']';
2215 name [nsize + rank + 1] = 0;
2217 class->type_token = 0;
2218 /* all arrays are marked serializable and sealed, bug #42779 */
2219 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
2220 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2221 class->parent = parent;
2222 class->instance_size = mono_class_instance_size (class->parent);
2223 class->class_size = 0;
2224 mono_class_setup_supertypes (class);
2228 if (eclass->enumtype)
2229 class->cast_class = eclass->element_class;
2231 class->cast_class = eclass;
2233 class->element_class = eclass;
2235 if ((rank > 1) || bounded) {
2236 MonoArrayType *at = g_new0 (MonoArrayType, 1);
2237 class->byval_arg.type = MONO_TYPE_ARRAY;
2238 class->byval_arg.data.array = at;
2239 at->eklass = eclass;
2241 /* FIXME: complete.... */
2243 class->byval_arg.type = MONO_TYPE_SZARRAY;
2244 class->byval_arg.data.klass = eclass;
2246 class->this_arg = class->byval_arg;
2247 class->this_arg.byref = 1;
2252 list = g_slist_append (rootlist, class);
2253 g_hash_table_insert (image->array_cache, eclass, list);
2255 mono_loader_unlock ();
2261 * mono_array_class_get:
2262 * @element_class: element class
2263 * @rank: the dimension of the array class
2265 * Returns: a class object describing the array with element type @element_type and
2269 mono_array_class_get (MonoClass *eclass, guint32 rank)
2271 return mono_bounded_array_class_get (eclass, rank, FALSE);
2275 * mono_class_instance_size:
2278 * Returns: the size of an object instance
2281 mono_class_instance_size (MonoClass *klass)
2283 if (!klass->size_inited)
2284 mono_class_init (klass);
2286 return klass->instance_size;
2290 * mono_class_min_align:
2293 * Returns: minimm alignment requirements
2296 mono_class_min_align (MonoClass *klass)
2298 if (!klass->size_inited)
2299 mono_class_init (klass);
2301 return klass->min_align;
2305 * mono_class_value_size:
2308 * This function is used for value types, and return the
2309 * space and the alignment to store that kind of value object.
2311 * Returns: the size of a value of kind @klass
2314 mono_class_value_size (MonoClass *klass, guint32 *align)
2318 /* fixme: check disable, because we still have external revereces to
2319 * mscorlib and Dummy Objects
2321 /*g_assert (klass->valuetype);*/
2323 size = mono_class_instance_size (klass) - sizeof (MonoObject);
2326 *align = klass->min_align;
2332 * mono_class_data_size:
2335 * Returns: the size of the static class data
2338 mono_class_data_size (MonoClass *klass)
2341 mono_class_init (klass);
2343 return klass->class_size;
2347 * Auxiliary routine to mono_class_get_field
2349 * Takes a field index instead of a field token.
2351 static MonoClassField *
2352 mono_class_get_field_idx (MonoClass *class, int idx)
2354 if (class->field.count){
2355 if ((idx >= class->field.first) && (idx < class->field.last)){
2356 return &class->fields [idx - class->field.first];
2363 return mono_class_get_field_idx (class->parent, idx);
2367 * mono_class_get_field:
2368 * @class: the class to lookup the field.
2369 * @field_token: the field token
2371 * Returns: A MonoClassField representing the type and offset of
2372 * the field, or a NULL value if the field does not belong to this
2376 mono_class_get_field (MonoClass *class, guint32 field_token)
2378 int idx = mono_metadata_token_index (field_token);
2380 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
2382 return mono_class_get_field_idx (class, idx - 1);
2386 mono_class_get_field_from_name (MonoClass *klass, const char *name)
2391 for (i = 0; i < klass->field.count; ++i) {
2392 if (strcmp (name, klass->fields [i].name) == 0)
2393 return &klass->fields [i];
2395 klass = klass->parent;
2401 mono_vtable_get_static_field_data (MonoVTable *vt)
2407 mono_class_get_property_from_name (MonoClass *klass, const char *name)
2412 for (i = 0; i < klass->property.count; ++i) {
2413 if (strcmp (name, klass->properties [i].name) == 0)
2414 return &klass->properties [i];
2416 klass = klass->parent;
2423 * @image: the image where the class resides
2424 * @type_token: the token for the class
2425 * @at: an optional pointer to return the array element type
2427 * Returns: the MonoClass that represents @type_token in @image
2430 mono_class_get (MonoImage *image, guint32 type_token)
2432 MonoClass *class = NULL;
2435 return mono_lookup_dynamic_token (image, type_token);
2437 switch (type_token & 0xff000000){
2438 case MONO_TOKEN_TYPE_DEF:
2439 class = mono_class_create_from_typedef (image, type_token);
2441 case MONO_TOKEN_TYPE_REF:
2442 class = mono_class_from_typeref (image, type_token);
2444 case MONO_TOKEN_TYPE_SPEC:
2445 class = mono_class_create_from_typespec (image, type_token, NULL);
2448 g_warning ("unknown token type %x", type_token & 0xff000000);
2449 g_assert_not_reached ();
2453 g_warning ("Could not load class from token 0x%08x in %s", type_token, image->name);
2459 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
2461 MonoClass *class = mono_class_get (image, type_token);
2464 if (!class || !context)
2467 switch (class->byval_arg.type) {
2468 case MONO_TYPE_GENERICINST:
2469 if (!class->generic_inst->is_open)
2473 case MONO_TYPE_MVAR:
2479 inflated = inflate_generic_type (&class->byval_arg, context);
2483 return mono_class_from_mono_type (inflated);
2487 * mono_class_from_name_case:
2488 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
2489 * @name_space: the type namespace
2490 * @name: the type short name.
2492 * Obtains a MonoClass with a given namespace and a given name which
2493 * is located in the given MonoImage. The namespace and name
2494 * lookups are case insensitive.
2496 * You can also pass `NULL' to the image, and that will lookup for
2497 * a type with the given namespace and name in all of the loaded
2498 * assemblies: notice that since there might be a name clash in this
2499 * case, passing NULL is not encouraged if you need a precise type.
2503 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
2505 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
2506 guint32 cols [MONO_TYPEDEF_SIZE];
2511 /* add a cache if needed */
2512 for (i = 1; i <= t->rows; ++i) {
2513 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
2514 /* nested types are accessed from the nesting name */
2515 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
2516 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
2518 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2519 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2520 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
2521 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
2527 return_nested_in (MonoClass *class, char *nested) {
2529 char *s = strchr (nested, '/');
2536 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
2538 if (strcmp (found->name, nested) == 0) {
2540 return return_nested_in (found, s);
2549 * mono_class_from_name_case:
2550 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
2551 * @name_space: the type namespace
2552 * @name: the type short name.
2554 * Obtains a MonoClass with a given namespace and a given name which
2555 * is located in the given MonoImage.
2557 * You can also pass `NULL' to the image, and that will lookup for
2558 * a type with the given namespace and name in all of the loaded
2559 * assemblies: notice that since there might be a name clash in this
2560 * case, passing NULL is not encouraged if you need a precise type.
2564 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
2566 GHashTable *nspace_table;
2567 MonoImage *loaded_image;
2573 if ((nested = strchr (name, '/'))) {
2574 int pos = nested - name;
2575 int len = strlen (name);
2578 memcpy (buf, name, len + 1);
2580 nested = buf + pos + 1;
2584 mono_loader_lock ();
2586 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
2589 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
2591 mono_loader_unlock ();
2596 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
2597 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
2598 guint32 cols [MONO_EXP_TYPE_SIZE];
2601 idx = mono_metadata_token_index (token);
2603 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
2605 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
2606 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
2607 loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
2610 class = mono_class_from_name (loaded_image, name_space, name);
2612 return return_nested_in (class, nested);
2615 g_error ("not yet implemented");
2619 token = MONO_TOKEN_TYPE_DEF | token;
2621 class = mono_class_get (image, token);
2623 return return_nested_in (class, nested);
2628 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
2629 gboolean check_interfaces)
2632 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
2633 if ((klassc->interface_id <= klass->max_interface_id) &&
2634 (klass->interface_offsets [klassc->interface_id] >= 0))
2636 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
2639 for (i = 0; i < klass->interface_count; i ++) {
2640 MonoClass *ic = klass->interfaces [i];
2645 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
2647 if (klass->generic_inst) {
2648 MonoType *parent = klass->generic_inst->parent;
2652 if (mono_metadata_type_equal (parent, &klassc->byval_arg))
2654 klass = mono_class_from_mono_type (parent);
2660 * MS.NET thinks interfaces are a subclass of Object, so we think it as
2663 if (klassc == mono_defaults.object_class)
2670 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
2673 mono_class_init (klass);
2675 if (!oklass->inited)
2676 mono_class_init (oklass);
2678 if (MONO_CLASS_IS_INTERFACE (klass)) {
2679 if ((klass->interface_id <= oklass->max_interface_id) &&
2680 (oklass->interface_offsets [klass->interface_id] != -1))
2684 MonoClass *eclass, *eoclass;
2686 if (oklass->rank != klass->rank)
2689 /* vectors vs. one dimensional arrays */
2690 if (oklass->byval_arg.type != klass->byval_arg.type)
2693 eclass = klass->cast_class;
2694 eoclass = oklass->cast_class;
2698 * a is b does not imply a[] is b[] when a is a valuetype, and
2699 * b is a reference type.
2702 if (eoclass->valuetype) {
2703 if ((eclass == mono_defaults.enum_class) ||
2704 (eclass == mono_defaults.enum_class->parent) ||
2705 (eclass == mono_defaults.object_class))
2709 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
2712 if (klass == mono_defaults.object_class)
2715 return mono_class_has_parent (oklass, klass);
2719 * mono_class_needs_cctor_run:
2721 * Determines whenever the class has a static constructor and whenever it
2722 * needs to be called when executing CALLER.
2725 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
2730 for (i = 0; i < klass->method.count; ++i) {
2731 method = klass->methods [i];
2732 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2733 (strcmp (".cctor", method->name) == 0)) {
2734 if (caller == method)
2743 * Returns the nnumber of bytes an element of type klass
2744 * uses when stored into an array.
2747 mono_class_array_element_size (MonoClass *klass)
2749 int t = klass->byval_arg.type;
2755 case MONO_TYPE_BOOLEAN:
2759 case MONO_TYPE_CHAR:
2768 case MONO_TYPE_CLASS:
2769 case MONO_TYPE_STRING:
2770 case MONO_TYPE_OBJECT:
2771 case MONO_TYPE_SZARRAY:
2772 case MONO_TYPE_ARRAY:
2774 case MONO_TYPE_MVAR:
2775 return sizeof (gpointer);
2780 case MONO_TYPE_VALUETYPE:
2781 if (klass->enumtype) {
2782 t = klass->enum_basetype->type;
2785 return mono_class_instance_size (klass) - sizeof (MonoObject);
2787 g_error ("unknown type 0x%02x in mono_class_array_element_size", t);
2793 * mono_array_element_size:
2794 * @ac: pointer to a #MonoArrayClass
2796 * Returns: the size of single array element.
2799 mono_array_element_size (MonoClass *ac)
2801 return mono_class_array_element_size (ac->element_class);
2805 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
2806 MonoGenericContext *context)
2808 if (image->dynamic) {
2809 gpointer obj = mono_lookup_dynamic_token (image, token);
2811 switch (token & 0xff000000) {
2812 case MONO_TOKEN_TYPE_DEF:
2813 case MONO_TOKEN_TYPE_REF:
2814 case MONO_TOKEN_TYPE_SPEC:
2816 *handle_class = mono_defaults.typehandle_class;
2817 return &((MonoClass*)obj)->byval_arg;
2818 case MONO_TOKEN_METHOD_DEF:
2820 *handle_class = mono_defaults.methodhandle_class;
2822 case MONO_TOKEN_FIELD_DEF:
2824 *handle_class = mono_defaults.fieldhandle_class;
2827 g_assert_not_reached ();
2831 switch (token & 0xff000000) {
2832 case MONO_TOKEN_TYPE_DEF:
2833 case MONO_TOKEN_TYPE_REF: {
2836 *handle_class = mono_defaults.typehandle_class;
2837 class = mono_class_get_full (image, token, context);
2838 mono_class_init (class);
2839 /* We return a MonoType* as handle */
2840 return &class->byval_arg;
2842 case MONO_TOKEN_TYPE_SPEC: {
2845 *handle_class = mono_defaults.typehandle_class;
2846 class = mono_class_create_from_typespec (image, token, context);
2847 mono_class_init (class);
2848 return &class->byval_arg;
2850 case MONO_TOKEN_FIELD_DEF: {
2852 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
2853 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
2854 mono_class_init (class);
2856 *handle_class = mono_defaults.fieldhandle_class;
2857 return mono_class_get_field (class, token);
2859 case MONO_TOKEN_METHOD_DEF: {
2861 meth = mono_get_method_full (image, token, NULL, context);
2863 *handle_class = mono_defaults.methodhandle_class;
2866 case MONO_TOKEN_MEMBER_REF: {
2867 guint32 cols [MONO_MEMBERREF_SIZE];
2869 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
2870 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
2871 mono_metadata_decode_blob_size (sig, &sig);
2872 if (*sig == 0x6) { /* it's a field */
2874 MonoClassField *field;
2875 field = mono_field_from_token (image, token, &klass, context);
2877 *handle_class = mono_defaults.fieldhandle_class;
2881 meth = mono_get_method_full (image, token, NULL, context);
2883 *handle_class = mono_defaults.methodhandle_class;
2888 g_warning ("Unknown token 0x%08x in ldtoken", token);
2895 * This function might need to call runtime functions so it can't be part
2896 * of the metadata library.
2898 static MonoLookupDynamicToken lookup_dynamic = NULL;
2901 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
2903 lookup_dynamic = func;
2907 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
2909 return lookup_dynamic (image, token);
2913 mono_class_get_image (MonoClass *klass)
2915 return klass->image;
2919 * mono_class_get_element_class:
2920 * @klass: the MonoClass to act on
2922 * Returns the element class of an array or an enumeration.
2925 mono_class_get_element_class (MonoClass *klass)
2927 return klass->element_class;
2931 * mono_class_is_valuetype:
2932 * @klass: the MonoClass to act on
2934 * Returns true if the MonoClass represents a ValueType.
2937 mono_class_is_valuetype (MonoClass *klass)
2939 return klass->valuetype;
2943 * mono_class_is_enum:
2944 * @klass: the MonoClass to act on
2946 * Returns true if the MonoClass represents an enumeration.
2949 mono_class_is_enum (MonoClass *klass)
2951 return klass->enumtype;
2955 * mono_class_enum_basetype:
2956 * @klass: the MonoClass to act on
2958 * Returns the underlying type representation for an enumeration.
2961 mono_class_enum_basetype (MonoClass *klass)
2963 return klass->enum_basetype;
2967 * mono_class_get_parent
2968 * @klass: the MonoClass to act on
2970 * Returns the parent class for this class.
2973 mono_class_get_parent (MonoClass *klass)
2975 return klass->parent;
2979 * mono_class_get_nesting_type;
2980 * @klass: the MonoClass to act on
2982 * Returns the container type where this type is nested or NULL if this type is not a nested type.
2985 mono_class_get_nesting_type (MonoClass *klass)
2987 return klass->nested_in;
2991 * mono_class_get_rank:
2992 * @klass: the MonoClass to act on
2994 * Returns the rank for the array (the number of dimensions).
2997 mono_class_get_rank (MonoClass *klass)
3003 * mono_class_get_flags:
3004 * @klass: the MonoClass to act on
3006 * The type flags from the TypeDef table from the metadata.
3007 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
3010 * Returns the flags from the TypeDef table.
3013 mono_class_get_flags (MonoClass *klass)
3015 return klass->flags;
3019 * mono_class_get_name
3020 * @klass: the MonoClass to act on
3022 * Returns the name of the class.
3025 mono_class_get_name (MonoClass *klass)
3031 * mono_class_get_namespace:
3032 * @klass: the MonoClass to act on
3034 * Returns the namespace of the class.
3037 mono_class_get_namespace (MonoClass *klass)
3039 return klass->name_space;
3043 * mono_class_get_type:
3044 * @klass: the MonoClass to act on
3046 * This method returns the internal Type representation for the class.
3048 * Returns the MonoType from the class.
3051 mono_class_get_type (MonoClass *klass)
3053 return &klass->byval_arg;
3057 * mono_class_get_byref_type:
3058 * @klass: the MonoClass to act on
3063 mono_class_get_byref_type (MonoClass *klass)
3065 return &klass->this_arg;
3069 * mono_class_num_fields:
3070 * @klass: the MonoClass to act on
3072 * Returns the number of static and instance fields in the class.
3075 mono_class_num_fields (MonoClass *klass)
3077 return klass->field.count;
3081 * mono_class_num_methods:
3082 * @klass: the MonoClass to act on
3084 * Returns the number of methods in the class.
3087 mono_class_num_methods (MonoClass *klass)
3089 return klass->method.count;
3093 * mono_class_num_properties
3094 * @klass: the MonoClass to act on
3096 * Returns the number of properties in the class.
3099 mono_class_num_properties (MonoClass *klass)
3101 return klass->property.count;
3105 * mono_class_num_events:
3106 * @klass: the MonoClass to act on
3108 * Returns the number of events in the class.
3111 mono_class_num_events (MonoClass *klass)
3113 return klass->event.count;
3117 * mono_class_get_fields:
3118 * @klass: the MonoClass to act on
3120 * This routine is an iterator routine for retrieving the fields in a class.
3122 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3123 * iterate over all of the elements. When no more values are
3124 * available, the return value is NULL.
3126 * Returns a `MonoClassField *' on each iteration, or NULL when no more fields are available.
3129 mono_class_get_fields (MonoClass* klass, gpointer *iter)
3131 MonoClassField* field;
3135 mono_class_init (klass);
3137 /* start from the first */
3138 if (klass->field.count) {
3139 return *iter = &klass->fields [0];
3147 if (field < &klass->fields [klass->field.count]) {
3148 return *iter = field;
3154 * mono_class_get_methods
3155 * @klass: the MonoClass to act on
3157 * This routine is an iterator routine for retrieving the fields in a class.
3159 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3160 * iterate over all of the elements. When no more values are
3161 * available, the return value is NULL.
3163 * Returns a MonoMethod on each iteration or NULL when no more methods are available.
3166 mono_class_get_methods (MonoClass* klass, gpointer *iter)
3168 MonoMethod** method;
3172 mono_class_init (klass);
3174 /* start from the first */
3175 if (klass->method.count) {
3176 *iter = &klass->methods [0];
3177 return klass->methods [0];
3185 if (method < &klass->methods [klass->method.count]) {
3193 * mono_class_get_properties:
3194 * @klass: the MonoClass to act on
3196 * This routine is an iterator routine for retrieving the properties in a class.
3198 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3199 * iterate over all of the elements. When no more values are
3200 * available, the return value is NULL.
3202 * Returns a `MonoProperty *' on each invocation, or NULL when no more are available.
3205 mono_class_get_properties (MonoClass* klass, gpointer *iter)
3207 MonoProperty* property;
3211 mono_class_init (klass);
3213 /* start from the first */
3214 if (klass->property.count) {
3215 return *iter = &klass->properties [0];
3223 if (property < &klass->properties [klass->property.count]) {
3224 return *iter = property;
3230 * mono_class_get_events:
3231 * @klass: the MonoClass to act on
3233 * This routine is an iterator routine for retrieving the properties in a class.
3235 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3236 * iterate over all of the elements. When no more values are
3237 * available, the return value is NULL.
3239 * Returns a `MonoEvent *' on each invocation, or NULL when no more are available.
3242 mono_class_get_events (MonoClass* klass, gpointer *iter)
3248 mono_class_init (klass);
3250 /* start from the first */
3251 if (klass->event.count) {
3252 return *iter = &klass->events [0];
3260 if (event < &klass->events [klass->event.count]) {
3261 return *iter = event;
3267 * mono_class_get_interfaaces
3268 * @klass: the MonoClass to act on
3270 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
3272 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3273 * iterate over all of the elements. When no more values are
3274 * available, the return value is NULL.
3276 * Returns a `Monoclass *' on each invocation, or NULL when no more are available.
3279 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
3285 mono_class_init (klass);
3287 /* start from the first */
3288 if (klass->interface_count) {
3289 *iter = &klass->interfaces [0];
3290 return klass->interfaces [0];
3298 if (iface < &klass->interfaces [klass->interface_count]) {
3306 * mono_class_get_interfaaces
3307 * @klass: the MonoClass to act on
3309 * This routine is an iterator routine for retrieving the nested types of a class.
3311 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3312 * iterate over all of the elements. When no more values are
3313 * available, the return value is NULL.
3315 * Returns a `Monoclass *' on each invocation, or NULL when no more are available.
3318 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
3324 mono_class_init (klass);
3326 /* start from the first */
3327 if (klass->nested_classes) {
3328 *iter = klass->nested_classes;
3329 return klass->nested_classes->data;
3331 /* no nested types */
3345 * mono_field_get_name:
3346 * @field: the MonoClassField to act on
3348 * Returns the name of the field.
3351 mono_field_get_name (MonoClassField *field)
3357 * mono_field_get_type:
3358 * @field: the MonoClassField to act on
3360 * Returns MonoType of the field.
3363 mono_field_get_type (MonoClassField *field)
3369 * mono_field_get_type:
3370 * @field: the MonoClassField to act on
3372 * Returns MonoClass where the field was defined.
3375 mono_field_get_parent (MonoClassField *field)
3377 return field->parent;
3381 * mono_field_get_flags;
3382 * @field: the MonoClassField to act on
3384 * The metadata flags for a field are encoded using the
3385 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
3387 * Returns the flags for the field.
3390 mono_field_get_flags (MonoClassField *field)
3392 return field->type->attrs;
3396 * mono_property_get_name:
3397 * @prop: the MonoProperty to act on
3399 * Returns the name of the property
3402 mono_property_get_name (MonoProperty *prop)
3408 * mono_property_get_set_method
3409 * @prop: the MonoProperty to act on.
3411 * Returns the setter method of the property (A MonoMethod)
3414 mono_property_get_set_method (MonoProperty *prop)
3420 * mono_property_get_get_method
3421 * @prop: the MonoProperty to act on.
3423 * Returns the setter method of the property (A MonoMethod)
3426 mono_property_get_get_method (MonoProperty *prop)
3432 * mono_property_get_parent:
3433 * @prop: the MonoProperty to act on.
3435 * Returns the MonoClass where the property was defined.
3438 mono_property_get_parent (MonoProperty *prop)
3440 return prop->parent;
3444 * mono_property_get_flags:
3445 * @prop: the MonoProperty to act on.
3447 * The metadata flags for a property are encoded using the
3448 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
3450 * Returns the flags for the property.
3453 mono_property_get_flags (MonoProperty *prop)
3459 * mono_event_get_name:
3460 * @event: the MonoEvent to act on
3462 * Returns the name of the event.
3465 mono_event_get_name (MonoEvent *event)
3471 * mono_event_get_add_method:
3472 * @event: The MonoEvent to act on.
3474 * Returns the `add' method for the event (a MonoMethod).
3477 mono_event_get_add_method (MonoEvent *event)
3483 * mono_event_get_remove_method:
3484 * @event: The MonoEvent to act on.
3486 * Returns the `remove' method for the event (a MonoMethod).
3489 mono_event_get_remove_method (MonoEvent *event)
3491 return event->remove;
3495 * mono_event_get_raise_method:
3496 * @event: The MonoEvent to act on.
3498 * Returns the `raise' method for the event (a MonoMethod).
3501 mono_event_get_raise_method (MonoEvent *event)
3503 return event->raise;
3507 * mono_event_get_parent:
3508 * @event: the MonoEvent to act on.
3510 * Returns the MonoClass where the event is defined.
3513 mono_event_get_parent (MonoEvent *event)
3515 return event->parent;
3519 * mono_event_get_flags
3520 * @event: the MonoEvent to act on.
3522 * The metadata flags for an event are encoded using the
3523 * EVENT_* constants. See the tabledefs.h file for details.
3525 * Returns the flags for the event.
3528 mono_event_get_flags (MonoEvent *event)
3530 return event->attrs;