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