]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/gfortran.texi
gcc.c (process_command): Update copyright notice dates.
[thirdparty/gcc.git] / gcc / fortran / gfortran.texi
CommitLineData
de514255 1\input texinfo @c -*-texinfo-*-
6de9cd9a
DN
2@c %**start of header
3@setfilename gfortran.info
3f27508c 4@set copyrights-gfortran 1999-2019
6de9cd9a
DN
5
6@include gcc-common.texi
7
7fc15ba5 8@settitle The GNU Fortran Compiler
6de9cd9a
DN
9
10@c Create a separate index for command line options
11@defcodeindex op
12@c Merge the standard indexes into a single one.
13@syncodeindex fn cp
14@syncodeindex vr cp
15@syncodeindex ky cp
16@syncodeindex pg cp
17@syncodeindex tp cp
18
c8cf50e4
BM
19@c TODO: The following "Part" definitions are included here temporarily
20@c until they are incorporated into the official Texinfo distribution.
21@c They borrow heavily from Texinfo's \unnchapentry definitions.
22
23@tex
24\gdef\part#1#2{%
25 \pchapsepmacro
26 \gdef\thischapter{}
27 \begingroup
28 \vglue\titlepagetopglue
29 \titlefonts \rm
30 \leftline{Part #1:@* #2}
31 \vskip4pt \hrule height 4pt width \hsize \vskip4pt
32 \endgroup
33 \writetocentry{part}{#2}{#1}
34}
35\gdef\blankpart{%
36 \writetocentry{blankpart}{}{}
37}
38% Part TOC-entry definition for summary contents.
39\gdef\dosmallpartentry#1#2#3#4{%
40 \vskip .5\baselineskip plus.2\baselineskip
41 \begingroup
42 \let\rm=\bf \rm
43 \tocentry{Part #2: #1}{\doshortpageno\bgroup#4\egroup}
44 \endgroup
45}
46\gdef\dosmallblankpartentry#1#2#3#4{%
47 \vskip .5\baselineskip plus.2\baselineskip
48}
49% Part TOC-entry definition for regular contents. This has to be
50% equated to an existing entry to not cause problems when the PDF
51% outline is created.
52\gdef\dopartentry#1#2#3#4{%
53 \unnchapentry{Part #2: #1}{}{#3}{#4}
54}
55\gdef\doblankpartentry#1#2#3#4{}
56@end tex
57
6de9cd9a
DN
58@c %**end of header
59
60@c Use with @@smallbook.
61
62@c %** start of document
63
64@c Cause even numbered pages to be printed on the left hand side of
65@c the page and odd numbered pages to be printed on the right hand
66@c side of the page. Using this, you can print on both sides of a
67@c sheet of paper and have the text on the same part of the sheet.
68
69@c The text on right hand pages is pushed towards the right hand
70@c margin and the text on left hand pages is pushed toward the left
71@c hand margin.
72@c (To provide the reverse effect, set bindingoffset to -0.75in.)
73
74@c @tex
75@c \global\bindingoffset=0.75in
76@c \global\normaloffset =0.75in
77@c @end tex
78
79@copying
80Copyright @copyright{} @value{copyrights-gfortran} Free Software Foundation, Inc.
81
82Permission is granted to copy, distribute and/or modify this document
07a67d6a 83under the terms of the GNU Free Documentation License, Version 1.3 or
6de9cd9a 84any later version published by the Free Software Foundation; with the
70b1e376
RW
85Invariant Sections being ``Funding Free Software'', the Front-Cover
86Texts being (a) (see below), and with the Back-Cover Texts being (b)
6de9cd9a
DN
87(see below). A copy of the license is included in the section entitled
88``GNU Free Documentation License''.
89
90(a) The FSF's Front-Cover Text is:
91
92 A GNU Manual
93
94(b) The FSF's Back-Cover Text is:
95
96 You have freedom to copy and modify this GNU Manual, like GNU
97 software. Copies published by the Free Software Foundation raise
98 funds for GNU development.
99@end copying
100
101@ifinfo
2cec61bd 102@dircategory Software development
6de9cd9a 103@direntry
7fc15ba5 104* gfortran: (gfortran). The GNU Fortran Compiler.
6de9cd9a
DN
105@end direntry
106This file documents the use and the internals of
7fc15ba5 107the GNU Fortran compiler, (@command{gfortran}).
6de9cd9a
DN
108
109Published by the Free Software Foundation
ab57747b
KC
11051 Franklin Street, Fifth Floor
111Boston, MA 02110-1301 USA
6de9cd9a
DN
112
113@insertcopying
114@end ifinfo
115
6de9cd9a
DN
116
117@setchapternewpage odd
118@titlepage
7fc15ba5 119@title Using GNU Fortran
7771bb62
BM
120@versionsubtitle
121@author The @t{gfortran} team
6de9cd9a
DN
122@page
123@vskip 0pt plus 1filll
4e8b3590 124Published by the Free Software Foundation@*
ab57747b
KC
12551 Franklin Street, Fifth Floor@*
126Boston, MA 02110-1301, USA@*
6de9cd9a
DN
127@c Last printed ??ber, 19??.@*
128@c Printed copies are available for $? each.@*
129@c ISBN ???
130@sp 1
131@insertcopying
132@end titlepage
c8cf50e4
BM
133
134@c TODO: The following "Part" definitions are included here temporarily
135@c until they are incorporated into the official Texinfo distribution.
136
137@tex
138\global\let\partentry=\dosmallpartentry
139\global\let\blankpartentry=\dosmallblankpartentry
140@end tex
6de9cd9a 141@summarycontents
c8cf50e4
BM
142
143@tex
144\global\let\partentry=\dopartentry
145\global\let\blankpartentry=\doblankpartentry
146@end tex
6de9cd9a 147@contents
c8cf50e4 148
6de9cd9a
DN
149@page
150
e6b38f67
BM
151@c ---------------------------------------------------------------------
152@c TexInfo table of contents.
153@c ---------------------------------------------------------------------
154
155@ifnottex
a63dad5b 156@node Top
6de9cd9a
DN
157@top Introduction
158@cindex Introduction
159
bc0229f9 160This manual documents the use of @command{gfortran},
3994c6b1 161the GNU Fortran compiler. You can find in this manual how to invoke
e0f2a7c6 162@command{gfortran}, as well as its features and incompatibilities.
6de9cd9a
DN
163
164@ifset DEVELOPMENT
165@emph{Warning:} This document, and the compiler it describes, are still
8db2ba40 166under development. While efforts are made to keep it up-to-date, it might
7fc15ba5 167not accurately reflect the status of the most recent GNU Fortran compiler.
6de9cd9a
DN
168@end ifset
169
8db2ba40
SK
170@comment
171@comment When you add a new menu item, please keep the right hand
172@comment aligned to the same column. Do not use tabs. This provides
173@comment better formatting.
174@comment
6de9cd9a 175@menu
e6b38f67 176* Introduction::
c8cf50e4 177
e6b38f67 178Part I: Invoking GNU Fortran
c8cf50e4 179* Invoking GNU Fortran:: Command options supported by @command{gfortran}.
eaa90d25 180* Runtime:: Influencing runtime behavior with environment variables.
c8cf50e4 181
e6b38f67 182Part II: Language Reference
8179b067 183* Fortran standards status:: Fortran 2003, 2008 and 2018 features supported by GNU Fortran.
927f4842 184* Compiler Characteristics:: User-visible implementation details.
58edd811 185* Extensions:: Language extensions implemented by GNU Fortran.
9e0667cd 186* Mixed-Language Programming:: Interoperability with C
c194537c 187* Coarray Programming::
7fc15ba5 188* Intrinsic Procedures:: Intrinsic procedures supported by GNU Fortran.
dcf6c255 189* Intrinsic Modules:: Intrinsic modules supported by GNU Fortran.
c8cf50e4
BM
190
191* Contributing:: How you can help.
a63dad5b
TS
192* Copying:: GNU General Public License says
193 how you can copy and share GNU Fortran.
194* GNU Free Documentation License::
6ccde948 195 How you can copy and share this manual.
a63dad5b 196* Funding:: How to help assure continued work for free software.
32864778
DF
197* Option Index:: Index of command line options
198* Keyword Index:: Index of concepts
6de9cd9a 199@end menu
e6b38f67 200@end ifnottex
6de9cd9a 201
6de9cd9a 202@c ---------------------------------------------------------------------
e6b38f67 203@c Introduction
6de9cd9a
DN
204@c ---------------------------------------------------------------------
205
e6b38f67
BM
206@node Introduction
207@chapter Introduction
208
209@c The following duplicates the text on the TexInfo table of contents.
210@iftex
211This manual documents the use of @command{gfortran}, the GNU Fortran
3994c6b1 212compiler. You can find in this manual how to invoke @command{gfortran},
e6b38f67
BM
213as well as its features and incompatibilities.
214
215@ifset DEVELOPMENT
216@emph{Warning:} This document, and the compiler it describes, are still
217under development. While efforts are made to keep it up-to-date, it
218might not accurately reflect the status of the most recent GNU Fortran
219compiler.
220@end ifset
221@end iftex
6de9cd9a 222
7fc15ba5 223The GNU Fortran compiler front end was
6de9cd9a 224designed initially as a free replacement for,
3e508131 225or alternative to, the Unix @command{f95} command;
c5a0818e 226@command{gfortran} is the command you will use to invoke the compiler.
6de9cd9a 227
e6b38f67
BM
228@menu
229* About GNU Fortran:: What you should know about the GNU Fortran compiler.
230* GNU Fortran and GCC:: You can compile Fortran, C, or other programs.
2b44ab8b 231* Preprocessing and conditional compilation:: The Fortran preprocessor
e6b38f67
BM
232* GNU Fortran and G77:: Why we chose to start from scratch.
233* Project Status:: Status of GNU Fortran, roadmap, proposed extensions.
6ccde948 234* Standards:: Standards supported by GNU Fortran.
e6b38f67
BM
235@end menu
236
237
238@c ---------------------------------------------------------------------
239@c About GNU Fortran
240@c ---------------------------------------------------------------------
241
242@node About GNU Fortran
243@section About GNU Fortran
244
9e0667cd 245The GNU Fortran compiler supports the Fortran 77, 90 and 95 standards
8179b067 246completely, parts of the Fortran 2003, 2008 and 2018 standards, and
3994c6b1 247several vendor extensions. The development goal is to provide the
9e0667cd 248following features:
6de9cd9a
DN
249
250@itemize @bullet
251@item
8179b067
JB
252Read a user's program, stored in a file and containing instructions
253written in Fortran 77, Fortran 90, Fortran 95, Fortran 2003, Fortran
2542008 or Fortran 2018. This file contains @dfn{source code}.
6de9cd9a
DN
255
256@item
257Translate the user's program into instructions a computer
258can carry out more quickly than it takes to translate the
259instructions in the first
260place. The result after compilation of a program is
261@dfn{machine code},
262code designed to be efficiently translated and processed
263by a machine such as your computer.
c5a0818e 264Humans usually are not as good writing machine code
6de9cd9a 265as they are at writing Fortran (or C++, Ada, or Java),
aad9c4f4 266because it is easy to make tiny mistakes writing machine code.
6de9cd9a
DN
267
268@item
269Provide the user with information about the reasons why
270the compiler is unable to create a binary from the source code.
271Usually this will be the case if the source code is flawed.
aad9c4f4 272The Fortran 90 standard requires that the compiler can point out
6de9cd9a
DN
273mistakes to the user.
274An incorrect usage of the language causes an @dfn{error message}.
275
276The compiler will also attempt to diagnose cases where the
277user's program contains a correct usage of the language,
278but instructs the computer to do something questionable.
279This kind of diagnostics message is called a @dfn{warning message}.
280
281@item
282Provide optional information about the translation passes
283from the source code to machine code.
284This can help a user of the compiler to find the cause of
285certain bugs which may not be obvious in the source code,
286but may be more easily found at a lower level compiler output.
287It also helps developers to find bugs in the compiler itself.
288
289@item
290Provide information in the generated machine code that can
291make it easier to find bugs in the program (using a debugging tool,
bc0229f9 292called a @dfn{debugger}, such as the GNU Debugger @command{gdb}).
6de9cd9a
DN
293
294@item
295Locate and gather machine code already generated to
296perform actions requested by statements in the user's program.
297This machine code is organized into @dfn{modules} and is located
bc0229f9 298and @dfn{linked} to the user program.
6de9cd9a
DN
299@end itemize
300
7fc15ba5 301The GNU Fortran compiler consists of several components:
6de9cd9a
DN
302
303@itemize @bullet
304@item
305A version of the @command{gcc} command
306(which also might be installed as the system's @command{cc} command)
307that also understands and accepts Fortran source code.
308The @command{gcc} command is the @dfn{driver} program for
309all the languages in the GNU Compiler Collection (GCC);
310With @command{gcc},
5724da63 311you can compile the source code of any language for
6de9cd9a
DN
312which a front end is available in GCC.
313
314@item
315The @command{gfortran} command itself,
316which also might be installed as the
317system's @command{f95} command.
318@command{gfortran} is just another driver program,
7fc15ba5 319but specifically for the Fortran compiler only.
6de9cd9a
DN
320The difference with @command{gcc} is that @command{gfortran}
321will automatically link the correct libraries to your program.
322
323@item
324A collection of run-time libraries.
5724da63 325These libraries contain the machine code needed to support
6de9cd9a
DN
326capabilities of the Fortran language that are not directly
327provided by the machine code generated by the
328@command{gfortran} compilation phase,
329such as intrinsic functions and subroutines,
330and routines for interaction with files and the operating system.
331@c and mechanisms to spawn,
332@c unleash and pause threads in parallelized code.
333
334@item
335The Fortran compiler itself, (@command{f951}).
7fc15ba5 336This is the GNU Fortran parser and code generator,
6de9cd9a
DN
337linked to and interfaced with the GCC backend library.
338@command{f951} ``translates'' the source code to
339assembler code. You would typically not use this
340program directly;
341instead, the @command{gcc} or @command{gfortran} driver
342programs will call it for you.
343@end itemize
344
345
6de9cd9a 346@c ---------------------------------------------------------------------
7fc15ba5 347@c GNU Fortran and GCC
6de9cd9a
DN
348@c ---------------------------------------------------------------------
349
7fc15ba5 350@node GNU Fortran and GCC
e6b38f67 351@section GNU Fortran and GCC
6de9cd9a 352@cindex GNU Compiler Collection
de43c613
BM
353@cindex GCC
354
355GNU Fortran is a part of GCC, the @dfn{GNU Compiler Collection}. GCC
356consists of a collection of front ends for various languages, which
357translate the source code into a language-independent form called
358@dfn{GENERIC}. This is then processed by a common middle end which
359provides optimization, and then passed to one of a collection of back
360ends which generate code for different computer architectures and
361operating systems.
362
363Functionally, this is implemented with a driver program (@command{gcc})
364which provides the command-line interface for the compiler. It calls
365the relevant compiler front-end program (e.g., @command{f951} for
366Fortran) for each file in the source code, and then calls the assembler
3994c6b1 367and linker as appropriate to produce the compiled output. In a copy of
de43c613 368GCC which has been compiled with Fortran language support enabled,
1200489c 369@command{gcc} will recognize files with @file{.f}, @file{.for}, @file{.ftn},
f489fba1 370@file{.f90}, @file{.f95}, @file{.f03} and @file{.f08} extensions as
3994c6b1 371Fortran source code, and compile it accordingly. A @command{gfortran}
f489fba1
FXC
372driver program is also provided, which is identical to @command{gcc}
373except that it automatically links the Fortran runtime libraries into the
374compiled program.
de43c613 375
48d5fab4
JD
376Source files with @file{.f}, @file{.for}, @file{.fpp}, @file{.ftn}, @file{.F},
377@file{.FOR}, @file{.FPP}, and @file{.FTN} extensions are treated as fixed form.
f489fba1
FXC
378Source files with @file{.f90}, @file{.f95}, @file{.f03}, @file{.f08},
379@file{.F90}, @file{.F95}, @file{.F03} and @file{.F08} extensions are
380treated as free form. The capitalized versions of either form are run
3994c6b1 381through preprocessing. Source files with the lower case @file{.fpp}
f489fba1 382extension are also run through preprocessing.
48d5fab4 383
de43c613
BM
384This manual specifically documents the Fortran front end, which handles
385the programming language's syntax and semantics. The aspects of GCC
386which relate to the optimization passes and the back-end code generation
387are documented in the GCC manual; see
388@ref{Top,,Introduction,gcc,Using the GNU Compiler Collection (GCC)}.
389The two manuals together provide a complete reference for the GNU
390Fortran compiler.
6de9cd9a
DN
391
392
2b44ab8b
TB
393@c ---------------------------------------------------------------------
394@c Preprocessing and conditional compilation
395@c ---------------------------------------------------------------------
396
397@node Preprocessing and conditional compilation
398@section Preprocessing and conditional compilation
399@cindex CPP
400@cindex FPP
401@cindex Conditional compilation
402@cindex Preprocessing
f34cf28d 403@cindex preprocessor, include file handling
2b44ab8b 404
48d5fab4
JD
405Many Fortran compilers including GNU Fortran allow passing the source code
406through a C preprocessor (CPP; sometimes also called the Fortran preprocessor,
3994c6b1
TB
407FPP) to allow for conditional compilation. In the case of GNU Fortran,
408this is the GNU C Preprocessor in the traditional mode. On systems with
2b44ab8b 409case-preserving file names, the preprocessor is automatically invoked if the
3994c6b1
TB
410filename extension is @file{.F}, @file{.FOR}, @file{.FTN}, @file{.fpp},
411@file{.FPP}, @file{.F90}, @file{.F95}, @file{.F03} or @file{.F08}. To manually
670637ee
DF
412invoke the preprocessor on any file, use @option{-cpp}, to disable
413preprocessing on files where the preprocessor is run automatically, use
414@option{-nocpp}.
2b44ab8b 415
f34cf28d 416If a preprocessed file includes another file with the Fortran @code{INCLUDE}
3994c6b1 417statement, the included file is not preprocessed. To preprocess included
f34cf28d
DF
418files, use the equivalent preprocessor statement @code{#include}.
419
420If GNU Fortran invokes the preprocessor, @code{__GFORTRAN__}
0b774bab 421is defined. The macros @code{__GNUC__}, @code{__GNUC_MINOR__} and
2b44ab8b 422@code{__GNUC_PATCHLEVEL__} can be used to determine the version of the
3994c6b1 423compiler. See @ref{Top,,Overview,cpp,The C Preprocessor} for details.
2b44ab8b 424
0b774bab
SK
425GNU Fortran supports a number of @code{INTEGER} and @code{REAL} kind types
426in additional to the kind types required by the Fortran standard.
427The availability of any given kind type is architecture dependent. The
428following pre-defined preprocessor macros can be used to conditional
429include code for these additional kind types: @code{__GFC_INTEGER_1__},
430@code{__GFC_INTEGER_2__}, @code{__GFC_INTEGER_8__}, @code{__GFC_INTEGER_16__},
431@code{__GFC_REAL_10__}, and @code{__GFC_REAL_16__}.
432
2b44ab8b
TB
433While CPP is the de-facto standard for preprocessing Fortran code,
434Part 3 of the Fortran 95 standard (ISO/IEC 1539-3:1998) defines
435Conditional Compilation, which is not widely used and not directly
3994c6b1 436supported by the GNU Fortran compiler. You can use the program coco
f39168b3 437to preprocess such files (@uref{http://www.daniellnagle.com/coco.html}).
2b44ab8b
TB
438
439
6de9cd9a 440@c ---------------------------------------------------------------------
7fc15ba5 441@c GNU Fortran and G77
6de9cd9a
DN
442@c ---------------------------------------------------------------------
443
7fc15ba5 444@node GNU Fortran and G77
e6b38f67 445@section GNU Fortran and G77
6de9cd9a 446@cindex Fortran 77
e739dfac 447@cindex @command{g77}
6de9cd9a 448
e739dfac
DF
449The GNU Fortran compiler is the successor to @command{g77}, the Fortran
45077 front end included in GCC prior to version 4. It is an entirely new
451program that has been designed to provide Fortran 95 support and
452extensibility for future Fortran language standards, as well as providing
453backwards compatibility for Fortran 77 and nearly all of the GNU language
454extensions supported by @command{g77}.
6de9cd9a 455
6de9cd9a 456
6de9cd9a
DN
457@c ---------------------------------------------------------------------
458@c Project Status
459@c ---------------------------------------------------------------------
460
461@node Project Status
e6b38f67 462@section Project Status
6de9cd9a
DN
463
464@quotation
7fc15ba5 465As soon as @command{gfortran} can parse all of the statements correctly,
6de9cd9a
DN
466it will be in the ``larva'' state.
467When we generate code, the ``puppa'' state.
7fc15ba5 468When @command{gfortran} is done,
6de9cd9a
DN
469we'll see if it will be a beautiful butterfly,
470or just a big bug....
471
472--Andy Vaught, April 2000
473@end quotation
474
475The start of the GNU Fortran 95 project was announced on
476the GCC homepage in March 18, 2000
477(even though Andy had already been working on it for a while,
5724da63 478of course).
6de9cd9a 479
cf822c04
BM
480The GNU Fortran compiler is able to compile nearly all
481standard-compliant Fortran 95, Fortran 90, and Fortran 77 programs,
482including a number of standard and non-standard extensions, and can be
483used on real-world programs. In particular, the supported extensions
f6288c24
FR
484include OpenMP, Cray-style pointers, some old vendor extensions, and several
485Fortran 2003 and Fortran 2008 features, including TR 15581. However, it is
486still under development and has a few remaining rough edges.
41dbbb37
TS
487There also is initial support for OpenACC.
488Note that this is an experimental feature, incomplete, and subject to
489change in future versions of GCC. See
490@uref{https://gcc.gnu.org/wiki/OpenACC} for more information.
cf822c04
BM
491
492At present, the GNU Fortran compiler passes the
493@uref{http://www.fortran-2000.com/ArnaudRecipes/fcvs21_f95.html,
494NIST Fortran 77 Test Suite}, and produces acceptable results on the
495@uref{http://www.netlib.org/lapack/faq.html#1.21, LAPACK Test Suite}.
496It also provides respectable performance on
2bf716a9
TB
497the @uref{http://www.polyhedron.com/fortran-compiler-comparisons/polyhedron-benchmark-suite,
498Polyhedron Fortran
cf822c04 499compiler benchmarks} and the
2bf716a9 500@uref{http://www.netlib.org/benchmark/livermore,
cf822c04
BM
501Livermore Fortran Kernels test}. It has been used to compile a number of
502large real-world programs, including
b4fb1c21 503@uref{http://hirlam.org/, the HARMONIE and HIRLAM weather forecasting code} and
2bf716a9
TB
504@uref{http://physical-chemistry.scb.uwa.edu.au/tonto/wiki/index.php/Main_Page,
505the Tonto quantum chemistry package}; see
506@url{https://gcc.gnu.org/@/wiki/@/GfortranApps} for an extended list.
cf822c04
BM
507
508Among other things, the GNU Fortran compiler is intended as a replacement
509for G77. At this point, nearly all programs that could be compiled with
510G77 can be compiled with GNU Fortran, although there are a few minor known
511regressions.
512
513The primary work remaining to be done on GNU Fortran falls into three
8179b067
JB
514categories: bug fixing (primarily regarding the treatment of invalid
515code and providing useful error messages), improving the compiler
516optimizations and the performance of compiled code, and extending the
517compiler to support future standards---in particular, Fortran 2003,
518Fortran 2008 and Fortran 2018.
cf822c04 519
6de9cd9a 520
c8cf50e4
BM
521@c ---------------------------------------------------------------------
522@c Standards
523@c ---------------------------------------------------------------------
6de9cd9a 524
c8cf50e4 525@node Standards
e6b38f67 526@section Standards
c8cf50e4 527@cindex Standards
6de9cd9a 528
9e0667cd
TB
529@menu
530* Varying Length Character Strings::
531@end menu
532
c8cf50e4
BM
533The GNU Fortran compiler implements
534ISO/IEC 1539:1997 (Fortran 95). As such, it can also compile essentially all
535standard-compliant Fortran 90 and Fortran 77 programs. It also supports
3994c6b1 536the ISO/IEC TR-15581 enhancements to allocatable arrays.
6de9cd9a 537
8179b067
JB
538GNU Fortran also have a partial support for ISO/IEC 1539-1:2004
539(Fortran 2003), ISO/IEC 1539-1:2010 (Fortran 2008), the Technical
540Specification @code{Further Interoperability of Fortran with C}
541(ISO/IEC TS 29113:2012). Full support of those standards and future
542Fortran standards is planned. The current status of the support is
543can be found in the @ref{Fortran 2003 status}, @ref{Fortran 2008
286f737c 544status} and @ref{Fortran 2018 status} sections of the documentation.
6de9cd9a 545
9e0667cd 546Additionally, the GNU Fortran compilers supports the OpenMP specification
8ebd1b31
JJ
547(version 4.0 and most of the features of the 4.5 version,
548@url{http://openmp.org/@/wp/@/openmp-specifications/}).
41dbbb37
TS
549There also is initial support for the OpenACC specification (targeting
550version 2.0, @uref{http://www.openacc.org/}).
551Note that this is an experimental feature, incomplete, and subject to
552change in future versions of GCC. See
553@uref{https://gcc.gnu.org/wiki/OpenACC} for more information.
9e0667cd
TB
554
555@node Varying Length Character Strings
556@subsection Varying Length Character Strings
557@cindex Varying length character strings
558@cindex Varying length strings
559@cindex strings, varying length
560
561The Fortran 95 standard specifies in Part 2 (ISO/IEC 1539-2:2000)
3994c6b1 562varying length character strings. While GNU Fortran currently does not
9e0667cd 563support such strings directly, there exist two Fortran implementations
3994c6b1 564for them, which work with GNU Fortran. They can be found at
9e0667cd
TB
565@uref{http://www.fortran.com/@/iso_varying_string.f95} and at
566@uref{ftp://ftp.nag.co.uk/@/sc22wg5/@/ISO_VARYING_STRING/}.
567
3e508131
TB
568Deferred-length character strings of Fortran 2003 supports part of
569the features of @code{ISO_VARYING_STRING} and should be considered as
570replacement. (Namely, allocatable or pointers of the type
571@code{character(len=:)}.)
9e0667cd 572
6de9cd9a 573
c8cf50e4 574@c =====================================================================
e6b38f67 575@c PART I: INVOCATION REFERENCE
c8cf50e4 576@c =====================================================================
6de9cd9a 577
c8cf50e4 578@tex
e6b38f67 579\part{I}{Invoking GNU Fortran}
c8cf50e4 580@end tex
6de9cd9a 581
c8cf50e4
BM
582@c ---------------------------------------------------------------------
583@c Compiler Options
584@c ---------------------------------------------------------------------
6de9cd9a 585
c8cf50e4 586@include invoke.texi
6de9cd9a 587
cf822c04
BM
588
589@c ---------------------------------------------------------------------
590@c Runtime
591@c ---------------------------------------------------------------------
592
eaa90d25
TK
593@node Runtime
594@chapter Runtime: Influencing runtime behavior with environment variables
e739dfac 595@cindex environment variable
eaa90d25 596
b82feea5 597The behavior of the @command{gfortran} can be influenced by
eaa90d25 598environment variables.
f5dc42bb
TK
599
600Malformed environment variables are silently ignored.
601
eaa90d25 602@menu
68ee9c08 603* TMPDIR:: Directory for scratch files
f5dc42bb
TK
604* GFORTRAN_STDIN_UNIT:: Unit number for standard input
605* GFORTRAN_STDOUT_UNIT:: Unit number for standard output
606* GFORTRAN_STDERR_UNIT:: Unit number for standard error
c5a0818e
FXC
607* GFORTRAN_UNBUFFERED_ALL:: Do not buffer I/O for all units.
608* GFORTRAN_UNBUFFERED_PRECONNECTED:: Do not buffer I/O for preconnected units.
f5dc42bb
TK
609* GFORTRAN_SHOW_LOCUS:: Show location for runtime errors
610* GFORTRAN_OPTIONAL_PLUS:: Print leading + where permitted
f5dc42bb 611* GFORTRAN_LIST_SEPARATOR:: Separator for list output
eaa90d25 612* GFORTRAN_CONVERT_UNIT:: Set endianness for unformatted I/O
a0cb58b2 613* GFORTRAN_ERROR_BACKTRACE:: Show backtrace on run-time errors
eaa90d25
TK
614@end menu
615
68ee9c08
JB
616@node TMPDIR
617@section @env{TMPDIR}---Directory for scratch files
618
619When opening a file with @code{STATUS='SCRATCH'}, GNU Fortran tries to
620create the file in one of the potential directories by testing each
621directory in the order below.
622
623@enumerate
624@item
625The environment variable @env{TMPDIR}, if it exists.
626
627@item
628On the MinGW target, the directory returned by the @code{GetTempPath}
629function. Alternatively, on the Cygwin target, the @env{TMP} and
630@env{TEMP} environment variables, if they exist, in that order.
631
632@item
633The @code{P_tmpdir} macro if it is defined, otherwise the directory
634@file{/tmp}.
635@end enumerate
636
f5dc42bb
TK
637@node GFORTRAN_STDIN_UNIT
638@section @env{GFORTRAN_STDIN_UNIT}---Unit number for standard input
639
640This environment variable can be used to select the unit number
641preconnected to standard input. This must be a positive integer.
642The default value is 5.
643
644@node GFORTRAN_STDOUT_UNIT
645@section @env{GFORTRAN_STDOUT_UNIT}---Unit number for standard output
646
647This environment variable can be used to select the unit number
648preconnected to standard output. This must be a positive integer.
649The default value is 6.
650
651@node GFORTRAN_STDERR_UNIT
652@section @env{GFORTRAN_STDERR_UNIT}---Unit number for standard error
653
654This environment variable can be used to select the unit number
655preconnected to standard error. This must be a positive integer.
656The default value is 0.
657
f5dc42bb 658@node GFORTRAN_UNBUFFERED_ALL
c5a0818e 659@section @env{GFORTRAN_UNBUFFERED_ALL}---Do not buffer I/O on all units
f5dc42bb 660
6e34d7b8
JB
661This environment variable controls whether all I/O is unbuffered. If
662the first letter is @samp{y}, @samp{Y} or @samp{1}, all I/O is
3994c6b1 663unbuffered. This will slow down small sequential reads and writes. If
6e34d7b8
JB
664the first letter is @samp{n}, @samp{N} or @samp{0}, I/O is buffered.
665This is the default.
f5dc42bb 666
f41899f6 667@node GFORTRAN_UNBUFFERED_PRECONNECTED
c5a0818e 668@section @env{GFORTRAN_UNBUFFERED_PRECONNECTED}---Do not buffer I/O on preconnected units
f41899f6
JD
669
670The environment variable named @env{GFORTRAN_UNBUFFERED_PRECONNECTED} controls
24219f12 671whether I/O on a preconnected unit (i.e.@: STDOUT or STDERR) is unbuffered. If
3994c6b1 672the first letter is @samp{y}, @samp{Y} or @samp{1}, I/O is unbuffered. This
f41899f6
JD
673will slow down small sequential reads and writes. If the first letter
674is @samp{n}, @samp{N} or @samp{0}, I/O is buffered. This is the default.
675
f5dc42bb
TK
676@node GFORTRAN_SHOW_LOCUS
677@section @env{GFORTRAN_SHOW_LOCUS}---Show location for runtime errors
678
679If the first letter is @samp{y}, @samp{Y} or @samp{1}, filename and
680line numbers for runtime errors are printed. If the first letter is
c5a0818e 681@samp{n}, @samp{N} or @samp{0}, do not print filename and line numbers
3994c6b1 682for runtime errors. The default is to print the location.
f5dc42bb
TK
683
684@node GFORTRAN_OPTIONAL_PLUS
685@section @env{GFORTRAN_OPTIONAL_PLUS}---Print leading + where permitted
686
687If the first letter is @samp{y}, @samp{Y} or @samp{1},
688a plus sign is printed
b82feea5 689where permitted by the Fortran standard. If the first letter
f5dc42bb 690is @samp{n}, @samp{N} or @samp{0}, a plus sign is not printed
3994c6b1 691in most cases. Default is not to print plus signs.
f5dc42bb 692
f5dc42bb
TK
693@node GFORTRAN_LIST_SEPARATOR
694@section @env{GFORTRAN_LIST_SEPARATOR}---Separator for list output
695
696This environment variable specifies the separator when writing
697list-directed output. It may contain any number of spaces and
698at most one comma. If you specify this on the command line,
699be sure to quote spaces, as in
700@smallexample
701$ GFORTRAN_LIST_SEPARATOR=' , ' ./a.out
702@end smallexample
40746dcc 703when @command{a.out} is the compiled Fortran program that you want to run.
f5dc42bb
TK
704Default is a single space.
705
eaa90d25 706@node GFORTRAN_CONVERT_UNIT
f5dc42bb 707@section @env{GFORTRAN_CONVERT_UNIT}---Set endianness for unformatted I/O
eaa90d25 708
f5dc42bb 709By setting the @env{GFORTRAN_CONVERT_UNIT} variable, it is possible
eaa90d25 710to change the representation of data for unformatted files.
f5dc42bb 711The syntax for the @env{GFORTRAN_CONVERT_UNIT} variable is:
eaa90d25 712@smallexample
1941551a 713GFORTRAN_CONVERT_UNIT: mode | mode ';' exception | exception ;
eaa90d25
TK
714mode: 'native' | 'swap' | 'big_endian' | 'little_endian' ;
715exception: mode ':' unit_list | unit_list ;
716unit_list: unit_spec | unit_list unit_spec ;
717unit_spec: INTEGER | INTEGER '-' INTEGER ;
718@end smallexample
719The variable consists of an optional default mode, followed by
720a list of optional exceptions, which are separated by semicolons
721from the preceding default and each other. Each exception consists
722of a format and a comma-separated list of units. Valid values for
723the modes are the same as for the @code{CONVERT} specifier:
724
725@itemize @w{}
726@item @code{NATIVE} Use the native format. This is the default.
727@item @code{SWAP} Swap between little- and big-endian.
728@item @code{LITTLE_ENDIAN} Use the little-endian format
6ccde948 729for unformatted files.
eaa90d25
TK
730@item @code{BIG_ENDIAN} Use the big-endian format for unformatted files.
731@end itemize
732A missing mode for an exception is taken to mean @code{BIG_ENDIAN}.
40746dcc 733Examples of values for @env{GFORTRAN_CONVERT_UNIT} are:
eaa90d25
TK
734@itemize @w{}
735@item @code{'big_endian'} Do all unformatted I/O in big_endian mode.
736@item @code{'little_endian;native:10-20,25'} Do all unformatted I/O
737in little_endian mode, except for units 10 to 20 and 25, which are in
738native format.
739@item @code{'10-20'} Units 10 to 20 are big-endian, the rest is native.
6de9cd9a
DN
740@end itemize
741
eaa90d25 742Setting the environment variables should be done on the command
40746dcc
BM
743line or via the @command{export}
744command for @command{sh}-compatible shells and via @command{setenv}
745for @command{csh}-compatible shells.
eaa90d25 746
40746dcc 747Example for @command{sh}:
eaa90d25
TK
748@smallexample
749$ gfortran foo.f90
750$ GFORTRAN_CONVERT_UNIT='big_endian;native:10-20' ./a.out
751@end smallexample
752
40746dcc 753Example code for @command{csh}:
eaa90d25
TK
754@smallexample
755% gfortran foo.f90
756% setenv GFORTRAN_CONVERT_UNIT 'big_endian;native:10-20'
757% ./a.out
758@end smallexample
759
760Using anything but the native representation for unformatted data
761carries a significant speed overhead. If speed in this area matters
762to you, it is best if you use this only for data that needs to be
763portable.
764
765@xref{CONVERT specifier}, for an alternative way to specify the
766data representation for unformatted files. @xref{Runtime Options}, for
767setting a default data representation for the whole program. The
40746dcc 768@code{CONVERT} specifier overrides the @option{-fconvert} compile options.
eaa90d25 769
1941551a
TB
770@emph{Note that the values specified via the GFORTRAN_CONVERT_UNIT
771environment variable will override the CONVERT specifier in the
772open statement}. This is to give control over data formats to
773users who do not have the source code of their program available.
774
a0cb58b2
TB
775@node GFORTRAN_ERROR_BACKTRACE
776@section @env{GFORTRAN_ERROR_BACKTRACE}---Show backtrace on run-time errors
777
de8bd142
JB
778If the @env{GFORTRAN_ERROR_BACKTRACE} variable is set to @samp{y},
779@samp{Y} or @samp{1} (only the first letter is relevant) then a
780backtrace is printed when a serious run-time error occurs. To disable
781the backtracing, set the variable to @samp{n}, @samp{N}, @samp{0}.
782Default is to print a backtrace unless the @option{-fno-backtrace}
783compile option was used.
c8cf50e4
BM
784
785@c =====================================================================
e6b38f67 786@c PART II: LANGUAGE REFERENCE
c8cf50e4
BM
787@c =====================================================================
788
789@tex
e6b38f67 790\part{II}{Language Reference}
c8cf50e4
BM
791@end tex
792
793@c ---------------------------------------------------------------------
8179b067 794@c Fortran standards status
c8cf50e4
BM
795@c ---------------------------------------------------------------------
796
8179b067
JB
797@node Fortran standards status
798@chapter Fortran standards status
f489fba1
FXC
799
800@menu
801* Fortran 2003 status::
802* Fortran 2008 status::
8179b067 803* Fortran 2018 status::
f489fba1
FXC
804@end menu
805
c8cf50e4 806@node Fortran 2003 status
f489fba1 807@section Fortran 2003 status
c8cf50e4 808
9e0667cd 809GNU Fortran supports several Fortran 2003 features; an incomplete
26ef2b42 810list can be found below. See also the
2bf716a9 811@uref{https://gcc.gnu.org/wiki/Fortran2003, wiki page} about Fortran 2003.
c8cf50e4
BM
812
813@itemize
fc1e05d2
TB
814@item Procedure pointers including procedure-pointer components with
815@code{PASS} attribute.
c8cf50e4 816
fc1e05d2
TB
817@item Procedures which are bound to a derived type (type-bound procedures)
818including @code{PASS}, @code{PROCEDURE} and @code{GENERIC}, and
819operators bound to a type.
820
dd5a833e 821@item Abstract interfaces and type extension with the possibility to
fc1e05d2
TB
822override type-bound procedures or to have deferred binding.
823
80d6e13e
TB
824@item Polymorphic entities (``@code{CLASS}'') for derived types and unlimited
825polymorphism (``@code{CLASS(*)}'') -- including @code{SAME_TYPE_AS},
826@code{EXTENDS_TYPE_OF} and @code{SELECT TYPE} for scalars and arrays and
827finalization.
fabe41e4 828
7c5825a7 829@item Generic interface names, which have the same name as derived types,
fabe41e4
TB
830are now supported. This allows one to write constructor functions. Note
831that Fortran does not support static constructor functions. For static
832variables, only default initialization or structure-constructor
833initialization are available.
fc1e05d2
TB
834
835@item The @code{ASSOCIATE} construct.
836
837@item Interoperability with C including enumerations,
838
839@item In structure constructors the components with default values may be
840omitted.
841
842@item Extensions to the @code{ALLOCATE} statement, allowing for a
843type-specification with type parameter and for allocation and initialization
844from a @code{SOURCE=} expression; @code{ALLOCATE} and @code{DEALLOCATE}
845optionally return an error message string via @code{ERRMSG=}.
846
8d51f26f 847@item Reallocation on assignment: If an intrinsic assignment is
fc1e05d2 848used, an allocatable variable on the left-hand side is automatically allocated
8d51f26f
PT
849(if unallocated) or reallocated (if the shape is different). Currently, scalar
850deferred character length left-hand sides are correctly handled but arrays
851are not yet fully implemented.
fc1e05d2 852
80d6e13e
TB
853@item Deferred-length character variables and scalar deferred-length character
854components of derived types are supported. (Note that array-valued compoents
855are not yet implemented.)
856
fc1e05d2
TB
857@item Transferring of allocations via @code{MOVE_ALLOC}.
858
859@item The @code{PRIVATE} and @code{PUBLIC} attributes may be given individually
860to derived-type components.
861
862@item In pointer assignments, the lower bound may be specified and
863the remapping of elements is supported.
864
865@item For pointers an @code{INTENT} may be specified which affect the
866association status not the value of the pointer target.
867
868@item Intrinsics @code{command_argument_count}, @code{get_command},
869@code{get_command_argument}, and @code{get_environment_variable}.
870
8578f640 871@item Support for Unicode characters (ISO 10646) and UTF-8, including
fc1e05d2
TB
872the @code{SELECTED_CHAR_KIND} and @code{NEW_LINE} intrinsic functions.
873
874@item Support for binary, octal and hexadecimal (BOZ) constants in the
875intrinsic functions @code{INT}, @code{REAL}, @code{CMPLX} and @code{DBLE}.
876
5582f599
TB
877@item Support for namelist variables with allocatable and pointer
878attribute and nonconstant length type parameter.
879
fc1e05d2 880@item
e739dfac 881@cindex array, constructors
c8cf50e4 882@cindex @code{[...]}
3994c6b1 883Array constructors using square brackets. That is, @code{[...]} rather
26ef2b42
DK
884than @code{(/.../)}. Type-specification for array constructors like
885@code{(/ some-type :: ... /)}.
c8cf50e4 886
fc1e05d2
TB
887@item Extensions to the specification and initialization expressions,
888including the support for intrinsics with real and complex arguments.
889
2b4c9065 890@item Support for the asynchronous input/output.
fc1e05d2 891
c8cf50e4
BM
892@item
893@cindex @code{FLUSH} statement
e739dfac 894@cindex statement, @code{FLUSH}
c8cf50e4
BM
895@code{FLUSH} statement.
896
897@item
898@cindex @code{IOMSG=} specifier
899@code{IOMSG=} specifier for I/O statements.
900
901@item
902@cindex @code{ENUM} statement
903@cindex @code{ENUMERATOR} statement
e739dfac
DF
904@cindex statement, @code{ENUM}
905@cindex statement, @code{ENUMERATOR}
32864778 906@opindex @code{fshort-enums}
c8cf50e4
BM
907Support for the declaration of enumeration constants via the
908@code{ENUM} and @code{ENUMERATOR} statements. Interoperability with
909@command{gcc} is guaranteed also for the case where the
910@command{-fshort-enums} command line option is given.
911
912@item
913@cindex TR 15581
914TR 15581:
915@itemize
916@item
917@cindex @code{ALLOCATABLE} dummy arguments
918@code{ALLOCATABLE} dummy arguments.
919@item
920@cindex @code{ALLOCATABLE} function results
921@code{ALLOCATABLE} function results
922@item
923@cindex @code{ALLOCATABLE} components of derived types
924@code{ALLOCATABLE} components of derived types
925@end itemize
926
927@item
928@cindex @code{STREAM} I/O
929@cindex @code{ACCESS='STREAM'} I/O
930The @code{OPEN} statement supports the @code{ACCESS='STREAM'} specifier,
931allowing I/O without any record structure.
932
933@item
934Namelist input/output for internal files.
935
80d6e13e 936@item Minor I/O features: Rounding during formatted output, using of
fc1e05d2 937a decimal comma instead of a decimal point, setting whether a plus sign
0e29a400 938should appear for positive numbers. On systems where @code{strtod} honours
80d6e13e 939the rounding mode, the rounding mode is also supported for input.
fc1e05d2 940
c8cf50e4 941@item
e739dfac
DF
942@cindex @code{PROTECTED} statement
943@cindex statement, @code{PROTECTED}
c8cf50e4
BM
944The @code{PROTECTED} statement and attribute.
945
946@item
e739dfac
DF
947@cindex @code{VALUE} statement
948@cindex statement, @code{VALUE}
c8cf50e4
BM
949The @code{VALUE} statement and attribute.
950
951@item
e739dfac
DF
952@cindex @code{VOLATILE} statement
953@cindex statement, @code{VOLATILE}
c8cf50e4
BM
954The @code{VOLATILE} statement and attribute.
955
956@item
e739dfac
DF
957@cindex @code{IMPORT} statement
958@cindex statement, @code{IMPORT}
c8cf50e4
BM
959The @code{IMPORT} statement, allowing to import
960host-associated derived types.
961
fc1e05d2
TB
962@item The intrinsic modules @code{ISO_FORTRAN_ENVIRONMENT} is supported,
963which contains parameters of the I/O units, storage sizes. Additionally,
964procedures for C interoperability are available in the @code{ISO_C_BINDING}
965module.
966
c8cf50e4 967@item
e739dfac
DF
968@cindex @code{USE, INTRINSIC} statement
969@cindex statement, @code{USE, INTRINSIC}
970@cindex @code{ISO_FORTRAN_ENV} statement
971@cindex statement, @code{ISO_FORTRAN_ENV}
c8cf50e4
BM
972@code{USE} statement with @code{INTRINSIC} and @code{NON_INTRINSIC}
973attribute; supported intrinsic modules: @code{ISO_FORTRAN_ENV},
41dbbb37
TS
974@code{ISO_C_BINDING}, @code{OMP_LIB} and @code{OMP_LIB_KINDS},
975and @code{OPENACC}.
c8cf50e4 976
56bedf42
TB
977@item
978Renaming of operators in the @code{USE} statement.
979
c8cf50e4
BM
980@end itemize
981
982
f489fba1
FXC
983@node Fortran 2008 status
984@section Fortran 2008 status
985
3994c6b1
TB
986The latest version of the Fortran standard is ISO/IEC 1539-1:2010, informally
987known as Fortran 2008. The official version is available from International
988Organization for Standardization (ISO) or its national member organizations.
989The the final draft (FDIS) can be downloaded free of charge from
990@url{http://www.nag.co.uk/@/sc22wg5/@/links.html}. Fortran is developed by the
991Working Group 5 of Sub-Committee 22 of the Joint Technical Committee 1 of the
992International Organization for Standardization and the International
993Electrotechnical Commission (IEC). This group is known as
994@uref{http://www.nag.co.uk/sc22wg5/, WG5}.
995
20c97ec9 996The GNU Fortran compiler supports several of the new features of Fortran 2008;
2bf716a9 997the @uref{https://gcc.gnu.org/wiki/Fortran2008Status, wiki} has some information
3994c6b1
TB
998about the current Fortran 2008 implementation status. In particular, the
999following is implemented.
1000
1001@itemize
1002@item The @option{-std=f2008} option and support for the file extensions
1003@file{.f08} and @file{.F08}.
1004
1005@item The @code{OPEN} statement now supports the @code{NEWUNIT=} option,
1006which returns a unique file unit, thus preventing inadvertent use of the
1007same unit in different parts of the program.
1008
1009@item The @code{g0} format descriptor and unlimited format items.
1010
1011@item The mathematical intrinsics @code{ASINH}, @code{ACOSH}, @code{ATANH},
1012@code{ERF}, @code{ERFC}, @code{GAMMA}, @code{LOG_GAMMA}, @code{BESSEL_J0},
1013@code{BESSEL_J1}, @code{BESSEL_JN}, @code{BESSEL_Y0}, @code{BESSEL_Y1},
1014@code{BESSEL_YN}, @code{HYPOT}, @code{NORM2}, and @code{ERFC_SCALED}.
1015
1016@item Using complex arguments with @code{TAN}, @code{SINH}, @code{COSH},
1017@code{TANH}, @code{ASIN}, @code{ACOS}, and @code{ATAN} is now possible;
1018@code{ATAN}(@var{Y},@var{X}) is now an alias for @code{ATAN2}(@var{Y},@var{X}).
1019
1020@item Support of the @code{PARITY} intrinsic functions.
1021
1022@item The following bit intrinsics: @code{LEADZ} and @code{TRAILZ} for
1023counting the number of leading and trailing zero bits, @code{POPCNT} and
1024@code{POPPAR} for counting the number of one bits and returning the parity;
1025@code{BGE}, @code{BGT}, @code{BLE}, and @code{BLT} for bitwise comparisons;
1026@code{DSHIFTL} and @code{DSHIFTR} for combined left and right shifts,
1027@code{MASKL} and @code{MASKR} for simple left and right justified masks,
1028@code{MERGE_BITS} for a bitwise merge using a mask, @code{SHIFTA},
1029@code{SHIFTL} and @code{SHIFTR} for shift operations, and the
1030transformational bit intrinsics @code{IALL}, @code{IANY} and @code{IPARITY}.
1031
1032@item Support of the @code{EXECUTE_COMMAND_LINE} intrinsic subroutine.
1033
1034@item Support for the @code{STORAGE_SIZE} intrinsic inquiry function.
1035
1036@item The @code{INT@{8,16,32@}} and @code{REAL@{32,64,128@}} kind type
e9853e1c
TB
1037parameters and the array-valued named constants @code{INTEGER_KINDS},
1038@code{LOGICAL_KINDS}, @code{REAL_KINDS} and @code{CHARACTER_KINDS} of
1039the intrinsic module @code{ISO_FORTRAN_ENV}.
1040
1041@item The module procedures @code{C_SIZEOF} of the intrinsic module
1042@code{ISO_C_BINDINGS} and @code{COMPILER_VERSION} and @code{COMPILER_OPTIONS}
1043of @code{ISO_FORTRAN_ENV}.
3994c6b1 1044
4650947d
TB
1045@item Coarray support for serial programs with @option{-fcoarray=single} flag
1046and experimental support for multiple images with the @option{-fcoarray=lib}
1047flag.
1048
e3b5d7ba
PT
1049@item Submodules are supported. It should noted that @code{MODULEs} do not
1050produce the smod file needed by the descendent @code{SUBMODULEs} unless they
1051contain at least one @code{MODULE PROCEDURE} interface. The reason for this is
1052that @code{SUBMODULEs} are useless without @code{MODULE PROCEDUREs}. See
1053http://j3-fortran.org/doc/meeting/207/15-209.txt for a discussion and a draft
1054interpretation. Adopting this interpretation has the advantage that code that
1055does not use submodules does not generate smod files.
1056
4650947d 1057@item The @code{DO CONCURRENT} construct is supported.
3994c6b1
TB
1058
1059@item The @code{BLOCK} construct is supported.
1060
1061@item The @code{STOP} and the new @code{ERROR STOP} statements now
80d6e13e
TB
1062support all constant expressions. Both show the signals which were signaling
1063at termination.
3994c6b1
TB
1064
1065@item Support for the @code{CONTIGUOUS} attribute.
1066
1067@item Support for @code{ALLOCATE} with @code{MOLD}.
1068
1069@item Support for the @code{IMPURE} attribute for procedures, which
1070allows for @code{ELEMENTAL} procedures without the restrictions of
1071@code{PURE}.
1072
1073@item Null pointers (including @code{NULL()}) and not-allocated variables
1074can be used as actual argument to optional non-pointer, non-allocatable
1075dummy arguments, denoting an absent argument.
1076
1077@item Non-pointer variables with @code{TARGET} attribute can be used as
1078actual argument to @code{POINTER} dummies with @code{INTENT(IN)}.
1079
1080@item Pointers including procedure pointers and those in a derived
1081type (pointer components) can now be initialized by a target instead
1082of only by @code{NULL}.
1083
1084@item The @code{EXIT} statement (with construct-name) can be now be
1085used to leave not only the @code{DO} but also the @code{ASSOCIATE},
1086@code{BLOCK}, @code{IF}, @code{SELECT CASE} and @code{SELECT TYPE}
1087constructs.
1088
1089@item Internal procedures can now be used as actual argument.
1090
1091@item Minor features: obsolesce diagnostics for @code{ENTRY} with
1092@option{-std=f2008}; a line may start with a semicolon; for internal
1093and module procedures @code{END} can be used instead of
1094@code{END SUBROUTINE} and @code{END FUNCTION}; @code{SELECTED_REAL_KIND}
1095now also takes a @code{RADIX} argument; intrinsic types are supported
1096for @code{TYPE}(@var{intrinsic-type-spec}); multiple type-bound procedures
1097can be declared in a single @code{PROCEDURE} statement; implied-shape
1098arrays are supported for named constants (@code{PARAMETER}).
1099@end itemize
f489fba1 1100
26ef2b42 1101
71810d0e 1102
286f737c
JW
1103@node Fortran 2018 status
1104@section Status of Fortran 2018 support
1105
1106So far very little work has been done to support Fortran 2018.
1107
1108@itemize
1109@item ERROR STOP in a PURE procedure
1110An @code{ERROR STOP} statement is permitted in a @code{PURE}
1111procedure.
1112
1113@item IMPLICIT NONE with a spec-list
1114Support the @code{IMPLICIT NONE} statement with an
1115@code{implicit-none-spec-list}.
1116
1117@item Behavior of INQUIRE with the RECL= specifier
1118
1119The behavior of the @code{INQUIRE} statement with the @code{RECL=}
1120specifier now conforms to Fortran 2018.
1121
1122@end itemize
1123
1124
1125@subsection TS 29113 Status (Further Interoperability with C)
20c97ec9 1126
4650947d
TB
1127GNU Fortran supports some of the new features of the Technical
1128Specification (TS) 29113 on Further Interoperability of Fortran with C.
2bf716a9 1129The @uref{https://gcc.gnu.org/wiki/TS29113Status, wiki} has some information
4650947d 1130about the current TS 29113 implementation status. In particular, the
20c97ec9
TB
1131following is implemented.
1132
3e508131
TB
1133See also @ref{Further Interoperability of Fortran with C}.
1134
20c97ec9 1135@itemize
4650947d
TB
1136@item The @code{OPTIONAL} attribute is allowed for dummy arguments
1137of @code{BIND(C) procedures.}
1138
3e508131 1139@item The @code{RANK} intrinsic is supported.
4650947d
TB
1140
1141@item GNU Fortran's implementation for variables with @code{ASYNCHRONOUS}
1142attribute is compatible with TS 29113.
3e508131 1143
ef78bc3c 1144@item Assumed types (@code{TYPE(*)}).
3e508131
TB
1145
1146@item Assumed-rank (@code{DIMENSION(..)}). However, the array descriptor
1147of the TS is not yet supported.
20c97ec9
TB
1148@end itemize
1149
1150
286f737c
JW
1151
1152@subsection TS 18508 Status (Additional Parallel Features)
1cb62d16
TB
1153
1154GNU Fortran supports the following new features of the Technical
1155Specification 18508 on Additional Parallel Features in Fortran:
1156
1157@itemize
1158@item The new atomic ADD, CAS, FETCH and ADD/OR/XOR, OR and XOR intrinsics.
1159
1160@item The @code{CO_MIN} and @code{CO_MAX} and @code{SUM} reduction intrinsics.
1161And the @code{CO_BROADCAST} and @code{CO_REDUCE} intrinsic, except that those
1162do not support polymorphic types or types with allocatable, pointer or
1163polymorphic components.
1164
1165@item Events (@code{EVENT POST}, @code{EVENT WAIT}, @code{EVENT_QUERY})
ef78bc3c
AV
1166
1167@item Failed images (@code{FAIL IMAGE}, @code{IMAGE_STATUS},
1168@code{FAILED_IMAGES}, @code{STOPPED_IMAGES})
1169
1cb62d16
TB
1170@end itemize
1171
20c97ec9 1172
71810d0e
DK
1173@c ---------------------------------------------------------------------
1174@c Compiler Characteristics
1175@c ---------------------------------------------------------------------
1176
1177@node Compiler Characteristics
1178@chapter Compiler Characteristics
1179
927f4842
JB
1180This chapter describes certain characteristics of the GNU Fortran
1181compiler, that are not specified by the Fortran standard, but which
1182might in some way or another become visible to the programmer.
71810d0e
DK
1183
1184@menu
1185* KIND Type Parameters::
927f4842 1186* Internal representation of LOGICAL variables::
6457b1f0 1187* Evaluation of logical expressions::
fa3d2d38 1188* MAX and MIN intrinsics with REAL NaN arguments::
85883d65 1189* Thread-safety of the runtime library::
ed10039e 1190* Data consistency and durability::
6234b543 1191* Files opened without an explicit ACTION= specifier::
9f801fd7 1192* File operations on symbolic links::
04c4bb30 1193* File format of unformatted sequential files::
2b4c9065 1194* Asynchronous I/O::
71810d0e
DK
1195@end menu
1196
1197
1198@node KIND Type Parameters
1199@section KIND Type Parameters
1200@cindex kind
1201
1202The @code{KIND} type parameters supported by GNU Fortran for the primitive
1203data types are:
1204
1205@table @code
1206
1207@item INTEGER
a4cf752c 12081, 2, 4, 8*, 16*, default: 4**
71810d0e
DK
1209
1210@item LOGICAL
a4cf752c 12111, 2, 4, 8*, 16*, default: 4**
71810d0e
DK
1212
1213@item REAL
a4cf752c 12144, 8, 10*, 16*, default: 4***
71810d0e
DK
1215
1216@item COMPLEX
a4cf752c 12174, 8, 10*, 16*, default: 4***
71810d0e 1218
8f606521
JW
1219@item DOUBLE PRECISION
12204, 8, 10*, 16*, default: 8***
1221
71810d0e
DK
1222@item CHARACTER
12231, 4, default: 1
1224
1225@end table
1226
1227@noindent
a4cf752c
JW
1228* not available on all systems @*
1229** unless @option{-fdefault-integer-8} is used @*
1230*** unless @option{-fdefault-real-8} is used (see @ref{Fortran Dialect Options})
71810d0e
DK
1231
1232@noindent
1233The @code{KIND} value matches the storage size in bytes, except for
1234@code{COMPLEX} where the storage size is twice as much (or both real and
1235imaginary part are a real value of the given size). It is recommended to use
a4cf752c
JW
1236the @ref{SELECTED_CHAR_KIND}, @ref{SELECTED_INT_KIND} and
1237@ref{SELECTED_REAL_KIND} intrinsics or the @code{INT8}, @code{INT16},
6a683de1
TB
1238@code{INT32}, @code{INT64}, @code{REAL32}, @code{REAL64}, and @code{REAL128}
1239parameters of the @code{ISO_FORTRAN_ENV} module instead of the concrete values.
1240The available kind parameters can be found in the constant arrays
1241@code{CHARACTER_KINDS}, @code{INTEGER_KINDS}, @code{LOGICAL_KINDS} and
a4cf752c
JW
1242@code{REAL_KINDS} in the @ref{ISO_FORTRAN_ENV} module. For C interoperability,
1243the kind parameters of the @ref{ISO_C_BINDING} module should be used.
71810d0e
DK
1244
1245
927f4842
JB
1246@node Internal representation of LOGICAL variables
1247@section Internal representation of LOGICAL variables
1248@cindex logical, variable representation
1249
1250The Fortran standard does not specify how variables of @code{LOGICAL}
1251type are represented, beyond requiring that @code{LOGICAL} variables
1252of default kind have the same storage size as default @code{INTEGER}
1253and @code{REAL} variables. The GNU Fortran internal representation is
1254as follows.
1255
1256A @code{LOGICAL(KIND=N)} variable is represented as an
1257@code{INTEGER(KIND=N)} variable, however, with only two permissible
1258values: @code{1} for @code{.TRUE.} and @code{0} for
3994c6b1 1259@code{.FALSE.}. Any other integer value results in undefined behavior.
927f4842 1260
83e03963 1261See also @ref{Argument passing conventions} and @ref{Interoperability with C}.
927f4842 1262
6985b4a1 1263
6457b1f0
JW
1264@node Evaluation of logical expressions
1265@section Evaluation of logical expressions
1266
1267The Fortran standard does not require the compiler to evaluate all parts of an
1268expression, if they do not contribute to the final result. For logical
1269expressions with @code{.AND.} or @code{.OR.} operators, in particular, GNU
1270Fortran will optimize out function calls (even to impure functions) if the
1271result of the expression can be established without them. However, since not
1272all compilers do that, and such an optimization can potentially modify the
1273program flow and subsequent results, GNU Fortran throws warnings for such
1274situations with the @option{-Wfunction-elimination} flag.
1275
1276
fa3d2d38
JB
1277@node MAX and MIN intrinsics with REAL NaN arguments
1278@section MAX and MIN intrinsics with REAL NaN arguments
1279@cindex MAX, MIN, NaN
1280
1281The Fortran standard does not specify what the result of the
1282@code{MAX} and @code{MIN} intrinsics are if one of the arguments is a
1283@code{NaN}. Accordingly, the GNU Fortran compiler does not specify
1284that either, as this allows for faster and more compact code to be
1285generated. If the programmer wishes to take some specific action in
1286case one of the arguments is a @code{NaN}, it is necessary to
1287explicitly test the arguments before calling @code{MAX} or @code{MIN},
1288e.g. with the @code{IEEE_IS_NAN} function from the intrinsic module
1289@code{IEEE_ARITHMETIC}.
1290
1291
6985b4a1
JB
1292@node Thread-safety of the runtime library
1293@section Thread-safety of the runtime library
1294@cindex thread-safety, threads
1295
159c2794 1296GNU Fortran can be used in programs with multiple threads, e.g.@: by
6985b4a1
JB
1297using OpenMP, by calling OS thread handling functions via the
1298@code{ISO_C_BINDING} facility, or by GNU Fortran compiled library code
1299being called from a multi-threaded program.
1300
f3f2c465 1301The GNU Fortran runtime library, (@code{libgfortran}), supports being
6985b4a1
JB
1302called concurrently from multiple threads with the following
1303exceptions.
1304
f3f2c465
JB
1305During library initialization, the C @code{getenv} function is used,
1306which need not be thread-safe. Similarly, the @code{getenv}
6985b4a1
JB
1307function is used to implement the @code{GET_ENVIRONMENT_VARIABLE} and
1308@code{GETENV} intrinsics. It is the responsibility of the user to
1309ensure that the environment is not being updated concurrently when any
1310of these actions are taking place.
1311
1312The @code{EXECUTE_COMMAND_LINE} and @code{SYSTEM} intrinsics are
f3f2c465 1313implemented with the @code{system} function, which need not be
6985b4a1 1314thread-safe. It is the responsibility of the user to ensure that
f3f2c465 1315@code{system} is not called concurrently.
6985b4a1 1316
9cbecd06
JB
1317For platforms not supporting thread-safe POSIX functions, further
1318functionality might not be thread-safe. For details, please consult
1319the documentation for your operating system.
1320
1321The GNU Fortran runtime library uses various C library functions that
1322depend on the locale, such as @code{strtod} and @code{snprintf}. In
1323order to work correctly in locale-aware programs that set the locale
1324using @code{setlocale}, the locale is reset to the default ``C''
1325locale while executing a formatted @code{READ} or @code{WRITE}
1326statement. On targets supporting the POSIX 2008 per-thread locale
1327functions (e.g. @code{newlocale}, @code{uselocale},
1328@code{freelocale}), these are used and thus the global locale set
1329using @code{setlocale} or the per-thread locales in other threads are
1330not affected. However, on targets lacking this functionality, the
1331global LC_NUMERIC locale is set to ``C'' during the formatted I/O.
1332Thus, on such targets it's not safe to call @code{setlocale}
1333concurrently from another thread while a Fortran formatted I/O
1334operation is in progress. Also, other threads doing something
1335dependent on the LC_NUMERIC locale might not work correctly if a
1336formatted I/O operation is in progress in another thread.
ed10039e
JB
1337
1338@node Data consistency and durability
1339@section Data consistency and durability
1340@cindex consistency, durability
1341
1342This section contains a brief overview of data and metadata
1343consistency and durability issues when doing I/O.
1344
1345With respect to durability, GNU Fortran makes no effort to ensure that
1346data is committed to stable storage. If this is required, the GNU
1347Fortran programmer can use the intrinsic @code{FNUM} to retrieve the
1348low level file descriptor corresponding to an open Fortran unit. Then,
1349using e.g. the @code{ISO_C_BINDING} feature, one can call the
1350underlying system call to flush dirty data to stable storage, such as
1351@code{fsync} on POSIX, @code{_commit} on MingW, or @code{fcntl(fd,
1352F_FULLSYNC, 0)} on Mac OS X. The following example shows how to call
1353fsync:
1354
1355@smallexample
1356 ! Declare the interface for POSIX fsync function
1357 interface
1358 function fsync (fd) bind(c,name="fsync")
1359 use iso_c_binding, only: c_int
1360 integer(c_int), value :: fd
1361 integer(c_int) :: fsync
1362 end function fsync
1363 end interface
1364
1365 ! Variable declaration
1366 integer :: ret
1367
1368 ! Opening unit 10
1369 open (10,file="foo")
1370
1371 ! ...
1372 ! Perform I/O on unit 10
1373 ! ...
1374
1375 ! Flush and sync
1376 flush(10)
1377 ret = fsync(fnum(10))
1378
1379 ! Handle possible error
1380 if (ret /= 0) stop "Error calling FSYNC"
1381@end smallexample
1382
1383With respect to consistency, for regular files GNU Fortran uses
1384buffered I/O in order to improve performance. This buffer is flushed
1385automatically when full and in some other situations, e.g. when
1386closing a unit. It can also be explicitly flushed with the
1387@code{FLUSH} statement. Also, the buffering can be turned off with the
1388@code{GFORTRAN_UNBUFFERED_ALL} and
1389@code{GFORTRAN_UNBUFFERED_PRECONNECTED} environment variables. Special
1390files, such as terminals and pipes, are always unbuffered. Sometimes,
1391however, further things may need to be done in order to allow other
1392processes to see data that GNU Fortran has written, as follows.
1393
1394The Windows platform supports a relaxed metadata consistency model,
1395where file metadata is written to the directory lazily. This means
1396that, for instance, the @code{dir} command can show a stale size for a
1397file. One can force a directory metadata update by closing the unit,
1398or by calling @code{_commit} on the file descriptor. Note, though,
1399that @code{_commit} will force all dirty data to stable storage, which
1400is often a very slow operation.
1401
1402The Network File System (NFS) implements a relaxed consistency model
1403called open-to-close consistency. Closing a file forces dirty data and
1404metadata to be flushed to the server, and opening a file forces the
1405client to contact the server in order to revalidate cached
1406data. @code{fsync} will also force a flush of dirty data and metadata
1407to the server. Similar to @code{open} and @code{close}, acquiring and
1408releasing @code{fcntl} file locks, if the server supports them, will
1409also force cache validation and flushing dirty data and metadata.
1410
1411
6234b543
JB
1412@node Files opened without an explicit ACTION= specifier
1413@section Files opened without an explicit ACTION= specifier
1414@cindex open, action
1415
1416The Fortran standard says that if an @code{OPEN} statement is executed
1417without an explicit @code{ACTION=} specifier, the default value is
1418processor dependent. GNU Fortran behaves as follows:
1419
1420@enumerate
1421@item Attempt to open the file with @code{ACTION='READWRITE'}
1422@item If that fails, try to open with @code{ACTION='READ'}
1423@item If that fails, try to open with @code{ACTION='WRITE'}
1424@item If that fails, generate an error
1425@end enumerate
1426
1427
9f801fd7
FXC
1428@node File operations on symbolic links
1429@section File operations on symbolic links
1430@cindex file, symbolic link
1431
1432This section documents the behavior of GNU Fortran for file operations on
1433symbolic links, on systems that support them.
1434
1435@itemize
1436
1437@item Results of INQUIRE statements of the ``inquire by file'' form will
1438relate to the target of the symbolic link. For example,
1439@code{INQUIRE(FILE="foo",EXIST=ex)} will set @var{ex} to @var{.true.} if
1440@var{foo} is a symbolic link pointing to an existing file, and @var{.false.}
1441if @var{foo} points to an non-existing file (``dangling'' symbolic link).
1442
1443@item Using the @code{OPEN} statement with a @code{STATUS="NEW"} specifier
1444on a symbolic link will result in an error condition, whether the symbolic
1445link points to an existing target or is dangling.
1446
1447@item If a symbolic link was connected, using the @code{CLOSE} statement
1448with a @code{STATUS="DELETE"} specifier will cause the symbolic link itself
1449to be deleted, not its target.
1450
1451@end itemize
1452
04c4bb30
TK
1453@node File format of unformatted sequential files
1454@section File format of unformatted sequential files
1455@cindex file, unformatted sequential
1456@cindex unformatted sequential
1457@cindex sequential, unformatted
1458@cindex record marker
1459@cindex subrecord
1460
1461Unformatted sequential files are stored as logical records using
1462record markers. Each logical record consists of one of more
1463subrecords.
1464
1465Each subrecord consists of a leading record marker, the data written
1466by the user program, and a trailing record marker. The record markers
1467are four-byte integers by default, and eight-byte integers if the
1468@option{-fmax-subrecord-length=8} option (which exists for backwards
1469compability only) is in effect.
1470
1471The representation of the record markers is that of unformatted files
1472given with the @option{-fconvert} option, the @xref{CONVERT specifier}
1473on the open statement or the @xref{GFORTRAN_CONVERT_UNIT} environment
1474variable.
1475
1476The maximum number of bytes of user data in a subrecord is 2147483639
1477(2 GiB - 9) for a four-byte record marker. This limit can be lowered
1478with the @option{-fmax-subrecord-length} option, altough this is
1479rarely useful. If the length of a logical record exceeds this limit,
1480the data is distributed among several subrecords.
1481
1482The absolute of the number stored in the record markers is the number
1483of bytes of user data in the corresponding subrecord. If the leading
1484record marker of a subrecord contains a negative number, another
1485subrecord follows the current one. If the trailing record marker
1486contains a negative number, then there is a preceding subrecord.
1487
1488In the most simple case, with only one subrecord per logical record,
1489both record markers contain the number of bytes of user data in the
ff5115e2 1490record.
04c4bb30
TK
1491
1492The format for unformatted sequential data can be duplicated using
1493unformatted stream, as shown in the example program for an unformatted
1494record containing a single subrecord:
9f801fd7 1495
04c4bb30
TK
1496@smallexample
1497program main
1498 use iso_fortran_env, only: int32
1499 implicit none
1500 integer(int32) :: i
1501 real, dimension(10) :: a, b
1502 call random_number(a)
1503 open (10,file='test.dat',form='unformatted',access='stream')
1504 inquire (iolength=i) a
1505 write (10) i, a, i
1506 close (10)
1507 open (10,file='test.dat',form='unformatted')
1508 read (10) b
1509 if (all (a == b)) print *,'success!'
1510end program main
1511@end smallexample
9f801fd7 1512
2b4c9065
NK
1513@node Asynchronous I/O
1514@section Asynchronous I/O
1515@cindex input/output, asynchronous
1516@cindex asynchronous I/O
1517
1518Asynchronous I/O is supported if the program is linked against the
1519POSIX thread library. If that is not the case, all I/O is performed
2532f0f5
TK
1520as synchronous. On systems which do not support pthread condition
1521variables, such as AIX, I/O is also performed as synchronous.
2b4c9065
NK
1522
1523On some systems, such as Darwin or Solaris, the POSIX thread library
1524is always linked in, so asynchronous I/O is always performed. On other
1525sytems, such as Linux, it is necessary to specify @option{-pthread},
1526@option{-lpthread} or @option{-fopenmp} during the linking step.
1527
294fbfc8
TS
1528@c ---------------------------------------------------------------------
1529@c Extensions
1530@c ---------------------------------------------------------------------
1531
1532@c Maybe this chapter should be merged with the 'Standards' section,
1533@c whenever that is written :-)
1534
1535@node Extensions
1536@chapter Extensions
49309826
FXC
1537@cindex extensions
1538
1539The two sections below detail the extensions to standard Fortran that are
1540implemented in GNU Fortran, as well as some of the popular or
1541historically important extensions that are not (or not yet) implemented.
1542For the latter case, we explain the alternatives available to GNU Fortran
1543users, including replacement by standard-conforming code or GNU
1544extensions.
1545
1546@menu
1547* Extensions implemented in GNU Fortran::
1548* Extensions not implemented in GNU Fortran::
1549@end menu
1550
1551
1552@node Extensions implemented in GNU Fortran
1553@section Extensions implemented in GNU Fortran
1554@cindex extensions, implemented
294fbfc8 1555
8179b067
JB
1556GNU Fortran implements a number of extensions over standard Fortran.
1557This chapter contains information on their syntax and meaning. There
1558are currently two categories of GNU Fortran extensions, those that
1559provide functionality beyond that provided by any standard, and those
1560that are supported by GNU Fortran purely for backward compatibility
1561with legacy compilers. By default, @option{-std=gnu} allows the
1562compiler to accept both types of extensions, but to warn about the use
1563of the latter. Specifying either @option{-std=f95},
1564@option{-std=f2003}, @option{-std=f2008}, or @option{-std=f2018}
1565disables both types of extensions, and @option{-std=legacy} allows
1566both without warning. The special compile flag @option{-fdec} enables
1567additional compatibility extensions along with those enabled by
1568@option{-std=legacy}.
294fbfc8
TS
1569
1570@menu
1571* Old-style kind specifications::
1572* Old-style variable initialization::
670026fb 1573* Extensions to namelist::
11de78ff 1574* X format descriptor without count field::
ec8a1940 1575* Commas in FORMAT specifications::
c9f4aa97 1576* Missing period in FORMAT specifications::
ec8a1940 1577* I/O item lists::
5a17346a 1578* @code{Q} exponent-letter::
58edd811 1579* BOZ literal constants::
ec8a1940
RS
1580* Real array indices::
1581* Unary operators::
11de78ff 1582* Implicitly convert LOGICAL and INTEGER values::
bc192c77 1583* Hollerith constants support::
83d890b9 1584* Cray pointers::
181c9f4a 1585* CONVERT specifier::
6c7a4dfd 1586* OpenMP::
41dbbb37 1587* OpenACC::
d60e76db 1588* Argument list functions::
34d417be 1589* Read/Write after EOF marker::
f6288c24
FR
1590* STRUCTURE and RECORD::
1591* UNION and MAP::
c98583e9 1592* Type variants for integer intrinsics::
34d567d1 1593* AUTOMATIC and STATIC attributes::
8e8c2744 1594* Extended math intrinsics::
ef144767 1595* Form feed as whitespace::
90051c26 1596* TYPE as an alias for PRINT::
cd714e1e 1597* %LOC as an rvalue::
1cf1719b 1598* .XOR. operator::
dd90ca33 1599* Bitwise logical operators::
0ef33d44 1600* Extended I/O specifiers::
35ea947f 1601* Legacy PARAMETER statements::
6869e9c6 1602* Default exponents::
294fbfc8 1603@end menu
6de9cd9a 1604
294fbfc8 1605@node Old-style kind specifications
49309826 1606@subsection Old-style kind specifications
e739dfac 1607@cindex kind, old-style
294fbfc8 1608
3994c6b1 1609GNU Fortran allows old-style kind specifications in declarations. These
b69862d1 1610look like:
294fbfc8 1611@smallexample
b69862d1 1612 TYPESPEC*size x,y,z
294fbfc8 1613@end smallexample
b2b81a3f 1614@noindent
11de78ff 1615where @code{TYPESPEC} is a basic type (@code{INTEGER}, @code{REAL},
b2b81a3f
BM
1616etc.), and where @code{size} is a byte count corresponding to the
1617storage size of a valid kind for that type. (For @code{COMPLEX}
1618variables, @code{size} is the total size of the real and imaginary
1619parts.) The statement then declares @code{x}, @code{y} and @code{z} to
1620be of type @code{TYPESPEC} with the appropriate kind. This is
1621equivalent to the standard-conforming declaration
294fbfc8
TS
1622@smallexample
1623 TYPESPEC(k) x,y,z
1624@end smallexample
b2b81a3f 1625@noindent
aad9c4f4
AM
1626where @code{k} is the kind parameter suitable for the intended precision. As
1627kind parameters are implementation-dependent, use the @code{KIND},
1628@code{SELECTED_INT_KIND} and @code{SELECTED_REAL_KIND} intrinsics to retrieve
1629the correct value, for instance @code{REAL*8 x} can be replaced by:
1630@smallexample
1631INTEGER, PARAMETER :: dbl = KIND(1.0d0)
1632REAL(KIND=dbl) :: x
1633@end smallexample
294fbfc8
TS
1634
1635@node Old-style variable initialization
49309826 1636@subsection Old-style variable initialization
294fbfc8 1637
7fc15ba5 1638GNU Fortran allows old-style initialization of variables of the
294fbfc8
TS
1639form:
1640@smallexample
11de78ff
BM
1641 INTEGER i/1/,j/2/
1642 REAL x(2,2) /3*0.,1./
294fbfc8 1643@end smallexample
11de78ff 1644The syntax for the initializers is as for the @code{DATA} statement, but
294fbfc8 1645unlike in a @code{DATA} statement, an initializer only applies to the
11de78ff
BM
1646variable immediately preceding the initialization. In other words,
1647something like @code{INTEGER I,J/2,3/} is not valid. This style of
1648initialization is only allowed in declarations without double colons
1649(@code{::}); the double colons were introduced in Fortran 90, which also
0979f01d 1650introduced a standard syntax for initializing variables in type
11de78ff
BM
1651declarations.
1652
1653Examples of standard-conforming code equivalent to the above example
1654are:
294fbfc8
TS
1655@smallexample
1656! Fortran 90
11de78ff
BM
1657 INTEGER :: i = 1, j = 2
1658 REAL :: x(2,2) = RESHAPE((/0.,0.,0.,1./),SHAPE(x))
294fbfc8 1659! Fortran 77
11de78ff
BM
1660 INTEGER i, j
1661 REAL x(2,2)
1662 DATA i/1/, j/2/, x/3*0.,1./
294fbfc8 1663@end smallexample
6de9cd9a 1664
11de78ff
BM
1665Note that variables which are explicitly initialized in declarations
1666or in @code{DATA} statements automatically acquire the @code{SAVE}
1667attribute.
9618502b 1668
670026fb 1669@node Extensions to namelist
49309826 1670@subsection Extensions to namelist
670026fb
PT
1671@cindex Namelist
1672
7fc15ba5 1673GNU Fortran fully supports the Fortran 95 standard for namelist I/O
670026fb
PT
1674including array qualifiers, substrings and fully qualified derived types.
1675The output from a namelist write is compatible with namelist read. The
1676output has all names in upper case and indentation to column 1 after the
1677namelist name. Two extensions are permitted:
1678
11de78ff 1679Old-style use of @samp{$} instead of @samp{&}
670026fb
PT
1680@smallexample
1681$MYNML
1682 X(:)%Y(2) = 1.0 2.0 3.0
1683 CH(1:4) = "abcd"
1684$END
1685@end smallexample
1686
11de78ff
BM
1687It should be noted that the default terminator is @samp{/} rather than
1688@samp{&END}.
670026fb 1689
3994c6b1 1690Querying of the namelist when inputting from stdin. After at least
11de78ff 1691one space, entering @samp{?} sends to stdout the namelist name and the names of
670026fb
PT
1692the variables in the namelist:
1693@smallexample
11de78ff 1694 ?
670026fb
PT
1695
1696&mynml
1697 x
1698 x%y
1699 ch
1700&end
1701@end smallexample
1702
11de78ff
BM
1703Entering @samp{=?} outputs the namelist to stdout, as if
1704@code{WRITE(*,NML = mynml)} had been called:
670026fb
PT
1705@smallexample
1706=?
1707
1708&MYNML
1709 X(1)%Y= 0.000000 , 1.000000 , 0.000000 ,
1710 X(2)%Y= 0.000000 , 2.000000 , 0.000000 ,
1711 X(3)%Y= 0.000000 , 3.000000 , 0.000000 ,
1712 CH=abcd, /
1713@end smallexample
1714
5724da63 1715To aid this dialog, when input is from stdin, errors send their
11de78ff 1716messages to stderr and execution continues, even if @code{IOSTAT} is set.
670026fb 1717
11de78ff
BM
1718@code{PRINT} namelist is permitted. This causes an error if
1719@option{-std=f95} is used.
21d7d31f
PT
1720@smallexample
1721PROGRAM test_print
1722 REAL, dimension (4) :: x = (/1.0, 2.0, 3.0, 4.0/)
1723 NAMELIST /mynml/ x
1724 PRINT mynml
1725END PROGRAM test_print
1726@end smallexample
1727
11de78ff
BM
1728Expanded namelist reads are permitted. This causes an error if
1729@option{-std=f95} is used. In the following example, the first element
1730of the array will be given the value 0.00 and the two succeeding
1731elements will be given the values 1.00 and 2.00.
c9f4aa97
JD
1732@smallexample
1733&MYNML
1734 X(1,1) = 0.00 , 1.00 , 2.00
1735/
1736@end smallexample
1737
e6e27788
JD
1738When writing a namelist, if no @code{DELIM=} is specified, by default a
1739double quote is used to delimit character strings. If -std=F95, F2003,
1740or F2008, etc, the delim status is set to 'none'. Defaulting to
1741quotes ensures that namelists with character strings can be subsequently
1742read back in accurately.
1743
11de78ff 1744@node X format descriptor without count field
49309826 1745@subsection @code{X} format descriptor without count field
ec8a1940 1746
11de78ff
BM
1747To support legacy codes, GNU Fortran permits the count field of the
1748@code{X} edit descriptor in @code{FORMAT} statements to be omitted.
1749When omitted, the count is implicitly assumed to be one.
ec8a1940
RS
1750
1751@smallexample
1752 PRINT 10, 2, 3
175310 FORMAT (I1, X, I1)
1754@end smallexample
1755
1756@node Commas in FORMAT specifications
49309826 1757@subsection Commas in @code{FORMAT} specifications
ec8a1940 1758
7fc15ba5 1759To support legacy codes, GNU Fortran allows the comma separator
ec8a1940 1760to be omitted immediately before and after character string edit
11de78ff 1761descriptors in @code{FORMAT} statements.
ec8a1940
RS
1762
1763@smallexample
1764 PRINT 10, 2, 3
176510 FORMAT ('FOO='I1' BAR='I2)
1766@end smallexample
1767
c9f4aa97
JD
1768
1769@node Missing period in FORMAT specifications
49309826 1770@subsection Missing period in @code{FORMAT} specifications
c9f4aa97 1771
7fc15ba5 1772To support legacy codes, GNU Fortran allows missing periods in format
11de78ff
BM
1773specifications if and only if @option{-std=legacy} is given on the
1774command line. This is considered non-conforming code and is
1775discouraged.
c9f4aa97
JD
1776
1777@smallexample
1778 REAL :: value
1779 READ(*,10) value
178010 FORMAT ('F4')
1781@end smallexample
1782
ec8a1940 1783@node I/O item lists
49309826 1784@subsection I/O item lists
ec8a1940
RS
1785@cindex I/O item lists
1786
7fc15ba5 1787To support legacy codes, GNU Fortran allows the input item list
11de78ff
BM
1788of the @code{READ} statement, and the output item lists of the
1789@code{WRITE} and @code{PRINT} statements, to start with a comma.
ec8a1940 1790
5a17346a
SK
1791@node @code{Q} exponent-letter
1792@subsection @code{Q} exponent-letter
1793@cindex @code{Q} exponent-letter
1794
1795GNU Fortran accepts real literal constants with an exponent-letter
1796of @code{Q}, for example, @code{1.23Q45}. The constant is interpreted
bf8367ac 1797as a @code{REAL(16)} entity on targets that support this type. If
5a17346a
SK
1798the target does not support @code{REAL(16)} but has a @code{REAL(10)}
1799type, then the real-literal-constant will be interpreted as a
1800@code{REAL(10)} entity. In the absence of @code{REAL(16)} and
1801@code{REAL(10)}, an error will occur.
1802
11de78ff 1803@node BOZ literal constants
49309826 1804@subsection BOZ literal constants
11de78ff 1805@cindex BOZ literal constants
ec8a1940 1806
00a4618b 1807Besides decimal constants, Fortran also supports binary (@code{b}),
3994c6b1 1808octal (@code{o}) and hexadecimal (@code{z}) integer constants. The
00a4618b
TB
1809syntax is: @samp{prefix quote digits quote}, were the prefix is
1810either @code{b}, @code{o} or @code{z}, quote is either @code{'} or
1811@code{"} and the digits are for binary @code{0} or @code{1}, for
1812octal between @code{0} and @code{7}, and for hexadecimal between
3994c6b1 1813@code{0} and @code{F}. (Example: @code{b'01011101'}.)
00a4618b
TB
1814
1815Up to Fortran 95, BOZ literals were only allowed to initialize
3994c6b1 1816integer variables in DATA statements. Since Fortran 2003 BOZ literals
00a4618b
TB
1817are also allowed as argument of @code{REAL}, @code{DBLE}, @code{INT}
1818and @code{CMPLX}; the result is the same as if the integer BOZ
1819literal had been converted by @code{TRANSFER} to, respectively,
1820@code{real}, @code{double precision}, @code{integer} or @code{complex}.
7f59aaba
TB
1821As GNU Fortran extension the intrinsic procedures @code{FLOAT},
1822@code{DFLOAT}, @code{COMPLEX} and @code{DCMPLX} are treated alike.
00a4618b 1823
11de78ff 1824As an extension, GNU Fortran allows hexadecimal BOZ literal constants to
00a4618b 1825be specified using the @code{X} prefix, in addition to the standard
3994c6b1 1826@code{Z} prefix. The BOZ literal can also be specified by adding a
00a4618b
TB
1827suffix to the string, for example, @code{Z'ABC'} and @code{'ABC'Z} are
1828equivalent.
1829
1830Furthermore, GNU Fortran allows using BOZ literal constants outside
1831DATA statements and the four intrinsic functions allowed by Fortran 2003.
1832In DATA statements, in direct assignments, where the right-hand side
1833only contains a BOZ literal constant, and for old-style initializers of
1834the form @code{integer i /o'0173'/}, the constant is transferred
c7abc45c 1835as if @code{TRANSFER} had been used; for @code{COMPLEX} numbers, only
3994c6b1 1836the real part is initialized unless @code{CMPLX} is used. In all other
c7abc45c 1837cases, the BOZ literal constant is converted to an @code{INTEGER} value with
00a4618b
TB
1838the largest decimal representation. This value is then converted
1839numerically to the type and kind of the variable in question.
9e0667cd 1840(For instance, @code{real :: r = b'0000001' + 1} initializes @code{r}
00a4618b
TB
1841with @code{2.0}.) As different compilers implement the extension
1842differently, one should be careful when doing bitwise initialization
1843of non-integer variables.
1844
1845Note that initializing an @code{INTEGER} variable with a statement such
1846as @code{DATA i/Z'FFFFFFFF'/} will give an integer overflow error rather
11de78ff
BM
1847than the desired result of @math{-1} when @code{i} is a 32-bit integer
1848on a system that supports 64-bit integers. The @samp{-fno-range-check}
1849option can be used as a workaround for legacy code that initializes
1850integers in this manner.
ec8a1940
RS
1851
1852@node Real array indices
49309826 1853@subsection Real array indices
e739dfac 1854@cindex array, indices of type real
ec8a1940 1855
11de78ff
BM
1856As an extension, GNU Fortran allows the use of @code{REAL} expressions
1857or variables as array indices.
ec8a1940
RS
1858
1859@node Unary operators
49309826 1860@subsection Unary operators
e739dfac 1861@cindex operators, unary
ec8a1940 1862
11de78ff
BM
1863As an extension, GNU Fortran allows unary plus and unary minus operators
1864to appear as the second operand of binary arithmetic operators without
1865the need for parenthesis.
ec8a1940
RS
1866
1867@smallexample
1868 X = Y * -Z
1869@end smallexample
1870
11de78ff 1871@node Implicitly convert LOGICAL and INTEGER values
49309826 1872@subsection Implicitly convert @code{LOGICAL} and @code{INTEGER} values
e739dfac
DF
1873@cindex conversion, to integer
1874@cindex conversion, to logical
c3a29423 1875
11de78ff
BM
1876As an extension for backwards compatibility with other compilers, GNU
1877Fortran allows the implicit conversion of @code{LOGICAL} values to
1878@code{INTEGER} values and vice versa. When converting from a
1879@code{LOGICAL} to an @code{INTEGER}, @code{.FALSE.} is interpreted as
1880zero, and @code{.TRUE.} is interpreted as one. When converting from
1881@code{INTEGER} to @code{LOGICAL}, the value zero is interpreted as
49de9e73 1882@code{.FALSE.} and any nonzero value is interpreted as @code{.TRUE.}.
c3a29423
RS
1883
1884@smallexample
a8eabe74
DF
1885 LOGICAL :: l
1886 l = 1
1887@end smallexample
1888@smallexample
1889 INTEGER :: i
1890 i = .TRUE.
c3a29423
RS
1891@end smallexample
1892
a8eabe74
DF
1893However, there is no implicit conversion of @code{INTEGER} values in
1894@code{if}-statements, nor of @code{LOGICAL} or @code{INTEGER} values
1895in I/O operations.
69130754 1896
bc192c77 1897@node Hollerith constants support
49309826 1898@subsection Hollerith constants support
bc192c77
FW
1899@cindex Hollerith constants
1900
11de78ff
BM
1901GNU Fortran supports Hollerith constants in assignments, function
1902arguments, and @code{DATA} and @code{ASSIGN} statements. A Hollerith
0979f01d 1903constant is written as a string of characters preceded by an integer
11de78ff
BM
1904constant indicating the character count, and the letter @code{H} or
1905@code{h}, and stored in bytewise fashion in a numeric (@code{INTEGER},
1906@code{REAL}, or @code{complex}) or @code{LOGICAL} variable. The
1907constant will be padded or truncated to fit the size of the variable in
1908which it is stored.
bc192c77 1909
11de78ff 1910Examples of valid uses of Hollerith constants:
bc192c77 1911@smallexample
11de78ff
BM
1912 complex*16 x(2)
1913 data x /16Habcdefghijklmnop, 16Hqrstuvwxyz012345/
1914 x(1) = 16HABCDEFGHIJKLMNOP
1915 call foo (4h abc)
bc192c77
FW
1916@end smallexample
1917
1918Invalid Hollerith constants examples:
1919@smallexample
11de78ff
BM
1920 integer*4 a
1921 a = 8H12345678 ! Valid, but the Hollerith constant will be truncated.
1922 a = 0H ! At least one character is needed.
bc192c77
FW
1923@end smallexample
1924
11de78ff
BM
1925In general, Hollerith constants were used to provide a rudimentary
1926facility for handling character strings in early Fortran compilers,
1927prior to the introduction of @code{CHARACTER} variables in Fortran 77;
1928in those cases, the standard-compliant equivalent is to convert the
1929program to use proper character strings. On occasion, there may be a
1930case where the intent is specifically to initialize a numeric variable
1931with a given byte sequence. In these cases, the same result can be
1932obtained by using the @code{TRANSFER} statement, as in this example.
1933@smallexample
1934 INTEGER(KIND=4) :: a
1935 a = TRANSFER ("abcd", a) ! equivalent to: a = 4Habcd
1936@end smallexample
1937
1938
83d890b9 1939@node Cray pointers
49309826
FXC
1940@subsection Cray pointers
1941@cindex pointer, Cray
83d890b9
AL
1942
1943Cray pointers are part of a non-standard extension that provides a
1944C-like pointer in Fortran. This is accomplished through a pair of
1945variables: an integer "pointer" that holds a memory address, and a
1946"pointee" that is used to dereference the pointer.
1947
1948Pointer/pointee pairs are declared in statements of the form:
1949@smallexample
1950 pointer ( <pointer> , <pointee> )
1951@end smallexample
1952or,
1953@smallexample
1954 pointer ( <pointer1> , <pointee1> ), ( <pointer2> , <pointee2> ), ...
1955@end smallexample
1956The pointer is an integer that is intended to hold a memory address.
58148bb6
SK
1957The pointee may be an array or scalar.
1958If an assumed-size array is permitted within the scoping unit, a
1959pointee can be an assumed-size array.
1960That is, the last dimension may be left unspecified by using a @code{*}
1961in place of a value. A pointee cannot be an assumed shape array.
1962No space is allocated for the pointee.
83d890b9
AL
1963
1964The pointee may have its type declared before or after the pointer
1965statement, and its array specification (if any) may be declared
1966before, during, or after the pointer statement. The pointer may be
1967declared as an integer prior to the pointer statement. However, some
1968machines have default integer sizes that are different than the size
1969of a pointer, and so the following code is not portable:
1970@smallexample
1971 integer ipt
1972 pointer (ipt, iarr)
1973@end smallexample
1974If a pointer is declared with a kind that is too small, the compiler
1975will issue a warning; the resulting binary will probably not work
1976correctly, because the memory addresses stored in the pointers may be
1977truncated. It is safer to omit the first line of the above example;
1978if explicit declaration of ipt's type is omitted, then the compiler
1979will ensure that ipt is an integer variable large enough to hold a
1980pointer.
1981
1982Pointer arithmetic is valid with Cray pointers, but it is not the same
1983as C pointer arithmetic. Cray pointers are just ordinary integers, so
1984the user is responsible for determining how many bytes to add to a
1985pointer in order to increment it. Consider the following example:
1986@smallexample
1987 real target(10)
1988 real pointee(10)
1989 pointer (ipt, pointee)
1990 ipt = loc (target)
1991 ipt = ipt + 1
1992@end smallexample
11de78ff
BM
1993The last statement does not set @code{ipt} to the address of
1994@code{target(1)}, as it would in C pointer arithmetic. Adding @code{1}
1995to @code{ipt} just adds one byte to the address stored in @code{ipt}.
83d890b9
AL
1996
1997Any expression involving the pointee will be translated to use the
b122dc6a 1998value stored in the pointer as the base address.
83d890b9
AL
1999
2000To get the address of elements, this extension provides an intrinsic
11de78ff
BM
2001function @code{LOC()}. The @code{LOC()} function is equivalent to the
2002@code{&} operator in C, except the address is cast to an integer type:
83d890b9
AL
2003@smallexample
2004 real ar(10)
2005 pointer(ipt, arpte(10))
2006 real arpte
2007 ipt = loc(ar) ! Makes arpte is an alias for ar
2008 arpte(1) = 1.0 ! Sets ar(1) to 1.0
2009@end smallexample
3397327c
BM
2010The pointer can also be set by a call to the @code{MALLOC} intrinsic
2011(see @ref{MALLOC}).
2012
83d890b9
AL
2013Cray pointees often are used to alias an existing variable. For
2014example:
2015@smallexample
2016 integer target(10)
2017 integer iarr(10)
2018 pointer (ipt, iarr)
2019 ipt = loc(target)
2020@end smallexample
11de78ff 2021As long as @code{ipt} remains unchanged, @code{iarr} is now an alias for
3994c6b1 2022@code{target}. The optimizer, however, will not detect this aliasing, so
11de78ff
BM
2023it is unsafe to use @code{iarr} and @code{target} simultaneously. Using
2024a pointee in any way that violates the Fortran aliasing rules or
3994c6b1 2025assumptions is illegal. It is the user's responsibility to avoid doing
11de78ff
BM
2026this; the compiler works under the assumption that no such aliasing
2027occurs.
2028
2029Cray pointers will work correctly when there is no aliasing (i.e., when
2030they are used to access a dynamically allocated block of memory), and
2031also in any routine where a pointee is used, but any variable with which
2032it shares storage is not used. Code that violates these rules may not
2033run as the user intends. This is not a bug in the optimizer; any code
2034that violates the aliasing rules is illegal. (Note that this is not
2035unique to GNU Fortran; any Fortran compiler that supports Cray pointers
2036will ``incorrectly'' optimize code with illegal aliasing.)
2037
2038There are a number of restrictions on the attributes that can be applied
2039to Cray pointers and pointees. Pointees may not have the
2040@code{ALLOCATABLE}, @code{INTENT}, @code{OPTIONAL}, @code{DUMMY},
3994c6b1 2041@code{TARGET}, @code{INTRINSIC}, or @code{POINTER} attributes. Pointers
11de78ff 2042may not have the @code{DIMENSION}, @code{POINTER}, @code{TARGET},
f14b9067 2043@code{ALLOCATABLE}, @code{EXTERNAL}, or @code{INTRINSIC} attributes, nor
3994c6b1 2044may they be function results. Pointees may not occur in more than one
f14b9067
FXC
2045pointer statement. A pointee cannot be a pointer. Pointees cannot occur
2046in equivalence, common, or data statements.
83d890b9 2047
11de78ff
BM
2048A Cray pointer may also point to a function or a subroutine. For
2049example, the following excerpt is valid:
7074ea72
AL
2050@smallexample
2051 implicit none
2052 external sub
2053 pointer (subptr,subpte)
2054 external subpte
2055 subptr = loc(sub)
2056 call subpte()
2057 [...]
2058 subroutine sub
2059 [...]
2060 end subroutine sub
2061@end smallexample
2062
83d890b9
AL
2063A pointer may be modified during the course of a program, and this
2064will change the location to which the pointee refers. However, when
2065pointees are passed as arguments, they are treated as ordinary
2066variables in the invoked function. Subsequent changes to the pointer
2067will not change the base address of the array that was passed.
2068
181c9f4a 2069@node CONVERT specifier
49309826
FXC
2070@subsection @code{CONVERT} specifier
2071@cindex @code{CONVERT} specifier
181c9f4a 2072
7fc15ba5 2073GNU Fortran allows the conversion of unformatted data between little-
181c9f4a 2074and big-endian representation to facilitate moving of data
eaa90d25 2075between different systems. The conversion can be indicated with
181c9f4a 2076the @code{CONVERT} specifier on the @code{OPEN} statement.
eaa90d25
TK
2077@xref{GFORTRAN_CONVERT_UNIT}, for an alternative way of specifying
2078the data format via an environment variable.
181c9f4a
TK
2079
2080Valid values for @code{CONVERT} are:
2081@itemize @w{}
2082@item @code{CONVERT='NATIVE'} Use the native format. This is the default.
2083@item @code{CONVERT='SWAP'} Swap between little- and big-endian.
eaa90d25 2084@item @code{CONVERT='LITTLE_ENDIAN'} Use the little-endian representation
6ccde948 2085for unformatted files.
eaa90d25 2086@item @code{CONVERT='BIG_ENDIAN'} Use the big-endian representation for
6ccde948 2087unformatted files.
181c9f4a
TK
2088@end itemize
2089
2090Using the option could look like this:
2091@smallexample
2092 open(file='big.dat',form='unformatted',access='sequential', &
2093 convert='big_endian')
2094@end smallexample
2095
2096The value of the conversion can be queried by using
2097@code{INQUIRE(CONVERT=ch)}. The values returned are
2098@code{'BIG_ENDIAN'} and @code{'LITTLE_ENDIAN'}.
2099
2100@code{CONVERT} works between big- and little-endian for
2101@code{INTEGER} values of all supported kinds and for @code{REAL}
8a6c4339 2102on IEEE systems of kinds 4 and 8. Conversion between different
181c9f4a 2103``extended double'' types on different architectures such as
7fc15ba5 2104m68k and x86_64, which GNU Fortran
11de78ff
BM
2105supports as @code{REAL(KIND=10)} and @code{REAL(KIND=16)}, will
2106probably not work.
181c9f4a 2107
eaa90d25
TK
2108@emph{Note that the values specified via the GFORTRAN_CONVERT_UNIT
2109environment variable will override the CONVERT specifier in the
2110open statement}. This is to give control over data formats to
11de78ff 2111users who do not have the source code of their program available.
eaa90d25
TK
2112
2113Using anything but the native representation for unformatted data
2114carries a significant speed overhead. If speed in this area matters
2115to you, it is best if you use this only for data that needs to be
2116portable.
2117
6c7a4dfd 2118@node OpenMP
49309826 2119@subsection OpenMP
6c7a4dfd
JJ
2120@cindex OpenMP
2121
3b303683
DF
2122OpenMP (Open Multi-Processing) is an application programming
2123interface (API) that supports multi-platform shared memory
2124multiprocessing programming in C/C++ and Fortran on many
2125architectures, including Unix and Microsoft Windows platforms.
2126It consists of a set of compiler directives, library routines,
2127and environment variables that influence run-time behavior.
2128
2129GNU Fortran strives to be compatible to the
0b4cb601 2130@uref{http://openmp.org/wp/openmp-specifications/,
8ebd1b31 2131OpenMP Application Program Interface v4.5}.
3b303683
DF
2132
2133To enable the processing of the OpenMP directive @code{!$omp} in
2134free-form source code; the @code{c$omp}, @code{*$omp} and @code{!$omp}
2135directives in fixed form; the @code{!$} conditional compilation sentinels
2136in free form; and the @code{c$}, @code{*$} and @code{!$} sentinels
2137in fixed form, @command{gfortran} needs to be invoked with the
3994c6b1 2138@option{-fopenmp}. This also arranges for automatic linking of the
f1f3453e
TS
2139GNU Offloading and Multi Processing Runtime Library
2140@ref{Top,,libgomp,libgomp,GNU Offloading and Multi Processing Runtime
2141Library}.
3b303683
DF
2142
2143The OpenMP Fortran runtime library routines are provided both in a
2144form of a Fortran 90 module named @code{omp_lib} and in a form of
2145a Fortran @code{include} file named @file{omp_lib.h}.
2146
2147An example of a parallelized loop taken from Appendix A.1 of
2148the OpenMP Application Program Interface v2.5:
2149@smallexample
2150SUBROUTINE A1(N, A, B)
2151 INTEGER I, N
2152 REAL B(N), A(N)
2153!$OMP PARALLEL DO !I is private by default
2154 DO I=2,N
2155 B(I) = (A(I) + A(I-1)) / 2.0
2156 ENDDO
2157!$OMP END PARALLEL DO
2158END SUBROUTINE A1
2159@end smallexample
2160
2161Please note:
2162@itemize
2163@item
24219f12 2164@option{-fopenmp} implies @option{-frecursive}, i.e., all local arrays
3994c6b1 2165will be allocated on the stack. When porting existing code to OpenMP,
3b303683
DF
2166this may lead to surprising results, especially to segmentation faults
2167if the stacksize is limited.
2168
2169@item
9e0667cd 2170On glibc-based systems, OpenMP enabled applications cannot be statically
3994c6b1 2171linked due to limitations of the underlying pthreads-implementation. It
3b303683
DF
2172might be possible to get a working solution if
2173@command{-Wl,--whole-archive -lpthread -Wl,--no-whole-archive} is added
3994c6b1 2174to the command line. However, this is not supported by @command{gcc} and
3b303683
DF
2175thus not recommended.
2176@end itemize
6c7a4dfd 2177
41dbbb37
TS
2178@node OpenACC
2179@subsection OpenACC
2180@cindex OpenACC
2181
2182OpenACC is an application programming interface (API) that supports
2183offloading of code to accelerator devices. It consists of a set of
2184compiler directives, library routines, and environment variables that
2185influence run-time behavior.
2186
2187GNU Fortran strives to be compatible to the
2188@uref{http://www.openacc.org/, OpenACC Application Programming
2189Interface v2.0}.
2190
2191To enable the processing of the OpenACC directive @code{!$acc} in
2192free-form source code; the @code{c$acc}, @code{*$acc} and @code{!$acc}
2193directives in fixed form; the @code{!$} conditional compilation
2194sentinels in free form; and the @code{c$}, @code{*$} and @code{!$}
2195sentinels in fixed form, @command{gfortran} needs to be invoked with
2196the @option{-fopenacc}. This also arranges for automatic linking of
2197the GNU Offloading and Multi Processing Runtime Library
2198@ref{Top,,libgomp,libgomp,GNU Offloading and Multi Processing Runtime
2199Library}.
2200
2201The OpenACC Fortran runtime library routines are provided both in a
2202form of a Fortran 90 module named @code{openacc} and in a form of a
2203Fortran @code{include} file named @file{openacc_lib.h}.
2204
2205Note that this is an experimental feature, incomplete, and subject to
2206change in future versions of GCC. See
2207@uref{https://gcc.gnu.org/wiki/OpenACC} for more information.
2208
d60e76db 2209@node Argument list functions
49309826 2210@subsection Argument list functions @code{%VAL}, @code{%REF} and @code{%LOC}
e739dfac 2211@cindex argument list functions
49309826
FXC
2212@cindex @code{%VAL}
2213@cindex @code{%REF}
2214@cindex @code{%LOC}
d60e76db
PT
2215
2216GNU Fortran supports argument list functions @code{%VAL}, @code{%REF}
2217and @code{%LOC} statements, for backward compatibility with g77.
2218It is recommended that these should be used only for code that is
2219accessing facilities outside of GNU Fortran, such as operating system
3994c6b1 2220or windowing facilities. It is best to constrain such uses to isolated
d60e76db 2221portions of a program--portions that deal specifically and exclusively
3994c6b1 2222with low-level, system-dependent facilities. Such portions might well
d60e76db
PT
2223provide a portable interface for use by the program as a whole, but are
2224themselves not portable, and should be thoroughly tested each time they
2225are rebuilt using a new compiler or version of a compiler.
2226
2227@code{%VAL} passes a scalar argument by value, @code{%REF} passes it by
2228reference and @code{%LOC} passes its memory location. Since gfortran
2229already passes scalar arguments by reference, @code{%REF} is in effect
9e0667cd 2230a do-nothing. @code{%LOC} has the same effect as a Fortran pointer.
d60e76db
PT
2231
2232An example of passing an argument by value to a C subroutine foo.:
2233@smallexample
2234C
2235C prototype void foo_ (float x);
2236C
2237 external foo
2238 real*4 x
2239 x = 3.14159
2240 call foo (%VAL (x))
2241 end
2242@end smallexample
2243
2244For details refer to the g77 manual
2bf716a9 2245@uref{https://gcc.gnu.org/@/onlinedocs/@/gcc-3.4.6/@/g77/@/index.html#Top}.
d60e76db 2246
9e0667cd
TB
2247Also, @code{c_by_val.f} and its partner @code{c_by_val.c} of the
2248GNU Fortran testsuite are worth a look.
49309826 2249
34d417be
JD
2250@node Read/Write after EOF marker
2251@subsection Read/Write after EOF marker
2252@cindex @code{EOF}
2253@cindex @code{BACKSPACE}
2254@cindex @code{REWIND}
2255
2256Some legacy codes rely on allowing @code{READ} or @code{WRITE} after the
2257EOF file marker in order to find the end of a file. GNU Fortran normally
2258rejects these codes with a run-time error message and suggests the user
2259consider @code{BACKSPACE} or @code{REWIND} to properly position
2260the file before the EOF marker. As an extension, the run-time error may
2261be disabled using -std=legacy.
49309826 2262
49309826
FXC
2263
2264@node STRUCTURE and RECORD
2265@subsection @code{STRUCTURE} and @code{RECORD}
2266@cindex @code{STRUCTURE}
2267@cindex @code{RECORD}
2268
87f56a65 2269Record structures are a pre-Fortran-90 vendor extension to create
40c84ee7
FR
2270user-defined aggregate data types. Support for record structures in GNU
2271Fortran can be enabled with the @option{-fdec-structure} compile flag.
2272If you have a choice, you should instead use Fortran 90's ``derived types'',
2273which have a different syntax.
87f56a65
TB
2274
2275In many cases, record structures can easily be converted to derived types.
2276To convert, replace @code{STRUCTURE /}@var{structure-name}@code{/}
2277by @code{TYPE} @var{type-name}. Additionally, replace
2278@code{RECORD /}@var{structure-name}@code{/} by
2279@code{TYPE(}@var{type-name}@code{)}. Finally, in the component access,
2280replace the period (@code{.}) by the percent sign (@code{%}).
2281
2282Here is an example of code using the non portable record structure syntax:
49309826
FXC
2283
2284@example
2285! Declaring a structure named ``item'' and containing three fields:
2286! an integer ID, an description string and a floating-point price.
2287STRUCTURE /item/
2288 INTEGER id
2289 CHARACTER(LEN=200) description
2290 REAL price
2291END STRUCTURE
2292
2293! Define two variables, an single record of type ``item''
2294! named ``pear'', and an array of items named ``store_catalog''
2295RECORD /item/ pear, store_catalog(100)
2296
2297! We can directly access the fields of both variables
2298pear.id = 92316
2299pear.description = "juicy D'Anjou pear"
2300pear.price = 0.15
2301store_catalog(7).id = 7831
2302store_catalog(7).description = "milk bottle"
2303store_catalog(7).price = 1.2
2304
aad9c4f4 2305! We can also manipulate the whole structure
49309826
FXC
2306store_catalog(12) = pear
2307print *, store_catalog(12)
2308@end example
2309
2310@noindent
2311This code can easily be rewritten in the Fortran 90 syntax as following:
2312
2313@example
2314! ``STRUCTURE /name/ ... END STRUCTURE'' becomes
2315! ``TYPE name ... END TYPE''
2316TYPE item
2317 INTEGER id
2318 CHARACTER(LEN=200) description
2319 REAL price
2320END TYPE
2321
2322! ``RECORD /name/ variable'' becomes ``TYPE(name) variable''
2323TYPE(item) pear, store_catalog(100)
2324
2325! Instead of using a dot (.) to access fields of a record, the
2326! standard syntax uses a percent sign (%)
2327pear%id = 92316
2328pear%description = "juicy D'Anjou pear"
2329pear%price = 0.15
2330store_catalog(7)%id = 7831
2331store_catalog(7)%description = "milk bottle"
2332store_catalog(7)%price = 1.2
2333
c5a0818e 2334! Assignments of a whole variable do not change
49309826
FXC
2335store_catalog(12) = pear
2336print *, store_catalog(12)
2337@end example
2338
f6288c24
FR
2339@noindent
2340GNU Fortran implements STRUCTURES like derived types with the following
2341rules and exceptions:
2342
2343@itemize @bullet
2344@item Structures act like derived types with the @code{SEQUENCE} attribute.
2345Otherwise they may contain no specifiers.
2346
6f855a26
FR
2347@item Structures may contain a special field with the name @code{%FILL}.
2348This will create an anonymous component which cannot be accessed but occupies
2349space just as if a component of the same type was declared in its place, useful
2350for alignment purposes. As an example, the following structure will consist
2351of at least sixteen bytes:
2352
2353@smallexample
2354structure /padded/
2355 character(4) start
2356 character(8) %FILL
2357 character(4) end
2358end structure
2359@end smallexample
2360
f6288c24
FR
2361@item Structures may share names with other symbols. For example, the following
2362is invalid for derived types, but valid for structures:
2363
2364@smallexample
2365structure /header/
2366 ! ...
2367end structure
2368record /header/ header
2369@end smallexample
2370
2371@item Structure types may be declared nested within another parent structure.
2372The syntax is:
2373@smallexample
2374structure /type-name/
2375 ...
2376 structure [/<type-name>/] <field-list>
2377...
2378@end smallexample
2379
2380The type name may be ommitted, in which case the structure type itself is
2381anonymous, and other structures of the same type cannot be instantiated. The
2382following shows some examples:
2383
2384@example
2385structure /appointment/
2386 ! nested structure definition: app_time is an array of two 'time'
2387 structure /time/ app_time (2)
2388 integer(1) hour, minute
2389 end structure
2390 character(10) memo
2391end structure
2392
2393! The 'time' structure is still usable
2394record /time/ now
2395now = time(5, 30)
2396
2397...
2398
2399structure /appointment/
2400 ! anonymous nested structure definition
2401 structure start, end
2402 integer(1) hour, minute
2403 end structure
2404 character(10) memo
2405end structure
2406@end example
2407
2408@item Structures may contain @code{UNION} blocks. For more detail see the
2409section on @ref{UNION and MAP}.
49309826 2410
f6288c24
FR
2411@item Structures support old-style initialization of components, like
2412those described in @ref{Old-style variable initialization}. For array
2413initializers, an initializer may contain a repeat specification of the form
2414@code{<literal-integer> * <constant-initializer>}. The value of the integer
2415indicates the number of times to repeat the constant initializer when expanding
2416the initializer list.
2417@end itemize
2418
2419@node UNION and MAP
2420@subsection @code{UNION} and @code{MAP}
2421@cindex @code{UNION}
2422@cindex @code{MAP}
2423
2424Unions are an old vendor extension which were commonly used with the
2425non-standard @ref{STRUCTURE and RECORD} extensions. Use of @code{UNION} and
2426@code{MAP} is automatically enabled with @option{-fdec-structure}.
2427
2428A @code{UNION} declaration occurs within a structure; within the definition of
2429each union is a number of @code{MAP} blocks. Each @code{MAP} shares storage
2430with its sibling maps (in the same union), and the size of the union is the
2431size of the largest map within it, just as with unions in C. The major
2432difference is that component references do not indicate which union or map the
2433component is in (the compiler gets to figure that out).
2434
2435Here is a small example:
2436@smallexample
2437structure /myunion/
2438union
2439 map
785abfd3 2440 character(2) w0, w1, w2
f6288c24
FR
2441 end map
2442 map
785abfd3 2443 character(6) long
f6288c24
FR
2444 end map
2445end union
2446end structure
2447
2448record /myunion/ rec
785abfd3
FR
2449! After this assignment...
2450rec.long = 'hello!'
f6288c24
FR
2451
2452! The following is true:
785abfd3
FR
2453! rec.w0 === 'he'
2454! rec.w1 === 'll'
2455! rec.w2 === 'o!'
f6288c24
FR
2456@end smallexample
2457
785abfd3 2458The two maps share memory, and the size of the union is ultimately six bytes:
f6288c24
FR
2459
2460@example
24610 1 2 3 4 5 6 Byte offset
2462-------------------------------
2463| | | | | | |
2464-------------------------------
2465
2466^ W0 ^ W1 ^ W2 ^
2467 \-------/ \-------/ \-------/
2468
785abfd3
FR
2469^ LONG ^
2470 \---------------------------/
f6288c24
FR
2471@end example
2472
2473Following is an example mirroring the layout of an Intel x86_64 register:
2474
2475@example
2476structure /reg/
785abfd3 2477 union ! U0 ! rax
f6288c24 2478 map
785abfd3 2479 character(16) rx
f6288c24
FR
2480 end map
2481 map
785abfd3
FR
2482 character(8) rh ! rah
2483 union ! U1
f6288c24 2484 map
785abfd3 2485 character(8) rl ! ral
f6288c24
FR
2486 end map
2487 map
785abfd3 2488 character(8) ex ! eax
f6288c24
FR
2489 end map
2490 map
785abfd3
FR
2491 character(4) eh ! eah
2492 union ! U2
f6288c24 2493 map
785abfd3 2494 character(4) el ! eal
f6288c24
FR
2495 end map
2496 map
785abfd3 2497 character(4) x ! ax
f6288c24
FR
2498 end map
2499 map
785abfd3
FR
2500 character(2) h ! ah
2501 character(2) l ! al
f6288c24 2502 end map
785abfd3 2503 end union
f6288c24 2504 end map
785abfd3 2505 end union
f6288c24 2506 end map
785abfd3 2507 end union
f6288c24 2508end structure
f6288c24
FR
2509record /reg/ a
2510
2511! After this assignment...
785abfd3 2512a.rx = 'AAAAAAAA.BBB.C.D'
f6288c24
FR
2513
2514! The following is true:
785abfd3
FR
2515a.rx === 'AAAAAAAA.BBB.C.D'
2516a.rh === 'AAAAAAAA'
2517a.rl === '.BBB.C.D'
2518a.ex === '.BBB.C.D'
2519a.eh === '.BBB'
2520a.el === '.C.D'
2521a.x === '.C.D'
2522a.h === '.C'
2523a.l === '.D'
f6288c24
FR
2524@end example
2525
c98583e9
FR
2526@node Type variants for integer intrinsics
2527@subsection Type variants for integer intrinsics
2528@cindex intrinsics, integer
2529
2530Similar to the D/C prefixes to real functions to specify the input/output
2531types, GNU Fortran offers B/I/J/K prefixes to integer functions for
2532compatibility with DEC programs. The types implied by each are:
2533
2534@example
2535@code{B} - @code{INTEGER(kind=1)}
2536@code{I} - @code{INTEGER(kind=2)}
2537@code{J} - @code{INTEGER(kind=4)}
2538@code{K} - @code{INTEGER(kind=8)}
2539@end example
2540
2541GNU Fortran supports these with the flag @option{-fdec-intrinsic-ints}.
2542Intrinsics for which prefixed versions are available and in what form are noted
2543in @ref{Intrinsic Procedures}. The complete list of supported intrinsics is
2544here:
2545
2546@multitable @columnfractions .2 .2 .2 .2 .2
2547
2548@headitem Intrinsic @tab B @tab I @tab J @tab K
2549
2550@item @code{@ref{ABS}}
2551 @tab @code{BABS} @tab @code{IIABS} @tab @code{JIABS} @tab @code{KIABS}
2552@item @code{@ref{BTEST}}
2553 @tab @code{BBTEST} @tab @code{BITEST} @tab @code{BJTEST} @tab @code{BKTEST}
2554@item @code{@ref{IAND}}
2555 @tab @code{BIAND} @tab @code{IIAND} @tab @code{JIAND} @tab @code{KIAND}
2556@item @code{@ref{IBCLR}}
2557 @tab @code{BBCLR} @tab @code{IIBCLR} @tab @code{JIBCLR} @tab @code{KIBCLR}
2558@item @code{@ref{IBITS}}
2559 @tab @code{BBITS} @tab @code{IIBITS} @tab @code{JIBITS} @tab @code{KIBITS}
2560@item @code{@ref{IBSET}}
2561 @tab @code{BBSET} @tab @code{IIBSET} @tab @code{JIBSET} @tab @code{KIBSET}
2562@item @code{@ref{IEOR}}
2563 @tab @code{BIEOR} @tab @code{IIEOR} @tab @code{JIEOR} @tab @code{KIEOR}
2564@item @code{@ref{IOR}}
2565 @tab @code{BIOR} @tab @code{IIOR} @tab @code{JIOR} @tab @code{KIOR}
2566@item @code{@ref{ISHFT}}
2567 @tab @code{BSHFT} @tab @code{IISHFT} @tab @code{JISHFT} @tab @code{KISHFT}
2568@item @code{@ref{ISHFTC}}
2569 @tab @code{BSHFTC} @tab @code{IISHFTC} @tab @code{JISHFTC} @tab @code{KISHFTC}
2570@item @code{@ref{MOD}}
2571 @tab @code{BMOD} @tab @code{IMOD} @tab @code{JMOD} @tab @code{KMOD}
2572@item @code{@ref{NOT}}
2573 @tab @code{BNOT} @tab @code{INOT} @tab @code{JNOT} @tab @code{KNOT}
2574@item @code{@ref{REAL}}
2575 @tab @code{--} @tab @code{FLOATI} @tab @code{FLOATJ} @tab @code{FLOATK}
2576@end multitable
2577
34d567d1
FR
2578@node AUTOMATIC and STATIC attributes
2579@subsection @code{AUTOMATIC} and @code{STATIC} attributes
2580@cindex variable attributes
2581@cindex @code{AUTOMATIC}
2582@cindex @code{STATIC}
2583
2584With @option{-fdec-static} GNU Fortran supports the DEC extended attributes
2585@code{STATIC} and @code{AUTOMATIC} to provide explicit specification of entity
2586storage. These follow the syntax of the Fortran standard @code{SAVE} attribute.
2587
2588@code{STATIC} is exactly equivalent to @code{SAVE}, and specifies that
2589an entity should be allocated in static memory. As an example, @code{STATIC}
2590local variables will retain their values across multiple calls to a function.
2591
2592Entities marked @code{AUTOMATIC} will be stack automatic whenever possible.
2593@code{AUTOMATIC} is the default for local variables smaller than
2594@option{-fmax-stack-var-size}, unless @option{-fno-automatic} is given. This
2595attribute overrides @option{-fno-automatic}, @option{-fmax-stack-var-size}, and
2596blanket @code{SAVE} statements.
2597
2598
2599Examples:
2600
2601@example
2602subroutine f
2603 integer, automatic :: i ! automatic variable
2604 integer x, y ! static variables
2605 save
2606 ...
2607endsubroutine
2608@end example
2609@example
2610subroutine f
2611 integer a, b, c, x, y, z
2612 static :: x
2613 save y
2614 automatic z, c
2615 ! a, b, c, and z are automatic
2616 ! x and y are static
2617endsubroutine
2618@end example
2619@example
2620! Compiled with -fno-automatic
2621subroutine f
2622 integer a, b, c, d
2623 automatic :: a
2624 ! a is automatic; b, c, and d are static
2625endsubroutine
2626@end example
2627
8e8c2744
FR
2628@node Extended math intrinsics
2629@subsection Extended math intrinsics
2630@cindex intrinsics, math
2631@cindex intrinsics, trigonometric functions
2632
2633GNU Fortran supports an extended list of mathematical intrinsics with the
2634compile flag @option{-fdec-math} for compatability with legacy code.
2635These intrinsics are described fully in @ref{Intrinsic Procedures} where it is
2636noted that they are extensions and should be avoided whenever possible.
2637
2638Specifically, @option{-fdec-math} enables the @ref{COTAN} intrinsic, and
2639trigonometric intrinsics which accept or produce values in degrees instead of
2640radians. Here is a summary of the new intrinsics:
2641
2642@multitable @columnfractions .5 .5
2643@headitem Radians @tab Degrees
2644@item @code{@ref{ACOS}} @tab @code{@ref{ACOSD}}*
2645@item @code{@ref{ASIN}} @tab @code{@ref{ASIND}}*
2646@item @code{@ref{ATAN}} @tab @code{@ref{ATAND}}*
2647@item @code{@ref{ATAN2}} @tab @code{@ref{ATAN2D}}*
2648@item @code{@ref{COS}} @tab @code{@ref{COSD}}*
2649@item @code{@ref{COTAN}}* @tab @code{@ref{COTAND}}*
2650@item @code{@ref{SIN}} @tab @code{@ref{SIND}}*
2651@item @code{@ref{TAN}} @tab @code{@ref{TAND}}*
2652@end multitable
2653
2654* Enabled with @option{-fdec-math}.
2655
2656For advanced users, it may be important to know the implementation of these
2657functions. They are simply wrappers around the standard radian functions, which
2658have more accurate builtin versions. These functions convert their arguments
2659(or results) to degrees (or radians) by taking the value modulus 360 (or 2*pi)
2660and then multiplying it by a constant radian-to-degree (or degree-to-radian)
2661factor, as appropriate. The factor is computed at compile-time as 180/pi (or
2662pi/180).
2663
ef144767
FR
2664@node Form feed as whitespace
2665@subsection Form feed as whitespace
2666@cindex form feed whitespace
2667
2668Historically, legacy compilers allowed insertion of form feed characters ('\f',
2669ASCII 0xC) at the beginning of lines for formatted output to line printers,
2670though the Fortran standard does not mention this. GNU Fortran supports the
2671interpretation of form feed characters in source as whitespace for
2672compatibility.
2673
90051c26
FR
2674@node TYPE as an alias for PRINT
2675@subsection TYPE as an alias for PRINT
2676@cindex type alias print
2677For compatibility, GNU Fortran will interpret @code{TYPE} statements as
2678@code{PRINT} statements with the flag @option{-fdec}. With this flag asserted,
2679the following two examples are equivalent:
2680
2681@smallexample
2682TYPE *, 'hello world'
2683@end smallexample
2684
2685@smallexample
2686PRINT *, 'hello world'
2687@end smallexample
2688
cd714e1e
FR
2689@node %LOC as an rvalue
2690@subsection %LOC as an rvalue
2691@cindex LOC
2692Normally @code{%LOC} is allowed only in parameter lists. However the intrinsic
2693function @code{LOC} does the same thing, and is usable as the right-hand-side of
2694assignments. For compatibility, GNU Fortran supports the use of @code{%LOC} as
2695an alias for the builtin @code{LOC} with @option{-std=legacy}. With this
2696feature enabled the following two examples are equivalent:
2697
2698@smallexample
2699integer :: i, l
2700l = %loc(i)
2701call sub(l)
2702@end smallexample
2703
2704@smallexample
2705integer :: i
2706call sub(%loc(i))
2707@end smallexample
2708
1cf1719b
FR
2709@node .XOR. operator
2710@subsection .XOR. operator
2711@cindex operators, xor
2712
2713GNU Fortran supports @code{.XOR.} as a logical operator with @code{-std=legacy}
2714for compatibility with legacy code. @code{.XOR.} is equivalent to
2715@code{.NEQV.}. That is, the output is true if and only if the inputs differ.
2716
dd90ca33
FR
2717@node Bitwise logical operators
2718@subsection Bitwise logical operators
2719@cindex logical, bitwise
2720
2721With @option{-fdec}, GNU Fortran relaxes the type constraints on
2722logical operators to allow integer operands, and performs the corresponding
2723bitwise operation instead. This flag is for compatibility only, and should be
2724avoided in new code. Consider:
2725
2726@smallexample
2727 INTEGER :: i, j
2728 i = z'33'
2729 j = z'cc'
2730 print *, i .AND. j
2731@end smallexample
2732
2733In this example, compiled with @option{-fdec}, GNU Fortran will
2734replace the @code{.AND.} operation with a call to the intrinsic
2735@code{@ref{IAND}} function, yielding the bitwise-and of @code{i} and @code{j}.
2736
2737Note that this conversion will occur if at least one operand is of integral
2738type. As a result, a logical operand will be converted to an integer when the
2739other operand is an integer in a logical operation. In this case,
2740@code{.TRUE.} is converted to @code{1} and @code{.FALSE.} to @code{0}.
2741
2742Here is the mapping of logical operator to bitwise intrinsic used with
2743@option{-fdec}:
2744
2745@multitable @columnfractions .25 .25 .5
2746@headitem Operator @tab Intrinsic @tab Bitwise operation
2747@item @code{.NOT.} @tab @code{@ref{NOT}} @tab complement
2748@item @code{.AND.} @tab @code{@ref{IAND}} @tab intersection
2749@item @code{.OR.} @tab @code{@ref{IOR}} @tab union
2750@item @code{.NEQV.} @tab @code{@ref{IEOR}} @tab exclusive or
2751@item @code{.EQV.} @tab @code{@ref{NOT}(@ref{IEOR})} @tab complement of exclusive or
2752@end multitable
2753
0ef33d44
FR
2754@node Extended I/O specifiers
2755@subsection Extended I/O specifiers
2756@cindex @code{CARRIAGECONTROL}
2757@cindex @code{READONLY}
2758@cindex @code{SHARE}
2759@cindex @code{SHARED}
2760@cindex @code{NOSHARED}
2761@cindex I/O specifiers
2762
2763GNU Fortran supports the additional legacy I/O specifiers
2764@code{CARRIAGECONTROL}, @code{READONLY}, and @code{SHARE} with the
2765compile flag @option{-fdec}, for compatibility.
2766
2767@table @code
2768@item CARRIAGECONTROL
2769The @code{CARRIAGECONTROL} specifier allows a user to control line
2770termination settings between output records for an I/O unit. The specifier has
2771no meaning for readonly files. When @code{CARRAIGECONTROL} is specified upon
2772opening a unit for formatted writing, the exact @code{CARRIAGECONTROL} setting
2773determines what characters to write between output records. The syntax is:
2774
2775@smallexample
2776OPEN(..., CARRIAGECONTROL=cc)
2777@end smallexample
2778
2779Where @emph{cc} is a character expression that evaluates to one of the
2780following values:
2781
2782@multitable @columnfractions .2 .8
2783@item @code{'LIST'} @tab One line feed between records (default)
2784@item @code{'FORTRAN'} @tab Legacy interpretation of the first character (see below)
2785@item @code{'NONE'} @tab No separator between records
2786@end multitable
2787
2788With @code{CARRIAGECONTROL='FORTRAN'}, when a record is written, the first
2789character of the input record is not written, and instead determines the output
2790record separator as follows:
2791
2792@multitable @columnfractions .3 .3 .4
2793@headitem Leading character @tab Meaning @tab Output separating character(s)
2794@item @code{'+'} @tab Overprinting @tab Carriage return only
2795@item @code{'-'} @tab New line @tab Line feed and carriage return
2796@item @code{'0'} @tab Skip line @tab Two line feeds and carriage return
2797@item @code{'1'} @tab New page @tab Form feed and carriage return
2798@item @code{'$'} @tab Prompting @tab Line feed (no carriage return)
2799@item @code{CHAR(0)} @tab Overprinting (no advance) @tab None
2800@end multitable
2801
2802@item READONLY
2803The @code{READONLY} specifier may be given upon opening a unit, and is
2804equivalent to specifying @code{ACTION='READ'}, except that the file may not be
2805deleted on close (i.e. @code{CLOSE} with @code{STATUS="DELETE"}). The syntax
2806is:
2807
2808@smallexample
2809@code{OPEN(..., READONLY)}
2810@end smallexample
2811
2812@item SHARE
2813The @code{SHARE} specifier allows system-level locking on a unit upon opening
2814it for controlled access from multiple processes/threads. The @code{SHARE}
2815specifier has several forms:
2816
2817@smallexample
2818OPEN(..., SHARE=sh)
2819OPEN(..., SHARED)
2820OPEN(..., NOSHARED)
2821@end smallexample
2822
2823Where @emph{sh} in the first form is a character expression that evaluates to
2824a value as seen in the table below. The latter two forms are aliases
2825for particular values of @emph{sh}:
2826
2827@multitable @columnfractions .3 .3 .4
2828@headitem Explicit form @tab Short form @tab Meaning
2829@item @code{SHARE='DENYRW'} @tab @code{NOSHARED} @tab Exclusive (write) lock
2830@item @code{SHARE='DENYNONE'} @tab @code{SHARED} @tab Shared (read) lock
2831@end multitable
2832
2833In general only one process may hold an exclusive (write) lock for a given file
2834at a time, whereas many processes may hold shared (read) locks for the same
2835file.
2836
2837The behavior of locking may vary with your operating system. On POSIX systems,
2838locking is implemented with @code{fcntl}. Consult your corresponding operating
2839system's manual pages for further details. Locking via @code{SHARE=} is not
2840supported on other systems.
2841
2842@end table
f6288c24 2843
35ea947f
FR
2844@node Legacy PARAMETER statements
2845@subsection Legacy PARAMETER statements
2846@cindex PARAMETER
2847
2848For compatibility, GNU Fortran supports legacy PARAMETER statements without
2849parentheses with @option{-std=legacy}. A warning is emitted if used with
2850@option{-std=gnu}, and an error is acknowledged with a real Fortran standard
2851flag (@option{-std=f95}, etc...). These statements take the following form:
2852
2853@smallexample
2854implicit real (E)
2855parameter e = 2.718282
2856real c
2857parameter c = 3.0e8
2858@end smallexample
2859
6869e9c6
FR
2860@node Default exponents
2861@subsection Default exponents
2862@cindex exponent
2863
2864For compatibility, GNU Fortran supports a default exponent of zero in real
2865constants with @option{-fdec}. For example, @code{9e} would be
2866interpreted as @code{9e0}, rather than an error.
2867
35ea947f 2868
f6288c24
FR
2869@node Extensions not implemented in GNU Fortran
2870@section Extensions not implemented in GNU Fortran
2871@cindex extensions, not implemented
2872
2873The long history of the Fortran language, its wide use and broad
2874userbase, the large number of different compiler vendors and the lack of
2875some features crucial to users in the first standards have lead to the
2876existence of a number of important extensions to the language. While
2877some of the most useful or popular extensions are supported by the GNU
2878Fortran compiler, not all existing extensions are supported. This section
2879aims at listing these extensions and offering advice on how best make
2880code that uses them running with the GNU Fortran compiler.
49309826 2881
f6288c24
FR
2882@c More can be found here:
2883@c -- https://gcc.gnu.org/onlinedocs/gcc-3.4.6/g77/Missing-Features.html
2884@c -- the list of Fortran and libgfortran bugs closed as WONTFIX:
2885@c http://tinyurl.com/2u4h5y
2886
2887@menu
2888* ENCODE and DECODE statements::
2889* Variable FORMAT expressions::
2890@c * Q edit descriptor::
f6288c24 2891@c * TYPE and ACCEPT I/O Statements::
0ef33d44 2892@c * DEFAULTFILE, DISPOSE and RECORDTYPE I/O specifiers::
f6288c24
FR
2893@c * Omitted arguments in procedure call::
2894* Alternate complex function syntax::
2895* Volatile COMMON blocks::
2896* OPEN( ... NAME=)::
2897@end menu
49309826
FXC
2898
2899@node ENCODE and DECODE statements
2900@subsection @code{ENCODE} and @code{DECODE} statements
2901@cindex @code{ENCODE}
2902@cindex @code{DECODE}
2903
c5a0818e 2904GNU Fortran does not support the @code{ENCODE} and @code{DECODE}
49309826
FXC
2905statements. These statements are best replaced by @code{READ} and
2906@code{WRITE} statements involving internal files (@code{CHARACTER}
2907variables and arrays), which have been part of the Fortran standard since
3994c6b1 2908Fortran 77. For example, replace a code fragment like
49309826
FXC
2909
2910@smallexample
2911 INTEGER*1 LINE(80)
2912 REAL A, B, C
2913c ... Code that sets LINE
2914 DECODE (80, 9000, LINE) A, B, C
2915 9000 FORMAT (1X, 3(F10.5))
2916@end smallexample
2917
2918@noindent
2919with the following:
2920
2921@smallexample
2922 CHARACTER(LEN=80) LINE
2923 REAL A, B, C
2924c ... Code that sets LINE
2925 READ (UNIT=LINE, FMT=9000) A, B, C
2926 9000 FORMAT (1X, 3(F10.5))
2927@end smallexample
2928
2929Similarly, replace a code fragment like
2930
2931@smallexample
2932 INTEGER*1 LINE(80)
2933 REAL A, B, C
2934c ... Code that sets A, B and C
2935 ENCODE (80, 9000, LINE) A, B, C
2936 9000 FORMAT (1X, 'OUTPUT IS ', 3(F10.5))
2937@end smallexample
2938
2939@noindent
2940with the following:
2941
2942@smallexample
3fe9e1ff 2943 CHARACTER(LEN=80) LINE
49309826
FXC
2944 REAL A, B, C
2945c ... Code that sets A, B and C
2946 WRITE (UNIT=LINE, FMT=9000) A, B, C
2947 9000 FORMAT (1X, 'OUTPUT IS ', 3(F10.5))
2948@end smallexample
2949
2950
2995ed9a
FXC
2951@node Variable FORMAT expressions
2952@subsection Variable @code{FORMAT} expressions
2953@cindex @code{FORMAT}
2954
2955A variable @code{FORMAT} expression is format statement which includes
3994c6b1
TB
2956angle brackets enclosing a Fortran expression: @code{FORMAT(I<N>)}. GNU
2957Fortran does not support this legacy extension. The effect of variable
2995ed9a 2958format expressions can be reproduced by using the more powerful (and
3994c6b1 2959standard) combination of internal output and string formats. For example,
2995ed9a
FXC
2960replace a code fragment like this:
2961
2962@smallexample
2963 WRITE(6,20) INT1
2964 20 FORMAT(I<N+1>)
2965@end smallexample
2966
2967@noindent
2968with the following:
2969
2970@smallexample
2971c Variable declaration
87187539 2972 CHARACTER(LEN=20) FMT
2995ed9a
FXC
2973c
2974c Other code here...
2975c
2976 WRITE(FMT,'("(I", I0, ")")') N+1
87187539 2977 WRITE(6,FMT) INT1
2995ed9a
FXC
2978@end smallexample
2979
2980@noindent
2981or with:
2982
2983@smallexample
2984c Variable declaration
2985 CHARACTER(LEN=20) FMT
2986c
2987c Other code here...
2988c
2989 WRITE(FMT,*) N+1
2990 WRITE(6,"(I" // ADJUSTL(FMT) // ")") INT1
2991@end smallexample
2992
2993
f14b9067
FXC
2994@node Alternate complex function syntax
2995@subsection Alternate complex function syntax
2996@cindex Complex function
2997
2998Some Fortran compilers, including @command{g77}, let the user declare
2999complex functions with the syntax @code{COMPLEX FUNCTION name*16()}, as
3994c6b1
TB
3000well as @code{COMPLEX*16 FUNCTION name()}. Both are non-standard, legacy
3001extensions. @command{gfortran} accepts the latter form, which is more
f14b9067
FXC
3002common, but not the former.
3003
3004
274c7b7b
DH
3005@node Volatile COMMON blocks
3006@subsection Volatile @code{COMMON} blocks
3007@cindex @code{VOLATILE}
3008@cindex @code{COMMON}
3009
3010Some Fortran compilers, including @command{g77}, let the user declare
3011@code{COMMON} with the @code{VOLATILE} attribute. This is
3012invalid standard Fortran syntax and is not supported by
3013@command{gfortran}. Note that @command{gfortran} accepts
3014@code{VOLATILE} variables in @code{COMMON} blocks since revision 4.3.
3015
3016
042c8364
DH
3017@node OPEN( ... NAME=)
3018@subsection @code{OPEN( ... NAME=)}
3019@cindex @code{NAM}
3020
3021Some Fortran compilers, including @command{g77}, let the user declare
3022@code{OPEN( ... NAME=)}. This is
3023invalid standard Fortran syntax and is not supported by
3024@command{gfortran}. @code{OPEN( ... NAME=)} should be replaced
3025with @code{OPEN( ... FILE=)}.
3026
3027
3028
3029@c ---------------------------------------------------------------------
9e0667cd
TB
3030@c ---------------------------------------------------------------------
3031@c Mixed-Language Programming
3032@c ---------------------------------------------------------------------
3033
3034@node Mixed-Language Programming
3035@chapter Mixed-Language Programming
3036@cindex Interoperability
3037@cindex Mixed-language programming
3038
3039@menu
3040* Interoperability with C::
08a6b8e0 3041* GNU Fortran Compiler Directives::
9e0667cd 3042* Non-Fortran Main Program::
83e03963 3043* Naming and argument-passing conventions::
9e0667cd
TB
3044@end menu
3045
3046This chapter is about mixed-language interoperability, but also applies
3994c6b1 3047if one links Fortran code compiled by different compilers. In most cases,
9e0667cd
TB
3048use of the C Binding features of the Fortran 2003 standard is sufficient,
3049and their use is highly recommended.
3050
3051
3052@node Interoperability with C
3053@section Interoperability with C
3054
3055@menu
3056* Intrinsic Types::
9e0667cd
TB
3057* Derived Types and struct::
3058* Interoperable Global Variables::
3059* Interoperable Subroutines and Functions::
da4dbc25
DK
3060* Working with Pointers::
3061* Further Interoperability of Fortran with C::
9e0667cd
TB
3062@end menu
3063
3064Since Fortran 2003 (ISO/IEC 1539-1:2004(E)) there is a
3065standardized way to generate procedure and derived-type
3066declarations and global variables which are interoperable with C
3994c6b1 3067(ISO/IEC 9899:1999). The @code{bind(C)} attribute has been added
9e0667cd 3068to inform the compiler that a symbol shall be interoperable with C;
3994c6b1
TB
3069also, some constraints are added. Note, however, that not
3070all C features have a Fortran equivalent or vice versa. For instance,
9e0667cd
TB
3071neither C's unsigned integers nor C's functions with variable number
3072of arguments have an equivalent in Fortran.
3073
c7d9f803 3074Note that array dimensions are reversely ordered in C and that arrays in
96c49324 3075C always start with index 0 while in Fortran they start by default with
3994c6b1 30761. Thus, an array declaration @code{A(n,m)} in Fortran matches
96c49324 3077@code{A[m][n]} in C and accessing the element @code{A(i,j)} matches
3994c6b1 3078@code{A[j-1][i-1]}. The element following @code{A(i,j)} (C: @code{A[j-1][i-1]};
96c49324 3079assuming @math{i < n}) in memory is @code{A(i+1,j)} (C: @code{A[j-1][i]}).
9e0667cd
TB
3080
3081@node Intrinsic Types
3082@subsection Intrinsic Types
3083
3084In order to ensure that exactly the same variable type and kind is used
3085in C and Fortran, the named constants shall be used which are defined in the
3994c6b1 3086@code{ISO_C_BINDING} intrinsic module. That module contains named constants
9e0667cd 3087for kind parameters and character named constants for the escape sequences
3994c6b1 3088in C. For a list of the constants, see @ref{ISO_C_BINDING}.
9e0667cd 3089
83e03963
TB
3090For logical types, please note that the Fortran standard only guarantees
3091interoperability between C99's @code{_Bool} and Fortran's @code{C_Bool}-kind
3092logicals and C99 defines that @code{true} has the value 1 and @code{false}
3093the value 0. Using any other integer value with GNU Fortran's @code{LOGICAL}
3094(with any kind parameter) gives an undefined result. (Passing other integer
3095values than 0 and 1 to GCC's @code{_Bool} is also undefined, unless the
3096integer is explicitly or implicitly casted to @code{_Bool}.)
3097
3098
3099
9e0667cd
TB
3100@node Derived Types and struct
3101@subsection Derived Types and struct
3102
3103For compatibility of derived types with @code{struct}, one needs to use
3994c6b1 3104the @code{BIND(C)} attribute in the type declaration. For instance, the
9e0667cd
TB
3105following type declaration
3106
3107@smallexample
3108 USE ISO_C_BINDING
3109 TYPE, BIND(C) :: myType
3110 INTEGER(C_INT) :: i1, i2
3111 INTEGER(C_SIGNED_CHAR) :: i3
3112 REAL(C_DOUBLE) :: d1
3113 COMPLEX(C_FLOAT_COMPLEX) :: c1
3114 CHARACTER(KIND=C_CHAR) :: str(5)
3115 END TYPE
3116@end smallexample
3117
3118matches the following @code{struct} declaration in C
3119
3120@smallexample
3121 struct @{
3122 int i1, i2;
3123 /* Note: "char" might be signed or unsigned. */
3124 signed char i3;
3125 double d1;
3126 float _Complex c1;
3127 char str[5];
3128 @} myType;
3129@end smallexample
3130
3131Derived types with the C binding attribute shall not have the @code{sequence}
3132attribute, type parameters, the @code{extends} attribute, nor type-bound
3994c6b1
TB
3133procedures. Every component must be of interoperable type and kind and may not
3134have the @code{pointer} or @code{allocatable} attribute. The names of the
3e508131 3135components are irrelevant for interoperability.
9e0667cd
TB
3136
3137As there exist no direct Fortran equivalents, neither unions nor structs
3138with bit field or variable-length array members are interoperable.
3139
3140@node Interoperable Global Variables
3141@subsection Interoperable Global Variables
3142
3143Variables can be made accessible from C using the C binding attribute,
3994c6b1 3144optionally together with specifying a binding name. Those variables
9e0667cd
TB
3145have to be declared in the declaration part of a @code{MODULE},
3146be of interoperable type, and have neither the @code{pointer} nor
3147the @code{allocatable} attribute.
3148
3149@smallexample
3150 MODULE m
3151 USE myType_module
3152 USE ISO_C_BINDING
3153 integer(C_INT), bind(C, name="_MyProject_flags") :: global_flag
3154 type(myType), bind(C) :: tp
3155 END MODULE
3156@end smallexample
3157
3158Here, @code{_MyProject_flags} is the case-sensitive name of the variable
3159as seen from C programs while @code{global_flag} is the case-insensitive
3994c6b1 3160name as seen from Fortran. If no binding name is specified, as for
9e0667cd
TB
3161@var{tp}, the C binding name is the (lowercase) Fortran binding name.
3162If a binding name is specified, only a single variable may be after the
3994c6b1 3163double colon. Note of warning: You cannot use a global variable to
9e0667cd 3164access @var{errno} of the C library as the C standard allows it to be
3994c6b1 3165a macro. Use the @code{IERRNO} intrinsic (GNU extension) instead.
9e0667cd
TB
3166
3167@node Interoperable Subroutines and Functions
3168@subsection Interoperable Subroutines and Functions
3169
3170Subroutines and functions have to have the @code{BIND(C)} attribute to
3994c6b1
TB
3171be compatible with C. The dummy argument declaration is relatively
3172straightforward. However, one needs to be careful because C uses
20460eb9 3173call-by-value by default while Fortran behaves usually similar to
3994c6b1 3174call-by-reference. Furthermore, strings and pointers are handled
3e508131
TB
3175differently. Note that in Fortran 2003 and 2008 only explicit size
3176and assumed-size arrays are supported but not assumed-shape or
3177deferred-shape (i.e. allocatable or pointer) arrays. However, those
3178are allowed since the Technical Specification 29113, see
3179@ref{Further Interoperability of Fortran with C}
9e0667cd
TB
3180
3181To pass a variable by value, use the @code{VALUE} attribute.
3e508131 3182Thus, the following C prototype
9e0667cd
TB
3183
3184@smallexample
3185@code{int func(int i, int *j)}
3186@end smallexample
3187
3188matches the Fortran declaration
3189
3190@smallexample
dae5882f
TB
3191 integer(c_int) function func(i,j)
3192 use iso_c_binding, only: c_int
3193 integer(c_int), VALUE :: i
3194 integer(c_int) :: j
9e0667cd
TB
3195@end smallexample
3196
da4dbc25
DK
3197Note that pointer arguments also frequently need the @code{VALUE} attribute,
3198see @ref{Working with Pointers}.
9e0667cd 3199
3994c6b1 3200Strings are handled quite differently in C and Fortran. In C a string
9e0667cd
TB
3201is a @code{NUL}-terminated array of characters while in Fortran each string
3202has a length associated with it and is thus not terminated (by e.g.
3994c6b1 3203@code{NUL}). For example, if one wants to use the following C function,
9e0667cd
TB
3204
3205@smallexample
3206 #include <stdio.h>
3207 void print_C(char *string) /* equivalent: char string[] */
3208 @{
3209 printf("%s\n", string);
3210 @}
3211@end smallexample
3212
3213to print ``Hello World'' from Fortran, one can call it using
3214
3215@smallexample
3216 use iso_c_binding, only: C_CHAR, C_NULL_CHAR
3217 interface
3218 subroutine print_c(string) bind(C, name="print_C")
3219 use iso_c_binding, only: c_char
3220 character(kind=c_char) :: string(*)
3221 end subroutine print_c
3222 end interface
3223 call print_c(C_CHAR_"Hello World"//C_NULL_CHAR)
3224@end smallexample
3225
3226As the example shows, one needs to ensure that the
3994c6b1 3227string is @code{NUL} terminated. Additionally, the dummy argument
9e0667cd 3228@var{string} of @code{print_C} is a length-one assumed-size
3994c6b1 3229array; using @code{character(len=*)} is not allowed. The example
9e0667cd
TB
3230above uses @code{c_char_"Hello World"} to ensure the string
3231literal has the right type; typically the default character
3232kind and @code{c_char} are the same and thus @code{"Hello World"}
3994c6b1 3233is equivalent. However, the standard does not guarantee this.
9e0667cd 3234
da4dbc25 3235The use of strings is now further illustrated using the C library
9e0667cd
TB
3236function @code{strncpy}, whose prototype is
3237
3238@smallexample
3239 char *strncpy(char *restrict s1, const char *restrict s2, size_t n);
3240@end smallexample
3241
3242The function @code{strncpy} copies at most @var{n} characters from
3994c6b1 3243string @var{s2} to @var{s1} and returns @var{s1}. In the following
9e0667cd
TB
3244example, we ignore the return value:
3245
3246@smallexample
3247 use iso_c_binding
3248 implicit none
3249 character(len=30) :: str,str2
3250 interface
3251 ! Ignore the return value of strncpy -> subroutine
3252 ! "restrict" is always assumed if we do not pass a pointer
3253 subroutine strncpy(dest, src, n) bind(C)
3254 import
3255 character(kind=c_char), intent(out) :: dest(*)
3256 character(kind=c_char), intent(in) :: src(*)
3257 integer(c_size_t), value, intent(in) :: n
3258 end subroutine strncpy
3259 end interface
3260 str = repeat('X',30) ! Initialize whole string with 'X'
3261 call strncpy(str, c_char_"Hello World"//C_NULL_CHAR, &
3262 len(c_char_"Hello World",kind=c_size_t))
3263 print '(a)', str ! prints: "Hello WorldXXXXXXXXXXXXXXXXXXX"
3264 end
3265@end smallexample
3266
da4dbc25
DK
3267The intrinsic procedures are described in @ref{Intrinsic Procedures}.
3268
3269@node Working with Pointers
3270@subsection Working with Pointers
3271
3272C pointers are represented in Fortran via the special opaque derived type
3994c6b1 3273@code{type(c_ptr)} (with private components). Thus one needs to
9e0667cd 3274use intrinsic conversion procedures to convert from or to C pointers.
3e508131
TB
3275
3276For some applications, using an assumed type (@code{TYPE(*)}) can be an
3277alternative to a C pointer; see
3278@ref{Further Interoperability of Fortran with C}.
3279
9e0667cd
TB
3280For example,
3281
3282@smallexample
3283 use iso_c_binding
3284 type(c_ptr) :: cptr1, cptr2
3285 integer, target :: array(7), scalar
3286 integer, pointer :: pa(:), ps
3287 cptr1 = c_loc(array(1)) ! The programmer needs to ensure that the
3288 ! array is contiguous if required by the C
3289 ! procedure
3290 cptr2 = c_loc(scalar)
3291 call c_f_pointer(cptr2, ps)
3292 call c_f_pointer(cptr2, pa, shape=[7])
3293@end smallexample
3294
3295When converting C to Fortran arrays, the one-dimensional @code{SHAPE} argument
da4dbc25
DK
3296has to be passed.
3297
3298If a pointer is a dummy-argument of an interoperable procedure, it usually
3299has to be declared using the @code{VALUE} attribute. @code{void*}
3300matches @code{TYPE(C_PTR), VALUE}, while @code{TYPE(C_PTR)} alone
3301matches @code{void**}.
9e0667cd
TB
3302
3303Procedure pointers are handled analogously to pointers; the C type is
3304@code{TYPE(C_FUNPTR)} and the intrinsic conversion procedures are
da4dbc25 3305@code{C_F_PROCPOINTER} and @code{C_FUNLOC}.
9e0667cd 3306
c5a0818e 3307Let us consider two examples of actually passing a procedure pointer from
da4dbc25 3308C to Fortran and vice versa. Note that these examples are also very
c5a0818e
FXC
3309similar to passing ordinary pointers between both languages. First,
3310consider this code in C:
da4dbc25
DK
3311
3312@smallexample
3313/* Procedure implemented in Fortran. */
3314void get_values (void (*)(double));
3315
3316/* Call-back routine we want called from Fortran. */
3317void
3318print_it (double x)
3319@{
3320 printf ("Number is %f.\n", x);
3321@}
3322
3323/* Call Fortran routine and pass call-back to it. */
3324void
3325foobar ()
3326@{
3327 get_values (&print_it);
3328@}
3329@end smallexample
3330
3331A matching implementation for @code{get_values} in Fortran, that correctly
3332receives the procedure pointer from C and is able to call it, is given
3333in the following @code{MODULE}:
3334
3335@smallexample
3336MODULE m
3337 IMPLICIT NONE
3338
3339 ! Define interface of call-back routine.
3340 ABSTRACT INTERFACE
3341 SUBROUTINE callback (x)
3342 USE, INTRINSIC :: ISO_C_BINDING
3343 REAL(KIND=C_DOUBLE), INTENT(IN), VALUE :: x
3344 END SUBROUTINE callback
3345 END INTERFACE
3346
3347CONTAINS
3348
3349 ! Define C-bound procedure.
3350 SUBROUTINE get_values (cproc) BIND(C)
3351 USE, INTRINSIC :: ISO_C_BINDING
3352 TYPE(C_FUNPTR), INTENT(IN), VALUE :: cproc
3353
3354 PROCEDURE(callback), POINTER :: proc
3355
3356 ! Convert C to Fortran procedure pointer.
3357 CALL C_F_PROCPOINTER (cproc, proc)
3358
3359 ! Call it.
3360 CALL proc (1.0_C_DOUBLE)
3361 CALL proc (-42.0_C_DOUBLE)
3362 CALL proc (18.12_C_DOUBLE)
3363 END SUBROUTINE get_values
3364
3365END MODULE m
3366@end smallexample
3367
3368Next, we want to call a C routine that expects a procedure pointer argument
3369and pass it a Fortran procedure (which clearly must be interoperable!).
3370Again, the C function may be:
3371
3372@smallexample
3373int
3374call_it (int (*func)(int), int arg)
3375@{
3376 return func (arg);
3377@}
3378@end smallexample
3379
3380It can be used as in the following Fortran code:
3381
3382@smallexample
3383MODULE m
3384 USE, INTRINSIC :: ISO_C_BINDING
3385 IMPLICIT NONE
3386
3387 ! Define interface of C function.
3388 INTERFACE
3389 INTEGER(KIND=C_INT) FUNCTION call_it (func, arg) BIND(C)
3390 USE, INTRINSIC :: ISO_C_BINDING
3391 TYPE(C_FUNPTR), INTENT(IN), VALUE :: func
3392 INTEGER(KIND=C_INT), INTENT(IN), VALUE :: arg
3393 END FUNCTION call_it
3394 END INTERFACE
3395
3396CONTAINS
3397
3398 ! Define procedure passed to C function.
3399 ! It must be interoperable!
3400 INTEGER(KIND=C_INT) FUNCTION double_it (arg) BIND(C)
3401 INTEGER(KIND=C_INT), INTENT(IN), VALUE :: arg
3402 double_it = arg + arg
3403 END FUNCTION double_it
3404
3405 ! Call C function.
3406 SUBROUTINE foobar ()
3407 TYPE(C_FUNPTR) :: cproc
3408 INTEGER(KIND=C_INT) :: i
3409
3410 ! Get C procedure pointer.
3411 cproc = C_FUNLOC (double_it)
3412
3413 ! Use it.
3414 DO i = 1_C_INT, 10_C_INT
3415 PRINT *, call_it (cproc, i)
3416 END DO
3417 END SUBROUTINE foobar
3418
3419END MODULE m
3420@end smallexample
9e0667cd
TB
3421
3422@node Further Interoperability of Fortran with C
3423@subsection Further Interoperability of Fortran with C
3424
3e508131
TB
3425The Technical Specification ISO/IEC TS 29113:2012 on further
3426interoperability of Fortran with C extends the interoperability support
3427of Fortran 2003 and Fortran 2008. Besides removing some restrictions
3428and constraints, it adds assumed-type (@code{TYPE(*)}) and assumed-rank
3429(@code{dimension}) variables and allows for interoperability of
3430assumed-shape, assumed-rank and deferred-shape arrays, including
3431allocatables and pointers.
3432
3433Note: Currently, GNU Fortran does not support the array descriptor
3434(dope vector) as specified in the Technical Specification, but uses
3435an array descriptor with different fields. The Chasm Language
9e0667cd 3436Interoperability Tools, @url{http://chasm-interop.sourceforge.net/},
3e508131
TB
3437provide an interface to GNU Fortran's array descriptor.
3438
3439The Technical Specification adds the following new features, which
3440are supported by GNU Fortran:
9e0667cd 3441
3e508131 3442@itemize @bullet
9e0667cd 3443
3e508131
TB
3444@item The @code{ASYNCHRONOUS} attribute has been clarified and
3445extended to allow its use with asynchronous communication in
3446user-provided libraries such as in implementations of the
3447Message Passing Interface specification.
3448
3449@item Many constraints have been relaxed, in particular for
3450the @code{C_LOC} and @code{C_F_POINTER} intrinsics.
3451
3452@item The @code{OPTIONAL} attribute is now allowed for dummy
3453arguments; an absent argument matches a @code{NULL} pointer.
3454
3455@item Assumed types (@code{TYPE(*)}) have been added, which may
3456only be used for dummy arguments. They are unlimited polymorphic
3457but contrary to @code{CLASS(*)} they do not contain any type
3458information, similar to C's @code{void *} pointers. Expressions
3459of any type and kind can be passed; thus, it can be used as
3460replacement for @code{TYPE(C_PTR)}, avoiding the use of
3461@code{C_LOC} in the caller.
3462
3463Note, however, that @code{TYPE(*)} only accepts scalar arguments,
3464unless the @code{DIMENSION} is explicitly specified. As
3465@code{DIMENSION(*)} only supports array (including array elements) but
3466no scalars, it is not a full replacement for @code{C_LOC}. On the
3467other hand, assumed-type assumed-rank dummy arguments
3468(@code{TYPE(*), DIMENSION(..)}) allow for both scalars and arrays, but
3469require special code on the callee side to handle the array descriptor.
3470
10180dd3 3471@item Assumed-rank arrays (@code{DIMENSION(..)}) as dummy argument
3e508131
TB
3472allow that scalars and arrays of any rank can be passed as actual
3473argument. As the Technical Specification does not provide for direct
3474means to operate with them, they have to be used either from the C side
3475or be converted using @code{C_LOC} and @code{C_F_POINTER} to scalars
3476or arrays of a specific rank. The rank can be determined using the
3477@code{RANK} intrinisic.
3478@end itemize
3479
3480
3481Currently unimplemented:
3482
3483@itemize @bullet
3484
3485@item GNU Fortran always uses an array descriptor, which does not
3486match the one of the Technical Specification. The
3487@code{ISO_Fortran_binding.h} header file and the C functions it
3488specifies are not available.
3489
3490@item Using assumed-shape, assumed-rank and deferred-shape arrays in
3491@code{BIND(C)} procedures is not fully supported. In particular,
3492C interoperable strings of other length than one are not supported
3493as this requires the new array descriptor.
3494@end itemize
08a6b8e0
TB
3495
3496
3497@node GNU Fortran Compiler Directives
3498@section GNU Fortran Compiler Directives
3499
170a8bd6
EB
3500@menu
3501* ATTRIBUTES directive::
3502* UNROLL directive::
3503@end menu
3504
3505@node ATTRIBUTES directive
3506@subsection ATTRIBUTES directive
3507
1f1c0dbd 3508The Fortran standard describes how a conforming program shall
3994c6b1 3509behave; however, the exact implementation is not standardized. In order
08a6b8e0
TB
3510to allow the user to choose specific implementation details, compiler
3511directives can be used to set attributes of variables and procedures
3994c6b1 3512which are not part of the standard. Whether a given attribute is
08a6b8e0
TB
3513supported and its exact effects depend on both the operating system and
3514on the processor; see
3515@ref{Top,,C Extensions,gcc,Using the GNU Compiler Collection (GCC)}
3516for details.
3517
3518For procedures and procedure pointers, the following attributes can
3519be used to change the calling convention:
3520
3521@itemize
3522@item @code{CDECL} -- standard C calling convention
3523@item @code{STDCALL} -- convention where the called procedure pops the stack
3524@item @code{FASTCALL} -- part of the arguments are passed via registers
3525instead using the stack
3526@end itemize
3527
3528Besides changing the calling convention, the attributes also influence
3529the decoration of the symbol name, e.g., by a leading underscore or by
3994c6b1 3530a trailing at-sign followed by the number of bytes on the stack. When
08a6b8e0
TB
3531assigning a procedure to a procedure pointer, both should use the same
3532calling convention.
3533
3534On some systems, procedures and global variables (module variables and
3535@code{COMMON} blocks) need special handling to be accessible when they
3994c6b1 3536are in a shared library. The following attributes are available:
08a6b8e0
TB
3537
3538@itemize
3539@item @code{DLLEXPORT} -- provide a global pointer to a pointer in the DLL
bc0229f9
TB
3540@item @code{DLLIMPORT} -- reference the function or variable using a
3541global pointer
08a6b8e0
TB
3542@end itemize
3543
e7ac6a7c
TB
3544For dummy arguments, the @code{NO_ARG_CHECK} attribute can be used; in
3545other compilers, it is also known as @code{IGNORE_TKR}. For dummy arguments
3546with this attribute actual arguments of any type and kind (similar to
3547@code{TYPE(*)}), scalars and arrays of any rank (no equivalent
3548in Fortran standard) are accepted. As with @code{TYPE(*)}, the argument
3549is unlimited polymorphic and no type information is available.
86307f49
TB
3550Additionally, the argument may only be passed to dummy arguments
3551with the @code{NO_ARG_CHECK} attribute and as argument to the
3552@code{PRESENT} intrinsic function and to @code{C_LOC} of the
3553@code{ISO_C_BINDING} module.
e7ac6a7c
TB
3554
3555Variables with @code{NO_ARG_CHECK} attribute shall be of assumed-type
86307f49
TB
3556(@code{TYPE(*)}; recommended) or of type @code{INTEGER}, @code{LOGICAL},
3557@code{REAL} or @code{COMPLEX}. They shall not have the @code{ALLOCATE},
3558@code{CODIMENSION}, @code{INTENT(OUT)}, @code{POINTER} or @code{VALUE}
3559attribute; furthermore, they shall be either scalar or of assumed-size
3560(@code{dimension(*)}). As @code{TYPE(*)}, the @code{NO_ARG_CHECK} attribute
3561requires an explicit interface.
e7ac6a7c
TB
3562
3563@itemize
3564@item @code{NO_ARG_CHECK} -- disable the type, kind and rank checking
3565@end itemize
3566
3567
08a6b8e0
TB
3568The attributes are specified using the syntax
3569
3570@code{!GCC$ ATTRIBUTES} @var{attribute-list} @code{::} @var{variable-list}
3571
3572where in free-form source code only whitespace is allowed before @code{!GCC$}
3573and in fixed-form source code @code{!GCC$}, @code{cGCC$} or @code{*GCC$} shall
3574start in the first column.
3575
3576For procedures, the compiler directives shall be placed into the body
3577of the procedure; for variables and procedure pointers, they shall be in
3578the same declaration part as the variable or procedure pointer.
3579
3580
170a8bd6
EB
3581@node UNROLL directive
3582@subsection UNROLL directive
3583
3584The syntax of the directive is
3585
3586@code{!GCC$ unroll N}
3587
3588You can use this directive to control how many times a loop should be unrolled.
3589It must be placed immediately before a @code{DO} loop and applies only to the
3590loop that follows. N is an integer constant specifying the unrolling factor.
3591The values of 0 and 1 block any unrolling of the loop.
3592
3593
08a6b8e0 3594
9e0667cd
TB
3595@node Non-Fortran Main Program
3596@section Non-Fortran Main Program
3597
3598@menu
3599* _gfortran_set_args:: Save command-line arguments
3600* _gfortran_set_options:: Set library option flags
3601* _gfortran_set_convert:: Set endian conversion
3602* _gfortran_set_record_marker:: Set length of record markers
96c49324 3603* _gfortran_set_fpe:: Set when a Floating Point Exception should be raised
58edd811 3604* _gfortran_set_max_subrecord_length:: Set subrecord length
9e0667cd
TB
3605@end menu
3606
3607Even if you are doing mixed-language programming, it is very
3608likely that you do not need to know or use the information in this
3994c6b1 3609section. Since it is about the internal structure of GNU Fortran,
9e0667cd
TB
3610it may also change in GCC minor releases.
3611
3612When you compile a @code{PROGRAM} with GNU Fortran, a function
3613with the name @code{main} (in the symbol table of the object file)
3614is generated, which initializes the libgfortran library and then
3615calls the actual program which uses the name @code{MAIN__}, for
3994c6b1 3616historic reasons. If you link GNU Fortran compiled procedures
9e0667cd
TB
3617to, e.g., a C or C++ program or to a Fortran program compiled by
3618a different compiler, the libgfortran library is not initialized
3619and thus a few intrinsic procedures do not work properly, e.g.
3620those for obtaining the command-line arguments.
3621
3622Therefore, if your @code{PROGRAM} is not compiled with
3623GNU Fortran and the GNU Fortran compiled procedures require
3624intrinsics relying on the library initialization, you need to
3994c6b1 3625initialize the library yourself. Using the default options,
9e0667cd 3626gfortran calls @code{_gfortran_set_args} and
3994c6b1 3627@code{_gfortran_set_options}. The initialization of the former
9e0667cd
TB
3628is needed if the called procedures access the command line
3629(and for backtracing); the latter sets some flags based on the
4e4c4f41 3630standard chosen or to enable backtracing. In typical programs,
9e0667cd
TB
3631it is not necessary to call any initialization function.
3632
3633If your @code{PROGRAM} is compiled with GNU Fortran, you shall
3994c6b1 3634not call any of the following functions. The libgfortran
9e0667cd
TB
3635initialization functions are shown in C syntax but using C
3636bindings they are also accessible from Fortran.
3637
3638
3639@node _gfortran_set_args
3640@subsection @code{_gfortran_set_args} --- Save command-line arguments
3641@fnindex _gfortran_set_args
3642@cindex libgfortran initialization, set_args
3643
3644@table @asis
3645@item @emph{Description}:
3646@code{_gfortran_set_args} saves the command-line arguments; this
3647initialization is required if any of the command-line intrinsics
3994c6b1 3648is called. Additionally, it shall be called if backtracing is
9e0667cd
TB
3649enabled (see @code{_gfortran_set_options}).
3650
3651@item @emph{Syntax}:
3652@code{void _gfortran_set_args (int argc, char *argv[])}
3653
3654@item @emph{Arguments}:
3655@multitable @columnfractions .15 .70
3656@item @var{argc} @tab number of command line argument strings
3657@item @var{argv} @tab the command-line argument strings; argv[0]
3658is the pathname of the executable itself.
3659@end multitable
3660
3661@item @emph{Example}:
3662@smallexample
3663int main (int argc, char *argv[])
3664@{
3665 /* Initialize libgfortran. */
3666 _gfortran_set_args (argc, argv);
3667 return 0;
3668@}
3669@end smallexample
3670@end table
3671
3672
3673@node _gfortran_set_options
3674@subsection @code{_gfortran_set_options} --- Set library option flags
3675@fnindex _gfortran_set_options
3676@cindex libgfortran initialization, set_options
3677
3678@table @asis
3679@item @emph{Description}:
3680@code{_gfortran_set_options} sets several flags related to the Fortran
7daa7b1d 3681standard to be used, whether backtracing should be enabled
3994c6b1 3682and whether range checks should be performed. The syntax allows for
9e0667cd 3683upward compatibility since the number of passed flags is specified; for
3994c6b1
TB
3684non-passed flags, the default value is used. See also
3685@pxref{Code Gen Options}. Please note that not all flags are actually
9e0667cd
TB
3686used.
3687
3688@item @emph{Syntax}:
3689@code{void _gfortran_set_options (int num, int options[])}
3690
3691@item @emph{Arguments}:
3692@multitable @columnfractions .15 .70
3693@item @var{num} @tab number of options passed
3694@item @var{argv} @tab The list of flag values
3695@end multitable
3696
3697@item @emph{option flag list}:
3698@multitable @columnfractions .15 .70
3699@item @var{option}[0] @tab Allowed standard; can give run-time errors
8179b067
JB
3700if e.g. an input-output edit descriptor is invalid in a given
3701standard. Possible values are (bitwise or-ed) @code{GFC_STD_F77} (1),
3702@code{GFC_STD_F95_OBS} (2), @code{GFC_STD_F95_DEL} (4),
3703@code{GFC_STD_F95} (8), @code{GFC_STD_F2003} (16), @code{GFC_STD_GNU}
3704(32), @code{GFC_STD_LEGACY} (64), @code{GFC_STD_F2008} (128),
3705@code{GFC_STD_F2008_OBS} (256), @code{GFC_STD_F2008_TS} (512),
3706@code{GFC_STD_F2018} (1024), @code{GFC_STD_F2018_OBS} (2048), and
3707@code{GFC_STD=F2018_DEL} (4096). Default: @code{GFC_STD_F95_OBS |
3708GFC_STD_F95_DEL | GFC_STD_F95 | GFC_STD_F2003 | GFC_STD_F2008 |
3709GFC_STD_F2008_TS | GFC_STD_F2008_OBS | GFC_STD_F77 | GFC_STD_F2018 |
3710GFC_STD_F2018_OBS | GFC_STD_F2018_DEL | GFC_STD_GNU | GFC_STD_LEGACY}.
9e0667cd 3711@item @var{option}[1] @tab Standard-warning flag; prints a warning to
3994c6b1 3712standard error. Default: @code{GFC_STD_F95_DEL | GFC_STD_LEGACY}.
9e0667cd
TB
3713@item @var{option}[2] @tab If non zero, enable pedantic checking.
3714Default: off.
7daa7b1d 3715@item @var{option}[3] @tab Unused.
9e0667cd 3716@item @var{option}[4] @tab If non zero, enable backtracing on run-time
fa86f4f9 3717errors. Default: off. (Default in the compiler: on.)
9e0667cd
TB
3718Note: Installs a signal handler and requires command-line
3719initialization using @code{_gfortran_set_args}.
3720@item @var{option}[5] @tab If non zero, supports signed zeros.
3721Default: enabled.
3994c6b1 3722@item @var{option}[6] @tab Enables run-time checking. Possible values
9e0667cd 3723are (bitwise or-ed): GFC_RTCHECK_BOUNDS (1), GFC_RTCHECK_ARRAY_TEMPS (2),
20460eb9 3724GFC_RTCHECK_RECURSION (4), GFC_RTCHECK_DO (16), GFC_RTCHECK_POINTER (32).
9e0667cd 3725Default: disabled.
fa86f4f9
TB
3726@item @var{option}[7] @tab Unused.
3727@item @var{option}[8] @tab Show a warning when invoking @code{STOP} and
3728@code{ERROR STOP} if a floating-point exception occurred. Possible values
3729are (bitwise or-ed) @code{GFC_FPE_INVALID} (1), @code{GFC_FPE_DENORMAL} (2),
3730@code{GFC_FPE_ZERO} (4), @code{GFC_FPE_OVERFLOW} (8),
3731@code{GFC_FPE_UNDERFLOW} (16), @code{GFC_FPE_INEXACT} (32). Default: None (0).
3732(Default in the compiler: @code{GFC_FPE_INVALID | GFC_FPE_DENORMAL |
3733GFC_FPE_ZERO | GFC_FPE_OVERFLOW | GFC_FPE_UNDERFLOW}.)
9e0667cd
TB
3734@end multitable
3735
3736@item @emph{Example}:
3737@smallexample
fa86f4f9
TB
3738 /* Use gfortran 4.9 default options. */
3739 static int options[] = @{68, 511, 0, 0, 1, 1, 0, 0, 31@};
3740 _gfortran_set_options (9, &options);
9e0667cd
TB
3741@end smallexample
3742@end table
3743
3744
3745@node _gfortran_set_convert
3746@subsection @code{_gfortran_set_convert} --- Set endian conversion
3747@fnindex _gfortran_set_convert
3748@cindex libgfortran initialization, set_convert
3749
3750@table @asis
3751@item @emph{Description}:
3752@code{_gfortran_set_convert} set the representation of data for
3753unformatted files.
3754
3755@item @emph{Syntax}:
3756@code{void _gfortran_set_convert (int conv)}
3757
3758@item @emph{Arguments}:
3759@multitable @columnfractions .15 .70
3760@item @var{conv} @tab Endian conversion, possible values:
3761GFC_CONVERT_NATIVE (0, default), GFC_CONVERT_SWAP (1),
3762GFC_CONVERT_BIG (2), GFC_CONVERT_LITTLE (3).
3763@end multitable
3764
3765@item @emph{Example}:
3766@smallexample
3767int main (int argc, char *argv[])
3768@{
3769 /* Initialize libgfortran. */
3770 _gfortran_set_args (argc, argv);
3771 _gfortran_set_convert (1);
3772 return 0;
3773@}
3774@end smallexample
3775@end table
3776
3777
3778@node _gfortran_set_record_marker
3779@subsection @code{_gfortran_set_record_marker} --- Set length of record markers
3780@fnindex _gfortran_set_record_marker
3781@cindex libgfortran initialization, set_record_marker
3782
3783@table @asis
3784@item @emph{Description}:
96c49324 3785@code{_gfortran_set_record_marker} sets the length of record markers
9e0667cd
TB
3786for unformatted files.
3787
3788@item @emph{Syntax}:
3789@code{void _gfortran_set_record_marker (int val)}
3790
3791@item @emph{Arguments}:
3792@multitable @columnfractions .15 .70
3793@item @var{val} @tab Length of the record marker; valid values
3994c6b1 3794are 4 and 8. Default is 4.
9e0667cd
TB
3795@end multitable
3796
3797@item @emph{Example}:
3798@smallexample
3799int main (int argc, char *argv[])
3800@{
3801 /* Initialize libgfortran. */
3802 _gfortran_set_args (argc, argv);
3803 _gfortran_set_record_marker (8);
3804 return 0;
3805@}
3806@end smallexample
3807@end table
3808
3809
96c49324 3810@node _gfortran_set_fpe
57b4d355 3811@subsection @code{_gfortran_set_fpe} --- Enable floating point exception traps
96c49324
TB
3812@fnindex _gfortran_set_fpe
3813@cindex libgfortran initialization, set_fpe
3814
3815@table @asis
3816@item @emph{Description}:
57b4d355
JB
3817@code{_gfortran_set_fpe} enables floating point exception traps for
3818the specified exceptions. On most systems, this will result in a
3819SIGFPE signal being sent and the program being aborted.
96c49324
TB
3820
3821@item @emph{Syntax}:
3822@code{void _gfortran_set_fpe (int val)}
3823
3824@item @emph{Arguments}:
3825@multitable @columnfractions .15 .70
3994c6b1 3826@item @var{option}[0] @tab IEEE exceptions. Possible values are
96c49324
TB
3827(bitwise or-ed) zero (0, default) no trapping,
3828@code{GFC_FPE_INVALID} (1), @code{GFC_FPE_DENORMAL} (2),
3829@code{GFC_FPE_ZERO} (4), @code{GFC_FPE_OVERFLOW} (8),
57b4d355 3830@code{GFC_FPE_UNDERFLOW} (16), and @code{GFC_FPE_INEXACT} (32).
96c49324
TB
3831@end multitable
3832
3833@item @emph{Example}:
3834@smallexample
3835int main (int argc, char *argv[])
3836@{
3837 /* Initialize libgfortran. */
3838 _gfortran_set_args (argc, argv);
3839 /* FPE for invalid operations such as SQRT(-1.0). */
3840 _gfortran_set_fpe (1);
3841 return 0;
3842@}
3843@end smallexample
3844@end table
3845
3846
9e0667cd
TB
3847@node _gfortran_set_max_subrecord_length
3848@subsection @code{_gfortran_set_max_subrecord_length} --- Set subrecord length
3849@fnindex _gfortran_set_max_subrecord_length
3850@cindex libgfortran initialization, set_max_subrecord_length
3851
3852@table @asis
3853@item @emph{Description}:
3854@code{_gfortran_set_max_subrecord_length} set the maximum length
3994c6b1 3855for a subrecord. This option only makes sense for testing and
9e0667cd
TB
3856debugging of unformatted I/O.
3857
3858@item @emph{Syntax}:
3859@code{void _gfortran_set_max_subrecord_length (int val)}
3860
3861@item @emph{Arguments}:
3862@multitable @columnfractions .15 .70
3863@item @var{val} @tab the maximum length for a subrecord;
3864the maximum permitted value is 2147483639, which is also
3865the default.
3866@end multitable
3867
3868@item @emph{Example}:
3869@smallexample
3870int main (int argc, char *argv[])
3871@{
3872 /* Initialize libgfortran. */
3873 _gfortran_set_args (argc, argv);
3874 _gfortran_set_max_subrecord_length (8);
3875 return 0;
3876@}
3877@end smallexample
3878@end table
3879
2995ed9a 3880
83e03963
TB
3881@node Naming and argument-passing conventions
3882@section Naming and argument-passing conventions
3883
3884This section gives an overview about the naming convention of procedures
3885and global variables and about the argument passing conventions used by
3886GNU Fortran. If a C binding has been specified, the naming convention
3887and some of the argument-passing conventions change. If possible,
3888mixed-language and mixed-compiler projects should use the better defined
3889C binding for interoperability. See @pxref{Interoperability with C}.
3890
3891@menu
3892* Naming conventions::
3893* Argument passing conventions::
3894@end menu
3895
3896
3897@node Naming conventions
3898@subsection Naming conventions
3899
3900According the Fortran standard, valid Fortran names consist of a letter
3901between @code{A} to @code{Z}, @code{a} to @code{z}, digits @code{0},
3902@code{1} to @code{9} and underscores (@code{_}) with the restriction
3903that names may only start with a letter. As vendor extension, the
3904dollar sign (@code{$}) is additionally permitted with the option
3905@option{-fdollar-ok}, but not as first character and only if the
3906target system supports it.
3907
3908By default, the procedure name is the lower-cased Fortran name with an
3909appended underscore (@code{_}); using @option{-fno-underscoring} no
3910underscore is appended while @code{-fsecond-underscore} appends two
3911underscores. Depending on the target system and the calling convention,
3912the procedure might be additionally dressed; for instance, on 32bit
3913Windows with @code{stdcall}, an at-sign @code{@@} followed by an integer
3914number is appended. For the changing the calling convention, see
3915@pxref{GNU Fortran Compiler Directives}.
3916
3917For common blocks, the same convention is used, i.e. by default an
3918underscore is appended to the lower-cased Fortran name. Blank commons
3919have the name @code{__BLNK__}.
3920
3921For procedures and variables declared in the specification space of a
3922module, the name is formed by @code{__}, followed by the lower-cased
3923module name, @code{_MOD_}, and the lower-cased Fortran name. Note that
3924no underscore is appended.
3925
3926
3927@node Argument passing conventions
3928@subsection Argument passing conventions
3929
3930Subroutines do not return a value (matching C99's @code{void}) while
3931functions either return a value as specified in the platform ABI or
3932the result variable is passed as hidden argument to the function and
3933no result is returned. A hidden result variable is used when the
3934result variable is an array or of type @code{CHARACTER}.
3935
3936Arguments are passed according to the platform ABI. In particular,
3937complex arguments might not be compatible to a struct with two real
3938components for the real and imaginary part. The argument passing
3939matches the one of C99's @code{_Complex}. Functions with scalar
3940complex result variables return their value and do not use a
3941by-reference argument. Note that with the @option{-ff2c} option,
3942the argument passing is modified and no longer completely matches
3943the platform ABI. Some other Fortran compilers use @code{f2c}
3944semantic by default; this might cause problems with
bc0229f9 3945interoperablility.
83e03963
TB
3946
3947GNU Fortran passes most arguments by reference, i.e. by passing a
3948pointer to the data. Note that the compiler might use a temporary
3949variable into which the actual argument has been copied, if required
3950semantically (copy-in/copy-out).
3951
3952For arguments with @code{ALLOCATABLE} and @code{POINTER}
3953attribute (including procedure pointers), a pointer to the pointer
3954is passed such that the pointer address can be modified in the
3955procedure.
3956
3957For dummy arguments with the @code{VALUE} attribute: Scalar arguments
3958of the type @code{INTEGER}, @code{LOGICAL}, @code{REAL} and
3959@code{COMPLEX} are passed by value according to the platform ABI.
3960(As vendor extension and not recommended, using @code{%VAL()} in the
3961call to a procedure has the same effect.) For @code{TYPE(C_PTR)} and
3962procedure pointers, the pointer itself is passed such that it can be
3963modified without affecting the caller.
3964@c FIXME: Document how VALUE is handled for CHARACTER, TYPE,
3965@c CLASS and arrays, i.e. whether the copy-in is done in the caller
3966@c or in the callee.
3967
3968For Boolean (@code{LOGICAL}) arguments, please note that GCC expects
3969only the integer value 0 and 1. If a GNU Fortran @code{LOGICAL}
3970variable contains another integer value, the result is undefined.
3971As some other Fortran compilers use @math{-1} for @code{.TRUE.},
3972extra care has to be taken -- such as passing the value as
3973@code{INTEGER}. (The same value restriction also applies to other
3974front ends of GCC, e.g. to GCC's C99 compiler for @code{_Bool}
3975or GCC's Ada compiler for @code{Boolean}.)
3976
3977For arguments of @code{CHARACTER} type, the character length is passed
f622221a
JB
3978as a hidden argument at the end of the argument list. For
3979deferred-length strings, the value is passed by reference, otherwise
3980by value. The character length has the C type @code{size_t} (or
3981@code{INTEGER(kind=C_SIZE_T)} in Fortran). Note that this is
3982different to older versions of the GNU Fortran compiler, where the
3983type of the hidden character length argument was a C @code{int}. In
3984order to retain compatibility with older versions, one can e.g. for
3985the following Fortran procedure
3986
3987@smallexample
3988subroutine fstrlen (s, a)
3989 character(len=*) :: s
3990 integer :: a
3991 print*, len(s)
3992end subroutine fstrlen
3993@end smallexample
3994
3995define the corresponding C prototype as follows:
3996
3997@smallexample
3998#if __GNUC__ > 7
3999typedef size_t fortran_charlen_t;
4000#else
4001typedef int fortran_charlen_t;
4002#endif
4003
4004void fstrlen_ (char*, int*, fortran_charlen_t);
4005@end smallexample
4006
4007In order to avoid such compiler-specific details, for new code it is
4008instead recommended to use the ISO_C_BINDING feature.
4009
4010Note with C binding, @code{CHARACTER(len=1)} result variables are
4011returned according to the platform ABI and no hidden length argument
4012is used for dummy arguments; with @code{VALUE}, those variables are
4013passed by value.
83e03963
TB
4014
4015For @code{OPTIONAL} dummy arguments, an absent argument is denoted
4016by a NULL pointer, except for scalar dummy arguments of type
4017@code{INTEGER}, @code{LOGICAL}, @code{REAL} and @code{COMPLEX}
4018which have the @code{VALUE} attribute. For those, a hidden Boolean
4019argument (@code{logical(kind=C_bool),value}) is used to indicate
4020whether the argument is present.
4021
4022Arguments which are assumed-shape, assumed-rank or deferred-rank
4023arrays or, with @option{-fcoarray=lib}, allocatable scalar coarrays use
4024an array descriptor. All other arrays pass the address of the
4025first element of the array. With @option{-fcoarray=lib}, the token
4026and the offset belonging to nonallocatable coarrays dummy arguments
4027are passed as hidden argument along the character length hidden
4028arguments. The token is an oparque pointer identifying the coarray
4029and the offset is a passed-by-value integer of kind @code{C_PTRDIFF_T},
4030denoting the byte offset between the base address of the coarray and
4031the passed scalar or first element of the passed array.
4032
4033The arguments are passed in the following order
4034@itemize @bullet
4035@item Result variable, when the function result is passed by reference
4036@item Character length of the function result, if it is a of type
4037@code{CHARACTER} and no C binding is used
4038@item The arguments in the order in which they appear in the Fortran
4039declaration
4040@item The the present status for optional arguments with value attribute,
4041which are internally passed by value
4042@item The character length and/or coarray token and offset for the first
4043argument which is a @code{CHARACTER} or a nonallocatable coarray dummy
4044argument, followed by the hidden arguments of the next dummy argument
4045of such a type
4046@end itemize
4047
4048
c194537c
TB
4049@c ---------------------------------------------------------------------
4050@c Coarray Programming
4051@c ---------------------------------------------------------------------
4052
4053@node Coarray Programming
4054@chapter Coarray Programming
4055@cindex Coarrays
4056
4057@menu
4058* Type and enum ABI Documentation::
4059* Function ABI Documentation::
4060@end menu
4061
4062
4063@node Type and enum ABI Documentation
4064@section Type and enum ABI Documentation
4065
4066@menu
4067* caf_token_t::
4068* caf_register_t::
ba85c8c3 4069* caf_deregister_t::
3c9f5092 4070* caf_reference_t::
ef78bc3c 4071* caf_team_t::
c194537c
TB
4072@end menu
4073
4074@node caf_token_t
4075@subsection @code{caf_token_t}
4076
4077Typedef of type @code{void *} on the compiler side. Can be any data
4078type on the library side.
4079
4080@node caf_register_t
4081@subsection @code{caf_register_t}
4082
4083Indicates which kind of coarray variable should be registered.
4084
4085@verbatim
4086typedef enum caf_register_t {
4087 CAF_REGTYPE_COARRAY_STATIC,
4088 CAF_REGTYPE_COARRAY_ALLOC,
4089 CAF_REGTYPE_LOCK_STATIC,
bc0229f9 4090 CAF_REGTYPE_LOCK_ALLOC,
5df445a2
TB
4091 CAF_REGTYPE_CRITICAL,
4092 CAF_REGTYPE_EVENT_STATIC,
ba85c8c3
AV
4093 CAF_REGTYPE_EVENT_ALLOC,
4094 CAF_REGTYPE_COARRAY_ALLOC_REGISTER_ONLY,
4095 CAF_REGTYPE_COARRAY_ALLOC_ALLOCATE_ONLY
c194537c
TB
4096}
4097caf_register_t;
4098@end verbatim
4099
ba85c8c3
AV
4100The values @code{CAF_REGTYPE_COARRAY_ALLOC_REGISTER_ONLY} and
4101@code{CAF_REGTYPE_COARRAY_ALLOC_ALLOCATE_ONLY} are for allocatable components
4102in derived type coarrays only. The first one sets up the token without
4103allocating memory for allocatable component. The latter one only allocates the
4104memory for an allocatable component in a derived type coarray. The token
4105needs to be setup previously by the REGISTER_ONLY. This allows to have
4106allocatable components un-allocated on some images. The status whether an
4107allocatable component is allocated on a remote image can be queried by
4108@code{_caf_is_present} which used internally by the @code{ALLOCATED}
4109intrinsic.
4110
4111@node caf_deregister_t
4112@subsection @code{caf_deregister_t}
4113
4114@verbatim
4115typedef enum caf_deregister_t {
4116 CAF_DEREGTYPE_COARRAY_DEREGISTER,
4117 CAF_DEREGTYPE_COARRAY_DEALLOCATE_ONLY
4118}
4119caf_deregister_t;
4120@end verbatim
4121
4122Allows to specifiy the type of deregistration of a coarray object. The
4123@code{CAF_DEREGTYPE_COARRAY_DEALLOCATE_ONLY} flag is only allowed for
4124allocatable components in derived type coarrays.
4125
3c9f5092
AV
4126@node caf_reference_t
4127@subsection @code{caf_reference_t}
4128
4129The structure used for implementing arbitrary reference chains.
4130A @code{CAF_REFERENCE_T} allows to specify a component reference or any kind
4131of array reference of any rank supported by gfortran. For array references all
4132kinds as known by the compiler/Fortran standard are supported indicated by
4133a @code{MODE}.
4134
4135@verbatim
4136typedef enum caf_ref_type_t {
4137 /* Reference a component of a derived type, either regular one or an
4138 allocatable or pointer type. For regular ones idx in caf_reference_t is
4139 set to -1. */
4140 CAF_REF_COMPONENT,
4141 /* Reference an allocatable array. */
4142 CAF_REF_ARRAY,
4143 /* Reference a non-allocatable/non-pointer array. I.e., the coarray object
4144 has no array descriptor associated and the addressing is done
4145 completely using the ref. */
4146 CAF_REF_STATIC_ARRAY
4147} caf_ref_type_t;
4148@end verbatim
4149
4150@verbatim
4151typedef enum caf_array_ref_t {
4152 /* No array ref. This terminates the array ref. */
4153 CAF_ARR_REF_NONE = 0,
4154 /* Reference array elements given by a vector. Only for this mode
4155 caf_reference_t.u.a.dim[i].v is valid. */
4156 CAF_ARR_REF_VECTOR,
4157 /* A full array ref (:). */
4158 CAF_ARR_REF_FULL,
4159 /* Reference a range on elements given by start, end and stride. */
4160 CAF_ARR_REF_RANGE,
4161 /* Only a single item is referenced given in the start member. */
4162 CAF_ARR_REF_SINGLE,
4163 /* An array ref of the kind (i:), where i is an arbitrary valid index in the
4164 array. The index i is given in the start member. */
4165 CAF_ARR_REF_OPEN_END,
4166 /* An array ref of the kind (:i), where the lower bound of the array ref
4167 is given by the remote side. The index i is given in the end member. */
4168 CAF_ARR_REF_OPEN_START
4169} caf_array_ref_t;
4170@end verbatim
4171
4172@verbatim
4173/* References to remote components of a derived type. */
4174typedef struct caf_reference_t {
4175 /* A pointer to the next ref or NULL. */
4176 struct caf_reference_t *next;
4177 /* The type of the reference. */
4178 /* caf_ref_type_t, replaced by int to allow specification in fortran FE. */
4179 int type;
4180 /* The size of an item referenced in bytes. I.e. in an array ref this is
4181 the factor to advance the array pointer with to get to the next item.
4182 For component refs this gives just the size of the element referenced. */
4183 size_t item_size;
4184 union {
4185 struct {
4186 /* The offset (in bytes) of the component in the derived type.
4187 Unused for allocatable or pointer components. */
4188 ptrdiff_t offset;
4189 /* The offset (in bytes) to the caf_token associated with this
4190 component. NULL, when not allocatable/pointer ref. */
4191 ptrdiff_t caf_token_offset;
4192 } c;
4193 struct {
4194 /* The mode of the array ref. See CAF_ARR_REF_*. */
4195 /* caf_array_ref_t, replaced by unsigend char to allow specification in
4196 fortran FE. */
4197 unsigned char mode[GFC_MAX_DIMENSIONS];
4198 /* The type of a static array. Unset for array's with descriptors. */
4199 int static_array_type;
4200 /* Subscript refs (s) or vector refs (v). */
4201 union {
4202 struct {
4203 /* The start and end boundary of the ref and the stride. */
4204 index_type start, end, stride;
4205 } s;
4206 struct {
4207 /* nvec entries of kind giving the elements to reference. */
4208 void *vector;
4209 /* The number of entries in vector. */
4210 size_t nvec;
4211 /* The integer kind used for the elements in vector. */
4212 int kind;
4213 } v;
4214 } dim[GFC_MAX_DIMENSIONS];
4215 } a;
4216 } u;
4217} caf_reference_t;
4218@end verbatim
4219
4220The references make up a single linked list of reference operations. The
4221@code{NEXT} member links to the next reference or NULL to indicate the end of
4222the chain. Component and array refs can be arbitrarly mixed as long as they
4223comply to the Fortran standard.
4224
4225@emph{NOTES}
4226The member @code{STATIC_ARRAY_TYPE} is used only when the @code{TYPE} is
4227@code{CAF_REF_STATIC_ARRAY}. The member gives the type of the data referenced.
4228Because no array descriptor is available for a descriptor-less array and
4229type conversion still needs to take place the type is transported here.
4230
4231At the moment @code{CAF_ARR_REF_VECTOR} is not implemented in the front end for
4232descriptor-less arrays. The library caf_single has untested support for it.
4233
ef78bc3c
AV
4234@node caf_team_t
4235@subsection @code{caf_team_t}
4236
4237Opaque pointer to represent a team-handle. This type is a stand-in for the
4238future implementation of teams. It is about to change without further notice.
c194537c
TB
4239
4240@node Function ABI Documentation
4241@section Function ABI Documentation
4242
4243@menu
4244* _gfortran_caf_init:: Initialiation function
4245* _gfortran_caf_finish:: Finalization function
4246* _gfortran_caf_this_image:: Querying the image number
4247* _gfortran_caf_num_images:: Querying the maximal number of images
ef78bc3c
AV
4248* _gfortran_caf_image_status :: Query the status of an image
4249* _gfortran_caf_failed_images :: Get an array of the indexes of the failed images
4250* _gfortran_caf_stopped_images :: Get an array of the indexes of the stopped images
c194537c
TB
4251* _gfortran_caf_register:: Registering coarrays
4252* _gfortran_caf_deregister:: Deregistering coarrays
29dbb95a 4253* _gfortran_caf_is_present:: Query whether an allocatable or pointer component in a derived type coarray is allocated
c194537c
TB
4254* _gfortran_caf_send:: Sending data from a local image to a remote image
4255* _gfortran_caf_get:: Getting data from a remote image
4256* _gfortran_caf_sendget:: Sending data between remote images
3c9f5092
AV
4257* _gfortran_caf_send_by_ref:: Sending data from a local image to a remote image using enhanced references
4258* _gfortran_caf_get_by_ref:: Getting data from a remote image using enhanced references
4259* _gfortran_caf_sendget_by_ref:: Sending data between remote images using enhanced references
bc0229f9
TB
4260* _gfortran_caf_lock:: Locking a lock variable
4261* _gfortran_caf_unlock:: Unlocking a lock variable
5df445a2
TB
4262* _gfortran_caf_event_post:: Post an event
4263* _gfortran_caf_event_wait:: Wait that an event occurred
4264* _gfortran_caf_event_query:: Query event count
2691415b
TB
4265* _gfortran_caf_sync_all:: All-image barrier
4266* _gfortran_caf_sync_images:: Barrier for selected images
4267* _gfortran_caf_sync_memory:: Wait for completion of segment-memory operations
4268* _gfortran_caf_error_stop:: Error termination with exit code
4269* _gfortran_caf_error_stop_str:: Error termination with string
ef78bc3c 4270* _gfortran_caf_fail_image :: Mark the image failed and end its execution
2691415b
TB
4271* _gfortran_caf_atomic_define:: Atomic variable assignment
4272* _gfortran_caf_atomic_ref:: Atomic variable reference
4273* _gfortran_caf_atomic_cas:: Atomic compare and swap
4274* _gfortran_caf_atomic_op:: Atomic operation
229c5919
TB
4275* _gfortran_caf_co_broadcast:: Sending data to all images
4276* _gfortran_caf_co_max:: Collective maximum reduction
4277* _gfortran_caf_co_min:: Collective minimum reduction
4278* _gfortran_caf_co_sum:: Collective summing reduction
4279* _gfortran_caf_co_reduce:: Generic collective reduction
c194537c
TB
4280@end menu
4281
4282
4283@node _gfortran_caf_init
4284@subsection @code{_gfortran_caf_init} --- Initialiation function
4285@cindex Coarray, _gfortran_caf_init
4286
4287@table @asis
4288@item @emph{Description}:
4289This function is called at startup of the program before the Fortran main
4290program, if the latter has been compiled with @option{-fcoarray=lib}.
4291It takes as arguments the command-line arguments of the program. It is
29dbb95a 4292permitted to pass two @code{NULL} pointers as argument; if non-@code{NULL},
c194537c
TB
4293the library is permitted to modify the arguments.
4294
4295@item @emph{Syntax}:
4296@code{void _gfortran_caf_init (int *argc, char ***argv)}
4297
4298@item @emph{Arguments}:
4299@multitable @columnfractions .15 .70
4300@item @var{argc} @tab intent(inout) An integer pointer with the number of
4301arguments passed to the program or @code{NULL}.
4302@item @var{argv} @tab intent(inout) A pointer to an array of strings with the
4303command-line arguments or @code{NULL}.
4304@end multitable
4305
4306@item @emph{NOTES}
4307The function is modelled after the initialization function of the Message
4308Passing Interface (MPI) specification. Due to the way coarray registration
29dbb95a 4309works, it might not be the first call to the library. If the main program is
c194537c
TB
4310not written in Fortran and only a library uses coarrays, it can happen that
4311this function is never called. Therefore, it is recommended that the library
4312does not rely on the passed arguments and whether the call has been done.
4313@end table
4314
4315
4316@node _gfortran_caf_finish
4317@subsection @code{_gfortran_caf_finish} --- Finalization function
4318@cindex Coarray, _gfortran_caf_finish
4319
4320@table @asis
4321@item @emph{Description}:
4322This function is called at the end of the Fortran main program, if it has
4323been compiled with the @option{-fcoarray=lib} option.
4324
4325@item @emph{Syntax}:
4326@code{void _gfortran_caf_finish (void)}
4327
4328@item @emph{NOTES}
4329For non-Fortran programs, it is recommended to call the function at the end
4330of the main program. To ensure that the shutdown is also performed for
4331programs where this function is not explicitly invoked, for instance
4332non-Fortran programs or calls to the system's exit() function, the library
4333can use a destructor function. Note that programs can also be terminated
4334using the STOP and ERROR STOP statements; those use different library calls.
4335@end table
4336
4337
4338@node _gfortran_caf_this_image
4339@subsection @code{_gfortran_caf_this_image} --- Querying the image number
4340@cindex Coarray, _gfortran_caf_this_image
4341
4342@table @asis
4343@item @emph{Description}:
4344This function returns the current image number, which is a positive number.
4345
4346@item @emph{Syntax}:
4347@code{int _gfortran_caf_this_image (int distance)}
4348
4349@item @emph{Arguments}:
4350@multitable @columnfractions .15 .70
4351@item @var{distance} @tab As specified for the @code{this_image} intrinsic
29dbb95a 4352in TS18508. Shall be a non-negative number.
c194537c
TB
4353@end multitable
4354
4355@item @emph{NOTES}
4356If the Fortran intrinsic @code{this_image} is invoked without an argument, which
4357is the only permitted form in Fortran 2008, GCC passes @code{0} as
4358first argument.
4359@end table
4360
4361
4362@node _gfortran_caf_num_images
4363@subsection @code{_gfortran_caf_num_images} --- Querying the maximal number of images
4364@cindex Coarray, _gfortran_caf_num_images
4365
4366@table @asis
4367@item @emph{Description}:
4368This function returns the number of images in the current team, if
4369@var{distance} is 0 or the number of images in the parent team at the specified
4370distance. If failed is -1, the function returns the number of all images at
4371the specified distance; if it is 0, the function returns the number of
4372nonfailed images, and if it is 1, it returns the number of failed images.
4373
4374@item @emph{Syntax}:
4375@code{int _gfortran_caf_num_images(int distance, int failed)}
4376
4377@item @emph{Arguments}:
4378@multitable @columnfractions .15 .70
4379@item @var{distance} @tab the distance from this image to the ancestor.
4380Shall be positive.
4381@item @var{failed} @tab shall be -1, 0, or 1
4382@end multitable
4383
4384@item @emph{NOTES}
4385This function follows TS18508. If the num_image intrinsic has no arguments,
29dbb95a 4386then the compiler passes @code{distance=0} and @code{failed=-1} to the function.
c194537c
TB
4387@end table
4388
4389
ef78bc3c
AV
4390@node _gfortran_caf_image_status
4391@subsection @code{_gfortran_caf_image_status} --- Query the status of an image
4392@cindex Coarray, _gfortran_caf_image_status
4393
4394@table @asis
4395@item @emph{Description}:
4396Get the status of the image given by the id @var{image} of the team given by
4397@var{team}. Valid results are zero, for image is ok, @code{STAT_STOPPED_IMAGE}
4398from the ISO_FORTRAN_ENV module to indicate that the image has been stopped and
4399@code{STAT_FAILED_IMAGE} also from ISO_FORTRAN_ENV to indicate that the image
4400has executed a @code{FAIL IMAGE} statement.
4401
4402@item @emph{Syntax}:
4403@code{int _gfortran_caf_image_status (int image, caf_team_t * team)}
4404
4405@item @emph{Arguments}:
4406@multitable @columnfractions .15 .70
4407@item @var{image} @tab the positive scalar id of the image in the current TEAM.
4408@item @var{team} @tab optional; team on the which the inquiry is to be
4409performed.
4410@end multitable
4411
4412@item @emph{NOTES}
4413This function follows TS18508. Because team-functionality is not yet
4414implemented a null-pointer is passed for the @var{team} argument at the moment.
4415@end table
4416
4417
4418@node _gfortran_caf_failed_images
4419@subsection @code{_gfortran_caf_failed_images} --- Get an array of the indexes of the failed images
4420@cindex Coarray, _gfortran_caf_failed_images
4421
4422@table @asis
4423@item @emph{Description}:
4424Get an array of image indexes in the current @var{team} that have failed. The
4425array is sorted ascendingly. When @var{team} is not provided the current team
4426is to be used. When @var{kind} is provided then the resulting array is of that
4427integer kind else it is of default integer kind. The returns an unallocated
4428size zero array when no images have failed.
4429
4430@item @emph{Syntax}:
4431@code{int _gfortran_caf_failed_images (caf_team_t * team, int * kind)}
4432
4433@item @emph{Arguments}:
4434@multitable @columnfractions .15 .70
4435@item @var{team} @tab optional; team on the which the inquiry is to be
4436performed.
4437@item @var{image} @tab optional; the kind of the resulting integer array.
4438@end multitable
4439
4440@item @emph{NOTES}
4441This function follows TS18508. Because team-functionality is not yet
4442implemented a null-pointer is passed for the @var{team} argument at the moment.
4443@end table
4444
4445
4446@node _gfortran_caf_stopped_images
4447@subsection @code{_gfortran_caf_stopped_images} --- Get an array of the indexes of the stopped images
4448@cindex Coarray, _gfortran_caf_stopped_images
4449
4450@table @asis
4451@item @emph{Description}:
4452Get an array of image indexes in the current @var{team} that have stopped. The
4453array is sorted ascendingly. When @var{team} is not provided the current team
4454is to be used. When @var{kind} is provided then the resulting array is of that
4455integer kind else it is of default integer kind. The returns an unallocated
4456size zero array when no images have failed.
4457
4458@item @emph{Syntax}:
4459@code{int _gfortran_caf_stopped_images (caf_team_t * team, int * kind)}
4460
4461@item @emph{Arguments}:
4462@multitable @columnfractions .15 .70
4463@item @var{team} @tab optional; team on the which the inquiry is to be
4464performed.
4465@item @var{image} @tab optional; the kind of the resulting integer array.
4466@end multitable
4467
4468@item @emph{NOTES}
4469This function follows TS18508. Because team-functionality is not yet
4470implemented a null-pointer is passed for the @var{team} argument at the moment.
4471@end table
4472
4473
c194537c
TB
4474@node _gfortran_caf_register
4475@subsection @code{_gfortran_caf_register} --- Registering coarrays
3c9f5092 4476@cindex Coarray, _gfortran_caf_register
c194537c
TB
4477
4478@table @asis
4479@item @emph{Description}:
3c9f5092
AV
4480Registers memory for a coarray and creates a token to identify the coarray. The
4481routine is called for both coarrays with @code{SAVE} attribute and using an
4482explicit @code{ALLOCATE} statement. If an error occurs and @var{STAT} is a
c194537c
TB
4483@code{NULL} pointer, the function shall abort with printing an error message
4484and starting the error termination. If no error occurs and @var{STAT} is
3c9f5092 4485present, it shall be set to zero. Otherwise, it shall be set to a positive
c194537c 4486value and, if not-@code{NULL}, @var{ERRMSG} shall be set to a string describing
3c9f5092
AV
4487the failure. The routine shall register the memory provided in the
4488@code{DATA}-component of the array descriptor @var{DESC}, when that component
4489is non-@code{NULL}, else it shall allocate sufficient memory and provide a
4490pointer to it in the @code{DATA}-component of @var{DESC}. The array descriptor
4491has rank zero, when a scalar object is to be registered and the array
4492descriptor may be invalid after the call to @code{_gfortran_caf_register}.
4493When an array is to be allocated the descriptor persists.
c194537c 4494
bc0229f9
TB
4495For @code{CAF_REGTYPE_COARRAY_STATIC} and @code{CAF_REGTYPE_COARRAY_ALLOC},
4496the passed size is the byte size requested. For @code{CAF_REGTYPE_LOCK_STATIC},
4497@code{CAF_REGTYPE_LOCK_ALLOC} and @code{CAF_REGTYPE_CRITICAL} it is the array
4498size or one for a scalar.
4499
29dbb95a
AV
4500When @code{CAF_REGTYPE_COARRAY_ALLOC_REGISTER_ONLY} is used, then only a token
4501for an allocatable or pointer component is created. The @code{SIZE} parameter
4502is not used then. On the contrary when
4503@code{CAF_REGTYPE_COARRAY_ALLOC_ALLOCATE_ONLY} is specified, then the
4504@var{token} needs to be registered by a previous call with regtype
4505@code{CAF_REGTYPE_COARRAY_ALLOC_REGISTER_ONLY} and either the memory specified
61fad608
AV
4506in the @var{DESC}'s data-ptr is registered or allocate when the data-ptr is
4507@code{NULL}.
bc0229f9 4508
c194537c 4509@item @emph{Syntax}:
3c9f5092 4510@code{void caf_register (size_t size, caf_register_t type, caf_token_t *token,
3f5fabc0 4511gfc_descriptor_t *desc, int *stat, char *errmsg, size_t errmsg_len)}
c194537c
TB
4512
4513@item @emph{Arguments}:
4514@multitable @columnfractions .15 .70
bc0229f9 4515@item @var{size} @tab For normal coarrays, the byte size of the coarray to be
5df445a2 4516allocated; for lock types and event types, the number of elements.
c194537c
TB
4517@item @var{type} @tab one of the caf_register_t types.
4518@item @var{token} @tab intent(out) An opaque pointer identifying the coarray.
3c9f5092 4519@item @var{desc} @tab intent(inout) The (pseudo) array descriptor.
bc0229f9 4520@item @var{stat} @tab intent(out) For allocatable coarrays, stores the STAT=;
61fad608 4521may be @code{NULL}
bc0229f9 4522@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to
61fad608 4523an error message; may be @code{NULL}
c194537c
TB
4524@item @var{errmsg_len} @tab the buffer size of errmsg.
4525@end multitable
4526
4527@item @emph{NOTES}
61fad608 4528Nonallocatable coarrays have to be registered prior use from remote images.
c194537c
TB
4529In order to guarantee this, they have to be registered before the main
4530program. This can be achieved by creating constructor functions. That is what
61fad608
AV
4531GCC does such that also for nonallocatable coarrays the memory is allocated and
4532no static memory is used. The token permits to identify the coarray; to the
c194537c
TB
4533processor, the token is a nonaliasing pointer. The library can, for instance,
4534store the base address of the coarray in the token, some handle or a more
3c9f5092
AV
4535complicated struct. The library may also store the array descriptor
4536@var{DESC} when its rank is non-zero.
bc0229f9 4537
61fad608 4538For lock types, the value shall only be used for checking the allocation
bc0229f9 4539status. Note that for critical blocks, the locking is only required on one
3c9f5092 4540image; in the locking statement, the processor shall always pass an
bc0229f9 4541image index of one for critical-block lock variables
5df445a2
TB
4542(@code{CAF_REGTYPE_CRITICAL}). For lock types and critical-block variables,
4543the initial value shall be unlocked (or, respecitively, not in critical
4544section) such as the value false; for event types, the initial state should
4545be no event, e.g. zero.
c194537c
TB
4546@end table
4547
ba85c8c3 4548
c194537c
TB
4549@node _gfortran_caf_deregister
4550@subsection @code{_gfortran_caf_deregister} --- Deregistering coarrays
4551@cindex Coarray, _gfortran_caf_deregister
4552
4553@table @asis
4554@item @emph{Description}:
29dbb95a
AV
4555Called to free or deregister the memory of a coarray; the processor calls this
4556function for automatic and explicit deallocation. In case of an error, this
4557function shall fail with an error message, unless the @var{STAT} variable is
4558not null. The library is only expected to free memory it allocated itself
4559during a call to @code{_gfortran_caf_register}.
c194537c
TB
4560
4561@item @emph{Syntax}:
ba85c8c3 4562@code{void caf_deregister (caf_token_t *token, caf_deregister_t type,
3f5fabc0 4563int *stat, char *errmsg, size_t errmsg_len)}
c194537c
TB
4564
4565@item @emph{Arguments}:
4566@multitable @columnfractions .15 .70
3c9f5092 4567@item @var{token} @tab the token to free.
ba85c8c3 4568@item @var{type} @tab the type of action to take for the coarray. A
29dbb95a
AV
4569@code{CAF_DEREGTYPE_COARRAY_DEALLOCATE_ONLY} is allowed only for allocatable or
4570pointer components of derived type coarrays. The action only deallocates the
4571local memory without deleting the token.
5df445a2 4572@item @var{stat} @tab intent(out) Stores the STAT=; may be NULL
bc0229f9
TB
4573@item @var{errmsg} @tab intent(out) When an error occurs, this will be set
4574to an error message; may be NULL
c194537c
TB
4575@item @var{errmsg_len} @tab the buffer size of errmsg.
4576@end multitable
4577
4578@item @emph{NOTES}
4579For nonalloatable coarrays this function is never called. If a cleanup is
4580required, it has to be handled via the finish, stop and error stop functions,
4581and via destructors.
4582@end table
4583
4584
ba85c8c3 4585@node _gfortran_caf_is_present
29dbb95a 4586@subsection @code{_gfortran_caf_is_present} --- Query whether an allocatable or pointer component in a derived type coarray is allocated
ba85c8c3
AV
4587@cindex Coarray, _gfortran_caf_is_present
4588
4589@table @asis
4590@item @emph{Description}:
4591Used to query the coarray library whether an allocatable component in a derived
4592type coarray is allocated on a remote image.
4593
4594@item @emph{Syntax}:
4595@code{void _gfortran_caf_is_present (caf_token_t token, int image_index,
4596gfc_reference_t *ref)}
4597
4598@item @emph{Arguments}:
4599@multitable @columnfractions .15 .70
4600@item @var{token} @tab An opaque pointer identifying the coarray.
4601@item @var{image_index} @tab The ID of the remote image; must be a positive
4602number.
29dbb95a
AV
4603@item @var{ref} @tab A chain of references to address the allocatable or
4604pointer component in the derived type coarray. The object reference needs to be
4605a scalar or a full array reference, respectively.
ba85c8c3
AV
4606@end multitable
4607
4608@end table
4609
c194537c
TB
4610@node _gfortran_caf_send
4611@subsection @code{_gfortran_caf_send} --- Sending data from a local image to a remote image
4612@cindex Coarray, _gfortran_caf_send
4613
4614@table @asis
4615@item @emph{Description}:
29dbb95a 4616Called to send a scalar, an array section or a whole array from a local
c194537c
TB
4617to a remote image identified by the image_index.
4618
4619@item @emph{Syntax}:
4620@code{void _gfortran_caf_send (caf_token_t token, size_t offset,
4621int image_index, gfc_descriptor_t *dest, caf_vector_t *dst_vector,
29dbb95a
AV
4622gfc_descriptor_t *src, int dst_kind, int src_kind, bool may_require_tmp,
4623int *stat)}
c194537c
TB
4624
4625@item @emph{Arguments}:
4626@multitable @columnfractions .15 .70
4627@item @var{token} @tab intent(in) An opaque pointer identifying the coarray.
29dbb95a
AV
4628@item @var{offset} @tab intent(in) By which amount of bytes the actual data is
4629shifted compared to the base address of the coarray.
4630@item @var{image_index} @tab intent(in) The ID of the remote image; must be a
4631positive number.
4632@item @var{dest} @tab intent(in) Array descriptor for the remote image for the
4633bounds and the size. The @code{base_addr} shall not be accessed.
4971dd80 4634@item @var{dst_vector} @tab intent(in) If not NULL, it contains the vector
c194537c
TB
4635subscript of the destination array; the values are relative to the dimension
4636triplet of the dest argument.
29dbb95a 4637@item @var{src} @tab intent(in) Array descriptor of the local array to be
c194537c 4638transferred to the remote image
29dbb95a
AV
4639@item @var{dst_kind} @tab intent(in) Kind of the destination argument
4640@item @var{src_kind} @tab intent(in) Kind of the source argument
4641@item @var{may_require_tmp} @tab intent(in) The variable is @code{false} when
4642it is known at compile time that the @var{dest} and @var{src} either cannot
4643overlap or overlap (fully or partially) such that walking @var{src} and
4644@var{dest} in element wise element order (honoring the stride value) will not
4645lead to wrong results. Otherwise, the value is @code{true}.
4646@item @var{stat} @tab intent(out) when non-NULL give the result of the
4647operation, i.e., zero on success and non-zero on error. When NULL and an error
4648occurs, then an error message is printed and the program is terminated.
c194537c
TB
4649@end multitable
4650
4651@item @emph{NOTES}
29dbb95a
AV
4652It is permitted to have @var{image_index} equal the current image; the memory
4653of the send-to and the send-from might (partially) overlap in that case. The
93e2e046
TB
4654implementation has to take care that it handles this case, e.g. using
4655@code{memmove} which handles (partially) overlapping memory. If
4656@var{may_require_tmp} is true, the library might additionally create a
4657temporary variable, unless additional checks show that this is not required
4658(e.g. because walking backward is possible or because both arrays are
4659contiguous and @code{memmove} takes care of overlap issues).
4660
4661Note that the assignment of a scalar to an array is permitted. In addition,
4662the library has to handle numeric-type conversion and for strings, padding
4663and different character kinds.
c194537c
TB
4664@end table
4665
4666
4667@node _gfortran_caf_get
4668@subsection @code{_gfortran_caf_get} --- Getting data from a remote image
4669@cindex Coarray, _gfortran_caf_get
4670
4671@table @asis
4672@item @emph{Description}:
29dbb95a 4673Called to get an array section or a whole array from a remote,
c194537c
TB
4674image identified by the image_index.
4675
4676@item @emph{Syntax}:
4971dd80 4677@code{void _gfortran_caf_get (caf_token_t token, size_t offset,
c194537c 4678int image_index, gfc_descriptor_t *src, caf_vector_t *src_vector,
29dbb95a
AV
4679gfc_descriptor_t *dest, int src_kind, int dst_kind, bool may_require_tmp,
4680int *stat)}
c194537c
TB
4681
4682@item @emph{Arguments}:
4683@multitable @columnfractions .15 .70
4684@item @var{token} @tab intent(in) An opaque pointer identifying the coarray.
29dbb95a
AV
4685@item @var{offset} @tab intent(in) By which amount of bytes the actual data is
4686shifted compared to the base address of the coarray.
4687@item @var{image_index} @tab intent(in) The ID of the remote image; must be a
4688positive number.
4971dd80 4689@item @var{dest} @tab intent(out) Array descriptor of the local array to store
29dbb95a 4690the data retrieved from the remote image
c194537c 4691@item @var{src} @tab intent(in) Array descriptor for the remote image for the
29dbb95a 4692bounds and the size. The @code{base_addr} shall not be accessed.
4971dd80
AV
4693@item @var{src_vector} @tab intent(in) If not NULL, it contains the vector
4694subscript of the source array; the values are relative to the dimension
29dbb95a
AV
4695triplet of the @var{src} argument.
4696@item @var{dst_kind} @tab intent(in) Kind of the destination argument
4697@item @var{src_kind} @tab intent(in) Kind of the source argument
4698@item @var{may_require_tmp} @tab intent(in) The variable is @code{false} when
4699it is known at compile time that the @var{dest} and @var{src} either cannot
4700overlap or overlap (fully or partially) such that walking @var{src} and
4701@var{dest} in element wise element order (honoring the stride value) will not
4702lead to wrong results. Otherwise, the value is @code{true}.
4703@item @var{stat} @tab intent(out) When non-NULL give the result of the
4704operation, i.e., zero on success and non-zero on error. When NULL and an error
4705occurs, then an error message is printed and the program is terminated.
c194537c
TB
4706@end multitable
4707
4708@item @emph{NOTES}
29dbb95a
AV
4709It is permitted to have @var{image_index} equal the current image; the memory of
4710the send-to and the send-from might (partially) overlap in that case. The
93e2e046
TB
4711implementation has to take care that it handles this case, e.g. using
4712@code{memmove} which handles (partially) overlapping memory. If
4713@var{may_require_tmp} is true, the library might additionally create a
4714temporary variable, unless additional checks show that this is not required
4715(e.g. because walking backward is possible or because both arrays are
4716contiguous and @code{memmove} takes care of overlap issues).
4717
4718Note that the library has to handle numeric-type conversion and for strings,
4719padding and different character kinds.
c194537c
TB
4720@end table
4721
4722
4723@node _gfortran_caf_sendget
4724@subsection @code{_gfortran_caf_sendget} --- Sending data between remote images
4725@cindex Coarray, _gfortran_caf_sendget
4726
4727@table @asis
4728@item @emph{Description}:
29dbb95a
AV
4729Called to send a scalar, an array section or a whole array from a remote image
4730identified by the @var{src_image_index} to a remote image identified by the
4731@var{dst_image_index}.
c194537c
TB
4732
4733@item @emph{Syntax}:
4734@code{void _gfortran_caf_sendget (caf_token_t dst_token, size_t dst_offset,
4735int dst_image_index, gfc_descriptor_t *dest, caf_vector_t *dst_vector,
4736caf_token_t src_token, size_t src_offset, int src_image_index,
93e2e046 4737gfc_descriptor_t *src, caf_vector_t *src_vector, int dst_kind, int src_kind,
29dbb95a 4738bool may_require_tmp, int *stat)}
c194537c
TB
4739
4740@item @emph{Arguments}:
4741@multitable @columnfractions .15 .70
4742@item @var{dst_token} @tab intent(in) An opaque pointer identifying the
4743destination coarray.
29dbb95a
AV
4744@item @var{dst_offset} @tab intent(in) By which amount of bytes the actual data
4745is shifted compared to the base address of the destination coarray.
4746@item @var{dst_image_index} @tab intent(in) The ID of the destination remote
4747image; must be a positive number.
93e2e046 4748@item @var{dest} @tab intent(in) Array descriptor for the destination
29dbb95a
AV
4749remote image for the bounds and the size. The @code{base_addr} shall not be
4750accessed.
c194537c
TB
4751@item @var{dst_vector} @tab intent(int) If not NULL, it contains the vector
4752subscript of the destination array; the values are relative to the dimension
29dbb95a
AV
4753triplet of the @var{dest} argument.
4754@item @var{src_token} @tab intent(in) An opaque pointer identifying the source
4755coarray.
4756@item @var{src_offset} @tab intent(in) By which amount of bytes the actual data
4757is shifted compared to the base address of the source coarray.
4758@item @var{src_image_index} @tab intent(in) The ID of the source remote image;
4759must be a positive number.
93e2e046 4760@item @var{src} @tab intent(in) Array descriptor of the local array to be
c194537c
TB
4761transferred to the remote image.
4762@item @var{src_vector} @tab intent(in) Array descriptor of the local array to
4763be transferred to the remote image
29dbb95a
AV
4764@item @var{dst_kind} @tab intent(in) Kind of the destination argument
4765@item @var{src_kind} @tab intent(in) Kind of the source argument
4766@item @var{may_require_tmp} @tab intent(in) The variable is @code{false} when
4767it is known at compile time that the @var{dest} and @var{src} either cannot
4768overlap or overlap (fully or partially) such that walking @var{src} and
4769@var{dest} in element wise element order (honoring the stride value) will not
4770lead to wrong results. Otherwise, the value is @code{true}.
4771@item @var{stat} @tab intent(out) when non-NULL give the result of the
4772operation, i.e., zero on success and non-zero on error. When NULL and an error
4773occurs, then an error message is printed and the program is terminated.
c194537c
TB
4774@end multitable
4775
4776@item @emph{NOTES}
29dbb95a
AV
4777It is permitted to have the same image index for both @var{src_image_index} and
4778@var{dst_image_index}; the memory of the send-to and the send-from might
4779(partially) overlap in that case. The implementation has to take care that it
4780handles this case, e.g. using @code{memmove} which handles (partially)
4781overlapping memory. If @var{may_require_tmp} is true, the library
93e2e046
TB
4782might additionally create a temporary variable, unless additional checks show
4783that this is not required (e.g. because walking backward is possible or because
4784both arrays are contiguous and @code{memmove} takes care of overlap issues).
4785
4786Note that the assignment of a scalar to an array is permitted. In addition,
4787the library has to handle numeric-type conversion and for strings, padding and
4788different character kinds.
c194537c
TB
4789@end table
4790
3c9f5092
AV
4791@node _gfortran_caf_send_by_ref
4792@subsection @code{_gfortran_caf_send_by_ref} --- Sending data from a local image to a remote image with enhanced referencing options
4793@cindex Coarray, _gfortran_caf_send_by_ref
4794
4795@table @asis
4796@item @emph{Description}:
29dbb95a
AV
4797Called to send a scalar, an array section or a whole array from a local to a
4798remote image identified by the @var{image_index}.
3c9f5092
AV
4799
4800@item @emph{Syntax}:
4801@code{void _gfortran_caf_send_by_ref (caf_token_t token, int image_index,
4802gfc_descriptor_t *src, caf_reference_t *refs, int dst_kind, int src_kind,
87e8aa3b 4803bool may_require_tmp, bool dst_reallocatable, int *stat, int dst_type)}
3c9f5092
AV
4804
4805@item @emph{Arguments}:
4806@multitable @columnfractions .15 .70
4807@item @var{token} @tab intent(in) An opaque pointer identifying the coarray.
29dbb95a
AV
4808@item @var{image_index} @tab intent(in) The ID of the remote image; must be a
4809positive number.
3c9f5092
AV
4810@item @var{src} @tab intent(in) Array descriptor of the local array to be
4811transferred to the remote image
29dbb95a 4812@item @var{refs} @tab intent(in) The references on the remote array to store
3c9f5092 4813the data given by src. Guaranteed to have at least one entry.
29dbb95a
AV
4814@item @var{dst_kind} @tab intent(in) Kind of the destination argument
4815@item @var{src_kind} @tab intent(in) Kind of the source argument
4816@item @var{may_require_tmp} @tab intent(in) The variable is @code{false} when
4817it is known at compile time that the @var{dest} and @var{src} either cannot
4818overlap or overlap (fully or partially) such that walking @var{src} and
4819@var{dest} in element wise element order (honoring the stride value) will not
4820lead to wrong results. Otherwise, the value is @code{true}.
4821@item @var{dst_reallocatable} @tab intent(in) Set when the destination is of
4822allocatable or pointer type and the refs will allow reallocation, i.e., the ref
4823is a full array or component ref.
4824@item @var{stat} @tab intent(out) When non-@code{NULL} give the result of the
3c9f5092 4825operation, i.e., zero on success and non-zero on error. When @code{NULL} and
29dbb95a 4826an error occurs, then an error message is printed and the program is terminated.
87e8aa3b
AV
4827@item @var{dst_type} @tab intent(in) Give the type of the destination. When
4828the destination is not an array, than the precise type, e.g. of a component in
4829a derived type, is not known, but provided here.
3c9f5092
AV
4830@end multitable
4831
4832@item @emph{NOTES}
29dbb95a
AV
4833It is permitted to have @var{image_index} equal the current image; the memory of
4834the send-to and the send-from might (partially) overlap in that case. The
3c9f5092
AV
4835implementation has to take care that it handles this case, e.g. using
4836@code{memmove} which handles (partially) overlapping memory. If
4837@var{may_require_tmp} is true, the library might additionally create a
4838temporary variable, unless additional checks show that this is not required
4839(e.g. because walking backward is possible or because both arrays are
4840contiguous and @code{memmove} takes care of overlap issues).
4841
4842Note that the assignment of a scalar to an array is permitted. In addition,
4843the library has to handle numeric-type conversion and for strings, padding
4844and different character kinds.
4845
4846Because of the more complicated references possible some operations may be
4847unsupported by certain libraries. The library is expected to issue a precise
4848error message why the operation is not permitted.
4849@end table
4850
4851
4852@node _gfortran_caf_get_by_ref
4853@subsection @code{_gfortran_caf_get_by_ref} --- Getting data from a remote image using enhanced references
4854@cindex Coarray, _gfortran_caf_get_by_ref
4855
4856@table @asis
4857@item @emph{Description}:
29dbb95a
AV
4858Called to get a scalar, an array section or a whole array from a remote image
4859identified by the @var{image_index}.
3c9f5092
AV
4860
4861@item @emph{Syntax}:
4862@code{void _gfortran_caf_get_by_ref (caf_token_t token, int image_index,
4863caf_reference_t *refs, gfc_descriptor_t *dst, int dst_kind, int src_kind,
87e8aa3b 4864bool may_require_tmp, bool dst_reallocatable, int *stat, int src_type)}
3c9f5092
AV
4865
4866@item @emph{Arguments}:
4867@multitable @columnfractions .15 .70
4868@item @var{token} @tab intent(in) An opaque pointer identifying the coarray.
29dbb95a
AV
4869@item @var{image_index} @tab intent(in) The ID of the remote image; must be a
4870positive number.
4871@item @var{refs} @tab intent(in) The references to apply to the remote structure
3c9f5092
AV
4872to get the data.
4873@item @var{dst} @tab intent(in) Array descriptor of the local array to store
4874the data transferred from the remote image. May be reallocated where needed
4875and when @var{DST_REALLOCATABLE} allows it.
29dbb95a
AV
4876@item @var{dst_kind} @tab intent(in) Kind of the destination argument
4877@item @var{src_kind} @tab intent(in) Kind of the source argument
4878@item @var{may_require_tmp} @tab intent(in) The variable is @code{false} when
4879it is known at compile time that the @var{dest} and @var{src} either cannot
4880overlap or overlap (fully or partially) such that walking @var{src} and
4881@var{dest} in element wise element order (honoring the stride value) will not
4882lead to wrong results. Otherwise, the value is @code{true}.
4883@item @var{dst_reallocatable} @tab intent(in) Set when @var{DST} is of
4884allocatable or pointer type and its refs allow reallocation, i.e., the full
4885array or a component is referenced.
4886@item @var{stat} @tab intent(out) When non-@code{NULL} give the result of the
4887operation, i.e., zero on success and non-zero on error. When @code{NULL} and an
3c9f5092 4888error occurs, then an error message is printed and the program is terminated.
87e8aa3b
AV
4889@item @var{src_type} @tab intent(in) Give the type of the source. When the
4890source is not an array, than the precise type, e.g. of a component in a
4891derived type, is not known, but provided here.
3c9f5092
AV
4892@end multitable
4893
4894@item @emph{NOTES}
29dbb95a
AV
4895It is permitted to have @code{image_index} equal the current image; the memory
4896of the send-to and the send-from might (partially) overlap in that case. The
3c9f5092
AV
4897implementation has to take care that it handles this case, e.g. using
4898@code{memmove} which handles (partially) overlapping memory. If
4899@var{may_require_tmp} is true, the library might additionally create a
4900temporary variable, unless additional checks show that this is not required
4901(e.g. because walking backward is possible or because both arrays are
4902contiguous and @code{memmove} takes care of overlap issues).
4903
4904Note that the library has to handle numeric-type conversion and for strings,
4905padding and different character kinds.
4906
4907Because of the more complicated references possible some operations may be
4908unsupported by certain libraries. The library is expected to issue a precise
4909error message why the operation is not permitted.
4910@end table
4911
4912
4913@node _gfortran_caf_sendget_by_ref
4914@subsection @code{_gfortran_caf_sendget_by_ref} --- Sending data between remote images using enhanced references on both sides
4915@cindex Coarray, _gfortran_caf_sendget_by_ref
4916
4917@table @asis
4918@item @emph{Description}:
29dbb95a
AV
4919Called to send a scalar, an array section or a whole array from a remote image
4920identified by the @var{src_image_index} to a remote image identified by the
4921@var{dst_image_index}.
3c9f5092
AV
4922
4923@item @emph{Syntax}:
4924@code{void _gfortran_caf_sendget_by_ref (caf_token_t dst_token,
4925int dst_image_index, caf_reference_t *dst_refs,
4926caf_token_t src_token, int src_image_index, caf_reference_t *src_refs,
87e8aa3b
AV
4927int dst_kind, int src_kind, bool may_require_tmp, int *dst_stat,
4928int *src_stat, int dst_type, int src_type)}
3c9f5092
AV
4929
4930@item @emph{Arguments}:
4931@multitable @columnfractions .15 .70
4932@item @var{dst_token} @tab intent(in) An opaque pointer identifying the
4933destination coarray.
29dbb95a
AV
4934@item @var{dst_image_index} @tab intent(in) The ID of the destination remote
4935image; must be a positive number.
4936@item @var{dst_refs} @tab intent(in) The references on the remote array to store
4937the data given by the source. Guaranteed to have at least one entry.
4938@item @var{src_token} @tab intent(in) An opaque pointer identifying the source
4939coarray.
4940@item @var{src_image_index} @tab intent(in) The ID of the source remote image;
4941must be a positive number.
4942@item @var{src_refs} @tab intent(in) The references to apply to the remote
3c9f5092 4943structure to get the data.
29dbb95a
AV
4944@item @var{dst_kind} @tab intent(in) Kind of the destination argument
4945@item @var{src_kind} @tab intent(in) Kind of the source argument
4946@item @var{may_require_tmp} @tab intent(in) The variable is @code{false} when
4947it is known at compile time that the @var{dest} and @var{src} either cannot
4948overlap or overlap (fully or partially) such that walking @var{src} and
4949@var{dest} in element wise element order (honoring the stride value) will not
4950lead to wrong results. Otherwise, the value is @code{true}.
3c9f5092
AV
4951@item @var{dst_stat} @tab intent(out) when non-@code{NULL} give the result of
4952the send-operation, i.e., zero on success and non-zero on error. When
4953@code{NULL} and an error occurs, then an error message is printed and the
4954program is terminated.
29dbb95a 4955@item @var{src_stat} @tab intent(out) When non-@code{NULL} give the result of
3c9f5092
AV
4956the get-operation, i.e., zero on success and non-zero on error. When
4957@code{NULL} and an error occurs, then an error message is printed and the
4958program is terminated.
87e8aa3b
AV
4959@item @var{dst_type} @tab intent(in) Give the type of the destination. When
4960the destination is not an array, than the precise type, e.g. of a component in
4961a derived type, is not known, but provided here.
4962@item @var{src_type} @tab intent(in) Give the type of the source. When the
4963source is not an array, than the precise type, e.g. of a component in a
4964derived type, is not known, but provided here.
3c9f5092
AV
4965@end multitable
4966
4967@item @emph{NOTES}
29dbb95a
AV
4968It is permitted to have the same image index for both @var{src_image_index} and
4969@var{dst_image_index}; the memory of the send-to and the send-from might
4970(partially) overlap in that case. The implementation has to take care that it
4971handles this case, e.g. using @code{memmove} which handles (partially)
4972overlapping memory. If @var{may_require_tmp} is true, the library
3c9f5092
AV
4973might additionally create a temporary variable, unless additional checks show
4974that this is not required (e.g. because walking backward is possible or because
4975both arrays are contiguous and @code{memmove} takes care of overlap issues).
4976
4977Note that the assignment of a scalar to an array is permitted. In addition,
4978the library has to handle numeric-type conversion and for strings, padding and
4979different character kinds.
4980
4981Because of the more complicated references possible some operations may be
4982unsupported by certain libraries. The library is expected to issue a precise
4983error message why the operation is not permitted.
4984@end table
4985
c194537c 4986
bc0229f9
TB
4987@node _gfortran_caf_lock
4988@subsection @code{_gfortran_caf_lock} --- Locking a lock variable
4989@cindex Coarray, _gfortran_caf_lock
4990
4991@table @asis
4992@item @emph{Description}:
4993Acquire a lock on the given image on a scalar locking variable or for the
29dbb95a
AV
4994given array element for an array-valued variable. If the @var{aquired_lock}
4995is @code{NULL}, the function returns after having obtained the lock. If it is
4996non-@code{NULL}, then @var{acquired_lock} is assigned the value true (one) when
4997the lock could be obtained and false (zero) otherwise. Locking a lock variable
4998which has already been locked by the same image is an error.
bc0229f9
TB
4999
5000@item @emph{Syntax}:
5001@code{void _gfortran_caf_lock (caf_token_t token, size_t index, int image_index,
3f5fabc0 5002int *aquired_lock, int *stat, char *errmsg, size_t errmsg_len)}
bc0229f9
TB
5003
5004@item @emph{Arguments}:
5005@multitable @columnfractions .15 .70
29dbb95a
AV
5006@item @var{token} @tab intent(in) An opaque pointer identifying the coarray.
5007@item @var{index} @tab intent(in) Array index; first array index is 0. For
5008scalars, it is always 0.
5009@item @var{image_index} @tab intent(in) The ID of the remote image; must be a
5010positive number.
bc0229f9 5011@item @var{aquired_lock} @tab intent(out) If not NULL, it returns whether lock
29dbb95a
AV
5012could be obtained.
5013@item @var{stat} @tab intent(out) Stores the STAT=; may be NULL.
bc0229f9 5014@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to
29dbb95a
AV
5015an error message; may be NULL.
5016@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg
bc0229f9
TB
5017@end multitable
5018
5019@item @emph{NOTES}
5020This function is also called for critical blocks; for those, the array index
5021is always zero and the image index is one. Libraries are permitted to use other
5022images for critical-block locking variables.
5023@end table
5024
bc0229f9
TB
5025@node _gfortran_caf_unlock
5026@subsection @code{_gfortran_caf_lock} --- Unlocking a lock variable
5027@cindex Coarray, _gfortran_caf_unlock
5028
5029@table @asis
5030@item @emph{Description}:
5031Release a lock on the given image on a scalar locking variable or for the
5032given array element for an array-valued variable. Unlocking a lock variable
5033which is unlocked or has been locked by a different image is an error.
5034
5035@item @emph{Syntax}:
5036@code{void _gfortran_caf_unlock (caf_token_t token, size_t index, int image_index,
3f5fabc0 5037int *stat, char *errmsg, size_t errmsg_len)}
bc0229f9
TB
5038
5039@item @emph{Arguments}:
5040@multitable @columnfractions .15 .70
29dbb95a
AV
5041@item @var{token} @tab intent(in) An opaque pointer identifying the coarray.
5042@item @var{index} @tab intent(in) Array index; first array index is 0. For
5043scalars, it is always 0.
5044@item @var{image_index} @tab intent(in) The ID of the remote image; must be a
5045positive number.
bc0229f9 5046@item @var{stat} @tab intent(out) For allocatable coarrays, stores the STAT=;
29dbb95a 5047may be NULL.
bc0229f9 5048@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to
29dbb95a
AV
5049an error message; may be NULL.
5050@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg
bc0229f9
TB
5051@end multitable
5052
5053@item @emph{NOTES}
5054This function is also called for critical block; for those, the array index
5055is always zero and the image index is one. Libraries are permitted to use other
5056images for critical-block locking variables.
5057@end table
c194537c 5058
5df445a2
TB
5059@node _gfortran_caf_event_post
5060@subsection @code{_gfortran_caf_event_post} --- Post an event
5061@cindex Coarray, _gfortran_caf_event_post
5062
5063@table @asis
5064@item @emph{Description}:
5065Increment the event count of the specified event variable.
5066
5067@item @emph{Syntax}:
5068@code{void _gfortran_caf_event_post (caf_token_t token, size_t index,
3f5fabc0 5069int image_index, int *stat, char *errmsg, size_t errmsg_len)}
5df445a2
TB
5070
5071@item @emph{Arguments}:
5072@multitable @columnfractions .15 .70
29dbb95a
AV
5073@item @var{token} @tab intent(in) An opaque pointer identifying the coarray.
5074@item @var{index} @tab intent(in) Array index; first array index is 0. For
5075scalars, it is always 0.
5076@item @var{image_index} @tab intent(in) The ID of the remote image; must be a
5077positive number; zero indicates the current image, when accessed noncoindexed.
5078@item @var{stat} @tab intent(out) Stores the STAT=; may be NULL.
5079@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to
5080an error message; may be NULL.
5081@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg
5df445a2
TB
5082@end multitable
5083
5084@item @emph{NOTES}
5085This acts like an atomic add of one to the remote image's event variable.
5086The statement is an image-control statement but does not imply sync memory.
5087Still, all preceeding push communications of this image to the specified
29dbb95a 5088remote image have to be completed before @code{event_wait} on the remote
5df445a2
TB
5089image returns.
5090@end table
5091
5092
5093
5094@node _gfortran_caf_event_wait
5095@subsection @code{_gfortran_caf_event_wait} --- Wait that an event occurred
5096@cindex Coarray, _gfortran_caf_event_wait
5097
5098@table @asis
5099@item @emph{Description}:
5100Wait until the event count has reached at least the specified
5101@var{until_count}; if so, atomically decrement the event variable by this
5102amount and return.
5103
5104@item @emph{Syntax}:
5105@code{void _gfortran_caf_event_wait (caf_token_t token, size_t index,
3f5fabc0 5106int until_count, int *stat, char *errmsg, size_t errmsg_len)}
5df445a2
TB
5107
5108@item @emph{Arguments}:
5109@multitable @columnfractions .15 .70
29dbb95a
AV
5110@item @var{token} @tab intent(in) An opaque pointer identifying the coarray.
5111@item @var{index} @tab intent(in) Array index; first array index is 0. For
5112scalars, it is always 0.
5113@item @var{until_count} @tab intent(in) The number of events which have to be
5114available before the function returns.
5115@item @var{stat} @tab intent(out) Stores the STAT=; may be NULL.
5116@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to
5117an error message; may be NULL.
5118@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg
5df445a2
TB
5119@end multitable
5120
5121@item @emph{NOTES}
5122This function only operates on a local coarray. It acts like a loop checking
5123atomically the value of the event variable, breaking if the value is greater
5124or equal the requested number of counts. Before the function returns, the
5125event variable has to be decremented by the requested @var{until_count} value.
5126A possible implementation would be a busy loop for a certain number of spins
5127(possibly depending on the number of threads relative to the number of available
29dbb95a
AV
5128cores) followed by another waiting strategy such as a sleeping wait (possibly
5129with an increasing number of sleep time) or, if possible, a futex wait.
5df445a2
TB
5130
5131The statement is an image-control statement but does not imply sync memory.
29dbb95a
AV
5132Still, all preceeding push communications of this image to the specified
5133remote image have to be completed before @code{event_wait} on the remote
5134image returns.
5df445a2
TB
5135@end table
5136
5137
5138
5139@node _gfortran_caf_event_query
5140@subsection @code{_gfortran_caf_event_query} --- Query event count
5141@cindex Coarray, _gfortran_caf_event_query
5142
5143@table @asis
5144@item @emph{Description}:
29dbb95a 5145Return the event count of the specified event variable.
5df445a2
TB
5146
5147@item @emph{Syntax}:
5148@code{void _gfortran_caf_event_query (caf_token_t token, size_t index,
5149int image_index, int *count, int *stat)}
5150
5151@item @emph{Arguments}:
5152@multitable @columnfractions .15 .70
29dbb95a
AV
5153@item @var{token} @tab intent(in) An opaque pointer identifying the coarray.
5154@item @var{index} @tab intent(in) Array index; first array index is 0. For
5155scalars, it is always 0.
5156@item @var{image_index} @tab intent(in) The ID of the remote image; must be a
5157positive number; zero indicates the current image when accessed noncoindexed.
5158@item @var{count} @tab intent(out) The number of events currently posted to
5159the event variable.
5160@item @var{stat} @tab intent(out) Stores the STAT=; may be NULL.
5df445a2
TB
5161@end multitable
5162
5163@item @emph{NOTES}
29dbb95a 5164The typical use is to check the local event variable to only call
5df445a2
TB
5165@code{event_wait} when the data is available. However, a coindexed variable
5166is permitted; there is no ordering or synchronization implied. It acts like
5167an atomic fetch of the value of the event variable.
5168@end table
c194537c 5169
29dbb95a
AV
5170
5171
2691415b
TB
5172@node _gfortran_caf_sync_all
5173@subsection @code{_gfortran_caf_sync_all} --- All-image barrier
5174@cindex Coarray, _gfortran_caf_sync_all
5175
5176@table @asis
5177@item @emph{Description}:
5178Synchronization of all images in the current team; the program only continues
5179on a given image after this function has been called on all images of the
5180current team. Additionally, it ensures that all pending data transfers of
5181previous segment have completed.
5182
5183@item @emph{Syntax}:
3f5fabc0 5184@code{void _gfortran_caf_sync_all (int *stat, char *errmsg, size_t errmsg_len)}
2691415b
TB
5185
5186@item @emph{Arguments}:
5187@multitable @columnfractions .15 .70
29dbb95a
AV
5188@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL.
5189@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to
5190an error message; may be NULL.
5191@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg
2691415b
TB
5192@end multitable
5193@end table
5194
5195
5196
5197@node _gfortran_caf_sync_images
5198@subsection @code{_gfortran_caf_sync_images} --- Barrier for selected images
5199@cindex Coarray, _gfortran_caf_sync_images
5200
5201@table @asis
5202@item @emph{Description}:
5203Synchronization between the specified images; the program only continues on a
5204given image after this function has been called on all images specified for
5205that image. Note that one image can wait for all other images in the current
5206team (e.g. via @code{sync images(*)}) while those only wait for that specific
29dbb95a
AV
5207image. Additionally, @code{sync images} ensures that all pending data
5208transfers of previous segments have completed.
2691415b
TB
5209
5210@item @emph{Syntax}:
5211@code{void _gfortran_caf_sync_images (int count, int images[], int *stat,
3f5fabc0 5212char *errmsg, size_t errmsg_len)}
2691415b
TB
5213
5214@item @emph{Arguments}:
5215@multitable @columnfractions .15 .70
29dbb95a
AV
5216@item @var{count} @tab intent(in) The number of images which are provided in
5217the next argument. For a zero-sized array, the value is zero. For
5218@code{sync images (*)}, the value is @math{-1}.
5219@item @var{images} @tab intent(in) An array with the images provided by the
5220user. If @var{count} is zero, a NULL pointer is passed.
5221@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL.
5222@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to
5223an error message; may be NULL.
5224@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg
2691415b
TB
5225@end multitable
5226@end table
5227
5228
5229
5230@node _gfortran_caf_sync_memory
5231@subsection @code{_gfortran_caf_sync_memory} --- Wait for completion of segment-memory operations
5232@cindex Coarray, _gfortran_caf_sync_memory
5233
5234@table @asis
5235@item @emph{Description}:
5236Acts as optimization barrier between different segments. It also ensures that
5237all pending memory operations of this image have been completed.
5238
5239@item @emph{Syntax}:
3f5fabc0 5240@code{void _gfortran_caf_sync_memory (int *stat, char *errmsg, size_t errmsg_len)}
2691415b
TB
5241
5242@item @emph{Arguments}:
5243@multitable @columnfractions .15 .70
29dbb95a
AV
5244@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL.
5245@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to
5246an error message; may be NULL.
5247@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg
2691415b
TB
5248@end multitable
5249
f7a35a83
IS
5250@item @emph{NOTE} A simple implementation could be
5251@code{__asm__ __volatile__ ("":::"memory")} to prevent code movements.
2691415b
TB
5252@end table
5253
5254
5255
5256@node _gfortran_caf_error_stop
5257@subsection @code{_gfortran_caf_error_stop} --- Error termination with exit code
5258@cindex Coarray, _gfortran_caf_error_stop
5259
5260@table @asis
5261@item @emph{Description}:
5262Invoked for an @code{ERROR STOP} statement which has an integer argument. The
5263function should terminate the program with the specified exit code.
5264
5265
5266@item @emph{Syntax}:
3f5fabc0 5267@code{void _gfortran_caf_error_stop (int error)}
2691415b
TB
5268
5269@item @emph{Arguments}:
5270@multitable @columnfractions .15 .70
29dbb95a 5271@item @var{error} @tab intent(in) The exit status to be used.
2691415b
TB
5272@end multitable
5273@end table
5274
5275
5276
5277@node _gfortran_caf_error_stop_str
5278@subsection @code{_gfortran_caf_error_stop_str} --- Error termination with string
5279@cindex Coarray, _gfortran_caf_error_stop_str
5280
5281@table @asis
5282@item @emph{Description}:
5283Invoked for an @code{ERROR STOP} statement which has a string as argument. The
5284function should terminate the program with a nonzero-exit code.
5285
5286@item @emph{Syntax}:
3f5fabc0 5287@code{void _gfortran_caf_error_stop (const char *string, size_t len)}
2691415b
TB
5288
5289@item @emph{Arguments}:
5290@multitable @columnfractions .15 .70
29dbb95a
AV
5291@item @var{string} @tab intent(in) the error message (not zero terminated)
5292@item @var{len} @tab intent(in) the length of the string
2691415b
TB
5293@end multitable
5294@end table
5295
5296
5297
ef78bc3c
AV
5298@node _gfortran_caf_fail_image
5299@subsection @code{_gfortran_caf_fail_image} --- Mark the image failed and end its execution
5300@cindex Coarray, _gfortran_caf_fail_image
5301
5302@table @asis
5303@item @emph{Description}:
5304Invoked for an @code{FAIL IMAGE} statement. The function should terminate the
5305current image.
5306
5307@item @emph{Syntax}:
5308@code{void _gfortran_caf_fail_image ()}
5309
5310@item @emph{NOTES}
5311This function follows TS18508.
5312@end table
5313
5314
5315
2691415b
TB
5316@node _gfortran_caf_atomic_define
5317@subsection @code{_gfortran_caf_atomic_define} --- Atomic variable assignment
5318@cindex Coarray, _gfortran_caf_atomic_define
5319
5320@table @asis
5321@item @emph{Description}:
5322Assign atomically a value to an integer or logical variable.
5323
5324@item @emph{Syntax}:
5325@code{void _gfortran_caf_atomic_define (caf_token_t token, size_t offset,
5326int image_index, void *value, int *stat, int type, int kind)}
5327
5328@item @emph{Arguments}:
5329@multitable @columnfractions .15 .70
29dbb95a
AV
5330@item @var{token} @tab intent(in) An opaque pointer identifying the coarray.
5331@item @var{offset} @tab intent(in) By which amount of bytes the actual data is
5332shifted compared to the base address of the coarray.
5333@item @var{image_index} @tab intent(in) The ID of the remote image; must be a
5334positive number; zero indicates the current image when used noncoindexed.
5335@item @var{value} @tab intent(in) the value to be assigned, passed by reference
5336@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL.
5337@item @var{type} @tab intent(in) The data type, i.e. @code{BT_INTEGER} (1) or
2691415b 5338@code{BT_LOGICAL} (2).
29dbb95a 5339@item @var{kind} @tab intent(in) The kind value (only 4; always @code{int})
2691415b
TB
5340@end multitable
5341@end table
5342
5343
5344
5345@node _gfortran_caf_atomic_ref
5346@subsection @code{_gfortran_caf_atomic_ref} --- Atomic variable reference
5347@cindex Coarray, _gfortran_caf_atomic_ref
5348
5349@table @asis
5350@item @emph{Description}:
5351Reference atomically a value of a kind-4 integer or logical variable.
5352
5353@item @emph{Syntax}:
5354@code{void _gfortran_caf_atomic_ref (caf_token_t token, size_t offset,
5355int image_index, void *value, int *stat, int type, int kind)}
5356
2691415b
TB
5357@item @emph{Arguments}:
5358@multitable @columnfractions .15 .70
29dbb95a
AV
5359@item @var{token} @tab intent(in) An opaque pointer identifying the coarray.
5360@item @var{offset} @tab intent(in) By which amount of bytes the actual data is
5361shifted compared to the base address of the coarray.
5362@item @var{image_index} @tab intent(in) The ID of the remote image; must be a
5363positive number; zero indicates the current image when used noncoindexed.
5364@item @var{value} @tab intent(out) The variable assigned the atomically
2691415b
TB
5365referenced variable.
5366@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL.
5367@item @var{type} @tab the data type, i.e. @code{BT_INTEGER} (1) or
5368@code{BT_LOGICAL} (2).
5369@item @var{kind} @tab The kind value (only 4; always @code{int})
5370@end multitable
5371@end table
5372
5373
5374
5375@node _gfortran_caf_atomic_cas
5376@subsection @code{_gfortran_caf_atomic_cas} --- Atomic compare and swap
5377@cindex Coarray, _gfortran_caf_atomic_cas
5378
5379@table @asis
5380@item @emph{Description}:
5381Atomic compare and swap of a kind-4 integer or logical variable. Assigns
5382atomically the specified value to the atomic variable, if the latter has
5383the value specified by the passed condition value.
5384
5385@item @emph{Syntax}:
5386@code{void _gfortran_caf_atomic_cas (caf_token_t token, size_t offset,
5387int image_index, void *old, void *compare, void *new_val, int *stat,
5388int type, int kind)}
5389
5390@item @emph{Arguments}:
5391@multitable @columnfractions .15 .70
29dbb95a
AV
5392@item @var{token} @tab intent(in) An opaque pointer identifying the coarray.
5393@item @var{offset} @tab intent(in) By which amount of bytes the actual data is
5394shifted compared to the base address of the coarray.
5395@item @var{image_index} @tab intent(in) The ID of the remote image; must be a
5396positive number; zero indicates the current image when used noncoindexed.
5397@item @var{old} @tab intent(out) The value which the atomic variable had
2691415b 5398just before the cas operation.
29dbb95a
AV
5399@item @var{compare} @tab intent(in) The value used for comparision.
5400@item @var{new_val} @tab intent(in) The new value for the atomic variable,
2691415b
TB
5401assigned to the atomic variable, if @code{compare} equals the value of the
5402atomic variable.
29dbb95a
AV
5403@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL.
5404@item @var{type} @tab intent(in) the data type, i.e. @code{BT_INTEGER} (1) or
2691415b 5405@code{BT_LOGICAL} (2).
29dbb95a 5406@item @var{kind} @tab intent(in) The kind value (only 4; always @code{int})
2691415b
TB
5407@end multitable
5408@end table
5409
5410
5411
5412@node _gfortran_caf_atomic_op
5413@subsection @code{_gfortran_caf_atomic_op} --- Atomic operation
5414@cindex Coarray, _gfortran_caf_atomic_op
5415
5416@table @asis
5417@item @emph{Description}:
5418Apply an operation atomically to an atomic integer or logical variable.
5419After the operation, @var{old} contains the value just before the operation,
5420which, respectively, adds (GFC_CAF_ATOMIC_ADD) atomically the @code{value} to
29dbb95a 5421the atomic integer variable or does a bitwise AND, OR or exclusive OR
2691415b
TB
5422between the atomic variable and @var{value}; the result is then stored in the
5423atomic variable.
5424
5425@item @emph{Syntax}:
5426@code{void _gfortran_caf_atomic_op (int op, caf_token_t token, size_t offset,
5427int image_index, void *value, void *old, int *stat, int type, int kind)}
5428
5429@item @emph{Arguments}:
5430@multitable @columnfractions .15 .70
29dbb95a 5431@item @var{op} @tab intent(in) the operation to be performed; possible values
2691415b
TB
5432@code{GFC_CAF_ATOMIC_ADD} (1), @code{GFC_CAF_ATOMIC_AND} (2),
5433@code{GFC_CAF_ATOMIC_OR} (3), @code{GFC_CAF_ATOMIC_XOR} (4).
29dbb95a
AV
5434@item @var{token} @tab intent(in) An opaque pointer identifying the coarray.
5435@item @var{offset} @tab intent(in) By which amount of bytes the actual data is
5436shifted compared to the base address of the coarray.
5437@item @var{image_index} @tab intent(in) The ID of the remote image; must be a
5438positive number; zero indicates the current image when used noncoindexed.
5439@item @var{old} @tab intent(out) The value which the atomic variable had
2691415b 5440just before the atomic operation.
29dbb95a 5441@item @var{val} @tab intent(in) The new value for the atomic variable,
2691415b
TB
5442assigned to the atomic variable, if @code{compare} equals the value of the
5443atomic variable.
29dbb95a
AV
5444@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL.
5445@item @var{type} @tab intent(in) the data type, i.e. @code{BT_INTEGER} (1) or
5446@code{BT_LOGICAL} (2)
5447@item @var{kind} @tab intent(in) the kind value (only 4; always @code{int})
2691415b
TB
5448@end multitable
5449@end table
5450
5451
5452
2995ed9a 5453
229c5919
TB
5454@node _gfortran_caf_co_broadcast
5455@subsection @code{_gfortran_caf_co_broadcast} --- Sending data to all images
5456@cindex Coarray, _gfortran_caf_co_broadcast
5457
5458@table @asis
5459@item @emph{Description}:
5460Distribute a value from a given image to all other images in the team. Has to
5461be called collectively.
5462
5463@item @emph{Syntax}:
5464@code{void _gfortran_caf_co_broadcast (gfc_descriptor_t *a,
3f5fabc0 5465int source_image, int *stat, char *errmsg, size_t errmsg_len)}
229c5919
TB
5466
5467@item @emph{Arguments}:
5468@multitable @columnfractions .15 .70
29dbb95a
AV
5469@item @var{a} @tab intent(inout) An array descriptor with the data to be
5470broadcasted (on @var{source_image}) or to be received (other images).
5471@item @var{source_image} @tab intent(in) The ID of the image from which the
5472data should be broadcasted.
5473@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL.
5474@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to
5475an error message; may be NULL.
5476@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg.
229c5919
TB
5477@end multitable
5478@end table
5479
5480
5481
5482@node _gfortran_caf_co_max
5483@subsection @code{_gfortran_caf_co_max} --- Collective maximum reduction
5484@cindex Coarray, _gfortran_caf_co_max
5485
5486@table @asis
5487@item @emph{Description}:
29dbb95a 5488Calculates for each array element of the variable @var{a} the maximum
229c5919
TB
5489value for that element in the current team; if @var{result_image} has the
5490value 0, the result shall be stored on all images, otherwise, only on the
5491specified image. This function operates on numeric values and character
5492strings.
5493
5494@item @emph{Syntax}:
5495@code{void _gfortran_caf_co_max (gfc_descriptor_t *a, int result_image,
3f5fabc0 5496int *stat, char *errmsg, int a_len, size_t errmsg_len)}
229c5919
TB
5497
5498@item @emph{Arguments}:
5499@multitable @columnfractions .15 .70
29dbb95a
AV
5500@item @var{a} @tab intent(inout) An array descriptor for the data to be
5501processed. On the destination image(s) the result overwrites the old content.
5502@item @var{result_image} @tab intent(in) The ID of the image to which the
5503reduced value should be copied to; if zero, it has to be copied to all images.
5504@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL.
5505@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to
5506an error message; may be NULL.
5507@item @var{a_len} @tab intent(in) the string length of argument @var{a}
5508@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg
229c5919
TB
5509@end multitable
5510
5511@item @emph{NOTES}
29dbb95a
AV
5512If @var{result_image} is nonzero, the data in the array descriptor @var{a} on
5513all images except of the specified one become undefined; hence, the library may
5514make use of this.
229c5919
TB
5515@end table
5516
5517
5518
5519@node _gfortran_caf_co_min
5520@subsection @code{_gfortran_caf_co_min} --- Collective minimum reduction
5521@cindex Coarray, _gfortran_caf_co_min
5522
5523@table @asis
5524@item @emph{Description}:
29dbb95a 5525Calculates for each array element of the variable @var{a} the minimum
229c5919
TB
5526value for that element in the current team; if @var{result_image} has the
5527value 0, the result shall be stored on all images, otherwise, only on the
5528specified image. This function operates on numeric values and character
5529strings.
5530
5531@item @emph{Syntax}:
5532@code{void _gfortran_caf_co_min (gfc_descriptor_t *a, int result_image,
3f5fabc0 5533int *stat, char *errmsg, int a_len, size_t errmsg_len)}
229c5919
TB
5534
5535@item @emph{Arguments}:
5536@multitable @columnfractions .15 .70
29dbb95a
AV
5537@item @var{a} @tab intent(inout) An array descriptor for the data to be
5538processed. On the destination image(s) the result overwrites the old content.
5539@item @var{result_image} @tab intent(in) The ID of the image to which the
5540reduced value should be copied to; if zero, it has to be copied to all images.
5541@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL.
5542@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to
5543an error message; may be NULL.
5544@item @var{a_len} @tab intent(in) the string length of argument @var{a}
5545@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg
229c5919
TB
5546@end multitable
5547
5548@item @emph{NOTES}
29dbb95a
AV
5549If @var{result_image} is nonzero, the data in the array descriptor @var{a} on
5550all images except of the specified one become undefined; hence, the library may
5551make use of this.
229c5919
TB
5552@end table
5553
5554
5555
5556@node _gfortran_caf_co_sum
5557@subsection @code{_gfortran_caf_co_sum} --- Collective summing reduction
5558@cindex Coarray, _gfortran_caf_co_sum
5559
5560@table @asis
5561@item @emph{Description}:
29dbb95a
AV
5562Calculates for each array element of the variable @var{a} the sum of all
5563values for that element in the current team; if @var{result_image} has the
229c5919 5564value 0, the result shall be stored on all images, otherwise, only on the
29dbb95a 5565specified image. This function operates on numeric values only.
229c5919
TB
5566
5567@item @emph{Syntax}:
5568@code{void _gfortran_caf_co_sum (gfc_descriptor_t *a, int result_image,
3f5fabc0 5569int *stat, char *errmsg, size_t errmsg_len)}
229c5919
TB
5570
5571@item @emph{Arguments}:
5572@multitable @columnfractions .15 .70
29dbb95a
AV
5573@item @var{a} @tab intent(inout) An array descriptor with the data to be
5574processed. On the destination image(s) the result overwrites the old content.
5575@item @var{result_image} @tab intent(in) The ID of the image to which the
5576reduced value should be copied to; if zero, it has to be copied to all images.
5577@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL.
5578@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to
5579an error message; may be NULL.
5580@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg
229c5919
TB
5581@end multitable
5582
5583@item @emph{NOTES}
29dbb95a
AV
5584If @var{result_image} is nonzero, the data in the array descriptor @var{a} on
5585all images except of the specified one become undefined; hence, the library may
5586make use of this.
229c5919
TB
5587@end table
5588
5589
5590
5591@node _gfortran_caf_co_reduce
5592@subsection @code{_gfortran_caf_co_reduce} --- Generic collective reduction
5593@cindex Coarray, _gfortran_caf_co_reduce
5594
5595@table @asis
5596@item @emph{Description}:
29dbb95a 5597Calculates for each array element of the variable @var{a} the reduction
229c5919
TB
5598value for that element in the current team; if @var{result_image} has the
5599value 0, the result shall be stored on all images, otherwise, only on the
29dbb95a 5600specified image. The @var{opr} is a pure function doing a mathematically
229c5919
TB
5601commutative and associative operation.
5602
5603The @var{opr_flags} denote the following; the values are bitwise ored.
5604@code{GFC_CAF_BYREF} (1) if the result should be returned
29dbb95a
AV
5605by reference; @code{GFC_CAF_HIDDENLEN} (2) whether the result and argument
5606string lengths shall be specified as hidden arguments;
229c5919
TB
5607@code{GFC_CAF_ARG_VALUE} (4) whether the arguments shall be passed by value,
5608@code{GFC_CAF_ARG_DESC} (8) whether the arguments shall be passed by descriptor.
5609
5610
5611@item @emph{Syntax}:
5612@code{void _gfortran_caf_co_reduce (gfc_descriptor_t *a,
5613void * (*opr) (void *, void *), int opr_flags, int result_image,
3f5fabc0 5614int *stat, char *errmsg, int a_len, size_t errmsg_len)}
229c5919
TB
5615
5616@item @emph{Arguments}:
5617@multitable @columnfractions .15 .70
29dbb95a
AV
5618@item @var{a} @tab intent(inout) An array descriptor with the data to be
5619processed. On the destination image(s) the result overwrites the old content.
5620@item @var{opr} @tab intent(in) Function pointer to the reduction function
5621@item @var{opr_flags} @tab intent(in) Flags regarding the reduction function
5622@item @var{result_image} @tab intent(in) The ID of the image to which the
5623reduced value should be copied to; if zero, it has to be copied to all images.
5624@item @var{stat} @tab intent(out) Stores the status STAT= and may be NULL.
5625@item @var{errmsg} @tab intent(out) When an error occurs, this will be set to
5626an error message; may be NULL.
5627@item @var{a_len} @tab intent(in) the string length of argument @var{a}
5628@item @var{errmsg_len} @tab intent(in) the buffer size of errmsg
229c5919
TB
5629@end multitable
5630
5631@item @emph{NOTES}
29dbb95a
AV
5632If @var{result_image} is nonzero, the data in the array descriptor @var{a} on
5633all images except of the specified one become undefined; hence, the library may
5634make use of this.
5635
229c5919
TB
5636For character arguments, the result is passed as first argument, followed
5637by the result string length, next come the two string arguments, followed
29dbb95a
AV
5638by the two hidden string length arguments. With C binding, there are no hidden
5639arguments and by-reference passing and either only a single character is passed
5640or an array descriptor.
229c5919
TB
5641@end table
5642
5643
c8cf50e4 5644@c Intrinsic Procedures
a63dad5b
TS
5645@c ---------------------------------------------------------------------
5646
c8cf50e4
BM
5647@include intrinsic.texi
5648
5649
5650@tex
5651\blankpart
5652@end tex
5653
6de9cd9a
DN
5654@c ---------------------------------------------------------------------
5655@c Contributing
5656@c ---------------------------------------------------------------------
5657
5658@node Contributing
c8cf50e4 5659@unnumbered Contributing
6de9cd9a
DN
5660@cindex Contributing
5661
5662Free software is only possible if people contribute to efforts
5663to create it.
5664We're always in need of more people helping out with ideas
5665and comments, writing documentation and contributing code.
5666
7fc15ba5 5667If you want to contribute to GNU Fortran,
6de9cd9a
DN
5668have a look at the long lists of projects you can take on.
5669Some of these projects are small,
5670some of them are large;
5671some are completely orthogonal to the rest of what is
7fc15ba5 5672happening on GNU Fortran,
6de9cd9a
DN
5673but others are ``mainstream'' projects in need of enthusiastic hackers.
5674All of these projects are important!
c5a0818e 5675We will eventually get around to the things here,
6de9cd9a
DN
5676but they are also things doable by someone who is willing and able.
5677
5678@menu
5679* Contributors::
5680* Projects::
c8cf50e4 5681* Proposed Extensions::
6de9cd9a
DN
5682@end menu
5683
5684
5685@node Contributors
7fc15ba5 5686@section Contributors to GNU Fortran
6de9cd9a
DN
5687@cindex Contributors
5688@cindex Credits
5689@cindex Authors
5690
5691Most of the parser was hand-crafted by @emph{Andy Vaught}, who is
5692also the initiator of the whole project. Thanks Andy!
5693Most of the interface with GCC was written by @emph{Paul Brook}.
5694
5695The following individuals have contributed code and/or
7fc15ba5 5696ideas and significant help to the GNU Fortran project
3b303683 5697(in alphabetical order):
6de9cd9a
DN
5698
5699@itemize @minus
3b303683 5700@item Janne Blomqvist
6de9cd9a 5701@item Steven Bosscher
6de9cd9a 5702@item Paul Brook
3b303683 5703@item Tobias Burnus
deeddce6 5704@item Fran@,{c}ois-Xavier Coudert
3b303683
DF
5705@item Bud Davis
5706@item Jerry DeLisle
cf6ae955 5707@item Erik Edelmann
3b303683
DF
5708@item Bernhard Fischer
5709@item Daniel Franke
5710@item Richard Guenther
5711@item Richard Henderson
5712@item Katherine Holcomb
5713@item Jakub Jelinek
5714@item Niels Kristian Bech Jensen
5715@item Steven Johnson
5716@item Steven G. Kargl
cf6ae955
SB
5717@item Thomas Koenig
5718@item Asher Langton
3b303683
DF
5719@item H. J. Lu
5720@item Toon Moene
5721@item Brooks Moses
5722@item Andrew Pinski
5723@item Tim Prince
5724@item Christopher D. Rickett
deeddce6 5725@item Richard Sandiford
3b303683
DF
5726@item Tobias Schl@"uter
5727@item Roger Sayle
5728@item Paul Thomas
5729@item Andy Vaught
5730@item Feng Wang
5731@item Janus Weil
9e0667cd 5732@item Daniel Kraft
6de9cd9a
DN
5733@end itemize
5734
5735The following people have contributed bug reports,
5736smaller or larger patches,
5737and much needed feedback and encouragement for the
7fc15ba5 5738GNU Fortran project:
6de9cd9a
DN
5739
5740@itemize @minus
6de9cd9a 5741@item Bill Clodius
49309826 5742@item Dominique d'Humi@`eres
6de9cd9a 5743@item Kate Hedstrom
3b303683 5744@item Erik Schnetter
9e0667cd 5745@item Joost VandeVondele
6de9cd9a
DN
5746@end itemize
5747
5748Many other individuals have helped debug,
7fc15ba5 5749test and improve the GNU Fortran compiler over the past few years,
ed499b9f 5750and we welcome you to do the same!
6de9cd9a
DN
5751If you already have done so,
5752and you would like to see your name listed in the
5753list above, please contact us.
5754
5755
5756@node Projects
5757@section Projects
5758
5759@table @emph
5760
5761@item Help build the test suite
49309826
FXC
5762Solicit more code for donation to the test suite: the more extensive the
5763testsuite, the smaller the risk of breaking things in the future! We can
5764keep code private on request.
6de9cd9a
DN
5765
5766@item Bug hunting/squishing
49309826
FXC
5767Find bugs and write more test cases! Test cases are especially very
5768welcome, because it allows us to concentrate on fixing bugs instead of
3994c6b1 5769isolating them. Going through the bugzilla database at
2bf716a9 5770@url{https://gcc.gnu.org/@/bugzilla/} to reduce testcases posted there and
49309826
FXC
5771add more information (for example, for which version does the testcase
5772work, for which versions does it fail?) is also very helpful.
6de9cd9a 5773
49309826 5774@end table
6de9cd9a
DN
5775
5776
c8cf50e4
BM
5777@node Proposed Extensions
5778@section Proposed Extensions
6de9cd9a 5779
c8cf50e4
BM
5780Here's a list of proposed extensions for the GNU Fortran compiler, in no particular
5781order. Most of these are necessary to be fully compatible with
5782existing Fortran compilers, but they are not part of the official
5783J3 Fortran 95 standard.
6de9cd9a 5784
bc0229f9 5785@subsection Compiler extensions:
c8cf50e4
BM
5786@itemize @bullet
5787@item
5788User-specified alignment rules for structures.
6de9cd9a 5789
c8cf50e4
BM
5790@item
5791Automatically extend single precision constants to double.
e014df90 5792
c8cf50e4
BM
5793@item
5794Compile code that conserves memory by dynamically allocating common and
5795module storage either on stack or heap.
e014df90 5796
c8cf50e4
BM
5797@item
5798Compile flag to generate code for array conformance checking (suggest -CC).
e014df90 5799
c8cf50e4
BM
5800@item
5801User control of symbol names (underscores, etc).
e014df90 5802
c8cf50e4
BM
5803@item
5804Compile setting for maximum size of stack frame size before spilling
5805parts to static or heap.
e014df90 5806
a63dad5b 5807@item
c8cf50e4 5808Flag to force local variables into static space.
e27edcd4
TK
5809
5810@item
c8cf50e4 5811Flag to force local variables onto stack.
c8cf50e4
BM
5812@end itemize
5813
5814
5815@subsection Environment Options
5816@itemize @bullet
aa08038d 5817@item
c8cf50e4
BM
5818Pluggable library modules for random numbers, linear algebra.
5819LA should use BLAS calling conventions.
5820
8e119f1b 5821@item
c8cf50e4
BM
5822Environment variables controlling actions on arithmetic exceptions like
5823overflow, underflow, precision loss---Generate NaN, abort, default.
5824action.
5825
da1e2517 5826@item
c8cf50e4 5827Set precision for fp units that support it (i387).
aa08038d 5828
ffcba571 5829@item
c8cf50e4 5830Variable for setting fp rounding mode.
ffcba571 5831
08d7f64e 5832@item
c8cf50e4
BM
5833Variable to fill uninitialized variables with a user-defined bit
5834pattern.
08d7f64e 5835
669353d5 5836@item
c8cf50e4
BM
5837Environment variable controlling filename that is opened for that unit
5838number.
669353d5
TB
5839
5840@item
c8cf50e4 5841Environment variable to clear/trash memory being freed.
669353d5 5842
08d7f64e 5843@item
c8cf50e4 5844Environment variable to control tracing of allocations and frees.
ffcba571 5845
8998be20 5846@item
c8cf50e4 5847Environment variable to display allocated memory at normal program end.
8998be20 5848
669353d5 5849@item
c8cf50e4
BM
5850Environment variable for filename for * IO-unit.
5851
5852@item
5853Environment variable for temporary file directory.
5854
5855@item
3e508131 5856Environment variable forcing standard output to be line buffered (Unix).
ffcba571 5857
e014df90
JB
5858@end itemize
5859
5860
a63dad5b
TS
5861@c ---------------------------------------------------------------------
5862@c GNU General Public License
5863@c ---------------------------------------------------------------------
5864
7f9766e4 5865@include gpl_v3.texi
a63dad5b
TS
5866
5867
5868
5869@c ---------------------------------------------------------------------
5870@c GNU Free Documentation License
5871@c ---------------------------------------------------------------------
5872
5873@include fdl.texi
5874
5875
5876
5877@c ---------------------------------------------------------------------
5878@c Funding Free Software
5879@c ---------------------------------------------------------------------
5880
5881@include funding.texi
5882
e014df90 5883@c ---------------------------------------------------------------------
32864778 5884@c Indices
e014df90 5885@c ---------------------------------------------------------------------
6de9cd9a 5886
32864778 5887@node Option Index
e739dfac 5888@unnumbered Option Index
67948fd2 5889@command{gfortran}'s command line options are indexed here without any
3994c6b1 5890initial @samp{-} or @samp{--}. Where an option has both positive and
67948fd2
BM
5891negative forms (such as -foption and -fno-option), relevant entries in
5892the manual are indexed under the most appropriate form; it may sometimes
5893be useful to look up both forms.
32864778
DF
5894@printindex op
5895
5896@node Keyword Index
e739dfac 5897@unnumbered Keyword Index
6de9cd9a
DN
5898@printindex cp
5899
5900@bye