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);
43 static void mono_class_create_generic_2 (MonoGenericClass *gclass);
45 void (*mono_debugger_start_class_init_func) (MonoClass *klass) = NULL;
46 void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
49 mono_class_from_typeref (MonoImage *image, guint32 type_token)
51 guint32 cols [MONO_TYPEREF_SIZE];
52 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
54 const char *name, *nspace;
56 MonoAssembly **references;
58 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
60 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
61 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
63 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
64 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
65 case MONO_RESOLTION_SCOPE_MODULE:
67 g_error ("null ResolutionScope not yet handled");
68 /* a typedef in disguise */
69 return mono_class_from_name (image, nspace, name);
70 case MONO_RESOLTION_SCOPE_MODULEREF:
71 return mono_class_from_name (image->modules [idx - 1], nspace, name);
72 case MONO_RESOLTION_SCOPE_TYPEREF: {
73 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
75 mono_class_init (enclosing);
76 for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
78 if (strcmp (res->name, name) == 0)
81 g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
84 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
88 references = image->references;
89 if (!references [idx-1])
90 mono_assembly_load_reference (image, idx - 1);
91 if (references [idx - 1] == (gpointer)-1)
94 image = references [idx-1]->image;
96 return mono_class_from_name (image, nspace, name);
100 dup_type (MonoType* t, const MonoType *original)
102 MonoType *r = g_new0 (MonoType, 1);
104 r->attrs = original->attrs;
105 r->byref = original->byref;
106 mono_stats.generics_metadata_size += sizeof (MonoType);
111 mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed,
112 gboolean include_ns, gboolean include_arity)
116 switch (type->type) {
117 case MONO_TYPE_ARRAY: {
118 int i, rank = type->data.array->rank;
120 mono_type_get_name_recurse (
121 &type->data.array->eklass->byval_arg, str,
122 FALSE, include_ns, include_arity);
123 g_string_append_c (str, '[');
124 for (i = 1; i < rank; i++)
125 g_string_append_c (str, ',');
126 g_string_append_c (str, ']');
129 case MONO_TYPE_SZARRAY:
130 mono_type_get_name_recurse (
131 &type->data.klass->byval_arg, str, FALSE, include_ns, include_arity);
132 g_string_append (str, "[]");
135 mono_type_get_name_recurse (type->data.type, str, FALSE, include_ns, include_arity);
136 g_string_append_c (str, '*');
139 klass = mono_class_from_mono_type (type);
140 if (klass->nested_in) {
141 mono_type_get_name_recurse (
142 &klass->nested_in->byval_arg, str, TRUE, include_ns, include_arity);
143 g_string_append_c (str, '+');
145 if (include_ns && *klass->name_space) {
146 g_string_append (str, klass->name_space);
147 g_string_append_c (str, '.');
149 if (!include_arity) {
150 char *s = strchr (klass->name, '`');
151 int len = s ? s - klass->name : strlen (klass->name);
153 g_string_append_len (str, klass->name, len);
155 g_string_append (str, klass->name);
158 if (klass->generic_class) {
159 MonoGenericClass *gclass = klass->generic_class;
162 g_string_append_c (str, '[');
163 for (i = 0; i < gclass->inst->type_argc; i++) {
165 g_string_append_c (str, ',');
166 mono_type_get_name_recurse (
167 gclass->inst->type_argv [i], str, FALSE, include_ns, include_arity);
169 g_string_append_c (str, ']');
170 } else if (klass->generic_container) {
173 g_string_append_c (str, '[');
174 for (i = 0; i < klass->generic_container->type_argc; i++) {
176 g_string_append_c (str, ',');
177 g_string_append (str, klass->generic_container->type_params [i].name);
179 g_string_append_c (str, ']');
186 * mono_type_get_name:
189 * Returns: the string representation for type as required by System.Reflection.
190 * The inverse of mono_reflection_parse_type ().
193 _mono_type_get_name (MonoType *type, gboolean is_recursed, gboolean include_ns,
194 gboolean include_arity)
196 GString* result = g_string_new ("");
197 mono_type_get_name_recurse (type, result, is_recursed, include_ns, include_arity);
200 g_string_append_c (result, '&');
202 return g_string_free (result, FALSE);
206 mono_type_get_name (MonoType *type)
208 return _mono_type_get_name (type, TRUE, TRUE, TRUE);
212 mono_type_get_full_name (MonoType *type)
214 return _mono_type_get_name (type, FALSE, TRUE, TRUE);
218 mono_type_get_underlying_type (MonoType *type)
220 switch (type->type) {
221 case MONO_TYPE_VALUETYPE:
222 if (type->data.klass->enumtype)
223 return type->data.klass->enum_basetype;
225 case MONO_TYPE_GENERICINST:
226 return mono_type_get_underlying_type (&type->data.generic_class->container_class->byval_arg);
235 mono_class_is_open_constructed_type (MonoType *t)
241 case MONO_TYPE_SZARRAY:
242 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
243 case MONO_TYPE_ARRAY:
244 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
246 return mono_class_is_open_constructed_type (t->data.type);
247 case MONO_TYPE_GENERICINST: {
248 MonoGenericClass *gclass = t->data.generic_class;
251 if (mono_class_is_open_constructed_type (&gclass->container_class->byval_arg))
253 for (i = 0; i < gclass->inst->type_argc; i++)
254 if (mono_class_is_open_constructed_type (gclass->inst->type_argv [i]))
263 static MonoGenericClass *
264 inflate_generic_class (MonoGenericClass *ogclass, MonoGenericContext *context)
266 MonoGenericClass *ngclass, *cached;
268 if (ogclass->is_dynamic) {
269 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
270 ngclass = &dgclass->generic_class;
271 ngclass->is_dynamic = 1;
273 ngclass = g_new0 (MonoGenericClass, 1);
277 ngclass->inst = mono_metadata_inflate_generic_inst (ogclass->inst, context);
279 ngclass->klass = NULL;
281 ngclass->context = g_new0 (MonoGenericContext, 1);
282 ngclass->context->container = context->container;
283 ngclass->context->gclass = ngclass;
285 ngclass->initialized = FALSE;
288 cached = mono_metadata_lookup_generic_class (ngclass);
289 mono_loader_unlock ();
295 mono_class_create_generic (ngclass);
301 inflate_generic_type (MonoType *type, MonoGenericContext *context)
303 switch (type->type) {
305 if (context->gmethod && context->gmethod->inst->type_argv)
307 context->gmethod->inst->type_argv [type->data.generic_param->num],
314 context->gclass->inst->type_argv [type->data.generic_param->num],
318 case MONO_TYPE_SZARRAY: {
319 MonoClass *eclass = type->data.klass;
320 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
323 nt = dup_type (type, type);
324 nt->data.klass = mono_class_from_mono_type (inflated);
327 case MONO_TYPE_GENERICINST: {
328 MonoGenericClass *ngclass = inflate_generic_class (type->data.generic_class, context);
329 MonoType *nt = dup_type (type, type);
330 nt->data.generic_class = ngclass;
333 case MONO_TYPE_CLASS:
334 case MONO_TYPE_VALUETYPE: {
335 MonoClass *klass = type->data.klass;
336 MonoGenericClass *gclass;
339 if (!klass->generic_container)
342 gclass = inflate_generic_class (klass->generic_container->context.gclass, context);
344 nt = dup_type (type, type);
345 nt->type = MONO_TYPE_GENERICINST;
346 nt->data.generic_class = gclass;
356 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
358 MonoType *inflated = inflate_generic_type (type, context);
361 return dup_type (type, type);
363 mono_stats.inflated_type_count++;
368 mono_class_inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
370 MonoMethodSignature *res;
377 res = mono_metadata_signature_alloc (image, sig->param_count);
378 res->ret = mono_class_inflate_generic_type (sig->ret, context);
379 is_open = mono_class_is_open_constructed_type (res->ret);
380 for (i = 0; i < sig->param_count; ++i) {
381 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
383 is_open = mono_class_is_open_constructed_type (res->params [i]);
385 res->hasthis = sig->hasthis;
386 res->explicit_this = sig->explicit_this;
387 res->call_convention = sig->call_convention;
388 res->generic_param_count = sig->generic_param_count;
389 res->sentinelpos = sig->sentinelpos;
390 res->has_type_parameters = is_open;
391 res->is_inflated = 1;
395 static MonoMethodHeader*
396 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
398 MonoMethodHeader *res;
400 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
401 res->code = header->code;
402 res->code_size = header->code_size;
403 res->max_stack = header->max_stack;
404 res->num_clauses = header->num_clauses;
405 res->init_locals = header->init_locals;
406 res->num_locals = header->num_locals;
407 res->clauses = header->clauses;
408 for (i = 0; i < header->num_locals; ++i)
409 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
413 static MonoGenericContext *
414 inflate_generic_context (MonoGenericContext *context, MonoGenericContext *inflate_with)
416 MonoGenericContext *res = g_new0 (MonoGenericContext, 1);
418 res->container = context->container;
421 res->gclass = inflate_generic_class (context->gclass, inflate_with);
423 if (context->gmethod) {
424 res->gmethod = g_new0 (MonoGenericMethod, 1);
426 res->gmethod->container = context->gmethod->container;
427 res->gmethod->inst = mono_metadata_inflate_generic_inst (context->gmethod->inst, inflate_with);
429 res->gmethod = inflate_with->gmethod;
435 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context,
438 MonoMethodInflated *result;
441 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
442 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
445 if (method->is_inflated || method->signature->is_inflated) {
446 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
448 context = inflate_generic_context (imethod->context, context);
449 method = imethod->declaring;
452 mono_stats.inflated_method_count++;
454 result = g_new0 (MonoMethodInflated, 1);
455 result->nmethod = *(MonoMethodNormal*)method;
456 result->nmethod.method.is_inflated = 1;
457 result->context = context;
458 result->declaring = method;
460 if (result->nmethod.method.klass->generic_class)
461 result->nmethod.method.klass = result->nmethod.method.klass->generic_class->container_class;
463 return (MonoMethod *) result;
467 mono_get_inflated_method (MonoMethod *method)
469 MonoMethodInflated *imethod, *res;
470 MonoMethodHeader *mh;
474 if (!method->is_inflated)
477 imethod = (MonoMethodInflated *) method;
478 if (imethod->inflated)
479 return (MonoMethod *) imethod->inflated;
481 mono_stats.inflated_method_count_2++;
482 mono_stats.generics_metadata_size +=
483 sizeof (MonoMethodInflated) - sizeof (MonoMethodNormal);
485 res = g_new0 (MonoMethodInflated, 1);
487 res->inflated = imethod->inflated = res;
489 mh = mono_method_get_header (method);
491 res->nmethod.header = inflate_generic_header (mh, imethod->context);
493 dtype = mono_class_inflate_generic_type (&method->klass->byval_arg, imethod->context);
494 rklass = res->nmethod.method.klass = mono_class_from_mono_type (dtype);
496 res->nmethod.method.signature = mono_class_inflate_generic_signature (
497 method->klass->image, method->signature, imethod->context);
499 return (MonoMethod *) res;
503 * class_compute_field_layout:
504 * @m: pointer to the metadata.
505 * @class: The class to initialize
507 * Initializes the class->fields.
509 * Currently we only support AUTO_LAYOUT, and do not even try to do
510 * a good job at it. This is temporary to get the code for Paolo.
513 class_compute_field_layout (MonoClass *class)
515 MonoImage *m = class->image;
516 const int top = class->field.count;
517 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
518 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
519 int i, blittable = TRUE, real_size = 0;
521 guint32 packing_size = 0;
522 gboolean explicit_size;
523 MonoClassField *field;
525 if (class->size_inited)
529 if (!class->parent->size_inited)
530 class_compute_field_layout (class->parent);
531 class->instance_size += class->parent->instance_size;
532 class->min_align = class->parent->min_align;
533 blittable = class->parent->blittable;
535 class->instance_size = sizeof (MonoObject);
536 class->min_align = 1;
539 /* Get the real size */
540 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
543 g_assert ((packing_size & 0xfffffff0) == 0);
544 class->packing_size = packing_size;
545 real_size += class->instance_size;
549 if (explicit_size && real_size) {
550 class->instance_size = MAX (real_size, class->instance_size);
552 class->size_inited = 1;
553 class->blittable = blittable;
557 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT)
560 class->fields = g_new0 (MonoClassField, top);
563 * Fetch all the field information.
565 for (i = 0; i < top; i++){
567 guint32 cols [MONO_FIELD_SIZE];
568 int idx = class->field.first + i;
569 MonoGenericContainer *container = NULL;
571 field = &class->fields [i];
572 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
573 /* The name is needed for fieldrefs */
574 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
575 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
576 mono_metadata_decode_value (sig, &sig);
577 /* FIELD signature == 0x06 */
578 g_assert (*sig == 0x06);
579 if (class->generic_container)
580 container = class->generic_container;
581 else if (class->generic_class) {
582 container = class->generic_class->container_class->generic_container;
583 g_assert (container);
585 field->type = mono_metadata_parse_type_full (
586 m, (MonoGenericContext *) container, MONO_PARSE_FIELD,
587 cols [MONO_FIELD_FLAGS], sig + 1, &sig);
588 if (mono_field_is_deleted (field))
590 if (class->generic_class) {
591 field->type = mono_class_inflate_generic_type (
592 field->type, class->generic_class->context);
593 field->type->attrs = cols [MONO_FIELD_FLAGS];
596 field->parent = class;
598 /* Only do these checks if we still think this type is blittable */
599 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
600 if (field->type->byref || MONO_TYPE_IS_REFERENCE (field->type)) {
603 MonoClass *field_class = mono_class_from_mono_type (field->type);
604 if (!field_class || !field_class->blittable)
609 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
610 mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
611 if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
612 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, field->name);
615 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
616 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
618 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
619 field->data = mono_image_rva_map (class->image, rva);
622 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
623 class->enum_basetype = field->type;
624 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
625 blittable = class->element_class->blittable;
628 /* The def_value of fields is compute lazily during vtable creation */
631 if (class == mono_defaults.string_class)
634 class->blittable = blittable;
636 if (class->enumtype && !class->enum_basetype) {
637 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
640 if (explicit_size && real_size) {
641 class->instance_size = MAX (real_size, class->instance_size);
644 if (class->generic_container ||
645 (class->generic_class && class->generic_class->inst->is_open))
648 mono_class_layout_fields (class);
652 mono_class_layout_fields (MonoClass *class)
655 const int top = class->field.count;
656 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
657 guint32 pass, passes, real_size;
658 gboolean gc_aware_layout = FALSE;
659 MonoClassField *field;
662 * Enable GC aware auto layout: in this mode, reference
663 * fields are grouped together inside objects, increasing collector
665 * Requires that all classes whose layout is known to native code be annotated
666 * with [StructLayout (LayoutKind.Sequential)]
667 * Value types have gc_aware_layout disabled by default, as per
668 * what the default is for other runtimes.
670 /* corlib is missing [StructLayout] directives in many places */
671 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
672 if (class->image != mono_defaults.corlib &&
673 class->byval_arg.type != MONO_TYPE_VALUETYPE)
674 gc_aware_layout = TRUE;
678 * Compute field layout and total size (not considering static fields)
682 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
683 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
690 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
694 real_size = class->parent->instance_size;
696 real_size = sizeof (MonoObject);
698 for (pass = 0; pass < passes; ++pass) {
699 for (i = 0; i < top; i++){
701 field = &class->fields [i];
703 if (mono_field_is_deleted (field))
705 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
708 if (gc_aware_layout) {
710 * We process fields with reference type in the first pass,
711 * and fields with non-reference type in the second pass.
712 * We use IS_POINTER instead of IS_REFERENCE because in
713 * some internal structures, we store GC_MALLOCed memory
714 * in IntPtr fields...
716 if (MONO_TYPE_IS_POINTER (field->type)) {
725 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
726 (strcmp (field->name, "$PRIVATE$") == 0)) {
727 /* This field is a hack inserted by MCS to empty structures */
731 size = mono_type_size (field->type, &align);
733 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
734 align = class->packing_size ? MIN (class->packing_size, align): align;
735 class->min_align = MAX (align, class->min_align);
736 field->offset = real_size;
737 field->offset += align - 1;
738 field->offset &= ~(align - 1);
739 real_size = field->offset + size;
742 class->instance_size = MAX (real_size, class->instance_size);
744 if (class->instance_size & (class->min_align - 1)) {
745 class->instance_size += class->min_align - 1;
746 class->instance_size &= ~(class->min_align - 1);
750 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
752 for (i = 0; i < top; i++) {
754 field = &class->fields [i];
757 * There must be info about all the fields in a type if it
758 * uses explicit layout.
761 if (mono_field_is_deleted (field))
763 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
766 size = mono_type_size (field->type, &align);
769 * When we get here, field->offset is already set by the
770 * loader (for either runtime fields or fields loaded from metadata).
771 * The offset is from the start of the object: this works for both
772 * classes and valuetypes.
774 field->offset += sizeof (MonoObject);
779 real_size = MAX (real_size, size + field->offset);
781 class->instance_size = MAX (real_size, class->instance_size);
785 class->size_inited = 1;
788 * Compute static field layout and size
790 for (i = 0; i < top; i++){
792 field = &class->fields [i];
794 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
796 if (mono_field_is_deleted (field))
799 size = mono_type_size (field->type, &align);
800 field->offset = class->class_size;
801 field->offset += align - 1;
802 field->offset &= ~(align - 1);
803 class->class_size = field->offset + size;
808 init_properties (MonoClass *class)
810 guint startm, endm, i, j;
811 guint32 cols [MONO_PROPERTY_SIZE];
812 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
813 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
815 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
816 class->property.count = class->property.last - class->property.first;
818 class->properties = g_new0 (MonoProperty, class->property.count);
819 for (i = class->property.first; i < class->property.last; ++i) {
820 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
821 class->properties [i - class->property.first].parent = class;
822 class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
823 class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
825 startm = mono_metadata_methods_from_property (class->image, i, &endm);
826 for (j = startm; j < endm; ++j) {
827 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
828 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
829 case METHOD_SEMANTIC_SETTER:
830 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
832 case METHOD_SEMANTIC_GETTER:
833 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
843 init_events (MonoClass *class)
845 guint startm, endm, i, j;
846 guint32 cols [MONO_EVENT_SIZE];
847 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
848 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
850 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
851 class->event.count = class->event.last - class->event.first;
853 class->events = g_new0 (MonoEvent, class->event.count);
854 for (i = class->event.first; i < class->event.last; ++i) {
855 MonoEvent *event = &class->events [i - class->event.first];
857 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
858 event->parent = class;
859 event->attrs = cols [MONO_EVENT_FLAGS];
860 event->name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
862 startm = mono_metadata_methods_from_event (class->image, i, &endm);
863 for (j = startm; j < endm; ++j) {
864 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
865 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
866 case METHOD_SEMANTIC_ADD_ON:
867 event->add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
869 case METHOD_SEMANTIC_REMOVE_ON:
870 event->remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
872 case METHOD_SEMANTIC_FIRE:
873 event->raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
875 case METHOD_SEMANTIC_OTHER: {
878 if (event->other == NULL) {
879 event->other = g_new0 (MonoMethod*, 1);
881 while (event->other [n])
883 event->other = g_realloc (event->other, (n + 1) * sizeof (MonoMethod*));
885 event->other [n] = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
896 mono_get_unique_iid (MonoClass *class)
898 static GHashTable *iid_hash = NULL;
899 static guint iid = 0;
904 g_assert (MONO_CLASS_IS_INTERFACE (class));
909 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
911 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
913 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
914 mono_loader_unlock ();
918 g_hash_table_insert (iid_hash, str, (gpointer)iid);
922 mono_loader_unlock ();
928 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
933 for (i = 0; i < klass->interface_count; i++) {
934 ic = klass->interfaces [i];
937 *res = g_ptr_array_new ();
938 g_ptr_array_add (*res, ic);
939 mono_class_init (ic);
941 collect_implemented_interfaces_aux (ic, res);
946 mono_class_get_implemented_interfaces (MonoClass *klass)
948 GPtrArray *res = NULL;
950 collect_implemented_interfaces_aux (klass, &res);
955 setup_interface_offsets (MonoClass *class, int cur_slot)
961 /* compute maximum number of slots and maximum interface id */
963 for (k = class; k ; k = k->parent) {
964 for (i = 0; i < k->interface_count; i++) {
965 ic = k->interfaces [i];
968 mono_class_init (ic);
970 if (max_iid < ic->interface_id)
971 max_iid = ic->interface_id;
975 if (MONO_CLASS_IS_INTERFACE (class)) {
976 if (max_iid < class->interface_id)
977 max_iid = class->interface_id;
979 class->max_interface_id = max_iid;
980 /* compute vtable offset for interfaces */
981 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
983 for (i = 0; i <= max_iid; i++)
984 class->interface_offsets [i] = -1;
986 ifaces = mono_class_get_implemented_interfaces (class);
988 for (i = 0; i < ifaces->len; ++i) {
989 ic = g_ptr_array_index (ifaces, i);
990 class->interface_offsets [ic->interface_id] = cur_slot;
991 cur_slot += ic->method.count;
993 g_ptr_array_free (ifaces, TRUE);
996 for (k = class->parent; k ; k = k->parent) {
997 ifaces = mono_class_get_implemented_interfaces (k);
999 for (i = 0; i < ifaces->len; ++i) {
1000 ic = g_ptr_array_index (ifaces, i);
1002 if (class->interface_offsets [ic->interface_id] == -1) {
1003 int io = k->interface_offsets [ic->interface_id];
1007 class->interface_offsets [ic->interface_id] = io;
1010 g_ptr_array_free (ifaces, TRUE);
1014 if (MONO_CLASS_IS_INTERFACE (class))
1015 class->interface_offsets [class->interface_id] = cur_slot;
1021 mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
1024 MonoMethod **vtable;
1025 int i, max_vtsize = 0, max_iid, cur_slot = 0;
1027 GHashTable *override_map = NULL;
1029 /* setup_vtable() must be called only once on the type */
1030 if (class->interface_offsets) {
1031 g_warning ("vtable already computed in %s.%s", class->name_space, class->name);
1035 ifaces = mono_class_get_implemented_interfaces (class);
1037 for (i = 0; i < ifaces->len; i++) {
1038 MonoClass *ic = g_ptr_array_index (ifaces, i);
1039 max_vtsize += ic->method.count;
1041 g_ptr_array_free (ifaces, TRUE);
1044 if (class->parent) {
1045 mono_class_init (class->parent);
1046 max_vtsize += class->parent->vtable_size;
1047 cur_slot = class->parent->vtable_size;
1050 max_vtsize += class->method.count;
1052 vtable = alloca (sizeof (gpointer) * max_vtsize);
1053 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
1055 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
1057 cur_slot = setup_interface_offsets (class, cur_slot);
1058 max_iid = class->max_interface_id;
1060 if (class->parent && class->parent->vtable_size)
1061 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
1063 /* override interface methods */
1064 for (i = 0; i < onum; i++) {
1065 MonoMethod *decl = overrides [i*2];
1066 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
1068 g_assert (decl->slot != -1);
1069 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
1070 vtable [dslot] = overrides [i*2 + 1];
1071 vtable [dslot]->slot = dslot;
1073 override_map = g_hash_table_new (NULL, NULL);
1075 g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
1079 for (k = class; k ; k = k->parent) {
1081 ifaces = mono_class_get_implemented_interfaces (k);
1083 nifaces = ifaces->len;
1084 for (i = 0; i < nifaces; i++) {
1087 ic = g_ptr_array_index (ifaces, i);
1088 io = k->interface_offsets [ic->interface_id];
1091 g_assert (io <= max_vtsize);
1094 for (l = 0; l < ic->method.count; l++) {
1095 MonoMethod *im = ic->methods [l];
1097 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1100 for (j = 0; j < class->method.count; ++j) {
1101 MonoMethod *cm = class->methods [j];
1102 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1103 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
1104 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
1106 if (!strcmp(cm->name, im->name) &&
1107 mono_metadata_signature_equal (cm->signature, im->signature)) {
1108 g_assert (io + l <= max_vtsize);
1109 vtable [io + l] = cm;
1114 /* already implemented */
1115 if (io >= k->vtable_size)
1119 for (l = 0; l < ic->method.count; l++) {
1120 MonoMethod *im = ic->methods [l];
1123 g_assert (io + l <= max_vtsize);
1125 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1128 for (k1 = class; k1; k1 = k1->parent) {
1129 for (j = 0; j < k1->method.count; ++j) {
1130 MonoMethod *cm = k1->methods [j];
1132 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1133 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
1136 if (!strcmp(cm->name, im->name) &&
1137 mono_metadata_signature_equal (cm->signature, im->signature)) {
1138 g_assert (io + l <= max_vtsize);
1139 vtable [io + l] = cm;
1144 g_assert (io + l <= max_vtsize);
1145 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1150 for (l = 0; l < ic->method.count; l++) {
1151 MonoMethod *im = ic->methods [l];
1152 char *qname, *fqname, *cname, *the_cname;
1155 if (vtable [io + l])
1158 if (ic->generic_class) {
1159 MonoClass *the_ic = ic->generic_class->container_class;
1160 the_cname = _mono_type_get_name (&the_ic->byval_arg, TRUE, FALSE, TRUE);
1164 cname = (char*)ic->name;
1167 qname = g_strconcat (cname, ".", im->name, NULL);
1168 if (ic->name_space && ic->name_space [0])
1169 fqname = g_strconcat (ic->name_space, ".", cname, ".", im->name, NULL);
1173 for (k1 = class; k1; k1 = k1->parent) {
1174 for (j = 0; j < k1->method.count; ++j) {
1175 MonoMethod *cm = k1->methods [j];
1177 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1180 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
1181 mono_metadata_signature_equal (cm->signature, im->signature)) {
1182 g_assert (io + l <= max_vtsize);
1183 vtable [io + l] = cm;
1194 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
1195 for (l = 0; l < ic->method.count; l++) {
1197 MonoMethod *im = ic->methods [l];
1198 if (im->flags & METHOD_ATTRIBUTE_STATIC)
1200 g_assert (io + l <= max_vtsize);
1203 * If one of our parents already implements this interface
1204 * we can inherit the implementation.
1206 if (!(vtable [io + l])) {
1207 MonoClass *parent = class->parent;
1209 if ((ic->interface_id <= parent->max_interface_id) &&
1210 (parent->interface_offsets [ic->interface_id]) &&
1212 vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
1215 if (!(vtable [io + l])) {
1216 for (j = 0; j < onum; ++j) {
1217 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
1218 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
1220 msig = mono_signature_get_desc (im->signature, FALSE);
1221 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
1222 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
1224 for (j = 0; j < class->method.count; ++j) {
1225 MonoMethod *cm = class->methods [j];
1226 msig = mono_signature_get_desc (cm->signature, FALSE);
1228 printf ("METHOD %s(%s)\n", cm->name, msig);
1231 g_assert_not_reached ();
1236 for (l = 0; l < ic->method.count; l++) {
1237 MonoMethod *im = vtable [io + l];
1240 g_assert (io + l <= max_vtsize);
1242 /* FIXME: why do we need this ? */
1244 /* g_assert_not_reached (); */
1250 g_ptr_array_free (ifaces, TRUE);
1253 for (i = 0; i < class->method.count; ++i) {
1256 cm = class->methods [i];
1259 * Non-virtual method have no place in the vtable.
1260 * This also catches static methods (since they are not virtual).
1262 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1266 * If the method is REUSE_SLOT, we must check in the
1267 * base class for a method to override.
1269 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
1271 for (k = class->parent; k ; k = k->parent) {
1273 for (j = 0; j < k->method.count; ++j) {
1274 MonoMethod *m1 = k->methods [j];
1275 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
1277 if (!strcmp(cm->name, m1->name) &&
1278 mono_metadata_signature_equal (cm->signature, m1->signature)) {
1279 slot = k->methods [j]->slot;
1280 g_assert (cm->slot < max_vtsize);
1282 override_map = g_hash_table_new (NULL, NULL);
1283 g_hash_table_insert (override_map, m1, cm);
1295 cm->slot = cur_slot++;
1297 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT) && !cm->signature->generic_param_count)
1298 vtable [cm->slot] = cm;
1301 /* override non interface methods */
1302 for (i = 0; i < onum; i++) {
1303 MonoMethod *decl = overrides [i*2];
1304 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
1305 g_assert (decl->slot != -1);
1306 vtable [decl->slot] = overrides [i*2 + 1];
1307 overrides [i * 2 + 1]->slot = decl->slot;
1309 override_map = g_hash_table_new (NULL, NULL);
1310 g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
1315 * If a method occupies more than one place in the vtable, and it is
1316 * overriden, then change the other occurances too.
1319 for (i = 0; i < max_vtsize; ++i)
1321 MonoMethod *cm = g_hash_table_lookup (override_map, vtable [i]);
1326 g_hash_table_destroy (override_map);
1329 if (class->generic_class) {
1330 MonoClass *gklass = class->generic_class->container_class;
1332 mono_class_init (gklass);
1333 class->vtable_size = gklass->vtable_size;
1335 class->vtable_size = cur_slot;
1337 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
1338 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
1340 if (mono_print_vtable) {
1343 for (i = 0; i <= max_iid; i++)
1344 if (class->interface_offsets [i] != -1)
1347 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
1348 class->name, class->vtable_size, icount);
1350 for (i = 0; i < class->vtable_size; ++i) {
1355 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
1356 cm->klass->name_space, cm->klass->name,
1363 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
1364 class->name, max_iid);
1366 for (i = 0; i < class->interface_count; i++) {
1367 ic = class->interfaces [i];
1368 printf (" slot %03d(%03d) %s.%s\n",
1369 class->interface_offsets [ic->interface_id],
1370 ic->method.count, ic->name_space, ic->name);
1373 for (k = class->parent; k ; k = k->parent) {
1374 for (i = 0; i < k->interface_count; i++) {
1375 ic = k->interfaces [i];
1376 printf (" slot %03d(%03d) %s.%s\n",
1377 class->interface_offsets [ic->interface_id],
1378 ic->method.count, ic->name_space, ic->name);
1387 * @class: the class to initialize
1389 * compute the instance_size, class_size and other infos that cannot be
1390 * computed at mono_class_get() time. Also compute a generic vtable and
1391 * the method slot numbers. We use this infos later to create a domain
1395 mono_class_init (MonoClass *class)
1398 static MonoMethod *default_ghc = NULL;
1399 static MonoMethod *default_finalize = NULL;
1400 static int finalize_slot = -1;
1401 static int ghc_slot = -1;
1402 MonoMethod **overrides;
1410 /*g_print ("Init class %s\n", class->name);*/
1412 /* We do everything inside the lock to prevent races */
1413 mono_loader_lock ();
1415 if (class->inited) {
1416 mono_loader_unlock ();
1417 /* Somebody might have gotten in before us */
1421 if (class->init_pending) {
1422 /* this indicates a cyclic dependency */
1423 g_error ("pending init %s.%s\n", class->name_space, class->name);
1426 class->init_pending = 1;
1428 if (mono_debugger_start_class_init_func)
1429 mono_debugger_start_class_init_func (class);
1431 mono_stats.initialized_class_count++;
1433 if (class->generic_class && !class->generic_class->is_dynamic) {
1434 MonoGenericClass *gclass = class->generic_class;
1435 MonoClass *gklass = gclass->container_class;
1437 mono_stats.generic_class_count++;
1439 class->method = gklass->method;
1440 class->field = gklass->field;
1442 mono_class_create_generic_2 (gclass);
1444 mono_class_init (gklass);
1446 if (MONO_CLASS_IS_INTERFACE (class))
1447 class->interface_id = mono_get_unique_iid (class);
1449 g_assert (class->method.count == gklass->method.count);
1450 class->methods = g_new0 (MonoMethod *, class->method.count);
1452 for (i = 0; i < class->method.count; i++) {
1453 MonoMethod *inflated = mono_class_inflate_generic_method (
1454 gklass->methods [i], gclass->context, gclass->klass);
1456 class->methods [i] = mono_get_inflated_method (inflated);
1459 g_assert (class->field.count == gklass->field.count);
1460 class->fields = g_new0 (MonoClassField, class->field.count);
1462 for (i = 0; i < class->field.count; i++) {
1463 MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
1464 ifield->generic_type = gklass->fields [i].type;
1466 class->fields [i] = gklass->fields [i];
1467 class->fields [i].generic_info = ifield;
1468 class->fields [i].parent = class;
1469 class->fields [i].type = mono_class_inflate_generic_type (
1470 class->fields [i].type, gclass->context);
1473 class->property = gklass->property;
1474 class->properties = g_new0 (MonoProperty, class->property.count);
1476 for (i = 0; i < class->property.count; i++) {
1477 MonoProperty *prop = &class->properties [i];
1479 *prop = gklass->properties [i];
1482 prop->get = mono_class_inflate_generic_method (
1483 prop->get, gclass->context, gclass->klass);
1485 prop->set = mono_class_inflate_generic_method (
1486 prop->set, gclass->context, gclass->klass);
1488 prop->parent = class;
1491 g_assert (class->interface_count == gklass->interface_count);
1494 if (class->parent && !class->parent->inited)
1495 mono_class_init (class->parent);
1498 * Computes the size used by the fields, and their locations
1500 if (!class->size_inited)
1501 class_compute_field_layout (class);
1503 /* initialize method pointers */
1506 MonoMethodSignature *sig;
1507 class->method.count = class->rank > 1? 2: 1;
1508 sig = mono_metadata_signature_alloc (class->image, class->rank);
1509 sig->ret = &mono_defaults.void_class->byval_arg;
1510 sig->pinvoke = TRUE;
1511 for (i = 0; i < class->rank; ++i)
1512 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1514 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1515 ctor->klass = class;
1516 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1517 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1518 ctor->signature = sig;
1519 ctor->name = ".ctor";
1521 class->methods = g_new (MonoMethod*, class->method.count);
1522 class->methods [0] = ctor;
1523 if (class->rank > 1) {
1524 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
1525 sig->ret = &mono_defaults.void_class->byval_arg;
1526 sig->pinvoke = TRUE;
1527 for (i = 0; i < class->rank * 2; ++i)
1528 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1530 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1531 ctor->klass = class;
1532 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1533 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1534 ctor->signature = sig;
1535 ctor->name = ".ctor";
1537 class->methods [1] = ctor;
1540 if (!class->generic_class && !class->methods) {
1541 class->methods = g_new (MonoMethod*, class->method.count);
1542 for (i = 0; i < class->method.count; ++i) {
1543 class->methods [i] = mono_get_method (class->image,
1544 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
1549 if (!class->generic_class) {
1550 init_properties (class);
1551 init_events (class);
1553 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
1556 guint32 cols [MONO_NESTED_CLASS_SIZE];
1557 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
1558 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
1559 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
1561 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
1565 mono_class_setup_supertypes (class);
1567 if (MONO_CLASS_IS_INTERFACE (class)) {
1568 for (i = 0; i < class->method.count; ++i)
1569 class->methods [i]->slot = i;
1570 class->init_pending = 0;
1573 * class->interface_offsets is needed for the castclass/isinst code, so
1574 * we have to setup them for interfaces, too.
1576 setup_interface_offsets (class, 0);
1577 mono_loader_unlock ();
1579 if (mono_debugger_class_init_func)
1580 mono_debugger_class_init_func (class);
1585 overrides = mono_class_get_overrides (class->image, class->type_token, &onum);
1586 mono_class_setup_vtable (class, overrides, onum);
1590 class->init_pending = 0;
1593 if (class == mono_defaults.object_class) {
1595 for (i = 0; i < class->vtable_size; ++i) {
1596 MonoMethod *cm = class->vtable [i];
1598 if (!strcmp (cm->name, "GetHashCode")) {
1604 g_assert (ghc_slot > 0);
1606 default_ghc = class->vtable [ghc_slot];
1611 if (class->parent) {
1613 if (class->vtable [ghc_slot] == default_ghc) {
1618 if (!default_finalize) {
1619 if (class == mono_defaults.object_class) {
1621 for (i = 0; i < class->vtable_size; ++i) {
1622 MonoMethod *cm = class->vtable [i];
1624 if (!strcmp (cm->name, "Finalize")) {
1630 g_assert (finalize_slot > 0);
1632 default_finalize = class->vtable [finalize_slot];
1636 /* Object::Finalize should have empty implemenatation */
1637 class->has_finalize = 0;
1638 if (class->parent) {
1639 if (class->vtable [finalize_slot] != default_finalize)
1640 class->has_finalize = 1;
1643 mono_loader_unlock ();
1645 if (mono_debugger_class_init_func)
1646 mono_debugger_class_init_func (class);
1650 mono_class_setup_mono_type (MonoClass *class)
1652 const char *name = class->name;
1653 const char *nspace = class->name_space;
1655 if (MONO_CLASS_IS_INTERFACE (class))
1656 class->interface_id = mono_get_unique_iid (class);
1658 class->this_arg.byref = 1;
1659 class->this_arg.data.klass = class;
1660 class->this_arg.type = MONO_TYPE_CLASS;
1661 class->byval_arg.data.klass = class;
1662 class->byval_arg.type = MONO_TYPE_CLASS;
1664 if (!strcmp (nspace, "System")) {
1665 if (!strcmp (name, "ValueType")) {
1667 * do not set the valuetype bit for System.ValueType.
1668 * class->valuetype = 1;
1670 class->blittable = TRUE;
1671 } else if (!strcmp (name, "Enum")) {
1673 * do not set the valuetype bit for System.Enum.
1674 * class->valuetype = 1;
1676 class->valuetype = 0;
1677 class->enumtype = 0;
1678 } else if (!strcmp (name, "Object")) {
1679 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
1680 } else if (!strcmp (name, "String")) {
1681 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
1682 } else if (!strcmp (name, "TypedReference")) {
1683 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
1687 if (class->valuetype) {
1688 int t = MONO_TYPE_VALUETYPE;
1689 if (!strcmp (nspace, "System")) {
1692 if (!strcmp (name, "Boolean")) {
1693 t = MONO_TYPE_BOOLEAN;
1694 } else if (!strcmp(name, "Byte")) {
1696 class->blittable = TRUE;
1700 if (!strcmp (name, "Char")) {
1705 if (!strcmp (name, "Double")) {
1707 class->blittable = TRUE;
1711 if (!strcmp (name, "Int32")) {
1713 class->blittable = TRUE;
1714 } else if (!strcmp(name, "Int16")) {
1716 class->blittable = TRUE;
1717 } else if (!strcmp(name, "Int64")) {
1719 class->blittable = TRUE;
1720 } else if (!strcmp(name, "IntPtr")) {
1722 class->blittable = TRUE;
1726 if (!strcmp (name, "Single")) {
1728 class->blittable = TRUE;
1729 } else if (!strcmp(name, "SByte")) {
1731 class->blittable = TRUE;
1735 if (!strcmp (name, "UInt32")) {
1737 class->blittable = TRUE;
1738 } else if (!strcmp(name, "UInt16")) {
1740 class->blittable = TRUE;
1741 } else if (!strcmp(name, "UInt64")) {
1743 class->blittable = TRUE;
1744 } else if (!strcmp(name, "UIntPtr")) {
1746 class->blittable = TRUE;
1750 if (!strcmp (name, "TypedReference")) {
1751 t = MONO_TYPE_TYPEDBYREF;
1752 class->blittable = TRUE;
1756 if (!strcmp (name, "Void")) {
1764 class->this_arg.type = class->byval_arg.type = t;
1769 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
1771 gboolean system_namespace;
1773 system_namespace = !strcmp (class->name_space, "System");
1775 /* if root of the hierarchy */
1776 if (system_namespace && !strcmp (class->name, "Object")) {
1777 class->parent = NULL;
1778 class->instance_size = sizeof (MonoObject);
1781 if (!strcmp (class->name, "<Module>")) {
1782 class->parent = NULL;
1783 class->instance_size = 0;
1787 if (parent && parent->generic_class)
1788 mono_class_create_generic_2 (parent->generic_class);
1790 if (!MONO_CLASS_IS_INTERFACE (class)) {
1791 class->parent = parent;
1794 g_assert_not_reached (); /* FIXME */
1796 if (parent->generic_class && !parent->name) {
1798 * If the parent is a generic instance, we may get
1799 * called before it is fully initialized, especially
1800 * before it has its name.
1805 class->marshalbyref = parent->marshalbyref;
1806 class->contextbound = parent->contextbound;
1807 class->delegate = parent->delegate;
1809 if (system_namespace) {
1810 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
1811 class->marshalbyref = 1;
1813 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
1814 class->contextbound = 1;
1816 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
1817 class->delegate = 1;
1820 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
1821 (strcmp (class->parent->name_space, "System") == 0)))
1822 class->valuetype = 1;
1823 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
1824 class->valuetype = class->enumtype = 1;
1826 /*class->enumtype = class->parent->enumtype; */
1827 mono_class_setup_supertypes (class);
1829 class->parent = NULL;
1835 mono_class_setup_supertypes (MonoClass *class)
1840 if (class->supertypes)
1844 for (k = class; k ; k = k->parent) {
1848 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
1849 class->supertypes = g_new0 (MonoClass *, ms);
1851 if (class->parent) {
1852 for (i = class->idepth, k = class; k ; k = k->parent)
1853 class->supertypes [--i] = k;
1855 class->supertypes [0] = class;
1860 * If we inherit a type parameter from an outer class, set its owner to that class.
1863 set_generic_param_owner (MonoGenericContainer *container, MonoClass *klass, int pos)
1865 MonoGenericContainer *gc;
1868 if (klass->nested_in)
1869 pos = set_generic_param_owner (container, klass->nested_in, pos);
1871 if (!klass->generic_container)
1874 gc = klass->generic_container;
1875 for (i = pos; i < gc->type_argc; i++)
1876 container->type_params [i].owner = gc;
1878 return pos + gc->type_argc;
1881 static MonoGenericInst *
1882 get_shared_inst (MonoGenericContainer *container)
1884 MonoGenericInst *nginst;
1887 nginst = g_new0 (MonoGenericInst, 1);
1888 nginst->type_argc = container->type_argc;
1889 nginst->type_argv = g_new0 (MonoType *, nginst->type_argc);
1890 nginst->is_open = 1;
1892 for (i = 0; i < nginst->type_argc; i++) {
1893 MonoType *t = g_new0 (MonoType, 1);
1895 t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
1896 t->data.generic_param = &container->type_params [i];
1898 nginst->type_argv [i] = t;
1901 return mono_metadata_lookup_generic_inst (nginst);
1905 mono_get_shared_generic_class (MonoGenericContainer *container, gboolean is_dynamic)
1907 MonoGenericClass *gclass;
1910 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
1911 gclass = &dgclass->generic_class;
1912 gclass->is_dynamic = 1;
1914 gclass = g_new0 (MonoGenericClass, 1);
1916 gclass->context = &container->context;
1917 gclass->container_class = container->klass;
1918 gclass->inst = get_shared_inst (container);
1921 MonoGenericClass *cached = mono_metadata_lookup_generic_class (gclass);
1929 gclass->klass = container->klass;
1935 * @image: context where the image is created
1936 * @type_token: typedef token
1939 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
1941 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
1942 MonoClass *class, *parent = NULL;
1943 guint32 cols [MONO_TYPEDEF_SIZE];
1944 guint32 cols_next [MONO_TYPEDEF_SIZE];
1945 guint tidx = mono_metadata_token_index (type_token);
1946 MonoGenericContext *context = NULL;
1947 const char *name, *nspace;
1949 MonoClass **interfaces;
1951 mono_loader_lock ();
1953 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
1954 mono_loader_unlock ();
1958 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
1960 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
1962 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1963 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1965 class = g_malloc0 (sizeof (MonoClass));
1968 class->name_space = nspace;
1970 class->image = image;
1971 class->type_token = type_token;
1972 class->flags = cols [MONO_TYPEDEF_FLAGS];
1974 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
1976 class->generic_container = mono_metadata_load_generic_params (image, class->type_token, NULL);
1977 if (class->generic_container) {
1978 class->generic_container->klass = class;
1979 context = &class->generic_container->context;
1981 context->gclass = mono_get_shared_generic_class (context->container, FALSE);
1984 if (cols [MONO_TYPEDEF_EXTENDS])
1985 parent = mono_class_get_full (
1986 image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]), context);
1988 mono_class_setup_parent (class, parent);
1990 mono_class_setup_mono_type (class);
1992 interfaces = mono_metadata_interfaces_from_typedef_full (image, type_token, &icount, context);
1994 class->interfaces = interfaces;
1995 class->interface_count = icount;
1997 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
1999 /* fixme: maybe we must set this on windows
2000 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
2004 class->cast_class = class->element_class = class;
2006 /*g_print ("Load class %s\n", name);*/
2009 * Compute the field and method lists
2011 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
2012 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
2014 if (tt->rows > tidx){
2015 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
2016 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
2017 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
2019 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
2020 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
2023 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
2024 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
2025 class->field.count = class->field.last - class->field.first;
2027 class->field.count = 0;
2029 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
2030 class->method.count = class->method.last - class->method.first;
2032 class->method.count = 0;
2034 /* reserve space to store vector pointer in arrays */
2035 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
2036 class->instance_size += 2 * sizeof (gpointer);
2037 g_assert (class->field.count == 0);
2040 if (class->enumtype)
2041 class_compute_field_layout (class);
2043 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
2044 class->nested_in = mono_class_create_from_typedef (image, type_token);
2046 if (class->nested_in && class->generic_container)
2047 set_generic_param_owner (class->generic_container, class->nested_in, 0);
2049 mono_loader_unlock ();
2055 mono_class_create_generic (MonoGenericClass *gclass)
2057 MonoClass *klass, *gklass;
2060 gclass->klass = g_malloc0 (sizeof (MonoClass));
2061 klass = gclass->klass;
2063 gklass = gclass->container_class;
2065 klass->nested_in = gklass->nested_in;
2067 klass->name = gklass->name;
2068 klass->name_space = gklass->name_space;
2069 klass->image = gklass->image;
2070 klass->flags = gklass->flags;
2072 klass->generic_class = gclass;
2074 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
2075 klass->this_arg.data.generic_class = klass->byval_arg.data.generic_class = gclass;
2076 klass->this_arg.byref = TRUE;
2078 klass->cast_class = klass->element_class = klass;
2080 if (gclass->is_dynamic) {
2081 klass->instance_size = gklass->instance_size;
2082 klass->class_size = gklass->class_size;
2083 klass->size_inited = 1;
2086 klass->valuetype = gklass->valuetype;
2088 mono_class_setup_supertypes (klass);
2093 mono_class_create_generic_2 (MonoGenericClass *gclass)
2095 MonoClass *klass, *gklass;
2099 klass = gclass->klass;
2100 gklass = gclass->container_class;
2102 klass->interface_count = gklass->interface_count;
2103 klass->interfaces = g_new0 (MonoClass *, klass->interface_count);
2104 for (i = 0; i < klass->interface_count; i++) {
2105 MonoType *it = &gklass->interfaces [i]->byval_arg;
2106 MonoType *inflated = mono_class_inflate_generic_type (it, gclass->context);
2107 klass->interfaces [i] = mono_class_from_mono_type (inflated);
2110 for (list = gklass->nested_classes; list; list = list->next)
2111 klass->nested_classes = g_list_append (
2112 klass->nested_classes, list->data);
2115 klass->parent = mono_class_from_mono_type (gclass->parent);
2116 else if (gklass->parent) {
2117 MonoType *inflated = mono_class_inflate_generic_type (&gklass->parent->byval_arg, gclass->context);
2119 klass->parent = mono_class_from_mono_type (inflated);
2123 mono_class_setup_parent (klass, klass->parent);
2127 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
2129 MonoClass *klass, **ptr;
2133 return param->pklass;
2135 klass = param->pklass = g_new0 (MonoClass, 1);
2137 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
2141 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
2142 klass->parent = param->constraints [0];
2146 if (count - pos > 0) {
2147 klass->interface_count = count - pos;
2148 klass->interfaces = g_new0 (MonoClass *, count - pos);
2149 for (i = pos; i < count; i++)
2150 klass->interfaces [i - pos] = param->constraints [i];
2153 g_assert (param->name && param->owner);
2155 klass->name = param->name;
2156 klass->name_space = "";
2157 klass->image = image;
2158 klass->cast_class = klass->element_class = klass;
2159 klass->enum_basetype = &klass->element_class->byval_arg;
2160 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2162 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2163 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2164 klass->this_arg.byref = TRUE;
2166 mono_class_init (klass);
2172 my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
2177 return param->pklass;
2179 g_assert (param->owner);
2181 klass = g_new0 (MonoClass, 1);
2184 klass->name = param->name;
2186 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
2187 klass->name_space = "";
2188 klass->image = mono_defaults.corlib;
2189 klass->cast_class = klass->element_class = klass;
2190 klass->enum_basetype = &klass->element_class->byval_arg;
2191 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2193 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2194 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2195 klass->this_arg.byref = TRUE;
2197 mono_class_init (klass);
2203 mono_ptr_class_get (MonoType *type)
2206 MonoClass *el_class;
2207 static GHashTable *ptr_hash = NULL;
2209 mono_loader_lock ();
2212 ptr_hash = g_hash_table_new (NULL, NULL);
2213 el_class = mono_class_from_mono_type (type);
2214 if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
2215 mono_loader_unlock ();
2218 result = g_new0 (MonoClass, 1);
2220 result->parent = NULL; /* no parent for PTR types */
2221 result->name_space = el_class->name_space;
2222 result->name = g_strdup_printf ("%s*", el_class->name);
2223 result->image = el_class->image;
2224 result->inited = TRUE;
2225 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2226 /* Can pointers get boxed? */
2227 result->instance_size = sizeof (gpointer);
2228 result->cast_class = result->element_class = el_class;
2229 result->enum_basetype = &result->element_class->byval_arg;
2230 result->blittable = TRUE;
2232 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
2233 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
2234 result->this_arg.byref = TRUE;
2236 mono_class_setup_supertypes (result);
2238 g_hash_table_insert (ptr_hash, el_class, result);
2240 mono_loader_unlock ();
2246 mono_fnptr_class_get (MonoMethodSignature *sig)
2249 static GHashTable *ptr_hash = NULL;
2251 mono_loader_lock ();
2254 ptr_hash = g_hash_table_new (NULL, NULL);
2256 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
2257 mono_loader_unlock ();
2260 result = g_new0 (MonoClass, 1);
2262 result->parent = NULL; /* no parent for PTR types */
2263 result->name = "System";
2264 result->name_space = "MonoFNPtrFakeClass";
2265 result->image = NULL; /* need to fix... */
2266 result->inited = TRUE;
2267 result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
2268 /* Can pointers get boxed? */
2269 result->instance_size = sizeof (gpointer);
2270 result->cast_class = result->element_class = result;
2271 result->blittable = TRUE;
2273 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
2274 result->this_arg.data.method = result->byval_arg.data.method = sig;
2275 result->this_arg.byref = TRUE;
2276 result->enum_basetype = &result->element_class->byval_arg;
2277 result->blittable = TRUE;
2279 mono_class_setup_supertypes (result);
2281 g_hash_table_insert (ptr_hash, sig, result);
2283 mono_loader_unlock ();
2289 mono_class_from_mono_type (MonoType *type)
2291 switch (type->type) {
2292 case MONO_TYPE_OBJECT:
2293 return type->data.klass? type->data.klass: mono_defaults.object_class;
2294 case MONO_TYPE_VOID:
2295 return type->data.klass? type->data.klass: mono_defaults.void_class;
2296 case MONO_TYPE_BOOLEAN:
2297 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
2298 case MONO_TYPE_CHAR:
2299 return type->data.klass? type->data.klass: mono_defaults.char_class;
2301 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
2303 return type->data.klass? type->data.klass: mono_defaults.byte_class;
2305 return type->data.klass? type->data.klass: mono_defaults.int16_class;
2307 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
2309 return type->data.klass? type->data.klass: mono_defaults.int32_class;
2311 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
2313 return type->data.klass? type->data.klass: mono_defaults.int_class;
2315 return type->data.klass? type->data.klass: mono_defaults.uint_class;
2317 return type->data.klass? type->data.klass: mono_defaults.int64_class;
2319 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
2321 return type->data.klass? type->data.klass: mono_defaults.single_class;
2323 return type->data.klass? type->data.klass: mono_defaults.double_class;
2324 case MONO_TYPE_STRING:
2325 return type->data.klass? type->data.klass: mono_defaults.string_class;
2326 case MONO_TYPE_TYPEDBYREF:
2327 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
2328 case MONO_TYPE_ARRAY:
2329 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
2331 return mono_ptr_class_get (type->data.type);
2332 case MONO_TYPE_FNPTR:
2333 return mono_fnptr_class_get (type->data.method);
2334 case MONO_TYPE_SZARRAY:
2335 return mono_array_class_get (type->data.klass, 1);
2336 case MONO_TYPE_CLASS:
2337 case MONO_TYPE_VALUETYPE:
2338 return type->data.klass;
2339 case MONO_TYPE_GENERICINST:
2340 g_assert (type->data.generic_class->klass);
2341 return type->data.generic_class->klass;
2343 return my_mono_class_from_generic_parameter (type->data.generic_param, FALSE);
2344 case MONO_TYPE_MVAR:
2345 return my_mono_class_from_generic_parameter (type->data.generic_param, TRUE);
2347 g_warning ("implement me 0x%02x\n", type->type);
2348 g_assert_not_reached ();
2355 * @image: context where the image is created
2356 * @type_spec: typespec token
2359 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec,
2360 MonoGenericContext *context)
2362 MonoType *type, *inflated;
2365 type = mono_type_create_from_typespec_full (image, context, type_spec);
2367 switch (type->type) {
2368 case MONO_TYPE_ARRAY:
2369 class = mono_array_class_get (type->data.array->eklass, type->data.array->rank);
2371 case MONO_TYPE_SZARRAY:
2372 class = mono_array_class_get (type->data.klass, 1);
2375 class = mono_ptr_class_get (type->data.type);
2377 case MONO_TYPE_GENERICINST:
2378 g_assert (type->data.generic_class->klass);
2379 class = type->data.generic_class->klass;
2382 /* it seems any type can be stored in TypeSpec as well */
2383 class = mono_class_from_mono_type (type);
2387 if (!class || !context || (!context->gclass && !context->gmethod))
2390 inflated = mono_class_inflate_generic_type (&class->byval_arg, context);
2392 return mono_class_from_mono_type (inflated);
2396 * mono_bounded_array_class_get:
2397 * @element_class: element class
2398 * @rank: the dimension of the array class
2399 * @bounded: whenever the array has non-zero bounds
2401 * Returns: a class object describing the array with element type @element_type and
2405 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
2409 MonoClass *parent = NULL;
2410 GSList *list, *rootlist;
2413 gboolean corlib_type = FALSE;
2415 g_assert (rank <= 255);
2418 /* bounded only matters for one-dimensional arrays */
2421 image = eclass->image;
2423 mono_loader_lock ();
2425 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
2426 for (; list; list = list->next) {
2428 if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
2429 mono_loader_unlock ();
2435 /* for the building corlib use System.Array from it */
2436 if (image->assembly && image->assembly->dynamic && strcmp (image->assembly_name, "mscorlib") == 0) {
2437 parent = mono_class_from_name (image, "System", "Array");
2440 parent = mono_defaults.array_class;
2441 if (!parent->inited)
2442 mono_class_init (parent);
2445 class = g_malloc0 (sizeof (MonoClass));
2447 class->image = image;
2448 class->name_space = eclass->name_space;
2449 nsize = strlen (eclass->name);
2450 name = g_malloc (nsize + 2 + rank);
2451 memcpy (name, eclass->name, nsize);
2454 memset (name + nsize + 1, ',', rank - 1);
2455 name [nsize + rank] = ']';
2456 name [nsize + rank + 1] = 0;
2458 class->type_token = 0;
2459 /* all arrays are marked serializable and sealed, bug #42779 */
2460 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
2461 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2462 class->parent = parent;
2463 class->instance_size = mono_class_instance_size (class->parent);
2464 class->class_size = 0;
2465 mono_class_setup_supertypes (class);
2469 if (eclass->enumtype)
2470 class->cast_class = eclass->element_class;
2472 class->cast_class = eclass;
2474 class->element_class = eclass;
2476 if ((rank > 1) || bounded) {
2477 MonoArrayType *at = g_new0 (MonoArrayType, 1);
2478 class->byval_arg.type = MONO_TYPE_ARRAY;
2479 class->byval_arg.data.array = at;
2480 at->eklass = eclass;
2482 /* FIXME: complete.... */
2484 class->byval_arg.type = MONO_TYPE_SZARRAY;
2485 class->byval_arg.data.klass = eclass;
2487 class->this_arg = class->byval_arg;
2488 class->this_arg.byref = 1;
2493 list = g_slist_append (rootlist, class);
2494 g_hash_table_insert (image->array_cache, eclass, list);
2496 mono_loader_unlock ();
2502 * mono_array_class_get:
2503 * @element_class: element class
2504 * @rank: the dimension of the array class
2506 * Returns: a class object describing the array with element type @element_type and
2510 mono_array_class_get (MonoClass *eclass, guint32 rank)
2512 return mono_bounded_array_class_get (eclass, rank, FALSE);
2516 * mono_class_instance_size:
2519 * Returns: the size of an object instance
2522 mono_class_instance_size (MonoClass *klass)
2524 if (!klass->size_inited)
2525 mono_class_init (klass);
2527 g_assert (!klass->generic_container &&
2528 (!klass->generic_class || !klass->generic_class->inst->is_open));
2529 return klass->instance_size;
2533 * mono_class_min_align:
2536 * Returns: minimm alignment requirements
2539 mono_class_min_align (MonoClass *klass)
2541 if (!klass->size_inited)
2542 mono_class_init (klass);
2544 return klass->min_align;
2548 * mono_class_value_size:
2551 * This function is used for value types, and return the
2552 * space and the alignment to store that kind of value object.
2554 * Returns: the size of a value of kind @klass
2557 mono_class_value_size (MonoClass *klass, guint32 *align)
2561 /* fixme: check disable, because we still have external revereces to
2562 * mscorlib and Dummy Objects
2564 /*g_assert (klass->valuetype);*/
2566 size = mono_class_instance_size (klass) - sizeof (MonoObject);
2569 *align = klass->min_align;
2575 * mono_class_data_size:
2578 * Returns: the size of the static class data
2581 mono_class_data_size (MonoClass *klass)
2584 mono_class_init (klass);
2586 return klass->class_size;
2590 * Auxiliary routine to mono_class_get_field
2592 * Takes a field index instead of a field token.
2594 static MonoClassField *
2595 mono_class_get_field_idx (MonoClass *class, int idx)
2597 if (class->field.count){
2598 if ((idx >= class->field.first) && (idx < class->field.last)){
2599 return &class->fields [idx - class->field.first];
2606 return mono_class_get_field_idx (class->parent, idx);
2610 * mono_class_get_field:
2611 * @class: the class to lookup the field.
2612 * @field_token: the field token
2614 * Returns: A MonoClassField representing the type and offset of
2615 * the field, or a NULL value if the field does not belong to this
2619 mono_class_get_field (MonoClass *class, guint32 field_token)
2621 int idx = mono_metadata_token_index (field_token);
2623 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
2625 return mono_class_get_field_idx (class, idx - 1);
2629 mono_class_get_field_from_name (MonoClass *klass, const char *name)
2634 for (i = 0; i < klass->field.count; ++i) {
2635 if (strcmp (name, klass->fields [i].name) == 0)
2636 return &klass->fields [i];
2638 klass = klass->parent;
2644 mono_class_get_field_token (MonoClassField *field)
2646 MonoClass *klass = field->parent;
2650 for (i = 0; i < klass->field.count; ++i) {
2651 if (&klass->fields [i] == field)
2652 return mono_metadata_make_token (MONO_TABLE_FIELD, klass->field.first + i + 1);
2654 klass = klass->parent;
2657 g_assert_not_reached ();
2662 mono_class_get_event_token (MonoEvent *event)
2664 MonoClass *klass = event->parent;
2668 for (i = 0; i < klass->event.count; ++i) {
2669 if (&klass->events [i] == event)
2670 return mono_metadata_make_token (MONO_TABLE_EVENT, klass->event.first + i + 1);
2672 klass = klass->parent;
2675 g_assert_not_reached ();
2680 mono_vtable_get_static_field_data (MonoVTable *vt)
2686 mono_class_get_property_from_name (MonoClass *klass, const char *name)
2691 for (i = 0; i < klass->property.count; ++i) {
2692 if (strcmp (name, klass->properties [i].name) == 0)
2693 return &klass->properties [i];
2695 klass = klass->parent;
2701 mono_class_get_property_token (MonoProperty *prop)
2703 MonoClass *klass = prop->parent;
2707 for (i = 0; i < klass->property.count; ++i) {
2708 if (&klass->properties [i] == prop)
2709 return mono_metadata_make_token (MONO_TABLE_PROPERTY, klass->property.first + i + 1);
2711 klass = klass->parent;
2714 g_assert_not_reached ();
2720 * @image: the image where the class resides
2721 * @type_token: the token for the class
2722 * @at: an optional pointer to return the array element type
2724 * Returns: the MonoClass that represents @type_token in @image
2727 _mono_class_get (MonoImage *image, guint32 type_token, MonoGenericContext *context)
2729 MonoClass *class = NULL;
2732 return mono_lookup_dynamic_token (image, type_token);
2734 switch (type_token & 0xff000000){
2735 case MONO_TOKEN_TYPE_DEF:
2736 class = mono_class_create_from_typedef (image, type_token);
2738 case MONO_TOKEN_TYPE_REF:
2739 class = mono_class_from_typeref (image, type_token);
2741 case MONO_TOKEN_TYPE_SPEC:
2742 class = mono_class_create_from_typespec (image, type_token, context);
2745 g_warning ("unknown token type %x", type_token & 0xff000000);
2746 g_assert_not_reached ();
2750 g_warning ("Could not load class from token 0x%08x in %s", type_token, image->name);
2756 mono_class_get (MonoImage *image, guint32 type_token)
2758 return _mono_class_get (image, type_token, NULL);
2762 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
2764 MonoClass *class = _mono_class_get (image, type_token, context);
2767 if (!class || !context || (!context->gclass && !context->gmethod))
2770 switch (class->byval_arg.type) {
2771 case MONO_TYPE_GENERICINST:
2772 if (!class->generic_class->inst->is_open)
2776 case MONO_TYPE_MVAR:
2782 inflated = inflate_generic_type (&class->byval_arg, context);
2786 return mono_class_from_mono_type (inflated);
2790 * mono_class_from_name_case:
2791 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
2792 * @name_space: the type namespace
2793 * @name: the type short name.
2795 * Obtains a MonoClass with a given namespace and a given name which
2796 * is located in the given MonoImage. The namespace and name
2797 * lookups are case insensitive.
2799 * You can also pass @NULL to the image, and that will lookup for
2800 * a type with the given namespace and name in all of the loaded
2801 * assemblies: notice that since there might be a name clash in this
2802 * case, passing @NULL is not encouraged if you need a precise type.
2806 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
2808 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
2809 guint32 cols [MONO_TYPEDEF_SIZE];
2814 /* add a cache if needed */
2815 for (i = 1; i <= t->rows; ++i) {
2816 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
2817 /* nested types are accessed from the nesting name */
2818 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
2819 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
2821 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2822 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2823 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
2824 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
2830 return_nested_in (MonoClass *class, char *nested) {
2832 char *s = strchr (nested, '/');
2839 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
2841 if (strcmp (found->name, nested) == 0) {
2843 return return_nested_in (found, s);
2852 * mono_class_from_name_case:
2853 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
2854 * @name_space: the type namespace
2855 * @name: the type short name.
2857 * Obtains a MonoClass with a given namespace and a given name which
2858 * is located in the given MonoImage.
2860 * You can also pass `NULL' to the image, and that will lookup for
2861 * a type with the given namespace and name in all of the loaded
2862 * assemblies: notice that since there might be a name clash in this
2863 * case, passing NULL is not encouraged if you need a precise type.
2867 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
2869 GHashTable *nspace_table;
2870 MonoImage *loaded_image;
2876 if ((nested = strchr (name, '/'))) {
2877 int pos = nested - name;
2878 int len = strlen (name);
2881 memcpy (buf, name, len + 1);
2883 nested = buf + pos + 1;
2887 mono_loader_lock ();
2889 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
2892 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
2894 mono_loader_unlock ();
2899 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
2900 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
2901 guint32 cols [MONO_EXP_TYPE_SIZE];
2904 idx = mono_metadata_token_index (token);
2906 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
2908 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
2909 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
2910 loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
2913 class = mono_class_from_name (loaded_image, name_space, name);
2915 return return_nested_in (class, nested);
2918 g_error ("not yet implemented");
2922 token = MONO_TOKEN_TYPE_DEF | token;
2924 class = mono_class_get (image, token);
2926 return return_nested_in (class, nested);
2931 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
2932 gboolean check_interfaces)
2935 g_assert (klassc->idepth > 0);
2936 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
2937 if ((klassc->interface_id <= klass->max_interface_id) &&
2938 (klass->interface_offsets [klassc->interface_id] >= 0))
2940 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
2943 for (i = 0; i < klass->interface_count; i ++) {
2944 MonoClass *ic = klass->interfaces [i];
2949 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
2954 * MS.NET thinks interfaces are a subclass of Object, so we think it as
2957 if (klassc == mono_defaults.object_class)
2960 if (klass->generic_class) {
2961 MonoType *parent = klass->generic_class->parent;
2965 if (mono_metadata_type_equal (parent, &klassc->byval_arg))
2967 klass = mono_class_from_mono_type (parent);
2975 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
2978 mono_class_init (klass);
2980 if (!oklass->inited)
2981 mono_class_init (oklass);
2983 if (MONO_CLASS_IS_INTERFACE (klass)) {
2984 if ((klass->interface_id <= oklass->max_interface_id) &&
2985 (oklass->interface_offsets [klass->interface_id] != -1))
2989 MonoClass *eclass, *eoclass;
2991 if (oklass->rank != klass->rank)
2994 /* vectors vs. one dimensional arrays */
2995 if (oklass->byval_arg.type != klass->byval_arg.type)
2998 eclass = klass->cast_class;
2999 eoclass = oklass->cast_class;
3003 * a is b does not imply a[] is b[] when a is a valuetype, and
3004 * b is a reference type.
3007 if (eoclass->valuetype) {
3008 if ((eclass == mono_defaults.enum_class) ||
3009 (eclass == mono_defaults.enum_class->parent) ||
3010 (eclass == mono_defaults.object_class))
3014 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
3017 if (klass == mono_defaults.object_class)
3020 return mono_class_has_parent (oklass, klass);
3024 * mono_class_needs_cctor_run:
3026 * Determines whenever the class has a static constructor and whenever it
3027 * needs to be called when executing CALLER.
3030 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
3035 for (i = 0; i < klass->method.count; ++i) {
3036 method = klass->methods [i];
3037 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
3038 (strcmp (".cctor", method->name) == 0)) {
3039 if (caller == method)
3048 * mono_class_array_element_size:
3051 * Returns: the number of bytes an element of type @klass
3052 * uses when stored into an array.
3055 mono_class_array_element_size (MonoClass *klass)
3057 MonoType *type = &klass->byval_arg;
3060 switch (type->type) {
3063 case MONO_TYPE_BOOLEAN:
3067 case MONO_TYPE_CHAR:
3076 case MONO_TYPE_CLASS:
3077 case MONO_TYPE_STRING:
3078 case MONO_TYPE_OBJECT:
3079 case MONO_TYPE_SZARRAY:
3080 case MONO_TYPE_ARRAY:
3082 case MONO_TYPE_MVAR:
3083 return sizeof (gpointer);
3088 case MONO_TYPE_VALUETYPE:
3089 if (type->data.klass->enumtype) {
3090 type = type->data.klass->enum_basetype;
3091 klass = klass->element_class;
3094 return mono_class_instance_size (klass) - sizeof (MonoObject);
3095 case MONO_TYPE_GENERICINST:
3096 type = &type->data.generic_class->container_class->byval_arg;
3099 g_error ("unknown type 0x%02x in mono_class_array_element_size", type->type);
3105 * mono_array_element_size:
3106 * @ac: pointer to a #MonoArrayClass
3108 * Returns: the size of single array element.
3111 mono_array_element_size (MonoClass *ac)
3113 return mono_class_array_element_size (ac->element_class);
3117 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
3118 MonoGenericContext *context)
3120 if (image->dynamic) {
3121 gpointer obj = mono_lookup_dynamic_token (image, token);
3123 switch (token & 0xff000000) {
3124 case MONO_TOKEN_TYPE_DEF:
3125 case MONO_TOKEN_TYPE_REF:
3126 case MONO_TOKEN_TYPE_SPEC:
3128 *handle_class = mono_defaults.typehandle_class;
3129 return &((MonoClass*)obj)->byval_arg;
3130 case MONO_TOKEN_METHOD_DEF:
3132 *handle_class = mono_defaults.methodhandle_class;
3134 case MONO_TOKEN_FIELD_DEF:
3136 *handle_class = mono_defaults.fieldhandle_class;
3139 g_assert_not_reached ();
3143 switch (token & 0xff000000) {
3144 case MONO_TOKEN_TYPE_DEF:
3145 case MONO_TOKEN_TYPE_REF: {
3148 *handle_class = mono_defaults.typehandle_class;
3149 class = mono_class_get_full (image, token, context);
3150 mono_class_init (class);
3151 /* We return a MonoType* as handle */
3152 return &class->byval_arg;
3154 case MONO_TOKEN_TYPE_SPEC: {
3157 *handle_class = mono_defaults.typehandle_class;
3158 class = mono_class_create_from_typespec (image, token, context);
3159 mono_class_init (class);
3160 return &class->byval_arg;
3162 case MONO_TOKEN_FIELD_DEF: {
3164 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
3165 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
3166 mono_class_init (class);
3168 *handle_class = mono_defaults.fieldhandle_class;
3169 return mono_class_get_field (class, token);
3171 case MONO_TOKEN_METHOD_DEF: {
3173 meth = mono_get_method_full (image, token, NULL, context);
3175 *handle_class = mono_defaults.methodhandle_class;
3178 case MONO_TOKEN_MEMBER_REF: {
3179 guint32 cols [MONO_MEMBERREF_SIZE];
3181 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
3182 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
3183 mono_metadata_decode_blob_size (sig, &sig);
3184 if (*sig == 0x6) { /* it's a field */
3186 MonoClassField *field;
3187 field = mono_field_from_token (image, token, &klass, context);
3189 *handle_class = mono_defaults.fieldhandle_class;
3193 meth = mono_get_method_full (image, token, NULL, context);
3195 *handle_class = mono_defaults.methodhandle_class;
3200 g_warning ("Unknown token 0x%08x in ldtoken", token);
3207 * This function might need to call runtime functions so it can't be part
3208 * of the metadata library.
3210 static MonoLookupDynamicToken lookup_dynamic = NULL;
3213 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
3215 lookup_dynamic = func;
3219 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
3221 return lookup_dynamic (image, token);
3225 mono_class_get_image (MonoClass *klass)
3227 return klass->image;
3231 * mono_class_get_element_class:
3232 * @klass: the MonoClass to act on
3234 * Returns: the element class of an array or an enumeration.
3237 mono_class_get_element_class (MonoClass *klass)
3239 return klass->element_class;
3243 * mono_class_is_valuetype:
3244 * @klass: the MonoClass to act on
3246 * Returns: true if the MonoClass represents a ValueType.
3249 mono_class_is_valuetype (MonoClass *klass)
3251 return klass->valuetype;
3255 * mono_class_is_enum:
3256 * @klass: the MonoClass to act on
3258 * Returns: true if the MonoClass represents an enumeration.
3261 mono_class_is_enum (MonoClass *klass)
3263 return klass->enumtype;
3267 * mono_class_enum_basetype:
3268 * @klass: the MonoClass to act on
3270 * Returns: the underlying type representation for an enumeration.
3273 mono_class_enum_basetype (MonoClass *klass)
3275 return klass->enum_basetype;
3279 * mono_class_get_parent
3280 * @klass: the MonoClass to act on
3282 * Returns: the parent class for this class.
3285 mono_class_get_parent (MonoClass *klass)
3287 return klass->parent;
3291 * mono_class_get_nesting_type;
3292 * @klass: the MonoClass to act on
3294 * Returns: the container type where this type is nested or NULL if this type is not a nested type.
3297 mono_class_get_nesting_type (MonoClass *klass)
3299 return klass->nested_in;
3303 * mono_class_get_rank:
3304 * @klass: the MonoClass to act on
3306 * Returns: the rank for the array (the number of dimensions).
3309 mono_class_get_rank (MonoClass *klass)
3315 * mono_class_get_flags:
3316 * @klass: the MonoClass to act on
3318 * The type flags from the TypeDef table from the metadata.
3319 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
3322 * Returns: the flags from the TypeDef table.
3325 mono_class_get_flags (MonoClass *klass)
3327 return klass->flags;
3331 * mono_class_get_name
3332 * @klass: the MonoClass to act on
3334 * Returns: the name of the class.
3337 mono_class_get_name (MonoClass *klass)
3343 * mono_class_get_namespace:
3344 * @klass: the MonoClass to act on
3346 * Returns: the namespace of the class.
3349 mono_class_get_namespace (MonoClass *klass)
3351 return klass->name_space;
3355 * mono_class_get_type:
3356 * @klass: the MonoClass to act on
3358 * This method returns the internal Type representation for the class.
3360 * Returns: the MonoType from the class.
3363 mono_class_get_type (MonoClass *klass)
3365 return &klass->byval_arg;
3369 * mono_class_get_byref_type:
3370 * @klass: the MonoClass to act on
3375 mono_class_get_byref_type (MonoClass *klass)
3377 return &klass->this_arg;
3381 * mono_class_num_fields:
3382 * @klass: the MonoClass to act on
3384 * Returns: the number of static and instance fields in the class.
3387 mono_class_num_fields (MonoClass *klass)
3389 return klass->field.count;
3393 * mono_class_num_methods:
3394 * @klass: the MonoClass to act on
3396 * Returns: the number of methods in the class.
3399 mono_class_num_methods (MonoClass *klass)
3401 return klass->method.count;
3405 * mono_class_num_properties
3406 * @klass: the MonoClass to act on
3408 * Returns: the number of properties in the class.
3411 mono_class_num_properties (MonoClass *klass)
3413 return klass->property.count;
3417 * mono_class_num_events:
3418 * @klass: the MonoClass to act on
3420 * Returns: the number of events in the class.
3423 mono_class_num_events (MonoClass *klass)
3425 return klass->event.count;
3429 * mono_class_get_fields:
3430 * @klass: the MonoClass to act on
3432 * This routine is an iterator routine for retrieving the fields in a class.
3434 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3435 * iterate over all of the elements. When no more values are
3436 * available, the return value is NULL.
3438 * Returns: a @MonoClassField* on each iteration, or NULL when no more fields are available.
3441 mono_class_get_fields (MonoClass* klass, gpointer *iter)
3443 MonoClassField* field;
3447 mono_class_init (klass);
3449 /* start from the first */
3450 if (klass->field.count) {
3451 return *iter = &klass->fields [0];
3459 if (field < &klass->fields [klass->field.count]) {
3460 return *iter = field;
3466 * mono_class_get_methods
3467 * @klass: the MonoClass to act on
3469 * This routine is an iterator routine for retrieving the fields in a class.
3471 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3472 * iterate over all of the elements. When no more values are
3473 * available, the return value is NULL.
3475 * Returns: a MonoMethod on each iteration or NULL when no more methods are available.
3478 mono_class_get_methods (MonoClass* klass, gpointer *iter)
3480 MonoMethod** method;
3484 mono_class_init (klass);
3486 /* start from the first */
3487 if (klass->method.count) {
3488 *iter = &klass->methods [0];
3489 return klass->methods [0];
3497 if (method < &klass->methods [klass->method.count]) {
3505 * mono_class_get_properties:
3506 * @klass: the MonoClass to act on
3508 * This routine is an iterator routine for retrieving the properties in a class.
3510 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3511 * iterate over all of the elements. When no more values are
3512 * available, the return value is NULL.
3514 * Returns: a @MonoProperty* on each invocation, or NULL when no more are available.
3517 mono_class_get_properties (MonoClass* klass, gpointer *iter)
3519 MonoProperty* property;
3523 mono_class_init (klass);
3525 /* start from the first */
3526 if (klass->property.count) {
3527 return *iter = &klass->properties [0];
3535 if (property < &klass->properties [klass->property.count]) {
3536 return *iter = property;
3542 * mono_class_get_events:
3543 * @klass: the MonoClass to act on
3545 * This routine is an iterator routine for retrieving the properties in a class.
3547 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3548 * iterate over all of the elements. When no more values are
3549 * available, the return value is NULL.
3551 * Returns: a @MonoEvent* on each invocation, or NULL when no more are available.
3554 mono_class_get_events (MonoClass* klass, gpointer *iter)
3560 mono_class_init (klass);
3562 /* start from the first */
3563 if (klass->event.count) {
3564 return *iter = &klass->events [0];
3572 if (event < &klass->events [klass->event.count]) {
3573 return *iter = event;
3579 * mono_class_get_interfaces
3580 * @klass: the MonoClass to act on
3582 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
3584 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3585 * iterate over all of the elements. When no more values are
3586 * available, the return value is NULL.
3588 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
3591 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
3597 mono_class_init (klass);
3599 /* start from the first */
3600 if (klass->interface_count) {
3601 *iter = &klass->interfaces [0];
3602 return klass->interfaces [0];
3610 if (iface < &klass->interfaces [klass->interface_count]) {
3618 * mono_class_get_nested_types
3619 * @klass: the MonoClass to act on
3621 * This routine is an iterator routine for retrieving the nested types of a class.
3623 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3624 * iterate over all of the elements. When no more values are
3625 * available, the return value is NULL.
3627 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
3630 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
3636 mono_class_init (klass);
3638 /* start from the first */
3639 if (klass->nested_classes) {
3640 *iter = klass->nested_classes;
3641 return klass->nested_classes->data;
3643 /* no nested types */
3657 * mono_field_get_name:
3658 * @field: the MonoClassField to act on
3660 * Returns: the name of the field.
3663 mono_field_get_name (MonoClassField *field)
3669 * mono_field_get_type:
3670 * @field: the MonoClassField to act on
3672 * Returns: MonoType of the field.
3675 mono_field_get_type (MonoClassField *field)
3681 * mono_field_get_type:
3682 * @field: the MonoClassField to act on
3684 * Returns: MonoClass where the field was defined.
3687 mono_field_get_parent (MonoClassField *field)
3689 return field->parent;
3693 * mono_field_get_flags;
3694 * @field: the MonoClassField to act on
3696 * The metadata flags for a field are encoded using the
3697 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
3699 * Returns: the flags for the field.
3702 mono_field_get_flags (MonoClassField *field)
3704 return field->type->attrs;
3708 * mono_property_get_name:
3709 * @prop: the MonoProperty to act on
3711 * Returns: the name of the property
3714 mono_property_get_name (MonoProperty *prop)
3720 * mono_property_get_set_method
3721 * @prop: the MonoProperty to act on.
3723 * Returns: the setter method of the property (A MonoMethod)
3726 mono_property_get_set_method (MonoProperty *prop)
3732 * mono_property_get_get_method
3733 * @prop: the MonoProperty to act on.
3735 * Returns: the setter method of the property (A MonoMethod)
3738 mono_property_get_get_method (MonoProperty *prop)
3744 * mono_property_get_parent:
3745 * @prop: the MonoProperty to act on.
3747 * Returns: the MonoClass where the property was defined.
3750 mono_property_get_parent (MonoProperty *prop)
3752 return prop->parent;
3756 * mono_property_get_flags:
3757 * @prop: the MonoProperty to act on.
3759 * The metadata flags for a property are encoded using the
3760 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
3762 * Returns: the flags for the property.
3765 mono_property_get_flags (MonoProperty *prop)
3771 * mono_event_get_name:
3772 * @event: the MonoEvent to act on
3774 * Returns: the name of the event.
3777 mono_event_get_name (MonoEvent *event)
3783 * mono_event_get_add_method:
3784 * @event: The MonoEvent to act on.
3786 * Returns: the @add' method for the event (a MonoMethod).
3789 mono_event_get_add_method (MonoEvent *event)
3795 * mono_event_get_remove_method:
3796 * @event: The MonoEvent to act on.
3798 * Returns: the @remove method for the event (a MonoMethod).
3801 mono_event_get_remove_method (MonoEvent *event)
3803 return event->remove;
3807 * mono_event_get_raise_method:
3808 * @event: The MonoEvent to act on.
3810 * Returns: the @raise method for the event (a MonoMethod).
3813 mono_event_get_raise_method (MonoEvent *event)
3815 return event->raise;
3819 * mono_event_get_parent:
3820 * @event: the MonoEvent to act on.
3822 * Returns: the MonoClass where the event is defined.
3825 mono_event_get_parent (MonoEvent *event)
3827 return event->parent;
3831 * mono_event_get_flags
3832 * @event: the MonoEvent to act on.
3834 * The metadata flags for an event are encoded using the
3835 * EVENT_* constants. See the tabledefs.h file for details.
3837 * Returns: the flags for the event.
3840 mono_event_get_flags (MonoEvent *event)
3842 return event->attrs;
3846 * mono_find_method_by_name:
3847 * @klass: where to look for the method
3848 * @name_space: name of the method
3849 * @param_count: number of parameters. -1 for any number.
3851 * Obtains a MonoMethod with a given name and number of parameters.
3852 * It only works if there are no multiple signatures for any given method name.
3855 mono_class_get_method_from_name (MonoClass *klass, const char *name, int param_count)
3857 MonoMethod *res = NULL;
3860 mono_class_init (klass);
3862 for (i = 0; i < klass->method.count; ++i) {
3863 if (klass->methods [i]->name[0] == name [0] &&
3864 !strcmp (name, klass->methods [i]->name) &&
3865 (param_count == -1 || klass->methods [i]->signature->param_count == param_count)) {
3866 res = klass->methods [i];