[Bug fortran/92482] BIND(C) with array-descriptor mishandled for type character

2022-01-09 Thread pinskia at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92482

Andrew Pinski  changed:

   What|Removed |Added

   Target Milestone|--- |12.0

[Bug fortran/92482] BIND(C) with array-descriptor mishandled for type character

2021-10-22 Thread sandra at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92482

sandra at gcc dot gnu.org changed:

   What|Removed |Added

 CC||everythingfunctional@proton
   ||mail.com

--- Comment #11 from sandra at gcc dot gnu.org ---
*** Bug 99922 has been marked as a duplicate of this bug. ***

[Bug fortran/92482] BIND(C) with array-descriptor mishandled for type character

2021-10-19 Thread burnus at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92482

Tobias Burnus  changed:

   What|Removed |Added

 Resolution|--- |FIXED
 Status|NEW |RESOLVED

--- Comment #10 from Tobias Burnus  ---
Close as FIXED (in GCC 12) now that also the remaining issue is solved :-)

Thanks again for the report!

[Bug fortran/92482] BIND(C) with array-descriptor mishandled for type character

2021-10-19 Thread cvs-commit at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92482

--- Comment #9 from CVS Commits  ---
The master branch has been updated by Tobias Burnus :

https://gcc.gnu.org/g:6920d5a1a2834e9c62d441b8f4c6186b01107d13

commit r12-4505-g6920d5a1a2834e9c62d441b8f4c6186b01107d13
Author: Tobias Burnus 
Date:   Tue Oct 19 15:16:01 2021 +0200

Fortran: Fix "str" to scalar descriptor conversion [PR92482]

PR fortran/92482
gcc/fortran/ChangeLog:

* trans-expr.c (gfc_conv_procedure_call): Use TREE_OPERAND not
build_fold_indirect_ref_loc to undo an ADDR_EXPR.

gcc/testsuite/ChangeLog:

* gfortran.dg/bind-c-char-descr.f90: Remove xfail; extend a bit.

[Bug fortran/92482] BIND(C) with array-descriptor mishandled for type character

2021-10-19 Thread burnus at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92482

Tobias Burnus  changed:

   What|Removed |Added

   Keywords||wrong-code

--- Comment #8 from Tobias Burnus  ---
The BIND(C) issue is fixed - but I keep this PR open as there is still an issue
with the non-bind(C) part. Namely:

   testsuite/gfortran.dg/bind-c-char-descr.f90

fails for:

...
  subroutine strg_print_2(this, xfail)
if(associated(strn))then
  if (len (strn) /= 3) stop 31
  if (strn /= "abc") then
if (present (xfail)) then
  print *, 'INVALID STRING - EXPECTED "abc" / PR47225' ! <<< PR is
wrong :-/
else
  stop 32
end if

The call has:

scalar.4 = 97;   // that's  'a' -> expected: "abc".
desc.3.dtype = {.elem_len=1, .rank=0, .type=1}; // not the elem_len=1;
should be len*kind
desc.3.data = (void *) 
desc.3.span = (integer(kind=8)) desc.3.dtype.elem_len;

[Bug fortran/92482] BIND(C) with array-descriptor mishandled for type character

2021-10-18 Thread cvs-commit at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92482

--- Comment #7 from CVS Commits  ---
The master branch has been updated by Tobias Burnus :

https://gcc.gnu.org/g:64f9623765da3306b0ab6a47997dc5d62c2ea261

commit r12-4467-g64f9623765da3306b0ab6a47997dc5d62c2ea261
Author: Tobias Burnus 
Date:   Mon Oct 18 09:51:36 2021 +0200

Fortran: Fix Bind(C) Array-Descriptor Conversion

gfortran uses internally a different array descriptor ("gfc") as
Fortran 2018 alias TS291113 defines for C interoperability via
ISO_Fortran_binding.h ("CFI").  Hence, when calling a C function
from Fortran, it has to be converted in the callee - and if a
BIND(C) procedure is written in Fortran, the CFI argument has
to be converted to gfc in order work with the rest of the FE
code and the library calls.

