OSDN Git Service

2010-08-10 Daniel Kraft <d@domob.eu>
[pf3gnuchains/gcc-fork.git] / gcc / fortran / gfortran.texi
1 \input texinfo  @c -*-texinfo-*-
2 @c %**start of header
3 @setfilename gfortran.info
4 @set copyrights-gfortran 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 2003 and 2008 status::  Fortran 2003 and 2008 features supported by GNU Fortran.
184 * Compiler Characteristics::      User-visible implementation details.
185 * Mixed-Language Programming::    Interoperability with C
186 * Extensions::           Language extensions implemented by GNU Fortran.
187 * Intrinsic Procedures:: Intrinsic procedures supported by GNU Fortran.
188 * Intrinsic Modules::    Intrinsic modules supported by GNU Fortran.
189
190 * Contributing::         How you can help.
191 * Copying::              GNU General Public License says
192                          how you can copy and share GNU Fortran.
193 * GNU Free Documentation License::
194                          How you can copy and share this manual.
195 * Funding::              How to help assure continued work for free software.
196 * Option Index::         Index of command line options
197 * Keyword Index::        Index of concepts
198 @end menu
199 @end ifnottex
200
201 @c ---------------------------------------------------------------------
202 @c Introduction
203 @c ---------------------------------------------------------------------
204
205 @node Introduction
206 @chapter Introduction
207
208 @c The following duplicates the text on the TexInfo table of contents.
209 @iftex
210 This manual documents the use of @command{gfortran}, the GNU Fortran
211 compiler. You can find in this manual how to invoke @command{gfortran},
212 as well as its features and incompatibilities.
213
214 @ifset DEVELOPMENT
215 @emph{Warning:} This document, and the compiler it describes, are still
216 under development.  While efforts are made to keep it up-to-date, it
217 might not accurately reflect the status of the most recent GNU Fortran
218 compiler.
219 @end ifset
220 @end iftex
221
222 The GNU Fortran compiler front end was
223 designed initially as a free replacement for,
224 or alternative to, the unix @command{f95} command;
225 @command{gfortran} is the command you'll use to invoke the compiler.
226
227 @menu
228 * About GNU Fortran::    What you should know about the GNU Fortran compiler.
229 * GNU Fortran and GCC::  You can compile Fortran, C, or other programs.
230 * Preprocessing and conditional compilation:: The Fortran preprocessor
231 * GNU Fortran and G77::  Why we chose to start from scratch.
232 * Project Status::       Status of GNU Fortran, roadmap, proposed extensions.
233 * Standards::            Standards supported by GNU Fortran.
234 @end menu
235
236
237 @c ---------------------------------------------------------------------
238 @c About GNU Fortran
239 @c ---------------------------------------------------------------------
240
241 @node About GNU Fortran
242 @section About GNU Fortran
243
244 The GNU Fortran compiler supports the Fortran 77, 90 and 95 standards
245 completely, parts of the Fortran 2003 and Fortran 2008 standards, and
246 several vendor extensions. The development goal is to provide the
247 following features:
248
249 @itemize @bullet
250 @item
251 Read a user's program,
252 stored in a file and containing instructions written
253 in Fortran 77, Fortran 90, Fortran 95, Fortran 2003 or Fortran 2008.
254 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 aren't 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 @code{.F}, @code{.FOR}, @code{.FTN}, @code{.fpp},
411 @code{.FPP}, @code{.F90}, @code{.F95}, @code{.F03} or @code{.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 and @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 While CPP is the de-facto standard for preprocessing Fortran code,
426 Part 3 of the Fortran 95 standard (ISO/IEC 1539-3:1998) defines
427 Conditional Compilation, which is not widely used and not directly
428 supported by the GNU Fortran compiler. You can use the program coco
429 to preprocess such files (@uref{http://users.erols.com/dnagle/coco.html}).
430
431
432 @c ---------------------------------------------------------------------
433 @c GNU Fortran and G77
434 @c ---------------------------------------------------------------------
435
436 @node GNU Fortran and G77
437 @section GNU Fortran and G77
438 @cindex Fortran 77
439 @cindex @command{g77}
440
441 The GNU Fortran compiler is the successor to @command{g77}, the Fortran 
442 77 front end included in GCC prior to version 4.  It is an entirely new 
443 program that has been designed to provide Fortran 95 support and 
444 extensibility for future Fortran language standards, as well as providing 
445 backwards compatibility for Fortran 77 and nearly all of the GNU language 
446 extensions supported by @command{g77}.
447
448
449 @c ---------------------------------------------------------------------
450 @c Project Status
451 @c ---------------------------------------------------------------------
452
453 @node Project Status
454 @section Project Status
455
456 @quotation
457 As soon as @command{gfortran} can parse all of the statements correctly,
458 it will be in the ``larva'' state.
459 When we generate code, the ``puppa'' state.
460 When @command{gfortran} is done,
461 we'll see if it will be a beautiful butterfly,
462 or just a big bug....
463
464 --Andy Vaught, April 2000
465 @end quotation
466
467 The start of the GNU Fortran 95 project was announced on
468 the GCC homepage in March 18, 2000
469 (even though Andy had already been working on it for a while,
470 of course).
471
472 The GNU Fortran compiler is able to compile nearly all
473 standard-compliant Fortran 95, Fortran 90, and Fortran 77 programs,
474 including a number of standard and non-standard extensions, and can be
475 used on real-world programs.  In particular, the supported extensions
476 include OpenMP, Cray-style pointers, and several Fortran 2003 and Fortran
477 2008 features such as enumeration, stream I/O, and some of the
478 enhancements to allocatable array support from TR 15581.  However, it is
479 still under development and has a few remaining rough edges.
480
481 At present, the GNU Fortran compiler passes the
482 @uref{http://www.fortran-2000.com/ArnaudRecipes/fcvs21_f95.html, 
483 NIST Fortran 77 Test Suite}, and produces acceptable results on the
484 @uref{http://www.netlib.org/lapack/faq.html#1.21, LAPACK Test Suite}.
485 It also provides respectable performance on 
486 the @uref{http://www.polyhedron.com/pb05.html, Polyhedron Fortran
487 compiler benchmarks} and the
488 @uref{http://www.llnl.gov/asci_benchmarks/asci/limited/lfk/README.html,
489 Livermore Fortran Kernels test}.  It has been used to compile a number of
490 large real-world programs, including
491 @uref{http://mysite.verizon.net/serveall/moene.pdf, the HIRLAM
492 weather-forecasting code} and
493 @uref{http://www.theochem.uwa.edu.au/tonto/, the Tonto quantum 
494 chemistry package}; see @url{http://gcc.gnu.org/wiki/GfortranApps} for an
495 extended list.
496
497 Among other things, the GNU Fortran compiler is intended as a replacement
498 for G77.  At this point, nearly all programs that could be compiled with
499 G77 can be compiled with GNU Fortran, although there are a few minor known
500 regressions.
501
502 The primary work remaining to be done on GNU Fortran falls into three
503 categories: bug fixing (primarily regarding the treatment of invalid code
504 and providing useful error messages), improving the compiler optimizations
505 and the performance of compiled code, and extending the compiler to support
506 future standards---in particular, Fortran 2003 and Fortran 2008.
507
508
509 @c ---------------------------------------------------------------------
510 @c Standards
511 @c ---------------------------------------------------------------------
512
513 @node Standards
514 @section Standards
515 @cindex Standards
516
517 @menu
518 * Varying Length Character Strings::
519 @end menu
520
521 The GNU Fortran compiler implements
522 ISO/IEC 1539:1997 (Fortran 95).  As such, it can also compile essentially all
523 standard-compliant Fortran 90 and Fortran 77 programs.   It also supports
524 the ISO/IEC TR-15581 enhancements to allocatable arrays, and
525 the @uref{http://www.openmp.org/drupal/mp-documents/spec25.pdf,
526 OpenMP Application Program Interface v2.5} specification.
527
528 In the future, the GNU Fortran compiler will also support ISO/IEC
529 1539-1:2004 (Fortran 2003) and future Fortran standards. Partial support
530 of that standard is already provided; the current status of Fortran 2003
531 support is reported in the @ref{Fortran 2003 status} section of the
532 documentation.
533
534 The next version of the Fortran standard (Fortran 2008) is currently
535 being developed and the GNU Fortran compiler supports some of its new
536 features. This support is based on the latest draft of the standard
537 (available from @url{http://www.nag.co.uk/sc22wg5/}) and no guarantee of
538 future compatibility is made, as the final standard might differ from the
539 draft. For more information, see the @ref{Fortran 2008 status} section.
540
541 Additionally, the GNU Fortran compilers supports the OpenMP specification
542 (version 3.0, @url{http://openmp.org/wp/openmp-specifications/}).
543
544 @node Varying Length Character Strings
545 @subsection Varying Length Character Strings
546 @cindex Varying length character strings
547 @cindex Varying length strings
548 @cindex strings, varying length
549
550 The Fortran 95 standard specifies in Part 2 (ISO/IEC 1539-2:2000)
551 varying length character strings. While GNU Fortran currently does not
552 support such strings directly, there exist two Fortran implementations
553 for them, which work with GNU Fortran. They can be found at
554 @uref{http://www.fortran.com/@/iso_varying_string.f95} and at
555 @uref{ftp://ftp.nag.co.uk/@/sc22wg5/@/ISO_VARYING_STRING/}.
556
557
558
559 @c =====================================================================
560 @c PART I: INVOCATION REFERENCE
561 @c =====================================================================
562
563 @tex
564 \part{I}{Invoking GNU Fortran}
565 @end tex
566
567 @c ---------------------------------------------------------------------
568 @c Compiler Options
569 @c ---------------------------------------------------------------------
570
571 @include invoke.texi
572
573
574 @c ---------------------------------------------------------------------
575 @c Runtime
576 @c ---------------------------------------------------------------------
577
578 @node Runtime
579 @chapter Runtime:  Influencing runtime behavior with environment variables
580 @cindex environment variable
581
582 The behavior of the @command{gfortran} can be influenced by
583 environment variables.
584
585 Malformed environment variables are silently ignored.
586
587 @menu
588 * GFORTRAN_STDIN_UNIT:: Unit number for standard input
589 * GFORTRAN_STDOUT_UNIT:: Unit number for standard output
590 * GFORTRAN_STDERR_UNIT:: Unit number for standard error
591 * GFORTRAN_USE_STDERR:: Send library output to standard error
592 * GFORTRAN_TMPDIR:: Directory for scratch files
593 * GFORTRAN_UNBUFFERED_ALL:: Don't buffer I/O for all units.
594 * GFORTRAN_UNBUFFERED_PRECONNECTED:: Don't buffer I/O for preconnected units.
595 * GFORTRAN_SHOW_LOCUS::  Show location for runtime errors
596 * GFORTRAN_OPTIONAL_PLUS:: Print leading + where permitted
597 * GFORTRAN_DEFAULT_RECL:: Default record length for new files
598 * GFORTRAN_LIST_SEPARATOR::  Separator for list output
599 * GFORTRAN_CONVERT_UNIT::  Set endianness for unformatted I/O
600 * GFORTRAN_ERROR_DUMPCORE:: Dump core on run-time errors
601 * GFORTRAN_ERROR_BACKTRACE:: Show backtrace on run-time errors
602 @end menu
603
604 @node GFORTRAN_STDIN_UNIT
605 @section @env{GFORTRAN_STDIN_UNIT}---Unit number for standard input
606
607 This environment variable can be used to select the unit number
608 preconnected to standard input.  This must be a positive integer.
609 The default value is 5.
610
611 @node GFORTRAN_STDOUT_UNIT
612 @section @env{GFORTRAN_STDOUT_UNIT}---Unit number for standard output
613
614 This environment variable can be used to select the unit number
615 preconnected to standard output.  This must be a positive integer.
616 The default value is 6.
617
618 @node GFORTRAN_STDERR_UNIT
619 @section @env{GFORTRAN_STDERR_UNIT}---Unit number for standard error
620
621 This environment variable can be used to select the unit number
622 preconnected to standard error.  This must be a positive integer.
623 The default value is 0.
624
625 @node GFORTRAN_USE_STDERR
626 @section @env{GFORTRAN_USE_STDERR}---Send library output to standard error
627
628 This environment variable controls where library output is sent.
629 If the first letter is @samp{y}, @samp{Y} or @samp{1}, standard
630 error is used. If the first letter is @samp{n}, @samp{N} or
631 @samp{0}, standard output is used.
632
633 @node GFORTRAN_TMPDIR
634 @section @env{GFORTRAN_TMPDIR}---Directory for scratch files
635
636 This environment variable controls where scratch files are
637 created.  If this environment variable is missing,
638 GNU Fortran searches for the environment variable @env{TMP}, then @env{TEMP}.
639 If these are missing, the default is @file{/tmp}.
640
641 @node GFORTRAN_UNBUFFERED_ALL
642 @section @env{GFORTRAN_UNBUFFERED_ALL}---Don't buffer I/O on all units
643
644 This environment variable controls whether all I/O is unbuffered.  If
645 the first letter is @samp{y}, @samp{Y} or @samp{1}, all I/O is
646 unbuffered. This will slow down small sequential reads and writes.  If
647 the first letter is @samp{n}, @samp{N} or @samp{0}, I/O is buffered.
648 This is the default.
649
650 @node GFORTRAN_UNBUFFERED_PRECONNECTED
651 @section @env{GFORTRAN_UNBUFFERED_PRECONNECTED}---Don't buffer I/O on preconnected units
652
653 The environment variable named @env{GFORTRAN_UNBUFFERED_PRECONNECTED} controls
654 whether I/O on a preconnected unit (i.e.@: STDOUT or STDERR) is unbuffered.  If 
655 the first letter is @samp{y}, @samp{Y} or @samp{1}, I/O is unbuffered. This
656 will slow down small sequential reads and writes.  If the first letter
657 is @samp{n}, @samp{N} or @samp{0}, I/O is buffered.  This is the default.
658
659 @node GFORTRAN_SHOW_LOCUS
660 @section @env{GFORTRAN_SHOW_LOCUS}---Show location for runtime errors
661
662 If the first letter is @samp{y}, @samp{Y} or @samp{1}, filename and
663 line numbers for runtime errors are printed.  If the first letter is
664 @samp{n}, @samp{N} or @samp{0}, don't print filename and line numbers
665 for runtime errors. The default is to print the location.
666
667 @node GFORTRAN_OPTIONAL_PLUS
668 @section @env{GFORTRAN_OPTIONAL_PLUS}---Print leading + where permitted
669
670 If the first letter is @samp{y}, @samp{Y} or @samp{1},
671 a plus sign is printed
672 where permitted by the Fortran standard.  If the first letter
673 is @samp{n}, @samp{N} or @samp{0}, a plus sign is not printed
674 in most cases. Default is not to print plus signs.
675
676 @node GFORTRAN_DEFAULT_RECL
677 @section @env{GFORTRAN_DEFAULT_RECL}---Default record length for new files
678
679 This environment variable specifies the default record length, in
680 bytes, for files which are opened without a @code{RECL} tag in the
681 @code{OPEN} statement.  This must be a positive integer.  The
682 default value is 1073741824 bytes (1 GB).
683
684 @node GFORTRAN_LIST_SEPARATOR
685 @section @env{GFORTRAN_LIST_SEPARATOR}---Separator for list output
686
687 This environment variable specifies the separator when writing
688 list-directed output.  It may contain any number of spaces and
689 at most one comma.  If you specify this on the command line,
690 be sure to quote spaces, as in
691 @smallexample
692 $ GFORTRAN_LIST_SEPARATOR='  ,  ' ./a.out
693 @end smallexample
694 when @command{a.out} is the compiled Fortran program that you want to run.
695 Default is a single space.
696
697 @node GFORTRAN_CONVERT_UNIT
698 @section @env{GFORTRAN_CONVERT_UNIT}---Set endianness for unformatted I/O
699
700 By setting the @env{GFORTRAN_CONVERT_UNIT} variable, it is possible
701 to change the representation of data for unformatted files.
702 The syntax for the @env{GFORTRAN_CONVERT_UNIT} variable is:
703 @smallexample
704 GFORTRAN_CONVERT_UNIT: mode | mode ';' exception | exception ;
705 mode: 'native' | 'swap' | 'big_endian' | 'little_endian' ;
706 exception: mode ':' unit_list | unit_list ;
707 unit_list: unit_spec | unit_list unit_spec ;
708 unit_spec: INTEGER | INTEGER '-' INTEGER ;
709 @end smallexample
710 The variable consists of an optional default mode, followed by
711 a list of optional exceptions, which are separated by semicolons
712 from the preceding default and each other.  Each exception consists
713 of a format and a comma-separated list of units.  Valid values for
714 the modes are the same as for the @code{CONVERT} specifier:
715
716 @itemize @w{}
717 @item @code{NATIVE} Use the native format.  This is the default.
718 @item @code{SWAP} Swap between little- and big-endian.
719 @item @code{LITTLE_ENDIAN} Use the little-endian format
720 for unformatted files.
721 @item @code{BIG_ENDIAN} Use the big-endian format for unformatted files.
722 @end itemize
723 A missing mode for an exception is taken to mean @code{BIG_ENDIAN}.
724 Examples of values for @env{GFORTRAN_CONVERT_UNIT} are:
725 @itemize @w{}
726 @item @code{'big_endian'}  Do all unformatted I/O in big_endian mode.
727 @item @code{'little_endian;native:10-20,25'}  Do all unformatted I/O 
728 in little_endian mode, except for units 10 to 20 and 25, which are in
729 native format.
730 @item @code{'10-20'}  Units 10 to 20 are big-endian, the rest is native.
731 @end itemize
732
733 Setting the environment variables should be done on the command
734 line or via the @command{export}
735 command for @command{sh}-compatible shells and via @command{setenv}
736 for @command{csh}-compatible shells.
737
738 Example for @command{sh}:
739 @smallexample
740 $ gfortran foo.f90
741 $ GFORTRAN_CONVERT_UNIT='big_endian;native:10-20' ./a.out
742 @end smallexample
743
744 Example code for @command{csh}:
745 @smallexample
746 % gfortran foo.f90
747 % setenv GFORTRAN_CONVERT_UNIT 'big_endian;native:10-20'
748 % ./a.out
749 @end smallexample
750
751 Using anything but the native representation for unformatted data
752 carries a significant speed overhead.  If speed in this area matters
753 to you, it is best if you use this only for data that needs to be
754 portable.
755
756 @xref{CONVERT specifier}, for an alternative way to specify the
757 data representation for unformatted files.  @xref{Runtime Options}, for
758 setting a default data representation for the whole program.  The
759 @code{CONVERT} specifier overrides the @option{-fconvert} compile options.
760
761 @emph{Note that the values specified via the GFORTRAN_CONVERT_UNIT
762 environment variable will override the CONVERT specifier in the
763 open statement}.  This is to give control over data formats to
764 users who do not have the source code of their program available.
765
766 @node GFORTRAN_ERROR_DUMPCORE
767 @section @env{GFORTRAN_ERROR_DUMPCORE}---Dump core on run-time errors
768
769 If the @env{GFORTRAN_ERROR_DUMPCORE} variable is set to
770 @samp{y}, @samp{Y} or @samp{1} (only the first letter is relevant)
771 then library run-time errors cause core dumps. To disable the core
772 dumps, set the variable to @samp{n}, @samp{N}, @samp{0}. Default
773 is not to core dump unless the @option{-fdump-core} compile option
774 was used.
775
776 @node GFORTRAN_ERROR_BACKTRACE
777 @section @env{GFORTRAN_ERROR_BACKTRACE}---Show backtrace on run-time errors
778
779 If the @env{GFORTRAN_ERROR_BACKTRACE} variable is set to
780 @samp{y}, @samp{Y} or @samp{1} (only the first letter is relevant)
781 then a backtrace is printed when a run-time error occurs.
782 To disable the backtracing, set the variable to
783 @samp{n}, @samp{N}, @samp{0}. Default is not to print a backtrace
784 unless the @option{-fbacktrace} compile option
785 was used.
786
787 @c =====================================================================
788 @c PART II: LANGUAGE REFERENCE
789 @c =====================================================================
790
791 @tex
792 \part{II}{Language Reference}
793 @end tex
794
795 @c ---------------------------------------------------------------------
796 @c Fortran 2003 and 2008 Status
797 @c ---------------------------------------------------------------------
798
799 @node Fortran 2003 and 2008 status
800 @chapter Fortran 2003 and 2008 Status
801
802 @menu
803 * Fortran 2003 status::
804 * Fortran 2008 status::
805 @end menu
806
807 @node Fortran 2003 status
808 @section Fortran 2003 status
809
810 GNU Fortran supports several Fortran 2003 features; an incomplete
811 list can be found below.  See also the
812 @uref{http://gcc.gnu.org/wiki/Fortran2003, wiki page} about Fortran 2003.
813
814 @itemize
815 @item 
816 Intrinsics @code{command_argument_count}, @code{get_command},
817 @code{get_command_argument}, @code{get_environment_variable}, and
818 @code{move_alloc}.
819
820 @item 
821 @cindex array, constructors
822 @cindex @code{[...]}
823 Array constructors using square brackets. That is, @code{[...]} rather
824 than @code{(/.../)}.  Type-specification for array constructors like
825 @code{(/ some-type :: ... /)}.
826
827 @item
828 @cindex @code{FLUSH} statement
829 @cindex statement, @code{FLUSH}
830 @code{FLUSH} statement.
831
832 @item
833 @cindex @code{IOMSG=} specifier
834 @code{IOMSG=} specifier for I/O statements.
835
836 @item
837 @cindex @code{ENUM} statement
838 @cindex @code{ENUMERATOR} statement
839 @cindex statement, @code{ENUM}
840 @cindex statement, @code{ENUMERATOR}
841 @opindex @code{fshort-enums}
842 Support for the declaration of enumeration constants via the
843 @code{ENUM} and @code{ENUMERATOR} statements.  Interoperability with
844 @command{gcc} is guaranteed also for the case where the
845 @command{-fshort-enums} command line option is given.
846
847 @item
848 @cindex TR 15581
849 TR 15581:
850 @itemize
851 @item
852 @cindex @code{ALLOCATABLE} dummy arguments
853 @code{ALLOCATABLE} dummy arguments.
854 @item
855 @cindex @code{ALLOCATABLE} function results
856 @code{ALLOCATABLE} function results
857 @item
858 @cindex @code{ALLOCATABLE} components of derived types
859 @code{ALLOCATABLE} components of derived types
860 @end itemize
861
862 @item
863 @cindex @code{ALLOCATE}
864 The @code{ERRMSG=} tag is now supported in @code{ALLOCATE} and
865 @code{DEALLOCATE} statements.  The @code{SOURCE=} tag is supported
866 in an @code{ALLOCATE} statement.  An @emph{intrinsic-type-spec}
867 can be used as the @emph{type-spec} in an @code{ALLOCATE} statement;
868 while the use of a @emph{derived-type-name} is currently unsupported.
869
870 @item
871 @cindex @code{STREAM} I/O
872 @cindex @code{ACCESS='STREAM'} I/O
873 The @code{OPEN} statement supports the @code{ACCESS='STREAM'} specifier,
874 allowing I/O without any record structure.
875
876 @item
877 Namelist input/output for internal files.
878
879 @item
880 @cindex @code{PROTECTED} statement
881 @cindex statement, @code{PROTECTED}
882 The @code{PROTECTED} statement and attribute.
883
884 @item
885 @cindex @code{VALUE} statement
886 @cindex statement, @code{VALUE}
887 The @code{VALUE} statement and attribute.
888
889 @item
890 @cindex @code{VOLATILE} statement
891 @cindex statement, @code{VOLATILE}
892 The @code{VOLATILE} statement and attribute.
893
894 @item
895 @cindex @code{IMPORT} statement
896 @cindex statement, @code{IMPORT}
897 The @code{IMPORT} statement, allowing to import
898 host-associated derived types.
899
900 @item
901 @cindex @code{USE, INTRINSIC} statement
902 @cindex statement, @code{USE, INTRINSIC}
903 @cindex @code{ISO_FORTRAN_ENV} statement
904 @cindex statement, @code{ISO_FORTRAN_ENV}
905 @code{USE} statement with @code{INTRINSIC} and @code{NON_INTRINSIC}
906 attribute; supported intrinsic modules: @code{ISO_FORTRAN_ENV},
907 @code{OMP_LIB} and @code{OMP_LIB_KINDS}.
908
909 @item
910 Renaming of operators in the @code{USE} statement.
911
912 @item
913 @cindex ISO C Bindings
914 Interoperability with C (ISO C Bindings)
915
916 @item
917 BOZ as argument of @code{INT}, @code{REAL}, @code{DBLE} and @code{CMPLX}.
918
919 @item
920 @cindex type-bound procedure
921 @cindex type-bound operator
922 Type-bound procedures with @code{PROCEDURE} or @code{GENERIC}, and operators
923 bound to a derived-type.
924
925 @item
926 @cindex @code{EXTENDS}
927 @cindex derived-type extension
928 Extension of derived-types (the @code{EXTENDS(...)} syntax).
929
930 @item
931 @cindex @code{ABSTRACT} type
932 @cindex @code{DEFERRED} procedure binding
933 @code{ABSTRACT} derived-types and declaring procedure bindings @code{DEFERRED}.
934
935 @end itemize
936
937
938 @node Fortran 2008 status
939 @section Fortran 2008 status
940
941 The next version of the Fortran standard after Fortran 2003 is currently
942 being worked on by the Working Group 5 of Sub-Committee 22 of the Joint
943 Technical Committee 1 of the International Organization for
944 Standardization (ISO) and the International Electrotechnical Commission
945 (IEC). This group is known as @uref{http://www.nag.co.uk/sc22wg5/, WG5}.
946 The next revision of the Fortran standard is informally referred to as
947 Fortran 2008, reflecting its planned release year. The GNU Fortran
948 compiler has support for some of the new features in Fortran 2008. This
949 support is based on the latest draft, available from
950 @url{http://www.nag.co.uk/sc22wg5/}. However, as the final standard may
951 differ from the drafts, no guarantee of backward compatibility can be
952 made and you should only use it for experimental purposes.
953
954 The @uref{http://gcc.gnu.org/wiki/Fortran2008Status, wiki} has some information
955 about the current Fortran 2008 implementation status.
956
957
958 @c ---------------------------------------------------------------------
959 @c Compiler Characteristics
960 @c ---------------------------------------------------------------------
961
962 @node Compiler Characteristics
963 @chapter Compiler Characteristics
964
965 This chapter describes certain characteristics of the GNU Fortran
966 compiler, that are not specified by the Fortran standard, but which
967 might in some way or another become visible to the programmer.
968
969 @menu
970 * KIND Type Parameters::
971 * Internal representation of LOGICAL variables::
972 @end menu
973
974
975 @node KIND Type Parameters
976 @section KIND Type Parameters
977 @cindex kind
978
979 The @code{KIND} type parameters supported by GNU Fortran for the primitive
980 data types are:
981
982 @table @code
983
984 @item INTEGER
985 1, 2, 4, 8*, 16*, default: 4 (1)
986
987 @item LOGICAL
988 1, 2, 4, 8*, 16*, default: 4 (1)
989
990 @item REAL
991 4, 8, 10**, 16**, default: 4 (2)
992
993 @item COMPLEX
994 4, 8, 10**, 16**, default: 4 (2)
995
996 @item CHARACTER
997 1, 4, default: 1
998
999 @end table
1000
1001 @noindent
1002 * = not available on all systems @*
1003 ** = not available on all systems; additionally 10 and 16 are never
1004 available at the same time @*
1005 (1) Unless -fdefault-integer-8 is used @*
1006 (2) Unless -fdefault-real-8 is used
1007
1008 @noindent
1009 The @code{KIND} value matches the storage size in bytes, except for
1010 @code{COMPLEX} where the storage size is twice as much (or both real and
1011 imaginary part are a real value of the given size).  It is recommended to use
1012 the @code{SELECT_*_KIND} intrinsics instead of the concrete values.
1013
1014
1015 @node Internal representation of LOGICAL variables
1016 @section Internal representation of LOGICAL variables
1017 @cindex logical, variable representation
1018
1019 The Fortran standard does not specify how variables of @code{LOGICAL}
1020 type are represented, beyond requiring that @code{LOGICAL} variables
1021 of default kind have the same storage size as default @code{INTEGER}
1022 and @code{REAL} variables.  The GNU Fortran internal representation is
1023 as follows.
1024
1025 A @code{LOGICAL(KIND=N)} variable is represented as an
1026 @code{INTEGER(KIND=N)} variable, however, with only two permissible
1027 values: @code{1} for @code{.TRUE.} and @code{0} for
1028 @code{.FALSE.}. Any other integer value results in undefined behavior.
1029
1030 Note that for mixed-language programming using the
1031 @code{ISO_C_BINDING} feature, there is a @code{C_BOOL} kind that can
1032 be used to create @code{LOGICAL(KIND=C_BOOL)} variables which are
1033 interoperable with the C99 _Bool type.  The C99 _Bool type has an
1034 internal representation described in the C99 standard, which is
1035 identical to the above description, i.e. with 1 for true and 0 for
1036 false being the only permissible values.  Thus the internal
1037 representation of @code{LOGICAL} variables in GNU Fortran is identical
1038 to C99 _Bool, except for a possible difference in storage size
1039 depending on the kind.
1040
1041 @c ---------------------------------------------------------------------
1042 @c Extensions
1043 @c ---------------------------------------------------------------------
1044
1045 @c Maybe this chapter should be merged with the 'Standards' section,
1046 @c whenever that is written :-)
1047
1048 @node Extensions
1049 @chapter Extensions
1050 @cindex extensions
1051
1052 The two sections below detail the extensions to standard Fortran that are
1053 implemented in GNU Fortran, as well as some of the popular or
1054 historically important extensions that are not (or not yet) implemented.
1055 For the latter case, we explain the alternatives available to GNU Fortran
1056 users, including replacement by standard-conforming code or GNU
1057 extensions.
1058
1059 @menu
1060 * Extensions implemented in GNU Fortran::
1061 * Extensions not implemented in GNU Fortran::
1062 @end menu
1063
1064
1065 @node Extensions implemented in GNU Fortran
1066 @section Extensions implemented in GNU Fortran
1067 @cindex extensions, implemented
1068
1069 GNU Fortran implements a number of extensions over standard
1070 Fortran. This chapter contains information on their syntax and
1071 meaning.  There are currently two categories of GNU Fortran
1072 extensions, those that provide functionality beyond that provided
1073 by any standard, and those that are supported by GNU Fortran
1074 purely for backward compatibility with legacy compilers.  By default,
1075 @option{-std=gnu} allows the compiler to accept both types of
1076 extensions, but to warn about the use of the latter.  Specifying
1077 either @option{-std=f95}, @option{-std=f2003} or @option{-std=f2008}
1078 disables both types of extensions, and @option{-std=legacy} allows both
1079 without warning.
1080
1081 @menu
1082 * Old-style kind specifications::
1083 * Old-style variable initialization::
1084 * Extensions to namelist::
1085 * X format descriptor without count field::
1086 * Commas in FORMAT specifications::
1087 * Missing period in FORMAT specifications::
1088 * I/O item lists::
1089 * BOZ literal constants::
1090 * Real array indices::
1091 * Unary operators::
1092 * Implicitly convert LOGICAL and INTEGER values::
1093 * Hollerith constants support::
1094 * Cray pointers::
1095 * CONVERT specifier::
1096 * OpenMP::
1097 * Argument list functions::
1098 @end menu
1099
1100 @node Old-style kind specifications
1101 @subsection Old-style kind specifications
1102 @cindex kind, old-style
1103
1104 GNU Fortran allows old-style kind specifications in declarations. These
1105 look like:
1106 @smallexample
1107       TYPESPEC*size x,y,z
1108 @end smallexample
1109 @noindent
1110 where @code{TYPESPEC} is a basic type (@code{INTEGER}, @code{REAL},
1111 etc.), and where @code{size} is a byte count corresponding to the
1112 storage size of a valid kind for that type.  (For @code{COMPLEX}
1113 variables, @code{size} is the total size of the real and imaginary
1114 parts.)  The statement then declares @code{x}, @code{y} and @code{z} to
1115 be of type @code{TYPESPEC} with the appropriate kind.  This is
1116 equivalent to the standard-conforming declaration
1117 @smallexample
1118       TYPESPEC(k) x,y,z
1119 @end smallexample
1120 @noindent
1121 where @code{k} is the kind parameter suitable for the intended precision.  As
1122 kind parameters are implementation-dependent, use the @code{KIND},
1123 @code{SELECTED_INT_KIND} and @code{SELECTED_REAL_KIND} intrinsics to retrieve
1124 the correct value, for instance @code{REAL*8 x} can be replaced by:
1125 @smallexample
1126 INTEGER, PARAMETER :: dbl = KIND(1.0d0)
1127 REAL(KIND=dbl) :: x
1128 @end smallexample
1129
1130 @node Old-style variable initialization
1131 @subsection Old-style variable initialization
1132
1133 GNU Fortran allows old-style initialization of variables of the
1134 form:
1135 @smallexample
1136       INTEGER i/1/,j/2/
1137       REAL x(2,2) /3*0.,1./
1138 @end smallexample
1139 The syntax for the initializers is as for the @code{DATA} statement, but
1140 unlike in a @code{DATA} statement, an initializer only applies to the
1141 variable immediately preceding the initialization.  In other words,
1142 something like @code{INTEGER I,J/2,3/} is not valid.  This style of
1143 initialization is only allowed in declarations without double colons
1144 (@code{::}); the double colons were introduced in Fortran 90, which also
1145 introduced a standard syntax for initializing variables in type
1146 declarations.
1147
1148 Examples of standard-conforming code equivalent to the above example
1149 are:
1150 @smallexample
1151 ! Fortran 90
1152       INTEGER :: i = 1, j = 2
1153       REAL :: x(2,2) = RESHAPE((/0.,0.,0.,1./),SHAPE(x))
1154 ! Fortran 77
1155       INTEGER i, j
1156       REAL x(2,2)
1157       DATA i/1/, j/2/, x/3*0.,1./
1158 @end smallexample
1159
1160 Note that variables which are explicitly initialized in declarations
1161 or in @code{DATA} statements automatically acquire the @code{SAVE}
1162 attribute.
1163
1164 @node Extensions to namelist
1165 @subsection Extensions to namelist
1166 @cindex Namelist
1167
1168 GNU Fortran fully supports the Fortran 95 standard for namelist I/O
1169 including array qualifiers, substrings and fully qualified derived types.
1170 The output from a namelist write is compatible with namelist read.  The
1171 output has all names in upper case and indentation to column 1 after the
1172 namelist name.  Two extensions are permitted:
1173
1174 Old-style use of @samp{$} instead of @samp{&}
1175 @smallexample
1176 $MYNML
1177  X(:)%Y(2) = 1.0 2.0 3.0
1178  CH(1:4) = "abcd"
1179 $END
1180 @end smallexample
1181
1182 It should be noted that the default terminator is @samp{/} rather than
1183 @samp{&END}.
1184
1185 Querying of the namelist when inputting from stdin. After at least
1186 one space, entering @samp{?} sends to stdout the namelist name and the names of
1187 the variables in the namelist:
1188 @smallexample
1189  ?
1190
1191 &mynml
1192  x
1193  x%y
1194  ch
1195 &end
1196 @end smallexample
1197
1198 Entering @samp{=?} outputs the namelist to stdout, as if
1199 @code{WRITE(*,NML = mynml)} had been called:
1200 @smallexample
1201 =?
1202
1203 &MYNML
1204  X(1)%Y=  0.000000    ,  1.000000    ,  0.000000    ,
1205  X(2)%Y=  0.000000    ,  2.000000    ,  0.000000    ,
1206  X(3)%Y=  0.000000    ,  3.000000    ,  0.000000    ,
1207  CH=abcd,  /
1208 @end smallexample
1209
1210 To aid this dialog, when input is from stdin, errors send their
1211 messages to stderr and execution continues, even if @code{IOSTAT} is set.
1212
1213 @code{PRINT} namelist is permitted.  This causes an error if
1214 @option{-std=f95} is used.
1215 @smallexample
1216 PROGRAM test_print
1217   REAL, dimension (4)  ::  x = (/1.0, 2.0, 3.0, 4.0/)
1218   NAMELIST /mynml/ x
1219   PRINT mynml
1220 END PROGRAM test_print
1221 @end smallexample
1222
1223 Expanded namelist reads are permitted.  This causes an error if 
1224 @option{-std=f95} is used.  In the following example, the first element
1225 of the array will be given the value 0.00 and the two succeeding
1226 elements will be given the values 1.00 and 2.00.
1227 @smallexample
1228 &MYNML
1229   X(1,1) = 0.00 , 1.00 , 2.00
1230 /
1231 @end smallexample
1232
1233 @node X format descriptor without count field
1234 @subsection @code{X} format descriptor without count field
1235
1236 To support legacy codes, GNU Fortran permits the count field of the
1237 @code{X} edit descriptor in @code{FORMAT} statements to be omitted.
1238 When omitted, the count is implicitly assumed to be one.
1239
1240 @smallexample
1241        PRINT 10, 2, 3
1242 10     FORMAT (I1, X, I1)
1243 @end smallexample
1244
1245 @node Commas in FORMAT specifications
1246 @subsection Commas in @code{FORMAT} specifications
1247
1248 To support legacy codes, GNU Fortran allows the comma separator
1249 to be omitted immediately before and after character string edit
1250 descriptors in @code{FORMAT} statements.
1251
1252 @smallexample
1253        PRINT 10, 2, 3
1254 10     FORMAT ('FOO='I1' BAR='I2)
1255 @end smallexample
1256
1257
1258 @node Missing period in FORMAT specifications
1259 @subsection Missing period in @code{FORMAT} specifications
1260
1261 To support legacy codes, GNU Fortran allows missing periods in format
1262 specifications if and only if @option{-std=legacy} is given on the
1263 command line.  This is considered non-conforming code and is
1264 discouraged.
1265
1266 @smallexample
1267        REAL :: value
1268        READ(*,10) value
1269 10     FORMAT ('F4')
1270 @end smallexample
1271
1272 @node I/O item lists
1273 @subsection I/O item lists
1274 @cindex I/O item lists
1275
1276 To support legacy codes, GNU Fortran allows the input item list
1277 of the @code{READ} statement, and the output item lists of the
1278 @code{WRITE} and @code{PRINT} statements, to start with a comma.
1279
1280 @node BOZ literal constants
1281 @subsection BOZ literal constants
1282 @cindex BOZ literal constants
1283
1284 Besides decimal constants, Fortran also supports binary (@code{b}),
1285 octal (@code{o}) and hexadecimal (@code{z}) integer constants. The
1286 syntax is: @samp{prefix quote digits quote}, were the prefix is
1287 either @code{b}, @code{o} or @code{z}, quote is either @code{'} or
1288 @code{"} and the digits are for binary @code{0} or @code{1}, for
1289 octal between @code{0} and @code{7}, and for hexadecimal between
1290 @code{0} and @code{F}. (Example: @code{b'01011101'}.)
1291
1292 Up to Fortran 95, BOZ literals were only allowed to initialize
1293 integer variables in DATA statements. Since Fortran 2003 BOZ literals
1294 are also allowed as argument of @code{REAL}, @code{DBLE}, @code{INT}
1295 and @code{CMPLX}; the result is the same as if the integer BOZ
1296 literal had been converted by @code{TRANSFER} to, respectively,
1297 @code{real}, @code{double precision}, @code{integer} or @code{complex}.
1298 As GNU Fortran extension the intrinsic procedures @code{FLOAT},
1299 @code{DFLOAT}, @code{COMPLEX} and @code{DCMPLX} are treated alike.
1300
1301 As an extension, GNU Fortran allows hexadecimal BOZ literal constants to
1302 be specified using the @code{X} prefix, in addition to the standard
1303 @code{Z} prefix. The BOZ literal can also be specified by adding a
1304 suffix to the string, for example, @code{Z'ABC'} and @code{'ABC'Z} are
1305 equivalent.
1306
1307 Furthermore, GNU Fortran allows using BOZ literal constants outside
1308 DATA statements and the four intrinsic functions allowed by Fortran 2003.
1309 In DATA statements, in direct assignments, where the right-hand side
1310 only contains a BOZ literal constant, and for old-style initializers of
1311 the form @code{integer i /o'0173'/}, the constant is transferred
1312 as if @code{TRANSFER} had been used; for @code{COMPLEX} numbers, only
1313 the real part is initialized unless @code{CMPLX} is used. In all other
1314 cases, the BOZ literal constant is converted to an @code{INTEGER} value with
1315 the largest decimal representation.  This value is then converted
1316 numerically to the type and kind of the variable in question.
1317 (For instance, @code{real :: r = b'0000001' + 1} initializes @code{r}
1318 with @code{2.0}.) As different compilers implement the extension
1319 differently, one should be careful when doing bitwise initialization
1320 of non-integer variables.
1321
1322 Note that initializing an @code{INTEGER} variable with a statement such
1323 as @code{DATA i/Z'FFFFFFFF'/} will give an integer overflow error rather
1324 than the desired result of @math{-1} when @code{i} is a 32-bit integer
1325 on a system that supports 64-bit integers.  The @samp{-fno-range-check}
1326 option can be used as a workaround for legacy code that initializes
1327 integers in this manner.
1328
1329 @node Real array indices
1330 @subsection Real array indices
1331 @cindex array, indices of type real
1332
1333 As an extension, GNU Fortran allows the use of @code{REAL} expressions
1334 or variables as array indices.
1335
1336 @node Unary operators
1337 @subsection Unary operators
1338 @cindex operators, unary
1339
1340 As an extension, GNU Fortran allows unary plus and unary minus operators
1341 to appear as the second operand of binary arithmetic operators without
1342 the need for parenthesis.
1343
1344 @smallexample
1345        X = Y * -Z
1346 @end smallexample
1347
1348 @node Implicitly convert LOGICAL and INTEGER values
1349 @subsection Implicitly convert @code{LOGICAL} and @code{INTEGER} values
1350 @cindex conversion, to integer
1351 @cindex conversion, to logical
1352
1353 As an extension for backwards compatibility with other compilers, GNU
1354 Fortran allows the implicit conversion of @code{LOGICAL} values to
1355 @code{INTEGER} values and vice versa.  When converting from a
1356 @code{LOGICAL} to an @code{INTEGER}, @code{.FALSE.} is interpreted as
1357 zero, and @code{.TRUE.} is interpreted as one.  When converting from
1358 @code{INTEGER} to @code{LOGICAL}, the value zero is interpreted as
1359 @code{.FALSE.} and any nonzero value is interpreted as @code{.TRUE.}.
1360
1361 @smallexample
1362         LOGICAL :: l
1363         l = 1
1364 @end smallexample
1365 @smallexample
1366         INTEGER :: i
1367         i = .TRUE.
1368 @end smallexample
1369
1370 However, there is no implicit conversion of @code{INTEGER} values in
1371 @code{if}-statements, nor of @code{LOGICAL} or @code{INTEGER} values
1372 in I/O operations.
1373
1374 @node Hollerith constants support
1375 @subsection Hollerith constants support
1376 @cindex Hollerith constants
1377
1378 GNU Fortran supports Hollerith constants in assignments, function
1379 arguments, and @code{DATA} and @code{ASSIGN} statements.  A Hollerith
1380 constant is written as a string of characters preceded by an integer
1381 constant indicating the character count, and the letter @code{H} or
1382 @code{h}, and stored in bytewise fashion in a numeric (@code{INTEGER},
1383 @code{REAL}, or @code{complex}) or @code{LOGICAL} variable.  The
1384 constant will be padded or truncated to fit the size of the variable in
1385 which it is stored.
1386
1387 Examples of valid uses of Hollerith constants:
1388 @smallexample
1389       complex*16 x(2)
1390       data x /16Habcdefghijklmnop, 16Hqrstuvwxyz012345/
1391       x(1) = 16HABCDEFGHIJKLMNOP
1392       call foo (4h abc)
1393 @end smallexample
1394
1395 Invalid Hollerith constants examples:
1396 @smallexample
1397       integer*4 a
1398       a = 8H12345678 ! Valid, but the Hollerith constant will be truncated.
1399       a = 0H         ! At least one character is needed.
1400 @end smallexample
1401
1402 In general, Hollerith constants were used to provide a rudimentary
1403 facility for handling character strings in early Fortran compilers,
1404 prior to the introduction of @code{CHARACTER} variables in Fortran 77;
1405 in those cases, the standard-compliant equivalent is to convert the
1406 program to use proper character strings.  On occasion, there may be a
1407 case where the intent is specifically to initialize a numeric variable
1408 with a given byte sequence.  In these cases, the same result can be
1409 obtained by using the @code{TRANSFER} statement, as in this example.
1410 @smallexample
1411       INTEGER(KIND=4) :: a
1412       a = TRANSFER ("abcd", a)     ! equivalent to: a = 4Habcd
1413 @end smallexample
1414
1415
1416 @node Cray pointers
1417 @subsection Cray pointers
1418 @cindex pointer, Cray
1419
1420 Cray pointers are part of a non-standard extension that provides a
1421 C-like pointer in Fortran.  This is accomplished through a pair of
1422 variables: an integer "pointer" that holds a memory address, and a
1423 "pointee" that is used to dereference the pointer.
1424
1425 Pointer/pointee pairs are declared in statements of the form:
1426 @smallexample
1427         pointer ( <pointer> , <pointee> )
1428 @end smallexample
1429 or,
1430 @smallexample
1431         pointer ( <pointer1> , <pointee1> ), ( <pointer2> , <pointee2> ), ...
1432 @end smallexample
1433 The pointer is an integer that is intended to hold a memory address.
1434 The pointee may be an array or scalar.  A pointee can be an assumed
1435 size array---that is, the last dimension may be left unspecified by
1436 using a @code{*} in place of a value---but a pointee cannot be an
1437 assumed shape array.  No space is allocated for the pointee.
1438
1439 The pointee may have its type declared before or after the pointer
1440 statement, and its array specification (if any) may be declared
1441 before, during, or after the pointer statement.  The pointer may be
1442 declared as an integer prior to the pointer statement.  However, some
1443 machines have default integer sizes that are different than the size
1444 of a pointer, and so the following code is not portable:
1445 @smallexample
1446         integer ipt
1447         pointer (ipt, iarr)
1448 @end smallexample
1449 If a pointer is declared with a kind that is too small, the compiler
1450 will issue a warning; the resulting binary will probably not work
1451 correctly, because the memory addresses stored in the pointers may be
1452 truncated.  It is safer to omit the first line of the above example;
1453 if explicit declaration of ipt's type is omitted, then the compiler
1454 will ensure that ipt is an integer variable large enough to hold a
1455 pointer.
1456
1457 Pointer arithmetic is valid with Cray pointers, but it is not the same
1458 as C pointer arithmetic.  Cray pointers are just ordinary integers, so
1459 the user is responsible for determining how many bytes to add to a
1460 pointer in order to increment it.  Consider the following example:
1461 @smallexample
1462         real target(10)
1463         real pointee(10)
1464         pointer (ipt, pointee)
1465         ipt = loc (target)
1466         ipt = ipt + 1       
1467 @end smallexample
1468 The last statement does not set @code{ipt} to the address of
1469 @code{target(1)}, as it would in C pointer arithmetic.  Adding @code{1}
1470 to @code{ipt} just adds one byte to the address stored in @code{ipt}.
1471
1472 Any expression involving the pointee will be translated to use the
1473 value stored in the pointer as the base address.
1474
1475 To get the address of elements, this extension provides an intrinsic
1476 function @code{LOC()}.  The @code{LOC()} function is equivalent to the
1477 @code{&} operator in C, except the address is cast to an integer type:
1478 @smallexample
1479         real ar(10)
1480         pointer(ipt, arpte(10))
1481         real arpte
1482         ipt = loc(ar)  ! Makes arpte is an alias for ar
1483         arpte(1) = 1.0 ! Sets ar(1) to 1.0
1484 @end smallexample
1485 The pointer can also be set by a call to the @code{MALLOC} intrinsic
1486 (see @ref{MALLOC}).
1487
1488 Cray pointees often are used to alias an existing variable.  For
1489 example:
1490 @smallexample
1491         integer target(10)
1492         integer iarr(10)
1493         pointer (ipt, iarr)
1494         ipt = loc(target)
1495 @end smallexample
1496 As long as @code{ipt} remains unchanged, @code{iarr} is now an alias for
1497 @code{target}. The optimizer, however, will not detect this aliasing, so
1498 it is unsafe to use @code{iarr} and @code{target} simultaneously.  Using
1499 a pointee in any way that violates the Fortran aliasing rules or
1500 assumptions is illegal. It is the user's responsibility to avoid doing
1501 this; the compiler works under the assumption that no such aliasing
1502 occurs.
1503
1504 Cray pointers will work correctly when there is no aliasing (i.e., when
1505 they are used to access a dynamically allocated block of memory), and
1506 also in any routine where a pointee is used, but any variable with which
1507 it shares storage is not used.  Code that violates these rules may not
1508 run as the user intends.  This is not a bug in the optimizer; any code
1509 that violates the aliasing rules is illegal.  (Note that this is not
1510 unique to GNU Fortran; any Fortran compiler that supports Cray pointers
1511 will ``incorrectly'' optimize code with illegal aliasing.)
1512
1513 There are a number of restrictions on the attributes that can be applied
1514 to Cray pointers and pointees.  Pointees may not have the
1515 @code{ALLOCATABLE}, @code{INTENT}, @code{OPTIONAL}, @code{DUMMY},
1516 @code{TARGET}, @code{INTRINSIC}, or @code{POINTER} attributes. Pointers
1517 may not have the @code{DIMENSION}, @code{POINTER}, @code{TARGET},
1518 @code{ALLOCATABLE}, @code{EXTERNAL}, or @code{INTRINSIC} attributes, nor
1519 may they be function results. Pointees may not occur in more than one
1520 pointer statement.  A pointee cannot be a pointer.  Pointees cannot occur
1521 in equivalence, common, or data statements.
1522
1523 A Cray pointer may also point to a function or a subroutine.  For
1524 example, the following excerpt is valid:
1525 @smallexample
1526   implicit none
1527   external sub
1528   pointer (subptr,subpte)
1529   external subpte
1530   subptr = loc(sub)
1531   call subpte()
1532   [...]
1533   subroutine sub
1534   [...]
1535   end subroutine sub
1536 @end smallexample
1537
1538 A pointer may be modified during the course of a program, and this
1539 will change the location to which the pointee refers.  However, when
1540 pointees are passed as arguments, they are treated as ordinary
1541 variables in the invoked function.  Subsequent changes to the pointer
1542 will not change the base address of the array that was passed.
1543
1544 @node CONVERT specifier
1545 @subsection @code{CONVERT} specifier
1546 @cindex @code{CONVERT} specifier
1547
1548 GNU Fortran allows the conversion of unformatted data between little-
1549 and big-endian representation to facilitate moving of data
1550 between different systems.  The conversion can be indicated with
1551 the @code{CONVERT} specifier on the @code{OPEN} statement.
1552 @xref{GFORTRAN_CONVERT_UNIT}, for an alternative way of specifying
1553 the data format via an environment variable.
1554
1555 Valid values for @code{CONVERT} are:
1556 @itemize @w{}
1557 @item @code{CONVERT='NATIVE'} Use the native format.  This is the default.
1558 @item @code{CONVERT='SWAP'} Swap between little- and big-endian.
1559 @item @code{CONVERT='LITTLE_ENDIAN'} Use the little-endian representation
1560 for unformatted files.
1561 @item @code{CONVERT='BIG_ENDIAN'} Use the big-endian representation for
1562 unformatted files.
1563 @end itemize
1564
1565 Using the option could look like this:
1566 @smallexample
1567   open(file='big.dat',form='unformatted',access='sequential', &
1568        convert='big_endian')
1569 @end smallexample
1570
1571 The value of the conversion can be queried by using
1572 @code{INQUIRE(CONVERT=ch)}.  The values returned are
1573 @code{'BIG_ENDIAN'} and @code{'LITTLE_ENDIAN'}.
1574
1575 @code{CONVERT} works between big- and little-endian for
1576 @code{INTEGER} values of all supported kinds and for @code{REAL}
1577 on IEEE systems of kinds 4 and 8.  Conversion between different
1578 ``extended double'' types on different architectures such as
1579 m68k and x86_64, which GNU Fortran
1580 supports as @code{REAL(KIND=10)} and @code{REAL(KIND=16)}, will
1581 probably not work.
1582
1583 @emph{Note that the values specified via the GFORTRAN_CONVERT_UNIT
1584 environment variable will override the CONVERT specifier in the
1585 open statement}.  This is to give control over data formats to
1586 users who do not have the source code of their program available.
1587
1588 Using anything but the native representation for unformatted data
1589 carries a significant speed overhead.  If speed in this area matters
1590 to you, it is best if you use this only for data that needs to be
1591 portable.
1592
1593 @node OpenMP
1594 @subsection OpenMP
1595 @cindex OpenMP
1596
1597 OpenMP (Open Multi-Processing) is an application programming
1598 interface (API) that supports multi-platform shared memory 
1599 multiprocessing programming in C/C++ and Fortran on many 
1600 architectures, including Unix and Microsoft Windows platforms.
1601 It consists of a set of compiler directives, library routines,
1602 and environment variables that influence run-time behavior.
1603
1604 GNU Fortran strives to be compatible to the 
1605 @uref{http://www.openmp.org/mp-documents/spec30.pdf,
1606 OpenMP Application Program Interface v3.0}.
1607
1608 To enable the processing of the OpenMP directive @code{!$omp} in
1609 free-form source code; the @code{c$omp}, @code{*$omp} and @code{!$omp}
1610 directives in fixed form; the @code{!$} conditional compilation sentinels
1611 in free form; and the @code{c$}, @code{*$} and @code{!$} sentinels
1612 in fixed form, @command{gfortran} needs to be invoked with the
1613 @option{-fopenmp}. This also arranges for automatic linking of the
1614 GNU OpenMP runtime library @ref{Top,,libgomp,libgomp,GNU OpenMP
1615 runtime library}.
1616
1617 The OpenMP Fortran runtime library routines are provided both in a
1618 form of a Fortran 90 module named @code{omp_lib} and in a form of
1619 a Fortran @code{include} file named @file{omp_lib.h}.
1620
1621 An example of a parallelized loop taken from Appendix A.1 of
1622 the OpenMP Application Program Interface v2.5:
1623 @smallexample
1624 SUBROUTINE A1(N, A, B)
1625   INTEGER I, N
1626   REAL B(N), A(N)
1627 !$OMP PARALLEL DO !I is private by default
1628   DO I=2,N
1629     B(I) = (A(I) + A(I-1)) / 2.0
1630   ENDDO
1631 !$OMP END PARALLEL DO
1632 END SUBROUTINE A1
1633 @end smallexample
1634
1635 Please note:
1636 @itemize
1637 @item
1638 @option{-fopenmp} implies @option{-frecursive}, i.e., all local arrays
1639 will be allocated on the stack. When porting existing code to OpenMP,
1640 this may lead to surprising results, especially to segmentation faults
1641 if the stacksize is limited.
1642
1643 @item
1644 On glibc-based systems, OpenMP enabled applications cannot be statically
1645 linked due to limitations of the underlying pthreads-implementation. It
1646 might be possible to get a working solution if 
1647 @command{-Wl,--whole-archive -lpthread -Wl,--no-whole-archive} is added
1648 to the command line. However, this is not supported by @command{gcc} and
1649 thus not recommended.
1650 @end itemize
1651
1652 @node Argument list functions
1653 @subsection Argument list functions @code{%VAL}, @code{%REF} and @code{%LOC}
1654 @cindex argument list functions
1655 @cindex @code{%VAL}
1656 @cindex @code{%REF}
1657 @cindex @code{%LOC}
1658
1659 GNU Fortran supports argument list functions @code{%VAL}, @code{%REF} 
1660 and @code{%LOC} statements, for backward compatibility with g77. 
1661 It is recommended that these should be used only for code that is 
1662 accessing facilities outside of GNU Fortran, such as operating system 
1663 or windowing facilities. It is best to constrain such uses to isolated 
1664 portions of a program--portions that deal specifically and exclusively 
1665 with low-level, system-dependent facilities. Such portions might well 
1666 provide a portable interface for use by the program as a whole, but are 
1667 themselves not portable, and should be thoroughly tested each time they 
1668 are rebuilt using a new compiler or version of a compiler.
1669
1670 @code{%VAL} passes a scalar argument by value, @code{%REF} passes it by 
1671 reference and @code{%LOC} passes its memory location.  Since gfortran 
1672 already passes scalar arguments by reference, @code{%REF} is in effect 
1673 a do-nothing.  @code{%LOC} has the same effect as a Fortran pointer.
1674
1675 An example of passing an argument by value to a C subroutine foo.:
1676 @smallexample
1677 C
1678 C prototype      void foo_ (float x);
1679 C
1680       external foo
1681       real*4 x
1682       x = 3.14159
1683       call foo (%VAL (x))
1684       end
1685 @end smallexample
1686
1687 For details refer to the g77 manual
1688 @uref{http://gcc.gnu.org/onlinedocs/gcc-3.4.6/g77/index.html#Top}.
1689
1690 Also, @code{c_by_val.f} and its partner @code{c_by_val.c} of the
1691 GNU Fortran testsuite are worth a look.
1692
1693
1694 @node Extensions not implemented in GNU Fortran
1695 @section Extensions not implemented in GNU Fortran
1696 @cindex extensions, not implemented
1697
1698 The long history of the Fortran language, its wide use and broad
1699 userbase, the large number of different compiler vendors and the lack of
1700 some features crucial to users in the first standards have lead to the
1701 existence of a number of important extensions to the language.  While
1702 some of the most useful or popular extensions are supported by the GNU
1703 Fortran compiler, not all existing extensions are supported.  This section
1704 aims at listing these extensions and offering advice on how best make
1705 code that uses them running with the GNU Fortran compiler.
1706
1707 @c More can be found here:
1708 @c   -- http://gcc.gnu.org/onlinedocs/gcc-3.4.6/g77/Missing-Features.html
1709 @c   -- the list of Fortran and libgfortran bugs closed as WONTFIX:
1710 @c      http://tinyurl.com/2u4h5y
1711
1712 @menu
1713 * STRUCTURE and RECORD::
1714 @c * UNION and MAP::
1715 * ENCODE and DECODE statements::
1716 * Variable FORMAT expressions::
1717 @c * Q edit descriptor::
1718 @c * AUTOMATIC statement::
1719 @c * TYPE and ACCEPT I/O Statements::
1720 @c * .XOR. operator::
1721 @c * CARRIAGECONTROL, DEFAULTFILE, DISPOSE and RECORDTYPE I/O specifiers::
1722 @c * Omitted arguments in procedure call::
1723 * Alternate complex function syntax::
1724 @end menu
1725
1726
1727 @node STRUCTURE and RECORD
1728 @subsection @code{STRUCTURE} and @code{RECORD}
1729 @cindex @code{STRUCTURE}
1730 @cindex @code{RECORD}
1731
1732 Structures are user-defined aggregate data types; this functionality was
1733 standardized in Fortran 90 with an different syntax, under the name of
1734 ``derived types''. Here is an example of code using the non portable
1735 structure syntax:
1736
1737 @example
1738 ! Declaring a structure named ``item'' and containing three fields:
1739 ! an integer ID, an description string and a floating-point price.
1740 STRUCTURE /item/
1741   INTEGER id
1742   CHARACTER(LEN=200) description
1743   REAL price
1744 END STRUCTURE
1745
1746 ! Define two variables, an single record of type ``item''
1747 ! named ``pear'', and an array of items named ``store_catalog''
1748 RECORD /item/ pear, store_catalog(100)
1749
1750 ! We can directly access the fields of both variables
1751 pear.id = 92316
1752 pear.description = "juicy D'Anjou pear"
1753 pear.price = 0.15
1754 store_catalog(7).id = 7831
1755 store_catalog(7).description = "milk bottle"
1756 store_catalog(7).price = 1.2
1757
1758 ! We can also manipulate the whole structure
1759 store_catalog(12) = pear
1760 print *, store_catalog(12)
1761 @end example
1762
1763 @noindent
1764 This code can easily be rewritten in the Fortran 90 syntax as following:
1765
1766 @example
1767 ! ``STRUCTURE /name/ ... END STRUCTURE'' becomes
1768 ! ``TYPE name ... END TYPE''
1769 TYPE item
1770   INTEGER id
1771   CHARACTER(LEN=200) description
1772   REAL price
1773 END TYPE
1774
1775 ! ``RECORD /name/ variable'' becomes ``TYPE(name) variable''
1776 TYPE(item) pear, store_catalog(100)
1777
1778 ! Instead of using a dot (.) to access fields of a record, the
1779 ! standard syntax uses a percent sign (%)
1780 pear%id = 92316
1781 pear%description = "juicy D'Anjou pear"
1782 pear%price = 0.15
1783 store_catalog(7)%id = 7831
1784 store_catalog(7)%description = "milk bottle"
1785 store_catalog(7)%price = 1.2
1786
1787 ! Assignments of a whole variable don't change
1788 store_catalog(12) = pear
1789 print *, store_catalog(12)
1790 @end example
1791
1792
1793 @c @node UNION and MAP
1794 @c @subsection @code{UNION} and @code{MAP}
1795 @c @cindex @code{UNION}
1796 @c @cindex @code{MAP}
1797 @c
1798 @c For help writing this one, see
1799 @c http://www.eng.umd.edu/~nsw/ench250/fortran1.htm#UNION and
1800 @c http://www.tacc.utexas.edu/services/userguides/pgi/pgiws_ug/pgi32u06.htm
1801
1802
1803 @node ENCODE and DECODE statements
1804 @subsection @code{ENCODE} and @code{DECODE} statements
1805 @cindex @code{ENCODE}
1806 @cindex @code{DECODE}
1807
1808 GNU Fortran doesn't support the @code{ENCODE} and @code{DECODE}
1809 statements.  These statements are best replaced by @code{READ} and
1810 @code{WRITE} statements involving internal files (@code{CHARACTER}
1811 variables and arrays), which have been part of the Fortran standard since
1812 Fortran 77. For example, replace a code fragment like
1813
1814 @smallexample
1815       INTEGER*1 LINE(80)
1816       REAL A, B, C
1817 c     ... Code that sets LINE
1818       DECODE (80, 9000, LINE) A, B, C
1819  9000 FORMAT (1X, 3(F10.5))
1820 @end smallexample
1821
1822 @noindent
1823 with the following:
1824
1825 @smallexample
1826       CHARACTER(LEN=80) LINE
1827       REAL A, B, C
1828 c     ... Code that sets LINE
1829       READ (UNIT=LINE, FMT=9000) A, B, C
1830  9000 FORMAT (1X, 3(F10.5))
1831 @end smallexample
1832
1833 Similarly, replace a code fragment like
1834
1835 @smallexample
1836       INTEGER*1 LINE(80)
1837       REAL A, B, C
1838 c     ... Code that sets A, B and C
1839       ENCODE (80, 9000, LINE) A, B, C
1840  9000 FORMAT (1X, 'OUTPUT IS ', 3(F10.5))
1841 @end smallexample
1842
1843 @noindent
1844 with the following:
1845
1846 @smallexample
1847       CHARACTER(LEN=80) LINE
1848       REAL A, B, C
1849 c     ... Code that sets A, B and C
1850       WRITE (UNIT=LINE, FMT=9000) A, B, C
1851  9000 FORMAT (1X, 'OUTPUT IS ', 3(F10.5))
1852 @end smallexample
1853
1854
1855 @node Variable FORMAT expressions
1856 @subsection Variable @code{FORMAT} expressions
1857 @cindex @code{FORMAT}
1858
1859 A variable @code{FORMAT} expression is format statement which includes
1860 angle brackets enclosing a Fortran expression: @code{FORMAT(I<N>)}. GNU
1861 Fortran does not support this legacy extension. The effect of variable
1862 format expressions can be reproduced by using the more powerful (and
1863 standard) combination of internal output and string formats. For example,
1864 replace a code fragment like this:
1865
1866 @smallexample
1867       WRITE(6,20) INT1
1868  20   FORMAT(I<N+1>)
1869 @end smallexample
1870
1871 @noindent
1872 with the following:
1873
1874 @smallexample
1875 c     Variable declaration
1876       CHARACTER(LEN=20) FMT
1877 c     
1878 c     Other code here...
1879 c
1880       WRITE(FMT,'("(I", I0, ")")') N+1
1881       WRITE(6,FMT) INT1
1882 @end smallexample
1883
1884 @noindent
1885 or with:
1886
1887 @smallexample
1888 c     Variable declaration
1889       CHARACTER(LEN=20) FMT
1890 c     
1891 c     Other code here...
1892 c
1893       WRITE(FMT,*) N+1
1894       WRITE(6,"(I" // ADJUSTL(FMT) // ")") INT1
1895 @end smallexample
1896
1897
1898 @node Alternate complex function syntax
1899 @subsection Alternate complex function syntax
1900 @cindex Complex function
1901
1902 Some Fortran compilers, including @command{g77}, let the user declare
1903 complex functions with the syntax @code{COMPLEX FUNCTION name*16()}, as
1904 well as @code{COMPLEX*16 FUNCTION name()}. Both are non-standard, legacy
1905 extensions. @command{gfortran} accepts the latter form, which is more
1906 common, but not the former.
1907
1908
1909
1910 @c ---------------------------------------------------------------------
1911 @c Mixed-Language Programming
1912 @c ---------------------------------------------------------------------
1913
1914 @node Mixed-Language Programming
1915 @chapter Mixed-Language Programming
1916 @cindex Interoperability
1917 @cindex Mixed-language programming
1918
1919 @menu
1920 * Interoperability with C::
1921 * GNU Fortran Compiler Directives::
1922 * Non-Fortran Main Program::
1923 @end menu
1924
1925 This chapter is about mixed-language interoperability, but also applies
1926 if one links Fortran code compiled by different compilers. In most cases,
1927 use of the C Binding features of the Fortran 2003 standard is sufficient,
1928 and their use is highly recommended.
1929
1930
1931 @node Interoperability with C
1932 @section Interoperability with C
1933
1934 @menu
1935 * Intrinsic Types::
1936 * Derived Types and struct::
1937 * Interoperable Global Variables::
1938 * Interoperable Subroutines and Functions::
1939 * Working with Pointers::
1940 * Further Interoperability of Fortran with C::
1941 @end menu
1942
1943 Since Fortran 2003 (ISO/IEC 1539-1:2004(E)) there is a
1944 standardized way to generate procedure and derived-type
1945 declarations and global variables which are interoperable with C
1946 (ISO/IEC 9899:1999). The @code{bind(C)} attribute has been added
1947 to inform the compiler that a symbol shall be interoperable with C;
1948 also, some constraints are added. Note, however, that not
1949 all C features have a Fortran equivalent or vice versa. For instance,
1950 neither C's unsigned integers nor C's functions with variable number
1951 of arguments have an equivalent in Fortran.
1952
1953 Note that array dimensions are reversely ordered in C and that arrays in
1954 C always start with index 0 while in Fortran they start by default with
1955 1. Thus, an array declaration @code{A(n,m)} in Fortran matches
1956 @code{A[m][n]} in C and accessing the element @code{A(i,j)} matches
1957 @code{A[j-1][i-1]}. The element following @code{A(i,j)} (C: @code{A[j-1][i-1]};
1958 assuming @math{i < n}) in memory is @code{A(i+1,j)} (C: @code{A[j-1][i]}).
1959
1960 @node Intrinsic Types
1961 @subsection Intrinsic Types
1962
1963 In order to ensure that exactly the same variable type and kind is used
1964 in C and Fortran, the named constants shall be used which are defined in the
1965 @code{ISO_C_BINDING} intrinsic module. That module contains named constants
1966 for kind parameters and character named constants for the escape sequences
1967 in C. For a list of the constants, see @ref{ISO_C_BINDING}.
1968
1969 @node Derived Types and struct
1970 @subsection Derived Types and struct
1971
1972 For compatibility of derived types with @code{struct}, one needs to use
1973 the @code{BIND(C)} attribute in the type declaration. For instance, the
1974 following type declaration
1975
1976 @smallexample
1977  USE ISO_C_BINDING
1978  TYPE, BIND(C) :: myType
1979    INTEGER(C_INT) :: i1, i2
1980    INTEGER(C_SIGNED_CHAR) :: i3
1981    REAL(C_DOUBLE) :: d1
1982    COMPLEX(C_FLOAT_COMPLEX) :: c1
1983    CHARACTER(KIND=C_CHAR) :: str(5)
1984  END TYPE
1985 @end smallexample
1986
1987 matches the following @code{struct} declaration in C
1988
1989 @smallexample
1990  struct @{
1991    int i1, i2;
1992    /* Note: "char" might be signed or unsigned.  */
1993    signed char i3;
1994    double d1;
1995    float _Complex c1;
1996    char str[5];
1997  @} myType;
1998 @end smallexample
1999
2000 Derived types with the C binding attribute shall not have the @code{sequence}
2001 attribute, type parameters, the @code{extends} attribute, nor type-bound
2002 procedures. Every component must be of interoperable type and kind and may not
2003 have the @code{pointer} or @code{allocatable} attribute. The names of the
2004 variables are irrelevant for interoperability.
2005
2006 As there exist no direct Fortran equivalents, neither unions nor structs
2007 with bit field or variable-length array members are interoperable.
2008
2009 @node Interoperable Global Variables
2010 @subsection Interoperable Global Variables
2011
2012 Variables can be made accessible from C using the C binding attribute,
2013 optionally together with specifying a binding name. Those variables
2014 have to be declared in the declaration part of a @code{MODULE},
2015 be of interoperable type, and have neither the @code{pointer} nor
2016 the @code{allocatable} attribute.
2017
2018 @smallexample
2019   MODULE m
2020     USE myType_module
2021     USE ISO_C_BINDING
2022     integer(C_INT), bind(C, name="_MyProject_flags") :: global_flag
2023     type(myType), bind(C) :: tp
2024   END MODULE
2025 @end smallexample
2026
2027 Here, @code{_MyProject_flags} is the case-sensitive name of the variable
2028 as seen from C programs while @code{global_flag} is the case-insensitive
2029 name as seen from Fortran. If no binding name is specified, as for
2030 @var{tp}, the C binding name is the (lowercase) Fortran binding name.
2031 If a binding name is specified, only a single variable may be after the
2032 double colon. Note of warning: You cannot use a global variable to
2033 access @var{errno} of the C library as the C standard allows it to be
2034 a macro. Use the @code{IERRNO} intrinsic (GNU extension) instead.
2035
2036 @node Interoperable Subroutines and Functions
2037 @subsection Interoperable Subroutines and Functions
2038
2039 Subroutines and functions have to have the @code{BIND(C)} attribute to
2040 be compatible with C. The dummy argument declaration is relatively
2041 straightforward. However, one needs to be careful because C uses
2042 call-by-value by default while Fortran behaves usually similar to
2043 call-by-reference. Furthermore, strings and pointers are handled
2044 differently. Note that only explicit size and assumed-size arrays are
2045 supported but not assumed-shape or allocatable arrays.
2046
2047 To pass a variable by value, use the @code{VALUE} attribute.
2048 Thus the following C prototype
2049
2050 @smallexample
2051 @code{int func(int i, int *j)}
2052 @end smallexample
2053
2054 matches the Fortran declaration
2055
2056 @smallexample
2057   integer(c_int) function func(i,j)
2058     use iso_c_binding, only: c_int
2059     integer(c_int), VALUE :: i
2060     integer(c_int) :: j
2061 @end smallexample
2062
2063 Note that pointer arguments also frequently need the @code{VALUE} attribute,
2064 see @ref{Working with Pointers}.
2065
2066 Strings are handled quite differently in C and Fortran. In C a string
2067 is a @code{NUL}-terminated array of characters while in Fortran each string
2068 has a length associated with it and is thus not terminated (by e.g.
2069 @code{NUL}). For example, if one wants to use the following C function,
2070
2071 @smallexample
2072   #include <stdio.h>
2073   void print_C(char *string) /* equivalent: char string[]  */
2074   @{
2075      printf("%s\n", string);
2076   @}
2077 @end smallexample
2078
2079 to print ``Hello World'' from Fortran, one can call it using
2080
2081 @smallexample
2082   use iso_c_binding, only: C_CHAR, C_NULL_CHAR
2083   interface
2084     subroutine print_c(string) bind(C, name="print_C")
2085       use iso_c_binding, only: c_char
2086       character(kind=c_char) :: string(*)
2087     end subroutine print_c
2088   end interface
2089   call print_c(C_CHAR_"Hello World"//C_NULL_CHAR)
2090 @end smallexample
2091
2092 As the example shows, one needs to ensure that the
2093 string is @code{NUL} terminated. Additionally, the dummy argument
2094 @var{string} of @code{print_C} is a length-one assumed-size
2095 array; using @code{character(len=*)} is not allowed. The example
2096 above uses @code{c_char_"Hello World"} to ensure the string
2097 literal has the right type; typically the default character
2098 kind and @code{c_char} are the same and thus @code{"Hello World"}
2099 is equivalent. However, the standard does not guarantee this.
2100
2101 The use of strings is now further illustrated using the C library
2102 function @code{strncpy}, whose prototype is
2103
2104 @smallexample
2105   char *strncpy(char *restrict s1, const char *restrict s2, size_t n);
2106 @end smallexample
2107
2108 The function @code{strncpy} copies at most @var{n} characters from
2109 string @var{s2} to @var{s1} and returns @var{s1}. In the following
2110 example, we ignore the return value:
2111
2112 @smallexample
2113   use iso_c_binding
2114   implicit none
2115   character(len=30) :: str,str2
2116   interface
2117     ! Ignore the return value of strncpy -> subroutine
2118     ! "restrict" is always assumed if we do not pass a pointer
2119     subroutine strncpy(dest, src, n) bind(C)
2120       import
2121       character(kind=c_char),  intent(out) :: dest(*)
2122       character(kind=c_char),  intent(in)  :: src(*)
2123       integer(c_size_t), value, intent(in) :: n
2124     end subroutine strncpy
2125   end interface
2126   str = repeat('X',30) ! Initialize whole string with 'X'
2127   call strncpy(str, c_char_"Hello World"//C_NULL_CHAR, &
2128                len(c_char_"Hello World",kind=c_size_t))
2129   print '(a)', str ! prints: "Hello WorldXXXXXXXXXXXXXXXXXXX"
2130   end
2131 @end smallexample
2132
2133 The intrinsic procedures are described in @ref{Intrinsic Procedures}.
2134
2135 @node Working with Pointers
2136 @subsection Working with Pointers
2137
2138 C pointers are represented in Fortran via the special opaque derived type
2139 @code{type(c_ptr)} (with private components). Thus one needs to
2140 use intrinsic conversion procedures to convert from or to C pointers.
2141 For example,
2142
2143 @smallexample
2144   use iso_c_binding
2145   type(c_ptr) :: cptr1, cptr2
2146   integer, target :: array(7), scalar
2147   integer, pointer :: pa(:), ps
2148   cptr1 = c_loc(array(1)) ! The programmer needs to ensure that the
2149                           ! array is contiguous if required by the C
2150                           ! procedure
2151   cptr2 = c_loc(scalar)
2152   call c_f_pointer(cptr2, ps)
2153   call c_f_pointer(cptr2, pa, shape=[7])
2154 @end smallexample
2155
2156 When converting C to Fortran arrays, the one-dimensional @code{SHAPE} argument
2157 has to be passed.
2158
2159 If a pointer is a dummy-argument of an interoperable procedure, it usually
2160 has to be declared using the @code{VALUE} attribute.  @code{void*}
2161 matches @code{TYPE(C_PTR), VALUE}, while @code{TYPE(C_PTR)} alone
2162 matches @code{void**}.
2163
2164 Procedure pointers are handled analogously to pointers; the C type is
2165 @code{TYPE(C_FUNPTR)} and the intrinsic conversion procedures are
2166 @code{C_F_PROCPOINTER} and @code{C_FUNLOC}.
2167
2168 Let's consider two examples of actually passing a procedure pointer from
2169 C to Fortran and vice versa.  Note that these examples are also very
2170 similar to passing ordinary pointers between both languages.
2171 First, consider this code in C:
2172
2173 @smallexample
2174 /* Procedure implemented in Fortran.  */
2175 void get_values (void (*)(double));
2176
2177 /* Call-back routine we want called from Fortran.  */
2178 void
2179 print_it (double x)
2180 @{
2181   printf ("Number is %f.\n", x);
2182 @}
2183
2184 /* Call Fortran routine and pass call-back to it.  */
2185 void
2186 foobar ()
2187 @{
2188   get_values (&print_it);
2189 @}
2190 @end smallexample
2191
2192 A matching implementation for @code{get_values} in Fortran, that correctly
2193 receives the procedure pointer from C and is able to call it, is given
2194 in the following @code{MODULE}:
2195
2196 @smallexample
2197 MODULE m
2198   IMPLICIT NONE
2199
2200   ! Define interface of call-back routine.
2201   ABSTRACT INTERFACE
2202     SUBROUTINE callback (x)
2203       USE, INTRINSIC :: ISO_C_BINDING
2204       REAL(KIND=C_DOUBLE), INTENT(IN), VALUE :: x
2205     END SUBROUTINE callback
2206   END INTERFACE
2207
2208 CONTAINS
2209
2210   ! Define C-bound procedure.
2211   SUBROUTINE get_values (cproc) BIND(C)
2212     USE, INTRINSIC :: ISO_C_BINDING
2213     TYPE(C_FUNPTR), INTENT(IN), VALUE :: cproc
2214
2215     PROCEDURE(callback), POINTER :: proc
2216
2217     ! Convert C to Fortran procedure pointer.
2218     CALL C_F_PROCPOINTER (cproc, proc)
2219
2220     ! Call it.
2221     CALL proc (1.0_C_DOUBLE)
2222     CALL proc (-42.0_C_DOUBLE)
2223     CALL proc (18.12_C_DOUBLE)
2224   END SUBROUTINE get_values
2225
2226 END MODULE m
2227 @end smallexample
2228
2229 Next, we want to call a C routine that expects a procedure pointer argument
2230 and pass it a Fortran procedure (which clearly must be interoperable!).
2231 Again, the C function may be:
2232
2233 @smallexample
2234 int
2235 call_it (int (*func)(int), int arg)
2236 @{
2237   return func (arg);
2238 @}
2239 @end smallexample
2240
2241 It can be used as in the following Fortran code:
2242
2243 @smallexample
2244 MODULE m
2245   USE, INTRINSIC :: ISO_C_BINDING
2246   IMPLICIT NONE
2247
2248   ! Define interface of C function.
2249   INTERFACE
2250     INTEGER(KIND=C_INT) FUNCTION call_it (func, arg) BIND(C)
2251       USE, INTRINSIC :: ISO_C_BINDING
2252       TYPE(C_FUNPTR), INTENT(IN), VALUE :: func
2253       INTEGER(KIND=C_INT), INTENT(IN), VALUE :: arg
2254     END FUNCTION call_it
2255   END INTERFACE
2256
2257 CONTAINS
2258
2259   ! Define procedure passed to C function.
2260   ! It must be interoperable!
2261   INTEGER(KIND=C_INT) FUNCTION double_it (arg) BIND(C)
2262     INTEGER(KIND=C_INT), INTENT(IN), VALUE :: arg
2263     double_it = arg + arg
2264   END FUNCTION double_it
2265
2266   ! Call C function.
2267   SUBROUTINE foobar ()
2268     TYPE(C_FUNPTR) :: cproc
2269     INTEGER(KIND=C_INT) :: i
2270
2271     ! Get C procedure pointer.
2272     cproc = C_FUNLOC (double_it)
2273
2274     ! Use it.
2275     DO i = 1_C_INT, 10_C_INT
2276       PRINT *, call_it (cproc, i)
2277     END DO
2278   END SUBROUTINE foobar
2279
2280 END MODULE m
2281 @end smallexample
2282
2283 @node Further Interoperability of Fortran with C
2284 @subsection Further Interoperability of Fortran with C
2285
2286 Assumed-shape and allocatable arrays are passed using an array descriptor
2287 (dope vector). The internal structure of the array descriptor used
2288 by GNU Fortran is not yet documented and will change. There will also be
2289 a Technical Report (TR 29113) which standardizes an interoperable
2290 array descriptor. Until then, you can use the Chasm Language
2291 Interoperability Tools, @url{http://chasm-interop.sourceforge.net/},
2292 which provide an interface to GNU Fortran's array descriptor.
2293
2294 The technical report 29113 will presumably also include support for
2295 C-interoperable @code{OPTIONAL} and for assumed-rank and assumed-type
2296 dummy arguments. However, the TR has neither been approved nor implemented
2297 in GNU Fortran; therefore, these features are not yet available.
2298
2299
2300
2301 @node GNU Fortran Compiler Directives
2302 @section GNU Fortran Compiler Directives
2303
2304 The Fortran standard standard describes how a conforming program shall
2305 behave; however, the exact implementation is not standardized. In order
2306 to allow the user to choose specific implementation details, compiler
2307 directives can be used to set attributes of variables and procedures
2308 which are not part of the standard. Whether a given attribute is
2309 supported and its exact effects depend on both the operating system and
2310 on the processor; see
2311 @ref{Top,,C Extensions,gcc,Using the GNU Compiler Collection (GCC)}
2312 for details.
2313
2314 For procedures and procedure pointers, the following attributes can
2315 be used to change the calling convention:
2316
2317 @itemize
2318 @item @code{CDECL} -- standard C calling convention
2319 @item @code{STDCALL} -- convention where the called procedure pops the stack
2320 @item @code{FASTCALL} -- part of the arguments are passed via registers
2321 instead using the stack
2322 @end itemize
2323
2324 Besides changing the calling convention, the attributes also influence
2325 the decoration of the symbol name, e.g., by a leading underscore or by
2326 a trailing at-sign followed by the number of bytes on the stack. When
2327 assigning a procedure to a procedure pointer, both should use the same
2328 calling convention.
2329
2330 On some systems, procedures and global variables (module variables and
2331 @code{COMMON} blocks) need special handling to be accessible when they
2332 are in a shared library. The following attributes are available:
2333
2334 @itemize
2335 @item @code{DLLEXPORT} -- provide a global pointer to a pointer in the DLL
2336 @item @code{DLLIMPORT} -- reference the function or variable using a global pointer 
2337 @end itemize
2338
2339 The attributes are specified using the syntax
2340
2341 @code{!GCC$ ATTRIBUTES} @var{attribute-list} @code{::} @var{variable-list}
2342
2343 where in free-form source code only whitespace is allowed before @code{!GCC$}
2344 and in fixed-form source code @code{!GCC$}, @code{cGCC$} or @code{*GCC$} shall
2345 start in the first column.
2346
2347 For procedures, the compiler directives shall be placed into the body
2348 of the procedure; for variables and procedure pointers, they shall be in
2349 the same declaration part as the variable or procedure pointer.
2350
2351
2352
2353 @node Non-Fortran Main Program
2354 @section Non-Fortran Main Program
2355
2356 @menu
2357 * _gfortran_set_args:: Save command-line arguments
2358 * _gfortran_set_options:: Set library option flags
2359 * _gfortran_set_convert:: Set endian conversion
2360 * _gfortran_set_record_marker:: Set length of record markers
2361 * _gfortran_set_max_subrecord_length:: Set subrecord length
2362 * _gfortran_set_fpe:: Set when a Floating Point Exception should be raised
2363 @end menu
2364
2365 Even if you are doing mixed-language programming, it is very
2366 likely that you do not need to know or use the information in this
2367 section. Since it is about the internal structure of GNU Fortran,
2368 it may also change in GCC minor releases.
2369
2370 When you compile a @code{PROGRAM} with GNU Fortran, a function
2371 with the name @code{main} (in the symbol table of the object file)
2372 is generated, which initializes the libgfortran library and then
2373 calls the actual program which uses the name @code{MAIN__}, for
2374 historic reasons. If you link GNU Fortran compiled procedures
2375 to, e.g., a C or C++ program or to a Fortran program compiled by
2376 a different compiler, the libgfortran library is not initialized
2377 and thus a few intrinsic procedures do not work properly, e.g.
2378 those for obtaining the command-line arguments.
2379
2380 Therefore, if your @code{PROGRAM} is not compiled with
2381 GNU Fortran and the GNU Fortran compiled procedures require
2382 intrinsics relying on the library initialization, you need to
2383 initialize the library yourself. Using the default options,
2384 gfortran calls @code{_gfortran_set_args} and
2385 @code{_gfortran_set_options}. The initialization of the former
2386 is needed if the called procedures access the command line
2387 (and for backtracing); the latter sets some flags based on the
2388 standard chosen or to enable backtracing. In typical programs,
2389 it is not necessary to call any initialization function.
2390
2391 If your @code{PROGRAM} is compiled with GNU Fortran, you shall
2392 not call any of the following functions. The libgfortran
2393 initialization functions are shown in C syntax but using C
2394 bindings they are also accessible from Fortran.
2395
2396
2397 @node _gfortran_set_args
2398 @subsection @code{_gfortran_set_args} --- Save command-line arguments
2399 @fnindex _gfortran_set_args
2400 @cindex libgfortran initialization, set_args
2401
2402 @table @asis
2403 @item @emph{Description}:
2404 @code{_gfortran_set_args} saves the command-line arguments; this
2405 initialization is required if any of the command-line intrinsics
2406 is called. Additionally, it shall be called if backtracing is
2407 enabled (see @code{_gfortran_set_options}).
2408
2409 @item @emph{Syntax}:
2410 @code{void _gfortran_set_args (int argc, char *argv[])}
2411
2412 @item @emph{Arguments}:
2413 @multitable @columnfractions .15 .70
2414 @item @var{argc} @tab number of command line argument strings
2415 @item @var{argv} @tab the command-line argument strings; argv[0]
2416 is the pathname of the executable itself.
2417 @end multitable
2418
2419 @item @emph{Example}:
2420 @smallexample
2421 int main (int argc, char *argv[])
2422 @{
2423   /* Initialize libgfortran.  */
2424   _gfortran_set_args (argc, argv);
2425   return 0;
2426 @}
2427 @end smallexample
2428 @end table
2429
2430
2431 @node _gfortran_set_options
2432 @subsection @code{_gfortran_set_options} --- Set library option flags
2433 @fnindex _gfortran_set_options
2434 @cindex libgfortran initialization, set_options
2435
2436 @table @asis
2437 @item @emph{Description}:
2438 @code{_gfortran_set_options} sets several flags related to the Fortran
2439 standard to be used, whether backtracing or core dumps should be enabled
2440 and whether range checks should be performed. The syntax allows for
2441 upward compatibility since the number of passed flags is specified; for
2442 non-passed flags, the default value is used. See also
2443 @pxref{Code Gen Options}. Please note that not all flags are actually
2444 used.
2445
2446 @item @emph{Syntax}:
2447 @code{void _gfortran_set_options (int num, int options[])}
2448
2449 @item @emph{Arguments}:
2450 @multitable @columnfractions .15 .70
2451 @item @var{num} @tab number of options passed
2452 @item @var{argv} @tab The list of flag values
2453 @end multitable
2454
2455 @item @emph{option flag list}:
2456 @multitable @columnfractions .15 .70
2457 @item @var{option}[0] @tab Allowed standard; can give run-time errors
2458 if e.g. an input-output edit descriptor is invalid in a given standard.
2459 Possible values are (bitwise or-ed) @code{GFC_STD_F77} (1),
2460 @code{GFC_STD_F95_OBS} (2), @code{GFC_STD_F95_DEL} (4), @code{GFC_STD_F95}
2461 (8), @code{GFC_STD_F2003} (16), @code{GFC_STD_GNU} (32),
2462 @code{GFC_STD_LEGACY} (64), @code{GFC_STD_F2008} (128), and
2463 @code{GFC_STD_F2008_OBS} (256).  Default: @code{GFC_STD_F95_OBS
2464 | GFC_STD_F95_DEL | GFC_STD_F95 | GFC_STD_F2003 | GFC_STD_F2008
2465 | GFC_STD_F2008_OBS | GFC_STD_F77 | GFC_STD_GNU | GFC_STD_LEGACY}.
2466 @item @var{option}[1] @tab Standard-warning flag; prints a warning to
2467 standard error. Default: @code{GFC_STD_F95_DEL | GFC_STD_LEGACY}.
2468 @item @var{option}[2] @tab If non zero, enable pedantic checking.
2469 Default: off.
2470 @item @var{option}[3] @tab If non zero, enable core dumps on run-time
2471 errors. Default: off.
2472 @item @var{option}[4] @tab If non zero, enable backtracing on run-time
2473 errors. Default: off.
2474 Note: Installs a signal handler and requires command-line
2475 initialization using @code{_gfortran_set_args}.
2476 @item @var{option}[5] @tab If non zero, supports signed zeros.
2477 Default: enabled.
2478 @item @var{option}[6] @tab Enables run-time checking. Possible values
2479 are (bitwise or-ed): GFC_RTCHECK_BOUNDS (1), GFC_RTCHECK_ARRAY_TEMPS (2),
2480 GFC_RTCHECK_RECURSION (4), GFC_RTCHECK_DO (16), GFC_RTCHECK_POINTER (32).
2481 Default: disabled.
2482 @item @var{option}[7] @tab If non zero, range checking is enabled.
2483 Default: enabled. See -frange-check (@pxref{Code Gen Options}).
2484 @end multitable
2485
2486 @item @emph{Example}:
2487 @smallexample
2488   /* Use gfortran 4.5 default options.  */
2489   static int options[] = @{68, 255, 0, 0, 0, 1, 0, 1@};
2490   _gfortran_set_options (8, &options);
2491 @end smallexample
2492 @end table
2493
2494
2495 @node _gfortran_set_convert
2496 @subsection @code{_gfortran_set_convert} --- Set endian conversion
2497 @fnindex _gfortran_set_convert
2498 @cindex libgfortran initialization, set_convert
2499
2500 @table @asis
2501 @item @emph{Description}:
2502 @code{_gfortran_set_convert} set the representation of data for
2503 unformatted files.
2504
2505 @item @emph{Syntax}:
2506 @code{void _gfortran_set_convert (int conv)}
2507
2508 @item @emph{Arguments}:
2509 @multitable @columnfractions .15 .70
2510 @item @var{conv} @tab Endian conversion, possible values:
2511 GFC_CONVERT_NATIVE (0, default), GFC_CONVERT_SWAP (1),
2512 GFC_CONVERT_BIG (2), GFC_CONVERT_LITTLE (3).
2513 @end multitable
2514
2515 @item @emph{Example}:
2516 @smallexample
2517 int main (int argc, char *argv[])
2518 @{
2519   /* Initialize libgfortran.  */
2520   _gfortran_set_args (argc, argv);
2521   _gfortran_set_convert (1);
2522   return 0;
2523 @}
2524 @end smallexample
2525 @end table
2526
2527
2528 @node _gfortran_set_record_marker
2529 @subsection @code{_gfortran_set_record_marker} --- Set length of record markers
2530 @fnindex _gfortran_set_record_marker
2531 @cindex libgfortran initialization, set_record_marker
2532
2533 @table @asis
2534 @item @emph{Description}:
2535 @code{_gfortran_set_record_marker} sets the length of record markers
2536 for unformatted files.
2537
2538 @item @emph{Syntax}:
2539 @code{void _gfortran_set_record_marker (int val)}
2540
2541 @item @emph{Arguments}:
2542 @multitable @columnfractions .15 .70
2543 @item @var{val} @tab Length of the record marker; valid values
2544 are 4 and 8. Default is 4.
2545 @end multitable
2546
2547 @item @emph{Example}:
2548 @smallexample
2549 int main (int argc, char *argv[])
2550 @{
2551   /* Initialize libgfortran.  */
2552   _gfortran_set_args (argc, argv);
2553   _gfortran_set_record_marker (8);
2554   return 0;
2555 @}
2556 @end smallexample
2557 @end table
2558
2559
2560 @node _gfortran_set_fpe
2561 @subsection @code{_gfortran_set_fpe} --- Set when a Floating Point Exception should be raised
2562 @fnindex _gfortran_set_fpe
2563 @cindex libgfortran initialization, set_fpe
2564
2565 @table @asis
2566 @item @emph{Description}:
2567 @code{_gfortran_set_fpe} sets the IEEE exceptions for which a
2568 Floating Point Exception (FPE) should be raised. On most systems,
2569 this will result in a SIGFPE signal being sent and the program
2570 being interrupted.
2571
2572 @item @emph{Syntax}:
2573 @code{void _gfortran_set_fpe (int val)}
2574
2575 @item @emph{Arguments}:
2576 @multitable @columnfractions .15 .70
2577 @item @var{option}[0] @tab IEEE exceptions. Possible values are
2578 (bitwise or-ed) zero (0, default) no trapping,
2579 @code{GFC_FPE_INVALID} (1), @code{GFC_FPE_DENORMAL} (2),
2580 @code{GFC_FPE_ZERO} (4), @code{GFC_FPE_OVERFLOW} (8),
2581 @code{GFC_FPE_UNDERFLOW} (16), and @code{GFC_FPE_PRECISION} (32).
2582 @end multitable
2583
2584 @item @emph{Example}:
2585 @smallexample
2586 int main (int argc, char *argv[])
2587 @{
2588   /* Initialize libgfortran.  */
2589   _gfortran_set_args (argc, argv);
2590   /* FPE for invalid operations such as SQRT(-1.0).  */
2591   _gfortran_set_fpe (1);
2592   return 0;
2593 @}
2594 @end smallexample
2595 @end table
2596
2597
2598 @node _gfortran_set_max_subrecord_length
2599 @subsection @code{_gfortran_set_max_subrecord_length} --- Set subrecord length
2600 @fnindex _gfortran_set_max_subrecord_length
2601 @cindex libgfortran initialization, set_max_subrecord_length
2602
2603 @table @asis
2604 @item @emph{Description}:
2605 @code{_gfortran_set_max_subrecord_length} set the maximum length
2606 for a subrecord. This option only makes sense for testing and
2607 debugging of unformatted I/O.
2608
2609 @item @emph{Syntax}:
2610 @code{void _gfortran_set_max_subrecord_length (int val)}
2611
2612 @item @emph{Arguments}:
2613 @multitable @columnfractions .15 .70
2614 @item @var{val} @tab the maximum length for a subrecord;
2615 the maximum permitted value is 2147483639, which is also
2616 the default.
2617 @end multitable
2618
2619 @item @emph{Example}:
2620 @smallexample
2621 int main (int argc, char *argv[])
2622 @{
2623   /* Initialize libgfortran.  */
2624   _gfortran_set_args (argc, argv);
2625   _gfortran_set_max_subrecord_length (8);
2626   return 0;
2627 @}
2628 @end smallexample
2629 @end table
2630
2631
2632
2633 @c Intrinsic Procedures
2634 @c ---------------------------------------------------------------------
2635
2636 @include intrinsic.texi
2637
2638
2639 @tex
2640 \blankpart
2641 @end tex
2642
2643 @c ---------------------------------------------------------------------
2644 @c Contributing
2645 @c ---------------------------------------------------------------------
2646
2647 @node Contributing
2648 @unnumbered Contributing
2649 @cindex Contributing
2650
2651 Free software is only possible if people contribute to efforts
2652 to create it.
2653 We're always in need of more people helping out with ideas
2654 and comments, writing documentation and contributing code.
2655
2656 If you want to contribute to GNU Fortran,
2657 have a look at the long lists of projects you can take on.
2658 Some of these projects are small,
2659 some of them are large;
2660 some are completely orthogonal to the rest of what is
2661 happening on GNU Fortran,
2662 but others are ``mainstream'' projects in need of enthusiastic hackers.
2663 All of these projects are important!
2664 We'll eventually get around to the things here,
2665 but they are also things doable by someone who is willing and able.
2666
2667 @menu
2668 * Contributors::
2669 * Projects::
2670 * Proposed Extensions::
2671 @end menu
2672
2673
2674 @node Contributors
2675 @section Contributors to GNU Fortran
2676 @cindex Contributors
2677 @cindex Credits
2678 @cindex Authors
2679
2680 Most of the parser was hand-crafted by @emph{Andy Vaught}, who is
2681 also the initiator of the whole project.  Thanks Andy!
2682 Most of the interface with GCC was written by @emph{Paul Brook}.
2683
2684 The following individuals have contributed code and/or
2685 ideas and significant help to the GNU Fortran project
2686 (in alphabetical order):
2687
2688 @itemize @minus
2689 @item Janne Blomqvist
2690 @item Steven Bosscher
2691 @item Paul Brook
2692 @item Tobias Burnus
2693 @item Fran@,{c}ois-Xavier Coudert
2694 @item Bud Davis
2695 @item Jerry DeLisle
2696 @item Erik Edelmann
2697 @item Bernhard Fischer
2698 @item Daniel Franke
2699 @item Richard Guenther
2700 @item Richard Henderson
2701 @item Katherine Holcomb
2702 @item Jakub Jelinek
2703 @item Niels Kristian Bech Jensen
2704 @item Steven Johnson
2705 @item Steven G. Kargl
2706 @item Thomas Koenig
2707 @item Asher Langton
2708 @item H. J. Lu
2709 @item Toon Moene
2710 @item Brooks Moses
2711 @item Andrew Pinski
2712 @item Tim Prince
2713 @item Christopher D. Rickett
2714 @item Richard Sandiford
2715 @item Tobias Schl@"uter
2716 @item Roger Sayle
2717 @item Paul Thomas
2718 @item Andy Vaught
2719 @item Feng Wang
2720 @item Janus Weil
2721 @item Daniel Kraft
2722 @end itemize
2723
2724 The following people have contributed bug reports,
2725 smaller or larger patches,
2726 and much needed feedback and encouragement for the
2727 GNU Fortran project: 
2728
2729 @itemize @minus
2730 @item Bill Clodius
2731 @item Dominique d'Humi@`eres
2732 @item Kate Hedstrom
2733 @item Erik Schnetter
2734 @item Joost VandeVondele
2735 @end itemize
2736
2737 Many other individuals have helped debug,
2738 test and improve the GNU Fortran compiler over the past few years,
2739 and we welcome you to do the same!
2740 If you already have done so,
2741 and you would like to see your name listed in the
2742 list above, please contact us.
2743
2744
2745 @node Projects
2746 @section Projects
2747
2748 @table @emph
2749
2750 @item Help build the test suite
2751 Solicit more code for donation to the test suite: the more extensive the
2752 testsuite, the smaller the risk of breaking things in the future! We can
2753 keep code private on request.
2754
2755 @item Bug hunting/squishing
2756 Find bugs and write more test cases! Test cases are especially very
2757 welcome, because it allows us to concentrate on fixing bugs instead of
2758 isolating them. Going through the bugzilla database at
2759 @url{http://gcc.gnu.org/bugzilla/} to reduce testcases posted there and
2760 add more information (for example, for which version does the testcase
2761 work, for which versions does it fail?) is also very helpful.
2762
2763 @end table
2764
2765
2766 @node Proposed Extensions
2767 @section Proposed Extensions
2768
2769 Here's a list of proposed extensions for the GNU Fortran compiler, in no particular
2770 order.  Most of these are necessary to be fully compatible with
2771 existing Fortran compilers, but they are not part of the official
2772 J3 Fortran 95 standard.
2773
2774 @subsection Compiler extensions: 
2775 @itemize @bullet
2776 @item
2777 User-specified alignment rules for structures.
2778
2779 @item
2780 Automatically extend single precision constants to double.
2781
2782 @item
2783 Compile code that conserves memory by dynamically allocating common and
2784 module storage either on stack or heap.
2785
2786 @item
2787 Compile flag to generate code for array conformance checking (suggest -CC).
2788
2789 @item
2790 User control of symbol names (underscores, etc).
2791
2792 @item
2793 Compile setting for maximum size of stack frame size before spilling
2794 parts to static or heap.
2795
2796 @item
2797 Flag to force local variables into static space.
2798
2799 @item
2800 Flag to force local variables onto stack.
2801 @end itemize
2802
2803
2804 @subsection Environment Options
2805 @itemize @bullet
2806 @item
2807 Pluggable library modules for random numbers, linear algebra.
2808 LA should use BLAS calling conventions.
2809
2810 @item
2811 Environment variables controlling actions on arithmetic exceptions like
2812 overflow, underflow, precision loss---Generate NaN, abort, default.
2813 action.
2814
2815 @item
2816 Set precision for fp units that support it (i387).
2817
2818 @item
2819 Variable for setting fp rounding mode.
2820
2821 @item
2822 Variable to fill uninitialized variables with a user-defined bit
2823 pattern.
2824
2825 @item
2826 Environment variable controlling filename that is opened for that unit
2827 number.
2828
2829 @item
2830 Environment variable to clear/trash memory being freed.
2831
2832 @item
2833 Environment variable to control tracing of allocations and frees.
2834
2835 @item
2836 Environment variable to display allocated memory at normal program end.
2837
2838 @item
2839 Environment variable for filename for * IO-unit.
2840
2841 @item
2842 Environment variable for temporary file directory.
2843
2844 @item
2845 Environment variable forcing standard output to be line buffered (unix).
2846
2847 @end itemize
2848
2849
2850 @c ---------------------------------------------------------------------
2851 @c GNU General Public License
2852 @c ---------------------------------------------------------------------
2853
2854 @include gpl_v3.texi
2855
2856
2857
2858 @c ---------------------------------------------------------------------
2859 @c GNU Free Documentation License
2860 @c ---------------------------------------------------------------------
2861
2862 @include fdl.texi
2863
2864
2865
2866 @c ---------------------------------------------------------------------
2867 @c Funding Free Software
2868 @c ---------------------------------------------------------------------
2869
2870 @include funding.texi
2871
2872 @c ---------------------------------------------------------------------
2873 @c Indices
2874 @c ---------------------------------------------------------------------
2875
2876 @node Option Index
2877 @unnumbered Option Index
2878 @command{gfortran}'s command line options are indexed here without any
2879 initial @samp{-} or @samp{--}. Where an option has both positive and
2880 negative forms (such as -foption and -fno-option), relevant entries in
2881 the manual are indexed under the most appropriate form; it may sometimes
2882 be useful to look up both forms.
2883 @printindex op
2884
2885 @node Keyword Index
2886 @unnumbered Keyword Index
2887 @printindex cp
2888
2889 @bye