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_class_init_func) (MonoClass *klass) = NULL;
46 mono_class_from_typeref (MonoImage *image, guint32 type_token)
48 guint32 cols [MONO_TYPEREF_SIZE];
49 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
51 const char *name, *nspace;
53 MonoAssembly **references;
55 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
57 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
58 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
60 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
61 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
62 case MONO_RESOLTION_SCOPE_MODULE:
64 g_error ("null ResolutionScope not yet handled");
65 /* a typedef in disguise */
66 return mono_class_from_name (image, nspace, name);
67 case MONO_RESOLTION_SCOPE_MODULEREF:
68 return mono_class_from_name (image->modules [idx - 1], nspace, name);
69 case MONO_RESOLTION_SCOPE_TYPEREF: {
70 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
72 mono_class_init (enclosing);
73 for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
75 if (strcmp (res->name, name) == 0)
78 g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
81 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
85 references = image->references;
86 if (!references || !references [idx-1]) {
88 * detected a reference to mscorlib, we simply return a reference to a dummy
89 * until we have a better solution.
91 * once a better solution is in place, the System.MonoDummy
92 * class should be removed from CVS.
94 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]));
96 res = mono_class_from_name (image, "System", "MonoDummy");
97 /* prevent method loading */
99 /* some storage if the type is used - very ugly hack */
100 res->instance_size = 2*sizeof (gpointer);
104 /* load referenced assembly */
105 image = references [idx-1]->image;
107 return mono_class_from_name (image, nspace, name);
111 dup_type (MonoType* t, const MonoType *original)
113 MonoType *r = g_new0 (MonoType, 1);
115 r->attrs = original->attrs;
116 mono_stats.generics_metadata_size += sizeof (MonoType);
121 mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed)
125 switch (type->type) {
126 case MONO_TYPE_ARRAY: {
127 int i, rank = type->data.array->rank;
129 mono_type_get_name_recurse (&type->data.array->eklass->byval_arg, str, FALSE);
130 g_string_append_c (str, '[');
131 for (i = 1; i < rank; i++)
132 g_string_append_c (str, ',');
133 g_string_append_c (str, ']');
136 case MONO_TYPE_SZARRAY:
137 mono_type_get_name_recurse (&type->data.klass->byval_arg, str, FALSE);
138 g_string_append (str, "[]");
141 mono_type_get_name_recurse (type->data.type, str, FALSE);
142 g_string_append_c (str, '*');
145 klass = mono_class_from_mono_type (type);
146 if (klass->nested_in) {
147 mono_type_get_name_recurse (&klass->nested_in->byval_arg, str, TRUE);
148 g_string_append_c (str, '+');
150 if (*klass->name_space) {
151 g_string_append (str, klass->name_space);
152 g_string_append_c (str, '.');
154 g_string_append (str, klass->name);
157 if (klass->generic_inst) {
158 MonoGenericInst *ginst = klass->generic_inst;
161 g_string_append_c (str, '[');
162 for (i = 0; i < ginst->type_argc; i++) {
164 g_string_append_c (str, ',');
165 mono_type_get_name_recurse (ginst->type_argv [i], str, FALSE);
167 g_string_append_c (str, ']');
168 } else if (klass->gen_params) {
171 g_string_append_c (str, '[');
172 for (i = 0; i < klass->num_gen_params; i++) {
174 g_string_append_c (str, ',');
175 g_string_append (str, klass->gen_params [i].name);
177 g_string_append_c (str, ']');
184 * mono_type_get_name:
187 * Returns the string representation for type as required by System.Reflection.
188 * The inverse of mono_reflection_parse_type ().
191 mono_type_get_name (MonoType *type)
193 GString* result = g_string_new ("");
194 mono_type_get_name_recurse (type, result, FALSE);
197 g_string_append_c (result, '&');
199 return g_string_free (result, FALSE);
203 mono_class_is_open_constructed_type (MonoType *t)
209 case MONO_TYPE_SZARRAY:
210 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
211 case MONO_TYPE_ARRAY:
212 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
214 return mono_class_is_open_constructed_type (t->data.type);
215 case MONO_TYPE_GENERICINST: {
216 MonoGenericInst *ginst = t->data.generic_inst;
219 if (mono_class_is_open_constructed_type (ginst->generic_type))
221 for (i = 0; i < ginst->type_argc; i++)
222 if (mono_class_is_open_constructed_type (ginst->type_argv [i]))
232 inflate_generic_type (MonoType *type, MonoGenericContext *context)
234 switch (type->type) {
236 if (context->gmethod && context->gmethod->mtype_argv)
238 context->gmethod->mtype_argv [type->data.generic_param->num],
245 context->ginst->type_argv [type->data.generic_param->num],
249 case MONO_TYPE_SZARRAY: {
250 MonoClass *eclass = type->data.klass;
251 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
254 nt = dup_type (type, type);
255 nt->data.klass = mono_class_from_mono_type (inflated);
258 case MONO_TYPE_GENERICINST: {
259 MonoGenericInst *oginst = type->data.generic_inst;
260 MonoGenericInst *nginst;
264 nginst = g_new0 (MonoGenericInst, 1);
267 nginst->is_open = FALSE;
269 nginst->type_argv = g_new0 (MonoType *, oginst->type_argc);
271 for (i = 0; i < oginst->type_argc; i++) {
272 MonoType *t = oginst->type_argv [i];
273 nginst->type_argv [i] = mono_class_inflate_generic_type (t, context);
275 if (!nginst->is_open)
276 nginst->is_open = mono_class_is_open_constructed_type (nginst->type_argv [i]);
279 nginst->klass = NULL;
281 nginst->context = g_new0 (MonoGenericContext, 1);
282 nginst->context->ginst = nginst;
285 nt = g_hash_table_lookup (oginst->klass->image->generic_inst_cache, nginst);
288 g_free (nginst->type_argv);
290 mono_loader_unlock ();
294 nginst->dynamic_info = NULL;
295 nginst->initialized = FALSE;
297 mono_class_create_generic (nginst);
299 mono_stats.generic_instance_count++;
300 mono_stats.generics_metadata_size += sizeof (MonoGenericInst) +
301 sizeof (MonoGenericContext) +
302 nginst->type_argc * sizeof (MonoType);
304 nt = dup_type (type, type);
305 nt->data.generic_inst = nginst;
306 g_hash_table_insert (oginst->klass->image->generic_inst_cache, nginst, nt);
307 mono_loader_unlock ();
317 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
319 MonoType *inflated = inflate_generic_type (type, context);
324 mono_stats.inflated_type_count++;
328 static MonoMethodSignature*
329 inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig,
330 MonoGenericContext *context)
332 MonoMethodSignature *res;
334 res = mono_metadata_signature_alloc (image, sig->param_count);
335 res->ret = mono_class_inflate_generic_type (sig->ret, context);
336 for (i = 0; i < sig->param_count; ++i)
337 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
338 res->hasthis = sig->hasthis;
339 res->explicit_this = sig->explicit_this;
340 res->call_convention = sig->call_convention;
341 res->generic_param_count = sig->generic_param_count;
342 res->is_inflated = 1;
346 static MonoMethodHeader*
347 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
349 MonoMethodHeader *res;
351 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
352 res->code = header->code;
353 res->code_size = header->code_size;
354 res->max_stack = header->max_stack;
355 res->num_clauses = header->num_clauses;
356 res->init_locals = header->init_locals;
357 res->num_locals = header->num_locals;
358 res->clauses = header->clauses;
359 res->gen_params = header->gen_params;
360 for (i = 0; i < header->num_locals; ++i)
361 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
366 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context,
369 MonoMethodInflated *result;
371 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
372 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
375 mono_stats.inflated_method_count++;
376 mono_stats.generics_metadata_size +=
377 sizeof (MonoMethodInflated) - sizeof (MonoMethodNormal);
379 result = g_new0 (MonoMethodInflated, 1);
380 result->nmethod = *(MonoMethodNormal*)method;
382 if (result->nmethod.header)
383 result->nmethod.header = inflate_generic_header (
384 result->nmethod.header, context);
387 result->nmethod.method.klass = klass;
389 MonoType *declaring = mono_class_inflate_generic_type (
390 &method->klass->byval_arg, context);
391 result->nmethod.method.klass = mono_class_from_mono_type (declaring);
394 result->nmethod.method.signature = inflate_generic_signature (
395 method->klass->image, method->signature, context);
397 if (context->gmethod) {
398 result->context = g_new0 (MonoGenericContext, 1);
399 result->context->gmethod = context->gmethod;
400 result->context->ginst = result->nmethod.method.klass->generic_inst;
402 mono_stats.generics_metadata_size += sizeof (MonoGenericContext);
404 result->context = result->nmethod.method.klass->generic_inst->context;
406 if (method->signature->is_inflated)
407 result->declaring = ((MonoMethodInflated *) method)->declaring;
409 result->declaring = method;
411 return (MonoMethod *) result;
415 * class_compute_field_layout:
416 * @m: pointer to the metadata.
417 * @class: The class to initialize
419 * Initializes the class->fields.
421 * Currently we only support AUTO_LAYOUT, and do not even try to do
422 * a good job at it. This is temporary to get the code for Paolo.
425 class_compute_field_layout (MonoClass *class)
427 MonoImage *m = class->image;
428 const int top = class->field.count;
429 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
430 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
431 int i, blittable = TRUE, real_size = 0;
433 guint32 packing_size = 0;
434 gboolean explicit_size;
435 MonoClassField *field;
437 if (class->size_inited)
441 if (!class->parent->size_inited)
442 class_compute_field_layout (class->parent);
443 class->instance_size += class->parent->instance_size;
444 class->min_align = class->parent->min_align;
445 blittable = class->parent->blittable;
447 class->instance_size = sizeof (MonoObject);
448 class->min_align = 1;
451 /* Get the real size */
452 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
455 g_assert ((packing_size & 0xfffffff0) == 0);
456 class->packing_size = packing_size;
457 real_size += class->instance_size;
461 if (explicit_size && real_size) {
462 class->instance_size = MAX (real_size, class->instance_size);
464 class->size_inited = 1;
468 class->fields = g_new0 (MonoClassField, top);
471 * Fetch all the field information.
473 for (i = 0; i < top; i++){
475 guint32 cols [MONO_FIELD_SIZE];
476 int idx = class->field.first + i;
478 field = &class->fields [i];
479 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
480 /* The name is needed for fieldrefs */
481 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
482 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
483 mono_metadata_decode_value (sig, &sig);
484 /* FIELD signature == 0x06 */
485 g_assert (*sig == 0x06);
486 field->type = mono_metadata_parse_field_type (
487 m, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
488 if (mono_field_is_deleted (field))
490 if (class->generic_inst) {
491 field->type = mono_class_inflate_generic_type (
492 field->type, class->generic_inst->context);
493 field->type->attrs = cols [MONO_FIELD_FLAGS];
496 field->parent = class;
498 /* Only do these checks if we still think this type is blittable */
499 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
500 if (field->type->byref) {
503 MonoClass *field_class = mono_class_from_mono_type (field->type);
504 if (!field_class || !field_class->blittable)
509 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
510 mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
511 if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
512 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, field->name);
515 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
516 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
518 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
519 field->data = mono_cli_rva_map (class->image->image_info, rva);
522 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
523 class->enum_basetype = field->type;
524 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
525 blittable = class->element_class->blittable;
528 /* The def_value of fields is compute lazily during vtable creation */
531 if (class == mono_defaults.string_class)
534 class->blittable = blittable;
536 if (class->enumtype && !class->enum_basetype) {
537 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
540 if (explicit_size && real_size) {
541 class->instance_size = MAX (real_size, class->instance_size);
544 if (class->gen_params)
547 mono_class_layout_fields (class);
551 mono_class_layout_fields (MonoClass *class)
554 const int top = class->field.count;
555 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
556 guint32 pass, passes, real_size;
557 gboolean gc_aware_layout = FALSE;
558 MonoClassField *field;
561 * Enable GC aware auto layout: in this mode, reference
562 * fields are grouped together inside objects, increasing collector
564 * Requires that all classes whose layout is known to native code be annotated
565 * with [StructLayout (LayoutKind.Sequential)]
566 * Value types have gc_aware_layout disabled by default, as per
567 * what the default is for other runtimes.
569 /* corlib is missing [StructLayout] directives in many places */
570 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
571 if (class->image != mono_defaults.corlib &&
572 class->byval_arg.type != MONO_TYPE_VALUETYPE)
573 gc_aware_layout = TRUE;
577 * Compute field layout and total size (not considering static fields)
581 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
582 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
589 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
593 real_size = class->parent->instance_size;
595 real_size = sizeof (MonoObject);
597 for (pass = 0; pass < passes; ++pass) {
598 for (i = 0; i < top; i++){
600 field = &class->fields [i];
602 if (mono_field_is_deleted (field))
604 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
607 if (gc_aware_layout) {
609 * We process fields with reference type in the first pass,
610 * and fields with non-reference type in the second pass.
611 * We use IS_POINTER instead of IS_REFERENCE because in
612 * some internal structures, we store GC_MALLOCed memory
613 * in IntPtr fields...
615 if (MONO_TYPE_IS_POINTER (field->type)) {
624 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
625 (strcmp (field->name, "$PRIVATE$") == 0)) {
626 /* This field is a hack inserted by MCS to empty structures */
630 size = mono_type_size (field->type, &align);
632 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
633 align = class->packing_size ? MIN (class->packing_size, align): align;
634 class->min_align = MAX (align, class->min_align);
635 field->offset = real_size;
636 field->offset += align - 1;
637 field->offset &= ~(align - 1);
638 real_size = field->offset + size;
641 class->instance_size = MAX (real_size, class->instance_size);
643 if (class->instance_size & (class->min_align - 1)) {
644 class->instance_size += class->min_align - 1;
645 class->instance_size &= ~(class->min_align - 1);
649 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
651 for (i = 0; i < top; i++) {
653 field = &class->fields [i];
656 * There must be info about all the fields in a type if it
657 * uses explicit layout.
660 if (mono_field_is_deleted (field))
662 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
665 size = mono_type_size (field->type, &align);
668 * When we get here, field->offset is already set by the
669 * loader (for either runtime fields or fields loaded from metadata).
670 * The offset is from the start of the object: this works for both
671 * classes and valuetypes.
673 field->offset += sizeof (MonoObject);
678 real_size = MAX (real_size, size + field->offset);
680 class->instance_size = MAX (real_size, class->instance_size);
684 class->size_inited = 1;
687 * Compute static field layout and size
689 for (i = 0; i < top; i++){
691 field = &class->fields [i];
693 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
695 if (mono_field_is_deleted (field))
698 size = mono_type_size (field->type, &align);
699 field->offset = class->class_size;
700 field->offset += align - 1;
701 field->offset &= ~(align - 1);
702 class->class_size = field->offset + size;
707 init_properties (MonoClass *class)
709 guint startm, endm, i, j;
710 guint32 cols [MONO_PROPERTY_SIZE];
711 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
712 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
714 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
715 class->property.count = class->property.last - class->property.first;
717 class->properties = g_new0 (MonoProperty, class->property.count);
718 for (i = class->property.first; i < class->property.last; ++i) {
719 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
720 class->properties [i - class->property.first].parent = class;
721 class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
722 class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
724 startm = mono_metadata_methods_from_property (class->image, i, &endm);
725 for (j = startm; j < endm; ++j) {
726 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
727 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
728 case METHOD_SEMANTIC_SETTER:
729 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
731 case METHOD_SEMANTIC_GETTER:
732 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
742 init_events (MonoClass *class)
744 guint startm, endm, i, j;
745 guint32 cols [MONO_EVENT_SIZE];
746 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
747 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
749 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
750 class->event.count = class->event.last - class->event.first;
752 class->events = g_new0 (MonoEvent, class->event.count);
753 for (i = class->event.first; i < class->event.last; ++i) {
754 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
755 class->events [i - class->event.first].parent = class;
756 class->events [i - class->event.first].attrs = cols [MONO_EVENT_FLAGS];
757 class->events [i - class->event.first].name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
759 startm = mono_metadata_methods_from_event (class->image, i, &endm);
760 for (j = startm; j < endm; ++j) {
761 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
762 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
763 case METHOD_SEMANTIC_ADD_ON:
764 class->events [i - class->event.first].add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
766 case METHOD_SEMANTIC_REMOVE_ON:
767 class->events [i - class->event.first].remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
769 case METHOD_SEMANTIC_FIRE:
770 class->events [i - class->event.first].raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
772 case METHOD_SEMANTIC_OTHER: /* don't care for now */
781 mono_get_unique_iid (MonoClass *class)
783 static GHashTable *iid_hash = NULL;
784 static guint iid = 0;
789 g_assert (MONO_CLASS_IS_INTERFACE (class));
794 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
796 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
798 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
799 mono_loader_unlock ();
803 g_hash_table_insert (iid_hash, str, (gpointer)iid);
807 mono_loader_unlock ();
813 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
818 for (i = 0; i < klass->interface_count; i++) {
819 ic = klass->interfaces [i];
822 *res = g_ptr_array_new ();
823 g_ptr_array_add (*res, ic);
825 collect_implemented_interfaces_aux (ic, res);
829 static inline GPtrArray*
830 collect_implemented_interfaces (MonoClass *klass)
832 GPtrArray *res = NULL;
834 collect_implemented_interfaces_aux (klass, &res);
839 setup_interface_offsets (MonoClass *class, int cur_slot)
845 /* compute maximum number of slots and maximum interface id */
847 for (k = class; k ; k = k->parent) {
848 for (i = 0; i < k->interface_count; i++) {
849 ic = k->interfaces [i];
852 mono_class_init (ic);
854 if (max_iid < ic->interface_id)
855 max_iid = ic->interface_id;
859 if (MONO_CLASS_IS_INTERFACE (class)) {
860 if (max_iid < class->interface_id)
861 max_iid = class->interface_id;
863 class->max_interface_id = max_iid;
864 /* compute vtable offset for interfaces */
865 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
867 for (i = 0; i <= max_iid; i++)
868 class->interface_offsets [i] = -1;
870 ifaces = collect_implemented_interfaces (class);
872 for (i = 0; i < ifaces->len; ++i) {
873 ic = g_ptr_array_index (ifaces, i);
874 class->interface_offsets [ic->interface_id] = cur_slot;
875 cur_slot += ic->method.count;
877 g_ptr_array_free (ifaces, TRUE);
880 for (k = class->parent; k ; k = k->parent) {
881 ifaces = collect_implemented_interfaces (k);
883 for (i = 0; i < ifaces->len; ++i) {
884 ic = g_ptr_array_index (ifaces, i);
886 if (class->interface_offsets [ic->interface_id] == -1) {
887 int io = k->interface_offsets [ic->interface_id];
891 class->interface_offsets [ic->interface_id] = io;
894 g_ptr_array_free (ifaces, TRUE);
898 if (MONO_CLASS_IS_INTERFACE (class))
899 class->interface_offsets [class->interface_id] = cur_slot;
905 mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
909 int i, max_vtsize = 0, max_iid, cur_slot = 0;
911 MonoGHashTable *override_map = NULL;
913 /* setup_vtable() must be called only once on the type */
914 if (class->interface_offsets) {
915 g_warning ("vtable already computed in %s.%s", class->name_space, class->name);
919 ifaces = collect_implemented_interfaces (class);
921 for (i = 0; i < ifaces->len; i++) {
922 MonoClass *ic = g_ptr_array_index (ifaces, i);
923 max_vtsize += ic->method.count;
925 g_ptr_array_free (ifaces, TRUE);
929 max_vtsize += class->parent->vtable_size;
930 cur_slot = class->parent->vtable_size;
933 max_vtsize += class->method.count;
935 vtable = alloca (sizeof (gpointer) * max_vtsize);
936 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
938 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
940 cur_slot = setup_interface_offsets (class, cur_slot);
941 max_iid = class->max_interface_id;
943 if (class->parent && class->parent->vtable_size)
944 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
946 /* override interface methods */
947 for (i = 0; i < onum; i++) {
948 MonoMethod *decl = overrides [i*2];
949 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
951 g_assert (decl->slot != -1);
952 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
953 vtable [dslot] = overrides [i*2 + 1];
954 vtable [dslot]->slot = dslot;
956 override_map = mono_g_hash_table_new (NULL, NULL);
958 mono_g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
962 for (k = class; k ; k = k->parent) {
964 ifaces = collect_implemented_interfaces (k);
966 nifaces = ifaces->len;
967 for (i = 0; i < nifaces; i++) {
970 ic = g_ptr_array_index (ifaces, i);
971 io = k->interface_offsets [ic->interface_id];
974 g_assert (io <= max_vtsize);
977 for (l = 0; l < ic->method.count; l++) {
978 MonoMethod *im = ic->methods [l];
980 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
983 for (j = 0; j < class->method.count; ++j) {
984 MonoMethod *cm = class->methods [j];
985 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
986 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
987 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
989 if (!strcmp(cm->name, im->name) &&
990 mono_metadata_signature_equal (cm->signature, im->signature)) {
991 g_assert (io + l <= max_vtsize);
992 vtable [io + l] = cm;
997 /* already implemented */
998 if (io >= k->vtable_size)
1002 for (l = 0; l < ic->method.count; l++) {
1003 MonoMethod *im = ic->methods [l];
1006 g_assert (io + l <= max_vtsize);
1008 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1011 for (k1 = class; k1; k1 = k1->parent) {
1012 for (j = 0; j < k1->method.count; ++j) {
1013 MonoMethod *cm = k1->methods [j];
1015 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1016 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
1019 if (!strcmp(cm->name, im->name) &&
1020 mono_metadata_signature_equal (cm->signature, im->signature)) {
1021 g_assert (io + l <= max_vtsize);
1022 vtable [io + l] = cm;
1027 g_assert (io + l <= max_vtsize);
1028 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1033 for (l = 0; l < ic->method.count; l++) {
1034 MonoMethod *im = ic->methods [l];
1035 char *qname, *fqname;
1038 if (vtable [io + l])
1041 qname = g_strconcat (ic->name, ".", im->name, NULL);
1042 if (ic->name_space && ic->name_space [0])
1043 fqname = g_strconcat (ic->name_space, ".", ic->name, ".", im->name, NULL);
1047 for (k1 = class; k1; k1 = k1->parent) {
1048 for (j = 0; j < k1->method.count; ++j) {
1049 MonoMethod *cm = k1->methods [j];
1051 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1054 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
1055 mono_metadata_signature_equal (cm->signature, im->signature)) {
1056 g_assert (io + l <= max_vtsize);
1057 vtable [io + l] = cm;
1067 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
1068 for (l = 0; l < ic->method.count; l++) {
1070 MonoMethod *im = ic->methods [l];
1071 if (im->flags & METHOD_ATTRIBUTE_STATIC)
1073 g_assert (io + l <= max_vtsize);
1076 * If one of our parents already implements this interface
1077 * we can inherit the implementation.
1079 if (!(vtable [io + l])) {
1080 MonoClass *parent = class->parent;
1082 if ((ic->interface_id <= parent->max_interface_id) &&
1083 (parent->interface_offsets [ic->interface_id]) &&
1085 vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
1088 if (!(vtable [io + l])) {
1089 for (j = 0; j < onum; ++j) {
1090 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
1091 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
1093 msig = mono_signature_get_desc (im->signature, FALSE);
1094 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
1095 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
1097 for (j = 0; j < class->method.count; ++j) {
1098 MonoMethod *cm = class->methods [j];
1099 msig = mono_signature_get_desc (cm->signature, FALSE);
1101 printf ("METHOD %s(%s)\n", cm->name, msig);
1104 g_assert_not_reached ();
1109 for (l = 0; l < ic->method.count; l++) {
1110 MonoMethod *im = vtable [io + l];
1113 g_assert (io + l <= max_vtsize);
1115 /* FIXME: why do we need this ? */
1117 /* g_assert_not_reached (); */
1123 g_ptr_array_free (ifaces, TRUE);
1126 for (i = 0; i < class->method.count; ++i) {
1129 cm = class->methods [i];
1132 * Non-virtual method have no place in the vtable.
1133 * This also catches static methods (since they are not virtual).
1135 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1139 * If the method is REUSE_SLOT, we must check in the
1140 * base class for a method to override.
1142 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
1144 for (k = class->parent; k ; k = k->parent) {
1146 for (j = 0; j < k->method.count; ++j) {
1147 MonoMethod *m1 = k->methods [j];
1148 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
1150 if (!strcmp(cm->name, m1->name) &&
1151 mono_metadata_signature_equal (cm->signature, m1->signature)) {
1152 slot = k->methods [j]->slot;
1153 g_assert (cm->slot < max_vtsize);
1155 override_map = mono_g_hash_table_new (NULL, NULL);
1156 mono_g_hash_table_insert (override_map, m1, cm);
1168 cm->slot = cur_slot++;
1170 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT) && !cm->signature->generic_param_count)
1171 vtable [cm->slot] = cm;
1174 /* override non interface methods */
1175 for (i = 0; i < onum; i++) {
1176 MonoMethod *decl = overrides [i*2];
1177 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
1178 g_assert (decl->slot != -1);
1179 vtable [decl->slot] = overrides [i*2 + 1];
1180 overrides [i * 2 + 1]->slot = decl->slot;
1182 override_map = mono_g_hash_table_new (NULL, NULL);
1183 mono_g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
1188 * If a method occupies more than one place in the vtable, and it is
1189 * overriden, then change the other occurances too.
1192 for (i = 0; i < max_vtsize; ++i)
1194 MonoMethod *cm = mono_g_hash_table_lookup (override_map, vtable [i]);
1199 mono_g_hash_table_destroy (override_map);
1202 if (class->generic_inst) {
1203 MonoClass *gklass = mono_class_from_mono_type (class->generic_inst->generic_type);
1205 mono_class_init (gklass);
1206 class->vtable_size = gklass->vtable_size;
1208 class->vtable_size = cur_slot;
1210 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
1211 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
1213 if (mono_print_vtable) {
1216 for (i = 0; i <= max_iid; i++)
1217 if (class->interface_offsets [i] != -1)
1220 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
1221 class->name, class->vtable_size, icount);
1223 for (i = 0; i < class->vtable_size; ++i) {
1228 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
1229 cm->klass->name_space, cm->klass->name,
1236 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
1237 class->name, max_iid);
1239 for (i = 0; i < class->interface_count; i++) {
1240 ic = class->interfaces [i];
1241 printf (" slot %03d(%03d) %s.%s\n",
1242 class->interface_offsets [ic->interface_id],
1243 ic->method.count, ic->name_space, ic->name);
1246 for (k = class->parent; k ; k = k->parent) {
1247 for (i = 0; i < k->interface_count; i++) {
1248 ic = k->interfaces [i];
1249 printf (" slot %03d(%03d) %s.%s\n",
1250 class->interface_offsets [ic->interface_id],
1251 ic->method.count, ic->name_space, ic->name);
1260 * @class: the class to initialize
1262 * compute the instance_size, class_size and other infos that cannot be
1263 * computed at mono_class_get() time. Also compute a generic vtable and
1264 * the method slot numbers. We use this infos later to create a domain
1268 mono_class_init (MonoClass *class)
1271 static MonoMethod *default_ghc = NULL;
1272 static MonoMethod *default_finalize = NULL;
1273 static int finalize_slot = -1;
1274 static int ghc_slot = -1;
1275 MonoMethod **overrides;
1283 /*g_print ("Init class %s\n", class->name);*/
1285 /* We do everything inside the lock to prevent races */
1286 mono_loader_lock ();
1288 if (class->inited) {
1289 mono_loader_unlock ();
1290 /* Somebody might have gotten in before us */
1294 if (class->init_pending) {
1295 /* this indicates a cyclic dependency */
1296 g_error ("pending init %s.%s\n", class->name_space, class->name);
1299 class->init_pending = 1;
1301 mono_stats.initialized_class_count++;
1303 if (class->generic_inst && !class->generic_inst->is_dynamic) {
1304 MonoGenericInst *ginst = class->generic_inst;
1308 gklass = mono_class_from_mono_type (ginst->generic_type);
1309 mono_class_init (gklass);
1312 class->parent = mono_class_from_mono_type (ginst->parent);
1314 class->parent = gklass->parent;
1316 mono_class_setup_parent (class, class->parent);
1318 if (MONO_CLASS_IS_INTERFACE (class))
1319 class->interface_id = mono_get_unique_iid (class);
1321 class->method = gklass->method;
1322 class->methods = g_new0 (MonoMethod *, class->method.count);
1324 for (i = 0; i < class->method.count; i++)
1325 class->methods [i] = mono_class_inflate_generic_method (
1326 gklass->methods [i], ginst->context, ginst->klass);
1328 class->field = gklass->field;
1329 class->fields = g_new0 (MonoClassField, class->field.count);
1331 for (i = 0; i < class->field.count; i++) {
1332 MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
1333 ifield->generic_type = gklass->fields [i].type;
1335 class->fields [i] = gklass->fields [i];
1336 class->fields [i].generic_info = ifield;
1337 class->fields [i].parent = class;
1338 class->fields [i].type = mono_class_inflate_generic_type (
1339 class->fields [i].type, ginst->context);
1342 class->property = gklass->property;
1343 class->properties = g_new0 (MonoProperty, class->property.count);
1345 for (i = 0; i < class->property.count; i++) {
1346 MonoProperty *prop = &class->properties [i];
1348 *prop = gklass->properties [i];
1351 prop->get = mono_class_inflate_generic_method (
1352 prop->get, ginst->context, ginst->klass);
1354 prop->set = mono_class_inflate_generic_method (
1355 prop->set, ginst->context, ginst->klass);
1357 prop->parent = class;
1360 class->interface_count = gklass->interface_count;
1361 class->interfaces = g_new0 (MonoClass *, class->interface_count);
1362 for (i = 0; i < class->interface_count; i++) {
1363 MonoType *it = &gklass->interfaces [i]->byval_arg;
1364 MonoType *inflated = mono_class_inflate_generic_type (
1365 it, ginst->context);
1366 class->interfaces [i] = mono_class_from_mono_type (inflated);
1367 mono_class_init (class->interfaces [i]);
1370 for (list = gklass->nested_classes; list; list = list->next)
1371 class->nested_classes = g_list_append (
1372 class->nested_classes, list->data);
1375 if (class->parent && !class->parent->inited)
1376 mono_class_init (class->parent);
1379 * Computes the size used by the fields, and their locations
1381 if (!class->size_inited)
1382 class_compute_field_layout (class);
1384 /* initialize method pointers */
1387 MonoMethodSignature *sig;
1388 class->method.count = class->rank > 1? 2: 1;
1389 sig = mono_metadata_signature_alloc (class->image, class->rank);
1390 sig->ret = &mono_defaults.void_class->byval_arg;
1391 sig->pinvoke = TRUE;
1392 for (i = 0; i < class->rank; ++i)
1393 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1395 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1396 ctor->klass = class;
1397 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1398 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1399 ctor->signature = sig;
1400 ctor->name = ".ctor";
1402 class->methods = g_new (MonoMethod*, class->method.count);
1403 class->methods [0] = ctor;
1404 if (class->rank > 1) {
1405 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
1406 sig->ret = &mono_defaults.void_class->byval_arg;
1407 sig->pinvoke = TRUE;
1408 for (i = 0; i < class->rank * 2; ++i)
1409 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1411 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1412 ctor->klass = class;
1413 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1414 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1415 ctor->signature = sig;
1416 ctor->name = ".ctor";
1418 class->methods [1] = ctor;
1421 if (!class->generic_inst && !class->methods) {
1422 class->methods = g_new (MonoMethod*, class->method.count);
1423 for (i = 0; i < class->method.count; ++i) {
1424 class->methods [i] = mono_get_method (class->image,
1425 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
1430 if (!class->generic_inst) {
1431 init_properties (class);
1432 init_events (class);
1434 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
1437 guint32 cols [MONO_NESTED_CLASS_SIZE];
1438 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
1439 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
1440 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
1442 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
1446 mono_class_setup_supertypes (class);
1448 if (MONO_CLASS_IS_INTERFACE (class)) {
1449 for (i = 0; i < class->method.count; ++i)
1450 class->methods [i]->slot = i;
1451 class->init_pending = 0;
1454 * class->interface_offsets is needed for the castclass/isinst code, so
1455 * we have to setup them for interfaces, too.
1457 setup_interface_offsets (class, 0);
1458 mono_loader_unlock ();
1462 overrides = mono_class_get_overrides (class->image, class->type_token, &onum);
1463 mono_class_setup_vtable (class, overrides, onum);
1467 class->init_pending = 0;
1470 if (class == mono_defaults.object_class) {
1472 for (i = 0; i < class->vtable_size; ++i) {
1473 MonoMethod *cm = class->vtable [i];
1475 if (!strcmp (cm->name, "GetHashCode")) {
1481 g_assert (ghc_slot > 0);
1483 default_ghc = class->vtable [ghc_slot];
1488 if (class->parent) {
1490 if (class->vtable [ghc_slot] == default_ghc) {
1495 if (!default_finalize) {
1496 if (class == mono_defaults.object_class) {
1498 for (i = 0; i < class->vtable_size; ++i) {
1499 MonoMethod *cm = class->vtable [i];
1501 if (!strcmp (cm->name, "Finalize")) {
1507 g_assert (finalize_slot > 0);
1509 default_finalize = class->vtable [finalize_slot];
1513 /* Object::Finalize should have empty implemenatation */
1514 class->has_finalize = 0;
1515 if (class->parent) {
1516 if (class->vtable [finalize_slot] != default_finalize)
1517 class->has_finalize = 1;
1520 mono_loader_unlock ();
1522 if (mono_debugger_class_init_func)
1523 mono_debugger_class_init_func (class);
1528 mono_class_setup_mono_type (MonoClass *class)
1530 const char *name = class->name;
1531 const char *nspace = class->name_space;
1533 if (MONO_CLASS_IS_INTERFACE (class))
1534 class->interface_id = mono_get_unique_iid (class);
1536 class->this_arg.byref = 1;
1537 class->this_arg.data.klass = class;
1538 class->this_arg.type = MONO_TYPE_CLASS;
1539 class->byval_arg.data.klass = class;
1540 class->byval_arg.type = MONO_TYPE_CLASS;
1542 if (!strcmp (nspace, "System")) {
1543 if (!strcmp (name, "ValueType")) {
1545 * do not set the valuetype bit for System.ValueType.
1546 * class->valuetype = 1;
1548 class->blittable = TRUE;
1549 } else if (!strcmp (name, "Enum")) {
1551 * do not set the valuetype bit for System.Enum.
1552 * class->valuetype = 1;
1554 class->valuetype = 0;
1555 class->enumtype = 0;
1556 } else if (!strcmp (name, "Object")) {
1557 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
1558 } else if (!strcmp (name, "String")) {
1559 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
1560 } else if (!strcmp (name, "TypedReference")) {
1561 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
1565 if (class->valuetype) {
1566 int t = MONO_TYPE_VALUETYPE;
1567 if (!strcmp (nspace, "System")) {
1570 if (!strcmp (name, "Boolean")) {
1571 t = MONO_TYPE_BOOLEAN;
1572 } else if (!strcmp(name, "Byte")) {
1574 class->blittable = TRUE;
1578 if (!strcmp (name, "Char")) {
1583 if (!strcmp (name, "Double")) {
1585 class->blittable = TRUE;
1589 if (!strcmp (name, "Int32")) {
1591 class->blittable = TRUE;
1592 } else if (!strcmp(name, "Int16")) {
1594 class->blittable = TRUE;
1595 } else if (!strcmp(name, "Int64")) {
1597 class->blittable = TRUE;
1598 } else if (!strcmp(name, "IntPtr")) {
1600 class->blittable = TRUE;
1604 if (!strcmp (name, "Single")) {
1606 class->blittable = TRUE;
1607 } else if (!strcmp(name, "SByte")) {
1609 class->blittable = TRUE;
1613 if (!strcmp (name, "UInt32")) {
1615 class->blittable = TRUE;
1616 } else if (!strcmp(name, "UInt16")) {
1618 class->blittable = TRUE;
1619 } else if (!strcmp(name, "UInt64")) {
1621 class->blittable = TRUE;
1622 } else if (!strcmp(name, "UIntPtr")) {
1624 class->blittable = TRUE;
1628 if (!strcmp (name, "TypedReference")) {
1629 t = MONO_TYPE_TYPEDBYREF;
1630 class->blittable = TRUE;
1634 if (!strcmp (name, "Void")) {
1642 class->this_arg.type = class->byval_arg.type = t;
1647 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
1649 gboolean system_namespace;
1651 system_namespace = !strcmp (class->name_space, "System");
1653 /* if root of the hierarchy */
1654 if (system_namespace && !strcmp (class->name, "Object")) {
1655 class->parent = NULL;
1656 class->instance_size = sizeof (MonoObject);
1659 if (!strcmp (class->name, "<Module>")) {
1660 class->parent = NULL;
1661 class->instance_size = 0;
1665 if (!MONO_CLASS_IS_INTERFACE (class)) {
1666 class->parent = parent;
1669 g_assert_not_reached (); /* FIXME */
1671 if (parent->generic_inst && !parent->name) {
1673 * If the parent is a generic instance, we may get
1674 * called before it is fully initialized, especially
1675 * before it has its name.
1680 class->marshalbyref = parent->marshalbyref;
1681 class->contextbound = parent->contextbound;
1682 class->delegate = parent->delegate;
1684 if (system_namespace) {
1685 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
1686 class->marshalbyref = 1;
1688 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
1689 class->contextbound = 1;
1691 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
1692 class->delegate = 1;
1695 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
1696 (strcmp (class->parent->name_space, "System") == 0)))
1697 class->valuetype = 1;
1698 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
1699 class->valuetype = class->enumtype = 1;
1701 /*class->enumtype = class->parent->enumtype; */
1702 mono_class_setup_supertypes (class);
1704 class->parent = NULL;
1710 mono_class_setup_supertypes (MonoClass *class)
1715 if (class->supertypes)
1719 for (k = class; k ; k = k->parent) {
1723 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
1724 class->supertypes = g_new0 (MonoClass *, ms);
1726 if (class->parent) {
1727 for (i = class->idepth, k = class; k ; k = k->parent)
1728 class->supertypes [--i] = k;
1730 class->supertypes [0] = class;
1735 * @image: context where the image is created
1736 * @type_token: typedef token
1739 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
1741 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
1742 MonoClass *class, *parent = NULL;
1743 guint32 cols [MONO_TYPEDEF_SIZE];
1744 guint32 cols_next [MONO_TYPEDEF_SIZE];
1745 guint tidx = mono_metadata_token_index (type_token);
1746 const char *name, *nspace;
1748 MonoClass **interfaces;
1750 mono_loader_lock ();
1752 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
1753 mono_loader_unlock ();
1757 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
1759 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
1761 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1762 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1764 if (cols [MONO_TYPEDEF_EXTENDS])
1765 parent = mono_class_get (image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]));
1766 interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
1768 class = g_malloc0 (sizeof (MonoClass));
1770 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
1772 class->interfaces = interfaces;
1773 class->interface_count = icount;
1776 class->name_space = nspace;
1778 class->image = image;
1779 class->type_token = type_token;
1780 class->flags = cols [MONO_TYPEDEF_FLAGS];
1782 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
1784 /* fixme: maybe we must set this on windows
1785 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
1789 class->cast_class = class->element_class = class;
1791 /*g_print ("Load class %s\n", name);*/
1793 mono_class_setup_parent (class, parent);
1795 mono_class_setup_mono_type (class);
1798 * Compute the field and method lists
1800 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
1801 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
1803 if (tt->rows > tidx){
1804 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
1805 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1806 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1808 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
1809 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
1812 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
1813 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
1814 class->field.count = class->field.last - class->field.first;
1816 class->field.count = 0;
1818 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
1819 class->method.count = class->method.last - class->method.first;
1821 class->method.count = 0;
1823 /* reserve space to store vector pointer in arrays */
1824 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
1825 class->instance_size += 2 * sizeof (gpointer);
1826 g_assert (class->field.count == 0);
1829 if (class->enumtype)
1830 class_compute_field_layout (class);
1832 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
1833 class->nested_in = mono_class_create_from_typedef (image, type_token);
1835 class->gen_params = mono_metadata_load_generic_params (image, class->type_token, &icount);
1836 class->num_gen_params = icount;
1838 mono_loader_unlock ();
1844 mono_class_create_generic (MonoGenericInst *ginst)
1846 MonoClass *klass, *gklass;
1849 ginst->klass = g_malloc0 (sizeof (MonoClass));
1850 klass = ginst->klass;
1852 gklass = mono_class_from_mono_type (ginst->generic_type);
1854 klass->nested_in = gklass->nested_in;
1856 klass->name = gklass->name;
1857 klass->name_space = gklass->name_space;
1858 klass->image = gklass->image;
1859 klass->flags = gklass->flags;
1861 klass->generic_inst = ginst;
1863 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
1864 klass->this_arg.data.generic_inst = klass->byval_arg.data.generic_inst = ginst;
1865 klass->this_arg.byref = TRUE;
1867 klass->cast_class = klass->element_class = klass;
1869 if (ginst->is_dynamic) {
1870 klass->instance_size = gklass->instance_size;
1871 klass->class_size = gklass->class_size;
1872 klass->size_inited = 1;
1874 klass->valuetype = gklass->valuetype;
1881 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
1883 MonoClass *klass, **ptr;
1887 return param->pklass;
1889 klass = param->pklass = g_new0 (MonoClass, 1);
1891 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
1895 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
1896 klass->parent = param->constraints [0];
1900 if (count - pos > 0) {
1901 klass->interface_count = count - pos;
1902 klass->interfaces = g_new0 (MonoClass *, count - pos);
1903 for (i = pos; i < count; i++)
1904 klass->interfaces [i - pos] = param->constraints [i];
1907 g_assert (param->name);
1909 klass->name = param->name;
1910 klass->name_space = "";
1911 klass->image = image;
1912 klass->cast_class = klass->element_class = klass;
1913 klass->enum_basetype = &klass->element_class->byval_arg;
1914 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
1916 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
1917 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
1918 klass->this_arg.byref = TRUE;
1920 mono_class_init (klass);
1926 my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
1931 return param->pklass;
1933 klass = g_new0 (MonoClass, 1);
1936 klass->name = param->name;
1938 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
1939 klass->name_space = "";
1940 klass->image = mono_defaults.corlib;
1941 klass->cast_class = klass->element_class = klass;
1942 klass->enum_basetype = &klass->element_class->byval_arg;
1943 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
1945 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
1946 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
1947 klass->this_arg.byref = TRUE;
1949 mono_class_init (klass);
1955 mono_ptr_class_get (MonoType *type)
1958 MonoClass *el_class;
1959 static GHashTable *ptr_hash = NULL;
1961 mono_loader_lock ();
1964 ptr_hash = g_hash_table_new (NULL, NULL);
1965 el_class = mono_class_from_mono_type (type);
1966 if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
1967 mono_loader_unlock ();
1970 result = g_new0 (MonoClass, 1);
1972 result->parent = NULL; /* no parent for PTR types */
1973 result->name = "System";
1974 result->name_space = "MonoPtrFakeClass";
1975 result->image = el_class->image;
1976 result->inited = TRUE;
1977 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
1978 /* Can pointers get boxed? */
1979 result->instance_size = sizeof (gpointer);
1980 result->cast_class = result->element_class = el_class;
1981 result->enum_basetype = &result->element_class->byval_arg;
1982 result->blittable = TRUE;
1984 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
1985 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
1986 result->this_arg.byref = TRUE;
1988 mono_class_setup_supertypes (result);
1990 g_hash_table_insert (ptr_hash, el_class, result);
1992 mono_loader_unlock ();
1998 mono_fnptr_class_get (MonoMethodSignature *sig)
2001 static GHashTable *ptr_hash = NULL;
2003 mono_loader_lock ();
2006 ptr_hash = g_hash_table_new (NULL, NULL);
2008 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
2009 mono_loader_unlock ();
2012 result = g_new0 (MonoClass, 1);
2014 result->parent = NULL; /* no parent for PTR types */
2015 result->name = "System";
2016 result->name_space = "MonoFNPtrFakeClass";
2017 result->image = NULL; /* need to fix... */
2018 result->inited = TRUE;
2019 result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
2020 /* Can pointers get boxed? */
2021 result->instance_size = sizeof (gpointer);
2022 result->cast_class = result->element_class = result;
2023 result->blittable = TRUE;
2025 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
2026 result->this_arg.data.method = result->byval_arg.data.method = sig;
2027 result->this_arg.byref = TRUE;
2028 result->enum_basetype = &result->element_class->byval_arg;
2029 result->blittable = TRUE;
2031 mono_class_setup_supertypes (result);
2033 g_hash_table_insert (ptr_hash, sig, result);
2035 mono_loader_unlock ();
2041 mono_class_from_mono_type (MonoType *type)
2043 switch (type->type) {
2044 case MONO_TYPE_OBJECT:
2045 return type->data.klass? type->data.klass: mono_defaults.object_class;
2046 case MONO_TYPE_VOID:
2047 return type->data.klass? type->data.klass: mono_defaults.void_class;
2048 case MONO_TYPE_BOOLEAN:
2049 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
2050 case MONO_TYPE_CHAR:
2051 return type->data.klass? type->data.klass: mono_defaults.char_class;
2053 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
2055 return type->data.klass? type->data.klass: mono_defaults.byte_class;
2057 return type->data.klass? type->data.klass: mono_defaults.int16_class;
2059 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
2061 return type->data.klass? type->data.klass: mono_defaults.int32_class;
2063 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
2065 return type->data.klass? type->data.klass: mono_defaults.int_class;
2067 return type->data.klass? type->data.klass: mono_defaults.uint_class;
2069 return type->data.klass? type->data.klass: mono_defaults.int64_class;
2071 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
2073 return type->data.klass? type->data.klass: mono_defaults.single_class;
2075 return type->data.klass? type->data.klass: mono_defaults.double_class;
2076 case MONO_TYPE_STRING:
2077 return type->data.klass? type->data.klass: mono_defaults.string_class;
2078 case MONO_TYPE_TYPEDBYREF:
2079 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
2080 case MONO_TYPE_ARRAY:
2081 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
2083 return mono_ptr_class_get (type->data.type);
2084 case MONO_TYPE_FNPTR:
2085 return mono_fnptr_class_get (type->data.method);
2086 case MONO_TYPE_SZARRAY:
2087 return mono_array_class_get (type->data.klass, 1);
2088 case MONO_TYPE_CLASS:
2089 case MONO_TYPE_VALUETYPE:
2090 return type->data.klass;
2091 case MONO_TYPE_GENERICINST:
2092 g_assert (type->data.generic_inst->klass);
2093 return type->data.generic_inst->klass;
2095 return my_mono_class_from_generic_parameter (type->data.generic_param, FALSE);
2096 case MONO_TYPE_MVAR:
2097 return my_mono_class_from_generic_parameter (type->data.generic_param, TRUE);
2099 g_warning ("implement me 0x%02x\n", type->type);
2100 g_assert_not_reached ();
2107 * @image: context where the image is created
2108 * @type_spec: typespec token
2111 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec,
2112 MonoGenericContext *context)
2114 MonoType *type, *inflated;
2117 type = mono_type_create_from_typespec (image, type_spec);
2119 switch (type->type) {
2120 case MONO_TYPE_ARRAY:
2121 class = mono_array_class_get (type->data.array->eklass, type->data.array->rank);
2123 case MONO_TYPE_SZARRAY:
2124 class = mono_array_class_get (type->data.klass, 1);
2127 class = mono_ptr_class_get (type->data.type);
2129 case MONO_TYPE_GENERICINST:
2130 g_assert (type->data.generic_inst->klass);
2131 class = type->data.generic_inst->klass;
2134 /* it seems any type can be stored in TypeSpec as well */
2135 class = mono_class_from_mono_type (type);
2139 if (!class || !context)
2142 inflated = mono_class_inflate_generic_type (&class->byval_arg, context);
2144 return mono_class_from_mono_type (inflated);
2148 * mono_bounded_array_class_get:
2149 * @element_class: element class
2150 * @rank: the dimension of the array class
2151 * @bounded: whenever the array has non-zero bounds
2153 * Returns: a class object describing the array with element type @element_type and
2157 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
2161 MonoClass *parent = NULL;
2162 GSList *list, *rootlist;
2165 gboolean corlib_type = FALSE;
2167 g_assert (rank <= 255);
2170 /* bounded only matters for one-dimensional arrays */
2173 image = eclass->image;
2175 mono_loader_lock ();
2177 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
2178 for (; list; list = list->next) {
2180 if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
2181 mono_loader_unlock ();
2187 /* for the building corlib use System.Array from it */
2188 if (image->assembly && image->assembly->dynamic && strcmp (image->assembly_name, "mscorlib") == 0) {
2189 parent = mono_class_from_name (image, "System", "Array");
2192 parent = mono_defaults.array_class;
2193 if (!parent->inited)
2194 mono_class_init (parent);
2197 class = g_malloc0 (sizeof (MonoClass));
2199 class->image = image;
2200 class->name_space = eclass->name_space;
2201 nsize = strlen (eclass->name);
2202 name = g_malloc (nsize + 2 + rank);
2203 memcpy (name, eclass->name, nsize);
2206 memset (name + nsize + 1, ',', rank - 1);
2207 name [nsize + rank] = ']';
2208 name [nsize + rank + 1] = 0;
2210 class->type_token = 0;
2211 /* all arrays are marked serializable and sealed, bug #42779 */
2212 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
2213 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2214 class->parent = parent;
2215 class->instance_size = mono_class_instance_size (class->parent);
2216 class->class_size = 0;
2217 mono_class_setup_supertypes (class);
2221 if (eclass->enumtype)
2222 class->cast_class = eclass->element_class;
2224 class->cast_class = eclass;
2226 class->element_class = eclass;
2228 if ((rank > 1) || bounded) {
2229 MonoArrayType *at = g_new0 (MonoArrayType, 1);
2230 class->byval_arg.type = MONO_TYPE_ARRAY;
2231 class->byval_arg.data.array = at;
2232 at->eklass = eclass;
2234 /* FIXME: complete.... */
2236 class->byval_arg.type = MONO_TYPE_SZARRAY;
2237 class->byval_arg.data.klass = eclass;
2239 class->this_arg = class->byval_arg;
2240 class->this_arg.byref = 1;
2245 list = g_slist_append (rootlist, class);
2246 g_hash_table_insert (image->array_cache, eclass, list);
2248 mono_loader_unlock ();
2254 * mono_array_class_get:
2255 * @element_class: element class
2256 * @rank: the dimension of the array class
2258 * Returns: a class object describing the array with element type @element_type and
2262 mono_array_class_get (MonoClass *eclass, guint32 rank)
2264 return mono_bounded_array_class_get (eclass, rank, FALSE);
2268 * mono_class_instance_size:
2271 * Returns: the size of an object instance
2274 mono_class_instance_size (MonoClass *klass)
2276 if (!klass->size_inited)
2277 mono_class_init (klass);
2279 return klass->instance_size;
2283 * mono_class_min_align:
2286 * Returns: minimm alignment requirements
2289 mono_class_min_align (MonoClass *klass)
2291 if (!klass->size_inited)
2292 mono_class_init (klass);
2294 return klass->min_align;
2298 * mono_class_value_size:
2301 * This function is used for value types, and return the
2302 * space and the alignment to store that kind of value object.
2304 * Returns: the size of a value of kind @klass
2307 mono_class_value_size (MonoClass *klass, guint32 *align)
2311 /* fixme: check disable, because we still have external revereces to
2312 * mscorlib and Dummy Objects
2314 /*g_assert (klass->valuetype);*/
2316 size = mono_class_instance_size (klass) - sizeof (MonoObject);
2319 *align = klass->min_align;
2325 * mono_class_data_size:
2328 * Returns: the size of the static class data
2331 mono_class_data_size (MonoClass *klass)
2334 mono_class_init (klass);
2336 return klass->class_size;
2340 * Auxiliary routine to mono_class_get_field
2342 * Takes a field index instead of a field token.
2344 static MonoClassField *
2345 mono_class_get_field_idx (MonoClass *class, int idx)
2347 if (class->field.count){
2348 if ((idx >= class->field.first) && (idx < class->field.last)){
2349 return &class->fields [idx - class->field.first];
2356 return mono_class_get_field_idx (class->parent, idx);
2360 * mono_class_get_field:
2361 * @class: the class to lookup the field.
2362 * @field_token: the field token
2364 * Returns: A MonoClassField representing the type and offset of
2365 * the field, or a NULL value if the field does not belong to this
2369 mono_class_get_field (MonoClass *class, guint32 field_token)
2371 int idx = mono_metadata_token_index (field_token);
2373 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
2375 return mono_class_get_field_idx (class, idx - 1);
2379 mono_class_get_field_from_name (MonoClass *klass, const char *name)
2384 for (i = 0; i < klass->field.count; ++i) {
2385 if (strcmp (name, klass->fields [i].name) == 0)
2386 return &klass->fields [i];
2388 klass = klass->parent;
2394 mono_vtable_get_static_field_data (MonoVTable *vt)
2400 mono_class_get_property_from_name (MonoClass *klass, const char *name)
2405 for (i = 0; i < klass->property.count; ++i) {
2406 if (strcmp (name, klass->properties [i].name) == 0)
2407 return &klass->properties [i];
2409 klass = klass->parent;
2416 * @image: the image where the class resides
2417 * @type_token: the token for the class
2418 * @at: an optional pointer to return the array element type
2420 * Returns: the MonoClass that represents @type_token in @image
2423 mono_class_get (MonoImage *image, guint32 type_token)
2425 MonoClass *class = NULL;
2428 return mono_lookup_dynamic_token (image, type_token);
2430 switch (type_token & 0xff000000){
2431 case MONO_TOKEN_TYPE_DEF:
2432 class = mono_class_create_from_typedef (image, type_token);
2434 case MONO_TOKEN_TYPE_REF:
2435 class = mono_class_from_typeref (image, type_token);
2437 case MONO_TOKEN_TYPE_SPEC:
2438 class = mono_class_create_from_typespec (image, type_token, NULL);
2441 g_warning ("unknown token type %x", type_token & 0xff000000);
2442 g_assert_not_reached ();
2446 g_warning ("Could not load class from token 0x%08x in %s", type_token, image->name);
2452 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
2454 MonoClass *class = mono_class_get (image, type_token);
2457 if (!class || !context)
2460 switch (class->byval_arg.type) {
2461 case MONO_TYPE_GENERICINST:
2462 if (!class->generic_inst->is_open)
2466 case MONO_TYPE_MVAR:
2472 inflated = inflate_generic_type (&class->byval_arg, context);
2476 return mono_class_from_mono_type (inflated);
2480 * mono_class_from_name_case:
2481 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
2482 * @name_space: the type namespace
2483 * @name: the type short name.
2485 * Obtains a MonoClass with a given namespace and a given name which
2486 * is located in the given MonoImage. The namespace and name
2487 * lookups are case insensitive.
2489 * You can also pass `NULL' to the image, and that will lookup for
2490 * a type with the given namespace and name in all of the loaded
2491 * assemblies: notice that since there might be a name clash in this
2492 * case, passing NULL is not encouraged if you need a precise type.
2496 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
2498 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
2499 guint32 cols [MONO_TYPEDEF_SIZE];
2504 /* add a cache if needed */
2505 for (i = 1; i <= t->rows; ++i) {
2506 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
2507 /* nested types are accessed from the nesting name */
2508 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
2509 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
2511 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2512 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2513 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
2514 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
2520 return_nested_in (MonoClass *class, char *nested) {
2522 char *s = strchr (nested, '/');
2529 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
2531 if (strcmp (found->name, nested) == 0) {
2533 return return_nested_in (found, s);
2542 * mono_class_from_name_case:
2543 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
2544 * @name_space: the type namespace
2545 * @name: the type short name.
2547 * Obtains a MonoClass with a given namespace and a given name which
2548 * is located in the given MonoImage.
2550 * You can also pass `NULL' to the image, and that will lookup for
2551 * a type with the given namespace and name in all of the loaded
2552 * assemblies: notice that since there might be a name clash in this
2553 * case, passing NULL is not encouraged if you need a precise type.
2557 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
2559 GHashTable *nspace_table;
2560 MonoImage *loaded_image;
2566 if ((nested = strchr (name, '/'))) {
2567 int pos = nested - name;
2568 int len = strlen (name);
2571 memcpy (buf, name, len + 1);
2573 nested = buf + pos + 1;
2577 mono_loader_lock ();
2579 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
2582 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
2584 mono_loader_unlock ();
2589 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
2590 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
2591 guint32 cols [MONO_EXP_TYPE_SIZE];
2594 idx = mono_metadata_token_index (token);
2596 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
2598 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
2599 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
2600 loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
2603 class = mono_class_from_name (loaded_image, name_space, name);
2605 return return_nested_in (class, nested);
2608 g_error ("not yet implemented");
2612 token = MONO_TOKEN_TYPE_DEF | token;
2614 class = mono_class_get (image, token);
2616 return return_nested_in (class, nested);
2621 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
2622 gboolean check_interfaces)
2625 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
2626 if ((klassc->interface_id <= klass->max_interface_id) &&
2627 (klass->interface_offsets [klassc->interface_id] >= 0))
2629 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
2632 for (i = 0; i < klass->interface_count; i ++) {
2633 MonoClass *ic = klass->interfaces [i];
2638 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
2640 if (klass->generic_inst) {
2641 MonoType *parent = klass->generic_inst->parent;
2645 if (mono_metadata_type_equal (parent, &klassc->byval_arg))
2647 klass = mono_class_from_mono_type (parent);
2653 * MS.NET thinks interfaces are a subclass of Object, so we think it as
2656 if (klassc == mono_defaults.object_class)
2663 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
2666 mono_class_init (klass);
2668 if (!oklass->inited)
2669 mono_class_init (oklass);
2671 if (MONO_CLASS_IS_INTERFACE (klass)) {
2672 if ((klass->interface_id <= oklass->max_interface_id) &&
2673 (oklass->interface_offsets [klass->interface_id] != -1))
2677 MonoClass *eclass, *eoclass;
2679 if (oklass->rank != klass->rank)
2682 /* vectors vs. one dimensional arrays */
2683 if (oklass->byval_arg.type != klass->byval_arg.type)
2686 eclass = klass->cast_class;
2687 eoclass = oklass->cast_class;
2691 * a is b does not imply a[] is b[] when a is a valuetype, and
2692 * b is a reference type.
2695 if (eoclass->valuetype) {
2696 if ((eclass == mono_defaults.enum_class) ||
2697 (eclass == mono_defaults.enum_class->parent) ||
2698 (eclass == mono_defaults.object_class))
2702 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
2705 if (klass == mono_defaults.object_class)
2708 return mono_class_has_parent (oklass, klass);
2712 * mono_class_needs_cctor_run:
2714 * Determines whenever the class has a static constructor and whenever it
2715 * needs to be called when executing CALLER.
2718 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
2723 for (i = 0; i < klass->method.count; ++i) {
2724 method = klass->methods [i];
2725 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2726 (strcmp (".cctor", method->name) == 0)) {
2727 if (caller == method)
2736 * Returns the nnumber of bytes an element of type klass
2737 * uses when stored into an array.
2740 mono_class_array_element_size (MonoClass *klass)
2742 int t = klass->byval_arg.type;
2748 case MONO_TYPE_BOOLEAN:
2752 case MONO_TYPE_CHAR:
2761 case MONO_TYPE_CLASS:
2762 case MONO_TYPE_STRING:
2763 case MONO_TYPE_OBJECT:
2764 case MONO_TYPE_SZARRAY:
2765 case MONO_TYPE_ARRAY:
2767 case MONO_TYPE_MVAR:
2768 return sizeof (gpointer);
2773 case MONO_TYPE_VALUETYPE:
2774 if (klass->enumtype) {
2775 t = klass->enum_basetype->type;
2778 return mono_class_instance_size (klass) - sizeof (MonoObject);
2780 g_error ("unknown type 0x%02x in mono_class_array_element_size", t);
2786 * mono_array_element_size:
2787 * @ac: pointer to a #MonoArrayClass
2789 * Returns: the size of single array element.
2792 mono_array_element_size (MonoClass *ac)
2794 return mono_class_array_element_size (ac->element_class);
2798 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
2799 MonoGenericContext *context)
2801 if (image->dynamic) {
2802 gpointer obj = mono_lookup_dynamic_token (image, token);
2804 switch (token & 0xff000000) {
2805 case MONO_TOKEN_TYPE_DEF:
2806 case MONO_TOKEN_TYPE_REF:
2807 case MONO_TOKEN_TYPE_SPEC:
2809 *handle_class = mono_defaults.typehandle_class;
2810 return &((MonoClass*)obj)->byval_arg;
2811 case MONO_TOKEN_METHOD_DEF:
2813 *handle_class = mono_defaults.methodhandle_class;
2815 case MONO_TOKEN_FIELD_DEF:
2817 *handle_class = mono_defaults.fieldhandle_class;
2820 g_assert_not_reached ();
2824 switch (token & 0xff000000) {
2825 case MONO_TOKEN_TYPE_DEF:
2826 case MONO_TOKEN_TYPE_REF: {
2829 *handle_class = mono_defaults.typehandle_class;
2830 class = mono_class_get_full (image, token, context);
2831 mono_class_init (class);
2832 /* We return a MonoType* as handle */
2833 return &class->byval_arg;
2835 case MONO_TOKEN_TYPE_SPEC: {
2838 *handle_class = mono_defaults.typehandle_class;
2839 class = mono_class_create_from_typespec (image, token, context);
2840 mono_class_init (class);
2841 return &class->byval_arg;
2843 case MONO_TOKEN_FIELD_DEF: {
2845 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
2846 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
2847 mono_class_init (class);
2849 *handle_class = mono_defaults.fieldhandle_class;
2850 return mono_class_get_field (class, token);
2852 case MONO_TOKEN_METHOD_DEF: {
2854 meth = mono_get_method_full (image, token, NULL, context);
2856 *handle_class = mono_defaults.methodhandle_class;
2859 case MONO_TOKEN_MEMBER_REF: {
2860 guint32 cols [MONO_MEMBERREF_SIZE];
2862 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
2863 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
2864 mono_metadata_decode_blob_size (sig, &sig);
2865 if (*sig == 0x6) { /* it's a field */
2867 MonoClassField *field;
2868 field = mono_field_from_token (image, token, &klass, context);
2870 *handle_class = mono_defaults.fieldhandle_class;
2874 meth = mono_get_method_full (image, token, NULL, context);
2876 *handle_class = mono_defaults.methodhandle_class;
2881 g_warning ("Unknown token 0x%08x in ldtoken", token);
2888 * This function might need to call runtime functions so it can't be part
2889 * of the metadata library.
2891 static MonoLookupDynamicToken lookup_dynamic = NULL;
2894 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
2896 lookup_dynamic = func;
2900 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
2902 return lookup_dynamic (image, token);
2906 mono_class_get_image (MonoClass *klass)
2908 return klass->image;
2912 * mono_class_get_element_class:
2913 * @klass: the MonoClass to act on
2915 * Returns the element class of an array or an enumeration.
2918 mono_class_get_element_class (MonoClass *klass)
2920 return klass->element_class;
2924 * mono_class_is_valuetype:
2925 * @klass: the MonoClass to act on
2927 * Returns true if the MonoClass represents a ValueType.
2930 mono_class_is_valuetype (MonoClass *klass)
2932 return klass->valuetype;
2936 * mono_class_is_enum:
2937 * @klass: the MonoClass to act on
2939 * Returns true if the MonoClass represents an enumeration.
2942 mono_class_is_enum (MonoClass *klass)
2944 return klass->enumtype;
2948 * mono_class_enum_basetype:
2949 * @klass: the MonoClass to act on
2951 * Returns the underlying type representation for an enumeration.
2954 mono_class_enum_basetype (MonoClass *klass)
2956 return klass->enum_basetype;
2960 * mono_class_get_parent
2961 * @klass: the MonoClass to act on
2963 * Returns the parent class for this class.
2966 mono_class_get_parent (MonoClass *klass)
2968 return klass->parent;
2972 * mono_class_get_nesting_type;
2973 * @klass: the MonoClass to act on
2975 * Returns the container type where this type is nested or NULL if this type is not a nested type.
2978 mono_class_get_nesting_type (MonoClass *klass)
2980 return klass->nested_in;
2984 * mono_class_get_rank:
2985 * @klass: the MonoClass to act on
2987 * Returns the rank for the array (the number of dimensions).
2990 mono_class_get_rank (MonoClass *klass)
2996 * mono_class_get_flags:
2997 * @klass: the MonoClass to act on
2999 * The type flags from the TypeDef table from the metadata.
3000 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
3003 * Returns the flags from the TypeDef table.
3006 mono_class_get_flags (MonoClass *klass)
3008 return klass->flags;
3012 * mono_class_get_name
3013 * @klass: the MonoClass to act on
3015 * Returns the name of the class.
3018 mono_class_get_name (MonoClass *klass)
3024 * mono_class_get_namespace:
3025 * @klass: the MonoClass to act on
3027 * Returns the namespace of the class.
3030 mono_class_get_namespace (MonoClass *klass)
3032 return klass->name_space;
3036 * mono_class_get_type:
3037 * @klass: the MonoClass to act on
3039 * This method returns the internal Type representation for the class.
3041 * Returns the MonoType from the class.
3044 mono_class_get_type (MonoClass *klass)
3046 return &klass->byval_arg;
3050 * mono_class_get_byref_type:
3051 * @klass: the MonoClass to act on
3056 mono_class_get_byref_type (MonoClass *klass)
3058 return &klass->this_arg;
3062 * mono_class_num_fields:
3063 * @klass: the MonoClass to act on
3065 * Returns the number of static and instance fields in the class.
3068 mono_class_num_fields (MonoClass *klass)
3070 return klass->field.count;
3074 * mono_class_num_methods:
3075 * @klass: the MonoClass to act on
3077 * Returns the number of methods in the class.
3080 mono_class_num_methods (MonoClass *klass)
3082 return klass->method.count;
3086 * mono_class_num_properties
3087 * @klass: the MonoClass to act on
3089 * Returns the number of properties in the class.
3092 mono_class_num_properties (MonoClass *klass)
3094 return klass->property.count;
3098 * mono_class_num_events:
3099 * @klass: the MonoClass to act on
3101 * Returns the number of events in the class.
3104 mono_class_num_events (MonoClass *klass)
3106 return klass->event.count;
3110 * mono_class_get_fields:
3111 * @klass: the MonoClass to act on
3113 * This routine is an iterator routine for retrieving the fields in a class.
3115 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3116 * iterate over all of the elements. When no more values are
3117 * available, the return value is NULL.
3119 * Returns a `MonoClassField *' on each iteration, or NULL when no more fields are available.
3122 mono_class_get_fields (MonoClass* klass, gpointer *iter)
3124 MonoClassField* field;
3128 mono_class_init (klass);
3130 /* start from the first */
3131 if (klass->field.count) {
3132 return *iter = &klass->fields [0];
3140 if (field < &klass->fields [klass->field.count]) {
3141 return *iter = field;
3147 * mono_class_get_methods
3148 * @klass: the MonoClass to act on
3150 * This routine is an iterator routine for retrieving the fields in a class.
3152 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3153 * iterate over all of the elements. When no more values are
3154 * available, the return value is NULL.
3156 * Returns a MonoMethod on each iteration or NULL when no more methods are available.
3159 mono_class_get_methods (MonoClass* klass, gpointer *iter)
3161 MonoMethod** method;
3165 mono_class_init (klass);
3167 /* start from the first */
3168 if (klass->method.count) {
3169 *iter = &klass->methods [0];
3170 return klass->methods [0];
3178 if (method < &klass->methods [klass->method.count]) {
3186 * mono_class_get_properties:
3187 * @klass: the MonoClass to act on
3189 * This routine is an iterator routine for retrieving the properties in a class.
3191 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3192 * iterate over all of the elements. When no more values are
3193 * available, the return value is NULL.
3195 * Returns a `MonoProperty *' on each invocation, or NULL when no more are available.
3198 mono_class_get_properties (MonoClass* klass, gpointer *iter)
3200 MonoProperty* property;
3204 mono_class_init (klass);
3206 /* start from the first */
3207 if (klass->property.count) {
3208 return *iter = &klass->properties [0];
3216 if (property < &klass->properties [klass->property.count]) {
3217 return *iter = property;
3223 * mono_class_get_events:
3224 * @klass: the MonoClass to act on
3226 * This routine is an iterator routine for retrieving the properties in a class.
3228 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3229 * iterate over all of the elements. When no more values are
3230 * available, the return value is NULL.
3232 * Returns a `MonoEvent *' on each invocation, or NULL when no more are available.
3235 mono_class_get_events (MonoClass* klass, gpointer *iter)
3241 mono_class_init (klass);
3243 /* start from the first */
3244 if (klass->event.count) {
3245 return *iter = &klass->events [0];
3253 if (event < &klass->events [klass->event.count]) {
3254 return *iter = event;
3260 * mono_class_get_interfaaces
3261 * @klass: the MonoClass to act on
3263 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
3265 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3266 * iterate over all of the elements. When no more values are
3267 * available, the return value is NULL.
3269 * Returns a `Monoclass *' on each invocation, or NULL when no more are available.
3272 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
3278 mono_class_init (klass);
3280 /* start from the first */
3281 if (klass->interface_count) {
3282 *iter = &klass->interfaces [0];
3283 return klass->interfaces [0];
3291 if (iface < &klass->interfaces [klass->interface_count]) {
3299 * mono_class_get_interfaaces
3300 * @klass: the MonoClass to act on
3302 * This routine is an iterator routine for retrieving the nested types of a class.
3304 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3305 * iterate over all of the elements. When no more values are
3306 * available, the return value is NULL.
3308 * Returns a `Monoclass *' on each invocation, or NULL when no more are available.
3311 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
3317 mono_class_init (klass);
3319 /* start from the first */
3320 if (klass->nested_classes) {
3321 *iter = klass->nested_classes;
3322 return klass->nested_classes->data;
3324 /* no nested types */
3338 * mono_field_get_name:
3339 * @field: the MonoClassField to act on
3341 * Returns the name of the field.
3344 mono_field_get_name (MonoClassField *field)
3350 * mono_field_get_type:
3351 * @field: the MonoClassField to act on
3353 * Returns MonoType of the field.
3356 mono_field_get_type (MonoClassField *field)
3362 * mono_field_get_type:
3363 * @field: the MonoClassField to act on
3365 * Returns MonoClass where the field was defined.
3368 mono_field_get_parent (MonoClassField *field)
3370 return field->parent;
3374 * mono_field_get_flags;
3375 * @field: the MonoClassField to act on
3377 * The metadata flags for a field are encoded using the
3378 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
3380 * Returns the flags for the field.
3383 mono_field_get_flags (MonoClassField *field)
3385 return field->type->attrs;
3389 * mono_property_get_name:
3390 * @prop: the MonoProperty to act on
3392 * Returns the name of the property
3395 mono_property_get_name (MonoProperty *prop)
3401 * mono_property_get_set_method
3402 * @prop: the MonoProperty to act on.
3404 * Returns the setter method of the property (A MonoMethod)
3407 mono_property_get_set_method (MonoProperty *prop)
3413 * mono_property_get_get_method
3414 * @prop: the MonoProperty to act on.
3416 * Returns the setter method of the property (A MonoMethod)
3419 mono_property_get_get_method (MonoProperty *prop)
3425 * mono_property_get_parent:
3426 * @prop: the MonoProperty to act on.
3428 * Returns the MonoClass where the property was defined.
3431 mono_property_get_parent (MonoProperty *prop)
3433 return prop->parent;
3437 * mono_property_get_flags:
3438 * @prop: the MonoProperty to act on.
3440 * The metadata flags for a property are encoded using the
3441 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
3443 * Returns the flags for the property.
3446 mono_property_get_flags (MonoProperty *prop)
3452 * mono_event_get_name:
3453 * @event: the MonoEvent to act on
3455 * Returns the name of the event.
3458 mono_event_get_name (MonoEvent *event)
3464 * mono_event_get_add_method:
3465 * @event: The MonoEvent to act on.
3467 * Returns the `add' method for the event (a MonoMethod).
3470 mono_event_get_add_method (MonoEvent *event)
3476 * mono_event_get_remove_method:
3477 * @event: The MonoEvent to act on.
3479 * Returns the `remove' method for the event (a MonoMethod).
3482 mono_event_get_remove_method (MonoEvent *event)
3484 return event->remove;
3488 * mono_event_get_raise_method:
3489 * @event: The MonoEvent to act on.
3491 * Returns the `raise' method for the event (a MonoMethod).
3494 mono_event_get_raise_method (MonoEvent *event)
3496 return event->raise;
3500 * mono_event_get_parent:
3501 * @event: the MonoEvent to act on.
3503 * Returns the MonoClass where the event is defined.
3506 mono_event_get_parent (MonoEvent *event)
3508 return event->parent;
3512 * mono_event_get_flags
3513 * @event: the MonoEvent to act on.
3515 * The metadata flags for an event are encoded using the
3516 * EVENT_* constants. See the tabledefs.h file for details.
3518 * Returns the flags for the event.
3521 mono_event_get_flags (MonoEvent *event)
3523 return event->attrs;