In Fortran, I need a procedure pointer inside a derived type that can point to one of several routines. This problem seems common for SO:
Fortran save procedure as a property of a derived type
Overloading Related Types in Fortran 2003
There is no corresponding subroutine for a related generic subroutine call of this type
Generic String Binding Procedures with Procedure Arguments
Type to bind procedure as arguments
to name a few. The answer to this question for functions is provided very well in the first link.
However, I still do not understand the methodology for developing such code in the event that a type-bound procedure pointer points to a subroutine. The difficulty is that there is no type associated with the returned one (since nothing is returned ")".
I would also like to point out a nuance that, although a simple solution may exist in the newer fortran standard (2003,2008), this solution may not work on all compilers, which may be problematic in the future. Therefore, I am interested in compilers.
I have a small code (shown below) that currently works, but in my large code I get an internal compiler error (also shown below) in a file where I use method pointers in derived types. My question is: what can I do with the code below
1) Strictly use explicit interfaces
2) Maximize the information passed to the compiler
3) Make sure that the code is ported between as many compilers as possible (i.e. uses fortran 90/95).
To what extent can this be accomplished (1 most important)? Is it possible to satisfy all these criteria above? I know that “satisfying all these criteria” is subjective, but I would say that the answer is “yes” for the same question regarding functions instead of routines.
gcc version 5.1.0 (i686-posix-dwarf-rev0, Built by MinGW-W64 project)
Small code:
module subs_mod implicit none public :: add,mult contains subroutine add(x,y,z) implicit none integer,intent(inout) :: x integer,intent(in) :: y,z x = y+z end subroutine subroutine mult(x,y,z) implicit none integer,intent(inout) :: x integer,intent(in) :: y,z x = y*z end subroutine end module module type_A_mod use subs_mod implicit none public :: type_A,init,operate type type_A procedure(),pointer,nopass :: op end type contains subroutine init(A,op) implicit none external :: op type(type_A),intent(inout) :: A A%op => op end subroutine subroutine operate(A,x,y,z) implicit none type(type_A),intent(in) :: A integer,intent(inout) :: x integer,intent(in) :: y,z call A%op(x,y,z) end subroutine end module program test use type_A_mod use subs_mod implicit none type(type_A) :: A integer :: x call init(A,mult) call operate(A,x,3,5) write(*,*) 'x = ',x end program
Compiler error in large code:
f951.exe: internal compiler error: Segmentation fault libbacktrace could not find executable to open Please submit a full bug report, with preprocessed source if appropriate. See <http://sourceforge.net/projects/mingw-w64> for instructions.
UPDATE
Here's a small modification that gives the compiler more information, but I have not tried this in large code. However, this seems arbitrary, and I have no idea whether this will help or not.
... function add(x,y,z) result(TF) ... logical :: TF x = y+z TF = .true. end function function mult(x,y,z) result(TF) ... logical :: TF x = y*z TF = .true. end function end module module type_A_mod ... type type_A procedure(logical),pointer,nopass :: op end type ... subroutine init(A,op) implicit none logical,external :: op ... end subroutine subroutine operate(A,x,y,z) ... logical :: TF TF = A%op(x,y,z) end subroutine end module program test ... end program
SOLUTIONS COMMENTS Just to comment on a solution (provided by @IanH): there was one more wrinkle, and it was that I had some derived types included in the abstract interface, which according to the New Fortran 2003 functions , the Import statement should be included to tell an abstract interface about any incoming derived types. Here is a small working example that applies to large code, reduces the internal compiler error I had :)
module DT_mod implicit none private public :: DT type DT integer :: i end type contains end module module subs_mod use DT_mod implicit none private public :: add,mult,op_int abstract interface subroutine op_int(d,x,y,z) import :: DT implicit none type(DT),intent(inout) :: d integer,intent(inout) :: x integer,intent(in) :: y,z end subroutine end interface contains subroutine add(d,x,y,z) implicit none type(DT),intent(inout) :: d integer,intent(inout) :: x integer,intent(in) :: y,z x = y+z d%i = 1 end subroutine subroutine mult(d,x,y,z) implicit none type(DT),intent(inout) :: d integer,intent(inout) :: x integer,intent(in) :: y,z x = y*z d%i = 2 end subroutine end module module type_A_mod use DT_mod use subs_mod implicit none private public :: type_A,init,operate type type_A procedure(op_int),pointer,nopass :: op end type contains subroutine init(A,op) implicit none procedure(op_int) :: op type(type_A),intent(inout) :: A A%op => op end subroutine subroutine operate(A,d,x,y,z) implicit none type(DT),intent(inout) :: d type(type_A),intent(in) :: A integer,intent(inout) :: x integer,intent(in) :: y,z call A%op(d,x,y,z) end subroutine end module program test use type_A_mod use subs_mod use DT_mod implicit none type(type_A) :: A type(DT) :: d integer :: x,y,z y = 3; z = 5 call init(A,mult) call operate(A,d,x,y,z) write(*,*) 'x,y,x = ',y,z,x write(*,*) 'd%i = ',d%i end program
Any help is greatly appreciated.