diff options
author | Rico Tzschichholz <ricotz@ubuntu.com> | 2023-01-04 22:47:01 +0100 |
---|---|---|
committer | Rico Tzschichholz <ricotz@ubuntu.com> | 2023-01-04 22:47:01 +0100 |
commit | f156de68664e919ee5ad1aa9f6b811512e75122c (patch) | |
tree | b2d95bbdf5ab6742986bbadf0870916d3c85cb6a /tests | |
parent | 3c64222b6361aceb5ee42c7523c9d854a0d7f731 (diff) | |
download | vala-f156de68664e919ee5ad1aa9f6b811512e75122c.tar.gz |
codegen: Correctly handle fixed-length array initialization of fields in classes
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 1 | ||||
-rw-r--r-- | tests/arrays/class-field-fixed-length-initializer.c-expected | 412 | ||||
-rw-r--r-- | tests/arrays/class-field-fixed-length-initializer.vala | 32 | ||||
-rw-r--r-- | tests/arrays/class-field-initializer.c-expected | 4 | ||||
-rw-r--r-- | tests/basic-types/bug686336.c-expected | 2 | ||||
-rw-r--r-- | tests/objects/compact-class-custom-ref.c-expected | 2 | ||||
-rw-r--r-- | tests/objects/destructors.c-expected | 2 |
7 files changed, 453 insertions, 2 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index 407bffc5b..577b926d1 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -126,6 +126,7 @@ TESTS = \ namespace/unique.vala \ arrays/cast-silent-invalid.test \ arrays/cast-struct-boxed-element-access.vala \ + arrays/class-field-fixed-length-initializer.vala \ arrays/class-field-initializer.vala \ arrays/class-field-length-cname.vala \ arrays/constant-element-access.vala \ diff --git a/tests/arrays/class-field-fixed-length-initializer.c-expected b/tests/arrays/class-field-fixed-length-initializer.c-expected new file mode 100644 index 000000000..4b402f912 --- /dev/null +++ b/tests/arrays/class-field-fixed-length-initializer.c-expected @@ -0,0 +1,412 @@ +/* arrays_class_field_fixed_length_initializer.c generated by valac, the Vala compiler + * generated from arrays_class_field_fixed_length_initializer.vala, do not modify */ + +#include <glib-object.h> +#include <stdlib.h> +#include <string.h> +#include <glib.h> +#include <gobject/gvaluecollector.h> + +#if !defined(VALA_EXTERN) +#if defined(_MSC_VER) +#define VALA_EXTERN __declspec(dllexport) extern +#elif __GNUC__ >= 4 +#define VALA_EXTERN __attribute__((visibility("default"))) extern +#else +#define VALA_EXTERN extern +#endif +#endif + +#define TYPE_FOO (foo_get_type ()) +#define FOO(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_FOO, Foo)) +#define FOO_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_FOO, FooClass)) +#define IS_FOO(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_FOO)) +#define IS_FOO_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_FOO)) +#define FOO_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_FOO, FooClass)) + +typedef struct _Foo Foo; +typedef struct _FooClass FooClass; +typedef struct _FooPrivate FooPrivate; +typedef struct _ParamSpecFoo ParamSpecFoo; +#define _foo_unref0(var) ((var == NULL) ? NULL : (var = (foo_unref (var), NULL))) +#define _vala_assert(expr, msg) if G_LIKELY (expr) ; else g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg); +#define _vala_return_if_fail(expr, msg) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return; } +#define _vala_return_val_if_fail(expr, msg, val) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return val; } +#define _vala_warn_if_fail(expr, msg) if G_LIKELY (expr) ; else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg); + +struct _Foo { + GTypeInstance parent_instance; + volatile int ref_count; + FooPrivate * priv; + gchar* f[2]; + gint i[3]; +}; + +struct _FooClass { + GTypeClass parent_class; + void (*finalize) (Foo *self); + gchar* cf[2]; + gint ci[3]; +}; + +struct _ParamSpecFoo { + GParamSpec parent_instance; +}; + +static gpointer foo_parent_class = NULL; +VALA_EXTERN gchar* foo_sf[2]; +gchar* foo_sf[2] = {0}; +VALA_EXTERN gint foo_si[3]; +gint foo_si[3] = {0}; + +VALA_EXTERN gpointer foo_ref (gpointer instance); +VALA_EXTERN void foo_unref (gpointer instance); +VALA_EXTERN GParamSpec* param_spec_foo (const gchar* name, + const gchar* nick, + const gchar* blurb, + GType object_type, + GParamFlags flags); +VALA_EXTERN void value_set_foo (GValue* value, + gpointer v_object); +VALA_EXTERN void value_take_foo (GValue* value, + gpointer v_object); +VALA_EXTERN gpointer value_get_foo (const GValue* value); +VALA_EXTERN GType foo_get_type (void) G_GNUC_CONST ; +G_DEFINE_AUTOPTR_CLEANUP_FUNC (Foo, foo_unref) +VALA_EXTERN Foo* foo_new (void); +VALA_EXTERN Foo* foo_construct (GType object_type); +static void foo_finalize (Foo * obj); +static GType foo_get_type_once (void); +static void _vala_main (void); +static void _vala_array_destroy (gpointer array, + gssize array_length, + GDestroyNotify destroy_func); +static void _vala_array_free (gpointer array, + gssize array_length, + GDestroyNotify destroy_func); + +Foo* +foo_construct (GType object_type) +{ + Foo* self = NULL; + self = (Foo*) g_type_create_instance (object_type); + return self; +} + +Foo* +foo_new (void) +{ + return foo_construct (TYPE_FOO); +} + +static void +value_foo_init (GValue* value) +{ + value->data[0].v_pointer = NULL; +} + +static void +value_foo_free_value (GValue* value) +{ + if (value->data[0].v_pointer) { + foo_unref (value->data[0].v_pointer); + } +} + +static void +value_foo_copy_value (const GValue* src_value, + GValue* dest_value) +{ + if (src_value->data[0].v_pointer) { + dest_value->data[0].v_pointer = foo_ref (src_value->data[0].v_pointer); + } else { + dest_value->data[0].v_pointer = NULL; + } +} + +static gpointer +value_foo_peek_pointer (const GValue* value) +{ + return value->data[0].v_pointer; +} + +static gchar* +value_foo_collect_value (GValue* value, + guint n_collect_values, + GTypeCValue* collect_values, + guint collect_flags) +{ + if (collect_values[0].v_pointer) { + Foo * object; + object = collect_values[0].v_pointer; + if (object->parent_instance.g_class == NULL) { + return g_strconcat ("invalid unclassed object pointer for value type `", G_VALUE_TYPE_NAME (value), "'", NULL); + } else if (!g_value_type_compatible (G_TYPE_FROM_INSTANCE (object), G_VALUE_TYPE (value))) { + return g_strconcat ("invalid object type `", g_type_name (G_TYPE_FROM_INSTANCE (object)), "' for value type `", G_VALUE_TYPE_NAME (value), "'", NULL); + } + value->data[0].v_pointer = foo_ref (object); + } else { + value->data[0].v_pointer = NULL; + } + return NULL; +} + +static gchar* +value_foo_lcopy_value (const GValue* value, + guint n_collect_values, + GTypeCValue* collect_values, + guint collect_flags) +{ + Foo ** object_p; + object_p = collect_values[0].v_pointer; + if (!object_p) { + return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value)); + } + if (!value->data[0].v_pointer) { + *object_p = NULL; + } else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) { + *object_p = value->data[0].v_pointer; + } else { + *object_p = foo_ref (value->data[0].v_pointer); + } + return NULL; +} + +GParamSpec* +param_spec_foo (const gchar* name, + const gchar* nick, + const gchar* blurb, + GType object_type, + GParamFlags flags) +{ + ParamSpecFoo* spec; + g_return_val_if_fail (g_type_is_a (object_type, TYPE_FOO), NULL); + spec = g_param_spec_internal (G_TYPE_PARAM_OBJECT, name, nick, blurb, flags); + G_PARAM_SPEC (spec)->value_type = object_type; + return G_PARAM_SPEC (spec); +} + +gpointer +value_get_foo (const GValue* value) +{ + g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_FOO), NULL); + return value->data[0].v_pointer; +} + +void +value_set_foo (GValue* value, + gpointer v_object) +{ + Foo * old; + g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_FOO)); + old = value->data[0].v_pointer; + if (v_object) { + g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_FOO)); + g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value))); + value->data[0].v_pointer = v_object; + foo_ref (value->data[0].v_pointer); + } else { + value->data[0].v_pointer = NULL; + } + if (old) { + foo_unref (old); + } +} + +void +value_take_foo (GValue* value, + gpointer v_object) +{ + Foo * old; + g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_FOO)); + old = value->data[0].v_pointer; + if (v_object) { + g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_FOO)); + g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value))); + value->data[0].v_pointer = v_object; + } else { + value->data[0].v_pointer = NULL; + } + if (old) { + foo_unref (old); + } +} + +static void +foo_class_init (FooClass * klass, + gpointer klass_data) +{ + gchar* _tmp0_; + gchar* _tmp1_; + gchar* _tmp2_[2] = {0}; + gint _tmp3_[3] = {0}; + gchar* _tmp4_; + gchar* _tmp5_; + gchar* _tmp6_[2] = {0}; + gint _tmp7_[3] = {0}; + foo_parent_class = g_type_class_peek_parent (klass); + ((FooClass *) klass)->finalize = foo_finalize; + _tmp0_ = g_strdup ("foo"); + _tmp1_ = g_strdup ("bar"); + _tmp2_[0] = _tmp0_; + _tmp2_[1] = _tmp1_; + memcpy (klass->cf, _tmp2_, 2 * sizeof (gchar*)); + _tmp3_[0] = 23; + _tmp3_[1] = 42; + _tmp3_[2] = 4711; + memcpy (klass->ci, _tmp3_, 3 * sizeof (gint)); + _tmp4_ = g_strdup ("foo"); + _tmp5_ = g_strdup ("bar"); + _tmp6_[0] = _tmp4_; + _tmp6_[1] = _tmp5_; + memcpy (foo_sf, _tmp6_, 2 * sizeof (gchar*)); + _tmp7_[0] = 23; + _tmp7_[1] = 42; + _tmp7_[2] = 4711; + memcpy (foo_si, _tmp7_, 3 * sizeof (gint)); +} + +static void +foo_instance_init (Foo * self, + gpointer klass) +{ + gchar* _tmp0_; + gchar* _tmp1_; + gchar* _tmp2_[2] = {0}; + gint _tmp3_[3] = {0}; + _tmp0_ = g_strdup ("foo"); + _tmp1_ = g_strdup ("bar"); + _tmp2_[0] = _tmp0_; + _tmp2_[1] = _tmp1_; + memcpy (self->f, _tmp2_, 2 * sizeof (gchar*)); + _tmp3_[0] = 23; + _tmp3_[1] = 42; + _tmp3_[2] = 4711; + memcpy (self->i, _tmp3_, 3 * sizeof (gint)); + self->ref_count = 1; +} + +static void +foo_finalize (Foo * obj) +{ + Foo * self; + self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_FOO, Foo); + g_signal_handlers_destroy (self); + _vala_array_destroy (self->f, 2, (GDestroyNotify) g_free); +} + +static GType +foo_get_type_once (void) +{ + static const GTypeValueTable g_define_type_value_table = { value_foo_init, value_foo_free_value, value_foo_copy_value, value_foo_peek_pointer, "p", value_foo_collect_value, "p", value_foo_lcopy_value }; + static const GTypeInfo g_define_type_info = { sizeof (FooClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) foo_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (Foo), 0, (GInstanceInitFunc) foo_instance_init, &g_define_type_value_table }; + static const GTypeFundamentalInfo g_define_type_fundamental_info = { (G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE) }; + GType foo_type_id; + foo_type_id = g_type_register_fundamental (g_type_fundamental_next (), "Foo", &g_define_type_info, &g_define_type_fundamental_info, 0); + return foo_type_id; +} + +GType +foo_get_type (void) +{ + static volatile gsize foo_type_id__once = 0; + if (g_once_init_enter (&foo_type_id__once)) { + GType foo_type_id; + foo_type_id = foo_get_type_once (); + g_once_init_leave (&foo_type_id__once, foo_type_id); + } + return foo_type_id__once; +} + +gpointer +foo_ref (gpointer instance) +{ + Foo * self; + self = instance; + g_atomic_int_inc (&self->ref_count); + return instance; +} + +void +foo_unref (gpointer instance) +{ + Foo * self; + self = instance; + if (g_atomic_int_dec_and_test (&self->ref_count)) { + FOO_GET_CLASS (self)->finalize (self); + g_type_free_instance ((GTypeInstance *) self); + } +} + +static void +_vala_main (void) +{ + Foo* foo = NULL; + Foo* _tmp0_; + _tmp0_ = foo_new (); + foo = _tmp0_; + { + const gchar* _tmp1_; + gint _tmp2_; + _vala_assert (2 == 2, "foo.f.length == 2"); + _tmp1_ = foo->f[0]; + _vala_assert (g_strcmp0 (_tmp1_, "foo") == 0, "foo.f[0] == \"foo\""); + _vala_assert (3 == 3, "foo.i.length == 3"); + _tmp2_ = foo->i[2]; + _vala_assert (_tmp2_ == 4711, "foo.i[2] == 4711"); + } + { + const gchar* _tmp3_; + gint _tmp4_; + _vala_assert (2 == 2, "foo.cf.length == 2"); + _tmp3_ = FOO_GET_CLASS (foo)->cf[0]; + _vala_assert (g_strcmp0 (_tmp3_, "foo") == 0, "foo.cf[0] == \"foo\""); + _vala_assert (3 == 3, "foo.ci.length == 3"); + _tmp4_ = FOO_GET_CLASS (foo)->ci[2]; + _vala_assert (_tmp4_ == 4711, "foo.ci[2] == 4711"); + } + { + const gchar* _tmp5_; + gint _tmp6_; + _vala_assert (2 == 2, "Foo.sf.length == 2"); + _tmp5_ = foo_sf[0]; + _vala_assert (g_strcmp0 (_tmp5_, "foo") == 0, "Foo.sf[0] == \"foo\""); + _vala_assert (3 == 3, "Foo.si.length == 3"); + _tmp6_ = foo_si[2]; + _vala_assert (_tmp6_ == 4711, "Foo.si[2] == 4711"); + } + _foo_unref0 (foo); +} + +int +main (int argc, + char ** argv) +{ + _vala_main (); + return 0; +} + +static void +_vala_array_destroy (gpointer array, + gssize array_length, + GDestroyNotify destroy_func) +{ + if ((array != NULL) && (destroy_func != NULL)) { + gssize i; + for (i = 0; i < array_length; i = i + 1) { + if (((gpointer*) array)[i] != NULL) { + destroy_func (((gpointer*) array)[i]); + } + } + } +} + +static void +_vala_array_free (gpointer array, + gssize array_length, + GDestroyNotify destroy_func) +{ + _vala_array_destroy (array, array_length, destroy_func); + g_free (array); +} + diff --git a/tests/arrays/class-field-fixed-length-initializer.vala b/tests/arrays/class-field-fixed-length-initializer.vala new file mode 100644 index 000000000..5552c20e6 --- /dev/null +++ b/tests/arrays/class-field-fixed-length-initializer.vala @@ -0,0 +1,32 @@ +class Foo { + public string f[2] = { "foo", "bar" }; + public int i[3] = { 23, 42, 4711 }; + + public class string cf[2] = { "foo", "bar" }; + public class int ci[3] = { 23, 42, 4711 }; + + public static string sf[2] = { "foo", "bar" }; + public static int si[3] = { 23, 42, 4711 }; +} + +void main () { + var foo = new Foo (); + { + assert (foo.f.length == 2); + assert (foo.f[0] == "foo"); + assert (foo.i.length == 3); + assert (foo.i[2] == 4711); + } + { + assert (foo.cf.length == 2); + assert (foo.cf[0] == "foo"); + assert (foo.ci.length == 3); + assert (foo.ci[2] == 4711); + } + { + assert (Foo.sf.length == 2); + assert (Foo.sf[0] == "foo"); + assert (Foo.si.length == 3); + assert (Foo.si[2] == 4711); + } +} diff --git a/tests/arrays/class-field-initializer.c-expected b/tests/arrays/class-field-initializer.c-expected index d58abb4a8..5faddf3db 100644 --- a/tests/arrays/class-field-initializer.c-expected +++ b/tests/arrays/class-field-initializer.c-expected @@ -379,6 +379,7 @@ foo_class_init (FooClass * klass, _tmp1__length1 = _tmp0__length1; foo_sf = _tmp1_; foo_sf_length1 = _tmp1__length1; + _foo_sf_size_ = foo_sf_length1; _tmp2_ = g_strdup ("baz"); _tmp3_ = g_strdup ("foo"); _tmp4_ = g_strdup ("bar"); @@ -388,6 +389,7 @@ foo_class_init (FooClass * klass, _tmp5_[2] = _tmp4_; foo_si = _tmp5_; foo_si_length1 = 3; + _foo_si_size_ = foo_si_length1; _tmp6_ = g_strdup ("baz"); _tmp7_ = manam; _tmp7__length1 = _vala_array_length (manam); @@ -400,6 +402,7 @@ foo_class_init (FooClass * klass, _tmp11_[2] = _tmp10_; foo_sa = _tmp11_; foo_sa_length1 = 3; + _foo_sa_size_ = foo_sa_length1; } static void @@ -707,6 +710,7 @@ bar_class_init (BarClass * klass, _tmp1__length1 = _tmp0__length1; bar_sf = _tmp1_; bar_sf_length1 = _tmp1__length1; + _bar_sf_size_ = bar_sf_length1; } static void diff --git a/tests/basic-types/bug686336.c-expected b/tests/basic-types/bug686336.c-expected index cec001168..f485d7871 100644 --- a/tests/basic-types/bug686336.c-expected +++ b/tests/basic-types/bug686336.c-expected @@ -306,9 +306,11 @@ foo_class_init (FooClass * klass, _tmp0_[2] = 3; foo_static_bar = _tmp0_; foo_static_bar_length1 = 3; + _foo_static_bar_size_ = foo_static_bar_length1; _tmp2_ = foo_create_array (&_tmp1_); foo_static_baz = _tmp2_; foo_static_baz_length1 = _tmp1_; + _foo_static_baz_size_ = foo_static_baz_length1; } static void diff --git a/tests/objects/compact-class-custom-ref.c-expected b/tests/objects/compact-class-custom-ref.c-expected index db323d17a..7d15c46c7 100644 --- a/tests/objects/compact-class-custom-ref.c-expected +++ b/tests/objects/compact-class-custom-ref.c-expected @@ -73,7 +73,7 @@ foo_new (void) static void foo_instance_init (Foo * self) { - self->ref_count = 1; + self->ref_count = (volatile int) 1; } void diff --git a/tests/objects/destructors.c-expected b/tests/objects/destructors.c-expected index de713d1a2..a3e969bef 100644 --- a/tests/objects/destructors.c-expected +++ b/tests/objects/destructors.c-expected @@ -2,9 +2,9 @@ * generated from objects_destructors.vala, do not modify */ #include <glib-object.h> -#include <glib.h> #include <stdlib.h> #include <string.h> +#include <glib.h> #if !defined(VALA_EXTERN) #if defined(_MSC_VER) |