re PR middle-end/42834 (memcpy folding overeager)
[gcc.git] / gcc / c-family / c-semantics.c
1 /* This file contains subroutine used by the C front-end to construct GENERIC.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008
3 Free Software Foundation, Inc.
4 Written by Benjamin Chelf (chelf@codesourcery.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "function.h"
28 #include "splay-tree.h"
29 #include "c-common.h"
30 /* In order for the format checking to accept the C frontend
31 diagnostic framework extensions, you must define this token before
32 including toplev.h. */
33 #define GCC_DIAG_STYLE __gcc_cdiag__
34 #include "toplev.h"
35 #include "flags.h"
36 #include "output.h"
37 #include "tree-iterator.h"
38
39 /* Create an empty statement tree rooted at T. */
40
41 tree
42 push_stmt_list (void)
43 {
44 tree t;
45 t = alloc_stmt_list ();
46 TREE_CHAIN (t) = cur_stmt_list;
47 cur_stmt_list = t;
48 return t;
49 }
50
51 /* Finish the statement tree rooted at T. */
52
53 tree
54 pop_stmt_list (tree t)
55 {
56 tree u = cur_stmt_list, chain;
57
58 /* Pop statement lists until we reach the target level. The extra
59 nestings will be due to outstanding cleanups. */
60 while (1)
61 {
62 chain = TREE_CHAIN (u);
63 TREE_CHAIN (u) = NULL_TREE;
64 if (chain)
65 STATEMENT_LIST_HAS_LABEL (chain) |= STATEMENT_LIST_HAS_LABEL (u);
66 if (t == u)
67 break;
68 u = chain;
69 }
70 cur_stmt_list = chain;
71
72 /* If the statement list is completely empty, just return it. This is
73 just as good small as build_empty_stmt, with the advantage that
74 statement lists are merged when they appended to one another. So
75 using the STATEMENT_LIST avoids pathological buildup of EMPTY_STMT_P
76 statements. */
77 if (TREE_SIDE_EFFECTS (t))
78 {
79 tree_stmt_iterator i = tsi_start (t);
80
81 /* If the statement list contained exactly one statement, then
82 extract it immediately. */
83 if (tsi_one_before_end_p (i))
84 {
85 u = tsi_stmt (i);
86 tsi_delink (&i);
87 free_stmt_list (t);
88 t = u;
89 }
90 }
91
92 return t;
93 }
94
95 /* Build a generic statement based on the given type of node and
96 arguments. Similar to `build_nt', except that we set
97 EXPR_LOCATION to LOC. */
98 /* ??? This should be obsolete with the lineno_stmt productions
99 in the grammar. */
100
101 tree
102 build_stmt (location_t loc, enum tree_code code, ...)
103 {
104 tree ret;
105 int length, i;
106 va_list p;
107 bool side_effects;
108
109 /* This function cannot be used to construct variably-sized nodes. */
110 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
111
112 va_start (p, code);
113
114 ret = make_node (code);
115 TREE_TYPE (ret) = void_type_node;
116 length = TREE_CODE_LENGTH (code);
117 SET_EXPR_LOCATION (ret, loc);
118
119 /* TREE_SIDE_EFFECTS will already be set for statements with
120 implicit side effects. Here we make sure it is set for other
121 expressions by checking whether the parameters have side
122 effects. */
123
124 side_effects = false;
125 for (i = 0; i < length; i++)
126 {
127 tree t = va_arg (p, tree);
128 if (t && !TYPE_P (t))
129 side_effects |= TREE_SIDE_EFFECTS (t);
130 TREE_OPERAND (ret, i) = t;
131 }
132
133 TREE_SIDE_EFFECTS (ret) |= side_effects;
134
135 va_end (p);
136 return ret;
137 }
138
139 /* Create a CASE_LABEL_EXPR tree node and return it. */
140
141 tree
142 build_case_label (location_t loc,
143 tree low_value, tree high_value, tree label_decl)
144 {
145 return build_stmt (loc, CASE_LABEL_EXPR, low_value, high_value, label_decl);
146 }