swig-fortran / swig

This fork of SWIG creates Fortran wrapper code from C++ headers.
http://www.swig.org
Other
42 stars 11 forks source link

Bug in bindc when using SWIGTYPE pointers as arguments #124

Closed rcoacci closed 5 years ago

rcoacci commented 5 years ago

Suppose I have the following interface file (example.i):

%module example
%fortranbindc;

typedef struct _undefined1 undefined1; // Opaque struct
int example2_f7(undefined1* foo);

The generated interface file (example.f90) is:

! Generated stuff
function example2_f7(foo) &
bind(C, name="example2_f7") &
result(fresult)
use, intrinsic :: ISO_C_BINDING
type(C_PTR):: foo
integer(C_INT) :: fresult
end function

Notice that the dummy argument foo is declared as type(C_PTR) which is incorrect, as it should be type(C_PTR), value.

I believe the problem is in classes.swg:322 where you state:

%typemap(bindc) SWIGTYPE* "type(C_PTR)";

It should be:

%typemap(bindc, in="type(C_PTR), value") SWIGTYPE* "type(C_PTR)";

rcoacci commented 5 years ago

On a similar note, the same thing happens for fundamental pointer types (int, long, etc). It seems the problem is in fundamental.swg :

%typemap(bindc, in="$typemap(imtype, $*1_ltype)") FORTRAN_INTRINSIC_TYPE&

should be

%typemap(bindc, in="$typemap(imtype, $*1_ltype), value") FORTRAN_INTRINSIC_TYPE&

As $typemap(imtype, $*1_ltype) for FORTRAN_INTRINSIC_TYPE& is type(C_PTR)

Note that the imtype typemap in defined correctly for dummy arguments.

sethrj commented 5 years ago

I think this too is a consequence of https://github.com/swig-fortran/swig/pull/123 . I'll take a look