Branch data Line data Source code
1 : : /* Language-level data type conversion for GNU C++.
2 : : Copyright (C) 1987-2024 Free Software Foundation, Inc.
3 : : Hacked by Michael Tiemann (tiemann@cygnus.com)
4 : :
5 : : This file is part of GCC.
6 : :
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.
11 : :
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.
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 : :
22 : : /* This file contains the functions for converting C++ expressions
23 : : to different data types. The only entry point is `convert'.
24 : : Every language front end must have a `convert' function
25 : : but what kind of conversions it does will depend on the language. */
26 : :
27 : : #include "config.h"
28 : : #include "system.h"
29 : : #include "coretypes.h"
30 : : #include "target.h"
31 : : #include "cp-tree.h"
32 : : #include "stor-layout.h"
33 : : #include "flags.h"
34 : : #include "intl.h"
35 : : #include "convert.h"
36 : : #include "stringpool.h"
37 : : #include "attribs.h"
38 : : #include "escaped_string.h"
39 : :
40 : : static tree convert_to_pointer_force (tree, tree, tsubst_flags_t);
41 : : static tree build_type_conversion (tree, tree);
42 : : static tree build_up_reference (tree, tree, int, tree, tsubst_flags_t);
43 : : static void diagnose_ref_binding (location_t, tree, tree, tree);
44 : :
45 : : /* Change of width--truncation and extension of integers or reals--
46 : : is represented with NOP_EXPR. Proper functioning of many things
47 : : assumes that no other conversions can be NOP_EXPRs.
48 : :
49 : : Conversion between integer and pointer is represented with CONVERT_EXPR.
50 : : Converting integer to real uses FLOAT_EXPR
51 : : and real to integer uses FIX_TRUNC_EXPR.
52 : :
53 : : Here is a list of all the functions that assume that widening and
54 : : narrowing is always done with a NOP_EXPR:
55 : : In convert.cc, convert_to_integer[_maybe_fold].
56 : : In c-typeck.cc, build_binary_op_nodefault (boolean ops),
57 : : and c_common_truthvalue_conversion.
58 : : In expr.cc: expand_expr, for operands of a MULT_EXPR.
59 : : In fold-const.cc: fold.
60 : : In tree.cc: get_narrower and get_unwidened.
61 : :
62 : : C++: in multiple-inheritance, converting between pointers may involve
63 : : adjusting them by a delta stored within the class definition. */
64 : :
65 : : /* Subroutines of `convert'. */
66 : :
67 : : /* if converting pointer to pointer
68 : : if dealing with classes, check for derived->base or vice versa
69 : : else if dealing with method pointers, delegate
70 : : else convert blindly
71 : : else if converting class, pass off to build_type_conversion
72 : : else try C-style pointer conversion. */
73 : :
74 : : static tree
75 : 24593026 : cp_convert_to_pointer (tree type, tree expr, bool dofold,
76 : : tsubst_flags_t complain)
77 : : {
78 : 24593026 : tree intype = TREE_TYPE (expr);
79 : 24593026 : enum tree_code form;
80 : 24593026 : tree rval;
81 : 24593026 : location_t loc = cp_expr_loc_or_input_loc (expr);
82 : :
83 : 24593026 : if (intype == error_mark_node)
84 : : return error_mark_node;
85 : :
86 : 24593026 : if (MAYBE_CLASS_TYPE_P (intype))
87 : : {
88 : 3 : intype = complete_type (intype);
89 : 3 : if (!COMPLETE_TYPE_P (intype))
90 : : {
91 : 0 : if (complain & tf_error)
92 : 0 : error_at (loc, "cannot convert from incomplete type %qH to %qI",
93 : : intype, type);
94 : 0 : return error_mark_node;
95 : : }
96 : :
97 : 3 : rval = build_type_conversion (type, expr);
98 : 3 : if (rval)
99 : : {
100 : 0 : if ((complain & tf_error)
101 : 0 : && rval == error_mark_node)
102 : 0 : error_at (loc, "conversion of %qE from %qH to %qI is ambiguous",
103 : : expr, intype, type);
104 : 0 : return rval;
105 : : }
106 : : }
107 : :
108 : : /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
109 : 24593026 : if (TYPE_PTR_P (type)
110 : 24593026 : && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
111 : 24434977 : || VOID_TYPE_P (TREE_TYPE (type))))
112 : : {
113 : 12 : if (TYPE_PTRMEMFUNC_P (intype)
114 : 2035055 : || TREE_CODE (intype) == METHOD_TYPE)
115 : 9 : return convert_member_func_to_ptr (type, expr, complain);
116 : 2035043 : if (TYPE_PTR_P (TREE_TYPE (expr)))
117 : 1074399 : return build_nop (type, expr);
118 : 960644 : intype = TREE_TYPE (expr);
119 : : }
120 : :
121 : 23518618 : if (expr == error_mark_node)
122 : : return error_mark_node;
123 : :
124 : 23518618 : form = TREE_CODE (intype);
125 : :
126 : 23518618 : if (INDIRECT_TYPE_P (intype))
127 : : {
128 : 15497190 : intype = TYPE_MAIN_VARIANT (intype);
129 : :
130 : 15497190 : if (TYPE_MAIN_VARIANT (type) != intype
131 : 10256264 : && TYPE_PTR_P (type)
132 : 10250304 : && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
133 : 4253343 : && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
134 : 4173401 : && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
135 : 19584093 : && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
136 : : {
137 : 4086903 : enum tree_code code = PLUS_EXPR;
138 : 4086903 : tree binfo;
139 : 4086903 : tree intype_class;
140 : 4086903 : tree type_class;
141 : 4086903 : bool same_p;
142 : :
143 : 4086903 : intype_class = TREE_TYPE (intype);
144 : 4086903 : type_class = TREE_TYPE (type);
145 : :
146 : 4086903 : same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class),
147 : : TYPE_MAIN_VARIANT (type_class));
148 : 4086903 : binfo = NULL_TREE;
149 : : /* Try derived to base conversion. */
150 : 4086903 : if (!same_p)
151 : 41084 : binfo = lookup_base (intype_class, type_class, ba_check,
152 : : NULL, complain);
153 : 4086903 : if (!same_p && !binfo)
154 : : {
155 : : /* Try base to derived conversion. */
156 : 19 : binfo = lookup_base (type_class, intype_class, ba_check,
157 : : NULL, complain);
158 : 19 : code = MINUS_EXPR;
159 : : }
160 : 4086903 : if (binfo == error_mark_node)
161 : : return error_mark_node;
162 : 4086894 : if (binfo || same_p)
163 : : {
164 : 4086894 : if (binfo)
165 : 41075 : expr = build_base_path (code, expr, binfo, 0, complain);
166 : : /* Add any qualifier conversions. */
167 : 4086894 : return build_nop (type, expr);
168 : : }
169 : : }
170 : :
171 : 11410287 : if (TYPE_PTRMEMFUNC_P (type))
172 : : {
173 : 0 : if (complain & tf_error)
174 : 0 : error_at (loc, "cannot convert %qE from type %qH to type %qI",
175 : : expr, intype, type);
176 : 0 : return error_mark_node;
177 : : }
178 : :
179 : 11410287 : return build_nop (type, expr);
180 : : }
181 : 1528 : else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
182 : 8022858 : || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
183 : 113 : return convert_ptrmem (type, expr, /*allow_inverse_p=*/false,
184 : 113 : /*c_cast_p=*/false, complain);
185 : 8021315 : else if (TYPE_PTRMEMFUNC_P (intype))
186 : : {
187 : 0 : if (!warn_pmf2ptr)
188 : : {
189 : 0 : if (TREE_CODE (expr) == PTRMEM_CST)
190 : 0 : return cp_convert_to_pointer (type, PTRMEM_CST_MEMBER (expr),
191 : 0 : dofold, complain);
192 : 0 : else if (TREE_CODE (expr) == OFFSET_REF)
193 : : {
194 : 0 : tree object = TREE_OPERAND (expr, 0);
195 : 0 : return get_member_function_from_ptrfunc (&object,
196 : 0 : TREE_OPERAND (expr, 1),
197 : : complain);
198 : : }
199 : : }
200 : 0 : if (complain & tf_error)
201 : 0 : error_at (loc, "cannot convert %qE from type %qH to type %qI",
202 : : expr, intype, type);
203 : 0 : return error_mark_node;
204 : : }
205 : :
206 : 8021315 : if (null_ptr_cst_p (expr))
207 : : {
208 : 5074311 : if (TYPE_PTRMEMFUNC_P (type))
209 : 753 : return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
210 : 753 : /*c_cast_p=*/false, complain);
211 : :
212 : 5073558 : if (complain & tf_warning)
213 : 3606634 : maybe_warn_zero_as_null_pointer_constant (expr, loc);
214 : :
215 : : /* A NULL pointer-to-data-member is represented by -1, not by
216 : : zero. */
217 : 5073558 : tree val = (TYPE_PTRDATAMEM_P (type)
218 : 5073558 : ? build_int_cst_type (type, -1)
219 : 5072128 : : build_int_cst (type, 0));
220 : :
221 : 5073558 : return (TREE_SIDE_EFFECTS (expr)
222 : 5073558 : ? build2 (COMPOUND_EXPR, type, expr, val) : val);
223 : : }
224 : 2947004 : else if (TYPE_PTRMEM_P (type) && INTEGRAL_CODE_P (form))
225 : : {
226 : 0 : if (complain & tf_error)
227 : 0 : error_at (loc, "invalid conversion from %qH to %qI", intype, type);
228 : 0 : return error_mark_node;
229 : : }
230 : :
231 : 2947004 : if (INTEGRAL_CODE_P (form))
232 : : {
233 : 2962215 : if (TYPE_PRECISION (intype) == POINTER_SIZE)
234 : 2926857 : return build1 (CONVERT_EXPR, type, expr);
235 : 20141 : expr = cp_convert (c_common_type_for_size (TYPE_PRECISION (type), 0), expr,
236 : : complain);
237 : : /* Modes may be different but sizes should be the same. There
238 : : is supposed to be some integral type that is the same width
239 : : as a pointer. */
240 : 60423 : gcc_assert (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (expr)))
241 : : == GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)));
242 : :
243 : : /* FIXME needed because convert_to_pointer_maybe_fold still folds
244 : : conversion of constants. */
245 : 20141 : if (!dofold)
246 : 20141 : return build1 (CONVERT_EXPR, type, expr);
247 : :
248 : 0 : return convert_to_pointer_maybe_fold (type, expr, dofold);
249 : : }
250 : :
251 : 6 : if (type_unknown_p (expr))
252 : 0 : return instantiate_type (type, expr, complain);
253 : :
254 : 6 : if (complain & tf_error)
255 : 6 : error_at (loc, "cannot convert %qE from type %qH to type %qI",
256 : : expr, intype, type);
257 : 6 : return error_mark_node;
258 : : }
259 : :
260 : : /* Like convert, except permit conversions to take place which
261 : : are not normally allowed due to access restrictions
262 : : (such as conversion from sub-type to private super-type). */
263 : :
264 : : static tree
265 : 7551778 : convert_to_pointer_force (tree type, tree expr, tsubst_flags_t complain)
266 : : {
267 : 7551778 : tree intype = TREE_TYPE (expr);
268 : 7551778 : enum tree_code form = TREE_CODE (intype);
269 : :
270 : 7551778 : if (form == POINTER_TYPE)
271 : : {
272 : 7551778 : intype = TYPE_MAIN_VARIANT (intype);
273 : :
274 : 7551778 : if (TYPE_MAIN_VARIANT (type) != intype
275 : 2310831 : && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
276 : 2310802 : && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
277 : 2310799 : && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
278 : 9862577 : && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
279 : : {
280 : 2310799 : enum tree_code code = PLUS_EXPR;
281 : 2310799 : tree binfo;
282 : :
283 : 2310799 : binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
284 : : ba_unique, NULL, complain);
285 : 2310799 : if (!binfo)
286 : : {
287 : 3 : binfo = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
288 : : ba_unique, NULL, complain);
289 : 3 : code = MINUS_EXPR;
290 : : }
291 : 2310799 : if (binfo == error_mark_node)
292 : : return error_mark_node;
293 : 2310799 : if (binfo)
294 : : {
295 : 2310796 : expr = build_base_path (code, expr, binfo, 0, complain);
296 : 2310796 : if (expr == error_mark_node)
297 : : return error_mark_node;
298 : : /* Add any qualifier conversions. */
299 : 2310796 : if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
300 : : TREE_TYPE (type)))
301 : 375359 : expr = build_nop (type, expr);
302 : 2310796 : return expr;
303 : : }
304 : : }
305 : : }
306 : :
307 : 5240982 : return cp_convert_to_pointer (type, expr, /*fold*/false, complain);
308 : : }
309 : :
310 : : /* We are passing something to a function which requires a reference.
311 : : The type we are interested in is in TYPE. The initial
312 : : value we have to begin with is in ARG.
313 : :
314 : : FLAGS controls how we manage access checking.
315 : : DIRECT_BIND in FLAGS controls how any temporaries are generated.
316 : : If DIRECT_BIND is set, DECL is the reference we're binding to. */
317 : :
318 : : static tree
319 : 235 : build_up_reference (tree type, tree arg, int flags, tree decl,
320 : : tsubst_flags_t complain)
321 : : {
322 : 235 : tree rval;
323 : 235 : tree argtype = TREE_TYPE (arg);
324 : 235 : tree target_type = TREE_TYPE (type);
325 : :
326 : 235 : gcc_assert (TYPE_REF_P (type));
327 : :
328 : 235 : if ((flags & DIRECT_BIND) && ! lvalue_p (arg))
329 : : {
330 : : /* Create a new temporary variable. We can't just use a TARGET_EXPR
331 : : here because it needs to live as long as DECL. */
332 : 0 : tree targ = arg;
333 : :
334 : 0 : arg = make_temporary_var_for_ref_to_temp (decl, target_type);
335 : :
336 : : /* Process the initializer for the declaration. */
337 : 0 : DECL_INITIAL (arg) = targ;
338 : 0 : cp_finish_decl (arg, targ, /*init_const_expr_p=*/false, NULL_TREE,
339 : : LOOKUP_ONLYCONVERTING|DIRECT_BIND);
340 : : }
341 : 235 : else if (!(flags & DIRECT_BIND) && ! obvalue_p (arg))
342 : 1 : return get_target_expr (arg, complain);
343 : :
344 : : /* If we had a way to wrap this up, and say, if we ever needed its
345 : : address, transform all occurrences of the register, into a memory
346 : : reference we could win better. */
347 : 234 : rval = cp_build_addr_expr (arg, complain);
348 : 234 : if (rval == error_mark_node)
349 : : return error_mark_node;
350 : :
351 : 234 : if ((flags & LOOKUP_PROTECT)
352 : 234 : && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
353 : 9 : && MAYBE_CLASS_TYPE_P (argtype)
354 : 243 : && MAYBE_CLASS_TYPE_P (target_type))
355 : : {
356 : : /* We go through lookup_base for the access control. */
357 : 9 : tree binfo = lookup_base (argtype, target_type, ba_check,
358 : : NULL, complain);
359 : 9 : if (binfo == error_mark_node)
360 : : return error_mark_node;
361 : 9 : if (binfo == NULL_TREE)
362 : 0 : return error_not_base_type (target_type, argtype);
363 : 9 : rval = build_base_path (PLUS_EXPR, rval, binfo, 1, complain);
364 : : }
365 : : else
366 : 225 : rval
367 : 225 : = convert_to_pointer_force (build_pointer_type (target_type),
368 : : rval, complain);
369 : 234 : return build_nop (type, rval);
370 : : }
371 : :
372 : : /* Subroutine of convert_to_reference. REFTYPE is the target reference type.
373 : : INTYPE is the original rvalue type and DECL is an optional _DECL node
374 : : for diagnostics.
375 : :
376 : : [dcl.init.ref] says that if an rvalue is used to
377 : : initialize a reference, then the reference must be to a
378 : : non-volatile const type. */
379 : :
380 : : static void
381 : 7 : diagnose_ref_binding (location_t loc, tree reftype, tree intype, tree decl)
382 : : {
383 : 7 : tree ttl = TREE_TYPE (reftype);
384 : :
385 : 7 : if (!TYPE_REF_IS_RVALUE (reftype)
386 : 7 : && !CP_TYPE_CONST_NON_VOLATILE_P (ttl))
387 : : {
388 : 0 : const char *msg;
389 : :
390 : 0 : if (CP_TYPE_VOLATILE_P (ttl) && decl)
391 : : msg = G_("initialization of volatile reference type %q#T from "
392 : : "rvalue of type %qT");
393 : 0 : else if (CP_TYPE_VOLATILE_P (ttl))
394 : : msg = G_("conversion to volatile reference type %q#T "
395 : : "from rvalue of type %qT");
396 : 0 : else if (decl)
397 : : msg = G_("initialization of non-const reference type %q#T from "
398 : : "rvalue of type %qT");
399 : : else
400 : 0 : msg = G_("conversion to non-const reference type %q#T from "
401 : : "rvalue of type %qT");
402 : :
403 : 0 : permerror (loc, msg, reftype, intype);
404 : : }
405 : 7 : }
406 : :
407 : : /* For C++: Only need to do one-level references, but cannot
408 : : get tripped up on signed/unsigned differences.
409 : :
410 : : DECL is either NULL_TREE or the _DECL node for a reference that is being
411 : : initialized. It can be error_mark_node if we don't know the _DECL but
412 : : we know it's an initialization. */
413 : :
414 : : tree
415 : 237 : convert_to_reference (tree reftype, tree expr, int convtype,
416 : : int flags, tree decl, tsubst_flags_t complain)
417 : : {
418 : 237 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
419 : 237 : tree intype;
420 : 237 : tree rval = NULL_TREE;
421 : 237 : tree rval_as_conversion = NULL_TREE;
422 : 237 : bool can_convert_intype_to_type;
423 : 237 : location_t loc = cp_expr_loc_or_input_loc (expr);
424 : :
425 : 237 : if (TREE_CODE (type) == FUNCTION_TYPE
426 : 237 : && TREE_TYPE (expr) == unknown_type_node)
427 : 0 : expr = instantiate_type (type, expr, complain);
428 : :
429 : 237 : if (expr == error_mark_node)
430 : : return error_mark_node;
431 : :
432 : 235 : intype = TREE_TYPE (expr);
433 : :
434 : 235 : gcc_assert (!TYPE_REF_P (intype));
435 : 235 : gcc_assert (TYPE_REF_P (reftype));
436 : :
437 : 235 : intype = TYPE_MAIN_VARIANT (intype);
438 : :
439 : 235 : can_convert_intype_to_type = can_convert_standard (type, intype, complain);
440 : :
441 : 235 : if (!can_convert_intype_to_type
442 : 4 : && (convtype & CONV_IMPLICIT) && MAYBE_CLASS_TYPE_P (intype)
443 : 235 : && ! (flags & LOOKUP_NO_CONVERSION))
444 : : {
445 : : /* Look for a user-defined conversion to lvalue that we can use. */
446 : :
447 : 0 : rval_as_conversion
448 : 0 : = build_type_conversion (reftype, expr);
449 : :
450 : 0 : if (rval_as_conversion && rval_as_conversion != error_mark_node
451 : 0 : && lvalue_p (rval_as_conversion))
452 : : {
453 : : expr = rval_as_conversion;
454 : 235 : rval_as_conversion = NULL_TREE;
455 : : intype = type;
456 : : can_convert_intype_to_type = 1;
457 : : }
458 : : }
459 : :
460 : 235 : if (((convtype & CONV_STATIC)
461 : 0 : && can_convert_standard (intype, type, complain))
462 : 235 : || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type))
463 : : {
464 : 231 : {
465 : 231 : tree ttl = TREE_TYPE (reftype);
466 : 231 : tree ttr = lvalue_type (expr);
467 : :
468 : 231 : if ((complain & tf_error)
469 : 231 : && ! lvalue_p (expr))
470 : 3 : diagnose_ref_binding (loc, reftype, intype, decl);
471 : :
472 : 231 : if (! (convtype & CONV_CONST)
473 : 231 : && !at_least_as_qualified_p (ttl, ttr))
474 : : {
475 : 6 : if (complain & tf_error)
476 : 6 : permerror (loc, "conversion from %qH to %qI discards qualifiers",
477 : : ttr, reftype);
478 : : else
479 : 0 : return error_mark_node;
480 : : }
481 : : }
482 : :
483 : 231 : return build_up_reference (reftype, expr, flags, decl, complain);
484 : : }
485 : 4 : else if ((convtype & CONV_REINTERPRET) && obvalue_p (expr))
486 : : {
487 : : /* When casting an lvalue to a reference type, just convert into
488 : : a pointer to the new type and deference it. This is allowed
489 : : by San Diego WP section 5.2.9 paragraph 12, though perhaps it
490 : : should be done directly (jason). (int &)ri ---> *(int*)&ri */
491 : :
492 : : /* B* bp; A& ar = (A&)bp; is valid, but it's probably not what they
493 : : meant. */
494 : 0 : if ((complain & tf_warning)
495 : 0 : && TYPE_PTR_P (intype)
496 : 0 : && (comptypes (TREE_TYPE (intype), type,
497 : : COMPARE_BASE | COMPARE_DERIVED)))
498 : 0 : warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
499 : : intype, reftype);
500 : :
501 : 0 : rval = cp_build_addr_expr (expr, complain);
502 : 0 : if (rval != error_mark_node)
503 : 0 : rval = convert_force (build_pointer_type (TREE_TYPE (reftype)),
504 : : rval, 0, complain);
505 : 0 : if (rval != error_mark_node)
506 : 0 : rval = build1 (NOP_EXPR, reftype, rval);
507 : : }
508 : : else
509 : : {
510 : 4 : rval = convert_for_initialization (NULL_TREE, type, expr, flags,
511 : : ICR_CONVERTING, 0, 0, complain);
512 : 4 : if (rval == NULL_TREE || rval == error_mark_node)
513 : : return rval;
514 : 4 : if (complain & tf_error)
515 : 4 : diagnose_ref_binding (loc, reftype, intype, decl);
516 : 4 : rval = build_up_reference (reftype, rval, flags, decl, complain);
517 : : }
518 : :
519 : 4 : if (rval)
520 : : {
521 : : /* If we found a way to convert earlier, then use it. */
522 : : return rval;
523 : : }
524 : :
525 : 0 : if (complain & tf_error)
526 : 0 : error_at (loc, "cannot convert type %qH to type %qI", intype, reftype);
527 : :
528 : 0 : return error_mark_node;
529 : : }
530 : :
531 : : /* We are using a reference VAL for its value. Bash that reference all the
532 : : way down to its lowest form. */
533 : :
534 : : tree
535 : 1646694785 : convert_from_reference (tree val)
536 : : {
537 : 1646694785 : if (TREE_TYPE (val)
538 : 1646694785 : && TYPE_REF_P (TREE_TYPE (val)))
539 : : {
540 : 180791355 : tree t = TREE_TYPE (TREE_TYPE (val));
541 : 180791355 : tree ref = build1 (INDIRECT_REF, t, val);
542 : :
543 : 180791355 : mark_exp_read (val);
544 : : /* We *must* set TREE_READONLY when dereferencing a pointer to const,
545 : : so that we get the proper error message if the result is used
546 : : to assign to. Also, &* is supposed to be a no-op. */
547 : 180791355 : TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
548 : 180791355 : TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
549 : 180791355 : TREE_SIDE_EFFECTS (ref)
550 : 180791355 : = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val));
551 : 180791355 : val = ref;
552 : : }
553 : :
554 : 1646694785 : return val;
555 : : }
556 : :
557 : : /* Really perform an lvalue-to-rvalue conversion, including copying an
558 : : argument of class type into a temporary. */
559 : :
560 : : tree
561 : 12419546 : force_rvalue (tree expr, tsubst_flags_t complain)
562 : : {
563 : 12419546 : tree type = TREE_TYPE (expr);
564 : 12419546 : if (MAYBE_CLASS_TYPE_P (type) && TREE_CODE (expr) != TARGET_EXPR)
565 : : {
566 : 139538 : releasing_vec args (make_tree_vector_single (expr));
567 : 139538 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
568 : : &args, type, LOOKUP_NORMAL, complain);
569 : 139538 : expr = build_cplus_new (type, expr, complain);
570 : 139538 : }
571 : : else
572 : 12280008 : expr = decay_conversion (expr, complain);
573 : :
574 : 12419546 : return expr;
575 : : }
576 : :
577 : :
578 : : /* If EXPR and ORIG are INTEGER_CSTs, return a version of EXPR that has
579 : : TREE_OVERFLOW set only if it is set in ORIG. Otherwise, return EXPR
580 : : unchanged. */
581 : :
582 : : static tree
583 : 137640059 : ignore_overflows (tree expr, tree orig)
584 : : {
585 : 137640059 : tree stripped_expr = tree_strip_any_location_wrapper (expr);
586 : 137640059 : tree stripped_orig = tree_strip_any_location_wrapper (orig);
587 : :
588 : 137640059 : if (TREE_CODE (stripped_expr) == INTEGER_CST
589 : 85243071 : && TREE_CODE (stripped_orig) == INTEGER_CST
590 : 222880563 : && TREE_OVERFLOW (stripped_expr) != TREE_OVERFLOW (stripped_orig))
591 : : {
592 : 4756 : gcc_assert (!TREE_OVERFLOW (stripped_orig));
593 : : /* Ensure constant sharing. */
594 : 4756 : stripped_expr = wide_int_to_tree (TREE_TYPE (stripped_expr),
595 : 9512 : wi::to_wide (stripped_expr));
596 : : }
597 : :
598 : 137640059 : return preserve_any_location_wrapper (stripped_expr, expr);
599 : : }
600 : :
601 : : /* Fold away simple conversions, but make sure TREE_OVERFLOW is set
602 : : properly and propagate TREE_NO_WARNING if folding EXPR results
603 : : in the same expression code. */
604 : :
605 : : tree
606 : 26869615 : cp_fold_convert (tree type, tree expr)
607 : : {
608 : 26869615 : tree conv;
609 : 26869615 : if (TREE_TYPE (expr) == type)
610 : : conv = expr;
611 : 12493542 : else if (TREE_CODE (expr) == PTRMEM_CST
612 : 12493542 : && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
613 : : PTRMEM_CST_CLASS (expr)))
614 : : {
615 : : /* Avoid wrapping a PTRMEM_CST in NOP_EXPR. */
616 : 181 : conv = copy_node (expr);
617 : 181 : TREE_TYPE (conv) = type;
618 : : }
619 : 12493361 : else if (TYPE_PTRMEM_P (type))
620 : : {
621 : 6 : conv = convert_ptrmem (type, expr, true, false,
622 : : tf_warning_or_error);
623 : 6 : conv = cp_fully_fold (conv);
624 : : }
625 : : else
626 : : {
627 : 12493355 : conv = fold_convert (type, expr);
628 : 12493355 : conv = ignore_overflows (conv, expr);
629 : : }
630 : :
631 : 26869615 : if (TREE_CODE (expr) == TREE_CODE (conv))
632 : 17512121 : copy_warning (conv, expr);
633 : :
634 : 26869615 : return conv;
635 : : }
636 : :
637 : : /* C++ conversions, preference to static cast conversions. */
638 : :
639 : : tree
640 : 208992065 : cp_convert (tree type, tree expr, tsubst_flags_t complain)
641 : : {
642 : 208992065 : return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL, complain);
643 : : }
644 : :
645 : : /* C++ equivalent of convert_and_check but using cp_convert as the
646 : : conversion function.
647 : :
648 : : Convert EXPR to TYPE, warning about conversion problems with constants.
649 : : Invoke this function on every expression that is converted implicitly,
650 : : i.e. because of language rules and not because of an explicit cast. */
651 : :
652 : : tree
653 : 57634057 : cp_convert_and_check (tree type, tree expr, tsubst_flags_t complain)
654 : : {
655 : 57634057 : tree result, expr_for_warning = expr;
656 : :
657 : 57634057 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
658 : 94 : expr = TREE_OPERAND (expr, 0);
659 : 57634057 : if (TREE_TYPE (expr) == type)
660 : : return expr;
661 : 57634044 : if (expr == error_mark_node)
662 : : return expr;
663 : 57634044 : result = cp_convert (type, expr, complain);
664 : :
665 : 57634044 : if ((complain & tf_warning)
666 : 53761282 : && c_inhibit_evaluation_warnings == 0)
667 : : {
668 : 52585550 : tree folded = cp_fully_fold (expr_for_warning);
669 : 52585550 : tree folded_result;
670 : 52585550 : if (folded == expr)
671 : : folded_result = result;
672 : : else
673 : : {
674 : : /* Avoid bogus -Wparentheses warnings. */
675 : 23289891 : warning_sentinel w (warn_parentheses);
676 : 23289891 : warning_sentinel c (warn_int_in_bool_context);
677 : 23289891 : folded_result = cp_convert (type, folded, tf_none);
678 : 23289891 : }
679 : 52585550 : folded_result = fold_simple (folded_result);
680 : 40278581 : if (!TREE_OVERFLOW_P (folded)
681 : 92864128 : && folded_result != error_mark_node)
682 : 79582120 : warnings_for_convert_and_check (cp_expr_loc_or_input_loc (expr),
683 : : type, folded, folded_result);
684 : : }
685 : :
686 : : return result;
687 : : }
688 : :
689 : : /* Conversion...
690 : :
691 : : FLAGS indicates how we should behave. */
692 : :
693 : : tree
694 : 336025197 : ocp_convert (tree type, tree expr, int convtype, int flags,
695 : : tsubst_flags_t complain)
696 : : {
697 : 336025197 : tree e = expr;
698 : 336025197 : enum tree_code code = TREE_CODE (type);
699 : 336025197 : const char *invalid_conv_diag;
700 : 336025197 : tree e1;
701 : 336025197 : location_t loc = cp_expr_loc_or_input_loc (expr);
702 : 336025197 : bool dofold = (convtype & CONV_FOLD);
703 : :
704 : 336025197 : if (error_operand_p (e) || type == error_mark_node)
705 : 107 : return error_mark_node;
706 : :
707 : 336025090 : if (TREE_CODE (e) == COMPOUND_EXPR)
708 : : {
709 : 425234 : e = ocp_convert (type, TREE_OPERAND (e, 1), convtype, flags, complain);
710 : 425234 : if (e == error_mark_node)
711 : : return error_mark_node;
712 : 425234 : if (e == TREE_OPERAND (expr, 1))
713 : : return expr;
714 : 425216 : e = build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (e),
715 : 425216 : TREE_OPERAND (expr, 0), e);
716 : 425216 : copy_warning (e, expr);
717 : 425216 : return e;
718 : : }
719 : :
720 : 335599856 : complete_type (type);
721 : 335599856 : complete_type (TREE_TYPE (expr));
722 : :
723 : 671199712 : if ((invalid_conv_diag
724 : 335599856 : = targetm.invalid_conversion (TREE_TYPE (expr), type)))
725 : : {
726 : 0 : if (complain & tf_error)
727 : 0 : error (invalid_conv_diag);
728 : 0 : return error_mark_node;
729 : : }
730 : :
731 : : /* FIXME remove when moving to c_fully_fold model. */
732 : 335599856 : if (!CLASS_TYPE_P (type))
733 : : {
734 : 318534322 : e = mark_rvalue_use (e);
735 : 318534322 : tree v = scalar_constant_value (e);
736 : 318534322 : if (!error_operand_p (v))
737 : 318534256 : e = v;
738 : : }
739 : 335599856 : if (error_operand_p (e))
740 : 0 : return error_mark_node;
741 : :
742 : 335599856 : if (NULLPTR_TYPE_P (type) && null_ptr_cst_p (e))
743 : : {
744 : 1927 : if (complain & tf_warning)
745 : 998 : maybe_warn_zero_as_null_pointer_constant (e, loc);
746 : :
747 : 1927 : if (!TREE_SIDE_EFFECTS (e))
748 : 1921 : return nullptr_node;
749 : : }
750 : :
751 : 335597935 : if (MAYBE_CLASS_TYPE_P (type) && (convtype & CONV_FORCE_TEMP))
752 : : /* We need a new temporary; don't take this shortcut. */;
753 : 334739297 : else if (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (e)))
754 : : {
755 : 162059678 : tree etype = TREE_TYPE (e);
756 : 162059678 : if (same_type_p (type, etype))
757 : : /* The call to fold will not always remove the NOP_EXPR as
758 : : might be expected, since if one of the types is a typedef;
759 : : the comparison in fold is just equality of pointers, not a
760 : : call to comptypes. We don't call fold in this case because
761 : : that can result in infinite recursion; fold will call
762 : : convert, which will call ocp_convert, etc. */
763 : : return e;
764 : : /* For complex data types, we need to perform componentwise
765 : : conversion. */
766 : 15231400 : else if (TREE_CODE (type) == COMPLEX_TYPE)
767 : 0 : return convert_to_complex_maybe_fold (type, e, dofold);
768 : 15231400 : else if (VECTOR_TYPE_P (type))
769 : 3052 : return convert_to_vector (type, rvalue (e));
770 : 15228348 : else if (TREE_CODE (e) == TARGET_EXPR)
771 : : {
772 : : /* Don't build a NOP_EXPR of class type. Instead, change the
773 : : type of the temporary. */
774 : 87170 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, etype));
775 : 87170 : TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type;
776 : 87170 : return e;
777 : : }
778 : 15141178 : else if (TREE_CODE (e) == CONSTRUCTOR)
779 : : {
780 : 5 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, etype));
781 : 5 : TREE_TYPE (e) = type;
782 : 5 : return e;
783 : : }
784 : : else
785 : : {
786 : : /* We shouldn't be treating objects of ADDRESSABLE type as
787 : : rvalues. */
788 : 15141173 : gcc_assert (!TREE_ADDRESSABLE (type));
789 : 15141173 : return build_nop (type, e);
790 : : }
791 : : }
792 : :
793 : 173538257 : e1 = targetm.convert_to_type (type, e);
794 : 173538257 : if (e1)
795 : : return e1;
796 : :
797 : 173538257 : if (code == VOID_TYPE && (convtype & CONV_STATIC))
798 : : {
799 : 20438 : e = convert_to_void (e, ICV_CAST, complain);
800 : 20438 : return e;
801 : : }
802 : :
803 : 173517819 : if (INTEGRAL_CODE_P (code))
804 : : {
805 : 133271682 : tree intype = TREE_TYPE (e);
806 : 133271682 : tree converted;
807 : :
808 : 133271682 : if (TREE_CODE (type) == ENUMERAL_TYPE)
809 : : {
810 : : /* enum = enum, enum = int, enum = float, (enum)pointer are all
811 : : errors. */
812 : 732891 : if (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
813 : 6 : || SCALAR_FLOAT_TYPE_P (intype))
814 : 732891 : && ! (convtype & CONV_STATIC))
815 : 732891 : || TYPE_PTR_P (intype))
816 : : {
817 : 0 : if (complain & tf_error)
818 : 0 : permerror (loc, "conversion from %q#T to %q#T", intype, type);
819 : : else
820 : 0 : return error_mark_node;
821 : : }
822 : :
823 : : /* [expr.static.cast]
824 : :
825 : : 8. A value of integral or enumeration type can be explicitly
826 : : converted to an enumeration type. The value is unchanged if
827 : : the original value is within the range of the enumeration
828 : : values. Otherwise, the resulting enumeration value is
829 : : unspecified. */
830 : 732891 : tree val = fold_for_warn (e);
831 : 732891 : if ((complain & tf_warning)
832 : 732519 : && TREE_CODE (val) == INTEGER_CST
833 : 177891 : && ENUM_UNDERLYING_TYPE (type)
834 : 910779 : && !int_fits_type_p (val, ENUM_UNDERLYING_TYPE (type)))
835 : 117 : warning_at (loc, OPT_Wconversion,
836 : : "the result of the conversion is unspecified because "
837 : : "%qE is outside the range of type %qT",
838 : : expr, type);
839 : : }
840 : 133271682 : if (MAYBE_CLASS_TYPE_P (intype))
841 : : {
842 : 5 : tree rval;
843 : 10 : rval = build_type_conversion (type, e);
844 : 5 : if (rval)
845 : : return rval;
846 : 0 : if (complain & tf_error)
847 : 0 : error_at (loc, "%q#T used where a %qT was expected", intype, type);
848 : 0 : return error_mark_node;
849 : : }
850 : 133271677 : if (code == BOOLEAN_TYPE)
851 : : {
852 : 8124973 : if (VOID_TYPE_P (intype))
853 : : {
854 : 18 : if (complain & tf_error)
855 : 18 : error_at (loc,
856 : : "could not convert %qE from %<void%> to %<bool%>",
857 : : expr);
858 : 18 : return error_mark_node;
859 : : }
860 : :
861 : 8124955 : if (VECTOR_TYPE_P (intype) && !gnu_vector_type_p (intype))
862 : : {
863 : 0 : if (complain & tf_error)
864 : 0 : error_at (loc, "could not convert %qE from %qH to %qI", expr,
865 : 0 : TREE_TYPE (expr), type);
866 : 0 : return error_mark_node;
867 : : }
868 : :
869 : : /* We can't implicitly convert a scoped enum to bool, so convert
870 : : to the underlying type first. */
871 : 8124955 : if (SCOPED_ENUM_P (intype) && (convtype & CONV_STATIC))
872 : 38 : e = build_nop (ENUM_UNDERLYING_TYPE (intype), e);
873 : 8124955 : if (complain & tf_warning)
874 : 4668790 : return cp_truthvalue_conversion (e, complain);
875 : : else
876 : : {
877 : : /* Prevent bogus -Wint-in-bool-context warnings coming
878 : : from c_common_truthvalue_conversion down the line. */
879 : 3456165 : warning_sentinel w (warn_int_in_bool_context);
880 : 3456165 : warning_sentinel c (warn_sign_compare);
881 : 3456165 : return cp_truthvalue_conversion (e, complain);
882 : 3456165 : }
883 : : }
884 : :
885 : 125146704 : converted = convert_to_integer_maybe_fold (type, e, dofold);
886 : :
887 : : /* Ignore any integer overflow caused by the conversion. */
888 : 125146704 : return ignore_overflows (converted, e);
889 : : }
890 : 40246137 : if (INDIRECT_TYPE_P (type) || TYPE_PTRMEM_P (type))
891 : 19352044 : return cp_convert_to_pointer (type, e, dofold, complain);
892 : 20894093 : if (code == VECTOR_TYPE)
893 : : {
894 : 4354 : tree in_vtype = TREE_TYPE (e);
895 : 4354 : if (MAYBE_CLASS_TYPE_P (in_vtype))
896 : : {
897 : 0 : tree ret_val;
898 : 0 : ret_val = build_type_conversion (type, e);
899 : 0 : if (ret_val)
900 : : return ret_val;
901 : 0 : if (complain & tf_error)
902 : 0 : error_at (loc, "%q#T used where a %qT was expected",
903 : : in_vtype, type);
904 : 0 : return error_mark_node;
905 : : }
906 : 4354 : return convert_to_vector (type, rvalue (e));
907 : : }
908 : 20889739 : if (code == REAL_TYPE || code == COMPLEX_TYPE)
909 : : {
910 : 20031095 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (e)))
911 : : {
912 : 0 : tree rval;
913 : 0 : rval = build_type_conversion (type, e);
914 : 0 : if (rval)
915 : : return rval;
916 : 0 : else if (complain & tf_error)
917 : 0 : error_at (loc,
918 : : "%q#T used where a floating-point value was expected",
919 : 0 : TREE_TYPE (e));
920 : : }
921 : 20031095 : if (code == REAL_TYPE)
922 : 19595834 : return convert_to_real_maybe_fold (type, e, dofold);
923 : 435261 : else if (code == COMPLEX_TYPE)
924 : 435261 : return convert_to_complex_maybe_fold (type, e, dofold);
925 : : }
926 : :
927 : : /* New C++ semantics: since assignment is now based on
928 : : memberwise copying, if the rhs type is derived from the
929 : : lhs type, then we may still do a conversion. */
930 : 858644 : if (RECORD_OR_UNION_CODE_P (code))
931 : : {
932 : 858638 : tree dtype = TREE_TYPE (e);
933 : 858638 : tree ctor = NULL_TREE;
934 : :
935 : 858638 : dtype = TYPE_MAIN_VARIANT (dtype);
936 : :
937 : : /* Conversion between aggregate types. New C++ semantics allow
938 : : objects of derived type to be cast to objects of base type.
939 : : Old semantics only allowed this between pointers.
940 : :
941 : : There may be some ambiguity between using a constructor
942 : : vs. using a type conversion operator when both apply. */
943 : :
944 : 858638 : ctor = e;
945 : :
946 : 858638 : if (abstract_virtuals_error (NULL_TREE, type, complain))
947 : 0 : return error_mark_node;
948 : :
949 : 858638 : if (BRACE_ENCLOSED_INITIALIZER_P (ctor))
950 : 21065 : ctor = perform_implicit_conversion (type, ctor, complain);
951 : 837573 : else if ((flags & LOOKUP_ONLYCONVERTING)
952 : 837573 : && ! (CLASS_TYPE_P (dtype) && DERIVED_FROM_P (type, dtype)))
953 : : /* For copy-initialization, first we create a temp of the proper type
954 : : with a user-defined conversion sequence, then we direct-initialize
955 : : the target with the temp (see [dcl.init]). */
956 : 13921 : ctor = build_user_type_conversion (type, ctor, flags, complain);
957 : : else
958 : : {
959 : 823652 : releasing_vec ctor_vec (make_tree_vector_single (ctor));
960 : 823652 : ctor = build_special_member_call (NULL_TREE,
961 : : complete_ctor_identifier,
962 : : &ctor_vec,
963 : : type, flags, complain);
964 : 823652 : }
965 : 858638 : if (ctor)
966 : 858465 : return build_cplus_new (type, ctor, complain);
967 : : }
968 : :
969 : 179 : if (complain & tf_error)
970 : : {
971 : : /* If the conversion failed and expr was an invalid use of pointer to
972 : : member function, try to report a meaningful error. */
973 : 179 : if (invalid_nonstatic_memfn_p (loc, expr, complain))
974 : : /* We displayed the error message. */;
975 : : else
976 : 176 : error_at (loc, "conversion from %qH to non-scalar type %qI requested",
977 : 176 : TREE_TYPE (expr), type);
978 : : }
979 : 179 : return error_mark_node;
980 : : }
981 : :
982 : : /* If CALL is a call, return the callee; otherwise null. */
983 : :
984 : : tree
985 : 661916899 : cp_get_callee (tree call)
986 : : {
987 : 661916899 : if (call == NULL_TREE)
988 : : return call;
989 : 661916899 : else if (TREE_CODE (call) == CALL_EXPR)
990 : 636265129 : return CALL_EXPR_FN (call);
991 : 25651770 : else if (TREE_CODE (call) == AGGR_INIT_EXPR)
992 : 14707041 : return AGGR_INIT_EXPR_FN (call);
993 : : return NULL_TREE;
994 : : }
995 : :
996 : : /* FN is the callee of a CALL_EXPR or AGGR_INIT_EXPR; return the FUNCTION_DECL
997 : : if we can. */
998 : :
999 : : tree
1000 : 524508439 : cp_get_fndecl_from_callee (tree fn, bool fold /* = true */)
1001 : : {
1002 : 524508439 : if (fn == NULL_TREE)
1003 : : return fn;
1004 : :
1005 : : /* We evaluate constexpr functions on the original, pre-genericization
1006 : : bodies. So block-scope extern declarations have not been mapped to
1007 : : declarations in outer scopes. Use the namespace-scope declaration,
1008 : : if any, so that retrieve_constexpr_fundef can find it (PR111132). */
1009 : 999359777 : auto fn_or_local_alias = [] (tree f)
1010 : : {
1011 : 476274669 : if (DECL_LOCAL_DECL_P (f))
1012 : 41400 : if (tree alias = DECL_LOCAL_DECL_ALIAS (f))
1013 : 41400 : if (alias != error_mark_node)
1014 : 41400 : return alias;
1015 : : return f;
1016 : : };
1017 : :
1018 : 523085108 : if (TREE_CODE (fn) == FUNCTION_DECL)
1019 : 5771714 : return fn_or_local_alias (fn);
1020 : 517313394 : tree type = TREE_TYPE (fn);
1021 : 517313394 : if (type == NULL_TREE || !INDIRECT_TYPE_P (type))
1022 : : return NULL_TREE;
1023 : 475584192 : if (fold)
1024 : 2261398 : fn = maybe_constant_init (fn);
1025 : 475584192 : STRIP_NOPS (fn);
1026 : 475584192 : if (TREE_CODE (fn) == ADDR_EXPR
1027 : 475584192 : || TREE_CODE (fn) == FDESC_EXPR)
1028 : 470503049 : fn = TREE_OPERAND (fn, 0);
1029 : 475584192 : if (TREE_CODE (fn) == FUNCTION_DECL)
1030 : 470502955 : return fn_or_local_alias (fn);
1031 : : return NULL_TREE;
1032 : : }
1033 : :
1034 : : /* Like get_callee_fndecl, but handles AGGR_INIT_EXPR as well and uses the
1035 : : constexpr machinery. */
1036 : :
1037 : : tree
1038 : 0 : cp_get_callee_fndecl (tree call)
1039 : : {
1040 : 0 : return cp_get_fndecl_from_callee (cp_get_callee (call));
1041 : : }
1042 : :
1043 : : /* As above, but not using the constexpr machinery. */
1044 : :
1045 : : tree
1046 : 219906690 : cp_get_callee_fndecl_nofold (tree call)
1047 : : {
1048 : 219906690 : return cp_get_fndecl_from_callee (cp_get_callee (call), false);
1049 : : }
1050 : :
1051 : : /* Subroutine of convert_to_void. Warn if we're discarding something with
1052 : : attribute [[nodiscard]]. */
1053 : :
1054 : : static void
1055 : 3926046 : maybe_warn_nodiscard (tree expr, impl_conv_void implicit)
1056 : : {
1057 : 3926046 : if (!warn_unused_result || c_inhibit_evaluation_warnings)
1058 : : return;
1059 : :
1060 : 3908198 : tree call = expr;
1061 : 3908198 : if (TREE_CODE (expr) == TARGET_EXPR)
1062 : 28639 : call = TARGET_EXPR_INITIAL (expr);
1063 : 3908198 : location_t loc = cp_expr_loc_or_input_loc (call);
1064 : 3908198 : tree callee = cp_get_callee (call);
1065 : 3908198 : if (!callee || !TREE_TYPE (callee))
1066 : : return;
1067 : :
1068 : 3906657 : tree type = TREE_TYPE (callee);
1069 : 3906657 : if (TYPE_PTRMEMFUNC_P (type))
1070 : 0 : type = TYPE_PTRMEMFUNC_FN_TYPE (type);
1071 : 3906657 : if (INDIRECT_TYPE_P (type))
1072 : 2261398 : type = TREE_TYPE (type);
1073 : 3906657 : if (!FUNC_OR_METHOD_TYPE_P (type))
1074 : : return;
1075 : :
1076 : 3906606 : tree rettype = TREE_TYPE (type);
1077 : 3906606 : tree fn = cp_get_fndecl_from_callee (callee);
1078 : 3906606 : tree attr;
1079 : 3906606 : if (implicit != ICV_CAST && fn
1080 : 3906606 : && (attr = lookup_attribute ("nodiscard", DECL_ATTRIBUTES (fn))))
1081 : : {
1082 : 395 : escaped_string msg;
1083 : 395 : tree args = TREE_VALUE (attr);
1084 : 395 : if (args)
1085 : 57 : msg.escape (TREE_STRING_POINTER (TREE_VALUE (args)));
1086 : 395 : const char *format
1087 : 395 : = (msg
1088 : 395 : ? G_("ignoring return value of %qD, "
1089 : : "declared with attribute %<nodiscard%>: %<%s%>")
1090 : : : G_("ignoring return value of %qD, "
1091 : 395 : "declared with attribute %<nodiscard%>%s"));
1092 : 395 : const char *raw_msg = msg ? (const char *) msg : "";
1093 : 395 : auto_diagnostic_group d;
1094 : 395 : if (warning_at (loc, OPT_Wunused_result, format, fn, raw_msg))
1095 : 395 : inform (DECL_SOURCE_LOCATION (fn), "declared here");
1096 : 395 : }
1097 : 3906211 : else if (implicit != ICV_CAST
1098 : 3906211 : && (attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype))))
1099 : : {
1100 : 117 : escaped_string msg;
1101 : 117 : tree args = TREE_VALUE (attr);
1102 : 117 : if (args)
1103 : 54 : msg.escape (TREE_STRING_POINTER (TREE_VALUE (args)));
1104 : 117 : const char *format
1105 : 117 : = (msg
1106 : 117 : ? G_("ignoring returned value of type %qT, "
1107 : : "declared with attribute %<nodiscard%>: %<%s%>")
1108 : : : G_("ignoring returned value of type %qT, "
1109 : 117 : "declared with attribute %<nodiscard%>%s"));
1110 : 117 : const char *raw_msg = msg ? (const char *) msg : "";
1111 : 117 : auto_diagnostic_group d;
1112 : 117 : if (warning_at (loc, OPT_Wunused_result, format, rettype, raw_msg))
1113 : : {
1114 : 117 : if (fn)
1115 : 18 : inform (DECL_SOURCE_LOCATION (fn),
1116 : : "in call to %qD, declared here", fn);
1117 : 117 : inform (DECL_SOURCE_LOCATION (TYPE_NAME (rettype)),
1118 : : "%qT declared here", rettype);
1119 : : }
1120 : 117 : }
1121 : 3906094 : else if (TREE_CODE (expr) == TARGET_EXPR
1122 : 3906094 : && lookup_attribute ("warn_unused_result", TYPE_ATTRIBUTES (type)))
1123 : : {
1124 : : /* The TARGET_EXPR confuses do_warn_unused_result into thinking that the
1125 : : result is used, so handle that case here. */
1126 : 60 : if (fn)
1127 : : {
1128 : 60 : auto_diagnostic_group d;
1129 : 60 : if (warning_at (loc, OPT_Wunused_result,
1130 : : "ignoring return value of %qD, "
1131 : : "declared with attribute %<warn_unused_result%>",
1132 : : fn))
1133 : 60 : inform (DECL_SOURCE_LOCATION (fn), "declared here");
1134 : 60 : }
1135 : : else
1136 : 0 : warning_at (loc, OPT_Wunused_result,
1137 : : "ignoring return value of function "
1138 : : "declared with attribute %<warn_unused_result%>");
1139 : : }
1140 : : }
1141 : :
1142 : : /* When an expression is used in a void context, its value is discarded and
1143 : : no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
1144 : : stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
1145 : : in a void context. The C++ standard does not define what an `access' to an
1146 : : object is, but there is reason to believe that it is the lvalue to rvalue
1147 : : conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
1148 : : accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
1149 : : indicates that volatile semantics should be the same between C and C++
1150 : : where ever possible. C leaves it implementation defined as to what
1151 : : constitutes an access to a volatile. So, we interpret `*vp' as a read of
1152 : : the volatile object `vp' points to, unless that is an incomplete type. For
1153 : : volatile references we do not do this interpretation, because that would
1154 : : make it impossible to ignore the reference return value from functions. We
1155 : : issue warnings in the confusing cases.
1156 : :
1157 : : The IMPLICIT is ICV_CAST when the user is explicitly converting an expression
1158 : : to void via a cast. If an expression is being implicitly converted, IMPLICIT
1159 : : indicates the context of the implicit conversion. */
1160 : :
1161 : : tree
1162 : 76502964 : convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain)
1163 : : {
1164 : 76502964 : location_t loc = cp_expr_loc_or_input_loc (expr);
1165 : :
1166 : 76502964 : if (expr == error_mark_node
1167 : 76502964 : || TREE_TYPE (expr) == error_mark_node)
1168 : : return error_mark_node;
1169 : :
1170 : 76332556 : expr = maybe_undo_parenthesized_ref (expr);
1171 : :
1172 : 76332556 : expr = mark_discarded_use (expr);
1173 : 76332556 : if (implicit == ICV_CAST)
1174 : : /* An explicit cast to void avoids all -Wunused-but-set* warnings. */
1175 : 289880 : mark_exp_read (expr);
1176 : :
1177 : 76332556 : if (!TREE_TYPE (expr))
1178 : : return expr;
1179 : 76332316 : if (invalid_nonstatic_memfn_p (loc, expr, complain))
1180 : 20 : return error_mark_node;
1181 : 76332296 : if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
1182 : : {
1183 : 6 : if (complain & tf_error)
1184 : 6 : error_at (loc, "pseudo-destructor is not called");
1185 : 6 : return error_mark_node;
1186 : : }
1187 : :
1188 : : /* Explicitly evaluate void-converted concept checks since their
1189 : : satisfaction may produce ill-formed programs. */
1190 : 76332290 : if (concept_check_p (expr))
1191 : 29 : expr = evaluate_concept_check (expr);
1192 : :
1193 : 76332290 : if (VOID_TYPE_P (TREE_TYPE (expr)))
1194 : : return expr;
1195 : 47480346 : switch (TREE_CODE (expr))
1196 : : {
1197 : 5105 : case COND_EXPR:
1198 : 5105 : {
1199 : : /* The two parts of a cond expr might be separate lvalues. */
1200 : 5105 : tree op1 = TREE_OPERAND (expr,1);
1201 : 5105 : tree op2 = TREE_OPERAND (expr,2);
1202 : 5102 : bool side_effects = ((op1 && TREE_SIDE_EFFECTS (op1))
1203 : 5414 : || TREE_SIDE_EFFECTS (op2));
1204 : 5105 : tree new_op1, new_op2;
1205 : 5105 : new_op1 = NULL_TREE;
1206 : 5105 : if (implicit != ICV_CAST && !side_effects)
1207 : : {
1208 : 69 : if (op1)
1209 : 66 : new_op1 = convert_to_void (op1, ICV_SECOND_OF_COND, complain);
1210 : 69 : new_op2 = convert_to_void (op2, ICV_THIRD_OF_COND, complain);
1211 : : }
1212 : : else
1213 : : {
1214 : 5036 : if (op1)
1215 : 5036 : new_op1 = convert_to_void (op1, ICV_CAST, complain);
1216 : 5036 : new_op2 = convert_to_void (op2, ICV_CAST, complain);
1217 : : }
1218 : :
1219 : 5105 : expr = build3_loc (loc, COND_EXPR, TREE_TYPE (new_op2),
1220 : 5105 : TREE_OPERAND (expr, 0), new_op1, new_op2);
1221 : 5105 : break;
1222 : : }
1223 : :
1224 : 307183 : case COMPOUND_EXPR:
1225 : 307183 : {
1226 : : /* The second part of a compound expr contains the value. */
1227 : 307183 : tree op1 = TREE_OPERAND (expr,1);
1228 : 307183 : tree new_op1;
1229 : 307183 : if (implicit != ICV_CAST && !warning_suppressed_p (expr /* What warning? */))
1230 : 187206 : new_op1 = convert_to_void (op1, ICV_RIGHT_OF_COMMA, complain);
1231 : : else
1232 : 119977 : new_op1 = convert_to_void (op1, ICV_CAST, complain);
1233 : :
1234 : 307183 : if (new_op1 != op1)
1235 : : {
1236 : 307183 : tree t = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (new_op1),
1237 : 307183 : TREE_OPERAND (expr, 0), new_op1);
1238 : 307183 : expr = t;
1239 : : }
1240 : :
1241 : : break;
1242 : : }
1243 : :
1244 : : case NON_LVALUE_EXPR:
1245 : : case NOP_EXPR:
1246 : : /* These have already decayed to rvalue. */
1247 : : break;
1248 : :
1249 : 2135049 : case CALL_EXPR: /* We have a special meaning for volatile void fn(). */
1250 : : /* cdtors may return this or void, depending on
1251 : : targetm.cxx.cdtor_returns_this, but this shouldn't affect our
1252 : : decisions here: neither nodiscard warnings (nodiscard dtors
1253 : : are nonsensical and ctors have a different behavior with that
1254 : : attribute that is handled in the TARGET_EXPR case), nor should
1255 : : any constexpr or template instantiations be affected by an ABI
1256 : : property that is, or at least ought to be transparent to the
1257 : : language. */
1258 : 2135049 : if (tree fn = cp_get_callee_fndecl_nofold (expr))
1259 : 3694774 : if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
1260 : : return expr;
1261 : :
1262 : 2135049 : if (complain & tf_warning)
1263 : 1652423 : maybe_warn_nodiscard (expr, implicit);
1264 : : break;
1265 : :
1266 : 3283443 : case INDIRECT_REF:
1267 : 3283443 : {
1268 : 3283443 : tree type = TREE_TYPE (expr);
1269 : 3283443 : int is_reference = TYPE_REF_P (TREE_TYPE (TREE_OPERAND (expr, 0)));
1270 : 3283443 : int is_volatile = TYPE_VOLATILE (type);
1271 : 3283443 : if (is_volatile)
1272 : 328 : complete_type (type);
1273 : 3283443 : int is_complete = COMPLETE_TYPE_P (type);
1274 : :
1275 : : /* Can't load the value if we don't know the type. */
1276 : 3283443 : if (is_volatile && !is_complete)
1277 : : {
1278 : 39 : if (complain & tf_warning)
1279 : 39 : switch (implicit)
1280 : : {
1281 : 24 : case ICV_CAST:
1282 : 24 : warning_at (loc, 0, "conversion to void will not access "
1283 : : "object of incomplete type %qT", type);
1284 : 24 : break;
1285 : 0 : case ICV_SECOND_OF_COND:
1286 : 0 : warning_at (loc, 0, "indirection will not access object of "
1287 : : "incomplete type %qT in second operand "
1288 : : "of conditional expression", type);
1289 : 0 : break;
1290 : 0 : case ICV_THIRD_OF_COND:
1291 : 0 : warning_at (loc, 0, "indirection will not access object of "
1292 : : "incomplete type %qT in third operand "
1293 : : "of conditional expression", type);
1294 : 0 : break;
1295 : 0 : case ICV_RIGHT_OF_COMMA:
1296 : 0 : warning_at (loc, 0, "indirection will not access object of "
1297 : : "incomplete type %qT in right operand of "
1298 : : "comma operator", type);
1299 : 0 : break;
1300 : 0 : case ICV_LEFT_OF_COMMA:
1301 : 0 : warning_at (loc, 0, "indirection will not access object of "
1302 : : "incomplete type %qT in left operand of "
1303 : : "comma operator", type);
1304 : 0 : break;
1305 : 15 : case ICV_STATEMENT:
1306 : 15 : warning_at (loc, 0, "indirection will not access object of "
1307 : : "incomplete type %qT in statement", type);
1308 : 15 : break;
1309 : 0 : case ICV_THIRD_IN_FOR:
1310 : 0 : warning_at (loc, 0, "indirection will not access object of "
1311 : : "incomplete type %qT in for increment "
1312 : : "expression", type);
1313 : 0 : break;
1314 : 0 : default:
1315 : 0 : gcc_unreachable ();
1316 : : }
1317 : : }
1318 : : /* Don't load the value if this is an implicit dereference, or if
1319 : : the type needs to be handled by ctors/dtors. */
1320 : 3283404 : else if (is_volatile && is_reference)
1321 : : {
1322 : 116 : if (complain & tf_warning)
1323 : 42 : switch (implicit)
1324 : : {
1325 : 24 : case ICV_CAST:
1326 : 24 : warning_at (loc, 0, "conversion to void will not access "
1327 : : "object of type %qT", type);
1328 : 24 : break;
1329 : 0 : case ICV_SECOND_OF_COND:
1330 : 0 : warning_at (loc, 0, "implicit dereference will not access "
1331 : : "object of type %qT in second operand of "
1332 : : "conditional expression", type);
1333 : 0 : break;
1334 : 0 : case ICV_THIRD_OF_COND:
1335 : 0 : warning_at (loc, 0, "implicit dereference will not access "
1336 : : "object of type %qT in third operand of "
1337 : : "conditional expression", type);
1338 : 0 : break;
1339 : 0 : case ICV_RIGHT_OF_COMMA:
1340 : 0 : warning_at (loc, 0, "implicit dereference will not access "
1341 : : "object of type %qT in right operand of "
1342 : : "comma operator", type);
1343 : 0 : break;
1344 : 0 : case ICV_LEFT_OF_COMMA:
1345 : 0 : warning_at (loc, 0, "implicit dereference will not access "
1346 : : "object of type %qT in left operand of comma "
1347 : : "operator", type);
1348 : 0 : break;
1349 : 18 : case ICV_STATEMENT:
1350 : 18 : warning_at (loc, 0, "implicit dereference will not access "
1351 : : "object of type %qT in statement", type);
1352 : 18 : break;
1353 : 0 : case ICV_THIRD_IN_FOR:
1354 : 0 : warning_at (loc, 0, "implicit dereference will not access "
1355 : : "object of type %qT in for increment expression",
1356 : : type);
1357 : 0 : break;
1358 : 0 : default:
1359 : 0 : gcc_unreachable ();
1360 : : }
1361 : : }
1362 : 3283288 : else if (is_volatile && TREE_ADDRESSABLE (type))
1363 : : {
1364 : 3 : if (complain & tf_warning)
1365 : 3 : switch (implicit)
1366 : : {
1367 : 0 : case ICV_CAST:
1368 : 0 : warning_at (loc, 0, "conversion to void will not access "
1369 : : "object of non-trivially-copyable type %qT",
1370 : : type);
1371 : 0 : break;
1372 : 0 : case ICV_SECOND_OF_COND:
1373 : 0 : warning_at (loc, 0, "indirection will not access object of "
1374 : : "non-trivially-copyable type %qT in second "
1375 : : "operand of conditional expression", type);
1376 : 0 : break;
1377 : 0 : case ICV_THIRD_OF_COND:
1378 : 0 : warning_at (loc, 0, "indirection will not access object of "
1379 : : "non-trivially-copyable type %qT in third "
1380 : : "operand of conditional expression", type);
1381 : 0 : break;
1382 : 0 : case ICV_RIGHT_OF_COMMA:
1383 : 0 : warning_at (loc, 0, "indirection will not access object of "
1384 : : "non-trivially-copyable type %qT in right "
1385 : : "operand of comma operator", type);
1386 : 0 : break;
1387 : 0 : case ICV_LEFT_OF_COMMA:
1388 : 0 : warning_at (loc, 0, "indirection will not access object of "
1389 : : "non-trivially-copyable type %qT in left "
1390 : : "operand of comma operator", type);
1391 : 0 : break;
1392 : 3 : case ICV_STATEMENT:
1393 : 3 : warning_at (loc, 0, "indirection will not access object of "
1394 : : "non-trivially-copyable type %qT in statement",
1395 : : type);
1396 : 3 : break;
1397 : 0 : case ICV_THIRD_IN_FOR:
1398 : 0 : warning_at (loc, 0, "indirection will not access object of "
1399 : : "non-trivially-copyable type %qT in for "
1400 : : "increment expression", type);
1401 : 0 : break;
1402 : 0 : default:
1403 : 0 : gcc_unreachable ();
1404 : : }
1405 : : }
1406 : 3283443 : if (is_reference || !is_volatile || !is_complete || TREE_ADDRESSABLE (type))
1407 : : {
1408 : : /* Emit a warning (if enabled) when the "effect-less" INDIRECT_REF
1409 : : operation is stripped off. Note that we don't warn about
1410 : : - an expression with TREE_NO_WARNING set. (For an example of
1411 : : such expressions, see build_over_call in call.cc.)
1412 : : - automatic dereferencing of references, since the user cannot
1413 : : control it. (See also warn_if_unused_value() in c-common.cc.) */
1414 : 3283273 : if (warn_unused_value
1415 : 30569 : && implicit != ICV_CAST
1416 : 30027 : && (complain & tf_warning)
1417 : 24664 : && !warning_suppressed_p (expr, OPT_Wunused_value)
1418 : 3306986 : && !is_reference)
1419 : 9 : warning_at (loc, OPT_Wunused_value, "value computed is not used");
1420 : 3283273 : expr = TREE_OPERAND (expr, 0);
1421 : 3283273 : if (TREE_CODE (expr) == CALL_EXPR
1422 : 2580704 : && (complain & tf_warning))
1423 : 2244841 : maybe_warn_nodiscard (expr, implicit);
1424 : : }
1425 : :
1426 : : break;
1427 : : }
1428 : :
1429 : 12937 : case VAR_DECL:
1430 : 12937 : {
1431 : : /* External variables might be incomplete. */
1432 : 12937 : tree type = TREE_TYPE (expr);
1433 : :
1434 : 12937 : if (TYPE_VOLATILE (type)
1435 : 138 : && !COMPLETE_TYPE_P (complete_type (type))
1436 : 12952 : && (complain & tf_warning))
1437 : 15 : switch (implicit)
1438 : : {
1439 : 12 : case ICV_CAST:
1440 : 12 : warning_at (loc, 0, "conversion to void will not access "
1441 : : "object %qE of incomplete type %qT", expr, type);
1442 : 12 : break;
1443 : 0 : case ICV_SECOND_OF_COND:
1444 : 0 : warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1445 : : "not be accessed in second operand of "
1446 : : "conditional expression", expr, type);
1447 : 0 : break;
1448 : 0 : case ICV_THIRD_OF_COND:
1449 : 0 : warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1450 : : "not be accessed in third operand of "
1451 : : "conditional expression", expr, type);
1452 : 0 : break;
1453 : 0 : case ICV_RIGHT_OF_COMMA:
1454 : 0 : warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1455 : : "not be accessed in right operand of comma operator",
1456 : : expr, type);
1457 : 0 : break;
1458 : 0 : case ICV_LEFT_OF_COMMA:
1459 : 0 : warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1460 : : "not be accessed in left operand of comma operator",
1461 : : expr, type);
1462 : 0 : break;
1463 : 3 : case ICV_STATEMENT:
1464 : 3 : warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1465 : : "not be accessed in statement", expr, type);
1466 : 3 : break;
1467 : 0 : case ICV_THIRD_IN_FOR:
1468 : 0 : warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1469 : : "not be accessed in for increment expression",
1470 : : expr, type);
1471 : 0 : break;
1472 : 0 : default:
1473 : 0 : gcc_unreachable ();
1474 : : }
1475 : :
1476 : : break;
1477 : : }
1478 : :
1479 : 586693 : case TARGET_EXPR:
1480 : : /* Don't bother with the temporary object returned from a function if
1481 : : we don't use it, don't need to destroy it, and won't abort in
1482 : : assign_temp. We'll still
1483 : : allocate space for it in expand_call or declare_return_variable,
1484 : : but we don't need to track it through all the tree phases. */
1485 : 586693 : if (TARGET_EXPR_IMPLICIT_P (expr)
1486 : 586693 : && !TREE_ADDRESSABLE (TREE_TYPE (expr)))
1487 : : {
1488 : 285565 : tree init = TARGET_EXPR_INITIAL (expr);
1489 : 285565 : if (TREE_CODE (init) == AGGR_INIT_EXPR
1490 : 285565 : && !AGGR_INIT_VIA_CTOR_P (init))
1491 : : {
1492 : 0 : tree fn = AGGR_INIT_EXPR_FN (init);
1493 : 0 : expr = build_call_array_loc (input_location,
1494 : 0 : TREE_TYPE (TREE_TYPE
1495 : : (TREE_TYPE (fn))),
1496 : : fn,
1497 : 0 : aggr_init_expr_nargs (init),
1498 : 0 : AGGR_INIT_EXPR_ARGP (init));
1499 : : }
1500 : : }
1501 : 586693 : if (complain & tf_warning)
1502 : 28782 : maybe_warn_nodiscard (expr, implicit);
1503 : : break;
1504 : :
1505 : 50 : case CO_AWAIT_EXPR:
1506 : 50 : {
1507 : 50 : auto awr = co_await_get_resume_call (expr);
1508 : 50 : if (awr && *awr)
1509 : 50 : *awr = convert_to_void (*awr, implicit, complain);
1510 : : break;
1511 : : }
1512 : :
1513 : 47480346 : default:;
1514 : : }
1515 : 47480346 : expr = resolve_nondeduced_context (expr, complain);
1516 : 47480346 : if (!mark_single_function (expr, complain))
1517 : 3 : return error_mark_node;
1518 : :
1519 : 47480343 : {
1520 : 47480343 : tree probe = expr;
1521 : :
1522 : 47480343 : if (TREE_CODE (probe) == ADDR_EXPR)
1523 : 1040 : probe = TREE_OPERAND (expr, 0);
1524 : 47480343 : if (type_unknown_p (probe))
1525 : : {
1526 : : /* [over.over] enumerates the places where we can take the address
1527 : : of an overloaded function, and this is not one of them. */
1528 : 55 : if (complain & tf_error)
1529 : 36 : switch (implicit)
1530 : : {
1531 : 12 : case ICV_CAST:
1532 : 12 : error_at (loc, "conversion to void "
1533 : : "cannot resolve address of overloaded function");
1534 : 12 : break;
1535 : 0 : case ICV_SECOND_OF_COND:
1536 : 0 : error_at (loc, "second operand of conditional expression "
1537 : : "cannot resolve address of overloaded function");
1538 : 0 : break;
1539 : 0 : case ICV_THIRD_OF_COND:
1540 : 0 : error_at (loc, "third operand of conditional expression "
1541 : : "cannot resolve address of overloaded function");
1542 : 0 : break;
1543 : 0 : case ICV_RIGHT_OF_COMMA:
1544 : 0 : error_at (loc, "right operand of comma operator "
1545 : : "cannot resolve address of overloaded function");
1546 : 0 : break;
1547 : 0 : case ICV_LEFT_OF_COMMA:
1548 : 0 : error_at (loc, "left operand of comma operator "
1549 : : "cannot resolve address of overloaded function");
1550 : 0 : break;
1551 : 24 : case ICV_STATEMENT:
1552 : 24 : error_at (loc, "statement "
1553 : : "cannot resolve address of overloaded function");
1554 : 24 : break;
1555 : 0 : case ICV_THIRD_IN_FOR:
1556 : 0 : error_at (loc, "for increment expression "
1557 : : "cannot resolve address of overloaded function");
1558 : 0 : break;
1559 : : }
1560 : : else
1561 : 19 : return error_mark_node;
1562 : 36 : expr = void_node;
1563 : : }
1564 : 47480288 : else if (implicit != ICV_CAST && probe == expr && is_overloaded_fn (probe))
1565 : : {
1566 : : /* Only warn when there is no &. */
1567 : 102 : if (complain & tf_warning)
1568 : 102 : switch (implicit)
1569 : : {
1570 : 3 : case ICV_SECOND_OF_COND:
1571 : 3 : warning_at (loc, OPT_Waddress,
1572 : : "second operand of conditional expression "
1573 : : "is a reference, not call, to function %qE", expr);
1574 : 3 : break;
1575 : 3 : case ICV_THIRD_OF_COND:
1576 : 3 : warning_at (loc, OPT_Waddress,
1577 : : "third operand of conditional expression "
1578 : : "is a reference, not call, to function %qE", expr);
1579 : 3 : break;
1580 : 0 : case ICV_RIGHT_OF_COMMA:
1581 : 0 : warning_at (loc, OPT_Waddress,
1582 : : "right operand of comma operator "
1583 : : "is a reference, not call, to function %qE", expr);
1584 : 0 : break;
1585 : 9 : case ICV_LEFT_OF_COMMA:
1586 : 9 : warning_at (loc, OPT_Waddress,
1587 : : "left operand of comma operator "
1588 : : "is a reference, not call, to function %qE", expr);
1589 : 9 : break;
1590 : 87 : case ICV_STATEMENT:
1591 : 87 : warning_at (loc, OPT_Waddress,
1592 : : "statement is a reference, not call, to function %qE",
1593 : : expr);
1594 : 87 : break;
1595 : 0 : case ICV_THIRD_IN_FOR:
1596 : 0 : warning_at (loc, OPT_Waddress,
1597 : : "for increment expression "
1598 : : "is a reference, not call, to function %qE", expr);
1599 : 0 : break;
1600 : 0 : default:
1601 : 0 : gcc_unreachable ();
1602 : : }
1603 : :
1604 : 102 : if (TREE_CODE (expr) == COMPONENT_REF)
1605 : 9 : expr = TREE_OPERAND (expr, 0);
1606 : : }
1607 : : }
1608 : :
1609 : 47480324 : if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
1610 : : {
1611 : 47168000 : if (implicit != ICV_CAST
1612 : 46886107 : && warn_unused_value
1613 : 618293 : && !warning_suppressed_p (expr, OPT_Wunused_value)
1614 : 617328 : && !processing_template_decl
1615 : 493742 : && !cp_unevaluated_operand
1616 : 47638908 : && (complain & tf_warning))
1617 : : {
1618 : : /* The middle end does not warn about expressions that have
1619 : : been explicitly cast to void, so we must do so here. */
1620 : 469938 : if (!TREE_SIDE_EFFECTS (expr))
1621 : : {
1622 : 93 : switch (implicit)
1623 : : {
1624 : 3 : case ICV_SECOND_OF_COND:
1625 : 3 : warning_at (loc, OPT_Wunused_value,
1626 : : "second operand of conditional expression "
1627 : : "has no effect");
1628 : 3 : break;
1629 : 3 : case ICV_THIRD_OF_COND:
1630 : 3 : warning_at (loc, OPT_Wunused_value,
1631 : : "third operand of conditional expression "
1632 : : "has no effect");
1633 : 3 : break;
1634 : 36 : case ICV_RIGHT_OF_COMMA:
1635 : 36 : warning_at (loc, OPT_Wunused_value,
1636 : : "right operand of comma operator has no effect");
1637 : 36 : break;
1638 : 12 : case ICV_LEFT_OF_COMMA:
1639 : 12 : warning_at (loc, OPT_Wunused_value,
1640 : : "left operand of comma operator has no effect");
1641 : 12 : break;
1642 : 39 : case ICV_STATEMENT:
1643 : 39 : warning_at (loc, OPT_Wunused_value,
1644 : : "statement has no effect");
1645 : 39 : break;
1646 : 0 : case ICV_THIRD_IN_FOR:
1647 : 0 : warning_at (loc, OPT_Wunused_value,
1648 : : "for increment expression has no effect");
1649 : 0 : break;
1650 : 0 : default:
1651 : 0 : gcc_unreachable ();
1652 : : }
1653 : : }
1654 : : else
1655 : : {
1656 : : tree e = expr;
1657 : : /* We might like to warn about (say) "(int) f()", as the
1658 : : cast has no effect, but the compiler itself will
1659 : : generate implicit conversions under some
1660 : : circumstances. (For example a block copy will be
1661 : : turned into a call to "__builtin_memcpy", with a
1662 : : conversion of the return value to an appropriate
1663 : : type.) So, to avoid false positives, we strip
1664 : : conversions. Do not use STRIP_NOPs because it will
1665 : : not strip conversions to "void", as that is not a
1666 : : mode-preserving conversion. */
1667 : 470200 : while (TREE_CODE (e) == NOP_EXPR)
1668 : 355 : e = TREE_OPERAND (e, 0);
1669 : :
1670 : 469845 : enum tree_code code = TREE_CODE (e);
1671 : 469845 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
1672 : 469845 : if (tclass == tcc_comparison
1673 : : || tclass == tcc_unary
1674 : 469845 : || tclass == tcc_binary
1675 : 469770 : || code == TRUTH_NOT_EXPR
1676 : 469770 : || code == ADDR_EXPR
1677 : : || code == VEC_PERM_EXPR
1678 : 469763 : || code == VEC_COND_EXPR)
1679 : 88 : warn_if_unused_value (e, loc);
1680 : : }
1681 : : }
1682 : 47167997 : expr = build1 (CONVERT_EXPR, void_type_node, expr);
1683 : : }
1684 : 47480321 : if (! TREE_SIDE_EFFECTS (expr))
1685 : 899273 : expr = void_node;
1686 : : return expr;
1687 : : }
1688 : :
1689 : : /* Create an expression whose value is that of EXPR,
1690 : : converted to type TYPE. The TREE_TYPE of the value
1691 : : is always TYPE. This function implements all reasonable
1692 : : conversions; callers should filter out those that are
1693 : : not permitted by the language being compiled.
1694 : :
1695 : : Most of this routine is from build_reinterpret_cast.
1696 : :
1697 : : The back end cannot call cp_convert (what was convert) because
1698 : : conversions to/from basetypes may involve memory references
1699 : : (vbases) and adding or subtracting small values (multiple
1700 : : inheritance), but it calls convert from the constant folding code
1701 : : on subtrees of already built trees after it has ripped them apart.
1702 : :
1703 : : Also, if we ever support range variables, we'll probably also have to
1704 : : do a little bit more work. */
1705 : :
1706 : : tree
1707 : 158387072 : convert (tree type, tree expr)
1708 : : {
1709 : 158387072 : tree intype;
1710 : :
1711 : 158387072 : if (type == error_mark_node || expr == error_mark_node)
1712 : : return error_mark_node;
1713 : :
1714 : 158384970 : intype = TREE_TYPE (expr);
1715 : :
1716 : 158384970 : if (INDIRECT_TYPE_P (type) && INDIRECT_TYPE_P (intype))
1717 : 34191387 : return build_nop (type, expr);
1718 : :
1719 : 124193583 : return ocp_convert (type, expr, CONV_BACKEND_CONVERT,
1720 : : LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
1721 : 124193583 : tf_warning_or_error);
1722 : : }
1723 : :
1724 : : /* Like convert, but in a static initializer (called from
1725 : : convert_and_check). */
1726 : :
1727 : : tree
1728 : 0 : convert_init (tree type, tree expr)
1729 : : {
1730 : 0 : return convert (type, expr);
1731 : : }
1732 : :
1733 : : /* Like cp_convert, except permit conversions to take place which
1734 : : are not normally allowed due to access restrictions
1735 : : (such as conversion from sub-type to private super-type). */
1736 : :
1737 : : tree
1738 : 7551553 : convert_force (tree type, tree expr, int convtype, tsubst_flags_t complain)
1739 : : {
1740 : 7551553 : tree e = expr;
1741 : 7551553 : enum tree_code code = TREE_CODE (type);
1742 : :
1743 : 7551553 : if (code == REFERENCE_TYPE)
1744 : 0 : return convert_to_reference (type, e, CONV_C_CAST, 0,
1745 : 0 : NULL_TREE, complain);
1746 : :
1747 : 7551553 : if (code == POINTER_TYPE)
1748 : 7551553 : return convert_to_pointer_force (type, e, complain);
1749 : :
1750 : : /* From typeck.cc convert_for_assignment */
1751 : 0 : if (((TYPE_PTR_P (TREE_TYPE (e)) && TREE_CODE (e) == ADDR_EXPR
1752 : 0 : && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1753 : 0 : || integer_zerop (e)
1754 : 0 : || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1755 : 0 : && TYPE_PTRMEMFUNC_P (type))
1756 : : /* compatible pointer to member functions. */
1757 : 0 : return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1,
1758 : 0 : /*c_cast_p=*/1, complain);
1759 : :
1760 : 0 : return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL, complain);
1761 : : }
1762 : :
1763 : : /* Convert an aggregate EXPR to type XTYPE. If a conversion
1764 : : exists, return the attempted conversion. This may
1765 : : return ERROR_MARK_NODE if the conversion is not
1766 : : allowed (references private members, etc).
1767 : : If no conversion exists, NULL_TREE is returned.
1768 : :
1769 : : FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1770 : : object parameter, or by the second standard conversion sequence if
1771 : : that doesn't do it. This will probably wait for an overloading rewrite.
1772 : : (jason 8/9/95) */
1773 : :
1774 : : static tree
1775 : 8 : build_type_conversion (tree xtype, tree expr)
1776 : : {
1777 : : /* C++: check to see if we can convert this aggregate type
1778 : : into the required type. */
1779 : 8 : return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL,
1780 : 3 : tf_warning_or_error);
1781 : : }
1782 : :
1783 : : /* Convert the given EXPR to one of a group of types suitable for use in an
1784 : : expression. DESIRES is a combination of various WANT_* flags (q.v.)
1785 : : which indicates which types are suitable. If COMPLAIN is true, complain
1786 : : about ambiguity; otherwise, the caller will deal with it. */
1787 : :
1788 : : tree
1789 : 42323118 : build_expr_type_conversion (int desires, tree expr, bool complain)
1790 : : {
1791 : 42323118 : tree basetype = TREE_TYPE (expr);
1792 : 42323118 : tree conv = NULL_TREE;
1793 : 42323118 : tree winner = NULL_TREE;
1794 : :
1795 : 42323118 : if (null_node_p (expr)
1796 : : && (desires & WANT_INT)
1797 : 42323118 : && !(desires & WANT_NULL))
1798 : : {
1799 : 36 : location_t loc =
1800 : 36 : expansion_point_location_if_in_system_header (input_location);
1801 : :
1802 : 36 : warning_at (loc, OPT_Wconversion_null,
1803 : : "converting NULL to non-pointer type");
1804 : : }
1805 : :
1806 : 42323118 : if (basetype == error_mark_node)
1807 : : return error_mark_node;
1808 : :
1809 : 42323106 : if (! MAYBE_CLASS_TYPE_P (basetype))
1810 : 42322907 : switch (TREE_CODE (basetype))
1811 : : {
1812 : 33462416 : case INTEGER_TYPE:
1813 : 33462416 : if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
1814 : : return expr;
1815 : : /* fall through. */
1816 : :
1817 : 33491547 : case BOOLEAN_TYPE:
1818 : 36474064 : return (desires & WANT_INT) ? expr : NULL_TREE;
1819 : 65702 : case ENUMERAL_TYPE:
1820 : 67378 : return (desires & WANT_ENUM) ? expr : NULL_TREE;
1821 : 1738784 : case REAL_TYPE:
1822 : 1738823 : return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1823 : 5912720 : case POINTER_TYPE:
1824 : 7816035 : return (desires & WANT_POINTER) ? expr : NULL_TREE;
1825 : :
1826 : 1044035 : case FUNCTION_TYPE:
1827 : 1044035 : case ARRAY_TYPE:
1828 : 1044035 : return (desires & WANT_POINTER) ? decay_conversion (expr,
1829 : : tf_warning_or_error)
1830 : : : NULL_TREE;
1831 : :
1832 : 54699 : case VECTOR_TYPE:
1833 : 54699 : if (!gnu_vector_type_p (basetype))
1834 : : return NULL_TREE;
1835 : : /* FALLTHROUGH */
1836 : 70059 : case COMPLEX_TYPE:
1837 : 70059 : if ((desires & WANT_VECTOR_OR_COMPLEX) == 0)
1838 : : return NULL_TREE;
1839 : 33032 : switch (TREE_CODE (TREE_TYPE (basetype)))
1840 : : {
1841 : 389 : case INTEGER_TYPE:
1842 : 389 : case BOOLEAN_TYPE:
1843 : 389 : return (desires & WANT_INT) ? expr : NULL_TREE;
1844 : 0 : case ENUMERAL_TYPE:
1845 : 0 : return (desires & WANT_ENUM) ? expr : NULL_TREE;
1846 : 32643 : case REAL_TYPE:
1847 : 32649 : return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1848 : : default:
1849 : : return NULL_TREE;
1850 : : }
1851 : :
1852 : : default:
1853 : : return NULL_TREE;
1854 : : }
1855 : :
1856 : : /* The code for conversions from class type is currently only used for
1857 : : delete expressions. Other expressions are handled by build_new_op. */
1858 : 199 : if (!complete_type_or_maybe_complain (basetype, expr, complain))
1859 : 0 : return error_mark_node;
1860 : 199 : if (!TYPE_HAS_CONVERSION (basetype))
1861 : : return NULL_TREE;
1862 : :
1863 : 401 : for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1864 : : {
1865 : 217 : int win = 0;
1866 : 217 : tree candidate;
1867 : 217 : tree cand = TREE_VALUE (conv);
1868 : 217 : cand = OVL_FIRST (cand);
1869 : :
1870 : 217 : if (winner && winner == cand)
1871 : 0 : continue;
1872 : :
1873 : 217 : if (DECL_NONCONVERTING_P (cand))
1874 : 3 : continue;
1875 : :
1876 : 214 : candidate = non_reference (TREE_TYPE (TREE_TYPE (cand)));
1877 : :
1878 : 214 : switch (TREE_CODE (candidate))
1879 : : {
1880 : 160 : case BOOLEAN_TYPE:
1881 : 160 : case INTEGER_TYPE:
1882 : 160 : win = (desires & WANT_INT); break;
1883 : 15 : case ENUMERAL_TYPE:
1884 : 15 : win = (desires & WANT_ENUM); break;
1885 : 3 : case REAL_TYPE:
1886 : 3 : win = (desires & WANT_FLOAT); break;
1887 : 33 : case POINTER_TYPE:
1888 : 33 : win = (desires & WANT_POINTER); break;
1889 : :
1890 : 0 : case COMPLEX_TYPE:
1891 : 0 : case VECTOR_TYPE:
1892 : 0 : if ((desires & WANT_VECTOR_OR_COMPLEX) == 0)
1893 : : break;
1894 : 0 : switch (TREE_CODE (TREE_TYPE (candidate)))
1895 : : {
1896 : 0 : case BOOLEAN_TYPE:
1897 : 0 : case INTEGER_TYPE:
1898 : 0 : win = (desires & WANT_INT); break;
1899 : 0 : case ENUMERAL_TYPE:
1900 : 0 : win = (desires & WANT_ENUM); break;
1901 : 0 : case REAL_TYPE:
1902 : 0 : win = (desires & WANT_FLOAT); break;
1903 : : default:
1904 : : break;
1905 : : }
1906 : : break;
1907 : :
1908 : 3 : default:
1909 : : /* A wildcard could be instantiated to match any desired
1910 : : type, but we can't deduce the template argument. */
1911 : 3 : if (WILDCARD_TYPE_P (candidate))
1912 : : win = true;
1913 : : break;
1914 : : }
1915 : :
1916 : 211 : if (win)
1917 : : {
1918 : 202 : if (TREE_CODE (cand) == TEMPLATE_DECL)
1919 : : {
1920 : 6 : if (complain)
1921 : 6 : error ("default type conversion cannot deduce template"
1922 : : " argument for %qD", cand);
1923 : 6 : return error_mark_node;
1924 : : }
1925 : :
1926 : 196 : if (winner)
1927 : : {
1928 : 15 : tree winner_type
1929 : 15 : = non_reference (TREE_TYPE (TREE_TYPE (winner)));
1930 : :
1931 : 15 : if (!same_type_ignoring_top_level_qualifiers_p (winner_type,
1932 : : candidate))
1933 : : {
1934 : 3 : if (complain)
1935 : : {
1936 : 3 : auto_diagnostic_group d;
1937 : 3 : error ("ambiguous default type conversion from %qT",
1938 : : basetype);
1939 : 3 : inform (input_location,
1940 : : " candidate conversions include %qD and %qD",
1941 : : winner, cand);
1942 : 3 : }
1943 : 3 : return error_mark_node;
1944 : : }
1945 : : }
1946 : :
1947 : : winner = cand;
1948 : : }
1949 : : }
1950 : :
1951 : 184 : if (winner)
1952 : : {
1953 : 178 : tree type = non_reference (TREE_TYPE (TREE_TYPE (winner)));
1954 : 178 : return build_user_type_conversion (type, expr, LOOKUP_NORMAL,
1955 : 178 : tf_warning_or_error);
1956 : : }
1957 : :
1958 : : return NULL_TREE;
1959 : : }
1960 : :
1961 : : /* Implements integral promotion (4.1) and float->double promotion. */
1962 : :
1963 : : tree
1964 : 289588612 : type_promotes_to (tree type)
1965 : : {
1966 : 295757683 : tree promoted_type;
1967 : :
1968 : 295757683 : if (type == error_mark_node)
1969 : : return error_mark_node;
1970 : :
1971 : 295757683 : type = TYPE_MAIN_VARIANT (type);
1972 : :
1973 : : /* Check for promotions of target-defined types first. */
1974 : 295757683 : promoted_type = targetm.promoted_type (type);
1975 : 295757683 : if (promoted_type)
1976 : : return promoted_type;
1977 : :
1978 : : /* bool always promotes to int (not unsigned), even if it's the same
1979 : : size. */
1980 : 295757683 : if (TREE_CODE (type) == BOOLEAN_TYPE)
1981 : 10529053 : type = integer_type_node;
1982 : :
1983 : : /* Normally convert enums to int, but convert wide enums to something
1984 : : wider. Scoped enums don't promote, but pretend they do for backward
1985 : : ABI bug compatibility wrt varargs. */
1986 : 285228630 : else if (TREE_CODE (type) == ENUMERAL_TYPE
1987 : 261456422 : || type == char8_type_node
1988 : 261391019 : || type == char16_type_node
1989 : 261108082 : || type == char32_type_node
1990 : 260775693 : || type == wchar_type_node)
1991 : : {
1992 : 25213360 : tree prom = type;
1993 : :
1994 : 25213360 : if (TREE_CODE (type) == ENUMERAL_TYPE)
1995 : : {
1996 : 23772208 : prom = ENUM_UNDERLYING_TYPE (prom);
1997 : 23772208 : if (!ENUM_IS_SCOPED (type)
1998 : 23772208 : && ENUM_FIXED_UNDERLYING_TYPE_P (type))
1999 : : {
2000 : : /* ISO C++17, 7.6/4. A prvalue of an unscoped enumeration type
2001 : : whose underlying type is fixed (10.2) can be converted to a
2002 : : prvalue of its underlying type. Moreover, if integral promotion
2003 : : can be applied to its underlying type, a prvalue of an unscoped
2004 : : enumeration type whose underlying type is fixed can also be
2005 : : converted to a prvalue of the promoted underlying type. */
2006 : : return type_promotes_to (prom);
2007 : : }
2008 : : }
2009 : :
2010 : 19044289 : int precision = MAX (TYPE_PRECISION (type),
2011 : : TYPE_PRECISION (integer_type_node));
2012 : 19044289 : tree totype = c_common_type_for_size (precision, 0);
2013 : 19044289 : if (TYPE_UNSIGNED (prom)
2014 : 19044289 : && ! int_fits_type_p (TYPE_MAX_VALUE (prom), totype))
2015 : 629140 : prom = c_common_type_for_size (precision, 1);
2016 : : else
2017 : : prom = totype;
2018 : 19044289 : if (SCOPED_ENUM_P (type))
2019 : : {
2020 : 63 : if (abi_version_crosses (6)
2021 : 33 : && TYPE_MODE (prom) != TYPE_MODE (type))
2022 : 60 : warning (OPT_Wabi, "scoped enum %qT passed through %<...%> as "
2023 : : "%qT before %<-fabi-version=6%>, %qT after",
2024 : 30 : type, prom, ENUM_UNDERLYING_TYPE (type));
2025 : 33 : if (!abi_version_at_least (6))
2026 : 19044259 : type = prom;
2027 : : }
2028 : : else
2029 : : type = prom;
2030 : : }
2031 : 260015270 : else if (c_promoting_integer_type_p (type))
2032 : : {
2033 : : /* Retain unsignedness if really not getting bigger. */
2034 : 9186900 : if (TYPE_UNSIGNED (type)
2035 : 9186900 : && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2036 : 0 : type = unsigned_type_node;
2037 : : else
2038 : 9186900 : type = integer_type_node;
2039 : : }
2040 : 250828370 : else if (type == float_type_node)
2041 : 10799555 : type = double_type_node;
2042 : :
2043 : : return type;
2044 : : }
2045 : :
2046 : : /* The routines below this point are carefully written to conform to
2047 : : the standard. They use the same terminology, and follow the rules
2048 : : closely. Although they are used only in pt.cc at the moment, they
2049 : : should presumably be used everywhere in the future. */
2050 : :
2051 : : /* True iff EXPR can be converted to TYPE via a qualification conversion.
2052 : : Callers should check for identical types before calling this function. */
2053 : :
2054 : : bool
2055 : 51 : can_convert_qual (tree type, tree expr)
2056 : : {
2057 : 51 : tree expr_type = TREE_TYPE (expr);
2058 : 51 : gcc_assert (!same_type_p (type, expr_type));
2059 : :
2060 : : /* A function pointer conversion also counts as a Qualification Adjustment
2061 : : under [over.ics.scs]. */
2062 : 51 : if (fnptr_conv_p (type, expr_type))
2063 : : return true;
2064 : :
2065 : 41 : if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type))
2066 : 8 : return comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type));
2067 : 33 : else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (expr_type))
2068 : 32 : return (same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
2069 : : TYPE_PTRMEM_CLASS_TYPE (expr_type))
2070 : 61 : && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type),
2071 : 29 : TYPE_PTRMEM_POINTED_TO_TYPE (expr_type)));
2072 : : else
2073 : : return false;
2074 : : }
2075 : :
2076 : : /* Attempt to perform qualification conversions on EXPR to convert it
2077 : : to TYPE. Return the resulting expression, or error_mark_node if
2078 : : the conversion was impossible. Since this is only used by
2079 : : convert_nontype_argument, we fold the conversion. */
2080 : :
2081 : : tree
2082 : 887 : perform_qualification_conversions (tree type, tree expr)
2083 : : {
2084 : 887 : tree expr_type;
2085 : :
2086 : 887 : expr_type = TREE_TYPE (expr);
2087 : :
2088 : 887 : if (same_type_p (type, expr_type))
2089 : : return expr;
2090 : 21 : else if (can_convert_qual (type, expr))
2091 : 14 : return cp_fold_convert (type, expr);
2092 : : else
2093 : 7 : return error_mark_node;
2094 : : }
2095 : :
2096 : : /* True iff T is a transaction-safe function type. */
2097 : :
2098 : : bool
2099 : 7504611 : tx_safe_fn_type_p (tree t)
2100 : : {
2101 : 7504611 : if (!FUNC_OR_METHOD_TYPE_P (t))
2102 : : return false;
2103 : 7500150 : return !!lookup_attribute ("transaction_safe", TYPE_ATTRIBUTES (t));
2104 : : }
2105 : :
2106 : : /* Return the transaction-unsafe variant of transaction-safe function type
2107 : : T. */
2108 : :
2109 : : tree
2110 : 38 : tx_unsafe_fn_variant (tree t)
2111 : : {
2112 : 38 : gcc_assert (tx_safe_fn_type_p (t));
2113 : 38 : tree attrs = remove_attribute ("transaction_safe",
2114 : 38 : TYPE_ATTRIBUTES (t));
2115 : 38 : return cp_build_type_attribute_variant (t, attrs);
2116 : : }
2117 : :
2118 : : /* Return true iff FROM can convert to TO by a transaction-safety
2119 : : conversion. */
2120 : :
2121 : : static bool
2122 : 86471471 : can_convert_tx_safety (tree to, tree from)
2123 : : {
2124 : 3634 : return (flag_tm && tx_safe_fn_type_p (from)
2125 : 86471507 : && same_type_p (to, tx_unsafe_fn_variant (from)));
2126 : : }
2127 : :
2128 : : /* Return true iff FROM can convert to TO by dropping noexcept.
2129 : : This is just a subroutine of fnptr_conv_p. */
2130 : :
2131 : : static bool
2132 : 86904957 : noexcept_conv_p (tree to, tree from)
2133 : : {
2134 : 86904957 : if (!flag_noexcept_type)
2135 : : return false;
2136 : :
2137 : 86477360 : if (TREE_CODE (to) != TREE_CODE (from))
2138 : : return false;
2139 : 45077821 : if (!FUNC_OR_METHOD_TYPE_P (from))
2140 : : return false;
2141 : 1830617 : if (!type_throw_all_p (to)
2142 : 1830617 : || type_throw_all_p (from))
2143 : 91320 : return false;
2144 : 1739297 : tree v = build_exception_variant (from, NULL_TREE);
2145 : 1739297 : return same_type_p (to, v);
2146 : : }
2147 : :
2148 : : /* Return true iff FROM can convert to TO by a function pointer conversion. */
2149 : :
2150 : : bool
2151 : 86904957 : fnptr_conv_p (tree to, tree from)
2152 : : {
2153 : 86904957 : tree t = to;
2154 : 86904957 : tree f = from;
2155 : 765 : if (TYPE_PTRMEMFUNC_P (t)
2156 : 86905722 : && TYPE_PTRMEMFUNC_P (f))
2157 : : {
2158 : 702 : t = TYPE_PTRMEMFUNC_FN_TYPE (t);
2159 : 702 : f = TYPE_PTRMEMFUNC_FN_TYPE (f);
2160 : : }
2161 : 86904957 : if (INDIRECT_TYPE_P (t)
2162 : 85290475 : && INDIRECT_TYPE_P (f))
2163 : : {
2164 : 85290464 : t = TREE_TYPE (t);
2165 : 85290464 : f = TREE_TYPE (f);
2166 : : }
2167 : :
2168 : 86904957 : return (noexcept_conv_p (t, f)
2169 : 86904957 : || can_convert_tx_safety (t, f));
2170 : : }
2171 : :
2172 : : /* Return FN with any NOP_EXPRs stripped that represent function pointer
2173 : : conversions or conversions to the same type. */
2174 : :
2175 : : tree
2176 : 725 : strip_fnptr_conv (tree fn)
2177 : : {
2178 : 736 : while (TREE_CODE (fn) == NOP_EXPR)
2179 : : {
2180 : 33 : tree op = TREE_OPERAND (fn, 0);
2181 : 33 : tree ft = TREE_TYPE (fn);
2182 : 33 : tree ot = TREE_TYPE (op);
2183 : 66 : if (same_type_p (ft, ot)
2184 : 33 : || fnptr_conv_p (ft, ot))
2185 : : fn = op;
2186 : : else
2187 : : break;
2188 : : }
2189 : 725 : return fn;
2190 : : }
|