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_2 (MonoGenericClass *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, 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);
165 mono_type_get_name_recurse (
166 type->data.type, str, FALSE, format);
167 g_string_append_c (str, '*');
170 klass = mono_class_from_mono_type (type);
171 if (klass->nested_in) {
172 mono_type_get_name_recurse (
173 &klass->nested_in->byval_arg, str, TRUE, format);
174 if (format == MONO_TYPE_NAME_FORMAT_IL)
175 g_string_append_c (str, '.');
177 g_string_append_c (str, '+');
179 if (*klass->name_space) {
180 g_string_append (str, klass->name_space);
181 g_string_append_c (str, '.');
183 if (format == MONO_TYPE_NAME_FORMAT_IL) {
184 char *s = strchr (klass->name, '`');
185 int len = s ? s - klass->name : strlen (klass->name);
187 g_string_append_len (str, klass->name, len);
189 g_string_append (str, klass->name);
192 if (klass->generic_class) {
193 MonoGenericClass *gclass = klass->generic_class;
194 MonoTypeNameFormat nested_format;
197 nested_format = format == MONO_TYPE_NAME_FORMAT_FULL_NAME ?
198 MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED : format;
200 if (format == MONO_TYPE_NAME_FORMAT_IL)
201 g_string_append_c (str, '<');
203 g_string_append_c (str, '[');
204 for (i = 0; i < gclass->inst->type_argc; i++) {
206 g_string_append_c (str, ',');
207 if (nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
208 g_string_append_c (str, '[');
209 mono_type_get_name_recurse (
210 gclass->inst->type_argv [i], str, FALSE, nested_format);
211 if (nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
212 g_string_append_c (str, ']');
214 if (format == MONO_TYPE_NAME_FORMAT_IL)
215 g_string_append_c (str, '>');
217 g_string_append_c (str, ']');
218 } else if (klass->generic_container &&
219 (format != MONO_TYPE_NAME_FORMAT_FULL_NAME) &&
220 (format != MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)) {
223 if (format == MONO_TYPE_NAME_FORMAT_IL)
224 g_string_append_c (str, '<');
226 g_string_append_c (str, '[');
227 for (i = 0; i < klass->generic_container->type_argc; i++) {
229 g_string_append_c (str, ',');
230 g_string_append (str, klass->generic_container->type_params [i].name);
232 if (format == MONO_TYPE_NAME_FORMAT_IL)
233 g_string_append_c (str, '>');
235 g_string_append_c (str, ']');
237 if ((format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
238 (type->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
239 _mono_type_get_assembly_name (klass, str);
245 * mono_type_get_name:
248 * Returns: the string representation for type as required by System.Reflection.
249 * The inverse of mono_reflection_parse_type ().
252 mono_type_get_name_full (MonoType *type, MonoTypeNameFormat format)
254 GString* result = g_string_new ("");
256 mono_type_get_name_recurse (type, result, FALSE, format);
259 g_string_append_c (result, '&');
261 return g_string_free (result, FALSE);
265 mono_type_get_name (MonoType *type)
267 return mono_type_get_name_full (type, MONO_TYPE_NAME_FORMAT_IL);
271 mono_type_get_underlying_type (MonoType *type)
273 switch (type->type) {
274 case MONO_TYPE_VALUETYPE:
275 if (type->data.klass->enumtype)
276 return type->data.klass->enum_basetype;
278 case MONO_TYPE_GENERICINST:
279 return mono_type_get_underlying_type (&type->data.generic_class->container_class->byval_arg);
288 mono_class_is_open_constructed_type (MonoType *t)
294 case MONO_TYPE_SZARRAY:
295 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
296 case MONO_TYPE_ARRAY:
297 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
299 return mono_class_is_open_constructed_type (t->data.type);
300 case MONO_TYPE_GENERICINST: {
301 MonoGenericClass *gclass = t->data.generic_class;
304 if (mono_class_is_open_constructed_type (&gclass->container_class->byval_arg))
306 for (i = 0; i < gclass->inst->type_argc; i++)
307 if (mono_class_is_open_constructed_type (gclass->inst->type_argv [i]))
316 static MonoGenericClass *
317 inflate_generic_class (MonoGenericClass *ogclass, MonoGenericContext *context)
319 MonoGenericClass *ngclass, *cached;
321 if (ogclass->is_dynamic) {
322 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
323 ngclass = &dgclass->generic_class;
324 ngclass->is_dynamic = 1;
326 ngclass = g_new0 (MonoGenericClass, 1);
330 ngclass->inst = mono_metadata_inflate_generic_inst (ogclass->inst, context);
332 ngclass->klass = NULL;
334 ngclass->context = g_new0 (MonoGenericContext, 1);
335 ngclass->context->container = context->container;
336 ngclass->context->gclass = ngclass;
339 cached = mono_metadata_lookup_generic_class (ngclass);
340 mono_loader_unlock ();
346 mono_class_create_generic (ngclass);
352 inflate_generic_type (MonoType *type, MonoGenericContext *context)
354 switch (type->type) {
356 if (context->gmethod && context->gmethod->inst->type_argv)
358 context->gmethod->inst->type_argv [type->data.generic_param->num],
365 context->gclass->inst->type_argv [type->data.generic_param->num],
369 case MONO_TYPE_SZARRAY: {
370 MonoClass *eclass = type->data.klass;
371 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
374 nt = dup_type (type, type);
375 nt->data.klass = mono_class_from_mono_type (inflated);
378 case MONO_TYPE_ARRAY: {
379 MonoClass *eclass = type->data.array->eklass;
380 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
383 nt = dup_type (type, type);
384 nt->data.array = g_memdup (nt->data.array, sizeof (MonoArrayType));
385 nt->data.array->eklass = mono_class_from_mono_type (inflated);
388 case MONO_TYPE_GENERICINST: {
389 MonoGenericClass *ngclass = inflate_generic_class (type->data.generic_class, context);
390 MonoType *nt = dup_type (type, type);
391 nt->data.generic_class = ngclass;
394 case MONO_TYPE_CLASS:
395 case MONO_TYPE_VALUETYPE: {
396 MonoClass *klass = type->data.klass;
397 MonoGenericClass *gclass;
400 if (!klass->generic_container)
403 gclass = inflate_generic_class (klass->generic_container->context.gclass, context);
405 nt = dup_type (type, type);
406 nt->type = MONO_TYPE_GENERICINST;
407 nt->data.generic_class = gclass;
417 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
419 MonoType *inflated = inflate_generic_type (type, context);
422 return dup_type (type, type);
424 mono_stats.inflated_type_count++;
429 mono_class_inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
431 MonoMethodSignature *res;
438 res = mono_metadata_signature_alloc (image, sig->param_count);
439 res->ret = mono_class_inflate_generic_type (sig->ret, context);
440 is_open = mono_class_is_open_constructed_type (res->ret);
441 for (i = 0; i < sig->param_count; ++i) {
442 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
444 is_open = mono_class_is_open_constructed_type (res->params [i]);
446 res->hasthis = sig->hasthis;
447 res->explicit_this = sig->explicit_this;
448 res->call_convention = sig->call_convention;
449 res->generic_param_count = sig->generic_param_count;
450 res->sentinelpos = sig->sentinelpos;
451 res->has_type_parameters = is_open;
452 res->is_inflated = 1;
456 static MonoMethodHeader*
457 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
459 MonoMethodHeader *res;
461 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
462 res->code = header->code;
463 res->code_size = header->code_size;
464 res->max_stack = header->max_stack;
465 res->num_clauses = header->num_clauses;
466 res->init_locals = header->init_locals;
467 res->num_locals = header->num_locals;
468 res->clauses = header->clauses;
469 for (i = 0; i < header->num_locals; ++i)
470 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
474 static MonoGenericContext *
475 inflate_generic_context (MonoGenericContext *context, MonoGenericContext *inflate_with)
477 MonoGenericContext *res = g_new0 (MonoGenericContext, 1);
479 res->container = context->container;
482 res->gclass = inflate_generic_class (context->gclass, inflate_with);
484 if (context->gmethod) {
485 res->gmethod = g_new0 (MonoGenericMethod, 1);
486 res->gmethod->generic_class = res->gclass;
488 res->gmethod->container = context->gmethod->container;
489 res->gmethod->inst = mono_metadata_inflate_generic_inst (context->gmethod->inst, inflate_with);
491 res->gmethod = inflate_with->gmethod;
497 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context,
500 MonoMethodInflated *result;
502 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
503 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
506 if (method->is_inflated || mono_method_signature_full (method, context)->is_inflated) {
507 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
509 context = inflate_generic_context (imethod->context, context);
510 method = imethod->declaring;
513 mono_stats.inflated_method_count++;
515 result = g_new0 (MonoMethodInflated, 1);
516 result->nmethod = *(MonoMethodNormal*)method;
517 result->nmethod.method.is_inflated = 1;
518 result->context = context;
519 result->declaring = method;
521 if (result->nmethod.method.klass->generic_class)
522 result->nmethod.method.klass = result->nmethod.method.klass->generic_class->container_class;
524 return (MonoMethod *) result;
528 mono_get_inflated_method (MonoMethod *method)
530 MonoMethodInflated *imethod, *res;
531 MonoMethodHeader *mh;
535 if (!method->is_inflated)
538 imethod = (MonoMethodInflated *) method;
539 if (imethod->inflated)
540 return (MonoMethod *) imethod->inflated;
542 mono_stats.inflated_method_count_2++;
543 mono_stats.generics_metadata_size +=
544 sizeof (MonoMethodInflated) - sizeof (MonoMethodNormal);
546 res = g_new0 (MonoMethodInflated, 1);
548 res->inflated = imethod->inflated = res;
550 mh = mono_method_get_header (method);
552 res->nmethod.header = inflate_generic_header (mh, imethod->context);
554 dtype = mono_class_inflate_generic_type (&method->klass->byval_arg, imethod->context);
555 rklass = res->nmethod.method.klass = mono_class_from_mono_type (dtype);
557 res->nmethod.method.signature = mono_class_inflate_generic_signature (
558 method->klass->image, mono_method_signature (method), imethod->context);
560 return (MonoMethod *) res;
564 * class_compute_field_layout:
565 * @m: pointer to the metadata.
566 * @class: The class to initialize
568 * Initializes the class->fields.
571 class_compute_field_layout (MonoClass *class)
573 MonoImage *m = class->image;
574 const int top = class->field.count;
575 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
576 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
577 int i, blittable = TRUE, real_size = 0;
579 guint32 packing_size = 0;
580 gboolean explicit_size;
581 MonoClassField *field;
583 if (class->size_inited)
587 if (!class->parent->size_inited)
588 class_compute_field_layout (class->parent);
589 class->instance_size += class->parent->instance_size;
590 class->min_align = class->parent->min_align;
591 /* we use |= since it may have been set already */
592 class->has_references |= class->parent->has_references;
593 blittable = class->parent->blittable;
595 class->instance_size = sizeof (MonoObject);
596 class->min_align = 1;
599 /* Get the real size */
600 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
603 g_assert ((packing_size & 0xfffffff0) == 0);
604 class->packing_size = packing_size;
605 real_size += class->instance_size;
609 if (explicit_size && real_size) {
610 class->instance_size = MAX (real_size, class->instance_size);
612 class->size_inited = 1;
613 class->blittable = blittable;
617 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT)
620 /* Prevent infinite loops if the class references itself */
621 class->size_inited = 1;
623 class->fields = g_new0 (MonoClassField, top);
626 * Fetch all the field information.
628 for (i = 0; i < top; i++){
630 guint32 cols [MONO_FIELD_SIZE];
631 int idx = class->field.first + i;
632 MonoGenericContainer *container = NULL;
634 field = &class->fields [i];
635 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
636 /* The name is needed for fieldrefs */
637 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
638 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
639 mono_metadata_decode_value (sig, &sig);
640 /* FIELD signature == 0x06 */
641 g_assert (*sig == 0x06);
642 if (class->generic_container)
643 container = class->generic_container;
644 else if (class->generic_class) {
645 container = class->generic_class->container_class->generic_container;
646 g_assert (container);
648 field->type = mono_metadata_parse_type_full (
649 m, (MonoGenericContext *) container, MONO_PARSE_FIELD,
650 cols [MONO_FIELD_FLAGS], sig + 1, &sig);
651 if (mono_field_is_deleted (field))
653 if (class->generic_class) {
654 field->type = mono_class_inflate_generic_type (
655 field->type, class->generic_class->context);
656 field->type->attrs = cols [MONO_FIELD_FLAGS];
659 field->parent = class;
661 /* Only do these checks if we still think this type is blittable */
662 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
663 if (field->type->byref || MONO_TYPE_IS_REFERENCE (field->type)) {
666 MonoClass *field_class = mono_class_from_mono_type (field->type);
667 if (!field_class || !field_class->blittable)
672 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
673 mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
674 if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
675 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, field->name);
678 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
679 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
681 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
682 field->data = mono_image_rva_map (class->image, rva);
685 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
686 class->enum_basetype = field->type;
687 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
688 blittable = class->element_class->blittable;
691 /* The def_value of fields is compute lazily during vtable creation */
694 if (class == mono_defaults.string_class)
697 class->blittable = blittable;
699 if (class->enumtype && !class->enum_basetype) {
700 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
703 if (explicit_size && real_size) {
704 class->instance_size = MAX (real_size, class->instance_size);
707 if (class->generic_container ||
708 (class->generic_class && class->generic_class->inst->is_open))
711 mono_class_layout_fields (class);
714 /* useful until we keep track of gc-references in corlib etc. */
715 #define IS_GC_REFERENCE(t) ((t)->type == MONO_TYPE_U || (t)->type == MONO_TYPE_I || (t)->type == MONO_TYPE_PTR)
718 * LOCKING: this is supposed to be called with the loader lock held.
721 mono_class_layout_fields (MonoClass *class)
724 const int top = class->field.count;
725 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
726 guint32 pass, passes, real_size;
727 gboolean gc_aware_layout = FALSE;
728 MonoClassField *field;
731 * Enable GC aware auto layout: in this mode, reference
732 * fields are grouped together inside objects, increasing collector
734 * Requires that all classes whose layout is known to native code be annotated
735 * with [StructLayout (LayoutKind.Sequential)]
736 * Value types have gc_aware_layout disabled by default, as per
737 * what the default is for other runtimes.
739 /* corlib is missing [StructLayout] directives in many places */
740 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
741 if (class->image != mono_defaults.corlib &&
742 class->byval_arg.type != MONO_TYPE_VALUETYPE)
743 gc_aware_layout = TRUE;
747 * Compute field layout and total size (not considering static fields)
751 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
752 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
759 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
763 real_size = class->parent->instance_size;
765 real_size = sizeof (MonoObject);
767 for (pass = 0; pass < passes; ++pass) {
768 for (i = 0; i < top; i++){
770 field = &class->fields [i];
772 if (mono_field_is_deleted (field))
774 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
777 if (gc_aware_layout) {
779 * We process fields with reference type in the first pass,
780 * and fields with non-reference type in the second pass.
781 * We use IS_POINTER instead of IS_REFERENCE because in
782 * some internal structures, we store GC_MALLOCed memory
783 * in IntPtr fields...
785 if (MONO_TYPE_IS_POINTER (field->type)) {
794 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
795 (strcmp (field->name, "$PRIVATE$") == 0)) {
796 /* This field is a hack inserted by MCS to empty structures */
800 size = mono_type_size (field->type, &align);
802 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
803 align = class->packing_size ? MIN (class->packing_size, align): align;
804 class->min_align = MAX (align, class->min_align);
805 field->offset = real_size;
806 field->offset += align - 1;
807 field->offset &= ~(align - 1);
808 real_size = field->offset + size;
809 if (MONO_TYPE_IS_REFERENCE (field->type) || IS_GC_REFERENCE (field->type))
810 class->has_references = TRUE;
811 else if (field->type->type == MONO_TYPE_VALUETYPE && field->type->data.klass->has_references)
812 class->has_references = TRUE;
815 class->instance_size = MAX (real_size, class->instance_size);
817 if (class->instance_size & (class->min_align - 1)) {
818 class->instance_size += class->min_align - 1;
819 class->instance_size &= ~(class->min_align - 1);
823 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
825 for (i = 0; i < top; i++) {
827 field = &class->fields [i];
830 * There must be info about all the fields in a type if it
831 * uses explicit layout.
834 if (mono_field_is_deleted (field))
836 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
839 if (MONO_TYPE_IS_REFERENCE (field->type) || IS_GC_REFERENCE (field->type))
840 class->has_references = TRUE;
841 else if (field->type->type == MONO_TYPE_VALUETYPE && field->type->data.klass->has_references)
842 class->has_references = TRUE;
843 size = mono_type_size (field->type, &align);
846 * When we get here, field->offset is already set by the
847 * loader (for either runtime fields or fields loaded from metadata).
848 * The offset is from the start of the object: this works for both
849 * classes and valuetypes.
851 field->offset += sizeof (MonoObject);
856 real_size = MAX (real_size, size + field->offset);
858 class->instance_size = MAX (real_size, class->instance_size);
862 class->size_inited = 1;
865 * Compute static field layout and size
867 for (i = 0; i < top; i++){
869 field = &class->fields [i];
871 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
873 if (mono_field_is_deleted (field))
876 if (MONO_TYPE_IS_REFERENCE (field->type) || IS_GC_REFERENCE (field->type))
877 class->has_static_refs = TRUE;
878 else if (field->type->type == MONO_TYPE_VALUETYPE && field->type->data.klass->has_references)
879 class->has_static_refs = TRUE;
880 size = mono_type_size (field->type, &align);
881 field->offset = class->class_size;
882 field->offset += align - 1;
883 field->offset &= ~(align - 1);
884 class->class_size = field->offset + size;
889 * mono_class_setup_methods:
891 * Initializes the 'methods' array in the klass.
892 * Calling this method should be avoided if possible since it allocates a lot
893 * of long-living MonoMethod structures.
896 mono_class_setup_methods (MonoClass *class)
899 MonoMethod **methods;
901 if (class->methods || class->generic_class)
906 if (class->methods) {
907 mono_loader_unlock ();
911 //printf ("INIT: %s.%s\n", class->name_space, class->name);
913 if (!class->generic_class && !class->methods) {
914 methods = g_new (MonoMethod*, class->method.count);
915 for (i = 0; i < class->method.count; ++i) {
916 methods [i] = mono_get_method (class->image,
917 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
921 if (MONO_CLASS_IS_INTERFACE (class))
922 for (i = 0; i < class->method.count; ++i)
923 methods [i]->slot = i;
925 class->methods = methods;
927 mono_loader_unlock ();
932 mono_class_setup_properties (MonoClass *class)
934 guint startm, endm, i, j;
935 guint32 cols [MONO_PROPERTY_SIZE];
936 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
937 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
938 MonoProperty *properties;
940 if (class->properties)
945 if (class->properties) {
946 mono_loader_unlock ();
950 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
951 class->property.count = class->property.last - class->property.first;
953 if (class->property.count)
954 mono_class_setup_methods (class);
956 properties = g_new0 (MonoProperty, class->property.count);
957 for (i = class->property.first; i < class->property.last; ++i) {
958 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
959 properties [i - class->property.first].parent = class;
960 properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
961 properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
963 startm = mono_metadata_methods_from_property (class->image, i, &endm);
964 for (j = startm; j < endm; ++j) {
965 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
966 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
967 case METHOD_SEMANTIC_SETTER:
968 properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
970 case METHOD_SEMANTIC_GETTER:
971 properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
979 class->properties = properties;
981 mono_loader_unlock ();
985 mono_class_setup_events (MonoClass *class)
987 guint startm, endm, i, j;
988 guint32 cols [MONO_EVENT_SIZE];
989 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
990 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
995 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
996 class->event.count = class->event.last - class->event.first;
998 if (class->event.count)
999 mono_class_setup_methods (class);
1001 class->events = g_new0 (MonoEvent, class->event.count);
1002 for (i = class->event.first; i < class->event.last; ++i) {
1003 MonoEvent *event = &class->events [i - class->event.first];
1005 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
1006 event->parent = class;
1007 event->attrs = cols [MONO_EVENT_FLAGS];
1008 event->name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
1010 startm = mono_metadata_methods_from_event (class->image, i, &endm);
1011 for (j = startm; j < endm; ++j) {
1012 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
1013 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1014 case METHOD_SEMANTIC_ADD_ON:
1015 event->add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1017 case METHOD_SEMANTIC_REMOVE_ON:
1018 event->remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1020 case METHOD_SEMANTIC_FIRE:
1021 event->raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1023 case METHOD_SEMANTIC_OTHER: {
1026 if (event->other == NULL) {
1027 event->other = g_new0 (MonoMethod*, 1);
1029 while (event->other [n])
1031 event->other = g_realloc (event->other, (n + 1) * sizeof (MonoMethod*));
1033 event->other [n] = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1044 mono_get_unique_iid (MonoClass *class)
1046 static GHashTable *iid_hash = NULL;
1047 static guint iid = 0;
1052 g_assert (MONO_CLASS_IS_INTERFACE (class));
1054 mono_loader_lock ();
1057 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
1059 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
1061 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
1062 mono_loader_unlock ();
1064 return GPOINTER_TO_INT (value);
1066 g_hash_table_insert (iid_hash, str, GINT_TO_POINTER (iid));
1070 mono_loader_unlock ();
1072 g_assert (iid <= 65535);
1077 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
1082 for (i = 0; i < klass->interface_count; i++) {
1083 ic = klass->interfaces [i];
1086 *res = g_ptr_array_new ();
1087 g_ptr_array_add (*res, ic);
1088 mono_class_init (ic);
1090 collect_implemented_interfaces_aux (ic, res);
1095 mono_class_get_implemented_interfaces (MonoClass *klass)
1097 GPtrArray *res = NULL;
1099 collect_implemented_interfaces_aux (klass, &res);
1103 typedef struct _IOffsetInfo IOffsetInfo;
1104 struct _IOffsetInfo {
1108 int data [MONO_ZERO_LEN_ARRAY];
1111 static IOffsetInfo *cached_offset_info = NULL;
1112 static int next_offset_info_size = 128;
1115 cache_interface_offsets (int max_iid, int *data)
1117 IOffsetInfo *cached_info;
1120 for (cached_info = cached_offset_info; cached_info; cached_info = cached_info->next) {
1121 cached = cached_info->data;
1122 while (cached < cached_info->data + cached_info->size && *cached) {
1123 if (*cached == max_iid) {
1124 int i, matched = TRUE;
1126 for (i = 0; i < max_iid; ++i) {
1127 if (cached [i] != data [i]) {
1136 cached += *cached + 1;
1140 /* find a free slot */
1141 for (cached_info = cached_offset_info; cached_info; cached_info = cached_info->next) {
1142 if (cached_info->size - cached_info->next_free >= max_iid + 1) {
1143 cached = &cached_info->data [cached_info->next_free];
1144 *cached++ = max_iid;
1145 memcpy (cached, data, max_iid * sizeof (int));
1146 cached_info->next_free += max_iid + 1;
1150 /* allocate a new chunk */
1151 if (max_iid + 1 < next_offset_info_size) {
1152 new_size = next_offset_info_size;
1153 if (next_offset_info_size < 4096)
1154 next_offset_info_size += next_offset_info_size >> 2;
1156 new_size = max_iid + 1;
1158 cached_info = g_malloc0 (sizeof (IOffsetInfo) + sizeof (int) * new_size);
1159 cached_info->size = new_size;
1160 /*g_print ("allocated %d offset entries at %p (total: %d)\n", new_size, cached_info->data, offset_info_total_size);*/
1161 cached = &cached_info->data [0];
1162 *cached++ = max_iid;
1163 memcpy (cached, data, max_iid * sizeof (int));
1164 cached_info->next_free += max_iid + 1;
1165 cached_info->next = cached_offset_info;
1166 cached_offset_info = cached_info;
1171 setup_interface_offsets (MonoClass *class, int cur_slot)
1178 /* compute maximum number of slots and maximum interface id */
1180 for (k = class; k ; k = k->parent) {
1181 for (i = 0; i < k->interface_count; i++) {
1182 ic = k->interfaces [i];
1185 mono_class_init (ic);
1187 if (max_iid < ic->interface_id)
1188 max_iid = ic->interface_id;
1190 ifaces = mono_class_get_implemented_interfaces (k);
1192 for (i = 0; i < ifaces->len; ++i) {
1193 ic = g_ptr_array_index (ifaces, i);
1194 if (max_iid < ic->interface_id)
1195 max_iid = ic->interface_id;
1197 g_ptr_array_free (ifaces, TRUE);
1201 if (MONO_CLASS_IS_INTERFACE (class)) {
1202 if (max_iid < class->interface_id)
1203 max_iid = class->interface_id;
1205 class->max_interface_id = max_iid;
1206 /* compute vtable offset for interfaces */
1207 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
1209 for (i = 0; i <= max_iid; i++)
1210 class->interface_offsets [i] = -1;
1212 ifaces = mono_class_get_implemented_interfaces (class);
1214 for (i = 0; i < ifaces->len; ++i) {
1215 ic = g_ptr_array_index (ifaces, i);
1216 class->interface_offsets [ic->interface_id] = cur_slot;
1217 cur_slot += ic->method.count;
1219 g_ptr_array_free (ifaces, TRUE);
1222 for (k = class->parent; k ; k = k->parent) {
1223 ifaces = mono_class_get_implemented_interfaces (k);
1225 for (i = 0; i < ifaces->len; ++i) {
1226 ic = g_ptr_array_index (ifaces, i);
1228 if (class->interface_offsets [ic->interface_id] == -1) {
1229 int io = k->interface_offsets [ic->interface_id];
1233 class->interface_offsets [ic->interface_id] = io;
1236 g_ptr_array_free (ifaces, TRUE);
1240 if (MONO_CLASS_IS_INTERFACE (class))
1241 class->interface_offsets [class->interface_id] = cur_slot;
1243 cached_data = cache_interface_offsets (max_iid + 1, class->interface_offsets);
1244 g_free (class->interface_offsets);
1245 class->interface_offsets = cached_data;
1251 mono_class_setup_vtable (MonoClass *class)
1253 MonoMethod **overrides;
1259 mono_class_setup_methods (class);
1261 if (MONO_CLASS_IS_INTERFACE (class))
1264 mono_loader_lock ();
1266 if (class->vtable) {
1267 mono_loader_unlock ();
1271 overrides = mono_class_get_overrides (class->image, class->type_token, &onum);
1272 mono_class_setup_vtable_general (class, overrides, onum);
1275 mono_loader_unlock ();
1279 setup_generic_vtable (MonoClass *class, MonoMethod **overrides, int onum)
1281 MonoClass *gklass = class->generic_class->container_class;
1282 MonoGenericContext *gcontext = class->generic_class->context;
1285 mono_class_init (gklass);
1286 class->vtable_size = gklass->vtable_size;
1288 class->vtable = g_new0 (gpointer, class->vtable_size);
1289 memcpy (class->vtable, gklass->vtable, sizeof (gpointer) * class->vtable_size);
1291 for (i = 0; i < class->vtable_size; i++) {
1292 MonoMethod *m = class->vtable [i];
1297 m = mono_class_inflate_generic_method (m, gcontext, class);
1298 class->vtable [i] = mono_get_inflated_method (m);
1301 class->max_interface_id = gklass->max_interface_id;
1302 class->interface_offsets = g_new0 (gpointer, gklass->max_interface_id + 1);
1303 memcpy (class->interface_offsets, gklass->interface_offsets,
1304 sizeof (gpointer) * (gklass->max_interface_id + 1));
1308 * LOCKING: this is supposed to be called with the loader lock held.
1311 mono_class_setup_vtable_general (MonoClass *class, MonoMethod **overrides, int onum)
1314 MonoMethod **vtable;
1315 int i, max_vtsize = 0, max_iid, cur_slot = 0;
1317 GHashTable *override_map = NULL;
1318 gboolean security_enabled = mono_is_security_manager_active ();
1323 if (class->generic_class) {
1324 setup_generic_vtable (class, overrides, onum);
1328 ifaces = mono_class_get_implemented_interfaces (class);
1330 for (i = 0; i < ifaces->len; i++) {
1331 MonoClass *ic = g_ptr_array_index (ifaces, i);
1332 max_vtsize += ic->method.count;
1334 g_ptr_array_free (ifaces, TRUE);
1337 if (class->parent) {
1338 mono_class_init (class->parent);
1339 mono_class_setup_vtable (class->parent);
1340 max_vtsize += class->parent->vtable_size;
1341 cur_slot = class->parent->vtable_size;
1344 max_vtsize += class->method.count;
1346 vtable = alloca (sizeof (gpointer) * max_vtsize);
1347 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
1349 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
1351 cur_slot = setup_interface_offsets (class, cur_slot);
1352 max_iid = class->max_interface_id;
1354 if (class->parent && class->parent->vtable_size)
1355 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
1357 /* override interface methods */
1358 for (i = 0; i < onum; i++) {
1359 MonoMethod *decl = overrides [i*2];
1360 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
1362 mono_class_setup_methods (decl->klass);
1363 g_assert (decl->slot != -1);
1364 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
1365 vtable [dslot] = overrides [i*2 + 1];
1366 vtable [dslot]->slot = dslot;
1368 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1370 g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
1374 for (k = class; k ; k = k->parent) {
1377 ifaces = mono_class_get_implemented_interfaces (k);
1379 nifaces = ifaces->len;
1380 for (i = 0; i < nifaces; i++) {
1383 ic = g_ptr_array_index (ifaces, i);
1384 io = k->interface_offsets [ic->interface_id];
1387 g_assert (io <= max_vtsize);
1390 mono_class_setup_methods (ic);
1391 for (l = 0; l < ic->method.count; l++) {
1392 MonoMethod *im = ic->methods [l];
1394 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1397 for (j = 0; j < class->method.count; ++j) {
1398 MonoMethod *cm = class->methods [j];
1399 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1400 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
1401 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
1403 if (!strcmp(cm->name, im->name) &&
1404 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1406 /* CAS - SecurityAction.InheritanceDemand on interface */
1407 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1408 mono_secman_inheritancedemand_method (cm, im);
1411 g_assert (io + l <= max_vtsize);
1412 vtable [io + l] = cm;
1417 /* already implemented */
1418 if (io >= k->vtable_size)
1422 for (l = 0; l < ic->method.count; l++) {
1423 MonoMethod *im = ic->methods [l];
1426 g_assert (io + l <= max_vtsize);
1428 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1431 for (k1 = class; k1; k1 = k1->parent) {
1432 for (j = 0; j < k1->method.count; ++j) {
1433 MonoMethod *cm = k1->methods [j];
1435 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1436 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
1439 if (!strcmp(cm->name, im->name) &&
1440 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1442 /* CAS - SecurityAction.InheritanceDemand on interface */
1443 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1444 mono_secman_inheritancedemand_method (cm, im);
1447 g_assert (io + l <= max_vtsize);
1448 vtable [io + l] = cm;
1453 g_assert (io + l <= max_vtsize);
1454 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1459 for (l = 0; l < ic->method.count; l++) {
1460 MonoMethod *im = ic->methods [l];
1461 char *qname, *fqname, *cname, *the_cname;
1464 if (vtable [io + l])
1467 if (ic->generic_class) {
1468 MonoClass *the_ic = ic->generic_class->container_class;
1469 the_cname = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
1473 cname = (char*)ic->name;
1476 qname = g_strconcat (cname, ".", im->name, NULL);
1477 if (ic->name_space && ic->name_space [0])
1478 fqname = g_strconcat (ic->name_space, ".", cname, ".", im->name, NULL);
1482 for (k1 = class; k1; k1 = k1->parent) {
1483 for (j = 0; j < k1->method.count; ++j) {
1484 MonoMethod *cm = k1->methods [j];
1486 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1489 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
1490 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1492 /* CAS - SecurityAction.InheritanceDemand on interface */
1493 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1494 mono_secman_inheritancedemand_method (cm, im);
1497 g_assert (io + l <= max_vtsize);
1498 vtable [io + l] = cm;
1509 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
1510 for (l = 0; l < ic->method.count; l++) {
1512 MonoMethod *im = ic->methods [l];
1513 if (im->flags & METHOD_ATTRIBUTE_STATIC)
1515 g_assert (io + l <= max_vtsize);
1518 * If one of our parents already implements this interface
1519 * we can inherit the implementation.
1521 if (!(vtable [io + l])) {
1522 MonoClass *parent = class->parent;
1524 if ((ic->interface_id <= parent->max_interface_id) &&
1525 (parent->interface_offsets [ic->interface_id]) &&
1527 vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
1530 if (!(vtable [io + l])) {
1531 for (j = 0; j < onum; ++j) {
1532 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
1533 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
1535 msig = mono_signature_get_desc (mono_method_signature (im), FALSE);
1536 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
1537 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
1539 for (j = 0; j < class->method.count; ++j) {
1540 MonoMethod *cm = class->methods [j];
1541 msig = mono_signature_get_desc (mono_method_signature (cm), FALSE);
1543 printf ("METHOD %s(%s)\n", cm->name, msig);
1546 g_assert_not_reached ();
1551 for (l = 0; l < ic->method.count; l++) {
1552 MonoMethod *im = vtable [io + l];
1555 g_assert (io + l <= max_vtsize);
1557 /* FIXME: why do we need this ? */
1559 /* g_assert_not_reached (); */
1565 g_ptr_array_free (ifaces, TRUE);
1568 for (i = 0; i < class->method.count; ++i) {
1571 cm = class->methods [i];
1574 * Non-virtual method have no place in the vtable.
1575 * This also catches static methods (since they are not virtual).
1577 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1581 * If the method is REUSE_SLOT, we must check in the
1582 * base class for a method to override.
1584 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
1586 for (k = class->parent; k ; k = k->parent) {
1588 for (j = 0; j < k->method.count; ++j) {
1589 MonoMethod *m1 = k->methods [j];
1590 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
1592 if (!strcmp(cm->name, m1->name) &&
1593 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (m1))) {
1595 /* CAS - SecurityAction.InheritanceDemand */
1596 if (security_enabled && (m1->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1597 mono_secman_inheritancedemand_method (cm, m1);
1600 slot = k->methods [j]->slot;
1601 g_assert (cm->slot < max_vtsize);
1603 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1604 g_hash_table_insert (override_map, m1, cm);
1616 cm->slot = cur_slot++;
1618 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT) && ! mono_method_signature (cm)->generic_param_count)
1619 vtable [cm->slot] = cm;
1622 /* override non interface methods */
1623 for (i = 0; i < onum; i++) {
1624 MonoMethod *decl = overrides [i*2];
1625 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
1626 g_assert (decl->slot != -1);
1627 vtable [decl->slot] = overrides [i*2 + 1];
1628 overrides [i * 2 + 1]->slot = decl->slot;
1630 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1631 g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
1636 * If a method occupies more than one place in the vtable, and it is
1637 * overriden, then change the other occurances too.
1640 for (i = 0; i < max_vtsize; ++i)
1642 MonoMethod *cm = g_hash_table_lookup (override_map, vtable [i]);
1647 g_hash_table_destroy (override_map);
1650 if (class->generic_class) {
1651 MonoClass *gklass = class->generic_class->container_class;
1653 mono_class_init (gklass);
1654 class->vtable_size = gklass->vtable_size;
1656 class->vtable_size = cur_slot;
1658 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
1659 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
1661 if (mono_print_vtable) {
1664 for (i = 0; i <= max_iid; i++)
1665 if (class->interface_offsets [i] != -1)
1668 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
1669 class->name, class->vtable_size, icount);
1671 for (i = 0; i < class->vtable_size; ++i) {
1676 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
1677 cm->klass->name_space, cm->klass->name,
1684 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
1685 class->name, max_iid);
1687 for (i = 0; i < class->interface_count; i++) {
1688 ic = class->interfaces [i];
1689 printf (" slot %03d(%03d) %s.%s\n",
1690 class->interface_offsets [ic->interface_id],
1691 ic->method.count, ic->name_space, ic->name);
1694 for (k = class->parent; k ; k = k->parent) {
1695 for (i = 0; i < k->interface_count; i++) {
1696 ic = k->interfaces [i];
1697 printf (" slot %03d(%03d) %s.%s\n",
1698 class->interface_offsets [ic->interface_id],
1699 ic->method.count, ic->name_space, ic->name);
1708 * @class: the class to initialize
1710 * compute the instance_size, class_size and other infos that cannot be
1711 * computed at mono_class_get() time. Also compute a generic vtable and
1712 * the method slot numbers. We use this infos later to create a domain
1716 mono_class_init (MonoClass *class)
1719 static MonoMethod *default_ghc = NULL;
1720 static MonoMethod *default_finalize = NULL;
1721 static int finalize_slot = -1;
1722 static int ghc_slot = -1;
1723 MonoCachedClassInfo cached_info;
1730 /*g_print ("Init class %s\n", class->name);*/
1732 /* We do everything inside the lock to prevent races */
1733 mono_loader_lock ();
1735 if (class->inited) {
1736 mono_loader_unlock ();
1737 /* Somebody might have gotten in before us */
1741 if (class->init_pending) {
1743 * We might be called recursively from mono_class_from_typeref if
1744 * one of our fields has a type which is a nested type of this class,
1745 * and the compiler encodes it as a typeref, like older versions of
1748 if (class->size_inited) {
1749 mono_loader_unlock ();
1753 /* this indicates a cyclic dependency */
1754 g_error ("pending init %s.%s\n", class->name_space, class->name);
1757 class->init_pending = 1;
1759 /* CAS - SecurityAction.InheritanceDemand */
1760 if (mono_is_security_manager_active () && class->parent && (class->parent->flags & TYPE_ATTRIBUTE_HAS_SECURITY)) {
1761 mono_secman_inheritancedemand_class (class, class->parent);
1764 if (mono_debugger_start_class_init_func)
1765 mono_debugger_start_class_init_func (class);
1767 mono_stats.initialized_class_count++;
1769 if (class->generic_class && !class->generic_class->is_dynamic) {
1770 MonoGenericClass *gclass = class->generic_class;
1771 MonoClass *gklass = gclass->container_class;
1773 mono_stats.generic_class_count++;
1775 class->method = gklass->method;
1776 class->field = gklass->field;
1778 mono_class_create_generic_2 (gclass);
1780 mono_class_init (gklass);
1781 mono_class_setup_methods (gklass);
1782 mono_class_setup_properties (gklass);
1784 if (MONO_CLASS_IS_INTERFACE (class))
1785 class->interface_id = mono_get_unique_iid (class);
1787 g_assert (class->method.count == gklass->method.count);
1788 class->methods = g_new0 (MonoMethod *, class->method.count);
1790 for (i = 0; i < class->method.count; i++) {
1791 MonoMethod *inflated = mono_class_inflate_generic_method (
1792 gklass->methods [i], gclass->context, gclass->klass);
1794 class->methods [i] = mono_get_inflated_method (inflated);
1797 g_assert (class->field.count == gklass->field.count);
1798 class->fields = g_new0 (MonoClassField, class->field.count);
1800 for (i = 0; i < class->field.count; i++) {
1801 MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
1802 ifield->generic_type = gklass->fields [i].type;
1804 class->fields [i] = gklass->fields [i];
1805 class->fields [i].generic_info = ifield;
1806 class->fields [i].parent = class;
1807 class->fields [i].type = mono_class_inflate_generic_type (
1808 class->fields [i].type, gclass->context);
1811 class->property = gklass->property;
1812 class->properties = g_new0 (MonoProperty, class->property.count);
1814 for (i = 0; i < class->property.count; i++) {
1815 MonoProperty *prop = &class->properties [i];
1817 *prop = gklass->properties [i];
1820 prop->get = mono_class_inflate_generic_method (
1821 prop->get, gclass->context, gclass->klass);
1823 prop->set = mono_class_inflate_generic_method (
1824 prop->set, gclass->context, gclass->klass);
1826 prop->parent = class;
1829 g_assert (class->interface_count == gklass->interface_count);
1832 if (class->parent && !class->parent->inited)
1833 mono_class_init (class->parent);
1835 if (!class->generic_class) {
1836 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
1839 guint32 cols [MONO_NESTED_CLASS_SIZE];
1840 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
1841 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
1842 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
1844 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
1849 * Computes the size used by the fields, and their locations
1851 if (!class->size_inited)
1852 class_compute_field_layout (class);
1854 /* initialize method pointers */
1857 MonoMethodSignature *sig;
1858 class->method.count = class->rank > 1? 2: 1;
1859 sig = mono_metadata_signature_alloc (class->image, class->rank);
1860 sig->ret = &mono_defaults.void_class->byval_arg;
1861 sig->pinvoke = TRUE;
1862 for (i = 0; i < class->rank; ++i)
1863 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1865 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1866 ctor->klass = class;
1867 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1868 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1869 ctor->signature = sig;
1870 ctor->name = ".ctor";
1872 class->methods = g_new (MonoMethod*, class->method.count);
1873 class->methods [0] = ctor;
1874 if (class->rank > 1) {
1875 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
1876 sig->ret = &mono_defaults.void_class->byval_arg;
1877 sig->pinvoke = TRUE;
1878 for (i = 0; i < class->rank * 2; ++i)
1879 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1881 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1882 ctor->klass = class;
1883 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1884 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1885 ctor->signature = sig;
1886 ctor->name = ".ctor";
1888 class->methods [1] = ctor;
1892 mono_class_setup_supertypes (class);
1894 if (MONO_CLASS_IS_INTERFACE (class)) {
1895 class->init_pending = 0;
1898 * class->interface_offsets is needed for the castclass/isinst code, so
1899 * we have to setup them for interfaces, too.
1901 setup_interface_offsets (class, 0);
1902 mono_loader_unlock ();
1904 if (mono_debugger_class_init_func)
1905 mono_debugger_class_init_func (class);
1911 if (class == mono_defaults.object_class) {
1912 mono_class_setup_vtable (class);
1913 for (i = 0; i < class->vtable_size; ++i) {
1914 MonoMethod *cm = class->vtable [i];
1916 if (!strcmp (cm->name, "GetHashCode")) {
1922 g_assert (ghc_slot > 0);
1924 default_ghc = class->vtable [ghc_slot];
1928 if (!default_finalize) {
1929 if (class == mono_defaults.object_class) {
1930 mono_class_setup_vtable (class);
1931 for (i = 0; i < class->vtable_size; ++i) {
1932 MonoMethod *cm = class->vtable [i];
1934 if (!strcmp (cm->name, "Finalize")) {
1940 g_assert (finalize_slot > 0);
1942 default_finalize = class->vtable [finalize_slot];
1947 * If possible, avoid the creation of the generic vtable by requesting
1948 * cached info from the runtime.
1950 if (mono_class_get_cached_class_info (class, &cached_info)) {
1951 guint32 cur_slot = 0;
1953 class->vtable_size = cached_info.vtable_size;
1954 class->has_finalize = cached_info.has_finalize;
1955 class->ghcimpl = cached_info.ghcimpl;
1956 class->has_cctor = cached_info.has_cctor;
1958 if (class->parent) {
1959 mono_class_init (class->parent);
1960 cur_slot = class->parent->vtable_size;
1963 setup_interface_offsets (class, cur_slot);
1966 mono_class_setup_vtable (class);
1969 if (class->parent) {
1970 if (class->vtable [ghc_slot] == default_ghc) {
1975 /* Object::Finalize should have empty implemenatation */
1976 class->has_finalize = 0;
1977 if (class->parent) {
1978 if (class->vtable [finalize_slot] != default_finalize)
1979 class->has_finalize = 1;
1982 for (i = 0; i < class->method.count; ++i) {
1983 MonoMethod *method = class->methods [i];
1984 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
1985 (strcmp (".cctor", method->name) == 0)) {
1986 class->has_cctor = 1;
1993 class->init_pending = 0;
1995 mono_loader_unlock ();
1997 if (mono_debugger_class_init_func)
1998 mono_debugger_class_init_func (class);
2002 * LOCKING: this assumes the loader lock is held
2005 mono_class_setup_mono_type (MonoClass *class)
2007 const char *name = class->name;
2008 const char *nspace = class->name_space;
2010 if (MONO_CLASS_IS_INTERFACE (class))
2011 class->interface_id = mono_get_unique_iid (class);
2013 class->this_arg.byref = 1;
2014 class->this_arg.data.klass = class;
2015 class->this_arg.type = MONO_TYPE_CLASS;
2016 class->byval_arg.data.klass = class;
2017 class->byval_arg.type = MONO_TYPE_CLASS;
2019 if (!strcmp (nspace, "System")) {
2020 if (!strcmp (name, "ValueType")) {
2022 * do not set the valuetype bit for System.ValueType.
2023 * class->valuetype = 1;
2025 class->blittable = TRUE;
2026 } else if (!strcmp (name, "Enum")) {
2028 * do not set the valuetype bit for System.Enum.
2029 * class->valuetype = 1;
2031 class->valuetype = 0;
2032 class->enumtype = 0;
2033 } else if (!strcmp (name, "Object")) {
2034 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
2035 } else if (!strcmp (name, "String")) {
2036 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
2037 } else if (!strcmp (name, "TypedReference")) {
2038 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
2042 if (class->valuetype) {
2043 int t = MONO_TYPE_VALUETYPE;
2044 if (!strcmp (nspace, "System")) {
2047 if (!strcmp (name, "Boolean")) {
2048 t = MONO_TYPE_BOOLEAN;
2049 } else if (!strcmp(name, "Byte")) {
2051 class->blittable = TRUE;
2055 if (!strcmp (name, "Char")) {
2060 if (!strcmp (name, "Double")) {
2062 class->blittable = TRUE;
2066 if (!strcmp (name, "Int32")) {
2068 class->blittable = TRUE;
2069 } else if (!strcmp(name, "Int16")) {
2071 class->blittable = TRUE;
2072 } else if (!strcmp(name, "Int64")) {
2074 class->blittable = TRUE;
2075 } else if (!strcmp(name, "IntPtr")) {
2077 class->blittable = TRUE;
2081 if (!strcmp (name, "Single")) {
2083 class->blittable = TRUE;
2084 } else if (!strcmp(name, "SByte")) {
2086 class->blittable = TRUE;
2090 if (!strcmp (name, "UInt32")) {
2092 class->blittable = TRUE;
2093 } else if (!strcmp(name, "UInt16")) {
2095 class->blittable = TRUE;
2096 } else if (!strcmp(name, "UInt64")) {
2098 class->blittable = TRUE;
2099 } else if (!strcmp(name, "UIntPtr")) {
2101 class->blittable = TRUE;
2105 if (!strcmp (name, "TypedReference")) {
2106 t = MONO_TYPE_TYPEDBYREF;
2107 class->blittable = TRUE;
2111 if (!strcmp (name, "Void")) {
2119 class->this_arg.type = class->byval_arg.type = t;
2124 * LOCKING: this assumes the loader lock is held
2127 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
2129 gboolean system_namespace;
2131 system_namespace = !strcmp (class->name_space, "System");
2133 /* if root of the hierarchy */
2134 if (system_namespace && !strcmp (class->name, "Object")) {
2135 class->parent = NULL;
2136 class->instance_size = sizeof (MonoObject);
2139 if (!strcmp (class->name, "<Module>")) {
2140 class->parent = NULL;
2141 class->instance_size = 0;
2145 if (parent && parent->generic_class)
2146 mono_class_create_generic_2 (parent->generic_class);
2148 if (!MONO_CLASS_IS_INTERFACE (class)) {
2149 class->parent = parent;
2152 g_assert_not_reached (); /* FIXME */
2154 if (parent->generic_class && !parent->name) {
2156 * If the parent is a generic instance, we may get
2157 * called before it is fully initialized, especially
2158 * before it has its name.
2163 class->marshalbyref = parent->marshalbyref;
2164 class->contextbound = parent->contextbound;
2165 class->delegate = parent->delegate;
2167 if (system_namespace) {
2168 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
2169 class->marshalbyref = 1;
2171 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
2172 class->contextbound = 1;
2174 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
2175 class->delegate = 1;
2178 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
2179 (strcmp (class->parent->name_space, "System") == 0)))
2180 class->valuetype = 1;
2181 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
2182 class->valuetype = class->enumtype = 1;
2184 /*class->enumtype = class->parent->enumtype; */
2185 mono_class_setup_supertypes (class);
2187 class->parent = NULL;
2193 * LOCKING: this assumes the loader lock is held
2196 mono_class_setup_supertypes (MonoClass *class)
2200 if (class->supertypes)
2203 if (class->parent && !class->parent->supertypes)
2204 mono_class_setup_supertypes (class->parent);
2206 class->idepth = class->parent->idepth + 1;
2210 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
2211 class->supertypes = g_new0 (MonoClass *, ms);
2213 if (class->parent) {
2214 class->supertypes [class->idepth - 1] = class;
2215 memcpy (class->supertypes, class->parent->supertypes, class->parent->idepth * sizeof (gpointer));
2217 class->supertypes [0] = class;
2222 * If we inherit a type parameter from an outer class, set its owner to that class.
2225 set_generic_param_owner (MonoGenericContainer *container, MonoClass *klass, int pos)
2227 MonoGenericContainer *gc;
2230 if (klass->nested_in)
2231 pos = set_generic_param_owner (container, klass->nested_in, pos);
2233 if (!klass->generic_container)
2236 gc = klass->generic_container;
2237 for (i = pos; i < gc->type_argc; i++)
2238 container->type_params [i].owner = gc;
2240 return pos + gc->type_argc;
2243 static MonoGenericInst *
2244 get_shared_inst (MonoGenericContainer *container)
2246 MonoGenericInst *nginst;
2249 nginst = g_new0 (MonoGenericInst, 1);
2250 nginst->type_argc = container->type_argc;
2251 nginst->type_argv = g_new0 (MonoType *, nginst->type_argc);
2252 nginst->is_reference = 1;
2253 nginst->is_open = 1;
2255 for (i = 0; i < nginst->type_argc; i++) {
2256 MonoType *t = g_new0 (MonoType, 1);
2258 t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2259 t->data.generic_param = &container->type_params [i];
2261 nginst->type_argv [i] = t;
2264 return mono_metadata_lookup_generic_inst (nginst);
2268 mono_get_shared_generic_class (MonoGenericContainer *container, gboolean is_dynamic)
2270 MonoGenericClass *gclass;
2273 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
2274 gclass = &dgclass->generic_class;
2275 gclass->is_dynamic = 1;
2277 gclass = g_new0 (MonoGenericClass, 1);
2279 gclass->context = &container->context;
2280 gclass->container_class = container->klass;
2281 gclass->inst = get_shared_inst (container);
2284 MonoGenericClass *cached = mono_metadata_lookup_generic_class (gclass);
2292 gclass->klass = container->klass;
2298 * @image: context where the image is created
2299 * @type_token: typedef token
2302 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
2304 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
2305 MonoClass *class, *parent = NULL;
2306 guint32 cols [MONO_TYPEDEF_SIZE];
2307 guint32 cols_next [MONO_TYPEDEF_SIZE];
2308 guint tidx = mono_metadata_token_index (type_token);
2309 MonoGenericContext *context = NULL;
2310 const char *name, *nspace;
2312 MonoClass **interfaces;
2314 mono_loader_lock ();
2316 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
2317 mono_loader_unlock ();
2321 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
2323 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
2325 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2326 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2328 class = g_malloc0 (sizeof (MonoClass));
2331 class->name_space = nspace;
2333 class->image = image;
2334 class->type_token = type_token;
2335 class->flags = cols [MONO_TYPEDEF_FLAGS];
2337 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
2339 class->generic_container = mono_metadata_load_generic_params (image, class->type_token, NULL);
2340 if (class->generic_container) {
2341 class->generic_container->klass = class;
2342 context = &class->generic_container->context;
2344 context->gclass = mono_get_shared_generic_class (context->container, FALSE);
2347 if (cols [MONO_TYPEDEF_EXTENDS])
2348 parent = mono_class_get_full (
2349 image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]), context);
2351 mono_class_setup_parent (class, parent);
2353 mono_class_setup_mono_type (class);
2355 interfaces = mono_metadata_interfaces_from_typedef_full (image, type_token, &icount, context);
2357 class->interfaces = interfaces;
2358 class->interface_count = icount;
2360 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
2362 /* fixme: maybe we must set this on windows
2363 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
2367 class->cast_class = class->element_class = class;
2369 /*g_print ("Load class %s\n", name);*/
2372 * Compute the field and method lists
2374 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
2375 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
2377 if (tt->rows > tidx){
2378 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
2379 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
2380 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
2382 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
2383 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
2386 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
2387 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
2388 class->field.count = class->field.last - class->field.first;
2390 class->field.count = 0;
2392 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
2393 class->method.count = class->method.last - class->method.first;
2395 class->method.count = 0;
2397 /* reserve space to store vector pointer in arrays */
2398 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
2399 class->instance_size += 2 * sizeof (gpointer);
2400 g_assert (class->field.count == 0);
2403 if (class->enumtype)
2404 class_compute_field_layout (class);
2406 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
2407 class->nested_in = mono_class_create_from_typedef (image, type_token);
2409 if (class->nested_in && class->generic_container)
2410 set_generic_param_owner (class->generic_container, class->nested_in, 0);
2412 mono_loader_unlock ();
2418 mono_class_create_generic (MonoGenericClass *gclass)
2420 MonoClass *klass, *gklass;
2423 gclass->klass = g_malloc0 (sizeof (MonoClass));
2424 klass = gclass->klass;
2426 gklass = gclass->container_class;
2428 klass->nested_in = gklass->nested_in;
2430 klass->name = gklass->name;
2431 klass->name_space = gklass->name_space;
2432 klass->image = gklass->image;
2433 klass->flags = gklass->flags;
2435 klass->generic_class = gclass;
2437 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
2438 klass->this_arg.data.generic_class = klass->byval_arg.data.generic_class = gclass;
2439 klass->this_arg.byref = TRUE;
2441 klass->cast_class = klass->element_class = klass;
2443 if (gclass->is_dynamic) {
2444 klass->instance_size = gklass->instance_size;
2445 klass->class_size = gklass->class_size;
2446 klass->size_inited = 1;
2449 klass->valuetype = gklass->valuetype;
2451 mono_class_setup_supertypes (klass);
2456 mono_class_create_generic_2 (MonoGenericClass *gclass)
2458 MonoClass *klass, *gklass;
2462 klass = gclass->klass;
2463 gklass = gclass->container_class;
2465 klass->interface_count = gklass->interface_count;
2466 klass->interfaces = g_new0 (MonoClass *, klass->interface_count);
2467 for (i = 0; i < klass->interface_count; i++) {
2468 MonoType *it = &gklass->interfaces [i]->byval_arg;
2469 MonoType *inflated = mono_class_inflate_generic_type (it, gclass->context);
2470 klass->interfaces [i] = mono_class_from_mono_type (inflated);
2473 i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, 1);
2476 guint32 cols [MONO_NESTED_CLASS_SIZE];
2477 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
2478 nclass = mono_class_create_from_typedef (klass->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
2479 klass->nested_classes = g_list_prepend (klass->nested_classes, nclass);
2481 i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, i + 1);
2485 klass->parent = mono_class_from_mono_type (gclass->parent);
2486 else if (gklass->parent) {
2487 MonoType *inflated = mono_class_inflate_generic_type (&gklass->parent->byval_arg, gclass->context);
2489 klass->parent = mono_class_from_mono_type (inflated);
2493 mono_class_setup_parent (klass, klass->parent);
2497 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
2499 MonoClass *klass, **ptr;
2503 return param->pklass;
2505 klass = param->pklass = g_new0 (MonoClass, 1);
2507 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
2511 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
2512 klass->parent = param->constraints [0];
2516 if (count - pos > 0) {
2517 klass->interface_count = count - pos;
2518 klass->interfaces = g_new0 (MonoClass *, count - pos);
2519 for (i = pos; i < count; i++)
2520 klass->interfaces [i - pos] = param->constraints [i];
2523 g_assert (param->name && param->owner);
2525 klass->name = param->name;
2526 klass->name_space = "";
2527 klass->image = image;
2528 klass->cast_class = klass->element_class = klass;
2529 klass->enum_basetype = &klass->element_class->byval_arg;
2530 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2532 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2533 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2534 klass->this_arg.byref = TRUE;
2536 mono_class_init (klass);
2542 my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
2547 return param->pklass;
2549 g_assert (param->owner);
2551 klass = g_new0 (MonoClass, 1);
2554 klass->name = param->name;
2556 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
2557 klass->name_space = "";
2558 klass->image = mono_defaults.corlib;
2559 klass->cast_class = klass->element_class = klass;
2560 klass->enum_basetype = &klass->element_class->byval_arg;
2561 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2563 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2564 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2565 klass->this_arg.byref = TRUE;
2567 mono_class_init (klass);
2573 mono_ptr_class_get (MonoType *type)
2576 MonoClass *el_class;
2577 static GHashTable *ptr_hash = NULL;
2579 mono_loader_lock ();
2582 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2583 el_class = mono_class_from_mono_type (type);
2584 if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
2585 mono_loader_unlock ();
2588 result = g_new0 (MonoClass, 1);
2590 result->parent = NULL; /* no parent for PTR types */
2591 result->name_space = el_class->name_space;
2592 result->name = g_strdup_printf ("%s*", el_class->name);
2593 result->image = el_class->image;
2594 result->inited = TRUE;
2595 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2596 /* Can pointers get boxed? */
2597 result->instance_size = sizeof (gpointer);
2598 result->cast_class = result->element_class = el_class;
2599 result->enum_basetype = &result->element_class->byval_arg;
2600 result->blittable = TRUE;
2602 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
2603 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
2604 result->this_arg.byref = TRUE;
2606 mono_class_setup_supertypes (result);
2608 g_hash_table_insert (ptr_hash, el_class, result);
2610 mono_loader_unlock ();
2616 mono_fnptr_class_get (MonoMethodSignature *sig)
2619 static GHashTable *ptr_hash = NULL;
2621 mono_loader_lock ();
2624 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2626 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
2627 mono_loader_unlock ();
2630 result = g_new0 (MonoClass, 1);
2632 result->parent = NULL; /* no parent for PTR types */
2633 result->name = "System";
2634 result->name_space = "MonoFNPtrFakeClass";
2635 result->image = NULL; /* need to fix... */
2636 result->inited = TRUE;
2637 result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
2638 /* Can pointers get boxed? */
2639 result->instance_size = sizeof (gpointer);
2640 result->cast_class = result->element_class = result;
2641 result->blittable = TRUE;
2643 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
2644 result->this_arg.data.method = result->byval_arg.data.method = sig;
2645 result->this_arg.byref = TRUE;
2646 result->enum_basetype = &result->element_class->byval_arg;
2647 result->blittable = TRUE;
2649 mono_class_setup_supertypes (result);
2651 g_hash_table_insert (ptr_hash, sig, result);
2653 mono_loader_unlock ();
2659 mono_class_from_mono_type (MonoType *type)
2661 switch (type->type) {
2662 case MONO_TYPE_OBJECT:
2663 return type->data.klass? type->data.klass: mono_defaults.object_class;
2664 case MONO_TYPE_VOID:
2665 return type->data.klass? type->data.klass: mono_defaults.void_class;
2666 case MONO_TYPE_BOOLEAN:
2667 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
2668 case MONO_TYPE_CHAR:
2669 return type->data.klass? type->data.klass: mono_defaults.char_class;
2671 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
2673 return type->data.klass? type->data.klass: mono_defaults.byte_class;
2675 return type->data.klass? type->data.klass: mono_defaults.int16_class;
2677 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
2679 return type->data.klass? type->data.klass: mono_defaults.int32_class;
2681 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
2683 return type->data.klass? type->data.klass: mono_defaults.int_class;
2685 return type->data.klass? type->data.klass: mono_defaults.uint_class;
2687 return type->data.klass? type->data.klass: mono_defaults.int64_class;
2689 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
2691 return type->data.klass? type->data.klass: mono_defaults.single_class;
2693 return type->data.klass? type->data.klass: mono_defaults.double_class;
2694 case MONO_TYPE_STRING:
2695 return type->data.klass? type->data.klass: mono_defaults.string_class;
2696 case MONO_TYPE_TYPEDBYREF:
2697 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
2698 case MONO_TYPE_ARRAY:
2699 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
2701 return mono_ptr_class_get (type->data.type);
2702 case MONO_TYPE_FNPTR:
2703 return mono_fnptr_class_get (type->data.method);
2704 case MONO_TYPE_SZARRAY:
2705 return mono_array_class_get (type->data.klass, 1);
2706 case MONO_TYPE_CLASS:
2707 case MONO_TYPE_VALUETYPE:
2708 return type->data.klass;
2709 case MONO_TYPE_GENERICINST:
2710 g_assert (type->data.generic_class->klass);
2711 return type->data.generic_class->klass;
2713 return my_mono_class_from_generic_parameter (type->data.generic_param, FALSE);
2714 case MONO_TYPE_MVAR:
2715 return my_mono_class_from_generic_parameter (type->data.generic_param, TRUE);
2717 g_warning ("implement me 0x%02x\n", type->type);
2718 g_assert_not_reached ();
2725 * @image: context where the image is created
2726 * @type_spec: typespec token
2729 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec,
2730 MonoGenericContext *context)
2732 MonoType *type, *inflated;
2735 type = mono_type_create_from_typespec_full (image, context, type_spec);
2737 switch (type->type) {
2738 case MONO_TYPE_ARRAY:
2739 class = mono_array_class_get (type->data.array->eklass, type->data.array->rank);
2741 case MONO_TYPE_SZARRAY:
2742 class = mono_array_class_get (type->data.klass, 1);
2745 class = mono_ptr_class_get (type->data.type);
2747 case MONO_TYPE_GENERICINST:
2748 g_assert (type->data.generic_class->klass);
2749 class = type->data.generic_class->klass;
2752 /* it seems any type can be stored in TypeSpec as well */
2753 class = mono_class_from_mono_type (type);
2757 if (!class || !context || (!context->gclass && !context->gmethod))
2760 inflated = mono_class_inflate_generic_type (&class->byval_arg, context);
2762 return mono_class_from_mono_type (inflated);
2766 * mono_bounded_array_class_get:
2767 * @element_class: element class
2768 * @rank: the dimension of the array class
2769 * @bounded: whenever the array has non-zero bounds
2771 * Returns: a class object describing the array with element type @element_type and
2775 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
2779 MonoClass *parent = NULL;
2780 GSList *list, *rootlist;
2783 gboolean corlib_type = FALSE;
2785 g_assert (rank <= 255);
2788 /* bounded only matters for one-dimensional arrays */
2791 image = eclass->image;
2793 mono_loader_lock ();
2795 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
2796 for (; list; list = list->next) {
2798 if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
2799 mono_loader_unlock ();
2805 /* for the building corlib use System.Array from it */
2806 if (image->assembly && image->assembly->dynamic && strcmp (image->assembly_name, "mscorlib") == 0) {
2807 parent = mono_class_from_name (image, "System", "Array");
2810 parent = mono_defaults.array_class;
2811 if (!parent->inited)
2812 mono_class_init (parent);
2815 class = g_malloc0 (sizeof (MonoClass));
2817 class->image = image;
2818 class->name_space = eclass->name_space;
2819 nsize = strlen (eclass->name);
2820 name = g_malloc (nsize + 2 + rank);
2821 memcpy (name, eclass->name, nsize);
2824 memset (name + nsize + 1, ',', rank - 1);
2825 name [nsize + rank] = ']';
2826 name [nsize + rank + 1] = 0;
2828 class->type_token = 0;
2829 /* all arrays are marked serializable and sealed, bug #42779 */
2830 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
2831 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2832 class->parent = parent;
2833 class->instance_size = mono_class_instance_size (class->parent);
2834 class->class_size = 0;
2835 mono_class_setup_supertypes (class);
2836 if (eclass->generic_class)
2837 mono_class_init (eclass);
2838 if (!eclass->size_inited)
2839 class_compute_field_layout (eclass);
2840 class->has_references = MONO_TYPE_IS_REFERENCE (&eclass->byval_arg) || eclass->has_references? TRUE: FALSE;
2844 if (eclass->enumtype)
2845 class->cast_class = eclass->element_class;
2847 class->cast_class = eclass;
2849 class->element_class = eclass;
2851 if ((rank > 1) || bounded) {
2852 MonoArrayType *at = g_new0 (MonoArrayType, 1);
2853 class->byval_arg.type = MONO_TYPE_ARRAY;
2854 class->byval_arg.data.array = at;
2855 at->eklass = eclass;
2857 /* FIXME: complete.... */
2859 class->byval_arg.type = MONO_TYPE_SZARRAY;
2860 class->byval_arg.data.klass = eclass;
2862 class->this_arg = class->byval_arg;
2863 class->this_arg.byref = 1;
2868 list = g_slist_append (rootlist, class);
2869 g_hash_table_insert (image->array_cache, eclass, list);
2871 mono_loader_unlock ();
2877 * mono_array_class_get:
2878 * @element_class: element class
2879 * @rank: the dimension of the array class
2881 * Returns: a class object describing the array with element type @element_type and
2885 mono_array_class_get (MonoClass *eclass, guint32 rank)
2887 return mono_bounded_array_class_get (eclass, rank, FALSE);
2891 * mono_class_instance_size:
2894 * Returns: the size of an object instance
2897 mono_class_instance_size (MonoClass *klass)
2899 if (!klass->size_inited)
2900 mono_class_init (klass);
2902 g_assert (!klass->generic_container &&
2903 (!klass->generic_class || !klass->generic_class->inst->is_open));
2904 return klass->instance_size;
2908 * mono_class_min_align:
2911 * Returns: minimm alignment requirements
2914 mono_class_min_align (MonoClass *klass)
2916 if (!klass->size_inited)
2917 mono_class_init (klass);
2919 return klass->min_align;
2923 * mono_class_value_size:
2926 * This function is used for value types, and return the
2927 * space and the alignment to store that kind of value object.
2929 * Returns: the size of a value of kind @klass
2932 mono_class_value_size (MonoClass *klass, guint32 *align)
2936 /* fixme: check disable, because we still have external revereces to
2937 * mscorlib and Dummy Objects
2939 /*g_assert (klass->valuetype);*/
2941 size = mono_class_instance_size (klass) - sizeof (MonoObject);
2944 *align = klass->min_align;
2950 * mono_class_data_size:
2953 * Returns: the size of the static class data
2956 mono_class_data_size (MonoClass *klass)
2959 mono_class_init (klass);
2961 return klass->class_size;
2965 * Auxiliary routine to mono_class_get_field
2967 * Takes a field index instead of a field token.
2969 static MonoClassField *
2970 mono_class_get_field_idx (MonoClass *class, int idx)
2972 if (class->field.count){
2973 if ((idx >= class->field.first) && (idx < class->field.last)){
2974 return &class->fields [idx - class->field.first];
2981 return mono_class_get_field_idx (class->parent, idx);
2985 * mono_class_get_field:
2986 * @class: the class to lookup the field.
2987 * @field_token: the field token
2989 * Returns: A MonoClassField representing the type and offset of
2990 * the field, or a NULL value if the field does not belong to this
2994 mono_class_get_field (MonoClass *class, guint32 field_token)
2996 int idx = mono_metadata_token_index (field_token);
2998 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
3000 return mono_class_get_field_idx (class, idx - 1);
3004 mono_class_get_field_from_name (MonoClass *klass, const char *name)
3009 for (i = 0; i < klass->field.count; ++i) {
3010 if (strcmp (name, klass->fields [i].name) == 0)
3011 return &klass->fields [i];
3013 klass = klass->parent;
3019 mono_class_get_field_token (MonoClassField *field)
3021 MonoClass *klass = field->parent;
3025 for (i = 0; i < klass->field.count; ++i) {
3026 if (&klass->fields [i] == field)
3027 return mono_metadata_make_token (MONO_TABLE_FIELD, klass->field.first + i + 1);
3029 klass = klass->parent;
3032 g_assert_not_reached ();
3037 mono_class_get_event_token (MonoEvent *event)
3039 MonoClass *klass = event->parent;
3043 for (i = 0; i < klass->event.count; ++i) {
3044 if (&klass->events [i] == event)
3045 return mono_metadata_make_token (MONO_TABLE_EVENT, klass->event.first + i + 1);
3047 klass = klass->parent;
3050 g_assert_not_reached ();
3055 mono_vtable_get_static_field_data (MonoVTable *vt)
3061 mono_class_get_property_from_name (MonoClass *klass, const char *name)
3065 gpointer iter = NULL;
3066 while ((p = mono_class_get_properties (klass, &iter))) {
3067 if (! strcmp (name, p->name))
3070 klass = klass->parent;
3076 mono_class_get_property_token (MonoProperty *prop)
3078 MonoClass *klass = prop->parent;
3082 gpointer iter = NULL;
3083 while ((p = mono_class_get_properties (klass, &iter))) {
3084 if (&klass->properties [i] == prop)
3085 return mono_metadata_make_token (MONO_TABLE_PROPERTY, klass->property.first + i + 1);
3089 klass = klass->parent;
3092 g_assert_not_reached ();
3097 mono_class_name_from_token (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3099 const char *name, *nspace;
3101 return g_strdup_printf ("DynamicType 0x%08x", type_token);
3103 switch (type_token & 0xff000000){
3104 case MONO_TOKEN_TYPE_DEF: {
3105 guint32 cols [MONO_TYPEDEF_SIZE];
3106 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
3107 guint tidx = mono_metadata_token_index (type_token);
3109 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
3110 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3111 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3112 if (strlen (nspace) == 0)
3113 return g_strdup_printf ("%s", name);
3115 return g_strdup_printf ("%s.%s", nspace, name);
3118 case MONO_TOKEN_TYPE_REF: {
3119 guint32 cols [MONO_TYPEREF_SIZE];
3120 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
3122 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
3123 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
3124 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
3125 if (strlen (nspace) == 0)
3126 return g_strdup_printf ("%s", name);
3128 return g_strdup_printf ("%s.%s", nspace, name);
3131 case MONO_TOKEN_TYPE_SPEC:
3132 return g_strdup_printf ("Typespec 0x%08x", type_token);
3134 g_assert_not_reached ();
3142 * @image: the image where the class resides
3143 * @type_token: the token for the class
3144 * @at: an optional pointer to return the array element type
3146 * Returns: the MonoClass that represents @type_token in @image
3149 _mono_class_get (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3151 MonoClass *class = NULL;
3154 return mono_lookup_dynamic_token (image, type_token);
3156 switch (type_token & 0xff000000){
3157 case MONO_TOKEN_TYPE_DEF:
3158 class = mono_class_create_from_typedef (image, type_token);
3160 case MONO_TOKEN_TYPE_REF:
3161 class = mono_class_from_typeref (image, type_token);
3163 case MONO_TOKEN_TYPE_SPEC:
3164 class = mono_class_create_from_typespec (image, type_token, context);
3167 g_warning ("unknown token type %x", type_token & 0xff000000);
3168 g_assert_not_reached ();
3172 char *name = mono_class_name_from_token (image, type_token, context);
3173 g_warning ("The class %s could not be loaded, used in %s (token 0x%08x)", name, image->name, type_token);
3181 mono_class_get (MonoImage *image, guint32 type_token)
3183 return _mono_class_get (image, type_token, NULL);
3187 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3189 MonoClass *class = _mono_class_get (image, type_token, context);
3192 if (!class || !context || (!context->gclass && !context->gmethod))
3195 switch (class->byval_arg.type) {
3196 case MONO_TYPE_GENERICINST:
3197 if (!class->generic_class->inst->is_open)
3201 case MONO_TYPE_MVAR:
3207 inflated = inflate_generic_type (&class->byval_arg, context);
3211 return mono_class_from_mono_type (inflated);
3215 * mono_class_from_name_case:
3216 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3217 * @name_space: the type namespace
3218 * @name: the type short name.
3220 * Obtains a MonoClass with a given namespace and a given name which
3221 * is located in the given MonoImage. The namespace and name
3222 * lookups are case insensitive.
3224 * You can also pass @NULL to the image, and that will lookup for
3225 * a type with the given namespace and name in all of the loaded
3226 * assemblies: notice that since there might be a name clash in this
3227 * case, passing @NULL is not encouraged if you need a precise type.
3231 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
3233 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
3234 guint32 cols [MONO_TYPEDEF_SIZE];
3239 /* add a cache if needed */
3240 for (i = 1; i <= t->rows; ++i) {
3241 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
3242 /* nested types are accessed from the nesting name */
3243 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
3244 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
3246 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3247 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3248 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
3249 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
3255 return_nested_in (MonoClass *class, char *nested) {
3257 char *s = strchr (nested, '/');
3264 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
3266 if (strcmp (found->name, nested) == 0) {
3268 return return_nested_in (found, s);
3277 * mono_class_from_name_case:
3278 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3279 * @name_space: the type namespace
3280 * @name: the type short name.
3282 * Obtains a MonoClass with a given namespace and a given name which
3283 * is located in the given MonoImage.
3285 * You can also pass `NULL' to the image, and that will lookup for
3286 * a type with the given namespace and name in all of the loaded
3287 * assemblies: notice that since there might be a name clash in this
3288 * case, passing NULL is not encouraged if you need a precise type.
3292 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
3294 GHashTable *nspace_table;
3295 MonoImage *loaded_image;
3301 if ((nested = strchr (name, '/'))) {
3302 int pos = nested - name;
3303 int len = strlen (name);
3306 memcpy (buf, name, len + 1);
3308 nested = buf + pos + 1;
3312 mono_loader_lock ();
3314 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
3317 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
3319 mono_loader_unlock ();
3324 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
3325 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
3326 guint32 cols [MONO_EXP_TYPE_SIZE];
3329 idx = mono_metadata_token_index (token);
3331 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
3333 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
3334 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
3335 loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
3338 class = mono_class_from_name (loaded_image, name_space, name);
3340 return return_nested_in (class, nested);
3343 g_error ("not yet implemented");
3347 token = MONO_TOKEN_TYPE_DEF | token;
3349 class = mono_class_get (image, token);
3351 return return_nested_in (class, nested);
3356 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
3357 gboolean check_interfaces)
3360 g_assert (klassc->idepth > 0);
3361 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
3362 if ((klassc->interface_id <= klass->max_interface_id) &&
3363 (klass->interface_offsets [klassc->interface_id] >= 0))
3365 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
3368 for (i = 0; i < klass->interface_count; i ++) {
3369 MonoClass *ic = klass->interfaces [i];
3374 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
3379 * MS.NET thinks interfaces are a subclass of Object, so we think it as
3382 if (klassc == mono_defaults.object_class)
3385 if (klass->generic_class) {
3386 MonoType *parent = klass->generic_class->parent;
3390 if (mono_metadata_type_equal (parent, &klassc->byval_arg))
3392 klass = mono_class_from_mono_type (parent);
3400 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
3403 mono_class_init (klass);
3405 if (!oklass->inited)
3406 mono_class_init (oklass);
3408 if (klass->generic_class)
3409 klass = klass->generic_class->container_class;
3410 if (oklass->generic_class)
3411 oklass = oklass->generic_class->container_class;
3413 if (MONO_CLASS_IS_INTERFACE (klass)) {
3414 if ((oklass->byval_arg.type == MONO_TYPE_VAR) || (oklass->byval_arg.type == MONO_TYPE_MVAR))
3417 /* interface_offsets might not be set for dynamic classes */
3418 if (oklass->reflection_info && !oklass->interface_offsets)
3420 * oklass might be a generic type parameter but they have
3421 * interface_offsets set.
3423 return mono_reflection_call_is_assignable_to (oklass, klass);
3425 if ((klass->interface_id <= oklass->max_interface_id) &&
3426 (oklass->interface_offsets [klass->interface_id] != -1))
3430 MonoClass *eclass, *eoclass;
3432 if (oklass->rank != klass->rank)
3435 /* vectors vs. one dimensional arrays */
3436 if (oklass->byval_arg.type != klass->byval_arg.type)
3439 eclass = klass->cast_class;
3440 eoclass = oklass->cast_class;
3444 * a is b does not imply a[] is b[] when a is a valuetype, and
3445 * b is a reference type.
3448 if (eoclass->valuetype) {
3449 if ((eclass == mono_defaults.enum_class) ||
3450 (eclass == mono_defaults.enum_class->parent) ||
3451 (eclass == mono_defaults.object_class))
3455 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
3458 if (klass == mono_defaults.object_class)
3462 * Custom version of mono_class_has_parent (oklass, klass)
3464 if (oklass->idepth >= klass->idepth) {
3465 MonoClass *parent = oklass->supertypes [klass->idepth - 1];
3467 if (parent->generic_class)
3468 parent = parent->generic_class->container_class;
3470 return klass == parent;
3477 * mono_class_get_cctor:
3479 * Returns the static constructor of @klass if it exists, NULL otherwise.
3482 mono_class_get_cctor (MonoClass *klass)
3484 MonoCachedClassInfo cached_info;
3486 if (!klass->has_cctor)
3489 if (mono_class_get_cached_class_info (klass, &cached_info))
3490 return mono_get_method (klass->image, cached_info.cctor_token, klass);
3492 return mono_class_get_method_from_name_flags (klass, ".cctor", -1, METHOD_ATTRIBUTE_SPECIAL_NAME);
3496 * mono_class_get_finalizer:
3498 * Returns the finalizer method of @klass if it exists, NULL otherwise.
3501 mono_class_get_finalizer (MonoClass *klass)
3503 MonoCachedClassInfo cached_info;
3504 static int finalize_slot = -1;
3506 if (finalize_slot < 0) {
3508 MonoClass* obj_class = mono_get_object_class ();
3509 mono_class_setup_vtable (obj_class);
3510 for (i = 0; i < obj_class->vtable_size; ++i) {
3511 MonoMethod *cm = obj_class->vtable [i];
3513 if (!strcmp (mono_method_get_name (cm), "Finalize")) {
3520 if (!klass->has_finalize)
3523 if (mono_class_get_cached_class_info (klass, &cached_info))
3524 return mono_get_method (cached_info.finalize_image, cached_info.finalize_token, NULL);
3526 mono_class_setup_vtable (klass);
3527 return klass->vtable [finalize_slot];
3532 * mono_class_needs_cctor_run:
3534 * Determines whenever the class has a static constructor and whenever it
3535 * needs to be called when executing CALLER.
3538 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
3542 method = mono_class_get_cctor (klass);
3544 return (method == caller) ? FALSE : TRUE;
3550 * mono_class_array_element_size:
3553 * Returns: the number of bytes an element of type @klass
3554 * uses when stored into an array.
3557 mono_class_array_element_size (MonoClass *klass)
3559 MonoType *type = &klass->byval_arg;
3562 switch (type->type) {
3565 case MONO_TYPE_BOOLEAN:
3569 case MONO_TYPE_CHAR:
3578 case MONO_TYPE_CLASS:
3579 case MONO_TYPE_STRING:
3580 case MONO_TYPE_OBJECT:
3581 case MONO_TYPE_SZARRAY:
3582 case MONO_TYPE_ARRAY:
3584 case MONO_TYPE_MVAR:
3585 return sizeof (gpointer);
3590 case MONO_TYPE_VALUETYPE:
3591 if (type->data.klass->enumtype) {
3592 type = type->data.klass->enum_basetype;
3593 klass = klass->element_class;
3596 return mono_class_instance_size (klass) - sizeof (MonoObject);
3597 case MONO_TYPE_GENERICINST:
3598 type = &type->data.generic_class->container_class->byval_arg;
3601 g_error ("unknown type 0x%02x in mono_class_array_element_size", type->type);
3607 * mono_array_element_size:
3608 * @ac: pointer to a #MonoArrayClass
3610 * Returns: the size of single array element.
3613 mono_array_element_size (MonoClass *ac)
3615 return mono_class_array_element_size (ac->element_class);
3619 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
3620 MonoGenericContext *context)
3622 if (image->dynamic) {
3623 MonoClass *tmp_handle_class;
3624 gpointer obj = mono_lookup_dynamic_token_class (image, token, &tmp_handle_class);
3626 g_assert (tmp_handle_class);
3628 *handle_class = tmp_handle_class;
3630 if (tmp_handle_class == mono_defaults.typehandle_class)
3631 return &((MonoClass*)obj)->byval_arg;
3636 switch (token & 0xff000000) {
3637 case MONO_TOKEN_TYPE_DEF:
3638 case MONO_TOKEN_TYPE_REF: {
3641 *handle_class = mono_defaults.typehandle_class;
3642 class = mono_class_get_full (image, token, context);
3643 mono_class_init (class);
3644 /* We return a MonoType* as handle */
3645 return &class->byval_arg;
3647 case MONO_TOKEN_TYPE_SPEC: {
3650 *handle_class = mono_defaults.typehandle_class;
3651 class = mono_class_create_from_typespec (image, token, context);
3652 mono_class_init (class);
3653 return &class->byval_arg;
3655 case MONO_TOKEN_FIELD_DEF: {
3657 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
3658 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
3659 mono_class_init (class);
3661 *handle_class = mono_defaults.fieldhandle_class;
3662 return mono_class_get_field (class, token);
3664 case MONO_TOKEN_METHOD_DEF: {
3666 meth = mono_get_method_full (image, token, NULL, context);
3668 *handle_class = mono_defaults.methodhandle_class;
3671 case MONO_TOKEN_MEMBER_REF: {
3672 guint32 cols [MONO_MEMBERREF_SIZE];
3674 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
3675 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
3676 mono_metadata_decode_blob_size (sig, &sig);
3677 if (*sig == 0x6) { /* it's a field */
3679 MonoClassField *field;
3680 field = mono_field_from_token (image, token, &klass, context);
3682 *handle_class = mono_defaults.fieldhandle_class;
3686 meth = mono_get_method_full (image, token, NULL, context);
3688 *handle_class = mono_defaults.methodhandle_class;
3693 g_warning ("Unknown token 0x%08x in ldtoken", token);
3700 * This function might need to call runtime functions so it can't be part
3701 * of the metadata library.
3703 static MonoLookupDynamicToken lookup_dynamic = NULL;
3706 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
3708 lookup_dynamic = func;
3712 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
3714 MonoClass *handle_class;
3716 return lookup_dynamic (image, token, &handle_class);
3720 mono_lookup_dynamic_token_class (MonoImage *image, guint32 token, MonoClass **handle_class)
3722 return lookup_dynamic (image, token, handle_class);
3725 static MonoGetCachedClassInfo get_cached_class_info = NULL;
3728 mono_install_get_cached_class_info (MonoGetCachedClassInfo func)
3730 get_cached_class_info = func;
3734 mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
3736 if (!get_cached_class_info)
3739 return get_cached_class_info (klass, res);
3743 mono_class_get_image (MonoClass *klass)
3745 return klass->image;
3749 * mono_class_get_element_class:
3750 * @klass: the MonoClass to act on
3752 * Returns: the element class of an array or an enumeration.
3755 mono_class_get_element_class (MonoClass *klass)
3757 return klass->element_class;
3761 * mono_class_is_valuetype:
3762 * @klass: the MonoClass to act on
3764 * Returns: true if the MonoClass represents a ValueType.
3767 mono_class_is_valuetype (MonoClass *klass)
3769 return klass->valuetype;
3773 * mono_class_is_enum:
3774 * @klass: the MonoClass to act on
3776 * Returns: true if the MonoClass represents an enumeration.
3779 mono_class_is_enum (MonoClass *klass)
3781 return klass->enumtype;
3785 * mono_class_enum_basetype:
3786 * @klass: the MonoClass to act on
3788 * Returns: the underlying type representation for an enumeration.
3791 mono_class_enum_basetype (MonoClass *klass)
3793 return klass->enum_basetype;
3797 * mono_class_get_parent
3798 * @klass: the MonoClass to act on
3800 * Returns: the parent class for this class.
3803 mono_class_get_parent (MonoClass *klass)
3805 return klass->parent;
3809 * mono_class_get_nesting_type;
3810 * @klass: the MonoClass to act on
3812 * Returns: the container type where this type is nested or NULL if this type is not a nested type.
3815 mono_class_get_nesting_type (MonoClass *klass)
3817 return klass->nested_in;
3821 * mono_class_get_rank:
3822 * @klass: the MonoClass to act on
3824 * Returns: the rank for the array (the number of dimensions).
3827 mono_class_get_rank (MonoClass *klass)
3833 * mono_class_get_flags:
3834 * @klass: the MonoClass to act on
3836 * The type flags from the TypeDef table from the metadata.
3837 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
3840 * Returns: the flags from the TypeDef table.
3843 mono_class_get_flags (MonoClass *klass)
3845 return klass->flags;
3849 * mono_class_get_name
3850 * @klass: the MonoClass to act on
3852 * Returns: the name of the class.
3855 mono_class_get_name (MonoClass *klass)
3861 * mono_class_get_namespace:
3862 * @klass: the MonoClass to act on
3864 * Returns: the namespace of the class.
3867 mono_class_get_namespace (MonoClass *klass)
3869 return klass->name_space;
3873 * mono_class_get_type:
3874 * @klass: the MonoClass to act on
3876 * This method returns the internal Type representation for the class.
3878 * Returns: the MonoType from the class.
3881 mono_class_get_type (MonoClass *klass)
3883 return &klass->byval_arg;
3887 * mono_class_get_byref_type:
3888 * @klass: the MonoClass to act on
3893 mono_class_get_byref_type (MonoClass *klass)
3895 return &klass->this_arg;
3899 * mono_class_num_fields:
3900 * @klass: the MonoClass to act on
3902 * Returns: the number of static and instance fields in the class.
3905 mono_class_num_fields (MonoClass *klass)
3907 return klass->field.count;
3911 * mono_class_num_methods:
3912 * @klass: the MonoClass to act on
3914 * Returns: the number of methods in the class.
3917 mono_class_num_methods (MonoClass *klass)
3919 return klass->method.count;
3923 * mono_class_num_properties
3924 * @klass: the MonoClass to act on
3926 * Returns: the number of properties in the class.
3929 mono_class_num_properties (MonoClass *klass)
3931 mono_class_setup_properties (klass);
3933 return klass->property.count;
3937 * mono_class_num_events:
3938 * @klass: the MonoClass to act on
3940 * Returns: the number of events in the class.
3943 mono_class_num_events (MonoClass *klass)
3945 mono_class_setup_events (klass);
3947 return klass->event.count;
3951 * mono_class_get_fields:
3952 * @klass: the MonoClass to act on
3954 * This routine is an iterator routine for retrieving the fields in a class.
3956 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3957 * iterate over all of the elements. When no more values are
3958 * available, the return value is NULL.
3960 * Returns: a @MonoClassField* on each iteration, or NULL when no more fields are available.
3963 mono_class_get_fields (MonoClass* klass, gpointer *iter)
3965 MonoClassField* field;
3969 mono_class_init (klass);
3971 /* start from the first */
3972 if (klass->field.count) {
3973 return *iter = &klass->fields [0];
3981 if (field < &klass->fields [klass->field.count]) {
3982 return *iter = field;
3988 * mono_class_get_methods
3989 * @klass: the MonoClass to act on
3991 * This routine is an iterator routine for retrieving the fields in a class.
3993 * You must pass a gpointer that points to zero and is treated as an opaque handle to
3994 * iterate over all of the elements. When no more values are
3995 * available, the return value is NULL.
3997 * Returns: a MonoMethod on each iteration or NULL when no more methods are available.
4000 mono_class_get_methods (MonoClass* klass, gpointer *iter)
4002 MonoMethod** method;
4006 mono_class_init (klass);
4008 mono_class_setup_methods (klass);
4009 /* start from the first */
4010 if (klass->method.count) {
4011 *iter = &klass->methods [0];
4012 return klass->methods [0];
4020 if (method < &klass->methods [klass->method.count]) {
4028 * mono_class_get_properties:
4029 * @klass: the MonoClass to act on
4031 * This routine is an iterator routine for retrieving the properties in a class.
4033 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4034 * iterate over all of the elements. When no more values are
4035 * available, the return value is NULL.
4037 * Returns: a @MonoProperty* on each invocation, or NULL when no more are available.
4040 mono_class_get_properties (MonoClass* klass, gpointer *iter)
4042 MonoProperty* property;
4046 mono_class_init (klass);
4048 mono_class_setup_properties (klass);
4049 /* start from the first */
4050 if (klass->property.count) {
4051 return *iter = &klass->properties [0];
4059 if (property < &klass->properties [klass->property.count]) {
4060 return *iter = property;
4066 * mono_class_get_events:
4067 * @klass: the MonoClass to act on
4069 * This routine is an iterator routine for retrieving the properties in a class.
4071 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4072 * iterate over all of the elements. When no more values are
4073 * available, the return value is NULL.
4075 * Returns: a @MonoEvent* on each invocation, or NULL when no more are available.
4078 mono_class_get_events (MonoClass* klass, gpointer *iter)
4084 mono_class_init (klass);
4086 mono_class_setup_events (klass);
4087 /* start from the first */
4088 if (klass->event.count) {
4089 return *iter = &klass->events [0];
4097 if (event < &klass->events [klass->event.count]) {
4098 return *iter = event;
4104 * mono_class_get_interfaces
4105 * @klass: the MonoClass to act on
4107 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
4109 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4110 * iterate over all of the elements. When no more values are
4111 * available, the return value is NULL.
4113 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
4116 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
4122 mono_class_init (klass);
4124 /* start from the first */
4125 if (klass->interface_count) {
4126 *iter = &klass->interfaces [0];
4127 return klass->interfaces [0];
4135 if (iface < &klass->interfaces [klass->interface_count]) {
4143 * mono_class_get_nested_types
4144 * @klass: the MonoClass to act on
4146 * This routine is an iterator routine for retrieving the nested types of a class.
4148 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4149 * iterate over all of the elements. When no more values are
4150 * available, the return value is NULL.
4152 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
4155 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
4161 mono_class_init (klass);
4163 /* start from the first */
4164 if (klass->nested_classes) {
4165 *iter = klass->nested_classes;
4166 return klass->nested_classes->data;
4168 /* no nested types */
4182 * mono_field_get_name:
4183 * @field: the MonoClassField to act on
4185 * Returns: the name of the field.
4188 mono_field_get_name (MonoClassField *field)
4194 * mono_field_get_type:
4195 * @field: the MonoClassField to act on
4197 * Returns: MonoType of the field.
4200 mono_field_get_type (MonoClassField *field)
4206 * mono_field_get_type:
4207 * @field: the MonoClassField to act on
4209 * Returns: MonoClass where the field was defined.
4212 mono_field_get_parent (MonoClassField *field)
4214 return field->parent;
4218 * mono_field_get_flags;
4219 * @field: the MonoClassField to act on
4221 * The metadata flags for a field are encoded using the
4222 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4224 * Returns: the flags for the field.
4227 mono_field_get_flags (MonoClassField *field)
4229 return field->type->attrs;
4233 * mono_property_get_name:
4234 * @prop: the MonoProperty to act on
4236 * Returns: the name of the property
4239 mono_property_get_name (MonoProperty *prop)
4245 * mono_property_get_set_method
4246 * @prop: the MonoProperty to act on.
4248 * Returns: the setter method of the property (A MonoMethod)
4251 mono_property_get_set_method (MonoProperty *prop)
4257 * mono_property_get_get_method
4258 * @prop: the MonoProperty to act on.
4260 * Returns: the setter method of the property (A MonoMethod)
4263 mono_property_get_get_method (MonoProperty *prop)
4269 * mono_property_get_parent:
4270 * @prop: the MonoProperty to act on.
4272 * Returns: the MonoClass where the property was defined.
4275 mono_property_get_parent (MonoProperty *prop)
4277 return prop->parent;
4281 * mono_property_get_flags:
4282 * @prop: the MonoProperty to act on.
4284 * The metadata flags for a property are encoded using the
4285 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4287 * Returns: the flags for the property.
4290 mono_property_get_flags (MonoProperty *prop)
4296 * mono_event_get_name:
4297 * @event: the MonoEvent to act on
4299 * Returns: the name of the event.
4302 mono_event_get_name (MonoEvent *event)
4308 * mono_event_get_add_method:
4309 * @event: The MonoEvent to act on.
4311 * Returns: the @add' method for the event (a MonoMethod).
4314 mono_event_get_add_method (MonoEvent *event)
4320 * mono_event_get_remove_method:
4321 * @event: The MonoEvent to act on.
4323 * Returns: the @remove method for the event (a MonoMethod).
4326 mono_event_get_remove_method (MonoEvent *event)
4328 return event->remove;
4332 * mono_event_get_raise_method:
4333 * @event: The MonoEvent to act on.
4335 * Returns: the @raise method for the event (a MonoMethod).
4338 mono_event_get_raise_method (MonoEvent *event)
4340 return event->raise;
4344 * mono_event_get_parent:
4345 * @event: the MonoEvent to act on.
4347 * Returns: the MonoClass where the event is defined.
4350 mono_event_get_parent (MonoEvent *event)
4352 return event->parent;
4356 * mono_event_get_flags
4357 * @event: the MonoEvent to act on.
4359 * The metadata flags for an event are encoded using the
4360 * EVENT_* constants. See the tabledefs.h file for details.
4362 * Returns: the flags for the event.
4365 mono_event_get_flags (MonoEvent *event)
4367 return event->attrs;
4371 * mono_class_get_method_from_name:
4372 * @klass: where to look for the method
4373 * @name_space: name of the method
4374 * @param_count: number of parameters. -1 for any number.
4376 * Obtains a MonoMethod with a given name and number of parameters.
4377 * It only works if there are no multiple signatures for any given method name.
4380 mono_class_get_method_from_name (MonoClass *klass, const char *name, int param_count)
4382 return mono_class_get_method_from_name_flags (klass, name, param_count, 0);
4386 * mono_class_get_method_from_name_flags:
4387 * @klass: where to look for the method
4388 * @name_space: name of the method
4389 * @param_count: number of parameters. -1 for any number.
4390 * @flags: flags which must be set in the method
4392 * Obtains a MonoMethod with a given name and number of parameters.
4393 * It only works if there are no multiple signatures for any given method name.
4396 mono_class_get_method_from_name_flags (MonoClass *klass, const char *name, int param_count, int flags)
4398 MonoMethod *res = NULL;
4401 mono_class_init (klass);
4403 if (klass->methods) {
4404 mono_class_setup_methods (klass);
4405 for (i = 0; i < klass->method.count; ++i) {
4406 MonoMethod *method = klass->methods [i];
4408 if (method->name[0] == name [0] &&
4409 !strcmp (name, method->name) &&
4410 (param_count == -1 || mono_method_signature (method)->param_count == param_count) &&
4411 ((method->flags & flags) == flags)) {
4418 /* Search directly in the metadata to avoid calling setup_methods () */
4419 for (i = 0; i < klass->method.count; ++i) {
4420 guint32 cols [MONO_METHOD_SIZE];
4423 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_METHOD], klass->method.first + i, cols, MONO_METHOD_SIZE);
4425 if (!strcmp (mono_metadata_string_heap (klass->image, cols [MONO_METHOD_NAME]), name)) {
4426 method = mono_get_method (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass);
4427 if ((param_count == -1) || mono_method_signature (method)->param_count == param_count) {
4439 * mono_class_set_failure:
4440 * @klass: class in which the failure was detected
4441 * @ex_type: the kind of exception/error to be thrown (later)
4442 * @ex_data: exception data (specific to each type of exception/error)
4444 * Keep a detected failure informations in the class for later processing.
4445 * Note that only the first failure is kept.
4448 mono_class_set_failure (MonoClass *klass, guint32 ex_type, void *ex_data)
4450 if (klass->exception_type)
4452 klass->exception_type = ex_type;
4453 klass->exception_data = ex_data;
4458 * mono_class_get_exception_for_failure:
4459 * @klass: class in which the failure was detected
4461 * Return a constructed MonoException than the caller can then throw
4462 * using mono_raise_exception - or NULL if no failure is present (or
4463 * doesn't result in an exception).
4466 mono_class_get_exception_for_failure (MonoClass *klass)
4468 switch (klass->exception_type) {
4469 case MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND: {
4470 MonoDomain *domain = mono_domain_get ();
4471 MonoSecurityManager* secman = mono_security_manager_get_methods ();
4472 MonoMethod *method = klass->exception_data;
4473 guint32 error = (method) ? MONO_METADATA_INHERITANCEDEMAND_CLASS : MONO_METADATA_INHERITANCEDEMAND_METHOD;
4474 MonoObject *exc = NULL;
4478 args [1] = mono_assembly_get_object (domain, mono_image_get_assembly (klass->image));
4479 args [2] = mono_type_get_object (domain, &klass->byval_arg);
4480 args [3] = (method) ? mono_method_get_object (domain, method, NULL) : NULL;
4482 mono_runtime_invoke (secman->inheritsecurityexception, NULL, args, &exc);
4483 return (MonoException*) exc;
4485 /* TODO - handle other class related failures */