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 37929252 : cp_convert_to_pointer (tree type, tree expr, bool dofold,
77 : tsubst_flags_t complain)
78 : {
79 37929252 : tree intype = TREE_TYPE (expr);
80 37929252 : enum tree_code form;
81 37929252 : tree rval;
82 37929252 : location_t loc = cp_expr_loc_or_input_loc (expr);
83 :
84 37929252 : if (intype == error_mark_node)
85 : return error_mark_node;
86 :
87 37929252 : 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 37929252 : if (TYPE_PTR_P (type)
111 37929252 : && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
112 37765558 : || VOID_TYPE_P (TREE_TYPE (type))))
113 : {
114 12 : if (TYPE_PTRMEMFUNC_P (intype)
115 3486670 : || TREE_CODE (intype) == METHOD_TYPE)
116 9 : return convert_member_func_to_ptr (type, expr, complain);
117 3486658 : if (TYPE_PTR_P (TREE_TYPE (expr)))
118 2065117 : return build_nop (type, expr);
119 1421541 : intype = TREE_TYPE (expr);
120 : }
121 :
122 35864126 : if (expr == error_mark_node)
123 : return error_mark_node;
124 :
125 35864126 : form = TREE_CODE (intype);
126 :
127 35864126 : if (INDIRECT_TYPE_P (intype))
128 : {
129 24148659 : intype = TYPE_MAIN_VARIANT (intype);
130 :
131 24148659 : if (TYPE_MAIN_VARIANT (type) != intype
132 17197896 : && TYPE_PTR_P (type)
133 17188981 : && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
134 7759668 : && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
135 7673771 : && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
136 31748065 : && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
137 : {
138 7599406 : enum tree_code code = PLUS_EXPR;
139 7599406 : tree binfo;
140 7599406 : tree intype_class;
141 7599406 : tree type_class;
142 7599406 : bool same_p;
143 :
144 7599406 : intype_class = TREE_TYPE (intype);
145 7599406 : type_class = TREE_TYPE (type);
146 :
147 7599406 : same_p = same_type_p (TYPE_MAIN_VARIANT (intype_class),
148 : TYPE_MAIN_VARIANT (type_class));
149 7599406 : binfo = NULL_TREE;
150 : /* Try derived to base conversion. */
151 7599406 : if (!same_p)
152 31523 : binfo = lookup_base (intype_class, type_class, ba_check,
153 : NULL, complain);
154 7599406 : 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 7599406 : if (binfo == error_mark_node)
162 : return error_mark_node;
163 7599397 : if (binfo || same_p)
164 : {
165 7599397 : if (binfo)
166 31514 : expr = build_base_path (code, expr, binfo, 0, complain);
167 : /* Add any qualifier conversions. */
168 7599397 : return build_nop (type, expr);
169 : }
170 : }
171 :
172 16549253 : 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 16549253 : return build_nop (type, expr);
181 : }
182 1817 : else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
183 11717144 : || (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 11715292 : 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 11715292 : if (null_ptr_cst_p (expr))
208 : {
209 7606055 : 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 7605381 : if (complain & tf_warning)
214 5270673 : 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 7605381 : tree val = (TYPE_PTRDATAMEM_P (type)
219 7605381 : ? build_int_cst_type (type, -1)
220 7603704 : : build_int_cst (type, 0));
221 :
222 7605381 : return (TREE_SIDE_EFFECTS (expr)
223 7605381 : ? build2 (COMPOUND_EXPR, type, expr, val) : val);
224 : }
225 4109237 : 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 4109237 : if (INTEGRAL_CODE_P (form))
233 : {
234 4127996 : if (TYPE_PRECISION (intype) == POINTER_SIZE)
235 4088853 : return build1 (CONVERT_EXPR, type, expr);
236 20378 : 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 61134 : 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 20378 : if (!dofold)
247 20378 : 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 10233087 : convert_to_pointer_force (tree type, tree expr, tsubst_flags_t complain)
267 : {
268 10233087 : tree intype = TREE_TYPE (expr);
269 10233087 : enum tree_code form = TREE_CODE (intype);
270 :
271 10233087 : if (form == POINTER_TYPE)
272 : {
273 10233087 : intype = TYPE_MAIN_VARIANT (intype);
274 :
275 10233087 : if (TYPE_MAIN_VARIANT (type) != intype
276 3282305 : && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
277 3282274 : && MAYBE_CLASS_TYPE_P (TREE_TYPE (type))
278 3282271 : && MAYBE_CLASS_TYPE_P (TREE_TYPE (intype))
279 13515358 : && TREE_CODE (TREE_TYPE (intype)) == RECORD_TYPE)
280 : {
281 3282271 : enum tree_code code = PLUS_EXPR;
282 3282271 : tree binfo;
283 :
284 3282271 : binfo = lookup_base (TREE_TYPE (intype), TREE_TYPE (type),
285 : ba_unique, NULL, complain);
286 3282271 : 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 3282271 : if (binfo == error_mark_node)
293 : return error_mark_node;
294 3282271 : if (binfo)
295 : {
296 3282268 : expr = build_base_path (code, expr, binfo, 0, complain);
297 3282268 : if (expr == error_mark_node)
298 : return error_mark_node;
299 : /* Add any qualifier conversions. */
300 3282268 : if (!same_type_p (TREE_TYPE (TREE_TYPE (expr)),
301 : TREE_TYPE (type)))
302 380200 : expr = build_nop (type, expr);
303 : return expr;
304 : }
305 : }
306 : }
307 :
308 6950819 : 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 2548930165 : convert_from_reference (tree val)
537 : {
538 2548930165 : if (TREE_TYPE (val)
539 2548930165 : && TYPE_REF_P (TREE_TYPE (val)))
540 : {
541 276920233 : tree t = TREE_TYPE (TREE_TYPE (val));
542 276920233 : tree ref = build1 (INDIRECT_REF, t, val);
543 :
544 276920233 : 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 276920233 : TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
549 276920233 : TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
550 276920233 : TREE_SIDE_EFFECTS (ref)
551 276920233 : = (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (val));
552 276920233 : val = ref;
553 : }
554 :
555 2548930165 : 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 37938770 : force_rvalue (tree expr, tsubst_flags_t complain)
563 : {
564 37938770 : tree type = TREE_TYPE (expr);
565 37938770 : if (MAYBE_CLASS_TYPE_P (type) && TREE_CODE (expr) != TARGET_EXPR)
566 : {
567 142351 : releasing_vec args (make_tree_vector_single (expr));
568 142351 : expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
569 : &args, type, LOOKUP_NORMAL, complain);
570 142351 : expr = build_cplus_new (type, expr, complain);
571 142351 : }
572 : else
573 37796419 : expr = decay_conversion (expr, complain);
574 :
575 37938770 : return expr;
576 : }
577 :
578 : /* Force EXPR to be an lvalue, if it isn't already. */
579 :
580 : tree
581 532780 : force_lvalue (tree expr, tsubst_flags_t complain)
582 : {
583 532780 : 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 532780 : 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 238032682 : ignore_overflows (tree expr, tree orig)
598 : {
599 238032682 : tree stripped_expr = tree_strip_any_location_wrapper (expr);
600 238032682 : tree stripped_orig = tree_strip_any_location_wrapper (orig);
601 :
602 238032682 : if (TREE_CODE (stripped_expr) == INTEGER_CST
603 158237862 : && TREE_CODE (stripped_orig) == INTEGER_CST
604 396267744 : && TREE_OVERFLOW (stripped_expr) != TREE_OVERFLOW (stripped_orig))
605 : {
606 4487 : gcc_assert (!TREE_OVERFLOW (stripped_orig));
607 : /* Ensure constant sharing. */
608 4487 : stripped_expr = wide_int_to_tree (TREE_TYPE (stripped_expr),
609 8974 : wi::to_wide (stripped_expr));
610 : }
611 :
612 238032682 : 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 76137652 : cp_fold_convert (tree type, tree expr)
621 : {
622 76137652 : tree conv;
623 76137652 : if (TREE_TYPE (expr) == type)
624 : conv = expr;
625 30947755 : else if ((TREE_CODE (expr) == PTRMEM_CST
626 316 : && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
627 : PTRMEM_CST_CLASS (expr)))
628 30947758 : || (REFLECT_EXPR_P (expr) && REFLECTION_TYPE_P (type)))
629 : {
630 : /* Avoid wrapping a PTRMEM_CST/REFLECT_EXPR in NOP_EXPR. */
631 325 : conv = copy_node (expr);
632 325 : TREE_TYPE (conv) = type;
633 : }
634 30947430 : 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 30947424 : conv = fold_convert (type, expr);
643 30947424 : conv = ignore_overflows (conv, expr);
644 : }
645 :
646 76137652 : if (TREE_CODE (expr) == TREE_CODE (conv))
647 46786957 : copy_warning (conv, expr);
648 :
649 76137652 : return conv;
650 : }
651 :
652 : /* C++ conversions, preference to static cast conversions. */
653 :
654 : tree
655 337712190 : cp_convert (tree type, tree expr, tsubst_flags_t complain)
656 : {
657 337712190 : 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 110504036 : cp_convert_and_check (tree type, tree expr, tsubst_flags_t complain)
669 : {
670 110504036 : tree result, expr_for_warning = expr;
671 :
672 110504036 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
673 117 : expr = TREE_OPERAND (expr, 0);
674 110504036 : if (TREE_TYPE (expr) == type)
675 : return expr;
676 110504023 : if (expr == error_mark_node)
677 : return expr;
678 110504023 : result = cp_convert (type, expr, complain);
679 :
680 110504023 : if ((complain & tf_warning)
681 102935535 : && c_inhibit_evaluation_warnings == 0)
682 : {
683 101248058 : tree folded = cp_fully_fold (expr_for_warning);
684 101248058 : tree folded_result;
685 101248058 : if (folded == expr)
686 : folded_result = result;
687 : else
688 : {
689 : /* Avoid bogus -Wparentheses warnings. */
690 36716716 : warning_sentinel w (warn_parentheses);
691 36716716 : warning_sentinel c (warn_int_in_bool_context);
692 36716716 : folded_result = cp_convert (type, folded, tf_none);
693 36716716 : }
694 101248058 : folded_result = fold_simple (folded_result);
695 87388646 : if (!TREE_OVERFLOW_P (folded)
696 188636701 : && folded_result != error_mark_node)
697 162821548 : 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 518880791 : ocp_convert (tree type, tree expr, int convtype, int flags,
710 : tsubst_flags_t complain)
711 : {
712 518880791 : tree e = expr;
713 518880791 : enum tree_code code = TREE_CODE (type);
714 518880791 : const char *invalid_conv_diag;
715 518880791 : tree e1;
716 518880791 : location_t loc = cp_expr_loc_or_input_loc (expr);
717 518880791 : bool dofold = (convtype & CONV_FOLD);
718 :
719 518880791 : if (error_operand_p (e) || type == error_mark_node)
720 91 : return error_mark_node;
721 :
722 518880700 : if (TREE_CODE (e) == COMPOUND_EXPR)
723 : {
724 486197 : e = ocp_convert (type, TREE_OPERAND (e, 1), convtype, flags, complain);
725 486197 : if (e == error_mark_node)
726 : return error_mark_node;
727 486197 : if (e == TREE_OPERAND (expr, 1))
728 : return expr;
729 483986 : e = build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (e),
730 483986 : TREE_OPERAND (expr, 0), e);
731 483986 : copy_warning (e, expr);
732 483986 : return e;
733 : }
734 :
735 518394503 : complete_type (type);
736 518394503 : complete_type (TREE_TYPE (expr));
737 :
738 1036789006 : if ((invalid_conv_diag
739 518394503 : = 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 518394503 : if (!CLASS_TYPE_P (type))
748 : {
749 490205050 : e = mark_rvalue_use (e);
750 490205050 : tree v = scalar_constant_value (e);
751 490205050 : if (!error_operand_p (v))
752 490204989 : e = v;
753 : }
754 518394503 : if (error_operand_p (e))
755 0 : return error_mark_node;
756 :
757 518394503 : if (NULLPTR_TYPE_P (type) && null_ptr_cst_p (e))
758 : {
759 5208 : if (complain & tf_warning)
760 2734 : maybe_warn_zero_as_null_pointer_constant (e, loc);
761 :
762 5208 : if (!TREE_SIDE_EFFECTS (e))
763 5197 : return nullptr_node;
764 : }
765 :
766 518389306 : if (MAYBE_CLASS_TYPE_P (type) && (convtype & CONV_FORCE_TEMP))
767 : /* We need a new temporary; don't take this shortcut. */;
768 516823572 : else if (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (e)))
769 : {
770 249389012 : tree etype = TREE_TYPE (e);
771 249389012 : 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 23210371 : else if (TREE_CODE (type) == COMPLEX_TYPE)
782 0 : return convert_to_complex_maybe_fold (type, e, dofold);
783 23210371 : else if (VECTOR_TYPE_P (type))
784 3851 : return convert_to_vector (type, rvalue (e));
785 23206520 : 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 210258 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, etype));
790 210258 : TREE_TYPE (e) = TREE_TYPE (TARGET_EXPR_SLOT (e)) = type;
791 210258 : return e;
792 : }
793 22996262 : 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 22996196 : gcc_assert (!TREE_ADDRESSABLE (type));
804 22996196 : return build_nop (type, e);
805 : }
806 : }
807 :
808 269000294 : e1 = targetm.convert_to_type (type, e);
809 269000294 : if (e1)
810 : return e1;
811 :
812 269000294 : if (code == VOID_TYPE && (convtype & CONV_STATIC))
813 : {
814 38216 : e = convert_to_void (e, ICV_CAST, complain);
815 38216 : return e;
816 : }
817 :
818 268962078 : if (INTEGRAL_CODE_P (code))
819 : {
820 215754550 : tree intype = TREE_TYPE (e);
821 215754550 : tree converted;
822 :
823 215754550 : if (TREE_CODE (type) == ENUMERAL_TYPE)
824 : {
825 : /* enum = enum, enum = int, enum = float, (enum)pointer are all
826 : errors. */
827 738892 : if (((INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
828 21 : || SCALAR_FLOAT_TYPE_P (intype))
829 738892 : && ! (convtype & CONV_STATIC))
830 738892 : || 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 a complete enumeration type. If the enumeration
842 : type has a fixed underlying type, the value is first converted
843 : to that type by integral promotion or integral conversion, if
844 : necessary, and then to the enumeration type. If the
845 : enumeration type does not have a fixed underlying type, the
846 : value is unchanged if the original value is within the range
847 : of the enumeration values, and otherwise, the behavior is
848 : undefined. A value of floating-point type can also be
849 : explicitly converted to an enumeration type. The resulting
850 : value is the same as converting the original value to the
851 : underlying type of the enumeration, and subsequently to the
852 : enumeration type. */
853 738892 : if ((ENUM_FIXED_UNDERLYING_TYPE_P (type)
854 567134 : && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
855 738907 : || SCALAR_FLOAT_TYPE_P (intype))
856 : {
857 567140 : e = ocp_convert (ENUM_UNDERLYING_TYPE (type), e, convtype,
858 : flags, complain);
859 567140 : if (e == error_mark_node)
860 : return error_mark_node;
861 : }
862 :
863 738892 : tree val = fold_for_warn (e);
864 738892 : if ((complain & tf_warning)
865 737894 : && TREE_CODE (val) == INTEGER_CST
866 31029 : && ENUM_UNDERLYING_TYPE (type)
867 769918 : && !int_fits_type_p (val, ENUM_UNDERLYING_TYPE (type)))
868 118 : warning_at (loc, OPT_Wconversion,
869 : "the result of the conversion is unspecified because "
870 : "%qE is outside the range of type %qT",
871 : expr, type);
872 : }
873 215754550 : if (MAYBE_CLASS_TYPE_P (intype))
874 : {
875 9 : tree rval;
876 18 : rval = build_type_conversion (type, e);
877 9 : if (rval)
878 : return rval;
879 0 : if (complain & tf_error)
880 0 : error_at (loc, "%q#T used where a %qT was expected", intype, type);
881 0 : return error_mark_node;
882 : }
883 215754541 : if (code == BOOLEAN_TYPE)
884 : {
885 8669283 : if (VOID_TYPE_P (intype))
886 : {
887 18 : if (complain & tf_error)
888 18 : error_at (loc,
889 : "could not convert %qE from %<void%> to %<bool%>",
890 : expr);
891 18 : return error_mark_node;
892 : }
893 :
894 8669265 : if (VECTOR_TYPE_P (intype) && !gnu_vector_type_p (intype))
895 : {
896 0 : if (complain & tf_error)
897 0 : error_at (loc, "could not convert %qE from %qH to %qI", expr,
898 0 : TREE_TYPE (expr), type);
899 0 : return error_mark_node;
900 : }
901 :
902 : /* We can't implicitly convert a scoped enum to bool, so convert
903 : to the underlying type first. */
904 8669265 : if (SCOPED_ENUM_P (intype) && (convtype & CONV_STATIC))
905 115 : e = build_nop (ENUM_UNDERLYING_TYPE (intype), e);
906 8669265 : if (complain & tf_warning)
907 5159144 : e = cp_truthvalue_conversion (e, complain);
908 : else
909 : {
910 : /* Prevent bogus -Wint-in-bool-context warnings coming
911 : from c_common_truthvalue_conversion down the line. */
912 3510121 : warning_sentinel w (warn_int_in_bool_context);
913 3510121 : warning_sentinel c (warn_sign_compare);
914 3510121 : e = cp_truthvalue_conversion (e, complain);
915 3510121 : }
916 :
917 : /* Sometimes boolean types don't match if a non-standard boolean
918 : type has been invented by the target. */
919 8669265 : if (tree e2 = targetm.convert_to_type (type, e))
920 : return e2;
921 :
922 8669265 : return e;
923 : }
924 :
925 207085258 : converted = convert_to_integer_maybe_fold (type, e, dofold);
926 :
927 : /* Ignore any integer overflow caused by the conversion. */
928 207085258 : return ignore_overflows (converted, e);
929 : }
930 53207528 : if (INDIRECT_TYPE_P (type) || TYPE_PTRMEM_P (type))
931 30978433 : return cp_convert_to_pointer (type, e, dofold, complain);
932 22229095 : if (code == VECTOR_TYPE)
933 : {
934 5137 : tree in_vtype = TREE_TYPE (e);
935 5137 : if (MAYBE_CLASS_TYPE_P (in_vtype))
936 : {
937 0 : tree ret_val;
938 0 : ret_val = build_type_conversion (type, e);
939 0 : if (ret_val)
940 : return ret_val;
941 0 : if (complain & tf_error)
942 0 : error_at (loc, "%q#T used where a %qT was expected",
943 : in_vtype, type);
944 0 : return error_mark_node;
945 : }
946 5137 : return convert_to_vector (type, rvalue (e));
947 : }
948 22223958 : if (code == REAL_TYPE || code == COMPLEX_TYPE)
949 : {
950 20658218 : if (MAYBE_CLASS_TYPE_P (TREE_TYPE (e)))
951 : {
952 0 : tree rval;
953 0 : rval = build_type_conversion (type, e);
954 0 : if (rval)
955 : return rval;
956 0 : else if (complain & tf_error)
957 0 : error_at (loc,
958 : "%q#T used where a floating-point value was expected",
959 0 : TREE_TYPE (e));
960 : }
961 20658218 : if (code == REAL_TYPE)
962 20251154 : return convert_to_real_maybe_fold (type, e, dofold);
963 407064 : else if (code == COMPLEX_TYPE)
964 407064 : return convert_to_complex_maybe_fold (type, e, dofold);
965 : }
966 :
967 : /* New C++ semantics: since assignment is now based on
968 : memberwise copying, if the rhs type is derived from the
969 : lhs type, then we may still do a conversion. */
970 1565740 : if (RECORD_OR_UNION_CODE_P (code))
971 : {
972 1565734 : tree dtype = TREE_TYPE (e);
973 1565734 : tree ctor = NULL_TREE;
974 :
975 1565734 : dtype = TYPE_MAIN_VARIANT (dtype);
976 :
977 : /* Conversion between aggregate types. New C++ semantics allow
978 : objects of derived type to be cast to objects of base type.
979 : Old semantics only allowed this between pointers.
980 :
981 : There may be some ambiguity between using a constructor
982 : vs. using a type conversion operator when both apply. */
983 :
984 1565734 : ctor = e;
985 :
986 1565734 : if (abstract_virtuals_error (NULL_TREE, type, complain))
987 9 : return error_mark_node;
988 :
989 1124 : if (BRACE_ENCLOSED_INITIALIZER_P (ctor)
990 : /* We don't want to create a TARGET_EXPR in a template by the
991 : build_cplus_new below. */
992 1565734 : || processing_template_decl)
993 20093 : ctor = perform_implicit_conversion_flags (type, ctor, complain, flags);
994 1545632 : else if ((flags & LOOKUP_ONLYCONVERTING)
995 1545632 : && ! (CLASS_TYPE_P (dtype) && DERIVED_FROM_P (type, dtype)))
996 : /* For copy-initialization, first we create a temp of the proper type
997 : with a user-defined conversion sequence, then we direct-initialize
998 : the target with the temp (see [dcl.init]). */
999 24129 : ctor = build_user_type_conversion (type, ctor, flags, complain);
1000 : else
1001 : {
1002 1521503 : releasing_vec ctor_vec (make_tree_vector_single (ctor));
1003 1521503 : ctor = build_special_member_call (NULL_TREE,
1004 : complete_ctor_identifier,
1005 : &ctor_vec,
1006 : type, flags, complain);
1007 1521503 : }
1008 1565725 : if (ctor)
1009 1565498 : return build_cplus_new (type, ctor, complain);
1010 : }
1011 :
1012 233 : if (complain & tf_error)
1013 : {
1014 : /* If the conversion failed and expr was an invalid use of pointer to
1015 : member function, try to report a meaningful error. */
1016 233 : if (invalid_nonstatic_memfn_p (loc, expr, complain))
1017 : /* We displayed the error message. */;
1018 : else
1019 : {
1020 230 : auto_diagnostic_group d;
1021 230 : error_at (loc, "conversion from %qH to non-scalar type %qI requested",
1022 230 : TREE_TYPE (expr), type);
1023 230 : maybe_show_nonconverting_candidate (type, TREE_TYPE (expr), expr,
1024 : flags);
1025 230 : }
1026 : }
1027 233 : return error_mark_node;
1028 : }
1029 :
1030 : /* If CALL is a call, return the callee; otherwise null. */
1031 :
1032 : tree
1033 1417874866 : cp_get_callee (tree call)
1034 : {
1035 1417874866 : if (call == NULL_TREE)
1036 : return call;
1037 1417874866 : else if (TREE_CODE (call) == CALL_EXPR)
1038 1327260369 : return CALL_EXPR_FN (call);
1039 90614497 : else if (TREE_CODE (call) == AGGR_INIT_EXPR)
1040 61620497 : return AGGR_INIT_EXPR_FN (call);
1041 : return NULL_TREE;
1042 : }
1043 :
1044 : /* FN is the callee of a CALL_EXPR or AGGR_INIT_EXPR; return the FUNCTION_DECL
1045 : if we can. */
1046 :
1047 : tree
1048 1184408343 : cp_get_fndecl_from_callee (tree fn, bool fold /* = true */)
1049 : {
1050 1184408343 : if (fn == NULL_TREE)
1051 : return fn;
1052 :
1053 : /* We evaluate constexpr functions on the original, pre-genericization
1054 : bodies. So block-scope extern declarations have not been mapped to
1055 : declarations in outer scopes. Use the namespace-scope declaration,
1056 : if any, so that retrieve_constexpr_fundef can find it (PR111132). */
1057 2279242751 : auto fn_or_local_alias = [] (tree f)
1058 : {
1059 1108308668 : if (DECL_LOCAL_DECL_P (f))
1060 58969 : if (tree alias = DECL_LOCAL_DECL_ALIAS (f))
1061 58969 : if (alias != error_mark_node)
1062 58969 : return alias;
1063 : return f;
1064 : };
1065 :
1066 1170934083 : if (TREE_CODE (fn) == FUNCTION_DECL)
1067 11978373 : return fn_or_local_alias (fn);
1068 1158955710 : tree type = TREE_TYPE (fn);
1069 1158955710 : if (type == NULL_TREE || !INDIRECT_TYPE_P (type))
1070 : return NULL_TREE;
1071 1107693400 : if (fold)
1072 3827834 : fn = fold_non_dependent_expr (fn);
1073 1107693400 : STRIP_NOPS (fn);
1074 1107693400 : if (TREE_CODE (fn) == ADDR_EXPR
1075 1107693400 : || TREE_CODE (fn) == FDESC_EXPR)
1076 1096330399 : fn = TREE_OPERAND (fn, 0);
1077 1107693400 : if (TREE_CODE (fn) == FUNCTION_DECL)
1078 1096330295 : return fn_or_local_alias (fn);
1079 : return NULL_TREE;
1080 : }
1081 :
1082 : /* Like get_callee_fndecl, but handles AGGR_INIT_EXPR as well and uses the
1083 : constexpr machinery. */
1084 :
1085 : tree
1086 0 : cp_get_callee_fndecl (tree call)
1087 : {
1088 0 : return cp_get_fndecl_from_callee (cp_get_callee (call));
1089 : }
1090 :
1091 : /* As above, but not using the constexpr machinery. */
1092 :
1093 : tree
1094 421588438 : cp_get_callee_fndecl_nofold (tree call)
1095 : {
1096 421588438 : return cp_get_fndecl_from_callee (cp_get_callee (call), false);
1097 : }
1098 :
1099 : /* Subroutine of convert_to_void. Warn if we're discarding something with
1100 : attribute [[nodiscard]]. */
1101 :
1102 : static void
1103 6452534 : maybe_warn_nodiscard (tree expr, impl_conv_void implicit)
1104 : {
1105 6452534 : if (!warn_unused_result || c_inhibit_evaluation_warnings)
1106 : return;
1107 :
1108 6441678 : tree call = expr;
1109 6441678 : if (TREE_CODE (expr) == TARGET_EXPR)
1110 143518 : call = TARGET_EXPR_INITIAL (expr);
1111 6441678 : location_t loc = cp_expr_loc_or_input_loc (call);
1112 6441678 : tree callee = cp_get_callee (call);
1113 6441678 : if (!callee || !TREE_TYPE (callee))
1114 : return;
1115 :
1116 6439637 : tree type = TREE_TYPE (callee);
1117 6439637 : if (TYPE_PTRMEMFUNC_P (type))
1118 0 : type = TYPE_PTRMEMFUNC_FN_TYPE (type);
1119 6439637 : if (INDIRECT_TYPE_P (type))
1120 3827834 : type = TREE_TYPE (type);
1121 6439637 : if (!FUNC_OR_METHOD_TYPE_P (type))
1122 : return;
1123 :
1124 6410691 : tree rettype = TREE_TYPE (type);
1125 6410691 : tree fn = cp_get_fndecl_from_callee (callee);
1126 6410691 : tree attr;
1127 6410691 : if (implicit != ICV_CAST && fn
1128 6410691 : && (attr = lookup_attribute ("nodiscard", DECL_ATTRIBUTES (fn))))
1129 : {
1130 398 : escaped_string msg;
1131 398 : tree args = TREE_VALUE (attr);
1132 398 : if (args)
1133 63 : msg.escape (TREE_STRING_POINTER (TREE_VALUE (args)));
1134 398 : const char *format
1135 398 : = (msg
1136 398 : ? G_("ignoring return value of %qD, "
1137 : "declared with attribute %<nodiscard%>: %qs")
1138 : : G_("ignoring return value of %qD, "
1139 335 : "declared with attribute %<nodiscard%>%s"));
1140 398 : const char *raw_msg = msg ? (const char *) msg : "";
1141 398 : auto_diagnostic_group d;
1142 398 : auto_urlify_attributes sentinel;
1143 398 : if (warning_at (loc, OPT_Wunused_result, format, fn, raw_msg))
1144 398 : inform (DECL_SOURCE_LOCATION (fn), "declared here");
1145 398 : }
1146 6410293 : else if (implicit != ICV_CAST
1147 6410293 : && (attr = lookup_attribute ("nodiscard", TYPE_ATTRIBUTES (rettype))))
1148 : {
1149 121 : escaped_string msg;
1150 121 : tree args = TREE_VALUE (attr);
1151 121 : if (args)
1152 54 : msg.escape (TREE_STRING_POINTER (TREE_VALUE (args)));
1153 121 : const char *format
1154 121 : = (msg
1155 121 : ? G_("ignoring returned value of type %qT, "
1156 : "declared with attribute %<nodiscard%>: %qs")
1157 : : G_("ignoring returned value of type %qT, "
1158 67 : "declared with attribute %<nodiscard%>%s"));
1159 121 : const char *raw_msg = msg ? (const char *) msg : "";
1160 121 : auto_diagnostic_group d;
1161 121 : auto_urlify_attributes sentinel;
1162 121 : if (warning_at (loc, OPT_Wunused_result, format, rettype, raw_msg))
1163 : {
1164 121 : if (fn)
1165 22 : inform (DECL_SOURCE_LOCATION (fn),
1166 : "in call to %qD, declared here", fn);
1167 121 : inform (DECL_SOURCE_LOCATION (TYPE_NAME (rettype)),
1168 : "%qT declared here", rettype);
1169 : }
1170 121 : }
1171 6410172 : else if (TREE_CODE (expr) == TARGET_EXPR
1172 6410172 : && lookup_attribute ("warn_unused_result", TYPE_ATTRIBUTES (type)))
1173 : {
1174 : /* The TARGET_EXPR confuses do_warn_unused_result into thinking that the
1175 : result is used, so handle that case here. */
1176 63 : auto_urlify_attributes sentinel;
1177 63 : if (fn)
1178 : {
1179 63 : auto_diagnostic_group d;
1180 63 : if (warning_at (loc, OPT_Wunused_result,
1181 : "ignoring return value of %qD, "
1182 : "declared with attribute %<warn_unused_result%>",
1183 : fn))
1184 63 : inform (DECL_SOURCE_LOCATION (fn), "declared here");
1185 63 : }
1186 : else
1187 0 : warning_at (loc, OPT_Wunused_result,
1188 : "ignoring return value of function "
1189 : "declared with attribute %<warn_unused_result%>");
1190 63 : }
1191 : }
1192 :
1193 : /* When an expression is used in a void context, its value is discarded and
1194 : no lvalue-rvalue and similar conversions happen [expr.static.cast/4,
1195 : stmt.expr/1, expr.comma/1]. This permits dereferencing an incomplete type
1196 : in a void context. The C++ standard does not define what an `access' to an
1197 : object is, but there is reason to believe that it is the lvalue to rvalue
1198 : conversion -- if it were not, `*&*p = 1' would violate [expr]/4 in that it
1199 : accesses `*p' not to calculate the value to be stored. But, dcl.type.cv/8
1200 : indicates that volatile semantics should be the same between C and C++
1201 : where ever possible. C leaves it implementation defined as to what
1202 : constitutes an access to a volatile. So, we interpret `*vp' as a read of
1203 : the volatile object `vp' points to, unless that is an incomplete type. For
1204 : volatile references we do not do this interpretation, because that would
1205 : make it impossible to ignore the reference return value from functions. We
1206 : issue warnings in the confusing cases.
1207 :
1208 : The IMPLICIT is ICV_CAST when the user is explicitly converting an expression
1209 : to void via a cast. If an expression is being implicitly converted, IMPLICIT
1210 : indicates the context of the implicit conversion. */
1211 :
1212 : tree
1213 104322213 : convert_to_void (tree expr, impl_conv_void implicit, tsubst_flags_t complain)
1214 : {
1215 110863909 : location_t loc = cp_expr_loc_or_input_loc (expr);
1216 :
1217 110863909 : if (expr == error_mark_node
1218 110863909 : || TREE_TYPE (expr) == error_mark_node)
1219 : return error_mark_node;
1220 :
1221 110859682 : expr = maybe_undo_parenthesized_ref (expr);
1222 :
1223 110859682 : if (invalid_nonstatic_memfn_p (loc, expr, complain))
1224 25 : return error_mark_node;
1225 110859657 : if (TREE_CODE (expr) == PSEUDO_DTOR_EXPR)
1226 : {
1227 6 : if (complain & tf_error)
1228 6 : error_at (loc, "pseudo-destructor is not called");
1229 6 : return error_mark_node;
1230 : }
1231 :
1232 : /* Explicitly evaluate void-converted concept checks since their
1233 : satisfaction may produce ill-formed programs. */
1234 110859651 : if (concept_check_p (expr) && !cp_unevaluated_operand)
1235 25 : expr = evaluate_concept_check (expr);
1236 :
1237 110859651 : if (VOID_TYPE_P (TREE_TYPE (expr)))
1238 : return expr;
1239 :
1240 77346081 : expr = mark_discarded_use (expr);
1241 77346081 : if (implicit == ICV_CAST)
1242 : /* An explicit cast to void avoids all -Wunused-but-set* warnings. */
1243 1124600 : mark_exp_read (expr);
1244 :
1245 77346081 : switch (TREE_CODE (expr))
1246 : {
1247 142034 : case COND_EXPR:
1248 142034 : {
1249 : /* The two parts of a cond expr might be separate lvalues. */
1250 142034 : tree op1 = TREE_OPERAND (expr,1);
1251 142034 : tree op2 = TREE_OPERAND (expr,2);
1252 142028 : bool side_effects = ((op1 && TREE_SIDE_EFFECTS (op1))
1253 269726 : || TREE_SIDE_EFFECTS (op2));
1254 142034 : tree new_op1, new_op2;
1255 142034 : new_op1 = NULL_TREE;
1256 142034 : if (implicit != ICV_CAST && !side_effects)
1257 : {
1258 69 : if (op1)
1259 66 : new_op1 = convert_to_void (op1, ICV_SECOND_OF_COND, complain);
1260 69 : new_op2 = convert_to_void (op2, ICV_THIRD_OF_COND, complain);
1261 : }
1262 : else
1263 : {
1264 141965 : if (op1)
1265 141962 : new_op1 = convert_to_void (op1, ICV_CAST, complain);
1266 141965 : new_op2 = convert_to_void (op2, ICV_CAST, complain);
1267 : }
1268 :
1269 142034 : expr = build3_loc (loc, COND_EXPR, TREE_TYPE (new_op2),
1270 142034 : TREE_OPERAND (expr, 0), new_op1, new_op2);
1271 142034 : break;
1272 : }
1273 :
1274 511050 : case COMPOUND_EXPR:
1275 511050 : {
1276 : /* The second part of a compound expr contains the value. */
1277 511050 : tree op1 = TREE_OPERAND (expr,1);
1278 511050 : tree new_op1;
1279 511050 : if (implicit != ICV_CAST && !warning_suppressed_p (expr /* What warning? */))
1280 195779 : new_op1 = convert_to_void (op1, ICV_RIGHT_OF_COMMA, complain);
1281 : else
1282 315271 : new_op1 = convert_to_void (op1, ICV_CAST, complain);
1283 :
1284 511050 : if (new_op1 != op1)
1285 : {
1286 511050 : tree t = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (new_op1),
1287 511050 : TREE_OPERAND (expr, 0), new_op1);
1288 511050 : expr = t;
1289 : }
1290 :
1291 : break;
1292 : }
1293 :
1294 : case NON_LVALUE_EXPR:
1295 : case NOP_EXPR:
1296 : /* These have already decayed to rvalue. */
1297 : break;
1298 :
1299 8403729 : case CALL_EXPR: /* We have a special meaning for volatile void fn(). */
1300 : /* cdtors may return this or void, depending on
1301 : targetm.cxx.cdtor_returns_this, but this shouldn't affect our
1302 : decisions here: neither nodiscard warnings (nodiscard dtors
1303 : are nonsensical and ctors have a different behavior with that
1304 : attribute that is handled in the TARGET_EXPR case), nor should
1305 : any constexpr or template instantiations be affected by an ABI
1306 : property that is, or at least ought to be transparent to the
1307 : language. */
1308 8403729 : if (tree fn = cp_get_callee_fndecl_nofold (expr))
1309 15453326 : if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
1310 : return expr;
1311 :
1312 8403729 : if (complain & tf_warning)
1313 6308429 : maybe_warn_nodiscard (expr, implicit);
1314 : break;
1315 :
1316 7342260 : case INDIRECT_REF:
1317 7342260 : {
1318 7342260 : tree type = TREE_TYPE (expr);
1319 7342260 : int is_reference = TYPE_REF_P (TREE_TYPE (TREE_OPERAND (expr, 0)));
1320 7342260 : int is_volatile = TYPE_VOLATILE (type);
1321 7342260 : if (is_volatile)
1322 433 : complete_type (type);
1323 7342260 : int is_complete = COMPLETE_TYPE_P (type);
1324 :
1325 : /* Don't load the value if this is an implicit dereference, or if
1326 : the type needs to be handled by ctors/dtors. */
1327 7342260 : if (is_reference)
1328 : {
1329 267 : if (is_volatile && (complain & tf_warning)
1330 : /* A co_await expression, in its await_resume expression, also
1331 : contains an implicit dereference. As a result, we don't
1332 : need to warn about them here. */
1333 6541777 : && TREE_CODE (TREE_OPERAND (expr, 0)) != CO_AWAIT_EXPR)
1334 72 : switch (implicit)
1335 : {
1336 36 : case ICV_CAST:
1337 36 : warning_at (loc, 0, "conversion to void will not access "
1338 : "object of type %qT", type);
1339 36 : break;
1340 0 : case ICV_SECOND_OF_COND:
1341 0 : warning_at (loc, 0, "implicit dereference will not access "
1342 : "object of type %qT in second operand of "
1343 : "conditional expression", type);
1344 0 : break;
1345 0 : case ICV_THIRD_OF_COND:
1346 0 : warning_at (loc, 0, "implicit dereference will not access "
1347 : "object of type %qT in third operand of "
1348 : "conditional expression", type);
1349 0 : break;
1350 0 : case ICV_RIGHT_OF_COMMA:
1351 0 : warning_at (loc, 0, "implicit dereference will not access "
1352 : "object of type %qT in right operand of "
1353 : "comma operator", type);
1354 0 : break;
1355 0 : case ICV_LEFT_OF_COMMA:
1356 0 : warning_at (loc, 0, "implicit dereference will not access "
1357 : "object of type %qT in left operand of comma "
1358 : "operator", type);
1359 0 : break;
1360 36 : case ICV_STATEMENT:
1361 36 : warning_at (loc, 0, "implicit dereference will not access "
1362 : "object of type %qT in statement", type);
1363 36 : break;
1364 0 : case ICV_THIRD_IN_FOR:
1365 0 : warning_at (loc, 0, "implicit dereference will not access "
1366 : "object of type %qT in for increment expression",
1367 : type);
1368 0 : break;
1369 0 : default:
1370 0 : gcc_unreachable ();
1371 : }
1372 :
1373 : /* Since this was an implicit dereference, we should also act as if
1374 : it was never there. */
1375 6541696 : return convert_to_void (TREE_OPERAND (expr, 0), implicit, complain);
1376 : }
1377 : /* Can't load the value if we don't know the type. */
1378 800564 : else if (is_volatile && !is_complete)
1379 : {
1380 18 : if (complain & tf_warning)
1381 18 : switch (implicit)
1382 : {
1383 12 : case ICV_CAST:
1384 12 : warning_at (loc, 0, "conversion to void will not access "
1385 : "object of incomplete type %qT", type);
1386 12 : break;
1387 0 : case ICV_SECOND_OF_COND:
1388 0 : warning_at (loc, 0, "indirection will not access object of "
1389 : "incomplete type %qT in second operand "
1390 : "of conditional expression", type);
1391 0 : break;
1392 0 : case ICV_THIRD_OF_COND:
1393 0 : warning_at (loc, 0, "indirection will not access object of "
1394 : "incomplete type %qT in third operand "
1395 : "of conditional expression", type);
1396 0 : break;
1397 0 : case ICV_RIGHT_OF_COMMA:
1398 0 : warning_at (loc, 0, "indirection will not access object of "
1399 : "incomplete type %qT in right operand of "
1400 : "comma operator", type);
1401 0 : break;
1402 0 : case ICV_LEFT_OF_COMMA:
1403 0 : warning_at (loc, 0, "indirection will not access object of "
1404 : "incomplete type %qT in left operand of "
1405 : "comma operator", type);
1406 0 : break;
1407 6 : case ICV_STATEMENT:
1408 6 : warning_at (loc, 0, "indirection will not access object of "
1409 : "incomplete type %qT in statement", type);
1410 6 : break;
1411 0 : case ICV_THIRD_IN_FOR:
1412 0 : warning_at (loc, 0, "indirection will not access object of "
1413 : "incomplete type %qT in for increment "
1414 : "expression", type);
1415 0 : break;
1416 0 : default:
1417 0 : gcc_unreachable ();
1418 : }
1419 : }
1420 800546 : else if (is_volatile && TREE_ADDRESSABLE (type))
1421 : {
1422 3 : if (complain & tf_warning)
1423 3 : switch (implicit)
1424 : {
1425 0 : case ICV_CAST:
1426 0 : warning_at (loc, 0, "conversion to void will not access "
1427 : "object of non-trivially-copyable type %qT",
1428 : type);
1429 0 : break;
1430 0 : case ICV_SECOND_OF_COND:
1431 0 : warning_at (loc, 0, "indirection will not access object of "
1432 : "non-trivially-copyable type %qT in second "
1433 : "operand of conditional expression", type);
1434 0 : break;
1435 0 : case ICV_THIRD_OF_COND:
1436 0 : warning_at (loc, 0, "indirection will not access object of "
1437 : "non-trivially-copyable type %qT in third "
1438 : "operand of conditional expression", type);
1439 0 : break;
1440 0 : case ICV_RIGHT_OF_COMMA:
1441 0 : warning_at (loc, 0, "indirection will not access object of "
1442 : "non-trivially-copyable type %qT in right "
1443 : "operand of comma operator", type);
1444 0 : break;
1445 0 : case ICV_LEFT_OF_COMMA:
1446 0 : warning_at (loc, 0, "indirection will not access object of "
1447 : "non-trivially-copyable type %qT in left "
1448 : "operand of comma operator", type);
1449 0 : break;
1450 3 : case ICV_STATEMENT:
1451 3 : warning_at (loc, 0, "indirection will not access object of "
1452 : "non-trivially-copyable type %qT in statement",
1453 : type);
1454 3 : break;
1455 0 : case ICV_THIRD_IN_FOR:
1456 0 : warning_at (loc, 0, "indirection will not access object of "
1457 : "non-trivially-copyable type %qT in for "
1458 : "increment expression", type);
1459 0 : break;
1460 0 : default:
1461 0 : gcc_unreachable ();
1462 : }
1463 : }
1464 800564 : if (!is_volatile || !is_complete || TREE_ADDRESSABLE (type))
1465 : {
1466 : /* Emit a warning (if enabled) when the "effect-less" INDIRECT_REF
1467 : operation is stripped off. Note that we don't warn about
1468 : - an expression with TREE_NO_WARNING set. (For an example of
1469 : such expressions, see build_over_call in call.cc.)
1470 : - automatic dereferencing of references, since the user cannot
1471 : control it. (See also warn_if_unused_value() in c-common.cc.) */
1472 800419 : if (warn_unused_value
1473 3735 : && implicit != ICV_CAST
1474 2690 : && (complain & tf_warning)
1475 9 : && !warning_suppressed_p (expr, OPT_Wunused_value)
1476 800419 : && !is_reference)
1477 9 : warning_at (loc, OPT_Wunused_value, "value computed is not used");
1478 800419 : expr = TREE_OPERAND (expr, 0);
1479 : }
1480 :
1481 : break;
1482 : }
1483 :
1484 18035 : case VAR_DECL:
1485 18035 : {
1486 : /* External variables might be incomplete. */
1487 18035 : tree type = TREE_TYPE (expr);
1488 :
1489 18035 : if (TYPE_VOLATILE (type)
1490 138 : && !COMPLETE_TYPE_P (complete_type (type))
1491 18050 : && (complain & tf_warning))
1492 15 : switch (implicit)
1493 : {
1494 12 : case ICV_CAST:
1495 12 : warning_at (loc, 0, "conversion to void will not access "
1496 : "object %qE of incomplete type %qT", expr, type);
1497 12 : break;
1498 0 : case ICV_SECOND_OF_COND:
1499 0 : warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1500 : "not be accessed in second operand of "
1501 : "conditional expression", expr, type);
1502 0 : break;
1503 0 : case ICV_THIRD_OF_COND:
1504 0 : warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1505 : "not be accessed in third operand of "
1506 : "conditional expression", expr, type);
1507 0 : break;
1508 0 : case ICV_RIGHT_OF_COMMA:
1509 0 : warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1510 : "not be accessed in right operand of comma operator",
1511 : expr, type);
1512 0 : break;
1513 0 : case ICV_LEFT_OF_COMMA:
1514 0 : warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1515 : "not be accessed in left operand of comma operator",
1516 : expr, type);
1517 0 : break;
1518 3 : case ICV_STATEMENT:
1519 3 : warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1520 : "not be accessed in statement", expr, type);
1521 3 : break;
1522 0 : case ICV_THIRD_IN_FOR:
1523 0 : warning_at (loc, 0, "variable %qE of incomplete type %qT will "
1524 : "not be accessed in for increment expression",
1525 : expr, type);
1526 0 : break;
1527 0 : default:
1528 0 : gcc_unreachable ();
1529 : }
1530 :
1531 : break;
1532 : }
1533 :
1534 1705007 : case TARGET_EXPR:
1535 : /* Don't bother with the temporary object returned from a function if
1536 : we don't use it, don't need to destroy it, and won't abort in
1537 : assign_temp. We'll still
1538 : allocate space for it in expand_call or declare_return_variable,
1539 : but we don't need to track it through all the tree phases. */
1540 1705007 : if (TARGET_EXPR_IMPLICIT_P (expr)
1541 1705007 : && !TREE_ADDRESSABLE (TREE_TYPE (expr)))
1542 : {
1543 910125 : tree init = TARGET_EXPR_INITIAL (expr);
1544 910125 : if (TREE_CODE (init) == AGGR_INIT_EXPR
1545 910125 : && !AGGR_INIT_VIA_CTOR_P (init))
1546 : {
1547 0 : tree fn = AGGR_INIT_EXPR_FN (init);
1548 0 : expr = build_call_array_loc (input_location,
1549 0 : TREE_TYPE (TREE_TYPE
1550 : (TREE_TYPE (fn))),
1551 : fn,
1552 0 : aggr_init_expr_nargs (init),
1553 0 : AGGR_INIT_EXPR_ARGP (init));
1554 : }
1555 : }
1556 1705007 : if (complain & tf_warning)
1557 144105 : maybe_warn_nodiscard (expr, implicit);
1558 : break;
1559 :
1560 230 : case CO_AWAIT_EXPR:
1561 230 : if (auto awr = co_await_get_resume_call (expr))
1562 218 : convert_to_void (awr, implicit, complain);
1563 : break;
1564 :
1565 70804385 : default:;
1566 : }
1567 70804385 : expr = resolve_nondeduced_context (expr, complain);
1568 70804385 : if (!mark_single_function (expr, complain))
1569 9 : return error_mark_node;
1570 :
1571 70804376 : {
1572 70804376 : tree probe = expr;
1573 :
1574 70804376 : if (TREE_CODE (probe) == ADDR_EXPR)
1575 353 : probe = TREE_OPERAND (expr, 0);
1576 70804376 : if (type_unknown_p (probe))
1577 : {
1578 : /* [over.over] enumerates the places where we can take the address
1579 : of an overloaded function, and this is not one of them. */
1580 55 : if (complain & tf_error)
1581 36 : switch (implicit)
1582 : {
1583 12 : case ICV_CAST:
1584 12 : error_at (loc, "conversion to void "
1585 : "cannot resolve address of overloaded function");
1586 12 : break;
1587 0 : case ICV_SECOND_OF_COND:
1588 0 : error_at (loc, "second operand of conditional expression "
1589 : "cannot resolve address of overloaded function");
1590 0 : break;
1591 0 : case ICV_THIRD_OF_COND:
1592 0 : error_at (loc, "third operand of conditional expression "
1593 : "cannot resolve address of overloaded function");
1594 0 : break;
1595 0 : case ICV_RIGHT_OF_COMMA:
1596 0 : error_at (loc, "right operand of comma operator "
1597 : "cannot resolve address of overloaded function");
1598 0 : break;
1599 0 : case ICV_LEFT_OF_COMMA:
1600 0 : error_at (loc, "left operand of comma operator "
1601 : "cannot resolve address of overloaded function");
1602 0 : break;
1603 24 : case ICV_STATEMENT:
1604 24 : error_at (loc, "statement "
1605 : "cannot resolve address of overloaded function");
1606 24 : break;
1607 0 : case ICV_THIRD_IN_FOR:
1608 0 : error_at (loc, "for increment expression "
1609 : "cannot resolve address of overloaded function");
1610 0 : break;
1611 : }
1612 : else
1613 19 : return error_mark_node;
1614 36 : expr = void_node;
1615 : }
1616 70804321 : else if (implicit != ICV_CAST && probe == expr && is_overloaded_fn (probe))
1617 : {
1618 : /* Only warn when there is no &. */
1619 123 : if (complain & tf_warning)
1620 123 : switch (implicit)
1621 : {
1622 3 : case ICV_SECOND_OF_COND:
1623 3 : warning_at (loc, OPT_Waddress,
1624 : "second operand of conditional expression "
1625 : "is a reference, not call, to function %qE", expr);
1626 3 : break;
1627 3 : case ICV_THIRD_OF_COND:
1628 3 : warning_at (loc, OPT_Waddress,
1629 : "third operand of conditional expression "
1630 : "is a reference, not call, to function %qE", expr);
1631 3 : break;
1632 0 : case ICV_RIGHT_OF_COMMA:
1633 0 : warning_at (loc, OPT_Waddress,
1634 : "right operand of comma operator "
1635 : "is a reference, not call, to function %qE", expr);
1636 0 : break;
1637 11 : case ICV_LEFT_OF_COMMA:
1638 11 : warning_at (loc, OPT_Waddress,
1639 : "left operand of comma operator "
1640 : "is a reference, not call, to function %qE", expr);
1641 11 : break;
1642 106 : case ICV_STATEMENT:
1643 106 : warning_at (loc, OPT_Waddress,
1644 : "statement is a reference, not call, to function %qE",
1645 : expr);
1646 106 : break;
1647 0 : case ICV_THIRD_IN_FOR:
1648 0 : warning_at (loc, OPT_Waddress,
1649 : "for increment expression "
1650 : "is a reference, not call, to function %qE", expr);
1651 0 : break;
1652 0 : default:
1653 0 : gcc_unreachable ();
1654 : }
1655 :
1656 123 : if (TREE_CODE (expr) == COMPONENT_REF)
1657 9 : expr = TREE_OPERAND (expr, 0);
1658 : }
1659 : }
1660 :
1661 70804357 : if (expr != error_mark_node && !VOID_TYPE_P (TREE_TYPE (expr)))
1662 : {
1663 70151237 : if (implicit != ICV_CAST
1664 69159090 : && warn_unused_value
1665 704158 : && !warning_suppressed_p (expr, OPT_Wunused_value)
1666 701522 : && !processing_template_decl
1667 567543 : && !cp_unevaluated_operand
1668 70659854 : && (complain & tf_warning))
1669 : {
1670 : /* The middle end does not warn about expressions that have
1671 : been explicitly cast to void, so we must do so here. */
1672 507253 : if (!TREE_SIDE_EFFECTS (expr))
1673 : {
1674 99 : switch (implicit)
1675 : {
1676 3 : case ICV_SECOND_OF_COND:
1677 3 : warning_at (loc, OPT_Wunused_value,
1678 : "second operand of conditional expression "
1679 : "has no effect");
1680 3 : break;
1681 3 : case ICV_THIRD_OF_COND:
1682 3 : warning_at (loc, OPT_Wunused_value,
1683 : "third operand of conditional expression "
1684 : "has no effect");
1685 3 : break;
1686 39 : case ICV_RIGHT_OF_COMMA:
1687 39 : warning_at (loc, OPT_Wunused_value,
1688 : "right operand of comma operator has no effect");
1689 39 : break;
1690 15 : case ICV_LEFT_OF_COMMA:
1691 15 : warning_at (loc, OPT_Wunused_value,
1692 : "left operand of comma operator has no effect");
1693 15 : break;
1694 39 : case ICV_STATEMENT:
1695 39 : warning_at (loc, OPT_Wunused_value,
1696 : "statement has no effect");
1697 36 : break;
1698 0 : case ICV_THIRD_IN_FOR:
1699 0 : warning_at (loc, OPT_Wunused_value,
1700 : "for increment expression has no effect");
1701 0 : break;
1702 0 : default:
1703 0 : gcc_unreachable ();
1704 : }
1705 : }
1706 : else
1707 : {
1708 : tree e = expr;
1709 : /* We might like to warn about (say) "(int) f()", as the
1710 : cast has no effect, but the compiler itself will
1711 : generate implicit conversions under some
1712 : circumstances. (For example a block copy will be
1713 : turned into a call to "__builtin_memcpy", with a
1714 : conversion of the return value to an appropriate
1715 : type.) So, to avoid false positives, we strip
1716 : conversions. Do not use STRIP_NOPs because it will
1717 : not strip conversions to "void", as that is not a
1718 : mode-preserving conversion. */
1719 507464 : while (TREE_CODE (e) == NOP_EXPR)
1720 310 : e = TREE_OPERAND (e, 0);
1721 :
1722 507154 : enum tree_code code = TREE_CODE (e);
1723 507154 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
1724 507154 : if (tclass == tcc_comparison
1725 : || tclass == tcc_unary
1726 507154 : || tclass == tcc_binary
1727 507079 : || code == TRUTH_NOT_EXPR
1728 507079 : || code == ADDR_EXPR
1729 : || code == VEC_PERM_EXPR
1730 507071 : || code == VEC_COND_EXPR)
1731 89 : warn_if_unused_value (e, loc);
1732 : }
1733 : }
1734 70151234 : expr = build1_loc (loc, CONVERT_EXPR, void_type_node, expr);
1735 : }
1736 : return expr;
1737 : }
1738 :
1739 : /* Create an expression whose value is that of EXPR,
1740 : converted to type TYPE. The TREE_TYPE of the value
1741 : is always TYPE. This function implements all reasonable
1742 : conversions; callers should filter out those that are
1743 : not permitted by the language being compiled.
1744 :
1745 : Most of this routine is from build_reinterpret_cast.
1746 :
1747 : The back end cannot call cp_convert (what was convert) because
1748 : conversions to/from basetypes may involve memory references
1749 : (vbases) and adding or subtracting small values (multiple
1750 : inheritance), but it calls convert from the constant folding code
1751 : on subtrees of already built trees after it has ripped them apart.
1752 :
1753 : Also, if we ever support range variables, we'll probably also have to
1754 : do a little bit more work. */
1755 :
1756 : tree
1757 222403479 : convert (tree type, tree expr)
1758 : {
1759 222403479 : tree intype;
1760 :
1761 222403479 : if (type == error_mark_node || expr == error_mark_node)
1762 : return error_mark_node;
1763 :
1764 222401014 : intype = TREE_TYPE (expr);
1765 :
1766 222401014 : if (INDIRECT_TYPE_P (type) && INDIRECT_TYPE_P (intype))
1767 45933051 : return build_nop (type, expr);
1768 :
1769 176467963 : return ocp_convert (type, expr, CONV_BACKEND_CONVERT,
1770 : LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
1771 176467963 : tf_warning_or_error);
1772 : }
1773 :
1774 : /* Like convert, but in a static initializer (called from
1775 : convert_and_check). */
1776 :
1777 : tree
1778 0 : convert_init (tree type, tree expr)
1779 : {
1780 0 : return convert (type, expr);
1781 : }
1782 :
1783 : /* Like cp_convert, except permit conversions to take place which
1784 : are not normally allowed due to access restrictions
1785 : (such as conversion from sub-type to private super-type). */
1786 :
1787 : tree
1788 10232843 : convert_force (tree type, tree expr, int convtype, tsubst_flags_t complain)
1789 : {
1790 10232843 : tree e = expr;
1791 10232843 : enum tree_code code = TREE_CODE (type);
1792 :
1793 10232843 : if (code == REFERENCE_TYPE)
1794 0 : return convert_to_reference (type, e, CONV_C_CAST, 0,
1795 0 : NULL_TREE, complain);
1796 :
1797 10232843 : if (code == POINTER_TYPE)
1798 10232843 : return convert_to_pointer_force (type, e, complain);
1799 :
1800 : /* From typeck.cc convert_for_assignment */
1801 0 : if (((TYPE_PTR_P (TREE_TYPE (e)) && TREE_CODE (e) == ADDR_EXPR
1802 0 : && TREE_CODE (TREE_TYPE (TREE_TYPE (e))) == METHOD_TYPE)
1803 0 : || integer_zerop (e)
1804 0 : || TYPE_PTRMEMFUNC_P (TREE_TYPE (e)))
1805 0 : && TYPE_PTRMEMFUNC_P (type))
1806 : /* compatible pointer to member functions. */
1807 0 : return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), e, 1,
1808 0 : /*c_cast_p=*/1, complain);
1809 :
1810 0 : return ocp_convert (type, e, CONV_C_CAST|convtype, LOOKUP_NORMAL, complain);
1811 : }
1812 :
1813 : /* Convert an aggregate EXPR to type XTYPE. If a conversion
1814 : exists, return the attempted conversion. This may
1815 : return ERROR_MARK_NODE if the conversion is not
1816 : allowed (references private members, etc).
1817 : If no conversion exists, NULL_TREE is returned.
1818 :
1819 : FIXME: Ambiguity checking is wrong. Should choose one by the implicit
1820 : object parameter, or by the second standard conversion sequence if
1821 : that doesn't do it. This will probably wait for an overloading rewrite.
1822 : (jason 8/9/95) */
1823 :
1824 : static tree
1825 12 : build_type_conversion (tree xtype, tree expr)
1826 : {
1827 : /* C++: check to see if we can convert this aggregate type
1828 : into the required type. */
1829 12 : return build_user_type_conversion (xtype, expr, LOOKUP_NORMAL,
1830 0 : tf_warning_or_error);
1831 : }
1832 :
1833 : /* Convert the given EXPR to one of a group of types suitable for use in an
1834 : expression. DESIRES is a combination of various WANT_* flags (q.v.)
1835 : which indicates which types are suitable. If COMPLAIN is true, complain
1836 : about ambiguity; otherwise, the caller will deal with it. */
1837 :
1838 : tree
1839 133939494 : build_expr_type_conversion (int desires, tree expr, bool complain)
1840 : {
1841 133939494 : tree basetype = TREE_TYPE (expr);
1842 133939494 : tree conv = NULL_TREE;
1843 133939494 : tree winner = NULL_TREE;
1844 :
1845 133939494 : if (null_node_p (expr)
1846 45 : && (desires & WANT_INT)
1847 133939530 : && !(desires & WANT_NULL))
1848 : {
1849 36 : location_t loc =
1850 36 : expansion_point_location_if_in_system_header (input_location);
1851 :
1852 36 : warning_at (loc, OPT_Wconversion_null,
1853 : "converting NULL to non-pointer type");
1854 : }
1855 :
1856 133939494 : if (basetype == error_mark_node)
1857 : return error_mark_node;
1858 :
1859 133939479 : if (! MAYBE_CLASS_TYPE_P (basetype))
1860 133939277 : switch (TREE_CODE (basetype))
1861 : {
1862 123589710 : case INTEGER_TYPE:
1863 123589710 : if ((desires & WANT_NULL) && null_ptr_cst_p (expr))
1864 : return expr;
1865 : /* fall through. */
1866 :
1867 123629970 : case BOOLEAN_TYPE:
1868 123629970 : return (desires & WANT_INT) ? expr : NULL_TREE;
1869 67240 : case ENUMERAL_TYPE:
1870 67240 : return (desires & WANT_ENUM) ? expr : NULL_TREE;
1871 1763630 : case REAL_TYPE:
1872 1763630 : return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1873 7288266 : case POINTER_TYPE:
1874 7288266 : return (desires & WANT_POINTER) ? expr : NULL_TREE;
1875 :
1876 1111506 : case FUNCTION_TYPE:
1877 1111506 : case ARRAY_TYPE:
1878 1111506 : return (desires & WANT_POINTER) ? decay_conversion (expr,
1879 : tf_warning_or_error)
1880 : : NULL_TREE;
1881 :
1882 63241 : case VECTOR_TYPE:
1883 63241 : if (!gnu_vector_type_p (basetype))
1884 : return NULL_TREE;
1885 : /* FALLTHROUGH */
1886 78601 : case COMPLEX_TYPE:
1887 78601 : if ((desires & WANT_VECTOR_OR_COMPLEX) == 0)
1888 : return NULL_TREE;
1889 35205 : switch (TREE_CODE (TREE_TYPE (basetype)))
1890 : {
1891 776 : case INTEGER_TYPE:
1892 776 : case BOOLEAN_TYPE:
1893 776 : return (desires & WANT_INT) ? expr : NULL_TREE;
1894 0 : case ENUMERAL_TYPE:
1895 0 : return (desires & WANT_ENUM) ? expr : NULL_TREE;
1896 34429 : case REAL_TYPE:
1897 34429 : return (desires & WANT_FLOAT) ? expr : NULL_TREE;
1898 : default:
1899 : return NULL_TREE;
1900 : }
1901 :
1902 : default:
1903 : return NULL_TREE;
1904 : }
1905 :
1906 : /* The code for conversions from class type is currently only used for
1907 : delete expressions. Other expressions are handled by build_new_op. */
1908 202 : if (!complete_type_or_maybe_complain (basetype, expr, complain))
1909 0 : return error_mark_node;
1910 202 : if (!TYPE_HAS_CONVERSION (basetype))
1911 : return NULL_TREE;
1912 :
1913 407 : for (conv = lookup_conversions (basetype); conv; conv = TREE_CHAIN (conv))
1914 : {
1915 220 : int win = 0;
1916 220 : tree candidate;
1917 220 : tree cand = TREE_VALUE (conv);
1918 220 : cand = OVL_FIRST (cand);
1919 :
1920 220 : if (winner && winner == cand)
1921 0 : continue;
1922 :
1923 220 : if (DECL_NONCONVERTING_P (cand))
1924 3 : continue;
1925 :
1926 217 : candidate = non_reference (TREE_TYPE (TREE_TYPE (cand)));
1927 :
1928 217 : switch (TREE_CODE (candidate))
1929 : {
1930 160 : case BOOLEAN_TYPE:
1931 160 : case INTEGER_TYPE:
1932 160 : win = (desires & WANT_INT); break;
1933 15 : case ENUMERAL_TYPE:
1934 15 : win = (desires & WANT_ENUM); break;
1935 3 : case REAL_TYPE:
1936 3 : win = (desires & WANT_FLOAT); break;
1937 36 : case POINTER_TYPE:
1938 36 : win = (desires & WANT_POINTER); break;
1939 :
1940 0 : case COMPLEX_TYPE:
1941 0 : case VECTOR_TYPE:
1942 0 : if ((desires & WANT_VECTOR_OR_COMPLEX) == 0)
1943 : break;
1944 0 : switch (TREE_CODE (TREE_TYPE (candidate)))
1945 : {
1946 0 : case BOOLEAN_TYPE:
1947 0 : case INTEGER_TYPE:
1948 0 : win = (desires & WANT_INT); break;
1949 0 : case ENUMERAL_TYPE:
1950 0 : win = (desires & WANT_ENUM); break;
1951 0 : case REAL_TYPE:
1952 0 : win = (desires & WANT_FLOAT); break;
1953 : default:
1954 : break;
1955 : }
1956 : break;
1957 :
1958 3 : default:
1959 : /* A wildcard could be instantiated to match any desired
1960 : type, but we can't deduce the template argument. */
1961 3 : if (WILDCARD_TYPE_P (candidate))
1962 : win = true;
1963 : break;
1964 : }
1965 :
1966 214 : if (win)
1967 : {
1968 205 : if (TREE_CODE (cand) == TEMPLATE_DECL)
1969 : {
1970 6 : if (complain)
1971 6 : error ("default type conversion cannot deduce template"
1972 : " argument for %qD", cand);
1973 6 : return error_mark_node;
1974 : }
1975 :
1976 199 : if (winner)
1977 : {
1978 15 : tree winner_type
1979 15 : = non_reference (TREE_TYPE (TREE_TYPE (winner)));
1980 :
1981 15 : if (!same_type_ignoring_top_level_qualifiers_p (winner_type,
1982 : candidate))
1983 : {
1984 3 : if (complain)
1985 : {
1986 3 : auto_diagnostic_group d;
1987 3 : error ("ambiguous default type conversion from %qT",
1988 : basetype);
1989 3 : inform (input_location,
1990 : " candidate conversions include %qD and %qD",
1991 : winner, cand);
1992 3 : }
1993 3 : return error_mark_node;
1994 : }
1995 : }
1996 :
1997 : winner = cand;
1998 : }
1999 : }
2000 :
2001 187 : if (winner)
2002 : {
2003 181 : tree type = non_reference (TREE_TYPE (TREE_TYPE (winner)));
2004 181 : return build_user_type_conversion (type, expr, LOOKUP_NORMAL,
2005 181 : tf_warning_or_error);
2006 : }
2007 :
2008 : return NULL_TREE;
2009 : }
2010 :
2011 : /* Implements integral promotion (4.1) and float->double promotion. */
2012 :
2013 : tree
2014 448096978 : type_promotes_to (tree type)
2015 : {
2016 451792853 : tree promoted_type;
2017 :
2018 451792853 : if (type == error_mark_node)
2019 : return error_mark_node;
2020 :
2021 451792853 : type = TYPE_MAIN_VARIANT (type);
2022 :
2023 : /* Check for promotions of target-defined types first. */
2024 451792853 : promoted_type = targetm.promoted_type (type);
2025 451792853 : if (promoted_type)
2026 : return promoted_type;
2027 :
2028 : /* bool always promotes to int (not unsigned), even if it's the same
2029 : size. */
2030 451792853 : if (TREE_CODE (type) == BOOLEAN_TYPE)
2031 5996449 : type = integer_type_node;
2032 :
2033 : /* Normally convert enums to int, but convert wide enums to something
2034 : wider. Scoped enums don't promote, but pretend they do for backward
2035 : ABI bug compatibility wrt varargs. */
2036 445796404 : else if (TREE_CODE (type) == ENUMERAL_TYPE
2037 426235609 : || type == char8_type_node
2038 425896664 : || type == char16_type_node
2039 425616966 : || type == char32_type_node
2040 424881388 : || type == wchar_type_node)
2041 : {
2042 22071263 : tree prom = type;
2043 :
2044 22071263 : if (TREE_CODE (type) == ENUMERAL_TYPE)
2045 : {
2046 19560795 : prom = ENUM_UNDERLYING_TYPE (prom);
2047 19560795 : if (!ENUM_IS_SCOPED (type)
2048 19560795 : && ENUM_FIXED_UNDERLYING_TYPE_P (type))
2049 : {
2050 : /* ISO C++17, 7.6/4. A prvalue of an unscoped enumeration type
2051 : whose underlying type is fixed (10.2) can be converted to a
2052 : prvalue of its underlying type. Moreover, if integral promotion
2053 : can be applied to its underlying type, a prvalue of an unscoped
2054 : enumeration type whose underlying type is fixed can also be
2055 : converted to a prvalue of the promoted underlying type. */
2056 : return type_promotes_to (prom);
2057 : }
2058 : }
2059 :
2060 18375388 : int precision = MAX (TYPE_PRECISION (type),
2061 : TYPE_PRECISION (integer_type_node));
2062 18375388 : tree totype = c_common_type_for_size (precision, 0);
2063 18375388 : if (TYPE_UNSIGNED (prom)
2064 18375388 : && ! int_fits_type_p (TYPE_MAX_VALUE (prom), totype))
2065 922485 : prom = c_common_type_for_size (precision, 1);
2066 : else
2067 : prom = totype;
2068 18375388 : if (SCOPED_ENUM_P (type))
2069 : {
2070 63 : if (abi_version_crosses (6)
2071 33 : && TYPE_MODE (prom) != TYPE_MODE (type))
2072 60 : warning (OPT_Wabi, "scoped enum %qT passed through %<...%> as "
2073 : "%qT before %<-fabi-version=6%>, %qT after",
2074 30 : type, prom, ENUM_UNDERLYING_TYPE (type));
2075 33 : if (!abi_version_at_least (6))
2076 18375358 : type = prom;
2077 : }
2078 : else
2079 : type = prom;
2080 : }
2081 423725141 : else if (c_promoting_integer_type_p (type))
2082 : {
2083 : /* Retain unsignedness if really not getting bigger. */
2084 11477540 : if (TYPE_UNSIGNED (type)
2085 11477540 : && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2086 0 : type = unsigned_type_node;
2087 : else
2088 11477540 : type = integer_type_node;
2089 : }
2090 412247601 : else if (type == float_type_node)
2091 10690886 : type = double_type_node;
2092 :
2093 : return type;
2094 : }
2095 :
2096 : /* The routines below this point are carefully written to conform to
2097 : the standard. They use the same terminology, and follow the rules
2098 : closely. Although they are used only in pt.cc at the moment, they
2099 : should presumably be used everywhere in the future. */
2100 :
2101 : /* True iff EXPR can be converted to TYPE via a qualification conversion.
2102 : Callers should check for identical types before calling this function. */
2103 :
2104 : bool
2105 109 : can_convert_qual (tree type, tree expr)
2106 : {
2107 109 : tree expr_type = TREE_TYPE (expr);
2108 109 : gcc_assert (!same_type_p (type, expr_type));
2109 :
2110 : /* A function pointer conversion also counts as a Qualification Adjustment
2111 : under [over.ics.scs]. */
2112 109 : if (fnptr_conv_p (type, expr_type))
2113 : return true;
2114 :
2115 69 : if (TYPE_PTR_P (type) && TYPE_PTR_P (expr_type))
2116 8 : return comp_ptr_ttypes (TREE_TYPE (type), TREE_TYPE (expr_type));
2117 61 : else if (TYPE_PTRMEM_P (type) && TYPE_PTRMEM_P (expr_type))
2118 60 : return (same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
2119 : TYPE_PTRMEM_CLASS_TYPE (expr_type))
2120 117 : && comp_ptr_ttypes (TYPE_PTRMEM_POINTED_TO_TYPE (type),
2121 57 : TYPE_PTRMEM_POINTED_TO_TYPE (expr_type)));
2122 : else
2123 : return false;
2124 : }
2125 :
2126 : /* Attempt to perform qualification conversions on EXPR to convert it
2127 : to TYPE. Return the resulting expression, or error_mark_node if
2128 : the conversion was impossible. Since this is only used by
2129 : convert_nontype_argument, we fold the conversion. */
2130 :
2131 : tree
2132 3826 : perform_qualification_conversions (tree type, tree expr)
2133 : {
2134 3826 : tree expr_type;
2135 :
2136 3826 : expr_type = TREE_TYPE (expr);
2137 :
2138 3826 : if (same_type_p (type, expr_type))
2139 : return expr;
2140 21 : else if (can_convert_qual (type, expr))
2141 14 : return cp_fold_convert (type, expr);
2142 : else
2143 7 : return error_mark_node;
2144 : }
2145 :
2146 : /* True iff T is a transaction-safe function type. */
2147 :
2148 : bool
2149 8461052 : tx_safe_fn_type_p (tree t)
2150 : {
2151 8461052 : if (!FUNC_OR_METHOD_TYPE_P (t))
2152 : return false;
2153 8454820 : return !!lookup_attribute ("transaction_safe", TYPE_ATTRIBUTES (t));
2154 : }
2155 :
2156 : /* Return the transaction-unsafe variant of transaction-safe function type
2157 : T. */
2158 :
2159 : tree
2160 38 : tx_unsafe_fn_variant (tree t)
2161 : {
2162 38 : gcc_assert (tx_safe_fn_type_p (t));
2163 38 : tree attrs = remove_attribute ("transaction_safe",
2164 38 : TYPE_ATTRIBUTES (t));
2165 38 : return cp_build_type_attribute_variant (t, attrs);
2166 : }
2167 :
2168 : /* Return true iff FROM can convert to TO by a transaction-safety
2169 : conversion. */
2170 :
2171 : static bool
2172 155238656 : can_convert_tx_safety (tree to, tree from)
2173 : {
2174 5355 : return (flag_tm && tx_safe_fn_type_p (from)
2175 155238692 : && same_type_p (to, tx_unsafe_fn_variant (from)));
2176 : }
2177 :
2178 : /* Return true iff FROM can convert to TO by dropping noexcept.
2179 : This is just a subroutine of fnptr_conv_p. */
2180 :
2181 : static bool
2182 155484070 : noexcept_conv_p (tree to, tree from)
2183 : {
2184 155484070 : if (!flag_noexcept_type)
2185 : return false;
2186 :
2187 155091980 : if (TREE_CODE (to) != TREE_CODE (from))
2188 : return false;
2189 87738826 : if (!FUNC_OR_METHOD_TYPE_P (from))
2190 : return false;
2191 339222 : if (!type_throw_all_p (to)
2192 339222 : || type_throw_all_p (from))
2193 : return false;
2194 246105 : tree v = build_exception_variant (from, NULL_TREE);
2195 246105 : return same_type_p (to, v);
2196 : }
2197 :
2198 : /* Return true iff FROM can convert to TO by a function pointer conversion. */
2199 :
2200 : bool
2201 155484070 : fnptr_conv_p (tree to, tree from)
2202 : {
2203 155484070 : tree t = to;
2204 155484070 : tree f = from;
2205 1921 : if (TYPE_PTRMEMFUNC_P (t)
2206 155485991 : && TYPE_PTRMEMFUNC_P (f))
2207 : {
2208 1850 : t = TYPE_PTRMEMFUNC_FN_TYPE (t);
2209 1850 : f = TYPE_PTRMEMFUNC_FN_TYPE (f);
2210 : }
2211 155484070 : if (INDIRECT_TYPE_P (t)
2212 155391600 : && INDIRECT_TYPE_P (f))
2213 : {
2214 155391553 : t = TREE_TYPE (t);
2215 155391553 : f = TREE_TYPE (f);
2216 : }
2217 :
2218 155484070 : return (noexcept_conv_p (t, f)
2219 155484070 : || can_convert_tx_safety (t, f));
2220 : }
2221 :
2222 : /* Return FN with any NOP_EXPRs stripped that represent function pointer
2223 : conversions or conversions to the same type. */
2224 :
2225 : tree
2226 1801 : strip_fnptr_conv (tree fn)
2227 : {
2228 1838 : while (TREE_CODE (fn) == NOP_EXPR)
2229 : {
2230 59 : tree op = TREE_OPERAND (fn, 0);
2231 59 : tree ft = TREE_TYPE (fn);
2232 59 : tree ot = TREE_TYPE (op);
2233 118 : if (same_type_p (ft, ot)
2234 59 : || fnptr_conv_p (ft, ot))
2235 : fn = op;
2236 : else
2237 : break;
2238 : }
2239 1801 : return fn;
2240 : }
|