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 this assert fails, it probably means that you haven't installed an assembly load/search hook */
110 g_assert (references == image->references);
111 g_assert (references [idx - 1]);
112 if (references [idx - 1] == (gpointer)-1)
115 return mono_class_from_name (references [idx - 1]->image, nspace, name);
118 static inline MonoType*
119 dup_type (MonoType* t, const MonoType *original)
121 MonoType *r = g_new0 (MonoType, 1);
123 r->attrs = original->attrs;
124 r->byref = original->byref;
125 if (t->type == MONO_TYPE_PTR)
126 t->data.type = dup_type (t->data.type, original->data.type);
127 else if (t->type == MONO_TYPE_ARRAY)
128 t->data.array = mono_dup_array_type (t->data.array);
129 else if (t->type == MONO_TYPE_FNPTR)
130 t->data.method = mono_metadata_signature_deep_dup (t->data.method);
131 mono_stats.generics_metadata_size += sizeof (MonoType);
135 /* Copy everything mono_metadata_free_array free. */
137 mono_dup_array_type (MonoArrayType *a)
139 a = g_memdup (a, sizeof (MonoArrayType));
141 a->sizes = g_memdup (a->sizes, a->numsizes * sizeof (int));
143 a->lobounds = g_memdup (a->lobounds, a->numlobounds * sizeof (int));
147 /* Copy everything mono_metadata_free_method_signature free. */
149 mono_metadata_signature_deep_dup (MonoMethodSignature *sig)
153 sig = mono_metadata_signature_dup (sig);
155 sig->ret = dup_type (sig->ret, sig->ret);
156 for (i = 0; i < sig->param_count; ++i)
157 sig->params [i] = dup_type (sig->params [i], sig->params [i]);
163 _mono_type_get_assembly_name (MonoClass *klass, GString *str)
165 MonoAssembly *ta = klass->image->assembly;
167 g_string_append_printf (
168 str, ", %s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s",
170 ta->aname.major, ta->aname.minor, ta->aname.build, ta->aname.revision,
171 ta->aname.culture && *ta->aname.culture? ta->aname.culture: "neutral",
172 ta->aname.public_key_token [0] ? (char *)ta->aname.public_key_token : "null");
176 mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed,
177 MonoTypeNameFormat format)
181 switch (type->type) {
182 case MONO_TYPE_ARRAY: {
183 int i, rank = type->data.array->rank;
184 MonoTypeNameFormat nested_format;
186 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
187 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
189 mono_type_get_name_recurse (
190 &type->data.array->eklass->byval_arg, str, FALSE, nested_format);
191 g_string_append_c (str, '[');
192 for (i = 1; i < rank; i++)
193 g_string_append_c (str, ',');
194 g_string_append_c (str, ']');
195 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
196 _mono_type_get_assembly_name (type->data.array->eklass, str);
199 case MONO_TYPE_SZARRAY: {
200 MonoTypeNameFormat nested_format;
202 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
203 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
205 mono_type_get_name_recurse (
206 &type->data.klass->byval_arg, str, FALSE, nested_format);
207 g_string_append (str, "[]");
208 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
209 _mono_type_get_assembly_name (type->data.klass, str);
212 case MONO_TYPE_PTR: {
213 MonoTypeNameFormat nested_format;
215 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
216 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
218 mono_type_get_name_recurse (
219 type->data.type, str, FALSE, nested_format);
220 g_string_append (str, "*");
221 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
222 _mono_type_get_assembly_name (type->data.klass, str);
227 g_assert (type->data.generic_param->name);
228 g_string_append (str, type->data.generic_param->name);
231 klass = mono_class_from_mono_type (type);
232 if (klass->nested_in) {
233 mono_type_get_name_recurse (
234 &klass->nested_in->byval_arg, str, TRUE, format);
235 if (format == MONO_TYPE_NAME_FORMAT_IL)
236 g_string_append_c (str, '.');
238 g_string_append_c (str, '+');
239 } else if (*klass->name_space) {
240 g_string_append (str, klass->name_space);
241 g_string_append_c (str, '.');
243 if (format == MONO_TYPE_NAME_FORMAT_IL) {
244 char *s = strchr (klass->name, '`');
245 int len = s ? s - klass->name : strlen (klass->name);
247 g_string_append_len (str, klass->name, len);
249 g_string_append (str, klass->name);
252 if (klass->generic_class) {
253 MonoGenericClass *gclass = klass->generic_class;
254 MonoTypeNameFormat nested_format;
257 nested_format = format == MONO_TYPE_NAME_FORMAT_FULL_NAME ?
258 MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED : format;
260 if (format == MONO_TYPE_NAME_FORMAT_IL)
261 g_string_append_c (str, '<');
263 g_string_append_c (str, '[');
264 for (i = 0; i < gclass->inst->type_argc; i++) {
265 MonoType *t = gclass->inst->type_argv [i];
268 g_string_append_c (str, ',');
269 if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
270 (t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
271 g_string_append_c (str, '[');
272 mono_type_get_name_recurse (
273 gclass->inst->type_argv [i], str, FALSE, nested_format);
274 if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
275 (t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
276 g_string_append_c (str, ']');
278 if (format == MONO_TYPE_NAME_FORMAT_IL)
279 g_string_append_c (str, '>');
281 g_string_append_c (str, ']');
282 } else if (klass->generic_container &&
283 (format != MONO_TYPE_NAME_FORMAT_FULL_NAME) &&
284 (format != MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)) {
287 if (format == MONO_TYPE_NAME_FORMAT_IL)
288 g_string_append_c (str, '<');
290 g_string_append_c (str, '[');
291 for (i = 0; i < klass->generic_container->type_argc; i++) {
293 g_string_append_c (str, ',');
294 g_string_append (str, klass->generic_container->type_params [i].name);
296 if (format == MONO_TYPE_NAME_FORMAT_IL)
297 g_string_append_c (str, '>');
299 g_string_append_c (str, ']');
301 if ((format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
302 (type->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
303 _mono_type_get_assembly_name (klass, str);
309 * mono_type_get_name:
311 * @format: the format for the return string.
314 * Returns: the string representation in a number of formats:
316 * if format is MONO_TYPE_NAME_FORMAT_REFLECTION, the return string is
317 * returned in the formatrequired by System.Reflection, this is the
318 * inverse of mono_reflection_parse_type ().
320 * if format is MONO_TYPE_NAME_FORMAT_IL, it returns a syntax that can
321 * be used by the IL assembler.
323 * if format is MONO_TYPE_NAME_FORMAT_FULL_NAME
325 * if format is MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
328 mono_type_get_name_full (MonoType *type, MonoTypeNameFormat format)
332 result = g_string_new ("");
334 mono_type_get_name_recurse (type, result, FALSE, format);
337 g_string_append_c (result, '&');
339 return g_string_free (result, FALSE);
343 * mono_type_get_full_name:
346 * Returns: the string representation for type as required by System.Reflection.
347 * The inverse of mono_reflection_parse_type ().
350 mono_type_get_full_name (MonoClass *class)
352 return mono_type_get_name_full (mono_class_get_type (class), MONO_TYPE_NAME_FORMAT_REFLECTION);
356 mono_type_get_name (MonoType *type)
358 return mono_type_get_name_full (type, MONO_TYPE_NAME_FORMAT_IL);
362 mono_type_get_underlying_type (MonoType *type)
364 switch (type->type) {
365 case MONO_TYPE_VALUETYPE:
366 if (type->data.klass->enumtype)
367 return type->data.klass->enum_basetype;
369 case MONO_TYPE_GENERICINST:
370 return mono_type_get_underlying_type (&type->data.generic_class->container_class->byval_arg);
379 mono_class_is_open_constructed_type (MonoType *t)
385 case MONO_TYPE_SZARRAY:
386 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
387 case MONO_TYPE_ARRAY:
388 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
390 return mono_class_is_open_constructed_type (t->data.type);
391 case MONO_TYPE_GENERICINST: {
392 MonoGenericClass *gclass = t->data.generic_class;
395 if (mono_class_is_open_constructed_type (&gclass->container_class->byval_arg))
397 for (i = 0; i < gclass->inst->type_argc; i++)
398 if (mono_class_is_open_constructed_type (gclass->inst->type_argv [i]))
407 static MonoGenericClass *
408 inflate_generic_class (MonoGenericClass *ogclass, MonoGenericContext *context)
410 MonoInflatedGenericClass *igclass;
411 MonoGenericClass *ngclass, *cached;
413 if (ogclass->is_dynamic) {
414 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
415 igclass = &dgclass->generic_class;
416 ngclass = &igclass->generic_class;
417 ngclass->is_inflated = 1;
418 ngclass->is_dynamic = 1;
420 igclass = g_new0 (MonoInflatedGenericClass, 1);
421 ngclass = &igclass->generic_class;
422 ngclass->is_inflated = 1;
427 ngclass->inst = mono_metadata_inflate_generic_inst (ogclass->inst, context);
429 igclass->klass = NULL;
431 ngclass->context = g_new0 (MonoGenericContext, 1);
432 ngclass->context->container = ngclass->container_class->generic_container;
433 ngclass->context->gclass = ngclass;
436 cached = mono_metadata_lookup_generic_class (ngclass);
437 mono_loader_unlock ();
439 g_free (ngclass->context);
448 inflate_generic_type (MonoType *type, MonoGenericContext *context)
450 switch (type->type) {
452 if (context->gmethod && context->gmethod->inst->type_argv)
454 context->gmethod->inst->type_argv [type->data.generic_param->num],
461 context->gclass->inst->type_argv [type->data.generic_param->num],
465 case MONO_TYPE_SZARRAY: {
466 MonoClass *eclass = type->data.klass;
467 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
470 nt = dup_type (type, type);
471 nt->data.klass = mono_class_from_mono_type (inflated);
474 case MONO_TYPE_ARRAY: {
475 MonoClass *eclass = type->data.array->eklass;
476 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
479 nt = dup_type (type, type);
480 nt->data.array = g_memdup (nt->data.array, sizeof (MonoArrayType));
481 nt->data.array->eklass = mono_class_from_mono_type (inflated);
484 case MONO_TYPE_GENERICINST: {
485 MonoGenericClass *ngclass = inflate_generic_class (type->data.generic_class, context);
486 MonoType *nt = dup_type (type, type);
487 nt->data.generic_class = ngclass;
490 case MONO_TYPE_CLASS:
491 case MONO_TYPE_VALUETYPE: {
492 MonoClass *klass = type->data.klass;
493 MonoGenericClass *gclass;
496 if (!klass->generic_container)
499 gclass = inflate_generic_class (klass->generic_container->context.gclass, context);
501 nt = dup_type (type, type);
502 nt->type = MONO_TYPE_GENERICINST;
503 nt->data.generic_class = gclass;
512 MonoInflatedGenericClass*
513 mono_get_inflated_generic_class (MonoGenericClass *gclass)
515 g_assert (gclass->is_inflated);
516 mono_class_create_generic ((MonoInflatedGenericClass *) gclass);
517 return (MonoInflatedGenericClass *) gclass;
521 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
523 MonoType *inflated = inflate_generic_type (type, context);
526 return dup_type (type, type);
528 mono_stats.inflated_type_count++;
533 mono_class_inflate_generic_signature (MonoImage *image, MonoMethodSignature *sig, MonoGenericContext *context)
535 MonoMethodSignature *res;
542 res = mono_metadata_signature_alloc (image, sig->param_count);
543 res->ret = mono_class_inflate_generic_type (sig->ret, context);
544 is_open = mono_class_is_open_constructed_type (res->ret);
545 for (i = 0; i < sig->param_count; ++i) {
546 res->params [i] = mono_class_inflate_generic_type (sig->params [i], context);
548 is_open = mono_class_is_open_constructed_type (res->params [i]);
550 res->hasthis = sig->hasthis;
551 res->explicit_this = sig->explicit_this;
552 res->call_convention = sig->call_convention;
553 res->pinvoke = sig->pinvoke;
554 res->generic_param_count = sig->generic_param_count;
555 res->sentinelpos = sig->sentinelpos;
556 res->has_type_parameters = is_open;
557 res->is_inflated = 1;
561 static MonoMethodHeader*
562 inflate_generic_header (MonoMethodHeader *header, MonoGenericContext *context)
564 MonoMethodHeader *res;
566 res = g_malloc0 (sizeof (MonoMethodHeader) + sizeof (gpointer) * header->num_locals);
567 res->code = header->code;
568 res->code_size = header->code_size;
569 res->max_stack = header->max_stack;
570 res->num_clauses = header->num_clauses;
571 res->init_locals = header->init_locals;
572 res->num_locals = header->num_locals;
573 res->clauses = header->clauses;
574 for (i = 0; i < header->num_locals; ++i)
575 res->locals [i] = mono_class_inflate_generic_type (header->locals [i], context);
579 static MonoGenericContext *
580 inflate_generic_context (MonoGenericContext *context, MonoGenericContext *inflate_with)
582 MonoGenericContext *res = g_new0 (MonoGenericContext, 1);
584 res->container = context->container;
587 res->gclass = inflate_generic_class (context->gclass, inflate_with);
589 if (context->gmethod) {
590 res->gmethod = g_new0 (MonoGenericMethod, 1);
591 res->gmethod->generic_class = res->gclass;
593 res->gmethod->container = context->gmethod->container;
594 res->gmethod->inst = mono_metadata_inflate_generic_inst (context->gmethod->inst, inflate_with);
596 res->gmethod = inflate_with->gmethod;
599 res->gmethod->container->parent = res->container;
600 res->container = res->gmethod->container;
607 * mono_class_inflate_generic_method:
609 * Instantiate method @method with the generic context @context.
610 * BEWARE: All non-trivial fields are invalid, including klass, signature, and header.
611 * Use mono_get_inflated_method (), mono_method_signature () and mono_method_get_header () to get the correct values.
614 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context)
616 MonoMethodInflated *result;
617 MonoGenericContainer *container = context ? context->container : NULL;
618 MonoMethodSignature *sig;
620 /* The `method' has already been instantiated before -> we need to create a new context. */
621 sig = mono_method_signature_full (method, container);
622 if (method->is_inflated || sig->is_inflated) {
623 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
625 context = inflate_generic_context (imethod->context, context);
626 method = imethod->declaring;
629 mono_stats.inflated_method_count++;
631 /* Just create a copy, but don't actually inflate the method for performance reasons. */
632 result = g_new0 (MonoMethodInflated, 1);
634 result->method.pinvoke = *(MonoMethodPInvoke*)method;
636 result->method.normal = *(MonoMethodNormal*)method;
637 result->method.method.is_inflated = 1;
638 result->context = context;
639 result->declaring = method;
641 if (result->method.method.klass->generic_class)
642 result->method.method.klass = result->method.method.klass->generic_class->container_class;
644 return (MonoMethod *) result;
648 * mono_get_inflated_method:
650 * For performance reasons, mono_class_inflate_generic_method() does not actually instantiate the
651 * method, it just "prepares" it for that. If you really need to fully instantiate the method
652 * (including its signature and header), call this method.
655 mono_get_inflated_method (MonoMethod *method)
657 MonoMethodInflated *imethod, *res;
658 MonoMethodHeader *mh;
662 if (!method->is_inflated)
665 imethod = (MonoMethodInflated *) method;
666 if (imethod->inflated)
667 return (MonoMethod *) imethod->inflated;
669 mono_stats.inflated_method_count_2++;
670 mono_stats.generics_metadata_size +=
671 sizeof (MonoMethodInflated) - sizeof (MonoMethodNormal);
673 res = g_new0 (MonoMethodInflated, 1);
675 res->inflated = imethod->inflated = res;
677 mh = mono_method_get_header (method);
679 res->method.normal.header = inflate_generic_header (mh, imethod->context);
681 dtype = mono_class_inflate_generic_type (&method->klass->byval_arg, imethod->context);
682 rklass = res->method.method.klass = mono_class_from_mono_type (dtype);
684 res->method.method.signature = mono_class_inflate_generic_signature (
685 method->klass->image, mono_method_signature (method), imethod->context);
687 return (MonoMethod *) res;
691 * mono_class_find_enum_basetype:
692 * @class: The enum class
694 * Determine the basetype of an enum by iterating through its fields. We do this
695 * in a separate function since it is cheaper than calling mono_class_setup_fields.
698 mono_class_find_enum_basetype (MonoClass *class)
700 MonoImage *m = class->image;
701 const int top = class->field.count;
702 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
705 g_assert (class->enumtype);
708 * Fetch all the field information.
710 for (i = 0; i < top; i++){
712 guint32 cols [MONO_FIELD_SIZE];
713 int idx = class->field.first + i;
714 MonoGenericContainer *container = NULL;
717 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
718 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
719 mono_metadata_decode_value (sig, &sig);
720 /* FIELD signature == 0x06 */
721 g_assert (*sig == 0x06);
722 if (class->generic_container)
723 container = class->generic_container;
724 else if (class->generic_class) {
725 MonoClass *gklass = class->generic_class->container_class;
727 container = gklass->generic_container;
728 g_assert (container);
730 ftype = mono_metadata_parse_type_full (
731 m, (MonoGenericContext *) container, MONO_PARSE_FIELD,
732 cols [MONO_FIELD_FLAGS], sig + 1, &sig);
733 if (class->generic_class) {
734 ftype = mono_class_inflate_generic_type (
735 ftype, class->generic_class->context);
736 ftype->attrs = cols [MONO_FIELD_FLAGS];
739 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC))
747 * mono_class_setup_fields:
748 * @class: The class to initialize
750 * Initializes the class->fields.
751 * Assumes the loader lock is held.
754 mono_class_setup_fields (MonoClass *class)
756 MonoImage *m = class->image;
757 const int top = class->field.count;
758 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
759 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
760 int i, blittable = TRUE, real_size = 0;
762 guint32 packing_size = 0;
763 gboolean explicit_size;
764 MonoClassField *field;
766 if (class->size_inited)
770 mono_class_init (class);
772 class->instance_size = 0;
773 class->class_size = 0;
776 if (!class->parent->size_inited)
777 mono_class_setup_fields (class->parent);
778 class->instance_size += class->parent->instance_size;
779 class->min_align = class->parent->min_align;
780 /* we use |= since it may have been set already */
781 class->has_references |= class->parent->has_references;
782 blittable = class->parent->blittable;
784 class->instance_size = sizeof (MonoObject);
785 class->min_align = 1;
788 /* Get the real size */
789 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
792 g_assert ((packing_size & 0xfffffff0) == 0);
793 class->packing_size = packing_size;
794 real_size += class->instance_size;
798 if (explicit_size && real_size) {
799 class->instance_size = MAX (real_size, class->instance_size);
801 class->size_inited = 1;
802 class->blittable = blittable;
806 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT)
809 /* Prevent infinite loops if the class references itself */
810 class->size_inited = 1;
812 class->fields = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoClassField) * top);
815 * Fetch all the field information.
817 for (i = 0; i < top; i++){
819 guint32 cols [MONO_FIELD_SIZE];
820 int idx = class->field.first + i;
821 MonoGenericContainer *container = NULL;
823 field = &class->fields [i];
824 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
825 /* The name is needed for fieldrefs */
826 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
827 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
828 mono_metadata_decode_value (sig, &sig);
829 /* FIELD signature == 0x06 */
830 g_assert (*sig == 0x06);
831 if (class->generic_container)
832 container = class->generic_container;
833 else if (class->generic_class) {
834 MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
835 MonoClass *gklass = class->generic_class->container_class;
837 container = gklass->generic_container;
838 g_assert (container);
840 ifield->generic_type = gklass->fields [i].type;
841 field->generic_info = ifield;
843 field->type = mono_metadata_parse_type_full (
844 m, (MonoGenericContext *) container, MONO_PARSE_FIELD,
845 cols [MONO_FIELD_FLAGS], sig + 1, &sig);
846 if (mono_field_is_deleted (field))
848 if (class->generic_class) {
849 field->type = mono_class_inflate_generic_type (
850 field->type, class->generic_class->context);
851 field->type->attrs = cols [MONO_FIELD_FLAGS];
854 field->parent = class;
856 /* Only do these checks if we still think this type is blittable */
857 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
858 if (field->type->byref || MONO_TYPE_IS_REFERENCE (field->type)) {
861 MonoClass *field_class = mono_class_from_mono_type (field->type);
862 if (!field_class || !field_class->blittable)
867 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
868 mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
869 if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
870 g_warning ("%s not initialized correctly (missing field layout info for %s)", class->name, field->name);
873 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
874 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
876 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
877 field->data = mono_image_rva_map (class->image, rva);
880 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC)) {
881 class->enum_basetype = field->type;
882 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
883 blittable = class->element_class->blittable;
886 /* The def_value of fields is compute lazily during vtable creation */
889 if (class == mono_defaults.string_class)
892 class->blittable = blittable;
894 if (class->enumtype && !class->enum_basetype) {
895 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
898 if (explicit_size && real_size) {
899 class->instance_size = MAX (real_size, class->instance_size);
902 mono_class_layout_fields (class);
906 * mono_class_setup_fields_locking:
907 * @class: The class to initialize
909 * Initializes the class->fields.
910 * Aquires the loader lock.
913 mono_class_setup_fields_locking (MonoClass *class)
916 mono_class_setup_fields (class);
917 mono_loader_unlock ();
921 * mono_class_has_references:
923 * Returns whenever @klass->has_references is set, initializing it if needed.
924 * Aquires the loader lock.
927 mono_class_has_references (MonoClass *klass)
929 if (klass->init_pending) {
930 /* Be conservative */
933 mono_class_init (klass);
935 return klass->has_references;
939 /* useful until we keep track of gc-references in corlib etc. */
940 #define IS_GC_REFERENCE(t) ((t)->type == MONO_TYPE_U || (t)->type == MONO_TYPE_I || (t)->type == MONO_TYPE_PTR)
943 * LOCKING: this is supposed to be called with the loader lock held.
946 mono_class_layout_fields (MonoClass *class)
949 const int top = class->field.count;
950 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
951 guint32 pass, passes, real_size;
952 gboolean gc_aware_layout = FALSE;
953 MonoClassField *field;
955 if (class->generic_container ||
956 (class->generic_class && class->generic_class->inst->is_open))
960 * Enable GC aware auto layout: in this mode, reference
961 * fields are grouped together inside objects, increasing collector
963 * Requires that all classes whose layout is known to native code be annotated
964 * with [StructLayout (LayoutKind.Sequential)]
965 * Value types have gc_aware_layout disabled by default, as per
966 * what the default is for other runtimes.
968 /* corlib is missing [StructLayout] directives in many places */
969 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
970 if (class->image != mono_defaults.corlib &&
971 class->byval_arg.type != MONO_TYPE_VALUETYPE)
972 gc_aware_layout = TRUE;
975 /* Compute klass->has_references */
977 * Process non-static fields first, since static fields might recursively
978 * refer to the class itself.
980 for (i = 0; i < top; i++) {
983 field = &class->fields [i];
985 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
986 ftype = mono_type_get_underlying_type (field->type);
987 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype)))))
988 class->has_references = TRUE;
992 for (i = 0; i < top; i++) {
995 field = &class->fields [i];
997 if (!field->type->attrs & FIELD_ATTRIBUTE_STATIC) {
998 ftype = mono_type_get_underlying_type (field->type);
999 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype)))))
1000 class->has_static_refs = TRUE;
1004 for (i = 0; i < top; i++) {
1007 field = &class->fields [i];
1009 ftype = mono_type_get_underlying_type (field->type);
1010 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype))))) {
1011 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1012 class->has_static_refs = TRUE;
1014 class->has_references = TRUE;
1019 * Compute field layout and total size (not considering static fields)
1023 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
1024 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
1026 if (gc_aware_layout)
1031 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
1035 real_size = class->parent->instance_size;
1037 real_size = sizeof (MonoObject);
1039 for (pass = 0; pass < passes; ++pass) {
1040 for (i = 0; i < top; i++){
1043 field = &class->fields [i];
1045 if (mono_field_is_deleted (field))
1047 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1050 if (gc_aware_layout) {
1052 * We process fields with reference type in the first pass,
1053 * and fields with non-reference type in the second pass.
1054 * We use IS_POINTER instead of IS_REFERENCE because in
1055 * some internal structures, we store GC_MALLOCed memory
1056 * in IntPtr fields...
1058 if (MONO_TYPE_IS_POINTER (field->type)) {
1067 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
1068 (strcmp (field->name, "$PRIVATE$") == 0)) {
1069 /* This field is a hack inserted by MCS to empty structures */
1073 size = mono_type_size (field->type, &align);
1075 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
1076 align = class->packing_size ? MIN (class->packing_size, align): align;
1077 class->min_align = MAX (align, class->min_align);
1078 field->offset = real_size;
1079 field->offset += align - 1;
1080 field->offset &= ~(align - 1);
1081 real_size = field->offset + size;
1084 class->instance_size = MAX (real_size, class->instance_size);
1086 if (class->instance_size & (class->min_align - 1)) {
1087 class->instance_size += class->min_align - 1;
1088 class->instance_size &= ~(class->min_align - 1);
1092 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
1094 for (i = 0; i < top; i++) {
1097 field = &class->fields [i];
1100 * There must be info about all the fields in a type if it
1101 * uses explicit layout.
1104 if (mono_field_is_deleted (field))
1106 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1109 size = mono_type_size (field->type, &align);
1112 * When we get here, field->offset is already set by the
1113 * loader (for either runtime fields or fields loaded from metadata).
1114 * The offset is from the start of the object: this works for both
1115 * classes and valuetypes.
1117 field->offset += sizeof (MonoObject);
1122 real_size = MAX (real_size, size + field->offset);
1124 class->instance_size = MAX (real_size, class->instance_size);
1128 class->size_inited = 1;
1131 * Compute static field layout and size
1133 for (i = 0; i < top; i++){
1136 field = &class->fields [i];
1138 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
1140 if (mono_field_is_deleted (field))
1143 size = mono_type_size (field->type, &align);
1144 field->offset = class->class_size;
1145 field->offset += align - 1;
1146 field->offset &= ~(align - 1);
1147 class->class_size = field->offset + size;
1152 * mono_class_setup_methods:
1154 * Initializes the 'methods' array in the klass.
1155 * Calling this method should be avoided if possible since it allocates a lot
1156 * of long-living MonoMethod structures.
1159 mono_class_setup_methods (MonoClass *class)
1162 MonoMethod **methods;
1167 mono_loader_lock ();
1169 if (class->methods) {
1170 mono_loader_unlock ();
1174 //printf ("INIT: %s.%s\n", class->name_space, class->name);
1176 if (!class->methods) {
1177 methods = mono_mempool_alloc (class->image->mempool, sizeof (MonoMethod*) * class->method.count);
1178 for (i = 0; i < class->method.count; ++i) {
1179 methods [i] = mono_get_method (class->image, MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
1183 if (MONO_CLASS_IS_INTERFACE (class))
1184 for (i = 0; i < class->method.count; ++i)
1185 methods [i]->slot = i;
1187 /* Leave this assignment as the last op in this function */
1188 class->methods = methods;
1190 mono_loader_unlock ();
1195 mono_class_setup_properties (MonoClass *class)
1197 guint startm, endm, i, j;
1198 guint32 cols [MONO_PROPERTY_SIZE];
1199 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
1200 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
1201 MonoProperty *properties;
1204 if (class->properties)
1207 mono_loader_lock ();
1209 if (class->properties) {
1210 mono_loader_unlock ();
1214 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &last);
1215 class->property.count = last - class->property.first;
1217 if (class->property.count)
1218 mono_class_setup_methods (class);
1220 properties = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoProperty) * class->property.count);
1221 for (i = class->property.first; i < last; ++i) {
1222 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
1223 properties [i - class->property.first].parent = class;
1224 properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
1225 properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
1227 startm = mono_metadata_methods_from_property (class->image, i, &endm);
1228 for (j = startm; j < endm; ++j) {
1229 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
1230 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1231 case METHOD_SEMANTIC_SETTER:
1232 properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1234 case METHOD_SEMANTIC_GETTER:
1235 properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1243 /* Leave this assignment as the last op in the function */
1244 class->properties = properties;
1246 mono_loader_unlock ();
1250 inflate_event (MonoClass *class, MonoEvent *event, MonoInflatedGenericClass *gclass)
1252 event->parent = class;
1255 MonoMethod *inflated = mono_class_inflate_generic_method (
1256 event->add, gclass->generic_class.context);
1258 event->add = mono_get_inflated_method (inflated);
1261 if (event->remove) {
1262 MonoMethod *inflated = mono_class_inflate_generic_method (
1263 event->remove, gclass->generic_class.context);
1265 event->remove = mono_get_inflated_method (inflated);
1269 MonoMethod *inflated = mono_class_inflate_generic_method (
1270 event->raise, gclass->generic_class.context);
1272 event->raise = mono_get_inflated_method (inflated);
1276 MonoMethod *inflated = mono_class_inflate_generic_method (
1277 event->other, gclass->generic_class.context);
1279 event->other = mono_get_inflated_method (inflated);
1284 mono_class_setup_events (MonoClass *class)
1286 guint startm, endm, i, j;
1287 guint32 cols [MONO_EVENT_SIZE];
1288 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
1289 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
1296 mono_loader_lock ();
1298 if (class->events) {
1299 mono_loader_unlock ();
1303 if (class->generic_class) {
1304 MonoInflatedGenericClass *gclass;
1307 gclass = mono_get_inflated_generic_class (class->generic_class);
1308 gklass = gclass->generic_class.container_class;
1310 mono_class_setup_events (gklass);
1311 class->event = gklass->event;
1313 class->events = g_new0 (MonoEvent, class->event.count);
1315 for (i = 0; i < class->event.count; i++) {
1316 class->events [i] = gklass->events [i];
1317 inflate_event (class, &class->events [i], gclass);
1320 mono_loader_unlock ();
1324 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &last);
1325 class->event.count = last - class->event.first;
1327 if (class->event.count)
1328 mono_class_setup_methods (class);
1330 events = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoEvent) * class->event.count);
1331 for (i = class->event.first; i < last; ++i) {
1332 MonoEvent *event = &events [i - class->event.first];
1334 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
1335 event->parent = class;
1336 event->attrs = cols [MONO_EVENT_FLAGS];
1337 event->name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
1339 startm = mono_metadata_methods_from_event (class->image, i, &endm);
1340 for (j = startm; j < endm; ++j) {
1341 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
1342 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1343 case METHOD_SEMANTIC_ADD_ON:
1344 event->add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1346 case METHOD_SEMANTIC_REMOVE_ON:
1347 event->remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1349 case METHOD_SEMANTIC_FIRE:
1350 event->raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1352 case METHOD_SEMANTIC_OTHER: {
1355 if (event->other == NULL) {
1356 event->other = g_new0 (MonoMethod*, 1);
1358 while (event->other [n])
1360 event->other = g_realloc (event->other, (n + 1) * sizeof (MonoMethod*));
1362 event->other [n] = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1370 /* Leave this assignment as the last op in the function */
1371 class->events = events;
1373 mono_loader_unlock ();
1377 mono_get_unique_iid (MonoClass *class)
1379 static GHashTable *iid_hash = NULL;
1380 static guint iid = 0;
1381 char *str, *type_name;
1385 g_assert (MONO_CLASS_IS_INTERFACE (class));
1387 mono_loader_lock ();
1390 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
1392 if (class->generic_class && !class->generic_class->inst->is_open) {
1393 generic_id = class->generic_class->inst->id;
1394 g_assert (generic_id != 0);
1398 type_name = mono_type_full_name (&class->byval_arg);
1399 str = g_strdup_printf ("%s|%s|%d", class->image->name, type_name, generic_id);
1402 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
1403 mono_loader_unlock ();
1404 if (mono_print_vtable)
1405 printf ("Interface: reusing id %d for %s\n", iid, str);
1407 return GPOINTER_TO_INT (value);
1409 if (mono_print_vtable)
1410 printf ("Interface: assigned id %d to %s\n", iid, str);
1411 g_hash_table_insert (iid_hash, str, GINT_TO_POINTER (iid));
1415 mono_loader_unlock ();
1417 g_assert (iid <= 65535);
1422 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
1427 for (i = 0; i < klass->interface_count; i++) {
1428 ic = klass->interfaces [i];
1431 *res = g_ptr_array_new ();
1432 g_ptr_array_add (*res, ic);
1433 mono_class_init (ic);
1435 collect_implemented_interfaces_aux (ic, res);
1440 mono_class_get_implemented_interfaces (MonoClass *klass)
1442 GPtrArray *res = NULL;
1444 collect_implemented_interfaces_aux (klass, &res);
1448 typedef struct _IOffsetInfo IOffsetInfo;
1449 struct _IOffsetInfo {
1453 int data [MONO_ZERO_LEN_ARRAY];
1456 static IOffsetInfo *cached_offset_info = NULL;
1457 static int next_offset_info_size = 128;
1460 cache_interface_offsets (int max_iid, int *data)
1462 IOffsetInfo *cached_info;
1465 for (cached_info = cached_offset_info; cached_info; cached_info = cached_info->next) {
1466 cached = cached_info->data;
1467 while (cached < cached_info->data + cached_info->size && *cached) {
1468 if (*cached == max_iid) {
1469 int i, matched = TRUE;
1471 for (i = 0; i < max_iid; ++i) {
1472 if (cached [i] != data [i]) {
1481 cached += *cached + 1;
1485 /* find a free slot */
1486 for (cached_info = cached_offset_info; cached_info; cached_info = cached_info->next) {
1487 if (cached_info->size - cached_info->next_free >= max_iid + 1) {
1488 cached = &cached_info->data [cached_info->next_free];
1489 *cached++ = max_iid;
1490 memcpy (cached, data, max_iid * sizeof (int));
1491 cached_info->next_free += max_iid + 1;
1495 /* allocate a new chunk */
1496 if (max_iid + 1 < next_offset_info_size) {
1497 new_size = next_offset_info_size;
1498 if (next_offset_info_size < 4096)
1499 next_offset_info_size += next_offset_info_size >> 2;
1501 new_size = max_iid + 1;
1503 cached_info = g_malloc0 (sizeof (IOffsetInfo) + sizeof (int) * new_size);
1504 cached_info->size = new_size;
1505 /*g_print ("allocated %d offset entries at %p (total: %d)\n", new_size, cached_info->data, offset_info_total_size);*/
1506 cached = &cached_info->data [0];
1507 *cached++ = max_iid;
1508 memcpy (cached, data, max_iid * sizeof (int));
1509 cached_info->next_free += max_iid + 1;
1510 cached_info->next = cached_offset_info;
1511 cached_offset_info = cached_info;
1516 setup_interface_offsets (MonoClass *class, int cur_slot)
1523 /* compute maximum number of slots and maximum interface id */
1525 for (k = class; k ; k = k->parent) {
1526 for (i = 0; i < k->interface_count; i++) {
1527 ic = k->interfaces [i];
1530 mono_class_init (ic);
1532 if (max_iid < ic->interface_id)
1533 max_iid = ic->interface_id;
1535 ifaces = mono_class_get_implemented_interfaces (k);
1537 for (i = 0; i < ifaces->len; ++i) {
1538 ic = g_ptr_array_index (ifaces, i);
1539 if (max_iid < ic->interface_id)
1540 max_iid = ic->interface_id;
1542 g_ptr_array_free (ifaces, TRUE);
1546 if (MONO_CLASS_IS_INTERFACE (class)) {
1547 if (max_iid < class->interface_id)
1548 max_iid = class->interface_id;
1550 class->max_interface_id = max_iid;
1551 /* compute vtable offset for interfaces */
1552 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
1554 for (i = 0; i <= max_iid; i++)
1555 class->interface_offsets [i] = -1;
1557 ifaces = mono_class_get_implemented_interfaces (class);
1559 for (i = 0; i < ifaces->len; ++i) {
1560 ic = g_ptr_array_index (ifaces, i);
1561 class->interface_offsets [ic->interface_id] = cur_slot;
1562 cur_slot += ic->method.count;
1564 g_ptr_array_free (ifaces, TRUE);
1567 for (k = class->parent; k ; k = k->parent) {
1568 ifaces = mono_class_get_implemented_interfaces (k);
1570 for (i = 0; i < ifaces->len; ++i) {
1571 ic = g_ptr_array_index (ifaces, i);
1573 if (class->interface_offsets [ic->interface_id] == -1) {
1574 int io = k->interface_offsets [ic->interface_id];
1578 class->interface_offsets [ic->interface_id] = io;
1581 g_ptr_array_free (ifaces, TRUE);
1585 if (MONO_CLASS_IS_INTERFACE (class))
1586 class->interface_offsets [class->interface_id] = cur_slot;
1588 cached_data = cache_interface_offsets (max_iid + 1, class->interface_offsets);
1589 g_free (class->interface_offsets);
1590 class->interface_offsets = cached_data;
1596 setup_generic_vtable (MonoClass *class)
1601 gklass = class->generic_class->container_class;
1603 mono_class_init (gklass);
1604 class->vtable_size = gklass->vtable_size;
1606 class->vtable = g_new0 (MonoMethod*, class->vtable_size);
1607 memcpy (class->vtable, gklass->vtable, sizeof (MonoMethod*) * class->vtable_size);
1609 for (i = 0; i < class->vtable_size; i++) {
1610 MonoMethod *m = class->vtable [i];
1615 m = mono_class_inflate_generic_method (m, class->generic_class->context);
1616 class->vtable [i] = m;
1619 class->max_interface_id = gklass->max_interface_id;
1620 class->interface_offsets = g_new0 (gint, gklass->max_interface_id + 1);
1621 memcpy (class->interface_offsets, gklass->interface_offsets,
1622 sizeof (gint) * (gklass->max_interface_id + 1));
1626 mono_class_setup_vtable (MonoClass *class)
1628 MonoMethod **overrides;
1629 MonoGenericContext *context;
1636 mono_class_setup_methods (class);
1638 if (MONO_CLASS_IS_INTERFACE (class))
1641 mono_loader_lock ();
1643 if (class->vtable) {
1644 mono_loader_unlock ();
1648 if (class->generic_class) {
1649 if (class->generic_class->inst->is_open) {
1650 setup_generic_vtable (class);
1651 mono_loader_unlock ();
1655 context = class->generic_class->context;
1656 type_token = class->generic_class->container_class->type_token;
1658 context = (MonoGenericContext *) class->generic_container;
1659 type_token = class->type_token;
1662 if (class->image->dynamic)
1663 mono_reflection_get_dynamic_overrides (class, &overrides, &onum);
1665 mono_class_get_overrides_full (class->image, type_token, &overrides, &onum, context);
1667 mono_class_setup_vtable_general (class, overrides, onum);
1670 mono_loader_unlock ();
1674 * LOCKING: this is supposed to be called with the loader lock held.
1677 mono_class_setup_vtable_general (MonoClass *class, MonoMethod **overrides, int onum)
1680 MonoMethod **vtable;
1681 int i, max_vtsize = 0, max_iid, cur_slot = 0;
1682 GPtrArray *ifaces, *pifaces = NULL;
1683 GHashTable *override_map = NULL;
1684 gboolean security_enabled = mono_is_security_manager_active ();
1689 if (class->generic_class && class->generic_class->inst->is_open) {
1690 setup_generic_vtable (class);
1694 ifaces = mono_class_get_implemented_interfaces (class);
1696 for (i = 0; i < ifaces->len; i++) {
1697 MonoClass *ic = g_ptr_array_index (ifaces, i);
1698 max_vtsize += ic->method.count;
1700 g_ptr_array_free (ifaces, TRUE);
1703 if (class->parent) {
1704 mono_class_init (class->parent);
1705 mono_class_setup_vtable (class->parent);
1706 max_vtsize += class->parent->vtable_size;
1707 cur_slot = class->parent->vtable_size;
1710 max_vtsize += class->method.count;
1712 vtable = alloca (sizeof (gpointer) * max_vtsize);
1713 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
1715 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
1717 cur_slot = setup_interface_offsets (class, cur_slot);
1718 max_iid = class->max_interface_id;
1720 if (class->parent && class->parent->vtable_size)
1721 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
1723 /* override interface methods */
1724 for (i = 0; i < onum; i++) {
1725 MonoMethod *decl = overrides [i*2];
1726 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
1728 mono_class_setup_methods (decl->klass);
1729 g_assert (decl->slot != -1);
1730 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
1731 vtable [dslot] = overrides [i*2 + 1];
1732 vtable [dslot]->slot = dslot;
1734 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1736 g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
1740 for (k = class; k ; k = k->parent) {
1743 ifaces = mono_class_get_implemented_interfaces (k);
1745 nifaces = ifaces->len;
1746 if (k->generic_class) {
1747 pifaces = mono_class_get_implemented_interfaces (
1748 k->generic_class->container_class);
1749 g_assert (pifaces && (pifaces->len == nifaces));
1752 for (i = 0; i < nifaces; i++) {
1753 MonoClass *pic = NULL;
1756 ic = g_ptr_array_index (ifaces, i);
1758 pic = g_ptr_array_index (pifaces, i);
1759 g_assert (ic->interface_id <= k->max_interface_id);
1760 io = k->interface_offsets [ic->interface_id];
1763 g_assert (io <= max_vtsize);
1766 mono_class_setup_methods (ic);
1767 for (l = 0; l < ic->method.count; l++) {
1768 MonoMethod *im = ic->methods [l];
1770 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1773 for (j = 0; j < class->method.count; ++j) {
1774 MonoMethod *cm = class->methods [j];
1775 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1776 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
1777 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
1779 if (!strcmp(cm->name, im->name) &&
1780 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1782 /* CAS - SecurityAction.InheritanceDemand on interface */
1783 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1784 mono_secman_inheritancedemand_method (cm, im);
1787 g_assert (io + l <= max_vtsize);
1788 vtable [io + l] = cm;
1793 /* already implemented */
1794 if (io >= k->vtable_size)
1798 for (l = 0; l < ic->method.count; l++) {
1799 MonoMethod *im = ic->methods [l];
1802 g_assert (io + l <= max_vtsize);
1804 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1807 for (k1 = class; k1; k1 = k1->parent) {
1808 for (j = 0; j < k1->method.count; ++j) {
1809 MonoMethod *cm = k1->methods [j];
1811 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1812 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
1815 if (!strcmp(cm->name, im->name) &&
1816 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1818 /* CAS - SecurityAction.InheritanceDemand on interface */
1819 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1820 mono_secman_inheritancedemand_method (cm, im);
1823 g_assert (io + l <= max_vtsize);
1824 vtable [io + l] = cm;
1829 g_assert (io + l <= max_vtsize);
1830 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1835 for (l = 0; l < ic->method.count; l++) {
1836 MonoMethod *im = ic->methods [l];
1837 char *qname, *fqname, *cname, *the_cname;
1840 if (vtable [io + l])
1844 the_cname = mono_type_get_name_full (&pic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
1848 cname = (char*)ic->name;
1851 qname = g_strconcat (cname, ".", im->name, NULL);
1852 if (ic->name_space && ic->name_space [0])
1853 fqname = g_strconcat (ic->name_space, ".", cname, ".", im->name, NULL);
1857 for (k1 = class; k1; k1 = k1->parent) {
1858 for (j = 0; j < k1->method.count; ++j) {
1859 MonoMethod *cm = k1->methods [j];
1861 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1864 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
1865 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1867 /* CAS - SecurityAction.InheritanceDemand on interface */
1868 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1869 mono_secman_inheritancedemand_method (cm, im);
1872 g_assert (io + l <= max_vtsize);
1873 vtable [io + l] = cm;
1884 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
1885 for (l = 0; l < ic->method.count; l++) {
1887 MonoMethod *im = ic->methods [l];
1888 if (im->flags & METHOD_ATTRIBUTE_STATIC)
1890 g_assert (io + l <= max_vtsize);
1893 * If one of our parents already implements this interface
1894 * we can inherit the implementation.
1896 if (!(vtable [io + l])) {
1897 MonoClass *parent = class->parent;
1899 if ((ic->interface_id <= parent->max_interface_id) &&
1900 (parent->interface_offsets [ic->interface_id] != -1) &&
1902 vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
1905 if (!(vtable [io + l])) {
1906 for (j = 0; j < onum; ++j) {
1907 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
1908 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
1910 msig = mono_signature_get_desc (mono_method_signature (im), FALSE);
1911 printf ("no implementation for interface method %s.%s::%s(%s) in class %s.%s\n",
1912 ic->name_space, ic->name, im->name, msig, class->name_space, class->name);
1914 for (j = 0; j < class->method.count; ++j) {
1915 MonoMethod *cm = class->methods [j];
1916 msig = mono_signature_get_desc (mono_method_signature (cm), TRUE);
1918 printf ("METHOD %s(%s)\n", cm->name, msig);
1921 g_assert_not_reached ();
1926 for (l = 0; l < ic->method.count; l++) {
1927 MonoMethod *im = vtable [io + l];
1930 g_assert (io + l <= max_vtsize);
1932 /* FIXME: why do we need this ? */
1934 /* g_assert_not_reached (); */
1940 g_ptr_array_free (ifaces, TRUE);
1943 for (i = 0; i < class->method.count; ++i) {
1946 cm = class->methods [i];
1949 * Non-virtual method have no place in the vtable.
1950 * This also catches static methods (since they are not virtual).
1952 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1956 * If the method is REUSE_SLOT, we must check in the
1957 * base class for a method to override.
1959 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
1961 for (k = class->parent; k ; k = k->parent) {
1963 for (j = 0; j < k->method.count; ++j) {
1964 MonoMethod *m1 = k->methods [j];
1965 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
1967 if (!strcmp(cm->name, m1->name) &&
1968 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (m1))) {
1970 /* CAS - SecurityAction.InheritanceDemand */
1971 if (security_enabled && (m1->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1972 mono_secman_inheritancedemand_method (cm, m1);
1975 slot = k->methods [j]->slot;
1976 g_assert (cm->slot < max_vtsize);
1978 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1979 g_hash_table_insert (override_map, m1, cm);
1991 cm->slot = cur_slot++;
1993 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
1994 vtable [cm->slot] = cm;
1997 /* override non interface methods */
1998 for (i = 0; i < onum; i++) {
1999 MonoMethod *decl = overrides [i*2];
2000 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
2001 g_assert (decl->slot != -1);
2002 vtable [decl->slot] = overrides [i*2 + 1];
2003 overrides [i * 2 + 1]->slot = decl->slot;
2005 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
2006 g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
2011 * If a method occupies more than one place in the vtable, and it is
2012 * overriden, then change the other occurances too.
2015 for (i = 0; i < max_vtsize; ++i)
2017 MonoMethod *cm = g_hash_table_lookup (override_map, vtable [i]);
2022 g_hash_table_destroy (override_map);
2025 if (class->generic_class) {
2026 MonoClass *gklass = class->generic_class->container_class;
2028 mono_class_init (gklass);
2030 class->vtable_size = MAX (gklass->vtable_size, cur_slot);
2032 class->vtable_size = cur_slot;
2034 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
2035 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
2037 if (mono_print_vtable) {
2040 for (i = 0; i <= max_iid; i++)
2041 if (class->interface_offsets [i] != -1)
2044 printf ("VTable %s (vtable entries = %d, interfaces = %d)\n", mono_type_full_name (&class->byval_arg),
2045 class->vtable_size, icount);
2047 for (i = 0; i < class->vtable_size; ++i) {
2052 printf (" slot assigned: %03d, slot index: %03d %s\n", i, cm->slot,
2053 mono_method_full_name (cm, TRUE));
2059 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
2060 class->name, max_iid);
2062 for (i = 0; i < class->interface_count; i++) {
2063 ic = class->interfaces [i];
2064 printf (" slot offset: %03d, method count: %03d, iid: %03d %s\n",
2065 class->interface_offsets [ic->interface_id],
2066 ic->method.count, ic->interface_id, mono_type_full_name (&ic->byval_arg));
2069 for (k = class->parent; k ; k = k->parent) {
2070 for (i = 0; i < k->interface_count; i++) {
2071 ic = k->interfaces [i];
2072 printf (" slot offset: %03d, method count: %03d, iid: %03d %s\n",
2073 class->interface_offsets [ic->interface_id],
2074 ic->method.count, ic->interface_id, mono_type_full_name (&ic->byval_arg));
2083 * @class: the class to initialize
2085 * compute the instance_size, class_size and other infos that cannot be
2086 * computed at mono_class_get() time. Also compute a generic vtable and
2087 * the method slot numbers. We use this infos later to create a domain
2091 mono_class_init (MonoClass *class)
2094 static MonoMethod *default_ghc = NULL;
2095 static MonoMethod *default_finalize = NULL;
2096 static int finalize_slot = -1;
2097 static int ghc_slot = -1;
2098 MonoCachedClassInfo cached_info;
2099 gboolean has_cached_info;
2106 /*g_print ("Init class %s\n", class->name);*/
2108 /* We do everything inside the lock to prevent races */
2109 mono_loader_lock ();
2111 if (class->inited) {
2112 mono_loader_unlock ();
2113 /* Somebody might have gotten in before us */
2117 if (class->init_pending) {
2118 mono_loader_unlock ();
2119 /* this indicates a cyclic dependency */
2120 g_error ("pending init %s.%s\n", class->name_space, class->name);
2123 class->init_pending = 1;
2125 /* CAS - SecurityAction.InheritanceDemand */
2126 if (mono_is_security_manager_active () && class->parent && (class->parent->flags & TYPE_ATTRIBUTE_HAS_SECURITY)) {
2127 mono_secman_inheritancedemand_class (class, class->parent);
2130 if (mono_debugger_start_class_init_func)
2131 mono_debugger_start_class_init_func (class);
2133 mono_stats.initialized_class_count++;
2135 if (class->generic_class && !class->generic_class->is_dynamic) {
2136 MonoInflatedGenericClass *gclass;
2139 gclass = mono_get_inflated_generic_class (class->generic_class);
2140 gklass = gclass->generic_class.container_class;
2142 mono_stats.generic_class_count++;
2144 class->method = gklass->method;
2145 class->field = gklass->field;
2147 mono_class_init (gklass);
2148 mono_class_setup_methods (gklass);
2149 mono_class_setup_properties (gklass);
2151 if (MONO_CLASS_IS_INTERFACE (class))
2152 class->interface_id = mono_get_unique_iid (class);
2154 g_assert (class->method.count == gklass->method.count);
2155 class->methods = g_new0 (MonoMethod *, class->method.count);
2157 for (i = 0; i < class->method.count; i++) {
2158 MonoMethod *inflated = mono_class_inflate_generic_method (
2159 gklass->methods [i], gclass->generic_class.context);
2161 class->methods [i] = mono_get_inflated_method (inflated);
2164 class->property = gklass->property;
2165 class->properties = g_new0 (MonoProperty, class->property.count);
2167 for (i = 0; i < class->property.count; i++) {
2168 MonoProperty *prop = &class->properties [i];
2170 *prop = gklass->properties [i];
2173 prop->get = mono_class_inflate_generic_method (
2174 prop->get, gclass->generic_class.context);
2176 prop->set = mono_class_inflate_generic_method (
2177 prop->set, gclass->generic_class.context);
2179 prop->parent = class;
2182 g_assert (class->interface_count == gklass->interface_count);
2185 if (class->parent && !class->parent->inited)
2186 mono_class_init (class->parent);
2188 has_cached_info = mono_class_get_cached_class_info (class, &cached_info);
2190 if (!class->generic_class && (!has_cached_info || (has_cached_info && cached_info.has_nested_classes))) {
2191 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
2194 guint32 cols [MONO_NESTED_CLASS_SIZE];
2195 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
2196 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
2197 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
2199 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
2204 * Computes the size used by the fields, and their locations
2206 if (has_cached_info) {
2207 class->instance_size = cached_info.instance_size;
2208 class->class_size = cached_info.class_size;
2209 class->packing_size = cached_info.packing_size;
2210 class->min_align = cached_info.min_align;
2211 class->blittable = cached_info.blittable;
2212 class->has_references = cached_info.has_references;
2213 class->has_static_refs = cached_info.has_static_refs;
2216 if (!class->size_inited)
2217 mono_class_setup_fields (class);
2219 /* initialize method pointers */
2222 MonoMethodSignature *sig;
2223 class->method.count = class->rank > 1? 2: 1;
2224 sig = mono_metadata_signature_alloc (class->image, class->rank);
2225 sig->ret = &mono_defaults.void_class->byval_arg;
2226 sig->pinvoke = TRUE;
2227 for (i = 0; i < class->rank; ++i)
2228 sig->params [i] = &mono_defaults.int32_class->byval_arg;
2230 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
2231 ctor->klass = class;
2232 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
2233 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
2234 ctor->signature = sig;
2235 ctor->name = ".ctor";
2237 class->methods = g_new (MonoMethod*, class->method.count);
2238 class->methods [0] = ctor;
2239 if (class->rank > 1) {
2240 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
2241 sig->ret = &mono_defaults.void_class->byval_arg;
2242 sig->pinvoke = TRUE;
2243 for (i = 0; i < class->rank * 2; ++i)
2244 sig->params [i] = &mono_defaults.int32_class->byval_arg;
2246 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
2247 ctor->klass = class;
2248 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
2249 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
2250 ctor->signature = sig;
2251 ctor->name = ".ctor";
2253 class->methods [1] = ctor;
2257 mono_class_setup_supertypes (class);
2260 if (class == mono_defaults.object_class) {
2261 mono_class_setup_vtable (class);
2262 for (i = 0; i < class->vtable_size; ++i) {
2263 MonoMethod *cm = class->vtable [i];
2265 if (!strcmp (cm->name, "GetHashCode")) {
2271 g_assert (ghc_slot > 0);
2273 default_ghc = class->vtable [ghc_slot];
2277 if (!default_finalize) {
2278 if (class == mono_defaults.object_class) {
2279 mono_class_setup_vtable (class);
2280 for (i = 0; i < class->vtable_size; ++i) {
2281 MonoMethod *cm = class->vtable [i];
2283 if (!strcmp (cm->name, "Finalize")) {
2289 g_assert (finalize_slot > 0);
2291 default_finalize = class->vtable [finalize_slot];
2296 * If possible, avoid the creation of the generic vtable by requesting
2297 * cached info from the runtime.
2299 if (has_cached_info) {
2300 guint32 cur_slot = 0;
2302 class->vtable_size = cached_info.vtable_size;
2303 class->has_finalize = cached_info.has_finalize;
2304 class->ghcimpl = cached_info.ghcimpl;
2305 class->has_cctor = cached_info.has_cctor;
2307 if (class->parent) {
2308 mono_class_init (class->parent);
2309 cur_slot = class->parent->vtable_size;
2312 setup_interface_offsets (class, cur_slot);
2315 mono_class_setup_vtable (class);
2318 if (class->parent) {
2319 MonoMethod *cmethod = class->vtable [ghc_slot];
2320 if (cmethod->is_inflated)
2321 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
2322 if (cmethod == default_ghc) {
2327 /* Object::Finalize should have empty implemenatation */
2328 class->has_finalize = 0;
2329 if (class->parent) {
2330 MonoMethod *cmethod = class->vtable [finalize_slot];
2331 if (cmethod->is_inflated)
2332 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
2333 if (cmethod != default_finalize) {
2334 class->has_finalize = 1;
2338 for (i = 0; i < class->method.count; ++i) {
2339 MonoMethod *method = class->methods [i];
2340 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2341 (strcmp (".cctor", method->name) == 0)) {
2342 class->has_cctor = 1;
2349 class->init_pending = 0;
2351 mono_loader_unlock ();
2353 if (MONO_CLASS_IS_INTERFACE (class)) {
2355 * class->interface_offsets is needed for the castclass/isinst code, so
2356 * we have to setup them for interfaces, too.
2358 setup_interface_offsets (class, 0);
2361 if (mono_debugger_class_init_func)
2362 mono_debugger_class_init_func (class);
2366 * LOCKING: this assumes the loader lock is held
2369 mono_class_setup_mono_type (MonoClass *class)
2371 const char *name = class->name;
2372 const char *nspace = class->name_space;
2374 class->this_arg.byref = 1;
2375 class->this_arg.data.klass = class;
2376 class->this_arg.type = MONO_TYPE_CLASS;
2377 class->byval_arg.data.klass = class;
2378 class->byval_arg.type = MONO_TYPE_CLASS;
2380 if (!strcmp (nspace, "System")) {
2381 if (!strcmp (name, "ValueType")) {
2383 * do not set the valuetype bit for System.ValueType.
2384 * class->valuetype = 1;
2386 class->blittable = TRUE;
2387 } else if (!strcmp (name, "Enum")) {
2389 * do not set the valuetype bit for System.Enum.
2390 * class->valuetype = 1;
2392 class->valuetype = 0;
2393 class->enumtype = 0;
2394 } else if (!strcmp (name, "Object")) {
2395 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
2396 } else if (!strcmp (name, "String")) {
2397 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
2398 } else if (!strcmp (name, "TypedReference")) {
2399 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
2403 if (class->valuetype) {
2404 int t = MONO_TYPE_VALUETYPE;
2405 if (!strcmp (nspace, "System")) {
2408 if (!strcmp (name, "Boolean")) {
2409 t = MONO_TYPE_BOOLEAN;
2410 } else if (!strcmp(name, "Byte")) {
2412 class->blittable = TRUE;
2416 if (!strcmp (name, "Char")) {
2421 if (!strcmp (name, "Double")) {
2423 class->blittable = TRUE;
2427 if (!strcmp (name, "Int32")) {
2429 class->blittable = TRUE;
2430 } else if (!strcmp(name, "Int16")) {
2432 class->blittable = TRUE;
2433 } else if (!strcmp(name, "Int64")) {
2435 class->blittable = TRUE;
2436 } else if (!strcmp(name, "IntPtr")) {
2438 class->blittable = TRUE;
2442 if (!strcmp (name, "Single")) {
2444 class->blittable = TRUE;
2445 } else if (!strcmp(name, "SByte")) {
2447 class->blittable = TRUE;
2451 if (!strcmp (name, "UInt32")) {
2453 class->blittable = TRUE;
2454 } else if (!strcmp(name, "UInt16")) {
2456 class->blittable = TRUE;
2457 } else if (!strcmp(name, "UInt64")) {
2459 class->blittable = TRUE;
2460 } else if (!strcmp(name, "UIntPtr")) {
2462 class->blittable = TRUE;
2466 if (!strcmp (name, "TypedReference")) {
2467 t = MONO_TYPE_TYPEDBYREF;
2468 class->blittable = TRUE;
2472 if (!strcmp (name, "Void")) {
2480 class->this_arg.type = class->byval_arg.type = t;
2483 if (MONO_CLASS_IS_INTERFACE (class))
2484 class->interface_id = mono_get_unique_iid (class);
2489 * LOCKING: this assumes the loader lock is held
2492 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
2494 gboolean system_namespace;
2496 system_namespace = !strcmp (class->name_space, "System");
2498 /* if root of the hierarchy */
2499 if (system_namespace && !strcmp (class->name, "Object")) {
2500 class->parent = NULL;
2501 class->instance_size = sizeof (MonoObject);
2504 if (!strcmp (class->name, "<Module>")) {
2505 class->parent = NULL;
2506 class->instance_size = 0;
2510 if (!MONO_CLASS_IS_INTERFACE (class)) {
2511 class->parent = parent;
2514 g_assert_not_reached (); /* FIXME */
2516 if (parent->generic_class && !parent->name) {
2518 * If the parent is a generic instance, we may get
2519 * called before it is fully initialized, especially
2520 * before it has its name.
2525 class->marshalbyref = parent->marshalbyref;
2526 class->contextbound = parent->contextbound;
2527 class->delegate = parent->delegate;
2529 if (system_namespace) {
2530 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
2531 class->marshalbyref = 1;
2533 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
2534 class->contextbound = 1;
2536 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
2537 class->delegate = 1;
2540 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
2541 (strcmp (class->parent->name_space, "System") == 0)))
2542 class->valuetype = 1;
2543 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
2544 class->valuetype = class->enumtype = 1;
2546 /*class->enumtype = class->parent->enumtype; */
2547 mono_class_setup_supertypes (class);
2549 class->parent = NULL;
2555 * LOCKING: this assumes the loader lock is held
2558 mono_class_setup_supertypes (MonoClass *class)
2562 if (class->supertypes)
2565 if (class->parent && !class->parent->supertypes)
2566 mono_class_setup_supertypes (class->parent);
2568 class->idepth = class->parent->idepth + 1;
2572 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
2573 class->supertypes = g_new0 (MonoClass *, ms);
2575 if (class->parent) {
2576 class->supertypes [class->idepth - 1] = class;
2577 memcpy (class->supertypes, class->parent->supertypes, class->parent->idepth * sizeof (gpointer));
2579 class->supertypes [0] = class;
2583 static MonoGenericInst *
2584 get_shared_inst (MonoGenericContainer *container)
2586 MonoGenericInst *nginst;
2589 nginst = g_new0 (MonoGenericInst, 1);
2590 nginst->type_argc = container->type_argc;
2591 nginst->type_argv = g_new0 (MonoType *, nginst->type_argc);
2592 nginst->is_reference = 1;
2593 nginst->is_open = 1;
2595 for (i = 0; i < nginst->type_argc; i++) {
2596 MonoType *t = g_new0 (MonoType, 1);
2598 t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2599 t->data.generic_param = &container->type_params [i];
2601 nginst->type_argv [i] = t;
2604 return mono_metadata_lookup_generic_inst (nginst);
2608 * In preparation for implementing shared code.
2611 mono_get_shared_generic_class (MonoGenericContainer *container, gboolean is_dynamic)
2613 MonoInflatedGenericClass *igclass;
2614 MonoGenericClass *gclass;
2617 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
2618 igclass = &dgclass->generic_class;
2619 gclass = &igclass->generic_class;
2620 gclass->is_inflated = 1;
2621 gclass->is_dynamic = 1;
2623 igclass = g_new0 (MonoInflatedGenericClass, 1);
2624 gclass = &igclass->generic_class;
2625 gclass->is_inflated = 1;
2628 gclass->context = &container->context;
2629 gclass->container_class = container->klass;
2630 gclass->inst = get_shared_inst (container);
2633 MonoGenericClass *cached = mono_metadata_lookup_generic_class (gclass);
2641 igclass->klass = container->klass;
2647 * @image: context where the image is created
2648 * @type_token: typedef token
2651 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
2653 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
2654 MonoClass *class, *parent = NULL;
2655 guint32 cols [MONO_TYPEDEF_SIZE];
2656 guint32 cols_next [MONO_TYPEDEF_SIZE];
2657 guint tidx = mono_metadata_token_index (type_token);
2658 MonoGenericContext *context = NULL;
2659 const char *name, *nspace;
2661 MonoClass **interfaces;
2662 guint32 field_last, method_last;
2663 guint32 nesting_tokeen;
2665 mono_loader_lock ();
2667 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
2668 mono_loader_unlock ();
2672 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
2674 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
2676 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2677 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2679 class = g_malloc0 (sizeof (MonoClass));
2682 class->name_space = nspace;
2684 class->image = image;
2685 class->type_token = type_token;
2686 class->flags = cols [MONO_TYPEDEF_FLAGS];
2688 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
2691 * Check whether we're a generic type definition.
2693 class->generic_container = mono_metadata_load_generic_params (image, class->type_token, NULL);
2694 if (class->generic_container) {
2695 class->generic_container->klass = class;
2696 context = &class->generic_container->context;
2698 context->gclass = mono_get_shared_generic_class (context->container, FALSE);
2701 if (cols [MONO_TYPEDEF_EXTENDS]) {
2702 parent = mono_class_get_full (
2703 image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]), context);
2706 /* do this early so it's available for interfaces in setup_mono_type () */
2707 if ((nesting_tokeen = mono_metadata_nested_in_typedef (image, type_token)))
2708 class->nested_in = mono_class_create_from_typedef (image, nesting_tokeen);
2710 mono_class_setup_parent (class, parent);
2712 mono_class_setup_mono_type (class);
2714 if (!class->enumtype) {
2715 mono_metadata_interfaces_from_typedef_full (image, type_token, &interfaces, &icount, context);
2717 class->interfaces = interfaces;
2718 class->interface_count = icount;
2721 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
2723 /* fixme: maybe we must set this on windows
2724 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
2728 class->cast_class = class->element_class = class;
2730 /*g_print ("Load class %s\n", name);*/
2733 * Compute the field and method lists
2735 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
2736 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
2738 if (tt->rows > tidx){
2739 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
2740 field_last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
2741 method_last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
2743 field_last = image->tables [MONO_TABLE_FIELD].rows;
2744 method_last = image->tables [MONO_TABLE_METHOD].rows;
2747 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
2748 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
2749 class->field.count = field_last - class->field.first;
2751 class->field.count = 0;
2753 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
2754 class->method.count = method_last - class->method.first;
2756 class->method.count = 0;
2758 /* reserve space to store vector pointer in arrays */
2759 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
2760 class->instance_size += 2 * sizeof (gpointer);
2761 g_assert (class->field.count == 0);
2764 if (class->enumtype) {
2765 class->enum_basetype = mono_class_find_enum_basetype (class);
2766 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
2770 * If we're a generic type definition, load the constraints.
2771 * We must do this after the class has been constructed to make certain recursive scenarios
2774 if (class->generic_container)
2775 mono_metadata_load_generic_param_constraints (
2776 image, type_token, class->generic_container);
2778 mono_loader_unlock ();
2783 /** is klass Nullable<T>? */
2785 mono_class_is_nullable (MonoClass *klass)
2787 return klass->generic_class != NULL &&
2788 klass->generic_class->container_class == mono_defaults.generic_nullable_class;
2792 /** if klass is T? return T */
2794 mono_class_get_nullable_param (MonoClass *klass)
2796 g_assert (mono_class_is_nullable (klass));
2797 return mono_class_from_mono_type (klass->generic_class->inst->type_argv [0]);
2801 * Create the `MonoClass' for an instantiation of a generic type.
2802 * We only do this if we actually need it.
2805 mono_class_create_generic (MonoInflatedGenericClass *gclass)
2807 MonoClass *klass, *gklass;
2810 if (gclass->is_initialized)
2812 gclass->is_initialized = TRUE;
2815 gclass->klass = g_malloc0 (sizeof (MonoClass));
2816 klass = gclass->klass;
2818 gklass = gclass->generic_class.container_class;
2820 klass->nested_in = gklass->nested_in;
2822 klass->name = gklass->name;
2823 klass->name_space = gklass->name_space;
2824 klass->image = gklass->image;
2825 klass->flags = gklass->flags;
2827 klass->generic_class = &gclass->generic_class;
2829 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
2830 klass->this_arg.data.generic_class = klass->byval_arg.data.generic_class = &gclass->generic_class;
2831 klass->this_arg.byref = TRUE;
2833 klass->cast_class = klass->element_class = klass;
2835 if (mono_class_is_nullable (klass))
2836 klass->cast_class = klass->element_class = mono_class_get_nullable_param (klass);
2838 if (gclass->generic_class.is_dynamic) {
2839 klass->instance_size = gklass->instance_size;
2840 klass->class_size = gklass->class_size;
2841 klass->size_inited = 1;
2844 klass->valuetype = gklass->valuetype;
2846 mono_class_setup_supertypes (klass);
2849 klass->interface_count = gklass->interface_count;
2850 klass->interfaces = g_new0 (MonoClass *, klass->interface_count);
2851 for (i = 0; i < klass->interface_count; i++) {
2852 MonoType *it = &gklass->interfaces [i]->byval_arg;
2853 MonoType *inflated = mono_class_inflate_generic_type (
2854 it, gclass->generic_class.context);
2855 klass->interfaces [i] = mono_class_from_mono_type (inflated);
2858 i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, 1);
2861 guint32 cols [MONO_NESTED_CLASS_SIZE];
2862 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
2863 nclass = mono_class_create_from_typedef (klass->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
2864 klass->nested_classes = g_list_prepend (klass->nested_classes, nclass);
2866 i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, i + 1);
2869 if (gclass->generic_class.is_dynamic) {
2870 MonoDynamicGenericClass *dgclass = (MonoDynamicGenericClass *) gclass;
2872 if (dgclass->parent)
2873 klass->parent = mono_class_from_mono_type (dgclass->parent);
2874 } else if (gklass->parent) {
2875 MonoType *inflated = mono_class_inflate_generic_type (
2876 &gklass->parent->byval_arg, gclass->generic_class.context);
2878 klass->parent = mono_class_from_mono_type (inflated);
2882 mono_class_setup_parent (klass, klass->parent);
2884 if (MONO_CLASS_IS_INTERFACE (klass))
2885 setup_interface_offsets (klass, 0);
2889 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
2891 MonoClass *klass, **ptr;
2895 return param->pklass;
2897 klass = param->pklass = g_new0 (MonoClass, 1);
2899 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
2903 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
2904 klass->parent = param->constraints [0];
2906 } else if (param->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT)
2907 klass->parent = mono_class_from_name (mono_defaults.corlib, "System", "ValueType");
2909 klass->parent = mono_defaults.object_class;
2911 if (count - pos > 0) {
2912 klass->interface_count = count - pos;
2913 klass->interfaces = g_new0 (MonoClass *, count - pos);
2914 for (i = pos; i < count; i++)
2915 klass->interfaces [i - pos] = param->constraints [i];
2918 g_assert (param->name && param->owner);
2920 klass->name = param->name;
2921 klass->name_space = "";
2922 klass->image = image;
2923 klass->inited = TRUE;
2924 klass->cast_class = klass->element_class = klass;
2925 klass->enum_basetype = &klass->element_class->byval_arg;
2926 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2928 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2929 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2930 klass->this_arg.byref = TRUE;
2932 mono_class_setup_supertypes (klass);
2938 my_mono_class_from_generic_parameter (MonoGenericParam *param, gboolean is_mvar)
2943 return param->pklass;
2945 klass = g_new0 (MonoClass, 1);
2948 klass->name = param->name;
2950 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
2951 klass->name_space = "";
2952 klass->image = mono_defaults.corlib;
2953 klass->inited = TRUE;
2954 klass->cast_class = klass->element_class = klass;
2955 klass->enum_basetype = &klass->element_class->byval_arg;
2956 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2958 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2959 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2960 klass->this_arg.byref = TRUE;
2962 mono_class_setup_supertypes (klass);
2968 mono_ptr_class_get (MonoType *type)
2971 MonoClass *el_class;
2972 static GHashTable *ptr_hash = NULL;
2974 mono_loader_lock ();
2977 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2978 el_class = mono_class_from_mono_type (type);
2979 if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
2980 mono_loader_unlock ();
2983 result = g_new0 (MonoClass, 1);
2985 result->parent = NULL; /* no parent for PTR types */
2986 result->name_space = el_class->name_space;
2987 result->name = g_strdup_printf ("%s*", el_class->name);
2988 result->image = el_class->image;
2989 result->inited = TRUE;
2990 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2991 /* Can pointers get boxed? */
2992 result->instance_size = sizeof (gpointer);
2993 result->cast_class = result->element_class = el_class;
2994 result->enum_basetype = &result->element_class->byval_arg;
2995 result->blittable = TRUE;
2997 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
2998 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
2999 result->this_arg.byref = TRUE;
3001 mono_class_setup_supertypes (result);
3003 g_hash_table_insert (ptr_hash, el_class, result);
3005 mono_loader_unlock ();
3011 mono_fnptr_class_get (MonoMethodSignature *sig)
3014 static GHashTable *ptr_hash = NULL;
3016 mono_loader_lock ();
3019 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
3021 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
3022 mono_loader_unlock ();
3025 result = g_new0 (MonoClass, 1);
3027 result->parent = NULL; /* no parent for PTR types */
3028 result->name = "System";
3029 result->name_space = "MonoFNPtrFakeClass";
3030 result->image = NULL; /* need to fix... */
3031 result->inited = TRUE;
3032 result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
3033 /* Can pointers get boxed? */
3034 result->instance_size = sizeof (gpointer);
3035 result->cast_class = result->element_class = result;
3036 result->blittable = TRUE;
3038 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
3039 result->this_arg.data.method = result->byval_arg.data.method = sig;
3040 result->this_arg.byref = TRUE;
3041 result->enum_basetype = &result->element_class->byval_arg;
3042 result->blittable = TRUE;
3044 mono_class_setup_supertypes (result);
3046 g_hash_table_insert (ptr_hash, sig, result);
3048 mono_loader_unlock ();
3054 mono_class_from_mono_type (MonoType *type)
3056 switch (type->type) {
3057 case MONO_TYPE_OBJECT:
3058 return type->data.klass? type->data.klass: mono_defaults.object_class;
3059 case MONO_TYPE_VOID:
3060 return type->data.klass? type->data.klass: mono_defaults.void_class;
3061 case MONO_TYPE_BOOLEAN:
3062 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
3063 case MONO_TYPE_CHAR:
3064 return type->data.klass? type->data.klass: mono_defaults.char_class;
3066 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
3068 return type->data.klass? type->data.klass: mono_defaults.byte_class;
3070 return type->data.klass? type->data.klass: mono_defaults.int16_class;
3072 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
3074 return type->data.klass? type->data.klass: mono_defaults.int32_class;
3076 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
3078 return type->data.klass? type->data.klass: mono_defaults.int_class;
3080 return type->data.klass? type->data.klass: mono_defaults.uint_class;
3082 return type->data.klass? type->data.klass: mono_defaults.int64_class;
3084 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
3086 return type->data.klass? type->data.klass: mono_defaults.single_class;
3088 return type->data.klass? type->data.klass: mono_defaults.double_class;
3089 case MONO_TYPE_STRING:
3090 return type->data.klass? type->data.klass: mono_defaults.string_class;
3091 case MONO_TYPE_TYPEDBYREF:
3092 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
3093 case MONO_TYPE_ARRAY:
3094 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
3096 return mono_ptr_class_get (type->data.type);
3097 case MONO_TYPE_FNPTR:
3098 return mono_fnptr_class_get (type->data.method);
3099 case MONO_TYPE_SZARRAY:
3100 return mono_array_class_get (type->data.klass, 1);
3101 case MONO_TYPE_CLASS:
3102 case MONO_TYPE_VALUETYPE:
3103 return type->data.klass;
3104 case MONO_TYPE_GENERICINST: {
3105 MonoInflatedGenericClass *gclass;
3106 gclass = mono_get_inflated_generic_class (type->data.generic_class);
3107 g_assert (gclass->klass);
3108 return gclass->klass;
3111 return my_mono_class_from_generic_parameter (type->data.generic_param, FALSE);
3112 case MONO_TYPE_MVAR:
3113 return my_mono_class_from_generic_parameter (type->data.generic_param, TRUE);
3115 g_warning ("implement me 0x%02x\n", type->type);
3116 g_assert_not_reached ();
3123 * @image: context where the image is created
3124 * @type_spec: typespec token
3127 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec,
3128 MonoGenericContext *context)
3130 MonoType *type, *inflated;
3133 type = mono_type_create_from_typespec_full (image, context, type_spec);
3135 switch (type->type) {
3136 case MONO_TYPE_ARRAY:
3137 class = mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
3139 case MONO_TYPE_SZARRAY:
3140 class = mono_array_class_get (type->data.klass, 1);
3143 class = mono_ptr_class_get (type->data.type);
3145 case MONO_TYPE_GENERICINST: {
3146 MonoInflatedGenericClass *gclass;
3147 gclass = mono_get_inflated_generic_class (type->data.generic_class);
3148 g_assert (gclass->klass);
3149 class = gclass->klass;
3153 /* it seems any type can be stored in TypeSpec as well */
3154 class = mono_class_from_mono_type (type);
3158 if (!class || !context || (!context->gclass && !context->gmethod))
3161 inflated = mono_class_inflate_generic_type (&class->byval_arg, context);
3163 return mono_class_from_mono_type (inflated);
3167 * mono_bounded_array_class_get:
3168 * @element_class: element class
3169 * @rank: the dimension of the array class
3170 * @bounded: whenever the array has non-zero bounds
3172 * Returns: a class object describing the array with element type @element_type and
3176 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
3180 MonoClass *parent = NULL;
3181 GSList *list, *rootlist;
3184 gboolean corlib_type = FALSE;
3186 g_assert (rank <= 255);
3189 /* bounded only matters for one-dimensional arrays */
3192 image = eclass->image;
3194 mono_loader_lock ();
3196 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
3197 for (; list; list = list->next) {
3199 if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
3200 mono_loader_unlock ();
3206 /* for the building corlib use System.Array from it */
3207 if (image->assembly && image->assembly->dynamic && image->assembly_name && strcmp (image->assembly_name, "mscorlib") == 0) {
3208 parent = mono_class_from_name (image, "System", "Array");
3210 } else if (mono_defaults.generic_array_class) {
3211 MonoType *inflated, **args;
3213 args = g_new0 (MonoType *, 1);
3214 args [0] = &eclass->byval_arg;
3216 inflated = mono_class_bind_generic_parameters (
3217 &mono_defaults.generic_array_class->byval_arg, 1, args);
3218 parent = mono_class_from_mono_type (inflated);
3220 if (!parent->inited)
3221 mono_class_init (parent);
3223 parent = mono_defaults.array_class;
3224 if (!parent->inited)
3225 mono_class_init (parent);
3228 class = g_malloc0 (sizeof (MonoClass));
3230 class->image = image;
3231 class->name_space = eclass->name_space;
3232 nsize = strlen (eclass->name);
3233 name = g_malloc (nsize + 2 + rank);
3234 memcpy (name, eclass->name, nsize);
3237 memset (name + nsize + 1, ',', rank - 1);
3238 name [nsize + rank] = ']';
3239 name [nsize + rank + 1] = 0;
3241 class->type_token = 0;
3242 /* all arrays are marked serializable and sealed, bug #42779 */
3243 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
3244 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
3245 class->parent = parent;
3246 class->instance_size = mono_class_instance_size (class->parent);
3247 class->class_size = 0;
3248 mono_class_setup_supertypes (class);
3249 if (eclass->generic_class)
3250 mono_class_init (eclass);
3251 if (!eclass->size_inited)
3252 mono_class_setup_fields (eclass);
3253 class->has_references = MONO_TYPE_IS_REFERENCE (&eclass->byval_arg) || eclass->has_references? TRUE: FALSE;
3257 if (eclass->enumtype)
3258 class->cast_class = eclass->element_class;
3260 class->cast_class = eclass;
3262 class->element_class = eclass;
3264 if ((rank > 1) || bounded) {
3265 MonoArrayType *at = g_new0 (MonoArrayType, 1);
3266 class->byval_arg.type = MONO_TYPE_ARRAY;
3267 class->byval_arg.data.array = at;
3268 at->eklass = eclass;
3270 /* FIXME: complete.... */
3272 class->byval_arg.type = MONO_TYPE_SZARRAY;
3273 class->byval_arg.data.klass = eclass;
3275 class->this_arg = class->byval_arg;
3276 class->this_arg.byref = 1;
3281 class->generic_container = eclass->generic_container;
3283 list = g_slist_append (rootlist, class);
3284 g_hash_table_insert (image->array_cache, eclass, list);
3286 mono_loader_unlock ();
3292 * mono_array_class_get:
3293 * @element_class: element class
3294 * @rank: the dimension of the array class
3296 * Returns: a class object describing the array with element type @element_type and
3300 mono_array_class_get (MonoClass *eclass, guint32 rank)
3302 return mono_bounded_array_class_get (eclass, rank, FALSE);
3306 * mono_class_instance_size:
3309 * Returns: the size of an object instance
3312 mono_class_instance_size (MonoClass *klass)
3314 if (!klass->size_inited)
3315 mono_class_init (klass);
3317 return klass->instance_size;
3321 * mono_class_min_align:
3324 * Returns: minimm alignment requirements
3327 mono_class_min_align (MonoClass *klass)
3329 if (!klass->size_inited)
3330 mono_class_init (klass);
3332 return klass->min_align;
3336 * mono_class_value_size:
3339 * This function is used for value types, and return the
3340 * space and the alignment to store that kind of value object.
3342 * Returns: the size of a value of kind @klass
3345 mono_class_value_size (MonoClass *klass, guint32 *align)
3349 /* fixme: check disable, because we still have external revereces to
3350 * mscorlib and Dummy Objects
3352 /*g_assert (klass->valuetype);*/
3354 size = mono_class_instance_size (klass) - sizeof (MonoObject);
3357 *align = klass->min_align;
3363 * mono_class_data_size:
3366 * Returns: the size of the static class data
3369 mono_class_data_size (MonoClass *klass)
3372 mono_class_init (klass);
3374 return klass->class_size;
3378 * Auxiliary routine to mono_class_get_field
3380 * Takes a field index instead of a field token.
3382 static MonoClassField *
3383 mono_class_get_field_idx (MonoClass *class, int idx)
3385 mono_class_setup_fields_locking (class);
3388 if (class->field.count) {
3389 if ((idx >= class->field.first) && (idx < class->field.first + class->field.count)){
3390 return &class->fields [idx - class->field.first];
3393 class = class->parent;
3399 * mono_class_get_field:
3400 * @class: the class to lookup the field.
3401 * @field_token: the field token
3403 * Returns: A MonoClassField representing the type and offset of
3404 * the field, or a NULL value if the field does not belong to this
3408 mono_class_get_field (MonoClass *class, guint32 field_token)
3410 int idx = mono_metadata_token_index (field_token);
3412 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
3414 return mono_class_get_field_idx (class, idx - 1);
3418 * mono_class_get_field_from_name:
3419 * @klass: the class to lookup the field.
3420 * @name: the field name
3422 * Search the class @klass and it's parents for a field with the name @name.
3424 * Returns: the MonoClassField pointer of the named field or NULL
3427 mono_class_get_field_from_name (MonoClass *klass, const char *name)
3431 mono_class_setup_fields_locking (klass);
3433 for (i = 0; i < klass->field.count; ++i) {
3434 if (strcmp (name, klass->fields [i].name) == 0)
3435 return &klass->fields [i];
3437 klass = klass->parent;
3443 * mono_class_get_field_token:
3444 * @field: the field we need the token of
3446 * Get the token of a field. Note that the tokesn is only valid for the image
3447 * the field was loaded from. Don't use this function for fields in dynamic types.
3449 * Returns: the token representing the field in the image it was loaded from.
3452 mono_class_get_field_token (MonoClassField *field)
3454 MonoClass *klass = field->parent;
3457 mono_class_setup_fields_locking (klass);
3459 for (i = 0; i < klass->field.count; ++i) {
3460 if (&klass->fields [i] == field)
3461 return mono_metadata_make_token (MONO_TABLE_FIELD, klass->field.first + i + 1);
3463 klass = klass->parent;
3466 g_assert_not_reached ();
3471 mono_class_get_event_token (MonoEvent *event)
3473 MonoClass *klass = event->parent;
3477 for (i = 0; i < klass->event.count; ++i) {
3478 if (&klass->events [i] == event)
3479 return mono_metadata_make_token (MONO_TABLE_EVENT, klass->event.first + i + 1);
3481 klass = klass->parent;
3484 g_assert_not_reached ();
3489 mono_vtable_get_static_field_data (MonoVTable *vt)
3495 mono_class_get_property_from_name (MonoClass *klass, const char *name)
3499 gpointer iter = NULL;
3500 while ((p = mono_class_get_properties (klass, &iter))) {
3501 if (! strcmp (name, p->name))
3504 klass = klass->parent;
3510 mono_class_get_property_token (MonoProperty *prop)
3512 MonoClass *klass = prop->parent;
3516 gpointer iter = NULL;
3517 while ((p = mono_class_get_properties (klass, &iter))) {
3518 if (&klass->properties [i] == prop)
3519 return mono_metadata_make_token (MONO_TABLE_PROPERTY, klass->property.first + i + 1);
3523 klass = klass->parent;
3526 g_assert_not_reached ();
3531 mono_class_name_from_token (MonoImage *image, guint32 type_token)
3533 const char *name, *nspace;
3535 return g_strdup_printf ("DynamicType 0x%08x", type_token);
3537 switch (type_token & 0xff000000){
3538 case MONO_TOKEN_TYPE_DEF: {
3539 guint32 cols [MONO_TYPEDEF_SIZE];
3540 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
3541 guint tidx = mono_metadata_token_index (type_token);
3543 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
3544 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3545 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3546 if (strlen (nspace) == 0)
3547 return g_strdup_printf ("%s", name);
3549 return g_strdup_printf ("%s.%s", nspace, name);
3552 case MONO_TOKEN_TYPE_REF: {
3553 guint32 cols [MONO_TYPEREF_SIZE];
3554 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
3556 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
3557 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
3558 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
3559 if (strlen (nspace) == 0)
3560 return g_strdup_printf ("%s", name);
3562 return g_strdup_printf ("%s.%s", nspace, name);
3565 case MONO_TOKEN_TYPE_SPEC:
3566 return g_strdup_printf ("Typespec 0x%08x", type_token);
3568 g_assert_not_reached ();
3575 mono_assembly_name_from_token (MonoImage *image, guint32 type_token)
3578 return g_strdup_printf ("DynamicAssembly %s", image->name);
3580 switch (type_token & 0xff000000){
3581 case MONO_TOKEN_TYPE_DEF:
3582 return mono_stringify_assembly_name (&image->assembly->aname);
3584 case MONO_TOKEN_TYPE_REF: {
3585 MonoAssemblyName aname;
3586 guint32 cols [MONO_TYPEREF_SIZE];
3587 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
3590 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
3592 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
3593 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
3594 case MONO_RESOLTION_SCOPE_MODULE:
3596 return g_strdup ("");
3597 case MONO_RESOLTION_SCOPE_MODULEREF:
3599 return g_strdup ("");
3600 case MONO_RESOLTION_SCOPE_TYPEREF:
3602 return g_strdup ("");
3603 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
3604 mono_assembly_get_assemblyref (image, idx - 1, &aname);
3605 return mono_stringify_assembly_name (&aname);
3607 g_assert_not_reached ();
3611 case MONO_TOKEN_TYPE_SPEC:
3613 return g_strdup ("");
3615 g_assert_not_reached ();
3623 * @image: the image where the class resides
3624 * @type_token: the token for the class
3625 * @at: an optional pointer to return the array element type
3627 * Returns: the MonoClass that represents @type_token in @image
3630 _mono_class_get (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3632 MonoClass *class = NULL;
3635 return mono_lookup_dynamic_token (image, type_token);
3637 switch (type_token & 0xff000000){
3638 case MONO_TOKEN_TYPE_DEF:
3639 class = mono_class_create_from_typedef (image, type_token);
3641 case MONO_TOKEN_TYPE_REF:
3642 class = mono_class_from_typeref (image, type_token);
3644 case MONO_TOKEN_TYPE_SPEC:
3645 class = mono_class_create_from_typespec (image, type_token, context);
3648 g_warning ("unknown token type %x", type_token & 0xff000000);
3649 g_assert_not_reached ();
3653 char *name = mono_class_name_from_token (image, type_token);
3654 char *assembly = mono_assembly_name_from_token (image, type_token);
3655 mono_loader_set_error_type_load (name, assembly);
3662 mono_class_get (MonoImage *image, guint32 type_token)
3664 return _mono_class_get (image, type_token, NULL);
3668 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3670 MonoClass *class = _mono_class_get (image, type_token, context);
3673 if (!class || !context || (!context->gclass && !context->gmethod))
3676 switch (class->byval_arg.type) {
3677 case MONO_TYPE_GENERICINST:
3678 if (!class->generic_class->inst->is_open)
3682 case MONO_TYPE_MVAR:
3688 inflated = inflate_generic_type (&class->byval_arg, context);
3692 return mono_class_from_mono_type (inflated);
3701 find_nocase (gpointer key, gpointer value, gpointer user_data)
3703 char *name = (char*)key;
3704 FindUserData *data = (FindUserData*)user_data;
3706 if (!data->value && (g_strcasecmp (name, (char*)data->key) == 0))
3707 data->value = value;
3711 * mono_class_from_name_case:
3712 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3713 * @name_space: the type namespace
3714 * @name: the type short name.
3716 * Obtains a MonoClass with a given namespace and a given name which
3717 * is located in the given MonoImage. The namespace and name
3718 * lookups are case insensitive.
3720 * You can also pass @NULL to the image, and that will lookup for
3721 * a type with the given namespace and name in all of the loaded
3722 * assemblies: notice that since there might be a name clash in this
3723 * case, passing @NULL is not encouraged if you need a precise type.
3727 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
3729 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
3730 guint32 cols [MONO_TYPEDEF_SIZE];
3735 if (image->dynamic) {
3737 FindUserData user_data;
3739 mono_loader_lock ();
3741 user_data.key = name_space;
3742 user_data.value = NULL;
3743 g_hash_table_foreach (image->name_cache, find_nocase, &user_data);
3745 if (user_data.value) {
3746 GHashTable *nspace_table = (GHashTable*)user_data.value;
3748 user_data.key = name;
3749 user_data.value = NULL;
3751 g_hash_table_foreach (nspace_table, find_nocase, &user_data);
3753 if (user_data.value)
3754 token = GPOINTER_TO_UINT (user_data.value);
3757 mono_loader_unlock ();
3760 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | token);
3766 /* add a cache if needed */
3767 for (i = 1; i <= t->rows; ++i) {
3768 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
3769 /* nested types are accessed from the nesting name */
3770 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
3771 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
3773 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3774 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3775 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
3776 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
3782 return_nested_in (MonoClass *class, char *nested) {
3784 char *s = strchr (nested, '/');
3791 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
3793 if (strcmp (found->name, nested) == 0) {
3795 return return_nested_in (found, s);
3804 * mono_class_from_name:
3805 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3806 * @name_space: the type namespace
3807 * @name: the type short name.
3809 * Obtains a MonoClass with a given namespace and a given name which
3810 * is located in the given MonoImage.
3812 * You can also pass `NULL' to the image, and that will lookup for
3813 * a type with the given namespace and name in all of the loaded
3814 * assemblies: notice that since there might be a name clash in this
3815 * case, passing NULL is not encouraged if you need a precise type.
3819 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
3821 GHashTable *nspace_table;
3822 MonoImage *loaded_image;
3828 if ((nested = strchr (name, '/'))) {
3829 int pos = nested - name;
3830 int len = strlen (name);
3833 memcpy (buf, name, len + 1);
3835 nested = buf + pos + 1;
3839 mono_loader_lock ();
3841 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
3844 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
3846 mono_loader_unlock ();
3851 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
3852 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
3853 guint32 cols [MONO_EXP_TYPE_SIZE];
3856 idx = mono_metadata_token_index (token);
3858 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
3860 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
3861 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
3862 loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
3865 class = mono_class_from_name (loaded_image, name_space, name);
3867 return return_nested_in (class, nested);
3870 g_error ("not yet implemented");
3874 token = MONO_TOKEN_TYPE_DEF | token;
3876 class = mono_class_get (image, token);
3878 return return_nested_in (class, nested);
3883 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
3884 gboolean check_interfaces)
3887 g_assert (klassc->idepth > 0);
3888 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
3889 if ((klassc->interface_id <= klass->max_interface_id) &&
3890 (klass->interface_offsets [klassc->interface_id] >= 0))
3892 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
3895 for (i = 0; i < klass->interface_count; i ++) {
3896 MonoClass *ic = klass->interfaces [i];
3901 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
3906 * MS.NET thinks interfaces are a subclass of Object, so we think it as
3909 if (klassc == mono_defaults.object_class)
3912 if (klass->generic_class && klass->generic_class->is_dynamic) {
3913 MonoDynamicGenericClass *dgclass = (MonoDynamicGenericClass *) klass->generic_class;
3915 if (!dgclass->parent)
3918 if (mono_metadata_type_equal (dgclass->parent, &klassc->byval_arg))
3920 klass = mono_class_from_mono_type (dgclass->parent);
3928 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
3931 mono_class_init (klass);
3933 if (!oklass->inited)
3934 mono_class_init (oklass);
3936 if (MONO_CLASS_IS_INTERFACE (klass)) {
3937 if ((oklass->byval_arg.type == MONO_TYPE_VAR) || (oklass->byval_arg.type == MONO_TYPE_MVAR))
3940 /* interface_offsets might not be set for dynamic classes */
3941 if (oklass->reflection_info && !oklass->interface_offsets)
3943 * oklass might be a generic type parameter but they have
3944 * interface_offsets set.
3946 return mono_reflection_call_is_assignable_to (oklass, klass);
3948 if ((klass->interface_id <= oklass->max_interface_id) &&
3949 (oklass->interface_offsets [klass->interface_id] != -1))
3951 } else if (klass->rank) {
3952 MonoClass *eclass, *eoclass;
3954 if (oklass->rank != klass->rank)
3957 /* vectors vs. one dimensional arrays */
3958 if (oklass->byval_arg.type != klass->byval_arg.type)
3961 eclass = klass->cast_class;
3962 eoclass = oklass->cast_class;
3965 * a is b does not imply a[] is b[] when a is a valuetype, and
3966 * b is a reference type.
3969 if (eoclass->valuetype) {
3970 if ((eclass == mono_defaults.enum_class) ||
3971 (eclass == mono_defaults.enum_class->parent) ||
3972 (eclass == mono_defaults.object_class))
3976 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
3977 } else if (mono_class_is_nullable (klass))
3978 return (mono_class_is_assignable_from (klass->cast_class, oklass));
3979 else if (klass == mono_defaults.object_class)
3982 return mono_class_has_parent (oklass, klass);
3986 * mono_class_get_cctor:
3988 * Returns the static constructor of @klass if it exists, NULL otherwise.
3991 mono_class_get_cctor (MonoClass *klass)
3993 MonoCachedClassInfo cached_info;
3995 if (!klass->has_cctor)
3998 if (mono_class_get_cached_class_info (klass, &cached_info))
3999 return mono_get_method (klass->image, cached_info.cctor_token, klass);
4001 return mono_class_get_method_from_name_flags (klass, ".cctor", -1, METHOD_ATTRIBUTE_SPECIAL_NAME);
4005 * mono_class_get_finalizer:
4007 * Returns the finalizer method of @klass if it exists, NULL otherwise.
4010 mono_class_get_finalizer (MonoClass *klass)
4012 MonoCachedClassInfo cached_info;
4013 static int finalize_slot = -1;
4015 if (finalize_slot < 0) {
4017 MonoClass* obj_class = mono_get_object_class ();
4018 mono_class_setup_vtable (obj_class);
4019 for (i = 0; i < obj_class->vtable_size; ++i) {
4020 MonoMethod *cm = obj_class->vtable [i];
4022 if (!strcmp (mono_method_get_name (cm), "Finalize")) {
4029 if (!klass->has_finalize)
4032 if (mono_class_get_cached_class_info (klass, &cached_info))
4033 return mono_get_method (cached_info.finalize_image, cached_info.finalize_token, NULL);
4035 mono_class_setup_vtable (klass);
4036 return klass->vtable [finalize_slot];
4041 * mono_class_needs_cctor_run:
4043 * Determines whenever the class has a static constructor and whenever it
4044 * needs to be called when executing CALLER.
4047 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
4051 method = mono_class_get_cctor (klass);
4053 return (method == caller) ? FALSE : TRUE;
4059 * mono_class_array_element_size:
4062 * Returns: the number of bytes an element of type @klass
4063 * uses when stored into an array.
4066 mono_class_array_element_size (MonoClass *klass)
4068 MonoType *type = &klass->byval_arg;
4071 switch (type->type) {
4074 case MONO_TYPE_BOOLEAN:
4078 case MONO_TYPE_CHAR:
4087 case MONO_TYPE_CLASS:
4088 case MONO_TYPE_STRING:
4089 case MONO_TYPE_OBJECT:
4090 case MONO_TYPE_SZARRAY:
4091 case MONO_TYPE_ARRAY:
4093 case MONO_TYPE_MVAR:
4094 return sizeof (gpointer);
4099 case MONO_TYPE_VALUETYPE:
4100 if (type->data.klass->enumtype) {
4101 type = type->data.klass->enum_basetype;
4102 klass = klass->element_class;
4105 return mono_class_instance_size (klass) - sizeof (MonoObject);
4106 case MONO_TYPE_GENERICINST:
4107 type = &type->data.generic_class->container_class->byval_arg;
4110 g_error ("unknown type 0x%02x in mono_class_array_element_size", type->type);
4116 * mono_array_element_size:
4117 * @ac: pointer to a #MonoArrayClass
4119 * Returns: the size of single array element.
4122 mono_array_element_size (MonoClass *ac)
4124 return mono_class_array_element_size (ac->element_class);
4128 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
4129 MonoGenericContext *context)
4131 if (image->dynamic) {
4132 MonoClass *tmp_handle_class;
4133 gpointer obj = mono_lookup_dynamic_token_class (image, token, &tmp_handle_class);
4135 g_assert (tmp_handle_class);
4137 *handle_class = tmp_handle_class;
4139 if (tmp_handle_class == mono_defaults.typehandle_class)
4140 return &((MonoClass*)obj)->byval_arg;
4145 switch (token & 0xff000000) {
4146 case MONO_TOKEN_TYPE_DEF:
4147 case MONO_TOKEN_TYPE_REF: {
4150 *handle_class = mono_defaults.typehandle_class;
4151 class = mono_class_get_full (image, token, context);
4154 mono_class_init (class);
4155 /* We return a MonoType* as handle */
4156 return &class->byval_arg;
4158 case MONO_TOKEN_TYPE_SPEC: {
4161 *handle_class = mono_defaults.typehandle_class;
4162 class = mono_class_create_from_typespec (image, token, context);
4165 mono_class_init (class);
4166 return &class->byval_arg;
4168 case MONO_TOKEN_FIELD_DEF: {
4170 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
4172 *handle_class = mono_defaults.fieldhandle_class;
4173 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
4176 mono_class_init (class);
4177 return mono_class_get_field (class, token);
4179 case MONO_TOKEN_METHOD_DEF: {
4181 meth = mono_get_method_full (image, token, NULL, context);
4183 *handle_class = mono_defaults.methodhandle_class;
4186 case MONO_TOKEN_MEMBER_REF: {
4187 guint32 cols [MONO_MEMBERREF_SIZE];
4189 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
4190 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
4191 mono_metadata_decode_blob_size (sig, &sig);
4192 if (*sig == 0x6) { /* it's a field */
4194 MonoClassField *field;
4195 field = mono_field_from_token (image, token, &klass, context);
4197 *handle_class = mono_defaults.fieldhandle_class;
4201 meth = mono_get_method_full (image, token, NULL, context);
4203 *handle_class = mono_defaults.methodhandle_class;
4208 g_warning ("Unknown token 0x%08x in ldtoken", token);
4215 * This function might need to call runtime functions so it can't be part
4216 * of the metadata library.
4218 static MonoLookupDynamicToken lookup_dynamic = NULL;
4221 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
4223 lookup_dynamic = func;
4227 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
4229 MonoClass *handle_class;
4231 return lookup_dynamic (image, token, &handle_class);
4235 mono_lookup_dynamic_token_class (MonoImage *image, guint32 token, MonoClass **handle_class)
4237 return lookup_dynamic (image, token, handle_class);
4240 static MonoGetCachedClassInfo get_cached_class_info = NULL;
4243 mono_install_get_cached_class_info (MonoGetCachedClassInfo func)
4245 get_cached_class_info = func;
4249 mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
4251 if (!get_cached_class_info)
4254 return get_cached_class_info (klass, res);
4258 mono_class_get_image (MonoClass *klass)
4260 return klass->image;
4264 * mono_class_get_element_class:
4265 * @klass: the MonoClass to act on
4267 * Returns: the element class of an array or an enumeration.
4270 mono_class_get_element_class (MonoClass *klass)
4272 return klass->element_class;
4276 * mono_class_is_valuetype:
4277 * @klass: the MonoClass to act on
4279 * Returns: true if the MonoClass represents a ValueType.
4282 mono_class_is_valuetype (MonoClass *klass)
4284 return klass->valuetype;
4288 * mono_class_is_enum:
4289 * @klass: the MonoClass to act on
4291 * Returns: true if the MonoClass represents an enumeration.
4294 mono_class_is_enum (MonoClass *klass)
4296 return klass->enumtype;
4300 * mono_class_enum_basetype:
4301 * @klass: the MonoClass to act on
4303 * Returns: the underlying type representation for an enumeration.
4306 mono_class_enum_basetype (MonoClass *klass)
4308 return klass->enum_basetype;
4312 * mono_class_get_parent
4313 * @klass: the MonoClass to act on
4315 * Returns: the parent class for this class.
4318 mono_class_get_parent (MonoClass *klass)
4320 return klass->parent;
4324 * mono_class_get_nesting_type;
4325 * @klass: the MonoClass to act on
4327 * Returns: the container type where this type is nested or NULL if this type is not a nested type.
4330 mono_class_get_nesting_type (MonoClass *klass)
4332 return klass->nested_in;
4336 * mono_class_get_rank:
4337 * @klass: the MonoClass to act on
4339 * Returns: the rank for the array (the number of dimensions).
4342 mono_class_get_rank (MonoClass *klass)
4348 * mono_class_get_flags:
4349 * @klass: the MonoClass to act on
4351 * The type flags from the TypeDef table from the metadata.
4352 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
4355 * Returns: the flags from the TypeDef table.
4358 mono_class_get_flags (MonoClass *klass)
4360 return klass->flags;
4364 * mono_class_get_name
4365 * @klass: the MonoClass to act on
4367 * Returns: the name of the class.
4370 mono_class_get_name (MonoClass *klass)
4376 * mono_class_get_namespace:
4377 * @klass: the MonoClass to act on
4379 * Returns: the namespace of the class.
4382 mono_class_get_namespace (MonoClass *klass)
4384 return klass->name_space;
4388 * mono_class_get_type:
4389 * @klass: the MonoClass to act on
4391 * This method returns the internal Type representation for the class.
4393 * Returns: the MonoType from the class.
4396 mono_class_get_type (MonoClass *klass)
4398 return &klass->byval_arg;
4402 * mono_class_get_byref_type:
4403 * @klass: the MonoClass to act on
4408 mono_class_get_byref_type (MonoClass *klass)
4410 return &klass->this_arg;
4414 * mono_class_num_fields:
4415 * @klass: the MonoClass to act on
4417 * Returns: the number of static and instance fields in the class.
4420 mono_class_num_fields (MonoClass *klass)
4422 return klass->field.count;
4426 * mono_class_num_methods:
4427 * @klass: the MonoClass to act on
4429 * Returns: the number of methods in the class.
4432 mono_class_num_methods (MonoClass *klass)
4434 return klass->method.count;
4438 * mono_class_num_properties
4439 * @klass: the MonoClass to act on
4441 * Returns: the number of properties in the class.
4444 mono_class_num_properties (MonoClass *klass)
4446 mono_class_setup_properties (klass);
4448 return klass->property.count;
4452 * mono_class_num_events:
4453 * @klass: the MonoClass to act on
4455 * Returns: the number of events in the class.
4458 mono_class_num_events (MonoClass *klass)
4460 mono_class_setup_events (klass);
4462 return klass->event.count;
4466 * mono_class_get_fields:
4467 * @klass: the MonoClass to act on
4469 * This routine is an iterator routine for retrieving the fields in a class.
4471 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4472 * iterate over all of the elements. When no more values are
4473 * available, the return value is NULL.
4475 * Returns: a @MonoClassField* on each iteration, or NULL when no more fields are available.
4478 mono_class_get_fields (MonoClass* klass, gpointer *iter)
4480 MonoClassField* field;
4483 mono_class_setup_fields_locking (klass);
4485 /* start from the first */
4486 if (klass->field.count) {
4487 return *iter = &klass->fields [0];
4495 if (field < &klass->fields [klass->field.count]) {
4496 return *iter = field;
4502 * mono_class_get_methods
4503 * @klass: the MonoClass to act on
4505 * This routine is an iterator routine for retrieving the fields in a class.
4507 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4508 * iterate over all of the elements. When no more values are
4509 * available, the return value is NULL.
4511 * Returns: a MonoMethod on each iteration or NULL when no more methods are available.
4514 mono_class_get_methods (MonoClass* klass, gpointer *iter)
4516 MonoMethod** method;
4520 mono_class_init (klass);
4522 mono_class_setup_methods (klass);
4523 /* start from the first */
4524 if (klass->method.count) {
4525 *iter = &klass->methods [0];
4526 return klass->methods [0];
4534 if (method < &klass->methods [klass->method.count]) {
4542 * mono_class_get_properties:
4543 * @klass: the MonoClass to act on
4545 * This routine is an iterator routine for retrieving the properties in a class.
4547 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4548 * iterate over all of the elements. When no more values are
4549 * available, the return value is NULL.
4551 * Returns: a @MonoProperty* on each invocation, or NULL when no more are available.
4554 mono_class_get_properties (MonoClass* klass, gpointer *iter)
4556 MonoProperty* property;
4560 mono_class_init (klass);
4562 mono_class_setup_properties (klass);
4563 /* start from the first */
4564 if (klass->property.count) {
4565 return *iter = &klass->properties [0];
4573 if (property < &klass->properties [klass->property.count]) {
4574 return *iter = property;
4580 * mono_class_get_events:
4581 * @klass: the MonoClass to act on
4583 * This routine is an iterator routine for retrieving the properties in a class.
4585 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4586 * iterate over all of the elements. When no more values are
4587 * available, the return value is NULL.
4589 * Returns: a @MonoEvent* on each invocation, or NULL when no more are available.
4592 mono_class_get_events (MonoClass* klass, gpointer *iter)
4598 mono_class_init (klass);
4600 mono_class_setup_events (klass);
4601 /* start from the first */
4602 if (klass->event.count) {
4603 return *iter = &klass->events [0];
4611 if (event < &klass->events [klass->event.count]) {
4612 return *iter = event;
4618 * mono_class_get_interfaces
4619 * @klass: the MonoClass to act on
4621 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
4623 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4624 * iterate over all of the elements. When no more values are
4625 * available, the return value is NULL.
4627 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
4630 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
4636 mono_class_init (klass);
4638 /* start from the first */
4639 if (klass->interface_count) {
4640 *iter = &klass->interfaces [0];
4641 return klass->interfaces [0];
4649 if (iface < &klass->interfaces [klass->interface_count]) {
4657 * mono_class_get_nested_types
4658 * @klass: the MonoClass to act on
4660 * This routine is an iterator routine for retrieving the nested types of a class.
4662 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4663 * iterate over all of the elements. When no more values are
4664 * available, the return value is NULL.
4666 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
4669 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
4675 mono_class_init (klass);
4677 /* start from the first */
4678 if (klass->nested_classes) {
4679 *iter = klass->nested_classes;
4680 return klass->nested_classes->data;
4682 /* no nested types */
4696 * mono_field_get_name:
4697 * @field: the MonoClassField to act on
4699 * Returns: the name of the field.
4702 mono_field_get_name (MonoClassField *field)
4708 * mono_field_get_type:
4709 * @field: the MonoClassField to act on
4711 * Returns: MonoType of the field.
4714 mono_field_get_type (MonoClassField *field)
4720 * mono_field_get_type:
4721 * @field: the MonoClassField to act on
4723 * Returns: MonoClass where the field was defined.
4726 mono_field_get_parent (MonoClassField *field)
4728 return field->parent;
4732 * mono_field_get_flags;
4733 * @field: the MonoClassField to act on
4735 * The metadata flags for a field are encoded using the
4736 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4738 * Returns: the flags for the field.
4741 mono_field_get_flags (MonoClassField *field)
4743 return field->type->attrs;
4747 * mono_property_get_name:
4748 * @prop: the MonoProperty to act on
4750 * Returns: the name of the property
4753 mono_property_get_name (MonoProperty *prop)
4759 * mono_property_get_set_method
4760 * @prop: the MonoProperty to act on.
4762 * Returns: the setter method of the property (A MonoMethod)
4765 mono_property_get_set_method (MonoProperty *prop)
4771 * mono_property_get_get_method
4772 * @prop: the MonoProperty to act on.
4774 * Returns: the setter method of the property (A MonoMethod)
4777 mono_property_get_get_method (MonoProperty *prop)
4783 * mono_property_get_parent:
4784 * @prop: the MonoProperty to act on.
4786 * Returns: the MonoClass where the property was defined.
4789 mono_property_get_parent (MonoProperty *prop)
4791 return prop->parent;
4795 * mono_property_get_flags:
4796 * @prop: the MonoProperty to act on.
4798 * The metadata flags for a property are encoded using the
4799 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4801 * Returns: the flags for the property.
4804 mono_property_get_flags (MonoProperty *prop)
4810 * mono_event_get_name:
4811 * @event: the MonoEvent to act on
4813 * Returns: the name of the event.
4816 mono_event_get_name (MonoEvent *event)
4822 * mono_event_get_add_method:
4823 * @event: The MonoEvent to act on.
4825 * Returns: the @add' method for the event (a MonoMethod).
4828 mono_event_get_add_method (MonoEvent *event)
4834 * mono_event_get_remove_method:
4835 * @event: The MonoEvent to act on.
4837 * Returns: the @remove method for the event (a MonoMethod).
4840 mono_event_get_remove_method (MonoEvent *event)
4842 return event->remove;
4846 * mono_event_get_raise_method:
4847 * @event: The MonoEvent to act on.
4849 * Returns: the @raise method for the event (a MonoMethod).
4852 mono_event_get_raise_method (MonoEvent *event)
4854 return event->raise;
4858 * mono_event_get_parent:
4859 * @event: the MonoEvent to act on.
4861 * Returns: the MonoClass where the event is defined.
4864 mono_event_get_parent (MonoEvent *event)
4866 return event->parent;
4870 * mono_event_get_flags
4871 * @event: the MonoEvent to act on.
4873 * The metadata flags for an event are encoded using the
4874 * EVENT_* constants. See the tabledefs.h file for details.
4876 * Returns: the flags for the event.
4879 mono_event_get_flags (MonoEvent *event)
4881 return event->attrs;
4885 * mono_class_get_method_from_name:
4886 * @klass: where to look for the method
4887 * @name_space: name of the method
4888 * @param_count: number of parameters. -1 for any number.
4890 * Obtains a MonoMethod with a given name and number of parameters.
4891 * It only works if there are no multiple signatures for any given method name.
4894 mono_class_get_method_from_name (MonoClass *klass, const char *name, int param_count)
4896 return mono_class_get_method_from_name_flags (klass, name, param_count, 0);
4900 * mono_class_get_method_from_name_flags:
4901 * @klass: where to look for the method
4902 * @name_space: name of the method
4903 * @param_count: number of parameters. -1 for any number.
4904 * @flags: flags which must be set in the method
4906 * Obtains a MonoMethod with a given name and number of parameters.
4907 * It only works if there are no multiple signatures for any given method name.
4910 mono_class_get_method_from_name_flags (MonoClass *klass, const char *name, int param_count, int flags)
4912 MonoMethod *res = NULL;
4915 mono_class_init (klass);
4917 if (klass->methods) {
4918 mono_class_setup_methods (klass);
4919 for (i = 0; i < klass->method.count; ++i) {
4920 MonoMethod *method = klass->methods [i];
4922 if (method->name[0] == name [0] &&
4923 !strcmp (name, method->name) &&
4924 (param_count == -1 || mono_method_signature (method)->param_count == param_count) &&
4925 ((method->flags & flags) == flags)) {
4932 /* Search directly in the metadata to avoid calling setup_methods () */
4933 for (i = 0; i < klass->method.count; ++i) {
4934 guint32 cols [MONO_METHOD_SIZE];
4937 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_METHOD], klass->method.first + i, cols, MONO_METHOD_SIZE);
4939 if (!strcmp (mono_metadata_string_heap (klass->image, cols [MONO_METHOD_NAME]), name)) {
4940 method = mono_get_method (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass);
4941 if ((param_count == -1) || mono_method_signature (method)->param_count == param_count) {
4953 * mono_class_set_failure:
4954 * @klass: class in which the failure was detected
4955 * @ex_type: the kind of exception/error to be thrown (later)
4956 * @ex_data: exception data (specific to each type of exception/error)
4958 * Keep a detected failure informations in the class for later processing.
4959 * Note that only the first failure is kept.
4962 mono_class_set_failure (MonoClass *klass, guint32 ex_type, void *ex_data)
4964 if (klass->exception_type)
4966 klass->exception_type = ex_type;
4967 klass->exception_data = ex_data;
4972 * mono_class_get_exception_for_failure:
4973 * @klass: class in which the failure was detected
4975 * Return a constructed MonoException than the caller can then throw
4976 * using mono_raise_exception - or NULL if no failure is present (or
4977 * doesn't result in an exception).
4980 mono_class_get_exception_for_failure (MonoClass *klass)
4982 switch (klass->exception_type) {
4983 case MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND: {
4984 MonoDomain *domain = mono_domain_get ();
4985 MonoSecurityManager* secman = mono_security_manager_get_methods ();
4986 MonoMethod *method = klass->exception_data;
4987 guint32 error = (method) ? MONO_METADATA_INHERITANCEDEMAND_METHOD : MONO_METADATA_INHERITANCEDEMAND_CLASS;
4988 MonoObject *exc = NULL;
4992 args [1] = mono_assembly_get_object (domain, mono_image_get_assembly (klass->image));
4993 args [2] = mono_type_get_object (domain, &klass->byval_arg);
4994 args [3] = (method) ? mono_method_get_object (domain, method, NULL) : NULL;
4996 mono_runtime_invoke (secman->inheritsecurityexception, NULL, args, &exc);
4997 return (MonoException*) exc;
4999 /* TODO - handle other class related failures */