stfc / fparser

This project maintains and develops a Fortran parser called fparser2 written purely in Python which supports Fortran 2003 and some Fortran 2008. A legacy parser fparser1 is also available but is not supported. The parsers were originally part of the f2py project by Pearu Peterson.
https://fparser.readthedocs.io
Other
64 stars 29 forks source link

Allocate with mold= parameter #298

Closed hiker closed 1 year ago

hiker commented 3 years ago

This is valid Fortran, but not accepted by fparser:

subroutine a(b1)
   integer, dimension(:,:), allocatable :: b1
   integer, dimension(:,:), allocatable :: a1
   allocate(a1, mold=b1)
end subroutine a

Supporting the mold argument makes it easier to create the kernel-extraction driver (since we can then allocate variables without having to analyse the number of dimensions). It's not urgent, since I have a work around.

arporter commented 2 years ago

It turns out that mold is a Fortran2008 addition. From http://fcode.cn/html/keyword/KEYWORD_ALLOCATE.htm:

In Fortran 2003, there are several additions.

The ERRMSG=variable clause has been added; this sets a character string variable to an error message if an allocation fails. (This variable is unchanged if there is no error, so STAT= is still needed to prevent termination.)
The SOURCE=expression clause has been added; this sets the value (and type if polymorphic) of the item to that of the source expression. In this case, Fortran 2003 only permits a single item to be allocated at a time.
The item-list can be preceded by a type-spec; this sets the type of a polymorphic item. 

In Fortran 2008, there are more additions.

The MOLD=expression has been added; this sets the type of a polymorphic allocation to be the same as the dynamic type of expression; the expression is permitted to be an undefined variable, since the value of the variable is not used. Any default initialisation for the type will be applied to the new object.

If MOLD= appears, neither SOURCE= nor type-spec may appear.
If MOLD= or SOURCE= appears with an expression that is an array, the bounds can be omitted from the array variable. In this case, the allocated bounds will be the same as those of the expression.
More than one item is permitted to be allocated by an ALLOCATE statement with a SOURCE= clause. 
arporter commented 2 years ago

So, it is actually the definition of the alloc-opt rule (R627) that has changed in F2008.

arporter commented 1 year ago

I'm reopening this as I've discovered that while mold on its own is OK, if we also have stat then fparser2 raises a SyntaxError (sigh):

(psyclone310) $ gfortran -c mold_test.f90 
(psyclone310) $ fparser2 --std=f2008 mold_test.f90
File: 'mold_test.f90'
Syntax error: at line 6
>>>   allocate(a1, mold=b1, stat=ierr)