1/* Convert language-specific tree expression to rtl instructions,
2 for GNU compiler.
3 Copyright (C) 1988-2017 Free Software Foundation, Inc.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful,
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
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "cp-tree.h"
26
27/* Expand C++-specific constants. Currently, this means PTRMEM_CST. */
28
29tree
30cplus_expand_constant (tree cst)
31{
32 switch (TREE_CODE (cst))
33 {
34 case PTRMEM_CST:
35 {
36 tree type = TREE_TYPE (cst);
37 tree member;
38
39 /* Find the member. */
40 member = PTRMEM_CST_MEMBER (cst);
41
42 /* We can't lower this until the class is complete. */
43 if (!COMPLETE_TYPE_P (DECL_CONTEXT (member)))
44 return cst;
45
46 if (TREE_CODE (member) == FIELD_DECL)
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 }
62 else
63 {
64 tree delta;
65 tree pfn;
66
67 expand_ptrmemfunc_cst (cst, &delta, &pfn);
68 cst = build_ptrmemfunc1 (type, delta, pfn);
69 }
70 }
71 break;
72
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
81 default:
82 /* There's nothing to do. */
83 break;
84 }
85
86 return cst;
87}
88
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
92static tree
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
99 if (expr == NULL_TREE || expr == error_mark_node)
100 return expr;
101
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
108 tree oexpr = expr;
109 bool recurse_op[3] = { false, false, false };
110 switch (TREE_CODE (expr))
111 {
112 case VAR_DECL:
113 case PARM_DECL:
114 if (rvalue_p && is_normal_capture_proxy (expr))
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))
120 return RECUR (cap);
121 }
122 if (outer_automatic_var_p (expr)
123 && decl_constant_var_p (expr))
124 {
125 if (rvalue_p)
126 {
127 tree t = maybe_constant_value (expr);
128 if (TREE_CONSTANT (t))
129 {
130 expr = t;
131 break;
132 }
133 }
134 expr = process_outer_var_ref (expr, tf_warning_or_error, true);
135 if (!(TREE_TYPE (oexpr)
136 && TREE_CODE (TREE_TYPE (oexpr)) == REFERENCE_TYPE))
137 expr = convert_from_reference (expr);
138 }
139 break;
140 case COMPONENT_REF:
141 case NON_DEPENDENT_EXPR:
142 recurse_op[0] = true;
143 break;
144 case COMPOUND_EXPR:
145 recurse_op[1] = true;
146 break;
147 case COND_EXPR:
148 recurse_op[2] = true;
149 if (TREE_OPERAND (expr, 1))
150 recurse_op[1] = true;
151 break;
152 case INDIRECT_REF:
153 if (REFERENCE_REF_P (expr))
154 {
155 /* Try to look through the reference. */
156 tree ref = TREE_OPERAND (expr, 0);
157 if (rvalue_p && is_normal_capture_proxy (ref))
158 {
159 /* Look through capture by reference. */
160 tree cap = DECL_CAPTURED_VARIABLE (ref);
161 if (TREE_CODE (TREE_TYPE (cap)) != REFERENCE_TYPE
162 && decl_constant_var_p (cap))
163 return RECUR (cap);
164 }
165 tree r = mark_rvalue_use (ref, loc, reject_builtin);
166 if (r != ref)
167 expr = convert_from_reference (r);
168 }
169 break;
170 default:
171 break;
172 }
173
174 for (int i = 0; i < 3; ++i)
175 if (recurse_op[i])
176 {
177 tree op = TREE_OPERAND (expr, i);
178 op = RECUR (op);
179 if (op == error_mark_node)
180 return error_mark_node;
181 TREE_OPERAND (expr, i) = op;
182 }
183
184 return expr;
185#undef RECUR
186}
187
188/* Called whenever the expression EXPR is used in an rvalue context.
189 When REJECT_BUILTIN is true the expression is checked to make sure
190 it doesn't make it possible to obtain the address of a GCC built-in
191 function with no library fallback (or any of its bits, such as in
192 a conversion to bool). */
193
194tree
195mark_rvalue_use (tree e,
196 location_t loc /* = UNKNOWN_LOCATION */,
197 bool reject_builtin /* = true */)
198{
199 return mark_use (e, true, true, loc, reject_builtin);
200}
201
202/* Called when expr appears as a discarded-value expression. */
203
204tree
205mark_discarded_use (tree expr)
206{
207 /* The lvalue-to-rvalue conversion (7.1) is applied if and only if the
208 expression is a glvalue of volatile-qualified type and it is one of the
209 following:
210 * ( expression ), where expression is one of these expressions,
211 * id-expression (8.1.4),
212 * subscripting (8.2.1),
213 * class member access (8.2.5),
214 * indirection (8.3.1),
215 * pointer-to-member operation (8.5),
216 * conditional expression (8.16) where both the second and the third
217 operands are one of these expressions, or
218 * comma expression (8.19) where the right operand is one of these
219 expressions. */
220 if (expr == NULL_TREE)
221 return expr;
222
223 switch (TREE_CODE (expr))
224 {
225 case COND_EXPR:
226 TREE_OPERAND (expr, 2) = mark_discarded_use (TREE_OPERAND (expr, 2));
227 gcc_fallthrough ();
228 case COMPOUND_EXPR:
229 TREE_OPERAND (expr, 1) = mark_discarded_use (TREE_OPERAND (expr, 1));
230 return expr;
231
232 case COMPONENT_REF:
233 case ARRAY_REF:
234 case INDIRECT_REF:
235 case MEMBER_REF:
236 break;
237 default:
238 if (DECL_P (expr))
239 break;
240 else
241 return expr;
242 }
243
244 /* Like mark_rvalue_use, but don't reject built-ins. */
245 return mark_use (expr, true, true, input_location, false);
246}
247
248/* Called whenever an expression is used in an lvalue context. */
249
250tree
251mark_lvalue_use (tree expr)
252{
253 return mark_use (expr, false, true, input_location, false);
254}
255
256/* As above, but don't consider this use a read. */
257
258tree
259mark_lvalue_use_nonread (tree expr)
260{
261 return mark_use (expr, false, false, input_location, false);
262}
263
264/* Called whenever an expression is used in a type use context. */
265
266tree
267mark_type_use (tree expr)
268{
269 mark_exp_read (expr);
270 return expr;
271}
272
273/* Mark EXP as read, not just set, for set but not used -Wunused
274 warning purposes. */
275
276void
277mark_exp_read (tree exp)
278{
279 if (exp == NULL)
280 return;
281
282 switch (TREE_CODE (exp))
283 {
284 case VAR_DECL:
285 if (DECL_DECOMPOSITION_P (exp))
286 mark_exp_read (DECL_DECOMP_BASE (exp));
287 gcc_fallthrough ();
288 case PARM_DECL:
289 DECL_READ_P (exp) = 1;
290 break;
291 case ARRAY_REF:
292 case COMPONENT_REF:
293 case MODIFY_EXPR:
294 case REALPART_EXPR:
295 case IMAGPART_EXPR:
296 CASE_CONVERT:
297 case ADDR_EXPR:
298 case INDIRECT_REF:
299 case FLOAT_EXPR:
300 case NON_DEPENDENT_EXPR:
301 case VIEW_CONVERT_EXPR:
302 mark_exp_read (TREE_OPERAND (exp, 0));
303 break;
304 case COMPOUND_EXPR:
305 mark_exp_read (TREE_OPERAND (exp, 1));
306 break;
307 case COND_EXPR:
308 if (TREE_OPERAND (exp, 1))
309 mark_exp_read (TREE_OPERAND (exp, 1));
310 if (TREE_OPERAND (exp, 2))
311 mark_exp_read (TREE_OPERAND (exp, 2));
312 break;
313 default:
314 break;
315 }
316}
317
318