.. contents:: :local:
After the Fortran input file(s) has been parsed into a syntax tree, the compiler must check that the program checks semantically. Target labels must be checked and violations of legal semantics should be reported to the user.
This is the detailed design document on how these labels will be semantically checked. Legal semantics may result in rewrite operations on the syntax tree. Semantics violations will be reported as errors to the user.
Enforcement of the general label constraints. There are three sorts of label usage. Labels can serve
FORMATstatements) for data transfer statements (I/O constructs)
Labels are related to the standard definition of inclusive scope. For example, control-flow arcs are not allowed to originate from one inclusive scope and target statements outside of that inclusive scope.
Inclusive scope is defined as a tree structure of nested scoping constructs. A statement, s, is said to be in the same inclusive scope as another statement, t, if and only if s and t are in the same scope or t is in one of the enclosing scopes of s, otherwise s is not in the same inclusive scope as t. (Inclusive scope is unidirectional and is always from innermost scopes to outermost scopes.)
A label-do-stmt is a control construct that results in the iterative execution of a number of statements. A label-do-stmt has a (possibly shared, nonblock-do-construct) label that will be called the loop target label. The statements to be executed will be the range from the label-do-stmt to the statement identified by the loop target label, inclusive. This range of statements will be called the loop's body and logically forms a do-block.
A label-do-stmt is quite similar to a block-do-construct in semantics, but the parse tree is different in that the parser does not impose a do-block structure on the loop body.
In F18, the nonblock
DO construct has been removed. For legacy support (through F08), we will need to handle nonblock
DO constructs. In F18, the following legacy code is an error.
DO 100 I = 1, 100 DO 100 J = 1, 100 ... 100 CONTINUE
DO, error of malformed
GOTOs from the loop body to “extended ranges” and back again (PGI & gfortan appear to allow, NAG & Intel do not.)
DO CONCURRENTor loops annotated with OpenACC or OpenMP directives.
GOTOs into the
DOs inclusive scope, error/warn of invalid transfer of control
END DOor a
Semantics for F08 and earlier that support sharing the loop terminating statement in a nonblock-do-construct between multiple loops
DOloop term position
DO loop is a
DO CONCURRENT construct, there are additional constraints (18.104.22.168).
IEEE_GET_HALTING_MODEcannot appear in the body of a
ADVANCE=specifier by an I/O statement in the body of a
DO CONCURRENTis not allowed (22.214.171.124, para 5)
GOTO statement is a simple, direct transfer of control from the
GOTO to the labelled statement.
GOTO statement is analogous to a
switch statement in C++.
GOTO ( label-list ) [,] scalar-int-expr
This control-flow construct is deleted in F18.
IF (scalar-numeric-expr) label1,label2,label3
The arithmetic if statement is like a three-way branch operator. If the scalar numeric expression is less than zero goto label-1, else if the variable is equal to zero goto label-2, else if the variable is greater than zero goto label-3.
These are a Fortran control-flow construct for combining a return from a subroutine with a branch to a labelled statement in the calling routine all in one operation. A typical implementation is for the subroutine to return a hidden integer, which is used as a key in the calling code to then, possibly, branch to a labelled statement in inclusive scope.
The labels are passed by the calling routine. We want to check those labels at the call-site, that is instances of alt-return-spec.
These specifiers can appear in I/O statements and can transfer control to specific labelled statements under exceptional conditions like end-of-file, end-of-record, and other error conditions. (The PGI compiler adds code to test the results from the runtime routines to determine if these branches should take place.)
Deleted feature since Fortran 95.
The assigned-goto-stmt and assign-stmt were action-stmt in the Fortran 90 standard. They are included here for completeness. This pair of obsolete statements can (will) be enabled as part of the compiler's legacy Fortran support.
The assign-stmt stores a label in an integer variable. The assigned-goto-stmt will then transfer control to the label stored in that integer variable.
ASSIGN 10 TO i ... GOTO i (10,20,30)
iin the example above) must be named and of type default integer
Some interpretation is needed as the terms of the older standard are different.
A “scoping unit” is defined as
This is a more lax definition of scope than inclusive scope.
A named variable distinguishes a variable such as,
i, from an element of an array,
a(i), for example.
In data transfer (I/O) statements (e.g.,
READ), the user can specify a
FMT= specifier that can take a label as its argument. (R1215)
FMT=specifier has a label as its argument (C1230)
FORMAT, error statement must be a
FORMATstatement must be in the same inclusive scope as the originating data transfer statement (also in 2008)
FORMATstatements may appear in a host procedure
FORMATstatement may be ignored, allowing a
FORMATto be referenced from any scope in the compilation.
Various Fortran constructs can have names. These include
CHANGE TEAMconstruct (11.1.5)
SELECT CASEconstruct (11.1.9)
SELECT RANKconstruct (11.1.10)
SELECT TYPEconstruct (11.1.11)
A construct name is a name formed under 6.2.2. A name is an identifier. Identifiers are parsed by the parser.
Names must either not be given for the construct or used throughout when specified.
ENDstatement must also specify the same name (
SELECT CASEhas additional
SELECT RANKhas additional
SELECT TYPEhas additional type-guard-stmt These additional statements must meet the same constraint as the
ENDof the construct. Names must match, if present, or there must be no names for any of the clauses.
CYCLE statement takes an optional do-construct-name.
CYCLEhas a construct-name, then the
CYCLEstatement must appear within that named do-construct (C1134)
CYCLEdoes not have a do-construct-name, the
CYCLEstatement must appear within a do-construct (C1134)
EXIT statement takes an optional construct-name.
EXIThas a construct-name, then the
EXITstatement must appear within that named construct (C1166)
EXITdoes not have a construct-name, the
EXITstatement must appear within a do-construct (C1166)
DO CONCURRENTif the
EXITbelongs to the
DO CONCURRENTor an outer construct enclosing the
CRITICAL) if the
EXITbelongs to an outer construct enclosing the