A little pointer on POINTER in data would be helpful

Steve Kargl sgk@troutmask.apl.washington.edu
Sat Mar 24 20:51:00 GMT 2018


On Sat, Mar 24, 2018 at 01:08:58PM -0700, Tim Zeisloft wrote:
> > program p
> >     integer, target, save :: m = 3
> >     type t
> >        integer, pointer :: n
> >     end type
> >     type(t) :: z
> >     data z%n / m /
> >     m = 3
> >     print *, z%n
> > end
> >
> > % gfcx -o z b.f90
> > b.f90:7:15:
> >
> >      data z%n / m /
> >                 1
> > Error: Symbol 'm' must be a PARAMETER in DATA statement at (1)
> >
> > Whoops.
> 
> The rule R443 appears in the context of default initialization, but we 
> are explicitly initializing because of the data statement, so R443 
> doesn't seem to apply.
> If one looks at the definition of what can appear in a 
> data-stmt-value-list (5.4.7.6), everything must reduce to a constant 
> expression, which confirms that the compiler is correctly diagnosing the 
> situation.

I think the Fortran 2018 standard may disagree with you.

R837 data-stmt         is DATA data-stmt-set [ [ , ] data-stmt-set ] ...
R838 data-stmt-set     is data-stmt-object-list / data-stmt-value-list /
R839 data-stmt-object  is variable
                       or data-implied-do

C874 (R839) A data-stmt-object that is a variable shall be a designator.

C876 (R839) A data-i-do-object or a variable that appears as a
     data-stmt-object shall not be an object designator in which a pointer
     appears other than as the entire rightmost part-ref.

R843 data-stmt-value  is [ data-stmt-repeat * ] data-stmt-constant

R845 data-stmt-constant  is scalar-constant
                         or scalar-constant-subobject
                         or signed-int-literal-constant
                         or signed-real-literal-constant
                         or null-init
                         or initial-data-target      <-- Here's the problem
                         or structure-constructor

R744 initial-data-target  is designator

R901 designator   is object-name
R804 object-name  is name
R603 name         is letter [ alphanumeric-character ] ...

A data-stmt-constant shall be null-init or initial-data-target if and
only if the corresponding data-stmt-object has the POINTER attribute.
If data-stmt-constant is null-init, the initial association status of
the corresponding data statement object is disassociated.  If
data-stmt-constant is initial-data-target the corresponding data statement
object shall be data-pointer-initialization compatible (7.5.4.6) with the
initial data target; the data statement object is initially associated
with the target.

So, I believe the standard says the following code is conforming

subroutine sub(i)
   integer, intent(in) :: i
   integer, save, target :: j
   integer, pointer :: k
   data k/j/
   if (i /= 0) then
      j = i
   else
      j = 42
   end if
   print *, k
end subroutine

An equivalent subroutine would be

subroutine sub(i)
   integer, intent(in) :: i
   integer, save, target :: j
   integer, pointer :: k => j
   if (i /= 0) then
      j = i
   else
      j = 42
   end if
   print *, k
end subroutine

Of course, I could be wrong.

-- 
Steve



More information about the Fortran mailing list