2000-07-03 Donn Terry (donnte@microsoft.com)
[official-gcc.git] / gcc / c-semantics.c
blob4e5e3ffe8dca912a0fe2feb0f35c45ad4856a625
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 Free Software Foundation, Inc. Written by
5 Benjamin Chelf (chelf@codesourcery.com).
7 This file is part of GNU CC.
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
24 #include "config.h"
25 #include "system.h"
26 #include "tree.h"
27 #include "function.h"
28 #include "splay-tree.h"
29 #include "varray.h"
30 #include "c-common.h"
31 #include "except.h"
32 #include "toplev.h"
33 #include "flags.h"
34 #include "ggc.h"
35 #include "rtl.h"
36 #include "output.h"
37 #include "timevar.h"
39 /* Some statements, like for-statements or if-statements, require a
40 condition. This condition can be a declaration. If T is such a
41 declaration it is processed, and an expression appropriate to use
42 as the condition is returned. Otherwise, T itself is returned. */
44 tree
45 expand_cond (t)
46 tree t;
48 if (t && TREE_CODE (t) == TREE_LIST)
50 expand_stmt (TREE_PURPOSE (t));
51 return TREE_VALUE (t);
53 else
54 return t;
57 /* Create RTL for the local static variable DECL. */
59 void
60 make_rtl_for_local_static (decl)
61 tree decl;
63 const char *asmspec = NULL;
65 /* If we inlined this variable, we could see it's declaration
66 again. */
67 if (DECL_RTL (decl))
68 return;
70 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
72 /* The only way this situaton can occur is if the
73 user specified a name for this DECL using the
74 `attribute' syntax. */
75 asmspec = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
76 DECL_ASSEMBLER_NAME (decl) = DECL_NAME (decl);
79 rest_of_decl_compilation (decl, asmspec, /*top_level=*/0, /*at_end=*/0);
82 /* Let the back-end know about DECL. */
84 void
85 emit_local_var (decl)
86 tree decl;
88 /* Create RTL for this variable. */
89 if (!DECL_RTL (decl))
91 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
92 /* The user must have specified an assembler name for this
93 variable. Set that up now. */
94 rest_of_decl_compilation
95 (decl, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
96 /*top_level=*/0, /*at_end=*/0);
97 else
98 expand_decl (decl);
101 /* Actually do the initialization. */
102 if (stmts_are_full_exprs_p ())
103 expand_start_target_temps ();
105 expand_decl_init (decl);
107 if (stmts_are_full_exprs_p ())
108 expand_end_target_temps ();
111 /* Helper for generating the RTL at the beginning of a scope. */
113 void
114 genrtl_do_pushlevel ()
116 emit_line_note (input_filename, lineno);
117 clear_last_expr ();
120 /* Helper for generating the RTL. */
122 void
123 genrtl_clear_out_block ()
125 /* If COND wasn't a declaration, clear out the
126 block we made for it and start a new one here so the
127 optimization in expand_end_loop will work. */
128 if (getdecls () == NULL_TREE)
129 genrtl_do_pushlevel ();
132 /* Generate the RTL for DESTINATION, which is a GOTO_STMT. */
134 void
135 genrtl_goto_stmt (destination)
136 tree destination;
138 if (TREE_CODE (destination) == IDENTIFIER_NODE)
139 abort ();
141 /* We warn about unused labels with -Wunused. That means we have to
142 mark the used labels as used. */
143 if (TREE_CODE (destination) == LABEL_DECL)
144 TREE_USED (destination) = 1;
146 emit_line_note (input_filename, lineno);
148 if (TREE_CODE (destination) == LABEL_DECL)
150 label_rtx (destination);
151 expand_goto (destination);
153 else
154 expand_computed_goto (destination);
157 /* Generate the RTL for EXPR, which is an EXPR_STMT. */
159 void
160 genrtl_expr_stmt (expr)
161 tree expr;
163 if (expr != NULL_TREE)
165 emit_line_note (input_filename, lineno);
167 if (stmts_are_full_exprs_p ())
168 expand_start_target_temps ();
170 lang_expand_expr_stmt (expr);
172 if (stmts_are_full_exprs_p ())
173 expand_end_target_temps ();
177 /* Generate the RTL for T, which is a DECL_STMT. */
179 void
180 genrtl_decl_stmt (t)
181 tree t;
183 tree decl;
184 emit_line_note (input_filename, lineno);
185 decl = DECL_STMT_DECL (t);
186 /* If this is a declaration for an automatic local
187 variable, initialize it. Note that we might also see a
188 declaration for a namespace-scope object (declared with
189 `extern'). We don't have to handle the initialization
190 of those objects here; they can only be declarations,
191 rather than definitions. */
192 if (TREE_CODE (decl) == VAR_DECL
193 && !TREE_STATIC (decl)
194 && !DECL_EXTERNAL (decl))
196 /* Let the back-end know about this variable. */
197 if (!anon_aggr_type_p (TREE_TYPE (decl)))
198 emit_local_var (decl);
199 else
200 expand_anon_union_decl (decl, NULL_TREE,
201 DECL_ANON_UNION_ELEMS (decl));
203 else if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
205 if (DECL_ARTIFICIAL (decl) && ! TREE_USED (decl))
206 /* Do not emit unused decls. This is not just an
207 optimization. We really do not want to emit
208 __PRETTY_FUNCTION__ etc, if they're never used. */
209 DECL_IGNORED_P (decl) = 1;
210 else
211 make_rtl_for_local_static (decl);
215 /* Generate the RTL for T, which is an IF_STMT. */
217 void
218 genrtl_if_stmt (t)
219 tree t;
221 tree cond;
222 genrtl_do_pushlevel ();
223 cond = expand_cond (IF_COND (t));
224 emit_line_note (input_filename, lineno);
225 expand_start_cond (cond, 0);
226 if (THEN_CLAUSE (t))
227 expand_stmt (THEN_CLAUSE (t));
228 if (ELSE_CLAUSE (t))
230 expand_start_else ();
231 expand_stmt (ELSE_CLAUSE (t));
233 expand_end_cond ();
236 /* Generate the RTL for T, which is a WHILE_STMT. */
238 void
239 genrtl_while_stmt (t)
240 tree t;
242 tree cond;
243 emit_nop ();
244 emit_line_note (input_filename, lineno);
245 expand_start_loop (1);
246 genrtl_do_pushlevel ();
248 cond = expand_cond (WHILE_COND (t));
249 emit_line_note (input_filename, lineno);
250 expand_exit_loop_if_false (0, cond);
251 genrtl_clear_out_block ();
253 expand_stmt (WHILE_BODY (t));
255 expand_end_loop ();
258 /* Generate the RTL for T, which is a DO_STMT. */
260 void
261 genrtl_do_stmt (t)
262 tree t;
264 tree cond;
265 emit_nop ();
266 emit_line_note (input_filename, lineno);
267 expand_start_loop_continue_elsewhere (1);
269 expand_stmt (DO_BODY (t));
271 expand_loop_continue_here ();
273 cond = expand_cond (DO_COND (t));
274 emit_line_note (input_filename, lineno);
275 expand_exit_loop_if_false (0, cond);
276 expand_end_loop ();
279 /* Generate the RTL for EXPR, which is a RETURN_STMT. */
281 void
282 genrtl_return_stmt (expr)
283 tree expr;
285 emit_line_note (input_filename, lineno);
286 c_expand_return (expr);
289 /* Generate the RTL for T, which is a FOR_STMT. */
291 void
292 genrtl_for_stmt (t)
293 tree t;
295 tree tmp;
296 tree cond;
297 if (NEW_FOR_SCOPE_P (t))
298 genrtl_do_pushlevel ();
300 expand_stmt (FOR_INIT_STMT (t));
302 emit_nop ();
303 emit_line_note (input_filename, lineno);
304 expand_start_loop_continue_elsewhere (1);
305 genrtl_do_pushlevel ();
306 cond = expand_cond (FOR_COND (t));
307 emit_line_note (input_filename, lineno);
308 if (cond)
309 expand_exit_loop_if_false (0, cond);
310 genrtl_clear_out_block ();
311 tmp = FOR_EXPR (t);
313 expand_stmt (FOR_BODY (t));
315 emit_line_note (input_filename, lineno);
316 expand_loop_continue_here ();
317 if (tmp)
318 genrtl_expr_stmt (tmp);
319 expand_end_loop ();
322 /* Generate the RTL for a BREAK_STMT. */
324 void
325 genrtl_break_stmt ()
327 emit_line_note (input_filename, lineno);
328 if ( ! expand_exit_something ())
329 error ("break statement not within loop or switch");
332 /* Generate the RTL for a CONTINUE_STMT. */
334 void
335 genrtl_continue_stmt ()
337 emit_line_note (input_filename, lineno);
338 if (! expand_continue_loop (0))
339 error ("continue statement not within a loop");
342 /* Generate the RTL for T, which is a SCOPE_STMT. */
344 void
345 genrtl_scope_stmt (t)
346 tree t;
348 if (!SCOPE_NO_CLEANUPS_P (t))
350 if (SCOPE_BEGIN_P (t))
351 expand_start_bindings_and_block (2 * SCOPE_NULLIFIED_P (t),
352 SCOPE_STMT_BLOCK (t));
353 else if (SCOPE_END_P (t))
354 expand_end_bindings (NULL_TREE, !SCOPE_NULLIFIED_P (t), 0);
356 else if (!SCOPE_NULLIFIED_P (t))
358 rtx note = emit_note (NULL,
359 (SCOPE_BEGIN_P (t)
360 ? NOTE_INSN_BLOCK_BEG
361 : NOTE_INSN_BLOCK_END));
362 NOTE_BLOCK (note) = SCOPE_STMT_BLOCK (t);
366 /* Generate the RTL for T, which is a SWITCH_STMT. */
368 void
369 genrtl_switch_stmt (t)
370 tree t;
372 tree cond;
373 genrtl_do_pushlevel ();
375 cond = expand_cond (SWITCH_COND (t));
376 if (cond != error_mark_node)
378 emit_line_note (input_filename, lineno);
379 c_expand_start_case (cond);
381 else
382 /* The code is in error, but we don't want expand_end_case to
383 crash. */
384 c_expand_start_case (boolean_false_node);
386 expand_stmt (SWITCH_BODY (t));
388 expand_end_case (cond);
391 /* Generate the RTL for a CASE_LABEL. */
393 void
394 genrtl_case_label (low_value, high_value)
395 tree low_value;
396 tree high_value;
398 do_case (low_value, high_value);
401 /* Generate the RTL for T, which is a COMPOUND_STMT. */
403 void
404 genrtl_compound_stmt (t)
405 tree t;
407 /* If this is the outermost block of the function, declare the
408 variables __FUNCTION__, __PRETTY_FUNCTION__, and so forth. */
409 if (cfun
410 && !current_function_name_declared ()
411 && !COMPOUND_STMT_NO_SCOPE (t))
413 set_current_function_name_declared (1);
414 declare_function_name ();
417 expand_stmt (COMPOUND_BODY (t));
420 /* Generate the RTL for an ASM_STMT. */
422 void
423 genrtl_asm_stmt (cv_qualifier, string, output_operands,
424 input_operands, clobbers)
425 tree cv_qualifier;
426 tree string;
427 tree output_operands;
428 tree input_operands;
429 tree clobbers;
431 if (TREE_CHAIN (string))
432 string = combine_strings (string);
434 if (cv_qualifier != NULL_TREE
435 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
437 warning ("%s qualifier ignored on asm",
438 IDENTIFIER_POINTER (cv_qualifier));
439 cv_qualifier = NULL_TREE;
442 emit_line_note (input_filename, lineno);
443 if (output_operands != NULL_TREE || input_operands != NULL_TREE
444 || clobbers != NULL_TREE)
445 c_expand_asm_operands (string, output_operands,
446 input_operands,
447 clobbers,
448 cv_qualifier != NULL_TREE,
449 input_filename, lineno);
450 else
451 expand_asm (string);
454 /* Generate the RTL for a DECL_CLEANUP. */
456 void
457 genrtl_decl_cleanup (decl, cleanup)
458 tree decl;
459 tree cleanup;
461 if (!decl || (DECL_SIZE (decl) && TREE_TYPE (decl) != error_mark_node))
462 expand_decl_cleanup (decl, cleanup);
465 /* Generate the RTL for the statement T, its substatements, and any
466 other statements at its nesting level. */
468 tree
469 expand_stmt (t)
470 tree t;
472 tree rval;
473 rval = lang_expand_stmt (t);
474 return rval;