This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: ISO C binding and character variables


hi all,

i modified the way by-value character args are handled for bind(c) procedures and i want to see if it's behaving more as expected.

the following code:

module c_char_tests
  use, intrinsic :: iso_c_binding, only: c_char
  implicit none
contains
  subroutine param_test(my_char, my_char_2) bind(c)
    character(c_char), value :: my_char
    character(c_char), value :: my_char_2
    if(my_char /= c_char_'y') call abort()
    if(my_char_2 /= c_char_'z') call abort()
  end subroutine param_test
end module c_char_tests

produces the following output w/ my patch applied:

param_test (my_char, my_char_2, _my_char, _my_char_2)
{
  if (my_char != 121)
    {
      _gfortran_abort ();
    }
  if (my_char_2 != 122)
    {
      _gfortran_abort ();
    }

and the following w/o my patch:

param_test (my_char, my_char_2, _my_char, _my_char_2)
{
  if (my_char[1]{lb: 1 sz: 1} != 121)
    {
      _gfortran_abort ();
    }
  if (my_char_2[1]{lb: 1 sz: 1} != 122)
    {
      _gfortran_abort ();
    }

the code runs as expected on my box, but i can't test it on the arch in question. also, for non-bind(c), there is no change from what is currently done.

thoughts?
Chris

On Wed, 11 Jul 2007, Tobias Burnus wrote:

Hi again,

one has to be careful though; assuming the following file (based on the
original example):

--------------------
subroutine param_test(my_char) bind(c)
 use iso_c_binding
 character(c_char), value :: my_char
 if(my_char /= c_char_'y') stop 'error'
end subroutine param_test
--------------------

If now "my_char" is a scalar variable ("char my_char_") then one needs
to remember this later on in the program; otherwise one does the same
mistake as NAG f95 and produces:

----------------
void param_test(char my_char_)
{
 if ( *my_char_!=121)
   __NAGf90_stop("error");
}
----------------

That is: One uses "*my_char" although my_char is not a pointer. And as
Steve E. has shown simply assuming "char[]" does not work on IA64 HP-UX
and thus one needs to go through all trans-* and make sure it works.

Who has ever claimed that ISO C Binding is trivial?

As post script:
I filled this as PR32732

As another post script:

The extra length parameter(s) is/are not nice, but harmless: It/They
always come(s) last in the argument list and can therefore easily be
ignored. And while the parameter is present in the argument list, it is
never used.
I'm almost sure, however, that someone will find a platform or scenario
where this fails.

Tobias



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]