xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/fortran/gfc-internals.texi (revision 4c3eb207d36f67d31994830c0a694161fc1ca39b)
1627f7eb2Smrg\input texinfo  @c -*-texinfo-*-
2627f7eb2Smrg@c %**start of header
3627f7eb2Smrg@setfilename gfc-internals.info
4*4c3eb207Smrg@set copyrights-gfortran 2007-2020
5627f7eb2Smrg
6627f7eb2Smrg@include gcc-common.texi
7627f7eb2Smrg
8627f7eb2Smrg@synindex tp cp
9627f7eb2Smrg
10627f7eb2Smrg@settitle GNU Fortran Compiler Internals
11627f7eb2Smrg
12627f7eb2Smrg@c %**end of header
13627f7eb2Smrg
14627f7eb2Smrg@c Use with @@smallbook.
15627f7eb2Smrg
16627f7eb2Smrg@c %** start of document
17627f7eb2Smrg
18627f7eb2Smrg@c Cause even numbered pages to be printed on the left hand side of
19627f7eb2Smrg@c the page and odd numbered pages to be printed on the right hand
20627f7eb2Smrg@c side of the page.  Using this, you can print on both sides of a
21627f7eb2Smrg@c sheet of paper and have the text on the same part of the sheet.
22627f7eb2Smrg
23627f7eb2Smrg@c The text on right hand pages is pushed towards the right hand
24627f7eb2Smrg@c margin and the text on left hand pages is pushed toward the left
25627f7eb2Smrg@c hand margin.
26627f7eb2Smrg@c (To provide the reverse effect, set bindingoffset to -0.75in.)
27627f7eb2Smrg
28627f7eb2Smrg@c @tex
29627f7eb2Smrg@c \global\bindingoffset=0.75in
30627f7eb2Smrg@c \global\normaloffset =0.75in
31627f7eb2Smrg@c @end tex
32627f7eb2Smrg
33627f7eb2Smrg@copying
34627f7eb2SmrgCopyright @copyright{} @value{copyrights-gfortran} Free Software Foundation, Inc.
35627f7eb2Smrg
36627f7eb2SmrgPermission is granted to copy, distribute and/or modify this document
37627f7eb2Smrgunder the terms of the GNU Free Documentation License, Version 1.3 or
38627f7eb2Smrgany later version published by the Free Software Foundation; with the
39627f7eb2SmrgInvariant Sections being ``Funding Free Software'', the Front-Cover
40627f7eb2SmrgTexts being (a) (see below), and with the Back-Cover Texts being (b)
41627f7eb2Smrg(see below).  A copy of the license is included in the section entitled
42627f7eb2Smrg``GNU Free Documentation License''.
43627f7eb2Smrg
44627f7eb2Smrg(a) The FSF's Front-Cover Text is:
45627f7eb2Smrg
46627f7eb2Smrg     A GNU Manual
47627f7eb2Smrg
48627f7eb2Smrg(b) The FSF's Back-Cover Text is:
49627f7eb2Smrg
50627f7eb2Smrg     You have freedom to copy and modify this GNU Manual, like GNU
51627f7eb2Smrg     software.  Copies published by the Free Software Foundation raise
52627f7eb2Smrg     funds for GNU development.
53627f7eb2Smrg@end copying
54627f7eb2Smrg
55627f7eb2Smrg@ifinfo
56627f7eb2Smrg@dircategory Software development
57627f7eb2Smrg@direntry
58627f7eb2Smrg* gfortran: (gfortran).                  The GNU Fortran Compiler.
59627f7eb2Smrg@end direntry
60627f7eb2SmrgThis file documents the internals of the GNU Fortran
61627f7eb2Smrgcompiler, (@command{gfortran}).
62627f7eb2Smrg
63627f7eb2SmrgPublished by the Free Software Foundation
64627f7eb2Smrg51 Franklin Street, Fifth Floor
65627f7eb2SmrgBoston, MA 02110-1301 USA
66627f7eb2Smrg
67627f7eb2Smrg@insertcopying
68627f7eb2Smrg@end ifinfo
69627f7eb2Smrg
70627f7eb2Smrg
71627f7eb2Smrg@setchapternewpage odd
72627f7eb2Smrg@titlepage
73627f7eb2Smrg@title GNU Fortran Internals
74627f7eb2Smrg@versionsubtitle
75627f7eb2Smrg@author The @t{gfortran} team
76627f7eb2Smrg@page
77627f7eb2Smrg@vskip 0pt plus 1filll
78627f7eb2SmrgPublished by the Free Software Foundation@*
79627f7eb2Smrg51 Franklin Street, Fifth Floor@*
80627f7eb2SmrgBoston, MA 02110-1301, USA@*
81627f7eb2Smrg@c Last printed ??ber, 19??.@*
82627f7eb2Smrg@c Printed copies are available for $? each.@*
83627f7eb2Smrg@c ISBN ???
84627f7eb2Smrg@sp 1
85627f7eb2Smrg@insertcopying
86627f7eb2Smrg@end titlepage
87627f7eb2Smrg
88627f7eb2Smrg@summarycontents
89627f7eb2Smrg@contents
90627f7eb2Smrg
91627f7eb2Smrg@page
92627f7eb2Smrg
93627f7eb2Smrg@c ---------------------------------------------------------------------
94627f7eb2Smrg@c TexInfo table of contents.
95627f7eb2Smrg@c ---------------------------------------------------------------------
96627f7eb2Smrg
97627f7eb2Smrg@ifnottex
98627f7eb2Smrg@node Top
99627f7eb2Smrg@top Introduction
100627f7eb2Smrg@cindex Introduction
101627f7eb2Smrg
102627f7eb2SmrgThis manual documents the internals of @command{gfortran},
103627f7eb2Smrgthe GNU Fortran compiler.
104627f7eb2Smrg
105627f7eb2Smrg@ifset DEVELOPMENT
106627f7eb2Smrg@emph{Warning:} This document, and the compiler it describes, are still
107627f7eb2Smrgunder development.  While efforts are made to keep it up-to-date, it might
108627f7eb2Smrgnot accurately reflect the status of the most recent GNU Fortran compiler.
109627f7eb2Smrg@end ifset
110627f7eb2Smrg
111627f7eb2Smrg@comment
112627f7eb2Smrg@comment  When you add a new menu item, please keep the right hand
113627f7eb2Smrg@comment  aligned to the same column.  Do not use tabs.  This provides
114627f7eb2Smrg@comment  better formatting.
115627f7eb2Smrg@comment
116627f7eb2Smrg@menu
117627f7eb2Smrg* Introduction::           About this manual.
118627f7eb2Smrg* User Interface::         Code that Interacts with the User.
119627f7eb2Smrg* Frontend Data Structures::
120627f7eb2Smrg                           Data structures used by the frontend
121627f7eb2Smrg* Object Orientation::     Internals of Fortran 2003 OOP features.
122*4c3eb207Smrg* Translating to GENERIC::
123*4c3eb207Smrg                           Generating the intermediate language for later stages.
124627f7eb2Smrg* LibGFortran::            The LibGFortran Runtime Library.
125627f7eb2Smrg* GNU Free Documentation License::
126627f7eb2Smrg                           How you can copy and share this manual.
127627f7eb2Smrg* Index::                  Index of this documentation.
128627f7eb2Smrg@end menu
129627f7eb2Smrg@end ifnottex
130627f7eb2Smrg
131627f7eb2Smrg@c ---------------------------------------------------------------------
132627f7eb2Smrg@c Introduction
133627f7eb2Smrg@c ---------------------------------------------------------------------
134627f7eb2Smrg
135627f7eb2Smrg@node Introduction
136627f7eb2Smrg@chapter Introduction
137627f7eb2Smrg
138627f7eb2Smrg@c The following duplicates the text on the TexInfo table of contents.
139627f7eb2Smrg@iftex
140627f7eb2SmrgThis manual documents the internals of @command{gfortran}, the GNU Fortran
141627f7eb2Smrgcompiler.
142627f7eb2Smrg
143627f7eb2Smrg@ifset DEVELOPMENT
144627f7eb2Smrg@emph{Warning:} This document, and the compiler it describes, are still
145627f7eb2Smrgunder development.  While efforts are made to keep it up-to-date, it
146627f7eb2Smrgmight not accurately reflect the status of the most recent GNU Fortran
147627f7eb2Smrgcompiler.
148627f7eb2Smrg@end ifset
149627f7eb2Smrg@end iftex
150627f7eb2Smrg
151627f7eb2SmrgAt present, this manual is very much a work in progress, containing
152627f7eb2Smrgmiscellaneous notes about the internals of the compiler.  It is hoped
153627f7eb2Smrgthat at some point in the future it will become a reasonably complete
154627f7eb2Smrgguide; in the interim, GNU Fortran developers are strongly encouraged to
155627f7eb2Smrgcontribute to it as a way of keeping notes while working on the
156627f7eb2Smrgcompiler.
157627f7eb2Smrg
158627f7eb2Smrg
159627f7eb2Smrg@c ---------------------------------------------------------------------
160627f7eb2Smrg@c Code that Interacts with the User
161627f7eb2Smrg@c ---------------------------------------------------------------------
162627f7eb2Smrg
163627f7eb2Smrg@node User Interface
164627f7eb2Smrg@chapter Code that Interacts with the User
165627f7eb2Smrg
166627f7eb2Smrg@menu
167627f7eb2Smrg* Command-Line Options::    Command-Line Options.
168627f7eb2Smrg* Error Handling::          Error Handling.
169627f7eb2Smrg@end menu
170627f7eb2Smrg
171627f7eb2Smrg
172627f7eb2Smrg@c ---------------------------------------------------------------------
173627f7eb2Smrg@c Command-Line Options
174627f7eb2Smrg@c ---------------------------------------------------------------------
175627f7eb2Smrg
176627f7eb2Smrg@node Command-Line Options
177627f7eb2Smrg@section Command-Line Options
178627f7eb2Smrg
179627f7eb2SmrgCommand-line options for @command{gfortran} involve four interrelated
180627f7eb2Smrgpieces within the Fortran compiler code.
181627f7eb2Smrg
182627f7eb2SmrgThe relevant command-line flag is defined in @file{lang.opt}, according
183627f7eb2Smrgto the documentation in @ref{Options,, Options, gccint, GNU Compiler
184627f7eb2SmrgCollection Internals}.  This is then processed by the overall GCC
185627f7eb2Smrgmachinery to create the code that enables @command{gfortran} and
186627f7eb2Smrg@command{gcc} to recognize the option in the command-line arguments and
187627f7eb2Smrgcall the relevant handler function.
188627f7eb2Smrg
189627f7eb2SmrgThis generated code calls the @code{gfc_handle_option} code in
190627f7eb2Smrg@file{options.c} with an enumerator variable indicating which option is
191627f7eb2Smrgto be processed, and the relevant integer or string values associated
192627f7eb2Smrgwith that option flag.  Typically, @code{gfc_handle_option} uses these
193627f7eb2Smrgarguments to set global flags which record the option states.
194627f7eb2Smrg
195627f7eb2SmrgThe global flags that record the option states are stored in the
196627f7eb2Smrg@code{gfc_option_t} struct, which is defined in @file{gfortran.h}.
197627f7eb2SmrgBefore the options are processed, initial values for these flags are set
198627f7eb2Smrgin @code{gfc_init_option} in @file{options.c}; these become the default
199627f7eb2Smrgvalues for the options.
200627f7eb2Smrg
201627f7eb2Smrg
202627f7eb2Smrg
203627f7eb2Smrg@c ---------------------------------------------------------------------
204627f7eb2Smrg@c Error Handling
205627f7eb2Smrg@c ---------------------------------------------------------------------
206627f7eb2Smrg
207627f7eb2Smrg@node Error Handling
208627f7eb2Smrg@section Error Handling
209627f7eb2Smrg
210627f7eb2SmrgThe GNU Fortran compiler's parser operates by testing each piece of
211627f7eb2Smrgsource code against a variety of matchers.  In some cases, if these
212627f7eb2Smrgmatchers do not match the source code, they will store an error message
213627f7eb2Smrgin a buffer.  If the parser later finds a matcher that does correctly
214627f7eb2Smrgmatch the source code, then the buffered error is discarded.  However,
215627f7eb2Smrgif the parser cannot find a match, then the buffered error message is
216627f7eb2Smrgreported to the user.  This enables the compiler to provide more
217627f7eb2Smrgmeaningful error messages even in the many cases where (erroneous)
218627f7eb2SmrgFortran syntax is ambiguous due to things like the absence of reserved
219627f7eb2Smrgkeywords.
220627f7eb2Smrg
221627f7eb2SmrgAs an example of how this works, consider the following line:
222627f7eb2Smrg@smallexample
223627f7eb2SmrgIF = 3
224627f7eb2Smrg@end smallexample
225627f7eb2SmrgHypothetically, this may get passed to the matcher for an @code{IF}
226627f7eb2Smrgstatement.  Since this could plausibly be an erroneous @code{IF}
227627f7eb2Smrgstatement, the matcher will buffer an error message reporting the
228627f7eb2Smrgabsence of an expected @samp{(} following an @code{IF}.  Since no
229627f7eb2Smrgmatchers reported an error-free match, however, the parser will also try
230627f7eb2Smrgmatching this against a variable assignment.  When @code{IF} is a valid
231627f7eb2Smrgvariable, this will be parsed as an assignment statement, and the error
232627f7eb2Smrgdiscarded.  However, when @code{IF} is not a valid variable, this
233627f7eb2Smrgbuffered error message will be reported to the user.
234627f7eb2Smrg
235627f7eb2SmrgThe error handling code is implemented in @file{error.c}.  Errors are
236627f7eb2Smrgnormally entered into the buffer with the @code{gfc_error} function.
237627f7eb2SmrgWarnings go through a similar buffering process, and are entered into
238627f7eb2Smrgthe buffer with @code{gfc_warning}.  There is also a special-purpose
239627f7eb2Smrgfunction, @code{gfc_notify_std}, for things which have an error/warning
240627f7eb2Smrgstatus that depends on the currently-selected language standard.
241627f7eb2Smrg
242627f7eb2SmrgThe @code{gfc_error_check} function checks the buffer for errors,
243627f7eb2Smrgreports the error message to the user if one exists, clears the buffer,
244627f7eb2Smrgand returns a flag to the user indicating whether or not an error
245627f7eb2Smrgexisted.  To check the state of the buffer without changing its state or
246627f7eb2Smrgreporting the errors, the @code{gfc_error_flag_test} function can be
247627f7eb2Smrgused.  The @code{gfc_clear_error} function will clear out any errors in
248627f7eb2Smrgthe buffer, without reporting them.  The @code{gfc_warning_check} and
249627f7eb2Smrg@code{gfc_clear_warning} functions provide equivalent functionality for
250627f7eb2Smrgthe warning buffer.
251627f7eb2Smrg
252627f7eb2SmrgOnly one error and one warning can be in the buffers at a time, and
253627f7eb2Smrgbuffering another will overwrite the existing one.  In cases where one
254627f7eb2Smrgmay wish to work on a smaller piece of source code without disturbing an
255627f7eb2Smrgexisting error state, the @code{gfc_push_error}, @code{gfc_pop_error},
256627f7eb2Smrgand @code{gfc_free_error} mechanism exists to implement a stack for the
257627f7eb2Smrgerror buffer.
258627f7eb2Smrg
259627f7eb2SmrgFor cases where an error or warning should be reported immediately
260627f7eb2Smrgrather than buffered, the @code{gfc_error_now} and
261627f7eb2Smrg@code{gfc_warning_now} functions can be used.  Normally, the compiler
262627f7eb2Smrgwill continue attempting to parse the program after an error has
263627f7eb2Smrgoccurred, but if this is not appropriate, the @code{gfc_fatal_error}
264627f7eb2Smrgfunction should be used instead.  For errors that are always the result
265627f7eb2Smrgof a bug somewhere in the compiler, the @code{gfc_internal_error}
266627f7eb2Smrgfunction should be used.
267627f7eb2Smrg
268627f7eb2SmrgThe syntax for the strings used to produce the error/warning message in
269627f7eb2Smrgthe various error and warning functions is similar to the @code{printf}
270627f7eb2Smrgsyntax, with @samp{%}-escapes to insert variable values.  The details,
271627f7eb2Smrgand the allowable codes, are documented in the @code{error_print}
272627f7eb2Smrgfunction in @file{error.c}.
273627f7eb2Smrg
274627f7eb2Smrg@c ---------------------------------------------------------------------
275627f7eb2Smrg@c Frontend Data Structures
276627f7eb2Smrg@c ---------------------------------------------------------------------
277627f7eb2Smrg
278627f7eb2Smrg@node Frontend Data Structures
279627f7eb2Smrg@chapter Frontend Data Structures
280627f7eb2Smrg@cindex data structures
281627f7eb2Smrg
282627f7eb2SmrgThis chapter should describe the details necessary to understand how
283627f7eb2Smrgthe various @code{gfc_*} data are used and interact.  In general it is
284627f7eb2Smrgadvisable to read the code in @file{dump-parse-tree.c} as its routines
285627f7eb2Smrgshould exhaust all possible valid combinations of content for these
286627f7eb2Smrgstructures.
287627f7eb2Smrg
288627f7eb2Smrg@menu
289627f7eb2Smrg* gfc_code:: Representation of Executable Statements.
290627f7eb2Smrg* gfc_expr:: Representation of Values and Expressions.
291627f7eb2Smrg@end menu
292627f7eb2Smrg
293627f7eb2Smrg
294627f7eb2Smrg@c gfc_code
295627f7eb2Smrg@c --------
296627f7eb2Smrg
297627f7eb2Smrg@node gfc_code
298627f7eb2Smrg@section @code{gfc_code}
299627f7eb2Smrg@cindex statement chaining
300627f7eb2Smrg@tindex @code{gfc_code}
301627f7eb2Smrg@tindex @code{struct gfc_code}
302627f7eb2Smrg
303627f7eb2SmrgThe executable statements in a program unit are represented by a
304627f7eb2Smrgnested chain of @code{gfc_code} structures.  The type of statement is
305627f7eb2Smrgidentified by the @code{op} member of the structure, the different
306627f7eb2Smrgpossible values are enumerated in @code{gfc_exec_op}.  A special
307627f7eb2Smrgmember of this @code{enum} is @code{EXEC_NOP} which is used to
308627f7eb2Smrgrepresent the various @code{END} statements if they carry a label.
309627f7eb2SmrgDepending on the type of statement some of the other fields will be
310627f7eb2Smrgfilled in.  Fields that are generally applicable are the @code{next}
311627f7eb2Smrgand @code{here} fields.  The former points to the next statement in
312627f7eb2Smrgthe current block or is @code{NULL} if the current statement is the
313627f7eb2Smrglast in a block, @code{here} points to the statement label of the
314627f7eb2Smrgcurrent statement.
315627f7eb2Smrg
316627f7eb2SmrgIf the current statement is one of @code{IF}, @code{DO}, @code{SELECT}
317627f7eb2Smrgit starts a block, i.e.@: a nested level in the program.  In order to
318627f7eb2Smrgrepresent this, the @code{block} member is set to point to a
319627f7eb2Smrg@code{gfc_code} structure whose @code{next} member starts the chain of
320627f7eb2Smrgstatements inside the block; this structure's @code{op} member should be set to
321627f7eb2Smrgthe same value as the parent structure's @code{op} member.  The @code{SELECT}
322627f7eb2Smrgand @code{IF} statements may contain various blocks (the chain of @code{ELSE IF}
323627f7eb2Smrgand @code{ELSE} blocks or the various @code{CASE}s, respectively).  These chains
324627f7eb2Smrgare linked-lists formed by the @code{block} members.
325627f7eb2Smrg
326627f7eb2SmrgConsider the following example code:
327627f7eb2Smrg
328627f7eb2Smrg@example
329627f7eb2SmrgIF (foo < 20) THEN
330627f7eb2Smrg  PRINT *, "Too small"
331627f7eb2Smrg  foo = 20
332627f7eb2SmrgELSEIF (foo > 50) THEN
333627f7eb2Smrg  PRINT *, "Too large"
334627f7eb2Smrg  foo = 50
335627f7eb2SmrgELSE
336627f7eb2Smrg  PRINT *, "Good"
337627f7eb2SmrgEND IF
338627f7eb2Smrg@end example
339627f7eb2Smrg
340627f7eb2SmrgThis statement-block will be represented in the internal gfortran tree as
341627f7eb2Smrgfollows, were the horizontal link-chains are those induced by the @code{next}
342627f7eb2Smrgmembers and vertical links down are those of @code{block}. @samp{==|} and
343627f7eb2Smrg@samp{--|} mean @code{NULL} pointers to mark the end of a chain:
344627f7eb2Smrg
345627f7eb2Smrg@example
346627f7eb2Smrg... ==> IF ==> ...
347627f7eb2Smrg        |
348627f7eb2Smrg        +--> IF foo < 20 ==> PRINT *, "Too small" ==> foo = 20 ==|
349627f7eb2Smrg             |
350627f7eb2Smrg             +--> IF foo > 50 ==> PRINT *, "Too large" ==> foo = 50 ==|
351627f7eb2Smrg                  |
352627f7eb2Smrg                  +--> ELSE ==> PRINT *, "Good" ==|
353627f7eb2Smrg                       |
354627f7eb2Smrg                       +--|
355627f7eb2Smrg@end example
356627f7eb2Smrg
357627f7eb2Smrg
358627f7eb2Smrg@subsection IF Blocks
359627f7eb2Smrg
360627f7eb2SmrgConditionals are represented by @code{gfc_code} structures with their
361627f7eb2Smrg@code{op} member set to @code{EXEC_IF}.  This structure's @code{block}
362627f7eb2Smrgmember must point to another @code{gfc_code} node that is the header of the
363627f7eb2Smrgif-block.  This header's @code{op} member must be set to @code{EXEC_IF}, too,
364627f7eb2Smrgits @code{expr} member holds the condition to check for, and its @code{next}
365627f7eb2Smrgshould point to the code-chain of the statements to execute if the condition is
366627f7eb2Smrgtrue.
367627f7eb2Smrg
368627f7eb2SmrgIf in addition an @code{ELSEIF} or @code{ELSE} block is present, the
369627f7eb2Smrg@code{block} member of the if-block-header node points to yet another
370627f7eb2Smrg@code{gfc_code} structure that is the header of the elseif- or else-block.  Its
371627f7eb2Smrgstructure is identical to that of the if-block-header, except that in case of an
372627f7eb2Smrg@code{ELSE} block without a new condition the @code{expr} member should be
373627f7eb2Smrg@code{NULL}.  This block can itself have its @code{block} member point to the
374627f7eb2Smrgnext @code{ELSEIF} or @code{ELSE} block if there's a chain of them.
375627f7eb2Smrg
376627f7eb2Smrg
377627f7eb2Smrg@subsection Loops
378627f7eb2Smrg
379627f7eb2Smrg@code{DO} loops are stored in the tree as @code{gfc_code} nodes with their
380627f7eb2Smrg@code{op} set to @code{EXEC_DO} for a @code{DO} loop with iterator variable and
381627f7eb2Smrgto @code{EXEC_DO_WHILE} for infinite @code{DO}s and @code{DO WHILE} blocks.
382627f7eb2SmrgTheir @code{block} member should point to a @code{gfc_code} structure heading
383627f7eb2Smrgthe code-chain of the loop body; its @code{op} member should be set to
384627f7eb2Smrg@code{EXEC_DO} or @code{EXEC_DO_WHILE}, too, respectively.
385627f7eb2Smrg
386627f7eb2SmrgFor @code{DO WHILE} loops, the loop condition is stored on the top
387627f7eb2Smrg@code{gfc_code} structure's @code{expr} member; @code{DO} forever loops are
388627f7eb2Smrgsimply @code{DO WHILE} loops with a constant @code{.TRUE.} loop condition in
389627f7eb2Smrgthe internal representation.
390627f7eb2Smrg
391627f7eb2SmrgSimilarly, @code{DO} loops with an iterator have instead of the condition their
392627f7eb2Smrg@code{ext.iterator} member set to the correct values for the loop iterator
393627f7eb2Smrgvariable and its range.
394627f7eb2Smrg
395627f7eb2Smrg
396627f7eb2Smrg@subsection @code{SELECT} Statements
397627f7eb2Smrg
398627f7eb2SmrgA @code{SELECT} block is introduced by a @code{gfc_code} structure with an
399627f7eb2Smrg@code{op} member of @code{EXEC_SELECT} and @code{expr} containing the expression
400627f7eb2Smrgto evaluate and test.  Its @code{block} member starts a list of @code{gfc_code}
401627f7eb2Smrgstructures linked together by their @code{block} members that stores the various
402627f7eb2Smrg@code{CASE} parts.
403627f7eb2Smrg
404627f7eb2SmrgEach @code{CASE} node has its @code{op} member set to @code{EXEC_SELECT}, too,
405627f7eb2Smrgits @code{next} member points to the code-chain to be executed in the current
406627f7eb2Smrgcase-block, and @code{extx.case_list} contains the case-values this block
407627f7eb2Smrgcorresponds to.  The @code{block} member links to the next case in the list.
408627f7eb2Smrg
409627f7eb2Smrg
410627f7eb2Smrg@subsection @code{BLOCK} and @code{ASSOCIATE}
411627f7eb2Smrg
412627f7eb2SmrgThe code related to a @code{BLOCK} statement is stored inside an
413627f7eb2Smrg@code{gfc_code} structure (say @var{c})
414627f7eb2Smrgwith @code{c.op} set to @code{EXEC_BLOCK}.  The
415627f7eb2Smrg@code{gfc_namespace} holding the locally defined variables of the
416627f7eb2Smrg@code{BLOCK} is stored in @code{c.ext.block.ns}.  The code inside the
417627f7eb2Smrgconstruct is in @code{c.code}.
418627f7eb2Smrg
419627f7eb2Smrg@code{ASSOCIATE} constructs are based on @code{BLOCK} and thus also have
420627f7eb2Smrgthe internal storage structure described above (including @code{EXEC_BLOCK}).
421627f7eb2SmrgHowever, for them @code{c.ext.block.assoc} is set additionally and points
422627f7eb2Smrgto a linked list of @code{gfc_association_list} structures.  Those
423627f7eb2Smrgstructures basically store a link of associate-names to target expressions.
424627f7eb2SmrgThe associate-names themselves are still also added to the @code{BLOCK}'s
425627f7eb2Smrgnamespace as ordinary symbols, but they have their @code{gfc_symbol}'s
426627f7eb2Smrgmember @code{assoc} set also pointing to the association-list structure.
427627f7eb2SmrgThis way associate-names can be distinguished from ordinary variables
428627f7eb2Smrgand their target expressions identified.
429627f7eb2Smrg
430627f7eb2SmrgFor association to expressions (as opposed to variables), at the very beginning
431627f7eb2Smrgof the @code{BLOCK} construct assignments are automatically generated to
432627f7eb2Smrgset the corresponding variables to their target expressions' values, and
433627f7eb2Smrglater on the compiler simply disallows using such associate-names in contexts
434627f7eb2Smrgthat may change the value.
435627f7eb2Smrg
436627f7eb2Smrg
437627f7eb2Smrg@c gfc_expr
438627f7eb2Smrg@c --------
439627f7eb2Smrg
440627f7eb2Smrg@node gfc_expr
441627f7eb2Smrg@section @code{gfc_expr}
442627f7eb2Smrg@tindex @code{gfc_expr}
443627f7eb2Smrg@tindex @code{struct gfc_expr}
444627f7eb2Smrg
445627f7eb2SmrgExpressions and ``values'', including constants, variable-, array- and
446627f7eb2Smrgcomponent-references as well as complex expressions consisting of operators and
447627f7eb2Smrgfunction calls are internally represented as one or a whole tree of
448627f7eb2Smrg@code{gfc_expr} objects.  The member @code{expr_type} specifies the overall
449627f7eb2Smrgtype of an expression (for instance, @code{EXPR_CONSTANT} for constants or
450627f7eb2Smrg@code{EXPR_VARIABLE} for variable references).  The members @code{ts} and
451627f7eb2Smrg@code{rank} as well as @code{shape}, which can be @code{NULL}, specify
452627f7eb2Smrgthe type, rank and, if applicable, shape of the whole expression or expression
453627f7eb2Smrgtree of which the current structure is the root.  @code{where} is the locus of
454627f7eb2Smrgthis expression in the source code.
455627f7eb2Smrg
456627f7eb2SmrgDepending on the flavor of the expression being described by the object
457627f7eb2Smrg(that is, the value of its @code{expr_type} member), the corresponding structure
458627f7eb2Smrgin the @code{value} union will usually contain additional data describing the
459627f7eb2Smrgexpression's value in a type-specific manner.  The @code{ref} member is used to
460627f7eb2Smrgbuild chains of (array-, component- and substring-) references if the expression
461627f7eb2Smrgin question contains such references, see below for details.
462627f7eb2Smrg
463627f7eb2Smrg
464627f7eb2Smrg@subsection Constants
465627f7eb2Smrg
466627f7eb2SmrgScalar constants are represented by @code{gfc_expr} nodes with their
467627f7eb2Smrg@code{expr_type} set to @code{EXPR_CONSTANT}.  The constant's value shall
468627f7eb2Smrgalready be known at compile-time and is stored in the @code{logical},
469627f7eb2Smrg@code{integer}, @code{real}, @code{complex} or @code{character} struct inside
470627f7eb2Smrg@code{value}, depending on the constant's type specification.
471627f7eb2Smrg
472627f7eb2Smrg
473627f7eb2Smrg@subsection Operators
474627f7eb2Smrg
475627f7eb2SmrgOperator-expressions are expressions that are the result of the execution of
476627f7eb2Smrgsome operator on one or two operands.  The expressions have an @code{expr_type}
477627f7eb2Smrgof @code{EXPR_OP}.  Their @code{value.op} structure contains additional data.
478627f7eb2Smrg
479627f7eb2Smrg@code{op1} and optionally @code{op2} if the operator is binary point to the
480627f7eb2Smrgtwo operands, and @code{operator} or @code{uop} describe the operator that
481627f7eb2Smrgshould be evaluated on these operands, where @code{uop} describes a user-defined
482627f7eb2Smrgoperator.
483627f7eb2Smrg
484627f7eb2Smrg
485627f7eb2Smrg@subsection Function Calls
486627f7eb2Smrg
487627f7eb2SmrgIf the expression is the return value of a function-call, its @code{expr_type}
488627f7eb2Smrgis set to @code{EXPR_FUNCTION}, and @code{symtree} must point to the symtree
489627f7eb2Smrgidentifying the function to be called.  @code{value.function.actual} holds the
490627f7eb2Smrgactual arguments given to the function as a linked list of
491627f7eb2Smrg@code{gfc_actual_arglist} nodes.
492627f7eb2Smrg
493627f7eb2SmrgThe other members of @code{value.function} describe the function being called
494627f7eb2Smrgin more detail, containing a link to the intrinsic symbol or user-defined
495627f7eb2Smrgfunction symbol if the call is to an intrinsic or external function,
496627f7eb2Smrgrespectively.  These values are determined during resolution-phase from the
497627f7eb2Smrgstructure's @code{symtree} member.
498627f7eb2Smrg
499627f7eb2SmrgA special case of function calls are ``component calls'' to type-bound
500627f7eb2Smrgprocedures; those have the @code{expr_type} @code{EXPR_COMPCALL} with
501627f7eb2Smrg@code{value.compcall} containing the argument list and the procedure called,
502627f7eb2Smrgwhile @code{symtree} and @code{ref} describe the object on which the procedure
503627f7eb2Smrgwas called in the same way as a @code{EXPR_VARIABLE} expression would.
504627f7eb2Smrg@xref{Type-bound Procedures}.
505627f7eb2Smrg
506627f7eb2Smrg
507627f7eb2Smrg@subsection Array- and Structure-Constructors
508627f7eb2Smrg
509627f7eb2SmrgArray- and structure-constructors (one could probably call them ``array-'' and
510627f7eb2Smrg``derived-type constants'') are @code{gfc_expr} structures with their
511627f7eb2Smrg@code{expr_type} member set to @code{EXPR_ARRAY} or @code{EXPR_STRUCTURE},
512627f7eb2Smrgrespectively.  For structure constructors, @code{symtree} points to the
513627f7eb2Smrgderived-type symbol for the type being constructed.
514627f7eb2Smrg
515627f7eb2SmrgThe values for initializing each array element or structure component are
516627f7eb2Smrgstored as linked-list of @code{gfc_constructor} nodes in the
517627f7eb2Smrg@code{value.constructor} member.
518627f7eb2Smrg
519627f7eb2Smrg
520627f7eb2Smrg@subsection Null
521627f7eb2Smrg
522627f7eb2Smrg@code{NULL} is a special value for pointers; it can be of different base types.
523627f7eb2SmrgSuch a @code{NULL} value is represented in the internal tree by a
524627f7eb2Smrg@code{gfc_expr} node with @code{expr_type} @code{EXPR_NULL}.  If the base type
525627f7eb2Smrgof the @code{NULL} expression is known, it is stored in @code{ts} (that's for
526627f7eb2Smrginstance the case for default-initializers of @code{ALLOCATABLE} components),
527627f7eb2Smrgbut this member can also be set to @code{BT_UNKNOWN} if the information is not
528627f7eb2Smrgavailable (for instance, when the expression is a pointer-initializer
529627f7eb2Smrg@code{NULL()}).
530627f7eb2Smrg
531627f7eb2Smrg
532627f7eb2Smrg@subsection Variables and Reference Expressions
533627f7eb2Smrg
534627f7eb2SmrgVariable references are @code{gfc_expr} structures with their @code{expr_type}
535627f7eb2Smrgset to @code{EXPR_VARIABLE}; their @code{symtree} should point to the variable
536627f7eb2Smrgthat is referenced.
537627f7eb2Smrg
538627f7eb2SmrgFor this type of expression, it's also possible to chain array-, component-
539627f7eb2Smrgor substring-references to the original expression to get something like
540627f7eb2Smrg@samp{struct%component(2:5)}, where @code{component} is either an array or
541627f7eb2Smrga @code{CHARACTER} member of @code{struct} that is of some derived-type.  Such a
542627f7eb2Smrgchain of references is achieved by a linked list headed by @code{ref} of the
543627f7eb2Smrg@code{gfc_expr} node.  For the example above it would be (@samp{==|} is the
544627f7eb2Smrglast @code{NULL} pointer):
545627f7eb2Smrg
546627f7eb2Smrg@smallexample
547627f7eb2SmrgEXPR_VARIABLE(struct) ==> REF_COMPONENT(component) ==> REF_ARRAY(2:5) ==|
548627f7eb2Smrg@end smallexample
549627f7eb2Smrg
550627f7eb2SmrgIf @code{component} is a string rather than an array, the last element would be
551627f7eb2Smrga @code{REF_SUBSTRING} reference, of course.  If the variable itself or some
552627f7eb2Smrgcomponent referenced is an array and the expression should reference the whole
553627f7eb2Smrgarray rather than being followed by an array-element or -section reference, a
554627f7eb2Smrg@code{REF_ARRAY} reference must be built as the last element in the chain with
555627f7eb2Smrgan array-reference type of @code{AR_FULL}. Consider this example code:
556627f7eb2Smrg
557627f7eb2Smrg@smallexample
558627f7eb2SmrgTYPE :: mytype
559627f7eb2Smrg  INTEGER :: array(42)
560627f7eb2SmrgEND TYPE mytype
561627f7eb2Smrg
562627f7eb2SmrgTYPE(mytype) :: variable
563627f7eb2SmrgINTEGER :: local_array(5)
564627f7eb2Smrg
565627f7eb2SmrgCALL do_something (variable%array, local_array)
566627f7eb2Smrg@end smallexample
567627f7eb2Smrg
568627f7eb2SmrgThe @code{gfc_expr} nodes representing the arguments to the @samp{do_something}
569627f7eb2Smrgcall will have a reference-chain like this:
570627f7eb2Smrg
571627f7eb2Smrg@smallexample
572627f7eb2SmrgEXPR_VARIABLE(variable) ==> REF_COMPONENT(array) ==> REF_ARRAY(FULL) ==|
573627f7eb2SmrgEXPR_VARIABLE(local_array) ==> REF_ARRAY(FULL) ==|
574627f7eb2Smrg@end smallexample
575627f7eb2Smrg
576627f7eb2Smrg
577627f7eb2Smrg@subsection Constant Substring References
578627f7eb2Smrg
579627f7eb2Smrg@code{EXPR_SUBSTRING} is a special type of expression that encodes a substring
580627f7eb2Smrgreference of a constant string, as in the following code snippet:
581627f7eb2Smrg
582627f7eb2Smrg@smallexample
583627f7eb2Smrgx = "abcde"(1:2)
584627f7eb2Smrg@end smallexample
585627f7eb2Smrg
586627f7eb2SmrgIn this case, @code{value.character} contains the full string's data as if it
587627f7eb2Smrgwas a string constant, but the @code{ref} member is also set and points to a
588627f7eb2Smrgsubstring reference as described in the subsection above.
589627f7eb2Smrg
590627f7eb2Smrg
591627f7eb2Smrg@c ---------------------------------------------------------------------
592627f7eb2Smrg@c F2003 OOP
593627f7eb2Smrg@c ---------------------------------------------------------------------
594627f7eb2Smrg
595627f7eb2Smrg@node Object Orientation
596627f7eb2Smrg@chapter Internals of Fortran 2003 OOP Features
597627f7eb2Smrg
598627f7eb2Smrg@menu
599627f7eb2Smrg* Type-bound Procedures:: Type-bound procedures.
600627f7eb2Smrg* Type-bound Operators::  Type-bound operators.
601627f7eb2Smrg@end menu
602627f7eb2Smrg
603627f7eb2Smrg
604627f7eb2Smrg@c Type-bound procedures
605627f7eb2Smrg@c ---------------------
606627f7eb2Smrg
607627f7eb2Smrg@node Type-bound Procedures
608627f7eb2Smrg@section Type-bound Procedures
609627f7eb2Smrg
610627f7eb2SmrgType-bound procedures are stored in the @code{tb_sym_root} of the namespace
611627f7eb2Smrg@code{f2k_derived} associated with the derived-type symbol as @code{gfc_symtree}
612627f7eb2Smrgnodes.  The name and symbol of these symtrees corresponds to the binding-name
613627f7eb2Smrgof the procedure, i.e. the name that is used to call it from the context of an
614627f7eb2Smrgobject of the derived-type.
615627f7eb2Smrg
616627f7eb2SmrgIn addition, this type of symtrees stores in @code{n.tb} a struct of type
617627f7eb2Smrg@code{gfc_typebound_proc} containing the additional data needed:  The
618627f7eb2Smrgbinding attributes (like @code{PASS} and @code{NOPASS}, @code{NON_OVERRIDABLE}
619627f7eb2Smrgor the access-specifier), the binding's target(s) and, if the current binding
620627f7eb2Smrgoverrides or extends an inherited binding of the same name, @code{overridden}
621627f7eb2Smrgpoints to this binding's @code{gfc_typebound_proc} structure.
622627f7eb2Smrg
623627f7eb2Smrg
624627f7eb2Smrg@subsection Specific Bindings
625627f7eb2Smrg@c --------------------------
626627f7eb2Smrg
627627f7eb2SmrgFor specific bindings (declared with @code{PROCEDURE}), if they have a
628627f7eb2Smrgpassed-object argument, the passed-object dummy argument is first saved by its
629627f7eb2Smrgname, and later during resolution phase the corresponding argument is looked for
630627f7eb2Smrgand its position remembered as @code{pass_arg_num} in @code{gfc_typebound_proc}.
631627f7eb2SmrgThe binding's target procedure is pointed-to by @code{u.specific}.
632627f7eb2Smrg
633627f7eb2Smrg@code{DEFERRED} bindings are just like ordinary specific bindings, except
634627f7eb2Smrgthat their @code{deferred} flag is set of course and that @code{u.specific}
635627f7eb2Smrgpoints to their ``interface'' defining symbol (might be an abstract interface)
636627f7eb2Smrginstead of the target procedure.
637627f7eb2Smrg
638627f7eb2SmrgAt the moment, all type-bound procedure calls are statically dispatched and
639627f7eb2Smrgtransformed into ordinary procedure calls at resolution time; their actual
640627f7eb2Smrgargument list is updated to include at the right position the passed-object
641627f7eb2Smrgargument, if applicable, and then a simple procedure call to the binding's
642627f7eb2Smrgtarget procedure is built.  To handle dynamic dispatch in the future, this will
643627f7eb2Smrgbe extended to allow special code generation during the trans-phase to dispatch
644627f7eb2Smrgbased on the object's dynamic type.
645627f7eb2Smrg
646627f7eb2Smrg
647627f7eb2Smrg@subsection Generic Bindings
648627f7eb2Smrg@c -------------------------
649627f7eb2Smrg
650627f7eb2SmrgBindings declared as @code{GENERIC} store the specific bindings they target as
651627f7eb2Smrga linked list using nodes of type @code{gfc_tbp_generic} in @code{u.generic}.
652627f7eb2SmrgFor each specific target, the parser records its symtree and during resolution
653627f7eb2Smrgthis symtree is bound to the corresponding @code{gfc_typebound_proc} structure
654627f7eb2Smrgof the specific target.
655627f7eb2Smrg
656627f7eb2SmrgCalls to generic bindings are handled entirely in the resolution-phase, where
657627f7eb2Smrgfor the actual argument list present the matching specific binding is found
658627f7eb2Smrgand the call's target procedure (@code{value.compcall.tbp}) is re-pointed to
659627f7eb2Smrgthe found specific binding and this call is subsequently handled by the logic
660627f7eb2Smrgfor specific binding calls.
661627f7eb2Smrg
662627f7eb2Smrg
663627f7eb2Smrg@subsection Calls to Type-bound Procedures
664627f7eb2Smrg@c ---------------------------------------
665627f7eb2Smrg
666627f7eb2SmrgCalls to type-bound procedures are stored in the parse-tree as @code{gfc_expr}
667627f7eb2Smrgnodes of type @code{EXPR_COMPCALL}.  Their @code{value.compcall.actual} saves
668627f7eb2Smrgthe actual argument list of the call and @code{value.compcall.tbp} points to the
669627f7eb2Smrg@code{gfc_typebound_proc} structure of the binding to be called.  The object
670627f7eb2Smrgin whose context the procedure was called is saved by combination of
671627f7eb2Smrg@code{symtree} and @code{ref}, as if the expression was of type
672627f7eb2Smrg@code{EXPR_VARIABLE}.
673627f7eb2Smrg
674627f7eb2SmrgFor code like this:
675627f7eb2Smrg@smallexample
676627f7eb2SmrgCALL myobj%procedure (arg1, arg2)
677627f7eb2Smrg@end smallexample
678627f7eb2Smrg@noindent
679627f7eb2Smrgthe @code{CALL} is represented in the parse-tree as a @code{gfc_code} node of
680627f7eb2Smrgtype @code{EXEC_COMPCALL}.  The @code{expr} member of this node holds an
681627f7eb2Smrgexpression of type @code{EXPR_COMPCALL} of the same structure as mentioned above
682627f7eb2Smrgexcept that its target procedure is of course a @code{SUBROUTINE} and not a
683627f7eb2Smrg@code{FUNCTION}.
684627f7eb2Smrg
685627f7eb2SmrgExpressions that are generated internally (as expansion of a type-bound
686627f7eb2Smrgoperator call) may also use additional flags and members.
687627f7eb2Smrg@code{value.compcall.ignore_pass} signals that even though a @code{PASS}
688627f7eb2Smrgattribute may be present the actual argument list should not be updated because
689627f7eb2Smrgit already contains the passed-object.
690627f7eb2Smrg@code{value.compcall.base_object} overrides, if it is set, the base-object
691627f7eb2Smrg(that is normally stored in @code{symtree} and @code{ref} as mentioned above);
692627f7eb2Smrgthis is needed because type-bound operators can be called on a base-object that
693627f7eb2Smrgneed not be of type @code{EXPR_VARIABLE} and thus representable in this way.
694627f7eb2SmrgFinally, if @code{value.compcall.assign} is set, the call was produced in
695627f7eb2Smrgexpansion of a type-bound assignment; this means that proper dependency-checking
696627f7eb2Smrgneeds to be done when relevant.
697627f7eb2Smrg
698627f7eb2Smrg
699627f7eb2Smrg@c Type-bound operators
700627f7eb2Smrg@c --------------------
701627f7eb2Smrg
702627f7eb2Smrg@node Type-bound Operators
703627f7eb2Smrg@section Type-bound Operators
704627f7eb2Smrg
705627f7eb2SmrgType-bound operators are in fact basically just @code{GENERIC} procedure
706627f7eb2Smrgbindings and are represented much in the same way as those (see
707627f7eb2Smrg@ref{Type-bound Procedures}).
708627f7eb2Smrg
709627f7eb2SmrgThey come in two flavours:
710627f7eb2SmrgUser-defined operators (like @code{.MYOPERATOR.})
711627f7eb2Smrgare stored in the @code{f2k_derived} namespace's @code{tb_uop_root}
712627f7eb2Smrgsymtree exactly like ordinary type-bound procedures are stored in
713627f7eb2Smrg@code{tb_sym_root}; their symtrees' names are the operator-names (e.g.
714627f7eb2Smrg@samp{myoperator} in the example).
715627f7eb2SmrgIntrinsic operators on the other hand are stored in the namespace's
716627f7eb2Smrgarray member @code{tb_op} indexed by the intrinsic operator's enum
717627f7eb2Smrgvalue.  Those need not be packed into @code{gfc_symtree} structures and are
718627f7eb2Smrgonly @code{gfc_typebound_proc} instances.
719627f7eb2Smrg
720627f7eb2SmrgWhen an operator call or assignment is found that cannot be handled in
721627f7eb2Smrganother way (i.e. neither matches an intrinsic nor interface operator
722627f7eb2Smrgdefinition) but that contains a derived-type expression, all type-bound
723627f7eb2Smrgoperators defined on that derived-type are checked for a match with
724627f7eb2Smrgthe operator call.  If there's indeed a relevant definition, the
725627f7eb2Smrgoperator call is replaced with an internally generated @code{GENERIC}
726627f7eb2Smrgtype-bound procedure call to the respective definition and that call is
727627f7eb2Smrgfurther processed.
728627f7eb2Smrg
729*4c3eb207Smrg@c ---------------------------------------------------------------------
730*4c3eb207Smrg@c - Translating to GENERIC
731*4c3eb207Smrg@c ---------------------------------------------------------------------
732*4c3eb207Smrg
733*4c3eb207Smrg@node Translating to GENERIC
734*4c3eb207Smrg@chapter Generating the intermediate language for later stages.
735*4c3eb207Smrg
736*4c3eb207SmrgThis chapter deals with the transformation of gfortran's frontend data
737*4c3eb207Smrgstructures to the intermediate language used by the later stages of
738*4c3eb207Smrgthe compiler, the so-called middle end.
739*4c3eb207Smrg
740*4c3eb207SmrgData structures relating to this are found in the source files
741*4c3eb207Smrg@file{trans*.h} and @file{trans-*.c}.
742*4c3eb207Smrg
743*4c3eb207Smrg@menu
744*4c3eb207Smrg* Basic Data Structures::       Basic data structures.
745*4c3eb207Smrg* Converting Expressions::      Converting expressions to tree.
746*4c3eb207Smrg* Translating Statements::      Translating statements.
747*4c3eb207Smrg* Accessing Declarations::      Accessing declarations.
748*4c3eb207Smrg@end menu
749*4c3eb207Smrg
750*4c3eb207Smrg@node Basic Data Structures
751*4c3eb207Smrg@section Basic data structures
752*4c3eb207Smrg
753*4c3eb207SmrgGfortran creates GENERIC as an intermediate language for the
754*4c3eb207Smrgmiddle-end. Details about GENERIC can be found in the GCC manual.
755*4c3eb207Smrg
756*4c3eb207SmrgThe basic data structure of GENERIC is a @code{tree}. Everything in
757*4c3eb207SmrgGENERIC is a @code{tree}, including types and statements.  Fortunately
758*4c3eb207Smrgfor the gfortran programmer, @code{tree} variables are
759*4c3eb207Smrggarbage-collected, so doing memory management for them is not
760*4c3eb207Smrgnecessary.
761*4c3eb207Smrg
762*4c3eb207Smrg@code{tree} expressions are built using functions such as, for
763*4c3eb207Smrgexample, @code{fold_build2_loc}.  For two tree variables @code{a} and
764*4c3eb207Smrg@code{b}, both of which have the type @code{gfc_arry_index_type},
765*4c3eb207Smrgcalculation @code{c = a * b} would be done by
766*4c3eb207Smrg
767*4c3eb207Smrg@smallexample
768*4c3eb207Smrgc = fold_build2_loc (input_location, MULT_EXPR,
769*4c3eb207Smrg                     gfc_array_index_type, a, b);
770*4c3eb207Smrg@end smallexample
771*4c3eb207Smrg
772*4c3eb207SmrgThe types have to agree, otherwise internal compiler errors will occur
773*4c3eb207Smrgat a later stage.  Expressions can be converted to a different type
774*4c3eb207Smrgusing @code{fold_convert}.
775*4c3eb207Smrg
776*4c3eb207SmrgAccessing individual members in the @code{tree} structures should not
777*4c3eb207Smrgbe done. Rather, access should be done via macros.
778*4c3eb207Smrg
779*4c3eb207SmrgOne basic data structure is the @code{stmtblock_t} struct. This is
780*4c3eb207Smrgused for holding a list of statements, expressed as @code{tree}
781*4c3eb207Smrgexpressions.  If a block is created using @code{gfc_start_block}, it
782*4c3eb207Smrghas its own scope for variables; if it is created using
783*4c3eb207Smrg@code{gfc_init_block}, it does not have its own scope.
784*4c3eb207Smrg
785*4c3eb207SmrgIt is possible to
786*4c3eb207Smrg@itemize @bullet
787*4c3eb207Smrg@item Add an expression to the end of a block using
788*4c3eb207Smrg      @code{gfc_add_expr_to_block}
789*4c3eb207Smrg@item Add an expression to the beginning of a block using
790*4c3eb207Smrg      @code{void gfc_prepend_expr_to_block}
791*4c3eb207Smrg@item Make a block into a single @code{tree} using
792*4c3eb207Smrg      @code{gfc_finish_block}.  For example, this is needed to put the
793*4c3eb207Smrg      contents of a block into the @code{if} or @code{else} branch of
794*4c3eb207Smrg      a @code{COND_EXPR}.
795*4c3eb207Smrg@end itemize
796*4c3eb207Smrg
797*4c3eb207SmrgVariables are also @code{tree} expressions, they can be created using
798*4c3eb207Smrg@code{gfc_create_var}. Assigning to a variable can be done with
799*4c3eb207Smrg@code{gfc_add_modify}.
800*4c3eb207Smrg
801*4c3eb207SmrgAn example: Creating a default integer type variable in the current
802*4c3eb207Smrgscope with the prefix ``everything'' in the @code{stmt_block}
803*4c3eb207Smrg@code{block} and assigning the value 42 would be
804*4c3eb207Smrg
805*4c3eb207Smrg@smallexample
806*4c3eb207Smrgtree var, *block;
807*4c3eb207Smrg/* Initialize block somewhere here.  */
808*4c3eb207Smrgvar = gfc_create_var (integer_type_node, "everything");
809*4c3eb207Smrggfc_add_modify (block, var, build_int_cst (integer_type_node, 42));
810*4c3eb207Smrg@end smallexample
811*4c3eb207Smrg
812*4c3eb207Smrg@node Converting Expressions
813*4c3eb207Smrg@section Converting Expressons to tree
814*4c3eb207Smrg
815*4c3eb207SmrgConverting expressions to @code{tree} is done by functions called
816*4c3eb207Smrg@code{gfc_conv_*}.
817*4c3eb207Smrg
818*4c3eb207SmrgThe central data structure for a GENERIC expression is the
819*4c3eb207Smrg@code{gfc_se} structure.  Its @code{expr} member is a @code{tree} that
820*4c3eb207Smrgholds the value of the expression.  A @code{gfc_se} structure is
821*4c3eb207Smrginitialized using @code{gfc_init_se}; it needs to be embedded in an
822*4c3eb207Smrgouter @code{gfc_se}.
823*4c3eb207Smrg
824*4c3eb207SmrgEvaluating Fortran expressions often require things to be done before
825*4c3eb207Smrgand after evaluation of the expression, for example code for the
826*4c3eb207Smrgallocation of a temporary variable and its subsequent deallocation.
827*4c3eb207SmrgTherefore, @code{gfc_se} contains the members @code{pre} and
828*4c3eb207Smrg@code{post}, which point to @code{stmt_block} blocks for code that
829*4c3eb207Smrgneeds to be executed before and after evaluation of the expression.
830*4c3eb207Smrg
831*4c3eb207SmrgWhen using a local @code{gfc_se} to convert some expression, it is
832*4c3eb207Smrgoften necessary to add the generated @code{pre} and @code{post} blocks
833*4c3eb207Smrgto the @code{pre} or @code{post} blocks of the outer @code{gfc_se}.
834*4c3eb207SmrgCode like this (lifted from @file{trans-expr.c}) is fairly common:
835*4c3eb207Smrg
836*4c3eb207Smrg@smallexample
837*4c3eb207Smrggfc_se cont_se;
838*4c3eb207Smrgtree cont_var;
839*4c3eb207Smrg
840*4c3eb207Smrg/* cont_var = is_contiguous (expr); .  */
841*4c3eb207Smrggfc_init_se (&cont_se, parmse);
842*4c3eb207Smrggfc_conv_is_contiguous_expr (&cont_se, expr);
843*4c3eb207Smrggfc_add_block_to_block (&se->pre, &(&cont_se)->pre);
844*4c3eb207Smrggfc_add_modify (&se->pre, cont_var, cont_se.expr);
845*4c3eb207Smrggfc_add_block_to_block (&se->pre, &(&cont_se)->post);
846*4c3eb207Smrg@end smallexample
847*4c3eb207Smrg
848*4c3eb207SmrgConversion functions which need a @code{gfc_se} structure will have a
849*4c3eb207Smrgcorresponding argument.
850*4c3eb207Smrg
851*4c3eb207Smrg@code{gfc_se} also contains pointers to a @code{gfc_ss} and a
852*4c3eb207Smrg@code{gfc_loopinfo} structure.  These are needed by the scalarizer.
853*4c3eb207Smrg
854*4c3eb207Smrg@node Translating Statements
855*4c3eb207Smrg@section Translating statements
856*4c3eb207SmrgTranslating statements to @code{tree} is done by functions called
857*4c3eb207Smrg@code{gfc_trans_*}.  These functions usually get passed a
858*4c3eb207Smrg@code{gfc_code} structure, evaluate any expressions and then
859*4c3eb207Smrgreturn a @code{tree} structure.
860*4c3eb207Smrg
861*4c3eb207Smrg@node Accessing Declarations
862*4c3eb207Smrg@section Accessing declarations
863*4c3eb207Smrg
864*4c3eb207Smrg@code{gfc_symbol}, @code{gfc_charlen} and other front-end structures
865*4c3eb207Smrgcontain a @code{backend_decl} variable, which contains the @code{tree}
866*4c3eb207Smrgused for accessing that entity in the middle-end.
867*4c3eb207Smrg
868*4c3eb207SmrgAccessing declarations is usually done by functions called
869*4c3eb207Smrg@code{gfc_get*}.
870627f7eb2Smrg
871627f7eb2Smrg@c ---------------------------------------------------------------------
872627f7eb2Smrg@c LibGFortran
873627f7eb2Smrg@c ---------------------------------------------------------------------
874627f7eb2Smrg
875627f7eb2Smrg@node LibGFortran
876627f7eb2Smrg@chapter The LibGFortran Runtime Library
877627f7eb2Smrg
878627f7eb2Smrg@menu
879627f7eb2Smrg* Symbol Versioning::    Symbol Versioning.
880627f7eb2Smrg@end menu
881627f7eb2Smrg
882627f7eb2Smrg
883627f7eb2Smrg@c ---------------------------------------------------------------------
884627f7eb2Smrg@c Symbol Versioning
885627f7eb2Smrg@c ---------------------------------------------------------------------
886627f7eb2Smrg
887627f7eb2Smrg@node Symbol Versioning
888627f7eb2Smrg@section Symbol Versioning
889627f7eb2Smrg@comment Based on https://gcc.gnu.org/wiki/SymbolVersioning,
890627f7eb2Smrg@comment as of 2006-11-05, written by Janne Blomqvist.
891627f7eb2Smrg
892627f7eb2SmrgIn general, this capability exists only on a few platforms, thus there
893627f7eb2Smrgis a need for configure magic so that it is used only on those targets
894627f7eb2Smrgwhere it is supported.
895627f7eb2Smrg
896627f7eb2SmrgThe central concept in symbol versioning is the so-called map file,
897627f7eb2Smrgwhich specifies the version node(s) exported symbols are labeled with.
898627f7eb2SmrgAlso, the map file is used to hide local symbols.
899627f7eb2Smrg
900627f7eb2SmrgSome relevant references:
901627f7eb2Smrg@itemize @bullet
902627f7eb2Smrg@item
903627f7eb2Smrg@uref{https://sourceware.org/binutils/docs/ld/VERSION.html,
904627f7eb2SmrgGNU @command{ld} manual}
905627f7eb2Smrg
906627f7eb2Smrg@item
907627f7eb2Smrg@uref{https://www.akkadia.org/drepper/symbol-versioning, ELF Symbol
908627f7eb2SmrgVersioning - Ulrich Depper}
909627f7eb2Smrg
910627f7eb2Smrg@item
911627f7eb2Smrg@uref{https://www.akkadia.org/drepper/dsohowto.pdf, How to Write Shared
912627f7eb2SmrgLibraries - Ulrich Drepper (see Chapter 3)}
913627f7eb2Smrg
914627f7eb2Smrg@end itemize
915627f7eb2Smrg
916627f7eb2SmrgIf one adds a new symbol to a library that should be exported, the new
917627f7eb2Smrgsymbol should be mentioned in the map file and a new version node
918627f7eb2Smrgdefined, e.g., if one adds a new symbols @code{foo} and @code{bar} to
919627f7eb2Smrglibgfortran for the next GCC release, the following should be added to
920627f7eb2Smrgthe map file:
921627f7eb2Smrg@smallexample
922627f7eb2SmrgGFORTRAN_1.1 @{
923627f7eb2Smrg    global:
924627f7eb2Smrg        foo;
925627f7eb2Smrg        bar;
926627f7eb2Smrg@} GFORTRAN_1.0;
927627f7eb2Smrg@end smallexample
928627f7eb2Smrg@noindent
929627f7eb2Smrgwhere @code{GFORTRAN_1.0} is the version node of the current release,
930627f7eb2Smrgand @code{GFORTRAN_1.1} is the version node of the next release where
931627f7eb2Smrgfoo and bar are made available.
932627f7eb2Smrg
933627f7eb2SmrgIf one wants to change an existing interface, it is possible by using
934627f7eb2Smrgsome asm trickery (from the @command{ld} manual referenced above):
935627f7eb2Smrg
936627f7eb2Smrg@smallexample
937627f7eb2Smrg__asm__(".symver original_foo,foo@@");
938627f7eb2Smrg__asm__(".symver old_foo,foo@@VERS_1.1");
939627f7eb2Smrg__asm__(".symver old_foo1,foo@@VERS_1.2");
940627f7eb2Smrg__asm__(".symver new_foo,foo@@VERS_2.0");
941627f7eb2Smrg@end smallexample
942627f7eb2Smrg
943627f7eb2SmrgIn this example, @code{foo@@} represents the symbol @code{foo} bound to
944627f7eb2Smrgthe unspecified base version of the symbol. The source file that
945627f7eb2Smrgcontains this example would define 4 C functions: @code{original_foo},
946627f7eb2Smrg@code{old_foo}, @code{old_foo1}, and @code{new_foo}.
947627f7eb2Smrg
948627f7eb2SmrgIn this case the map file must contain @code{foo} in @code{VERS_1.1}
949627f7eb2Smrgand @code{VERS_1.2} as well as in @code{VERS_2.0}.
950627f7eb2Smrg
951627f7eb2Smrg
952627f7eb2Smrg@c ---------------------------------------------------------------------
953627f7eb2Smrg@c GNU Free Documentation License
954627f7eb2Smrg@c ---------------------------------------------------------------------
955627f7eb2Smrg
956627f7eb2Smrg@include fdl.texi
957627f7eb2Smrg
958627f7eb2Smrg
959627f7eb2Smrg@c ---------------------------------------------------------------------
960627f7eb2Smrg@c Index
961627f7eb2Smrg@c ---------------------------------------------------------------------
962627f7eb2Smrg
963627f7eb2Smrg@node Index
964627f7eb2Smrg@unnumbered Index
965627f7eb2Smrg
966627f7eb2Smrg@printindex cp
967627f7eb2Smrg
968627f7eb2Smrg@bye
969