xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/c-family/c-semantics.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
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).
4 
5 This file is part of GCC.
6 
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.
11 
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.
16 
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/>.  */
20 
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "options.h"
33 #include "wide-int.h"
34 #include "inchash.h"
35 #include "tree.h"
36 #include "hard-reg-set.h"
37 #include "function.h"
38 #include "splay-tree.h"
39 #include "c-common.h"
40 #include "flags.h"
41 #include "tree-iterator.h"
42 
43 /* Create an empty statement tree rooted at T.  */
44 
45 tree
46 push_stmt_list (void)
47 {
48   tree t;
49   t = alloc_stmt_list ();
50   vec_safe_push (stmt_list_stack, t);
51   return t;
52 }
53 
54 /* Finish the statement tree rooted at T.  */
55 
56 tree
57 pop_stmt_list (tree t)
58 {
59   tree u = NULL_TREE;
60 
61   /* Pop statement lists until we reach the target level.  The extra
62      nestings will be due to outstanding cleanups.  */
63   while (1)
64     {
65       u = stmt_list_stack->pop ();
66       if (!stmt_list_stack->is_empty ())
67 	{
68 	  tree x = stmt_list_stack->last ();
69 	  STATEMENT_LIST_HAS_LABEL (x) |= STATEMENT_LIST_HAS_LABEL (u);
70 	}
71       if (t == u)
72 	break;
73     }
74 
75   gcc_assert (u != NULL_TREE);
76 
77   /* If the statement list is completely empty, just return it.  This is
78      just as good small as build_empty_stmt, with the advantage that
79      statement lists are merged when they appended to one another.  So
80      using the STATEMENT_LIST avoids pathological buildup of EMPTY_STMT_P
81      statements.  */
82   if (TREE_SIDE_EFFECTS (t))
83     {
84       tree_stmt_iterator i = tsi_start (t);
85 
86       /* If the statement list contained exactly one statement, then
87 	 extract it immediately.  */
88       if (tsi_one_before_end_p (i))
89 	{
90 	  u = tsi_stmt (i);
91 	  tsi_delink (&i);
92 	  free_stmt_list (t);
93 	  t = u;
94 	}
95     }
96 
97   return t;
98 }
99 
100 /* Build a generic statement based on the given type of node and
101    arguments. Similar to `build_nt', except that we set
102    EXPR_LOCATION to LOC. */
103 /* ??? This should be obsolete with the lineno_stmt productions
104    in the grammar.  */
105 
106 tree
107 build_stmt (location_t loc, enum tree_code code, ...)
108 {
109   tree ret;
110   int length, i;
111   va_list p;
112   bool side_effects;
113 
114   /* This function cannot be used to construct variably-sized nodes.  */
115   gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
116 
117   va_start (p, code);
118 
119   ret = make_node (code);
120   TREE_TYPE (ret) = void_type_node;
121   length = TREE_CODE_LENGTH (code);
122   SET_EXPR_LOCATION (ret, loc);
123 
124   /* TREE_SIDE_EFFECTS will already be set for statements with
125      implicit side effects.  Here we make sure it is set for other
126      expressions by checking whether the parameters have side
127      effects.  */
128 
129   side_effects = false;
130   for (i = 0; i < length; i++)
131     {
132       tree t = va_arg (p, tree);
133       if (t && !TYPE_P (t))
134 	side_effects |= TREE_SIDE_EFFECTS (t);
135       TREE_OPERAND (ret, i) = t;
136     }
137 
138   TREE_SIDE_EFFECTS (ret) |= side_effects;
139 
140   va_end (p);
141   return ret;
142 }
143 
144 /* Build a REALPART_EXPR or IMAGPART_EXPR, according to CODE, from ARG.  */
145 
146 tree
147 build_real_imag_expr (location_t location, enum tree_code code, tree arg)
148 {
149   tree ret;
150   tree arg_type = TREE_TYPE (arg);
151 
152   gcc_assert (code == REALPART_EXPR || code == IMAGPART_EXPR);
153 
154   if (TREE_CODE (arg_type) == COMPLEX_TYPE)
155     {
156       ret = build1 (code, TREE_TYPE (TREE_TYPE (arg)), arg);
157       SET_EXPR_LOCATION (ret, location);
158     }
159   else if (INTEGRAL_TYPE_P (arg_type) || SCALAR_FLOAT_TYPE_P (arg_type))
160     {
161       ret = (code == REALPART_EXPR
162 	     ? arg
163 	     : omit_one_operand_loc (location, arg_type,
164 				     integer_zero_node, arg));
165     }
166   else
167     {
168       error_at (location, "wrong type argument to %s",
169 		code == REALPART_EXPR ? "__real" : "__imag");
170       ret = error_mark_node;
171     }
172 
173   return ret;
174 }
175