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've attached an initial patch that changes how by-value character dummy
args of bind(c) procedures are handled.  it bootstraps and regtests on
x86, but definitely needs tested on other archs.

thanks.
Chris

>> 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.
>> [...]
>>
>> param_test (my_char, my_char_2, _my_char, _my_char_2)
>> {
>>   if (my_char != 121)
>>   if (my_char_2 != 122)
> I think it looks ok now. But you need to attach the patch so one can
> check and/or test it ;-)
>
> I wonder whether for non-VALUE scalar, bind(c) character variables one
> should do something similar:
>   if ((*a)[1]{lb: 1 sz: 1} != 49)
> would become
>   if(*a != 49)
> However, I don't know whether any platform or the middle end cares.
Index: gcc/testsuite/gfortran.dg/c_char_tests.f03
===================================================================
--- gcc/testsuite/gfortran.dg/c_char_tests.f03	(revision 0)
+++ gcc/testsuite/gfortran.dg/c_char_tests.f03	(revision 0)
@@ -0,0 +1,28 @@
+! { dg-do run }
+! { dg-additional-sources c_char_driver.c }
+! Verify that character dummy arguments for bind(c) procedures can work both 
+! by-value and by-reference when called by either C or Fortran.
+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()
+    
+    call sub1(my_char)
+  end subroutine param_test
+
+  subroutine sub0() bind(c)
+    call param_test('y', 'z')
+  end subroutine sub0
+
+  subroutine sub1(my_char_ref) bind(c)
+    character(c_char) :: my_char_ref
+    if(my_char_ref /= c_char_'y') call abort()
+  end subroutine sub1
+end module c_char_tests
+
+! { dg-final { cleanup-modules "c_char_tests" } }
Index: gcc/testsuite/gfortran.dg/c_char_tests_2.f03
===================================================================
--- gcc/testsuite/gfortran.dg/c_char_tests_2.f03	(revision 0)
+++ gcc/testsuite/gfortran.dg/c_char_tests_2.f03	(revision 0)
@@ -0,0 +1,32 @@
+! { dg-do run }
+! Verify that the changes made to character dummy arguments for bind(c) 
+! procedures doesn't break non-bind(c) routines.
+subroutine bar(a)
+  use, intrinsic :: iso_c_binding, only: c_char
+  character(c_char), value :: a
+  if(a /= c_char_'a') call abort()
+end subroutine bar
+
+subroutine bar2(a)
+  use, intrinsic :: iso_c_binding, only: c_char
+  character(c_char) :: a
+  if(a /= c_char_'a') call abort()
+end subroutine bar2
+
+use iso_c_binding
+implicit none
+interface
+  subroutine bar(a)
+    import
+    character(c_char),value :: a
+  end subroutine bar
+  subroutine bar2(a)
+    import
+    character(c_char) :: a
+  end subroutine bar2
+end interface
+ character(c_char) :: z
+ z = 'a'
+ call bar(z)
+ call bar2(z)
+end
Index: gcc/testsuite/gfortran.dg/c_char_driver.c
===================================================================
--- gcc/testsuite/gfortran.dg/c_char_driver.c	(revision 0)
+++ gcc/testsuite/gfortran.dg/c_char_driver.c	(revision 0)
@@ -0,0 +1,14 @@
+void param_test(char my_char, char my_char_2);
+void sub0(void);
+void sub1(char *my_char);
+
+int main(int argc, char **argv)
+{
+  char my_char = 'y';
+  
+  param_test('y', 'z');
+  sub0();
+  sub1(&my_char);
+  
+  return 0;
+}
Index: gcc/fortran/trans-decl.c
===================================================================
--- gcc/fortran/trans-decl.c	(revision 126603)
+++ gcc/fortran/trans-decl.c	(working copy)
@@ -3023,6 +3023,18 @@ generate_local_decl (gfc_symbol * sym)
 		     &sym->declared_at);
     }
 
+  if (sym->attr.dummy == 1)
+    {
+      /* Modify the tree type for character dummy arguments of bind(c)
+	 procedures if they are passed by value.  The tree type for them will
+	 be promoted to INTEGER_TYPE for the middle end, which appears to be
+	 what C would do with characters passed by-value.  */
+      if (sym->attr.value == 1 && sym->backend_decl != NULL
+	  && sym->ts.type == BT_CHARACTER && sym->ts.is_c_interop
+	  && sym->ns->proc_name != NULL && sym->ns->proc_name->attr.is_bind_c)
+	TREE_TYPE (sym->backend_decl) = unsigned_char_type_node;
+    }
+
   /* Make sure we convert the types of the derived types from iso_c_binding
      into (void *).  */
   if (sym->attr.flavor != FL_PROCEDURE && sym->attr.is_iso_c

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