2 * class.c: Class management for the Mono runtime
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
9 * Possible Optimizations:
10 * in mono_class_create, do not allocate the class right away,
11 * but wait until you know the size of the FieldMap, so that
12 * the class embeds directly the FieldMap after the vtable.
22 #include <mono/metadata/image.h>
23 #include <mono/metadata/assembly.h>
24 #include <mono/metadata/cil-coff.h>
25 #include <mono/metadata/metadata.h>
26 #include <mono/metadata/metadata-internals.h>
27 #include <mono/metadata/tabledefs.h>
28 #include <mono/metadata/tokentype.h>
29 #include <mono/metadata/class-internals.h>
30 #include <mono/metadata/object.h>
31 #include <mono/metadata/appdomain.h>
32 #include <mono/metadata/mono-endian.h>
33 #include <mono/metadata/debug-helpers.h>
34 #include <mono/metadata/reflection.h>
35 #include <mono/metadata/mono-debug-debugger.h>
36 #include <mono/metadata/security-manager.h>
37 #include <mono/os/gc_wrapper.h>
41 gboolean mono_print_vtable = FALSE;
43 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
44 static void mono_class_create_generic (MonoInflatedGenericClass *gclass);
45 static gboolean mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res);
47 void (*mono_debugger_start_class_init_func) (MonoClass *klass) = NULL;
48 void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
51 mono_class_from_typeref (MonoImage *image, guint32 type_token)
53 guint32 cols [MONO_TYPEREF_SIZE];
54 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
56 const char *name, *nspace;
58 MonoAssembly **references;
60 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
62 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
63 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
65 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
66 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
67 case MONO_RESOLTION_SCOPE_MODULE:
69 g_error ("null ResolutionScope not yet handled");
70 /* a typedef in disguise */
71 return mono_class_from_name (image, nspace, name);
72 case MONO_RESOLTION_SCOPE_MODULEREF:
73 return mono_class_from_name (image->modules [idx - 1], nspace, name);
74 case MONO_RESOLTION_SCOPE_TYPEREF: {
75 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
78 if (enclosing->inited) {
79 /* Micro-optimization: don't scan the metadata tables if enclosing is already inited */
80 for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
82 if (strcmp (res->name, name) == 0)
86 /* Don't call mono_class_init as we might've been called by it recursively */
87 int i = mono_metadata_nesting_typedef (enclosing->image, enclosing->type_token, 1);
89 guint32 class_nested = mono_metadata_decode_row_col (&enclosing->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, MONO_NESTED_CLASS_NESTED);
90 guint32 string_offset = mono_metadata_decode_row_col (&enclosing->image->tables [MONO_TABLE_TYPEDEF], class_nested - 1, MONO_TYPEDEF_NAME);
91 const char *nname = mono_metadata_string_heap (enclosing->image, string_offset);
93 if (strcmp (nname, name) == 0)
94 return mono_class_create_from_typedef (enclosing->image, MONO_TOKEN_TYPE_DEF | class_nested);
96 i = mono_metadata_nesting_typedef (enclosing->image, enclosing->type_token, i + 1);
99 g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
102 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
106 references = image->references;
107 if (!references [idx-1])
108 mono_assembly_load_reference (image, idx - 1);
109 if (references [idx - 1] == (gpointer)-1)
112 image = references [idx-1]->image;
114 return mono_class_from_name (image, nspace, name);
117 static inline MonoType*
118 dup_type (MonoType* t, const MonoType *original)
120 MonoType *r = g_new0 (MonoType, 1);
122 r->attrs = original->attrs;
123 r->byref = original->byref;
124 if (t->type == MONO_TYPE_PTR)
125 t->data.type = dup_type (t->data.type, original->data.type);
126 else if (t->type == MONO_TYPE_ARRAY)
127 t->data.array = mono_dup_array_type (t->data.array);
128 else if (t->type == MONO_TYPE_FNPTR)
129 t->data.method = mono_metadata_signature_deep_dup (t->data.method);
130 mono_stats.generics_metadata_size += sizeof (MonoType);
134 /* Copy everything mono_metadata_free_array free. */
136 mono_dup_array_type (MonoArrayType *a)
138 a = g_memdup (a, sizeof (MonoArrayType));
140 a->sizes = g_memdup (a->sizes, a->numsizes * sizeof (int));
142 a->lobounds = g_memdup (a->lobounds, a->numlobounds * sizeof (int));
146 /* Copy everything mono_metadata_free_method_signature free. */
148 mono_metadata_signature_deep_dup (MonoMethodSignature *sig)
152 sig = mono_metadata_signature_dup (sig);
154 sig->ret = dup_type (sig->ret, sig->ret);
155 for (i = 0; i < sig->param_count; ++i)
156 sig->params [i] = dup_type (sig->params [i], sig->params [i]);
162 _mono_type_get_assembly_name (MonoClass *klass, GString *str)
164 MonoAssembly *ta = klass->image->assembly;
166 g_string_append_printf (
167 str, ", %s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s",
169 ta->aname.major, ta->aname.minor, ta->aname.build, ta->aname.revision,
170 ta->aname.culture && *ta->aname.culture? ta->aname.culture: "neutral",
171 ta->aname.public_key_token [0] ? (char *)ta->aname.public_key_token : "null");
175 mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed,
176 MonoTypeNameFormat format)
180 switch (type->type) {
181 case MONO_TYPE_ARRAY: {
182 int i, rank = type->data.array->rank;
183 MonoTypeNameFormat nested_format;
185 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
186 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
188 mono_type_get_name_recurse (
189 &type->data.array->eklass->byval_arg, str, FALSE, nested_format);
190 g_string_append_c (str, '[');
191 for (i = 1; i < rank; i++)
192 g_string_append_c (str, ',');
193 g_string_append_c (str, ']');
194 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
195 _mono_type_get_assembly_name (type->data.array->eklass, str);
198 case MONO_TYPE_SZARRAY: {
199 MonoTypeNameFormat nested_format;
201 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
202 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
204 mono_type_get_name_recurse (
205 &type->data.klass->byval_arg, str, FALSE, nested_format);
206 g_string_append (str, "[]");
207 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
208 _mono_type_get_assembly_name (type->data.klass, str);
211 case MONO_TYPE_PTR: {
212 MonoTypeNameFormat nested_format;
214 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
215 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
217 mono_type_get_name_recurse (
218 type->data.type, str, FALSE, nested_format);
219 g_string_append (str, "*");
220 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
221 _mono_type_get_assembly_name (type->data.klass, str);
225 klass = mono_class_from_mono_type (type);
226 if (klass->nested_in) {
227 mono_type_get_name_recurse (
228 &klass->nested_in->byval_arg, str, TRUE, format);
229 if (format == MONO_TYPE_NAME_FORMAT_IL)
230 g_string_append_c (str, '.');
232 g_string_append_c (str, '+');
233 } else if (*klass->name_space) {
234 g_string_append (str, klass->name_space);
235 g_string_append_c (str, '.');
237 if (format == MONO_TYPE_NAME_FORMAT_IL) {
238 char *s = strchr (klass->name, '`');
239 int len = s ? s - klass->name : strlen (klass->name);
241 g_string_append_len (str, klass->name, len);
243 g_string_append (str, klass->name);
246 if (klass->generic_class) {
247 MonoGenericClass *gclass = klass->generic_class;
248 MonoTypeNameFormat nested_format;
251 nested_format = format == MONO_TYPE_NAME_FORMAT_FULL_NAME ?
252 MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED : format;
254 if (format == MONO_TYPE_NAME_FORMAT_IL)
255 g_string_append_c (str, '<');
257 g_string_append_c (str, '[');
258 for (i = 0; i < gclass->inst->type_argc; i++) {
259 MonoType *t = gclass->inst->type_argv [i];
262 g_string_append_c (str, ',');
263 if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
264 (t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
265 g_string_append_c (str, '[');
266 mono_type_get_name_recurse (
267 gclass->inst->type_argv [i], str, FALSE, nested_format);
268 if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
269 (t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
270 g_string_append_c (str, ']');
272 if (format == MONO_TYPE_NAME_FORMAT_IL)
273 g_string_append_c (str, '>');
275 g_string_append_c (str, ']');
276 } else if (klass->generic_container &&
277 (format != MONO_TYPE_NAME_FORMAT_FULL_NAME) &&
278 (format != MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)) {
281 if (format == MONO_TYPE_NAME_FORMAT_IL)
282 g_string_append_c (str, '<');
284 g_string_append_c (str, '[');
285 for (i = 0; i < klass->generic_container->type_argc; i++) {
287 g_string_append_c (str, ',');
288 g_string_append (str, klass->generic_container->type_params [i].name);
290 if (format == MONO_TYPE_NAME_FORMAT_IL)
291 g_string_append_c (str, '>');
293 g_string_append_c (str, ']');
295 if ((format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
296 (type->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
297 _mono_type_get_assembly_name (klass, str);
303 * mono_type_get_name:
306 * Returns: the string representation for type as required by System.Reflection.
307 * The inverse of mono_reflection_parse_type ().
310 mono_type_get_name_full (MonoType *type, MonoTypeNameFormat format)
314 if (format == MONO_TYPE_NAME_FORMAT_FULL_NAME &&
315 ((type->type == MONO_TYPE_VAR) || (type->type == MONO_TYPE_MVAR) ||
316 ((type->type == MONO_TYPE_GENERICINST) && type->data.generic_class->inst->is_open)))
319 result = g_string_new ("");
321 mono_type_get_name_recurse (type, result, FALSE, format);
324 g_string_append_c (result, '&');
326 return g_string_free (result, FALSE);
330 mono_type_get_name (MonoType *type)
332 return mono_type_get_name_full (type, MONO_TYPE_NAME_FORMAT_IL);
336 mono_type_get_underlying_type (MonoType *type)
338 switch (type->type) {
339 case MONO_TYPE_VALUETYPE:
340 if (type->data.klass->enumtype)
341 return type->data.klass->enum_basetype;
343 case MONO_TYPE_GENERICINST:
344 return mono_type_get_underlying_type (&type->data.generic_class->container_class->byval_arg);
353 mono_class_is_open_constructed_type (MonoType *t)
359 case MONO_TYPE_SZARRAY:
360 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
361 case MONO_TYPE_ARRAY:
362 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
364 return mono_class_is_open_constructed_type (t->data.type);
365 case MONO_TYPE_GENERICINST: {
366 MonoGenericClass *gclass = t->data.generic_class;
369 if (mono_class_is_open_constructed_type (&gclass->container_class->byval_arg))
371 for (i = 0; i < gclass->inst->type_argc; i++)
372 if (mono_class_is_open_constructed_type (gclass->inst->type_argv [i]))
381 static MonoGenericClass *
382 inflate_generic_class (MonoGenericClass *ogclass, MonoGenericContext *context)
384 MonoInflatedGenericClass *igclass;
385 MonoGenericClass *ngclass, *cached;
387 if (ogclass->is_dynamic) {
388 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
389 igclass = &dgclass->generic_class;
390 ngclass = &igclass->generic_class;
391 ngclass->is_inflated = 1;
392 ngclass->is_dynamic = 1;
394 igclass = g_new0 (MonoInflatedGenericClass, 1);
395 ngclass = &igclass->generic_class;
396 ngclass->is_inflated = 1;
401 ngclass->inst = mono_metadata_inflate_generic_inst (ogclass->inst, context);
403 igclass->klass = NULL;
405 ngclass->context = g_new0 (MonoGenericContext, 1);
406 ngclass->context->container = context->container;
407 ngclass->context->gclass = ngclass;
410 cached = mono_metadata_lookup_generic_class (ngclass);
411 mono_loader_unlock ();
413 g_free (ngclass->context);
422 inflate_generic_type (MonoType *type, MonoGenericContext *context)
424 switch (type->type) {
426 if (context->gmethod && context->gmethod->inst->type_argv)
428 context->gmethod->inst->type_argv [type->data.generic_param->num],
435 context->gclass->inst->type_argv [type->data.generic_param->num],
439 case MONO_TYPE_SZARRAY: {
440 MonoClass *eclass = type->data.klass;
441 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
444 nt = dup_type (type, type);
445 nt->data.klass = mono_class_from_mono_type (inflated);
448 case MONO_TYPE_ARRAY: {
449 MonoClass *eclass = type->data.array->eklass;
450 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
453 nt = dup_type (type, type);
454 nt->data.array = g_memdup (nt->data.array, sizeof (MonoArrayType));
455 nt->data.array->eklass = mono_class_from_mono_type (inflated);
458 case MONO_TYPE_GENERICINST: {
459 MonoGenericClass *ngclass = inflate_generic_class (type->data.generic_class, context);
460 MonoType *nt = dup_type (type, type);
461 nt->data.generic_class = ngclass;
464 case MONO_TYPE_CLASS:
465 case MONO_TYPE_VALUETYPE: {
466 MonoClass *klass = type->data.klass;
467 MonoGenericClass *gclass;
470 if (!klass->generic_container)
473 gclass = inflate_generic_class (klass->generic_container->context.gclass, context);
475 nt = dup_type (type, type);
476 nt->type = MONO_TYPE_GENERICINST;
477 nt->data.generic_class = gclass;
486 MonoInflatedGenericClass*
487 mono_get_inflated_generic_class (MonoGenericClass *gclass)
489 g_assert (gclass->is_inflated);
490 mono_class_create_generic ((MonoInflatedGenericClass *) gclass);
491 return (MonoInflatedGenericClass *) gclass;
495 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
497 MonoType *inflated = inflate_generic_type (type, context);
500 return dup_type (type, type);
502 mono_stats.inflated_type_count++;
507 mono_class_inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
509 MonoMethodSignature *res;
516 res = mono_metadata_signature_alloc (image, sig->param_count);
517 res->ret = mono_class_inflate_generic_type (sig->ret, context);
518 is_open = mono_class_is_open_constructed_type (res->ret);
519 for (i = 0; i < sig->param_count; ++i) {
520 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
522 is_open = mono_class_is_open_constructed_type (res->params [i]);
524 res->hasthis = sig->hasthis;
525 res->explicit_this = sig->explicit_this;
526 res->call_convention = sig->call_convention;
527 res->pinvoke = sig->pinvoke;
528 res->generic_param_count = sig->generic_param_count;
529 res->sentinelpos = sig->sentinelpos;
530 res->has_type_parameters = is_open;
531 res->is_inflated = 1;
535 static MonoMethodHeader*
536 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
538 MonoMethodHeader *res;
540 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
541 res->code = header->code;
542 res->code_size = header->code_size;
543 res->max_stack = header->max_stack;
544 res->num_clauses = header->num_clauses;
545 res->init_locals = header->init_locals;
546 res->num_locals = header->num_locals;
547 res->clauses = header->clauses;
548 for (i = 0; i < header->num_locals; ++i)
549 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
553 static MonoGenericContext *
554 inflate_generic_context (MonoGenericContext *context, MonoGenericContext *inflate_with)
556 MonoGenericContext *res = g_new0 (MonoGenericContext, 1);
558 res->container = context->container;
561 res->gclass = inflate_generic_class (context->gclass, inflate_with);
563 if (context->gmethod) {
564 res->gmethod = g_new0 (MonoGenericMethod, 1);
565 res->gmethod->generic_class = res->gclass;
567 res->gmethod->container = context->gmethod->container;
568 res->gmethod->inst = mono_metadata_inflate_generic_inst (context->gmethod->inst, inflate_with);
570 res->gmethod = inflate_with->gmethod;
576 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context)
578 MonoMethodInflated *result;
580 if (method->is_inflated || mono_method_signature_full (method, context)->is_inflated) {
581 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
583 context = inflate_generic_context (imethod->context, context);
584 method = imethod->declaring;
587 mono_stats.inflated_method_count++;
589 result = g_new0 (MonoMethodInflated, 1);
590 result->nmethod = *(MonoMethodNormal*)method;
591 result->nmethod.method.is_inflated = 1;
592 result->context = context;
593 result->declaring = method;
595 if (result->nmethod.method.klass->generic_class)
596 result->nmethod.method.klass = result->nmethod.method.klass->generic_class->container_class;
598 return (MonoMethod *) result;
602 mono_get_inflated_method (MonoMethod *method)
604 MonoMethodInflated *imethod, *res;
605 MonoMethodHeader *mh;
609 if (!method->is_inflated)
612 imethod = (MonoMethodInflated *) method;
613 if (imethod->inflated)
614 return (MonoMethod *) imethod->inflated;
616 mono_stats.inflated_method_count_2++;
617 mono_stats.generics_metadata_size +=
618 sizeof (MonoMethodInflated) - sizeof (MonoMethodNormal);
620 res = g_new0 (MonoMethodInflated, 1);
622 res->inflated = imethod->inflated = res;
624 mh = mono_method_get_header (method);
626 res->nmethod.header = inflate_generic_header (mh, imethod->context);
628 dtype = mono_class_inflate_generic_type (&method->klass->byval_arg, imethod->context);
629 rklass = res->nmethod.method.klass = mono_class_from_mono_type (dtype);
631 res->nmethod.method.signature = mono_class_inflate_generic_signature (
632 method->klass->image, mono_method_signature (method), imethod->context);
634 return (MonoMethod *) res;
638 * mono_class_setup_fields:
639 * @m: pointer to the metadata.
640 * @class: The class to initialize
642 * Initializes the class->fields.
645 mono_class_setup_fields (MonoClass *class)
647 MonoImage *m = class->image;
648 const int top = class->field.count;
649 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
650 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
651 int i, blittable = TRUE, real_size = 0;
653 guint32 packing_size = 0;
654 gboolean explicit_size;
655 MonoClassField *field;
657 if (class->size_inited)
660 class->instance_size = 0;
661 class->class_size = 0;
664 if (!class->parent->size_inited)
665 mono_class_setup_fields (class->parent);
666 class->instance_size += class->parent->instance_size;
667 class->min_align = class->parent->min_align;
668 /* we use |= since it may have been set already */
669 class->has_references |= class->parent->has_references;
670 blittable = class->parent->blittable;
672 class->instance_size = sizeof (MonoObject);
673 class->min_align = 1;
676 /* Get the real size */
677 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
680 g_assert ((packing_size & 0xfffffff0) == 0);
681 class->packing_size = packing_size;
682 real_size += class->instance_size;
686 if (explicit_size && real_size) {
687 class->instance_size = MAX (real_size, class->instance_size);
689 class->size_inited = 1;
690 class->blittable = blittable;
694 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT)
697 /* Prevent infinite loops if the class references itself */
698 class->size_inited = 1;
700 class->fields = g_new0 (MonoClassField, top);
703 * Fetch all the field information.
705 for (i = 0; i < top; i++){
707 guint32 cols [MONO_FIELD_SIZE];
708 int idx = class->field.first + i;
709 MonoGenericContainer *container = NULL;
711 field = &class->fields [i];
712 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
713 /* The name is needed for fieldrefs */
714 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
715 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
716 mono_metadata_decode_value (sig, &sig);
717 /* FIELD signature == 0x06 */
718 g_assert (*sig == 0x06);
719 if (class->generic_container)
720 container = class->generic_container;
721 else if (class->generic_class) {
722 MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
723 MonoClass *gklass = class->generic_class->container_class;
725 container = gklass->generic_container;
726 g_assert (container);
728 ifield->generic_type = gklass->fields [i].type;
729 field->generic_info = ifield;
731 field->type = mono_metadata_parse_type_full (
732 m, (MonoGenericContext *) container, MONO_PARSE_FIELD,
733 cols [MONO_FIELD_FLAGS], sig + 1, &sig);
734 if (mono_field_is_deleted (field))
736 if (class->generic_class) {
737 field->type = mono_class_inflate_generic_type (
738 field->type, class->generic_class->context);
739 field->type->attrs = cols [MONO_FIELD_FLAGS];
742 field->parent = class;
744 /* Only do these checks if we still think this type is blittable */
745 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
746 if (field->type->byref || MONO_TYPE_IS_REFERENCE (field->type)) {
749 MonoClass *field_class = mono_class_from_mono_type (field->type);
750 if (!field_class || !field_class->blittable)
755 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
756 mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
757 if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
758 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, field->name);
761 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
762 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
764 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
765 field->data = mono_image_rva_map (class->image, rva);
768 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
769 class->enum_basetype = field->type;
770 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
771 blittable = class->element_class->blittable;
774 /* The def_value of fields is compute lazily during vtable creation */
777 if (class == mono_defaults.string_class)
780 class->blittable = blittable;
782 if (class->enumtype && !class->enum_basetype) {
783 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
786 if (explicit_size && real_size) {
787 class->instance_size = MAX (real_size, class->instance_size);
790 mono_class_layout_fields (class);
793 /* useful until we keep track of gc-references in corlib etc. */
794 #define IS_GC_REFERENCE(t) ((t)->type == MONO_TYPE_U || (t)->type == MONO_TYPE_I || (t)->type == MONO_TYPE_PTR)
797 * LOCKING: this is supposed to be called with the loader lock held.
800 mono_class_layout_fields (MonoClass *class)
803 const int top = class->field.count;
804 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
805 guint32 pass, passes, real_size;
806 gboolean gc_aware_layout = FALSE;
807 MonoClassField *field;
809 if (class->generic_container ||
810 (class->generic_class && class->generic_class->inst->is_open))
814 * Enable GC aware auto layout: in this mode, reference
815 * fields are grouped together inside objects, increasing collector
817 * Requires that all classes whose layout is known to native code be annotated
818 * with [StructLayout (LayoutKind.Sequential)]
819 * Value types have gc_aware_layout disabled by default, as per
820 * what the default is for other runtimes.
822 /* corlib is missing [StructLayout] directives in many places */
823 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
824 if (class->image != mono_defaults.corlib &&
825 class->byval_arg.type != MONO_TYPE_VALUETYPE)
826 gc_aware_layout = TRUE;
829 for (i = 0; i < top; i++) {
832 field = &class->fields [i];
834 ftype = mono_type_get_underlying_type (field->type);
835 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_from_mono_type (ftype)->has_references))) {
836 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
837 class->has_static_refs = TRUE;
839 class->has_references = TRUE;
844 * Compute field layout and total size (not considering static fields)
848 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
849 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
856 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
860 real_size = class->parent->instance_size;
862 real_size = sizeof (MonoObject);
864 for (pass = 0; pass < passes; ++pass) {
865 for (i = 0; i < top; i++){
868 field = &class->fields [i];
870 if (mono_field_is_deleted (field))
872 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
875 if (gc_aware_layout) {
877 * We process fields with reference type in the first pass,
878 * and fields with non-reference type in the second pass.
879 * We use IS_POINTER instead of IS_REFERENCE because in
880 * some internal structures, we store GC_MALLOCed memory
881 * in IntPtr fields...
883 if (MONO_TYPE_IS_POINTER (field->type)) {
892 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
893 (strcmp (field->name, "$PRIVATE$") == 0)) {
894 /* This field is a hack inserted by MCS to empty structures */
898 size = mono_type_size (field->type, &align);
900 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
901 align = class->packing_size ? MIN (class->packing_size, align): align;
902 class->min_align = MAX (align, class->min_align);
903 field->offset = real_size;
904 field->offset += align - 1;
905 field->offset &= ~(align - 1);
906 real_size = field->offset + size;
909 class->instance_size = MAX (real_size, class->instance_size);
911 if (class->instance_size & (class->min_align - 1)) {
912 class->instance_size += class->min_align - 1;
913 class->instance_size &= ~(class->min_align - 1);
917 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
919 for (i = 0; i < top; i++) {
922 field = &class->fields [i];
925 * There must be info about all the fields in a type if it
926 * uses explicit layout.
929 if (mono_field_is_deleted (field))
931 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
934 size = mono_type_size (field->type, &align);
937 * When we get here, field->offset is already set by the
938 * loader (for either runtime fields or fields loaded from metadata).
939 * The offset is from the start of the object: this works for both
940 * classes and valuetypes.
942 field->offset += sizeof (MonoObject);
947 real_size = MAX (real_size, size + field->offset);
949 class->instance_size = MAX (real_size, class->instance_size);
953 class->size_inited = 1;
956 * Compute static field layout and size
958 for (i = 0; i < top; i++){
961 field = &class->fields [i];
963 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
965 if (mono_field_is_deleted (field))
968 size = mono_type_size (field->type, &align);
969 field->offset = class->class_size;
970 field->offset += align - 1;
971 field->offset &= ~(align - 1);
972 class->class_size = field->offset + size;
977 * mono_class_setup_methods:
979 * Initializes the 'methods' array in the klass.
980 * Calling this method should be avoided if possible since it allocates a lot
981 * of long-living MonoMethod structures.
984 mono_class_setup_methods (MonoClass *class)
987 MonoMethod **methods;
989 if (class->methods || class->generic_class)
994 if (class->methods) {
995 mono_loader_unlock ();
999 //printf ("INIT: %s.%s\n", class->name_space, class->name);
1001 if (!class->generic_class && !class->methods) {
1002 methods = g_new (MonoMethod*, class->method.count);
1003 for (i = 0; i < class->method.count; ++i) {
1004 methods [i] = mono_get_method (class->image,
1005 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
1009 if (MONO_CLASS_IS_INTERFACE (class))
1010 for (i = 0; i < class->method.count; ++i)
1011 methods [i]->slot = i;
1013 class->methods = methods;
1015 mono_loader_unlock ();
1020 mono_class_setup_properties (MonoClass *class)
1022 guint startm, endm, i, j;
1023 guint32 cols [MONO_PROPERTY_SIZE];
1024 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
1025 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
1026 MonoProperty *properties;
1028 if (class->properties)
1031 mono_loader_lock ();
1033 if (class->properties) {
1034 mono_loader_unlock ();
1038 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
1039 class->property.count = class->property.last - class->property.first;
1041 if (class->property.count)
1042 mono_class_setup_methods (class);
1044 properties = g_new0 (MonoProperty, class->property.count);
1045 for (i = class->property.first; i < class->property.last; ++i) {
1046 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
1047 properties [i - class->property.first].parent = class;
1048 properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
1049 properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
1051 startm = mono_metadata_methods_from_property (class->image, i, &endm);
1052 for (j = startm; j < endm; ++j) {
1053 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
1054 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1055 case METHOD_SEMANTIC_SETTER:
1056 properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1058 case METHOD_SEMANTIC_GETTER:
1059 properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1067 class->properties = properties;
1069 mono_loader_unlock ();
1073 mono_class_setup_events (MonoClass *class)
1075 guint startm, endm, i, j;
1076 guint32 cols [MONO_EVENT_SIZE];
1077 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
1078 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
1083 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
1084 class->event.count = class->event.last - class->event.first;
1086 if (class->event.count)
1087 mono_class_setup_methods (class);
1089 class->events = g_new0 (MonoEvent, class->event.count);
1090 for (i = class->event.first; i < class->event.last; ++i) {
1091 MonoEvent *event = &class->events [i - class->event.first];
1093 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
1094 event->parent = class;
1095 event->attrs = cols [MONO_EVENT_FLAGS];
1096 event->name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
1098 startm = mono_metadata_methods_from_event (class->image, i, &endm);
1099 for (j = startm; j < endm; ++j) {
1100 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
1101 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1102 case METHOD_SEMANTIC_ADD_ON:
1103 event->add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1105 case METHOD_SEMANTIC_REMOVE_ON:
1106 event->remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1108 case METHOD_SEMANTIC_FIRE:
1109 event->raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1111 case METHOD_SEMANTIC_OTHER: {
1114 if (event->other == NULL) {
1115 event->other = g_new0 (MonoMethod*, 1);
1117 while (event->other [n])
1119 event->other = g_realloc (event->other, (n + 1) * sizeof (MonoMethod*));
1121 event->other [n] = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1132 mono_get_unique_iid (MonoClass *class)
1134 static GHashTable *iid_hash = NULL;
1135 static guint iid = 0;
1140 g_assert (MONO_CLASS_IS_INTERFACE (class));
1142 mono_loader_lock ();
1145 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
1147 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
1149 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
1150 mono_loader_unlock ();
1152 return GPOINTER_TO_INT (value);
1154 g_hash_table_insert (iid_hash, str, GINT_TO_POINTER (iid));
1158 mono_loader_unlock ();
1160 g_assert (iid <= 65535);
1165 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
1170 for (i = 0; i < klass->interface_count; i++) {
1171 ic = klass->interfaces [i];
1174 *res = g_ptr_array_new ();
1175 g_ptr_array_add (*res, ic);
1176 mono_class_init (ic);
1178 collect_implemented_interfaces_aux (ic, res);
1183 mono_class_get_implemented_interfaces (MonoClass *klass)
1185 GPtrArray *res = NULL;
1187 collect_implemented_interfaces_aux (klass, &res);
1191 typedef struct _IOffsetInfo IOffsetInfo;
1192 struct _IOffsetInfo {
1196 int data [MONO_ZERO_LEN_ARRAY];
1199 static IOffsetInfo *cached_offset_info = NULL;
1200 static int next_offset_info_size = 128;
1203 cache_interface_offsets (int max_iid, int *data)
1205 IOffsetInfo *cached_info;
1208 for (cached_info = cached_offset_info; cached_info; cached_info = cached_info->next) {
1209 cached = cached_info->data;
1210 while (cached < cached_info->data + cached_info->size && *cached) {
1211 if (*cached == max_iid) {
1212 int i, matched = TRUE;
1214 for (i = 0; i < max_iid; ++i) {
1215 if (cached [i] != data [i]) {
1224 cached += *cached + 1;
1228 /* find a free slot */
1229 for (cached_info = cached_offset_info; cached_info; cached_info = cached_info->next) {
1230 if (cached_info->size - cached_info->next_free >= max_iid + 1) {
1231 cached = &cached_info->data [cached_info->next_free];
1232 *cached++ = max_iid;
1233 memcpy (cached, data, max_iid * sizeof (int));
1234 cached_info->next_free += max_iid + 1;
1238 /* allocate a new chunk */
1239 if (max_iid + 1 < next_offset_info_size) {
1240 new_size = next_offset_info_size;
1241 if (next_offset_info_size < 4096)
1242 next_offset_info_size += next_offset_info_size >> 2;
1244 new_size = max_iid + 1;
1246 cached_info = g_malloc0 (sizeof (IOffsetInfo) + sizeof (int) * new_size);
1247 cached_info->size = new_size;
1248 /*g_print ("allocated %d offset entries at %p (total: %d)\n", new_size, cached_info->data, offset_info_total_size);*/
1249 cached = &cached_info->data [0];
1250 *cached++ = max_iid;
1251 memcpy (cached, data, max_iid * sizeof (int));
1252 cached_info->next_free += max_iid + 1;
1253 cached_info->next = cached_offset_info;
1254 cached_offset_info = cached_info;
1259 setup_interface_offsets (MonoClass *class, int cur_slot)
1266 /* compute maximum number of slots and maximum interface id */
1268 for (k = class; k ; k = k->parent) {
1269 for (i = 0; i < k->interface_count; i++) {
1270 ic = k->interfaces [i];
1273 mono_class_init (ic);
1275 if (max_iid < ic->interface_id)
1276 max_iid = ic->interface_id;
1278 ifaces = mono_class_get_implemented_interfaces (k);
1280 for (i = 0; i < ifaces->len; ++i) {
1281 ic = g_ptr_array_index (ifaces, i);
1282 if (max_iid < ic->interface_id)
1283 max_iid = ic->interface_id;
1285 g_ptr_array_free (ifaces, TRUE);
1289 if (MONO_CLASS_IS_INTERFACE (class)) {
1290 if (max_iid < class->interface_id)
1291 max_iid = class->interface_id;
1293 class->max_interface_id = max_iid;
1294 /* compute vtable offset for interfaces */
1295 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
1297 for (i = 0; i <= max_iid; i++)
1298 class->interface_offsets [i] = -1;
1300 ifaces = mono_class_get_implemented_interfaces (class);
1302 for (i = 0; i < ifaces->len; ++i) {
1303 ic = g_ptr_array_index (ifaces, i);
1304 class->interface_offsets [ic->interface_id] = cur_slot;
1305 cur_slot += ic->method.count;
1307 g_ptr_array_free (ifaces, TRUE);
1310 for (k = class->parent; k ; k = k->parent) {
1311 ifaces = mono_class_get_implemented_interfaces (k);
1313 for (i = 0; i < ifaces->len; ++i) {
1314 ic = g_ptr_array_index (ifaces, i);
1316 if (class->interface_offsets [ic->interface_id] == -1) {
1317 int io = k->interface_offsets [ic->interface_id];
1321 class->interface_offsets [ic->interface_id] = io;
1324 g_ptr_array_free (ifaces, TRUE);
1328 if (MONO_CLASS_IS_INTERFACE (class))
1329 class->interface_offsets [class->interface_id] = cur_slot;
1331 cached_data = cache_interface_offsets (max_iid + 1, class->interface_offsets);
1332 g_free (class->interface_offsets);
1333 class->interface_offsets = cached_data;
1339 mono_class_setup_vtable (MonoClass *class)
1341 MonoMethod **overrides;
1342 MonoGenericContext *context;
1348 mono_class_setup_methods (class);
1350 if (MONO_CLASS_IS_INTERFACE (class))
1353 mono_loader_lock ();
1355 if (class->vtable) {
1356 mono_loader_unlock ();
1360 if (class->generic_class)
1361 context = class->generic_class->context;
1363 context = (MonoGenericContext *) class->generic_container;
1365 overrides = mono_class_get_overrides_full (
1366 class->image, class->type_token, &onum, context);
1367 mono_class_setup_vtable_general (class, overrides, onum);
1370 mono_loader_unlock ();
1374 setup_generic_vtable (MonoClass *class, MonoMethod **overrides, int onum)
1379 gklass = class->generic_class->container_class;
1381 mono_class_init (gklass);
1382 class->vtable_size = gklass->vtable_size;
1384 class->vtable = g_new0 (MonoMethod*, class->vtable_size);
1385 memcpy (class->vtable, gklass->vtable, sizeof (MonoMethod*) * class->vtable_size);
1387 for (i = 0; i < class->vtable_size; i++) {
1388 MonoMethod *m = class->vtable [i];
1393 m = mono_class_inflate_generic_method (m, class->generic_class->context);
1394 class->vtable [i] = m;
1397 class->max_interface_id = gklass->max_interface_id;
1398 class->interface_offsets = g_new0 (gint, gklass->max_interface_id + 1);
1399 memcpy (class->interface_offsets, gklass->interface_offsets,
1400 sizeof (gint) * (gklass->max_interface_id + 1));
1404 * LOCKING: this is supposed to be called with the loader lock held.
1407 mono_class_setup_vtable_general (MonoClass *class, MonoMethod **overrides, int onum)
1410 MonoMethod **vtable;
1411 int i, max_vtsize = 0, max_iid, cur_slot = 0;
1413 GHashTable *override_map = NULL;
1414 gboolean security_enabled = mono_is_security_manager_active ();
1419 if (class->generic_class) {
1420 setup_generic_vtable (class, overrides, onum);
1424 ifaces = mono_class_get_implemented_interfaces (class);
1426 for (i = 0; i < ifaces->len; i++) {
1427 MonoClass *ic = g_ptr_array_index (ifaces, i);
1428 max_vtsize += ic->method.count;
1430 g_ptr_array_free (ifaces, TRUE);
1433 if (class->parent) {
1434 mono_class_init (class->parent);
1435 mono_class_setup_vtable (class->parent);
1436 max_vtsize += class->parent->vtable_size;
1437 cur_slot = class->parent->vtable_size;
1440 max_vtsize += class->method.count;
1442 vtable = alloca (sizeof (gpointer) * max_vtsize);
1443 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
1445 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
1447 cur_slot = setup_interface_offsets (class, cur_slot);
1448 max_iid = class->max_interface_id;
1450 if (class->parent && class->parent->vtable_size)
1451 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
1453 /* override interface methods */
1454 for (i = 0; i < onum; i++) {
1455 MonoMethod *decl = overrides [i*2];
1456 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
1458 mono_class_setup_methods (decl->klass);
1459 g_assert (decl->slot != -1);
1460 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
1461 vtable [dslot] = overrides [i*2 + 1];
1462 vtable [dslot]->slot = dslot;
1464 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1466 g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
1470 for (k = class; k ; k = k->parent) {
1473 ifaces = mono_class_get_implemented_interfaces (k);
1475 nifaces = ifaces->len;
1476 for (i = 0; i < nifaces; i++) {
1479 ic = g_ptr_array_index (ifaces, i);
1480 io = k->interface_offsets [ic->interface_id];
1483 g_assert (io <= max_vtsize);
1486 mono_class_setup_methods (ic);
1487 for (l = 0; l < ic->method.count; l++) {
1488 MonoMethod *im = ic->methods [l];
1490 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1493 for (j = 0; j < class->method.count; ++j) {
1494 MonoMethod *cm = class->methods [j];
1495 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1496 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
1497 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
1499 if (!strcmp(cm->name, im->name) &&
1500 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1502 /* CAS - SecurityAction.InheritanceDemand on interface */
1503 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1504 mono_secman_inheritancedemand_method (cm, im);
1507 g_assert (io + l <= max_vtsize);
1508 vtable [io + l] = cm;
1513 /* already implemented */
1514 if (io >= k->vtable_size)
1518 for (l = 0; l < ic->method.count; l++) {
1519 MonoMethod *im = ic->methods [l];
1522 g_assert (io + l <= max_vtsize);
1524 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1527 for (k1 = class; k1; k1 = k1->parent) {
1528 for (j = 0; j < k1->method.count; ++j) {
1529 MonoMethod *cm = k1->methods [j];
1531 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1532 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
1535 if (!strcmp(cm->name, im->name) &&
1536 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1538 /* CAS - SecurityAction.InheritanceDemand on interface */
1539 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1540 mono_secman_inheritancedemand_method (cm, im);
1543 g_assert (io + l <= max_vtsize);
1544 vtable [io + l] = cm;
1549 g_assert (io + l <= max_vtsize);
1550 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1555 for (l = 0; l < ic->method.count; l++) {
1556 MonoMethod *im = ic->methods [l];
1557 char *qname, *fqname, *cname, *the_cname;
1560 if (vtable [io + l])
1563 if (ic->generic_class) {
1564 the_cname = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
1568 cname = (char*)ic->name;
1571 qname = g_strconcat (cname, ".", im->name, NULL);
1572 if (ic->name_space && ic->name_space [0])
1573 fqname = g_strconcat (ic->name_space, ".", cname, ".", im->name, NULL);
1577 for (k1 = class; k1; k1 = k1->parent) {
1578 for (j = 0; j < k1->method.count; ++j) {
1579 MonoMethod *cm = k1->methods [j];
1581 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1584 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
1585 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1587 /* CAS - SecurityAction.InheritanceDemand on interface */
1588 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1589 mono_secman_inheritancedemand_method (cm, im);
1592 g_assert (io + l <= max_vtsize);
1593 vtable [io + l] = cm;
1604 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
1605 for (l = 0; l < ic->method.count; l++) {
1607 MonoMethod *im = ic->methods [l];
1608 if (im->flags & METHOD_ATTRIBUTE_STATIC)
1610 g_assert (io + l <= max_vtsize);
1613 * If one of our parents already implements this interface
1614 * we can inherit the implementation.
1616 if (!(vtable [io + l])) {
1617 MonoClass *parent = class->parent;
1619 if ((ic->interface_id <= parent->max_interface_id) &&
1620 (parent->interface_offsets [ic->interface_id]) &&
1622 vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
1625 if (!(vtable [io + l])) {
1626 for (j = 0; j < onum; ++j) {
1627 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
1628 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
1630 msig = mono_signature_get_desc (mono_method_signature (im), FALSE);
1631 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
1632 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
1634 for (j = 0; j < class->method.count; ++j) {
1635 MonoMethod *cm = class->methods [j];
1636 msig = mono_signature_get_desc (mono_method_signature (cm), FALSE);
1638 printf ("METHOD %s(%s)\n", cm->name, msig);
1641 g_assert_not_reached ();
1646 for (l = 0; l < ic->method.count; l++) {
1647 MonoMethod *im = vtable [io + l];
1650 g_assert (io + l <= max_vtsize);
1652 /* FIXME: why do we need this ? */
1654 /* g_assert_not_reached (); */
1660 g_ptr_array_free (ifaces, TRUE);
1663 for (i = 0; i < class->method.count; ++i) {
1666 cm = class->methods [i];
1669 * Non-virtual method have no place in the vtable.
1670 * This also catches static methods (since they are not virtual).
1672 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1676 * If the method is REUSE_SLOT, we must check in the
1677 * base class for a method to override.
1679 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
1681 for (k = class->parent; k ; k = k->parent) {
1683 for (j = 0; j < k->method.count; ++j) {
1684 MonoMethod *m1 = k->methods [j];
1685 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
1687 if (!strcmp(cm->name, m1->name) &&
1688 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (m1))) {
1690 /* CAS - SecurityAction.InheritanceDemand */
1691 if (security_enabled && (m1->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1692 mono_secman_inheritancedemand_method (cm, m1);
1695 slot = k->methods [j]->slot;
1696 g_assert (cm->slot < max_vtsize);
1698 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1699 g_hash_table_insert (override_map, m1, cm);
1711 cm->slot = cur_slot++;
1713 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT) && ! mono_method_signature (cm)->generic_param_count)
1714 vtable [cm->slot] = cm;
1717 /* override non interface methods */
1718 for (i = 0; i < onum; i++) {
1719 MonoMethod *decl = overrides [i*2];
1720 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
1721 g_assert (decl->slot != -1);
1722 vtable [decl->slot] = overrides [i*2 + 1];
1723 overrides [i * 2 + 1]->slot = decl->slot;
1725 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1726 g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
1731 * If a method occupies more than one place in the vtable, and it is
1732 * overriden, then change the other occurances too.
1735 for (i = 0; i < max_vtsize; ++i)
1737 MonoMethod *cm = g_hash_table_lookup (override_map, vtable [i]);
1742 g_hash_table_destroy (override_map);
1745 if (class->generic_class) {
1746 MonoClass *gklass = class->generic_class->container_class;
1748 mono_class_init (gklass);
1749 class->vtable_size = gklass->vtable_size;
1751 class->vtable_size = cur_slot;
1753 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
1754 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
1756 if (mono_print_vtable) {
1759 for (i = 0; i <= max_iid; i++)
1760 if (class->interface_offsets [i] != -1)
1763 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
1764 class->name, class->vtable_size, icount);
1766 for (i = 0; i < class->vtable_size; ++i) {
1771 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
1772 cm->klass->name_space, cm->klass->name,
1779 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
1780 class->name, max_iid);
1782 for (i = 0; i < class->interface_count; i++) {
1783 ic = class->interfaces [i];
1784 printf (" slot %03d(%03d) %s.%s\n",
1785 class->interface_offsets [ic->interface_id],
1786 ic->method.count, ic->name_space, ic->name);
1789 for (k = class->parent; k ; k = k->parent) {
1790 for (i = 0; i < k->interface_count; i++) {
1791 ic = k->interfaces [i];
1792 printf (" slot %03d(%03d) %s.%s\n",
1793 class->interface_offsets [ic->interface_id],
1794 ic->method.count, ic->name_space, ic->name);
1803 * @class: the class to initialize
1805 * compute the instance_size, class_size and other infos that cannot be
1806 * computed at mono_class_get() time. Also compute a generic vtable and
1807 * the method slot numbers. We use this infos later to create a domain
1811 mono_class_init (MonoClass *class)
1814 static MonoMethod *default_ghc = NULL;
1815 static MonoMethod *default_finalize = NULL;
1816 static int finalize_slot = -1;
1817 static int ghc_slot = -1;
1818 MonoCachedClassInfo cached_info;
1819 gboolean has_cached_info;
1826 /*g_print ("Init class %s\n", class->name);*/
1828 /* We do everything inside the lock to prevent races */
1829 mono_loader_lock ();
1831 if (class->inited) {
1832 mono_loader_unlock ();
1833 /* Somebody might have gotten in before us */
1837 if (class->init_pending) {
1838 mono_loader_unlock ();
1839 /* this indicates a cyclic dependency */
1840 g_error ("pending init %s.%s\n", class->name_space, class->name);
1843 class->init_pending = 1;
1845 /* CAS - SecurityAction.InheritanceDemand */
1846 if (mono_is_security_manager_active () && class->parent && (class->parent->flags & TYPE_ATTRIBUTE_HAS_SECURITY)) {
1847 mono_secman_inheritancedemand_class (class, class->parent);
1850 if (mono_debugger_start_class_init_func)
1851 mono_debugger_start_class_init_func (class);
1853 mono_stats.initialized_class_count++;
1855 if (class->generic_class && !class->generic_class->is_dynamic) {
1856 MonoInflatedGenericClass *gclass;
1859 gclass = mono_get_inflated_generic_class (class->generic_class);
1860 gklass = gclass->generic_class.container_class;
1862 mono_stats.generic_class_count++;
1864 class->method = gklass->method;
1865 class->field = gklass->field;
1867 mono_class_init (gklass);
1868 mono_class_setup_methods (gklass);
1869 mono_class_setup_properties (gklass);
1871 if (MONO_CLASS_IS_INTERFACE (class))
1872 class->interface_id = mono_get_unique_iid (class);
1874 g_assert (class->method.count == gklass->method.count);
1875 class->methods = g_new0 (MonoMethod *, class->method.count);
1877 for (i = 0; i < class->method.count; i++) {
1878 MonoMethod *inflated = mono_class_inflate_generic_method (
1879 gklass->methods [i], gclass->generic_class.context);
1881 class->methods [i] = mono_get_inflated_method (inflated);
1884 class->property = gklass->property;
1885 class->properties = g_new0 (MonoProperty, class->property.count);
1887 for (i = 0; i < class->property.count; i++) {
1888 MonoProperty *prop = &class->properties [i];
1890 *prop = gklass->properties [i];
1893 prop->get = mono_class_inflate_generic_method (
1894 prop->get, gclass->generic_class.context);
1896 prop->set = mono_class_inflate_generic_method (
1897 prop->set, gclass->generic_class.context);
1899 prop->parent = class;
1902 g_assert (class->interface_count == gklass->interface_count);
1905 if (class->parent && !class->parent->inited)
1906 mono_class_init (class->parent);
1908 has_cached_info = mono_class_get_cached_class_info (class, &cached_info);
1910 if (!class->generic_class && (!has_cached_info || (has_cached_info && cached_info.has_nested_classes))) {
1911 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
1914 guint32 cols [MONO_NESTED_CLASS_SIZE];
1915 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
1916 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
1917 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
1919 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
1924 * Computes the size used by the fields, and their locations
1926 if (has_cached_info) {
1927 class->instance_size = cached_info.instance_size;
1928 class->class_size = cached_info.class_size;
1929 class->packing_size = cached_info.packing_size;
1930 class->min_align = cached_info.min_align;
1931 class->blittable = cached_info.blittable;
1932 class->has_references = cached_info.has_references;
1933 class->has_static_refs = cached_info.has_static_refs;
1936 if (!class->size_inited)
1937 mono_class_setup_fields (class);
1939 /* initialize method pointers */
1942 MonoMethodSignature *sig;
1943 class->method.count = class->rank > 1? 2: 1;
1944 sig = mono_metadata_signature_alloc (class->image, class->rank);
1945 sig->ret = &mono_defaults.void_class->byval_arg;
1946 sig->pinvoke = TRUE;
1947 for (i = 0; i < class->rank; ++i)
1948 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1950 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1951 ctor->klass = class;
1952 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1953 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1954 ctor->signature = sig;
1955 ctor->name = ".ctor";
1957 class->methods = g_new (MonoMethod*, class->method.count);
1958 class->methods [0] = ctor;
1959 if (class->rank > 1) {
1960 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
1961 sig->ret = &mono_defaults.void_class->byval_arg;
1962 sig->pinvoke = TRUE;
1963 for (i = 0; i < class->rank * 2; ++i)
1964 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1966 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1967 ctor->klass = class;
1968 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1969 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1970 ctor->signature = sig;
1971 ctor->name = ".ctor";
1973 class->methods [1] = ctor;
1977 mono_class_setup_supertypes (class);
1980 if (class == mono_defaults.object_class) {
1981 mono_class_setup_vtable (class);
1982 for (i = 0; i < class->vtable_size; ++i) {
1983 MonoMethod *cm = class->vtable [i];
1985 if (!strcmp (cm->name, "GetHashCode")) {
1991 g_assert (ghc_slot > 0);
1993 default_ghc = class->vtable [ghc_slot];
1997 if (!default_finalize) {
1998 if (class == mono_defaults.object_class) {
1999 mono_class_setup_vtable (class);
2000 for (i = 0; i < class->vtable_size; ++i) {
2001 MonoMethod *cm = class->vtable [i];
2003 if (!strcmp (cm->name, "Finalize")) {
2009 g_assert (finalize_slot > 0);
2011 default_finalize = class->vtable [finalize_slot];
2016 * If possible, avoid the creation of the generic vtable by requesting
2017 * cached info from the runtime.
2019 if (has_cached_info) {
2020 guint32 cur_slot = 0;
2022 class->vtable_size = cached_info.vtable_size;
2023 class->has_finalize = cached_info.has_finalize;
2024 class->ghcimpl = cached_info.ghcimpl;
2025 class->has_cctor = cached_info.has_cctor;
2027 if (class->parent) {
2028 mono_class_init (class->parent);
2029 cur_slot = class->parent->vtable_size;
2032 setup_interface_offsets (class, cur_slot);
2035 mono_class_setup_vtable (class);
2038 if (class->parent) {
2039 MonoMethod *cmethod = class->vtable [ghc_slot];
2040 if (cmethod->is_inflated)
2041 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
2042 if (cmethod == default_ghc) {
2047 /* Object::Finalize should have empty implemenatation */
2048 class->has_finalize = 0;
2049 if (class->parent) {
2050 MonoMethod *cmethod = class->vtable [finalize_slot];
2051 if (cmethod->is_inflated)
2052 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
2053 if (cmethod != default_finalize) {
2054 class->has_finalize = 1;
2058 for (i = 0; i < class->method.count; ++i) {
2059 MonoMethod *method = class->methods [i];
2060 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2061 (strcmp (".cctor", method->name) == 0)) {
2062 class->has_cctor = 1;
2069 class->init_pending = 0;
2071 mono_loader_unlock ();
2073 if (MONO_CLASS_IS_INTERFACE (class)) {
2075 * class->interface_offsets is needed for the castclass/isinst code, so
2076 * we have to setup them for interfaces, too.
2078 setup_interface_offsets (class, 0);
2081 if (mono_debugger_class_init_func)
2082 mono_debugger_class_init_func (class);
2086 * LOCKING: this assumes the loader lock is held
2089 mono_class_setup_mono_type (MonoClass *class)
2091 const char *name = class->name;
2092 const char *nspace = class->name_space;
2094 if (MONO_CLASS_IS_INTERFACE (class))
2095 class->interface_id = mono_get_unique_iid (class);
2097 class->this_arg.byref = 1;
2098 class->this_arg.data.klass = class;
2099 class->this_arg.type = MONO_TYPE_CLASS;
2100 class->byval_arg.data.klass = class;
2101 class->byval_arg.type = MONO_TYPE_CLASS;
2103 if (!strcmp (nspace, "System")) {
2104 if (!strcmp (name, "ValueType")) {
2106 * do not set the valuetype bit for System.ValueType.
2107 * class->valuetype = 1;
2109 class->blittable = TRUE;
2110 } else if (!strcmp (name, "Enum")) {
2112 * do not set the valuetype bit for System.Enum.
2113 * class->valuetype = 1;
2115 class->valuetype = 0;
2116 class->enumtype = 0;
2117 } else if (!strcmp (name, "Object")) {
2118 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
2119 } else if (!strcmp (name, "String")) {
2120 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
2121 } else if (!strcmp (name, "TypedReference")) {
2122 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
2126 if (class->valuetype) {
2127 int t = MONO_TYPE_VALUETYPE;
2128 if (!strcmp (nspace, "System")) {
2131 if (!strcmp (name, "Boolean")) {
2132 t = MONO_TYPE_BOOLEAN;
2133 } else if (!strcmp(name, "Byte")) {
2135 class->blittable = TRUE;
2139 if (!strcmp (name, "Char")) {
2144 if (!strcmp (name, "Double")) {
2146 class->blittable = TRUE;
2150 if (!strcmp (name, "Int32")) {
2152 class->blittable = TRUE;
2153 } else if (!strcmp(name, "Int16")) {
2155 class->blittable = TRUE;
2156 } else if (!strcmp(name, "Int64")) {
2158 class->blittable = TRUE;
2159 } else if (!strcmp(name, "IntPtr")) {
2161 class->blittable = TRUE;
2165 if (!strcmp (name, "Single")) {
2167 class->blittable = TRUE;
2168 } else if (!strcmp(name, "SByte")) {
2170 class->blittable = TRUE;
2174 if (!strcmp (name, "UInt32")) {
2176 class->blittable = TRUE;
2177 } else if (!strcmp(name, "UInt16")) {
2179 class->blittable = TRUE;
2180 } else if (!strcmp(name, "UInt64")) {
2182 class->blittable = TRUE;
2183 } else if (!strcmp(name, "UIntPtr")) {
2185 class->blittable = TRUE;
2189 if (!strcmp (name, "TypedReference")) {
2190 t = MONO_TYPE_TYPEDBYREF;
2191 class->blittable = TRUE;
2195 if (!strcmp (name, "Void")) {
2203 class->this_arg.type = class->byval_arg.type = t;
2208 * LOCKING: this assumes the loader lock is held
2211 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
2213 gboolean system_namespace;
2215 system_namespace = !strcmp (class->name_space, "System");
2217 /* if root of the hierarchy */
2218 if (system_namespace && !strcmp (class->name, "Object")) {
2219 class->parent = NULL;
2220 class->instance_size = sizeof (MonoObject);
2223 if (!strcmp (class->name, "<Module>")) {
2224 class->parent = NULL;
2225 class->instance_size = 0;
2229 if (!MONO_CLASS_IS_INTERFACE (class)) {
2230 class->parent = parent;
2233 g_assert_not_reached (); /* FIXME */
2235 if (parent->generic_class && !parent->name) {
2237 * If the parent is a generic instance, we may get
2238 * called before it is fully initialized, especially
2239 * before it has its name.
2244 class->marshalbyref = parent->marshalbyref;
2245 class->contextbound = parent->contextbound;
2246 class->delegate = parent->delegate;
2248 if (system_namespace) {
2249 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
2250 class->marshalbyref = 1;
2252 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
2253 class->contextbound = 1;
2255 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
2256 class->delegate = 1;
2259 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
2260 (strcmp (class->parent->name_space, "System") == 0)))
2261 class->valuetype = 1;
2262 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
2263 class->valuetype = class->enumtype = 1;
2265 /*class->enumtype = class->parent->enumtype; */
2266 mono_class_setup_supertypes (class);
2268 class->parent = NULL;
2274 * LOCKING: this assumes the loader lock is held
2277 mono_class_setup_supertypes (MonoClass *class)
2281 if (class->supertypes)
2284 if (class->parent && !class->parent->supertypes)
2285 mono_class_setup_supertypes (class->parent);
2287 class->idepth = class->parent->idepth + 1;
2291 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
2292 class->supertypes = g_new0 (MonoClass *, ms);
2294 if (class->parent) {
2295 class->supertypes [class->idepth - 1] = class;
2296 memcpy (class->supertypes, class->parent->supertypes, class->parent->idepth * sizeof (gpointer));
2298 class->supertypes [0] = class;
2303 * If we inherit a type parameter from an outer class, set its owner to that class.
2306 set_generic_param_owner (MonoGenericContainer *container, MonoClass *klass, int pos)
2308 MonoGenericContainer *gc;
2311 if (klass->nested_in)
2312 pos = set_generic_param_owner (container, klass->nested_in, pos);
2314 if (!klass->generic_container)
2317 gc = klass->generic_container;
2318 for (i = pos; i < gc->type_argc; i++)
2319 container->type_params [i].owner = gc;
2321 return pos + gc->type_argc;
2324 static MonoGenericInst *
2325 get_shared_inst (MonoGenericContainer *container)
2327 MonoGenericInst *nginst;
2330 nginst = g_new0 (MonoGenericInst, 1);
2331 nginst->type_argc = container->type_argc;
2332 nginst->type_argv = g_new0 (MonoType *, nginst->type_argc);
2333 nginst->is_reference = 1;
2334 nginst->is_open = 1;
2336 for (i = 0; i < nginst->type_argc; i++) {
2337 MonoType *t = g_new0 (MonoType, 1);
2339 t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2340 t->data.generic_param = &container->type_params [i];
2342 nginst->type_argv [i] = t;
2345 return mono_metadata_lookup_generic_inst (nginst);
2349 mono_get_shared_generic_class (MonoGenericContainer *container, gboolean is_dynamic)
2351 MonoInflatedGenericClass *igclass;
2352 MonoGenericClass *gclass;
2355 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
2356 igclass = &dgclass->generic_class;
2357 gclass = &igclass->generic_class;
2358 gclass->is_inflated = 1;
2359 gclass->is_dynamic = 1;
2361 igclass = g_new0 (MonoInflatedGenericClass, 1);
2362 gclass = &igclass->generic_class;
2363 gclass->is_inflated = 1;
2366 gclass->context = &container->context;
2367 gclass->container_class = container->klass;
2368 gclass->inst = get_shared_inst (container);
2371 MonoGenericClass *cached = mono_metadata_lookup_generic_class (gclass);
2379 igclass->klass = container->klass;
2385 * @image: context where the image is created
2386 * @type_token: typedef token
2389 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
2391 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
2392 MonoClass *class, *parent = NULL;
2393 guint32 cols [MONO_TYPEDEF_SIZE];
2394 guint32 cols_next [MONO_TYPEDEF_SIZE];
2395 guint tidx = mono_metadata_token_index (type_token);
2396 MonoGenericContext *context = NULL;
2397 const char *name, *nspace;
2399 MonoClass **interfaces;
2401 mono_loader_lock ();
2403 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
2404 mono_loader_unlock ();
2408 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
2410 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
2412 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2413 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2415 class = g_malloc0 (sizeof (MonoClass));
2418 class->name_space = nspace;
2420 class->image = image;
2421 class->type_token = type_token;
2422 class->flags = cols [MONO_TYPEDEF_FLAGS];
2424 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
2426 class->generic_container = mono_metadata_load_generic_params (image, class->type_token, NULL);
2427 if (class->generic_container) {
2428 class->generic_container->klass = class;
2429 context = &class->generic_container->context;
2431 context->gclass = mono_get_shared_generic_class (context->container, FALSE);
2434 if (cols [MONO_TYPEDEF_EXTENDS])
2435 parent = mono_class_get_full (
2436 image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]), context);
2438 mono_class_setup_parent (class, parent);
2440 mono_class_setup_mono_type (class);
2442 interfaces = mono_metadata_interfaces_from_typedef_full (image, type_token, &icount, context);
2444 class->interfaces = interfaces;
2445 class->interface_count = icount;
2447 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
2449 /* fixme: maybe we must set this on windows
2450 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
2454 class->cast_class = class->element_class = class;
2456 /*g_print ("Load class %s\n", name);*/
2459 * Compute the field and method lists
2461 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
2462 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
2464 if (tt->rows > tidx){
2465 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
2466 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
2467 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
2469 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
2470 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
2473 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
2474 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
2475 class->field.count = class->field.last - class->field.first;
2477 class->field.count = 0;
2479 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
2480 class->method.count = class->method.last - class->method.first;
2482 class->method.count = 0;
2484 /* reserve space to store vector pointer in arrays */
2485 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
2486 class->instance_size += 2 * sizeof (gpointer);
2487 g_assert (class->field.count == 0);
2490 if (class->enumtype)
2491 mono_class_setup_fields (class);
2493 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
2494 class->nested_in = mono_class_create_from_typedef (image, type_token);
2496 if (class->nested_in && class->generic_container)
2497 set_generic_param_owner (class->generic_container, class->nested_in, 0);
2499 mono_loader_unlock ();
2505 mono_class_create_generic (MonoInflatedGenericClass *gclass)
2507 MonoClass *klass, *gklass;
2510 if (gclass->is_initialized)
2512 gclass->is_initialized = TRUE;
2515 gclass->klass = g_malloc0 (sizeof (MonoClass));
2516 klass = gclass->klass;
2518 gklass = gclass->generic_class.container_class;
2520 klass->nested_in = gklass->nested_in;
2522 klass->name = gklass->name;
2523 klass->name_space = gklass->name_space;
2524 klass->image = gklass->image;
2525 klass->flags = gklass->flags;
2527 klass->generic_class = &gclass->generic_class;
2529 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
2530 klass->this_arg.data.generic_class = klass->byval_arg.data.generic_class = &gclass->generic_class;
2531 klass->this_arg.byref = TRUE;
2533 klass->cast_class = klass->element_class = klass;
2535 if (gclass->generic_class.is_dynamic) {
2536 klass->instance_size = gklass->instance_size;
2537 klass->class_size = gklass->class_size;
2538 klass->size_inited = 1;
2541 klass->valuetype = gklass->valuetype;
2543 mono_class_setup_supertypes (klass);
2546 klass->interface_count = gklass->interface_count;
2547 klass->interfaces = g_new0 (MonoClass *, klass->interface_count);
2548 for (i = 0; i < klass->interface_count; i++) {
2549 MonoType *it = &gklass->interfaces [i]->byval_arg;
2550 MonoType *inflated = mono_class_inflate_generic_type (
2551 it, gclass->generic_class.context);
2552 klass->interfaces [i] = mono_class_from_mono_type (inflated);
2555 i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, 1);
2558 guint32 cols [MONO_NESTED_CLASS_SIZE];
2559 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
2560 nclass = mono_class_create_from_typedef (klass->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
2561 klass->nested_classes = g_list_prepend (klass->nested_classes, nclass);
2563 i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, i + 1);
2566 if (gclass->generic_class.is_dynamic) {
2567 MonoDynamicGenericClass *dgclass = (MonoDynamicGenericClass *) gclass;
2569 if (dgclass->parent)
2570 klass->parent = mono_class_from_mono_type (dgclass->parent);
2571 } else if (gklass->parent) {
2572 MonoType *inflated = mono_class_inflate_generic_type (
2573 &gklass->parent->byval_arg, gclass->generic_class.context);
2575 klass->parent = mono_class_from_mono_type (inflated);
2579 mono_class_setup_parent (klass, klass->parent);
2583 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
2585 MonoClass *klass, **ptr;
2589 return param->pklass;
2591 klass = param->pklass = g_new0 (MonoClass, 1);
2593 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
2597 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
2598 klass->parent = param->constraints [0];
2600 } else if (param->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT)
2601 klass->parent = mono_class_from_name (mono_defaults.corlib, "System", "ValueType");
2603 klass->parent = mono_defaults.object_class;
2605 if (count - pos > 0) {
2606 klass->interface_count = count - pos;
2607 klass->interfaces = g_new0 (MonoClass *, count - pos);
2608 for (i = pos; i < count; i++)
2609 klass->interfaces [i - pos] = param->constraints [i];
2612 g_assert (param->name && param->owner);
2614 klass->name = param->name;
2615 klass->name_space = "";
2616 klass->image = image;
2617 klass->inited = TRUE;
2618 klass->cast_class = klass->element_class = klass;
2619 klass->enum_basetype = &klass->element_class->byval_arg;
2620 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2622 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2623 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2624 klass->this_arg.byref = TRUE;
2626 mono_class_setup_supertypes (klass);
2632 my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
2637 return param->pklass;
2639 g_assert (param->owner);
2641 klass = g_new0 (MonoClass, 1);
2644 klass->name = param->name;
2646 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
2647 klass->name_space = "";
2648 klass->image = mono_defaults.corlib;
2649 klass->inited = TRUE;
2650 klass->cast_class = klass->element_class = klass;
2651 klass->enum_basetype = &klass->element_class->byval_arg;
2652 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2654 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2655 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2656 klass->this_arg.byref = TRUE;
2658 mono_class_setup_supertypes (klass);
2664 mono_ptr_class_get (MonoType *type)
2667 MonoClass *el_class;
2668 static GHashTable *ptr_hash = NULL;
2670 mono_loader_lock ();
2673 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2674 el_class = mono_class_from_mono_type (type);
2675 if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
2676 mono_loader_unlock ();
2679 result = g_new0 (MonoClass, 1);
2681 result->parent = NULL; /* no parent for PTR types */
2682 result->name_space = el_class->name_space;
2683 result->name = g_strdup_printf ("%s*", el_class->name);
2684 result->image = el_class->image;
2685 result->inited = TRUE;
2686 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2687 /* Can pointers get boxed? */
2688 result->instance_size = sizeof (gpointer);
2689 result->cast_class = result->element_class = el_class;
2690 result->enum_basetype = &result->element_class->byval_arg;
2691 result->blittable = TRUE;
2693 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
2694 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
2695 result->this_arg.byref = TRUE;
2697 mono_class_setup_supertypes (result);
2699 g_hash_table_insert (ptr_hash, el_class, result);
2701 mono_loader_unlock ();
2707 mono_fnptr_class_get (MonoMethodSignature *sig)
2710 static GHashTable *ptr_hash = NULL;
2712 mono_loader_lock ();
2715 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2717 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
2718 mono_loader_unlock ();
2721 result = g_new0 (MonoClass, 1);
2723 result->parent = NULL; /* no parent for PTR types */
2724 result->name = "System";
2725 result->name_space = "MonoFNPtrFakeClass";
2726 result->image = NULL; /* need to fix... */
2727 result->inited = TRUE;
2728 result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
2729 /* Can pointers get boxed? */
2730 result->instance_size = sizeof (gpointer);
2731 result->cast_class = result->element_class = result;
2732 result->blittable = TRUE;
2734 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
2735 result->this_arg.data.method = result->byval_arg.data.method = sig;
2736 result->this_arg.byref = TRUE;
2737 result->enum_basetype = &result->element_class->byval_arg;
2738 result->blittable = TRUE;
2740 mono_class_setup_supertypes (result);
2742 g_hash_table_insert (ptr_hash, sig, result);
2744 mono_loader_unlock ();
2750 mono_class_from_mono_type (MonoType *type)
2752 switch (type->type) {
2753 case MONO_TYPE_OBJECT:
2754 return type->data.klass? type->data.klass: mono_defaults.object_class;
2755 case MONO_TYPE_VOID:
2756 return type->data.klass? type->data.klass: mono_defaults.void_class;
2757 case MONO_TYPE_BOOLEAN:
2758 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
2759 case MONO_TYPE_CHAR:
2760 return type->data.klass? type->data.klass: mono_defaults.char_class;
2762 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
2764 return type->data.klass? type->data.klass: mono_defaults.byte_class;
2766 return type->data.klass? type->data.klass: mono_defaults.int16_class;
2768 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
2770 return type->data.klass? type->data.klass: mono_defaults.int32_class;
2772 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
2774 return type->data.klass? type->data.klass: mono_defaults.int_class;
2776 return type->data.klass? type->data.klass: mono_defaults.uint_class;
2778 return type->data.klass? type->data.klass: mono_defaults.int64_class;
2780 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
2782 return type->data.klass? type->data.klass: mono_defaults.single_class;
2784 return type->data.klass? type->data.klass: mono_defaults.double_class;
2785 case MONO_TYPE_STRING:
2786 return type->data.klass? type->data.klass: mono_defaults.string_class;
2787 case MONO_TYPE_TYPEDBYREF:
2788 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
2789 case MONO_TYPE_ARRAY:
2790 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
2792 return mono_ptr_class_get (type->data.type);
2793 case MONO_TYPE_FNPTR:
2794 return mono_fnptr_class_get (type->data.method);
2795 case MONO_TYPE_SZARRAY:
2796 return mono_array_class_get (type->data.klass, 1);
2797 case MONO_TYPE_CLASS:
2798 case MONO_TYPE_VALUETYPE:
2799 return type->data.klass;
2800 case MONO_TYPE_GENERICINST: {
2801 MonoInflatedGenericClass *gclass;
2802 gclass = mono_get_inflated_generic_class (type->data.generic_class);
2803 g_assert (gclass->klass);
2804 return gclass->klass;
2807 return my_mono_class_from_generic_parameter (type->data.generic_param, FALSE);
2808 case MONO_TYPE_MVAR:
2809 return my_mono_class_from_generic_parameter (type->data.generic_param, TRUE);
2811 g_warning ("implement me 0x%02x\n", type->type);
2812 g_assert_not_reached ();
2819 * @image: context where the image is created
2820 * @type_spec: typespec token
2823 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec,
2824 MonoGenericContext *context)
2826 MonoType *type, *inflated;
2829 type = mono_type_create_from_typespec_full (image, context, type_spec);
2831 switch (type->type) {
2832 case MONO_TYPE_ARRAY:
2833 class = mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
2835 case MONO_TYPE_SZARRAY:
2836 class = mono_array_class_get (type->data.klass, 1);
2839 class = mono_ptr_class_get (type->data.type);
2841 case MONO_TYPE_GENERICINST: {
2842 MonoInflatedGenericClass *gclass;
2843 gclass = mono_get_inflated_generic_class (type->data.generic_class);
2844 g_assert (gclass->klass);
2845 class = gclass->klass;
2849 /* it seems any type can be stored in TypeSpec as well */
2850 class = mono_class_from_mono_type (type);
2854 if (!class || !context || (!context->gclass && !context->gmethod))
2857 inflated = mono_class_inflate_generic_type (&class->byval_arg, context);
2859 return mono_class_from_mono_type (inflated);
2863 * mono_bounded_array_class_get:
2864 * @element_class: element class
2865 * @rank: the dimension of the array class
2866 * @bounded: whenever the array has non-zero bounds
2868 * Returns: a class object describing the array with element type @element_type and
2872 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
2876 MonoClass *parent = NULL;
2877 GSList *list, *rootlist;
2880 gboolean corlib_type = FALSE;
2882 g_assert (rank <= 255);
2885 /* bounded only matters for one-dimensional arrays */
2888 image = eclass->image;
2890 mono_loader_lock ();
2892 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
2893 for (; list; list = list->next) {
2895 if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
2896 mono_loader_unlock ();
2902 /* for the building corlib use System.Array from it */
2903 if (image->assembly && image->assembly->dynamic && image->assembly_name && strcmp (image->assembly_name, "mscorlib") == 0) {
2904 parent = mono_class_from_name (image, "System", "Array");
2906 } else if (mono_defaults.generic_array_class) {
2907 MonoType *inflated, **args;
2909 args = g_new0 (MonoType *, 1);
2910 args [0] = &eclass->byval_arg;
2912 inflated = mono_class_bind_generic_parameters (
2913 &mono_defaults.generic_array_class->byval_arg, 1, args);
2914 parent = mono_class_from_mono_type (inflated);
2916 if (!parent->inited)
2917 mono_class_init (parent);
2919 parent = mono_defaults.array_class;
2920 if (!parent->inited)
2921 mono_class_init (parent);
2924 class = g_malloc0 (sizeof (MonoClass));
2926 class->image = image;
2927 class->name_space = eclass->name_space;
2928 nsize = strlen (eclass->name);
2929 name = g_malloc (nsize + 2 + rank);
2930 memcpy (name, eclass->name, nsize);
2933 memset (name + nsize + 1, ',', rank - 1);
2934 name [nsize + rank] = ']';
2935 name [nsize + rank + 1] = 0;
2937 class->type_token = 0;
2938 /* all arrays are marked serializable and sealed, bug #42779 */
2939 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
2940 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2941 class->parent = parent;
2942 class->instance_size = mono_class_instance_size (class->parent);
2943 class->class_size = 0;
2944 mono_class_setup_supertypes (class);
2945 if (eclass->generic_class)
2946 mono_class_init (eclass);
2947 if (!eclass->size_inited)
2948 mono_class_setup_fields (eclass);
2949 class->has_references = MONO_TYPE_IS_REFERENCE (&eclass->byval_arg) || eclass->has_references? TRUE: FALSE;
2953 if (eclass->enumtype)
2954 class->cast_class = eclass->element_class;
2956 class->cast_class = eclass;
2958 class->element_class = eclass;
2960 if ((rank > 1) || bounded) {
2961 MonoArrayType *at = g_new0 (MonoArrayType, 1);
2962 class->byval_arg.type = MONO_TYPE_ARRAY;
2963 class->byval_arg.data.array = at;
2964 at->eklass = eclass;
2966 /* FIXME: complete.... */
2968 class->byval_arg.type = MONO_TYPE_SZARRAY;
2969 class->byval_arg.data.klass = eclass;
2971 class->this_arg = class->byval_arg;
2972 class->this_arg.byref = 1;
2977 list = g_slist_append (rootlist, class);
2978 g_hash_table_insert (image->array_cache, eclass, list);
2980 mono_loader_unlock ();
2986 * mono_array_class_get:
2987 * @element_class: element class
2988 * @rank: the dimension of the array class
2990 * Returns: a class object describing the array with element type @element_type and
2994 mono_array_class_get (MonoClass *eclass, guint32 rank)
2996 return mono_bounded_array_class_get (eclass, rank, FALSE);
3000 * mono_class_instance_size:
3003 * Returns: the size of an object instance
3006 mono_class_instance_size (MonoClass *klass)
3008 if (!klass->size_inited)
3009 mono_class_init (klass);
3011 return klass->instance_size;
3015 * mono_class_min_align:
3018 * Returns: minimm alignment requirements
3021 mono_class_min_align (MonoClass *klass)
3023 if (!klass->size_inited)
3024 mono_class_init (klass);
3026 return klass->min_align;
3030 * mono_class_value_size:
3033 * This function is used for value types, and return the
3034 * space and the alignment to store that kind of value object.
3036 * Returns: the size of a value of kind @klass
3039 mono_class_value_size (MonoClass *klass, guint32 *align)
3043 /* fixme: check disable, because we still have external revereces to
3044 * mscorlib and Dummy Objects
3046 /*g_assert (klass->valuetype);*/
3048 size = mono_class_instance_size (klass) - sizeof (MonoObject);
3051 *align = klass->min_align;
3057 * mono_class_data_size:
3060 * Returns: the size of the static class data
3063 mono_class_data_size (MonoClass *klass)
3066 mono_class_init (klass);
3068 return klass->class_size;
3072 * Auxiliary routine to mono_class_get_field
3074 * Takes a field index instead of a field token.
3076 static MonoClassField *
3077 mono_class_get_field_idx (MonoClass *class, int idx)
3079 mono_class_setup_fields (class);
3081 if (class->field.count){
3082 if ((idx >= class->field.first) && (idx < class->field.last)){
3083 return &class->fields [idx - class->field.first];
3090 return mono_class_get_field_idx (class->parent, idx);
3094 * mono_class_get_field:
3095 * @class: the class to lookup the field.
3096 * @field_token: the field token
3098 * Returns: A MonoClassField representing the type and offset of
3099 * the field, or a NULL value if the field does not belong to this
3103 mono_class_get_field (MonoClass *class, guint32 field_token)
3105 int idx = mono_metadata_token_index (field_token);
3107 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
3109 return mono_class_get_field_idx (class, idx - 1);
3113 mono_class_get_field_from_name (MonoClass *klass, const char *name)
3118 mono_class_setup_fields (klass);
3119 for (i = 0; i < klass->field.count; ++i) {
3120 if (strcmp (name, klass->fields [i].name) == 0)
3121 return &klass->fields [i];
3123 klass = klass->parent;
3129 mono_class_get_field_token (MonoClassField *field)
3131 MonoClass *klass = field->parent;
3135 mono_class_setup_fields (klass);
3136 for (i = 0; i < klass->field.count; ++i) {
3137 if (&klass->fields [i] == field)
3138 return mono_metadata_make_token (MONO_TABLE_FIELD, klass->field.first + i + 1);
3140 klass = klass->parent;
3143 g_assert_not_reached ();
3148 mono_class_get_event_token (MonoEvent *event)
3150 MonoClass *klass = event->parent;
3154 for (i = 0; i < klass->event.count; ++i) {
3155 if (&klass->events [i] == event)
3156 return mono_metadata_make_token (MONO_TABLE_EVENT, klass->event.first + i + 1);
3158 klass = klass->parent;
3161 g_assert_not_reached ();
3166 mono_vtable_get_static_field_data (MonoVTable *vt)
3172 mono_class_get_property_from_name (MonoClass *klass, const char *name)
3176 gpointer iter = NULL;
3177 while ((p = mono_class_get_properties (klass, &iter))) {
3178 if (! strcmp (name, p->name))
3181 klass = klass->parent;
3187 mono_class_get_property_token (MonoProperty *prop)
3189 MonoClass *klass = prop->parent;
3193 gpointer iter = NULL;
3194 while ((p = mono_class_get_properties (klass, &iter))) {
3195 if (&klass->properties [i] == prop)
3196 return mono_metadata_make_token (MONO_TABLE_PROPERTY, klass->property.first + i + 1);
3200 klass = klass->parent;
3203 g_assert_not_reached ();
3208 mono_class_name_from_token (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3210 const char *name, *nspace;
3212 return g_strdup_printf ("DynamicType 0x%08x", type_token);
3214 switch (type_token & 0xff000000){
3215 case MONO_TOKEN_TYPE_DEF: {
3216 guint32 cols [MONO_TYPEDEF_SIZE];
3217 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
3218 guint tidx = mono_metadata_token_index (type_token);
3220 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
3221 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3222 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3223 if (strlen (nspace) == 0)
3224 return g_strdup_printf ("%s", name);
3226 return g_strdup_printf ("%s.%s", nspace, name);
3229 case MONO_TOKEN_TYPE_REF: {
3230 guint32 cols [MONO_TYPEREF_SIZE];
3231 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
3233 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
3234 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
3235 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
3236 if (strlen (nspace) == 0)
3237 return g_strdup_printf ("%s", name);
3239 return g_strdup_printf ("%s.%s", nspace, name);
3242 case MONO_TOKEN_TYPE_SPEC:
3243 return g_strdup_printf ("Typespec 0x%08x", type_token);
3245 g_assert_not_reached ();
3253 * @image: the image where the class resides
3254 * @type_token: the token for the class
3255 * @at: an optional pointer to return the array element type
3257 * Returns: the MonoClass that represents @type_token in @image
3260 _mono_class_get (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3262 MonoClass *class = NULL;
3265 return mono_lookup_dynamic_token (image, type_token);
3267 switch (type_token & 0xff000000){
3268 case MONO_TOKEN_TYPE_DEF:
3269 class = mono_class_create_from_typedef (image, type_token);
3271 case MONO_TOKEN_TYPE_REF:
3272 class = mono_class_from_typeref (image, type_token);
3274 case MONO_TOKEN_TYPE_SPEC:
3275 class = mono_class_create_from_typespec (image, type_token, context);
3278 g_warning ("unknown token type %x", type_token & 0xff000000);
3279 g_assert_not_reached ();
3283 char *name = mono_class_name_from_token (image, type_token, context);
3284 g_warning ("The class %s could not be loaded, used in %s (token 0x%08x)", name, image->name, type_token);
3292 mono_class_get (MonoImage *image, guint32 type_token)
3294 return _mono_class_get (image, type_token, NULL);
3298 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3300 MonoClass *class = _mono_class_get (image, type_token, context);
3303 if (!class || !context || (!context->gclass && !context->gmethod))
3306 switch (class->byval_arg.type) {
3307 case MONO_TYPE_GENERICINST:
3308 if (!class->generic_class->inst->is_open)
3312 case MONO_TYPE_MVAR:
3318 inflated = inflate_generic_type (&class->byval_arg, context);
3322 return mono_class_from_mono_type (inflated);
3331 find_nocase (gpointer key, gpointer value, gpointer user_data)
3333 char *name = (char*)key;
3334 FindUserData *data = (FindUserData*)user_data;
3336 if (!data->value && (g_strcasecmp (name, (char*)data->key) == 0))
3337 data->value = value;
3341 * mono_class_from_name_case:
3342 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3343 * @name_space: the type namespace
3344 * @name: the type short name.
3346 * Obtains a MonoClass with a given namespace and a given name which
3347 * is located in the given MonoImage. The namespace and name
3348 * lookups are case insensitive.
3350 * You can also pass @NULL to the image, and that will lookup for
3351 * a type with the given namespace and name in all of the loaded
3352 * assemblies: notice that since there might be a name clash in this
3353 * case, passing @NULL is not encouraged if you need a precise type.
3357 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
3359 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
3360 guint32 cols [MONO_TYPEDEF_SIZE];
3365 if (image->dynamic) {
3367 FindUserData user_data;
3369 mono_loader_lock ();
3371 user_data.key = name_space;
3372 user_data.value = NULL;
3373 g_hash_table_foreach (image->name_cache, find_nocase, &user_data);
3375 if (user_data.value) {
3376 GHashTable *nspace_table = (GHashTable*)user_data.value;
3378 user_data.key = name;
3379 user_data.value = NULL;
3381 g_hash_table_foreach (nspace_table, find_nocase, &user_data);
3383 if (user_data.value)
3384 token = GPOINTER_TO_UINT (user_data.value);
3387 mono_loader_unlock ();
3390 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | token);
3396 /* add a cache if needed */
3397 for (i = 1; i <= t->rows; ++i) {
3398 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
3399 /* nested types are accessed from the nesting name */
3400 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
3401 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
3403 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3404 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3405 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
3406 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
3412 return_nested_in (MonoClass *class, char *nested) {
3414 char *s = strchr (nested, '/');
3421 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
3423 if (strcmp (found->name, nested) == 0) {
3425 return return_nested_in (found, s);
3434 * mono_class_from_name:
3435 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3436 * @name_space: the type namespace
3437 * @name: the type short name.
3439 * Obtains a MonoClass with a given namespace and a given name which
3440 * is located in the given MonoImage.
3442 * You can also pass `NULL' to the image, and that will lookup for
3443 * a type with the given namespace and name in all of the loaded
3444 * assemblies: notice that since there might be a name clash in this
3445 * case, passing NULL is not encouraged if you need a precise type.
3449 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
3451 GHashTable *nspace_table;
3452 MonoImage *loaded_image;
3458 if ((nested = strchr (name, '/'))) {
3459 int pos = nested - name;
3460 int len = strlen (name);
3463 memcpy (buf, name, len + 1);
3465 nested = buf + pos + 1;
3469 mono_loader_lock ();
3471 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
3474 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
3476 mono_loader_unlock ();
3481 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
3482 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
3483 guint32 cols [MONO_EXP_TYPE_SIZE];
3486 idx = mono_metadata_token_index (token);
3488 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
3490 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
3491 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
3492 loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
3495 class = mono_class_from_name (loaded_image, name_space, name);
3497 return return_nested_in (class, nested);
3500 g_error ("not yet implemented");
3504 token = MONO_TOKEN_TYPE_DEF | token;
3506 class = mono_class_get (image, token);
3508 return return_nested_in (class, nested);
3513 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
3514 gboolean check_interfaces)
3517 g_assert (klassc->idepth > 0);
3518 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
3519 if ((klassc->interface_id <= klass->max_interface_id) &&
3520 (klass->interface_offsets [klassc->interface_id] >= 0))
3522 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
3525 for (i = 0; i < klass->interface_count; i ++) {
3526 MonoClass *ic = klass->interfaces [i];
3531 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
3536 * MS.NET thinks interfaces are a subclass of Object, so we think it as
3539 if (klassc == mono_defaults.object_class)
3542 if (klass->generic_class && klass->generic_class->is_dynamic) {
3543 MonoDynamicGenericClass *dgclass = (MonoDynamicGenericClass *) klass->generic_class;
3545 if (!dgclass->parent)
3548 if (mono_metadata_type_equal (dgclass->parent, &klassc->byval_arg))
3550 klass = mono_class_from_mono_type (dgclass->parent);
3558 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
3561 mono_class_init (klass);
3563 if (!oklass->inited)
3564 mono_class_init (oklass);
3566 if (klass->generic_class)
3567 klass = klass->generic_class->container_class;
3568 if (oklass->generic_class)
3569 oklass = oklass->generic_class->container_class;
3571 if (MONO_CLASS_IS_INTERFACE (klass)) {
3572 if ((oklass->byval_arg.type == MONO_TYPE_VAR) || (oklass->byval_arg.type == MONO_TYPE_MVAR))
3575 /* interface_offsets might not be set for dynamic classes */
3576 if (oklass->reflection_info && !oklass->interface_offsets)
3578 * oklass might be a generic type parameter but they have
3579 * interface_offsets set.
3581 return mono_reflection_call_is_assignable_to (oklass, klass);
3583 if ((klass->interface_id <= oklass->max_interface_id) &&
3584 (oklass->interface_offsets [klass->interface_id] != -1))
3588 MonoClass *eclass, *eoclass;
3590 if (oklass->rank != klass->rank)
3593 /* vectors vs. one dimensional arrays */
3594 if (oklass->byval_arg.type != klass->byval_arg.type)
3597 eclass = klass->cast_class;
3598 eoclass = oklass->cast_class;
3602 * a is b does not imply a[] is b[] when a is a valuetype, and
3603 * b is a reference type.
3606 if (eoclass->valuetype) {
3607 if ((eclass == mono_defaults.enum_class) ||
3608 (eclass == mono_defaults.enum_class->parent) ||
3609 (eclass == mono_defaults.object_class))
3613 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
3616 if (klass == mono_defaults.object_class)
3620 * Custom version of mono_class_has_parent (oklass, klass)
3622 if (oklass->idepth >= klass->idepth) {
3623 MonoClass *parent = oklass->supertypes [klass->idepth - 1];
3625 if (parent->generic_class)
3626 parent = parent->generic_class->container_class;
3628 return klass == parent;
3635 * mono_class_get_cctor:
3637 * Returns the static constructor of @klass if it exists, NULL otherwise.
3640 mono_class_get_cctor (MonoClass *klass)
3642 MonoCachedClassInfo cached_info;
3644 if (!klass->has_cctor)
3647 if (mono_class_get_cached_class_info (klass, &cached_info))
3648 return mono_get_method (klass->image, cached_info.cctor_token, klass);
3650 return mono_class_get_method_from_name_flags (klass, ".cctor", -1, METHOD_ATTRIBUTE_SPECIAL_NAME);
3654 * mono_class_get_finalizer:
3656 * Returns the finalizer method of @klass if it exists, NULL otherwise.
3659 mono_class_get_finalizer (MonoClass *klass)
3661 MonoCachedClassInfo cached_info;
3662 static int finalize_slot = -1;
3664 if (finalize_slot < 0) {
3666 MonoClass* obj_class = mono_get_object_class ();
3667 mono_class_setup_vtable (obj_class);
3668 for (i = 0; i < obj_class->vtable_size; ++i) {
3669 MonoMethod *cm = obj_class->vtable [i];
3671 if (!strcmp (mono_method_get_name (cm), "Finalize")) {
3678 if (!klass->has_finalize)
3681 if (mono_class_get_cached_class_info (klass, &cached_info))
3682 return mono_get_method (cached_info.finalize_image, cached_info.finalize_token, NULL);
3684 mono_class_setup_vtable (klass);
3685 return klass->vtable [finalize_slot];
3690 * mono_class_needs_cctor_run:
3692 * Determines whenever the class has a static constructor and whenever it
3693 * needs to be called when executing CALLER.
3696 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
3700 method = mono_class_get_cctor (klass);
3702 return (method == caller) ? FALSE : TRUE;
3708 * mono_class_array_element_size:
3711 * Returns: the number of bytes an element of type @klass
3712 * uses when stored into an array.
3715 mono_class_array_element_size (MonoClass *klass)
3717 MonoType *type = &klass->byval_arg;
3720 switch (type->type) {
3723 case MONO_TYPE_BOOLEAN:
3727 case MONO_TYPE_CHAR:
3736 case MONO_TYPE_CLASS:
3737 case MONO_TYPE_STRING:
3738 case MONO_TYPE_OBJECT:
3739 case MONO_TYPE_SZARRAY:
3740 case MONO_TYPE_ARRAY:
3742 case MONO_TYPE_MVAR:
3743 return sizeof (gpointer);
3748 case MONO_TYPE_VALUETYPE:
3749 if (type->data.klass->enumtype) {
3750 type = type->data.klass->enum_basetype;
3751 klass = klass->element_class;
3754 return mono_class_instance_size (klass) - sizeof (MonoObject);
3755 case MONO_TYPE_GENERICINST:
3756 type = &type->data.generic_class->container_class->byval_arg;
3759 g_error ("unknown type 0x%02x in mono_class_array_element_size", type->type);
3765 * mono_array_element_size:
3766 * @ac: pointer to a #MonoArrayClass
3768 * Returns: the size of single array element.
3771 mono_array_element_size (MonoClass *ac)
3773 return mono_class_array_element_size (ac->element_class);
3777 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
3778 MonoGenericContext *context)
3780 if (image->dynamic) {
3781 MonoClass *tmp_handle_class;
3782 gpointer obj = mono_lookup_dynamic_token_class (image, token, &tmp_handle_class);
3784 g_assert (tmp_handle_class);
3786 *handle_class = tmp_handle_class;
3788 if (tmp_handle_class == mono_defaults.typehandle_class)
3789 return &((MonoClass*)obj)->byval_arg;
3794 switch (token & 0xff000000) {
3795 case MONO_TOKEN_TYPE_DEF:
3796 case MONO_TOKEN_TYPE_REF: {
3799 *handle_class = mono_defaults.typehandle_class;
3800 class = mono_class_get_full (image, token, context);
3801 mono_class_init (class);
3802 /* We return a MonoType* as handle */
3803 return &class->byval_arg;
3805 case MONO_TOKEN_TYPE_SPEC: {
3808 *handle_class = mono_defaults.typehandle_class;
3809 class = mono_class_create_from_typespec (image, token, context);
3810 mono_class_init (class);
3811 return &class->byval_arg;
3813 case MONO_TOKEN_FIELD_DEF: {
3815 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
3816 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
3817 mono_class_init (class);
3819 *handle_class = mono_defaults.fieldhandle_class;
3820 return mono_class_get_field (class, token);
3822 case MONO_TOKEN_METHOD_DEF: {
3824 meth = mono_get_method_full (image, token, NULL, context);
3826 *handle_class = mono_defaults.methodhandle_class;
3829 case MONO_TOKEN_MEMBER_REF: {
3830 guint32 cols [MONO_MEMBERREF_SIZE];
3832 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
3833 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
3834 mono_metadata_decode_blob_size (sig, &sig);
3835 if (*sig == 0x6) { /* it's a field */
3837 MonoClassField *field;
3838 field = mono_field_from_token (image, token, &klass, context);
3840 *handle_class = mono_defaults.fieldhandle_class;
3844 meth = mono_get_method_full (image, token, NULL, context);
3846 *handle_class = mono_defaults.methodhandle_class;
3851 g_warning ("Unknown token 0x%08x in ldtoken", token);
3858 * This function might need to call runtime functions so it can't be part
3859 * of the metadata library.
3861 static MonoLookupDynamicToken lookup_dynamic = NULL;
3864 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
3866 lookup_dynamic = func;
3870 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
3872 MonoClass *handle_class;
3874 return lookup_dynamic (image, token, &handle_class);
3878 mono_lookup_dynamic_token_class (MonoImage *image, guint32 token, MonoClass **handle_class)
3880 return lookup_dynamic (image, token, handle_class);
3883 static MonoGetCachedClassInfo get_cached_class_info = NULL;
3886 mono_install_get_cached_class_info (MonoGetCachedClassInfo func)
3888 get_cached_class_info = func;
3892 mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
3894 if (!get_cached_class_info)
3897 return get_cached_class_info (klass, res);
3901 mono_class_get_image (MonoClass *klass)
3903 return klass->image;
3907 * mono_class_get_element_class:
3908 * @klass: the MonoClass to act on
3910 * Returns: the element class of an array or an enumeration.
3913 mono_class_get_element_class (MonoClass *klass)
3915 return klass->element_class;
3919 * mono_class_is_valuetype:
3920 * @klass: the MonoClass to act on
3922 * Returns: true if the MonoClass represents a ValueType.
3925 mono_class_is_valuetype (MonoClass *klass)
3927 return klass->valuetype;
3931 * mono_class_is_enum:
3932 * @klass: the MonoClass to act on
3934 * Returns: true if the MonoClass represents an enumeration.
3937 mono_class_is_enum (MonoClass *klass)
3939 return klass->enumtype;
3943 * mono_class_enum_basetype:
3944 * @klass: the MonoClass to act on
3946 * Returns: the underlying type representation for an enumeration.
3949 mono_class_enum_basetype (MonoClass *klass)
3951 return klass->enum_basetype;
3955 * mono_class_get_parent
3956 * @klass: the MonoClass to act on
3958 * Returns: the parent class for this class.
3961 mono_class_get_parent (MonoClass *klass)
3963 return klass->parent;
3967 * mono_class_get_nesting_type;
3968 * @klass: the MonoClass to act on
3970 * Returns: the container type where this type is nested or NULL if this type is not a nested type.
3973 mono_class_get_nesting_type (MonoClass *klass)
3975 return klass->nested_in;
3979 * mono_class_get_rank:
3980 * @klass: the MonoClass to act on
3982 * Returns: the rank for the array (the number of dimensions).
3985 mono_class_get_rank (MonoClass *klass)
3991 * mono_class_get_flags:
3992 * @klass: the MonoClass to act on
3994 * The type flags from the TypeDef table from the metadata.
3995 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
3998 * Returns: the flags from the TypeDef table.
4001 mono_class_get_flags (MonoClass *klass)
4003 return klass->flags;
4007 * mono_class_get_name
4008 * @klass: the MonoClass to act on
4010 * Returns: the name of the class.
4013 mono_class_get_name (MonoClass *klass)
4019 * mono_class_get_namespace:
4020 * @klass: the MonoClass to act on
4022 * Returns: the namespace of the class.
4025 mono_class_get_namespace (MonoClass *klass)
4027 return klass->name_space;
4031 * mono_class_get_type:
4032 * @klass: the MonoClass to act on
4034 * This method returns the internal Type representation for the class.
4036 * Returns: the MonoType from the class.
4039 mono_class_get_type (MonoClass *klass)
4041 return &klass->byval_arg;
4045 * mono_class_get_byref_type:
4046 * @klass: the MonoClass to act on
4051 mono_class_get_byref_type (MonoClass *klass)
4053 return &klass->this_arg;
4057 * mono_class_num_fields:
4058 * @klass: the MonoClass to act on
4060 * Returns: the number of static and instance fields in the class.
4063 mono_class_num_fields (MonoClass *klass)
4065 return klass->field.count;
4069 * mono_class_num_methods:
4070 * @klass: the MonoClass to act on
4072 * Returns: the number of methods in the class.
4075 mono_class_num_methods (MonoClass *klass)
4077 return klass->method.count;
4081 * mono_class_num_properties
4082 * @klass: the MonoClass to act on
4084 * Returns: the number of properties in the class.
4087 mono_class_num_properties (MonoClass *klass)
4089 mono_class_setup_properties (klass);
4091 return klass->property.count;
4095 * mono_class_num_events:
4096 * @klass: the MonoClass to act on
4098 * Returns: the number of events in the class.
4101 mono_class_num_events (MonoClass *klass)
4103 mono_class_setup_events (klass);
4105 return klass->event.count;
4109 * mono_class_get_fields:
4110 * @klass: the MonoClass to act on
4112 * This routine is an iterator routine for retrieving the fields in a class.
4114 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4115 * iterate over all of the elements. When no more values are
4116 * available, the return value is NULL.
4118 * Returns: a @MonoClassField* on each iteration, or NULL when no more fields are available.
4121 mono_class_get_fields (MonoClass* klass, gpointer *iter)
4123 MonoClassField* field;
4127 mono_class_init (klass);
4129 mono_class_setup_fields (klass);
4130 /* start from the first */
4131 if (klass->field.count) {
4132 return *iter = &klass->fields [0];
4140 if (field < &klass->fields [klass->field.count]) {
4141 return *iter = field;
4147 * mono_class_get_methods
4148 * @klass: the MonoClass to act on
4150 * This routine is an iterator routine for retrieving the fields in a class.
4152 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4153 * iterate over all of the elements. When no more values are
4154 * available, the return value is NULL.
4156 * Returns: a MonoMethod on each iteration or NULL when no more methods are available.
4159 mono_class_get_methods (MonoClass* klass, gpointer *iter)
4161 MonoMethod** method;
4165 mono_class_init (klass);
4167 mono_class_setup_methods (klass);
4168 /* start from the first */
4169 if (klass->method.count) {
4170 *iter = &klass->methods [0];
4171 return klass->methods [0];
4179 if (method < &klass->methods [klass->method.count]) {
4187 * mono_class_get_properties:
4188 * @klass: the MonoClass to act on
4190 * This routine is an iterator routine for retrieving the properties in a class.
4192 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4193 * iterate over all of the elements. When no more values are
4194 * available, the return value is NULL.
4196 * Returns: a @MonoProperty* on each invocation, or NULL when no more are available.
4199 mono_class_get_properties (MonoClass* klass, gpointer *iter)
4201 MonoProperty* property;
4205 mono_class_init (klass);
4207 mono_class_setup_properties (klass);
4208 /* start from the first */
4209 if (klass->property.count) {
4210 return *iter = &klass->properties [0];
4218 if (property < &klass->properties [klass->property.count]) {
4219 return *iter = property;
4225 * mono_class_get_events:
4226 * @klass: the MonoClass to act on
4228 * This routine is an iterator routine for retrieving the properties in a class.
4230 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4231 * iterate over all of the elements. When no more values are
4232 * available, the return value is NULL.
4234 * Returns: a @MonoEvent* on each invocation, or NULL when no more are available.
4237 mono_class_get_events (MonoClass* klass, gpointer *iter)
4243 mono_class_init (klass);
4245 mono_class_setup_events (klass);
4246 /* start from the first */
4247 if (klass->event.count) {
4248 return *iter = &klass->events [0];
4256 if (event < &klass->events [klass->event.count]) {
4257 return *iter = event;
4263 * mono_class_get_interfaces
4264 * @klass: the MonoClass to act on
4266 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
4268 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4269 * iterate over all of the elements. When no more values are
4270 * available, the return value is NULL.
4272 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
4275 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
4281 mono_class_init (klass);
4283 /* start from the first */
4284 if (klass->interface_count) {
4285 *iter = &klass->interfaces [0];
4286 return klass->interfaces [0];
4294 if (iface < &klass->interfaces [klass->interface_count]) {
4302 * mono_class_get_nested_types
4303 * @klass: the MonoClass to act on
4305 * This routine is an iterator routine for retrieving the nested types of a class.
4307 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4308 * iterate over all of the elements. When no more values are
4309 * available, the return value is NULL.
4311 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
4314 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
4320 mono_class_init (klass);
4322 /* start from the first */
4323 if (klass->nested_classes) {
4324 *iter = klass->nested_classes;
4325 return klass->nested_classes->data;
4327 /* no nested types */
4341 * mono_field_get_name:
4342 * @field: the MonoClassField to act on
4344 * Returns: the name of the field.
4347 mono_field_get_name (MonoClassField *field)
4353 * mono_field_get_type:
4354 * @field: the MonoClassField to act on
4356 * Returns: MonoType of the field.
4359 mono_field_get_type (MonoClassField *field)
4365 * mono_field_get_type:
4366 * @field: the MonoClassField to act on
4368 * Returns: MonoClass where the field was defined.
4371 mono_field_get_parent (MonoClassField *field)
4373 return field->parent;
4377 * mono_field_get_flags;
4378 * @field: the MonoClassField to act on
4380 * The metadata flags for a field are encoded using the
4381 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4383 * Returns: the flags for the field.
4386 mono_field_get_flags (MonoClassField *field)
4388 return field->type->attrs;
4392 * mono_property_get_name:
4393 * @prop: the MonoProperty to act on
4395 * Returns: the name of the property
4398 mono_property_get_name (MonoProperty *prop)
4404 * mono_property_get_set_method
4405 * @prop: the MonoProperty to act on.
4407 * Returns: the setter method of the property (A MonoMethod)
4410 mono_property_get_set_method (MonoProperty *prop)
4416 * mono_property_get_get_method
4417 * @prop: the MonoProperty to act on.
4419 * Returns: the setter method of the property (A MonoMethod)
4422 mono_property_get_get_method (MonoProperty *prop)
4428 * mono_property_get_parent:
4429 * @prop: the MonoProperty to act on.
4431 * Returns: the MonoClass where the property was defined.
4434 mono_property_get_parent (MonoProperty *prop)
4436 return prop->parent;
4440 * mono_property_get_flags:
4441 * @prop: the MonoProperty to act on.
4443 * The metadata flags for a property are encoded using the
4444 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4446 * Returns: the flags for the property.
4449 mono_property_get_flags (MonoProperty *prop)
4455 * mono_event_get_name:
4456 * @event: the MonoEvent to act on
4458 * Returns: the name of the event.
4461 mono_event_get_name (MonoEvent *event)
4467 * mono_event_get_add_method:
4468 * @event: The MonoEvent to act on.
4470 * Returns: the @add' method for the event (a MonoMethod).
4473 mono_event_get_add_method (MonoEvent *event)
4479 * mono_event_get_remove_method:
4480 * @event: The MonoEvent to act on.
4482 * Returns: the @remove method for the event (a MonoMethod).
4485 mono_event_get_remove_method (MonoEvent *event)
4487 return event->remove;
4491 * mono_event_get_raise_method:
4492 * @event: The MonoEvent to act on.
4494 * Returns: the @raise method for the event (a MonoMethod).
4497 mono_event_get_raise_method (MonoEvent *event)
4499 return event->raise;
4503 * mono_event_get_parent:
4504 * @event: the MonoEvent to act on.
4506 * Returns: the MonoClass where the event is defined.
4509 mono_event_get_parent (MonoEvent *event)
4511 return event->parent;
4515 * mono_event_get_flags
4516 * @event: the MonoEvent to act on.
4518 * The metadata flags for an event are encoded using the
4519 * EVENT_* constants. See the tabledefs.h file for details.
4521 * Returns: the flags for the event.
4524 mono_event_get_flags (MonoEvent *event)
4526 return event->attrs;
4530 * mono_class_get_method_from_name:
4531 * @klass: where to look for the method
4532 * @name_space: name of the method
4533 * @param_count: number of parameters. -1 for any number.
4535 * Obtains a MonoMethod with a given name and number of parameters.
4536 * It only works if there are no multiple signatures for any given method name.
4539 mono_class_get_method_from_name (MonoClass *klass, const char *name, int param_count)
4541 return mono_class_get_method_from_name_flags (klass, name, param_count, 0);
4545 * mono_class_get_method_from_name_flags:
4546 * @klass: where to look for the method
4547 * @name_space: name of the method
4548 * @param_count: number of parameters. -1 for any number.
4549 * @flags: flags which must be set in the method
4551 * Obtains a MonoMethod with a given name and number of parameters.
4552 * It only works if there are no multiple signatures for any given method name.
4555 mono_class_get_method_from_name_flags (MonoClass *klass, const char *name, int param_count, int flags)
4557 MonoMethod *res = NULL;
4560 mono_class_init (klass);
4562 if (klass->methods) {
4563 mono_class_setup_methods (klass);
4564 for (i = 0; i < klass->method.count; ++i) {
4565 MonoMethod *method = klass->methods [i];
4567 if (method->name[0] == name [0] &&
4568 !strcmp (name, method->name) &&
4569 (param_count == -1 || mono_method_signature (method)->param_count == param_count) &&
4570 ((method->flags & flags) == flags)) {
4577 /* Search directly in the metadata to avoid calling setup_methods () */
4578 for (i = 0; i < klass->method.count; ++i) {
4579 guint32 cols [MONO_METHOD_SIZE];
4582 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_METHOD], klass->method.first + i, cols, MONO_METHOD_SIZE);
4584 if (!strcmp (mono_metadata_string_heap (klass->image, cols [MONO_METHOD_NAME]), name)) {
4585 method = mono_get_method (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass);
4586 if ((param_count == -1) || mono_method_signature (method)->param_count == param_count) {
4598 * mono_class_set_failure:
4599 * @klass: class in which the failure was detected
4600 * @ex_type: the kind of exception/error to be thrown (later)
4601 * @ex_data: exception data (specific to each type of exception/error)
4603 * Keep a detected failure informations in the class for later processing.
4604 * Note that only the first failure is kept.
4607 mono_class_set_failure (MonoClass *klass, guint32 ex_type, void *ex_data)
4609 if (klass->exception_type)
4611 klass->exception_type = ex_type;
4612 klass->exception_data = ex_data;
4617 * mono_class_get_exception_for_failure:
4618 * @klass: class in which the failure was detected
4620 * Return a constructed MonoException than the caller can then throw
4621 * using mono_raise_exception - or NULL if no failure is present (or
4622 * doesn't result in an exception).
4625 mono_class_get_exception_for_failure (MonoClass *klass)
4627 switch (klass->exception_type) {
4628 case MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND: {
4629 MonoDomain *domain = mono_domain_get ();
4630 MonoSecurityManager* secman = mono_security_manager_get_methods ();
4631 MonoMethod *method = klass->exception_data;
4632 guint32 error = (method) ? MONO_METADATA_INHERITANCEDEMAND_METHOD : MONO_METADATA_INHERITANCEDEMAND_CLASS;
4633 MonoObject *exc = NULL;
4637 args [1] = mono_assembly_get_object (domain, mono_image_get_assembly (klass->image));
4638 args [2] = mono_type_get_object (domain, &klass->byval_arg);
4639 args [3] = (method) ? mono_method_get_object (domain, method, NULL) : NULL;
4641 mono_runtime_invoke (secman->inheritsecurityexception, NULL, args, &exc);
4642 return (MonoException*) exc;
4644 /* TODO - handle other class related failures */