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)
266 GString* result = g_string_new ("");
268 mono_type_get_name_recurse (type, result, FALSE, format);
271 g_string_append_c (result, '&');
273 return g_string_free (result, FALSE);
277 mono_type_get_name (MonoType *type)
279 return mono_type_get_name_full (type, MONO_TYPE_NAME_FORMAT_IL);
283 mono_type_get_underlying_type (MonoType *type)
285 switch (type->type) {
286 case MONO_TYPE_VALUETYPE:
287 if (type->data.klass->enumtype)
288 return type->data.klass->enum_basetype;
290 case MONO_TYPE_GENERICINST:
291 return mono_type_get_underlying_type (&type->data.generic_class->container_class->byval_arg);
300 mono_class_is_open_constructed_type (MonoType *t)
306 case MONO_TYPE_SZARRAY:
307 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
308 case MONO_TYPE_ARRAY:
309 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
311 return mono_class_is_open_constructed_type (t->data.type);
312 case MONO_TYPE_GENERICINST: {
313 MonoGenericClass *gclass = t->data.generic_class;
316 if (mono_class_is_open_constructed_type (&gclass->container_class->byval_arg))
318 for (i = 0; i < gclass->inst->type_argc; i++)
319 if (mono_class_is_open_constructed_type (gclass->inst->type_argv [i]))
328 static MonoGenericClass *
329 inflate_generic_class (MonoGenericClass *ogclass, MonoGenericContext *context)
331 MonoInflatedGenericClass *igclass;
332 MonoGenericClass *ngclass, *cached;
334 if (ogclass->is_dynamic) {
335 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
336 igclass = &dgclass->generic_class;
337 ngclass = &igclass->generic_class;
338 ngclass->is_inflated = 1;
339 ngclass->is_dynamic = 1;
341 igclass = g_new0 (MonoInflatedGenericClass, 1);
342 ngclass = &igclass->generic_class;
343 ngclass->is_inflated = 1;
348 ngclass->inst = mono_metadata_inflate_generic_inst (ogclass->inst, context);
350 igclass->klass = NULL;
352 ngclass->context = g_new0 (MonoGenericContext, 1);
353 ngclass->context->container = context->container;
354 ngclass->context->gclass = ngclass;
357 cached = mono_metadata_lookup_generic_class (ngclass);
358 mono_loader_unlock ();
368 inflate_generic_type (MonoType *type, MonoGenericContext *context)
370 switch (type->type) {
372 if (context->gmethod && context->gmethod->inst->type_argv)
374 context->gmethod->inst->type_argv [type->data.generic_param->num],
381 context->gclass->inst->type_argv [type->data.generic_param->num],
385 case MONO_TYPE_SZARRAY: {
386 MonoClass *eclass = type->data.klass;
387 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
390 nt = dup_type (type, type);
391 nt->data.klass = mono_class_from_mono_type (inflated);
394 case MONO_TYPE_ARRAY: {
395 MonoClass *eclass = type->data.array->eklass;
396 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
399 nt = dup_type (type, type);
400 nt->data.array = g_memdup (nt->data.array, sizeof (MonoArrayType));
401 nt->data.array->eklass = mono_class_from_mono_type (inflated);
404 case MONO_TYPE_GENERICINST: {
405 MonoGenericClass *ngclass = inflate_generic_class (type->data.generic_class, context);
406 MonoType *nt = dup_type (type, type);
407 nt->data.generic_class = ngclass;
410 case MONO_TYPE_CLASS:
411 case MONO_TYPE_VALUETYPE: {
412 MonoClass *klass = type->data.klass;
413 MonoGenericClass *gclass;
416 if (!klass->generic_container)
419 gclass = inflate_generic_class (klass->generic_container->context.gclass, context);
421 nt = dup_type (type, type);
422 nt->type = MONO_TYPE_GENERICINST;
423 nt->data.generic_class = gclass;
432 MonoInflatedGenericClass*
433 mono_get_inflated_generic_class (MonoGenericClass *gclass)
435 g_assert (gclass->is_inflated);
436 mono_class_create_generic ((MonoInflatedGenericClass *) gclass);
437 return (MonoInflatedGenericClass *) gclass;
441 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
443 MonoType *inflated = inflate_generic_type (type, context);
446 return dup_type (type, type);
448 mono_stats.inflated_type_count++;
453 mono_class_inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
455 MonoMethodSignature *res;
462 res = mono_metadata_signature_alloc (image, sig->param_count);
463 res->ret = mono_class_inflate_generic_type (sig->ret, context);
464 is_open = mono_class_is_open_constructed_type (res->ret);
465 for (i = 0; i < sig->param_count; ++i) {
466 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
468 is_open = mono_class_is_open_constructed_type (res->params [i]);
470 res->hasthis = sig->hasthis;
471 res->explicit_this = sig->explicit_this;
472 res->call_convention = sig->call_convention;
473 res->pinvoke = sig->pinvoke;
474 res->generic_param_count = sig->generic_param_count;
475 res->sentinelpos = sig->sentinelpos;
476 res->has_type_parameters = is_open;
477 res->is_inflated = 1;
481 static MonoMethodHeader*
482 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
484 MonoMethodHeader *res;
486 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
487 res->code = header->code;
488 res->code_size = header->code_size;
489 res->max_stack = header->max_stack;
490 res->num_clauses = header->num_clauses;
491 res->init_locals = header->init_locals;
492 res->num_locals = header->num_locals;
493 res->clauses = header->clauses;
494 for (i = 0; i < header->num_locals; ++i)
495 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
499 static MonoGenericContext *
500 inflate_generic_context (MonoGenericContext *context, MonoGenericContext *inflate_with)
502 MonoGenericContext *res = g_new0 (MonoGenericContext, 1);
504 res->container = context->container;
507 res->gclass = inflate_generic_class (context->gclass, inflate_with);
509 if (context->gmethod) {
510 res->gmethod = g_new0 (MonoGenericMethod, 1);
511 res->gmethod->generic_class = res->gclass;
513 res->gmethod->container = context->gmethod->container;
514 res->gmethod->inst = mono_metadata_inflate_generic_inst (context->gmethod->inst, inflate_with);
516 res->gmethod = inflate_with->gmethod;
522 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context)
524 MonoMethodInflated *result;
526 if (method->is_inflated || mono_method_signature_full (method, context)->is_inflated) {
527 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
529 context = inflate_generic_context (imethod->context, context);
530 method = imethod->declaring;
533 mono_stats.inflated_method_count++;
535 result = g_new0 (MonoMethodInflated, 1);
536 result->nmethod = *(MonoMethodNormal*)method;
537 result->nmethod.method.is_inflated = 1;
538 result->context = context;
539 result->declaring = method;
541 if (result->nmethod.method.klass->generic_class)
542 result->nmethod.method.klass = result->nmethod.method.klass->generic_class->container_class;
544 return (MonoMethod *) result;
548 mono_get_inflated_method (MonoMethod *method)
550 MonoMethodInflated *imethod, *res;
551 MonoMethodHeader *mh;
555 if (!method->is_inflated)
558 imethod = (MonoMethodInflated *) method;
559 if (imethod->inflated)
560 return (MonoMethod *) imethod->inflated;
562 mono_stats.inflated_method_count_2++;
563 mono_stats.generics_metadata_size +=
564 sizeof (MonoMethodInflated) - sizeof (MonoMethodNormal);
566 res = g_new0 (MonoMethodInflated, 1);
568 res->inflated = imethod->inflated = res;
570 mh = mono_method_get_header (method);
572 res->nmethod.header = inflate_generic_header (mh, imethod->context);
574 dtype = mono_class_inflate_generic_type (&method->klass->byval_arg, imethod->context);
575 rklass = res->nmethod.method.klass = mono_class_from_mono_type (dtype);
577 res->nmethod.method.signature = mono_class_inflate_generic_signature (
578 method->klass->image, mono_method_signature (method), imethod->context);
580 return (MonoMethod *) res;
584 * class_compute_field_layout:
585 * @m: pointer to the metadata.
586 * @class: The class to initialize
588 * Initializes the class->fields.
591 class_compute_field_layout (MonoClass *class)
593 MonoImage *m = class->image;
594 const int top = class->field.count;
595 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
596 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
597 int i, blittable = TRUE, real_size = 0;
599 guint32 packing_size = 0;
600 gboolean explicit_size;
601 MonoClassField *field;
603 if (class->size_inited)
607 if (!class->parent->size_inited)
608 class_compute_field_layout (class->parent);
609 class->instance_size += class->parent->instance_size;
610 class->min_align = class->parent->min_align;
611 /* we use |= since it may have been set already */
612 class->has_references |= class->parent->has_references;
613 blittable = class->parent->blittable;
615 class->instance_size = sizeof (MonoObject);
616 class->min_align = 1;
619 /* Get the real size */
620 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
623 g_assert ((packing_size & 0xfffffff0) == 0);
624 class->packing_size = packing_size;
625 real_size += class->instance_size;
629 if (explicit_size && real_size) {
630 class->instance_size = MAX (real_size, class->instance_size);
632 class->size_inited = 1;
633 class->blittable = blittable;
637 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT)
640 /* Prevent infinite loops if the class references itself */
641 class->size_inited = 1;
643 class->fields = g_new0 (MonoClassField, top);
646 * Fetch all the field information.
648 for (i = 0; i < top; i++){
650 guint32 cols [MONO_FIELD_SIZE];
651 int idx = class->field.first + i;
652 MonoGenericContainer *container = NULL;
654 field = &class->fields [i];
655 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
656 /* The name is needed for fieldrefs */
657 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
658 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
659 mono_metadata_decode_value (sig, &sig);
660 /* FIELD signature == 0x06 */
661 g_assert (*sig == 0x06);
662 if (class->generic_container)
663 container = class->generic_container;
664 else if (class->generic_class) {
665 MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
666 MonoClass *gklass = class->generic_class->container_class;
668 container = gklass->generic_container;
669 g_assert (container);
671 ifield->generic_type = gklass->fields [i].type;
672 field->generic_info = ifield;
674 field->type = mono_metadata_parse_type_full (
675 m, (MonoGenericContext *) container, MONO_PARSE_FIELD,
676 cols [MONO_FIELD_FLAGS], sig + 1, &sig);
677 if (mono_field_is_deleted (field))
679 if (class->generic_class) {
680 field->type = mono_class_inflate_generic_type (
681 field->type, class->generic_class->context);
682 field->type->attrs = cols [MONO_FIELD_FLAGS];
685 field->parent = class;
687 /* Only do these checks if we still think this type is blittable */
688 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
689 if (field->type->byref || MONO_TYPE_IS_REFERENCE (field->type)) {
692 MonoClass *field_class = mono_class_from_mono_type (field->type);
693 if (!field_class || !field_class->blittable)
698 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
699 mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
700 if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
701 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, field->name);
704 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
705 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
707 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
708 field->data = mono_image_rva_map (class->image, rva);
711 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
712 class->enum_basetype = field->type;
713 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
714 blittable = class->element_class->blittable;
717 /* The def_value of fields is compute lazily during vtable creation */
720 if (class == mono_defaults.string_class)
723 class->blittable = blittable;
725 if (class->enumtype && !class->enum_basetype) {
726 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
729 if (explicit_size && real_size) {
730 class->instance_size = MAX (real_size, class->instance_size);
733 mono_class_layout_fields (class);
736 /* useful until we keep track of gc-references in corlib etc. */
737 #define IS_GC_REFERENCE(t) ((t)->type == MONO_TYPE_U || (t)->type == MONO_TYPE_I || (t)->type == MONO_TYPE_PTR)
740 * LOCKING: this is supposed to be called with the loader lock held.
743 mono_class_layout_fields (MonoClass *class)
746 const int top = class->field.count;
747 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
748 guint32 pass, passes, real_size;
749 gboolean gc_aware_layout = FALSE;
750 MonoClassField *field;
752 if (class->generic_container ||
753 (class->generic_class && class->generic_class->inst->is_open))
757 * Enable GC aware auto layout: in this mode, reference
758 * fields are grouped together inside objects, increasing collector
760 * Requires that all classes whose layout is known to native code be annotated
761 * with [StructLayout (LayoutKind.Sequential)]
762 * Value types have gc_aware_layout disabled by default, as per
763 * what the default is for other runtimes.
765 /* corlib is missing [StructLayout] directives in many places */
766 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
767 if (class->image != mono_defaults.corlib &&
768 class->byval_arg.type != MONO_TYPE_VALUETYPE)
769 gc_aware_layout = TRUE;
773 * Compute field layout and total size (not considering static fields)
777 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
778 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
785 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
789 real_size = class->parent->instance_size;
791 real_size = sizeof (MonoObject);
793 for (pass = 0; pass < passes; ++pass) {
794 for (i = 0; i < top; i++){
796 field = &class->fields [i];
798 if (mono_field_is_deleted (field))
800 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
803 if (gc_aware_layout) {
805 * We process fields with reference type in the first pass,
806 * and fields with non-reference type in the second pass.
807 * We use IS_POINTER instead of IS_REFERENCE because in
808 * some internal structures, we store GC_MALLOCed memory
809 * in IntPtr fields...
811 if (MONO_TYPE_IS_POINTER (field->type)) {
820 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
821 (strcmp (field->name, "$PRIVATE$") == 0)) {
822 /* This field is a hack inserted by MCS to empty structures */
826 size = mono_type_size (field->type, &align);
828 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
829 align = class->packing_size ? MIN (class->packing_size, align): align;
830 class->min_align = MAX (align, class->min_align);
831 field->offset = real_size;
832 field->offset += align - 1;
833 field->offset &= ~(align - 1);
834 real_size = field->offset + size;
835 if (MONO_TYPE_IS_REFERENCE (field->type) || IS_GC_REFERENCE (field->type))
836 class->has_references = TRUE;
837 else if (field->type->type == MONO_TYPE_VALUETYPE && field->type->data.klass->has_references)
838 class->has_references = TRUE;
841 class->instance_size = MAX (real_size, class->instance_size);
843 if (class->instance_size & (class->min_align - 1)) {
844 class->instance_size += class->min_align - 1;
845 class->instance_size &= ~(class->min_align - 1);
849 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
851 for (i = 0; i < top; i++) {
853 field = &class->fields [i];
856 * There must be info about all the fields in a type if it
857 * uses explicit layout.
860 if (mono_field_is_deleted (field))
862 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
865 if (MONO_TYPE_IS_REFERENCE (field->type) || IS_GC_REFERENCE (field->type))
866 class->has_references = TRUE;
867 else if (field->type->type == MONO_TYPE_VALUETYPE && field->type->data.klass->has_references)
868 class->has_references = TRUE;
869 size = mono_type_size (field->type, &align);
872 * When we get here, field->offset is already set by the
873 * loader (for either runtime fields or fields loaded from metadata).
874 * The offset is from the start of the object: this works for both
875 * classes and valuetypes.
877 field->offset += sizeof (MonoObject);
882 real_size = MAX (real_size, size + field->offset);
884 class->instance_size = MAX (real_size, class->instance_size);
888 class->size_inited = 1;
891 * Compute static field layout and size
893 for (i = 0; i < top; i++){
895 field = &class->fields [i];
897 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
899 if (mono_field_is_deleted (field))
902 if (MONO_TYPE_IS_REFERENCE (field->type) || IS_GC_REFERENCE (field->type))
903 class->has_static_refs = TRUE;
904 else if (field->type->type == MONO_TYPE_VALUETYPE && field->type->data.klass->has_references)
905 class->has_static_refs = TRUE;
906 size = mono_type_size (field->type, &align);
907 field->offset = class->class_size;
908 field->offset += align - 1;
909 field->offset &= ~(align - 1);
910 class->class_size = field->offset + size;
915 * mono_class_setup_methods:
917 * Initializes the 'methods' array in the klass.
918 * Calling this method should be avoided if possible since it allocates a lot
919 * of long-living MonoMethod structures.
922 mono_class_setup_methods (MonoClass *class)
925 MonoMethod **methods;
927 if (class->methods || class->generic_class)
932 if (class->methods) {
933 mono_loader_unlock ();
937 //printf ("INIT: %s.%s\n", class->name_space, class->name);
939 if (!class->generic_class && !class->methods) {
940 methods = g_new (MonoMethod*, class->method.count);
941 for (i = 0; i < class->method.count; ++i) {
942 methods [i] = mono_get_method (class->image,
943 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
947 if (MONO_CLASS_IS_INTERFACE (class))
948 for (i = 0; i < class->method.count; ++i)
949 methods [i]->slot = i;
951 class->methods = methods;
953 mono_loader_unlock ();
958 mono_class_setup_properties (MonoClass *class)
960 guint startm, endm, i, j;
961 guint32 cols [MONO_PROPERTY_SIZE];
962 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
963 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
964 MonoProperty *properties;
966 if (class->properties)
971 if (class->properties) {
972 mono_loader_unlock ();
976 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
977 class->property.count = class->property.last - class->property.first;
979 if (class->property.count)
980 mono_class_setup_methods (class);
982 properties = g_new0 (MonoProperty, class->property.count);
983 for (i = class->property.first; i < class->property.last; ++i) {
984 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
985 properties [i - class->property.first].parent = class;
986 properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
987 properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
989 startm = mono_metadata_methods_from_property (class->image, i, &endm);
990 for (j = startm; j < endm; ++j) {
991 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
992 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
993 case METHOD_SEMANTIC_SETTER:
994 properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
996 case METHOD_SEMANTIC_GETTER:
997 properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1005 class->properties = properties;
1007 mono_loader_unlock ();
1011 mono_class_setup_events (MonoClass *class)
1013 guint startm, endm, i, j;
1014 guint32 cols [MONO_EVENT_SIZE];
1015 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
1016 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
1021 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
1022 class->event.count = class->event.last - class->event.first;
1024 if (class->event.count)
1025 mono_class_setup_methods (class);
1027 class->events = g_new0 (MonoEvent, class->event.count);
1028 for (i = class->event.first; i < class->event.last; ++i) {
1029 MonoEvent *event = &class->events [i - class->event.first];
1031 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
1032 event->parent = class;
1033 event->attrs = cols [MONO_EVENT_FLAGS];
1034 event->name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
1036 startm = mono_metadata_methods_from_event (class->image, i, &endm);
1037 for (j = startm; j < endm; ++j) {
1038 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
1039 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1040 case METHOD_SEMANTIC_ADD_ON:
1041 event->add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1043 case METHOD_SEMANTIC_REMOVE_ON:
1044 event->remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1046 case METHOD_SEMANTIC_FIRE:
1047 event->raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1049 case METHOD_SEMANTIC_OTHER: {
1052 if (event->other == NULL) {
1053 event->other = g_new0 (MonoMethod*, 1);
1055 while (event->other [n])
1057 event->other = g_realloc (event->other, (n + 1) * sizeof (MonoMethod*));
1059 event->other [n] = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1070 mono_get_unique_iid (MonoClass *class)
1072 static GHashTable *iid_hash = NULL;
1073 static guint iid = 0;
1078 g_assert (MONO_CLASS_IS_INTERFACE (class));
1080 mono_loader_lock ();
1083 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
1085 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
1087 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
1088 mono_loader_unlock ();
1090 return GPOINTER_TO_INT (value);
1092 g_hash_table_insert (iid_hash, str, GINT_TO_POINTER (iid));
1096 mono_loader_unlock ();
1098 g_assert (iid <= 65535);
1103 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
1108 for (i = 0; i < klass->interface_count; i++) {
1109 ic = klass->interfaces [i];
1112 *res = g_ptr_array_new ();
1113 g_ptr_array_add (*res, ic);
1114 mono_class_init (ic);
1116 collect_implemented_interfaces_aux (ic, res);
1121 mono_class_get_implemented_interfaces (MonoClass *klass)
1123 GPtrArray *res = NULL;
1125 collect_implemented_interfaces_aux (klass, &res);
1129 typedef struct _IOffsetInfo IOffsetInfo;
1130 struct _IOffsetInfo {
1134 int data [MONO_ZERO_LEN_ARRAY];
1137 static IOffsetInfo *cached_offset_info = NULL;
1138 static int next_offset_info_size = 128;
1141 cache_interface_offsets (int max_iid, int *data)
1143 IOffsetInfo *cached_info;
1146 for (cached_info = cached_offset_info; cached_info; cached_info = cached_info->next) {
1147 cached = cached_info->data;
1148 while (cached < cached_info->data + cached_info->size && *cached) {
1149 if (*cached == max_iid) {
1150 int i, matched = TRUE;
1152 for (i = 0; i < max_iid; ++i) {
1153 if (cached [i] != data [i]) {
1162 cached += *cached + 1;
1166 /* find a free slot */
1167 for (cached_info = cached_offset_info; cached_info; cached_info = cached_info->next) {
1168 if (cached_info->size - cached_info->next_free >= max_iid + 1) {
1169 cached = &cached_info->data [cached_info->next_free];
1170 *cached++ = max_iid;
1171 memcpy (cached, data, max_iid * sizeof (int));
1172 cached_info->next_free += max_iid + 1;
1176 /* allocate a new chunk */
1177 if (max_iid + 1 < next_offset_info_size) {
1178 new_size = next_offset_info_size;
1179 if (next_offset_info_size < 4096)
1180 next_offset_info_size += next_offset_info_size >> 2;
1182 new_size = max_iid + 1;
1184 cached_info = g_malloc0 (sizeof (IOffsetInfo) + sizeof (int) * new_size);
1185 cached_info->size = new_size;
1186 /*g_print ("allocated %d offset entries at %p (total: %d)\n", new_size, cached_info->data, offset_info_total_size);*/
1187 cached = &cached_info->data [0];
1188 *cached++ = max_iid;
1189 memcpy (cached, data, max_iid * sizeof (int));
1190 cached_info->next_free += max_iid + 1;
1191 cached_info->next = cached_offset_info;
1192 cached_offset_info = cached_info;
1197 setup_interface_offsets (MonoClass *class, int cur_slot)
1204 /* compute maximum number of slots and maximum interface id */
1206 for (k = class; k ; k = k->parent) {
1207 for (i = 0; i < k->interface_count; i++) {
1208 ic = k->interfaces [i];
1211 mono_class_init (ic);
1213 if (max_iid < ic->interface_id)
1214 max_iid = ic->interface_id;
1216 ifaces = mono_class_get_implemented_interfaces (k);
1218 for (i = 0; i < ifaces->len; ++i) {
1219 ic = g_ptr_array_index (ifaces, i);
1220 if (max_iid < ic->interface_id)
1221 max_iid = ic->interface_id;
1223 g_ptr_array_free (ifaces, TRUE);
1227 if (MONO_CLASS_IS_INTERFACE (class)) {
1228 if (max_iid < class->interface_id)
1229 max_iid = class->interface_id;
1231 class->max_interface_id = max_iid;
1232 /* compute vtable offset for interfaces */
1233 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
1235 for (i = 0; i <= max_iid; i++)
1236 class->interface_offsets [i] = -1;
1238 ifaces = mono_class_get_implemented_interfaces (class);
1240 for (i = 0; i < ifaces->len; ++i) {
1241 ic = g_ptr_array_index (ifaces, i);
1242 class->interface_offsets [ic->interface_id] = cur_slot;
1243 cur_slot += ic->method.count;
1245 g_ptr_array_free (ifaces, TRUE);
1248 for (k = class->parent; k ; k = k->parent) {
1249 ifaces = mono_class_get_implemented_interfaces (k);
1251 for (i = 0; i < ifaces->len; ++i) {
1252 ic = g_ptr_array_index (ifaces, i);
1254 if (class->interface_offsets [ic->interface_id] == -1) {
1255 int io = k->interface_offsets [ic->interface_id];
1259 class->interface_offsets [ic->interface_id] = io;
1262 g_ptr_array_free (ifaces, TRUE);
1266 if (MONO_CLASS_IS_INTERFACE (class))
1267 class->interface_offsets [class->interface_id] = cur_slot;
1269 cached_data = cache_interface_offsets (max_iid + 1, class->interface_offsets);
1270 g_free (class->interface_offsets);
1271 class->interface_offsets = cached_data;
1277 mono_class_setup_vtable (MonoClass *class)
1279 MonoMethod **overrides;
1280 MonoGenericContext *context;
1286 mono_class_setup_methods (class);
1288 if (MONO_CLASS_IS_INTERFACE (class))
1291 mono_loader_lock ();
1293 if (class->vtable) {
1294 mono_loader_unlock ();
1298 if (class->generic_class)
1299 context = class->generic_class->context;
1301 context = (MonoGenericContext *) class->generic_container;
1303 overrides = mono_class_get_overrides_full (
1304 class->image, class->type_token, &onum, context);
1305 mono_class_setup_vtable_general (class, overrides, onum);
1308 mono_loader_unlock ();
1312 setup_generic_vtable (MonoClass *class, MonoMethod **overrides, int onum)
1317 gklass = class->generic_class->container_class;
1319 mono_class_init (gklass);
1320 class->vtable_size = gklass->vtable_size;
1322 class->vtable = g_new0 (MonoMethod*, class->vtable_size);
1323 memcpy (class->vtable, gklass->vtable, sizeof (MonoMethod*) * class->vtable_size);
1325 for (i = 0; i < class->vtable_size; i++) {
1326 MonoMethod *m = class->vtable [i];
1331 m = mono_class_inflate_generic_method (m, class->generic_class->context);
1332 class->vtable [i] = m;
1335 class->max_interface_id = gklass->max_interface_id;
1336 class->interface_offsets = g_new0 (gint, gklass->max_interface_id + 1);
1337 memcpy (class->interface_offsets, gklass->interface_offsets,
1338 sizeof (gint) * (gklass->max_interface_id + 1));
1342 * LOCKING: this is supposed to be called with the loader lock held.
1345 mono_class_setup_vtable_general (MonoClass *class, MonoMethod **overrides, int onum)
1348 MonoMethod **vtable;
1349 int i, max_vtsize = 0, max_iid, cur_slot = 0;
1351 GHashTable *override_map = NULL;
1352 gboolean security_enabled = mono_is_security_manager_active ();
1357 if (class->generic_class) {
1358 setup_generic_vtable (class, overrides, onum);
1362 ifaces = mono_class_get_implemented_interfaces (class);
1364 for (i = 0; i < ifaces->len; i++) {
1365 MonoClass *ic = g_ptr_array_index (ifaces, i);
1366 max_vtsize += ic->method.count;
1368 g_ptr_array_free (ifaces, TRUE);
1371 if (class->parent) {
1372 mono_class_init (class->parent);
1373 mono_class_setup_vtable (class->parent);
1374 max_vtsize += class->parent->vtable_size;
1375 cur_slot = class->parent->vtable_size;
1378 max_vtsize += class->method.count;
1380 vtable = alloca (sizeof (gpointer) * max_vtsize);
1381 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
1383 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
1385 cur_slot = setup_interface_offsets (class, cur_slot);
1386 max_iid = class->max_interface_id;
1388 if (class->parent && class->parent->vtable_size)
1389 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
1391 /* override interface methods */
1392 for (i = 0; i < onum; i++) {
1393 MonoMethod *decl = overrides [i*2];
1394 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
1396 mono_class_setup_methods (decl->klass);
1397 g_assert (decl->slot != -1);
1398 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
1399 vtable [dslot] = overrides [i*2 + 1];
1400 vtable [dslot]->slot = dslot;
1402 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1404 g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
1408 for (k = class; k ; k = k->parent) {
1411 ifaces = mono_class_get_implemented_interfaces (k);
1413 nifaces = ifaces->len;
1414 for (i = 0; i < nifaces; i++) {
1417 ic = g_ptr_array_index (ifaces, i);
1418 io = k->interface_offsets [ic->interface_id];
1421 g_assert (io <= max_vtsize);
1424 mono_class_setup_methods (ic);
1425 for (l = 0; l < ic->method.count; l++) {
1426 MonoMethod *im = ic->methods [l];
1428 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1431 for (j = 0; j < class->method.count; ++j) {
1432 MonoMethod *cm = class->methods [j];
1433 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1434 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
1435 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
1437 if (!strcmp(cm->name, im->name) &&
1438 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1440 /* CAS - SecurityAction.InheritanceDemand on interface */
1441 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1442 mono_secman_inheritancedemand_method (cm, im);
1445 g_assert (io + l <= max_vtsize);
1446 vtable [io + l] = cm;
1451 /* already implemented */
1452 if (io >= k->vtable_size)
1456 for (l = 0; l < ic->method.count; l++) {
1457 MonoMethod *im = ic->methods [l];
1460 g_assert (io + l <= max_vtsize);
1462 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1465 for (k1 = class; k1; k1 = k1->parent) {
1466 for (j = 0; j < k1->method.count; ++j) {
1467 MonoMethod *cm = k1->methods [j];
1469 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1470 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
1473 if (!strcmp(cm->name, im->name) &&
1474 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1476 /* CAS - SecurityAction.InheritanceDemand on interface */
1477 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1478 mono_secman_inheritancedemand_method (cm, im);
1481 g_assert (io + l <= max_vtsize);
1482 vtable [io + l] = cm;
1487 g_assert (io + l <= max_vtsize);
1488 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1493 for (l = 0; l < ic->method.count; l++) {
1494 MonoMethod *im = ic->methods [l];
1495 char *qname, *fqname, *cname, *the_cname;
1498 if (vtable [io + l])
1501 if (ic->generic_class) {
1502 the_cname = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
1506 cname = (char*)ic->name;
1509 qname = g_strconcat (cname, ".", im->name, NULL);
1510 if (ic->name_space && ic->name_space [0])
1511 fqname = g_strconcat (ic->name_space, ".", cname, ".", im->name, NULL);
1515 for (k1 = class; k1; k1 = k1->parent) {
1516 for (j = 0; j < k1->method.count; ++j) {
1517 MonoMethod *cm = k1->methods [j];
1519 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1522 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
1523 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1525 /* CAS - SecurityAction.InheritanceDemand on interface */
1526 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1527 mono_secman_inheritancedemand_method (cm, im);
1530 g_assert (io + l <= max_vtsize);
1531 vtable [io + l] = cm;
1542 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
1543 for (l = 0; l < ic->method.count; l++) {
1545 MonoMethod *im = ic->methods [l];
1546 if (im->flags & METHOD_ATTRIBUTE_STATIC)
1548 g_assert (io + l <= max_vtsize);
1551 * If one of our parents already implements this interface
1552 * we can inherit the implementation.
1554 if (!(vtable [io + l])) {
1555 MonoClass *parent = class->parent;
1557 if ((ic->interface_id <= parent->max_interface_id) &&
1558 (parent->interface_offsets [ic->interface_id]) &&
1560 vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
1563 if (!(vtable [io + l])) {
1564 for (j = 0; j < onum; ++j) {
1565 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
1566 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
1568 msig = mono_signature_get_desc (mono_method_signature (im), FALSE);
1569 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
1570 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
1572 for (j = 0; j < class->method.count; ++j) {
1573 MonoMethod *cm = class->methods [j];
1574 msig = mono_signature_get_desc (mono_method_signature (cm), FALSE);
1576 printf ("METHOD %s(%s)\n", cm->name, msig);
1579 g_assert_not_reached ();
1584 for (l = 0; l < ic->method.count; l++) {
1585 MonoMethod *im = vtable [io + l];
1588 g_assert (io + l <= max_vtsize);
1590 /* FIXME: why do we need this ? */
1592 /* g_assert_not_reached (); */
1598 g_ptr_array_free (ifaces, TRUE);
1601 for (i = 0; i < class->method.count; ++i) {
1604 cm = class->methods [i];
1607 * Non-virtual method have no place in the vtable.
1608 * This also catches static methods (since they are not virtual).
1610 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1614 * If the method is REUSE_SLOT, we must check in the
1615 * base class for a method to override.
1617 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
1619 for (k = class->parent; k ; k = k->parent) {
1621 for (j = 0; j < k->method.count; ++j) {
1622 MonoMethod *m1 = k->methods [j];
1623 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
1625 if (!strcmp(cm->name, m1->name) &&
1626 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (m1))) {
1628 /* CAS - SecurityAction.InheritanceDemand */
1629 if (security_enabled && (m1->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1630 mono_secman_inheritancedemand_method (cm, m1);
1633 slot = k->methods [j]->slot;
1634 g_assert (cm->slot < max_vtsize);
1636 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1637 g_hash_table_insert (override_map, m1, cm);
1649 cm->slot = cur_slot++;
1651 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT) && ! mono_method_signature (cm)->generic_param_count)
1652 vtable [cm->slot] = cm;
1655 /* override non interface methods */
1656 for (i = 0; i < onum; i++) {
1657 MonoMethod *decl = overrides [i*2];
1658 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
1659 g_assert (decl->slot != -1);
1660 vtable [decl->slot] = overrides [i*2 + 1];
1661 overrides [i * 2 + 1]->slot = decl->slot;
1663 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1664 g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
1669 * If a method occupies more than one place in the vtable, and it is
1670 * overriden, then change the other occurances too.
1673 for (i = 0; i < max_vtsize; ++i)
1675 MonoMethod *cm = g_hash_table_lookup (override_map, vtable [i]);
1680 g_hash_table_destroy (override_map);
1683 if (class->generic_class) {
1684 MonoClass *gklass = class->generic_class->container_class;
1686 mono_class_init (gklass);
1687 class->vtable_size = gklass->vtable_size;
1689 class->vtable_size = cur_slot;
1691 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
1692 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
1694 if (mono_print_vtable) {
1697 for (i = 0; i <= max_iid; i++)
1698 if (class->interface_offsets [i] != -1)
1701 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
1702 class->name, class->vtable_size, icount);
1704 for (i = 0; i < class->vtable_size; ++i) {
1709 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
1710 cm->klass->name_space, cm->klass->name,
1717 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
1718 class->name, max_iid);
1720 for (i = 0; i < class->interface_count; i++) {
1721 ic = class->interfaces [i];
1722 printf (" slot %03d(%03d) %s.%s\n",
1723 class->interface_offsets [ic->interface_id],
1724 ic->method.count, ic->name_space, ic->name);
1727 for (k = class->parent; k ; k = k->parent) {
1728 for (i = 0; i < k->interface_count; i++) {
1729 ic = k->interfaces [i];
1730 printf (" slot %03d(%03d) %s.%s\n",
1731 class->interface_offsets [ic->interface_id],
1732 ic->method.count, ic->name_space, ic->name);
1741 * @class: the class to initialize
1743 * compute the instance_size, class_size and other infos that cannot be
1744 * computed at mono_class_get() time. Also compute a generic vtable and
1745 * the method slot numbers. We use this infos later to create a domain
1749 mono_class_init (MonoClass *class)
1752 static MonoMethod *default_ghc = NULL;
1753 static MonoMethod *default_finalize = NULL;
1754 static int finalize_slot = -1;
1755 static int ghc_slot = -1;
1756 MonoCachedClassInfo cached_info;
1763 /*g_print ("Init class %s\n", class->name);*/
1765 /* We do everything inside the lock to prevent races */
1766 mono_loader_lock ();
1768 if (class->inited) {
1769 mono_loader_unlock ();
1770 /* Somebody might have gotten in before us */
1774 if (class->init_pending) {
1776 * We might be called recursively from mono_class_from_typeref if
1777 * one of our fields has a type which is a nested type of this class,
1778 * and the compiler encodes it as a typeref, like older versions of
1781 if (class->size_inited) {
1782 mono_loader_unlock ();
1786 /* this indicates a cyclic dependency */
1787 g_error ("pending init %s.%s\n", class->name_space, class->name);
1790 class->init_pending = 1;
1792 /* CAS - SecurityAction.InheritanceDemand */
1793 if (mono_is_security_manager_active () && class->parent && (class->parent->flags & TYPE_ATTRIBUTE_HAS_SECURITY)) {
1794 mono_secman_inheritancedemand_class (class, class->parent);
1797 if (mono_debugger_start_class_init_func)
1798 mono_debugger_start_class_init_func (class);
1800 mono_stats.initialized_class_count++;
1802 if (class->generic_class && !class->generic_class->is_dynamic) {
1803 MonoInflatedGenericClass *gclass;
1806 gclass = mono_get_inflated_generic_class (class->generic_class);
1807 gklass = gclass->generic_class.container_class;
1809 mono_stats.generic_class_count++;
1811 class->method = gklass->method;
1812 class->field = gklass->field;
1814 mono_class_init (gklass);
1815 mono_class_setup_methods (gklass);
1816 mono_class_setup_properties (gklass);
1818 if (MONO_CLASS_IS_INTERFACE (class))
1819 class->interface_id = mono_get_unique_iid (class);
1821 g_assert (class->method.count == gklass->method.count);
1822 class->methods = g_new0 (MonoMethod *, class->method.count);
1824 for (i = 0; i < class->method.count; i++) {
1825 MonoMethod *inflated = mono_class_inflate_generic_method (
1826 gklass->methods [i], gclass->generic_class.context);
1828 class->methods [i] = mono_get_inflated_method (inflated);
1831 class->property = gklass->property;
1832 class->properties = g_new0 (MonoProperty, class->property.count);
1834 for (i = 0; i < class->property.count; i++) {
1835 MonoProperty *prop = &class->properties [i];
1837 *prop = gklass->properties [i];
1840 prop->get = mono_class_inflate_generic_method (
1841 prop->get, gclass->generic_class.context);
1843 prop->set = mono_class_inflate_generic_method (
1844 prop->set, gclass->generic_class.context);
1846 prop->parent = class;
1849 g_assert (class->interface_count == gklass->interface_count);
1852 if (class->parent && !class->parent->inited)
1853 mono_class_init (class->parent);
1855 if (!class->generic_class) {
1856 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
1859 guint32 cols [MONO_NESTED_CLASS_SIZE];
1860 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
1861 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
1862 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
1864 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
1869 * Computes the size used by the fields, and their locations
1871 if (!class->size_inited)
1872 class_compute_field_layout (class);
1874 /* initialize method pointers */
1877 MonoMethodSignature *sig;
1878 class->method.count = class->rank > 1? 2: 1;
1879 sig = mono_metadata_signature_alloc (class->image, class->rank);
1880 sig->ret = &mono_defaults.void_class->byval_arg;
1881 sig->pinvoke = TRUE;
1882 for (i = 0; i < class->rank; ++i)
1883 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1885 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1886 ctor->klass = class;
1887 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1888 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1889 ctor->signature = sig;
1890 ctor->name = ".ctor";
1892 class->methods = g_new (MonoMethod*, class->method.count);
1893 class->methods [0] = ctor;
1894 if (class->rank > 1) {
1895 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
1896 sig->ret = &mono_defaults.void_class->byval_arg;
1897 sig->pinvoke = TRUE;
1898 for (i = 0; i < class->rank * 2; ++i)
1899 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1901 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1902 ctor->klass = class;
1903 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1904 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1905 ctor->signature = sig;
1906 ctor->name = ".ctor";
1908 class->methods [1] = ctor;
1912 mono_class_setup_supertypes (class);
1915 if (class == mono_defaults.object_class) {
1916 mono_class_setup_vtable (class);
1917 for (i = 0; i < class->vtable_size; ++i) {
1918 MonoMethod *cm = class->vtable [i];
1920 if (!strcmp (cm->name, "GetHashCode")) {
1926 g_assert (ghc_slot > 0);
1928 default_ghc = class->vtable [ghc_slot];
1932 if (!default_finalize) {
1933 if (class == mono_defaults.object_class) {
1934 mono_class_setup_vtable (class);
1935 for (i = 0; i < class->vtable_size; ++i) {
1936 MonoMethod *cm = class->vtable [i];
1938 if (!strcmp (cm->name, "Finalize")) {
1944 g_assert (finalize_slot > 0);
1946 default_finalize = class->vtable [finalize_slot];
1951 * If possible, avoid the creation of the generic vtable by requesting
1952 * cached info from the runtime.
1954 if (mono_class_get_cached_class_info (class, &cached_info)) {
1955 guint32 cur_slot = 0;
1957 class->vtable_size = cached_info.vtable_size;
1958 class->has_finalize = cached_info.has_finalize;
1959 class->ghcimpl = cached_info.ghcimpl;
1960 class->has_cctor = cached_info.has_cctor;
1962 if (class->parent) {
1963 mono_class_init (class->parent);
1964 cur_slot = class->parent->vtable_size;
1967 setup_interface_offsets (class, cur_slot);
1970 mono_class_setup_vtable (class);
1973 if (class->parent) {
1974 if (class->vtable [ghc_slot] == default_ghc) {
1979 /* Object::Finalize should have empty implemenatation */
1980 class->has_finalize = 0;
1981 if (class->parent) {
1982 if (class->vtable [finalize_slot] != default_finalize)
1983 class->has_finalize = 1;
1986 for (i = 0; i < class->method.count; ++i) {
1987 MonoMethod *method = class->methods [i];
1988 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
1989 (strcmp (".cctor", method->name) == 0)) {
1990 class->has_cctor = 1;
1997 class->init_pending = 0;
1999 mono_loader_unlock ();
2001 if (MONO_CLASS_IS_INTERFACE (class)) {
2003 * class->interface_offsets is needed for the castclass/isinst code, so
2004 * we have to setup them for interfaces, too.
2006 setup_interface_offsets (class, 0);
2009 if (mono_debugger_class_init_func)
2010 mono_debugger_class_init_func (class);
2014 * LOCKING: this assumes the loader lock is held
2017 mono_class_setup_mono_type (MonoClass *class)
2019 const char *name = class->name;
2020 const char *nspace = class->name_space;
2022 if (MONO_CLASS_IS_INTERFACE (class))
2023 class->interface_id = mono_get_unique_iid (class);
2025 class->this_arg.byref = 1;
2026 class->this_arg.data.klass = class;
2027 class->this_arg.type = MONO_TYPE_CLASS;
2028 class->byval_arg.data.klass = class;
2029 class->byval_arg.type = MONO_TYPE_CLASS;
2031 if (!strcmp (nspace, "System")) {
2032 if (!strcmp (name, "ValueType")) {
2034 * do not set the valuetype bit for System.ValueType.
2035 * class->valuetype = 1;
2037 class->blittable = TRUE;
2038 } else if (!strcmp (name, "Enum")) {
2040 * do not set the valuetype bit for System.Enum.
2041 * class->valuetype = 1;
2043 class->valuetype = 0;
2044 class->enumtype = 0;
2045 } else if (!strcmp (name, "Object")) {
2046 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
2047 } else if (!strcmp (name, "String")) {
2048 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
2049 } else if (!strcmp (name, "TypedReference")) {
2050 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
2054 if (class->valuetype) {
2055 int t = MONO_TYPE_VALUETYPE;
2056 if (!strcmp (nspace, "System")) {
2059 if (!strcmp (name, "Boolean")) {
2060 t = MONO_TYPE_BOOLEAN;
2061 } else if (!strcmp(name, "Byte")) {
2063 class->blittable = TRUE;
2067 if (!strcmp (name, "Char")) {
2072 if (!strcmp (name, "Double")) {
2074 class->blittable = TRUE;
2078 if (!strcmp (name, "Int32")) {
2080 class->blittable = TRUE;
2081 } else if (!strcmp(name, "Int16")) {
2083 class->blittable = TRUE;
2084 } else if (!strcmp(name, "Int64")) {
2086 class->blittable = TRUE;
2087 } else if (!strcmp(name, "IntPtr")) {
2089 class->blittable = TRUE;
2093 if (!strcmp (name, "Single")) {
2095 class->blittable = TRUE;
2096 } else if (!strcmp(name, "SByte")) {
2098 class->blittable = TRUE;
2102 if (!strcmp (name, "UInt32")) {
2104 class->blittable = TRUE;
2105 } else if (!strcmp(name, "UInt16")) {
2107 class->blittable = TRUE;
2108 } else if (!strcmp(name, "UInt64")) {
2110 class->blittable = TRUE;
2111 } else if (!strcmp(name, "UIntPtr")) {
2113 class->blittable = TRUE;
2117 if (!strcmp (name, "TypedReference")) {
2118 t = MONO_TYPE_TYPEDBYREF;
2119 class->blittable = TRUE;
2123 if (!strcmp (name, "Void")) {
2131 class->this_arg.type = class->byval_arg.type = t;
2136 * LOCKING: this assumes the loader lock is held
2139 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
2141 gboolean system_namespace;
2143 system_namespace = !strcmp (class->name_space, "System");
2145 /* if root of the hierarchy */
2146 if (system_namespace && !strcmp (class->name, "Object")) {
2147 class->parent = NULL;
2148 class->instance_size = sizeof (MonoObject);
2151 if (!strcmp (class->name, "<Module>")) {
2152 class->parent = NULL;
2153 class->instance_size = 0;
2157 if (!MONO_CLASS_IS_INTERFACE (class)) {
2158 class->parent = parent;
2161 g_assert_not_reached (); /* FIXME */
2163 if (parent->generic_class && !parent->name) {
2165 * If the parent is a generic instance, we may get
2166 * called before it is fully initialized, especially
2167 * before it has its name.
2172 class->marshalbyref = parent->marshalbyref;
2173 class->contextbound = parent->contextbound;
2174 class->delegate = parent->delegate;
2176 if (system_namespace) {
2177 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
2178 class->marshalbyref = 1;
2180 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
2181 class->contextbound = 1;
2183 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
2184 class->delegate = 1;
2187 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
2188 (strcmp (class->parent->name_space, "System") == 0)))
2189 class->valuetype = 1;
2190 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
2191 class->valuetype = class->enumtype = 1;
2193 /*class->enumtype = class->parent->enumtype; */
2194 mono_class_setup_supertypes (class);
2196 class->parent = NULL;
2202 * LOCKING: this assumes the loader lock is held
2205 mono_class_setup_supertypes (MonoClass *class)
2209 if (class->supertypes)
2212 if (class->parent && !class->parent->supertypes)
2213 mono_class_setup_supertypes (class->parent);
2215 class->idepth = class->parent->idepth + 1;
2219 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
2220 class->supertypes = g_new0 (MonoClass *, ms);
2222 if (class->parent) {
2223 class->supertypes [class->idepth - 1] = class;
2224 memcpy (class->supertypes, class->parent->supertypes, class->parent->idepth * sizeof (gpointer));
2226 class->supertypes [0] = class;
2231 * If we inherit a type parameter from an outer class, set its owner to that class.
2234 set_generic_param_owner (MonoGenericContainer *container, MonoClass *klass, int pos)
2236 MonoGenericContainer *gc;
2239 if (klass->nested_in)
2240 pos = set_generic_param_owner (container, klass->nested_in, pos);
2242 if (!klass->generic_container)
2245 gc = klass->generic_container;
2246 for (i = pos; i < gc->type_argc; i++)
2247 container->type_params [i].owner = gc;
2249 return pos + gc->type_argc;
2252 static MonoGenericInst *
2253 get_shared_inst (MonoGenericContainer *container)
2255 MonoGenericInst *nginst;
2258 nginst = g_new0 (MonoGenericInst, 1);
2259 nginst->type_argc = container->type_argc;
2260 nginst->type_argv = g_new0 (MonoType *, nginst->type_argc);
2261 nginst->is_reference = 1;
2262 nginst->is_open = 1;
2264 for (i = 0; i < nginst->type_argc; i++) {
2265 MonoType *t = g_new0 (MonoType, 1);
2267 t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2268 t->data.generic_param = &container->type_params [i];
2270 nginst->type_argv [i] = t;
2273 return mono_metadata_lookup_generic_inst (nginst);
2277 mono_get_shared_generic_class (MonoGenericContainer *container, gboolean is_dynamic)
2279 MonoInflatedGenericClass *igclass;
2280 MonoGenericClass *gclass;
2283 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
2284 igclass = &dgclass->generic_class;
2285 gclass = &igclass->generic_class;
2286 gclass->is_inflated = 1;
2287 gclass->is_dynamic = 1;
2289 igclass = g_new0 (MonoInflatedGenericClass, 1);
2290 gclass = &igclass->generic_class;
2291 gclass->is_inflated = 1;
2294 gclass->context = &container->context;
2295 gclass->container_class = container->klass;
2296 gclass->inst = get_shared_inst (container);
2299 MonoGenericClass *cached = mono_metadata_lookup_generic_class (gclass);
2307 igclass->klass = container->klass;
2313 * @image: context where the image is created
2314 * @type_token: typedef token
2317 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
2319 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
2320 MonoClass *class, *parent = NULL;
2321 guint32 cols [MONO_TYPEDEF_SIZE];
2322 guint32 cols_next [MONO_TYPEDEF_SIZE];
2323 guint tidx = mono_metadata_token_index (type_token);
2324 MonoGenericContext *context = NULL;
2325 const char *name, *nspace;
2327 MonoClass **interfaces;
2329 mono_loader_lock ();
2331 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
2332 mono_loader_unlock ();
2336 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
2338 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
2340 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2341 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2343 class = g_malloc0 (sizeof (MonoClass));
2346 class->name_space = nspace;
2348 class->image = image;
2349 class->type_token = type_token;
2350 class->flags = cols [MONO_TYPEDEF_FLAGS];
2352 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
2354 class->generic_container = mono_metadata_load_generic_params (image, class->type_token, NULL);
2355 if (class->generic_container) {
2356 class->generic_container->klass = class;
2357 context = &class->generic_container->context;
2359 context->gclass = mono_get_shared_generic_class (context->container, FALSE);
2362 if (cols [MONO_TYPEDEF_EXTENDS])
2363 parent = mono_class_get_full (
2364 image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]), context);
2366 mono_class_setup_parent (class, parent);
2368 mono_class_setup_mono_type (class);
2370 interfaces = mono_metadata_interfaces_from_typedef_full (image, type_token, &icount, context);
2372 class->interfaces = interfaces;
2373 class->interface_count = icount;
2375 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
2377 /* fixme: maybe we must set this on windows
2378 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
2382 class->cast_class = class->element_class = class;
2384 /*g_print ("Load class %s\n", name);*/
2387 * Compute the field and method lists
2389 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
2390 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
2392 if (tt->rows > tidx){
2393 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
2394 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
2395 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
2397 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
2398 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
2401 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
2402 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
2403 class->field.count = class->field.last - class->field.first;
2405 class->field.count = 0;
2407 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
2408 class->method.count = class->method.last - class->method.first;
2410 class->method.count = 0;
2412 /* reserve space to store vector pointer in arrays */
2413 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
2414 class->instance_size += 2 * sizeof (gpointer);
2415 g_assert (class->field.count == 0);
2418 if (class->enumtype)
2419 class_compute_field_layout (class);
2421 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
2422 class->nested_in = mono_class_create_from_typedef (image, type_token);
2424 if (class->nested_in && class->generic_container)
2425 set_generic_param_owner (class->generic_container, class->nested_in, 0);
2427 mono_loader_unlock ();
2433 mono_class_create_generic (MonoInflatedGenericClass *gclass)
2435 MonoClass *klass, *gklass;
2438 if (gclass->is_initialized)
2440 gclass->is_initialized = TRUE;
2443 gclass->klass = g_malloc0 (sizeof (MonoClass));
2444 klass = gclass->klass;
2446 gklass = gclass->generic_class.container_class;
2448 klass->nested_in = gklass->nested_in;
2450 klass->name = gklass->name;
2451 klass->name_space = gklass->name_space;
2452 klass->image = gklass->image;
2453 klass->flags = gklass->flags;
2455 klass->generic_class = &gclass->generic_class;
2457 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
2458 klass->this_arg.data.generic_class = klass->byval_arg.data.generic_class = &gclass->generic_class;
2459 klass->this_arg.byref = TRUE;
2461 klass->cast_class = klass->element_class = klass;
2463 if (gclass->generic_class.is_dynamic) {
2464 klass->instance_size = gklass->instance_size;
2465 klass->class_size = gklass->class_size;
2466 klass->size_inited = 1;
2469 klass->valuetype = gklass->valuetype;
2471 mono_class_setup_supertypes (klass);
2474 klass->interface_count = gklass->interface_count;
2475 klass->interfaces = g_new0 (MonoClass *, klass->interface_count);
2476 for (i = 0; i < klass->interface_count; i++) {
2477 MonoType *it = &gklass->interfaces [i]->byval_arg;
2478 MonoType *inflated = mono_class_inflate_generic_type (
2479 it, gclass->generic_class.context);
2480 klass->interfaces [i] = mono_class_from_mono_type (inflated);
2483 i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, 1);
2486 guint32 cols [MONO_NESTED_CLASS_SIZE];
2487 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
2488 nclass = mono_class_create_from_typedef (klass->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
2489 klass->nested_classes = g_list_prepend (klass->nested_classes, nclass);
2491 i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, i + 1);
2495 klass->parent = mono_class_from_mono_type (gclass->parent);
2496 else if (gklass->parent) {
2497 MonoType *inflated = mono_class_inflate_generic_type (
2498 &gklass->parent->byval_arg, gclass->generic_class.context);
2500 klass->parent = mono_class_from_mono_type (inflated);
2504 mono_class_setup_parent (klass, klass->parent);
2508 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
2510 MonoClass *klass, **ptr;
2514 return param->pklass;
2516 klass = param->pklass = g_new0 (MonoClass, 1);
2518 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
2522 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
2523 klass->parent = param->constraints [0];
2525 } else if (param->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT)
2526 klass->parent = mono_class_from_name (mono_defaults.corlib, "System", "ValueType");
2528 klass->parent = mono_defaults.object_class;
2530 if (count - pos > 0) {
2531 klass->interface_count = count - pos;
2532 klass->interfaces = g_new0 (MonoClass *, count - pos);
2533 for (i = pos; i < count; i++)
2534 klass->interfaces [i - pos] = param->constraints [i];
2537 g_assert (param->name && param->owner);
2539 klass->name = param->name;
2540 klass->name_space = "";
2541 klass->image = image;
2542 klass->inited = TRUE;
2543 klass->cast_class = klass->element_class = klass;
2544 klass->enum_basetype = &klass->element_class->byval_arg;
2545 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2547 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2548 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2549 klass->this_arg.byref = TRUE;
2551 mono_class_setup_supertypes (klass);
2557 my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
2562 return param->pklass;
2564 g_assert (param->owner);
2566 klass = g_new0 (MonoClass, 1);
2569 klass->name = param->name;
2571 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
2572 klass->name_space = "";
2573 klass->image = mono_defaults.corlib;
2574 klass->inited = TRUE;
2575 klass->cast_class = klass->element_class = klass;
2576 klass->enum_basetype = &klass->element_class->byval_arg;
2577 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2579 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2580 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2581 klass->this_arg.byref = TRUE;
2583 mono_class_setup_supertypes (klass);
2589 mono_ptr_class_get (MonoType *type)
2592 MonoClass *el_class;
2593 static GHashTable *ptr_hash = NULL;
2595 mono_loader_lock ();
2598 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2599 el_class = mono_class_from_mono_type (type);
2600 if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
2601 mono_loader_unlock ();
2604 result = g_new0 (MonoClass, 1);
2606 result->parent = NULL; /* no parent for PTR types */
2607 result->name_space = el_class->name_space;
2608 result->name = g_strdup_printf ("%s*", el_class->name);
2609 result->image = el_class->image;
2610 result->inited = TRUE;
2611 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2612 /* Can pointers get boxed? */
2613 result->instance_size = sizeof (gpointer);
2614 result->cast_class = result->element_class = el_class;
2615 result->enum_basetype = &result->element_class->byval_arg;
2616 result->blittable = TRUE;
2618 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
2619 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
2620 result->this_arg.byref = TRUE;
2622 mono_class_setup_supertypes (result);
2624 g_hash_table_insert (ptr_hash, el_class, result);
2626 mono_loader_unlock ();
2632 mono_fnptr_class_get (MonoMethodSignature *sig)
2635 static GHashTable *ptr_hash = NULL;
2637 mono_loader_lock ();
2640 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2642 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
2643 mono_loader_unlock ();
2646 result = g_new0 (MonoClass, 1);
2648 result->parent = NULL; /* no parent for PTR types */
2649 result->name = "System";
2650 result->name_space = "MonoFNPtrFakeClass";
2651 result->image = NULL; /* need to fix... */
2652 result->inited = TRUE;
2653 result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
2654 /* Can pointers get boxed? */
2655 result->instance_size = sizeof (gpointer);
2656 result->cast_class = result->element_class = result;
2657 result->blittable = TRUE;
2659 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
2660 result->this_arg.data.method = result->byval_arg.data.method = sig;
2661 result->this_arg.byref = TRUE;
2662 result->enum_basetype = &result->element_class->byval_arg;
2663 result->blittable = TRUE;
2665 mono_class_setup_supertypes (result);
2667 g_hash_table_insert (ptr_hash, sig, result);
2669 mono_loader_unlock ();
2675 mono_class_from_mono_type (MonoType *type)
2677 switch (type->type) {
2678 case MONO_TYPE_OBJECT:
2679 return type->data.klass? type->data.klass: mono_defaults.object_class;
2680 case MONO_TYPE_VOID:
2681 return type->data.klass? type->data.klass: mono_defaults.void_class;
2682 case MONO_TYPE_BOOLEAN:
2683 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
2684 case MONO_TYPE_CHAR:
2685 return type->data.klass? type->data.klass: mono_defaults.char_class;
2687 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
2689 return type->data.klass? type->data.klass: mono_defaults.byte_class;
2691 return type->data.klass? type->data.klass: mono_defaults.int16_class;
2693 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
2695 return type->data.klass? type->data.klass: mono_defaults.int32_class;
2697 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
2699 return type->data.klass? type->data.klass: mono_defaults.int_class;
2701 return type->data.klass? type->data.klass: mono_defaults.uint_class;
2703 return type->data.klass? type->data.klass: mono_defaults.int64_class;
2705 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
2707 return type->data.klass? type->data.klass: mono_defaults.single_class;
2709 return type->data.klass? type->data.klass: mono_defaults.double_class;
2710 case MONO_TYPE_STRING:
2711 return type->data.klass? type->data.klass: mono_defaults.string_class;
2712 case MONO_TYPE_TYPEDBYREF:
2713 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
2714 case MONO_TYPE_ARRAY:
2715 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
2717 return mono_ptr_class_get (type->data.type);
2718 case MONO_TYPE_FNPTR:
2719 return mono_fnptr_class_get (type->data.method);
2720 case MONO_TYPE_SZARRAY:
2721 return mono_array_class_get (type->data.klass, 1);
2722 case MONO_TYPE_CLASS:
2723 case MONO_TYPE_VALUETYPE:
2724 return type->data.klass;
2725 case MONO_TYPE_GENERICINST: {
2726 MonoInflatedGenericClass *gclass;
2727 gclass = mono_get_inflated_generic_class (type->data.generic_class);
2728 g_assert (gclass->klass);
2729 return gclass->klass;
2732 return my_mono_class_from_generic_parameter (type->data.generic_param, FALSE);
2733 case MONO_TYPE_MVAR:
2734 return my_mono_class_from_generic_parameter (type->data.generic_param, TRUE);
2736 g_warning ("implement me 0x%02x\n", type->type);
2737 g_assert_not_reached ();
2744 * @image: context where the image is created
2745 * @type_spec: typespec token
2748 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec,
2749 MonoGenericContext *context)
2751 MonoType *type, *inflated;
2754 type = mono_type_create_from_typespec_full (image, context, type_spec);
2756 switch (type->type) {
2757 case MONO_TYPE_ARRAY:
2758 class = mono_array_class_get (type->data.array->eklass, type->data.array->rank);
2760 case MONO_TYPE_SZARRAY:
2761 class = mono_array_class_get (type->data.klass, 1);
2764 class = mono_ptr_class_get (type->data.type);
2766 case MONO_TYPE_GENERICINST: {
2767 MonoInflatedGenericClass *gclass;
2768 gclass = mono_get_inflated_generic_class (type->data.generic_class);
2769 g_assert (gclass->klass);
2770 class = gclass->klass;
2774 /* it seems any type can be stored in TypeSpec as well */
2775 class = mono_class_from_mono_type (type);
2779 if (!class || !context || (!context->gclass && !context->gmethod))
2782 inflated = mono_class_inflate_generic_type (&class->byval_arg, context);
2784 return mono_class_from_mono_type (inflated);
2788 * mono_bounded_array_class_get:
2789 * @element_class: element class
2790 * @rank: the dimension of the array class
2791 * @bounded: whenever the array has non-zero bounds
2793 * Returns: a class object describing the array with element type @element_type and
2797 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
2801 MonoClass *parent = NULL;
2802 GSList *list, *rootlist;
2805 gboolean corlib_type = FALSE;
2807 g_assert (rank <= 255);
2810 /* bounded only matters for one-dimensional arrays */
2813 image = eclass->image;
2815 mono_loader_lock ();
2817 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
2818 for (; list; list = list->next) {
2820 if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
2821 mono_loader_unlock ();
2827 /* for the building corlib use System.Array from it */
2828 if (image->assembly && image->assembly->dynamic && strcmp (image->assembly_name, "mscorlib") == 0) {
2829 parent = mono_class_from_name (image, "System", "Array");
2831 } else if (mono_defaults.generic_array_class) {
2832 MonoType *inflated, **args;
2834 args = g_new0 (MonoType *, 1);
2835 args [0] = &eclass->byval_arg;
2837 inflated = mono_class_bind_generic_parameters (
2838 &mono_defaults.generic_array_class->byval_arg, 1, args);
2839 parent = mono_class_from_mono_type (inflated);
2841 if (!parent->inited)
2842 mono_class_init (parent);
2844 parent = mono_defaults.array_class;
2845 if (!parent->inited)
2846 mono_class_init (parent);
2849 class = g_malloc0 (sizeof (MonoClass));
2851 class->image = image;
2852 class->name_space = eclass->name_space;
2853 nsize = strlen (eclass->name);
2854 name = g_malloc (nsize + 2 + rank);
2855 memcpy (name, eclass->name, nsize);
2858 memset (name + nsize + 1, ',', rank - 1);
2859 name [nsize + rank] = ']';
2860 name [nsize + rank + 1] = 0;
2862 class->type_token = 0;
2863 /* all arrays are marked serializable and sealed, bug #42779 */
2864 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
2865 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2866 class->parent = parent;
2867 class->instance_size = mono_class_instance_size (class->parent);
2868 class->class_size = 0;
2869 mono_class_setup_supertypes (class);
2870 if (eclass->generic_class)
2871 mono_class_init (eclass);
2872 if (!eclass->size_inited)
2873 class_compute_field_layout (eclass);
2874 class->has_references = MONO_TYPE_IS_REFERENCE (&eclass->byval_arg) || eclass->has_references? TRUE: FALSE;
2878 if (eclass->enumtype)
2879 class->cast_class = eclass->element_class;
2881 class->cast_class = eclass;
2883 class->element_class = eclass;
2885 if ((rank > 1) || bounded) {
2886 MonoArrayType *at = g_new0 (MonoArrayType, 1);
2887 class->byval_arg.type = MONO_TYPE_ARRAY;
2888 class->byval_arg.data.array = at;
2889 at->eklass = eclass;
2891 /* FIXME: complete.... */
2893 class->byval_arg.type = MONO_TYPE_SZARRAY;
2894 class->byval_arg.data.klass = eclass;
2896 class->this_arg = class->byval_arg;
2897 class->this_arg.byref = 1;
2902 list = g_slist_append (rootlist, class);
2903 g_hash_table_insert (image->array_cache, eclass, list);
2905 mono_loader_unlock ();
2911 * mono_array_class_get:
2912 * @element_class: element class
2913 * @rank: the dimension of the array class
2915 * Returns: a class object describing the array with element type @element_type and
2919 mono_array_class_get (MonoClass *eclass, guint32 rank)
2921 return mono_bounded_array_class_get (eclass, rank, FALSE);
2925 * mono_class_instance_size:
2928 * Returns: the size of an object instance
2931 mono_class_instance_size (MonoClass *klass)
2933 if (!klass->size_inited)
2934 mono_class_init (klass);
2936 return klass->instance_size;
2940 * mono_class_min_align:
2943 * Returns: minimm alignment requirements
2946 mono_class_min_align (MonoClass *klass)
2948 if (!klass->size_inited)
2949 mono_class_init (klass);
2951 return klass->min_align;
2955 * mono_class_value_size:
2958 * This function is used for value types, and return the
2959 * space and the alignment to store that kind of value object.
2961 * Returns: the size of a value of kind @klass
2964 mono_class_value_size (MonoClass *klass, guint32 *align)
2968 /* fixme: check disable, because we still have external revereces to
2969 * mscorlib and Dummy Objects
2971 /*g_assert (klass->valuetype);*/
2973 size = mono_class_instance_size (klass) - sizeof (MonoObject);
2976 *align = klass->min_align;
2982 * mono_class_data_size:
2985 * Returns: the size of the static class data
2988 mono_class_data_size (MonoClass *klass)
2991 mono_class_init (klass);
2993 return klass->class_size;
2997 * Auxiliary routine to mono_class_get_field
2999 * Takes a field index instead of a field token.
3001 static MonoClassField *
3002 mono_class_get_field_idx (MonoClass *class, int idx)
3004 if (class->field.count){
3005 if ((idx >= class->field.first) && (idx < class->field.last)){
3006 return &class->fields [idx - class->field.first];
3013 return mono_class_get_field_idx (class->parent, idx);
3017 * mono_class_get_field:
3018 * @class: the class to lookup the field.
3019 * @field_token: the field token
3021 * Returns: A MonoClassField representing the type and offset of
3022 * the field, or a NULL value if the field does not belong to this
3026 mono_class_get_field (MonoClass *class, guint32 field_token)
3028 int idx = mono_metadata_token_index (field_token);
3030 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
3032 return mono_class_get_field_idx (class, idx - 1);
3036 mono_class_get_field_from_name (MonoClass *klass, const char *name)
3041 for (i = 0; i < klass->field.count; ++i) {
3042 if (strcmp (name, klass->fields [i].name) == 0)
3043 return &klass->fields [i];
3045 klass = klass->parent;
3051 mono_class_get_field_token (MonoClassField *field)
3053 MonoClass *klass = field->parent;
3057 for (i = 0; i < klass->field.count; ++i) {
3058 if (&klass->fields [i] == field)
3059 return mono_metadata_make_token (MONO_TABLE_FIELD, klass->field.first + i + 1);
3061 klass = klass->parent;
3064 g_assert_not_reached ();
3069 mono_class_get_event_token (MonoEvent *event)
3071 MonoClass *klass = event->parent;
3075 for (i = 0; i < klass->event.count; ++i) {
3076 if (&klass->events [i] == event)
3077 return mono_metadata_make_token (MONO_TABLE_EVENT, klass->event.first + i + 1);
3079 klass = klass->parent;
3082 g_assert_not_reached ();
3087 mono_vtable_get_static_field_data (MonoVTable *vt)
3093 mono_class_get_property_from_name (MonoClass *klass, const char *name)
3097 gpointer iter = NULL;
3098 while ((p = mono_class_get_properties (klass, &iter))) {
3099 if (! strcmp (name, p->name))
3102 klass = klass->parent;
3108 mono_class_get_property_token (MonoProperty *prop)
3110 MonoClass *klass = prop->parent;
3114 gpointer iter = NULL;
3115 while ((p = mono_class_get_properties (klass, &iter))) {
3116 if (&klass->properties [i] == prop)
3117 return mono_metadata_make_token (MONO_TABLE_PROPERTY, klass->property.first + i + 1);
3121 klass = klass->parent;
3124 g_assert_not_reached ();
3129 mono_class_name_from_token (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3131 const char *name, *nspace;
3133 return g_strdup_printf ("DynamicType 0x%08x", type_token);
3135 switch (type_token & 0xff000000){
3136 case MONO_TOKEN_TYPE_DEF: {
3137 guint32 cols [MONO_TYPEDEF_SIZE];
3138 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
3139 guint tidx = mono_metadata_token_index (type_token);
3141 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
3142 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3143 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3144 if (strlen (nspace) == 0)
3145 return g_strdup_printf ("%s", name);
3147 return g_strdup_printf ("%s.%s", nspace, name);
3150 case MONO_TOKEN_TYPE_REF: {
3151 guint32 cols [MONO_TYPEREF_SIZE];
3152 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
3154 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
3155 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
3156 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
3157 if (strlen (nspace) == 0)
3158 return g_strdup_printf ("%s", name);
3160 return g_strdup_printf ("%s.%s", nspace, name);
3163 case MONO_TOKEN_TYPE_SPEC:
3164 return g_strdup_printf ("Typespec 0x%08x", type_token);
3166 g_assert_not_reached ();
3174 * @image: the image where the class resides
3175 * @type_token: the token for the class
3176 * @at: an optional pointer to return the array element type
3178 * Returns: the MonoClass that represents @type_token in @image
3181 _mono_class_get (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3183 MonoClass *class = NULL;
3186 return mono_lookup_dynamic_token (image, type_token);
3188 switch (type_token & 0xff000000){
3189 case MONO_TOKEN_TYPE_DEF:
3190 class = mono_class_create_from_typedef (image, type_token);
3192 case MONO_TOKEN_TYPE_REF:
3193 class = mono_class_from_typeref (image, type_token);
3195 case MONO_TOKEN_TYPE_SPEC:
3196 class = mono_class_create_from_typespec (image, type_token, context);
3199 g_warning ("unknown token type %x", type_token & 0xff000000);
3200 g_assert_not_reached ();
3204 char *name = mono_class_name_from_token (image, type_token, context);
3205 g_warning ("The class %s could not be loaded, used in %s (token 0x%08x)", name, image->name, type_token);
3213 mono_class_get (MonoImage *image, guint32 type_token)
3215 return _mono_class_get (image, type_token, NULL);
3219 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3221 MonoClass *class = _mono_class_get (image, type_token, context);
3224 if (!class || !context || (!context->gclass && !context->gmethod))
3227 switch (class->byval_arg.type) {
3228 case MONO_TYPE_GENERICINST:
3229 if (!class->generic_class->inst->is_open)
3233 case MONO_TYPE_MVAR:
3239 inflated = inflate_generic_type (&class->byval_arg, context);
3243 return mono_class_from_mono_type (inflated);
3247 * mono_class_from_name_case:
3248 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3249 * @name_space: the type namespace
3250 * @name: the type short name.
3252 * Obtains a MonoClass with a given namespace and a given name which
3253 * is located in the given MonoImage. The namespace and name
3254 * lookups are case insensitive.
3256 * You can also pass @NULL to the image, and that will lookup for
3257 * a type with the given namespace and name in all of the loaded
3258 * assemblies: notice that since there might be a name clash in this
3259 * case, passing @NULL is not encouraged if you need a precise type.
3263 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
3265 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
3266 guint32 cols [MONO_TYPEDEF_SIZE];
3271 /* add a cache if needed */
3272 for (i = 1; i <= t->rows; ++i) {
3273 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
3274 /* nested types are accessed from the nesting name */
3275 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
3276 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
3278 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3279 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3280 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
3281 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
3287 return_nested_in (MonoClass *class, char *nested) {
3289 char *s = strchr (nested, '/');
3296 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
3298 if (strcmp (found->name, nested) == 0) {
3300 return return_nested_in (found, s);
3309 * mono_class_from_name_case:
3310 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3311 * @name_space: the type namespace
3312 * @name: the type short name.
3314 * Obtains a MonoClass with a given namespace and a given name which
3315 * is located in the given MonoImage.
3317 * You can also pass `NULL' to the image, and that will lookup for
3318 * a type with the given namespace and name in all of the loaded
3319 * assemblies: notice that since there might be a name clash in this
3320 * case, passing NULL is not encouraged if you need a precise type.
3324 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
3326 GHashTable *nspace_table;
3327 MonoImage *loaded_image;
3333 if ((nested = strchr (name, '/'))) {
3334 int pos = nested - name;
3335 int len = strlen (name);
3338 memcpy (buf, name, len + 1);
3340 nested = buf + pos + 1;
3344 mono_loader_lock ();
3346 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
3349 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
3351 mono_loader_unlock ();
3356 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
3357 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
3358 guint32 cols [MONO_EXP_TYPE_SIZE];
3361 idx = mono_metadata_token_index (token);
3363 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
3365 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
3366 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
3367 loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
3370 class = mono_class_from_name (loaded_image, name_space, name);
3372 return return_nested_in (class, nested);
3375 g_error ("not yet implemented");
3379 token = MONO_TOKEN_TYPE_DEF | token;
3381 class = mono_class_get (image, token);
3383 return return_nested_in (class, nested);
3388 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
3389 gboolean check_interfaces)
3392 g_assert (klassc->idepth > 0);
3393 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
3394 if ((klassc->interface_id <= klass->max_interface_id) &&
3395 (klass->interface_offsets [klassc->interface_id] >= 0))
3397 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
3400 for (i = 0; i < klass->interface_count; i ++) {
3401 MonoClass *ic = klass->interfaces [i];
3406 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
3411 * MS.NET thinks interfaces are a subclass of Object, so we think it as
3414 if (klassc == mono_defaults.object_class)
3417 if (klass->generic_class) {
3418 MonoInflatedGenericClass *igclass;
3420 igclass = mono_get_inflated_generic_class (klass->generic_class);
3421 if (!igclass->parent)
3424 if (mono_metadata_type_equal (igclass->parent, &klassc->byval_arg))
3426 klass = mono_class_from_mono_type (igclass->parent);
3434 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
3437 mono_class_init (klass);
3439 if (!oklass->inited)
3440 mono_class_init (oklass);
3442 if (klass->generic_class)
3443 klass = klass->generic_class->container_class;
3444 if (oklass->generic_class)
3445 oklass = oklass->generic_class->container_class;
3447 if (MONO_CLASS_IS_INTERFACE (klass)) {
3448 if ((oklass->byval_arg.type == MONO_TYPE_VAR) || (oklass->byval_arg.type == MONO_TYPE_MVAR))
3451 /* interface_offsets might not be set for dynamic classes */
3452 if (oklass->reflection_info && !oklass->interface_offsets)
3454 * oklass might be a generic type parameter but they have
3455 * interface_offsets set.
3457 return mono_reflection_call_is_assignable_to (oklass, klass);
3459 if ((klass->interface_id <= oklass->max_interface_id) &&
3460 (oklass->interface_offsets [klass->interface_id] != -1))
3464 MonoClass *eclass, *eoclass;
3466 if (oklass->rank != klass->rank)
3469 /* vectors vs. one dimensional arrays */
3470 if (oklass->byval_arg.type != klass->byval_arg.type)
3473 eclass = klass->cast_class;
3474 eoclass = oklass->cast_class;
3478 * a is b does not imply a[] is b[] when a is a valuetype, and
3479 * b is a reference type.
3482 if (eoclass->valuetype) {
3483 if ((eclass == mono_defaults.enum_class) ||
3484 (eclass == mono_defaults.enum_class->parent) ||
3485 (eclass == mono_defaults.object_class))
3489 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
3492 if (klass == mono_defaults.object_class)
3496 * Custom version of mono_class_has_parent (oklass, klass)
3498 if (oklass->idepth >= klass->idepth) {
3499 MonoClass *parent = oklass->supertypes [klass->idepth - 1];
3501 if (parent->generic_class)
3502 parent = parent->generic_class->container_class;
3504 return klass == parent;
3511 * mono_class_get_cctor:
3513 * Returns the static constructor of @klass if it exists, NULL otherwise.
3516 mono_class_get_cctor (MonoClass *klass)
3518 MonoCachedClassInfo cached_info;
3520 if (!klass->has_cctor)
3523 if (mono_class_get_cached_class_info (klass, &cached_info))
3524 return mono_get_method (klass->image, cached_info.cctor_token, klass);
3526 return mono_class_get_method_from_name_flags (klass, ".cctor", -1, METHOD_ATTRIBUTE_SPECIAL_NAME);
3530 * mono_class_get_finalizer:
3532 * Returns the finalizer method of @klass if it exists, NULL otherwise.
3535 mono_class_get_finalizer (MonoClass *klass)
3537 MonoCachedClassInfo cached_info;
3538 static int finalize_slot = -1;
3540 if (finalize_slot < 0) {
3542 MonoClass* obj_class = mono_get_object_class ();
3543 mono_class_setup_vtable (obj_class);
3544 for (i = 0; i < obj_class->vtable_size; ++i) {
3545 MonoMethod *cm = obj_class->vtable [i];
3547 if (!strcmp (mono_method_get_name (cm), "Finalize")) {
3554 if (!klass->has_finalize)
3557 if (mono_class_get_cached_class_info (klass, &cached_info))
3558 return mono_get_method (cached_info.finalize_image, cached_info.finalize_token, NULL);
3560 mono_class_setup_vtable (klass);
3561 return klass->vtable [finalize_slot];
3566 * mono_class_needs_cctor_run:
3568 * Determines whenever the class has a static constructor and whenever it
3569 * needs to be called when executing CALLER.
3572 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
3576 method = mono_class_get_cctor (klass);
3578 return (method == caller) ? FALSE : TRUE;
3584 * mono_class_array_element_size:
3587 * Returns: the number of bytes an element of type @klass
3588 * uses when stored into an array.
3591 mono_class_array_element_size (MonoClass *klass)
3593 MonoType *type = &klass->byval_arg;
3596 switch (type->type) {
3599 case MONO_TYPE_BOOLEAN:
3603 case MONO_TYPE_CHAR:
3612 case MONO_TYPE_CLASS:
3613 case MONO_TYPE_STRING:
3614 case MONO_TYPE_OBJECT:
3615 case MONO_TYPE_SZARRAY:
3616 case MONO_TYPE_ARRAY:
3618 case MONO_TYPE_MVAR:
3619 return sizeof (gpointer);
3624 case MONO_TYPE_VALUETYPE:
3625 if (type->data.klass->enumtype) {
3626 type = type->data.klass->enum_basetype;
3627 klass = klass->element_class;
3630 return mono_class_instance_size (klass) - sizeof (MonoObject);
3631 case MONO_TYPE_GENERICINST:
3632 type = &type->data.generic_class->container_class->byval_arg;
3635 g_error ("unknown type 0x%02x in mono_class_array_element_size", type->type);
3641 * mono_array_element_size:
3642 * @ac: pointer to a #MonoArrayClass
3644 * Returns: the size of single array element.
3647 mono_array_element_size (MonoClass *ac)
3649 return mono_class_array_element_size (ac->element_class);
3653 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
3654 MonoGenericContext *context)
3656 if (image->dynamic) {
3657 MonoClass *tmp_handle_class;
3658 gpointer obj = mono_lookup_dynamic_token_class (image, token, &tmp_handle_class);
3660 g_assert (tmp_handle_class);
3662 *handle_class = tmp_handle_class;
3664 if (tmp_handle_class == mono_defaults.typehandle_class)
3665 return &((MonoClass*)obj)->byval_arg;
3670 switch (token & 0xff000000) {
3671 case MONO_TOKEN_TYPE_DEF:
3672 case MONO_TOKEN_TYPE_REF: {
3675 *handle_class = mono_defaults.typehandle_class;
3676 class = mono_class_get_full (image, token, context);
3677 mono_class_init (class);
3678 /* We return a MonoType* as handle */
3679 return &class->byval_arg;
3681 case MONO_TOKEN_TYPE_SPEC: {
3684 *handle_class = mono_defaults.typehandle_class;
3685 class = mono_class_create_from_typespec (image, token, context);
3686 mono_class_init (class);
3687 return &class->byval_arg;
3689 case MONO_TOKEN_FIELD_DEF: {
3691 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
3692 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
3693 mono_class_init (class);
3695 *handle_class = mono_defaults.fieldhandle_class;
3696 return mono_class_get_field (class, token);
3698 case MONO_TOKEN_METHOD_DEF: {
3700 meth = mono_get_method_full (image, token, NULL, context);
3702 *handle_class = mono_defaults.methodhandle_class;
3705 case MONO_TOKEN_MEMBER_REF: {
3706 guint32 cols [MONO_MEMBERREF_SIZE];
3708 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
3709 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
3710 mono_metadata_decode_blob_size (sig, &sig);
3711 if (*sig == 0x6) { /* it's a field */
3713 MonoClassField *field;
3714 field = mono_field_from_token (image, token, &klass, context);
3716 *handle_class = mono_defaults.fieldhandle_class;
3720 meth = mono_get_method_full (image, token, NULL, context);
3722 *handle_class = mono_defaults.methodhandle_class;
3727 g_warning ("Unknown token 0x%08x in ldtoken", token);
3734 * This function might need to call runtime functions so it can't be part
3735 * of the metadata library.
3737 static MonoLookupDynamicToken lookup_dynamic = NULL;
3740 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
3742 lookup_dynamic = func;
3746 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
3748 MonoClass *handle_class;
3750 return lookup_dynamic (image, token, &handle_class);
3754 mono_lookup_dynamic_token_class (MonoImage *image, guint32 token, MonoClass **handle_class)
3756 return lookup_dynamic (image, token, handle_class);
3759 static MonoGetCachedClassInfo get_cached_class_info = NULL;
3762 mono_install_get_cached_class_info (MonoGetCachedClassInfo func)
3764 get_cached_class_info = func;
3768 mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
3770 if (!get_cached_class_info)
3773 return get_cached_class_info (klass, res);
3777 mono_class_get_image (MonoClass *klass)
3779 return klass->image;
3783 * mono_class_get_element_class:
3784 * @klass: the MonoClass to act on
3786 * Returns: the element class of an array or an enumeration.
3789 mono_class_get_element_class (MonoClass *klass)
3791 return klass->element_class;
3795 * mono_class_is_valuetype:
3796 * @klass: the MonoClass to act on
3798 * Returns: true if the MonoClass represents a ValueType.
3801 mono_class_is_valuetype (MonoClass *klass)
3803 return klass->valuetype;
3807 * mono_class_is_enum:
3808 * @klass: the MonoClass to act on
3810 * Returns: true if the MonoClass represents an enumeration.
3813 mono_class_is_enum (MonoClass *klass)
3815 return klass->enumtype;
3819 * mono_class_enum_basetype:
3820 * @klass: the MonoClass to act on
3822 * Returns: the underlying type representation for an enumeration.
3825 mono_class_enum_basetype (MonoClass *klass)
3827 return klass->enum_basetype;
3831 * mono_class_get_parent
3832 * @klass: the MonoClass to act on
3834 * Returns: the parent class for this class.
3837 mono_class_get_parent (MonoClass *klass)
3839 return klass->parent;
3843 * mono_class_get_nesting_type;
3844 * @klass: the MonoClass to act on
3846 * Returns: the container type where this type is nested or NULL if this type is not a nested type.
3849 mono_class_get_nesting_type (MonoClass *klass)
3851 return klass->nested_in;
3855 * mono_class_get_rank:
3856 * @klass: the MonoClass to act on
3858 * Returns: the rank for the array (the number of dimensions).
3861 mono_class_get_rank (MonoClass *klass)
3867 * mono_class_get_flags:
3868 * @klass: the MonoClass to act on
3870 * The type flags from the TypeDef table from the metadata.
3871 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
3874 * Returns: the flags from the TypeDef table.
3877 mono_class_get_flags (MonoClass *klass)
3879 return klass->flags;
3883 * mono_class_get_name
3884 * @klass: the MonoClass to act on
3886 * Returns: the name of the class.
3889 mono_class_get_name (MonoClass *klass)
3895 * mono_class_get_namespace:
3896 * @klass: the MonoClass to act on
3898 * Returns: the namespace of the class.
3901 mono_class_get_namespace (MonoClass *klass)
3903 return klass->name_space;
3907 * mono_class_get_type:
3908 * @klass: the MonoClass to act on
3910 * This method returns the internal Type representation for the class.
3912 * Returns: the MonoType from the class.
3915 mono_class_get_type (MonoClass *klass)
3917 return &klass->byval_arg;
3921 * mono_class_get_byref_type:
3922 * @klass: the MonoClass to act on
3927 mono_class_get_byref_type (MonoClass *klass)
3929 return &klass->this_arg;
3933 * mono_class_num_fields:
3934 * @klass: the MonoClass to act on
3936 * Returns: the number of static and instance fields in the class.
3939 mono_class_num_fields (MonoClass *klass)
3941 return klass->field.count;
3945 * mono_class_num_methods:
3946 * @klass: the MonoClass to act on
3948 * Returns: the number of methods in the class.
3951 mono_class_num_methods (MonoClass *klass)
3953 return klass->method.count;
3957 * mono_class_num_properties
3958 * @klass: the MonoClass to act on
3960 * Returns: the number of properties in the class.
3963 mono_class_num_properties (MonoClass *klass)
3965 mono_class_setup_properties (klass);
3967 return klass->property.count;
3971 * mono_class_num_events:
3972 * @klass: the MonoClass to act on
3974 * Returns: the number of events in the class.
3977 mono_class_num_events (MonoClass *klass)
3979 mono_class_setup_events (klass);
3981 return klass->event.count;
3985 * mono_class_get_fields:
3986 * @klass: the MonoClass to act on
3988 * This routine is an iterator routine for retrieving the fields in a class.
3990 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3991 * iterate over all of the elements. When no more values are
3992 * available, the return value is NULL.
3994 * Returns: a @MonoClassField* on each iteration, or NULL when no more fields are available.
3997 mono_class_get_fields (MonoClass* klass, gpointer *iter)
3999 MonoClassField* field;
4003 mono_class_init (klass);
4005 /* start from the first */
4006 if (klass->field.count) {
4007 return *iter = &klass->fields [0];
4015 if (field < &klass->fields [klass->field.count]) {
4016 return *iter = field;
4022 * mono_class_get_methods
4023 * @klass: the MonoClass to act on
4025 * This routine is an iterator routine for retrieving the fields in a class.
4027 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4028 * iterate over all of the elements. When no more values are
4029 * available, the return value is NULL.
4031 * Returns: a MonoMethod on each iteration or NULL when no more methods are available.
4034 mono_class_get_methods (MonoClass* klass, gpointer *iter)
4036 MonoMethod** method;
4040 mono_class_init (klass);
4042 mono_class_setup_methods (klass);
4043 /* start from the first */
4044 if (klass->method.count) {
4045 *iter = &klass->methods [0];
4046 return klass->methods [0];
4054 if (method < &klass->methods [klass->method.count]) {
4062 * mono_class_get_properties:
4063 * @klass: the MonoClass to act on
4065 * This routine is an iterator routine for retrieving the properties in a class.
4067 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4068 * iterate over all of the elements. When no more values are
4069 * available, the return value is NULL.
4071 * Returns: a @MonoProperty* on each invocation, or NULL when no more are available.
4074 mono_class_get_properties (MonoClass* klass, gpointer *iter)
4076 MonoProperty* property;
4080 mono_class_init (klass);
4082 mono_class_setup_properties (klass);
4083 /* start from the first */
4084 if (klass->property.count) {
4085 return *iter = &klass->properties [0];
4093 if (property < &klass->properties [klass->property.count]) {
4094 return *iter = property;
4100 * mono_class_get_events:
4101 * @klass: the MonoClass to act on
4103 * This routine is an iterator routine for retrieving the properties in a class.
4105 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4106 * iterate over all of the elements. When no more values are
4107 * available, the return value is NULL.
4109 * Returns: a @MonoEvent* on each invocation, or NULL when no more are available.
4112 mono_class_get_events (MonoClass* klass, gpointer *iter)
4118 mono_class_init (klass);
4120 mono_class_setup_events (klass);
4121 /* start from the first */
4122 if (klass->event.count) {
4123 return *iter = &klass->events [0];
4131 if (event < &klass->events [klass->event.count]) {
4132 return *iter = event;
4138 * mono_class_get_interfaces
4139 * @klass: the MonoClass to act on
4141 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
4143 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4144 * iterate over all of the elements. When no more values are
4145 * available, the return value is NULL.
4147 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
4150 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
4156 mono_class_init (klass);
4158 /* start from the first */
4159 if (klass->interface_count) {
4160 *iter = &klass->interfaces [0];
4161 return klass->interfaces [0];
4169 if (iface < &klass->interfaces [klass->interface_count]) {
4177 * mono_class_get_nested_types
4178 * @klass: the MonoClass to act on
4180 * This routine is an iterator routine for retrieving the nested types of a class.
4182 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4183 * iterate over all of the elements. When no more values are
4184 * available, the return value is NULL.
4186 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
4189 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
4195 mono_class_init (klass);
4197 /* start from the first */
4198 if (klass->nested_classes) {
4199 *iter = klass->nested_classes;
4200 return klass->nested_classes->data;
4202 /* no nested types */
4216 * mono_field_get_name:
4217 * @field: the MonoClassField to act on
4219 * Returns: the name of the field.
4222 mono_field_get_name (MonoClassField *field)
4228 * mono_field_get_type:
4229 * @field: the MonoClassField to act on
4231 * Returns: MonoType of the field.
4234 mono_field_get_type (MonoClassField *field)
4240 * mono_field_get_type:
4241 * @field: the MonoClassField to act on
4243 * Returns: MonoClass where the field was defined.
4246 mono_field_get_parent (MonoClassField *field)
4248 return field->parent;
4252 * mono_field_get_flags;
4253 * @field: the MonoClassField to act on
4255 * The metadata flags for a field are encoded using the
4256 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4258 * Returns: the flags for the field.
4261 mono_field_get_flags (MonoClassField *field)
4263 return field->type->attrs;
4267 * mono_property_get_name:
4268 * @prop: the MonoProperty to act on
4270 * Returns: the name of the property
4273 mono_property_get_name (MonoProperty *prop)
4279 * mono_property_get_set_method
4280 * @prop: the MonoProperty to act on.
4282 * Returns: the setter method of the property (A MonoMethod)
4285 mono_property_get_set_method (MonoProperty *prop)
4291 * mono_property_get_get_method
4292 * @prop: the MonoProperty to act on.
4294 * Returns: the setter method of the property (A MonoMethod)
4297 mono_property_get_get_method (MonoProperty *prop)
4303 * mono_property_get_parent:
4304 * @prop: the MonoProperty to act on.
4306 * Returns: the MonoClass where the property was defined.
4309 mono_property_get_parent (MonoProperty *prop)
4311 return prop->parent;
4315 * mono_property_get_flags:
4316 * @prop: the MonoProperty to act on.
4318 * The metadata flags for a property are encoded using the
4319 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4321 * Returns: the flags for the property.
4324 mono_property_get_flags (MonoProperty *prop)
4330 * mono_event_get_name:
4331 * @event: the MonoEvent to act on
4333 * Returns: the name of the event.
4336 mono_event_get_name (MonoEvent *event)
4342 * mono_event_get_add_method:
4343 * @event: The MonoEvent to act on.
4345 * Returns: the @add' method for the event (a MonoMethod).
4348 mono_event_get_add_method (MonoEvent *event)
4354 * mono_event_get_remove_method:
4355 * @event: The MonoEvent to act on.
4357 * Returns: the @remove method for the event (a MonoMethod).
4360 mono_event_get_remove_method (MonoEvent *event)
4362 return event->remove;
4366 * mono_event_get_raise_method:
4367 * @event: The MonoEvent to act on.
4369 * Returns: the @raise method for the event (a MonoMethod).
4372 mono_event_get_raise_method (MonoEvent *event)
4374 return event->raise;
4378 * mono_event_get_parent:
4379 * @event: the MonoEvent to act on.
4381 * Returns: the MonoClass where the event is defined.
4384 mono_event_get_parent (MonoEvent *event)
4386 return event->parent;
4390 * mono_event_get_flags
4391 * @event: the MonoEvent to act on.
4393 * The metadata flags for an event are encoded using the
4394 * EVENT_* constants. See the tabledefs.h file for details.
4396 * Returns: the flags for the event.
4399 mono_event_get_flags (MonoEvent *event)
4401 return event->attrs;
4405 * mono_class_get_method_from_name:
4406 * @klass: where to look for the method
4407 * @name_space: name of the method
4408 * @param_count: number of parameters. -1 for any number.
4410 * Obtains a MonoMethod with a given name and number of parameters.
4411 * It only works if there are no multiple signatures for any given method name.
4414 mono_class_get_method_from_name (MonoClass *klass, const char *name, int param_count)
4416 return mono_class_get_method_from_name_flags (klass, name, param_count, 0);
4420 * mono_class_get_method_from_name_flags:
4421 * @klass: where to look for the method
4422 * @name_space: name of the method
4423 * @param_count: number of parameters. -1 for any number.
4424 * @flags: flags which must be set in the method
4426 * Obtains a MonoMethod with a given name and number of parameters.
4427 * It only works if there are no multiple signatures for any given method name.
4430 mono_class_get_method_from_name_flags (MonoClass *klass, const char *name, int param_count, int flags)
4432 MonoMethod *res = NULL;
4435 mono_class_init (klass);
4437 if (klass->methods) {
4438 mono_class_setup_methods (klass);
4439 for (i = 0; i < klass->method.count; ++i) {
4440 MonoMethod *method = klass->methods [i];
4442 if (method->name[0] == name [0] &&
4443 !strcmp (name, method->name) &&
4444 (param_count == -1 || mono_method_signature (method)->param_count == param_count) &&
4445 ((method->flags & flags) == flags)) {
4452 /* Search directly in the metadata to avoid calling setup_methods () */
4453 for (i = 0; i < klass->method.count; ++i) {
4454 guint32 cols [MONO_METHOD_SIZE];
4457 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_METHOD], klass->method.first + i, cols, MONO_METHOD_SIZE);
4459 if (!strcmp (mono_metadata_string_heap (klass->image, cols [MONO_METHOD_NAME]), name)) {
4460 method = mono_get_method (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass);
4461 if ((param_count == -1) || mono_method_signature (method)->param_count == param_count) {
4473 * mono_class_set_failure:
4474 * @klass: class in which the failure was detected
4475 * @ex_type: the kind of exception/error to be thrown (later)
4476 * @ex_data: exception data (specific to each type of exception/error)
4478 * Keep a detected failure informations in the class for later processing.
4479 * Note that only the first failure is kept.
4482 mono_class_set_failure (MonoClass *klass, guint32 ex_type, void *ex_data)
4484 if (klass->exception_type)
4486 klass->exception_type = ex_type;
4487 klass->exception_data = ex_data;
4492 * mono_class_get_exception_for_failure:
4493 * @klass: class in which the failure was detected
4495 * Return a constructed MonoException than the caller can then throw
4496 * using mono_raise_exception - or NULL if no failure is present (or
4497 * doesn't result in an exception).
4500 mono_class_get_exception_for_failure (MonoClass *klass)
4502 switch (klass->exception_type) {
4503 case MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND: {
4504 MonoDomain *domain = mono_domain_get ();
4505 MonoSecurityManager* secman = mono_security_manager_get_methods ();
4506 MonoMethod *method = klass->exception_data;
4507 guint32 error = (method) ? MONO_METADATA_INHERITANCEDEMAND_CLASS : MONO_METADATA_INHERITANCEDEMAND_METHOD;
4508 MonoObject *exc = NULL;
4512 args [1] = mono_assembly_get_object (domain, mono_image_get_assembly (klass->image));
4513 args [2] = mono_type_get_object (domain, &klass->byval_arg);
4514 args [3] = (method) ? mono_method_get_object (domain, method, NULL) : NULL;
4516 mono_runtime_invoke (secman->inheritsecurityexception, NULL, args, &exc);
4517 return (MonoException*) exc;
4519 /* TODO - handle other class related failures */