]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/c-semantics.c
c-common.def (IF_STMT, [...]): Move to cp-tree.def.
[thirdparty/gcc.git] / gcc / c-semantics.c
1 /* This file contains the definitions and documentation for the common
2 tree codes used in the GNU C and C++ compilers (see c-common.def
3 for the standard codes).
4 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 Written by Benjamin Chelf (chelf@codesourcery.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA. */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "function.h"
30 #include "splay-tree.h"
31 #include "varray.h"
32 #include "c-common.h"
33 #include "except.h"
34 /* In order for the format checking to accept the C frontend
35 diagnostic framework extensions, you must define this token before
36 including toplev.h. */
37 #define GCC_DIAG_STYLE __gcc_cdiag__
38 #include "toplev.h"
39 #include "flags.h"
40 #include "ggc.h"
41 #include "rtl.h"
42 #include "output.h"
43 #include "timevar.h"
44 #include "predict.h"
45 #include "tree-inline.h"
46 #include "tree-gimple.h"
47 #include "langhooks.h"
48
49 /* Create an empty statement tree rooted at T. */
50
51 tree
52 push_stmt_list (void)
53 {
54 tree t;
55 t = alloc_stmt_list ();
56 TREE_CHAIN (t) = cur_stmt_list;
57 cur_stmt_list = t;
58 return t;
59 }
60
61 /* Similarly, except that T may have already been pushed/popped, and
62 thus may already contain statement(s). Arrage for new statements
63 to be appended. */
64
65 tree
66 re_push_stmt_list (tree t)
67 {
68 if (t)
69 {
70 if (TREE_CODE (t) != STATEMENT_LIST)
71 {
72 tree u = alloc_stmt_list ();
73 append_to_statement_list_force (t, &u);
74 t = u;
75 }
76 }
77 else
78 t = alloc_stmt_list ();
79 TREE_CHAIN (t) = cur_stmt_list;
80 cur_stmt_list = t;
81 return t;
82 }
83
84 /* Finish the statement tree rooted at T. */
85
86 tree
87 pop_stmt_list (tree t)
88 {
89 tree u = cur_stmt_list, chain;
90
91 /* Pop statement lists until we reach the target level. The extra
92 nestings will be due to outstanding cleanups. */
93 while (1)
94 {
95 chain = TREE_CHAIN (u);
96 TREE_CHAIN (u) = NULL_TREE;
97 if (t == u)
98 break;
99 u = chain;
100 }
101 cur_stmt_list = chain;
102
103 /* If the statement list is completely empty, just return it. This is
104 just as good small as build_empty_stmt, with the advantage that
105 statement lists are merged when they appended to one another. So
106 using the STATEMENT_LIST avoids pathological buildup of EMPTY_STMT_P
107 statements. */
108 if (TREE_SIDE_EFFECTS (t))
109 {
110 tree_stmt_iterator i = tsi_start (t);
111
112 /* If the statement list contained exactly one statement, then
113 extract it immediately. */
114 if (tsi_one_before_end_p (i))
115 {
116 u = tsi_stmt (i);
117 tsi_delink (&i);
118 free_stmt_list (t);
119 t = u;
120 }
121 }
122
123 return t;
124 }
125
126 /* T is a statement. Add it to the statement-tree. */
127
128 tree
129 add_stmt (tree t)
130 {
131 if (EXPR_P (t) || STATEMENT_CODE_P (TREE_CODE (t)))
132 {
133 if (!EXPR_LOCUS (t))
134 annotate_with_locus (t, input_location);
135
136 /* When we expand a statement-tree, we must know whether or not the
137 statements are full-expressions. We record that fact here. */
138 STMT_IS_FULL_EXPR_P (t) = stmts_are_full_exprs_p ();
139 }
140
141 /* Add T to the statement-tree. Non-side-effect statements need to be
142 recorded during statement expressions. */
143 append_to_statement_list_force (t, &cur_stmt_list);
144
145 return t;
146 }
147
148 /* Create a declaration statement for the declaration given by the
149 DECL. */
150
151 void
152 add_decl_stmt (tree decl)
153 {
154 tree decl_stmt;
155
156 /* We need the type to last until instantiation time. */
157 decl_stmt = build_stmt (DECL_STMT, decl);
158 add_stmt (decl_stmt);
159 }
160
161 /* Build a generic statement based on the given type of node and
162 arguments. Similar to `build_nt', except that we set
163 EXPR_LOCUS to be the current source location. */
164 /* ??? This should be obsolete with the lineno_stmt productions
165 in the grammar. */
166
167 tree
168 build_stmt (enum tree_code code, ...)
169 {
170 tree ret;
171 int length, i;
172 va_list p;
173 bool side_effects;
174
175 va_start (p, code);
176
177 ret = make_node (code);
178 TREE_TYPE (ret) = void_type_node;
179 length = TREE_CODE_LENGTH (code);
180 annotate_with_locus (ret, input_location);
181
182 /* Most statements have implicit side effects all on their own,
183 such as control transfer. For those that do, we'll compute
184 the real value of TREE_SIDE_EFFECTS from its arguments. */
185 switch (code)
186 {
187 case EXPR_STMT:
188 side_effects = false;
189 break;
190 default:
191 side_effects = true;
192 break;
193 }
194
195 for (i = 0; i < length; i++)
196 {
197 tree t = va_arg (p, tree);
198 if (t && IS_NON_TYPE_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (t))))
199 side_effects |= TREE_SIDE_EFFECTS (t);
200 TREE_OPERAND (ret, i) = t;
201 }
202
203 TREE_SIDE_EFFECTS (ret) = side_effects;
204
205 va_end (p);
206 return ret;
207 }
208
209 /* Create RTL for the local static variable DECL. */
210
211 void
212 make_rtl_for_local_static (tree decl)
213 {
214 const char *asmspec = NULL;
215
216 /* If we inlined this variable, we could see it's declaration
217 again. */
218 if (TREE_ASM_WRITTEN (decl))
219 return;
220
221 /* If the DECL_ASSEMBLER_NAME is not the same as the DECL_NAME, then
222 either we already created RTL for this DECL (and since it was a
223 local variable, its DECL_ASSEMBLER_NAME got hacked up to prevent
224 clashes with other local statics with the same name by a previous
225 call to make_decl_rtl), or the user explicitly requested a
226 particular assembly name for this variable, using the GNU
227 extension for this purpose:
228
229 int i asm ("j");
230
231 There's no way to know which case we're in, here. But, it turns
232 out we're safe. If there's already RTL, then
233 rest_of_decl_compilation ignores the ASMSPEC parameter, so we
234 may as well not pass it in. If there isn't RTL, then we didn't
235 already create RTL, which means that the modification to
236 DECL_ASSEMBLER_NAME came only via the explicit extension. */
237 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
238 && !DECL_RTL_SET_P (decl))
239 asmspec = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
240
241 rest_of_decl_compilation (decl, asmspec, /*top_level=*/0, /*at_end=*/0);
242 }
243
244 /* Let the back-end know about DECL. */
245
246 void
247 emit_local_var (tree decl)
248 {
249 /* Create RTL for this variable. */
250 if (!DECL_RTL_SET_P (decl))
251 {
252 if (DECL_HARD_REGISTER (decl))
253 /* The user specified an assembler name for this variable.
254 Set that up now. */
255 rest_of_decl_compilation
256 (decl, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
257 /*top_level=*/0, /*at_end=*/0);
258 else
259 expand_decl (decl);
260 }
261 }
262
263 /* Build the node for a return statement and return it. */
264
265 tree
266 build_return_stmt (tree expr)
267 {
268 return (build_stmt (RETURN_STMT, expr));
269 }
270
271 /* Build a break statement node and return it. */
272
273 tree
274 build_break_stmt (void)
275 {
276 return (build_stmt (BREAK_STMT));
277 }
278
279 /* Build a continue statement node and return it. */
280
281 tree
282 build_continue_stmt (void)
283 {
284 return (build_stmt (CONTINUE_STMT));
285 }
286
287 /* Create a CASE_LABEL_EXPR tree node and return it. */
288
289 tree
290 build_case_label (tree low_value, tree high_value, tree label_decl)
291 {
292 return build_stmt (CASE_LABEL_EXPR, low_value, high_value, label_decl);
293 }