* varray.h (VARRAY_TOP_GENERIC_PTR): Remove spurious parameter.
[official-gcc.git] / gcc / c-semantics.c
blobeca64babe8d18b5f1fad708028d15b021877b762
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 Free Software Foundation, Inc.
5 Written by 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 "expr.h"
37 #include "output.h"
38 #include "timevar.h"
40 /* If non-NULL, the address of a language-specific function for
41 expanding statements. */
42 void (*lang_expand_stmt) PARAMS ((tree));
44 /* If non-NULL, the address of a language-specific function for
45 expanding a DECL_STMT. After the language-independent cases are
46 handled, this function will be called. If this function is not
47 defined, it is assumed that declarations other than those for
48 variables and labels do not require any RTL generation. */
49 void (*lang_expand_decl_stmt) PARAMS ((tree));
51 /* Create an empty statement tree rooted at T. */
53 void
54 begin_stmt_tree (t)
55 tree *t;
57 /* We create a trivial EXPR_STMT so that last_tree is never NULL in
58 what follows. We remove the extraneous statement in
59 finish_stmt_tree. */
60 *t = build_nt (EXPR_STMT, void_zero_node);
61 last_tree = *t;
62 last_expr_type = NULL_TREE;
65 /* T is a statement. Add it to the statement-tree. */
67 tree
68 add_stmt (t)
69 tree t;
71 /* Add T to the statement-tree. */
72 TREE_CHAIN (last_tree) = t;
73 last_tree = t;
75 /* When we expand a statement-tree, we must know whether or not the
76 statements are full-expressions. We record that fact here. */
77 STMT_IS_FULL_EXPR_P (last_tree) = stmts_are_full_exprs_p ();
79 /* Keep track of the number of statements in this function. */
80 if (current_function_decl)
81 ++DECL_NUM_STMTS (current_function_decl);
83 return t;
86 /* Create a declaration statement for the declaration given by the
87 DECL. */
89 void
90 add_decl_stmt (decl)
91 tree decl;
93 tree decl_stmt;
95 /* We need the type to last until instantiation time. */
96 decl_stmt = build_stmt (DECL_STMT, decl);
97 add_stmt (decl_stmt);
100 /* Add a scope-statement to the statement-tree. BEGIN_P indicates
101 whether this statements opens or closes a scope. PARTIAL_P is true
102 for a partial scope, i.e, the scope that begins after a label when
103 an object that needs a cleanup is created. If BEGIN_P is nonzero,
104 returns a new TREE_LIST representing the top of the SCOPE_STMT
105 stack. The TREE_PURPOSE is the new SCOPE_STMT. If BEGIN_P is
106 zero, returns a TREE_LIST whose TREE_VALUE is the new SCOPE_STMT,
107 and whose TREE_PURPOSE is the matching SCOPE_STMT with
108 SCOPE_BEGIN_P set. */
110 tree
111 add_scope_stmt (begin_p, partial_p)
112 int begin_p;
113 int partial_p;
115 tree *stack_ptr = current_scope_stmt_stack ();
116 tree ss;
117 tree top = *stack_ptr;
119 /* Build the statement. */
120 ss = build_stmt (SCOPE_STMT, NULL_TREE);
121 SCOPE_BEGIN_P (ss) = begin_p;
122 SCOPE_PARTIAL_P (ss) = partial_p;
124 /* Keep the scope stack up to date. */
125 if (begin_p)
127 top = tree_cons (ss, NULL_TREE, top);
128 *stack_ptr = top;
130 else
132 TREE_VALUE (top) = ss;
133 *stack_ptr = TREE_CHAIN (top);
136 /* Add the new statement to the statement-tree. */
137 add_stmt (ss);
139 return top;
142 /* Finish the statement tree rooted at T. */
144 void
145 finish_stmt_tree (t)
146 tree *t;
148 tree stmt;
150 /* Remove the fake extra statement added in begin_stmt_tree. */
151 stmt = TREE_CHAIN (*t);
152 *t = stmt;
153 last_tree = NULL_TREE;
155 if (cfun && stmt)
157 /* The line-number recorded in the outermost statement in a function
158 is the line number of the end of the function. */
159 STMT_LINENO (stmt) = lineno;
160 STMT_LINENO_FOR_FN_P (stmt) = 1;
164 /* Build a generic statement based on the given type of node and
165 arguments. Similar to `build_nt', except that we set
166 STMT_LINENO to be the current line number. */
167 /* ??? This should be obsolete with the lineno_stmt productions
168 in the grammar. */
170 tree
171 build_stmt VPARAMS ((enum tree_code code, ...))
173 #ifndef ANSI_PROTOTYPES
174 enum tree_code code;
175 #endif
176 va_list p;
177 register tree t;
178 register int length;
179 register int i;
181 VA_START (p, code);
183 #ifndef ANSI_PROTOTYPES
184 code = va_arg (p, enum tree_code);
185 #endif
187 t = make_node (code);
188 length = TREE_CODE_LENGTH (code);
189 STMT_LINENO (t) = lineno;
191 for (i = 0; i < length; i++)
192 TREE_OPERAND (t, i) = va_arg (p, tree);
194 va_end (p);
195 return t;
198 /* Some statements, like for-statements or if-statements, require a
199 condition. This condition can be a declaration. If T is such a
200 declaration it is processed, and an expression appropriate to use
201 as the condition is returned. Otherwise, T itself is returned. */
203 tree
204 expand_cond (t)
205 tree t;
207 if (t && TREE_CODE (t) == TREE_LIST)
209 expand_stmt (TREE_PURPOSE (t));
210 return TREE_VALUE (t);
212 else
213 return t;
216 /* Create RTL for the local static variable DECL. */
218 void
219 make_rtl_for_local_static (decl)
220 tree decl;
222 const char *asmspec = NULL;
224 /* If we inlined this variable, we could see it's declaration
225 again. */
226 if (TREE_ASM_WRITTEN (decl))
227 return;
229 /* If the DECL_ASSEMBLER_NAME is not the same as the DECL_NAME, then
230 either we already created RTL for this DECL (and since it was a
231 local variable, its DECL_ASSEMBLER_NAME got hacked up to prevent
232 clashes with other local statics with the same name by a previous
233 call to make_decl_rtl), or the user explicitly requested a
234 particular assembly name for this variable, using the GNU
235 extension for this purpose:
237 int i asm ("j");
239 There's no way to know which case we're in, here. But, it turns
240 out we're safe. If there's already RTL, then
241 rest_of_decl_compilation ignores the ASMSPEC parameter, so we
242 may as well not pass it in. If there isn't RTL, then we didn't
243 already create RTL, which means that the modification to
244 DECL_ASSEMBLER_NAME came only via the explicit extension. */
245 if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
246 && !DECL_RTL (decl))
247 asmspec = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
249 rest_of_decl_compilation (decl, asmspec, /*top_level=*/0, /*at_end=*/0);
252 /* Let the back-end know about DECL. */
254 void
255 emit_local_var (decl)
256 tree decl;
258 /* Create RTL for this variable. */
259 if (!DECL_RTL_SET_P (decl))
261 if (DECL_C_HARD_REGISTER (decl))
262 /* The user specified an assembler name for this variable.
263 Set that up now. */
264 rest_of_decl_compilation
265 (decl, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
266 /*top_level=*/0, /*at_end=*/0);
267 else
268 expand_decl (decl);
271 /* Actually do the initialization. */
272 if (stmts_are_full_exprs_p ())
273 expand_start_target_temps ();
275 expand_decl_init (decl);
277 if (stmts_are_full_exprs_p ())
278 expand_end_target_temps ();
281 /* Helper for generating the RTL at the beginning of a scope. */
283 void
284 genrtl_do_pushlevel ()
286 emit_line_note (input_filename, lineno);
287 clear_last_expr ();
290 /* Generate the RTL for DESTINATION, which is a GOTO_STMT. */
292 void
293 genrtl_goto_stmt (destination)
294 tree destination;
296 if (TREE_CODE (destination) == IDENTIFIER_NODE)
297 abort ();
299 /* We warn about unused labels with -Wunused. That means we have to
300 mark the used labels as used. */
301 if (TREE_CODE (destination) == LABEL_DECL)
302 TREE_USED (destination) = 1;
304 emit_line_note (input_filename, lineno);
306 if (TREE_CODE (destination) == LABEL_DECL)
308 label_rtx (destination);
309 expand_goto (destination);
311 else
312 expand_computed_goto (destination);
315 /* Generate the RTL for EXPR, which is an EXPR_STMT. */
317 void
318 genrtl_expr_stmt (expr)
319 tree expr;
321 if (expr != NULL_TREE)
323 emit_line_note (input_filename, lineno);
325 if (stmts_are_full_exprs_p ())
326 expand_start_target_temps ();
328 if (expr != error_mark_node)
329 expand_expr_stmt (expr);
331 if (stmts_are_full_exprs_p ())
332 expand_end_target_temps ();
336 /* Generate the RTL for T, which is a DECL_STMT. */
338 void
339 genrtl_decl_stmt (t)
340 tree t;
342 tree decl;
343 emit_line_note (input_filename, lineno);
344 decl = DECL_STMT_DECL (t);
345 /* If this is a declaration for an automatic local
346 variable, initialize it. Note that we might also see a
347 declaration for a namespace-scope object (declared with
348 `extern'). We don't have to handle the initialization
349 of those objects here; they can only be declarations,
350 rather than definitions. */
351 if (TREE_CODE (decl) == VAR_DECL
352 && !TREE_STATIC (decl)
353 && !DECL_EXTERNAL (decl))
355 /* Let the back-end know about this variable. */
356 if (!anon_aggr_type_p (TREE_TYPE (decl)))
357 emit_local_var (decl);
358 else
359 expand_anon_union_decl (decl, NULL_TREE,
360 DECL_ANON_UNION_ELEMS (decl));
362 else if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
363 make_rtl_for_local_static (decl);
364 else if (TREE_CODE (decl) == LABEL_DECL
365 && C_DECLARED_LABEL_FLAG (decl))
366 declare_nonlocal_label (decl);
367 else if (lang_expand_decl_stmt)
368 (*lang_expand_decl_stmt) (t);
371 /* Generate the RTL for T, which is an IF_STMT. */
373 void
374 genrtl_if_stmt (t)
375 tree t;
377 tree cond;
378 genrtl_do_pushlevel ();
379 cond = expand_cond (IF_COND (t));
380 emit_line_note (input_filename, lineno);
381 expand_start_cond (cond, 0);
382 if (THEN_CLAUSE (t))
383 expand_stmt (THEN_CLAUSE (t));
384 if (ELSE_CLAUSE (t))
386 expand_start_else ();
387 expand_stmt (ELSE_CLAUSE (t));
389 expand_end_cond ();
392 /* Generate the RTL for T, which is a WHILE_STMT. */
394 void
395 genrtl_while_stmt (t)
396 tree t;
398 tree cond;
399 emit_nop ();
400 emit_line_note (input_filename, lineno);
401 expand_start_loop (1);
402 genrtl_do_pushlevel ();
404 cond = expand_cond (WHILE_COND (t));
405 emit_line_note (input_filename, lineno);
406 expand_exit_loop_if_false (0, cond);
407 genrtl_do_pushlevel ();
409 expand_stmt (WHILE_BODY (t));
411 expand_end_loop ();
414 /* Generate the RTL for T, which is a DO_STMT. */
416 void
417 genrtl_do_stmt (t)
418 tree t;
420 tree cond = DO_COND (t);
422 /* Recognize the common special-case of do { ... } while (0) and do
423 not emit the loop widgetry in this case. In particular this
424 avoids cluttering the rtl with dummy loop notes, which can affect
425 alignment of adjacent labels. */
426 if (integer_zerop (cond))
428 expand_start_null_loop ();
429 expand_stmt (DO_BODY (t));
430 expand_end_null_loop ();
432 else
434 emit_nop ();
435 emit_line_note (input_filename, lineno);
436 expand_start_loop_continue_elsewhere (1);
438 expand_stmt (DO_BODY (t));
440 expand_loop_continue_here ();
441 cond = expand_cond (cond);
442 emit_line_note (input_filename, lineno);
443 expand_exit_loop_if_false (0, cond);
444 expand_end_loop ();
448 /* Build the node for a return statement and return it. */
450 tree
451 build_return_stmt (expr)
452 tree expr;
454 return (build_stmt (RETURN_STMT, expr));
457 /* Generate the RTL for STMT, which is a RETURN_STMT. */
459 void
460 genrtl_return_stmt (stmt)
461 tree stmt;
463 tree expr;
465 /* If RETURN_NULLIFIED_P is set, the frontend has arranged to set up
466 the return value separately, so just return the return value
467 itself. This is used for the C++ named return value optimization. */
468 if (RETURN_NULLIFIED_P (stmt))
469 expr = DECL_RESULT (current_function_decl);
470 else
471 expr = RETURN_EXPR (stmt);
473 emit_line_note (input_filename, lineno);
474 if (!expr)
475 expand_null_return ();
476 else
478 expand_start_target_temps ();
479 expand_return (expr);
480 expand_end_target_temps ();
484 /* Generate the RTL for T, which is a FOR_STMT. */
486 void
487 genrtl_for_stmt (t)
488 tree t;
490 tree cond;
491 const char *saved_filename;
492 int saved_lineno;
494 if (NEW_FOR_SCOPE_P (t))
495 genrtl_do_pushlevel ();
497 expand_stmt (FOR_INIT_STMT (t));
499 /* Expand the initialization. */
500 emit_nop ();
501 emit_line_note (input_filename, lineno);
502 expand_start_loop_continue_elsewhere (1);
503 genrtl_do_pushlevel ();
504 cond = expand_cond (FOR_COND (t));
506 /* Save the filename and line number so that we expand the FOR_EXPR
507 we can reset them back to the saved values. */
508 saved_filename = input_filename;
509 saved_lineno = lineno;
511 /* Expand the condition. */
512 emit_line_note (input_filename, lineno);
513 if (cond)
514 expand_exit_loop_if_false (0, cond);
516 /* Expand the body. */
517 genrtl_do_pushlevel ();
518 expand_stmt (FOR_BODY (t));
520 /* Expand the increment expression. */
521 input_filename = saved_filename;
522 lineno = saved_lineno;
523 emit_line_note (input_filename, lineno);
524 expand_loop_continue_here ();
525 if (FOR_EXPR (t))
526 genrtl_expr_stmt (FOR_EXPR (t));
527 expand_end_loop ();
530 /* Build a break statement node and return it. */
532 tree
533 build_break_stmt ()
535 return (build_stmt (BREAK_STMT));
538 /* Generate the RTL for a BREAK_STMT. */
540 void
541 genrtl_break_stmt ()
543 emit_line_note (input_filename, lineno);
544 if ( ! expand_exit_something ())
545 error ("break statement not within loop or switch");
548 /* Build a continue statement node and return it. */
550 tree
551 build_continue_stmt ()
553 return (build_stmt (CONTINUE_STMT));
556 /* Generate the RTL for a CONTINUE_STMT. */
558 void
559 genrtl_continue_stmt ()
561 emit_line_note (input_filename, lineno);
562 if (! expand_continue_loop (0))
563 error ("continue statement not within a loop");
566 /* Generate the RTL for T, which is a SCOPE_STMT. */
568 void
569 genrtl_scope_stmt (t)
570 tree t;
572 tree block = SCOPE_STMT_BLOCK (t);
574 if (!SCOPE_NO_CLEANUPS_P (t))
576 if (SCOPE_BEGIN_P (t))
577 expand_start_bindings_and_block (2 * SCOPE_NULLIFIED_P (t), block);
578 else if (SCOPE_END_P (t))
579 expand_end_bindings (NULL_TREE, !SCOPE_NULLIFIED_P (t), 0);
581 else if (!SCOPE_NULLIFIED_P (t))
583 rtx note = emit_note (NULL,
584 (SCOPE_BEGIN_P (t)
585 ? NOTE_INSN_BLOCK_BEG
586 : NOTE_INSN_BLOCK_END));
587 NOTE_BLOCK (note) = block;
590 /* If we're at the end of a scope that contains inlined nested
591 functions, we have to decide whether or not to write them out. */
592 if (block && SCOPE_END_P (t))
594 tree fn;
596 for (fn = BLOCK_VARS (block); fn; fn = TREE_CHAIN (fn))
598 if (TREE_CODE (fn) == FUNCTION_DECL
599 && DECL_CONTEXT (fn) == current_function_decl
600 && !TREE_ASM_WRITTEN (fn)
601 && TREE_ADDRESSABLE (fn))
603 push_function_context ();
604 output_inline_function (fn);
605 pop_function_context ();
611 /* Generate the RTL for T, which is a SWITCH_STMT. */
613 void
614 genrtl_switch_stmt (t)
615 tree t;
617 tree cond;
618 genrtl_do_pushlevel ();
620 cond = expand_cond (SWITCH_COND (t));
621 if (cond == error_mark_node)
622 /* The code is in error, but we don't want expand_end_case to
623 crash. */
624 cond = boolean_false_node;
626 emit_line_note (input_filename, lineno);
627 expand_start_case (1, cond, TREE_TYPE (cond), "switch statement");
628 expand_stmt (SWITCH_BODY (t));
629 expand_end_case (cond);
632 /* Create a CASE_LABEL tree node and return it. */
634 tree
635 build_case_label (low_value, high_value, label_decl)
636 tree low_value;
637 tree high_value;
638 tree label_decl;
640 return build_stmt (CASE_LABEL, low_value, high_value, label_decl);
644 /* Generate the RTL for a CASE_LABEL. */
646 void
647 genrtl_case_label (case_label)
648 tree case_label;
650 tree duplicate;
651 tree cleanup;
653 cleanup = last_cleanup_this_contour ();
654 if (cleanup)
656 static int explained = 0;
657 warning_with_decl (TREE_PURPOSE (cleanup),
658 "destructor needed for `%#D'");
659 warning ("where case label appears here");
660 if (!explained)
662 warning ("(enclose actions of previous case statements requiring destructors in their own scope.)");
663 explained = 1;
667 add_case_node (CASE_LOW (case_label), CASE_HIGH (case_label),
668 CASE_LABEL_DECL (case_label), &duplicate);
671 /* Generate the RTL for T, which is a COMPOUND_STMT. */
673 void
674 genrtl_compound_stmt (t)
675 tree t;
677 #ifdef ENABLE_CHECKING
678 struct nesting *n = current_nesting_level ();
679 #endif
681 expand_stmt (COMPOUND_BODY (t));
683 #ifdef ENABLE_CHECKING
684 /* Make sure that we've pushed and popped the same number of levels. */
685 if (n != current_nesting_level ())
686 abort ();
687 #endif
690 /* Generate the RTL for an ASM_STMT. */
692 void
693 genrtl_asm_stmt (cv_qualifier, string, output_operands,
694 input_operands, clobbers, asm_input_p)
695 tree cv_qualifier;
696 tree string;
697 tree output_operands;
698 tree input_operands;
699 tree clobbers;
700 int asm_input_p;
702 if (cv_qualifier != NULL_TREE
703 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
705 warning ("%s qualifier ignored on asm",
706 IDENTIFIER_POINTER (cv_qualifier));
707 cv_qualifier = NULL_TREE;
710 emit_line_note (input_filename, lineno);
711 if (asm_input_p)
712 expand_asm (string);
713 else
714 c_expand_asm_operands (string, output_operands, input_operands,
715 clobbers, cv_qualifier != NULL_TREE,
716 input_filename, lineno);
719 /* Generate the RTL for a DECL_CLEANUP. */
721 void
722 genrtl_decl_cleanup (decl, cleanup)
723 tree decl;
724 tree cleanup;
726 if (!decl || (DECL_SIZE (decl) && TREE_TYPE (decl) != error_mark_node))
727 expand_decl_cleanup (decl, cleanup);
730 /* We're about to expand T, a statement. Set up appropriate context
731 for the substitution. */
733 void
734 prep_stmt (t)
735 tree t;
737 if (!STMT_LINENO_FOR_FN_P (t))
738 lineno = STMT_LINENO (t);
739 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
742 /* Generate the RTL for the statement T, its substatements, and any
743 other statements at its nesting level. */
745 void
746 expand_stmt (t)
747 tree t;
749 while (t && t != error_mark_node)
751 int saved_stmts_are_full_exprs_p;
753 /* Set up context appropriately for handling this statement. */
754 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
755 prep_stmt (t);
757 switch (TREE_CODE (t))
759 case RETURN_STMT:
760 genrtl_return_stmt (t);
761 break;
763 case EXPR_STMT:
764 genrtl_expr_stmt (EXPR_STMT_EXPR (t));
765 break;
767 case DECL_STMT:
768 genrtl_decl_stmt (t);
769 break;
771 case FOR_STMT:
772 genrtl_for_stmt (t);
773 break;
775 case WHILE_STMT:
776 genrtl_while_stmt (t);
777 break;
779 case DO_STMT:
780 genrtl_do_stmt (t);
781 break;
783 case IF_STMT:
784 genrtl_if_stmt (t);
785 break;
787 case COMPOUND_STMT:
788 genrtl_compound_stmt (t);
789 break;
791 case BREAK_STMT:
792 genrtl_break_stmt ();
793 break;
795 case CONTINUE_STMT:
796 genrtl_continue_stmt ();
797 break;
799 case SWITCH_STMT:
800 genrtl_switch_stmt (t);
801 break;
803 case CASE_LABEL:
804 genrtl_case_label (t);
805 break;
807 case LABEL_STMT:
808 expand_label (LABEL_STMT_LABEL (t));
809 break;
811 case GOTO_STMT:
812 genrtl_goto_stmt (GOTO_DESTINATION (t));
813 break;
815 case ASM_STMT:
816 genrtl_asm_stmt (ASM_CV_QUAL (t), ASM_STRING (t),
817 ASM_OUTPUTS (t), ASM_INPUTS (t),
818 ASM_CLOBBERS (t), ASM_INPUT_P (t));
819 break;
821 case SCOPE_STMT:
822 genrtl_scope_stmt (t);
823 break;
825 default:
826 if (lang_expand_stmt)
827 (*lang_expand_stmt) (t);
828 else
829 abort ();
830 break;
833 /* Restore saved state. */
834 current_stmt_tree ()->stmts_are_full_exprs_p
835 = saved_stmts_are_full_exprs_p;
837 /* Go on to the next statement in this scope. */
838 t = TREE_CHAIN (t);