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 37326517 : cp_convert_to_pointer (tree type, tree expr, bool dofold,
77 : tsubst_flags_t complain)
78 : {
79 37326517 : tree intype = TREE_TYPE (expr);
80 37326517 : enum tree_code form;
81 37326517 : tree rval;
82 37326517 : location_t loc = cp_expr_loc_or_input_loc (expr);
83 :
84 37326517 : if (intype == error_mark_node)
85 : return error_mark_node;
86 :
87 37326517 : 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 0 : return rval;
106 : }
107 : }
108 :
109 : /* Handle anachronistic conversions from (::*)() to cv void* or (*)(). */
110 37326517 : if (TYPE_PTR_P (type)
111 37326517 : && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
112 37164527 : || VOID_TYPE_P (TREE_TYPE (type))))
113 : {
114 12 : if (TYPE_PTRMEMFUNC_P (intype)
115 3446830 : || TREE_CODE (intype) == METHOD_TYPE)
116 9 : return convert_member_func_to_ptr (type, expr, complain);
117 3446818 : if (TYPE_PTR_P (TREE_TYPE (expr)))
118 2036994 : return build_nop (type, expr);
119 1409824 : intype = TREE_TYPE (expr);
120 : }
121 :
122 35289514 : if (expr == error_mark_node)
123 : return error_mark_node;
124 :
125 35289514 : form = TREE_CODE (intype);
126 :
127 35289514 : if (INDIRECT_TYPE_P (intype))
128 : {
129 23718528 : intype = TYPE_MAIN_VARIANT (intype);
130 :
131 23718528 : if (TYPE_MAIN_VARIANT (type) != intype
132 16860688 : && TYPE_PTR_P (type)
133 16852394 : && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
134 7578798 : && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
135 7493330 : && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
136 31138193 : && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
137 : {
138 7419665 : enum tree_code code = PLUS_EXPR;
139 7419665 : tree binfo;
140 7419665 : tree intype_class;
141 7419665 : tree type_class;
142 7419665 : bool same_p;
143 :
144 7419665 : intype_class = TREE_TYPE (intype);
145 7419665 : type_class = TREE_TYPE (type);
146 :
147 7419665 : same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class),
148 : TYPE_MAIN_VARIANT (type_class));
149 7419665 : binfo = NULL_TREE;
150 : /* Try derived to base conversion. */
151 7419665 : if (!same_p)
152 31304 : binfo = lookup_base (intype_class, type_class, ba_check,
153 : NULL, complain);
154 7419665 : 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 7419665 : if (binfo == error_mark_node)
162 : return error_mark_node;
163 7419656 : if (binfo || same_p)
164 : {
165 7419656 : if (binfo)
166 31295 : expr = build_base_path (code, expr, binfo, 0, complain);
167 : /* Add any qualifier conversions. */
168 7419656 : return build_nop (type, expr);
169 : }
170 : }
171 :
172 16298863 : 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 16298863 : return build_nop (type, expr);
181 : }
182 1735 : else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
183 11572601 : || (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
184 139 : return convert_ptrmem (type, expr, /*allow_inverse_p=*/false,
185 139 : /*c_cast_p=*/false, complain);
186 11570847 : 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 11570847 : if (null_ptr_cst_p (expr))
208 : {
209 7528201 : if (TYPE_PTRMEMFUNC_P (type))
210 650 : return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr, 0,
211 650 : /*c_cast_p=*/false, complain);
212 :
213 7527551 : if (complain & tf_warning)
214 5219940 : 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 7527551 : tree val = (TYPE_PTRDATAMEM_P (type)
219 7527551 : ? build_int_cst_type (type, -1)
220 7525936 : : build_int_cst (type, 0));
221 :
222 7527551 : return (TREE_SIDE_EFFECTS (expr)
223 7527551 : ? build2 (COMPOUND_EXPR, type, expr, val) : val);
224 : }
225 4042646 : 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 4042646 : if (INTEGRAL_CODE_P (form))
233 : {
234 4061277 : if (TYPE_PRECISION (intype) == POINTER_SIZE)
235 4022390 : return build1 (CONVERT_EXPR, type, expr);
236 20250 : 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 60750 : 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 20250 : if (!dofold)
247 20250 : 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 10160349 : convert_to_pointer_force (tree type, tree expr, tsubst_flags_t complain)
267 : {
268 10160349 : tree intype = TREE_TYPE (expr);
269 10160349 : enum tree_code form = TREE_CODE (intype);
270 :
271 10160349 : if (form == POINTER_TYPE)
272 : {
273 10160349 : intype = TYPE_MAIN_VARIANT (intype);
274 :
275 10160349 : if (TYPE_MAIN_VARIANT (type) != intype
276 3302490 : && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
277 3302459 : && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
278 3302456 : && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
279 13462805 : && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
280 : {
281 3302456 : enum tree_code code = PLUS_EXPR;
282 3302456 : tree binfo;
283 :
284 3302456 : binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
285 : ba_unique, NULL, complain);
286 3302456 : 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 3302456 : if (binfo == error_mark_node)
293 : return error_mark_node;
294 3302456 : if (binfo)
295 : {
296 3302453 : expr = build_base_path (code, expr, binfo, 0, complain);
297 3302453 : if (expr == error_mark_node)
298 : return error_mark_node;
299 : /* Add any qualifier conversions. */
300 3302453 : if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
301 : TREE_TYPE (type)))
302 432305 : expr = build_nop (type, expr);
303 3302453 : return expr;
304 : }
305 : }
306 : }
307 :
308 6857896 : 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 248 : build_up_reference (tree type, tree arg, int flags, tree decl,
321 : tsubst_flags_t complain)
322 : {
323 248 : tree rval;
324 248 : tree argtype = TREE_TYPE (arg);
325 248 : tree target_type = TREE_TYPE (type);
326 :
327 248 : gcc_assert (TYPE_REF_P (type));
328 :
329 248 : 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 248 : 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 247 : rval = cp_build_addr_expr (arg, complain);
349 247 : if (rval == error_mark_node)
350 : return error_mark_node;
351 :
352 247 : if ((flags & LOOKUP_PROTECT)
353 247 : && TYPE_MAIN_VARIANT (argtype) != TYPE_MAIN_VARIANT (target_type)
354 9 : && MAYBE_CLASS_TYPE_P (argtype)
355 256 : && 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 238 : rval
368 238 : = convert_to_pointer_force (build_pointer_type (target_type),
369 : rval, complain);
370 247 : 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 250 : convert_to_reference (tree reftype, tree expr, int convtype,
417 : int flags, tree decl, tsubst_flags_t complain)
418 : {
419 250 : tree type = TYPE_MAIN_VARIANT (TREE_TYPE (reftype));
420 250 : tree intype;
421 250 : tree rval = NULL_TREE;
422 250 : tree rval_as_conversion = NULL_TREE;
423 250 : bool can_convert_intype_to_type;
424 250 : location_t loc = cp_expr_loc_or_input_loc (expr);
425 :
426 250 : if (TREE_CODE (type) == FUNCTION_TYPE
427 250 : && TREE_TYPE (expr) == unknown_type_node)
428 0 : expr = instantiate_type (type, expr, complain);
429 :
430 250 : if (expr == error_mark_node)
431 : return error_mark_node;
432 :
433 248 : intype = TREE_TYPE (expr);
434 :
435 248 : gcc_assert (!TYPE_REF_P (intype));
436 248 : gcc_assert (TYPE_REF_P (reftype));
437 :
438 248 : intype = TYPE_MAIN_VARIANT (intype);
439 :
440 248 : can_convert_intype_to_type = can_convert_standard (type, intype, complain);
441 :
442 248 : if (!can_convert_intype_to_type
443 4 : && (convtype & CONV_IMPLICIT) && MAYBE_CLASS_TYPE_P (intype)
444 248 : && ! (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 248 : rval_as_conversion = NULL_TREE;
456 : intype = type;
457 : can_convert_intype_to_type = 1;
458 : }
459 : }
460 :
461 248 : if (((convtype & CONV_STATIC)
462 0 : && can_convert_standard (intype, type, complain))
463 248 : || ((convtype & CONV_IMPLICIT) && can_convert_intype_to_type))
464 : {
465 244 : {
466 244 : tree ttl = TREE_TYPE (reftype);
467 244 : tree ttr = lvalue_type (expr);
468 :
469 244 : if ((complain & tf_error)
470 244 : && ! lvalue_p (expr))
471 3 : diagnose_ref_binding (loc, reftype, intype, decl);
472 :
473 244 : if (! (convtype & CONV_CONST)
474 244 : && !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 244 : 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 2485143272 : convert_from_reference (tree val)
537 : {
538 2485143272 : if (TREE_TYPE (val)
539 2485143272 : && TYPE_REF_P (TREE_TYPE (val)))
540 : {
541 274156833 : tree t = TREE_TYPE (TREE_TYPE (val));
542 274156833 : tree ref = build1 (INDIRECT_REF, t, val);
543 :
544 274156833 : 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 274156833 : TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
549 274156833 : TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
550 274156833 : TREE_SIDE_EFFECTS (ref)
551 274156833 : = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val));
552 274156833 : val = ref;
553 : }
554 :
555 2485143272 : 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 37366257 : force_rvalue (tree expr, tsubst_flags_t complain)
563 : {
564 37366257 : tree type = TREE_TYPE (expr);
565 37366257 : if (MAYBE_CLASS_TYPE_P (type) && TREE_CODE (expr) != TARGET_EXPR)
566 : {
567 179685 : releasing_vec args (make_tree_vector_single (expr));
568 179685 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
569 : &args, type, LOOKUP_NORMAL, complain);
570 179685 : expr = build_cplus_new (type, expr, complain);
571 179685 : }
572 : else
573 37186572 : expr = decay_conversion (expr, complain);
574 :
575 37366257 : return expr;
576 : }
577 :
578 : /* Force EXPR to be an lvalue, if it isn't already. */
579 :
580 : tree
581 528303 : force_lvalue (tree expr, tsubst_flags_t complain)
582 : {
583 528303 : if (!lvalue_p (expr))
584 : {
585 27 : expr = cp_build_addr_expr (expr, complain);
586 27 : expr = cp_build_indirect_ref (input_location, expr, RO_ARROW, complain);
587 : }
588 528303 : return expr;
589 : }
590 :
591 :
592 : /* If EXPR and ORIG are INTEGER_CSTs, return a version of EXPR that has
593 : TREE_OVERFLOW set only if it is set in ORIG. Otherwise, return EXPR
594 : unchanged. */
595 :
596 : static tree
597 231340752 : ignore_overflows (tree expr, tree orig)
598 : {
599 231340752 : tree stripped_expr = tree_strip_any_location_wrapper (expr);
600 231340752 : tree stripped_orig = tree_strip_any_location_wrapper (orig);
601 :
602 231340752 : if (TREE_CODE (stripped_expr) == INTEGER_CST
603 155965450 : && TREE_CODE (stripped_orig) == INTEGER_CST
604 387303487 : && TREE_OVERFLOW (stripped_expr) != TREE_OVERFLOW (stripped_orig))
605 : {
606 4417 : gcc_assert (!TREE_OVERFLOW (stripped_orig));
607 : /* Ensure constant sharing. */
608 4417 : stripped_expr = wide_int_to_tree (TREE_TYPE (stripped_expr),
609 8834 : wi::to_wide (stripped_expr));
610 : }
611 :
612 231340752 : return preserve_any_location_wrapper (stripped_expr, expr);
613 : }
614 :
615 : /* Fold away simple conversions, but make sure TREE_OVERFLOW is set
616 : properly and propagate TREE_NO_WARNING if folding EXPR results
617 : in the same expression code. */
618 :
619 : tree
620 69551493 : cp_fold_convert (tree type, tree expr)
621 : {
622 69551493 : tree conv;
623 69551493 : if (TREE_TYPE (expr) == type)
624 : conv = expr;
625 28039080 : else if ((TREE_CODE (expr) == PTRMEM_CST
626 261 : && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
627 : PTRMEM_CST_CLASS (expr)))
628 28039083 : || (REFLECT_EXPR_P (expr) && REFLECTION_TYPE_P (type)))
629 : {
630 : /* Avoid wrapping a PTRMEM_CST/REFLECT_EXPR in NOP_EXPR. */
631 260 : conv = copy_node (expr);
632 260 : TREE_TYPE (conv) = type;
633 : }
634 28038820 : else if (TYPE_PTRMEM_P (type))
635 : {
636 6 : conv = convert_ptrmem (type, expr, true, false,
637 : tf_warning_or_error);
638 6 : conv = cp_fully_fold (conv);
639 : }
640 : else
641 : {
642 28038814 : conv = fold_convert (type, expr);
643 28038814 : conv = ignore_overflows (conv, expr);
644 : }
645 :
646 69551493 : if (TREE_CODE (expr) == TREE_CODE (conv))
647 42978560 : copy_warning (conv, expr);
648 :
649 69551493 : return conv;
650 : }
651 :
652 : /* C++ conversions, preference to static cast conversions. */
653 :
654 : tree
655 331855181 : cp_convert (tree type, tree expr, tsubst_flags_t complain)
656 : {
657 331855181 : return ocp_convert (type, expr, CONV_OLD_CONVERT, LOOKUP_NORMAL, complain);
658 : }
659 :
660 : /* C++ equivalent of convert_and_check but using cp_convert as the
661 : conversion function.
662 :
663 : Convert EXPR to TYPE, warning about conversion problems with constants.
664 : Invoke this function on every expression that is converted implicitly,
665 : i.e. because of language rules and not because of an explicit cast. */
666 :
667 : tree
668 108883909 : cp_convert_and_check (tree type, tree expr, tsubst_flags_t complain)
669 : {
670 108883909 : tree result, expr_for_warning = expr;
671 :
672 108883909 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
673 117 : expr = TREE_OPERAND (expr, 0);
674 108883909 : if (TREE_TYPE (expr) == type)
675 : return expr;
676 108883896 : if (expr == error_mark_node)
677 : return expr;
678 108883896 : result = cp_convert (type, expr, complain);
679 :
680 108883896 : if ((complain & tf_warning)
681 101391588 : && c_inhibit_evaluation_warnings == 0)
682 : {
683 99719595 : tree folded = cp_fully_fold (expr_for_warning);
684 99719595 : tree folded_result;
685 99719595 : if (folded == expr)
686 : folded_result = result;
687 : else
688 : {
689 : /* Avoid bogus -Wparentheses warnings. */
690 36118437 : warning_sentinel w (warn_parentheses);
691 36118437 : warning_sentinel c (warn_int_in_bool_context);
692 36118437 : folded_result = cp_convert (type, folded, tf_none);
693 36118437 : }
694 99719595 : folded_result = fold_simple (folded_result);
695 86186993 : if (!TREE_OVERFLOW_P (folded)
696 185906585 : && folded_result != error_mark_node)
697 160404970 : warnings_for_convert_and_check (cp_expr_loc_or_input_loc (expr),
698 : type, folded, folded_result);
699 : }
700 :
701 : return result;
702 : }
703 :
704 : /* Conversion...
705 :
706 : FLAGS indicates how we should behave. */
707 :
708 : tree
709 509145178 : ocp_convert (tree type, tree expr, int convtype, int flags,
710 : tsubst_flags_t complain)
711 : {
712 509145178 : tree e = expr;
713 509145178 : enum tree_code code = TREE_CODE (type);
714 509145178 : const char *invalid_conv_diag;
715 509145178 : tree e1;
716 509145178 : location_t loc = cp_expr_loc_or_input_loc (expr);
717 509145178 : bool dofold = (convtype & CONV_FOLD);
718 :
719 509145178 : if (error_operand_p (e) || type == error_mark_node)
720 91 : return error_mark_node;
721 :
722 509145087 : if (TREE_CODE (e) == COMPOUND_EXPR)
723 : {
724 487887 : e = ocp_convert (type, TREE_OPERAND (e, 1), convtype, flags, complain);
725 487887 : if (e == error_mark_node)
726 : return error_mark_node;
727 487887 : if (e == TREE_OPERAND (expr, 1))
728 : return expr;
729 485676 : e = build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (e),
730 485676 : TREE_OPERAND (expr, 0), e);
731 485676 : copy_warning (e, expr);
732 485676 : return e;
733 : }
734 :
735 508657200 : complete_type (type);
736 508657200 : complete_type (TREE_TYPE (expr));
737 :
738 1017314400 : if ((invalid_conv_diag
739 508657200 : = targetm.invalid_conversion (TREE_TYPE (expr), type)))
740 : {
741 0 : if (complain & tf_error)
742 0 : error (invalid_conv_diag);
743 0 : return error_mark_node;
744 : }
745 :
746 : /* FIXME remove when moving to c_fully_fold model. */
747 508657200 : if (!CLASS_TYPE_P (type))
748 : {
749 480879849 : e = mark_rvalue_use (e);
750 480879849 : tree v = scalar_constant_value (e);
751 480879849 : if (!error_operand_p (v))
752 480879788 : e = v;
753 : }
754 508657200 : if (error_operand_p (e))
755 0 : return error_mark_node;
756 :
757 508657200 : if (NULLPTR_TYPE_P (type) && null_ptr_cst_p (e))
758 : {
759 5176 : if (complain & tf_warning)
760 2718 : maybe_warn_zero_as_null_pointer_constant (e, loc);
761 :
762 5176 : if (!TREE_SIDE_EFFECTS (e))
763 5165 : return nullptr_node;
764 : }
765 :
766 508652035 : if (MAYBE_CLASS_TYPE_P (type) && (convtype & CONV_FORCE_TEMP))
767 : /* We need a new temporary; don't take this shortcut. */;
768 507131694 : else if (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (e)))
769 : {
770 244501321 : tree etype = TREE_TYPE (e);
771 244501321 : if (same_type_p (type, etype))
772 : /* The call to fold will not always remove the NOP_EXPR as
773 : might be expected, since if one of the types is a typedef;
774 : the comparison in fold is just equality of pointers, not a
775 : call to comptypes. We don't call fold in this case because
776 : that can result in infinite recursion; fold will call
777 : convert, which will call ocp_convert, etc. */
778 : return e;
779 : /* For complex data types, we need to perform componentwise
780 : conversion. */
781 22848546 : else if (TREE_CODE (type) == COMPLEX_TYPE)
782 0 : return convert_to_complex_maybe_fold (type, e, dofold);
783 22848546 : else if (VECTOR_TYPE_P (type))
784 3851 : return convert_to_vector (type, rvalue (e));
785 22844695 : else if (TREE_CODE (e) == TARGET_EXPR)
786 : {
787 : /* Don't build a NOP_EXPR of class type. Instead, change the
788 : type of the temporary. */
789 207923 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, etype));
790 207923 : TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type;
791 207923 : return e;
792 : }
793 22636772 : else if (TREE_CODE (e) == CONSTRUCTOR)
794 : {
795 66 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, etype));
796 66 : TREE_TYPE (e) = type;
797 66 : return e;
798 : }
799 : else
800 : {
801 : /* We shouldn't be treating objects of ADDRESSABLE type as
802 : rvalues. */
803 22636706 : gcc_assert (!TREE_ADDRESSABLE (type));
804 22636706 : return build_nop (type, e);
805 : }
806 : }
807 :
808 264150714 : e1 = targetm.convert_to_type (type, e);
809 264150714 : if (e1)
810 : return e1;
811 :
812 264150714 : if (code == VOID_TYPE && (convtype & CONV_STATIC))
813 : {
814 37972 : e = convert_to_void (e, ICV_CAST, complain);
815 37972 : return e;
816 : }
817 :
818 264112742 : if (INTEGRAL_CODE_P (code))
819 : {
820 211659457 : tree intype = TREE_TYPE (e);
821 211659457 : tree converted;
822 :
823 211659457 : if (TREE_CODE (type) == ENUMERAL_TYPE)
824 : {
825 : /* enum = enum, enum = int, enum = float, (enum)pointer are all
826 : errors. */
827 733752 : if (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
828 6 : || SCALAR_FLOAT_TYPE_P (intype))
829 733752 : && ! (convtype & CONV_STATIC))
830 733752 : || TYPE_PTR_P (intype))
831 : {
832 0 : if (complain & tf_error)
833 0 : permerror (loc, "conversion from %q#T to %q#T", intype, type);
834 : else
835 0 : return error_mark_node;
836 : }
837 :
838 : /* [expr.static.cast]
839 :
840 : 8. A value of integral or enumeration type can be explicitly
841 : converted to an enumeration type. The value is unchanged if
842 : the original value is within the range of the enumeration
843 : values. Otherwise, the resulting enumeration value is
844 : unspecified. */
845 733752 : tree val = fold_for_warn (e);
846 733752 : if ((complain & tf_warning)
847 732754 : && TREE_CODE (val) == INTEGER_CST
848 30744 : && ENUM_UNDERLYING_TYPE (type)
849 764493 : && !int_fits_type_p (val, ENUM_UNDERLYING_TYPE (type)))
850 120 : warning_at (loc, OPT_Wconversion,
851 : "the result of the conversion is unspecified because "
852 : "%qE is outside the range of type %qT",
853 : expr, type);
854 : }
855 211659457 : if (MAYBE_CLASS_TYPE_P (intype))
856 : {
857 9 : tree rval;
858 18 : rval = build_type_conversion (type, e);
859 9 : if (rval)
860 : return rval;
861 0 : if (complain & tf_error)
862 0 : error_at (loc, "%q#T used where a %qT was expected", intype, type);
863 0 : return error_mark_node;
864 : }
865 211659448 : if (code == BOOLEAN_TYPE)
866 : {
867 8357510 : if (VOID_TYPE_P (intype))
868 : {
869 18 : if (complain & tf_error)
870 18 : error_at (loc,
871 : "could not convert %qE from %<void%> to %<bool%>",
872 : expr);
873 18 : return error_mark_node;
874 : }
875 :
876 8357492 : if (VECTOR_TYPE_P (intype) && !gnu_vector_type_p (intype))
877 : {
878 0 : if (complain & tf_error)
879 0 : error_at (loc, "could not convert %qE from %qH to %qI", expr,
880 0 : TREE_TYPE (expr), type);
881 0 : return error_mark_node;
882 : }
883 :
884 : /* We can't implicitly convert a scoped enum to bool, so convert
885 : to the underlying type first. */
886 8357492 : if (SCOPED_ENUM_P (intype) && (convtype & CONV_STATIC))
887 109 : e = build_nop (ENUM_UNDERLYING_TYPE (intype), e);
888 8357492 : if (complain & tf_warning)
889 4984832 : e = cp_truthvalue_conversion (e, complain);
890 : else
891 : {
892 : /* Prevent bogus -Wint-in-bool-context warnings coming
893 : from c_common_truthvalue_conversion down the line. */
894 3372660 : warning_sentinel w (warn_int_in_bool_context);
895 3372660 : warning_sentinel c (warn_sign_compare);
896 3372660 : e = cp_truthvalue_conversion (e, complain);
897 3372660 : }
898 :
899 : /* Sometimes boolean types don't match if a non-standard boolean
900 : type has been invented by the target. */
901 8357492 : if (tree e2 = targetm.convert_to_type (type, e))
902 : return e2;
903 :
904 8357492 : return e;
905 : }
906 :
907 203301938 : converted = convert_to_integer_maybe_fold (type, e, dofold);
908 :
909 : /* Ignore any integer overflow caused by the conversion. */
910 203301938 : return ignore_overflows (converted, e);
911 : }
912 52453285 : if (INDIRECT_TYPE_P (type) || TYPE_PTRMEM_P (type))
913 30468621 : return cp_convert_to_pointer (type, e, dofold, complain);
914 21984664 : if (code == VECTOR_TYPE)
915 : {
916 5137 : tree in_vtype = TREE_TYPE (e);
917 5137 : if (MAYBE_CLASS_TYPE_P (in_vtype))
918 : {
919 0 : tree ret_val;
920 0 : ret_val = build_type_conversion (type, e);
921 0 : if (ret_val)
922 : return ret_val;
923 0 : if (complain & tf_error)
924 0 : error_at (loc, "%q#T used where a %qT was expected",
925 : in_vtype, type);
926 0 : return error_mark_node;
927 : }
928 5137 : return convert_to_vector (type, rvalue (e));
929 : }
930 21979527 : if (code == REAL_TYPE || code == COMPLEX_TYPE)
931 : {
932 20459180 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (e)))
933 : {
934 0 : tree rval;
935 0 : rval = build_type_conversion (type, e);
936 0 : if (rval)
937 : return rval;
938 0 : else if (complain & tf_error)
939 0 : error_at (loc,
940 : "%q#T used where a floating-point value was expected",
941 0 : TREE_TYPE (e));
942 : }
943 20459180 : if (code == REAL_TYPE)
944 20057942 : return convert_to_real_maybe_fold (type, e, dofold);
945 401238 : else if (code == COMPLEX_TYPE)
946 401238 : return convert_to_complex_maybe_fold (type, e, dofold);
947 : }
948 :
949 : /* New C++ semantics: since assignment is now based on
950 : memberwise copying, if the rhs type is derived from the
951 : lhs type, then we may still do a conversion. */
952 1520347 : if (RECORD_OR_UNION_CODE_P (code))
953 : {
954 1520341 : tree dtype = TREE_TYPE (e);
955 1520341 : tree ctor = NULL_TREE;
956 :
957 1520341 : dtype = TYPE_MAIN_VARIANT (dtype);
958 :
959 : /* Conversion between aggregate types. New C++ semantics allow
960 : objects of derived type to be cast to objects of base type.
961 : Old semantics only allowed this between pointers.
962 :
963 : There may be some ambiguity between using a constructor
964 : vs. using a type conversion operator when both apply. */
965 :
966 1520341 : ctor = e;
967 :
968 1520341 : if (abstract_virtuals_error (NULL_TREE, type, complain))
969 3 : return error_mark_node;
970 :
971 1089 : if (BRACE_ENCLOSED_INITIALIZER_P (ctor)
972 : /* We don't want to create a TARGET_EXPR in a template by the
973 : build_cplus_new below. */
974 1520347 : || processing_template_decl)
975 19940 : ctor = perform_implicit_conversion_flags (type, ctor, complain, flags);
976 1500398 : else if ((flags & LOOKUP_ONLYCONVERTING)
977 1500398 : && ! (CLASS_TYPE_P (dtype) && DERIVED_FROM_P (type, dtype)))
978 : /* For copy-initialization, first we create a temp of the proper type
979 : with a user-defined conversion sequence, then we direct-initialize
980 : the target with the temp (see [dcl.init]). */
981 23778 : ctor = build_user_type_conversion (type, ctor, flags, complain);
982 : else
983 : {
984 1476620 : releasing_vec ctor_vec (make_tree_vector_single (ctor));
985 1476620 : ctor = build_special_member_call (NULL_TREE,
986 : complete_ctor_identifier,
987 : &ctor_vec,
988 : type, flags, complain);
989 1476620 : }
990 1520338 : if (ctor)
991 1520111 : return build_cplus_new (type, ctor, complain);
992 : }
993 :
994 233 : if (complain & tf_error)
995 : {
996 : /* If the conversion failed and expr was an invalid use of pointer to
997 : member function, try to report a meaningful error. */
998 233 : if (invalid_nonstatic_memfn_p (loc, expr, complain))
999 : /* We displayed the error message. */;
1000 : else
1001 : {
1002 230 : auto_diagnostic_group d;
1003 230 : error_at (loc, "conversion from %qH to non-scalar type %qI requested",
1004 230 : TREE_TYPE (expr), type);
1005 230 : maybe_show_nonconverting_candidate (type, TREE_TYPE (expr), expr,
1006 : flags);
1007 230 : }
1008 : }
1009 233 : return error_mark_node;
1010 : }
1011 :
1012 : /* If CALL is a call, return the callee; otherwise null. */
1013 :
1014 : tree
1015 1378863351 : cp_get_callee (tree call)
1016 : {
1017 1378863351 : if (call == NULL_TREE)
1018 : return call;
1019 1378863351 : else if (TREE_CODE (call) == CALL_EXPR)
1020 1253712285 : return CALL_EXPR_FN (call);
1021 125151066 : else if (TREE_CODE (call) == AGGR_INIT_EXPR)
1022 53047735 : return AGGR_INIT_EXPR_FN (call);
1023 : return NULL_TREE;
1024 : }
1025 :
1026 : /* FN is the callee of a CALL_EXPR or AGGR_INIT_EXPR; return the FUNCTION_DECL
1027 : if we can. */
1028 :
1029 : tree
1030 1148947348 : cp_get_fndecl_from_callee (tree fn, bool fold /* = true */)
1031 : {
1032 1148947348 : if (fn == NULL_TREE)
1033 : return fn;
1034 :
1035 : /* We evaluate constexpr functions on the original, pre-genericization
1036 : bodies. So block-scope extern declarations have not been mapped to
1037 : declarations in outer scopes. Use the namespace-scope declaration,
1038 : if any, so that retrieve_constexpr_fundef can find it (PR111132). */
1039 2123399903 : auto fn_or_local_alias = [] (tree f)
1040 : {
1041 1031286597 : if (DECL_LOCAL_DECL_P (f))
1042 58927 : if (tree alias = DECL_LOCAL_DECL_ALIAS (f))
1043 58927 : if (alias != error_mark_node)
1044 58927 : return alias;
1045 : return f;
1046 : };
1047 :
1048 1092113306 : if (TREE_CODE (fn) == FUNCTION_DECL)
1049 11919197 : return fn_or_local_alias (fn);
1050 1080194109 : tree type = TREE_TYPE (fn);
1051 1080194109 : if (type == NULL_TREE || !INDIRECT_TYPE_P (type))
1052 : return NULL_TREE;
1053 1029784598 : if (fold)
1054 3725808 : fn = maybe_constant_init (fn);
1055 1029784598 : STRIP_NOPS (fn);
1056 1029784598 : if (TREE_CODE (fn) == ADDR_EXPR
1057 1029784598 : || TREE_CODE (fn) == FDESC_EXPR)
1058 1019368326 : fn = TREE_OPERAND (fn, 0);
1059 1029784598 : if (TREE_CODE (fn) == FUNCTION_DECL)
1060 1019367400 : return fn_or_local_alias (fn);
1061 : return NULL_TREE;
1062 : }
1063 :
1064 : /* Like get_callee_fndecl, but handles AGGR_INIT_EXPR as well and uses the
1065 : constexpr machinery. */
1066 :
1067 : tree
1068 0 : cp_get_callee_fndecl (tree call)
1069 : {
1070 0 : return cp_get_fndecl_from_callee (cp_get_callee (call));
1071 : }
1072 :
1073 : /* As above, but not using the constexpr machinery. */
1074 :
1075 : tree
1076 452976390 : cp_get_callee_fndecl_nofold (tree call)
1077 : {
1078 452976390 : return cp_get_fndecl_from_callee (cp_get_callee (call), false);
1079 : }
1080 :
1081 : /* Subroutine of convert_to_void. Warn if we're discarding something with
1082 : attribute [[nodiscard]]. */
1083 :
1084 : static void
1085 6376320 : maybe_warn_nodiscard (tree expr, impl_conv_void implicit)
1086 : {
1087 6376320 : if (!warn_unused_result || c_inhibit_evaluation_warnings)
1088 : return;
1089 :
1090 6365910 : tree call = expr;
1091 6365910 : if (TREE_CODE (expr) == TARGET_EXPR)
1092 131753 : call = TARGET_EXPR_INITIAL (expr);
1093 6365910 : location_t loc = cp_expr_loc_or_input_loc (call);
1094 6365910 : tree callee = cp_get_callee (call);
1095 6365910 : if (!callee || !TREE_TYPE (callee))
1096 : return;
1097 :
1098 6363904 : tree type = TREE_TYPE (callee);
1099 6363904 : if (TYPE_PTRMEMFUNC_P (type))
1100 0 : type = TYPE_PTRMEMFUNC_FN_TYPE (type);
1101 6363904 : if (INDIRECT_TYPE_P (type))
1102 3725808 : type = TREE_TYPE (type);
1103 6363904 : if (!FUNC_OR_METHOD_TYPE_P (type))
1104 : return;
1105 :
1106 6335141 : tree rettype = TREE_TYPE (type);
1107 6335141 : tree fn = cp_get_fndecl_from_callee (callee);
1108 6335141 : tree attr;
1109 6335141 : if (implicit != ICV_CAST && fn
1110 6335141 : && (attr = lookup_attribute ("nodiscard", DECL_ATTRIBUTES (fn))))
1111 : {
1112 398 : escaped_string msg;
1113 398 : tree args = TREE_VALUE (attr);
1114 398 : if (args)
1115 63 : msg.escape (TREE_STRING_POINTER (TREE_VALUE (args)));
1116 398 : const char *format
1117 398 : = (msg
1118 398 : ? G_("ignoring return value of %qD, "
1119 : "declared with attribute %<nodiscard%>: %qs")
1120 : : G_("ignoring return value of %qD, "
1121 335 : "declared with attribute %<nodiscard%>%s"));
1122 398 : const char *raw_msg = msg ? (const char *) msg : "";
1123 398 : auto_diagnostic_group d;
1124 398 : auto_urlify_attributes sentinel;
1125 398 : if (warning_at (loc, OPT_Wunused_result, format, fn, raw_msg))
1126 398 : inform (DECL_SOURCE_LOCATION (fn), "declared here");
1127 398 : }
1128 6334743 : else if (implicit != ICV_CAST
1129 6334743 : && (attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype))))
1130 : {
1131 121 : escaped_string msg;
1132 121 : tree args = TREE_VALUE (attr);
1133 121 : if (args)
1134 54 : msg.escape (TREE_STRING_POINTER (TREE_VALUE (args)));
1135 121 : const char *format
1136 121 : = (msg
1137 121 : ? G_("ignoring returned value of type %qT, "
1138 : "declared with attribute %<nodiscard%>: %qs")
1139 : : G_("ignoring returned value of type %qT, "
1140 67 : "declared with attribute %<nodiscard%>%s"));
1141 121 : const char *raw_msg = msg ? (const char *) msg : "";
1142 121 : auto_diagnostic_group d;
1143 121 : auto_urlify_attributes sentinel;
1144 121 : if (warning_at (loc, OPT_Wunused_result, format, rettype, raw_msg))
1145 : {
1146 121 : if (fn)
1147 22 : inform (DECL_SOURCE_LOCATION (fn),
1148 : "in call to %qD, declared here", fn);
1149 121 : inform (DECL_SOURCE_LOCATION (TYPE_NAME (rettype)),
1150 : "%qT declared here", rettype);
1151 : }
1152 121 : }
1153 6334622 : else if (TREE_CODE (expr) == TARGET_EXPR
1154 6334622 : && lookup_attribute ("warn_unused_result", TYPE_ATTRIBUTES (type)))
1155 : {
1156 : /* The TARGET_EXPR confuses do_warn_unused_result into thinking that the
1157 : result is used, so handle that case here. */
1158 63 : auto_urlify_attributes sentinel;
1159 63 : if (fn)
1160 : {
1161 63 : auto_diagnostic_group d;
1162 63 : if (warning_at (loc, OPT_Wunused_result,
1163 : "ignoring return value of %qD, "
1164 : "declared with attribute %<warn_unused_result%>",
1165 : fn))
1166 63 : inform (DECL_SOURCE_LOCATION (fn), "declared here");
1167 63 : }
1168 : else
1169 0 : warning_at (loc, OPT_Wunused_result,
1170 : "ignoring return value of function "
1171 : "declared with attribute %<warn_unused_result%>");
1172 63 : }
1173 : }
1174 :
1175 : /* When an expression is used in a void context, its value is discarded and
1176 : no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
1177 : stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
1178 : in a void context. The C++ standard does not define what an `access' to an
1179 : object is, but there is reason to believe that it is the lvalue to rvalue
1180 : conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
1181 : accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
1182 : indicates that volatile semantics should be the same between C and C++
1183 : where ever possible. C leaves it implementation defined as to what
1184 : constitutes an access to a volatile. So, we interpret `*vp' as a read of
1185 : the volatile object `vp' points to, unless that is an incomplete type. For
1186 : volatile references we do not do this interpretation, because that would
1187 : make it impossible to ignore the reference return value from functions. We
1188 : issue warnings in the confusing cases.
1189 :
1190 : The IMPLICIT is ICV_CAST when the user is explicitly converting an expression
1191 : to void via a cast. If an expression is being implicitly converted, IMPLICIT
1192 : indicates the context of the implicit conversion. */
1193 :
1194 : tree
1195 102675720 : convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain)
1196 : {
1197 109097948 : location_t loc = cp_expr_loc_or_input_loc (expr);
1198 :
1199 109097948 : if (expr == error_mark_node
1200 109097948 : || TREE_TYPE (expr) == error_mark_node)
1201 : return error_mark_node;
1202 :
1203 109093714 : expr = maybe_undo_parenthesized_ref (expr);
1204 :
1205 109093714 : if (invalid_nonstatic_memfn_p (loc, expr, complain))
1206 25 : return error_mark_node;
1207 109093689 : if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
1208 : {
1209 6 : if (complain & tf_error)
1210 6 : error_at (loc, "pseudo-destructor is not called");
1211 6 : return error_mark_node;
1212 : }
1213 :
1214 : /* Explicitly evaluate void-converted concept checks since their
1215 : satisfaction may produce ill-formed programs. */
1216 109093683 : if (concept_check_p (expr) && !cp_unevaluated_operand)
1217 25 : expr = evaluate_concept_check (expr);
1218 :
1219 : /* Detect using expressions of consteval-only types outside manifestly
1220 : constant-evaluated contexts. We are going to discard this expression,
1221 : so we can't wait till cp_fold_immediate_r. FIXME This is too early;
1222 : code like "int i = (^^i, 42);" is OK. We should stop discarding
1223 : expressions here (PR124249). */
1224 109093683 : if (stmts_are_full_exprs_p () && check_out_of_consteval_use (expr))
1225 28 : return error_mark_node;
1226 :
1227 109093655 : if (VOID_TYPE_P (TREE_TYPE (expr)))
1228 : return expr;
1229 :
1230 76020565 : expr = mark_discarded_use (expr);
1231 76020565 : if (implicit == ICV_CAST)
1232 : /* An explicit cast to void avoids all -Wunused-but-set* warnings. */
1233 1095540 : mark_exp_read (expr);
1234 :
1235 76020565 : switch (TREE_CODE (expr))
1236 : {
1237 133261 : case COND_EXPR:
1238 133261 : {
1239 : /* The two parts of a cond expr might be separate lvalues. */
1240 133261 : tree op1 = TREE_OPERAND (expr,1);
1241 133261 : tree op2 = TREE_OPERAND (expr,2);
1242 133255 : bool side_effects = ((op1 && TREE_SIDE_EFFECTS (op1))
1243 252290 : || TREE_SIDE_EFFECTS (op2));
1244 133261 : tree new_op1, new_op2;
1245 133261 : new_op1 = NULL_TREE;
1246 133261 : if (implicit != ICV_CAST && !side_effects)
1247 : {
1248 69 : if (op1)
1249 66 : new_op1 = convert_to_void (op1, ICV_SECOND_OF_COND, complain);
1250 69 : new_op2 = convert_to_void (op2, ICV_THIRD_OF_COND, complain);
1251 : }
1252 : else
1253 : {
1254 133192 : if (op1)
1255 133189 : new_op1 = convert_to_void (op1, ICV_CAST, complain);
1256 133192 : new_op2 = convert_to_void (op2, ICV_CAST, complain);
1257 : }
1258 :
1259 133261 : expr = build3_loc (loc, COND_EXPR, TREE_TYPE (new_op2),
1260 133261 : TREE_OPERAND (expr, 0), new_op1, new_op2);
1261 133261 : break;
1262 : }
1263 :
1264 506549 : case COMPOUND_EXPR:
1265 506549 : {
1266 : /* The second part of a compound expr contains the value. */
1267 506549 : tree op1 = TREE_OPERAND (expr,1);
1268 506549 : tree new_op1;
1269 506549 : if (implicit != ICV_CAST && !warning_suppressed_p (expr /* What warning? */))
1270 194189 : new_op1 = convert_to_void (op1, ICV_RIGHT_OF_COMMA, complain);
1271 : else
1272 312360 : new_op1 = convert_to_void (op1, ICV_CAST, complain);
1273 :
1274 506549 : if (new_op1 != op1)
1275 : {
1276 506549 : tree t = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (new_op1),
1277 506549 : TREE_OPERAND (expr, 0), new_op1);
1278 506549 : expr = t;
1279 : }
1280 :
1281 : break;
1282 : }
1283 :
1284 : case NON_LVALUE_EXPR:
1285 : case NOP_EXPR:
1286 : /* These have already decayed to rvalue. */
1287 : break;
1288 :
1289 8290325 : case CALL_EXPR: /* We have a special meaning for volatile void fn(). */
1290 : /* cdtors may return this or void, depending on
1291 : targetm.cxx.cdtor_returns_this, but this shouldn't affect our
1292 : decisions here: neither nodiscard warnings (nodiscard dtors
1293 : are nonsensical and ctors have a different behavior with that
1294 : attribute that is handled in the TARGET_EXPR case), nor should
1295 : any constexpr or template instantiations be affected by an ABI
1296 : property that is, or at least ought to be transparent to the
1297 : language. */
1298 8290325 : if (tree fn = cp_get_callee_fndecl_nofold (expr))
1299 15244174 : if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
1300 : return expr;
1301 :
1302 8290325 : if (complain & tf_warning)
1303 6243980 : maybe_warn_nodiscard (expr, implicit);
1304 : break;
1305 :
1306 7215575 : case INDIRECT_REF:
1307 7215575 : {
1308 7215575 : tree type = TREE_TYPE (expr);
1309 7215575 : int is_reference = TYPE_REF_P (TREE_TYPE (TREE_OPERAND (expr, 0)));
1310 7215575 : int is_volatile = TYPE_VOLATILE (type);
1311 7215575 : if (is_volatile)
1312 433 : complete_type (type);
1313 7215575 : int is_complete = COMPLETE_TYPE_P (type);
1314 :
1315 : /* Don't load the value if this is an implicit dereference, or if
1316 : the type needs to be handled by ctors/dtors. */
1317 7215575 : if (is_reference)
1318 : {
1319 267 : if (is_volatile && (complain & tf_warning)
1320 : /* A co_await expression, in its await_resume expression, also
1321 : contains an implicit dereference. As a result, we don't
1322 : need to warn about them here. */
1323 6422309 : && TREE_CODE (TREE_OPERAND (expr, 0)) != CO_AWAIT_EXPR)
1324 72 : switch (implicit)
1325 : {
1326 36 : case ICV_CAST:
1327 36 : warning_at (loc, 0, "conversion to void will not access "
1328 : "object of type %qT", type);
1329 36 : break;
1330 0 : case ICV_SECOND_OF_COND:
1331 0 : warning_at (loc, 0, "implicit dereference will not access "
1332 : "object of type %qT in second operand of "
1333 : "conditional expression", type);
1334 0 : break;
1335 0 : case ICV_THIRD_OF_COND:
1336 0 : warning_at (loc, 0, "implicit dereference will not access "
1337 : "object of type %qT in third operand of "
1338 : "conditional expression", type);
1339 0 : break;
1340 0 : case ICV_RIGHT_OF_COMMA:
1341 0 : warning_at (loc, 0, "implicit dereference will not access "
1342 : "object of type %qT in right operand of "
1343 : "comma operator", type);
1344 0 : break;
1345 0 : case ICV_LEFT_OF_COMMA:
1346 0 : warning_at (loc, 0, "implicit dereference will not access "
1347 : "object of type %qT in left operand of comma "
1348 : "operator", type);
1349 0 : break;
1350 36 : case ICV_STATEMENT:
1351 36 : warning_at (loc, 0, "implicit dereference will not access "
1352 : "object of type %qT in statement", type);
1353 36 : break;
1354 0 : case ICV_THIRD_IN_FOR:
1355 0 : warning_at (loc, 0, "implicit dereference will not access "
1356 : "object of type %qT in for increment expression",
1357 : type);
1358 0 : break;
1359 0 : default:
1360 0 : gcc_unreachable ();
1361 : }
1362 :
1363 : /* Since this was an implicit dereference, we should also act as if
1364 : it was never there. */
1365 6422228 : return convert_to_void (TREE_OPERAND (expr, 0), implicit, complain);
1366 : }
1367 : /* Can't load the value if we don't know the type. */
1368 793347 : else if (is_volatile && !is_complete)
1369 : {
1370 18 : if (complain & tf_warning)
1371 18 : switch (implicit)
1372 : {
1373 12 : case ICV_CAST:
1374 12 : warning_at (loc, 0, "conversion to void will not access "
1375 : "object of incomplete type %qT", type);
1376 12 : break;
1377 0 : case ICV_SECOND_OF_COND:
1378 0 : warning_at (loc, 0, "indirection will not access object of "
1379 : "incomplete type %qT in second operand "
1380 : "of conditional expression", type);
1381 0 : break;
1382 0 : case ICV_THIRD_OF_COND:
1383 0 : warning_at (loc, 0, "indirection will not access object of "
1384 : "incomplete type %qT in third operand "
1385 : "of conditional expression", type);
1386 0 : break;
1387 0 : case ICV_RIGHT_OF_COMMA:
1388 0 : warning_at (loc, 0, "indirection will not access object of "
1389 : "incomplete type %qT in right operand of "
1390 : "comma operator", type);
1391 0 : break;
1392 0 : case ICV_LEFT_OF_COMMA:
1393 0 : warning_at (loc, 0, "indirection will not access object of "
1394 : "incomplete type %qT in left operand of "
1395 : "comma operator", type);
1396 0 : break;
1397 6 : case ICV_STATEMENT:
1398 6 : warning_at (loc, 0, "indirection will not access object of "
1399 : "incomplete type %qT in statement", type);
1400 6 : break;
1401 0 : case ICV_THIRD_IN_FOR:
1402 0 : warning_at (loc, 0, "indirection will not access object of "
1403 : "incomplete type %qT in for increment "
1404 : "expression", type);
1405 0 : break;
1406 0 : default:
1407 0 : gcc_unreachable ();
1408 : }
1409 : }
1410 793329 : else if (is_volatile && TREE_ADDRESSABLE (type))
1411 : {
1412 3 : if (complain & tf_warning)
1413 3 : switch (implicit)
1414 : {
1415 0 : case ICV_CAST:
1416 0 : warning_at (loc, 0, "conversion to void will not access "
1417 : "object of non-trivially-copyable type %qT",
1418 : type);
1419 0 : break;
1420 0 : case ICV_SECOND_OF_COND:
1421 0 : warning_at (loc, 0, "indirection will not access object of "
1422 : "non-trivially-copyable type %qT in second "
1423 : "operand of conditional expression", type);
1424 0 : break;
1425 0 : case ICV_THIRD_OF_COND:
1426 0 : warning_at (loc, 0, "indirection will not access object of "
1427 : "non-trivially-copyable type %qT in third "
1428 : "operand of conditional expression", type);
1429 0 : break;
1430 0 : case ICV_RIGHT_OF_COMMA:
1431 0 : warning_at (loc, 0, "indirection will not access object of "
1432 : "non-trivially-copyable type %qT in right "
1433 : "operand of comma operator", type);
1434 0 : break;
1435 0 : case ICV_LEFT_OF_COMMA:
1436 0 : warning_at (loc, 0, "indirection will not access object of "
1437 : "non-trivially-copyable type %qT in left "
1438 : "operand of comma operator", type);
1439 0 : break;
1440 3 : case ICV_STATEMENT:
1441 3 : warning_at (loc, 0, "indirection will not access object of "
1442 : "non-trivially-copyable type %qT in statement",
1443 : type);
1444 3 : break;
1445 0 : case ICV_THIRD_IN_FOR:
1446 0 : warning_at (loc, 0, "indirection will not access object of "
1447 : "non-trivially-copyable type %qT in for "
1448 : "increment expression", type);
1449 0 : break;
1450 0 : default:
1451 0 : gcc_unreachable ();
1452 : }
1453 : }
1454 793347 : if (!is_volatile || !is_complete || TREE_ADDRESSABLE (type))
1455 : {
1456 : /* Emit a warning (if enabled) when the "effect-less" INDIRECT_REF
1457 : operation is stripped off. Note that we don't warn about
1458 : - an expression with TREE_NO_WARNING set. (For an example of
1459 : such expressions, see build_over_call in call.cc.)
1460 : - automatic dereferencing of references, since the user cannot
1461 : control it. (See also warn_if_unused_value() in c-common.cc.) */
1462 793202 : if (warn_unused_value
1463 3751 : && implicit != ICV_CAST
1464 2706 : && (complain & tf_warning)
1465 9 : && !warning_suppressed_p (expr, OPT_Wunused_value)
1466 793202 : && !is_reference)
1467 9 : warning_at (loc, OPT_Wunused_value, "value computed is not used");
1468 793202 : expr = TREE_OPERAND (expr, 0);
1469 : }
1470 :
1471 : break;
1472 : }
1473 :
1474 17853 : case VAR_DECL:
1475 17853 : {
1476 : /* External variables might be incomplete. */
1477 17853 : tree type = TREE_TYPE (expr);
1478 :
1479 17853 : if (TYPE_VOLATILE (type)
1480 138 : && !COMPLETE_TYPE_P (complete_type (type))
1481 17868 : && (complain & tf_warning))
1482 15 : switch (implicit)
1483 : {
1484 12 : case ICV_CAST:
1485 12 : warning_at (loc, 0, "conversion to void will not access "
1486 : "object %qE of incomplete type %qT", expr, type);
1487 12 : break;
1488 0 : case ICV_SECOND_OF_COND:
1489 0 : warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1490 : "not be accessed in second operand of "
1491 : "conditional expression", expr, type);
1492 0 : break;
1493 0 : case ICV_THIRD_OF_COND:
1494 0 : warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1495 : "not be accessed in third operand of "
1496 : "conditional expression", expr, type);
1497 0 : break;
1498 0 : case ICV_RIGHT_OF_COMMA:
1499 0 : warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1500 : "not be accessed in right operand of comma operator",
1501 : expr, type);
1502 0 : break;
1503 0 : case ICV_LEFT_OF_COMMA:
1504 0 : warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1505 : "not be accessed in left operand of comma operator",
1506 : expr, type);
1507 0 : break;
1508 3 : case ICV_STATEMENT:
1509 3 : warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1510 : "not be accessed in statement", expr, type);
1511 3 : break;
1512 0 : case ICV_THIRD_IN_FOR:
1513 0 : warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1514 : "not be accessed in for increment expression",
1515 : expr, type);
1516 0 : break;
1517 0 : default:
1518 0 : gcc_unreachable ();
1519 : }
1520 :
1521 : break;
1522 : }
1523 :
1524 1657988 : case TARGET_EXPR:
1525 : /* Don't bother with the temporary object returned from a function if
1526 : we don't use it, don't need to destroy it, and won't abort in
1527 : assign_temp. We'll still
1528 : allocate space for it in expand_call or declare_return_variable,
1529 : but we don't need to track it through all the tree phases. */
1530 1657988 : if (TARGET_EXPR_IMPLICIT_P (expr)
1531 1657988 : && !TREE_ADDRESSABLE (TREE_TYPE (expr)))
1532 : {
1533 881935 : tree init = TARGET_EXPR_INITIAL (expr);
1534 881935 : if (TREE_CODE (init) == AGGR_INIT_EXPR
1535 881935 : && !AGGR_INIT_VIA_CTOR_P (init))
1536 : {
1537 0 : tree fn = AGGR_INIT_EXPR_FN (init);
1538 0 : expr = build_call_array_loc (input_location,
1539 0 : TREE_TYPE (TREE_TYPE
1540 : (TREE_TYPE (fn))),
1541 : fn,
1542 0 : aggr_init_expr_nargs (init),
1543 0 : AGGR_INIT_EXPR_ARGP (init));
1544 : }
1545 : }
1546 1657988 : if (complain & tf_warning)
1547 132340 : maybe_warn_nodiscard (expr, implicit);
1548 : break;
1549 :
1550 230 : case CO_AWAIT_EXPR:
1551 230 : if (auto awr = co_await_get_resume_call (expr))
1552 218 : convert_to_void (awr, implicit, complain);
1553 : break;
1554 :
1555 69598337 : default:;
1556 : }
1557 69598337 : expr = resolve_nondeduced_context (expr, complain);
1558 69598337 : if (!mark_single_function (expr, complain))
1559 9 : return error_mark_node;
1560 :
1561 69598328 : {
1562 69598328 : tree probe = expr;
1563 :
1564 69598328 : if (TREE_CODE (probe) == ADDR_EXPR)
1565 352 : probe = TREE_OPERAND (expr, 0);
1566 69598328 : if (type_unknown_p (probe))
1567 : {
1568 : /* [over.over] enumerates the places where we can take the address
1569 : of an overloaded function, and this is not one of them. */
1570 55 : if (complain & tf_error)
1571 36 : switch (implicit)
1572 : {
1573 12 : case ICV_CAST:
1574 12 : error_at (loc, "conversion to void "
1575 : "cannot resolve address of overloaded function");
1576 12 : break;
1577 0 : case ICV_SECOND_OF_COND:
1578 0 : error_at (loc, "second operand of conditional expression "
1579 : "cannot resolve address of overloaded function");
1580 0 : break;
1581 0 : case ICV_THIRD_OF_COND:
1582 0 : error_at (loc, "third operand of conditional expression "
1583 : "cannot resolve address of overloaded function");
1584 0 : break;
1585 0 : case ICV_RIGHT_OF_COMMA:
1586 0 : error_at (loc, "right operand of comma operator "
1587 : "cannot resolve address of overloaded function");
1588 0 : break;
1589 0 : case ICV_LEFT_OF_COMMA:
1590 0 : error_at (loc, "left operand of comma operator "
1591 : "cannot resolve address of overloaded function");
1592 0 : break;
1593 24 : case ICV_STATEMENT:
1594 24 : error_at (loc, "statement "
1595 : "cannot resolve address of overloaded function");
1596 24 : break;
1597 0 : case ICV_THIRD_IN_FOR:
1598 0 : error_at (loc, "for increment expression "
1599 : "cannot resolve address of overloaded function");
1600 0 : break;
1601 : }
1602 : else
1603 19 : return error_mark_node;
1604 36 : expr = void_node;
1605 : }
1606 69598273 : else if (implicit != ICV_CAST && probe == expr && is_overloaded_fn (probe))
1607 : {
1608 : /* Only warn when there is no &. */
1609 123 : if (complain & tf_warning)
1610 123 : switch (implicit)
1611 : {
1612 3 : case ICV_SECOND_OF_COND:
1613 3 : warning_at (loc, OPT_Waddress,
1614 : "second operand of conditional expression "
1615 : "is a reference, not call, to function %qE", expr);
1616 3 : break;
1617 3 : case ICV_THIRD_OF_COND:
1618 3 : warning_at (loc, OPT_Waddress,
1619 : "third operand of conditional expression "
1620 : "is a reference, not call, to function %qE", expr);
1621 3 : break;
1622 0 : case ICV_RIGHT_OF_COMMA:
1623 0 : warning_at (loc, OPT_Waddress,
1624 : "right operand of comma operator "
1625 : "is a reference, not call, to function %qE", expr);
1626 0 : break;
1627 11 : case ICV_LEFT_OF_COMMA:
1628 11 : warning_at (loc, OPT_Waddress,
1629 : "left operand of comma operator "
1630 : "is a reference, not call, to function %qE", expr);
1631 11 : break;
1632 106 : case ICV_STATEMENT:
1633 106 : warning_at (loc, OPT_Waddress,
1634 : "statement is a reference, not call, to function %qE",
1635 : expr);
1636 106 : break;
1637 0 : case ICV_THIRD_IN_FOR:
1638 0 : warning_at (loc, OPT_Waddress,
1639 : "for increment expression "
1640 : "is a reference, not call, to function %qE", expr);
1641 0 : break;
1642 0 : default:
1643 0 : gcc_unreachable ();
1644 : }
1645 :
1646 123 : if (TREE_CODE (expr) == COMPONENT_REF)
1647 9 : expr = TREE_OPERAND (expr, 0);
1648 : }
1649 : }
1650 :
1651 69598309 : if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
1652 : {
1653 68958463 : if (implicit != ICV_CAST
1654 67986482 : && warn_unused_value
1655 697218 : && !warning_suppressed_p (expr, OPT_Wunused_value)
1656 694595 : && !processing_template_decl
1657 560767 : && !cp_unevaluated_operand
1658 69462239 : && (complain & tf_warning))
1659 : {
1660 : /* The middle end does not warn about expressions that have
1661 : been explicitly cast to void, so we must do so here. */
1662 502416 : if (!TREE_SIDE_EFFECTS (expr))
1663 : {
1664 99 : switch (implicit)
1665 : {
1666 3 : case ICV_SECOND_OF_COND:
1667 3 : warning_at (loc, OPT_Wunused_value,
1668 : "second operand of conditional expression "
1669 : "has no effect");
1670 3 : break;
1671 3 : case ICV_THIRD_OF_COND:
1672 3 : warning_at (loc, OPT_Wunused_value,
1673 : "third operand of conditional expression "
1674 : "has no effect");
1675 3 : break;
1676 39 : case ICV_RIGHT_OF_COMMA:
1677 39 : warning_at (loc, OPT_Wunused_value,
1678 : "right operand of comma operator has no effect");
1679 39 : break;
1680 15 : case ICV_LEFT_OF_COMMA:
1681 15 : warning_at (loc, OPT_Wunused_value,
1682 : "left operand of comma operator has no effect");
1683 15 : break;
1684 39 : case ICV_STATEMENT:
1685 39 : warning_at (loc, OPT_Wunused_value,
1686 : "statement has no effect");
1687 36 : break;
1688 0 : case ICV_THIRD_IN_FOR:
1689 0 : warning_at (loc, OPT_Wunused_value,
1690 : "for increment expression has no effect");
1691 0 : break;
1692 0 : default:
1693 0 : gcc_unreachable ();
1694 : }
1695 : }
1696 : else
1697 : {
1698 : tree e = expr;
1699 : /* We might like to warn about (say) "(int) f()", as the
1700 : cast has no effect, but the compiler itself will
1701 : generate implicit conversions under some
1702 : circumstances. (For example a block copy will be
1703 : turned into a call to "__builtin_memcpy", with a
1704 : conversion of the return value to an appropriate
1705 : type.) So, to avoid false positives, we strip
1706 : conversions. Do not use STRIP_NOPs because it will
1707 : not strip conversions to "void", as that is not a
1708 : mode-preserving conversion. */
1709 502627 : while (TREE_CODE (e) == NOP_EXPR)
1710 310 : e = TREE_OPERAND (e, 0);
1711 :
1712 502317 : enum tree_code code = TREE_CODE (e);
1713 502317 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
1714 502317 : if (tclass == tcc_comparison
1715 : || tclass == tcc_unary
1716 502317 : || tclass == tcc_binary
1717 502242 : || code == TRUTH_NOT_EXPR
1718 502242 : || code == ADDR_EXPR
1719 : || code == VEC_PERM_EXPR
1720 502234 : || code == VEC_COND_EXPR)
1721 89 : warn_if_unused_value (e, loc);
1722 : }
1723 : }
1724 68958460 : expr = build1 (CONVERT_EXPR, void_type_node, expr);
1725 : }
1726 69598306 : if (! TREE_SIDE_EFFECTS (expr))
1727 5113060 : expr = void_node;
1728 : return expr;
1729 : }
1730 :
1731 : /* Create an expression whose value is that of EXPR,
1732 : converted to type TYPE. The TREE_TYPE of the value
1733 : is always TYPE. This function implements all reasonable
1734 : conversions; callers should filter out those that are
1735 : not permitted by the language being compiled.
1736 :
1737 : Most of this routine is from build_reinterpret_cast.
1738 :
1739 : The back end cannot call cp_convert (what was convert) because
1740 : conversions to/from basetypes may involve memory references
1741 : (vbases) and adding or subtracting small values (multiple
1742 : inheritance), but it calls convert from the constant folding code
1743 : on subtrees of already built trees after it has ripped them apart.
1744 :
1745 : Also, if we ever support range variables, we'll probably also have to
1746 : do a little bit more work. */
1747 :
1748 : tree
1749 218634740 : convert (tree type, tree expr)
1750 : {
1751 218634740 : tree intype;
1752 :
1753 218634740 : if (type == error_mark_node || expr == error_mark_node)
1754 : return error_mark_node;
1755 :
1756 218632328 : intype = TREE_TYPE (expr);
1757 :
1758 218632328 : if (INDIRECT_TYPE_P (type) && INDIRECT_TYPE_P (intype))
1759 45415655 : return build_nop (type, expr);
1760 :
1761 173216673 : return ocp_convert (type, expr, CONV_BACKEND_CONVERT,
1762 : LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
1763 173216673 : tf_warning_or_error);
1764 : }
1765 :
1766 : /* Like convert, but in a static initializer (called from
1767 : convert_and_check). */
1768 :
1769 : tree
1770 0 : convert_init (tree type, tree expr)
1771 : {
1772 0 : return convert (type, expr);
1773 : }
1774 :
1775 : /* Like cp_convert, except permit conversions to take place which
1776 : are not normally allowed due to access restrictions
1777 : (such as conversion from sub-type to private super-type). */
1778 :
1779 : tree
1780 10160111 : convert_force (tree type, tree expr, int convtype, tsubst_flags_t complain)
1781 : {
1782 10160111 : tree e = expr;
1783 10160111 : enum tree_code code = TREE_CODE (type);
1784 :
1785 10160111 : if (code == REFERENCE_TYPE)
1786 0 : return convert_to_reference (type, e, CONV_C_CAST, 0,
1787 0 : NULL_TREE, complain);
1788 :
1789 10160111 : if (code == POINTER_TYPE)
1790 10160111 : return convert_to_pointer_force (type, e, complain);
1791 :
1792 : /* From typeck.cc convert_for_assignment */
1793 0 : if (((TYPE_PTR_P (TREE_TYPE (e)) && TREE_CODE (e) == ADDR_EXPR
1794 0 : && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1795 0 : || integer_zerop (e)
1796 0 : || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1797 0 : && TYPE_PTRMEMFUNC_P (type))
1798 : /* compatible pointer to member functions. */
1799 0 : return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1,
1800 0 : /*c_cast_p=*/1, complain);
1801 :
1802 0 : return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL, complain);
1803 : }
1804 :
1805 : /* Convert an aggregate EXPR to type XTYPE. If a conversion
1806 : exists, return the attempted conversion. This may
1807 : return ERROR_MARK_NODE if the conversion is not
1808 : allowed (references private members, etc).
1809 : If no conversion exists, NULL_TREE is returned.
1810 :
1811 : FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1812 : object parameter, or by the second standard conversion sequence if
1813 : that doesn't do it. This will probably wait for an overloading rewrite.
1814 : (jason 8/9/95) */
1815 :
1816 : static tree
1817 12 : build_type_conversion (tree xtype, tree expr)
1818 : {
1819 : /* C++: check to see if we can convert this aggregate type
1820 : into the required type. */
1821 12 : return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL,
1822 0 : tf_warning_or_error);
1823 : }
1824 :
1825 : /* Convert the given EXPR to one of a group of types suitable for use in an
1826 : expression. DESIRES is a combination of various WANT_* flags (q.v.)
1827 : which indicates which types are suitable. If COMPLAIN is true, complain
1828 : about ambiguity; otherwise, the caller will deal with it. */
1829 :
1830 : tree
1831 132426451 : build_expr_type_conversion (int desires, tree expr, bool complain)
1832 : {
1833 132426451 : tree basetype = TREE_TYPE (expr);
1834 132426451 : tree conv = NULL_TREE;
1835 132426451 : tree winner = NULL_TREE;
1836 :
1837 132426451 : if (null_node_p (expr)
1838 45 : && (desires & WANT_INT)
1839 132426487 : && !(desires & WANT_NULL))
1840 : {
1841 36 : location_t loc =
1842 36 : expansion_point_location_if_in_system_header (input_location);
1843 :
1844 36 : warning_at (loc, OPT_Wconversion_null,
1845 : "converting NULL to non-pointer type");
1846 : }
1847 :
1848 132426451 : if (basetype == error_mark_node)
1849 : return error_mark_node;
1850 :
1851 132426436 : if (! MAYBE_CLASS_TYPE_P (basetype))
1852 132426234 : switch (TREE_CODE (basetype))
1853 : {
1854 122164976 : case INTEGER_TYPE:
1855 122164976 : if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
1856 : return expr;
1857 : /* fall through. */
1858 :
1859 122203767 : case BOOLEAN_TYPE:
1860 125489851 : return (desires & WANT_INT) ? expr : NULL_TREE;
1861 65746 : case ENUMERAL_TYPE:
1862 67337 : return (desires & WANT_ENUM) ? expr : NULL_TREE;
1863 1751461 : case REAL_TYPE:
1864 1751506 : return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1865 7221523 : case POINTER_TYPE:
1866 9360995 : return (desires & WANT_POINTER) ? expr : NULL_TREE;
1867 :
1868 1105078 : case FUNCTION_TYPE:
1869 1105078 : case ARRAY_TYPE:
1870 1105078 : return (desires & WANT_POINTER) ? decay_conversion (expr,
1871 : tf_warning_or_error)
1872 : : NULL_TREE;
1873 :
1874 63235 : case VECTOR_TYPE:
1875 63235 : if (!gnu_vector_type_p (basetype))
1876 : return NULL_TREE;
1877 : /* FALLTHROUGH */
1878 78595 : case COMPLEX_TYPE:
1879 78595 : if ((desires & WANT_VECTOR_OR_COMPLEX) == 0)
1880 : return NULL_TREE;
1881 35199 : switch (TREE_CODE (TREE_TYPE (basetype)))
1882 : {
1883 770 : case INTEGER_TYPE:
1884 770 : case BOOLEAN_TYPE:
1885 770 : return (desires & WANT_INT) ? expr : NULL_TREE;
1886 0 : case ENUMERAL_TYPE:
1887 0 : return (desires & WANT_ENUM) ? expr : NULL_TREE;
1888 34429 : case REAL_TYPE:
1889 34435 : return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1890 : default:
1891 : return NULL_TREE;
1892 : }
1893 :
1894 : default:
1895 : return NULL_TREE;
1896 : }
1897 :
1898 : /* The code for conversions from class type is currently only used for
1899 : delete expressions. Other expressions are handled by build_new_op. */
1900 202 : if (!complete_type_or_maybe_complain (basetype, expr, complain))
1901 0 : return error_mark_node;
1902 202 : if (!TYPE_HAS_CONVERSION (basetype))
1903 : return NULL_TREE;
1904 :
1905 407 : for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1906 : {
1907 220 : int win = 0;
1908 220 : tree candidate;
1909 220 : tree cand = TREE_VALUE (conv);
1910 220 : cand = OVL_FIRST (cand);
1911 :
1912 220 : if (winner && winner == cand)
1913 0 : continue;
1914 :
1915 220 : if (DECL_NONCONVERTING_P (cand))
1916 3 : continue;
1917 :
1918 217 : candidate = non_reference (TREE_TYPE (TREE_TYPE (cand)));
1919 :
1920 217 : switch (TREE_CODE (candidate))
1921 : {
1922 160 : case BOOLEAN_TYPE:
1923 160 : case INTEGER_TYPE:
1924 160 : win = (desires & WANT_INT); break;
1925 15 : case ENUMERAL_TYPE:
1926 15 : win = (desires & WANT_ENUM); break;
1927 3 : case REAL_TYPE:
1928 3 : win = (desires & WANT_FLOAT); break;
1929 36 : case POINTER_TYPE:
1930 36 : win = (desires & WANT_POINTER); break;
1931 :
1932 0 : case COMPLEX_TYPE:
1933 0 : case VECTOR_TYPE:
1934 0 : if ((desires & WANT_VECTOR_OR_COMPLEX) == 0)
1935 : break;
1936 0 : switch (TREE_CODE (TREE_TYPE (candidate)))
1937 : {
1938 0 : case BOOLEAN_TYPE:
1939 0 : case INTEGER_TYPE:
1940 0 : win = (desires & WANT_INT); break;
1941 0 : case ENUMERAL_TYPE:
1942 0 : win = (desires & WANT_ENUM); break;
1943 0 : case REAL_TYPE:
1944 0 : win = (desires & WANT_FLOAT); break;
1945 : default:
1946 : break;
1947 : }
1948 : break;
1949 :
1950 3 : default:
1951 : /* A wildcard could be instantiated to match any desired
1952 : type, but we can't deduce the template argument. */
1953 3 : if (WILDCARD_TYPE_P (candidate))
1954 : win = true;
1955 : break;
1956 : }
1957 :
1958 214 : if (win)
1959 : {
1960 205 : if (TREE_CODE (cand) == TEMPLATE_DECL)
1961 : {
1962 6 : if (complain)
1963 6 : error ("default type conversion cannot deduce template"
1964 : " argument for %qD", cand);
1965 6 : return error_mark_node;
1966 : }
1967 :
1968 199 : if (winner)
1969 : {
1970 15 : tree winner_type
1971 15 : = non_reference (TREE_TYPE (TREE_TYPE (winner)));
1972 :
1973 15 : if (!same_type_ignoring_top_level_qualifiers_p (winner_type,
1974 : candidate))
1975 : {
1976 3 : if (complain)
1977 : {
1978 3 : auto_diagnostic_group d;
1979 3 : error ("ambiguous default type conversion from %qT",
1980 : basetype);
1981 3 : inform (input_location,
1982 : " candidate conversions include %qD and %qD",
1983 : winner, cand);
1984 3 : }
1985 3 : return error_mark_node;
1986 : }
1987 : }
1988 :
1989 : winner = cand;
1990 : }
1991 : }
1992 :
1993 187 : if (winner)
1994 : {
1995 181 : tree type = non_reference (TREE_TYPE (TREE_TYPE (winner)));
1996 181 : return build_user_type_conversion (type, expr, LOOKUP_NORMAL,
1997 181 : tf_warning_or_error);
1998 : }
1999 :
2000 : return NULL_TREE;
2001 : }
2002 :
2003 : /* Implements integral promotion (4.1) and float->double promotion. */
2004 :
2005 : tree
2006 439792327 : type_promotes_to (tree type)
2007 : {
2008 443426068 : tree promoted_type;
2009 :
2010 443426068 : if (type == error_mark_node)
2011 : return error_mark_node;
2012 :
2013 443426068 : type = TYPE_MAIN_VARIANT (type);
2014 :
2015 : /* Check for promotions of target-defined types first. */
2016 443426068 : promoted_type = targetm.promoted_type (type);
2017 443426068 : if (promoted_type)
2018 : return promoted_type;
2019 :
2020 : /* bool always promotes to int (not unsigned), even if it's the same
2021 : size. */
2022 443426068 : if (TREE_CODE (type) == BOOLEAN_TYPE)
2023 5559109 : type = integer_type_node;
2024 :
2025 : /* Normally convert enums to int, but convert wide enums to something
2026 : wider. Scoped enums don't promote, but pretend they do for backward
2027 : ABI bug compatibility wrt varargs. */
2028 437866959 : else if (TREE_CODE (type) == ENUMERAL_TYPE
2029 418471639 : || type == char8_type_node
2030 418135524 : || type == char16_type_node
2031 417858151 : || type == char32_type_node
2032 417128491 : || type == wchar_type_node)
2033 : {
2034 21875218 : tree prom = type;
2035 :
2036 21875218 : if (TREE_CODE (type) == ENUMERAL_TYPE)
2037 : {
2038 19395320 : prom = ENUM_UNDERLYING_TYPE (prom);
2039 19395320 : if (!ENUM_IS_SCOPED (type)
2040 19395320 : && ENUM_FIXED_UNDERLYING_TYPE_P (type))
2041 : {
2042 : /* ISO C++17, 7.6/4. A prvalue of an unscoped enumeration type
2043 : whose underlying type is fixed (10.2) can be converted to a
2044 : prvalue of its underlying type. Moreover, if integral promotion
2045 : can be applied to its underlying type, a prvalue of an unscoped
2046 : enumeration type whose underlying type is fixed can also be
2047 : converted to a prvalue of the promoted underlying type. */
2048 : return type_promotes_to (prom);
2049 : }
2050 : }
2051 :
2052 18241477 : int precision = MAX (TYPE_PRECISION (type),
2053 : TYPE_PRECISION (integer_type_node));
2054 18241477 : tree totype = c_common_type_for_size (precision, 0);
2055 18241477 : if (TYPE_UNSIGNED (prom)
2056 18241477 : && ! int_fits_type_p (TYPE_MAX_VALUE (prom), totype))
2057 915464 : prom = c_common_type_for_size (precision, 1);
2058 : else
2059 : prom = totype;
2060 18241477 : if (SCOPED_ENUM_P (type))
2061 : {
2062 63 : if (abi_version_crosses (6)
2063 33 : && TYPE_MODE (prom) != TYPE_MODE (type))
2064 60 : warning (OPT_Wabi, "scoped enum %qT passed through %<...%> as "
2065 : "%qT before %<-fabi-version=6%>, %qT after",
2066 30 : type, prom, ENUM_UNDERLYING_TYPE (type));
2067 33 : if (!abi_version_at_least (6))
2068 18241447 : type = prom;
2069 : }
2070 : else
2071 : type = prom;
2072 : }
2073 415991741 : else if (c_promoting_integer_type_p (type))
2074 : {
2075 : /* Retain unsignedness if really not getting bigger. */
2076 11319416 : if (TYPE_UNSIGNED (type)
2077 11319416 : && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2078 0 : type = unsigned_type_node;
2079 : else
2080 11319416 : type = integer_type_node;
2081 : }
2082 404672325 : else if (type == float_type_node)
2083 10590813 : type = double_type_node;
2084 :
2085 : return type;
2086 : }
2087 :
2088 : /* The routines below this point are carefully written to conform to
2089 : the standard. They use the same terminology, and follow the rules
2090 : closely. Although they are used only in pt.cc at the moment, they
2091 : should presumably be used everywhere in the future. */
2092 :
2093 : /* True iff EXPR can be converted to TYPE via a qualification conversion.
2094 : Callers should check for identical types before calling this function. */
2095 :
2096 : bool
2097 57 : can_convert_qual (tree type, tree expr)
2098 : {
2099 57 : tree expr_type = TREE_TYPE (expr);
2100 57 : gcc_assert (!same_type_p (type, expr_type));
2101 :
2102 : /* A function pointer conversion also counts as a Qualification Adjustment
2103 : under [over.ics.scs]. */
2104 57 : if (fnptr_conv_p (type, expr_type))
2105 : return true;
2106 :
2107 47 : if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type))
2108 8 : return comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type));
2109 39 : else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (expr_type))
2110 38 : return (same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
2111 : TYPE_PTRMEM_CLASS_TYPE (expr_type))
2112 73 : && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type),
2113 35 : TYPE_PTRMEM_POINTED_TO_TYPE (expr_type)));
2114 : else
2115 : return false;
2116 : }
2117 :
2118 : /* Attempt to perform qualification conversions on EXPR to convert it
2119 : to TYPE. Return the resulting expression, or error_mark_node if
2120 : the conversion was impossible. Since this is only used by
2121 : convert_nontype_argument, we fold the conversion. */
2122 :
2123 : tree
2124 3818 : perform_qualification_conversions (tree type, tree expr)
2125 : {
2126 3818 : tree expr_type;
2127 :
2128 3818 : expr_type = TREE_TYPE (expr);
2129 :
2130 3818 : if (same_type_p (type, expr_type))
2131 : return expr;
2132 21 : else if (can_convert_qual (type, expr))
2133 14 : return cp_fold_convert (type, expr);
2134 : else
2135 7 : return error_mark_node;
2136 : }
2137 :
2138 : /* True iff T is a transaction-safe function type. */
2139 :
2140 : bool
2141 8380309 : tx_safe_fn_type_p (tree t)
2142 : {
2143 8380309 : if (!FUNC_OR_METHOD_TYPE_P (t))
2144 : return false;
2145 8374084 : return !!lookup_attribute ("transaction_safe", TYPE_ATTRIBUTES (t));
2146 : }
2147 :
2148 : /* Return the transaction-unsafe variant of transaction-safe function type
2149 : T. */
2150 :
2151 : tree
2152 444 : tx_unsafe_fn_variant (tree t)
2153 : {
2154 444 : gcc_assert (tx_safe_fn_type_p (t));
2155 444 : tree attrs = remove_attribute ("transaction_safe",
2156 444 : TYPE_ATTRIBUTES (t));
2157 444 : return cp_build_type_attribute_variant (t, attrs);
2158 : }
2159 :
2160 : /* Return true iff FROM can convert to TO by a transaction-safety
2161 : conversion. */
2162 :
2163 : static bool
2164 155218220 : can_convert_tx_safety (tree to, tree from)
2165 : {
2166 5761 : return (flag_tm && tx_safe_fn_type_p (from)
2167 155218662 : && same_type_p (to, tx_unsafe_fn_variant (from)));
2168 : }
2169 :
2170 : /* Return true iff FROM can convert to TO by dropping noexcept.
2171 : This is just a subroutine of fnptr_conv_p. */
2172 :
2173 : static bool
2174 156135340 : noexcept_conv_p (tree to, tree from)
2175 : {
2176 156135340 : if (!flag_noexcept_type)
2177 : return false;
2178 :
2179 155743333 : if (TREE_CODE (to) != TREE_CODE (from))
2180 : return false;
2181 91743540 : if (!FUNC_OR_METHOD_TYPE_P (from))
2182 : return false;
2183 5055405 : if (!type_throw_all_p (to)
2184 5055405 : || type_throw_all_p (from))
2185 92713 : return false;
2186 4962692 : tree v = build_exception_variant (from, NULL_TREE);
2187 4962692 : return same_type_p (to, v);
2188 : }
2189 :
2190 : /* Return true iff FROM can convert to TO by a function pointer conversion. */
2191 :
2192 : bool
2193 156135340 : fnptr_conv_p (tree to, tree from)
2194 : {
2195 156135340 : tree t = to;
2196 156135340 : tree f = from;
2197 1871 : if (TYPE_PTRMEMFUNC_P (t)
2198 156137211 : && TYPE_PTRMEMFUNC_P (f))
2199 : {
2200 1800 : t = TYPE_PTRMEMFUNC_FN_TYPE (t);
2201 1800 : f = TYPE_PTRMEMFUNC_FN_TYPE (f);
2202 : }
2203 156135340 : if (INDIRECT_TYPE_P (t)
2204 151323176 : && INDIRECT_TYPE_P (f))
2205 : {
2206 151323129 : t = TREE_TYPE (t);
2207 151323129 : f = TREE_TYPE (f);
2208 : }
2209 :
2210 156135340 : return (noexcept_conv_p (t, f)
2211 156135340 : || can_convert_tx_safety (t, f));
2212 : }
2213 :
2214 : /* Return FN with any NOP_EXPRs stripped that represent function pointer
2215 : conversions or conversions to the same type. */
2216 :
2217 : tree
2218 1777 : strip_fnptr_conv (tree fn)
2219 : {
2220 1796 : while (TREE_CODE (fn) == NOP_EXPR)
2221 : {
2222 41 : tree op = TREE_OPERAND (fn, 0);
2223 41 : tree ft = TREE_TYPE (fn);
2224 41 : tree ot = TREE_TYPE (op);
2225 82 : if (same_type_p (ft, ot)
2226 41 : || fnptr_conv_p (ft, ot))
2227 : fn = op;
2228 : else
2229 : break;
2230 : }
2231 1777 : return fn;
2232 : }
|