return false;
}
- if (!c_loc && expr->rank > 0 && expr->expr_type != EXPR_ARRAY)
+ /* Checks for C_SIZEOF need to take into account edits to 18-007r1, see
+ https://j3-fortran.org/doc/year/22/22-101r1.txt . */
+ if (!c_loc && !c_f_ptr && expr->rank > 0 && expr->expr_type == EXPR_VARIABLE)
{
gfc_array_ref *ar = gfc_find_array_ref (expr);
- if (ar->type != AR_FULL)
+ if (ar->type == AR_FULL && ar->as->type == AS_ASSUMED_SIZE)
{
- *msg = "Only whole-arrays are interoperable";
- return false;
- }
- if (!c_f_ptr && ar->as->type != AS_EXPLICIT
- && ar->as->type != AS_ASSUMED_SIZE)
- {
- *msg = "Only explicit-size and assumed-size arrays are interoperable";
+ *msg = "Assumed-size arrays are not interoperable";
return false;
}
}
return false;
}
+ if (fptr->ts.type == BT_PROCEDURE && attr.function)
+ {
+ gfc_error ("FPTR argument to C_F_POINTER at %L is a function "
+ "returning a pointer", &fptr->where);
+ return false;
+ }
+
if (fptr->rank > 0 && !is_c_interoperable (fptr, &msg, false, true))
- return gfc_notify_std (GFC_STD_F2018, "Noninteroperable array FPTR "
- "at %L to C_F_POINTER: %s", &fptr->where, msg);
+ return gfc_notify_std (GFC_STD_F2018,
+ "Noninteroperable array FPTR argument to "
+ "C_F_POINTER at %L: %s", &fptr->where, msg);
return true;
}
--- /dev/null
+! { dg-do compile }
+!
+! A function returning a pointer cannot be interoperable
+! and cannot be used as FPTR argument to C_F_POINTER.
+
+subroutine s ()
+ use, intrinsic :: iso_c_binding
+ implicit none
+ type(c_ptr) :: cPtr
+ call c_f_pointer (cPtr, p0) ! { dg-error "function returning a pointer" }
+ call c_f_pointer (cPtr, p1, shape=[2]) ! { dg-error "function returning a pointer" }
+contains
+ function p0 ()
+ integer, pointer :: p0
+ nullify (p0)
+ end
+ function p1 ()
+ integer, pointer :: p1(:)
+ nullify (p1)
+ end
+ function fp0 ()
+ integer, pointer :: fp0
+ call c_f_pointer (cPtr, fp0) ! valid here
+ end
+ function fp1 ()
+ integer, pointer :: fp1(:)
+ call c_f_pointer (cPtr, fp1, shape=[2]) ! valid here
+ end
+ function ffp0 () result (fp0)
+ integer, pointer :: fp0
+ call c_f_pointer (cPtr, fp0) ! valid here
+ end
+ function ffp1 () result (fp1)
+ integer, pointer :: fp1(:)
+ call c_f_pointer (cPtr, fp1, shape=[2]) ! valid here
+ end
+end
character(kind=c_char,len=1),parameter :: str2(4) = ["a","b","c","d"]
- i = c_sizeof(str2(1:3)) ! { dg-error "must be an interoperable data" }
+ i = c_sizeof(str2(1:3))
if (i /= 3) STOP 1
--- /dev/null
+! { dg-do compile }
+! PR fortran/106500 - fix checking of arguments to C_SIZEOF
+!
+! Check support of the following EDIT to 18-007r1:
+! https://j3-fortran.org/doc/year/22/22-101r1.txt
+
+subroutine foo (n, x, y, z, w, u)
+ use, intrinsic :: iso_c_binding
+ implicit none
+ integer, intent(in) :: n
+ real :: x(n)
+ real :: y(:)
+ real :: z(2,*)
+ real :: w(..)
+ real, allocatable :: a(:)
+ real, pointer :: b(:)
+ type t
+ real, allocatable :: a(:)
+ end type t
+ type(t) :: u
+
+ print *, c_sizeof (x)
+ print *, c_sizeof (x(::2))
+ print *, c_sizeof (x+1)
+ print *, c_sizeof (y)
+ print *, c_sizeof (y(1:2))
+ print *, c_sizeof (z(:,1:2))
+ print *, c_sizeof (w)
+ print *, c_sizeof (1._c_float)
+ !
+ allocate (a(n))
+ allocate (b(n))
+ if (.not. allocated (u%a)) allocate (u%a(n))
+ print *, c_sizeof (a)
+ print *, c_sizeof (b)
+ !
+ print *, c_sizeof (u%a)
+ print *, c_sizeof (u%a(1:2))
+ !
+ print *, c_sizeof (z) ! { dg-error "Assumed-size arrays are not interoperable" }
+ print *, c_sizeof (u) ! { dg-error "Expression is a noninteroperable derived type" }
+end
ii = storage_size (x) ! { dg-error "Assumed-type argument at .1. is not permitted as actual argument to the intrinsic storage_size" }
ii = sizeof (y) ! { dg-error "shall not be an assumed-size array" }
- ii = c_sizeof (y) ! { dg-error "shall not be an assumed-size array" }
+ ii = c_sizeof (y) ! { dg-error "\[Aa\]ssumed-size array" }
ii = storage_size (y) ! okay, element-size is known
ii = sizeof (proc) ! { dg-error "shall not be a procedure" }