1<!--===- docs/Extensions.md
2
3   Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4   See https://llvm.org/LICENSE.txt for license information.
5   SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
7-->
8
9# Fortran Extensions supported by Flang
10
11```eval_rst
12.. contents::
13   :local:
14```
15
16As a general principle, this compiler will accept by default and
17without complaint many legacy features, extensions to the standard
18language, and features that have been deleted from the standard,
19so long as the recognition of those features would not cause a
20standard-conforming program to be rejected or misinterpreted.
21
22Other non-standard features, which do conflict with the current
23standard specification of the Fortran programming language, are
24accepted if enabled by command-line options.
25
26## Intentional violations of the standard
27
28* Scalar `INTEGER` actual argument expressions (not variables!)
29  are converted to the kinds of scalar `INTEGER` dummy arguments
30  when the interface is explicit and the kinds differ.
31  This conversion allows the results of the intrinsics like
32  `SIZE` that (as mentioned below) may return non-default
33  `INTEGER` results by default to be passed.  A warning is
34  emitted when truncation is possible.
35* We are not strict on the contents of `BLOCK DATA` subprograms
36  so long as they contain no executable code, no internal subprograms,
37  and allocate no storage outside a named `COMMON` block.  (C1415)
38* Delimited list-directed (and NAMELIST) character output is required
39  to emit contiguous doubled instances of the delimiter character
40  when it appears in the output value.  When fixed-size records
41  are being emitted, as is the case with internal output, this
42  is not possible when the problematic character falls on the last
43  position of a record.  No two other Fortran compilers do the same
44  thing in this situation so there is no good precedent to follow.
45  Because it seems least wrong, we emit one copy of the delimiter as
46  the last character of the current record and another as the first
47  character of the next record.  (The second-least-wrong alternative
48  might be to flag a runtime error, but that seems harsh since it's
49  not an explicit error in the standard, and the output may not have
50  to be usable later as input anyway.)
51  Consequently, the output is not suitable for use as list-directed or
52  NAMELIST input.  If a later standard were to clarify this case, this
53  behavior will change as needed to conform.
54```
55character(11) :: buffer(3)
56character(10) :: quotes = '""""""""""'
57write(buffer,*,delim="QUOTE") quotes
58print "('>',a10,'<')", buffer
59end
60```
61* The name of the control variable in an implied DO loop in an array
62  constructor or DATA statement has a scope over the value-list only,
63  not the bounds of the implied DO loop.  It is not advisable to use
64  an object of the same name as the index variable in a bounds
65  expression, but it will work, instead of being needlessly undefined.
66
67## Extensions, deletions, and legacy features supported by default
68
69* Tabs in source
70* `<>` as synonym for `.NE.` and `/=`
71* `$` and `@` as legal characters in names
72* Initialization in type declaration statements using `/values/`
73* Kind specification with `*`, e.g. `REAL*4`
74* `DOUBLE COMPLEX`
75* Signed complex literal constants
76* DEC `STRUCTURE`, `RECORD`, `UNION`, and `MAP`
77* Structure field access with `.field`
78* `BYTE` as synonym for `INTEGER(KIND=1)`
79* Quad precision REAL literals with `Q`
80* `X` prefix/suffix as synonym for `Z` on hexadecimal literals
81* `B`, `O`, `Z`, and `X` accepted as suffixes as well as prefixes
82* Triplets allowed in array constructors
83* `%LOC`, `%VAL`, and `%REF`
84* Leading comma allowed before I/O item list
85* Empty parentheses allowed in `PROGRAM P()`
86* Missing parentheses allowed in `FUNCTION F`
87* Cray based `POINTER(p,x)` and `LOC()` intrinsic (with `%LOC()` as
88  an alias)
89* Arithmetic `IF`.  (Which branch should NaN take? Fall through?)
90* `ASSIGN` statement, assigned `GO TO`, and assigned format
91* `PAUSE` statement
92* Hollerith literals and edit descriptors
93* `NAMELIST` allowed in the execution part
94* Omitted colons on type declaration statements with attributes
95* COMPLEX constructor expression, e.g. `(x+y,z)`
96* `+` and `-` before all primary expressions, e.g. `x*-y`
97* `.NOT. .NOT.` accepted
98* `NAME=` as synonym for `FILE=`
99* Data edit descriptors without width or other details
100* `D` lines in fixed form as comments or debug code
101* `CARRIAGECONTROL=` on the OPEN and INQUIRE statements
102* `CONVERT=` on the OPEN and INQUIRE statements
103* `DISPOSE=` on the OPEN and INQUIRE statements
104* Leading semicolons are ignored before any statement that
105  could have a label
106* The character `&` in column 1 in fixed form source is a variant form
107  of continuation line.
108* Character literals as elements of an array constructor without an explicit
109  type specifier need not have the same length; the longest literal determines
110  the length parameter of the implicit type, not the first.
111* Outside a character literal, a comment after a continuation marker (&)
112  need not begin with a comment marker (!).
113* Classic C-style /*comments*/ are skipped, so multi-language header
114  files are easier to write and use.
115* $ and \ edit descriptors are supported in FORMAT to suppress newline
116  output on user prompts.
117* REAL and DOUBLE PRECISION variable and bounds in DO loops
118* Integer literals without explicit kind specifiers that are out of range
119  for the default kind of INTEGER are assumed to have the least larger kind
120  that can hold them, if one exists.
121* BOZ literals can be used as INTEGER values in contexts where the type is
122  unambiguous: the right hand sides of assigments and initializations
123  of INTEGER entities, and as actual arguments to a few intrinsic functions
124  (ACHAR, BTEST, CHAR).  BOZ literals are interpreted as default INTEGER
125  when they appear as the first items of array constructors with no
126  explicit type.  Otherwise, they generally cannot be used if the type would
127  not be known (e.g., `IAND(X'1',X'2')`).
128* BOZ literals can also be used as REAL values in some contexts where the
129  type is unambiguous, such as initializations of REAL parameters.
130* EQUIVALENCE of numeric and character sequences (a ubiquitous extension)
131* Values for whole anonymous parent components in structure constructors
132  (e.g., `EXTENDEDTYPE(PARENTTYPE(1,2,3))` rather than `EXTENDEDTYPE(1,2,3)`
133   or `EXTENDEDTYPE(PARENTTYPE=PARENTTYPE(1,2,3))`).
134* Some intrinsic functions are specified in the standard as requiring the
135  same type and kind for their arguments (viz., ATAN with two arguments,
136  ATAN2, DIM, HYPOT, MAX, MIN, MOD, and MODULO);
137  we allow distinct types to be used, promoting
138  the arguments as if they were operands to an intrinsic `+` operator,
139  and defining the result type accordingly.
140* DOUBLE COMPLEX intrinsics DREAL, DCMPLX, DCONJG, and DIMAG.
141* The DFLOAT intrinsic function.
142* INT_PTR_KIND intrinsic returns the kind of c_intptr_t.
143* Restricted specific conversion intrinsics FLOAT, SNGL, IDINT, IFIX, DREAL,
144  and DCMPLX accept arguments of any kind instead of only the default kind or
145  double precision kind. Their result kinds remain as specified.
146* Specific intrinsics AMAX0, AMAX1, AMIN0, AMIN1, DMAX1, DMIN1, MAX0, MAX1,
147  MIN0, and MIN1 accept more argument types than specified. They are replaced by
148  the related generics followed by conversions to the specified result types.
149* When a scalar CHARACTER actual argument of the same kind is known to
150  have a length shorter than the associated dummy argument, it is extended
151  on the right with blanks, similar to assignment.
152* When a dummy argument is `POINTER` or `ALLOCATABLE` and is `INTENT(IN)`, we
153  relax enforcement of some requirements on actual arguments that must otherwise
154  hold true for definable arguments.
155* Assignment of `LOGICAL` to `INTEGER` and vice versa (but not other types) is
156  allowed.  The values are normalized.
157* An effectively empty source file (no program unit) is accepted and
158  produces an empty relocatable output file.
159* A `RETURN` statement may appear in a main program.
160* DATA statement initialization is allowed for procedure pointers outside
161  structure constructors.
162* Nonstandard intrinsic functions: ISNAN, SIZEOF
163* A forward reference to a default INTEGER scalar dummy argument is
164  permitted to appear in a specification expression, such as an array
165  bound, in a scope with IMPLICIT NONE(TYPE) if the name
166  of the dummy argument would have caused it to be implicitly typed
167  as default INTEGER if IMPLICIT NONE(TYPE) were absent.
168* OPEN(ACCESS='APPEND') is interpreted as OPEN(POSITION='APPEND')
169  to ease porting from Sun Fortran.
170
171### Extensions supported when enabled by options
172
173* C-style backslash escape sequences in quoted CHARACTER literals
174  (but not Hollerith) [-fbackslash]
175* Logical abbreviations `.T.`, `.F.`, `.N.`, `.A.`, `.O.`, and `.X.`
176  [-flogical-abbreviations]
177* `.XOR.` as a synonym for `.NEQV.` [-fxor-operator]
178* The default `INTEGER` type is required by the standard to occupy
179  the same amount of storage as the default `REAL` type.  Default
180  `REAL` is of course 32-bit IEEE-754 floating-point today.  This legacy
181  rule imposes an artificially small constraint in some cases
182  where Fortran mandates that something have the default `INTEGER`
183  type: specifically, the results of references to the intrinsic functions
184  `SIZE`, `STORAGE_SIZE`,`LBOUND`, `UBOUND`, `SHAPE`, and the location reductions
185  `FINDLOC`, `MAXLOC`, and `MINLOC` in the absence of an explicit
186  `KIND=` actual argument.  We return `INTEGER(KIND=8)` by default in
187  these cases when the `-flarge-sizes` option is enabled.
188  `SIZEOF` and `C_SIZEOF` always return `INTEGER(KIND=8)`.
189* Treat each specification-part like is has `IMPLICIT NONE`
190  [-fimplicit-none-type-always]
191* Ignore occurrences of `IMPLICIT NONE` and `IMPLICIT NONE(TYPE)`
192  [-fimplicit-none-type-never]
193* Old-style `PARAMETER pi=3.14` statement without parentheses
194  [-falternative-parameter-statement]
195
196### Extensions and legacy features deliberately not supported
197
198* `.LG.` as synonym for `.NE.`
199* `REDIMENSION`
200* Allocatable `COMMON`
201* Expressions in formats
202* `ACCEPT` as synonym for `READ *`
203* `TYPE` as synonym for `PRINT`
204* `ARRAY` as synonym for `DIMENSION`
205* `VIRTUAL` as synonym for `DIMENSION`
206* `ENCODE` and `DECODE` as synonyms for internal I/O
207* `IMPLICIT AUTOMATIC`, `IMPLICIT STATIC`
208* Default exponent of zero, e.g. `3.14159E`
209* Characters in defined operators that are neither letters nor digits
210* `B` suffix on unquoted octal constants
211* `Z` prefix on unquoted hexadecimal constants (dangerous)
212* `T` and `F` as abbreviations for `.TRUE.` and `.FALSE.` in DATA (PGI/XLF)
213* Use of host FORMAT labels in internal subprograms (PGI-only feature)
214* ALLOCATE(TYPE(derived)::...) as variant of correct ALLOCATE(derived::...) (PGI only)
215* Defining an explicit interface for a subprogram within itself (PGI only)
216* USE association of a procedure interface within that same procedure's definition
217* NULL() as a structure constructor expression for an ALLOCATABLE component (PGI).
218* Conversion of LOGICAL to INTEGER in expressions.
219* IF (integer expression) THEN ... END IF  (PGI/Intel)
220* Comparsion of LOGICAL with ==/.EQ. rather than .EQV. (also .NEQV.) (PGI/Intel)
221* Procedure pointers in COMMON blocks (PGI/Intel)
222* Underindexing multi-dimensional arrays (e.g., A(1) rather than A(1,1)) (PGI only)
223* Legacy PGI `NCHARACTER` type and `NC` Kanji character literals
224* Using non-integer expressions for array bounds (e.g., REAL A(3.14159)) (PGI/Intel)
225* Mixing INTEGER types as operands to bit intrinsics (e.g., IAND); only two
226  compilers support it, and they disagree on sign extension.
227* Module & program names that conflict with an object inside the unit (PGI only).
228* When the same name is brought into scope via USE association from
229  multiple modules, the name must refer to a generic interface; PGI
230  allows a name to be a procedure from one module and a generic interface
231  from another.
232* Type parameter declarations must come first in a derived type definition;
233  some compilers allow them to follow `PRIVATE`, or be intermixed with the
234  component declarations.
235* Wrong argument types in calls to specific intrinsics that have different names than the
236  related generics. Some accepted exceptions are listed above in the allowed extensions.
237  PGI, Intel, and XLF support this in ways that are not numerically equivalent.
238  PGI converts the arguments while Intel and XLF replace the specific by the related generic.
239
240## Preprocessing behavior
241
242* The preprocessor is always run, whatever the filename extension may be.
243* We respect Fortran comments in macro actual arguments (like GNU, Intel, NAG;
244  unlike PGI and XLF) on the principle that macro calls should be treated
245  like function references.  Fortran's line continuation methods also work.
246
247## Standard features not silently accepted
248
249* Fortran explicitly ignores type declaration statements when they
250  attempt to type the name of a generic intrinsic function (8.2 p3).
251  One can declare `CHARACTER::COS` and still get a real result
252  from `COS(3.14159)`, for example.  f18 will complain when a
253  generic intrinsic function's inferred result type does not
254  match an explicit declaration.  This message is a warning.
255
256## Standard features that might as well not be
257
258* f18 supports designators with constant expressions, properly
259  constrained, as initial data targets for data pointers in
260  initializers of variable and component declarations and in
261  `DATA` statements; e.g., `REAL, POINTER :: P => T(1:10:2)`.
262  This Fortran 2008 feature might as well be viewed like an
263  extension; no other compiler that we've tested can handle
264  it yet.
265