poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] pkl: Fix size calculation for complete union types


From: Mohammad-Reza Nabipoor
Subject: [PATCH] pkl: Fix size calculation for complete union types
Date: Wed, 2 Feb 2022 22:58:22 +0330

2022-02-02  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>

        * libpoke/pkl-ast.c (pkl_ast_sizeof_type): Add case for complete
        unions.
        * testsuite/poke.pkl/sizeof-14.pk: New test.
        * testsuite/poke.pkl/sizeof-15.pk: Likewise.
        * testsuite/poke.pkl/sizeof-16.pk: Likewise.
        * testsuite/poke.pkl/sizeof-diag-2.pk: Likewise.
        * testsuite/Makefile.am (EXTRA_DIST): Update.
---
 ChangeLog                           | 10 ++++++++++
 libpoke/pkl-ast.c                   | 11 +++++++++--
 testsuite/Makefile.am               |  4 ++++
 testsuite/poke.pkl/sizeof-14.pk     |  7 +++++++
 testsuite/poke.pkl/sizeof-15.pk     |  7 +++++++
 testsuite/poke.pkl/sizeof-16.pk     |  7 +++++++
 testsuite/poke.pkl/sizeof-diag-2.pk |  8 ++++++++
 7 files changed, 52 insertions(+), 2 deletions(-)
 create mode 100644 testsuite/poke.pkl/sizeof-14.pk
 create mode 100644 testsuite/poke.pkl/sizeof-15.pk
 create mode 100644 testsuite/poke.pkl/sizeof-16.pk
 create mode 100644 testsuite/poke.pkl/sizeof-diag-2.pk

diff --git a/ChangeLog b/ChangeLog
index fab003b6..4f75fb4f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2022-02-02  Mohammad-Reza Nabipoor  <mnabipoor@gnu.org>
+
+       * libpoke/pkl-ast.c (pkl_ast_sizeof_type): Add case for complete
+       unions.
+       * testsuite/poke.pkl/sizeof-14.pk: New test.
+       * testsuite/poke.pkl/sizeof-15.pk: Likewise.
+       * testsuite/poke.pkl/sizeof-16.pk: Likewise.
+       * testsuite/poke.pkl/sizeof-diag-2.pk: Likewise.
+       * testsuite/Makefile.am (EXTRA_DIST): Update.
+
 2022-02-02  Jose E. Marchesi  <jemarch@gnu.org>
 
        * cfg.mk (sc_rockdabootism_missing_space): Remove syntax-check
diff --git a/libpoke/pkl-ast.c b/libpoke/pkl-ast.c
index 4f6279df..9246bcfc 100644
--- a/libpoke/pkl-ast.c
+++ b/libpoke/pkl-ast.c
@@ -1079,12 +1079,19 @@ pkl_ast_sizeof_type (pkl_ast ast, pkl_ast_node type)
                     || PKL_AST_CODE (field_label) == PKL_AST_OFFSET);
             assert (PKL_AST_STRUCT_TYPE_FIELD_OPTCOND (t) == NULL);
 
-            /* If struct is pinned, the new size is
+            /* All fields of a complete union have the same size.
+               If struct is pinned, the new size is
                `max (size, elem_type_size)`.
                Otherwise if the field has a constant label, the new size
                is `max (size, label_in_bits + elem_type_size)'.
                Otherwise, it is `size + elem_type_size'.  */
-            if (PKL_AST_TYPE_S_PINNED_P (type))
+            if (PKL_AST_TYPE_S_UNION_P (type))
+              {
+                res = ASTREF (elem_type_size);
+                PKL_AST_TYPE (res) = ASTREF (res_type);
+                break;
+              }
+            else if (PKL_AST_TYPE_S_PINNED_P (type))
               {
                 pkl_ast_node cond;
 
diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
index 254c1435..117a1955 100644
--- a/testsuite/Makefile.am
+++ b/testsuite/Makefile.am
@@ -1914,7 +1914,11 @@ EXTRA_DIST = \
   poke.pkl/sizeof-11.pk \
   poke.pkl/sizeof-12.pk \
   poke.pkl/sizeof-13.pk \
+  poke.pkl/sizeof-14.pk \
+  poke.pkl/sizeof-15.pk \
+  poke.pkl/sizeof-16.pk \
   poke.pkl/sizeof-diag-1.pk \
+  poke.pkl/sizeof-diag-2.pk \
   poke.pkl/sl-diag-1.pk \
   poke.pkl/sl-diag-2.pk \
   poke.pkl/sl-diag-3.pk \
diff --git a/testsuite/poke.pkl/sizeof-14.pk b/testsuite/poke.pkl/sizeof-14.pk
new file mode 100644
index 00000000..8f44e351
--- /dev/null
+++ b/testsuite/poke.pkl/sizeof-14.pk
@@ -0,0 +1,7 @@
+/* { dg-do run } */
+
+type union_for_sizeof = union { long a == 10; long b; };
+
+/* { dg-command {  .set obase 10  } } */
+/* { dg-command {  sizeof (union_for_sizeof) } } */
+/* { dg-output "64UL#b" } */
diff --git a/testsuite/poke.pkl/sizeof-15.pk b/testsuite/poke.pkl/sizeof-15.pk
new file mode 100644
index 00000000..92844848
--- /dev/null
+++ b/testsuite/poke.pkl/sizeof-15.pk
@@ -0,0 +1,7 @@
+/* { dg-do run } */
+
+type union_for_sizeof = union { long a == 10; byte[a'size] b; };
+
+/* { dg-command {  .set obase 10  } } */
+/* { dg-command {  sizeof (union_for_sizeof) } } */
+/* { dg-output "64UL#b" } */
diff --git a/testsuite/poke.pkl/sizeof-16.pk b/testsuite/poke.pkl/sizeof-16.pk
new file mode 100644
index 00000000..3e112919
--- /dev/null
+++ b/testsuite/poke.pkl/sizeof-16.pk
@@ -0,0 +1,7 @@
+/* { dg-do run } */
+
+type union_for_sizeof = union { int[1+1] a == [-1, 1]; long b; };
+
+/* { dg-command {  .set obase 10  } } */
+/* { dg-command {  sizeof (union_for_sizeof) } } */
+/* { dg-output "64UL#b" } */
diff --git a/testsuite/poke.pkl/sizeof-diag-2.pk 
b/testsuite/poke.pkl/sizeof-diag-2.pk
new file mode 100644
index 00000000..f01b7460
--- /dev/null
+++ b/testsuite/poke.pkl/sizeof-diag-2.pk
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+
+fun foo = offset<uint<64>,b>:
+  {
+    type T = union { long a == 10; int b; };
+
+    return sizeof (T); /* { dg-error "" } */
+  }
-- 
2.35.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]