This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Patch, fortran] PR27613 - Recursive functions


:ADDPATCH fortran:

This patch fixes PR27613 in which directly recursive, scalar functions were generating an "unclassifiable statement" error for the recursive statement(s). What I mean by "direct" here is that there is no RESULT in the function statement. The fix is effected in gfc_match_rvalue by detecting the left parenthesis for the function argument, generating an error if the symbol represents an array and generates the recursive function reference, otherwise.

Regtested on Cygwin_NT/PIV.

OK for trunk and 4.1, when it reopens?

Paul

2006-05-21 Paul Thomas <pault@gcc.gnu.org>

   PR fortran/27613
   * primary.c (gfc_match_rvalue): Test if symbol represents a
   direct recursive function reference.  Error if array valued,
   go to function0 otherwise.

2006-05-21 Paul Thomas <pault@gcc.gnu.org>

   PR fortran/27613
   * gfortran.dg/recursive_reference_1.f90: New test.


Index: gcc/fortran/primary.c
===================================================================
--- gcc/fortran/primary.c	(révision 113722)
+++ gcc/fortran/primary.c	(copie de travail)
@@ -1933,6 +1933,21 @@
 
   if (sym->attr.function && sym->result == sym)
     {
+      /* See if this is a directly recursive function call.  */
+      gfc_gobble_whitespace ();
+      if (sym->attr.recursive
+	    && gfc_peek_char () == '('
+	    && gfc_current_ns->proc_name == sym)
+	{
+	  if (!sym->attr.dimension)
+	    goto function0;
+
+	  gfc_error ("'%s' is array valued and directly recursive "
+		     "at %C , so the keyword RESULT must be specified "
+		     "in the FUNCTION statement", sym->name);
+	  return MATCH_ERROR;
+	}
+	
       if (gfc_current_ns->proc_name == sym
 	  || (gfc_current_ns->parent != NULL
 	      && gfc_current_ns->parent->proc_name == sym))
! { dg-do compile }
! Tests the patch for PR27613, in which directly recursive, scalar
! functions were generating an "unclassifiable statement" error
! for the recursive statement(s).
!
! Based on PR testcase by Nicolas Bock  <nicolasbock@gmail.com>
!
program test
  if (original_stuff(1) .ne. 5) call abort ()
  if (scalar_stuff(-4) .ne. 10) call abort ()
  if (any (array_stuff((/-19,-30/)) .ne. (/25,25/))) call abort ()
contains
  recursive function original_stuff(n)
    integer :: original_stuff
    integer :: n
    original_stuff = 1
    if(n < 5) then
      original_stuff = original_stuff + original_stuff (n+1)
    endif
  end function original_stuff

  recursive function scalar_stuff(n) result (tmp)
    integer :: tmp
    integer :: n
    tmp = 1
    if(n < 5) then
      tmp = tmp + scalar_stuff (n+1)
    endif
  end function scalar_stuff

  recursive function array_stuff(n) result (tmp)
    integer :: tmp (2)
    integer :: n (2)
    tmp = 1
    if(maxval (n) < 5) then
      tmp = tmp + array_stuff (n+1)
    endif
  end function array_stuff

  recursive function bad_stuff(n)
    integer :: bad_stuff (2)
    integer :: n(2)
    bad_stuff = 1
    if(maxval (n) < 5) then
      bad_stuff = bad_stuff + bad_stuff (n+1) ! { dg-error "RESULT must be specified" }
    endif
  end function bad_stuff
end program test
2006-05-21  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/27613
	* primary.c (gfc_match_rvalue): Test if symbol represents a
	direct recursive function reference.  Error if array valued,
	go to function0 otherwise.

2006-05-21  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/27613
	* gfortran.dg/recursive_reference_1.f90: New test.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]