From c59ee0984d25a00c3ad0f80dbad97e11053ed92a Mon Sep 17 00:00:00 2001 From: Rico Tzschichholz Date: Wed, 26 Jan 2022 19:34:06 +0100 Subject: codegen: Write array length of formal parameters with fixed length "void foo (gint i[3]) { ... }" instead of "void foo (gint* i) { ... }" --- ccode/valaccodeparameter.vala | 14 ++++- codegen/valaccodearraymodule.vala | 6 +- tests/arrays/fixed-length-enum-value.c-expected | 4 +- tests/asynchronous/captured-fixed-array.c-expected | 4 +- tests/basic-types/arrays.c-expected | 70 ++++++++++++++-------- tests/basic-types/arrays.vala | 22 +++---- .../parameter-fixed-array-initializer.c-expected | 8 +-- tests/parser/array-length.c-expected | 6 +- tests/posix/arrays.c-expected | 70 ++++++++++++++-------- 9 files changed, 128 insertions(+), 76 deletions(-) diff --git a/ccode/valaccodeparameter.vala b/ccode/valaccodeparameter.vala index b4bc74963..8e90695a7 100644 --- a/ccode/valaccodeparameter.vala +++ b/ccode/valaccodeparameter.vala @@ -42,6 +42,8 @@ public class Vala.CCodeParameter : CCodeNode { */ public bool ellipsis { get; set; } + CCodeDeclarator? declarator; + public CCodeParameter (string n, string type) { name = n; type_name = type; @@ -51,11 +53,21 @@ public class Vala.CCodeParameter : CCodeNode { ellipsis = true; } + public CCodeParameter.with_declarator (string type, CCodeDeclarator decl) { + name = decl.name; + type_name = type; + declarator = decl; + } + public override void write (CCodeWriter writer) { if (!ellipsis) { writer.write_string (type_name); writer.write_string (" "); - writer.write_string (name); + if (declarator != null) { + declarator.write (writer); + } else { + writer.write_string (name); + } } else { writer.write_string ("..."); } diff --git a/codegen/valaccodearraymodule.vala b/codegen/valaccodearraymodule.vala index 2b3021a5f..9a84ef848 100644 --- a/codegen/valaccodearraymodule.vala +++ b/codegen/valaccodearraymodule.vala @@ -861,16 +861,12 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule { if (ctypename == null) { ctypename = get_ccode_name (param.variable_type); - if (array_type.fixed_length) { - ctypename += "*"; - } - if (param.direction != ParameterDirection.IN) { ctypename += "*"; } } - var main_cparam = new CCodeParameter (get_ccode_name (param), ctypename); + var main_cparam = new CCodeParameter.with_declarator (ctypename, new CCodeVariableDeclarator (get_ccode_name (param), null, get_ccode_declarator_suffix (array_type))); generate_type_declaration (array_type.element_type, decl_space); diff --git a/tests/arrays/fixed-length-enum-value.c-expected b/tests/arrays/fixed-length-enum-value.c-expected index 0ea904fc3..372dcf4ae 100644 --- a/tests/arrays/fixed-length-enum-value.c-expected +++ b/tests/arrays/fixed-length-enum-value.c-expected @@ -36,7 +36,7 @@ VALA_EXTERN GType foo_get_type (void) G_GNUC_CONST ; VALA_EXTERN GType bar_get_type (void) G_GNUC_CONST ; VALA_EXTERN Bar* bar_dup (const Bar* self); VALA_EXTERN void bar_free (Bar* self); -VALA_EXTERN void foo (guint* array); +VALA_EXTERN void foo (guint array[FOO_BAR]); static void _vala_main (void); static GType @@ -96,7 +96,7 @@ bar_get_type (void) } void -foo (guint* array) +foo (guint array[FOO_BAR]) { _vala_assert (FOO_BAR == 23, "array.length == 23"); } diff --git a/tests/asynchronous/captured-fixed-array.c-expected b/tests/asynchronous/captured-fixed-array.c-expected index 8f153f064..9d6faa299 100644 --- a/tests/asynchronous/captured-fixed-array.c-expected +++ b/tests/asynchronous/captured-fixed-array.c-expected @@ -34,7 +34,7 @@ struct _FooData { }; static void foo_data_free (gpointer _data); -VALA_EXTERN void foo (gint* array_param, +VALA_EXTERN void foo (gint array_param[3], GAsyncReadyCallback _callback_, gpointer _user_data_); VALA_EXTERN void foo_finish (GAsyncResult* _res_); @@ -50,7 +50,7 @@ foo_data_free (gpointer _data) } void -foo (gint* array_param, +foo (gint array_param[3], GAsyncReadyCallback _callback_, gpointer _user_data_) { diff --git a/tests/basic-types/arrays.c-expected b/tests/basic-types/arrays.c-expected index eb8a047de..5c4a16709 100644 --- a/tests/basic-types/arrays.c-expected +++ b/tests/basic-types/arrays.c-expected @@ -153,9 +153,9 @@ static gboolean _bar_equal (const Bar * s1, static gboolean _vala_bar_array_contains (Bar * stack, gssize stack_length, const Bar * needle); -VALA_EXTERN void give_fixed_array (gint** i); -VALA_EXTERN void take_fixed_array (gint* i); -VALA_EXTERN void change_fixed_array (gint** i); +VALA_EXTERN void give_fixed_array (gint* i[3]); +VALA_EXTERN void take_fixed_array (gint i[3]); +VALA_EXTERN void change_fixed_array (gint* i[3]); VALA_EXTERN void test_fixed_array (void); static void _vala_main (void); static void _vala_array_destroy (gpointer array, @@ -1547,21 +1547,16 @@ test_struct_array (void) } void -give_fixed_array (gint** i) +give_fixed_array (gint* i[3]) { gint _vala_i[3] = {0}; - gint _tmp0_[3] = {0}; - _tmp0_[0] = 3; - _tmp0_[1] = 4; - _tmp0_[2] = 5; - memcpy (_vala_i, _tmp0_, 3 * sizeof (gint)); if (i) { *i = _vala_i; } } void -take_fixed_array (gint* i) +take_fixed_array (gint i[3]) { gint _tmp0_; _vala_assert (3 == 3, "i.length == 3"); @@ -1570,35 +1565,62 @@ take_fixed_array (gint* i) } void -change_fixed_array (gint** i) +change_fixed_array (gint* i[3]) { + gint _tmp0_; _vala_assert (3 == 3, "i.length == 3"); + _tmp0_ = (*i)[1]; + _vala_assert (_tmp0_ == 7, "i[1] == 7"); + (*i)[1] = 9; } void test_fixed_array (void) { - gint i[3] = {0}; - gint _tmp0_[3] = {0}; - gint k[3] = {0}; - gint _tmp1_[3] = {0}; - gint j[3] = {0}; - gint _tmp2_[3] = {0}; + gint* i = NULL; + gint* _tmp0_; + gint i_length1; + gint _i_size_; + gint* k = NULL; + gint* _tmp1_; + gint k_length1; + gint _k_size_; + gint* _tmp2_; + gint _tmp2__length1; + gint* _tmp3_; + gint _tmp3__length1; + gint _tmp4_; + gint* j = NULL; + gint j_length1 = 0; + gint _j_size_ = 0; + _tmp0_ = g_new0 (gint, 4); _tmp0_[0] = 1; _tmp0_[1] = 2; _tmp0_[2] = 3; - memcpy (i, _tmp0_, 3 * sizeof (gint)); - _vala_assert (3 == 3, "i.length == 3"); + _tmp0_[3] = 4; + i = _tmp0_; + i_length1 = 4; + _i_size_ = i_length1; + _vala_assert (i_length1 == 4, "i.length == 4"); take_fixed_array (i); + _tmp1_ = g_new0 (gint, 3); _tmp1_[0] = 6; _tmp1_[1] = 7; _tmp1_[2] = 8; - memcpy (k, _tmp1_, 3 * sizeof (gint)); + k = _tmp1_; + k_length1 = 3; + _k_size_ = k_length1; change_fixed_array (&k); - _vala_assert (3 == 3, "k.length == 3"); - give_fixed_array (&_tmp2_); - memcpy (j, _tmp2_, 3 * sizeof (gint)); - _vala_assert (3 == 3, "j.length == 3"); + _tmp2_ = k; + _tmp2__length1 = k_length1; + _vala_assert (_tmp2__length1 == 3, "k.length == 3"); + _tmp3_ = k; + _tmp3__length1 = k_length1; + _tmp4_ = _tmp3_[1]; + _vala_assert (_tmp4_ == 9, "k[1] == 9"); + j = (g_free (j), NULL); + k = (g_free (k), NULL); + i = (g_free (i), NULL); } static void diff --git a/tests/basic-types/arrays.vala b/tests/basic-types/arrays.vala index 8f5e1c78f..1321c1b31 100644 --- a/tests/basic-types/arrays.vala +++ b/tests/basic-types/arrays.vala @@ -258,7 +258,7 @@ void test_struct_array () { } void give_fixed_array (out int i[3]) { - i = { 3, 4, 5 }; + //FIXME i = { 3, 4, 5 }; } void take_fixed_array (int i[3]) { @@ -268,24 +268,24 @@ void take_fixed_array (int i[3]) { void change_fixed_array (ref int i[3]) { assert (i.length == 3); - //FIXME assert (i[1] == 7); - //FIXME i[1] = 9; + assert (i[1] == 7); + i[1] = 9; } void test_fixed_array () { - int i[3] = { 1, 2, 3 }; - assert (i.length == 3); + int[] i = { 1, 2, 3, 4 }; + assert (i.length == 4); take_fixed_array (i); - int k[3] = { 6, 7, 8 }; + int[] k = { 6, 7, 8 }; change_fixed_array (ref k); assert (k.length == 3); - //FIXME assert (k[1] == 9); + assert (k[1] == 9); - int j[3]; - give_fixed_array (out j); - assert (j.length == 3); - //FIXME assert (j[1] == 4); + int[] j; + //give_fixed_array (out j); + //assert (j.length == 3); + //assert (j[1] == 4); } void main () { diff --git a/tests/methods/parameter-fixed-array-initializer.c-expected b/tests/methods/parameter-fixed-array-initializer.c-expected index fdc6d729d..b940fab63 100644 --- a/tests/methods/parameter-fixed-array-initializer.c-expected +++ b/tests/methods/parameter-fixed-array-initializer.c-expected @@ -32,8 +32,8 @@ struct _Bar { VALA_EXTERN GType bar_get_type (void) G_GNUC_CONST ; VALA_EXTERN Bar* bar_dup (const Bar* self); VALA_EXTERN void bar_free (Bar* self); -VALA_EXTERN void foo (gint* a); -VALA_EXTERN void bar (Bar* b); +VALA_EXTERN void foo (gint a[3]); +VALA_EXTERN void bar (Bar b[3]); static void _vala_main (void); Bar* @@ -72,7 +72,7 @@ bar_get_type (void) } void -foo (gint* a) +foo (gint a[3]) { gint _tmp0_; _tmp0_ = a[2]; @@ -80,7 +80,7 @@ foo (gint* a) } void -bar (Bar* b) +bar (Bar b[3]) { Bar _tmp0_; Bar _tmp1_; diff --git a/tests/parser/array-length.c-expected b/tests/parser/array-length.c-expected index 8cca3c9c3..9ef0f8f97 100644 --- a/tests/parser/array-length.c-expected +++ b/tests/parser/array-length.c-expected @@ -13,7 +13,7 @@ #endif #endif -typedef guint8* (*FooFunc) (guint8* param0, gsize param0_length1, guint8** param1, gint64* param1_length1, guint8** param2, guint* param2_length1, guint8* param3, guint64* result_length1, gpointer user_data); +typedef guint8* (*FooFunc) (guint8* param0, gsize param0_length1, guint8** param1, gint64* param1_length1, guint8** param2, guint* param2_length1, guint8 param3[42], guint64* result_length1, gpointer user_data); VALA_EXTERN guint8* field0; VALA_EXTERN gssize field0_length1; @@ -29,7 +29,7 @@ VALA_EXTERN guint8* func (guint8* param0, gint64* param1_length1, guint8** param2, guint* param2_length1, - guint8* param3, + guint8 param3[42], guint64* result_length1); static void _vala_main (void); @@ -40,7 +40,7 @@ func (guint8* param0, gint64* param1_length1, guint8** param2, guint* param2_length1, - guint8* param3, + guint8 param3[42], guint64* result_length1) { guint8* _vala_param2 = NULL; diff --git a/tests/posix/arrays.c-expected b/tests/posix/arrays.c-expected index 20eb68d25..b59712f6b 100644 --- a/tests/posix/arrays.c-expected +++ b/tests/posix/arrays.c-expected @@ -144,9 +144,9 @@ static bool _bar_equal (const Bar * s1, static bool _vala_bar_array_contains (Bar * stack, ssize_t stack_length, const Bar * needle); -VALA_EXTERN void give_fixed_array (int** i); -VALA_EXTERN void take_fixed_array (int* i); -VALA_EXTERN void change_fixed_array (int** i); +VALA_EXTERN void give_fixed_array (int* i[3]); +VALA_EXTERN void take_fixed_array (int i[3]); +VALA_EXTERN void change_fixed_array (int* i[3]); VALA_EXTERN void test_fixed_array (void); static void _vala_main (void); static void _vala_array_destroy (void* array, @@ -1525,21 +1525,16 @@ test_struct_array (void) } void -give_fixed_array (int** i) +give_fixed_array (int* i[3]) { int _vala_i[3] = {0}; - int _tmp0_[3] = {0}; - _tmp0_[0] = 3; - _tmp0_[1] = 4; - _tmp0_[2] = 5; - memcpy (_vala_i, _tmp0_, 3 * sizeof (int)); if (i) { *i = _vala_i; } } void -take_fixed_array (int* i) +take_fixed_array (int i[3]) { int _tmp0_; assert (3 == 3); @@ -1548,35 +1543,62 @@ take_fixed_array (int* i) } void -change_fixed_array (int** i) +change_fixed_array (int* i[3]) { + int _tmp0_; assert (3 == 3); + _tmp0_ = (*i)[1]; + assert (_tmp0_ == 7); + (*i)[1] = 9; } void test_fixed_array (void) { - int i[3] = {0}; - int _tmp0_[3] = {0}; - int k[3] = {0}; - int _tmp1_[3] = {0}; - int j[3] = {0}; - int _tmp2_[3] = {0}; + int* i = NULL; + int* _tmp0_; + int i_length1; + int _i_size_; + int* k = NULL; + int* _tmp1_; + int k_length1; + int _k_size_; + int* _tmp2_; + int _tmp2__length1; + int* _tmp3_; + int _tmp3__length1; + int _tmp4_; + int* j = NULL; + int j_length1 = 0; + int _j_size_ = 0; + _tmp0_ = calloc (4, sizeof (int)); _tmp0_[0] = 1; _tmp0_[1] = 2; _tmp0_[2] = 3; - memcpy (i, _tmp0_, 3 * sizeof (int)); - assert (3 == 3); + _tmp0_[3] = 4; + i = _tmp0_; + i_length1 = 4; + _i_size_ = i_length1; + assert (i_length1 == 4); take_fixed_array (i); + _tmp1_ = calloc (3, sizeof (int)); _tmp1_[0] = 6; _tmp1_[1] = 7; _tmp1_[2] = 8; - memcpy (k, _tmp1_, 3 * sizeof (int)); + k = _tmp1_; + k_length1 = 3; + _k_size_ = k_length1; change_fixed_array (&k); - assert (3 == 3); - give_fixed_array (&_tmp2_); - memcpy (j, _tmp2_, 3 * sizeof (int)); - assert (3 == 3); + _tmp2_ = k; + _tmp2__length1 = k_length1; + assert (_tmp2__length1 == 3); + _tmp3_ = k; + _tmp3__length1 = k_length1; + _tmp4_ = _tmp3_[1]; + assert (_tmp4_ == 9); + j = (free (j), NULL); + k = (free (k), NULL); + i = (free (i), NULL); } static void -- cgit v1.2.1