aboutsummaryrefslogtreecommitdiff
path: root/gcc/c-semantics.c
blob: e75caa70101b3f7be0d533d6778d1921203e730a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
/* This file contains the definitions and documentation for the common
   tree codes used in the GNU C and C++ compilers (see c-common.def
   for the standard codes).
   Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
   Written by Benjamin Chelf (chelf@codesourcery.com).

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "function.h"
#include "splay-tree.h"
#include "varray.h"
#include "c-common.h"
#include "except.h"
#include "toplev.h"
#include "flags.h"
#include "ggc.h"
#include "rtl.h"
#include "expr.h"
#include "output.h"
#include "timevar.h"
#include "predict.h"
#include "tree-inline.h"

/* If non-NULL, the address of a language-specific function for
   expanding statements.  */
void (*lang_expand_stmt) (tree);

/* If non-NULL, the address of a language-specific function for
   expanding a DECL_STMT.  After the language-independent cases are
   handled, this function will be called.  If this function is not
   defined, it is assumed that declarations other than those for
   variables and labels do not require any RTL generation.  */
void (*lang_expand_decl_stmt) (tree);

static tree find_reachable_label_1 (tree *, int *, void *);
static tree find_reachable_label (tree);
static bool expand_unreachable_if_stmt (tree);
static tree expand_unreachable_stmt (tree, int);
static void genrtl_do_stmt_1 (tree, tree);

/* Create an empty statement tree rooted at T.  */

void
begin_stmt_tree (tree *t)
{
  /* We create a trivial EXPR_STMT so that last_tree is never NULL in
     what follows.  We remove the extraneous statement in
     finish_stmt_tree.  */
  *t = build_nt (EXPR_STMT, void_zero_node);
  last_tree = *t;
  last_expr_type = NULL_TREE;
  last_expr_filename = input_filename;
}

/* T is a statement.  Add it to the statement-tree.  */

tree
add_stmt (tree t)
{
  if (input_filename != last_expr_filename)
    {
      /* If the filename has changed, also add in a FILE_STMT.  Do a string
	 compare first, though, as it might be an equivalent string.  */
      int add = (strcmp (input_filename, last_expr_filename) != 0);
      last_expr_filename = input_filename;
      if (add)
	{
	  tree pos = build_nt (FILE_STMT, get_identifier (input_filename));
	  add_stmt (pos);
	}
    }

  /* Add T to the statement-tree.  */
  TREE_CHAIN (last_tree) = t;
  last_tree = t;

  /* When we expand a statement-tree, we must know whether or not the
     statements are full-expressions.  We record that fact here.  */
  STMT_IS_FULL_EXPR_P (last_tree) = stmts_are_full_exprs_p ();

  /* Keep track of the number of statements in this function.  */
  if (current_function_decl)
    ++DECL_NUM_STMTS (current_function_decl);

  return t;
}

/* Create a declaration statement for the declaration given by the
   DECL.  */

void
add_decl_stmt (tree decl)
{
  tree decl_stmt;

  /* We need the type to last until instantiation time.  */
  decl_stmt = build_stmt (DECL_STMT, decl);
  add_stmt (decl_stmt);
}

/* Add a scope-statement to the statement-tree.  BEGIN_P indicates
   whether this statements opens or closes a scope.  PARTIAL_P is true
   for a partial scope, i.e, the scope that begins after a label when
   an object that needs a cleanup is created.  If BEGIN_P is nonzero,
   returns a new TREE_LIST representing the top of the SCOPE_STMT
   stack.  The TREE_PURPOSE is the new SCOPE_STMT.  If BEGIN_P is
   zero, returns a TREE_LIST whose TREE_VALUE is the new SCOPE_STMT,
   and whose TREE_PURPOSE is the matching SCOPE_STMT with
   SCOPE_BEGIN_P set.  */

tree
add_scope_stmt (int begin_p, int partial_p)
{
  tree *stack_ptr = current_scope_stmt_stack ();
  tree ss;
  tree top = *stack_ptr;

  /* Build the statement.  */
  ss = build_stmt (SCOPE_STMT, NULL_TREE);
  SCOPE_BEGIN_P (ss) = begin_p;
  SCOPE_PARTIAL_P (ss) = partial_p;

  /* Keep the scope stack up to date.  */
  if (begin_p)
    {
      top = tree_cons (ss, NULL_TREE, top);
      *stack_ptr = top;
    }
  else
    {
      if (partial_p != SCOPE_PARTIAL_P (TREE_PURPOSE (top)))
	abort ();
      TREE_VALUE (top) = ss;
      *stack_ptr = TREE_CHAIN (top);
    }

  /* Add the new statement to the statement-tree.  */
  add_stmt (ss);

  return top;
}

