From 1f073c9270d23a01f59b6871f127d99d54bd6670 Mon Sep 17 00:00:00 2001 From: Eric Botcazou Date: Tue, 5 Sep 2017 09:12:07 +0000 Subject: [PATCH] utils.c (unchecked_convert): When the result type is a non-biased integral type with size 0... * gcc-interface/utils.c (unchecked_convert): When the result type is a non-biased integral type with size 0, set the result to 0 directly. From-SVN: r251701 --- gcc/ada/ChangeLog | 5 +++++ gcc/ada/gcc-interface/utils.c | 34 +++++++++++++++++------------ gcc/testsuite/ChangeLog | 4 ++++ gcc/testsuite/gnat.dg/specs/uc2.ads | 18 +++++++++++++++ 4 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 gcc/testsuite/gnat.dg/specs/uc2.ads diff --git a/gcc/ada/ChangeLog b/gcc/ada/ChangeLog index a263b95e08b..0dbc77918de 100644 --- a/gcc/ada/ChangeLog +++ b/gcc/ada/ChangeLog @@ -1,3 +1,8 @@ +2017-09-05 Eric Botcazou + + * gcc-interface/utils.c (unchecked_convert): When the result type is a + non-biased integral type with size 0, set the result to 0 directly. + 2017-09-05 Eric Botcazou * gcc-interface/gigi.h (renaming_from_generic_instantiation_p): Turn to diff --git a/gcc/ada/gcc-interface/utils.c b/gcc/ada/gcc-interface/utils.c index b0f6d2dba1e..1c83a08d5bd 100644 --- a/gcc/ada/gcc-interface/utils.c +++ b/gcc/ada/gcc-interface/utils.c @@ -5257,20 +5257,26 @@ unchecked_convert (tree type, tree expr, bool notrunc_p) ? TYPE_RM_SIZE (etype) : TYPE_SIZE (etype)) == 0))) { - tree base_type - = gnat_type_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)), - type_unsigned_for_rm (type)); - tree shift_expr - = convert (base_type, - size_binop (MINUS_EXPR, - TYPE_SIZE (type), TYPE_RM_SIZE (type))); - expr - = convert (type, - build_binary_op (RSHIFT_EXPR, base_type, - build_binary_op (LSHIFT_EXPR, base_type, - convert (base_type, expr), - shift_expr), - shift_expr)); + if (integer_zerop (TYPE_RM_SIZE (type))) + expr = build_int_cst (type, 0); + else + { + tree base_type + = gnat_type_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)), + type_unsigned_for_rm (type)); + tree shift_expr + = convert (base_type, + size_binop (MINUS_EXPR, + TYPE_SIZE (type), TYPE_RM_SIZE (type))); + expr + = convert (type, + build_binary_op (RSHIFT_EXPR, base_type, + build_binary_op (LSHIFT_EXPR, base_type, + convert (base_type, + expr), + shift_expr), + shift_expr)); + } } /* An unchecked conversion should never raise Constraint_Error. The code diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index f6210be478b..68c4076278d 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2017-09-05 Eric Botcazou + + * gnat.dg/specs/uc2.ads: New test. + 2017-09-05 Eric Botcazou * gnat.dg/taft_type4.adb: New test. diff --git a/gcc/testsuite/gnat.dg/specs/uc2.ads b/gcc/testsuite/gnat.dg/specs/uc2.ads new file mode 100644 index 00000000000..84d4e043291 --- /dev/null +++ b/gcc/testsuite/gnat.dg/specs/uc2.ads @@ -0,0 +1,18 @@ +-- { dg-do compile } +-- { dg-options "-O" } + +with Ada.Unchecked_Conversion; + +package UC2 is + + subtype Word_Type is Integer range 0 .. 0; + type Arr is array (1 .. Word_Type'Size) of Boolean; + pragma Pack(Arr); + + function Conv is + new Ada.Unchecked_Conversion (Source => Arr, Target => Word_Type); + + A : Arr; + W : Word_Type := Conv(A); + +end UC2; -- 2.30.2