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/exception.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:
365 * Returns: the MonoType for the underlying interger type if @type
366 * is an enum, otherwise the type itself.
369 mono_type_get_underlying_type (MonoType *type)
371 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
372 return type->data.klass->enum_basetype;
377 mono_class_is_open_constructed_type (MonoType *t)
383 case MONO_TYPE_SZARRAY:
384 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
385 case MONO_TYPE_ARRAY:
386 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
388 return mono_class_is_open_constructed_type (t->data.type);
389 case MONO_TYPE_GENERICINST: {
390 MonoGenericClass *gclass = t->data.generic_class;
393 if (mono_class_is_open_constructed_type (&gclass->container_class->byval_arg))
395 for (i = 0; i < gclass->inst->type_argc; i++)
396 if (mono_class_is_open_constructed_type (gclass->inst->type_argv [i]))
405 static MonoGenericClass *
406 inflate_generic_class (MonoGenericClass *ogclass, MonoGenericContext *context)
408 MonoInflatedGenericClass *igclass;
409 MonoGenericClass *ngclass, *cached;
410 MonoGenericInst *ninst;
412 ninst = mono_metadata_inflate_generic_inst (ogclass->inst, context);
413 if (ninst == ogclass->inst)
416 if (ogclass->is_dynamic) {
417 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
418 igclass = &dgclass->generic_class;
419 ngclass = &igclass->generic_class;
420 ngclass->is_inflated = 1;
421 ngclass->is_dynamic = 1;
423 igclass = g_new0 (MonoInflatedGenericClass, 1);
424 ngclass = &igclass->generic_class;
425 ngclass->is_inflated = 1;
430 ngclass->inst = ninst;
432 igclass->klass = NULL;
434 ngclass->context = g_new0 (MonoGenericContext, 1);
435 ngclass->context->container = ngclass->container_class->generic_container;
436 ngclass->context->gclass = ngclass;
439 cached = mono_metadata_lookup_generic_class (ngclass);
440 mono_loader_unlock ();
442 g_free (ngclass->context);
451 inflate_generic_type (MonoType *type, MonoGenericContext *context)
453 switch (type->type) {
454 case MONO_TYPE_MVAR: {
455 int num = type->data.generic_param->num;
456 MonoGenericInst *inst = context->gmethod ? context->gmethod->inst : NULL;
457 if (!inst || !inst->type_argv)
459 if (num >= inst->type_argc)
460 g_error ("MVAR %d (%s) cannot be expanded in this context with %d instantiations", num, type->data.generic_param->name, inst->type_argc);
461 return dup_type (inst->type_argv [num], type);
463 case MONO_TYPE_VAR: {
464 int num = type->data.generic_param->num;
465 MonoGenericInst *inst = context->gclass ? context->gclass->inst : NULL;
468 if (num >= inst->type_argc)
469 g_error ("VAR %d (%s) cannot be expanded in this context with %d instantiations", num, type->data.generic_param->name, inst->type_argc);
470 return dup_type (inst->type_argv [num], type);
472 case MONO_TYPE_SZARRAY: {
473 MonoClass *eclass = type->data.klass;
474 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
477 nt = dup_type (type, type);
478 nt->data.klass = mono_class_from_mono_type (inflated);
481 case MONO_TYPE_ARRAY: {
482 MonoClass *eclass = type->data.array->eklass;
483 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
486 nt = dup_type (type, type);
487 nt->data.array = g_memdup (nt->data.array, sizeof (MonoArrayType));
488 nt->data.array->eklass = mono_class_from_mono_type (inflated);
491 case MONO_TYPE_GENERICINST: {
492 MonoGenericClass *gclass = type->data.generic_class;
494 if (!gclass->inst->is_open)
496 gclass = inflate_generic_class (gclass, context);
497 if (gclass == type->data.generic_class)
499 nt = dup_type (type, type);
500 nt->data.generic_class = gclass;
503 case MONO_TYPE_CLASS:
504 case MONO_TYPE_VALUETYPE: {
505 MonoClass *klass = type->data.klass;
506 MonoGenericClass *gclass;
509 if (!klass->generic_container)
511 gclass = inflate_generic_class (klass->generic_container->context.gclass, context);
512 if (gclass == klass->generic_container->context.gclass)
514 nt = dup_type (type, type);
515 nt->type = MONO_TYPE_GENERICINST;
516 nt->data.generic_class = gclass;
525 MonoInflatedGenericClass*
526 mono_get_inflated_generic_class (MonoGenericClass *gclass)
528 g_assert (gclass->is_inflated);
529 mono_class_create_generic ((MonoInflatedGenericClass *) gclass);
530 return (MonoInflatedGenericClass *) gclass;
534 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
536 MonoType *inflated = inflate_generic_type (type, context);
539 return dup_type (type, type);
541 mono_stats.inflated_type_count++;
545 static MonoGenericContext *
546 inflate_generic_context (MonoGenericContext *context, MonoGenericContext *inflate_with)
548 MonoGenericClass *gclass = NULL;
549 MonoGenericMethod *gmethod = NULL;
550 MonoGenericContext *res;
553 gclass = inflate_generic_class (context->gclass, inflate_with);
555 if (context->gmethod) {
556 MonoGenericInst *ninst = mono_metadata_inflate_generic_inst (context->gmethod->inst, inflate_with);
557 if (gclass == context->gclass && ninst == context->gmethod->inst) {
558 gmethod = context->gmethod;
560 gmethod = g_new0 (MonoGenericMethod, 1);
561 gmethod->generic_class = gclass;
562 gmethod->container = context->container;
563 gmethod->inst = ninst;
567 if (gclass == context->gclass && gmethod == context->gmethod)
570 res = g_new0 (MonoGenericContext, 1);
572 res->container = gmethod ? gmethod->container : context->container;
573 res->gclass = gclass;
574 res->gmethod = gmethod;
580 * mono_class_inflate_generic_method:
582 * Instantiate method @method with the generic context @context.
583 * BEWARE: All non-trivial fields are invalid, including klass, signature, and header.
584 * Use mono_get_inflated_method (), mono_method_signature () and mono_method_get_header () to get the correct values.
587 mono_class_inflate_generic_method (MonoMethod *method, MonoClass *klass_hint, MonoGenericContext *context)
590 MonoMethodInflated *iresult;
591 MonoMethodSignature *sig;
593 /* The `method' has already been instantiated before -> we need to create a new context. */
594 while (method->is_inflated) {
595 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
596 context = inflate_generic_context (imethod->context, context);
597 if (context == imethod->context)
599 method = imethod->declaring;
602 if (!method->generic_container && !method->klass->generic_container)
605 mono_stats.inflated_method_count++;
606 iresult = g_new0 (MonoMethodInflated, 1);
608 sig = mono_method_signature (method);
610 iresult->method.pinvoke = *(MonoMethodPInvoke*)method;
612 iresult->method.normal = *(MonoMethodNormal*)method;
613 iresult->method.normal.header = NULL;
616 result = (MonoMethod *) iresult;
617 result->is_inflated = 1;
618 result->signature = NULL;
619 iresult->context = context;
620 iresult->declaring = method;
622 if (method->generic_container && !context->gmethod) {
623 MonoGenericMethod *gmethod = g_memdup (method->generic_container->context.gmethod, sizeof (*gmethod));
624 context = g_memdup (context, sizeof (*context));
626 gmethod->generic_class = context->gclass;
627 context->container = method->generic_container;
628 context->gmethod = gmethod;
629 iresult->context = context;
632 if (klass_hint && klass_hint->generic_class &&
633 (klass_hint->generic_class->container_class != method->klass || klass_hint->generic_class->inst != context->gclass->inst))
636 if (method->klass->generic_container)
637 result->klass = klass_hint;
639 if (!result->klass) {
640 MonoType *inflated = inflate_generic_type (&method->klass->byval_arg, context);
641 result->klass = inflated ? mono_class_from_mono_type (inflated) : method->klass;
644 iresult->inflated = result;
650 * mono_get_inflated_method:
652 * For performance reasons, mono_class_inflate_generic_method() does not actually instantiate the
653 * method, it just "prepares" it for that. If you really need to fully instantiate the method
654 * (including its signature and header), call this method.
657 mono_get_inflated_method (MonoMethod *method)
663 * mono_class_find_enum_basetype:
664 * @class: The enum class
666 * Determine the basetype of an enum by iterating through its fields. We do this
667 * in a separate function since it is cheaper than calling mono_class_setup_fields.
670 mono_class_find_enum_basetype (MonoClass *class)
672 MonoImage *m = class->image;
673 const int top = class->field.count;
674 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
677 g_assert (class->enumtype);
680 * Fetch all the field information.
682 for (i = 0; i < top; i++){
684 guint32 cols [MONO_FIELD_SIZE];
685 int idx = class->field.first + i;
686 MonoGenericContainer *container = NULL;
689 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
690 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
691 mono_metadata_decode_value (sig, &sig);
692 /* FIELD signature == 0x06 */
693 g_assert (*sig == 0x06);
694 if (class->generic_container)
695 container = class->generic_container;
696 else if (class->generic_class) {
697 MonoClass *gklass = class->generic_class->container_class;
699 container = gklass->generic_container;
700 g_assert (container);
702 ftype = mono_metadata_parse_type_full (m, container, MONO_PARSE_FIELD, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
705 if (class->generic_class) {
706 ftype = mono_class_inflate_generic_type (ftype, class->generic_class->context);
707 ftype->attrs = cols [MONO_FIELD_FLAGS];
710 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC))
718 * mono_class_setup_fields:
719 * @class: The class to initialize
721 * Initializes the class->fields.
722 * Assumes the loader lock is held.
725 mono_class_setup_fields (MonoClass *class)
727 MonoImage *m = class->image;
728 const int top = class->field.count;
729 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
730 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
731 int i, blittable = TRUE, real_size = 0;
732 guint32 packing_size = 0;
733 gboolean explicit_size;
734 MonoClassField *field;
735 MonoGenericContainer *container = NULL;
736 MonoClass *gklass = NULL;
738 if (class->size_inited)
742 mono_class_init (class);
744 class->instance_size = 0;
745 class->class_size = 0;
748 if (!class->parent->size_inited)
749 mono_class_setup_fields (class->parent);
750 class->instance_size += class->parent->instance_size;
751 class->min_align = class->parent->min_align;
752 /* we use |= since it may have been set already */
753 class->has_references |= class->parent->has_references;
754 blittable = class->parent->blittable;
756 class->instance_size = sizeof (MonoObject);
757 class->min_align = 1;
760 /* Get the real size */
761 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
764 g_assert ((packing_size & 0xfffffff0) == 0);
765 class->packing_size = packing_size;
766 real_size += class->instance_size;
770 if (explicit_size && real_size) {
771 class->instance_size = MAX (real_size, class->instance_size);
773 class->size_inited = 1;
774 class->blittable = blittable;
778 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT)
781 /* Prevent infinite loops if the class references itself */
782 class->size_inited = 1;
784 class->fields = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoClassField) * top);
786 if (class->generic_container) {
787 container = class->generic_container;
788 } else if (class->generic_class) {
789 gklass = class->generic_class->container_class;
790 container = gklass->generic_container;
791 g_assert (container);
793 mono_class_setup_fields (gklass);
797 * Fetch all the field information.
799 for (i = 0; i < top; i++){
800 int idx = class->field.first + i;
801 field = &class->fields [i];
803 field->parent = class;
805 if (class->generic_class) {
806 MonoClassField *gfield = &gklass->fields [i];
807 MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
809 ifield->generic_type = gfield->type;
810 field->name = gfield->name;
811 field->generic_info = ifield;
812 field->type = mono_class_inflate_generic_type (gfield->type, class->generic_class->context);
813 field->type->attrs = gfield->type->attrs;
814 if (mono_field_is_deleted (field))
816 field->offset = gfield->offset;
817 field->data = gfield->data;
821 guint32 cols [MONO_FIELD_SIZE];
823 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
824 /* The name is needed for fieldrefs */
825 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
826 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
827 mono_metadata_decode_value (sig, &sig);
828 /* FIELD signature == 0x06 */
829 g_assert (*sig == 0x06);
830 field->type = mono_metadata_parse_type_full (m, container, MONO_PARSE_FIELD, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
832 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
835 if (mono_field_is_deleted (field))
837 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
838 mono_metadata_field_info (m, idx, &field->offset, NULL, NULL);
839 if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
840 g_warning ("%s not initialized correctly (missing field layout info for %s)",
841 class->name, field->name);
844 if (field->type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
845 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
847 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
848 field->data = mono_image_rva_map (class->image, rva);
852 /* Only do these checks if we still think this type is blittable */
853 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
854 if (field->type->byref || MONO_TYPE_IS_REFERENCE (field->type)) {
857 MonoClass *field_class = mono_class_from_mono_type (field->type);
858 if (!field_class || !field_class->blittable)
863 if (class->enumtype && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
864 class->enum_basetype = field->type;
865 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
866 blittable = class->element_class->blittable;
869 /* The def_value of fields is compute lazily during vtable creation */
872 if (class == mono_defaults.string_class)
875 class->blittable = blittable;
877 if (class->enumtype && !class->enum_basetype) {
878 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
881 if (explicit_size && real_size) {
882 class->instance_size = MAX (real_size, class->instance_size);
885 mono_class_layout_fields (class);
889 * mono_class_setup_fields_locking:
890 * @class: The class to initialize
892 * Initializes the class->fields.
893 * Aquires the loader lock.
896 mono_class_setup_fields_locking (MonoClass *class)
899 mono_class_setup_fields (class);
900 mono_loader_unlock ();
904 * mono_class_has_references:
906 * Returns whenever @klass->has_references is set, initializing it if needed.
907 * Aquires the loader lock.
910 mono_class_has_references (MonoClass *klass)
912 if (klass->init_pending) {
913 /* Be conservative */
916 mono_class_init (klass);
918 return klass->has_references;
922 /* useful until we keep track of gc-references in corlib etc. */
923 #define IS_GC_REFERENCE(t) ((t)->type == MONO_TYPE_U || (t)->type == MONO_TYPE_I || (t)->type == MONO_TYPE_PTR)
926 * LOCKING: this is supposed to be called with the loader lock held.
929 mono_class_layout_fields (MonoClass *class)
932 const int top = class->field.count;
933 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
934 guint32 pass, passes, real_size;
935 gboolean gc_aware_layout = FALSE;
936 MonoClassField *field;
938 if (class->generic_container ||
939 (class->generic_class && class->generic_class->inst->is_open))
943 * Enable GC aware auto layout: in this mode, reference
944 * fields are grouped together inside objects, increasing collector
946 * Requires that all classes whose layout is known to native code be annotated
947 * with [StructLayout (LayoutKind.Sequential)]
948 * Value types have gc_aware_layout disabled by default, as per
949 * what the default is for other runtimes.
951 /* corlib is missing [StructLayout] directives in many places */
952 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
953 if (class->image != mono_defaults.corlib &&
954 class->byval_arg.type != MONO_TYPE_VALUETYPE)
955 gc_aware_layout = TRUE;
958 /* Compute klass->has_references */
960 * Process non-static fields first, since static fields might recursively
961 * refer to the class itself.
963 for (i = 0; i < top; i++) {
966 field = &class->fields [i];
968 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
969 ftype = mono_type_get_underlying_type (field->type);
970 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype)))))
971 class->has_references = TRUE;
975 for (i = 0; i < top; i++) {
978 field = &class->fields [i];
980 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC) {
981 ftype = mono_type_get_underlying_type (field->type);
982 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype)))))
983 class->has_static_refs = TRUE;
987 for (i = 0; i < top; i++) {
990 field = &class->fields [i];
992 ftype = mono_type_get_underlying_type (field->type);
993 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype))))) {
994 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
995 class->has_static_refs = TRUE;
997 class->has_references = TRUE;
1002 * Compute field layout and total size (not considering static fields)
1006 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
1007 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
1009 if (gc_aware_layout)
1014 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
1018 real_size = class->parent->instance_size;
1020 real_size = sizeof (MonoObject);
1022 for (pass = 0; pass < passes; ++pass) {
1023 for (i = 0; i < top; i++){
1026 field = &class->fields [i];
1028 if (mono_field_is_deleted (field))
1030 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1033 if (gc_aware_layout) {
1035 * We process fields with reference type in the first pass,
1036 * and fields with non-reference type in the second pass.
1037 * We use IS_POINTER instead of IS_REFERENCE because in
1038 * some internal structures, we store GC_MALLOCed memory
1039 * in IntPtr fields...
1041 if (MONO_TYPE_IS_POINTER (field->type)) {
1050 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
1051 (strcmp (field->name, "$PRIVATE$") == 0)) {
1052 /* This field is a hack inserted by MCS to empty structures */
1056 size = mono_type_size (field->type, &align);
1058 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
1059 align = class->packing_size ? MIN (class->packing_size, align): align;
1060 class->min_align = MAX (align, class->min_align);
1061 field->offset = real_size;
1062 field->offset += align - 1;
1063 field->offset &= ~(align - 1);
1064 real_size = field->offset + size;
1067 class->instance_size = MAX (real_size, class->instance_size);
1069 if (class->instance_size & (class->min_align - 1)) {
1070 class->instance_size += class->min_align - 1;
1071 class->instance_size &= ~(class->min_align - 1);
1075 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
1077 for (i = 0; i < top; i++) {
1080 field = &class->fields [i];
1083 * There must be info about all the fields in a type if it
1084 * uses explicit layout.
1087 if (mono_field_is_deleted (field))
1089 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1092 size = mono_type_size (field->type, &align);
1095 * When we get here, field->offset is already set by the
1096 * loader (for either runtime fields or fields loaded from metadata).
1097 * The offset is from the start of the object: this works for both
1098 * classes and valuetypes.
1100 field->offset += sizeof (MonoObject);
1105 real_size = MAX (real_size, size + field->offset);
1107 class->instance_size = MAX (real_size, class->instance_size);
1111 class->size_inited = 1;
1114 * Compute static field layout and size
1116 for (i = 0; i < top; i++){
1119 field = &class->fields [i];
1121 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
1123 if (mono_field_is_deleted (field))
1126 size = mono_type_size (field->type, &align);
1127 field->offset = class->class_size;
1128 field->offset += align - 1;
1129 field->offset &= ~(align - 1);
1130 class->class_size = field->offset + size;
1135 * mono_class_setup_methods:
1137 * Initializes the 'methods' array in the klass.
1138 * Calling this method should be avoided if possible since it allocates a lot
1139 * of long-living MonoMethod structures.
1142 mono_class_setup_methods (MonoClass *class)
1145 MonoMethod **methods;
1150 mono_loader_lock ();
1152 if (class->methods) {
1153 mono_loader_unlock ();
1157 //printf ("INIT: %s.%s\n", class->name_space, class->name);
1159 if (!class->methods) {
1160 methods = mono_mempool_alloc (class->image->mempool, sizeof (MonoMethod*) * class->method.count);
1161 for (i = 0; i < class->method.count; ++i) {
1162 methods [i] = mono_get_method (class->image, MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
1166 if (MONO_CLASS_IS_INTERFACE (class))
1167 for (i = 0; i < class->method.count; ++i)
1168 methods [i]->slot = i;
1170 /* Leave this assignment as the last op in this function */
1171 class->methods = methods;
1173 mono_loader_unlock ();
1178 mono_class_setup_properties (MonoClass *class)
1180 guint startm, endm, i, j;
1181 guint32 cols [MONO_PROPERTY_SIZE];
1182 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
1183 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
1184 MonoProperty *properties;
1187 if (class->properties)
1190 mono_loader_lock ();
1192 if (class->properties) {
1193 mono_loader_unlock ();
1197 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &last);
1198 class->property.count = last - class->property.first;
1200 if (class->property.count)
1201 mono_class_setup_methods (class);
1203 properties = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoProperty) * class->property.count);
1204 for (i = class->property.first; i < last; ++i) {
1205 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
1206 properties [i - class->property.first].parent = class;
1207 properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
1208 properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
1210 startm = mono_metadata_methods_from_property (class->image, i, &endm);
1211 for (j = startm; j < endm; ++j) {
1212 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
1213 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1214 case METHOD_SEMANTIC_SETTER:
1215 properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1217 case METHOD_SEMANTIC_GETTER:
1218 properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1226 /* Leave this assignment as the last op in the function */
1227 class->properties = properties;
1229 mono_loader_unlock ();
1233 inflate_event (MonoClass *class, MonoEvent *event, MonoInflatedGenericClass *gclass)
1235 event->parent = class;
1238 MonoMethod *inflated = mono_class_inflate_generic_method (
1239 event->add, class, gclass->generic_class.context);
1241 event->add = mono_get_inflated_method (inflated);
1244 if (event->remove) {
1245 MonoMethod *inflated = mono_class_inflate_generic_method (
1246 event->remove, class, gclass->generic_class.context);
1248 event->remove = mono_get_inflated_method (inflated);
1252 MonoMethod *inflated = mono_class_inflate_generic_method (
1253 event->raise, class, gclass->generic_class.context);
1255 event->raise = mono_get_inflated_method (inflated);
1259 MonoMethod *inflated = mono_class_inflate_generic_method (
1260 event->other, class, gclass->generic_class.context);
1262 event->other = mono_get_inflated_method (inflated);
1267 mono_class_setup_events (MonoClass *class)
1269 guint startm, endm, i, j;
1270 guint32 cols [MONO_EVENT_SIZE];
1271 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
1272 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
1279 mono_loader_lock ();
1281 if (class->events) {
1282 mono_loader_unlock ();
1286 if (class->generic_class) {
1287 MonoInflatedGenericClass *gclass;
1290 gclass = mono_get_inflated_generic_class (class->generic_class);
1291 gklass = gclass->generic_class.container_class;
1293 mono_class_setup_events (gklass);
1294 class->event = gklass->event;
1296 class->events = g_new0 (MonoEvent, class->event.count);
1298 for (i = 0; i < class->event.count; i++) {
1299 class->events [i] = gklass->events [i];
1300 inflate_event (class, &class->events [i], gclass);
1303 mono_loader_unlock ();
1307 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &last);
1308 class->event.count = last - class->event.first;
1310 if (class->event.count)
1311 mono_class_setup_methods (class);
1313 events = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoEvent) * class->event.count);
1314 for (i = class->event.first; i < last; ++i) {
1315 MonoEvent *event = &events [i - class->event.first];
1317 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
1318 event->parent = class;
1319 event->attrs = cols [MONO_EVENT_FLAGS];
1320 event->name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
1322 startm = mono_metadata_methods_from_event (class->image, i, &endm);
1323 for (j = startm; j < endm; ++j) {
1324 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
1325 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1326 case METHOD_SEMANTIC_ADD_ON:
1327 event->add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1329 case METHOD_SEMANTIC_REMOVE_ON:
1330 event->remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1332 case METHOD_SEMANTIC_FIRE:
1333 event->raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1335 case METHOD_SEMANTIC_OTHER: {
1338 if (event->other == NULL) {
1339 event->other = g_new0 (MonoMethod*, 1);
1341 while (event->other [n])
1343 event->other = g_realloc (event->other, (n + 1) * sizeof (MonoMethod*));
1345 event->other [n] = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1353 /* Leave this assignment as the last op in the function */
1354 class->events = events;
1356 mono_loader_unlock ();
1360 mono_get_unique_iid (MonoClass *class)
1362 static GHashTable *iid_hash = NULL;
1363 static guint iid = 0;
1364 char *str, *type_name;
1368 g_assert (MONO_CLASS_IS_INTERFACE (class));
1370 mono_loader_lock ();
1373 iid_hash = g_hash_table_new (g_str_hash, g_str_equal);
1375 if (class->generic_class && !class->generic_class->inst->is_open) {
1376 generic_id = class->generic_class->inst->id;
1377 g_assert (generic_id != 0);
1381 type_name = mono_type_full_name (&class->byval_arg);
1382 str = g_strdup_printf ("%s|%s|%d", class->image->name, type_name, generic_id);
1385 if (g_hash_table_lookup_extended (iid_hash, str, NULL, &value)) {
1386 mono_loader_unlock ();
1387 if (mono_print_vtable)
1388 printf ("Interface: reusing id %d for %s\n", iid, str);
1390 return GPOINTER_TO_INT (value);
1392 if (mono_print_vtable)
1393 printf ("Interface: assigned id %d to %s\n", iid, str);
1394 g_hash_table_insert (iid_hash, str, GINT_TO_POINTER (iid));
1398 mono_loader_unlock ();
1400 g_assert (iid <= 65535);
1405 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
1410 for (i = 0; i < klass->interface_count; i++) {
1411 ic = klass->interfaces [i];
1414 *res = g_ptr_array_new ();
1415 g_ptr_array_add (*res, ic);
1416 mono_class_init (ic);
1418 collect_implemented_interfaces_aux (ic, res);
1423 mono_class_get_implemented_interfaces (MonoClass *klass)
1425 GPtrArray *res = NULL;
1427 collect_implemented_interfaces_aux (klass, &res);
1431 typedef struct _IOffsetInfo IOffsetInfo;
1432 struct _IOffsetInfo {
1436 int data [MONO_ZERO_LEN_ARRAY];
1439 static IOffsetInfo *cached_offset_info = NULL;
1440 static int next_offset_info_size = 128;
1443 cache_interface_offsets (int max_iid, int *data)
1445 IOffsetInfo *cached_info;
1448 for (cached_info = cached_offset_info; cached_info; cached_info = cached_info->next) {
1449 cached = cached_info->data;
1450 while (cached < cached_info->data + cached_info->size && *cached) {
1451 if (*cached == max_iid) {
1452 int i, matched = TRUE;
1454 for (i = 0; i < max_iid; ++i) {
1455 if (cached [i] != data [i]) {
1464 cached += *cached + 1;
1468 /* find a free slot */
1469 for (cached_info = cached_offset_info; cached_info; cached_info = cached_info->next) {
1470 if (cached_info->size - cached_info->next_free >= max_iid + 1) {
1471 cached = &cached_info->data [cached_info->next_free];
1472 *cached++ = max_iid;
1473 memcpy (cached, data, max_iid * sizeof (int));
1474 cached_info->next_free += max_iid + 1;
1478 /* allocate a new chunk */
1479 if (max_iid + 1 < next_offset_info_size) {
1480 new_size = next_offset_info_size;
1481 if (next_offset_info_size < 4096)
1482 next_offset_info_size += next_offset_info_size >> 2;
1484 new_size = max_iid + 1;
1486 cached_info = g_malloc0 (sizeof (IOffsetInfo) + sizeof (int) * new_size);
1487 cached_info->size = new_size;
1488 /*g_print ("allocated %d offset entries at %p (total: %d)\n", new_size, cached_info->data, offset_info_total_size);*/
1489 cached = &cached_info->data [0];
1490 *cached++ = max_iid;
1491 memcpy (cached, data, max_iid * sizeof (int));
1492 cached_info->next_free += max_iid + 1;
1493 cached_info->next = cached_offset_info;
1494 cached_offset_info = cached_info;
1499 setup_interface_offsets (MonoClass *class, int cur_slot)
1506 /* compute maximum number of slots and maximum interface id */
1508 for (k = class; k ; k = k->parent) {
1509 for (i = 0; i < k->interface_count; i++) {
1510 ic = k->interfaces [i];
1513 mono_class_init (ic);
1515 if (max_iid < ic->interface_id)
1516 max_iid = ic->interface_id;
1518 ifaces = mono_class_get_implemented_interfaces (k);
1520 for (i = 0; i < ifaces->len; ++i) {
1521 ic = g_ptr_array_index (ifaces, i);
1522 if (max_iid < ic->interface_id)
1523 max_iid = ic->interface_id;
1525 g_ptr_array_free (ifaces, TRUE);
1529 if (MONO_CLASS_IS_INTERFACE (class)) {
1530 if (max_iid < class->interface_id)
1531 max_iid = class->interface_id;
1533 class->max_interface_id = max_iid;
1534 /* compute vtable offset for interfaces */
1535 class->interface_offsets = g_malloc (sizeof (gpointer) * (max_iid + 1));
1537 for (i = 0; i <= max_iid; i++)
1538 class->interface_offsets [i] = -1;
1540 ifaces = mono_class_get_implemented_interfaces (class);
1542 for (i = 0; i < ifaces->len; ++i) {
1543 ic = g_ptr_array_index (ifaces, i);
1544 class->interface_offsets [ic->interface_id] = cur_slot;
1545 cur_slot += ic->method.count;
1547 g_ptr_array_free (ifaces, TRUE);
1550 for (k = class->parent; k ; k = k->parent) {
1551 ifaces = mono_class_get_implemented_interfaces (k);
1553 for (i = 0; i < ifaces->len; ++i) {
1554 ic = g_ptr_array_index (ifaces, i);
1556 if (class->interface_offsets [ic->interface_id] == -1) {
1557 int io = k->interface_offsets [ic->interface_id];
1561 class->interface_offsets [ic->interface_id] = io;
1564 g_ptr_array_free (ifaces, TRUE);
1568 if (MONO_CLASS_IS_INTERFACE (class))
1569 class->interface_offsets [class->interface_id] = cur_slot;
1571 cached_data = cache_interface_offsets (max_iid + 1, class->interface_offsets);
1572 g_free (class->interface_offsets);
1573 class->interface_offsets = cached_data;
1579 setup_generic_vtable (MonoClass *class)
1584 gklass = class->generic_class->container_class;
1586 mono_class_init (gklass);
1587 class->vtable_size = gklass->vtable_size;
1589 class->vtable = g_new0 (MonoMethod*, class->vtable_size);
1590 memcpy (class->vtable, gklass->vtable, sizeof (MonoMethod*) * class->vtable_size);
1592 for (i = 0; i < class->vtable_size; i++) {
1593 MonoMethod *m = class->vtable [i];
1598 m = mono_class_inflate_generic_method (m, class, class->generic_class->context);
1599 class->vtable [i] = m;
1602 class->max_interface_id = gklass->max_interface_id;
1603 class->interface_offsets = g_new0 (gint, gklass->max_interface_id + 1);
1604 memcpy (class->interface_offsets, gklass->interface_offsets,
1605 sizeof (gint) * (gklass->max_interface_id + 1));
1609 mono_class_setup_vtable (MonoClass *class)
1611 MonoMethod **overrides;
1612 MonoGenericContext *context;
1619 mono_class_setup_methods (class);
1621 if (MONO_CLASS_IS_INTERFACE (class))
1624 mono_loader_lock ();
1626 if (class->vtable) {
1627 mono_loader_unlock ();
1631 if (class->generic_class) {
1632 if (class->generic_class->inst->is_open) {
1633 setup_generic_vtable (class);
1634 mono_loader_unlock ();
1638 context = class->generic_class->context;
1639 type_token = class->generic_class->container_class->type_token;
1641 context = (MonoGenericContext *) class->generic_container;
1642 type_token = class->type_token;
1645 if (class->image->dynamic)
1646 mono_reflection_get_dynamic_overrides (class, &overrides, &onum);
1648 mono_class_get_overrides_full (class->image, type_token, &overrides, &onum, context);
1650 mono_class_setup_vtable_general (class, overrides, onum);
1653 mono_loader_unlock ();
1657 * LOCKING: this is supposed to be called with the loader lock held.
1660 mono_class_setup_vtable_general (MonoClass *class, MonoMethod **overrides, int onum)
1663 MonoMethod **vtable;
1664 int i, max_vtsize = 0, max_iid, cur_slot = 0;
1665 GPtrArray *ifaces, *pifaces = NULL;
1666 GHashTable *override_map = NULL;
1667 gboolean security_enabled = mono_is_security_manager_active ();
1672 if (class->generic_class && class->generic_class->inst->is_open) {
1673 setup_generic_vtable (class);
1677 ifaces = mono_class_get_implemented_interfaces (class);
1679 for (i = 0; i < ifaces->len; i++) {
1680 MonoClass *ic = g_ptr_array_index (ifaces, i);
1681 max_vtsize += ic->method.count;
1683 g_ptr_array_free (ifaces, TRUE);
1686 if (class->parent) {
1687 mono_class_init (class->parent);
1688 mono_class_setup_vtable (class->parent);
1689 max_vtsize += class->parent->vtable_size;
1690 cur_slot = class->parent->vtable_size;
1693 max_vtsize += class->method.count;
1695 vtable = alloca (sizeof (gpointer) * max_vtsize);
1696 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
1698 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
1700 cur_slot = setup_interface_offsets (class, cur_slot);
1701 max_iid = class->max_interface_id;
1703 if (class->parent && class->parent->vtable_size)
1704 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
1706 /* override interface methods */
1707 for (i = 0; i < onum; i++) {
1708 MonoMethod *decl = overrides [i*2];
1709 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
1711 mono_class_setup_methods (decl->klass);
1712 g_assert (decl->slot != -1);
1713 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
1714 vtable [dslot] = overrides [i*2 + 1];
1715 vtable [dslot]->slot = dslot;
1717 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1719 g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
1723 for (k = class; k ; k = k->parent) {
1726 ifaces = mono_class_get_implemented_interfaces (k);
1728 nifaces = ifaces->len;
1729 if (k->generic_class) {
1730 pifaces = mono_class_get_implemented_interfaces (
1731 k->generic_class->container_class);
1732 g_assert (pifaces && (pifaces->len == nifaces));
1735 for (i = 0; i < nifaces; i++) {
1736 MonoClass *pic = NULL;
1739 ic = g_ptr_array_index (ifaces, i);
1741 pic = g_ptr_array_index (pifaces, i);
1742 g_assert (ic->interface_id <= k->max_interface_id);
1743 io = k->interface_offsets [ic->interface_id];
1746 g_assert (io <= max_vtsize);
1749 mono_class_setup_methods (ic);
1750 for (l = 0; l < ic->method.count; l++) {
1751 MonoMethod *im = ic->methods [l];
1753 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1756 for (j = 0; j < class->method.count; ++j) {
1757 MonoMethod *cm = class->methods [j];
1758 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1759 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
1760 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
1762 if (!strcmp(cm->name, im->name) &&
1763 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1765 /* CAS - SecurityAction.InheritanceDemand on interface */
1766 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1767 mono_secman_inheritancedemand_method (cm, im);
1770 g_assert (io + l <= max_vtsize);
1771 vtable [io + l] = cm;
1776 /* already implemented */
1777 if (io >= k->vtable_size)
1781 for (l = 0; l < ic->method.count; l++) {
1782 MonoMethod *im = ic->methods [l];
1785 g_assert (io + l <= max_vtsize);
1787 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1790 for (k1 = class; k1; k1 = k1->parent) {
1791 for (j = 0; j < k1->method.count; ++j) {
1792 MonoMethod *cm = k1->methods [j];
1794 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1795 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
1798 if (!strcmp(cm->name, im->name) &&
1799 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1801 /* CAS - SecurityAction.InheritanceDemand on interface */
1802 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1803 mono_secman_inheritancedemand_method (cm, im);
1806 g_assert (io + l <= max_vtsize);
1807 vtable [io + l] = cm;
1812 g_assert (io + l <= max_vtsize);
1813 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1818 for (l = 0; l < ic->method.count; l++) {
1819 MonoMethod *im = ic->methods [l];
1820 char *qname, *fqname, *cname, *the_cname;
1823 if (vtable [io + l])
1827 the_cname = mono_type_get_name_full (&pic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
1831 cname = (char*)ic->name;
1834 qname = g_strconcat (cname, ".", im->name, NULL);
1835 if (ic->name_space && ic->name_space [0])
1836 fqname = g_strconcat (ic->name_space, ".", cname, ".", im->name, NULL);
1840 for (k1 = class; k1; k1 = k1->parent) {
1841 for (j = 0; j < k1->method.count; ++j) {
1842 MonoMethod *cm = k1->methods [j];
1844 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1847 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
1848 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1850 /* CAS - SecurityAction.InheritanceDemand on interface */
1851 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1852 mono_secman_inheritancedemand_method (cm, im);
1855 g_assert (io + l <= max_vtsize);
1856 vtable [io + l] = cm;
1867 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
1868 for (l = 0; l < ic->method.count; l++) {
1870 MonoMethod *im = ic->methods [l];
1871 if (im->flags & METHOD_ATTRIBUTE_STATIC)
1873 g_assert (io + l <= max_vtsize);
1876 * If one of our parents already implements this interface
1877 * we can inherit the implementation.
1879 if (!(vtable [io + l])) {
1880 MonoClass *parent = class->parent;
1882 if ((ic->interface_id <= parent->max_interface_id) &&
1883 (parent->interface_offsets [ic->interface_id] != -1) &&
1885 vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
1888 if (!(vtable [io + l])) {
1889 for (j = 0; j < onum; ++j) {
1890 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
1891 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
1893 msig = mono_signature_get_desc (mono_method_signature (im), FALSE);
1894 printf ("no implementation for interface method %s::%s(%s) in class %s.%s\n",
1895 mono_type_get_name (&ic->byval_arg), im->name, msig, class->name_space, class->name);
1897 for (j = 0; j < class->method.count; ++j) {
1898 MonoMethod *cm = class->methods [j];
1899 msig = mono_signature_get_desc (mono_method_signature (cm), TRUE);
1901 printf ("METHOD %s(%s)\n", cm->name, msig);
1904 g_assert_not_reached ();
1909 for (l = 0; l < ic->method.count; l++) {
1910 MonoMethod *im = vtable [io + l];
1913 g_assert (io + l <= max_vtsize);
1915 /* FIXME: why do we need this ? */
1917 /* g_assert_not_reached (); */
1923 g_ptr_array_free (ifaces, TRUE);
1926 for (i = 0; i < class->method.count; ++i) {
1929 cm = class->methods [i];
1932 * Non-virtual method have no place in the vtable.
1933 * This also catches static methods (since they are not virtual).
1935 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1939 * If the method is REUSE_SLOT, we must check in the
1940 * base class for a method to override.
1942 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
1944 for (k = class->parent; k ; k = k->parent) {
1946 for (j = 0; j < k->method.count; ++j) {
1947 MonoMethod *m1 = k->methods [j];
1948 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
1950 if (!strcmp(cm->name, m1->name) &&
1951 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (m1))) {
1953 /* CAS - SecurityAction.InheritanceDemand */
1954 if (security_enabled && (m1->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1955 mono_secman_inheritancedemand_method (cm, m1);
1958 slot = k->methods [j]->slot;
1959 g_assert (cm->slot < max_vtsize);
1961 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1962 g_hash_table_insert (override_map, m1, cm);
1974 cm->slot = cur_slot++;
1976 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
1977 vtable [cm->slot] = cm;
1980 /* override non interface methods */
1981 for (i = 0; i < onum; i++) {
1982 MonoMethod *decl = overrides [i*2];
1983 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
1984 g_assert (decl->slot != -1);
1985 vtable [decl->slot] = overrides [i*2 + 1];
1986 overrides [i * 2 + 1]->slot = decl->slot;
1988 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1989 g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
1994 * If a method occupies more than one place in the vtable, and it is
1995 * overriden, then change the other occurances too.
1998 for (i = 0; i < max_vtsize; ++i)
2000 MonoMethod *cm = g_hash_table_lookup (override_map, vtable [i]);
2005 g_hash_table_destroy (override_map);
2008 if (class->generic_class) {
2009 MonoClass *gklass = class->generic_class->container_class;
2011 mono_class_init (gklass);
2013 class->vtable_size = MAX (gklass->vtable_size, cur_slot);
2015 class->vtable_size = cur_slot;
2017 class->vtable = g_malloc0 (sizeof (gpointer) * class->vtable_size);
2018 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
2020 if (mono_print_vtable) {
2023 for (i = 0; i <= max_iid; i++)
2024 if (class->interface_offsets [i] != -1)
2027 printf ("VTable %s (vtable entries = %d, interfaces = %d)\n", mono_type_full_name (&class->byval_arg),
2028 class->vtable_size, icount);
2030 for (i = 0; i < class->vtable_size; ++i) {
2035 printf (" slot assigned: %03d, slot index: %03d %s\n", i, cm->slot,
2036 mono_method_full_name (cm, TRUE));
2042 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
2043 class->name, max_iid);
2045 for (i = 0; i < class->interface_count; i++) {
2046 ic = class->interfaces [i];
2047 printf (" slot offset: %03d, method count: %03d, iid: %03d %s\n",
2048 class->interface_offsets [ic->interface_id],
2049 ic->method.count, ic->interface_id, mono_type_full_name (&ic->byval_arg));
2052 for (k = class->parent; k ; k = k->parent) {
2053 for (i = 0; i < k->interface_count; i++) {
2054 ic = k->interfaces [i];
2055 printf (" slot offset: %03d, method count: %03d, iid: %03d %s\n",
2056 class->interface_offsets [ic->interface_id],
2057 ic->method.count, ic->interface_id, mono_type_full_name (&ic->byval_arg));
2066 * @class: the class to initialize
2068 * compute the instance_size, class_size and other infos that cannot be
2069 * computed at mono_class_get() time. Also compute a generic vtable and
2070 * the method slot numbers. We use this infos later to create a domain
2074 mono_class_init (MonoClass *class)
2077 static MonoMethod *default_ghc = NULL;
2078 static MonoMethod *default_finalize = NULL;
2079 static int finalize_slot = -1;
2080 static int ghc_slot = -1;
2081 MonoCachedClassInfo cached_info;
2082 gboolean has_cached_info;
2089 /*g_print ("Init class %s\n", class->name);*/
2091 /* We do everything inside the lock to prevent races */
2092 mono_loader_lock ();
2094 if (class->inited) {
2095 mono_loader_unlock ();
2096 /* Somebody might have gotten in before us */
2100 if (class->init_pending) {
2101 mono_loader_unlock ();
2102 /* this indicates a cyclic dependency */
2103 g_error ("pending init %s.%s\n", class->name_space, class->name);
2106 class->init_pending = 1;
2108 /* CAS - SecurityAction.InheritanceDemand */
2109 if (mono_is_security_manager_active () && class->parent && (class->parent->flags & TYPE_ATTRIBUTE_HAS_SECURITY)) {
2110 mono_secman_inheritancedemand_class (class, class->parent);
2113 if (mono_debugger_start_class_init_func)
2114 mono_debugger_start_class_init_func (class);
2116 mono_stats.initialized_class_count++;
2118 if (class->generic_class && !class->generic_class->is_dynamic) {
2119 MonoInflatedGenericClass *gclass;
2122 gclass = mono_get_inflated_generic_class (class->generic_class);
2123 gklass = gclass->generic_class.container_class;
2125 mono_stats.generic_class_count++;
2127 class->method = gklass->method;
2128 class->field = gklass->field;
2130 mono_class_init (gklass);
2131 mono_class_setup_methods (gklass);
2132 mono_class_setup_properties (gklass);
2134 if (MONO_CLASS_IS_INTERFACE (class))
2135 class->interface_id = mono_get_unique_iid (class);
2137 g_assert (class->method.count == gklass->method.count);
2138 class->methods = g_new0 (MonoMethod *, class->method.count);
2140 for (i = 0; i < class->method.count; i++) {
2141 MonoMethod *inflated = mono_class_inflate_generic_method (
2142 gklass->methods [i], class, gclass->generic_class.context);
2144 class->methods [i] = mono_get_inflated_method (inflated);
2147 class->property = gklass->property;
2148 class->properties = g_new0 (MonoProperty, class->property.count);
2150 for (i = 0; i < class->property.count; i++) {
2151 MonoProperty *prop = &class->properties [i];
2153 *prop = gklass->properties [i];
2156 prop->get = mono_class_inflate_generic_method (
2157 prop->get, class, gclass->generic_class.context);
2159 prop->set = mono_class_inflate_generic_method (
2160 prop->set, class, gclass->generic_class.context);
2162 prop->parent = class;
2165 g_assert (class->interface_count == gklass->interface_count);
2168 if (class->parent && !class->parent->inited)
2169 mono_class_init (class->parent);
2171 has_cached_info = mono_class_get_cached_class_info (class, &cached_info);
2173 if (!class->generic_class && (!has_cached_info || (has_cached_info && cached_info.has_nested_classes))) {
2174 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
2177 guint32 cols [MONO_NESTED_CLASS_SIZE];
2178 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
2179 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
2180 class->nested_classes = g_list_prepend (class->nested_classes, nclass);
2182 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
2187 * Computes the size used by the fields, and their locations
2189 if (has_cached_info) {
2190 class->instance_size = cached_info.instance_size;
2191 class->class_size = cached_info.class_size;
2192 class->packing_size = cached_info.packing_size;
2193 class->min_align = cached_info.min_align;
2194 class->blittable = cached_info.blittable;
2195 class->has_references = cached_info.has_references;
2196 class->has_static_refs = cached_info.has_static_refs;
2199 if (!class->size_inited)
2200 mono_class_setup_fields (class);
2202 /* initialize method pointers */
2205 MonoMethodSignature *sig;
2206 class->method.count = class->rank > 1? 2: 1;
2207 sig = mono_metadata_signature_alloc (class->image, class->rank);
2208 sig->ret = &mono_defaults.void_class->byval_arg;
2209 sig->pinvoke = TRUE;
2210 for (i = 0; i < class->rank; ++i)
2211 sig->params [i] = &mono_defaults.int32_class->byval_arg;
2213 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
2214 ctor->klass = class;
2215 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
2216 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
2217 ctor->signature = sig;
2218 ctor->name = ".ctor";
2220 class->methods = g_new (MonoMethod*, class->method.count);
2221 class->methods [0] = ctor;
2222 if (class->rank > 1) {
2223 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
2224 sig->ret = &mono_defaults.void_class->byval_arg;
2225 sig->pinvoke = TRUE;
2226 for (i = 0; i < class->rank * 2; ++i)
2227 sig->params [i] = &mono_defaults.int32_class->byval_arg;
2229 ctor = (MonoMethod *) g_new0 (MonoMethodPInvoke, 1);
2230 ctor->klass = class;
2231 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
2232 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
2233 ctor->signature = sig;
2234 ctor->name = ".ctor";
2236 class->methods [1] = ctor;
2240 mono_class_setup_supertypes (class);
2243 if (class == mono_defaults.object_class) {
2244 mono_class_setup_vtable (class);
2245 for (i = 0; i < class->vtable_size; ++i) {
2246 MonoMethod *cm = class->vtable [i];
2248 if (!strcmp (cm->name, "GetHashCode")) {
2254 g_assert (ghc_slot > 0);
2256 default_ghc = class->vtable [ghc_slot];
2260 if (!default_finalize) {
2261 if (class == mono_defaults.object_class) {
2262 mono_class_setup_vtable (class);
2263 for (i = 0; i < class->vtable_size; ++i) {
2264 MonoMethod *cm = class->vtable [i];
2266 if (!strcmp (cm->name, "Finalize")) {
2272 g_assert (finalize_slot > 0);
2274 default_finalize = class->vtable [finalize_slot];
2279 * If possible, avoid the creation of the generic vtable by requesting
2280 * cached info from the runtime.
2282 if (has_cached_info) {
2283 guint32 cur_slot = 0;
2285 class->vtable_size = cached_info.vtable_size;
2286 class->has_finalize = cached_info.has_finalize;
2287 class->ghcimpl = cached_info.ghcimpl;
2288 class->has_cctor = cached_info.has_cctor;
2290 if (class->parent) {
2291 mono_class_init (class->parent);
2292 cur_slot = class->parent->vtable_size;
2295 setup_interface_offsets (class, cur_slot);
2298 mono_class_setup_vtable (class);
2301 if (class->parent) {
2302 MonoMethod *cmethod = class->vtable [ghc_slot];
2303 if (cmethod->is_inflated)
2304 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
2305 if (cmethod == default_ghc) {
2310 /* Object::Finalize should have empty implemenatation */
2311 class->has_finalize = 0;
2312 if (class->parent) {
2313 MonoMethod *cmethod = class->vtable [finalize_slot];
2314 if (cmethod->is_inflated)
2315 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
2316 if (cmethod != default_finalize) {
2317 class->has_finalize = 1;
2321 for (i = 0; i < class->method.count; ++i) {
2322 MonoMethod *method = class->methods [i];
2323 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2324 (strcmp (".cctor", method->name) == 0)) {
2325 class->has_cctor = 1;
2332 class->init_pending = 0;
2334 mono_loader_unlock ();
2336 if (MONO_CLASS_IS_INTERFACE (class)) {
2338 * class->interface_offsets is needed for the castclass/isinst code, so
2339 * we have to setup them for interfaces, too.
2341 setup_interface_offsets (class, 0);
2344 if (mono_debugger_class_init_func)
2345 mono_debugger_class_init_func (class);
2349 * LOCKING: this assumes the loader lock is held
2352 mono_class_setup_mono_type (MonoClass *class)
2354 const char *name = class->name;
2355 const char *nspace = class->name_space;
2357 class->this_arg.byref = 1;
2358 class->this_arg.data.klass = class;
2359 class->this_arg.type = MONO_TYPE_CLASS;
2360 class->byval_arg.data.klass = class;
2361 class->byval_arg.type = MONO_TYPE_CLASS;
2363 if (!strcmp (nspace, "System")) {
2364 if (!strcmp (name, "ValueType")) {
2366 * do not set the valuetype bit for System.ValueType.
2367 * class->valuetype = 1;
2369 class->blittable = TRUE;
2370 } else if (!strcmp (name, "Enum")) {
2372 * do not set the valuetype bit for System.Enum.
2373 * class->valuetype = 1;
2375 class->valuetype = 0;
2376 class->enumtype = 0;
2377 } else if (!strcmp (name, "Object")) {
2378 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
2379 } else if (!strcmp (name, "String")) {
2380 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
2381 } else if (!strcmp (name, "TypedReference")) {
2382 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
2386 if (class->valuetype) {
2387 int t = MONO_TYPE_VALUETYPE;
2388 if (!strcmp (nspace, "System")) {
2391 if (!strcmp (name, "Boolean")) {
2392 t = MONO_TYPE_BOOLEAN;
2393 } else if (!strcmp(name, "Byte")) {
2395 class->blittable = TRUE;
2399 if (!strcmp (name, "Char")) {
2404 if (!strcmp (name, "Double")) {
2406 class->blittable = TRUE;
2410 if (!strcmp (name, "Int32")) {
2412 class->blittable = TRUE;
2413 } else if (!strcmp(name, "Int16")) {
2415 class->blittable = TRUE;
2416 } else if (!strcmp(name, "Int64")) {
2418 class->blittable = TRUE;
2419 } else if (!strcmp(name, "IntPtr")) {
2421 class->blittable = TRUE;
2425 if (!strcmp (name, "Single")) {
2427 class->blittable = TRUE;
2428 } else if (!strcmp(name, "SByte")) {
2430 class->blittable = TRUE;
2434 if (!strcmp (name, "UInt32")) {
2436 class->blittable = TRUE;
2437 } else if (!strcmp(name, "UInt16")) {
2439 class->blittable = TRUE;
2440 } else if (!strcmp(name, "UInt64")) {
2442 class->blittable = TRUE;
2443 } else if (!strcmp(name, "UIntPtr")) {
2445 class->blittable = TRUE;
2449 if (!strcmp (name, "TypedReference")) {
2450 t = MONO_TYPE_TYPEDBYREF;
2451 class->blittable = TRUE;
2455 if (!strcmp (name, "Void")) {
2463 class->this_arg.type = class->byval_arg.type = t;
2466 if (MONO_CLASS_IS_INTERFACE (class))
2467 class->interface_id = mono_get_unique_iid (class);
2472 * LOCKING: this assumes the loader lock is held
2475 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
2477 gboolean system_namespace;
2479 system_namespace = !strcmp (class->name_space, "System");
2481 /* if root of the hierarchy */
2482 if (system_namespace && !strcmp (class->name, "Object")) {
2483 class->parent = NULL;
2484 class->instance_size = sizeof (MonoObject);
2487 if (!strcmp (class->name, "<Module>")) {
2488 class->parent = NULL;
2489 class->instance_size = 0;
2493 if (!MONO_CLASS_IS_INTERFACE (class)) {
2494 class->parent = parent;
2497 g_assert_not_reached (); /* FIXME */
2499 if (parent->generic_class && !parent->name) {
2501 * If the parent is a generic instance, we may get
2502 * called before it is fully initialized, especially
2503 * before it has its name.
2508 class->marshalbyref = parent->marshalbyref;
2509 class->contextbound = parent->contextbound;
2510 class->delegate = parent->delegate;
2512 if (system_namespace) {
2513 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
2514 class->marshalbyref = 1;
2516 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
2517 class->contextbound = 1;
2519 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
2520 class->delegate = 1;
2523 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
2524 (strcmp (class->parent->name_space, "System") == 0)))
2525 class->valuetype = 1;
2526 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
2527 class->valuetype = class->enumtype = 1;
2529 /*class->enumtype = class->parent->enumtype; */
2530 mono_class_setup_supertypes (class);
2532 class->parent = NULL;
2538 * LOCKING: this assumes the loader lock is held
2541 mono_class_setup_supertypes (MonoClass *class)
2545 if (class->supertypes)
2548 if (class->parent && !class->parent->supertypes)
2549 mono_class_setup_supertypes (class->parent);
2551 class->idepth = class->parent->idepth + 1;
2555 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
2556 class->supertypes = g_new0 (MonoClass *, ms);
2558 if (class->parent) {
2559 class->supertypes [class->idepth - 1] = class;
2560 memcpy (class->supertypes, class->parent->supertypes, class->parent->idepth * sizeof (gpointer));
2562 class->supertypes [0] = class;
2567 mono_get_shared_generic_inst (MonoGenericContainer *container)
2569 MonoGenericInst *nginst;
2572 nginst = g_new0 (MonoGenericInst, 1);
2573 nginst->type_argc = container->type_argc;
2574 nginst->type_argv = g_new0 (MonoType *, nginst->type_argc);
2575 nginst->is_reference = 1;
2576 nginst->is_open = 1;
2578 for (i = 0; i < nginst->type_argc; i++) {
2579 MonoType *t = g_new0 (MonoType, 1);
2581 t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2582 t->data.generic_param = &container->type_params [i];
2584 nginst->type_argv [i] = t;
2587 return mono_metadata_lookup_generic_inst (nginst);
2591 * In preparation for implementing shared code.
2594 mono_get_shared_generic_class (MonoGenericContainer *container, gboolean is_dynamic)
2596 MonoInflatedGenericClass *igclass;
2597 MonoGenericClass *gclass;
2600 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
2601 igclass = &dgclass->generic_class;
2602 gclass = &igclass->generic_class;
2603 gclass->is_inflated = 1;
2604 gclass->is_dynamic = 1;
2606 igclass = g_new0 (MonoInflatedGenericClass, 1);
2607 gclass = &igclass->generic_class;
2608 gclass->is_inflated = 1;
2611 gclass->context = &container->context;
2612 gclass->container_class = container->klass;
2613 gclass->inst = mono_get_shared_generic_inst (container);
2616 MonoGenericClass *cached = mono_metadata_lookup_generic_class (gclass);
2624 igclass->klass = container->klass;
2630 * @image: context where the image is created
2631 * @type_token: typedef token
2634 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
2636 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
2637 MonoClass *class, *parent = NULL;
2638 guint32 cols [MONO_TYPEDEF_SIZE];
2639 guint32 cols_next [MONO_TYPEDEF_SIZE];
2640 guint tidx = mono_metadata_token_index (type_token);
2641 MonoGenericContext *context = NULL;
2642 const char *name, *nspace;
2644 MonoClass **interfaces;
2645 guint32 field_last, method_last;
2646 guint32 nesting_tokeen;
2648 mono_loader_lock ();
2650 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
2651 mono_loader_unlock ();
2655 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
2657 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
2659 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2660 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2662 class = g_malloc0 (sizeof (MonoClass));
2665 class->name_space = nspace;
2667 class->image = image;
2668 class->type_token = type_token;
2669 class->flags = cols [MONO_TYPEDEF_FLAGS];
2671 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
2674 * Check whether we're a generic type definition.
2676 class->generic_container = mono_metadata_load_generic_params (image, class->type_token, NULL);
2677 if (class->generic_container) {
2678 class->generic_container->klass = class;
2679 context = &class->generic_container->context;
2681 context->gclass = mono_get_shared_generic_class (context->container, FALSE);
2684 if (cols [MONO_TYPEDEF_EXTENDS]) {
2685 parent = mono_class_get_full (
2686 image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]), context);
2689 /* do this early so it's available for interfaces in setup_mono_type () */
2690 if ((nesting_tokeen = mono_metadata_nested_in_typedef (image, type_token)))
2691 class->nested_in = mono_class_create_from_typedef (image, nesting_tokeen);
2693 mono_class_setup_parent (class, parent);
2695 mono_class_setup_mono_type (class);
2697 if (!class->enumtype) {
2698 mono_metadata_interfaces_from_typedef_full (image, type_token, &interfaces, &icount, context);
2700 class->interfaces = interfaces;
2701 class->interface_count = icount;
2704 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
2706 /* fixme: maybe we must set this on windows
2707 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
2711 class->cast_class = class->element_class = class;
2713 /*g_print ("Load class %s\n", name);*/
2716 * Compute the field and method lists
2718 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
2719 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
2721 if (tt->rows > tidx){
2722 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
2723 field_last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
2724 method_last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
2726 field_last = image->tables [MONO_TABLE_FIELD].rows;
2727 method_last = image->tables [MONO_TABLE_METHOD].rows;
2730 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
2731 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
2732 class->field.count = field_last - class->field.first;
2734 class->field.count = 0;
2736 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
2737 class->method.count = method_last - class->method.first;
2739 class->method.count = 0;
2741 /* reserve space to store vector pointer in arrays */
2742 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
2743 class->instance_size += 2 * sizeof (gpointer);
2744 g_assert (class->field.count == 0);
2747 if (class->enumtype) {
2748 class->enum_basetype = mono_class_find_enum_basetype (class);
2749 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
2753 * If we're a generic type definition, load the constraints.
2754 * We must do this after the class has been constructed to make certain recursive scenarios
2757 if (class->generic_container)
2758 mono_metadata_load_generic_param_constraints (
2759 image, type_token, class->generic_container);
2761 mono_loader_unlock ();
2766 /** is klass Nullable<T>? */
2768 mono_class_is_nullable (MonoClass *klass)
2770 return klass->generic_class != NULL &&
2771 klass->generic_class->container_class == mono_defaults.generic_nullable_class;
2775 /** if klass is T? return T */
2777 mono_class_get_nullable_param (MonoClass *klass)
2779 g_assert (mono_class_is_nullable (klass));
2780 return mono_class_from_mono_type (klass->generic_class->inst->type_argv [0]);
2784 * Create the `MonoClass' for an instantiation of a generic type.
2785 * We only do this if we actually need it.
2788 mono_class_create_generic (MonoInflatedGenericClass *gclass)
2790 MonoClass *klass, *gklass;
2793 if (gclass->is_initialized)
2795 gclass->is_initialized = TRUE;
2798 gclass->klass = g_malloc0 (sizeof (MonoClass));
2799 klass = gclass->klass;
2801 gklass = gclass->generic_class.container_class;
2803 klass->nested_in = gklass->nested_in;
2805 klass->name = gklass->name;
2806 klass->name_space = gklass->name_space;
2807 klass->image = gklass->image;
2808 klass->flags = gklass->flags;
2809 klass->type_token = gklass->type_token;
2811 klass->generic_class = &gclass->generic_class;
2813 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
2814 klass->this_arg.data.generic_class = klass->byval_arg.data.generic_class = &gclass->generic_class;
2815 klass->this_arg.byref = TRUE;
2817 klass->cast_class = klass->element_class = klass;
2819 if (mono_class_is_nullable (klass))
2820 klass->cast_class = klass->element_class = mono_class_get_nullable_param (klass);
2822 if (gclass->generic_class.is_dynamic) {
2823 klass->instance_size = gklass->instance_size;
2824 klass->class_size = gklass->class_size;
2825 klass->size_inited = 1;
2828 klass->valuetype = gklass->valuetype;
2830 mono_class_setup_supertypes (klass);
2833 klass->interface_count = gklass->interface_count;
2834 klass->interfaces = g_new0 (MonoClass *, klass->interface_count);
2835 for (i = 0; i < klass->interface_count; i++) {
2836 MonoType *it = &gklass->interfaces [i]->byval_arg;
2837 MonoType *inflated = mono_class_inflate_generic_type (
2838 it, gclass->generic_class.context);
2839 klass->interfaces [i] = mono_class_from_mono_type (inflated);
2842 i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, 1);
2845 guint32 cols [MONO_NESTED_CLASS_SIZE];
2846 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
2847 nclass = mono_class_create_from_typedef (klass->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
2848 klass->nested_classes = g_list_prepend (klass->nested_classes, nclass);
2850 i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, i + 1);
2853 if (gclass->generic_class.is_dynamic) {
2854 MonoDynamicGenericClass *dgclass = (MonoDynamicGenericClass *) gclass;
2856 if (dgclass->parent)
2857 klass->parent = mono_class_from_mono_type (dgclass->parent);
2858 } else if (gklass->parent) {
2859 MonoType *inflated = mono_class_inflate_generic_type (
2860 &gklass->parent->byval_arg, gclass->generic_class.context);
2862 klass->parent = mono_class_from_mono_type (inflated);
2866 mono_class_setup_parent (klass, klass->parent);
2868 if (MONO_CLASS_IS_INTERFACE (klass))
2869 setup_interface_offsets (klass, 0);
2873 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
2875 MonoClass *klass, **ptr;
2879 return param->pklass;
2881 klass = param->pklass = g_new0 (MonoClass, 1);
2883 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
2887 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
2888 klass->parent = param->constraints [0];
2890 } else if (param->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT)
2891 klass->parent = mono_class_from_name (mono_defaults.corlib, "System", "ValueType");
2893 klass->parent = mono_defaults.object_class;
2895 if (count - pos > 0) {
2896 klass->interface_count = count - pos;
2897 klass->interfaces = g_new0 (MonoClass *, count - pos);
2898 for (i = pos; i < count; i++)
2899 klass->interfaces [i - pos] = param->constraints [i];
2903 klass->name = param->name;
2905 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
2907 klass->name_space = "";
2910 klass->image = image;
2911 else if (is_mvar && param->method && param->method->klass)
2912 klass->image = param->method->klass->image;
2913 else if (param->owner && param->owner->klass)
2914 klass->image = param->owner->klass->image;
2916 klass->image = mono_defaults.corlib;
2918 klass->inited = TRUE;
2919 klass->cast_class = klass->element_class = klass;
2920 klass->enum_basetype = &klass->element_class->byval_arg;
2921 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
2923 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2924 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
2925 klass->this_arg.byref = TRUE;
2927 mono_class_setup_supertypes (klass);
2933 mono_ptr_class_get (MonoType *type)
2936 MonoClass *el_class;
2937 static GHashTable *ptr_hash = NULL;
2939 mono_loader_lock ();
2942 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2943 el_class = mono_class_from_mono_type (type);
2944 if ((result = g_hash_table_lookup (ptr_hash, el_class))) {
2945 mono_loader_unlock ();
2948 result = g_new0 (MonoClass, 1);
2950 result->parent = NULL; /* no parent for PTR types */
2951 result->name_space = el_class->name_space;
2952 result->name = g_strdup_printf ("%s*", el_class->name);
2953 result->image = el_class->image;
2954 result->inited = TRUE;
2955 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
2956 /* Can pointers get boxed? */
2957 result->instance_size = sizeof (gpointer);
2958 result->cast_class = result->element_class = el_class;
2959 result->enum_basetype = &result->element_class->byval_arg;
2960 result->blittable = TRUE;
2962 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
2963 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
2964 result->this_arg.byref = TRUE;
2966 mono_class_setup_supertypes (result);
2968 g_hash_table_insert (ptr_hash, el_class, result);
2970 mono_loader_unlock ();
2976 mono_fnptr_class_get (MonoMethodSignature *sig)
2979 static GHashTable *ptr_hash = NULL;
2981 mono_loader_lock ();
2984 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
2986 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
2987 mono_loader_unlock ();
2990 result = g_new0 (MonoClass, 1);
2992 result->parent = NULL; /* no parent for PTR types */
2993 result->name_space = "System";
2994 result->name = "MonoFNPtrFakeClass";
2995 result->image = NULL; /* need to fix... */
2996 result->inited = TRUE;
2997 result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
2998 /* Can pointers get boxed? */
2999 result->instance_size = sizeof (gpointer);
3000 result->cast_class = result->element_class = result;
3001 result->blittable = TRUE;
3003 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
3004 result->this_arg.data.method = result->byval_arg.data.method = sig;
3005 result->this_arg.byref = TRUE;
3006 result->enum_basetype = &result->element_class->byval_arg;
3007 result->blittable = TRUE;
3009 mono_class_setup_supertypes (result);
3011 g_hash_table_insert (ptr_hash, sig, result);
3013 mono_loader_unlock ();
3019 mono_class_from_mono_type (MonoType *type)
3021 switch (type->type) {
3022 case MONO_TYPE_OBJECT:
3023 return type->data.klass? type->data.klass: mono_defaults.object_class;
3024 case MONO_TYPE_VOID:
3025 return type->data.klass? type->data.klass: mono_defaults.void_class;
3026 case MONO_TYPE_BOOLEAN:
3027 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
3028 case MONO_TYPE_CHAR:
3029 return type->data.klass? type->data.klass: mono_defaults.char_class;
3031 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
3033 return type->data.klass? type->data.klass: mono_defaults.byte_class;
3035 return type->data.klass? type->data.klass: mono_defaults.int16_class;
3037 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
3039 return type->data.klass? type->data.klass: mono_defaults.int32_class;
3041 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
3043 return type->data.klass? type->data.klass: mono_defaults.int_class;
3045 return type->data.klass? type->data.klass: mono_defaults.uint_class;
3047 return type->data.klass? type->data.klass: mono_defaults.int64_class;
3049 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
3051 return type->data.klass? type->data.klass: mono_defaults.single_class;
3053 return type->data.klass? type->data.klass: mono_defaults.double_class;
3054 case MONO_TYPE_STRING:
3055 return type->data.klass? type->data.klass: mono_defaults.string_class;
3056 case MONO_TYPE_TYPEDBYREF:
3057 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
3058 case MONO_TYPE_ARRAY:
3059 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
3061 return mono_ptr_class_get (type->data.type);
3062 case MONO_TYPE_FNPTR:
3063 return mono_fnptr_class_get (type->data.method);
3064 case MONO_TYPE_SZARRAY:
3065 return mono_array_class_get (type->data.klass, 1);
3066 case MONO_TYPE_CLASS:
3067 case MONO_TYPE_VALUETYPE:
3068 return type->data.klass;
3069 case MONO_TYPE_GENERICINST: {
3070 MonoInflatedGenericClass *gclass;
3071 gclass = mono_get_inflated_generic_class (type->data.generic_class);
3072 g_assert (gclass->klass);
3073 return gclass->klass;
3076 return mono_class_from_generic_parameter (type->data.generic_param, NULL, FALSE);
3077 case MONO_TYPE_MVAR:
3078 return mono_class_from_generic_parameter (type->data.generic_param, NULL, TRUE);
3080 g_warning ("implement me 0x%02x\n", type->type);
3081 g_assert_not_reached ();
3088 * @image: context where the image is created
3089 * @type_spec: typespec token
3092 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec)
3094 return mono_class_from_mono_type (mono_type_create_from_typespec (image, type_spec));
3098 * mono_bounded_array_class_get:
3099 * @element_class: element class
3100 * @rank: the dimension of the array class
3101 * @bounded: whenever the array has non-zero bounds
3103 * Returns: a class object describing the array with element type @element_type and
3107 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
3111 MonoClass *parent = NULL;
3112 GSList *list, *rootlist;
3115 gboolean corlib_type = FALSE;
3117 g_assert (rank <= 255);
3120 /* bounded only matters for one-dimensional arrays */
3123 image = eclass->image;
3125 mono_loader_lock ();
3127 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
3128 for (; list; list = list->next) {
3130 if ((class->rank == rank) && (class->byval_arg.type == (bounded ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
3131 mono_loader_unlock ();
3137 /* for the building corlib use System.Array from it */
3138 if (image->assembly && image->assembly->dynamic && image->assembly_name && strcmp (image->assembly_name, "mscorlib") == 0) {
3139 parent = mono_class_from_name (image, "System", "Array");
3141 } else if (mono_defaults.generic_array_class) {
3142 MonoType *inflated, **args;
3144 args = g_new0 (MonoType *, 1);
3145 args [0] = &eclass->byval_arg;
3147 inflated = mono_class_bind_generic_parameters (
3148 &mono_defaults.generic_array_class->byval_arg, 1, args);
3149 parent = mono_class_from_mono_type (inflated);
3151 if (!parent->inited)
3152 mono_class_init (parent);
3154 parent = mono_defaults.array_class;
3155 if (!parent->inited)
3156 mono_class_init (parent);
3159 class = g_malloc0 (sizeof (MonoClass));
3161 class->image = image;
3162 class->name_space = eclass->name_space;
3163 nsize = strlen (eclass->name);
3164 name = g_malloc (nsize + 2 + rank);
3165 memcpy (name, eclass->name, nsize);
3168 memset (name + nsize + 1, ',', rank - 1);
3169 name [nsize + rank] = ']';
3170 name [nsize + rank + 1] = 0;
3172 class->type_token = 0;
3173 /* all arrays are marked serializable and sealed, bug #42779 */
3174 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
3175 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
3176 class->parent = parent;
3177 class->instance_size = mono_class_instance_size (class->parent);
3178 class->class_size = 0;
3179 mono_class_setup_supertypes (class);
3180 if (eclass->generic_class)
3181 mono_class_init (eclass);
3182 if (!eclass->size_inited)
3183 mono_class_setup_fields (eclass);
3184 class->has_references = MONO_TYPE_IS_REFERENCE (&eclass->byval_arg) || eclass->has_references? TRUE: FALSE;
3188 if (eclass->enumtype)
3189 class->cast_class = eclass->element_class;
3191 class->cast_class = eclass;
3193 class->element_class = eclass;
3195 if ((rank > 1) || bounded) {
3196 MonoArrayType *at = g_new0 (MonoArrayType, 1);
3197 class->byval_arg.type = MONO_TYPE_ARRAY;
3198 class->byval_arg.data.array = at;
3199 at->eklass = eclass;
3201 /* FIXME: complete.... */
3203 class->byval_arg.type = MONO_TYPE_SZARRAY;
3204 class->byval_arg.data.klass = eclass;
3206 class->this_arg = class->byval_arg;
3207 class->this_arg.byref = 1;
3212 class->generic_container = eclass->generic_container;
3214 list = g_slist_append (rootlist, class);
3215 g_hash_table_insert (image->array_cache, eclass, list);
3217 mono_loader_unlock ();
3223 * mono_array_class_get:
3224 * @element_class: element class
3225 * @rank: the dimension of the array class
3227 * Returns: a class object describing the array with element type @element_type and
3231 mono_array_class_get (MonoClass *eclass, guint32 rank)
3233 return mono_bounded_array_class_get (eclass, rank, FALSE);
3237 * mono_class_instance_size:
3240 * Returns: the size of an object instance
3243 mono_class_instance_size (MonoClass *klass)
3245 if (!klass->size_inited)
3246 mono_class_init (klass);
3248 return klass->instance_size;
3252 * mono_class_min_align:
3255 * Returns: minimm alignment requirements
3258 mono_class_min_align (MonoClass *klass)
3260 if (!klass->size_inited)
3261 mono_class_init (klass);
3263 return klass->min_align;
3267 * mono_class_value_size:
3270 * This function is used for value types, and return the
3271 * space and the alignment to store that kind of value object.
3273 * Returns: the size of a value of kind @klass
3276 mono_class_value_size (MonoClass *klass, guint32 *align)
3280 /* fixme: check disable, because we still have external revereces to
3281 * mscorlib and Dummy Objects
3283 /*g_assert (klass->valuetype);*/
3285 size = mono_class_instance_size (klass) - sizeof (MonoObject);
3288 *align = klass->min_align;
3294 * mono_class_data_size:
3297 * Returns: the size of the static class data
3300 mono_class_data_size (MonoClass *klass)
3303 mono_class_init (klass);
3305 return klass->class_size;
3309 * Auxiliary routine to mono_class_get_field
3311 * Takes a field index instead of a field token.
3313 static MonoClassField *
3314 mono_class_get_field_idx (MonoClass *class, int idx)
3316 mono_class_setup_fields_locking (class);
3319 if (class->field.count) {
3320 if ((idx >= class->field.first) && (idx < class->field.first + class->field.count)){
3321 return &class->fields [idx - class->field.first];
3324 class = class->parent;
3330 * mono_class_get_field:
3331 * @class: the class to lookup the field.
3332 * @field_token: the field token
3334 * Returns: A MonoClassField representing the type and offset of
3335 * the field, or a NULL value if the field does not belong to this
3339 mono_class_get_field (MonoClass *class, guint32 field_token)
3341 int idx = mono_metadata_token_index (field_token);
3343 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
3345 return mono_class_get_field_idx (class, idx - 1);
3349 * mono_class_get_field_from_name:
3350 * @klass: the class to lookup the field.
3351 * @name: the field name
3353 * Search the class @klass and it's parents for a field with the name @name.
3355 * Returns: the MonoClassField pointer of the named field or NULL
3358 mono_class_get_field_from_name (MonoClass *klass, const char *name)
3362 mono_class_setup_fields_locking (klass);
3364 for (i = 0; i < klass->field.count; ++i) {
3365 if (strcmp (name, klass->fields [i].name) == 0)
3366 return &klass->fields [i];
3368 klass = klass->parent;
3374 * mono_class_get_field_token:
3375 * @field: the field we need the token of
3377 * Get the token of a field. Note that the tokesn is only valid for the image
3378 * the field was loaded from. Don't use this function for fields in dynamic types.
3380 * Returns: the token representing the field in the image it was loaded from.
3383 mono_class_get_field_token (MonoClassField *field)
3385 MonoClass *klass = field->parent;
3388 mono_class_setup_fields_locking (klass);
3390 for (i = 0; i < klass->field.count; ++i) {
3391 if (&klass->fields [i] == field)
3392 return mono_metadata_make_token (MONO_TABLE_FIELD, klass->field.first + i + 1);
3394 klass = klass->parent;
3397 g_assert_not_reached ();
3402 mono_class_get_event_token (MonoEvent *event)
3404 MonoClass *klass = event->parent;
3408 for (i = 0; i < klass->event.count; ++i) {
3409 if (&klass->events [i] == event)
3410 return mono_metadata_make_token (MONO_TABLE_EVENT, klass->event.first + i + 1);
3412 klass = klass->parent;
3415 g_assert_not_reached ();
3420 mono_vtable_get_static_field_data (MonoVTable *vt)
3426 mono_class_get_property_from_name (MonoClass *klass, const char *name)
3430 gpointer iter = NULL;
3431 while ((p = mono_class_get_properties (klass, &iter))) {
3432 if (! strcmp (name, p->name))
3435 klass = klass->parent;
3441 mono_class_get_property_token (MonoProperty *prop)
3443 MonoClass *klass = prop->parent;
3447 gpointer iter = NULL;
3448 while ((p = mono_class_get_properties (klass, &iter))) {
3449 if (&klass->properties [i] == prop)
3450 return mono_metadata_make_token (MONO_TABLE_PROPERTY, klass->property.first + i + 1);
3454 klass = klass->parent;
3457 g_assert_not_reached ();
3462 mono_class_name_from_token (MonoImage *image, guint32 type_token)
3464 const char *name, *nspace;
3466 return g_strdup_printf ("DynamicType 0x%08x", type_token);
3468 switch (type_token & 0xff000000){
3469 case MONO_TOKEN_TYPE_DEF: {
3470 guint32 cols [MONO_TYPEDEF_SIZE];
3471 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
3472 guint tidx = mono_metadata_token_index (type_token);
3474 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
3475 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3476 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3477 if (strlen (nspace) == 0)
3478 return g_strdup_printf ("%s", name);
3480 return g_strdup_printf ("%s.%s", nspace, name);
3483 case MONO_TOKEN_TYPE_REF: {
3484 guint32 cols [MONO_TYPEREF_SIZE];
3485 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
3487 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
3488 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
3489 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
3490 if (strlen (nspace) == 0)
3491 return g_strdup_printf ("%s", name);
3493 return g_strdup_printf ("%s.%s", nspace, name);
3496 case MONO_TOKEN_TYPE_SPEC:
3497 return g_strdup_printf ("Typespec 0x%08x", type_token);
3499 g_assert_not_reached ();
3506 mono_assembly_name_from_token (MonoImage *image, guint32 type_token)
3509 return g_strdup_printf ("DynamicAssembly %s", image->name);
3511 switch (type_token & 0xff000000){
3512 case MONO_TOKEN_TYPE_DEF:
3513 return mono_stringify_assembly_name (&image->assembly->aname);
3515 case MONO_TOKEN_TYPE_REF: {
3516 MonoAssemblyName aname;
3517 guint32 cols [MONO_TYPEREF_SIZE];
3518 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
3521 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
3523 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
3524 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
3525 case MONO_RESOLTION_SCOPE_MODULE:
3527 return g_strdup ("");
3528 case MONO_RESOLTION_SCOPE_MODULEREF:
3530 return g_strdup ("");
3531 case MONO_RESOLTION_SCOPE_TYPEREF:
3533 return g_strdup ("");
3534 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
3535 mono_assembly_get_assemblyref (image, idx - 1, &aname);
3536 return mono_stringify_assembly_name (&aname);
3538 g_assert_not_reached ();
3542 case MONO_TOKEN_TYPE_SPEC:
3544 return g_strdup ("");
3546 g_assert_not_reached ();
3554 * @image: the image where the class resides
3555 * @type_token: the token for the class
3557 * Returns: the MonoClass that represents @type_token in @image
3560 mono_class_get (MonoImage *image, guint32 type_token)
3562 MonoClass *class = NULL;
3565 return mono_lookup_dynamic_token (image, type_token);
3567 switch (type_token & 0xff000000){
3568 case MONO_TOKEN_TYPE_DEF:
3569 class = mono_class_create_from_typedef (image, type_token);
3571 case MONO_TOKEN_TYPE_REF:
3572 class = mono_class_from_typeref (image, type_token);
3574 case MONO_TOKEN_TYPE_SPEC:
3575 class = mono_class_create_from_typespec (image, type_token);
3578 g_warning ("unknown token type %x", type_token & 0xff000000);
3579 g_assert_not_reached ();
3583 char *name = mono_class_name_from_token (image, type_token);
3584 char *assembly = mono_assembly_name_from_token (image, type_token);
3585 mono_loader_set_error_type_load (name, assembly);
3592 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3594 MonoClass *class = mono_class_get (image, type_token);
3595 if (class && context && (context->gclass || context->gmethod)) {
3596 MonoType *inflated = inflate_generic_type (&class->byval_arg, context);
3598 class = mono_class_from_mono_type (inflated);
3609 find_nocase (gpointer key, gpointer value, gpointer user_data)
3611 char *name = (char*)key;
3612 FindUserData *data = (FindUserData*)user_data;
3614 if (!data->value && (g_strcasecmp (name, (char*)data->key) == 0))
3615 data->value = value;
3619 * mono_class_from_name_case:
3620 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3621 * @name_space: the type namespace
3622 * @name: the type short name.
3624 * Obtains a MonoClass with a given namespace and a given name which
3625 * is located in the given MonoImage. The namespace and name
3626 * lookups are case insensitive.
3628 * You can also pass @NULL to the image, and that will lookup for
3629 * a type with the given namespace and name in all of the loaded
3630 * assemblies: notice that since there might be a name clash in this
3631 * case, passing @NULL is not encouraged if you need a precise type.
3635 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
3637 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
3638 guint32 cols [MONO_TYPEDEF_SIZE];
3643 if (image->dynamic) {
3645 FindUserData user_data;
3647 mono_loader_lock ();
3649 user_data.key = name_space;
3650 user_data.value = NULL;
3651 g_hash_table_foreach (image->name_cache, find_nocase, &user_data);
3653 if (user_data.value) {
3654 GHashTable *nspace_table = (GHashTable*)user_data.value;
3656 user_data.key = name;
3657 user_data.value = NULL;
3659 g_hash_table_foreach (nspace_table, find_nocase, &user_data);
3661 if (user_data.value)
3662 token = GPOINTER_TO_UINT (user_data.value);
3665 mono_loader_unlock ();
3668 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | token);
3674 /* add a cache if needed */
3675 for (i = 1; i <= t->rows; ++i) {
3676 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
3677 /* nested types are accessed from the nesting name */
3678 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
3679 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
3681 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3682 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3683 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
3684 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
3690 return_nested_in (MonoClass *class, char *nested) {
3692 char *s = strchr (nested, '/');
3699 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
3701 if (strcmp (found->name, nested) == 0) {
3703 return return_nested_in (found, s);
3712 * mono_class_from_name:
3713 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3714 * @name_space: the type namespace
3715 * @name: the type short name.
3717 * Obtains a MonoClass with a given namespace and a given name which
3718 * is located in the given MonoImage.
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 (MonoImage *image, const char* name_space, const char *name)
3729 GHashTable *nspace_table;
3730 MonoImage *loaded_image;
3736 if ((nested = strchr (name, '/'))) {
3737 int pos = nested - name;
3738 int len = strlen (name);
3741 memcpy (buf, name, len + 1);
3743 nested = buf + pos + 1;
3747 mono_loader_lock ();
3749 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
3752 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
3754 mono_loader_unlock ();
3759 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
3760 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
3761 guint32 cols [MONO_EXP_TYPE_SIZE];
3764 idx = mono_metadata_token_index (token);
3766 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
3768 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
3769 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
3770 loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
3773 class = mono_class_from_name (loaded_image, name_space, name);
3775 return return_nested_in (class, nested);
3778 g_error ("not yet implemented");
3782 token = MONO_TOKEN_TYPE_DEF | token;
3784 class = mono_class_get (image, token);
3786 return return_nested_in (class, nested);
3791 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
3792 gboolean check_interfaces)
3795 g_assert (klassc->idepth > 0);
3796 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
3797 if ((klassc->interface_id <= klass->max_interface_id) &&
3798 (klass->interface_offsets [klassc->interface_id] >= 0))
3800 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
3803 for (i = 0; i < klass->interface_count; i ++) {
3804 MonoClass *ic = klass->interfaces [i];
3809 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
3814 * MS.NET thinks interfaces are a subclass of Object, so we think it as
3817 if (klassc == mono_defaults.object_class)
3820 if (klass->generic_class && klass->generic_class->is_dynamic) {
3821 MonoDynamicGenericClass *dgclass = (MonoDynamicGenericClass *) klass->generic_class;
3823 if (!dgclass->parent)
3826 if (mono_metadata_type_equal (dgclass->parent, &klassc->byval_arg))
3828 klass = mono_class_from_mono_type (dgclass->parent);
3836 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
3839 mono_class_init (klass);
3841 if (!oklass->inited)
3842 mono_class_init (oklass);
3844 if (MONO_CLASS_IS_INTERFACE (klass)) {
3845 if ((oklass->byval_arg.type == MONO_TYPE_VAR) || (oklass->byval_arg.type == MONO_TYPE_MVAR))
3848 /* interface_offsets might not be set for dynamic classes */
3849 if (oklass->reflection_info && !oklass->interface_offsets)
3851 * oklass might be a generic type parameter but they have
3852 * interface_offsets set.
3854 return mono_reflection_call_is_assignable_to (oklass, klass);
3856 if ((klass->interface_id <= oklass->max_interface_id) &&
3857 (oklass->interface_offsets [klass->interface_id] != -1))
3859 } else if (klass->rank) {
3860 MonoClass *eclass, *eoclass;
3862 if (oklass->rank != klass->rank)
3865 /* vectors vs. one dimensional arrays */
3866 if (oklass->byval_arg.type != klass->byval_arg.type)
3869 eclass = klass->cast_class;
3870 eoclass = oklass->cast_class;
3873 * a is b does not imply a[] is b[] when a is a valuetype, and
3874 * b is a reference type.
3877 if (eoclass->valuetype) {
3878 if ((eclass == mono_defaults.enum_class) ||
3879 (eclass == mono_defaults.enum_class->parent) ||
3880 (eclass == mono_defaults.object_class))
3884 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
3885 } else if (mono_class_is_nullable (klass))
3886 return (mono_class_is_assignable_from (klass->cast_class, oklass));
3887 else if (klass == mono_defaults.object_class)
3890 return mono_class_has_parent (oklass, klass);
3894 * mono_class_get_cctor:
3896 * Returns the static constructor of @klass if it exists, NULL otherwise.
3899 mono_class_get_cctor (MonoClass *klass)
3901 MonoCachedClassInfo cached_info;
3903 if (!klass->has_cctor)
3906 if (mono_class_get_cached_class_info (klass, &cached_info))
3907 return mono_get_method (klass->image, cached_info.cctor_token, klass);
3909 return mono_class_get_method_from_name_flags (klass, ".cctor", -1, METHOD_ATTRIBUTE_SPECIAL_NAME);
3913 * mono_class_get_finalizer:
3915 * Returns the finalizer method of @klass if it exists, NULL otherwise.
3918 mono_class_get_finalizer (MonoClass *klass)
3920 MonoCachedClassInfo cached_info;
3921 static int finalize_slot = -1;
3923 if (finalize_slot < 0) {
3925 MonoClass* obj_class = mono_get_object_class ();
3926 mono_class_setup_vtable (obj_class);
3927 for (i = 0; i < obj_class->vtable_size; ++i) {
3928 MonoMethod *cm = obj_class->vtable [i];
3930 if (!strcmp (mono_method_get_name (cm), "Finalize")) {
3937 if (!klass->has_finalize)
3940 if (mono_class_get_cached_class_info (klass, &cached_info))
3941 return mono_get_method (cached_info.finalize_image, cached_info.finalize_token, NULL);
3943 mono_class_setup_vtable (klass);
3944 return klass->vtable [finalize_slot];
3949 * mono_class_needs_cctor_run:
3951 * Determines whenever the class has a static constructor and whenever it
3952 * needs to be called when executing CALLER.
3955 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
3959 method = mono_class_get_cctor (klass);
3961 return (method == caller) ? FALSE : TRUE;
3967 * mono_class_array_element_size:
3970 * Returns: the number of bytes an element of type @klass
3971 * uses when stored into an array.
3974 mono_class_array_element_size (MonoClass *klass)
3976 MonoType *type = &klass->byval_arg;
3979 switch (type->type) {
3982 case MONO_TYPE_BOOLEAN:
3986 case MONO_TYPE_CHAR:
3995 case MONO_TYPE_CLASS:
3996 case MONO_TYPE_STRING:
3997 case MONO_TYPE_OBJECT:
3998 case MONO_TYPE_SZARRAY:
3999 case MONO_TYPE_ARRAY:
4001 case MONO_TYPE_MVAR:
4002 return sizeof (gpointer);
4007 case MONO_TYPE_VALUETYPE:
4008 if (type->data.klass->enumtype) {
4009 type = type->data.klass->enum_basetype;
4010 klass = klass->element_class;
4013 return mono_class_instance_size (klass) - sizeof (MonoObject);
4014 case MONO_TYPE_GENERICINST:
4015 type = &type->data.generic_class->container_class->byval_arg;
4018 g_error ("unknown type 0x%02x in mono_class_array_element_size", type->type);
4024 * mono_array_element_size:
4025 * @ac: pointer to a #MonoArrayClass
4027 * Returns: the size of single array element.
4030 mono_array_element_size (MonoClass *ac)
4032 return mono_class_array_element_size (ac->element_class);
4036 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
4037 MonoGenericContext *context)
4039 if (image->dynamic) {
4040 MonoClass *tmp_handle_class;
4041 gpointer obj = mono_lookup_dynamic_token_class (image, token, &tmp_handle_class);
4043 g_assert (tmp_handle_class);
4045 *handle_class = tmp_handle_class;
4047 if (tmp_handle_class == mono_defaults.typehandle_class)
4048 return &((MonoClass*)obj)->byval_arg;
4053 switch (token & 0xff000000) {
4054 case MONO_TOKEN_TYPE_DEF:
4055 case MONO_TOKEN_TYPE_REF:
4056 case MONO_TOKEN_TYPE_SPEC: {
4059 *handle_class = mono_defaults.typehandle_class;
4060 class = mono_class_get_full (image, token, context);
4063 mono_class_init (class);
4064 /* We return a MonoType* as handle */
4065 return &class->byval_arg;
4067 case MONO_TOKEN_FIELD_DEF: {
4069 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
4071 *handle_class = mono_defaults.fieldhandle_class;
4072 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
4075 mono_class_init (class);
4076 return mono_class_get_field (class, token);
4078 case MONO_TOKEN_METHOD_DEF: {
4080 meth = mono_get_method_full (image, token, NULL, context);
4082 *handle_class = mono_defaults.methodhandle_class;
4085 case MONO_TOKEN_MEMBER_REF: {
4086 guint32 cols [MONO_MEMBERREF_SIZE];
4088 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
4089 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
4090 mono_metadata_decode_blob_size (sig, &sig);
4091 if (*sig == 0x6) { /* it's a field */
4093 MonoClassField *field;
4094 field = mono_field_from_token (image, token, &klass, context);
4096 *handle_class = mono_defaults.fieldhandle_class;
4100 meth = mono_get_method_full (image, token, NULL, context);
4102 *handle_class = mono_defaults.methodhandle_class;
4107 g_warning ("Unknown token 0x%08x in ldtoken", token);
4114 * This function might need to call runtime functions so it can't be part
4115 * of the metadata library.
4117 static MonoLookupDynamicToken lookup_dynamic = NULL;
4120 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
4122 lookup_dynamic = func;
4126 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
4128 MonoClass *handle_class;
4130 return lookup_dynamic (image, token, &handle_class);
4134 mono_lookup_dynamic_token_class (MonoImage *image, guint32 token, MonoClass **handle_class)
4136 return lookup_dynamic (image, token, handle_class);
4139 static MonoGetCachedClassInfo get_cached_class_info = NULL;
4142 mono_install_get_cached_class_info (MonoGetCachedClassInfo func)
4144 get_cached_class_info = func;
4148 mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
4150 if (!get_cached_class_info)
4153 return get_cached_class_info (klass, res);
4157 mono_class_get_image (MonoClass *klass)
4159 return klass->image;
4163 * mono_class_get_element_class:
4164 * @klass: the MonoClass to act on
4166 * Returns: the element class of an array or an enumeration.
4169 mono_class_get_element_class (MonoClass *klass)
4171 return klass->element_class;
4175 * mono_class_is_valuetype:
4176 * @klass: the MonoClass to act on
4178 * Returns: true if the MonoClass represents a ValueType.
4181 mono_class_is_valuetype (MonoClass *klass)
4183 return klass->valuetype;
4187 * mono_class_is_enum:
4188 * @klass: the MonoClass to act on
4190 * Returns: true if the MonoClass represents an enumeration.
4193 mono_class_is_enum (MonoClass *klass)
4195 return klass->enumtype;
4199 * mono_class_enum_basetype:
4200 * @klass: the MonoClass to act on
4202 * Returns: the underlying type representation for an enumeration.
4205 mono_class_enum_basetype (MonoClass *klass)
4207 return klass->enum_basetype;
4211 * mono_class_get_parent
4212 * @klass: the MonoClass to act on
4214 * Returns: the parent class for this class.
4217 mono_class_get_parent (MonoClass *klass)
4219 return klass->parent;
4223 * mono_class_get_nesting_type;
4224 * @klass: the MonoClass to act on
4226 * Returns: the container type where this type is nested or NULL if this type is not a nested type.
4229 mono_class_get_nesting_type (MonoClass *klass)
4231 return klass->nested_in;
4235 * mono_class_get_rank:
4236 * @klass: the MonoClass to act on
4238 * Returns: the rank for the array (the number of dimensions).
4241 mono_class_get_rank (MonoClass *klass)
4247 * mono_class_get_flags:
4248 * @klass: the MonoClass to act on
4250 * The type flags from the TypeDef table from the metadata.
4251 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
4254 * Returns: the flags from the TypeDef table.
4257 mono_class_get_flags (MonoClass *klass)
4259 return klass->flags;
4263 * mono_class_get_name
4264 * @klass: the MonoClass to act on
4266 * Returns: the name of the class.
4269 mono_class_get_name (MonoClass *klass)
4275 * mono_class_get_namespace:
4276 * @klass: the MonoClass to act on
4278 * Returns: the namespace of the class.
4281 mono_class_get_namespace (MonoClass *klass)
4283 return klass->name_space;
4287 * mono_class_get_type:
4288 * @klass: the MonoClass to act on
4290 * This method returns the internal Type representation for the class.
4292 * Returns: the MonoType from the class.
4295 mono_class_get_type (MonoClass *klass)
4297 return &klass->byval_arg;
4301 * mono_class_get_byref_type:
4302 * @klass: the MonoClass to act on
4307 mono_class_get_byref_type (MonoClass *klass)
4309 return &klass->this_arg;
4313 * mono_class_num_fields:
4314 * @klass: the MonoClass to act on
4316 * Returns: the number of static and instance fields in the class.
4319 mono_class_num_fields (MonoClass *klass)
4321 return klass->field.count;
4325 * mono_class_num_methods:
4326 * @klass: the MonoClass to act on
4328 * Returns: the number of methods in the class.
4331 mono_class_num_methods (MonoClass *klass)
4333 return klass->method.count;
4337 * mono_class_num_properties
4338 * @klass: the MonoClass to act on
4340 * Returns: the number of properties in the class.
4343 mono_class_num_properties (MonoClass *klass)
4345 mono_class_setup_properties (klass);
4347 return klass->property.count;
4351 * mono_class_num_events:
4352 * @klass: the MonoClass to act on
4354 * Returns: the number of events in the class.
4357 mono_class_num_events (MonoClass *klass)
4359 mono_class_setup_events (klass);
4361 return klass->event.count;
4365 * mono_class_get_fields:
4366 * @klass: the MonoClass to act on
4368 * This routine is an iterator routine for retrieving the fields in a class.
4370 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4371 * iterate over all of the elements. When no more values are
4372 * available, the return value is NULL.
4374 * Returns: a @MonoClassField* on each iteration, or NULL when no more fields are available.
4377 mono_class_get_fields (MonoClass* klass, gpointer *iter)
4379 MonoClassField* field;
4382 mono_class_setup_fields_locking (klass);
4384 /* start from the first */
4385 if (klass->field.count) {
4386 return *iter = &klass->fields [0];
4394 if (field < &klass->fields [klass->field.count]) {
4395 return *iter = field;
4401 * mono_class_get_methods
4402 * @klass: the MonoClass to act on
4404 * This routine is an iterator routine for retrieving the fields in a class.
4406 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4407 * iterate over all of the elements. When no more values are
4408 * available, the return value is NULL.
4410 * Returns: a MonoMethod on each iteration or NULL when no more methods are available.
4413 mono_class_get_methods (MonoClass* klass, gpointer *iter)
4415 MonoMethod** method;
4419 mono_class_init (klass);
4421 mono_class_setup_methods (klass);
4422 /* start from the first */
4423 if (klass->method.count) {
4424 *iter = &klass->methods [0];
4425 return klass->methods [0];
4433 if (method < &klass->methods [klass->method.count]) {
4441 * mono_class_get_properties:
4442 * @klass: the MonoClass to act on
4444 * This routine is an iterator routine for retrieving the properties in a class.
4446 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4447 * iterate over all of the elements. When no more values are
4448 * available, the return value is NULL.
4450 * Returns: a @MonoProperty* on each invocation, or NULL when no more are available.
4453 mono_class_get_properties (MonoClass* klass, gpointer *iter)
4455 MonoProperty* property;
4459 mono_class_init (klass);
4461 mono_class_setup_properties (klass);
4462 /* start from the first */
4463 if (klass->property.count) {
4464 return *iter = &klass->properties [0];
4472 if (property < &klass->properties [klass->property.count]) {
4473 return *iter = property;
4479 * mono_class_get_events:
4480 * @klass: the MonoClass to act on
4482 * This routine is an iterator routine for retrieving the properties in a class.
4484 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4485 * iterate over all of the elements. When no more values are
4486 * available, the return value is NULL.
4488 * Returns: a @MonoEvent* on each invocation, or NULL when no more are available.
4491 mono_class_get_events (MonoClass* klass, gpointer *iter)
4497 mono_class_init (klass);
4499 mono_class_setup_events (klass);
4500 /* start from the first */
4501 if (klass->event.count) {
4502 return *iter = &klass->events [0];
4510 if (event < &klass->events [klass->event.count]) {
4511 return *iter = event;
4517 * mono_class_get_interfaces
4518 * @klass: the MonoClass to act on
4520 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
4522 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4523 * iterate over all of the elements. When no more values are
4524 * available, the return value is NULL.
4526 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
4529 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
4535 mono_class_init (klass);
4537 /* start from the first */
4538 if (klass->interface_count) {
4539 *iter = &klass->interfaces [0];
4540 return klass->interfaces [0];
4548 if (iface < &klass->interfaces [klass->interface_count]) {
4556 * mono_class_get_nested_types
4557 * @klass: the MonoClass to act on
4559 * This routine is an iterator routine for retrieving the nested types of a class.
4561 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4562 * iterate over all of the elements. When no more values are
4563 * available, the return value is NULL.
4565 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
4568 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
4574 mono_class_init (klass);
4576 /* start from the first */
4577 if (klass->nested_classes) {
4578 *iter = klass->nested_classes;
4579 return klass->nested_classes->data;
4581 /* no nested types */
4595 * mono_field_get_name:
4596 * @field: the MonoClassField to act on
4598 * Returns: the name of the field.
4601 mono_field_get_name (MonoClassField *field)
4607 * mono_field_get_type:
4608 * @field: the MonoClassField to act on
4610 * Returns: MonoType of the field.
4613 mono_field_get_type (MonoClassField *field)
4619 * mono_field_get_type:
4620 * @field: the MonoClassField to act on
4622 * Returns: MonoClass where the field was defined.
4625 mono_field_get_parent (MonoClassField *field)
4627 return field->parent;
4631 * mono_field_get_flags;
4632 * @field: the MonoClassField to act on
4634 * The metadata flags for a field are encoded using the
4635 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4637 * Returns: the flags for the field.
4640 mono_field_get_flags (MonoClassField *field)
4642 return field->type->attrs;
4646 * mono_property_get_name:
4647 * @prop: the MonoProperty to act on
4649 * Returns: the name of the property
4652 mono_property_get_name (MonoProperty *prop)
4658 * mono_property_get_set_method
4659 * @prop: the MonoProperty to act on.
4661 * Returns: the setter method of the property (A MonoMethod)
4664 mono_property_get_set_method (MonoProperty *prop)
4670 * mono_property_get_get_method
4671 * @prop: the MonoProperty to act on.
4673 * Returns: the setter method of the property (A MonoMethod)
4676 mono_property_get_get_method (MonoProperty *prop)
4682 * mono_property_get_parent:
4683 * @prop: the MonoProperty to act on.
4685 * Returns: the MonoClass where the property was defined.
4688 mono_property_get_parent (MonoProperty *prop)
4690 return prop->parent;
4694 * mono_property_get_flags:
4695 * @prop: the MonoProperty to act on.
4697 * The metadata flags for a property are encoded using the
4698 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4700 * Returns: the flags for the property.
4703 mono_property_get_flags (MonoProperty *prop)
4709 * mono_event_get_name:
4710 * @event: the MonoEvent to act on
4712 * Returns: the name of the event.
4715 mono_event_get_name (MonoEvent *event)
4721 * mono_event_get_add_method:
4722 * @event: The MonoEvent to act on.
4724 * Returns: the @add' method for the event (a MonoMethod).
4727 mono_event_get_add_method (MonoEvent *event)
4733 * mono_event_get_remove_method:
4734 * @event: The MonoEvent to act on.
4736 * Returns: the @remove method for the event (a MonoMethod).
4739 mono_event_get_remove_method (MonoEvent *event)
4741 return event->remove;
4745 * mono_event_get_raise_method:
4746 * @event: The MonoEvent to act on.
4748 * Returns: the @raise method for the event (a MonoMethod).
4751 mono_event_get_raise_method (MonoEvent *event)
4753 return event->raise;
4757 * mono_event_get_parent:
4758 * @event: the MonoEvent to act on.
4760 * Returns: the MonoClass where the event is defined.
4763 mono_event_get_parent (MonoEvent *event)
4765 return event->parent;
4769 * mono_event_get_flags
4770 * @event: the MonoEvent to act on.
4772 * The metadata flags for an event are encoded using the
4773 * EVENT_* constants. See the tabledefs.h file for details.
4775 * Returns: the flags for the event.
4778 mono_event_get_flags (MonoEvent *event)
4780 return event->attrs;
4784 * mono_class_get_method_from_name:
4785 * @klass: where to look for the method
4786 * @name_space: name of the method
4787 * @param_count: number of parameters. -1 for any number.
4789 * Obtains a MonoMethod with a given name and number of parameters.
4790 * It only works if there are no multiple signatures for any given method name.
4793 mono_class_get_method_from_name (MonoClass *klass, const char *name, int param_count)
4795 return mono_class_get_method_from_name_flags (klass, name, param_count, 0);
4799 * mono_class_get_method_from_name_flags:
4800 * @klass: where to look for the method
4801 * @name_space: name of the method
4802 * @param_count: number of parameters. -1 for any number.
4803 * @flags: flags which must be set in the method
4805 * Obtains a MonoMethod with a given name and number of parameters.
4806 * It only works if there are no multiple signatures for any given method name.
4809 mono_class_get_method_from_name_flags (MonoClass *klass, const char *name, int param_count, int flags)
4811 MonoMethod *res = NULL;
4814 mono_class_init (klass);
4816 if (klass->methods) {
4817 mono_class_setup_methods (klass);
4818 for (i = 0; i < klass->method.count; ++i) {
4819 MonoMethod *method = klass->methods [i];
4821 if (method->name[0] == name [0] &&
4822 !strcmp (name, method->name) &&
4823 (param_count == -1 || mono_method_signature (method)->param_count == param_count) &&
4824 ((method->flags & flags) == flags)) {
4831 /* Search directly in the metadata to avoid calling setup_methods () */
4832 for (i = 0; i < klass->method.count; ++i) {
4833 guint32 cols [MONO_METHOD_SIZE];
4836 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_METHOD], klass->method.first + i, cols, MONO_METHOD_SIZE);
4838 if (!strcmp (mono_metadata_string_heap (klass->image, cols [MONO_METHOD_NAME]), name)) {
4839 method = mono_get_method (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass);
4840 if ((param_count == -1) || mono_method_signature (method)->param_count == param_count) {
4852 * mono_class_set_failure:
4853 * @klass: class in which the failure was detected
4854 * @ex_type: the kind of exception/error to be thrown (later)
4855 * @ex_data: exception data (specific to each type of exception/error)
4857 * Keep a detected failure informations in the class for later processing.
4858 * Note that only the first failure is kept.
4861 mono_class_set_failure (MonoClass *klass, guint32 ex_type, void *ex_data)
4863 if (klass->exception_type)
4865 klass->exception_type = ex_type;
4866 klass->exception_data = ex_data;
4871 * mono_class_get_exception_for_failure:
4872 * @klass: class in which the failure was detected
4874 * Return a constructed MonoException than the caller can then throw
4875 * using mono_raise_exception - or NULL if no failure is present (or
4876 * doesn't result in an exception).
4879 mono_class_get_exception_for_failure (MonoClass *klass)
4881 switch (klass->exception_type) {
4882 case MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND: {
4883 MonoDomain *domain = mono_domain_get ();
4884 MonoSecurityManager* secman = mono_security_manager_get_methods ();
4885 MonoMethod *method = klass->exception_data;
4886 guint32 error = (method) ? MONO_METADATA_INHERITANCEDEMAND_METHOD : MONO_METADATA_INHERITANCEDEMAND_CLASS;
4887 MonoObject *exc = NULL;
4891 args [1] = mono_assembly_get_object (domain, mono_image_get_assembly (klass->image));
4892 args [2] = mono_type_get_object (domain, &klass->byval_arg);
4893 args [3] = (method) ? mono_method_get_object (domain, method, NULL) : NULL;
4895 mono_runtime_invoke (secman->inheritsecurityexception, NULL, args, &exc);
4896 return (MonoException*) exc;
4898 case MONO_EXCEPTION_TYPE_LOAD:
4899 return mono_exception_from_name (mono_defaults.corlib, "System", "TypeLoadException");
4900 /* TODO - handle other class related failures */