]> git.ipfire.org Git - thirdparty/gcc.git/blob
2c564dae5da4e04a94c9f262177aaf72ad3009ec
[thirdparty/gcc.git] /
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 .. _basic-data-structures:
7
8 Basic data structures
9 *********************
10
11 Gfortran creates GENERIC as an intermediate language for the
12 middle-end. Details about GENERIC can be found in the GCC manual.
13
14 The basic data structure of GENERIC is a ``tree``. Everything in
15 GENERIC is a ``tree``, including types and statements. Fortunately
16 for the gfortran programmer, ``tree`` variables are
17 garbage-collected, so doing memory management for them is not
18 necessary.
19
20 ``tree`` expressions are built using functions such as, for
21 example, ``fold_build2_loc``. For two tree variables ``a`` and
22 ``b``, both of which have the type ``gfc_arry_index_type``,
23 calculation ``c = a * b`` would be done by
24
25 .. code-block:: c++
26
27 c = fold_build2_loc (input_location, MULT_EXPR,
28 gfc_array_index_type, a, b);
29
30 The types have to agree, otherwise internal compiler errors will occur
31 at a later stage. Expressions can be converted to a different type
32 using ``fold_convert``.
33
34 Accessing individual members in the ``tree`` structures should not
35 be done. Rather, access should be done via macros.
36
37 One basic data structure is the ``stmtblock_t`` struct. This is
38 used for holding a list of statements, expressed as ``tree``
39 expressions. If a block is created using ``gfc_start_block``, it
40 has its own scope for variables; if it is created using
41 ``gfc_init_block``, it does not have its own scope.
42
43 It is possible to
44
45 * Add an expression to the end of a block using ``gfc_add_expr_to_block``
46
47 * Add an expression to the beginning of a block using ``void gfc_prepend_expr_to_block``
48
49 * Make a block into a single ``tree`` using
50 ``gfc_finish_block``. For example, this is needed to put the
51 contents of a block into the ``if`` or ``else`` branch of
52 a ``COND_EXPR``.
53
54 Variables are also ``tree`` expressions, they can be created using
55 ``gfc_create_var``. Assigning to a variable can be done with
56 ``gfc_add_modify``.
57
58 An example: Creating a default integer type variable in the current
59 scope with the prefix 'everything' in the ``stmt_block``
60 ``block`` and assigning the value 42 would be
61
62 .. code-block:: c++
63
64 tree var, *block;
65 /* Initialize block somewhere here. */
66 var = gfc_create_var (integer_type_node, "everything");
67 gfc_add_modify (block, var, build_int_cst (integer_type_node, 42));