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_stringify_aname (MonoAssemblyName *aname)
164 return g_strdup_printf (
165 "%s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s",
167 aname->major, aname->minor, aname->build, aname->revision,
168 aname->culture && *aname->culture? aname->culture: "neutral",
169 aname->public_key_token [0] ? (char *)aname->public_key_token : "null");
173 _mono_type_get_assembly_name (MonoClass *klass, GString *str)
175 MonoAssembly *ta = klass->image->assembly;
177 g_string_append_printf (
178 str, ", %s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s",
180 ta->aname.major, ta->aname.minor, ta->aname.build, ta->aname.revision,
181 ta->aname.culture && *ta->aname.culture? ta->aname.culture: "neutral",
182 ta->aname.public_key_token [0] ? (char *)ta->aname.public_key_token : "null");
186 mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed,
187 MonoTypeNameFormat format)
191 switch (type->type) {
192 case MONO_TYPE_ARRAY: {
193 int i, rank = type->data.array->rank;
194 MonoTypeNameFormat nested_format;
196 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
197 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
199 mono_type_get_name_recurse (
200 &type->data.array->eklass->byval_arg, str, FALSE, nested_format);
201 g_string_append_c (str, '[');
202 for (i = 1; i < rank; i++)
203 g_string_append_c (str, ',');
204 g_string_append_c (str, ']');
205 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
206 _mono_type_get_assembly_name (type->data.array->eklass, str);
209 case MONO_TYPE_SZARRAY: {
210 MonoTypeNameFormat nested_format;
212 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
213 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
215 mono_type_get_name_recurse (
216 &type->data.klass->byval_arg, str, FALSE, nested_format);
217 g_string_append (str, "[]");
218 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
219 _mono_type_get_assembly_name (type->data.klass, str);
222 case MONO_TYPE_PTR: {
223 MonoTypeNameFormat nested_format;
225 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
226 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
228 mono_type_get_name_recurse (
229 type->data.type, str, FALSE, nested_format);
230 g_string_append (str, "*");
231 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
232 _mono_type_get_assembly_name (type->data.klass, str);
236 klass = mono_class_from_mono_type (type);
237 if (klass->nested_in) {
238 mono_type_get_name_recurse (
239 &klass->nested_in->byval_arg, str, TRUE, format);
240 if (format == MONO_TYPE_NAME_FORMAT_IL)
241 g_string_append_c (str, '.');
243 g_string_append_c (str, '+');
244 } else if (*klass->name_space) {
245 g_string_append (str, klass->name_space);
246 g_string_append_c (str, '.');
248 if (format == MONO_TYPE_NAME_FORMAT_IL) {
249 char *s = strchr (klass->name, '`');
250 int len = s ? s - klass->name : strlen (klass->name);
252 g_string_append_len (str, klass->name, len);
254 g_string_append (str, klass->name);
257 if (klass->generic_class) {
258 MonoGenericClass *gclass = klass->generic_class;
259 MonoTypeNameFormat nested_format;
262 nested_format = format == MONO_TYPE_NAME_FORMAT_FULL_NAME ?
263 MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED : format;
265 if (format == MONO_TYPE_NAME_FORMAT_IL)
266 g_string_append_c (str, '<');
268 g_string_append_c (str, '[');
269 for (i = 0; i < gclass->inst->type_argc; i++) {
270 MonoType *t = gclass->inst->type_argv [i];
273 g_string_append_c (str, ',');
274 if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
275 (t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
276 g_string_append_c (str, '[');
277 mono_type_get_name_recurse (
278 gclass->inst->type_argv [i], str, FALSE, nested_format);
279 if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
280 (t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
281 g_string_append_c (str, ']');
283 if (format == MONO_TYPE_NAME_FORMAT_IL)
284 g_string_append_c (str, '>');
286 g_string_append_c (str, ']');
287 } else if (klass->generic_container &&
288 (format != MONO_TYPE_NAME_FORMAT_FULL_NAME) &&
289 (format != MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)) {
292 if (format == MONO_TYPE_NAME_FORMAT_IL)
293 g_string_append_c (str, '<');
295 g_string_append_c (str, '[');
296 for (i = 0; i < klass->generic_container->type_argc; i++) {
298 g_string_append_c (str, ',');
299 g_string_append (str, klass->generic_container->type_params [i].name);
301 if (format == MONO_TYPE_NAME_FORMAT_IL)
302 g_string_append_c (str, '>');
304 g_string_append_c (str, ']');
306 if ((format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
307 (type->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
308 _mono_type_get_assembly_name (klass, str);
314 * mono_type_get_name:
317 * Returns: the string representation for type as required by System.Reflection.
318 * The inverse of mono_reflection_parse_type ().
321 mono_type_get_name_full (MonoType *type, MonoTypeNameFormat format)
325 if (format == MONO_TYPE_NAME_FORMAT_FULL_NAME &&
326 ((type->type == MONO_TYPE_VAR) || (type->type == MONO_TYPE_MVAR) ||
327 ((type->type == MONO_TYPE_GENERICINST) && type->data.generic_class->inst->is_open)))
330 result = g_string_new ("");
332 mono_type_get_name_recurse (type, result, FALSE, format);
335 g_string_append_c (result, '&');
337 return g_string_free (result, FALSE);
341 mono_type_get_name (MonoType *type)
343 return mono_type_get_name_full (type, MONO_TYPE_NAME_FORMAT_IL);
347 mono_type_get_underlying_type (MonoType *type)
349 switch (type->type) {
350 case MONO_TYPE_VALUETYPE:
351 if (type->data.klass->enumtype)
352 return type->data.klass->enum_basetype;
354 case MONO_TYPE_GENERICINST:
355 return mono_type_get_underlying_type (&type->data.generic_class->container_class->byval_arg);
364 mono_class_is_open_constructed_type (MonoType *t)
370 case MONO_TYPE_SZARRAY:
371 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
372 case MONO_TYPE_ARRAY:
373 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
375 return mono_class_is_open_constructed_type (t->data.type);
376 case MONO_TYPE_GENERICINST: {
377 MonoGenericClass *gclass = t->data.generic_class;
380 if (mono_class_is_open_constructed_type (&gclass->container_class->byval_arg))
382 for (i = 0; i < gclass->inst->type_argc; i++)
383 if (mono_class_is_open_constructed_type (gclass->inst->type_argv [i]))
392 static MonoGenericClass *
393 inflate_generic_class (MonoGenericClass *ogclass, MonoGenericContext *context)
395 MonoInflatedGenericClass *igclass;
396 MonoGenericClass *ngclass, *cached;
398 if (ogclass->is_dynamic) {
399 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
400 igclass = &dgclass->generic_class;
401 ngclass = &igclass->generic_class;
402 ngclass->is_inflated = 1;
403 ngclass->is_dynamic = 1;
405 igclass = g_new0 (MonoInflatedGenericClass, 1);
406 ngclass = &igclass->generic_class;
407 ngclass->is_inflated = 1;
412 ngclass->inst = mono_metadata_inflate_generic_inst (ogclass->inst, context);
414 igclass->klass = NULL;
416 ngclass->context = g_new0 (MonoGenericContext, 1);
417 ngclass->context->container = context->container;
418 ngclass->context->gclass = ngclass;
421 cached = mono_metadata_lookup_generic_class (ngclass);
422 mono_loader_unlock ();
424 g_free (ngclass->context);
433 inflate_generic_type (MonoType *type, MonoGenericContext *context)
435 switch (type->type) {
437 if (context->gmethod && context->gmethod->inst->type_argv)
439 context->gmethod->inst->type_argv [type->data.generic_param->num],
446 context->gclass->inst->type_argv [type->data.generic_param->num],
450 case MONO_TYPE_SZARRAY: {
451 MonoClass *eclass = type->data.klass;
452 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
455 nt = dup_type (type, type);
456 nt->data.klass = mono_class_from_mono_type (inflated);
459 case MONO_TYPE_ARRAY: {
460 MonoClass *eclass = type->data.array->eklass;
461 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
464 nt = dup_type (type, type);
465 nt->data.array = g_memdup (nt->data.array, sizeof (MonoArrayType));
466 nt->data.array->eklass = mono_class_from_mono_type (inflated);
469 case MONO_TYPE_GENERICINST: {
470 MonoGenericClass *ngclass = inflate_generic_class (type->data.generic_class, context);
471 MonoType *nt = dup_type (type, type);
472 nt->data.generic_class = ngclass;
475 case MONO_TYPE_CLASS:
476 case MONO_TYPE_VALUETYPE: {
477 MonoClass *klass = type->data.klass;
478 MonoGenericClass *gclass;
481 if (!klass->generic_container)
484 gclass = inflate_generic_class (klass->generic_container->context.gclass, context);
486 nt = dup_type (type, type);
487 nt->type = MONO_TYPE_GENERICINST;
488 nt->data.generic_class = gclass;
497 MonoInflatedGenericClass*
498 mono_get_inflated_generic_class (MonoGenericClass *gclass)
500 g_assert (gclass->is_inflated);
501 mono_class_create_generic ((MonoInflatedGenericClass *) gclass);
502 return (MonoInflatedGenericClass *) gclass;
506 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
508 MonoType *inflated = inflate_generic_type (type, context);
511 return dup_type (type, type);
513 mono_stats.inflated_type_count++;
518 mono_class_inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
520 MonoMethodSignature *res;
527 res = mono_metadata_signature_alloc (image, sig->param_count);
528 res->ret = mono_class_inflate_generic_type (sig->ret, context);
529 is_open = mono_class_is_open_constructed_type (res->ret);
530 for (i = 0; i < sig->param_count; ++i) {
531 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
533 is_open = mono_class_is_open_constructed_type (res->params [i]);
535 res->hasthis = sig->hasthis;
536 res->explicit_this = sig->explicit_this;
537 res->call_convention = sig->call_convention;
538 res->pinvoke = sig->pinvoke;
539 res->generic_param_count = sig->generic_param_count;
540 res->sentinelpos = sig->sentinelpos;
541 res->has_type_parameters = is_open;
542 res->is_inflated = 1;
546 static MonoMethodHeader*
547 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
549 MonoMethodHeader *res;
551 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
552 res->code = header->code;
553 res->code_size = header->code_size;
554 res->max_stack = header->max_stack;
555 res->num_clauses = header->num_clauses;
556 res->init_locals = header->init_locals;
557 res->num_locals = header->num_locals;
558 res->clauses = header->clauses;
559 for (i = 0; i < header->num_locals; ++i)
560 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
564 static MonoGenericContext *
565 inflate_generic_context (MonoGenericContext *context, MonoGenericContext *inflate_with)
567 MonoGenericContext *res = g_new0 (MonoGenericContext, 1);
569 res->container = context->container;
572 res->gclass = inflate_generic_class (context->gclass, inflate_with);
574 if (context->gmethod) {
575 res->gmethod = g_new0 (MonoGenericMethod, 1);
576 res->gmethod->generic_class = res->gclass;
578 res->gmethod->container = context->gmethod->container;
579 res->gmethod->inst = mono_metadata_inflate_generic_inst (context->gmethod->inst, inflate_with);
581 res->gmethod = inflate_with->gmethod;
587 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context)
589 MonoMethodInflated *result;
591 if (method->is_inflated || mono_method_signature_full (method, context)->is_inflated) {
592 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
594 context = inflate_generic_context (imethod->context, context);
595 method = imethod->declaring;
598 mono_stats.inflated_method_count++;
600 result = g_new0 (MonoMethodInflated, 1);
601 result->nmethod = *(MonoMethodNormal*)method;
602 result->nmethod.method.is_inflated = 1;
603 result->context = context;
604 result->declaring = method;
606 if (result->nmethod.method.klass->generic_class)
607 result->nmethod.method.klass = result->nmethod.method.klass->generic_class->container_class;
609 return (MonoMethod *) result;
613 mono_get_inflated_method (MonoMethod *method)
615 MonoMethodInflated *imethod, *res;
616 MonoMethodHeader *mh;
620 if (!method->is_inflated)
623 imethod = (MonoMethodInflated *) method;
624 if (imethod->inflated)
625 return (MonoMethod *) imethod->inflated;
627 mono_stats.inflated_method_count_2++;
628 mono_stats.generics_metadata_size +=
629 sizeof (MonoMethodInflated) - sizeof (MonoMethodNormal);
631 res = g_new0 (MonoMethodInflated, 1);
633 res->inflated = imethod->inflated = res;
635 mh = mono_method_get_header (method);
637 res->nmethod.header = inflate_generic_header (mh, imethod->context);
639 dtype = mono_class_inflate_generic_type (&method->klass->byval_arg, imethod->context);
640 rklass = res->nmethod.method.klass = mono_class_from_mono_type (dtype);
642 res->nmethod.method.signature = mono_class_inflate_generic_signature (
643 method->klass->image, mono_method_signature (method), imethod->context);
645 return (MonoMethod *) res;
649 * mono_class_setup_fields:
650 * @m: pointer to the metadata.
651 * @class: The class to initialize
653 * Initializes the class->fields.
656 mono_class_setup_fields (MonoClass *class)
658 MonoImage *m = class->image;
659 const int top = class->field.count;
660 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
661 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
662 int i, blittable = TRUE, real_size = 0;
664 guint32 packing_size = 0;
665 gboolean explicit_size;
666 MonoClassField *field;
668 if (class->size_inited)
671 class->instance_size = 0;
672 class->class_size = 0;
675 if (!class->parent->size_inited)
676 mono_class_setup_fields (class->parent);
677 class->instance_size += class->parent->instance_size;
678 class->min_align = class->parent->min_align;
679 /* we use |= since it may have been set already */
680 class->has_references |= class->parent->has_references;
681 blittable = class->parent->blittable;
683 class->instance_size = sizeof (MonoObject);
684 class->min_align = 1;
687 /* Get the real size */
688 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
691 g_assert ((packing_size & 0xfffffff0) == 0);
692 class->packing_size = packing_size;
693 real_size += class->instance_size;
697 if (explicit_size && real_size) {
698 class->instance_size = MAX (real_size, class->instance_size);
700 class->size_inited = 1;
701 class->blittable = blittable;
705 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT)
708 /* Prevent infinite loops if the class references itself */
709 class->size_inited = 1;
711 class->fields = g_new0 (MonoClassField, top);
714 * Fetch all the field information.
716 for (i = 0; i < top; i++){
718 guint32 cols [MONO_FIELD_SIZE];
719 int idx = class->field.first + i;
720 MonoGenericContainer *container = NULL;
722 field = &class->fields [i];
723 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
724 /* The name is needed for fieldrefs */
725 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
726 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
727 mono_metadata_decode_value (sig, &sig);
728 /* FIELD signature == 0x06 */
729 g_assert (*sig == 0x06);
730 if (class->generic_container)
731 container = class->generic_container;
732 else if (class->generic_class) {
733 MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
734 MonoClass *gklass = class->generic_class->container_class;
736 container = gklass->generic_container;
737 g_assert (container);
739 ifield->generic_type = gklass->fields [i].type;
740 field->generic_info = ifield;
742 field->type = mono_metadata_parse_type_full (
743 m, (MonoGenericContext *) container, MONO_PARSE_FIELD,
744 cols [MONO_FIELD_FLAGS], sig + 1, &sig);
745 if (mono_field_is_deleted (field))
747 if (class->generic_class) {
748 field->type = mono_class_inflate_generic_type (
749 field->type, class->generic_class->context);
750 field->type->attrs = cols [MONO_FIELD_FLAGS];
753 field->parent = class;
755 /* Only do these checks if we still think this type is blittable */
756 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
757 if (field->type->byref || MONO_TYPE_IS_REFERENCE (field->type)) {
760 MonoClass *field_class = mono_class_from_mono_type (field->type);
761 if (!field_class || !field_class->blittable)
766 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
767 mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
768 if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
769 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, field->name);
772 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
773 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
775 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
776 field->data = mono_image_rva_map (class->image, rva);
779 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
780 class->enum_basetype = field->type;
781 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
782 blittable = class->element_class->blittable;
785 /* The def_value of fields is compute lazily during vtable creation */
788 if (class == mono_defaults.string_class)
791 class->blittable = blittable;
793 if (class->enumtype && !class->enum_basetype) {
794 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
797 if (explicit_size && real_size) {
798 class->instance_size = MAX (real_size, class->instance_size);
801 mono_class_layout_fields (class);
804 /* useful until we keep track of gc-references in corlib etc. */
805 #define IS_GC_REFERENCE(t) ((t)->type == MONO_TYPE_U || (t)->type == MONO_TYPE_I || (t)->type == MONO_TYPE_PTR)
808 * LOCKING: this is supposed to be called with the loader lock held.
811 mono_class_layout_fields (MonoClass *class)
814 const int top = class->field.count;
815 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
816 guint32 pass, passes, real_size;
817 gboolean gc_aware_layout = FALSE;
818 MonoClassField *field;
820 if (class->generic_container ||
821 (class->generic_class && class->generic_class->inst->is_open))
825 * Enable GC aware auto layout: in this mode, reference
826 * fields are grouped together inside objects, increasing collector
828 * Requires that all classes whose layout is known to native code be annotated
829 * with [StructLayout (LayoutKind.Sequential)]
830 * Value types have gc_aware_layout disabled by default, as per
831 * what the default is for other runtimes.
833 /* corlib is missing [StructLayout] directives in many places */
834 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
835 if (class->image != mono_defaults.corlib &&
836 class->byval_arg.type != MONO_TYPE_VALUETYPE)
837 gc_aware_layout = TRUE;
840 for (i = 0; i < top; i++) {
843 field = &class->fields [i];
845 ftype = mono_type_get_underlying_type (field->type);
846 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_from_mono_type (ftype)->has_references))) {
847 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
848 class->has_static_refs = TRUE;
850 class->has_references = TRUE;
855 * Compute field layout and total size (not considering static fields)
859 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
860 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
867 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
871 real_size = class->parent->instance_size;
873 real_size = sizeof (MonoObject);
875 for (pass = 0; pass < passes; ++pass) {
876 for (i = 0; i < top; i++){
879 field = &class->fields [i];
881 if (mono_field_is_deleted (field))
883 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
886 if (gc_aware_layout) {
888 * We process fields with reference type in the first pass,
889 * and fields with non-reference type in the second pass.
890 * We use IS_POINTER instead of IS_REFERENCE because in
891 * some internal structures, we store GC_MALLOCed memory
892 * in IntPtr fields...
894 if (MONO_TYPE_IS_POINTER (field->type)) {
903 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
904 (strcmp (field->name, "$PRIVATE$") == 0)) {
905 /* This field is a hack inserted by MCS to empty structures */
909 size = mono_type_size (field->type, &align);
911 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
912 align = class->packing_size ? MIN (class->packing_size, align): align;
913 class->min_align = MAX (align, class->min_align);
914 field->offset = real_size;
915 field->offset += align - 1;
916 field->offset &= ~(align - 1);
917 real_size = field->offset + size;
920 class->instance_size = MAX (real_size, class->instance_size);
922 if (class->instance_size & (class->min_align - 1)) {
923 class->instance_size += class->min_align - 1;
924 class->instance_size &= ~(class->min_align - 1);
928 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
930 for (i = 0; i < top; i++) {
933 field = &class->fields [i];
936 * There must be info about all the fields in a type if it
937 * uses explicit layout.
940 if (mono_field_is_deleted (field))
942 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
945 size = mono_type_size (field->type, &align);
948 * When we get here, field->offset is already set by the
949 * loader (for either runtime fields or fields loaded from metadata).
950 * The offset is from the start of the object: this works for both
951 * classes and valuetypes.
953 field->offset += sizeof (MonoObject);
958 real_size = MAX (real_size, size + field->offset);
960 class->instance_size = MAX (real_size, class->instance_size);
964 class->size_inited = 1;
967 * Compute static field layout and size
969 for (i = 0; i < top; i++){
972 field = &class->fields [i];
974 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
976 if (mono_field_is_deleted (field))
979 size = mono_type_size (field->type, &align);
980 field->offset = class->class_size;
981 field->offset += align - 1;
982 field->offset &= ~(align - 1);
983 class->class_size = field->offset + size;
988 * mono_class_setup_methods:
990 * Initializes the 'methods' array in the klass.
991 * Calling this method should be avoided if possible since it allocates a lot
992 * of long-living MonoMethod structures.
995 mono_class_setup_methods (MonoClass *class)
998 MonoMethod **methods;
1000 if (class->methods || class->generic_class)
1003 mono_loader_lock ();
1005 if (class->methods) {
1006 mono_loader_unlock ();
1010 //printf ("INIT: %s.%s\n", class->name_space, class->name);
1012 if (!class->generic_class && !class->methods) {
1013 methods = g_new (MonoMethod*, class->method.count);
1014 for (i = 0; i < class->method.count; ++i) {
1015 methods [i] = mono_get_method (class->image,
1016 MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
1020 if (MONO_CLASS_IS_INTERFACE (class))
1021 for (i = 0; i < class->method.count; ++i)
1022 methods [i]->slot = i;
1024 class->methods = methods;
1026 mono_loader_unlock ();
1031 mono_class_setup_properties (MonoClass *class)
1033 guint startm, endm, i, j;
1034 guint32 cols [MONO_PROPERTY_SIZE];
1035 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
1036 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
1037 MonoProperty *properties;
1039 if (class->properties)
1042 mono_loader_lock ();
1044 if (class->properties) {
1045 mono_loader_unlock ();
1049 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->property.last);
1050 class->property.count = class->property.last - class->property.first;
1052 if (class->property.count)
1053 mono_class_setup_methods (class);
1055 properties = g_new0 (MonoProperty, class->property.count);
1056 for (i = class->property.first; i < class->property.last; ++i) {
1057 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
1058 properties [i - class->property.first].parent = class;
1059 properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
1060 properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
1062 startm = mono_metadata_methods_from_property (class->image, i, &endm);
1063 for (j = startm; j < endm; ++j) {
1064 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
1065 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1066 case METHOD_SEMANTIC_SETTER:
1067 properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1069 case METHOD_SEMANTIC_GETTER:
1070 properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1078 class->properties = properties;
1080 mono_loader_unlock ();
1084 mono_class_setup_events (MonoClass *class)
1086 guint startm, endm, i, j;
1087 guint32 cols [MONO_EVENT_SIZE];
1088 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
1089 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
1094 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &class->event.last);
1095 class->event.count = class->event.last - class->event.first;
1097 if (class->event.count)
1098 mono_class_setup_methods (class);
1100 class->events = g_new0 (MonoEvent, class->event.count);
1101 for (i = class->event.first; i < class->event.last; ++i) {
1102 MonoEvent *event = &class->events [i - class->event.first];
1104 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
1105 event->parent = class;
1106 event->attrs = cols [MONO_EVENT_FLAGS];
1107 event->name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
1109 startm = mono_metadata_methods_from_event (class->image, i, &endm);
1110 for (j = startm; j < endm; ++j) {
1111 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
1112 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1113 case METHOD_SEMANTIC_ADD_ON:
1114 event->add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1116 case METHOD_SEMANTIC_REMOVE_ON:
1117 event->remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1119 case METHOD_SEMANTIC_FIRE:
1120 event->raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1122 case METHOD_SEMANTIC_OTHER: {
1125 if (event->other == NULL) {
1126 event->other = g_new0 (MonoMethod*, 1);
1128 while (event->other [n])
1130 event->other = g_realloc (event->other, (n + 1) * sizeof (MonoMethod*));
1132 event->other [n] = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1143 mono_get_unique_iid (MonoClass *class)
1145 static GHashTable *iid_hash = NULL;
1146 static guint iid = 0;
1151 g_assert (MONO_CLASS_IS_INTERFACE (class));
1153 mono_loader_lock ();
1156 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
1158 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
1160 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
1161 mono_loader_unlock ();
1163 return GPOINTER_TO_INT (value);
1165 g_hash_table_insert (iid_hash, str, GINT_TO_POINTER (iid));
1169 mono_loader_unlock ();
1171 g_assert (iid <= 65535);
1176 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
1181 for (i = 0; i < klass->interface_count; i++) {
1182 ic = klass->interfaces [i];
1185 *res = g_ptr_array_new ();
1186 g_ptr_array_add (*res, ic);
1187 mono_class_init (ic);
1189 collect_implemented_interfaces_aux (ic, res);
1194 mono_class_get_implemented_interfaces (MonoClass *klass)
1196 GPtrArray *res = NULL;
1198 collect_implemented_interfaces_aux (klass, &res);
1202 typedef struct _IOffsetInfo IOffsetInfo;
1203 struct _IOffsetInfo {
1207 int data [MONO_ZERO_LEN_ARRAY];
1210 static IOffsetInfo *cached_offset_info = NULL;
1211 static int next_offset_info_size = 128;
1214 cache_interface_offsets (int max_iid, int *data)
1216 IOffsetInfo *cached_info;
1219 for (cached_info = cached_offset_info; cached_info; cached_info = cached_info->next) {
1220 cached = cached_info->data;
1221 while (cached < cached_info->data + cached_info->size && *cached) {
1222 if (*cached == max_iid) {
1223 int i, matched = TRUE;
1225 for (i = 0; i < max_iid; ++i) {
1226 if (cached [i] != data [i]) {
1235 cached += *cached + 1;
1239 /* find a free slot */
1240 for (cached_info = cached_offset_info; cached_info; cached_info = cached_info->next) {
1241 if (cached_info->size - cached_info->next_free >= max_iid + 1) {
1242 cached = &cached_info->data [cached_info->next_free];
1243 *cached++ = max_iid;
1244 memcpy (cached, data, max_iid * sizeof (int));
1245 cached_info->next_free += max_iid + 1;
1249 /* allocate a new chunk */
1250 if (max_iid + 1 < next_offset_info_size) {
1251 new_size = next_offset_info_size;
1252 if (next_offset_info_size < 4096)
1253 next_offset_info_size += next_offset_info_size >> 2;
1255 new_size = max_iid + 1;
1257 cached_info = g_malloc0 (sizeof (IOffsetInfo) + sizeof (int) * new_size);
1258 cached_info->size = new_size;
1259 /*g_print ("allocated %d offset entries at %p (total: %d)\n", new_size, cached_info->data, offset_info_total_size);*/
1260 cached = &cached_info->data [0];
1261 *cached++ = max_iid;
1262 memcpy (cached, data, max_iid * sizeof (int));
1263 cached_info->next_free += max_iid + 1;
1264 cached_info->next = cached_offset_info;
1265 cached_offset_info = cached_info;
1270 setup_interface_offsets (MonoClass *class, int cur_slot)
1277 /* compute maximum number of slots and maximum interface id */
1279 for (k = class; k ; k = k->parent) {
1280 for (i = 0; i < k->interface_count; i++) {
1281 ic = k->interfaces [i];
1284 mono_class_init (ic);
1286 if (max_iid < ic->interface_id)
1287 max_iid = ic->interface_id;
1289 ifaces = mono_class_get_implemented_interfaces (k);
1291 for (i = 0; i < ifaces->len; ++i) {
1292 ic = g_ptr_array_index (ifaces, i);
1293 if (max_iid < ic->interface_id)
1294 max_iid = ic->interface_id;
1296 g_ptr_array_free (ifaces, TRUE);
1300 if (MONO_CLASS_IS_INTERFACE (class)) {
1301 if (max_iid < class->interface_id)
1302 max_iid = class->interface_id;
1304 class->max_interface_id = max_iid;
1305 /* compute vtable offset for interfaces */
1306 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
1308 for (i = 0; i <= max_iid; i++)
1309 class->interface_offsets [i] = -1;
1311 ifaces = mono_class_get_implemented_interfaces (class);
1313 for (i = 0; i < ifaces->len; ++i) {
1314 ic = g_ptr_array_index (ifaces, i);
1315 class->interface_offsets [ic->interface_id] = cur_slot;
1316 cur_slot += ic->method.count;
1318 g_ptr_array_free (ifaces, TRUE);
1321 for (k = class->parent; k ; k = k->parent) {
1322 ifaces = mono_class_get_implemented_interfaces (k);
1324 for (i = 0; i < ifaces->len; ++i) {
1325 ic = g_ptr_array_index (ifaces, i);
1327 if (class->interface_offsets [ic->interface_id] == -1) {
1328 int io = k->interface_offsets [ic->interface_id];
1332 class->interface_offsets [ic->interface_id] = io;
1335 g_ptr_array_free (ifaces, TRUE);
1339 if (MONO_CLASS_IS_INTERFACE (class))
1340 class->interface_offsets [class->interface_id] = cur_slot;
1342 cached_data = cache_interface_offsets (max_iid + 1, class->interface_offsets);
1343 g_free (class->interface_offsets);
1344 class->interface_offsets = cached_data;
1350 mono_class_setup_vtable (MonoClass *class)
1352 MonoMethod **overrides;
1353 MonoGenericContext *context;
1359 mono_class_setup_methods (class);
1361 if (MONO_CLASS_IS_INTERFACE (class))
1364 mono_loader_lock ();
1366 if (class->vtable) {
1367 mono_loader_unlock ();
1371 if (class->generic_class)
1372 context = class->generic_class->context;
1374 context = (MonoGenericContext *) class->generic_container;
1376 mono_class_get_overrides_full (class->image, class->type_token, &overrides, &onum, context);
1377 mono_class_setup_vtable_general (class, overrides, onum);
1380 mono_loader_unlock ();
1384 setup_generic_vtable (MonoClass *class, MonoMethod **overrides, int onum)
1389 gklass = class->generic_class->container_class;
1391 mono_class_init (gklass);
1392 class->vtable_size = gklass->vtable_size;
1394 class->vtable = g_new0 (MonoMethod*, class->vtable_size);
1395 memcpy (class->vtable, gklass->vtable, sizeof (MonoMethod*) * class->vtable_size);
1397 for (i = 0; i < class->vtable_size; i++) {
1398 MonoMethod *m = class->vtable [i];
1403 m = mono_class_inflate_generic_method (m, class->generic_class->context);
1404 class->vtable [i] = m;
1407 class->max_interface_id = gklass->max_interface_id;
1408 class->interface_offsets = g_new0 (gint, gklass->max_interface_id + 1);
1409 memcpy (class->interface_offsets, gklass->interface_offsets,
1410 sizeof (gint) * (gklass->max_interface_id + 1));
1414 * LOCKING: this is supposed to be called with the loader lock held.
1417 mono_class_setup_vtable_general (MonoClass *class, MonoMethod **overrides, int onum)
1420 MonoMethod **vtable;
1421 int i, max_vtsize = 0, max_iid, cur_slot = 0;
1423 GHashTable *override_map = NULL;
1424 gboolean security_enabled = mono_is_security_manager_active ();
1429 if (class->generic_class) {
1430 setup_generic_vtable (class, overrides, onum);
1434 ifaces = mono_class_get_implemented_interfaces (class);
1436 for (i = 0; i < ifaces->len; i++) {
1437 MonoClass *ic = g_ptr_array_index (ifaces, i);
1438 max_vtsize += ic->method.count;
1440 g_ptr_array_free (ifaces, TRUE);
1443 if (class->parent) {
1444 mono_class_init (class->parent);
1445 mono_class_setup_vtable (class->parent);
1446 max_vtsize += class->parent->vtable_size;
1447 cur_slot = class->parent->vtable_size;
1450 max_vtsize += class->method.count;
1452 vtable = alloca (sizeof (gpointer) * max_vtsize);
1453 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
1455 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
1457 cur_slot = setup_interface_offsets (class, cur_slot);
1458 max_iid = class->max_interface_id;
1460 if (class->parent && class->parent->vtable_size)
1461 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
1463 /* override interface methods */
1464 for (i = 0; i < onum; i++) {
1465 MonoMethod *decl = overrides [i*2];
1466 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
1468 mono_class_setup_methods (decl->klass);
1469 g_assert (decl->slot != -1);
1470 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
1471 vtable [dslot] = overrides [i*2 + 1];
1472 vtable [dslot]->slot = dslot;
1474 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1476 g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
1480 for (k = class; k ; k = k->parent) {
1483 ifaces = mono_class_get_implemented_interfaces (k);
1485 nifaces = ifaces->len;
1486 for (i = 0; i < nifaces; i++) {
1489 ic = g_ptr_array_index (ifaces, i);
1490 io = k->interface_offsets [ic->interface_id];
1493 g_assert (io <= max_vtsize);
1496 mono_class_setup_methods (ic);
1497 for (l = 0; l < ic->method.count; l++) {
1498 MonoMethod *im = ic->methods [l];
1500 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1503 for (j = 0; j < class->method.count; ++j) {
1504 MonoMethod *cm = class->methods [j];
1505 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1506 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
1507 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
1509 if (!strcmp(cm->name, im->name) &&
1510 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1512 /* CAS - SecurityAction.InheritanceDemand on interface */
1513 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1514 mono_secman_inheritancedemand_method (cm, im);
1517 g_assert (io + l <= max_vtsize);
1518 vtable [io + l] = cm;
1523 /* already implemented */
1524 if (io >= k->vtable_size)
1528 for (l = 0; l < ic->method.count; l++) {
1529 MonoMethod *im = ic->methods [l];
1532 g_assert (io + l <= max_vtsize);
1534 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1537 for (k1 = class; k1; k1 = k1->parent) {
1538 for (j = 0; j < k1->method.count; ++j) {
1539 MonoMethod *cm = k1->methods [j];
1541 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1542 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
1545 if (!strcmp(cm->name, im->name) &&
1546 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1548 /* CAS - SecurityAction.InheritanceDemand on interface */
1549 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1550 mono_secman_inheritancedemand_method (cm, im);
1553 g_assert (io + l <= max_vtsize);
1554 vtable [io + l] = cm;
1559 g_assert (io + l <= max_vtsize);
1560 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1565 for (l = 0; l < ic->method.count; l++) {
1566 MonoMethod *im = ic->methods [l];
1567 char *qname, *fqname, *cname, *the_cname;
1570 if (vtable [io + l])
1573 if (ic->generic_class) {
1574 the_cname = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
1578 cname = (char*)ic->name;
1581 qname = g_strconcat (cname, ".", im->name, NULL);
1582 if (ic->name_space && ic->name_space [0])
1583 fqname = g_strconcat (ic->name_space, ".", cname, ".", im->name, NULL);
1587 for (k1 = class; k1; k1 = k1->parent) {
1588 for (j = 0; j < k1->method.count; ++j) {
1589 MonoMethod *cm = k1->methods [j];
1591 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1594 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
1595 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1597 /* CAS - SecurityAction.InheritanceDemand on interface */
1598 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1599 mono_secman_inheritancedemand_method (cm, im);
1602 g_assert (io + l <= max_vtsize);
1603 vtable [io + l] = cm;
1614 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
1615 for (l = 0; l < ic->method.count; l++) {
1617 MonoMethod *im = ic->methods [l];
1618 if (im->flags & METHOD_ATTRIBUTE_STATIC)
1620 g_assert (io + l <= max_vtsize);
1623 * If one of our parents already implements this interface
1624 * we can inherit the implementation.
1626 if (!(vtable [io + l])) {
1627 MonoClass *parent = class->parent;
1629 if ((ic->interface_id <= parent->max_interface_id) &&
1630 (parent->interface_offsets [ic->interface_id]) &&
1632 vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
1635 if (!(vtable [io + l])) {
1636 for (j = 0; j < onum; ++j) {
1637 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
1638 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
1640 msig = mono_signature_get_desc (mono_method_signature (im), FALSE);
1641 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
1642 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
1644 for (j = 0; j < class->method.count; ++j) {
1645 MonoMethod *cm = class->methods [j];
1646 msig = mono_signature_get_desc (mono_method_signature (cm), FALSE);
1648 printf ("METHOD %s(%s)\n", cm->name, msig);
1651 g_assert_not_reached ();
1656 for (l = 0; l < ic->method.count; l++) {
1657 MonoMethod *im = vtable [io + l];
1660 g_assert (io + l <= max_vtsize);
1662 /* FIXME: why do we need this ? */
1664 /* g_assert_not_reached (); */
1670 g_ptr_array_free (ifaces, TRUE);
1673 for (i = 0; i < class->method.count; ++i) {
1676 cm = class->methods [i];
1679 * Non-virtual method have no place in the vtable.
1680 * This also catches static methods (since they are not virtual).
1682 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1686 * If the method is REUSE_SLOT, we must check in the
1687 * base class for a method to override.
1689 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
1691 for (k = class->parent; k ; k = k->parent) {
1693 for (j = 0; j < k->method.count; ++j) {
1694 MonoMethod *m1 = k->methods [j];
1695 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
1697 if (!strcmp(cm->name, m1->name) &&
1698 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (m1))) {
1700 /* CAS - SecurityAction.InheritanceDemand */
1701 if (security_enabled && (m1->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1702 mono_secman_inheritancedemand_method (cm, m1);
1705 slot = k->methods [j]->slot;
1706 g_assert (cm->slot < max_vtsize);
1708 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1709 g_hash_table_insert (override_map, m1, cm);
1721 cm->slot = cur_slot++;
1723 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT) && ! mono_method_signature (cm)->generic_param_count)
1724 vtable [cm->slot] = cm;
1727 /* override non interface methods */
1728 for (i = 0; i < onum; i++) {
1729 MonoMethod *decl = overrides [i*2];
1730 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
1731 g_assert (decl->slot != -1);
1732 vtable [decl->slot] = overrides [i*2 + 1];
1733 overrides [i * 2 + 1]->slot = decl->slot;
1735 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1736 g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
1741 * If a method occupies more than one place in the vtable, and it is
1742 * overriden, then change the other occurances too.
1745 for (i = 0; i < max_vtsize; ++i)
1747 MonoMethod *cm = g_hash_table_lookup (override_map, vtable [i]);
1752 g_hash_table_destroy (override_map);
1755 if (class->generic_class) {
1756 MonoClass *gklass = class->generic_class->container_class;
1758 mono_class_init (gklass);
1759 class->vtable_size = gklass->vtable_size;
1761 class->vtable_size = cur_slot;
1763 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
1764 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
1766 if (mono_print_vtable) {
1769 for (i = 0; i <= max_iid; i++)
1770 if (class->interface_offsets [i] != -1)
1773 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
1774 class->name, class->vtable_size, icount);
1776 for (i = 0; i < class->vtable_size; ++i) {
1781 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
1782 cm->klass->name_space, cm->klass->name,
1789 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
1790 class->name, max_iid);
1792 for (i = 0; i < class->interface_count; i++) {
1793 ic = class->interfaces [i];
1794 printf (" slot %03d(%03d) %s.%s\n",
1795 class->interface_offsets [ic->interface_id],
1796 ic->method.count, ic->name_space, ic->name);
1799 for (k = class->parent; k ; k = k->parent) {
1800 for (i = 0; i < k->interface_count; i++) {
1801 ic = k->interfaces [i];
1802 printf (" slot %03d(%03d) %s.%s\n",
1803 class->interface_offsets [ic->interface_id],
1804 ic->method.count, ic->name_space, ic->name);
1813 * @class: the class to initialize
1815 * compute the instance_size, class_size and other infos that cannot be
1816 * computed at mono_class_get() time. Also compute a generic vtable and
1817 * the method slot numbers. We use this infos later to create a domain
1821 mono_class_init (MonoClass *class)
1824 static MonoMethod *default_ghc = NULL;
1825 static MonoMethod *default_finalize = NULL;
1826 static int finalize_slot = -1;
1827 static int ghc_slot = -1;
1828 MonoCachedClassInfo cached_info;
1829 gboolean has_cached_info;
1836 /*g_print ("Init class %s\n", class->name);*/
1838 /* We do everything inside the lock to prevent races */
1839 mono_loader_lock ();
1841 if (class->inited) {
1842 mono_loader_unlock ();
1843 /* Somebody might have gotten in before us */
1847 if (class->init_pending) {
1848 mono_loader_unlock ();
1849 /* this indicates a cyclic dependency */
1850 g_error ("pending init %s.%s\n", class->name_space, class->name);
1853 class->init_pending = 1;
1855 /* CAS - SecurityAction.InheritanceDemand */
1856 if (mono_is_security_manager_active () && class->parent && (class->parent->flags & TYPE_ATTRIBUTE_HAS_SECURITY)) {
1857 mono_secman_inheritancedemand_class (class, class->parent);
1860 if (mono_debugger_start_class_init_func)
1861 mono_debugger_start_class_init_func (class);
1863 mono_stats.initialized_class_count++;
1865 if (class->generic_class && !class->generic_class->is_dynamic) {
1866 MonoInflatedGenericClass *gclass;
1869 gclass = mono_get_inflated_generic_class (class->generic_class);
1870 gklass = gclass->generic_class.container_class;
1872 mono_stats.generic_class_count++;
1874 class->method = gklass->method;
1875 class->field = gklass->field;
1877 mono_class_init (gklass);
1878 mono_class_setup_methods (gklass);
1879 mono_class_setup_properties (gklass);
1881 if (MONO_CLASS_IS_INTERFACE (class))
1882 class->interface_id = mono_get_unique_iid (class);
1884 g_assert (class->method.count == gklass->method.count);
1885 class->methods = g_new0 (MonoMethod *, class->method.count);
1887 for (i = 0; i < class->method.count; i++) {
1888 MonoMethod *inflated = mono_class_inflate_generic_method (
1889 gklass->methods [i], gclass->generic_class.context);
1891 class->methods [i] = mono_get_inflated_method (inflated);
1894 class->property = gklass->property;
1895 class->properties = g_new0 (MonoProperty, class->property.count);
1897 for (i = 0; i < class->property.count; i++) {
1898 MonoProperty *prop = &class->properties [i];
1900 *prop = gklass->properties [i];
1903 prop->get = mono_class_inflate_generic_method (
1904 prop->get, gclass->generic_class.context);
1906 prop->set = mono_class_inflate_generic_method (
1907 prop->set, gclass->generic_class.context);
1909 prop->parent = class;
1912 g_assert (class->interface_count == gklass->interface_count);
1915 if (class->parent && !class->parent->inited)
1916 mono_class_init (class->parent);
1918 has_cached_info = mono_class_get_cached_class_info (class, &cached_info);
1920 if (!class->generic_class && (!has_cached_info || (has_cached_info && cached_info.has_nested_classes))) {
1921 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
1924 guint32 cols [MONO_NESTED_CLASS_SIZE];
1925 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
1926 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
1927 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
1929 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
1934 * Computes the size used by the fields, and their locations
1936 if (has_cached_info) {
1937 class->instance_size = cached_info.instance_size;
1938 class->class_size = cached_info.class_size;
1939 class->packing_size = cached_info.packing_size;
1940 class->min_align = cached_info.min_align;
1941 class->blittable = cached_info.blittable;
1942 class->has_references = cached_info.has_references;
1943 class->has_static_refs = cached_info.has_static_refs;
1946 if (!class->size_inited)
1947 mono_class_setup_fields (class);
1949 /* initialize method pointers */
1952 MonoMethodSignature *sig;
1953 class->method.count = class->rank > 1? 2: 1;
1954 sig = mono_metadata_signature_alloc (class->image, class->rank);
1955 sig->ret = &mono_defaults.void_class->byval_arg;
1956 sig->pinvoke = TRUE;
1957 for (i = 0; i < class->rank; ++i)
1958 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1960 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1961 ctor->klass = class;
1962 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1963 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1964 ctor->signature = sig;
1965 ctor->name = ".ctor";
1967 class->methods = g_new (MonoMethod*, class->method.count);
1968 class->methods [0] = ctor;
1969 if (class->rank > 1) {
1970 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
1971 sig->ret = &mono_defaults.void_class->byval_arg;
1972 sig->pinvoke = TRUE;
1973 for (i = 0; i < class->rank * 2; ++i)
1974 sig->params [i] = &mono_defaults.int32_class->byval_arg;
1976 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
1977 ctor->klass = class;
1978 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
1979 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
1980 ctor->signature = sig;
1981 ctor->name = ".ctor";
1983 class->methods [1] = ctor;
1987 mono_class_setup_supertypes (class);
1990 if (class == mono_defaults.object_class) {
1991 mono_class_setup_vtable (class);
1992 for (i = 0; i < class->vtable_size; ++i) {
1993 MonoMethod *cm = class->vtable [i];
1995 if (!strcmp (cm->name, "GetHashCode")) {
2001 g_assert (ghc_slot > 0);
2003 default_ghc = class->vtable [ghc_slot];
2007 if (!default_finalize) {
2008 if (class == mono_defaults.object_class) {
2009 mono_class_setup_vtable (class);
2010 for (i = 0; i < class->vtable_size; ++i) {
2011 MonoMethod *cm = class->vtable [i];
2013 if (!strcmp (cm->name, "Finalize")) {
2019 g_assert (finalize_slot > 0);
2021 default_finalize = class->vtable [finalize_slot];
2026 * If possible, avoid the creation of the generic vtable by requesting
2027 * cached info from the runtime.
2029 if (has_cached_info) {
2030 guint32 cur_slot = 0;
2032 class->vtable_size = cached_info.vtable_size;
2033 class->has_finalize = cached_info.has_finalize;
2034 class->ghcimpl = cached_info.ghcimpl;
2035 class->has_cctor = cached_info.has_cctor;
2037 if (class->parent) {
2038 mono_class_init (class->parent);
2039 cur_slot = class->parent->vtable_size;
2042 setup_interface_offsets (class, cur_slot);
2045 mono_class_setup_vtable (class);
2048 if (class->parent) {
2049 MonoMethod *cmethod = class->vtable [ghc_slot];
2050 if (cmethod->is_inflated)
2051 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
2052 if (cmethod == default_ghc) {
2057 /* Object::Finalize should have empty implemenatation */
2058 class->has_finalize = 0;
2059 if (class->parent) {
2060 MonoMethod *cmethod = class->vtable [finalize_slot];
2061 if (cmethod->is_inflated)
2062 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
2063 if (cmethod != default_finalize) {
2064 class->has_finalize = 1;
2068 for (i = 0; i < class->method.count; ++i) {
2069 MonoMethod *method = class->methods [i];
2070 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2071 (strcmp (".cctor", method->name) == 0)) {
2072 class->has_cctor = 1;
2079 class->init_pending = 0;
2081 mono_loader_unlock ();
2083 if (MONO_CLASS_IS_INTERFACE (class)) {
2085 * class->interface_offsets is needed for the castclass/isinst code, so
2086 * we have to setup them for interfaces, too.
2088 setup_interface_offsets (class, 0);
2091 if (mono_debugger_class_init_func)
2092 mono_debugger_class_init_func (class);
2096 * LOCKING: this assumes the loader lock is held
2099 mono_class_setup_mono_type (MonoClass *class)
2101 const char *name = class->name;
2102 const char *nspace = class->name_space;
2104 if (MONO_CLASS_IS_INTERFACE (class))
2105 class->interface_id = mono_get_unique_iid (class);
2107 class->this_arg.byref = 1;
2108 class->this_arg.data.klass = class;
2109 class->this_arg.type = MONO_TYPE_CLASS;
2110 class->byval_arg.data.klass = class;
2111 class->byval_arg.type = MONO_TYPE_CLASS;
2113 if (!strcmp (nspace, "System")) {
2114 if (!strcmp (name, "ValueType")) {
2116 * do not set the valuetype bit for System.ValueType.
2117 * class->valuetype = 1;
2119 class->blittable = TRUE;
2120 } else if (!strcmp (name, "Enum")) {
2122 * do not set the valuetype bit for System.Enum.
2123 * class->valuetype = 1;
2125 class->valuetype = 0;
2126 class->enumtype = 0;
2127 } else if (!strcmp (name, "Object")) {
2128 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
2129 } else if (!strcmp (name, "String")) {
2130 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
2131 } else if (!strcmp (name, "TypedReference")) {
2132 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
2136 if (class->valuetype) {
2137 int t = MONO_TYPE_VALUETYPE;
2138 if (!strcmp (nspace, "System")) {
2141 if (!strcmp (name, "Boolean")) {
2142 t = MONO_TYPE_BOOLEAN;
2143 } else if (!strcmp(name, "Byte")) {
2145 class->blittable = TRUE;
2149 if (!strcmp (name, "Char")) {
2154 if (!strcmp (name, "Double")) {
2156 class->blittable = TRUE;
2160 if (!strcmp (name, "Int32")) {
2162 class->blittable = TRUE;
2163 } else if (!strcmp(name, "Int16")) {
2165 class->blittable = TRUE;
2166 } else if (!strcmp(name, "Int64")) {
2168 class->blittable = TRUE;
2169 } else if (!strcmp(name, "IntPtr")) {
2171 class->blittable = TRUE;
2175 if (!strcmp (name, "Single")) {
2177 class->blittable = TRUE;
2178 } else if (!strcmp(name, "SByte")) {
2180 class->blittable = TRUE;
2184 if (!strcmp (name, "UInt32")) {
2186 class->blittable = TRUE;
2187 } else if (!strcmp(name, "UInt16")) {
2189 class->blittable = TRUE;
2190 } else if (!strcmp(name, "UInt64")) {
2192 class->blittable = TRUE;
2193 } else if (!strcmp(name, "UIntPtr")) {
2195 class->blittable = TRUE;
2199 if (!strcmp (name, "TypedReference")) {
2200 t = MONO_TYPE_TYPEDBYREF;
2201 class->blittable = TRUE;
2205 if (!strcmp (name, "Void")) {
2213 class->this_arg.type = class->byval_arg.type = t;
2218 * LOCKING: this assumes the loader lock is held
2221 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
2223 gboolean system_namespace;
2225 system_namespace = !strcmp (class->name_space, "System");
2227 /* if root of the hierarchy */
2228 if (system_namespace && !strcmp (class->name, "Object")) {
2229 class->parent = NULL;
2230 class->instance_size = sizeof (MonoObject);
2233 if (!strcmp (class->name, "<Module>")) {
2234 class->parent = NULL;
2235 class->instance_size = 0;
2239 if (!MONO_CLASS_IS_INTERFACE (class)) {
2240 class->parent = parent;
2243 g_assert_not_reached (); /* FIXME */
2245 if (parent->generic_class && !parent->name) {
2247 * If the parent is a generic instance, we may get
2248 * called before it is fully initialized, especially
2249 * before it has its name.
2254 class->marshalbyref = parent->marshalbyref;
2255 class->contextbound = parent->contextbound;
2256 class->delegate = parent->delegate;
2258 if (system_namespace) {
2259 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
2260 class->marshalbyref = 1;
2262 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
2263 class->contextbound = 1;
2265 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
2266 class->delegate = 1;
2269 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
2270 (strcmp (class->parent->name_space, "System") == 0)))
2271 class->valuetype = 1;
2272 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
2273 class->valuetype = class->enumtype = 1;
2275 /*class->enumtype = class->parent->enumtype; */
2276 mono_class_setup_supertypes (class);
2278 class->parent = NULL;
2284 * LOCKING: this assumes the loader lock is held
2287 mono_class_setup_supertypes (MonoClass *class)
2291 if (class->supertypes)
2294 if (class->parent && !class->parent->supertypes)
2295 mono_class_setup_supertypes (class->parent);
2297 class->idepth = class->parent->idepth + 1;
2301 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
2302 class->supertypes = g_new0 (MonoClass *, ms);
2304 if (class->parent) {
2305 class->supertypes [class->idepth - 1] = class;
2306 memcpy (class->supertypes, class->parent->supertypes, class->parent->idepth * sizeof (gpointer));
2308 class->supertypes [0] = class;
2313 * If we inherit a type parameter from an outer class, set its owner to that class.
2316 set_generic_param_owner (MonoGenericContainer *container, MonoClass *klass, int pos)
2318 MonoGenericContainer *gc;
2321 if (klass->nested_in)
2322 pos = set_generic_param_owner (container, klass->nested_in, pos);
2324 if (!klass->generic_container)
2327 gc = klass->generic_container;
2328 for (i = pos; i < gc->type_argc; i++)
2329 container->type_params [i].owner = gc;
2331 return pos + gc->type_argc;
2334 static MonoGenericInst *
2335 get_shared_inst (MonoGenericContainer *container)
2337 MonoGenericInst *nginst;
2340 nginst = g_new0 (MonoGenericInst, 1);
2341 nginst->type_argc = container->type_argc;
2342 nginst->type_argv = g_new0 (MonoType *, nginst->type_argc);
2343 nginst->is_reference = 1;
2344 nginst->is_open = 1;
2346 for (i = 0; i < nginst->type_argc; i++) {
2347 MonoType *t = g_new0 (MonoType, 1);
2349 t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2350 t->data.generic_param = &container->type_params [i];
2352 nginst->type_argv [i] = t;
2355 return mono_metadata_lookup_generic_inst (nginst);
2359 mono_get_shared_generic_class (MonoGenericContainer *container, gboolean is_dynamic)
2361 MonoInflatedGenericClass *igclass;
2362 MonoGenericClass *gclass;
2365 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
2366 igclass = &dgclass->generic_class;
2367 gclass = &igclass->generic_class;
2368 gclass->is_inflated = 1;
2369 gclass->is_dynamic = 1;
2371 igclass = g_new0 (MonoInflatedGenericClass, 1);
2372 gclass = &igclass->generic_class;
2373 gclass->is_inflated = 1;
2376 gclass->context = &container->context;
2377 gclass->container_class = container->klass;
2378 gclass->inst = get_shared_inst (container);
2381 MonoGenericClass *cached = mono_metadata_lookup_generic_class (gclass);
2389 igclass->klass = container->klass;
2395 * @image: context where the image is created
2396 * @type_token: typedef token
2399 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
2401 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
2402 MonoClass *class, *parent = NULL;
2403 guint32 cols [MONO_TYPEDEF_SIZE];
2404 guint32 cols_next [MONO_TYPEDEF_SIZE];
2405 guint tidx = mono_metadata_token_index (type_token);
2406 MonoGenericContext *context = NULL;
2407 const char *name, *nspace;
2409 MonoClass **interfaces;
2411 mono_loader_lock ();
2413 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
2414 mono_loader_unlock ();
2418 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
2420 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
2422 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2423 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2425 class = g_malloc0 (sizeof (MonoClass));
2428 class->name_space = nspace;
2430 class->image = image;
2431 class->type_token = type_token;
2432 class->flags = cols [MONO_TYPEDEF_FLAGS];
2434 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
2436 class->generic_container = mono_metadata_load_generic_params (image, class->type_token, NULL);
2437 if (class->generic_container) {
2438 class->generic_container->klass = class;
2439 context = &class->generic_container->context;
2441 context->gclass = mono_get_shared_generic_class (context->container, FALSE);
2444 if (cols [MONO_TYPEDEF_EXTENDS]) {
2445 parent = mono_class_get_full (
2446 image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]), context);
2449 mono_class_setup_parent (class, parent);
2451 mono_class_setup_mono_type (class);
2453 mono_metadata_interfaces_from_typedef_full (image, type_token, &interfaces, &icount, context);
2455 class->interfaces = interfaces;
2456 class->interface_count = icount;
2458 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
2460 /* fixme: maybe we must set this on windows
2461 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
2465 class->cast_class = class->element_class = class;
2467 /*g_print ("Load class %s\n", name);*/
2470 * Compute the field and method lists
2472 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
2473 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
2475 if (tt->rows > tidx){
2476 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
2477 class->field.last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
2478 class->method.last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
2480 class->field.last = image->tables [MONO_TABLE_FIELD].rows;
2481 class->method.last = image->tables [MONO_TABLE_METHOD].rows;
2484 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
2485 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
2486 class->field.count = class->field.last - class->field.first;
2488 class->field.count = 0;
2490 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
2491 class->method.count = class->method.last - class->method.first;
2493 class->method.count = 0;
2495 /* reserve space to store vector pointer in arrays */
2496 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
2497 class->instance_size += 2 * sizeof (gpointer);
2498 g_assert (class->field.count == 0);
2501 if (class->enumtype)
2502 mono_class_setup_fields (class);
2504 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
2505 class->nested_in = mono_class_create_from_typedef (image, type_token);
2507 if (class->nested_in && class->generic_container)
2508 set_generic_param_owner (class->generic_container, class->nested_in, 0);
2510 mono_loader_unlock ();
2516 mono_class_create_generic (MonoInflatedGenericClass *gclass)
2518 MonoClass *klass, *gklass;
2521 if (gclass->is_initialized)
2523 gclass->is_initialized = TRUE;
2526 gclass->klass = g_malloc0 (sizeof (MonoClass));
2527 klass = gclass->klass;
2529 gklass = gclass->generic_class.container_class;
2531 klass->nested_in = gklass->nested_in;
2533 klass->name = gklass->name;
2534 klass->name_space = gklass->name_space;
2535 klass->image = gklass->image;
2536 klass->flags = gklass->flags;
2538 klass->generic_class = &gclass->generic_class;
2540 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
2541 klass->this_arg.data.generic_class = klass->byval_arg.data.generic_class = &gclass->generic_class;
2542 klass->this_arg.byref = TRUE;
2544 klass->cast_class = klass->element_class = klass;
2546 if (gclass->generic_class.is_dynamic) {
2547 klass->instance_size = gklass->instance_size;
2548 klass->class_size = gklass->class_size;
2549 klass->size_inited = 1;
2552 klass->valuetype = gklass->valuetype;
2554 mono_class_setup_supertypes (klass);
2557 klass->interface_count = gklass->interface_count;
2558 klass->interfaces = g_new0 (MonoClass *, klass->interface_count);
2559 for (i = 0; i < klass->interface_count; i++) {
2560 MonoType *it = &gklass->interfaces [i]->byval_arg;
2561 MonoType *inflated = mono_class_inflate_generic_type (
2562 it, gclass->generic_class.context);
2563 klass->interfaces [i] = mono_class_from_mono_type (inflated);
2566 i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, 1);
2569 guint32 cols [MONO_NESTED_CLASS_SIZE];
2570 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
2571 nclass = mono_class_create_from_typedef (klass->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
2572 klass->nested_classes = g_list_prepend (klass->nested_classes, nclass);
2574 i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, i + 1);
2577 if (gclass->generic_class.is_dynamic) {
2578 MonoDynamicGenericClass *dgclass = (MonoDynamicGenericClass *) gclass;
2580 if (dgclass->parent)
2581 klass->parent = mono_class_from_mono_type (dgclass->parent);
2582 } else if (gklass->parent) {
2583 MonoType *inflated = mono_class_inflate_generic_type (
2584 &gklass->parent->byval_arg, gclass->generic_class.context);
2586 klass->parent = mono_class_from_mono_type (inflated);
2590 mono_class_setup_parent (klass, klass->parent);
2594 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
2596 MonoClass *klass, **ptr;
2600 return param->pklass;
2602 klass = param->pklass = g_new0 (MonoClass, 1);
2604 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
2608 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
2609 klass->parent = param->constraints [0];
2611 } else if (param->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT)
2612 klass->parent = mono_class_from_name (mono_defaults.corlib, "System", "ValueType");
2614 klass->parent = mono_defaults.object_class;
2616 if (count - pos > 0) {
2617 klass->interface_count = count - pos;
2618 klass->interfaces = g_new0 (MonoClass *, count - pos);
2619 for (i = pos; i < count; i++)
2620 klass->interfaces [i - pos] = param->constraints [i];
2623 g_assert (param->name && param->owner);
2625 klass->name = param->name;
2626 klass->name_space = "";
2627 klass->image = image;
2628 klass->inited = TRUE;
2629 klass->cast_class = klass->element_class = klass;
2630 klass->enum_basetype = &klass->element_class->byval_arg;
2631 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2633 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2634 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2635 klass->this_arg.byref = TRUE;
2637 mono_class_setup_supertypes (klass);
2643 my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
2648 return param->pklass;
2650 g_assert (param->owner);
2652 klass = g_new0 (MonoClass, 1);
2655 klass->name = param->name;
2657 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
2658 klass->name_space = "";
2659 klass->image = mono_defaults.corlib;
2660 klass->inited = TRUE;
2661 klass->cast_class = klass->element_class = klass;
2662 klass->enum_basetype = &klass->element_class->byval_arg;
2663 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2665 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2666 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2667 klass->this_arg.byref = TRUE;
2669 mono_class_setup_supertypes (klass);
2675 mono_ptr_class_get (MonoType *type)
2678 MonoClass *el_class;
2679 static GHashTable *ptr_hash = NULL;
2681 mono_loader_lock ();
2684 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2685 el_class = mono_class_from_mono_type (type);
2686 if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
2687 mono_loader_unlock ();
2690 result = g_new0 (MonoClass, 1);
2692 result->parent = NULL; /* no parent for PTR types */
2693 result->name_space = el_class->name_space;
2694 result->name = g_strdup_printf ("%s*", el_class->name);
2695 result->image = el_class->image;
2696 result->inited = TRUE;
2697 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2698 /* Can pointers get boxed? */
2699 result->instance_size = sizeof (gpointer);
2700 result->cast_class = result->element_class = el_class;
2701 result->enum_basetype = &result->element_class->byval_arg;
2702 result->blittable = TRUE;
2704 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
2705 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
2706 result->this_arg.byref = TRUE;
2708 mono_class_setup_supertypes (result);
2710 g_hash_table_insert (ptr_hash, el_class, result);
2712 mono_loader_unlock ();
2718 mono_fnptr_class_get (MonoMethodSignature *sig)
2721 static GHashTable *ptr_hash = NULL;
2723 mono_loader_lock ();
2726 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2728 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
2729 mono_loader_unlock ();
2732 result = g_new0 (MonoClass, 1);
2734 result->parent = NULL; /* no parent for PTR types */
2735 result->name = "System";
2736 result->name_space = "MonoFNPtrFakeClass";
2737 result->image = NULL; /* need to fix... */
2738 result->inited = TRUE;
2739 result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
2740 /* Can pointers get boxed? */
2741 result->instance_size = sizeof (gpointer);
2742 result->cast_class = result->element_class = result;
2743 result->blittable = TRUE;
2745 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
2746 result->this_arg.data.method = result->byval_arg.data.method = sig;
2747 result->this_arg.byref = TRUE;
2748 result->enum_basetype = &result->element_class->byval_arg;
2749 result->blittable = TRUE;
2751 mono_class_setup_supertypes (result);
2753 g_hash_table_insert (ptr_hash, sig, result);
2755 mono_loader_unlock ();
2761 mono_class_from_mono_type (MonoType *type)
2763 switch (type->type) {
2764 case MONO_TYPE_OBJECT:
2765 return type->data.klass? type->data.klass: mono_defaults.object_class;
2766 case MONO_TYPE_VOID:
2767 return type->data.klass? type->data.klass: mono_defaults.void_class;
2768 case MONO_TYPE_BOOLEAN:
2769 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
2770 case MONO_TYPE_CHAR:
2771 return type->data.klass? type->data.klass: mono_defaults.char_class;
2773 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
2775 return type->data.klass? type->data.klass: mono_defaults.byte_class;
2777 return type->data.klass? type->data.klass: mono_defaults.int16_class;
2779 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
2781 return type->data.klass? type->data.klass: mono_defaults.int32_class;
2783 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
2785 return type->data.klass? type->data.klass: mono_defaults.int_class;
2787 return type->data.klass? type->data.klass: mono_defaults.uint_class;
2789 return type->data.klass? type->data.klass: mono_defaults.int64_class;
2791 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
2793 return type->data.klass? type->data.klass: mono_defaults.single_class;
2795 return type->data.klass? type->data.klass: mono_defaults.double_class;
2796 case MONO_TYPE_STRING:
2797 return type->data.klass? type->data.klass: mono_defaults.string_class;
2798 case MONO_TYPE_TYPEDBYREF:
2799 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
2800 case MONO_TYPE_ARRAY:
2801 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
2803 return mono_ptr_class_get (type->data.type);
2804 case MONO_TYPE_FNPTR:
2805 return mono_fnptr_class_get (type->data.method);
2806 case MONO_TYPE_SZARRAY:
2807 return mono_array_class_get (type->data.klass, 1);
2808 case MONO_TYPE_CLASS:
2809 case MONO_TYPE_VALUETYPE:
2810 return type->data.klass;
2811 case MONO_TYPE_GENERICINST: {
2812 MonoInflatedGenericClass *gclass;
2813 gclass = mono_get_inflated_generic_class (type->data.generic_class);
2814 g_assert (gclass->klass);
2815 return gclass->klass;
2818 return my_mono_class_from_generic_parameter (type->data.generic_param, FALSE);
2819 case MONO_TYPE_MVAR:
2820 return my_mono_class_from_generic_parameter (type->data.generic_param, TRUE);
2822 g_warning ("implement me 0x%02x\n", type->type);
2823 g_assert_not_reached ();
2830 * @image: context where the image is created
2831 * @type_spec: typespec token
2834 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec,
2835 MonoGenericContext *context)
2837 MonoType *type, *inflated;
2840 type = mono_type_create_from_typespec_full (image, context, type_spec);
2842 switch (type->type) {
2843 case MONO_TYPE_ARRAY:
2844 class = mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
2846 case MONO_TYPE_SZARRAY:
2847 class = mono_array_class_get (type->data.klass, 1);
2850 class = mono_ptr_class_get (type->data.type);
2852 case MONO_TYPE_GENERICINST: {
2853 MonoInflatedGenericClass *gclass;
2854 gclass = mono_get_inflated_generic_class (type->data.generic_class);
2855 g_assert (gclass->klass);
2856 class = gclass->klass;
2860 /* it seems any type can be stored in TypeSpec as well */
2861 class = mono_class_from_mono_type (type);
2865 if (!class || !context || (!context->gclass && !context->gmethod))
2868 inflated = mono_class_inflate_generic_type (&class->byval_arg, context);
2870 return mono_class_from_mono_type (inflated);
2874 * mono_bounded_array_class_get:
2875 * @element_class: element class
2876 * @rank: the dimension of the array class
2877 * @bounded: whenever the array has non-zero bounds
2879 * Returns: a class object describing the array with element type @element_type and
2883 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
2887 MonoClass *parent = NULL;
2888 GSList *list, *rootlist;
2891 gboolean corlib_type = FALSE;
2893 g_assert (rank <= 255);
2896 /* bounded only matters for one-dimensional arrays */
2899 image = eclass->image;
2901 mono_loader_lock ();
2903 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
2904 for (; list; list = list->next) {
2906 if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
2907 mono_loader_unlock ();
2913 /* for the building corlib use System.Array from it */
2914 if (image->assembly && image->assembly->dynamic && image->assembly_name && strcmp (image->assembly_name, "mscorlib") == 0) {
2915 parent = mono_class_from_name (image, "System", "Array");
2917 } else if (mono_defaults.generic_array_class) {
2918 MonoType *inflated, **args;
2920 args = g_new0 (MonoType *, 1);
2921 args [0] = &eclass->byval_arg;
2923 inflated = mono_class_bind_generic_parameters (
2924 &mono_defaults.generic_array_class->byval_arg, 1, args);
2925 parent = mono_class_from_mono_type (inflated);
2927 if (!parent->inited)
2928 mono_class_init (parent);
2930 parent = mono_defaults.array_class;
2931 if (!parent->inited)
2932 mono_class_init (parent);
2935 class = g_malloc0 (sizeof (MonoClass));
2937 class->image = image;
2938 class->name_space = eclass->name_space;
2939 nsize = strlen (eclass->name);
2940 name = g_malloc (nsize + 2 + rank);
2941 memcpy (name, eclass->name, nsize);
2944 memset (name + nsize + 1, ',', rank - 1);
2945 name [nsize + rank] = ']';
2946 name [nsize + rank + 1] = 0;
2948 class->type_token = 0;
2949 /* all arrays are marked serializable and sealed, bug #42779 */
2950 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
2951 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2952 class->parent = parent;
2953 class->instance_size = mono_class_instance_size (class->parent);
2954 class->class_size = 0;
2955 mono_class_setup_supertypes (class);
2956 if (eclass->generic_class)
2957 mono_class_init (eclass);
2958 if (!eclass->size_inited)
2959 mono_class_setup_fields (eclass);
2960 class->has_references = MONO_TYPE_IS_REFERENCE (&eclass->byval_arg) || eclass->has_references? TRUE: FALSE;
2964 if (eclass->enumtype)
2965 class->cast_class = eclass->element_class;
2967 class->cast_class = eclass;
2969 class->element_class = eclass;
2971 if ((rank > 1) || bounded) {
2972 MonoArrayType *at = g_new0 (MonoArrayType, 1);
2973 class->byval_arg.type = MONO_TYPE_ARRAY;
2974 class->byval_arg.data.array = at;
2975 at->eklass = eclass;
2977 /* FIXME: complete.... */
2979 class->byval_arg.type = MONO_TYPE_SZARRAY;
2980 class->byval_arg.data.klass = eclass;
2982 class->this_arg = class->byval_arg;
2983 class->this_arg.byref = 1;
2988 list = g_slist_append (rootlist, class);
2989 g_hash_table_insert (image->array_cache, eclass, list);
2991 mono_loader_unlock ();
2997 * mono_array_class_get:
2998 * @element_class: element class
2999 * @rank: the dimension of the array class
3001 * Returns: a class object describing the array with element type @element_type and
3005 mono_array_class_get (MonoClass *eclass, guint32 rank)
3007 return mono_bounded_array_class_get (eclass, rank, FALSE);
3011 * mono_class_instance_size:
3014 * Returns: the size of an object instance
3017 mono_class_instance_size (MonoClass *klass)
3019 if (!klass->size_inited)
3020 mono_class_init (klass);
3022 return klass->instance_size;
3026 * mono_class_min_align:
3029 * Returns: minimm alignment requirements
3032 mono_class_min_align (MonoClass *klass)
3034 if (!klass->size_inited)
3035 mono_class_init (klass);
3037 return klass->min_align;
3041 * mono_class_value_size:
3044 * This function is used for value types, and return the
3045 * space and the alignment to store that kind of value object.
3047 * Returns: the size of a value of kind @klass
3050 mono_class_value_size (MonoClass *klass, guint32 *align)
3054 /* fixme: check disable, because we still have external revereces to
3055 * mscorlib and Dummy Objects
3057 /*g_assert (klass->valuetype);*/
3059 size = mono_class_instance_size (klass) - sizeof (MonoObject);
3062 *align = klass->min_align;
3068 * mono_class_data_size:
3071 * Returns: the size of the static class data
3074 mono_class_data_size (MonoClass *klass)
3077 mono_class_init (klass);
3079 return klass->class_size;
3083 * Auxiliary routine to mono_class_get_field
3085 * Takes a field index instead of a field token.
3087 static MonoClassField *
3088 mono_class_get_field_idx (MonoClass *class, int idx)
3090 mono_class_setup_fields (class);
3092 if (class->field.count){
3093 if ((idx >= class->field.first) && (idx < class->field.last)){
3094 return &class->fields [idx - class->field.first];
3101 return mono_class_get_field_idx (class->parent, idx);
3105 * mono_class_get_field:
3106 * @class: the class to lookup the field.
3107 * @field_token: the field token
3109 * Returns: A MonoClassField representing the type and offset of
3110 * the field, or a NULL value if the field does not belong to this
3114 mono_class_get_field (MonoClass *class, guint32 field_token)
3116 int idx = mono_metadata_token_index (field_token);
3118 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
3120 return mono_class_get_field_idx (class, idx - 1);
3124 mono_class_get_field_from_name (MonoClass *klass, const char *name)
3129 mono_class_setup_fields (klass);
3130 for (i = 0; i < klass->field.count; ++i) {
3131 if (strcmp (name, klass->fields [i].name) == 0)
3132 return &klass->fields [i];
3134 klass = klass->parent;
3140 mono_class_get_field_token (MonoClassField *field)
3142 MonoClass *klass = field->parent;
3146 mono_class_setup_fields (klass);
3147 for (i = 0; i < klass->field.count; ++i) {
3148 if (&klass->fields [i] == field)
3149 return mono_metadata_make_token (MONO_TABLE_FIELD, klass->field.first + i + 1);
3151 klass = klass->parent;
3154 g_assert_not_reached ();
3159 mono_class_get_event_token (MonoEvent *event)
3161 MonoClass *klass = event->parent;
3165 for (i = 0; i < klass->event.count; ++i) {
3166 if (&klass->events [i] == event)
3167 return mono_metadata_make_token (MONO_TABLE_EVENT, klass->event.first + i + 1);
3169 klass = klass->parent;
3172 g_assert_not_reached ();
3177 mono_vtable_get_static_field_data (MonoVTable *vt)
3183 mono_class_get_property_from_name (MonoClass *klass, const char *name)
3187 gpointer iter = NULL;
3188 while ((p = mono_class_get_properties (klass, &iter))) {
3189 if (! strcmp (name, p->name))
3192 klass = klass->parent;
3198 mono_class_get_property_token (MonoProperty *prop)
3200 MonoClass *klass = prop->parent;
3204 gpointer iter = NULL;
3205 while ((p = mono_class_get_properties (klass, &iter))) {
3206 if (&klass->properties [i] == prop)
3207 return mono_metadata_make_token (MONO_TABLE_PROPERTY, klass->property.first + i + 1);
3211 klass = klass->parent;
3214 g_assert_not_reached ();
3219 mono_class_name_from_token (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3221 const char *name, *nspace;
3223 return g_strdup_printf ("DynamicType 0x%08x", type_token);
3225 switch (type_token & 0xff000000){
3226 case MONO_TOKEN_TYPE_DEF: {
3227 guint32 cols [MONO_TYPEDEF_SIZE];
3228 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
3229 guint tidx = mono_metadata_token_index (type_token);
3231 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
3232 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3233 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3234 if (strlen (nspace) == 0)
3235 return g_strdup_printf ("%s", name);
3237 return g_strdup_printf ("%s.%s", nspace, name);
3240 case MONO_TOKEN_TYPE_REF: {
3241 guint32 cols [MONO_TYPEREF_SIZE];
3242 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
3244 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
3245 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
3246 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
3247 if (strlen (nspace) == 0)
3248 return g_strdup_printf ("%s", name);
3250 return g_strdup_printf ("%s.%s", nspace, name);
3253 case MONO_TOKEN_TYPE_SPEC:
3254 return g_strdup_printf ("Typespec 0x%08x", type_token);
3256 g_assert_not_reached ();
3263 mono_assembly_name_from_token (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3266 return g_strdup_printf ("DynamicAssembly %s", image->name);
3268 switch (type_token & 0xff000000){
3269 case MONO_TOKEN_TYPE_DEF:
3270 return _mono_stringify_aname (&image->assembly->aname);
3272 case MONO_TOKEN_TYPE_REF: {
3273 MonoAssemblyName aname;
3274 guint32 cols [MONO_TYPEREF_SIZE];
3275 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
3278 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
3280 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
3281 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
3282 case MONO_RESOLTION_SCOPE_MODULE:
3284 return g_strdup ("");
3285 case MONO_RESOLTION_SCOPE_MODULEREF:
3287 return g_strdup ("");
3288 case MONO_RESOLTION_SCOPE_TYPEREF:
3290 return g_strdup ("");
3291 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
3292 mono_assembly_get_assemblyref (image, idx - 1, &aname);
3293 return _mono_stringify_aname (&aname);
3295 g_assert_not_reached ();
3299 case MONO_TOKEN_TYPE_SPEC:
3301 return g_strdup ("");
3303 g_assert_not_reached ();
3311 * @image: the image where the class resides
3312 * @type_token: the token for the class
3313 * @at: an optional pointer to return the array element type
3315 * Returns: the MonoClass that represents @type_token in @image
3318 _mono_class_get (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3320 MonoClass *class = NULL;
3323 return mono_lookup_dynamic_token (image, type_token);
3325 switch (type_token & 0xff000000){
3326 case MONO_TOKEN_TYPE_DEF:
3327 class = mono_class_create_from_typedef (image, type_token);
3329 case MONO_TOKEN_TYPE_REF:
3330 class = mono_class_from_typeref (image, type_token);
3332 case MONO_TOKEN_TYPE_SPEC:
3333 class = mono_class_create_from_typespec (image, type_token, context);
3336 g_warning ("unknown token type %x", type_token & 0xff000000);
3337 g_assert_not_reached ();
3341 char *name = mono_class_name_from_token (image, type_token, context);
3342 char *assembly = mono_assembly_name_from_token (image, type_token, context);
3343 mono_loader_set_error_type_load (name, assembly);
3350 mono_class_get (MonoImage *image, guint32 type_token)
3352 return _mono_class_get (image, type_token, NULL);
3356 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3358 MonoClass *class = _mono_class_get (image, type_token, context);
3361 if (!class || !context || (!context->gclass && !context->gmethod))
3364 switch (class->byval_arg.type) {
3365 case MONO_TYPE_GENERICINST:
3366 if (!class->generic_class->inst->is_open)
3370 case MONO_TYPE_MVAR:
3376 inflated = inflate_generic_type (&class->byval_arg, context);
3380 return mono_class_from_mono_type (inflated);
3389 find_nocase (gpointer key, gpointer value, gpointer user_data)
3391 char *name = (char*)key;
3392 FindUserData *data = (FindUserData*)user_data;
3394 if (!data->value && (g_strcasecmp (name, (char*)data->key) == 0))
3395 data->value = value;
3399 * mono_class_from_name_case:
3400 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3401 * @name_space: the type namespace
3402 * @name: the type short name.
3404 * Obtains a MonoClass with a given namespace and a given name which
3405 * is located in the given MonoImage. The namespace and name
3406 * lookups are case insensitive.
3408 * You can also pass @NULL to the image, and that will lookup for
3409 * a type with the given namespace and name in all of the loaded
3410 * assemblies: notice that since there might be a name clash in this
3411 * case, passing @NULL is not encouraged if you need a precise type.
3415 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
3417 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
3418 guint32 cols [MONO_TYPEDEF_SIZE];
3423 if (image->dynamic) {
3425 FindUserData user_data;
3427 mono_loader_lock ();
3429 user_data.key = name_space;
3430 user_data.value = NULL;
3431 g_hash_table_foreach (image->name_cache, find_nocase, &user_data);
3433 if (user_data.value) {
3434 GHashTable *nspace_table = (GHashTable*)user_data.value;
3436 user_data.key = name;
3437 user_data.value = NULL;
3439 g_hash_table_foreach (nspace_table, find_nocase, &user_data);
3441 if (user_data.value)
3442 token = GPOINTER_TO_UINT (user_data.value);
3445 mono_loader_unlock ();
3448 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | token);
3454 /* add a cache if needed */
3455 for (i = 1; i <= t->rows; ++i) {
3456 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
3457 /* nested types are accessed from the nesting name */
3458 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
3459 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
3461 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3462 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3463 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
3464 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
3470 return_nested_in (MonoClass *class, char *nested) {
3472 char *s = strchr (nested, '/');
3479 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
3481 if (strcmp (found->name, nested) == 0) {
3483 return return_nested_in (found, s);
3492 * mono_class_from_name:
3493 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3494 * @name_space: the type namespace
3495 * @name: the type short name.
3497 * Obtains a MonoClass with a given namespace and a given name which
3498 * is located in the given MonoImage.
3500 * You can also pass `NULL' to the image, and that will lookup for
3501 * a type with the given namespace and name in all of the loaded
3502 * assemblies: notice that since there might be a name clash in this
3503 * case, passing NULL is not encouraged if you need a precise type.
3507 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
3509 GHashTable *nspace_table;
3510 MonoImage *loaded_image;
3516 if ((nested = strchr (name, '/'))) {
3517 int pos = nested - name;
3518 int len = strlen (name);
3521 memcpy (buf, name, len + 1);
3523 nested = buf + pos + 1;
3527 mono_loader_lock ();
3529 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
3532 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
3534 mono_loader_unlock ();
3539 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
3540 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
3541 guint32 cols [MONO_EXP_TYPE_SIZE];
3544 idx = mono_metadata_token_index (token);
3546 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
3548 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
3549 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
3550 loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
3553 class = mono_class_from_name (loaded_image, name_space, name);
3555 return return_nested_in (class, nested);
3558 g_error ("not yet implemented");
3562 token = MONO_TOKEN_TYPE_DEF | token;
3564 class = mono_class_get (image, token);
3566 return return_nested_in (class, nested);
3571 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
3572 gboolean check_interfaces)
3575 g_assert (klassc->idepth > 0);
3576 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
3577 if ((klassc->interface_id <= klass->max_interface_id) &&
3578 (klass->interface_offsets [klassc->interface_id] >= 0))
3580 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
3583 for (i = 0; i < klass->interface_count; i ++) {
3584 MonoClass *ic = klass->interfaces [i];
3589 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
3594 * MS.NET thinks interfaces are a subclass of Object, so we think it as
3597 if (klassc == mono_defaults.object_class)
3600 if (klass->generic_class && klass->generic_class->is_dynamic) {
3601 MonoDynamicGenericClass *dgclass = (MonoDynamicGenericClass *) klass->generic_class;
3603 if (!dgclass->parent)
3606 if (mono_metadata_type_equal (dgclass->parent, &klassc->byval_arg))
3608 klass = mono_class_from_mono_type (dgclass->parent);
3616 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
3619 mono_class_init (klass);
3621 if (!oklass->inited)
3622 mono_class_init (oklass);
3624 if (klass->generic_class)
3625 klass = klass->generic_class->container_class;
3626 if (oklass->generic_class)
3627 oklass = oklass->generic_class->container_class;
3629 if (MONO_CLASS_IS_INTERFACE (klass)) {
3630 if ((oklass->byval_arg.type == MONO_TYPE_VAR) || (oklass->byval_arg.type == MONO_TYPE_MVAR))
3633 /* interface_offsets might not be set for dynamic classes */
3634 if (oklass->reflection_info && !oklass->interface_offsets)
3636 * oklass might be a generic type parameter but they have
3637 * interface_offsets set.
3639 return mono_reflection_call_is_assignable_to (oklass, klass);
3641 if ((klass->interface_id <= oklass->max_interface_id) &&
3642 (oklass->interface_offsets [klass->interface_id] != -1))
3646 MonoClass *eclass, *eoclass;
3648 if (oklass->rank != klass->rank)
3651 /* vectors vs. one dimensional arrays */
3652 if (oklass->byval_arg.type != klass->byval_arg.type)
3655 eclass = klass->cast_class;
3656 eoclass = oklass->cast_class;
3660 * a is b does not imply a[] is b[] when a is a valuetype, and
3661 * b is a reference type.
3664 if (eoclass->valuetype) {
3665 if ((eclass == mono_defaults.enum_class) ||
3666 (eclass == mono_defaults.enum_class->parent) ||
3667 (eclass == mono_defaults.object_class))
3671 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
3674 if (klass == mono_defaults.object_class)
3678 * Custom version of mono_class_has_parent (oklass, klass)
3680 if (oklass->idepth >= klass->idepth) {
3681 MonoClass *parent = oklass->supertypes [klass->idepth - 1];
3683 if (parent->generic_class)
3684 parent = parent->generic_class->container_class;
3686 return klass == parent;
3693 * mono_class_get_cctor:
3695 * Returns the static constructor of @klass if it exists, NULL otherwise.
3698 mono_class_get_cctor (MonoClass *klass)
3700 MonoCachedClassInfo cached_info;
3702 if (!klass->has_cctor)
3705 if (mono_class_get_cached_class_info (klass, &cached_info))
3706 return mono_get_method (klass->image, cached_info.cctor_token, klass);
3708 return mono_class_get_method_from_name_flags (klass, ".cctor", -1, METHOD_ATTRIBUTE_SPECIAL_NAME);
3712 * mono_class_get_finalizer:
3714 * Returns the finalizer method of @klass if it exists, NULL otherwise.
3717 mono_class_get_finalizer (MonoClass *klass)
3719 MonoCachedClassInfo cached_info;
3720 static int finalize_slot = -1;
3722 if (finalize_slot < 0) {
3724 MonoClass* obj_class = mono_get_object_class ();
3725 mono_class_setup_vtable (obj_class);
3726 for (i = 0; i < obj_class->vtable_size; ++i) {
3727 MonoMethod *cm = obj_class->vtable [i];
3729 if (!strcmp (mono_method_get_name (cm), "Finalize")) {
3736 if (!klass->has_finalize)
3739 if (mono_class_get_cached_class_info (klass, &cached_info))
3740 return mono_get_method (cached_info.finalize_image, cached_info.finalize_token, NULL);
3742 mono_class_setup_vtable (klass);
3743 return klass->vtable [finalize_slot];
3748 * mono_class_needs_cctor_run:
3750 * Determines whenever the class has a static constructor and whenever it
3751 * needs to be called when executing CALLER.
3754 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
3758 method = mono_class_get_cctor (klass);
3760 return (method == caller) ? FALSE : TRUE;
3766 * mono_class_array_element_size:
3769 * Returns: the number of bytes an element of type @klass
3770 * uses when stored into an array.
3773 mono_class_array_element_size (MonoClass *klass)
3775 MonoType *type = &klass->byval_arg;
3778 switch (type->type) {
3781 case MONO_TYPE_BOOLEAN:
3785 case MONO_TYPE_CHAR:
3794 case MONO_TYPE_CLASS:
3795 case MONO_TYPE_STRING:
3796 case MONO_TYPE_OBJECT:
3797 case MONO_TYPE_SZARRAY:
3798 case MONO_TYPE_ARRAY:
3800 case MONO_TYPE_MVAR:
3801 return sizeof (gpointer);
3806 case MONO_TYPE_VALUETYPE:
3807 if (type->data.klass->enumtype) {
3808 type = type->data.klass->enum_basetype;
3809 klass = klass->element_class;
3812 return mono_class_instance_size (klass) - sizeof (MonoObject);
3813 case MONO_TYPE_GENERICINST:
3814 type = &type->data.generic_class->container_class->byval_arg;
3817 g_error ("unknown type 0x%02x in mono_class_array_element_size", type->type);
3823 * mono_array_element_size:
3824 * @ac: pointer to a #MonoArrayClass
3826 * Returns: the size of single array element.
3829 mono_array_element_size (MonoClass *ac)
3831 return mono_class_array_element_size (ac->element_class);
3835 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
3836 MonoGenericContext *context)
3838 if (image->dynamic) {
3839 MonoClass *tmp_handle_class;
3840 gpointer obj = mono_lookup_dynamic_token_class (image, token, &tmp_handle_class);
3842 g_assert (tmp_handle_class);
3844 *handle_class = tmp_handle_class;
3846 if (tmp_handle_class == mono_defaults.typehandle_class)
3847 return &((MonoClass*)obj)->byval_arg;
3852 switch (token & 0xff000000) {
3853 case MONO_TOKEN_TYPE_DEF:
3854 case MONO_TOKEN_TYPE_REF: {
3857 *handle_class = mono_defaults.typehandle_class;
3858 class = mono_class_get_full (image, token, context);
3861 mono_class_init (class);
3862 /* We return a MonoType* as handle */
3863 return &class->byval_arg;
3865 case MONO_TOKEN_TYPE_SPEC: {
3868 *handle_class = mono_defaults.typehandle_class;
3869 class = mono_class_create_from_typespec (image, token, context);
3872 mono_class_init (class);
3873 return &class->byval_arg;
3875 case MONO_TOKEN_FIELD_DEF: {
3877 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
3879 *handle_class = mono_defaults.fieldhandle_class;
3880 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
3883 mono_class_init (class);
3884 return mono_class_get_field (class, token);
3886 case MONO_TOKEN_METHOD_DEF: {
3888 meth = mono_get_method_full (image, token, NULL, context);
3890 *handle_class = mono_defaults.methodhandle_class;
3893 case MONO_TOKEN_MEMBER_REF: {
3894 guint32 cols [MONO_MEMBERREF_SIZE];
3896 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
3897 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
3898 mono_metadata_decode_blob_size (sig, &sig);
3899 if (*sig == 0x6) { /* it's a field */
3901 MonoClassField *field;
3902 field = mono_field_from_token (image, token, &klass, context);
3904 *handle_class = mono_defaults.fieldhandle_class;
3908 meth = mono_get_method_full (image, token, NULL, context);
3910 *handle_class = mono_defaults.methodhandle_class;
3915 g_warning ("Unknown token 0x%08x in ldtoken", token);
3922 * This function might need to call runtime functions so it can't be part
3923 * of the metadata library.
3925 static MonoLookupDynamicToken lookup_dynamic = NULL;
3928 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
3930 lookup_dynamic = func;
3934 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
3936 MonoClass *handle_class;
3938 return lookup_dynamic (image, token, &handle_class);
3942 mono_lookup_dynamic_token_class (MonoImage *image, guint32 token, MonoClass **handle_class)
3944 return lookup_dynamic (image, token, handle_class);
3947 static MonoGetCachedClassInfo get_cached_class_info = NULL;
3950 mono_install_get_cached_class_info (MonoGetCachedClassInfo func)
3952 get_cached_class_info = func;
3956 mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
3958 if (!get_cached_class_info)
3961 return get_cached_class_info (klass, res);
3965 mono_class_get_image (MonoClass *klass)
3967 return klass->image;
3971 * mono_class_get_element_class:
3972 * @klass: the MonoClass to act on
3974 * Returns: the element class of an array or an enumeration.
3977 mono_class_get_element_class (MonoClass *klass)
3979 return klass->element_class;
3983 * mono_class_is_valuetype:
3984 * @klass: the MonoClass to act on
3986 * Returns: true if the MonoClass represents a ValueType.
3989 mono_class_is_valuetype (MonoClass *klass)
3991 return klass->valuetype;
3995 * mono_class_is_enum:
3996 * @klass: the MonoClass to act on
3998 * Returns: true if the MonoClass represents an enumeration.
4001 mono_class_is_enum (MonoClass *klass)
4003 return klass->enumtype;
4007 * mono_class_enum_basetype:
4008 * @klass: the MonoClass to act on
4010 * Returns: the underlying type representation for an enumeration.
4013 mono_class_enum_basetype (MonoClass *klass)
4015 return klass->enum_basetype;
4019 * mono_class_get_parent
4020 * @klass: the MonoClass to act on
4022 * Returns: the parent class for this class.
4025 mono_class_get_parent (MonoClass *klass)
4027 return klass->parent;
4031 * mono_class_get_nesting_type;
4032 * @klass: the MonoClass to act on
4034 * Returns: the container type where this type is nested or NULL if this type is not a nested type.
4037 mono_class_get_nesting_type (MonoClass *klass)
4039 return klass->nested_in;
4043 * mono_class_get_rank:
4044 * @klass: the MonoClass to act on
4046 * Returns: the rank for the array (the number of dimensions).
4049 mono_class_get_rank (MonoClass *klass)
4055 * mono_class_get_flags:
4056 * @klass: the MonoClass to act on
4058 * The type flags from the TypeDef table from the metadata.
4059 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
4062 * Returns: the flags from the TypeDef table.
4065 mono_class_get_flags (MonoClass *klass)
4067 return klass->flags;
4071 * mono_class_get_name
4072 * @klass: the MonoClass to act on
4074 * Returns: the name of the class.
4077 mono_class_get_name (MonoClass *klass)
4083 * mono_class_get_namespace:
4084 * @klass: the MonoClass to act on
4086 * Returns: the namespace of the class.
4089 mono_class_get_namespace (MonoClass *klass)
4091 return klass->name_space;
4095 * mono_class_get_type:
4096 * @klass: the MonoClass to act on
4098 * This method returns the internal Type representation for the class.
4100 * Returns: the MonoType from the class.
4103 mono_class_get_type (MonoClass *klass)
4105 return &klass->byval_arg;
4109 * mono_class_get_byref_type:
4110 * @klass: the MonoClass to act on
4115 mono_class_get_byref_type (MonoClass *klass)
4117 return &klass->this_arg;
4121 * mono_class_num_fields:
4122 * @klass: the MonoClass to act on
4124 * Returns: the number of static and instance fields in the class.
4127 mono_class_num_fields (MonoClass *klass)
4129 return klass->field.count;
4133 * mono_class_num_methods:
4134 * @klass: the MonoClass to act on
4136 * Returns: the number of methods in the class.
4139 mono_class_num_methods (MonoClass *klass)
4141 return klass->method.count;
4145 * mono_class_num_properties
4146 * @klass: the MonoClass to act on
4148 * Returns: the number of properties in the class.
4151 mono_class_num_properties (MonoClass *klass)
4153 mono_class_setup_properties (klass);
4155 return klass->property.count;
4159 * mono_class_num_events:
4160 * @klass: the MonoClass to act on
4162 * Returns: the number of events in the class.
4165 mono_class_num_events (MonoClass *klass)
4167 mono_class_setup_events (klass);
4169 return klass->event.count;
4173 * mono_class_get_fields:
4174 * @klass: the MonoClass to act on
4176 * This routine is an iterator routine for retrieving the fields in a class.
4178 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4179 * iterate over all of the elements. When no more values are
4180 * available, the return value is NULL.
4182 * Returns: a @MonoClassField* on each iteration, or NULL when no more fields are available.
4185 mono_class_get_fields (MonoClass* klass, gpointer *iter)
4187 MonoClassField* field;
4191 mono_class_init (klass);
4193 mono_class_setup_fields (klass);
4194 /* start from the first */
4195 if (klass->field.count) {
4196 return *iter = &klass->fields [0];
4204 if (field < &klass->fields [klass->field.count]) {
4205 return *iter = field;
4211 * mono_class_get_methods
4212 * @klass: the MonoClass to act on
4214 * This routine is an iterator routine for retrieving the fields in a class.
4216 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4217 * iterate over all of the elements. When no more values are
4218 * available, the return value is NULL.
4220 * Returns: a MonoMethod on each iteration or NULL when no more methods are available.
4223 mono_class_get_methods (MonoClass* klass, gpointer *iter)
4225 MonoMethod** method;
4229 mono_class_init (klass);
4231 mono_class_setup_methods (klass);
4232 /* start from the first */
4233 if (klass->method.count) {
4234 *iter = &klass->methods [0];
4235 return klass->methods [0];
4243 if (method < &klass->methods [klass->method.count]) {
4251 * mono_class_get_properties:
4252 * @klass: the MonoClass to act on
4254 * This routine is an iterator routine for retrieving the properties in a class.
4256 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4257 * iterate over all of the elements. When no more values are
4258 * available, the return value is NULL.
4260 * Returns: a @MonoProperty* on each invocation, or NULL when no more are available.
4263 mono_class_get_properties (MonoClass* klass, gpointer *iter)
4265 MonoProperty* property;
4269 mono_class_init (klass);
4271 mono_class_setup_properties (klass);
4272 /* start from the first */
4273 if (klass->property.count) {
4274 return *iter = &klass->properties [0];
4282 if (property < &klass->properties [klass->property.count]) {
4283 return *iter = property;
4289 * mono_class_get_events:
4290 * @klass: the MonoClass to act on
4292 * This routine is an iterator routine for retrieving the properties in a class.
4294 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4295 * iterate over all of the elements. When no more values are
4296 * available, the return value is NULL.
4298 * Returns: a @MonoEvent* on each invocation, or NULL when no more are available.
4301 mono_class_get_events (MonoClass* klass, gpointer *iter)
4307 mono_class_init (klass);
4309 mono_class_setup_events (klass);
4310 /* start from the first */
4311 if (klass->event.count) {
4312 return *iter = &klass->events [0];
4320 if (event < &klass->events [klass->event.count]) {
4321 return *iter = event;
4327 * mono_class_get_interfaces
4328 * @klass: the MonoClass to act on
4330 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
4332 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4333 * iterate over all of the elements. When no more values are
4334 * available, the return value is NULL.
4336 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
4339 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
4345 mono_class_init (klass);
4347 /* start from the first */
4348 if (klass->interface_count) {
4349 *iter = &klass->interfaces [0];
4350 return klass->interfaces [0];
4358 if (iface < &klass->interfaces [klass->interface_count]) {
4366 * mono_class_get_nested_types
4367 * @klass: the MonoClass to act on
4369 * This routine is an iterator routine for retrieving the nested types of a class.
4371 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4372 * iterate over all of the elements. When no more values are
4373 * available, the return value is NULL.
4375 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
4378 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
4384 mono_class_init (klass);
4386 /* start from the first */
4387 if (klass->nested_classes) {
4388 *iter = klass->nested_classes;
4389 return klass->nested_classes->data;
4391 /* no nested types */
4405 * mono_field_get_name:
4406 * @field: the MonoClassField to act on
4408 * Returns: the name of the field.
4411 mono_field_get_name (MonoClassField *field)
4417 * mono_field_get_type:
4418 * @field: the MonoClassField to act on
4420 * Returns: MonoType of the field.
4423 mono_field_get_type (MonoClassField *field)
4429 * mono_field_get_type:
4430 * @field: the MonoClassField to act on
4432 * Returns: MonoClass where the field was defined.
4435 mono_field_get_parent (MonoClassField *field)
4437 return field->parent;
4441 * mono_field_get_flags;
4442 * @field: the MonoClassField to act on
4444 * The metadata flags for a field are encoded using the
4445 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4447 * Returns: the flags for the field.
4450 mono_field_get_flags (MonoClassField *field)
4452 return field->type->attrs;
4456 * mono_property_get_name:
4457 * @prop: the MonoProperty to act on
4459 * Returns: the name of the property
4462 mono_property_get_name (MonoProperty *prop)
4468 * mono_property_get_set_method
4469 * @prop: the MonoProperty to act on.
4471 * Returns: the setter method of the property (A MonoMethod)
4474 mono_property_get_set_method (MonoProperty *prop)
4480 * mono_property_get_get_method
4481 * @prop: the MonoProperty to act on.
4483 * Returns: the setter method of the property (A MonoMethod)
4486 mono_property_get_get_method (MonoProperty *prop)
4492 * mono_property_get_parent:
4493 * @prop: the MonoProperty to act on.
4495 * Returns: the MonoClass where the property was defined.
4498 mono_property_get_parent (MonoProperty *prop)
4500 return prop->parent;
4504 * mono_property_get_flags:
4505 * @prop: the MonoProperty to act on.
4507 * The metadata flags for a property are encoded using the
4508 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4510 * Returns: the flags for the property.
4513 mono_property_get_flags (MonoProperty *prop)
4519 * mono_event_get_name:
4520 * @event: the MonoEvent to act on
4522 * Returns: the name of the event.
4525 mono_event_get_name (MonoEvent *event)
4531 * mono_event_get_add_method:
4532 * @event: The MonoEvent to act on.
4534 * Returns: the @add' method for the event (a MonoMethod).
4537 mono_event_get_add_method (MonoEvent *event)
4543 * mono_event_get_remove_method:
4544 * @event: The MonoEvent to act on.
4546 * Returns: the @remove method for the event (a MonoMethod).
4549 mono_event_get_remove_method (MonoEvent *event)
4551 return event->remove;
4555 * mono_event_get_raise_method:
4556 * @event: The MonoEvent to act on.
4558 * Returns: the @raise method for the event (a MonoMethod).
4561 mono_event_get_raise_method (MonoEvent *event)
4563 return event->raise;
4567 * mono_event_get_parent:
4568 * @event: the MonoEvent to act on.
4570 * Returns: the MonoClass where the event is defined.
4573 mono_event_get_parent (MonoEvent *event)
4575 return event->parent;
4579 * mono_event_get_flags
4580 * @event: the MonoEvent to act on.
4582 * The metadata flags for an event are encoded using the
4583 * EVENT_* constants. See the tabledefs.h file for details.
4585 * Returns: the flags for the event.
4588 mono_event_get_flags (MonoEvent *event)
4590 return event->attrs;
4594 * mono_class_get_method_from_name:
4595 * @klass: where to look for the method
4596 * @name_space: name of the method
4597 * @param_count: number of parameters. -1 for any number.
4599 * Obtains a MonoMethod with a given name and number of parameters.
4600 * It only works if there are no multiple signatures for any given method name.
4603 mono_class_get_method_from_name (MonoClass *klass, const char *name, int param_count)
4605 return mono_class_get_method_from_name_flags (klass, name, param_count, 0);
4609 * mono_class_get_method_from_name_flags:
4610 * @klass: where to look for the method
4611 * @name_space: name of the method
4612 * @param_count: number of parameters. -1 for any number.
4613 * @flags: flags which must be set in the method
4615 * Obtains a MonoMethod with a given name and number of parameters.
4616 * It only works if there are no multiple signatures for any given method name.
4619 mono_class_get_method_from_name_flags (MonoClass *klass, const char *name, int param_count, int flags)
4621 MonoMethod *res = NULL;
4624 mono_class_init (klass);
4626 if (klass->methods) {
4627 mono_class_setup_methods (klass);
4628 for (i = 0; i < klass->method.count; ++i) {
4629 MonoMethod *method = klass->methods [i];
4631 if (method->name[0] == name [0] &&
4632 !strcmp (name, method->name) &&
4633 (param_count == -1 || mono_method_signature (method)->param_count == param_count) &&
4634 ((method->flags & flags) == flags)) {
4641 /* Search directly in the metadata to avoid calling setup_methods () */
4642 for (i = 0; i < klass->method.count; ++i) {
4643 guint32 cols [MONO_METHOD_SIZE];
4646 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_METHOD], klass->method.first + i, cols, MONO_METHOD_SIZE);
4648 if (!strcmp (mono_metadata_string_heap (klass->image, cols [MONO_METHOD_NAME]), name)) {
4649 method = mono_get_method (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass);
4650 if ((param_count == -1) || mono_method_signature (method)->param_count == param_count) {
4662 * mono_class_set_failure:
4663 * @klass: class in which the failure was detected
4664 * @ex_type: the kind of exception/error to be thrown (later)
4665 * @ex_data: exception data (specific to each type of exception/error)
4667 * Keep a detected failure informations in the class for later processing.
4668 * Note that only the first failure is kept.
4671 mono_class_set_failure (MonoClass *klass, guint32 ex_type, void *ex_data)
4673 if (klass->exception_type)
4675 klass->exception_type = ex_type;
4676 klass->exception_data = ex_data;
4681 * mono_class_get_exception_for_failure:
4682 * @klass: class in which the failure was detected
4684 * Return a constructed MonoException than the caller can then throw
4685 * using mono_raise_exception - or NULL if no failure is present (or
4686 * doesn't result in an exception).
4689 mono_class_get_exception_for_failure (MonoClass *klass)
4691 switch (klass->exception_type) {
4692 case MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND: {
4693 MonoDomain *domain = mono_domain_get ();
4694 MonoSecurityManager* secman = mono_security_manager_get_methods ();
4695 MonoMethod *method = klass->exception_data;
4696 guint32 error = (method) ? MONO_METADATA_INHERITANCEDEMAND_METHOD : MONO_METADATA_INHERITANCEDEMAND_CLASS;
4697 MonoObject *exc = NULL;
4701 args [1] = mono_assembly_get_object (domain, mono_image_get_assembly (klass->image));
4702 args [2] = mono_type_get_object (domain, &klass->byval_arg);
4703 args [3] = (method) ? mono_method_get_object (domain, method, NULL) : NULL;
4705 mono_runtime_invoke (secman->inheritsecurityexception, NULL, args, &exc);
4706 return (MonoException*) exc;
4708 /* TODO - handle other class related failures */