2 * class.c: Class management for the Mono runtime
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
9 * Possible Optimizations:
10 * in mono_class_create, do not allocate the class right away,
11 * but wait until you know the size of the FieldMap, so that
12 * the class embeds directly the FieldMap after the vtable.
22 #include <mono/metadata/image.h>
23 #include <mono/metadata/assembly.h>
24 #include <mono/metadata/cil-coff.h>
25 #include <mono/metadata/metadata.h>
26 #include <mono/metadata/metadata-internals.h>
27 #include <mono/metadata/tabledefs.h>
28 #include <mono/metadata/tokentype.h>
29 #include <mono/metadata/class-internals.h>
30 #include <mono/metadata/object.h>
31 #include <mono/metadata/appdomain.h>
32 #include <mono/metadata/mono-endian.h>
33 #include <mono/metadata/debug-helpers.h>
34 #include <mono/metadata/reflection.h>
35 #include <mono/metadata/mono-debug-debugger.h>
36 #include <mono/metadata/security-manager.h>
37 #include <mono/os/gc_wrapper.h>
41 gboolean mono_print_vtable = FALSE;
43 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
44 static void mono_class_create_generic (MonoInflatedGenericClass *gclass);
45 static gboolean mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res);
47 void (*mono_debugger_start_class_init_func) (MonoClass *klass) = NULL;
48 void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
51 mono_class_from_typeref (MonoImage *image, guint32 type_token)
53 guint32 cols [MONO_TYPEREF_SIZE];
54 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
56 const char *name, *nspace;
58 MonoAssembly **references;
60 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
62 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
63 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
65 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
66 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
67 case MONO_RESOLTION_SCOPE_MODULE:
69 g_error ("null ResolutionScope not yet handled");
70 /* a typedef in disguise */
71 return mono_class_from_name (image, nspace, name);
72 case MONO_RESOLTION_SCOPE_MODULEREF:
73 return mono_class_from_name (image->modules [idx - 1], nspace, name);
74 case MONO_RESOLTION_SCOPE_TYPEREF: {
75 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
78 if (enclosing->inited) {
79 /* Micro-optimization: don't scan the metadata tables if enclosing is already inited */
80 for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
82 if (strcmp (res->name, name) == 0)
86 /* Don't call mono_class_init as we might've been called by it recursively */
87 int i = mono_metadata_nesting_typedef (enclosing->image, enclosing->type_token, 1);
89 guint32 class_nested = mono_metadata_decode_row_col (&enclosing->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, MONO_NESTED_CLASS_NESTED);
90 guint32 string_offset = mono_metadata_decode_row_col (&enclosing->image->tables [MONO_TABLE_TYPEDEF], class_nested - 1, MONO_TYPEDEF_NAME);
91 const char *nname = mono_metadata_string_heap (enclosing->image, string_offset);
93 if (strcmp (nname, name) == 0)
94 return mono_class_create_from_typedef (enclosing->image, MONO_TOKEN_TYPE_DEF | class_nested);
96 i = mono_metadata_nesting_typedef (enclosing->image, enclosing->type_token, i + 1);
99 g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
102 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
106 references = image->references;
107 if (!references [idx-1])
108 mono_assembly_load_reference (image, idx - 1);
109 if (references [idx - 1] == (gpointer)-1)
112 image = references [idx-1]->image;
114 return mono_class_from_name (image, nspace, name);
117 static inline MonoType*
118 dup_type (MonoType* t, const MonoType *original)
120 MonoType *r = g_new0 (MonoType, 1);
122 r->attrs = original->attrs;
123 r->byref = original->byref;
124 if (t->type == MONO_TYPE_PTR)
125 t->data.type = dup_type (t->data.type, original->data.type);
126 else if (t->type == MONO_TYPE_ARRAY)
127 t->data.array = mono_dup_array_type (t->data.array);
128 else if (t->type == MONO_TYPE_FNPTR)
129 t->data.method = mono_metadata_signature_deep_dup (t->data.method);
130 mono_stats.generics_metadata_size += sizeof (MonoType);
134 /* Copy everything mono_metadata_free_array free. */
136 mono_dup_array_type (MonoArrayType *a)
138 a = g_memdup (a, sizeof (MonoArrayType));
140 a->sizes = g_memdup (a->sizes, a->numsizes * sizeof (int));
142 a->lobounds = g_memdup (a->lobounds, a->numlobounds * sizeof (int));
146 /* Copy everything mono_metadata_free_method_signature free. */
148 mono_metadata_signature_deep_dup (MonoMethodSignature *sig)
152 sig = mono_metadata_signature_dup (sig);
154 sig->ret = dup_type (sig->ret, sig->ret);
155 for (i = 0; i < sig->param_count; ++i)
156 sig->params [i] = dup_type (sig->params [i], sig->params [i]);
162 _mono_type_get_assembly_name (MonoClass *klass, GString *str)
164 MonoAssembly *ta = klass->image->assembly;
166 g_string_append_printf (
167 str, ", %s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s",
169 ta->aname.major, ta->aname.minor, ta->aname.build, ta->aname.revision,
170 ta->aname.culture && *ta->aname.culture? ta->aname.culture: "neutral",
171 ta->aname.public_key_token [0] ? (char *)ta->aname.public_key_token : "null");
175 mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed,
176 MonoTypeNameFormat format)
180 switch (type->type) {
181 case MONO_TYPE_ARRAY: {
182 int i, rank = type->data.array->rank;
183 MonoTypeNameFormat nested_format;
185 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
186 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
188 mono_type_get_name_recurse (
189 &type->data.array->eklass->byval_arg, str, FALSE, nested_format);
190 g_string_append_c (str, '[');
191 for (i = 1; i < rank; i++)
192 g_string_append_c (str, ',');
193 g_string_append_c (str, ']');
194 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
195 _mono_type_get_assembly_name (type->data.array->eklass, str);
198 case MONO_TYPE_SZARRAY: {
199 MonoTypeNameFormat nested_format;
201 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
202 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
204 mono_type_get_name_recurse (
205 &type->data.klass->byval_arg, str, FALSE, nested_format);
206 g_string_append (str, "[]");
207 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
208 _mono_type_get_assembly_name (type->data.klass, str);
211 case MONO_TYPE_PTR: {
212 MonoTypeNameFormat nested_format;
214 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
215 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
217 mono_type_get_name_recurse (
218 type->data.type, str, FALSE, nested_format);
219 g_string_append (str, "*");
220 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
221 _mono_type_get_assembly_name (type->data.klass, str);
226 g_assert (type->data.generic_param->name);
227 g_string_append (str, type->data.generic_param->name);
230 klass = mono_class_from_mono_type (type);
231 if (klass->nested_in) {
232 mono_type_get_name_recurse (
233 &klass->nested_in->byval_arg, str, TRUE, format);
234 if (format == MONO_TYPE_NAME_FORMAT_IL)
235 g_string_append_c (str, '.');
237 g_string_append_c (str, '+');
238 } else if (*klass->name_space) {
239 g_string_append (str, klass->name_space);
240 g_string_append_c (str, '.');
242 if (format == MONO_TYPE_NAME_FORMAT_IL) {
243 char *s = strchr (klass->name, '`');
244 int len = s ? s - klass->name : strlen (klass->name);
246 g_string_append_len (str, klass->name, len);
248 g_string_append (str, klass->name);
251 if (klass->generic_class) {
252 MonoGenericClass *gclass = klass->generic_class;
253 MonoTypeNameFormat nested_format;
256 nested_format = format == MONO_TYPE_NAME_FORMAT_FULL_NAME ?
257 MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED : format;
259 if (format == MONO_TYPE_NAME_FORMAT_IL)
260 g_string_append_c (str, '<');
262 g_string_append_c (str, '[');
263 for (i = 0; i < gclass->inst->type_argc; i++) {
264 MonoType *t = gclass->inst->type_argv [i];
267 g_string_append_c (str, ',');
268 if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
269 (t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
270 g_string_append_c (str, '[');
271 mono_type_get_name_recurse (
272 gclass->inst->type_argv [i], str, FALSE, nested_format);
273 if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
274 (t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
275 g_string_append_c (str, ']');
277 if (format == MONO_TYPE_NAME_FORMAT_IL)
278 g_string_append_c (str, '>');
280 g_string_append_c (str, ']');
281 } else if (klass->generic_container &&
282 (format != MONO_TYPE_NAME_FORMAT_FULL_NAME) &&
283 (format != MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)) {
286 if (format == MONO_TYPE_NAME_FORMAT_IL)
287 g_string_append_c (str, '<');
289 g_string_append_c (str, '[');
290 for (i = 0; i < klass->generic_container->type_argc; i++) {
292 g_string_append_c (str, ',');
293 g_string_append (str, klass->generic_container->type_params [i].name);
295 if (format == MONO_TYPE_NAME_FORMAT_IL)
296 g_string_append_c (str, '>');
298 g_string_append_c (str, ']');
300 if ((format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
301 (type->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
302 _mono_type_get_assembly_name (klass, str);
308 * mono_type_get_name:
310 * @format: the format for the return string.
313 * Returns: the string representation in a number of formats:
315 * if format is MONO_TYPE_NAME_FORMAT_REFLECTION, the return string is
316 * returned in the formatrequired by System.Reflection, this is the
317 * inverse of mono_reflection_parse_type ().
319 * if format is MONO_TYPE_NAME_FORMAT_IL, it returns a syntax that can
320 * be used by the IL assembler.
322 * if format is MONO_TYPE_NAME_FORMAT_FULL_NAME
324 * if format is MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
327 mono_type_get_name_full (MonoType *type, MonoTypeNameFormat format)
331 if (format == MONO_TYPE_NAME_FORMAT_FULL_NAME &&
332 ((type->type == MONO_TYPE_VAR) || (type->type == MONO_TYPE_MVAR) ||
333 ((type->type == MONO_TYPE_GENERICINST) && type->data.generic_class->inst->is_open)))
336 result = g_string_new ("");
338 mono_type_get_name_recurse (type, result, FALSE, format);
341 g_string_append_c (result, '&');
343 return g_string_free (result, FALSE);
347 * mono_type_get_full_name:
350 * Returns: the string representation for type as required by System.Reflection.
351 * The inverse of mono_reflection_parse_type ().
354 mono_type_get_full_name (MonoClass *class)
356 return mono_type_get_name_full (mono_class_get_type (class), MONO_TYPE_NAME_FORMAT_REFLECTION);
360 mono_type_get_name (MonoType *type)
362 return mono_type_get_name_full (type, MONO_TYPE_NAME_FORMAT_IL);
366 mono_type_get_underlying_type (MonoType *type)
368 switch (type->type) {
369 case MONO_TYPE_VALUETYPE:
370 if (type->data.klass->enumtype)
371 return type->data.klass->enum_basetype;
373 case MONO_TYPE_GENERICINST:
374 return mono_type_get_underlying_type (&type->data.generic_class->container_class->byval_arg);
383 mono_class_is_open_constructed_type (MonoType *t)
389 case MONO_TYPE_SZARRAY:
390 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
391 case MONO_TYPE_ARRAY:
392 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
394 return mono_class_is_open_constructed_type (t->data.type);
395 case MONO_TYPE_GENERICINST: {
396 MonoGenericClass *gclass = t->data.generic_class;
399 if (mono_class_is_open_constructed_type (&gclass->container_class->byval_arg))
401 for (i = 0; i < gclass->inst->type_argc; i++)
402 if (mono_class_is_open_constructed_type (gclass->inst->type_argv [i]))
411 static MonoGenericClass *
412 inflate_generic_class (MonoGenericClass *ogclass, MonoGenericContext *context)
414 MonoInflatedGenericClass *igclass;
415 MonoGenericClass *ngclass, *cached;
417 if (ogclass->is_dynamic) {
418 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
419 igclass = &dgclass->generic_class;
420 ngclass = &igclass->generic_class;
421 ngclass->is_inflated = 1;
422 ngclass->is_dynamic = 1;
424 igclass = g_new0 (MonoInflatedGenericClass, 1);
425 ngclass = &igclass->generic_class;
426 ngclass->is_inflated = 1;
431 ngclass->inst = mono_metadata_inflate_generic_inst (ogclass->inst, context);
433 igclass->klass = NULL;
435 ngclass->context = g_new0 (MonoGenericContext, 1);
436 ngclass->context->container = ngclass->container_class->generic_container;
437 ngclass->context->gclass = ngclass;
440 cached = mono_metadata_lookup_generic_class (ngclass);
441 mono_loader_unlock ();
443 g_free (ngclass->context);
452 inflate_generic_type (MonoType *type, MonoGenericContext *context)
454 switch (type->type) {
456 if (context->gmethod && context->gmethod->inst->type_argv)
458 context->gmethod->inst->type_argv [type->data.generic_param->num],
465 context->gclass->inst->type_argv [type->data.generic_param->num],
469 case MONO_TYPE_SZARRAY: {
470 MonoClass *eclass = type->data.klass;
471 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
474 nt = dup_type (type, type);
475 nt->data.klass = mono_class_from_mono_type (inflated);
478 case MONO_TYPE_ARRAY: {
479 MonoClass *eclass = type->data.array->eklass;
480 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
483 nt = dup_type (type, type);
484 nt->data.array = g_memdup (nt->data.array, sizeof (MonoArrayType));
485 nt->data.array->eklass = mono_class_from_mono_type (inflated);
488 case MONO_TYPE_GENERICINST: {
489 MonoGenericClass *ngclass = inflate_generic_class (type->data.generic_class, context);
490 MonoType *nt = dup_type (type, type);
491 nt->data.generic_class = ngclass;
494 case MONO_TYPE_CLASS:
495 case MONO_TYPE_VALUETYPE: {
496 MonoClass *klass = type->data.klass;
497 MonoGenericClass *gclass;
500 if (!klass->generic_container)
503 gclass = inflate_generic_class (klass->generic_container->context.gclass, context);
505 nt = dup_type (type, type);
506 nt->type = MONO_TYPE_GENERICINST;
507 nt->data.generic_class = gclass;
516 MonoInflatedGenericClass*
517 mono_get_inflated_generic_class (MonoGenericClass *gclass)
519 g_assert (gclass->is_inflated);
520 mono_class_create_generic ((MonoInflatedGenericClass *) gclass);
521 return (MonoInflatedGenericClass *) gclass;
525 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
527 MonoType *inflated = inflate_generic_type (type, context);
530 return dup_type (type, type);
532 mono_stats.inflated_type_count++;
537 mono_class_inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
539 MonoMethodSignature *res;
546 res = mono_metadata_signature_alloc (image, sig->param_count);
547 res->ret = mono_class_inflate_generic_type (sig->ret, context);
548 is_open = mono_class_is_open_constructed_type (res->ret);
549 for (i = 0; i < sig->param_count; ++i) {
550 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
552 is_open = mono_class_is_open_constructed_type (res->params [i]);
554 res->hasthis = sig->hasthis;
555 res->explicit_this = sig->explicit_this;
556 res->call_convention = sig->call_convention;
557 res->pinvoke = sig->pinvoke;
558 res->generic_param_count = sig->generic_param_count;
559 res->sentinelpos = sig->sentinelpos;
560 res->has_type_parameters = is_open;
561 res->is_inflated = 1;
565 static MonoMethodHeader*
566 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
568 MonoMethodHeader *res;
570 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
571 res->code = header->code;
572 res->code_size = header->code_size;
573 res->max_stack = header->max_stack;
574 res->num_clauses = header->num_clauses;
575 res->init_locals = header->init_locals;
576 res->num_locals = header->num_locals;
577 res->clauses = header->clauses;
578 for (i = 0; i < header->num_locals; ++i)
579 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
583 static MonoGenericContext *
584 inflate_generic_context (MonoGenericContext *context, MonoGenericContext *inflate_with)
586 MonoGenericContext *res = g_new0 (MonoGenericContext, 1);
588 res->container = context->container;
591 res->gclass = inflate_generic_class (context->gclass, inflate_with);
593 if (context->gmethod) {
594 res->gmethod = g_new0 (MonoGenericMethod, 1);
595 res->gmethod->generic_class = res->gclass;
597 res->gmethod->container = context->gmethod->container;
598 res->gmethod->inst = mono_metadata_inflate_generic_inst (context->gmethod->inst, inflate_with);
600 res->gmethod = inflate_with->gmethod;
603 res->gmethod->container->parent = res->container;
604 res->container = res->gmethod->container;
611 * mono_class_inflate_generic_method:
613 * Instantiate method @method with the generic context @context.
616 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context)
618 MonoMethodInflated *result;
619 MonoGenericContainer *container = context ? context->container : NULL;
620 MonoMethodSignature *sig;
622 /* The `method' has already been instantiated before -> we need to create a new context. */
623 sig = mono_method_signature_full (method, container);
624 if (method->is_inflated || sig->is_inflated) {
625 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
627 context = inflate_generic_context (imethod->context, context);
628 method = imethod->declaring;
631 mono_stats.inflated_method_count++;
633 /* Just create a copy, but don't actually inflate the method for performance reasons. */
634 result = g_new0 (MonoMethodInflated, 1);
636 result->method.pinvoke = *(MonoMethodPInvoke*)method;
638 result->method.normal = *(MonoMethodNormal*)method;
639 result->method.method.is_inflated = 1;
640 result->context = context;
641 result->declaring = method;
643 if (result->method.method.klass->generic_class)
644 result->method.method.klass = result->method.method.klass->generic_class->container_class;
646 return (MonoMethod *) result;
650 * mono_get_inflated_method:
652 * For performance reasons, mono_class_inflate_generic_method() does not actually instantiate the
653 * method, it just "prepares" it for that. If you really need to fully instantiate the method
654 * (including its signature and header), call this method.
657 mono_get_inflated_method (MonoMethod *method)
659 MonoMethodInflated *imethod, *res;
660 MonoMethodHeader *mh;
664 if (!method->is_inflated)
667 imethod = (MonoMethodInflated *) method;
668 if (imethod->inflated)
669 return (MonoMethod *) imethod->inflated;
671 mono_stats.inflated_method_count_2++;
672 mono_stats.generics_metadata_size +=
673 sizeof (MonoMethodInflated) - sizeof (MonoMethodNormal);
675 res = g_new0 (MonoMethodInflated, 1);
677 res->inflated = imethod->inflated = res;
679 mh = mono_method_get_header (method);
681 res->method.normal.header = inflate_generic_header (mh, imethod->context);
683 dtype = mono_class_inflate_generic_type (&method->klass->byval_arg, imethod->context);
684 rklass = res->method.method.klass = mono_class_from_mono_type (dtype);
686 res->method.method.signature = mono_class_inflate_generic_signature (
687 method->klass->image, mono_method_signature (method), imethod->context);
689 return (MonoMethod *) res;
693 * mono_class_find_enum_basetype:
694 * @class: The enum class
696 * Determine the basetype of an enum by iterating through its fields. We do this
697 * in a separate function since it is cheaper than calling mono_class_setup_fields.
700 mono_class_find_enum_basetype (MonoClass *class)
702 MonoImage *m = class->image;
703 const int top = class->field.count;
704 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
707 g_assert (class->enumtype);
710 * Fetch all the field information.
712 for (i = 0; i < top; i++){
714 guint32 cols [MONO_FIELD_SIZE];
715 int idx = class->field.first + i;
716 MonoGenericContainer *container = NULL;
719 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
720 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
721 mono_metadata_decode_value (sig, &sig);
722 /* FIELD signature == 0x06 */
723 g_assert (*sig == 0x06);
724 if (class->generic_container)
725 container = class->generic_container;
726 else if (class->generic_class) {
727 MonoClass *gklass = class->generic_class->container_class;
729 container = gklass->generic_container;
730 g_assert (container);
732 ftype = mono_metadata_parse_type_full (
733 m, (MonoGenericContext *) container, MONO_PARSE_FIELD,
734 cols [MONO_FIELD_FLAGS], sig + 1, &sig);
735 if (class->generic_class) {
736 ftype = mono_class_inflate_generic_type (
737 ftype, class->generic_class->context);
738 ftype->attrs = cols [MONO_FIELD_FLAGS];
741 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC))
749 * mono_class_setup_fields:
750 * @class: The class to initialize
752 * Initializes the class->fields.
753 * Assumes the loader lock is held.
756 mono_class_setup_fields (MonoClass *class)
758 MonoImage *m = class->image;
759 const int top = class->field.count;
760 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
761 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
762 int i, blittable = TRUE, real_size = 0;
764 guint32 packing_size = 0;
765 gboolean explicit_size;
766 MonoClassField *field;
768 if (class->size_inited)
772 mono_class_init (class);
774 class->instance_size = 0;
775 class->class_size = 0;
778 if (!class->parent->size_inited)
779 mono_class_setup_fields (class->parent);
780 class->instance_size += class->parent->instance_size;
781 class->min_align = class->parent->min_align;
782 /* we use |= since it may have been set already */
783 class->has_references |= class->parent->has_references;
784 blittable = class->parent->blittable;
786 class->instance_size = sizeof (MonoObject);
787 class->min_align = 1;
790 /* Get the real size */
791 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
794 g_assert ((packing_size & 0xfffffff0) == 0);
795 class->packing_size = packing_size;
796 real_size += class->instance_size;
800 if (explicit_size && real_size) {
801 class->instance_size = MAX (real_size, class->instance_size);
803 class->size_inited = 1;
804 class->blittable = blittable;
808 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT)
811 /* Prevent infinite loops if the class references itself */
812 class->size_inited = 1;
814 class->fields = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoClassField) * top);
817 * Fetch all the field information.
819 for (i = 0; i < top; i++){
821 guint32 cols [MONO_FIELD_SIZE];
822 int idx = class->field.first + i;
823 MonoGenericContainer *container = NULL;
825 field = &class->fields [i];
826 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
827 /* The name is needed for fieldrefs */
828 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
829 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
830 mono_metadata_decode_value (sig, &sig);
831 /* FIELD signature == 0x06 */
832 g_assert (*sig == 0x06);
833 if (class->generic_container)
834 container = class->generic_container;
835 else if (class->generic_class) {
836 MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
837 MonoClass *gklass = class->generic_class->container_class;
839 container = gklass->generic_container;
840 g_assert (container);
842 ifield->generic_type = gklass->fields [i].type;
843 field->generic_info = ifield;
845 field->type = mono_metadata_parse_type_full (
846 m, (MonoGenericContext *) container, MONO_PARSE_FIELD,
847 cols [MONO_FIELD_FLAGS], sig + 1, &sig);
848 if (mono_field_is_deleted (field))
850 if (class->generic_class) {
851 field->type = mono_class_inflate_generic_type (
852 field->type, class->generic_class->context);
853 field->type->attrs = cols [MONO_FIELD_FLAGS];
856 field->parent = class;
858 /* Only do these checks if we still think this type is blittable */
859 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
860 if (field->type->byref || MONO_TYPE_IS_REFERENCE (field->type)) {
863 MonoClass *field_class = mono_class_from_mono_type (field->type);
864 if (!field_class || !field_class->blittable)
869 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
870 mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
871 if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
872 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, field->name);
875 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
876 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
878 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
879 field->data = mono_image_rva_map (class->image, rva);
882 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
883 class->enum_basetype = field->type;
884 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
885 blittable = class->element_class->blittable;
888 /* The def_value of fields is compute lazily during vtable creation */
891 if (class == mono_defaults.string_class)
894 class->blittable = blittable;
896 if (class->enumtype && !class->enum_basetype) {
897 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
900 if (explicit_size && real_size) {
901 class->instance_size = MAX (real_size, class->instance_size);
904 mono_class_layout_fields (class);
908 * mono_class_setup_fields_locking:
909 * @class: The class to initialize
911 * Initializes the class->fields.
912 * Aquires the loader lock.
915 mono_class_setup_fields_locking (MonoClass *class)
918 mono_class_setup_fields (class);
919 mono_loader_unlock ();
923 * mono_class_has_references:
925 * Returns whenever @klass->has_references is set, initializing it if needed.
926 * Aquires the loader lock.
929 mono_class_has_references (MonoClass *klass)
931 if (klass->init_pending) {
932 /* Be conservative */
935 mono_class_init (klass);
937 return klass->has_references;
941 /* useful until we keep track of gc-references in corlib etc. */
942 #define IS_GC_REFERENCE(t) ((t)->type == MONO_TYPE_U || (t)->type == MONO_TYPE_I || (t)->type == MONO_TYPE_PTR)
945 * LOCKING: this is supposed to be called with the loader lock held.
948 mono_class_layout_fields (MonoClass *class)
951 const int top = class->field.count;
952 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
953 guint32 pass, passes, real_size;
954 gboolean gc_aware_layout = FALSE;
955 MonoClassField *field;
957 if (class->generic_container ||
958 (class->generic_class && class->generic_class->inst->is_open))
962 * Enable GC aware auto layout: in this mode, reference
963 * fields are grouped together inside objects, increasing collector
965 * Requires that all classes whose layout is known to native code be annotated
966 * with [StructLayout (LayoutKind.Sequential)]
967 * Value types have gc_aware_layout disabled by default, as per
968 * what the default is for other runtimes.
970 /* corlib is missing [StructLayout] directives in many places */
971 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
972 if (class->image != mono_defaults.corlib &&
973 class->byval_arg.type != MONO_TYPE_VALUETYPE)
974 gc_aware_layout = TRUE;
977 /* Compute klass->has_references */
979 * Process non-static fields first, since static fields might recursively
980 * refer to the class itself.
982 for (i = 0; i < top; i++) {
985 field = &class->fields [i];
987 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
988 ftype = mono_type_get_underlying_type (field->type);
989 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype)))))
990 class->has_references = TRUE;
994 for (i = 0; i < top; i++) {
997 field = &class->fields [i];
999 if (!field->type->attrs & FIELD_ATTRIBUTE_STATIC) {
1000 ftype = mono_type_get_underlying_type (field->type);
1001 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype)))))
1002 class->has_static_refs = TRUE;
1006 for (i = 0; i < top; i++) {
1009 field = &class->fields [i];
1011 ftype = mono_type_get_underlying_type (field->type);
1012 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype))))) {
1013 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1014 class->has_static_refs = TRUE;
1016 class->has_references = TRUE;
1021 * Compute field layout and total size (not considering static fields)
1025 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
1026 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
1028 if (gc_aware_layout)
1033 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
1037 real_size = class->parent->instance_size;
1039 real_size = sizeof (MonoObject);
1041 for (pass = 0; pass < passes; ++pass) {
1042 for (i = 0; i < top; i++){
1045 field = &class->fields [i];
1047 if (mono_field_is_deleted (field))
1049 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1052 if (gc_aware_layout) {
1054 * We process fields with reference type in the first pass,
1055 * and fields with non-reference type in the second pass.
1056 * We use IS_POINTER instead of IS_REFERENCE because in
1057 * some internal structures, we store GC_MALLOCed memory
1058 * in IntPtr fields...
1060 if (MONO_TYPE_IS_POINTER (field->type)) {
1069 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
1070 (strcmp (field->name, "$PRIVATE$") == 0)) {
1071 /* This field is a hack inserted by MCS to empty structures */
1075 size = mono_type_size (field->type, &align);
1077 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
1078 align = class->packing_size ? MIN (class->packing_size, align): align;
1079 class->min_align = MAX (align, class->min_align);
1080 field->offset = real_size;
1081 field->offset += align - 1;
1082 field->offset &= ~(align - 1);
1083 real_size = field->offset + size;
1086 class->instance_size = MAX (real_size, class->instance_size);
1088 if (class->instance_size & (class->min_align - 1)) {
1089 class->instance_size += class->min_align - 1;
1090 class->instance_size &= ~(class->min_align - 1);
1094 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
1096 for (i = 0; i < top; i++) {
1099 field = &class->fields [i];
1102 * There must be info about all the fields in a type if it
1103 * uses explicit layout.
1106 if (mono_field_is_deleted (field))
1108 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1111 size = mono_type_size (field->type, &align);
1114 * When we get here, field->offset is already set by the
1115 * loader (for either runtime fields or fields loaded from metadata).
1116 * The offset is from the start of the object: this works for both
1117 * classes and valuetypes.
1119 field->offset += sizeof (MonoObject);
1124 real_size = MAX (real_size, size + field->offset);
1126 class->instance_size = MAX (real_size, class->instance_size);
1130 class->size_inited = 1;
1133 * Compute static field layout and size
1135 for (i = 0; i < top; i++){
1138 field = &class->fields [i];
1140 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
1142 if (mono_field_is_deleted (field))
1145 size = mono_type_size (field->type, &align);
1146 field->offset = class->class_size;
1147 field->offset += align - 1;
1148 field->offset &= ~(align - 1);
1149 class->class_size = field->offset + size;
1154 * mono_class_setup_methods:
1156 * Initializes the 'methods' array in the klass.
1157 * Calling this method should be avoided if possible since it allocates a lot
1158 * of long-living MonoMethod structures.
1161 mono_class_setup_methods (MonoClass *class)
1164 MonoMethod **methods;
1166 if (class->methods || class->generic_class)
1169 mono_loader_lock ();
1171 if (class->methods) {
1172 mono_loader_unlock ();
1176 //printf ("INIT: %s.%s\n", class->name_space, class->name);
1178 if (!class->generic_class && !class->methods) {
1179 methods = mono_mempool_alloc (class->image->mempool, sizeof (MonoMethod*) * class->method.count);
1180 for (i = 0; i < class->method.count; ++i) {
1181 methods [i] = mono_get_method (class->image, MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
1185 if (MONO_CLASS_IS_INTERFACE (class))
1186 for (i = 0; i < class->method.count; ++i)
1187 methods [i]->slot = i;
1189 /* Leave this assignment as the last op in this function */
1190 class->methods = methods;
1192 mono_loader_unlock ();
1197 mono_class_setup_properties (MonoClass *class)
1199 guint startm, endm, i, j;
1200 guint32 cols [MONO_PROPERTY_SIZE];
1201 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
1202 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
1203 MonoProperty *properties;
1206 if (class->properties)
1209 mono_loader_lock ();
1211 if (class->properties) {
1212 mono_loader_unlock ();
1216 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &last);
1217 class->property.count = last - class->property.first;
1219 if (class->property.count)
1220 mono_class_setup_methods (class);
1222 properties = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoProperty) * class->property.count);
1223 for (i = class->property.first; i < last; ++i) {
1224 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
1225 properties [i - class->property.first].parent = class;
1226 properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
1227 properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
1229 startm = mono_metadata_methods_from_property (class->image, i, &endm);
1230 for (j = startm; j < endm; ++j) {
1231 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
1232 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1233 case METHOD_SEMANTIC_SETTER:
1234 properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1236 case METHOD_SEMANTIC_GETTER:
1237 properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1245 /* Leave this assignment as the last op in the function */
1246 class->properties = properties;
1248 mono_loader_unlock ();
1252 mono_class_setup_events (MonoClass *class)
1254 guint startm, endm, i, j;
1255 guint32 cols [MONO_EVENT_SIZE];
1256 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
1257 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
1264 mono_loader_lock ();
1266 if (class->events) {
1267 mono_loader_unlock ();
1270 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &last);
1271 class->event.count = last - class->event.first;
1273 if (class->event.count)
1274 mono_class_setup_methods (class);
1276 events = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoEvent) * class->event.count);
1277 for (i = class->event.first; i < last; ++i) {
1278 MonoEvent *event = &events [i - class->event.first];
1280 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
1281 event->parent = class;
1282 event->attrs = cols [MONO_EVENT_FLAGS];
1283 event->name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
1285 startm = mono_metadata_methods_from_event (class->image, i, &endm);
1286 for (j = startm; j < endm; ++j) {
1287 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
1288 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1289 case METHOD_SEMANTIC_ADD_ON:
1290 event->add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1292 case METHOD_SEMANTIC_REMOVE_ON:
1293 event->remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1295 case METHOD_SEMANTIC_FIRE:
1296 event->raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1298 case METHOD_SEMANTIC_OTHER: {
1301 if (event->other == NULL) {
1302 event->other = g_new0 (MonoMethod*, 1);
1304 while (event->other [n])
1306 event->other = g_realloc (event->other, (n + 1) * sizeof (MonoMethod*));
1308 event->other [n] = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1316 /* Leave this assignment as the last op in the function */
1317 class->events = events;
1319 mono_loader_unlock ();
1323 mono_get_unique_iid (MonoClass *class)
1325 static GHashTable *iid_hash = NULL;
1326 static guint iid = 0;
1331 g_assert (MONO_CLASS_IS_INTERFACE (class));
1333 mono_loader_lock ();
1336 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
1338 str = g_strdup_printf ("%s|%s.%s\n", class->image->name, class->name_space, class->name);
1340 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
1341 mono_loader_unlock ();
1343 return GPOINTER_TO_INT (value);
1345 g_hash_table_insert (iid_hash, str, GINT_TO_POINTER (iid));
1349 mono_loader_unlock ();
1351 g_assert (iid <= 65535);
1356 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
1361 for (i = 0; i < klass->interface_count; i++) {
1362 ic = klass->interfaces [i];
1365 *res = g_ptr_array_new ();
1366 g_ptr_array_add (*res, ic);
1367 mono_class_init (ic);
1369 collect_implemented_interfaces_aux (ic, res);
1374 mono_class_get_implemented_interfaces (MonoClass *klass)
1376 GPtrArray *res = NULL;
1378 collect_implemented_interfaces_aux (klass, &res);
1382 typedef struct _IOffsetInfo IOffsetInfo;
1383 struct _IOffsetInfo {
1387 int data [MONO_ZERO_LEN_ARRAY];
1390 static IOffsetInfo *cached_offset_info = NULL;
1391 static int next_offset_info_size = 128;
1394 cache_interface_offsets (int max_iid, int *data)
1396 IOffsetInfo *cached_info;
1399 for (cached_info = cached_offset_info; cached_info; cached_info = cached_info->next) {
1400 cached = cached_info->data;
1401 while (cached < cached_info->data + cached_info->size && *cached) {
1402 if (*cached == max_iid) {
1403 int i, matched = TRUE;
1405 for (i = 0; i < max_iid; ++i) {
1406 if (cached [i] != data [i]) {
1415 cached += *cached + 1;
1419 /* find a free slot */
1420 for (cached_info = cached_offset_info; cached_info; cached_info = cached_info->next) {
1421 if (cached_info->size - cached_info->next_free >= max_iid + 1) {
1422 cached = &cached_info->data [cached_info->next_free];
1423 *cached++ = max_iid;
1424 memcpy (cached, data, max_iid * sizeof (int));
1425 cached_info->next_free += max_iid + 1;
1429 /* allocate a new chunk */
1430 if (max_iid + 1 < next_offset_info_size) {
1431 new_size = next_offset_info_size;
1432 if (next_offset_info_size < 4096)
1433 next_offset_info_size += next_offset_info_size >> 2;
1435 new_size = max_iid + 1;
1437 cached_info = g_malloc0 (sizeof (IOffsetInfo) + sizeof (int) * new_size);
1438 cached_info->size = new_size;
1439 /*g_print ("allocated %d offset entries at %p (total: %d)\n", new_size, cached_info->data, offset_info_total_size);*/
1440 cached = &cached_info->data [0];
1441 *cached++ = max_iid;
1442 memcpy (cached, data, max_iid * sizeof (int));
1443 cached_info->next_free += max_iid + 1;
1444 cached_info->next = cached_offset_info;
1445 cached_offset_info = cached_info;
1450 setup_interface_offsets (MonoClass *class, int cur_slot)
1457 /* compute maximum number of slots and maximum interface id */
1459 for (k = class; k ; k = k->parent) {
1460 for (i = 0; i < k->interface_count; i++) {
1461 ic = k->interfaces [i];
1464 mono_class_init (ic);
1466 if (max_iid < ic->interface_id)
1467 max_iid = ic->interface_id;
1469 ifaces = mono_class_get_implemented_interfaces (k);
1471 for (i = 0; i < ifaces->len; ++i) {
1472 ic = g_ptr_array_index (ifaces, i);
1473 if (max_iid < ic->interface_id)
1474 max_iid = ic->interface_id;
1476 g_ptr_array_free (ifaces, TRUE);
1480 if (MONO_CLASS_IS_INTERFACE (class)) {
1481 if (max_iid < class->interface_id)
1482 max_iid = class->interface_id;
1484 class->max_interface_id = max_iid;
1485 /* compute vtable offset for interfaces */
1486 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
1488 for (i = 0; i <= max_iid; i++)
1489 class->interface_offsets [i] = -1;
1491 ifaces = mono_class_get_implemented_interfaces (class);
1493 for (i = 0; i < ifaces->len; ++i) {
1494 ic = g_ptr_array_index (ifaces, i);
1495 class->interface_offsets [ic->interface_id] = cur_slot;
1496 cur_slot += ic->method.count;
1498 g_ptr_array_free (ifaces, TRUE);
1501 for (k = class->parent; k ; k = k->parent) {
1502 ifaces = mono_class_get_implemented_interfaces (k);
1504 for (i = 0; i < ifaces->len; ++i) {
1505 ic = g_ptr_array_index (ifaces, i);
1507 if (class->interface_offsets [ic->interface_id] == -1) {
1508 int io = k->interface_offsets [ic->interface_id];
1512 class->interface_offsets [ic->interface_id] = io;
1515 g_ptr_array_free (ifaces, TRUE);
1519 if (MONO_CLASS_IS_INTERFACE (class))
1520 class->interface_offsets [class->interface_id] = cur_slot;
1522 cached_data = cache_interface_offsets (max_iid + 1, class->interface_offsets);
1523 g_free (class->interface_offsets);
1524 class->interface_offsets = cached_data;
1530 mono_class_setup_vtable (MonoClass *class)
1532 MonoMethod **overrides;
1533 MonoGenericContext *context;
1539 mono_class_setup_methods (class);
1541 if (MONO_CLASS_IS_INTERFACE (class))
1544 mono_loader_lock ();
1546 if (class->vtable) {
1547 mono_loader_unlock ();
1551 if (class->generic_class)
1552 context = class->generic_class->context;
1554 context = (MonoGenericContext *) class->generic_container;
1556 mono_class_get_overrides_full (class->image, class->type_token, &overrides, &onum, context);
1557 mono_class_setup_vtable_general (class, overrides, onum);
1560 mono_loader_unlock ();
1564 setup_generic_vtable (MonoClass *class, MonoMethod **overrides, int onum)
1569 gklass = class->generic_class->container_class;
1571 mono_class_init (gklass);
1572 class->vtable_size = gklass->vtable_size;
1574 class->vtable = g_new0 (MonoMethod*, class->vtable_size);
1575 memcpy (class->vtable, gklass->vtable, sizeof (MonoMethod*) * class->vtable_size);
1577 for (i = 0; i < class->vtable_size; i++) {
1578 MonoMethod *m = class->vtable [i];
1583 m = mono_class_inflate_generic_method (m, class->generic_class->context);
1584 class->vtable [i] = m;
1587 class->max_interface_id = gklass->max_interface_id;
1588 class->interface_offsets = g_new0 (gint, gklass->max_interface_id + 1);
1589 memcpy (class->interface_offsets, gklass->interface_offsets,
1590 sizeof (gint) * (gklass->max_interface_id + 1));
1594 * LOCKING: this is supposed to be called with the loader lock held.
1597 mono_class_setup_vtable_general (MonoClass *class, MonoMethod **overrides, int onum)
1600 MonoMethod **vtable;
1601 int i, max_vtsize = 0, max_iid, cur_slot = 0;
1603 GHashTable *override_map = NULL;
1604 gboolean security_enabled = mono_is_security_manager_active ();
1609 if (class->generic_class) {
1610 setup_generic_vtable (class, overrides, onum);
1614 ifaces = mono_class_get_implemented_interfaces (class);
1616 for (i = 0; i < ifaces->len; i++) {
1617 MonoClass *ic = g_ptr_array_index (ifaces, i);
1618 max_vtsize += ic->method.count;
1620 g_ptr_array_free (ifaces, TRUE);
1623 if (class->parent) {
1624 mono_class_init (class->parent);
1625 mono_class_setup_vtable (class->parent);
1626 max_vtsize += class->parent->vtable_size;
1627 cur_slot = class->parent->vtable_size;
1630 max_vtsize += class->method.count;
1632 vtable = alloca (sizeof (gpointer) * max_vtsize);
1633 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
1635 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
1637 cur_slot = setup_interface_offsets (class, cur_slot);
1638 max_iid = class->max_interface_id;
1640 if (class->parent && class->parent->vtable_size)
1641 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
1643 /* override interface methods */
1644 for (i = 0; i < onum; i++) {
1645 MonoMethod *decl = overrides [i*2];
1646 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
1648 mono_class_setup_methods (decl->klass);
1649 g_assert (decl->slot != -1);
1650 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
1651 vtable [dslot] = overrides [i*2 + 1];
1652 vtable [dslot]->slot = dslot;
1654 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1656 g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
1660 for (k = class; k ; k = k->parent) {
1663 ifaces = mono_class_get_implemented_interfaces (k);
1665 nifaces = ifaces->len;
1666 for (i = 0; i < nifaces; i++) {
1669 ic = g_ptr_array_index (ifaces, i);
1670 io = k->interface_offsets [ic->interface_id];
1673 g_assert (io <= max_vtsize);
1676 mono_class_setup_methods (ic);
1677 for (l = 0; l < ic->method.count; l++) {
1678 MonoMethod *im = ic->methods [l];
1680 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1683 for (j = 0; j < class->method.count; ++j) {
1684 MonoMethod *cm = class->methods [j];
1685 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1686 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
1687 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
1689 if (!strcmp(cm->name, im->name) &&
1690 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1692 /* CAS - SecurityAction.InheritanceDemand on interface */
1693 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1694 mono_secman_inheritancedemand_method (cm, im);
1697 g_assert (io + l <= max_vtsize);
1698 vtable [io + l] = cm;
1703 /* already implemented */
1704 if (io >= k->vtable_size)
1708 for (l = 0; l < ic->method.count; l++) {
1709 MonoMethod *im = ic->methods [l];
1712 g_assert (io + l <= max_vtsize);
1714 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1717 for (k1 = class; k1; k1 = k1->parent) {
1718 for (j = 0; j < k1->method.count; ++j) {
1719 MonoMethod *cm = k1->methods [j];
1721 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1722 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
1725 if (!strcmp(cm->name, im->name) &&
1726 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1728 /* CAS - SecurityAction.InheritanceDemand on interface */
1729 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1730 mono_secman_inheritancedemand_method (cm, im);
1733 g_assert (io + l <= max_vtsize);
1734 vtable [io + l] = cm;
1739 g_assert (io + l <= max_vtsize);
1740 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1745 for (l = 0; l < ic->method.count; l++) {
1746 MonoMethod *im = ic->methods [l];
1747 char *qname, *fqname, *cname, *the_cname;
1750 if (vtable [io + l])
1753 if (ic->generic_class) {
1754 the_cname = mono_type_get_name_full (&ic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
1758 cname = (char*)ic->name;
1761 qname = g_strconcat (cname, ".", im->name, NULL);
1762 if (ic->name_space && ic->name_space [0])
1763 fqname = g_strconcat (ic->name_space, ".", cname, ".", im->name, NULL);
1767 for (k1 = class; k1; k1 = k1->parent) {
1768 for (j = 0; j < k1->method.count; ++j) {
1769 MonoMethod *cm = k1->methods [j];
1771 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1774 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
1775 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1777 /* CAS - SecurityAction.InheritanceDemand on interface */
1778 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1779 mono_secman_inheritancedemand_method (cm, im);
1782 g_assert (io + l <= max_vtsize);
1783 vtable [io + l] = cm;
1794 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
1795 for (l = 0; l < ic->method.count; l++) {
1797 MonoMethod *im = ic->methods [l];
1798 if (im->flags & METHOD_ATTRIBUTE_STATIC)
1800 g_assert (io + l <= max_vtsize);
1803 * If one of our parents already implements this interface
1804 * we can inherit the implementation.
1806 if (!(vtable [io + l])) {
1807 MonoClass *parent = class->parent;
1809 if ((ic->interface_id <= parent->max_interface_id) &&
1810 (parent->interface_offsets [ic->interface_id]) &&
1812 vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
1815 if (!(vtable [io + l])) {
1816 for (j = 0; j < onum; ++j) {
1817 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
1818 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
1820 msig = mono_signature_get_desc (mono_method_signature (im), FALSE);
1821 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
1822 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
1824 for (j = 0; j < class->method.count; ++j) {
1825 MonoMethod *cm = class->methods [j];
1826 msig = mono_signature_get_desc (mono_method_signature (cm), FALSE);
1828 printf ("METHOD %s(%s)\n", cm->name, msig);
1831 g_assert_not_reached ();
1836 for (l = 0; l < ic->method.count; l++) {
1837 MonoMethod *im = vtable [io + l];
1840 g_assert (io + l <= max_vtsize);
1842 /* FIXME: why do we need this ? */
1844 /* g_assert_not_reached (); */
1850 g_ptr_array_free (ifaces, TRUE);
1853 for (i = 0; i < class->method.count; ++i) {
1856 cm = class->methods [i];
1859 * Non-virtual method have no place in the vtable.
1860 * This also catches static methods (since they are not virtual).
1862 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1866 * If the method is REUSE_SLOT, we must check in the
1867 * base class for a method to override.
1869 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
1871 for (k = class->parent; k ; k = k->parent) {
1873 for (j = 0; j < k->method.count; ++j) {
1874 MonoMethod *m1 = k->methods [j];
1875 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
1877 if (!strcmp(cm->name, m1->name) &&
1878 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (m1))) {
1880 /* CAS - SecurityAction.InheritanceDemand */
1881 if (security_enabled && (m1->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1882 mono_secman_inheritancedemand_method (cm, m1);
1885 slot = k->methods [j]->slot;
1886 g_assert (cm->slot < max_vtsize);
1888 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1889 g_hash_table_insert (override_map, m1, cm);
1901 cm->slot = cur_slot++;
1903 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
1904 vtable [cm->slot] = cm;
1907 /* override non interface methods */
1908 for (i = 0; i < onum; i++) {
1909 MonoMethod *decl = overrides [i*2];
1910 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
1911 g_assert (decl->slot != -1);
1912 vtable [decl->slot] = overrides [i*2 + 1];
1913 overrides [i * 2 + 1]->slot = decl->slot;
1915 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1916 g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
1921 * If a method occupies more than one place in the vtable, and it is
1922 * overriden, then change the other occurances too.
1925 for (i = 0; i < max_vtsize; ++i)
1927 MonoMethod *cm = g_hash_table_lookup (override_map, vtable [i]);
1932 g_hash_table_destroy (override_map);
1935 if (class->generic_class) {
1936 MonoClass *gklass = class->generic_class->container_class;
1938 mono_class_init (gklass);
1939 class->vtable_size = gklass->vtable_size;
1941 class->vtable_size = cur_slot;
1943 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
1944 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
1946 if (mono_print_vtable) {
1949 for (i = 0; i <= max_iid; i++)
1950 if (class->interface_offsets [i] != -1)
1953 printf ("VTable %s.%s (size = %d, interfaces = %d)\n", class->name_space,
1954 class->name, class->vtable_size, icount);
1956 for (i = 0; i < class->vtable_size; ++i) {
1961 printf (" slot %03d(%03d) %s.%s:%s\n", i, cm->slot,
1962 cm->klass->name_space, cm->klass->name,
1969 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
1970 class->name, max_iid);
1972 for (i = 0; i < class->interface_count; i++) {
1973 ic = class->interfaces [i];
1974 printf (" slot %03d(%03d) %s.%s\n",
1975 class->interface_offsets [ic->interface_id],
1976 ic->method.count, ic->name_space, ic->name);
1979 for (k = class->parent; k ; k = k->parent) {
1980 for (i = 0; i < k->interface_count; i++) {
1981 ic = k->interfaces [i];
1982 printf (" slot %03d(%03d) %s.%s\n",
1983 class->interface_offsets [ic->interface_id],
1984 ic->method.count, ic->name_space, ic->name);
1993 * @class: the class to initialize
1995 * compute the instance_size, class_size and other infos that cannot be
1996 * computed at mono_class_get() time. Also compute a generic vtable and
1997 * the method slot numbers. We use this infos later to create a domain
2001 mono_class_init (MonoClass *class)
2004 static MonoMethod *default_ghc = NULL;
2005 static MonoMethod *default_finalize = NULL;
2006 static int finalize_slot = -1;
2007 static int ghc_slot = -1;
2008 MonoCachedClassInfo cached_info;
2009 gboolean has_cached_info;
2016 /*g_print ("Init class %s\n", class->name);*/
2018 /* We do everything inside the lock to prevent races */
2019 mono_loader_lock ();
2021 if (class->inited) {
2022 mono_loader_unlock ();
2023 /* Somebody might have gotten in before us */
2027 if (class->init_pending) {
2028 mono_loader_unlock ();
2029 /* this indicates a cyclic dependency */
2030 g_error ("pending init %s.%s\n", class->name_space, class->name);
2033 class->init_pending = 1;
2035 /* CAS - SecurityAction.InheritanceDemand */
2036 if (mono_is_security_manager_active () && class->parent && (class->parent->flags & TYPE_ATTRIBUTE_HAS_SECURITY)) {
2037 mono_secman_inheritancedemand_class (class, class->parent);
2040 if (mono_debugger_start_class_init_func)
2041 mono_debugger_start_class_init_func (class);
2043 mono_stats.initialized_class_count++;
2045 if (class->generic_class && !class->generic_class->is_dynamic) {
2046 MonoInflatedGenericClass *gclass;
2049 gclass = mono_get_inflated_generic_class (class->generic_class);
2050 gklass = gclass->generic_class.container_class;
2052 mono_stats.generic_class_count++;
2054 class->method = gklass->method;
2055 class->field = gklass->field;
2057 mono_class_init (gklass);
2058 mono_class_setup_methods (gklass);
2059 mono_class_setup_properties (gklass);
2061 if (MONO_CLASS_IS_INTERFACE (class))
2062 class->interface_id = mono_get_unique_iid (class);
2064 g_assert (class->method.count == gklass->method.count);
2065 class->methods = g_new0 (MonoMethod *, class->method.count);
2067 for (i = 0; i < class->method.count; i++) {
2068 MonoMethod *inflated = mono_class_inflate_generic_method (
2069 gklass->methods [i], gclass->generic_class.context);
2071 class->methods [i] = mono_get_inflated_method (inflated);
2074 class->property = gklass->property;
2075 class->properties = g_new0 (MonoProperty, class->property.count);
2077 for (i = 0; i < class->property.count; i++) {
2078 MonoProperty *prop = &class->properties [i];
2080 *prop = gklass->properties [i];
2083 prop->get = mono_class_inflate_generic_method (
2084 prop->get, gclass->generic_class.context);
2086 prop->set = mono_class_inflate_generic_method (
2087 prop->set, gclass->generic_class.context);
2089 prop->parent = class;
2092 g_assert (class->interface_count == gklass->interface_count);
2095 if (class->parent && !class->parent->inited)
2096 mono_class_init (class->parent);
2098 has_cached_info = mono_class_get_cached_class_info (class, &cached_info);
2100 if (!class->generic_class && (!has_cached_info || (has_cached_info && cached_info.has_nested_classes))) {
2101 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
2104 guint32 cols [MONO_NESTED_CLASS_SIZE];
2105 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
2106 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
2107 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
2109 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
2114 * Computes the size used by the fields, and their locations
2116 if (has_cached_info) {
2117 class->instance_size = cached_info.instance_size;
2118 class->class_size = cached_info.class_size;
2119 class->packing_size = cached_info.packing_size;
2120 class->min_align = cached_info.min_align;
2121 class->blittable = cached_info.blittable;
2122 class->has_references = cached_info.has_references;
2123 class->has_static_refs = cached_info.has_static_refs;
2126 if (!class->size_inited)
2127 mono_class_setup_fields (class);
2129 /* initialize method pointers */
2132 MonoMethodSignature *sig;
2133 class->method.count = class->rank > 1? 2: 1;
2134 sig = mono_metadata_signature_alloc (class->image, class->rank);
2135 sig->ret = &mono_defaults.void_class->byval_arg;
2136 sig->pinvoke = TRUE;
2137 for (i = 0; i < class->rank; ++i)
2138 sig->params [i] = &mono_defaults.int32_class->byval_arg;
2140 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
2141 ctor->klass = class;
2142 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
2143 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
2144 ctor->signature = sig;
2145 ctor->name = ".ctor";
2147 class->methods = g_new (MonoMethod*, class->method.count);
2148 class->methods [0] = ctor;
2149 if (class->rank > 1) {
2150 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
2151 sig->ret = &mono_defaults.void_class->byval_arg;
2152 sig->pinvoke = TRUE;
2153 for (i = 0; i < class->rank * 2; ++i)
2154 sig->params [i] = &mono_defaults.int32_class->byval_arg;
2156 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
2157 ctor->klass = class;
2158 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
2159 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
2160 ctor->signature = sig;
2161 ctor->name = ".ctor";
2163 class->methods [1] = ctor;
2167 mono_class_setup_supertypes (class);
2170 if (class == mono_defaults.object_class) {
2171 mono_class_setup_vtable (class);
2172 for (i = 0; i < class->vtable_size; ++i) {
2173 MonoMethod *cm = class->vtable [i];
2175 if (!strcmp (cm->name, "GetHashCode")) {
2181 g_assert (ghc_slot > 0);
2183 default_ghc = class->vtable [ghc_slot];
2187 if (!default_finalize) {
2188 if (class == mono_defaults.object_class) {
2189 mono_class_setup_vtable (class);
2190 for (i = 0; i < class->vtable_size; ++i) {
2191 MonoMethod *cm = class->vtable [i];
2193 if (!strcmp (cm->name, "Finalize")) {
2199 g_assert (finalize_slot > 0);
2201 default_finalize = class->vtable [finalize_slot];
2206 * If possible, avoid the creation of the generic vtable by requesting
2207 * cached info from the runtime.
2209 if (has_cached_info) {
2210 guint32 cur_slot = 0;
2212 class->vtable_size = cached_info.vtable_size;
2213 class->has_finalize = cached_info.has_finalize;
2214 class->ghcimpl = cached_info.ghcimpl;
2215 class->has_cctor = cached_info.has_cctor;
2217 if (class->parent) {
2218 mono_class_init (class->parent);
2219 cur_slot = class->parent->vtable_size;
2222 setup_interface_offsets (class, cur_slot);
2225 mono_class_setup_vtable (class);
2228 if (class->parent) {
2229 MonoMethod *cmethod = class->vtable [ghc_slot];
2230 if (cmethod->is_inflated)
2231 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
2232 if (cmethod == default_ghc) {
2237 /* Object::Finalize should have empty implemenatation */
2238 class->has_finalize = 0;
2239 if (class->parent) {
2240 MonoMethod *cmethod = class->vtable [finalize_slot];
2241 if (cmethod->is_inflated)
2242 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
2243 if (cmethod != default_finalize) {
2244 class->has_finalize = 1;
2248 for (i = 0; i < class->method.count; ++i) {
2249 MonoMethod *method = class->methods [i];
2250 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2251 (strcmp (".cctor", method->name) == 0)) {
2252 class->has_cctor = 1;
2259 class->init_pending = 0;
2261 mono_loader_unlock ();
2263 if (MONO_CLASS_IS_INTERFACE (class)) {
2265 * class->interface_offsets is needed for the castclass/isinst code, so
2266 * we have to setup them for interfaces, too.
2268 setup_interface_offsets (class, 0);
2271 if (mono_debugger_class_init_func)
2272 mono_debugger_class_init_func (class);
2276 * LOCKING: this assumes the loader lock is held
2279 mono_class_setup_mono_type (MonoClass *class)
2281 const char *name = class->name;
2282 const char *nspace = class->name_space;
2284 if (MONO_CLASS_IS_INTERFACE (class))
2285 class->interface_id = mono_get_unique_iid (class);
2287 class->this_arg.byref = 1;
2288 class->this_arg.data.klass = class;
2289 class->this_arg.type = MONO_TYPE_CLASS;
2290 class->byval_arg.data.klass = class;
2291 class->byval_arg.type = MONO_TYPE_CLASS;
2293 if (!strcmp (nspace, "System")) {
2294 if (!strcmp (name, "ValueType")) {
2296 * do not set the valuetype bit for System.ValueType.
2297 * class->valuetype = 1;
2299 class->blittable = TRUE;
2300 } else if (!strcmp (name, "Enum")) {
2302 * do not set the valuetype bit for System.Enum.
2303 * class->valuetype = 1;
2305 class->valuetype = 0;
2306 class->enumtype = 0;
2307 } else if (!strcmp (name, "Object")) {
2308 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
2309 } else if (!strcmp (name, "String")) {
2310 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
2311 } else if (!strcmp (name, "TypedReference")) {
2312 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
2316 if (class->valuetype) {
2317 int t = MONO_TYPE_VALUETYPE;
2318 if (!strcmp (nspace, "System")) {
2321 if (!strcmp (name, "Boolean")) {
2322 t = MONO_TYPE_BOOLEAN;
2323 } else if (!strcmp(name, "Byte")) {
2325 class->blittable = TRUE;
2329 if (!strcmp (name, "Char")) {
2334 if (!strcmp (name, "Double")) {
2336 class->blittable = TRUE;
2340 if (!strcmp (name, "Int32")) {
2342 class->blittable = TRUE;
2343 } else if (!strcmp(name, "Int16")) {
2345 class->blittable = TRUE;
2346 } else if (!strcmp(name, "Int64")) {
2348 class->blittable = TRUE;
2349 } else if (!strcmp(name, "IntPtr")) {
2351 class->blittable = TRUE;
2355 if (!strcmp (name, "Single")) {
2357 class->blittable = TRUE;
2358 } else if (!strcmp(name, "SByte")) {
2360 class->blittable = TRUE;
2364 if (!strcmp (name, "UInt32")) {
2366 class->blittable = TRUE;
2367 } else if (!strcmp(name, "UInt16")) {
2369 class->blittable = TRUE;
2370 } else if (!strcmp(name, "UInt64")) {
2372 class->blittable = TRUE;
2373 } else if (!strcmp(name, "UIntPtr")) {
2375 class->blittable = TRUE;
2379 if (!strcmp (name, "TypedReference")) {
2380 t = MONO_TYPE_TYPEDBYREF;
2381 class->blittable = TRUE;
2385 if (!strcmp (name, "Void")) {
2393 class->this_arg.type = class->byval_arg.type = t;
2398 * LOCKING: this assumes the loader lock is held
2401 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
2403 gboolean system_namespace;
2405 system_namespace = !strcmp (class->name_space, "System");
2407 /* if root of the hierarchy */
2408 if (system_namespace && !strcmp (class->name, "Object")) {
2409 class->parent = NULL;
2410 class->instance_size = sizeof (MonoObject);
2413 if (!strcmp (class->name, "<Module>")) {
2414 class->parent = NULL;
2415 class->instance_size = 0;
2419 if (!MONO_CLASS_IS_INTERFACE (class)) {
2420 class->parent = parent;
2423 g_assert_not_reached (); /* FIXME */
2425 if (parent->generic_class && !parent->name) {
2427 * If the parent is a generic instance, we may get
2428 * called before it is fully initialized, especially
2429 * before it has its name.
2434 class->marshalbyref = parent->marshalbyref;
2435 class->contextbound = parent->contextbound;
2436 class->delegate = parent->delegate;
2438 if (system_namespace) {
2439 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
2440 class->marshalbyref = 1;
2442 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
2443 class->contextbound = 1;
2445 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
2446 class->delegate = 1;
2449 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
2450 (strcmp (class->parent->name_space, "System") == 0)))
2451 class->valuetype = 1;
2452 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
2453 class->valuetype = class->enumtype = 1;
2455 /*class->enumtype = class->parent->enumtype; */
2456 mono_class_setup_supertypes (class);
2458 class->parent = NULL;
2464 * LOCKING: this assumes the loader lock is held
2467 mono_class_setup_supertypes (MonoClass *class)
2471 if (class->supertypes)
2474 if (class->parent && !class->parent->supertypes)
2475 mono_class_setup_supertypes (class->parent);
2477 class->idepth = class->parent->idepth + 1;
2481 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
2482 class->supertypes = g_new0 (MonoClass *, ms);
2484 if (class->parent) {
2485 class->supertypes [class->idepth - 1] = class;
2486 memcpy (class->supertypes, class->parent->supertypes, class->parent->idepth * sizeof (gpointer));
2488 class->supertypes [0] = class;
2493 * If we inherit a type parameter from an outer class, set its owner to that class.
2496 set_generic_param_owner (MonoGenericContainer *container, MonoClass *klass, int pos)
2498 MonoGenericContainer *gc;
2501 if (klass->nested_in)
2502 pos = set_generic_param_owner (container, klass->nested_in, pos);
2504 if (!klass->generic_container)
2507 gc = klass->generic_container;
2508 for (i = pos; i < gc->type_argc; i++)
2509 container->type_params [i].owner = gc;
2511 return pos + gc->type_argc;
2514 static MonoGenericInst *
2515 get_shared_inst (MonoGenericContainer *container)
2517 MonoGenericInst *nginst;
2520 nginst = g_new0 (MonoGenericInst, 1);
2521 nginst->type_argc = container->type_argc;
2522 nginst->type_argv = g_new0 (MonoType *, nginst->type_argc);
2523 nginst->is_reference = 1;
2524 nginst->is_open = 1;
2526 for (i = 0; i < nginst->type_argc; i++) {
2527 MonoType *t = g_new0 (MonoType, 1);
2529 t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2530 t->data.generic_param = &container->type_params [i];
2532 nginst->type_argv [i] = t;
2535 return mono_metadata_lookup_generic_inst (nginst);
2539 * In preparation for implementing shared code.
2542 mono_get_shared_generic_class (MonoGenericContainer *container, gboolean is_dynamic)
2544 MonoInflatedGenericClass *igclass;
2545 MonoGenericClass *gclass;
2548 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
2549 igclass = &dgclass->generic_class;
2550 gclass = &igclass->generic_class;
2551 gclass->is_inflated = 1;
2552 gclass->is_dynamic = 1;
2554 igclass = g_new0 (MonoInflatedGenericClass, 1);
2555 gclass = &igclass->generic_class;
2556 gclass->is_inflated = 1;
2559 gclass->context = &container->context;
2560 gclass->container_class = container->klass;
2561 gclass->inst = get_shared_inst (container);
2564 MonoGenericClass *cached = mono_metadata_lookup_generic_class (gclass);
2572 igclass->klass = container->klass;
2578 * @image: context where the image is created
2579 * @type_token: typedef token
2582 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
2584 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
2585 MonoClass *class, *parent = NULL;
2586 guint32 cols [MONO_TYPEDEF_SIZE];
2587 guint32 cols_next [MONO_TYPEDEF_SIZE];
2588 guint tidx = mono_metadata_token_index (type_token);
2589 MonoGenericContext *context = NULL;
2590 const char *name, *nspace;
2592 MonoClass **interfaces;
2593 guint32 field_last, method_last;
2595 mono_loader_lock ();
2597 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
2598 mono_loader_unlock ();
2602 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
2604 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
2606 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2607 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2609 class = g_malloc0 (sizeof (MonoClass));
2612 class->name_space = nspace;
2614 class->image = image;
2615 class->type_token = type_token;
2616 class->flags = cols [MONO_TYPEDEF_FLAGS];
2618 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
2621 * Check whether we're a generic type definition.
2623 class->generic_container = mono_metadata_load_generic_params (image, class->type_token, NULL);
2624 if (class->generic_container) {
2625 class->generic_container->klass = class;
2626 context = &class->generic_container->context;
2628 context->gclass = mono_get_shared_generic_class (context->container, FALSE);
2631 if (cols [MONO_TYPEDEF_EXTENDS]) {
2632 parent = mono_class_get_full (
2633 image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]), context);
2636 mono_class_setup_parent (class, parent);
2638 mono_class_setup_mono_type (class);
2640 if (!class->enumtype) {
2641 mono_metadata_interfaces_from_typedef_full (image, type_token, &interfaces, &icount, context);
2643 class->interfaces = interfaces;
2644 class->interface_count = icount;
2647 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
2649 /* fixme: maybe we must set this on windows
2650 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
2654 class->cast_class = class->element_class = class;
2656 /*g_print ("Load class %s\n", name);*/
2659 * Compute the field and method lists
2661 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
2662 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
2664 if (tt->rows > tidx){
2665 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
2666 field_last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
2667 method_last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
2669 field_last = image->tables [MONO_TABLE_FIELD].rows;
2670 method_last = image->tables [MONO_TABLE_METHOD].rows;
2673 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
2674 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
2675 class->field.count = field_last - class->field.first;
2677 class->field.count = 0;
2679 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
2680 class->method.count = method_last - class->method.first;
2682 class->method.count = 0;
2684 /* reserve space to store vector pointer in arrays */
2685 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
2686 class->instance_size += 2 * sizeof (gpointer);
2687 g_assert (class->field.count == 0);
2690 if (class->enumtype) {
2691 class->enum_basetype = mono_class_find_enum_basetype (class);
2692 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
2696 * If we're a generic type definition, load the constraints.
2697 * We must do this after the class has been constructed to make certain recursive scenarios
2700 if (class->generic_container)
2701 mono_metadata_load_generic_param_constraints (
2702 image, type_token, class->generic_container);
2704 if ((type_token = mono_metadata_nested_in_typedef (image, type_token)))
2705 class->nested_in = mono_class_create_from_typedef (image, type_token);
2708 * If we inherit any type parameters from our containing class, we need to modify
2711 if (class->nested_in && class->generic_container)
2712 set_generic_param_owner (class->generic_container, class->nested_in, 0);
2714 mono_loader_unlock ();
2720 * Create the `MonoClass' for an instantiation of a generic type.
2721 * We only do this if we actually need it.
2724 mono_class_create_generic (MonoInflatedGenericClass *gclass)
2726 MonoClass *klass, *gklass;
2729 if (gclass->is_initialized)
2731 gclass->is_initialized = TRUE;
2734 gclass->klass = g_malloc0 (sizeof (MonoClass));
2735 klass = gclass->klass;
2737 gklass = gclass->generic_class.container_class;
2739 klass->nested_in = gklass->nested_in;
2741 klass->name = gklass->name;
2742 klass->name_space = gklass->name_space;
2743 klass->image = gklass->image;
2744 klass->flags = gklass->flags;
2746 klass->generic_class = &gclass->generic_class;
2748 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
2749 klass->this_arg.data.generic_class = klass->byval_arg.data.generic_class = &gclass->generic_class;
2750 klass->this_arg.byref = TRUE;
2752 klass->cast_class = klass->element_class = klass;
2754 if (gclass->generic_class.is_dynamic) {
2755 klass->instance_size = gklass->instance_size;
2756 klass->class_size = gklass->class_size;
2757 klass->size_inited = 1;
2760 klass->valuetype = gklass->valuetype;
2762 mono_class_setup_supertypes (klass);
2765 klass->interface_count = gklass->interface_count;
2766 klass->interfaces = g_new0 (MonoClass *, klass->interface_count);
2767 for (i = 0; i < klass->interface_count; i++) {
2768 MonoType *it = &gklass->interfaces [i]->byval_arg;
2769 MonoType *inflated = mono_class_inflate_generic_type (
2770 it, gclass->generic_class.context);
2771 klass->interfaces [i] = mono_class_from_mono_type (inflated);
2774 i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, 1);
2777 guint32 cols [MONO_NESTED_CLASS_SIZE];
2778 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
2779 nclass = mono_class_create_from_typedef (klass->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
2780 klass->nested_classes = g_list_prepend (klass->nested_classes, nclass);
2782 i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, i + 1);
2785 if (gclass->generic_class.is_dynamic) {
2786 MonoDynamicGenericClass *dgclass = (MonoDynamicGenericClass *) gclass;
2788 if (dgclass->parent)
2789 klass->parent = mono_class_from_mono_type (dgclass->parent);
2790 } else if (gklass->parent) {
2791 MonoType *inflated = mono_class_inflate_generic_type (
2792 &gklass->parent->byval_arg, gclass->generic_class.context);
2794 klass->parent = mono_class_from_mono_type (inflated);
2798 mono_class_setup_parent (klass, klass->parent);
2802 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
2804 MonoClass *klass, **ptr;
2808 return param->pklass;
2810 klass = param->pklass = g_new0 (MonoClass, 1);
2812 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
2816 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
2817 klass->parent = param->constraints [0];
2819 } else if (param->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT)
2820 klass->parent = mono_class_from_name (mono_defaults.corlib, "System", "ValueType");
2822 klass->parent = mono_defaults.object_class;
2824 if (count - pos > 0) {
2825 klass->interface_count = count - pos;
2826 klass->interfaces = g_new0 (MonoClass *, count - pos);
2827 for (i = pos; i < count; i++)
2828 klass->interfaces [i - pos] = param->constraints [i];
2831 g_assert (param->name && param->owner);
2833 klass->name = param->name;
2834 klass->name_space = "";
2835 klass->image = image;
2836 klass->inited = TRUE;
2837 klass->cast_class = klass->element_class = klass;
2838 klass->enum_basetype = &klass->element_class->byval_arg;
2839 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2841 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2842 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2843 klass->this_arg.byref = TRUE;
2845 klass->generic_container = param->owner;
2847 mono_class_setup_supertypes (klass);
2853 my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
2858 return param->pklass;
2860 klass = g_new0 (MonoClass, 1);
2863 klass->name = param->name;
2865 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
2866 klass->name_space = "";
2867 klass->image = mono_defaults.corlib;
2868 klass->inited = TRUE;
2869 klass->cast_class = klass->element_class = klass;
2870 klass->enum_basetype = &klass->element_class->byval_arg;
2871 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2873 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2874 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2875 klass->this_arg.byref = TRUE;
2877 klass->generic_container = param->owner;
2879 mono_class_setup_supertypes (klass);
2885 mono_ptr_class_get (MonoType *type)
2888 MonoClass *el_class;
2889 static GHashTable *ptr_hash = NULL;
2891 mono_loader_lock ();
2894 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2895 el_class = mono_class_from_mono_type (type);
2896 if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
2897 mono_loader_unlock ();
2900 result = g_new0 (MonoClass, 1);
2902 result->parent = NULL; /* no parent for PTR types */
2903 result->name_space = el_class->name_space;
2904 result->name = g_strdup_printf ("%s*", el_class->name);
2905 result->image = el_class->image;
2906 result->inited = TRUE;
2907 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2908 /* Can pointers get boxed? */
2909 result->instance_size = sizeof (gpointer);
2910 result->cast_class = result->element_class = el_class;
2911 result->enum_basetype = &result->element_class->byval_arg;
2912 result->blittable = TRUE;
2914 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
2915 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
2916 result->this_arg.byref = TRUE;
2918 mono_class_setup_supertypes (result);
2920 g_hash_table_insert (ptr_hash, el_class, result);
2922 mono_loader_unlock ();
2928 mono_fnptr_class_get (MonoMethodSignature *sig)
2931 static GHashTable *ptr_hash = NULL;
2933 mono_loader_lock ();
2936 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2938 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
2939 mono_loader_unlock ();
2942 result = g_new0 (MonoClass, 1);
2944 result->parent = NULL; /* no parent for PTR types */
2945 result->name = "System";
2946 result->name_space = "MonoFNPtrFakeClass";
2947 result->image = NULL; /* need to fix... */
2948 result->inited = TRUE;
2949 result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
2950 /* Can pointers get boxed? */
2951 result->instance_size = sizeof (gpointer);
2952 result->cast_class = result->element_class = result;
2953 result->blittable = TRUE;
2955 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
2956 result->this_arg.data.method = result->byval_arg.data.method = sig;
2957 result->this_arg.byref = TRUE;
2958 result->enum_basetype = &result->element_class->byval_arg;
2959 result->blittable = TRUE;
2961 mono_class_setup_supertypes (result);
2963 g_hash_table_insert (ptr_hash, sig, result);
2965 mono_loader_unlock ();
2971 mono_class_from_mono_type (MonoType *type)
2973 switch (type->type) {
2974 case MONO_TYPE_OBJECT:
2975 return type->data.klass? type->data.klass: mono_defaults.object_class;
2976 case MONO_TYPE_VOID:
2977 return type->data.klass? type->data.klass: mono_defaults.void_class;
2978 case MONO_TYPE_BOOLEAN:
2979 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
2980 case MONO_TYPE_CHAR:
2981 return type->data.klass? type->data.klass: mono_defaults.char_class;
2983 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
2985 return type->data.klass? type->data.klass: mono_defaults.byte_class;
2987 return type->data.klass? type->data.klass: mono_defaults.int16_class;
2989 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
2991 return type->data.klass? type->data.klass: mono_defaults.int32_class;
2993 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
2995 return type->data.klass? type->data.klass: mono_defaults.int_class;
2997 return type->data.klass? type->data.klass: mono_defaults.uint_class;
2999 return type->data.klass? type->data.klass: mono_defaults.int64_class;
3001 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
3003 return type->data.klass? type->data.klass: mono_defaults.single_class;
3005 return type->data.klass? type->data.klass: mono_defaults.double_class;
3006 case MONO_TYPE_STRING:
3007 return type->data.klass? type->data.klass: mono_defaults.string_class;
3008 case MONO_TYPE_TYPEDBYREF:
3009 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
3010 case MONO_TYPE_ARRAY:
3011 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
3013 return mono_ptr_class_get (type->data.type);
3014 case MONO_TYPE_FNPTR:
3015 return mono_fnptr_class_get (type->data.method);
3016 case MONO_TYPE_SZARRAY:
3017 return mono_array_class_get (type->data.klass, 1);
3018 case MONO_TYPE_CLASS:
3019 case MONO_TYPE_VALUETYPE:
3020 return type->data.klass;
3021 case MONO_TYPE_GENERICINST: {
3022 MonoInflatedGenericClass *gclass;
3023 gclass = mono_get_inflated_generic_class (type->data.generic_class);
3024 g_assert (gclass->klass);
3025 return gclass->klass;
3028 return my_mono_class_from_generic_parameter (type->data.generic_param, FALSE);
3029 case MONO_TYPE_MVAR:
3030 return my_mono_class_from_generic_parameter (type->data.generic_param, TRUE);
3032 g_warning ("implement me 0x%02x\n", type->type);
3033 g_assert_not_reached ();
3040 * @image: context where the image is created
3041 * @type_spec: typespec token
3044 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec,
3045 MonoGenericContext *context)
3047 MonoType *type, *inflated;
3050 type = mono_type_create_from_typespec_full (image, context, type_spec);
3052 switch (type->type) {
3053 case MONO_TYPE_ARRAY:
3054 class = mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
3056 case MONO_TYPE_SZARRAY:
3057 class = mono_array_class_get (type->data.klass, 1);
3060 class = mono_ptr_class_get (type->data.type);
3062 case MONO_TYPE_GENERICINST: {
3063 MonoInflatedGenericClass *gclass;
3064 gclass = mono_get_inflated_generic_class (type->data.generic_class);
3065 g_assert (gclass->klass);
3066 class = gclass->klass;
3070 /* it seems any type can be stored in TypeSpec as well */
3071 class = mono_class_from_mono_type (type);
3075 if (!class || !context || (!context->gclass && !context->gmethod))
3078 inflated = mono_class_inflate_generic_type (&class->byval_arg, context);
3080 return mono_class_from_mono_type (inflated);
3084 * mono_bounded_array_class_get:
3085 * @element_class: element class
3086 * @rank: the dimension of the array class
3087 * @bounded: whenever the array has non-zero bounds
3089 * Returns: a class object describing the array with element type @element_type and
3093 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
3097 MonoClass *parent = NULL;
3098 GSList *list, *rootlist;
3101 gboolean corlib_type = FALSE;
3103 g_assert (rank <= 255);
3106 /* bounded only matters for one-dimensional arrays */
3109 image = eclass->image;
3111 mono_loader_lock ();
3113 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
3114 for (; list; list = list->next) {
3116 if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
3117 mono_loader_unlock ();
3123 /* for the building corlib use System.Array from it */
3124 if (image->assembly && image->assembly->dynamic && image->assembly_name && strcmp (image->assembly_name, "mscorlib") == 0) {
3125 parent = mono_class_from_name (image, "System", "Array");
3127 } else if (mono_defaults.generic_array_class) {
3128 MonoType *inflated, **args;
3130 args = g_new0 (MonoType *, 1);
3131 args [0] = &eclass->byval_arg;
3133 inflated = mono_class_bind_generic_parameters (
3134 &mono_defaults.generic_array_class->byval_arg, 1, args);
3135 parent = mono_class_from_mono_type (inflated);
3137 if (!parent->inited)
3138 mono_class_init (parent);
3140 parent = mono_defaults.array_class;
3141 if (!parent->inited)
3142 mono_class_init (parent);
3145 class = g_malloc0 (sizeof (MonoClass));
3147 class->image = image;
3148 class->name_space = eclass->name_space;
3149 nsize = strlen (eclass->name);
3150 name = g_malloc (nsize + 2 + rank);
3151 memcpy (name, eclass->name, nsize);
3154 memset (name + nsize + 1, ',', rank - 1);
3155 name [nsize + rank] = ']';
3156 name [nsize + rank + 1] = 0;
3158 class->type_token = 0;
3159 /* all arrays are marked serializable and sealed, bug #42779 */
3160 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
3161 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
3162 class->parent = parent;
3163 class->instance_size = mono_class_instance_size (class->parent);
3164 class->class_size = 0;
3165 mono_class_setup_supertypes (class);
3166 if (eclass->generic_class)
3167 mono_class_init (eclass);
3168 if (!eclass->size_inited)
3169 mono_class_setup_fields (eclass);
3170 class->has_references = MONO_TYPE_IS_REFERENCE (&eclass->byval_arg) || eclass->has_references? TRUE: FALSE;
3174 if (eclass->enumtype)
3175 class->cast_class = eclass->element_class;
3177 class->cast_class = eclass;
3179 class->element_class = eclass;
3181 if ((rank > 1) || bounded) {
3182 MonoArrayType *at = g_new0 (MonoArrayType, 1);
3183 class->byval_arg.type = MONO_TYPE_ARRAY;
3184 class->byval_arg.data.array = at;
3185 at->eklass = eclass;
3187 /* FIXME: complete.... */
3189 class->byval_arg.type = MONO_TYPE_SZARRAY;
3190 class->byval_arg.data.klass = eclass;
3192 class->this_arg = class->byval_arg;
3193 class->this_arg.byref = 1;
3198 class->generic_container = eclass->generic_container;
3200 list = g_slist_append (rootlist, class);
3201 g_hash_table_insert (image->array_cache, eclass, list);
3203 mono_loader_unlock ();
3209 * mono_array_class_get:
3210 * @element_class: element class
3211 * @rank: the dimension of the array class
3213 * Returns: a class object describing the array with element type @element_type and
3217 mono_array_class_get (MonoClass *eclass, guint32 rank)
3219 return mono_bounded_array_class_get (eclass, rank, FALSE);
3223 * mono_class_instance_size:
3226 * Returns: the size of an object instance
3229 mono_class_instance_size (MonoClass *klass)
3231 if (!klass->size_inited)
3232 mono_class_init (klass);
3234 return klass->instance_size;
3238 * mono_class_min_align:
3241 * Returns: minimm alignment requirements
3244 mono_class_min_align (MonoClass *klass)
3246 if (!klass->size_inited)
3247 mono_class_init (klass);
3249 return klass->min_align;
3253 * mono_class_value_size:
3256 * This function is used for value types, and return the
3257 * space and the alignment to store that kind of value object.
3259 * Returns: the size of a value of kind @klass
3262 mono_class_value_size (MonoClass *klass, guint32 *align)
3266 /* fixme: check disable, because we still have external revereces to
3267 * mscorlib and Dummy Objects
3269 /*g_assert (klass->valuetype);*/
3271 size = mono_class_instance_size (klass) - sizeof (MonoObject);
3274 *align = klass->min_align;
3280 * mono_class_data_size:
3283 * Returns: the size of the static class data
3286 mono_class_data_size (MonoClass *klass)
3289 mono_class_init (klass);
3291 return klass->class_size;
3295 * Auxiliary routine to mono_class_get_field
3297 * Takes a field index instead of a field token.
3299 static MonoClassField *
3300 mono_class_get_field_idx (MonoClass *class, int idx)
3302 mono_class_setup_fields_locking (class);
3305 if (class->field.count) {
3306 if ((idx >= class->field.first) && (idx < class->field.first + class->field.count)){
3307 return &class->fields [idx - class->field.first];
3310 class = class->parent;
3316 * mono_class_get_field:
3317 * @class: the class to lookup the field.
3318 * @field_token: the field token
3320 * Returns: A MonoClassField representing the type and offset of
3321 * the field, or a NULL value if the field does not belong to this
3325 mono_class_get_field (MonoClass *class, guint32 field_token)
3327 int idx = mono_metadata_token_index (field_token);
3329 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
3331 return mono_class_get_field_idx (class, idx - 1);
3335 * mono_class_get_field_from_name:
3336 * @klass: the class to lookup the field.
3337 * @name: the field name
3339 * Search the class @klass and it's parents for a field with the name @name.
3341 * Returns: the MonoClassField pointer of the named field or NULL
3344 mono_class_get_field_from_name (MonoClass *klass, const char *name)
3348 mono_class_setup_fields_locking (klass);
3350 for (i = 0; i < klass->field.count; ++i) {
3351 if (strcmp (name, klass->fields [i].name) == 0)
3352 return &klass->fields [i];
3354 klass = klass->parent;
3360 * mono_class_get_field_token:
3361 * @field: the field we need the token of
3363 * Get the token of a field. Note that the tokesn is only valid for the image
3364 * the field was loaded from. Don't use this function for fields in dynamic types.
3366 * Returns: the token representing the field in the image it was loaded from.
3369 mono_class_get_field_token (MonoClassField *field)
3371 MonoClass *klass = field->parent;
3374 mono_class_setup_fields_locking (klass);
3376 for (i = 0; i < klass->field.count; ++i) {
3377 if (&klass->fields [i] == field)
3378 return mono_metadata_make_token (MONO_TABLE_FIELD, klass->field.first + i + 1);
3380 klass = klass->parent;
3383 g_assert_not_reached ();
3388 mono_class_get_event_token (MonoEvent *event)
3390 MonoClass *klass = event->parent;
3394 for (i = 0; i < klass->event.count; ++i) {
3395 if (&klass->events [i] == event)
3396 return mono_metadata_make_token (MONO_TABLE_EVENT, klass->event.first + i + 1);
3398 klass = klass->parent;
3401 g_assert_not_reached ();
3406 mono_vtable_get_static_field_data (MonoVTable *vt)
3412 mono_class_get_property_from_name (MonoClass *klass, const char *name)
3416 gpointer iter = NULL;
3417 while ((p = mono_class_get_properties (klass, &iter))) {
3418 if (! strcmp (name, p->name))
3421 klass = klass->parent;
3427 mono_class_get_property_token (MonoProperty *prop)
3429 MonoClass *klass = prop->parent;
3433 gpointer iter = NULL;
3434 while ((p = mono_class_get_properties (klass, &iter))) {
3435 if (&klass->properties [i] == prop)
3436 return mono_metadata_make_token (MONO_TABLE_PROPERTY, klass->property.first + i + 1);
3440 klass = klass->parent;
3443 g_assert_not_reached ();
3448 mono_class_name_from_token (MonoImage *image, guint32 type_token)
3450 const char *name, *nspace;
3452 return g_strdup_printf ("DynamicType 0x%08x", type_token);
3454 switch (type_token & 0xff000000){
3455 case MONO_TOKEN_TYPE_DEF: {
3456 guint32 cols [MONO_TYPEDEF_SIZE];
3457 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
3458 guint tidx = mono_metadata_token_index (type_token);
3460 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
3461 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3462 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3463 if (strlen (nspace) == 0)
3464 return g_strdup_printf ("%s", name);
3466 return g_strdup_printf ("%s.%s", nspace, name);
3469 case MONO_TOKEN_TYPE_REF: {
3470 guint32 cols [MONO_TYPEREF_SIZE];
3471 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
3473 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
3474 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
3475 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
3476 if (strlen (nspace) == 0)
3477 return g_strdup_printf ("%s", name);
3479 return g_strdup_printf ("%s.%s", nspace, name);
3482 case MONO_TOKEN_TYPE_SPEC:
3483 return g_strdup_printf ("Typespec 0x%08x", type_token);
3485 g_assert_not_reached ();
3492 mono_assembly_name_from_token (MonoImage *image, guint32 type_token)
3495 return g_strdup_printf ("DynamicAssembly %s", image->name);
3497 switch (type_token & 0xff000000){
3498 case MONO_TOKEN_TYPE_DEF:
3499 return mono_stringify_assembly_name (&image->assembly->aname);
3501 case MONO_TOKEN_TYPE_REF: {
3502 MonoAssemblyName aname;
3503 guint32 cols [MONO_TYPEREF_SIZE];
3504 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
3507 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
3509 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
3510 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
3511 case MONO_RESOLTION_SCOPE_MODULE:
3513 return g_strdup ("");
3514 case MONO_RESOLTION_SCOPE_MODULEREF:
3516 return g_strdup ("");
3517 case MONO_RESOLTION_SCOPE_TYPEREF:
3519 return g_strdup ("");
3520 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
3521 mono_assembly_get_assemblyref (image, idx - 1, &aname);
3522 return mono_stringify_assembly_name (&aname);
3524 g_assert_not_reached ();
3528 case MONO_TOKEN_TYPE_SPEC:
3530 return g_strdup ("");
3532 g_assert_not_reached ();
3540 * @image: the image where the class resides
3541 * @type_token: the token for the class
3542 * @at: an optional pointer to return the array element type
3544 * Returns: the MonoClass that represents @type_token in @image
3547 _mono_class_get (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3549 MonoClass *class = NULL;
3552 return mono_lookup_dynamic_token (image, type_token);
3554 switch (type_token & 0xff000000){
3555 case MONO_TOKEN_TYPE_DEF:
3556 class = mono_class_create_from_typedef (image, type_token);
3558 case MONO_TOKEN_TYPE_REF:
3559 class = mono_class_from_typeref (image, type_token);
3561 case MONO_TOKEN_TYPE_SPEC:
3562 class = mono_class_create_from_typespec (image, type_token, context);
3565 g_warning ("unknown token type %x", type_token & 0xff000000);
3566 g_assert_not_reached ();
3570 char *name = mono_class_name_from_token (image, type_token);
3571 char *assembly = mono_assembly_name_from_token (image, type_token);
3572 mono_loader_set_error_type_load (name, assembly);
3579 mono_class_get (MonoImage *image, guint32 type_token)
3581 return _mono_class_get (image, type_token, NULL);
3585 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3587 MonoClass *class = _mono_class_get (image, type_token, context);
3590 if (!class || !context || (!context->gclass && !context->gmethod))
3593 switch (class->byval_arg.type) {
3594 case MONO_TYPE_GENERICINST:
3595 if (!class->generic_class->inst->is_open)
3599 case MONO_TYPE_MVAR:
3605 inflated = inflate_generic_type (&class->byval_arg, context);
3609 return mono_class_from_mono_type (inflated);
3618 find_nocase (gpointer key, gpointer value, gpointer user_data)
3620 char *name = (char*)key;
3621 FindUserData *data = (FindUserData*)user_data;
3623 if (!data->value && (g_strcasecmp (name, (char*)data->key) == 0))
3624 data->value = value;
3628 * mono_class_from_name_case:
3629 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3630 * @name_space: the type namespace
3631 * @name: the type short name.
3633 * Obtains a MonoClass with a given namespace and a given name which
3634 * is located in the given MonoImage. The namespace and name
3635 * lookups are case insensitive.
3637 * You can also pass @NULL to the image, and that will lookup for
3638 * a type with the given namespace and name in all of the loaded
3639 * assemblies: notice that since there might be a name clash in this
3640 * case, passing @NULL is not encouraged if you need a precise type.
3644 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
3646 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
3647 guint32 cols [MONO_TYPEDEF_SIZE];
3652 if (image->dynamic) {
3654 FindUserData user_data;
3656 mono_loader_lock ();
3658 user_data.key = name_space;
3659 user_data.value = NULL;
3660 g_hash_table_foreach (image->name_cache, find_nocase, &user_data);
3662 if (user_data.value) {
3663 GHashTable *nspace_table = (GHashTable*)user_data.value;
3665 user_data.key = name;
3666 user_data.value = NULL;
3668 g_hash_table_foreach (nspace_table, find_nocase, &user_data);
3670 if (user_data.value)
3671 token = GPOINTER_TO_UINT (user_data.value);
3674 mono_loader_unlock ();
3677 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | token);
3683 /* add a cache if needed */
3684 for (i = 1; i <= t->rows; ++i) {
3685 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
3686 /* nested types are accessed from the nesting name */
3687 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
3688 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
3690 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3691 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3692 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
3693 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
3699 return_nested_in (MonoClass *class, char *nested) {
3701 char *s = strchr (nested, '/');
3708 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
3710 if (strcmp (found->name, nested) == 0) {
3712 return return_nested_in (found, s);
3721 * mono_class_from_name:
3722 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3723 * @name_space: the type namespace
3724 * @name: the type short name.
3726 * Obtains a MonoClass with a given namespace and a given name which
3727 * is located in the given MonoImage.
3729 * You can also pass `NULL' to the image, and that will lookup for
3730 * a type with the given namespace and name in all of the loaded
3731 * assemblies: notice that since there might be a name clash in this
3732 * case, passing NULL is not encouraged if you need a precise type.
3736 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
3738 GHashTable *nspace_table;
3739 MonoImage *loaded_image;
3745 if ((nested = strchr (name, '/'))) {
3746 int pos = nested - name;
3747 int len = strlen (name);
3750 memcpy (buf, name, len + 1);
3752 nested = buf + pos + 1;
3756 mono_loader_lock ();
3758 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
3761 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
3763 mono_loader_unlock ();
3768 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
3769 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
3770 guint32 cols [MONO_EXP_TYPE_SIZE];
3773 idx = mono_metadata_token_index (token);
3775 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
3777 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
3778 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
3779 loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
3782 class = mono_class_from_name (loaded_image, name_space, name);
3784 return return_nested_in (class, nested);
3787 g_error ("not yet implemented");
3791 token = MONO_TOKEN_TYPE_DEF | token;
3793 class = mono_class_get (image, token);
3795 return return_nested_in (class, nested);
3800 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
3801 gboolean check_interfaces)
3804 g_assert (klassc->idepth > 0);
3805 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
3806 if ((klassc->interface_id <= klass->max_interface_id) &&
3807 (klass->interface_offsets [klassc->interface_id] >= 0))
3809 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
3812 for (i = 0; i < klass->interface_count; i ++) {
3813 MonoClass *ic = klass->interfaces [i];
3818 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
3823 * MS.NET thinks interfaces are a subclass of Object, so we think it as
3826 if (klassc == mono_defaults.object_class)
3829 if (klass->generic_class && klass->generic_class->is_dynamic) {
3830 MonoDynamicGenericClass *dgclass = (MonoDynamicGenericClass *) klass->generic_class;
3832 if (!dgclass->parent)
3835 if (mono_metadata_type_equal (dgclass->parent, &klassc->byval_arg))
3837 klass = mono_class_from_mono_type (dgclass->parent);
3845 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
3848 mono_class_init (klass);
3850 if (!oklass->inited)
3851 mono_class_init (oklass);
3853 if (klass->generic_class)
3854 klass = klass->generic_class->container_class;
3855 if (oklass->generic_class)
3856 oklass = oklass->generic_class->container_class;
3858 if (MONO_CLASS_IS_INTERFACE (klass)) {
3859 if ((oklass->byval_arg.type == MONO_TYPE_VAR) || (oklass->byval_arg.type == MONO_TYPE_MVAR))
3862 /* interface_offsets might not be set for dynamic classes */
3863 if (oklass->reflection_info && !oklass->interface_offsets)
3865 * oklass might be a generic type parameter but they have
3866 * interface_offsets set.
3868 return mono_reflection_call_is_assignable_to (oklass, klass);
3870 if ((klass->interface_id <= oklass->max_interface_id) &&
3871 (oklass->interface_offsets [klass->interface_id] != -1))
3875 MonoClass *eclass, *eoclass;
3877 if (oklass->rank != klass->rank)
3880 /* vectors vs. one dimensional arrays */
3881 if (oklass->byval_arg.type != klass->byval_arg.type)
3884 eclass = klass->cast_class;
3885 eoclass = oklass->cast_class;
3889 * a is b does not imply a[] is b[] when a is a valuetype, and
3890 * b is a reference type.
3893 if (eoclass->valuetype) {
3894 if ((eclass == mono_defaults.enum_class) ||
3895 (eclass == mono_defaults.enum_class->parent) ||
3896 (eclass == mono_defaults.object_class))
3900 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
3903 if (klass == mono_defaults.object_class)
3907 * Custom version of mono_class_has_parent (oklass, klass)
3909 if (oklass->idepth >= klass->idepth) {
3910 MonoClass *parent = oklass->supertypes [klass->idepth - 1];
3912 if (parent->generic_class)
3913 parent = parent->generic_class->container_class;
3915 return klass == parent;
3922 * mono_class_get_cctor:
3924 * Returns the static constructor of @klass if it exists, NULL otherwise.
3927 mono_class_get_cctor (MonoClass *klass)
3929 MonoCachedClassInfo cached_info;
3931 if (!klass->has_cctor)
3934 if (mono_class_get_cached_class_info (klass, &cached_info))
3935 return mono_get_method (klass->image, cached_info.cctor_token, klass);
3937 return mono_class_get_method_from_name_flags (klass, ".cctor", -1, METHOD_ATTRIBUTE_SPECIAL_NAME);
3941 * mono_class_get_finalizer:
3943 * Returns the finalizer method of @klass if it exists, NULL otherwise.
3946 mono_class_get_finalizer (MonoClass *klass)
3948 MonoCachedClassInfo cached_info;
3949 static int finalize_slot = -1;
3951 if (finalize_slot < 0) {
3953 MonoClass* obj_class = mono_get_object_class ();
3954 mono_class_setup_vtable (obj_class);
3955 for (i = 0; i < obj_class->vtable_size; ++i) {
3956 MonoMethod *cm = obj_class->vtable [i];
3958 if (!strcmp (mono_method_get_name (cm), "Finalize")) {
3965 if (!klass->has_finalize)
3968 if (mono_class_get_cached_class_info (klass, &cached_info))
3969 return mono_get_method (cached_info.finalize_image, cached_info.finalize_token, NULL);
3971 mono_class_setup_vtable (klass);
3972 return klass->vtable [finalize_slot];
3977 * mono_class_needs_cctor_run:
3979 * Determines whenever the class has a static constructor and whenever it
3980 * needs to be called when executing CALLER.
3983 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
3987 method = mono_class_get_cctor (klass);
3989 return (method == caller) ? FALSE : TRUE;
3995 * mono_class_array_element_size:
3998 * Returns: the number of bytes an element of type @klass
3999 * uses when stored into an array.
4002 mono_class_array_element_size (MonoClass *klass)
4004 MonoType *type = &klass->byval_arg;
4007 switch (type->type) {
4010 case MONO_TYPE_BOOLEAN:
4014 case MONO_TYPE_CHAR:
4023 case MONO_TYPE_CLASS:
4024 case MONO_TYPE_STRING:
4025 case MONO_TYPE_OBJECT:
4026 case MONO_TYPE_SZARRAY:
4027 case MONO_TYPE_ARRAY:
4029 case MONO_TYPE_MVAR:
4030 return sizeof (gpointer);
4035 case MONO_TYPE_VALUETYPE:
4036 if (type->data.klass->enumtype) {
4037 type = type->data.klass->enum_basetype;
4038 klass = klass->element_class;
4041 return mono_class_instance_size (klass) - sizeof (MonoObject);
4042 case MONO_TYPE_GENERICINST:
4043 type = &type->data.generic_class->container_class->byval_arg;
4046 g_error ("unknown type 0x%02x in mono_class_array_element_size", type->type);
4052 * mono_array_element_size:
4053 * @ac: pointer to a #MonoArrayClass
4055 * Returns: the size of single array element.
4058 mono_array_element_size (MonoClass *ac)
4060 return mono_class_array_element_size (ac->element_class);
4064 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
4065 MonoGenericContext *context)
4067 if (image->dynamic) {
4068 MonoClass *tmp_handle_class;
4069 gpointer obj = mono_lookup_dynamic_token_class (image, token, &tmp_handle_class);
4071 g_assert (tmp_handle_class);
4073 *handle_class = tmp_handle_class;
4075 if (tmp_handle_class == mono_defaults.typehandle_class)
4076 return &((MonoClass*)obj)->byval_arg;
4081 switch (token & 0xff000000) {
4082 case MONO_TOKEN_TYPE_DEF:
4083 case MONO_TOKEN_TYPE_REF: {
4086 *handle_class = mono_defaults.typehandle_class;
4087 class = mono_class_get_full (image, token, context);
4090 mono_class_init (class);
4091 /* We return a MonoType* as handle */
4092 return &class->byval_arg;
4094 case MONO_TOKEN_TYPE_SPEC: {
4097 *handle_class = mono_defaults.typehandle_class;
4098 class = mono_class_create_from_typespec (image, token, context);
4101 mono_class_init (class);
4102 return &class->byval_arg;
4104 case MONO_TOKEN_FIELD_DEF: {
4106 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
4108 *handle_class = mono_defaults.fieldhandle_class;
4109 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
4112 mono_class_init (class);
4113 return mono_class_get_field (class, token);
4115 case MONO_TOKEN_METHOD_DEF: {
4117 meth = mono_get_method_full (image, token, NULL, context);
4119 *handle_class = mono_defaults.methodhandle_class;
4122 case MONO_TOKEN_MEMBER_REF: {
4123 guint32 cols [MONO_MEMBERREF_SIZE];
4125 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
4126 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
4127 mono_metadata_decode_blob_size (sig, &sig);
4128 if (*sig == 0x6) { /* it's a field */
4130 MonoClassField *field;
4131 field = mono_field_from_token (image, token, &klass, context);
4133 *handle_class = mono_defaults.fieldhandle_class;
4137 meth = mono_get_method_full (image, token, NULL, context);
4139 *handle_class = mono_defaults.methodhandle_class;
4144 g_warning ("Unknown token 0x%08x in ldtoken", token);
4151 * This function might need to call runtime functions so it can't be part
4152 * of the metadata library.
4154 static MonoLookupDynamicToken lookup_dynamic = NULL;
4157 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
4159 lookup_dynamic = func;
4163 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
4165 MonoClass *handle_class;
4167 return lookup_dynamic (image, token, &handle_class);
4171 mono_lookup_dynamic_token_class (MonoImage *image, guint32 token, MonoClass **handle_class)
4173 return lookup_dynamic (image, token, handle_class);
4176 static MonoGetCachedClassInfo get_cached_class_info = NULL;
4179 mono_install_get_cached_class_info (MonoGetCachedClassInfo func)
4181 get_cached_class_info = func;
4185 mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
4187 if (!get_cached_class_info)
4190 return get_cached_class_info (klass, res);
4194 mono_class_get_image (MonoClass *klass)
4196 return klass->image;
4200 * mono_class_get_element_class:
4201 * @klass: the MonoClass to act on
4203 * Returns: the element class of an array or an enumeration.
4206 mono_class_get_element_class (MonoClass *klass)
4208 return klass->element_class;
4212 * mono_class_is_valuetype:
4213 * @klass: the MonoClass to act on
4215 * Returns: true if the MonoClass represents a ValueType.
4218 mono_class_is_valuetype (MonoClass *klass)
4220 return klass->valuetype;
4224 * mono_class_is_enum:
4225 * @klass: the MonoClass to act on
4227 * Returns: true if the MonoClass represents an enumeration.
4230 mono_class_is_enum (MonoClass *klass)
4232 return klass->enumtype;
4236 * mono_class_enum_basetype:
4237 * @klass: the MonoClass to act on
4239 * Returns: the underlying type representation for an enumeration.
4242 mono_class_enum_basetype (MonoClass *klass)
4244 return klass->enum_basetype;
4248 * mono_class_get_parent
4249 * @klass: the MonoClass to act on
4251 * Returns: the parent class for this class.
4254 mono_class_get_parent (MonoClass *klass)
4256 return klass->parent;
4260 * mono_class_get_nesting_type;
4261 * @klass: the MonoClass to act on
4263 * Returns: the container type where this type is nested or NULL if this type is not a nested type.
4266 mono_class_get_nesting_type (MonoClass *klass)
4268 return klass->nested_in;
4272 * mono_class_get_rank:
4273 * @klass: the MonoClass to act on
4275 * Returns: the rank for the array (the number of dimensions).
4278 mono_class_get_rank (MonoClass *klass)
4284 * mono_class_get_flags:
4285 * @klass: the MonoClass to act on
4287 * The type flags from the TypeDef table from the metadata.
4288 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
4291 * Returns: the flags from the TypeDef table.
4294 mono_class_get_flags (MonoClass *klass)
4296 return klass->flags;
4300 * mono_class_get_name
4301 * @klass: the MonoClass to act on
4303 * Returns: the name of the class.
4306 mono_class_get_name (MonoClass *klass)
4312 * mono_class_get_namespace:
4313 * @klass: the MonoClass to act on
4315 * Returns: the namespace of the class.
4318 mono_class_get_namespace (MonoClass *klass)
4320 return klass->name_space;
4324 * mono_class_get_type:
4325 * @klass: the MonoClass to act on
4327 * This method returns the internal Type representation for the class.
4329 * Returns: the MonoType from the class.
4332 mono_class_get_type (MonoClass *klass)
4334 return &klass->byval_arg;
4338 * mono_class_get_byref_type:
4339 * @klass: the MonoClass to act on
4344 mono_class_get_byref_type (MonoClass *klass)
4346 return &klass->this_arg;
4350 * mono_class_num_fields:
4351 * @klass: the MonoClass to act on
4353 * Returns: the number of static and instance fields in the class.
4356 mono_class_num_fields (MonoClass *klass)
4358 return klass->field.count;
4362 * mono_class_num_methods:
4363 * @klass: the MonoClass to act on
4365 * Returns: the number of methods in the class.
4368 mono_class_num_methods (MonoClass *klass)
4370 return klass->method.count;
4374 * mono_class_num_properties
4375 * @klass: the MonoClass to act on
4377 * Returns: the number of properties in the class.
4380 mono_class_num_properties (MonoClass *klass)
4382 mono_class_setup_properties (klass);
4384 return klass->property.count;
4388 * mono_class_num_events:
4389 * @klass: the MonoClass to act on
4391 * Returns: the number of events in the class.
4394 mono_class_num_events (MonoClass *klass)
4396 mono_class_setup_events (klass);
4398 return klass->event.count;
4402 * mono_class_get_fields:
4403 * @klass: the MonoClass to act on
4405 * This routine is an iterator routine for retrieving the fields in a class.
4407 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4408 * iterate over all of the elements. When no more values are
4409 * available, the return value is NULL.
4411 * Returns: a @MonoClassField* on each iteration, or NULL when no more fields are available.
4414 mono_class_get_fields (MonoClass* klass, gpointer *iter)
4416 MonoClassField* field;
4419 mono_class_setup_fields_locking (klass);
4421 /* start from the first */
4422 if (klass->field.count) {
4423 return *iter = &klass->fields [0];
4431 if (field < &klass->fields [klass->field.count]) {
4432 return *iter = field;
4438 * mono_class_get_methods
4439 * @klass: the MonoClass to act on
4441 * This routine is an iterator routine for retrieving the fields in a class.
4443 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4444 * iterate over all of the elements. When no more values are
4445 * available, the return value is NULL.
4447 * Returns: a MonoMethod on each iteration or NULL when no more methods are available.
4450 mono_class_get_methods (MonoClass* klass, gpointer *iter)
4452 MonoMethod** method;
4456 mono_class_init (klass);
4458 mono_class_setup_methods (klass);
4459 /* start from the first */
4460 if (klass->method.count) {
4461 *iter = &klass->methods [0];
4462 return klass->methods [0];
4470 if (method < &klass->methods [klass->method.count]) {
4478 * mono_class_get_properties:
4479 * @klass: the MonoClass to act on
4481 * This routine is an iterator routine for retrieving the properties in a class.
4483 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4484 * iterate over all of the elements. When no more values are
4485 * available, the return value is NULL.
4487 * Returns: a @MonoProperty* on each invocation, or NULL when no more are available.
4490 mono_class_get_properties (MonoClass* klass, gpointer *iter)
4492 MonoProperty* property;
4496 mono_class_init (klass);
4498 mono_class_setup_properties (klass);
4499 /* start from the first */
4500 if (klass->property.count) {
4501 return *iter = &klass->properties [0];
4509 if (property < &klass->properties [klass->property.count]) {
4510 return *iter = property;
4516 * mono_class_get_events:
4517 * @klass: the MonoClass to act on
4519 * This routine is an iterator routine for retrieving the properties in a class.
4521 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4522 * iterate over all of the elements. When no more values are
4523 * available, the return value is NULL.
4525 * Returns: a @MonoEvent* on each invocation, or NULL when no more are available.
4528 mono_class_get_events (MonoClass* klass, gpointer *iter)
4534 mono_class_init (klass);
4536 mono_class_setup_events (klass);
4537 /* start from the first */
4538 if (klass->event.count) {
4539 return *iter = &klass->events [0];
4547 if (event < &klass->events [klass->event.count]) {
4548 return *iter = event;
4554 * mono_class_get_interfaces
4555 * @klass: the MonoClass to act on
4557 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
4559 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4560 * iterate over all of the elements. When no more values are
4561 * available, the return value is NULL.
4563 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
4566 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
4572 mono_class_init (klass);
4574 /* start from the first */
4575 if (klass->interface_count) {
4576 *iter = &klass->interfaces [0];
4577 return klass->interfaces [0];
4585 if (iface < &klass->interfaces [klass->interface_count]) {
4593 * mono_class_get_nested_types
4594 * @klass: the MonoClass to act on
4596 * This routine is an iterator routine for retrieving the nested types of a class.
4598 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4599 * iterate over all of the elements. When no more values are
4600 * available, the return value is NULL.
4602 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
4605 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
4611 mono_class_init (klass);
4613 /* start from the first */
4614 if (klass->nested_classes) {
4615 *iter = klass->nested_classes;
4616 return klass->nested_classes->data;
4618 /* no nested types */
4632 * mono_field_get_name:
4633 * @field: the MonoClassField to act on
4635 * Returns: the name of the field.
4638 mono_field_get_name (MonoClassField *field)
4644 * mono_field_get_type:
4645 * @field: the MonoClassField to act on
4647 * Returns: MonoType of the field.
4650 mono_field_get_type (MonoClassField *field)
4656 * mono_field_get_type:
4657 * @field: the MonoClassField to act on
4659 * Returns: MonoClass where the field was defined.
4662 mono_field_get_parent (MonoClassField *field)
4664 return field->parent;
4668 * mono_field_get_flags;
4669 * @field: the MonoClassField to act on
4671 * The metadata flags for a field are encoded using the
4672 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4674 * Returns: the flags for the field.
4677 mono_field_get_flags (MonoClassField *field)
4679 return field->type->attrs;
4683 * mono_property_get_name:
4684 * @prop: the MonoProperty to act on
4686 * Returns: the name of the property
4689 mono_property_get_name (MonoProperty *prop)
4695 * mono_property_get_set_method
4696 * @prop: the MonoProperty to act on.
4698 * Returns: the setter method of the property (A MonoMethod)
4701 mono_property_get_set_method (MonoProperty *prop)
4707 * mono_property_get_get_method
4708 * @prop: the MonoProperty to act on.
4710 * Returns: the setter method of the property (A MonoMethod)
4713 mono_property_get_get_method (MonoProperty *prop)
4719 * mono_property_get_parent:
4720 * @prop: the MonoProperty to act on.
4722 * Returns: the MonoClass where the property was defined.
4725 mono_property_get_parent (MonoProperty *prop)
4727 return prop->parent;
4731 * mono_property_get_flags:
4732 * @prop: the MonoProperty to act on.
4734 * The metadata flags for a property are encoded using the
4735 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4737 * Returns: the flags for the property.
4740 mono_property_get_flags (MonoProperty *prop)
4746 * mono_event_get_name:
4747 * @event: the MonoEvent to act on
4749 * Returns: the name of the event.
4752 mono_event_get_name (MonoEvent *event)
4758 * mono_event_get_add_method:
4759 * @event: The MonoEvent to act on.
4761 * Returns: the @add' method for the event (a MonoMethod).
4764 mono_event_get_add_method (MonoEvent *event)
4770 * mono_event_get_remove_method:
4771 * @event: The MonoEvent to act on.
4773 * Returns: the @remove method for the event (a MonoMethod).
4776 mono_event_get_remove_method (MonoEvent *event)
4778 return event->remove;
4782 * mono_event_get_raise_method:
4783 * @event: The MonoEvent to act on.
4785 * Returns: the @raise method for the event (a MonoMethod).
4788 mono_event_get_raise_method (MonoEvent *event)
4790 return event->raise;
4794 * mono_event_get_parent:
4795 * @event: the MonoEvent to act on.
4797 * Returns: the MonoClass where the event is defined.
4800 mono_event_get_parent (MonoEvent *event)
4802 return event->parent;
4806 * mono_event_get_flags
4807 * @event: the MonoEvent to act on.
4809 * The metadata flags for an event are encoded using the
4810 * EVENT_* constants. See the tabledefs.h file for details.
4812 * Returns: the flags for the event.
4815 mono_event_get_flags (MonoEvent *event)
4817 return event->attrs;
4821 * mono_class_get_method_from_name:
4822 * @klass: where to look for the method
4823 * @name_space: name of the method
4824 * @param_count: number of parameters. -1 for any number.
4826 * Obtains a MonoMethod with a given name and number of parameters.
4827 * It only works if there are no multiple signatures for any given method name.
4830 mono_class_get_method_from_name (MonoClass *klass, const char *name, int param_count)
4832 return mono_class_get_method_from_name_flags (klass, name, param_count, 0);
4836 * mono_class_get_method_from_name_flags:
4837 * @klass: where to look for the method
4838 * @name_space: name of the method
4839 * @param_count: number of parameters. -1 for any number.
4840 * @flags: flags which must be set in the method
4842 * Obtains a MonoMethod with a given name and number of parameters.
4843 * It only works if there are no multiple signatures for any given method name.
4846 mono_class_get_method_from_name_flags (MonoClass *klass, const char *name, int param_count, int flags)
4848 MonoMethod *res = NULL;
4851 mono_class_init (klass);
4853 if (klass->methods) {
4854 mono_class_setup_methods (klass);
4855 for (i = 0; i < klass->method.count; ++i) {
4856 MonoMethod *method = klass->methods [i];
4858 if (method->name[0] == name [0] &&
4859 !strcmp (name, method->name) &&
4860 (param_count == -1 || mono_method_signature (method)->param_count == param_count) &&
4861 ((method->flags & flags) == flags)) {
4868 /* Search directly in the metadata to avoid calling setup_methods () */
4869 for (i = 0; i < klass->method.count; ++i) {
4870 guint32 cols [MONO_METHOD_SIZE];
4873 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_METHOD], klass->method.first + i, cols, MONO_METHOD_SIZE);
4875 if (!strcmp (mono_metadata_string_heap (klass->image, cols [MONO_METHOD_NAME]), name)) {
4876 method = mono_get_method (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass);
4877 if ((param_count == -1) || mono_method_signature (method)->param_count == param_count) {
4889 * mono_class_set_failure:
4890 * @klass: class in which the failure was detected
4891 * @ex_type: the kind of exception/error to be thrown (later)
4892 * @ex_data: exception data (specific to each type of exception/error)
4894 * Keep a detected failure informations in the class for later processing.
4895 * Note that only the first failure is kept.
4898 mono_class_set_failure (MonoClass *klass, guint32 ex_type, void *ex_data)
4900 if (klass->exception_type)
4902 klass->exception_type = ex_type;
4903 klass->exception_data = ex_data;
4908 * mono_class_get_exception_for_failure:
4909 * @klass: class in which the failure was detected
4911 * Return a constructed MonoException than the caller can then throw
4912 * using mono_raise_exception - or NULL if no failure is present (or
4913 * doesn't result in an exception).
4916 mono_class_get_exception_for_failure (MonoClass *klass)
4918 switch (klass->exception_type) {
4919 case MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND: {
4920 MonoDomain *domain = mono_domain_get ();
4921 MonoSecurityManager* secman = mono_security_manager_get_methods ();
4922 MonoMethod *method = klass->exception_data;
4923 guint32 error = (method) ? MONO_METADATA_INHERITANCEDEMAND_METHOD : MONO_METADATA_INHERITANCEDEMAND_CLASS;
4924 MonoObject *exc = NULL;
4928 args [1] = mono_assembly_get_object (domain, mono_image_get_assembly (klass->image));
4929 args [2] = mono_type_get_object (domain, &klass->byval_arg);
4930 args [3] = (method) ? mono_method_get_object (domain, method, NULL) : NULL;
4932 mono_runtime_invoke (secman->inheritsecurityexception, NULL, args, &exc);
4933 return (MonoException*) exc;
4935 /* TODO - handle other class related failures */