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/metadata/security-manager.h>
37 #include <mono/os/gc_wrapper.h>
41 gboolean mono_print_vtable = FALSE;
43 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
44 static void mono_class_create_generic (MonoInflatedGenericClass *gclass);
45 static gboolean mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res);
47 void (*mono_debugger_start_class_init_func) (MonoClass *klass) = NULL;
48 void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
51 mono_class_from_typeref (MonoImage *image, guint32 type_token)
53 guint32 cols [MONO_TYPEREF_SIZE];
54 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
56 const char *name, *nspace;
58 MonoAssembly **references;
60 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
62 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
63 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
65 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
66 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
67 case MONO_RESOLTION_SCOPE_MODULE:
69 g_error ("null ResolutionScope not yet handled");
70 /* a typedef in disguise */
71 return mono_class_from_name (image, nspace, name);
72 case MONO_RESOLTION_SCOPE_MODULEREF:
73 return mono_class_from_name (image->modules [idx - 1], nspace, name);
74 case MONO_RESOLTION_SCOPE_TYPEREF: {
75 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
77 mono_class_init (enclosing);
78 for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
80 if (strcmp (res->name, name) == 0)
83 g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
86 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
90 references = image->references;
91 if (!references [idx-1])
92 mono_assembly_load_reference (image, idx - 1);
93 if (references [idx - 1] == (gpointer)-1)
96 image = references [idx-1]->image;
98 return mono_class_from_name (image, nspace, name);
101 static inline MonoType*
102 dup_type (MonoType* t, const MonoType *original)
104 MonoType *r = g_new0 (MonoType, 1);
106 r->attrs = original->attrs;
107 r->byref = original->byref;
108 if (t->type == MONO_TYPE_PTR)
109 t->data.type = dup_type (t->data.type, original->data.type);
110 mono_stats.generics_metadata_size += sizeof (MonoType);
115 _mono_type_get_assembly_name (MonoClass *klass, GString *str)
117 MonoAssembly *ta = klass->image->assembly;
119 g_string_append_printf (
120 str, ", %s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s",
122 ta->aname.major, ta->aname.minor, ta->aname.build, ta->aname.revision,
123 ta->aname.culture && *ta->aname.culture? ta->aname.culture: "neutral",
124 ta->aname.public_key_token [0] ? (char *)ta->aname.public_key_token : "null");
128 mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed,
129 MonoTypeNameFormat format)
133 switch (type->type) {
134 case MONO_TYPE_ARRAY: {
135 int i, rank = type->data.array->rank;
136 MonoTypeNameFormat nested_format;
138 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
139 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
141 mono_type_get_name_recurse (
142 &type->data.array->eklass->byval_arg, str, FALSE, nested_format);
143 g_string_append_c (str, '[');
144 for (i = 1; i < rank; i++)
145 g_string_append_c (str, ',');
146 g_string_append_c (str, ']');
147 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
148 _mono_type_get_assembly_name (type->data.array->eklass, str);
151 case MONO_TYPE_SZARRAY: {
152 MonoTypeNameFormat nested_format;
154 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
155 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
157 mono_type_get_name_recurse (
158 &type->data.klass->byval_arg, str, FALSE, nested_format);
159 g_string_append (str, "[]");
160 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
161 _mono_type_get_assembly_name (type->data.klass, str);
164 case MONO_TYPE_PTR: {
165 MonoTypeNameFormat nested_format;
167 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
168 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
170 mono_type_get_name_recurse (
171 type->data.type, str, FALSE, nested_format);
172 g_string_append (str, "*");
173 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
174 _mono_type_get_assembly_name (type->data.klass, str);
178 klass = mono_class_from_mono_type (type);
179 if (klass->nested_in) {
180 mono_type_get_name_recurse (
181 &klass->nested_in->byval_arg, str, TRUE, format);
182 if (format == MONO_TYPE_NAME_FORMAT_IL)
183 g_string_append_c (str, '.');
185 g_string_append_c (str, '+');
187 if (*klass->name_space) {
188 g_string_append (str, klass->name_space);
189 g_string_append_c (str, '.');
191 if (format == MONO_TYPE_NAME_FORMAT_IL) {
192 char *s = strchr (klass->name, '`');
193 int len = s ? s - klass->name : strlen (klass->name);
195 g_string_append_len (str, klass->name, len);
197 g_string_append (str, klass->name);
200 if (klass->generic_class) {
201 MonoGenericClass *gclass = klass->generic_class;
202 MonoTypeNameFormat nested_format;
205 nested_format = format == MONO_TYPE_NAME_FORMAT_FULL_NAME ?
206 MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED : format;
208 if (format == MONO_TYPE_NAME_FORMAT_IL)
209 g_string_append_c (str, '<');
211 g_string_append_c (str, '[');
212 for (i = 0; i < gclass->inst->type_argc; i++) {
213 MonoType *t = gclass->inst->type_argv [i];
216 g_string_append_c (str, ',');
217 if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
218 (t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
219 g_string_append_c (str, '[');
220 mono_type_get_name_recurse (
221 gclass->inst->type_argv [i], str, FALSE, nested_format);
222 if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
223 (t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
224 g_string_append_c (str, ']');
226 if (format == MONO_TYPE_NAME_FORMAT_IL)
227 g_string_append_c (str, '>');
229 g_string_append_c (str, ']');
230 } else if (klass->generic_container &&
231 (format != MONO_TYPE_NAME_FORMAT_FULL_NAME) &&
232 (format != MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)) {
235 if (format == MONO_TYPE_NAME_FORMAT_IL)
236 g_string_append_c (str, '<');
238 g_string_append_c (str, '[');
239 for (i = 0; i < klass->generic_container->type_argc; i++) {
241 g_string_append_c (str, ',');
242 g_string_append (str, klass->generic_container->type_params [i].name);
244 if (format == MONO_TYPE_NAME_FORMAT_IL)
245 g_string_append_c (str, '>');
247 g_string_append_c (str, ']');
249 if ((format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
250 (type->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
251 _mono_type_get_assembly_name (klass, str);
257 * mono_type_get_name:
260 * Returns: the string representation for type as required by System.Reflection.
261 * The inverse of mono_reflection_parse_type ().
264 mono_type_get_name_full (MonoType *type, MonoTypeNameFormat format)
268 if (format == MONO_TYPE_NAME_FORMAT_FULL_NAME &&
269 ((type->type == MONO_TYPE_VAR) || (type->type == MONO_TYPE_MVAR) ||
270 ((type->type == MONO_TYPE_GENERICINST) && type->data.generic_class->inst->is_open)))
273 result = g_string_new ("");
275 mono_type_get_name_recurse (type, result, FALSE, format);
278 g_string_append_c (result, '&');
280 return g_string_free (result, FALSE);
284 mono_type_get_name (MonoType *type)
286 return mono_type_get_name_full (type, MONO_TYPE_NAME_FORMAT_IL);
290 mono_type_get_underlying_type (MonoType *type)
292 switch (type->type) {
293 case MONO_TYPE_VALUETYPE:
294 if (type->data.klass->enumtype)
295 return type->data.klass->enum_basetype;
297 case MONO_TYPE_GENERICINST:
298 return mono_type_get_underlying_type (&type->data.generic_class->container_class->byval_arg);
307 mono_class_is_open_constructed_type (MonoType *t)
313 case MONO_TYPE_SZARRAY:
314 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
315 case MONO_TYPE_ARRAY:
316 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
318 return mono_class_is_open_constructed_type (t->data.type);
319 case MONO_TYPE_GENERICINST: {
320 MonoGenericClass *gclass = t->data.generic_class;
323 if (mono_class_is_open_constructed_type (&gclass->container_class->byval_arg))
325 for (i = 0; i < gclass->inst->type_argc; i++)
326 if (mono_class_is_open_constructed_type (gclass->inst->type_argv [i]))
335 static MonoGenericClass *
336 inflate_generic_class (MonoGenericClass *ogclass, MonoGenericContext *context)
338 MonoInflatedGenericClass *igclass;
339 MonoGenericClass *ngclass, *cached;
341 if (ogclass->is_dynamic) {
342 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
343 igclass = &dgclass->generic_class;
344 ngclass = &igclass->generic_class;
345 ngclass->is_inflated = 1;
346 ngclass->is_dynamic = 1;
348 igclass = g_new0 (MonoInflatedGenericClass, 1);
349 ngclass = &igclass->generic_class;
350 ngclass->is_inflated = 1;
355 ngclass->inst = mono_metadata_inflate_generic_inst (ogclass->inst, context);
357 igclass->klass = NULL;
359 ngclass->context = g_new0 (MonoGenericContext, 1);
360 ngclass->context->container = context->container;
361 ngclass->context->gclass = ngclass;
364 cached = mono_metadata_lookup_generic_class (ngclass);
365 mono_loader_unlock ();
375 inflate_generic_type (MonoType *type, MonoGenericContext *context)
377 switch (type->type) {
379 if (context->gmethod && context->gmethod->inst->type_argv)
381 context->gmethod->inst->type_argv [type->data.generic_param->num],
388 context->gclass->inst->type_argv [type->data.generic_param->num],
392 case MONO_TYPE_SZARRAY: {
393 MonoClass *eclass = type->data.klass;
394 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
397 nt = dup_type (type, type);
398 nt->data.klass = mono_class_from_mono_type (inflated);
401 case MONO_TYPE_ARRAY: {
402 MonoClass *eclass = type->data.array->eklass;
403 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
406 nt = dup_type (type, type);
407 nt->data.array = g_memdup (nt->data.array, sizeof (MonoArrayType));
408 nt->data.array->eklass = mono_class_from_mono_type (inflated);
411 case MONO_TYPE_GENERICINST: {
412 MonoGenericClass *ngclass = inflate_generic_class (type->data.generic_class, context);
413 MonoType *nt = dup_type (type, type);
414 nt->data.generic_class = ngclass;
417 case MONO_TYPE_CLASS:
418 case MONO_TYPE_VALUETYPE: {
419 MonoClass *klass = type->data.klass;
420 MonoGenericClass *gclass;
423 if (!klass->generic_container)
426 gclass = inflate_generic_class (klass->generic_container->context.gclass, context);
428 nt = dup_type (type, type);
429 nt->type = MONO_TYPE_GENERICINST;
430 nt->data.generic_class = gclass;
439 MonoInflatedGenericClass*
440 mono_get_inflated_generic_class (MonoGenericClass *gclass)
442 g_assert (gclass->is_inflated);
443 mono_class_create_generic ((MonoInflatedGenericClass *) gclass);
444 return (MonoInflatedGenericClass *) gclass;
448 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
450 MonoType *inflated = inflate_generic_type (type, context);
453 return dup_type (type, type);
455 mono_stats.inflated_type_count++;
460 mono_class_inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
462 MonoMethodSignature *res;
469 res = mono_metadata_signature_alloc (image, sig->param_count);
470 res->ret = mono_class_inflate_generic_type (sig->ret, context);
471 is_open = mono_class_is_open_constructed_type (res->ret);
472 for (i = 0; i < sig->param_count; ++i) {
473 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
475 is_open = mono_class_is_open_constructed_type (res->params [i]);
477 res->hasthis = sig->hasthis;
478 res->explicit_this = sig->explicit_this;
479 res->call_convention = sig->call_convention;
480 res->pinvoke = sig->pinvoke;
481 res->generic_param_count = sig->generic_param_count;
482 res->sentinelpos = sig->sentinelpos;
483 res->has_type_parameters = is_open;
484 res->is_inflated = 1;
488 static MonoMethodHeader*
489 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
491 MonoMethodHeader *res;
493 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
494 res->code = header->code;
495 res->code_size = header->code_size;
496 res->max_stack = header->max_stack;
497 res->num_clauses = header->num_clauses;
498 res->init_locals = header->init_locals;
499 res->num_locals = header->num_locals;
500 res->clauses = header->clauses;
501 for (i = 0; i < header->num_locals; ++i)
502 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
506 static MonoGenericContext *
507 inflate_generic_context (MonoGenericContext *context, MonoGenericContext *inflate_with)
509 MonoGenericContext *res = g_new0 (MonoGenericContext, 1);
511 res->container = context->container;
514 res->gclass = inflate_generic_class (context->gclass, inflate_with);
516 if (context->gmethod) {
517 res->gmethod = g_new0 (MonoGenericMethod, 1);
518 res->gmethod->generic_class = res->gclass;
520 res->gmethod->container = context->gmethod->container;
521 res->gmethod->inst = mono_metadata_inflate_generic_inst (context->gmethod->inst, inflate_with);
523 res->gmethod = inflate_with->gmethod;
529 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context)
531 MonoMethodInflated *result;
533 if (method->is_inflated || mono_method_signature_full (method, context)->is_inflated) {
534 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
536 context = inflate_generic_context (imethod->context, context);
537 method = imethod->declaring;
540 mono_stats.inflated_method_count++;
542 result = g_new0 (MonoMethodInflated, 1);
543 result->nmethod = *(MonoMethodNormal*)method;
544 result->nmethod.method.is_inflated = 1;
545 result->context = context;
546 result->declaring = method;
548 if (result->nmethod.method.klass->generic_class)
549 result->nmethod.method.klass = result->nmethod.method.klass->generic_class->container_class;
551 return (MonoMethod *) result;
555 mono_get_inflated_method (MonoMethod *method)
557 MonoMethodInflated *imethod, *res;
558 MonoMethodHeader *mh;
562 if (!method->is_inflated)
565 imethod = (MonoMethodInflated *) method;
566 if (imethod->inflated)
567 return (MonoMethod *) imethod->inflated;
569 mono_stats.inflated_method_count_2++;
570 mono_stats.generics_metadata_size +=
571 sizeof (MonoMethodInflated) - sizeof (MonoMethodNormal);
573 res = g_new0 (MonoMethodInflated, 1);
575 res->inflated = imethod->inflated = res;
577 mh = mono_method_get_header (method);
579 res->nmethod.header = inflate_generic_header (mh, imethod->context);
581 dtype = mono_class_inflate_generic_type (&method->klass->byval_arg, imethod->context);
582 rklass = res->nmethod.method.klass = mono_class_from_mono_type (dtype);
584 res->nmethod.method.signature = mono_class_inflate_generic_signature (
585 method->klass->image, mono_method_signature (method), imethod->context);
587 return (MonoMethod *) res;
591 * class_compute_field_layout:
592 * @m: pointer to the metadata.
593 * @class: The class to initialize
595 * Initializes the class->fields.
598 class_compute_field_layout (MonoClass *class)
600 MonoImage *m = class->image;
601 const int top = class->field.count;
602 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
603 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
604 int i, blittable = TRUE, real_size = 0;
606 guint32 packing_size = 0;
607 gboolean explicit_size;
608 MonoClassField *field;
610 if (class->size_inited)
614 if (!class->parent->size_inited)
615 class_compute_field_layout (class->parent);
616 class->instance_size += class->parent->instance_size;
617 class->min_align = class->parent->min_align;
618 /* we use |= since it may have been set already */
619 class->has_references |= class->parent->has_references;
620 blittable = class->parent->blittable;
622 class->instance_size = sizeof (MonoObject);
623 class->min_align = 1;
626 /* Get the real size */
627 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
630 g_assert ((packing_size & 0xfffffff0) == 0);
631 class->packing_size = packing_size;
632 real_size += class->instance_size;
636 if (explicit_size && real_size) {
637 class->instance_size = MAX (real_size, class->instance_size);
639 class->size_inited = 1;
640 class->blittable = blittable;
644 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT)
647 /* Prevent infinite loops if the class references itself */
648 class->size_inited = 1;
650 class->fields = g_new0 (MonoClassField, top);
653 * Fetch all the field information.
655 for (i = 0; i < top; i++){
657 guint32 cols [MONO_FIELD_SIZE];
658 int idx = class->field.first + i;
659 MonoGenericContainer *container = NULL;
661 field = &class->fields [i];
662 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
663 /* The name is needed for fieldrefs */
664 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
665 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
666 mono_metadata_decode_value (sig, &sig);
667 /* FIELD signature == 0x06 */
668 g_assert (*sig == 0x06);
669 if (class->generic_container)
670 container = class->generic_container;
671 else if (class->generic_class) {
672 MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
673 MonoClass *gklass = class->generic_class->container_class;
675 container = gklass->generic_container;
676 g_assert (container);
678 ifield->generic_type = gklass->fields [i].type;
679 field->generic_info = ifield;
681 field->type = mono_metadata_parse_type_full (
682 m, (MonoGenericContext *) container, MONO_PARSE_FIELD,
683 cols [MONO_FIELD_FLAGS], sig + 1, &sig);
684 if (mono_field_is_deleted (field))
686 if (class->generic_class) {
687 field->type = mono_class_inflate_generic_type (
688 field->type, class->generic_class->context);
689 field->type->attrs = cols [MONO_FIELD_FLAGS];
692 field->parent = class;
694 /* Only do these checks if we still think this type is blittable */
695 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
696 if (field->type->byref || MONO_TYPE_IS_REFERENCE (field->type)) {
699 MonoClass *field_class = mono_class_from_mono_type (field->type);
700 if (!field_class || !field_class->blittable)
705 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
706 mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
707 if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
708 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, field->name);
711 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
712 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
714 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
715 field->data = mono_image_rva_map (class->image, rva);
718 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
719 class->enum_basetype = field->type;
720 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
721 blittable = class->element_class->blittable;
724 /* The def_value of fields is compute lazily during vtable creation */
727 if (class == mono_defaults.string_class)
730 class->blittable = blittable;
732 if (class->enumtype && !class->enum_basetype) {
733 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
736 if (explicit_size && real_size) {
737 class->instance_size = MAX (real_size, class->instance_size);
740 mono_class_layout_fields (class);
743 /* useful until we keep track of gc-references in corlib etc. */
744 #define IS_GC_REFERENCE(t) ((t)->type == MONO_TYPE_U || (t)->type == MONO_TYPE_I || (t)->type == MONO_TYPE_PTR)
747 * LOCKING: this is supposed to be called with the loader lock held.
750 mono_class_layout_fields (MonoClass *class)
753 const int top = class->field.count;
754 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
755 guint32 pass, passes, real_size;
756 gboolean gc_aware_layout = FALSE;
757 MonoClassField *field;
759 if (class->generic_container ||
760 (class->generic_class && class->generic_class->inst->is_open))
764 * Enable GC aware auto layout: in this mode, reference
765 * fields are grouped together inside objects, increasing collector
767 * Requires that all classes whose layout is known to native code be annotated
768 * with [StructLayout (LayoutKind.Sequential)]
769 * Value types have gc_aware_layout disabled by default, as per
770 * what the default is for other runtimes.
772 /* corlib is missing [StructLayout] directives in many places */
773 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
774 if (class->image != mono_defaults.corlib &&
775 class->byval_arg.type != MONO_TYPE_VALUETYPE)
776 gc_aware_layout = TRUE;
780 * Compute field layout and total size (not considering static fields)
784 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
785 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
792 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
796 real_size = class->parent->instance_size;
798 real_size = sizeof (MonoObject);
800 for (pass = 0; pass < passes; ++pass) {
801 for (i = 0; i < top; i++){
803 field = &class->fields [i];
805 if (mono_field_is_deleted (field))
807 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
810 if (gc_aware_layout) {
812 * We process fields with reference type in the first pass,
813 * and fields with non-reference type in the second pass.
814 * We use IS_POINTER instead of IS_REFERENCE because in
815 * some internal structures, we store GC_MALLOCed memory
816 * in IntPtr fields...
818 if (MONO_TYPE_IS_POINTER (field->type)) {
827 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
828 (strcmp (field->name, "$PRIVATE$") == 0)) {
829 /* This field is a hack inserted by MCS to empty structures */
833 size = mono_type_size (field->type, &align);
835 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
836 align = class->packing_size ? MIN (class->packing_size, align): align;
837 class->min_align = MAX (align, class->min_align);
838 field->offset = real_size;
839 field->offset += align - 1;
840 field->offset &= ~(align - 1);
841 real_size = field->offset + size;
842 if (MONO_TYPE_IS_REFERENCE (field->type) || IS_GC_REFERENCE (field->type))
843 class->has_references = TRUE;
844 else if (field->type->type == MONO_TYPE_VALUETYPE && field->type->data.klass->has_references)
845 class->has_references = TRUE;
848 class->instance_size = MAX (real_size, class->instance_size);
850 if (class->instance_size & (class->min_align - 1)) {
851 class->instance_size += class->min_align - 1;
852 class->instance_size &= ~(class->min_align - 1);
856 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
858 for (i = 0; i < top; i++) {
860 field = &class->fields [i];
863 * There must be info about all the fields in a type if it
864 * uses explicit layout.
867 if (mono_field_is_deleted (field))
869 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
872 if (MONO_TYPE_IS_REFERENCE (field->type) || IS_GC_REFERENCE (field->type))
873 class->has_references = TRUE;
874 else if (field->type->type == MONO_TYPE_VALUETYPE && field->type->data.klass->has_references)
875 class->has_references = TRUE;
876 size = mono_type_size (field->type, &align);
879 * When we get here, field->offset is already set by the
880 * loader (for either runtime fields or fields loaded from metadata).
881 * The offset is from the start of the object: this works for both
882 * classes and valuetypes.
884 field->offset += sizeof (MonoObject);
889 real_size = MAX (real_size, size + field->offset);
891 class->instance_size = MAX (real_size, class->instance_size);
895 class->size_inited = 1;
898 * Compute static field layout and size
900 for (i = 0; i < top; i++){
902 field = &class->fields [i];
904 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
906 if (mono_field_is_deleted (field))
909 if (MONO_TYPE_IS_REFERENCE (field->type) || IS_GC_REFERENCE (field->type))
910 class->has_static_refs = TRUE;
911 else if (field->type->type == MONO_TYPE_VALUETYPE && field->type->data.klass->has_references)
912 class->has_static_refs = TRUE;
913 size = mono_type_size (field->type, &align);
914 field->offset = class->class_size;
915 field->offset += align - 1;
916 field->offset &= ~(align - 1);
917 class->class_size = field->offset + size;
922 * mono_class_setup_methods:
924 * Initializes the 'methods' array in the klass.
925 * Calling this method should be avoided if possible since it allocates a lot
926 * of long-living MonoMethod structures.
929 mono_class_setup_methods (MonoClass *class)
932 MonoMethod **methods;
934 if (class->methods || class->generic_class)
939 if (class->methods) {
940 mono_loader_unlock ();
944 //printf ("INIT: %s.%s\n", class->name_space, class->name);
946 if (!class->generic_class && !class->methods) {
947 methods = g_new (MonoMethod*, class->method.count);
948 for (i = 0; i < class->method.count; ++i) {
949 methods [i] = mono_get_method (class->image,
950 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
954 if (MONO_CLASS_IS_INTERFACE (class))
955 for (i = 0; i < class->method.count; ++i)
956 methods [i]->slot = i;
958 class->methods = methods;
960 mono_loader_unlock ();
965 mono_class_setup_properties (MonoClass *class)
967 guint startm, endm, i, j;
968 guint32 cols [MONO_PROPERTY_SIZE];
969 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
970 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
971 MonoProperty *properties;
973 if (class->properties)
978 if (class->properties) {
979 mono_loader_unlock ();
983 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
984 class->property.count = class->property.last - class->property.first;
986 if (class->property.count)
987 mono_class_setup_methods (class);
989 properties = g_new0 (MonoProperty, class->property.count);
990 for (i = class->property.first; i < class->property.last; ++i) {
991 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
992 properties [i - class->property.first].parent = class;
993 properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
994 properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
996 startm = mono_metadata_methods_from_property (class->image, i, &endm);
997 for (j = startm; j < endm; ++j) {
998 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
999 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1000 case METHOD_SEMANTIC_SETTER:
1001 properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1003 case METHOD_SEMANTIC_GETTER:
1004 properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1012 class->properties = properties;
1014 mono_loader_unlock ();
1018 mono_class_setup_events (MonoClass *class)
1020 guint startm, endm, i, j;
1021 guint32 cols [MONO_EVENT_SIZE];
1022 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
1023 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
1028 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
1029 class->event.count = class->event.last - class->event.first;
1031 if (class->event.count)
1032 mono_class_setup_methods (class);
1034 class->events = g_new0 (MonoEvent, class->event.count);
1035 for (i = class->event.first; i < class->event.last; ++i) {
1036 MonoEvent *event = &class->events [i - class->event.first];
1038 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
1039 event->parent = class;
1040 event->attrs = cols [MONO_EVENT_FLAGS];
1041 event->name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
1043 startm = mono_metadata_methods_from_event (class->image, i, &endm);
1044 for (j = startm; j < endm; ++j) {
1045 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
1046 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1047 case METHOD_SEMANTIC_ADD_ON:
1048 event->add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1050 case METHOD_SEMANTIC_REMOVE_ON:
1051 event->remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1053 case METHOD_SEMANTIC_FIRE:
1054 event->raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1056 case METHOD_SEMANTIC_OTHER: {
1059 if (event->other == NULL) {
1060 event->other = g_new0 (MonoMethod*, 1);
1062 while (event->other [n])
1064 event->other = g_realloc (event->other, (n + 1) * sizeof (MonoMethod*));
1066 event->other [n] = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1077 mono_get_unique_iid (MonoClass *class)
1079 static GHashTable *iid_hash = NULL;
1080 static guint iid = 0;
1085 g_assert (MONO_CLASS_IS_INTERFACE (class));
1087 mono_loader_lock ();
1090 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
1092 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
1094 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
1095 mono_loader_unlock ();
1097 return GPOINTER_TO_INT (value);
1099 g_hash_table_insert (iid_hash, str, GINT_TO_POINTER (iid));
1103 mono_loader_unlock ();
1105 g_assert (iid <= 65535);
1110 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
1115 for (i = 0; i < klass->interface_count; i++) {
1116 ic = klass->interfaces [i];
1119 *res = g_ptr_array_new ();
1120 g_ptr_array_add (*res, ic);
1121 mono_class_init (ic);
1123 collect_implemented_interfaces_aux (ic, res);
1128 mono_class_get_implemented_interfaces (MonoClass *klass)
1130 GPtrArray *res = NULL;
1132 collect_implemented_interfaces_aux (klass, &res);
1136 typedef struct _IOffsetInfo IOffsetInfo;
1137 struct _IOffsetInfo {
1141 int data [MONO_ZERO_LEN_ARRAY];
1144 static IOffsetInfo *cached_offset_info = NULL;
1145 static int next_offset_info_size = 128;
1148 cache_interface_offsets (int max_iid, int *data)
1150 IOffsetInfo *cached_info;
1153 for (cached_info = cached_offset_info; cached_info; cached_info = cached_info->next) {
1154 cached = cached_info->data;
1155 while (cached < cached_info->data + cached_info->size && *cached) {
1156 if (*cached == max_iid) {
1157 int i, matched = TRUE;
1159 for (i = 0; i < max_iid; ++i) {
1160 if (cached [i] != data [i]) {
1169 cached += *cached + 1;
1173 /* find a free slot */
1174 for (cached_info = cached_offset_info; cached_info; cached_info = cached_info->next) {
1175 if (cached_info->size - cached_info->next_free >= max_iid + 1) {
1176 cached = &cached_info->data [cached_info->next_free];
1177 *cached++ = max_iid;
1178 memcpy (cached, data, max_iid * sizeof (int));
1179 cached_info->next_free += max_iid + 1;
1183 /* allocate a new chunk */
1184 if (max_iid + 1 < next_offset_info_size) {
1185 new_size = next_offset_info_size;
1186 if (next_offset_info_size < 4096)
1187 next_offset_info_size += next_offset_info_size >> 2;
1189 new_size = max_iid + 1;
1191 cached_info = g_malloc0 (sizeof (IOffsetInfo) + sizeof (int) * new_size);
1192 cached_info->size = new_size;
1193 /*g_print ("allocated %d offset entries at %p (total: %d)\n", new_size, cached_info->data, offset_info_total_size);*/
1194 cached = &cached_info->data [0];
1195 *cached++ = max_iid;
1196 memcpy (cached, data, max_iid * sizeof (int));
1197 cached_info->next_free += max_iid + 1;
1198 cached_info->next = cached_offset_info;
1199 cached_offset_info = cached_info;
1204 setup_interface_offsets (MonoClass *class, int cur_slot)
1211 /* compute maximum number of slots and maximum interface id */
1213 for (k = class; k ; k = k->parent) {
1214 for (i = 0; i < k->interface_count; i++) {
1215 ic = k->interfaces [i];
1218 mono_class_init (ic);
1220 if (max_iid < ic->interface_id)
1221 max_iid = ic->interface_id;
1223 ifaces = mono_class_get_implemented_interfaces (k);
1225 for (i = 0; i < ifaces->len; ++i) {
1226 ic = g_ptr_array_index (ifaces, i);
1227 if (max_iid < ic->interface_id)
1228 max_iid = ic->interface_id;
1230 g_ptr_array_free (ifaces, TRUE);
1234 if (MONO_CLASS_IS_INTERFACE (class)) {
1235 if (max_iid < class->interface_id)
1236 max_iid = class->interface_id;
1238 class->max_interface_id = max_iid;
1239 /* compute vtable offset for interfaces */
1240 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
1242 for (i = 0; i <= max_iid; i++)
1243 class->interface_offsets [i] = -1;
1245 ifaces = mono_class_get_implemented_interfaces (class);
1247 for (i = 0; i < ifaces->len; ++i) {
1248 ic = g_ptr_array_index (ifaces, i);
1249 class->interface_offsets [ic->interface_id] = cur_slot;
1250 cur_slot += ic->method.count;
1252 g_ptr_array_free (ifaces, TRUE);
1255 for (k = class->parent; k ; k = k->parent) {
1256 ifaces = mono_class_get_implemented_interfaces (k);
1258 for (i = 0; i < ifaces->len; ++i) {
1259 ic = g_ptr_array_index (ifaces, i);
1261 if (class->interface_offsets [ic->interface_id] == -1) {
1262 int io = k->interface_offsets [ic->interface_id];
1266 class->interface_offsets [ic->interface_id] = io;
1269 g_ptr_array_free (ifaces, TRUE);
1273 if (MONO_CLASS_IS_INTERFACE (class))
1274 class->interface_offsets [class->interface_id] = cur_slot;
1276 cached_data = cache_interface_offsets (max_iid + 1, class->interface_offsets);
1277 g_free (class->interface_offsets);
1278 class->interface_offsets = cached_data;
1284 mono_class_setup_vtable (MonoClass *class)
1286 MonoMethod **overrides;
1287 MonoGenericContext *context;
1293 mono_class_setup_methods (class);
1295 if (MONO_CLASS_IS_INTERFACE (class))
1298 mono_loader_lock ();
1300 if (class->vtable) {
1301 mono_loader_unlock ();
1305 if (class->generic_class)
1306 context = class->generic_class->context;
1308 context = (MonoGenericContext *) class->generic_container;
1310 overrides = mono_class_get_overrides_full (
1311 class->image, class->type_token, &onum, context);
1312 mono_class_setup_vtable_general (class, overrides, onum);
1315 mono_loader_unlock ();
1319 setup_generic_vtable (MonoClass *class, MonoMethod **overrides, int onum)
1324 gklass = class->generic_class->container_class;
1326 mono_class_init (gklass);
1327 class->vtable_size = gklass->vtable_size;
1329 class->vtable = g_new0 (MonoMethod*, class->vtable_size);
1330 memcpy (class->vtable, gklass->vtable, sizeof (MonoMethod*) * class->vtable_size);
1332 for (i = 0; i < class->vtable_size; i++) {
1333 MonoMethod *m = class->vtable [i];
1338 m = mono_class_inflate_generic_method (m, class->generic_class->context);
1339 class->vtable [i] = m;
1342 class->max_interface_id = gklass->max_interface_id;
1343 class->interface_offsets = g_new0 (gint, gklass->max_interface_id + 1);
1344 memcpy (class->interface_offsets, gklass->interface_offsets,
1345 sizeof (gint) * (gklass->max_interface_id + 1));
1349 * LOCKING: this is supposed to be called with the loader lock held.
1352 mono_class_setup_vtable_general (MonoClass *class, MonoMethod **overrides, int onum)
1355 MonoMethod **vtable;
1356 int i, max_vtsize = 0, max_iid, cur_slot = 0;
1358 GHashTable *override_map = NULL;
1359 gboolean security_enabled = mono_is_security_manager_active ();
1364 if (class->generic_class) {
1365 setup_generic_vtable (class, overrides, onum);
1369 ifaces = mono_class_get_implemented_interfaces (class);
1371 for (i = 0; i < ifaces->len; i++) {
1372 MonoClass *ic = g_ptr_array_index (ifaces, i);
1373 max_vtsize += ic->method.count;
1375 g_ptr_array_free (ifaces, TRUE);
1378 if (class->parent) {
1379 mono_class_init (class->parent);
1380 mono_class_setup_vtable (class->parent);
1381 max_vtsize += class->parent->vtable_size;
1382 cur_slot = class->parent->vtable_size;
1385 max_vtsize += class->method.count;
1387 vtable = alloca (sizeof (gpointer) * max_vtsize);
1388 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
1390 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
1392 cur_slot = setup_interface_offsets (class, cur_slot);
1393 max_iid = class->max_interface_id;
1395 if (class->parent && class->parent->vtable_size)
1396 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
1398 /* override interface methods */
1399 for (i = 0; i < onum; i++) {
1400 MonoMethod *decl = overrides [i*2];
1401 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
1403 mono_class_setup_methods (decl->klass);
1404 g_assert (decl->slot != -1);
1405 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
1406 vtable [dslot] = overrides [i*2 + 1];
1407 vtable [dslot]->slot = dslot;
1409 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1411 g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
1415 for (k = class; k ; k = k->parent) {
1418 ifaces = mono_class_get_implemented_interfaces (k);
1420 nifaces = ifaces->len;
1421 for (i = 0; i < nifaces; i++) {
1424 ic = g_ptr_array_index (ifaces, i);
1425 io = k->interface_offsets [ic->interface_id];
1428 g_assert (io <= max_vtsize);
1431 mono_class_setup_methods (ic);
1432 for (l = 0; l < ic->method.count; l++) {
1433 MonoMethod *im = ic->methods [l];
1435 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1438 for (j = 0; j < class->method.count; ++j) {
1439 MonoMethod *cm = class->methods [j];
1440 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1441 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
1442 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
1444 if (!strcmp(cm->name, im->name) &&
1445 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1447 /* CAS - SecurityAction.InheritanceDemand on interface */
1448 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1449 mono_secman_inheritancedemand_method (cm, im);
1452 g_assert (io + l <= max_vtsize);
1453 vtable [io + l] = cm;
1458 /* already implemented */
1459 if (io >= k->vtable_size)
1463 for (l = 0; l < ic->method.count; l++) {
1464 MonoMethod *im = ic->methods [l];
1467 g_assert (io + l <= max_vtsize);
1469 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1472 for (k1 = class; k1; k1 = k1->parent) {
1473 for (j = 0; j < k1->method.count; ++j) {
1474 MonoMethod *cm = k1->methods [j];
1476 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1477 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
1480 if (!strcmp(cm->name, im->name) &&
1481 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1483 /* CAS - SecurityAction.InheritanceDemand on interface */
1484 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1485 mono_secman_inheritancedemand_method (cm, im);
1488 g_assert (io + l <= max_vtsize);
1489 vtable [io + l] = cm;
1494 g_assert (io + l <= max_vtsize);
1495 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1500 for (l = 0; l < ic->method.count; l++) {
1501 MonoMethod *im = ic->methods [l];
1502 char *qname, *fqname, *cname, *the_cname;
1505 if (vtable [io + l])
1508 if (ic->generic_class) {
1509 the_cname = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
1513 cname = (char*)ic->name;
1516 qname = g_strconcat (cname, ".", im->name, NULL);
1517 if (ic->name_space && ic->name_space [0])
1518 fqname = g_strconcat (ic->name_space, ".", cname, ".", im->name, NULL);
1522 for (k1 = class; k1; k1 = k1->parent) {
1523 for (j = 0; j < k1->method.count; ++j) {
1524 MonoMethod *cm = k1->methods [j];
1526 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1529 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
1530 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1532 /* CAS - SecurityAction.InheritanceDemand on interface */
1533 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1534 mono_secman_inheritancedemand_method (cm, im);
1537 g_assert (io + l <= max_vtsize);
1538 vtable [io + l] = cm;
1549 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
1550 for (l = 0; l < ic->method.count; l++) {
1552 MonoMethod *im = ic->methods [l];
1553 if (im->flags & METHOD_ATTRIBUTE_STATIC)
1555 g_assert (io + l <= max_vtsize);
1558 * If one of our parents already implements this interface
1559 * we can inherit the implementation.
1561 if (!(vtable [io + l])) {
1562 MonoClass *parent = class->parent;
1564 if ((ic->interface_id <= parent->max_interface_id) &&
1565 (parent->interface_offsets [ic->interface_id]) &&
1567 vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
1570 if (!(vtable [io + l])) {
1571 for (j = 0; j < onum; ++j) {
1572 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
1573 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
1575 msig = mono_signature_get_desc (mono_method_signature (im), FALSE);
1576 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
1577 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
1579 for (j = 0; j < class->method.count; ++j) {
1580 MonoMethod *cm = class->methods [j];
1581 msig = mono_signature_get_desc (mono_method_signature (cm), FALSE);
1583 printf ("METHOD %s(%s)\n", cm->name, msig);
1586 g_assert_not_reached ();
1591 for (l = 0; l < ic->method.count; l++) {
1592 MonoMethod *im = vtable [io + l];
1595 g_assert (io + l <= max_vtsize);
1597 /* FIXME: why do we need this ? */
1599 /* g_assert_not_reached (); */
1605 g_ptr_array_free (ifaces, TRUE);
1608 for (i = 0; i < class->method.count; ++i) {
1611 cm = class->methods [i];
1614 * Non-virtual method have no place in the vtable.
1615 * This also catches static methods (since they are not virtual).
1617 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1621 * If the method is REUSE_SLOT, we must check in the
1622 * base class for a method to override.
1624 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
1626 for (k = class->parent; k ; k = k->parent) {
1628 for (j = 0; j < k->method.count; ++j) {
1629 MonoMethod *m1 = k->methods [j];
1630 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
1632 if (!strcmp(cm->name, m1->name) &&
1633 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (m1))) {
1635 /* CAS - SecurityAction.InheritanceDemand */
1636 if (security_enabled && (m1->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1637 mono_secman_inheritancedemand_method (cm, m1);
1640 slot = k->methods [j]->slot;
1641 g_assert (cm->slot < max_vtsize);
1643 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1644 g_hash_table_insert (override_map, m1, cm);
1656 cm->slot = cur_slot++;
1658 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT) && ! mono_method_signature (cm)->generic_param_count)
1659 vtable [cm->slot] = cm;
1662 /* override non interface methods */
1663 for (i = 0; i < onum; i++) {
1664 MonoMethod *decl = overrides [i*2];
1665 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
1666 g_assert (decl->slot != -1);
1667 vtable [decl->slot] = overrides [i*2 + 1];
1668 overrides [i * 2 + 1]->slot = decl->slot;
1670 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1671 g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
1676 * If a method occupies more than one place in the vtable, and it is
1677 * overriden, then change the other occurances too.
1680 for (i = 0; i < max_vtsize; ++i)
1682 MonoMethod *cm = g_hash_table_lookup (override_map, vtable [i]);
1687 g_hash_table_destroy (override_map);
1690 if (class->generic_class) {
1691 MonoClass *gklass = class->generic_class->container_class;
1693 mono_class_init (gklass);
1694 class->vtable_size = gklass->vtable_size;
1696 class->vtable_size = cur_slot;
1698 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
1699 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
1701 if (mono_print_vtable) {
1704 for (i = 0; i <= max_iid; i++)
1705 if (class->interface_offsets [i] != -1)
1708 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
1709 class->name, class->vtable_size, icount);
1711 for (i = 0; i < class->vtable_size; ++i) {
1716 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
1717 cm->klass->name_space, cm->klass->name,
1724 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
1725 class->name, max_iid);
1727 for (i = 0; i < class->interface_count; i++) {
1728 ic = class->interfaces [i];
1729 printf (" slot %03d(%03d) %s.%s\n",
1730 class->interface_offsets [ic->interface_id],
1731 ic->method.count, ic->name_space, ic->name);
1734 for (k = class->parent; k ; k = k->parent) {
1735 for (i = 0; i < k->interface_count; i++) {
1736 ic = k->interfaces [i];
1737 printf (" slot %03d(%03d) %s.%s\n",
1738 class->interface_offsets [ic->interface_id],
1739 ic->method.count, ic->name_space, ic->name);
1748 * @class: the class to initialize
1750 * compute the instance_size, class_size and other infos that cannot be
1751 * computed at mono_class_get() time. Also compute a generic vtable and
1752 * the method slot numbers. We use this infos later to create a domain
1756 mono_class_init (MonoClass *class)
1759 static MonoMethod *default_ghc = NULL;
1760 static MonoMethod *default_finalize = NULL;
1761 static int finalize_slot = -1;
1762 static int ghc_slot = -1;
1763 MonoCachedClassInfo cached_info;
1770 /*g_print ("Init class %s\n", class->name);*/
1772 /* We do everything inside the lock to prevent races */
1773 mono_loader_lock ();
1775 if (class->inited) {
1776 mono_loader_unlock ();
1777 /* Somebody might have gotten in before us */
1781 if (class->init_pending) {
1783 * We might be called recursively from mono_class_from_typeref if
1784 * one of our fields has a type which is a nested type of this class,
1785 * and the compiler encodes it as a typeref, like older versions of
1788 if (class->size_inited) {
1789 mono_loader_unlock ();
1793 /* this indicates a cyclic dependency */
1794 g_error ("pending init %s.%s\n", class->name_space, class->name);
1797 class->init_pending = 1;
1799 /* CAS - SecurityAction.InheritanceDemand */
1800 if (mono_is_security_manager_active () && class->parent && (class->parent->flags & TYPE_ATTRIBUTE_HAS_SECURITY)) {
1801 mono_secman_inheritancedemand_class (class, class->parent);
1804 if (mono_debugger_start_class_init_func)
1805 mono_debugger_start_class_init_func (class);
1807 mono_stats.initialized_class_count++;
1809 if (class->generic_class && !class->generic_class->is_dynamic) {
1810 MonoInflatedGenericClass *gclass;
1813 gclass = mono_get_inflated_generic_class (class->generic_class);
1814 gklass = gclass->generic_class.container_class;
1816 mono_stats.generic_class_count++;
1818 class->method = gklass->method;
1819 class->field = gklass->field;
1821 mono_class_init (gklass);
1822 mono_class_setup_methods (gklass);
1823 mono_class_setup_properties (gklass);
1825 if (MONO_CLASS_IS_INTERFACE (class))
1826 class->interface_id = mono_get_unique_iid (class);
1828 g_assert (class->method.count == gklass->method.count);
1829 class->methods = g_new0 (MonoMethod *, class->method.count);
1831 for (i = 0; i < class->method.count; i++) {
1832 MonoMethod *inflated = mono_class_inflate_generic_method (
1833 gklass->methods [i], gclass->generic_class.context);
1835 class->methods [i] = mono_get_inflated_method (inflated);
1838 class->property = gklass->property;
1839 class->properties = g_new0 (MonoProperty, class->property.count);
1841 for (i = 0; i < class->property.count; i++) {
1842 MonoProperty *prop = &class->properties [i];
1844 *prop = gklass->properties [i];
1847 prop->get = mono_class_inflate_generic_method (
1848 prop->get, gclass->generic_class.context);
1850 prop->set = mono_class_inflate_generic_method (
1851 prop->set, gclass->generic_class.context);
1853 prop->parent = class;
1856 g_assert (class->interface_count == gklass->interface_count);
1859 if (class->parent && !class->parent->inited)
1860 mono_class_init (class->parent);
1862 if (!class->generic_class) {
1863 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
1866 guint32 cols [MONO_NESTED_CLASS_SIZE];
1867 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
1868 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
1869 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
1871 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
1876 * Computes the size used by the fields, and their locations
1878 if (!class->size_inited)
1879 class_compute_field_layout (class);
1881 /* initialize method pointers */
1884 MonoMethodSignature *sig;
1885 class->method.count = class->rank > 1? 2: 1;
1886 sig = mono_metadata_signature_alloc (class->image, class->rank);
1887 sig->ret = &mono_defaults.void_class->byval_arg;
1888 sig->pinvoke = TRUE;
1889 for (i = 0; i < class->rank; ++i)
1890 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1892 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1893 ctor->klass = class;
1894 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1895 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1896 ctor->signature = sig;
1897 ctor->name = ".ctor";
1899 class->methods = g_new (MonoMethod*, class->method.count);
1900 class->methods [0] = ctor;
1901 if (class->rank > 1) {
1902 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
1903 sig->ret = &mono_defaults.void_class->byval_arg;
1904 sig->pinvoke = TRUE;
1905 for (i = 0; i < class->rank * 2; ++i)
1906 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1908 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1909 ctor->klass = class;
1910 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1911 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1912 ctor->signature = sig;
1913 ctor->name = ".ctor";
1915 class->methods [1] = ctor;
1919 mono_class_setup_supertypes (class);
1922 if (class == mono_defaults.object_class) {
1923 mono_class_setup_vtable (class);
1924 for (i = 0; i < class->vtable_size; ++i) {
1925 MonoMethod *cm = class->vtable [i];
1927 if (!strcmp (cm->name, "GetHashCode")) {
1933 g_assert (ghc_slot > 0);
1935 default_ghc = class->vtable [ghc_slot];
1939 if (!default_finalize) {
1940 if (class == mono_defaults.object_class) {
1941 mono_class_setup_vtable (class);
1942 for (i = 0; i < class->vtable_size; ++i) {
1943 MonoMethod *cm = class->vtable [i];
1945 if (!strcmp (cm->name, "Finalize")) {
1951 g_assert (finalize_slot > 0);
1953 default_finalize = class->vtable [finalize_slot];
1958 * If possible, avoid the creation of the generic vtable by requesting
1959 * cached info from the runtime.
1961 if (mono_class_get_cached_class_info (class, &cached_info)) {
1962 guint32 cur_slot = 0;
1964 class->vtable_size = cached_info.vtable_size;
1965 class->has_finalize = cached_info.has_finalize;
1966 class->ghcimpl = cached_info.ghcimpl;
1967 class->has_cctor = cached_info.has_cctor;
1969 if (class->parent) {
1970 mono_class_init (class->parent);
1971 cur_slot = class->parent->vtable_size;
1974 setup_interface_offsets (class, cur_slot);
1977 mono_class_setup_vtable (class);
1980 if (class->parent) {
1981 if (class->vtable [ghc_slot] == default_ghc) {
1986 /* Object::Finalize should have empty implemenatation */
1987 class->has_finalize = 0;
1988 if (class->parent) {
1989 if (class->vtable [finalize_slot] != default_finalize)
1990 class->has_finalize = 1;
1993 for (i = 0; i < class->method.count; ++i) {
1994 MonoMethod *method = class->methods [i];
1995 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
1996 (strcmp (".cctor", method->name) == 0)) {
1997 class->has_cctor = 1;
2004 class->init_pending = 0;
2006 mono_loader_unlock ();
2008 if (MONO_CLASS_IS_INTERFACE (class)) {
2010 * class->interface_offsets is needed for the castclass/isinst code, so
2011 * we have to setup them for interfaces, too.
2013 setup_interface_offsets (class, 0);
2016 if (mono_debugger_class_init_func)
2017 mono_debugger_class_init_func (class);
2021 * LOCKING: this assumes the loader lock is held
2024 mono_class_setup_mono_type (MonoClass *class)
2026 const char *name = class->name;
2027 const char *nspace = class->name_space;
2029 if (MONO_CLASS_IS_INTERFACE (class))
2030 class->interface_id = mono_get_unique_iid (class);
2032 class->this_arg.byref = 1;
2033 class->this_arg.data.klass = class;
2034 class->this_arg.type = MONO_TYPE_CLASS;
2035 class->byval_arg.data.klass = class;
2036 class->byval_arg.type = MONO_TYPE_CLASS;
2038 if (!strcmp (nspace, "System")) {
2039 if (!strcmp (name, "ValueType")) {
2041 * do not set the valuetype bit for System.ValueType.
2042 * class->valuetype = 1;
2044 class->blittable = TRUE;
2045 } else if (!strcmp (name, "Enum")) {
2047 * do not set the valuetype bit for System.Enum.
2048 * class->valuetype = 1;
2050 class->valuetype = 0;
2051 class->enumtype = 0;
2052 } else if (!strcmp (name, "Object")) {
2053 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
2054 } else if (!strcmp (name, "String")) {
2055 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
2056 } else if (!strcmp (name, "TypedReference")) {
2057 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
2061 if (class->valuetype) {
2062 int t = MONO_TYPE_VALUETYPE;
2063 if (!strcmp (nspace, "System")) {
2066 if (!strcmp (name, "Boolean")) {
2067 t = MONO_TYPE_BOOLEAN;
2068 } else if (!strcmp(name, "Byte")) {
2070 class->blittable = TRUE;
2074 if (!strcmp (name, "Char")) {
2079 if (!strcmp (name, "Double")) {
2081 class->blittable = TRUE;
2085 if (!strcmp (name, "Int32")) {
2087 class->blittable = TRUE;
2088 } else if (!strcmp(name, "Int16")) {
2090 class->blittable = TRUE;
2091 } else if (!strcmp(name, "Int64")) {
2093 class->blittable = TRUE;
2094 } else if (!strcmp(name, "IntPtr")) {
2096 class->blittable = TRUE;
2100 if (!strcmp (name, "Single")) {
2102 class->blittable = TRUE;
2103 } else if (!strcmp(name, "SByte")) {
2105 class->blittable = TRUE;
2109 if (!strcmp (name, "UInt32")) {
2111 class->blittable = TRUE;
2112 } else if (!strcmp(name, "UInt16")) {
2114 class->blittable = TRUE;
2115 } else if (!strcmp(name, "UInt64")) {
2117 class->blittable = TRUE;
2118 } else if (!strcmp(name, "UIntPtr")) {
2120 class->blittable = TRUE;
2124 if (!strcmp (name, "TypedReference")) {
2125 t = MONO_TYPE_TYPEDBYREF;
2126 class->blittable = TRUE;
2130 if (!strcmp (name, "Void")) {
2138 class->this_arg.type = class->byval_arg.type = t;
2143 * LOCKING: this assumes the loader lock is held
2146 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
2148 gboolean system_namespace;
2150 system_namespace = !strcmp (class->name_space, "System");
2152 /* if root of the hierarchy */
2153 if (system_namespace && !strcmp (class->name, "Object")) {
2154 class->parent = NULL;
2155 class->instance_size = sizeof (MonoObject);
2158 if (!strcmp (class->name, "<Module>")) {
2159 class->parent = NULL;
2160 class->instance_size = 0;
2164 if (!MONO_CLASS_IS_INTERFACE (class)) {
2165 class->parent = parent;
2168 g_assert_not_reached (); /* FIXME */
2170 if (parent->generic_class && !parent->name) {
2172 * If the parent is a generic instance, we may get
2173 * called before it is fully initialized, especially
2174 * before it has its name.
2179 class->marshalbyref = parent->marshalbyref;
2180 class->contextbound = parent->contextbound;
2181 class->delegate = parent->delegate;
2183 if (system_namespace) {
2184 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
2185 class->marshalbyref = 1;
2187 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
2188 class->contextbound = 1;
2190 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
2191 class->delegate = 1;
2194 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
2195 (strcmp (class->parent->name_space, "System") == 0)))
2196 class->valuetype = 1;
2197 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
2198 class->valuetype = class->enumtype = 1;
2200 /*class->enumtype = class->parent->enumtype; */
2201 mono_class_setup_supertypes (class);
2203 class->parent = NULL;
2209 * LOCKING: this assumes the loader lock is held
2212 mono_class_setup_supertypes (MonoClass *class)
2216 if (class->supertypes)
2219 if (class->parent && !class->parent->supertypes)
2220 mono_class_setup_supertypes (class->parent);
2222 class->idepth = class->parent->idepth + 1;
2226 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
2227 class->supertypes = g_new0 (MonoClass *, ms);
2229 if (class->parent) {
2230 class->supertypes [class->idepth - 1] = class;
2231 memcpy (class->supertypes, class->parent->supertypes, class->parent->idepth * sizeof (gpointer));
2233 class->supertypes [0] = class;
2238 * If we inherit a type parameter from an outer class, set its owner to that class.
2241 set_generic_param_owner (MonoGenericContainer *container, MonoClass *klass, int pos)
2243 MonoGenericContainer *gc;
2246 if (klass->nested_in)
2247 pos = set_generic_param_owner (container, klass->nested_in, pos);
2249 if (!klass->generic_container)
2252 gc = klass->generic_container;
2253 for (i = pos; i < gc->type_argc; i++)
2254 container->type_params [i].owner = gc;
2256 return pos + gc->type_argc;
2259 static MonoGenericInst *
2260 get_shared_inst (MonoGenericContainer *container)
2262 MonoGenericInst *nginst;
2265 nginst = g_new0 (MonoGenericInst, 1);
2266 nginst->type_argc = container->type_argc;
2267 nginst->type_argv = g_new0 (MonoType *, nginst->type_argc);
2268 nginst->is_reference = 1;
2269 nginst->is_open = 1;
2271 for (i = 0; i < nginst->type_argc; i++) {
2272 MonoType *t = g_new0 (MonoType, 1);
2274 t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2275 t->data.generic_param = &container->type_params [i];
2277 nginst->type_argv [i] = t;
2280 return mono_metadata_lookup_generic_inst (nginst);
2284 mono_get_shared_generic_class (MonoGenericContainer *container, gboolean is_dynamic)
2286 MonoInflatedGenericClass *igclass;
2287 MonoGenericClass *gclass;
2290 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
2291 igclass = &dgclass->generic_class;
2292 gclass = &igclass->generic_class;
2293 gclass->is_inflated = 1;
2294 gclass->is_dynamic = 1;
2296 igclass = g_new0 (MonoInflatedGenericClass, 1);
2297 gclass = &igclass->generic_class;
2298 gclass->is_inflated = 1;
2301 gclass->context = &container->context;
2302 gclass->container_class = container->klass;
2303 gclass->inst = get_shared_inst (container);
2306 MonoGenericClass *cached = mono_metadata_lookup_generic_class (gclass);
2314 igclass->klass = container->klass;
2320 * @image: context where the image is created
2321 * @type_token: typedef token
2324 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
2326 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
2327 MonoClass *class, *parent = NULL;
2328 guint32 cols [MONO_TYPEDEF_SIZE];
2329 guint32 cols_next [MONO_TYPEDEF_SIZE];
2330 guint tidx = mono_metadata_token_index (type_token);
2331 MonoGenericContext *context = NULL;
2332 const char *name, *nspace;
2334 MonoClass **interfaces;
2336 mono_loader_lock ();
2338 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
2339 mono_loader_unlock ();
2343 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
2345 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
2347 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2348 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2350 class = g_malloc0 (sizeof (MonoClass));
2353 class->name_space = nspace;
2355 class->image = image;
2356 class->type_token = type_token;
2357 class->flags = cols [MONO_TYPEDEF_FLAGS];
2359 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
2361 class->generic_container = mono_metadata_load_generic_params (image, class->type_token, NULL);
2362 if (class->generic_container) {
2363 class->generic_container->klass = class;
2364 context = &class->generic_container->context;
2366 context->gclass = mono_get_shared_generic_class (context->container, FALSE);
2369 if (cols [MONO_TYPEDEF_EXTENDS])
2370 parent = mono_class_get_full (
2371 image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]), context);
2373 mono_class_setup_parent (class, parent);
2375 mono_class_setup_mono_type (class);
2377 interfaces = mono_metadata_interfaces_from_typedef_full (image, type_token, &icount, context);
2379 class->interfaces = interfaces;
2380 class->interface_count = icount;
2382 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
2384 /* fixme: maybe we must set this on windows
2385 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
2389 class->cast_class = class->element_class = class;
2391 /*g_print ("Load class %s\n", name);*/
2394 * Compute the field and method lists
2396 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
2397 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
2399 if (tt->rows > tidx){
2400 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
2401 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
2402 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
2404 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
2405 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
2408 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
2409 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
2410 class->field.count = class->field.last - class->field.first;
2412 class->field.count = 0;
2414 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
2415 class->method.count = class->method.last - class->method.first;
2417 class->method.count = 0;
2419 /* reserve space to store vector pointer in arrays */
2420 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
2421 class->instance_size += 2 * sizeof (gpointer);
2422 g_assert (class->field.count == 0);
2425 if (class->enumtype)
2426 class_compute_field_layout (class);
2428 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
2429 class->nested_in = mono_class_create_from_typedef (image, type_token);
2431 if (class->nested_in && class->generic_container)
2432 set_generic_param_owner (class->generic_container, class->nested_in, 0);
2434 mono_loader_unlock ();
2440 mono_class_create_generic (MonoInflatedGenericClass *gclass)
2442 MonoClass *klass, *gklass;
2445 if (gclass->is_initialized)
2447 gclass->is_initialized = TRUE;
2450 gclass->klass = g_malloc0 (sizeof (MonoClass));
2451 klass = gclass->klass;
2453 gklass = gclass->generic_class.container_class;
2455 klass->nested_in = gklass->nested_in;
2457 klass->name = gklass->name;
2458 klass->name_space = gklass->name_space;
2459 klass->image = gklass->image;
2460 klass->flags = gklass->flags;
2462 klass->generic_class = &gclass->generic_class;
2464 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
2465 klass->this_arg.data.generic_class = klass->byval_arg.data.generic_class = &gclass->generic_class;
2466 klass->this_arg.byref = TRUE;
2468 klass->cast_class = klass->element_class = klass;
2470 if (gclass->generic_class.is_dynamic) {
2471 klass->instance_size = gklass->instance_size;
2472 klass->class_size = gklass->class_size;
2473 klass->size_inited = 1;
2476 klass->valuetype = gklass->valuetype;
2478 mono_class_setup_supertypes (klass);
2481 klass->interface_count = gklass->interface_count;
2482 klass->interfaces = g_new0 (MonoClass *, klass->interface_count);
2483 for (i = 0; i < klass->interface_count; i++) {
2484 MonoType *it = &gklass->interfaces [i]->byval_arg;
2485 MonoType *inflated = mono_class_inflate_generic_type (
2486 it, gclass->generic_class.context);
2487 klass->interfaces [i] = mono_class_from_mono_type (inflated);
2490 i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, 1);
2493 guint32 cols [MONO_NESTED_CLASS_SIZE];
2494 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
2495 nclass = mono_class_create_from_typedef (klass->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
2496 klass->nested_classes = g_list_prepend (klass->nested_classes, nclass);
2498 i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, i + 1);
2501 if (gclass->generic_class.is_dynamic) {
2502 MonoDynamicGenericClass *dgclass = (MonoDynamicGenericClass *) gclass;
2504 if (dgclass->parent)
2505 klass->parent = mono_class_from_mono_type (dgclass->parent);
2506 } else if (gklass->parent) {
2507 MonoType *inflated = mono_class_inflate_generic_type (
2508 &gklass->parent->byval_arg, gclass->generic_class.context);
2510 klass->parent = mono_class_from_mono_type (inflated);
2514 mono_class_setup_parent (klass, klass->parent);
2518 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
2520 MonoClass *klass, **ptr;
2524 return param->pklass;
2526 klass = param->pklass = g_new0 (MonoClass, 1);
2528 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
2532 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
2533 klass->parent = param->constraints [0];
2535 } else if (param->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT)
2536 klass->parent = mono_class_from_name (mono_defaults.corlib, "System", "ValueType");
2538 klass->parent = mono_defaults.object_class;
2540 if (count - pos > 0) {
2541 klass->interface_count = count - pos;
2542 klass->interfaces = g_new0 (MonoClass *, count - pos);
2543 for (i = pos; i < count; i++)
2544 klass->interfaces [i - pos] = param->constraints [i];
2547 g_assert (param->name && param->owner);
2549 klass->name = param->name;
2550 klass->name_space = "";
2551 klass->image = image;
2552 klass->inited = TRUE;
2553 klass->cast_class = klass->element_class = klass;
2554 klass->enum_basetype = &klass->element_class->byval_arg;
2555 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2557 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2558 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2559 klass->this_arg.byref = TRUE;
2561 mono_class_setup_supertypes (klass);
2567 my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
2572 return param->pklass;
2574 g_assert (param->owner);
2576 klass = g_new0 (MonoClass, 1);
2579 klass->name = param->name;
2581 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
2582 klass->name_space = "";
2583 klass->image = mono_defaults.corlib;
2584 klass->inited = TRUE;
2585 klass->cast_class = klass->element_class = klass;
2586 klass->enum_basetype = &klass->element_class->byval_arg;
2587 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2589 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2590 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2591 klass->this_arg.byref = TRUE;
2593 mono_class_setup_supertypes (klass);
2599 mono_ptr_class_get (MonoType *type)
2602 MonoClass *el_class;
2603 static GHashTable *ptr_hash = NULL;
2605 mono_loader_lock ();
2608 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2609 el_class = mono_class_from_mono_type (type);
2610 if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
2611 mono_loader_unlock ();
2614 result = g_new0 (MonoClass, 1);
2616 result->parent = NULL; /* no parent for PTR types */
2617 result->name_space = el_class->name_space;
2618 result->name = g_strdup_printf ("%s*", el_class->name);
2619 result->image = el_class->image;
2620 result->inited = TRUE;
2621 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2622 /* Can pointers get boxed? */
2623 result->instance_size = sizeof (gpointer);
2624 result->cast_class = result->element_class = el_class;
2625 result->enum_basetype = &result->element_class->byval_arg;
2626 result->blittable = TRUE;
2628 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
2629 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
2630 result->this_arg.byref = TRUE;
2632 mono_class_setup_supertypes (result);
2634 g_hash_table_insert (ptr_hash, el_class, result);
2636 mono_loader_unlock ();
2642 mono_fnptr_class_get (MonoMethodSignature *sig)
2645 static GHashTable *ptr_hash = NULL;
2647 mono_loader_lock ();
2650 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2652 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
2653 mono_loader_unlock ();
2656 result = g_new0 (MonoClass, 1);
2658 result->parent = NULL; /* no parent for PTR types */
2659 result->name = "System";
2660 result->name_space = "MonoFNPtrFakeClass";
2661 result->image = NULL; /* need to fix... */
2662 result->inited = TRUE;
2663 result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
2664 /* Can pointers get boxed? */
2665 result->instance_size = sizeof (gpointer);
2666 result->cast_class = result->element_class = result;
2667 result->blittable = TRUE;
2669 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
2670 result->this_arg.data.method = result->byval_arg.data.method = sig;
2671 result->this_arg.byref = TRUE;
2672 result->enum_basetype = &result->element_class->byval_arg;
2673 result->blittable = TRUE;
2675 mono_class_setup_supertypes (result);
2677 g_hash_table_insert (ptr_hash, sig, result);
2679 mono_loader_unlock ();
2685 mono_class_from_mono_type (MonoType *type)
2687 switch (type->type) {
2688 case MONO_TYPE_OBJECT:
2689 return type->data.klass? type->data.klass: mono_defaults.object_class;
2690 case MONO_TYPE_VOID:
2691 return type->data.klass? type->data.klass: mono_defaults.void_class;
2692 case MONO_TYPE_BOOLEAN:
2693 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
2694 case MONO_TYPE_CHAR:
2695 return type->data.klass? type->data.klass: mono_defaults.char_class;
2697 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
2699 return type->data.klass? type->data.klass: mono_defaults.byte_class;
2701 return type->data.klass? type->data.klass: mono_defaults.int16_class;
2703 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
2705 return type->data.klass? type->data.klass: mono_defaults.int32_class;
2707 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
2709 return type->data.klass? type->data.klass: mono_defaults.int_class;
2711 return type->data.klass? type->data.klass: mono_defaults.uint_class;
2713 return type->data.klass? type->data.klass: mono_defaults.int64_class;
2715 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
2717 return type->data.klass? type->data.klass: mono_defaults.single_class;
2719 return type->data.klass? type->data.klass: mono_defaults.double_class;
2720 case MONO_TYPE_STRING:
2721 return type->data.klass? type->data.klass: mono_defaults.string_class;
2722 case MONO_TYPE_TYPEDBYREF:
2723 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
2724 case MONO_TYPE_ARRAY:
2725 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
2727 return mono_ptr_class_get (type->data.type);
2728 case MONO_TYPE_FNPTR:
2729 return mono_fnptr_class_get (type->data.method);
2730 case MONO_TYPE_SZARRAY:
2731 return mono_array_class_get (type->data.klass, 1);
2732 case MONO_TYPE_CLASS:
2733 case MONO_TYPE_VALUETYPE:
2734 return type->data.klass;
2735 case MONO_TYPE_GENERICINST: {
2736 MonoInflatedGenericClass *gclass;
2737 gclass = mono_get_inflated_generic_class (type->data.generic_class);
2738 g_assert (gclass->klass);
2739 return gclass->klass;
2742 return my_mono_class_from_generic_parameter (type->data.generic_param, FALSE);
2743 case MONO_TYPE_MVAR:
2744 return my_mono_class_from_generic_parameter (type->data.generic_param, TRUE);
2746 g_warning ("implement me 0x%02x\n", type->type);
2747 g_assert_not_reached ();
2754 * @image: context where the image is created
2755 * @type_spec: typespec token
2758 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec,
2759 MonoGenericContext *context)
2761 MonoType *type, *inflated;
2764 type = mono_type_create_from_typespec_full (image, context, type_spec);
2766 switch (type->type) {
2767 case MONO_TYPE_ARRAY:
2768 class = mono_array_class_get (type->data.array->eklass, type->data.array->rank);
2770 case MONO_TYPE_SZARRAY:
2771 class = mono_array_class_get (type->data.klass, 1);
2774 class = mono_ptr_class_get (type->data.type);
2776 case MONO_TYPE_GENERICINST: {
2777 MonoInflatedGenericClass *gclass;
2778 gclass = mono_get_inflated_generic_class (type->data.generic_class);
2779 g_assert (gclass->klass);
2780 class = gclass->klass;
2784 /* it seems any type can be stored in TypeSpec as well */
2785 class = mono_class_from_mono_type (type);
2789 if (!class || !context || (!context->gclass && !context->gmethod))
2792 inflated = mono_class_inflate_generic_type (&class->byval_arg, context);
2794 return mono_class_from_mono_type (inflated);
2798 * mono_bounded_array_class_get:
2799 * @element_class: element class
2800 * @rank: the dimension of the array class
2801 * @bounded: whenever the array has non-zero bounds
2803 * Returns: a class object describing the array with element type @element_type and
2807 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
2811 MonoClass *parent = NULL;
2812 GSList *list, *rootlist;
2815 gboolean corlib_type = FALSE;
2817 g_assert (rank <= 255);
2820 /* bounded only matters for one-dimensional arrays */
2823 image = eclass->image;
2825 mono_loader_lock ();
2827 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
2828 for (; list; list = list->next) {
2830 if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
2831 mono_loader_unlock ();
2837 /* for the building corlib use System.Array from it */
2838 if (image->assembly && image->assembly->dynamic && strcmp (image->assembly_name, "mscorlib") == 0) {
2839 parent = mono_class_from_name (image, "System", "Array");
2841 } else if (mono_defaults.generic_array_class) {
2842 MonoType *inflated, **args;
2844 args = g_new0 (MonoType *, 1);
2845 args [0] = &eclass->byval_arg;
2847 inflated = mono_class_bind_generic_parameters (
2848 &mono_defaults.generic_array_class->byval_arg, 1, args);
2849 parent = mono_class_from_mono_type (inflated);
2851 if (!parent->inited)
2852 mono_class_init (parent);
2854 parent = mono_defaults.array_class;
2855 if (!parent->inited)
2856 mono_class_init (parent);
2859 class = g_malloc0 (sizeof (MonoClass));
2861 class->image = image;
2862 class->name_space = eclass->name_space;
2863 nsize = strlen (eclass->name);
2864 name = g_malloc (nsize + 2 + rank);
2865 memcpy (name, eclass->name, nsize);
2868 memset (name + nsize + 1, ',', rank - 1);
2869 name [nsize + rank] = ']';
2870 name [nsize + rank + 1] = 0;
2872 class->type_token = 0;
2873 /* all arrays are marked serializable and sealed, bug #42779 */
2874 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
2875 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2876 class->parent = parent;
2877 class->instance_size = mono_class_instance_size (class->parent);
2878 class->class_size = 0;
2879 mono_class_setup_supertypes (class);
2880 if (eclass->generic_class)
2881 mono_class_init (eclass);
2882 if (!eclass->size_inited)
2883 class_compute_field_layout (eclass);
2884 class->has_references = MONO_TYPE_IS_REFERENCE (&eclass->byval_arg) || eclass->has_references? TRUE: FALSE;
2888 if (eclass->enumtype)
2889 class->cast_class = eclass->element_class;
2891 class->cast_class = eclass;
2893 class->element_class = eclass;
2895 if ((rank > 1) || bounded) {
2896 MonoArrayType *at = g_new0 (MonoArrayType, 1);
2897 class->byval_arg.type = MONO_TYPE_ARRAY;
2898 class->byval_arg.data.array = at;
2899 at->eklass = eclass;
2901 /* FIXME: complete.... */
2903 class->byval_arg.type = MONO_TYPE_SZARRAY;
2904 class->byval_arg.data.klass = eclass;
2906 class->this_arg = class->byval_arg;
2907 class->this_arg.byref = 1;
2912 list = g_slist_append (rootlist, class);
2913 g_hash_table_insert (image->array_cache, eclass, list);
2915 mono_loader_unlock ();
2921 * mono_array_class_get:
2922 * @element_class: element class
2923 * @rank: the dimension of the array class
2925 * Returns: a class object describing the array with element type @element_type and
2929 mono_array_class_get (MonoClass *eclass, guint32 rank)
2931 return mono_bounded_array_class_get (eclass, rank, FALSE);
2935 * mono_class_instance_size:
2938 * Returns: the size of an object instance
2941 mono_class_instance_size (MonoClass *klass)
2943 if (!klass->size_inited)
2944 mono_class_init (klass);
2946 return klass->instance_size;
2950 * mono_class_min_align:
2953 * Returns: minimm alignment requirements
2956 mono_class_min_align (MonoClass *klass)
2958 if (!klass->size_inited)
2959 mono_class_init (klass);
2961 return klass->min_align;
2965 * mono_class_value_size:
2968 * This function is used for value types, and return the
2969 * space and the alignment to store that kind of value object.
2971 * Returns: the size of a value of kind @klass
2974 mono_class_value_size (MonoClass *klass, guint32 *align)
2978 /* fixme: check disable, because we still have external revereces to
2979 * mscorlib and Dummy Objects
2981 /*g_assert (klass->valuetype);*/
2983 size = mono_class_instance_size (klass) - sizeof (MonoObject);
2986 *align = klass->min_align;
2992 * mono_class_data_size:
2995 * Returns: the size of the static class data
2998 mono_class_data_size (MonoClass *klass)
3001 mono_class_init (klass);
3003 return klass->class_size;
3007 * Auxiliary routine to mono_class_get_field
3009 * Takes a field index instead of a field token.
3011 static MonoClassField *
3012 mono_class_get_field_idx (MonoClass *class, int idx)
3014 if (class->field.count){
3015 if ((idx >= class->field.first) && (idx < class->field.last)){
3016 return &class->fields [idx - class->field.first];
3023 return mono_class_get_field_idx (class->parent, idx);
3027 * mono_class_get_field:
3028 * @class: the class to lookup the field.
3029 * @field_token: the field token
3031 * Returns: A MonoClassField representing the type and offset of
3032 * the field, or a NULL value if the field does not belong to this
3036 mono_class_get_field (MonoClass *class, guint32 field_token)
3038 int idx = mono_metadata_token_index (field_token);
3040 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
3042 return mono_class_get_field_idx (class, idx - 1);
3046 mono_class_get_field_from_name (MonoClass *klass, const char *name)
3051 for (i = 0; i < klass->field.count; ++i) {
3052 if (strcmp (name, klass->fields [i].name) == 0)
3053 return &klass->fields [i];
3055 klass = klass->parent;
3061 mono_class_get_field_token (MonoClassField *field)
3063 MonoClass *klass = field->parent;
3067 for (i = 0; i < klass->field.count; ++i) {
3068 if (&klass->fields [i] == field)
3069 return mono_metadata_make_token (MONO_TABLE_FIELD, klass->field.first + i + 1);
3071 klass = klass->parent;
3074 g_assert_not_reached ();
3079 mono_class_get_event_token (MonoEvent *event)
3081 MonoClass *klass = event->parent;
3085 for (i = 0; i < klass->event.count; ++i) {
3086 if (&klass->events [i] == event)
3087 return mono_metadata_make_token (MONO_TABLE_EVENT, klass->event.first + i + 1);
3089 klass = klass->parent;
3092 g_assert_not_reached ();
3097 mono_vtable_get_static_field_data (MonoVTable *vt)
3103 mono_class_get_property_from_name (MonoClass *klass, const char *name)
3107 gpointer iter = NULL;
3108 while ((p = mono_class_get_properties (klass, &iter))) {
3109 if (! strcmp (name, p->name))
3112 klass = klass->parent;
3118 mono_class_get_property_token (MonoProperty *prop)
3120 MonoClass *klass = prop->parent;
3124 gpointer iter = NULL;
3125 while ((p = mono_class_get_properties (klass, &iter))) {
3126 if (&klass->properties [i] == prop)
3127 return mono_metadata_make_token (MONO_TABLE_PROPERTY, klass->property.first + i + 1);
3131 klass = klass->parent;
3134 g_assert_not_reached ();
3139 mono_class_name_from_token (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3141 const char *name, *nspace;
3143 return g_strdup_printf ("DynamicType 0x%08x", type_token);
3145 switch (type_token & 0xff000000){
3146 case MONO_TOKEN_TYPE_DEF: {
3147 guint32 cols [MONO_TYPEDEF_SIZE];
3148 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
3149 guint tidx = mono_metadata_token_index (type_token);
3151 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
3152 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3153 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3154 if (strlen (nspace) == 0)
3155 return g_strdup_printf ("%s", name);
3157 return g_strdup_printf ("%s.%s", nspace, name);
3160 case MONO_TOKEN_TYPE_REF: {
3161 guint32 cols [MONO_TYPEREF_SIZE];
3162 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
3164 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
3165 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
3166 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
3167 if (strlen (nspace) == 0)
3168 return g_strdup_printf ("%s", name);
3170 return g_strdup_printf ("%s.%s", nspace, name);
3173 case MONO_TOKEN_TYPE_SPEC:
3174 return g_strdup_printf ("Typespec 0x%08x", type_token);
3176 g_assert_not_reached ();
3184 * @image: the image where the class resides
3185 * @type_token: the token for the class
3186 * @at: an optional pointer to return the array element type
3188 * Returns: the MonoClass that represents @type_token in @image
3191 _mono_class_get (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3193 MonoClass *class = NULL;
3196 return mono_lookup_dynamic_token (image, type_token);
3198 switch (type_token & 0xff000000){
3199 case MONO_TOKEN_TYPE_DEF:
3200 class = mono_class_create_from_typedef (image, type_token);
3202 case MONO_TOKEN_TYPE_REF:
3203 class = mono_class_from_typeref (image, type_token);
3205 case MONO_TOKEN_TYPE_SPEC:
3206 class = mono_class_create_from_typespec (image, type_token, context);
3209 g_warning ("unknown token type %x", type_token & 0xff000000);
3210 g_assert_not_reached ();
3214 char *name = mono_class_name_from_token (image, type_token, context);
3215 g_warning ("The class %s could not be loaded, used in %s (token 0x%08x)", name, image->name, type_token);
3223 mono_class_get (MonoImage *image, guint32 type_token)
3225 return _mono_class_get (image, type_token, NULL);
3229 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3231 MonoClass *class = _mono_class_get (image, type_token, context);
3234 if (!class || !context || (!context->gclass && !context->gmethod))
3237 switch (class->byval_arg.type) {
3238 case MONO_TYPE_GENERICINST:
3239 if (!class->generic_class->inst->is_open)
3243 case MONO_TYPE_MVAR:
3249 inflated = inflate_generic_type (&class->byval_arg, context);
3253 return mono_class_from_mono_type (inflated);
3257 * mono_class_from_name_case:
3258 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3259 * @name_space: the type namespace
3260 * @name: the type short name.
3262 * Obtains a MonoClass with a given namespace and a given name which
3263 * is located in the given MonoImage. The namespace and name
3264 * lookups are case insensitive.
3266 * You can also pass @NULL to the image, and that will lookup for
3267 * a type with the given namespace and name in all of the loaded
3268 * assemblies: notice that since there might be a name clash in this
3269 * case, passing @NULL is not encouraged if you need a precise type.
3273 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
3275 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
3276 guint32 cols [MONO_TYPEDEF_SIZE];
3281 /* add a cache if needed */
3282 for (i = 1; i <= t->rows; ++i) {
3283 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
3284 /* nested types are accessed from the nesting name */
3285 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
3286 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
3288 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3289 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3290 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
3291 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
3297 return_nested_in (MonoClass *class, char *nested) {
3299 char *s = strchr (nested, '/');
3306 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
3308 if (strcmp (found->name, nested) == 0) {
3310 return return_nested_in (found, s);
3319 * mono_class_from_name_case:
3320 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3321 * @name_space: the type namespace
3322 * @name: the type short name.
3324 * Obtains a MonoClass with a given namespace and a given name which
3325 * is located in the given MonoImage.
3327 * You can also pass `NULL' to the image, and that will lookup for
3328 * a type with the given namespace and name in all of the loaded
3329 * assemblies: notice that since there might be a name clash in this
3330 * case, passing NULL is not encouraged if you need a precise type.
3334 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
3336 GHashTable *nspace_table;
3337 MonoImage *loaded_image;
3343 if ((nested = strchr (name, '/'))) {
3344 int pos = nested - name;
3345 int len = strlen (name);
3348 memcpy (buf, name, len + 1);
3350 nested = buf + pos + 1;
3354 mono_loader_lock ();
3356 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
3359 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
3361 mono_loader_unlock ();
3366 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
3367 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
3368 guint32 cols [MONO_EXP_TYPE_SIZE];
3371 idx = mono_metadata_token_index (token);
3373 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
3375 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
3376 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
3377 loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
3380 class = mono_class_from_name (loaded_image, name_space, name);
3382 return return_nested_in (class, nested);
3385 g_error ("not yet implemented");
3389 token = MONO_TOKEN_TYPE_DEF | token;
3391 class = mono_class_get (image, token);
3393 return return_nested_in (class, nested);
3398 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
3399 gboolean check_interfaces)
3402 g_assert (klassc->idepth > 0);
3403 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
3404 if ((klassc->interface_id <= klass->max_interface_id) &&
3405 (klass->interface_offsets [klassc->interface_id] >= 0))
3407 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
3410 for (i = 0; i < klass->interface_count; i ++) {
3411 MonoClass *ic = klass->interfaces [i];
3416 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
3421 * MS.NET thinks interfaces are a subclass of Object, so we think it as
3424 if (klassc == mono_defaults.object_class)
3427 if (klass->generic_class && klass->generic_class->is_dynamic) {
3428 MonoDynamicGenericClass *dgclass = (MonoDynamicGenericClass *) klass->generic_class;
3430 if (!dgclass->parent)
3433 if (mono_metadata_type_equal (dgclass->parent, &klassc->byval_arg))
3435 klass = mono_class_from_mono_type (dgclass->parent);
3443 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
3446 mono_class_init (klass);
3448 if (!oklass->inited)
3449 mono_class_init (oklass);
3451 if (klass->generic_class)
3452 klass = klass->generic_class->container_class;
3453 if (oklass->generic_class)
3454 oklass = oklass->generic_class->container_class;
3456 if (MONO_CLASS_IS_INTERFACE (klass)) {
3457 if ((oklass->byval_arg.type == MONO_TYPE_VAR) || (oklass->byval_arg.type == MONO_TYPE_MVAR))
3460 /* interface_offsets might not be set for dynamic classes */
3461 if (oklass->reflection_info && !oklass->interface_offsets)
3463 * oklass might be a generic type parameter but they have
3464 * interface_offsets set.
3466 return mono_reflection_call_is_assignable_to (oklass, klass);
3468 if ((klass->interface_id <= oklass->max_interface_id) &&
3469 (oklass->interface_offsets [klass->interface_id] != -1))
3473 MonoClass *eclass, *eoclass;
3475 if (oklass->rank != klass->rank)
3478 /* vectors vs. one dimensional arrays */
3479 if (oklass->byval_arg.type != klass->byval_arg.type)
3482 eclass = klass->cast_class;
3483 eoclass = oklass->cast_class;
3487 * a is b does not imply a[] is b[] when a is a valuetype, and
3488 * b is a reference type.
3491 if (eoclass->valuetype) {
3492 if ((eclass == mono_defaults.enum_class) ||
3493 (eclass == mono_defaults.enum_class->parent) ||
3494 (eclass == mono_defaults.object_class))
3498 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
3501 if (klass == mono_defaults.object_class)
3505 * Custom version of mono_class_has_parent (oklass, klass)
3507 if (oklass->idepth >= klass->idepth) {
3508 MonoClass *parent = oklass->supertypes [klass->idepth - 1];
3510 if (parent->generic_class)
3511 parent = parent->generic_class->container_class;
3513 return klass == parent;
3520 * mono_class_get_cctor:
3522 * Returns the static constructor of @klass if it exists, NULL otherwise.
3525 mono_class_get_cctor (MonoClass *klass)
3527 MonoCachedClassInfo cached_info;
3529 if (!klass->has_cctor)
3532 if (mono_class_get_cached_class_info (klass, &cached_info))
3533 return mono_get_method (klass->image, cached_info.cctor_token, klass);
3535 return mono_class_get_method_from_name_flags (klass, ".cctor", -1, METHOD_ATTRIBUTE_SPECIAL_NAME);
3539 * mono_class_get_finalizer:
3541 * Returns the finalizer method of @klass if it exists, NULL otherwise.
3544 mono_class_get_finalizer (MonoClass *klass)
3546 MonoCachedClassInfo cached_info;
3547 static int finalize_slot = -1;
3549 if (finalize_slot < 0) {
3551 MonoClass* obj_class = mono_get_object_class ();
3552 mono_class_setup_vtable (obj_class);
3553 for (i = 0; i < obj_class->vtable_size; ++i) {
3554 MonoMethod *cm = obj_class->vtable [i];
3556 if (!strcmp (mono_method_get_name (cm), "Finalize")) {
3563 if (!klass->has_finalize)
3566 if (mono_class_get_cached_class_info (klass, &cached_info))
3567 return mono_get_method (cached_info.finalize_image, cached_info.finalize_token, NULL);
3569 mono_class_setup_vtable (klass);
3570 return klass->vtable [finalize_slot];
3575 * mono_class_needs_cctor_run:
3577 * Determines whenever the class has a static constructor and whenever it
3578 * needs to be called when executing CALLER.
3581 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
3585 method = mono_class_get_cctor (klass);
3587 return (method == caller) ? FALSE : TRUE;
3593 * mono_class_array_element_size:
3596 * Returns: the number of bytes an element of type @klass
3597 * uses when stored into an array.
3600 mono_class_array_element_size (MonoClass *klass)
3602 MonoType *type = &klass->byval_arg;
3605 switch (type->type) {
3608 case MONO_TYPE_BOOLEAN:
3612 case MONO_TYPE_CHAR:
3621 case MONO_TYPE_CLASS:
3622 case MONO_TYPE_STRING:
3623 case MONO_TYPE_OBJECT:
3624 case MONO_TYPE_SZARRAY:
3625 case MONO_TYPE_ARRAY:
3627 case MONO_TYPE_MVAR:
3628 return sizeof (gpointer);
3633 case MONO_TYPE_VALUETYPE:
3634 if (type->data.klass->enumtype) {
3635 type = type->data.klass->enum_basetype;
3636 klass = klass->element_class;
3639 return mono_class_instance_size (klass) - sizeof (MonoObject);
3640 case MONO_TYPE_GENERICINST:
3641 type = &type->data.generic_class->container_class->byval_arg;
3644 g_error ("unknown type 0x%02x in mono_class_array_element_size", type->type);
3650 * mono_array_element_size:
3651 * @ac: pointer to a #MonoArrayClass
3653 * Returns: the size of single array element.
3656 mono_array_element_size (MonoClass *ac)
3658 return mono_class_array_element_size (ac->element_class);
3662 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
3663 MonoGenericContext *context)
3665 if (image->dynamic) {
3666 MonoClass *tmp_handle_class;
3667 gpointer obj = mono_lookup_dynamic_token_class (image, token, &tmp_handle_class);
3669 g_assert (tmp_handle_class);
3671 *handle_class = tmp_handle_class;
3673 if (tmp_handle_class == mono_defaults.typehandle_class)
3674 return &((MonoClass*)obj)->byval_arg;
3679 switch (token & 0xff000000) {
3680 case MONO_TOKEN_TYPE_DEF:
3681 case MONO_TOKEN_TYPE_REF: {
3684 *handle_class = mono_defaults.typehandle_class;
3685 class = mono_class_get_full (image, token, context);
3686 mono_class_init (class);
3687 /* We return a MonoType* as handle */
3688 return &class->byval_arg;
3690 case MONO_TOKEN_TYPE_SPEC: {
3693 *handle_class = mono_defaults.typehandle_class;
3694 class = mono_class_create_from_typespec (image, token, context);
3695 mono_class_init (class);
3696 return &class->byval_arg;
3698 case MONO_TOKEN_FIELD_DEF: {
3700 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
3701 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
3702 mono_class_init (class);
3704 *handle_class = mono_defaults.fieldhandle_class;
3705 return mono_class_get_field (class, token);
3707 case MONO_TOKEN_METHOD_DEF: {
3709 meth = mono_get_method_full (image, token, NULL, context);
3711 *handle_class = mono_defaults.methodhandle_class;
3714 case MONO_TOKEN_MEMBER_REF: {
3715 guint32 cols [MONO_MEMBERREF_SIZE];
3717 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
3718 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
3719 mono_metadata_decode_blob_size (sig, &sig);
3720 if (*sig == 0x6) { /* it's a field */
3722 MonoClassField *field;
3723 field = mono_field_from_token (image, token, &klass, context);
3725 *handle_class = mono_defaults.fieldhandle_class;
3729 meth = mono_get_method_full (image, token, NULL, context);
3731 *handle_class = mono_defaults.methodhandle_class;
3736 g_warning ("Unknown token 0x%08x in ldtoken", token);
3743 * This function might need to call runtime functions so it can't be part
3744 * of the metadata library.
3746 static MonoLookupDynamicToken lookup_dynamic = NULL;
3749 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
3751 lookup_dynamic = func;
3755 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
3757 MonoClass *handle_class;
3759 return lookup_dynamic (image, token, &handle_class);
3763 mono_lookup_dynamic_token_class (MonoImage *image, guint32 token, MonoClass **handle_class)
3765 return lookup_dynamic (image, token, handle_class);
3768 static MonoGetCachedClassInfo get_cached_class_info = NULL;
3771 mono_install_get_cached_class_info (MonoGetCachedClassInfo func)
3773 get_cached_class_info = func;
3777 mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
3779 if (!get_cached_class_info)
3782 return get_cached_class_info (klass, res);
3786 mono_class_get_image (MonoClass *klass)
3788 return klass->image;
3792 * mono_class_get_element_class:
3793 * @klass: the MonoClass to act on
3795 * Returns: the element class of an array or an enumeration.
3798 mono_class_get_element_class (MonoClass *klass)
3800 return klass->element_class;
3804 * mono_class_is_valuetype:
3805 * @klass: the MonoClass to act on
3807 * Returns: true if the MonoClass represents a ValueType.
3810 mono_class_is_valuetype (MonoClass *klass)
3812 return klass->valuetype;
3816 * mono_class_is_enum:
3817 * @klass: the MonoClass to act on
3819 * Returns: true if the MonoClass represents an enumeration.
3822 mono_class_is_enum (MonoClass *klass)
3824 return klass->enumtype;
3828 * mono_class_enum_basetype:
3829 * @klass: the MonoClass to act on
3831 * Returns: the underlying type representation for an enumeration.
3834 mono_class_enum_basetype (MonoClass *klass)
3836 return klass->enum_basetype;
3840 * mono_class_get_parent
3841 * @klass: the MonoClass to act on
3843 * Returns: the parent class for this class.
3846 mono_class_get_parent (MonoClass *klass)
3848 return klass->parent;
3852 * mono_class_get_nesting_type;
3853 * @klass: the MonoClass to act on
3855 * Returns: the container type where this type is nested or NULL if this type is not a nested type.
3858 mono_class_get_nesting_type (MonoClass *klass)
3860 return klass->nested_in;
3864 * mono_class_get_rank:
3865 * @klass: the MonoClass to act on
3867 * Returns: the rank for the array (the number of dimensions).
3870 mono_class_get_rank (MonoClass *klass)
3876 * mono_class_get_flags:
3877 * @klass: the MonoClass to act on
3879 * The type flags from the TypeDef table from the metadata.
3880 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
3883 * Returns: the flags from the TypeDef table.
3886 mono_class_get_flags (MonoClass *klass)
3888 return klass->flags;
3892 * mono_class_get_name
3893 * @klass: the MonoClass to act on
3895 * Returns: the name of the class.
3898 mono_class_get_name (MonoClass *klass)
3904 * mono_class_get_namespace:
3905 * @klass: the MonoClass to act on
3907 * Returns: the namespace of the class.
3910 mono_class_get_namespace (MonoClass *klass)
3912 return klass->name_space;
3916 * mono_class_get_type:
3917 * @klass: the MonoClass to act on
3919 * This method returns the internal Type representation for the class.
3921 * Returns: the MonoType from the class.
3924 mono_class_get_type (MonoClass *klass)
3926 return &klass->byval_arg;
3930 * mono_class_get_byref_type:
3931 * @klass: the MonoClass to act on
3936 mono_class_get_byref_type (MonoClass *klass)
3938 return &klass->this_arg;
3942 * mono_class_num_fields:
3943 * @klass: the MonoClass to act on
3945 * Returns: the number of static and instance fields in the class.
3948 mono_class_num_fields (MonoClass *klass)
3950 return klass->field.count;
3954 * mono_class_num_methods:
3955 * @klass: the MonoClass to act on
3957 * Returns: the number of methods in the class.
3960 mono_class_num_methods (MonoClass *klass)
3962 return klass->method.count;
3966 * mono_class_num_properties
3967 * @klass: the MonoClass to act on
3969 * Returns: the number of properties in the class.
3972 mono_class_num_properties (MonoClass *klass)
3974 mono_class_setup_properties (klass);
3976 return klass->property.count;
3980 * mono_class_num_events:
3981 * @klass: the MonoClass to act on
3983 * Returns: the number of events in the class.
3986 mono_class_num_events (MonoClass *klass)
3988 mono_class_setup_events (klass);
3990 return klass->event.count;
3994 * mono_class_get_fields:
3995 * @klass: the MonoClass to act on
3997 * This routine is an iterator routine for retrieving the fields in a class.
3999 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4000 * iterate over all of the elements. When no more values are
4001 * available, the return value is NULL.
4003 * Returns: a @MonoClassField* on each iteration, or NULL when no more fields are available.
4006 mono_class_get_fields (MonoClass* klass, gpointer *iter)
4008 MonoClassField* field;
4012 mono_class_init (klass);
4014 /* start from the first */
4015 if (klass->field.count) {
4016 return *iter = &klass->fields [0];
4024 if (field < &klass->fields [klass->field.count]) {
4025 return *iter = field;
4031 * mono_class_get_methods
4032 * @klass: the MonoClass to act on
4034 * This routine is an iterator routine for retrieving the fields in a class.
4036 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4037 * iterate over all of the elements. When no more values are
4038 * available, the return value is NULL.
4040 * Returns: a MonoMethod on each iteration or NULL when no more methods are available.
4043 mono_class_get_methods (MonoClass* klass, gpointer *iter)
4045 MonoMethod** method;
4049 mono_class_init (klass);
4051 mono_class_setup_methods (klass);
4052 /* start from the first */
4053 if (klass->method.count) {
4054 *iter = &klass->methods [0];
4055 return klass->methods [0];
4063 if (method < &klass->methods [klass->method.count]) {
4071 * mono_class_get_properties:
4072 * @klass: the MonoClass to act on
4074 * This routine is an iterator routine for retrieving the properties in a class.
4076 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4077 * iterate over all of the elements. When no more values are
4078 * available, the return value is NULL.
4080 * Returns: a @MonoProperty* on each invocation, or NULL when no more are available.
4083 mono_class_get_properties (MonoClass* klass, gpointer *iter)
4085 MonoProperty* property;
4089 mono_class_init (klass);
4091 mono_class_setup_properties (klass);
4092 /* start from the first */
4093 if (klass->property.count) {
4094 return *iter = &klass->properties [0];
4102 if (property < &klass->properties [klass->property.count]) {
4103 return *iter = property;
4109 * mono_class_get_events:
4110 * @klass: the MonoClass to act on
4112 * This routine is an iterator routine for retrieving the properties in a class.
4114 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4115 * iterate over all of the elements. When no more values are
4116 * available, the return value is NULL.
4118 * Returns: a @MonoEvent* on each invocation, or NULL when no more are available.
4121 mono_class_get_events (MonoClass* klass, gpointer *iter)
4127 mono_class_init (klass);
4129 mono_class_setup_events (klass);
4130 /* start from the first */
4131 if (klass->event.count) {
4132 return *iter = &klass->events [0];
4140 if (event < &klass->events [klass->event.count]) {
4141 return *iter = event;
4147 * mono_class_get_interfaces
4148 * @klass: the MonoClass to act on
4150 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
4152 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4153 * iterate over all of the elements. When no more values are
4154 * available, the return value is NULL.
4156 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
4159 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
4165 mono_class_init (klass);
4167 /* start from the first */
4168 if (klass->interface_count) {
4169 *iter = &klass->interfaces [0];
4170 return klass->interfaces [0];
4178 if (iface < &klass->interfaces [klass->interface_count]) {
4186 * mono_class_get_nested_types
4187 * @klass: the MonoClass to act on
4189 * This routine is an iterator routine for retrieving the nested types of a class.
4191 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4192 * iterate over all of the elements. When no more values are
4193 * available, the return value is NULL.
4195 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
4198 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
4204 mono_class_init (klass);
4206 /* start from the first */
4207 if (klass->nested_classes) {
4208 *iter = klass->nested_classes;
4209 return klass->nested_classes->data;
4211 /* no nested types */
4225 * mono_field_get_name:
4226 * @field: the MonoClassField to act on
4228 * Returns: the name of the field.
4231 mono_field_get_name (MonoClassField *field)
4237 * mono_field_get_type:
4238 * @field: the MonoClassField to act on
4240 * Returns: MonoType of the field.
4243 mono_field_get_type (MonoClassField *field)
4249 * mono_field_get_type:
4250 * @field: the MonoClassField to act on
4252 * Returns: MonoClass where the field was defined.
4255 mono_field_get_parent (MonoClassField *field)
4257 return field->parent;
4261 * mono_field_get_flags;
4262 * @field: the MonoClassField to act on
4264 * The metadata flags for a field are encoded using the
4265 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4267 * Returns: the flags for the field.
4270 mono_field_get_flags (MonoClassField *field)
4272 return field->type->attrs;
4276 * mono_property_get_name:
4277 * @prop: the MonoProperty to act on
4279 * Returns: the name of the property
4282 mono_property_get_name (MonoProperty *prop)
4288 * mono_property_get_set_method
4289 * @prop: the MonoProperty to act on.
4291 * Returns: the setter method of the property (A MonoMethod)
4294 mono_property_get_set_method (MonoProperty *prop)
4300 * mono_property_get_get_method
4301 * @prop: the MonoProperty to act on.
4303 * Returns: the setter method of the property (A MonoMethod)
4306 mono_property_get_get_method (MonoProperty *prop)
4312 * mono_property_get_parent:
4313 * @prop: the MonoProperty to act on.
4315 * Returns: the MonoClass where the property was defined.
4318 mono_property_get_parent (MonoProperty *prop)
4320 return prop->parent;
4324 * mono_property_get_flags:
4325 * @prop: the MonoProperty to act on.
4327 * The metadata flags for a property are encoded using the
4328 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4330 * Returns: the flags for the property.
4333 mono_property_get_flags (MonoProperty *prop)
4339 * mono_event_get_name:
4340 * @event: the MonoEvent to act on
4342 * Returns: the name of the event.
4345 mono_event_get_name (MonoEvent *event)
4351 * mono_event_get_add_method:
4352 * @event: The MonoEvent to act on.
4354 * Returns: the @add' method for the event (a MonoMethod).
4357 mono_event_get_add_method (MonoEvent *event)
4363 * mono_event_get_remove_method:
4364 * @event: The MonoEvent to act on.
4366 * Returns: the @remove method for the event (a MonoMethod).
4369 mono_event_get_remove_method (MonoEvent *event)
4371 return event->remove;
4375 * mono_event_get_raise_method:
4376 * @event: The MonoEvent to act on.
4378 * Returns: the @raise method for the event (a MonoMethod).
4381 mono_event_get_raise_method (MonoEvent *event)
4383 return event->raise;
4387 * mono_event_get_parent:
4388 * @event: the MonoEvent to act on.
4390 * Returns: the MonoClass where the event is defined.
4393 mono_event_get_parent (MonoEvent *event)
4395 return event->parent;
4399 * mono_event_get_flags
4400 * @event: the MonoEvent to act on.
4402 * The metadata flags for an event are encoded using the
4403 * EVENT_* constants. See the tabledefs.h file for details.
4405 * Returns: the flags for the event.
4408 mono_event_get_flags (MonoEvent *event)
4410 return event->attrs;
4414 * mono_class_get_method_from_name:
4415 * @klass: where to look for the method
4416 * @name_space: name of the method
4417 * @param_count: number of parameters. -1 for any number.
4419 * Obtains a MonoMethod with a given name and number of parameters.
4420 * It only works if there are no multiple signatures for any given method name.
4423 mono_class_get_method_from_name (MonoClass *klass, const char *name, int param_count)
4425 return mono_class_get_method_from_name_flags (klass, name, param_count, 0);
4429 * mono_class_get_method_from_name_flags:
4430 * @klass: where to look for the method
4431 * @name_space: name of the method
4432 * @param_count: number of parameters. -1 for any number.
4433 * @flags: flags which must be set in the method
4435 * Obtains a MonoMethod with a given name and number of parameters.
4436 * It only works if there are no multiple signatures for any given method name.
4439 mono_class_get_method_from_name_flags (MonoClass *klass, const char *name, int param_count, int flags)
4441 MonoMethod *res = NULL;
4444 mono_class_init (klass);
4446 if (klass->methods) {
4447 mono_class_setup_methods (klass);
4448 for (i = 0; i < klass->method.count; ++i) {
4449 MonoMethod *method = klass->methods [i];
4451 if (method->name[0] == name [0] &&
4452 !strcmp (name, method->name) &&
4453 (param_count == -1 || mono_method_signature (method)->param_count == param_count) &&
4454 ((method->flags & flags) == flags)) {
4461 /* Search directly in the metadata to avoid calling setup_methods () */
4462 for (i = 0; i < klass->method.count; ++i) {
4463 guint32 cols [MONO_METHOD_SIZE];
4466 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_METHOD], klass->method.first + i, cols, MONO_METHOD_SIZE);
4468 if (!strcmp (mono_metadata_string_heap (klass->image, cols [MONO_METHOD_NAME]), name)) {
4469 method = mono_get_method (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass);
4470 if ((param_count == -1) || mono_method_signature (method)->param_count == param_count) {
4482 * mono_class_set_failure:
4483 * @klass: class in which the failure was detected
4484 * @ex_type: the kind of exception/error to be thrown (later)
4485 * @ex_data: exception data (specific to each type of exception/error)
4487 * Keep a detected failure informations in the class for later processing.
4488 * Note that only the first failure is kept.
4491 mono_class_set_failure (MonoClass *klass, guint32 ex_type, void *ex_data)
4493 if (klass->exception_type)
4495 klass->exception_type = ex_type;
4496 klass->exception_data = ex_data;
4501 * mono_class_get_exception_for_failure:
4502 * @klass: class in which the failure was detected
4504 * Return a constructed MonoException than the caller can then throw
4505 * using mono_raise_exception - or NULL if no failure is present (or
4506 * doesn't result in an exception).
4509 mono_class_get_exception_for_failure (MonoClass *klass)
4511 switch (klass->exception_type) {
4512 case MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND: {
4513 MonoDomain *domain = mono_domain_get ();
4514 MonoSecurityManager* secman = mono_security_manager_get_methods ();
4515 MonoMethod *method = klass->exception_data;
4516 guint32 error = (method) ? MONO_METADATA_INHERITANCEDEMAND_CLASS : MONO_METADATA_INHERITANCEDEMAND_METHOD;
4517 MonoObject *exc = NULL;
4521 args [1] = mono_assembly_get_object (domain, mono_image_get_assembly (klass->image));
4522 args [2] = mono_type_get_object (domain, &klass->byval_arg);
4523 args [3] = (method) ? mono_method_get_object (domain, method, NULL) : NULL;
4525 mono_runtime_invoke (secman->inheritsecurityexception, NULL, args, &exc);
4526 return (MonoException*) exc;
4528 /* TODO - handle other class related failures */