c-decl.c (finish_decl): Make a decl_stmt for a variable-sized TYPE_DECL.
[platform/upstream/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 "expr.h"
43 #include "output.h"
44 #include "timevar.h"
45 #include "predict.h"
46 #include "tree-inline.h"
47
48 /* If non-NULL, the address of a language-specific function for
49    expanding statements.  */
50 void (*lang_expand_stmt) (tree);
51
52 /* If non-NULL, the address of a language-specific function for
53    expanding a DECL_STMT.  After the language-independent cases are
54    handled, this function will be called.  If this function is not
55    defined, it is assumed that declarations other than those for
56    variables and labels do not require any RTL generation.  */
57 void (*lang_expand_decl_stmt) (tree);
58
59 static tree find_reachable_label_1 (tree *, int *, void *);
60 static tree find_reachable_label (tree);
61 static bool expand_unreachable_if_stmt (tree);
62 static tree expand_unreachable_stmt (tree, int);
63 static void genrtl_do_stmt_1 (tree, tree);
64
65 /* Create an empty statement tree rooted at T.  */
66
67 void
68 begin_stmt_tree (tree *t)
69 {
70   /* We create a trivial EXPR_STMT so that last_tree is never NULL in
71      what follows.  We remove the extraneous statement in
72      finish_stmt_tree.  */
73   *t = build_nt (EXPR_STMT, void_zero_node);
74   last_tree = *t;
75   last_expr_type = NULL_TREE;
76   last_expr_filename = input_filename;
77 }
78
79 /* T is a statement.  Add it to the statement-tree.  */
80
81 tree
82 add_stmt (tree t)
83 {
84   if (input_filename != last_expr_filename)
85     {
86       /* If the filename has changed, also add in a FILE_STMT.  Do a string
87          compare first, though, as it might be an equivalent string.  */
88       int add = (strcmp (input_filename, last_expr_filename) != 0);
89       last_expr_filename = input_filename;
90       if (add)
91         {
92           tree pos = build_nt (FILE_STMT, get_identifier (input_filename));
93           add_stmt (pos);
94         }
95     }
96
97   /* Add T to the statement-tree.  */
98   TREE_CHAIN (last_tree) = t;
99   last_tree = t;
100
101   /* When we expand a statement-tree, we must know whether or not the
102      statements are full-expressions.  We record that fact here.  */
103   STMT_IS_FULL_EXPR_P (last_tree) = stmts_are_full_exprs_p ();
104
105   return t;
106 }
107
108 /* Create a declaration statement for the declaration given by the
109    DECL.  */
110
111 void
112 add_decl_stmt (tree decl)
113 {
114   tree decl_stmt;
115
116   /* We need the type to last until instantiation time.  */
117   decl_stmt = build_stmt (DECL_STMT, decl);
118   add_stmt (decl_stmt);
119 }
120
121 /* Add a scope-statement to the statement-tree.  BEGIN_P indicates
122    whether this statements opens or closes a scope.  PARTIAL_P is true
123    for a partial scope, i.e, the scope that begins after a label when
124    an object that needs a cleanup is created.  If BEGIN_P is nonzero,
125    returns a new TREE_LIST representing the top of the SCOPE_STMT
126    stack.  The TREE_PURPOSE is the new SCOPE_STMT.  If BEGIN_P is
127    zero, returns a TREE_LIST whose TREE_VALUE is the new SCOPE_STMT,
128    and whose TREE_PURPOSE is the matching SCOPE_STMT with
129    SCOPE_BEGIN_P set.  */
130
131 tree
132 add_scope_stmt (int begin_p, int partial_p)
133 {
134   tree *stack_ptr = current_scope_stmt_stack ();
135   tree ss;
136   tree top = *stack_ptr;
137
138   /* Build the statement.  */
139   ss = build_stmt (SCOPE_STMT, NULL_TREE);
140   SCOPE_BEGIN_P (ss) = begin_p;
141   SCOPE_PARTIAL_P (ss) = partial_p;
142
143   /* Keep the scope stack up to date.  */
144   if (begin_p)
145     {
146       top = tree_cons (ss, NULL_TREE, top);
147       *stack_ptr = top;
148     }
149   else
150     {
151       if (partial_p != SCOPE_PARTIAL_P (TREE_PURPOSE (top)))
152         abort ();
153       TREE_VALUE (top) = ss;
154       *stack_ptr = TREE_CHAIN (top);
155     }
156
157   /* Add the new statement to the statement-tree.  */
158   add_stmt (ss);
159
160   return top;
161 }
162
163 /* Finish the statement tree rooted at T.  */
164
165 void
166 finish_stmt_tree (tree *t)
167 {
168   tree stmt;
169
170   /* Remove the fake extra statement added in begin_stmt_tree.  */
171   stmt = TREE_CHAIN (*t);
172   *t = stmt;
173   last_tree = NULL_TREE;
174
175   if (cfun && stmt)
176     {
177       /* The line-number recorded in the outermost statement in a function
178          is the line number of the end of the function.  */
179       STMT_LINENO (stmt) = input_line;
180       STMT_LINENO_FOR_FN_P (stmt) = 1;
181     }
182 }
183
184 /* Build a generic statement based on the given type of node and
185    arguments. Similar to `build_nt', except that we set
186    STMT_LINENO to be the current line number.  */
187 /* ??? This should be obsolete with the lineno_stmt productions
188    in the grammar.  */
189
190 tree
191 build_stmt (enum tree_code code, ...)
192 {
193   tree t;
194   int length;
195   int i;
196   va_list p;
197
198   va_start (p, code);
199
200   t = make_node (code);
201   length = TREE_CODE_LENGTH (code);
202   STMT_LINENO (t) = input_line;
203
204   for (i = 0; i < length; i++)
205     TREE_OPERAND (t, i) = va_arg (p, tree);
206
207   va_end (p);
208   return t;
209 }
210
211 /* Some statements, like for-statements or if-statements, require a
212    condition.  This condition can be a declaration.  If T is such a
213    declaration it is processed, and an expression appropriate to use
214    as the condition is returned.  Otherwise, T itself is returned.  */
215
216 tree
217 expand_cond (tree t)
218 {
219   if (t && TREE_CODE (t) == TREE_LIST)
220     {
221       expand_stmt (TREE_PURPOSE (t));
222       return TREE_VALUE (t);
223     }
224   else
225     return t;
226 }
227
228 /* Create RTL for the local static variable DECL.  */
229
230 void
231 make_rtl_for_local_static (tree decl)
232 {
233   const char *asmspec = NULL;
234
235   /* If we inlined this variable, we could see it's declaration
236      again.  */
237   if (TREE_ASM_WRITTEN (decl))
238     return;
239
240   /* If the DECL_ASSEMBLER_NAME is not the same as the DECL_NAME, then
241      either we already created RTL for this DECL (and since it was a
242      local variable, its DECL_ASSEMBLER_NAME got hacked up to prevent
243      clashes with other local statics with the same name by a previous
244      call to make_decl_rtl), or the user explicitly requested a
245      particular assembly name for this variable, using the GNU
246      extension for this purpose:
247
248        int i asm ("j");
249
250      There's no way to know which case we're in, here.  But, it turns
251      out we're safe.  If there's already RTL, then
252      rest_of_decl_compilation ignores the ASMSPEC parameter, so we
253      may as well not pass it in.  If there isn't RTL, then we didn't
254      already create RTL, which means that the modification to
255      DECL_ASSEMBLER_NAME came only via the explicit extension.  */
256   if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
257       && !DECL_RTL_SET_P (decl))
258     asmspec = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
259
260   rest_of_decl_compilation (decl, asmspec, /*top_level=*/0, /*at_end=*/0);
261 }
262
263 /* Let the back-end know about DECL.  */
264
265 void
266 emit_local_var (tree decl)
267 {
268   /* Create RTL for this variable.  */
269   if (!DECL_RTL_SET_P (decl))
270     {
271       if (DECL_C_HARD_REGISTER (decl))
272         /* The user specified an assembler name for this variable.
273            Set that up now.  */
274         rest_of_decl_compilation
275           (decl, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
276            /*top_level=*/0, /*at_end=*/0);
277       else
278         expand_decl (decl);
279     }
280
281   if (DECL_INITIAL (decl))
282     {
283       /* Actually do the initialization.  */
284       if (stmts_are_full_exprs_p ())
285         expand_start_target_temps ();
286
287       expand_decl_init (decl);
288
289       if (stmts_are_full_exprs_p ())
290         expand_end_target_temps ();
291     }
292 }
293
294 /* Helper for generating the RTL at the beginning of a scope.  */
295
296 void
297 genrtl_do_pushlevel (void)
298 {
299   emit_line_note (input_location);
300   clear_last_expr ();
301 }
302
303 /* Generate the RTL for DESTINATION, which is a GOTO_STMT.  */
304
305 void
306 genrtl_goto_stmt (tree destination)
307 {
308   if (TREE_CODE (destination) == IDENTIFIER_NODE)
309     abort ();
310
311   /* We warn about unused labels with -Wunused.  That means we have to
312      mark the used labels as used.  */
313   if (TREE_CODE (destination) == LABEL_DECL)
314     TREE_USED (destination) = 1;
315
316   emit_line_note (input_location);
317
318   if (TREE_CODE (destination) == LABEL_DECL)
319     {
320       label_rtx (destination);
321       expand_goto (destination);
322     }
323   else
324     expand_computed_goto (destination);
325 }
326
327 /* Generate the RTL for EXPR, which is an EXPR_STMT.  Provided just
328    for backward compatibility.  genrtl_expr_stmt_value() should be
329    used for new code.  */
330
331 void
332 genrtl_expr_stmt (tree expr)
333 {
334   genrtl_expr_stmt_value (expr, -1, 1);
335 }
336
337 /* Generate the RTL for EXPR, which is an EXPR_STMT.  WANT_VALUE tells
338    whether to (1) save the value of the expression, (0) discard it or
339    (-1) use expr_stmts_for_value to tell.  The use of -1 is
340    deprecated, and retained only for backward compatibility.
341    MAYBE_LAST is nonzero if this EXPR_STMT might be the last statement
342    in expression statement.  */
343
344 void
345 genrtl_expr_stmt_value (tree expr, int want_value, int maybe_last)
346 {
347   if (expr != NULL_TREE)
348     {
349       emit_line_note (input_location);
350
351       if (stmts_are_full_exprs_p ())
352         expand_start_target_temps ();
353
354       if (expr != error_mark_node)
355         expand_expr_stmt_value (expr, want_value, maybe_last);
356
357       if (stmts_are_full_exprs_p ())
358         expand_end_target_temps ();
359     }
360 }
361
362 /* Generate the RTL for T, which is a DECL_STMT.  */
363
364 void
365 genrtl_decl_stmt (tree t)
366 {
367   tree decl;
368   emit_line_note (input_location);
369   decl = DECL_STMT_DECL (t);
370   /* If this is a declaration for an automatic local
371      variable, initialize it.  Note that we might also see a
372      declaration for a namespace-scope object (declared with
373      `extern').  We don't have to handle the initialization
374      of those objects here; they can only be declarations,
375      rather than definitions.  */
376   if (TREE_CODE (decl) == VAR_DECL
377       && !TREE_STATIC (decl)
378       && !DECL_EXTERNAL (decl))
379     {
380       /* Let the back-end know about this variable.  */
381       if (!anon_aggr_type_p (TREE_TYPE (decl)))
382         emit_local_var (decl);
383       else
384         expand_anon_union_decl (decl, NULL_TREE,
385                                 DECL_ANON_UNION_ELEMS (decl));
386     }
387   else if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
388     make_rtl_for_local_static (decl);
389   else if (TREE_CODE (decl) == LABEL_DECL
390            && C_DECLARED_LABEL_FLAG (decl))
391     declare_nonlocal_label (decl);
392   else if (TREE_CODE (decl) == TYPE_DECL)
393     force_type_save_exprs (TREE_TYPE (decl));
394   else if (lang_expand_decl_stmt)
395     (*lang_expand_decl_stmt) (t);
396 }
397
398 /* Generate the RTL for T, which is an IF_STMT.  */
399
400 void
401 genrtl_if_stmt (tree t)
402 {
403   tree cond;
404   genrtl_do_pushlevel ();
405   cond = expand_cond (IF_COND (t));
406   emit_line_note (input_location);
407   expand_start_cond (cond, 0);
408   if (THEN_CLAUSE (t))
409     {
410       tree nextt = THEN_CLAUSE (t);
411
412       if (cond && integer_zerop (cond))
413         nextt = expand_unreachable_stmt (nextt, warn_notreached);
414       expand_stmt (nextt);
415     }
416
417   if (ELSE_CLAUSE (t))
418     {
419       tree nextt = ELSE_CLAUSE (t);
420       expand_start_else ();
421       if (cond && integer_nonzerop (cond))
422         nextt = expand_unreachable_stmt (nextt, warn_notreached);
423       expand_stmt (nextt);
424     }
425   expand_end_cond ();
426 }
427
428 /* Generate the RTL for T, which is a WHILE_STMT.  */
429
430 void
431 genrtl_while_stmt (tree t)
432 {
433   tree cond = WHILE_COND (t);
434
435   emit_line_note (input_location);
436   expand_start_loop (1);
437   genrtl_do_pushlevel ();
438
439   if (cond && !integer_nonzerop (cond))
440     {
441       cond = expand_cond (cond);
442       emit_line_note (input_location);
443       expand_exit_loop_top_cond (0, cond);
444       genrtl_do_pushlevel ();
445     }
446
447   expand_stmt (WHILE_BODY (t));
448
449   expand_end_loop ();
450 }
451
452 /* Generate the RTL for a DO_STMT with condition COND and loop BODY
453    body.  This is reused for expanding unreachable WHILE_STMTS.  */
454
455 static void
456 genrtl_do_stmt_1 (tree cond, tree body)
457 {
458   /* Recognize the common special-case of do { ... } while (0) and do
459      not emit the loop widgetry in this case.  In particular this
460      avoids cluttering the rtl with dummy loop notes, which can affect
461      alignment of adjacent labels.  COND can be NULL due to parse
462      errors.  */
463   if (!cond || integer_zerop (cond))
464     {
465       expand_start_null_loop ();
466       expand_stmt (body);
467       expand_end_null_loop ();
468     }
469   else if (integer_nonzerop (cond))
470     {
471       emit_line_note (input_location);
472       expand_start_loop (1);
473
474       expand_stmt (body);
475
476       emit_line_note (input_location);
477       expand_end_loop ();
478     }
479   else
480     {
481       emit_line_note (input_location);
482       expand_start_loop_continue_elsewhere (1);
483
484       expand_stmt (body);
485
486       expand_loop_continue_here ();
487       cond = expand_cond (cond);
488       emit_line_note (input_location);
489       expand_exit_loop_if_false (0, cond);
490       expand_end_loop ();
491     }
492 }
493
494 /* Generate the RTL for T, which is a DO_STMT.  */
495
496 void
497 genrtl_do_stmt (tree t)
498 {
499   genrtl_do_stmt_1 (DO_COND (t), DO_BODY (t));
500 }
501
502 /* Build the node for a return statement and return it.  */
503
504 tree
505 build_return_stmt (tree expr)
506 {
507   return (build_stmt (RETURN_STMT, expr));
508 }
509
510 /* Generate the RTL for STMT, which is a RETURN_STMT.  */
511
512 void
513 genrtl_return_stmt (tree stmt)
514 {
515   tree expr;
516
517   expr = RETURN_STMT_EXPR (stmt);
518
519   emit_line_note (input_location);
520   if (!expr)
521     expand_null_return ();
522   else
523     {
524       expand_start_target_temps ();
525       expand_return (expr);
526       expand_end_target_temps ();
527     }
528 }
529
530 /* Generate the RTL for T, which is a FOR_STMT.  */
531
532 void
533 genrtl_for_stmt (tree t)
534 {
535   tree cond = FOR_COND (t);
536   location_t saved_loc;
537
538   if (NEW_FOR_SCOPE_P (t))
539     genrtl_do_pushlevel ();
540
541   expand_stmt (FOR_INIT_STMT (t));
542
543   /* Expand the initialization.  */
544   emit_line_note (input_location);
545   if (FOR_EXPR (t))
546     expand_start_loop_continue_elsewhere (1);
547   else
548     expand_start_loop (1);
549   genrtl_do_pushlevel ();
550
551   /* Save the filename and line number so that we expand the FOR_EXPR
552      we can reset them back to the saved values.  */
553   saved_loc = input_location;
554
555   /* Expand the condition.  */
556   if (cond && !integer_nonzerop (cond))
557     {
558       cond = expand_cond (cond);
559       emit_line_note (input_location);
560       expand_exit_loop_top_cond (0, cond);
561       genrtl_do_pushlevel ();
562     }
563
564   /* Expand the body.  */
565   expand_stmt (FOR_BODY (t));
566
567   /* Expand the increment expression.  */
568   input_location = saved_loc;
569   emit_line_note (input_location);
570   if (FOR_EXPR (t))
571     {
572       expand_loop_continue_here ();
573       genrtl_expr_stmt (FOR_EXPR (t));
574     }
575   expand_end_loop ();
576 }
577
578 /* Build a break statement node and return it.  */
579
580 tree
581 build_break_stmt (void)
582 {
583   return (build_stmt (BREAK_STMT));
584 }
585
586 /* Generate the RTL for a BREAK_STMT.  */
587
588 void
589 genrtl_break_stmt (void)
590 {
591   emit_line_note (input_location);
592   if ( ! expand_exit_something ())
593     abort ();
594 }
595
596 /* Build a continue statement node and return it.  */
597
598 tree
599 build_continue_stmt (void)
600 {
601   return (build_stmt (CONTINUE_STMT));
602 }
603
604 /* Generate the RTL for a CONTINUE_STMT.  */
605
606 void
607 genrtl_continue_stmt (void)
608 {
609   emit_line_note (input_location);
610   if (! expand_continue_loop (0))
611     abort ();
612 }
613
614 /* Generate the RTL for T, which is a SCOPE_STMT.  */
615
616 void
617 genrtl_scope_stmt (tree t)
618 {
619   tree block = SCOPE_STMT_BLOCK (t);
620
621   if (!SCOPE_NO_CLEANUPS_P (t))
622     {
623       if (SCOPE_BEGIN_P (t))
624         expand_start_bindings_and_block (2 * SCOPE_NULLIFIED_P (t), block);
625       else if (SCOPE_END_P (t))
626         expand_end_bindings (NULL_TREE, !SCOPE_NULLIFIED_P (t), 0);
627     }
628   else if (!SCOPE_NULLIFIED_P (t))
629     {
630       rtx note = emit_note (SCOPE_BEGIN_P (t)
631                             ? NOTE_INSN_BLOCK_BEG : NOTE_INSN_BLOCK_END);
632       NOTE_BLOCK (note) = block;
633     }
634
635   /* If we're at the end of a scope that contains inlined nested
636      functions, we have to decide whether or not to write them out.  */
637   if (block && SCOPE_END_P (t))
638     {
639       tree fn;
640
641       for (fn = BLOCK_VARS (block); fn; fn = TREE_CHAIN (fn))
642         {
643           if (TREE_CODE (fn) == FUNCTION_DECL
644               && DECL_CONTEXT (fn) == current_function_decl
645               && DECL_STRUCT_FUNCTION (fn)
646               && DECL_STRUCT_FUNCTION (fn)->saved_for_inline
647               && !TREE_ASM_WRITTEN (fn)
648               && TREE_ADDRESSABLE (fn))
649             {
650               push_function_context ();
651               output_inline_function (fn);
652               pop_function_context ();
653             }
654         }
655     }
656 }
657
658 /* Generate the RTL for T, which is a SWITCH_STMT.  */
659
660 void
661 genrtl_switch_stmt (tree t)
662 {
663   tree cond;
664   genrtl_do_pushlevel ();
665
666   cond = expand_cond (SWITCH_COND (t));
667   if (cond == error_mark_node)
668     /* The code is in error, but we don't want expand_end_case to
669        crash.  */
670     cond = truthvalue_false_node;
671
672   emit_line_note (input_location);
673   expand_start_case (1, cond, TREE_TYPE (cond), "switch statement");
674   expand_stmt (expand_unreachable_stmt (SWITCH_BODY (t), warn_notreached));
675   expand_end_case_type (cond, SWITCH_TYPE (t));
676 }
677
678 /* Create a CASE_LABEL tree node and return it.  */
679
680 tree
681 build_case_label (tree low_value, tree high_value, tree label_decl)
682 {
683   return build_stmt (CASE_LABEL, low_value, high_value, label_decl);
684 }
685
686
687 /* Generate the RTL for a CASE_LABEL.  */
688
689 void
690 genrtl_case_label (tree case_label)
691 {
692   tree duplicate;
693   tree cleanup;
694
695   cleanup = last_cleanup_this_contour ();
696   if (cleanup)
697     {
698       static int explained = 0;
699       warning ("destructor needed for `%D'", (TREE_PURPOSE (cleanup)));
700       warning ("where case label appears here");
701       if (!explained)
702         {
703           warning ("(enclose actions of previous case statements requiring destructors in their own scope.)");
704           explained = 1;
705         }
706     }
707
708   add_case_node (CASE_LOW (case_label), CASE_HIGH (case_label),
709                  CASE_LABEL_DECL (case_label), &duplicate);
710 }
711
712 /* Generate the RTL for T, which is a COMPOUND_STMT.  */
713
714 void
715 genrtl_compound_stmt (tree t)
716 {
717 #ifdef ENABLE_CHECKING
718   struct nesting *n = current_nesting_level ();
719 #endif
720
721   expand_stmt (COMPOUND_BODY (t));
722
723 #ifdef ENABLE_CHECKING
724   /* Make sure that we've pushed and popped the same number of levels.  */
725   if (!COMPOUND_STMT_NO_SCOPE (t) && n != current_nesting_level ())
726     abort ();
727 #endif
728 }
729
730 /* Generate the RTL for an ASM_STMT.  */
731
732 void
733 genrtl_asm_stmt (tree cv_qualifier, tree string, tree output_operands,
734                  tree input_operands, tree clobbers, int asm_input_p)
735 {
736   if (cv_qualifier != NULL_TREE
737       && cv_qualifier != ridpointers[(int) RID_VOLATILE])
738     {
739       warning ("%s qualifier ignored on asm",
740                IDENTIFIER_POINTER (cv_qualifier));
741       cv_qualifier = NULL_TREE;
742     }
743
744   emit_line_note (input_location);
745   if (asm_input_p)
746     expand_asm (string, cv_qualifier != NULL_TREE);
747   else
748     c_expand_asm_operands (string, output_operands, input_operands,
749                            clobbers, cv_qualifier != NULL_TREE,
750                            input_location);
751 }
752
753 /* Generate the RTL for a CLEANUP_STMT.  */
754
755 void
756 genrtl_cleanup_stmt (tree t)
757 {
758   tree decl = CLEANUP_DECL (t);
759   if (!decl || !DECL_P (decl)
760       || (DECL_SIZE (decl) && TREE_TYPE (decl) != error_mark_node))
761     expand_decl_cleanup_eh (decl, CLEANUP_EXPR (t), CLEANUP_EH_ONLY (t));
762 }
763
764 /* We're about to expand T, a statement.  Set up appropriate context
765    for the substitution.  */
766
767 void
768 prep_stmt (tree t)
769 {
770   if (!STMT_LINENO_FOR_FN_P (t))
771     input_line = STMT_LINENO (t);
772   current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
773 }
774
775 /* Generate the RTL for the statement T, its substatements, and any
776    other statements at its nesting level.  */
777
778 void
779 expand_stmt (tree t)
780 {
781   while (t && t != error_mark_node)
782     {
783       int saved_stmts_are_full_exprs_p;
784
785       /* Set up context appropriately for handling this statement.  */
786       saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
787       prep_stmt (t);
788
789       switch (TREE_CODE (t))
790         {
791         case FILE_STMT:
792           input_filename = FILE_STMT_FILENAME (t);
793           break;
794
795         case RETURN_STMT:
796           genrtl_return_stmt (t);
797           t = expand_unreachable_stmt (TREE_CHAIN (t), warn_notreached);
798           goto process_t;
799
800         case EXPR_STMT:
801           genrtl_expr_stmt_value (EXPR_STMT_EXPR (t), TREE_ADDRESSABLE (t),
802                                   TREE_CHAIN (t) == NULL
803                                   || (TREE_CODE (TREE_CHAIN (t)) == SCOPE_STMT
804                                       && TREE_CHAIN (TREE_CHAIN (t)) == NULL));
805           break;
806
807         case DECL_STMT:
808           genrtl_decl_stmt (t);
809           break;
810
811         case FOR_STMT:
812           genrtl_for_stmt (t);
813           break;
814
815         case WHILE_STMT:
816           genrtl_while_stmt (t);
817           break;
818
819         case DO_STMT:
820           genrtl_do_stmt (t);
821           break;
822
823         case IF_STMT:
824           genrtl_if_stmt (t);
825           break;
826
827         case COMPOUND_STMT:
828           genrtl_compound_stmt (t);
829           break;
830
831         case BREAK_STMT:
832           genrtl_break_stmt ();
833           t = expand_unreachable_stmt (TREE_CHAIN (t), warn_notreached);
834           goto process_t;
835
836         case CONTINUE_STMT:
837           genrtl_continue_stmt ();
838           t = expand_unreachable_stmt (TREE_CHAIN (t), warn_notreached);
839           goto process_t;
840
841         case SWITCH_STMT:
842           genrtl_switch_stmt (t);
843           break;
844
845         case CASE_LABEL:
846           genrtl_case_label (t);
847           break;
848
849         case LABEL_STMT:
850           expand_label (LABEL_STMT_LABEL (t));
851           break;
852
853         case GOTO_STMT:
854           /* Emit information for branch prediction.  */
855           if (!GOTO_FAKE_P (t)
856               && TREE_CODE (GOTO_DESTINATION (t)) == LABEL_DECL
857               && flag_guess_branch_prob)
858             {
859               rtx note = emit_note (NOTE_INSN_PREDICTION);
860
861               NOTE_PREDICTION (note) = NOTE_PREDICT (PRED_GOTO, NOT_TAKEN);
862             }
863           genrtl_goto_stmt (GOTO_DESTINATION (t));
864           t = expand_unreachable_stmt (TREE_CHAIN (t), warn_notreached);
865           goto process_t;
866
867         case ASM_STMT:
868           genrtl_asm_stmt (ASM_CV_QUAL (t), ASM_STRING (t),
869                            ASM_OUTPUTS (t), ASM_INPUTS (t),
870                            ASM_CLOBBERS (t), ASM_INPUT_P (t));
871           break;
872
873         case SCOPE_STMT:
874           genrtl_scope_stmt (t);
875           break;
876
877         case CLEANUP_STMT:
878           genrtl_cleanup_stmt (t);
879           break;
880
881         default:
882           if (lang_expand_stmt)
883             (*lang_expand_stmt) (t);
884           else
885             abort ();
886           break;
887         }
888
889       /* Go on to the next statement in this scope.  */
890       t = TREE_CHAIN (t);
891
892     process_t:
893       /* Restore saved state.  */
894       current_stmt_tree ()->stmts_are_full_exprs_p
895         = saved_stmts_are_full_exprs_p;
896     }
897 }
898 \f
899 /* If *TP is a potentially reachable label, return nonzero.  */
900
901 static tree
902 find_reachable_label_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
903                         void *data ATTRIBUTE_UNUSED)
904 {
905   switch (TREE_CODE (*tp))
906     {
907     case LABEL_STMT:
908     case CASE_LABEL:
909       return *tp;
910
911     default:
912       break;
913     }
914   return NULL_TREE;
915 }
916
917 /* Determine whether expression EXP contains a potentially
918    reachable label.  */
919 static tree
920 find_reachable_label (tree exp)
921 {
922   location_t saved_loc = input_location;
923   tree ret = walk_tree_without_duplicates
924                 (&exp, find_reachable_label_1, NULL);
925   input_location = saved_loc;
926   return ret;
927 }
928
929 /* Expand an unreachable if statement, T.  This function returns
930    true if the IF_STMT contains a potentially reachable code_label.  */
931 static bool
932 expand_unreachable_if_stmt (tree t)
933 {
934   tree n;
935
936   if (find_reachable_label (IF_COND (t)) != NULL_TREE)
937     {
938       genrtl_if_stmt (t);
939       return true;
940     }
941
942   if (THEN_CLAUSE (t) && ELSE_CLAUSE (t))
943     {
944       n = expand_unreachable_stmt (THEN_CLAUSE (t), 0);
945
946       if (n != NULL_TREE)
947         {
948           rtx label;
949           expand_stmt (n);
950           label = gen_label_rtx ();
951           emit_jump (label);
952           expand_stmt (expand_unreachable_stmt (ELSE_CLAUSE (t), 0));
953           emit_label (label);
954           return true;
955         }
956       else
957         n = expand_unreachable_stmt (ELSE_CLAUSE (t), 0);
958     }
959   else if (THEN_CLAUSE (t))
960     n = expand_unreachable_stmt (THEN_CLAUSE (t), 0);
961   else if (ELSE_CLAUSE (t))
962     n = expand_unreachable_stmt (ELSE_CLAUSE (t), 0);
963   else
964     n = NULL_TREE;
965
966   expand_stmt (n);
967
968   return n != NULL_TREE;
969 }
970
971 /* Expand an unreachable statement list.  This function skips all
972    statements preceding the first potentially reachable label and
973    then returns the label (or, in same cases, the statement after
974    one containing the label).  */
975 static tree
976 expand_unreachable_stmt (tree t, int warn)
977 {
978   int saved;
979
980   while (t && t != error_mark_node)
981     {
982       if (warn)
983         switch (TREE_CODE (t))
984           {
985           case BREAK_STMT:
986           case CONTINUE_STMT:
987           case EXPR_STMT:
988           case GOTO_STMT:
989           case IF_STMT:
990           case RETURN_STMT:
991             if (!STMT_LINENO_FOR_FN_P (t))
992               input_line = STMT_LINENO (t);
993             warning("will never be executed");
994             warn = false;
995             break;
996
997           default:
998             break;
999           }
1000
1001       switch (TREE_CODE (t))
1002         {
1003         case GOTO_STMT:
1004         case CONTINUE_STMT:
1005         case BREAK_STMT:
1006           break;
1007
1008         case FILE_STMT:
1009           input_filename = FILE_STMT_FILENAME (t);
1010           break;
1011
1012         case RETURN_STMT:
1013           if (find_reachable_label (RETURN_STMT_EXPR (t)) != NULL_TREE)
1014             return t;
1015           break;
1016
1017         case EXPR_STMT:
1018           if (find_reachable_label (EXPR_STMT_EXPR (t)) != NULL_TREE)
1019             return t;
1020           break;
1021
1022         case IF_STMT:
1023           if (expand_unreachable_if_stmt (t))
1024             return TREE_CHAIN (t);
1025           break;
1026
1027         case WHILE_STMT:
1028           /* If the start of a while statement is unreachable, there is
1029              no need to rotate the loop, instead the WHILE_STMT can be
1030              expanded like a DO_STMT.  */
1031           genrtl_do_stmt_1 (WHILE_COND (t), WHILE_BODY (t));
1032           return TREE_CHAIN (t);
1033
1034         case COMPOUND_STMT:
1035           {
1036             tree n;
1037             n = expand_unreachable_stmt (COMPOUND_BODY (t), warn);
1038             if (n != NULL_TREE)
1039               {
1040                 expand_stmt (n);
1041                 return TREE_CHAIN (t);
1042               }
1043             warn = false;
1044             break;
1045           }
1046
1047         case SCOPE_STMT:
1048           saved = stmts_are_full_exprs_p ();
1049           prep_stmt (t);
1050           genrtl_scope_stmt (t);
1051           current_stmt_tree ()->stmts_are_full_exprs_p = saved;
1052           break;
1053
1054         default:
1055           return t;
1056         }
1057       t = TREE_CHAIN (t);
1058     }
1059   return NULL_TREE;
1060 }