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