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. These conversions 35 are not applied in calls to non-intrinsic generic procedures. 36* We are not strict on the contents of `BLOCK DATA` subprograms 37 so long as they contain no executable code, no internal subprograms, 38 and allocate no storage outside a named `COMMON` block. (C1415) 39* Delimited list-directed (and NAMELIST) character output is required 40 to emit contiguous doubled instances of the delimiter character 41 when it appears in the output value. When fixed-size records 42 are being emitted, as is the case with internal output, this 43 is not possible when the problematic character falls on the last 44 position of a record. No two other Fortran compilers do the same 45 thing in this situation so there is no good precedent to follow. 46 Because it seems least wrong, we emit one copy of the delimiter as 47 the last character of the current record and another as the first 48 character of the next record. (The second-least-wrong alternative 49 might be to flag a runtime error, but that seems harsh since it's 50 not an explicit error in the standard, and the output may not have 51 to be usable later as input anyway.) 52 Consequently, the output is not suitable for use as list-directed or 53 NAMELIST input. If a later standard were to clarify this case, this 54 behavior will change as needed to conform. 55``` 56character(11) :: buffer(3) 57character(10) :: quotes = '""""""""""' 58write(buffer,*,delim="QUOTE") quotes 59print "('>',a10,'<')", buffer 60end 61``` 62* The name of the control variable in an implied DO loop in an array 63 constructor or DATA statement has a scope over the value-list only, 64 not the bounds of the implied DO loop. It is not advisable to use 65 an object of the same name as the index variable in a bounds 66 expression, but it will work, instead of being needlessly undefined. 67* If both the `COUNT=` and the `COUNT_MAX=` optional arguments are 68 present on the same call to the intrinsic subroutine `SYSTEM_CLOCK`, 69 we require that their types have the same integer kind, since the 70 kind of these arguments is used to select the clock rate. In common 71 with some other compilers, the clock rate varies from tenths of a 72 second to nanoseconds depending on argument kind and platform support. 73* If a dimension of a descriptor has zero extent in a call to 74 `CFI_section`, `CFI_setpointer` or `CFI_allocate`, the lower 75 bound on that dimension will be set to 1 for consistency with 76 the `LBOUND()` intrinsic function. 77 78## Extensions, deletions, and legacy features supported by default 79 80* Tabs in source 81* `<>` as synonym for `.NE.` and `/=` 82* `$` and `@` as legal characters in names 83* Initialization in type declaration statements using `/values/` 84* Kind specification with `*`, e.g. `REAL*4` 85* `DOUBLE COMPLEX` as a synonym for `COMPLEX(KIND(0.D0))` -- 86 but not when spelled `TYPE(DOUBLECOMPLEX)`. 87* Signed complex literal constants 88* DEC `STRUCTURE`, `RECORD`, with '%FILL'; but `UNION`, and `MAP` 89 are not yet supported throughout compilation, and elicit a 90 "not yet implemented" message. 91* Structure field access with `.field` 92* `BYTE` as synonym for `INTEGER(KIND=1)`; but not when spelled `TYPE(BYTE)`. 93* Quad precision REAL literals with `Q` 94* `X` prefix/suffix as synonym for `Z` on hexadecimal literals 95* `B`, `O`, `Z`, and `X` accepted as suffixes as well as prefixes 96* Triplets allowed in array constructors 97* `%LOC`, `%VAL`, and `%REF` 98* Leading comma allowed before I/O item list 99* Empty parentheses allowed in `PROGRAM P()` 100* Missing parentheses allowed in `FUNCTION F` 101* Cray based `POINTER(p,x)` and `LOC()` intrinsic (with `%LOC()` as 102 an alias) 103* Arithmetic `IF`. (Which branch should NaN take? Fall through?) 104* `ASSIGN` statement, assigned `GO TO`, and assigned format 105* `PAUSE` statement 106* Hollerith literals and edit descriptors 107* `NAMELIST` allowed in the execution part 108* Omitted colons on type declaration statements with attributes 109* COMPLEX constructor expression, e.g. `(x+y,z)` 110* `+` and `-` before all primary expressions, e.g. `x*-y` 111* `.NOT. .NOT.` accepted 112* `NAME=` as synonym for `FILE=` 113* Data edit descriptors without width or other details 114* `D` lines in fixed form as comments or debug code 115* `CARRIAGECONTROL=` on the OPEN and INQUIRE statements 116* `CONVERT=` on the OPEN and INQUIRE statements 117* `DISPOSE=` on the OPEN and INQUIRE statements 118* Leading semicolons are ignored before any statement that 119 could have a label 120* The character `&` in column 1 in fixed form source is a variant form 121 of continuation line. 122* Character literals as elements of an array constructor without an explicit 123 type specifier need not have the same length; the longest literal determines 124 the length parameter of the implicit type, not the first. 125* Outside a character literal, a comment after a continuation marker (&) 126 need not begin with a comment marker (!). 127* Classic C-style /*comments*/ are skipped, so multi-language header 128 files are easier to write and use. 129* $ and \ edit descriptors are supported in FORMAT to suppress newline 130 output on user prompts. 131* Tabs in format strings (not `FORMAT` statements) are allowed on output. 132* REAL and DOUBLE PRECISION variable and bounds in DO loops 133* Integer literals without explicit kind specifiers that are out of range 134 for the default kind of INTEGER are assumed to have the least larger kind 135 that can hold them, if one exists. 136* BOZ literals can be used as INTEGER values in contexts where the type is 137 unambiguous: the right hand sides of assigments and initializations 138 of INTEGER entities, as actual arguments to a few intrinsic functions 139 (ACHAR, BTEST, CHAR), and as actual arguments of references to 140 procedures with explicit interfaces whose corresponding dummy 141 argument has a numeric type to which the BOZ literal may be 142 converted. BOZ literals are interpreted as default INTEGER only 143 when they appear as the first items of array constructors with no 144 explicit type. Otherwise, they generally cannot be used if the type would 145 not be known (e.g., `IAND(X'1',X'2')`). 146* BOZ literals can also be used as REAL values in some contexts where the 147 type is unambiguous, such as initializations of REAL parameters. 148* EQUIVALENCE of numeric and character sequences (a ubiquitous extension), 149 as well as of sequences of non-default kinds of numeric types 150 with each other. 151* Values for whole anonymous parent components in structure constructors 152 (e.g., `EXTENDEDTYPE(PARENTTYPE(1,2,3))` rather than `EXTENDEDTYPE(1,2,3)` 153 or `EXTENDEDTYPE(PARENTTYPE=PARENTTYPE(1,2,3))`). 154* Some intrinsic functions are specified in the standard as requiring the 155 same type and kind for their arguments (viz., ATAN with two arguments, 156 ATAN2, DIM, HYPOT, MAX, MIN, MOD, and MODULO); 157 we allow distinct types to be used, promoting 158 the arguments as if they were operands to an intrinsic `+` operator, 159 and defining the result type accordingly. 160* DOUBLE COMPLEX intrinsics DREAL, DCMPLX, DCONJG, and DIMAG. 161* The DFLOAT intrinsic function. 162* INT_PTR_KIND intrinsic returns the kind of c_intptr_t. 163* Restricted specific conversion intrinsics FLOAT, SNGL, IDINT, IFIX, DREAL, 164 and DCMPLX accept arguments of any kind instead of only the default kind or 165 double precision kind. Their result kinds remain as specified. 166* Specific intrinsics AMAX0, AMAX1, AMIN0, AMIN1, DMAX1, DMIN1, MAX0, MAX1, 167 MIN0, and MIN1 accept more argument types than specified. They are replaced by 168 the related generics followed by conversions to the specified result types. 169* When a scalar CHARACTER actual argument of the same kind is known to 170 have a length shorter than the associated dummy argument, it is extended 171 on the right with blanks, similar to assignment. 172* When a dummy argument is `POINTER` or `ALLOCATABLE` and is `INTENT(IN)`, we 173 relax enforcement of some requirements on actual arguments that must otherwise 174 hold true for definable arguments. 175* Assignment of `LOGICAL` to `INTEGER` and vice versa (but not other types) is 176 allowed. The values are normalized. 177* Static initialization of `LOGICAL` with `INTEGER` is allowed in `DATA` statements 178 and object initializers. 179 The results are *not* normalized to canonical `.TRUE.`/`.FALSE.`. 180 Static initialization of `INTEGER` with `LOGICAL` is also permitted. 181* An effectively empty source file (no program unit) is accepted and 182 produces an empty relocatable output file. 183* A `RETURN` statement may appear in a main program. 184* DATA statement initialization is allowed for procedure pointers outside 185 structure constructors. 186* Nonstandard intrinsic functions: ISNAN, SIZEOF 187* A forward reference to a default INTEGER scalar dummy argument is 188 permitted to appear in a specification expression, such as an array 189 bound, in a scope with IMPLICIT NONE(TYPE) if the name 190 of the dummy argument would have caused it to be implicitly typed 191 as default INTEGER if IMPLICIT NONE(TYPE) were absent. 192* OPEN(ACCESS='APPEND') is interpreted as OPEN(POSITION='APPEND') 193 to ease porting from Sun Fortran. 194* Intrinsic subroutines EXIT([status]) and ABORT() 195* The definition of simple contiguity in 9.5.4 applies only to arrays; 196 we also treat scalars as being trivially contiguous, so that they 197 can be used in contexts like data targets in pointer assignments 198 with bounds remapping. 199* We support some combinations of specific procedures in generic 200 interfaces that a strict reading of the standard would preclude 201 when their calls must nonetheless be distinguishable. 202 Specifically, `ALLOCATABLE` dummy arguments are distinguishing 203 if an actual argument acceptable to one could not be passed to 204 the other & vice versa because exactly one is polymorphic or 205 exactly one is unlimited polymorphic). 206* External unit 0 is predefined and connected to the standard error output, 207 and defined as `ERROR_UNIT` in the intrinsic `ISO_FORTRAN_ENV` module. 208* Objects in blank COMMON may be initialized. 209* Multiple specifications of the SAVE attribute on the same object 210 are allowed, with a warning. 211* Specific intrinsic functions BABS, IIABS, JIABS, KIABS, ZABS, and CDABS. 212* A `POINTER` component's type need not be a sequence type when 213 the component appears in a derived type with `SEQUENCE`. 214 (This case should probably be an exception to constraint C740 in 215 the standard.) 216* Format expressions that have type but are not character and not 217 integer scalars are accepted so long as they are simply contiguous. 218 This legacy extension supports pre-Fortran'77 usage in which 219 variables initialized in DATA statements with Hollerith literals 220 as modifiable formats. 221* At runtime, `NAMELIST` input will skip over `NAMELIST` groups 222 with other names, and will treat text before and between groups 223 as if they were comment lines, even if not begun with `!`. 224* Commas are required in FORMAT statements and character variables 225 only when they prevent ambiguity. 226* Legacy names `AND`, `OR`, and `XOR` are accepted as aliases for 227 the standard intrinsic functions `IAND`, `IOR`, and `IEOR` 228 respectively. 229 230### Extensions supported when enabled by options 231 232* C-style backslash escape sequences in quoted CHARACTER literals 233 (but not Hollerith) [-fbackslash] 234* Logical abbreviations `.T.`, `.F.`, `.N.`, `.A.`, `.O.`, and `.X.` 235 [-flogical-abbreviations] 236* `.XOR.` as a synonym for `.NEQV.` [-fxor-operator] 237* The default `INTEGER` type is required by the standard to occupy 238 the same amount of storage as the default `REAL` type. Default 239 `REAL` is of course 32-bit IEEE-754 floating-point today. This legacy 240 rule imposes an artificially small constraint in some cases 241 where Fortran mandates that something have the default `INTEGER` 242 type: specifically, the results of references to the intrinsic functions 243 `SIZE`, `STORAGE_SIZE`,`LBOUND`, `UBOUND`, `SHAPE`, and the location reductions 244 `FINDLOC`, `MAXLOC`, and `MINLOC` in the absence of an explicit 245 `KIND=` actual argument. We return `INTEGER(KIND=8)` by default in 246 these cases when the `-flarge-sizes` option is enabled. 247 `SIZEOF` and `C_SIZEOF` always return `INTEGER(KIND=8)`. 248* Treat each specification-part like is has `IMPLICIT NONE` 249 [-fimplicit-none-type-always] 250* Ignore occurrences of `IMPLICIT NONE` and `IMPLICIT NONE(TYPE)` 251 [-fimplicit-none-type-never] 252* Old-style `PARAMETER pi=3.14` statement without parentheses 253 [-falternative-parameter-statement] 254 255### Extensions and legacy features deliberately not supported 256 257* `.LG.` as synonym for `.NE.` 258* `REDIMENSION` 259* Allocatable `COMMON` 260* Expressions in formats 261* `ACCEPT` as synonym for `READ *` 262* `TYPE` as synonym for `PRINT` 263* `ARRAY` as synonym for `DIMENSION` 264* `VIRTUAL` as synonym for `DIMENSION` 265* `ENCODE` and `DECODE` as synonyms for internal I/O 266* `IMPLICIT AUTOMATIC`, `IMPLICIT STATIC` 267* Default exponent of zero, e.g. `3.14159E` 268* Characters in defined operators that are neither letters nor digits 269* `B` suffix on unquoted octal constants 270* `Z` prefix on unquoted hexadecimal constants (dangerous) 271* `T` and `F` as abbreviations for `.TRUE.` and `.FALSE.` in DATA (PGI/XLF) 272* Use of host FORMAT labels in internal subprograms (PGI-only feature) 273* ALLOCATE(TYPE(derived)::...) as variant of correct ALLOCATE(derived::...) (PGI only) 274* Defining an explicit interface for a subprogram within itself (PGI only) 275* USE association of a procedure interface within that same procedure's definition 276* NULL() as a structure constructor expression for an ALLOCATABLE component (PGI). 277* Conversion of LOGICAL to INTEGER in expressions. 278* IF (integer expression) THEN ... END IF (PGI/Intel) 279* Comparsion of LOGICAL with ==/.EQ. rather than .EQV. (also .NEQV.) (PGI/Intel) 280* Procedure pointers in COMMON blocks (PGI/Intel) 281* Underindexing multi-dimensional arrays (e.g., A(1) rather than A(1,1)) (PGI only) 282* Legacy PGI `NCHARACTER` type and `NC` Kanji character literals 283* Using non-integer expressions for array bounds (e.g., REAL A(3.14159)) (PGI/Intel) 284* Mixing INTEGER types as operands to bit intrinsics (e.g., IAND); only two 285 compilers support it, and they disagree on sign extension. 286* Module & program names that conflict with an object inside the unit (PGI only). 287* When the same name is brought into scope via USE association from 288 multiple modules, the name must refer to a generic interface; PGI 289 allows a name to be a procedure from one module and a generic interface 290 from another. 291* Type parameter declarations must come first in a derived type definition; 292 some compilers allow them to follow `PRIVATE`, or be intermixed with the 293 component declarations. 294* Wrong argument types in calls to specific intrinsics that have different names than the 295 related generics. Some accepted exceptions are listed above in the allowed extensions. 296 PGI, Intel, and XLF support this in ways that are not numerically equivalent. 297 PGI converts the arguments while Intel and XLF replace the specific by the related generic. 298 299## Preprocessing behavior 300 301* The preprocessor is always run, whatever the filename extension may be. 302* We respect Fortran comments in macro actual arguments (like GNU, Intel, NAG; 303 unlike PGI and XLF) on the principle that macro calls should be treated 304 like function references. Fortran's line continuation methods also work. 305 306## Standard features not silently accepted 307 308* Fortran explicitly ignores type declaration statements when they 309 attempt to type the name of a generic intrinsic function (8.2 p3). 310 One can declare `CHARACTER::COS` and still get a real result 311 from `COS(3.14159)`, for example. f18 will complain when a 312 generic intrinsic function's inferred result type does not 313 match an explicit declaration. This message is a warning. 314 315## Standard features that might as well not be 316 317* f18 supports designators with constant expressions, properly 318 constrained, as initial data targets for data pointers in 319 initializers of variable and component declarations and in 320 `DATA` statements; e.g., `REAL, POINTER :: P => T(1:10:2)`. 321 This Fortran 2008 feature might as well be viewed like an 322 extension; no other compiler that we've tested can handle 323 it yet. 324 325## Behavior in cases where the standard is ambiguous or indefinite 326 327* When an inner procedure of a subprogram uses the value or an attribute 328 of an undeclared name in a specification expression and that name does 329 not appear in the host, it is not clear in the standard whether that 330 name is an implicitly typed local variable of the inner procedure or a 331 host association with an implicitly typed local variable of the host. 332 For example: 333``` 334module module 335 contains 336 subroutine host(j) 337 ! Although "m" never appears in the specification or executable 338 ! parts of this subroutine, both of its contained subroutines 339 ! might be accessing it via host association. 340 integer, intent(in out) :: j 341 call inner1(j) 342 call inner2(j) 343 contains 344 subroutine inner1(n) 345 integer(kind(m)), intent(in) :: n 346 m = n + 1 347 end subroutine 348 subroutine inner2(n) 349 integer(kind(m)), intent(out) :: n 350 n = m + 2 351 end subroutine 352 end subroutine 353end module 354 355program demo 356 use module 357 integer :: k 358 k = 0 359 call host(k) 360 print *, k, " should be 3" 361end 362 363``` 364 365 Other Fortran compilers disagree in their interpretations of this example; 366 some seem to treat the references to `m` as if they were host associations 367 to an implicitly typed variable (and print `3`), while others seem to 368 treat them as references to implicitly typed local variabless, and 369 load uninitialized values. 370 371 In f18, we chose to emit an error message for this case since the standard 372 is unclear, the usage is not portable, and the issue can be easily resolved 373 by adding a declaration. 374 375* In subclause 7.5.6.2 of Fortran 2018 the standard defines a partial ordering 376 of the final subroutine calls for finalizable objects, their non-parent 377 components, and then their parent components. 378 (The object is finalized, then the non-parent components of each element, 379 and then the parent component.) 380 Some have argued that the standard permits an implementation 381 to finalize the parent component before finalizing an allocatable component in 382 the context of deallocation, and the next revision of the language may codify 383 this option. 384 In the interest of avoiding needless confusion, this compiler implements what 385 we believe to be the least surprising order of finalization. 386 Specifically: all non-parent components are finalized before 387 the parent, allocatable or not; 388 all finalization takes place before any deallocation; 389 and no object or subobject will be finalized more than once. 390 391* When `RECL=` is set via the `OPEN` statement for a sequential formatted input 392 file, it functions as an effective maximum record length. 393 Longer records, if any, will appear as if they had been truncated to 394 the value of `RECL=`. 395 (Other compilers ignore `RECL=`, signal an error, or apply effective truncation 396 to some forms of input in this situation.) 397 For sequential formatted output, RECL= serves as a limit on record lengths 398 that raises an error when it is exceeded. 399