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 [idx-1])
88 mono_assembly_load_reference (image, idx - 1);
89 if (references [idx - 1] == (gpointer)-1)
92 image = references [idx-1]->image;
94 return mono_class_from_name (image, nspace, name);
98 dup_type (MonoType* t, const MonoType *original)
100 MonoType *r = g_new0 (MonoType, 1);
102 r->attrs = original->attrs;
103 r->byref = original->byref;
104 mono_stats.generics_metadata_size += sizeof (MonoType);
109 mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed,
110 gboolean include_arity)
114 switch (type->type) {
115 case MONO_TYPE_ARRAY: {
116 int i, rank = type->data.array->rank;
118 mono_type_get_name_recurse (
119 &type->data.array->eklass->byval_arg, str, FALSE, include_arity);
120 g_string_append_c (str, '[');
121 for (i = 1; i < rank; i++)
122 g_string_append_c (str, ',');
123 g_string_append_c (str, ']');
126 case MONO_TYPE_SZARRAY:
127 mono_type_get_name_recurse (
128 &type->data.klass->byval_arg, str, FALSE, include_arity);
129 g_string_append (str, "[]");
132 mono_type_get_name_recurse (type->data.type, str, FALSE, include_arity);
133 g_string_append_c (str, '*');
136 klass = mono_class_from_mono_type (type);
137 if (klass->nested_in) {
138 mono_type_get_name_recurse (
139 &klass->nested_in->byval_arg, str, TRUE, include_arity);
140 g_string_append_c (str, '+');
142 if (*klass->name_space) {
143 g_string_append (str, klass->name_space);
144 g_string_append_c (str, '.');
146 if (!include_arity) {
147 char *s = strchr (klass->name, '`');
148 int len = s ? s - klass->name : strlen (klass->name);
150 g_string_append_len (str, klass->name, len);
152 g_string_append (str, klass->name);
155 if (klass->generic_inst) {
156 MonoGenericInst *ginst = klass->generic_inst;
159 g_string_append_c (str, '<');
160 for (i = 0; i < ginst->type_argc; i++) {
162 g_string_append_c (str, ',');
163 mono_type_get_name_recurse (
164 ginst->type_argv [i], str, FALSE, include_arity);
166 g_string_append_c (str, '>');
167 } else if (klass->gen_params) {
170 g_string_append_c (str, '<');
171 for (i = 0; i < klass->num_gen_params; i++) {
173 g_string_append_c (str, ',');
174 g_string_append (str, klass->gen_params [i].name);
176 g_string_append_c (str, '>');
183 * mono_type_get_name:
186 * Returns the string representation for type as required by System.Reflection.
187 * The inverse of mono_reflection_parse_type ().
190 _mono_type_get_name (MonoType *type, gboolean is_recursed, gboolean include_arity)
192 GString* result = g_string_new ("");
193 mono_type_get_name_recurse (type, result, is_recursed, include_arity);
196 g_string_append_c (result, '&');
198 return g_string_free (result, FALSE);
202 mono_type_get_name (MonoType *type)
204 return _mono_type_get_name (type, TRUE, TRUE);
208 mono_type_get_full_name (MonoType *type)
210 return _mono_type_get_name (type, FALSE, TRUE);
214 mono_type_get_underlying_type (MonoType *type)
216 switch (type->type) {
217 case MONO_TYPE_VALUETYPE:
218 if (type->data.klass->enumtype)
219 return type->data.klass->enum_basetype;
221 case MONO_TYPE_GENERICINST:
222 return mono_type_get_underlying_type (type->data.generic_inst->generic_type);
231 mono_class_is_open_constructed_type (MonoType *t)
237 case MONO_TYPE_SZARRAY:
238 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
239 case MONO_TYPE_ARRAY:
240 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
242 return mono_class_is_open_constructed_type (t->data.type);
243 case MONO_TYPE_GENERICINST: {
244 MonoGenericInst *ginst = t->data.generic_inst;
247 if (mono_class_is_open_constructed_type (ginst->generic_type))
249 for (i = 0; i < ginst->type_argc; i++)
250 if (mono_class_is_open_constructed_type (ginst->type_argv [i]))
260 inflate_generic_type (MonoType *type, MonoGenericContext *context)
262 switch (type->type) {
264 if (context->gmethod && context->gmethod->mtype_argv)
266 context->gmethod->mtype_argv [type->data.generic_param->num],
273 context->ginst->type_argv [type->data.generic_param->num],
277 case MONO_TYPE_SZARRAY: {
278 MonoClass *eclass = type->data.klass;
279 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
282 nt = dup_type (type, type);
283 nt->data.klass = mono_class_from_mono_type (inflated);
286 case MONO_TYPE_GENERICINST: {
287 MonoGenericInst *oginst = type->data.generic_inst;
288 MonoGenericInst *nginst, *cached;
292 nginst = g_new0 (MonoGenericInst, 1);
295 nginst->is_open = FALSE;
297 nginst->type_argv = g_new0 (MonoType *, oginst->type_argc);
299 for (i = 0; i < oginst->type_argc; i++) {
300 MonoType *t = oginst->type_argv [i];
301 nginst->type_argv [i] = mono_class_inflate_generic_type (t, context);
303 if (!nginst->is_open)
304 nginst->is_open = mono_class_is_open_constructed_type (nginst->type_argv [i]);
307 nginst->klass = NULL;
309 nginst->context = g_new0 (MonoGenericContext, 1);
310 nginst->context->ginst = nginst;
313 cached = g_hash_table_lookup (oginst->klass->image->generic_inst_cache, nginst);
316 g_free (nginst->type_argv);
318 mono_loader_unlock ();
320 nt = dup_type (type, type);
321 nt->data.generic_inst = cached;
325 nginst->dynamic_info = NULL;
326 nginst->initialized = FALSE;
328 mono_class_create_generic (nginst);
330 mono_stats.generic_instance_count++;
331 mono_stats.generics_metadata_size += sizeof (MonoGenericInst) +
332 sizeof (MonoGenericContext) +
333 nginst->type_argc * sizeof (MonoType);
335 nt = dup_type (type, type);
336 nt->data.generic_inst = nginst;
337 g_hash_table_insert (oginst->klass->image->generic_inst_cache, nginst, nginst);
338 mono_loader_unlock ();
348 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
350 MonoType *inflated = inflate_generic_type (type, context);
355 mono_stats.inflated_type_count++;
359 static MonoMethodSignature*
360 inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig,
361 MonoGenericContext *context)
363 MonoMethodSignature *res;
365 res = mono_metadata_signature_alloc (image, sig->param_count);
366 res->ret = mono_class_inflate_generic_type (sig->ret, context);
367 for (i = 0; i < sig->param_count; ++i)
368 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
369 res->hasthis = sig->hasthis;
370 res->explicit_this = sig->explicit_this;
371 res->call_convention = sig->call_convention;
372 res->generic_param_count = sig->generic_param_count;
373 res->is_inflated = 1;
377 static MonoMethodHeader*
378 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
380 MonoMethodHeader *res;
382 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
383 res->code = header->code;
384 res->code_size = header->code_size;
385 res->max_stack = header->max_stack;
386 res->num_clauses = header->num_clauses;
387 res->init_locals = header->init_locals;
388 res->num_locals = header->num_locals;
389 res->clauses = header->clauses;
390 for (i = 0; i < header->num_locals; ++i)
391 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
396 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context,
399 MonoMethodInflated *result;
401 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
402 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
405 mono_stats.inflated_method_count++;
406 mono_stats.generics_metadata_size +=
407 sizeof (MonoMethodInflated) - sizeof (MonoMethodNormal);
409 result = g_new0 (MonoMethodInflated, 1);
410 result->nmethod = *(MonoMethodNormal*)method;
412 if (result->nmethod.header)
413 result->nmethod.header = inflate_generic_header (
414 result->nmethod.header, context);
417 result->nmethod.method.klass = klass;
419 MonoType *declaring = mono_class_inflate_generic_type (
420 &method->klass->byval_arg, context);
421 result->nmethod.method.klass = mono_class_from_mono_type (declaring);
424 result->nmethod.method.signature = inflate_generic_signature (
425 method->klass->image, method->signature, context);
427 if (context->gmethod) {
428 result->context = g_new0 (MonoGenericContext, 1);
429 result->context->gmethod = context->gmethod;
430 result->context->ginst = result->nmethod.method.klass->generic_inst;
432 mono_stats.generics_metadata_size += sizeof (MonoGenericContext);
434 result->context = result->nmethod.method.klass->generic_inst->context;
436 if (method->signature->is_inflated)
437 result->declaring = ((MonoMethodInflated *) method)->declaring;
439 result->declaring = method;
441 return (MonoMethod *) result;
445 * class_compute_field_layout:
446 * @m: pointer to the metadata.
447 * @class: The class to initialize
449 * Initializes the class->fields.
451 * Currently we only support AUTO_LAYOUT, and do not even try to do
452 * a good job at it. This is temporary to get the code for Paolo.
455 class_compute_field_layout (MonoClass *class)
457 MonoImage *m = class->image;
458 const int top = class->field.count;
459 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
460 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
461 int i, blittable = TRUE, real_size = 0;
463 guint32 packing_size = 0;
464 gboolean explicit_size;
465 MonoClassField *field;
467 if (class->size_inited)
471 if (!class->parent->size_inited)
472 class_compute_field_layout (class->parent);
473 class->instance_size += class->parent->instance_size;
474 class->min_align = class->parent->min_align;
475 blittable = class->parent->blittable;
477 class->instance_size = sizeof (MonoObject);
478 class->min_align = 1;
481 /* Get the real size */
482 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
485 g_assert ((packing_size & 0xfffffff0) == 0);
486 class->packing_size = packing_size;
487 real_size += class->instance_size;
491 if (explicit_size && real_size) {
492 class->instance_size = MAX (real_size, class->instance_size);
494 class->size_inited = 1;
495 class->blittable = blittable;
499 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT)
502 class->fields = g_new0 (MonoClassField, top);
505 * Fetch all the field information.
507 for (i = 0; i < top; i++){
509 guint32 cols [MONO_FIELD_SIZE];
510 int idx = class->field.first + i;
512 field = &class->fields [i];
513 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
514 /* The name is needed for fieldrefs */
515 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
516 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
517 mono_metadata_decode_value (sig, &sig);
518 /* FIELD signature == 0x06 */
519 g_assert (*sig == 0x06);
520 field->type = mono_metadata_parse_field_type (
521 m, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
522 if (mono_field_is_deleted (field))
524 if (class->generic_inst) {
525 field->type = mono_class_inflate_generic_type (
526 field->type, class->generic_inst->context);
527 field->type->attrs = cols [MONO_FIELD_FLAGS];
530 field->parent = class;
532 /* Only do these checks if we still think this type is blittable */
533 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
534 if (field->type->byref || MONO_TYPE_IS_REFERENCE (field->type)) {
537 MonoClass *field_class = mono_class_from_mono_type (field->type);
538 if (!field_class || !field_class->blittable)
543 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
544 mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
545 if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
546 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, field->name);
549 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
550 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
552 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
553 field->data = mono_image_rva_map (class->image, rva);
556 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
557 class->enum_basetype = field->type;
558 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
559 blittable = class->element_class->blittable;
562 /* The def_value of fields is compute lazily during vtable creation */
565 if (class == mono_defaults.string_class)
568 class->blittable = blittable;
570 if (class->enumtype && !class->enum_basetype) {
571 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
574 if (explicit_size && real_size) {
575 class->instance_size = MAX (real_size, class->instance_size);
578 if (class->gen_params ||
579 (class->generic_inst && class->generic_inst->is_open))
582 mono_class_layout_fields (class);
586 mono_class_layout_fields (MonoClass *class)
589 const int top = class->field.count;
590 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
591 guint32 pass, passes, real_size;
592 gboolean gc_aware_layout = FALSE;
593 MonoClassField *field;
596 * Enable GC aware auto layout: in this mode, reference
597 * fields are grouped together inside objects, increasing collector
599 * Requires that all classes whose layout is known to native code be annotated
600 * with [StructLayout (LayoutKind.Sequential)]
601 * Value types have gc_aware_layout disabled by default, as per
602 * what the default is for other runtimes.
604 /* corlib is missing [StructLayout] directives in many places */
605 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
606 if (class->image != mono_defaults.corlib &&
607 class->byval_arg.type != MONO_TYPE_VALUETYPE)
608 gc_aware_layout = TRUE;
612 * Compute field layout and total size (not considering static fields)
616 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
617 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
624 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
628 real_size = class->parent->instance_size;
630 real_size = sizeof (MonoObject);
632 for (pass = 0; pass < passes; ++pass) {
633 for (i = 0; i < top; i++){
635 field = &class->fields [i];
637 if (mono_field_is_deleted (field))
639 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
642 if (gc_aware_layout) {
644 * We process fields with reference type in the first pass,
645 * and fields with non-reference type in the second pass.
646 * We use IS_POINTER instead of IS_REFERENCE because in
647 * some internal structures, we store GC_MALLOCed memory
648 * in IntPtr fields...
650 if (MONO_TYPE_IS_POINTER (field->type)) {
659 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
660 (strcmp (field->name, "$PRIVATE$") == 0)) {
661 /* This field is a hack inserted by MCS to empty structures */
665 size = mono_type_size (field->type, &align);
667 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
668 align = class->packing_size ? MIN (class->packing_size, align): align;
669 class->min_align = MAX (align, class->min_align);
670 field->offset = real_size;
671 field->offset += align - 1;
672 field->offset &= ~(align - 1);
673 real_size = field->offset + size;
676 class->instance_size = MAX (real_size, class->instance_size);
678 if (class->instance_size & (class->min_align - 1)) {
679 class->instance_size += class->min_align - 1;
680 class->instance_size &= ~(class->min_align - 1);
684 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
686 for (i = 0; i < top; i++) {
688 field = &class->fields [i];
691 * There must be info about all the fields in a type if it
692 * uses explicit layout.
695 if (mono_field_is_deleted (field))
697 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
700 size = mono_type_size (field->type, &align);
703 * When we get here, field->offset is already set by the
704 * loader (for either runtime fields or fields loaded from metadata).
705 * The offset is from the start of the object: this works for both
706 * classes and valuetypes.
708 field->offset += sizeof (MonoObject);
713 real_size = MAX (real_size, size + field->offset);
715 class->instance_size = MAX (real_size, class->instance_size);
719 class->size_inited = 1;
722 * Compute static field layout and size
724 for (i = 0; i < top; i++){
726 field = &class->fields [i];
728 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
730 if (mono_field_is_deleted (field))
733 size = mono_type_size (field->type, &align);
734 field->offset = class->class_size;
735 field->offset += align - 1;
736 field->offset &= ~(align - 1);
737 class->class_size = field->offset + size;
742 init_properties (MonoClass *class)
744 guint startm, endm, i, j;
745 guint32 cols [MONO_PROPERTY_SIZE];
746 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
747 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
749 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
750 class->property.count = class->property.last - class->property.first;
752 class->properties = g_new0 (MonoProperty, class->property.count);
753 for (i = class->property.first; i < class->property.last; ++i) {
754 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
755 class->properties [i - class->property.first].parent = class;
756 class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
757 class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
759 startm = mono_metadata_methods_from_property (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_SETTER:
764 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
766 case METHOD_SEMANTIC_GETTER:
767 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
777 init_events (MonoClass *class)
779 guint startm, endm, i, j;
780 guint32 cols [MONO_EVENT_SIZE];
781 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
782 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
784 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
785 class->event.count = class->event.last - class->event.first;
787 class->events = g_new0 (MonoEvent, class->event.count);
788 for (i = class->event.first; i < class->event.last; ++i) {
789 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
790 class->events [i - class->event.first].parent = class;
791 class->events [i - class->event.first].attrs = cols [MONO_EVENT_FLAGS];
792 class->events [i - class->event.first].name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
794 startm = mono_metadata_methods_from_event (class->image, i, &endm);
795 for (j = startm; j < endm; ++j) {
796 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
797 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
798 case METHOD_SEMANTIC_ADD_ON:
799 class->events [i - class->event.first].add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
801 case METHOD_SEMANTIC_REMOVE_ON:
802 class->events [i - class->event.first].remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
804 case METHOD_SEMANTIC_FIRE:
805 class->events [i - class->event.first].raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
807 case METHOD_SEMANTIC_OTHER: /* don't care for now */
816 mono_get_unique_iid (MonoClass *class)
818 static GHashTable *iid_hash = NULL;
819 static guint iid = 0;
824 g_assert (MONO_CLASS_IS_INTERFACE (class));
829 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
831 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
833 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
834 mono_loader_unlock ();
838 g_hash_table_insert (iid_hash, str, (gpointer)iid);
842 mono_loader_unlock ();
848 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
853 for (i = 0; i < klass->interface_count; i++) {
854 ic = klass->interfaces [i];
857 *res = g_ptr_array_new ();
858 g_ptr_array_add (*res, ic);
860 collect_implemented_interfaces_aux (ic, res);
864 static inline GPtrArray*
865 collect_implemented_interfaces (MonoClass *klass)
867 GPtrArray *res = NULL;
869 collect_implemented_interfaces_aux (klass, &res);
874 setup_interface_offsets (MonoClass *class, int cur_slot)
880 /* compute maximum number of slots and maximum interface id */
882 for (k = class; k ; k = k->parent) {
883 for (i = 0; i < k->interface_count; i++) {
884 ic = k->interfaces [i];
887 mono_class_init (ic);
889 if (max_iid < ic->interface_id)
890 max_iid = ic->interface_id;
894 if (MONO_CLASS_IS_INTERFACE (class)) {
895 if (max_iid < class->interface_id)
896 max_iid = class->interface_id;
898 class->max_interface_id = max_iid;
899 /* compute vtable offset for interfaces */
900 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
902 for (i = 0; i <= max_iid; i++)
903 class->interface_offsets [i] = -1;
905 ifaces = collect_implemented_interfaces (class);
907 for (i = 0; i < ifaces->len; ++i) {
908 ic = g_ptr_array_index (ifaces, i);
909 class->interface_offsets [ic->interface_id] = cur_slot;
910 cur_slot += ic->method.count;
912 g_ptr_array_free (ifaces, TRUE);
915 for (k = class->parent; k ; k = k->parent) {
916 ifaces = collect_implemented_interfaces (k);
918 for (i = 0; i < ifaces->len; ++i) {
919 ic = g_ptr_array_index (ifaces, i);
921 if (class->interface_offsets [ic->interface_id] == -1) {
922 int io = k->interface_offsets [ic->interface_id];
926 class->interface_offsets [ic->interface_id] = io;
929 g_ptr_array_free (ifaces, TRUE);
933 if (MONO_CLASS_IS_INTERFACE (class))
934 class->interface_offsets [class->interface_id] = cur_slot;
940 mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
944 int i, max_vtsize = 0, max_iid, cur_slot = 0;
946 MonoGHashTable *override_map = NULL;
948 /* setup_vtable() must be called only once on the type */
949 if (class->interface_offsets) {
950 g_warning ("vtable already computed in %s.%s", class->name_space, class->name);
954 ifaces = collect_implemented_interfaces (class);
956 for (i = 0; i < ifaces->len; i++) {
957 MonoClass *ic = g_ptr_array_index (ifaces, i);
958 max_vtsize += ic->method.count;
960 g_ptr_array_free (ifaces, TRUE);
964 max_vtsize += class->parent->vtable_size;
965 cur_slot = class->parent->vtable_size;
968 max_vtsize += class->method.count;
970 vtable = alloca (sizeof (gpointer) * max_vtsize);
971 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
973 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
975 cur_slot = setup_interface_offsets (class, cur_slot);
976 max_iid = class->max_interface_id;
978 if (class->parent && class->parent->vtable_size)
979 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
981 /* override interface methods */
982 for (i = 0; i < onum; i++) {
983 MonoMethod *decl = overrides [i*2];
984 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
986 g_assert (decl->slot != -1);
987 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
988 vtable [dslot] = overrides [i*2 + 1];
989 vtable [dslot]->slot = dslot;
991 override_map = mono_g_hash_table_new (NULL, NULL);
993 mono_g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
997 for (k = class; k ; k = k->parent) {
999 ifaces = collect_implemented_interfaces (k);
1001 nifaces = ifaces->len;
1002 for (i = 0; i < nifaces; i++) {
1005 ic = g_ptr_array_index (ifaces, i);
1006 io = k->interface_offsets [ic->interface_id];
1009 g_assert (io <= max_vtsize);
1012 for (l = 0; l < ic->method.count; l++) {
1013 MonoMethod *im = ic->methods [l];
1015 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1018 for (j = 0; j < class->method.count; ++j) {
1019 MonoMethod *cm = class->methods [j];
1020 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1021 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
1022 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
1024 if (!strcmp(cm->name, im->name) &&
1025 mono_metadata_signature_equal (cm->signature, im->signature)) {
1026 g_assert (io + l <= max_vtsize);
1027 vtable [io + l] = cm;
1032 /* already implemented */
1033 if (io >= k->vtable_size)
1037 for (l = 0; l < ic->method.count; l++) {
1038 MonoMethod *im = ic->methods [l];
1041 g_assert (io + l <= max_vtsize);
1043 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1046 for (k1 = class; k1; k1 = k1->parent) {
1047 for (j = 0; j < k1->method.count; ++j) {
1048 MonoMethod *cm = k1->methods [j];
1050 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1051 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
1054 if (!strcmp(cm->name, im->name) &&
1055 mono_metadata_signature_equal (cm->signature, im->signature)) {
1056 g_assert (io + l <= max_vtsize);
1057 vtable [io + l] = cm;
1062 g_assert (io + l <= max_vtsize);
1063 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1068 for (l = 0; l < ic->method.count; l++) {
1069 MonoMethod *im = ic->methods [l];
1070 char *qname, *fqname, *cname, *the_cname;
1073 if (vtable [io + l])
1076 if (ic->generic_inst) {
1077 MonoClass *the_ic = mono_class_from_mono_type (ic->generic_inst->generic_type);
1078 the_cname = _mono_type_get_name (&the_ic->byval_arg, TRUE, FALSE);
1085 qname = g_strconcat (cname, ".", im->name, NULL);
1086 if (ic->name_space && ic->name_space [0])
1087 fqname = g_strconcat (ic->name_space, ".", cname, ".", im->name, NULL);
1091 for (k1 = class; k1; k1 = k1->parent) {
1092 for (j = 0; j < k1->method.count; ++j) {
1093 MonoMethod *cm = k1->methods [j];
1095 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1098 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
1099 mono_metadata_signature_equal (cm->signature, im->signature)) {
1100 g_assert (io + l <= max_vtsize);
1101 vtable [io + l] = cm;
1112 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
1113 for (l = 0; l < ic->method.count; l++) {
1115 MonoMethod *im = ic->methods [l];
1116 if (im->flags & METHOD_ATTRIBUTE_STATIC)
1118 g_assert (io + l <= max_vtsize);
1121 * If one of our parents already implements this interface
1122 * we can inherit the implementation.
1124 if (!(vtable [io + l])) {
1125 MonoClass *parent = class->parent;
1127 if ((ic->interface_id <= parent->max_interface_id) &&
1128 (parent->interface_offsets [ic->interface_id]) &&
1130 vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
1133 if (!(vtable [io + l])) {
1134 for (j = 0; j < onum; ++j) {
1135 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
1136 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
1138 msig = mono_signature_get_desc (im->signature, FALSE);
1139 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
1140 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
1142 for (j = 0; j < class->method.count; ++j) {
1143 MonoMethod *cm = class->methods [j];
1144 msig = mono_signature_get_desc (cm->signature, FALSE);
1146 printf ("METHOD %s(%s)\n", cm->name, msig);
1149 g_assert_not_reached ();
1154 for (l = 0; l < ic->method.count; l++) {
1155 MonoMethod *im = vtable [io + l];
1158 g_assert (io + l <= max_vtsize);
1160 /* FIXME: why do we need this ? */
1162 /* g_assert_not_reached (); */
1168 g_ptr_array_free (ifaces, TRUE);
1171 for (i = 0; i < class->method.count; ++i) {
1174 cm = class->methods [i];
1177 * Non-virtual method have no place in the vtable.
1178 * This also catches static methods (since they are not virtual).
1180 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1184 * If the method is REUSE_SLOT, we must check in the
1185 * base class for a method to override.
1187 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
1189 for (k = class->parent; k ; k = k->parent) {
1191 for (j = 0; j < k->method.count; ++j) {
1192 MonoMethod *m1 = k->methods [j];
1193 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
1195 if (!strcmp(cm->name, m1->name) &&
1196 mono_metadata_signature_equal (cm->signature, m1->signature)) {
1197 slot = k->methods [j]->slot;
1198 g_assert (cm->slot < max_vtsize);
1200 override_map = mono_g_hash_table_new (NULL, NULL);
1201 mono_g_hash_table_insert (override_map, m1, cm);
1213 cm->slot = cur_slot++;
1215 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT) && !cm->signature->generic_param_count)
1216 vtable [cm->slot] = cm;
1219 /* override non interface methods */
1220 for (i = 0; i < onum; i++) {
1221 MonoMethod *decl = overrides [i*2];
1222 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
1223 g_assert (decl->slot != -1);
1224 vtable [decl->slot] = overrides [i*2 + 1];
1225 overrides [i * 2 + 1]->slot = decl->slot;
1227 override_map = mono_g_hash_table_new (NULL, NULL);
1228 mono_g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
1233 * If a method occupies more than one place in the vtable, and it is
1234 * overriden, then change the other occurances too.
1237 for (i = 0; i < max_vtsize; ++i)
1239 MonoMethod *cm = mono_g_hash_table_lookup (override_map, vtable [i]);
1244 mono_g_hash_table_destroy (override_map);
1247 if (class->generic_inst) {
1248 MonoClass *gklass = mono_class_from_mono_type (class->generic_inst->generic_type);
1250 mono_class_init (gklass);
1251 class->vtable_size = gklass->vtable_size;
1253 class->vtable_size = cur_slot;
1255 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
1256 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
1258 if (mono_print_vtable) {
1261 for (i = 0; i <= max_iid; i++)
1262 if (class->interface_offsets [i] != -1)
1265 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
1266 class->name, class->vtable_size, icount);
1268 for (i = 0; i < class->vtable_size; ++i) {
1273 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
1274 cm->klass->name_space, cm->klass->name,
1281 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
1282 class->name, max_iid);
1284 for (i = 0; i < class->interface_count; i++) {
1285 ic = class->interfaces [i];
1286 printf (" slot %03d(%03d) %s.%s\n",
1287 class->interface_offsets [ic->interface_id],
1288 ic->method.count, ic->name_space, ic->name);
1291 for (k = class->parent; k ; k = k->parent) {
1292 for (i = 0; i < k->interface_count; i++) {
1293 ic = k->interfaces [i];
1294 printf (" slot %03d(%03d) %s.%s\n",
1295 class->interface_offsets [ic->interface_id],
1296 ic->method.count, ic->name_space, ic->name);
1305 * @class: the class to initialize
1307 * compute the instance_size, class_size and other infos that cannot be
1308 * computed at mono_class_get() time. Also compute a generic vtable and
1309 * the method slot numbers. We use this infos later to create a domain
1313 mono_class_init (MonoClass *class)
1316 static MonoMethod *default_ghc = NULL;
1317 static MonoMethod *default_finalize = NULL;
1318 static int finalize_slot = -1;
1319 static int ghc_slot = -1;
1320 MonoMethod **overrides;
1328 /*g_print ("Init class %s\n", class->name);*/
1330 /* We do everything inside the lock to prevent races */
1331 mono_loader_lock ();
1333 if (class->inited) {
1334 mono_loader_unlock ();
1335 /* Somebody might have gotten in before us */
1339 if (class->init_pending) {
1340 /* this indicates a cyclic dependency */
1341 g_error ("pending init %s.%s\n", class->name_space, class->name);
1344 class->init_pending = 1;
1346 if (mono_debugger_start_class_init_func)
1347 mono_debugger_start_class_init_func (class);
1349 mono_stats.initialized_class_count++;
1351 if (class->generic_inst && !class->generic_inst->is_dynamic) {
1352 MonoGenericInst *ginst = class->generic_inst;
1356 gklass = mono_class_from_mono_type (ginst->generic_type);
1357 mono_class_init (gklass);
1360 class->parent = mono_class_from_mono_type (ginst->parent);
1362 class->parent = gklass->parent;
1364 mono_class_setup_parent (class, class->parent);
1366 if (MONO_CLASS_IS_INTERFACE (class))
1367 class->interface_id = mono_get_unique_iid (class);
1369 class->method = gklass->method;
1370 class->methods = g_new0 (MonoMethod *, class->method.count);
1372 for (i = 0; i < class->method.count; i++)
1373 class->methods [i] = mono_class_inflate_generic_method (
1374 gklass->methods [i], ginst->context, ginst->klass);
1376 class->field = gklass->field;
1377 class->fields = g_new0 (MonoClassField, class->field.count);
1379 for (i = 0; i < class->field.count; i++) {
1380 MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
1381 ifield->generic_type = gklass->fields [i].type;
1383 class->fields [i] = gklass->fields [i];
1384 class->fields [i].generic_info = ifield;
1385 class->fields [i].parent = class;
1386 class->fields [i].type = mono_class_inflate_generic_type (
1387 class->fields [i].type, ginst->context);
1390 class->property = gklass->property;
1391 class->properties = g_new0 (MonoProperty, class->property.count);
1393 for (i = 0; i < class->property.count; i++) {
1394 MonoProperty *prop = &class->properties [i];
1396 *prop = gklass->properties [i];
1399 prop->get = mono_class_inflate_generic_method (
1400 prop->get, ginst->context, ginst->klass);
1402 prop->set = mono_class_inflate_generic_method (
1403 prop->set, ginst->context, ginst->klass);
1405 prop->parent = class;
1408 class->interface_count = gklass->interface_count;
1409 class->interfaces = g_new0 (MonoClass *, class->interface_count);
1410 for (i = 0; i < class->interface_count; i++) {
1411 MonoType *it = &gklass->interfaces [i]->byval_arg;
1412 MonoType *inflated = mono_class_inflate_generic_type (
1413 it, ginst->context);
1414 class->interfaces [i] = mono_class_from_mono_type (inflated);
1415 mono_class_init (class->interfaces [i]);
1418 for (list = gklass->nested_classes; list; list = list->next)
1419 class->nested_classes = g_list_append (
1420 class->nested_classes, list->data);
1423 if (class->parent && !class->parent->inited)
1424 mono_class_init (class->parent);
1427 * Computes the size used by the fields, and their locations
1429 if (!class->size_inited)
1430 class_compute_field_layout (class);
1432 /* initialize method pointers */
1435 MonoMethodSignature *sig;
1436 class->method.count = class->rank > 1? 2: 1;
1437 sig = mono_metadata_signature_alloc (class->image, class->rank);
1438 sig->ret = &mono_defaults.void_class->byval_arg;
1439 sig->pinvoke = TRUE;
1440 for (i = 0; i < class->rank; ++i)
1441 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1443 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1444 ctor->klass = class;
1445 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1446 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1447 ctor->signature = sig;
1448 ctor->name = ".ctor";
1450 class->methods = g_new (MonoMethod*, class->method.count);
1451 class->methods [0] = ctor;
1452 if (class->rank > 1) {
1453 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
1454 sig->ret = &mono_defaults.void_class->byval_arg;
1455 sig->pinvoke = TRUE;
1456 for (i = 0; i < class->rank * 2; ++i)
1457 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1459 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1460 ctor->klass = class;
1461 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1462 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1463 ctor->signature = sig;
1464 ctor->name = ".ctor";
1466 class->methods [1] = ctor;
1469 if (!class->generic_inst && !class->methods) {
1470 class->methods = g_new (MonoMethod*, class->method.count);
1471 for (i = 0; i < class->method.count; ++i) {
1472 class->methods [i] = mono_get_method (class->image,
1473 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
1478 if (!class->generic_inst) {
1479 init_properties (class);
1480 init_events (class);
1482 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
1485 guint32 cols [MONO_NESTED_CLASS_SIZE];
1486 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
1487 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
1488 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
1490 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
1494 mono_class_setup_supertypes (class);
1496 if (MONO_CLASS_IS_INTERFACE (class)) {
1497 for (i = 0; i < class->method.count; ++i)
1498 class->methods [i]->slot = i;
1499 class->init_pending = 0;
1502 * class->interface_offsets is needed for the castclass/isinst code, so
1503 * we have to setup them for interfaces, too.
1505 setup_interface_offsets (class, 0);
1506 mono_loader_unlock ();
1508 if (mono_debugger_class_init_func)
1509 mono_debugger_class_init_func (class);
1514 overrides = mono_class_get_overrides (class->image, class->type_token, &onum);
1515 mono_class_setup_vtable (class, overrides, onum);
1519 class->init_pending = 0;
1522 if (class == mono_defaults.object_class) {
1524 for (i = 0; i < class->vtable_size; ++i) {
1525 MonoMethod *cm = class->vtable [i];
1527 if (!strcmp (cm->name, "GetHashCode")) {
1533 g_assert (ghc_slot > 0);
1535 default_ghc = class->vtable [ghc_slot];
1540 if (class->parent) {
1542 if (class->vtable [ghc_slot] == default_ghc) {
1547 if (!default_finalize) {
1548 if (class == mono_defaults.object_class) {
1550 for (i = 0; i < class->vtable_size; ++i) {
1551 MonoMethod *cm = class->vtable [i];
1553 if (!strcmp (cm->name, "Finalize")) {
1559 g_assert (finalize_slot > 0);
1561 default_finalize = class->vtable [finalize_slot];
1565 /* Object::Finalize should have empty implemenatation */
1566 class->has_finalize = 0;
1567 if (class->parent) {
1568 if (class->vtable [finalize_slot] != default_finalize)
1569 class->has_finalize = 1;
1572 mono_loader_unlock ();
1574 if (mono_debugger_class_init_func)
1575 mono_debugger_class_init_func (class);
1579 mono_class_setup_mono_type (MonoClass *class)
1581 const char *name = class->name;
1582 const char *nspace = class->name_space;
1584 if (MONO_CLASS_IS_INTERFACE (class))
1585 class->interface_id = mono_get_unique_iid (class);
1587 class->this_arg.byref = 1;
1588 class->this_arg.data.klass = class;
1589 class->this_arg.type = MONO_TYPE_CLASS;
1590 class->byval_arg.data.klass = class;
1591 class->byval_arg.type = MONO_TYPE_CLASS;
1593 if (!strcmp (nspace, "System")) {
1594 if (!strcmp (name, "ValueType")) {
1596 * do not set the valuetype bit for System.ValueType.
1597 * class->valuetype = 1;
1599 class->blittable = TRUE;
1600 } else if (!strcmp (name, "Enum")) {
1602 * do not set the valuetype bit for System.Enum.
1603 * class->valuetype = 1;
1605 class->valuetype = 0;
1606 class->enumtype = 0;
1607 } else if (!strcmp (name, "Object")) {
1608 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
1609 } else if (!strcmp (name, "String")) {
1610 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
1611 } else if (!strcmp (name, "TypedReference")) {
1612 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
1616 if (class->valuetype) {
1617 int t = MONO_TYPE_VALUETYPE;
1618 if (!strcmp (nspace, "System")) {
1621 if (!strcmp (name, "Boolean")) {
1622 t = MONO_TYPE_BOOLEAN;
1623 } else if (!strcmp(name, "Byte")) {
1625 class->blittable = TRUE;
1629 if (!strcmp (name, "Char")) {
1634 if (!strcmp (name, "Double")) {
1636 class->blittable = TRUE;
1640 if (!strcmp (name, "Int32")) {
1642 class->blittable = TRUE;
1643 } else if (!strcmp(name, "Int16")) {
1645 class->blittable = TRUE;
1646 } else if (!strcmp(name, "Int64")) {
1648 class->blittable = TRUE;
1649 } else if (!strcmp(name, "IntPtr")) {
1651 class->blittable = TRUE;
1655 if (!strcmp (name, "Single")) {
1657 class->blittable = TRUE;
1658 } else if (!strcmp(name, "SByte")) {
1660 class->blittable = TRUE;
1664 if (!strcmp (name, "UInt32")) {
1666 class->blittable = TRUE;
1667 } else if (!strcmp(name, "UInt16")) {
1669 class->blittable = TRUE;
1670 } else if (!strcmp(name, "UInt64")) {
1672 class->blittable = TRUE;
1673 } else if (!strcmp(name, "UIntPtr")) {
1675 class->blittable = TRUE;
1679 if (!strcmp (name, "TypedReference")) {
1680 t = MONO_TYPE_TYPEDBYREF;
1681 class->blittable = TRUE;
1685 if (!strcmp (name, "Void")) {
1693 class->this_arg.type = class->byval_arg.type = t;
1698 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
1700 gboolean system_namespace;
1702 system_namespace = !strcmp (class->name_space, "System");
1704 /* if root of the hierarchy */
1705 if (system_namespace && !strcmp (class->name, "Object")) {
1706 class->parent = NULL;
1707 class->instance_size = sizeof (MonoObject);
1710 if (!strcmp (class->name, "<Module>")) {
1711 class->parent = NULL;
1712 class->instance_size = 0;
1716 if (!MONO_CLASS_IS_INTERFACE (class)) {
1717 class->parent = parent;
1720 g_assert_not_reached (); /* FIXME */
1722 if (parent->generic_inst && !parent->name) {
1724 * If the parent is a generic instance, we may get
1725 * called before it is fully initialized, especially
1726 * before it has its name.
1731 class->marshalbyref = parent->marshalbyref;
1732 class->contextbound = parent->contextbound;
1733 class->delegate = parent->delegate;
1735 if (system_namespace) {
1736 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
1737 class->marshalbyref = 1;
1739 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
1740 class->contextbound = 1;
1742 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
1743 class->delegate = 1;
1746 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
1747 (strcmp (class->parent->name_space, "System") == 0)))
1748 class->valuetype = 1;
1749 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
1750 class->valuetype = class->enumtype = 1;
1752 /*class->enumtype = class->parent->enumtype; */
1753 mono_class_setup_supertypes (class);
1755 class->parent = NULL;
1761 mono_class_setup_supertypes (MonoClass *class)
1766 if (class->supertypes)
1770 for (k = class; k ; k = k->parent) {
1774 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
1775 class->supertypes = g_new0 (MonoClass *, ms);
1777 if (class->parent) {
1778 for (i = class->idepth, k = class; k ; k = k->parent)
1779 class->supertypes [--i] = k;
1781 class->supertypes [0] = class;
1786 * @image: context where the image is created
1787 * @type_token: typedef token
1790 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
1792 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
1793 MonoClass *class, *parent = NULL;
1794 guint32 cols [MONO_TYPEDEF_SIZE];
1795 guint32 cols_next [MONO_TYPEDEF_SIZE];
1796 guint tidx = mono_metadata_token_index (type_token);
1797 const char *name, *nspace;
1799 MonoClass **interfaces;
1801 mono_loader_lock ();
1803 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
1804 mono_loader_unlock ();
1808 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
1810 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
1812 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1813 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1815 class = g_malloc0 (sizeof (MonoClass));
1818 class->name_space = nspace;
1820 class->image = image;
1821 class->type_token = type_token;
1822 class->flags = cols [MONO_TYPEDEF_FLAGS];
1824 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
1826 if (cols [MONO_TYPEDEF_EXTENDS])
1827 parent = mono_class_get (image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]));
1828 interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
1830 class->interfaces = interfaces;
1831 class->interface_count = icount;
1833 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
1835 /* fixme: maybe we must set this on windows
1836 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
1840 class->cast_class = class->element_class = class;
1842 /*g_print ("Load class %s\n", name);*/
1844 mono_class_setup_parent (class, parent);
1846 mono_class_setup_mono_type (class);
1849 * Compute the field and method lists
1851 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
1852 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
1854 if (tt->rows > tidx){
1855 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
1856 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1857 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1859 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
1860 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
1863 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
1864 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
1865 class->field.count = class->field.last - class->field.first;
1867 class->field.count = 0;
1869 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
1870 class->method.count = class->method.last - class->method.first;
1872 class->method.count = 0;
1874 /* reserve space to store vector pointer in arrays */
1875 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
1876 class->instance_size += 2 * sizeof (gpointer);
1877 g_assert (class->field.count == 0);
1880 if (class->enumtype)
1881 class_compute_field_layout (class);
1883 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
1884 class->nested_in = mono_class_create_from_typedef (image, type_token);
1886 class->gen_params = mono_metadata_load_generic_params (image, class->type_token, &icount);
1887 class->num_gen_params = icount;
1889 mono_loader_unlock ();
1895 mono_class_create_generic (MonoGenericInst *ginst)
1897 MonoClass *klass, *gklass;
1900 ginst->klass = g_malloc0 (sizeof (MonoClass));
1901 klass = ginst->klass;
1903 gklass = mono_class_from_mono_type (ginst->generic_type);
1905 klass->nested_in = gklass->nested_in;
1907 klass->name = gklass->name;
1908 klass->name_space = gklass->name_space;
1909 klass->image = gklass->image;
1910 klass->flags = gklass->flags;
1912 klass->generic_inst = ginst;
1914 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
1915 klass->this_arg.data.generic_inst = klass->byval_arg.data.generic_inst = ginst;
1916 klass->this_arg.byref = TRUE;
1918 klass->cast_class = klass->element_class = klass;
1920 if (ginst->is_dynamic) {
1921 klass->instance_size = gklass->instance_size;
1922 klass->class_size = gklass->class_size;
1923 klass->size_inited = 1;
1925 klass->valuetype = gklass->valuetype;
1932 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
1934 MonoClass *klass, **ptr;
1938 return param->pklass;
1940 klass = param->pklass = g_new0 (MonoClass, 1);
1942 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
1946 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
1947 klass->parent = param->constraints [0];
1951 if (count - pos > 0) {
1952 klass->interface_count = count - pos;
1953 klass->interfaces = g_new0 (MonoClass *, count - pos);
1954 for (i = pos; i < count; i++)
1955 klass->interfaces [i - pos] = param->constraints [i];
1958 g_assert (param->name);
1960 klass->name = param->name;
1961 klass->name_space = "";
1962 klass->image = image;
1963 klass->cast_class = klass->element_class = klass;
1964 klass->enum_basetype = &klass->element_class->byval_arg;
1965 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
1967 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
1968 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
1969 klass->this_arg.byref = TRUE;
1971 mono_class_init (klass);
1977 my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
1982 return param->pklass;
1984 klass = g_new0 (MonoClass, 1);
1987 klass->name = param->name;
1989 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
1990 klass->name_space = "";
1991 klass->image = mono_defaults.corlib;
1992 klass->cast_class = klass->element_class = klass;
1993 klass->enum_basetype = &klass->element_class->byval_arg;
1994 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
1996 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
1997 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
1998 klass->this_arg.byref = TRUE;
2000 mono_class_init (klass);
2006 mono_ptr_class_get (MonoType *type)
2009 MonoClass *el_class;
2010 static GHashTable *ptr_hash = NULL;
2012 mono_loader_lock ();
2015 ptr_hash = g_hash_table_new (NULL, NULL);
2016 el_class = mono_class_from_mono_type (type);
2017 if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
2018 mono_loader_unlock ();
2021 result = g_new0 (MonoClass, 1);
2023 result->parent = NULL; /* no parent for PTR types */
2024 result->name_space = el_class->name_space;
2025 result->name = g_strdup_printf ("%s*", el_class->name);
2026 result->image = el_class->image;
2027 result->inited = TRUE;
2028 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2029 /* Can pointers get boxed? */
2030 result->instance_size = sizeof (gpointer);
2031 result->cast_class = result->element_class = el_class;
2032 result->enum_basetype = &result->element_class->byval_arg;
2033 result->blittable = TRUE;
2035 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
2036 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
2037 result->this_arg.byref = TRUE;
2039 mono_class_setup_supertypes (result);
2041 g_hash_table_insert (ptr_hash, el_class, result);
2043 mono_loader_unlock ();
2049 mono_fnptr_class_get (MonoMethodSignature *sig)
2052 static GHashTable *ptr_hash = NULL;
2054 mono_loader_lock ();
2057 ptr_hash = g_hash_table_new (NULL, NULL);
2059 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
2060 mono_loader_unlock ();
2063 result = g_new0 (MonoClass, 1);
2065 result->parent = NULL; /* no parent for PTR types */
2066 result->name = "System";
2067 result->name_space = "MonoFNPtrFakeClass";
2068 result->image = NULL; /* need to fix... */
2069 result->inited = TRUE;
2070 result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
2071 /* Can pointers get boxed? */
2072 result->instance_size = sizeof (gpointer);
2073 result->cast_class = result->element_class = result;
2074 result->blittable = TRUE;
2076 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
2077 result->this_arg.data.method = result->byval_arg.data.method = sig;
2078 result->this_arg.byref = TRUE;
2079 result->enum_basetype = &result->element_class->byval_arg;
2080 result->blittable = TRUE;
2082 mono_class_setup_supertypes (result);
2084 g_hash_table_insert (ptr_hash, sig, result);
2086 mono_loader_unlock ();
2092 mono_class_from_mono_type (MonoType *type)
2094 switch (type->type) {
2095 case MONO_TYPE_OBJECT:
2096 return type->data.klass? type->data.klass: mono_defaults.object_class;
2097 case MONO_TYPE_VOID:
2098 return type->data.klass? type->data.klass: mono_defaults.void_class;
2099 case MONO_TYPE_BOOLEAN:
2100 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
2101 case MONO_TYPE_CHAR:
2102 return type->data.klass? type->data.klass: mono_defaults.char_class;
2104 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
2106 return type->data.klass? type->data.klass: mono_defaults.byte_class;
2108 return type->data.klass? type->data.klass: mono_defaults.int16_class;
2110 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
2112 return type->data.klass? type->data.klass: mono_defaults.int32_class;
2114 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
2116 return type->data.klass? type->data.klass: mono_defaults.int_class;
2118 return type->data.klass? type->data.klass: mono_defaults.uint_class;
2120 return type->data.klass? type->data.klass: mono_defaults.int64_class;
2122 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
2124 return type->data.klass? type->data.klass: mono_defaults.single_class;
2126 return type->data.klass? type->data.klass: mono_defaults.double_class;
2127 case MONO_TYPE_STRING:
2128 return type->data.klass? type->data.klass: mono_defaults.string_class;
2129 case MONO_TYPE_TYPEDBYREF:
2130 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
2131 case MONO_TYPE_ARRAY:
2132 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
2134 return mono_ptr_class_get (type->data.type);
2135 case MONO_TYPE_FNPTR:
2136 return mono_fnptr_class_get (type->data.method);
2137 case MONO_TYPE_SZARRAY:
2138 return mono_array_class_get (type->data.klass, 1);
2139 case MONO_TYPE_CLASS:
2140 case MONO_TYPE_VALUETYPE:
2141 return type->data.klass;
2142 case MONO_TYPE_GENERICINST:
2143 g_assert (type->data.generic_inst->klass);
2144 return type->data.generic_inst->klass;
2146 return my_mono_class_from_generic_parameter (type->data.generic_param, FALSE);
2147 case MONO_TYPE_MVAR:
2148 return my_mono_class_from_generic_parameter (type->data.generic_param, TRUE);
2150 g_warning ("implement me 0x%02x\n", type->type);
2151 g_assert_not_reached ();
2158 * @image: context where the image is created
2159 * @type_spec: typespec token
2162 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec,
2163 MonoGenericContext *context)
2165 MonoType *type, *inflated;
2168 type = mono_type_create_from_typespec (image, type_spec);
2170 switch (type->type) {
2171 case MONO_TYPE_ARRAY:
2172 class = mono_array_class_get (type->data.array->eklass, type->data.array->rank);
2174 case MONO_TYPE_SZARRAY:
2175 class = mono_array_class_get (type->data.klass, 1);
2178 class = mono_ptr_class_get (type->data.type);
2180 case MONO_TYPE_GENERICINST:
2181 g_assert (type->data.generic_inst->klass);
2182 class = type->data.generic_inst->klass;
2185 /* it seems any type can be stored in TypeSpec as well */
2186 class = mono_class_from_mono_type (type);
2190 if (!class || !context)
2193 inflated = mono_class_inflate_generic_type (&class->byval_arg, context);
2195 return mono_class_from_mono_type (inflated);
2199 * mono_bounded_array_class_get:
2200 * @element_class: element class
2201 * @rank: the dimension of the array class
2202 * @bounded: whenever the array has non-zero bounds
2204 * Returns: a class object describing the array with element type @element_type and
2208 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
2212 MonoClass *parent = NULL;
2213 GSList *list, *rootlist;
2216 gboolean corlib_type = FALSE;
2218 g_assert (rank <= 255);
2221 /* bounded only matters for one-dimensional arrays */
2224 image = eclass->image;
2226 mono_loader_lock ();
2228 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
2229 for (; list; list = list->next) {
2231 if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
2232 mono_loader_unlock ();
2238 /* for the building corlib use System.Array from it */
2239 if (image->assembly && image->assembly->dynamic && strcmp (image->assembly_name, "mscorlib") == 0) {
2240 parent = mono_class_from_name (image, "System", "Array");
2243 parent = mono_defaults.array_class;
2244 if (!parent->inited)
2245 mono_class_init (parent);
2248 class = g_malloc0 (sizeof (MonoClass));
2250 class->image = image;
2251 class->name_space = eclass->name_space;
2252 nsize = strlen (eclass->name);
2253 name = g_malloc (nsize + 2 + rank);
2254 memcpy (name, eclass->name, nsize);
2257 memset (name + nsize + 1, ',', rank - 1);
2258 name [nsize + rank] = ']';
2259 name [nsize + rank + 1] = 0;
2261 class->type_token = 0;
2262 /* all arrays are marked serializable and sealed, bug #42779 */
2263 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
2264 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2265 class->parent = parent;
2266 class->instance_size = mono_class_instance_size (class->parent);
2267 class->class_size = 0;
2268 mono_class_setup_supertypes (class);
2272 if (eclass->enumtype)
2273 class->cast_class = eclass->element_class;
2275 class->cast_class = eclass;
2277 class->element_class = eclass;
2279 if ((rank > 1) || bounded) {
2280 MonoArrayType *at = g_new0 (MonoArrayType, 1);
2281 class->byval_arg.type = MONO_TYPE_ARRAY;
2282 class->byval_arg.data.array = at;
2283 at->eklass = eclass;
2285 /* FIXME: complete.... */
2287 class->byval_arg.type = MONO_TYPE_SZARRAY;
2288 class->byval_arg.data.klass = eclass;
2290 class->this_arg = class->byval_arg;
2291 class->this_arg.byref = 1;
2296 list = g_slist_append (rootlist, class);
2297 g_hash_table_insert (image->array_cache, eclass, list);
2299 mono_loader_unlock ();
2305 * mono_array_class_get:
2306 * @element_class: element class
2307 * @rank: the dimension of the array class
2309 * Returns: a class object describing the array with element type @element_type and
2313 mono_array_class_get (MonoClass *eclass, guint32 rank)
2315 return mono_bounded_array_class_get (eclass, rank, FALSE);
2319 * mono_class_instance_size:
2322 * Returns: the size of an object instance
2325 mono_class_instance_size (MonoClass *klass)
2327 if (!klass->size_inited)
2328 mono_class_init (klass);
2330 g_assert (!klass->gen_params &&
2331 (!klass->generic_inst || !klass->generic_inst->is_open));
2332 return klass->instance_size;
2336 * mono_class_min_align:
2339 * Returns: minimm alignment requirements
2342 mono_class_min_align (MonoClass *klass)
2344 if (!klass->size_inited)
2345 mono_class_init (klass);
2347 return klass->min_align;
2351 * mono_class_value_size:
2354 * This function is used for value types, and return the
2355 * space and the alignment to store that kind of value object.
2357 * Returns: the size of a value of kind @klass
2360 mono_class_value_size (MonoClass *klass, guint32 *align)
2364 /* fixme: check disable, because we still have external revereces to
2365 * mscorlib and Dummy Objects
2367 /*g_assert (klass->valuetype);*/
2369 size = mono_class_instance_size (klass) - sizeof (MonoObject);
2372 *align = klass->min_align;
2378 * mono_class_data_size:
2381 * Returns: the size of the static class data
2384 mono_class_data_size (MonoClass *klass)
2387 mono_class_init (klass);
2389 return klass->class_size;
2393 * Auxiliary routine to mono_class_get_field
2395 * Takes a field index instead of a field token.
2397 static MonoClassField *
2398 mono_class_get_field_idx (MonoClass *class, int idx)
2400 if (class->field.count){
2401 if ((idx >= class->field.first) && (idx < class->field.last)){
2402 return &class->fields [idx - class->field.first];
2409 return mono_class_get_field_idx (class->parent, idx);
2413 * mono_class_get_field:
2414 * @class: the class to lookup the field.
2415 * @field_token: the field token
2417 * Returns: A MonoClassField representing the type and offset of
2418 * the field, or a NULL value if the field does not belong to this
2422 mono_class_get_field (MonoClass *class, guint32 field_token)
2424 int idx = mono_metadata_token_index (field_token);
2426 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
2428 return mono_class_get_field_idx (class, idx - 1);
2432 mono_class_get_field_from_name (MonoClass *klass, const char *name)
2437 for (i = 0; i < klass->field.count; ++i) {
2438 if (strcmp (name, klass->fields [i].name) == 0)
2439 return &klass->fields [i];
2441 klass = klass->parent;
2447 mono_class_get_field_token (MonoClassField *field)
2449 MonoClass *klass = field->parent;
2453 for (i = 0; i < klass->field.count; ++i) {
2454 if (&klass->fields [i] == field)
2455 return mono_metadata_make_token (MONO_TABLE_FIELD, klass->field.first + i + 1);
2457 klass = klass->parent;
2460 g_assert_not_reached ();
2465 mono_class_get_event_token (MonoEvent *event)
2467 MonoClass *klass = event->parent;
2471 for (i = 0; i < klass->event.count; ++i) {
2472 if (&klass->events [i] == event)
2473 return mono_metadata_make_token (MONO_TABLE_EVENT, klass->event.first + i + 1);
2475 klass = klass->parent;
2478 g_assert_not_reached ();
2483 mono_vtable_get_static_field_data (MonoVTable *vt)
2489 mono_class_get_property_from_name (MonoClass *klass, const char *name)
2494 for (i = 0; i < klass->property.count; ++i) {
2495 if (strcmp (name, klass->properties [i].name) == 0)
2496 return &klass->properties [i];
2498 klass = klass->parent;
2504 mono_class_get_property_token (MonoProperty *prop)
2506 MonoClass *klass = prop->parent;
2510 for (i = 0; i < klass->property.count; ++i) {
2511 if (&klass->properties [i] == prop)
2512 return mono_metadata_make_token (MONO_TABLE_PROPERTY, klass->property.first + i + 1);
2514 klass = klass->parent;
2517 g_assert_not_reached ();
2523 * @image: the image where the class resides
2524 * @type_token: the token for the class
2525 * @at: an optional pointer to return the array element type
2527 * Returns: the MonoClass that represents @type_token in @image
2530 mono_class_get (MonoImage *image, guint32 type_token)
2532 MonoClass *class = NULL;
2535 return mono_lookup_dynamic_token (image, type_token);
2537 switch (type_token & 0xff000000){
2538 case MONO_TOKEN_TYPE_DEF:
2539 class = mono_class_create_from_typedef (image, type_token);
2541 case MONO_TOKEN_TYPE_REF:
2542 class = mono_class_from_typeref (image, type_token);
2544 case MONO_TOKEN_TYPE_SPEC:
2545 class = mono_class_create_from_typespec (image, type_token, NULL);
2548 g_warning ("unknown token type %x", type_token & 0xff000000);
2549 g_assert_not_reached ();
2553 g_warning ("Could not load class from token 0x%08x in %s", type_token, image->name);
2559 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
2561 MonoClass *class = mono_class_get (image, type_token);
2564 if (!class || !context)
2567 switch (class->byval_arg.type) {
2568 case MONO_TYPE_GENERICINST:
2569 if (!class->generic_inst->is_open)
2573 case MONO_TYPE_MVAR:
2579 inflated = inflate_generic_type (&class->byval_arg, context);
2583 return mono_class_from_mono_type (inflated);
2587 * mono_class_from_name_case:
2588 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
2589 * @name_space: the type namespace
2590 * @name: the type short name.
2592 * Obtains a MonoClass with a given namespace and a given name which
2593 * is located in the given MonoImage. The namespace and name
2594 * lookups are case insensitive.
2596 * You can also pass `NULL' to the image, and that will lookup for
2597 * a type with the given namespace and name in all of the loaded
2598 * assemblies: notice that since there might be a name clash in this
2599 * case, passing NULL is not encouraged if you need a precise type.
2603 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
2605 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
2606 guint32 cols [MONO_TYPEDEF_SIZE];
2611 /* add a cache if needed */
2612 for (i = 1; i <= t->rows; ++i) {
2613 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
2614 /* nested types are accessed from the nesting name */
2615 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
2616 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
2618 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2619 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2620 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
2621 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
2627 return_nested_in (MonoClass *class, char *nested) {
2629 char *s = strchr (nested, '/');
2636 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
2638 if (strcmp (found->name, nested) == 0) {
2640 return return_nested_in (found, s);
2649 * mono_class_from_name_case:
2650 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
2651 * @name_space: the type namespace
2652 * @name: the type short name.
2654 * Obtains a MonoClass with a given namespace and a given name which
2655 * is located in the given MonoImage.
2657 * You can also pass `NULL' to the image, and that will lookup for
2658 * a type with the given namespace and name in all of the loaded
2659 * assemblies: notice that since there might be a name clash in this
2660 * case, passing NULL is not encouraged if you need a precise type.
2664 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
2666 GHashTable *nspace_table;
2667 MonoImage *loaded_image;
2673 if ((nested = strchr (name, '/'))) {
2674 int pos = nested - name;
2675 int len = strlen (name);
2678 memcpy (buf, name, len + 1);
2680 nested = buf + pos + 1;
2684 mono_loader_lock ();
2686 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
2689 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
2691 mono_loader_unlock ();
2696 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
2697 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
2698 guint32 cols [MONO_EXP_TYPE_SIZE];
2701 idx = mono_metadata_token_index (token);
2703 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
2705 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
2706 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
2707 loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
2710 class = mono_class_from_name (loaded_image, name_space, name);
2712 return return_nested_in (class, nested);
2715 g_error ("not yet implemented");
2719 token = MONO_TOKEN_TYPE_DEF | token;
2721 class = mono_class_get (image, token);
2723 return return_nested_in (class, nested);
2728 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
2729 gboolean check_interfaces)
2732 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
2733 if ((klassc->interface_id <= klass->max_interface_id) &&
2734 (klass->interface_offsets [klassc->interface_id] >= 0))
2736 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
2739 for (i = 0; i < klass->interface_count; i ++) {
2740 MonoClass *ic = klass->interfaces [i];
2745 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
2750 * MS.NET thinks interfaces are a subclass of Object, so we think it as
2753 if (klassc == mono_defaults.object_class)
2756 if (klass->generic_inst) {
2757 MonoType *parent = klass->generic_inst->parent;
2761 if (mono_metadata_type_equal (parent, &klassc->byval_arg))
2763 klass = mono_class_from_mono_type (parent);
2771 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
2774 mono_class_init (klass);
2776 if (!oklass->inited)
2777 mono_class_init (oklass);
2779 if (MONO_CLASS_IS_INTERFACE (klass)) {
2780 if ((klass->interface_id <= oklass->max_interface_id) &&
2781 (oklass->interface_offsets [klass->interface_id] != -1))
2785 MonoClass *eclass, *eoclass;
2787 if (oklass->rank != klass->rank)
2790 /* vectors vs. one dimensional arrays */
2791 if (oklass->byval_arg.type != klass->byval_arg.type)
2794 eclass = klass->cast_class;
2795 eoclass = oklass->cast_class;
2799 * a is b does not imply a[] is b[] when a is a valuetype, and
2800 * b is a reference type.
2803 if (eoclass->valuetype) {
2804 if ((eclass == mono_defaults.enum_class) ||
2805 (eclass == mono_defaults.enum_class->parent) ||
2806 (eclass == mono_defaults.object_class))
2810 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
2813 if (klass == mono_defaults.object_class)
2816 return mono_class_has_parent (oklass, klass);
2820 * mono_class_needs_cctor_run:
2822 * Determines whenever the class has a static constructor and whenever it
2823 * needs to be called when executing CALLER.
2826 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
2831 for (i = 0; i < klass->method.count; ++i) {
2832 method = klass->methods [i];
2833 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2834 (strcmp (".cctor", method->name) == 0)) {
2835 if (caller == method)
2844 * Returns the nnumber of bytes an element of type klass
2845 * uses when stored into an array.
2848 mono_class_array_element_size (MonoClass *klass)
2850 MonoType *type = &klass->byval_arg;
2853 switch (type->type) {
2856 case MONO_TYPE_BOOLEAN:
2860 case MONO_TYPE_CHAR:
2869 case MONO_TYPE_CLASS:
2870 case MONO_TYPE_STRING:
2871 case MONO_TYPE_OBJECT:
2872 case MONO_TYPE_SZARRAY:
2873 case MONO_TYPE_ARRAY:
2875 case MONO_TYPE_MVAR:
2876 return sizeof (gpointer);
2881 case MONO_TYPE_VALUETYPE:
2882 if (type->data.klass->enumtype) {
2883 type = type->data.klass->enum_basetype;
2884 klass = klass->element_class;
2887 return mono_class_instance_size (klass) - sizeof (MonoObject);
2888 case MONO_TYPE_GENERICINST:
2889 type = type->data.generic_inst->generic_type;
2892 g_error ("unknown type 0x%02x in mono_class_array_element_size", type->type);
2898 * mono_array_element_size:
2899 * @ac: pointer to a #MonoArrayClass
2901 * Returns: the size of single array element.
2904 mono_array_element_size (MonoClass *ac)
2906 return mono_class_array_element_size (ac->element_class);
2910 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
2911 MonoGenericContext *context)
2913 if (image->dynamic) {
2914 gpointer obj = mono_lookup_dynamic_token (image, token);
2916 switch (token & 0xff000000) {
2917 case MONO_TOKEN_TYPE_DEF:
2918 case MONO_TOKEN_TYPE_REF:
2919 case MONO_TOKEN_TYPE_SPEC:
2921 *handle_class = mono_defaults.typehandle_class;
2922 return &((MonoClass*)obj)->byval_arg;
2923 case MONO_TOKEN_METHOD_DEF:
2925 *handle_class = mono_defaults.methodhandle_class;
2927 case MONO_TOKEN_FIELD_DEF:
2929 *handle_class = mono_defaults.fieldhandle_class;
2932 g_assert_not_reached ();
2936 switch (token & 0xff000000) {
2937 case MONO_TOKEN_TYPE_DEF:
2938 case MONO_TOKEN_TYPE_REF: {
2941 *handle_class = mono_defaults.typehandle_class;
2942 class = mono_class_get_full (image, token, context);
2943 mono_class_init (class);
2944 /* We return a MonoType* as handle */
2945 return &class->byval_arg;
2947 case MONO_TOKEN_TYPE_SPEC: {
2950 *handle_class = mono_defaults.typehandle_class;
2951 class = mono_class_create_from_typespec (image, token, context);
2952 mono_class_init (class);
2953 return &class->byval_arg;
2955 case MONO_TOKEN_FIELD_DEF: {
2957 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
2958 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
2959 mono_class_init (class);
2961 *handle_class = mono_defaults.fieldhandle_class;
2962 return mono_class_get_field (class, token);
2964 case MONO_TOKEN_METHOD_DEF: {
2966 meth = mono_get_method_full (image, token, NULL, context);
2968 *handle_class = mono_defaults.methodhandle_class;
2971 case MONO_TOKEN_MEMBER_REF: {
2972 guint32 cols [MONO_MEMBERREF_SIZE];
2974 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
2975 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
2976 mono_metadata_decode_blob_size (sig, &sig);
2977 if (*sig == 0x6) { /* it's a field */
2979 MonoClassField *field;
2980 field = mono_field_from_token (image, token, &klass, context);
2982 *handle_class = mono_defaults.fieldhandle_class;
2986 meth = mono_get_method_full (image, token, NULL, context);
2988 *handle_class = mono_defaults.methodhandle_class;
2993 g_warning ("Unknown token 0x%08x in ldtoken", token);
3000 * This function might need to call runtime functions so it can't be part
3001 * of the metadata library.
3003 static MonoLookupDynamicToken lookup_dynamic = NULL;
3006 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
3008 lookup_dynamic = func;
3012 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
3014 return lookup_dynamic (image, token);
3018 mono_class_get_image (MonoClass *klass)
3020 return klass->image;
3024 * mono_class_get_element_class:
3025 * @klass: the MonoClass to act on
3027 * Returns the element class of an array or an enumeration.
3030 mono_class_get_element_class (MonoClass *klass)
3032 return klass->element_class;
3036 * mono_class_is_valuetype:
3037 * @klass: the MonoClass to act on
3039 * Returns true if the MonoClass represents a ValueType.
3042 mono_class_is_valuetype (MonoClass *klass)
3044 return klass->valuetype;
3048 * mono_class_is_enum:
3049 * @klass: the MonoClass to act on
3051 * Returns true if the MonoClass represents an enumeration.
3054 mono_class_is_enum (MonoClass *klass)
3056 return klass->enumtype;
3060 * mono_class_enum_basetype:
3061 * @klass: the MonoClass to act on
3063 * Returns the underlying type representation for an enumeration.
3066 mono_class_enum_basetype (MonoClass *klass)
3068 return klass->enum_basetype;
3072 * mono_class_get_parent
3073 * @klass: the MonoClass to act on
3075 * Returns the parent class for this class.
3078 mono_class_get_parent (MonoClass *klass)
3080 return klass->parent;
3084 * mono_class_get_nesting_type;
3085 * @klass: the MonoClass to act on
3087 * Returns the container type where this type is nested or NULL if this type is not a nested type.
3090 mono_class_get_nesting_type (MonoClass *klass)
3092 return klass->nested_in;
3096 * mono_class_get_rank:
3097 * @klass: the MonoClass to act on
3099 * Returns the rank for the array (the number of dimensions).
3102 mono_class_get_rank (MonoClass *klass)
3108 * mono_class_get_flags:
3109 * @klass: the MonoClass to act on
3111 * The type flags from the TypeDef table from the metadata.
3112 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
3115 * Returns the flags from the TypeDef table.
3118 mono_class_get_flags (MonoClass *klass)
3120 return klass->flags;
3124 * mono_class_get_name
3125 * @klass: the MonoClass to act on
3127 * Returns the name of the class.
3130 mono_class_get_name (MonoClass *klass)
3136 * mono_class_get_namespace:
3137 * @klass: the MonoClass to act on
3139 * Returns the namespace of the class.
3142 mono_class_get_namespace (MonoClass *klass)
3144 return klass->name_space;
3148 * mono_class_get_type:
3149 * @klass: the MonoClass to act on
3151 * This method returns the internal Type representation for the class.
3153 * Returns the MonoType from the class.
3156 mono_class_get_type (MonoClass *klass)
3158 return &klass->byval_arg;
3162 * mono_class_get_byref_type:
3163 * @klass: the MonoClass to act on
3168 mono_class_get_byref_type (MonoClass *klass)
3170 return &klass->this_arg;
3174 * mono_class_num_fields:
3175 * @klass: the MonoClass to act on
3177 * Returns the number of static and instance fields in the class.
3180 mono_class_num_fields (MonoClass *klass)
3182 return klass->field.count;
3186 * mono_class_num_methods:
3187 * @klass: the MonoClass to act on
3189 * Returns the number of methods in the class.
3192 mono_class_num_methods (MonoClass *klass)
3194 return klass->method.count;
3198 * mono_class_num_properties
3199 * @klass: the MonoClass to act on
3201 * Returns the number of properties in the class.
3204 mono_class_num_properties (MonoClass *klass)
3206 return klass->property.count;
3210 * mono_class_num_events:
3211 * @klass: the MonoClass to act on
3213 * Returns the number of events in the class.
3216 mono_class_num_events (MonoClass *klass)
3218 return klass->event.count;
3222 * mono_class_get_fields:
3223 * @klass: the MonoClass to act on
3225 * This routine is an iterator routine for retrieving the fields in a class.
3227 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3228 * iterate over all of the elements. When no more values are
3229 * available, the return value is NULL.
3231 * Returns a `MonoClassField *' on each iteration, or NULL when no more fields are available.
3234 mono_class_get_fields (MonoClass* klass, gpointer *iter)
3236 MonoClassField* field;
3240 mono_class_init (klass);
3242 /* start from the first */
3243 if (klass->field.count) {
3244 return *iter = &klass->fields [0];
3252 if (field < &klass->fields [klass->field.count]) {
3253 return *iter = field;
3259 * mono_class_get_methods
3260 * @klass: the MonoClass to act on
3262 * This routine is an iterator routine for retrieving the fields in a class.
3264 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3265 * iterate over all of the elements. When no more values are
3266 * available, the return value is NULL.
3268 * Returns a MonoMethod on each iteration or NULL when no more methods are available.
3271 mono_class_get_methods (MonoClass* klass, gpointer *iter)
3273 MonoMethod** method;
3277 mono_class_init (klass);
3279 /* start from the first */
3280 if (klass->method.count) {
3281 *iter = &klass->methods [0];
3282 return klass->methods [0];
3290 if (method < &klass->methods [klass->method.count]) {
3298 * mono_class_get_properties:
3299 * @klass: the MonoClass to act on
3301 * This routine is an iterator routine for retrieving the properties in a class.
3303 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3304 * iterate over all of the elements. When no more values are
3305 * available, the return value is NULL.
3307 * Returns a `MonoProperty *' on each invocation, or NULL when no more are available.
3310 mono_class_get_properties (MonoClass* klass, gpointer *iter)
3312 MonoProperty* property;
3316 mono_class_init (klass);
3318 /* start from the first */
3319 if (klass->property.count) {
3320 return *iter = &klass->properties [0];
3328 if (property < &klass->properties [klass->property.count]) {
3329 return *iter = property;
3335 * mono_class_get_events:
3336 * @klass: the MonoClass to act on
3338 * This routine is an iterator routine for retrieving the properties in a class.
3340 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3341 * iterate over all of the elements. When no more values are
3342 * available, the return value is NULL.
3344 * Returns a `MonoEvent *' on each invocation, or NULL when no more are available.
3347 mono_class_get_events (MonoClass* klass, gpointer *iter)
3353 mono_class_init (klass);
3355 /* start from the first */
3356 if (klass->event.count) {
3357 return *iter = &klass->events [0];
3365 if (event < &klass->events [klass->event.count]) {
3366 return *iter = event;
3372 * mono_class_get_interfaaces
3373 * @klass: the MonoClass to act on
3375 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
3377 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3378 * iterate over all of the elements. When no more values are
3379 * available, the return value is NULL.
3381 * Returns a `Monoclass *' on each invocation, or NULL when no more are available.
3384 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
3390 mono_class_init (klass);
3392 /* start from the first */
3393 if (klass->interface_count) {
3394 *iter = &klass->interfaces [0];
3395 return klass->interfaces [0];
3403 if (iface < &klass->interfaces [klass->interface_count]) {
3411 * mono_class_get_interfaaces
3412 * @klass: the MonoClass to act on
3414 * This routine is an iterator routine for retrieving the nested types of a class.
3416 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3417 * iterate over all of the elements. When no more values are
3418 * available, the return value is NULL.
3420 * Returns a `Monoclass *' on each invocation, or NULL when no more are available.
3423 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
3429 mono_class_init (klass);
3431 /* start from the first */
3432 if (klass->nested_classes) {
3433 *iter = klass->nested_classes;
3434 return klass->nested_classes->data;
3436 /* no nested types */
3450 * mono_field_get_name:
3451 * @field: the MonoClassField to act on
3453 * Returns the name of the field.
3456 mono_field_get_name (MonoClassField *field)
3462 * mono_field_get_type:
3463 * @field: the MonoClassField to act on
3465 * Returns MonoType of the field.
3468 mono_field_get_type (MonoClassField *field)
3474 * mono_field_get_type:
3475 * @field: the MonoClassField to act on
3477 * Returns MonoClass where the field was defined.
3480 mono_field_get_parent (MonoClassField *field)
3482 return field->parent;
3486 * mono_field_get_flags;
3487 * @field: the MonoClassField to act on
3489 * The metadata flags for a field are encoded using the
3490 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
3492 * Returns the flags for the field.
3495 mono_field_get_flags (MonoClassField *field)
3497 return field->type->attrs;
3501 * mono_property_get_name:
3502 * @prop: the MonoProperty to act on
3504 * Returns the name of the property
3507 mono_property_get_name (MonoProperty *prop)
3513 * mono_property_get_set_method
3514 * @prop: the MonoProperty to act on.
3516 * Returns the setter method of the property (A MonoMethod)
3519 mono_property_get_set_method (MonoProperty *prop)
3525 * mono_property_get_get_method
3526 * @prop: the MonoProperty to act on.
3528 * Returns the setter method of the property (A MonoMethod)
3531 mono_property_get_get_method (MonoProperty *prop)
3537 * mono_property_get_parent:
3538 * @prop: the MonoProperty to act on.
3540 * Returns the MonoClass where the property was defined.
3543 mono_property_get_parent (MonoProperty *prop)
3545 return prop->parent;
3549 * mono_property_get_flags:
3550 * @prop: the MonoProperty to act on.
3552 * The metadata flags for a property are encoded using the
3553 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
3555 * Returns the flags for the property.
3558 mono_property_get_flags (MonoProperty *prop)
3564 * mono_event_get_name:
3565 * @event: the MonoEvent to act on
3567 * Returns the name of the event.
3570 mono_event_get_name (MonoEvent *event)
3576 * mono_event_get_add_method:
3577 * @event: The MonoEvent to act on.
3579 * Returns the `add' method for the event (a MonoMethod).
3582 mono_event_get_add_method (MonoEvent *event)
3588 * mono_event_get_remove_method:
3589 * @event: The MonoEvent to act on.
3591 * Returns the `remove' method for the event (a MonoMethod).
3594 mono_event_get_remove_method (MonoEvent *event)
3596 return event->remove;
3600 * mono_event_get_raise_method:
3601 * @event: The MonoEvent to act on.
3603 * Returns the `raise' method for the event (a MonoMethod).
3606 mono_event_get_raise_method (MonoEvent *event)
3608 return event->raise;
3612 * mono_event_get_parent:
3613 * @event: the MonoEvent to act on.
3615 * Returns the MonoClass where the event is defined.
3618 mono_event_get_parent (MonoEvent *event)
3620 return event->parent;
3624 * mono_event_get_flags
3625 * @event: the MonoEvent to act on.
3627 * The metadata flags for an event are encoded using the
3628 * EVENT_* constants. See the tabledefs.h file for details.
3630 * Returns the flags for the event.
3633 mono_event_get_flags (MonoEvent *event)
3635 return event->attrs;