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