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/tabledefs.h>
27 #include <mono/metadata/tokentype.h>
28 #include <mono/metadata/class.h>
29 #include <mono/metadata/object.h>
30 #include <mono/metadata/appdomain.h>
31 #include <mono/metadata/mono-endian.h>
32 #include <mono/metadata/debug-helpers.h>
33 #include <mono/metadata/reflection.h>
34 #include <mono/os/gc_wrapper.h>
38 gboolean mono_print_vtable = FALSE;
40 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
42 void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
45 mono_class_from_typeref (MonoImage *image, guint32 type_token)
47 guint32 cols [MONO_TYPEREF_SIZE];
48 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
50 const char *name, *nspace;
52 MonoAssembly **references;
54 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
56 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
57 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
59 idx = cols [MONO_TYPEREF_SCOPE] >> RESOLTION_SCOPE_BITS;
60 switch (cols [MONO_TYPEREF_SCOPE] & RESOLTION_SCOPE_MASK) {
61 case RESOLTION_SCOPE_MODULE:
63 g_error ("null ResolutionScope not yet handled");
64 /* a typedef in disguise */
65 return mono_class_from_name (image, nspace, name);
66 case RESOLTION_SCOPE_MODULEREF:
67 return mono_class_from_name (image->modules [idx - 1], nspace, name);
68 case RESOLTION_SCOPE_TYPEREF: {
69 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
71 mono_class_init (enclosing);
72 for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
74 if (strcmp (res->name, name) == 0)
77 g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
80 case RESOLTION_SCOPE_ASSEMBLYREF:
84 references = image->references;
85 if (!references || !references [idx-1]) {
87 * detected a reference to mscorlib, we simply return a reference to a dummy
88 * until we have a better solution.
90 * once a better solution is in place, the System.MonoDummy
91 * class should be removed from CVS.
93 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]));
95 res = mono_class_from_name (image, "System", "MonoDummy");
96 /* prevent method loading */
98 /* some storage if the type is used - very ugly hack */
99 res->instance_size = 2*sizeof (gpointer);
103 /* load referenced assembly */
104 image = references [idx-1]->image;
106 return mono_class_from_name (image, nspace, name);
110 dup_type (MonoType* t, const MonoType *original)
112 MonoType *r = g_new0 (MonoType, 1);
114 r->attrs = original->attrs;
115 mono_stats.generics_metadata_size += sizeof (MonoType);
120 mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed)
124 switch (type->type) {
125 case MONO_TYPE_ARRAY: {
126 int i, rank = type->data.array->rank;
128 mono_type_get_name_recurse (&type->data.array->eklass->byval_arg, str, FALSE);
129 g_string_append_c (str, '[');
130 for (i = 1; i < rank; i++)
131 g_string_append_c (str, ',');
132 g_string_append_c (str, ']');
135 case MONO_TYPE_SZARRAY:
136 mono_type_get_name_recurse (&type->data.klass->byval_arg, str, FALSE);
137 g_string_append (str, "[]");
140 mono_type_get_name_recurse (type->data.type, str, FALSE);
141 g_string_append_c (str, '*');
144 klass = mono_class_from_mono_type (type);
145 if (klass->nested_in) {
146 mono_type_get_name_recurse (&klass->nested_in->byval_arg, str, TRUE);
147 g_string_append_c (str, '+');
149 if (*klass->name_space) {
150 g_string_append (str, klass->name_space);
151 g_string_append_c (str, '.');
153 g_string_append (str, klass->name);
156 if (klass->generic_inst) {
157 MonoGenericInst *ginst = klass->generic_inst;
160 g_string_append_c (str, '[');
161 for (i = 0; i < ginst->type_argc; i++) {
163 g_string_append_c (str, ',');
164 mono_type_get_name_recurse (ginst->type_argv [i], str, FALSE);
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)
192 GString* result = g_string_new ("");
193 mono_type_get_name_recurse (type, result, FALSE);
196 g_string_append_c (result, '&');
198 return g_string_free (result, FALSE);
202 mono_class_is_open_constructed_type (MonoType *t)
208 case MONO_TYPE_SZARRAY:
209 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
210 case MONO_TYPE_ARRAY:
211 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
213 return mono_class_is_open_constructed_type (t->data.type);
214 case MONO_TYPE_GENERICINST: {
215 MonoGenericInst *ginst = t->data.generic_inst;
218 if (mono_class_is_open_constructed_type (ginst->generic_type))
220 for (i = 0; i < ginst->type_argc; i++)
221 if (mono_class_is_open_constructed_type (ginst->type_argv [i]))
231 inflate_generic_type (MonoType *type, MonoGenericContext *context)
233 switch (type->type) {
235 if (context->gmethod && context->gmethod->mtype_argv)
237 context->gmethod->mtype_argv [type->data.generic_param->num],
244 context->ginst->type_argv [type->data.generic_param->num],
248 case MONO_TYPE_SZARRAY: {
249 MonoClass *eclass = type->data.klass;
250 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
253 nt = dup_type (type, type);
254 nt->data.klass = mono_class_from_mono_type (inflated);
257 case MONO_TYPE_GENERICINST: {
258 MonoGenericInst *oginst = type->data.generic_inst;
259 MonoGenericInst *nginst;
263 nginst = g_new0 (MonoGenericInst, 1);
266 nginst->is_open = FALSE;
268 nginst->type_argv = g_new0 (MonoType *, oginst->type_argc);
270 for (i = 0; i < oginst->type_argc; i++) {
271 MonoType *t = oginst->type_argv [i];
272 nginst->type_argv [i] = mono_class_inflate_generic_type (t, context);
274 if (!nginst->is_open)
275 nginst->is_open = mono_class_is_open_constructed_type (nginst->type_argv [i]);
278 nginst->klass = NULL;
280 nginst->context = g_new0 (MonoGenericContext, 1);
281 nginst->context->ginst = nginst;
284 nt = g_hash_table_lookup (oginst->klass->image->generic_inst_cache, nginst);
287 g_free (nginst->type_argv);
289 mono_loader_unlock ();
293 nginst->dynamic_info = NULL;
294 nginst->initialized = FALSE;
296 mono_class_create_generic (nginst);
298 mono_stats.generic_instance_count++;
299 mono_stats.generics_metadata_size += sizeof (MonoGenericInst) +
300 sizeof (MonoGenericContext) +
301 nginst->type_argc * sizeof (MonoType);
303 nt = dup_type (type, type);
304 nt->data.generic_inst = nginst;
305 g_hash_table_insert (oginst->klass->image->generic_inst_cache, nginst, nt);
306 mono_loader_unlock ();
316 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
318 MonoType *inflated = inflate_generic_type (type, context);
323 mono_stats.inflated_type_count++;
327 static MonoMethodSignature*
328 inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig,
329 MonoGenericContext *context)
331 MonoMethodSignature *res;
333 res = mono_metadata_signature_alloc (image, sig->param_count);
334 res->ret = mono_class_inflate_generic_type (sig->ret, context);
335 for (i = 0; i < sig->param_count; ++i)
336 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
337 res->hasthis = sig->hasthis;
338 res->explicit_this = sig->explicit_this;
339 res->call_convention = sig->call_convention;
340 res->generic_param_count = sig->generic_param_count;
341 res->is_inflated = 1;
345 static MonoMethodHeader*
346 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
348 MonoMethodHeader *res;
350 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
351 res->code = header->code;
352 res->code_size = header->code_size;
353 res->max_stack = header->max_stack;
354 res->num_clauses = header->num_clauses;
355 res->init_locals = header->init_locals;
356 res->num_locals = header->num_locals;
357 res->clauses = header->clauses;
358 res->gen_params = header->gen_params;
359 for (i = 0; i < header->num_locals; ++i)
360 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
365 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context,
368 MonoMethodInflated *result;
370 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
371 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
374 mono_stats.inflated_method_count++;
375 mono_stats.generics_metadata_size +=
376 sizeof (MonoMethodInflated) - sizeof (MonoMethodNormal);
378 result = g_new0 (MonoMethodInflated, 1);
379 result->nmethod = *(MonoMethodNormal*)method;
381 if (result->nmethod.header)
382 result->nmethod.header = inflate_generic_header (
383 result->nmethod.header, context);
386 result->nmethod.method.klass = klass;
388 MonoType *declaring = mono_class_inflate_generic_type (
389 &method->klass->byval_arg, context);
390 result->nmethod.method.klass = mono_class_from_mono_type (declaring);
393 result->nmethod.method.signature = inflate_generic_signature (
394 method->klass->image, method->signature, context);
396 if (context->gmethod) {
397 result->context = g_new0 (MonoGenericContext, 1);
398 result->context->gmethod = context->gmethod;
399 result->context->ginst = result->nmethod.method.klass->generic_inst;
401 mono_stats.generics_metadata_size += sizeof (MonoGenericContext);
403 result->context = result->nmethod.method.klass->generic_inst->context;
405 if (method->signature->is_inflated)
406 result->declaring = ((MonoMethodInflated *) method)->declaring;
408 result->declaring = method;
410 return (MonoMethod *) result;
414 * class_compute_field_layout:
415 * @m: pointer to the metadata.
416 * @class: The class to initialize
418 * Initializes the class->fields.
420 * Currently we only support AUTO_LAYOUT, and do not even try to do
421 * a good job at it. This is temporary to get the code for Paolo.
424 class_compute_field_layout (MonoClass *class)
426 MonoImage *m = class->image;
427 const int top = class->field.count;
428 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
429 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
430 int i, blittable = TRUE, real_size = 0;
432 guint32 packing_size = 0;
433 gboolean explicit_size;
434 MonoClassField *field;
436 if (class->size_inited)
440 if (!class->parent->size_inited)
441 class_compute_field_layout (class->parent);
442 class->instance_size += class->parent->instance_size;
443 class->min_align = class->parent->min_align;
444 blittable = class->parent->blittable;
446 class->instance_size = sizeof (MonoObject);
447 class->min_align = 1;
450 /* Get the real size */
451 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
454 g_assert ((packing_size & 0xfffffff0) == 0);
455 class->packing_size = packing_size;
456 real_size += class->instance_size;
460 if (explicit_size && real_size) {
461 class->instance_size = MAX (real_size, class->instance_size);
463 class->size_inited = 1;
467 class->fields = g_new0 (MonoClassField, top);
470 * Fetch all the field information.
472 for (i = 0; i < top; i++){
474 guint32 cols [MONO_FIELD_SIZE];
475 int idx = class->field.first + i;
477 field = &class->fields [i];
478 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
479 /* The name is needed for fieldrefs */
480 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
481 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
482 mono_metadata_decode_value (sig, &sig);
483 /* FIELD signature == 0x06 */
484 g_assert (*sig == 0x06);
485 field->type = mono_metadata_parse_field_type (
486 m, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
487 if (mono_field_is_deleted (field))
489 if (class->generic_inst) {
490 field->type = mono_class_inflate_generic_type (
491 field->type, class->generic_inst->context);
492 field->type->attrs = cols [MONO_FIELD_FLAGS];
495 field->parent = class;
497 /* Only do these checks if we still think this type is blittable */
498 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
499 if (field->type->byref) {
502 MonoClass *field_class = mono_class_from_mono_type (field->type);
503 if (!field_class || !field_class->blittable)
508 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
509 mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
510 if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
511 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, field->name);
514 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
515 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
517 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
518 field->data = mono_cli_rva_map (class->image->image_info, rva);
521 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
522 class->enum_basetype = field->type;
523 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
524 blittable = class->element_class->blittable;
527 /* The def_value of fields is compute lazily during vtable creation */
530 if (class == mono_defaults.string_class)
533 class->blittable = blittable;
535 if (class->enumtype && !class->enum_basetype) {
536 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
539 if (explicit_size && real_size) {
540 class->instance_size = MAX (real_size, class->instance_size);
543 if (class->gen_params)
546 mono_class_layout_fields (class);
550 mono_class_layout_fields (MonoClass *class)
553 const int top = class->field.count;
554 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
555 guint32 pass, passes, real_size;
556 gboolean gc_aware_layout = FALSE;
557 MonoClassField *field;
560 * Enable GC aware auto layout: in this mode, reference
561 * fields are grouped together inside objects, increasing collector
563 * Requires that all classes whose layout is known to native code be annotated
564 * with [StructLayout (LayoutKind.Sequential)]
565 * Value types have gc_aware_layout disabled by default, as per
566 * what the default is for other runtimes.
568 /* corlib is missing [StructLayout] directives in many places */
569 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
570 if (class->image != mono_defaults.corlib &&
571 class->byval_arg.type != MONO_TYPE_VALUETYPE)
572 gc_aware_layout = TRUE;
576 * Compute field layout and total size (not considering static fields)
580 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
581 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
588 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
592 real_size = class->parent->instance_size;
594 real_size = sizeof (MonoObject);
596 for (pass = 0; pass < passes; ++pass) {
597 for (i = 0; i < top; i++){
599 field = &class->fields [i];
601 if (mono_field_is_deleted (field))
603 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
606 if (gc_aware_layout) {
608 * We process fields with reference type in the first pass,
609 * and fields with non-reference type in the second pass.
610 * We use IS_POINTER instead of IS_REFERENCE because in
611 * some internal structures, we store GC_MALLOCed memory
612 * in IntPtr fields...
614 if (MONO_TYPE_IS_POINTER (field->type)) {
623 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
624 (strcmp (field->name, "$PRIVATE$") == 0)) {
625 /* This field is a hack inserted by MCS to empty structures */
629 size = mono_type_size (field->type, &align);
631 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
632 align = class->packing_size ? MIN (class->packing_size, align): align;
633 class->min_align = MAX (align, class->min_align);
634 field->offset = real_size;
635 field->offset += align - 1;
636 field->offset &= ~(align - 1);
637 real_size = field->offset + size;
640 class->instance_size = MAX (real_size, class->instance_size);
642 if (class->instance_size & (class->min_align - 1)) {
643 class->instance_size += class->min_align - 1;
644 class->instance_size &= ~(class->min_align - 1);
648 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
650 for (i = 0; i < top; i++) {
652 field = &class->fields [i];
655 * There must be info about all the fields in a type if it
656 * uses explicit layout.
659 if (mono_field_is_deleted (field))
661 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
664 size = mono_type_size (field->type, &align);
667 * When we get here, field->offset is already set by the
668 * loader (for either runtime fields or fields loaded from metadata).
669 * The offset is from the start of the object: this works for both
670 * classes and valuetypes.
672 field->offset += sizeof (MonoObject);
677 real_size = MAX (real_size, size + field->offset);
679 class->instance_size = MAX (real_size, class->instance_size);
683 class->size_inited = 1;
686 * Compute static field layout and size
688 for (i = 0; i < top; i++){
690 field = &class->fields [i];
692 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
694 if (mono_field_is_deleted (field))
697 size = mono_type_size (field->type, &align);
698 field->offset = class->class_size;
699 field->offset += align - 1;
700 field->offset &= ~(align - 1);
701 class->class_size = field->offset + size;
706 init_properties (MonoClass *class)
708 guint startm, endm, i, j;
709 guint32 cols [MONO_PROPERTY_SIZE];
710 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
711 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
713 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
714 class->property.count = class->property.last - class->property.first;
716 class->properties = g_new0 (MonoProperty, class->property.count);
717 for (i = class->property.first; i < class->property.last; ++i) {
718 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
719 class->properties [i - class->property.first].parent = class;
720 class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
721 class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
723 startm = mono_metadata_methods_from_property (class->image, i, &endm);
724 for (j = startm; j < endm; ++j) {
725 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
726 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
727 case METHOD_SEMANTIC_SETTER:
728 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
730 case METHOD_SEMANTIC_GETTER:
731 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
741 init_events (MonoClass *class)
743 guint startm, endm, i, j;
744 guint32 cols [MONO_EVENT_SIZE];
745 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
746 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
748 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
749 class->event.count = class->event.last - class->event.first;
751 class->events = g_new0 (MonoEvent, class->event.count);
752 for (i = class->event.first; i < class->event.last; ++i) {
753 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
754 class->events [i - class->event.first].parent = class;
755 class->events [i - class->event.first].attrs = cols [MONO_EVENT_FLAGS];
756 class->events [i - class->event.first].name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
758 startm = mono_metadata_methods_from_event (class->image, i, &endm);
759 for (j = startm; j < endm; ++j) {
760 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
761 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
762 case METHOD_SEMANTIC_ADD_ON:
763 class->events [i - class->event.first].add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
765 case METHOD_SEMANTIC_REMOVE_ON:
766 class->events [i - class->event.first].remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
768 case METHOD_SEMANTIC_FIRE:
769 class->events [i - class->event.first].raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
771 case METHOD_SEMANTIC_OTHER: /* don't care for now */
780 mono_get_unique_iid (MonoClass *class)
782 static GHashTable *iid_hash = NULL;
783 static guint iid = 0;
788 g_assert (MONO_CLASS_IS_INTERFACE (class));
793 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
795 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
797 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
798 mono_loader_unlock ();
802 g_hash_table_insert (iid_hash, str, (gpointer)iid);
806 mono_loader_unlock ();
812 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
817 for (i = 0; i < klass->interface_count; i++) {
818 ic = klass->interfaces [i];
821 *res = g_ptr_array_new ();
822 g_ptr_array_add (*res, ic);
824 collect_implemented_interfaces_aux (ic, res);
828 static inline GPtrArray*
829 collect_implemented_interfaces (MonoClass *klass)
831 GPtrArray *res = NULL;
833 collect_implemented_interfaces_aux (klass, &res);
838 setup_interface_offsets (MonoClass *class, int cur_slot)
844 /* compute maximum number of slots and maximum interface id */
846 for (k = class; k ; k = k->parent) {
847 for (i = 0; i < k->interface_count; i++) {
848 ic = k->interfaces [i];
851 mono_class_init (ic);
853 if (max_iid < ic->interface_id)
854 max_iid = ic->interface_id;
858 if (MONO_CLASS_IS_INTERFACE (class)) {
859 if (max_iid < class->interface_id)
860 max_iid = class->interface_id;
862 class->max_interface_id = max_iid;
863 /* compute vtable offset for interfaces */
864 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
866 for (i = 0; i <= max_iid; i++)
867 class->interface_offsets [i] = -1;
869 ifaces = collect_implemented_interfaces (class);
871 for (i = 0; i < ifaces->len; ++i) {
872 ic = g_ptr_array_index (ifaces, i);
873 class->interface_offsets [ic->interface_id] = cur_slot;
874 cur_slot += ic->method.count;
876 g_ptr_array_free (ifaces, TRUE);
879 for (k = class->parent; k ; k = k->parent) {
880 ifaces = collect_implemented_interfaces (k);
882 for (i = 0; i < ifaces->len; ++i) {
883 ic = g_ptr_array_index (ifaces, i);
885 if (class->interface_offsets [ic->interface_id] == -1) {
886 int io = k->interface_offsets [ic->interface_id];
890 class->interface_offsets [ic->interface_id] = io;
893 g_ptr_array_free (ifaces, TRUE);
897 if (MONO_CLASS_IS_INTERFACE (class))
898 class->interface_offsets [class->interface_id] = cur_slot;
904 mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
908 int i, max_vtsize = 0, max_iid, cur_slot = 0;
910 MonoGHashTable *override_map = NULL;
912 /* setup_vtable() must be called only once on the type */
913 if (class->interface_offsets) {
914 g_warning ("vtable already computed in %s.%s", class->name_space, class->name);
918 ifaces = collect_implemented_interfaces (class);
920 for (i = 0; i < ifaces->len; i++) {
921 MonoClass *ic = g_ptr_array_index (ifaces, i);
922 max_vtsize += ic->method.count;
924 g_ptr_array_free (ifaces, TRUE);
928 max_vtsize += class->parent->vtable_size;
929 cur_slot = class->parent->vtable_size;
932 max_vtsize += class->method.count;
934 vtable = alloca (sizeof (gpointer) * max_vtsize);
935 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
937 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
939 cur_slot = setup_interface_offsets (class, cur_slot);
940 max_iid = class->max_interface_id;
942 if (class->parent && class->parent->vtable_size)
943 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
945 /* override interface methods */
946 for (i = 0; i < onum; i++) {
947 MonoMethod *decl = overrides [i*2];
948 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
950 g_assert (decl->slot != -1);
951 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
952 vtable [dslot] = overrides [i*2 + 1];
953 vtable [dslot]->slot = dslot;
955 override_map = mono_g_hash_table_new (NULL, NULL);
957 mono_g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
961 for (k = class; k ; k = k->parent) {
963 ifaces = collect_implemented_interfaces (k);
965 nifaces = ifaces->len;
966 for (i = 0; i < nifaces; i++) {
969 ic = g_ptr_array_index (ifaces, i);
970 io = k->interface_offsets [ic->interface_id];
973 g_assert (io <= max_vtsize);
976 for (l = 0; l < ic->method.count; l++) {
977 MonoMethod *im = ic->methods [l];
979 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
982 for (j = 0; j < class->method.count; ++j) {
983 MonoMethod *cm = class->methods [j];
984 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
985 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
986 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
988 if (!strcmp(cm->name, im->name) &&
989 mono_metadata_signature_equal (cm->signature, im->signature)) {
990 g_assert (io + l <= max_vtsize);
991 vtable [io + l] = cm;
996 /* already implemented */
997 if (io >= k->vtable_size)
1001 for (l = 0; l < ic->method.count; l++) {
1002 MonoMethod *im = ic->methods [l];
1005 g_assert (io + l <= max_vtsize);
1007 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1010 for (k1 = class; k1; k1 = k1->parent) {
1011 for (j = 0; j < k1->method.count; ++j) {
1012 MonoMethod *cm = k1->methods [j];
1014 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1015 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
1018 if (!strcmp(cm->name, im->name) &&
1019 mono_metadata_signature_equal (cm->signature, im->signature)) {
1020 g_assert (io + l <= max_vtsize);
1021 vtable [io + l] = cm;
1026 g_assert (io + l <= max_vtsize);
1027 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1032 for (l = 0; l < ic->method.count; l++) {
1033 MonoMethod *im = ic->methods [l];
1034 char *qname, *fqname;
1037 if (vtable [io + l])
1040 qname = g_strconcat (ic->name, ".", im->name, NULL);
1041 if (ic->name_space && ic->name_space [0])
1042 fqname = g_strconcat (ic->name_space, ".", ic->name, ".", im->name, NULL);
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))
1053 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
1054 mono_metadata_signature_equal (cm->signature, im->signature)) {
1055 g_assert (io + l <= max_vtsize);
1056 vtable [io + l] = cm;
1066 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
1067 for (l = 0; l < ic->method.count; l++) {
1069 MonoMethod *im = ic->methods [l];
1070 if (im->flags & METHOD_ATTRIBUTE_STATIC)
1072 g_assert (io + l <= max_vtsize);
1075 * If one of our parents already implements this interface
1076 * we can inherit the implementation.
1078 if (!(vtable [io + l])) {
1079 MonoClass *parent = class->parent;
1081 if ((ic->interface_id <= parent->max_interface_id) &&
1082 (parent->interface_offsets [ic->interface_id]) &&
1084 vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
1087 if (!(vtable [io + l])) {
1088 for (j = 0; j < onum; ++j) {
1089 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
1090 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
1092 msig = mono_signature_get_desc (im->signature, FALSE);
1093 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
1094 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
1096 for (j = 0; j < class->method.count; ++j) {
1097 MonoMethod *cm = class->methods [j];
1098 msig = mono_signature_get_desc (cm->signature, FALSE);
1100 printf ("METHOD %s(%s)\n", cm->name, msig);
1103 g_assert_not_reached ();
1108 for (l = 0; l < ic->method.count; l++) {
1109 MonoMethod *im = vtable [io + l];
1112 g_assert (io + l <= max_vtsize);
1114 /* FIXME: why do we need this ? */
1116 /* g_assert_not_reached (); */
1122 g_ptr_array_free (ifaces, TRUE);
1125 for (i = 0; i < class->method.count; ++i) {
1128 cm = class->methods [i];
1131 * Non-virtual method have no place in the vtable.
1132 * This also catches static methods (since they are not virtual).
1134 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1138 * If the method is REUSE_SLOT, we must check in the
1139 * base class for a method to override.
1141 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
1143 for (k = class->parent; k ; k = k->parent) {
1145 for (j = 0; j < k->method.count; ++j) {
1146 MonoMethod *m1 = k->methods [j];
1147 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
1149 if (!strcmp(cm->name, m1->name) &&
1150 mono_metadata_signature_equal (cm->signature, m1->signature)) {
1151 slot = k->methods [j]->slot;
1152 g_assert (cm->slot < max_vtsize);
1154 override_map = mono_g_hash_table_new (NULL, NULL);
1155 mono_g_hash_table_insert (override_map, m1, cm);
1167 cm->slot = cur_slot++;
1169 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT) && !cm->signature->generic_param_count)
1170 vtable [cm->slot] = cm;
1173 /* override non interface methods */
1174 for (i = 0; i < onum; i++) {
1175 MonoMethod *decl = overrides [i*2];
1176 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
1177 g_assert (decl->slot != -1);
1178 vtable [decl->slot] = overrides [i*2 + 1];
1179 overrides [i * 2 + 1]->slot = decl->slot;
1181 override_map = mono_g_hash_table_new (NULL, NULL);
1182 mono_g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
1187 * If a method occupies more than one place in the vtable, and it is
1188 * overriden, then change the other occurances too.
1191 for (i = 0; i < max_vtsize; ++i)
1193 MonoMethod *cm = mono_g_hash_table_lookup (override_map, vtable [i]);
1198 mono_g_hash_table_destroy (override_map);
1201 if (class->generic_inst) {
1202 MonoClass *gklass = mono_class_from_mono_type (class->generic_inst->generic_type);
1204 mono_class_init (gklass);
1205 class->vtable_size = gklass->vtable_size;
1207 class->vtable_size = cur_slot;
1209 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
1210 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
1212 if (mono_print_vtable) {
1215 for (i = 0; i <= max_iid; i++)
1216 if (class->interface_offsets [i] != -1)
1219 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
1220 class->name, class->vtable_size, icount);
1222 for (i = 0; i < class->vtable_size; ++i) {
1227 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
1228 cm->klass->name_space, cm->klass->name,
1235 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
1236 class->name, max_iid);
1238 for (i = 0; i < class->interface_count; i++) {
1239 ic = class->interfaces [i];
1240 printf (" slot %03d(%03d) %s.%s\n",
1241 class->interface_offsets [ic->interface_id],
1242 ic->method.count, ic->name_space, ic->name);
1245 for (k = class->parent; k ; k = k->parent) {
1246 for (i = 0; i < k->interface_count; i++) {
1247 ic = k->interfaces [i];
1248 printf (" slot %03d(%03d) %s.%s\n",
1249 class->interface_offsets [ic->interface_id],
1250 ic->method.count, ic->name_space, ic->name);
1259 * @class: the class to initialize
1261 * compute the instance_size, class_size and other infos that cannot be
1262 * computed at mono_class_get() time. Also compute a generic vtable and
1263 * the method slot numbers. We use this infos later to create a domain
1267 mono_class_init (MonoClass *class)
1270 static MonoMethod *default_ghc = NULL;
1271 static MonoMethod *default_finalize = NULL;
1272 static int finalize_slot = -1;
1273 static int ghc_slot = -1;
1274 MonoMethod **overrides;
1282 /*g_print ("Init class %s\n", class->name);*/
1284 /* We do everything inside the lock to prevent races */
1285 mono_loader_lock ();
1287 if (class->inited) {
1288 mono_loader_unlock ();
1289 /* Somebody might have gotten in before us */
1293 if (class->init_pending) {
1294 /* this indicates a cyclic dependency */
1295 g_error ("pending init %s.%s\n", class->name_space, class->name);
1298 class->init_pending = 1;
1300 mono_stats.initialized_class_count++;
1302 if (class->generic_inst && !class->generic_inst->is_dynamic) {
1303 MonoGenericInst *ginst = class->generic_inst;
1307 gklass = mono_class_from_mono_type (ginst->generic_type);
1308 mono_class_init (gklass);
1311 class->parent = mono_class_from_mono_type (ginst->parent);
1313 class->parent = gklass->parent;
1315 mono_class_setup_parent (class, class->parent);
1317 if (MONO_CLASS_IS_INTERFACE (class))
1318 class->interface_id = mono_get_unique_iid (class);
1320 class->method = gklass->method;
1321 class->methods = g_new0 (MonoMethod *, class->method.count);
1323 for (i = 0; i < class->method.count; i++)
1324 class->methods [i] = mono_class_inflate_generic_method (
1325 gklass->methods [i], ginst->context, ginst->klass);
1327 class->field = gklass->field;
1328 class->fields = g_new0 (MonoClassField, class->field.count);
1330 for (i = 0; i < class->field.count; i++) {
1331 class->fields [i] = gklass->fields [i];
1332 class->fields [i].generic_type = gklass->fields [i].type;
1333 class->fields [i].parent = class;
1334 class->fields [i].type = mono_class_inflate_generic_type (
1335 class->fields [i].type, ginst->context);
1338 class->property = gklass->property;
1339 class->properties = g_new0 (MonoProperty, class->property.count);
1341 for (i = 0; i < class->property.count; i++) {
1342 MonoProperty *prop = &class->properties [i];
1344 *prop = gklass->properties [i];
1347 prop->get = mono_class_inflate_generic_method (
1348 prop->get, ginst->context, ginst->klass);
1350 prop->set = mono_class_inflate_generic_method (
1351 prop->set, ginst->context, ginst->klass);
1353 prop->parent = class;
1356 class->interface_count = gklass->interface_count;
1357 class->interfaces = g_new0 (MonoClass *, class->interface_count);
1358 for (i = 0; i < class->interface_count; i++) {
1359 MonoType *it = &gklass->interfaces [i]->byval_arg;
1360 MonoType *inflated = mono_class_inflate_generic_type (
1361 it, ginst->context);
1362 class->interfaces [i] = mono_class_from_mono_type (inflated);
1363 mono_class_init (class->interfaces [i]);
1366 for (list = gklass->nested_classes; list; list = list->next)
1367 class->nested_classes = g_list_append (
1368 class->nested_classes, list->data);
1371 if (class->parent && !class->parent->inited)
1372 mono_class_init (class->parent);
1375 * Computes the size used by the fields, and their locations
1377 if (!class->size_inited)
1378 class_compute_field_layout (class);
1380 /* initialize method pointers */
1383 MonoMethodSignature *sig;
1384 class->method.count = class->rank > 1? 2: 1;
1385 sig = mono_metadata_signature_alloc (class->image, class->rank);
1386 sig->ret = &mono_defaults.void_class->byval_arg;
1387 sig->pinvoke = TRUE;
1388 for (i = 0; i < class->rank; ++i)
1389 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1391 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1392 ctor->klass = class;
1393 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1394 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1395 ctor->signature = sig;
1396 ctor->name = ".ctor";
1398 class->methods = g_new (MonoMethod*, class->method.count);
1399 class->methods [0] = ctor;
1400 if (class->rank > 1) {
1401 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
1402 sig->ret = &mono_defaults.void_class->byval_arg;
1403 sig->pinvoke = TRUE;
1404 for (i = 0; i < class->rank * 2; ++i)
1405 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1407 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1408 ctor->klass = class;
1409 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1410 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1411 ctor->signature = sig;
1412 ctor->name = ".ctor";
1414 class->methods [1] = ctor;
1417 if (!class->generic_inst && !class->methods) {
1418 class->methods = g_new (MonoMethod*, class->method.count);
1419 for (i = 0; i < class->method.count; ++i) {
1420 class->methods [i] = mono_get_method (class->image,
1421 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
1426 if (!class->generic_inst) {
1427 init_properties (class);
1428 init_events (class);
1430 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
1433 guint32 cols [MONO_NESTED_CLASS_SIZE];
1434 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
1435 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
1436 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
1438 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
1442 mono_class_setup_supertypes (class);
1444 if (MONO_CLASS_IS_INTERFACE (class)) {
1445 for (i = 0; i < class->method.count; ++i)
1446 class->methods [i]->slot = i;
1447 class->init_pending = 0;
1450 * class->interface_offsets is needed for the castclass/isinst code, so
1451 * we have to setup them for interfaces, too.
1453 setup_interface_offsets (class, 0);
1454 mono_loader_unlock ();
1458 overrides = mono_class_get_overrides (class->image, class->type_token, &onum);
1459 mono_class_setup_vtable (class, overrides, onum);
1463 class->init_pending = 0;
1466 if (class == mono_defaults.object_class) {
1468 for (i = 0; i < class->vtable_size; ++i) {
1469 MonoMethod *cm = class->vtable [i];
1471 if (!strcmp (cm->name, "GetHashCode")) {
1477 g_assert (ghc_slot > 0);
1479 default_ghc = class->vtable [ghc_slot];
1484 if (class->parent) {
1486 if (class->vtable [ghc_slot] == default_ghc) {
1491 if (!default_finalize) {
1492 if (class == mono_defaults.object_class) {
1494 for (i = 0; i < class->vtable_size; ++i) {
1495 MonoMethod *cm = class->vtable [i];
1497 if (!strcmp (cm->name, "Finalize")) {
1503 g_assert (finalize_slot > 0);
1505 default_finalize = class->vtable [finalize_slot];
1509 /* Object::Finalize should have empty implemenatation */
1510 class->has_finalize = 0;
1511 if (class->parent) {
1512 if (class->vtable [finalize_slot] != default_finalize)
1513 class->has_finalize = 1;
1516 mono_loader_unlock ();
1518 if (mono_debugger_class_init_func)
1519 mono_debugger_class_init_func (class);
1524 mono_class_setup_mono_type (MonoClass *class)
1526 const char *name = class->name;
1527 const char *nspace = class->name_space;
1529 if (MONO_CLASS_IS_INTERFACE (class))
1530 class->interface_id = mono_get_unique_iid (class);
1532 class->this_arg.byref = 1;
1533 class->this_arg.data.klass = class;
1534 class->this_arg.type = MONO_TYPE_CLASS;
1535 class->byval_arg.data.klass = class;
1536 class->byval_arg.type = MONO_TYPE_CLASS;
1538 if (!strcmp (nspace, "System")) {
1539 if (!strcmp (name, "ValueType")) {
1541 * do not set the valuetype bit for System.ValueType.
1542 * class->valuetype = 1;
1544 class->blittable = TRUE;
1545 } else if (!strcmp (name, "Enum")) {
1547 * do not set the valuetype bit for System.Enum.
1548 * class->valuetype = 1;
1550 class->valuetype = 0;
1551 class->enumtype = 0;
1552 } else if (!strcmp (name, "Object")) {
1553 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
1554 } else if (!strcmp (name, "String")) {
1555 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
1556 } else if (!strcmp (name, "TypedReference")) {
1557 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
1561 if (class->valuetype) {
1562 int t = MONO_TYPE_VALUETYPE;
1563 if (!strcmp (nspace, "System")) {
1566 if (!strcmp (name, "Boolean")) {
1567 t = MONO_TYPE_BOOLEAN;
1568 } else if (!strcmp(name, "Byte")) {
1570 class->blittable = TRUE;
1574 if (!strcmp (name, "Char")) {
1579 if (!strcmp (name, "Double")) {
1581 class->blittable = TRUE;
1585 if (!strcmp (name, "Int32")) {
1587 class->blittable = TRUE;
1588 } else if (!strcmp(name, "Int16")) {
1590 class->blittable = TRUE;
1591 } else if (!strcmp(name, "Int64")) {
1593 class->blittable = TRUE;
1594 } else if (!strcmp(name, "IntPtr")) {
1596 class->blittable = TRUE;
1600 if (!strcmp (name, "Single")) {
1602 class->blittable = TRUE;
1603 } else if (!strcmp(name, "SByte")) {
1605 class->blittable = TRUE;
1609 if (!strcmp (name, "UInt32")) {
1611 class->blittable = TRUE;
1612 } else if (!strcmp(name, "UInt16")) {
1614 class->blittable = TRUE;
1615 } else if (!strcmp(name, "UInt64")) {
1617 class->blittable = TRUE;
1618 } else if (!strcmp(name, "UIntPtr")) {
1620 class->blittable = TRUE;
1624 if (!strcmp (name, "TypedReference")) {
1625 t = MONO_TYPE_TYPEDBYREF;
1626 class->blittable = TRUE;
1630 if (!strcmp (name, "Void")) {
1638 class->this_arg.type = class->byval_arg.type = t;
1643 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
1645 gboolean system_namespace;
1647 system_namespace = !strcmp (class->name_space, "System");
1649 /* if root of the hierarchy */
1650 if (system_namespace && !strcmp (class->name, "Object")) {
1651 class->parent = NULL;
1652 class->instance_size = sizeof (MonoObject);
1655 if (!strcmp (class->name, "<Module>")) {
1656 class->parent = NULL;
1657 class->instance_size = 0;
1661 if (!MONO_CLASS_IS_INTERFACE (class)) {
1662 class->parent = parent;
1665 g_assert_not_reached (); /* FIXME */
1667 if (parent->generic_inst && !parent->name) {
1669 * If the parent is a generic instance, we may get
1670 * called before it is fully initialized, especially
1671 * before it has its name.
1676 class->marshalbyref = parent->marshalbyref;
1677 class->contextbound = parent->contextbound;
1678 class->delegate = parent->delegate;
1680 if (system_namespace) {
1681 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
1682 class->marshalbyref = 1;
1684 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
1685 class->contextbound = 1;
1687 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
1688 class->delegate = 1;
1691 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
1692 (strcmp (class->parent->name_space, "System") == 0)))
1693 class->valuetype = 1;
1694 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
1695 class->valuetype = class->enumtype = 1;
1697 /*class->enumtype = class->parent->enumtype; */
1698 mono_class_setup_supertypes (class);
1700 class->parent = NULL;
1706 mono_class_setup_supertypes (MonoClass *class)
1711 if (class->supertypes)
1715 for (k = class; k ; k = k->parent) {
1719 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
1720 class->supertypes = g_new0 (MonoClass *, ms);
1722 if (class->parent) {
1723 for (i = class->idepth, k = class; k ; k = k->parent)
1724 class->supertypes [--i] = k;
1726 class->supertypes [0] = class;
1731 * @image: context where the image is created
1732 * @type_token: typedef token
1735 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
1737 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
1738 MonoClass *class, *parent = NULL;
1739 guint32 cols [MONO_TYPEDEF_SIZE];
1740 guint32 cols_next [MONO_TYPEDEF_SIZE];
1741 guint tidx = mono_metadata_token_index (type_token);
1742 const char *name, *nspace;
1744 MonoClass **interfaces;
1746 mono_loader_lock ();
1748 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
1749 mono_loader_unlock ();
1753 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
1755 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
1757 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1758 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1760 if (cols [MONO_TYPEDEF_EXTENDS])
1761 parent = mono_class_get (image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]));
1762 interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
1764 class = g_malloc0 (sizeof (MonoClass));
1766 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
1768 class->interfaces = interfaces;
1769 class->interface_count = icount;
1772 class->name_space = nspace;
1774 class->image = image;
1775 class->type_token = type_token;
1776 class->flags = cols [MONO_TYPEDEF_FLAGS];
1778 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
1780 /* fixme: maybe we must set this on windows
1781 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
1785 class->cast_class = class->element_class = class;
1787 /*g_print ("Load class %s\n", name);*/
1789 mono_class_setup_parent (class, parent);
1791 mono_class_setup_mono_type (class);
1794 * Compute the field and method lists
1796 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
1797 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
1799 if (tt->rows > tidx){
1800 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
1801 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1802 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1804 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
1805 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
1808 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
1809 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
1810 class->field.count = class->field.last - class->field.first;
1812 class->field.count = 0;
1814 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
1815 class->method.count = class->method.last - class->method.first;
1817 class->method.count = 0;
1819 /* reserve space to store vector pointer in arrays */
1820 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
1821 class->instance_size += 2 * sizeof (gpointer);
1822 g_assert (class->field.count == 0);
1825 if (class->enumtype)
1826 class_compute_field_layout (class);
1828 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
1829 class->nested_in = mono_class_create_from_typedef (image, type_token);
1831 class->gen_params = mono_metadata_load_generic_params (image, class->type_token, &icount);
1832 class->num_gen_params = icount;
1834 mono_loader_unlock ();
1840 mono_class_create_generic (MonoGenericInst *ginst)
1842 MonoClass *klass, *gklass;
1845 ginst->klass = g_malloc0 (sizeof (MonoClass));
1846 klass = ginst->klass;
1848 gklass = mono_class_from_mono_type (ginst->generic_type);
1850 klass->nested_in = gklass->nested_in;
1852 klass->name = gklass->name;
1853 klass->name_space = gklass->name_space;
1854 klass->image = gklass->image;
1855 klass->flags = gklass->flags;
1857 klass->generic_inst = ginst;
1859 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
1860 klass->this_arg.data.generic_inst = klass->byval_arg.data.generic_inst = ginst;
1861 klass->this_arg.byref = TRUE;
1863 klass->cast_class = klass->element_class = klass;
1865 if (ginst->is_dynamic) {
1866 klass->instance_size = gklass->instance_size;
1867 klass->class_size = gklass->class_size;
1868 klass->size_inited = 1;
1870 klass->valuetype = gklass->valuetype;
1877 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
1879 MonoClass *klass, **ptr;
1883 return param->pklass;
1885 klass = param->pklass = g_new0 (MonoClass, 1);
1887 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
1891 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
1892 klass->parent = param->constraints [0];
1896 if (count - pos > 0) {
1897 klass->interface_count = count - pos;
1898 klass->interfaces = g_new0 (MonoClass *, count - pos);
1899 for (i = pos; i < count; i++)
1900 klass->interfaces [i - pos] = param->constraints [i];
1903 g_assert (param->name);
1905 klass->name = param->name;
1906 klass->name_space = "";
1907 klass->image = image;
1908 klass->cast_class = klass->element_class = klass;
1909 klass->enum_basetype = &klass->element_class->byval_arg;
1910 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
1912 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
1913 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
1914 klass->this_arg.byref = TRUE;
1916 mono_class_init (klass);
1922 my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
1927 return param->pklass;
1929 klass = g_new0 (MonoClass, 1);
1932 klass->name = param->name;
1934 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
1935 klass->name_space = "";
1936 klass->image = mono_defaults.corlib;
1937 klass->cast_class = klass->element_class = klass;
1938 klass->enum_basetype = &klass->element_class->byval_arg;
1939 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
1941 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
1942 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
1943 klass->this_arg.byref = TRUE;
1945 mono_class_init (klass);
1951 mono_ptr_class_get (MonoType *type)
1954 MonoClass *el_class;
1955 static GHashTable *ptr_hash = NULL;
1957 mono_loader_lock ();
1960 ptr_hash = g_hash_table_new (NULL, NULL);
1961 el_class = mono_class_from_mono_type (type);
1962 if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
1963 mono_loader_unlock ();
1966 result = g_new0 (MonoClass, 1);
1968 result->parent = NULL; /* no parent for PTR types */
1969 result->name = "System";
1970 result->name_space = "MonoPtrFakeClass";
1971 result->image = el_class->image;
1972 result->inited = TRUE;
1973 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
1974 /* Can pointers get boxed? */
1975 result->instance_size = sizeof (gpointer);
1976 result->cast_class = result->element_class = el_class;
1977 result->enum_basetype = &result->element_class->byval_arg;
1978 result->blittable = TRUE;
1980 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
1981 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
1982 result->this_arg.byref = TRUE;
1984 mono_class_setup_supertypes (result);
1986 g_hash_table_insert (ptr_hash, el_class, result);
1988 mono_loader_unlock ();
1994 mono_fnptr_class_get (MonoMethodSignature *sig)
1997 static GHashTable *ptr_hash = NULL;
1999 mono_loader_lock ();
2002 ptr_hash = g_hash_table_new (NULL, NULL);
2004 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
2005 mono_loader_unlock ();
2008 result = g_new0 (MonoClass, 1);
2010 result->parent = NULL; /* no parent for PTR types */
2011 result->name = "System";
2012 result->name_space = "MonoFNPtrFakeClass";
2013 result->image = NULL; /* need to fix... */
2014 result->inited = TRUE;
2015 result->flags = TYPE_ATTRIBUTE_CLASS; // | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2016 /* Can pointers get boxed? */
2017 result->instance_size = sizeof (gpointer);
2018 result->cast_class = result->element_class = result;
2019 result->blittable = TRUE;
2021 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
2022 result->this_arg.data.method = result->byval_arg.data.method = sig;
2023 result->this_arg.byref = TRUE;
2024 result->enum_basetype = &result->element_class->byval_arg;
2025 result->blittable = TRUE;
2027 mono_class_setup_supertypes (result);
2029 g_hash_table_insert (ptr_hash, sig, result);
2031 mono_loader_unlock ();
2037 mono_class_from_mono_type (MonoType *type)
2039 switch (type->type) {
2040 case MONO_TYPE_OBJECT:
2041 return type->data.klass? type->data.klass: mono_defaults.object_class;
2042 case MONO_TYPE_VOID:
2043 return type->data.klass? type->data.klass: mono_defaults.void_class;
2044 case MONO_TYPE_BOOLEAN:
2045 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
2046 case MONO_TYPE_CHAR:
2047 return type->data.klass? type->data.klass: mono_defaults.char_class;
2049 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
2051 return type->data.klass? type->data.klass: mono_defaults.byte_class;
2053 return type->data.klass? type->data.klass: mono_defaults.int16_class;
2055 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
2057 return type->data.klass? type->data.klass: mono_defaults.int32_class;
2059 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
2061 return type->data.klass? type->data.klass: mono_defaults.int_class;
2063 return type->data.klass? type->data.klass: mono_defaults.uint_class;
2065 return type->data.klass? type->data.klass: mono_defaults.int64_class;
2067 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
2069 return type->data.klass? type->data.klass: mono_defaults.single_class;
2071 return type->data.klass? type->data.klass: mono_defaults.double_class;
2072 case MONO_TYPE_STRING:
2073 return type->data.klass? type->data.klass: mono_defaults.string_class;
2074 case MONO_TYPE_TYPEDBYREF:
2075 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
2076 case MONO_TYPE_ARRAY:
2077 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
2079 return mono_ptr_class_get (type->data.type);
2080 case MONO_TYPE_FNPTR:
2081 return mono_fnptr_class_get (type->data.method);
2082 case MONO_TYPE_SZARRAY:
2083 return mono_array_class_get (type->data.klass, 1);
2084 case MONO_TYPE_CLASS:
2085 case MONO_TYPE_VALUETYPE:
2086 return type->data.klass;
2087 case MONO_TYPE_GENERICINST:
2088 g_assert (type->data.generic_inst->klass);
2089 return type->data.generic_inst->klass;
2091 return my_mono_class_from_generic_parameter (type->data.generic_param, FALSE);
2092 case MONO_TYPE_MVAR:
2093 return my_mono_class_from_generic_parameter (type->data.generic_param, TRUE);
2095 g_warning ("implement me 0x%02x\n", type->type);
2096 g_assert_not_reached ();
2103 * @image: context where the image is created
2104 * @type_spec: typespec token
2107 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec,
2108 MonoGenericContext *context)
2110 MonoType *type, *inflated;
2113 type = mono_type_create_from_typespec (image, type_spec);
2115 switch (type->type) {
2116 case MONO_TYPE_ARRAY:
2117 class = mono_array_class_get (type->data.array->eklass, type->data.array->rank);
2119 case MONO_TYPE_SZARRAY:
2120 class = mono_array_class_get (type->data.klass, 1);
2123 class = mono_class_from_mono_type (type->data.type);
2125 case MONO_TYPE_GENERICINST:
2126 g_assert (type->data.generic_inst->klass);
2127 class = type->data.generic_inst->klass;
2130 /* it seems any type can be stored in TypeSpec as well */
2131 class = mono_class_from_mono_type (type);
2135 if (!class || !context)
2138 inflated = mono_class_inflate_generic_type (&class->byval_arg, context);
2140 return mono_class_from_mono_type (inflated);
2144 * mono_bounded_array_class_get:
2145 * @element_class: element class
2146 * @rank: the dimension of the array class
2147 * @bounded: whenever the array has non-zero bounds
2149 * Returns: a class object describing the array with element type @element_type and
2153 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
2157 MonoClass *parent = NULL;
2158 GSList *list, *rootlist;
2161 gboolean corlib_type = FALSE;
2163 g_assert (rank <= 255);
2166 /* bounded only matters for one-dimensional arrays */
2169 image = eclass->image;
2171 mono_loader_lock ();
2173 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
2174 for (; list; list = list->next) {
2176 if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
2177 mono_loader_unlock ();
2183 /* for the building corlib use System.Array from it */
2184 if (image->assembly && image->assembly->dynamic && strcmp (image->assembly_name, "mscorlib") == 0) {
2185 parent = mono_class_from_name (image, "System", "Array");
2188 parent = mono_defaults.array_class;
2189 if (!parent->inited)
2190 mono_class_init (parent);
2193 class = g_malloc0 (sizeof (MonoClass));
2195 class->image = image;
2196 class->name_space = eclass->name_space;
2197 nsize = strlen (eclass->name);
2198 name = g_malloc (nsize + 2 + rank);
2199 memcpy (name, eclass->name, nsize);
2202 memset (name + nsize + 1, ',', rank - 1);
2203 name [nsize + rank] = ']';
2204 name [nsize + rank + 1] = 0;
2206 class->type_token = 0;
2207 /* all arrays are marked serializable and sealed, bug #42779 */
2208 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
2209 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2210 class->parent = parent;
2211 class->instance_size = mono_class_instance_size (class->parent);
2212 class->class_size = 0;
2213 mono_class_setup_supertypes (class);
2217 if (eclass->enumtype)
2218 class->cast_class = eclass->element_class;
2220 class->cast_class = eclass;
2222 class->element_class = eclass;
2224 if ((rank > 1) || bounded) {
2225 MonoArrayType *at = g_new0 (MonoArrayType, 1);
2226 class->byval_arg.type = MONO_TYPE_ARRAY;
2227 class->byval_arg.data.array = at;
2228 at->eklass = eclass;
2230 /* FIXME: complete.... */
2232 class->byval_arg.type = MONO_TYPE_SZARRAY;
2233 class->byval_arg.data.klass = eclass;
2235 class->this_arg = class->byval_arg;
2236 class->this_arg.byref = 1;
2241 list = g_slist_append (rootlist, class);
2242 g_hash_table_insert (image->array_cache, eclass, list);
2244 mono_loader_unlock ();
2250 * mono_array_class_get:
2251 * @element_class: element class
2252 * @rank: the dimension of the array class
2254 * Returns: a class object describing the array with element type @element_type and
2258 mono_array_class_get (MonoClass *eclass, guint32 rank)
2260 return mono_bounded_array_class_get (eclass, rank, FALSE);
2264 * mono_class_instance_size:
2267 * Returns: the size of an object instance
2270 mono_class_instance_size (MonoClass *klass)
2272 if (!klass->size_inited)
2273 mono_class_init (klass);
2275 return klass->instance_size;
2279 * mono_class_min_align:
2282 * Returns: minimm alignment requirements
2285 mono_class_min_align (MonoClass *klass)
2287 if (!klass->size_inited)
2288 mono_class_init (klass);
2290 return klass->min_align;
2294 * mono_class_value_size:
2297 * This function is used for value types, and return the
2298 * space and the alignment to store that kind of value object.
2300 * Returns: the size of a value of kind @klass
2303 mono_class_value_size (MonoClass *klass, guint32 *align)
2307 /* fixme: check disable, because we still have external revereces to
2308 * mscorlib and Dummy Objects
2310 /*g_assert (klass->valuetype);*/
2312 size = mono_class_instance_size (klass) - sizeof (MonoObject);
2315 *align = klass->min_align;
2321 * mono_class_data_size:
2324 * Returns: the size of the static class data
2327 mono_class_data_size (MonoClass *klass)
2330 mono_class_init (klass);
2332 return klass->class_size;
2336 * Auxiliary routine to mono_class_get_field
2338 * Takes a field index instead of a field token.
2340 static MonoClassField *
2341 mono_class_get_field_idx (MonoClass *class, int idx)
2343 if (class->field.count){
2344 if ((idx >= class->field.first) && (idx < class->field.last)){
2345 return &class->fields [idx - class->field.first];
2352 return mono_class_get_field_idx (class->parent, idx);
2356 * mono_class_get_field:
2357 * @class: the class to lookup the field.
2358 * @field_token: the field token
2360 * Returns: A MonoClassField representing the type and offset of
2361 * the field, or a NULL value if the field does not belong to this
2365 mono_class_get_field (MonoClass *class, guint32 field_token)
2367 int idx = mono_metadata_token_index (field_token);
2369 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
2371 return mono_class_get_field_idx (class, idx - 1);
2375 mono_class_get_field_from_name (MonoClass *klass, const char *name)
2380 for (i = 0; i < klass->field.count; ++i) {
2381 if (strcmp (name, klass->fields [i].name) == 0)
2382 return &klass->fields [i];
2384 klass = klass->parent;
2390 mono_class_get_property_from_name (MonoClass *klass, const char *name)
2395 for (i = 0; i < klass->property.count; ++i) {
2396 if (strcmp (name, klass->properties [i].name) == 0)
2397 return &klass->properties [i];
2399 klass = klass->parent;
2406 * @image: the image where the class resides
2407 * @type_token: the token for the class
2408 * @at: an optional pointer to return the array element type
2410 * Returns: the MonoClass that represents @type_token in @image
2413 mono_class_get (MonoImage *image, guint32 type_token)
2415 MonoClass *class = NULL;
2418 return mono_lookup_dynamic_token (image, type_token);
2420 switch (type_token & 0xff000000){
2421 case MONO_TOKEN_TYPE_DEF:
2422 class = mono_class_create_from_typedef (image, type_token);
2424 case MONO_TOKEN_TYPE_REF:
2425 class = mono_class_from_typeref (image, type_token);
2427 case MONO_TOKEN_TYPE_SPEC:
2428 class = mono_class_create_from_typespec (image, type_token, NULL);
2431 g_warning ("unknown token type %x", type_token & 0xff000000);
2432 g_assert_not_reached ();
2436 g_warning ("Could not load class from token 0x%08x in %s", type_token, image->name);
2442 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
2444 MonoClass *class = mono_class_get (image, type_token);
2447 if (!class || !context)
2450 switch (class->byval_arg.type) {
2451 case MONO_TYPE_GENERICINST:
2452 if (!class->generic_inst->is_open)
2456 case MONO_TYPE_MVAR:
2462 inflated = inflate_generic_type (&class->byval_arg, context);
2466 return mono_class_from_mono_type (inflated);
2470 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
2472 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
2473 guint32 cols [MONO_TYPEDEF_SIZE];
2478 /* add a cache if needed */
2479 for (i = 1; i <= t->rows; ++i) {
2480 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
2481 /* nested types are accessed from the nesting name */
2482 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
2483 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
2485 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2486 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2487 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
2488 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
2494 return_nested_in (MonoClass *class, char *nested) {
2496 char *s = strchr (nested, '/');
2503 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
2505 if (strcmp (found->name, nested) == 0) {
2507 return return_nested_in (found, s);
2515 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
2517 GHashTable *nspace_table;
2518 MonoImage *loaded_image;
2524 if ((nested = strchr (name, '/'))) {
2525 int pos = nested - name;
2526 int len = strlen (name);
2529 memcpy (buf, name, len + 1);
2531 nested = buf + pos + 1;
2535 mono_loader_lock ();
2537 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
2540 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
2542 mono_loader_unlock ();
2547 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
2548 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
2549 guint32 cols [MONO_EXP_TYPE_SIZE];
2552 idx = mono_metadata_token_index (token);
2554 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
2556 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
2557 if ((impl & IMPLEMENTATION_MASK) == IMPLEMENTATION_FILE) {
2558 loaded_image = mono_assembly_load_module (image->assembly, impl >> IMPLEMENTATION_BITS);
2561 class = mono_class_from_name (loaded_image, name_space, name);
2563 return return_nested_in (class, nested);
2566 g_error ("not yet implemented");
2570 token = MONO_TOKEN_TYPE_DEF | token;
2572 class = mono_class_get (image, token);
2574 return return_nested_in (class, nested);
2579 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
2580 gboolean check_interfaces)
2583 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
2584 if ((klassc->interface_id <= klass->max_interface_id) &&
2585 (klass->interface_offsets [klassc->interface_id] >= 0))
2587 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
2590 for (i = 0; i < klass->interface_count; i ++) {
2591 MonoClass *ic = klass->interfaces [i];
2596 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
2598 if (klass->generic_inst) {
2599 MonoType *parent = klass->generic_inst->parent;
2603 if (mono_metadata_type_equal (parent, &klassc->byval_arg))
2605 klass = mono_class_from_mono_type (parent);
2611 * MS.NET thinks interfaces are a subclass of Object, so we think it as
2614 if (klassc == mono_defaults.object_class)
2621 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
2624 mono_class_init (klass);
2626 if (!oklass->inited)
2627 mono_class_init (oklass);
2629 if (MONO_CLASS_IS_INTERFACE (klass)) {
2630 if ((klass->interface_id <= oklass->max_interface_id) &&
2631 (oklass->interface_offsets [klass->interface_id] != -1))
2635 MonoClass *eclass, *eoclass;
2637 if (oklass->rank != klass->rank)
2640 /* vectors vs. one dimensional arrays */
2641 if (oklass->byval_arg.type != klass->byval_arg.type)
2644 eclass = klass->cast_class;
2645 eoclass = oklass->cast_class;
2649 * a is b does not imply a[] is b[] when a is a valuetype, and
2650 * b is a reference type.
2653 if (eoclass->valuetype) {
2654 if ((eclass == mono_defaults.enum_class) ||
2655 (eclass == mono_defaults.enum_class->parent) ||
2656 (eclass == mono_defaults.object_class))
2660 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
2663 if (klass == mono_defaults.object_class)
2666 return mono_class_has_parent (oklass, klass);
2670 * mono_class_needs_cctor_run:
2672 * Determines whenever the class has a static constructor and whenever it
2673 * needs to be called when executing CALLER.
2676 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
2681 for (i = 0; i < klass->method.count; ++i) {
2682 method = klass->methods [i];
2683 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2684 (strcmp (".cctor", method->name) == 0)) {
2685 if (caller == method)
2694 * Returns the nnumber of bytes an element of type klass
2695 * uses when stored into an array.
2698 mono_class_array_element_size (MonoClass *klass)
2700 int t = klass->byval_arg.type;
2706 case MONO_TYPE_BOOLEAN:
2710 case MONO_TYPE_CHAR:
2719 case MONO_TYPE_CLASS:
2720 case MONO_TYPE_STRING:
2721 case MONO_TYPE_OBJECT:
2722 case MONO_TYPE_SZARRAY:
2723 case MONO_TYPE_ARRAY:
2725 case MONO_TYPE_MVAR:
2726 return sizeof (gpointer);
2731 case MONO_TYPE_VALUETYPE:
2732 if (klass->enumtype) {
2733 t = klass->enum_basetype->type;
2736 return mono_class_instance_size (klass) - sizeof (MonoObject);
2738 g_error ("unknown type 0x%02x in mono_class_array_element_size", t);
2744 * mono_array_element_size:
2745 * @ac: pointer to a #MonoArrayClass
2747 * Returns: the size of single array element.
2750 mono_array_element_size (MonoClass *ac)
2752 return mono_class_array_element_size (ac->element_class);
2756 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
2757 MonoGenericContext *context)
2759 if (image->dynamic) {
2760 gpointer obj = mono_lookup_dynamic_token (image, token);
2762 switch (token & 0xff000000) {
2763 case MONO_TOKEN_TYPE_DEF:
2764 case MONO_TOKEN_TYPE_REF:
2765 case MONO_TOKEN_TYPE_SPEC:
2767 *handle_class = mono_defaults.typehandle_class;
2768 return &((MonoClass*)obj)->byval_arg;
2769 case MONO_TOKEN_METHOD_DEF:
2771 *handle_class = mono_defaults.methodhandle_class;
2773 case MONO_TOKEN_FIELD_DEF:
2775 *handle_class = mono_defaults.fieldhandle_class;
2778 g_assert_not_reached ();
2782 switch (token & 0xff000000) {
2783 case MONO_TOKEN_TYPE_DEF:
2784 case MONO_TOKEN_TYPE_REF: {
2787 *handle_class = mono_defaults.typehandle_class;
2788 class = mono_class_get_full (image, token, context);
2789 mono_class_init (class);
2790 /* We return a MonoType* as handle */
2791 return &class->byval_arg;
2793 case MONO_TOKEN_TYPE_SPEC: {
2796 *handle_class = mono_defaults.typehandle_class;
2797 class = mono_class_create_from_typespec (image, token, context);
2798 mono_class_init (class);
2799 return &class->byval_arg;
2801 case MONO_TOKEN_FIELD_DEF: {
2803 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
2804 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
2805 mono_class_init (class);
2807 *handle_class = mono_defaults.fieldhandle_class;
2808 return mono_class_get_field (class, token);
2810 case MONO_TOKEN_METHOD_DEF: {
2812 meth = mono_get_method_full (image, token, NULL, context);
2814 *handle_class = mono_defaults.methodhandle_class;
2817 case MONO_TOKEN_MEMBER_REF: {
2818 guint32 cols [MONO_MEMBERREF_SIZE];
2820 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
2821 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
2822 mono_metadata_decode_blob_size (sig, &sig);
2823 if (*sig == 0x6) { /* it's a field */
2825 MonoClassField *field;
2826 field = mono_field_from_token (image, token, &klass, context);
2828 *handle_class = mono_defaults.fieldhandle_class;
2832 meth = mono_get_method_full (image, token, NULL, context);
2834 *handle_class = mono_defaults.methodhandle_class;
2839 g_warning ("Unknown token 0x%08x in ldtoken", token);
2846 * This function might need to call runtime functions so it can't be part
2847 * of the metadata library.
2849 static MonoLookupDynamicToken lookup_dynamic = NULL;
2852 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
2854 lookup_dynamic = func;
2858 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
2860 return lookup_dynamic (image, token);