This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran 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]

Re: [patch, fortran] fixes for functions


On Tuesday 10 July 2007 14:28:23 Tobias Burnus wrote:
> > 2007-07-09  Daniel Franke  <franke.daniel@gmail.com>
> >
> >	* decl.c (gfc_match_suffix): Removed surplus general error that hides
> >	a more specific message.
> >	* resolve.c (resolve_fl_variable): Reject illegal initializiers only
> >	if not already done.
> >	(resolve_fl_procedure): Added check for initializers of functions.
> >
> > Regression tested on i686-pc-linux-gnu. Ok for trunk?
>
> This patch fails for me when used with statement functions:
>
>       Z2(x) = x*(Alft-x)/Vg - x*(x-Alfb)/Vf
>        1
> Error: Function 'z2' at (1) cannot have an inital value
>
> Actually, I also get tons of errors from the test suite for the same
> reason.

Tobias, 

thanks again for catching this! The solution for statement functions turned 
out to be straightforward.

I also changed the error message according to Brooks' suggestion. It now 
reads: "cannot have an initializer" instead of "cannot have an initial 
value".


gcc/fortran:
2007-07-09  Daniel Franke  <franke.daniel@gmail.com>

	PR fortran/31639
        * decl.c (gfc_match_suffix): Removed surplus general error that hides 
        a more specific message.
        * resolve.c (resolve_fl_variable): Reject illegal initializiers only
	if not already done.
        (resolve_fl_procedure): Added check for initializers of functions.

gcc/testsuite:
2007-07-09  Daniel Franke  <franke.daniel@gmail.com>

	PR fortran/31639
        * gfortran.dg/func_decl_4.f90: New test.


Attached patch has been bootstrapped and regtested on i686-pc-linux-gnu. 
Ok for trunk?

	Daniel

Index: fortran/resolve.c
===================================================================
--- fortran/resolve.c	(revision 126598)
+++ fortran/resolve.c	(working copy)
@@ -6547,7 +6547,7 @@
   }
 
   /* Reject illegal initializers.  */
-  if (sym->value && flag)
+  if (!sym->mark && sym->value && flag)
     {
       if (sym->attr.allocatable)
 	gfc_error ("Allocatable '%s' at %L cannot have an initializer",
@@ -6745,6 +6745,14 @@
 	}
     }
 
+  /* A function, that is not a statement-function, shall not have an initializer.  */
+  if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION)
+    {
+      gfc_error ("Function '%s' at %L cannot have an initializer",
+		 sym->name, &sym->declared_at);
+      return FAILURE;
+    }
+
   /* An external symbol may not have an initializer because it is taken to be
      a procedure.  */
   if (sym->attr.external && sym->value)
Index: fortran/decl.c
===================================================================
--- fortran/decl.c	(revision 126598)
+++ fortran/decl.c	(working copy)
@@ -3578,12 +3578,6 @@
       break;
     }
 
-  if (is_result == MATCH_ERROR || is_bind_c == MATCH_ERROR)
-    {
-      gfc_error ("Error in function suffix at %C");
-      return MATCH_ERROR;
-    }
-
   if (is_bind_c == MATCH_YES)
     if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
         == FAILURE)
Index: testsuite/gfortran.dg/func_decl_4.f90
===================================================================
--- testsuite/gfortran.dg/func_decl_4.f90	(revision 0)
+++ testsuite/gfortran.dg/func_decl_4.f90	(revision 0)
@@ -0,0 +1,17 @@
+! { dg-do compile }
+! { dg-options "-c" }
+!
+! Functions shall not have an initializer.
+!
+
+function f1()                      ! { dg-error "cannot have an initializer" }
+  integer :: f1 = 42
+end function
+
+function f2() RESULT (r)           ! { dg-error "cannot have an initializer" }
+  integer :: r = 42
+end function
+
+function f3() RESULT (f3)          ! { dg-error "must be different than function name" }
+  integer :: f3 = 42
+end function                       ! { dg-excess-errors "" }

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