/* Finish the statement tree rooted at T.  */

void
finish_stmt_tree (tree *t)
{
  tree stmt;

  /* Remove the fake extra statement added in begin_stmt_tree.  */
  stmt = TREE_CHAIN (*t);
  *t = stmt;
  last_tree = NULL_TREE;

  if (cfun && stmt)
    {
      /* The line-number recorded in the outermost statement in a function
	 is the line number of the end of the function.  */
      STMT_LINENO (stmt) = input_line;
      STMT_LINENO_FOR_FN_P (stmt) = 1;
    }
}

/* Build a generic statement based on the given type of node and
   arguments. Similar to `build_nt', except that we set
   STMT_LINENO to be the current line number.  */
/* ??? This should be obsolete with the lineno_stmt productions
   in the grammar.  */

tree
build_stmt (enum tree_code code, ...)
{
  tree t;
  int length;
  int i;
  va_list p;

  va_start (p, code);

  t = make_node (code);
  length = TREE_CODE_LENGTH (code);
  STMT_LINENO (t) = input_line;

  for (i = 0; i < length; i++)
    TREE_OPERAND (t, i) = va_arg (p, tree);

  va_end (p);
  return t;
}

/* Some statements, like for-statements or if-statements, require a
   condition.  This condition can be a declaration.  If T is such a
   declaration it is processed, and an expression appropriate to use
   as the condition is returned.  Otherwise, T itself is returned.  */

tree
expand_cond (tree t)
{
  if (t && TREE_CODE (t) == TREE_LIST)
    {
      expand_stmt (TREE_PURPOSE (t));
      return TREE_VALUE (t);
    }
  else
    return t;
}

/* Create RTL for the local static variable DECL.  */

void
make_rtl_for_local_static (tree decl)
{
  const char *asmspec = NULL;

  /* If we inlined this variable, we could see it's declaration
     again.  */
  if (TREE_ASM_WRITTEN (decl))
    return;

  /* If the DECL_ASSEMBLER_NAME is not the same as the DECL_NAME, then
     either we already created RTL for this DECL (and since it was a
     local variable, its DECL_ASSEMBLER_NAME got hacked up to prevent
     clashes with other local statics with the same name by a previous
     call to make_decl_rtl), or the user explicitly requested a
     particular assembly name for this variable, using the GNU
     extension for this purpose:

       int i asm ("j");

     There's no way to know which case we're in, here.  But, it turns
     out we're safe.  If there's already RTL, then
     rest_of_decl_compilation ignores the ASMSPEC parameter, so we
     may as well not pass it in.  If there isn't RTL, then we didn't
     already create RTL, which means that the modification to
     DECL_ASSEMBLER_NAME came only via the explicit extension.  */
  if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
      && !DECL_RTL_SET_P (decl))
    asmspec = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));

  rest_of_decl_compilation (decl, asmspec, /*top_level=*/0, /*at_end=*/0);
}

/* Let the back-end know about DECL.  */

void
emit_local_var (tree decl)
{
  /* Create RTL for this variable.  */
  if (!DECL_RTL_SET_P (decl))
    {
      if (DECL_C_HARD_REGISTER (decl))
	/* The user specified an assembler name for this variable.
	   Set that up now.  */
	rest_of_decl_compilation
	  (decl, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
	   /*top_level=*/0, /*at_end=*/0);
      else
	expand_decl (decl);
    }

  if (DECL_INITIAL (decl))
    {
      /* Actually do the initialization.  */
      if (stmts_are_full_exprs_p ())
	expand_start_target_temps ();

      expand_decl_init (decl);

      if (stmts_are_full_exprs_p ())
	expand_end_target_temps ();
    }
}

/* Helper for generating the RTL at the beginning of a scope.  */

void
genrtl_do_pushlevel (void)
{
  emit_line_note (input_filename, input_line);
  clear_last_expr ();
}

/* Generate the RTL for DESTINATION, which is a GOTO_STMT.  */