Before this patch, part was handled in the FE generated code and
other parts in libgfortran.  With this patch, all code is generated
and CFI is defined as proper type - visible in the debugger and to
the middle end - avoiding both alias issues and missed optimization
issues.

This patch also fixes issues like: intent(out) deallocation in
the bind(C) callee, using the CFI descriptor also for allocatable
and pointer scalars and for len=* character strings.
For 'select rank', it also optimizes the code + avoid accessing
uninitialized memory if the dummy argument is allocatable/a pointer.
It additionally rejects passing a descriptorless type(*) to an
assumed-rank dummy argument. [F2018:C711]

PR fortran/102086
PR fortran/92189
PR fortran/92621
PR fortran/101308
PR fortran/101309
PR fortran/101635
PR fortran/92482

gcc/fortran/ChangeLog:

* decl.c (gfc_verify_c_interop_param): Remove 'sorry' for
scalar allocatable/pointer and len=*.
* expr.c (is_CFI_desc): Return true for for those.
* gfortran.h (CFI_type_kind_shift, CFI_type_mask,
CFI_type_from_type_kind, CFI_VERSION, CFI_MAX_RANK,
CFI_attribute_pointer, CFI_attribute_allocatable,
CFI_attribute_other, CFI_type_Integer, CFI_type_Logical,
CFI_type_Real, CFI_type_Complex, CFI_type_Character,
CFI_type_ucs4_char, CFI_type_struct, CFI_type_cptr,
CFI_type_cfunptr, CFI_type_other): New #define.
* trans-array.c (CFI_FIELD_BASE_ADDR, CFI_FIELD_ELEM_LEN,
CFI_FIELD_VERSION, CFI_FIELD_RANK, CFI_FIELD_ATTRIBUTE,
CFI_FIELD_TYPE, CFI_FIELD_DIM, CFI_DIM_FIELD_LOWER_BOUND,
CFI_DIM_FIELD_EXTENT, CFI_DIM_FIELD_SM,
gfc_get_cfi_descriptor_field, gfc_get_cfi_desc_base_addr,
gfc_get_cfi_desc_elem_len, gfc_get_cfi_desc_version,
gfc_get_cfi_desc_rank, gfc_get_cfi_desc_type,
gfc_get_cfi_desc_attribute, gfc_get_cfi_dim_item,
gfc_get_cfi_dim_lbound, gfc_get_cfi_dim_extent,
gfc_get_cfi_dim_sm):
New define/functions to access the CFI array descriptor.
(gfc_conv_descriptor_type): New function for the GFC descriptor.
(gfc_get_array_span): Handle expr of CFI descriptors and
assumed-type descriptors.
(gfc_trans_array_bounds): Remove 'static'.
(gfc_conv_expr_descriptor): For assumed type, use the dtype of
the actual argument.
(structure_alloc_comps): Remove ' ' inside tabs.
* trans-array.h (gfc_trans_array_bounds, gfc_conv_descriptor_type,
gfc_get_cfi_desc_base_addr, gfc_get_cfi_desc_elem_len,
gfc_get_cfi_desc_version, gfc_get_cfi_desc_rank,
gfc_get_cfi_desc_type, gfc_get_cfi_desc_attribute,
gfc_get_cfi_dim_lbound, gfc_get_cfi_dim_extent,
gfc_get_cfi_dim_sm):
New prototypes.
* trans-decl.c (gfor_fndecl_cfi_to_gfc, gfor_fndecl_gfc_to_cfi):
Remove global vars.
(gfc_build_builtin_function_decls): Remove their initialization.
(gfc_get_symbol_decl, create_function_arglist,
gfc_trans_deferred_vars): Update for CFI.
(convert_CFI_desc): Remove and replace by ...
(gfc_conv_cfi_to_gfc): ... this function
(gfc_generate_function_code): Call it; create local GFC var for
CFI.
* trans-expr.c (gfc_maybe_dereference_var): Handle CFI.
(gfc_conv_subref_array_arg): Handle the if-noncontigous-only copy
in
when the result should be a descriptor.
(gfc_conv_gfc_desc_to_cfi_desc): Completely rewritten.
(gfc_conv_procedure_call): CFI fixes.
* trans-openmp.c (gfc_omp_is_optional_argument,
gfc_omp_check_optional_argument): Handle optional
CFI.
* trans-stmt.c (gfc_trans_select_rank_cases): Cleanup, avoid
invalid
code for allocatable/pointer dummies, which cannot be assumed size.
 

