Merge pull request #4045 from lambdageek/bug-47867
[mono.git] / mono / metadata / sre.c
index 10a916758df11f2e6d84f09e1f5a80634b3c473a..041a2ba6f4859f38494cd49ea37422e4e4f3a8aa 100644 (file)
@@ -44,6 +44,8 @@ static guint32 mono_image_get_sighelper_token (MonoDynamicImage *assembly, MonoR
 static gboolean ensure_runtime_vtable (MonoClass *klass, MonoError  *error);
 static void reflection_methodbuilder_from_dynamic_method (ReflectionMethodBuilder *rmb, MonoReflectionDynamicMethod *mb);
 static gboolean reflection_setup_internal_class (MonoReflectionTypeBuilder *tb, MonoError *error);
+static gboolean reflection_create_generic_class (MonoReflectionTypeBuilder *tb, MonoError *error);
+
 
 static gpointer register_assembly (MonoDomain *domain, MonoReflectionAssembly *res, MonoAssembly *assembly);
 #endif
@@ -1304,13 +1306,13 @@ mono_reflection_dynimage_basic_init (MonoReflectionAssemblyBuilder *assemblyb)
 static gpointer
 register_assembly (MonoDomain *domain, MonoReflectionAssembly *res, MonoAssembly *assembly)
 {
-       CACHE_OBJECT (MonoReflectionAssembly *, assembly, res, NULL);
+       return CACHE_OBJECT (MonoReflectionAssembly *, assembly, &res->object, NULL);
 }
 
 static gpointer
 register_module (MonoDomain *domain, MonoReflectionModuleBuilder *res, MonoDynamicImage *module)
 {
-       CACHE_OBJECT (MonoReflectionModuleBuilder *, module, res, NULL);
+       return CACHE_OBJECT (MonoReflectionModuleBuilder *, module, &res->module.obj, NULL);
 }
 
 static gboolean
@@ -1547,7 +1549,21 @@ mono_reflection_type_get_handle (MonoReflectionType* ref, MonoError *error)
                                return NULL;
                        }
                }
-
+               /* Need to resolve the generic_type in order for it to create its generic context. */
+               MonoType *gtd = mono_reflection_type_get_handle (gclass->generic_type, error);
+               if (!is_ok (error)) {
+                       g_free (types);
+                       return NULL;
+               }
+               MonoClass *gtd_klass = mono_class_from_mono_type (gtd);
+               if (is_sre_type_builder (mono_object_class (gclass->generic_type))) {
+                       reflection_create_generic_class ((MonoReflectionTypeBuilder*)gclass->generic_type, error);
+                       if (!is_ok (error)) {
+                               g_free (types);
+                               return NULL;
+                       }
+               }
+               g_assert (count == 0 || mono_class_is_gtd (gtd_klass));
                res = mono_reflection_bind_generic_parameters (gclass->generic_type, count, types, error);
                g_free (types);
                g_assert (res);