void
genrtl_goto_stmt (tree destination)
{
  if (TREE_CODE (destination) == IDENTIFIER_NODE)
    abort ();

  /* We warn about unused labels with -Wunused.  That means we have to
     mark the used labels as used.  */
  if (TREE_CODE (destination) == LABEL_DECL)
    TREE_USED (destination) = 1;

  emit_line_note (input_filename, input_line);

  if (TREE_CODE (destination) == LABEL_DECL)
    {
      label_rtx (destination);
      expand_goto (destination);
    }
  else
    expand_computed_goto (destination);
}

/* Generate the RTL for EXPR, which is an EXPR_STMT.  Provided just
   for backward compatibility.  genrtl_expr_stmt_value() should be
   used for new code.  */

void
genrtl_expr_stmt (tree expr)
{
  genrtl_expr_stmt_value (expr, -1, 1);
}

/* Generate the RTL for EXPR, which is an EXPR_STMT.  WANT_VALUE tells
   whether to (1) save the value of the expression, (0) discard it or
   (-1) use expr_stmts_for_value to tell.  The use of -1 is
   deprecated, and retained only for backward compatibility.
   MAYBE_LAST is nonzero if this EXPR_STMT might be the last statement
   in expression statement.  */

void
genrtl_expr_stmt_value (tree expr, int want_value, int maybe_last)
{
  if (expr != NULL_TREE)
    {
      emit_line_note (input_filename, input_line);

      if (stmts_are_full_exprs_p ())
	expand_start_target_temps ();

      if (expr != error_mark_node)
	expand_expr_stmt_value (expr, want_value, maybe_last);

      if (stmts_are_full_exprs_p ())
	expand_end_target_temps ();
    }
}

/* Generate the RTL for T, which is a DECL_STMT.  */

void
genrtl_decl_stmt (tree t)
{
  tree decl;
  emit_line_note (input_filename, input_line);
  decl = DECL_STMT_DECL (t);
  /* If this is a declaration for an automatic local
     variable, initialize it.  Note that we might also see a
     declaration for a namespace-scope object (declared with
     `extern').  We don't have to handle the initialization
     of those objects here; they can only be declarations,
     rather than definitions.  */
  if (TREE_CODE (decl) == VAR_DECL
      && !TREE_STATIC (decl)
      && !DECL_EXTERNAL (decl))
    {
      /* Let the back-end know about this variable.  */
      if (!anon_aggr_type_p (TREE_TYPE (decl)))
	emit_local_var (decl);
      else
	expand_anon_union_decl (decl, NULL_TREE,
				DECL_ANON_UNION_ELEMS (decl));
    }
  else if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
    make_rtl_for_local_static (decl);
  else if (TREE_CODE (decl) == LABEL_DECL
	   && C_DECLARED_LABEL_FLAG (decl))
    declare_nonlocal_label (decl);
  else if (lang_expand_decl_stmt)
    (*lang_expand_decl_stmt) (t);
}

/* Generate the RTL for T, which is an IF_STMT.  */

void
genrtl_if_stmt (tree t)
{
  tree cond;
  genrtl_do_pushlevel ();
  cond = expand_cond (IF_COND (t));
  emit_line_note (input_filename, input_line);
  expand_start_cond (cond, 0);
  if (THEN_CLAUSE (t))
    {
      tree nextt = THEN_CLAUSE (t);

      if (cond && integer_zerop (cond))
	nextt = expand_unreachable_stmt (nextt, warn_notreached);
      expand_stmt (nextt);
    }

  if (ELSE_CLAUSE (t))
    {
      tree nextt = ELSE_CLAUSE (t);
      expand_start_else ();
      if (cond && integer_nonzerop (cond))
	nextt = expand_unreachable_stmt (nextt, warn_notreached);
      expand_stmt (nextt);
    }
  expand_end_cond ();
}

/* Generate the RTL for T, which is a WHILE_STMT.  */

void
genrtl_while_stmt (tree t)
{
  tree cond = WHILE_COND (t);

  emit_nop ();
  emit_line_note (input_filename, input_line);
  expand_start_loop (1);
  genrtl_do_pushlevel ();

  if (cond && !integer_nonzerop (cond))
    {
      cond = expand_cond (cond);
      emit_line_note (input_filename, input_line);
      expand_exit_loop_top_cond (0, cond);
      genrtl_do_pushlevel ();
    }

  expand_stmt (WHILE_BODY (t));

  expand_end_loop ();
}

