2 * class.c: Class management for the Mono runtime
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001-2006 Novell, Inc.
16 #include <mono/metadata/image.h>
17 #include <mono/metadata/assembly.h>
18 #include <mono/metadata/cil-coff.h>
19 #include <mono/metadata/metadata.h>
20 #include <mono/metadata/metadata-internals.h>
21 #include <mono/metadata/tabledefs.h>
22 #include <mono/metadata/tokentype.h>
23 #include <mono/metadata/class-internals.h>
24 #include <mono/metadata/object.h>
25 #include <mono/metadata/appdomain.h>
26 #include <mono/metadata/mono-endian.h>
27 #include <mono/metadata/debug-helpers.h>
28 #include <mono/metadata/reflection.h>
29 #include <mono/metadata/exception.h>
30 #include <mono/metadata/security-manager.h>
31 #include <mono/os/gc_wrapper.h>
32 #include <mono/utils/mono-counters.h>
36 gboolean mono_print_vtable = FALSE;
38 static MonoClass * mono_class_create_from_typedef (MonoImage *image, guint32 type_token);
39 static void mono_class_create_generic (MonoInflatedGenericClass *gclass);
40 static gboolean mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res);
42 void (*mono_debugger_start_class_init_func) (MonoClass *klass) = NULL;
43 void (*mono_debugger_class_init_func) (MonoClass *klass) = NULL;
46 * mono_class_from_typeref:
48 * @type_token: a TypeRef token
50 * Creates the MonoClass* structure representing the type defined by
51 * the typeref token valid inside @image.
52 * Returns: the MonoClass* representing the typeref token, NULL ifcould
56 mono_class_from_typeref (MonoImage *image, guint32 type_token)
58 guint32 cols [MONO_TYPEREF_SIZE];
59 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
61 const char *name, *nspace;
63 MonoAssembly **references;
65 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
67 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
68 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
70 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
71 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
72 case MONO_RESOLTION_SCOPE_MODULE:
74 g_error ("null ResolutionScope not yet handled");
75 /* a typedef in disguise */
76 return mono_class_from_name (image, nspace, name);
77 case MONO_RESOLTION_SCOPE_MODULEREF:
78 if (image->modules [idx-1])
79 return mono_class_from_name (image->modules [idx - 1], nspace, name);
81 char *msg = g_strdup_printf ("%s%s%s", nspace, nspace [0] ? "." : "", name);
84 human_name = mono_stringify_assembly_name (&image->assembly->aname);
85 mono_loader_set_error_type_load (msg, human_name);
91 case MONO_RESOLTION_SCOPE_TYPEREF: {
92 MonoClass *enclosing = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | idx);
95 if (enclosing->inited) {
96 /* Micro-optimization: don't scan the metadata tables if enclosing is already inited */
97 for (tmp = enclosing->nested_classes; tmp; tmp = tmp->next) {
99 if (strcmp (res->name, name) == 0)
103 /* Don't call mono_class_init as we might've been called by it recursively */
104 int i = mono_metadata_nesting_typedef (enclosing->image, enclosing->type_token, 1);
106 guint32 class_nested = mono_metadata_decode_row_col (&enclosing->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, MONO_NESTED_CLASS_NESTED);
107 guint32 string_offset = mono_metadata_decode_row_col (&enclosing->image->tables [MONO_TABLE_TYPEDEF], class_nested - 1, MONO_TYPEDEF_NAME);
108 const char *nname = mono_metadata_string_heap (enclosing->image, string_offset);
110 if (strcmp (nname, name) == 0)
111 return mono_class_create_from_typedef (enclosing->image, MONO_TOKEN_TYPE_DEF | class_nested);
113 i = mono_metadata_nesting_typedef (enclosing->image, enclosing->type_token, i + 1);
116 g_warning ("TypeRef ResolutionScope not yet handled (%d)", idx);
119 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
123 references = image->references;
124 if (!references [idx - 1])
125 mono_assembly_load_reference (image, idx - 1);
126 /* If this assert fails, it probably means that you haven't installed an assembly load/search hook */
127 g_assert (references == image->references);
128 g_assert (references [idx - 1]);
130 /* If the assembly did not load, register this as a type load exception */
131 if (references [idx - 1] == REFERENCE_MISSING){
132 MonoAssemblyName aname;
135 mono_assembly_get_assemblyref (image, idx - 1, &aname);
136 human_name = mono_stringify_assembly_name (&aname);
137 mono_loader_set_error_assembly_load (human_name, image->assembly->ref_only);
143 return mono_class_from_name (references [idx - 1]->image, nspace, name);
146 static inline MonoType*
147 dup_type (MonoType* t, const MonoType *original)
149 MonoType *r = g_new0 (MonoType, 1);
151 r->attrs = original->attrs;
152 r->byref = original->byref;
153 if (t->type == MONO_TYPE_PTR)
154 t->data.type = dup_type (t->data.type, original->data.type);
155 else if (t->type == MONO_TYPE_ARRAY)
156 t->data.array = mono_dup_array_type (t->data.array);
157 else if (t->type == MONO_TYPE_FNPTR)
158 t->data.method = mono_metadata_signature_deep_dup (t->data.method);
159 mono_stats.generics_metadata_size += sizeof (MonoType);
163 /* Copy everything mono_metadata_free_array free. */
165 mono_dup_array_type (MonoArrayType *a)
167 a = g_memdup (a, sizeof (MonoArrayType));
169 a->sizes = g_memdup (a->sizes, a->numsizes * sizeof (int));
171 a->lobounds = g_memdup (a->lobounds, a->numlobounds * sizeof (int));
175 /* Copy everything mono_metadata_free_method_signature free. */
177 mono_metadata_signature_deep_dup (MonoMethodSignature *sig)
181 sig = mono_metadata_signature_dup (sig);
183 sig->ret = dup_type (sig->ret, sig->ret);
184 for (i = 0; i < sig->param_count; ++i)
185 sig->params [i] = dup_type (sig->params [i], sig->params [i]);
191 _mono_type_get_assembly_name (MonoClass *klass, GString *str)
193 MonoAssembly *ta = klass->image->assembly;
195 g_string_append_printf (
196 str, ", %s, Version=%d.%d.%d.%d, Culture=%s, PublicKeyToken=%s",
198 ta->aname.major, ta->aname.minor, ta->aname.build, ta->aname.revision,
199 ta->aname.culture && *ta->aname.culture? ta->aname.culture: "neutral",
200 ta->aname.public_key_token [0] ? (char *)ta->aname.public_key_token : "null");
204 mono_type_get_name_recurse (MonoType *type, GString *str, gboolean is_recursed,
205 MonoTypeNameFormat format)
209 switch (type->type) {
210 case MONO_TYPE_ARRAY: {
211 int i, rank = type->data.array->rank;
212 MonoTypeNameFormat nested_format;
214 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
215 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
217 mono_type_get_name_recurse (
218 &type->data.array->eklass->byval_arg, str, FALSE, nested_format);
219 g_string_append_c (str, '[');
221 g_string_append_c (str, '*');
222 for (i = 1; i < rank; i++)
223 g_string_append_c (str, ',');
224 g_string_append_c (str, ']');
225 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
226 _mono_type_get_assembly_name (type->data.array->eklass, str);
229 case MONO_TYPE_SZARRAY: {
230 MonoTypeNameFormat nested_format;
232 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
233 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
235 mono_type_get_name_recurse (
236 &type->data.klass->byval_arg, str, FALSE, nested_format);
237 g_string_append (str, "[]");
238 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
239 _mono_type_get_assembly_name (type->data.klass, str);
242 case MONO_TYPE_PTR: {
243 MonoTypeNameFormat nested_format;
245 nested_format = format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED ?
246 MONO_TYPE_NAME_FORMAT_FULL_NAME : format;
248 mono_type_get_name_recurse (
249 type->data.type, str, FALSE, nested_format);
250 g_string_append (str, "*");
251 if (format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)
252 _mono_type_get_assembly_name (type->data.klass, str);
257 g_assert (type->data.generic_param->name);
258 g_string_append (str, type->data.generic_param->name);
261 klass = mono_class_from_mono_type (type);
262 if (klass->nested_in) {
263 mono_type_get_name_recurse (
264 &klass->nested_in->byval_arg, str, TRUE, format);
265 if (format == MONO_TYPE_NAME_FORMAT_IL)
266 g_string_append_c (str, '.');
268 g_string_append_c (str, '+');
269 } else if (*klass->name_space) {
270 g_string_append (str, klass->name_space);
271 g_string_append_c (str, '.');
273 if (format == MONO_TYPE_NAME_FORMAT_IL) {
274 char *s = strchr (klass->name, '`');
275 int len = s ? s - klass->name : strlen (klass->name);
277 g_string_append_len (str, klass->name, len);
279 g_string_append (str, klass->name);
282 if (klass->generic_class) {
283 MonoGenericClass *gclass = klass->generic_class;
284 MonoTypeNameFormat nested_format;
287 nested_format = format == MONO_TYPE_NAME_FORMAT_FULL_NAME ?
288 MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED : format;
290 if (format == MONO_TYPE_NAME_FORMAT_IL)
291 g_string_append_c (str, '<');
293 g_string_append_c (str, '[');
294 for (i = 0; i < gclass->inst->type_argc; i++) {
295 MonoType *t = gclass->inst->type_argv [i];
298 g_string_append_c (str, ',');
299 if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
300 (t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
301 g_string_append_c (str, '[');
302 mono_type_get_name_recurse (
303 gclass->inst->type_argv [i], str, FALSE, nested_format);
304 if ((nested_format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
305 (t->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
306 g_string_append_c (str, ']');
308 if (format == MONO_TYPE_NAME_FORMAT_IL)
309 g_string_append_c (str, '>');
311 g_string_append_c (str, ']');
312 } else if (klass->generic_container &&
313 (format != MONO_TYPE_NAME_FORMAT_FULL_NAME) &&
314 (format != MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED)) {
317 if (format == MONO_TYPE_NAME_FORMAT_IL)
318 g_string_append_c (str, '<');
320 g_string_append_c (str, '[');
321 for (i = 0; i < klass->generic_container->type_argc; i++) {
323 g_string_append_c (str, ',');
324 g_string_append (str, klass->generic_container->type_params [i].name);
326 if (format == MONO_TYPE_NAME_FORMAT_IL)
327 g_string_append_c (str, '>');
329 g_string_append_c (str, ']');
331 if ((format == MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED) &&
332 (type->type != MONO_TYPE_VAR) && (type->type != MONO_TYPE_MVAR))
333 _mono_type_get_assembly_name (klass, str);
339 * mono_type_get_name:
341 * @format: the format for the return string.
344 * Returns: the string representation in a number of formats:
346 * if format is MONO_TYPE_NAME_FORMAT_REFLECTION, the return string is
347 * returned in the formatrequired by System.Reflection, this is the
348 * inverse of mono_reflection_parse_type ().
350 * if format is MONO_TYPE_NAME_FORMAT_IL, it returns a syntax that can
351 * be used by the IL assembler.
353 * if format is MONO_TYPE_NAME_FORMAT_FULL_NAME
355 * if format is MONO_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED
358 mono_type_get_name_full (MonoType *type, MonoTypeNameFormat format)
362 result = g_string_new ("");
364 mono_type_get_name_recurse (type, result, FALSE, format);
367 g_string_append_c (result, '&');
369 return g_string_free (result, FALSE);
373 * mono_type_get_full_name:
376 * Returns: the string representation for type as required by System.Reflection.
377 * The inverse of mono_reflection_parse_type ().
380 mono_type_get_full_name (MonoClass *class)
382 return mono_type_get_name_full (mono_class_get_type (class), MONO_TYPE_NAME_FORMAT_REFLECTION);
386 * mono_type_get_name:
389 * Returns: the string representation for type as it would be represented in IL code.
392 mono_type_get_name (MonoType *type)
394 return mono_type_get_name_full (type, MONO_TYPE_NAME_FORMAT_IL);
398 * mono_type_get_underlying_type:
401 * Returns: the MonoType for the underlying interger type if @type
402 * is an enum, otherwise the type itself.
405 mono_type_get_underlying_type (MonoType *type)
407 if (type->type == MONO_TYPE_VALUETYPE && type->data.klass->enumtype)
408 return type->data.klass->enum_basetype;
413 * mono_class_is_open_constructed_type:
416 * Returns TRUE if type represents a generics open constructed type
417 * (not all the type parameters required for the instantiation have
421 mono_class_is_open_constructed_type (MonoType *t)
427 case MONO_TYPE_SZARRAY:
428 return mono_class_is_open_constructed_type (&t->data.klass->byval_arg);
429 case MONO_TYPE_ARRAY:
430 return mono_class_is_open_constructed_type (&t->data.array->eklass->byval_arg);
432 return mono_class_is_open_constructed_type (t->data.type);
433 case MONO_TYPE_GENERICINST: {
434 MonoGenericClass *gclass = t->data.generic_class;
437 if (mono_class_is_open_constructed_type (&gclass->container_class->byval_arg))
439 for (i = 0; i < gclass->inst->type_argc; i++)
440 if (mono_class_is_open_constructed_type (gclass->inst->type_argv [i]))
449 static MonoGenericClass *
450 inflate_generic_class (MonoGenericClass *ogclass, MonoGenericContext *context)
452 MonoInflatedGenericClass *igclass;
453 MonoGenericClass *ngclass, *cached;
454 MonoGenericInst *ninst;
456 ninst = mono_metadata_inflate_generic_inst (ogclass->inst, context);
457 if (ninst == ogclass->inst)
460 if (ogclass->is_dynamic) {
461 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
462 igclass = &dgclass->generic_class;
463 ngclass = &igclass->generic_class;
464 ngclass->is_inflated = 1;
465 ngclass->is_dynamic = 1;
467 igclass = g_new0 (MonoInflatedGenericClass, 1);
468 ngclass = &igclass->generic_class;
469 ngclass->is_inflated = 1;
474 ngclass->inst = ninst;
476 igclass->klass = NULL;
478 ngclass->context = g_new0 (MonoGenericContext, 1);
479 ngclass->context->container = ngclass->container_class->generic_container;
480 ngclass->context->gclass = ngclass;
483 cached = mono_metadata_lookup_generic_class (ngclass);
484 mono_loader_unlock ();
486 g_free (ngclass->context);
495 inflate_generic_type (MonoType *type, MonoGenericContext *context)
497 switch (type->type) {
498 case MONO_TYPE_MVAR: {
499 int num = type->data.generic_param->num;
500 MonoGenericInst *inst = context->gmethod ? context->gmethod->inst : NULL;
501 if (!inst || !inst->type_argv)
503 if (num >= inst->type_argc)
504 g_error ("MVAR %d (%s) cannot be expanded in this context with %d instantiations", num, type->data.generic_param->name, inst->type_argc);
505 return dup_type (inst->type_argv [num], type);
507 case MONO_TYPE_VAR: {
508 int num = type->data.generic_param->num;
509 MonoGenericInst *inst = context->gclass ? context->gclass->inst : NULL;
512 if (num >= inst->type_argc)
513 g_error ("VAR %d (%s) cannot be expanded in this context with %d instantiations", num, type->data.generic_param->name, inst->type_argc);
514 return dup_type (inst->type_argv [num], type);
516 case MONO_TYPE_SZARRAY: {
517 MonoClass *eclass = type->data.klass;
518 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
521 nt = dup_type (type, type);
522 nt->data.klass = mono_class_from_mono_type (inflated);
525 case MONO_TYPE_ARRAY: {
526 MonoClass *eclass = type->data.array->eklass;
527 MonoType *nt, *inflated = inflate_generic_type (&eclass->byval_arg, context);
530 nt = dup_type (type, type);
531 nt->data.array = g_memdup (nt->data.array, sizeof (MonoArrayType));
532 nt->data.array->eklass = mono_class_from_mono_type (inflated);
535 case MONO_TYPE_GENERICINST: {
536 MonoGenericClass *gclass = type->data.generic_class;
538 if (!gclass->inst->is_open)
540 gclass = inflate_generic_class (gclass, context);
541 if (gclass == type->data.generic_class)
543 nt = dup_type (type, type);
544 nt->data.generic_class = gclass;
547 case MONO_TYPE_CLASS:
548 case MONO_TYPE_VALUETYPE: {
549 MonoClass *klass = type->data.klass;
550 MonoGenericClass *gclass;
553 if (!klass->generic_container)
555 gclass = inflate_generic_class (klass->generic_container->context.gclass, context);
556 if (gclass == klass->generic_container->context.gclass)
558 nt = dup_type (type, type);
559 nt->type = MONO_TYPE_GENERICINST;
560 nt->data.generic_class = gclass;
569 MonoInflatedGenericClass*
570 mono_get_inflated_generic_class (MonoGenericClass *gclass)
572 g_assert (gclass->is_inflated);
573 mono_class_create_generic ((MonoInflatedGenericClass *) gclass);
574 return (MonoInflatedGenericClass *) gclass;
578 * mono_class_inflate_generic_type:
580 * @context: a generics context
582 * Instantiate the generic type @type, using the generics context @context.
584 * Returns: the instantiated type
587 mono_class_inflate_generic_type (MonoType *type, MonoGenericContext *context)
589 MonoType *inflated = inflate_generic_type (type, context);
592 return dup_type (type, type);
594 mono_stats.inflated_type_count++;
598 static MonoGenericContext *
599 inflate_generic_context (MonoGenericContext *context, MonoGenericContext *inflate_with)
601 MonoGenericClass *gclass = NULL;
602 MonoGenericMethod *gmethod = NULL;
603 MonoGenericContext *res;
606 gclass = inflate_generic_class (context->gclass, inflate_with);
608 if (context->gmethod) {
609 MonoGenericInst *ninst = mono_metadata_inflate_generic_inst (context->gmethod->inst, inflate_with);
610 if (gclass == context->gclass && ninst == context->gmethod->inst) {
611 gmethod = context->gmethod;
613 gmethod = g_new0 (MonoGenericMethod, 1);
614 gmethod->generic_class = gclass;
615 gmethod->container = context->container;
616 gmethod->inst = ninst;
620 if (gclass == context->gclass && gmethod == context->gmethod)
623 res = g_new0 (MonoGenericContext, 1);
625 res->container = gmethod ? gmethod->container : context->container;
626 res->gclass = gclass;
627 res->gmethod = gmethod;
633 * mono_class_inflate_generic_method:
634 * @method: a generic method
635 * @context: a generics context
637 * Instantiate the generic method @method using the generics context @context.
639 * Returns: the new instantiated method
642 mono_class_inflate_generic_method (MonoMethod *method, MonoGenericContext *context)
644 return mono_class_inflate_generic_method_full (method, NULL, context);
648 * mono_class_inflate_generic_method:
650 * Instantiate method @method with the generic context @context.
651 * BEWARE: All non-trivial fields are invalid, including klass, signature, and header.
652 * Use mono_get_inflated_method (), mono_method_signature () and mono_method_get_header () to get the correct values.
655 mono_class_inflate_generic_method_full (MonoMethod *method, MonoClass *klass_hint, MonoGenericContext *context)
658 MonoMethodInflated *iresult;
659 MonoMethodSignature *sig;
661 /* The `method' has already been instantiated before -> we need to create a new context. */
662 while (method->is_inflated) {
663 MonoMethodInflated *imethod = (MonoMethodInflated *) method;
664 context = inflate_generic_context (imethod->context, context);
665 if (context == imethod->context)
667 method = imethod->declaring;
670 if (!method->generic_container && !method->klass->generic_container)
673 mono_stats.inflated_method_count++;
674 iresult = g_new0 (MonoMethodInflated, 1);
676 sig = mono_method_signature (method);
678 iresult->method.pinvoke = *(MonoMethodPInvoke*)method;
680 iresult->method.normal = *(MonoMethodNormal*)method;
681 iresult->method.normal.header = NULL;
684 result = (MonoMethod *) iresult;
685 result->is_inflated = 1;
686 result->signature = NULL;
687 iresult->context = context;
688 iresult->declaring = method;
690 if (!klass_hint || !klass_hint->generic_class ||
691 klass_hint->generic_class->container_class != method->klass ||
692 klass_hint->generic_class->inst != context->gclass->inst)
695 if (method->klass->generic_container)
696 result->klass = klass_hint;
698 if (!result->klass) {
699 MonoType *inflated = inflate_generic_type (&method->klass->byval_arg, context);
700 result->klass = inflated ? mono_class_from_mono_type (inflated) : method->klass;
703 if (method->generic_container && !context->gmethod) {
704 MonoGenericMethod *gmethod = g_memdup (method->generic_container->context.gmethod, sizeof (*gmethod));
705 gmethod->generic_class = result->klass->generic_class;
707 context = g_new0 (MonoGenericContext, 1);
708 context->container = method->generic_container;
709 context->gclass = result->klass->generic_class;
710 context->gmethod = gmethod;
712 iresult->context = context;
719 * mono_get_inflated_method:
721 * For performance reasons, mono_class_inflate_generic_method() does not actually instantiate the
722 * method, it just "prepares" it for that. If you really need to fully instantiate the method
723 * (including its signature and header), call this method.
724 * FIXME: Martin? this description looks completely wrong.
727 mono_get_inflated_method (MonoMethod *method)
733 * mono_class_find_enum_basetype:
734 * @class: The enum class
736 * Determine the basetype of an enum by iterating through its fields. We do this
737 * in a separate function since it is cheaper than calling mono_class_setup_fields.
740 mono_class_find_enum_basetype (MonoClass *class)
742 MonoImage *m = class->image;
743 const int top = class->field.count;
744 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
747 g_assert (class->enumtype);
750 * Fetch all the field information.
752 for (i = 0; i < top; i++){
754 guint32 cols [MONO_FIELD_SIZE];
755 int idx = class->field.first + i;
756 MonoGenericContainer *container = NULL;
759 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
760 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
761 mono_metadata_decode_value (sig, &sig);
762 /* FIELD signature == 0x06 */
763 g_assert (*sig == 0x06);
764 if (class->generic_container)
765 container = class->generic_container;
766 else if (class->generic_class) {
767 MonoClass *gklass = class->generic_class->container_class;
769 container = gklass->generic_container;
770 g_assert (container);
772 ftype = mono_metadata_parse_type_full (m, container, MONO_PARSE_FIELD, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
775 if (class->generic_class) {
776 ftype = mono_class_inflate_generic_type (ftype, class->generic_class->context);
777 ftype->attrs = cols [MONO_FIELD_FLAGS];
780 if (class->enumtype && !(cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_STATIC))
788 * mono_class_setup_fields:
789 * @class: The class to initialize
791 * Initializes the class->fields.
792 * LOCKING: Assumes the loader lock is held.
795 mono_class_setup_fields (MonoClass *class)
797 MonoImage *m = class->image;
798 int top = class->field.count;
799 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
800 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
801 int i, blittable = TRUE;
802 guint32 real_size = 0;
803 guint32 packing_size = 0;
804 gboolean explicit_size;
805 MonoClassField *field;
806 MonoGenericContainer *container = NULL;
807 MonoClass *gklass = NULL;
809 if (class->size_inited)
812 if (class->generic_class) {
813 MonoClass *gklass = class->generic_class->container_class;
814 mono_class_setup_fields (gklass);
815 top = gklass->field.count;
818 class->instance_size = 0;
819 class->class_size = 0;
822 /* For generic instances, class->parent might not have been initialized */
823 mono_class_init (class->parent);
824 if (!class->parent->size_inited)
825 mono_class_setup_fields (class->parent);
826 class->instance_size += class->parent->instance_size;
827 class->min_align = class->parent->min_align;
828 /* we use |= since it may have been set already */
829 class->has_references |= class->parent->has_references;
830 blittable = class->parent->blittable;
832 class->instance_size = sizeof (MonoObject);
833 class->min_align = 1;
836 /* Get the real size */
837 explicit_size = mono_metadata_packing_from_typedef (class->image, class->type_token, &packing_size, &real_size);
840 g_assert ((packing_size & 0xfffffff0) == 0);
841 class->packing_size = packing_size;
842 real_size += class->instance_size;
846 if (explicit_size && real_size) {
847 class->instance_size = MAX (real_size, class->instance_size);
849 class->size_inited = 1;
850 class->blittable = blittable;
854 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT)
857 /* Prevent infinite loops if the class references itself */
858 class->size_inited = 1;
860 class->fields = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoClassField) * top);
862 if (class->generic_container) {
863 container = class->generic_container;
864 } else if (class->generic_class) {
865 gklass = class->generic_class->container_class;
866 container = gklass->generic_container;
867 g_assert (container);
869 mono_class_setup_fields (gklass);
873 * Fetch all the field information.
875 for (i = 0; i < top; i++){
876 int idx = class->field.first + i;
877 field = &class->fields [i];
879 field->parent = class;
881 if (class->generic_class) {
882 MonoClassField *gfield = &gklass->fields [i];
883 MonoInflatedField *ifield = g_new0 (MonoInflatedField, 1);
885 ifield->generic_type = gfield->type;
886 field->name = gfield->name;
887 field->generic_info = ifield;
888 field->type = mono_class_inflate_generic_type (gfield->type, class->generic_class->context);
889 field->type->attrs = gfield->type->attrs;
890 if (mono_field_is_deleted (field))
892 field->offset = gfield->offset;
893 field->data = gfield->data;
897 guint32 cols [MONO_FIELD_SIZE];
899 mono_metadata_decode_row (t, idx, cols, MONO_FIELD_SIZE);
900 /* The name is needed for fieldrefs */
901 field->name = mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]);
902 sig = mono_metadata_blob_heap (m, cols [MONO_FIELD_SIGNATURE]);
903 mono_metadata_decode_value (sig, &sig);
904 /* FIELD signature == 0x06 */
905 g_assert (*sig == 0x06);
906 field->type = mono_metadata_parse_type_full (m, container, MONO_PARSE_FIELD, cols [MONO_FIELD_FLAGS], sig + 1, &sig);
908 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
911 if (mono_field_is_deleted (field))
913 if (layout == TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
915 mono_metadata_field_info (m, idx, &offset, NULL, NULL);
916 field->offset = offset;
917 if (field->offset == (guint32)-1 && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
918 g_warning ("%s not initialized correctly (missing field layout info for %s)",
919 class->name, field->name);
922 if (field->type->attrs & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
923 mono_metadata_field_info (m, idx, NULL, &rva, NULL);
925 g_warning ("field %s in %s should have RVA data, but hasn't", field->name, class->name);
926 field->data = mono_image_rva_map (class->image, rva);
930 /* Only do these checks if we still think this type is blittable */
931 if (blittable && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
932 if (field->type->byref || MONO_TYPE_IS_REFERENCE (field->type)) {
935 MonoClass *field_class = mono_class_from_mono_type (field->type);
936 if (!field_class || !field_class->blittable)
941 if (class->enumtype && !(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
942 class->enum_basetype = field->type;
943 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
944 blittable = class->element_class->blittable;
947 /* The def_value of fields is compute lazily during vtable creation */
950 if (class == mono_defaults.string_class)
953 class->blittable = blittable;
955 if (class->enumtype && !class->enum_basetype) {
956 if (!((strcmp (class->name, "Enum") == 0) && (strcmp (class->name_space, "System") == 0)))
959 if (explicit_size && real_size) {
960 class->instance_size = MAX (real_size, class->instance_size);
963 if (class->exception_type)
965 mono_class_layout_fields (class);
969 * mono_class_setup_fields_locking:
970 * @class: The class to initialize
972 * Initializes the class->fields array of fields.
973 * Aquires the loader lock.
976 mono_class_setup_fields_locking (MonoClass *class)
979 mono_class_setup_fields (class);
980 mono_loader_unlock ();
984 * mono_class_has_references:
986 * Returns whenever @klass->has_references is set, initializing it if needed.
987 * Aquires the loader lock.
990 mono_class_has_references (MonoClass *klass)
992 if (klass->init_pending) {
993 /* Be conservative */
996 mono_class_init (klass);
998 return klass->has_references;
1002 /* useful until we keep track of gc-references in corlib etc. */
1004 #define IS_GC_REFERENCE(t) FALSE
1006 #define IS_GC_REFERENCE(t) ((t)->type == MONO_TYPE_U || (t)->type == MONO_TYPE_I || (t)->type == MONO_TYPE_PTR)
1010 * mono_class_layout_fields:
1013 * Compute the placement of fields inside an object or struct, according to
1014 * the layout rules and set the following fields in @class:
1015 * - has_references (if the class contains instance references firled or structs that contain references)
1016 * - has_static_refs (same, but for static fields)
1017 * - instance_size (size of the object in memory)
1018 * - class_size (size needed for the static fields)
1019 * - size_inited (flag set when the instance_size is set)
1021 * LOCKING: this is supposed to be called with the loader lock held.
1024 mono_class_layout_fields (MonoClass *class)
1027 const int top = class->field.count;
1028 guint32 layout = class->flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
1029 guint32 pass, passes, real_size;
1030 gboolean gc_aware_layout = FALSE;
1031 MonoClassField *field;
1033 if (class->generic_container ||
1034 (class->generic_class && class->generic_class->inst->is_open))
1038 * Enable GC aware auto layout: in this mode, reference
1039 * fields are grouped together inside objects, increasing collector
1041 * Requires that all classes whose layout is known to native code be annotated
1042 * with [StructLayout (LayoutKind.Sequential)]
1043 * Value types have gc_aware_layout disabled by default, as per
1044 * what the default is for other runtimes.
1046 /* corlib is missing [StructLayout] directives in many places */
1047 if (layout == TYPE_ATTRIBUTE_AUTO_LAYOUT) {
1048 if (class->image != mono_defaults.corlib &&
1049 class->byval_arg.type != MONO_TYPE_VALUETYPE)
1050 gc_aware_layout = TRUE;
1053 /* Compute klass->has_references */
1055 * Process non-static fields first, since static fields might recursively
1056 * refer to the class itself.
1058 for (i = 0; i < top; i++) {
1061 field = &class->fields [i];
1063 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC)) {
1064 ftype = mono_type_get_underlying_type (field->type);
1065 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype)))))
1066 class->has_references = TRUE;
1070 for (i = 0; i < top; i++) {
1073 field = &class->fields [i];
1075 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC) {
1076 ftype = mono_type_get_underlying_type (field->type);
1077 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype)))))
1078 class->has_static_refs = TRUE;
1082 for (i = 0; i < top; i++) {
1085 field = &class->fields [i];
1087 ftype = mono_type_get_underlying_type (field->type);
1088 if (MONO_TYPE_IS_REFERENCE (ftype) || IS_GC_REFERENCE (ftype) || ((MONO_TYPE_ISSTRUCT (ftype) && mono_class_has_references (mono_class_from_mono_type (ftype))))) {
1089 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1090 class->has_static_refs = TRUE;
1092 class->has_references = TRUE;
1097 * Compute field layout and total size (not considering static fields)
1101 case TYPE_ATTRIBUTE_AUTO_LAYOUT:
1102 case TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT:
1104 if (gc_aware_layout)
1109 if (layout != TYPE_ATTRIBUTE_AUTO_LAYOUT)
1113 real_size = class->parent->instance_size;
1115 real_size = sizeof (MonoObject);
1117 for (pass = 0; pass < passes; ++pass) {
1118 for (i = 0; i < top; i++){
1121 field = &class->fields [i];
1123 if (mono_field_is_deleted (field))
1125 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1128 if (gc_aware_layout) {
1130 * We process fields with reference type in the first pass,
1131 * and fields with non-reference type in the second pass.
1132 * We use IS_POINTER instead of IS_REFERENCE because in
1133 * some internal structures, we store GC_MALLOCed memory
1134 * in IntPtr fields...
1136 if (MONO_TYPE_IS_POINTER (field->type)) {
1145 if ((top == 1) && (class->instance_size == sizeof (MonoObject)) &&
1146 (strcmp (field->name, "$PRIVATE$") == 0)) {
1147 /* This field is a hack inserted by MCS to empty structures */
1151 size = mono_type_size (field->type, &align);
1153 /* FIXME (LAMESPEC): should we also change the min alignment according to pack? */
1154 align = class->packing_size ? MIN (class->packing_size, align): align;
1155 class->min_align = MAX (align, class->min_align);
1156 field->offset = real_size;
1157 field->offset += align - 1;
1158 field->offset &= ~(align - 1);
1159 real_size = field->offset + size;
1162 class->instance_size = MAX (real_size, class->instance_size);
1164 if (class->instance_size & (class->min_align - 1)) {
1165 class->instance_size += class->min_align - 1;
1166 class->instance_size &= ~(class->min_align - 1);
1170 case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT:
1172 for (i = 0; i < top; i++) {
1175 field = &class->fields [i];
1178 * There must be info about all the fields in a type if it
1179 * uses explicit layout.
1182 if (mono_field_is_deleted (field))
1184 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1187 size = mono_type_size (field->type, &align);
1190 * When we get here, field->offset is already set by the
1191 * loader (for either runtime fields or fields loaded from metadata).
1192 * The offset is from the start of the object: this works for both
1193 * classes and valuetypes.
1195 field->offset += sizeof (MonoObject);
1200 real_size = MAX (real_size, size + field->offset);
1202 class->instance_size = MAX (real_size, class->instance_size);
1206 if (layout != TYPE_ATTRIBUTE_EXPLICIT_LAYOUT) {
1208 * For small structs, set min_align to at least the struct size, since the
1209 * JIT memset/memcpy code assumes this and generates unaligned accesses
1210 * otherwise. See #78990 for a testcase.
1212 if (class->instance_size <= sizeof (MonoObject) + sizeof (gpointer))
1213 class->min_align = MAX (class->min_align, class->instance_size - sizeof (MonoObject));
1216 class->size_inited = 1;
1219 * Compute static field layout and size
1221 for (i = 0; i < top; i++){
1224 field = &class->fields [i];
1226 if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC) || field->type->attrs & FIELD_ATTRIBUTE_LITERAL)
1228 if (mono_field_is_deleted (field))
1231 size = mono_type_size (field->type, &align);
1232 field->offset = class->class_size;
1233 field->offset += align - 1;
1234 field->offset &= ~(align - 1);
1235 class->class_size = field->offset + size;
1240 * mono_class_setup_methods:
1243 * Initializes the 'methods' array in the klass.
1244 * Calling this method should be avoided if possible since it allocates a lot
1245 * of long-living MonoMethod structures.
1246 * Methods belonging to an interface are assigned a sequential slot starting
1250 mono_class_setup_methods (MonoClass *class)
1253 MonoMethod **methods;
1258 mono_loader_lock ();
1260 if (class->methods) {
1261 mono_loader_unlock ();
1265 //printf ("INIT: %s.%s\n", class->name_space, class->name);
1267 if (!class->methods) {
1268 methods = mono_mempool_alloc (class->image->mempool, sizeof (MonoMethod*) * class->method.count);
1269 for (i = 0; i < class->method.count; ++i) {
1270 methods [i] = mono_get_method (class->image, MONO_TOKEN_METHOD_DEF | (i + class->method.first + 1), class);
1274 if (MONO_CLASS_IS_INTERFACE (class))
1275 for (i = 0; i < class->method.count; ++i)
1276 methods [i]->slot = i;
1278 /* Leave this assignment as the last op in this function */
1279 class->methods = methods;
1281 mono_loader_unlock ();
1286 mono_class_setup_properties (MonoClass *class)
1288 guint startm, endm, i, j;
1289 guint32 cols [MONO_PROPERTY_SIZE];
1290 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_PROPERTY];
1291 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
1292 MonoProperty *properties;
1295 if (class->properties)
1298 mono_loader_lock ();
1300 if (class->properties) {
1301 mono_loader_unlock ();
1305 class->property.first = mono_metadata_properties_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &last);
1306 class->property.count = last - class->property.first;
1308 if (class->property.count)
1309 mono_class_setup_methods (class);
1311 properties = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoProperty) * class->property.count);
1312 for (i = class->property.first; i < last; ++i) {
1313 mono_metadata_decode_row (pt, i, cols, MONO_PROPERTY_SIZE);
1314 properties [i - class->property.first].parent = class;
1315 properties [i - class->property.first].attrs = cols [MONO_PROPERTY_FLAGS];
1316 properties [i - class->property.first].name = mono_metadata_string_heap (class->image, cols [MONO_PROPERTY_NAME]);
1318 startm = mono_metadata_methods_from_property (class->image, i, &endm);
1319 for (j = startm; j < endm; ++j) {
1320 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
1321 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1322 case METHOD_SEMANTIC_SETTER:
1323 properties [i - class->property.first].set = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1325 case METHOD_SEMANTIC_GETTER:
1326 properties [i - class->property.first].get = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1334 /* Leave this assignment as the last op in the function */
1335 class->properties = properties;
1337 mono_loader_unlock ();
1341 * LOCKING: assumes the loader lock is held.
1344 inflate_event (MonoClass *class, MonoEvent *event, MonoInflatedGenericClass *gclass)
1346 event->parent = class;
1349 MonoMethod *inflated = mono_class_inflate_generic_method_full (
1350 event->add, class, gclass->generic_class.context);
1352 event->add = mono_get_inflated_method (inflated);
1355 if (event->remove) {
1356 MonoMethod *inflated = mono_class_inflate_generic_method_full (
1357 event->remove, class, gclass->generic_class.context);
1359 event->remove = mono_get_inflated_method (inflated);
1363 MonoMethod *inflated = mono_class_inflate_generic_method_full (
1364 event->raise, class, gclass->generic_class.context);
1366 event->raise = mono_get_inflated_method (inflated);
1370 MonoMethod **om = event->other;
1377 event->other = g_new0 (MonoMethod*, count + 1);
1380 MonoMethod *inflated = mono_class_inflate_generic_method_full (
1381 *om, class, gclass->generic_class.context);
1383 event->other [count++] = mono_get_inflated_method (inflated);
1390 mono_class_setup_events (MonoClass *class)
1392 guint startm, endm, i, j;
1393 guint32 cols [MONO_EVENT_SIZE];
1394 MonoTableInfo *pt = &class->image->tables [MONO_TABLE_EVENT];
1395 MonoTableInfo *msemt = &class->image->tables [MONO_TABLE_METHODSEMANTICS];
1402 mono_loader_lock ();
1404 if (class->events) {
1405 mono_loader_unlock ();
1409 if (class->generic_class) {
1410 MonoInflatedGenericClass *gclass;
1413 gclass = mono_get_inflated_generic_class (class->generic_class);
1414 gklass = gclass->generic_class.container_class;
1416 mono_class_setup_events (gklass);
1417 class->event = gklass->event;
1419 class->events = g_new0 (MonoEvent, class->event.count);
1421 for (i = 0; i < class->event.count; i++) {
1422 class->events [i] = gklass->events [i];
1423 inflate_event (class, &class->events [i], gclass);
1426 mono_loader_unlock ();
1430 class->event.first = mono_metadata_events_from_typedef (class->image, mono_metadata_token_index (class->type_token) - 1, &last);
1431 class->event.count = last - class->event.first;
1433 if (class->event.count)
1434 mono_class_setup_methods (class);
1436 events = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoEvent) * class->event.count);
1437 for (i = class->event.first; i < last; ++i) {
1438 MonoEvent *event = &events [i - class->event.first];
1440 mono_metadata_decode_row (pt, i, cols, MONO_EVENT_SIZE);
1441 event->parent = class;
1442 event->attrs = cols [MONO_EVENT_FLAGS];
1443 event->name = mono_metadata_string_heap (class->image, cols [MONO_EVENT_NAME]);
1445 startm = mono_metadata_methods_from_event (class->image, i, &endm);
1446 for (j = startm; j < endm; ++j) {
1447 mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
1448 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1449 case METHOD_SEMANTIC_ADD_ON:
1450 event->add = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1452 case METHOD_SEMANTIC_REMOVE_ON:
1453 event->remove = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1455 case METHOD_SEMANTIC_FIRE:
1456 event->raise = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1458 case METHOD_SEMANTIC_OTHER: {
1461 if (event->other == NULL) {
1462 event->other = g_new0 (MonoMethod*, 2);
1464 while (event->other [n])
1466 event->other = g_realloc (event->other, (n + 2) * sizeof (MonoMethod*));
1468 event->other [n] = class->methods [cols [MONO_METHOD_SEMA_METHOD] - 1 - class->method.first];
1469 /* NULL terminated */
1470 event->other [n + 1] = NULL;
1478 /* Leave this assignment as the last op in the function */
1479 class->events = events;
1481 mono_loader_unlock ();
1485 * Global pool of interface IDs, represented as a bitset.
1486 * LOCKING: this is supposed to be accessed with the loader lock held.
1488 static MonoBitSet *global_interface_bitset = NULL;
1491 * mono_unload_interface_ids:
1492 * @bitset: bit set of interface IDs
1494 * When an image is unloaded, the interface IDs associated with
1495 * the image are put back in the global pool of IDs so the numbers
1499 mono_unload_interface_ids (MonoBitSet *bitset)
1501 mono_loader_lock ();
1502 mono_bitset_sub (global_interface_bitset, bitset);
1503 mono_loader_unlock ();
1507 * mono_get_unique_iid:
1510 * Assign a unique integer ID to the interface represented by @class.
1511 * The ID will positive and as small as possible.
1512 * LOCKING: this is supposed to be called with the loader lock held.
1513 * Returns: the new ID.
1516 mono_get_unique_iid (MonoClass *class)
1520 g_assert (MONO_CLASS_IS_INTERFACE (class));
1522 if (!global_interface_bitset) {
1523 global_interface_bitset = mono_bitset_new (128, 0);
1526 iid = mono_bitset_find_first_unset (global_interface_bitset, -1);
1528 int old_size = mono_bitset_size (global_interface_bitset);
1529 MonoBitSet *new_set = mono_bitset_clone (global_interface_bitset, old_size * 2);
1530 mono_bitset_free (global_interface_bitset);
1531 global_interface_bitset = new_set;
1534 mono_bitset_set (global_interface_bitset, iid);
1535 /* set the bit also in the per-image set */
1536 if (class->image->interface_bitset) {
1537 if (iid >= mono_bitset_size (class->image->interface_bitset)) {
1538 MonoBitSet *new_set = mono_bitset_clone (class->image->interface_bitset, iid + 1);
1539 mono_bitset_free (class->image->interface_bitset);
1540 class->image->interface_bitset = new_set;
1543 class->image->interface_bitset = mono_bitset_new (iid + 1, 0);
1545 mono_bitset_set (class->image->interface_bitset, iid);
1547 if (mono_print_vtable) {
1549 char *type_name = mono_type_full_name (&class->byval_arg);
1550 if (class->generic_class && !class->generic_class->inst->is_open) {
1551 generic_id = class->generic_class->inst->id;
1552 g_assert (generic_id != 0);
1556 printf ("Interface: assigned id %d to %s|%s|%d\n", iid, class->image->name, type_name, generic_id);
1560 g_assert (iid <= 65535);
1565 collect_implemented_interfaces_aux (MonoClass *klass, GPtrArray **res)
1570 for (i = 0; i < klass->interface_count; i++) {
1571 ic = klass->interfaces [i];
1574 *res = g_ptr_array_new ();
1575 g_ptr_array_add (*res, ic);
1576 mono_class_init (ic);
1578 collect_implemented_interfaces_aux (ic, res);
1583 mono_class_get_implemented_interfaces (MonoClass *klass)
1585 GPtrArray *res = NULL;
1587 collect_implemented_interfaces_aux (klass, &res);
1591 typedef struct _IOffsetInfo IOffsetInfo;
1592 struct _IOffsetInfo {
1596 int data [MONO_ZERO_LEN_ARRAY];
1599 static IOffsetInfo *cached_offset_info = NULL;
1600 static int next_offset_info_size = 128;
1603 cache_interface_offsets (int max_iid, int *data)
1605 IOffsetInfo *cached_info;
1608 for (cached_info = cached_offset_info; cached_info; cached_info = cached_info->next) {
1609 cached = cached_info->data;
1610 while (cached < cached_info->data + cached_info->size && *cached) {
1611 if (*cached == max_iid) {
1612 int i, matched = TRUE;
1614 for (i = 0; i < max_iid; ++i) {
1615 if (cached [i] != data [i]) {
1624 cached += *cached + 1;
1628 /* find a free slot */
1629 for (cached_info = cached_offset_info; cached_info; cached_info = cached_info->next) {
1630 if (cached_info->size - cached_info->next_free >= max_iid + 1) {
1631 cached = &cached_info->data [cached_info->next_free];
1632 *cached++ = max_iid;
1633 memcpy (cached, data, max_iid * sizeof (int));
1634 cached_info->next_free += max_iid + 1;
1638 /* allocate a new chunk */
1639 if (max_iid + 1 < next_offset_info_size) {
1640 new_size = next_offset_info_size;
1641 if (next_offset_info_size < 4096)
1642 next_offset_info_size += next_offset_info_size >> 2;
1644 new_size = max_iid + 1;
1646 cached_info = g_malloc0 (sizeof (IOffsetInfo) + sizeof (int) * new_size);
1647 cached_info->size = new_size;
1648 /*g_print ("allocated %d offset entries at %p (total: %d)\n", new_size, cached_info->data, offset_info_total_size);*/
1649 cached = &cached_info->data [0];
1650 *cached++ = max_iid;
1651 memcpy (cached, data, max_iid * sizeof (int));
1652 cached_info->next_free += max_iid + 1;
1653 cached_info->next = cached_offset_info;
1654 cached_offset_info = cached_info;
1659 * LOCKING: this is supposed to be called with the loader lock held.
1662 setup_interface_offsets (MonoClass *class, int cur_slot)
1669 /* compute maximum number of slots and maximum interface id */
1671 for (k = class; k ; k = k->parent) {
1672 for (i = 0; i < k->interface_count; i++) {
1673 ic = k->interfaces [i];
1676 mono_class_init (ic);
1678 if (max_iid < ic->interface_id)
1679 max_iid = ic->interface_id;
1681 ifaces = mono_class_get_implemented_interfaces (k);
1683 for (i = 0; i < ifaces->len; ++i) {
1684 ic = g_ptr_array_index (ifaces, i);
1685 if (max_iid < ic->interface_id)
1686 max_iid = ic->interface_id;
1688 g_ptr_array_free (ifaces, TRUE);
1692 if (MONO_CLASS_IS_INTERFACE (class)) {
1693 if (max_iid < class->interface_id)
1694 max_iid = class->interface_id;
1696 class->max_interface_id = max_iid;
1697 /* compute vtable offset for interfaces */
1698 class->interface_offsets = g_malloc (sizeof (int) * (max_iid + 1));
1700 for (i = 0; i <= max_iid; i++)
1701 class->interface_offsets [i] = -1;
1703 ifaces = mono_class_get_implemented_interfaces (class);
1705 for (i = 0; i < ifaces->len; ++i) {
1706 ic = g_ptr_array_index (ifaces, i);
1707 class->interface_offsets [ic->interface_id] = cur_slot;
1708 cur_slot += ic->method.count;
1710 g_ptr_array_free (ifaces, TRUE);
1713 for (k = class->parent; k ; k = k->parent) {
1714 ifaces = mono_class_get_implemented_interfaces (k);
1716 for (i = 0; i < ifaces->len; ++i) {
1717 ic = g_ptr_array_index (ifaces, i);
1719 if (class->interface_offsets [ic->interface_id] == -1) {
1720 int io = k->interface_offsets [ic->interface_id];
1724 class->interface_offsets [ic->interface_id] = io;
1727 g_ptr_array_free (ifaces, TRUE);
1731 if (MONO_CLASS_IS_INTERFACE (class))
1732 class->interface_offsets [class->interface_id] = cur_slot;
1734 cached_data = cache_interface_offsets (max_iid + 1, class->interface_offsets);
1735 g_free (class->interface_offsets);
1736 class->interface_offsets = cached_data;
1742 * Setup interface offsets for interfaces. Used by Ref.Emit.
1745 mono_class_setup_interface_offsets (MonoClass *class)
1747 mono_loader_lock ();
1749 setup_interface_offsets (class, 0);
1751 mono_loader_unlock ();
1755 mono_class_setup_vtable (MonoClass *class)
1757 MonoMethod **overrides;
1758 MonoGenericContext *context;
1766 mono_class_setup_methods (class);
1768 if (MONO_CLASS_IS_INTERFACE (class))
1771 mono_loader_lock ();
1773 if (class->vtable) {
1774 mono_loader_unlock ();
1778 if (class->generic_class) {
1779 context = class->generic_class->context;
1780 type_token = class->generic_class->container_class->type_token;
1782 context = (MonoGenericContext *) class->generic_container;
1783 type_token = class->type_token;
1786 if (class->image->dynamic)
1787 mono_reflection_get_dynamic_overrides (class, &overrides, &onum);
1789 /* The following call fails if there are missing methods in the type */
1790 ok = mono_class_get_overrides_full (class->image, type_token, &overrides, &onum, context);
1794 mono_class_setup_vtable_general (class, overrides, onum);
1798 mono_loader_unlock ();
1804 * LOCKING: this is supposed to be called with the loader lock held.
1807 mono_class_setup_vtable_general (MonoClass *class, MonoMethod **overrides, int onum)
1810 MonoMethod **vtable;
1811 int i, max_vtsize = 0, max_iid, cur_slot = 0;
1812 GPtrArray *ifaces, *pifaces = NULL;
1813 GHashTable *override_map = NULL;
1814 gboolean security_enabled = mono_is_security_manager_active ();
1819 ifaces = mono_class_get_implemented_interfaces (class);
1821 for (i = 0; i < ifaces->len; i++) {
1822 MonoClass *ic = g_ptr_array_index (ifaces, i);
1823 max_vtsize += ic->method.count;
1825 g_ptr_array_free (ifaces, TRUE);
1828 if (class->parent) {
1829 mono_class_init (class->parent);
1830 mono_class_setup_vtable (class->parent);
1831 max_vtsize += class->parent->vtable_size;
1832 cur_slot = class->parent->vtable_size;
1835 max_vtsize += class->method.count;
1837 vtable = alloca (sizeof (gpointer) * max_vtsize);
1838 memset (vtable, 0, sizeof (gpointer) * max_vtsize);
1840 /* printf ("METAINIT %s.%s\n", class->name_space, class->name); */
1842 cur_slot = setup_interface_offsets (class, cur_slot);
1843 max_iid = class->max_interface_id;
1845 if (class->parent && class->parent->vtable_size)
1846 memcpy (vtable, class->parent->vtable, sizeof (gpointer) * class->parent->vtable_size);
1848 /* override interface methods */
1849 for (i = 0; i < onum; i++) {
1850 MonoMethod *decl = overrides [i*2];
1851 if (MONO_CLASS_IS_INTERFACE (decl->klass)) {
1853 mono_class_setup_methods (decl->klass);
1854 g_assert (decl->slot != -1);
1855 dslot = decl->slot + class->interface_offsets [decl->klass->interface_id];
1856 vtable [dslot] = overrides [i*2 + 1];
1857 vtable [dslot]->slot = dslot;
1859 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
1861 g_hash_table_insert (override_map, overrides [i * 2], overrides [i * 2 + 1]);
1865 for (k = class; k ; k = k->parent) {
1868 ifaces = mono_class_get_implemented_interfaces (k);
1870 nifaces = ifaces->len;
1871 if (k->generic_class) {
1872 pifaces = mono_class_get_implemented_interfaces (
1873 k->generic_class->container_class);
1874 g_assert (pifaces && (pifaces->len == nifaces));
1877 for (i = 0; i < nifaces; i++) {
1878 MonoClass *pic = NULL;
1881 ic = g_ptr_array_index (ifaces, i);
1883 pic = g_ptr_array_index (pifaces, i);
1884 g_assert (ic->interface_id <= k->max_interface_id);
1885 io = k->interface_offsets [ic->interface_id];
1888 g_assert (io <= max_vtsize);
1891 mono_class_setup_methods (ic);
1892 for (l = 0; l < ic->method.count; l++) {
1893 MonoMethod *im = ic->methods [l];
1895 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1898 for (j = 0; j < class->method.count; ++j) {
1899 MonoMethod *cm = class->methods [j];
1900 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1901 !((cm->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) ||
1902 !(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT))
1904 if (!strcmp(cm->name, im->name) &&
1905 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1907 /* CAS - SecurityAction.InheritanceDemand on interface */
1908 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1909 mono_secman_inheritancedemand_method (cm, im);
1912 g_assert (io + l <= max_vtsize);
1913 vtable [io + l] = cm;
1918 /* already implemented */
1919 if (io >= k->vtable_size)
1923 for (l = 0; l < ic->method.count; l++) {
1924 MonoMethod *im = ic->methods [l];
1927 g_assert (io + l <= max_vtsize);
1929 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1932 for (k1 = class; k1; k1 = k1->parent) {
1933 for (j = 0; j < k1->method.count; ++j) {
1934 MonoMethod *cm = k1->methods [j];
1936 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL) ||
1937 !(cm->flags & METHOD_ATTRIBUTE_PUBLIC))
1940 if (!strcmp(cm->name, im->name) &&
1941 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1943 /* CAS - SecurityAction.InheritanceDemand on interface */
1944 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1945 mono_secman_inheritancedemand_method (cm, im);
1948 g_assert (io + l <= max_vtsize);
1949 vtable [io + l] = cm;
1954 g_assert (io + l <= max_vtsize);
1955 if (vtable [io + l] && !(vtable [io + l]->flags & METHOD_ATTRIBUTE_ABSTRACT))
1960 for (l = 0; l < ic->method.count; l++) {
1961 MonoMethod *im = ic->methods [l];
1962 char *qname, *fqname, *cname, *the_cname;
1965 if (vtable [io + l])
1969 the_cname = mono_type_get_name_full (&pic->byval_arg, MONO_TYPE_NAME_FORMAT_IL);
1973 cname = (char*)ic->name;
1976 qname = g_strconcat (cname, ".", im->name, NULL);
1977 if (ic->name_space && ic->name_space [0])
1978 fqname = g_strconcat (ic->name_space, ".", cname, ".", im->name, NULL);
1982 for (k1 = class; k1; k1 = k1->parent) {
1983 for (j = 0; j < k1->method.count; ++j) {
1984 MonoMethod *cm = k1->methods [j];
1986 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
1989 if (((fqname && !strcmp (cm->name, fqname)) || !strcmp (cm->name, qname)) &&
1990 mono_metadata_signature_equal (mono_method_signature (cm), mono_method_signature (im))) {
1992 /* CAS - SecurityAction.InheritanceDemand on interface */
1993 if (security_enabled && (im->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
1994 mono_secman_inheritancedemand_method (cm, im);
1997 g_assert (io + l <= max_vtsize);
1998 vtable [io + l] = cm;
2009 if (!(class->flags & TYPE_ATTRIBUTE_ABSTRACT)) {
2010 for (l = 0; l < ic->method.count; l++) {
2012 MonoMethod *im = ic->methods [l];
2013 if (im->flags & METHOD_ATTRIBUTE_STATIC)
2015 g_assert (io + l <= max_vtsize);
2018 * If one of our parents already implements this interface
2019 * we can inherit the implementation.
2021 if (!(vtable [io + l])) {
2022 MonoClass *parent = class->parent;
2024 for (; parent; parent = parent->parent) {
2025 if ((ic->interface_id <= parent->max_interface_id) &&
2026 (parent->interface_offsets [ic->interface_id] != -1) &&
2028 vtable [io + l] = parent->vtable [parent->interface_offsets [ic->interface_id] + l];
2033 if (!(vtable [io + l])) {
2034 for (j = 0; j < onum; ++j) {
2035 g_print (" at slot %d: %s (%d) overrides %s (%d)\n", io+l, overrides [j*2+1]->name,
2036 overrides [j*2+1]->slot, overrides [j*2]->name, overrides [j*2]->slot);
2038 msig = mono_signature_get_desc (mono_method_signature (im), FALSE);
2039 printf ("no implementation for interface method %s::%s(%s) in class %s.%s\n",
2040 mono_type_get_name (&ic->byval_arg), im->name, msig, class->name_space, class->name);
2042 for (j = 0; j < class->method.count; ++j) {
2043 MonoMethod *cm = class->methods [j];
2044 msig = mono_signature_get_desc (mono_method_signature (cm), TRUE);
2046 printf ("METHOD %s(%s)\n", cm->name, msig);
2049 g_assert_not_reached ();
2054 for (l = 0; l < ic->method.count; l++) {
2055 MonoMethod *im = vtable [io + l];
2058 g_assert (io + l <= max_vtsize);
2060 /* FIXME: why do we need this ? */
2062 /* g_assert_not_reached (); */
2068 g_ptr_array_free (ifaces, TRUE);
2071 for (i = 0; i < class->method.count; ++i) {
2074 cm = class->methods [i];
2077 * Non-virtual method have no place in the vtable.
2078 * This also catches static methods (since they are not virtual).
2080 if (!(cm->flags & METHOD_ATTRIBUTE_VIRTUAL))
2084 * If the method is REUSE_SLOT, we must check in the
2085 * base class for a method to override.
2087 if (!(cm->flags & METHOD_ATTRIBUTE_NEW_SLOT)) {
2089 for (k = class->parent; k ; k = k->parent) {
2091 for (j = 0; j < k->method.count; ++j) {
2092 MonoMethod *m1 = k->methods [j];
2093 MonoMethodSignature *cmsig, *m1sig;
2095 if (!(m1->flags & METHOD_ATTRIBUTE_VIRTUAL))
2098 cmsig = mono_method_signature (cm);
2099 m1sig = mono_method_signature (m1);
2101 if (!cmsig || !m1sig) {
2102 mono_class_set_failure (class, MONO_EXCEPTION_TYPE_LOAD, NULL);
2106 if (!strcmp(cm->name, m1->name) &&
2107 mono_metadata_signature_equal (cmsig, m1sig)) {
2109 /* CAS - SecurityAction.InheritanceDemand */
2110 if (security_enabled && (m1->flags & METHOD_ATTRIBUTE_HAS_SECURITY)) {
2111 mono_secman_inheritancedemand_method (cm, m1);
2114 slot = k->methods [j]->slot;
2115 g_assert (cm->slot < max_vtsize);
2117 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
2118 g_hash_table_insert (override_map, m1, cm);
2130 cm->slot = cur_slot++;
2132 if (!(cm->flags & METHOD_ATTRIBUTE_ABSTRACT))
2133 vtable [cm->slot] = cm;
2136 /* override non interface methods */
2137 for (i = 0; i < onum; i++) {
2138 MonoMethod *decl = overrides [i*2];
2139 if (!MONO_CLASS_IS_INTERFACE (decl->klass)) {
2140 g_assert (decl->slot != -1);
2141 vtable [decl->slot] = overrides [i*2 + 1];
2142 overrides [i * 2 + 1]->slot = decl->slot;
2144 override_map = g_hash_table_new (mono_aligned_addr_hash, NULL);
2145 g_hash_table_insert (override_map, decl, overrides [i * 2 + 1]);
2150 * If a method occupies more than one place in the vtable, and it is
2151 * overriden, then change the other occurances too.
2154 for (i = 0; i < max_vtsize; ++i)
2156 MonoMethod *cm = g_hash_table_lookup (override_map, vtable [i]);
2161 g_hash_table_destroy (override_map);
2164 if (class->generic_class) {
2165 MonoClass *gklass = class->generic_class->container_class;
2167 mono_class_init (gklass);
2169 class->vtable_size = MAX (gklass->vtable_size, cur_slot);
2171 class->vtable_size = cur_slot;
2173 class->vtable = mono_mempool_alloc0 (class->image->mempool, sizeof (gpointer) * class->vtable_size);
2174 memcpy (class->vtable, vtable, sizeof (gpointer) * class->vtable_size);
2176 if (mono_print_vtable) {
2179 for (i = 0; i <= max_iid; i++)
2180 if (class->interface_offsets [i] != -1)
2183 printf ("VTable %s (vtable entries = %d, interfaces = %d)\n", mono_type_full_name (&class->byval_arg),
2184 class->vtable_size, icount);
2186 for (i = 0; i < class->vtable_size; ++i) {
2191 printf (" slot assigned: %03d, slot index: %03d %s\n", i, cm->slot,
2192 mono_method_full_name (cm, TRUE));
2198 printf ("Interfaces %s.%s (max_iid = %d)\n", class->name_space,
2199 class->name, max_iid);
2201 for (i = 0; i < class->interface_count; i++) {
2202 ic = class->interfaces [i];
2203 printf (" slot offset: %03d, method count: %03d, iid: %03d %s\n",
2204 class->interface_offsets [ic->interface_id],
2205 ic->method.count, ic->interface_id, mono_type_full_name (&ic->byval_arg));
2208 for (k = class->parent; k ; k = k->parent) {
2209 for (i = 0; i < k->interface_count; i++) {
2210 ic = k->interfaces [i];
2211 printf (" slot offset: %03d, method count: %03d, iid: %03d %s\n",
2212 class->interface_offsets [ic->interface_id],
2213 ic->method.count, ic->interface_id, mono_type_full_name (&ic->byval_arg));
2220 static MonoMethod *default_ghc = NULL;
2221 static MonoMethod *default_finalize = NULL;
2222 static int finalize_slot = -1;
2223 static int ghc_slot = -1;
2226 initialize_object_slots (MonoClass *class)
2231 if (class == mono_defaults.object_class) {
2232 mono_class_setup_vtable (class);
2233 for (i = 0; i < class->vtable_size; ++i) {
2234 MonoMethod *cm = class->vtable [i];
2236 if (!strcmp (cm->name, "GetHashCode"))
2238 else if (!strcmp (cm->name, "Finalize"))
2242 g_assert (ghc_slot > 0);
2243 default_ghc = class->vtable [ghc_slot];
2245 g_assert (finalize_slot > 0);
2246 default_finalize = class->vtable [finalize_slot];
2251 g_list_prepend_mempool (GList* l, MonoMemPool* mp, gpointer datum)
2253 GList* n = mono_mempool_alloc (mp, sizeof (GList));
2262 * @class: the class to initialize
2264 * compute the instance_size, class_size and other infos that cannot be
2265 * computed at mono_class_get() time. Also compute a generic vtable and
2266 * the method slot numbers. We use this infos later to create a domain
2269 * Returns TRUE on success or FALSE if there was a problem in loading
2270 * the type (incorrect assemblies, missing assemblies, methods, etc).
2273 mono_class_init (MonoClass *class)
2276 MonoCachedClassInfo cached_info;
2277 gboolean has_cached_info;
2278 int class_init_ok = TRUE;
2285 /*g_print ("Init class %s\n", class->name);*/
2287 /* We do everything inside the lock to prevent races */
2288 mono_loader_lock ();
2290 if (class->inited) {
2291 mono_loader_unlock ();
2292 /* Somebody might have gotten in before us */
2296 if (class->init_pending) {
2297 mono_loader_unlock ();
2298 /* this indicates a cyclic dependency */
2299 g_error ("pending init %s.%s\n", class->name_space, class->name);
2302 class->init_pending = 1;
2304 /* CAS - SecurityAction.InheritanceDemand */
2305 if (mono_is_security_manager_active () && class->parent && (class->parent->flags & TYPE_ATTRIBUTE_HAS_SECURITY)) {
2306 mono_secman_inheritancedemand_class (class, class->parent);
2309 if (mono_debugger_start_class_init_func)
2310 mono_debugger_start_class_init_func (class);
2312 mono_stats.initialized_class_count++;
2314 if (class->generic_class && !class->generic_class->is_dynamic) {
2315 MonoInflatedGenericClass *gclass;
2318 gclass = mono_get_inflated_generic_class (class->generic_class);
2319 gklass = gclass->generic_class.container_class;
2321 mono_stats.generic_class_count++;
2323 class->method = gklass->method;
2324 class->field = gklass->field;
2326 mono_class_init (gklass);
2327 mono_class_setup_methods (gklass);
2328 mono_class_setup_properties (gklass);
2330 if (MONO_CLASS_IS_INTERFACE (class))
2331 class->interface_id = mono_get_unique_iid (class);
2333 g_assert (class->method.count == gklass->method.count);
2334 class->methods = g_new0 (MonoMethod *, class->method.count);
2336 for (i = 0; i < class->method.count; i++) {
2337 MonoMethod *inflated = mono_class_inflate_generic_method_full (
2338 gklass->methods [i], class, gclass->generic_class.context);
2340 class->methods [i] = mono_get_inflated_method (inflated);
2343 class->property = gklass->property;
2344 class->properties = g_new0 (MonoProperty, class->property.count);
2346 for (i = 0; i < class->property.count; i++) {
2347 MonoProperty *prop = &class->properties [i];
2349 *prop = gklass->properties [i];
2352 prop->get = mono_class_inflate_generic_method_full (
2353 prop->get, class, gclass->generic_class.context);
2355 prop->set = mono_class_inflate_generic_method_full (
2356 prop->set, class, gclass->generic_class.context);
2358 prop->parent = class;
2361 g_assert (class->interface_count == gklass->interface_count);
2364 if (class->parent && !class->parent->inited)
2365 mono_class_init (class->parent);
2367 has_cached_info = mono_class_get_cached_class_info (class, &cached_info);
2369 if (!class->generic_class && (!has_cached_info || (has_cached_info && cached_info.has_nested_classes))) {
2370 i = mono_metadata_nesting_typedef (class->image, class->type_token, 1);
2373 guint32 cols [MONO_NESTED_CLASS_SIZE];
2374 mono_metadata_decode_row (&class->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
2375 nclass = mono_class_create_from_typedef (class->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
2376 class->nested_classes = g_list_prepend_mempool (class->nested_classes, class->image->mempool, nclass);
2378 i = mono_metadata_nesting_typedef (class->image, class->type_token, i + 1);
2383 * Computes the size used by the fields, and their locations
2385 if (has_cached_info) {
2386 class->instance_size = cached_info.instance_size;
2387 class->class_size = cached_info.class_size;
2388 class->packing_size = cached_info.packing_size;
2389 class->min_align = cached_info.min_align;
2390 class->blittable = cached_info.blittable;
2391 class->has_references = cached_info.has_references;
2392 class->has_static_refs = cached_info.has_static_refs;
2395 if (!class->size_inited){
2396 mono_class_setup_fields (class);
2397 if (class->exception_type || mono_loader_get_last_error ()){
2398 class_init_ok = FALSE;
2404 /* initialize method pointers */
2407 MonoMethodSignature *sig;
2408 class->method.count = class->rank > 1? 2: 1;
2409 sig = mono_metadata_signature_alloc (class->image, class->rank);
2410 sig->ret = &mono_defaults.void_class->byval_arg;
2411 sig->pinvoke = TRUE;
2412 for (i = 0; i < class->rank; ++i)
2413 sig->params [i] = &mono_defaults.int32_class->byval_arg;
2415 ctor = (MonoMethod *) mono_mempool_alloc0 (class->image->mempool, sizeof (MonoMethodPInvoke));
2416 ctor->klass = class;
2417 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
2418 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
2419 ctor->signature = sig;
2420 ctor->name = ".ctor";
2422 class->methods = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoMethod*) * class->method.count);
2423 class->methods [0] = ctor;
2424 if (class->rank > 1) {
2425 sig = mono_metadata_signature_alloc (class->image, class->rank * 2);
2426 sig->ret = &mono_defaults.void_class->byval_arg;
2427 sig->pinvoke = TRUE;
2428 for (i = 0; i < class->rank * 2; ++i)
2429 sig->params [i] = &mono_defaults.int32_class->byval_arg;
2431 ctor = (MonoMethod *) mono_mempool_alloc0 (class->image->mempool, sizeof (MonoMethodPInvoke));
2432 ctor->klass = class;
2433 ctor->flags = METHOD_ATTRIBUTE_PUBLIC | METHOD_ATTRIBUTE_RT_SPECIAL_NAME | METHOD_ATTRIBUTE_SPECIAL_NAME;
2434 ctor->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
2435 ctor->signature = sig;
2436 ctor->name = ".ctor";
2438 class->methods [1] = ctor;
2442 mono_class_setup_supertypes (class);
2445 initialize_object_slots (class);
2448 * If possible, avoid the creation of the generic vtable by requesting
2449 * cached info from the runtime.
2451 if (has_cached_info) {
2452 guint32 cur_slot = 0;
2454 class->vtable_size = cached_info.vtable_size;
2455 class->has_finalize = cached_info.has_finalize;
2456 class->ghcimpl = cached_info.ghcimpl;
2457 class->has_cctor = cached_info.has_cctor;
2459 if (class->parent) {
2460 mono_class_init (class->parent);
2461 cur_slot = class->parent->vtable_size;
2464 setup_interface_offsets (class, cur_slot);
2467 mono_class_setup_vtable (class);
2469 if (class->exception_type || mono_loader_get_last_error ()){
2470 class_init_ok = FALSE;
2475 if (class->parent) {
2476 MonoMethod *cmethod = class->vtable [ghc_slot];
2477 if (cmethod->is_inflated)
2478 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
2479 if (cmethod == default_ghc) {
2484 /* Object::Finalize should have empty implemenatation */
2485 class->has_finalize = 0;
2486 if (class->parent) {
2487 MonoMethod *cmethod = class->vtable [finalize_slot];
2488 if (cmethod->is_inflated)
2489 cmethod = ((MonoMethodInflated*)cmethod)->declaring;
2490 if (cmethod != default_finalize) {
2491 class->has_finalize = 1;
2495 for (i = 0; i < class->method.count; ++i) {
2496 MonoMethod *method = class->methods [i];
2497 if ((method->flags & METHOD_ATTRIBUTE_SPECIAL_NAME) &&
2498 (strcmp (".cctor", method->name) == 0)) {
2499 class->has_cctor = 1;
2505 if (MONO_CLASS_IS_INTERFACE (class)) {
2507 * class->interface_offsets is needed for the castclass/isinst code, so
2508 * we have to setup them for interfaces, too.
2510 setup_interface_offsets (class, 0);
2515 class->init_pending = 0;
2517 mono_loader_unlock ();
2519 if (mono_debugger_class_init_func)
2520 mono_debugger_class_init_func (class);
2522 return class_init_ok;
2526 * LOCKING: this assumes the loader lock is held
2529 mono_class_setup_mono_type (MonoClass *class)
2531 const char *name = class->name;
2532 const char *nspace = class->name_space;
2534 class->this_arg.byref = 1;
2535 class->this_arg.data.klass = class;
2536 class->this_arg.type = MONO_TYPE_CLASS;
2537 class->byval_arg.data.klass = class;
2538 class->byval_arg.type = MONO_TYPE_CLASS;
2540 if (!strcmp (nspace, "System")) {
2541 if (!strcmp (name, "ValueType")) {
2543 * do not set the valuetype bit for System.ValueType.
2544 * class->valuetype = 1;
2546 class->blittable = TRUE;
2547 } else if (!strcmp (name, "Enum")) {
2549 * do not set the valuetype bit for System.Enum.
2550 * class->valuetype = 1;
2552 class->valuetype = 0;
2553 class->enumtype = 0;
2554 } else if (!strcmp (name, "Object")) {
2555 class->this_arg.type = class->byval_arg.type = MONO_TYPE_OBJECT;
2556 } else if (!strcmp (name, "String")) {
2557 class->this_arg.type = class->byval_arg.type = MONO_TYPE_STRING;
2558 } else if (!strcmp (name, "TypedReference")) {
2559 class->this_arg.type = class->byval_arg.type = MONO_TYPE_TYPEDBYREF;
2563 if (class->valuetype) {
2564 int t = MONO_TYPE_VALUETYPE;
2565 if (!strcmp (nspace, "System")) {
2568 if (!strcmp (name, "Boolean")) {
2569 t = MONO_TYPE_BOOLEAN;
2570 } else if (!strcmp(name, "Byte")) {
2572 class->blittable = TRUE;
2576 if (!strcmp (name, "Char")) {
2581 if (!strcmp (name, "Double")) {
2583 class->blittable = TRUE;
2587 if (!strcmp (name, "Int32")) {
2589 class->blittable = TRUE;
2590 } else if (!strcmp(name, "Int16")) {
2592 class->blittable = TRUE;
2593 } else if (!strcmp(name, "Int64")) {
2595 class->blittable = TRUE;
2596 } else if (!strcmp(name, "IntPtr")) {
2598 class->blittable = TRUE;
2602 if (!strcmp (name, "Single")) {
2604 class->blittable = TRUE;
2605 } else if (!strcmp(name, "SByte")) {
2607 class->blittable = TRUE;
2611 if (!strcmp (name, "UInt32")) {
2613 class->blittable = TRUE;
2614 } else if (!strcmp(name, "UInt16")) {
2616 class->blittable = TRUE;
2617 } else if (!strcmp(name, "UInt64")) {
2619 class->blittable = TRUE;
2620 } else if (!strcmp(name, "UIntPtr")) {
2622 class->blittable = TRUE;
2626 if (!strcmp (name, "TypedReference")) {
2627 t = MONO_TYPE_TYPEDBYREF;
2628 class->blittable = TRUE;
2632 if (!strcmp (name, "Void")) {
2640 class->this_arg.type = class->byval_arg.type = t;
2643 if (MONO_CLASS_IS_INTERFACE (class))
2644 class->interface_id = mono_get_unique_iid (class);
2649 * LOCKING: this assumes the loader lock is held
2652 mono_class_setup_parent (MonoClass *class, MonoClass *parent)
2654 gboolean system_namespace;
2656 system_namespace = !strcmp (class->name_space, "System");
2658 /* if root of the hierarchy */
2659 if (system_namespace && !strcmp (class->name, "Object")) {
2660 class->parent = NULL;
2661 class->instance_size = sizeof (MonoObject);
2664 if (!strcmp (class->name, "<Module>")) {
2665 class->parent = NULL;
2666 class->instance_size = 0;
2670 if (!MONO_CLASS_IS_INTERFACE (class)) {
2671 /* Imported COM Objects always derive from __ComObject. */
2672 if (MONO_CLASS_IS_IMPORT (class)) {
2673 if (parent == mono_defaults.object_class)
2674 parent = mono_defaults.com_object_class;
2676 class->parent = parent;
2680 g_assert_not_reached (); /* FIXME */
2682 if (parent->generic_class && !parent->name) {
2684 * If the parent is a generic instance, we may get
2685 * called before it is fully initialized, especially
2686 * before it has its name.
2691 class->marshalbyref = parent->marshalbyref;
2692 class->contextbound = parent->contextbound;
2693 class->delegate = parent->delegate;
2694 if (MONO_CLASS_IS_IMPORT (class))
2695 class->is_com_object = 1;
2697 class->is_com_object = parent->is_com_object;
2699 if (system_namespace) {
2700 if (*class->name == 'M' && !strcmp (class->name, "MarshalByRefObject"))
2701 class->marshalbyref = 1;
2703 if (*class->name == 'C' && !strcmp (class->name, "ContextBoundObject"))
2704 class->contextbound = 1;
2706 if (*class->name == 'D' && !strcmp (class->name, "Delegate"))
2707 class->delegate = 1;
2710 if (class->parent->enumtype || ((strcmp (class->parent->name, "ValueType") == 0) &&
2711 (strcmp (class->parent->name_space, "System") == 0)))
2712 class->valuetype = 1;
2713 if (((strcmp (class->parent->name, "Enum") == 0) && (strcmp (class->parent->name_space, "System") == 0))) {
2714 class->valuetype = class->enumtype = 1;
2716 /*class->enumtype = class->parent->enumtype; */
2717 mono_class_setup_supertypes (class);
2719 class->parent = NULL;
2725 * mono_class_setup_supertypes:
2728 * Build the data structure needed to make fast type checks work.
2729 * This currently sets two fields in @class:
2730 * - idepth: distance between @class and System.Object in the type
2732 * - supertypes: array of classes: each element has a class in the hierarchy
2733 * starting from @class up to System.Object
2735 * LOCKING: this assumes the loader lock is held
2738 mono_class_setup_supertypes (MonoClass *class)
2742 if (class->supertypes)
2745 if (class->parent && !class->parent->supertypes)
2746 mono_class_setup_supertypes (class->parent);
2748 class->idepth = class->parent->idepth + 1;
2752 ms = MAX (MONO_DEFAULT_SUPERTABLE_SIZE, class->idepth);
2753 class->supertypes = mono_mempool_alloc0 (class->image->mempool, sizeof (MonoClass *) * ms);
2755 if (class->parent) {
2756 class->supertypes [class->idepth - 1] = class;
2757 memcpy (class->supertypes, class->parent->supertypes, class->parent->idepth * sizeof (gpointer));
2759 class->supertypes [0] = class;
2764 mono_get_shared_generic_inst (MonoGenericContainer *container)
2766 MonoGenericInst *nginst;
2769 nginst = g_new0 (MonoGenericInst, 1);
2770 nginst->type_argc = container->type_argc;
2771 nginst->type_argv = g_new0 (MonoType *, nginst->type_argc);
2772 nginst->is_reference = 1;
2773 nginst->is_open = 1;
2775 for (i = 0; i < nginst->type_argc; i++) {
2776 MonoType *t = g_new0 (MonoType, 1);
2778 t->type = container->is_method ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
2779 t->data.generic_param = &container->type_params [i];
2781 nginst->type_argv [i] = t;
2784 return mono_metadata_lookup_generic_inst (nginst);
2788 * In preparation for implementing shared code.
2791 mono_get_shared_generic_class (MonoGenericContainer *container, gboolean is_dynamic)
2793 MonoInflatedGenericClass *igclass;
2794 MonoGenericClass *gclass;
2797 MonoDynamicGenericClass *dgclass = g_new0 (MonoDynamicGenericClass, 1);
2798 igclass = &dgclass->generic_class;
2799 gclass = &igclass->generic_class;
2800 gclass->is_inflated = 1;
2801 gclass->is_dynamic = 1;
2803 igclass = g_new0 (MonoInflatedGenericClass, 1);
2804 gclass = &igclass->generic_class;
2805 gclass->is_inflated = 1;
2808 gclass->context = &container->context;
2809 gclass->container_class = container->klass;
2810 gclass->inst = mono_get_shared_generic_inst (container);
2813 MonoGenericClass *cached = mono_metadata_lookup_generic_class (gclass);
2821 igclass->klass = container->klass;
2827 * mono_class_create_from_typedef:
2828 * @image: image where the token is valid
2829 * @type_token: typedef token
2831 * Create the MonoClass* representing the specified type token.
2832 * @type_token must be a TypeDef token.
2835 mono_class_create_from_typedef (MonoImage *image, guint32 type_token)
2837 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
2838 MonoClass *class, *parent = NULL;
2839 guint32 cols [MONO_TYPEDEF_SIZE];
2840 guint32 cols_next [MONO_TYPEDEF_SIZE];
2841 guint tidx = mono_metadata_token_index (type_token);
2842 MonoGenericContext *context = NULL;
2843 const char *name, *nspace;
2845 MonoClass **interfaces;
2846 guint32 field_last, method_last;
2847 guint32 nesting_tokeen;
2849 mono_loader_lock ();
2851 if ((class = g_hash_table_lookup (image->class_cache, GUINT_TO_POINTER (type_token)))) {
2852 mono_loader_unlock ();
2856 g_assert (mono_metadata_token_table (type_token) == MONO_TABLE_TYPEDEF);
2858 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
2860 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2861 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2863 class = mono_mempool_alloc0 (image->mempool, sizeof (MonoClass));
2866 class->name_space = nspace;
2868 class->image = image;
2869 class->type_token = type_token;
2870 class->flags = cols [MONO_TYPEDEF_FLAGS];
2872 g_hash_table_insert (image->class_cache, GUINT_TO_POINTER (type_token), class);
2875 * Check whether we're a generic type definition.
2877 class->generic_container = mono_metadata_load_generic_params (image, class->type_token, NULL);
2878 if (class->generic_container) {
2879 class->generic_container->klass = class;
2880 context = &class->generic_container->context;
2882 context->gclass = mono_get_shared_generic_class (context->container, FALSE);
2885 if (cols [MONO_TYPEDEF_EXTENDS]) {
2886 parent = mono_class_get_full (
2887 image, mono_metadata_token_from_dor (cols [MONO_TYPEDEF_EXTENDS]), context);
2888 if (parent == NULL){
2889 g_hash_table_remove (image->class_cache, GUINT_TO_POINTER (type_token));
2890 mono_loader_unlock ();
2895 /* do this early so it's available for interfaces in setup_mono_type () */
2896 if ((nesting_tokeen = mono_metadata_nested_in_typedef (image, type_token)))
2897 class->nested_in = mono_class_create_from_typedef (image, nesting_tokeen);
2899 mono_class_setup_parent (class, parent);
2901 /* uses ->valuetype, which is initialized by mono_class_setup_parent above */
2902 mono_class_setup_mono_type (class);
2904 if (!class->enumtype) {
2905 if (!mono_metadata_interfaces_from_typedef_full (
2906 image, type_token, &interfaces, &icount, context)){
2907 mono_loader_unlock ();
2911 class->interfaces = interfaces;
2912 class->interface_count = icount;
2915 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_UNICODE_CLASS)
2919 if ((class->flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK) == TYPE_ATTRIBUTE_AUTO_CLASS)
2923 class->cast_class = class->element_class = class;
2925 /*g_print ("Load class %s\n", name);*/
2928 * Compute the field and method lists
2930 class->field.first = cols [MONO_TYPEDEF_FIELD_LIST] - 1;
2931 class->method.first = cols [MONO_TYPEDEF_METHOD_LIST] - 1;
2933 if (tt->rows > tidx){
2934 mono_metadata_decode_row (tt, tidx, cols_next, MONO_TYPEDEF_SIZE);
2935 field_last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
2936 method_last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
2938 field_last = image->tables [MONO_TABLE_FIELD].rows;
2939 method_last = image->tables [MONO_TABLE_METHOD].rows;
2942 if (cols [MONO_TYPEDEF_FIELD_LIST] &&
2943 cols [MONO_TYPEDEF_FIELD_LIST] <= image->tables [MONO_TABLE_FIELD].rows)
2944 class->field.count = field_last - class->field.first;
2946 class->field.count = 0;
2948 if (cols [MONO_TYPEDEF_METHOD_LIST] <= image->tables [MONO_TABLE_METHOD].rows)
2949 class->method.count = method_last - class->method.first;
2951 class->method.count = 0;
2953 /* reserve space to store vector pointer in arrays */
2954 if (!strcmp (nspace, "System") && !strcmp (name, "Array")) {
2955 class->instance_size += 2 * sizeof (gpointer);
2956 g_assert (class->field.count == 0);
2959 if (class->enumtype) {
2960 class->enum_basetype = mono_class_find_enum_basetype (class);
2961 class->cast_class = class->element_class = mono_class_from_mono_type (class->enum_basetype);
2965 * If we're a generic type definition, load the constraints.
2966 * We must do this after the class has been constructed to make certain recursive scenarios
2969 if (class->generic_container)
2970 mono_metadata_load_generic_param_constraints (
2971 image, type_token, class->generic_container);
2973 mono_loader_unlock ();
2978 /** is klass Nullable<T>? */
2980 mono_class_is_nullable (MonoClass *klass)
2982 return klass->generic_class != NULL &&
2983 klass->generic_class->container_class == mono_defaults.generic_nullable_class;
2987 /** if klass is T? return T */
2989 mono_class_get_nullable_param (MonoClass *klass)
2991 g_assert (mono_class_is_nullable (klass));
2992 return mono_class_from_mono_type (klass->generic_class->inst->type_argv [0]);
2996 * Create the `MonoClass' for an instantiation of a generic type.
2997 * We only do this if we actually need it.
3000 mono_class_create_generic (MonoInflatedGenericClass *gclass)
3002 MonoClass *klass, *gklass;
3005 mono_loader_lock ();
3006 if (gclass->is_initialized) {
3007 mono_loader_unlock ();
3012 gclass->klass = g_malloc0 (sizeof (MonoClass));
3013 klass = gclass->klass;
3015 gklass = gclass->generic_class.container_class;
3017 if (gklass->nested_in) {
3019 * FIXME: the nested type context should include everything the
3020 * nesting context should have, but it may also have additional
3021 * generic parameters...
3023 MonoType *inflated = mono_class_inflate_generic_type (
3024 &gklass->nested_in->byval_arg, gclass->generic_class.context);
3025 klass->nested_in = mono_class_from_mono_type (inflated);
3028 klass->name = gklass->name;
3029 klass->name_space = gklass->name_space;
3030 klass->image = gklass->image;
3031 klass->flags = gklass->flags;
3032 klass->type_token = gklass->type_token;
3034 klass->generic_class = &gclass->generic_class;
3036 klass->this_arg.type = klass->byval_arg.type = MONO_TYPE_GENERICINST;
3037 klass->this_arg.data.generic_class = klass->byval_arg.data.generic_class = &gclass->generic_class;
3038 klass->this_arg.byref = TRUE;
3040 klass->cast_class = klass->element_class = klass;
3042 if (mono_class_is_nullable (klass))
3043 klass->cast_class = klass->element_class = mono_class_get_nullable_param (klass);
3045 if (gclass->generic_class.is_dynamic) {
3046 klass->instance_size = gklass->instance_size;
3047 klass->class_size = gklass->class_size;
3048 klass->size_inited = 1;
3051 klass->valuetype = gklass->valuetype;
3053 mono_class_setup_supertypes (klass);
3056 klass->interface_count = gklass->interface_count;
3057 klass->interfaces = g_new0 (MonoClass *, klass->interface_count);
3058 for (i = 0; i < klass->interface_count; i++) {
3059 MonoType *it = &gklass->interfaces [i]->byval_arg;
3060 MonoType *inflated = mono_class_inflate_generic_type (
3061 it, gclass->generic_class.context);
3062 klass->interfaces [i] = mono_class_from_mono_type (inflated);
3065 i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, 1);
3068 guint32 cols [MONO_NESTED_CLASS_SIZE];
3069 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_NESTEDCLASS], i - 1, cols, MONO_NESTED_CLASS_SIZE);
3070 nclass = mono_class_create_from_typedef (klass->image, MONO_TOKEN_TYPE_DEF | cols [MONO_NESTED_CLASS_NESTED]);
3071 klass->nested_classes = g_list_prepend (klass->nested_classes, nclass);
3073 i = mono_metadata_nesting_typedef (klass->image, gklass->type_token, i + 1);
3076 if (gklass->parent) {
3077 MonoType *inflated = mono_class_inflate_generic_type (
3078 &gklass->parent->byval_arg, gclass->generic_class.context);
3080 klass->parent = mono_class_from_mono_type (inflated);
3084 mono_class_setup_parent (klass, klass->parent);
3086 if (MONO_CLASS_IS_INTERFACE (klass))
3087 setup_interface_offsets (klass, 0);
3088 gclass->is_initialized = TRUE;
3089 mono_loader_unlock ();
3093 mono_class_from_generic_parameter (MonoGenericParam *param, MonoImage *image, gboolean is_mvar)
3095 MonoClass *klass, **ptr;
3099 return param->pklass;
3101 klass = param->pklass = g_new0 (MonoClass, 1);
3103 for (count = 0, ptr = param->constraints; ptr && *ptr; ptr++, count++)
3107 if ((count > 0) && !MONO_CLASS_IS_INTERFACE (param->constraints [0])) {
3108 klass->parent = param->constraints [0];
3110 } else if (param->flags & GENERIC_PARAMETER_ATTRIBUTE_VALUE_TYPE_CONSTRAINT)
3111 klass->parent = mono_class_from_name (mono_defaults.corlib, "System", "ValueType");
3113 klass->parent = mono_defaults.object_class;
3115 if (count - pos > 0) {
3116 klass->interface_count = count - pos;
3117 klass->interfaces = g_new0 (MonoClass *, count - pos);
3118 for (i = pos; i < count; i++)
3119 klass->interfaces [i - pos] = param->constraints [i];
3123 klass->name = param->name;
3125 klass->name = g_strdup_printf (is_mvar ? "!!%d" : "!%d", param->num);
3127 klass->name_space = "";
3130 klass->image = image;
3131 else if (is_mvar && param->method && param->method->klass)
3132 klass->image = param->method->klass->image;
3133 else if (param->owner && param->owner->klass)
3134 klass->image = param->owner->klass->image;
3136 klass->image = mono_defaults.corlib;
3138 klass->inited = TRUE;
3139 klass->cast_class = klass->element_class = klass;
3140 klass->enum_basetype = &klass->element_class->byval_arg;
3141 klass->flags = TYPE_ATTRIBUTE_PUBLIC;
3143 klass->this_arg.type = klass->byval_arg.type = is_mvar ? MONO_TYPE_MVAR : MONO_TYPE_VAR;
3144 klass->this_arg.data.generic_param = klass->byval_arg.data.generic_param = param;
3145 klass->this_arg.byref = TRUE;
3147 mono_class_setup_supertypes (klass);
3153 mono_ptr_class_get (MonoType *type)
3156 MonoClass *el_class;
3160 el_class = mono_class_from_mono_type (type);
3161 image = el_class->image;
3163 mono_loader_lock ();
3165 if (!image->ptr_cache)
3166 image->ptr_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
3168 if ((result = g_hash_table_lookup (image->ptr_cache, el_class))) {
3169 mono_loader_unlock ();
3172 result = mono_mempool_alloc0 (image->mempool, sizeof (MonoClass));
3174 result->parent = NULL; /* no parent for PTR types */
3175 result->name_space = el_class->name_space;
3176 name = g_strdup_printf ("%s*", el_class->name);
3177 result->name = mono_mempool_strdup (image->mempool, name);
3179 result->image = el_class->image;
3180 result->inited = TRUE;
3181 result->flags = TYPE_ATTRIBUTE_CLASS | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
3182 /* Can pointers get boxed? */
3183 result->instance_size = sizeof (gpointer);
3184 result->cast_class = result->element_class = el_class;
3185 result->enum_basetype = &result->element_class->byval_arg;
3186 result->blittable = TRUE;
3188 result->this_arg.type = result->byval_arg.type = MONO_TYPE_PTR;
3189 result->this_arg.data.type = result->byval_arg.data.type = result->enum_basetype;
3190 result->this_arg.byref = TRUE;
3192 mono_class_setup_supertypes (result);
3194 g_hash_table_insert (image->ptr_cache, el_class, result);
3196 mono_loader_unlock ();
3202 mono_fnptr_class_get (MonoMethodSignature *sig)
3205 static GHashTable *ptr_hash = NULL;
3207 /* FIXME: These should be allocate from a mempool as well, but which one ? */
3209 mono_loader_lock ();
3212 ptr_hash = g_hash_table_new (mono_aligned_addr_hash, NULL);
3214 if ((result = g_hash_table_lookup (ptr_hash, sig))) {
3215 mono_loader_unlock ();
3218 result = g_new0 (MonoClass, 1);
3220 result->parent = NULL; /* no parent for PTR types */
3221 result->name_space = "System";
3222 result->name = "MonoFNPtrFakeClass";
3223 result->image = mono_defaults.corlib; /* need to fix... */
3224 result->inited = TRUE;
3225 result->flags = TYPE_ATTRIBUTE_CLASS; /* | (el_class->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK); */
3226 /* Can pointers get boxed? */
3227 result->instance_size = sizeof (gpointer);
3228 result->cast_class = result->element_class = result;
3229 result->blittable = TRUE;
3231 result->this_arg.type = result->byval_arg.type = MONO_TYPE_FNPTR;
3232 result->this_arg.data.method = result->byval_arg.data.method = sig;
3233 result->this_arg.byref = TRUE;
3234 result->enum_basetype = &result->element_class->byval_arg;
3235 result->blittable = TRUE;
3237 mono_class_setup_supertypes (result);
3239 g_hash_table_insert (ptr_hash, sig, result);
3241 mono_loader_unlock ();
3247 mono_class_from_mono_type (MonoType *type)
3249 switch (type->type) {
3250 case MONO_TYPE_OBJECT:
3251 return type->data.klass? type->data.klass: mono_defaults.object_class;
3252 case MONO_TYPE_VOID:
3253 return type->data.klass? type->data.klass: mono_defaults.void_class;
3254 case MONO_TYPE_BOOLEAN:
3255 return type->data.klass? type->data.klass: mono_defaults.boolean_class;
3256 case MONO_TYPE_CHAR:
3257 return type->data.klass? type->data.klass: mono_defaults.char_class;
3259 return type->data.klass? type->data.klass: mono_defaults.sbyte_class;
3261 return type->data.klass? type->data.klass: mono_defaults.byte_class;
3263 return type->data.klass? type->data.klass: mono_defaults.int16_class;
3265 return type->data.klass? type->data.klass: mono_defaults.uint16_class;
3267 return type->data.klass? type->data.klass: mono_defaults.int32_class;
3269 return type->data.klass? type->data.klass: mono_defaults.uint32_class;
3271 return type->data.klass? type->data.klass: mono_defaults.int_class;
3273 return type->data.klass? type->data.klass: mono_defaults.uint_class;
3275 return type->data.klass? type->data.klass: mono_defaults.int64_class;
3277 return type->data.klass? type->data.klass: mono_defaults.uint64_class;
3279 return type->data.klass? type->data.klass: mono_defaults.single_class;
3281 return type->data.klass? type->data.klass: mono_defaults.double_class;
3282 case MONO_TYPE_STRING:
3283 return type->data.klass? type->data.klass: mono_defaults.string_class;
3284 case MONO_TYPE_TYPEDBYREF:
3285 return type->data.klass? type->data.klass: mono_defaults.typed_reference_class;
3286 case MONO_TYPE_ARRAY:
3287 return mono_bounded_array_class_get (type->data.array->eklass, type->data.array->rank, TRUE);
3289 return mono_ptr_class_get (type->data.type);
3290 case MONO_TYPE_FNPTR:
3291 return mono_fnptr_class_get (type->data.method);
3292 case MONO_TYPE_SZARRAY:
3293 return mono_array_class_get (type->data.klass, 1);
3294 case MONO_TYPE_CLASS:
3295 case MONO_TYPE_VALUETYPE:
3296 return type->data.klass;
3297 case MONO_TYPE_GENERICINST: {
3298 MonoInflatedGenericClass *gclass;
3299 gclass = mono_get_inflated_generic_class (type->data.generic_class);
3300 g_assert (gclass->klass);
3301 return gclass->klass;
3304 return mono_class_from_generic_parameter (type->data.generic_param, NULL, FALSE);
3305 case MONO_TYPE_MVAR:
3306 return mono_class_from_generic_parameter (type->data.generic_param, NULL, TRUE);
3308 g_warning ("implement me 0x%02x\n", type->type);
3309 g_assert_not_reached ();
3316 * @image: context where the image is created
3317 * @type_spec: typespec token
3318 * @context: the generic context used to evaluate generic instantiations in
3321 mono_class_create_from_typespec (MonoImage *image, guint32 type_spec, MonoGenericContext *context)
3323 MonoType *t = mono_type_create_from_typespec (image, type_spec);
3326 if (context && (context->gclass || context->gmethod)) {
3327 MonoType *inflated = inflate_generic_type (t, context);
3331 return mono_class_from_mono_type (t);
3335 * mono_bounded_array_class_get:
3336 * @element_class: element class
3337 * @rank: the dimension of the array class
3338 * @bounded: whenever the array has non-zero bounds
3340 * Returns: a class object describing the array with element type @element_type and
3344 mono_bounded_array_class_get (MonoClass *eclass, guint32 rank, gboolean bounded)
3348 MonoClass *parent = NULL;
3349 GSList *list, *rootlist;
3352 gboolean corlib_type = FALSE;
3354 g_assert (rank <= 255);
3357 /* bounded only matters for one-dimensional arrays */
3360 image = eclass->image;
3362 mono_loader_lock ();
3364 if (!image->array_cache)
3365 image->array_cache = g_hash_table_new (mono_aligned_addr_hash, NULL);
3367 if ((rootlist = list = g_hash_table_lookup (image->array_cache, eclass))) {
3368 for (; list; list = list->next) {
3370 if ((class->rank == rank) && (class->byval_arg.type == (((rank > 1) || bounded) ? MONO_TYPE_ARRAY : MONO_TYPE_SZARRAY))) {
3371 mono_loader_unlock ();
3377 /* for the building corlib use System.Array from it */
3378 if (image->assembly && image->assembly->dynamic && image->assembly_name && strcmp (image->assembly_name, "mscorlib") == 0) {
3379 parent = mono_class_from_name (image, "System", "Array");
3381 } else if (mono_defaults.generic_array_class) {
3382 MonoType *inflated, **args;
3384 args = g_new0 (MonoType *, 1);
3385 args [0] = &eclass->byval_arg;
3387 inflated = mono_class_bind_generic_parameters (
3388 &mono_defaults.generic_array_class->byval_arg, 1, args);
3389 parent = mono_class_from_mono_type (inflated);
3391 if (!parent->inited)
3392 mono_class_init (parent);
3394 parent = mono_defaults.array_class;
3395 if (!parent->inited)
3396 mono_class_init (parent);
3399 class = mono_mempool_alloc0 (image->mempool, sizeof (MonoClass));
3401 class->image = image;
3402 class->name_space = eclass->name_space;
3403 nsize = strlen (eclass->name);
3404 name = g_malloc (nsize + 2 + rank);
3405 memcpy (name, eclass->name, nsize);
3408 memset (name + nsize + 1, ',', rank - 1);
3409 name [nsize + rank] = ']';
3410 name [nsize + rank + 1] = 0;
3411 class->name = mono_mempool_strdup (image->mempool, name);
3413 class->type_token = 0;
3414 /* all arrays are marked serializable and sealed, bug #42779 */
3415 class->flags = TYPE_ATTRIBUTE_CLASS | TYPE_ATTRIBUTE_SERIALIZABLE | TYPE_ATTRIBUTE_SEALED |
3416 (eclass->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK);
3417 class->parent = parent;
3418 class->instance_size = mono_class_instance_size (class->parent);
3419 class->class_size = 0;
3420 mono_class_setup_supertypes (class);
3421 if (eclass->generic_class)
3422 mono_class_init (eclass);
3423 if (!eclass->size_inited)
3424 mono_class_setup_fields (eclass);
3425 class->has_references = MONO_TYPE_IS_REFERENCE (&eclass->byval_arg) || eclass->has_references? TRUE: FALSE;
3429 if (eclass->enumtype)
3430 class->cast_class = eclass->element_class;
3432 class->cast_class = eclass;
3434 class->element_class = eclass;
3436 if ((rank > 1) || bounded) {
3437 MonoArrayType *at = mono_mempool_alloc0 (image->mempool, sizeof (MonoArrayType));
3438 class->byval_arg.type = MONO_TYPE_ARRAY;
3439 class->byval_arg.data.array = at;
3440 at->eklass = eclass;
3442 /* FIXME: complete.... */
3444 class->byval_arg.type = MONO_TYPE_SZARRAY;
3445 class->byval_arg.data.klass = eclass;
3447 class->this_arg = class->byval_arg;
3448 class->this_arg.byref = 1;
3453 class->generic_container = eclass->generic_container;
3455 list = g_slist_append (rootlist, class);
3456 g_hash_table_insert (image->array_cache, eclass, list);
3458 mono_loader_unlock ();
3464 * mono_array_class_get:
3465 * @element_class: element class
3466 * @rank: the dimension of the array class
3468 * Returns: a class object describing the array with element type @element_type and
3472 mono_array_class_get (MonoClass *eclass, guint32 rank)
3474 return mono_bounded_array_class_get (eclass, rank, FALSE);
3478 * mono_class_instance_size:
3481 * Returns: the size of an object instance
3484 mono_class_instance_size (MonoClass *klass)
3486 if (!klass->size_inited)
3487 mono_class_init (klass);
3489 return klass->instance_size;
3493 * mono_class_min_align:
3496 * Returns: minimm alignment requirements
3499 mono_class_min_align (MonoClass *klass)
3501 if (!klass->size_inited)
3502 mono_class_init (klass);
3504 return klass->min_align;
3508 * mono_class_value_size:
3511 * This function is used for value types, and return the
3512 * space and the alignment to store that kind of value object.
3514 * Returns: the size of a value of kind @klass
3517 mono_class_value_size (MonoClass *klass, guint32 *align)
3521 /* fixme: check disable, because we still have external revereces to
3522 * mscorlib and Dummy Objects
3524 /*g_assert (klass->valuetype);*/
3526 size = mono_class_instance_size (klass) - sizeof (MonoObject);
3529 *align = klass->min_align;
3535 * mono_class_data_size:
3538 * Returns: the size of the static class data
3541 mono_class_data_size (MonoClass *klass)
3544 mono_class_init (klass);
3546 return klass->class_size;
3550 * Auxiliary routine to mono_class_get_field
3552 * Takes a field index instead of a field token.
3554 static MonoClassField *
3555 mono_class_get_field_idx (MonoClass *class, int idx)
3557 mono_class_setup_fields_locking (class);
3560 if (class->field.count) {
3561 if ((idx >= class->field.first) && (idx < class->field.first + class->field.count)){
3562 return &class->fields [idx - class->field.first];
3565 class = class->parent;
3571 * mono_class_get_field:
3572 * @class: the class to lookup the field.
3573 * @field_token: the field token
3575 * Returns: A MonoClassField representing the type and offset of
3576 * the field, or a NULL value if the field does not belong to this
3580 mono_class_get_field (MonoClass *class, guint32 field_token)
3582 int idx = mono_metadata_token_index (field_token);
3584 g_assert (mono_metadata_token_code (field_token) == MONO_TOKEN_FIELD_DEF);
3586 return mono_class_get_field_idx (class, idx - 1);
3590 * mono_class_get_field_from_name:
3591 * @klass: the class to lookup the field.
3592 * @name: the field name
3594 * Search the class @klass and it's parents for a field with the name @name.
3596 * Returns: the MonoClassField pointer of the named field or NULL
3599 mono_class_get_field_from_name (MonoClass *klass, const char *name)
3603 mono_class_setup_fields_locking (klass);
3605 for (i = 0; i < klass->field.count; ++i) {
3606 if (strcmp (name, klass->fields [i].name) == 0)
3607 return &klass->fields [i];
3609 klass = klass->parent;
3615 * mono_class_get_field_token:
3616 * @field: the field we need the token of
3618 * Get the token of a field. Note that the tokesn is only valid for the image
3619 * the field was loaded from. Don't use this function for fields in dynamic types.
3621 * Returns: the token representing the field in the image it was loaded from.
3624 mono_class_get_field_token (MonoClassField *field)
3626 MonoClass *klass = field->parent;
3629 mono_class_setup_fields_locking (klass);
3631 for (i = 0; i < klass->field.count; ++i) {
3632 if (&klass->fields [i] == field)
3633 return mono_metadata_make_token (MONO_TABLE_FIELD, klass->field.first + i + 1);
3635 klass = klass->parent;
3638 g_assert_not_reached ();
3643 mono_class_get_event_token (MonoEvent *event)
3645 MonoClass *klass = event->parent;
3649 for (i = 0; i < klass->event.count; ++i) {
3650 if (&klass->events [i] == event)
3651 return mono_metadata_make_token (MONO_TABLE_EVENT, klass->event.first + i + 1);
3653 klass = klass->parent;
3656 g_assert_not_reached ();
3661 mono_class_get_property_from_name (MonoClass *klass, const char *name)
3665 gpointer iter = NULL;
3666 while ((p = mono_class_get_properties (klass, &iter))) {
3667 if (! strcmp (name, p->name))
3670 klass = klass->parent;
3676 mono_class_get_property_token (MonoProperty *prop)
3678 MonoClass *klass = prop->parent;
3682 gpointer iter = NULL;
3683 while ((p = mono_class_get_properties (klass, &iter))) {
3684 if (&klass->properties [i] == prop)
3685 return mono_metadata_make_token (MONO_TABLE_PROPERTY, klass->property.first + i + 1);
3689 klass = klass->parent;
3692 g_assert_not_reached ();
3697 mono_class_name_from_token (MonoImage *image, guint32 type_token)
3699 const char *name, *nspace;
3701 return g_strdup_printf ("DynamicType 0x%08x", type_token);
3703 switch (type_token & 0xff000000){
3704 case MONO_TOKEN_TYPE_DEF: {
3705 guint32 cols [MONO_TYPEDEF_SIZE];
3706 MonoTableInfo *tt = &image->tables [MONO_TABLE_TYPEDEF];
3707 guint tidx = mono_metadata_token_index (type_token);
3709 mono_metadata_decode_row (tt, tidx - 1, cols, MONO_TYPEDEF_SIZE);
3710 name = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3711 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3712 if (strlen (nspace) == 0)
3713 return g_strdup_printf ("%s", name);
3715 return g_strdup_printf ("%s.%s", nspace, name);
3718 case MONO_TOKEN_TYPE_REF: {
3719 guint32 cols [MONO_TYPEREF_SIZE];
3720 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
3722 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
3723 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
3724 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
3725 if (strlen (nspace) == 0)
3726 return g_strdup_printf ("%s", name);
3728 return g_strdup_printf ("%s.%s", nspace, name);
3731 case MONO_TOKEN_TYPE_SPEC:
3732 return g_strdup_printf ("Typespec 0x%08x", type_token);
3734 g_assert_not_reached ();
3741 mono_assembly_name_from_token (MonoImage *image, guint32 type_token)
3744 return g_strdup_printf ("DynamicAssembly %s", image->name);
3746 switch (type_token & 0xff000000){
3747 case MONO_TOKEN_TYPE_DEF:
3748 return mono_stringify_assembly_name (&image->assembly->aname);
3750 case MONO_TOKEN_TYPE_REF: {
3751 MonoAssemblyName aname;
3752 guint32 cols [MONO_TYPEREF_SIZE];
3753 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEREF];
3756 mono_metadata_decode_row (t, (type_token&0xffffff)-1, cols, MONO_TYPEREF_SIZE);
3758 idx = cols [MONO_TYPEREF_SCOPE] >> MONO_RESOLTION_SCOPE_BITS;
3759 switch (cols [MONO_TYPEREF_SCOPE] & MONO_RESOLTION_SCOPE_MASK) {
3760 case MONO_RESOLTION_SCOPE_MODULE:
3762 return g_strdup ("");
3763 case MONO_RESOLTION_SCOPE_MODULEREF:
3765 return g_strdup ("");
3766 case MONO_RESOLTION_SCOPE_TYPEREF:
3768 return g_strdup ("");
3769 case MONO_RESOLTION_SCOPE_ASSEMBLYREF:
3770 mono_assembly_get_assemblyref (image, idx - 1, &aname);
3771 return mono_stringify_assembly_name (&aname);
3773 g_assert_not_reached ();
3777 case MONO_TOKEN_TYPE_SPEC:
3779 return g_strdup ("");
3781 g_assert_not_reached ();
3788 * mono_class_get_full:
3789 * @image: the image where the class resides
3790 * @type_token: the token for the class
3791 * @context: the generic context used to evaluate generic instantiations in
3793 * Returns: the MonoClass that represents @type_token in @image
3796 mono_class_get_full (MonoImage *image, guint32 type_token, MonoGenericContext *context)
3798 MonoClass *class = NULL;
3801 return mono_lookup_dynamic_token (image, type_token);
3803 switch (type_token & 0xff000000){
3804 case MONO_TOKEN_TYPE_DEF:
3805 class = mono_class_create_from_typedef (image, type_token);
3807 case MONO_TOKEN_TYPE_REF:
3808 class = mono_class_from_typeref (image, type_token);
3810 case MONO_TOKEN_TYPE_SPEC:
3811 class = mono_class_create_from_typespec (image, type_token, context);
3814 g_warning ("unknown token type %x", type_token & 0xff000000);
3815 g_assert_not_reached ();
3819 char *name = mono_class_name_from_token (image, type_token);
3820 char *assembly = mono_assembly_name_from_token (image, type_token);
3821 mono_loader_set_error_type_load (name, assembly);
3828 mono_class_get (MonoImage *image, guint32 type_token)
3830 return mono_class_get_full (image, type_token, NULL);
3839 find_nocase (gpointer key, gpointer value, gpointer user_data)
3841 char *name = (char*)key;
3842 FindUserData *data = (FindUserData*)user_data;
3844 if (!data->value && (g_strcasecmp (name, (char*)data->key) == 0))
3845 data->value = value;
3849 * mono_class_from_name_case:
3850 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3851 * @name_space: the type namespace
3852 * @name: the type short name.
3854 * Obtains a MonoClass with a given namespace and a given name which
3855 * is located in the given MonoImage. The namespace and name
3856 * lookups are case insensitive.
3858 * You can also pass @NULL to the image, and that will lookup for
3859 * a type with the given namespace and name in all of the loaded
3860 * assemblies: notice that since there might be a name clash in this
3861 * case, passing @NULL is not encouraged if you need a precise type.
3865 mono_class_from_name_case (MonoImage *image, const char* name_space, const char *name)
3867 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
3868 guint32 cols [MONO_TYPEDEF_SIZE];
3873 if (image->dynamic) {
3875 FindUserData user_data;
3877 mono_loader_lock ();
3879 user_data.key = name_space;
3880 user_data.value = NULL;
3881 g_hash_table_foreach (image->name_cache, find_nocase, &user_data);
3883 if (user_data.value) {
3884 GHashTable *nspace_table = (GHashTable*)user_data.value;
3886 user_data.key = name;
3887 user_data.value = NULL;
3889 g_hash_table_foreach (nspace_table, find_nocase, &user_data);
3891 if (user_data.value)
3892 token = GPOINTER_TO_UINT (user_data.value);
3895 mono_loader_unlock ();
3898 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | token);
3904 /* add a cache if needed */
3905 for (i = 1; i <= t->rows; ++i) {
3906 mono_metadata_decode_row (t, i - 1, cols, MONO_TYPEDEF_SIZE);
3907 /* nested types are accessed from the nesting name */
3908 visib = cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_VISIBILITY_MASK;
3909 if (visib > TYPE_ATTRIBUTE_PUBLIC && visib <= TYPE_ATTRIBUTE_NESTED_ASSEMBLY)
3911 n = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
3912 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
3913 if (g_strcasecmp (n, name) == 0 && g_strcasecmp (nspace, name_space) == 0)
3914 return mono_class_get (image, MONO_TOKEN_TYPE_DEF | i);
3920 return_nested_in (MonoClass *class, char *nested) {
3922 char *s = strchr (nested, '/');
3929 for (tmp = class->nested_classes; tmp; tmp = tmp->next) {
3931 if (strcmp (found->name, nested) == 0) {
3933 return return_nested_in (found, s);
3942 * mono_class_from_name:
3943 * @image: The MonoImage where the type is looked up in, or NULL for looking up in all loaded assemblies
3944 * @name_space: the type namespace
3945 * @name: the type short name.
3947 * Obtains a MonoClass with a given namespace and a given name which
3948 * is located in the given MonoImage.
3950 * You can also pass `NULL' to the image, and that will lookup for
3951 * a type with the given namespace and name in all of the loaded
3952 * assemblies: notice that since there might be a name clash in this
3953 * case, passing NULL is not encouraged if you need a precise type.
3957 mono_class_from_name (MonoImage *image, const char* name_space, const char *name)
3959 GHashTable *nspace_table;
3960 MonoImage *loaded_image;
3966 if ((nested = strchr (name, '/'))) {
3967 int pos = nested - name;
3968 int len = strlen (name);
3971 memcpy (buf, name, len + 1);
3973 nested = buf + pos + 1;
3977 mono_loader_lock ();
3979 nspace_table = g_hash_table_lookup (image->name_cache, name_space);
3982 token = GPOINTER_TO_UINT (g_hash_table_lookup (nspace_table, name));
3984 mono_loader_unlock ();
3989 if (mono_metadata_token_table (token) == MONO_TABLE_EXPORTEDTYPE) {
3990 MonoTableInfo *t = &image->tables [MONO_TABLE_EXPORTEDTYPE];
3991 guint32 cols [MONO_EXP_TYPE_SIZE];
3994 idx = mono_metadata_token_index (token);
3996 mono_metadata_decode_row (t, idx - 1, cols, MONO_EXP_TYPE_SIZE);
3998 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
3999 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
4000 loaded_image = mono_assembly_load_module (image->assembly, impl >> MONO_IMPLEMENTATION_BITS);
4003 class = mono_class_from_name (loaded_image, name_space, name);
4005 return return_nested_in (class, nested);
4007 } else if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_ASSEMBLYREF) {
4008 MonoAssembly **references = image->references;
4009 if (!references [idx - 1])
4010 mono_assembly_load_reference (image, idx - 1);
4011 g_assert (references == image->references);
4012 g_assert (references [idx - 1]);
4013 if (references [idx - 1] == (gpointer)-1)
4016 /* FIXME: Cycle detection */
4017 return mono_class_from_name (references [idx - 1]->image, name_space, name);
4019 g_error ("not yet implemented");
4023 token = MONO_TOKEN_TYPE_DEF | token;
4025 class = mono_class_get (image, token);
4027 return return_nested_in (class, nested);
4032 mono_class_is_subclass_of (MonoClass *klass, MonoClass *klassc,
4033 gboolean check_interfaces)
4035 g_assert (klassc->idepth > 0);
4036 if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && !MONO_CLASS_IS_INTERFACE (klass)) {
4037 if ((klassc->interface_id <= klass->max_interface_id) &&
4038 (klass->interface_offsets [klassc->interface_id] >= 0))
4040 } else if (check_interfaces && MONO_CLASS_IS_INTERFACE (klassc) && MONO_CLASS_IS_INTERFACE (klass)) {
4043 for (i = 0; i < klass->interface_count; i ++) {
4044 MonoClass *ic = klass->interfaces [i];
4049 if (!MONO_CLASS_IS_INTERFACE (klass) && mono_class_has_parent (klass, klassc))
4054 * MS.NET thinks interfaces are a subclass of Object, so we think it as
4057 if (klassc == mono_defaults.object_class)
4064 mono_class_is_assignable_from (MonoClass *klass, MonoClass *oklass)
4067 mono_class_init (klass);
4069 if (!oklass->inited)
4070 mono_class_init (oklass);
4072 if (MONO_CLASS_IS_INTERFACE (klass)) {
4073 if ((oklass->byval_arg.type == MONO_TYPE_VAR) || (oklass->byval_arg.type == MONO_TYPE_MVAR))
4076 /* interface_offsets might not be set for dynamic classes */
4077 if (oklass->reflection_info && !oklass->interface_offsets)
4079 * oklass might be a generic type parameter but they have
4080 * interface_offsets set.
4082 return mono_reflection_call_is_assignable_to (oklass, klass);
4084 if ((klass->interface_id <= oklass->max_interface_id) &&
4085 (oklass->interface_offsets [klass->interface_id] != -1))
4087 } else if (klass->rank) {
4088 MonoClass *eclass, *eoclass;
4090 if (oklass->rank != klass->rank)
4093 /* vectors vs. one dimensional arrays */
4094 if (oklass->byval_arg.type != klass->byval_arg.type)
4097 eclass = klass->cast_class;
4098 eoclass = oklass->cast_class;
4101 * a is b does not imply a[] is b[] when a is a valuetype, and
4102 * b is a reference type.
4105 if (eoclass->valuetype) {
4106 if ((eclass == mono_defaults.enum_class) ||
4107 (eclass == mono_defaults.enum_class->parent) ||
4108 (eclass == mono_defaults.object_class))
4112 return mono_class_is_assignable_from (klass->cast_class, oklass->cast_class);
4113 } else if (mono_class_is_nullable (klass))
4114 return (mono_class_is_assignable_from (klass->cast_class, oklass));
4115 else if (klass == mono_defaults.object_class)
4118 return mono_class_has_parent (oklass, klass);
4122 * mono_class_get_cctor:
4124 * Returns the static constructor of @klass if it exists, NULL otherwise.
4127 mono_class_get_cctor (MonoClass *klass)
4129 MonoCachedClassInfo cached_info;
4131 if (!klass->has_cctor)
4134 if (mono_class_get_cached_class_info (klass, &cached_info))
4135 return mono_get_method (klass->image, cached_info.cctor_token, klass);
4137 return mono_class_get_method_from_name_flags (klass, ".cctor", -1, METHOD_ATTRIBUTE_SPECIAL_NAME);
4141 * mono_class_get_finalizer:
4143 * Returns the finalizer method of @klass if it exists, NULL otherwise.
4146 mono_class_get_finalizer (MonoClass *klass)
4148 MonoCachedClassInfo cached_info;
4151 mono_class_init (klass);
4152 if (!klass->has_finalize)
4155 if (mono_class_get_cached_class_info (klass, &cached_info))
4156 return mono_get_method (cached_info.finalize_image, cached_info.finalize_token, NULL);
4158 mono_class_setup_vtable (klass);
4159 return klass->vtable [finalize_slot];
4164 * mono_class_needs_cctor_run:
4166 * Determines whenever the class has a static constructor and whenever it
4167 * needs to be called when executing CALLER.
4170 mono_class_needs_cctor_run (MonoClass *klass, MonoMethod *caller)
4174 method = mono_class_get_cctor (klass);
4176 return (method == caller) ? FALSE : TRUE;
4182 * mono_class_array_element_size:
4185 * Returns: the number of bytes an element of type @klass
4186 * uses when stored into an array.
4189 mono_class_array_element_size (MonoClass *klass)
4191 MonoType *type = &klass->byval_arg;
4194 switch (type->type) {
4197 case MONO_TYPE_BOOLEAN:
4201 case MONO_TYPE_CHAR:
4210 case MONO_TYPE_CLASS:
4211 case MONO_TYPE_STRING:
4212 case MONO_TYPE_OBJECT:
4213 case MONO_TYPE_SZARRAY:
4214 case MONO_TYPE_ARRAY:
4216 case MONO_TYPE_MVAR:
4217 return sizeof (gpointer);
4222 case MONO_TYPE_VALUETYPE:
4223 if (type->data.klass->enumtype) {
4224 type = type->data.klass->enum_basetype;
4225 klass = klass->element_class;
4228 return mono_class_instance_size (klass) - sizeof (MonoObject);
4229 case MONO_TYPE_GENERICINST:
4230 type = &type->data.generic_class->container_class->byval_arg;
4233 g_error ("unknown type 0x%02x in mono_class_array_element_size", type->type);
4239 * mono_array_element_size:
4240 * @ac: pointer to a #MonoArrayClass
4242 * Returns: the size of single array element.
4245 mono_array_element_size (MonoClass *ac)
4247 return mono_class_array_element_size (ac->element_class);
4251 mono_ldtoken (MonoImage *image, guint32 token, MonoClass **handle_class,
4252 MonoGenericContext *context)
4254 if (image->dynamic) {
4255 MonoClass *tmp_handle_class;
4256 gpointer obj = mono_lookup_dynamic_token_class (image, token, &tmp_handle_class);
4258 g_assert (tmp_handle_class);
4260 *handle_class = tmp_handle_class;
4262 if (tmp_handle_class == mono_defaults.typehandle_class)
4263 return &((MonoClass*)obj)->byval_arg;
4268 switch (token & 0xff000000) {
4269 case MONO_TOKEN_TYPE_DEF:
4270 case MONO_TOKEN_TYPE_REF:
4271 case MONO_TOKEN_TYPE_SPEC: {
4274 *handle_class = mono_defaults.typehandle_class;
4275 class = mono_class_get_full (image, token, context);
4278 mono_class_init (class);
4279 /* We return a MonoType* as handle */
4280 return &class->byval_arg;
4282 case MONO_TOKEN_FIELD_DEF: {
4284 guint32 type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
4286 *handle_class = mono_defaults.fieldhandle_class;
4287 class = mono_class_get_full (image, MONO_TOKEN_TYPE_DEF | type, context);
4290 mono_class_init (class);
4291 return mono_class_get_field (class, token);
4293 case MONO_TOKEN_METHOD_DEF: {
4295 meth = mono_get_method_full (image, token, NULL, context);
4297 *handle_class = mono_defaults.methodhandle_class;
4300 case MONO_TOKEN_MEMBER_REF: {
4301 guint32 cols [MONO_MEMBERREF_SIZE];
4303 mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], mono_metadata_token_index (token) - 1, cols, MONO_MEMBERREF_SIZE);
4304 sig = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
4305 mono_metadata_decode_blob_size (sig, &sig);
4306 if (*sig == 0x6) { /* it's a field */
4308 MonoClassField *field;
4309 field = mono_field_from_token (image, token, &klass, context);
4311 *handle_class = mono_defaults.fieldhandle_class;
4315 meth = mono_get_method_full (image, token, NULL, context);
4317 *handle_class = mono_defaults.methodhandle_class;
4322 g_warning ("Unknown token 0x%08x in ldtoken", token);
4329 * This function might need to call runtime functions so it can't be part
4330 * of the metadata library.
4332 static MonoLookupDynamicToken lookup_dynamic = NULL;
4335 mono_install_lookup_dynamic_token (MonoLookupDynamicToken func)
4337 lookup_dynamic = func;
4341 mono_lookup_dynamic_token (MonoImage *image, guint32 token)
4343 MonoClass *handle_class;
4345 return lookup_dynamic (image, token, &handle_class);
4349 mono_lookup_dynamic_token_class (MonoImage *image, guint32 token, MonoClass **handle_class)
4351 return lookup_dynamic (image, token, handle_class);
4354 static MonoGetCachedClassInfo get_cached_class_info = NULL;
4357 mono_install_get_cached_class_info (MonoGetCachedClassInfo func)
4359 get_cached_class_info = func;
4363 mono_class_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
4365 if (!get_cached_class_info)
4368 return get_cached_class_info (klass, res);
4372 mono_class_get_image (MonoClass *klass)
4374 return klass->image;
4378 * mono_class_get_element_class:
4379 * @klass: the MonoClass to act on
4381 * Returns: the element class of an array or an enumeration.
4384 mono_class_get_element_class (MonoClass *klass)
4386 return klass->element_class;
4390 * mono_class_is_valuetype:
4391 * @klass: the MonoClass to act on
4393 * Returns: true if the MonoClass represents a ValueType.
4396 mono_class_is_valuetype (MonoClass *klass)
4398 return klass->valuetype;
4402 * mono_class_is_enum:
4403 * @klass: the MonoClass to act on
4405 * Returns: true if the MonoClass represents an enumeration.
4408 mono_class_is_enum (MonoClass *klass)
4410 return klass->enumtype;
4414 * mono_class_enum_basetype:
4415 * @klass: the MonoClass to act on
4417 * Returns: the underlying type representation for an enumeration.
4420 mono_class_enum_basetype (MonoClass *klass)
4422 return klass->enum_basetype;
4426 * mono_class_get_parent
4427 * @klass: the MonoClass to act on
4429 * Returns: the parent class for this class.
4432 mono_class_get_parent (MonoClass *klass)
4434 return klass->parent;
4438 * mono_class_get_nesting_type;
4439 * @klass: the MonoClass to act on
4441 * Returns: the container type where this type is nested or NULL if this type is not a nested type.
4444 mono_class_get_nesting_type (MonoClass *klass)
4446 return klass->nested_in;
4450 * mono_class_get_rank:
4451 * @klass: the MonoClass to act on
4453 * Returns: the rank for the array (the number of dimensions).
4456 mono_class_get_rank (MonoClass *klass)
4462 * mono_class_get_flags:
4463 * @klass: the MonoClass to act on
4465 * The type flags from the TypeDef table from the metadata.
4466 * see the TYPE_ATTRIBUTE_* definitions on tabledefs.h for the
4469 * Returns: the flags from the TypeDef table.
4472 mono_class_get_flags (MonoClass *klass)
4474 return klass->flags;
4478 * mono_class_get_name
4479 * @klass: the MonoClass to act on
4481 * Returns: the name of the class.
4484 mono_class_get_name (MonoClass *klass)
4490 * mono_class_get_namespace:
4491 * @klass: the MonoClass to act on
4493 * Returns: the namespace of the class.
4496 mono_class_get_namespace (MonoClass *klass)
4498 return klass->name_space;
4502 * mono_class_get_type:
4503 * @klass: the MonoClass to act on
4505 * This method returns the internal Type representation for the class.
4507 * Returns: the MonoType from the class.
4510 mono_class_get_type (MonoClass *klass)
4512 return &klass->byval_arg;
4516 * mono_class_get_byref_type:
4517 * @klass: the MonoClass to act on
4522 mono_class_get_byref_type (MonoClass *klass)
4524 return &klass->this_arg;
4528 * mono_class_num_fields:
4529 * @klass: the MonoClass to act on
4531 * Returns: the number of static and instance fields in the class.
4534 mono_class_num_fields (MonoClass *klass)
4536 return klass->field.count;
4540 * mono_class_num_methods:
4541 * @klass: the MonoClass to act on
4543 * Returns: the number of methods in the class.
4546 mono_class_num_methods (MonoClass *klass)
4548 return klass->method.count;
4552 * mono_class_num_properties
4553 * @klass: the MonoClass to act on
4555 * Returns: the number of properties in the class.
4558 mono_class_num_properties (MonoClass *klass)
4560 mono_class_setup_properties (klass);
4562 return klass->property.count;
4566 * mono_class_num_events:
4567 * @klass: the MonoClass to act on
4569 * Returns: the number of events in the class.
4572 mono_class_num_events (MonoClass *klass)
4574 mono_class_setup_events (klass);
4576 return klass->event.count;
4580 * mono_class_get_fields:
4581 * @klass: the MonoClass to act on
4583 * This routine is an iterator routine for retrieving the fields in a class.
4585 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4586 * iterate over all of the elements. When no more values are
4587 * available, the return value is NULL.
4589 * Returns: a @MonoClassField* on each iteration, or NULL when no more fields are available.
4592 mono_class_get_fields (MonoClass* klass, gpointer *iter)
4594 MonoClassField* field;
4597 mono_class_setup_fields_locking (klass);
4599 /* start from the first */
4600 if (klass->field.count) {
4601 return *iter = &klass->fields [0];
4609 if (field < &klass->fields [klass->field.count]) {
4610 return *iter = field;
4616 * mono_class_get_methods
4617 * @klass: the MonoClass to act on
4619 * This routine is an iterator routine for retrieving the fields in a class.
4621 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4622 * iterate over all of the elements. When no more values are
4623 * available, the return value is NULL.
4625 * Returns: a MonoMethod on each iteration or NULL when no more methods are available.
4628 mono_class_get_methods (MonoClass* klass, gpointer *iter)
4630 MonoMethod** method;
4634 mono_class_init (klass);
4636 mono_class_setup_methods (klass);
4637 /* start from the first */
4638 if (klass->method.count) {
4639 *iter = &klass->methods [0];
4640 return klass->methods [0];
4648 if (method < &klass->methods [klass->method.count]) {
4656 * mono_class_get_properties:
4657 * @klass: the MonoClass to act on
4659 * This routine is an iterator routine for retrieving the properties in a class.
4661 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4662 * iterate over all of the elements. When no more values are
4663 * available, the return value is NULL.
4665 * Returns: a @MonoProperty* on each invocation, or NULL when no more are available.
4668 mono_class_get_properties (MonoClass* klass, gpointer *iter)
4670 MonoProperty* property;
4674 mono_class_init (klass);
4676 mono_class_setup_properties (klass);
4677 /* start from the first */
4678 if (klass->property.count) {
4679 return *iter = &klass->properties [0];
4687 if (property < &klass->properties [klass->property.count]) {
4688 return *iter = property;
4694 * mono_class_get_events:
4695 * @klass: the MonoClass to act on
4697 * This routine is an iterator routine for retrieving the properties in a class.
4699 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4700 * iterate over all of the elements. When no more values are
4701 * available, the return value is NULL.
4703 * Returns: a @MonoEvent* on each invocation, or NULL when no more are available.
4706 mono_class_get_events (MonoClass* klass, gpointer *iter)
4712 mono_class_init (klass);
4714 mono_class_setup_events (klass);
4715 /* start from the first */
4716 if (klass->event.count) {
4717 return *iter = &klass->events [0];
4725 if (event < &klass->events [klass->event.count]) {
4726 return *iter = event;
4732 * mono_class_get_interfaces
4733 * @klass: the MonoClass to act on
4735 * This routine is an iterator routine for retrieving the interfaces implemented by this class.
4737 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4738 * iterate over all of the elements. When no more values are
4739 * available, the return value is NULL.
4741 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
4744 mono_class_get_interfaces (MonoClass* klass, gpointer *iter)
4750 mono_class_init (klass);
4752 /* start from the first */
4753 if (klass->interface_count) {
4754 *iter = &klass->interfaces [0];
4755 return klass->interfaces [0];
4763 if (iface < &klass->interfaces [klass->interface_count]) {
4771 * mono_class_get_nested_types
4772 * @klass: the MonoClass to act on
4774 * This routine is an iterator routine for retrieving the nested types of a class.
4776 * You must pass a gpointer that points to zero and is treated as an opaque handle to
4777 * iterate over all of the elements. When no more values are
4778 * available, the return value is NULL.
4780 * Returns: a @Monoclass* on each invocation, or NULL when no more are available.
4783 mono_class_get_nested_types (MonoClass* klass, gpointer *iter)
4789 mono_class_init (klass);
4791 /* start from the first */
4792 if (klass->nested_classes) {
4793 *iter = klass->nested_classes;
4794 return klass->nested_classes->data;
4796 /* no nested types */
4810 * mono_field_get_name:
4811 * @field: the MonoClassField to act on
4813 * Returns: the name of the field.
4816 mono_field_get_name (MonoClassField *field)
4822 * mono_field_get_type:
4823 * @field: the MonoClassField to act on
4825 * Returns: MonoType of the field.
4828 mono_field_get_type (MonoClassField *field)
4834 * mono_field_get_type:
4835 * @field: the MonoClassField to act on
4837 * Returns: MonoClass where the field was defined.
4840 mono_field_get_parent (MonoClassField *field)
4842 return field->parent;
4846 * mono_field_get_flags;
4847 * @field: the MonoClassField to act on
4849 * The metadata flags for a field are encoded using the
4850 * FIELD_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4852 * Returns: the flags for the field.
4855 mono_field_get_flags (MonoClassField *field)
4857 return field->type->attrs;
4861 * mono_property_get_name:
4862 * @prop: the MonoProperty to act on
4864 * Returns: the name of the property
4867 mono_property_get_name (MonoProperty *prop)
4873 * mono_property_get_set_method
4874 * @prop: the MonoProperty to act on.
4876 * Returns: the setter method of the property (A MonoMethod)
4879 mono_property_get_set_method (MonoProperty *prop)
4885 * mono_property_get_get_method
4886 * @prop: the MonoProperty to act on.
4888 * Returns: the setter method of the property (A MonoMethod)
4891 mono_property_get_get_method (MonoProperty *prop)
4897 * mono_property_get_parent:
4898 * @prop: the MonoProperty to act on.
4900 * Returns: the MonoClass where the property was defined.
4903 mono_property_get_parent (MonoProperty *prop)
4905 return prop->parent;
4909 * mono_property_get_flags:
4910 * @prop: the MonoProperty to act on.
4912 * The metadata flags for a property are encoded using the
4913 * PROPERTY_ATTRIBUTE_* constants. See the tabledefs.h file for details.
4915 * Returns: the flags for the property.
4918 mono_property_get_flags (MonoProperty *prop)
4924 * mono_event_get_name:
4925 * @event: the MonoEvent to act on
4927 * Returns: the name of the event.
4930 mono_event_get_name (MonoEvent *event)
4936 * mono_event_get_add_method:
4937 * @event: The MonoEvent to act on.
4939 * Returns: the @add' method for the event (a MonoMethod).
4942 mono_event_get_add_method (MonoEvent *event)
4948 * mono_event_get_remove_method:
4949 * @event: The MonoEvent to act on.
4951 * Returns: the @remove method for the event (a MonoMethod).
4954 mono_event_get_remove_method (MonoEvent *event)
4956 return event->remove;
4960 * mono_event_get_raise_method:
4961 * @event: The MonoEvent to act on.
4963 * Returns: the @raise method for the event (a MonoMethod).
4966 mono_event_get_raise_method (MonoEvent *event)
4968 return event->raise;
4972 * mono_event_get_parent:
4973 * @event: the MonoEvent to act on.
4975 * Returns: the MonoClass where the event is defined.
4978 mono_event_get_parent (MonoEvent *event)
4980 return event->parent;
4984 * mono_event_get_flags
4985 * @event: the MonoEvent to act on.
4987 * The metadata flags for an event are encoded using the
4988 * EVENT_* constants. See the tabledefs.h file for details.
4990 * Returns: the flags for the event.
4993 mono_event_get_flags (MonoEvent *event)
4995 return event->attrs;
4999 * mono_class_get_method_from_name:
5000 * @klass: where to look for the method
5001 * @name_space: name of the method
5002 * @param_count: number of parameters. -1 for any number.
5004 * Obtains a MonoMethod with a given name and number of parameters.
5005 * It only works if there are no multiple signatures for any given method name.
5008 mono_class_get_method_from_name (MonoClass *klass, const char *name, int param_count)
5010 return mono_class_get_method_from_name_flags (klass, name, param_count, 0);
5014 * mono_class_get_method_from_name_flags:
5015 * @klass: where to look for the method
5016 * @name_space: name of the method
5017 * @param_count: number of parameters. -1 for any number.
5018 * @flags: flags which must be set in the method
5020 * Obtains a MonoMethod with a given name and number of parameters.
5021 * It only works if there are no multiple signatures for any given method name.
5024 mono_class_get_method_from_name_flags (MonoClass *klass, const char *name, int param_count, int flags)
5026 MonoMethod *res = NULL;
5029 mono_class_init (klass);
5031 if (klass->methods) {
5032 mono_class_setup_methods (klass);
5033 for (i = 0; i < klass->method.count; ++i) {
5034 MonoMethod *method = klass->methods [i];
5036 if (method->name[0] == name [0] &&
5037 !strcmp (name, method->name) &&
5038 (param_count == -1 || mono_method_signature (method)->param_count == param_count) &&
5039 ((method->flags & flags) == flags)) {
5046 /* Search directly in the metadata to avoid calling setup_methods () */
5047 for (i = 0; i < klass->method.count; ++i) {
5048 guint32 cols [MONO_METHOD_SIZE];
5051 mono_metadata_decode_row (&klass->image->tables [MONO_TABLE_METHOD], klass->method.first + i, cols, MONO_METHOD_SIZE);
5053 if (!strcmp (mono_metadata_string_heap (klass->image, cols [MONO_METHOD_NAME]), name)) {
5054 method = mono_get_method (klass->image, MONO_TOKEN_METHOD_DEF | (klass->method.first + i + 1), klass);
5055 if ((param_count == -1) || mono_method_signature (method)->param_count == param_count) {
5067 * mono_class_set_failure:
5068 * @klass: class in which the failure was detected
5069 * @ex_type: the kind of exception/error to be thrown (later)
5070 * @ex_data: exception data (specific to each type of exception/error)
5072 * Keep a detected failure informations in the class for later processing.
5073 * Note that only the first failure is kept.
5076 mono_class_set_failure (MonoClass *klass, guint32 ex_type, void *ex_data)
5078 if (klass->exception_type)
5080 klass->exception_type = ex_type;
5081 klass->exception_data = ex_data;
5086 * mono_classes_init:
5088 * Initialize the resources used by this module.
5091 mono_classes_init (void)
5096 * mono_classes_cleanup:
5098 * Free the resources used by this module.
5101 mono_classes_cleanup (void)
5103 IOffsetInfo *cached_info, *next;
5105 if (global_interface_bitset)
5106 mono_bitset_free (global_interface_bitset);
5108 for (cached_info = cached_offset_info; cached_info;) {
5109 next = cached_info->next;
5111 g_free (cached_info);
5117 * mono_class_get_exception_for_failure:
5118 * @klass: class in which the failure was detected
5120 * Return a constructed MonoException than the caller can then throw
5121 * using mono_raise_exception - or NULL if no failure is present (or
5122 * doesn't result in an exception).
5125 mono_class_get_exception_for_failure (MonoClass *klass)
5127 switch (klass->exception_type) {
5128 case MONO_EXCEPTION_SECURITY_INHERITANCEDEMAND: {
5129 MonoDomain *domain = mono_domain_get ();
5130 MonoSecurityManager* secman = mono_security_manager_get_methods ();
5131 MonoMethod *method = klass->exception_data;
5132 guint32 error = (method) ? MONO_METADATA_INHERITANCEDEMAND_METHOD : MONO_METADATA_INHERITANCEDEMAND_CLASS;
5133 MonoObject *exc = NULL;
5137 args [1] = mono_assembly_get_object (domain, mono_image_get_assembly (klass->image));
5138 args [2] = mono_type_get_object (domain, &klass->byval_arg);
5139 args [3] = (method) ? mono_method_get_object (domain, method, NULL) : NULL;
5141 mono_runtime_invoke (secman->inheritsecurityexception, NULL, args, &exc);
5142 return (MonoException*) exc;
5144 case MONO_EXCEPTION_TYPE_LOAD:
5145 return mono_exception_from_name (mono_defaults.corlib, "System", "TypeLoadException");
5148 MonoLoaderError *error;
5151 error = mono_loader_get_last_error ();
5153 ex = mono_loader_error_prepare_exception (error);
5157 /* TODO - handle other class related failures */