2 * class.c: Class management for the Mono runtime
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
9 * Possible Optimizations:
10 * in mono_class_create, do not allocate the class right away,
11 * but wait until you know the size of the FieldMap, so that
12 * the class embeds directly the FieldMap after the vtable.
22 #include <mono/metadata/image.h>
23 #include <mono/metadata/assembly.h>
24 #include <mono/metadata/cil-coff.h>
25 #include <mono/metadata/metadata.h>
26 #include <mono/metadata/metadata-internals.h>
27 #include <mono/metadata/tabledefs.h>
28 #include <mono/metadata/tokentype.h>
29 #include <mono/metadata/class-internals.h>
30 #include <mono/metadata/object.h>
31 #include <mono/metadata/appdomain.h>
32 #include <mono/metadata/mono-endian.h>
33 #include <mono/metadata/debug-helpers.h>
34 #include <mono/metadata/reflection.h>
35 #include <mono/metadata/mono-debug-debugger.h>
36 #include <mono/os/gc_wrapper.h>
40 gboolean mono_print_vtable = FALSE;
42 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
44 void (*mono_debugger_start_class_init_func) (MonoClass *klass) = NULL;
45 void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
48 mono_class_from_typeref (MonoImage *image, guint32 type_token)
50 guint32 cols [MONO_TYPEREF_SIZE];
51 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
53 const char *name, *nspace;
55 MonoAssembly **references;
57 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
59 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
60 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
62 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
63 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
64 case MONO_RESOLTION_SCOPE_MODULE:
66 g_error ("null ResolutionScope not yet handled");
67 /* a typedef in disguise */
68 return mono_class_from_name (image, nspace, name);
69 case MONO_RESOLTION_SCOPE_MODULEREF:
70 return mono_class_from_name (image->modules [idx - 1], nspace, name);
71 case MONO_RESOLTION_SCOPE_TYPEREF: {
72 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
74 mono_class_init (enclosing);
75 for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
77 if (strcmp (res->name, name) == 0)
80 g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
83 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
87 references = image->references;
88 if (!references [idx-1])
89 mono_assembly_load_reference (image, idx - 1);
90 if (references [idx - 1] == (gpointer)-1)
93 image = references [idx-1]->image;
95 return mono_class_from_name (image, nspace, name);
99 dup_type (MonoType* t, const MonoType *original)
101 MonoType *r = g_new0 (MonoType, 1);
103 r->attrs = original->attrs;
104 r->byref = original->byref;
105 mono_stats.generics_metadata_size += sizeof (MonoType);
110 mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed,
111 gboolean include_ns, gboolean include_arity)
115 switch (type->type) {
116 case MONO_TYPE_ARRAY: {
117 int i, rank = type->data.array->rank;
119 mono_type_get_name_recurse (
120 &type->data.array->eklass->byval_arg, str,
121 FALSE, include_ns, include_arity);
122 g_string_append_c (str, '[');
123 for (i = 1; i < rank; i++)
124 g_string_append_c (str, ',');
125 g_string_append_c (str, ']');
128 case MONO_TYPE_SZARRAY:
129 mono_type_get_name_recurse (
130 &type->data.klass->byval_arg, str, FALSE, include_ns, include_arity);
131 g_string_append (str, "[]");
134 mono_type_get_name_recurse (type->data.type, str, FALSE, include_ns, include_arity);
135 g_string_append_c (str, '*');
138 klass = mono_class_from_mono_type (type);
139 if (klass->nested_in) {
140 mono_type_get_name_recurse (
141 &klass->nested_in->byval_arg, str, TRUE, include_ns, include_arity);
142 g_string_append_c (str, '+');
144 if (include_ns && *klass->name_space) {
145 g_string_append (str, klass->name_space);
146 g_string_append_c (str, '.');
148 if (!include_arity) {
149 char *s = strchr (klass->name, '`');
150 int len = s ? s - klass->name : strlen (klass->name);
152 g_string_append_len (str, klass->name, len);
154 g_string_append (str, klass->name);
157 if (klass->generic_inst) {
158 MonoGenericInst *ginst = klass->generic_inst;
161 g_string_append_c (str, '[');
162 for (i = 0; i < ginst->type_argc; i++) {
164 g_string_append_c (str, ',');
165 mono_type_get_name_recurse (
166 ginst->type_argv [i], str, FALSE, include_ns, include_arity);
168 g_string_append_c (str, ']');
169 } else if (klass->generic_container) {
172 g_string_append_c (str, '[');
173 for (i = 0; i < klass->generic_container->type_argc; i++) {
175 g_string_append_c (str, ',');
176 g_string_append (str, klass->generic_container->type_params [i].name);
178 g_string_append_c (str, ']');
185 * mono_type_get_name:
188 * Returns the string representation for type as required by System.Reflection.
189 * The inverse of mono_reflection_parse_type ().
192 _mono_type_get_name (MonoType *type, gboolean is_recursed, gboolean include_ns,
193 gboolean include_arity)
195 GString* result = g_string_new ("");
196 mono_type_get_name_recurse (type, result, is_recursed, include_ns, include_arity);
199 g_string_append_c (result, '&');
201 return g_string_free (result, FALSE);
205 mono_type_get_name (MonoType *type)
207 return _mono_type_get_name (type, TRUE, TRUE, TRUE);
211 mono_type_get_full_name (MonoType *type)
213 return _mono_type_get_name (type, FALSE, TRUE, TRUE);
217 mono_type_get_underlying_type (MonoType *type)
219 switch (type->type) {
220 case MONO_TYPE_VALUETYPE:
221 if (type->data.klass->enumtype)
222 return type->data.klass->enum_basetype;
224 case MONO_TYPE_GENERICINST:
225 return mono_type_get_underlying_type (type->data.generic_inst->generic_type);
234 mono_class_is_open_constructed_type (MonoType *t)
240 case MONO_TYPE_SZARRAY:
241 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
242 case MONO_TYPE_ARRAY:
243 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
245 return mono_class_is_open_constructed_type (t->data.type);
246 case MONO_TYPE_GENERICINST: {
247 MonoGenericInst *ginst = t->data.generic_inst;
250 if (mono_class_is_open_constructed_type (ginst->generic_type))
252 for (i = 0; i < ginst->type_argc; i++)
253 if (mono_class_is_open_constructed_type (ginst->type_argv [i]))
263 inflate_generic_type (MonoType *type, MonoGenericContext *context)
265 switch (type->type) {
267 if (context->gmethod && context->gmethod->mtype_argv)
269 context->gmethod->mtype_argv [type->data.generic_param->num],
276 context->ginst->type_argv [type->data.generic_param->num],
280 case MONO_TYPE_SZARRAY: {
281 MonoClass *eclass = type->data.klass;
282 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
285 nt = dup_type (type, type);
286 nt->data.klass = mono_class_from_mono_type (inflated);
289 case MONO_TYPE_GENERICINST: {
290 MonoGenericInst *oginst = type->data.generic_inst;
291 MonoGenericInst *nginst, *cached;
295 nginst = g_new0 (MonoGenericInst, 1);
298 nginst->is_open = FALSE;
300 nginst->type_argv = g_new0 (MonoType *, oginst->type_argc);
302 for (i = 0; i < oginst->type_argc; i++) {
303 MonoType *t = oginst->type_argv [i];
304 nginst->type_argv [i] = mono_class_inflate_generic_type (t, context);
306 if (!nginst->is_open)
307 nginst->is_open = mono_class_is_open_constructed_type (nginst->type_argv [i]);
310 nginst->klass = NULL;
312 nginst->context = g_new0 (MonoGenericContext, 1);
313 nginst->context->ginst = nginst;
316 cached = g_hash_table_lookup (oginst->klass->image->generic_inst_cache, nginst);
319 g_free (nginst->type_argv);
321 mono_loader_unlock ();
323 nt = dup_type (type, type);
324 nt->data.generic_inst = cached;
328 nginst->dynamic_info = NULL;
329 nginst->initialized = FALSE;
331 mono_class_create_generic (nginst);
332 mono_class_create_generic_2 (nginst);
334 mono_stats.generic_instance_count++;
335 mono_stats.generics_metadata_size += sizeof (MonoGenericInst) +
336 sizeof (MonoGenericContext) +
337 nginst->type_argc * sizeof (MonoType);
339 nt = dup_type (type, type);
340 nt->data.generic_inst = nginst;
341 g_hash_table_insert (oginst->klass->image->generic_inst_cache, nginst, nginst);
342 mono_loader_unlock ();
352 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
354 MonoType *inflated = inflate_generic_type (type, context);
359 mono_stats.inflated_type_count++;
363 static MonoMethodSignature*
364 inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig,
365 MonoGenericContext *context)
367 MonoMethodSignature *res;
369 res = mono_metadata_signature_alloc (image, sig->param_count);
370 res->ret = mono_class_inflate_generic_type (sig->ret, context);
371 for (i = 0; i < sig->param_count; ++i)
372 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
373 res->hasthis = sig->hasthis;
374 res->explicit_this = sig->explicit_this;
375 res->call_convention = sig->call_convention;
376 res->generic_param_count = sig->generic_param_count;
377 res->is_inflated = 1;
381 static MonoMethodHeader*
382 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
384 MonoMethodHeader *res;
386 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
387 res->code = header->code;
388 res->code_size = header->code_size;
389 res->max_stack = header->max_stack;
390 res->num_clauses = header->num_clauses;
391 res->init_locals = header->init_locals;
392 res->num_locals = header->num_locals;
393 res->clauses = header->clauses;
394 for (i = 0; i < header->num_locals; ++i)
395 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
400 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context,
403 MonoMethodInflated *result;
406 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
407 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
410 mono_stats.inflated_method_count++;
411 mono_stats.generics_metadata_size +=
412 sizeof (MonoMethodInflated) - sizeof (MonoMethodNormal);
414 result = g_new0 (MonoMethodInflated, 1);
415 result->nmethod = *(MonoMethodNormal*)method;
417 if (mono_method_get_header (method))
418 result->nmethod.header = inflate_generic_header (
419 mono_method_get_header (method), context);
422 rklass = result->nmethod.method.klass = klass;
424 MonoType *declaring = mono_class_inflate_generic_type (&method->klass->byval_arg, context);
425 rklass = result->nmethod.method.klass = mono_class_from_mono_type (declaring);
428 result->nmethod.method.signature = inflate_generic_signature (
429 method->klass->image, method->signature, context);
431 if (context->gmethod) {
432 result->context = g_new0 (MonoGenericContext, 1);
433 result->context->gmethod = context->gmethod;
434 result->context->ginst = rklass->generic_inst;
436 mono_stats.generics_metadata_size += sizeof (MonoGenericContext);
437 } else if (rklass->generic_inst)
438 result->context = rklass->generic_inst->context;
440 if (method->signature->is_inflated)
441 result->declaring = ((MonoMethodInflated *) method)->declaring;
443 result->declaring = method;
445 return (MonoMethod *) result;
449 * class_compute_field_layout:
450 * @m: pointer to the metadata.
451 * @class: The class to initialize
453 * Initializes the class->fields.
455 * Currently we only support AUTO_LAYOUT, and do not even try to do
456 * a good job at it. This is temporary to get the code for Paolo.
459 class_compute_field_layout (MonoClass *class)
461 MonoImage *m = class->image;
462 const int top = class->field.count;
463 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
464 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
465 int i, blittable = TRUE, real_size = 0;
467 guint32 packing_size = 0;
468 gboolean explicit_size;
469 MonoClassField *field;
471 if (class->size_inited)
475 if (!class->parent->size_inited)
476 class_compute_field_layout (class->parent);
477 class->instance_size += class->parent->instance_size;
478 class->min_align = class->parent->min_align;
479 blittable = class->parent->blittable;
481 class->instance_size = sizeof (MonoObject);
482 class->min_align = 1;
485 /* Get the real size */
486 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
489 g_assert ((packing_size & 0xfffffff0) == 0);
490 class->packing_size = packing_size;
491 real_size += class->instance_size;
495 if (explicit_size && real_size) {
496 class->instance_size = MAX (real_size, class->instance_size);
498 class->size_inited = 1;
499 class->blittable = blittable;
503 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT)
506 class->fields = g_new0 (MonoClassField, top);
509 * Fetch all the field information.
511 for (i = 0; i < top; i++){
513 guint32 cols [MONO_FIELD_SIZE];
514 int idx = class->field.first + i;
515 MonoGenericContainer *container = NULL;
517 field = &class->fields [i];
518 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
519 /* The name is needed for fieldrefs */
520 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
521 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
522 mono_metadata_decode_value (sig, &sig);
523 /* FIELD signature == 0x06 */
524 g_assert (*sig == 0x06);
525 if (class->generic_container)
526 container = class->generic_container;
527 else if (class->generic_inst) {
528 g_assert (class->generic_inst->container);
529 container = class->generic_inst->container;
531 field->type = mono_metadata_parse_type_full (
532 m, container, MONO_PARSE_FIELD, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
533 if (mono_field_is_deleted (field))
535 if (class->generic_inst) {
536 field->type = mono_class_inflate_generic_type (
537 field->type, class->generic_inst->context);
538 field->type->attrs = cols [MONO_FIELD_FLAGS];
541 field->parent = class;
543 /* Only do these checks if we still think this type is blittable */
544 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
545 if (field->type->byref || MONO_TYPE_IS_REFERENCE (field->type)) {
548 MonoClass *field_class = mono_class_from_mono_type (field->type);
549 if (!field_class || !field_class->blittable)
554 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
555 mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
556 if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
557 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, field->name);
560 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
561 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
563 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
564 field->data = mono_image_rva_map (class->image, rva);
567 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
568 class->enum_basetype = field->type;
569 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
570 blittable = class->element_class->blittable;
573 /* The def_value of fields is compute lazily during vtable creation */
576 if (class == mono_defaults.string_class)
579 class->blittable = blittable;
581 if (class->enumtype && !class->enum_basetype) {
582 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
585 if (explicit_size && real_size) {
586 class->instance_size = MAX (real_size, class->instance_size);
589 if (class->generic_container ||
590 (class->generic_inst && class->generic_inst->is_open))
593 mono_class_layout_fields (class);
597 mono_class_layout_fields (MonoClass *class)
600 const int top = class->field.count;
601 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
602 guint32 pass, passes, real_size;
603 gboolean gc_aware_layout = FALSE;
604 MonoClassField *field;
607 * Enable GC aware auto layout: in this mode, reference
608 * fields are grouped together inside objects, increasing collector
610 * Requires that all classes whose layout is known to native code be annotated
611 * with [StructLayout (LayoutKind.Sequential)]
612 * Value types have gc_aware_layout disabled by default, as per
613 * what the default is for other runtimes.
615 /* corlib is missing [StructLayout] directives in many places */
616 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
617 if (class->image != mono_defaults.corlib &&
618 class->byval_arg.type != MONO_TYPE_VALUETYPE)
619 gc_aware_layout = TRUE;
623 * Compute field layout and total size (not considering static fields)
627 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
628 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
635 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
639 real_size = class->parent->instance_size;
641 real_size = sizeof (MonoObject);
643 for (pass = 0; pass < passes; ++pass) {
644 for (i = 0; i < top; i++){
646 field = &class->fields [i];
648 if (mono_field_is_deleted (field))
650 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
653 if (gc_aware_layout) {
655 * We process fields with reference type in the first pass,
656 * and fields with non-reference type in the second pass.
657 * We use IS_POINTER instead of IS_REFERENCE because in
658 * some internal structures, we store GC_MALLOCed memory
659 * in IntPtr fields...
661 if (MONO_TYPE_IS_POINTER (field->type)) {
670 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
671 (strcmp (field->name, "$PRIVATE$") == 0)) {
672 /* This field is a hack inserted by MCS to empty structures */
676 size = mono_type_size (field->type, &align);
678 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
679 align = class->packing_size ? MIN (class->packing_size, align): align;
680 class->min_align = MAX (align, class->min_align);
681 field->offset = real_size;
682 field->offset += align - 1;
683 field->offset &= ~(align - 1);
684 real_size = field->offset + size;
687 class->instance_size = MAX (real_size, class->instance_size);
689 if (class->instance_size & (class->min_align - 1)) {
690 class->instance_size += class->min_align - 1;
691 class->instance_size &= ~(class->min_align - 1);
695 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
697 for (i = 0; i < top; i++) {
699 field = &class->fields [i];
702 * There must be info about all the fields in a type if it
703 * uses explicit layout.
706 if (mono_field_is_deleted (field))
708 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
711 size = mono_type_size (field->type, &align);
714 * When we get here, field->offset is already set by the
715 * loader (for either runtime fields or fields loaded from metadata).
716 * The offset is from the start of the object: this works for both
717 * classes and valuetypes.
719 field->offset += sizeof (MonoObject);
724 real_size = MAX (real_size, size + field->offset);
726 class->instance_size = MAX (real_size, class->instance_size);
730 class->size_inited = 1;
733 * Compute static field layout and size
735 for (i = 0; i < top; i++){
737 field = &class->fields [i];
739 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
741 if (mono_field_is_deleted (field))
744 size = mono_type_size (field->type, &align);
745 field->offset = class->class_size;
746 field->offset += align - 1;
747 field->offset &= ~(align - 1);
748 class->class_size = field->offset + size;
753 init_properties (MonoClass *class)
755 guint startm, endm, i, j;
756 guint32 cols [MONO_PROPERTY_SIZE];
757 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
758 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
760 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
761 class->property.count = class->property.last - class->property.first;
763 class->properties = g_new0 (MonoProperty, class->property.count);
764 for (i = class->property.first; i < class->property.last; ++i) {
765 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
766 class->properties [i - class->property.first].parent = class;
767 class->properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
768 class->properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
770 startm = mono_metadata_methods_from_property (class->image, i, &endm);
771 for (j = startm; j < endm; ++j) {
772 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
773 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
774 case METHOD_SEMANTIC_SETTER:
775 class->properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
777 case METHOD_SEMANTIC_GETTER:
778 class->properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
788 init_events (MonoClass *class)
790 guint startm, endm, i, j;
791 guint32 cols [MONO_EVENT_SIZE];
792 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
793 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
795 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
796 class->event.count = class->event.last - class->event.first;
798 class->events = g_new0 (MonoEvent, class->event.count);
799 for (i = class->event.first; i < class->event.last; ++i) {
800 MonoEvent *event = &class->events [i - class->event.first];
802 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
803 event->parent = class;
804 event->attrs = cols [MONO_EVENT_FLAGS];
805 event->name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
807 startm = mono_metadata_methods_from_event (class->image, i, &endm);
808 for (j = startm; j < endm; ++j) {
809 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
810 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
811 case METHOD_SEMANTIC_ADD_ON:
812 event->add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
814 case METHOD_SEMANTIC_REMOVE_ON:
815 event->remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
817 case METHOD_SEMANTIC_FIRE:
818 event->raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
820 case METHOD_SEMANTIC_OTHER: {
823 if (event->other == NULL) {
824 event->other = g_new0 (MonoMethod*, 1);
826 while (event->other [n])
828 event->other = g_realloc (event->other, (n + 1) * sizeof (MonoMethod*));
830 event->other [n] = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
841 mono_get_unique_iid (MonoClass *class)
843 static GHashTable *iid_hash = NULL;
844 static guint iid = 0;
849 g_assert (MONO_CLASS_IS_INTERFACE (class));
854 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
856 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
858 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
859 mono_loader_unlock ();
863 g_hash_table_insert (iid_hash, str, (gpointer)iid);
867 mono_loader_unlock ();
873 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
878 for (i = 0; i < klass->interface_count; i++) {
879 ic = klass->interfaces [i];
882 *res = g_ptr_array_new ();
883 g_ptr_array_add (*res, ic);
885 collect_implemented_interfaces_aux (ic, res);
890 mono_class_get_implemented_interfaces (MonoClass *klass)
892 GPtrArray *res = NULL;
894 collect_implemented_interfaces_aux (klass, &res);
899 setup_interface_offsets (MonoClass *class, int cur_slot)
905 /* compute maximum number of slots and maximum interface id */
907 for (k = class; k ; k = k->parent) {
908 for (i = 0; i < k->interface_count; i++) {
909 ic = k->interfaces [i];
912 mono_class_init (ic);
914 if (max_iid < ic->interface_id)
915 max_iid = ic->interface_id;
919 if (MONO_CLASS_IS_INTERFACE (class)) {
920 if (max_iid < class->interface_id)
921 max_iid = class->interface_id;
923 class->max_interface_id = max_iid;
924 /* compute vtable offset for interfaces */
925 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
927 for (i = 0; i <= max_iid; i++)
928 class->interface_offsets [i] = -1;
930 ifaces = mono_class_get_implemented_interfaces (class);
932 for (i = 0; i < ifaces->len; ++i) {
933 ic = g_ptr_array_index (ifaces, i);
934 class->interface_offsets [ic->interface_id] = cur_slot;
935 cur_slot += ic->method.count;
937 g_ptr_array_free (ifaces, TRUE);
940 for (k = class->parent; k ; k = k->parent) {
941 ifaces = mono_class_get_implemented_interfaces (k);
943 for (i = 0; i < ifaces->len; ++i) {
944 ic = g_ptr_array_index (ifaces, i);
946 if (class->interface_offsets [ic->interface_id] == -1) {
947 int io = k->interface_offsets [ic->interface_id];
951 class->interface_offsets [ic->interface_id] = io;
954 g_ptr_array_free (ifaces, TRUE);
958 if (MONO_CLASS_IS_INTERFACE (class))
959 class->interface_offsets [class->interface_id] = cur_slot;
965 mono_class_setup_vtable (MonoClass *class, MonoMethod **overrides, int onum)
969 int i, max_vtsize = 0, max_iid, cur_slot = 0;
971 MonoGHashTable *override_map = NULL;
973 /* setup_vtable() must be called only once on the type */
974 if (class->interface_offsets) {
975 g_warning ("vtable already computed in %s.%s", class->name_space, class->name);
979 ifaces = mono_class_get_implemented_interfaces (class);
981 for (i = 0; i < ifaces->len; i++) {
982 MonoClass *ic = g_ptr_array_index (ifaces, i);
983 max_vtsize += ic->method.count;
985 g_ptr_array_free (ifaces, TRUE);
989 max_vtsize += class->parent->vtable_size;
990 cur_slot = class->parent->vtable_size;
993 max_vtsize += class->method.count;
995 vtable = alloca (sizeof (gpointer) * max_vtsize);
996 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
998 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
1000 cur_slot = setup_interface_offsets (class, cur_slot);
1001 max_iid = class->max_interface_id;
1003 if (class->parent && class->parent->vtable_size)
1004 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
1006 /* override interface methods */
1007 for (i = 0; i < onum; i++) {
1008 MonoMethod *decl = overrides [i*2];
1009 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
1011 g_assert (decl->slot != -1);
1012 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
1013 vtable [dslot] = overrides [i*2 + 1];
1014 vtable [dslot]->slot = dslot;
1016 override_map = mono_g_hash_table_new (NULL, NULL);
1018 mono_g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
1022 for (k = class; k ; k = k->parent) {
1024 ifaces = mono_class_get_implemented_interfaces (k);
1026 nifaces = ifaces->len;
1027 for (i = 0; i < nifaces; i++) {
1030 ic = g_ptr_array_index (ifaces, i);
1031 io = k->interface_offsets [ic->interface_id];
1034 g_assert (io <= max_vtsize);
1037 for (l = 0; l < ic->method.count; l++) {
1038 MonoMethod *im = ic->methods [l];
1040 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1043 for (j = 0; j < class->method.count; ++j) {
1044 MonoMethod *cm = class->methods [j];
1045 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1046 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
1047 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
1049 if (!strcmp(cm->name, im->name) &&
1050 mono_metadata_signature_equal (cm->signature, im->signature)) {
1051 g_assert (io + l <= max_vtsize);
1052 vtable [io + l] = cm;
1057 /* already implemented */
1058 if (io >= k->vtable_size)
1062 for (l = 0; l < ic->method.count; l++) {
1063 MonoMethod *im = ic->methods [l];
1066 g_assert (io + l <= max_vtsize);
1068 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1071 for (k1 = class; k1; k1 = k1->parent) {
1072 for (j = 0; j < k1->method.count; ++j) {
1073 MonoMethod *cm = k1->methods [j];
1075 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1076 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
1079 if (!strcmp(cm->name, im->name) &&
1080 mono_metadata_signature_equal (cm->signature, im->signature)) {
1081 g_assert (io + l <= max_vtsize);
1082 vtable [io + l] = cm;
1087 g_assert (io + l <= max_vtsize);
1088 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1093 for (l = 0; l < ic->method.count; l++) {
1094 MonoMethod *im = ic->methods [l];
1095 char *qname, *fqname, *cname, *the_cname;
1098 if (vtable [io + l])
1101 if (ic->generic_inst) {
1102 MonoClass *the_ic = mono_class_from_mono_type (ic->generic_inst->generic_type);
1103 the_cname = _mono_type_get_name (&the_ic->byval_arg, TRUE, FALSE, TRUE);
1107 cname = (char*)ic->name;
1110 qname = g_strconcat (cname, ".", im->name, NULL);
1111 if (ic->name_space && ic->name_space [0])
1112 fqname = g_strconcat (ic->name_space, ".", cname, ".", im->name, NULL);
1116 for (k1 = class; k1; k1 = k1->parent) {
1117 for (j = 0; j < k1->method.count; ++j) {
1118 MonoMethod *cm = k1->methods [j];
1120 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1123 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
1124 mono_metadata_signature_equal (cm->signature, im->signature)) {
1125 g_assert (io + l <= max_vtsize);
1126 vtable [io + l] = cm;
1137 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
1138 for (l = 0; l < ic->method.count; l++) {
1140 MonoMethod *im = ic->methods [l];
1141 if (im->flags & METHOD_ATTRIBUTE_STATIC)
1143 g_assert (io + l <= max_vtsize);
1146 * If one of our parents already implements this interface
1147 * we can inherit the implementation.
1149 if (!(vtable [io + l])) {
1150 MonoClass *parent = class->parent;
1152 if ((ic->interface_id <= parent->max_interface_id) &&
1153 (parent->interface_offsets [ic->interface_id]) &&
1155 vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
1158 if (!(vtable [io + l])) {
1159 for (j = 0; j < onum; ++j) {
1160 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
1161 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
1163 msig = mono_signature_get_desc (im->signature, FALSE);
1164 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
1165 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
1167 for (j = 0; j < class->method.count; ++j) {
1168 MonoMethod *cm = class->methods [j];
1169 msig = mono_signature_get_desc (cm->signature, FALSE);
1171 printf ("METHOD %s(%s)\n", cm->name, msig);
1174 g_assert_not_reached ();
1179 for (l = 0; l < ic->method.count; l++) {
1180 MonoMethod *im = vtable [io + l];
1183 g_assert (io + l <= max_vtsize);
1185 /* FIXME: why do we need this ? */
1187 /* g_assert_not_reached (); */
1193 g_ptr_array_free (ifaces, TRUE);
1196 for (i = 0; i < class->method.count; ++i) {
1199 cm = class->methods [i];
1202 * Non-virtual method have no place in the vtable.
1203 * This also catches static methods (since they are not virtual).
1205 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1209 * If the method is REUSE_SLOT, we must check in the
1210 * base class for a method to override.
1212 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
1214 for (k = class->parent; k ; k = k->parent) {
1216 for (j = 0; j < k->method.count; ++j) {
1217 MonoMethod *m1 = k->methods [j];
1218 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
1220 if (!strcmp(cm->name, m1->name) &&
1221 mono_metadata_signature_equal (cm->signature, m1->signature)) {
1222 slot = k->methods [j]->slot;
1223 g_assert (cm->slot < max_vtsize);
1225 override_map = mono_g_hash_table_new (NULL, NULL);
1226 mono_g_hash_table_insert (override_map, m1, cm);
1238 cm->slot = cur_slot++;
1240 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT) && !cm->signature->generic_param_count)
1241 vtable [cm->slot] = cm;
1244 /* override non interface methods */
1245 for (i = 0; i < onum; i++) {
1246 MonoMethod *decl = overrides [i*2];
1247 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
1248 g_assert (decl->slot != -1);
1249 vtable [decl->slot] = overrides [i*2 + 1];
1250 overrides [i * 2 + 1]->slot = decl->slot;
1252 override_map = mono_g_hash_table_new (NULL, NULL);
1253 mono_g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
1258 * If a method occupies more than one place in the vtable, and it is
1259 * overriden, then change the other occurances too.
1262 for (i = 0; i < max_vtsize; ++i)
1264 MonoMethod *cm = mono_g_hash_table_lookup (override_map, vtable [i]);
1269 mono_g_hash_table_destroy (override_map);
1272 if (class->generic_inst) {
1273 MonoClass *gklass = mono_class_from_mono_type (class->generic_inst->generic_type);
1275 mono_class_init (gklass);
1276 class->vtable_size = gklass->vtable_size;
1278 class->vtable_size = cur_slot;
1280 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
1281 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
1283 if (mono_print_vtable) {
1286 for (i = 0; i <= max_iid; i++)
1287 if (class->interface_offsets [i] != -1)
1290 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
1291 class->name, class->vtable_size, icount);
1293 for (i = 0; i < class->vtable_size; ++i) {
1298 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
1299 cm->klass->name_space, cm->klass->name,
1306 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
1307 class->name, max_iid);
1309 for (i = 0; i < class->interface_count; i++) {
1310 ic = class->interfaces [i];
1311 printf (" slot %03d(%03d) %s.%s\n",
1312 class->interface_offsets [ic->interface_id],
1313 ic->method.count, ic->name_space, ic->name);
1316 for (k = class->parent; k ; k = k->parent) {
1317 for (i = 0; i < k->interface_count; i++) {
1318 ic = k->interfaces [i];
1319 printf (" slot %03d(%03d) %s.%s\n",
1320 class->interface_offsets [ic->interface_id],
1321 ic->method.count, ic->name_space, ic->name);
1330 * @class: the class to initialize
1332 * compute the instance_size, class_size and other infos that cannot be
1333 * computed at mono_class_get() time. Also compute a generic vtable and
1334 * the method slot numbers. We use this infos later to create a domain
1338 mono_class_init (MonoClass *class)
1341 static MonoMethod *default_ghc = NULL;
1342 static MonoMethod *default_finalize = NULL;
1343 static int finalize_slot = -1;
1344 static int ghc_slot = -1;
1345 MonoMethod **overrides;
1353 /*g_print ("Init class %s\n", class->name);*/
1355 /* We do everything inside the lock to prevent races */
1356 mono_loader_lock ();
1358 if (class->inited) {
1359 mono_loader_unlock ();
1360 /* Somebody might have gotten in before us */
1364 if (class->init_pending) {
1365 /* this indicates a cyclic dependency */
1366 g_error ("pending init %s.%s\n", class->name_space, class->name);
1369 class->init_pending = 1;
1371 if (mono_debugger_start_class_init_func)
1372 mono_debugger_start_class_init_func (class);
1374 mono_stats.initialized_class_count++;
1376 if (class->generic_inst && !class->generic_inst->is_dynamic) {
1377 MonoGenericInst *ginst = class->generic_inst;
1380 gklass = mono_class_from_mono_type (ginst->generic_type);
1381 mono_class_init (gklass);
1383 if (MONO_CLASS_IS_INTERFACE (class))
1384 class->interface_id = mono_get_unique_iid (class);
1386 g_assert (class->method.count == gklass->method.count);
1387 class->methods = g_new0 (MonoMethod *, class->method.count);
1389 for (i = 0; i < class->method.count; i++)
1390 class->methods [i] = mono_class_inflate_generic_method (
1391 gklass->methods [i], ginst->context, ginst->klass);
1393 g_assert (class->field.count == gklass->field.count);
1394 class->fields = g_new0 (MonoClassField, class->field.count);
1396 for (i = 0; i < class->field.count; i++) {
1397 MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
1398 ifield->generic_type = gklass->fields [i].type;
1400 class->fields [i] = gklass->fields [i];
1401 class->fields [i].generic_info = ifield;
1402 class->fields [i].parent = class;
1403 class->fields [i].type = mono_class_inflate_generic_type (
1404 class->fields [i].type, ginst->context);
1407 class->property = gklass->property;
1408 class->properties = g_new0 (MonoProperty, class->property.count);
1410 for (i = 0; i < class->property.count; i++) {
1411 MonoProperty *prop = &class->properties [i];
1413 *prop = gklass->properties [i];
1416 prop->get = mono_class_inflate_generic_method (
1417 prop->get, ginst->context, ginst->klass);
1419 prop->set = mono_class_inflate_generic_method (
1420 prop->set, ginst->context, ginst->klass);
1422 prop->parent = class;
1425 g_assert (class->interface_count == gklass->interface_count);
1428 if (class->parent && !class->parent->inited)
1429 mono_class_init (class->parent);
1432 * Computes the size used by the fields, and their locations
1434 if (!class->size_inited)
1435 class_compute_field_layout (class);
1437 /* initialize method pointers */
1440 MonoMethodSignature *sig;
1441 class->method.count = class->rank > 1? 2: 1;
1442 sig = mono_metadata_signature_alloc (class->image, class->rank);
1443 sig->ret = &mono_defaults.void_class->byval_arg;
1444 sig->pinvoke = TRUE;
1445 for (i = 0; i < class->rank; ++i)
1446 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1448 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1449 ctor->klass = class;
1450 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1451 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1452 ctor->signature = sig;
1453 ctor->name = ".ctor";
1455 class->methods = g_new (MonoMethod*, class->method.count);
1456 class->methods [0] = ctor;
1457 if (class->rank > 1) {
1458 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
1459 sig->ret = &mono_defaults.void_class->byval_arg;
1460 sig->pinvoke = TRUE;
1461 for (i = 0; i < class->rank * 2; ++i)
1462 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1464 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1465 ctor->klass = class;
1466 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1467 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1468 ctor->signature = sig;
1469 ctor->name = ".ctor";
1471 class->methods [1] = ctor;
1474 if (!class->generic_inst && !class->methods) {
1475 class->methods = g_new (MonoMethod*, class->method.count);
1476 for (i = 0; i < class->method.count; ++i) {
1477 class->methods [i] = mono_get_method (class->image,
1478 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
1483 if (!class->generic_inst) {
1484 init_properties (class);
1485 init_events (class);
1487 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
1490 guint32 cols [MONO_NESTED_CLASS_SIZE];
1491 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
1492 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
1493 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
1495 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
1499 mono_class_setup_supertypes (class);
1501 if (MONO_CLASS_IS_INTERFACE (class)) {
1502 for (i = 0; i < class->method.count; ++i)
1503 class->methods [i]->slot = i;
1504 class->init_pending = 0;
1507 * class->interface_offsets is needed for the castclass/isinst code, so
1508 * we have to setup them for interfaces, too.
1510 setup_interface_offsets (class, 0);
1511 mono_loader_unlock ();
1513 if (mono_debugger_class_init_func)
1514 mono_debugger_class_init_func (class);
1519 overrides = mono_class_get_overrides (class->image, class->type_token, &onum);
1520 mono_class_setup_vtable (class, overrides, onum);
1524 class->init_pending = 0;
1527 if (class == mono_defaults.object_class) {
1529 for (i = 0; i < class->vtable_size; ++i) {
1530 MonoMethod *cm = class->vtable [i];
1532 if (!strcmp (cm->name, "GetHashCode")) {
1538 g_assert (ghc_slot > 0);
1540 default_ghc = class->vtable [ghc_slot];
1545 if (class->parent) {
1547 if (class->vtable [ghc_slot] == default_ghc) {
1552 if (!default_finalize) {
1553 if (class == mono_defaults.object_class) {
1555 for (i = 0; i < class->vtable_size; ++i) {
1556 MonoMethod *cm = class->vtable [i];
1558 if (!strcmp (cm->name, "Finalize")) {
1564 g_assert (finalize_slot > 0);
1566 default_finalize = class->vtable [finalize_slot];
1570 /* Object::Finalize should have empty implemenatation */
1571 class->has_finalize = 0;
1572 if (class->parent) {
1573 if (class->vtable [finalize_slot] != default_finalize)
1574 class->has_finalize = 1;
1577 mono_loader_unlock ();
1579 if (mono_debugger_class_init_func)
1580 mono_debugger_class_init_func (class);
1584 mono_class_setup_mono_type (MonoClass *class)
1586 const char *name = class->name;
1587 const char *nspace = class->name_space;
1589 if (MONO_CLASS_IS_INTERFACE (class))
1590 class->interface_id = mono_get_unique_iid (class);
1592 class->this_arg.byref = 1;
1593 class->this_arg.data.klass = class;
1594 class->this_arg.type = MONO_TYPE_CLASS;
1595 class->byval_arg.data.klass = class;
1596 class->byval_arg.type = MONO_TYPE_CLASS;
1598 if (!strcmp (nspace, "System")) {
1599 if (!strcmp (name, "ValueType")) {
1601 * do not set the valuetype bit for System.ValueType.
1602 * class->valuetype = 1;
1604 class->blittable = TRUE;
1605 } else if (!strcmp (name, "Enum")) {
1607 * do not set the valuetype bit for System.Enum.
1608 * class->valuetype = 1;
1610 class->valuetype = 0;
1611 class->enumtype = 0;
1612 } else if (!strcmp (name, "Object")) {
1613 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
1614 } else if (!strcmp (name, "String")) {
1615 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
1616 } else if (!strcmp (name, "TypedReference")) {
1617 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
1621 if (class->valuetype) {
1622 int t = MONO_TYPE_VALUETYPE;
1623 if (!strcmp (nspace, "System")) {
1626 if (!strcmp (name, "Boolean")) {
1627 t = MONO_TYPE_BOOLEAN;
1628 } else if (!strcmp(name, "Byte")) {
1630 class->blittable = TRUE;
1634 if (!strcmp (name, "Char")) {
1639 if (!strcmp (name, "Double")) {
1641 class->blittable = TRUE;
1645 if (!strcmp (name, "Int32")) {
1647 class->blittable = TRUE;
1648 } else if (!strcmp(name, "Int16")) {
1650 class->blittable = TRUE;
1651 } else if (!strcmp(name, "Int64")) {
1653 class->blittable = TRUE;
1654 } else if (!strcmp(name, "IntPtr")) {
1656 class->blittable = TRUE;
1660 if (!strcmp (name, "Single")) {
1662 class->blittable = TRUE;
1663 } else if (!strcmp(name, "SByte")) {
1665 class->blittable = TRUE;
1669 if (!strcmp (name, "UInt32")) {
1671 class->blittable = TRUE;
1672 } else if (!strcmp(name, "UInt16")) {
1674 class->blittable = TRUE;
1675 } else if (!strcmp(name, "UInt64")) {
1677 class->blittable = TRUE;
1678 } else if (!strcmp(name, "UIntPtr")) {
1680 class->blittable = TRUE;
1684 if (!strcmp (name, "TypedReference")) {
1685 t = MONO_TYPE_TYPEDBYREF;
1686 class->blittable = TRUE;
1690 if (!strcmp (name, "Void")) {
1698 class->this_arg.type = class->byval_arg.type = t;
1703 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
1705 gboolean system_namespace;
1707 system_namespace = !strcmp (class->name_space, "System");
1709 /* if root of the hierarchy */
1710 if (system_namespace && !strcmp (class->name, "Object")) {
1711 class->parent = NULL;
1712 class->instance_size = sizeof (MonoObject);
1715 if (!strcmp (class->name, "<Module>")) {
1716 class->parent = NULL;
1717 class->instance_size = 0;
1721 if (!MONO_CLASS_IS_INTERFACE (class)) {
1722 class->parent = parent;
1725 g_assert_not_reached (); /* FIXME */
1727 if (parent->generic_inst && !parent->name) {
1729 * If the parent is a generic instance, we may get
1730 * called before it is fully initialized, especially
1731 * before it has its name.
1736 class->marshalbyref = parent->marshalbyref;
1737 class->contextbound = parent->contextbound;
1738 class->delegate = parent->delegate;
1740 if (system_namespace) {
1741 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
1742 class->marshalbyref = 1;
1744 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
1745 class->contextbound = 1;
1747 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
1748 class->delegate = 1;
1751 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
1752 (strcmp (class->parent->name_space, "System") == 0)))
1753 class->valuetype = 1;
1754 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
1755 class->valuetype = class->enumtype = 1;
1757 /*class->enumtype = class->parent->enumtype; */
1758 mono_class_setup_supertypes (class);
1760 class->parent = NULL;
1766 mono_class_setup_supertypes (MonoClass *class)
1771 if (class->supertypes)
1775 for (k = class; k ; k = k->parent) {
1779 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
1780 class->supertypes = g_new0 (MonoClass *, ms);
1782 if (class->parent) {
1783 for (i = class->idepth, k = class; k ; k = k->parent)
1784 class->supertypes [--i] = k;
1786 class->supertypes [0] = class;
1791 * If we inherit a type parameter from an outer class, set its owner to that class.
1794 set_generic_param_owner (MonoGenericContainer *container, MonoClass *klass, int pos)
1796 MonoGenericContainer *gc;
1799 if (klass->nested_in)
1800 pos = set_generic_param_owner (container, klass->nested_in, pos);
1802 if (!klass->generic_container)
1805 gc = klass->generic_container;
1806 for (i = pos; i < gc->type_argc; i++)
1807 container->type_params [i].owner = gc;
1809 return pos + gc->type_argc;
1813 * @image: context where the image is created
1814 * @type_token: typedef token
1817 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
1819 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
1820 MonoClass *class, *parent = NULL;
1821 guint32 cols [MONO_TYPEDEF_SIZE];
1822 guint32 cols_next [MONO_TYPEDEF_SIZE];
1823 guint tidx = mono_metadata_token_index (type_token);
1824 MonoGenericContext *context = NULL;
1825 const char *name, *nspace;
1827 MonoClass **interfaces;
1829 mono_loader_lock ();
1831 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
1832 mono_loader_unlock ();
1836 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
1838 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
1840 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1841 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1843 class = g_malloc0 (sizeof (MonoClass));
1846 class->name_space = nspace;
1848 class->image = image;
1849 class->type_token = type_token;
1850 class->flags = cols [MONO_TYPEDEF_FLAGS];
1852 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
1854 class->generic_container = mono_metadata_load_generic_params (image, class->type_token);
1855 if (class->generic_container) {
1856 class->generic_container->klass = class;
1857 context = g_new0 (MonoGenericContext, 1);
1858 context->container = class->generic_container;
1861 if (cols [MONO_TYPEDEF_EXTENDS])
1862 parent = mono_class_get_full (
1863 image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]), context);
1864 interfaces = mono_metadata_interfaces_from_typedef_full (image, type_token, &icount, context);
1866 class->interfaces = interfaces;
1867 class->interface_count = icount;
1869 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
1871 /* fixme: maybe we must set this on windows
1872 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
1876 class->cast_class = class->element_class = class;
1878 /*g_print ("Load class %s\n", name);*/
1880 mono_class_setup_parent (class, parent);
1882 mono_class_setup_mono_type (class);
1885 * Compute the field and method lists
1887 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
1888 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
1890 if (tt->rows > tidx){
1891 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
1892 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1893 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1895 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
1896 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
1899 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
1900 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
1901 class->field.count = class->field.last - class->field.first;
1903 class->field.count = 0;
1905 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
1906 class->method.count = class->method.last - class->method.first;
1908 class->method.count = 0;
1910 /* reserve space to store vector pointer in arrays */
1911 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
1912 class->instance_size += 2 * sizeof (gpointer);
1913 g_assert (class->field.count == 0);
1916 if (class->enumtype)
1917 class_compute_field_layout (class);
1919 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
1920 class->nested_in = mono_class_create_from_typedef (image, type_token);
1922 if (class->nested_in && class->generic_container)
1923 set_generic_param_owner (class->generic_container, class->nested_in, 0);
1925 mono_loader_unlock ();
1931 mono_class_create_generic (MonoGenericInst *ginst)
1933 MonoClass *klass, *gklass;
1936 ginst->klass = g_malloc0 (sizeof (MonoClass));
1937 klass = ginst->klass;
1939 gklass = mono_class_from_mono_type (ginst->generic_type);
1941 klass->nested_in = gklass->nested_in;
1943 klass->name = gklass->name;
1944 klass->name_space = gklass->name_space;
1945 klass->image = gklass->image;
1946 klass->flags = gklass->flags;
1948 klass->generic_inst = ginst;
1950 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
1951 klass->this_arg.data.generic_inst = klass->byval_arg.data.generic_inst = ginst;
1952 klass->this_arg.byref = TRUE;
1954 klass->cast_class = klass->element_class = klass;
1956 if (ginst->is_dynamic) {
1957 klass->instance_size = gklass->instance_size;
1958 klass->class_size = gklass->class_size;
1959 klass->size_inited = 1;
1962 klass->valuetype = gklass->valuetype;
1967 mono_class_create_generic_2 (MonoGenericInst *ginst)
1969 MonoClass *klass, *gklass;
1973 if (ginst->is_dynamic)
1976 klass = ginst->klass;
1977 gklass = mono_class_from_mono_type (ginst->generic_type);
1979 klass->method = gklass->method;
1980 klass->field = gklass->field;
1982 klass->interface_count = gklass->interface_count;
1983 klass->interfaces = g_new0 (MonoClass *, klass->interface_count);
1984 for (i = 0; i < klass->interface_count; i++) {
1985 MonoType *it = &gklass->interfaces [i]->byval_arg;
1986 MonoType *inflated = mono_class_inflate_generic_type (it, ginst->context);
1987 klass->interfaces [i] = mono_class_from_mono_type (inflated);
1990 for (list = gklass->nested_classes; list; list = list->next)
1991 klass->nested_classes = g_list_append (
1992 klass->nested_classes, list->data);
1995 klass->parent = mono_class_from_mono_type (ginst->parent);
1996 else if (gklass->parent) {
1997 MonoType *inflated = mono_class_inflate_generic_type (&gklass->parent->byval_arg, ginst->context);
1999 klass->parent = mono_class_from_mono_type (inflated);
2002 mono_class_setup_parent (klass, klass->parent);
2006 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
2008 MonoClass *klass, **ptr;
2012 return param->pklass;
2014 klass = param->pklass = g_new0 (MonoClass, 1);
2016 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
2020 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
2021 klass->parent = param->constraints [0];
2025 if (count - pos > 0) {
2026 klass->interface_count = count - pos;
2027 klass->interfaces = g_new0 (MonoClass *, count - pos);
2028 for (i = pos; i < count; i++)
2029 klass->interfaces [i - pos] = param->constraints [i];
2032 g_assert (param->name && param->owner);
2034 klass->name = param->name;
2035 klass->name_space = "";
2036 klass->image = image;
2037 klass->cast_class = klass->element_class = klass;
2038 klass->enum_basetype = &klass->element_class->byval_arg;
2039 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2041 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2042 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2043 klass->this_arg.byref = TRUE;
2045 mono_class_init (klass);
2051 my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
2056 return param->pklass;
2058 g_assert (param->owner);
2060 klass = g_new0 (MonoClass, 1);
2063 klass->name = param->name;
2065 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
2066 klass->name_space = "";
2067 klass->image = mono_defaults.corlib;
2068 klass->cast_class = klass->element_class = klass;
2069 klass->enum_basetype = &klass->element_class->byval_arg;
2070 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2072 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2073 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2074 klass->this_arg.byref = TRUE;
2076 mono_class_init (klass);
2082 mono_ptr_class_get (MonoType *type)
2085 MonoClass *el_class;
2086 static GHashTable *ptr_hash = NULL;
2088 mono_loader_lock ();
2091 ptr_hash = g_hash_table_new (NULL, NULL);
2092 el_class = mono_class_from_mono_type (type);
2093 if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
2094 mono_loader_unlock ();
2097 result = g_new0 (MonoClass, 1);
2099 result->parent = NULL; /* no parent for PTR types */
2100 result->name_space = el_class->name_space;
2101 result->name = g_strdup_printf ("%s*", el_class->name);
2102 result->image = el_class->image;
2103 result->inited = TRUE;
2104 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2105 /* Can pointers get boxed? */
2106 result->instance_size = sizeof (gpointer);
2107 result->cast_class = result->element_class = el_class;
2108 result->enum_basetype = &result->element_class->byval_arg;
2109 result->blittable = TRUE;
2111 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
2112 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
2113 result->this_arg.byref = TRUE;
2115 mono_class_setup_supertypes (result);
2117 g_hash_table_insert (ptr_hash, el_class, result);
2119 mono_loader_unlock ();
2125 mono_fnptr_class_get (MonoMethodSignature *sig)
2128 static GHashTable *ptr_hash = NULL;
2130 mono_loader_lock ();
2133 ptr_hash = g_hash_table_new (NULL, NULL);
2135 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
2136 mono_loader_unlock ();
2139 result = g_new0 (MonoClass, 1);
2141 result->parent = NULL; /* no parent for PTR types */
2142 result->name = "System";
2143 result->name_space = "MonoFNPtrFakeClass";
2144 result->image = NULL; /* need to fix... */
2145 result->inited = TRUE;
2146 result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
2147 /* Can pointers get boxed? */
2148 result->instance_size = sizeof (gpointer);
2149 result->cast_class = result->element_class = result;
2150 result->blittable = TRUE;
2152 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
2153 result->this_arg.data.method = result->byval_arg.data.method = sig;
2154 result->this_arg.byref = TRUE;
2155 result->enum_basetype = &result->element_class->byval_arg;
2156 result->blittable = TRUE;
2158 mono_class_setup_supertypes (result);
2160 g_hash_table_insert (ptr_hash, sig, result);
2162 mono_loader_unlock ();
2168 mono_class_from_mono_type (MonoType *type)
2170 switch (type->type) {
2171 case MONO_TYPE_OBJECT:
2172 return type->data.klass? type->data.klass: mono_defaults.object_class;
2173 case MONO_TYPE_VOID:
2174 return type->data.klass? type->data.klass: mono_defaults.void_class;
2175 case MONO_TYPE_BOOLEAN:
2176 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
2177 case MONO_TYPE_CHAR:
2178 return type->data.klass? type->data.klass: mono_defaults.char_class;
2180 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
2182 return type->data.klass? type->data.klass: mono_defaults.byte_class;
2184 return type->data.klass? type->data.klass: mono_defaults.int16_class;
2186 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
2188 return type->data.klass? type->data.klass: mono_defaults.int32_class;
2190 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
2192 return type->data.klass? type->data.klass: mono_defaults.int_class;
2194 return type->data.klass? type->data.klass: mono_defaults.uint_class;
2196 return type->data.klass? type->data.klass: mono_defaults.int64_class;
2198 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
2200 return type->data.klass? type->data.klass: mono_defaults.single_class;
2202 return type->data.klass? type->data.klass: mono_defaults.double_class;
2203 case MONO_TYPE_STRING:
2204 return type->data.klass? type->data.klass: mono_defaults.string_class;
2205 case MONO_TYPE_TYPEDBYREF:
2206 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
2207 case MONO_TYPE_ARRAY:
2208 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
2210 return mono_ptr_class_get (type->data.type);
2211 case MONO_TYPE_FNPTR:
2212 return mono_fnptr_class_get (type->data.method);
2213 case MONO_TYPE_SZARRAY:
2214 return mono_array_class_get (type->data.klass, 1);
2215 case MONO_TYPE_CLASS:
2216 case MONO_TYPE_VALUETYPE:
2217 return type->data.klass;
2218 case MONO_TYPE_GENERICINST:
2219 g_assert (type->data.generic_inst->klass);
2220 return type->data.generic_inst->klass;
2222 return my_mono_class_from_generic_parameter (type->data.generic_param, FALSE);
2223 case MONO_TYPE_MVAR:
2224 return my_mono_class_from_generic_parameter (type->data.generic_param, TRUE);
2226 g_warning ("implement me 0x%02x\n", type->type);
2227 g_assert_not_reached ();
2234 * @image: context where the image is created
2235 * @type_spec: typespec token
2238 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec,
2239 MonoGenericContext *context)
2241 MonoType *type, *inflated;
2242 MonoGenericContainer *container = NULL;
2246 if (context->gmethod && context->gmethod->container) {
2247 g_assert (context->gmethod->container);
2248 container = context->gmethod->container;
2249 } else if (context->ginst) {
2250 g_assert (context->ginst->container);
2251 container = context->ginst->container;
2252 } else if (context->container)
2253 container = context->container;
2256 type = mono_type_create_from_typespec_full (image, container, type_spec);
2258 switch (type->type) {
2259 case MONO_TYPE_ARRAY:
2260 class = mono_array_class_get (type->data.array->eklass, type->data.array->rank);
2262 case MONO_TYPE_SZARRAY:
2263 class = mono_array_class_get (type->data.klass, 1);
2266 class = mono_ptr_class_get (type->data.type);
2268 case MONO_TYPE_GENERICINST:
2269 g_assert (type->data.generic_inst->klass);
2270 class = type->data.generic_inst->klass;
2273 /* it seems any type can be stored in TypeSpec as well */
2274 class = mono_class_from_mono_type (type);
2278 if (!class || !context)
2281 inflated = mono_class_inflate_generic_type (&class->byval_arg, context);
2283 return mono_class_from_mono_type (inflated);
2287 * mono_bounded_array_class_get:
2288 * @element_class: element class
2289 * @rank: the dimension of the array class
2290 * @bounded: whenever the array has non-zero bounds
2292 * Returns: a class object describing the array with element type @element_type and
2296 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
2300 MonoClass *parent = NULL;
2301 GSList *list, *rootlist;
2304 gboolean corlib_type = FALSE;
2306 g_assert (rank <= 255);
2309 /* bounded only matters for one-dimensional arrays */
2312 image = eclass->image;
2314 mono_loader_lock ();
2316 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
2317 for (; list; list = list->next) {
2319 if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
2320 mono_loader_unlock ();
2326 /* for the building corlib use System.Array from it */
2327 if (image->assembly && image->assembly->dynamic && strcmp (image->assembly_name, "mscorlib") == 0) {
2328 parent = mono_class_from_name (image, "System", "Array");
2331 parent = mono_defaults.array_class;
2332 if (!parent->inited)
2333 mono_class_init (parent);
2336 class = g_malloc0 (sizeof (MonoClass));
2338 class->image = image;
2339 class->name_space = eclass->name_space;
2340 nsize = strlen (eclass->name);
2341 name = g_malloc (nsize + 2 + rank);
2342 memcpy (name, eclass->name, nsize);
2345 memset (name + nsize + 1, ',', rank - 1);
2346 name [nsize + rank] = ']';
2347 name [nsize + rank + 1] = 0;
2349 class->type_token = 0;
2350 /* all arrays are marked serializable and sealed, bug #42779 */
2351 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
2352 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2353 class->parent = parent;
2354 class->instance_size = mono_class_instance_size (class->parent);
2355 class->class_size = 0;
2356 mono_class_setup_supertypes (class);
2360 if (eclass->enumtype)
2361 class->cast_class = eclass->element_class;
2363 class->cast_class = eclass;
2365 class->element_class = eclass;
2367 if ((rank > 1) || bounded) {
2368 MonoArrayType *at = g_new0 (MonoArrayType, 1);
2369 class->byval_arg.type = MONO_TYPE_ARRAY;
2370 class->byval_arg.data.array = at;
2371 at->eklass = eclass;
2373 /* FIXME: complete.... */
2375 class->byval_arg.type = MONO_TYPE_SZARRAY;
2376 class->byval_arg.data.klass = eclass;
2378 class->this_arg = class->byval_arg;
2379 class->this_arg.byref = 1;
2384 list = g_slist_append (rootlist, class);
2385 g_hash_table_insert (image->array_cache, eclass, list);
2387 mono_loader_unlock ();
2393 * mono_array_class_get:
2394 * @element_class: element class
2395 * @rank: the dimension of the array class
2397 * Returns: a class object describing the array with element type @element_type and
2401 mono_array_class_get (MonoClass *eclass, guint32 rank)
2403 return mono_bounded_array_class_get (eclass, rank, FALSE);
2407 * mono_class_instance_size:
2410 * Returns: the size of an object instance
2413 mono_class_instance_size (MonoClass *klass)
2415 if (!klass->size_inited)
2416 mono_class_init (klass);
2418 g_assert (!klass->generic_container &&
2419 (!klass->generic_inst || !klass->generic_inst->is_open));
2420 return klass->instance_size;
2424 * mono_class_min_align:
2427 * Returns: minimm alignment requirements
2430 mono_class_min_align (MonoClass *klass)
2432 if (!klass->size_inited)
2433 mono_class_init (klass);
2435 return klass->min_align;
2439 * mono_class_value_size:
2442 * This function is used for value types, and return the
2443 * space and the alignment to store that kind of value object.
2445 * Returns: the size of a value of kind @klass
2448 mono_class_value_size (MonoClass *klass, guint32 *align)
2452 /* fixme: check disable, because we still have external revereces to
2453 * mscorlib and Dummy Objects
2455 /*g_assert (klass->valuetype);*/
2457 size = mono_class_instance_size (klass) - sizeof (MonoObject);
2460 *align = klass->min_align;
2466 * mono_class_data_size:
2469 * Returns: the size of the static class data
2472 mono_class_data_size (MonoClass *klass)
2475 mono_class_init (klass);
2477 return klass->class_size;
2481 * Auxiliary routine to mono_class_get_field
2483 * Takes a field index instead of a field token.
2485 static MonoClassField *
2486 mono_class_get_field_idx (MonoClass *class, int idx)
2488 if (class->field.count){
2489 if ((idx >= class->field.first) && (idx < class->field.last)){
2490 return &class->fields [idx - class->field.first];
2497 return mono_class_get_field_idx (class->parent, idx);
2501 * mono_class_get_field:
2502 * @class: the class to lookup the field.
2503 * @field_token: the field token
2505 * Returns: A MonoClassField representing the type and offset of
2506 * the field, or a NULL value if the field does not belong to this
2510 mono_class_get_field (MonoClass *class, guint32 field_token)
2512 int idx = mono_metadata_token_index (field_token);
2514 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
2516 return mono_class_get_field_idx (class, idx - 1);
2520 mono_class_get_field_from_name (MonoClass *klass, const char *name)
2525 for (i = 0; i < klass->field.count; ++i) {
2526 if (strcmp (name, klass->fields [i].name) == 0)
2527 return &klass->fields [i];
2529 klass = klass->parent;
2535 mono_class_get_field_token (MonoClassField *field)
2537 MonoClass *klass = field->parent;
2541 for (i = 0; i < klass->field.count; ++i) {
2542 if (&klass->fields [i] == field)
2543 return mono_metadata_make_token (MONO_TABLE_FIELD, klass->field.first + i + 1);
2545 klass = klass->parent;
2548 g_assert_not_reached ();
2553 mono_class_get_event_token (MonoEvent *event)
2555 MonoClass *klass = event->parent;
2559 for (i = 0; i < klass->event.count; ++i) {
2560 if (&klass->events [i] == event)
2561 return mono_metadata_make_token (MONO_TABLE_EVENT, klass->event.first + i + 1);
2563 klass = klass->parent;
2566 g_assert_not_reached ();
2571 mono_vtable_get_static_field_data (MonoVTable *vt)
2577 mono_class_get_property_from_name (MonoClass *klass, const char *name)
2582 for (i = 0; i < klass->property.count; ++i) {
2583 if (strcmp (name, klass->properties [i].name) == 0)
2584 return &klass->properties [i];
2586 klass = klass->parent;
2592 mono_class_get_property_token (MonoProperty *prop)
2594 MonoClass *klass = prop->parent;
2598 for (i = 0; i < klass->property.count; ++i) {
2599 if (&klass->properties [i] == prop)
2600 return mono_metadata_make_token (MONO_TABLE_PROPERTY, klass->property.first + i + 1);
2602 klass = klass->parent;
2605 g_assert_not_reached ();
2611 * @image: the image where the class resides
2612 * @type_token: the token for the class
2613 * @at: an optional pointer to return the array element type
2615 * Returns: the MonoClass that represents @type_token in @image
2618 _mono_class_get (MonoImage *image, guint32 type_token, MonoGenericContext *context)
2620 MonoClass *class = NULL;
2623 return mono_lookup_dynamic_token (image, type_token);
2625 switch (type_token & 0xff000000){
2626 case MONO_TOKEN_TYPE_DEF:
2627 class = mono_class_create_from_typedef (image, type_token);
2629 case MONO_TOKEN_TYPE_REF:
2630 class = mono_class_from_typeref (image, type_token);
2632 case MONO_TOKEN_TYPE_SPEC:
2633 class = mono_class_create_from_typespec (image, type_token, context);
2636 g_warning ("unknown token type %x", type_token & 0xff000000);
2637 g_assert_not_reached ();
2641 g_warning ("Could not load class from token 0x%08x in %s", type_token, image->name);
2647 mono_class_get (MonoImage *image, guint32 type_token)
2649 return _mono_class_get (image, type_token, NULL);
2653 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
2655 MonoClass *class = _mono_class_get (image, type_token, context);
2658 if (!class || !context || (!context->ginst && !context->gmethod))
2661 switch (class->byval_arg.type) {
2662 case MONO_TYPE_GENERICINST:
2663 if (!class->generic_inst->is_open)
2667 case MONO_TYPE_MVAR:
2673 inflated = inflate_generic_type (&class->byval_arg, context);
2677 return mono_class_from_mono_type (inflated);
2681 * mono_class_from_name_case:
2682 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
2683 * @name_space: the type namespace
2684 * @name: the type short name.
2686 * Obtains a MonoClass with a given namespace and a given name which
2687 * is located in the given MonoImage. The namespace and name
2688 * lookups are case insensitive.
2690 * You can also pass `NULL' to the image, and that will lookup for
2691 * a type with the given namespace and name in all of the loaded
2692 * assemblies: notice that since there might be a name clash in this
2693 * case, passing NULL is not encouraged if you need a precise type.
2697 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
2699 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
2700 guint32 cols [MONO_TYPEDEF_SIZE];
2705 /* add a cache if needed */
2706 for (i = 1; i <= t->rows; ++i) {
2707 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
2708 /* nested types are accessed from the nesting name */
2709 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
2710 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
2712 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2713 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2714 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
2715 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
2721 return_nested_in (MonoClass *class, char *nested) {
2723 char *s = strchr (nested, '/');
2730 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
2732 if (strcmp (found->name, nested) == 0) {
2734 return return_nested_in (found, s);
2743 * mono_class_from_name_case:
2744 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
2745 * @name_space: the type namespace
2746 * @name: the type short name.
2748 * Obtains a MonoClass with a given namespace and a given name which
2749 * is located in the given MonoImage.
2751 * You can also pass `NULL' to the image, and that will lookup for
2752 * a type with the given namespace and name in all of the loaded
2753 * assemblies: notice that since there might be a name clash in this
2754 * case, passing NULL is not encouraged if you need a precise type.
2758 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
2760 GHashTable *nspace_table;
2761 MonoImage *loaded_image;
2767 if ((nested = strchr (name, '/'))) {
2768 int pos = nested - name;
2769 int len = strlen (name);
2772 memcpy (buf, name, len + 1);
2774 nested = buf + pos + 1;
2778 mono_loader_lock ();
2780 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
2783 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
2785 mono_loader_unlock ();
2790 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
2791 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
2792 guint32 cols [MONO_EXP_TYPE_SIZE];
2795 idx = mono_metadata_token_index (token);
2797 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
2799 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
2800 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
2801 loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
2804 class = mono_class_from_name (loaded_image, name_space, name);
2806 return return_nested_in (class, nested);
2809 g_error ("not yet implemented");
2813 token = MONO_TOKEN_TYPE_DEF | token;
2815 class = mono_class_get (image, token);
2817 return return_nested_in (class, nested);
2822 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
2823 gboolean check_interfaces)
2826 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
2827 if ((klassc->interface_id <= klass->max_interface_id) &&
2828 (klass->interface_offsets [klassc->interface_id] >= 0))
2830 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
2833 for (i = 0; i < klass->interface_count; i ++) {
2834 MonoClass *ic = klass->interfaces [i];
2839 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
2844 * MS.NET thinks interfaces are a subclass of Object, so we think it as
2847 if (klassc == mono_defaults.object_class)
2850 if (klass->generic_inst) {
2851 MonoType *parent = klass->generic_inst->parent;
2855 if (mono_metadata_type_equal (parent, &klassc->byval_arg))
2857 klass = mono_class_from_mono_type (parent);
2865 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
2868 mono_class_init (klass);
2870 if (!oklass->inited)
2871 mono_class_init (oklass);
2873 if (MONO_CLASS_IS_INTERFACE (klass)) {
2874 if ((klass->interface_id <= oklass->max_interface_id) &&
2875 (oklass->interface_offsets [klass->interface_id] != -1))
2879 MonoClass *eclass, *eoclass;
2881 if (oklass->rank != klass->rank)
2884 /* vectors vs. one dimensional arrays */
2885 if (oklass->byval_arg.type != klass->byval_arg.type)
2888 eclass = klass->cast_class;
2889 eoclass = oklass->cast_class;
2893 * a is b does not imply a[] is b[] when a is a valuetype, and
2894 * b is a reference type.
2897 if (eoclass->valuetype) {
2898 if ((eclass == mono_defaults.enum_class) ||
2899 (eclass == mono_defaults.enum_class->parent) ||
2900 (eclass == mono_defaults.object_class))
2904 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
2907 if (klass == mono_defaults.object_class)
2910 return mono_class_has_parent (oklass, klass);
2914 * mono_class_needs_cctor_run:
2916 * Determines whenever the class has a static constructor and whenever it
2917 * needs to be called when executing CALLER.
2920 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
2925 for (i = 0; i < klass->method.count; ++i) {
2926 method = klass->methods [i];
2927 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2928 (strcmp (".cctor", method->name) == 0)) {
2929 if (caller == method)
2938 * Returns the nnumber of bytes an element of type klass
2939 * uses when stored into an array.
2942 mono_class_array_element_size (MonoClass *klass)
2944 MonoType *type = &klass->byval_arg;
2947 switch (type->type) {
2950 case MONO_TYPE_BOOLEAN:
2954 case MONO_TYPE_CHAR:
2963 case MONO_TYPE_CLASS:
2964 case MONO_TYPE_STRING:
2965 case MONO_TYPE_OBJECT:
2966 case MONO_TYPE_SZARRAY:
2967 case MONO_TYPE_ARRAY:
2969 case MONO_TYPE_MVAR:
2970 return sizeof (gpointer);
2975 case MONO_TYPE_VALUETYPE:
2976 if (type->data.klass->enumtype) {
2977 type = type->data.klass->enum_basetype;
2978 klass = klass->element_class;
2981 return mono_class_instance_size (klass) - sizeof (MonoObject);
2982 case MONO_TYPE_GENERICINST:
2983 type = type->data.generic_inst->generic_type;
2986 g_error ("unknown type 0x%02x in mono_class_array_element_size", type->type);
2992 * mono_array_element_size:
2993 * @ac: pointer to a #MonoArrayClass
2995 * Returns: the size of single array element.
2998 mono_array_element_size (MonoClass *ac)
3000 return mono_class_array_element_size (ac->element_class);
3004 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
3005 MonoGenericContext *context)
3007 if (image->dynamic) {
3008 gpointer obj = mono_lookup_dynamic_token (image, token);
3010 switch (token & 0xff000000) {
3011 case MONO_TOKEN_TYPE_DEF:
3012 case MONO_TOKEN_TYPE_REF:
3013 case MONO_TOKEN_TYPE_SPEC:
3015 *handle_class = mono_defaults.typehandle_class;
3016 return &((MonoClass*)obj)->byval_arg;
3017 case MONO_TOKEN_METHOD_DEF:
3019 *handle_class = mono_defaults.methodhandle_class;
3021 case MONO_TOKEN_FIELD_DEF:
3023 *handle_class = mono_defaults.fieldhandle_class;
3026 g_assert_not_reached ();
3030 switch (token & 0xff000000) {
3031 case MONO_TOKEN_TYPE_DEF:
3032 case MONO_TOKEN_TYPE_REF: {
3035 *handle_class = mono_defaults.typehandle_class;
3036 class = mono_class_get_full (image, token, context);
3037 mono_class_init (class);
3038 /* We return a MonoType* as handle */
3039 return &class->byval_arg;
3041 case MONO_TOKEN_TYPE_SPEC: {
3044 *handle_class = mono_defaults.typehandle_class;
3045 class = mono_class_create_from_typespec (image, token, context);
3046 mono_class_init (class);
3047 return &class->byval_arg;
3049 case MONO_TOKEN_FIELD_DEF: {
3051 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
3052 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
3053 mono_class_init (class);
3055 *handle_class = mono_defaults.fieldhandle_class;
3056 return mono_class_get_field (class, token);
3058 case MONO_TOKEN_METHOD_DEF: {
3060 meth = mono_get_method_full (image, token, NULL, context);
3062 *handle_class = mono_defaults.methodhandle_class;
3065 case MONO_TOKEN_MEMBER_REF: {
3066 guint32 cols [MONO_MEMBERREF_SIZE];
3068 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
3069 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
3070 mono_metadata_decode_blob_size (sig, &sig);
3071 if (*sig == 0x6) { /* it's a field */
3073 MonoClassField *field;
3074 field = mono_field_from_token (image, token, &klass, context);
3076 *handle_class = mono_defaults.fieldhandle_class;
3080 meth = mono_get_method_full (image, token, NULL, context);
3082 *handle_class = mono_defaults.methodhandle_class;
3087 g_warning ("Unknown token 0x%08x in ldtoken", token);
3094 * This function might need to call runtime functions so it can't be part
3095 * of the metadata library.
3097 static MonoLookupDynamicToken lookup_dynamic = NULL;
3100 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
3102 lookup_dynamic = func;
3106 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
3108 return lookup_dynamic (image, token);
3112 mono_class_get_image (MonoClass *klass)
3114 return klass->image;
3118 * mono_class_get_element_class:
3119 * @klass: the MonoClass to act on
3121 * Returns the element class of an array or an enumeration.
3124 mono_class_get_element_class (MonoClass *klass)
3126 return klass->element_class;
3130 * mono_class_is_valuetype:
3131 * @klass: the MonoClass to act on
3133 * Returns true if the MonoClass represents a ValueType.
3136 mono_class_is_valuetype (MonoClass *klass)
3138 return klass->valuetype;
3142 * mono_class_is_enum:
3143 * @klass: the MonoClass to act on
3145 * Returns true if the MonoClass represents an enumeration.
3148 mono_class_is_enum (MonoClass *klass)
3150 return klass->enumtype;
3154 * mono_class_enum_basetype:
3155 * @klass: the MonoClass to act on
3157 * Returns the underlying type representation for an enumeration.
3160 mono_class_enum_basetype (MonoClass *klass)
3162 return klass->enum_basetype;
3166 * mono_class_get_parent
3167 * @klass: the MonoClass to act on
3169 * Returns the parent class for this class.
3172 mono_class_get_parent (MonoClass *klass)
3174 return klass->parent;
3178 * mono_class_get_nesting_type;
3179 * @klass: the MonoClass to act on
3181 * Returns the container type where this type is nested or NULL if this type is not a nested type.
3184 mono_class_get_nesting_type (MonoClass *klass)
3186 return klass->nested_in;
3190 * mono_class_get_rank:
3191 * @klass: the MonoClass to act on
3193 * Returns the rank for the array (the number of dimensions).
3196 mono_class_get_rank (MonoClass *klass)
3202 * mono_class_get_flags:
3203 * @klass: the MonoClass to act on
3205 * The type flags from the TypeDef table from the metadata.
3206 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
3209 * Returns the flags from the TypeDef table.
3212 mono_class_get_flags (MonoClass *klass)
3214 return klass->flags;
3218 * mono_class_get_name
3219 * @klass: the MonoClass to act on
3221 * Returns the name of the class.
3224 mono_class_get_name (MonoClass *klass)
3230 * mono_class_get_namespace:
3231 * @klass: the MonoClass to act on
3233 * Returns the namespace of the class.
3236 mono_class_get_namespace (MonoClass *klass)
3238 return klass->name_space;
3242 * mono_class_get_type:
3243 * @klass: the MonoClass to act on
3245 * This method returns the internal Type representation for the class.
3247 * Returns the MonoType from the class.
3250 mono_class_get_type (MonoClass *klass)
3252 return &klass->byval_arg;
3256 * mono_class_get_byref_type:
3257 * @klass: the MonoClass to act on
3262 mono_class_get_byref_type (MonoClass *klass)
3264 return &klass->this_arg;
3268 * mono_class_num_fields:
3269 * @klass: the MonoClass to act on
3271 * Returns the number of static and instance fields in the class.
3274 mono_class_num_fields (MonoClass *klass)
3276 return klass->field.count;
3280 * mono_class_num_methods:
3281 * @klass: the MonoClass to act on
3283 * Returns the number of methods in the class.
3286 mono_class_num_methods (MonoClass *klass)
3288 return klass->method.count;
3292 * mono_class_num_properties
3293 * @klass: the MonoClass to act on
3295 * Returns the number of properties in the class.
3298 mono_class_num_properties (MonoClass *klass)
3300 return klass->property.count;
3304 * mono_class_num_events:
3305 * @klass: the MonoClass to act on
3307 * Returns the number of events in the class.
3310 mono_class_num_events (MonoClass *klass)
3312 return klass->event.count;
3316 * mono_class_get_fields:
3317 * @klass: the MonoClass to act on
3319 * This routine is an iterator routine for retrieving the fields in a class.
3321 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3322 * iterate over all of the elements. When no more values are
3323 * available, the return value is NULL.
3325 * Returns a `MonoClassField *' on each iteration, or NULL when no more fields are available.
3328 mono_class_get_fields (MonoClass* klass, gpointer *iter)
3330 MonoClassField* field;
3334 mono_class_init (klass);
3336 /* start from the first */
3337 if (klass->field.count) {
3338 return *iter = &klass->fields [0];
3346 if (field < &klass->fields [klass->field.count]) {
3347 return *iter = field;
3353 * mono_class_get_methods
3354 * @klass: the MonoClass to act on
3356 * This routine is an iterator routine for retrieving the fields in a class.
3358 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3359 * iterate over all of the elements. When no more values are
3360 * available, the return value is NULL.
3362 * Returns a MonoMethod on each iteration or NULL when no more methods are available.
3365 mono_class_get_methods (MonoClass* klass, gpointer *iter)
3367 MonoMethod** method;
3371 mono_class_init (klass);
3373 /* start from the first */
3374 if (klass->method.count) {
3375 *iter = &klass->methods [0];
3376 return klass->methods [0];
3384 if (method < &klass->methods [klass->method.count]) {
3392 * mono_class_get_properties:
3393 * @klass: the MonoClass to act on
3395 * This routine is an iterator routine for retrieving the properties in a class.
3397 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3398 * iterate over all of the elements. When no more values are
3399 * available, the return value is NULL.
3401 * Returns a `MonoProperty *' on each invocation, or NULL when no more are available.
3404 mono_class_get_properties (MonoClass* klass, gpointer *iter)
3406 MonoProperty* property;
3410 mono_class_init (klass);
3412 /* start from the first */
3413 if (klass->property.count) {
3414 return *iter = &klass->properties [0];
3422 if (property < &klass->properties [klass->property.count]) {
3423 return *iter = property;
3429 * mono_class_get_events:
3430 * @klass: the MonoClass to act on
3432 * This routine is an iterator routine for retrieving the properties 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 `MonoEvent *' on each invocation, or NULL when no more are available.
3441 mono_class_get_events (MonoClass* klass, gpointer *iter)
3447 mono_class_init (klass);
3449 /* start from the first */
3450 if (klass->event.count) {
3451 return *iter = &klass->events [0];
3459 if (event < &klass->events [klass->event.count]) {
3460 return *iter = event;
3466 * mono_class_get_interfaces
3467 * @klass: the MonoClass to act on
3469 * This routine is an iterator routine for retrieving the interfaces implemented by this 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 `Monoclass *' on each invocation, or NULL when no more are available.
3478 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
3484 mono_class_init (klass);
3486 /* start from the first */
3487 if (klass->interface_count) {
3488 *iter = &klass->interfaces [0];
3489 return klass->interfaces [0];
3497 if (iface < &klass->interfaces [klass->interface_count]) {
3505 * mono_class_get_nested_types
3506 * @klass: the MonoClass to act on
3508 * This routine is an iterator routine for retrieving the nested types of 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 `Monoclass *' on each invocation, or NULL when no more are available.
3517 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
3523 mono_class_init (klass);
3525 /* start from the first */
3526 if (klass->nested_classes) {
3527 *iter = klass->nested_classes;
3528 return klass->nested_classes->data;
3530 /* no nested types */
3544 * mono_field_get_name:
3545 * @field: the MonoClassField to act on
3547 * Returns the name of the field.
3550 mono_field_get_name (MonoClassField *field)
3556 * mono_field_get_type:
3557 * @field: the MonoClassField to act on
3559 * Returns MonoType of the field.
3562 mono_field_get_type (MonoClassField *field)
3568 * mono_field_get_type:
3569 * @field: the MonoClassField to act on
3571 * Returns MonoClass where the field was defined.
3574 mono_field_get_parent (MonoClassField *field)
3576 return field->parent;
3580 * mono_field_get_flags;
3581 * @field: the MonoClassField to act on
3583 * The metadata flags for a field are encoded using the
3584 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
3586 * Returns the flags for the field.
3589 mono_field_get_flags (MonoClassField *field)
3591 return field->type->attrs;
3595 * mono_property_get_name:
3596 * @prop: the MonoProperty to act on
3598 * Returns the name of the property
3601 mono_property_get_name (MonoProperty *prop)
3607 * mono_property_get_set_method
3608 * @prop: the MonoProperty to act on.
3610 * Returns the setter method of the property (A MonoMethod)
3613 mono_property_get_set_method (MonoProperty *prop)
3619 * mono_property_get_get_method
3620 * @prop: the MonoProperty to act on.
3622 * Returns the setter method of the property (A MonoMethod)
3625 mono_property_get_get_method (MonoProperty *prop)
3631 * mono_property_get_parent:
3632 * @prop: the MonoProperty to act on.
3634 * Returns the MonoClass where the property was defined.
3637 mono_property_get_parent (MonoProperty *prop)
3639 return prop->parent;
3643 * mono_property_get_flags:
3644 * @prop: the MonoProperty to act on.
3646 * The metadata flags for a property are encoded using the
3647 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
3649 * Returns the flags for the property.
3652 mono_property_get_flags (MonoProperty *prop)
3658 * mono_event_get_name:
3659 * @event: the MonoEvent to act on
3661 * Returns the name of the event.
3664 mono_event_get_name (MonoEvent *event)
3670 * mono_event_get_add_method:
3671 * @event: The MonoEvent to act on.
3673 * Returns the `add' method for the event (a MonoMethod).
3676 mono_event_get_add_method (MonoEvent *event)
3682 * mono_event_get_remove_method:
3683 * @event: The MonoEvent to act on.
3685 * Returns the `remove' method for the event (a MonoMethod).
3688 mono_event_get_remove_method (MonoEvent *event)
3690 return event->remove;
3694 * mono_event_get_raise_method:
3695 * @event: The MonoEvent to act on.
3697 * Returns the `raise' method for the event (a MonoMethod).
3700 mono_event_get_raise_method (MonoEvent *event)
3702 return event->raise;
3706 * mono_event_get_parent:
3707 * @event: the MonoEvent to act on.
3709 * Returns the MonoClass where the event is defined.
3712 mono_event_get_parent (MonoEvent *event)
3714 return event->parent;
3718 * mono_event_get_flags
3719 * @event: the MonoEvent to act on.
3721 * The metadata flags for an event are encoded using the
3722 * EVENT_* constants. See the tabledefs.h file for details.
3724 * Returns the flags for the event.
3727 mono_event_get_flags (MonoEvent *event)
3729 return event->attrs;
3733 * mono_find_method_by_name:
3734 * @klass: where to look for the method
3735 * @name_space: name of the method
3736 * @param_count: number of parameters. -1 for any number.
3738 * Obtains a MonoMethod with a given name and number of parameters.
3739 * It only works if there are no multiple signatures for any given method name.
3742 mono_class_get_method_from_name (MonoClass *klass, const char *name, int param_count)
3744 MonoMethod *res = NULL;
3747 mono_class_init (klass);
3749 for (i = 0; i < klass->method.count; ++i) {
3750 if (klass->methods [i]->name[0] == name [0] &&
3751 !strcmp (name, klass->methods [i]->name) &&
3752 (param_count == -1 || klass->methods [i]->signature->param_count == param_count)) {
3753 res = klass->methods [i];