/* Generate the RTL for a DO_STMT with condition COND and loop BODY
   body.  This is reused for expanding unreachable WHILE_STMTS.  */

static void
genrtl_do_stmt_1 (tree cond, tree body)
{
  /* Recognize the common special-case of do { ... } while (0) and do
     not emit the loop widgetry in this case.  In particular this
     avoids cluttering the rtl with dummy loop notes, which can affect
     alignment of adjacent labels.  COND can be NULL due to parse
     errors.  */
  if (!cond || integer_zerop (cond))
    {
      expand_start_null_loop ();
      expand_stmt (body);
      expand_end_null_loop ();
    }
  else if (integer_nonzerop (cond))
    {
      emit_nop ();
      emit_line_note (input_filename, input_line);
      expand_start_loop (1);

      expand_stmt (body);

      emit_line_note (input_filename, input_line);
      expand_end_loop ();
    }
  else
    {
      emit_nop ();
      emit_line_note (input_filename, input_line);
      expand_start_loop_continue_elsewhere (1);

      expand_stmt (body);

      expand_loop_continue_here ();
      cond = expand_cond (cond);
      emit_line_note (input_filename, input_line);
      expand_exit_loop_if_false (0, cond);
      expand_end_loop ();
    }
}

/* Generate the RTL for T, which is a DO_STMT.  */

void
genrtl_do_stmt (tree t)
{
  genrtl_do_stmt_1 (DO_COND (t), DO_BODY (t));
}

/* Build the node for a return statement and return it.  */

tree
build_return_stmt (tree expr)
{
  return (build_stmt (RETURN_STMT, expr));
}

/* Generate the RTL for STMT, which is a RETURN_STMT.  */

void
genrtl_return_stmt (tree stmt)
{
  tree expr;

  expr = RETURN_STMT_EXPR (stmt);

  emit_line_note (input_filename, input_line);
  if (!expr)
    expand_null_return ();
  else
    {
      expand_start_target_temps ();
      expand_return (expr);
      expand_end_target_temps ();
    }
}

/* Generate the RTL for T, which is a FOR_STMT.  */

void
genrtl_for_stmt (tree t)
{
  tree cond = FOR_COND (t);
  location_t saved_loc;

  if (NEW_FOR_SCOPE_P (t))
    genrtl_do_pushlevel ();

  expand_stmt (FOR_INIT_STMT (t));

  /* Expand the initialization.  */
  emit_nop ();
  emit_line_note (input_filename, input_line);
  if (FOR_EXPR (t))
    expand_start_loop_continue_elsewhere (1);
  else
    expand_start_loop (1);
  genrtl_do_pushlevel ();

  /* Save the filename and line number so that we expand the FOR_EXPR
     we can reset them back to the saved values.  */
  saved_loc = input_location;

  /* Expand the condition.  */
  if (cond && !integer_nonzerop (cond))
    {
      cond = expand_cond (cond);
      emit_line_note (input_filename, input_line);
      expand_exit_loop_top_cond (0, cond);
      genrtl_do_pushlevel ();
    }

  /* Expand the body.  */
  expand_stmt (FOR_BODY (t));

  /* Expand the increment expression.  */
  input_location = saved_loc;
  emit_line_note (input_filename, input_line);
  if (FOR_EXPR (t))
    {
      expand_loop_continue_here ();
      genrtl_expr_stmt (FOR_EXPR (t));
    }
  expand_end_loop ();
}

/* Build a break statement node and return it.  */

tree
build_break_stmt (void)
{
  return (build_stmt (BREAK_STMT));
}

/* Generate the RTL for a BREAK_STMT.  */

void
genrtl_break_stmt (void)
{
  emit_line_note (input_filename, input_line);
  if ( ! expand_exit_something ())
    error ("break statement not within loop or switch");
}

/* Build a continue statement node and return it.  */

tree
build_continue_stmt (void)
{
  return (build_stmt (CONTINUE_STMT));
}

/* Generate the RTL for a CONTINUE_STMT.  */

void
genrtl_continue_stmt (void)
{
  emit_line_note (input_filename, input_line);
  if (! expand_continue_loop (0))
    error ("continue statement not within a loop");
}

/* Generate the RTL for T, which is a SCOPE_STMT.  */

