Bug 32580

Summary: iso_c_binding c_f_procpointer / procedure pointers
Product: gcc Reporter: Joost VandeVondele <Joost.VandeVondele>
Component: fortranAssignee: janus
Status: RESOLVED FIXED    
Severity: normal CC: burnus, gcc-bugs, jaydub66, nicolasbock
Priority: P3    
Version: 4.3.0   
Target Milestone: ---   
See Also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=42072
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36325
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36322
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2008-07-02 20:19:25
Bug Depends on: 32578, 32579    
Bug Blocks: 32630, 29975, 36592    

Description Joost VandeVondele 2007-07-02 07:20:40 UTC
The following program doesn't compile & link:

---- test_f90.f90 ----
MODULE X

  USE ISO_C_BINDING
  INTERFACE
    REAL(KIND=C_FLOAT) FUNCTION mytype( a ) BIND(C)
       USE ISO_C_BINDING
       INTEGER(KIND=C_INT), VALUE :: a
    END FUNCTION
    SUBROUTINE init() BIND(C,name="init")
    END SUBROUTINE
  END INTERFACE

  TYPE(C_FUNPTR), BIND(C,name="funpointer") :: funpointer

END MODULE X

USE X
PROCEDURE(mytype), POINTER :: ptype

CALL init()
CALL C_F_PROCPOINTER(funpointer,ptype)
write(6,*) ptype(3)

END


---- test_c.c ----
float (*funpointer)(int);

float f(int t)
{
  return t*3;
}

void init()
{
 funpointer=f;
}


test as:

> gcc -c test_c.c
> g95 test_f90.f90 test_c.o
> ./a.out
 9.
Comment 1 Joost VandeVondele 2007-07-02 07:37:05 UTC
Fixing this bug would likely allow to compile/link the full CP2K (including the bit that depends on the ISO_C_BINDING)
Comment 2 kargls 2007-07-02 14:57:40 UTC
Resolution of this bug requires the PROCEDURE POINTER feature
from Fortran 2003.  Janus Weil, a Google SoC participant, is 
working on this feature.
Comment 3 Tobias Burnus 2007-07-04 18:29:46 UTC
Clean up from my Bind(C) notes: The following should give an error such as:
Error: 'fptr' argument of 'c_f_procpointer' intrinsic at (1) must be a PROCEDURE POINTER


  use iso_c_binding
  type(c_funptr) :: cfunptr
  real, pointer :: myFunc
  call c_f_procpointer(cfunptr, myFunc)