@@ -2408,6 +2424,7 @@ reflection_create_generic_class (MonoReflectionTypeBuilder *tb, MonoError *error
        mono_error_init (error);
 
        reflection_setup_internal_class (tb, error);
+       return_val_if_nok (error, FALSE);
 
        klass = mono_class_from_mono_type (tb->type.type);
 
@@ -2416,6 +2433,9 @@ reflection_create_generic_class (MonoReflectionTypeBuilder *tb, MonoError *error
        if (count == 0)
                return TRUE;
 
+       if (mono_class_try_get_generic_container (klass) != NULL)
+               return TRUE; /* already setup */
+
        MonoGenericContainer *generic_container = (MonoGenericContainer *)mono_image_alloc0 (klass->image, sizeof (MonoGenericContainer));
 
        generic_container->owner.klass = klass;
@@ -2939,11 +2959,13 @@ fix_partial_generic_class (MonoClass *klass, MonoError *error)
        if (!mono_class_get_generic_class (klass)->need_sync)
                return TRUE;
 
-       if (klass->method.count != gklass->method.count) {
-               klass->method.count = gklass->method.count;
-               klass->methods = (MonoMethod **)mono_image_alloc (klass->image, sizeof (MonoMethod*) * (klass->method.count + 1));
+       int mcount = mono_class_get_method_count (klass);
+       int gmcount = mono_class_get_method_count (gklass);
+       if (mcount != gmcount) {
+               mono_class_set_method_count (klass, gmcount);
+               klass->methods = (MonoMethod **)mono_image_alloc (klass->image, sizeof (MonoMethod*) * (gmcount + 1));
 
-               for (i = 0; i < klass->method.count; i++) {
+               for (i = 0; i < gmcount; i++) {
                        klass->methods [i] = mono_class_inflate_generic_method_full_checked (
                                gklass->methods [i], klass, mono_class_get_context (klass), error);
                        mono_error_assert_ok (error);
@@ -2968,11 +2990,13 @@ fix_partial_generic_class (MonoClass *klass, MonoError *error)
                klass->interfaces_inited = 1;
        }
 
-       if (klass->field.count != gklass->field.count) {
-               klass->field.count = gklass->field.count;
-               klass->fields = image_g_new0 (klass->image, MonoClassField, klass->field.count);
+       int fcount = mono_class_get_field_count (klass);
+       int gfcount = mono_class_get_field_count (gklass);
+       if (fcount != gfcount) {
+               mono_class_set_field_count (klass, gfcount);
+               klass->fields = image_g_new0 (klass->image, MonoClassField, gfcount);
 
-               for (i = 0; i < klass->field.count; i++) {
+               for (i = 0; i < gfcount; i++) {
                        klass->fields [i] = gklass->fields [i];
                        klass->fields [i].parent = klass;
                        klass->fields [i].type = mono_class_inflate_generic_type_checked (gklass->fields [i].type, mono_class_get_context (klass), error);
@@ -3032,7 +3056,7 @@ ensure_runtime_vtable (MonoClass *klass, MonoError *error)
        if (tb) {
                num = tb->ctors? mono_array_length (tb->ctors): 0;
                num += tb->num_methods;
-               klass->method.count = num;
+               mono_class_set_method_count (klass, num);
                klass->methods = (MonoMethod **)mono_image_alloc (klass->image, sizeof (MonoMethod*) * num);
                num = tb->ctors? mono_array_length (tb->ctors): 0;
                for (i = 0; i < num; ++i) {
@@ -3069,9 +3093,10 @@ ensure_runtime_vtable (MonoClass *klass, MonoError *error)
                }
        }
 
-       if (mono_class_is_interface (klass)) {
+       if (mono_class_is_interface (klass) && !mono_class_is_ginst (klass)) {
                int slot_num = 0;
-               for (i = 0; i < klass->method.count; ++i) {
+               int mcount = mono_class_get_method_count (klass);
+               for (i = 0; i < mcount; ++i) {
                        MonoMethod *im = klass->methods [i];
                        if (!(im->flags & METHOD_ATTRIBUTE_STATIC))
                                im->slot = slot_num++;
@@ -3185,6 +3210,7 @@ typebuilder_setup_fields (MonoClass *klass, MonoError *error)
        MonoReflectionTypeBuilder *tb = (MonoReflectionTypeBuilder *)mono_class_get_ref_info (klass);
        MonoReflectionFieldBuilder *fb;
        MonoClassField *field;
+       MonoFieldDefaultValue *def_values;
        MonoImage *image = klass->image;
        const char *p, *p2;
        int i, instance_size, packing_size = 0;
@@ -3198,7 +3224,8 @@ typebuilder_setup_fields (MonoClass *klass, MonoError *error)
                instance_size = sizeof (MonoObject);
        }
 
-       klass->field.count = tb->num_fields;
+       int fcount = tb->num_fields;
+       mono_class_set_field_count (klass, fcount);
 
        mono_error_init (error);
 
@@ -3207,9 +3234,9 @@ typebuilder_setup_fields (MonoClass *klass, MonoError *error)
                instance_size += tb->class_size;
        }
        
-       klass->fields = image_g_new0 (image, MonoClassField, klass->field.count);
-       mono_class_alloc_ext (klass);
-       klass->ext->field_def_values = image_g_new0 (image, MonoFieldDefaultValue, klass->field.count);
+       klass->fields = image_g_new0 (image, MonoClassField, fcount);
+       def_values = image_g_new0 (image, MonoFieldDefaultValue, fcount);
+       mono_class_set_field_def_values (klass, def_values);
        /*
        This is, guess what, a hack.
        The issue is that the runtime doesn't know how to setup the fields of a typebuider and crash.
@@ -3219,7 +3246,7 @@ typebuilder_setup_fields (MonoClass *klass, MonoError *error)
        */
        klass->size_inited = 1;
 
-       for (i = 0; i < klass->field.count; ++i) {
+       for (i = 0; i < fcount; ++i) {
                MonoArray *rva_data;
                fb = (MonoReflectionFieldBuilder *)mono_array_get (tb->fields, gpointer, i);
                field = &klass->fields [i];
@@ -3242,7 +3269,7 @@ typebuilder_setup_fields (MonoClass *klass, MonoError *error)
                        size_t size = mono_array_length (rva_data);
                        char *data = (char *)mono_image_alloc (klass->image, size);
                        memcpy (data, base, size);
-                       klass->ext->field_def_values [i].data = data;
+                       def_values [i].data = data;
                }
                if (fb->offset != -1)
                        field->offset = fb->offset;
@@ -3252,13 +3279,13 @@ typebuilder_setup_fields (MonoClass *klass, MonoError *error)
                if (fb->def_value) {
                        MonoDynamicImage *assembly = (MonoDynamicImage*)klass->image;
                        field->type->attrs |= FIELD_ATTRIBUTE_HAS_DEFAULT;
-                       idx = mono_dynimage_encode_constant (assembly, fb->def_value, &klass->ext->field_def_values [i].def_type);
+                       idx = mono_dynimage_encode_constant (assembly, fb->def_value, &def_values [i].def_type);
                        /* Copy the data from the blob since it might get realloc-ed */
                        p = assembly->blob.data + idx;
                        len = mono_metadata_decode_blob_size (p, &p2);
                        len += p2 - p;
-                       klass->ext->field_def_values [i].data = (const char *)mono_image_alloc (image, len);
-                       memcpy ((gpointer)klass->ext->field_def_values [i].data, p, len);
+                       def_values [i].data = (const char *)mono_image_alloc (image, len);
+                       memcpy ((gpointer)def_values [i].data, p, len);
                }
        }
 
@@ -3272,19 +3299,23 @@ typebuilder_setup_properties (MonoClass *klass, MonoError *error)
        MonoReflectionPropertyBuilder *pb;
        MonoImage *image = klass->image;
        MonoProperty *properties;
+       MonoClassPropertyInfo *info;
        int i;
 
        mono_error_init (error);
 
-       if (!klass->ext)
-               klass->ext = image_g_new0 (image, MonoClassExt, 1);
+       info = mono_class_get_property_info (klass);
+       if (!info) {
+               info = mono_class_alloc0 (klass, sizeof (MonoClassPropertyInfo));
+               mono_class_set_property_info (klass, info);
+       }
 
-       klass->ext->property.count = tb->properties ? mono_array_length (tb->properties) : 0;
-       klass->ext->property.first = 0;
+       info->count = tb->properties ? mono_array_length (tb->properties) : 0;
+       info->first = 0;
 
-       properties = image_g_new0 (image, MonoProperty, klass->ext->property.count);
-       klass->ext->properties = properties;
-       for (i = 0; i < klass->ext->property.count; ++i) {
+       properties = image_g_new0 (image, MonoProperty, info->count);
+       info->properties = properties;
+       for (i = 0; i < info->count; ++i) {
                pb = mono_array_get (tb->properties, MonoReflectionPropertyBuilder*, i);
                properties [i].parent = klass;
                properties [i].attrs = pb->attrs;
@@ -3301,16 +3332,16 @@ typebuilder_setup_properties (MonoClass *klass, MonoError *error)
                        guint32 len, idx;
                        const char *p, *p2;
                        MonoDynamicImage *assembly = (MonoDynamicImage*)klass->image;
-                       if (!klass->ext->prop_def_values)
-                               klass->ext->prop_def_values = image_g_new0 (image, MonoFieldDefaultValue, klass->ext->property.count);
+                       if (!info->def_values)
+                               info->def_values = image_g_new0 (image, MonoFieldDefaultValue, info->count);
                        properties [i].attrs |= PROPERTY_ATTRIBUTE_HAS_DEFAULT;
-                       idx = mono_dynimage_encode_constant (assembly, pb->def_value, &klass->ext->prop_def_values [i].def_type);
+                       idx = mono_dynimage_encode_constant (assembly, pb->def_value, &info->def_values [i].def_type);
                        /* Copy the data from the blob since it might get realloc-ed */
                        p = assembly->blob.data + idx;
                        len = mono_metadata_decode_blob_size (p, &p2);
                        len += p2 - p;
-                       klass->ext->prop_def_values [i].data = (const char *)mono_image_alloc (image, len);
-                       memcpy ((gpointer)klass->ext->prop_def_values [i].data, p, len);
+                       info->def_values [i].data = (const char *)mono_image_alloc (image, len);
+                       memcpy ((gpointer)info->def_values [i].data, p, len);
                }
        }
 }
@@ -3322,19 +3353,20 @@ typebuilder_setup_events (MonoClass *klass, MonoError *error)
        MonoReflectionEventBuilder *eb;
        MonoImage *image = klass->image;
        MonoEvent *events;
+       MonoClassEventInfo *info;
        int i;
 
        mono_error_init (error);
 
-       if (!klass->ext)
-               klass->ext = image_g_new0 (image, MonoClassExt, 1);
+       info = mono_class_alloc0 (klass, sizeof (MonoClassEventInfo));
+       mono_class_set_event_info (klass, info);
 
-       klass->ext->event.count = tb->events ? mono_array_length (tb->events) : 0;
-       klass->ext->event.first = 0;
+       info->count = tb->events ? mono_array_length (tb->events) : 0;
+       info->first = 0;
 
-       events = image_g_new0 (image, MonoEvent, klass->ext->event.count);
-       klass->ext->events = events;
-       for (i = 0; i < klass->ext->event.count; ++i) {
+       events = image_g_new0 (image, MonoEvent, info->count);
+       info->events = events;
+       for (i = 0; i < info->count; ++i) {
                eb = mono_array_get (tb->events, MonoReflectionEventBuilder*, i);
                events [i].parent = klass;
                events [i].attrs = eb->attrs;
@@ -3450,9 +3482,9 @@ ves_icall_TypeBuilder_create_runtime_class (MonoReflectionTypeBuilder *tb)
                        goto failure;
 
        if (tb->subtypes) {
+               GList *nested = NULL;
                for (i = 0; i < mono_array_length (tb->subtypes); ++i) {
                        MonoReflectionTypeBuilder *subtb = mono_array_get (tb->subtypes, MonoReflectionTypeBuilder*, i);
-                       mono_class_alloc_ext (klass);
 
                        if (!subtb->type.type) {
                                reflection_setup_internal_class (subtb, &error);
@@ -3461,8 +3493,9 @@ ves_icall_TypeBuilder_create_runtime_class (MonoReflectionTypeBuilder *tb)
 
                        MonoType *subtype = mono_reflection_type_get_handle ((MonoReflectionType*)subtb, &error);
                        if (!is_ok (&error)) goto failure;
-                       klass->ext->nested_classes = g_list_prepend_image (klass->image, klass->ext->nested_classes, mono_class_from_mono_type (subtype));
+                       nested = g_list_prepend_image (klass->image, nested, mono_class_from_mono_type (subtype));
                }
+               mono_class_set_nested_classes_property (klass, nested);
        }
 
        klass->nested_classes_inited = TRUE;