void
genrtl_scope_stmt (tree t)
{
  tree block = SCOPE_STMT_BLOCK (t);

  if (!SCOPE_NO_CLEANUPS_P (t))
    {
      if (SCOPE_BEGIN_P (t))
	expand_start_bindings_and_block (2 * SCOPE_NULLIFIED_P (t), block);
      else if (SCOPE_END_P (t))
	expand_end_bindings (NULL_TREE, !SCOPE_NULLIFIED_P (t), 0);
    }
  else if (!SCOPE_NULLIFIED_P (t))
    {
      rtx note = emit_note (SCOPE_BEGIN_P (t)
			    ? NOTE_INSN_BLOCK_BEG : NOTE_INSN_BLOCK_END);
      NOTE_BLOCK (note) = block;
    }

  /* If we're at the end of a scope that contains inlined nested
     functions, we have to decide whether or not to write them out.  */
  if (block && SCOPE_END_P (t))
    {
      tree fn;

      for (fn = BLOCK_VARS (block); fn; fn = TREE_CHAIN (fn))
	{
	  if (TREE_CODE (fn) == FUNCTION_DECL
	      && DECL_CONTEXT (fn) == current_function_decl
	      && DECL_SAVED_INSNS (fn)
	      && !TREE_ASM_WRITTEN (fn)
	      && TREE_ADDRESSABLE (fn))
	    {
	      push_function_context ();
	      output_inline_function (fn);
	      pop_function_context ();
	    }
	}
    }
}

/* Generate the RTL for T, which is a SWITCH_STMT.  */

void
genrtl_switch_stmt (tree t)
{
  tree cond;
  genrtl_do_pushlevel ();

  cond = expand_cond (SWITCH_COND (t));
  if (cond == error_mark_node)
    /* The code is in error, but we don't want expand_end_case to
       crash.  */
    cond = boolean_false_node;

  emit_line_note (input_filename, input_line);
  expand_start_case (1, cond, TREE_TYPE (cond), "switch statement");
  expand_stmt (expand_unreachable_stmt (SWITCH_BODY (t), warn_notreached));
  expand_end_case_type (cond, SWITCH_TYPE (t));
}

/* Create a CASE_LABEL tree node and return it.  */

tree
build_case_label (tree low_value, tree high_value, tree label_decl)
{
  return build_stmt (CASE_LABEL, low_value, high_value, label_decl);
}


/* Generate the RTL for a CASE_LABEL.  */

void
genrtl_case_label (tree case_label)
{
  tree duplicate;
  tree cleanup;

  cleanup = last_cleanup_this_contour ();
  if (cleanup)
    {
      static int explained = 0;
      warning ("destructor needed for `%#D'", (TREE_PURPOSE (cleanup)));
      warning ("where case label appears here");
      if (!explained)
	{
	  warning ("(enclose actions of previous case statements requiring destructors in their own scope.)");
	  explained = 1;
	}
    }

  add_case_node (CASE_LOW (case_label), CASE_HIGH (case_label),
		 CASE_LABEL_DECL (case_label), &duplicate);
}

/* Generate the RTL for T, which is a COMPOUND_STMT.  */

void
genrtl_compound_stmt (tree t)
{
#ifdef ENABLE_CHECKING
  struct nesting *n = current_nesting_level ();
#endif

  expand_stmt (COMPOUND_BODY (t));

#ifdef ENABLE_CHECKING
  /* Make sure that we've pushed and popped the same number of levels.  */
  if (!COMPOUND_STMT_NO_SCOPE (t) && n != current_nesting_level ())
    abort ();
#endif
}

/* Generate the RTL for an ASM_STMT.  */

void
genrtl_asm_stmt (tree cv_qualifier, tree string, tree output_operands,
		 tree input_operands, tree clobbers, int asm_input_p)
{
  if (cv_qualifier != NULL_TREE
      && cv_qualifier != ridpointers[(int) RID_VOLATILE])
    {
      warning ("%s qualifier ignored on asm",
	       IDENTIFIER_POINTER (cv_qualifier));
      cv_qualifier = NULL_TREE;
    }

  emit_line_note (input_filename, input_line);
  if (asm_input_p)
    expand_asm (string, cv_qualifier != NULL_TREE);
  else
    c_expand_asm_operands (string, output_operands, input_operands,
			   clobbers, cv_qualifier != NULL_TREE,
			   input_filename, input_line);
}

