]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/expr.c
2019-02-27 Bernd Edlinger <bernd.edlinger@hotmail.de>
[thirdparty/gcc.git] / gcc / cp / expr.c
CommitLineData
471086d6 1/* Convert language-specific tree expression to rtl instructions,
2 for GNU compiler.
fbd26352 3 Copyright (C) 1988-2019 Free Software Foundation, Inc.
471086d6 4
6f0d25a6 5This file is part of GCC.
471086d6 6
6f0d25a6 7GCC is free software; you can redistribute it and/or modify
471086d6 8it under the terms of the GNU General Public License as published by
aa139c3f 9the Free Software Foundation; either version 3, or (at your option)
471086d6 10any later version.
11
6f0d25a6 12GCC is distributed in the hope that it will be useful,
471086d6 13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
aa139c3f 18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
471086d6 20
21
22#include "config.h"
b3ef7553 23#include "system.h"
805e22b2 24#include "coretypes.h"
471086d6 25#include "cp-tree.h"
26
1b49e7ef 27/* Expand C++-specific constants. Currently, this means PTRMEM_CST. */
6ae2678b 28
a17aefa2 29tree
112233a0 30cplus_expand_constant (tree cst)
6ae2678b 31{
32 switch (TREE_CODE (cst))
33 {
34 case PTRMEM_CST:
35 {
36 tree type = TREE_TYPE (cst);
37 tree member;
9031d10b 38
6ae2678b 39 /* Find the member. */
40 member = PTRMEM_CST_MEMBER (cst);
41
88af6243 42 /* We can't lower this until the class is complete. */
43 if (!COMPLETE_TYPE_P (DECL_CONTEXT (member)))
44 return cst;
45
9031d10b 46 if (TREE_CODE (member) == FIELD_DECL)
962f99f9 47 {
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)))
52 {
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));
59 }
60 cst = fold (build_nop (type, cst));
61 }
6ae2678b 62 else
63 {
d1cabdaf 64 tree delta;
65 tree pfn;
6ae2678b 66
d75b8a68 67 expand_ptrmemfunc_cst (cst, &delta, &pfn);
68 cst = build_ptrmemfunc1 (type, delta, pfn);
6ae2678b 69 }
70 }
71 break;
72
21455488 73 case CONSTRUCTOR:
74 {
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);
79 }
80
6ae2678b 81 default:
82 /* There's nothing to do. */
83 break;
84 }
85
86 return cst;
87}
fbb73d9b 88
4cedc476 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. */
91
bc97e231 92tree
4cedc476 93mark_use (tree expr, bool rvalue_p, bool read_p,
94 location_t loc /* = UNKNOWN_LOCATION */,
95 bool reject_builtin /* = true */)
96{
97#define RECUR(t) mark_use ((t), rvalue_p, read_p, loc, reject_builtin)
98
c017458d 99 if (expr == NULL_TREE || expr == error_mark_node)
100 return expr;
101
4cedc476 102 if (reject_builtin && reject_gcc_builtin (expr, loc))
103 return error_mark_node;
104
105 if (read_p)
106 mark_exp_read (expr);
107
c017458d 108 tree oexpr = expr;
4cedc476 109 bool recurse_op[3] = { false, false, false };
110 switch (TREE_CODE (expr))
111 {
112 case VAR_DECL:
c017458d 113 case PARM_DECL:
d0e2b7e7 114 if (rvalue_p && is_normal_capture_proxy (expr))
297de7bc 115 {
116 /* Look through capture by copy. */
117 tree cap = DECL_CAPTURED_VARIABLE (expr);
118 if (TREE_CODE (TREE_TYPE (cap)) == TREE_CODE (TREE_TYPE (expr))
119 && decl_constant_var_p (cap))
80fdc40f 120 {
121 tree val = RECUR (cap);
122 if (!is_capture_proxy (val))
123 {
124 tree l = current_lambda_expr ();
125 LAMBDA_EXPR_CAPTURE_OPTIMIZED (l) = true;
126 }
127 return val;
128 }
297de7bc 129 }
4cedc476 130 if (outer_automatic_var_p (expr)
131 && decl_constant_var_p (expr))
132 {
133 if (rvalue_p)
134 {
135 tree t = maybe_constant_value (expr);
136 if (TREE_CONSTANT (t))
137 {
138 expr = t;
139 break;
140 }
141 }
69d1d385 142 temp_override<location_t> l (input_location);
143 if (loc != UNKNOWN_LOCATION)
144 input_location = loc;
4cedc476 145 expr = process_outer_var_ref (expr, tf_warning_or_error, true);
c017458d 146 if (!(TREE_TYPE (oexpr)
90ad495b 147 && TYPE_REF_P (TREE_TYPE (oexpr))))
c017458d 148 expr = convert_from_reference (expr);
4cedc476 149 }
150 break;
151 case COMPONENT_REF:
c017458d 152 case NON_DEPENDENT_EXPR:
4cedc476 153 recurse_op[0] = true;
154 break;
155 case COMPOUND_EXPR:
156 recurse_op[1] = true;
157 break;
158 case COND_EXPR:
159 recurse_op[2] = true;
160 if (TREE_OPERAND (expr, 1))
161 recurse_op[1] = true;
162 break;
163 case INDIRECT_REF:
164 if (REFERENCE_REF_P (expr))
165 {
166 /* Try to look through the reference. */
167 tree ref = TREE_OPERAND (expr, 0);
d0e2b7e7 168 if (rvalue_p && is_normal_capture_proxy (ref))
297de7bc 169 {
170 /* Look through capture by reference. */
171 tree cap = DECL_CAPTURED_VARIABLE (ref);
90ad495b 172 if (!TYPE_REF_P (TREE_TYPE (cap))
297de7bc 173 && decl_constant_var_p (cap))
80fdc40f 174 {
175 tree val = RECUR (cap);
176 if (!is_capture_proxy (val))
177 {
178 tree l = current_lambda_expr ();
179 LAMBDA_EXPR_CAPTURE_OPTIMIZED (l) = true;
180 }
181 return val;
182 }
297de7bc 183 }
4cedc476 184 tree r = mark_rvalue_use (ref, loc, reject_builtin);
185 if (r != ref)
c017458d 186 expr = convert_from_reference (r);
4cedc476 187 }
188 break;
aaee8db6 189
aaee8db6 190 case VIEW_CONVERT_EXPR:
69d1d385 191 if (location_wrapper_p (expr))
0c2ebbc4 192 {
193 loc = EXPR_LOCATION (expr);
194 tree op = TREE_OPERAND (expr, 0);
195 tree nop = RECUR (op);
196 if (nop == error_mark_node)
197 return error_mark_node;
198 TREE_OPERAND (expr, 0) = nop;
199 /* If we're replacing a DECL with a constant, we also need to change
200 the TREE_CODE of the location wrapper. */
201 if (op != nop && rvalue_p)
202 TREE_SET_CODE (expr, NON_LVALUE_EXPR);
203 return expr;
204 }
205 gcc_fallthrough();
206 CASE_CONVERT:
aaee8db6 207 recurse_op[0] = true;
208 break;
209
210 default:
4cedc476 211 break;
212 }
213
4cedc476 214 for (int i = 0; i < 3; ++i)
215 if (recurse_op[i])
216 {
217 tree op = TREE_OPERAND (expr, i);
c017458d 218 op = RECUR (op);
219 if (op == error_mark_node)
220 return error_mark_node;
221 TREE_OPERAND (expr, i) = op;
4cedc476 222 }
223
4cedc476 224 return expr;
225#undef RECUR
226}
227
547c6b1f 228/* Called whenever the expression EXPR is used in an rvalue context.
229 When REJECT_BUILTIN is true the expression is checked to make sure
230 it doesn't make it possible to obtain the address of a GCC built-in
231 function with no library fallback (or any of its bits, such as in
232 a conversion to bool). */
4cedc476 233
fbb73d9b 234tree
4cedc476 235mark_rvalue_use (tree e,
547c6b1f 236 location_t loc /* = UNKNOWN_LOCATION */,
237 bool reject_builtin /* = true */)
fbb73d9b 238{
4cedc476 239 return mark_use (e, true, true, loc, reject_builtin);
fbb73d9b 240}
241
d3e9b3e9 242/* Called whenever an expression is used in an lvalue context. */
243
244tree
245mark_lvalue_use (tree expr)
246{
247 return mark_use (expr, false, true, input_location, false);
248}
249
250/* As above, but don't consider this use a read. */
251
252tree
253mark_lvalue_use_nonread (tree expr)
254{
255 return mark_use (expr, false, false, input_location, false);
256}
257
c017458d 258/* Called when expr appears as a discarded-value expression. */
259
260tree
261mark_discarded_use (tree expr)
262{
263 /* The lvalue-to-rvalue conversion (7.1) is applied if and only if the
264 expression is a glvalue of volatile-qualified type and it is one of the
265 following:
266 * ( expression ), where expression is one of these expressions,
267 * id-expression (8.1.4),
268 * subscripting (8.2.1),
269 * class member access (8.2.5),
270 * indirection (8.3.1),
271 * pointer-to-member operation (8.5),
272 * conditional expression (8.16) where both the second and the third
273 operands are one of these expressions, or
274 * comma expression (8.19) where the right operand is one of these
275 expressions. */
276 if (expr == NULL_TREE)
277 return expr;
278
d582d140 279 STRIP_ANY_LOCATION_WRAPPER (expr);
280
c017458d 281 switch (TREE_CODE (expr))
282 {
283 case COND_EXPR:
284 TREE_OPERAND (expr, 2) = mark_discarded_use (TREE_OPERAND (expr, 2));
285 gcc_fallthrough ();
286 case COMPOUND_EXPR:
287 TREE_OPERAND (expr, 1) = mark_discarded_use (TREE_OPERAND (expr, 1));
288 return expr;
289
290 case COMPONENT_REF:
291 case ARRAY_REF:
292 case INDIRECT_REF:
293 case MEMBER_REF:
294 break;
295 default:
296 if (DECL_P (expr))
297 break;
298 else
299 return expr;
300 }
301
302 /* Like mark_rvalue_use, but don't reject built-ins. */
303 return mark_use (expr, true, true, input_location, false);
304}
305
fbb73d9b 306/* Called whenever an expression is used in a type use context. */
307
308tree
309mark_type_use (tree expr)
310{
311 mark_exp_read (expr);
312 return expr;
313}
314
315/* Mark EXP as read, not just set, for set but not used -Wunused
316 warning purposes. */
317
318void
319mark_exp_read (tree exp)
320{
321 if (exp == NULL)
322 return;
323
324 switch (TREE_CODE (exp))
325 {
326 case VAR_DECL:
3f430531 327 if (DECL_DECOMPOSITION_P (exp))
328 mark_exp_read (DECL_DECOMP_BASE (exp));
a440faa5 329 gcc_fallthrough ();
fbb73d9b 330 case PARM_DECL:
331 DECL_READ_P (exp) = 1;
332 break;
333 case ARRAY_REF:
334 case COMPONENT_REF:
335 case MODIFY_EXPR:
336 case REALPART_EXPR:
337 case IMAGPART_EXPR:
338 CASE_CONVERT:
339 case ADDR_EXPR:
0388391d 340 case INDIRECT_REF:
c8749fa8 341 case FLOAT_EXPR:
a4735361 342 case NON_DEPENDENT_EXPR:
7345b977 343 case VIEW_CONVERT_EXPR:
fbb73d9b 344 mark_exp_read (TREE_OPERAND (exp, 0));
345 break;
346 case COMPOUND_EXPR:
347 mark_exp_read (TREE_OPERAND (exp, 1));
348 break;
349 case COND_EXPR:
350 if (TREE_OPERAND (exp, 1))
351 mark_exp_read (TREE_OPERAND (exp, 1));
352 if (TREE_OPERAND (exp, 2))
353 mark_exp_read (TREE_OPERAND (exp, 2));
354 break;
355 default:
356 break;
357 }
358}
359
510e5f39 360/* Fold X for consideration by one of the warning functions when checking
361 whether an expression has a constant value. */
362
363tree
364fold_for_warn (tree x)
365{
366 /* C++ implementation. */
367
368 /* It's not generally safe to fully fold inside of a template, so
369 call fold_non_dependent_expr instead. */
370 if (processing_template_decl)
006b503a 371 {
372 tree f = fold_non_dependent_expr (x, tf_none);
373 if (f == error_mark_node)
374 return x;
375 else
376 return f;
377 }
510e5f39 378
379 return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
380}