end
Comment 4 Joost VandeVondele 2007-10-16 04:32:10 UTC
(In reply to comment #2)
> Resolution of this bug requires the PROCEDURE POINTER feature
> from Fortran 2003.  Janus Weil, a Google SoC participant, is 
> working on this feature.
> 

SoC is over, I assume this has been put on ice ?


Comment 5 Tobias Burnus 2007-10-16 06:46:22 UTC
(In reply to comment #4)
> SoC is over, I assume this has been put on ice ?

Yes, there was unfortunately no patch before GCC entered stage 3 (12 September) and in stage 3 merging new features is allowed. (Besides, there is not yet a ready patch for procedure pointers.)
In any case, before GCC 4.4 enters stage 1, at least the core developers will concentrate on fixing bugs.
Comment 6 Joost VandeVondele 2007-10-16 08:30:47 UTC
(In reply to comment #5)
> (In reply to comment #4)
> > SoC is over, I assume this has been put on ice ?
> 
> Yes, there was unfortunately no patch before GCC entered stage 3 (12 September)
> and in stage 3 merging new features is allowed. (Besides, there is not yet a
> ready patch for procedure pointers.)
> In any case, before GCC 4.4 enters stage 1, at least the core developers will
> concentrate on fixing bugs.
> 

Since adding c_f_procpointer might change the fortran runtime library, will this addition be OK for 4.4 ?
Comment 7 Tobias Burnus 2007-10-16 11:26:21 UTC
> Since adding c_f_procpointer might change the fortran runtime library, will
> this addition be OK for 4.4 ?
Additions of functions is no problem; also modifying functions arguments/functionality is no problem. However, obsolete (i.e. unused) functions remain in the library (for old programs).

If the function interface is changed, the old function remains in the library (with the old version number) and the new version of the function is added.
If symbol versioning is supported (e.g. under Linux), old programs continue to work flawlessly with new libraries.

Thus: Library interface changes before 4.3.0 is released are nice, because one will not need to carry obsolete functions along; otherwise the development is not hampered at all. For procedure pointers, I expect that one doesn't need to touch the library at at all (but I might be wrong).
Comment 8 Janus Weil 2008-05-24 17:52:13 UTC
I have a patch which can handle this test case, see
http://gcc.gnu.org/ml/fortran/2008-05/msg00296.html

It's not complete yet, and some details need to be fixed, but the basic functionality is there. I hope it can be committed to trunk quite soon.

Cheers,
Janus
Comment 9 Joost VandeVondele 2008-05-25 12:13:26 UTC
> It's not complete yet, and some details need to be fixed, but the basic
> functionality is there. I hope it can be committed to trunk quite soon.

that would be great... I really hope this will be enough to enable the -D__LIBINT bits of CP2K. 
Comment 10 Tobias Burnus 2008-05-29 11:29:06 UTC
Move comment from PR 36325, see also
http://groups.google.com/group/comp.lang.fortran/browse_thread/thread/bb371413b5cbe3d7

The following is regarded as valid and did not work with one of the last versions of proc pointer patches:

 abstract interface
   subroutine a
   end subroutine
 end interface
 procedure(a),pointer :: x
 procedure(x) :: y
Comment 11 Tobias Burnus 2008-06-02 15:32:21 UTC
Another link dump:
http://groups.google.com/group/comp.lang.fortran/browse_thread/thread/ff7ae6c7a7860bca/60213205751117d4
Some of the test cases should be checked when the proc pointer patch is ready to ensure all are passed. (Forward carrying this link from PR 36322 which is about to be fixed.)
Comment 12 janus 2008-07-02 19:54:31 UTC
Subject: Bug 32580

Author: janus
Date: Wed Jul  2 19:53:37 2008
New Revision: 137386

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=137386
Log:
2008-07-02  Janus Weil  <janus@gcc.gnu.org>
	    Tobias Burnus  <burnus@net-b.de>
	    Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/32580
	* gfortran.h (struct gfc_symbol): New member "proc_pointer".
	* check.c (gfc_check_associated,gfc_check_null): Implement
	procedure pointers.
	* decl.c (match_procedure_decl): Ditto.
	* expr.c (gfc_check_pointer_assign,gfc_check_assign_symbol): Ditto.
	* interface.c (compare_actual_formal): Ditto.
	* match.h: Ditto.
	* match.c (gfc_match_pointer_assignment): Ditto.
	* parse.c (parse_interface): Ditto.
	* primary.c (gfc_match_rvalue,match_variable): Ditto.
	* resolve.c (resolve_fl_procedure): Ditto.
	* symbol.c (check_conflict,gfc_add_external,gfc_add_pointer,
	gfc_copy_attr,gen_fptr_param,build_formal_args): Ditto.
	* trans-decl.c (get_proc_pointer_decl,gfc_get_extern_function_decl,
	create_function_arglist): Ditto.
	* trans-expr.c (gfc_conv_variable,gfc_conv_function_val,
	gfc_conv_function_call,gfc_trans_pointer_assignment): Ditto.


2008-07-02  Janus Weil  <janus@gcc.gnu.org>
	    Tobias Burnus  <burnus@net-b.de>

	PR fortran/32580
	* gfortran.dg/c_f_pointer_tests_3.f90: Updated.
	* gfortran.dg/proc_decl_1.f90: Updated.
	* gfortran.dg/proc_ptr_1.f90: New.
	* gfortran.dg/proc_ptr_2.f90: New.
	* gfortran.dg/proc_ptr_3.f90: New.
	* gfortran.dg/proc_ptr_4.f90: New.
	* gfortran.dg/proc_ptr_5.f90: New.
	* gfortran.dg/proc_ptr_6.f90: New.
	* gfortran.dg/proc_ptr_7.f90: New.
	* gfortran.dg/proc_ptr_8.f90: New.

Added:
    trunk/gcc/testsuite/gfortran.dg/proc_ptr_1.f90   (with props)
    trunk/gcc/testsuite/gfortran.dg/proc_ptr_2.f90   (with props)
    trunk/gcc/testsuite/gfortran.dg/proc_ptr_3.f90   (with props)
    trunk/gcc/testsuite/gfortran.dg/proc_ptr_4.f90   (with props)
    trunk/gcc/testsuite/gfortran.dg/proc_ptr_5.f90   (with props)
    trunk/gcc/testsuite/gfortran.dg/proc_ptr_6.f90   (with props)
    trunk/gcc/testsuite/gfortran.dg/proc_ptr_7.c
    trunk/gcc/testsuite/gfortran.dg/proc_ptr_7.f90
    trunk/gcc/testsuite/gfortran.dg/proc_ptr_8.c
    trunk/gcc/testsuite/gfortran.dg/proc_ptr_8.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/check.c
    trunk/gcc/fortran/decl.c
    trunk/gcc/fortran/expr.c
    trunk/gcc/fortran/gfortran.h
    trunk/gcc/fortran/interface.c
    trunk/gcc/fortran/match.c
    trunk/gcc/fortran/match.h
    trunk/gcc/fortran/parse.c
    trunk/gcc/fortran/primary.c
    trunk/gcc/fortran/resolve.c
    trunk/gcc/fortran/symbol.c
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/c_f_pointer_tests_3.f90
    trunk/gcc/testsuite/gfortran.dg/proc_decl_1.f90

Propchange: trunk/gcc/testsuite/gfortran.dg/proc_ptr_1.f90
            ('svn:executable' added)

Propchange: trunk/gcc/testsuite/gfortran.dg/proc_ptr_2.f90
            ('svn:executable' added)

Propchange: trunk/gcc/testsuite/gfortran.dg/proc_ptr_3.f90
            ('svn:executable' added)

Propchange: trunk/gcc/testsuite/gfortran.dg/proc_ptr_4.f90
            ('svn:executable' added)

Propchange: trunk/gcc/testsuite/gfortran.dg/proc_ptr_5.f90
            ('svn:executable' added)

Propchange: trunk/gcc/testsuite/gfortran.dg/proc_ptr_6.f90
            ('svn:executable' added)


Comment 13 janus 2008-07-02 20:19:25 UTC
Fixed with rev 137386. Btw I have also tried compiling the whole CP2K, which seems to work fine.
Comment 14 janus 2008-07-02 20:19:59 UTC
Closing.
Comment 15 Joost VandeVondele 2008-07-03 13:43:27 UTC
(In reply to comment #13)
> Fixed with rev 137386. Btw I have also tried compiling the whole CP2K, which
> seems to work fine.
> 
I've also checked that gfortran now appears to compile correctly the procedure pointer part of CP2K. That's great... many thanks.