1/* This file contains subroutine used by the C front-end to construct GENERIC.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
3 Written by Benjamin Chelf (chelf@codesourcery.com).
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24#include "c-common.h"
25#include "tree-iterator.h"
26
27/* Create an empty statement tree rooted at T. */
28
29tree
30push_stmt_list (void)
31{
32 tree t;
33 t = alloc_stmt_list ();
34 vec_safe_push (stmt_list_stack, t);
35 return t;
36}
37
38/* Finish the statement tree rooted at T. */
39
40tree
41pop_stmt_list (tree t)
42{
43 tree u = NULL_TREE;
44
45 /* Pop statement lists until we reach the target level. The extra
46 nestings will be due to outstanding cleanups. */
47 while (1)
48 {
49 u = stmt_list_stack->pop ();
50 if (!stmt_list_stack->is_empty ())
51 {
52 tree x = stmt_list_stack->last ();
53 STATEMENT_LIST_HAS_LABEL (x) |= STATEMENT_LIST_HAS_LABEL (u);
54 }
55 if (t == u)
56 break;
57 }
58
59 gcc_assert (u != NULL_TREE);
60
61 /* If the statement list is completely empty, just return it. This is
62 just as good small as build_empty_stmt, with the advantage that
63 statement lists are merged when they appended to one another. So
64 using the STATEMENT_LIST avoids pathological buildup of EMPTY_STMT_P
65 statements. */
66 if (TREE_SIDE_EFFECTS (t))
67 {
68 tree_stmt_iterator i = tsi_start (t);
69
70 /* If the statement list contained exactly one statement, then
71 extract it immediately. */
72 if (tsi_one_before_end_p (i))
73 {
74 u = tsi_stmt (i);
75 tsi_delink (&i);
76 free_stmt_list (t);
77 t = u;
78 }
79 /* If the statement list contained a debug begin stmt and a
80 statement list, move the debug begin stmt into the statement
81 list and return it. */
82 else if (!tsi_end_p (i)
83 && TREE_CODE (tsi_stmt (i)) == DEBUG_BEGIN_STMT)
84 {
85 u = tsi_stmt (i);
86 tsi_next (&i);
87 if (tsi_one_before_end_p (i)
88 && TREE_CODE (tsi_stmt (i)) == STATEMENT_LIST)
89 {
90 tree l = tsi_stmt (i);
91 tsi_prev (&i);
92 tsi_delink (&i);
93 tsi_delink (&i);
94 i = tsi_start (l);
95 free_stmt_list (t);
96 t = l;
97 tsi_link_before (&i, u, TSI_SAME_STMT);
98 }
99 }
100 }
101
102 return t;
103}
104
105/* Build a generic statement based on the given type of node and
106 arguments. Similar to `build_nt', except that we set
107 EXPR_LOCATION to LOC. */
108/* ??? This should be obsolete with the lineno_stmt productions
109 in the grammar. */
110
111tree
112build_stmt (location_t loc, enum tree_code code, ...)
113{
114 tree ret;
115 int length, i;
116 va_list p;
117 bool side_effects;
118
119 /* This function cannot be used to construct variably-sized nodes. */
120 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
121
122 va_start (p, code);
123
124 ret = make_node (code);
125 TREE_TYPE (ret) = void_type_node;
126 length = TREE_CODE_LENGTH (code);
127 SET_EXPR_LOCATION (ret, loc);
128
129 /* TREE_SIDE_EFFECTS will already be set for statements with
130 implicit side effects. Here we make sure it is set for other
131 expressions by checking whether the parameters have side
132 effects. */
133
134 side_effects = false;
135 for (i = 0; i < length; i++)
136 {
137 tree t = va_arg (p, tree);
138 if (t && !TYPE_P (t))
139 side_effects |= TREE_SIDE_EFFECTS (t);
140 TREE_OPERAND (ret, i) = t;
141 }
142
143 TREE_SIDE_EFFECTS (ret) |= side_effects;
144
145 va_end (p);
146 return ret;
147}
148
149/* Build a REALPART_EXPR or IMAGPART_EXPR, according to CODE, from ARG. */
150
151tree
152build_real_imag_expr (location_t location, enum tree_code code, tree arg)
153{
154 tree ret;
155 tree arg_type = TREE_TYPE (arg);
156
157 gcc_assert (code == REALPART_EXPR || code == IMAGPART_EXPR);
158
159 if (TREE_CODE (arg_type) == COMPLEX_TYPE)
160 {
161 ret = build1 (code, TREE_TYPE (TREE_TYPE (arg)), arg);
162 SET_EXPR_LOCATION (ret, location);
163 }
164 else if (INTEGRAL_TYPE_P (arg_type) || SCALAR_FLOAT_TYPE_P (arg_type))
165 {
166 ret = (code == REALPART_EXPR
167 ? arg
168 : omit_one_operand_loc (location, arg_type,
169 integer_zero_node, arg));
170 }
171 else
172 {
173 error_at (location, "wrong type argument to %s",
174 code == REALPART_EXPR ? "__real" : "__imag");
175 ret = error_mark_node;
176 }
177
178 return ret;
179}
180