fortran: Fix up gfc_typename CHARACTER length handling [PR97768]
authorJakub Jelinek <jakub@redhat.com>
Wed, 11 Nov 2020 07:27:38 +0000 (08:27 +0100)
committerJakub Jelinek <jakub@redhat.com>
Wed, 11 Nov 2020 07:29:17 +0000 (08:29 +0100)
commit81372618277bfae682434fcdc80b311ee6007476
tree5b3f8e8eea65ef33aa42f6d0fdad4ae4acbb971f
parent693a79a355e13b3cf381d4adebe3fcea2736c8f0
fortran: Fix up gfc_typename CHARACTER length handling [PR97768]

The first testcase below ICEs when f951 is 32-bit (or 64-bit big-endian).
The problem is that ex->ts.u.cl && ex->ts.u.cl->length are both non-NULL,
but ex->ts.u.cl->length->expr_type is not EXPR_CONSTANT, but EXPR_FUNCTION.
value.function.actual and value.function.name are in that case pointers,
but value._mp_alloc and value._mp_size are 4 byte integers no matter what.
So, in 64-bit little-endian the function returns most of the time incorrect
CHARACTER(0) because the most significant 32 bits of the
value.function.actual pointer are likely 0.
Anyway, the following patch is an attempt to get all the cases right.
Uses ex->value.character.length only for ex->expr_type == EXPR_CONSTANT
(i.e. CHARACTER literals), handles the deferred lengths, assumed lengths,
known constant lengths and finally if the length is something other,
just doesn't print it, i.e. prints just CHARACTER (for default kind)
or CHARACTER(KIND=4) (for e.g. kind 4).

2020-11-11  Jakub Jelinek  <jakub@redhat.com>

PR fortran/97768
gcc/fortran/
* misc.c (gfc_typename): Use ex->value.character.length only if
ex->expr_type == EXPR_CONSTANT.  If ex->ts.deferred, print : instead
of length.  If ex->ts.u.cl && ex->ts.u.cl->length == NULL, print *
instead of length.  Otherwise if character length is non-constant,
print just CHARACTER or CHARACTER(KIND=N).
gcc/testsuite/
* gfortran.dg/pr97768_1.f90: New test.
* gfortran.dg/pr97768_2.f90: New test.
gcc/fortran/misc.c
gcc/testsuite/gfortran.dg/pr97768_1.f90 [new file with mode: 0644]
gcc/testsuite/gfortran.dg/pr97768_2.f90 [new file with mode: 0644]