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.
6 .. _basic-data-structures:
11 Gfortran creates GENERIC as an intermediate language for the
12 middle-end. Details about GENERIC can be found in the GCC manual.
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
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
27 c = fold_build2_loc (input_location, MULT_EXPR,
28 gfc_array_index_type, a, b);
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``.
34 Accessing individual members in the ``tree`` structures should not
35 be done. Rather, access should be done via macros.
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.
45 * Add an expression to the end of a block using ``gfc_add_expr_to_block``
47 * Add an expression to the beginning of a block using ``void gfc_prepend_expr_to_block``
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
54 Variables are also ``tree`` expressions, they can be created using
55 ``gfc_create_var``. Assigning to a variable can be done with
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
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));