[Bug fortran/92482] BIND(C) with array-descriptor mishandled for type character

2021-08-31 Thread burnus at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92482

Tobias Burnus  changed:

   What|Removed |Added

   Last reconfirmed||2021-08-31
 Ever confirmed|0   |1
 Status|UNCONFIRMED |NEW

--- Comment #6 from Tobias Burnus  ---
With my bind-C array descriptor patch, all tests except of 
strg_print_2("abc").
(To be submitted; I will include this testcase as
gfortran.dg/bind-c-char-descr.f90
with the failing test commented.)

The still FAILING testcase does *not* use BIND(C) - and only TYPE(*) with
assumed-rank: 
That test passes this string to a gfortran array descriptor for type:
  type(*), target, intent(in) :: this(..)
which is then
  c_f_pointer(c_loc(this), strn)
to a Fortran pointer.

The call has:

scalar.4 = 97;   // that's  'a' -> expected: "abc".
desc.3.dtype = {.elem_len=1, .rank=0, .type=1}; // not the elem_len=1;
should be len*kind
desc.3.data = (void *) 
desc.3.span = (integer(kind=8)) desc.3.dtype.elem_len;

[Bug fortran/92482] BIND(C) with array-descriptor mishandled for type character

2021-07-27 Thread burnus at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92482

--- Comment #5 from Tobias Burnus  ---
Crossref:
See also PR 100906 "Bind(c): failure handling character with len/=1"
and PR 100906 comment 4 regarding a status (what needs to be done).

[Bug fortran/92482] BIND(C) with array-descriptor mishandled for type character

2021-07-25 Thread sandra at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92482

sandra at gcc dot gnu.org changed:

   What|Removed |Added

 CC||sandra at gcc dot gnu.org

--- Comment #4 from sandra at gcc dot gnu.org ---
Tobias's recent commit (which he forgot to tag with this issue) changed the
"must be length 1" messages to something more descriptive, but the
functionality itself still isn't working.

commit b3d4011ba10275fbd5d6ec5a16d5aaebbdfb5d3c
Author: Tobias Burnus 
Date:   Wed Jul 21 09:36:48 2021 +0200

Fortran: Fix bind(C) character length checks

gcc/fortran/ChangeLog:

* decl.c (gfc_verify_c_interop_param): Update for F2008 + F2018
changes; reject unsupported bits with 'Error: Sorry,'.
* trans-expr.c (gfc_conv_procedure_call): Fix condition to
For using CFI descriptor with characters.

gcc/testsuite/ChangeLog:

* gfortran.dg/iso_c_binding_char_1.f90: Update dg-error.
* gfortran.dg/pr32599.f03: Use -std=-f2003 + update comment.
* gfortran.dg/bind_c_char_10.f90: New test.
* gfortran.dg/bind_c_char_6.f90: New test.
* gfortran.dg/bind_c_char_7.f90: New test.
* gfortran.dg/bind_c_char_8.f90: New test.
* gfortran.dg/bind_c_char_9.f90: New test.

[Bug fortran/92482] BIND(C) with array-descriptor mishandled for type character

