]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/doc/gfc-internals/gfcexpr.rst
sphinx: add missing trailing newline
[thirdparty/gcc.git] / gcc / fortran / doc / gfc-internals / gfcexpr.rst
CommitLineData
c63539ff
ML
1..
2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
5
6.. _gfc_expr:
7
8gfc_expr
9********
10
11.. index:: gfc_expr, struct gfc_expr
12
13Expressions and 'values', including constants, variable-, array- and
14component-references as well as complex expressions consisting of operators and
15function calls are internally represented as one or a whole tree of
16``gfc_expr`` objects. The member ``expr_type`` specifies the overall
17type of an expression (for instance, ``EXPR_CONSTANT`` for constants or
18``EXPR_VARIABLE`` for variable references). The members ``ts`` and
19``rank`` as well as ``shape``, which can be ``NULL``, specify
20the type, rank and, if applicable, shape of the whole expression or expression
21tree of which the current structure is the root. ``where`` is the locus of
22this expression in the source code.
23
24Depending on the flavor of the expression being described by the object
25(that is, the value of its ``expr_type`` member), the corresponding structure
26in the ``value`` union will usually contain additional data describing the
27expression's value in a type-specific manner. The ``ref`` member is used to
28build chains of (array-, component- and substring-) references if the expression
29in question contains such references, see below for details.
30
31Constants
32^^^^^^^^^
33
34Scalar constants are represented by ``gfc_expr`` nodes with their
35``expr_type`` set to ``EXPR_CONSTANT``. The constant's value shall
36already be known at compile-time and is stored in the ``logical``,
37``integer``, ``real``, ``complex`` or ``character`` struct inside
38``value``, depending on the constant's type specification.
39
40Operators
41^^^^^^^^^
42
43Operator-expressions are expressions that are the result of the execution of
44some operator on one or two operands. The expressions have an ``expr_type``
45of ``EXPR_OP``. Their ``value.op`` structure contains additional data.
46
47``op1`` and optionally ``op2`` if the operator is binary point to the
48two operands, and ``operator`` or ``uop`` describe the operator that
49should be evaluated on these operands, where ``uop`` describes a user-defined
50operator.
51
52Function Calls
53^^^^^^^^^^^^^^
54
55If the expression is the return value of a function-call, its ``expr_type``
56is set to ``EXPR_FUNCTION``, and ``symtree`` must point to the symtree
57identifying the function to be called. ``value.function.actual`` holds the
58actual arguments given to the function as a linked list of
59``gfc_actual_arglist`` nodes.
60
61The other members of ``value.function`` describe the function being called
62in more detail, containing a link to the intrinsic symbol or user-defined
63function symbol if the call is to an intrinsic or external function,
64respectively. These values are determined during resolution-phase from the
65structure's ``symtree`` member.
66
67A special case of function calls are 'component calls' to type-bound
68procedures; those have the ``expr_type`` ``EXPR_COMPCALL`` with
69``value.compcall`` containing the argument list and the procedure called,
70while ``symtree`` and ``ref`` describe the object on which the procedure
71was called in the same way as a ``EXPR_VARIABLE`` expression would.
72See :ref:`type-bound-procedures`.
73
74Array- and Structure-Constructors
75^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
76
77Array- and structure-constructors (one could probably call them 'array-' and
78'derived-type constants') are ``gfc_expr`` structures with their
79``expr_type`` member set to ``EXPR_ARRAY`` or ``EXPR_STRUCTURE``,
80respectively. For structure constructors, ``symtree`` points to the
81derived-type symbol for the type being constructed.
82
83The values for initializing each array element or structure component are
84stored as linked-list of ``gfc_constructor`` nodes in the
85``value.constructor`` member.
86
87Null
88^^^^
89
90``NULL`` is a special value for pointers; it can be of different base types.
91Such a ``NULL`` value is represented in the internal tree by a
92``gfc_expr`` node with ``expr_type`` ``EXPR_NULL``. If the base type
93of the ``NULL`` expression is known, it is stored in ``ts`` (that's for
94instance the case for default-initializers of ``ALLOCATABLE`` components),
95but this member can also be set to ``BT_UNKNOWN`` if the information is not
96available (for instance, when the expression is a pointer-initializer
97``NULL()``).
98
99Variables and Reference Expressions
100^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
101
102Variable references are ``gfc_expr`` structures with their ``expr_type``
103set to ``EXPR_VARIABLE`` ; their ``symtree`` should point to the variable
104that is referenced.
105
106For this type of expression, it's also possible to chain array-, component-
107or substring-references to the original expression to get something like
108:samp:`struct%component(2:5)`, where ``component`` is either an array or
109a ``CHARACTER`` member of ``struct`` that is of some derived-type. Such a
110chain of references is achieved by a linked list headed by ``ref`` of the
111``gfc_expr`` node. For the example above it would be (:samp:`==|` is the
112last ``NULL`` pointer):
113
114.. code-block:: c++
115
116 EXPR_VARIABLE(struct) ==> REF_COMPONENT(component) ==> REF_ARRAY(2:5) ==|
117
118If ``component`` is a string rather than an array, the last element would be
119a ``REF_SUBSTRING`` reference, of course. If the variable itself or some
120component referenced is an array and the expression should reference the whole
121array rather than being followed by an array-element or -section reference, a
122``REF_ARRAY`` reference must be built as the last element in the chain with
123an array-reference type of ``AR_FULL``. Consider this example code:
124
125.. code-block:: fortran
126
127 TYPE :: mytype
128 INTEGER :: array(42)
129 END TYPE mytype
130
131 TYPE(mytype) :: variable
132 INTEGER :: local_array(5)
133
134 CALL do_something (variable%array, local_array)
135
136The ``gfc_expr`` nodes representing the arguments to the :samp:`do_something`
137call will have a reference-chain like this:
138
139.. code-block:: c++
140
141 EXPR_VARIABLE(variable) ==> REF_COMPONENT(array) ==> REF_ARRAY(FULL) ==|
142 EXPR_VARIABLE(local_array) ==> REF_ARRAY(FULL) ==|
143
144Constant Substring References
145^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
146
147``EXPR_SUBSTRING`` is a special type of expression that encodes a substring
148reference of a constant string, as in the following code snippet:
149
150.. code-block:: c++
151
152 x = "abcde"(1:2)
153
154In this case, ``value.character`` contains the full string's data as if it
155was a string constant, but the ``ref`` member is also set and points to a
3ed1b4ce 156substring reference as described in the subsection above.