2015-06-25 Zhouyi Zhou <yizhouzhou@ict.ac.cn>
[official-gcc.git] / gcc / c-family / c-semantics.c
blobc34026fb39c1aaed8ce64a39252504e20e747a83
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 "symtab.h"
27 #include "options.h"
28 #include "tree.h"
29 #include "hard-reg-set.h"
30 #include "function.h"
31 #include "splay-tree.h"
32 #include "c-common.h"
33 #include "flags.h"
34 #include "tree-iterator.h"
36 /* Create an empty statement tree rooted at T. */
38 tree
39 push_stmt_list (void)
41 tree t;
42 t = alloc_stmt_list ();
43 vec_safe_push (stmt_list_stack, t);
44 return t;
47 /* Finish the statement tree rooted at T. */
49 tree
50 pop_stmt_list (tree t)
52 tree u = NULL_TREE;
54 /* Pop statement lists until we reach the target level. The extra
55 nestings will be due to outstanding cleanups. */
56 while (1)
58 u = stmt_list_stack->pop ();
59 if (!stmt_list_stack->is_empty ())
61 tree x = stmt_list_stack->last ();
62 STATEMENT_LIST_HAS_LABEL (x) |= STATEMENT_LIST_HAS_LABEL (u);
64 if (t == u)
65 break;
68 gcc_assert (u != NULL_TREE);
70 /* If the statement list is completely empty, just return it. This is
71 just as good small as build_empty_stmt, with the advantage that
72 statement lists are merged when they appended to one another. So
73 using the STATEMENT_LIST avoids pathological buildup of EMPTY_STMT_P
74 statements. */
75 if (TREE_SIDE_EFFECTS (t))
77 tree_stmt_iterator i = tsi_start (t);
79 /* If the statement list contained exactly one statement, then
80 extract it immediately. */
81 if (tsi_one_before_end_p (i))
83 u = tsi_stmt (i);
84 tsi_delink (&i);
85 free_stmt_list (t);
86 t = u;
90 return t;
93 /* Build a generic statement based on the given type of node and
94 arguments. Similar to `build_nt', except that we set
95 EXPR_LOCATION to LOC. */
96 /* ??? This should be obsolete with the lineno_stmt productions
97 in the grammar. */
99 tree
100 build_stmt (location_t loc, enum tree_code code, ...)
102 tree ret;
103 int length, i;
104 va_list p;
105 bool side_effects;
107 /* This function cannot be used to construct variably-sized nodes. */
108 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
110 va_start (p, code);
112 ret = make_node (code);
113 TREE_TYPE (ret) = void_type_node;
114 length = TREE_CODE_LENGTH (code);
115 SET_EXPR_LOCATION (ret, loc);
117 /* TREE_SIDE_EFFECTS will already be set for statements with
118 implicit side effects. Here we make sure it is set for other
119 expressions by checking whether the parameters have side
120 effects. */
122 side_effects = false;
123 for (i = 0; i < length; i++)
125 tree t = va_arg (p, tree);
126 if (t && !TYPE_P (t))
127 side_effects |= TREE_SIDE_EFFECTS (t);
128 TREE_OPERAND (ret, i) = t;
131 TREE_SIDE_EFFECTS (ret) |= side_effects;
133 va_end (p);
134 return ret;
137 /* Build a REALPART_EXPR or IMAGPART_EXPR, according to CODE, from ARG. */
139 tree
140 build_real_imag_expr (location_t location, enum tree_code code, tree arg)
142 tree ret;
143 tree arg_type = TREE_TYPE (arg);
145 gcc_assert (code == REALPART_EXPR || code == IMAGPART_EXPR);
147 if (TREE_CODE (arg_type) == COMPLEX_TYPE)
149 ret = build1 (code, TREE_TYPE (TREE_TYPE (arg)), arg);
150 SET_EXPR_LOCATION (ret, location);
152 else if (INTEGRAL_TYPE_P (arg_type) || SCALAR_FLOAT_TYPE_P (arg_type))
154 ret = (code == REALPART_EXPR
155 ? arg
156 : omit_one_operand_loc (location, arg_type,
157 integer_zero_node, arg));
159 else
161 error_at (location, "wrong type argument to %s",
162 code == REALPART_EXPR ? "__real" : "__imag");
163 ret = error_mark_node;
166 return ret;