2014-09-12 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / gcc / c-family / c-semantics.c
blobf25805a79b0ff665cf1cd7c733733c90f6812cff
1 /* This file contains subroutine used by the C front-end to construct GENERIC.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 Written by Benjamin Chelf (chelf@codesourcery.com).
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "function.h"
27 #include "splay-tree.h"
28 #include "c-common.h"
29 #include "flags.h"
30 #include "tree-iterator.h"
32 /* Create an empty statement tree rooted at T. */
34 tree
35 push_stmt_list (void)
37 tree t;
38 t = alloc_stmt_list ();
39 vec_safe_push (stmt_list_stack, t);
40 return t;
43 /* Finish the statement tree rooted at T. */
45 tree
46 pop_stmt_list (tree t)
48 tree u = NULL_TREE;
50 /* Pop statement lists until we reach the target level. The extra
51 nestings will be due to outstanding cleanups. */
52 while (1)
54 u = stmt_list_stack->pop ();
55 if (!stmt_list_stack->is_empty ())
57 tree x = stmt_list_stack->last ();
58 STATEMENT_LIST_HAS_LABEL (x) |= STATEMENT_LIST_HAS_LABEL (u);
60 if (t == u)
61 break;
64 gcc_assert (u != NULL_TREE);
66 /* If the statement list is completely empty, just return it. This is
67 just as good small as build_empty_stmt, with the advantage that
68 statement lists are merged when they appended to one another. So
69 using the STATEMENT_LIST avoids pathological buildup of EMPTY_STMT_P
70 statements. */
71 if (TREE_SIDE_EFFECTS (t))
73 tree_stmt_iterator i = tsi_start (t);
75 /* If the statement list contained exactly one statement, then
76 extract it immediately. */
77 if (tsi_one_before_end_p (i))
79 u = tsi_stmt (i);
80 tsi_delink (&i);
81 free_stmt_list (t);
82 t = u;
86 return t;
89 /* Build a generic statement based on the given type of node and
90 arguments. Similar to `build_nt', except that we set
91 EXPR_LOCATION to LOC. */
92 /* ??? This should be obsolete with the lineno_stmt productions
93 in the grammar. */
95 tree
96 build_stmt (location_t loc, enum tree_code code, ...)
98 tree ret;
99 int length, i;
100 va_list p;
101 bool side_effects;
103 /* This function cannot be used to construct variably-sized nodes. */
104 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
106 va_start (p, code);
108 ret = make_node (code);
109 TREE_TYPE (ret) = void_type_node;
110 length = TREE_CODE_LENGTH (code);
111 SET_EXPR_LOCATION (ret, loc);
113 /* TREE_SIDE_EFFECTS will already be set for statements with
114 implicit side effects. Here we make sure it is set for other
115 expressions by checking whether the parameters have side
116 effects. */
118 side_effects = false;
119 for (i = 0; i < length; i++)
121 tree t = va_arg (p, tree);
122 if (t && !TYPE_P (t))
123 side_effects |= TREE_SIDE_EFFECTS (t);
124 TREE_OPERAND (ret, i) = t;
127 TREE_SIDE_EFFECTS (ret) |= side_effects;
129 va_end (p);
130 return ret;
133 /* Build a REALPART_EXPR or IMAGPART_EXPR, according to CODE, from ARG. */
135 tree
136 build_real_imag_expr (location_t location, enum tree_code code, tree arg)
138 tree ret;
139 tree arg_type = TREE_TYPE (arg);
141 gcc_assert (code == REALPART_EXPR || code == IMAGPART_EXPR);
143 if (TREE_CODE (arg_type) == COMPLEX_TYPE)
145 ret = build1 (code, TREE_TYPE (TREE_TYPE (arg)), arg);
146 SET_EXPR_LOCATION (ret, location);
148 else if (INTEGRAL_TYPE_P (arg_type) || SCALAR_FLOAT_TYPE_P (arg_type))
150 ret = (code == REALPART_EXPR
151 ? arg
152 : omit_one_operand_loc (location, arg_type,
153 integer_zero_node, arg));
155 else
157 error_at (location, "wrong type argument to %s",
158 code == REALPART_EXPR ? "__real" : "__imag");
159 ret = error_mark_node;
162 return ret;