/* Generate the RTL for a CLEANUP_STMT.  */

void
genrtl_cleanup_stmt (tree t)
{
  tree decl = CLEANUP_DECL (t);
  if (!decl || (DECL_SIZE (decl) && TREE_TYPE (decl) != error_mark_node))
    expand_decl_cleanup_eh (decl, CLEANUP_EXPR (t), CLEANUP_EH_ONLY (t));
}

/* We're about to expand T, a statement.  Set up appropriate context
   for the substitution.  */

void
prep_stmt (tree t)
{
  if (!STMT_LINENO_FOR_FN_P (t))
    input_line = STMT_LINENO (t);
  current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
}

/* Generate the RTL for the statement T, its substatements, and any
   other statements at its nesting level.  */

void
expand_stmt (tree t)
{
  while (t && t != error_mark_node)
    {
      int saved_stmts_are_full_exprs_p;

      /* Set up context appropriately for handling this statement.  */
      saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
      prep_stmt (t);

      switch (TREE_CODE (t))
	{
	case FILE_STMT:
	  input_filename = FILE_STMT_FILENAME (t);
	  break;

	case RETURN_STMT:
	  genrtl_return_stmt (t);
	  t = expand_unreachable_stmt (TREE_CHAIN (t), warn_notreached);
	  goto process_t;

	case EXPR_STMT:
	  genrtl_expr_stmt_value (EXPR_STMT_EXPR (t), TREE_ADDRESSABLE (t),
				  TREE_CHAIN (t) == NULL
				  || (TREE_CODE (TREE_CHAIN (t)) == SCOPE_STMT
				      && TREE_CHAIN (TREE_CHAIN (t)) == NULL));
	  break;

	case DECL_STMT:
	  genrtl_decl_stmt (t);
	  break;

	case FOR_STMT:
	  genrtl_for_stmt (t);
	  break;

	case WHILE_STMT:
	  genrtl_while_stmt (t);
	  break;

	case DO_STMT:
	  genrtl_do_stmt (t);
	  break;

	case IF_STMT:
	  genrtl_if_stmt (t);
	  break;

	case COMPOUND_STMT:
	  genrtl_compound_stmt (t);
	  break;

	case BREAK_STMT:
	  genrtl_break_stmt ();
	  t = expand_unreachable_stmt (TREE_CHAIN (t), warn_notreached);
	  goto process_t;

	case CONTINUE_STMT:
	  genrtl_continue_stmt ();
	  t = expand_unreachable_stmt (TREE_CHAIN (t), warn_notreached);
	  goto process_t;

	case SWITCH_STMT:
	  genrtl_switch_stmt (t);
	  break;

	case CASE_LABEL:
	  genrtl_case_label (t);
	  break;

	case LABEL_STMT:
	  expand_label (LABEL_STMT_LABEL (t));
	  break;

	case GOTO_STMT:
	  /* Emit information for branch prediction.  */
	  if (!GOTO_FAKE_P (t)
	      && TREE_CODE (GOTO_DESTINATION (t)) == LABEL_DECL
	      && flag_guess_branch_prob)
	    {
	      rtx note = emit_note (NOTE_INSN_PREDICTION);

	      NOTE_PREDICTION (note) = NOTE_PREDICT (PRED_GOTO, NOT_TAKEN);
	    }
	  genrtl_goto_stmt (GOTO_DESTINATION (t));
	  t = expand_unreachable_stmt (TREE_CHAIN (t), warn_notreached);
	  goto process_t;

	case ASM_STMT:
	  genrtl_asm_stmt (ASM_CV_QUAL (t), ASM_STRING (t),
			   ASM_OUTPUTS (t), ASM_INPUTS (t),
			   ASM_CLOBBERS (t), ASM_INPUT_P (t));
	  break;

	case SCOPE_STMT:
	  genrtl_scope_stmt (t);
	  break;

	case CLEANUP_STMT:
	  genrtl_cleanup_stmt (t);
	  break;

	default:
	  if (lang_expand_stmt)
	    (*lang_expand_stmt) (t);
	  else
	    abort ();
	  break;
	}

      /* Go on to the next statement in this scope.  */
      t = TREE_CHAIN (t);

    process_t:
      /* Restore saved state.  */
      current_stmt_tree ()->stmts_are_full_exprs_p
	= saved_stmts_are_full_exprs_p;
    }
}

