2 * class.c: Class management for the Mono runtime
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
9 * Possible Optimizations:
10 * in mono_class_create, do not allocate the class right away,
11 * but wait until you know the size of the FieldMap, so that
12 * the class embeds directly the FieldMap after the vtable.
22 #include <mono/metadata/image.h>
23 #include <mono/metadata/assembly.h>
24 #include <mono/metadata/cil-coff.h>
25 #include <mono/metadata/metadata.h>
26 #include <mono/metadata/metadata-internals.h>
27 #include <mono/metadata/tabledefs.h>
28 #include <mono/metadata/tokentype.h>
29 #include <mono/metadata/class-internals.h>
30 #include <mono/metadata/object.h>
31 #include <mono/metadata/appdomain.h>
32 #include <mono/metadata/mono-endian.h>
33 #include <mono/metadata/debug-helpers.h>
34 #include <mono/metadata/reflection.h>
35 #include <mono/os/gc_wrapper.h>
39 gboolean mono_print_vtable = FALSE;
41 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
43 void (*mono_debugger_start_class_init_func) (MonoClass *klass) = NULL;
44 void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
47 mono_class_from_typeref (MonoImage *image, guint32 type_token)
49 guint32 cols [MONO_TYPEREF_SIZE];
50 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
52 const char *name, *nspace;
54 MonoAssembly **references;
56 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
58 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
59 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
61 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
62 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
63 case MONO_RESOLTION_SCOPE_MODULE:
65 g_error ("null ResolutionScope not yet handled");
66 /* a typedef in disguise */
67 return mono_class_from_name (image, nspace, name);
68 case MONO_RESOLTION_SCOPE_MODULEREF:
69 return mono_class_from_name (image->modules [idx - 1], nspace, name);
70 case MONO_RESOLTION_SCOPE_TYPEREF: {
71 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
73 mono_class_init (enclosing);
74 for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
76 if (strcmp (res->name, name) == 0)
79 g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
82 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
86 references = image->references;
87 if (!references || !references [idx-1]) {
89 * detected a reference to mscorlib, we simply return a reference to a dummy
90 * until we have a better solution.
92 * once a better solution is in place, the System.MonoDummy
93 * class should be removed from CVS.
95 fprintf(stderr, "Sending dummy where %s.%s expected\n", mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]), mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]));
97 res = mono_class_from_name (image, "System", "MonoDummy");
98 /* prevent method loading */
100 /* some storage if the type is used - very ugly hack */
101 res->instance_size = 2*sizeof (gpointer);
105 /* load referenced assembly */
106 image = references [idx-1]->image;
108 return mono_class_from_name (image, nspace, name);
112 dup_type (MonoType* t, const MonoType *original)
114 MonoType *r = g_new0 (MonoType, 1);
116 r->attrs = original->attrs;
117 r->byref = original->byref;
118 mono_stats.generics_metadata_size += sizeof (MonoType);
123 mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed,
124 gboolean include_arity)
128 switch (type->type) {
129 case MONO_TYPE_ARRAY: {
130 int i, rank = type->data.array->rank;
132 mono_type_get_name_recurse (
133 &type->data.array->eklass->byval_arg, str, FALSE, include_arity);
134 g_string_append_c (str, '[');
135 for (i = 1; i < rank; i++)
136 g_string_append_c (str, ',');
137 g_string_append_c (str, ']');
140 case MONO_TYPE_SZARRAY:
141 mono_type_get_name_recurse (
142 &type->data.klass->byval_arg, str, FALSE, include_arity);
143 g_string_append (str, "[]");
146 mono_type_get_name_recurse (type->data.type, str, FALSE, include_arity);
147 g_string_append_c (str, '*');
150 klass = mono_class_from_mono_type (type);
151 if (klass->nested_in) {
152 mono_type_get_name_recurse (
153 &klass->nested_in->byval_arg, str, TRUE, include_arity);
154 g_string_append_c (str, '+');
156 if (*klass->name_space) {
157 g_string_append (str, klass->name_space);
158 g_string_append_c (str, '.');
160 if (!include_arity) {
161 char *s = strchr (klass->name, '`');
162 int len = s ? s - klass->name : strlen (klass->name);
164 g_string_append_len (str, klass->name, len);
166 g_string_append (str, klass->name);
169 if (klass->generic_inst) {
170 MonoGenericInst *ginst = klass->generic_inst;
173 g_string_append_c (str, '<');
174 for (i = 0; i < ginst->type_argc; i++) {
176 g_string_append_c (str, ',');
177 mono_type_get_name_recurse (
178 ginst->type_argv [i], str, FALSE, include_arity);
180 g_string_append_c (str, '>');
181 } else if (klass->gen_params) {
184 g_string_append_c (str, '<');
185 for (i = 0; i < klass->num_gen_params; i++) {
187 g_string_append_c (str, ',');
188 g_string_append (str, klass->gen_params [i].name);
190 g_string_append_c (str, '>');
197 * mono_type_get_name:
200 * Returns the string representation for type as required by System.Reflection.
201 * The inverse of mono_reflection_parse_type ().
204 _mono_type_get_name (MonoType *type, gboolean is_recursed, gboolean include_arity)
206 GString* result = g_string_new ("");
207 mono_type_get_name_recurse (type, result, is_recursed, include_arity);
210 g_string_append_c (result, '&');
212 return g_string_free (result, FALSE);
216 mono_type_get_name (MonoType *type)
218 return _mono_type_get_name (type, TRUE, TRUE);
222 mono_type_get_full_name (MonoType *type)
224 return _mono_type_get_name (type, FALSE, TRUE);
228 mono_class_is_open_constructed_type (MonoType *t)
234 case MONO_TYPE_SZARRAY:
235 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
236 case MONO_TYPE_ARRAY:
237 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
239 return mono_class_is_open_constructed_type (t->data.type);
240 case MONO_TYPE_GENERICINST: {
241 MonoGenericInst *ginst = t->data.generic_inst;
244 if (mono_class_is_open_constructed_type (ginst->generic_type))
246 for (i = 0; i < ginst->type_argc; i++)
247 if (mono_class_is_open_constructed_type (ginst->type_argv [i]))
257 inflate_generic_type (MonoType *type, MonoGenericContext *context)
259 switch (type->type) {
261 if (context->gmethod && context->gmethod->mtype_argv)
263 context->gmethod->mtype_argv [type->data.generic_param->num],
270 context->ginst->type_argv [type->data.generic_param->num],
274 case MONO_TYPE_SZARRAY: {
275 MonoClass *eclass = type->data.klass;
276 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
279 nt = dup_type (type, type);
280 nt->data.klass = mono_class_from_mono_type (inflated);
283 case MONO_TYPE_GENERICINST: {
284 MonoGenericInst *oginst = type->data.generic_inst;
285 MonoGenericInst *nginst, *cached;
289 nginst = g_new0 (MonoGenericInst, 1);
292 nginst->is_open = FALSE;
294 nginst->type_argv = g_new0 (MonoType *, oginst->type_argc);
296 for (i = 0; i < oginst->type_argc; i++) {
297 MonoType *t = oginst->type_argv [i];
298 nginst->type_argv [i] = mono_class_inflate_generic_type (t, context);
300 if (!nginst->is_open)
301 nginst->is_open = mono_class_is_open_constructed_type (nginst->type_argv [i]);
304 nginst->klass = NULL;
306 nginst->context = g_new0 (MonoGenericContext, 1);
307 nginst->context->ginst = nginst;
310 cached = g_hash_table_lookup (oginst->klass->image->generic_inst_cache, nginst);
313 g_free (nginst->type_argv);
315 mono_loader_unlock ();
317 nt = dup_type (type, type);
318 nt->data.generic_inst = cached;
322 nginst->dynamic_info = NULL;
323 nginst->initialized = FALSE;
325 mono_class_create_generic (nginst);
327 mono_stats.generic_instance_count++;
328 mono_stats.generics_metadata_size += sizeof (MonoGenericInst) +
329 sizeof (MonoGenericContext) +
330 nginst->type_argc * sizeof (MonoType);
332 nt = dup_type (type, type);
333 nt->data.generic_inst = nginst;
334 g_hash_table_insert (oginst->klass->image->generic_inst_cache, nginst, nginst);
335 mono_loader_unlock ();
345 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
347 MonoType *inflated = inflate_generic_type (type, context);
352 mono_stats.inflated_type_count++;
356 static MonoMethodSignature*
357 inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig,
358 MonoGenericContext *context)
360 MonoMethodSignature *res;
362 res = mono_metadata_signature_alloc (image, sig->param_count);
363 res->ret = mono_class_inflate_generic_type (sig->ret, context);
364 for (i = 0; i < sig->param_count; ++i)
365 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
366 res->hasthis = sig->hasthis;
367 res->explicit_this = sig->explicit_this;
368 res->call_convention = sig->call_convention;
369 res->generic_param_count = sig->generic_param_count;
370 res->is_inflated = 1;
374 static MonoMethodHeader*
375 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
377 MonoMethodHeader *res;
379 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
380 res->code = header->code;
381 res->code_size = header->code_size;
382 res->max_stack = header->max_stack;
383 res->num_clauses = header->num_clauses;
384 res->init_locals = header->init_locals;
385 res->num_locals = header->num_locals;
386 res->clauses = header->clauses;
387 for (i = 0; i < header->num_locals; ++i)
388 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
393 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context,
396 MonoMethodInflated *result;
398 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
399 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
402 mono_stats.inflated_method_count++;
403 mono_stats.generics_metadata_size +=
404 sizeof (MonoMethodInflated) - sizeof (MonoMethodNormal);
406 result = g_new0 (MonoMethodInflated, 1);
407 result->nmethod = *(MonoMethodNormal*)method;
409 if (result->nmethod.header)
410 result->nmethod.header = inflate_generic_header (
411 result->nmethod.header, context);
414 result->nmethod.method.klass = klass;
416 MonoType *declaring = mono_class_inflate_generic_type (
417 &method->klass->byval_arg, context);
418 result->nmethod.method.klass = mono_class_from_mono_type (declaring);
421 result->nmethod.method.signature = inflate_generic_signature (
422 method->klass->image, method->signature, context);
424 if (context->gmethod) {
425 result->context = g_new0 (MonoGenericContext, 1);
426 result->context->gmethod = context->gmethod;
427 result->context->ginst = result->nmethod.method.klass->generic_inst;
429 mono_stats.generics_metadata_size += sizeof (MonoGenericContext);
431 result->context = result->nmethod.method.klass->generic_inst->context;
433 if (method->signature->is_inflated)
434 result->declaring = ((MonoMethodInflated *) method)->declaring;
436 result->declaring = method;
438 return (MonoMethod *) result;
442 * class_compute_field_layout:
443 * @m: pointer to the metadata.
444 * @class: The class to initialize
446 * Initializes the class->fields.
448 * Currently we only support AUTO_LAYOUT, and do not even try to do
449 * a good job at it. This is temporary to get the code for Paolo.
452 class_compute_field_layout (MonoClass *class)
454 MonoImage *m = class->image;
455 const int top = class->field.count;
456 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
457 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
458 int i, blittable = TRUE, real_size = 0;
460 guint32 packing_size = 0;
461 gboolean explicit_size;
462 MonoClassField *field;
464 if (class->size_inited)
468 if (!class->parent->size_inited)
469 class_compute_field_layout (class->parent);
470 class->instance_size += class->parent->instance_size;
471 class->min_align = class->parent->min_align;
472 blittable = class->parent->blittable;
474 class->instance_size = sizeof (MonoObject);
475 class->min_align = 1;
478 /* Get the real size */
479 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
482 g_assert ((packing_size & 0xfffffff0) == 0);
483 class->packing_size = packing_size;
484 real_size += class->instance_size;
488 if (explicit_size && real_size) {
489 class->instance_size = MAX (real_size, class->instance_size);
491 class->size_inited = 1;
492 class->blittable = blittable;
496 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT)
499 class->fields = g_new0 (MonoClassField, top);
502 * Fetch all the field information.
504 for (i = 0; i < top; i++){
506 guint32 cols [MONO_FIELD_SIZE];
507 int idx = class->field.first + i;
509 field = &class->fields [i];
510 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
511 /* The name is needed for fieldrefs */
512 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
513 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
514 mono_metadata_decode_value (sig, &sig);
515 /* FIELD signature == 0x06 */
516 g_assert (*sig == 0x06);
517 field->type = mono_metadata_parse_field_type (
518 m, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
519 if (mono_field_is_deleted (field))
521 if (class->generic_inst) {
522 field->type = mono_class_inflate_generic_type (
523 field->type, class->generic_inst->context);
524 field->type->attrs = cols [MONO_FIELD_FLAGS];
527 field->parent = class;
529 /* Only do these checks if we still think this type is blittable */
530 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
531 if (field->type->byref || MONO_TYPE_IS_REFERENCE (field->type)) {
534 MonoClass *field_class = mono_class_from_mono_type (field->type);
535 if (!field_class || !field_class->blittable)
540 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
541 mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
542 if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
543 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, field->name);
546 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
547 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
549 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
550 field->data = mono_image_rva_map (class->image, rva);
553 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
554 class->enum_basetype = field->type;
555 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
556 blittable = class->element_class->blittable;
559 /* The def_value of fields is compute lazily during vtable creation */
562 if (class == mono_defaults.string_class)
565 class->blittable = blittable;
567 if (class->enumtype && !class->enum_basetype) {
568 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
571 if (explicit_size && real_size) {
572 class->instance_size = MAX (real_size, class->instance_size);
575 if (class->gen_params ||
576 (class->generic_inst && class->generic_inst->is_open))
579 mono_class_layout_fields (class);
583 mono_class_layout_fields (MonoClass *class)
586 const int top = class->field.count;
587 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
588 guint32 pass, passes, real_size;
589 gboolean gc_aware_layout = FALSE;
590 MonoClassField *field;
593 * Enable GC aware auto layout: in this mode, reference
594 * fields are grouped together inside objects, increasing collector
596 * Requires that all classes whose layout is known to native code be annotated
597 * with [StructLayout (LayoutKind.Sequential)]
598 * Value types have gc_aware_layout disabled by default, as per
599 * what the default is for other runtimes.
601 /* corlib is missing [StructLayout] directives in many places */
602 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
603 if (class->image != mono_defaults.corlib &&
604 class->byval_arg.type != MONO_TYPE_VALUETYPE)
605 gc_aware_layout = TRUE;
609 * Compute field layout and total size (not considering static fields)
613 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
614 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
621 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
625 real_size = class->parent->instance_size;
627 real_size = sizeof (MonoObject);
629 for (pass = 0; pass < passes; ++pass) {
630 for (i = 0; i < top; i++){
632 field = &class->fields [i];
634 if (mono_field_is_deleted (field))
636 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
639 if (gc_aware_layout) {
641 * We process fields with reference type in the first pass,
642 * and fields with non-reference type in the second pass.
643 * We use IS_POINTER instead of IS_REFERENCE because in
644 * some internal structures, we store GC_MALLOCed memory
645 * in IntPtr fields...
647 if (MONO_TYPE_IS_POINTER (field->type)) {
656 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
657 (strcmp (field->name, "$PRIVATE$") == 0)) {
658 /* This field is a hack inserted by MCS to empty structures */
662 size = mono_type_size (field->type, &align);
664 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
665 align = class->packing_size ? MIN (class->packing_size, align): align;
666 class->min_align = MAX (align, class->min_align);
667 field->offset = real_size;
668 field->offset += align - 1;
669 field->offset &= ~(align - 1);
670 real_size = field->offset + size;
673 class->instance_size = MAX (real_size, class->instance_size);
675 if (class->instance_size & (class->min_align - 1)) {
676 class->instance_size += class->min_align - 1;
677 class->instance_size &= ~(class->min_align - 1);
681 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
683 for (i = 0; i < top; i++) {
685 field = &class->fields [i];
688 * There must be info about all the fields in a type if it
689 * uses explicit layout.
692 if (mono_field_is_deleted (field))
694 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
697 size = mono_type_size (field->type, &align);
700 * When we get here, field->offset is already set by the
701 * loader (for either runtime fields or fields loaded from metadata).
702 * The offset is from the start of the object: this works for both
703 * classes and valuetypes.
705 field->offset += sizeof (MonoObject);
710 real_size = MAX (real_size, size + field->offset);
712 class->instance_size = MAX (real_size, class->instance_size);
716 class->size_inited = 1;
719 * Compute static field layout and size
721 for (i = 0; i < top; i++){
723 field = &class->fields [i];
725 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
727 if (mono_field_is_deleted (field))
730 size = mono_type_size (field->type, &align);
731 field->offset = class->class_size;
732 field->offset += align - 1;
733 field->offset &= ~(align - 1);
734 class->class_size = field->offset + size;
739 init_properties (MonoClass *class)
741 guint startm, endm, i, j;
742 guint32 cols [MONO_PROPERTY_SIZE];
743 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
744 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
746 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
747 class->property.count = class->property.last - class->property.first;
749 class->properties = g_new0 (MonoProperty, class->property.count);
750 for (i = class->property.first; i < class->property.last; ++i) {
751 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
752 class->properties [i - class->property.first].parent = class;
753 class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
754 class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
756 startm = mono_metadata_methods_from_property (class->image, i, &endm);
757 for (j = startm; j < endm; ++j) {
758 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
759 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
760 case METHOD_SEMANTIC_SETTER:
761 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
763 case METHOD_SEMANTIC_GETTER:
764 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
774 init_events (MonoClass *class)
776 guint startm, endm, i, j;
777 guint32 cols [MONO_EVENT_SIZE];
778 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
779 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
781 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
782 class->event.count = class->event.last - class->event.first;
784 class->events = g_new0 (MonoEvent, class->event.count);
785 for (i = class->event.first; i < class->event.last; ++i) {
786 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
787 class->events [i - class->event.first].parent = class;
788 class->events [i - class->event.first].attrs = cols [MONO_EVENT_FLAGS];
789 class->events [i - class->event.first].name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
791 startm = mono_metadata_methods_from_event (class->image, i, &endm);
792 for (j = startm; j < endm; ++j) {
793 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
794 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
795 case METHOD_SEMANTIC_ADD_ON:
796 class->events [i - class->event.first].add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
798 case METHOD_SEMANTIC_REMOVE_ON:
799 class->events [i - class->event.first].remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
801 case METHOD_SEMANTIC_FIRE:
802 class->events [i - class->event.first].raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
804 case METHOD_SEMANTIC_OTHER: /* don't care for now */
813 mono_get_unique_iid (MonoClass *class)
815 static GHashTable *iid_hash = NULL;
816 static guint iid = 0;
821 g_assert (MONO_CLASS_IS_INTERFACE (class));
826 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
828 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
830 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
831 mono_loader_unlock ();
835 g_hash_table_insert (iid_hash, str, (gpointer)iid);
839 mono_loader_unlock ();
845 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
850 for (i = 0; i < klass->interface_count; i++) {
851 ic = klass->interfaces [i];
854 *res = g_ptr_array_new ();
855 g_ptr_array_add (*res, ic);
857 collect_implemented_interfaces_aux (ic, res);
861 static inline GPtrArray*
862 collect_implemented_interfaces (MonoClass *klass)
864 GPtrArray *res = NULL;
866 collect_implemented_interfaces_aux (klass, &res);
871 setup_interface_offsets (MonoClass *class, int cur_slot)
877 /* compute maximum number of slots and maximum interface id */
879 for (k = class; k ; k = k->parent) {
880 for (i = 0; i < k->interface_count; i++) {
881 ic = k->interfaces [i];
884 mono_class_init (ic);
886 if (max_iid < ic->interface_id)
887 max_iid = ic->interface_id;
891 if (MONO_CLASS_IS_INTERFACE (class)) {
892 if (max_iid < class->interface_id)
893 max_iid = class->interface_id;
895 class->max_interface_id = max_iid;
896 /* compute vtable offset for interfaces */
897 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
899 for (i = 0; i <= max_iid; i++)
900 class->interface_offsets [i] = -1;
902 ifaces = collect_implemented_interfaces (class);
904 for (i = 0; i < ifaces->len; ++i) {
905 ic = g_ptr_array_index (ifaces, i);
906 class->interface_offsets [ic->interface_id] = cur_slot;
907 cur_slot += ic->method.count;
909 g_ptr_array_free (ifaces, TRUE);
912 for (k = class->parent; k ; k = k->parent) {
913 ifaces = collect_implemented_interfaces (k);
915 for (i = 0; i < ifaces->len; ++i) {
916 ic = g_ptr_array_index (ifaces, i);
918 if (class->interface_offsets [ic->interface_id] == -1) {
919 int io = k->interface_offsets [ic->interface_id];
923 class->interface_offsets [ic->interface_id] = io;
926 g_ptr_array_free (ifaces, TRUE);
930 if (MONO_CLASS_IS_INTERFACE (class))
931 class->interface_offsets [class->interface_id] = cur_slot;
937 mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
941 int i, max_vtsize = 0, max_iid, cur_slot = 0;
943 MonoGHashTable *override_map = NULL;
945 /* setup_vtable() must be called only once on the type */
946 if (class->interface_offsets) {
947 g_warning ("vtable already computed in %s.%s", class->name_space, class->name);
951 ifaces = collect_implemented_interfaces (class);
953 for (i = 0; i < ifaces->len; i++) {
954 MonoClass *ic = g_ptr_array_index (ifaces, i);
955 max_vtsize += ic->method.count;
957 g_ptr_array_free (ifaces, TRUE);
961 max_vtsize += class->parent->vtable_size;
962 cur_slot = class->parent->vtable_size;
965 max_vtsize += class->method.count;
967 vtable = alloca (sizeof (gpointer) * max_vtsize);
968 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
970 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
972 cur_slot = setup_interface_offsets (class, cur_slot);
973 max_iid = class->max_interface_id;
975 if (class->parent && class->parent->vtable_size)
976 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
978 /* override interface methods */
979 for (i = 0; i < onum; i++) {
980 MonoMethod *decl = overrides [i*2];
981 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
983 g_assert (decl->slot != -1);
984 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
985 vtable [dslot] = overrides [i*2 + 1];
986 vtable [dslot]->slot = dslot;
988 override_map = mono_g_hash_table_new (NULL, NULL);
990 mono_g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
994 for (k = class; k ; k = k->parent) {
996 ifaces = collect_implemented_interfaces (k);
998 nifaces = ifaces->len;
999 for (i = 0; i < nifaces; i++) {
1002 ic = g_ptr_array_index (ifaces, i);
1003 io = k->interface_offsets [ic->interface_id];
1006 g_assert (io <= max_vtsize);
1009 for (l = 0; l < ic->method.count; l++) {
1010 MonoMethod *im = ic->methods [l];
1012 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1015 for (j = 0; j < class->method.count; ++j) {
1016 MonoMethod *cm = class->methods [j];
1017 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1018 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
1019 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
1021 if (!strcmp(cm->name, im->name) &&
1022 mono_metadata_signature_equal (cm->signature, im->signature)) {
1023 g_assert (io + l <= max_vtsize);
1024 vtable [io + l] = cm;
1029 /* already implemented */
1030 if (io >= k->vtable_size)
1034 for (l = 0; l < ic->method.count; l++) {
1035 MonoMethod *im = ic->methods [l];
1038 g_assert (io + l <= max_vtsize);
1040 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1043 for (k1 = class; k1; k1 = k1->parent) {
1044 for (j = 0; j < k1->method.count; ++j) {
1045 MonoMethod *cm = k1->methods [j];
1047 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1048 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
1051 if (!strcmp(cm->name, im->name) &&
1052 mono_metadata_signature_equal (cm->signature, im->signature)) {
1053 g_assert (io + l <= max_vtsize);
1054 vtable [io + l] = cm;
1059 g_assert (io + l <= max_vtsize);
1060 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1065 for (l = 0; l < ic->method.count; l++) {
1066 MonoMethod *im = ic->methods [l];
1067 char *qname, *fqname, *cname, *the_cname;
1070 if (vtable [io + l])
1073 if (ic->generic_inst) {
1074 MonoClass *the_ic = mono_class_from_mono_type (ic->generic_inst->generic_type);
1075 the_cname = _mono_type_get_name (&the_ic->byval_arg, TRUE, FALSE);
1082 qname = g_strconcat (cname, ".", im->name, NULL);
1083 if (ic->name_space && ic->name_space [0])
1084 fqname = g_strconcat (ic->name_space, ".", cname, ".", im->name, NULL);
1088 for (k1 = class; k1; k1 = k1->parent) {
1089 for (j = 0; j < k1->method.count; ++j) {
1090 MonoMethod *cm = k1->methods [j];
1092 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1095 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
1096 mono_metadata_signature_equal (cm->signature, im->signature)) {
1097 g_assert (io + l <= max_vtsize);
1098 vtable [io + l] = cm;
1109 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
1110 for (l = 0; l < ic->method.count; l++) {
1112 MonoMethod *im = ic->methods [l];
1113 if (im->flags & METHOD_ATTRIBUTE_STATIC)
1115 g_assert (io + l <= max_vtsize);
1118 * If one of our parents already implements this interface
1119 * we can inherit the implementation.
1121 if (!(vtable [io + l])) {
1122 MonoClass *parent = class->parent;
1124 if ((ic->interface_id <= parent->max_interface_id) &&
1125 (parent->interface_offsets [ic->interface_id]) &&
1127 vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
1130 if (!(vtable [io + l])) {
1131 for (j = 0; j < onum; ++j) {
1132 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
1133 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
1135 msig = mono_signature_get_desc (im->signature, FALSE);
1136 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
1137 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
1139 for (j = 0; j < class->method.count; ++j) {
1140 MonoMethod *cm = class->methods [j];
1141 msig = mono_signature_get_desc (cm->signature, FALSE);
1143 printf ("METHOD %s(%s)\n", cm->name, msig);
1146 g_assert_not_reached ();
1151 for (l = 0; l < ic->method.count; l++) {
1152 MonoMethod *im = vtable [io + l];
1155 g_assert (io + l <= max_vtsize);
1157 /* FIXME: why do we need this ? */
1159 /* g_assert_not_reached (); */
1165 g_ptr_array_free (ifaces, TRUE);
1168 for (i = 0; i < class->method.count; ++i) {
1171 cm = class->methods [i];
1174 * Non-virtual method have no place in the vtable.
1175 * This also catches static methods (since they are not virtual).
1177 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1181 * If the method is REUSE_SLOT, we must check in the
1182 * base class for a method to override.
1184 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
1186 for (k = class->parent; k ; k = k->parent) {
1188 for (j = 0; j < k->method.count; ++j) {
1189 MonoMethod *m1 = k->methods [j];
1190 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
1192 if (!strcmp(cm->name, m1->name) &&
1193 mono_metadata_signature_equal (cm->signature, m1->signature)) {
1194 slot = k->methods [j]->slot;
1195 g_assert (cm->slot < max_vtsize);
1197 override_map = mono_g_hash_table_new (NULL, NULL);
1198 mono_g_hash_table_insert (override_map, m1, cm);
1210 cm->slot = cur_slot++;
1212 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT) && !cm->signature->generic_param_count)
1213 vtable [cm->slot] = cm;
1216 /* override non interface methods */
1217 for (i = 0; i < onum; i++) {
1218 MonoMethod *decl = overrides [i*2];
1219 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
1220 g_assert (decl->slot != -1);
1221 vtable [decl->slot] = overrides [i*2 + 1];
1222 overrides [i * 2 + 1]->slot = decl->slot;
1224 override_map = mono_g_hash_table_new (NULL, NULL);
1225 mono_g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
1230 * If a method occupies more than one place in the vtable, and it is
1231 * overriden, then change the other occurances too.
1234 for (i = 0; i < max_vtsize; ++i)
1236 MonoMethod *cm = mono_g_hash_table_lookup (override_map, vtable [i]);
1241 mono_g_hash_table_destroy (override_map);
1244 if (class->generic_inst) {
1245 MonoClass *gklass = mono_class_from_mono_type (class->generic_inst->generic_type);
1247 mono_class_init (gklass);
1248 class->vtable_size = gklass->vtable_size;
1250 class->vtable_size = cur_slot;
1252 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
1253 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
1255 if (mono_print_vtable) {
1258 for (i = 0; i <= max_iid; i++)
1259 if (class->interface_offsets [i] != -1)
1262 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
1263 class->name, class->vtable_size, icount);
1265 for (i = 0; i < class->vtable_size; ++i) {
1270 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
1271 cm->klass->name_space, cm->klass->name,
1278 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
1279 class->name, max_iid);
1281 for (i = 0; i < class->interface_count; i++) {
1282 ic = class->interfaces [i];
1283 printf (" slot %03d(%03d) %s.%s\n",
1284 class->interface_offsets [ic->interface_id],
1285 ic->method.count, ic->name_space, ic->name);
1288 for (k = class->parent; k ; k = k->parent) {
1289 for (i = 0; i < k->interface_count; i++) {
1290 ic = k->interfaces [i];
1291 printf (" slot %03d(%03d) %s.%s\n",
1292 class->interface_offsets [ic->interface_id],
1293 ic->method.count, ic->name_space, ic->name);
1302 * @class: the class to initialize
1304 * compute the instance_size, class_size and other infos that cannot be
1305 * computed at mono_class_get() time. Also compute a generic vtable and
1306 * the method slot numbers. We use this infos later to create a domain
1310 mono_class_init (MonoClass *class)
1313 static MonoMethod *default_ghc = NULL;
1314 static MonoMethod *default_finalize = NULL;
1315 static int finalize_slot = -1;
1316 static int ghc_slot = -1;
1317 MonoMethod **overrides;
1325 /*g_print ("Init class %s\n", class->name);*/
1327 /* We do everything inside the lock to prevent races */
1328 mono_loader_lock ();
1330 if (class->inited) {
1331 mono_loader_unlock ();
1332 /* Somebody might have gotten in before us */
1336 if (class->init_pending) {
1337 /* this indicates a cyclic dependency */
1338 g_error ("pending init %s.%s\n", class->name_space, class->name);
1341 class->init_pending = 1;
1343 if (mono_debugger_start_class_init_func)
1344 mono_debugger_start_class_init_func (class);
1346 mono_stats.initialized_class_count++;
1348 if (class->generic_inst && !class->generic_inst->is_dynamic) {
1349 MonoGenericInst *ginst = class->generic_inst;
1353 gklass = mono_class_from_mono_type (ginst->generic_type);
1354 mono_class_init (gklass);
1357 class->parent = mono_class_from_mono_type (ginst->parent);
1359 class->parent = gklass->parent;
1361 mono_class_setup_parent (class, class->parent);
1363 if (MONO_CLASS_IS_INTERFACE (class))
1364 class->interface_id = mono_get_unique_iid (class);
1366 class->method = gklass->method;
1367 class->methods = g_new0 (MonoMethod *, class->method.count);
1369 for (i = 0; i < class->method.count; i++)
1370 class->methods [i] = mono_class_inflate_generic_method (
1371 gklass->methods [i], ginst->context, ginst->klass);
1373 class->field = gklass->field;
1374 class->fields = g_new0 (MonoClassField, class->field.count);
1376 for (i = 0; i < class->field.count; i++) {
1377 MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
1378 ifield->generic_type = gklass->fields [i].type;
1380 class->fields [i] = gklass->fields [i];
1381 class->fields [i].generic_info = ifield;
1382 class->fields [i].parent = class;
1383 class->fields [i].type = mono_class_inflate_generic_type (
1384 class->fields [i].type, ginst->context);
1387 class->property = gklass->property;
1388 class->properties = g_new0 (MonoProperty, class->property.count);
1390 for (i = 0; i < class->property.count; i++) {
1391 MonoProperty *prop = &class->properties [i];
1393 *prop = gklass->properties [i];
1396 prop->get = mono_class_inflate_generic_method (
1397 prop->get, ginst->context, ginst->klass);
1399 prop->set = mono_class_inflate_generic_method (
1400 prop->set, ginst->context, ginst->klass);
1402 prop->parent = class;
1405 class->interface_count = gklass->interface_count;
1406 class->interfaces = g_new0 (MonoClass *, class->interface_count);
1407 for (i = 0; i < class->interface_count; i++) {
1408 MonoType *it = &gklass->interfaces [i]->byval_arg;
1409 MonoType *inflated = mono_class_inflate_generic_type (
1410 it, ginst->context);
1411 class->interfaces [i] = mono_class_from_mono_type (inflated);
1412 mono_class_init (class->interfaces [i]);
1415 for (list = gklass->nested_classes; list; list = list->next)
1416 class->nested_classes = g_list_append (
1417 class->nested_classes, list->data);
1420 if (class->parent && !class->parent->inited)
1421 mono_class_init (class->parent);
1424 * Computes the size used by the fields, and their locations
1426 if (!class->size_inited)
1427 class_compute_field_layout (class);
1429 /* initialize method pointers */
1432 MonoMethodSignature *sig;
1433 class->method.count = class->rank > 1? 2: 1;
1434 sig = mono_metadata_signature_alloc (class->image, class->rank);
1435 sig->ret = &mono_defaults.void_class->byval_arg;
1436 sig->pinvoke = TRUE;
1437 for (i = 0; i < class->rank; ++i)
1438 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1440 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1441 ctor->klass = class;
1442 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1443 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1444 ctor->signature = sig;
1445 ctor->name = ".ctor";
1447 class->methods = g_new (MonoMethod*, class->method.count);
1448 class->methods [0] = ctor;
1449 if (class->rank > 1) {
1450 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
1451 sig->ret = &mono_defaults.void_class->byval_arg;
1452 sig->pinvoke = TRUE;
1453 for (i = 0; i < class->rank * 2; ++i)
1454 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1456 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1457 ctor->klass = class;
1458 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1459 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1460 ctor->signature = sig;
1461 ctor->name = ".ctor";
1463 class->methods [1] = ctor;
1466 if (!class->generic_inst && !class->methods) {
1467 class->methods = g_new (MonoMethod*, class->method.count);
1468 for (i = 0; i < class->method.count; ++i) {
1469 class->methods [i] = mono_get_method (class->image,
1470 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
1475 if (!class->generic_inst) {
1476 init_properties (class);
1477 init_events (class);
1479 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
1482 guint32 cols [MONO_NESTED_CLASS_SIZE];
1483 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
1484 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
1485 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
1487 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
1491 mono_class_setup_supertypes (class);
1493 if (MONO_CLASS_IS_INTERFACE (class)) {
1494 for (i = 0; i < class->method.count; ++i)
1495 class->methods [i]->slot = i;
1496 class->init_pending = 0;
1499 * class->interface_offsets is needed for the castclass/isinst code, so
1500 * we have to setup them for interfaces, too.
1502 setup_interface_offsets (class, 0);
1503 mono_loader_unlock ();
1505 if (mono_debugger_class_init_func)
1506 mono_debugger_class_init_func (class);
1511 overrides = mono_class_get_overrides (class->image, class->type_token, &onum);
1512 mono_class_setup_vtable (class, overrides, onum);
1516 class->init_pending = 0;
1519 if (class == mono_defaults.object_class) {
1521 for (i = 0; i < class->vtable_size; ++i) {
1522 MonoMethod *cm = class->vtable [i];
1524 if (!strcmp (cm->name, "GetHashCode")) {
1530 g_assert (ghc_slot > 0);
1532 default_ghc = class->vtable [ghc_slot];
1537 if (class->parent) {
1539 if (class->vtable [ghc_slot] == default_ghc) {
1544 if (!default_finalize) {
1545 if (class == mono_defaults.object_class) {
1547 for (i = 0; i < class->vtable_size; ++i) {
1548 MonoMethod *cm = class->vtable [i];
1550 if (!strcmp (cm->name, "Finalize")) {
1556 g_assert (finalize_slot > 0);
1558 default_finalize = class->vtable [finalize_slot];
1562 /* Object::Finalize should have empty implemenatation */
1563 class->has_finalize = 0;
1564 if (class->parent) {
1565 if (class->vtable [finalize_slot] != default_finalize)
1566 class->has_finalize = 1;
1569 mono_loader_unlock ();
1571 if (mono_debugger_class_init_func)
1572 mono_debugger_class_init_func (class);
1576 mono_class_setup_mono_type (MonoClass *class)
1578 const char *name = class->name;
1579 const char *nspace = class->name_space;
1581 if (MONO_CLASS_IS_INTERFACE (class))
1582 class->interface_id = mono_get_unique_iid (class);
1584 class->this_arg.byref = 1;
1585 class->this_arg.data.klass = class;
1586 class->this_arg.type = MONO_TYPE_CLASS;
1587 class->byval_arg.data.klass = class;
1588 class->byval_arg.type = MONO_TYPE_CLASS;
1590 if (!strcmp (nspace, "System")) {
1591 if (!strcmp (name, "ValueType")) {
1593 * do not set the valuetype bit for System.ValueType.
1594 * class->valuetype = 1;
1596 class->blittable = TRUE;
1597 } else if (!strcmp (name, "Enum")) {
1599 * do not set the valuetype bit for System.Enum.
1600 * class->valuetype = 1;
1602 class->valuetype = 0;
1603 class->enumtype = 0;
1604 } else if (!strcmp (name, "Object")) {
1605 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
1606 } else if (!strcmp (name, "String")) {
1607 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
1608 } else if (!strcmp (name, "TypedReference")) {
1609 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
1613 if (class->valuetype) {
1614 int t = MONO_TYPE_VALUETYPE;
1615 if (!strcmp (nspace, "System")) {
1618 if (!strcmp (name, "Boolean")) {
1619 t = MONO_TYPE_BOOLEAN;
1620 } else if (!strcmp(name, "Byte")) {
1622 class->blittable = TRUE;
1626 if (!strcmp (name, "Char")) {
1631 if (!strcmp (name, "Double")) {
1633 class->blittable = TRUE;
1637 if (!strcmp (name, "Int32")) {
1639 class->blittable = TRUE;
1640 } else if (!strcmp(name, "Int16")) {
1642 class->blittable = TRUE;
1643 } else if (!strcmp(name, "Int64")) {
1645 class->blittable = TRUE;
1646 } else if (!strcmp(name, "IntPtr")) {
1648 class->blittable = TRUE;
1652 if (!strcmp (name, "Single")) {
1654 class->blittable = TRUE;
1655 } else if (!strcmp(name, "SByte")) {
1657 class->blittable = TRUE;
1661 if (!strcmp (name, "UInt32")) {
1663 class->blittable = TRUE;
1664 } else if (!strcmp(name, "UInt16")) {
1666 class->blittable = TRUE;
1667 } else if (!strcmp(name, "UInt64")) {
1669 class->blittable = TRUE;
1670 } else if (!strcmp(name, "UIntPtr")) {
1672 class->blittable = TRUE;
1676 if (!strcmp (name, "TypedReference")) {
1677 t = MONO_TYPE_TYPEDBYREF;
1678 class->blittable = TRUE;
1682 if (!strcmp (name, "Void")) {
1690 class->this_arg.type = class->byval_arg.type = t;
1695 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
1697 gboolean system_namespace;
1699 system_namespace = !strcmp (class->name_space, "System");
1701 /* if root of the hierarchy */
1702 if (system_namespace && !strcmp (class->name, "Object")) {
1703 class->parent = NULL;
1704 class->instance_size = sizeof (MonoObject);
1707 if (!strcmp (class->name, "<Module>")) {
1708 class->parent = NULL;
1709 class->instance_size = 0;
1713 if (!MONO_CLASS_IS_INTERFACE (class)) {
1714 class->parent = parent;
1717 g_assert_not_reached (); /* FIXME */
1719 if (parent->generic_inst && !parent->name) {
1721 * If the parent is a generic instance, we may get
1722 * called before it is fully initialized, especially
1723 * before it has its name.
1728 class->marshalbyref = parent->marshalbyref;
1729 class->contextbound = parent->contextbound;
1730 class->delegate = parent->delegate;
1732 if (system_namespace) {
1733 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
1734 class->marshalbyref = 1;
1736 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
1737 class->contextbound = 1;
1739 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
1740 class->delegate = 1;
1743 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
1744 (strcmp (class->parent->name_space, "System") == 0)))
1745 class->valuetype = 1;
1746 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
1747 class->valuetype = class->enumtype = 1;
1749 /*class->enumtype = class->parent->enumtype; */
1750 mono_class_setup_supertypes (class);
1752 class->parent = NULL;
1758 mono_class_setup_supertypes (MonoClass *class)
1763 if (class->supertypes)
1767 for (k = class; k ; k = k->parent) {
1771 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
1772 class->supertypes = g_new0 (MonoClass *, ms);
1774 if (class->parent) {
1775 for (i = class->idepth, k = class; k ; k = k->parent)
1776 class->supertypes [--i] = k;
1778 class->supertypes [0] = class;
1783 * @image: context where the image is created
1784 * @type_token: typedef token
1787 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
1789 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
1790 MonoClass *class, *parent = NULL;
1791 guint32 cols [MONO_TYPEDEF_SIZE];
1792 guint32 cols_next [MONO_TYPEDEF_SIZE];
1793 guint tidx = mono_metadata_token_index (type_token);
1794 const char *name, *nspace;
1796 MonoClass **interfaces;
1798 mono_loader_lock ();
1800 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
1801 mono_loader_unlock ();
1805 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
1807 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
1809 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1810 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1812 class = g_malloc0 (sizeof (MonoClass));
1815 class->name_space = nspace;
1817 class->image = image;
1818 class->type_token = type_token;
1819 class->flags = cols [MONO_TYPEDEF_FLAGS];
1821 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
1823 if (cols [MONO_TYPEDEF_EXTENDS])
1824 parent = mono_class_get (image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]));
1825 interfaces = mono_metadata_interfaces_from_typedef (image, type_token, &icount);
1827 class->interfaces = interfaces;
1828 class->interface_count = icount;
1830 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
1832 /* fixme: maybe we must set this on windows
1833 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
1837 class->cast_class = class->element_class = class;
1839 /*g_print ("Load class %s\n", name);*/
1841 mono_class_setup_parent (class, parent);
1843 mono_class_setup_mono_type (class);
1846 * Compute the field and method lists
1848 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
1849 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
1851 if (tt->rows > tidx){
1852 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
1853 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1854 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1856 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
1857 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
1860 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
1861 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
1862 class->field.count = class->field.last - class->field.first;
1864 class->field.count = 0;
1866 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
1867 class->method.count = class->method.last - class->method.first;
1869 class->method.count = 0;
1871 /* reserve space to store vector pointer in arrays */
1872 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
1873 class->instance_size += 2 * sizeof (gpointer);
1874 g_assert (class->field.count == 0);
1877 if (class->enumtype)
1878 class_compute_field_layout (class);
1880 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
1881 class->nested_in = mono_class_create_from_typedef (image, type_token);
1883 class->gen_params = mono_metadata_load_generic_params (image, class->type_token, &icount);
1884 class->num_gen_params = icount;
1886 mono_loader_unlock ();
1892 mono_class_create_generic (MonoGenericInst *ginst)
1894 MonoClass *klass, *gklass;
1897 ginst->klass = g_malloc0 (sizeof (MonoClass));
1898 klass = ginst->klass;
1900 gklass = mono_class_from_mono_type (ginst->generic_type);
1902 klass->nested_in = gklass->nested_in;
1904 klass->name = gklass->name;
1905 klass->name_space = gklass->name_space;
1906 klass->image = gklass->image;
1907 klass->flags = gklass->flags;
1909 klass->generic_inst = ginst;
1911 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
1912 klass->this_arg.data.generic_inst = klass->byval_arg.data.generic_inst = ginst;
1913 klass->this_arg.byref = TRUE;
1915 klass->cast_class = klass->element_class = klass;
1917 if (ginst->is_dynamic) {
1918 klass->instance_size = gklass->instance_size;
1919 klass->class_size = gklass->class_size;
1920 klass->size_inited = 1;
1922 klass->valuetype = gklass->valuetype;
1929 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
1931 MonoClass *klass, **ptr;
1935 return param->pklass;
1937 klass = param->pklass = g_new0 (MonoClass, 1);
1939 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
1943 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
1944 klass->parent = param->constraints [0];
1948 if (count - pos > 0) {
1949 klass->interface_count = count - pos;
1950 klass->interfaces = g_new0 (MonoClass *, count - pos);
1951 for (i = pos; i < count; i++)
1952 klass->interfaces [i - pos] = param->constraints [i];
1955 g_assert (param->name);
1957 klass->name = param->name;
1958 klass->name_space = "";
1959 klass->image = image;
1960 klass->cast_class = klass->element_class = klass;
1961 klass->enum_basetype = &klass->element_class->byval_arg;
1962 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
1964 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
1965 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
1966 klass->this_arg.byref = TRUE;
1968 mono_class_init (klass);
1974 my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
1979 return param->pklass;
1981 klass = g_new0 (MonoClass, 1);
1984 klass->name = param->name;
1986 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
1987 klass->name_space = "";
1988 klass->image = mono_defaults.corlib;
1989 klass->cast_class = klass->element_class = klass;
1990 klass->enum_basetype = &klass->element_class->byval_arg;
1991 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
1993 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
1994 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
1995 klass->this_arg.byref = TRUE;
1997 mono_class_init (klass);
2003 mono_ptr_class_get (MonoType *type)
2006 MonoClass *el_class;
2007 static GHashTable *ptr_hash = NULL;
2009 mono_loader_lock ();
2012 ptr_hash = g_hash_table_new (NULL, NULL);
2013 el_class = mono_class_from_mono_type (type);
2014 if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
2015 mono_loader_unlock ();
2018 result = g_new0 (MonoClass, 1);
2020 result->parent = NULL; /* no parent for PTR types */
2021 result->name_space = el_class->name_space;
2022 result->name = g_strdup_printf ("%s*", el_class->name);
2023 result->image = el_class->image;
2024 result->inited = TRUE;
2025 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2026 /* Can pointers get boxed? */
2027 result->instance_size = sizeof (gpointer);
2028 result->cast_class = result->element_class = el_class;
2029 result->enum_basetype = &result->element_class->byval_arg;
2030 result->blittable = TRUE;
2032 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
2033 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
2034 result->this_arg.byref = TRUE;
2036 mono_class_setup_supertypes (result);
2038 g_hash_table_insert (ptr_hash, el_class, result);
2040 mono_loader_unlock ();
2046 mono_fnptr_class_get (MonoMethodSignature *sig)
2049 static GHashTable *ptr_hash = NULL;
2051 mono_loader_lock ();
2054 ptr_hash = g_hash_table_new (NULL, NULL);
2056 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
2057 mono_loader_unlock ();
2060 result = g_new0 (MonoClass, 1);
2062 result->parent = NULL; /* no parent for PTR types */
2063 result->name = "System";
2064 result->name_space = "MonoFNPtrFakeClass";
2065 result->image = NULL; /* need to fix... */
2066 result->inited = TRUE;
2067 result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
2068 /* Can pointers get boxed? */
2069 result->instance_size = sizeof (gpointer);
2070 result->cast_class = result->element_class = result;
2071 result->blittable = TRUE;
2073 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
2074 result->this_arg.data.method = result->byval_arg.data.method = sig;
2075 result->this_arg.byref = TRUE;
2076 result->enum_basetype = &result->element_class->byval_arg;
2077 result->blittable = TRUE;
2079 mono_class_setup_supertypes (result);
2081 g_hash_table_insert (ptr_hash, sig, result);
2083 mono_loader_unlock ();
2089 mono_class_from_mono_type (MonoType *type)
2091 switch (type->type) {
2092 case MONO_TYPE_OBJECT:
2093 return type->data.klass? type->data.klass: mono_defaults.object_class;
2094 case MONO_TYPE_VOID:
2095 return type->data.klass? type->data.klass: mono_defaults.void_class;
2096 case MONO_TYPE_BOOLEAN:
2097 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
2098 case MONO_TYPE_CHAR:
2099 return type->data.klass? type->data.klass: mono_defaults.char_class;
2101 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
2103 return type->data.klass? type->data.klass: mono_defaults.byte_class;
2105 return type->data.klass? type->data.klass: mono_defaults.int16_class;
2107 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
2109 return type->data.klass? type->data.klass: mono_defaults.int32_class;
2111 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
2113 return type->data.klass? type->data.klass: mono_defaults.int_class;
2115 return type->data.klass? type->data.klass: mono_defaults.uint_class;
2117 return type->data.klass? type->data.klass: mono_defaults.int64_class;
2119 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
2121 return type->data.klass? type->data.klass: mono_defaults.single_class;
2123 return type->data.klass? type->data.klass: mono_defaults.double_class;
2124 case MONO_TYPE_STRING:
2125 return type->data.klass? type->data.klass: mono_defaults.string_class;
2126 case MONO_TYPE_TYPEDBYREF:
2127 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
2128 case MONO_TYPE_ARRAY:
2129 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
2131 return mono_ptr_class_get (type->data.type);
2132 case MONO_TYPE_FNPTR:
2133 return mono_fnptr_class_get (type->data.method);
2134 case MONO_TYPE_SZARRAY:
2135 return mono_array_class_get (type->data.klass, 1);
2136 case MONO_TYPE_CLASS:
2137 case MONO_TYPE_VALUETYPE:
2138 return type->data.klass;
2139 case MONO_TYPE_GENERICINST:
2140 g_assert (type->data.generic_inst->klass);
2141 return type->data.generic_inst->klass;
2143 return my_mono_class_from_generic_parameter (type->data.generic_param, FALSE);
2144 case MONO_TYPE_MVAR:
2145 return my_mono_class_from_generic_parameter (type->data.generic_param, TRUE);
2147 g_warning ("implement me 0x%02x\n", type->type);
2148 g_assert_not_reached ();
2155 * @image: context where the image is created
2156 * @type_spec: typespec token
2159 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec,
2160 MonoGenericContext *context)
2162 MonoType *type, *inflated;
2165 type = mono_type_create_from_typespec (image, type_spec);
2167 switch (type->type) {
2168 case MONO_TYPE_ARRAY:
2169 class = mono_array_class_get (type->data.array->eklass, type->data.array->rank);
2171 case MONO_TYPE_SZARRAY:
2172 class = mono_array_class_get (type->data.klass, 1);
2175 class = mono_ptr_class_get (type->data.type);
2177 case MONO_TYPE_GENERICINST:
2178 g_assert (type->data.generic_inst->klass);
2179 class = type->data.generic_inst->klass;
2182 /* it seems any type can be stored in TypeSpec as well */
2183 class = mono_class_from_mono_type (type);
2187 if (!class || !context)
2190 inflated = mono_class_inflate_generic_type (&class->byval_arg, context);
2192 return mono_class_from_mono_type (inflated);
2196 * mono_bounded_array_class_get:
2197 * @element_class: element class
2198 * @rank: the dimension of the array class
2199 * @bounded: whenever the array has non-zero bounds
2201 * Returns: a class object describing the array with element type @element_type and
2205 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
2209 MonoClass *parent = NULL;
2210 GSList *list, *rootlist;
2213 gboolean corlib_type = FALSE;
2215 g_assert (rank <= 255);
2218 /* bounded only matters for one-dimensional arrays */
2221 image = eclass->image;
2223 mono_loader_lock ();
2225 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
2226 for (; list; list = list->next) {
2228 if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
2229 mono_loader_unlock ();
2235 /* for the building corlib use System.Array from it */
2236 if (image->assembly && image->assembly->dynamic && strcmp (image->assembly_name, "mscorlib") == 0) {
2237 parent = mono_class_from_name (image, "System", "Array");
2240 parent = mono_defaults.array_class;
2241 if (!parent->inited)
2242 mono_class_init (parent);
2245 class = g_malloc0 (sizeof (MonoClass));
2247 class->image = image;
2248 class->name_space = eclass->name_space;
2249 nsize = strlen (eclass->name);
2250 name = g_malloc (nsize + 2 + rank);
2251 memcpy (name, eclass->name, nsize);
2254 memset (name + nsize + 1, ',', rank - 1);
2255 name [nsize + rank] = ']';
2256 name [nsize + rank + 1] = 0;
2258 class->type_token = 0;
2259 /* all arrays are marked serializable and sealed, bug #42779 */
2260 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
2261 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2262 class->parent = parent;
2263 class->instance_size = mono_class_instance_size (class->parent);
2264 class->class_size = 0;
2265 mono_class_setup_supertypes (class);
2269 if (eclass->enumtype)
2270 class->cast_class = eclass->element_class;
2272 class->cast_class = eclass;
2274 class->element_class = eclass;
2276 if ((rank > 1) || bounded) {
2277 MonoArrayType *at = g_new0 (MonoArrayType, 1);
2278 class->byval_arg.type = MONO_TYPE_ARRAY;
2279 class->byval_arg.data.array = at;
2280 at->eklass = eclass;
2282 /* FIXME: complete.... */
2284 class->byval_arg.type = MONO_TYPE_SZARRAY;
2285 class->byval_arg.data.klass = eclass;
2287 class->this_arg = class->byval_arg;
2288 class->this_arg.byref = 1;
2293 list = g_slist_append (rootlist, class);
2294 g_hash_table_insert (image->array_cache, eclass, list);
2296 mono_loader_unlock ();
2302 * mono_array_class_get:
2303 * @element_class: element class
2304 * @rank: the dimension of the array class
2306 * Returns: a class object describing the array with element type @element_type and
2310 mono_array_class_get (MonoClass *eclass, guint32 rank)
2312 return mono_bounded_array_class_get (eclass, rank, FALSE);
2316 * mono_class_instance_size:
2319 * Returns: the size of an object instance
2322 mono_class_instance_size (MonoClass *klass)
2324 if (!klass->size_inited)
2325 mono_class_init (klass);
2327 g_assert (!klass->gen_params &&
2328 (!klass->generic_inst || !klass->generic_inst->is_open));
2329 return klass->instance_size;
2333 * mono_class_min_align:
2336 * Returns: minimm alignment requirements
2339 mono_class_min_align (MonoClass *klass)
2341 if (!klass->size_inited)
2342 mono_class_init (klass);
2344 return klass->min_align;
2348 * mono_class_value_size:
2351 * This function is used for value types, and return the
2352 * space and the alignment to store that kind of value object.
2354 * Returns: the size of a value of kind @klass
2357 mono_class_value_size (MonoClass *klass, guint32 *align)
2361 /* fixme: check disable, because we still have external revereces to
2362 * mscorlib and Dummy Objects
2364 /*g_assert (klass->valuetype);*/
2366 size = mono_class_instance_size (klass) - sizeof (MonoObject);
2369 *align = klass->min_align;
2375 * mono_class_data_size:
2378 * Returns: the size of the static class data
2381 mono_class_data_size (MonoClass *klass)
2384 mono_class_init (klass);
2386 return klass->class_size;
2390 * Auxiliary routine to mono_class_get_field
2392 * Takes a field index instead of a field token.
2394 static MonoClassField *
2395 mono_class_get_field_idx (MonoClass *class, int idx)
2397 if (class->field.count){
2398 if ((idx >= class->field.first) && (idx < class->field.last)){
2399 return &class->fields [idx - class->field.first];
2406 return mono_class_get_field_idx (class->parent, idx);
2410 * mono_class_get_field:
2411 * @class: the class to lookup the field.
2412 * @field_token: the field token
2414 * Returns: A MonoClassField representing the type and offset of
2415 * the field, or a NULL value if the field does not belong to this
2419 mono_class_get_field (MonoClass *class, guint32 field_token)
2421 int idx = mono_metadata_token_index (field_token);
2423 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
2425 return mono_class_get_field_idx (class, idx - 1);
2429 mono_class_get_field_from_name (MonoClass *klass, const char *name)
2434 for (i = 0; i < klass->field.count; ++i) {
2435 if (strcmp (name, klass->fields [i].name) == 0)
2436 return &klass->fields [i];
2438 klass = klass->parent;
2444 mono_vtable_get_static_field_data (MonoVTable *vt)
2450 mono_class_get_property_from_name (MonoClass *klass, const char *name)
2455 for (i = 0; i < klass->property.count; ++i) {
2456 if (strcmp (name, klass->properties [i].name) == 0)
2457 return &klass->properties [i];
2459 klass = klass->parent;
2466 * @image: the image where the class resides
2467 * @type_token: the token for the class
2468 * @at: an optional pointer to return the array element type
2470 * Returns: the MonoClass that represents @type_token in @image
2473 mono_class_get (MonoImage *image, guint32 type_token)
2475 MonoClass *class = NULL;
2478 return mono_lookup_dynamic_token (image, type_token);
2480 switch (type_token & 0xff000000){
2481 case MONO_TOKEN_TYPE_DEF:
2482 class = mono_class_create_from_typedef (image, type_token);
2484 case MONO_TOKEN_TYPE_REF:
2485 class = mono_class_from_typeref (image, type_token);
2487 case MONO_TOKEN_TYPE_SPEC:
2488 class = mono_class_create_from_typespec (image, type_token, NULL);
2491 g_warning ("unknown token type %x", type_token & 0xff000000);
2492 g_assert_not_reached ();
2496 g_warning ("Could not load class from token 0x%08x in %s", type_token, image->name);
2502 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
2504 MonoClass *class = mono_class_get (image, type_token);
2507 if (!class || !context)
2510 switch (class->byval_arg.type) {
2511 case MONO_TYPE_GENERICINST:
2512 if (!class->generic_inst->is_open)
2516 case MONO_TYPE_MVAR:
2522 inflated = inflate_generic_type (&class->byval_arg, context);
2526 return mono_class_from_mono_type (inflated);
2530 * mono_class_from_name_case:
2531 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
2532 * @name_space: the type namespace
2533 * @name: the type short name.
2535 * Obtains a MonoClass with a given namespace and a given name which
2536 * is located in the given MonoImage. The namespace and name
2537 * lookups are case insensitive.
2539 * You can also pass `NULL' to the image, and that will lookup for
2540 * a type with the given namespace and name in all of the loaded
2541 * assemblies: notice that since there might be a name clash in this
2542 * case, passing NULL is not encouraged if you need a precise type.
2546 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
2548 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
2549 guint32 cols [MONO_TYPEDEF_SIZE];
2554 /* add a cache if needed */
2555 for (i = 1; i <= t->rows; ++i) {
2556 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
2557 /* nested types are accessed from the nesting name */
2558 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
2559 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
2561 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2562 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2563 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
2564 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
2570 return_nested_in (MonoClass *class, char *nested) {
2572 char *s = strchr (nested, '/');
2579 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
2581 if (strcmp (found->name, nested) == 0) {
2583 return return_nested_in (found, s);
2592 * mono_class_from_name_case:
2593 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
2594 * @name_space: the type namespace
2595 * @name: the type short name.
2597 * Obtains a MonoClass with a given namespace and a given name which
2598 * is located in the given MonoImage.
2600 * You can also pass `NULL' to the image, and that will lookup for
2601 * a type with the given namespace and name in all of the loaded
2602 * assemblies: notice that since there might be a name clash in this
2603 * case, passing NULL is not encouraged if you need a precise type.
2607 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
2609 GHashTable *nspace_table;
2610 MonoImage *loaded_image;
2616 if ((nested = strchr (name, '/'))) {
2617 int pos = nested - name;
2618 int len = strlen (name);
2621 memcpy (buf, name, len + 1);
2623 nested = buf + pos + 1;
2627 mono_loader_lock ();
2629 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
2632 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
2634 mono_loader_unlock ();
2639 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
2640 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
2641 guint32 cols [MONO_EXP_TYPE_SIZE];
2644 idx = mono_metadata_token_index (token);
2646 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
2648 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
2649 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
2650 loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
2653 class = mono_class_from_name (loaded_image, name_space, name);
2655 return return_nested_in (class, nested);
2658 g_error ("not yet implemented");
2662 token = MONO_TOKEN_TYPE_DEF | token;
2664 class = mono_class_get (image, token);
2666 return return_nested_in (class, nested);
2671 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
2672 gboolean check_interfaces)
2675 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
2676 if ((klassc->interface_id <= klass->max_interface_id) &&
2677 (klass->interface_offsets [klassc->interface_id] >= 0))
2679 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
2682 for (i = 0; i < klass->interface_count; i ++) {
2683 MonoClass *ic = klass->interfaces [i];
2688 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
2693 * MS.NET thinks interfaces are a subclass of Object, so we think it as
2696 if (klassc == mono_defaults.object_class)
2699 if (klass->generic_inst) {
2700 MonoType *parent = klass->generic_inst->parent;
2704 if (mono_metadata_type_equal (parent, &klassc->byval_arg))
2706 klass = mono_class_from_mono_type (parent);
2714 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
2717 mono_class_init (klass);
2719 if (!oklass->inited)
2720 mono_class_init (oklass);
2722 if (MONO_CLASS_IS_INTERFACE (klass)) {
2723 if ((klass->interface_id <= oklass->max_interface_id) &&
2724 (oklass->interface_offsets [klass->interface_id] != -1))
2728 MonoClass *eclass, *eoclass;
2730 if (oklass->rank != klass->rank)
2733 /* vectors vs. one dimensional arrays */
2734 if (oklass->byval_arg.type != klass->byval_arg.type)
2737 eclass = klass->cast_class;
2738 eoclass = oklass->cast_class;
2742 * a is b does not imply a[] is b[] when a is a valuetype, and
2743 * b is a reference type.
2746 if (eoclass->valuetype) {
2747 if ((eclass == mono_defaults.enum_class) ||
2748 (eclass == mono_defaults.enum_class->parent) ||
2749 (eclass == mono_defaults.object_class))
2753 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
2756 if (klass == mono_defaults.object_class)
2759 return mono_class_has_parent (oklass, klass);
2763 * mono_class_needs_cctor_run:
2765 * Determines whenever the class has a static constructor and whenever it
2766 * needs to be called when executing CALLER.
2769 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
2774 for (i = 0; i < klass->method.count; ++i) {
2775 method = klass->methods [i];
2776 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2777 (strcmp (".cctor", method->name) == 0)) {
2778 if (caller == method)
2787 * Returns the nnumber of bytes an element of type klass
2788 * uses when stored into an array.
2791 mono_class_array_element_size (MonoClass *klass)
2793 MonoType *type = &klass->byval_arg;
2796 switch (type->type) {
2799 case MONO_TYPE_BOOLEAN:
2803 case MONO_TYPE_CHAR:
2812 case MONO_TYPE_CLASS:
2813 case MONO_TYPE_STRING:
2814 case MONO_TYPE_OBJECT:
2815 case MONO_TYPE_SZARRAY:
2816 case MONO_TYPE_ARRAY:
2818 case MONO_TYPE_MVAR:
2819 return sizeof (gpointer);
2824 case MONO_TYPE_VALUETYPE:
2825 if (type->data.klass->enumtype) {
2826 type = type->data.klass->enum_basetype;
2827 klass = klass->element_class;
2830 return mono_class_instance_size (klass) - sizeof (MonoObject);
2831 case MONO_TYPE_GENERICINST:
2832 type = type->data.generic_inst->generic_type;
2835 g_error ("unknown type 0x%02x in mono_class_array_element_size", type->type);
2841 * mono_array_element_size:
2842 * @ac: pointer to a #MonoArrayClass
2844 * Returns: the size of single array element.
2847 mono_array_element_size (MonoClass *ac)
2849 return mono_class_array_element_size (ac->element_class);
2853 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
2854 MonoGenericContext *context)
2856 if (image->dynamic) {
2857 gpointer obj = mono_lookup_dynamic_token (image, token);
2859 switch (token & 0xff000000) {
2860 case MONO_TOKEN_TYPE_DEF:
2861 case MONO_TOKEN_TYPE_REF:
2862 case MONO_TOKEN_TYPE_SPEC:
2864 *handle_class = mono_defaults.typehandle_class;
2865 return &((MonoClass*)obj)->byval_arg;
2866 case MONO_TOKEN_METHOD_DEF:
2868 *handle_class = mono_defaults.methodhandle_class;
2870 case MONO_TOKEN_FIELD_DEF:
2872 *handle_class = mono_defaults.fieldhandle_class;
2875 g_assert_not_reached ();
2879 switch (token & 0xff000000) {
2880 case MONO_TOKEN_TYPE_DEF:
2881 case MONO_TOKEN_TYPE_REF: {
2884 *handle_class = mono_defaults.typehandle_class;
2885 class = mono_class_get_full (image, token, context);
2886 mono_class_init (class);
2887 /* We return a MonoType* as handle */
2888 return &class->byval_arg;
2890 case MONO_TOKEN_TYPE_SPEC: {
2893 *handle_class = mono_defaults.typehandle_class;
2894 class = mono_class_create_from_typespec (image, token, context);
2895 mono_class_init (class);
2896 return &class->byval_arg;
2898 case MONO_TOKEN_FIELD_DEF: {
2900 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
2901 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
2902 mono_class_init (class);
2904 *handle_class = mono_defaults.fieldhandle_class;
2905 return mono_class_get_field (class, token);
2907 case MONO_TOKEN_METHOD_DEF: {
2909 meth = mono_get_method_full (image, token, NULL, context);
2911 *handle_class = mono_defaults.methodhandle_class;
2914 case MONO_TOKEN_MEMBER_REF: {
2915 guint32 cols [MONO_MEMBERREF_SIZE];
2917 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
2918 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
2919 mono_metadata_decode_blob_size (sig, &sig);
2920 if (*sig == 0x6) { /* it's a field */
2922 MonoClassField *field;
2923 field = mono_field_from_token (image, token, &klass, context);
2925 *handle_class = mono_defaults.fieldhandle_class;
2929 meth = mono_get_method_full (image, token, NULL, context);
2931 *handle_class = mono_defaults.methodhandle_class;
2936 g_warning ("Unknown token 0x%08x in ldtoken", token);
2943 * This function might need to call runtime functions so it can't be part
2944 * of the metadata library.
2946 static MonoLookupDynamicToken lookup_dynamic = NULL;
2949 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
2951 lookup_dynamic = func;
2955 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
2957 return lookup_dynamic (image, token);
2961 mono_class_get_image (MonoClass *klass)
2963 return klass->image;
2967 * mono_class_get_element_class:
2968 * @klass: the MonoClass to act on
2970 * Returns the element class of an array or an enumeration.
2973 mono_class_get_element_class (MonoClass *klass)
2975 return klass->element_class;
2979 * mono_class_is_valuetype:
2980 * @klass: the MonoClass to act on
2982 * Returns true if the MonoClass represents a ValueType.
2985 mono_class_is_valuetype (MonoClass *klass)
2987 return klass->valuetype;
2991 * mono_class_is_enum:
2992 * @klass: the MonoClass to act on
2994 * Returns true if the MonoClass represents an enumeration.
2997 mono_class_is_enum (MonoClass *klass)
2999 return klass->enumtype;
3003 * mono_class_enum_basetype:
3004 * @klass: the MonoClass to act on
3006 * Returns the underlying type representation for an enumeration.
3009 mono_class_enum_basetype (MonoClass *klass)
3011 return klass->enum_basetype;
3015 * mono_class_get_parent
3016 * @klass: the MonoClass to act on
3018 * Returns the parent class for this class.
3021 mono_class_get_parent (MonoClass *klass)
3023 return klass->parent;
3027 * mono_class_get_nesting_type;
3028 * @klass: the MonoClass to act on
3030 * Returns the container type where this type is nested or NULL if this type is not a nested type.
3033 mono_class_get_nesting_type (MonoClass *klass)
3035 return klass->nested_in;
3039 * mono_class_get_rank:
3040 * @klass: the MonoClass to act on
3042 * Returns the rank for the array (the number of dimensions).
3045 mono_class_get_rank (MonoClass *klass)
3051 * mono_class_get_flags:
3052 * @klass: the MonoClass to act on
3054 * The type flags from the TypeDef table from the metadata.
3055 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
3058 * Returns the flags from the TypeDef table.
3061 mono_class_get_flags (MonoClass *klass)
3063 return klass->flags;
3067 * mono_class_get_name
3068 * @klass: the MonoClass to act on
3070 * Returns the name of the class.
3073 mono_class_get_name (MonoClass *klass)
3079 * mono_class_get_namespace:
3080 * @klass: the MonoClass to act on
3082 * Returns the namespace of the class.
3085 mono_class_get_namespace (MonoClass *klass)
3087 return klass->name_space;
3091 * mono_class_get_type:
3092 * @klass: the MonoClass to act on
3094 * This method returns the internal Type representation for the class.
3096 * Returns the MonoType from the class.
3099 mono_class_get_type (MonoClass *klass)
3101 return &klass->byval_arg;
3105 * mono_class_get_byref_type:
3106 * @klass: the MonoClass to act on
3111 mono_class_get_byref_type (MonoClass *klass)
3113 return &klass->this_arg;
3117 * mono_class_num_fields:
3118 * @klass: the MonoClass to act on
3120 * Returns the number of static and instance fields in the class.
3123 mono_class_num_fields (MonoClass *klass)
3125 return klass->field.count;
3129 * mono_class_num_methods:
3130 * @klass: the MonoClass to act on
3132 * Returns the number of methods in the class.
3135 mono_class_num_methods (MonoClass *klass)
3137 return klass->method.count;
3141 * mono_class_num_properties
3142 * @klass: the MonoClass to act on
3144 * Returns the number of properties in the class.
3147 mono_class_num_properties (MonoClass *klass)
3149 return klass->property.count;
3153 * mono_class_num_events:
3154 * @klass: the MonoClass to act on
3156 * Returns the number of events in the class.
3159 mono_class_num_events (MonoClass *klass)
3161 return klass->event.count;
3165 * mono_class_get_fields:
3166 * @klass: the MonoClass to act on
3168 * This routine is an iterator routine for retrieving the fields in a class.
3170 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3171 * iterate over all of the elements. When no more values are
3172 * available, the return value is NULL.
3174 * Returns a `MonoClassField *' on each iteration, or NULL when no more fields are available.
3177 mono_class_get_fields (MonoClass* klass, gpointer *iter)
3179 MonoClassField* field;
3183 mono_class_init (klass);
3185 /* start from the first */
3186 if (klass->field.count) {
3187 return *iter = &klass->fields [0];
3195 if (field < &klass->fields [klass->field.count]) {
3196 return *iter = field;
3202 * mono_class_get_methods
3203 * @klass: the MonoClass to act on
3205 * This routine is an iterator routine for retrieving the fields in a class.
3207 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3208 * iterate over all of the elements. When no more values are
3209 * available, the return value is NULL.
3211 * Returns a MonoMethod on each iteration or NULL when no more methods are available.
3214 mono_class_get_methods (MonoClass* klass, gpointer *iter)
3216 MonoMethod** method;
3220 mono_class_init (klass);
3222 /* start from the first */
3223 if (klass->method.count) {
3224 *iter = &klass->methods [0];
3225 return klass->methods [0];
3233 if (method < &klass->methods [klass->method.count]) {
3241 * mono_class_get_properties:
3242 * @klass: the MonoClass to act on
3244 * This routine is an iterator routine for retrieving the properties in a class.
3246 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3247 * iterate over all of the elements. When no more values are
3248 * available, the return value is NULL.
3250 * Returns a `MonoProperty *' on each invocation, or NULL when no more are available.
3253 mono_class_get_properties (MonoClass* klass, gpointer *iter)
3255 MonoProperty* property;
3259 mono_class_init (klass);
3261 /* start from the first */
3262 if (klass->property.count) {
3263 return *iter = &klass->properties [0];
3271 if (property < &klass->properties [klass->property.count]) {
3272 return *iter = property;
3278 * mono_class_get_events:
3279 * @klass: the MonoClass to act on
3281 * This routine is an iterator routine for retrieving the properties in a class.
3283 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3284 * iterate over all of the elements. When no more values are
3285 * available, the return value is NULL.
3287 * Returns a `MonoEvent *' on each invocation, or NULL when no more are available.
3290 mono_class_get_events (MonoClass* klass, gpointer *iter)
3296 mono_class_init (klass);
3298 /* start from the first */
3299 if (klass->event.count) {
3300 return *iter = &klass->events [0];
3308 if (event < &klass->events [klass->event.count]) {
3309 return *iter = event;
3315 * mono_class_get_interfaaces
3316 * @klass: the MonoClass to act on
3318 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
3320 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3321 * iterate over all of the elements. When no more values are
3322 * available, the return value is NULL.
3324 * Returns a `Monoclass *' on each invocation, or NULL when no more are available.
3327 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
3333 mono_class_init (klass);
3335 /* start from the first */
3336 if (klass->interface_count) {
3337 *iter = &klass->interfaces [0];
3338 return klass->interfaces [0];
3346 if (iface < &klass->interfaces [klass->interface_count]) {
3354 * mono_class_get_interfaaces
3355 * @klass: the MonoClass to act on
3357 * This routine is an iterator routine for retrieving the nested types of a class.
3359 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3360 * iterate over all of the elements. When no more values are
3361 * available, the return value is NULL.
3363 * Returns a `Monoclass *' on each invocation, or NULL when no more are available.
3366 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
3372 mono_class_init (klass);
3374 /* start from the first */
3375 if (klass->nested_classes) {
3376 *iter = klass->nested_classes;
3377 return klass->nested_classes->data;
3379 /* no nested types */
3393 * mono_field_get_name:
3394 * @field: the MonoClassField to act on
3396 * Returns the name of the field.
3399 mono_field_get_name (MonoClassField *field)
3405 * mono_field_get_type:
3406 * @field: the MonoClassField to act on
3408 * Returns MonoType of the field.
3411 mono_field_get_type (MonoClassField *field)
3417 * mono_field_get_type:
3418 * @field: the MonoClassField to act on
3420 * Returns MonoClass where the field was defined.
3423 mono_field_get_parent (MonoClassField *field)
3425 return field->parent;
3429 * mono_field_get_flags;
3430 * @field: the MonoClassField to act on
3432 * The metadata flags for a field are encoded using the
3433 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
3435 * Returns the flags for the field.
3438 mono_field_get_flags (MonoClassField *field)
3440 return field->type->attrs;
3444 * mono_property_get_name:
3445 * @prop: the MonoProperty to act on
3447 * Returns the name of the property
3450 mono_property_get_name (MonoProperty *prop)
3456 * mono_property_get_set_method
3457 * @prop: the MonoProperty to act on.
3459 * Returns the setter method of the property (A MonoMethod)
3462 mono_property_get_set_method (MonoProperty *prop)
3468 * mono_property_get_get_method
3469 * @prop: the MonoProperty to act on.
3471 * Returns the setter method of the property (A MonoMethod)
3474 mono_property_get_get_method (MonoProperty *prop)
3480 * mono_property_get_parent:
3481 * @prop: the MonoProperty to act on.
3483 * Returns the MonoClass where the property was defined.
3486 mono_property_get_parent (MonoProperty *prop)
3488 return prop->parent;
3492 * mono_property_get_flags:
3493 * @prop: the MonoProperty to act on.
3495 * The metadata flags for a property are encoded using the
3496 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
3498 * Returns the flags for the property.
3501 mono_property_get_flags (MonoProperty *prop)
3507 * mono_event_get_name:
3508 * @event: the MonoEvent to act on
3510 * Returns the name of the event.
3513 mono_event_get_name (MonoEvent *event)
3519 * mono_event_get_add_method:
3520 * @event: The MonoEvent to act on.
3522 * Returns the `add' method for the event (a MonoMethod).
3525 mono_event_get_add_method (MonoEvent *event)
3531 * mono_event_get_remove_method:
3532 * @event: The MonoEvent to act on.
3534 * Returns the `remove' method for the event (a MonoMethod).
3537 mono_event_get_remove_method (MonoEvent *event)
3539 return event->remove;
3543 * mono_event_get_raise_method:
3544 * @event: The MonoEvent to act on.
3546 * Returns the `raise' method for the event (a MonoMethod).
3549 mono_event_get_raise_method (MonoEvent *event)
3551 return event->raise;
3555 * mono_event_get_parent:
3556 * @event: the MonoEvent to act on.
3558 * Returns the MonoClass where the event is defined.
3561 mono_event_get_parent (MonoEvent *event)
3563 return event->parent;
3567 * mono_event_get_flags
3568 * @event: the MonoEvent to act on.
3570 * The metadata flags for an event are encoded using the
3571 * EVENT_* constants. See the tabledefs.h file for details.
3573 * Returns the flags for the event.
3576 mono_event_get_flags (MonoEvent *event)
3578 return event->attrs;