2021-04-29 Thread ivan.tubert-brohman at schrodinger dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92482

Ivan Tubert-Brohman  changed:

   What|Removed |Added

 CC||ivan.tubert-brohman@schrodi
   ||nger.com

--- Comment #3 from Ivan Tubert-Brohman  ---
I also encountered this issue. I was trying the example given by Metcalf et al.
on _Modern Fortran Explained_ (2018), section 21.4, "Assumed character length":

  interface
 subroutine err_msg(string) bind(c)
use iso_c_binding
character(*, c_char), intent(in):: string
 end subroutine err_msg
  end interface

The example worked with the Intel Fortran compiler, but with gfortran 11.1 I
get

  Error: Character argument ‘string’ at (1) must be length 1 because procedure
‘err_msg’ is BIND(C)

I only comment to confirm that both a famous author and an alternative compiler
implementation agree with the interpretation of the standard that this should
work. :-)

[Bug fortran/92482] BIND(C) with array-descriptor mishandled for type character

2019-11-13 Thread jrfsousa at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92482

--- Comment #2 from José Rui Faustino de Sousa  ---
IMHO the point here is if interoperable procedures (bind(c)) are required to
have arguments of interoperable type.

My reading of 18.3.6 is that it is not required, most relevant for character
type would be 18.3.6.2 (5) and (7). Which, like you write, imply the use of a C
descriptor. If I understood it correctly the descriptor should be scalar
(rank=0) and have "elem_len" equal to character length.

The error in 10.0.0 is raised without using any flags and the warning in 9.1.0
requires -Wall -Wmaybe-uninitialized.

Please notice that the problem shown by the "strg_print_2" procedure is without
using "bind(c)".

Thank you very much.

Best regards,
José Rui

[Bug fortran/92482] BIND(C) with array-descriptor mishandled for type character

2019-11-13 Thread burnus at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92482

Tobias Burnus  changed:

   What|Removed |Added

 CC||burnus at gcc dot gnu.org,
   ||pault at gcc dot gnu.org
Summary|Possibly wrong error|BIND(C) with
   |diagnostic  |array-descriptor mishandled
   ||for type character

--- Comment #1 from Tobias Burnus  ---
Pre-remark: I haven't looked at your code at all, just as your remark regarding
the standard.

I think at least older versions of the standard had such a restriction to avoid
the issue with passing the "len=" argument along. (Fortran by default passes
the length as hidden argument). The solution was to use:

subroutine test(foo) bind(C)
  character(kind=c_char,len=1), dimension(*) :: foo

And due to argument association, call test("Hello World"//c_null_char) still
worked/works.

Let's check F2008: "15.3.2 Interoperability of intrinsic types"
"[…] If the type is character, the length type parameter is interoperable21if
and only if its value is one."

And F2018 has the same wording in 18.3.1!


However, TS29113 and Fortran 2018 added support for array descriptors – and in
the array descriptor, one can encode the character length in elem_len (it's the
length as c_char is one-byte wide.)

Thus, a context where a descriptor is used with C binding, a len >1 or len=: is
permitted.  

That's not that clearly written in the standard but implicitly at multiple
locations in Chapter 18. For instance in the following, which also lists when
an array descriptor is used:

"The source file ISO_Fortran_binding.h provides definitions and prototypes to
enable a C function to interoperate with a Fortran procedure that has a dummy
data object that is allocatable, assumed-shape, assumed-rank, pointer, or is of
type character with an assumed length." (18.1p3)

An usage example is in 18.2.3, example case (iv) which uses both an explicit
length > 1 and an assumed length.

And the elem_len is mentioned in Note 2 of 18.5.3. – And there several other
places, from which one has to collect this data.


Hence, you have encountered a left-over warning (I assume: error w/ -std=f…)
from Fortran 2003/2008 which has not been updated for Fortran 2018 – plus bugs
in the implementation of the F2018 feature.