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/metadata/mono-debug-debugger.h>
36 #include <mono/os/gc_wrapper.h>
40 gboolean mono_print_vtable = FALSE;
42 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
44 void (*mono_debugger_start_class_init_func) (MonoClass *klass) = NULL;
45 void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
48 mono_class_from_typeref (MonoImage *image, guint32 type_token)
50 guint32 cols [MONO_TYPEREF_SIZE];
51 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
53 const char *name, *nspace;
55 MonoAssembly **references;
57 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
59 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
60 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
62 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
63 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
64 case MONO_RESOLTION_SCOPE_MODULE:
66 g_error ("null ResolutionScope not yet handled");
67 /* a typedef in disguise */
68 return mono_class_from_name (image, nspace, name);
69 case MONO_RESOLTION_SCOPE_MODULEREF:
70 return mono_class_from_name (image->modules [idx - 1], nspace, name);
71 case MONO_RESOLTION_SCOPE_TYPEREF: {
72 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
74 mono_class_init (enclosing);
75 for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
77 if (strcmp (res->name, name) == 0)
80 g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
83 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
87 references = image->references;
88 if (!references [idx-1])
89 mono_assembly_load_reference (image, idx - 1);
90 if (references [idx - 1] == (gpointer)-1)
93 image = references [idx-1]->image;
95 return mono_class_from_name (image, nspace, name);
99 dup_type (MonoType* t, const MonoType *original)
101 MonoType *r = g_new0 (MonoType, 1);
103 r->attrs = original->attrs;
104 r->byref = original->byref;
105 mono_stats.generics_metadata_size += sizeof (MonoType);
110 mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed,
111 gboolean include_ns, gboolean include_arity)
115 switch (type->type) {
116 case MONO_TYPE_ARRAY: {
117 int i, rank = type->data.array->rank;
119 mono_type_get_name_recurse (
120 &type->data.array->eklass->byval_arg, str,
121 FALSE, include_ns, include_arity);
122 g_string_append_c (str, '[');
123 for (i = 1; i < rank; i++)
124 g_string_append_c (str, ',');
125 g_string_append_c (str, ']');
128 case MONO_TYPE_SZARRAY:
129 mono_type_get_name_recurse (
130 &type->data.klass->byval_arg, str, FALSE, include_ns, include_arity);
131 g_string_append (str, "[]");
134 mono_type_get_name_recurse (type->data.type, str, FALSE, include_ns, include_arity);
135 g_string_append_c (str, '*');
138 klass = mono_class_from_mono_type (type);
139 if (klass->nested_in) {
140 mono_type_get_name_recurse (
141 &klass->nested_in->byval_arg, str, TRUE, include_ns, include_arity);
142 g_string_append_c (str, '+');
144 if (include_ns && *klass->name_space) {
145 g_string_append (str, klass->name_space);
146 g_string_append_c (str, '.');
148 if (!include_arity) {
149 char *s = strchr (klass->name, '`');
150 int len = s ? s - klass->name : strlen (klass->name);
152 g_string_append_len (str, klass->name, len);
154 g_string_append (str, klass->name);
157 if (klass->generic_class) {
158 MonoGenericClass *gclass = klass->generic_class;
161 g_string_append_c (str, '[');
162 for (i = 0; i < gclass->inst->type_argc; i++) {
164 g_string_append_c (str, ',');
165 mono_type_get_name_recurse (
166 gclass->inst->type_argv [i], str, FALSE, include_ns, include_arity);
168 g_string_append_c (str, ']');
169 } else if (klass->generic_container) {
172 g_string_append_c (str, '[');
173 for (i = 0; i < klass->generic_container->type_argc; i++) {
175 g_string_append_c (str, ',');
176 g_string_append (str, klass->generic_container->type_params [i].name);
178 g_string_append_c (str, ']');
185 * mono_type_get_name:
188 * Returns: the string representation for type as required by System.Reflection.
189 * The inverse of mono_reflection_parse_type ().
192 _mono_type_get_name (MonoType *type, gboolean is_recursed, gboolean include_ns,
193 gboolean include_arity)
195 GString* result = g_string_new ("");
196 mono_type_get_name_recurse (type, result, is_recursed, include_ns, include_arity);
199 g_string_append_c (result, '&');
201 return g_string_free (result, FALSE);
205 mono_type_get_name (MonoType *type)
207 return _mono_type_get_name (type, TRUE, TRUE, TRUE);
211 mono_type_get_full_name (MonoType *type)
213 return _mono_type_get_name (type, FALSE, TRUE, TRUE);
217 mono_type_get_underlying_type (MonoType *type)
219 switch (type->type) {
220 case MONO_TYPE_VALUETYPE:
221 if (type->data.klass->enumtype)
222 return type->data.klass->enum_basetype;
224 case MONO_TYPE_GENERICINST:
225 return mono_type_get_underlying_type (&type->data.generic_class->container_class->byval_arg);
234 mono_class_is_open_constructed_type (MonoType *t)
240 case MONO_TYPE_SZARRAY:
241 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
242 case MONO_TYPE_ARRAY:
243 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
245 return mono_class_is_open_constructed_type (t->data.type);
246 case MONO_TYPE_GENERICINST: {
247 MonoGenericClass *gclass = t->data.generic_class;
250 if (mono_class_is_open_constructed_type (&gclass->container_class->byval_arg))
252 for (i = 0; i < gclass->inst->type_argc; i++)
253 if (mono_class_is_open_constructed_type (gclass->inst->type_argv [i]))
262 static MonoGenericClass *
263 inflate_generic_class (MonoGenericClass *ogclass, MonoGenericContext *context)
265 MonoGenericClass *ngclass, *cached;
267 if (ogclass->is_dynamic) {
268 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
269 ngclass = &dgclass->generic_class;
271 ngclass = g_new0 (MonoGenericClass, 1);
275 ngclass->inst = mono_metadata_inflate_generic_inst (ogclass->inst, context);
277 ngclass->klass = NULL;
279 ngclass->context = g_new0 (MonoGenericContext, 1);
280 ngclass->context->container = context->container;
281 ngclass->context->gclass = ngclass;
283 ngclass->initialized = FALSE;
286 cached = mono_metadata_lookup_generic_class (ngclass);
287 mono_loader_unlock ();
293 mono_class_create_generic (ngclass);
294 mono_class_create_generic_2 (ngclass);
300 inflate_generic_type (MonoType *type, MonoGenericContext *context)
302 switch (type->type) {
304 if (context->gmethod && context->gmethod->inst->type_argv)
306 context->gmethod->inst->type_argv [type->data.generic_param->num],
313 context->gclass->inst->type_argv [type->data.generic_param->num],
317 case MONO_TYPE_SZARRAY: {
318 MonoClass *eclass = type->data.klass;
319 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
322 nt = dup_type (type, type);
323 nt->data.klass = mono_class_from_mono_type (inflated);
326 case MONO_TYPE_GENERICINST: {
327 MonoGenericClass *ngclass = inflate_generic_class (type->data.generic_class, context);
328 MonoType *nt = dup_type (type, type);
329 nt->data.generic_class = ngclass;
332 case MONO_TYPE_CLASS:
333 case MONO_TYPE_VALUETYPE: {
334 MonoClass *klass = type->data.klass;
335 MonoGenericClass *gclass;
338 if (!klass->generic_container)
341 gclass = inflate_generic_class (klass->generic_container->context.gclass, context);
343 nt = dup_type (type, type);
344 nt->type = MONO_TYPE_GENERICINST;
345 nt->data.generic_class = gclass;
355 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
357 MonoType *inflated = inflate_generic_type (type, context);
360 return dup_type (type, type);
362 mono_stats.inflated_type_count++;
367 mono_class_inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
369 MonoMethodSignature *res;
376 res = mono_metadata_signature_alloc (image, sig->param_count);
377 res->ret = mono_class_inflate_generic_type (sig->ret, context);
378 is_open = mono_class_is_open_constructed_type (res->ret);
379 for (i = 0; i < sig->param_count; ++i) {
380 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
382 is_open = mono_class_is_open_constructed_type (res->params [i]);
384 res->hasthis = sig->hasthis;
385 res->explicit_this = sig->explicit_this;
386 res->call_convention = sig->call_convention;
387 res->generic_param_count = sig->generic_param_count;
388 res->sentinelpos = sig->sentinelpos;
389 res->has_type_parameters = is_open;
390 res->is_inflated = 1;
394 static MonoMethodHeader*
395 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
397 MonoMethodHeader *res;
399 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
400 res->code = header->code;
401 res->code_size = header->code_size;
402 res->max_stack = header->max_stack;
403 res->num_clauses = header->num_clauses;
404 res->init_locals = header->init_locals;
405 res->num_locals = header->num_locals;
406 res->clauses = header->clauses;
407 for (i = 0; i < header->num_locals; ++i)
408 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
412 static MonoGenericContext *
413 inflate_generic_context (MonoGenericContext *context, MonoGenericContext *inflate_with)
415 MonoGenericContext *res = g_new0 (MonoGenericContext, 1);
417 res->container = context->container;
420 res->gclass = inflate_generic_class (context->gclass, inflate_with);
422 if (context->gmethod) {
423 res->gmethod = g_new0 (MonoGenericMethod, 1);
425 res->gmethod->container = context->gmethod->container;
426 res->gmethod->inst = mono_metadata_inflate_generic_inst (context->gmethod->inst, inflate_with);
428 res->gmethod = inflate_with->gmethod;
434 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context,
437 MonoMethodInflated *result;
440 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
441 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
444 if (method->signature->is_inflated) {
445 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
447 context = inflate_generic_context (imethod->context, context);
448 method = imethod->declaring;
451 mono_stats.inflated_method_count++;
452 mono_stats.generics_metadata_size +=
453 sizeof (MonoMethodInflated) - sizeof (MonoMethodNormal);
455 result = g_new0 (MonoMethodInflated, 1);
456 result->nmethod = *(MonoMethodNormal*)method;
458 if (mono_method_get_header (method))
459 result->nmethod.header = inflate_generic_header (
460 mono_method_get_header (method), context);
463 rklass = result->nmethod.method.klass = klass;
465 MonoType *declaring = mono_class_inflate_generic_type (&method->klass->byval_arg, context);
466 rklass = result->nmethod.method.klass = mono_class_from_mono_type (declaring);
469 result->nmethod.method.signature = mono_class_inflate_generic_signature (
470 method->klass->image, method->signature, context);
472 result->context = context;
473 result->declaring = method;
475 return (MonoMethod *) result;
479 * class_compute_field_layout:
480 * @m: pointer to the metadata.
481 * @class: The class to initialize
483 * Initializes the class->fields.
485 * Currently we only support AUTO_LAYOUT, and do not even try to do
486 * a good job at it. This is temporary to get the code for Paolo.
489 class_compute_field_layout (MonoClass *class)
491 MonoImage *m = class->image;
492 const int top = class->field.count;
493 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
494 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
495 int i, blittable = TRUE, real_size = 0;
497 guint32 packing_size = 0;
498 gboolean explicit_size;
499 MonoClassField *field;
501 if (class->size_inited)
505 if (!class->parent->size_inited)
506 class_compute_field_layout (class->parent);
507 class->instance_size += class->parent->instance_size;
508 class->min_align = class->parent->min_align;
509 blittable = class->parent->blittable;
511 class->instance_size = sizeof (MonoObject);
512 class->min_align = 1;
515 /* Get the real size */
516 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
519 g_assert ((packing_size & 0xfffffff0) == 0);
520 class->packing_size = packing_size;
521 real_size += class->instance_size;
525 if (explicit_size && real_size) {
526 class->instance_size = MAX (real_size, class->instance_size);
528 class->size_inited = 1;
529 class->blittable = blittable;
533 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT)
536 class->fields = g_new0 (MonoClassField, top);
539 * Fetch all the field information.
541 for (i = 0; i < top; i++){
543 guint32 cols [MONO_FIELD_SIZE];
544 int idx = class->field.first + i;
545 MonoGenericContainer *container = NULL;
547 field = &class->fields [i];
548 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
549 /* The name is needed for fieldrefs */
550 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
551 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
552 mono_metadata_decode_value (sig, &sig);
553 /* FIELD signature == 0x06 */
554 g_assert (*sig == 0x06);
555 if (class->generic_container)
556 container = class->generic_container;
557 else if (class->generic_class) {
558 container = class->generic_class->container_class->generic_container;
559 g_assert (container);
561 field->type = mono_metadata_parse_type_full (
562 m, (MonoGenericContext *) container, MONO_PARSE_FIELD,
563 cols [MONO_FIELD_FLAGS], sig + 1, &sig);
564 if (mono_field_is_deleted (field))
566 if (class->generic_class) {
567 field->type = mono_class_inflate_generic_type (
568 field->type, class->generic_class->context);
569 field->type->attrs = cols [MONO_FIELD_FLAGS];
572 field->parent = class;
574 /* Only do these checks if we still think this type is blittable */
575 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
576 if (field->type->byref || MONO_TYPE_IS_REFERENCE (field->type)) {
579 MonoClass *field_class = mono_class_from_mono_type (field->type);
580 if (!field_class || !field_class->blittable)
585 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
586 mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
587 if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
588 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, field->name);
591 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
592 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
594 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
595 field->data = mono_image_rva_map (class->image, rva);
598 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
599 class->enum_basetype = field->type;
600 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
601 blittable = class->element_class->blittable;
604 /* The def_value of fields is compute lazily during vtable creation */
607 if (class == mono_defaults.string_class)
610 class->blittable = blittable;
612 if (class->enumtype && !class->enum_basetype) {
613 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
616 if (explicit_size && real_size) {
617 class->instance_size = MAX (real_size, class->instance_size);
620 if (class->generic_container ||
621 (class->generic_class && class->generic_class->inst->is_open))
624 mono_class_layout_fields (class);
628 mono_class_layout_fields (MonoClass *class)
631 const int top = class->field.count;
632 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
633 guint32 pass, passes, real_size;
634 gboolean gc_aware_layout = FALSE;
635 MonoClassField *field;
638 * Enable GC aware auto layout: in this mode, reference
639 * fields are grouped together inside objects, increasing collector
641 * Requires that all classes whose layout is known to native code be annotated
642 * with [StructLayout (LayoutKind.Sequential)]
643 * Value types have gc_aware_layout disabled by default, as per
644 * what the default is for other runtimes.
646 /* corlib is missing [StructLayout] directives in many places */
647 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
648 if (class->image != mono_defaults.corlib &&
649 class->byval_arg.type != MONO_TYPE_VALUETYPE)
650 gc_aware_layout = TRUE;
654 * Compute field layout and total size (not considering static fields)
658 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
659 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
666 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
670 real_size = class->parent->instance_size;
672 real_size = sizeof (MonoObject);
674 for (pass = 0; pass < passes; ++pass) {
675 for (i = 0; i < top; i++){
677 field = &class->fields [i];
679 if (mono_field_is_deleted (field))
681 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
684 if (gc_aware_layout) {
686 * We process fields with reference type in the first pass,
687 * and fields with non-reference type in the second pass.
688 * We use IS_POINTER instead of IS_REFERENCE because in
689 * some internal structures, we store GC_MALLOCed memory
690 * in IntPtr fields...
692 if (MONO_TYPE_IS_POINTER (field->type)) {
701 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
702 (strcmp (field->name, "$PRIVATE$") == 0)) {
703 /* This field is a hack inserted by MCS to empty structures */
707 size = mono_type_size (field->type, &align);
709 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
710 align = class->packing_size ? MIN (class->packing_size, align): align;
711 class->min_align = MAX (align, class->min_align);
712 field->offset = real_size;
713 field->offset += align - 1;
714 field->offset &= ~(align - 1);
715 real_size = field->offset + size;
718 class->instance_size = MAX (real_size, class->instance_size);
720 if (class->instance_size & (class->min_align - 1)) {
721 class->instance_size += class->min_align - 1;
722 class->instance_size &= ~(class->min_align - 1);
726 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
728 for (i = 0; i < top; i++) {
730 field = &class->fields [i];
733 * There must be info about all the fields in a type if it
734 * uses explicit layout.
737 if (mono_field_is_deleted (field))
739 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
742 size = mono_type_size (field->type, &align);
745 * When we get here, field->offset is already set by the
746 * loader (for either runtime fields or fields loaded from metadata).
747 * The offset is from the start of the object: this works for both
748 * classes and valuetypes.
750 field->offset += sizeof (MonoObject);
755 real_size = MAX (real_size, size + field->offset);
757 class->instance_size = MAX (real_size, class->instance_size);
761 class->size_inited = 1;
764 * Compute static field layout and size
766 for (i = 0; i < top; i++){
768 field = &class->fields [i];
770 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
772 if (mono_field_is_deleted (field))
775 size = mono_type_size (field->type, &align);
776 field->offset = class->class_size;
777 field->offset += align - 1;
778 field->offset &= ~(align - 1);
779 class->class_size = field->offset + size;
784 init_properties (MonoClass *class)
786 guint startm, endm, i, j;
787 guint32 cols [MONO_PROPERTY_SIZE];
788 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
789 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
791 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
792 class->property.count = class->property.last - class->property.first;
794 class->properties = g_new0 (MonoProperty, class->property.count);
795 for (i = class->property.first; i < class->property.last; ++i) {
796 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
797 class->properties [i - class->property.first].parent = class;
798 class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
799 class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
801 startm = mono_metadata_methods_from_property (class->image, i, &endm);
802 for (j = startm; j < endm; ++j) {
803 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
804 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
805 case METHOD_SEMANTIC_SETTER:
806 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
808 case METHOD_SEMANTIC_GETTER:
809 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
819 init_events (MonoClass *class)
821 guint startm, endm, i, j;
822 guint32 cols [MONO_EVENT_SIZE];
823 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
824 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
826 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
827 class->event.count = class->event.last - class->event.first;
829 class->events = g_new0 (MonoEvent, class->event.count);
830 for (i = class->event.first; i < class->event.last; ++i) {
831 MonoEvent *event = &class->events [i - class->event.first];
833 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
834 event->parent = class;
835 event->attrs = cols [MONO_EVENT_FLAGS];
836 event->name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
838 startm = mono_metadata_methods_from_event (class->image, i, &endm);
839 for (j = startm; j < endm; ++j) {
840 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
841 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
842 case METHOD_SEMANTIC_ADD_ON:
843 event->add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
845 case METHOD_SEMANTIC_REMOVE_ON:
846 event->remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
848 case METHOD_SEMANTIC_FIRE:
849 event->raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
851 case METHOD_SEMANTIC_OTHER: {
854 if (event->other == NULL) {
855 event->other = g_new0 (MonoMethod*, 1);
857 while (event->other [n])
859 event->other = g_realloc (event->other, (n + 1) * sizeof (MonoMethod*));
861 event->other [n] = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
872 mono_get_unique_iid (MonoClass *class)
874 static GHashTable *iid_hash = NULL;
875 static guint iid = 0;
880 g_assert (MONO_CLASS_IS_INTERFACE (class));
885 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
887 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
889 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
890 mono_loader_unlock ();
894 g_hash_table_insert (iid_hash, str, (gpointer)iid);
898 mono_loader_unlock ();
904 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
909 for (i = 0; i < klass->interface_count; i++) {
910 ic = klass->interfaces [i];
913 *res = g_ptr_array_new ();
914 g_ptr_array_add (*res, ic);
916 collect_implemented_interfaces_aux (ic, res);
921 mono_class_get_implemented_interfaces (MonoClass *klass)
923 GPtrArray *res = NULL;
925 collect_implemented_interfaces_aux (klass, &res);
930 setup_interface_offsets (MonoClass *class, int cur_slot)
936 /* compute maximum number of slots and maximum interface id */
938 for (k = class; k ; k = k->parent) {
939 for (i = 0; i < k->interface_count; i++) {
940 ic = k->interfaces [i];
943 mono_class_init (ic);
945 if (max_iid < ic->interface_id)
946 max_iid = ic->interface_id;
950 if (MONO_CLASS_IS_INTERFACE (class)) {
951 if (max_iid < class->interface_id)
952 max_iid = class->interface_id;
954 class->max_interface_id = max_iid;
955 /* compute vtable offset for interfaces */
956 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
958 for (i = 0; i <= max_iid; i++)
959 class->interface_offsets [i] = -1;
961 ifaces = mono_class_get_implemented_interfaces (class);
963 for (i = 0; i < ifaces->len; ++i) {
964 ic = g_ptr_array_index (ifaces, i);
965 class->interface_offsets [ic->interface_id] = cur_slot;
966 cur_slot += ic->method.count;
968 g_ptr_array_free (ifaces, TRUE);
971 for (k = class->parent; k ; k = k->parent) {
972 ifaces = mono_class_get_implemented_interfaces (k);
974 for (i = 0; i < ifaces->len; ++i) {
975 ic = g_ptr_array_index (ifaces, i);
977 if (class->interface_offsets [ic->interface_id] == -1) {
978 int io = k->interface_offsets [ic->interface_id];
982 class->interface_offsets [ic->interface_id] = io;
985 g_ptr_array_free (ifaces, TRUE);
989 if (MONO_CLASS_IS_INTERFACE (class))
990 class->interface_offsets [class->interface_id] = cur_slot;
996 mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
1000 int i, max_vtsize = 0, max_iid, cur_slot = 0;
1002 GHashTable *override_map = NULL;
1004 /* setup_vtable() must be called only once on the type */
1005 if (class->interface_offsets) {
1006 g_warning ("vtable already computed in %s.%s", class->name_space, class->name);
1010 ifaces = mono_class_get_implemented_interfaces (class);
1012 for (i = 0; i < ifaces->len; i++) {
1013 MonoClass *ic = g_ptr_array_index (ifaces, i);
1014 max_vtsize += ic->method.count;
1016 g_ptr_array_free (ifaces, TRUE);
1019 if (class->parent) {
1020 max_vtsize += class->parent->vtable_size;
1021 cur_slot = class->parent->vtable_size;
1024 max_vtsize += class->method.count;
1026 vtable = alloca (sizeof (gpointer) * max_vtsize);
1027 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
1029 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
1031 cur_slot = setup_interface_offsets (class, cur_slot);
1032 max_iid = class->max_interface_id;
1034 if (class->parent && class->parent->vtable_size)
1035 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
1037 /* override interface methods */
1038 for (i = 0; i < onum; i++) {
1039 MonoMethod *decl = overrides [i*2];
1040 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
1042 g_assert (decl->slot != -1);
1043 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
1044 vtable [dslot] = overrides [i*2 + 1];
1045 vtable [dslot]->slot = dslot;
1047 override_map = g_hash_table_new (NULL, NULL);
1049 g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
1053 for (k = class; k ; k = k->parent) {
1055 ifaces = mono_class_get_implemented_interfaces (k);
1057 nifaces = ifaces->len;
1058 for (i = 0; i < nifaces; i++) {
1061 ic = g_ptr_array_index (ifaces, i);
1062 io = k->interface_offsets [ic->interface_id];
1065 g_assert (io <= max_vtsize);
1068 for (l = 0; l < ic->method.count; l++) {
1069 MonoMethod *im = ic->methods [l];
1071 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1074 for (j = 0; j < class->method.count; ++j) {
1075 MonoMethod *cm = class->methods [j];
1076 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1077 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
1078 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
1080 if (!strcmp(cm->name, im->name) &&
1081 mono_metadata_signature_equal (cm->signature, im->signature)) {
1082 g_assert (io + l <= max_vtsize);
1083 vtable [io + l] = cm;
1088 /* already implemented */
1089 if (io >= k->vtable_size)
1093 for (l = 0; l < ic->method.count; l++) {
1094 MonoMethod *im = ic->methods [l];
1097 g_assert (io + l <= max_vtsize);
1099 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1102 for (k1 = class; k1; k1 = k1->parent) {
1103 for (j = 0; j < k1->method.count; ++j) {
1104 MonoMethod *cm = k1->methods [j];
1106 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1107 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
1110 if (!strcmp(cm->name, im->name) &&
1111 mono_metadata_signature_equal (cm->signature, im->signature)) {
1112 g_assert (io + l <= max_vtsize);
1113 vtable [io + l] = cm;
1118 g_assert (io + l <= max_vtsize);
1119 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1124 for (l = 0; l < ic->method.count; l++) {
1125 MonoMethod *im = ic->methods [l];
1126 char *qname, *fqname, *cname, *the_cname;
1129 if (vtable [io + l])
1132 if (ic->generic_class) {
1133 MonoClass *the_ic = ic->generic_class->container_class;
1134 the_cname = _mono_type_get_name (&the_ic->byval_arg, TRUE, FALSE, TRUE);
1138 cname = (char*)ic->name;
1141 qname = g_strconcat (cname, ".", im->name, NULL);
1142 if (ic->name_space && ic->name_space [0])
1143 fqname = g_strconcat (ic->name_space, ".", cname, ".", im->name, NULL);
1147 for (k1 = class; k1; k1 = k1->parent) {
1148 for (j = 0; j < k1->method.count; ++j) {
1149 MonoMethod *cm = k1->methods [j];
1151 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1154 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
1155 mono_metadata_signature_equal (cm->signature, im->signature)) {
1156 g_assert (io + l <= max_vtsize);
1157 vtable [io + l] = cm;
1168 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
1169 for (l = 0; l < ic->method.count; l++) {
1171 MonoMethod *im = ic->methods [l];
1172 if (im->flags & METHOD_ATTRIBUTE_STATIC)
1174 g_assert (io + l <= max_vtsize);
1177 * If one of our parents already implements this interface
1178 * we can inherit the implementation.
1180 if (!(vtable [io + l])) {
1181 MonoClass *parent = class->parent;
1183 if ((ic->interface_id <= parent->max_interface_id) &&
1184 (parent->interface_offsets [ic->interface_id]) &&
1186 vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
1189 if (!(vtable [io + l])) {
1190 for (j = 0; j < onum; ++j) {
1191 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
1192 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
1194 msig = mono_signature_get_desc (im->signature, FALSE);
1195 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
1196 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
1198 for (j = 0; j < class->method.count; ++j) {
1199 MonoMethod *cm = class->methods [j];
1200 msig = mono_signature_get_desc (cm->signature, FALSE);
1202 printf ("METHOD %s(%s)\n", cm->name, msig);
1205 g_assert_not_reached ();
1210 for (l = 0; l < ic->method.count; l++) {
1211 MonoMethod *im = vtable [io + l];
1214 g_assert (io + l <= max_vtsize);
1216 /* FIXME: why do we need this ? */
1218 /* g_assert_not_reached (); */
1224 g_ptr_array_free (ifaces, TRUE);
1227 for (i = 0; i < class->method.count; ++i) {
1230 cm = class->methods [i];
1233 * Non-virtual method have no place in the vtable.
1234 * This also catches static methods (since they are not virtual).
1236 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1240 * If the method is REUSE_SLOT, we must check in the
1241 * base class for a method to override.
1243 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
1245 for (k = class->parent; k ; k = k->parent) {
1247 for (j = 0; j < k->method.count; ++j) {
1248 MonoMethod *m1 = k->methods [j];
1249 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
1251 if (!strcmp(cm->name, m1->name) &&
1252 mono_metadata_signature_equal (cm->signature, m1->signature)) {
1253 slot = k->methods [j]->slot;
1254 g_assert (cm->slot < max_vtsize);
1256 override_map = g_hash_table_new (NULL, NULL);
1257 g_hash_table_insert (override_map, m1, cm);
1269 cm->slot = cur_slot++;
1271 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT) && !cm->signature->generic_param_count)
1272 vtable [cm->slot] = cm;
1275 /* override non interface methods */
1276 for (i = 0; i < onum; i++) {
1277 MonoMethod *decl = overrides [i*2];
1278 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
1279 g_assert (decl->slot != -1);
1280 vtable [decl->slot] = overrides [i*2 + 1];
1281 overrides [i * 2 + 1]->slot = decl->slot;
1283 override_map = g_hash_table_new (NULL, NULL);
1284 g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
1289 * If a method occupies more than one place in the vtable, and it is
1290 * overriden, then change the other occurances too.
1293 for (i = 0; i < max_vtsize; ++i)
1295 MonoMethod *cm = g_hash_table_lookup (override_map, vtable [i]);
1300 g_hash_table_destroy (override_map);
1303 if (class->generic_class) {
1304 MonoClass *gklass = class->generic_class->container_class;
1306 mono_class_init (gklass);
1307 class->vtable_size = gklass->vtable_size;
1309 class->vtable_size = cur_slot;
1311 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
1312 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
1314 if (mono_print_vtable) {
1317 for (i = 0; i <= max_iid; i++)
1318 if (class->interface_offsets [i] != -1)
1321 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
1322 class->name, class->vtable_size, icount);
1324 for (i = 0; i < class->vtable_size; ++i) {
1329 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
1330 cm->klass->name_space, cm->klass->name,
1337 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
1338 class->name, max_iid);
1340 for (i = 0; i < class->interface_count; i++) {
1341 ic = class->interfaces [i];
1342 printf (" slot %03d(%03d) %s.%s\n",
1343 class->interface_offsets [ic->interface_id],
1344 ic->method.count, ic->name_space, ic->name);
1347 for (k = class->parent; k ; k = k->parent) {
1348 for (i = 0; i < k->interface_count; i++) {
1349 ic = k->interfaces [i];
1350 printf (" slot %03d(%03d) %s.%s\n",
1351 class->interface_offsets [ic->interface_id],
1352 ic->method.count, ic->name_space, ic->name);
1361 * @class: the class to initialize
1363 * compute the instance_size, class_size and other infos that cannot be
1364 * computed at mono_class_get() time. Also compute a generic vtable and
1365 * the method slot numbers. We use this infos later to create a domain
1369 mono_class_init (MonoClass *class)
1372 static MonoMethod *default_ghc = NULL;
1373 static MonoMethod *default_finalize = NULL;
1374 static int finalize_slot = -1;
1375 static int ghc_slot = -1;
1376 MonoMethod **overrides;
1384 /*g_print ("Init class %s\n", class->name);*/
1386 /* We do everything inside the lock to prevent races */
1387 mono_loader_lock ();
1389 if (class->inited) {
1390 mono_loader_unlock ();
1391 /* Somebody might have gotten in before us */
1395 if (class->init_pending) {
1396 /* this indicates a cyclic dependency */
1397 g_error ("pending init %s.%s\n", class->name_space, class->name);
1400 class->init_pending = 1;
1402 if (mono_debugger_start_class_init_func)
1403 mono_debugger_start_class_init_func (class);
1405 mono_stats.initialized_class_count++;
1407 if (class->generic_class && !class->generic_class->is_dynamic) {
1408 MonoGenericClass *gclass = class->generic_class;
1409 MonoClass *gklass = gclass->container_class;
1411 mono_class_init (gklass);
1413 if (MONO_CLASS_IS_INTERFACE (class))
1414 class->interface_id = mono_get_unique_iid (class);
1416 g_assert (class->method.count == gklass->method.count);
1417 class->methods = g_new0 (MonoMethod *, class->method.count);
1419 for (i = 0; i < class->method.count; i++)
1420 class->methods [i] = mono_class_inflate_generic_method (
1421 gklass->methods [i], gclass->context, gclass->klass);
1423 g_assert (class->field.count == gklass->field.count);
1424 class->fields = g_new0 (MonoClassField, class->field.count);
1426 for (i = 0; i < class->field.count; i++) {
1427 MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
1428 ifield->generic_type = gklass->fields [i].type;
1430 class->fields [i] = gklass->fields [i];
1431 class->fields [i].generic_info = ifield;
1432 class->fields [i].parent = class;
1433 class->fields [i].type = mono_class_inflate_generic_type (
1434 class->fields [i].type, gclass->context);
1437 class->property = gklass->property;
1438 class->properties = g_new0 (MonoProperty, class->property.count);
1440 for (i = 0; i < class->property.count; i++) {
1441 MonoProperty *prop = &class->properties [i];
1443 *prop = gklass->properties [i];
1446 prop->get = mono_class_inflate_generic_method (
1447 prop->get, gclass->context, gclass->klass);
1449 prop->set = mono_class_inflate_generic_method (
1450 prop->set, gclass->context, gclass->klass);
1452 prop->parent = class;
1455 g_assert (class->interface_count == gklass->interface_count);
1458 if (class->parent && !class->parent->inited)
1459 mono_class_init (class->parent);
1462 * Computes the size used by the fields, and their locations
1464 if (!class->size_inited)
1465 class_compute_field_layout (class);
1467 /* initialize method pointers */
1470 MonoMethodSignature *sig;
1471 class->method.count = class->rank > 1? 2: 1;
1472 sig = mono_metadata_signature_alloc (class->image, class->rank);
1473 sig->ret = &mono_defaults.void_class->byval_arg;
1474 sig->pinvoke = TRUE;
1475 for (i = 0; i < class->rank; ++i)
1476 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1478 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1479 ctor->klass = class;
1480 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1481 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1482 ctor->signature = sig;
1483 ctor->name = ".ctor";
1485 class->methods = g_new (MonoMethod*, class->method.count);
1486 class->methods [0] = ctor;
1487 if (class->rank > 1) {
1488 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
1489 sig->ret = &mono_defaults.void_class->byval_arg;
1490 sig->pinvoke = TRUE;
1491 for (i = 0; i < class->rank * 2; ++i)
1492 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1494 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1495 ctor->klass = class;
1496 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1497 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1498 ctor->signature = sig;
1499 ctor->name = ".ctor";
1501 class->methods [1] = ctor;
1504 if (!class->generic_class && !class->methods) {
1505 class->methods = g_new (MonoMethod*, class->method.count);
1506 for (i = 0; i < class->method.count; ++i) {
1507 class->methods [i] = mono_get_method (class->image,
1508 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
1513 if (!class->generic_class) {
1514 init_properties (class);
1515 init_events (class);
1517 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
1520 guint32 cols [MONO_NESTED_CLASS_SIZE];
1521 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
1522 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
1523 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
1525 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
1529 mono_class_setup_supertypes (class);
1531 if (MONO_CLASS_IS_INTERFACE (class)) {
1532 for (i = 0; i < class->method.count; ++i)
1533 class->methods [i]->slot = i;
1534 class->init_pending = 0;
1537 * class->interface_offsets is needed for the castclass/isinst code, so
1538 * we have to setup them for interfaces, too.
1540 setup_interface_offsets (class, 0);
1541 mono_loader_unlock ();
1543 if (mono_debugger_class_init_func)
1544 mono_debugger_class_init_func (class);
1549 overrides = mono_class_get_overrides (class->image, class->type_token, &onum);
1550 mono_class_setup_vtable (class, overrides, onum);
1554 class->init_pending = 0;
1557 if (class == mono_defaults.object_class) {
1559 for (i = 0; i < class->vtable_size; ++i) {
1560 MonoMethod *cm = class->vtable [i];
1562 if (!strcmp (cm->name, "GetHashCode")) {
1568 g_assert (ghc_slot > 0);
1570 default_ghc = class->vtable [ghc_slot];
1575 if (class->parent) {
1577 if (class->vtable [ghc_slot] == default_ghc) {
1582 if (!default_finalize) {
1583 if (class == mono_defaults.object_class) {
1585 for (i = 0; i < class->vtable_size; ++i) {
1586 MonoMethod *cm = class->vtable [i];
1588 if (!strcmp (cm->name, "Finalize")) {
1594 g_assert (finalize_slot > 0);
1596 default_finalize = class->vtable [finalize_slot];
1600 /* Object::Finalize should have empty implemenatation */
1601 class->has_finalize = 0;
1602 if (class->parent) {
1603 if (class->vtable [finalize_slot] != default_finalize)
1604 class->has_finalize = 1;
1607 mono_loader_unlock ();
1609 if (mono_debugger_class_init_func)
1610 mono_debugger_class_init_func (class);
1614 mono_class_setup_mono_type (MonoClass *class)
1616 const char *name = class->name;
1617 const char *nspace = class->name_space;
1619 if (MONO_CLASS_IS_INTERFACE (class))
1620 class->interface_id = mono_get_unique_iid (class);
1622 class->this_arg.byref = 1;
1623 class->this_arg.data.klass = class;
1624 class->this_arg.type = MONO_TYPE_CLASS;
1625 class->byval_arg.data.klass = class;
1626 class->byval_arg.type = MONO_TYPE_CLASS;
1628 if (!strcmp (nspace, "System")) {
1629 if (!strcmp (name, "ValueType")) {
1631 * do not set the valuetype bit for System.ValueType.
1632 * class->valuetype = 1;
1634 class->blittable = TRUE;
1635 } else if (!strcmp (name, "Enum")) {
1637 * do not set the valuetype bit for System.Enum.
1638 * class->valuetype = 1;
1640 class->valuetype = 0;
1641 class->enumtype = 0;
1642 } else if (!strcmp (name, "Object")) {
1643 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
1644 } else if (!strcmp (name, "String")) {
1645 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
1646 } else if (!strcmp (name, "TypedReference")) {
1647 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
1651 if (class->valuetype) {
1652 int t = MONO_TYPE_VALUETYPE;
1653 if (!strcmp (nspace, "System")) {
1656 if (!strcmp (name, "Boolean")) {
1657 t = MONO_TYPE_BOOLEAN;
1658 } else if (!strcmp(name, "Byte")) {
1660 class->blittable = TRUE;
1664 if (!strcmp (name, "Char")) {
1669 if (!strcmp (name, "Double")) {
1671 class->blittable = TRUE;
1675 if (!strcmp (name, "Int32")) {
1677 class->blittable = TRUE;
1678 } else if (!strcmp(name, "Int16")) {
1680 class->blittable = TRUE;
1681 } else if (!strcmp(name, "Int64")) {
1683 class->blittable = TRUE;
1684 } else if (!strcmp(name, "IntPtr")) {
1686 class->blittable = TRUE;
1690 if (!strcmp (name, "Single")) {
1692 class->blittable = TRUE;
1693 } else if (!strcmp(name, "SByte")) {
1695 class->blittable = TRUE;
1699 if (!strcmp (name, "UInt32")) {
1701 class->blittable = TRUE;
1702 } else if (!strcmp(name, "UInt16")) {
1704 class->blittable = TRUE;
1705 } else if (!strcmp(name, "UInt64")) {
1707 class->blittable = TRUE;
1708 } else if (!strcmp(name, "UIntPtr")) {
1710 class->blittable = TRUE;
1714 if (!strcmp (name, "TypedReference")) {
1715 t = MONO_TYPE_TYPEDBYREF;
1716 class->blittable = TRUE;
1720 if (!strcmp (name, "Void")) {
1728 class->this_arg.type = class->byval_arg.type = t;
1733 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
1735 gboolean system_namespace;
1737 system_namespace = !strcmp (class->name_space, "System");
1739 /* if root of the hierarchy */
1740 if (system_namespace && !strcmp (class->name, "Object")) {
1741 class->parent = NULL;
1742 class->instance_size = sizeof (MonoObject);
1745 if (!strcmp (class->name, "<Module>")) {
1746 class->parent = NULL;
1747 class->instance_size = 0;
1751 if (!MONO_CLASS_IS_INTERFACE (class)) {
1752 class->parent = parent;
1755 g_assert_not_reached (); /* FIXME */
1757 if (parent->generic_class && !parent->name) {
1759 * If the parent is a generic instance, we may get
1760 * called before it is fully initialized, especially
1761 * before it has its name.
1766 class->marshalbyref = parent->marshalbyref;
1767 class->contextbound = parent->contextbound;
1768 class->delegate = parent->delegate;
1770 if (system_namespace) {
1771 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
1772 class->marshalbyref = 1;
1774 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
1775 class->contextbound = 1;
1777 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
1778 class->delegate = 1;
1781 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
1782 (strcmp (class->parent->name_space, "System") == 0)))
1783 class->valuetype = 1;
1784 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
1785 class->valuetype = class->enumtype = 1;
1787 /*class->enumtype = class->parent->enumtype; */
1788 mono_class_setup_supertypes (class);
1790 class->parent = NULL;
1796 mono_class_setup_supertypes (MonoClass *class)
1801 if (class->supertypes)
1805 for (k = class; k ; k = k->parent) {
1809 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
1810 class->supertypes = g_new0 (MonoClass *, ms);
1812 if (class->parent) {
1813 for (i = class->idepth, k = class; k ; k = k->parent)
1814 class->supertypes [--i] = k;
1816 class->supertypes [0] = class;
1821 * If we inherit a type parameter from an outer class, set its owner to that class.
1824 set_generic_param_owner (MonoGenericContainer *container, MonoClass *klass, int pos)
1826 MonoGenericContainer *gc;
1829 if (klass->nested_in)
1830 pos = set_generic_param_owner (container, klass->nested_in, pos);
1832 if (!klass->generic_container)
1835 gc = klass->generic_container;
1836 for (i = pos; i < gc->type_argc; i++)
1837 container->type_params [i].owner = gc;
1839 return pos + gc->type_argc;
1842 static MonoGenericInst *
1843 get_shared_inst (MonoGenericContainer *container)
1845 MonoGenericInst *nginst;
1848 nginst = g_new0 (MonoGenericInst, 1);
1849 nginst->type_argc = container->type_argc;
1850 nginst->type_argv = g_new0 (MonoType *, nginst->type_argc);
1851 nginst->is_open = 1;
1853 for (i = 0; i < nginst->type_argc; i++) {
1854 MonoType *t = g_new0 (MonoType, 1);
1856 t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
1857 t->data.generic_param = &container->type_params [i];
1859 nginst->type_argv [i] = t;
1862 return mono_metadata_lookup_generic_inst (nginst);
1865 static MonoGenericClass *
1866 get_shared_gclass (MonoGenericContainer *container)
1868 MonoGenericClass *gclass = g_new0 (MonoGenericClass, 1);
1869 MonoGenericClass *cached;
1871 gclass->context = &container->context;
1872 gclass->container_class = container->klass;
1873 gclass->inst = get_shared_inst (container);
1875 cached = mono_metadata_lookup_generic_class (gclass);
1881 gclass->klass = container->klass;
1887 * @image: context where the image is created
1888 * @type_token: typedef token
1891 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
1893 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
1894 MonoClass *class, *parent = NULL;
1895 guint32 cols [MONO_TYPEDEF_SIZE];
1896 guint32 cols_next [MONO_TYPEDEF_SIZE];
1897 guint tidx = mono_metadata_token_index (type_token);
1898 MonoGenericContext *context = NULL;
1899 const char *name, *nspace;
1901 MonoClass **interfaces;
1903 mono_loader_lock ();
1905 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
1906 mono_loader_unlock ();
1910 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
1912 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
1914 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1915 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1917 class = g_malloc0 (sizeof (MonoClass));
1920 class->name_space = nspace;
1922 class->image = image;
1923 class->type_token = type_token;
1924 class->flags = cols [MONO_TYPEDEF_FLAGS];
1926 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
1928 class->generic_container = mono_metadata_load_generic_params (image, class->type_token, NULL);
1929 if (class->generic_container) {
1930 class->generic_container->klass = class;
1931 context = &class->generic_container->context;
1933 context->gclass = get_shared_gclass (context->container);
1936 if (cols [MONO_TYPEDEF_EXTENDS])
1937 parent = mono_class_get_full (
1938 image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]), context);
1940 mono_class_setup_parent (class, parent);
1942 mono_class_setup_mono_type (class);
1944 interfaces = mono_metadata_interfaces_from_typedef_full (image, type_token, &icount, context);
1946 class->interfaces = interfaces;
1947 class->interface_count = icount;
1949 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
1951 /* fixme: maybe we must set this on windows
1952 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
1956 class->cast_class = class->element_class = class;
1958 /*g_print ("Load class %s\n", name);*/
1961 * Compute the field and method lists
1963 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
1964 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
1966 if (tt->rows > tidx){
1967 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
1968 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1969 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1971 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
1972 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
1975 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
1976 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
1977 class->field.count = class->field.last - class->field.first;
1979 class->field.count = 0;
1981 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
1982 class->method.count = class->method.last - class->method.first;
1984 class->method.count = 0;
1986 /* reserve space to store vector pointer in arrays */
1987 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
1988 class->instance_size += 2 * sizeof (gpointer);
1989 g_assert (class->field.count == 0);
1992 if (class->enumtype)
1993 class_compute_field_layout (class);
1995 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
1996 class->nested_in = mono_class_create_from_typedef (image, type_token);
1998 if (class->nested_in && class->generic_container)
1999 set_generic_param_owner (class->generic_container, class->nested_in, 0);
2001 mono_loader_unlock ();
2007 mono_class_create_generic (MonoGenericClass *gclass)
2009 MonoClass *klass, *gklass;
2012 gclass->klass = g_malloc0 (sizeof (MonoClass));
2013 klass = gclass->klass;
2015 gklass = gclass->container_class;
2017 klass->nested_in = gklass->nested_in;
2019 klass->name = gklass->name;
2020 klass->name_space = gklass->name_space;
2021 klass->image = gklass->image;
2022 klass->flags = gklass->flags;
2024 klass->generic_class = gclass;
2026 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
2027 klass->this_arg.data.generic_class = klass->byval_arg.data.generic_class = gclass;
2028 klass->this_arg.byref = TRUE;
2030 klass->cast_class = klass->element_class = klass;
2032 if (gclass->is_dynamic) {
2033 klass->instance_size = gklass->instance_size;
2034 klass->class_size = gklass->class_size;
2035 klass->size_inited = 1;
2038 klass->valuetype = gklass->valuetype;
2040 mono_class_setup_supertypes (klass);
2045 mono_class_create_generic_2 (MonoGenericClass *gclass)
2047 MonoClass *klass, *gklass;
2051 if (gclass->is_dynamic)
2054 klass = gclass->klass;
2055 gklass = gclass->container_class;
2057 klass->method = gklass->method;
2058 klass->field = gklass->field;
2060 klass->interface_count = gklass->interface_count;
2061 klass->interfaces = g_new0 (MonoClass *, klass->interface_count);
2062 for (i = 0; i < klass->interface_count; i++) {
2063 MonoType *it = &gklass->interfaces [i]->byval_arg;
2064 MonoType *inflated = mono_class_inflate_generic_type (it, gclass->context);
2065 klass->interfaces [i] = mono_class_from_mono_type (inflated);
2068 for (list = gklass->nested_classes; list; list = list->next)
2069 klass->nested_classes = g_list_append (
2070 klass->nested_classes, list->data);
2073 klass->parent = mono_class_from_mono_type (gclass->parent);
2074 else if (gklass->parent) {
2075 MonoType *inflated = mono_class_inflate_generic_type (&gklass->parent->byval_arg, gclass->context);
2077 klass->parent = mono_class_from_mono_type (inflated);
2080 mono_class_setup_parent (klass, klass->parent);
2084 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
2086 MonoClass *klass, **ptr;
2090 return param->pklass;
2092 klass = param->pklass = g_new0 (MonoClass, 1);
2094 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
2098 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
2099 klass->parent = param->constraints [0];
2103 if (count - pos > 0) {
2104 klass->interface_count = count - pos;
2105 klass->interfaces = g_new0 (MonoClass *, count - pos);
2106 for (i = pos; i < count; i++)
2107 klass->interfaces [i - pos] = param->constraints [i];
2110 g_assert (param->name && param->owner);
2112 klass->name = param->name;
2113 klass->name_space = "";
2114 klass->image = image;
2115 klass->cast_class = klass->element_class = klass;
2116 klass->enum_basetype = &klass->element_class->byval_arg;
2117 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2119 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2120 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2121 klass->this_arg.byref = TRUE;
2123 mono_class_init (klass);
2129 my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
2134 return param->pklass;
2136 g_assert (param->owner);
2138 klass = g_new0 (MonoClass, 1);
2141 klass->name = param->name;
2143 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
2144 klass->name_space = "";
2145 klass->image = mono_defaults.corlib;
2146 klass->cast_class = klass->element_class = klass;
2147 klass->enum_basetype = &klass->element_class->byval_arg;
2148 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2150 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2151 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2152 klass->this_arg.byref = TRUE;
2154 mono_class_init (klass);
2160 mono_ptr_class_get (MonoType *type)
2163 MonoClass *el_class;
2164 static GHashTable *ptr_hash = NULL;
2166 mono_loader_lock ();
2169 ptr_hash = g_hash_table_new (NULL, NULL);
2170 el_class = mono_class_from_mono_type (type);
2171 if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
2172 mono_loader_unlock ();
2175 result = g_new0 (MonoClass, 1);
2177 result->parent = NULL; /* no parent for PTR types */
2178 result->name_space = el_class->name_space;
2179 result->name = g_strdup_printf ("%s*", el_class->name);
2180 result->image = el_class->image;
2181 result->inited = TRUE;
2182 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2183 /* Can pointers get boxed? */
2184 result->instance_size = sizeof (gpointer);
2185 result->cast_class = result->element_class = el_class;
2186 result->enum_basetype = &result->element_class->byval_arg;
2187 result->blittable = TRUE;
2189 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
2190 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
2191 result->this_arg.byref = TRUE;
2193 mono_class_setup_supertypes (result);
2195 g_hash_table_insert (ptr_hash, el_class, result);
2197 mono_loader_unlock ();
2203 mono_fnptr_class_get (MonoMethodSignature *sig)
2206 static GHashTable *ptr_hash = NULL;
2208 mono_loader_lock ();
2211 ptr_hash = g_hash_table_new (NULL, NULL);
2213 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
2214 mono_loader_unlock ();
2217 result = g_new0 (MonoClass, 1);
2219 result->parent = NULL; /* no parent for PTR types */
2220 result->name = "System";
2221 result->name_space = "MonoFNPtrFakeClass";
2222 result->image = NULL; /* need to fix... */
2223 result->inited = TRUE;
2224 result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
2225 /* Can pointers get boxed? */
2226 result->instance_size = sizeof (gpointer);
2227 result->cast_class = result->element_class = result;
2228 result->blittable = TRUE;
2230 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
2231 result->this_arg.data.method = result->byval_arg.data.method = sig;
2232 result->this_arg.byref = TRUE;
2233 result->enum_basetype = &result->element_class->byval_arg;
2234 result->blittable = TRUE;
2236 mono_class_setup_supertypes (result);
2238 g_hash_table_insert (ptr_hash, sig, result);
2240 mono_loader_unlock ();
2246 mono_class_from_mono_type (MonoType *type)
2248 switch (type->type) {
2249 case MONO_TYPE_OBJECT:
2250 return type->data.klass? type->data.klass: mono_defaults.object_class;
2251 case MONO_TYPE_VOID:
2252 return type->data.klass? type->data.klass: mono_defaults.void_class;
2253 case MONO_TYPE_BOOLEAN:
2254 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
2255 case MONO_TYPE_CHAR:
2256 return type->data.klass? type->data.klass: mono_defaults.char_class;
2258 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
2260 return type->data.klass? type->data.klass: mono_defaults.byte_class;
2262 return type->data.klass? type->data.klass: mono_defaults.int16_class;
2264 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
2266 return type->data.klass? type->data.klass: mono_defaults.int32_class;
2268 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
2270 return type->data.klass? type->data.klass: mono_defaults.int_class;
2272 return type->data.klass? type->data.klass: mono_defaults.uint_class;
2274 return type->data.klass? type->data.klass: mono_defaults.int64_class;
2276 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
2278 return type->data.klass? type->data.klass: mono_defaults.single_class;
2280 return type->data.klass? type->data.klass: mono_defaults.double_class;
2281 case MONO_TYPE_STRING:
2282 return type->data.klass? type->data.klass: mono_defaults.string_class;
2283 case MONO_TYPE_TYPEDBYREF:
2284 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
2285 case MONO_TYPE_ARRAY:
2286 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
2288 return mono_ptr_class_get (type->data.type);
2289 case MONO_TYPE_FNPTR:
2290 return mono_fnptr_class_get (type->data.method);
2291 case MONO_TYPE_SZARRAY:
2292 return mono_array_class_get (type->data.klass, 1);
2293 case MONO_TYPE_CLASS:
2294 case MONO_TYPE_VALUETYPE:
2295 return type->data.klass;
2296 case MONO_TYPE_GENERICINST:
2297 g_assert (type->data.generic_class->klass);
2298 return type->data.generic_class->klass;
2300 return my_mono_class_from_generic_parameter (type->data.generic_param, FALSE);
2301 case MONO_TYPE_MVAR:
2302 return my_mono_class_from_generic_parameter (type->data.generic_param, TRUE);
2304 g_warning ("implement me 0x%02x\n", type->type);
2305 g_assert_not_reached ();
2312 * @image: context where the image is created
2313 * @type_spec: typespec token
2316 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec,
2317 MonoGenericContext *context)
2319 MonoType *type, *inflated;
2322 type = mono_type_create_from_typespec_full (image, context, type_spec);
2324 switch (type->type) {
2325 case MONO_TYPE_ARRAY:
2326 class = mono_array_class_get (type->data.array->eklass, type->data.array->rank);
2328 case MONO_TYPE_SZARRAY:
2329 class = mono_array_class_get (type->data.klass, 1);
2332 class = mono_ptr_class_get (type->data.type);
2334 case MONO_TYPE_GENERICINST:
2335 g_assert (type->data.generic_class->klass);
2336 class = type->data.generic_class->klass;
2339 /* it seems any type can be stored in TypeSpec as well */
2340 class = mono_class_from_mono_type (type);
2344 if (!class || !context || (!context->gclass && !context->gmethod))
2347 inflated = mono_class_inflate_generic_type (&class->byval_arg, context);
2349 return mono_class_from_mono_type (inflated);
2353 * mono_bounded_array_class_get:
2354 * @element_class: element class
2355 * @rank: the dimension of the array class
2356 * @bounded: whenever the array has non-zero bounds
2358 * Returns: a class object describing the array with element type @element_type and
2362 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
2366 MonoClass *parent = NULL;
2367 GSList *list, *rootlist;
2370 gboolean corlib_type = FALSE;
2372 g_assert (rank <= 255);
2375 /* bounded only matters for one-dimensional arrays */
2378 image = eclass->image;
2380 mono_loader_lock ();
2382 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
2383 for (; list; list = list->next) {
2385 if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
2386 mono_loader_unlock ();
2392 /* for the building corlib use System.Array from it */
2393 if (image->assembly && image->assembly->dynamic && strcmp (image->assembly_name, "mscorlib") == 0) {
2394 parent = mono_class_from_name (image, "System", "Array");
2397 parent = mono_defaults.array_class;
2398 if (!parent->inited)
2399 mono_class_init (parent);
2402 class = g_malloc0 (sizeof (MonoClass));
2404 class->image = image;
2405 class->name_space = eclass->name_space;
2406 nsize = strlen (eclass->name);
2407 name = g_malloc (nsize + 2 + rank);
2408 memcpy (name, eclass->name, nsize);
2411 memset (name + nsize + 1, ',', rank - 1);
2412 name [nsize + rank] = ']';
2413 name [nsize + rank + 1] = 0;
2415 class->type_token = 0;
2416 /* all arrays are marked serializable and sealed, bug #42779 */
2417 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
2418 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2419 class->parent = parent;
2420 class->instance_size = mono_class_instance_size (class->parent);
2421 class->class_size = 0;
2422 mono_class_setup_supertypes (class);
2426 if (eclass->enumtype)
2427 class->cast_class = eclass->element_class;
2429 class->cast_class = eclass;
2431 class->element_class = eclass;
2433 if ((rank > 1) || bounded) {
2434 MonoArrayType *at = g_new0 (MonoArrayType, 1);
2435 class->byval_arg.type = MONO_TYPE_ARRAY;
2436 class->byval_arg.data.array = at;
2437 at->eklass = eclass;
2439 /* FIXME: complete.... */
2441 class->byval_arg.type = MONO_TYPE_SZARRAY;
2442 class->byval_arg.data.klass = eclass;
2444 class->this_arg = class->byval_arg;
2445 class->this_arg.byref = 1;
2450 list = g_slist_append (rootlist, class);
2451 g_hash_table_insert (image->array_cache, eclass, list);
2453 mono_loader_unlock ();
2459 * mono_array_class_get:
2460 * @element_class: element class
2461 * @rank: the dimension of the array class
2463 * Returns: a class object describing the array with element type @element_type and
2467 mono_array_class_get (MonoClass *eclass, guint32 rank)
2469 return mono_bounded_array_class_get (eclass, rank, FALSE);
2473 * mono_class_instance_size:
2476 * Returns: the size of an object instance
2479 mono_class_instance_size (MonoClass *klass)
2481 if (!klass->size_inited)
2482 mono_class_init (klass);
2484 g_assert (!klass->generic_container &&
2485 (!klass->generic_class || !klass->generic_class->inst->is_open));
2486 return klass->instance_size;
2490 * mono_class_min_align:
2493 * Returns: minimm alignment requirements
2496 mono_class_min_align (MonoClass *klass)
2498 if (!klass->size_inited)
2499 mono_class_init (klass);
2501 return klass->min_align;
2505 * mono_class_value_size:
2508 * This function is used for value types, and return the
2509 * space and the alignment to store that kind of value object.
2511 * Returns: the size of a value of kind @klass
2514 mono_class_value_size (MonoClass *klass, guint32 *align)
2518 /* fixme: check disable, because we still have external revereces to
2519 * mscorlib and Dummy Objects
2521 /*g_assert (klass->valuetype);*/
2523 size = mono_class_instance_size (klass) - sizeof (MonoObject);
2526 *align = klass->min_align;
2532 * mono_class_data_size:
2535 * Returns: the size of the static class data
2538 mono_class_data_size (MonoClass *klass)
2541 mono_class_init (klass);
2543 return klass->class_size;
2547 * Auxiliary routine to mono_class_get_field
2549 * Takes a field index instead of a field token.
2551 static MonoClassField *
2552 mono_class_get_field_idx (MonoClass *class, int idx)
2554 if (class->field.count){
2555 if ((idx >= class->field.first) && (idx < class->field.last)){
2556 return &class->fields [idx - class->field.first];
2563 return mono_class_get_field_idx (class->parent, idx);
2567 * mono_class_get_field:
2568 * @class: the class to lookup the field.
2569 * @field_token: the field token
2571 * Returns: A MonoClassField representing the type and offset of
2572 * the field, or a NULL value if the field does not belong to this
2576 mono_class_get_field (MonoClass *class, guint32 field_token)
2578 int idx = mono_metadata_token_index (field_token);
2580 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
2582 return mono_class_get_field_idx (class, idx - 1);
2586 mono_class_get_field_from_name (MonoClass *klass, const char *name)
2591 for (i = 0; i < klass->field.count; ++i) {
2592 if (strcmp (name, klass->fields [i].name) == 0)
2593 return &klass->fields [i];
2595 klass = klass->parent;
2601 mono_class_get_field_token (MonoClassField *field)
2603 MonoClass *klass = field->parent;
2607 for (i = 0; i < klass->field.count; ++i) {
2608 if (&klass->fields [i] == field)
2609 return mono_metadata_make_token (MONO_TABLE_FIELD, klass->field.first + i + 1);
2611 klass = klass->parent;
2614 g_assert_not_reached ();
2619 mono_class_get_event_token (MonoEvent *event)
2621 MonoClass *klass = event->parent;
2625 for (i = 0; i < klass->event.count; ++i) {
2626 if (&klass->events [i] == event)
2627 return mono_metadata_make_token (MONO_TABLE_EVENT, klass->event.first + i + 1);
2629 klass = klass->parent;
2632 g_assert_not_reached ();
2637 mono_vtable_get_static_field_data (MonoVTable *vt)
2643 mono_class_get_property_from_name (MonoClass *klass, const char *name)
2648 for (i = 0; i < klass->property.count; ++i) {
2649 if (strcmp (name, klass->properties [i].name) == 0)
2650 return &klass->properties [i];
2652 klass = klass->parent;
2658 mono_class_get_property_token (MonoProperty *prop)
2660 MonoClass *klass = prop->parent;
2664 for (i = 0; i < klass->property.count; ++i) {
2665 if (&klass->properties [i] == prop)
2666 return mono_metadata_make_token (MONO_TABLE_PROPERTY, klass->property.first + i + 1);
2668 klass = klass->parent;
2671 g_assert_not_reached ();
2677 * @image: the image where the class resides
2678 * @type_token: the token for the class
2679 * @at: an optional pointer to return the array element type
2681 * Returns: the MonoClass that represents @type_token in @image
2684 _mono_class_get (MonoImage *image, guint32 type_token, MonoGenericContext *context)
2686 MonoClass *class = NULL;
2689 return mono_lookup_dynamic_token (image, type_token);
2691 switch (type_token & 0xff000000){
2692 case MONO_TOKEN_TYPE_DEF:
2693 class = mono_class_create_from_typedef (image, type_token);
2695 case MONO_TOKEN_TYPE_REF:
2696 class = mono_class_from_typeref (image, type_token);
2698 case MONO_TOKEN_TYPE_SPEC:
2699 class = mono_class_create_from_typespec (image, type_token, context);
2702 g_warning ("unknown token type %x", type_token & 0xff000000);
2703 g_assert_not_reached ();
2707 g_warning ("Could not load class from token 0x%08x in %s", type_token, image->name);
2713 mono_class_get (MonoImage *image, guint32 type_token)
2715 return _mono_class_get (image, type_token, NULL);
2719 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
2721 MonoClass *class = _mono_class_get (image, type_token, context);
2724 if (!class || !context || (!context->gclass && !context->gmethod))
2727 switch (class->byval_arg.type) {
2728 case MONO_TYPE_GENERICINST:
2729 if (!class->generic_class->inst->is_open)
2733 case MONO_TYPE_MVAR:
2739 inflated = inflate_generic_type (&class->byval_arg, context);
2743 return mono_class_from_mono_type (inflated);
2747 * mono_class_from_name_case:
2748 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
2749 * @name_space: the type namespace
2750 * @name: the type short name.
2752 * Obtains a MonoClass with a given namespace and a given name which
2753 * is located in the given MonoImage. The namespace and name
2754 * lookups are case insensitive.
2756 * You can also pass @NULL to the image, and that will lookup for
2757 * a type with the given namespace and name in all of the loaded
2758 * assemblies: notice that since there might be a name clash in this
2759 * case, passing @NULL is not encouraged if you need a precise type.
2763 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
2765 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
2766 guint32 cols [MONO_TYPEDEF_SIZE];
2771 /* add a cache if needed */
2772 for (i = 1; i <= t->rows; ++i) {
2773 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
2774 /* nested types are accessed from the nesting name */
2775 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
2776 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
2778 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2779 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2780 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
2781 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
2787 return_nested_in (MonoClass *class, char *nested) {
2789 char *s = strchr (nested, '/');
2796 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
2798 if (strcmp (found->name, nested) == 0) {
2800 return return_nested_in (found, s);
2809 * mono_class_from_name_case:
2810 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
2811 * @name_space: the type namespace
2812 * @name: the type short name.
2814 * Obtains a MonoClass with a given namespace and a given name which
2815 * is located in the given MonoImage.
2817 * You can also pass `NULL' to the image, and that will lookup for
2818 * a type with the given namespace and name in all of the loaded
2819 * assemblies: notice that since there might be a name clash in this
2820 * case, passing NULL is not encouraged if you need a precise type.
2824 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
2826 GHashTable *nspace_table;
2827 MonoImage *loaded_image;
2833 if ((nested = strchr (name, '/'))) {
2834 int pos = nested - name;
2835 int len = strlen (name);
2838 memcpy (buf, name, len + 1);
2840 nested = buf + pos + 1;
2844 mono_loader_lock ();
2846 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
2849 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
2851 mono_loader_unlock ();
2856 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
2857 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
2858 guint32 cols [MONO_EXP_TYPE_SIZE];
2861 idx = mono_metadata_token_index (token);
2863 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
2865 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
2866 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
2867 loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
2870 class = mono_class_from_name (loaded_image, name_space, name);
2872 return return_nested_in (class, nested);
2875 g_error ("not yet implemented");
2879 token = MONO_TOKEN_TYPE_DEF | token;
2881 class = mono_class_get (image, token);
2883 return return_nested_in (class, nested);
2888 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
2889 gboolean check_interfaces)
2892 g_assert (klassc->idepth > 0);
2893 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
2894 if ((klassc->interface_id <= klass->max_interface_id) &&
2895 (klass->interface_offsets [klassc->interface_id] >= 0))
2897 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
2900 for (i = 0; i < klass->interface_count; i ++) {
2901 MonoClass *ic = klass->interfaces [i];
2906 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
2911 * MS.NET thinks interfaces are a subclass of Object, so we think it as
2914 if (klassc == mono_defaults.object_class)
2917 if (klass->generic_class) {
2918 MonoType *parent = klass->generic_class->parent;
2922 if (mono_metadata_type_equal (parent, &klassc->byval_arg))
2924 klass = mono_class_from_mono_type (parent);
2932 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
2935 mono_class_init (klass);
2937 if (!oklass->inited)
2938 mono_class_init (oklass);
2940 if (MONO_CLASS_IS_INTERFACE (klass)) {
2941 if ((klass->interface_id <= oklass->max_interface_id) &&
2942 (oklass->interface_offsets [klass->interface_id] != -1))
2946 MonoClass *eclass, *eoclass;
2948 if (oklass->rank != klass->rank)
2951 /* vectors vs. one dimensional arrays */
2952 if (oklass->byval_arg.type != klass->byval_arg.type)
2955 eclass = klass->cast_class;
2956 eoclass = oklass->cast_class;
2960 * a is b does not imply a[] is b[] when a is a valuetype, and
2961 * b is a reference type.
2964 if (eoclass->valuetype) {
2965 if ((eclass == mono_defaults.enum_class) ||
2966 (eclass == mono_defaults.enum_class->parent) ||
2967 (eclass == mono_defaults.object_class))
2971 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
2974 if (klass == mono_defaults.object_class)
2977 return mono_class_has_parent (oklass, klass);
2981 * mono_class_needs_cctor_run:
2983 * Determines whenever the class has a static constructor and whenever it
2984 * needs to be called when executing CALLER.
2987 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
2992 for (i = 0; i < klass->method.count; ++i) {
2993 method = klass->methods [i];
2994 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2995 (strcmp (".cctor", method->name) == 0)) {
2996 if (caller == method)
3005 * mono_class_array_element_size:
3008 * Returns: the number of bytes an element of type @klass
3009 * uses when stored into an array.
3012 mono_class_array_element_size (MonoClass *klass)
3014 MonoType *type = &klass->byval_arg;
3017 switch (type->type) {
3020 case MONO_TYPE_BOOLEAN:
3024 case MONO_TYPE_CHAR:
3033 case MONO_TYPE_CLASS:
3034 case MONO_TYPE_STRING:
3035 case MONO_TYPE_OBJECT:
3036 case MONO_TYPE_SZARRAY:
3037 case MONO_TYPE_ARRAY:
3039 case MONO_TYPE_MVAR:
3040 return sizeof (gpointer);
3045 case MONO_TYPE_VALUETYPE:
3046 if (type->data.klass->enumtype) {
3047 type = type->data.klass->enum_basetype;
3048 klass = klass->element_class;
3051 return mono_class_instance_size (klass) - sizeof (MonoObject);
3052 case MONO_TYPE_GENERICINST:
3053 type = &type->data.generic_class->container_class->byval_arg;
3056 g_error ("unknown type 0x%02x in mono_class_array_element_size", type->type);
3062 * mono_array_element_size:
3063 * @ac: pointer to a #MonoArrayClass
3065 * Returns: the size of single array element.
3068 mono_array_element_size (MonoClass *ac)
3070 return mono_class_array_element_size (ac->element_class);
3074 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
3075 MonoGenericContext *context)
3077 if (image->dynamic) {
3078 gpointer obj = mono_lookup_dynamic_token (image, token);
3080 switch (token & 0xff000000) {
3081 case MONO_TOKEN_TYPE_DEF:
3082 case MONO_TOKEN_TYPE_REF:
3083 case MONO_TOKEN_TYPE_SPEC:
3085 *handle_class = mono_defaults.typehandle_class;
3086 return &((MonoClass*)obj)->byval_arg;
3087 case MONO_TOKEN_METHOD_DEF:
3089 *handle_class = mono_defaults.methodhandle_class;
3091 case MONO_TOKEN_FIELD_DEF:
3093 *handle_class = mono_defaults.fieldhandle_class;
3096 g_assert_not_reached ();
3100 switch (token & 0xff000000) {
3101 case MONO_TOKEN_TYPE_DEF:
3102 case MONO_TOKEN_TYPE_REF: {
3105 *handle_class = mono_defaults.typehandle_class;
3106 class = mono_class_get_full (image, token, context);
3107 mono_class_init (class);
3108 /* We return a MonoType* as handle */
3109 return &class->byval_arg;
3111 case MONO_TOKEN_TYPE_SPEC: {
3114 *handle_class = mono_defaults.typehandle_class;
3115 class = mono_class_create_from_typespec (image, token, context);
3116 mono_class_init (class);
3117 return &class->byval_arg;
3119 case MONO_TOKEN_FIELD_DEF: {
3121 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
3122 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
3123 mono_class_init (class);
3125 *handle_class = mono_defaults.fieldhandle_class;
3126 return mono_class_get_field (class, token);
3128 case MONO_TOKEN_METHOD_DEF: {
3130 meth = mono_get_method_full (image, token, NULL, context);
3132 *handle_class = mono_defaults.methodhandle_class;
3135 case MONO_TOKEN_MEMBER_REF: {
3136 guint32 cols [MONO_MEMBERREF_SIZE];
3138 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
3139 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
3140 mono_metadata_decode_blob_size (sig, &sig);
3141 if (*sig == 0x6) { /* it's a field */
3143 MonoClassField *field;
3144 field = mono_field_from_token (image, token, &klass, context);
3146 *handle_class = mono_defaults.fieldhandle_class;
3150 meth = mono_get_method_full (image, token, NULL, context);
3152 *handle_class = mono_defaults.methodhandle_class;
3157 g_warning ("Unknown token 0x%08x in ldtoken", token);
3164 * This function might need to call runtime functions so it can't be part
3165 * of the metadata library.
3167 static MonoLookupDynamicToken lookup_dynamic = NULL;
3170 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
3172 lookup_dynamic = func;
3176 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
3178 return lookup_dynamic (image, token);
3182 mono_class_get_image (MonoClass *klass)
3184 return klass->image;
3188 * mono_class_get_element_class:
3189 * @klass: the MonoClass to act on
3191 * Returns: the element class of an array or an enumeration.
3194 mono_class_get_element_class (MonoClass *klass)
3196 return klass->element_class;
3200 * mono_class_is_valuetype:
3201 * @klass: the MonoClass to act on
3203 * Returns: true if the MonoClass represents a ValueType.
3206 mono_class_is_valuetype (MonoClass *klass)
3208 return klass->valuetype;
3212 * mono_class_is_enum:
3213 * @klass: the MonoClass to act on
3215 * Returns: true if the MonoClass represents an enumeration.
3218 mono_class_is_enum (MonoClass *klass)
3220 return klass->enumtype;
3224 * mono_class_enum_basetype:
3225 * @klass: the MonoClass to act on
3227 * Returns: the underlying type representation for an enumeration.
3230 mono_class_enum_basetype (MonoClass *klass)
3232 return klass->enum_basetype;
3236 * mono_class_get_parent
3237 * @klass: the MonoClass to act on
3239 * Returns: the parent class for this class.
3242 mono_class_get_parent (MonoClass *klass)
3244 return klass->parent;
3248 * mono_class_get_nesting_type;
3249 * @klass: the MonoClass to act on
3251 * Returns: the container type where this type is nested or NULL if this type is not a nested type.
3254 mono_class_get_nesting_type (MonoClass *klass)
3256 return klass->nested_in;
3260 * mono_class_get_rank:
3261 * @klass: the MonoClass to act on
3263 * Returns: the rank for the array (the number of dimensions).
3266 mono_class_get_rank (MonoClass *klass)
3272 * mono_class_get_flags:
3273 * @klass: the MonoClass to act on
3275 * The type flags from the TypeDef table from the metadata.
3276 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
3279 * Returns: the flags from the TypeDef table.
3282 mono_class_get_flags (MonoClass *klass)
3284 return klass->flags;
3288 * mono_class_get_name
3289 * @klass: the MonoClass to act on
3291 * Returns: the name of the class.
3294 mono_class_get_name (MonoClass *klass)
3300 * mono_class_get_namespace:
3301 * @klass: the MonoClass to act on
3303 * Returns: the namespace of the class.
3306 mono_class_get_namespace (MonoClass *klass)
3308 return klass->name_space;
3312 * mono_class_get_type:
3313 * @klass: the MonoClass to act on
3315 * This method returns the internal Type representation for the class.
3317 * Returns: the MonoType from the class.
3320 mono_class_get_type (MonoClass *klass)
3322 return &klass->byval_arg;
3326 * mono_class_get_byref_type:
3327 * @klass: the MonoClass to act on
3332 mono_class_get_byref_type (MonoClass *klass)
3334 return &klass->this_arg;
3338 * mono_class_num_fields:
3339 * @klass: the MonoClass to act on
3341 * Returns: the number of static and instance fields in the class.
3344 mono_class_num_fields (MonoClass *klass)
3346 return klass->field.count;
3350 * mono_class_num_methods:
3351 * @klass: the MonoClass to act on
3353 * Returns: the number of methods in the class.
3356 mono_class_num_methods (MonoClass *klass)
3358 return klass->method.count;
3362 * mono_class_num_properties
3363 * @klass: the MonoClass to act on
3365 * Returns: the number of properties in the class.
3368 mono_class_num_properties (MonoClass *klass)
3370 return klass->property.count;
3374 * mono_class_num_events:
3375 * @klass: the MonoClass to act on
3377 * Returns: the number of events in the class.
3380 mono_class_num_events (MonoClass *klass)
3382 return klass->event.count;
3386 * mono_class_get_fields:
3387 * @klass: the MonoClass to act on
3389 * This routine is an iterator routine for retrieving the fields in a class.
3391 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3392 * iterate over all of the elements. When no more values are
3393 * available, the return value is NULL.
3395 * Returns: a @MonoClassField* on each iteration, or NULL when no more fields are available.
3398 mono_class_get_fields (MonoClass* klass, gpointer *iter)
3400 MonoClassField* field;
3404 mono_class_init (klass);
3406 /* start from the first */
3407 if (klass->field.count) {
3408 return *iter = &klass->fields [0];
3416 if (field < &klass->fields [klass->field.count]) {
3417 return *iter = field;
3423 * mono_class_get_methods
3424 * @klass: the MonoClass to act on
3426 * This routine is an iterator routine for retrieving the fields in a class.
3428 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3429 * iterate over all of the elements. When no more values are
3430 * available, the return value is NULL.
3432 * Returns: a MonoMethod on each iteration or NULL when no more methods are available.
3435 mono_class_get_methods (MonoClass* klass, gpointer *iter)
3437 MonoMethod** method;
3441 mono_class_init (klass);
3443 /* start from the first */
3444 if (klass->method.count) {
3445 *iter = &klass->methods [0];
3446 return klass->methods [0];
3454 if (method < &klass->methods [klass->method.count]) {
3462 * mono_class_get_properties:
3463 * @klass: the MonoClass to act on
3465 * This routine is an iterator routine for retrieving the properties in a class.
3467 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3468 * iterate over all of the elements. When no more values are
3469 * available, the return value is NULL.
3471 * Returns: a @MonoProperty* on each invocation, or NULL when no more are available.
3474 mono_class_get_properties (MonoClass* klass, gpointer *iter)
3476 MonoProperty* property;
3480 mono_class_init (klass);
3482 /* start from the first */
3483 if (klass->property.count) {
3484 return *iter = &klass->properties [0];
3492 if (property < &klass->properties [klass->property.count]) {
3493 return *iter = property;
3499 * mono_class_get_events:
3500 * @klass: the MonoClass to act on
3502 * This routine is an iterator routine for retrieving the properties in a class.
3504 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3505 * iterate over all of the elements. When no more values are
3506 * available, the return value is NULL.
3508 * Returns: a @MonoEvent* on each invocation, or NULL when no more are available.
3511 mono_class_get_events (MonoClass* klass, gpointer *iter)
3517 mono_class_init (klass);
3519 /* start from the first */
3520 if (klass->event.count) {
3521 return *iter = &klass->events [0];
3529 if (event < &klass->events [klass->event.count]) {
3530 return *iter = event;
3536 * mono_class_get_interfaces
3537 * @klass: the MonoClass to act on
3539 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
3541 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3542 * iterate over all of the elements. When no more values are
3543 * available, the return value is NULL.
3545 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
3548 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
3554 mono_class_init (klass);
3556 /* start from the first */
3557 if (klass->interface_count) {
3558 *iter = &klass->interfaces [0];
3559 return klass->interfaces [0];
3567 if (iface < &klass->interfaces [klass->interface_count]) {
3575 * mono_class_get_nested_types
3576 * @klass: the MonoClass to act on
3578 * This routine is an iterator routine for retrieving the nested types of a class.
3580 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3581 * iterate over all of the elements. When no more values are
3582 * available, the return value is NULL.
3584 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
3587 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
3593 mono_class_init (klass);
3595 /* start from the first */
3596 if (klass->nested_classes) {
3597 *iter = klass->nested_classes;
3598 return klass->nested_classes->data;
3600 /* no nested types */
3614 * mono_field_get_name:
3615 * @field: the MonoClassField to act on
3617 * Returns: the name of the field.
3620 mono_field_get_name (MonoClassField *field)
3626 * mono_field_get_type:
3627 * @field: the MonoClassField to act on
3629 * Returns: MonoType of the field.
3632 mono_field_get_type (MonoClassField *field)
3638 * mono_field_get_type:
3639 * @field: the MonoClassField to act on
3641 * Returns: MonoClass where the field was defined.
3644 mono_field_get_parent (MonoClassField *field)
3646 return field->parent;
3650 * mono_field_get_flags;
3651 * @field: the MonoClassField to act on
3653 * The metadata flags for a field are encoded using the
3654 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
3656 * Returns: the flags for the field.
3659 mono_field_get_flags (MonoClassField *field)
3661 return field->type->attrs;
3665 * mono_property_get_name:
3666 * @prop: the MonoProperty to act on
3668 * Returns: the name of the property
3671 mono_property_get_name (MonoProperty *prop)
3677 * mono_property_get_set_method
3678 * @prop: the MonoProperty to act on.
3680 * Returns: the setter method of the property (A MonoMethod)
3683 mono_property_get_set_method (MonoProperty *prop)
3689 * mono_property_get_get_method
3690 * @prop: the MonoProperty to act on.
3692 * Returns: the setter method of the property (A MonoMethod)
3695 mono_property_get_get_method (MonoProperty *prop)
3701 * mono_property_get_parent:
3702 * @prop: the MonoProperty to act on.
3704 * Returns: the MonoClass where the property was defined.
3707 mono_property_get_parent (MonoProperty *prop)
3709 return prop->parent;
3713 * mono_property_get_flags:
3714 * @prop: the MonoProperty to act on.
3716 * The metadata flags for a property are encoded using the
3717 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
3719 * Returns: the flags for the property.
3722 mono_property_get_flags (MonoProperty *prop)
3728 * mono_event_get_name:
3729 * @event: the MonoEvent to act on
3731 * Returns: the name of the event.
3734 mono_event_get_name (MonoEvent *event)
3740 * mono_event_get_add_method:
3741 * @event: The MonoEvent to act on.
3743 * Returns: the @add' method for the event (a MonoMethod).
3746 mono_event_get_add_method (MonoEvent *event)
3752 * mono_event_get_remove_method:
3753 * @event: The MonoEvent to act on.
3755 * Returns: the @remove method for the event (a MonoMethod).
3758 mono_event_get_remove_method (MonoEvent *event)
3760 return event->remove;
3764 * mono_event_get_raise_method:
3765 * @event: The MonoEvent to act on.
3767 * Returns: the @raise method for the event (a MonoMethod).
3770 mono_event_get_raise_method (MonoEvent *event)
3772 return event->raise;
3776 * mono_event_get_parent:
3777 * @event: the MonoEvent to act on.
3779 * Returns: the MonoClass where the event is defined.
3782 mono_event_get_parent (MonoEvent *event)
3784 return event->parent;
3788 * mono_event_get_flags
3789 * @event: the MonoEvent to act on.
3791 * The metadata flags for an event are encoded using the
3792 * EVENT_* constants. See the tabledefs.h file for details.
3794 * Returns: the flags for the event.
3797 mono_event_get_flags (MonoEvent *event)
3799 return event->attrs;
3803 * mono_find_method_by_name:
3804 * @klass: where to look for the method
3805 * @name_space: name of the method
3806 * @param_count: number of parameters. -1 for any number.
3808 * Obtains a MonoMethod with a given name and number of parameters.
3809 * It only works if there are no multiple signatures for any given method name.
3812 mono_class_get_method_from_name (MonoClass *klass, const char *name, int param_count)
3814 MonoMethod *res = NULL;
3817 mono_class_init (klass);
3819 for (i = 0; i < klass->method.count; ++i) {
3820 if (klass->methods [i]->name[0] == name [0] &&
3821 !strcmp (name, klass->methods [i]->name) &&
3822 (param_count == -1 || klass->methods [i]->signature->param_count == param_count)) {
3823 res = klass->methods [i];