2011-03-31 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / c-family / c-semantics.c
blob0f9f51e061de9bd9b9c42c806566849576bc8e14
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, 2009, 2010
3 Free Software Foundation, Inc.
4 Written by Benjamin Chelf (chelf@codesourcery.com).
6 This file is part of GCC.
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.
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.
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/>. */
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 #include "flags.h"
31 #include "output.h"
32 #include "tree-iterator.h"
34 /* Create an empty statement tree rooted at T. */
36 tree
37 push_stmt_list (void)
39 tree t;
40 t = alloc_stmt_list ();
41 TREE_CHAIN (t) = cur_stmt_list;
42 cur_stmt_list = t;
43 return t;
46 /* Finish the statement tree rooted at T. */
48 tree
49 pop_stmt_list (tree t)
51 tree u = cur_stmt_list, chain;
53 /* Pop statement lists until we reach the target level. The extra
54 nestings will be due to outstanding cleanups. */
55 while (1)
57 chain = TREE_CHAIN (u);
58 TREE_CHAIN (u) = NULL_TREE;
59 if (chain)
60 STATEMENT_LIST_HAS_LABEL (chain) |= STATEMENT_LIST_HAS_LABEL (u);
61 if (t == u)
62 break;
63 u = chain;
65 cur_stmt_list = chain;
67 /* If the statement list is completely empty, just return it. This is
68 just as good small as build_empty_stmt, with the advantage that
69 statement lists are merged when they appended to one another. So
70 using the STATEMENT_LIST avoids pathological buildup of EMPTY_STMT_P
71 statements. */
72 if (TREE_SIDE_EFFECTS (t))
74 tree_stmt_iterator i = tsi_start (t);
76 /* If the statement list contained exactly one statement, then
77 extract it immediately. */
78 if (tsi_one_before_end_p (i))
80 u = tsi_stmt (i);
81 tsi_delink (&i);
82 free_stmt_list (t);
83 t = u;
87 return t;
90 /* Build a generic statement based on the given type of node and
91 arguments. Similar to `build_nt', except that we set
92 EXPR_LOCATION to LOC. */
93 /* ??? This should be obsolete with the lineno_stmt productions
94 in the grammar. */
96 tree
97 build_stmt (location_t loc, enum tree_code code, ...)
99 tree ret;
100 int length, i;
101 va_list p;
102 bool side_effects;
104 /* This function cannot be used to construct variably-sized nodes. */
105 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
107 va_start (p, code);
109 ret = make_node (code);
110 TREE_TYPE (ret) = void_type_node;
111 length = TREE_CODE_LENGTH (code);
112 SET_EXPR_LOCATION (ret, loc);
114 /* TREE_SIDE_EFFECTS will already be set for statements with
115 implicit side effects. Here we make sure it is set for other
116 expressions by checking whether the parameters have side
117 effects. */
119 side_effects = false;
120 for (i = 0; i < length; i++)
122 tree t = va_arg (p, tree);
123 if (t && !TYPE_P (t))
124 side_effects |= TREE_SIDE_EFFECTS (t);
125 TREE_OPERAND (ret, i) = t;
128 TREE_SIDE_EFFECTS (ret) |= side_effects;
130 va_end (p);
131 return ret;
134 /* Create a CASE_LABEL_EXPR tree node and return it. */
136 tree
137 build_case_label (location_t loc,
138 tree low_value, tree high_value, tree label_decl)
140 return build_stmt (loc, CASE_LABEL_EXPR, low_value, high_value, label_decl);
143 /* Build a REALPART_EXPR or IMAGPART_EXPR, according to CODE, from ARG. */
145 tree
146 build_real_imag_expr (location_t location, enum tree_code code, tree arg)
148 tree ret;
149 tree arg_type = TREE_TYPE (arg);
151 gcc_assert (code == REALPART_EXPR || code == IMAGPART_EXPR);
153 if (TREE_CODE (arg_type) == COMPLEX_TYPE)
155 ret = build1 (code, TREE_TYPE (TREE_TYPE (arg)), arg);
156 SET_EXPR_LOCATION (ret, location);
158 else if (INTEGRAL_TYPE_P (arg_type) || SCALAR_FLOAT_TYPE_P (arg_type))
160 ret = (code == REALPART_EXPR
161 ? arg
162 : omit_one_operand_loc (location, arg_type,
163 integer_zero_node, arg));
165 else
167 error_at (location, "wrong type argument to %s",
168 code == REALPART_EXPR ? "__real" : "__imag");
169 ret = error_mark_node;
172 return ret;