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