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
39## Extensions, deletions, and legacy features supported by default
40
41* Tabs in source
42* `<>` as synonym for `.NE.` and `/=`
43* `$` and `@` as legal characters in names
44* Initialization in type declaration statements using `/values/`
45* Kind specification with `*`, e.g. `REAL*4`
46* `DOUBLE COMPLEX`
47* Signed complex literal constants
48* DEC `STRUCTURE`, `RECORD`, `UNION`, and `MAP`
49* Structure field access with `.field`
50* `BYTE` as synonym for `INTEGER(KIND=1)`
51* Quad precision REAL literals with `Q`
52* `X` prefix/suffix as synonym for `Z` on hexadecimal literals
53* `B`, `O`, `Z`, and `X` accepted as suffixes as well as prefixes
54* Triplets allowed in array constructors
55* Old-style `PARAMETER pi=3.14` statement without parentheses
56* `%LOC`, `%VAL`, and `%REF`
57* Leading comma allowed before I/O item list
58* Empty parentheses allowed in `PROGRAM P()`
59* Missing parentheses allowed in `FUNCTION F`
60* Cray based `POINTER(p,x)` and `LOC()` intrinsic (with `%LOC()` as
61  an alias)
62* Arithmetic `IF`.  (Which branch should NaN take? Fall through?)
63* `ASSIGN` statement, assigned `GO TO`, and assigned format
64* `PAUSE` statement
65* Hollerith literals and edit descriptors
66* `NAMELIST` allowed in the execution part
67* Omitted colons on type declaration statements with attributes
68* COMPLEX constructor expression, e.g. `(x+y,z)`
69* `+` and `-` before all primary expressions, e.g. `x*-y`
70* `.NOT. .NOT.` accepted
71* `NAME=` as synonym for `FILE=`
72* Data edit descriptors without width or other details
73* `D` lines in fixed form as comments or debug code
74* `CARRIAGECONTROL=` on the OPEN and INQUIRE statements
75* `CONVERT=` on the OPEN and INQUIRE statements
76* `DISPOSE=` on the OPEN and INQUIRE statements
77* Leading semicolons are ignored before any statement that
78  could have a label
79* The character `&` in column 1 in fixed form source is a variant form
80  of continuation line.
81* Character literals as elements of an array constructor without an explicit
82  type specifier need not have the same length; the longest literal determines
83  the length parameter of the implicit type, not the first.
84* Outside a character literal, a comment after a continuation marker (&)
85  need not begin with a comment marker (!).
86* Classic C-style /*comments*/ are skipped, so multi-language header
87  files are easier to write and use.
88* $ and \ edit descriptors are supported in FORMAT to suppress newline
89  output on user prompts.
90* REAL and DOUBLE PRECISION variable and bounds in DO loops
91* Integer literals without explicit kind specifiers that are out of range
92  for the default kind of INTEGER are assumed to have the least larger kind
93  that can hold them, if one exists.
94* BOZ literals can be used as INTEGER values in contexts where the type is
95  unambiguous: the right hand sides of assigments and initializations
96  of INTEGER entities, and as actual arguments to a few intrinsic functions
97  (ACHAR, BTEST, CHAR).  But they cannot be used if the type would not
98  be known (e.g., `IAND(X'1',X'2')`).
99* BOZ literals can also be used as REAL values in some contexts where the
100  type is unambiguous, such as initializations of REAL parameters.
101* EQUIVALENCE of numeric and character sequences (a ubiquitous extension)
102* Values for whole anonymous parent components in structure constructors
103  (e.g., `EXTENDEDTYPE(PARENTTYPE(1,2,3))` rather than `EXTENDEDTYPE(1,2,3)`
104   or `EXTENDEDTYPE(PARENTTYPE=PARENTTYPE(1,2,3))`).
105* Some intrinsic functions are specified in the standard as requiring the
106  same type and kind for their arguments (viz., ATAN with two arguments,
107  ATAN2, DIM, HYPOT, MAX, MIN, MOD, and MODULO);
108  we allow distinct types to be used, promoting
109  the arguments as if they were operands to an intrinsic `+` operator,
110  and defining the result type accordingly.
111* DOUBLE COMPLEX intrinsics DREAL, DCMPLX, DCONJG, and DIMAG.
112* INT_PTR_KIND intrinsic returns the kind of c_intptr_t.
113* Restricted specific conversion intrinsics FLOAT, SNGL, IDINT, IFIX, DREAL,
114  and DCMPLX accept arguments of any kind instead of only the default kind or
115  double precision kind. Their result kinds remain as specified.
116* Specific intrinsics AMAX0, AMAX1, AMIN0, AMIN1, DMAX1, DMIN1, MAX0, MAX1,
117  MIN0, and MIN1 accept more argument types than specified. They are replaced by
118  the related generics followed by conversions to the specified result types.
119* When a scalar CHARACTER actual argument of the same kind is known to
120  have a length shorter than the associated dummy argument, it is extended
121  on the right with blanks, similar to assignment.
122* When a dummy argument is `POINTER` or `ALLOCATABLE` and is `INTENT(IN)`, we
123  relax enforcement of some requirements on actual arguments that must otherwise
124  hold true for definable arguments.
125* Assignment of `LOGICAL` to `INTEGER` and vice versa (but not other types) is
126  allowed.  The values are normalized.
127* An effectively empty source file (no program unit) is accepted and
128  produces an empty relocatable output file.
129* A `RETURN` statement may appear in a main program.
130* DATA statement initialization is allowed for procedure pointers outside
131  structure constructors.
132* Nonstandard intrinsic functions: ISNAN
133
134### Extensions supported when enabled by options
135
136* C-style backslash escape sequences in quoted CHARACTER literals
137  (but not Hollerith) [-fbackslash]
138* Logical abbreviations `.T.`, `.F.`, `.N.`, `.A.`, `.O.`, and `.X.`
139  [-flogical-abbreviations]
140* `.XOR.` as a synonym for `.NEQV.` [-fxor-operator]
141* The default `INTEGER` type is required by the standard to occupy
142  the same amount of storage as the default `REAL` type.  Default
143  `REAL` is of course 32-bit IEEE-754 floating-point today.  This legacy
144  rule imposes an artificially small constraint in some cases
145  where Fortran mandates that something have the default `INTEGER`
146  type: specifically, the results of references to the intrinsic functions
147  `SIZE`, `LBOUND`, `UBOUND`, `SHAPE`, and the location reductions
148  `FINDLOC`, `MAXLOC`, and `MINLOC` in the absence of an explicit
149  `KIND=` actual argument.  We return `INTEGER(KIND=8)` by default in
150  these cases when the `-flarge-sizes` option is enabled.
151* Treat each specification-part like is has `IMPLICIT NONE`
152  [-fimplicit-none-type-always]
153* Ignore occurrences of `IMPLICIT NONE` and `IMPLICIT NONE(TYPE)`
154  [-fimplicit-none-type-never]
155
156### Extensions and legacy features deliberately not supported
157
158* `.LG.` as synonym for `.NE.`
159* `REDIMENSION`
160* Allocatable `COMMON`
161* Expressions in formats
162* `ACCEPT` as synonym for `READ *`
163* `TYPE` as synonym for `PRINT`
164* `ARRAY` as synonym for `DIMENSION`
165* `VIRTUAL` as synonym for `DIMENSION`
166* `ENCODE` and `DECODE` as synonyms for internal I/O
167* `IMPLICIT AUTOMATIC`, `IMPLICIT STATIC`
168* Default exponent of zero, e.g. `3.14159E`
169* Characters in defined operators that are neither letters nor digits
170* `B` suffix on unquoted octal constants
171* `Z` prefix on unquoted hexadecimal constants (dangerous)
172* `T` and `F` as abbreviations for `.TRUE.` and `.FALSE.` in DATA (PGI/XLF)
173* Use of host FORMAT labels in internal subprograms (PGI-only feature)
174* ALLOCATE(TYPE(derived)::...) as variant of correct ALLOCATE(derived::...) (PGI only)
175* Defining an explicit interface for a subprogram within itself (PGI only)
176* USE association of a procedure interface within that same procedure's definition
177* NULL() as a structure constructor expression for an ALLOCATABLE component (PGI).
178* Conversion of LOGICAL to INTEGER in expressions.
179* IF (integer expression) THEN ... END IF  (PGI/Intel)
180* Comparsion of LOGICAL with ==/.EQ. rather than .EQV. (also .NEQV.) (PGI/Intel)
181* Procedure pointers in COMMON blocks (PGI/Intel)
182* Underindexing multi-dimensional arrays (e.g., A(1) rather than A(1,1)) (PGI only)
183* Legacy PGI `NCHARACTER` type and `NC` Kanji character literals
184* Using non-integer expressions for array bounds (e.g., REAL A(3.14159)) (PGI/Intel)
185* Mixing INTEGER types as operands to bit intrinsics (e.g., IAND); only two
186  compilers support it, and they disagree on sign extension.
187* Module & program names that conflict with an object inside the unit (PGI only).
188* When the same name is brought into scope via USE association from
189  multiple modules, the name must refer to a generic interface; PGI
190  allows a name to be a procedure from one module and a generic interface
191  from another.
192* Type parameter declarations must come first in a derived type definition;
193  some compilers allow them to follow `PRIVATE`, or be intermixed with the
194  component declarations.
195* Wrong argument types in calls to specific intrinsics that have different names than the
196  related generics. Some accepted exceptions are listed above in the allowed extensions.
197  PGI, Intel, and XLF support this in ways that are not numerically equivalent.
198  PGI converts the arguments while Intel and XLF replace the specific by the related generic.
199
200## Preprocessing behavior
201
202* The preprocessor is always run, whatever the filename extension may be.
203* We respect Fortran comments in macro actual arguments (like GNU, Intel, NAG;
204  unlike PGI and XLF) on the principle that macro calls should be treated
205  like function references.  Fortran's line continuation methods also work.
206