Functions returning pointers

Paul Brook paul@codesourcery.com
Sun Aug 22 22:47:00 GMT 2004


> Fix below, as you suggested off-list.

You should know better than to believe what I tell you ;-)

> 2004-08-23  Tobias Schlueter  <tobias.schlueter@physik.uni-muenchen.de>
>
> 	* expr.c (gfc_check_assign): Add comment, add warning in
> 	doubtful assignment.
> 	* trans-expr.c (gfc_conv_function_call): Dereference pointer
> 	result if not expecting pointer.

> +++ trans-expr.c        22 Aug 2004 22:09:58 -0000
> @@ -1170,6 +1170,13 @@ gfc_conv_function_call (gfc_se * se, gfc
>    se->expr = build (CALL_EXPR, TREE_TYPE (fntype), se->expr,
>                     arglist, NULL_TREE);
>
> +  /* If we have a pointer function, but we don't want a pointer, e.g.
> +     something like
> +        x = f()
> +     where f is pointer valued, we have to dereference the result.  */
> +  if (sym->attr.pointer && !se->want_pointer)
> +    se->expr = gfc_build_indirect_ref (se->expr);

This condition should be
if (sym->attr.pointer && !(se->want_pointer || byref))

Incomplete testcase:
program assignment_2
  character, target :: t
  character, pointer :: p

  p = f()
contains
function f()
  character, pointer :: f
  f => t
end function
end program

It also makes sense to test the warning.

! { dg-do run }
! { dg-options -Wsurprising }
! Tests assignment (not pointer assignment) of a function returning a pointer.
program assignment_1
integer, pointer :: p
integer, target :: t, s
t = 1
p => s
! We weren't dereferencing the result of the function.
p = f() ! { dg-warning "POINTER valued function" "" }
p = p+1
if (p.ne.2) call abort()
if (p.ne.s) call abort()
contains
function f()
integer, pointer :: f
f => t
end function f
end program

Ok with those changes, assuming it still passes testing.

Paul



More information about the Fortran mailing list