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