PR rtl-optimization/82913
[official-gcc.git] / gcc / cp / expr.c
blob23e30cf789c08d00152ae93f2181b45d061af20f
1 /* Convert language-specific tree expression to rtl instructions,
2 for GNU compiler.
3 Copyright (C) 1988-2017 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
27 /* Expand C++-specific constants. Currently, this means PTRMEM_CST. */
29 tree
30 cplus_expand_constant (tree cst)
32 switch (TREE_CODE (cst))
34 case PTRMEM_CST:
36 tree type = TREE_TYPE (cst);
37 tree member;
39 /* Find the member. */
40 member = PTRMEM_CST_MEMBER (cst);
42 /* We can't lower this until the class is complete. */
43 if (!COMPLETE_TYPE_P (DECL_CONTEXT (member)))
44 return cst;
46 if (TREE_CODE (member) == FIELD_DECL)
48 /* Find the offset for the field. */
49 cst = byte_position (member);
50 while (!same_type_p (DECL_CONTEXT (member),
51 TYPE_PTRMEM_CLASS_TYPE (type)))
53 /* The MEMBER must have been nestled within an
54 anonymous aggregate contained in TYPE. Find the
55 anonymous aggregate. */
56 member = lookup_anon_field (TYPE_PTRMEM_CLASS_TYPE (type),
57 DECL_CONTEXT (member));
58 cst = size_binop (PLUS_EXPR, cst, byte_position (member));
60 cst = fold (build_nop (type, cst));
62 else
64 tree delta;
65 tree pfn;
67 expand_ptrmemfunc_cst (cst, &delta, &pfn);
68 cst = build_ptrmemfunc1 (type, delta, pfn);
71 break;
73 case CONSTRUCTOR:
75 constructor_elt *elt;
76 unsigned HOST_WIDE_INT idx;
77 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (cst), idx, elt)
78 elt->value = cplus_expand_constant (elt->value);
81 default:
82 /* There's nothing to do. */
83 break;
86 return cst;
89 /* We've seen an actual use of EXPR. Possibly replace an outer variable
90 reference inside with its constant value or a lambda capture. */
92 static tree
93 mark_use (tree expr, bool rvalue_p, bool read_p,
94 location_t loc /* = UNKNOWN_LOCATION */,
95 bool reject_builtin /* = true */)
97 #define RECUR(t) mark_use ((t), rvalue_p, read_p, loc, reject_builtin)
99 if (expr == NULL_TREE || expr == error_mark_node)
100 return expr;
102 if (reject_builtin && reject_gcc_builtin (expr, loc))
103 return error_mark_node;
105 if (read_p)
106 mark_exp_read (expr);
108 tree oexpr = expr;
109 bool recurse_op[3] = { false, false, false };
110 switch (TREE_CODE (expr))
112 case VAR_DECL:
113 case PARM_DECL:
114 if (outer_automatic_var_p (expr)
115 && decl_constant_var_p (expr))
117 if (rvalue_p)
119 tree t = maybe_constant_value (expr);
120 if (TREE_CONSTANT (t))
122 expr = t;
123 break;
126 expr = process_outer_var_ref (expr, tf_warning_or_error, true);
127 if (!(TREE_TYPE (oexpr)
128 && TREE_CODE (TREE_TYPE (oexpr)) == REFERENCE_TYPE))
129 expr = convert_from_reference (expr);
131 break;
132 case COMPONENT_REF:
133 case NON_DEPENDENT_EXPR:
134 recurse_op[0] = true;
135 break;
136 case COMPOUND_EXPR:
137 recurse_op[1] = true;
138 break;
139 case COND_EXPR:
140 recurse_op[2] = true;
141 if (TREE_OPERAND (expr, 1))
142 recurse_op[1] = true;
143 break;
144 case INDIRECT_REF:
145 if (REFERENCE_REF_P (expr))
147 /* Try to look through the reference. */
148 tree ref = TREE_OPERAND (expr, 0);
149 tree r = mark_rvalue_use (ref, loc, reject_builtin);
150 if (r != ref)
151 expr = convert_from_reference (r);
153 break;
154 default:
155 break;
158 for (int i = 0; i < 3; ++i)
159 if (recurse_op[i])
161 tree op = TREE_OPERAND (expr, i);
162 op = RECUR (op);
163 if (op == error_mark_node)
164 return error_mark_node;
165 TREE_OPERAND (expr, i) = op;
168 return expr;
169 #undef RECUR
172 /* Called whenever the expression EXPR is used in an rvalue context.
173 When REJECT_BUILTIN is true the expression is checked to make sure
174 it doesn't make it possible to obtain the address of a GCC built-in
175 function with no library fallback (or any of its bits, such as in
176 a conversion to bool). */
178 tree
179 mark_rvalue_use (tree e,
180 location_t loc /* = UNKNOWN_LOCATION */,
181 bool reject_builtin /* = true */)
183 return mark_use (e, true, true, loc, reject_builtin);
186 /* Called when expr appears as a discarded-value expression. */
188 tree
189 mark_discarded_use (tree expr)
191 /* The lvalue-to-rvalue conversion (7.1) is applied if and only if the
192 expression is a glvalue of volatile-qualified type and it is one of the
193 following:
194 * ( expression ), where expression is one of these expressions,
195 * id-expression (8.1.4),
196 * subscripting (8.2.1),
197 * class member access (8.2.5),
198 * indirection (8.3.1),
199 * pointer-to-member operation (8.5),
200 * conditional expression (8.16) where both the second and the third
201 operands are one of these expressions, or
202 * comma expression (8.19) where the right operand is one of these
203 expressions. */
204 if (expr == NULL_TREE)
205 return expr;
207 switch (TREE_CODE (expr))
209 case COND_EXPR:
210 TREE_OPERAND (expr, 2) = mark_discarded_use (TREE_OPERAND (expr, 2));
211 gcc_fallthrough ();
212 case COMPOUND_EXPR:
213 TREE_OPERAND (expr, 1) = mark_discarded_use (TREE_OPERAND (expr, 1));
214 return expr;
216 case COMPONENT_REF:
217 case ARRAY_REF:
218 case INDIRECT_REF:
219 case MEMBER_REF:
220 break;
221 default:
222 if (DECL_P (expr))
223 break;
224 else
225 return expr;
228 /* Like mark_rvalue_use, but don't reject built-ins. */
229 return mark_use (expr, true, true, input_location, false);
232 /* Called whenever an expression is used in an lvalue context. */
234 tree
235 mark_lvalue_use (tree expr)
237 return mark_use (expr, false, true, input_location, false);
240 /* As above, but don't consider this use a read. */
242 tree
243 mark_lvalue_use_nonread (tree expr)
245 return mark_use (expr, false, false, input_location, false);
248 /* Called whenever an expression is used in a type use context. */
250 tree
251 mark_type_use (tree expr)
253 mark_exp_read (expr);
254 return expr;
257 /* Mark EXP as read, not just set, for set but not used -Wunused
258 warning purposes. */
260 void
261 mark_exp_read (tree exp)
263 if (exp == NULL)
264 return;
266 switch (TREE_CODE (exp))
268 case VAR_DECL:
269 if (DECL_DECOMPOSITION_P (exp))
270 mark_exp_read (DECL_DECOMP_BASE (exp));
271 gcc_fallthrough ();
272 case PARM_DECL:
273 DECL_READ_P (exp) = 1;
274 break;
275 case ARRAY_REF:
276 case COMPONENT_REF:
277 case MODIFY_EXPR:
278 case REALPART_EXPR:
279 case IMAGPART_EXPR:
280 CASE_CONVERT:
281 case ADDR_EXPR:
282 case INDIRECT_REF:
283 case FLOAT_EXPR:
284 case NON_DEPENDENT_EXPR:
285 case VIEW_CONVERT_EXPR:
286 mark_exp_read (TREE_OPERAND (exp, 0));
287 break;
288 case COMPOUND_EXPR:
289 mark_exp_read (TREE_OPERAND (exp, 1));
290 break;
291 case COND_EXPR:
292 if (TREE_OPERAND (exp, 1))
293 mark_exp_read (TREE_OPERAND (exp, 1));
294 if (TREE_OPERAND (exp, 2))
295 mark_exp_read (TREE_OPERAND (exp, 2));
296 break;
297 default:
298 break;