From: Janus Weil on
Hi all,

I am currently trying to implement the MOLD tag of the ALLOCATE
statement (introduced in F08) into gfortran, and I have a bit of
trouble interpreting the fine print of the standard.

Please consider the following example code:

type :: t
integer :: i
end type
class(t),allocatable :: x
type(t) :: y,z
ALLOCATE(x,SOURCE=y,MOLD=z)
end

All I want to know is: Is this valid or not? In particular, is it
allowed to use ALLOCATE with both SOURCE and MOLD at the same time?

In F08 one finds:

R626 allocate-stmt is ALLOCATE ( [ type-spec :: ] allocation-list [,
alloc-opt-list ] )

R627 alloc-opt
is ERRMSG = errmsg-variable
or MOLD = source-expr
or SOURCE = source-expr
or STAT = stat-variable

Note that both MOLD and SOURCE refer to a "source-expr". Now the
critical part is probably this:

C637 (R626) At most one of source-expr and type-spec shall appear.

This certainly forbids:
1) type-spec together with SOURCE
2) type-spec together with MOLD

But does it forbid SOURCE together with MOLD?


Cheers,
Janus
From: Tobias Burnus on
Janus Weil wrote:
> ALLOCATE(x,SOURCE=y,MOLD=z)
>
> All I want to know is: Is this valid or not? In particular, is it
> allowed to use ALLOCATE with both SOURCE and MOLD at the same time?
>
> or MOLD = source-expr
> or SOURCE = source-expr
>
> Note that both MOLD and SOURCE refer to a "source-expr". Now the
> critical part is probably this:
>
> C637 (R626) At most one of source-expr and type-spec shall appear.

I don't want to stop anyone from replying as seemingly my reading does
not completely convince Steve and Janus - but here it is.

* * *

a) I read C637 as "At most one of (source-expr and type-spec)", i.e.
there is only either a type-spec or a SOURCE or a MOLD - or none of them.

> This certainly forbids:
> 1) type-spec together with SOURCE
> 2) type-spec together with MOLD

Why? If you have a different reading, e.g. "((at most one of
source-expr) and (at most one of type-spec)) shall appear."
then there is not only redundancy (there can only be one type-spec and
R626 makes clear that it is optional), but also it will allow for having
both a type-spec and a (SOURCE or MOLD) in some cases. C629 does not
apply if the allocate-object is non polymorphic:

"C629 (R626) If any allocate-object has a deferred type parameter, is
unlimited polymorphic, or is of abstract type, either type-spec or
source-expr shall appear."

b) Assuming that both SOURCE and MOLD are allowed. What is supposed to
happen in case of a polymorphic source-expr? If the dynamic type of
SOURCE is used, MOLD has no purpose at all. If the dynamic type of MOLD
is used, the value of the allocate-object still needs to be that of
SOURCE (cf. below). I do not see how this can be fulfilled - unless
SOURCE and MOLD have the same dynamic type - which again makes MOLD useless.

Thus, if my reading is wrong, MOLD would be useless, there were
redundancy in the constraints, and a type-spec, SOURCE and MOLD could
appear all in the same ALLOCATE statement, but their (dynamic) type had
to be the same. While this is indeed possible, it seems to be highly
unlikely that this is the indent of the standard.

Tobias

Quote for (b):
"If SOURCE= appears, source-expr shall be conformable with allocation.
[...] On successful allocation, if allocate-object and source-expr have
the same rank the value of allocate-object becomes that of source-expr,
otherwise the value of each element of allocate-object becomes that of
source-expr."
From: Janus Weil on

> a) I read C637 as "At most one of (source-expr and type-spec)", i.e.
> there is only either a type-spec or a SOURCE or a MOLD - or none of them.
>
> > This certainly forbids:
> > 1) type-spec together with SOURCE
> > 2) type-spec together with MOLD
>
> Why? If you have a different reading, e.g. "((at most one of
> source-expr) and (at most one of type-spec)) shall appear."

This is absolutely not what I was suggesting. C637 says:

"At most one of source-expr and type-spec shall appear."

I am reading this as "source-expr and type-spec shall not appear is
one ALLOCATE statement". Which means that two source-expr's could
appear together (but only via "SOURCE=...,MOLD=...", not via
"SOURCE=...,SOURCE=..." which is explicitly forbidden by C636).



> b) Assuming that both SOURCE and MOLD are allowed. What is supposed to
> happen in case of a polymorphic source-expr? If the dynamic type of
> SOURCE is used, MOLD has no purpose at all. If the dynamic type of MOLD
> is used, the value of the allocate-object still needs to be that of
> SOURCE (cf. below). I do not see how this can be fulfilled - unless
> SOURCE and MOLD have the same dynamic type - which again makes MOLD useless.

Not true. The dynamic types do not have to be the same. The type of
MOLD could be an extension of the type of SOURCE.

Cheers,
Janus