2015-08-24 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / c-family / c-semantics.c
blobcea23541d95ed33a26bf224f6f5376d4c742c5bd
1 /* This file contains subroutine used by the C front-end to construct GENERIC.
2 Copyright (C) 2000-2015 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 "alias.h"
26 #include "tree.h"
27 #include "options.h"
28 #include "hard-reg-set.h"
29 #include "function.h"
30 #include "splay-tree.h"
31 #include "c-common.h"
32 #include "flags.h"
33 #include "tree-iterator.h"
35 /* Create an empty statement tree rooted at T. */
37 tree
38 push_stmt_list (void)
40 tree t;
41 t = alloc_stmt_list ();
42 vec_safe_push (stmt_list_stack, t);
43 return t;
46 /* Finish the statement tree rooted at T. */
48 tree
49 pop_stmt_list (tree t)
51 tree u = NULL_TREE;
53 /* Pop statement lists until we reach the target level. The extra
54 nestings will be due to outstanding cleanups. */
55 while (1)
57 u = stmt_list_stack->pop ();
58 if (!stmt_list_stack->is_empty ())
60 tree x = stmt_list_stack->last ();
61 STATEMENT_LIST_HAS_LABEL (x) |= STATEMENT_LIST_HAS_LABEL (u);
63 if (t == u)
64 break;
67 gcc_assert (u != NULL_TREE);
69 /* If the statement list is completely empty, just return it. This is
70 just as good small as build_empty_stmt, with the advantage that
71 statement lists are merged when they appended to one another. So
72 using the STATEMENT_LIST avoids pathological buildup of EMPTY_STMT_P
73 statements. */
74 if (TREE_SIDE_EFFECTS (t))
76 tree_stmt_iterator i = tsi_start (t);
78 /* If the statement list contained exactly one statement, then
79 extract it immediately. */
80 if (tsi_one_before_end_p (i))
82 u = tsi_stmt (i);
83 tsi_delink (&i);
84 free_stmt_list (t);
85 t = u;
89 return t;
92 /* Build a generic statement based on the given type of node and
93 arguments. Similar to `build_nt', except that we set
94 EXPR_LOCATION to LOC. */
95 /* ??? This should be obsolete with the lineno_stmt productions
96 in the grammar. */
98 tree
99 build_stmt (location_t loc, enum tree_code code, ...)
101 tree ret;
102 int length, i;
103 va_list p;
104 bool side_effects;
106 /* This function cannot be used to construct variably-sized nodes. */
107 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
109 va_start (p, code);
111 ret = make_node (code);
112 TREE_TYPE (ret) = void_type_node;
113 length = TREE_CODE_LENGTH (code);
114 SET_EXPR_LOCATION (ret, loc);
116 /* TREE_SIDE_EFFECTS will already be set for statements with
117 implicit side effects. Here we make sure it is set for other
118 expressions by checking whether the parameters have side
119 effects. */
121 side_effects = false;
122 for (i = 0; i < length; i++)
124 tree t = va_arg (p, tree);
125 if (t && !TYPE_P (t))
126 side_effects |= TREE_SIDE_EFFECTS (t);
127 TREE_OPERAND (ret, i) = t;
130 TREE_SIDE_EFFECTS (ret) |= side_effects;
132 va_end (p);
133 return ret;
136 /* Build a REALPART_EXPR or IMAGPART_EXPR, according to CODE, from ARG. */
138 tree
139 build_real_imag_expr (location_t location, enum tree_code code, tree arg)
141 tree ret;
142 tree arg_type = TREE_TYPE (arg);
144 gcc_assert (code == REALPART_EXPR || code == IMAGPART_EXPR);
146 if (TREE_CODE (arg_type) == COMPLEX_TYPE)
148 ret = build1 (code, TREE_TYPE (TREE_TYPE (arg)), arg);
149 SET_EXPR_LOCATION (ret, location);
151 else if (INTEGRAL_TYPE_P (arg_type) || SCALAR_FLOAT_TYPE_P (arg_type))
153 ret = (code == REALPART_EXPR
154 ? arg
155 : omit_one_operand_loc (location, arg_type,
156 integer_zero_node, arg));
158 else
160 error_at (location, "wrong type argument to %s",
161 code == REALPART_EXPR ? "__real" : "__imag");
162 ret = error_mark_node;
165 return ret;