/* If *TP is a potentially reachable label, return nonzero.  */

static tree
find_reachable_label_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
			void *data ATTRIBUTE_UNUSED)
{
  switch (TREE_CODE (*tp))
    {
    case LABEL_STMT:
    case CASE_LABEL:
      return *tp;

    default:
      break;
    }
  return NULL_TREE;
}

/* Determine whether expression EXP contains a potentially
   reachable label.  */
static tree
find_reachable_label (tree exp)
{
  location_t saved_loc = input_location;
  tree ret = walk_tree (&exp, find_reachable_label_1, NULL, NULL);
  input_location = saved_loc;
  return ret;
}

/* Expand an unreachable if statement, T.  This function returns
   true if the IF_STMT contains a potentially reachable code_label.  */
static bool
expand_unreachable_if_stmt (tree t)
{
  tree n;

  if (find_reachable_label (IF_COND (t)) != NULL_TREE)
    {
      genrtl_if_stmt (t);
      return true;
    }

  if (THEN_CLAUSE (t) && ELSE_CLAUSE (t))
    {
      n = expand_unreachable_stmt (THEN_CLAUSE (t), 0);

      if (n != NULL_TREE)
	{
	  rtx label;
	  expand_stmt (n);
	  label = gen_label_rtx ();
	  emit_jump (label);
	  expand_stmt (expand_unreachable_stmt (ELSE_CLAUSE (t), 0));
	  emit_label (label);
	  return true;
	}
      else
	n = expand_unreachable_stmt (ELSE_CLAUSE (t), 0);
    }
  else if (THEN_CLAUSE (t))
    n = expand_unreachable_stmt (THEN_CLAUSE (t), 0);
  else if (ELSE_CLAUSE (t))
    n = expand_unreachable_stmt (ELSE_CLAUSE (t), 0);
  else
    n = NULL_TREE;

  expand_stmt (n);

  return n != NULL_TREE;
}

/* Expand an unreachable statement list.  This function skips all
   statements preceding the first potentially reachable label and
   then returns the label (or, in same cases, the statement after
   one containing the label).  */
static tree
expand_unreachable_stmt (tree t, int warn)
{
  int saved;

  while (t && t != error_mark_node)
    {
      if (warn)
	switch (TREE_CODE (t))
	  {
	  case BREAK_STMT:
	  case CONTINUE_STMT:
	  case EXPR_STMT:
	  case GOTO_STMT:
	  case IF_STMT:
	  case RETURN_STMT:
	    if (!STMT_LINENO_FOR_FN_P (t))
	      input_line = STMT_LINENO (t);
	    warning("will never be executed");
	    warn = false;
	    break;

	  default:
	    break;
	  }

      switch (TREE_CODE (t))
	{
	case GOTO_STMT:
	case CONTINUE_STMT:
	case BREAK_STMT:
	  break;

	case FILE_STMT:
	  input_filename = FILE_STMT_FILENAME (t);
	  break;

	case RETURN_STMT:
	  if (find_reachable_label (RETURN_STMT_EXPR (t)) != NULL_TREE)
	    return t;
	  break;

	case EXPR_STMT:
	  if (find_reachable_label (EXPR_STMT_EXPR (t)) != NULL_TREE)
	    return t;
	  break;

	case IF_STMT:
	  if (expand_unreachable_if_stmt (t))
	    return TREE_CHAIN (t);
	  break;

	case WHILE_STMT:
	  /* If the start of a while statement is unreachable, there is
	     no need to rotate the loop, instead the WHILE_STMT can be
	     expanded like a DO_STMT.  */
	  genrtl_do_stmt_1 (WHILE_COND (t), WHILE_BODY (t));
	  return TREE_CHAIN (t);

	case COMPOUND_STMT:
	  {
	    tree n;
	    n = expand_unreachable_stmt (COMPOUND_BODY (t), warn);
	    if (n != NULL_TREE)
	      {
		expand_stmt (n);
		return TREE_CHAIN (t);
	      }
	    warn = false;
	    break;
	  }

	case SCOPE_STMT:
	  saved = stmts_are_full_exprs_p ();
	  prep_stmt (t);
	  genrtl_scope_stmt (t);
	  current_stmt_tree ()->stmts_are_full_exprs_p = saved;
	  break;

	default:
	  return t;
	}
      t = TREE_CHAIN (t);
    }
  return NULL_TREE;
}