1 /* Functions related to invoking -*- C++ -*- methods and overloaded functions. 2 Copyright (C) 1987-2024 Free Software Foundation, Inc. 3 Contributed by Michael Tiemann (tiemann (at) cygnus.com) and 4 modified by Brendan Kehoe (brendan (at) cygnus.com). 5 6 This file is part of GCC. 7 8 GCC is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 3, or (at your option) 11 any later version. 12 13 GCC is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with GCC; see the file COPYING3. If not see 20 <http://www.gnu.org/licenses/>. */ 21 22 23 /* High-level class interface. */ 24 25 #include "config.h" 26 #include "system.h" 27 #include "coretypes.h" 28 #include "target.h" 29 #include "cp-tree.h" 30 #include "timevar.h" 31 #include "stringpool.h" 32 #include "cgraph.h" 33 #include "stor-layout.h" 34 #include "trans-mem.h" 35 #include "flags.h" 36 #include "toplev.h" 37 #include "intl.h" 38 #include "convert.h" 39 #include "langhooks.h" 40 #include "c-family/c-objc.h" 41 #include "internal-fn.h" 42 #include "stringpool.h" 43 #include "attribs.h" 44 #include "decl.h" 45 #include "gcc-rich-location.h" 46 #include "tristate.h" 47 48 /* The various kinds of conversion. */ 49 50 enum conversion_kind { 51 ck_identity, 52 ck_lvalue, 53 ck_fnptr, 54 ck_qual, 55 ck_std, 56 ck_ptr, 57 ck_pmem, 58 ck_base, 59 ck_ref_bind, 60 ck_user, 61 ck_ambig, 62 ck_list, 63 ck_aggr, 64 ck_rvalue, 65 /* When LOOKUP_SHORTCUT_BAD_CONVS is set, we may return a conversion of 66 this kind whenever we know the true conversion is either bad or outright 67 invalid, but we don't want to attempt to compute the bad conversion (for 68 sake of avoiding unnecessary instantiation). bad_p should always be set 69 for these. */ 70 ck_deferred_bad, 71 }; 72 73 /* The rank of the conversion. Order of the enumerals matters; better 74 conversions should come earlier in the list. */ 75 76 enum conversion_rank { 77 cr_identity, 78 cr_exact, 79 cr_promotion, 80 cr_std, 81 cr_pbool, 82 cr_user, 83 cr_ellipsis, 84 cr_bad 85 }; 86 87 /* An implicit conversion sequence, in the sense of [over.best.ics]. 88 The first conversion to be performed is at the end of the chain. 89 That conversion is always a cr_identity conversion. */ 90 91 struct conversion { 92 /* The kind of conversion represented by this step. */ 93 conversion_kind kind; 94 /* The rank of this conversion. */ 95 conversion_rank rank; 96 BOOL_BITFIELD user_conv_p : 1; 97 BOOL_BITFIELD ellipsis_p : 1; 98 BOOL_BITFIELD this_p : 1; 99 /* True if this conversion would be permitted with a bending of 100 language standards, e.g. disregarding pointer qualifiers or 101 converting integers to pointers. */ 102 BOOL_BITFIELD bad_p : 1; 103 /* If KIND is ck_ref_bind or ck_base, true to indicate that a 104 temporary should be created to hold the result of the 105 conversion. If KIND is ck_ambig or ck_user, true means force 106 copy-initialization. */ 107 BOOL_BITFIELD need_temporary_p : 1; 108 /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion 109 from a pointer-to-derived to pointer-to-base is being performed. */ 110 BOOL_BITFIELD base_p : 1; 111 /* If KIND is ck_ref_bind, true when either an lvalue reference is 112 being bound to an lvalue expression or an rvalue reference is 113 being bound to an rvalue expression. If KIND is ck_rvalue or ck_base, 114 true when we are treating an lvalue as an rvalue (12.8p33). If 115 ck_identity, we will be binding a reference directly or decaying to 116 a pointer. */ 117 BOOL_BITFIELD rvaluedness_matches_p: 1; 118 BOOL_BITFIELD check_narrowing: 1; 119 /* Whether check_narrowing should only check TREE_CONSTANTs; used 120 in build_converted_constant_expr. */ 121 BOOL_BITFIELD check_narrowing_const_only: 1; 122 /* True if this conversion is taking place in a copy-initialization context 123 and we should only consider converting constructors. Only set in 124 ck_base and ck_rvalue. */ 125 BOOL_BITFIELD copy_init_p : 1; 126 /* The type of the expression resulting from the conversion. */ 127 tree type; 128 union { 129 /* The next conversion in the chain. Since the conversions are 130 arranged from outermost to innermost, the NEXT conversion will 131 actually be performed before this conversion. This variant is 132 used only when KIND is neither ck_identity, ck_aggr, ck_ambig nor 133 ck_list. Please use the next_conversion function instead 134 of using this field directly. */ 135 conversion *next; 136 /* The expression at the beginning of the conversion chain. This 137 variant is used only if KIND is ck_identity, ck_aggr, or ck_ambig. 138 You can use conv_get_original_expr to get this expression. */ 139 tree expr; 140 /* The array of conversions for an initializer_list, so this 141 variant is used only when KIN D is ck_list. */ 142 conversion **list; 143 } u; 144 /* The function candidate corresponding to this conversion 145 sequence. This field is only used if KIND is ck_user. */ 146 struct z_candidate *cand; 147 }; 148 149 #define CONVERSION_RANK(NODE) \ 150 ((NODE)->bad_p ? cr_bad \ 151 : (NODE)->ellipsis_p ? cr_ellipsis \ 152 : (NODE)->user_conv_p ? cr_user \ 153 : (NODE)->rank) 154 155 #define BAD_CONVERSION_RANK(NODE) \ 156 ((NODE)->ellipsis_p ? cr_ellipsis \ 157 : (NODE)->user_conv_p ? cr_user \ 158 : (NODE)->rank) 159 160 static struct obstack conversion_obstack; 161 static bool conversion_obstack_initialized; 162 struct rejection_reason; 163 164 static struct z_candidate * tourney (struct z_candidate *, tsubst_flags_t); 165 static int equal_functions (tree, tree); 166 static int joust (struct z_candidate *, struct z_candidate *, bool, 167 tsubst_flags_t); 168 static int compare_ics (conversion *, conversion *); 169 static void maybe_warn_class_memaccess (location_t, tree, 170 const vec<tree, va_gc> *); 171 static tree build_over_call (struct z_candidate *, int, tsubst_flags_t); 172 static tree convert_like (conversion *, tree, tsubst_flags_t); 173 static tree convert_like_with_context (conversion *, tree, tree, int, 174 tsubst_flags_t); 175 static void op_error (const op_location_t &, enum tree_code, enum tree_code, 176 tree, tree, tree, bool); 177 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int, 178 tsubst_flags_t); 179 static void print_z_candidate (location_t, const char *, struct z_candidate *); 180 static void print_z_candidates (location_t, struct z_candidate *, 181 tristate = tristate::unknown ()); 182 static tree build_this (tree); 183 static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *); 184 static bool any_strictly_viable (struct z_candidate *); 185 static struct z_candidate *add_template_candidate 186 (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *, 187 tree, tree, tree, int, unification_kind_t, bool, tsubst_flags_t); 188 static struct z_candidate *add_template_candidate_real 189 (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *, 190 tree, tree, tree, int, tree, unification_kind_t, bool, tsubst_flags_t); 191 static bool is_complete (tree); 192 static struct z_candidate *add_conv_candidate 193 (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, tree, 194 tree, tsubst_flags_t); 195 static struct z_candidate *add_function_candidate 196 (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *, tree, 197 tree, int, conversion**, bool, tsubst_flags_t); 198 static conversion *implicit_conversion (tree, tree, tree, bool, int, 199 tsubst_flags_t); 200 static conversion *reference_binding (tree, tree, tree, bool, int, 201 tsubst_flags_t); 202 static conversion *build_conv (conversion_kind, tree, conversion *); 203 static conversion *build_list_conv (tree, tree, int, tsubst_flags_t); 204 static conversion *next_conversion (conversion *); 205 static bool is_subseq (conversion *, conversion *); 206 static conversion *maybe_handle_ref_bind (conversion **); 207 static void maybe_handle_implicit_object (conversion **); 208 static struct z_candidate *add_candidate 209 (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, size_t, 210 conversion **, tree, tree, int, struct rejection_reason *, int); 211 static tree source_type (conversion *); 212 static void add_warning (struct z_candidate *, struct z_candidate *); 213 static conversion *direct_reference_binding (tree, conversion *); 214 static bool promoted_arithmetic_type_p (tree); 215 static conversion *conditional_conversion (tree, tree, tsubst_flags_t); 216 static char *name_as_c_string (tree, tree, bool *); 217 static tree prep_operand (tree); 218 static void add_candidates (tree, tree, const vec<tree, va_gc> *, tree, tree, 219 bool, tree, tree, int, struct z_candidate **, 220 tsubst_flags_t); 221 static conversion *merge_conversion_sequences (conversion *, conversion *); 222 static tree build_temp (tree, tree, int, diagnostic_t *, tsubst_flags_t); 223 static conversion *build_identity_conv (tree, tree); 224 static inline bool conv_binds_to_array_of_unknown_bound (conversion *); 225 static bool conv_is_prvalue (conversion *); 226 static tree prevent_lifetime_extension (tree); 227 228 /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE. 229 NAME can take many forms... */ 230 231 bool 232 check_dtor_name (tree basetype, tree name) 233 { 234 /* Just accept something we've already complained about. */ 235 if (name == error_mark_node) 236 return true; 237 238 if (TREE_CODE (name) == TYPE_DECL) 239 name = TREE_TYPE (name); 240 else if (TYPE_P (name)) 241 /* OK */; 242 else if (identifier_p (name)) 243 { 244 if ((MAYBE_CLASS_TYPE_P (basetype) 245 || TREE_CODE (basetype) == ENUMERAL_TYPE) 246 && name == constructor_name (basetype)) 247 return true; 248 249 /* Otherwise lookup the name, it could be an unrelated typedef 250 of the correct type. */ 251 name = lookup_name (name, LOOK_want::TYPE); 252 if (!name) 253 return false; 254 name = TREE_TYPE (name); 255 if (name == error_mark_node) 256 return false; 257 } 258 else 259 { 260 /* In the case of: 261 262 template <class T> struct S { ~S(); }; 263 int i; 264 i.~S(); 265 266 NAME will be a class template. */ 267 gcc_assert (DECL_CLASS_TEMPLATE_P (name)); 268 return false; 269 } 270 271 return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name)); 272 } 273 274 /* We want the address of a function or method. We avoid creating a 275 pointer-to-member function. */ 276 277 tree 278 build_addr_func (tree function, tsubst_flags_t complain) 279 { 280 tree type = TREE_TYPE (function); 281 282 /* We have to do these by hand to avoid real pointer to member 283 functions. */ 284 if (TREE_CODE (type) == METHOD_TYPE) 285 { 286 if (TREE_CODE (function) == OFFSET_REF) 287 { 288 tree object = build_address (TREE_OPERAND (function, 0)); 289 return get_member_function_from_ptrfunc (&object, 290 TREE_OPERAND (function, 1), 291 complain); 292 } 293 function = build_address (function); 294 } 295 else if (TREE_CODE (function) == FUNCTION_DECL 296 && DECL_IMMEDIATE_FUNCTION_P (function)) 297 function = build_address (function); 298 else 299 function = decay_conversion (function, complain, /*reject_builtin=*/false); 300 301 return function; 302 } 303 304 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or 305 POINTER_TYPE to those. Note, pointer to member function types 306 (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are 307 two variants. build_call_a is the primitive taking an array of 308 arguments, while build_call_n is a wrapper that handles varargs. */ 309 310 tree 311 build_call_n (tree function, int n, ...) 312 { 313 if (n == 0) 314 return build_call_a (function, 0, NULL); 315 else 316 { 317 tree *argarray = XALLOCAVEC (tree, n); 318 va_list ap; 319 int i; 320 321 va_start (ap, n); 322 for (i = 0; i < n; i++) 323 argarray[i] = va_arg (ap, tree); 324 va_end (ap); 325 return build_call_a (function, n, argarray); 326 } 327 } 328 329 /* Update various flags in cfun and the call itself based on what is being 330 called. Split out of build_call_a so that bot_manip can use it too. */ 331 332 void 333 set_flags_from_callee (tree call) 334 { 335 /* Handle both CALL_EXPRs and AGGR_INIT_EXPRs. */ 336 tree decl = cp_get_callee_fndecl_nofold (call); 337 338 /* We check both the decl and the type; a function may be known not to 339 throw without being declared throw(). */ 340 bool nothrow = decl && TREE_NOTHROW (decl); 341 tree callee = cp_get_callee (call); 342 if (callee) 343 nothrow |= TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (callee))); 344 else if (TREE_CODE (call) == CALL_EXPR 345 && internal_fn_flags (CALL_EXPR_IFN (call)) & ECF_NOTHROW) 346 nothrow = true; 347 348 if (cfun && cp_function_chain && !cp_unevaluated_operand) 349 { 350 if (!nothrow && at_function_scope_p ()) 351 cp_function_chain->can_throw = 1; 352 353 if (decl && TREE_THIS_VOLATILE (decl)) 354 current_function_returns_abnormally = 1; 355 } 356 357 TREE_NOTHROW (call) = nothrow; 358 } 359 360 tree 361 build_call_a (tree function, int n, tree *argarray) 362 { 363 tree decl; 364 tree result_type; 365 tree fntype; 366 int i; 367 368 function = build_addr_func (function, tf_warning_or_error); 369 370 gcc_assert (TYPE_PTR_P (TREE_TYPE (function))); 371 fntype = TREE_TYPE (TREE_TYPE (function)); 372 gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype)); 373 result_type = TREE_TYPE (fntype); 374 /* An rvalue has no cv-qualifiers. */ 375 if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type)) 376 result_type = cv_unqualified (result_type); 377 378 function = build_call_array_loc (input_location, 379 result_type, function, n, argarray); 380 set_flags_from_callee (function); 381 382 decl = get_callee_fndecl (function); 383 384 if (decl && !TREE_USED (decl)) 385 { 386 /* We invoke build_call directly for several library 387 functions. These may have been declared normally if 388 we're building libgcc, so we can't just check 389 DECL_ARTIFICIAL. */ 390 gcc_assert (DECL_ARTIFICIAL (decl) 391 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), 392 "__", 2)); 393 mark_used (decl); 394 } 395 396 require_complete_eh_spec_types (fntype, decl); 397 398 TREE_HAS_CONSTRUCTOR (function) = (decl && DECL_CONSTRUCTOR_P (decl)); 399 400 /* Don't pass empty class objects by value. This is useful 401 for tags in STL, which are used to control overload resolution. 402 We don't need to handle other cases of copying empty classes. */ 403 if (!decl || !fndecl_built_in_p (decl)) 404 for (i = 0; i < n; i++) 405 { 406 tree arg = CALL_EXPR_ARG (function, i); 407 if (is_empty_class (TREE_TYPE (arg)) 408 && simple_empty_class_p (TREE_TYPE (arg), arg, INIT_EXPR)) 409 { 410 while (TREE_CODE (arg) == TARGET_EXPR) 411 /* We're disconnecting the initializer from its target, 412 don't create a temporary. */ 413 arg = TARGET_EXPR_INITIAL (arg); 414 tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg)); 415 arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t); 416 CALL_EXPR_ARG (function, i) = arg; 417 } 418 } 419 420 return function; 421 } 422 423 /* New overloading code. */ 424 425 struct z_candidate; 426 427 struct candidate_warning { 428 z_candidate *loser; 429 candidate_warning *next; 430 }; 431 432 /* Information for providing diagnostics about why overloading failed. */ 433 434 enum rejection_reason_code { 435 rr_none, 436 rr_arity, 437 rr_explicit_conversion, 438 rr_template_conversion, 439 rr_arg_conversion, 440 rr_bad_arg_conversion, 441 rr_template_unification, 442 rr_invalid_copy, 443 rr_inherited_ctor, 444 rr_constraint_failure, 445 rr_ignored, 446 }; 447 448 struct conversion_info { 449 /* The index of the argument, 0-based. */ 450 int n_arg; 451 /* The actual argument or its type. */ 452 tree from; 453 /* The type of the parameter. */ 454 tree to_type; 455 /* The location of the argument. */ 456 location_t loc; 457 }; 458 459 struct rejection_reason { 460 enum rejection_reason_code code; 461 union { 462 /* Information about an arity mismatch. */ 463 struct { 464 /* The expected number of arguments. */ 465 int expected; 466 /* The actual number of arguments in the call. */ 467 int actual; 468 /* Whether EXPECTED should be treated as a lower bound. */ 469 bool least_p; 470 } arity; 471 /* Information about an argument conversion mismatch. */ 472 struct conversion_info conversion; 473 /* Same, but for bad argument conversions. */ 474 struct conversion_info bad_conversion; 475 /* Information about template unification failures. These are the 476 parameters passed to fn_type_unification. */ 477 struct { 478 tree tmpl; 479 tree explicit_targs; 480 int num_targs; 481 const tree *args; 482 unsigned int nargs; 483 tree return_type; 484 unification_kind_t strict; 485 int flags; 486 } template_unification; 487 /* Information about template instantiation failures. These are the 488 parameters passed to instantiate_template. */ 489 struct { 490 tree tmpl; 491 tree targs; 492 } template_instantiation; 493 } u; 494 }; 495 496 struct z_candidate { 497 /* The FUNCTION_DECL that will be called if this candidate is 498 selected by overload resolution. */ 499 tree fn; 500 /* If not NULL_TREE, the first argument to use when calling this 501 function. */ 502 tree first_arg; 503 /* The rest of the arguments to use when calling this function. If 504 there are no further arguments this may be NULL or it may be an 505 empty vector. */ 506 const vec<tree, va_gc> *args; 507 /* The implicit conversion sequences for each of the arguments to 508 FN. */ 509 conversion **convs; 510 /* The number of implicit conversion sequences. */ 511 size_t num_convs; 512 /* If FN is a user-defined conversion, the standard conversion 513 sequence from the type returned by FN to the desired destination 514 type. */ 515 conversion *second_conv; 516 struct rejection_reason *reason; 517 /* If FN is a member function, the binfo indicating the path used to 518 qualify the name of FN at the call site. This path is used to 519 determine whether or not FN is accessible if it is selected by 520 overload resolution. The DECL_CONTEXT of FN will always be a 521 (possibly improper) base of this binfo. */ 522 tree access_path; 523 /* If FN is a non-static member function, the binfo indicating the 524 subobject to which the `this' pointer should be converted if FN 525 is selected by overload resolution. The type pointed to by 526 the `this' pointer must correspond to the most derived class 527 indicated by the CONVERSION_PATH. */ 528 tree conversion_path; 529 tree template_decl; 530 tree explicit_targs; 531 candidate_warning *warnings; 532 z_candidate *next; 533 int viable; 534 535 /* The flags active in add_candidate. */ 536 int flags; 537 538 bool rewritten () const { return (flags & LOOKUP_REWRITTEN); } 539 bool reversed () const { return (flags & LOOKUP_REVERSED); } 540 }; 541 542 /* Returns true iff T is a null pointer constant in the sense of 543 [conv.ptr]. */ 544 545 bool 546 null_ptr_cst_p (tree t) 547 { 548 tree type = TREE_TYPE (t); 549 550 /* [conv.ptr] 551 552 A null pointer constant is an integer literal ([lex.icon]) with value 553 zero or a prvalue of type std::nullptr_t. */ 554 if (NULLPTR_TYPE_P (type)) 555 return true; 556 557 if (cxx_dialect >= cxx11) 558 { 559 STRIP_ANY_LOCATION_WRAPPER (t); 560 561 /* Core issue 903 says only literal 0 is a null pointer constant. */ 562 if (TREE_CODE (t) == INTEGER_CST 563 && !TREE_OVERFLOW (t) 564 && TREE_CODE (type) == INTEGER_TYPE 565 && integer_zerop (t) 566 && !char_type_p (type)) 567 return true; 568 } 569 else if (CP_INTEGRAL_TYPE_P (type)) 570 { 571 t = fold_non_dependent_expr (t, tf_none); 572 STRIP_NOPS (t); 573 if (integer_zerop (t) && !TREE_OVERFLOW (t)) 574 return true; 575 } 576 577 return false; 578 } 579 580 /* Returns true iff T is a null member pointer value (4.11). */ 581 582 bool 583 null_member_pointer_value_p (tree t) 584 { 585 tree type = TREE_TYPE (t); 586 if (!type) 587 return false; 588 else if (TYPE_PTRMEMFUNC_P (type)) 589 return (TREE_CODE (t) == CONSTRUCTOR 590 && CONSTRUCTOR_NELTS (t) 591 && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value)); 592 else if (TYPE_PTRDATAMEM_P (type)) 593 return integer_all_onesp (t); 594 else 595 return false; 596 } 597 598 /* Returns nonzero if PARMLIST consists of only default parms, 599 ellipsis, and/or undeduced parameter packs. */ 600 601 bool 602 sufficient_parms_p (const_tree parmlist) 603 { 604 for (; parmlist && parmlist != void_list_node; 605 parmlist = TREE_CHAIN (parmlist)) 606 if (!TREE_PURPOSE (parmlist) 607 && !PACK_EXPANSION_P (TREE_VALUE (parmlist))) 608 return false; 609 return true; 610 } 611 612 /* Allocate N bytes of memory from the conversion obstack. The memory 613 is zeroed before being returned. */ 614 615 static void * 616 conversion_obstack_alloc (size_t n) 617 { 618 void *p; 619 if (!conversion_obstack_initialized) 620 { 621 gcc_obstack_init (&conversion_obstack); 622 conversion_obstack_initialized = true; 623 } 624 p = obstack_alloc (&conversion_obstack, n); 625 memset (p, 0, n); 626 return p; 627 } 628 629 /* RAII class to discard anything added to conversion_obstack. */ 630 631 struct conversion_obstack_sentinel 632 { 633 void *p; 634 conversion_obstack_sentinel (): p (conversion_obstack_alloc (0)) {} 635 ~conversion_obstack_sentinel () { obstack_free (&conversion_obstack, p); } 636 }; 637 638 /* Allocate rejection reasons. */ 639 640 static struct rejection_reason * 641 alloc_rejection (enum rejection_reason_code code) 642 { 643 struct rejection_reason *p; 644 p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p); 645 p->code = code; 646 return p; 647 } 648 649 static struct rejection_reason * 650 arity_rejection (tree first_arg, int expected, int actual, bool least_p = false) 651 { 652 struct rejection_reason *r = alloc_rejection (rr_arity); 653 int adjust = first_arg != NULL_TREE; 654 r->u.arity.expected = expected - adjust; 655 r->u.arity.actual = actual - adjust; 656 r->u.arity.least_p = least_p; 657 return r; 658 } 659 660 static struct rejection_reason * 661 arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to, 662 location_t loc) 663 { 664 struct rejection_reason *r = alloc_rejection (rr_arg_conversion); 665 int adjust = first_arg != NULL_TREE; 666 r->u.conversion.n_arg = n_arg - adjust; 667 r->u.conversion.from = from; 668 r->u.conversion.to_type = to; 669 r->u.conversion.loc = loc; 670 return r; 671 } 672 673 static struct rejection_reason * 674 bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to, 675 location_t loc) 676 { 677 struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion); 678 int adjust = first_arg != NULL_TREE; 679 r->u.bad_conversion.n_arg = n_arg - adjust; 680 r->u.bad_conversion.from = from; 681 r->u.bad_conversion.to_type = to; 682 r->u.bad_conversion.loc = loc; 683 return r; 684 } 685 686 static struct rejection_reason * 687 explicit_conversion_rejection (tree from, tree to) 688 { 689 struct rejection_reason *r = alloc_rejection (rr_explicit_conversion); 690 r->u.conversion.n_arg = 0; 691 r->u.conversion.from = from; 692 r->u.conversion.to_type = to; 693 r->u.conversion.loc = UNKNOWN_LOCATION; 694 return r; 695 } 696 697 static struct rejection_reason * 698 template_conversion_rejection (tree from, tree to) 699 { 700 struct rejection_reason *r = alloc_rejection (rr_template_conversion); 701 r->u.conversion.n_arg = 0; 702 r->u.conversion.from = from; 703 r->u.conversion.to_type = to; 704 r->u.conversion.loc = UNKNOWN_LOCATION; 705 return r; 706 } 707 708 static struct rejection_reason * 709 template_unification_rejection (tree tmpl, tree explicit_targs, tree targs, 710 const tree *args, unsigned int nargs, 711 tree return_type, unification_kind_t strict, 712 int flags) 713 { 714 size_t args_n_bytes = sizeof (*args) * nargs; 715 tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes); 716 struct rejection_reason *r = alloc_rejection (rr_template_unification); 717 r->u.template_unification.tmpl = tmpl; 718 r->u.template_unification.explicit_targs = explicit_targs; 719 r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs); 720 /* Copy args to our own storage. */ 721 memcpy (args1, args, args_n_bytes); 722 r->u.template_unification.args = args1; 723 r->u.template_unification.nargs = nargs; 724 r->u.template_unification.return_type = return_type; 725 r->u.template_unification.strict = strict; 726 r->u.template_unification.flags = flags; 727 return r; 728 } 729 730 static struct rejection_reason * 731 template_unification_error_rejection (void) 732 { 733 return alloc_rejection (rr_template_unification); 734 } 735 736 static struct rejection_reason * 737 invalid_copy_with_fn_template_rejection (void) 738 { 739 struct rejection_reason *r = alloc_rejection (rr_invalid_copy); 740 return r; 741 } 742 743 static struct rejection_reason * 744 inherited_ctor_rejection (void) 745 { 746 struct rejection_reason *r = alloc_rejection (rr_inherited_ctor); 747 return r; 748 } 749 750 /* Build a constraint failure record. */ 751 752 static struct rejection_reason * 753 constraint_failure (void) 754 { 755 struct rejection_reason *r = alloc_rejection (rr_constraint_failure); 756 return r; 757 } 758 759 /* Dynamically allocate a conversion. */ 760 761 static conversion * 762 alloc_conversion (conversion_kind kind) 763 { 764 conversion *c; 765 c = (conversion *) conversion_obstack_alloc (sizeof (conversion)); 766 c->kind = kind; 767 return c; 768 } 769 770 /* Make sure that all memory on the conversion obstack has been 771 freed. */ 772 773 void 774 validate_conversion_obstack (void) 775 { 776 if (conversion_obstack_initialized) 777 gcc_assert ((obstack_next_free (&conversion_obstack) 778 == obstack_base (&conversion_obstack))); 779 } 780 781 /* Dynamically allocate an array of N conversions. */ 782 783 static conversion ** 784 alloc_conversions (size_t n) 785 { 786 return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *)); 787 } 788 789 /* True iff the active member of conversion::u for code CODE is NEXT. */ 790 791 static inline bool 792 has_next (conversion_kind code) 793 { 794 return !(code == ck_identity 795 || code == ck_ambig 796 || code == ck_list 797 || code == ck_aggr 798 || code == ck_deferred_bad); 799 } 800 801 static conversion * 802 build_conv (conversion_kind code, tree type, conversion *from) 803 { 804 conversion *t; 805 conversion_rank rank = CONVERSION_RANK (from); 806 807 /* Only call this function for conversions that use u.next. */ 808 gcc_assert (from == NULL || has_next (code)); 809 810 /* Note that the caller is responsible for filling in t->cand for 811 user-defined conversions. */ 812 t = alloc_conversion (code); 813 t->type = type; 814 t->u.next = from; 815 816 switch (code) 817 { 818 case ck_ptr: 819 case ck_pmem: 820 case ck_base: 821 case ck_std: 822 if (rank < cr_std) 823 rank = cr_std; 824 break; 825 826 case ck_qual: 827 case ck_fnptr: 828 if (rank < cr_exact) 829 rank = cr_exact; 830 break; 831 832 default: 833 break; 834 } 835 t->rank = rank; 836 t->user_conv_p = (code == ck_user || from->user_conv_p); 837 t->bad_p = from->bad_p; 838 t->base_p = false; 839 return t; 840 } 841 842 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a 843 specialization of std::initializer_list<T>, if such a conversion is 844 possible. */ 845 846 static conversion * 847 build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain) 848 { 849 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0); 850 unsigned len = CONSTRUCTOR_NELTS (ctor); 851 conversion **subconvs = alloc_conversions (len); 852 conversion *t; 853 unsigned i; 854 tree val; 855 856 /* Within a list-initialization we can have more user-defined 857 conversions. */ 858 flags &= ~LOOKUP_NO_CONVERSION; 859 /* But no narrowing conversions. */ 860 flags |= LOOKUP_NO_NARROWING; 861 862 /* Can't make an array of these types. */ 863 if (TYPE_REF_P (elttype) 864 || TREE_CODE (elttype) == FUNCTION_TYPE 865 || VOID_TYPE_P (elttype)) 866 return NULL; 867 868 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val) 869 { 870 conversion *sub 871 = implicit_conversion (elttype, TREE_TYPE (val), val, 872 false, flags, complain); 873 if (sub == NULL) 874 return NULL; 875 876 subconvs[i] = sub; 877 } 878 879 t = alloc_conversion (ck_list); 880 t->type = type; 881 t->u.list = subconvs; 882 t->rank = cr_exact; 883 884 for (i = 0; i < len; ++i) 885 { 886 conversion *sub = subconvs[i]; 887 if (sub->rank > t->rank) 888 t->rank = sub->rank; 889 if (sub->user_conv_p) 890 t->user_conv_p = true; 891 if (sub->bad_p) 892 t->bad_p = true; 893 } 894 895 return t; 896 } 897 898 /* Return the next conversion of the conversion chain (if applicable), 899 or NULL otherwise. Please use this function instead of directly 900 accessing fields of struct conversion. */ 901 902 static conversion * 903 next_conversion (conversion *conv) 904 { 905 if (conv == NULL 906 || !has_next (conv->kind)) 907 return NULL; 908 return conv->u.next; 909 } 910 911 /* Strip to the first ck_user, ck_ambig, ck_list, ck_aggr or ck_identity 912 encountered. */ 913 914 static conversion * 915 strip_standard_conversion (conversion *conv) 916 { 917 while (conv 918 && conv->kind != ck_user 919 && has_next (conv->kind)) 920 conv = next_conversion (conv); 921 return conv; 922 } 923 924 /* Subroutine of build_aggr_conv: check whether FROM is a valid aggregate 925 initializer for array type ATYPE. */ 926 927 static bool 928 can_convert_array (tree atype, tree from, int flags, tsubst_flags_t complain) 929 { 930 tree elttype = TREE_TYPE (atype); 931 unsigned i; 932 933 if (TREE_CODE (from) == CONSTRUCTOR) 934 { 935 for (i = 0; i < CONSTRUCTOR_NELTS (from); ++i) 936 { 937 tree val = CONSTRUCTOR_ELT (from, i)->value; 938 bool ok; 939 if (TREE_CODE (elttype) == ARRAY_TYPE) 940 ok = can_convert_array (elttype, val, flags, complain); 941 else 942 ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags, 943 complain); 944 if (!ok) 945 return false; 946 } 947 return true; 948 } 949 950 if (char_type_p (TYPE_MAIN_VARIANT (elttype)) 951 && TREE_CODE (tree_strip_any_location_wrapper (from)) == STRING_CST) 952 return array_string_literal_compatible_p (atype, from); 953 954 /* No other valid way to aggregate initialize an array. */ 955 return false; 956 } 957 958 /* Helper for build_aggr_conv. Return true if FIELD is in PSET, or if 959 FIELD has ANON_AGGR_TYPE_P and any initializable field in there recursively 960 is in PSET. */ 961 962 static bool 963 field_in_pset (hash_set<tree, true> &pset, tree field) 964 { 965 if (pset.contains (field)) 966 return true; 967 if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) 968 for (field = TYPE_FIELDS (TREE_TYPE (field)); 969 field; field = DECL_CHAIN (field)) 970 { 971 field = next_aggregate_field (field); 972 if (field == NULL_TREE) 973 break; 974 if (field_in_pset (pset, field)) 975 return true; 976 } 977 return false; 978 } 979 980 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an 981 aggregate class, if such a conversion is possible. */ 982 983 static conversion * 984 build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain) 985 { 986 unsigned HOST_WIDE_INT i = 0; 987 conversion *c; 988 tree field = next_aggregate_field (TYPE_FIELDS (type)); 989 tree empty_ctor = NULL_TREE; 990 hash_set<tree, true> pset; 991 992 /* We already called reshape_init in implicit_conversion, but it might not 993 have done anything in the case of parenthesized aggr init. */ 994 995 /* The conversions within the init-list aren't affected by the enclosing 996 context; they're always simple copy-initialization. */ 997 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING; 998 999 /* For designated initializers, verify that each initializer is convertible 1000 to corresponding TREE_TYPE (ce->index) and mark those FIELD_DECLs as 1001 visited. In the following loop then ignore already visited 1002 FIELD_DECLs. */ 1003 tree idx, val; 1004 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, val) 1005 { 1006 if (!idx) 1007 break; 1008 1009 gcc_checking_assert (TREE_CODE (idx) == FIELD_DECL); 1010 1011 tree ftype = TREE_TYPE (idx); 1012 bool ok; 1013 1014 if (TREE_CODE (ftype) == ARRAY_TYPE) 1015 ok = can_convert_array (ftype, val, flags, complain); 1016 else 1017 ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags, 1018 complain); 1019 1020 if (!ok) 1021 return NULL; 1022 1023 /* For unions, there should be just one initializer. */ 1024 if (TREE_CODE (type) == UNION_TYPE) 1025 { 1026 field = NULL_TREE; 1027 i = 1; 1028 break; 1029 } 1030 pset.add (idx); 1031 } 1032 1033 for (; field; field = next_aggregate_field (DECL_CHAIN (field))) 1034 { 1035 tree ftype = TREE_TYPE (field); 1036 bool ok; 1037 1038 if (!pset.is_empty () && field_in_pset (pset, field)) 1039 continue; 1040 if (i < CONSTRUCTOR_NELTS (ctor)) 1041 { 1042 constructor_elt *ce = CONSTRUCTOR_ELT (ctor, i); 1043 gcc_checking_assert (!ce->index); 1044 val = ce->value; 1045 ++i; 1046 } 1047 else if (DECL_INITIAL (field)) 1048 val = get_nsdmi (field, /*ctor*/false, complain); 1049 else if (TYPE_REF_P (ftype)) 1050 /* Value-initialization of reference is ill-formed. */ 1051 return NULL; 1052 else 1053 { 1054 if (empty_ctor == NULL_TREE) 1055 empty_ctor = build_constructor (init_list_type_node, NULL); 1056 val = empty_ctor; 1057 } 1058 1059 if (TREE_CODE (ftype) == ARRAY_TYPE) 1060 ok = can_convert_array (ftype, val, flags, complain); 1061 else 1062 ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags, 1063 complain); 1064 1065 if (!ok) 1066 return NULL; 1067 1068 if (TREE_CODE (type) == UNION_TYPE) 1069 break; 1070 } 1071 1072 if (i < CONSTRUCTOR_NELTS (ctor)) 1073 return NULL; 1074 1075 c = alloc_conversion (ck_aggr); 1076 c->type = type; 1077 c->rank = cr_exact; 1078 c->user_conv_p = true; 1079 c->check_narrowing = true; 1080 c->u.expr = ctor; 1081 return c; 1082 } 1083 1084 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an 1085 array type, if such a conversion is possible. */ 1086 1087 static conversion * 1088 build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain) 1089 { 1090 conversion *c; 1091 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor); 1092 tree elttype = TREE_TYPE (type); 1093 bool bad = false; 1094 bool user = false; 1095 enum conversion_rank rank = cr_exact; 1096 1097 /* We might need to propagate the size from the element to the array. */ 1098 complete_type (type); 1099 1100 if (TYPE_DOMAIN (type) 1101 && !variably_modified_type_p (TYPE_DOMAIN (type), NULL_TREE)) 1102 { 1103 unsigned HOST_WIDE_INT alen = tree_to_uhwi (array_type_nelts_top (type)); 1104 if (alen < len) 1105 return NULL; 1106 } 1107 1108 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING; 1109 1110 for (auto &e: CONSTRUCTOR_ELTS (ctor)) 1111 { 1112 conversion *sub 1113 = implicit_conversion (elttype, TREE_TYPE (e.value), e.value, 1114 false, flags, complain); 1115 if (sub == NULL) 1116 return NULL; 1117 1118 if (sub->rank > rank) 1119 rank = sub->rank; 1120 if (sub->user_conv_p) 1121 user = true; 1122 if (sub->bad_p) 1123 bad = true; 1124 } 1125 1126 c = alloc_conversion (ck_aggr); 1127 c->type = type; 1128 c->rank = rank; 1129 c->user_conv_p = user; 1130 c->bad_p = bad; 1131 c->u.expr = ctor; 1132 return c; 1133 } 1134 1135 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a 1136 complex type, if such a conversion is possible. */ 1137 1138 static conversion * 1139 build_complex_conv (tree type, tree ctor, int flags, 1140 tsubst_flags_t complain) 1141 { 1142 conversion *c; 1143 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor); 1144 tree elttype = TREE_TYPE (type); 1145 bool bad = false; 1146 bool user = false; 1147 enum conversion_rank rank = cr_exact; 1148 1149 if (len != 2) 1150 return NULL; 1151 1152 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING; 1153 1154 for (auto &e: CONSTRUCTOR_ELTS (ctor)) 1155 { 1156 conversion *sub 1157 = implicit_conversion (elttype, TREE_TYPE (e.value), e.value, 1158 false, flags, complain); 1159 if (sub == NULL) 1160 return NULL; 1161 1162 if (sub->rank > rank) 1163 rank = sub->rank; 1164 if (sub->user_conv_p) 1165 user = true; 1166 if (sub->bad_p) 1167 bad = true; 1168 } 1169 1170 c = alloc_conversion (ck_aggr); 1171 c->type = type; 1172 c->rank = rank; 1173 c->user_conv_p = user; 1174 c->bad_p = bad; 1175 c->u.expr = ctor; 1176 return c; 1177 } 1178 1179 /* Build a representation of the identity conversion from EXPR to 1180 itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */ 1181 1182 static conversion * 1183 build_identity_conv (tree type, tree expr) 1184 { 1185 conversion *c; 1186 1187 c = alloc_conversion (ck_identity); 1188 c->type = type; 1189 c->u.expr = expr; 1190 1191 return c; 1192 } 1193 1194 /* Converting from EXPR to TYPE was ambiguous in the sense that there 1195 were multiple user-defined conversions to accomplish the job. 1196 Build a conversion that indicates that ambiguity. */ 1197 1198 static conversion * 1199 build_ambiguous_conv (tree type, tree expr) 1200 { 1201 conversion *c; 1202 1203 c = alloc_conversion (ck_ambig); 1204 c->type = type; 1205 c->u.expr = expr; 1206 1207 return c; 1208 } 1209 1210 tree 1211 strip_top_quals (tree t) 1212 { 1213 if (TREE_CODE (t) == ARRAY_TYPE) 1214 return t; 1215 return cp_build_qualified_type (t, 0); 1216 } 1217 1218 /* Returns the standard conversion path (see [conv]) from type FROM to type 1219 TO, if any. For proper handling of null pointer constants, you must 1220 also pass the expression EXPR to convert from. If C_CAST_P is true, 1221 this conversion is coming from a C-style cast. */ 1222 1223 static conversion * 1224 standard_conversion (tree to, tree from, tree expr, bool c_cast_p, 1225 int flags, tsubst_flags_t complain) 1226 { 1227 enum tree_code fcode, tcode; 1228 conversion *conv; 1229 bool fromref = false; 1230 tree qualified_to; 1231 1232 to = non_reference (to); 1233 if (TYPE_REF_P (from)) 1234 { 1235 fromref = true; 1236 from = TREE_TYPE (from); 1237 } 1238 qualified_to = to; 1239 to = strip_top_quals (to); 1240 from = strip_top_quals (from); 1241 1242 if (expr && type_unknown_p (expr)) 1243 { 1244 if (TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to)) 1245 { 1246 tsubst_flags_t tflags = tf_conv; 1247 expr = instantiate_type (to, expr, tflags); 1248 if (expr == error_mark_node) 1249 return NULL; 1250 from = TREE_TYPE (expr); 1251 } 1252 else if (TREE_CODE (to) == BOOLEAN_TYPE) 1253 { 1254 /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */ 1255 expr = resolve_nondeduced_context (expr, complain); 1256 from = TREE_TYPE (expr); 1257 } 1258 } 1259 1260 fcode = TREE_CODE (from); 1261 tcode = TREE_CODE (to); 1262 1263 conv = build_identity_conv (from, expr); 1264 if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE) 1265 { 1266 from = type_decays_to (from); 1267 fcode = TREE_CODE (from); 1268 /* Tell convert_like that we're using the address. */ 1269 conv->rvaluedness_matches_p = true; 1270 conv = build_conv (ck_lvalue, from, conv); 1271 } 1272 /* Wrapping a ck_rvalue around a class prvalue (as a result of using 1273 obvalue_p) seems odd, since it's already a prvalue, but that's how we 1274 express the copy constructor call required by copy-initialization. */ 1275 else if (fromref || (expr && obvalue_p (expr))) 1276 { 1277 if (expr) 1278 { 1279 tree bitfield_type; 1280 bitfield_type = is_bitfield_expr_with_lowered_type (expr); 1281 if (bitfield_type) 1282 { 1283 from = strip_top_quals (bitfield_type); 1284 fcode = TREE_CODE (from); 1285 } 1286 } 1287 conv = build_conv (ck_rvalue, from, conv); 1288 /* If we're performing copy-initialization, remember to skip 1289 explicit constructors. */ 1290 if (flags & LOOKUP_ONLYCONVERTING) 1291 conv->copy_init_p = true; 1292 } 1293 1294 /* Allow conversion between `__complex__' data types. */ 1295 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE) 1296 { 1297 /* The standard conversion sequence to convert FROM to TO is 1298 the standard conversion sequence to perform componentwise 1299 conversion. */ 1300 conversion *part_conv = standard_conversion 1301 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags, 1302 complain); 1303 1304 if (!part_conv) 1305 conv = NULL; 1306 else if (part_conv->kind == ck_identity) 1307 /* Leave conv alone. */; 1308 else 1309 { 1310 conv = build_conv (part_conv->kind, to, conv); 1311 conv->rank = part_conv->rank; 1312 } 1313 1314 return conv; 1315 } 1316 1317 if (same_type_p (from, to)) 1318 { 1319 if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue) 1320 conv->type = qualified_to; 1321 return conv; 1322 } 1323 1324 /* [conv.ptr] 1325 A null pointer constant can be converted to a pointer type; ... A 1326 null pointer constant of integral type can be converted to an 1327 rvalue of type std::nullptr_t. */ 1328 if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to) 1329 || NULLPTR_TYPE_P (to)) 1330 && ((expr && null_ptr_cst_p (expr)) 1331 || NULLPTR_TYPE_P (from))) 1332 conv = build_conv (ck_std, to, conv); 1333 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE) 1334 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE)) 1335 { 1336 /* For backwards brain damage compatibility, allow interconversion of 1337 pointers and integers with a pedwarn. */ 1338 conv = build_conv (ck_std, to, conv); 1339 conv->bad_p = true; 1340 } 1341 else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE) 1342 { 1343 /* For backwards brain damage compatibility, allow interconversion of 1344 enums and integers with a pedwarn. */ 1345 conv = build_conv (ck_std, to, conv); 1346 conv->bad_p = true; 1347 } 1348 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE) 1349 || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from))) 1350 { 1351 tree to_pointee; 1352 tree from_pointee; 1353 1354 if (tcode == POINTER_TYPE) 1355 { 1356 to_pointee = TREE_TYPE (to); 1357 from_pointee = TREE_TYPE (from); 1358 1359 /* Since this is the target of a pointer, it can't have function 1360 qualifiers, so any TYPE_QUALS must be for attributes const or 1361 noreturn. Strip them. */ 1362 if (TREE_CODE (to_pointee) == FUNCTION_TYPE 1363 && TYPE_QUALS (to_pointee)) 1364 to_pointee = build_qualified_type (to_pointee, TYPE_UNQUALIFIED); 1365 if (TREE_CODE (from_pointee) == FUNCTION_TYPE 1366 && TYPE_QUALS (from_pointee)) 1367 from_pointee = build_qualified_type (from_pointee, TYPE_UNQUALIFIED); 1368 } 1369 else 1370 { 1371 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to); 1372 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from); 1373 } 1374 1375 if (tcode == POINTER_TYPE 1376 && same_type_ignoring_top_level_qualifiers_p (from_pointee, 1377 to_pointee)) 1378 ; 1379 else if (VOID_TYPE_P (to_pointee) 1380 && !TYPE_PTRDATAMEM_P (from) 1381 && TREE_CODE (from_pointee) != FUNCTION_TYPE) 1382 { 1383 tree nfrom = TREE_TYPE (from); 1384 /* Don't try to apply restrict to void. */ 1385 int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT; 1386 from_pointee = cp_build_qualified_type (void_type_node, quals); 1387 from = build_pointer_type (from_pointee); 1388 conv = build_conv (ck_ptr, from, conv); 1389 } 1390 else if (TYPE_PTRDATAMEM_P (from)) 1391 { 1392 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from); 1393 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to); 1394 1395 if (same_type_p (fbase, tbase)) 1396 /* No base conversion needed. */; 1397 else if (DERIVED_FROM_P (fbase, tbase) 1398 && (same_type_ignoring_top_level_qualifiers_p 1399 (from_pointee, to_pointee))) 1400 { 1401 from = build_ptrmem_type (tbase, from_pointee); 1402 conv = build_conv (ck_pmem, from, conv); 1403 } 1404 else 1405 return NULL; 1406 } 1407 else if (CLASS_TYPE_P (from_pointee) 1408 && CLASS_TYPE_P (to_pointee) 1409 /* [conv.ptr] 1410 1411 An rvalue of type "pointer to cv D," where D is a 1412 class type, can be converted to an rvalue of type 1413 "pointer to cv B," where B is a base class (clause 1414 _class.derived_) of D. If B is an inaccessible 1415 (clause _class.access_) or ambiguous 1416 (_class.member.lookup_) base class of D, a program 1417 that necessitates this conversion is ill-formed. 1418 Therefore, we use DERIVED_FROM_P, and do not check 1419 access or uniqueness. */ 1420 && DERIVED_FROM_P (to_pointee, from_pointee)) 1421 { 1422 from_pointee 1423 = cp_build_qualified_type (to_pointee, 1424 cp_type_quals (from_pointee)); 1425 from = build_pointer_type (from_pointee); 1426 conv = build_conv (ck_ptr, from, conv); 1427 conv->base_p = true; 1428 } 1429 1430 if (same_type_p (from, to)) 1431 /* OK */; 1432 else if (c_cast_p && comp_ptr_ttypes_const (to, from, bounds_either)) 1433 /* In a C-style cast, we ignore CV-qualification because we 1434 are allowed to perform a static_cast followed by a 1435 const_cast. */ 1436 conv = build_conv (ck_qual, to, conv); 1437 else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee)) 1438 conv = build_conv (ck_qual, to, conv); 1439 else if (expr && string_conv_p (to, expr, 0)) 1440 /* converting from string constant to char *. */ 1441 conv = build_conv (ck_qual, to, conv); 1442 else if (fnptr_conv_p (to, from)) 1443 conv = build_conv (ck_fnptr, to, conv); 1444 /* Allow conversions among compatible ObjC pointer types (base 1445 conversions have been already handled above). */ 1446 else if (c_dialect_objc () 1447 && objc_compare_types (to, from, -4, NULL_TREE)) 1448 conv = build_conv (ck_ptr, to, conv); 1449 else if (ptr_reasonably_similar (to_pointee, from_pointee)) 1450 { 1451 conv = build_conv (ck_ptr, to, conv); 1452 conv->bad_p = true; 1453 } 1454 else 1455 return NULL; 1456 1457 from = to; 1458 } 1459 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from)) 1460 { 1461 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from)); 1462 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to)); 1463 tree fbase = class_of_this_parm (fromfn); 1464 tree tbase = class_of_this_parm (tofn); 1465 1466 /* If FBASE and TBASE are equivalent but incomplete, DERIVED_FROM_P 1467 yields false. But a pointer to member of incomplete class is OK. */ 1468 if (!same_type_p (fbase, tbase) && !DERIVED_FROM_P (fbase, tbase)) 1469 return NULL; 1470 1471 tree fstat = static_fn_type (fromfn); 1472 tree tstat = static_fn_type (tofn); 1473 if (same_type_p (tstat, fstat) 1474 || fnptr_conv_p (tstat, fstat)) 1475 /* OK */; 1476 else 1477 return NULL; 1478 1479 if (!same_type_p (fbase, tbase)) 1480 { 1481 from = build_memfn_type (fstat, 1482 tbase, 1483 cp_type_quals (tbase), 1484 type_memfn_rqual (tofn)); 1485 from = build_ptrmemfunc_type (build_pointer_type (from)); 1486 conv = build_conv (ck_pmem, from, conv); 1487 conv->base_p = true; 1488 } 1489 if (fnptr_conv_p (tstat, fstat)) 1490 conv = build_conv (ck_fnptr, to, conv); 1491 } 1492 else if (tcode == BOOLEAN_TYPE) 1493 { 1494 /* [conv.bool] 1495 1496 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer 1497 to member type can be converted to a prvalue of type bool. ... 1498 For direct-initialization (8.5 [dcl.init]), a prvalue of type 1499 std::nullptr_t can be converted to a prvalue of type bool; */ 1500 if (ARITHMETIC_TYPE_P (from) 1501 || UNSCOPED_ENUM_P (from) 1502 || fcode == POINTER_TYPE 1503 || TYPE_PTRMEM_P (from) 1504 || NULLPTR_TYPE_P (from)) 1505 { 1506 conv = build_conv (ck_std, to, conv); 1507 if (fcode == POINTER_TYPE 1508 || TYPE_PTRDATAMEM_P (from) 1509 || (TYPE_PTRMEMFUNC_P (from) 1510 && conv->rank < cr_pbool) 1511 || NULLPTR_TYPE_P (from)) 1512 conv->rank = cr_pbool; 1513 if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING)) 1514 conv->bad_p = true; 1515 if (flags & LOOKUP_NO_NARROWING) 1516 conv->check_narrowing = true; 1517 return conv; 1518 } 1519 1520 return NULL; 1521 } 1522 /* We don't check for ENUMERAL_TYPE here because there are no standard 1523 conversions to enum type. */ 1524 /* As an extension, allow conversion to complex type. */ 1525 else if (ARITHMETIC_TYPE_P (to)) 1526 { 1527 if (! (INTEGRAL_CODE_P (fcode) 1528 || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL))) 1529 || SCOPED_ENUM_P (from)) 1530 return NULL; 1531 1532 /* If we're parsing an enum with no fixed underlying type, we're 1533 dealing with an incomplete type, which renders the conversion 1534 ill-formed. */ 1535 if (!COMPLETE_TYPE_P (from)) 1536 return NULL; 1537 1538 conv = build_conv (ck_std, to, conv); 1539 1540 tree underlying_type = NULL_TREE; 1541 if (TREE_CODE (from) == ENUMERAL_TYPE 1542 && ENUM_FIXED_UNDERLYING_TYPE_P (from)) 1543 underlying_type = ENUM_UNDERLYING_TYPE (from); 1544 1545 /* Give this a better rank if it's a promotion. 1546 1547 To handle CWG 1601, also bump the rank if we are converting 1548 an enumeration with a fixed underlying type to the underlying 1549 type. */ 1550 if ((same_type_p (to, type_promotes_to (from)) 1551 || (underlying_type && same_type_p (to, underlying_type))) 1552 && next_conversion (conv)->rank <= cr_promotion) 1553 conv->rank = cr_promotion; 1554 1555 /* A prvalue of floating-point type can be converted to a prvalue of 1556 another floating-point type with a greater or equal conversion 1557 rank ([conv.rank]). A prvalue of standard floating-point type can 1558 be converted to a prvalue of another standard floating-point type. 1559 For backwards compatibility with handling __float128 and other 1560 non-standard floating point types, allow all implicit floating 1561 point conversions if neither type is extended floating-point 1562 type and if at least one of them is, fail if they have unordered 1563 conversion rank or from has higher conversion rank. */ 1564 if (fcode == REAL_TYPE 1565 && tcode == REAL_TYPE 1566 && (extended_float_type_p (from) 1567 || extended_float_type_p (to)) 1568 && cp_compare_floating_point_conversion_ranks (from, to) >= 2) 1569 conv->bad_p = true; 1570 } 1571 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE 1572 && vector_types_convertible_p (from, to, false)) 1573 return build_conv (ck_std, to, conv); 1574 else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from) 1575 && is_properly_derived_from (from, to)) 1576 { 1577 if (conv->kind == ck_rvalue) 1578 conv = next_conversion (conv); 1579 conv = build_conv (ck_base, to, conv); 1580 /* The derived-to-base conversion indicates the initialization 1581 of a parameter with base type from an object of a derived 1582 type. A temporary object is created to hold the result of 1583 the conversion unless we're binding directly to a reference. */ 1584 conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND); 1585 /* If we're performing copy-initialization, remember to skip 1586 explicit constructors. */ 1587 if (flags & LOOKUP_ONLYCONVERTING) 1588 conv->copy_init_p = true; 1589 } 1590 else 1591 return NULL; 1592 1593 if (flags & LOOKUP_NO_NARROWING) 1594 conv->check_narrowing = true; 1595 1596 return conv; 1597 } 1598 1599 /* Returns nonzero if T1 is reference-related to T2. 1600 1601 This is considered when a reference to T1 is initialized by a T2. */ 1602 1603 bool 1604 reference_related_p (tree t1, tree t2) 1605 { 1606 if (t1 == error_mark_node || t2 == error_mark_node) 1607 return false; 1608 1609 t1 = TYPE_MAIN_VARIANT (t1); 1610 t2 = TYPE_MAIN_VARIANT (t2); 1611 1612 /* [dcl.init.ref] 1613 1614 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related 1615 to "cv2 T2" if T1 is similar to T2, or T1 is a base class of T2. */ 1616 return (similar_type_p (t1, t2) 1617 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2) 1618 && DERIVED_FROM_P (t1, t2))); 1619 } 1620 1621 /* Returns nonzero if T1 is reference-compatible with T2. */ 1622 1623 bool 1624 reference_compatible_p (tree t1, tree t2) 1625 { 1626 /* [dcl.init.ref] 1627 1628 "cv1 T1" is reference compatible with "cv2 T2" if 1629 a prvalue of type "pointer to cv2 T2" can be converted to the type 1630 "pointer to cv1 T1" via a standard conversion sequence. */ 1631 tree ptype1 = build_pointer_type (t1); 1632 tree ptype2 = build_pointer_type (t2); 1633 conversion *conv = standard_conversion (ptype1, ptype2, NULL_TREE, 1634 /*c_cast_p=*/false, 0, tf_none); 1635 if (!conv || conv->bad_p) 1636 return false; 1637 return true; 1638 } 1639 1640 /* Return true if converting FROM to TO would involve a qualification 1641 conversion. */ 1642 1643 static bool 1644 involves_qualification_conversion_p (tree to, tree from) 1645 { 1646 /* If we're not convering a pointer to another one, we won't get 1647 a qualification conversion. */ 1648 if (!((TYPE_PTR_P (to) && TYPE_PTR_P (from)) 1649 || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))) 1650 return false; 1651 1652 conversion *conv = standard_conversion (to, from, NULL_TREE, 1653 /*c_cast_p=*/false, 0, tf_none); 1654 for (conversion *t = conv; t; t = next_conversion (t)) 1655 if (t->kind == ck_qual) 1656 return true; 1657 1658 return false; 1659 } 1660 1661 /* A reference of the indicated TYPE is being bound directly to the 1662 expression represented by the implicit conversion sequence CONV. 1663 Return a conversion sequence for this binding. */ 1664 1665 static conversion * 1666 direct_reference_binding (tree type, conversion *conv) 1667 { 1668 tree t; 1669 1670 gcc_assert (TYPE_REF_P (type)); 1671 gcc_assert (!TYPE_REF_P (conv->type)); 1672 1673 t = TREE_TYPE (type); 1674 1675 if (conv->kind == ck_identity) 1676 /* Mark the identity conv as to not decay to rvalue. */ 1677 conv->rvaluedness_matches_p = true; 1678 1679 /* [over.ics.rank] 1680 1681 When a parameter of reference type binds directly 1682 (_dcl.init.ref_) to an argument expression, the implicit 1683 conversion sequence is the identity conversion, unless the 1684 argument expression has a type that is a derived class of the 1685 parameter type, in which case the implicit conversion sequence is 1686 a derived-to-base Conversion. 1687 1688 If the parameter binds directly to the result of applying a 1689 conversion function to the argument expression, the implicit 1690 conversion sequence is a user-defined conversion sequence 1691 (_over.ics.user_), with the second standard conversion sequence 1692 either an identity conversion or, if the conversion function 1693 returns an entity of a type that is a derived class of the 1694 parameter type, a derived-to-base conversion. */ 1695 if (is_properly_derived_from (conv->type, t)) 1696 { 1697 /* Represent the derived-to-base conversion. */ 1698 conv = build_conv (ck_base, t, conv); 1699 /* We will actually be binding to the base-class subobject in 1700 the derived class, so we mark this conversion appropriately. 1701 That way, convert_like knows not to generate a temporary. */ 1702 conv->need_temporary_p = false; 1703 } 1704 else if (involves_qualification_conversion_p (t, conv->type)) 1705 /* Represent the qualification conversion. After DR 2352 1706 #1 and #2 were indistinguishable conversion sequences: 1707 1708 void f(int*); // #1 1709 void f(const int* const &); // #2 1710 void g(int* p) { f(p); } 1711 1712 because the types "int *" and "const int *const" are 1713 reference-related and we were binding both directly and they 1714 had the same rank. To break it up, we add a ck_qual under the 1715 ck_ref_bind so that conversion sequence ranking chooses #1. 1716 1717 We strip_top_quals here which is also what standard_conversion 1718 does. Failure to do so would confuse comp_cv_qual_signature 1719 into thinking that in 1720 1721 void f(const int * const &); // #1 1722 void f(const int *); // #2 1723 int *x; 1724 f(x); 1725 1726 #2 is a better match than #1 even though they're ambiguous (97296). */ 1727 conv = build_conv (ck_qual, strip_top_quals (t), conv); 1728 1729 return build_conv (ck_ref_bind, type, conv); 1730 } 1731 1732 /* Returns the conversion path from type FROM to reference type TO for 1733 purposes of reference binding. For lvalue binding, either pass a 1734 reference type to FROM or an lvalue expression to EXPR. If the 1735 reference will be bound to a temporary, NEED_TEMPORARY_P is set for 1736 the conversion returned. If C_CAST_P is true, this 1737 conversion is coming from a C-style cast. */ 1738 1739 static conversion * 1740 reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags, 1741 tsubst_flags_t complain) 1742 { 1743 conversion *conv = NULL; 1744 conversion *bad_direct_conv = nullptr; 1745 tree to = TREE_TYPE (rto); 1746 tree from = rfrom; 1747 tree tfrom; 1748 bool related_p; 1749 bool compatible_p; 1750 cp_lvalue_kind gl_kind; 1751 bool is_lvalue; 1752 1753 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr)) 1754 { 1755 expr = instantiate_type (to, expr, tf_none); 1756 if (expr == error_mark_node) 1757 return NULL; 1758 from = TREE_TYPE (expr); 1759 } 1760 1761 bool copy_list_init = false; 1762 bool single_list_conv = false; 1763 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr)) 1764 { 1765 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 1766 /* DR 1288: Otherwise, if the initializer list has a single element 1767 of type E and ... [T's] referenced type is reference-related to E, 1768 the object or reference is initialized from that element... 1769 1770 ??? With P0388R4, we should bind 't' directly to U{}: 1771 using U = A[2]; 1772 A (&&t)[] = {U{}}; 1773 because A[] and A[2] are reference-related. But we don't do it 1774 because grok_reference_init has deduced the array size (to 1), and 1775 A[1] and A[2] aren't reference-related. */ 1776 if (CONSTRUCTOR_NELTS (expr) == 1 1777 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr)) 1778 { 1779 tree elt = CONSTRUCTOR_ELT (expr, 0)->value; 1780 if (error_operand_p (elt)) 1781 return NULL; 1782 tree etype = TREE_TYPE (elt); 1783 if (reference_related_p (to, etype)) 1784 { 1785 expr = elt; 1786 from = etype; 1787 goto skip; 1788 } 1789 else if (CLASS_TYPE_P (etype) && TYPE_HAS_CONVERSION (etype)) 1790 /* CWG1996: jason's proposed drafting adds "or initializing T from E 1791 would bind directly". We check that in the direct binding with 1792 conversion code below. */ 1793 single_list_conv = true; 1794 } 1795 /* Otherwise, if T is a reference type, a prvalue temporary of the type 1796 referenced by T is copy-list-initialized, and the reference is bound 1797 to that temporary. */ 1798 copy_list_init = true; 1799 skip:; 1800 } 1801 1802 if (TYPE_REF_P (from)) 1803 { 1804 from = TREE_TYPE (from); 1805 if (!TYPE_REF_IS_RVALUE (rfrom) 1806 || TREE_CODE (from) == FUNCTION_TYPE) 1807 gl_kind = clk_ordinary; 1808 else 1809 gl_kind = clk_rvalueref; 1810 } 1811 else if (expr) 1812 gl_kind = lvalue_kind (expr); 1813 else if (CLASS_TYPE_P (from) 1814 || TREE_CODE (from) == ARRAY_TYPE) 1815 gl_kind = clk_class; 1816 else 1817 gl_kind = clk_none; 1818 1819 /* Don't allow a class prvalue when LOOKUP_NO_TEMP_BIND. */ 1820 if ((flags & LOOKUP_NO_TEMP_BIND) 1821 && (gl_kind & clk_class)) 1822 gl_kind = clk_none; 1823 1824 /* Same mask as real_lvalue_p. */ 1825 is_lvalue = gl_kind && !(gl_kind & (clk_rvalueref|clk_class)); 1826 1827 tfrom = from; 1828 if ((gl_kind & clk_bitfield) != 0) 1829 tfrom = unlowered_expr_type (expr); 1830 1831 /* Figure out whether or not the types are reference-related and 1832 reference compatible. We have to do this after stripping 1833 references from FROM. */ 1834 related_p = reference_related_p (to, tfrom); 1835 /* If this is a C cast, first convert to an appropriately qualified 1836 type, so that we can later do a const_cast to the desired type. */ 1837 if (related_p && c_cast_p 1838 && !at_least_as_qualified_p (to, tfrom)) 1839 to = cp_build_qualified_type (to, cp_type_quals (tfrom)); 1840 compatible_p = reference_compatible_p (to, tfrom); 1841 1842 /* Directly bind reference when target expression's type is compatible with 1843 the reference and expression is an lvalue. In DR391, the wording in 1844 [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for 1845 const and rvalue references to rvalues of compatible class type. 1846 We should also do direct bindings for non-class xvalues. */ 1847 if ((related_p || compatible_p) && gl_kind) 1848 { 1849 /* [dcl.init.ref] 1850 1851 If the initializer expression 1852 1853 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1" 1854 is reference-compatible with "cv2 T2," 1855 1856 the reference is bound directly to the initializer expression 1857 lvalue. 1858 1859 [...] 1860 If the initializer expression is an rvalue, with T2 a class type, 1861 and "cv1 T1" is reference-compatible with "cv2 T2", the reference 1862 is bound to the object represented by the rvalue or to a sub-object 1863 within that object. */ 1864 1865 conv = build_identity_conv (tfrom, expr); 1866 conv = direct_reference_binding (rto, conv); 1867 1868 if (TYPE_REF_P (rfrom)) 1869 /* Handle rvalue reference to function properly. */ 1870 conv->rvaluedness_matches_p 1871 = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom)); 1872 else 1873 conv->rvaluedness_matches_p 1874 = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue); 1875 1876 if ((gl_kind & clk_bitfield) != 0 1877 || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to))) 1878 /* For the purposes of overload resolution, we ignore the fact 1879 this expression is a bitfield or packed field. (In particular, 1880 [over.ics.ref] says specifically that a function with a 1881 non-const reference parameter is viable even if the 1882 argument is a bitfield.) 1883 1884 However, when we actually call the function we must create 1885 a temporary to which to bind the reference. If the 1886 reference is volatile, or isn't const, then we cannot make 1887 a temporary, so we just issue an error when the conversion 1888 actually occurs. */ 1889 conv->need_temporary_p = true; 1890 1891 /* Don't allow binding of lvalues (other than function lvalues) to 1892 rvalue references. */ 1893 if (is_lvalue && TYPE_REF_IS_RVALUE (rto) 1894 && TREE_CODE (to) != FUNCTION_TYPE) 1895 conv->bad_p = true; 1896 1897 /* Nor the reverse. */ 1898 if (!is_lvalue && !TYPE_REF_IS_RVALUE (rto) 1899 /* Unless it's really a C++20 lvalue being treated as an xvalue. 1900 But in C++23, such an expression is just an xvalue, not a special 1901 lvalue, so the binding is once again ill-formed. */ 1902 && !(cxx_dialect <= cxx20 1903 && (gl_kind & clk_implicit_rval)) 1904 && (!CP_TYPE_CONST_NON_VOLATILE_P (to) 1905 || (flags & LOOKUP_NO_RVAL_BIND)) 1906 && TREE_CODE (to) != FUNCTION_TYPE) 1907 conv->bad_p = true; 1908 1909 if (!compatible_p) 1910 conv->bad_p = true; 1911 1912 return conv; 1913 } 1914 /* [class.conv.fct] A conversion function is never used to convert a 1915 (possibly cv-qualified) object to the (possibly cv-qualified) same 1916 object type (or a reference to it), to a (possibly cv-qualified) base 1917 class of that type (or a reference to it).... */ 1918 else if (!related_p 1919 && !(flags & LOOKUP_NO_CONVERSION) 1920 && (CLASS_TYPE_P (from) || single_list_conv)) 1921 { 1922 tree rexpr = expr; 1923 if (single_list_conv) 1924 rexpr = CONSTRUCTOR_ELT (expr, 0)->value; 1925 1926 /* [dcl.init.ref] 1927 1928 If the initializer expression 1929 1930 -- has a class type (i.e., T2 is a class type) can be 1931 implicitly converted to an lvalue of type "cv3 T3," where 1932 "cv1 T1" is reference-compatible with "cv3 T3". (this 1933 conversion is selected by enumerating the applicable 1934 conversion functions (_over.match.ref_) and choosing the 1935 best one through overload resolution. (_over.match_). 1936 1937 the reference is bound to the lvalue result of the conversion 1938 in the second case. */ 1939 z_candidate *cand = build_user_type_conversion_1 (rto, rexpr, flags, 1940 complain); 1941 if (cand) 1942 { 1943 if (!cand->second_conv->bad_p) 1944 return cand->second_conv; 1945 1946 /* Direct reference binding wasn't successful and yielded a bad 1947 conversion. Proceed with trying to go through a temporary 1948 instead, and if that also fails then we'll return this bad 1949 conversion rather than no conversion for sake of better 1950 diagnostics. */ 1951 bad_direct_conv = cand->second_conv; 1952 } 1953 } 1954 1955 /* From this point on, we conceptually need temporaries, even if we 1956 elide them. Only the cases above are "direct bindings". */ 1957 if (flags & LOOKUP_NO_TEMP_BIND) 1958 return bad_direct_conv ? bad_direct_conv : nullptr; 1959 1960 /* [over.ics.rank] 1961 1962 When a parameter of reference type is not bound directly to an 1963 argument expression, the conversion sequence is the one required 1964 to convert the argument expression to the underlying type of the 1965 reference according to _over.best.ics_. Conceptually, this 1966 conversion sequence corresponds to copy-initializing a temporary 1967 of the underlying type with the argument expression. Any 1968 difference in top-level cv-qualification is subsumed by the 1969 initialization itself and does not constitute a conversion. */ 1970 1971 bool maybe_valid_p = true; 1972 1973 /* [dcl.init.ref] 1974 1975 Otherwise, the reference shall be an lvalue reference to a 1976 non-volatile const type, or the reference shall be an rvalue 1977 reference. */ 1978 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto)) 1979 maybe_valid_p = false; 1980 1981 /* [dcl.init.ref] 1982 1983 Otherwise, a temporary of type "cv1 T1" is created and 1984 initialized from the initializer expression using the rules for a 1985 non-reference copy initialization. If T1 is reference-related to 1986 T2, cv1 must be the same cv-qualification as, or greater 1987 cv-qualification than, cv2; otherwise, the program is ill-formed. */ 1988 if (related_p && !at_least_as_qualified_p (to, from)) 1989 maybe_valid_p = false; 1990 1991 /* We try below to treat an invalid reference binding as a bad conversion 1992 to improve diagnostics, but doing so may cause otherwise unnecessary 1993 instantiations that can lead to a hard error. So during the first pass 1994 of overload resolution wherein we shortcut bad conversions, instead just 1995 produce a special conversion indicating a second pass is necessary if 1996 there's no strictly viable candidate. */ 1997 if (!maybe_valid_p && (flags & LOOKUP_SHORTCUT_BAD_CONVS)) 1998 { 1999 if (bad_direct_conv) 2000 return bad_direct_conv; 2001 2002 conv = alloc_conversion (ck_deferred_bad); 2003 conv->bad_p = true; 2004 return conv; 2005 } 2006 2007 /* We're generating a temporary now, but don't bind any more in the 2008 conversion (specifically, don't slice the temporary returned by a 2009 conversion operator). */ 2010 flags |= LOOKUP_NO_TEMP_BIND; 2011 2012 /* Core issue 899: When [copy-]initializing a temporary to be bound 2013 to the first parameter of a copy constructor (12.8) called with 2014 a single argument in the context of direct-initialization, 2015 explicit conversion functions are also considered. 2016 2017 So don't set LOOKUP_ONLYCONVERTING in that case. */ 2018 if (!(flags & LOOKUP_COPY_PARM)) 2019 flags |= LOOKUP_ONLYCONVERTING; 2020 2021 if (!conv) 2022 conv = implicit_conversion (to, from, expr, c_cast_p, 2023 flags, complain); 2024 if (!conv) 2025 return bad_direct_conv ? bad_direct_conv : nullptr; 2026 2027 if (conv->user_conv_p) 2028 { 2029 if (copy_list_init) 2030 /* Remember this was copy-list-initialization. */ 2031 conv->need_temporary_p = true; 2032 2033 /* If initializing the temporary used a conversion function, 2034 recalculate the second conversion sequence. */ 2035 for (conversion *t = conv; t; t = next_conversion (t)) 2036 if (t->kind == ck_user 2037 && c_cast_p && !maybe_valid_p) 2038 { 2039 if (complain & tf_warning) 2040 warning (OPT_Wcast_user_defined, 2041 "casting %qT to %qT does not use %qD", 2042 from, rto, t->cand->fn); 2043 /* Don't let recalculation try to make this valid. */ 2044 break; 2045 } 2046 else if (t->kind == ck_user 2047 && DECL_CONV_FN_P (t->cand->fn)) 2048 { 2049 tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn)); 2050 /* A prvalue of non-class type is cv-unqualified. */ 2051 if (!TYPE_REF_P (ftype) && !CLASS_TYPE_P (ftype)) 2052 ftype = cv_unqualified (ftype); 2053 int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND; 2054 conversion *new_second 2055 = reference_binding (rto, ftype, NULL_TREE, c_cast_p, 2056 sflags, complain); 2057 if (!new_second) 2058 return bad_direct_conv ? bad_direct_conv : nullptr; 2059 conv = merge_conversion_sequences (t, new_second); 2060 gcc_assert (maybe_valid_p || conv->bad_p); 2061 return conv; 2062 } 2063 } 2064 2065 conv = build_conv (ck_ref_bind, rto, conv); 2066 /* This reference binding, unlike those above, requires the 2067 creation of a temporary. */ 2068 conv->need_temporary_p = true; 2069 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto); 2070 conv->bad_p |= !maybe_valid_p; 2071 2072 return conv; 2073 } 2074 2075 /* Returns the implicit conversion sequence (see [over.ics]) from type 2076 FROM to type TO. The optional expression EXPR may affect the 2077 conversion. FLAGS are the usual overloading flags. If C_CAST_P is 2078 true, this conversion is coming from a C-style cast. */ 2079 2080 static conversion * 2081 implicit_conversion (tree to, tree from, tree expr, bool c_cast_p, 2082 int flags, tsubst_flags_t complain) 2083 { 2084 conversion *conv; 2085 2086 if (from == error_mark_node || to == error_mark_node 2087 || expr == error_mark_node) 2088 return NULL; 2089 2090 /* Other flags only apply to the primary function in overload 2091 resolution, or after we've chosen one. */ 2092 flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM 2093 |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_NO_NARROWING 2094 |LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL|LOOKUP_SHORTCUT_BAD_CONVS); 2095 2096 /* FIXME: actually we don't want warnings either, but we can't just 2097 have 'complain &= ~(tf_warning|tf_error)' because it would cause 2098 the regression of, eg, g++.old-deja/g++.benjamin/16077.C. 2099 We really ought not to issue that warning until we've committed 2100 to that conversion. */ 2101 complain &= ~tf_error; 2102 2103 /* Call reshape_init early to remove redundant braces. */ 2104 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr) && CLASS_TYPE_P (to)) 2105 { 2106 to = complete_type (to); 2107 if (!COMPLETE_TYPE_P (to)) 2108 return nullptr; 2109 if (!CLASSTYPE_NON_AGGREGATE (to)) 2110 { 2111 expr = reshape_init (to, expr, complain); 2112 if (expr == error_mark_node) 2113 return nullptr; 2114 from = TREE_TYPE (expr); 2115 } 2116 } 2117 2118 if (TYPE_REF_P (to)) 2119 conv = reference_binding (to, from, expr, c_cast_p, flags, complain); 2120 else 2121 conv = standard_conversion (to, from, expr, c_cast_p, flags, complain); 2122 2123 if (conv) 2124 return conv; 2125 2126 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr)) 2127 { 2128 if (is_std_init_list (to) && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr)) 2129 return build_list_conv (to, expr, flags, complain); 2130 2131 /* As an extension, allow list-initialization of _Complex. */ 2132 if (TREE_CODE (to) == COMPLEX_TYPE 2133 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr)) 2134 { 2135 conv = build_complex_conv (to, expr, flags, complain); 2136 if (conv) 2137 return conv; 2138 } 2139 2140 /* Allow conversion from an initializer-list with one element to a 2141 scalar type. */ 2142 if (SCALAR_TYPE_P (to)) 2143 { 2144 int nelts = CONSTRUCTOR_NELTS (expr); 2145 tree elt; 2146 2147 if (nelts == 0) 2148 elt = build_value_init (to, tf_none); 2149 else if (nelts == 1 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr)) 2150 elt = CONSTRUCTOR_ELT (expr, 0)->value; 2151 else 2152 elt = error_mark_node; 2153 2154 conv = implicit_conversion (to, TREE_TYPE (elt), elt, 2155 c_cast_p, flags, complain); 2156 if (conv) 2157 { 2158 conv->check_narrowing = true; 2159 if (BRACE_ENCLOSED_INITIALIZER_P (elt)) 2160 /* Too many levels of braces, i.e. '{{1}}'. */ 2161 conv->bad_p = true; 2162 return conv; 2163 } 2164 } 2165 else if (TREE_CODE (to) == ARRAY_TYPE) 2166 return build_array_conv (to, expr, flags, complain); 2167 } 2168 2169 if (expr != NULL_TREE 2170 && (MAYBE_CLASS_TYPE_P (from) 2171 || MAYBE_CLASS_TYPE_P (to)) 2172 && (flags & LOOKUP_NO_CONVERSION) == 0) 2173 { 2174 struct z_candidate *cand; 2175 2176 if (CLASS_TYPE_P (to) 2177 && BRACE_ENCLOSED_INITIALIZER_P (expr) 2178 && !CLASSTYPE_NON_AGGREGATE (complete_type (to))) 2179 return build_aggr_conv (to, expr, flags, complain); 2180 2181 cand = build_user_type_conversion_1 (to, expr, flags, complain); 2182 if (cand) 2183 { 2184 if (BRACE_ENCLOSED_INITIALIZER_P (expr) 2185 && CONSTRUCTOR_NELTS (expr) == 1 2186 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr) 2187 && !is_list_ctor (cand->fn)) 2188 { 2189 /* "If C is not an initializer-list constructor and the 2190 initializer list has a single element of type cv U, where U is 2191 X or a class derived from X, the implicit conversion sequence 2192 has Exact Match rank if U is X, or Conversion rank if U is 2193 derived from X." */ 2194 tree elt = CONSTRUCTOR_ELT (expr, 0)->value; 2195 tree elttype = TREE_TYPE (elt); 2196 if (reference_related_p (to, elttype)) 2197 return implicit_conversion (to, elttype, elt, 2198 c_cast_p, flags, complain); 2199 } 2200 conv = cand->second_conv; 2201 } 2202 2203 /* We used to try to bind a reference to a temporary here, but that 2204 is now handled after the recursive call to this function at the end 2205 of reference_binding. */ 2206 return conv; 2207 } 2208 2209 return NULL; 2210 } 2211 2212 /* Like implicit_conversion, but return NULL if the conversion is bad. 2213 2214 This is not static so that check_non_deducible_conversion can call it within 2215 add_template_candidate_real as part of overload resolution; it should not be 2216 called outside of overload resolution. */ 2217 2218 conversion * 2219 good_conversion (tree to, tree from, tree expr, 2220 int flags, tsubst_flags_t complain) 2221 { 2222 conversion *c = implicit_conversion (to, from, expr, /*cast*/false, 2223 flags, complain); 2224 if (c && c->bad_p) 2225 c = NULL; 2226 return c; 2227 } 2228 2229 /* Add a new entry to the list of candidates. Used by the add_*_candidate 2230 functions. ARGS will not be changed until a single candidate is 2231 selected. */ 2232 2233 static struct z_candidate * 2234 add_candidate (struct z_candidate **candidates, 2235 tree fn, tree first_arg, const vec<tree, va_gc> *args, 2236 size_t num_convs, conversion **convs, 2237 tree access_path, tree conversion_path, 2238 int viable, struct rejection_reason *reason, 2239 int flags) 2240 { 2241 struct z_candidate *cand = (struct z_candidate *) 2242 conversion_obstack_alloc (sizeof (struct z_candidate)); 2243 2244 cand->fn = fn; 2245 cand->first_arg = first_arg; 2246 cand->args = args; 2247 cand->convs = convs; 2248 cand->num_convs = num_convs; 2249 cand->access_path = access_path; 2250 cand->conversion_path = conversion_path; 2251 cand->viable = viable; 2252 cand->reason = reason; 2253 cand->next = *candidates; 2254 cand->flags = flags; 2255 *candidates = cand; 2256 2257 if (convs && cand->reversed ()) 2258 /* Swap the conversions for comparison in joust; we'll swap them back 2259 before build_over_call. */ 2260 std::swap (convs[0], convs[1]); 2261 2262 return cand; 2263 } 2264 2265 /* FN is a function from the overload set that we outright didn't even 2266 consider (for some reason); add it to the list as an non-viable "ignored" 2267 candidate. */ 2268 2269 static z_candidate * 2270 add_ignored_candidate (z_candidate **candidates, tree fn) 2271 { 2272 /* No need to dynamically allocate these. */ 2273 static const rejection_reason reason_ignored = { rr_ignored, {} }; 2274 2275 struct z_candidate *cand = (struct z_candidate *) 2276 conversion_obstack_alloc (sizeof (struct z_candidate)); 2277 2278 cand->fn = fn; 2279 cand->reason = const_cast<rejection_reason *> (&reason_ignored); 2280 cand->next = *candidates; 2281 *candidates = cand; 2282 2283 return cand; 2284 } 2285 2286 /* True iff CAND is a candidate added by add_ignored_candidate. */ 2287 2288 static bool 2289 ignored_candidate_p (const z_candidate *cand) 2290 { 2291 return cand->reason && cand->reason->code == rr_ignored; 2292 } 2293 2294 /* Return the number of remaining arguments in the parameter list 2295 beginning with ARG. */ 2296 2297 int 2298 remaining_arguments (tree arg) 2299 { 2300 int n; 2301 2302 for (n = 0; arg != NULL_TREE && arg != void_list_node; 2303 arg = TREE_CHAIN (arg)) 2304 n++; 2305 2306 return n; 2307 } 2308 2309 /* [over.match.copy]: When initializing a temporary object (12.2) to be bound 2310 to the first parameter of a constructor where the parameter is of type 2311 "reference to possibly cv-qualified T" and the constructor is called with a 2312 single argument in the context of direct-initialization of an object of type 2313 "cv2 T", explicit conversion functions are also considered. 2314 2315 So set LOOKUP_COPY_PARM to let reference_binding know that 2316 it's being called in that context. */ 2317 2318 int 2319 conv_flags (int i, int nargs, tree fn, tree arg, int flags) 2320 { 2321 int lflags = flags; 2322 tree t; 2323 if (i == 0 && nargs == 1 && DECL_CONSTRUCTOR_P (fn) 2324 && (t = FUNCTION_FIRST_USER_PARMTYPE (fn)) 2325 && (same_type_ignoring_top_level_qualifiers_p 2326 (non_reference (TREE_VALUE (t)), DECL_CONTEXT (fn)))) 2327 { 2328 if (!(flags & LOOKUP_ONLYCONVERTING)) 2329 lflags |= LOOKUP_COPY_PARM; 2330 if ((flags & LOOKUP_LIST_INIT_CTOR) 2331 && BRACE_ENCLOSED_INITIALIZER_P (arg)) 2332 lflags |= LOOKUP_NO_CONVERSION; 2333 } 2334 else 2335 lflags |= LOOKUP_ONLYCONVERTING; 2336 2337 return lflags; 2338 } 2339 2340 /* Build an appropriate 'this' conversion for the method FN and class 2341 type CTYPE from the value ARG (having type ARGTYPE) to the type PARMTYPE. 2342 This function modifies PARMTYPE, ARGTYPE and ARG. */ 2343 2344 static conversion * 2345 build_this_conversion (tree fn, tree ctype, 2346 tree& parmtype, tree& argtype, tree& arg, 2347 int flags, tsubst_flags_t complain) 2348 { 2349 gcc_assert (DECL_IOBJ_MEMBER_FUNCTION_P (fn) 2350 && !DECL_CONSTRUCTOR_P (fn)); 2351 2352 /* The type of the implicit object parameter ('this') for 2353 overload resolution is not always the same as for the 2354 function itself; conversion functions are considered to 2355 be members of the class being converted, and functions 2356 introduced by a using-declaration are considered to be 2357 members of the class that uses them. 2358 2359 Since build_over_call ignores the ICS for the `this' 2360 parameter, we can just change the parm type. */ 2361 parmtype = cp_build_qualified_type (ctype, 2362 cp_type_quals (TREE_TYPE (parmtype))); 2363 bool this_p = true; 2364 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn))) 2365 { 2366 /* If the function has a ref-qualifier, the implicit 2367 object parameter has reference type. */ 2368 bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn)); 2369 parmtype = cp_build_reference_type (parmtype, rv); 2370 /* The special handling of 'this' conversions in compare_ics 2371 does not apply if there is a ref-qualifier. */ 2372 this_p = false; 2373 } 2374 else 2375 { 2376 parmtype = build_pointer_type (parmtype); 2377 /* We don't use build_this here because we don't want to 2378 capture the object argument until we've chosen a 2379 non-static member function. */ 2380 arg = build_address (arg); 2381 argtype = lvalue_type (arg); 2382 } 2383 flags |= LOOKUP_ONLYCONVERTING; 2384 conversion *t = implicit_conversion (parmtype, argtype, arg, 2385 /*c_cast_p=*/false, flags, complain); 2386 t->this_p = this_p; 2387 return t; 2388 } 2389 2390 /* Create an overload candidate for the function or method FN called 2391 with the argument list FIRST_ARG/ARGS and add it to CANDIDATES. 2392 FLAGS is passed on to implicit_conversion. 2393 2394 This does not change ARGS. 2395 2396 CTYPE, if non-NULL, is the type we want to pretend this function 2397 comes from for purposes of overload resolution. 2398 2399 SHORTCUT_BAD_CONVS controls how we handle "bad" argument conversions. 2400 If true, we stop computing conversions upon seeing the first bad 2401 conversion. This is used by add_candidates to avoid computing 2402 more conversions than necessary in the presence of a strictly viable 2403 candidate, while preserving the defacto behavior of overload resolution 2404 when it turns out there are only non-strictly viable candidates. */ 2405 2406 static struct z_candidate * 2407 add_function_candidate (struct z_candidate **candidates, 2408 tree fn, tree ctype, tree first_arg, 2409 const vec<tree, va_gc> *args, tree access_path, 2410 tree conversion_path, int flags, 2411 conversion **convs, 2412 bool shortcut_bad_convs, 2413 tsubst_flags_t complain) 2414 { 2415 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn)); 2416 int i, len; 2417 tree parmnode; 2418 tree orig_first_arg = first_arg; 2419 int skip; 2420 int viable = 1; 2421 struct rejection_reason *reason = NULL; 2422 2423 /* The `this', `in_chrg' and VTT arguments to constructors are not 2424 considered in overload resolution. */ 2425 if (DECL_CONSTRUCTOR_P (fn)) 2426 { 2427 if (ctor_omit_inherited_parms (fn)) 2428 /* Bring back parameters omitted from an inherited ctor. */ 2429 parmlist = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn)); 2430 else 2431 parmlist = skip_artificial_parms_for (fn, parmlist); 2432 skip = num_artificial_parms_for (fn); 2433 if (skip > 0 && first_arg != NULL_TREE) 2434 { 2435 --skip; 2436 first_arg = NULL_TREE; 2437 } 2438 } 2439 else 2440 skip = 0; 2441 2442 len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0); 2443 if (!convs) 2444 convs = alloc_conversions (len); 2445 2446 /* 13.3.2 - Viable functions [over.match.viable] 2447 First, to be a viable function, a candidate function shall have enough 2448 parameters to agree in number with the arguments in the list. 2449 2450 We need to check this first; otherwise, checking the ICSes might cause 2451 us to produce an ill-formed template instantiation. */ 2452 2453 parmnode = parmlist; 2454 for (i = 0; i < len; ++i) 2455 { 2456 if (parmnode == NULL_TREE || parmnode == void_list_node) 2457 break; 2458 parmnode = TREE_CHAIN (parmnode); 2459 } 2460 2461 if ((i < len && parmnode) 2462 || !sufficient_parms_p (parmnode)) 2463 { 2464 int remaining = remaining_arguments (parmnode); 2465 viable = 0; 2466 reason = arity_rejection (first_arg, i + remaining, len); 2467 } 2468 2469 /* An inherited constructor (12.6.3 [class.inhctor.init]) that has a first 2470 parameter of type "reference to cv C" (including such a constructor 2471 instantiated from a template) is excluded from the set of candidate 2472 functions when used to construct an object of type D with an argument list 2473 containing a single argument if C is reference-related to D. */ 2474 if (viable && len == 1 && parmlist && DECL_CONSTRUCTOR_P (fn) 2475 && flag_new_inheriting_ctors 2476 && DECL_INHERITED_CTOR (fn)) 2477 { 2478 tree ptype = non_reference (TREE_VALUE (parmlist)); 2479 tree dtype = DECL_CONTEXT (fn); 2480 tree btype = DECL_INHERITED_CTOR_BASE (fn); 2481 if (reference_related_p (ptype, dtype) 2482 && reference_related_p (btype, ptype)) 2483 { 2484 viable = false; 2485 reason = inherited_ctor_rejection (); 2486 } 2487 } 2488 2489 /* Second, for a function to be viable, its constraints must be 2490 satisfied. */ 2491 if (flag_concepts && viable && !constraints_satisfied_p (fn)) 2492 { 2493 reason = constraint_failure (); 2494 viable = false; 2495 } 2496 2497 /* When looking for a function from a subobject from an implicit 2498 copy/move constructor/operator=, don't consider anything that takes (a 2499 reference to) an unrelated type. See c++/44909 and core 1092. */ 2500 if (viable && parmlist && (flags & LOOKUP_DEFAULTED)) 2501 { 2502 if (DECL_CONSTRUCTOR_P (fn)) 2503 i = 1; 2504 else if (DECL_ASSIGNMENT_OPERATOR_P (fn) 2505 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)) 2506 i = 2; 2507 else 2508 i = 0; 2509 if (i && len == i) 2510 { 2511 parmnode = chain_index (i-1, parmlist); 2512 if (!reference_related_p (non_reference (TREE_VALUE (parmnode)), 2513 ctype)) 2514 viable = 0; 2515 } 2516 2517 /* This only applies at the top level. */ 2518 flags &= ~LOOKUP_DEFAULTED; 2519 } 2520 2521 if (! viable) 2522 goto out; 2523 2524 if (shortcut_bad_convs) 2525 flags |= LOOKUP_SHORTCUT_BAD_CONVS; 2526 else 2527 flags &= ~LOOKUP_SHORTCUT_BAD_CONVS; 2528 2529 /* Third, for F to be a viable function, there shall exist for each 2530 argument an implicit conversion sequence that converts that argument 2531 to the corresponding parameter of F. */ 2532 2533 parmnode = parmlist; 2534 2535 for (i = 0; i < len; ++i) 2536 { 2537 tree argtype, to_type; 2538 tree arg; 2539 2540 if (parmnode == void_list_node) 2541 break; 2542 2543 if (convs[i]) 2544 { 2545 /* Already set during deduction. */ 2546 parmnode = TREE_CHAIN (parmnode); 2547 continue; 2548 } 2549 2550 if (i == 0 && first_arg != NULL_TREE) 2551 arg = first_arg; 2552 else 2553 arg = CONST_CAST_TREE ( 2554 (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]); 2555 argtype = lvalue_type (arg); 2556 2557 conversion *t; 2558 if (parmnode) 2559 { 2560 tree parmtype = TREE_VALUE (parmnode); 2561 if (i == 0 2562 && DECL_IOBJ_MEMBER_FUNCTION_P (fn) 2563 && !DECL_CONSTRUCTOR_P (fn)) 2564 t = build_this_conversion (fn, ctype, parmtype, argtype, arg, 2565 flags, complain); 2566 else 2567 { 2568 int lflags = conv_flags (i, len-skip, fn, arg, flags); 2569 t = implicit_conversion (parmtype, argtype, arg, 2570 /*c_cast_p=*/false, lflags, complain); 2571 } 2572 to_type = parmtype; 2573 parmnode = TREE_CHAIN (parmnode); 2574 } 2575 else 2576 { 2577 t = build_identity_conv (argtype, arg); 2578 t->ellipsis_p = true; 2579 to_type = argtype; 2580 } 2581 2582 convs[i] = t; 2583 if (! t) 2584 { 2585 viable = 0; 2586 reason = arg_conversion_rejection (first_arg, i, argtype, to_type, 2587 EXPR_LOCATION (arg)); 2588 break; 2589 } 2590 2591 if (t->bad_p) 2592 { 2593 viable = -1; 2594 reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type, 2595 EXPR_LOCATION (arg)); 2596 if (shortcut_bad_convs) 2597 break; 2598 } 2599 } 2600 2601 out: 2602 return add_candidate (candidates, fn, orig_first_arg, args, len, convs, 2603 access_path, conversion_path, viable, reason, flags); 2604 } 2605 2606 /* Create an overload candidate for the conversion function FN which will 2607 be invoked for expression OBJ, producing a pointer-to-function which 2608 will in turn be called with the argument list FIRST_ARG/ARGLIST, 2609 and add it to CANDIDATES. This does not change ARGLIST. FLAGS is 2610 passed on to implicit_conversion. 2611 2612 Actually, we don't really care about FN; we care about the type it 2613 converts to. There may be multiple conversion functions that will 2614 convert to that type, and we rely on build_user_type_conversion_1 to 2615 choose the best one; so when we create our candidate, we record the type 2616 instead of the function. */ 2617 2618 static struct z_candidate * 2619 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj, 2620 const vec<tree, va_gc> *arglist, 2621 tree access_path, tree conversion_path, 2622 tsubst_flags_t complain) 2623 { 2624 tree totype = TREE_TYPE (TREE_TYPE (fn)); 2625 int i, len, viable, flags; 2626 tree parmlist, parmnode; 2627 conversion **convs; 2628 struct rejection_reason *reason; 2629 2630 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; ) 2631 parmlist = TREE_TYPE (parmlist); 2632 parmlist = TYPE_ARG_TYPES (parmlist); 2633 2634 len = vec_safe_length (arglist) + 1; 2635 convs = alloc_conversions (len); 2636 parmnode = parmlist; 2637 viable = 1; 2638 flags = LOOKUP_IMPLICIT; 2639 reason = NULL; 2640 2641 /* Don't bother looking up the same type twice. */ 2642 if (*candidates && (*candidates)->fn == totype) 2643 return NULL; 2644 2645 if (!constraints_satisfied_p (fn)) 2646 { 2647 reason = constraint_failure (); 2648 viable = 0; 2649 return add_candidate (candidates, fn, obj, arglist, len, convs, 2650 access_path, conversion_path, viable, reason, flags); 2651 } 2652 2653 for (i = 0; i < len; ++i) 2654 { 2655 tree arg, argtype, convert_type = NULL_TREE; 2656 conversion *t; 2657 2658 if (i == 0) 2659 arg = obj; 2660 else 2661 arg = (*arglist)[i - 1]; 2662 argtype = lvalue_type (arg); 2663 2664 if (i == 0) 2665 { 2666 t = build_identity_conv (argtype, NULL_TREE); 2667 t = build_conv (ck_user, totype, t); 2668 /* Leave the 'cand' field null; we'll figure out the conversion in 2669 convert_like if this candidate is chosen. */ 2670 convert_type = totype; 2671 } 2672 else if (parmnode == void_list_node) 2673 break; 2674 else if (parmnode) 2675 { 2676 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg, 2677 /*c_cast_p=*/false, flags, complain); 2678 convert_type = TREE_VALUE (parmnode); 2679 } 2680 else 2681 { 2682 t = build_identity_conv (argtype, arg); 2683 t->ellipsis_p = true; 2684 convert_type = argtype; 2685 } 2686 2687 convs[i] = t; 2688 if (! t) 2689 break; 2690 2691 if (t->bad_p) 2692 { 2693 viable = -1; 2694 reason = bad_arg_conversion_rejection (NULL_TREE, i, arg, convert_type, 2695 EXPR_LOCATION (arg)); 2696 } 2697 2698 if (i == 0) 2699 continue; 2700 2701 if (parmnode) 2702 parmnode = TREE_CHAIN (parmnode); 2703 } 2704 2705 if (i < len 2706 || ! sufficient_parms_p (parmnode)) 2707 { 2708 int remaining = remaining_arguments (parmnode); 2709 viable = 0; 2710 reason = arity_rejection (NULL_TREE, i + remaining, len); 2711 } 2712 2713 return add_candidate (candidates, totype, obj, arglist, len, convs, 2714 access_path, conversion_path, viable, reason, flags); 2715 } 2716 2717 static void 2718 build_builtin_candidate (struct z_candidate **candidates, tree fnname, 2719 tree type1, tree type2, const vec<tree,va_gc> &args, 2720 tree *argtypes, int flags, tsubst_flags_t complain) 2721 { 2722 conversion *t; 2723 conversion **convs; 2724 size_t num_convs; 2725 int viable = 1; 2726 tree types[2]; 2727 struct rejection_reason *reason = NULL; 2728 2729 types[0] = type1; 2730 types[1] = type2; 2731 2732 num_convs = args.length (); 2733 convs = alloc_conversions (num_convs); 2734 2735 /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit 2736 conversion ops are allowed. We handle that here by just checking for 2737 boolean_type_node because other operators don't ask for it. COND_EXPR 2738 also does contextual conversion to bool for the first operand, but we 2739 handle that in build_conditional_expr, and type1 here is operand 2. */ 2740 if (type1 != boolean_type_node) 2741 flags |= LOOKUP_ONLYCONVERTING; 2742 2743 for (unsigned i = 0; i < 2 && i < num_convs; ++i) 2744 { 2745 t = implicit_conversion (types[i], argtypes[i], args[i], 2746 /*c_cast_p=*/false, flags, complain); 2747 if (! t) 2748 { 2749 viable = 0; 2750 /* We need something for printing the candidate. */ 2751 t = build_identity_conv (types[i], NULL_TREE); 2752 reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i], 2753 types[i], EXPR_LOCATION (args[i])); 2754 } 2755 else if (t->bad_p) 2756 { 2757 viable = 0; 2758 reason = bad_arg_conversion_rejection (NULL_TREE, i, args[i], 2759 types[i], 2760 EXPR_LOCATION (args[i])); 2761 } 2762 convs[i] = t; 2763 } 2764 2765 /* For COND_EXPR we rearranged the arguments; undo that now. */ 2766 if (num_convs == 3) 2767 { 2768 convs[2] = convs[1]; 2769 convs[1] = convs[0]; 2770 t = implicit_conversion (boolean_type_node, argtypes[2], args[2], 2771 /*c_cast_p=*/false, flags, 2772 complain); 2773 if (t) 2774 convs[0] = t; 2775 else 2776 { 2777 viable = 0; 2778 reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2], 2779 boolean_type_node, 2780 EXPR_LOCATION (args[2])); 2781 } 2782 } 2783 2784 add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL, 2785 num_convs, convs, 2786 /*access_path=*/NULL_TREE, 2787 /*conversion_path=*/NULL_TREE, 2788 viable, reason, flags); 2789 } 2790 2791 static bool 2792 is_complete (tree t) 2793 { 2794 return COMPLETE_TYPE_P (complete_type (t)); 2795 } 2796 2797 /* Returns nonzero if TYPE is a promoted arithmetic type. */ 2798 2799 static bool 2800 promoted_arithmetic_type_p (tree type) 2801 { 2802 /* [over.built] 2803 2804 In this section, the term promoted integral type is used to refer 2805 to those integral types which are preserved by integral promotion 2806 (including e.g. int and long but excluding e.g. char). 2807 Similarly, the term promoted arithmetic type refers to promoted 2808 integral types plus floating types. */ 2809 return ((CP_INTEGRAL_TYPE_P (type) 2810 && same_type_p (type_promotes_to (type), type)) 2811 || SCALAR_FLOAT_TYPE_P (type)); 2812 } 2813 2814 /* Create any builtin operator overload candidates for the operator in 2815 question given the converted operand types TYPE1 and TYPE2. The other 2816 args are passed through from add_builtin_candidates to 2817 build_builtin_candidate. 2818 2819 TYPE1 and TYPE2 may not be permissible, and we must filter them. 2820 If CODE is requires candidates operands of the same type of the kind 2821 of which TYPE1 and TYPE2 are, we add both candidates 2822 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */ 2823 2824 static void 2825 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code, 2826 enum tree_code code2, tree fnname, tree type1, 2827 tree type2, vec<tree,va_gc> &args, tree *argtypes, 2828 int flags, tsubst_flags_t complain) 2829 { 2830 switch (code) 2831 { 2832 case POSTINCREMENT_EXPR: 2833 case POSTDECREMENT_EXPR: 2834 args[1] = integer_zero_node; 2835 type2 = integer_type_node; 2836 break; 2837 default: 2838 break; 2839 } 2840 2841 switch (code) 2842 { 2843 2844 /* 4 For every pair (T, VQ), where T is an arithmetic type other than bool, 2845 and VQ is either volatile or empty, there exist candidate operator 2846 functions of the form 2847 VQ T& operator++(VQ T&); 2848 T operator++(VQ T&, int); 2849 5 For every pair (T, VQ), where T is an arithmetic type other than bool, 2850 and VQ is either volatile or empty, there exist candidate operator 2851 functions of the form 2852 VQ T& operator--(VQ T&); 2853 T operator--(VQ T&, int); 2854 6 For every pair (T, VQ), where T is a cv-qualified or cv-unqualified object 2855 type, and VQ is either volatile or empty, there exist candidate operator 2856 functions of the form 2857 T*VQ& operator++(T*VQ&); 2858 T*VQ& operator--(T*VQ&); 2859 T* operator++(T*VQ&, int); 2860 T* operator--(T*VQ&, int); */ 2861 2862 case POSTDECREMENT_EXPR: 2863 case PREDECREMENT_EXPR: 2864 if (TREE_CODE (type1) == BOOLEAN_TYPE) 2865 return; 2866 /* FALLTHRU */ 2867 case POSTINCREMENT_EXPR: 2868 case PREINCREMENT_EXPR: 2869 /* P0002R1, Remove deprecated operator++(bool) added "other than bool" 2870 to p4. */ 2871 if (TREE_CODE (type1) == BOOLEAN_TYPE && cxx_dialect >= cxx17) 2872 return; 2873 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1)) 2874 { 2875 type1 = build_reference_type (type1); 2876 break; 2877 } 2878 return; 2879 2880 /* 7 For every cv-qualified or cv-unqualified object type T, there 2881 exist candidate operator functions of the form 2882 2883 T& operator*(T*); 2884 2885 2886 8 For every function type T that does not have cv-qualifiers or 2887 a ref-qualifier, there exist candidate operator functions of the form 2888 T& operator*(T*); */ 2889 2890 case INDIRECT_REF: 2891 if (TYPE_PTR_P (type1) 2892 && (TYPE_PTROB_P (type1) 2893 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)) 2894 break; 2895 return; 2896 2897 /* 9 For every type T, there exist candidate operator functions of the form 2898 T* operator+(T*); 2899 2900 10 For every floating-point or promoted integral type T, there exist 2901 candidate operator functions of the form 2902 T operator+(T); 2903 T operator-(T); */ 2904 2905 case UNARY_PLUS_EXPR: /* unary + */ 2906 if (TYPE_PTR_P (type1)) 2907 break; 2908 /* FALLTHRU */ 2909 case NEGATE_EXPR: 2910 if (ARITHMETIC_TYPE_P (type1)) 2911 break; 2912 return; 2913 2914 /* 11 For every promoted integral type T, there exist candidate operator 2915 functions of the form 2916 T operator~(T); */ 2917 2918 case BIT_NOT_EXPR: 2919 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1)) 2920 break; 2921 return; 2922 2923 /* 12 For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, C1 2924 is the same type as C2 or is a derived class of C2, and T is an object 2925 type or a function type there exist candidate operator functions of the 2926 form 2927 CV12 T& operator->*(CV1 C1*, CV2 T C2::*); 2928 where CV12 is the union of CV1 and CV2. */ 2929 2930 case MEMBER_REF: 2931 if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2)) 2932 { 2933 tree c1 = TREE_TYPE (type1); 2934 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2); 2935 2936 if (CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1) 2937 && (TYPE_PTRMEMFUNC_P (type2) 2938 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2)))) 2939 break; 2940 } 2941 return; 2942 2943 /* 13 For every pair of types L and R, where each of L and R is a floating-point 2944 or promoted integral type, there exist candidate operator functions of the 2945 form 2946 LR operator*(L, R); 2947 LR operator/(L, R); 2948 LR operator+(L, R); 2949 LR operator-(L, R); 2950 bool operator<(L, R); 2951 bool operator>(L, R); 2952 bool operator<=(L, R); 2953 bool operator>=(L, R); 2954 bool operator==(L, R); 2955 bool operator!=(L, R); 2956 where LR is the result of the usual arithmetic conversions between 2957 types L and R. 2958 2959 14 For every integral type T there exists a candidate operator function of 2960 the form 2961 2962 std::strong_ordering operator<=>(T, T); 2963 2964 15 For every pair of floating-point types L and R, there exists a candidate 2965 operator function of the form 2966 2967 std::partial_ordering operator<=>(L, R); 2968 2969 16 For every cv-qualified or cv-unqualified object type T there exist 2970 candidate operator functions of the form 2971 T* operator+(T*, std::ptrdiff_t); 2972 T& operator[](T*, std::ptrdiff_t); 2973 T* operator-(T*, std::ptrdiff_t); 2974 T* operator+(std::ptrdiff_t, T*); 2975 T& operator[](std::ptrdiff_t, T*); 2976 2977 17 For every T, where T is a pointer to object type, there exist candidate 2978 operator functions of the form 2979 std::ptrdiff_t operator-(T, T); 2980 2981 18 For every T, where T is an enumeration type or a pointer type, there 2982 exist candidate operator functions of the form 2983 bool operator<(T, T); 2984 bool operator>(T, T); 2985 bool operator<=(T, T); 2986 bool operator>=(T, T); 2987 bool operator==(T, T); 2988 bool operator!=(T, T); 2989 R operator<=>(T, T); 2990 2991 where R is the result type specified in [expr.spaceship]. 2992 2993 19 For every T, where T is a pointer-to-member type or std::nullptr_t, 2994 there exist candidate operator functions of the form 2995 bool operator==(T, T); 2996 bool operator!=(T, T); */ 2997 2998 case MINUS_EXPR: 2999 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2)) 3000 break; 3001 if (TYPE_PTROB_P (type1) 3002 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2)) 3003 { 3004 type2 = ptrdiff_type_node; 3005 break; 3006 } 3007 /* FALLTHRU */ 3008 case MULT_EXPR: 3009 case TRUNC_DIV_EXPR: 3010 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) 3011 break; 3012 return; 3013 3014 /* This isn't exactly what's specified above for operator<=>, but it's 3015 close enough. In particular, we don't care about the return type 3016 specified above; it doesn't participate in overload resolution and it 3017 doesn't affect the semantics of the built-in operator. */ 3018 case SPACESHIP_EXPR: 3019 case EQ_EXPR: 3020 case NE_EXPR: 3021 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2)) 3022 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))) 3023 break; 3024 if (NULLPTR_TYPE_P (type1) && NULLPTR_TYPE_P (type2)) 3025 break; 3026 if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1])) 3027 { 3028 type2 = type1; 3029 break; 3030 } 3031 if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0])) 3032 { 3033 type1 = type2; 3034 break; 3035 } 3036 /* Fall through. */ 3037 case LT_EXPR: 3038 case GT_EXPR: 3039 case LE_EXPR: 3040 case GE_EXPR: 3041 case MAX_EXPR: 3042 case MIN_EXPR: 3043 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) 3044 break; 3045 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)) 3046 break; 3047 if (TREE_CODE (type1) == ENUMERAL_TYPE 3048 && TREE_CODE (type2) == ENUMERAL_TYPE) 3049 break; 3050 if (TYPE_PTR_P (type1) 3051 && null_ptr_cst_p (args[1])) 3052 { 3053 type2 = type1; 3054 break; 3055 } 3056 if (null_ptr_cst_p (args[0]) 3057 && TYPE_PTR_P (type2)) 3058 { 3059 type1 = type2; 3060 break; 3061 } 3062 return; 3063 3064 case PLUS_EXPR: 3065 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) 3066 break; 3067 /* FALLTHRU */ 3068 case ARRAY_REF: 3069 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2)) 3070 { 3071 type1 = ptrdiff_type_node; 3072 break; 3073 } 3074 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2)) 3075 { 3076 type2 = ptrdiff_type_node; 3077 break; 3078 } 3079 return; 3080 3081 /* 18For every pair of promoted integral types L and R, there exist candi- 3082 date operator functions of the form 3083 LR operator%(L, R); 3084 LR operator&(L, R); 3085 LR operator^(L, R); 3086 LR operator|(L, R); 3087 L operator<<(L, R); 3088 L operator>>(L, R); 3089 where LR is the result of the usual arithmetic conversions between 3090 types L and R. */ 3091 3092 case TRUNC_MOD_EXPR: 3093 case BIT_AND_EXPR: 3094 case BIT_IOR_EXPR: 3095 case BIT_XOR_EXPR: 3096 case LSHIFT_EXPR: 3097 case RSHIFT_EXPR: 3098 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2)) 3099 break; 3100 return; 3101 3102 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration 3103 type, VQ is either volatile or empty, and R is a promoted arithmetic 3104 type, there exist candidate operator functions of the form 3105 VQ L& operator=(VQ L&, R); 3106 VQ L& operator*=(VQ L&, R); 3107 VQ L& operator/=(VQ L&, R); 3108 VQ L& operator+=(VQ L&, R); 3109 VQ L& operator-=(VQ L&, R); 3110 3111 20For every pair T, VQ), where T is any type and VQ is either volatile 3112 or empty, there exist candidate operator functions of the form 3113 T*VQ& operator=(T*VQ&, T*); 3114 3115 21For every pair T, VQ), where T is a pointer to member type and VQ is 3116 either volatile or empty, there exist candidate operator functions of 3117 the form 3118 VQ T& operator=(VQ T&, T); 3119 3120 22For every triple T, VQ, I), where T is a cv-qualified or cv- 3121 unqualified complete object type, VQ is either volatile or empty, and 3122 I is a promoted integral type, there exist candidate operator func- 3123 tions of the form 3124 T*VQ& operator+=(T*VQ&, I); 3125 T*VQ& operator-=(T*VQ&, I); 3126 3127 23For every triple L, VQ, R), where L is an integral or enumeration 3128 type, VQ is either volatile or empty, and R is a promoted integral 3129 type, there exist candidate operator functions of the form 3130 3131 VQ L& operator%=(VQ L&, R); 3132 VQ L& operator<<=(VQ L&, R); 3133 VQ L& operator>>=(VQ L&, R); 3134 VQ L& operator&=(VQ L&, R); 3135 VQ L& operator^=(VQ L&, R); 3136 VQ L& operator|=(VQ L&, R); */ 3137 3138 case MODIFY_EXPR: 3139 switch (code2) 3140 { 3141 case PLUS_EXPR: 3142 case MINUS_EXPR: 3143 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2)) 3144 { 3145 type2 = ptrdiff_type_node; 3146 break; 3147 } 3148 /* FALLTHRU */ 3149 case MULT_EXPR: 3150 case TRUNC_DIV_EXPR: 3151 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) 3152 break; 3153 return; 3154 3155 case TRUNC_MOD_EXPR: 3156 case BIT_AND_EXPR: 3157 case BIT_IOR_EXPR: 3158 case BIT_XOR_EXPR: 3159 case LSHIFT_EXPR: 3160 case RSHIFT_EXPR: 3161 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2)) 3162 break; 3163 return; 3164 3165 case NOP_EXPR: 3166 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) 3167 break; 3168 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2)) 3169 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)) 3170 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)) 3171 || ((TYPE_PTRMEMFUNC_P (type1) 3172 || TYPE_PTR_P (type1)) 3173 && null_ptr_cst_p (args[1]))) 3174 { 3175 type2 = type1; 3176 break; 3177 } 3178 return; 3179 3180 default: 3181 gcc_unreachable (); 3182 } 3183 type1 = build_reference_type (type1); 3184 break; 3185 3186 case COND_EXPR: 3187 /* [over.built] 3188 3189 For every pair of promoted arithmetic types L and R, there 3190 exist candidate operator functions of the form 3191 3192 LR operator?(bool, L, R); 3193 3194 where LR is the result of the usual arithmetic conversions 3195 between types L and R. 3196 3197 For every type T, where T is a pointer or pointer-to-member 3198 type, there exist candidate operator functions of the form T 3199 operator?(bool, T, T); */ 3200 3201 if (promoted_arithmetic_type_p (type1) 3202 && promoted_arithmetic_type_p (type2)) 3203 /* That's OK. */ 3204 break; 3205 3206 /* Otherwise, the types should be pointers. */ 3207 if (!TYPE_PTR_OR_PTRMEM_P (type1) || !TYPE_PTR_OR_PTRMEM_P (type2)) 3208 return; 3209 3210 /* We don't check that the two types are the same; the logic 3211 below will actually create two candidates; one in which both 3212 parameter types are TYPE1, and one in which both parameter 3213 types are TYPE2. */ 3214 break; 3215 3216 case REALPART_EXPR: 3217 case IMAGPART_EXPR: 3218 if (ARITHMETIC_TYPE_P (type1)) 3219 break; 3220 return; 3221 3222 default: 3223 gcc_unreachable (); 3224 } 3225 3226 /* Make sure we don't create builtin candidates with dependent types. */ 3227 bool u1 = uses_template_parms (type1); 3228 bool u2 = type2 ? uses_template_parms (type2) : false; 3229 if (u1 || u2) 3230 { 3231 /* Try to recover if one of the types is non-dependent. But if 3232 there's only one type, there's nothing we can do. */ 3233 if (!type2) 3234 return; 3235 /* And we lose if both are dependent. */ 3236 if (u1 && u2) 3237 return; 3238 /* Or if they have different forms. */ 3239 if (TREE_CODE (type1) != TREE_CODE (type2)) 3240 return; 3241 3242 if (u1 && !u2) 3243 type1 = type2; 3244 else if (u2 && !u1) 3245 type2 = type1; 3246 } 3247 3248 /* If we're dealing with two pointer types or two enumeral types, 3249 we need candidates for both of them. */ 3250 if (type2 && !same_type_p (type1, type2) 3251 && TREE_CODE (type1) == TREE_CODE (type2) 3252 && (TYPE_REF_P (type1) 3253 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)) 3254 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)) 3255 || TYPE_PTRMEMFUNC_P (type1) 3256 || MAYBE_CLASS_TYPE_P (type1) 3257 || TREE_CODE (type1) == ENUMERAL_TYPE)) 3258 { 3259 if (TYPE_PTR_OR_PTRMEM_P (type1)) 3260 { 3261 tree cptype = composite_pointer_type (input_location, 3262 type1, type2, 3263 error_mark_node, 3264 error_mark_node, 3265 CPO_CONVERSION, 3266 tf_none); 3267 if (cptype != error_mark_node) 3268 { 3269 build_builtin_candidate 3270 (candidates, fnname, cptype, cptype, args, argtypes, 3271 flags, complain); 3272 return; 3273 } 3274 } 3275 3276 build_builtin_candidate 3277 (candidates, fnname, type1, type1, args, argtypes, flags, complain); 3278 build_builtin_candidate 3279 (candidates, fnname, type2, type2, args, argtypes, flags, complain); 3280 return; 3281 } 3282 3283 build_builtin_candidate 3284 (candidates, fnname, type1, type2, args, argtypes, flags, complain); 3285 } 3286 3287 tree 3288 type_decays_to (tree type) 3289 { 3290 if (TREE_CODE (type) == ARRAY_TYPE) 3291 return build_pointer_type (TREE_TYPE (type)); 3292 if (TREE_CODE (type) == FUNCTION_TYPE) 3293 return build_pointer_type (type); 3294 return type; 3295 } 3296 3297 /* There are three conditions of builtin candidates: 3298 3299 1) bool-taking candidates. These are the same regardless of the input. 3300 2) pointer-pair taking candidates. These are generated for each type 3301 one of the input types converts to. 3302 3) arithmetic candidates. According to the standard, we should generate 3303 all of these, but I'm trying not to... 3304 3305 Here we generate a superset of the possible candidates for this particular 3306 case. That is a subset of the full set the standard defines, plus some 3307 other cases which the standard disallows. add_builtin_candidate will 3308 filter out the invalid set. */ 3309 3310 static void 3311 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code, 3312 enum tree_code code2, tree fnname, 3313 vec<tree, va_gc> *argv, 3314 int flags, tsubst_flags_t complain) 3315 { 3316 int ref1; 3317 int enum_p = 0; 3318 tree type, argtypes[3], t; 3319 /* TYPES[i] is the set of possible builtin-operator parameter types 3320 we will consider for the Ith argument. */ 3321 vec<tree, va_gc> *types[2]; 3322 unsigned ix; 3323 vec<tree, va_gc> &args = *argv; 3324 unsigned len = args.length (); 3325 3326 for (unsigned i = 0; i < len; ++i) 3327 { 3328 if (args[i]) 3329 argtypes[i] = unlowered_expr_type (args[i]); 3330 else 3331 argtypes[i] = NULL_TREE; 3332 } 3333 3334 switch (code) 3335 { 3336 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type, 3337 and VQ is either volatile or empty, there exist candidate operator 3338 functions of the form 3339 VQ T& operator++(VQ T&); */ 3340 3341 case POSTINCREMENT_EXPR: 3342 case PREINCREMENT_EXPR: 3343 case POSTDECREMENT_EXPR: 3344 case PREDECREMENT_EXPR: 3345 case MODIFY_EXPR: 3346 ref1 = 1; 3347 break; 3348 3349 /* 24There also exist candidate operator functions of the form 3350 bool operator!(bool); 3351 bool operator&&(bool, bool); 3352 bool operator||(bool, bool); */ 3353 3354 case TRUTH_NOT_EXPR: 3355 build_builtin_candidate 3356 (candidates, fnname, boolean_type_node, 3357 NULL_TREE, args, argtypes, flags, complain); 3358 return; 3359 3360 case TRUTH_ORIF_EXPR: 3361 case TRUTH_ANDIF_EXPR: 3362 build_builtin_candidate 3363 (candidates, fnname, boolean_type_node, 3364 boolean_type_node, args, argtypes, flags, complain); 3365 return; 3366 3367 case ADDR_EXPR: 3368 case COMPOUND_EXPR: 3369 case COMPONENT_REF: 3370 case CO_AWAIT_EXPR: 3371 return; 3372 3373 case COND_EXPR: 3374 case EQ_EXPR: 3375 case NE_EXPR: 3376 case LT_EXPR: 3377 case LE_EXPR: 3378 case GT_EXPR: 3379 case GE_EXPR: 3380 case SPACESHIP_EXPR: 3381 enum_p = 1; 3382 /* Fall through. */ 3383 3384 default: 3385 ref1 = 0; 3386 } 3387 3388 types[0] = make_tree_vector (); 3389 types[1] = make_tree_vector (); 3390 3391 if (len == 3) 3392 len = 2; 3393 for (unsigned i = 0; i < len; ++i) 3394 { 3395 if (MAYBE_CLASS_TYPE_P (argtypes[i])) 3396 { 3397 tree convs; 3398 3399 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR) 3400 return; 3401 3402 convs = lookup_conversions (argtypes[i]); 3403 3404 if (code == COND_EXPR) 3405 { 3406 if (lvalue_p (args[i])) 3407 vec_safe_push (types[i], build_reference_type (argtypes[i])); 3408 3409 vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i])); 3410 } 3411 3412 else if (! convs) 3413 return; 3414 3415 for (; convs; convs = TREE_CHAIN (convs)) 3416 { 3417 type = TREE_TYPE (convs); 3418 3419 if (i == 0 && ref1 3420 && (!TYPE_REF_P (type) 3421 || CP_TYPE_CONST_P (TREE_TYPE (type)))) 3422 continue; 3423 3424 if (code == COND_EXPR && TYPE_REF_P (type)) 3425 vec_safe_push (types[i], type); 3426 3427 type = non_reference (type); 3428 if (i != 0 || ! ref1) 3429 { 3430 type = cv_unqualified (type_decays_to (type)); 3431 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE) 3432 vec_safe_push (types[i], type); 3433 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type)) 3434 type = type_promotes_to (type); 3435 } 3436 3437 if (! vec_member (type, types[i])) 3438 vec_safe_push (types[i], type); 3439 } 3440 } 3441 else 3442 { 3443 if (code == COND_EXPR && lvalue_p (args[i])) 3444 vec_safe_push (types[i], build_reference_type (argtypes[i])); 3445 type = non_reference (argtypes[i]); 3446 if (i != 0 || ! ref1) 3447 { 3448 type = cv_unqualified (type_decays_to (type)); 3449 if (enum_p && UNSCOPED_ENUM_P (type)) 3450 vec_safe_push (types[i], type); 3451 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type)) 3452 type = type_promotes_to (type); 3453 } 3454 vec_safe_push (types[i], type); 3455 } 3456 } 3457 3458 /* Run through the possible parameter types of both arguments, 3459 creating candidates with those parameter types. */ 3460 FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t) 3461 { 3462 unsigned jx; 3463 tree u; 3464 3465 if (!types[1]->is_empty ()) 3466 FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u) 3467 add_builtin_candidate 3468 (candidates, code, code2, fnname, t, 3469 u, args, argtypes, flags, complain); 3470 else 3471 add_builtin_candidate 3472 (candidates, code, code2, fnname, t, 3473 NULL_TREE, args, argtypes, flags, complain); 3474 } 3475 3476 release_tree_vector (types[0]); 3477 release_tree_vector (types[1]); 3478 } 3479 3480 3481 /* If TMPL can be successfully instantiated as indicated by 3482 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES. 3483 3484 TMPL is the template. EXPLICIT_TARGS are any explicit template 3485 arguments. ARGLIST is the arguments provided at the call-site. 3486 This does not change ARGLIST. The RETURN_TYPE is the desired type 3487 for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are 3488 as for add_function_candidate. If an OBJ is supplied, FLAGS and 3489 CTYPE are ignored, and OBJ is as for add_conv_candidate. 3490 3491 SHORTCUT_BAD_CONVS is as in add_function_candidate. */ 3492 3493 static struct z_candidate* 3494 add_template_candidate_real (struct z_candidate **candidates, tree tmpl, 3495 tree ctype, tree explicit_targs, tree first_arg, 3496 const vec<tree, va_gc> *arglist, tree return_type, 3497 tree access_path, tree conversion_path, 3498 int flags, tree obj, unification_kind_t strict, 3499 bool shortcut_bad_convs, tsubst_flags_t complain) 3500 { 3501 int ntparms = DECL_NTPARMS (tmpl); 3502 tree targs = make_tree_vec (ntparms); 3503 unsigned int len = vec_safe_length (arglist); 3504 unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len; 3505 unsigned int skip_without_in_chrg = 0; 3506 tree first_arg_without_in_chrg = first_arg; 3507 tree *args_without_in_chrg; 3508 unsigned int nargs_without_in_chrg; 3509 unsigned int ia, ix; 3510 tree arg; 3511 struct z_candidate *cand; 3512 tree fn; 3513 struct rejection_reason *reason = NULL; 3514 int errs; 3515 conversion **convs = NULL; 3516 3517 /* We don't do deduction on the in-charge parameter, the VTT 3518 parameter or 'this'. */ 3519 if (DECL_IOBJ_MEMBER_FUNCTION_P (tmpl)) 3520 { 3521 if (first_arg_without_in_chrg != NULL_TREE) 3522 first_arg_without_in_chrg = NULL_TREE; 3523 else if (return_type && strict == DEDUCE_CALL) 3524 /* We're deducing for a call to the result of a template conversion 3525 function, so the args don't contain 'this'; leave them alone. */; 3526 else 3527 ++skip_without_in_chrg; 3528 } 3529 3530 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl) 3531 || DECL_BASE_CONSTRUCTOR_P (tmpl)) 3532 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl))) 3533 { 3534 if (first_arg_without_in_chrg != NULL_TREE) 3535 first_arg_without_in_chrg = NULL_TREE; 3536 else 3537 ++skip_without_in_chrg; 3538 } 3539 3540 if (len < skip_without_in_chrg) 3541 return add_ignored_candidate (candidates, tmpl); 3542 3543 if (DECL_CONSTRUCTOR_P (tmpl) && nargs == 2 3544 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg), 3545 TREE_TYPE ((*arglist)[0]))) 3546 { 3547 /* 12.8/6 says, "A declaration of a constructor for a class X is 3548 ill-formed if its first parameter is of type (optionally cv-qualified) 3549 X and either there are no other parameters or else all other 3550 parameters have default arguments. A member function template is never 3551 instantiated to produce such a constructor signature." 3552 3553 So if we're trying to copy an object of the containing class, don't 3554 consider a template constructor that has a first parameter type that 3555 is just a template parameter, as we would deduce a signature that we 3556 would then reject in the code below. */ 3557 if (tree firstparm = FUNCTION_FIRST_USER_PARMTYPE (tmpl)) 3558 { 3559 firstparm = TREE_VALUE (firstparm); 3560 if (PACK_EXPANSION_P (firstparm)) 3561 firstparm = PACK_EXPANSION_PATTERN (firstparm); 3562 if (TREE_CODE (firstparm) == TEMPLATE_TYPE_PARM) 3563 { 3564 gcc_assert (!explicit_targs); 3565 reason = invalid_copy_with_fn_template_rejection (); 3566 goto fail; 3567 } 3568 } 3569 } 3570 3571 nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0) 3572 + (len - skip_without_in_chrg)); 3573 args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg); 3574 ia = 0; 3575 if (first_arg_without_in_chrg != NULL_TREE) 3576 { 3577 args_without_in_chrg[ia] = first_arg_without_in_chrg; 3578 ++ia; 3579 } 3580 for (ix = skip_without_in_chrg; 3581 vec_safe_iterate (arglist, ix, &arg); 3582 ++ix) 3583 { 3584 args_without_in_chrg[ia] = arg; 3585 ++ia; 3586 } 3587 gcc_assert (ia == nargs_without_in_chrg); 3588 3589 if (!obj) 3590 { 3591 /* Check that there's no obvious arity mismatch before proceeding with 3592 deduction. This avoids substituting explicit template arguments 3593 into the template or e.g. derived-to-base parm/arg unification 3594 (which could result in an error outside the immediate context) when 3595 the resulting candidate would be unviable anyway. */ 3596 int min_arity = 0, max_arity = 0; 3597 tree parms = TYPE_ARG_TYPES (TREE_TYPE (tmpl)); 3598 parms = skip_artificial_parms_for (tmpl, parms); 3599 for (; parms != void_list_node; parms = TREE_CHAIN (parms)) 3600 { 3601 if (!parms || PACK_EXPANSION_P (TREE_VALUE (parms))) 3602 { 3603 max_arity = -1; 3604 break; 3605 } 3606 if (TREE_PURPOSE (parms)) 3607 /* A parameter with a default argument. */ 3608 ++max_arity; 3609 else 3610 ++min_arity, ++max_arity; 3611 } 3612 if (ia < (unsigned)min_arity) 3613 { 3614 /* Too few arguments. */ 3615 reason = arity_rejection (NULL_TREE, min_arity, ia, 3616 /*least_p=*/(max_arity == -1)); 3617 goto fail; 3618 } 3619 else if (max_arity != -1 && ia > (unsigned)max_arity) 3620 { 3621 /* Too many arguments. */ 3622 reason = arity_rejection (NULL_TREE, max_arity, ia); 3623 goto fail; 3624 } 3625 3626 convs = alloc_conversions (nargs); 3627 3628 if (shortcut_bad_convs 3629 && DECL_IOBJ_MEMBER_FUNCTION_P (tmpl) 3630 && !DECL_CONSTRUCTOR_P (tmpl)) 3631 { 3632 /* Check the 'this' conversion before proceeding with deduction. 3633 This is effectively an extension of the DR 1391 resolution 3634 that we perform in check_non_deducible_conversions, though it's 3635 convenient to do this extra check here instead of there. */ 3636 tree parmtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (tmpl))); 3637 tree argtype = lvalue_type (first_arg); 3638 tree arg = first_arg; 3639 conversion *t = build_this_conversion (tmpl, ctype, 3640 parmtype, argtype, arg, 3641 flags, complain); 3642 convs[0] = t; 3643 if (t->bad_p) 3644 { 3645 reason = bad_arg_conversion_rejection (first_arg, 0, 3646 arg, parmtype, 3647 EXPR_LOCATION (arg)); 3648 goto fail; 3649 } 3650 } 3651 } 3652 3653 errs = errorcount+sorrycount; 3654 fn = fn_type_unification (tmpl, explicit_targs, targs, 3655 args_without_in_chrg, 3656 nargs_without_in_chrg, 3657 return_type, strict, flags, convs, 3658 false, complain & tf_decltype); 3659 3660 if (fn == error_mark_node) 3661 { 3662 /* Don't repeat unification later if it already resulted in errors. */ 3663 if (errorcount+sorrycount == errs) 3664 reason = template_unification_rejection (tmpl, explicit_targs, 3665 targs, args_without_in_chrg, 3666 nargs_without_in_chrg, 3667 return_type, strict, flags); 3668 else 3669 reason = template_unification_error_rejection (); 3670 goto fail; 3671 } 3672 3673 /* Now the explicit specifier might have been deduced; check if this 3674 declaration is explicit. If it is and we're ignoring non-converting 3675 constructors, don't add this function to the set of candidates. */ 3676 if (((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR)) 3677 == LOOKUP_ONLYCONVERTING) 3678 && DECL_NONCONVERTING_P (fn)) 3679 return add_ignored_candidate (candidates, fn); 3680 3681 if (DECL_CONSTRUCTOR_P (fn) && nargs == 2) 3682 { 3683 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn); 3684 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)), 3685 ctype)) 3686 { 3687 /* We're trying to produce a constructor with a prohibited signature, 3688 as discussed above; handle here any cases we didn't catch then, 3689 such as X(X<T>). */ 3690 reason = invalid_copy_with_fn_template_rejection (); 3691 goto fail; 3692 } 3693 } 3694 3695 if (obj != NULL_TREE) 3696 /* Aha, this is a conversion function. */ 3697 cand = add_conv_candidate (candidates, fn, obj, arglist, 3698 access_path, conversion_path, complain); 3699 else 3700 cand = add_function_candidate (candidates, fn, ctype, 3701 first_arg, arglist, access_path, 3702 conversion_path, flags, convs, 3703 shortcut_bad_convs, complain); 3704 if (DECL_TI_TEMPLATE (fn) != tmpl) 3705 /* This situation can occur if a member template of a template 3706 class is specialized. Then, instantiate_template might return 3707 an instantiation of the specialization, in which case the 3708 DECL_TI_TEMPLATE field will point at the original 3709 specialization. For example: 3710 3711 template <class T> struct S { template <class U> void f(U); 3712 template <> void f(int) {}; }; 3713 S<double> sd; 3714 sd.f(3); 3715 3716 Here, TMPL will be template <class U> S<double>::f(U). 3717 And, instantiate template will give us the specialization 3718 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field 3719 for this will point at template <class T> template <> S<T>::f(int), 3720 so that we can find the definition. For the purposes of 3721 overload resolution, however, we want the original TMPL. */ 3722 cand->template_decl = build_template_info (tmpl, targs); 3723 else 3724 cand->template_decl = DECL_TEMPLATE_INFO (fn); 3725 cand->explicit_targs = explicit_targs; 3726 3727 return cand; 3728 fail: 3729 int viable = (reason->code == rr_bad_arg_conversion ? -1 : 0); 3730 return add_candidate (candidates, tmpl, first_arg, arglist, nargs, convs, 3731 access_path, conversion_path, viable, reason, flags); 3732 } 3733 3734 3735 static struct z_candidate * 3736 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype, 3737 tree explicit_targs, tree first_arg, 3738 const vec<tree, va_gc> *arglist, tree return_type, 3739 tree access_path, tree conversion_path, int flags, 3740 unification_kind_t strict, bool shortcut_bad_convs, 3741 tsubst_flags_t complain) 3742 { 3743 return 3744 add_template_candidate_real (candidates, tmpl, ctype, 3745 explicit_targs, first_arg, arglist, 3746 return_type, access_path, conversion_path, 3747 flags, NULL_TREE, strict, shortcut_bad_convs, 3748 complain); 3749 } 3750 3751 /* Create an overload candidate for the conversion function template TMPL, 3752 returning RETURN_TYPE, which will be invoked for expression OBJ to produce a 3753 pointer-to-function which will in turn be called with the argument list 3754 ARGLIST, and add it to CANDIDATES. This does not change ARGLIST. FLAGS is 3755 passed on to implicit_conversion. */ 3756 3757 static struct z_candidate * 3758 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl, 3759 tree obj, 3760 const vec<tree, va_gc> *arglist, 3761 tree return_type, tree access_path, 3762 tree conversion_path, tsubst_flags_t complain) 3763 { 3764 return 3765 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE, 3766 NULL_TREE, arglist, return_type, access_path, 3767 conversion_path, 0, obj, DEDUCE_CALL, 3768 /*shortcut_bad_convs=*/false, complain); 3769 } 3770 3771 /* The CANDS are the set of candidates that were considered for 3772 overload resolution. Sort CANDS so that the strictly viable 3773 candidates appear first, followed by non-strictly viable candidates, 3774 followed by non-viable candidates. Returns the first candidate 3775 in this sorted list. If any of the candidates were viable, set 3776 *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be 3777 considered viable only if it is strictly viable when setting 3778 *ANY_VIABLE_P. */ 3779 3780 static struct z_candidate* 3781 splice_viable (struct z_candidate *cands, 3782 bool strict_p, 3783 bool *any_viable_p) 3784 { 3785 z_candidate *strictly_viable = nullptr; 3786 z_candidate **strictly_viable_tail = &strictly_viable; 3787 3788 z_candidate *non_strictly_viable = nullptr; 3789 z_candidate **non_strictly_viable_tail = &non_strictly_viable; 3790 3791 z_candidate *non_viable = nullptr; 3792 z_candidate **non_viable_tail = &non_viable; 3793 3794 z_candidate *non_viable_ignored = nullptr; 3795 z_candidate **non_viable_ignored_tail = &non_viable_ignored; 3796 3797 /* Be strict inside templates, since build_over_call won't actually 3798 do the conversions to get pedwarns. */ 3799 if (processing_template_decl) 3800 strict_p = true; 3801 3802 for (z_candidate *cand = cands; cand; cand = cand->next) 3803 { 3804 if (!strict_p 3805 && (cand->viable == 1 || TREE_CODE (cand->fn) == TEMPLATE_DECL)) 3806 /* Be strict in the presence of a viable candidate. Also if 3807 there are template candidates, so that we get deduction errors 3808 for them instead of silently preferring a bad conversion. */ 3809 strict_p = true; 3810 3811 /* Move this candidate to the appropriate list according to 3812 its viability. */ 3813 auto& tail = (cand->viable == 1 ? strictly_viable_tail 3814 : cand->viable == -1 ? non_strictly_viable_tail 3815 : ignored_candidate_p (cand) ? non_viable_ignored_tail 3816 : non_viable_tail); 3817 *tail = cand; 3818 tail = &cand->next; 3819 } 3820 3821 *any_viable_p = (strictly_viable != nullptr 3822 || (!strict_p && non_strictly_viable != nullptr)); 3823 3824 /* Combine the lists. */ 3825 *non_viable_ignored_tail = nullptr; 3826 *non_viable_tail = non_viable_ignored; 3827 *non_strictly_viable_tail = non_viable; 3828 *strictly_viable_tail = non_strictly_viable; 3829 3830 return strictly_viable; 3831 } 3832 3833 static bool 3834 any_strictly_viable (struct z_candidate *cands) 3835 { 3836 for (; cands; cands = cands->next) 3837 if (cands->viable == 1) 3838 return true; 3839 return false; 3840 } 3841 3842 /* OBJ is being used in an expression like "OBJ.f (...)". In other 3843 words, it is about to become the "this" pointer for a member 3844 function call. Take the address of the object. */ 3845 3846 static tree 3847 build_this (tree obj) 3848 { 3849 /* In a template, we are only concerned about the type of the 3850 expression, so we can take a shortcut. */ 3851 if (processing_template_decl) 3852 return build_address (obj); 3853 3854 return cp_build_addr_expr (obj, tf_warning_or_error); 3855 } 3856 3857 /* Returns true iff functions are equivalent. Equivalent functions are 3858 not '==' only if one is a function-local extern function or if 3859 both are extern "C". */ 3860 3861 static inline int 3862 equal_functions (tree fn1, tree fn2) 3863 { 3864 if (TREE_CODE (fn1) != TREE_CODE (fn2)) 3865 return 0; 3866 if (TREE_CODE (fn1) == TEMPLATE_DECL) 3867 return fn1 == fn2; 3868 if (DECL_LOCAL_DECL_P (fn1) || DECL_LOCAL_DECL_P (fn2) 3869 || DECL_EXTERN_C_FUNCTION_P (fn1)) 3870 return decls_match (fn1, fn2); 3871 return fn1 == fn2; 3872 } 3873 3874 /* Print information about a candidate FN being rejected due to INFO. */ 3875 3876 static void 3877 print_conversion_rejection (location_t loc, struct conversion_info *info, 3878 tree fn) 3879 { 3880 tree from = info->from; 3881 if (!TYPE_P (from)) 3882 from = lvalue_type (from); 3883 if (info->n_arg == -1) 3884 { 3885 /* Conversion of implicit `this' argument failed. */ 3886 if (!TYPE_P (info->from)) 3887 /* A bad conversion for 'this' must be discarding cv-quals. */ 3888 inform (loc, " passing %qT as %<this%> " 3889 "argument discards qualifiers", 3890 from); 3891 else 3892 inform (loc, " no known conversion for implicit " 3893 "%<this%> parameter from %qH to %qI", 3894 from, info->to_type); 3895 } 3896 else if (!TYPE_P (info->from)) 3897 { 3898 if (info->n_arg >= 0) 3899 inform (loc, " conversion of argument %d would be ill-formed:", 3900 info->n_arg + 1); 3901 iloc_sentinel ils = loc; 3902 perform_implicit_conversion (info->to_type, info->from, 3903 tf_warning_or_error); 3904 } 3905 else if (info->n_arg == -2) 3906 /* Conversion of conversion function return value failed. */ 3907 inform (loc, " no known conversion from %qH to %qI", 3908 from, info->to_type); 3909 else 3910 { 3911 if (TREE_CODE (fn) == FUNCTION_DECL) 3912 loc = get_fndecl_argument_location (fn, info->n_arg); 3913 inform (loc, " no known conversion for argument %d from %qH to %qI", 3914 info->n_arg + 1, from, info->to_type); 3915 } 3916 } 3917 3918 /* Print information about a candidate with WANT parameters and we found 3919 HAVE. */ 3920 3921 static void 3922 print_arity_information (location_t loc, unsigned int have, unsigned int want, 3923 bool least_p) 3924 { 3925 if (least_p) 3926 inform_n (loc, want, 3927 " candidate expects at least %d argument, %d provided", 3928 " candidate expects at least %d arguments, %d provided", 3929 want, have); 3930 else 3931 inform_n (loc, want, 3932 " candidate expects %d argument, %d provided", 3933 " candidate expects %d arguments, %d provided", 3934 want, have); 3935 } 3936 3937 /* Print information about one overload candidate CANDIDATE. MSGSTR 3938 is the text to print before the candidate itself. 3939 3940 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected 3941 to have been run through gettext by the caller. This wart makes 3942 life simpler in print_z_candidates and for the translators. */ 3943 3944 static void 3945 print_z_candidate (location_t loc, const char *msgstr, 3946 struct z_candidate *candidate) 3947 { 3948 const char *msg = (msgstr == NULL 3949 ? "" 3950 : ACONCAT ((_(msgstr), " ", NULL))); 3951 tree fn = candidate->fn; 3952 if (flag_new_inheriting_ctors) 3953 fn = strip_inheriting_ctors (fn); 3954 location_t cloc = location_of (fn); 3955 3956 if (identifier_p (fn)) 3957 { 3958 cloc = loc; 3959 if (candidate->num_convs == 3) 3960 inform (cloc, "%s%<%D(%T, %T, %T)%> (built-in)", msg, fn, 3961 candidate->convs[0]->type, 3962 candidate->convs[1]->type, 3963 candidate->convs[2]->type); 3964 else if (candidate->num_convs == 2) 3965 inform (cloc, "%s%<%D(%T, %T)%> (built-in)", msg, fn, 3966 candidate->convs[0]->type, 3967 candidate->convs[1]->type); 3968 else 3969 inform (cloc, "%s%<%D(%T)%> (built-in)", msg, fn, 3970 candidate->convs[0]->type); 3971 } 3972 else if (TYPE_P (fn)) 3973 inform (cloc, "%s%qT (conversion)", msg, fn); 3974 else if (candidate->viable == -1) 3975 inform (cloc, "%s%#qD (near match)", msg, fn); 3976 else if (ignored_candidate_p (candidate)) 3977 inform (cloc, "%s%#qD (ignored)", msg, fn); 3978 else if (DECL_DELETED_FN (fn)) 3979 inform (cloc, "%s%#qD (deleted)", msg, fn); 3980 else if (candidate->reversed ()) 3981 inform (cloc, "%s%#qD (reversed)", msg, fn); 3982 else if (candidate->rewritten ()) 3983 inform (cloc, "%s%#qD (rewritten)", msg, fn); 3984 else 3985 inform (cloc, "%s%#qD", msg, fn); 3986 if (fn != candidate->fn) 3987 { 3988 cloc = location_of (candidate->fn); 3989 inform (cloc, " inherited here"); 3990 } 3991 /* Give the user some information about why this candidate failed. */ 3992 if (candidate->reason != NULL) 3993 { 3994 struct rejection_reason *r = candidate->reason; 3995 3996 switch (r->code) 3997 { 3998 case rr_arity: 3999 print_arity_information (cloc, r->u.arity.actual, 4000 r->u.arity.expected, 4001 r->u.arity.least_p); 4002 break; 4003 case rr_arg_conversion: 4004 print_conversion_rejection (cloc, &r->u.conversion, fn); 4005 break; 4006 case rr_bad_arg_conversion: 4007 print_conversion_rejection (cloc, &r->u.bad_conversion, fn); 4008 break; 4009 case rr_explicit_conversion: 4010 inform (cloc, " return type %qT of explicit conversion function " 4011 "cannot be converted to %qT with a qualification " 4012 "conversion", r->u.conversion.from, 4013 r->u.conversion.to_type); 4014 break; 4015 case rr_template_conversion: 4016 inform (cloc, " conversion from return type %qT of template " 4017 "conversion function specialization to %qT is not an " 4018 "exact match", r->u.conversion.from, 4019 r->u.conversion.to_type); 4020 break; 4021 case rr_template_unification: 4022 /* We use template_unification_error_rejection if unification caused 4023 actual non-SFINAE errors, in which case we don't need to repeat 4024 them here. */ 4025 if (r->u.template_unification.tmpl == NULL_TREE) 4026 { 4027 inform (cloc, " substitution of deduced template arguments " 4028 "resulted in errors seen above"); 4029 break; 4030 } 4031 /* Re-run template unification with diagnostics. */ 4032 inform (cloc, " template argument deduction/substitution failed:"); 4033 fn_type_unification (r->u.template_unification.tmpl, 4034 r->u.template_unification.explicit_targs, 4035 (make_tree_vec 4036 (r->u.template_unification.num_targs)), 4037 r->u.template_unification.args, 4038 r->u.template_unification.nargs, 4039 r->u.template_unification.return_type, 4040 r->u.template_unification.strict, 4041 r->u.template_unification.flags, 4042 NULL, true, false); 4043 break; 4044 case rr_invalid_copy: 4045 inform (cloc, 4046 " a constructor taking a single argument of its own " 4047 "class type is invalid"); 4048 break; 4049 case rr_constraint_failure: 4050 diagnose_constraints (cloc, fn, NULL_TREE); 4051 break; 4052 case rr_inherited_ctor: 4053 inform (cloc, " an inherited constructor is not a candidate for " 4054 "initialization from an expression of the same or derived " 4055 "type"); 4056 break; 4057 case rr_ignored: 4058 break; 4059 case rr_none: 4060 default: 4061 /* This candidate didn't have any issues or we failed to 4062 handle a particular code. Either way... */ 4063 gcc_unreachable (); 4064 } 4065 } 4066 } 4067 4068 /* Print information about each overload candidate in CANDIDATES, 4069 which is assumed to have gone through splice_viable and tourney 4070 (if splice_viable succeeded). */ 4071 4072 static void 4073 print_z_candidates (location_t loc, struct z_candidate *candidates, 4074 tristate only_viable_p /* = tristate::unknown () */) 4075 { 4076 struct z_candidate *cand1; 4077 struct z_candidate **cand2; 4078 4079 if (!candidates) 4080 return; 4081 4082 /* Remove non-viable deleted candidates. */ 4083 cand1 = candidates; 4084 for (cand2 = &cand1; *cand2; ) 4085 { 4086 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL 4087 && !(*cand2)->viable 4088 && DECL_DELETED_FN ((*cand2)->fn)) 4089 *cand2 = (*cand2)->next; 4090 else 4091 cand2 = &(*cand2)->next; 4092 } 4093 /* ...if there are any non-deleted ones. */ 4094 if (cand1) 4095 candidates = cand1; 4096 4097 /* There may be duplicates in the set of candidates. We put off 4098 checking this condition as long as possible, since we have no way 4099 to eliminate duplicates from a set of functions in less than n^2 4100 time. Now we are about to emit an error message, so it is more 4101 permissible to go slowly. */ 4102 for (cand1 = candidates; cand1; cand1 = cand1->next) 4103 { 4104 tree fn = cand1->fn; 4105 /* Skip builtin candidates and conversion functions. */ 4106 if (!DECL_P (fn)) 4107 continue; 4108 cand2 = &cand1->next; 4109 while (*cand2) 4110 { 4111 if (DECL_P ((*cand2)->fn) 4112 && equal_functions (fn, (*cand2)->fn)) 4113 *cand2 = (*cand2)->next; 4114 else 4115 cand2 = &(*cand2)->next; 4116 } 4117 } 4118 4119 /* Unless otherwise specified, if there's a (strictly) viable candidate 4120 then we assume we're being called as part of diagnosing ambiguity, in 4121 which case we want to print only viable candidates since non-viable 4122 candidates couldn't have contributed to the ambiguity. */ 4123 if (only_viable_p.is_unknown ()) 4124 only_viable_p = candidates->viable == 1; 4125 4126 for (; candidates; candidates = candidates->next) 4127 { 4128 if (only_viable_p.is_true () && candidates->viable != 1) 4129 break; 4130 if (ignored_candidate_p (candidates) && !flag_diagnostics_all_candidates) 4131 { 4132 inform (loc, "some candidates omitted; " 4133 "use %<-fdiagnostics-all-candidates%> to display them"); 4134 break; 4135 } 4136 print_z_candidate (loc, N_("candidate:"), candidates); 4137 } 4138 } 4139 4140 /* USER_SEQ is a user-defined conversion sequence, beginning with a 4141 USER_CONV. STD_SEQ is the standard conversion sequence applied to 4142 the result of the conversion function to convert it to the final 4143 desired type. Merge the two sequences into a single sequence, 4144 and return the merged sequence. */ 4145 4146 static conversion * 4147 merge_conversion_sequences (conversion *user_seq, conversion *std_seq) 4148 { 4149 conversion **t; 4150 bool bad = user_seq->bad_p; 4151 4152 gcc_assert (user_seq->kind == ck_user); 4153 4154 /* Find the end of the second conversion sequence. */ 4155 for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next)) 4156 { 4157 /* The entire sequence is a user-conversion sequence. */ 4158 (*t)->user_conv_p = true; 4159 if (bad) 4160 (*t)->bad_p = true; 4161 } 4162 4163 if ((*t)->rvaluedness_matches_p) 4164 /* We're binding a reference directly to the result of the conversion. 4165 build_user_type_conversion_1 stripped the REFERENCE_TYPE from the return 4166 type, but we want it back. */ 4167 user_seq->type = TREE_TYPE (TREE_TYPE (user_seq->cand->fn)); 4168 4169 /* Replace the identity conversion with the user conversion 4170 sequence. */ 4171 *t = user_seq; 4172 4173 return std_seq; 4174 } 4175 4176 /* Handle overload resolution for initializing an object of class type from 4177 an initializer list. First we look for a suitable constructor that 4178 takes a std::initializer_list; if we don't find one, we then look for a 4179 non-list constructor. 4180 4181 Parameters are as for add_candidates, except that the arguments are in 4182 the form of a CONSTRUCTOR (the initializer list) rather than a vector, and 4183 the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */ 4184 4185 static void 4186 add_list_candidates (tree fns, tree first_arg, 4187 const vec<tree, va_gc> *args, tree totype, 4188 tree explicit_targs, bool template_only, 4189 tree conversion_path, tree access_path, 4190 int flags, 4191 struct z_candidate **candidates, 4192 tsubst_flags_t complain) 4193 { 4194 gcc_assert (*candidates == NULL); 4195 4196 /* We're looking for a ctor for list-initialization. */ 4197 flags |= LOOKUP_LIST_INIT_CTOR; 4198 /* And we don't allow narrowing conversions. We also use this flag to 4199 avoid the copy constructor call for copy-list-initialization. */ 4200 flags |= LOOKUP_NO_NARROWING; 4201 4202 unsigned nart = num_artificial_parms_for (OVL_FIRST (fns)) - 1; 4203 tree init_list = (*args)[nart]; 4204 4205 /* Always use the default constructor if the list is empty (DR 990). */ 4206 if (CONSTRUCTOR_NELTS (init_list) == 0 4207 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype)) 4208 ; 4209 else if (CONSTRUCTOR_IS_DESIGNATED_INIT (init_list) 4210 && !CP_AGGREGATE_TYPE_P (totype)) 4211 { 4212 if (complain & tf_error) 4213 error ("designated initializers cannot be used with a " 4214 "non-aggregate type %qT", totype); 4215 return; 4216 } 4217 /* If the class has a list ctor, try passing the list as a single 4218 argument first, but only consider list ctors. */ 4219 else if (TYPE_HAS_LIST_CTOR (totype)) 4220 { 4221 flags |= LOOKUP_LIST_ONLY; 4222 add_candidates (fns, first_arg, args, NULL_TREE, 4223 explicit_targs, template_only, conversion_path, 4224 access_path, flags, candidates, complain); 4225 if (any_strictly_viable (*candidates)) 4226 return; 4227 } 4228 4229 /* Expand the CONSTRUCTOR into a new argument vec. */ 4230 vec<tree, va_gc> *new_args; 4231 vec_alloc (new_args, nart + CONSTRUCTOR_NELTS (init_list)); 4232 for (unsigned i = 0; i < nart; ++i) 4233 new_args->quick_push ((*args)[i]); 4234 for (unsigned i = 0; i < CONSTRUCTOR_NELTS (init_list); ++i) 4235 new_args->quick_push (CONSTRUCTOR_ELT (init_list, i)->value); 4236 4237 /* We aren't looking for list-ctors anymore. */ 4238 flags &= ~LOOKUP_LIST_ONLY; 4239 /* We allow more user-defined conversions within an init-list. */ 4240 flags &= ~LOOKUP_NO_CONVERSION; 4241 4242 add_candidates (fns, first_arg, new_args, NULL_TREE, 4243 explicit_targs, template_only, conversion_path, 4244 access_path, flags, candidates, complain); 4245 } 4246 4247 /* Given C(std::initializer_list<A>), return A. */ 4248 4249 static tree 4250 list_ctor_element_type (tree fn) 4251 { 4252 gcc_checking_assert (is_list_ctor (fn)); 4253 4254 tree parm = FUNCTION_FIRST_USER_PARMTYPE (fn); 4255 parm = non_reference (TREE_VALUE (parm)); 4256 return TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0); 4257 } 4258 4259 /* If EXPR is a braced-init-list where the elements all decay to the same type, 4260 return that type. */ 4261 4262 static tree 4263 braced_init_element_type (tree expr) 4264 { 4265 if (TREE_CODE (expr) == CONSTRUCTOR 4266 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE) 4267 return TREE_TYPE (TREE_TYPE (expr)); 4268 if (!BRACE_ENCLOSED_INITIALIZER_P (expr)) 4269 return NULL_TREE; 4270 4271 tree elttype = NULL_TREE; 4272 for (constructor_elt &e: CONSTRUCTOR_ELTS (expr)) 4273 { 4274 tree type = TREE_TYPE (e.value); 4275 type = type_decays_to (type); 4276 if (!elttype) 4277 elttype = type; 4278 else if (!same_type_p (type, elttype)) 4279 return NULL_TREE; 4280 } 4281 return elttype; 4282 } 4283 4284 /* True iff EXPR contains any temporaries with non-trivial destruction. 4285 4286 ??? Also ignore classes with non-trivial but no-op destruction other than 4287 std::allocator? */ 4288 4289 static bool 4290 has_non_trivial_temporaries (tree expr) 4291 { 4292 auto_vec<tree*> temps; 4293 cp_walk_tree_without_duplicates (&expr, find_temps_r, &temps); 4294 for (tree *p : temps) 4295 { 4296 tree t = TREE_TYPE (*p); 4297 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (t) 4298 && !is_std_allocator (t)) 4299 return true; 4300 } 4301 return false; 4302 } 4303 4304 /* We're initializing an array of ELTTYPE from INIT. If it seems useful, 4305 return INIT as an array (of its own type) so the caller can initialize the 4306 target array in a loop. */ 4307 4308 static tree 4309 maybe_init_list_as_array (tree elttype, tree init) 4310 { 4311 /* Only do this if the array can go in rodata but not once converted. */ 4312 if (!TYPE_NON_AGGREGATE_CLASS (elttype)) 4313 return NULL_TREE; 4314 tree init_elttype = braced_init_element_type (init); 4315 if (!init_elttype || !SCALAR_TYPE_P (init_elttype) || !TREE_CONSTANT (init)) 4316 return NULL_TREE; 4317 4318 /* Check with a stub expression to weed out special cases, and check whether 4319 we call the same function for direct-init as copy-list-init. */ 4320 conversion_obstack_sentinel cos; 4321 init_elttype = cp_build_qualified_type (init_elttype, TYPE_QUAL_CONST); 4322 tree arg = build_stub_object (init_elttype); 4323 conversion *c = implicit_conversion (elttype, init_elttype, arg, false, 4324 LOOKUP_NORMAL, tf_none); 4325 if (c && c->kind == ck_rvalue) 4326 c = next_conversion (c); 4327 if (!c || c->kind != ck_user) 4328 return NULL_TREE; 4329 /* Check that we actually can perform the conversion. */ 4330 if (convert_like (c, arg, tf_none) == error_mark_node) 4331 /* Let the normal code give the error. */ 4332 return NULL_TREE; 4333 4334 /* A glvalue initializer might be significant to a reference constructor 4335 or conversion operator. */ 4336 if (!DECL_CONSTRUCTOR_P (c->cand->fn) 4337 || (TYPE_REF_P (TREE_VALUE 4338 (FUNCTION_FIRST_USER_PARMTYPE (c->cand->fn))))) 4339 for (auto &ce : CONSTRUCTOR_ELTS (init)) 4340 if (non_mergeable_glvalue_p (ce.value)) 4341 return NULL_TREE; 4342 4343 tree first = CONSTRUCTOR_ELT (init, 0)->value; 4344 conversion *fc = implicit_conversion (elttype, init_elttype, first, false, 4345 LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING, 4346 tf_none); 4347 if (fc && fc->kind == ck_rvalue) 4348 fc = next_conversion (fc); 4349 if (!fc || fc->kind != ck_user || fc->cand->fn != c->cand->fn) 4350 return NULL_TREE; 4351 first = convert_like (fc, first, tf_none); 4352 if (first == error_mark_node) 4353 /* Let the normal code give the error. */ 4354 return NULL_TREE; 4355 4356 /* Don't do this if the conversion would be constant. */ 4357 first = maybe_constant_init (first); 4358 if (TREE_CONSTANT (first)) 4359 return NULL_TREE; 4360 4361 /* We can't do this if the conversion creates temporaries that need 4362 to live until the whole array is initialized. */ 4363 if (has_non_trivial_temporaries (first)) 4364 return NULL_TREE; 4365 4366 /* We can't do this if copying from the initializer_list would be 4367 ill-formed. */ 4368 tree copy_argtypes = make_tree_vec (1); 4369 TREE_VEC_ELT (copy_argtypes, 0) 4370 = cp_build_qualified_type (elttype, TYPE_QUAL_CONST); 4371 if (!is_xible (INIT_EXPR, elttype, copy_argtypes)) 4372 return NULL_TREE; 4373 4374 tree arr = build_array_of_n_type (init_elttype, CONSTRUCTOR_NELTS (init)); 4375 arr = finish_compound_literal (arr, init, tf_none); 4376 DECL_MERGEABLE (TARGET_EXPR_SLOT (arr)) = true; 4377 return arr; 4378 } 4379 4380 /* If we were going to call e.g. vector(initializer_list<string>) starting 4381 with a list of string-literals (which is inefficient, see PR105838), 4382 instead build an array of const char* and pass it to the range constructor. 4383 But only do this for standard library types, where we can assume the 4384 transformation makes sense. 4385 4386 Really the container classes should have initializer_list<U> constructors to 4387 get the same effect more simply; this is working around that lack. */ 4388 4389 static tree 4390 maybe_init_list_as_range (tree fn, tree expr) 4391 { 4392 if (!processing_template_decl 4393 && BRACE_ENCLOSED_INITIALIZER_P (expr) 4394 && is_list_ctor (fn) 4395 && decl_in_std_namespace_p (fn)) 4396 { 4397 tree to = list_ctor_element_type (fn); 4398 if (tree init = maybe_init_list_as_array (to, expr)) 4399 { 4400 tree begin = decay_conversion (TARGET_EXPR_SLOT (init), tf_none); 4401 tree nelts = array_type_nelts_top (TREE_TYPE (init)); 4402 tree end = cp_build_binary_op (input_location, PLUS_EXPR, begin, 4403 nelts, tf_none); 4404 begin = cp_build_compound_expr (init, begin, tf_none); 4405 return build_constructor_va (init_list_type_node, 2, 4406 NULL_TREE, begin, NULL_TREE, end); 4407 } 4408 } 4409 4410 return NULL_TREE; 4411 } 4412 4413 /* Returns the best overload candidate to perform the requested 4414 conversion. This function is used for three the overloading situations 4415 described in [over.match.copy], [over.match.conv], and [over.match.ref]. 4416 If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as 4417 per [dcl.init.ref], so we ignore temporary bindings. */ 4418 4419 static struct z_candidate * 4420 build_user_type_conversion_1 (tree totype, tree expr, int flags, 4421 tsubst_flags_t complain) 4422 { 4423 struct z_candidate *candidates, *cand; 4424 tree fromtype; 4425 tree ctors = NULL_TREE; 4426 tree conv_fns = NULL_TREE; 4427 conversion *conv = NULL; 4428 tree first_arg = NULL_TREE; 4429 vec<tree, va_gc> *args = NULL; 4430 bool any_viable_p; 4431 int convflags; 4432 4433 if (!expr) 4434 return NULL; 4435 4436 fromtype = TREE_TYPE (expr); 4437 4438 /* We represent conversion within a hierarchy using RVALUE_CONV and 4439 BASE_CONV, as specified by [over.best.ics]; these become plain 4440 constructor calls, as specified in [dcl.init]. */ 4441 gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype) 4442 || !DERIVED_FROM_P (totype, fromtype)); 4443 4444 if (CLASS_TYPE_P (totype)) 4445 /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid 4446 creating a garbage BASELINK; constructors can't be inherited. */ 4447 ctors = get_class_binding (totype, complete_ctor_identifier); 4448 4449 tree to_nonref = non_reference (totype); 4450 if (MAYBE_CLASS_TYPE_P (fromtype)) 4451 { 4452 if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) || 4453 (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype) 4454 && DERIVED_FROM_P (to_nonref, fromtype))) 4455 { 4456 /* [class.conv.fct] A conversion function is never used to 4457 convert a (possibly cv-qualified) object to the (possibly 4458 cv-qualified) same object type (or a reference to it), to a 4459 (possibly cv-qualified) base class of that type (or a 4460 reference to it)... */ 4461 } 4462 else 4463 conv_fns = lookup_conversions (fromtype); 4464 } 4465 4466 candidates = 0; 4467 flags |= LOOKUP_NO_CONVERSION; 4468 if (BRACE_ENCLOSED_INITIALIZER_P (expr)) 4469 flags |= LOOKUP_NO_NARROWING; 4470 /* Prevent add_candidates from treating a non-strictly viable candidate 4471 as unviable. */ 4472 complain |= tf_conv; 4473 4474 /* It's OK to bind a temporary for converting constructor arguments, but 4475 not in converting the return value of a conversion operator. */ 4476 convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION 4477 | (flags & LOOKUP_NO_NARROWING)); 4478 flags &= ~LOOKUP_NO_TEMP_BIND; 4479 4480 if (ctors) 4481 { 4482 int ctorflags = flags; 4483 4484 first_arg = build_dummy_object (totype); 4485 4486 /* We should never try to call the abstract or base constructor 4487 from here. */ 4488 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors)) 4489 && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors))); 4490 4491 args = make_tree_vector_single (expr); 4492 if (BRACE_ENCLOSED_INITIALIZER_P (expr)) 4493 { 4494 /* List-initialization. */ 4495 add_list_candidates (ctors, first_arg, args, totype, NULL_TREE, 4496 false, TYPE_BINFO (totype), TYPE_BINFO (totype), 4497 ctorflags, &candidates, complain); 4498 } 4499 else 4500 { 4501 add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false, 4502 TYPE_BINFO (totype), TYPE_BINFO (totype), 4503 ctorflags, &candidates, complain); 4504 } 4505 4506 for (cand = candidates; cand; cand = cand->next) 4507 { 4508 cand->second_conv = build_identity_conv (totype, NULL_TREE); 4509 4510 /* If totype isn't a reference, and LOOKUP_ONLYCONVERTING is 4511 set, then this is copy-initialization. In that case, "The 4512 result of the call is then used to direct-initialize the 4513 object that is the destination of the copy-initialization." 4514 [dcl.init] 4515 4516 We represent this in the conversion sequence with an 4517 rvalue conversion, which means a constructor call. */ 4518 if (!TYPE_REF_P (totype) 4519 && cxx_dialect < cxx17 4520 && (flags & LOOKUP_ONLYCONVERTING) 4521 && !(convflags & LOOKUP_NO_TEMP_BIND)) 4522 cand->second_conv 4523 = build_conv (ck_rvalue, totype, cand->second_conv); 4524 } 4525 } 4526 4527 if (conv_fns) 4528 { 4529 if (BRACE_ENCLOSED_INITIALIZER_P (expr)) 4530 first_arg = CONSTRUCTOR_ELT (expr, 0)->value; 4531 else 4532 first_arg = expr; 4533 } 4534 4535 for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns)) 4536 { 4537 tree conversion_path = TREE_PURPOSE (conv_fns); 4538 struct z_candidate *old_candidates; 4539 4540 /* If LOOKUP_NO_CONVERSION, don't consider a conversion function that 4541 would need an addional user-defined conversion, i.e. if the return 4542 type differs in class-ness from the desired type. So we avoid 4543 considering operator bool when calling a copy constructor. 4544 4545 This optimization avoids the failure in PR97600, and is allowed by 4546 [temp.inst]/9: "If the function selected by overload resolution can be 4547 determined without instantiating a class template definition, it is 4548 unspecified whether that instantiation actually takes place." */ 4549 tree convtype = non_reference (TREE_TYPE (conv_fns)); 4550 if ((flags & LOOKUP_NO_CONVERSION) 4551 && !WILDCARD_TYPE_P (convtype) 4552 && (CLASS_TYPE_P (to_nonref) 4553 != CLASS_TYPE_P (convtype))) 4554 continue; 4555 4556 /* If we are called to convert to a reference type, we are trying to 4557 find a direct binding, so don't even consider temporaries. If 4558 we don't find a direct binding, the caller will try again to 4559 look for a temporary binding. */ 4560 if (TYPE_REF_P (totype)) 4561 convflags |= LOOKUP_NO_TEMP_BIND; 4562 4563 old_candidates = candidates; 4564 add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype, 4565 NULL_TREE, false, 4566 conversion_path, TYPE_BINFO (fromtype), 4567 flags, &candidates, complain); 4568 4569 for (cand = candidates; cand != old_candidates; cand = cand->next) 4570 { 4571 if (cand->viable == 0) 4572 /* Already rejected, don't change to -1. */ 4573 continue; 4574 4575 tree rettype = TREE_TYPE (TREE_TYPE (cand->fn)); 4576 conversion *ics 4577 = implicit_conversion (totype, 4578 rettype, 4579 0, 4580 /*c_cast_p=*/false, convflags, 4581 complain); 4582 4583 /* If LOOKUP_NO_TEMP_BIND isn't set, then this is 4584 copy-initialization. In that case, "The result of the 4585 call is then used to direct-initialize the object that is 4586 the destination of the copy-initialization." [dcl.init] 4587 4588 We represent this in the conversion sequence with an 4589 rvalue conversion, which means a constructor call. But 4590 don't add a second rvalue conversion if there's already 4591 one there. Which there really shouldn't be, but it's 4592 harmless since we'd add it here anyway. */ 4593 if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue 4594 && !(convflags & LOOKUP_NO_TEMP_BIND)) 4595 ics = build_conv (ck_rvalue, totype, ics); 4596 4597 cand->second_conv = ics; 4598 4599 if (!ics) 4600 { 4601 cand->viable = 0; 4602 cand->reason = arg_conversion_rejection (NULL_TREE, -2, 4603 rettype, totype, 4604 EXPR_LOCATION (expr)); 4605 } 4606 else if (TYPE_REF_P (totype) && !ics->rvaluedness_matches_p 4607 /* Limit this to non-templates for now (PR90546). */ 4608 && !cand->template_decl 4609 && TREE_CODE (TREE_TYPE (totype)) != FUNCTION_TYPE) 4610 { 4611 /* If we are called to convert to a reference type, we are trying 4612 to find a direct binding per [over.match.ref], so rvaluedness 4613 must match for non-functions. */ 4614 cand->viable = 0; 4615 } 4616 else if (DECL_NONCONVERTING_P (cand->fn) 4617 && ics->rank > cr_exact) 4618 { 4619 /* 13.3.1.5: For direct-initialization, those explicit 4620 conversion functions that are not hidden within S and 4621 yield type T or a type that can be converted to type T 4622 with a qualification conversion (4.4) are also candidate 4623 functions. */ 4624 /* 13.3.1.6 doesn't have a parallel restriction, but it should; 4625 I've raised this issue with the committee. --jason 9/2011 */ 4626 cand->viable = -1; 4627 cand->reason = explicit_conversion_rejection (rettype, totype); 4628 } 4629 else if (cand->viable == 1 && ics->bad_p) 4630 { 4631 cand->viable = -1; 4632 cand->reason 4633 = bad_arg_conversion_rejection (NULL_TREE, -2, 4634 rettype, totype, 4635 EXPR_LOCATION (expr)); 4636 } 4637 else if (primary_template_specialization_p (cand->fn) 4638 && ics->rank > cr_exact) 4639 { 4640 /* 13.3.3.1.2: If the user-defined conversion is specified by 4641 a specialization of a conversion function template, the 4642 second standard conversion sequence shall have exact match 4643 rank. */ 4644 cand->viable = -1; 4645 cand->reason = template_conversion_rejection (rettype, totype); 4646 } 4647 } 4648 } 4649 4650 candidates = splice_viable (candidates, false, &any_viable_p); 4651 if (!any_viable_p) 4652 { 4653 if (args) 4654 release_tree_vector (args); 4655 return NULL; 4656 } 4657 4658 cand = tourney (candidates, complain); 4659 if (cand == NULL) 4660 { 4661 if (complain & tf_error) 4662 { 4663 auto_diagnostic_group d; 4664 error_at (cp_expr_loc_or_input_loc (expr), 4665 "conversion from %qH to %qI is ambiguous", 4666 fromtype, totype); 4667 print_z_candidates (location_of (expr), candidates); 4668 } 4669 4670 cand = candidates; /* any one will do */ 4671 cand->second_conv = build_ambiguous_conv (totype, expr); 4672 cand->second_conv->user_conv_p = true; 4673 if (!any_strictly_viable (candidates)) 4674 cand->second_conv->bad_p = true; 4675 if (flags & LOOKUP_ONLYCONVERTING) 4676 cand->second_conv->need_temporary_p = true; 4677 /* If there are viable candidates, don't set ICS_BAD_FLAG; an 4678 ambiguous conversion is no worse than another user-defined 4679 conversion. */ 4680 4681 return cand; 4682 } 4683 4684 /* Maybe pass { } as iterators instead of an initializer_list. */ 4685 if (tree iters = maybe_init_list_as_range (cand->fn, expr)) 4686 if (z_candidate *cand2 4687 = build_user_type_conversion_1 (totype, iters, flags, tf_none)) 4688 if (cand2->viable == 1 && !is_list_ctor (cand2->fn)) 4689 { 4690 cand = cand2; 4691 expr = iters; 4692 } 4693 4694 tree convtype; 4695 if (!DECL_CONSTRUCTOR_P (cand->fn)) 4696 convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn))); 4697 else if (cand->second_conv->kind == ck_rvalue) 4698 /* DR 5: [in the first step of copy-initialization]...if the function 4699 is a constructor, the call initializes a temporary of the 4700 cv-unqualified version of the destination type. */ 4701 convtype = cv_unqualified (totype); 4702 else 4703 convtype = totype; 4704 /* Build the user conversion sequence. */ 4705 conv = build_conv 4706 (ck_user, 4707 convtype, 4708 build_identity_conv (TREE_TYPE (expr), expr)); 4709 conv->cand = cand; 4710 if (cand->viable == -1) 4711 conv->bad_p = true; 4712 4713 /* Remember that this was a list-initialization. */ 4714 if (flags & LOOKUP_NO_NARROWING) 4715 conv->check_narrowing = true; 4716 4717 /* Combine it with the second conversion sequence. */ 4718 cand->second_conv = merge_conversion_sequences (conv, 4719 cand->second_conv); 4720 4721 return cand; 4722 } 4723 4724 /* Wrapper for above. */ 4725 4726 tree 4727 build_user_type_conversion (tree totype, tree expr, int flags, 4728 tsubst_flags_t complain) 4729 { 4730 struct z_candidate *cand; 4731 tree ret; 4732 4733 auto_cond_timevar tv (TV_OVERLOAD); 4734 4735 conversion_obstack_sentinel cos; 4736 4737 cand = build_user_type_conversion_1 (totype, expr, flags, complain); 4738 4739 if (cand) 4740 { 4741 if (cand->second_conv->kind == ck_ambig) 4742 ret = error_mark_node; 4743 else 4744 { 4745 expr = convert_like (cand->second_conv, expr, complain); 4746 ret = convert_from_reference (expr); 4747 } 4748 } 4749 else 4750 ret = NULL_TREE; 4751 4752 return ret; 4753 } 4754 4755 /* Give a helpful diagnostic when implicit_conversion fails. */ 4756 4757 static void 4758 implicit_conversion_error (location_t loc, tree type, tree expr) 4759 { 4760 tsubst_flags_t complain = tf_warning_or_error; 4761 4762 /* If expr has unknown type, then it is an overloaded function. 4763 Call instantiate_type to get good error messages. */ 4764 if (TREE_TYPE (expr) == unknown_type_node) 4765 instantiate_type (type, expr, complain); 4766 else if (invalid_nonstatic_memfn_p (loc, expr, complain)) 4767 /* We gave an error. */; 4768 else if (BRACE_ENCLOSED_INITIALIZER_P (expr) 4769 && CONSTRUCTOR_IS_DESIGNATED_INIT (expr) 4770 && !CP_AGGREGATE_TYPE_P (type)) 4771 error_at (loc, "designated initializers cannot be used with a " 4772 "non-aggregate type %qT", type); 4773 else 4774 { 4775 range_label_for_type_mismatch label (TREE_TYPE (expr), type); 4776 gcc_rich_location rich_loc (loc, &label); 4777 error_at (&rich_loc, "could not convert %qE from %qH to %qI", 4778 expr, TREE_TYPE (expr), type); 4779 } 4780 } 4781 4782 /* Worker for build_converted_constant_expr. */ 4783 4784 static tree 4785 build_converted_constant_expr_internal (tree type, tree expr, 4786 int flags, tsubst_flags_t complain) 4787 { 4788 conversion *conv; 4789 tree t; 4790 location_t loc = cp_expr_loc_or_input_loc (expr); 4791 4792 if (error_operand_p (expr)) 4793 return error_mark_node; 4794 4795 conversion_obstack_sentinel cos; 4796 4797 conv = implicit_conversion (type, TREE_TYPE (expr), expr, 4798 /*c_cast_p=*/false, flags, complain); 4799 4800 /* A converted constant expression of type T is an expression, implicitly 4801 converted to type T, where the converted expression is a constant 4802 expression and the implicit conversion sequence contains only 4803 4804 * user-defined conversions, 4805 * lvalue-to-rvalue conversions (7.1), 4806 * array-to-pointer conversions (7.2), 4807 * function-to-pointer conversions (7.3), 4808 * qualification conversions (7.5), 4809 * integral promotions (7.6), 4810 * integral conversions (7.8) other than narrowing conversions (11.6.4), 4811 * null pointer conversions (7.11) from std::nullptr_t, 4812 * null member pointer conversions (7.12) from std::nullptr_t, and 4813 * function pointer conversions (7.13), 4814 4815 and where the reference binding (if any) binds directly. */ 4816 4817 for (conversion *c = conv; 4818 c && c->kind != ck_identity; 4819 c = next_conversion (c)) 4820 { 4821 switch (c->kind) 4822 { 4823 /* A conversion function is OK. If it isn't constexpr, we'll 4824 complain later that the argument isn't constant. */ 4825 case ck_user: 4826 /* List-initialization is OK. */ 4827 case ck_aggr: 4828 /* The lvalue-to-rvalue conversion is OK. */ 4829 case ck_rvalue: 4830 /* Array-to-pointer and function-to-pointer. */ 4831 case ck_lvalue: 4832 /* Function pointer conversions. */ 4833 case ck_fnptr: 4834 /* Qualification conversions. */ 4835 case ck_qual: 4836 break; 4837 4838 case ck_ref_bind: 4839 if (c->need_temporary_p) 4840 { 4841 if (complain & tf_error) 4842 error_at (loc, "initializing %qH with %qI in converted " 4843 "constant expression does not bind directly", 4844 type, next_conversion (c)->type); 4845 conv = NULL; 4846 } 4847 break; 4848 4849 case ck_base: 4850 case ck_pmem: 4851 case ck_ptr: 4852 case ck_std: 4853 t = next_conversion (c)->type; 4854 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t) 4855 && INTEGRAL_OR_ENUMERATION_TYPE_P (type)) 4856 /* Integral promotion or conversion. */ 4857 break; 4858 if (NULLPTR_TYPE_P (t)) 4859 /* Conversion from nullptr to pointer or pointer-to-member. */ 4860 break; 4861 4862 if (complain & tf_error) 4863 error_at (loc, "conversion from %qH to %qI in a " 4864 "converted constant expression", t, type); 4865 /* fall through. */ 4866 4867 default: 4868 conv = NULL; 4869 break; 4870 } 4871 } 4872 4873 /* Avoid confusing convert_nontype_argument by introducing 4874 a redundant conversion to the same reference type. */ 4875 if (conv && conv->kind == ck_ref_bind 4876 && REFERENCE_REF_P (expr)) 4877 { 4878 tree ref = TREE_OPERAND (expr, 0); 4879 if (same_type_p (type, TREE_TYPE (ref))) 4880 return ref; 4881 } 4882 4883 if (conv) 4884 { 4885 /* Don't copy a class in a template. */ 4886 if (CLASS_TYPE_P (type) && conv->kind == ck_rvalue 4887 && processing_template_decl) 4888 conv = next_conversion (conv); 4889 4890 /* Issuing conversion warnings for value-dependent expressions is 4891 likely too noisy. */ 4892 warning_sentinel w (warn_conversion); 4893 conv->check_narrowing = true; 4894 conv->check_narrowing_const_only = true; 4895 expr = convert_like (conv, expr, complain); 4896 } 4897 else 4898 { 4899 if (complain & tf_error) 4900 implicit_conversion_error (loc, type, expr); 4901 expr = error_mark_node; 4902 } 4903 4904 return expr; 4905 } 4906 4907 /* Subroutine of convert_nontype_argument. 4908 4909 EXPR is an expression used in a context that requires a converted 4910 constant-expression, such as a template non-type parameter. Do any 4911 necessary conversions (that are permitted for converted 4912 constant-expressions) to convert it to the desired type. 4913 4914 This function doesn't consider explicit conversion functions. If 4915 you mean to use "a contextually converted constant expression of type 4916 bool", use build_converted_constant_bool_expr. 4917 4918 If conversion is successful, returns the converted expression; 4919 otherwise, returns error_mark_node. */ 4920 4921 tree 4922 build_converted_constant_expr (tree type, tree expr, tsubst_flags_t complain) 4923 { 4924 return build_converted_constant_expr_internal (type, expr, LOOKUP_IMPLICIT, 4925 complain); 4926 } 4927 4928 /* Used to create "a contextually converted constant expression of type 4929 bool". This differs from build_converted_constant_expr in that it 4930 also considers explicit conversion functions. */ 4931 4932 tree 4933 build_converted_constant_bool_expr (tree expr, tsubst_flags_t complain) 4934 { 4935 return build_converted_constant_expr_internal (boolean_type_node, expr, 4936 LOOKUP_NORMAL, complain); 4937 } 4938 4939 /* Do any initial processing on the arguments to a function call. */ 4940 4941 vec<tree, va_gc> * 4942 resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain) 4943 { 4944 unsigned int ix; 4945 tree arg; 4946 4947 FOR_EACH_VEC_SAFE_ELT (args, ix, arg) 4948 { 4949 if (error_operand_p (arg)) 4950 return NULL; 4951 else if (VOID_TYPE_P (TREE_TYPE (arg))) 4952 { 4953 if (complain & tf_error) 4954 error_at (cp_expr_loc_or_input_loc (arg), 4955 "invalid use of void expression"); 4956 return NULL; 4957 } 4958 else if (invalid_nonstatic_memfn_p (EXPR_LOCATION (arg), arg, complain)) 4959 return NULL; 4960 4961 /* Force auto deduction now. Omit tf_warning to avoid redundant 4962 deprecated warning on deprecated-14.C. */ 4963 if (!mark_single_function (arg, complain & ~tf_warning)) 4964 return NULL; 4965 } 4966 return args; 4967 } 4968 4969 /* Perform overload resolution on FN, which is called with the ARGS. 4970 4971 Return the candidate function selected by overload resolution, or 4972 NULL if the event that overload resolution failed. In the case 4973 that overload resolution fails, *CANDIDATES will be the set of 4974 candidates considered, and ANY_VIABLE_P will be set to true or 4975 false to indicate whether or not any of the candidates were 4976 viable. 4977 4978 The ARGS should already have gone through RESOLVE_ARGS before this 4979 function is called. */ 4980 4981 static struct z_candidate * 4982 perform_overload_resolution (tree fn, 4983 const vec<tree, va_gc> *args, 4984 struct z_candidate **candidates, 4985 bool *any_viable_p, tsubst_flags_t complain) 4986 { 4987 struct z_candidate *cand; 4988 tree explicit_targs; 4989 int template_only; 4990 4991 auto_cond_timevar tv (TV_OVERLOAD); 4992 4993 explicit_targs = NULL_TREE; 4994 template_only = 0; 4995 4996 *candidates = NULL; 4997 *any_viable_p = true; 4998 4999 /* Check FN. */ 5000 gcc_assert (OVL_P (fn) || TREE_CODE (fn) == TEMPLATE_ID_EXPR); 5001 5002 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) 5003 { 5004 explicit_targs = TREE_OPERAND (fn, 1); 5005 fn = TREE_OPERAND (fn, 0); 5006 template_only = 1; 5007 } 5008 5009 /* Add the various candidate functions. */ 5010 add_candidates (fn, NULL_TREE, args, NULL_TREE, 5011 explicit_targs, template_only, 5012 /*conversion_path=*/NULL_TREE, 5013 /*access_path=*/NULL_TREE, 5014 LOOKUP_NORMAL, 5015 candidates, complain); 5016 5017 *candidates = splice_viable (*candidates, false, any_viable_p); 5018 if (*any_viable_p) 5019 cand = tourney (*candidates, complain); 5020 else 5021 cand = NULL; 5022 5023 return cand; 5024 } 5025 5026 /* Print an error message about being unable to build a call to FN with 5027 ARGS. ANY_VIABLE_P indicates whether any candidate functions could 5028 be located; CANDIDATES is a possibly empty list of such 5029 functions. */ 5030 5031 static void 5032 print_error_for_call_failure (tree fn, const vec<tree, va_gc> *args, 5033 struct z_candidate *candidates) 5034 { 5035 tree targs = NULL_TREE; 5036 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) 5037 { 5038 targs = TREE_OPERAND (fn, 1); 5039 fn = TREE_OPERAND (fn, 0); 5040 } 5041 tree name = OVL_NAME (fn); 5042 location_t loc = location_of (name); 5043 if (targs) 5044 name = lookup_template_function (name, targs); 5045 5046 auto_diagnostic_group d; 5047 if (!any_strictly_viable (candidates)) 5048 error_at (loc, "no matching function for call to %<%D(%A)%>", 5049 name, build_tree_list_vec (args)); 5050 else 5051 error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous", 5052 name, build_tree_list_vec (args)); 5053 if (candidates) 5054 print_z_candidates (loc, candidates); 5055 } 5056 5057 /* Perform overload resolution on the set of deduction guides DGUIDES 5058 using ARGS. Returns the selected deduction guide, or error_mark_node 5059 if overload resolution fails. */ 5060 5061 tree 5062 perform_dguide_overload_resolution (tree dguides, const vec<tree, va_gc> *args, 5063 tsubst_flags_t complain) 5064 { 5065 z_candidate *candidates; 5066 bool any_viable_p; 5067 tree result; 5068 5069 gcc_assert (deduction_guide_p (OVL_FIRST (dguides))); 5070 5071 conversion_obstack_sentinel cos; 5072 5073 z_candidate *cand = perform_overload_resolution (dguides, args, &candidates, 5074 &any_viable_p, complain); 5075 if (!cand) 5076 { 5077 if (complain & tf_error) 5078 print_error_for_call_failure (dguides, args, candidates); 5079 result = error_mark_node; 5080 } 5081 else 5082 result = cand->fn; 5083 5084 return result; 5085 } 5086 5087 /* Return an expression for a call to FN (a namespace-scope function, 5088 or a static member function) with the ARGS. This may change 5089 ARGS. */ 5090 5091 tree 5092 build_new_function_call (tree fn, vec<tree, va_gc> **args, 5093 tsubst_flags_t complain) 5094 { 5095 struct z_candidate *candidates, *cand; 5096 bool any_viable_p; 5097 tree result; 5098 5099 if (args != NULL && *args != NULL) 5100 { 5101 *args = resolve_args (*args, complain); 5102 if (*args == NULL) 5103 return error_mark_node; 5104 } 5105 5106 if (flag_tm) 5107 tm_malloc_replacement (fn); 5108 5109 conversion_obstack_sentinel cos; 5110 5111 cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p, 5112 complain); 5113 5114 if (!cand) 5115 { 5116 if (complain & tf_error) 5117 { 5118 // If there is a single (non-viable) function candidate, 5119 // let the error be diagnosed by cp_build_function_call_vec. 5120 if (!any_viable_p && candidates && ! candidates->next 5121 && TREE_CODE (candidates->fn) == FUNCTION_DECL 5122 /* A template-id callee consisting of a single (ignored) 5123 non-template candidate needs to be diagnosed the 5124 ordinary way. */ 5125 && (TREE_CODE (fn) != TEMPLATE_ID_EXPR 5126 || candidates->template_decl)) 5127 return cp_build_function_call_vec (candidates->fn, args, complain); 5128 5129 // Otherwise, emit notes for non-viable candidates. 5130 print_error_for_call_failure (fn, *args, candidates); 5131 } 5132 result = error_mark_node; 5133 } 5134 else 5135 { 5136 result = build_over_call (cand, LOOKUP_NORMAL, complain); 5137 } 5138 5139 if (flag_coroutines 5140 && result 5141 && TREE_CODE (result) == CALL_EXPR 5142 && DECL_BUILT_IN_CLASS (TREE_OPERAND (CALL_EXPR_FN (result), 0)) 5143 == BUILT_IN_NORMAL) 5144 result = coro_validate_builtin_call (result); 5145 5146 return result; 5147 } 5148 5149 /* Build a call to a global operator new. FNNAME is the name of the 5150 operator (either "operator new" or "operator new[]") and ARGS are 5151 the arguments provided. This may change ARGS. *SIZE points to the 5152 total number of bytes required by the allocation, and is updated if 5153 that is changed here. *COOKIE_SIZE is non-NULL if a cookie should 5154 be used. If this function determines that no cookie should be 5155 used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK 5156 is not NULL_TREE, it is evaluated before calculating the final 5157 array size, and if it fails, the array size is replaced with 5158 (size_t)-1 (usually triggering a std::bad_alloc exception). If FN 5159 is non-NULL, it will be set, upon return, to the allocation 5160 function called. */ 5161 5162 tree 5163 build_operator_new_call (tree fnname, vec<tree, va_gc> **args, 5164 tree *size, tree *cookie_size, 5165 tree align_arg, tree size_check, 5166 tree *fn, tsubst_flags_t complain) 5167 { 5168 tree original_size = *size; 5169 tree fns; 5170 struct z_candidate *candidates; 5171 struct z_candidate *cand = NULL; 5172 bool any_viable_p; 5173 5174 if (fn) 5175 *fn = NULL_TREE; 5176 /* Set to (size_t)-1 if the size check fails. */ 5177 if (size_check != NULL_TREE) 5178 { 5179 tree errval = TYPE_MAX_VALUE (sizetype); 5180 if (cxx_dialect >= cxx11 && flag_exceptions) 5181 errval = throw_bad_array_new_length (); 5182 *size = fold_build3 (COND_EXPR, sizetype, size_check, 5183 original_size, errval); 5184 } 5185 vec_safe_insert (*args, 0, *size); 5186 *args = resolve_args (*args, complain); 5187 if (*args == NULL) 5188 return error_mark_node; 5189 5190 conversion_obstack_sentinel cos; 5191 5192 /* Based on: 5193 5194 [expr.new] 5195 5196 If this lookup fails to find the name, or if the allocated type 5197 is not a class type, the allocation function's name is looked 5198 up in the global scope. 5199 5200 we disregard block-scope declarations of "operator new". */ 5201 fns = lookup_qualified_name (global_namespace, fnname); 5202 5203 if (align_arg) 5204 { 5205 vec<tree, va_gc>* align_args 5206 = vec_copy_and_insert (*args, align_arg, 1); 5207 cand = perform_overload_resolution (fns, align_args, &candidates, 5208 &any_viable_p, tf_none); 5209 if (cand) 5210 *args = align_args; 5211 /* If no aligned allocation function matches, try again without the 5212 alignment. */ 5213 } 5214 5215 /* Figure out what function is being called. */ 5216 if (!cand) 5217 cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p, 5218 complain); 5219 5220 /* If no suitable function could be found, issue an error message 5221 and give up. */ 5222 if (!cand) 5223 { 5224 if (complain & tf_error) 5225 print_error_for_call_failure (fns, *args, candidates); 5226 return error_mark_node; 5227 } 5228 5229 /* If a cookie is required, add some extra space. Whether 5230 or not a cookie is required cannot be determined until 5231 after we know which function was called. */ 5232 if (*cookie_size) 5233 { 5234 bool use_cookie = true; 5235 tree arg_types; 5236 5237 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn)); 5238 /* Skip the size_t parameter. */ 5239 arg_types = TREE_CHAIN (arg_types); 5240 /* Check the remaining parameters (if any). */ 5241 if (arg_types 5242 && TREE_CHAIN (arg_types) == void_list_node 5243 && same_type_p (TREE_VALUE (arg_types), 5244 ptr_type_node)) 5245 use_cookie = false; 5246 /* If we need a cookie, adjust the number of bytes allocated. */ 5247 if (use_cookie) 5248 { 5249 /* Update the total size. */ 5250 *size = size_binop (PLUS_EXPR, original_size, *cookie_size); 5251 if (size_check) 5252 { 5253 /* Set to (size_t)-1 if the size check fails. */ 5254 gcc_assert (size_check != NULL_TREE); 5255 *size = fold_build3 (COND_EXPR, sizetype, size_check, 5256 *size, TYPE_MAX_VALUE (sizetype)); 5257 } 5258 /* Update the argument list to reflect the adjusted size. */ 5259 (**args)[0] = *size; 5260 } 5261 else 5262 *cookie_size = NULL_TREE; 5263 } 5264 5265 /* Tell our caller which function we decided to call. */ 5266 if (fn) 5267 *fn = cand->fn; 5268 5269 /* Build the CALL_EXPR. */ 5270 tree ret = build_over_call (cand, LOOKUP_NORMAL, complain); 5271 5272 /* Set this flag for all callers of this function. In addition to 5273 new-expressions, this is called for allocating coroutine state; treat 5274 that as an implicit new-expression. */ 5275 tree call = extract_call_expr (ret); 5276 if (TREE_CODE (call) == CALL_EXPR) 5277 CALL_FROM_NEW_OR_DELETE_P (call) = 1; 5278 5279 return ret; 5280 } 5281 5282 /* Evaluate side-effects from OBJ before evaluating call 5283 to FN in RESULT expression. 5284 This is for expressions of the form `obj->fn(...)' 5285 where `fn' turns out to be a static member function and 5286 `obj' needs to be evaluated. `fn' could be also static operator[] 5287 or static operator(), in which cases the source expression 5288 would be `obj[...]' or `obj(...)'. */ 5289 5290 tree 5291 keep_unused_object_arg (tree result, tree obj, tree fn) 5292 { 5293 if (result == NULL_TREE 5294 || result == error_mark_node 5295 || DECL_OBJECT_MEMBER_FUNCTION_P (fn) 5296 || !TREE_SIDE_EFFECTS (obj)) 5297 return result; 5298 5299 /* But avoid the implicit lvalue-rvalue conversion when `obj' is 5300 volatile. */ 5301 tree a = obj; 5302 if (TREE_THIS_VOLATILE (a)) 5303 a = build_this (a); 5304 if (TREE_SIDE_EFFECTS (a)) 5305 return cp_build_compound_expr (a, result, tf_error); 5306 return result; 5307 } 5308 5309 /* Build a new call to operator(). This may change ARGS. */ 5310 5311 tree 5312 build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain) 5313 { 5314 struct z_candidate *candidates = 0, *cand; 5315 tree fns, convs, first_mem_arg = NULL_TREE; 5316 bool any_viable_p; 5317 tree result = NULL_TREE; 5318 5319 auto_cond_timevar tv (TV_OVERLOAD); 5320 5321 obj = mark_lvalue_use (obj); 5322 5323 if (error_operand_p (obj)) 5324 return error_mark_node; 5325 5326 tree type = TREE_TYPE (obj); 5327 5328 obj = prep_operand (obj); 5329 5330 if (TYPE_PTRMEMFUNC_P (type)) 5331 { 5332 if (complain & tf_error) 5333 /* It's no good looking for an overloaded operator() on a 5334 pointer-to-member-function. */ 5335 error ("pointer-to-member function %qE cannot be called without " 5336 "an object; consider using %<.*%> or %<->*%>", obj); 5337 return error_mark_node; 5338 } 5339 5340 if (TYPE_BINFO (type)) 5341 { 5342 fns = lookup_fnfields (TYPE_BINFO (type), call_op_identifier, 1, complain); 5343 if (fns == error_mark_node) 5344 return error_mark_node; 5345 } 5346 else 5347 fns = NULL_TREE; 5348 5349 if (args != NULL && *args != NULL) 5350 { 5351 *args = resolve_args (*args, complain); 5352 if (*args == NULL) 5353 return error_mark_node; 5354 } 5355 5356 conversion_obstack_sentinel cos; 5357 5358 if (fns) 5359 { 5360 first_mem_arg = obj; 5361 5362 add_candidates (BASELINK_FUNCTIONS (fns), 5363 first_mem_arg, *args, NULL_TREE, 5364 NULL_TREE, false, 5365 BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns), 5366 LOOKUP_NORMAL, &candidates, complain); 5367 } 5368 5369 bool any_call_ops = candidates != nullptr; 5370 5371 convs = lookup_conversions (type); 5372 5373 for (; convs; convs = TREE_CHAIN (convs)) 5374 { 5375 tree totype = TREE_TYPE (convs); 5376 5377 if (TYPE_PTRFN_P (totype) 5378 || TYPE_REFFN_P (totype) 5379 || (TYPE_REF_P (totype) 5380 && TYPE_PTRFN_P (TREE_TYPE (totype)))) 5381 for (tree fn : ovl_range (TREE_VALUE (convs))) 5382 { 5383 if (DECL_NONCONVERTING_P (fn)) 5384 continue; 5385 5386 if (TREE_CODE (fn) == TEMPLATE_DECL) 5387 { 5388 /* Making this work broke PR 71117 and 85118, so until the 5389 committee resolves core issue 2189, let's disable this 5390 candidate if there are any call operators. */ 5391 if (any_call_ops) 5392 continue; 5393 5394 add_template_conv_candidate 5395 (&candidates, fn, obj, *args, totype, 5396 /*access_path=*/NULL_TREE, 5397 /*conversion_path=*/NULL_TREE, complain); 5398 } 5399 else 5400 add_conv_candidate (&candidates, fn, obj, 5401 *args, /*conversion_path=*/NULL_TREE, 5402 /*access_path=*/NULL_TREE, complain); 5403 } 5404 } 5405 5406 /* Be strict here because if we choose a bad conversion candidate, the 5407 errors we get won't mention the call context. */ 5408 candidates = splice_viable (candidates, true, &any_viable_p); 5409 if (!any_viable_p) 5410 { 5411 if (complain & tf_error) 5412 { 5413 auto_diagnostic_group d; 5414 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj), 5415 build_tree_list_vec (*args)); 5416 print_z_candidates (location_of (TREE_TYPE (obj)), candidates); 5417 } 5418 result = error_mark_node; 5419 } 5420 else 5421 { 5422 cand = tourney (candidates, complain); 5423 if (cand == 0) 5424 { 5425 if (complain & tf_error) 5426 { 5427 auto_diagnostic_group d; 5428 error ("call of %<(%T) (%A)%> is ambiguous", 5429 TREE_TYPE (obj), build_tree_list_vec (*args)); 5430 print_z_candidates (location_of (TREE_TYPE (obj)), candidates); 5431 } 5432 result = error_mark_node; 5433 } 5434 else if (TREE_CODE (cand->fn) == FUNCTION_DECL 5435 && DECL_OVERLOADED_OPERATOR_P (cand->fn) 5436 && DECL_OVERLOADED_OPERATOR_IS (cand->fn, CALL_EXPR)) 5437 { 5438 result = build_over_call (cand, LOOKUP_NORMAL, complain); 5439 /* In an expression of the form `a()' where cand->fn 5440 which is operator() turns out to be a static member function, 5441 `a' is none-the-less evaluated. */ 5442 result = keep_unused_object_arg (result, obj, cand->fn); 5443 } 5444 else 5445 { 5446 if (TREE_CODE (cand->fn) == FUNCTION_DECL) 5447 obj = convert_like_with_context (cand->convs[0], obj, cand->fn, 5448 -1, complain); 5449 else 5450 { 5451 gcc_checking_assert (TYPE_P (cand->fn)); 5452 obj = convert_like (cand->convs[0], obj, complain); 5453 } 5454 obj = convert_from_reference (obj); 5455 result = cp_build_function_call_vec (obj, args, complain); 5456 } 5457 } 5458 5459 return result; 5460 } 5461 5462 /* Called by op_error to prepare format strings suitable for the error 5463 function. It concatenates a prefix (controlled by MATCH), ERRMSG, 5464 and a suffix (controlled by NTYPES). */ 5465 5466 static const char * 5467 op_error_string (const char *errmsg, int ntypes, bool match) 5468 { 5469 const char *msg; 5470 5471 const char *msgp = concat (match ? G_("ambiguous overload for ") 5472 : G_("no match for "), errmsg, NULL); 5473 5474 if (ntypes == 3) 5475 msg = concat (msgp, G_(" (operand types are %qT, %qT, and %qT)"), NULL); 5476 else if (ntypes == 2) 5477 msg = concat (msgp, G_(" (operand types are %qT and %qT)"), NULL); 5478 else 5479 msg = concat (msgp, G_(" (operand type is %qT)"), NULL); 5480 5481 return msg; 5482 } 5483 5484 static void 5485 op_error (const op_location_t &loc, 5486 enum tree_code code, enum tree_code code2, 5487 tree arg1, tree arg2, tree arg3, bool match) 5488 { 5489 bool assop = code == MODIFY_EXPR; 5490 const char *opname = OVL_OP_INFO (assop, assop ? code2 : code)->name; 5491 5492 switch (code) 5493 { 5494 case COND_EXPR: 5495 if (flag_diagnostics_show_caret) 5496 error_at (loc, op_error_string (G_("ternary %<operator?:%>"), 5497 3, match), 5498 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3)); 5499 else 5500 error_at (loc, op_error_string (G_("ternary %<operator?:%> " 5501 "in %<%E ? %E : %E%>"), 3, match), 5502 arg1, arg2, arg3, 5503 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3)); 5504 break; 5505 5506 case POSTINCREMENT_EXPR: 5507 case POSTDECREMENT_EXPR: 5508 if (flag_diagnostics_show_caret) 5509 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match), 5510 opname, TREE_TYPE (arg1)); 5511 else 5512 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"), 5513 1, match), 5514 opname, arg1, opname, TREE_TYPE (arg1)); 5515 break; 5516 5517 case ARRAY_REF: 5518 if (flag_diagnostics_show_caret) 5519 error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match), 5520 TREE_TYPE (arg1), TREE_TYPE (arg2)); 5521 else 5522 error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"), 5523 2, match), 5524 arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2)); 5525 break; 5526 5527 case REALPART_EXPR: 5528 case IMAGPART_EXPR: 5529 if (flag_diagnostics_show_caret) 5530 error_at (loc, op_error_string (G_("%qs"), 1, match), 5531 opname, TREE_TYPE (arg1)); 5532 else 5533 error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match), 5534 opname, opname, arg1, TREE_TYPE (arg1)); 5535 break; 5536 5537 case CO_AWAIT_EXPR: 5538 if (flag_diagnostics_show_caret) 5539 error_at (loc, op_error_string (G_("%<operator %s%>"), 1, match), 5540 opname, TREE_TYPE (arg1)); 5541 else 5542 error_at (loc, op_error_string (G_("%<operator %s%> in %<%s%E%>"), 5543 1, match), 5544 opname, opname, arg1, TREE_TYPE (arg1)); 5545 break; 5546 5547 default: 5548 if (arg2) 5549 if (flag_diagnostics_show_caret) 5550 { 5551 binary_op_rich_location richloc (loc, arg1, arg2, true); 5552 error_at (&richloc, 5553 op_error_string (G_("%<operator%s%>"), 2, match), 5554 opname, TREE_TYPE (arg1), TREE_TYPE (arg2)); 5555 } 5556 else 5557 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"), 5558 2, match), 5559 opname, arg1, opname, arg2, 5560 TREE_TYPE (arg1), TREE_TYPE (arg2)); 5561 else 5562 if (flag_diagnostics_show_caret) 5563 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match), 5564 opname, TREE_TYPE (arg1)); 5565 else 5566 error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"), 5567 1, match), 5568 opname, opname, arg1, TREE_TYPE (arg1)); 5569 break; 5570 } 5571 } 5572 5573 /* Return the implicit conversion sequence that could be used to 5574 convert E1 to E2 in [expr.cond]. */ 5575 5576 static conversion * 5577 conditional_conversion (tree e1, tree e2, tsubst_flags_t complain) 5578 { 5579 tree t1 = non_reference (TREE_TYPE (e1)); 5580 tree t2 = non_reference (TREE_TYPE (e2)); 5581 conversion *conv; 5582 bool good_base; 5583 5584 /* [expr.cond] 5585 5586 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be 5587 implicitly converted (clause _conv_) to the type "lvalue reference to 5588 T2", subject to the constraint that in the conversion the 5589 reference must bind directly (_dcl.init.ref_) to an lvalue. 5590 5591 If E2 is an xvalue: E1 can be converted to match E2 if E1 can be 5592 implicitly converted to the type "rvalue reference to T2", subject to 5593 the constraint that the reference must bind directly. */ 5594 if (glvalue_p (e2)) 5595 { 5596 tree rtype = cp_build_reference_type (t2, !lvalue_p (e2)); 5597 conv = implicit_conversion (rtype, 5598 t1, 5599 e1, 5600 /*c_cast_p=*/false, 5601 LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND 5602 |LOOKUP_ONLYCONVERTING, 5603 complain); 5604 if (conv && !conv->bad_p) 5605 return conv; 5606 } 5607 5608 /* If E2 is a prvalue or if neither of the conversions above can be done 5609 and at least one of the operands has (possibly cv-qualified) class 5610 type: */ 5611 if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2)) 5612 return NULL; 5613 5614 /* [expr.cond] 5615 5616 If E1 and E2 have class type, and the underlying class types are 5617 the same or one is a base class of the other: E1 can be converted 5618 to match E2 if the class of T2 is the same type as, or a base 5619 class of, the class of T1, and the cv-qualification of T2 is the 5620 same cv-qualification as, or a greater cv-qualification than, the 5621 cv-qualification of T1. If the conversion is applied, E1 is 5622 changed to an rvalue of type T2 that still refers to the original 5623 source class object (or the appropriate subobject thereof). */ 5624 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2) 5625 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2))) 5626 { 5627 if (good_base && at_least_as_qualified_p (t2, t1)) 5628 { 5629 conv = build_identity_conv (t1, e1); 5630 if (!same_type_p (TYPE_MAIN_VARIANT (t1), 5631 TYPE_MAIN_VARIANT (t2))) 5632 conv = build_conv (ck_base, t2, conv); 5633 else 5634 conv = build_conv (ck_rvalue, t2, conv); 5635 return conv; 5636 } 5637 else 5638 return NULL; 5639 } 5640 else 5641 /* [expr.cond] 5642 5643 Otherwise: E1 can be converted to match E2 if E1 can be implicitly 5644 converted to the type that expression E2 would have if E2 were 5645 converted to an rvalue (or the type it has, if E2 is an rvalue). */ 5646 return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false, 5647 LOOKUP_IMPLICIT, complain); 5648 } 5649 5650 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three 5651 arguments to the conditional expression. */ 5652 5653 tree 5654 build_conditional_expr (const op_location_t &loc, 5655 tree arg1, tree arg2, tree arg3, 5656 tsubst_flags_t complain) 5657 { 5658 tree arg2_type; 5659 tree arg3_type; 5660 tree result = NULL_TREE; 5661 tree result_type = NULL_TREE; 5662 tree semantic_result_type = NULL_TREE; 5663 bool is_glvalue = true; 5664 struct z_candidate *candidates = 0; 5665 struct z_candidate *cand; 5666 tree orig_arg2, orig_arg3; 5667 5668 auto_cond_timevar tv (TV_OVERLOAD); 5669 5670 /* As a G++ extension, the second argument to the conditional can be 5671 omitted. (So that `a ? : c' is roughly equivalent to `a ? a : 5672 c'.) If the second operand is omitted, make sure it is 5673 calculated only once. */ 5674 if (!arg2) 5675 { 5676 if (complain & tf_error) 5677 pedwarn (loc, OPT_Wpedantic, 5678 "ISO C++ forbids omitting the middle term of " 5679 "a %<?:%> expression"); 5680 5681 if ((complain & tf_warning) && !truth_value_p (TREE_CODE (arg1))) 5682 warn_for_omitted_condop (loc, arg1); 5683 5684 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */ 5685 if (glvalue_p (arg1)) 5686 { 5687 arg1 = cp_stabilize_reference (arg1); 5688 arg2 = arg1 = prevent_lifetime_extension (arg1); 5689 } 5690 else if (TREE_CODE (arg1) == TARGET_EXPR) 5691 /* arg1 can't be a prvalue result of the conditional 5692 expression, since it needs to be materialized for the 5693 conversion to bool, so treat it as an xvalue in arg2. */ 5694 arg2 = move (TARGET_EXPR_SLOT (arg1)); 5695 else if (TREE_CODE (arg1) == EXCESS_PRECISION_EXPR) 5696 arg2 = arg1 = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (arg1), 5697 cp_save_expr (TREE_OPERAND (arg1, 0))); 5698 else 5699 arg2 = arg1 = cp_save_expr (arg1); 5700 } 5701 5702 /* If something has already gone wrong, just pass that fact up the 5703 tree. */ 5704 if (error_operand_p (arg1) 5705 || error_operand_p (arg2) 5706 || error_operand_p (arg3)) 5707 return error_mark_node; 5708 5709 conversion_obstack_sentinel cos; 5710 5711 orig_arg2 = arg2; 5712 orig_arg3 = arg3; 5713 5714 if (gnu_vector_type_p (TREE_TYPE (arg1)) 5715 && VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1))) 5716 { 5717 tree arg1_type = TREE_TYPE (arg1); 5718 5719 /* If arg1 is another cond_expr choosing between -1 and 0, 5720 then we can use its comparison. It may help to avoid 5721 additional comparison, produce more accurate diagnostics 5722 and enables folding. */ 5723 if (TREE_CODE (arg1) == VEC_COND_EXPR 5724 && integer_minus_onep (TREE_OPERAND (arg1, 1)) 5725 && integer_zerop (TREE_OPERAND (arg1, 2))) 5726 arg1 = TREE_OPERAND (arg1, 0); 5727 5728 arg1 = force_rvalue (arg1, complain); 5729 arg2 = force_rvalue (arg2, complain); 5730 arg3 = force_rvalue (arg3, complain); 5731 5732 /* force_rvalue can return error_mark on valid arguments. */ 5733 if (error_operand_p (arg1) 5734 || error_operand_p (arg2) 5735 || error_operand_p (arg3)) 5736 return error_mark_node; 5737 5738 arg2_type = TREE_TYPE (arg2); 5739 arg3_type = TREE_TYPE (arg3); 5740 5741 if (!VECTOR_TYPE_P (arg2_type) 5742 && !VECTOR_TYPE_P (arg3_type)) 5743 { 5744 /* Rely on the error messages of the scalar version. */ 5745 tree scal = build_conditional_expr (loc, integer_one_node, 5746 orig_arg2, orig_arg3, complain); 5747 if (scal == error_mark_node) 5748 return error_mark_node; 5749 tree stype = TREE_TYPE (scal); 5750 tree ctype = TREE_TYPE (arg1_type); 5751 if (TYPE_SIZE (stype) != TYPE_SIZE (ctype) 5752 || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype))) 5753 { 5754 if (complain & tf_error) 5755 error_at (loc, "inferred scalar type %qT is not an integer or " 5756 "floating-point type of the same size as %qT", stype, 5757 COMPARISON_CLASS_P (arg1) 5758 ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0))) 5759 : ctype); 5760 return error_mark_node; 5761 } 5762 5763 tree vtype = build_opaque_vector_type (stype, 5764 TYPE_VECTOR_SUBPARTS (arg1_type)); 5765 /* We could pass complain & tf_warning to unsafe_conversion_p, 5766 but the warnings (like Wsign-conversion) have already been 5767 given by the scalar build_conditional_expr_1. We still check 5768 unsafe_conversion_p to forbid truncating long long -> float. */ 5769 if (unsafe_conversion_p (stype, arg2, NULL_TREE, false)) 5770 { 5771 if (complain & tf_error) 5772 error_at (loc, "conversion of scalar %qH to vector %qI " 5773 "involves truncation", arg2_type, vtype); 5774 return error_mark_node; 5775 } 5776 if (unsafe_conversion_p (stype, arg3, NULL_TREE, false)) 5777 { 5778 if (complain & tf_error) 5779 error_at (loc, "conversion of scalar %qH to vector %qI " 5780 "involves truncation", arg3_type, vtype); 5781 return error_mark_node; 5782 } 5783 5784 arg2 = cp_convert (stype, arg2, complain); 5785 arg2 = save_expr (arg2); 5786 arg2 = build_vector_from_val (vtype, arg2); 5787 arg2_type = vtype; 5788 arg3 = cp_convert (stype, arg3, complain); 5789 arg3 = save_expr (arg3); 5790 arg3 = build_vector_from_val (vtype, arg3); 5791 arg3_type = vtype; 5792 } 5793 5794 if ((gnu_vector_type_p (arg2_type) && !VECTOR_TYPE_P (arg3_type)) 5795 || (gnu_vector_type_p (arg3_type) && !VECTOR_TYPE_P (arg2_type))) 5796 { 5797 enum stv_conv convert_flag = 5798 scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3, 5799 complain & tf_error); 5800 5801 switch (convert_flag) 5802 { 5803 case stv_error: 5804 return error_mark_node; 5805 case stv_firstarg: 5806 { 5807 arg2 = save_expr (arg2); 5808 arg2 = convert (TREE_TYPE (arg3_type), arg2); 5809 arg2 = build_vector_from_val (arg3_type, arg2); 5810 arg2_type = TREE_TYPE (arg2); 5811 break; 5812 } 5813 case stv_secondarg: 5814 { 5815 arg3 = save_expr (arg3); 5816 arg3 = convert (TREE_TYPE (arg2_type), arg3); 5817 arg3 = build_vector_from_val (arg2_type, arg3); 5818 arg3_type = TREE_TYPE (arg3); 5819 break; 5820 } 5821 default: 5822 break; 5823 } 5824 } 5825 5826 if (!gnu_vector_type_p (arg2_type) 5827 || !gnu_vector_type_p (arg3_type) 5828 || !same_type_p (arg2_type, arg3_type) 5829 || maybe_ne (TYPE_VECTOR_SUBPARTS (arg1_type), 5830 TYPE_VECTOR_SUBPARTS (arg2_type)) 5831 || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type)) 5832 { 5833 if (complain & tf_error) 5834 error_at (loc, 5835 "incompatible vector types in conditional expression: " 5836 "%qT, %qT and %qT", TREE_TYPE (arg1), 5837 TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3)); 5838 return error_mark_node; 5839 } 5840 5841 if (!COMPARISON_CLASS_P (arg1)) 5842 { 5843 tree cmp_type = truth_type_for (arg1_type); 5844 arg1 = build2 (NE_EXPR, cmp_type, arg1, build_zero_cst (arg1_type)); 5845 } 5846 return build3_loc (loc, VEC_COND_EXPR, arg2_type, arg1, arg2, arg3); 5847 } 5848 5849 /* [expr.cond] 5850 5851 The first expression is implicitly converted to bool (clause 5852 _conv_). */ 5853 arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain, 5854 LOOKUP_NORMAL); 5855 if (error_operand_p (arg1)) 5856 return error_mark_node; 5857 5858 arg2_type = unlowered_expr_type (arg2); 5859 arg3_type = unlowered_expr_type (arg3); 5860 5861 if ((TREE_CODE (arg2) == EXCESS_PRECISION_EXPR 5862 || TREE_CODE (arg3) == EXCESS_PRECISION_EXPR) 5863 && (TREE_CODE (arg2_type) == INTEGER_TYPE 5864 || SCALAR_FLOAT_TYPE_P (arg2_type) 5865 || TREE_CODE (arg2_type) == COMPLEX_TYPE) 5866 && (TREE_CODE (arg3_type) == INTEGER_TYPE 5867 || SCALAR_FLOAT_TYPE_P (arg3_type) 5868 || TREE_CODE (arg3_type) == COMPLEX_TYPE)) 5869 { 5870 semantic_result_type 5871 = type_after_usual_arithmetic_conversions (arg2_type, arg3_type); 5872 if (semantic_result_type == error_mark_node) 5873 { 5874 tree t1 = arg2_type; 5875 tree t2 = arg3_type; 5876 if (TREE_CODE (t1) == COMPLEX_TYPE) 5877 t1 = TREE_TYPE (t1); 5878 if (TREE_CODE (t2) == COMPLEX_TYPE) 5879 t2 = TREE_TYPE (t2); 5880 gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1) 5881 && SCALAR_FLOAT_TYPE_P (t2) 5882 && (extended_float_type_p (t1) 5883 || extended_float_type_p (t2)) 5884 && cp_compare_floating_point_conversion_ranks 5885 (t1, t2) == 3); 5886 if (complain & tf_error) 5887 error_at (loc, "operands to %<?:%> of types %qT and %qT " 5888 "have unordered conversion rank", 5889 arg2_type, arg3_type); 5890 return error_mark_node; 5891 } 5892 if (TREE_CODE (arg2) == EXCESS_PRECISION_EXPR) 5893 { 5894 arg2 = TREE_OPERAND (arg2, 0); 5895 arg2_type = TREE_TYPE (arg2); 5896 } 5897 if (TREE_CODE (arg3) == EXCESS_PRECISION_EXPR) 5898 { 5899 arg3 = TREE_OPERAND (arg3, 0); 5900 arg3_type = TREE_TYPE (arg3); 5901 } 5902 } 5903 5904 /* [expr.cond] 5905 5906 If either the second or the third operand has type (possibly 5907 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_), 5908 array-to-pointer (_conv.array_), and function-to-pointer 5909 (_conv.func_) standard conversions are performed on the second 5910 and third operands. */ 5911 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type)) 5912 { 5913 /* 'void' won't help in resolving an overloaded expression on the 5914 other side, so require it to resolve by itself. */ 5915 if (arg2_type == unknown_type_node) 5916 { 5917 arg2 = resolve_nondeduced_context_or_error (arg2, complain); 5918 arg2_type = TREE_TYPE (arg2); 5919 } 5920 if (arg3_type == unknown_type_node) 5921 { 5922 arg3 = resolve_nondeduced_context_or_error (arg3, complain); 5923 arg3_type = TREE_TYPE (arg3); 5924 } 5925 5926 /* [expr.cond] 5927 5928 One of the following shall hold: 5929 5930 --The second or the third operand (but not both) is a 5931 throw-expression (_except.throw_); the result is of the type 5932 and value category of the other. 5933 5934 --Both the second and the third operands have type void; the 5935 result is of type void and is a prvalue. */ 5936 if (TREE_CODE (arg2) == THROW_EXPR 5937 && TREE_CODE (arg3) != THROW_EXPR) 5938 { 5939 result_type = arg3_type; 5940 is_glvalue = glvalue_p (arg3); 5941 } 5942 else if (TREE_CODE (arg2) != THROW_EXPR 5943 && TREE_CODE (arg3) == THROW_EXPR) 5944 { 5945 result_type = arg2_type; 5946 is_glvalue = glvalue_p (arg2); 5947 } 5948 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type)) 5949 { 5950 result_type = void_type_node; 5951 is_glvalue = false; 5952 } 5953 else 5954 { 5955 if (complain & tf_error) 5956 { 5957 if (VOID_TYPE_P (arg2_type)) 5958 error_at (cp_expr_loc_or_loc (arg3, loc), 5959 "second operand to the conditional operator " 5960 "is of type %<void%>, but the third operand is " 5961 "neither a throw-expression nor of type %<void%>"); 5962 else 5963 error_at (cp_expr_loc_or_loc (arg2, loc), 5964 "third operand to the conditional operator " 5965 "is of type %<void%>, but the second operand is " 5966 "neither a throw-expression nor of type %<void%>"); 5967 } 5968 return error_mark_node; 5969 } 5970 5971 goto valid_operands; 5972 } 5973 /* [expr.cond] 5974 5975 Otherwise, if the second and third operand have different types, 5976 and either has (possibly cv-qualified) class type, or if both are 5977 glvalues of the same value category and the same type except for 5978 cv-qualification, an attempt is made to convert each of those operands 5979 to the type of the other. */ 5980 else if (!same_type_p (arg2_type, arg3_type) 5981 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type) 5982 || (same_type_ignoring_top_level_qualifiers_p (arg2_type, 5983 arg3_type) 5984 && glvalue_p (arg2) && glvalue_p (arg3) 5985 && lvalue_p (arg2) == lvalue_p (arg3)))) 5986 { 5987 conversion *conv2; 5988 conversion *conv3; 5989 bool converted = false; 5990 5991 conv2 = conditional_conversion (arg2, arg3, complain); 5992 conv3 = conditional_conversion (arg3, arg2, complain); 5993 5994 /* [expr.cond] 5995 5996 If both can be converted, or one can be converted but the 5997 conversion is ambiguous, the program is ill-formed. If 5998 neither can be converted, the operands are left unchanged and 5999 further checking is performed as described below. If exactly 6000 one conversion is possible, that conversion is applied to the 6001 chosen operand and the converted operand is used in place of 6002 the original operand for the remainder of this section. */ 6003 if ((conv2 && !conv2->bad_p 6004 && conv3 && !conv3->bad_p) 6005 || (conv2 && conv2->kind == ck_ambig) 6006 || (conv3 && conv3->kind == ck_ambig)) 6007 { 6008 if (complain & tf_error) 6009 { 6010 error_at (loc, "operands to %<?:%> have different types " 6011 "%qT and %qT", 6012 arg2_type, arg3_type); 6013 if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p) 6014 inform (loc, " and each type can be converted to the other"); 6015 else if (conv2 && conv2->kind == ck_ambig) 6016 convert_like (conv2, arg2, complain); 6017 else 6018 convert_like (conv3, arg3, complain); 6019 } 6020 result = error_mark_node; 6021 } 6022 else if (conv2 && !conv2->bad_p) 6023 { 6024 arg2 = convert_like (conv2, arg2, complain); 6025 arg2 = convert_from_reference (arg2); 6026 arg2_type = TREE_TYPE (arg2); 6027 /* Even if CONV2 is a valid conversion, the result of the 6028 conversion may be invalid. For example, if ARG3 has type 6029 "volatile X", and X does not have a copy constructor 6030 accepting a "volatile X&", then even if ARG2 can be 6031 converted to X, the conversion will fail. */ 6032 if (error_operand_p (arg2)) 6033 result = error_mark_node; 6034 converted = true; 6035 } 6036 else if (conv3 && !conv3->bad_p) 6037 { 6038 arg3 = convert_like (conv3, arg3, complain); 6039 arg3 = convert_from_reference (arg3); 6040 arg3_type = TREE_TYPE (arg3); 6041 if (error_operand_p (arg3)) 6042 result = error_mark_node; 6043 converted = true; 6044 } 6045 6046 if (result) 6047 return result; 6048 6049 /* If, after the conversion, both operands have class type, 6050 treat the cv-qualification of both operands as if it were the 6051 union of the cv-qualification of the operands. 6052 6053 The standard is not clear about what to do in this 6054 circumstance. For example, if the first operand has type 6055 "const X" and the second operand has a user-defined 6056 conversion to "volatile X", what is the type of the second 6057 operand after this step? Making it be "const X" (matching 6058 the first operand) seems wrong, as that discards the 6059 qualification without actually performing a copy. Leaving it 6060 as "volatile X" seems wrong as that will result in the 6061 conditional expression failing altogether, even though, 6062 according to this step, the one operand could be converted to 6063 the type of the other. */ 6064 if (converted 6065 && CLASS_TYPE_P (arg2_type) 6066 && cp_type_quals (arg2_type) != cp_type_quals (arg3_type)) 6067 arg2_type = arg3_type = 6068 cp_build_qualified_type (arg2_type, 6069 cp_type_quals (arg2_type) 6070 | cp_type_quals (arg3_type)); 6071 } 6072 6073 /* [expr.cond] 6074 6075 If the second and third operands are glvalues of the same value 6076 category and have the same type, the result is of that type and 6077 value category. */ 6078 if (((lvalue_p (arg2) && lvalue_p (arg3)) 6079 || (xvalue_p (arg2) && xvalue_p (arg3))) 6080 && same_type_p (arg2_type, arg3_type)) 6081 { 6082 result_type = arg2_type; 6083 goto valid_operands; 6084 } 6085 6086 /* [expr.cond] 6087 6088 Otherwise, the result is an rvalue. If the second and third 6089 operand do not have the same type, and either has (possibly 6090 cv-qualified) class type, overload resolution is used to 6091 determine the conversions (if any) to be applied to the operands 6092 (_over.match.oper_, _over.built_). */ 6093 is_glvalue = false; 6094 if (!same_type_p (arg2_type, arg3_type) 6095 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type))) 6096 { 6097 releasing_vec args; 6098 conversion *conv; 6099 bool any_viable_p; 6100 6101 /* Rearrange the arguments so that add_builtin_candidate only has 6102 to know about two args. In build_builtin_candidate, the 6103 arguments are unscrambled. */ 6104 args->quick_push (arg2); 6105 args->quick_push (arg3); 6106 args->quick_push (arg1); 6107 add_builtin_candidates (&candidates, 6108 COND_EXPR, 6109 NOP_EXPR, 6110 ovl_op_identifier (false, COND_EXPR), 6111 args, 6112 LOOKUP_NORMAL, complain); 6113 6114 /* [expr.cond] 6115 6116 If the overload resolution fails, the program is 6117 ill-formed. */ 6118 candidates = splice_viable (candidates, false, &any_viable_p); 6119 if (!any_viable_p) 6120 { 6121 if (complain & tf_error) 6122 error_at (loc, "operands to %<?:%> have different types %qT and %qT", 6123 arg2_type, arg3_type); 6124 return error_mark_node; 6125 } 6126 cand = tourney (candidates, complain); 6127 if (!cand) 6128 { 6129 if (complain & tf_error) 6130 { 6131 auto_diagnostic_group d; 6132 op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, false); 6133 print_z_candidates (loc, candidates); 6134 } 6135 return error_mark_node; 6136 } 6137 6138 /* [expr.cond] 6139 6140 Otherwise, the conversions thus determined are applied, and 6141 the converted operands are used in place of the original 6142 operands for the remainder of this section. */ 6143 conv = cand->convs[0]; 6144 arg1 = convert_like (conv, arg1, complain); 6145 conv = cand->convs[1]; 6146 arg2 = convert_like (conv, arg2, complain); 6147 arg2_type = TREE_TYPE (arg2); 6148 conv = cand->convs[2]; 6149 arg3 = convert_like (conv, arg3, complain); 6150 arg3_type = TREE_TYPE (arg3); 6151 } 6152 6153 /* [expr.cond] 6154 6155 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_), 6156 and function-to-pointer (_conv.func_) standard conversions are 6157 performed on the second and third operands. 6158 6159 We need to force the lvalue-to-rvalue conversion here for class types, 6160 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues 6161 that isn't wrapped with a TARGET_EXPR plays havoc with exception 6162 regions. */ 6163 6164 arg2 = force_rvalue (arg2, complain); 6165 if (!CLASS_TYPE_P (arg2_type)) 6166 arg2_type = TREE_TYPE (arg2); 6167 6168 arg3 = force_rvalue (arg3, complain); 6169 if (!CLASS_TYPE_P (arg3_type)) 6170 arg3_type = TREE_TYPE (arg3); 6171 6172 if (arg2 == error_mark_node || arg3 == error_mark_node) 6173 return error_mark_node; 6174 6175 /* [expr.cond] 6176 6177 After those conversions, one of the following shall hold: 6178 6179 --The second and third operands have the same type; the result is of 6180 that type. */ 6181 if (same_type_p (arg2_type, arg3_type)) 6182 result_type = arg2_type; 6183 /* [expr.cond] 6184 6185 --The second and third operands have arithmetic or enumeration 6186 type; the usual arithmetic conversions are performed to bring 6187 them to a common type, and the result is of that type. */ 6188 else if ((ARITHMETIC_TYPE_P (arg2_type) 6189 || UNSCOPED_ENUM_P (arg2_type)) 6190 && (ARITHMETIC_TYPE_P (arg3_type) 6191 || UNSCOPED_ENUM_P (arg3_type))) 6192 { 6193 /* A conditional expression between a floating-point 6194 type and an integer type should convert the integer type to 6195 the evaluation format of the floating-point type, with 6196 possible excess precision. */ 6197 tree eptype2 = arg2_type; 6198 tree eptype3 = arg3_type; 6199 tree eptype; 6200 if (ANY_INTEGRAL_TYPE_P (arg2_type) 6201 && (eptype = excess_precision_type (arg3_type)) != NULL_TREE) 6202 { 6203 eptype3 = eptype; 6204 if (!semantic_result_type) 6205 semantic_result_type 6206 = type_after_usual_arithmetic_conversions (arg2_type, arg3_type); 6207 } 6208 else if (ANY_INTEGRAL_TYPE_P (arg3_type) 6209 && (eptype = excess_precision_type (arg2_type)) != NULL_TREE) 6210 { 6211 eptype2 = eptype; 6212 if (!semantic_result_type) 6213 semantic_result_type 6214 = type_after_usual_arithmetic_conversions (arg2_type, arg3_type); 6215 } 6216 result_type = type_after_usual_arithmetic_conversions (eptype2, 6217 eptype3); 6218 if (result_type == error_mark_node) 6219 { 6220 tree t1 = eptype2; 6221 tree t2 = eptype3; 6222 if (TREE_CODE (t1) == COMPLEX_TYPE) 6223 t1 = TREE_TYPE (t1); 6224 if (TREE_CODE (t2) == COMPLEX_TYPE) 6225 t2 = TREE_TYPE (t2); 6226 gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1) 6227 && SCALAR_FLOAT_TYPE_P (t2) 6228 && (extended_float_type_p (t1) 6229 || extended_float_type_p (t2)) 6230 && cp_compare_floating_point_conversion_ranks 6231 (t1, t2) == 3); 6232 if (complain & tf_error) 6233 error_at (loc, "operands to %<?:%> of types %qT and %qT " 6234 "have unordered conversion rank", 6235 eptype2, eptype3); 6236 return error_mark_node; 6237 } 6238 if (semantic_result_type == error_mark_node) 6239 { 6240 tree t1 = arg2_type; 6241 tree t2 = arg3_type; 6242 if (TREE_CODE (t1) == COMPLEX_TYPE) 6243 t1 = TREE_TYPE (t1); 6244 if (TREE_CODE (t2) == COMPLEX_TYPE) 6245 t2 = TREE_TYPE (t2); 6246 gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1) 6247 && SCALAR_FLOAT_TYPE_P (t2) 6248 && (extended_float_type_p (t1) 6249 || extended_float_type_p (t2)) 6250 && cp_compare_floating_point_conversion_ranks 6251 (t1, t2) == 3); 6252 if (complain & tf_error) 6253 error_at (loc, "operands to %<?:%> of types %qT and %qT " 6254 "have unordered conversion rank", 6255 arg2_type, arg3_type); 6256 return error_mark_node; 6257 } 6258 6259 if (complain & tf_warning) 6260 do_warn_double_promotion (result_type, arg2_type, arg3_type, 6261 "implicit conversion from %qH to %qI to " 6262 "match other result of conditional", 6263 loc); 6264 6265 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE 6266 && TREE_CODE (arg3_type) == ENUMERAL_TYPE) 6267 { 6268 tree stripped_orig_arg2 = tree_strip_any_location_wrapper (orig_arg2); 6269 tree stripped_orig_arg3 = tree_strip_any_location_wrapper (orig_arg3); 6270 if (TREE_CODE (stripped_orig_arg2) == CONST_DECL 6271 && TREE_CODE (stripped_orig_arg3) == CONST_DECL 6272 && (DECL_CONTEXT (stripped_orig_arg2) 6273 == DECL_CONTEXT (stripped_orig_arg3))) 6274 /* Two enumerators from the same enumeration can have different 6275 types when the enumeration is still being defined. */; 6276 else if (complain & (cxx_dialect >= cxx26 6277 ? tf_warning_or_error : tf_warning)) 6278 emit_diagnostic (cxx_dialect >= cxx26 ? DK_PEDWARN : DK_WARNING, 6279 loc, OPT_Wenum_compare, "enumerated mismatch " 6280 "in conditional expression: %qT vs %qT", 6281 arg2_type, arg3_type); 6282 else if (cxx_dialect >= cxx26) 6283 return error_mark_node; 6284 } 6285 else if ((((complain & (cxx_dialect >= cxx26 6286 ? tf_warning_or_error : tf_warning)) 6287 && warn_deprecated_enum_float_conv) 6288 || (cxx_dialect >= cxx26 6289 && (complain & tf_warning_or_error) == 0)) 6290 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE 6291 && SCALAR_FLOAT_TYPE_P (arg3_type)) 6292 || (SCALAR_FLOAT_TYPE_P (arg2_type) 6293 && TREE_CODE (arg3_type) == ENUMERAL_TYPE))) 6294 { 6295 if (cxx_dialect >= cxx26 && (complain & tf_warning_or_error) == 0) 6296 return error_mark_node; 6297 if (cxx_dialect >= cxx26 && TREE_CODE (arg2_type) == ENUMERAL_TYPE) 6298 pedwarn (loc, OPT_Wdeprecated_enum_float_conversion, 6299 "conditional expression between enumeration type " 6300 "%qT and floating-point type %qT", arg2_type, arg3_type); 6301 else if (cxx_dialect >= cxx26) 6302 pedwarn (loc, OPT_Wdeprecated_enum_float_conversion, 6303 "conditional expression between floating-point type " 6304 "%qT and enumeration type %qT", arg2_type, arg3_type); 6305 else if (TREE_CODE (arg2_type) == ENUMERAL_TYPE) 6306 warning_at (loc, OPT_Wdeprecated_enum_float_conversion, 6307 "conditional expression between enumeration type " 6308 "%qT and floating-point type %qT is deprecated", 6309 arg2_type, arg3_type); 6310 else 6311 warning_at (loc, OPT_Wdeprecated_enum_float_conversion, 6312 "conditional expression between floating-point " 6313 "type %qT and enumeration type %qT is deprecated", 6314 arg2_type, arg3_type); 6315 } 6316 else if ((extra_warnings || warn_enum_conversion) 6317 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE 6318 && !same_type_p (arg3_type, type_promotes_to (arg2_type))) 6319 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE 6320 && !same_type_p (arg2_type, 6321 type_promotes_to (arg3_type))))) 6322 { 6323 if (complain & tf_warning) 6324 { 6325 enum opt_code opt = (warn_enum_conversion 6326 ? OPT_Wenum_conversion 6327 : OPT_Wextra); 6328 warning_at (loc, opt, "enumerated and " 6329 "non-enumerated type in conditional expression"); 6330 } 6331 } 6332 6333 arg2 = perform_implicit_conversion (result_type, arg2, complain); 6334 arg3 = perform_implicit_conversion (result_type, arg3, complain); 6335 } 6336 /* [expr.cond] 6337 6338 --The second and third operands have pointer type, or one has 6339 pointer type and the other is a null pointer constant; pointer 6340 conversions (_conv.ptr_) and qualification conversions 6341 (_conv.qual_) are performed to bring them to their composite 6342 pointer type (_expr.rel_). The result is of the composite 6343 pointer type. 6344 6345 --The second and third operands have pointer to member type, or 6346 one has pointer to member type and the other is a null pointer 6347 constant; pointer to member conversions (_conv.mem_) and 6348 qualification conversions (_conv.qual_) are performed to bring 6349 them to a common type, whose cv-qualification shall match the 6350 cv-qualification of either the second or the third operand. 6351 The result is of the common type. */ 6352 else if ((null_ptr_cst_p (arg2) 6353 && TYPE_PTR_OR_PTRMEM_P (arg3_type)) 6354 || (null_ptr_cst_p (arg3) 6355 && TYPE_PTR_OR_PTRMEM_P (arg2_type)) 6356 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type)) 6357 || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type)) 6358 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type))) 6359 { 6360 result_type = composite_pointer_type (loc, 6361 arg2_type, arg3_type, arg2, 6362 arg3, CPO_CONDITIONAL_EXPR, 6363 complain); 6364 if (result_type == error_mark_node) 6365 return error_mark_node; 6366 arg2 = perform_implicit_conversion (result_type, arg2, complain); 6367 arg3 = perform_implicit_conversion (result_type, arg3, complain); 6368 } 6369 6370 if (!result_type) 6371 { 6372 if (complain & tf_error) 6373 error_at (loc, "operands to %<?:%> have different types %qT and %qT", 6374 arg2_type, arg3_type); 6375 return error_mark_node; 6376 } 6377 6378 if (arg2 == error_mark_node || arg3 == error_mark_node) 6379 return error_mark_node; 6380 6381 valid_operands: 6382 if (processing_template_decl && is_glvalue) 6383 { 6384 /* Let lvalue_kind know this was a glvalue. */ 6385 tree arg = (result_type == arg2_type ? arg2 : arg3); 6386 result_type = cp_build_reference_type (result_type, xvalue_p (arg)); 6387 } 6388 6389 result = build3_loc (loc, COND_EXPR, result_type, arg1, arg2, arg3); 6390 6391 /* If the ARG2 and ARG3 are the same and don't have side-effects, 6392 warn here, because the COND_EXPR will be turned into ARG2. */ 6393 if (warn_duplicated_branches 6394 && (complain & tf_warning) 6395 && (arg2 == arg3 || operand_equal_p (arg2, arg3, 6396 OEP_ADDRESS_OF_SAME_FIELD))) 6397 warning_at (EXPR_LOCATION (result), OPT_Wduplicated_branches, 6398 "this condition has identical branches"); 6399 6400 /* We can't use result_type below, as fold might have returned a 6401 throw_expr. */ 6402 6403 if (!is_glvalue) 6404 { 6405 /* Expand both sides into the same slot, hopefully the target of 6406 the ?: expression. We used to check for TARGET_EXPRs here, 6407 but now we sometimes wrap them in NOP_EXPRs so the test would 6408 fail. */ 6409 if (CLASS_TYPE_P (TREE_TYPE (result))) 6410 { 6411 result = get_target_expr (result, complain); 6412 /* Tell gimplify_modify_expr_rhs not to strip this in 6413 assignment context: we want both arms to initialize 6414 the same temporary. */ 6415 TARGET_EXPR_NO_ELIDE (result) = true; 6416 } 6417 /* If this expression is an rvalue, but might be mistaken for an 6418 lvalue, we must add a NON_LVALUE_EXPR. */ 6419 result = rvalue (result); 6420 if (semantic_result_type) 6421 result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, 6422 result); 6423 } 6424 else 6425 { 6426 result = force_paren_expr (result); 6427 gcc_assert (semantic_result_type == NULL_TREE); 6428 } 6429 6430 return result; 6431 } 6432 6433 /* OPERAND is an operand to an expression. Perform necessary steps 6434 required before using it. If OPERAND is NULL_TREE, NULL_TREE is 6435 returned. */ 6436 6437 static tree 6438 prep_operand (tree operand) 6439 { 6440 if (operand) 6441 { 6442 if (CLASS_TYPE_P (TREE_TYPE (operand)) 6443 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand))) 6444 /* Make sure the template type is instantiated now. */ 6445 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand))); 6446 } 6447 6448 return operand; 6449 } 6450 6451 /* True iff CONV represents a conversion sequence which no other can be better 6452 than under [over.ics.rank]: in other words, a "conversion" to the exact same 6453 type (including binding to a reference to the same type). This is stronger 6454 than the standard's "identity" category, which also includes reference 6455 bindings that add cv-qualifiers or change rvalueness. */ 6456 6457 static bool 6458 perfect_conversion_p (conversion *conv) 6459 { 6460 if (CONVERSION_RANK (conv) != cr_identity) 6461 return false; 6462 if (conv->kind == ck_ref_bind) 6463 { 6464 if (!conv->rvaluedness_matches_p) 6465 return false; 6466 if (!same_type_p (TREE_TYPE (conv->type), 6467 next_conversion (conv)->type)) 6468 return false; 6469 } 6470 if (conv->check_narrowing) 6471 /* Brace elision is imperfect. */ 6472 return false; 6473 return true; 6474 } 6475 6476 /* True if CAND represents a perfect match, i.e. all perfect conversions, so no 6477 other candidate can be a better match. Since the template/non-template 6478 tiebreaker comes immediately after the conversion comparison in 6479 [over.match.best], a perfect non-template candidate is better than all 6480 templates. */ 6481 6482 static bool 6483 perfect_candidate_p (z_candidate *cand) 6484 { 6485 if (cand->viable < 1) 6486 return false; 6487 /* CWG1402 makes an implicitly deleted move op worse than other 6488 candidates. */ 6489 if (DECL_DELETED_FN (cand->fn) && DECL_DEFAULTED_FN (cand->fn) 6490 && move_fn_p (cand->fn)) 6491 return false; 6492 int len = cand->num_convs; 6493 for (int i = 0; i < len; ++i) 6494 if (!perfect_conversion_p (cand->convs[i])) 6495 return false; 6496 if (conversion *conv = cand->second_conv) 6497 if (!perfect_conversion_p (conv)) 6498 return false; 6499 return true; 6500 } 6501 6502 /* True iff one of CAND's argument conversions is missing. */ 6503 6504 static bool 6505 missing_conversion_p (const z_candidate *cand) 6506 { 6507 for (unsigned i = 0; i < cand->num_convs; ++i) 6508 { 6509 conversion *conv = cand->convs[i]; 6510 if (!conv) 6511 return true; 6512 if (conv->kind == ck_deferred_bad) 6513 { 6514 /* We don't know whether this conversion is outright invalid or 6515 just bad, so conservatively assume it's missing. */ 6516 gcc_checking_assert (conv->bad_p); 6517 return true; 6518 } 6519 } 6520 return false; 6521 } 6522 6523 /* Add each of the viable functions in FNS (a FUNCTION_DECL or 6524 OVERLOAD) to the CANDIDATES, returning an updated list of 6525 CANDIDATES. The ARGS are the arguments provided to the call; 6526 if FIRST_ARG is non-null it is the implicit object argument, 6527 otherwise the first element of ARGS is used if needed. The 6528 EXPLICIT_TARGS are explicit template arguments provided. 6529 TEMPLATE_ONLY is true if only template functions should be 6530 considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for 6531 add_function_candidate. */ 6532 6533 static void 6534 add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args, 6535 tree return_type, 6536 tree explicit_targs, bool template_only, 6537 tree conversion_path, tree access_path, 6538 int flags, 6539 struct z_candidate **candidates, 6540 tsubst_flags_t complain) 6541 { 6542 tree ctype; 6543 const vec<tree, va_gc> *non_static_args; 6544 bool check_list_ctor = false; 6545 bool check_converting = false; 6546 unification_kind_t strict; 6547 tree ne_fns = NULL_TREE; 6548 6549 if (!fns) 6550 return; 6551 6552 /* Precalculate special handling of constructors and conversion ops. */ 6553 tree fn = OVL_FIRST (fns); 6554 if (DECL_CONV_FN_P (fn)) 6555 { 6556 check_list_ctor = false; 6557 check_converting = (flags & LOOKUP_ONLYCONVERTING) != 0; 6558 if (flags & LOOKUP_NO_CONVERSION) 6559 /* We're doing return_type(x). */ 6560 strict = DEDUCE_CONV; 6561 else 6562 /* We're doing x.operator return_type(). */ 6563 strict = DEDUCE_EXACT; 6564 /* [over.match.funcs] For conversion functions, the function 6565 is considered to be a member of the class of the implicit 6566 object argument for the purpose of defining the type of 6567 the implicit object parameter. */ 6568 ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg)); 6569 } 6570 else 6571 { 6572 if (DECL_CONSTRUCTOR_P (fn)) 6573 { 6574 check_list_ctor = (flags & LOOKUP_LIST_ONLY) != 0; 6575 /* For list-initialization we consider explicit constructors 6576 and complain if one is chosen. */ 6577 check_converting 6578 = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR)) 6579 == LOOKUP_ONLYCONVERTING); 6580 } 6581 strict = DEDUCE_CALL; 6582 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE; 6583 } 6584 6585 /* P2468: Check if operator== is a rewrite target with first operand 6586 (*args)[0]; for now just do the lookups. */ 6587 if ((flags & (LOOKUP_REWRITTEN | LOOKUP_REVERSED)) 6588 && DECL_OVERLOADED_OPERATOR_IS (fn, EQ_EXPR)) 6589 { 6590 tree ne_name = ovl_op_identifier (false, NE_EXPR); 6591 if (DECL_CLASS_SCOPE_P (fn)) 6592 { 6593 ne_fns = lookup_fnfields (TREE_TYPE ((*args)[0]), ne_name, 6594 1, tf_none); 6595 if (ne_fns == error_mark_node || ne_fns == NULL_TREE) 6596 ne_fns = NULL_TREE; 6597 else 6598 ne_fns = BASELINK_FUNCTIONS (ne_fns); 6599 } 6600 else 6601 { 6602 tree context = decl_namespace_context (fn); 6603 ne_fns = lookup_qualified_name (context, ne_name, LOOK_want::NORMAL, 6604 /*complain*/false); 6605 if (ne_fns == error_mark_node 6606 || !is_overloaded_fn (ne_fns)) 6607 ne_fns = NULL_TREE; 6608 } 6609 } 6610 6611 if (first_arg) 6612 non_static_args = args; 6613 else 6614 /* Delay creating the implicit this parameter until it is needed. */ 6615 non_static_args = NULL; 6616 6617 bool seen_strictly_viable = any_strictly_viable (*candidates); 6618 /* If there's a non-template perfect match, we don't need to consider 6619 templates. So check non-templates first. This optimization is only 6620 really needed for the defaulted copy constructor of tuple and the like 6621 (96926), but it seems like we might as well enable it more generally. */ 6622 bool seen_perfect = false; 6623 enum { templates, non_templates, either } which = either; 6624 if (template_only) 6625 which = templates; 6626 else /*if (flags & LOOKUP_DEFAULTED)*/ 6627 which = non_templates; 6628 6629 /* Template candidates that we'll potentially ignore if the 6630 perfect candidate optimization succeeds. */ 6631 z_candidate *ignored_template_cands = nullptr; 6632 6633 /* During overload resolution, we first consider each function under the 6634 assumption that we'll eventually find a strictly viable candidate. 6635 This allows us to circumvent our defacto behavior when checking 6636 argument conversions and shortcut consideration of the candidate 6637 upon encountering the first bad conversion. If this assumption 6638 turns out to be false, and all candidates end up being non-strictly 6639 viable, then we reconsider such candidates under the defacto behavior. 6640 This trick is important for pruning member function overloads according 6641 to their const/ref-qualifiers (since all 'this' conversions are at 6642 worst bad) without breaking -fpermissive. */ 6643 z_candidate *bad_cands = nullptr; 6644 bool shortcut_bad_convs = true; 6645 6646 again: 6647 for (tree fn : lkp_range (fns)) 6648 { 6649 if (which == templates && TREE_CODE (fn) != TEMPLATE_DECL) 6650 { 6651 if (template_only) 6652 add_ignored_candidate (candidates, fn); 6653 continue; 6654 } 6655 if (which == non_templates && TREE_CODE (fn) == TEMPLATE_DECL) 6656 { 6657 add_ignored_candidate (&ignored_template_cands, fn); 6658 continue; 6659 } 6660 if ((check_converting && DECL_NONCONVERTING_P (fn)) 6661 || (check_list_ctor && !is_list_ctor (fn))) 6662 { 6663 add_ignored_candidate (candidates, fn); 6664 continue; 6665 } 6666 6667 tree fn_first_arg = NULL_TREE; 6668 const vec<tree, va_gc> *fn_args = args; 6669 6670 if (DECL_OBJECT_MEMBER_FUNCTION_P (fn)) 6671 { 6672 /* Figure out where the object arg comes from. If this 6673 function is a non-static member and we didn't get an 6674 implicit object argument, move it out of args. */ 6675 if (first_arg == NULL_TREE) 6676 { 6677 unsigned int ix; 6678 tree arg; 6679 vec<tree, va_gc> *tempvec; 6680 vec_alloc (tempvec, args->length () - 1); 6681 for (ix = 1; args->iterate (ix, &arg); ++ix) 6682 tempvec->quick_push (arg); 6683 non_static_args = tempvec; 6684 first_arg = (*args)[0]; 6685 } 6686 6687 fn_first_arg = first_arg; 6688 fn_args = non_static_args; 6689 } 6690 6691 /* Don't bother reversing an operator with two identical parameters. */ 6692 else if (vec_safe_length (args) == 2 && (flags & LOOKUP_REVERSED)) 6693 { 6694 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn)); 6695 if (same_type_p (TREE_VALUE (parmlist), 6696 TREE_VALUE (TREE_CHAIN (parmlist)))) 6697 continue; 6698 } 6699 6700 /* When considering reversed operator==, if there's a corresponding 6701 operator!= in the same scope, it's not a rewrite target. */ 6702 if (ne_fns) 6703 { 6704 bool found = false; 6705 for (lkp_iterator ne (ne_fns); !found && ne; ++ne) 6706 if (0 && !ne.using_p () 6707 && DECL_NAMESPACE_SCOPE_P (fn) 6708 && DECL_CONTEXT (*ne) != DECL_CONTEXT (fn)) 6709 /* ??? This kludge excludes inline namespace members for the H 6710 test in spaceship-eq15.C, but I don't see why we would want 6711 that behavior. Asked Core 2022-11-04. Disabling for now. */; 6712 else if (fns_correspond (fn, *ne)) 6713 { 6714 found = true; 6715 break; 6716 } 6717 if (found) 6718 continue; 6719 } 6720 6721 if (TREE_CODE (fn) == TEMPLATE_DECL) 6722 add_template_candidate (candidates, 6723 fn, 6724 ctype, 6725 explicit_targs, 6726 fn_first_arg, 6727 fn_args, 6728 return_type, 6729 access_path, 6730 conversion_path, 6731 flags, 6732 strict, 6733 shortcut_bad_convs, 6734 complain); 6735 else 6736 { 6737 add_function_candidate (candidates, 6738 fn, 6739 ctype, 6740 fn_first_arg, 6741 fn_args, 6742 access_path, 6743 conversion_path, 6744 flags, 6745 NULL, 6746 shortcut_bad_convs, 6747 complain); 6748 if (perfect_candidate_p (*candidates)) 6749 seen_perfect = true; 6750 } 6751 6752 z_candidate *cand = *candidates; 6753 if (cand->viable == 1) 6754 seen_strictly_viable = true; 6755 6756 if (cand->viable == -1 6757 && shortcut_bad_convs 6758 && (missing_conversion_p (cand) 6759 || TREE_CODE (cand->fn) == TEMPLATE_DECL)) 6760 { 6761 /* This candidate has been tentatively marked non-strictly viable, 6762 and we didn't compute all argument conversions for it (having 6763 stopped at the first bad conversion). Move it to BAD_CANDS to 6764 to fully reconsider later if we don't find any strictly viable 6765 candidates. */ 6766 if (complain & (tf_error | tf_conv)) 6767 { 6768 *candidates = cand->next; 6769 cand->next = bad_cands; 6770 bad_cands = cand; 6771 } 6772 else 6773 /* But if we're in a SFINAE context, just mark this candidate as 6774 unviable outright and avoid potentially reconsidering it. 6775 This is safe to do because in a SFINAE context, performing a bad 6776 conversion is always an error (even with -fpermissive), so a 6777 non-strictly viable candidate is effectively unviable anyway. */ 6778 cand->viable = 0; 6779 } 6780 } 6781 if (which == non_templates && !seen_perfect) 6782 { 6783 which = templates; 6784 ignored_template_cands = nullptr; 6785 goto again; 6786 } 6787 else if (which == templates 6788 && !seen_strictly_viable 6789 && shortcut_bad_convs 6790 && bad_cands) 6791 { 6792 /* None of the candidates are strictly viable, so consider again those 6793 functions in BAD_CANDS, this time without shortcutting bad conversions 6794 so that all their argument conversions are computed. */ 6795 which = either; 6796 fns = NULL_TREE; 6797 for (z_candidate *cand = bad_cands; cand; cand = cand->next) 6798 { 6799 tree fn = cand->fn; 6800 if (tree ti = cand->template_decl) 6801 fn = TI_TEMPLATE (ti); 6802 fns = ovl_make (fn, fns); 6803 } 6804 shortcut_bad_convs = false; 6805 bad_cands = nullptr; 6806 goto again; 6807 } 6808 6809 if (complain & tf_error) 6810 { 6811 /* Remember any omitted candidates; we may want to print all candidates 6812 as part of overload resolution failure diagnostics. */ 6813 for (z_candidate *omitted_cands : { ignored_template_cands, bad_cands }) 6814 { 6815 z_candidate **omitted_cands_tail = &omitted_cands; 6816 while (*omitted_cands_tail) 6817 omitted_cands_tail = &(*omitted_cands_tail)->next; 6818 *omitted_cands_tail = *candidates; 6819 *candidates = omitted_cands; 6820 } 6821 } 6822 } 6823 6824 /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first, 6825 -1 if the RHS is evaluated first, or 0 if the order is unspecified. */ 6826 6827 static int 6828 op_is_ordered (tree_code code) 6829 { 6830 switch (code) 6831 { 6832 // 5. b @= a 6833 case MODIFY_EXPR: 6834 return (flag_strong_eval_order > 1 ? -1 : 0); 6835 6836 // 6. a[b] 6837 case ARRAY_REF: 6838 return (flag_strong_eval_order > 1 ? 1 : 0); 6839 6840 // 1. a.b 6841 // Not overloadable (yet). 6842 // 2. a->b 6843 // Only one argument. 6844 // 3. a->*b 6845 case MEMBER_REF: 6846 // 7. a << b 6847 case LSHIFT_EXPR: 6848 // 8. a >> b 6849 case RSHIFT_EXPR: 6850 // a && b 6851 // Predates P0145R3. 6852 case TRUTH_ANDIF_EXPR: 6853 // a || b 6854 // Predates P0145R3. 6855 case TRUTH_ORIF_EXPR: 6856 // a , b 6857 // Predates P0145R3. 6858 case COMPOUND_EXPR: 6859 return (flag_strong_eval_order ? 1 : 0); 6860 6861 default: 6862 return 0; 6863 } 6864 } 6865 6866 /* Subroutine of build_new_op: Add to CANDIDATES all candidates for the 6867 operator indicated by CODE/CODE2. This function calls itself recursively to 6868 handle C++20 rewritten comparison operator candidates. Returns NULL_TREE 6869 upon success, and error_mark_node if something went wrong that prevented 6870 us from performing overload resolution (e.g. ambiguous member name lookup). 6871 6872 LOOKUPS, if non-NULL, is the set of pertinent namespace-scope operator 6873 overloads to consider. This parameter is used when instantiating a 6874 dependent operator expression and has the same structure as 6875 DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS. */ 6876 6877 static tree 6878 add_operator_candidates (z_candidate **candidates, 6879 tree_code code, tree_code code2, 6880 vec<tree, va_gc> *arglist, tree lookups, 6881 int flags, tsubst_flags_t complain) 6882 { 6883 z_candidate *start_candidates = *candidates; 6884 bool ismodop = code2 != ERROR_MARK; 6885 tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code); 6886 6887 /* LOOKUP_REWRITTEN is set when we're looking for the == or <=> operator to 6888 rewrite from, and also when we're looking for the e.g. < operator to use 6889 on the result of <=>. In the latter case, we don't want the flag set in 6890 the candidate, we just want to suppress looking for rewrites. */ 6891 bool rewritten = (flags & LOOKUP_REWRITTEN); 6892 if (rewritten && code != EQ_EXPR && code != SPACESHIP_EXPR) 6893 flags &= ~LOOKUP_REWRITTEN; 6894 6895 bool memonly = false; 6896 switch (code) 6897 { 6898 /* =, ->, [], () must be non-static member functions. */ 6899 case MODIFY_EXPR: 6900 if (code2 != NOP_EXPR) 6901 break; 6902 /* FALLTHRU */ 6903 case COMPONENT_REF: 6904 case ARRAY_REF: 6905 memonly = true; 6906 break; 6907 6908 default: 6909 break; 6910 } 6911 6912 /* Add namespace-scope operators to the list of functions to 6913 consider. */ 6914 if (!memonly) 6915 { 6916 tree fns; 6917 if (!lookups) 6918 fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE); 6919 /* If LOOKUPS is non-NULL, then we're instantiating a dependent operator 6920 expression, and LOOKUPS is the result of stage 1 name lookup. */ 6921 else if (tree found = purpose_member (fnname, lookups)) 6922 fns = TREE_VALUE (found); 6923 else 6924 fns = NULL_TREE; 6925 fns = lookup_arg_dependent (fnname, fns, arglist); 6926 add_candidates (fns, NULL_TREE, arglist, NULL_TREE, 6927 NULL_TREE, false, NULL_TREE, NULL_TREE, 6928 flags, candidates, complain); 6929 } 6930 6931 /* Add class-member operators to the candidate set. */ 6932 tree arg1_type = TREE_TYPE ((*arglist)[0]); 6933 unsigned nargs = arglist->length () > 1 ? 2 : 1; 6934 tree arg2_type = nargs > 1 ? TREE_TYPE ((*arglist)[1]) : NULL_TREE; 6935 if (CLASS_TYPE_P (arg1_type)) 6936 { 6937 tree fns = lookup_fnfields (arg1_type, fnname, 1, complain); 6938 if (fns == error_mark_node) 6939 return error_mark_node; 6940 if (fns) 6941 { 6942 if (code == ARRAY_REF) 6943 { 6944 vec<tree,va_gc> *restlist = make_tree_vector (); 6945 for (unsigned i = 1; i < nargs; ++i) 6946 vec_safe_push (restlist, (*arglist)[i]); 6947 z_candidate *save_cand = *candidates; 6948 add_candidates (BASELINK_FUNCTIONS (fns), 6949 (*arglist)[0], restlist, NULL_TREE, 6950 NULL_TREE, false, 6951 BASELINK_BINFO (fns), 6952 BASELINK_ACCESS_BINFO (fns), 6953 flags, candidates, complain); 6954 /* Release the vec if we didn't add a candidate that uses it. */ 6955 for (z_candidate *c = *candidates; c != save_cand; c = c->next) 6956 if (c->args == restlist) 6957 { 6958 restlist = NULL; 6959 break; 6960 } 6961 release_tree_vector (restlist); 6962 } 6963 else 6964 add_candidates (BASELINK_FUNCTIONS (fns), 6965 NULL_TREE, arglist, NULL_TREE, 6966 NULL_TREE, false, 6967 BASELINK_BINFO (fns), 6968 BASELINK_ACCESS_BINFO (fns), 6969 flags, candidates, complain); 6970 } 6971 } 6972 /* Per [over.match.oper]3.2, if no operand has a class type, then 6973 only non-member functions that have type T1 or reference to 6974 cv-qualified-opt T1 for the first argument, if the first argument 6975 has an enumeration type, or T2 or reference to cv-qualified-opt 6976 T2 for the second argument, if the second argument has an 6977 enumeration type. Filter out those that don't match. */ 6978 else if (! arg2_type || ! CLASS_TYPE_P (arg2_type)) 6979 { 6980 struct z_candidate **candp, **next; 6981 6982 for (candp = candidates; *candp != start_candidates; candp = next) 6983 { 6984 unsigned i; 6985 z_candidate *cand = *candp; 6986 next = &cand->next; 6987 6988 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn)); 6989 6990 for (i = 0; i < nargs; ++i) 6991 { 6992 tree parmtype = TREE_VALUE (parmlist); 6993 tree argtype = unlowered_expr_type ((*arglist)[i]); 6994 6995 if (TYPE_REF_P (parmtype)) 6996 parmtype = TREE_TYPE (parmtype); 6997 if (TREE_CODE (argtype) == ENUMERAL_TYPE 6998 && (same_type_ignoring_top_level_qualifiers_p 6999 (argtype, parmtype))) 7000 break; 7001 7002 parmlist = TREE_CHAIN (parmlist); 7003 } 7004 7005 /* No argument has an appropriate type, so remove this 7006 candidate function from the list. */ 7007 if (i == nargs) 7008 { 7009 *candp = cand->next; 7010 next = candp; 7011 } 7012 } 7013 } 7014 7015 if (!rewritten) 7016 { 7017 /* The standard says to rewrite built-in candidates, too, 7018 but there's no point. */ 7019 add_builtin_candidates (candidates, code, code2, fnname, arglist, 7020 flags, complain); 7021 7022 /* Maybe add C++20 rewritten comparison candidates. */ 7023 tree_code rewrite_code = ERROR_MARK; 7024 if (cxx_dialect >= cxx20 7025 && nargs == 2 7026 && (OVERLOAD_TYPE_P (arg1_type) || OVERLOAD_TYPE_P (arg2_type))) 7027 switch (code) 7028 { 7029 case LT_EXPR: 7030 case LE_EXPR: 7031 case GT_EXPR: 7032 case GE_EXPR: 7033 case SPACESHIP_EXPR: 7034 rewrite_code = SPACESHIP_EXPR; 7035 break; 7036 7037 case NE_EXPR: 7038 case EQ_EXPR: 7039 rewrite_code = EQ_EXPR; 7040 break; 7041 7042 default:; 7043 } 7044 7045 if (rewrite_code) 7046 { 7047 tree r; 7048 flags |= LOOKUP_REWRITTEN; 7049 if (rewrite_code != code) 7050 { 7051 /* Add rewritten candidates in same order. */ 7052 r = add_operator_candidates (candidates, rewrite_code, ERROR_MARK, 7053 arglist, lookups, flags, complain); 7054 if (r == error_mark_node) 7055 return error_mark_node; 7056 } 7057 7058 z_candidate *save_cand = *candidates; 7059 7060 /* Add rewritten candidates in reverse order. */ 7061 flags |= LOOKUP_REVERSED; 7062 vec<tree,va_gc> *revlist = make_tree_vector (); 7063 revlist->quick_push ((*arglist)[1]); 7064 revlist->quick_push ((*arglist)[0]); 7065 r = add_operator_candidates (candidates, rewrite_code, ERROR_MARK, 7066 revlist, lookups, flags, complain); 7067 if (r == error_mark_node) 7068 return error_mark_node; 7069 7070 /* Release the vec if we didn't add a candidate that uses it. */ 7071 for (z_candidate *c = *candidates; c != save_cand; c = c->next) 7072 if (c->args == revlist) 7073 { 7074 revlist = NULL; 7075 break; 7076 } 7077 release_tree_vector (revlist); 7078 } 7079 } 7080 7081 return NULL_TREE; 7082 } 7083 7084 tree 7085 build_new_op (const op_location_t &loc, enum tree_code code, int flags, 7086 tree arg1, tree arg2, tree arg3, tree lookups, 7087 tree *overload, tsubst_flags_t complain) 7088 { 7089 struct z_candidate *candidates = 0, *cand; 7090 releasing_vec arglist; 7091 tree result = NULL_TREE; 7092 bool result_valid_p = false; 7093 enum tree_code code2 = ERROR_MARK; 7094 enum tree_code code_orig_arg1 = ERROR_MARK; 7095 enum tree_code code_orig_arg2 = ERROR_MARK; 7096 bool strict_p; 7097 bool any_viable_p; 7098 7099 auto_cond_timevar tv (TV_OVERLOAD); 7100 7101 if (error_operand_p (arg1) 7102 || error_operand_p (arg2) 7103 || error_operand_p (arg3)) 7104 return error_mark_node; 7105 7106 conversion_obstack_sentinel cos; 7107 7108 bool ismodop = code == MODIFY_EXPR; 7109 if (ismodop) 7110 { 7111 code2 = TREE_CODE (arg3); 7112 arg3 = NULL_TREE; 7113 } 7114 7115 tree arg1_type = unlowered_expr_type (arg1); 7116 tree arg2_type = arg2 ? unlowered_expr_type (arg2) : NULL_TREE; 7117 7118 arg1 = prep_operand (arg1); 7119 7120 switch (code) 7121 { 7122 case NEW_EXPR: 7123 case VEC_NEW_EXPR: 7124 case VEC_DELETE_EXPR: 7125 case DELETE_EXPR: 7126 /* Use build_operator_new_call and build_op_delete_call instead. */ 7127 gcc_unreachable (); 7128 7129 case CALL_EXPR: 7130 /* Use build_op_call instead. */ 7131 gcc_unreachable (); 7132 7133 case TRUTH_ORIF_EXPR: 7134 case TRUTH_ANDIF_EXPR: 7135 case TRUTH_AND_EXPR: 7136 case TRUTH_OR_EXPR: 7137 /* These are saved for the sake of warn_logical_operator. */ 7138 code_orig_arg1 = TREE_CODE (arg1); 7139 code_orig_arg2 = TREE_CODE (arg2); 7140 break; 7141 case GT_EXPR: 7142 case LT_EXPR: 7143 case GE_EXPR: 7144 case LE_EXPR: 7145 case EQ_EXPR: 7146 case NE_EXPR: 7147 /* These are saved for the sake of maybe_warn_bool_compare. */ 7148 code_orig_arg1 = TREE_CODE (arg1_type); 7149 code_orig_arg2 = TREE_CODE (arg2_type); 7150 break; 7151 7152 default: 7153 break; 7154 } 7155 7156 arg2 = prep_operand (arg2); 7157 arg3 = prep_operand (arg3); 7158 7159 if (code == COND_EXPR) 7160 /* Use build_conditional_expr instead. */ 7161 gcc_unreachable (); 7162 else if (! OVERLOAD_TYPE_P (arg1_type) 7163 && (! arg2 || ! OVERLOAD_TYPE_P (arg2_type))) 7164 goto builtin; 7165 7166 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR) 7167 { 7168 arg2 = integer_zero_node; 7169 arg2_type = integer_type_node; 7170 } 7171 7172 arglist->quick_push (arg1); 7173 if (arg2 != NULL_TREE) 7174 arglist->quick_push (arg2); 7175 if (arg3 != NULL_TREE) 7176 arglist->quick_push (arg3); 7177 7178 result = add_operator_candidates (&candidates, code, code2, arglist, 7179 lookups, flags, complain); 7180 if (result == error_mark_node) 7181 return error_mark_node; 7182 7183 switch (code) 7184 { 7185 case COMPOUND_EXPR: 7186 case ADDR_EXPR: 7187 /* For these, the built-in candidates set is empty 7188 [over.match.oper]/3. We don't want non-strict matches 7189 because exact matches are always possible with built-in 7190 operators. The built-in candidate set for COMPONENT_REF 7191 would be empty too, but since there are no such built-in 7192 operators, we accept non-strict matches for them. */ 7193 strict_p = true; 7194 break; 7195 7196 default: 7197 strict_p = false; 7198 break; 7199 } 7200 7201 candidates = splice_viable (candidates, strict_p, &any_viable_p); 7202 if (!any_viable_p) 7203 { 7204 switch (code) 7205 { 7206 case POSTINCREMENT_EXPR: 7207 case POSTDECREMENT_EXPR: 7208 /* Don't try anything fancy if we're not allowed to produce 7209 errors. */ 7210 if (!(complain & tf_error)) 7211 return error_mark_node; 7212 7213 /* Look for an `operator++ (int)'. Pre-1985 C++ didn't 7214 distinguish between prefix and postfix ++ and 7215 operator++() was used for both, so we allow this with 7216 -fpermissive. */ 7217 else 7218 { 7219 tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code); 7220 const char *msg = (flag_permissive) 7221 ? G_("no %<%D(int)%> declared for postfix %qs," 7222 " trying prefix operator instead") 7223 : G_("no %<%D(int)%> declared for postfix %qs"); 7224 permerror (loc, msg, fnname, OVL_OP_INFO (false, code)->name); 7225 } 7226 7227 if (!flag_permissive) 7228 return error_mark_node; 7229 7230 if (code == POSTINCREMENT_EXPR) 7231 code = PREINCREMENT_EXPR; 7232 else 7233 code = PREDECREMENT_EXPR; 7234 result = build_new_op (loc, code, flags, arg1, NULL_TREE, 7235 NULL_TREE, lookups, overload, complain); 7236 break; 7237 7238 /* The caller will deal with these. */ 7239 case ADDR_EXPR: 7240 case COMPOUND_EXPR: 7241 case COMPONENT_REF: 7242 case CO_AWAIT_EXPR: 7243 result = NULL_TREE; 7244 result_valid_p = true; 7245 break; 7246 7247 default: 7248 if (complain & tf_error) 7249 { 7250 /* If one of the arguments of the operator represents 7251 an invalid use of member function pointer, try to report 7252 a meaningful error ... */ 7253 if (invalid_nonstatic_memfn_p (loc, arg1, tf_error) 7254 || invalid_nonstatic_memfn_p (loc, arg2, tf_error) 7255 || invalid_nonstatic_memfn_p (loc, arg3, tf_error)) 7256 /* We displayed the error message. */; 7257 else 7258 { 7259 /* ... Otherwise, report the more generic 7260 "no matching operator found" error */ 7261 auto_diagnostic_group d; 7262 op_error (loc, code, code2, arg1, arg2, arg3, false); 7263 print_z_candidates (loc, candidates); 7264 } 7265 } 7266 result = error_mark_node; 7267 break; 7268 } 7269 } 7270 else 7271 { 7272 cand = tourney (candidates, complain); 7273 if (cand == 0) 7274 { 7275 if (complain & tf_error) 7276 { 7277 auto_diagnostic_group d; 7278 op_error (loc, code, code2, arg1, arg2, arg3, true); 7279 print_z_candidates (loc, candidates); 7280 } 7281 result = error_mark_node; 7282 if (overload) 7283 *overload = error_mark_node; 7284 } 7285 else if (TREE_CODE (cand->fn) == FUNCTION_DECL) 7286 { 7287 if (overload) 7288 *overload = cand->fn; 7289 7290 if (resolve_args (arglist, complain) == NULL) 7291 result = error_mark_node; 7292 else 7293 { 7294 tsubst_flags_t ocomplain = complain; 7295 if (cand->rewritten ()) 7296 /* We'll wrap this call in another one. */ 7297 ocomplain &= ~tf_decltype; 7298 if (cand->reversed ()) 7299 { 7300 /* We swapped these in add_candidate, swap them back now. */ 7301 std::swap (cand->convs[0], cand->convs[1]); 7302 if (cand->fn == current_function_decl) 7303 warning_at (loc, 0, "in C++20 this comparison calls the " 7304 "current function recursively with reversed " 7305 "arguments"); 7306 } 7307 result = build_over_call (cand, LOOKUP_NORMAL, ocomplain); 7308 } 7309 7310 if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn)) 7311 /* There won't be a CALL_EXPR. */; 7312 else if (result && result != error_mark_node) 7313 { 7314 tree call = extract_call_expr (result); 7315 CALL_EXPR_OPERATOR_SYNTAX (call) = true; 7316 7317 /* Specify evaluation order as per P0145R2. */ 7318 CALL_EXPR_ORDERED_ARGS (call) = false; 7319 switch (op_is_ordered (code)) 7320 { 7321 case -1: 7322 CALL_EXPR_REVERSE_ARGS (call) = true; 7323 break; 7324 7325 case 1: 7326 CALL_EXPR_ORDERED_ARGS (call) = true; 7327 break; 7328 7329 default: 7330 break; 7331 } 7332 } 7333 7334 /* If this was a C++20 rewritten comparison, adjust the result. */ 7335 if (cand->rewritten ()) 7336 { 7337 /* FIXME build_min_non_dep_op_overload can't handle rewrites. */ 7338 if (overload) 7339 *overload = NULL_TREE; 7340 switch (code) 7341 { 7342 case EQ_EXPR: 7343 gcc_checking_assert (cand->reversed ()); 7344 gcc_fallthrough (); 7345 case NE_EXPR: 7346 if (result == error_mark_node) 7347 ; 7348 /* If a rewritten operator== candidate is selected by 7349 overload resolution for an operator @, its return type 7350 shall be cv bool.... */ 7351 else if (TREE_CODE (TREE_TYPE (result)) != BOOLEAN_TYPE) 7352 { 7353 if (complain & tf_error) 7354 { 7355 auto_diagnostic_group d; 7356 error_at (loc, "return type of %qD is not %qs", 7357 cand->fn, "bool"); 7358 inform (loc, "used as rewritten candidate for " 7359 "comparison of %qT and %qT", 7360 arg1_type, arg2_type); 7361 } 7362 result = error_mark_node; 7363 } 7364 else if (code == NE_EXPR) 7365 /* !(y == x) or !(x == y) */ 7366 result = build1_loc (loc, TRUTH_NOT_EXPR, 7367 boolean_type_node, result); 7368 break; 7369 7370 /* If a rewritten operator<=> candidate is selected by 7371 overload resolution for an operator @, x @ y is 7372 interpreted as 0 @ (y <=> x) if the selected candidate is 7373 a synthesized candidate with reversed order of parameters, 7374 or (x <=> y) @ 0 otherwise, using the selected rewritten 7375 operator<=> candidate. */ 7376 case SPACESHIP_EXPR: 7377 if (!cand->reversed ()) 7378 /* We're in the build_new_op call below for an outer 7379 reversed call; we don't need to do anything more. */ 7380 break; 7381 gcc_fallthrough (); 7382 case LT_EXPR: 7383 case LE_EXPR: 7384 case GT_EXPR: 7385 case GE_EXPR: 7386 { 7387 tree lhs = result; 7388 tree rhs = integer_zero_node; 7389 if (cand->reversed ()) 7390 std::swap (lhs, rhs); 7391 warning_sentinel ws (warn_zero_as_null_pointer_constant); 7392 result = build_new_op (loc, code, 7393 LOOKUP_NORMAL|LOOKUP_REWRITTEN, 7394 lhs, rhs, NULL_TREE, lookups, 7395 NULL, complain); 7396 } 7397 break; 7398 7399 default: 7400 gcc_unreachable (); 7401 } 7402 } 7403 7404 /* In an expression of the form `a[]' where cand->fn 7405 which is operator[] turns out to be a static member function, 7406 `a' is none-the-less evaluated. */ 7407 if (code == ARRAY_REF) 7408 result = keep_unused_object_arg (result, arg1, cand->fn); 7409 } 7410 else 7411 { 7412 /* Give any warnings we noticed during overload resolution. */ 7413 if (cand->warnings && (complain & tf_warning)) 7414 { 7415 struct candidate_warning *w; 7416 for (w = cand->warnings; w; w = w->next) 7417 joust (cand, w->loser, 1, complain); 7418 } 7419 7420 /* Check for comparison of different enum types. */ 7421 switch (code) 7422 { 7423 case GT_EXPR: 7424 case LT_EXPR: 7425 case GE_EXPR: 7426 case LE_EXPR: 7427 case EQ_EXPR: 7428 case NE_EXPR: 7429 if (TREE_CODE (arg1_type) == ENUMERAL_TYPE 7430 && TREE_CODE (arg2_type) == ENUMERAL_TYPE 7431 && (TYPE_MAIN_VARIANT (arg1_type) 7432 != TYPE_MAIN_VARIANT (arg2_type))) 7433 { 7434 if (cxx_dialect >= cxx26 7435 && (complain & tf_warning_or_error) == 0) 7436 result = error_mark_node; 7437 else if (cxx_dialect >= cxx26 || (complain & tf_warning)) 7438 emit_diagnostic (cxx_dialect >= cxx26 7439 ? DK_PEDWARN : DK_WARNING, 7440 loc, OPT_Wenum_compare, 7441 "comparison between %q#T and %q#T", 7442 arg1_type, arg2_type); 7443 } 7444 break; 7445 default: 7446 break; 7447 } 7448 7449 /* "If a built-in candidate is selected by overload resolution, the 7450 operands of class type are converted to the types of the 7451 corresponding parameters of the selected operation function, 7452 except that the second standard conversion sequence of a 7453 user-defined conversion sequence (12.3.3.1.2) is not applied." */ 7454 conversion *conv = cand->convs[0]; 7455 if (conv->user_conv_p) 7456 { 7457 conv = strip_standard_conversion (conv); 7458 arg1 = convert_like (conv, arg1, complain); 7459 } 7460 7461 if (arg2) 7462 { 7463 conv = cand->convs[1]; 7464 if (conv->user_conv_p) 7465 { 7466 conv = strip_standard_conversion (conv); 7467 arg2 = convert_like (conv, arg2, complain); 7468 } 7469 } 7470 7471 if (arg3) 7472 { 7473 conv = cand->convs[2]; 7474 if (conv->user_conv_p) 7475 { 7476 conv = strip_standard_conversion (conv); 7477 arg3 = convert_like (conv, arg3, complain); 7478 } 7479 } 7480 } 7481 } 7482 7483 if (result || result_valid_p) 7484 return result; 7485 7486 builtin: 7487 switch (code) 7488 { 7489 case MODIFY_EXPR: 7490 return cp_build_modify_expr (loc, arg1, code2, arg2, complain); 7491 7492 case INDIRECT_REF: 7493 return cp_build_indirect_ref (loc, arg1, RO_UNARY_STAR, complain); 7494 7495 case TRUTH_ANDIF_EXPR: 7496 case TRUTH_ORIF_EXPR: 7497 case TRUTH_AND_EXPR: 7498 case TRUTH_OR_EXPR: 7499 if ((complain & tf_warning) && !processing_template_decl) 7500 warn_logical_operator (loc, code, boolean_type_node, 7501 code_orig_arg1, arg1, 7502 code_orig_arg2, arg2); 7503 /* Fall through. */ 7504 case GT_EXPR: 7505 case LT_EXPR: 7506 case GE_EXPR: 7507 case LE_EXPR: 7508 case EQ_EXPR: 7509 case NE_EXPR: 7510 if ((complain & tf_warning) 7511 && ((code_orig_arg1 == BOOLEAN_TYPE) 7512 ^ (code_orig_arg2 == BOOLEAN_TYPE))) 7513 maybe_warn_bool_compare (loc, code, arg1, arg2); 7514 if (complain & tf_warning && warn_tautological_compare) 7515 warn_tautological_cmp (loc, code, arg1, arg2); 7516 /* Fall through. */ 7517 case SPACESHIP_EXPR: 7518 case PLUS_EXPR: 7519 case MINUS_EXPR: 7520 case MULT_EXPR: 7521 case TRUNC_DIV_EXPR: 7522 case MAX_EXPR: 7523 case MIN_EXPR: 7524 case LSHIFT_EXPR: 7525 case RSHIFT_EXPR: 7526 case TRUNC_MOD_EXPR: 7527 case BIT_AND_EXPR: 7528 case BIT_IOR_EXPR: 7529 case BIT_XOR_EXPR: 7530 return cp_build_binary_op (loc, code, arg1, arg2, complain); 7531 7532 case UNARY_PLUS_EXPR: 7533 case NEGATE_EXPR: 7534 case BIT_NOT_EXPR: 7535 case TRUTH_NOT_EXPR: 7536 case PREINCREMENT_EXPR: 7537 case POSTINCREMENT_EXPR: 7538 case PREDECREMENT_EXPR: 7539 case POSTDECREMENT_EXPR: 7540 case REALPART_EXPR: 7541 case IMAGPART_EXPR: 7542 case ABS_EXPR: 7543 case CO_AWAIT_EXPR: 7544 return cp_build_unary_op (code, arg1, false, complain); 7545 7546 case ARRAY_REF: 7547 return cp_build_array_ref (input_location, arg1, arg2, complain); 7548 7549 case MEMBER_REF: 7550 return build_m_component_ref (cp_build_indirect_ref (loc, arg1, 7551 RO_ARROW_STAR, 7552 complain), 7553 arg2, complain); 7554 7555 /* The caller will deal with these. */ 7556 case ADDR_EXPR: 7557 case COMPONENT_REF: 7558 case COMPOUND_EXPR: 7559 return NULL_TREE; 7560 7561 default: 7562 gcc_unreachable (); 7563 } 7564 return NULL_TREE; 7565 } 7566 7567 /* Build a new call to operator[]. This may change ARGS. */ 7568 7569 tree 7570 build_op_subscript (const op_location_t &loc, tree obj, 7571 vec<tree, va_gc> **args, tree *overload, 7572 tsubst_flags_t complain) 7573 { 7574 struct z_candidate *candidates = 0, *cand; 7575 tree fns, first_mem_arg = NULL_TREE; 7576 bool any_viable_p; 7577 tree result = NULL_TREE; 7578 7579 auto_cond_timevar tv (TV_OVERLOAD); 7580 7581 obj = mark_lvalue_use (obj); 7582 7583 if (error_operand_p (obj)) 7584 return error_mark_node; 7585 7586 tree type = TREE_TYPE (obj); 7587 7588 obj = prep_operand (obj); 7589 7590 if (TYPE_BINFO (type)) 7591 { 7592 fns = lookup_fnfields (TYPE_BINFO (type), ovl_op_identifier (ARRAY_REF), 7593 1, complain); 7594 if (fns == error_mark_node) 7595 return error_mark_node; 7596 } 7597 else 7598 fns = NULL_TREE; 7599 7600 if (args != NULL && *args != NULL) 7601 { 7602 *args = resolve_args (*args, complain); 7603 if (*args == NULL) 7604 return error_mark_node; 7605 } 7606 7607 conversion_obstack_sentinel cos; 7608 7609 if (fns) 7610 { 7611 first_mem_arg = obj; 7612 7613 add_candidates (BASELINK_FUNCTIONS (fns), 7614 first_mem_arg, *args, NULL_TREE, 7615 NULL_TREE, false, 7616 BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns), 7617 LOOKUP_NORMAL, &candidates, complain); 7618 } 7619 7620 /* Be strict here because if we choose a bad conversion candidate, the 7621 errors we get won't mention the call context. */ 7622 candidates = splice_viable (candidates, true, &any_viable_p); 7623 if (!any_viable_p) 7624 { 7625 if (complain & tf_error) 7626 { 7627 auto_diagnostic_group d; 7628 error ("no match for call to %<%T::operator[] (%A)%>", 7629 TREE_TYPE (obj), build_tree_list_vec (*args)); 7630 print_z_candidates (loc, candidates); 7631 } 7632 result = error_mark_node; 7633 } 7634 else 7635 { 7636 cand = tourney (candidates, complain); 7637 if (cand == 0) 7638 { 7639 if (complain & tf_error) 7640 { 7641 auto_diagnostic_group d; 7642 error ("call of %<%T::operator[] (%A)%> is ambiguous", 7643 TREE_TYPE (obj), build_tree_list_vec (*args)); 7644 print_z_candidates (loc, candidates); 7645 } 7646 result = error_mark_node; 7647 } 7648 else if (TREE_CODE (cand->fn) == FUNCTION_DECL 7649 && DECL_OVERLOADED_OPERATOR_P (cand->fn) 7650 && DECL_OVERLOADED_OPERATOR_IS (cand->fn, ARRAY_REF)) 7651 { 7652 if (overload) 7653 *overload = cand->fn; 7654 result = build_over_call (cand, LOOKUP_NORMAL, complain); 7655 if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn)) 7656 /* There won't be a CALL_EXPR. */; 7657 else if (result && result != error_mark_node) 7658 { 7659 tree call = extract_call_expr (result); 7660 CALL_EXPR_OPERATOR_SYNTAX (call) = true; 7661 7662 /* Specify evaluation order as per P0145R2. */ 7663 CALL_EXPR_ORDERED_ARGS (call) = op_is_ordered (ARRAY_REF) == 1; 7664 } 7665 7666 /* In an expression of the form `a[]' where cand->fn 7667 which is operator[] turns out to be a static member function, 7668 `a' is none-the-less evaluated. */ 7669 result = keep_unused_object_arg (result, obj, cand->fn); 7670 } 7671 else 7672 gcc_unreachable (); 7673 } 7674 7675 return result; 7676 } 7677 7678 /* CALL was returned by some call-building function; extract the actual 7679 CALL_EXPR from any bits that have been tacked on, e.g. by 7680 convert_from_reference. */ 7681 7682 tree 7683 extract_call_expr (tree call) 7684 { 7685 while (TREE_CODE (call) == COMPOUND_EXPR) 7686 call = TREE_OPERAND (call, 1); 7687 if (REFERENCE_REF_P (call)) 7688 call = TREE_OPERAND (call, 0); 7689 if (TREE_CODE (call) == TARGET_EXPR) 7690 call = TARGET_EXPR_INITIAL (call); 7691 if (cxx_dialect >= cxx20) 7692 switch (TREE_CODE (call)) 7693 { 7694 /* C++20 rewritten comparison operators. */ 7695 case TRUTH_NOT_EXPR: 7696 call = TREE_OPERAND (call, 0); 7697 break; 7698 case LT_EXPR: 7699 case LE_EXPR: 7700 case GT_EXPR: 7701 case GE_EXPR: 7702 case SPACESHIP_EXPR: 7703 { 7704 tree op0 = TREE_OPERAND (call, 0); 7705 if (integer_zerop (op0)) 7706 call = TREE_OPERAND (call, 1); 7707 else 7708 call = op0; 7709 } 7710 break; 7711 default:; 7712 } 7713 7714 if (TREE_CODE (call) != CALL_EXPR 7715 && TREE_CODE (call) != AGGR_INIT_EXPR 7716 && call != error_mark_node) 7717 return NULL_TREE; 7718 return call; 7719 } 7720 7721 /* Returns true if FN has two parameters, of which the second has type 7722 size_t. */ 7723 7724 static bool 7725 second_parm_is_size_t (tree fn) 7726 { 7727 tree t = FUNCTION_ARG_CHAIN (fn); 7728 if (!t || !same_type_p (TREE_VALUE (t), size_type_node)) 7729 return false; 7730 t = TREE_CHAIN (t); 7731 if (t == void_list_node) 7732 return true; 7733 return false; 7734 } 7735 7736 /* True if T, an allocation function, has std::align_val_t as its second 7737 argument. */ 7738 7739 bool 7740 aligned_allocation_fn_p (tree t) 7741 { 7742 if (!aligned_new_threshold) 7743 return false; 7744 7745 tree a = FUNCTION_ARG_CHAIN (t); 7746 return (a && same_type_p (TREE_VALUE (a), align_type_node)); 7747 } 7748 7749 /* True if T is std::destroying_delete_t. */ 7750 7751 static bool 7752 std_destroying_delete_t_p (tree t) 7753 { 7754 return (TYPE_CONTEXT (t) == std_node 7755 && id_equal (TYPE_IDENTIFIER (t), "destroying_delete_t")); 7756 } 7757 7758 /* A deallocation function with at least two parameters whose second parameter 7759 type is of type std::destroying_delete_t is a destroying operator delete. A 7760 destroying operator delete shall be a class member function named operator 7761 delete. [ Note: Array deletion cannot use a destroying operator 7762 delete. --end note ] */ 7763 7764 tree 7765 destroying_delete_p (tree t) 7766 { 7767 tree a = TYPE_ARG_TYPES (TREE_TYPE (t)); 7768 if (!a || !TREE_CHAIN (a)) 7769 return NULL_TREE; 7770 tree type = TREE_VALUE (TREE_CHAIN (a)); 7771 return std_destroying_delete_t_p (type) ? type : NULL_TREE; 7772 } 7773 7774 struct dealloc_info 7775 { 7776 bool sized; 7777 bool aligned; 7778 tree destroying; 7779 }; 7780 7781 /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation 7782 function (3.7.4.2 [basic.stc.dynamic.deallocation]). If so, and DI is 7783 non-null, also set *DI. */ 7784 7785 static bool 7786 usual_deallocation_fn_p (tree t, dealloc_info *di) 7787 { 7788 if (di) *di = dealloc_info(); 7789 7790 /* A template instance is never a usual deallocation function, 7791 regardless of its signature. */ 7792 if (TREE_CODE (t) == TEMPLATE_DECL 7793 || primary_template_specialization_p (t)) 7794 return false; 7795 7796 /* A usual deallocation function is a deallocation function whose parameters 7797 after the first are 7798 - optionally, a parameter of type std::destroying_delete_t, then 7799 - optionally, a parameter of type std::size_t, then 7800 - optionally, a parameter of type std::align_val_t. */ 7801 bool global = DECL_NAMESPACE_SCOPE_P (t); 7802 tree chain = FUNCTION_ARG_CHAIN (t); 7803 if (chain && destroying_delete_p (t)) 7804 { 7805 if (di) di->destroying = TREE_VALUE (chain); 7806 chain = TREE_CHAIN (chain); 7807 } 7808 if (chain 7809 && (!global || flag_sized_deallocation) 7810 && same_type_p (TREE_VALUE (chain), size_type_node)) 7811 { 7812 if (di) di->sized = true; 7813 chain = TREE_CHAIN (chain); 7814 } 7815 if (chain && aligned_new_threshold 7816 && same_type_p (TREE_VALUE (chain), align_type_node)) 7817 { 7818 if (di) di->aligned = true; 7819 chain = TREE_CHAIN (chain); 7820 } 7821 return (chain == void_list_node); 7822 } 7823 7824 /* Just return whether FN is a usual deallocation function. */ 7825 7826 bool 7827 usual_deallocation_fn_p (tree fn) 7828 { 7829 return usual_deallocation_fn_p (fn, NULL); 7830 } 7831 7832 /* Build a call to operator delete. This has to be handled very specially, 7833 because the restrictions on what signatures match are different from all 7834 other call instances. For a normal delete, only a delete taking (void *) 7835 or (void *, size_t) is accepted. For a placement delete, only an exact 7836 match with the placement new is accepted. 7837 7838 CODE is either DELETE_EXPR or VEC_DELETE_EXPR. 7839 ADDR is the pointer to be deleted. 7840 SIZE is the size of the memory block to be deleted. 7841 GLOBAL_P is true if the delete-expression should not consider 7842 class-specific delete operators. 7843 PLACEMENT is the corresponding placement new call, or NULL_TREE. 7844 7845 If this call to "operator delete" is being generated as part to 7846 deallocate memory allocated via a new-expression (as per [expr.new] 7847 which requires that if the initialization throws an exception then 7848 we call a deallocation function), then ALLOC_FN is the allocation 7849 function. */ 7850 7851 tree 7852 build_op_delete_call (enum tree_code code, tree addr, tree size, 7853 bool global_p, tree placement, 7854 tree alloc_fn, tsubst_flags_t complain) 7855 { 7856 tree fn = NULL_TREE; 7857 tree fns, fnname, type, t; 7858 dealloc_info di_fn = { }; 7859 7860 if (addr == error_mark_node) 7861 return error_mark_node; 7862 7863 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr))); 7864 7865 fnname = ovl_op_identifier (false, code); 7866 7867 if (CLASS_TYPE_P (type) 7868 && COMPLETE_TYPE_P (complete_type (type)) 7869 && !global_p) 7870 /* In [class.free] 7871 7872 If the result of the lookup is ambiguous or inaccessible, or if 7873 the lookup selects a placement deallocation function, the 7874 program is ill-formed. 7875 7876 Therefore, we ask lookup_fnfields to complain about ambiguity. */ 7877 { 7878 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1, complain); 7879 if (fns == error_mark_node) 7880 return error_mark_node; 7881 } 7882 else 7883 fns = NULL_TREE; 7884 7885 if (fns == NULL_TREE) 7886 fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE); 7887 7888 /* Strip const and volatile from addr. */ 7889 tree oaddr = addr; 7890 addr = cp_convert (ptr_type_node, addr, complain); 7891 7892 tree excluded_destroying = NULL_TREE; 7893 7894 if (placement) 7895 { 7896 /* "A declaration of a placement deallocation function matches the 7897 declaration of a placement allocation function if it has the same 7898 number of parameters and, after parameter transformations (8.3.5), 7899 all parameter types except the first are identical." 7900 7901 So we build up the function type we want and ask instantiate_type 7902 to get it for us. */ 7903 t = FUNCTION_ARG_CHAIN (alloc_fn); 7904 t = tree_cons (NULL_TREE, ptr_type_node, t); 7905 t = build_function_type (void_type_node, t); 7906 7907 fn = instantiate_type (t, fns, tf_none); 7908 if (fn == error_mark_node) 7909 return NULL_TREE; 7910 7911 fn = MAYBE_BASELINK_FUNCTIONS (fn); 7912 7913 /* "If the lookup finds the two-parameter form of a usual deallocation 7914 function (3.7.4.2) and that function, considered as a placement 7915 deallocation function, would have been selected as a match for the 7916 allocation function, the program is ill-formed." */ 7917 if (second_parm_is_size_t (fn)) 7918 { 7919 const char *const msg1 7920 = G_("exception cleanup for this placement new selects " 7921 "non-placement %<operator delete%>"); 7922 const char *const msg2 7923 = G_("%qD is a usual (non-placement) deallocation " 7924 "function in C++14 (or with %<-fsized-deallocation%>)"); 7925 7926 /* But if the class has an operator delete (void *), then that is 7927 the usual deallocation function, so we shouldn't complain 7928 about using the operator delete (void *, size_t). */ 7929 if (DECL_CLASS_SCOPE_P (fn)) 7930 for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns))) 7931 { 7932 if (usual_deallocation_fn_p (elt) 7933 && FUNCTION_ARG_CHAIN (elt) == void_list_node) 7934 goto ok; 7935 } 7936 /* Before C++14 a two-parameter global deallocation function is 7937 always a placement deallocation function, but warn if 7938 -Wc++14-compat. */ 7939 else if (!flag_sized_deallocation) 7940 { 7941 if (complain & tf_warning) 7942 { 7943 auto_diagnostic_group d; 7944 if (warning (OPT_Wc__14_compat, msg1)) 7945 inform (DECL_SOURCE_LOCATION (fn), msg2, fn); 7946 } 7947 goto ok; 7948 } 7949 7950 if (complain & tf_warning_or_error) 7951 { 7952 auto_diagnostic_group d; 7953 if (permerror (input_location, msg1)) 7954 { 7955 /* Only mention C++14 for namespace-scope delete. */ 7956 if (DECL_NAMESPACE_SCOPE_P (fn)) 7957 inform (DECL_SOURCE_LOCATION (fn), msg2, fn); 7958 else 7959 inform (DECL_SOURCE_LOCATION (fn), 7960 "%qD is a usual (non-placement) deallocation " 7961 "function", fn); 7962 } 7963 } 7964 else 7965 return error_mark_node; 7966 ok:; 7967 } 7968 } 7969 else 7970 /* "Any non-placement deallocation function matches a non-placement 7971 allocation function. If the lookup finds a single matching 7972 deallocation function, that function will be called; otherwise, no 7973 deallocation function will be called." */ 7974 for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns))) 7975 { 7976 dealloc_info di_elt; 7977 if (usual_deallocation_fn_p (elt, &di_elt)) 7978 { 7979 /* If we're called for an EH cleanup in a new-expression, we can't 7980 use a destroying delete; the exception was thrown before the 7981 object was constructed. */ 7982 if (alloc_fn && di_elt.destroying) 7983 { 7984 excluded_destroying = elt; 7985 continue; 7986 } 7987 7988 if (!fn) 7989 { 7990 fn = elt; 7991 di_fn = di_elt; 7992 continue; 7993 } 7994 7995 /* -- If any of the deallocation functions is a destroying 7996 operator delete, all deallocation functions that are not 7997 destroying operator deletes are eliminated from further 7998 consideration. */ 7999 if (di_elt.destroying != di_fn.destroying) 8000 { 8001 if (di_elt.destroying) 8002 { 8003 fn = elt; 8004 di_fn = di_elt; 8005 } 8006 continue; 8007 } 8008 8009 /* -- If the type has new-extended alignment, a function with a 8010 parameter of type std::align_val_t is preferred; otherwise a 8011 function without such a parameter is preferred. If exactly one 8012 preferred function is found, that function is selected and the 8013 selection process terminates. If more than one preferred 8014 function is found, all non-preferred functions are eliminated 8015 from further consideration. */ 8016 if (aligned_new_threshold) 8017 { 8018 bool want_align = type_has_new_extended_alignment (type); 8019 if (di_elt.aligned != di_fn.aligned) 8020 { 8021 if (want_align == di_elt.aligned) 8022 { 8023 fn = elt; 8024 di_fn = di_elt; 8025 } 8026 continue; 8027 } 8028 } 8029 8030 /* -- If the deallocation functions have class scope, the one 8031 without a parameter of type std::size_t is selected. */ 8032 bool want_size; 8033 if (DECL_CLASS_SCOPE_P (fn)) 8034 want_size = false; 8035 8036 /* -- If the type is complete and if, for the second alternative 8037 (delete array) only, the operand is a pointer to a class type 8038 with a non-trivial destructor or a (possibly multi-dimensional) 8039 array thereof, the function with a parameter of type std::size_t 8040 is selected. 8041 8042 -- Otherwise, it is unspecified whether a deallocation function 8043 with a parameter of type std::size_t is selected. */ 8044 else 8045 { 8046 want_size = COMPLETE_TYPE_P (type); 8047 if (code == VEC_DELETE_EXPR 8048 && !TYPE_VEC_NEW_USES_COOKIE (type)) 8049 /* We need a cookie to determine the array size. */ 8050 want_size = false; 8051 } 8052 gcc_assert (di_fn.sized != di_elt.sized); 8053 if (want_size == di_elt.sized) 8054 { 8055 fn = elt; 8056 di_fn = di_elt; 8057 } 8058 } 8059 } 8060 8061 /* If we have a matching function, call it. */ 8062 if (fn) 8063 { 8064 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL); 8065 8066 /* If the FN is a member function, make sure that it is 8067 accessible. */ 8068 if (BASELINK_P (fns)) 8069 perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn, 8070 complain); 8071 8072 /* Core issue 901: It's ok to new a type with deleted delete. */ 8073 if (DECL_DELETED_FN (fn) && alloc_fn) 8074 return NULL_TREE; 8075 8076 tree ret; 8077 if (placement) 8078 { 8079 /* The placement args might not be suitable for overload 8080 resolution at this point, so build the call directly. */ 8081 int nargs = call_expr_nargs (placement); 8082 tree *argarray = XALLOCAVEC (tree, nargs); 8083 int i; 8084 argarray[0] = addr; 8085 for (i = 1; i < nargs; i++) 8086 argarray[i] = CALL_EXPR_ARG (placement, i); 8087 if (!mark_used (fn, complain) && !(complain & tf_error)) 8088 return error_mark_node; 8089 ret = build_cxx_call (fn, nargs, argarray, complain); 8090 } 8091 else 8092 { 8093 tree destroying = di_fn.destroying; 8094 if (destroying) 8095 { 8096 /* Strip const and volatile from addr but retain the type of the 8097 object. */ 8098 tree rtype = TREE_TYPE (TREE_TYPE (oaddr)); 8099 rtype = cv_unqualified (rtype); 8100 rtype = TYPE_POINTER_TO (rtype); 8101 addr = cp_convert (rtype, oaddr, complain); 8102 destroying = build_functional_cast (input_location, 8103 destroying, NULL_TREE, 8104 complain); 8105 } 8106 8107 releasing_vec args; 8108 args->quick_push (addr); 8109 if (destroying) 8110 args->quick_push (destroying); 8111 if (di_fn.sized) 8112 args->quick_push (size); 8113 if (di_fn.aligned) 8114 { 8115 tree al = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (type)); 8116 args->quick_push (al); 8117 } 8118 ret = cp_build_function_call_vec (fn, &args, complain); 8119 } 8120 8121 /* Set this flag for all callers of this function. In addition to 8122 delete-expressions, this is called for deallocating coroutine state; 8123 treat that as an implicit delete-expression. This is also called for 8124 the delete if the constructor throws in a new-expression, and for a 8125 deleting destructor (which implements a delete-expression). */ 8126 /* But leave this flag off for destroying delete to avoid wrong 8127 assumptions in the optimizers. */ 8128 tree call = extract_call_expr (ret); 8129 if (TREE_CODE (call) == CALL_EXPR && !destroying_delete_p (fn)) 8130 CALL_FROM_NEW_OR_DELETE_P (call) = 1; 8131 8132 return ret; 8133 } 8134 8135 /* If there's only a destroying delete that we can't use because the 8136 object isn't constructed yet, and we used global new, use global 8137 delete as well. */ 8138 if (excluded_destroying 8139 && DECL_NAMESPACE_SCOPE_P (alloc_fn)) 8140 return build_op_delete_call (code, addr, size, true, placement, 8141 alloc_fn, complain); 8142 8143 /* [expr.new] 8144 8145 If no unambiguous matching deallocation function can be found, 8146 propagating the exception does not cause the object's memory to 8147 be freed. */ 8148 if (alloc_fn) 8149 { 8150 if ((complain & tf_warning) 8151 && !placement) 8152 { 8153 bool w = warning (0, 8154 "no corresponding deallocation function for %qD", 8155 alloc_fn); 8156 if (w && excluded_destroying) 8157 inform (DECL_SOURCE_LOCATION (excluded_destroying), "destroying " 8158 "delete %qD cannot be used to release the allocated memory" 8159 " if the initialization throws because the object is not " 8160 "constructed yet", excluded_destroying); 8161 } 8162 return NULL_TREE; 8163 } 8164 8165 if (complain & tf_error) 8166 error ("no suitable %<operator %s%> for %qT", 8167 OVL_OP_INFO (false, code)->name, type); 8168 return error_mark_node; 8169 } 8170 8171 /* Issue diagnostics about a disallowed access of DECL, using DIAG_DECL 8172 in the diagnostics. 8173 8174 If ISSUE_ERROR is true, then issue an error about the access, followed 8175 by a note showing the declaration. Otherwise, just show the note. 8176 8177 DIAG_DECL and DIAG_LOCATION will almost always be the same. 8178 DIAG_LOCATION is just another DECL. NO_ACCESS_REASON is an optional 8179 parameter used to specify why DECL wasn't accessible (e.g. ak_private 8180 would be because DECL was private). If not using NO_ACCESS_REASON, 8181 then it must be ak_none, and the access failure reason will be 8182 figured out by looking at the protection of DECL. */ 8183 8184 void 8185 complain_about_access (tree decl, tree diag_decl, tree diag_location, 8186 bool issue_error, access_kind no_access_reason) 8187 { 8188 /* If we have not already figured out why DECL is inaccessible... */ 8189 if (no_access_reason == ak_none) 8190 { 8191 /* Examine the access of DECL to find out why. */ 8192 if (TREE_PRIVATE (decl)) 8193 no_access_reason = ak_private; 8194 else if (TREE_PROTECTED (decl)) 8195 no_access_reason = ak_protected; 8196 } 8197 8198 /* Now generate an error message depending on calculated access. */ 8199 if (no_access_reason == ak_private) 8200 { 8201 if (issue_error) 8202 error ("%q#D is private within this context", diag_decl); 8203 inform (DECL_SOURCE_LOCATION (diag_location), "declared private here"); 8204 } 8205 else if (no_access_reason == ak_protected) 8206 { 8207 if (issue_error) 8208 error ("%q#D is protected within this context", diag_decl); 8209 inform (DECL_SOURCE_LOCATION (diag_location), "declared protected here"); 8210 } 8211 /* Couldn't figure out why DECL is inaccesible, so just say it's 8212 inaccessible. */ 8213 else 8214 { 8215 if (issue_error) 8216 error ("%q#D is inaccessible within this context", diag_decl); 8217 inform (DECL_SOURCE_LOCATION (diag_decl), "declared here"); 8218 } 8219 } 8220 8221 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a 8222 bitwise or of LOOKUP_* values. If any errors are warnings are 8223 generated, set *DIAGNOSTIC_FN to "error" or "warning", 8224 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN 8225 to NULL. */ 8226 8227 static tree 8228 build_temp (tree expr, tree type, int flags, 8229 diagnostic_t *diagnostic_kind, tsubst_flags_t complain) 8230 { 8231 int savew, savee; 8232 8233 *diagnostic_kind = DK_UNSPECIFIED; 8234 8235 /* If the source is a packed field, calling the copy constructor will require 8236 binding the field to the reference parameter to the copy constructor, and 8237 we'll end up with an infinite loop. If we can use a bitwise copy, then 8238 do that now. */ 8239 if ((lvalue_kind (expr) & clk_packed) 8240 && CLASS_TYPE_P (TREE_TYPE (expr)) 8241 && !type_has_nontrivial_copy_init (TREE_TYPE (expr))) 8242 return get_target_expr (expr, complain); 8243 8244 /* In decltype, we might have decided not to wrap this call in a TARGET_EXPR. 8245 But it turns out to be a subexpression, so perform temporary 8246 materialization now. */ 8247 if (TREE_CODE (expr) == CALL_EXPR 8248 && CLASS_TYPE_P (type) 8249 && same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr))) 8250 expr = build_cplus_new (type, expr, complain); 8251 8252 savew = warningcount + werrorcount, savee = errorcount; 8253 releasing_vec args (make_tree_vector_single (expr)); 8254 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier, 8255 &args, type, flags, complain); 8256 if (warningcount + werrorcount > savew) 8257 *diagnostic_kind = DK_WARNING; 8258 else if (errorcount > savee) 8259 *diagnostic_kind = DK_ERROR; 8260 return expr; 8261 } 8262 8263 /* Get any location for EXPR, falling back to input_location. 8264 8265 If the result is in a system header and is the virtual location for 8266 a token coming from the expansion of a macro, unwind it to the 8267 location of the expansion point of the macro (e.g. to avoid the 8268 diagnostic being suppressed for expansions of NULL where "NULL" is 8269 in a system header). */ 8270 8271 static location_t 8272 get_location_for_expr_unwinding_for_system_header (tree expr) 8273 { 8274 location_t loc = EXPR_LOC_OR_LOC (expr, input_location); 8275 loc = expansion_point_location_if_in_system_header (loc); 8276 return loc; 8277 } 8278 8279 /* Perform warnings about peculiar, but valid, conversions from/to NULL. 8280 Also handle a subset of zero as null warnings. 8281 EXPR is implicitly converted to type TOTYPE. 8282 FN and ARGNUM are used for diagnostics. */ 8283 8284 static void 8285 conversion_null_warnings (tree totype, tree expr, tree fn, int argnum) 8286 { 8287 /* Issue warnings about peculiar, but valid, uses of NULL. */ 8288 if (TREE_CODE (totype) != BOOLEAN_TYPE 8289 && ARITHMETIC_TYPE_P (totype) 8290 && null_node_p (expr)) 8291 { 8292 location_t loc = get_location_for_expr_unwinding_for_system_header (expr); 8293 if (fn) 8294 { 8295 auto_diagnostic_group d; 8296 if (warning_at (loc, OPT_Wconversion_null, 8297 "passing NULL to non-pointer argument %P of %qD", 8298 argnum, fn)) 8299 inform (get_fndecl_argument_location (fn, argnum), 8300 " declared here"); 8301 } 8302 else 8303 warning_at (loc, OPT_Wconversion_null, 8304 "converting to non-pointer type %qT from NULL", totype); 8305 } 8306 8307 /* Issue warnings if "false" is converted to a NULL pointer */ 8308 else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE 8309 && TYPE_PTR_P (totype)) 8310 { 8311 location_t loc = get_location_for_expr_unwinding_for_system_header (expr); 8312 if (fn) 8313 { 8314 auto_diagnostic_group d; 8315 if (warning_at (loc, OPT_Wconversion_null, 8316 "converting %<false%> to pointer type for argument " 8317 "%P of %qD", argnum, fn)) 8318 inform (get_fndecl_argument_location (fn, argnum), 8319 " declared here"); 8320 } 8321 else 8322 warning_at (loc, OPT_Wconversion_null, 8323 "converting %<false%> to pointer type %qT", totype); 8324 } 8325 /* Handle zero as null pointer warnings for cases other 8326 than EQ_EXPR and NE_EXPR */ 8327 else if ((TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype)) 8328 && null_ptr_cst_p (expr)) 8329 { 8330 location_t loc = get_location_for_expr_unwinding_for_system_header (expr); 8331 maybe_warn_zero_as_null_pointer_constant (expr, loc); 8332 } 8333 } 8334 8335 /* We gave a diagnostic during a conversion. If this was in the second 8336 standard conversion sequence of a user-defined conversion sequence, say 8337 which user-defined conversion. */ 8338 8339 static void 8340 maybe_print_user_conv_context (conversion *convs) 8341 { 8342 if (convs->user_conv_p) 8343 for (conversion *t = convs; t; t = next_conversion (t)) 8344 if (t->kind == ck_user) 8345 { 8346 print_z_candidate (0, N_(" after user-defined conversion:"), 8347 t->cand); 8348 break; 8349 } 8350 } 8351 8352 /* Locate the parameter with the given index within FNDECL. 8353 ARGNUM is zero based, -1 indicates the `this' argument of a method. 8354 Return the location of the FNDECL itself if there are problems. */ 8355 8356 location_t 8357 get_fndecl_argument_location (tree fndecl, int argnum) 8358 { 8359 /* The locations of implicitly-declared functions are likely to be 8360 more meaningful than those of their parameters. */ 8361 if (DECL_ARTIFICIAL (fndecl)) 8362 return DECL_SOURCE_LOCATION (fndecl); 8363 8364 int i; 8365 tree param; 8366 8367 /* Locate param by index within DECL_ARGUMENTS (fndecl). */ 8368 for (i = 0, param = FUNCTION_FIRST_USER_PARM (fndecl); 8369 i < argnum && param; 8370 i++, param = TREE_CHAIN (param)) 8371 ; 8372 8373 /* If something went wrong (e.g. if we have a builtin and thus no arguments), 8374 return the location of FNDECL. */ 8375 if (param == NULL) 8376 return DECL_SOURCE_LOCATION (fndecl); 8377 8378 return DECL_SOURCE_LOCATION (param); 8379 } 8380 8381 /* If FNDECL is non-NULL, issue a note highlighting ARGNUM 8382 within its declaration (or the fndecl itself if something went 8383 wrong). */ 8384 8385 void 8386 maybe_inform_about_fndecl_for_bogus_argument_init (tree fn, int argnum) 8387 { 8388 if (fn) 8389 inform (get_fndecl_argument_location (fn, argnum), 8390 " initializing argument %P of %qD", argnum, fn); 8391 } 8392 8393 /* Maybe warn about C++20 Conversions to arrays of unknown bound. C is 8394 the conversion, EXPR is the expression we're converting. */ 8395 8396 static void 8397 maybe_warn_array_conv (location_t loc, conversion *c, tree expr) 8398 { 8399 if (cxx_dialect >= cxx20) 8400 return; 8401 8402 tree type = TREE_TYPE (expr); 8403 type = strip_pointer_operator (type); 8404 8405 if (TREE_CODE (type) != ARRAY_TYPE 8406 || TYPE_DOMAIN (type) == NULL_TREE) 8407 return; 8408 8409 if (pedantic && conv_binds_to_array_of_unknown_bound (c)) 8410 pedwarn (loc, OPT_Wc__20_extensions, 8411 "conversions to arrays of unknown bound " 8412 "are only available with %<-std=c++20%> or %<-std=gnu++20%>"); 8413 } 8414 8415 /* We call this recursively in convert_like_internal. */ 8416 static tree convert_like (conversion *, tree, tree, int, bool, bool, bool, 8417 tsubst_flags_t); 8418 8419 /* Perform the conversions in CONVS on the expression EXPR. FN and 8420 ARGNUM are used for diagnostics. ARGNUM is zero based, -1 8421 indicates the `this' argument of a method. INNER is nonzero when 8422 being called to continue a conversion chain. It is negative when a 8423 reference binding will be applied, positive otherwise. If 8424 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious 8425 conversions will be emitted if appropriate. If C_CAST_P is true, 8426 this conversion is coming from a C-style cast; in that case, 8427 conversions to inaccessible bases are permitted. */ 8428 8429 static tree 8430 convert_like_internal (conversion *convs, tree expr, tree fn, int argnum, 8431 bool issue_conversion_warnings, bool c_cast_p, 8432 bool nested_p, tsubst_flags_t complain) 8433 { 8434 tree totype = convs->type; 8435 diagnostic_t diag_kind; 8436 int flags; 8437 location_t loc = cp_expr_loc_or_input_loc (expr); 8438 8439 if (convs->bad_p && !(complain & tf_error)) 8440 return error_mark_node; 8441 8442 if (convs->bad_p 8443 && convs->kind != ck_user 8444 && convs->kind != ck_list 8445 && convs->kind != ck_ambig 8446 && (convs->kind != ck_ref_bind 8447 || (convs->user_conv_p && next_conversion (convs)->bad_p)) 8448 && (convs->kind != ck_rvalue 8449 || SCALAR_TYPE_P (totype)) 8450 && convs->kind != ck_base) 8451 { 8452 int complained = 0; 8453 conversion *t = convs; 8454 8455 /* Give a helpful error if this is bad because of excess braces. */ 8456 if (BRACE_ENCLOSED_INITIALIZER_P (expr) 8457 && SCALAR_TYPE_P (totype) 8458 && CONSTRUCTOR_NELTS (expr) > 0 8459 && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value)) 8460 { 8461 complained = permerror (loc, "too many braces around initializer " 8462 "for %qT", totype); 8463 while (BRACE_ENCLOSED_INITIALIZER_P (expr) 8464 && CONSTRUCTOR_NELTS (expr) == 1) 8465 expr = CONSTRUCTOR_ELT (expr, 0)->value; 8466 } 8467 8468 /* Give a helpful error if this is bad because a conversion to bool 8469 from std::nullptr_t requires direct-initialization. */ 8470 if (NULLPTR_TYPE_P (TREE_TYPE (expr)) 8471 && TREE_CODE (totype) == BOOLEAN_TYPE) 8472 complained = permerror (loc, "converting to %qH from %qI requires " 8473 "direct-initialization", 8474 totype, TREE_TYPE (expr)); 8475 8476 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (expr)) 8477 && SCALAR_FLOAT_TYPE_P (totype) 8478 && (extended_float_type_p (TREE_TYPE (expr)) 8479 || extended_float_type_p (totype))) 8480 switch (cp_compare_floating_point_conversion_ranks (TREE_TYPE (expr), 8481 totype)) 8482 { 8483 case 2: 8484 if (pedwarn (loc, OPT_Wnarrowing, "ISO C++ does not allow " 8485 "converting to %qH from %qI with greater " 8486 "conversion rank", totype, TREE_TYPE (expr))) 8487 complained = 1; 8488 else if (!complained) 8489 complained = -1; 8490 break; 8491 case 3: 8492 if (pedwarn (loc, OPT_Wnarrowing, "ISO C++ does not allow " 8493 "converting to %qH from %qI with unordered " 8494 "conversion rank", totype, TREE_TYPE (expr))) 8495 complained = 1; 8496 else if (!complained) 8497 complained = -1; 8498 break; 8499 default: 8500 break; 8501 } 8502 8503 for (; t ; t = next_conversion (t)) 8504 { 8505 if (t->kind == ck_user && t->cand->reason) 8506 { 8507 auto_diagnostic_group d; 8508 complained = permerror (loc, "invalid user-defined conversion " 8509 "from %qH to %qI", TREE_TYPE (expr), 8510 totype); 8511 if (complained) 8512 print_z_candidate (loc, N_("candidate is:"), t->cand); 8513 expr = convert_like (t, expr, fn, argnum, 8514 /*issue_conversion_warnings=*/false, 8515 /*c_cast_p=*/false, /*nested_p=*/true, 8516 complain); 8517 } 8518 else if (t->kind == ck_user || !t->bad_p) 8519 { 8520 expr = convert_like (t, expr, fn, argnum, 8521 /*issue_conversion_warnings=*/false, 8522 /*c_cast_p=*/false, /*nested_p=*/true, 8523 complain); 8524 if (t->bad_p) 8525 complained = 1; 8526 break; 8527 } 8528 else if (t->kind == ck_ambig) 8529 return convert_like (t, expr, fn, argnum, 8530 /*issue_conversion_warnings=*/false, 8531 /*c_cast_p=*/false, /*nested_p=*/true, 8532 complain); 8533 else if (t->kind == ck_identity) 8534 break; 8535 } 8536 if (!complained && expr != error_mark_node) 8537 { 8538 range_label_for_type_mismatch label (TREE_TYPE (expr), totype); 8539 gcc_rich_location richloc (loc, &label); 8540 complained = permerror (&richloc, 8541 "invalid conversion from %qH to %qI", 8542 TREE_TYPE (expr), totype); 8543 } 8544 if (convs->kind == ck_ref_bind) 8545 expr = convert_to_reference (totype, expr, CONV_IMPLICIT, 8546 LOOKUP_NORMAL, NULL_TREE, 8547 complain); 8548 else 8549 expr = cp_convert (totype, expr, complain); 8550 if (complained == 1) 8551 maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum); 8552 return expr; 8553 } 8554 8555 if (issue_conversion_warnings && (complain & tf_warning)) 8556 conversion_null_warnings (totype, expr, fn, argnum); 8557 8558 switch (convs->kind) 8559 { 8560 case ck_user: 8561 { 8562 struct z_candidate *cand = convs->cand; 8563 8564 if (cand == NULL) 8565 /* We chose the surrogate function from add_conv_candidate, now we 8566 actually need to build the conversion. */ 8567 cand = build_user_type_conversion_1 (totype, expr, 8568 LOOKUP_NO_CONVERSION, complain); 8569 8570 tree convfn = cand->fn; 8571 8572 /* When converting from an init list we consider explicit 8573 constructors, but actually trying to call one is an error. */ 8574 if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn) 8575 && BRACE_ENCLOSED_INITIALIZER_P (expr) 8576 /* Unless this is for direct-list-initialization. */ 8577 && (!CONSTRUCTOR_IS_DIRECT_INIT (expr) || convs->need_temporary_p) 8578 /* And in C++98 a default constructor can't be explicit. */ 8579 && cxx_dialect >= cxx11) 8580 { 8581 if (!(complain & tf_error)) 8582 return error_mark_node; 8583 location_t loc = location_of (expr); 8584 if (CONSTRUCTOR_NELTS (expr) == 0 8585 && FUNCTION_FIRST_USER_PARMTYPE (convfn) != void_list_node) 8586 { 8587 auto_diagnostic_group d; 8588 if (pedwarn (loc, 0, "converting to %qT from initializer list " 8589 "would use explicit constructor %qD", 8590 totype, convfn)) 8591 { 8592 inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here", 8593 convfn); 8594 inform (loc, "in C++11 and above a default constructor " 8595 "can be explicit"); 8596 } 8597 } 8598 else 8599 { 8600 auto_diagnostic_group d; 8601 error ("converting to %qT from initializer list would use " 8602 "explicit constructor %qD", totype, convfn); 8603 inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here", 8604 convfn); 8605 } 8606 } 8607 8608 /* If we're initializing from {}, it's value-initialization. */ 8609 if (BRACE_ENCLOSED_INITIALIZER_P (expr) 8610 && CONSTRUCTOR_NELTS (expr) == 0 8611 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype) 8612 && !processing_template_decl) 8613 { 8614 if (abstract_virtuals_error (NULL_TREE, totype, complain)) 8615 return error_mark_node; 8616 expr = build_value_init (totype, complain); 8617 expr = get_target_expr (expr, complain); 8618 if (expr != error_mark_node) 8619 TARGET_EXPR_LIST_INIT_P (expr) = true; 8620 return expr; 8621 } 8622 8623 /* We don't know here whether EXPR is being used as an lvalue or 8624 rvalue, but we know it's read. */ 8625 mark_exp_read (expr); 8626 8627 /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow 8628 any more UDCs. */ 8629 expr = build_over_call (cand, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION, 8630 complain); 8631 8632 /* If this is a constructor or a function returning an aggr type, 8633 we need to build up a TARGET_EXPR. */ 8634 if (DECL_CONSTRUCTOR_P (convfn)) 8635 { 8636 expr = build_cplus_new (totype, expr, complain); 8637 8638 /* Remember that this was list-initialization. */ 8639 if (convs->check_narrowing && expr != error_mark_node) 8640 TARGET_EXPR_LIST_INIT_P (expr) = true; 8641 } 8642 8643 return expr; 8644 } 8645 case ck_identity: 8646 if (BRACE_ENCLOSED_INITIALIZER_P (expr)) 8647 { 8648 int nelts = CONSTRUCTOR_NELTS (expr); 8649 if (nelts == 0) 8650 expr = build_value_init (totype, complain); 8651 else if (nelts == 1) 8652 expr = CONSTRUCTOR_ELT (expr, 0)->value; 8653 else 8654 gcc_unreachable (); 8655 } 8656 expr = mark_use (expr, /*rvalue_p=*/!convs->rvaluedness_matches_p, 8657 /*read_p=*/true, UNKNOWN_LOCATION, 8658 /*reject_builtin=*/true); 8659 8660 if (type_unknown_p (expr)) 8661 expr = instantiate_type (totype, expr, complain); 8662 if (!nested_p && TREE_CODE (expr) == EXCESS_PRECISION_EXPR) 8663 expr = cp_convert (totype, TREE_OPERAND (expr, 0), complain); 8664 if (expr == null_node 8665 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype)) 8666 /* If __null has been converted to an integer type, we do not want to 8667 continue to warn about uses of EXPR as an integer, rather than as a 8668 pointer. */ 8669 expr = build_int_cst (totype, 0); 8670 return expr; 8671 case ck_ambig: 8672 /* We leave bad_p off ck_ambig because overload resolution considers 8673 it valid, it just fails when we try to perform it. So we need to 8674 check complain here, too. */ 8675 if (complain & tf_error) 8676 { 8677 /* Call build_user_type_conversion again for the error. */ 8678 int flags = (convs->need_temporary_p 8679 ? LOOKUP_IMPLICIT : LOOKUP_NORMAL); 8680 build_user_type_conversion (totype, convs->u.expr, flags, complain); 8681 gcc_assert (seen_error ()); 8682 maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum); 8683 } 8684 return error_mark_node; 8685 8686 case ck_list: 8687 { 8688 /* Conversion to std::initializer_list<T>. */ 8689 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0); 8690 unsigned len = CONSTRUCTOR_NELTS (expr); 8691 tree array; 8692 8693 if (len) 8694 { 8695 tree val; unsigned ix; 8696 8697 tree new_ctor = build_constructor (init_list_type_node, NULL); 8698 8699 /* Convert all the elements. */ 8700 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val) 8701 { 8702 tree sub = convert_like (convs->u.list[ix], val, fn, 8703 argnum, false, false, 8704 /*nested_p=*/true, complain); 8705 if (sub == error_mark_node) 8706 return sub; 8707 if (!BRACE_ENCLOSED_INITIALIZER_P (val) 8708 && !check_narrowing (TREE_TYPE (sub), val, complain)) 8709 return error_mark_node; 8710 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor), 8711 NULL_TREE, sub); 8712 if (!TREE_CONSTANT (sub)) 8713 TREE_CONSTANT (new_ctor) = false; 8714 } 8715 /* Build up the array. */ 8716 elttype = cp_build_qualified_type 8717 (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST); 8718 array = build_array_of_n_type (elttype, len); 8719 array = finish_compound_literal (array, new_ctor, complain); 8720 /* This is dubious now, should be blessed by P2752. */ 8721 DECL_MERGEABLE (TARGET_EXPR_SLOT (array)) = true; 8722 array = cp_build_addr_expr (array, complain); 8723 } 8724 else 8725 array = nullptr_node; 8726 8727 array = cp_convert (build_pointer_type (elttype), array, complain); 8728 if (array == error_mark_node) 8729 return error_mark_node; 8730 8731 /* Build up the initializer_list object. Note: fail gracefully 8732 if the object cannot be completed because, for example, no 8733 definition is provided (c++/80956). */ 8734 totype = complete_type_or_maybe_complain (totype, NULL_TREE, complain); 8735 if (!totype) 8736 return error_mark_node; 8737 tree field = next_aggregate_field (TYPE_FIELDS (totype)); 8738 vec<constructor_elt, va_gc> *vec = NULL; 8739 CONSTRUCTOR_APPEND_ELT (vec, field, array); 8740 field = next_aggregate_field (DECL_CHAIN (field)); 8741 CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len)); 8742 tree new_ctor = build_constructor (totype, vec); 8743 return get_target_expr (new_ctor, complain); 8744 } 8745 8746 case ck_aggr: 8747 if (TREE_CODE (totype) == COMPLEX_TYPE) 8748 { 8749 tree real = CONSTRUCTOR_ELT (expr, 0)->value; 8750 tree imag = CONSTRUCTOR_ELT (expr, 1)->value; 8751 real = perform_implicit_conversion (TREE_TYPE (totype), 8752 real, complain); 8753 imag = perform_implicit_conversion (TREE_TYPE (totype), 8754 imag, complain); 8755 expr = build2 (COMPLEX_EXPR, totype, real, imag); 8756 return expr; 8757 } 8758 expr = reshape_init (totype, expr, complain); 8759 expr = get_target_expr (digest_init (totype, expr, complain), 8760 complain); 8761 if (expr != error_mark_node) 8762 TARGET_EXPR_LIST_INIT_P (expr) = true; 8763 return expr; 8764 8765 default: 8766 break; 8767 }; 8768 8769 conversion *nc = next_conversion (convs); 8770 if (convs->kind == ck_ref_bind && nc->kind == ck_qual 8771 && !convs->need_temporary_p) 8772 /* direct_reference_binding might have inserted a ck_qual under 8773 this ck_ref_bind for the benefit of conversion sequence ranking. 8774 Don't actually perform that conversion. */ 8775 nc = next_conversion (nc); 8776 8777 expr = convert_like (nc, expr, fn, argnum, 8778 convs->kind == ck_ref_bind 8779 ? issue_conversion_warnings : false, 8780 c_cast_p, /*nested_p=*/true, complain & ~tf_no_cleanup); 8781 if (expr == error_mark_node) 8782 return error_mark_node; 8783 8784 switch (convs->kind) 8785 { 8786 case ck_rvalue: 8787 expr = decay_conversion (expr, complain); 8788 if (expr == error_mark_node) 8789 { 8790 if (complain & tf_error) 8791 { 8792 auto_diagnostic_group d; 8793 maybe_print_user_conv_context (convs); 8794 maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum); 8795 } 8796 return error_mark_node; 8797 } 8798 8799 if (! MAYBE_CLASS_TYPE_P (totype)) 8800 return expr; 8801 8802 /* Don't introduce copies when passing arguments along to the inherited 8803 constructor. */ 8804 if (current_function_decl 8805 && flag_new_inheriting_ctors 8806 && DECL_INHERITED_CTOR (current_function_decl)) 8807 return expr; 8808 8809 if (TREE_CODE (expr) == TARGET_EXPR 8810 && TARGET_EXPR_LIST_INIT_P (expr)) 8811 /* Copy-list-initialization doesn't actually involve a copy. */ 8812 return expr; 8813 8814 /* Fall through. */ 8815 case ck_base: 8816 if (convs->kind == ck_base && !convs->need_temporary_p) 8817 { 8818 /* We are going to bind a reference directly to a base-class 8819 subobject of EXPR. */ 8820 /* Build an expression for `*((base*) &expr)'. */ 8821 expr = convert_to_base (expr, totype, 8822 !c_cast_p, /*nonnull=*/true, complain); 8823 return expr; 8824 } 8825 8826 /* Copy-initialization where the cv-unqualified version of the source 8827 type is the same class as, or a derived class of, the class of the 8828 destination [is treated as direct-initialization]. [dcl.init] */ 8829 flags = LOOKUP_NORMAL; 8830 /* This conversion is being done in the context of a user-defined 8831 conversion (i.e. the second step of copy-initialization), so 8832 don't allow any more. */ 8833 if (convs->user_conv_p) 8834 flags |= LOOKUP_NO_CONVERSION; 8835 /* We might be performing a conversion of the argument 8836 to the user-defined conversion, i.e., not a conversion of the 8837 result of the user-defined conversion. In which case we skip 8838 explicit constructors. */ 8839 if (convs->copy_init_p) 8840 flags |= LOOKUP_ONLYCONVERTING; 8841 expr = build_temp (expr, totype, flags, &diag_kind, complain); 8842 if (diag_kind && complain) 8843 { 8844 auto_diagnostic_group d; 8845 maybe_print_user_conv_context (convs); 8846 maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum); 8847 } 8848 8849 return build_cplus_new (totype, expr, complain); 8850 8851 case ck_ref_bind: 8852 { 8853 tree ref_type = totype; 8854 8855 if (convs->bad_p && !next_conversion (convs)->bad_p) 8856 { 8857 tree extype = TREE_TYPE (expr); 8858 auto_diagnostic_group d; 8859 if (TYPE_REF_IS_RVALUE (ref_type) 8860 && lvalue_p (expr)) 8861 error_at (loc, "cannot bind rvalue reference of type %qH to " 8862 "lvalue of type %qI", totype, extype); 8863 else if (!TYPE_REF_IS_RVALUE (ref_type) && !lvalue_p (expr) 8864 && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type))) 8865 { 8866 conversion *next = next_conversion (convs); 8867 if (next->kind == ck_std) 8868 { 8869 next = next_conversion (next); 8870 error_at (loc, "cannot bind non-const lvalue reference of " 8871 "type %qH to a value of type %qI", 8872 totype, next->type); 8873 } 8874 else if (!CP_TYPE_CONST_P (TREE_TYPE (ref_type))) 8875 error_at (loc, "cannot bind non-const lvalue reference of " 8876 "type %qH to an rvalue of type %qI", totype, extype); 8877 else // extype is volatile 8878 error_at (loc, "cannot bind lvalue reference of type " 8879 "%qH to an rvalue of type %qI", totype, 8880 extype); 8881 } 8882 else if (!reference_compatible_p (TREE_TYPE (totype), extype)) 8883 { 8884 /* If we're converting from T[] to T[N], don't talk 8885 about discarding qualifiers. (Converting from T[N] to 8886 T[] is allowed by P0388R4.) */ 8887 if (TREE_CODE (extype) == ARRAY_TYPE 8888 && TYPE_DOMAIN (extype) == NULL_TREE 8889 && TREE_CODE (TREE_TYPE (totype)) == ARRAY_TYPE 8890 && TYPE_DOMAIN (TREE_TYPE (totype)) != NULL_TREE) 8891 error_at (loc, "cannot bind reference of type %qH to %qI " 8892 "due to different array bounds", totype, extype); 8893 else 8894 error_at (loc, "binding reference of type %qH to %qI " 8895 "discards qualifiers", totype, extype); 8896 } 8897 else 8898 gcc_unreachable (); 8899 maybe_print_user_conv_context (convs); 8900 maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum); 8901 8902 return error_mark_node; 8903 } 8904 else if (complain & tf_warning) 8905 maybe_warn_array_conv (loc, convs, expr); 8906 8907 /* If necessary, create a temporary. 8908 8909 VA_ARG_EXPR and CONSTRUCTOR expressions are special cases 8910 that need temporaries, even when their types are reference 8911 compatible with the type of reference being bound, so the 8912 upcoming call to cp_build_addr_expr doesn't fail. */ 8913 if (convs->need_temporary_p 8914 || TREE_CODE (expr) == CONSTRUCTOR 8915 || TREE_CODE (expr) == VA_ARG_EXPR) 8916 { 8917 /* Otherwise, a temporary of type "cv1 T1" is created and 8918 initialized from the initializer expression using the rules 8919 for a non-reference copy-initialization (8.5). */ 8920 8921 tree type = TREE_TYPE (ref_type); 8922 cp_lvalue_kind lvalue = lvalue_kind (expr); 8923 8924 gcc_assert (similar_type_p (type, next_conversion (convs)->type)); 8925 if (!CP_TYPE_CONST_NON_VOLATILE_P (type) 8926 && !TYPE_REF_IS_RVALUE (ref_type)) 8927 { 8928 /* If the reference is volatile or non-const, we 8929 cannot create a temporary. */ 8930 if (complain & tf_error) 8931 { 8932 if (lvalue & clk_bitfield) 8933 error_at (loc, "cannot bind bit-field %qE to %qT", 8934 expr, ref_type); 8935 else if (lvalue & clk_packed) 8936 error_at (loc, "cannot bind packed field %qE to %qT", 8937 expr, ref_type); 8938 else 8939 error_at (loc, "cannot bind rvalue %qE to %qT", 8940 expr, ref_type); 8941 } 8942 return error_mark_node; 8943 } 8944 /* If the source is a packed field, and we must use a copy 8945 constructor, then building the target expr will require 8946 binding the field to the reference parameter to the 8947 copy constructor, and we'll end up with an infinite 8948 loop. If we can use a bitwise copy, then we'll be 8949 OK. */ 8950 if ((lvalue & clk_packed) 8951 && CLASS_TYPE_P (type) 8952 && type_has_nontrivial_copy_init (type)) 8953 { 8954 error_at (loc, "cannot bind packed field %qE to %qT", 8955 expr, ref_type); 8956 return error_mark_node; 8957 } 8958 if (lvalue & clk_bitfield) 8959 { 8960 expr = convert_bitfield_to_declared_type (expr); 8961 expr = fold_convert (type, expr); 8962 } 8963 8964 /* Creating &TARGET_EXPR<> in a template would break when 8965 tsubsting the expression, so use an IMPLICIT_CONV_EXPR 8966 instead. This can happen even when there's no class 8967 involved, e.g., when converting an integer to a reference 8968 type. */ 8969 if (processing_template_decl) 8970 return build1 (IMPLICIT_CONV_EXPR, totype, expr); 8971 expr = build_target_expr_with_type (expr, type, complain); 8972 } 8973 8974 /* Take the address of the thing to which we will bind the 8975 reference. */ 8976 expr = cp_build_addr_expr (expr, complain); 8977 if (expr == error_mark_node) 8978 return error_mark_node; 8979 8980 /* Convert it to a pointer to the type referred to by the 8981 reference. This will adjust the pointer if a derived to 8982 base conversion is being performed. */ 8983 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)), 8984 expr, complain); 8985 /* Convert the pointer to the desired reference type. */ 8986 return build_nop (ref_type, expr); 8987 } 8988 8989 case ck_lvalue: 8990 return decay_conversion (expr, complain); 8991 8992 case ck_fnptr: 8993 /* ??? Should the address of a transaction-safe pointer point to the TM 8994 clone, and this conversion look up the primary function? */ 8995 return build_nop (totype, expr); 8996 8997 case ck_qual: 8998 /* Warn about deprecated conversion if appropriate. */ 8999 if (complain & tf_warning) 9000 { 9001 string_conv_p (totype, expr, 1); 9002 maybe_warn_array_conv (loc, convs, expr); 9003 } 9004 break; 9005 9006 case ck_ptr: 9007 if (convs->base_p) 9008 expr = convert_to_base (expr, totype, !c_cast_p, 9009 /*nonnull=*/false, complain); 9010 return build_nop (totype, expr); 9011 9012 case ck_pmem: 9013 return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false, 9014 c_cast_p, complain); 9015 9016 default: 9017 break; 9018 } 9019 9020 if (convs->check_narrowing 9021 && !check_narrowing (totype, expr, complain, 9022 convs->check_narrowing_const_only)) 9023 return error_mark_node; 9024 9025 warning_sentinel w (warn_zero_as_null_pointer_constant); 9026 if (issue_conversion_warnings) 9027 expr = cp_convert_and_check (totype, expr, complain); 9028 else 9029 { 9030 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) 9031 expr = TREE_OPERAND (expr, 0); 9032 expr = cp_convert (totype, expr, complain); 9033 } 9034 9035 return expr; 9036 } 9037 9038 /* Return true if converting FROM to TO is unsafe in a template. */ 9039 9040 static bool 9041 conv_unsafe_in_template_p (tree to, tree from) 9042 { 9043 /* Converting classes involves TARGET_EXPR. */ 9044 if (CLASS_TYPE_P (to) || CLASS_TYPE_P (from)) 9045 return true; 9046 9047 /* Converting real to integer produces FIX_TRUNC_EXPR which tsubst 9048 doesn't handle. */ 9049 if (SCALAR_FLOAT_TYPE_P (from) && INTEGRAL_OR_ENUMERATION_TYPE_P (to)) 9050 return true; 9051 9052 /* Converting integer to real isn't a trivial conversion, either. */ 9053 if (INTEGRAL_OR_ENUMERATION_TYPE_P (from) && SCALAR_FLOAT_TYPE_P (to)) 9054 return true; 9055 9056 return false; 9057 } 9058 9059 /* Wrapper for convert_like_internal that handles creating 9060 IMPLICIT_CONV_EXPR. */ 9061 9062 static tree 9063 convert_like (conversion *convs, tree expr, tree fn, int argnum, 9064 bool issue_conversion_warnings, bool c_cast_p, bool nested_p, 9065 tsubst_flags_t complain) 9066 { 9067 /* Creating &TARGET_EXPR<> in a template breaks when substituting, 9068 and creating a CALL_EXPR in a template breaks in finish_call_expr 9069 so use an IMPLICIT_CONV_EXPR for this conversion. We would have 9070 created such codes e.g. when calling a user-defined conversion 9071 function. */ 9072 tree conv_expr = NULL_TREE; 9073 if (processing_template_decl 9074 && convs->kind != ck_identity 9075 && conv_unsafe_in_template_p (convs->type, TREE_TYPE (expr))) 9076 { 9077 conv_expr = build1 (IMPLICIT_CONV_EXPR, convs->type, expr); 9078 if (convs->kind != ck_ref_bind) 9079 conv_expr = convert_from_reference (conv_expr); 9080 if (!convs->bad_p) 9081 return conv_expr; 9082 /* Do the normal processing to give the bad_p errors. But we still 9083 need to return the IMPLICIT_CONV_EXPR, unless we're returning 9084 error_mark_node. */ 9085 } 9086 expr = convert_like_internal (convs, expr, fn, argnum, 9087 issue_conversion_warnings, c_cast_p, 9088 nested_p, complain); 9089 if (expr == error_mark_node) 9090 return error_mark_node; 9091 return conv_expr ? conv_expr : expr; 9092 } 9093 9094 /* Convenience wrapper for convert_like. */ 9095 9096 static inline tree 9097 convert_like (conversion *convs, tree expr, tsubst_flags_t complain) 9098 { 9099 return convert_like (convs, expr, NULL_TREE, 0, 9100 /*issue_conversion_warnings=*/true, 9101 /*c_cast_p=*/false, /*nested_p=*/false, complain); 9102 } 9103 9104 /* Convenience wrapper for convert_like. */ 9105 9106 static inline tree 9107 convert_like_with_context (conversion *convs, tree expr, tree fn, int argnum, 9108 tsubst_flags_t complain) 9109 { 9110 return convert_like (convs, expr, fn, argnum, 9111 /*issue_conversion_warnings=*/true, 9112 /*c_cast_p=*/false, /*nested_p=*/false, complain); 9113 } 9114 9115 /* ARG is being passed to a varargs function. Perform any conversions 9116 required. Return the converted value. */ 9117 9118 tree 9119 convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain) 9120 { 9121 tree arg_type = TREE_TYPE (arg); 9122 location_t loc = cp_expr_loc_or_input_loc (arg); 9123 9124 /* [expr.call] 9125 9126 If the argument has integral or enumeration type that is subject 9127 to the integral promotions (_conv.prom_), or a floating-point 9128 type that is subject to the floating-point promotion 9129 (_conv.fpprom_), the value of the argument is converted to the 9130 promoted type before the call. */ 9131 if (SCALAR_FLOAT_TYPE_P (arg_type) 9132 && (TYPE_PRECISION (arg_type) 9133 < TYPE_PRECISION (double_type_node)) 9134 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type)) 9135 && !extended_float_type_p (arg_type)) 9136 { 9137 if ((complain & tf_warning) 9138 && warn_double_promotion && !c_inhibit_evaluation_warnings) 9139 warning_at (loc, OPT_Wdouble_promotion, 9140 "implicit conversion from %qH to %qI when passing " 9141 "argument to function", 9142 arg_type, double_type_node); 9143 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR) 9144 arg = TREE_OPERAND (arg, 0); 9145 arg = mark_rvalue_use (arg); 9146 arg = convert_to_real_nofold (double_type_node, arg); 9147 } 9148 else if (NULLPTR_TYPE_P (arg_type)) 9149 { 9150 arg = mark_rvalue_use (arg); 9151 if (TREE_SIDE_EFFECTS (arg)) 9152 { 9153 warning_sentinel w(warn_unused_result); 9154 arg = cp_build_compound_expr (arg, null_pointer_node, complain); 9155 } 9156 else 9157 arg = null_pointer_node; 9158 } 9159 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type)) 9160 { 9161 if (SCOPED_ENUM_P (arg_type)) 9162 { 9163 tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg, 9164 complain); 9165 prom = cp_perform_integral_promotions (prom, complain); 9166 if (abi_version_crosses (6) 9167 && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type) 9168 && (complain & tf_warning)) 9169 warning_at (loc, OPT_Wabi, "scoped enum %qT passed through %<...%>" 9170 " as %qT before %<-fabi-version=6%>, %qT after", 9171 arg_type, 9172 TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type)); 9173 if (!abi_version_at_least (6)) 9174 arg = prom; 9175 } 9176 else 9177 arg = cp_perform_integral_promotions (arg, complain); 9178 } 9179 else 9180 /* [expr.call] 9181 9182 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer 9183 standard conversions are performed. */ 9184 arg = decay_conversion (arg, complain); 9185 9186 arg = require_complete_type (arg, complain); 9187 arg_type = TREE_TYPE (arg); 9188 9189 if (arg != error_mark_node 9190 /* In a template (or ill-formed code), we can have an incomplete type 9191 even after require_complete_type, in which case we don't know 9192 whether it has trivial copy or not. */ 9193 && COMPLETE_TYPE_P (arg_type) 9194 && !cp_unevaluated_operand) 9195 { 9196 /* [expr.call] 5.2.2/7: 9197 Passing a potentially-evaluated argument of class type (Clause 9) 9198 with a non-trivial copy constructor or a non-trivial destructor 9199 with no corresponding parameter is conditionally-supported, with 9200 implementation-defined semantics. 9201 9202 We support it as pass-by-invisible-reference, just like a normal 9203 value parameter. 9204 9205 If the call appears in the context of a sizeof expression, 9206 it is not potentially-evaluated. */ 9207 if (type_has_nontrivial_copy_init (arg_type) 9208 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type)) 9209 { 9210 arg = force_rvalue (arg, complain); 9211 if (complain & tf_warning) 9212 warning (OPT_Wconditionally_supported, 9213 "passing objects of non-trivially-copyable " 9214 "type %q#T through %<...%> is conditionally supported", 9215 arg_type); 9216 return build1 (ADDR_EXPR, build_reference_type (arg_type), arg); 9217 } 9218 /* Build up a real lvalue-to-rvalue conversion in case the 9219 copy constructor is trivial but not callable. */ 9220 else if (CLASS_TYPE_P (arg_type)) 9221 force_rvalue (arg, complain); 9222 9223 } 9224 9225 return arg; 9226 } 9227 9228 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */ 9229 9230 tree 9231 build_x_va_arg (location_t loc, tree expr, tree type) 9232 { 9233 if (processing_template_decl) 9234 { 9235 tree r = build_min (VA_ARG_EXPR, type, expr); 9236 SET_EXPR_LOCATION (r, loc); 9237 return r; 9238 } 9239 9240 type = complete_type_or_else (type, NULL_TREE); 9241 9242 if (expr == error_mark_node || !type) 9243 return error_mark_node; 9244 9245 expr = mark_lvalue_use (expr); 9246 9247 if (TYPE_REF_P (type)) 9248 { 9249 error ("cannot receive reference type %qT through %<...%>", type); 9250 return error_mark_node; 9251 } 9252 9253 if (type_has_nontrivial_copy_init (type) 9254 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 9255 { 9256 /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat 9257 it as pass by invisible reference. */ 9258 warning_at (loc, OPT_Wconditionally_supported, 9259 "receiving objects of non-trivially-copyable type %q#T " 9260 "through %<...%> is conditionally-supported", type); 9261 9262 tree ref = cp_build_reference_type (type, false); 9263 expr = build_va_arg (loc, expr, ref); 9264 return convert_from_reference (expr); 9265 } 9266 9267 tree ret = build_va_arg (loc, expr, type); 9268 if (CLASS_TYPE_P (type)) 9269 /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to 9270 know how to handle it. */ 9271 ret = get_target_expr (ret); 9272 return ret; 9273 } 9274 9275 /* TYPE has been given to va_arg. Apply the default conversions which 9276 would have happened when passed via ellipsis. Return the promoted 9277 type, or the passed type if there is no change. */ 9278 9279 tree 9280 cxx_type_promotes_to (tree type) 9281 { 9282 tree promote; 9283 9284 /* Perform the array-to-pointer and function-to-pointer 9285 conversions. */ 9286 type = type_decays_to (type); 9287 9288 promote = type_promotes_to (type); 9289 if (same_type_p (type, promote)) 9290 promote = type; 9291 9292 return promote; 9293 } 9294 9295 /* ARG is a default argument expression being passed to a parameter of 9296 the indicated TYPE, which is a parameter to FN. PARMNUM is the 9297 zero-based argument number. Do any required conversions. Return 9298 the converted value. */ 9299 9300 static GTY(()) vec<tree, va_gc> *default_arg_context; 9301 void 9302 push_defarg_context (tree fn) 9303 { vec_safe_push (default_arg_context, fn); } 9304 9305 void 9306 pop_defarg_context (void) 9307 { default_arg_context->pop (); } 9308 9309 tree 9310 convert_default_arg (tree type, tree arg, tree fn, int parmnum, 9311 tsubst_flags_t complain) 9312 { 9313 int i; 9314 tree t; 9315 9316 /* See through clones. */ 9317 fn = DECL_ORIGIN (fn); 9318 /* And inheriting ctors. */ 9319 if (flag_new_inheriting_ctors) 9320 fn = strip_inheriting_ctors (fn); 9321 9322 /* Detect recursion. */ 9323 FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t) 9324 if (t == fn) 9325 { 9326 if (complain & tf_error) 9327 error ("recursive evaluation of default argument for %q#D", fn); 9328 return error_mark_node; 9329 } 9330 9331 /* If the ARG is an unparsed default argument expression, the 9332 conversion cannot be performed. */ 9333 if (TREE_CODE (arg) == DEFERRED_PARSE) 9334 { 9335 if (complain & tf_error) 9336 error ("call to %qD uses the default argument for parameter %P, which " 9337 "is not yet defined", fn, parmnum); 9338 return error_mark_node; 9339 } 9340 9341 push_defarg_context (fn); 9342 9343 if (fn && DECL_TEMPLATE_INFO (fn)) 9344 arg = tsubst_default_argument (fn, parmnum, type, arg, complain); 9345 9346 /* Due to: 9347 9348 [dcl.fct.default] 9349 9350 The names in the expression are bound, and the semantic 9351 constraints are checked, at the point where the default 9352 expressions appears. 9353 9354 we must not perform access checks here. */ 9355 push_deferring_access_checks (dk_no_check); 9356 /* We must make a copy of ARG, in case subsequent processing 9357 alters any part of it. */ 9358 arg = break_out_target_exprs (arg, /*clear location*/true); 9359 9360 arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT, 9361 ICR_DEFAULT_ARGUMENT, fn, parmnum, 9362 complain); 9363 arg = convert_for_arg_passing (type, arg, complain); 9364 pop_deferring_access_checks(); 9365 9366 pop_defarg_context (); 9367 9368 return arg; 9369 } 9370 9371 /* Returns the type which will really be used for passing an argument of 9372 type TYPE. */ 9373 9374 tree 9375 type_passed_as (tree type) 9376 { 9377 /* Pass classes with copy ctors by invisible reference. */ 9378 if (TREE_ADDRESSABLE (type)) 9379 type = build_reference_type (type); 9380 else if (targetm.calls.promote_prototypes (NULL_TREE) 9381 && INTEGRAL_TYPE_P (type) 9382 && COMPLETE_TYPE_P (type) 9383 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node))) 9384 type = integer_type_node; 9385 9386 return type; 9387 } 9388 9389 /* Actually perform the appropriate conversion. */ 9390 9391 tree 9392 convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain) 9393 { 9394 tree bitfield_type; 9395 9396 /* If VAL is a bitfield, then -- since it has already been converted 9397 to TYPE -- it cannot have a precision greater than TYPE. 9398 9399 If it has a smaller precision, we must widen it here. For 9400 example, passing "int f:3;" to a function expecting an "int" will 9401 not result in any conversion before this point. 9402 9403 If the precision is the same we must not risk widening. For 9404 example, the COMPONENT_REF for a 32-bit "long long" bitfield will 9405 often have type "int", even though the C++ type for the field is 9406 "long long". If the value is being passed to a function 9407 expecting an "int", then no conversions will be required. But, 9408 if we call convert_bitfield_to_declared_type, the bitfield will 9409 be converted to "long long". */ 9410 bitfield_type = is_bitfield_expr_with_lowered_type (val); 9411 if (bitfield_type 9412 && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)) 9413 val = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type), val); 9414 9415 if (val == error_mark_node) 9416 ; 9417 /* Pass classes with copy ctors by invisible reference. */ 9418 else if (TREE_ADDRESSABLE (type)) 9419 val = build1 (ADDR_EXPR, build_reference_type (type), val); 9420 else if (targetm.calls.promote_prototypes (NULL_TREE) 9421 && INTEGRAL_TYPE_P (type) 9422 && COMPLETE_TYPE_P (type) 9423 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node))) 9424 val = cp_perform_integral_promotions (val, complain); 9425 if (complain & tf_warning) 9426 { 9427 if (warn_suggest_attribute_format) 9428 { 9429 tree rhstype = TREE_TYPE (val); 9430 const enum tree_code coder = TREE_CODE (rhstype); 9431 const enum tree_code codel = TREE_CODE (type); 9432 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE) 9433 && coder == codel 9434 && check_missing_format_attribute (type, rhstype)) 9435 warning (OPT_Wsuggest_attribute_format, 9436 "argument of function call might be a candidate " 9437 "for a format attribute"); 9438 } 9439 maybe_warn_parm_abi (type, cp_expr_loc_or_input_loc (val)); 9440 } 9441 9442 if (complain & tf_warning) 9443 warn_for_address_of_packed_member (type, val); 9444 9445 return val; 9446 } 9447 9448 /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for 9449 which just decay_conversion or no conversions at all should be done. 9450 This is true for some builtins which don't act like normal functions. 9451 Return 2 if just decay_conversion and removal of excess precision should 9452 be done, 1 if just decay_conversion. Return 3 for special treatment of 9453 the 3rd argument for __builtin_*_overflow_p. Return 4 for special 9454 treatment of the 1st argument for 9455 __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */ 9456 9457 int 9458 magic_varargs_p (tree fn) 9459 { 9460 if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL) 9461 switch (DECL_FUNCTION_CODE (fn)) 9462 { 9463 case BUILT_IN_CLASSIFY_TYPE: 9464 case BUILT_IN_CONSTANT_P: 9465 case BUILT_IN_NEXT_ARG: 9466 case BUILT_IN_VA_START: 9467 return 1; 9468 9469 case BUILT_IN_ADD_OVERFLOW_P: 9470 case BUILT_IN_SUB_OVERFLOW_P: 9471 case BUILT_IN_MUL_OVERFLOW_P: 9472 return 3; 9473 9474 case BUILT_IN_ISFINITE: 9475 case BUILT_IN_ISINF: 9476 case BUILT_IN_ISINF_SIGN: 9477 case BUILT_IN_ISNAN: 9478 case BUILT_IN_ISNORMAL: 9479 case BUILT_IN_FPCLASSIFY: 9480 return 2; 9481 9482 case BUILT_IN_CLZG: 9483 case BUILT_IN_CTZG: 9484 case BUILT_IN_CLRSBG: 9485 case BUILT_IN_FFSG: 9486 case BUILT_IN_PARITYG: 9487 case BUILT_IN_POPCOUNTG: 9488 return 4; 9489 9490 default: 9491 return lookup_attribute ("type generic", 9492 TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0; 9493 } 9494 9495 return 0; 9496 } 9497 9498 /* Returns the decl of the dispatcher function if FN is a function version. */ 9499 9500 tree 9501 get_function_version_dispatcher (tree fn) 9502 { 9503 tree dispatcher_decl = NULL; 9504 9505 if (DECL_LOCAL_DECL_P (fn)) 9506 fn = DECL_LOCAL_DECL_ALIAS (fn); 9507 9508 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL 9509 && DECL_FUNCTION_VERSIONED (fn)); 9510 9511 gcc_assert (targetm.get_function_versions_dispatcher); 9512 dispatcher_decl = targetm.get_function_versions_dispatcher (fn); 9513 9514 if (dispatcher_decl == NULL) 9515 { 9516 error_at (input_location, "use of multiversioned function " 9517 "without a default"); 9518 return NULL; 9519 } 9520 9521 retrofit_lang_decl (dispatcher_decl); 9522 gcc_assert (dispatcher_decl != NULL); 9523 return dispatcher_decl; 9524 } 9525 9526 /* fn is a function version dispatcher that is marked used. Mark all the 9527 semantically identical function versions it will dispatch as used. */ 9528 9529 void 9530 mark_versions_used (tree fn) 9531 { 9532 struct cgraph_node *node; 9533 struct cgraph_function_version_info *node_v; 9534 struct cgraph_function_version_info *it_v; 9535 9536 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL); 9537 9538 node = cgraph_node::get (fn); 9539 if (node == NULL) 9540 return; 9541 9542 gcc_assert (node->dispatcher_function); 9543 9544 node_v = node->function_version (); 9545 if (node_v == NULL) 9546 return; 9547 9548 /* All semantically identical versions are chained. Traverse and mark each 9549 one of them as used. */ 9550 it_v = node_v->next; 9551 while (it_v != NULL) 9552 { 9553 mark_used (it_v->this_node->decl); 9554 it_v = it_v->next; 9555 } 9556 } 9557 9558 /* Build a call to "the copy constructor" for the type of A, even if it 9559 wouldn't be selected by normal overload resolution. Used for 9560 diagnostics. */ 9561 9562 static tree 9563 call_copy_ctor (tree a, tsubst_flags_t complain) 9564 { 9565 tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (a)); 9566 tree binfo = TYPE_BINFO (ctype); 9567 tree copy = get_copy_ctor (ctype, complain); 9568 copy = build_baselink (binfo, binfo, copy, NULL_TREE); 9569 tree ob = build_dummy_object (ctype); 9570 releasing_vec args (make_tree_vector_single (a)); 9571 tree r = build_new_method_call (ob, copy, &args, NULL_TREE, 9572 LOOKUP_NORMAL, NULL, complain); 9573 return r; 9574 } 9575 9576 /* Return the base constructor corresponding to COMPLETE_CTOR or NULL_TREE. */ 9577 9578 static tree 9579 base_ctor_for (tree complete_ctor) 9580 { 9581 tree clone; 9582 FOR_EACH_CLONE (clone, DECL_CLONED_FUNCTION (complete_ctor)) 9583 if (DECL_BASE_CONSTRUCTOR_P (clone)) 9584 return clone; 9585 return NULL_TREE; 9586 } 9587 9588 /* Try to make EXP suitable to be used as the initializer for a base subobject, 9589 and return whether we were successful. EXP must have already been cleared 9590 by unsafe_copy_elision_p{,_opt}. */ 9591 9592 static bool 9593 make_base_init_ok (tree exp) 9594 { 9595 if (TREE_CODE (exp) == TARGET_EXPR) 9596 exp = TARGET_EXPR_INITIAL (exp); 9597 while (TREE_CODE (exp) == COMPOUND_EXPR) 9598 exp = TREE_OPERAND (exp, 1); 9599 if (TREE_CODE (exp) == COND_EXPR) 9600 { 9601 bool ret = make_base_init_ok (TREE_OPERAND (exp, 2)); 9602 if (tree op1 = TREE_OPERAND (exp, 1)) 9603 { 9604 bool r1 = make_base_init_ok (op1); 9605 /* If unsafe_copy_elision_p was false, the arms should match. */ 9606 gcc_assert (r1 == ret); 9607 } 9608 return ret; 9609 } 9610 if (TREE_CODE (exp) != AGGR_INIT_EXPR) 9611 /* A trivial copy is OK. */ 9612 return true; 9613 if (!AGGR_INIT_VIA_CTOR_P (exp)) 9614 /* unsafe_copy_elision_p_opt must have said this is OK. */ 9615 return true; 9616 tree fn = cp_get_callee_fndecl_nofold (exp); 9617 if (DECL_BASE_CONSTRUCTOR_P (fn)) 9618 return true; 9619 gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (fn)); 9620 fn = base_ctor_for (fn); 9621 if (!fn || DECL_HAS_VTT_PARM_P (fn)) 9622 /* The base constructor has more parameters, so we can't just change the 9623 call target. It would be possible to splice in the appropriate 9624 arguments, but probably not worth the complexity. */ 9625 return false; 9626 mark_used (fn); 9627 AGGR_INIT_EXPR_FN (exp) = build_address (fn); 9628 return true; 9629 } 9630 9631 /* Return 2 if T refers to a base, 1 if a potentially-overlapping field, 9632 neither of which can be used for return by invisible reference. We avoid 9633 doing C++17 mandatory copy elision for either of these cases. 9634 9635 This returns non-zero even if the type of T has no tail padding that other 9636 data could be allocated into, because that depends on the particular ABI. 9637 unsafe_copy_elision_p_opt does consider whether there is padding. */ 9638 9639 int 9640 unsafe_return_slot_p (tree t) 9641 { 9642 /* Check empty bases separately, they don't have fields. */ 9643 if (is_empty_base_ref (t)) 9644 return 2; 9645 9646 /* A delegating constructor might be used to initialize a base. */ 9647 if (current_function_decl 9648 && DECL_CONSTRUCTOR_P (current_function_decl) 9649 && (t == current_class_ref 9650 || tree_strip_nop_conversions (t) == current_class_ptr)) 9651 return 2; 9652 9653 STRIP_NOPS (t); 9654 if (TREE_CODE (t) == ADDR_EXPR) 9655 t = TREE_OPERAND (t, 0); 9656 if (TREE_CODE (t) == COMPONENT_REF) 9657 t = TREE_OPERAND (t, 1); 9658 if (TREE_CODE (t) != FIELD_DECL) 9659 return false; 9660 if (!CLASS_TYPE_P (TREE_TYPE (t))) 9661 /* The middle-end will do the right thing for scalar types. */ 9662 return false; 9663 if (DECL_FIELD_IS_BASE (t)) 9664 return 2; 9665 if (lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (t))) 9666 return 1; 9667 return 0; 9668 } 9669 9670 /* True IFF EXP is a prvalue that represents return by invisible reference. */ 9671 9672 static bool 9673 init_by_return_slot_p (tree exp) 9674 { 9675 /* Copy elision only happens with a TARGET_EXPR. */ 9676 if (TREE_CODE (exp) != TARGET_EXPR) 9677 return false; 9678 tree init = TARGET_EXPR_INITIAL (exp); 9679 /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */ 9680 while (TREE_CODE (init) == COMPOUND_EXPR) 9681 init = TREE_OPERAND (init, 1); 9682 if (TREE_CODE (init) == COND_EXPR) 9683 { 9684 /* We'll end up copying from each of the arms of the COND_EXPR directly 9685 into the target, so look at them. */ 9686 if (tree op = TREE_OPERAND (init, 1)) 9687 if (init_by_return_slot_p (op)) 9688 return true; 9689 return init_by_return_slot_p (TREE_OPERAND (init, 2)); 9690 } 9691 return (TREE_CODE (init) == AGGR_INIT_EXPR 9692 && !AGGR_INIT_VIA_CTOR_P (init)); 9693 } 9694 9695 /* We can't elide a copy from a function returning by value to a 9696 potentially-overlapping subobject, as the callee might clobber tail padding. 9697 Return true iff this could be that case. 9698 9699 Places that use this function (or _opt) to decide to elide a copy should 9700 probably use make_safe_copy_elision instead. */ 9701 9702 bool 9703 unsafe_copy_elision_p (tree target, tree exp) 9704 { 9705 return unsafe_return_slot_p (target) && init_by_return_slot_p (exp); 9706 } 9707 9708 /* As above, but for optimization allow more cases that are actually safe. */ 9709 9710 static bool 9711 unsafe_copy_elision_p_opt (tree target, tree exp) 9712 { 9713 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (exp)); 9714 /* It's safe to elide the copy for a class with no tail padding. */ 9715 if (!is_empty_class (type) 9716 && tree_int_cst_equal (TYPE_SIZE (type), CLASSTYPE_SIZE (type))) 9717 return false; 9718 return unsafe_copy_elision_p (target, exp); 9719 } 9720 9721 /* Try to make EXP suitable to be used as the initializer for TARGET, 9722 and return whether we were successful. */ 9723 9724 bool 9725 make_safe_copy_elision (tree target, tree exp) 9726 { 9727 int uns = unsafe_return_slot_p (target); 9728 if (!uns) 9729 return true; 9730 if (init_by_return_slot_p (exp)) 9731 return false; 9732 if (uns == 1) 9733 return true; 9734 return make_base_init_ok (exp); 9735 } 9736 9737 /* True IFF the result of the conversion C is a prvalue. */ 9738 9739 static bool 9740 conv_is_prvalue (conversion *c) 9741 { 9742 if (c->kind == ck_rvalue) 9743 return true; 9744 if (c->kind == ck_base && c->need_temporary_p) 9745 return true; 9746 if (c->kind == ck_user && !TYPE_REF_P (c->type)) 9747 return true; 9748 if (c->kind == ck_identity && c->u.expr 9749 && TREE_CODE (c->u.expr) == TARGET_EXPR) 9750 return true; 9751 9752 return false; 9753 } 9754 9755 /* True iff C is a conversion that binds a reference to a prvalue. */ 9756 9757 static bool 9758 conv_binds_ref_to_prvalue (conversion *c) 9759 { 9760 if (c->kind != ck_ref_bind) 9761 return false; 9762 if (c->need_temporary_p) 9763 return true; 9764 9765 return conv_is_prvalue (next_conversion (c)); 9766 } 9767 9768 /* True iff EXPR represents a (subobject of a) temporary. */ 9769 9770 static bool 9771 expr_represents_temporary_p (tree expr) 9772 { 9773 while (handled_component_p (expr)) 9774 expr = TREE_OPERAND (expr, 0); 9775 return TREE_CODE (expr) == TARGET_EXPR; 9776 } 9777 9778 /* True iff C is a conversion that binds a reference to a temporary. 9779 This is a superset of conv_binds_ref_to_prvalue: here we're also 9780 interested in xvalues. */ 9781 9782 static bool 9783 conv_binds_ref_to_temporary (conversion *c) 9784 { 9785 if (conv_binds_ref_to_prvalue (c)) 9786 return true; 9787 if (c->kind != ck_ref_bind) 9788 return false; 9789 c = next_conversion (c); 9790 /* This is the case for 9791 struct Base {}; 9792 struct Derived : Base {}; 9793 const Base& b(Derived{}); 9794 where we bind 'b' to the Base subobject of a temporary object of type 9795 Derived. The subobject is an xvalue; the whole object is a prvalue. 9796 9797 The ck_base doesn't have to be present for cases like X{}.m. */ 9798 if (c->kind == ck_base) 9799 c = next_conversion (c); 9800 if (c->kind == ck_identity && c->u.expr 9801 && expr_represents_temporary_p (c->u.expr)) 9802 return true; 9803 return false; 9804 } 9805 9806 /* Return tristate::TS_TRUE if converting EXPR to a reference type TYPE binds 9807 the reference to a temporary. Return tristate::TS_FALSE if converting 9808 EXPR to a reference type TYPE doesn't bind the reference to a temporary. If 9809 the conversion is invalid or bad, return tristate::TS_UNKNOWN. DIRECT_INIT_P 9810 says whether the conversion should be done in direct- or copy-initialization 9811 context. */ 9812 9813 tristate 9814 ref_conv_binds_to_temporary (tree type, tree expr, bool direct_init_p/*=false*/) 9815 { 9816 gcc_assert (TYPE_REF_P (type)); 9817 9818 conversion_obstack_sentinel cos; 9819 9820 const int flags = direct_init_p ? LOOKUP_NORMAL : LOOKUP_IMPLICIT; 9821 conversion *conv = implicit_conversion (type, TREE_TYPE (expr), expr, 9822 /*c_cast_p=*/false, flags, tf_none); 9823 tristate ret (tristate::TS_UNKNOWN); 9824 if (conv && !conv->bad_p) 9825 ret = tristate (conv_binds_ref_to_temporary (conv)); 9826 9827 return ret; 9828 } 9829 9830 /* Call the trivial destructor for INSTANCE, which can be either an lvalue of 9831 class type or a pointer to class type. If NO_PTR_DEREF is true and 9832 INSTANCE has pointer type, clobber the pointer rather than what it points 9833 to. */ 9834 9835 tree 9836 build_trivial_dtor_call (tree instance, bool no_ptr_deref) 9837 { 9838 gcc_assert (!is_dummy_object (instance)); 9839 9840 if (!flag_lifetime_dse) 9841 { 9842 no_clobber: 9843 return fold_convert (void_type_node, instance); 9844 } 9845 9846 if (INDIRECT_TYPE_P (TREE_TYPE (instance)) 9847 && (!no_ptr_deref || TYPE_REF_P (TREE_TYPE (instance)))) 9848 { 9849 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (instance)))) 9850 goto no_clobber; 9851 instance = cp_build_fold_indirect_ref (instance); 9852 } 9853 9854 /* A trivial destructor should still clobber the object. */ 9855 tree clobber = build_clobber (TREE_TYPE (instance), CLOBBER_OBJECT_END); 9856 return build2 (MODIFY_EXPR, void_type_node, 9857 instance, clobber); 9858 } 9859 9860 /* Return true if in an immediate function context, or an unevaluated operand, 9861 or a default argument/member initializer, or a subexpression of an immediate 9862 invocation. */ 9863 9864 bool 9865 in_immediate_context () 9866 { 9867 return (cp_unevaluated_operand != 0 9868 || (current_function_decl != NULL_TREE 9869 && DECL_IMMEDIATE_FUNCTION_P (current_function_decl)) 9870 /* DR 2631: default args and DMI aren't immediately evaluated. 9871 Return true here so immediate_invocation_p returns false. */ 9872 || current_binding_level->kind == sk_function_parms 9873 || current_binding_level->kind == sk_template_parms 9874 || parsing_nsdmi () 9875 || in_consteval_if_p); 9876 } 9877 9878 /* Return true if a call to FN with number of arguments NARGS 9879 is an immediate invocation. */ 9880 9881 bool 9882 immediate_invocation_p (tree fn) 9883 { 9884 return (TREE_CODE (fn) == FUNCTION_DECL 9885 && DECL_IMMEDIATE_FUNCTION_P (fn) 9886 && !in_immediate_context ()); 9887 } 9888 9889 /* Subroutine of the various build_*_call functions. Overload resolution 9890 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly. 9891 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a 9892 bitmask of various LOOKUP_* flags which apply to the call itself. */ 9893 9894 static tree 9895 build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain) 9896 { 9897 tree fn = cand->fn; 9898 const vec<tree, va_gc> *args = cand->args; 9899 tree first_arg = cand->first_arg; 9900 conversion **convs = cand->convs; 9901 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn)); 9902 int parmlen; 9903 tree val; 9904 int nargs; 9905 tree *argarray; 9906 bool already_used = false; 9907 9908 /* In a template, there is no need to perform all of the work that 9909 is normally done. We are only interested in the type of the call 9910 expression, i.e., the return type of the function. Any semantic 9911 errors will be deferred until the template is instantiated. */ 9912 if (processing_template_decl) 9913 { 9914 if (undeduced_auto_decl (fn)) 9915 mark_used (fn, complain); 9916 else 9917 /* Otherwise set TREE_USED for the benefit of -Wunused-function. 9918 See PR80598. */ 9919 TREE_USED (fn) = 1; 9920 9921 tree return_type = TREE_TYPE (TREE_TYPE (fn)); 9922 tree callee; 9923 if (first_arg == NULL_TREE) 9924 { 9925 callee = build_addr_func (fn, complain); 9926 if (callee == error_mark_node) 9927 return error_mark_node; 9928 } 9929 else 9930 { 9931 callee = build_baselink (cand->conversion_path, cand->access_path, 9932 fn, NULL_TREE); 9933 callee = build_min (COMPONENT_REF, TREE_TYPE (fn), 9934 first_arg, callee, NULL_TREE); 9935 } 9936 9937 tree expr = build_call_vec (return_type, callee, args); 9938 SET_EXPR_LOCATION (expr, input_location); 9939 if (TREE_THIS_VOLATILE (fn) && cfun) 9940 current_function_returns_abnormally = 1; 9941 if (immediate_invocation_p (fn)) 9942 { 9943 tree obj_arg = NULL_TREE, exprimm = expr; 9944 if (DECL_CONSTRUCTOR_P (fn)) 9945 obj_arg = first_arg; 9946 if (obj_arg 9947 && is_dummy_object (obj_arg) 9948 && !type_dependent_expression_p (obj_arg)) 9949 { 9950 exprimm = build_cplus_new (DECL_CONTEXT (fn), expr, complain); 9951 obj_arg = NULL_TREE; 9952 } 9953 /* Look through *(const T *)&obj. */ 9954 else if (obj_arg && INDIRECT_REF_P (obj_arg)) 9955 { 9956 tree addr = TREE_OPERAND (obj_arg, 0); 9957 STRIP_NOPS (addr); 9958 if (TREE_CODE (addr) == ADDR_EXPR) 9959 { 9960 tree typeo = TREE_TYPE (obj_arg); 9961 tree typei = TREE_TYPE (TREE_OPERAND (addr, 0)); 9962 if (same_type_ignoring_top_level_qualifiers_p (typeo, typei)) 9963 obj_arg = TREE_OPERAND (addr, 0); 9964 } 9965 } 9966 fold_non_dependent_expr (exprimm, complain, 9967 /*manifestly_const_eval=*/true, 9968 obj_arg); 9969 } 9970 return convert_from_reference (expr); 9971 } 9972 9973 /* Give any warnings we noticed during overload resolution. */ 9974 if (cand->warnings && (complain & tf_warning)) 9975 { 9976 struct candidate_warning *w; 9977 for (w = cand->warnings; w; w = w->next) 9978 joust (cand, w->loser, 1, complain); 9979 } 9980 9981 /* Core issue 2327: P0135 doesn't say how to handle the case where the 9982 argument to the copy constructor ends up being a prvalue after 9983 conversion. Let's do the normal processing, but pretend we aren't 9984 actually using the copy constructor. */ 9985 bool force_elide = false; 9986 if (cxx_dialect >= cxx17 9987 && cand->num_convs == 1 9988 && DECL_COMPLETE_CONSTRUCTOR_P (fn) 9989 && (DECL_COPY_CONSTRUCTOR_P (fn) 9990 || DECL_MOVE_CONSTRUCTOR_P (fn)) 9991 && !unsafe_return_slot_p (first_arg) 9992 && conv_binds_ref_to_prvalue (convs[0])) 9993 { 9994 force_elide = true; 9995 goto not_really_used; 9996 } 9997 9998 /* OK, we're actually calling this inherited constructor; set its deletedness 9999 appropriately. We can get away with doing this here because calling is 10000 the only way to refer to a constructor. */ 10001 if (DECL_INHERITED_CTOR (fn) 10002 && !deduce_inheriting_ctor (fn)) 10003 { 10004 if (complain & tf_error) 10005 mark_used (fn); 10006 return error_mark_node; 10007 } 10008 10009 /* Make =delete work with SFINAE. */ 10010 if (DECL_DELETED_FN (fn)) 10011 { 10012 if (complain & tf_error) 10013 { 10014 mark_used (fn); 10015 if (cand->next) 10016 { 10017 if (flag_diagnostics_all_candidates) 10018 print_z_candidates (input_location, cand, /*only_viable_p=*/false); 10019 else 10020 inform (input_location, 10021 "use %<-fdiagnostics-all-candidates%> to display " 10022 "considered candidates"); 10023 } 10024 } 10025 return error_mark_node; 10026 } 10027 10028 if (DECL_FUNCTION_MEMBER_P (fn)) 10029 { 10030 tree access_fn; 10031 /* If FN is a template function, two cases must be considered. 10032 For example: 10033 10034 struct A { 10035 protected: 10036 template <class T> void f(); 10037 }; 10038 template <class T> struct B { 10039 protected: 10040 void g(); 10041 }; 10042 struct C : A, B<int> { 10043 using A::f; // #1 10044 using B<int>::g; // #2 10045 }; 10046 10047 In case #1 where `A::f' is a member template, DECL_ACCESS is 10048 recorded in the primary template but not in its specialization. 10049 We check access of FN using its primary template. 10050 10051 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply 10052 because it is a member of class template B, DECL_ACCESS is 10053 recorded in the specialization `B<int>::g'. We cannot use its 10054 primary template because `B<T>::g' and `B<int>::g' may have 10055 different access. */ 10056 if (DECL_TEMPLATE_INFO (fn) 10057 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn))) 10058 access_fn = DECL_TI_TEMPLATE (fn); 10059 else 10060 access_fn = fn; 10061 if (!perform_or_defer_access_check (cand->access_path, access_fn, 10062 fn, complain)) 10063 return error_mark_node; 10064 } 10065 10066 /* If we're checking for implicit delete, don't bother with argument 10067 conversions. */ 10068 if (flags & LOOKUP_SPECULATIVE) 10069 { 10070 if (cand->viable == 1) 10071 return fn; 10072 else if (!(complain & tf_error)) 10073 /* Reject bad conversions now. */ 10074 return error_mark_node; 10075 /* else continue to get conversion error. */ 10076 } 10077 10078 not_really_used: 10079 10080 /* N3276 magic doesn't apply to nested calls. */ 10081 tsubst_flags_t decltype_flag = (complain & tf_decltype); 10082 complain &= ~tf_decltype; 10083 /* No-Cleanup doesn't apply to nested calls either. */ 10084 tsubst_flags_t no_cleanup_complain = complain; 10085 complain &= ~tf_no_cleanup; 10086 10087 /* Find maximum size of vector to hold converted arguments. */ 10088 parmlen = list_length (parm); 10089 nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0); 10090 if (parmlen > nargs) 10091 nargs = parmlen; 10092 argarray = XALLOCAVEC (tree, nargs); 10093 10094 in_consteval_if_p_temp_override icip; 10095 /* If the call is immediate function invocation, make sure 10096 taking address of immediate functions is allowed in its arguments. */ 10097 if (immediate_invocation_p (STRIP_TEMPLATE (fn))) 10098 in_consteval_if_p = true; 10099 10100 int argarray_size = 0; 10101 unsigned int arg_index = 0; 10102 int conv_index = 0; 10103 int param_index = 0; 10104 10105 auto consume_object_arg = [&arg_index, &first_arg, args]() 10106 { 10107 if (!first_arg) 10108 return (*args)[arg_index++]; 10109 tree object_arg = first_arg; 10110 first_arg = NULL_TREE; 10111 return object_arg; 10112 }; 10113 10114 /* The implicit parameters to a constructor are not considered by overload 10115 resolution, and must be of the proper type. */ 10116 if (DECL_CONSTRUCTOR_P (fn)) 10117 { 10118 tree object_arg = consume_object_arg (); 10119 argarray[argarray_size++] = build_this (object_arg); 10120 parm = TREE_CHAIN (parm); 10121 /* We should never try to call the abstract constructor. */ 10122 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn)); 10123 10124 if (DECL_HAS_VTT_PARM_P (fn)) 10125 { 10126 argarray[argarray_size++] = (*args)[arg_index]; 10127 ++arg_index; 10128 parm = TREE_CHAIN (parm); 10129 } 10130 } 10131 /* Bypass access control for 'this' parameter. */ 10132 else if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)) 10133 { 10134 tree arg = build_this (consume_object_arg ()); 10135 tree argtype = TREE_TYPE (arg); 10136 10137 if (arg == error_mark_node) 10138 return error_mark_node; 10139 if (convs[conv_index++]->bad_p) 10140 { 10141 if (complain & tf_error) 10142 { 10143 auto_diagnostic_group d; 10144 if (permerror (input_location, "passing %qT as %<this%> " 10145 "argument discards qualifiers", 10146 TREE_TYPE (argtype))) 10147 inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn); 10148 } 10149 else 10150 return error_mark_node; 10151 } 10152 10153 /* The class where FN is defined. */ 10154 tree ctx = DECL_CONTEXT (fn); 10155 10156 /* See if the function member or the whole class type is declared 10157 final and the call can be devirtualized. */ 10158 if (DECL_FINAL_P (fn) || CLASSTYPE_FINAL (ctx)) 10159 flags |= LOOKUP_NONVIRTUAL; 10160 10161 /* [class.mfct.non-static]: If a non-static member function of a class 10162 X is called for an object that is not of type X, or of a type 10163 derived from X, the behavior is undefined. 10164 10165 So we can assume that anything passed as 'this' is non-null, and 10166 optimize accordingly. */ 10167 /* Check that the base class is accessible. */ 10168 if (!accessible_base_p (TREE_TYPE (argtype), 10169 BINFO_TYPE (cand->conversion_path), true)) 10170 { 10171 if (complain & tf_error) 10172 error ("%qT is not an accessible base of %qT", 10173 BINFO_TYPE (cand->conversion_path), 10174 TREE_TYPE (argtype)); 10175 else 10176 return error_mark_node; 10177 } 10178 /* If fn was found by a using declaration, the conversion path 10179 will be to the derived class, not the base declaring fn. We 10180 must convert to the base. */ 10181 tree base_binfo = cand->conversion_path; 10182 if (BINFO_TYPE (base_binfo) != ctx) 10183 { 10184 base_binfo = lookup_base (base_binfo, ctx, ba_unique, NULL, complain); 10185 if (base_binfo == error_mark_node) 10186 return error_mark_node; 10187 } 10188 10189 /* If we know the dynamic type of the object, look up the final overrider 10190 in the BINFO. */ 10191 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0 10192 && resolves_to_fixed_type_p (arg)) 10193 { 10194 tree ov = lookup_vfn_in_binfo (DECL_VINDEX (fn), base_binfo); 10195 10196 /* And unwind base_binfo to match. If we don't find the type we're 10197 looking for in BINFO_INHERITANCE_CHAIN, we're looking at diamond 10198 inheritance; for now do a normal virtual call in that case. */ 10199 tree octx = DECL_CONTEXT (ov); 10200 tree obinfo = base_binfo; 10201 while (obinfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (obinfo), octx)) 10202 obinfo = BINFO_INHERITANCE_CHAIN (obinfo); 10203 if (obinfo) 10204 { 10205 fn = ov; 10206 base_binfo = obinfo; 10207 flags |= LOOKUP_NONVIRTUAL; 10208 } 10209 } 10210 10211 tree converted_arg = build_base_path (PLUS_EXPR, arg, 10212 base_binfo, 1, complain); 10213 10214 argarray[argarray_size++] = converted_arg; 10215 parm = TREE_CHAIN (parm); 10216 } 10217 10218 auto handle_arg = [fn, flags](tree type, 10219 tree arg, 10220 int const param_index, 10221 conversion *conv, 10222 tsubst_flags_t const arg_complain) 10223 { 10224 /* Set user_conv_p on the argument conversions, so rvalue/base handling 10225 knows not to allow any more UDCs. This needs to happen after we 10226 process cand->warnings. */ 10227 if (flags & LOOKUP_NO_CONVERSION) 10228 conv->user_conv_p = true; 10229 10230 if (arg_complain & tf_warning) 10231 maybe_warn_pessimizing_move (arg, type, /*return_p=*/false); 10232 10233 tree val = convert_like_with_context (conv, arg, fn, 10234 param_index, arg_complain); 10235 val = convert_for_arg_passing (type, val, arg_complain); 10236 return val; 10237 }; 10238 10239 if (DECL_XOBJ_MEMBER_FUNCTION_P (fn)) 10240 { 10241 gcc_assert (cand->num_convs > 0); 10242 tree object_arg = consume_object_arg (); 10243 val = handle_arg (TREE_VALUE (parm), 10244 object_arg, 10245 param_index++, 10246 convs[conv_index++], 10247 complain); 10248 10249 if (val == error_mark_node) 10250 return error_mark_node; 10251 else 10252 argarray[argarray_size++] = val; 10253 parm = TREE_CHAIN (parm); 10254 } 10255 10256 gcc_assert (first_arg == NULL_TREE); 10257 for (; arg_index < vec_safe_length (args) && parm; 10258 parm = TREE_CHAIN (parm), ++arg_index, ++param_index, ++conv_index) 10259 { 10260 tree current_arg = (*args)[arg_index]; 10261 10262 /* If the argument is NULL and used to (implicitly) instantiate a 10263 template function (and bind one of the template arguments to 10264 the type of 'long int'), we don't want to warn about passing NULL 10265 to non-pointer argument. 10266 For example, if we have this template function: 10267 10268 template<typename T> void func(T x) {} 10269 10270 we want to warn (when -Wconversion is enabled) in this case: 10271 10272 void foo() { 10273 func<int>(NULL); 10274 } 10275 10276 but not in this case: 10277 10278 void foo() { 10279 func(NULL); 10280 } 10281 */ 10282 bool const conversion_warning = !(null_node_p (current_arg) 10283 && DECL_TEMPLATE_INFO (fn) 10284 && cand->template_decl 10285 && !cand->explicit_targs); 10286 10287 tsubst_flags_t const arg_complain 10288 = conversion_warning ? complain : complain & ~tf_warning; 10289 10290 val = handle_arg (TREE_VALUE (parm), 10291 current_arg, 10292 param_index, 10293 convs[conv_index], 10294 arg_complain); 10295 10296 if (val == error_mark_node) 10297 return error_mark_node; 10298 else 10299 argarray[argarray_size++] = val; 10300 } 10301 10302 /* Default arguments */ 10303 for (; parm && parm != void_list_node; 10304 parm = TREE_CHAIN (parm), param_index++) 10305 { 10306 if (TREE_VALUE (parm) == error_mark_node) 10307 return error_mark_node; 10308 val = convert_default_arg (TREE_VALUE (parm), 10309 TREE_PURPOSE (parm), 10310 fn, param_index, 10311 complain); 10312 if (val == error_mark_node) 10313 return error_mark_node; 10314 argarray[argarray_size++] = val; 10315 } 10316 10317 /* Ellipsis */ 10318 int magic = magic_varargs_p (fn); 10319 for (; arg_index < vec_safe_length (args); ++arg_index) 10320 { 10321 tree a = (*args)[arg_index]; 10322 if ((magic == 3 && arg_index == 2) || (magic == 4 && arg_index == 0)) 10323 { 10324 /* Do no conversions for certain magic varargs. */ 10325 a = mark_type_use (a); 10326 if (TREE_CODE (a) == FUNCTION_DECL && reject_gcc_builtin (a)) 10327 return error_mark_node; 10328 } 10329 else if (magic != 0) 10330 { 10331 /* Don't truncate excess precision to the semantic type. */ 10332 if (magic == 1 && TREE_CODE (a) == EXCESS_PRECISION_EXPR) 10333 a = TREE_OPERAND (a, 0); 10334 /* For other magic varargs only do decay_conversion. */ 10335 a = decay_conversion (a, complain); 10336 } 10337 else if (DECL_CONSTRUCTOR_P (fn) 10338 && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn), 10339 TREE_TYPE (a))) 10340 { 10341 /* Avoid infinite recursion trying to call A(...). */ 10342 if (complain & tf_error) 10343 /* Try to call the actual copy constructor for a good error. */ 10344 call_copy_ctor (a, complain); 10345 return error_mark_node; 10346 } 10347 else 10348 a = convert_arg_to_ellipsis (a, complain); 10349 if (a == error_mark_node) 10350 return error_mark_node; 10351 argarray[argarray_size++] = a; 10352 } 10353 10354 gcc_assert (argarray_size <= nargs); 10355 nargs = argarray_size; 10356 icip.reset (); 10357 10358 /* Avoid performing argument transformation if warnings are disabled. 10359 When tf_warning is set and at least one of the warnings is active 10360 the check_function_arguments function might warn about something. */ 10361 10362 bool warned_p = false; 10363 if ((complain & tf_warning) 10364 && (warn_nonnull 10365 || warn_format 10366 || warn_suggest_attribute_format 10367 || warn_restrict)) 10368 { 10369 tree *fargs = (!nargs ? argarray 10370 : (tree *) alloca (nargs * sizeof (tree))); 10371 for (int j = 0; j < nargs; j++) 10372 { 10373 /* For -Wformat undo the implicit passing by hidden reference 10374 done by convert_arg_to_ellipsis. */ 10375 if (TREE_CODE (argarray[j]) == ADDR_EXPR 10376 && TYPE_REF_P (TREE_TYPE (argarray[j]))) 10377 fargs[j] = TREE_OPERAND (argarray[j], 0); 10378 else 10379 fargs[j] = argarray[j]; 10380 } 10381 10382 warned_p = check_function_arguments (input_location, fn, TREE_TYPE (fn), 10383 nargs, fargs, NULL); 10384 } 10385 10386 if (DECL_INHERITED_CTOR (fn)) 10387 { 10388 /* Check for passing ellipsis arguments to an inherited constructor. We 10389 could handle this by open-coding the inherited constructor rather than 10390 defining it, but let's not bother now. */ 10391 if (!cp_unevaluated_operand 10392 && cand->num_convs 10393 && cand->convs[cand->num_convs-1]->ellipsis_p) 10394 { 10395 if (complain & tf_error) 10396 { 10397 sorry ("passing arguments to ellipsis of inherited constructor " 10398 "%qD", cand->fn); 10399 inform (DECL_SOURCE_LOCATION (cand->fn), "declared here"); 10400 } 10401 return error_mark_node; 10402 } 10403 10404 /* A base constructor inheriting from a virtual base doesn't get the 10405 inherited arguments, just this and __vtt. */ 10406 if (ctor_omit_inherited_parms (fn)) 10407 nargs = 2; 10408 } 10409 10410 /* Avoid actually calling copy constructors and copy assignment operators, 10411 if possible. */ 10412 10413 if (!force_elide 10414 && (!flag_elide_constructors 10415 /* It's unsafe to elide the operation when handling 10416 a noexcept-expression, it may evaluate to the wrong 10417 value (c++/53025, c++/96090). */ 10418 || cp_noexcept_operand != 0)) 10419 /* Do things the hard way. */; 10420 else if (cand->num_convs == 1 10421 && (DECL_COPY_CONSTRUCTOR_P (fn) 10422 || DECL_MOVE_CONSTRUCTOR_P (fn))) 10423 { 10424 tree targ; 10425 tree arg = argarray[num_artificial_parms_for (fn)]; 10426 tree fa = argarray[0]; 10427 bool trivial = trivial_fn_p (fn); 10428 10429 /* Pull out the real argument, disregarding const-correctness. */ 10430 targ = arg; 10431 /* Strip the reference binding for the constructor parameter. */ 10432 if (CONVERT_EXPR_P (targ) 10433 && TYPE_REF_P (TREE_TYPE (targ))) 10434 targ = TREE_OPERAND (targ, 0); 10435 /* But don't strip any other reference bindings; binding a temporary to a 10436 reference prevents copy elision. */ 10437 while ((CONVERT_EXPR_P (targ) 10438 && !TYPE_REF_P (TREE_TYPE (targ))) 10439 || TREE_CODE (targ) == NON_LVALUE_EXPR) 10440 targ = TREE_OPERAND (targ, 0); 10441 if (TREE_CODE (targ) == ADDR_EXPR) 10442 { 10443 targ = TREE_OPERAND (targ, 0); 10444 if (!same_type_ignoring_top_level_qualifiers_p 10445 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ))) 10446 targ = NULL_TREE; 10447 } 10448 else 10449 targ = NULL_TREE; 10450 10451 if (targ) 10452 arg = targ; 10453 else 10454 arg = cp_build_fold_indirect_ref (arg); 10455 10456 /* In C++17 we shouldn't be copying a TARGET_EXPR except into a 10457 potentially-overlapping subobject. */ 10458 if (CHECKING_P && cxx_dialect >= cxx17) 10459 gcc_assert (TREE_CODE (arg) != TARGET_EXPR 10460 || force_elide 10461 /* It's from binding the ref parm to a packed field. */ 10462 || convs[0]->need_temporary_p 10463 || seen_error () 10464 /* See unsafe_copy_elision_p. */ 10465 || unsafe_return_slot_p (fa)); 10466 10467 bool unsafe = unsafe_copy_elision_p_opt (fa, arg); 10468 bool eliding_temp = (TREE_CODE (arg) == TARGET_EXPR && !unsafe); 10469 10470 /* [class.copy]: the copy constructor is implicitly defined even if the 10471 implementation elided its use. But don't warn about deprecation when 10472 eliding a temporary, as then no copy is actually performed. */ 10473 warning_sentinel s (warn_deprecated_copy, eliding_temp); 10474 if (force_elide) 10475 /* The language says this isn't called. */; 10476 else if (!trivial) 10477 { 10478 if (!mark_used (fn, complain) && !(complain & tf_error)) 10479 return error_mark_node; 10480 already_used = true; 10481 } 10482 else 10483 cp_handle_deprecated_or_unavailable (fn, complain); 10484 10485 if (eliding_temp && DECL_BASE_CONSTRUCTOR_P (fn) 10486 && !make_base_init_ok (arg)) 10487 unsafe = true; 10488 10489 /* If we're creating a temp and we already have one, don't create a 10490 new one. If we're not creating a temp but we get one, use 10491 INIT_EXPR to collapse the temp into our target. Otherwise, if the 10492 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a 10493 temp or an INIT_EXPR otherwise. */ 10494 if (is_dummy_object (fa)) 10495 { 10496 if (TREE_CODE (arg) == TARGET_EXPR) 10497 return arg; 10498 else if (trivial) 10499 return force_target_expr (DECL_CONTEXT (fn), arg, complain); 10500 } 10501 else if ((trivial || TREE_CODE (arg) == TARGET_EXPR) 10502 && !unsafe) 10503 { 10504 tree to = cp_build_fold_indirect_ref (fa); 10505 val = cp_build_init_expr (to, arg); 10506 return val; 10507 } 10508 } 10509 else if (DECL_ASSIGNMENT_OPERATOR_P (fn) 10510 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR) 10511 && trivial_fn_p (fn)) 10512 { 10513 tree to = cp_build_fold_indirect_ref (argarray[0]); 10514 tree type = TREE_TYPE (to); 10515 tree as_base = CLASSTYPE_AS_BASE (type); 10516 tree arg = argarray[1]; 10517 location_t loc = cp_expr_loc_or_input_loc (arg); 10518 10519 if (is_really_empty_class (type, /*ignore_vptr*/true)) 10520 { 10521 /* Avoid copying empty classes, but ensure op= returns an lvalue even 10522 if the object argument isn't one. */ 10523 to = force_lvalue (to, complain); 10524 val = build2 (COMPOUND_EXPR, type, arg, to); 10525 suppress_warning (val, OPT_Wunused); 10526 } 10527 else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base))) 10528 { 10529 if (is_std_init_list (type) 10530 && conv_binds_ref_to_prvalue (convs[1])) 10531 warning_at (loc, OPT_Winit_list_lifetime, 10532 "assignment from temporary %<initializer_list%> does " 10533 "not extend the lifetime of the underlying array"); 10534 arg = cp_build_fold_indirect_ref (arg); 10535 val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg); 10536 } 10537 else 10538 { 10539 /* We must only copy the non-tail padding parts. */ 10540 tree arg0, arg2, t; 10541 tree array_type, alias_set; 10542 10543 arg2 = TYPE_SIZE_UNIT (as_base); 10544 /* Ensure op= returns an lvalue even if the object argument isn't 10545 one. */ 10546 to = force_lvalue (to, complain); 10547 to = cp_stabilize_reference (to); 10548 arg0 = cp_build_addr_expr (to, complain); 10549 10550 array_type = build_array_type (unsigned_char_type_node, 10551 build_index_type 10552 (size_binop (MINUS_EXPR, 10553 arg2, size_int (1)))); 10554 alias_set = build_int_cst (build_pointer_type (type), 0); 10555 t = build2 (MODIFY_EXPR, void_type_node, 10556 build2 (MEM_REF, array_type, arg0, alias_set), 10557 build2 (MEM_REF, array_type, arg, alias_set)); 10558 val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to); 10559 suppress_warning (val, OPT_Wunused); 10560 } 10561 10562 cp_handle_deprecated_or_unavailable (fn, complain); 10563 10564 return val; 10565 } 10566 else if (trivial_fn_p (fn)) 10567 { 10568 if (DECL_DESTRUCTOR_P (fn)) 10569 return build_trivial_dtor_call (argarray[0]); 10570 else if (default_ctor_p (fn)) 10571 { 10572 if (is_dummy_object (argarray[0])) 10573 return force_target_expr (DECL_CONTEXT (fn), void_node, 10574 no_cleanup_complain); 10575 else 10576 return cp_build_fold_indirect_ref (argarray[0]); 10577 } 10578 } 10579 10580 gcc_assert (!force_elide); 10581 10582 if (!already_used 10583 && !mark_used (fn, complain)) 10584 return error_mark_node; 10585 10586 /* Warn if the built-in writes to an object of a non-trivial type. */ 10587 if (warn_class_memaccess 10588 && vec_safe_length (args) >= 2 10589 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL) 10590 maybe_warn_class_memaccess (input_location, fn, args); 10591 10592 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0) 10593 { 10594 tree t; 10595 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])), 10596 DECL_CONTEXT (fn), 10597 ba_any, NULL, complain); 10598 gcc_assert (binfo && binfo != error_mark_node); 10599 10600 argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1, 10601 complain); 10602 if (TREE_SIDE_EFFECTS (argarray[0])) 10603 argarray[0] = save_expr (argarray[0]); 10604 t = build_pointer_type (TREE_TYPE (fn)); 10605 fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn)); 10606 TREE_TYPE (fn) = t; 10607 } 10608 else 10609 { 10610 /* If FN is marked deprecated or unavailable, then we've already 10611 issued a diagnostic from mark_used above, so avoid redundantly 10612 issuing another one from build_addr_func. */ 10613 auto w = make_temp_override (deprecated_state, 10614 UNAVAILABLE_DEPRECATED_SUPPRESS); 10615 10616 fn = build_addr_func (fn, complain); 10617 if (fn == error_mark_node) 10618 return error_mark_node; 10619 10620 /* We're actually invoking the function. (Immediate functions get an 10621 & when invoking it even though the user didn't use &.) */ 10622 ADDR_EXPR_DENOTES_CALL_P (fn) = true; 10623 } 10624 10625 tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag); 10626 if (call == error_mark_node) 10627 return call; 10628 if (cand->flags & LOOKUP_LIST_INIT_CTOR) 10629 { 10630 tree c = extract_call_expr (call); 10631 /* build_new_op will clear this when appropriate. */ 10632 CALL_EXPR_ORDERED_ARGS (c) = true; 10633 } 10634 if (warned_p) 10635 { 10636 tree c = extract_call_expr (call); 10637 if (TREE_CODE (c) == CALL_EXPR) 10638 suppress_warning (c /* Suppress all warnings. */); 10639 } 10640 10641 return call; 10642 } 10643 10644 namespace 10645 { 10646 10647 /* Return the DECL of the first non-static subobject of class TYPE 10648 that satisfies the predicate PRED or null if none can be found. */ 10649 10650 template <class Predicate> 10651 tree 10652 first_non_static_field (tree type, Predicate pred) 10653 { 10654 if (!type || !CLASS_TYPE_P (type)) 10655 return NULL_TREE; 10656 10657 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 10658 { 10659 if (TREE_CODE (field) != FIELD_DECL) 10660 continue; 10661 if (TREE_STATIC (field)) 10662 continue; 10663 if (pred (field)) 10664 return field; 10665 } 10666 10667 int i = 0; 10668 10669 for (tree base_binfo, binfo = TYPE_BINFO (type); 10670 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 10671 { 10672 tree base = TREE_TYPE (base_binfo); 10673 if (pred (base)) 10674 return base; 10675 if (tree field = first_non_static_field (base, pred)) 10676 return field; 10677 } 10678 10679 return NULL_TREE; 10680 } 10681 10682 struct NonPublicField 10683 { 10684 bool operator() (const_tree t) const 10685 { 10686 return DECL_P (t) && (TREE_PRIVATE (t) || TREE_PROTECTED (t)); 10687 } 10688 }; 10689 10690 /* Return the DECL of the first non-public subobject of class TYPE 10691 or null if none can be found. */ 10692 10693 static inline tree 10694 first_non_public_field (tree type) 10695 { 10696 return first_non_static_field (type, NonPublicField ()); 10697 } 10698 10699 struct NonTrivialField 10700 { 10701 bool operator() (const_tree t) const 10702 { 10703 return !trivial_type_p (DECL_P (t) ? TREE_TYPE (t) : t); 10704 } 10705 }; 10706 10707 /* Return the DECL of the first non-trivial subobject of class TYPE 10708 or null if none can be found. */ 10709 10710 static inline tree 10711 first_non_trivial_field (tree type) 10712 { 10713 return first_non_static_field (type, NonTrivialField ()); 10714 } 10715 10716 } /* unnamed namespace */ 10717 10718 /* Return true if all copy and move assignment operator overloads for 10719 class TYPE are trivial and at least one of them is not deleted and, 10720 when ACCESS is set, accessible. Return false otherwise. Set 10721 HASASSIGN to true when the TYPE has a (not necessarily trivial) 10722 copy or move assignment. */ 10723 10724 static bool 10725 has_trivial_copy_assign_p (tree type, bool access, bool *hasassign) 10726 { 10727 tree fns = get_class_binding (type, assign_op_identifier); 10728 bool all_trivial = true; 10729 10730 /* Iterate over overloads of the assignment operator, checking 10731 accessible copy assignments for triviality. */ 10732 10733 for (tree f : ovl_range (fns)) 10734 { 10735 /* Skip operators that aren't copy assignments. */ 10736 if (!copy_fn_p (f)) 10737 continue; 10738 10739 bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f)) 10740 || accessible_p (TYPE_BINFO (type), f, true)); 10741 10742 /* Skip template assignment operators and deleted functions. */ 10743 if (TREE_CODE (f) != FUNCTION_DECL || DECL_DELETED_FN (f)) 10744 continue; 10745 10746 if (accessible) 10747 *hasassign = true; 10748 10749 if (!accessible || !trivial_fn_p (f)) 10750 all_trivial = false; 10751 10752 /* Break early when both properties have been determined. */ 10753 if (*hasassign && !all_trivial) 10754 break; 10755 } 10756 10757 /* Return true if they're all trivial and one of the expressions 10758 TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */ 10759 tree ref = cp_build_reference_type (type, false); 10760 return (all_trivial 10761 && (is_trivially_xible (MODIFY_EXPR, type, type) 10762 || is_trivially_xible (MODIFY_EXPR, type, ref))); 10763 } 10764 10765 /* Return true if all copy and move ctor overloads for class TYPE are 10766 trivial and at least one of them is not deleted and, when ACCESS is 10767 set, accessible. Return false otherwise. Set each element of HASCTOR[] 10768 to true when the TYPE has a (not necessarily trivial) default and copy 10769 (or move) ctor, respectively. */ 10770 10771 static bool 10772 has_trivial_copy_p (tree type, bool access, bool hasctor[2]) 10773 { 10774 tree fns = get_class_binding (type, complete_ctor_identifier); 10775 bool all_trivial = true; 10776 10777 for (tree f : ovl_range (fns)) 10778 { 10779 /* Skip template constructors. */ 10780 if (TREE_CODE (f) != FUNCTION_DECL) 10781 continue; 10782 10783 bool cpy_or_move_ctor_p = copy_fn_p (f); 10784 10785 /* Skip ctors other than default, copy, and move. */ 10786 if (!cpy_or_move_ctor_p && !default_ctor_p (f)) 10787 continue; 10788 10789 if (DECL_DELETED_FN (f)) 10790 continue; 10791 10792 bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f)) 10793 || accessible_p (TYPE_BINFO (type), f, true)); 10794 10795 if (accessible) 10796 hasctor[cpy_or_move_ctor_p] = true; 10797 10798 if (cpy_or_move_ctor_p && (!accessible || !trivial_fn_p (f))) 10799 all_trivial = false; 10800 10801 /* Break early when both properties have been determined. */ 10802 if (hasctor[0] && hasctor[1] && !all_trivial) 10803 break; 10804 } 10805 10806 return all_trivial; 10807 } 10808 10809 /* Issue a warning on a call to the built-in function FNDECL if it is 10810 a raw memory write whose destination is not an object of (something 10811 like) trivial or standard layout type with a non-deleted assignment 10812 and copy ctor. Detects const correctness violations, corrupting 10813 references, virtual table pointers, and bypassing non-trivial 10814 assignments. */ 10815 10816 static void 10817 maybe_warn_class_memaccess (location_t loc, tree fndecl, 10818 const vec<tree, va_gc> *args) 10819 { 10820 /* Except for bcopy where it's second, the destination pointer is 10821 the first argument for all functions handled here. Compute 10822 the index of the destination and source arguments. */ 10823 unsigned dstidx = DECL_FUNCTION_CODE (fndecl) == BUILT_IN_BCOPY; 10824 unsigned srcidx = !dstidx; 10825 10826 tree dest = (*args)[dstidx]; 10827 if (!TREE_TYPE (dest) 10828 || (TREE_CODE (TREE_TYPE (dest)) != ARRAY_TYPE 10829 && !INDIRECT_TYPE_P (TREE_TYPE (dest)))) 10830 return; 10831 10832 tree srctype = NULL_TREE; 10833 10834 /* Determine the type of the pointed-to object and whether it's 10835 a complete class type. */ 10836 tree desttype = TREE_TYPE (TREE_TYPE (dest)); 10837 10838 if (!desttype || !COMPLETE_TYPE_P (desttype) || !CLASS_TYPE_P (desttype)) 10839 return; 10840 10841 /* Check to see if the raw memory call is made by a non-static member 10842 function with THIS as the destination argument for the destination 10843 type. If so, and if the class has no non-trivial bases or members, 10844 be more permissive. */ 10845 if (current_function_decl 10846 && DECL_OBJECT_MEMBER_FUNCTION_P (current_function_decl) 10847 && is_object_parameter (tree_strip_nop_conversions (dest))) 10848 { 10849 tree ctx = DECL_CONTEXT (current_function_decl); 10850 bool special = same_type_ignoring_top_level_qualifiers_p (ctx, desttype); 10851 tree binfo = TYPE_BINFO (ctx); 10852 10853 if (special 10854 && !BINFO_VTABLE (binfo) 10855 && !first_non_trivial_field (desttype)) 10856 return; 10857 } 10858 10859 /* True if the class is trivial. */ 10860 bool trivial = trivial_type_p (desttype); 10861 10862 /* Set to true if DESTYPE has an accessible copy assignment. */ 10863 bool hasassign = false; 10864 /* True if all of the class' overloaded copy assignment operators 10865 are all trivial (and not deleted) and at least one of them is 10866 accessible. */ 10867 bool trivassign = has_trivial_copy_assign_p (desttype, true, &hasassign); 10868 10869 /* Set to true if DESTTYPE has an accessible default and copy ctor, 10870 respectively. */ 10871 bool hasctors[2] = { false, false }; 10872 10873 /* True if all of the class' overloaded copy constructors are all 10874 trivial (and not deleted) and at least one of them is accessible. */ 10875 bool trivcopy = has_trivial_copy_p (desttype, true, hasctors); 10876 10877 /* Set FLD to the first private/protected member of the class. */ 10878 tree fld = trivial ? first_non_public_field (desttype) : NULL_TREE; 10879 10880 /* The warning format string. */ 10881 const char *warnfmt = NULL; 10882 /* A suggested alternative to offer instead of the raw memory call. 10883 Empty string when none can be come up with. */ 10884 const char *suggest = ""; 10885 bool warned = false; 10886 10887 switch (DECL_FUNCTION_CODE (fndecl)) 10888 { 10889 case BUILT_IN_MEMSET: 10890 if (!integer_zerop (maybe_constant_value ((*args)[1]))) 10891 { 10892 /* Diagnose setting non-copy-assignable or non-trivial types, 10893 or types with a private member, to (potentially) non-zero 10894 bytes. Since the value of the bytes being written is unknown, 10895 suggest using assignment instead (if one exists). Also warn 10896 for writes into objects for which zero-initialization doesn't 10897 mean all bits clear (pointer-to-member data, where null is all 10898 bits set). Since the value being written is (most likely) 10899 non-zero, simply suggest assignment (but not copy assignment). */ 10900 suggest = "; use assignment instead"; 10901 if (!trivassign) 10902 warnfmt = G_("%qD writing to an object of type %#qT with " 10903 "no trivial copy-assignment"); 10904 else if (!trivial) 10905 warnfmt = G_("%qD writing to an object of non-trivial type %#qT%s"); 10906 else if (fld) 10907 { 10908 const char *access = TREE_PRIVATE (fld) ? "private" : "protected"; 10909 warned = warning_at (loc, OPT_Wclass_memaccess, 10910 "%qD writing to an object of type %#qT with " 10911 "%qs member %qD", 10912 fndecl, desttype, access, fld); 10913 } 10914 else if (!zero_init_p (desttype)) 10915 warnfmt = G_("%qD writing to an object of type %#qT containing " 10916 "a pointer to data member%s"); 10917 10918 break; 10919 } 10920 /* Fall through. */ 10921 10922 case BUILT_IN_BZERO: 10923 /* Similarly to the above, diagnose clearing non-trivial or non- 10924 standard layout objects, or objects of types with no assignmenmt. 10925 Since the value being written is known to be zero, suggest either 10926 copy assignment, copy ctor, or default ctor as an alternative, 10927 depending on what's available. */ 10928 10929 if (hasassign && hasctors[0]) 10930 suggest = G_("; use assignment or value-initialization instead"); 10931 else if (hasassign) 10932 suggest = G_("; use assignment instead"); 10933 else if (hasctors[0]) 10934 suggest = G_("; use value-initialization instead"); 10935 10936 if (!trivassign) 10937 warnfmt = G_("%qD clearing an object of type %#qT with " 10938 "no trivial copy-assignment%s"); 10939 else if (!trivial) 10940 warnfmt = G_("%qD clearing an object of non-trivial type %#qT%s"); 10941 else if (!zero_init_p (desttype)) 10942 warnfmt = G_("%qD clearing an object of type %#qT containing " 10943 "a pointer-to-member%s"); 10944 break; 10945 10946 case BUILT_IN_BCOPY: 10947 case BUILT_IN_MEMCPY: 10948 case BUILT_IN_MEMMOVE: 10949 case BUILT_IN_MEMPCPY: 10950 /* Determine the type of the source object. */ 10951 srctype = TREE_TYPE ((*args)[srcidx]); 10952 if (!srctype || !INDIRECT_TYPE_P (srctype)) 10953 srctype = void_type_node; 10954 else 10955 srctype = TREE_TYPE (srctype); 10956 10957 /* Since it's impossible to determine wheter the byte copy is 10958 being used in place of assignment to an existing object or 10959 as a substitute for initialization, assume it's the former. 10960 Determine the best alternative to use instead depending on 10961 what's not deleted. */ 10962 if (hasassign && hasctors[1]) 10963 suggest = G_("; use copy-assignment or copy-initialization instead"); 10964 else if (hasassign) 10965 suggest = G_("; use copy-assignment instead"); 10966 else if (hasctors[1]) 10967 suggest = G_("; use copy-initialization instead"); 10968 10969 if (!trivassign) 10970 warnfmt = G_("%qD writing to an object of type %#qT with no trivial " 10971 "copy-assignment%s"); 10972 else if (!trivially_copyable_p (desttype)) 10973 warnfmt = G_("%qD writing to an object of non-trivially copyable " 10974 "type %#qT%s"); 10975 else if (!trivcopy) 10976 warnfmt = G_("%qD writing to an object with a deleted copy constructor"); 10977 10978 else if (!trivial 10979 && !VOID_TYPE_P (srctype) 10980 && !is_byte_access_type (srctype) 10981 && !same_type_ignoring_top_level_qualifiers_p (desttype, 10982 srctype)) 10983 { 10984 /* Warn when copying into a non-trivial object from an object 10985 of a different type other than void or char. */ 10986 warned = warning_at (loc, OPT_Wclass_memaccess, 10987 "%qD copying an object of non-trivial type " 10988 "%#qT from an array of %#qT", 10989 fndecl, desttype, srctype); 10990 } 10991 else if (fld 10992 && !VOID_TYPE_P (srctype) 10993 && !is_byte_access_type (srctype) 10994 && !same_type_ignoring_top_level_qualifiers_p (desttype, 10995 srctype)) 10996 { 10997 const char *access = TREE_PRIVATE (fld) ? "private" : "protected"; 10998 warned = warning_at (loc, OPT_Wclass_memaccess, 10999 "%qD copying an object of type %#qT with " 11000 "%qs member %qD from an array of %#qT; use " 11001 "assignment or copy-initialization instead", 11002 fndecl, desttype, access, fld, srctype); 11003 } 11004 else if (!trivial && vec_safe_length (args) > 2) 11005 { 11006 tree sz = maybe_constant_value ((*args)[2]); 11007 if (!tree_fits_uhwi_p (sz)) 11008 break; 11009 11010 /* Finally, warn on partial copies. */ 11011 unsigned HOST_WIDE_INT typesize 11012 = tree_to_uhwi (TYPE_SIZE_UNIT (desttype)); 11013 if (typesize == 0) 11014 break; 11015 if (unsigned HOST_WIDE_INT partial = tree_to_uhwi (sz) % typesize) 11016 warned = warning_at (loc, OPT_Wclass_memaccess, 11017 (typesize - partial > 1 11018 ? G_("%qD writing to an object of " 11019 "a non-trivial type %#qT leaves %wu " 11020 "bytes unchanged") 11021 : G_("%qD writing to an object of " 11022 "a non-trivial type %#qT leaves %wu " 11023 "byte unchanged")), 11024 fndecl, desttype, typesize - partial); 11025 } 11026 break; 11027 11028 case BUILT_IN_REALLOC: 11029 11030 if (!trivially_copyable_p (desttype)) 11031 warnfmt = G_("%qD moving an object of non-trivially copyable type " 11032 "%#qT; use %<new%> and %<delete%> instead"); 11033 else if (!trivcopy) 11034 warnfmt = G_("%qD moving an object of type %#qT with deleted copy " 11035 "constructor; use %<new%> and %<delete%> instead"); 11036 else if (!get_dtor (desttype, tf_none)) 11037 warnfmt = G_("%qD moving an object of type %#qT with deleted " 11038 "destructor"); 11039 else if (!trivial) 11040 { 11041 tree sz = maybe_constant_value ((*args)[1]); 11042 if (TREE_CODE (sz) == INTEGER_CST 11043 && tree_int_cst_lt (sz, TYPE_SIZE_UNIT (desttype))) 11044 /* Finally, warn on reallocation into insufficient space. */ 11045 warned = warning_at (loc, OPT_Wclass_memaccess, 11046 "%qD moving an object of non-trivial type " 11047 "%#qT and size %E into a region of size %E", 11048 fndecl, desttype, TYPE_SIZE_UNIT (desttype), 11049 sz); 11050 } 11051 break; 11052 11053 default: 11054 return; 11055 } 11056 11057 if (warnfmt) 11058 { 11059 if (suggest) 11060 warned = warning_at (loc, OPT_Wclass_memaccess, 11061 warnfmt, fndecl, desttype, suggest); 11062 else 11063 warned = warning_at (loc, OPT_Wclass_memaccess, 11064 warnfmt, fndecl, desttype); 11065 } 11066 11067 if (warned) 11068 inform (location_of (desttype), "%#qT declared here", desttype); 11069 } 11070 11071 /* Build and return a call to FN, using NARGS arguments in ARGARRAY. 11072 If FN is the result of resolving an overloaded target built-in, 11073 ORIG_FNDECL is the original function decl, otherwise it is null. 11074 This function performs no overload resolution, conversion, or other 11075 high-level operations. */ 11076 11077 tree 11078 build_cxx_call (tree fn, int nargs, tree *argarray, 11079 tsubst_flags_t complain, tree orig_fndecl) 11080 { 11081 tree fndecl; 11082 11083 /* Remember roughly where this call is. */ 11084 location_t loc = cp_expr_loc_or_input_loc (fn); 11085 fn = build_call_a (fn, nargs, argarray); 11086 SET_EXPR_LOCATION (fn, loc); 11087 11088 fndecl = get_callee_fndecl (fn); 11089 if (!orig_fndecl) 11090 orig_fndecl = fndecl; 11091 11092 /* Check that arguments to builtin functions match the expectations. */ 11093 if (fndecl 11094 && !processing_template_decl 11095 && fndecl_built_in_p (fndecl)) 11096 { 11097 int i; 11098 11099 /* We need to take care that values to BUILT_IN_NORMAL 11100 are reduced. */ 11101 for (i = 0; i < nargs; i++) 11102 argarray[i] = maybe_constant_value (argarray[i]); 11103 11104 if (!check_builtin_function_arguments (EXPR_LOCATION (fn), vNULL, fndecl, 11105 orig_fndecl, nargs, argarray)) 11106 return error_mark_node; 11107 else if (fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING)) 11108 { 11109 tree arg0 = argarray[0]; 11110 STRIP_NOPS (arg0); 11111 if (TREE_CODE (arg0) == ADDR_EXPR 11112 && DECL_P (TREE_OPERAND (arg0, 0)) 11113 && same_type_ignoring_top_level_qualifiers_p 11114 (TREE_TYPE (TREE_TYPE (argarray[0])), 11115 TREE_TYPE (TREE_TYPE (arg0)))) 11116 /* For __builtin_clear_padding (&var) we know the type 11117 is for a complete object, so there is no risk in clearing 11118 padding that is reused in some derived class member. */; 11119 else if (!trivially_copyable_p (TREE_TYPE (TREE_TYPE (argarray[0])))) 11120 { 11121 error_at (EXPR_LOC_OR_LOC (argarray[0], input_location), 11122 "argument %u in call to function %qE " 11123 "has pointer to a non-trivially-copyable type (%qT)", 11124 1, fndecl, TREE_TYPE (argarray[0])); 11125 return error_mark_node; 11126 } 11127 } 11128 } 11129 11130 if (VOID_TYPE_P (TREE_TYPE (fn))) 11131 return fn; 11132 11133 /* 5.2.2/11: If a function call is a prvalue of object type: if the 11134 function call is either the operand of a decltype-specifier or the 11135 right operand of a comma operator that is the operand of a 11136 decltype-specifier, a temporary object is not introduced for the 11137 prvalue. The type of the prvalue may be incomplete. */ 11138 if (!(complain & tf_decltype)) 11139 { 11140 fn = require_complete_type (fn, complain); 11141 if (fn == error_mark_node) 11142 return error_mark_node; 11143 11144 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn))) 11145 { 11146 fn = build_cplus_new (TREE_TYPE (fn), fn, complain); 11147 maybe_warn_parm_abi (TREE_TYPE (fn), loc); 11148 } 11149 } 11150 return convert_from_reference (fn); 11151 } 11152 11153 /* Returns the value to use for the in-charge parameter when making a 11154 call to a function with the indicated NAME. 11155 11156 FIXME:Can't we find a neater way to do this mapping? */ 11157 11158 tree 11159 in_charge_arg_for_name (tree name) 11160 { 11161 if (IDENTIFIER_CTOR_P (name)) 11162 { 11163 if (name == complete_ctor_identifier) 11164 return integer_one_node; 11165 gcc_checking_assert (name == base_ctor_identifier); 11166 } 11167 else 11168 { 11169 if (name == complete_dtor_identifier) 11170 return integer_two_node; 11171 else if (name == deleting_dtor_identifier) 11172 return integer_three_node; 11173 gcc_checking_assert (name == base_dtor_identifier); 11174 } 11175 11176 return integer_zero_node; 11177 } 11178 11179 /* We've built up a constructor call RET. Complain if it delegates to the 11180 constructor we're currently compiling. */ 11181 11182 static void 11183 check_self_delegation (tree ret) 11184 { 11185 if (TREE_CODE (ret) == TARGET_EXPR) 11186 ret = TARGET_EXPR_INITIAL (ret); 11187 tree fn = cp_get_callee_fndecl_nofold (ret); 11188 if (fn && DECL_ABSTRACT_ORIGIN (fn) == current_function_decl) 11189 error ("constructor delegates to itself"); 11190 } 11191 11192 /* Build a call to a constructor, destructor, or an assignment 11193 operator for INSTANCE, an expression with class type. NAME 11194 indicates the special member function to call; *ARGS are the 11195 arguments. ARGS may be NULL. This may change ARGS. BINFO 11196 indicates the base of INSTANCE that is to be passed as the `this' 11197 parameter to the member function called. 11198 11199 FLAGS are the LOOKUP_* flags to use when processing the call. 11200 11201 If NAME indicates a complete object constructor, INSTANCE may be 11202 NULL_TREE. In this case, the caller will call build_cplus_new to 11203 store the newly constructed object into a VAR_DECL. */ 11204 11205 tree 11206 build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args, 11207 tree binfo, int flags, tsubst_flags_t complain) 11208 { 11209 tree fns; 11210 /* The type of the subobject to be constructed or destroyed. */ 11211 tree class_type; 11212 vec<tree, va_gc> *allocated = NULL; 11213 tree ret; 11214 11215 gcc_assert (IDENTIFIER_CDTOR_P (name) || name == assign_op_identifier); 11216 11217 if (error_operand_p (instance)) 11218 return error_mark_node; 11219 11220 if (IDENTIFIER_DTOR_P (name)) 11221 { 11222 gcc_assert (args == NULL || vec_safe_is_empty (*args)); 11223 if (!type_build_dtor_call (TREE_TYPE (instance))) 11224 /* Shortcut to avoid lazy destructor declaration. */ 11225 return build_trivial_dtor_call (instance); 11226 } 11227 11228 if (TYPE_P (binfo)) 11229 { 11230 /* Resolve the name. */ 11231 if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain)) 11232 return error_mark_node; 11233 11234 binfo = TYPE_BINFO (binfo); 11235 } 11236 11237 gcc_assert (binfo != NULL_TREE); 11238 11239 class_type = BINFO_TYPE (binfo); 11240 11241 /* Handle the special case where INSTANCE is NULL_TREE. */ 11242 if (name == complete_ctor_identifier && !instance) 11243 instance = build_dummy_object (class_type); 11244 else 11245 { 11246 /* Convert to the base class, if necessary. */ 11247 if (!same_type_ignoring_top_level_qualifiers_p 11248 (TREE_TYPE (instance), BINFO_TYPE (binfo))) 11249 { 11250 if (IDENTIFIER_CDTOR_P (name)) 11251 /* For constructors and destructors, either the base is 11252 non-virtual, or it is virtual but we are doing the 11253 conversion from a constructor or destructor for the 11254 complete object. In either case, we can convert 11255 statically. */ 11256 instance = convert_to_base_statically (instance, binfo); 11257 else 11258 { 11259 /* However, for assignment operators, we must convert 11260 dynamically if the base is virtual. */ 11261 gcc_checking_assert (name == assign_op_identifier); 11262 instance = build_base_path (PLUS_EXPR, instance, 11263 binfo, /*nonnull=*/1, complain); 11264 } 11265 } 11266 } 11267 11268 gcc_assert (instance != NULL_TREE); 11269 11270 /* In C++17, "If the initializer expression is a prvalue and the 11271 cv-unqualified version of the source type is the same class as the class 11272 of the destination, the initializer expression is used to initialize the 11273 destination object." Handle that here to avoid doing overload 11274 resolution. */ 11275 if (cxx_dialect >= cxx17 11276 && args && vec_safe_length (*args) == 1 11277 && !unsafe_return_slot_p (instance)) 11278 { 11279 tree arg = (**args)[0]; 11280 11281 if (BRACE_ENCLOSED_INITIALIZER_P (arg) 11282 && !TYPE_HAS_LIST_CTOR (class_type) 11283 && !CONSTRUCTOR_IS_DESIGNATED_INIT (arg) 11284 && CONSTRUCTOR_NELTS (arg) == 1) 11285 arg = CONSTRUCTOR_ELT (arg, 0)->value; 11286 11287 if ((TREE_CODE (arg) == TARGET_EXPR 11288 || TREE_CODE (arg) == CONSTRUCTOR) 11289 && (same_type_ignoring_top_level_qualifiers_p 11290 (class_type, TREE_TYPE (arg)))) 11291 { 11292 if (is_dummy_object (instance)) 11293 return arg; 11294 else if (TREE_CODE (arg) == TARGET_EXPR) 11295 TARGET_EXPR_DIRECT_INIT_P (arg) = true; 11296 11297 if ((complain & tf_error) 11298 && (flags & LOOKUP_DELEGATING_CONS)) 11299 check_self_delegation (arg); 11300 /* Avoid change of behavior on Wunused-var-2.C. */ 11301 instance = mark_lvalue_use (instance); 11302 return cp_build_init_expr (instance, arg); 11303 } 11304 } 11305 11306 fns = lookup_fnfields (binfo, name, 1, complain); 11307 11308 /* When making a call to a constructor or destructor for a subobject 11309 that uses virtual base classes, pass down a pointer to a VTT for 11310 the subobject. */ 11311 if ((name == base_ctor_identifier 11312 || name == base_dtor_identifier) 11313 && CLASSTYPE_VBASECLASSES (class_type)) 11314 { 11315 tree vtt; 11316 tree sub_vtt; 11317 11318 /* If the current function is a complete object constructor 11319 or destructor, then we fetch the VTT directly. 11320 Otherwise, we look it up using the VTT we were given. */ 11321 vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type)); 11322 vtt = decay_conversion (vtt, complain); 11323 if (vtt == error_mark_node) 11324 return error_mark_node; 11325 vtt = build_if_in_charge (vtt, current_vtt_parm); 11326 if (BINFO_SUBVTT_INDEX (binfo)) 11327 sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo)); 11328 else 11329 sub_vtt = vtt; 11330 11331 if (args == NULL) 11332 { 11333 allocated = make_tree_vector (); 11334 args = &allocated; 11335 } 11336 11337 vec_safe_insert (*args, 0, sub_vtt); 11338 } 11339 11340 ret = build_new_method_call (instance, fns, args, 11341 TYPE_BINFO (BINFO_TYPE (binfo)), 11342 flags, /*fn=*/NULL, 11343 complain); 11344 11345 if (allocated != NULL) 11346 release_tree_vector (allocated); 11347 11348 if ((complain & tf_error) 11349 && (flags & LOOKUP_DELEGATING_CONS) 11350 && name == complete_ctor_identifier) 11351 check_self_delegation (ret); 11352 11353 return ret; 11354 } 11355 11356 /* Return the NAME, as a C string. The NAME indicates a function that 11357 is a member of TYPE. *FREE_P is set to true if the caller must 11358 free the memory returned. 11359 11360 Rather than go through all of this, we should simply set the names 11361 of constructors and destructors appropriately, and dispense with 11362 ctor_identifier, dtor_identifier, etc. */ 11363 11364 static char * 11365 name_as_c_string (tree name, tree type, bool *free_p) 11366 { 11367 const char *pretty_name; 11368 11369 /* Assume that we will not allocate memory. */ 11370 *free_p = false; 11371 /* Constructors and destructors are special. */ 11372 if (IDENTIFIER_CDTOR_P (name)) 11373 { 11374 pretty_name 11375 = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type))); 11376 /* For a destructor, add the '~'. */ 11377 if (IDENTIFIER_DTOR_P (name)) 11378 { 11379 pretty_name = concat ("~", pretty_name, NULL); 11380 /* Remember that we need to free the memory allocated. */ 11381 *free_p = true; 11382 } 11383 } 11384 else if (IDENTIFIER_CONV_OP_P (name)) 11385 { 11386 pretty_name = concat ("operator ", 11387 type_as_string_translate (TREE_TYPE (name), 11388 TFF_PLAIN_IDENTIFIER), 11389 NULL); 11390 /* Remember that we need to free the memory allocated. */ 11391 *free_p = true; 11392 } 11393 else 11394 pretty_name = identifier_to_locale (IDENTIFIER_POINTER (name)); 11395 11396 return CONST_CAST (char *, pretty_name); 11397 } 11398 11399 /* If CANDIDATES contains exactly one candidate, return it, otherwise 11400 return NULL. */ 11401 11402 static z_candidate * 11403 single_z_candidate (z_candidate *candidates) 11404 { 11405 if (candidates == NULL) 11406 return NULL; 11407 11408 if (candidates->next) 11409 return NULL; 11410 11411 return candidates; 11412 } 11413 11414 /* If CANDIDATE is invalid due to a bad argument type, return the 11415 pertinent conversion_info. 11416 11417 Otherwise, return NULL. */ 11418 11419 static const conversion_info * 11420 maybe_get_bad_conversion_for_unmatched_call (const z_candidate *candidate) 11421 { 11422 /* Must be an rr_arg_conversion or rr_bad_arg_conversion. */ 11423 rejection_reason *r = candidate->reason; 11424 11425 if (r == NULL) 11426 return NULL; 11427 11428 switch (r->code) 11429 { 11430 default: 11431 return NULL; 11432 11433 case rr_arg_conversion: 11434 return &r->u.conversion; 11435 11436 case rr_bad_arg_conversion: 11437 return &r->u.bad_conversion; 11438 } 11439 } 11440 11441 /* Issue an error and note complaining about a bad argument type at a 11442 callsite with a single candidate FNDECL. 11443 11444 ARG_LOC is the location of the argument (or UNKNOWN_LOCATION, in which 11445 case input_location is used). 11446 FROM_TYPE is the type of the actual argument; TO_TYPE is the type of 11447 the formal parameter. */ 11448 11449 void 11450 complain_about_bad_argument (location_t arg_loc, 11451 tree from_type, tree to_type, 11452 tree fndecl, int parmnum) 11453 { 11454 auto_diagnostic_group d; 11455 range_label_for_type_mismatch rhs_label (from_type, to_type); 11456 range_label *label = &rhs_label; 11457 if (arg_loc == UNKNOWN_LOCATION) 11458 { 11459 arg_loc = input_location; 11460 label = NULL; 11461 } 11462 gcc_rich_location richloc (arg_loc, label); 11463 error_at (&richloc, 11464 "cannot convert %qH to %qI", 11465 from_type, to_type); 11466 maybe_inform_about_fndecl_for_bogus_argument_init (fndecl, 11467 parmnum); 11468 } 11469 11470 /* Subroutine of build_new_method_call_1, for where there are no viable 11471 candidates for the call. */ 11472 11473 static void 11474 complain_about_no_candidates_for_method_call (tree instance, 11475 z_candidate *candidates, 11476 tree explicit_targs, 11477 tree basetype, 11478 tree optype, tree name, 11479 bool skip_first_for_error, 11480 vec<tree, va_gc> *user_args) 11481 { 11482 auto_diagnostic_group d; 11483 if (!COMPLETE_OR_OPEN_TYPE_P (basetype)) 11484 cxx_incomplete_type_error (instance, basetype); 11485 else if (optype) 11486 error ("no matching function for call to %<%T::operator %T(%A)%#V%>", 11487 basetype, optype, build_tree_list_vec (user_args), 11488 TREE_TYPE (instance)); 11489 else 11490 { 11491 /* Special-case for when there's a single candidate that's failing 11492 due to a bad argument type. */ 11493 if (z_candidate *candidate = single_z_candidate (candidates)) 11494 if (const conversion_info *conv 11495 = maybe_get_bad_conversion_for_unmatched_call (candidate)) 11496 { 11497 tree from_type = conv->from; 11498 if (!TYPE_P (conv->from)) 11499 from_type = lvalue_type (conv->from); 11500 complain_about_bad_argument (conv->loc, 11501 from_type, conv->to_type, 11502 candidate->fn, conv->n_arg); 11503 return; 11504 } 11505 11506 tree arglist = build_tree_list_vec (user_args); 11507 tree errname = name; 11508 bool twiddle = false; 11509 if (IDENTIFIER_CDTOR_P (errname)) 11510 { 11511 twiddle = IDENTIFIER_DTOR_P (errname); 11512 errname = constructor_name (basetype); 11513 } 11514 if (explicit_targs) 11515 errname = lookup_template_function (errname, explicit_targs); 11516 if (skip_first_for_error) 11517 arglist = TREE_CHAIN (arglist); 11518 error ("no matching function for call to %<%T::%s%E(%A)%#V%>", 11519 basetype, &"~"[!twiddle], errname, arglist, 11520 TREE_TYPE (instance)); 11521 } 11522 print_z_candidates (location_of (name), candidates); 11523 } 11524 11525 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will 11526 be set, upon return, to the function called. ARGS may be NULL. 11527 This may change ARGS. */ 11528 11529 tree 11530 build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args, 11531 tree conversion_path, int flags, 11532 tree *fn_p, tsubst_flags_t complain) 11533 { 11534 struct z_candidate *candidates = 0, *cand; 11535 tree explicit_targs = NULL_TREE; 11536 tree basetype = NULL_TREE; 11537 tree access_binfo; 11538 tree optype; 11539 tree first_mem_arg = NULL_TREE; 11540 tree name; 11541 bool skip_first_for_error; 11542 vec<tree, va_gc> *user_args; 11543 tree call; 11544 tree fn; 11545 int template_only = 0; 11546 bool any_viable_p; 11547 tree orig_instance; 11548 tree orig_fns; 11549 vec<tree, va_gc> *orig_args = NULL; 11550 11551 auto_cond_timevar tv (TV_OVERLOAD); 11552 11553 gcc_assert (instance != NULL_TREE); 11554 11555 /* We don't know what function we're going to call, yet. */ 11556 if (fn_p) 11557 *fn_p = NULL_TREE; 11558 11559 if (error_operand_p (instance) 11560 || !fns || error_operand_p (fns)) 11561 return error_mark_node; 11562 11563 if (!BASELINK_P (fns)) 11564 { 11565 if (complain & tf_error) 11566 error ("call to non-function %qD", fns); 11567 return error_mark_node; 11568 } 11569 11570 orig_instance = instance; 11571 orig_fns = fns; 11572 11573 /* Dismantle the baselink to collect all the information we need. */ 11574 if (!conversion_path) 11575 conversion_path = BASELINK_BINFO (fns); 11576 access_binfo = BASELINK_ACCESS_BINFO (fns); 11577 optype = BASELINK_OPTYPE (fns); 11578 fns = BASELINK_FUNCTIONS (fns); 11579 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) 11580 { 11581 explicit_targs = TREE_OPERAND (fns, 1); 11582 fns = TREE_OPERAND (fns, 0); 11583 template_only = 1; 11584 } 11585 gcc_assert (OVL_P (fns)); 11586 fn = OVL_FIRST (fns); 11587 name = DECL_NAME (fn); 11588 11589 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance)); 11590 gcc_assert (CLASS_TYPE_P (basetype)); 11591 11592 user_args = args == NULL ? NULL : *args; 11593 /* Under DR 147 A::A() is an invalid constructor call, 11594 not a functional cast. */ 11595 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)) 11596 { 11597 if (! (complain & tf_error)) 11598 return error_mark_node; 11599 11600 basetype = DECL_CONTEXT (fn); 11601 name = constructor_name (basetype); 11602 auto_diagnostic_group d; 11603 if (permerror (input_location, 11604 "cannot call constructor %<%T::%D%> directly", 11605 basetype, name)) 11606 inform (input_location, "for a function-style cast, remove the " 11607 "redundant %<::%D%>", name); 11608 call = build_functional_cast (input_location, basetype, 11609 build_tree_list_vec (user_args), 11610 complain); 11611 return call; 11612 } 11613 11614 if (processing_template_decl) 11615 orig_args = args == NULL ? NULL : make_tree_vector_copy (*args); 11616 11617 /* Process the argument list. */ 11618 if (args != NULL && *args != NULL) 11619 { 11620 *args = resolve_args (*args, complain); 11621 if (*args == NULL) 11622 return error_mark_node; 11623 user_args = *args; 11624 } 11625 11626 /* Consider the object argument to be used even if we end up selecting a 11627 static member function. */ 11628 instance = mark_type_use (instance); 11629 11630 /* Figure out whether to skip the first argument for the error 11631 message we will display to users if an error occurs. We don't 11632 want to display any compiler-generated arguments. The "this" 11633 pointer hasn't been added yet. However, we must remove the VTT 11634 pointer if this is a call to a base-class constructor or 11635 destructor. */ 11636 skip_first_for_error = false; 11637 if (IDENTIFIER_CDTOR_P (name)) 11638 { 11639 /* Callers should explicitly indicate whether they want to ctor 11640 the complete object or just the part without virtual bases. */ 11641 gcc_assert (name != ctor_identifier); 11642 11643 /* Remove the VTT pointer, if present. */ 11644 if ((name == base_ctor_identifier || name == base_dtor_identifier) 11645 && CLASSTYPE_VBASECLASSES (basetype)) 11646 skip_first_for_error = true; 11647 11648 /* It's OK to call destructors and constructors on cv-qualified 11649 objects. Therefore, convert the INSTANCE to the unqualified 11650 type, if necessary. */ 11651 if (!same_type_p (basetype, TREE_TYPE (instance))) 11652 { 11653 instance = build_this (instance); 11654 instance = build_nop (build_pointer_type (basetype), instance); 11655 instance = build_fold_indirect_ref (instance); 11656 } 11657 } 11658 else 11659 gcc_assert (!DECL_DESTRUCTOR_P (fn) && !DECL_CONSTRUCTOR_P (fn)); 11660 11661 /* For the overload resolution we need to find the actual `this` 11662 that would be captured if the call turns out to be to a 11663 non-static member function. Do not actually capture it at this 11664 point. */ 11665 if (DECL_CONSTRUCTOR_P (fn)) 11666 /* Constructors don't use the enclosing 'this'. */ 11667 first_mem_arg = instance; 11668 else 11669 first_mem_arg = maybe_resolve_dummy (instance, false); 11670 11671 conversion_obstack_sentinel cos; 11672 11673 /* The number of arguments artificial parms in ARGS; we subtract one because 11674 there's no 'this' in ARGS. */ 11675 unsigned skip = num_artificial_parms_for (fn) - 1; 11676 11677 /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form 11678 initializer, not T({ }). */ 11679 if (DECL_CONSTRUCTOR_P (fn) 11680 && vec_safe_length (user_args) > skip 11681 && DIRECT_LIST_INIT_P ((*user_args)[skip])) 11682 { 11683 tree init_list = (*user_args)[skip]; 11684 tree init = NULL_TREE; 11685 11686 gcc_assert (user_args->length () == skip + 1 11687 && !(flags & LOOKUP_ONLYCONVERTING)); 11688 11689 /* If the initializer list has no elements and T is a class type with 11690 a default constructor, the object is value-initialized. Handle 11691 this here so we don't need to handle it wherever we use 11692 build_special_member_call. */ 11693 if (CONSTRUCTOR_NELTS (init_list) == 0 11694 && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype) 11695 /* For a user-provided default constructor, use the normal 11696 mechanisms so that protected access works. */ 11697 && type_has_non_user_provided_default_constructor (basetype) 11698 && !processing_template_decl) 11699 init = build_value_init (basetype, complain); 11700 11701 /* If BASETYPE is an aggregate, we need to do aggregate 11702 initialization. */ 11703 else if (CP_AGGREGATE_TYPE_P (basetype)) 11704 { 11705 init = reshape_init (basetype, init_list, complain); 11706 init = digest_init (basetype, init, complain); 11707 } 11708 11709 if (init) 11710 { 11711 if (is_dummy_object (instance)) 11712 return get_target_expr (init, complain); 11713 return cp_build_init_expr (instance, init); 11714 } 11715 11716 /* Otherwise go ahead with overload resolution. */ 11717 add_list_candidates (fns, first_mem_arg, user_args, 11718 basetype, explicit_targs, template_only, 11719 conversion_path, access_binfo, flags, 11720 &candidates, complain); 11721 } 11722 else 11723 add_candidates (fns, first_mem_arg, user_args, optype, 11724 explicit_targs, template_only, conversion_path, 11725 access_binfo, flags, &candidates, complain); 11726 11727 any_viable_p = false; 11728 candidates = splice_viable (candidates, false, &any_viable_p); 11729 11730 if (!any_viable_p) 11731 { 11732 /* [dcl.init], 17.6.2.2: 11733 11734 Otherwise, if no constructor is viable, the destination type is 11735 a (possibly cv-qualified) aggregate class A, and the initializer 11736 is a parenthesized expression-list, the object is initialized as 11737 follows... 11738 11739 We achieve this by building up a CONSTRUCTOR, as for list-init, 11740 and setting CONSTRUCTOR_IS_PAREN_INIT to distinguish between 11741 the two. */ 11742 if (DECL_CONSTRUCTOR_P (fn) 11743 && !(flags & LOOKUP_ONLYCONVERTING) 11744 && cxx_dialect >= cxx20 11745 && CP_AGGREGATE_TYPE_P (basetype) 11746 && !vec_safe_is_empty (user_args)) 11747 { 11748 /* Create a CONSTRUCTOR from ARGS, e.g. {1, 2} from <1, 2>. */ 11749 tree ctor = build_constructor_from_vec (init_list_type_node, 11750 user_args); 11751 CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; 11752 CONSTRUCTOR_IS_PAREN_INIT (ctor) = true; 11753 if (is_dummy_object (instance)) 11754 return ctor; 11755 else 11756 { 11757 ctor = digest_init (basetype, ctor, complain); 11758 if (ctor == error_mark_node) 11759 return error_mark_node; 11760 return cp_build_init_expr (instance, ctor); 11761 } 11762 } 11763 if (complain & tf_error) 11764 complain_about_no_candidates_for_method_call (instance, candidates, 11765 explicit_targs, basetype, 11766 optype, name, 11767 skip_first_for_error, 11768 user_args); 11769 call = error_mark_node; 11770 } 11771 else 11772 { 11773 cand = tourney (candidates, complain); 11774 if (cand == 0) 11775 { 11776 char *pretty_name; 11777 bool free_p; 11778 tree arglist; 11779 11780 if (complain & tf_error) 11781 { 11782 pretty_name = name_as_c_string (name, basetype, &free_p); 11783 arglist = build_tree_list_vec (user_args); 11784 if (skip_first_for_error) 11785 arglist = TREE_CHAIN (arglist); 11786 auto_diagnostic_group d; 11787 if (!any_strictly_viable (candidates)) 11788 error ("no matching function for call to %<%s(%A)%>", 11789 pretty_name, arglist); 11790 else 11791 error ("call of overloaded %<%s(%A)%> is ambiguous", 11792 pretty_name, arglist); 11793 print_z_candidates (location_of (name), candidates); 11794 if (free_p) 11795 free (pretty_name); 11796 } 11797 call = error_mark_node; 11798 if (fn_p) 11799 *fn_p = error_mark_node; 11800 } 11801 else 11802 { 11803 fn = cand->fn; 11804 call = NULL_TREE; 11805 11806 if (!(flags & LOOKUP_NONVIRTUAL) 11807 && DECL_PURE_VIRTUAL_P (fn) 11808 && instance == current_class_ref 11809 && (complain & tf_warning)) 11810 { 11811 /* This is not an error, it is runtime undefined 11812 behavior. */ 11813 if (!current_function_decl) 11814 warning (0, "pure virtual %q#D called from " 11815 "non-static data member initializer", fn); 11816 else if (DECL_CONSTRUCTOR_P (current_function_decl) 11817 || DECL_DESTRUCTOR_P (current_function_decl)) 11818 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl) 11819 ? G_("pure virtual %q#D called from constructor") 11820 : G_("pure virtual %q#D called from destructor")), 11821 fn); 11822 } 11823 11824 if (DECL_OBJECT_MEMBER_FUNCTION_P (fn) 11825 && !DECL_CONSTRUCTOR_P (fn) 11826 && is_dummy_object (instance)) 11827 { 11828 instance = maybe_resolve_dummy (instance, true); 11829 if (instance == error_mark_node) 11830 call = error_mark_node; 11831 else if (!is_dummy_object (instance)) 11832 { 11833 /* We captured 'this' in the current lambda now that 11834 we know we really need it. */ 11835 cand->first_arg = instance; 11836 } 11837 else if (current_class_ptr && any_dependent_bases_p ()) 11838 /* We can't tell until instantiation time whether we can use 11839 *this as the implicit object argument. */; 11840 else 11841 { 11842 if (complain & tf_error) 11843 error ("cannot call member function %qD without object", 11844 fn); 11845 call = error_mark_node; 11846 } 11847 } 11848 11849 if (call != error_mark_node) 11850 { 11851 /* Now we know what function is being called. */ 11852 if (fn_p) 11853 *fn_p = fn; 11854 /* Build the actual CALL_EXPR. */ 11855 call = build_over_call (cand, flags, complain); 11856 11857 /* Suppress warnings for if (my_struct.operator= (x)) where 11858 my_struct is implicitly converted to bool. */ 11859 if (TREE_CODE (call) == MODIFY_EXPR) 11860 suppress_warning (call, OPT_Wparentheses); 11861 11862 /* In an expression of the form `a->f()' where `f' turns 11863 out to be a static member function, `a' is 11864 none-the-less evaluated. */ 11865 if (!is_dummy_object (instance)) 11866 call = keep_unused_object_arg (call, instance, fn); 11867 if (call != error_mark_node 11868 && DECL_DESTRUCTOR_P (cand->fn) 11869 && !VOID_TYPE_P (TREE_TYPE (call))) 11870 /* An explicit call of the form "x->~X()" has type 11871 "void". However, on platforms where destructors 11872 return "this" (i.e., those where 11873 targetm.cxx.cdtor_returns_this is true), such calls 11874 will appear to have a return value of pointer type 11875 to the low-level call machinery. We do not want to 11876 change the low-level machinery, since we want to be 11877 able to optimize "delete f()" on such platforms as 11878 "operator delete(~X(f()))" (rather than generating 11879 "t = f(), ~X(t), operator delete (t)"). */ 11880 call = build_nop (void_type_node, call); 11881 } 11882 } 11883 } 11884 11885 if (processing_template_decl && call != error_mark_node) 11886 { 11887 bool cast_to_void = false; 11888 11889 if (TREE_CODE (call) == COMPOUND_EXPR) 11890 call = TREE_OPERAND (call, 1); 11891 else if (TREE_CODE (call) == NOP_EXPR) 11892 { 11893 cast_to_void = true; 11894 call = TREE_OPERAND (call, 0); 11895 } 11896 if (INDIRECT_REF_P (call)) 11897 call = TREE_OPERAND (call, 0); 11898 11899 /* Prune all but the selected function from the original overload 11900 set so that we can avoid some duplicate work at instantiation time. */ 11901 if (really_overloaded_fn (fns)) 11902 { 11903 if (DECL_TEMPLATE_INFO (fn) 11904 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn))) 11905 { 11906 /* Use the selected template, not the specialization, so that 11907 this looks like an actual lookup result for sake of 11908 filter_memfn_lookup. */ 11909 11910 if (OVL_SINGLE_P (fns)) 11911 /* If the original overload set consists of a single function 11912 template, this isn't beneficial. */ 11913 goto skip_prune; 11914 11915 fn = ovl_make (DECL_TI_TEMPLATE (fn)); 11916 if (template_only) 11917 fn = lookup_template_function (fn, explicit_targs); 11918 } 11919 orig_fns = copy_node (orig_fns); 11920 BASELINK_FUNCTIONS (orig_fns) = fn; 11921 BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (orig_fns) = true; 11922 } 11923 11924 skip_prune: 11925 call = (build_min_non_dep_call_vec 11926 (call, 11927 build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)), 11928 orig_instance, orig_fns, NULL_TREE), 11929 orig_args)); 11930 SET_EXPR_LOCATION (call, input_location); 11931 call = convert_from_reference (call); 11932 if (cast_to_void) 11933 call = build_nop (void_type_node, call); 11934 } 11935 11936 if (orig_args != NULL) 11937 release_tree_vector (orig_args); 11938 11939 return call; 11940 } 11941 11942 /* Returns true iff standard conversion sequence ICS1 is a proper 11943 subsequence of ICS2. */ 11944 11945 static bool 11946 is_subseq (conversion *ics1, conversion *ics2) 11947 { 11948 /* We can assume that a conversion of the same code 11949 between the same types indicates a subsequence since we only get 11950 here if the types we are converting from are the same. */ 11951 11952 while (ics1->kind == ck_rvalue 11953 || ics1->kind == ck_lvalue) 11954 ics1 = next_conversion (ics1); 11955 11956 while (1) 11957 { 11958 while (ics2->kind == ck_rvalue 11959 || ics2->kind == ck_lvalue) 11960 ics2 = next_conversion (ics2); 11961 11962 if (ics2->kind == ck_user 11963 || !has_next (ics2->kind)) 11964 /* At this point, ICS1 cannot be a proper subsequence of 11965 ICS2. We can get a USER_CONV when we are comparing the 11966 second standard conversion sequence of two user conversion 11967 sequences. */ 11968 return false; 11969 11970 ics2 = next_conversion (ics2); 11971 11972 while (ics2->kind == ck_rvalue 11973 || ics2->kind == ck_lvalue) 11974 ics2 = next_conversion (ics2); 11975 11976 if (ics2->kind == ics1->kind 11977 && same_type_p (ics2->type, ics1->type) 11978 && (ics1->kind == ck_identity 11979 || same_type_p (next_conversion (ics2)->type, 11980 next_conversion (ics1)->type))) 11981 return true; 11982 } 11983 } 11984 11985 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may 11986 be any _TYPE nodes. */ 11987 11988 bool 11989 is_properly_derived_from (tree derived, tree base) 11990 { 11991 if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base)) 11992 return false; 11993 11994 /* We only allow proper derivation here. The DERIVED_FROM_P macro 11995 considers every class derived from itself. */ 11996 return (!same_type_ignoring_top_level_qualifiers_p (derived, base) 11997 && DERIVED_FROM_P (base, derived)); 11998 } 11999 12000 /* We build the ICS for an implicit object parameter as a pointer 12001 conversion sequence. However, such a sequence should be compared 12002 as if it were a reference conversion sequence. If ICS is the 12003 implicit conversion sequence for an implicit object parameter, 12004 modify it accordingly. */ 12005 12006 static void 12007 maybe_handle_implicit_object (conversion **ics) 12008 { 12009 if ((*ics)->this_p) 12010 { 12011 /* [over.match.funcs] 12012 12013 For non-static member functions, the type of the 12014 implicit object parameter is "reference to cv X" 12015 where X is the class of which the function is a 12016 member and cv is the cv-qualification on the member 12017 function declaration. */ 12018 conversion *t = *ics; 12019 tree reference_type; 12020 12021 /* The `this' parameter is a pointer to a class type. Make the 12022 implicit conversion talk about a reference to that same class 12023 type. */ 12024 reference_type = TREE_TYPE (t->type); 12025 reference_type = build_reference_type (reference_type); 12026 12027 if (t->kind == ck_qual) 12028 t = next_conversion (t); 12029 if (t->kind == ck_ptr) 12030 t = next_conversion (t); 12031 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE); 12032 t = direct_reference_binding (reference_type, t); 12033 t->this_p = 1; 12034 t->rvaluedness_matches_p = 0; 12035 *ics = t; 12036 } 12037 } 12038 12039 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion, 12040 and return the initial reference binding conversion. Otherwise, 12041 leave *ICS unchanged and return NULL. */ 12042 12043 static conversion * 12044 maybe_handle_ref_bind (conversion **ics) 12045 { 12046 if ((*ics)->kind == ck_ref_bind) 12047 { 12048 conversion *old_ics = *ics; 12049 *ics = next_conversion (old_ics); 12050 (*ics)->user_conv_p = old_ics->user_conv_p; 12051 return old_ics; 12052 } 12053 12054 return NULL; 12055 } 12056 12057 /* Get the expression at the beginning of the conversion chain C. */ 12058 12059 static tree 12060 conv_get_original_expr (conversion *c) 12061 { 12062 for (; c; c = next_conversion (c)) 12063 if (c->kind == ck_identity || c->kind == ck_ambig || c->kind == ck_aggr) 12064 return c->u.expr; 12065 return NULL_TREE; 12066 } 12067 12068 /* Return a tree representing the number of elements initialized by the 12069 list-initialization C. The caller must check that C converts to an 12070 array type. */ 12071 12072 static tree 12073 nelts_initialized_by_list_init (conversion *c) 12074 { 12075 /* If the array we're converting to has a dimension, we'll use that. */ 12076 if (TYPE_DOMAIN (c->type)) 12077 return array_type_nelts_top (c->type); 12078 else 12079 { 12080 /* Otherwise, we look at how many elements the constructor we're 12081 initializing from has. */ 12082 tree ctor = conv_get_original_expr (c); 12083 return size_int (CONSTRUCTOR_NELTS (ctor)); 12084 } 12085 } 12086 12087 /* True iff C is a conversion that binds a reference or a pointer to 12088 an array of unknown bound. */ 12089 12090 static inline bool 12091 conv_binds_to_array_of_unknown_bound (conversion *c) 12092 { 12093 /* ck_ref_bind won't have the reference stripped. */ 12094 tree type = non_reference (c->type); 12095 /* ck_qual won't have the pointer stripped. */ 12096 type = strip_pointer_operator (type); 12097 return (TREE_CODE (type) == ARRAY_TYPE 12098 && TYPE_DOMAIN (type) == NULL_TREE); 12099 } 12100 12101 /* Compare two implicit conversion sequences according to the rules set out in 12102 [over.ics.rank]. Return values: 12103 12104 1: ics1 is better than ics2 12105 -1: ics2 is better than ics1 12106 0: ics1 and ics2 are indistinguishable */ 12107 12108 static int 12109 compare_ics (conversion *ics1, conversion *ics2) 12110 { 12111 tree from_type1; 12112 tree from_type2; 12113 tree to_type1; 12114 tree to_type2; 12115 tree deref_from_type1 = NULL_TREE; 12116 tree deref_from_type2 = NULL_TREE; 12117 tree deref_to_type1 = NULL_TREE; 12118 tree deref_to_type2 = NULL_TREE; 12119 conversion_rank rank1, rank2; 12120 12121 /* REF_BINDING is nonzero if the result of the conversion sequence 12122 is a reference type. In that case REF_CONV is the reference 12123 binding conversion. */ 12124 conversion *ref_conv1; 12125 conversion *ref_conv2; 12126 12127 /* Compare badness before stripping the reference conversion. */ 12128 if (ics1->bad_p > ics2->bad_p) 12129 return -1; 12130 else if (ics1->bad_p < ics2->bad_p) 12131 return 1; 12132 12133 /* Handle implicit object parameters. */ 12134 maybe_handle_implicit_object (&ics1); 12135 maybe_handle_implicit_object (&ics2); 12136 12137 /* Handle reference parameters. */ 12138 ref_conv1 = maybe_handle_ref_bind (&ics1); 12139 ref_conv2 = maybe_handle_ref_bind (&ics2); 12140 12141 /* List-initialization sequence L1 is a better conversion sequence than 12142 list-initialization sequence L2 if L1 converts to 12143 std::initializer_list<X> for some X and L2 does not. */ 12144 if (ics1->kind == ck_list && ics2->kind != ck_list) 12145 return 1; 12146 if (ics2->kind == ck_list && ics1->kind != ck_list) 12147 return -1; 12148 12149 /* [over.ics.rank] 12150 12151 When comparing the basic forms of implicit conversion sequences (as 12152 defined in _over.best.ics_) 12153 12154 --a standard conversion sequence (_over.ics.scs_) is a better 12155 conversion sequence than a user-defined conversion sequence 12156 or an ellipsis conversion sequence, and 12157 12158 --a user-defined conversion sequence (_over.ics.user_) is a 12159 better conversion sequence than an ellipsis conversion sequence 12160 (_over.ics.ellipsis_). */ 12161 /* Use BAD_CONVERSION_RANK because we already checked for a badness 12162 mismatch. If both ICS are bad, we try to make a decision based on 12163 what would have happened if they'd been good. This is not an 12164 extension, we'll still give an error when we build up the call; this 12165 just helps us give a more helpful error message. */ 12166 rank1 = BAD_CONVERSION_RANK (ics1); 12167 rank2 = BAD_CONVERSION_RANK (ics2); 12168 12169 if (rank1 > rank2) 12170 return -1; 12171 else if (rank1 < rank2) 12172 return 1; 12173 12174 if (ics1->ellipsis_p) 12175 /* Both conversions are ellipsis conversions. */ 12176 return 0; 12177 12178 /* User-defined conversion sequence U1 is a better conversion sequence 12179 than another user-defined conversion sequence U2 if they contain the 12180 same user-defined conversion operator or constructor and if the sec- 12181 ond standard conversion sequence of U1 is better than the second 12182 standard conversion sequence of U2. */ 12183 12184 /* Handle list-conversion with the same code even though it isn't always 12185 ranked as a user-defined conversion and it doesn't have a second 12186 standard conversion sequence; it will still have the desired effect. 12187 Specifically, we need to do the reference binding comparison at the 12188 end of this function. */ 12189 12190 if (ics1->user_conv_p || ics1->kind == ck_list 12191 || ics1->kind == ck_aggr || ics2->kind == ck_aggr) 12192 { 12193 conversion *t1 = strip_standard_conversion (ics1); 12194 conversion *t2 = strip_standard_conversion (ics2); 12195 12196 if (!t1 || !t2 || t1->kind != t2->kind) 12197 return 0; 12198 else if (t1->kind == ck_user) 12199 { 12200 tree f1 = t1->cand ? t1->cand->fn : t1->type; 12201 tree f2 = t2->cand ? t2->cand->fn : t2->type; 12202 if (f1 != f2) 12203 return 0; 12204 } 12205 /* List-initialization sequence L1 is a better conversion sequence than 12206 list-initialization sequence L2 if 12207 12208 -- L1 and L2 convert to arrays of the same element type, and either 12209 the number of elements n1 initialized by L1 is less than the number 12210 of elements n2 initialized by L2, or n1=n2 and L2 converts to an array 12211 of unknown bound and L1 does not. (Added in CWG 1307 and extended by 12212 P0388R4.) */ 12213 else if (t1->kind == ck_aggr 12214 && TREE_CODE (t1->type) == ARRAY_TYPE 12215 && TREE_CODE (t2->type) == ARRAY_TYPE 12216 && same_type_p (TREE_TYPE (t1->type), TREE_TYPE (t2->type))) 12217 { 12218 tree n1 = nelts_initialized_by_list_init (t1); 12219 tree n2 = nelts_initialized_by_list_init (t2); 12220 if (tree_int_cst_lt (n1, n2)) 12221 return 1; 12222 else if (tree_int_cst_lt (n2, n1)) 12223 return -1; 12224 /* The n1 == n2 case. */ 12225 bool c1 = conv_binds_to_array_of_unknown_bound (t1); 12226 bool c2 = conv_binds_to_array_of_unknown_bound (t2); 12227 if (c1 && !c2) 12228 return -1; 12229 else if (!c1 && c2) 12230 return 1; 12231 else 12232 return 0; 12233 } 12234 else 12235 { 12236 /* For ambiguous or aggregate conversions, use the target type as 12237 a proxy for the conversion function. */ 12238 if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type)) 12239 return 0; 12240 } 12241 12242 /* We can just fall through here, after setting up 12243 FROM_TYPE1 and FROM_TYPE2. */ 12244 from_type1 = t1->type; 12245 from_type2 = t2->type; 12246 } 12247 else 12248 { 12249 conversion *t1; 12250 conversion *t2; 12251 12252 /* We're dealing with two standard conversion sequences. 12253 12254 [over.ics.rank] 12255 12256 Standard conversion sequence S1 is a better conversion 12257 sequence than standard conversion sequence S2 if 12258 12259 --S1 is a proper subsequence of S2 (comparing the conversion 12260 sequences in the canonical form defined by _over.ics.scs_, 12261 excluding any Lvalue Transformation; the identity 12262 conversion sequence is considered to be a subsequence of 12263 any non-identity conversion sequence */ 12264 12265 t1 = ics1; 12266 while (t1->kind != ck_identity) 12267 t1 = next_conversion (t1); 12268 from_type1 = t1->type; 12269 12270 t2 = ics2; 12271 while (t2->kind != ck_identity) 12272 t2 = next_conversion (t2); 12273 from_type2 = t2->type; 12274 } 12275 12276 /* One sequence can only be a subsequence of the other if they start with 12277 the same type. They can start with different types when comparing the 12278 second standard conversion sequence in two user-defined conversion 12279 sequences. */ 12280 if (same_type_p (from_type1, from_type2)) 12281 { 12282 if (is_subseq (ics1, ics2)) 12283 return 1; 12284 if (is_subseq (ics2, ics1)) 12285 return -1; 12286 } 12287 12288 /* [over.ics.rank] 12289 12290 Or, if not that, 12291 12292 --the rank of S1 is better than the rank of S2 (by the rules 12293 defined below): 12294 12295 Standard conversion sequences are ordered by their ranks: an Exact 12296 Match is a better conversion than a Promotion, which is a better 12297 conversion than a Conversion. 12298 12299 Two conversion sequences with the same rank are indistinguishable 12300 unless one of the following rules applies: 12301 12302 --A conversion that does not a convert a pointer, pointer to member, 12303 or std::nullptr_t to bool is better than one that does. 12304 12305 The ICS_STD_RANK automatically handles the pointer-to-bool rule, 12306 so that we do not have to check it explicitly. */ 12307 if (ics1->rank < ics2->rank) 12308 return 1; 12309 else if (ics2->rank < ics1->rank) 12310 return -1; 12311 12312 to_type1 = ics1->type; 12313 to_type2 = ics2->type; 12314 12315 /* A conversion from scalar arithmetic type to complex is worse than a 12316 conversion between scalar arithmetic types. */ 12317 if (same_type_p (from_type1, from_type2) 12318 && ARITHMETIC_TYPE_P (from_type1) 12319 && ARITHMETIC_TYPE_P (to_type1) 12320 && ARITHMETIC_TYPE_P (to_type2) 12321 && ((TREE_CODE (to_type1) == COMPLEX_TYPE) 12322 != (TREE_CODE (to_type2) == COMPLEX_TYPE))) 12323 { 12324 if (TREE_CODE (to_type1) == COMPLEX_TYPE) 12325 return -1; 12326 else 12327 return 1; 12328 } 12329 12330 { 12331 /* A conversion in either direction between floating-point type FP1 and 12332 floating-point type FP2 is better than a conversion in the same 12333 direction between FP1 and arithmetic type T3 if 12334 - the floating-point conversion rank of FP1 is equal to the rank of 12335 FP2, and 12336 - T3 is not a floating-point type, or T3 is a floating-point type 12337 whose rank is not equal to the rank of FP1, or the floating-point 12338 conversion subrank of FP2 is greater than the subrank of T3. */ 12339 tree fp1 = from_type1; 12340 tree fp2 = to_type1; 12341 tree fp3 = from_type2; 12342 tree t3 = to_type2; 12343 int ret = 1; 12344 if (TYPE_MAIN_VARIANT (fp2) == TYPE_MAIN_VARIANT (t3)) 12345 { 12346 std::swap (fp1, fp2); 12347 std::swap (fp3, t3); 12348 } 12349 if (TYPE_MAIN_VARIANT (fp1) == TYPE_MAIN_VARIANT (fp3) 12350 && SCALAR_FLOAT_TYPE_P (fp1) 12351 /* Only apply this rule if at least one of the 3 types is 12352 extended floating-point type, otherwise keep them as 12353 before for compatibility reasons with types like __float128. 12354 float, double and long double alone have different conversion 12355 ranks and so when just those 3 types are involved, this 12356 rule doesn't trigger. */ 12357 && (extended_float_type_p (fp1) 12358 || (SCALAR_FLOAT_TYPE_P (fp2) && extended_float_type_p (fp2)) 12359 || (SCALAR_FLOAT_TYPE_P (t3) && extended_float_type_p (t3)))) 12360 { 12361 if (TREE_CODE (fp2) != REAL_TYPE) 12362 { 12363 ret = -ret; 12364 std::swap (fp2, t3); 12365 } 12366 if (SCALAR_FLOAT_TYPE_P (fp2)) 12367 { 12368 /* cp_compare_floating_point_conversion_ranks returns -1, 0 or 1 12369 if the conversion rank is equal (-1 or 1 if the subrank is 12370 different). */ 12371 if (IN_RANGE (cp_compare_floating_point_conversion_ranks (fp1, 12372 fp2), 12373 -1, 1)) 12374 { 12375 /* Conversion ranks of FP1 and FP2 are equal. */ 12376 if (TREE_CODE (t3) != REAL_TYPE 12377 || !IN_RANGE (cp_compare_floating_point_conversion_ranks 12378 (fp1, t3), 12379 -1, 1)) 12380 /* FP1 <-> FP2 conversion is better. */ 12381 return ret; 12382 int c = cp_compare_floating_point_conversion_ranks (fp2, t3); 12383 gcc_assert (IN_RANGE (c, -1, 1)); 12384 if (c == 1) 12385 /* Conversion subrank of FP2 is greater than subrank of T3. 12386 FP1 <-> FP2 conversion is better. */ 12387 return ret; 12388 else if (c == -1) 12389 /* Conversion subrank of FP2 is less than subrank of T3. 12390 FP1 <-> T3 conversion is better. */ 12391 return -ret; 12392 } 12393 else if (SCALAR_FLOAT_TYPE_P (t3) 12394 && IN_RANGE (cp_compare_floating_point_conversion_ranks 12395 (fp1, t3), 12396 -1, 1)) 12397 /* Conversion ranks of FP1 and FP2 are not equal, conversion 12398 ranks of FP1 and T3 are equal. 12399 FP1 <-> T3 conversion is better. */ 12400 return -ret; 12401 } 12402 } 12403 } 12404 12405 if (TYPE_PTR_P (from_type1) 12406 && TYPE_PTR_P (from_type2) 12407 && TYPE_PTR_P (to_type1) 12408 && TYPE_PTR_P (to_type2)) 12409 { 12410 deref_from_type1 = TREE_TYPE (from_type1); 12411 deref_from_type2 = TREE_TYPE (from_type2); 12412 deref_to_type1 = TREE_TYPE (to_type1); 12413 deref_to_type2 = TREE_TYPE (to_type2); 12414 } 12415 /* The rules for pointers to members A::* are just like the rules 12416 for pointers A*, except opposite: if B is derived from A then 12417 A::* converts to B::*, not vice versa. For that reason, we 12418 switch the from_ and to_ variables here. */ 12419 else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2) 12420 && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2)) 12421 || (TYPE_PTRMEMFUNC_P (from_type1) 12422 && TYPE_PTRMEMFUNC_P (from_type2) 12423 && TYPE_PTRMEMFUNC_P (to_type1) 12424 && TYPE_PTRMEMFUNC_P (to_type2))) 12425 { 12426 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1); 12427 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2); 12428 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1); 12429 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2); 12430 } 12431 12432 if (deref_from_type1 != NULL_TREE 12433 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1)) 12434 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2))) 12435 { 12436 /* This was one of the pointer or pointer-like conversions. 12437 12438 [over.ics.rank] 12439 12440 --If class B is derived directly or indirectly from class A, 12441 conversion of B* to A* is better than conversion of B* to 12442 void*, and conversion of A* to void* is better than 12443 conversion of B* to void*. */ 12444 if (VOID_TYPE_P (deref_to_type1) 12445 && VOID_TYPE_P (deref_to_type2)) 12446 { 12447 if (is_properly_derived_from (deref_from_type1, 12448 deref_from_type2)) 12449 return -1; 12450 else if (is_properly_derived_from (deref_from_type2, 12451 deref_from_type1)) 12452 return 1; 12453 } 12454 else if (VOID_TYPE_P (deref_to_type1) 12455 || VOID_TYPE_P (deref_to_type2)) 12456 { 12457 if (same_type_p (deref_from_type1, deref_from_type2)) 12458 { 12459 if (VOID_TYPE_P (deref_to_type2)) 12460 { 12461 if (is_properly_derived_from (deref_from_type1, 12462 deref_to_type1)) 12463 return 1; 12464 } 12465 /* We know that DEREF_TO_TYPE1 is `void' here. */ 12466 else if (is_properly_derived_from (deref_from_type1, 12467 deref_to_type2)) 12468 return -1; 12469 } 12470 } 12471 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1)) 12472 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2))) 12473 { 12474 /* [over.ics.rank] 12475 12476 --If class B is derived directly or indirectly from class A 12477 and class C is derived directly or indirectly from B, 12478 12479 --conversion of C* to B* is better than conversion of C* to 12480 A*, 12481 12482 --conversion of B* to A* is better than conversion of C* to 12483 A* */ 12484 if (same_type_p (deref_from_type1, deref_from_type2)) 12485 { 12486 if (is_properly_derived_from (deref_to_type1, 12487 deref_to_type2)) 12488 return 1; 12489 else if (is_properly_derived_from (deref_to_type2, 12490 deref_to_type1)) 12491 return -1; 12492 } 12493 else if (same_type_p (deref_to_type1, deref_to_type2)) 12494 { 12495 if (is_properly_derived_from (deref_from_type2, 12496 deref_from_type1)) 12497 return 1; 12498 else if (is_properly_derived_from (deref_from_type1, 12499 deref_from_type2)) 12500 return -1; 12501 } 12502 } 12503 } 12504 else if (CLASS_TYPE_P (non_reference (from_type1)) 12505 && same_type_p (from_type1, from_type2)) 12506 { 12507 tree from = non_reference (from_type1); 12508 12509 /* [over.ics.rank] 12510 12511 --binding of an expression of type C to a reference of type 12512 B& is better than binding an expression of type C to a 12513 reference of type A& 12514 12515 --conversion of C to B is better than conversion of C to A, */ 12516 if (is_properly_derived_from (from, to_type1) 12517 && is_properly_derived_from (from, to_type2)) 12518 { 12519 if (is_properly_derived_from (to_type1, to_type2)) 12520 return 1; 12521 else if (is_properly_derived_from (to_type2, to_type1)) 12522 return -1; 12523 } 12524 } 12525 else if (CLASS_TYPE_P (non_reference (to_type1)) 12526 && same_type_p (to_type1, to_type2)) 12527 { 12528 tree to = non_reference (to_type1); 12529 12530 /* [over.ics.rank] 12531 12532 --binding of an expression of type B to a reference of type 12533 A& is better than binding an expression of type C to a 12534 reference of type A&, 12535 12536 --conversion of B to A is better than conversion of C to A */ 12537 if (is_properly_derived_from (from_type1, to) 12538 && is_properly_derived_from (from_type2, to)) 12539 { 12540 if (is_properly_derived_from (from_type2, from_type1)) 12541 return 1; 12542 else if (is_properly_derived_from (from_type1, from_type2)) 12543 return -1; 12544 } 12545 } 12546 12547 /* [over.ics.rank] 12548 12549 --S1 and S2 differ only in their qualification conversion and yield 12550 similar types T1 and T2 (_conv.qual_), respectively, and the cv- 12551 qualification signature of type T1 is a proper subset of the cv- 12552 qualification signature of type T2 */ 12553 if (ics1->kind == ck_qual 12554 && ics2->kind == ck_qual 12555 && same_type_p (from_type1, from_type2)) 12556 { 12557 int result = comp_cv_qual_signature (to_type1, to_type2); 12558 if (result != 0) 12559 return result; 12560 } 12561 12562 /* [over.ics.rank] 12563 12564 --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers 12565 to an implicit object parameter of a non-static member function 12566 declared without a ref-qualifier, and either S1 binds an lvalue 12567 reference to an lvalue and S2 binds an rvalue reference or S1 binds an 12568 rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x 12569 draft standard, 13.3.3.2) 12570 12571 --S1 and S2 are reference bindings (_dcl.init.ref_), and the 12572 types to which the references refer are the same type except for 12573 top-level cv-qualifiers, and the type to which the reference 12574 initialized by S2 refers is more cv-qualified than the type to 12575 which the reference initialized by S1 refers. 12576 12577 DR 1328 [over.match.best]: the context is an initialization by 12578 conversion function for direct reference binding (13.3.1.6) of a 12579 reference to function type, the return type of F1 is the same kind of 12580 reference (i.e. lvalue or rvalue) as the reference being initialized, 12581 and the return type of F2 is not. */ 12582 12583 if (ref_conv1 && ref_conv2) 12584 { 12585 if (!ref_conv1->this_p && !ref_conv2->this_p 12586 && (ref_conv1->rvaluedness_matches_p 12587 != ref_conv2->rvaluedness_matches_p) 12588 && (same_type_p (ref_conv1->type, ref_conv2->type) 12589 || (TYPE_REF_IS_RVALUE (ref_conv1->type) 12590 != TYPE_REF_IS_RVALUE (ref_conv2->type)))) 12591 { 12592 if (ref_conv1->bad_p 12593 && !same_type_p (TREE_TYPE (ref_conv1->type), 12594 TREE_TYPE (ref_conv2->type))) 12595 /* Don't prefer a bad conversion that drops cv-quals to a bad 12596 conversion with the wrong rvalueness. */ 12597 return 0; 12598 return (ref_conv1->rvaluedness_matches_p 12599 - ref_conv2->rvaluedness_matches_p); 12600 } 12601 12602 if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2)) 12603 { 12604 /* Per P0388R4: 12605 12606 void f (int(&)[]), // (1) 12607 f (int(&)[1]), // (2) 12608 f (int*); // (3) 12609 12610 (2) is better than (1), but (3) should be equal to (1) and to 12611 (2). For that reason we don't use ck_qual for (1) which would 12612 give it the cr_exact rank while (3) remains ck_identity. 12613 Therefore we compare (1) and (2) here. For (1) we'll have 12614 12615 ck_ref_bind <- ck_identity 12616 int[] & int[1] 12617 12618 so to handle this we must look at ref_conv. */ 12619 bool c1 = conv_binds_to_array_of_unknown_bound (ref_conv1); 12620 bool c2 = conv_binds_to_array_of_unknown_bound (ref_conv2); 12621 if (c1 && !c2) 12622 return -1; 12623 else if (!c1 && c2) 12624 return 1; 12625 12626 int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type)); 12627 int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type)); 12628 if (ref_conv1->bad_p) 12629 { 12630 /* Prefer the one that drops fewer cv-quals. */ 12631 tree ftype = next_conversion (ref_conv1)->type; 12632 int fquals = cp_type_quals (ftype); 12633 q1 ^= fquals; 12634 q2 ^= fquals; 12635 } 12636 return comp_cv_qualification (q2, q1); 12637 } 12638 } 12639 12640 /* [over.ics.rank] 12641 12642 Per CWG 1601: 12643 -- A conversion that promotes an enumeration whose underlying type 12644 is fixed to its underlying type is better than one that promotes to 12645 the promoted underlying type, if the two are different. */ 12646 if (ics1->rank == cr_promotion 12647 && ics2->rank == cr_promotion 12648 && UNSCOPED_ENUM_P (from_type1) 12649 && ENUM_FIXED_UNDERLYING_TYPE_P (from_type1) 12650 && same_type_p (from_type1, from_type2)) 12651 { 12652 tree utype = ENUM_UNDERLYING_TYPE (from_type1); 12653 tree prom = type_promotes_to (from_type1); 12654 if (!same_type_p (utype, prom)) 12655 { 12656 if (same_type_p (to_type1, utype) 12657 && same_type_p (to_type2, prom)) 12658 return 1; 12659 else if (same_type_p (to_type2, utype) 12660 && same_type_p (to_type1, prom)) 12661 return -1; 12662 } 12663 } 12664 12665 /* Neither conversion sequence is better than the other. */ 12666 return 0; 12667 } 12668 12669 /* The source type for this standard conversion sequence. */ 12670 12671 static tree 12672 source_type (conversion *t) 12673 { 12674 return strip_standard_conversion (t)->type; 12675 } 12676 12677 /* Note a warning about preferring WINNER to LOSER. We do this by storing 12678 a pointer to LOSER and re-running joust to produce the warning if WINNER 12679 is actually used. */ 12680 12681 static void 12682 add_warning (struct z_candidate *winner, struct z_candidate *loser) 12683 { 12684 candidate_warning *cw = (candidate_warning *) 12685 conversion_obstack_alloc (sizeof (candidate_warning)); 12686 cw->loser = loser; 12687 cw->next = winner->warnings; 12688 winner->warnings = cw; 12689 } 12690 12691 /* CAND is a constructor candidate in joust in C++17 and up. If it copies a 12692 prvalue returned from a conversion function, return true. Otherwise, return 12693 false. */ 12694 12695 static bool 12696 joust_maybe_elide_copy (z_candidate *cand) 12697 { 12698 tree fn = cand->fn; 12699 if (!DECL_COPY_CONSTRUCTOR_P (fn) && !DECL_MOVE_CONSTRUCTOR_P (fn)) 12700 return false; 12701 conversion *conv = cand->convs[0]; 12702 if (conv->kind == ck_ambig) 12703 return false; 12704 gcc_checking_assert (conv->kind == ck_ref_bind); 12705 conv = next_conversion (conv); 12706 if (conv->kind == ck_user && !TYPE_REF_P (conv->type)) 12707 { 12708 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p 12709 (conv->type, DECL_CONTEXT (fn))); 12710 z_candidate *uc = conv->cand; 12711 if (DECL_CONV_FN_P (uc->fn)) 12712 return true; 12713 } 12714 return false; 12715 } 12716 12717 /* Return the class that CAND's implicit object parameter refers to. */ 12718 12719 static tree 12720 class_of_implicit_object (z_candidate *cand) 12721 { 12722 if (!DECL_IOBJ_MEMBER_FUNCTION_P (cand->fn)) 12723 return NULL_TREE; 12724 12725 /* "For conversion functions that are implicit object member functions, 12726 the function is considered to be a member of the class of the implied 12727 object argument for the purpose of defining the type of the implicit 12728 object parameter." */ 12729 if (DECL_CONV_FN_P (cand->fn)) 12730 return TYPE_MAIN_VARIANT (TREE_TYPE (cand->first_arg)); 12731 12732 /* "For non-conversion functions that are implicit object member 12733 functions nominated by a using-declaration in a derived class, the 12734 function is considered to be a member of the derived class for the 12735 purpose of defining the type of the implicit object parameter." 12736 12737 That derived class is reflected in the conversion_path binfo. */ 12738 return BINFO_TYPE (cand->conversion_path); 12739 } 12740 12741 /* True if candidates C1 and C2 have corresponding object parameters per 12742 [basic.scope.scope]. */ 12743 12744 static bool 12745 object_parms_correspond (z_candidate *c1, tree fn1, z_candidate *c2, tree fn2) 12746 { 12747 tree context = class_of_implicit_object (c1); 12748 tree ctx2 = class_of_implicit_object (c2); 12749 if (!ctx2) 12750 /* Leave context as is. */; 12751 else if (!context) 12752 context = ctx2; 12753 else if (context != ctx2) 12754 /* This can't happen for normal function calls, since it means finding 12755 functions in multiple bases which would fail with an ambiguous lookup, 12756 but it can occur with reversed operators. */ 12757 return false; 12758 12759 return object_parms_correspond (fn1, fn2, context); 12760 } 12761 12762 /* Return whether the first parameter of C1 matches the second parameter 12763 of C2. */ 12764 12765 static bool 12766 reversed_match (z_candidate *c1, z_candidate *c2) 12767 { 12768 tree fn1 = c1->fn; 12769 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (c2->fn)); 12770 tree parm2 = TREE_VALUE (TREE_CHAIN (parms2)); 12771 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn1)) 12772 { 12773 tree ctx = class_of_implicit_object (c1); 12774 return iobj_parm_corresponds_to (fn1, parm2, ctx); 12775 } 12776 else 12777 { 12778 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1)); 12779 tree parm1 = TREE_VALUE (parms1); 12780 return same_type_p (parm1, parm2); 12781 } 12782 } 12783 12784 /* True if the defining declarations of the two candidates have equivalent 12785 parameters. MATCH_KIND controls whether we're trying to compare the 12786 original declarations (for a warning) or the actual candidates. */ 12787 12788 enum class pmatch { original, current }; 12789 12790 static bool 12791 cand_parms_match (z_candidate *c1, z_candidate *c2, pmatch match_kind) 12792 { 12793 tree fn1 = c1->fn; 12794 tree fn2 = c2->fn; 12795 bool reversed = (match_kind == pmatch::current 12796 && c1->reversed () != c2->reversed ()); 12797 if (fn1 == fn2 && !reversed) 12798 return true; 12799 if (identifier_p (fn1) || identifier_p (fn2)) 12800 return false; 12801 if (match_kind == pmatch::original) 12802 { 12803 /* We don't look at c1->template_decl because that's only set for 12804 primary templates, not e.g. non-template member functions of 12805 class templates. */ 12806 tree t1 = most_general_template (fn1); 12807 tree t2 = most_general_template (fn2); 12808 if (t1 || t2) 12809 { 12810 if (!t1 || !t2) 12811 return false; 12812 if (t1 == t2) 12813 return true; 12814 fn1 = DECL_TEMPLATE_RESULT (t1); 12815 fn2 = DECL_TEMPLATE_RESULT (t2); 12816 } 12817 } 12818 12819 else if (reversed) 12820 return (reversed_match (c1, c2) 12821 && reversed_match (c2, c1)); 12822 12823 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1)); 12824 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (fn2)); 12825 12826 if (!(DECL_FUNCTION_MEMBER_P (fn1) 12827 && DECL_FUNCTION_MEMBER_P (fn2))) 12828 /* Early escape. */; 12829 12830 else if ((DECL_INHERITED_CTOR (fn1) || DECL_INHERITED_CTOR (fn2)) 12831 && (DECL_CONTEXT (strip_inheriting_ctors (fn1)) 12832 != DECL_CONTEXT (strip_inheriting_ctors (fn2)))) 12833 /* This should really be checked for all member functions as per 12834 CWG2789, but for GCC 14 we check this only for constructors since 12835 without r15-3740 doing so would result in inconsistent handling 12836 of object parameters (such as in concepts-memfun4.C). */ 12837 return false; 12838 /* CWG2789 is not adequate, it should specify corresponding object 12839 parameters, not same typed object parameters. */ 12840 else if (!object_parms_correspond (c1, fn1, c2, fn2)) 12841 return false; 12842 else 12843 { 12844 /* We just compared the object parameters, if they don't correspond 12845 we already returned false. */ 12846 auto skip_parms = [] (tree fn, tree parms) 12847 { 12848 if (DECL_XOBJ_MEMBER_FUNCTION_P (fn)) 12849 return TREE_CHAIN (parms); 12850 else 12851 return skip_artificial_parms_for (fn, parms); 12852 }; 12853 parms1 = skip_parms (fn1, parms1); 12854 parms2 = skip_parms (fn2, parms2); 12855 } 12856 return compparms (parms1, parms2); 12857 } 12858 12859 /* True iff FN is a copy or move constructor or assignment operator. */ 12860 12861 static bool 12862 sfk_copy_or_move (tree fn) 12863 { 12864 if (TREE_CODE (fn) != FUNCTION_DECL) 12865 return false; 12866 special_function_kind sfk = special_function_p (fn); 12867 return sfk >= sfk_copy_constructor && sfk <= sfk_move_assignment; 12868 } 12869 12870 /* Compare two candidates for overloading as described in 12871 [over.match.best]. Return values: 12872 12873 1: cand1 is better than cand2 12874 -1: cand2 is better than cand1 12875 0: cand1 and cand2 are indistinguishable */ 12876 12877 static int 12878 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn, 12879 tsubst_flags_t complain) 12880 { 12881 int winner = 0; 12882 int off1 = 0, off2 = 0; 12883 size_t i; 12884 size_t len; 12885 12886 /* Candidates that involve bad conversions are always worse than those 12887 that don't. */ 12888 if (cand1->viable > cand2->viable) 12889 return 1; 12890 if (cand1->viable < cand2->viable) 12891 return -1; 12892 12893 /* If we have two pseudo-candidates for conversions to the same type, 12894 or two candidates for the same function, arbitrarily pick one. */ 12895 if (cand1->fn == cand2->fn 12896 && cand1->reversed () == cand2->reversed () 12897 && (IS_TYPE_OR_DECL_P (cand1->fn))) 12898 return 1; 12899 12900 /* Prefer a non-deleted function over an implicitly deleted move 12901 constructor or assignment operator. This differs slightly from the 12902 wording for issue 1402 (which says the move op is ignored by overload 12903 resolution), but this way produces better error messages. */ 12904 if (TREE_CODE (cand1->fn) == FUNCTION_DECL 12905 && TREE_CODE (cand2->fn) == FUNCTION_DECL 12906 && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn)) 12907 { 12908 if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn) 12909 && move_fn_p (cand1->fn)) 12910 return -1; 12911 if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn) 12912 && move_fn_p (cand2->fn)) 12913 return 1; 12914 } 12915 12916 /* a viable function F1 12917 is defined to be a better function than another viable function F2 if 12918 for all arguments i, ICSi(F1) is not a worse conversion sequence than 12919 ICSi(F2), and then */ 12920 12921 /* for some argument j, ICSj(F1) is a better conversion sequence than 12922 ICSj(F2) */ 12923 12924 /* For comparing static and non-static member functions, we ignore 12925 the implicit object parameter of the non-static function. The 12926 standard says to pretend that the static function has an object 12927 parm, but that won't work with operator overloading. */ 12928 len = cand1->num_convs; 12929 if (len != cand2->num_convs) 12930 { 12931 int static_1 = (TREE_CODE (cand1->fn) == FUNCTION_DECL 12932 && DECL_STATIC_FUNCTION_P (cand1->fn)); 12933 int static_2 = (TREE_CODE (cand2->fn) == FUNCTION_DECL 12934 && DECL_STATIC_FUNCTION_P (cand2->fn)); 12935 12936 if (TREE_CODE (cand1->fn) == FUNCTION_DECL 12937 && TREE_CODE (cand2->fn) == FUNCTION_DECL 12938 && DECL_CONSTRUCTOR_P (cand1->fn) 12939 && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn)) 12940 /* We're comparing a near-match list constructor and a near-match 12941 non-list constructor. Just treat them as unordered. */ 12942 return 0; 12943 12944 gcc_assert (static_1 != static_2); 12945 12946 if (static_1) 12947 { 12948 /* C++23 [over.best.ics.general] says: 12949 When the parameter is the implicit object parameter of a static 12950 member function, the implicit conversion sequence is a standard 12951 conversion sequence that is neither better nor worse than any 12952 other standard conversion sequence. */ 12953 if (CONVERSION_RANK (cand2->convs[0]) >= cr_user) 12954 winner = 1; 12955 off2 = 1; 12956 } 12957 else 12958 { 12959 if (CONVERSION_RANK (cand1->convs[0]) >= cr_user) 12960 winner = -1; 12961 off1 = 1; 12962 --len; 12963 } 12964 } 12965 12966 for (i = 0; i < len; ++i) 12967 { 12968 conversion *t1 = cand1->convs[i + off1]; 12969 conversion *t2 = cand2->convs[i + off2]; 12970 int comp = compare_ics (t1, t2); 12971 12972 if (comp != 0) 12973 { 12974 if ((complain & tf_warning) 12975 && warn_sign_promo 12976 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2) 12977 == cr_std + cr_promotion) 12978 && t1->kind == ck_std 12979 && t2->kind == ck_std 12980 && TREE_CODE (t1->type) == INTEGER_TYPE 12981 && TREE_CODE (t2->type) == INTEGER_TYPE 12982 && (TYPE_PRECISION (t1->type) 12983 == TYPE_PRECISION (t2->type)) 12984 && (TYPE_UNSIGNED (next_conversion (t1)->type) 12985 || (TREE_CODE (next_conversion (t1)->type) 12986 == ENUMERAL_TYPE))) 12987 { 12988 tree type = next_conversion (t1)->type; 12989 tree type1, type2; 12990 struct z_candidate *w, *l; 12991 if (comp > 0) 12992 type1 = t1->type, type2 = t2->type, 12993 w = cand1, l = cand2; 12994 else 12995 type1 = t2->type, type2 = t1->type, 12996 w = cand2, l = cand1; 12997 12998 if (warn) 12999 { 13000 warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT", 13001 type, type1, type2); 13002 warning (OPT_Wsign_promo, " in call to %qD", w->fn); 13003 } 13004 else 13005 add_warning (w, l); 13006 } 13007 13008 if (winner && comp != winner) 13009 { 13010 /* Ambiguity between normal and reversed comparison operators 13011 with the same parameter types. P2468 decided not to go with 13012 this approach to resolving the ambiguity, so pedwarn. */ 13013 if ((complain & tf_warning_or_error) 13014 && (cand1->reversed () != cand2->reversed ()) 13015 && cand_parms_match (cand1, cand2, pmatch::original)) 13016 { 13017 struct z_candidate *w, *l; 13018 if (cand2->reversed ()) 13019 winner = 1, w = cand1, l = cand2; 13020 else 13021 winner = -1, w = cand2, l = cand1; 13022 if (warn) 13023 { 13024 auto_diagnostic_group d; 13025 if (pedwarn (input_location, 0, 13026 "C++20 says that these are ambiguous, " 13027 "even though the second is reversed:")) 13028 { 13029 print_z_candidate (input_location, 13030 N_("candidate 1:"), w); 13031 print_z_candidate (input_location, 13032 N_("candidate 2:"), l); 13033 if (w->fn == l->fn 13034 && DECL_IOBJ_MEMBER_FUNCTION_P (w->fn) 13035 && (type_memfn_quals (TREE_TYPE (w->fn)) 13036 & TYPE_QUAL_CONST) == 0) 13037 { 13038 /* Suggest adding const to 13039 struct A { bool operator==(const A&); }; */ 13040 tree parmtype 13041 = FUNCTION_FIRST_USER_PARMTYPE (w->fn); 13042 parmtype = TREE_VALUE (parmtype); 13043 if (TYPE_REF_P (parmtype) 13044 && TYPE_READONLY (TREE_TYPE (parmtype)) 13045 && (same_type_ignoring_top_level_qualifiers_p 13046 (TREE_TYPE (parmtype), 13047 DECL_CONTEXT (w->fn)))) 13048 inform (DECL_SOURCE_LOCATION (w->fn), 13049 "try making the operator a %<const%> " 13050 "member function"); 13051 } 13052 } 13053 } 13054 else 13055 add_warning (w, l); 13056 return winner; 13057 } 13058 13059 winner = 0; 13060 goto tweak; 13061 } 13062 winner = comp; 13063 } 13064 } 13065 13066 /* warn about confusing overload resolution for user-defined conversions, 13067 either between a constructor and a conversion op, or between two 13068 conversion ops. */ 13069 if ((complain & tf_warning) 13070 /* In C++17, the constructor might have been elided, which means that 13071 an originally null ->second_conv could become non-null. */ 13072 && winner && warn_conversion && cand1->second_conv && cand2->second_conv 13073 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn)) 13074 && winner != compare_ics (cand1->second_conv, cand2->second_conv)) 13075 { 13076 struct z_candidate *w, *l; 13077 bool give_warning = false; 13078 13079 if (winner == 1) 13080 w = cand1, l = cand2; 13081 else 13082 w = cand2, l = cand1; 13083 13084 /* We don't want to complain about `X::operator T1 ()' 13085 beating `X::operator T2 () const', when T2 is a no less 13086 cv-qualified version of T1. */ 13087 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn) 13088 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn)) 13089 { 13090 tree t = TREE_TYPE (TREE_TYPE (l->fn)); 13091 tree f = TREE_TYPE (TREE_TYPE (w->fn)); 13092 13093 if (TREE_CODE (t) == TREE_CODE (f) && INDIRECT_TYPE_P (t)) 13094 { 13095 t = TREE_TYPE (t); 13096 f = TREE_TYPE (f); 13097 } 13098 if (!comp_ptr_ttypes (t, f)) 13099 give_warning = true; 13100 } 13101 else 13102 give_warning = true; 13103 13104 if (!give_warning) 13105 /*NOP*/; 13106 else if (warn) 13107 { 13108 tree source = source_type (w->convs[0]); 13109 if (INDIRECT_TYPE_P (source)) 13110 source = TREE_TYPE (source); 13111 auto_diagnostic_group d; 13112 if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn) 13113 && warning (OPT_Wconversion, " for conversion from %qH to %qI", 13114 source, w->second_conv->type)) 13115 { 13116 inform (input_location, " because conversion sequence " 13117 "for the argument is better"); 13118 } 13119 } 13120 else 13121 add_warning (w, l); 13122 } 13123 13124 if (winner) 13125 return winner; 13126 13127 /* DR 495 moved this tiebreaker above the template ones. */ 13128 /* or, if not that, 13129 the context is an initialization by user-defined conversion (see 13130 _dcl.init_ and _over.match.user_) and the standard conversion 13131 sequence from the return type of F1 to the destination type (i.e., 13132 the type of the entity being initialized) is a better conversion 13133 sequence than the standard conversion sequence from the return type 13134 of F2 to the destination type. */ 13135 13136 if (cand1->second_conv) 13137 { 13138 winner = compare_ics (cand1->second_conv, cand2->second_conv); 13139 if (winner) 13140 return winner; 13141 } 13142 13143 /* CWG2735 (PR109247): A copy/move ctor/op= for which its operand uses an 13144 explicit conversion (due to list-initialization) is worse. */ 13145 { 13146 z_candidate *sp = nullptr; 13147 if (sfk_copy_or_move (cand1->fn)) 13148 sp = cand1; 13149 if (sfk_copy_or_move (cand2->fn)) 13150 sp = sp ? nullptr : cand2; 13151 if (sp) 13152 { 13153 conversion *conv = sp->convs[!DECL_CONSTRUCTOR_P (sp->fn)]; 13154 if (conv->user_conv_p) 13155 for (; conv; conv = next_conversion (conv)) 13156 if (conv->kind == ck_user 13157 && DECL_P (conv->cand->fn) 13158 && DECL_NONCONVERTING_P (conv->cand->fn)) 13159 return (sp == cand1) ? -1 : 1; 13160 } 13161 } 13162 13163 /* DR2327: C++17 copy elision in [over.match.ctor] (direct-init) context. 13164 The standard currently says that only constructors are candidates, but if 13165 one copies a prvalue returned by a conversion function we prefer that. 13166 13167 Clang does something similar, as discussed at 13168 http://lists.isocpp.org/core/2017/10/3166.php 13169 http://lists.isocpp.org/core/2019/03/5721.php */ 13170 if (len == 1 && cxx_dialect >= cxx17 13171 && DECL_P (cand1->fn) 13172 && DECL_COMPLETE_CONSTRUCTOR_P (cand1->fn) 13173 && !(cand1->flags & LOOKUP_ONLYCONVERTING)) 13174 { 13175 bool elided1 = joust_maybe_elide_copy (cand1); 13176 bool elided2 = joust_maybe_elide_copy (cand2); 13177 winner = elided1 - elided2; 13178 if (winner) 13179 return winner; 13180 } 13181 13182 /* or, if not that, 13183 F1 is a non-template function and F2 is a template function 13184 specialization. */ 13185 13186 if (!cand1->template_decl && cand2->template_decl) 13187 return 1; 13188 else if (cand1->template_decl && !cand2->template_decl) 13189 return -1; 13190 13191 /* or, if not that, 13192 F1 and F2 are template functions and the function template for F1 is 13193 more specialized than the template for F2 according to the partial 13194 ordering rules. */ 13195 13196 if (cand1->template_decl && cand2->template_decl) 13197 { 13198 winner = more_specialized_fn 13199 (TI_TEMPLATE (cand1->template_decl), 13200 TI_TEMPLATE (cand2->template_decl), 13201 /* [temp.func.order]: The presence of unused ellipsis and default 13202 arguments has no effect on the partial ordering of function 13203 templates. add_function_candidate() will not have 13204 counted the "this" argument for constructors. */ 13205 cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn)); 13206 if (winner) 13207 return winner; 13208 } 13209 13210 /* Concepts: F1 and F2 are non-template functions with the same 13211 parameter-type-lists, and F1 is more constrained than F2 according to the 13212 partial ordering of constraints described in 13.5.4. */ 13213 13214 if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn) 13215 && !cand1->template_decl && !cand2->template_decl 13216 && cand_parms_match (cand1, cand2, pmatch::current)) 13217 { 13218 winner = more_constrained (cand1->fn, cand2->fn); 13219 if (winner) 13220 return winner; 13221 } 13222 13223 /* F2 is a rewritten candidate (12.4.1.2) and F1 is not, or F1 and F2 are 13224 rewritten candidates, and F2 is a synthesized candidate with reversed 13225 order of parameters and F1 is not. */ 13226 if (cand1->rewritten ()) 13227 { 13228 if (!cand2->rewritten ()) 13229 return -1; 13230 if (!cand1->reversed () && cand2->reversed ()) 13231 return 1; 13232 if (cand1->reversed () && !cand2->reversed ()) 13233 return -1; 13234 } 13235 else if (cand2->rewritten ()) 13236 return 1; 13237 13238 if (deduction_guide_p (cand1->fn)) 13239 { 13240 gcc_assert (deduction_guide_p (cand2->fn)); 13241 13242 /* F1 and F2 are generated from class template argument deduction for a 13243 class D, and F2 is generated from inheriting constructors from a base 13244 class of D while F1 is not, and for each explicit function argument, 13245 the corresponding parameters of F1 and F2 are either both ellipses or 13246 have the same type. */ 13247 bool inherited1 = inherited_guide_p (cand1->fn); 13248 bool inherited2 = inherited_guide_p (cand2->fn); 13249 if (int diff = inherited2 - inherited1) 13250 { 13251 for (i = 0; i < len; ++i) 13252 { 13253 conversion *t1 = cand1->convs[i + off1]; 13254 conversion *t2 = cand2->convs[i + off2]; 13255 /* ??? It seems the ellipses part of this tiebreaker isn't 13256 needed since a mismatch should have broken the tie earlier 13257 during ICS comparison. */ 13258 gcc_checking_assert (t1->ellipsis_p == t2->ellipsis_p); 13259 if (!same_type_p (t1->type, t2->type)) 13260 break; 13261 } 13262 if (i == len) 13263 return diff; 13264 } 13265 13266 /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */ 13267 /* We distinguish between candidates from an explicit deduction guide and 13268 candidates built from a constructor based on DECL_ARTIFICIAL. */ 13269 int art1 = DECL_ARTIFICIAL (cand1->fn); 13270 int art2 = DECL_ARTIFICIAL (cand2->fn); 13271 if (art1 != art2) 13272 return art2 - art1; 13273 13274 if (art1) 13275 { 13276 /* Prefer the special copy guide over a declared copy/move 13277 constructor. */ 13278 if (copy_guide_p (cand1->fn)) 13279 return 1; 13280 if (copy_guide_p (cand2->fn)) 13281 return -1; 13282 13283 /* Prefer a candidate generated from a non-template constructor. */ 13284 int tg1 = template_guide_p (cand1->fn); 13285 int tg2 = template_guide_p (cand2->fn); 13286 if (tg1 != tg2) 13287 return tg2 - tg1; 13288 } 13289 } 13290 13291 /* F1 is a member of a class D, F2 is a member of a base class B of D, and 13292 for all arguments the corresponding parameters of F1 and F2 have the same 13293 type (CWG 2273/2277). */ 13294 if (DECL_P (cand1->fn) && DECL_CLASS_SCOPE_P (cand1->fn) 13295 && !DECL_CONV_FN_P (cand1->fn) 13296 && DECL_P (cand2->fn) && DECL_CLASS_SCOPE_P (cand2->fn) 13297 && !DECL_CONV_FN_P (cand2->fn)) 13298 { 13299 tree base1 = DECL_CONTEXT (strip_inheriting_ctors (cand1->fn)); 13300 tree base2 = DECL_CONTEXT (strip_inheriting_ctors (cand2->fn)); 13301 13302 bool used1 = false; 13303 bool used2 = false; 13304 if (base1 == base2) 13305 /* No difference. */; 13306 else if (DERIVED_FROM_P (base1, base2)) 13307 used1 = true; 13308 else if (DERIVED_FROM_P (base2, base1)) 13309 used2 = true; 13310 13311 if (int diff = used2 - used1) 13312 { 13313 for (i = 0; i < len; ++i) 13314 { 13315 conversion *t1 = cand1->convs[i + off1]; 13316 conversion *t2 = cand2->convs[i + off2]; 13317 if (!same_type_p (t1->type, t2->type)) 13318 break; 13319 } 13320 if (i == len) 13321 return diff; 13322 } 13323 } 13324 13325 /* Check whether we can discard a builtin candidate, either because we 13326 have two identical ones or matching builtin and non-builtin candidates. 13327 13328 (Pedantically in the latter case the builtin which matched the user 13329 function should not be added to the overload set, but we spot it here. 13330 13331 [over.match.oper] 13332 ... the builtin candidates include ... 13333 - do not have the same parameter type list as any non-template 13334 non-member candidate. */ 13335 13336 if (identifier_p (cand1->fn) || identifier_p (cand2->fn)) 13337 { 13338 for (i = 0; i < len; ++i) 13339 if (!same_type_p (cand1->convs[i]->type, 13340 cand2->convs[i]->type)) 13341 break; 13342 if (i == cand1->num_convs) 13343 { 13344 if (cand1->fn == cand2->fn) 13345 /* Two built-in candidates; arbitrarily pick one. */ 13346 return 1; 13347 else if (identifier_p (cand1->fn)) 13348 /* cand1 is built-in; prefer cand2. */ 13349 return -1; 13350 else 13351 /* cand2 is built-in; prefer cand1. */ 13352 return 1; 13353 } 13354 } 13355 13356 /* For candidates of a multi-versioned function, make the version with 13357 the highest priority win. This version will be checked for dispatching 13358 first. If this version can be inlined into the caller, the front-end 13359 will simply make a direct call to this function. */ 13360 13361 if (TREE_CODE (cand1->fn) == FUNCTION_DECL 13362 && DECL_FUNCTION_VERSIONED (cand1->fn) 13363 && TREE_CODE (cand2->fn) == FUNCTION_DECL 13364 && DECL_FUNCTION_VERSIONED (cand2->fn)) 13365 { 13366 tree f1 = TREE_TYPE (cand1->fn); 13367 tree f2 = TREE_TYPE (cand2->fn); 13368 tree p1 = TYPE_ARG_TYPES (f1); 13369 tree p2 = TYPE_ARG_TYPES (f2); 13370 13371 /* Check if cand1->fn and cand2->fn are versions of the same function. It 13372 is possible that cand1->fn and cand2->fn are function versions but of 13373 different functions. Check types to see if they are versions of the same 13374 function. */ 13375 if (compparms (p1, p2) 13376 && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2))) 13377 { 13378 /* Always make the version with the higher priority, more 13379 specialized, win. */ 13380 gcc_assert (targetm.compare_version_priority); 13381 if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0) 13382 return 1; 13383 else 13384 return -1; 13385 } 13386 } 13387 13388 /* If the two function declarations represent the same function (this can 13389 happen with declarations in multiple scopes and arg-dependent lookup), 13390 arbitrarily choose one. But first make sure the default args we're 13391 using match. */ 13392 if (DECL_P (cand1->fn) && DECL_P (cand2->fn) 13393 && equal_functions (cand1->fn, cand2->fn)) 13394 { 13395 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn)); 13396 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn)); 13397 13398 gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn)); 13399 13400 for (i = 0; i < len; ++i) 13401 { 13402 /* Don't crash if the fn is variadic. */ 13403 if (!parms1) 13404 break; 13405 parms1 = TREE_CHAIN (parms1); 13406 parms2 = TREE_CHAIN (parms2); 13407 } 13408 13409 if (off1) 13410 parms1 = TREE_CHAIN (parms1); 13411 else if (off2) 13412 parms2 = TREE_CHAIN (parms2); 13413 13414 for (; parms1; ++i) 13415 { 13416 if (!cp_tree_equal (TREE_PURPOSE (parms1), 13417 TREE_PURPOSE (parms2))) 13418 { 13419 if (warn) 13420 { 13421 if (complain & tf_error) 13422 { 13423 auto_diagnostic_group d; 13424 if (permerror (input_location, 13425 "default argument mismatch in " 13426 "overload resolution")) 13427 { 13428 inform (DECL_SOURCE_LOCATION (cand1->fn), 13429 " candidate 1: %q#F", cand1->fn); 13430 inform (DECL_SOURCE_LOCATION (cand2->fn), 13431 " candidate 2: %q#F", cand2->fn); 13432 } 13433 } 13434 else 13435 return 0; 13436 } 13437 else 13438 add_warning (cand1, cand2); 13439 break; 13440 } 13441 parms1 = TREE_CHAIN (parms1); 13442 parms2 = TREE_CHAIN (parms2); 13443 } 13444 13445 return 1; 13446 } 13447 13448 tweak: 13449 13450 /* Extension: If the worst conversion for one candidate is better than the 13451 worst conversion for the other, take the first. */ 13452 if (!pedantic && (complain & tf_warning_or_error)) 13453 { 13454 conversion_rank rank1 = cr_identity, rank2 = cr_identity; 13455 struct z_candidate *w = 0, *l = 0; 13456 13457 for (i = 0; i < len; ++i) 13458 { 13459 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1) 13460 rank1 = CONVERSION_RANK (cand1->convs[i+off1]); 13461 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2) 13462 rank2 = CONVERSION_RANK (cand2->convs[i + off2]); 13463 } 13464 if (rank1 < rank2) 13465 winner = 1, w = cand1, l = cand2; 13466 if (rank1 > rank2) 13467 winner = -1, w = cand2, l = cand1; 13468 if (winner) 13469 { 13470 /* Don't choose a deleted function over ambiguity. */ 13471 if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn)) 13472 return 0; 13473 if (warn) 13474 { 13475 auto_diagnostic_group d; 13476 if (pedwarn (input_location, 0, 13477 "ISO C++ says that these are ambiguous, even " 13478 "though the worst conversion for the first is " 13479 "better than the worst conversion for the second:")) 13480 { 13481 print_z_candidate (input_location, N_("candidate 1:"), w); 13482 print_z_candidate (input_location, N_("candidate 2:"), l); 13483 } 13484 } 13485 else 13486 add_warning (w, l); 13487 return winner; 13488 } 13489 } 13490 13491 gcc_assert (!winner); 13492 return 0; 13493 } 13494 13495 /* Given a list of candidates for overloading, find the best one, if any. 13496 This algorithm has a worst case of O(2n) (winner is last), and a best 13497 case of O(n/2) (totally ambiguous); much better than a sorting 13498 algorithm. The candidates list is assumed to be sorted according 13499 to viability (via splice_viable). */ 13500 13501 static struct z_candidate * 13502 tourney (struct z_candidate *candidates, tsubst_flags_t complain) 13503 { 13504 struct z_candidate **champ = &candidates, **challenger; 13505 int fate; 13506 struct z_candidate *previous_worse_champ = nullptr; 13507 13508 /* Walk through the list once, comparing each current champ to the next 13509 candidate, knocking out a candidate or two with each comparison. */ 13510 13511 for (challenger = &candidates->next; *challenger && (*challenger)->viable; ) 13512 { 13513 fate = joust (*champ, *challenger, 0, complain); 13514 if (fate == 1) 13515 challenger = &(*challenger)->next; 13516 else if (fate == -1) 13517 { 13518 previous_worse_champ = *champ; 13519 champ = challenger; 13520 challenger = &(*challenger)->next; 13521 } 13522 else 13523 { 13524 previous_worse_champ = nullptr; 13525 champ = &(*challenger)->next; 13526 if (!*champ || !(*champ)->viable 13527 || (*champ)->viable < (*challenger)->viable) 13528 { 13529 champ = nullptr; 13530 break; 13531 } 13532 challenger = &(*champ)->next; 13533 } 13534 } 13535 13536 /* Make sure the champ is better than all the candidates it hasn't yet 13537 been compared to. */ 13538 13539 if (champ) 13540 for (challenger = &candidates; 13541 challenger != champ; 13542 challenger = &(*challenger)->next) 13543 { 13544 if (*challenger == previous_worse_champ) 13545 /* We already know this candidate is worse than the champ. */ 13546 continue; 13547 fate = joust (*champ, *challenger, 0, complain); 13548 if (fate != 1) 13549 { 13550 champ = nullptr; 13551 break; 13552 } 13553 } 13554 13555 if (!champ) 13556 return nullptr; 13557 13558 /* Move the champ to the front of the candidate list. */ 13559 13560 if (champ != &candidates) 13561 { 13562 z_candidate *saved_champ = *champ; 13563 *champ = saved_champ->next; 13564 saved_champ->next = candidates; 13565 candidates = saved_champ; 13566 } 13567 13568 return candidates; 13569 } 13570 13571 /* Returns nonzero if things of type FROM can be converted to TO. */ 13572 13573 bool 13574 can_convert (tree to, tree from, tsubst_flags_t complain) 13575 { 13576 tree arg = NULL_TREE; 13577 /* implicit_conversion only considers user-defined conversions 13578 if it has an expression for the call argument list. */ 13579 if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to)) 13580 arg = build_stub_object (from); 13581 return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain); 13582 } 13583 13584 /* Returns nonzero if things of type FROM can be converted to TO with a 13585 standard conversion. */ 13586 13587 bool 13588 can_convert_standard (tree to, tree from, tsubst_flags_t complain) 13589 { 13590 return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain); 13591 } 13592 13593 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */ 13594 13595 bool 13596 can_convert_arg (tree to, tree from, tree arg, int flags, 13597 tsubst_flags_t complain) 13598 { 13599 conversion *t; 13600 bool ok_p; 13601 13602 conversion_obstack_sentinel cos; 13603 /* We want to discard any access checks done for this test, 13604 as we might not be in the appropriate access context and 13605 we'll do the check again when we actually perform the 13606 conversion. */ 13607 push_deferring_access_checks (dk_deferred); 13608 13609 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false, 13610 flags, complain); 13611 ok_p = (t && !t->bad_p); 13612 13613 /* Discard the access checks now. */ 13614 pop_deferring_access_checks (); 13615 13616 return ok_p; 13617 } 13618 13619 /* Like can_convert_arg, but allows dubious conversions as well. */ 13620 13621 bool 13622 can_convert_arg_bad (tree to, tree from, tree arg, int flags, 13623 tsubst_flags_t complain) 13624 { 13625 conversion *t; 13626 13627 conversion_obstack_sentinel cos; 13628 /* Try to perform the conversion. */ 13629 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false, 13630 flags, complain); 13631 13632 return t != NULL; 13633 } 13634 13635 /* Return an IMPLICIT_CONV_EXPR from EXPR to TYPE with bits set from overload 13636 resolution FLAGS. */ 13637 13638 tree 13639 build_implicit_conv_flags (tree type, tree expr, int flags) 13640 { 13641 /* In a template, we are only concerned about determining the 13642 type of non-dependent expressions, so we do not have to 13643 perform the actual conversion. But for initializers, we 13644 need to be able to perform it at instantiation 13645 (or instantiate_non_dependent_expr) time. */ 13646 expr = build1 (IMPLICIT_CONV_EXPR, type, expr); 13647 if (!(flags & LOOKUP_ONLYCONVERTING)) 13648 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true; 13649 if (flags & LOOKUP_NO_NARROWING) 13650 IMPLICIT_CONV_EXPR_BRACED_INIT (expr) = true; 13651 return expr; 13652 } 13653 13654 /* Convert EXPR to TYPE. Return the converted expression. 13655 13656 Note that we allow bad conversions here because by the time we get to 13657 this point we are committed to doing the conversion. If we end up 13658 doing a bad conversion, convert_like will complain. */ 13659 13660 tree 13661 perform_implicit_conversion_flags (tree type, tree expr, 13662 tsubst_flags_t complain, int flags) 13663 { 13664 conversion *conv; 13665 location_t loc = cp_expr_loc_or_input_loc (expr); 13666 13667 if (error_operand_p (expr)) 13668 return error_mark_node; 13669 13670 conversion_obstack_sentinel cos; 13671 13672 conv = implicit_conversion (type, TREE_TYPE (expr), expr, 13673 /*c_cast_p=*/false, 13674 flags, complain); 13675 13676 if (!conv) 13677 { 13678 if (complain & tf_error) 13679 implicit_conversion_error (loc, type, expr); 13680 expr = error_mark_node; 13681 } 13682 else if (processing_template_decl && conv->kind != ck_identity) 13683 expr = build_implicit_conv_flags (type, expr, flags); 13684 else 13685 { 13686 /* Give a conversion call the same location as expr. */ 13687 iloc_sentinel il (loc); 13688 expr = convert_like (conv, expr, complain); 13689 } 13690 13691 return expr; 13692 } 13693 13694 tree 13695 perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain) 13696 { 13697 return perform_implicit_conversion_flags (type, expr, complain, 13698 LOOKUP_IMPLICIT); 13699 } 13700 13701 /* Convert EXPR to TYPE (as a direct-initialization) if that is 13702 permitted. If the conversion is valid, the converted expression is 13703 returned. Otherwise, NULL_TREE is returned, except in the case 13704 that TYPE is a class type; in that case, an error is issued. If 13705 C_CAST_P is true, then this direct-initialization is taking 13706 place as part of a static_cast being attempted as part of a C-style 13707 cast. */ 13708 13709 tree 13710 perform_direct_initialization_if_possible (tree type, 13711 tree expr, 13712 bool c_cast_p, 13713 tsubst_flags_t complain) 13714 { 13715 conversion *conv; 13716 13717 if (type == error_mark_node || error_operand_p (expr)) 13718 return error_mark_node; 13719 /* [dcl.init] 13720 13721 If the destination type is a (possibly cv-qualified) class type: 13722 13723 -- If the initialization is direct-initialization ..., 13724 constructors are considered. 13725 13726 -- If overload resolution is successful, the selected constructor 13727 is called to initialize the object, with the initializer expression 13728 or expression-list as its argument(s). 13729 13730 -- Otherwise, if no constructor is viable, the destination type is 13731 a (possibly cv-qualified) aggregate class A, and the initializer is 13732 a parenthesized expression-list, the object is initialized as 13733 follows... */ 13734 if (CLASS_TYPE_P (type)) 13735 { 13736 releasing_vec args (make_tree_vector_single (expr)); 13737 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier, 13738 &args, type, LOOKUP_NORMAL, complain); 13739 return build_cplus_new (type, expr, complain); 13740 } 13741 13742 conversion_obstack_sentinel cos; 13743 13744 conv = implicit_conversion (type, TREE_TYPE (expr), expr, 13745 c_cast_p, 13746 LOOKUP_NORMAL, complain); 13747 if (!conv || conv->bad_p) 13748 expr = NULL_TREE; 13749 else if (processing_template_decl && conv->kind != ck_identity) 13750 { 13751 /* In a template, we are only concerned about determining the 13752 type of non-dependent expressions, so we do not have to 13753 perform the actual conversion. But for initializers, we 13754 need to be able to perform it at instantiation 13755 (or instantiate_non_dependent_expr) time. */ 13756 expr = build1 (IMPLICIT_CONV_EXPR, type, expr); 13757 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true; 13758 } 13759 else 13760 expr = convert_like (conv, expr, NULL_TREE, 0, 13761 /*issue_conversion_warnings=*/false, 13762 c_cast_p, /*nested_p=*/false, complain); 13763 13764 return expr; 13765 } 13766 13767 /* When initializing a reference that lasts longer than a full-expression, 13768 this special rule applies: 13769 13770 [class.temporary] 13771 13772 The temporary to which the reference is bound or the temporary 13773 that is the complete object to which the reference is bound 13774 persists for the lifetime of the reference. 13775 13776 The temporaries created during the evaluation of the expression 13777 initializing the reference, except the temporary to which the 13778 reference is bound, are destroyed at the end of the 13779 full-expression in which they are created. 13780 13781 In that case, we store the converted expression into a new 13782 VAR_DECL in a new scope. 13783 13784 However, we want to be careful not to create temporaries when 13785 they are not required. For example, given: 13786 13787 struct B {}; 13788 struct D : public B {}; 13789 D f(); 13790 const B& b = f(); 13791 13792 there is no need to copy the return value from "f"; we can just 13793 extend its lifetime. Similarly, given: 13794 13795 struct S {}; 13796 struct T { operator S(); }; 13797 T t; 13798 const S& s = t; 13799 13800 we can extend the lifetime of the return value of the conversion 13801 operator. 13802 13803 The next several functions are involved in this lifetime extension. */ 13804 13805 /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The 13806 reference is being bound to a temporary. Create and return a new 13807 VAR_DECL with the indicated TYPE; this variable will store the value to 13808 which the reference is bound. */ 13809 13810 tree 13811 make_temporary_var_for_ref_to_temp (tree decl, tree type) 13812 { 13813 tree var = create_temporary_var (type); 13814 13815 /* Register the variable. */ 13816 if (VAR_P (decl) 13817 && (TREE_STATIC (decl) || CP_DECL_THREAD_LOCAL_P (decl))) 13818 { 13819 /* Namespace-scope or local static; give it a mangled name. */ 13820 13821 /* If an initializer is visible to multiple translation units, those 13822 translation units must agree on the addresses of the 13823 temporaries. Therefore the temporaries must be given a consistent name 13824 and vague linkage. The mangled name of a temporary is the name of the 13825 non-temporary object in whose initializer they appear, prefixed with 13826 GR and suffixed with a sequence number mangled using the usual rules 13827 for a seq-id. Temporaries are numbered with a pre-order, depth-first, 13828 left-to-right walk of the complete initializer. */ 13829 copy_linkage (var, decl); 13830 13831 tree name = mangle_ref_init_variable (decl); 13832 DECL_NAME (var) = name; 13833 SET_DECL_ASSEMBLER_NAME (var, name); 13834 13835 /* Set the context to make the variable mergeable in modules. */ 13836 DECL_CONTEXT (var) = current_scope (); 13837 } 13838 else 13839 /* Create a new cleanup level if necessary. */ 13840 maybe_push_cleanup_level (type); 13841 13842 return pushdecl (var); 13843 } 13844 13845 /* EXPR is the initializer for a variable DECL of reference or 13846 std::initializer_list type. Create, push and return a new VAR_DECL 13847 for the initializer so that it will live as long as DECL. Any 13848 cleanup for the new variable is returned through CLEANUP, and the 13849 code to initialize the new variable is returned through INITP. */ 13850 13851 static tree 13852 set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups, 13853 tree *initp, tree *cond_guard) 13854 { 13855 tree init; 13856 tree type; 13857 tree var; 13858 13859 /* Create the temporary variable. */ 13860 type = TREE_TYPE (expr); 13861 var = make_temporary_var_for_ref_to_temp (decl, type); 13862 layout_decl (var, 0); 13863 /* If the rvalue is the result of a function call it will be 13864 a TARGET_EXPR. If it is some other construct (such as a 13865 member access expression where the underlying object is 13866 itself the result of a function call), turn it into a 13867 TARGET_EXPR here. It is important that EXPR be a 13868 TARGET_EXPR below since otherwise the INIT_EXPR will 13869 attempt to make a bitwise copy of EXPR to initialize 13870 VAR. */ 13871 if (TREE_CODE (expr) != TARGET_EXPR) 13872 expr = get_target_expr (expr); 13873 else 13874 { 13875 if (TREE_ADDRESSABLE (expr)) 13876 TREE_ADDRESSABLE (var) = 1; 13877 if (DECL_MERGEABLE (TARGET_EXPR_SLOT (expr))) 13878 DECL_MERGEABLE (var) = true; 13879 } 13880 13881 if (TREE_CODE (decl) == FIELD_DECL 13882 && extra_warnings && !warning_suppressed_p (decl)) 13883 { 13884 warning (OPT_Wextra, "a temporary bound to %qD only persists " 13885 "until the constructor exits", decl); 13886 suppress_warning (decl); 13887 } 13888 13889 /* Recursively extend temps in this initializer. */ 13890 TARGET_EXPR_INITIAL (expr) 13891 = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups, 13892 cond_guard); 13893 13894 /* Any reference temp has a non-trivial initializer. */ 13895 DECL_NONTRIVIALLY_INITIALIZED_P (var) = true; 13896 13897 /* If the initializer is constant, put it in DECL_INITIAL so we get 13898 static initialization and use in constant expressions. */ 13899 init = maybe_constant_init (expr, var, /*manifestly_const_eval=*/true); 13900 /* As in store_init_value. */ 13901 init = cp_fully_fold (init); 13902 if (TREE_CONSTANT (init)) 13903 { 13904 if (literal_type_p (type) 13905 && CP_TYPE_CONST_NON_VOLATILE_P (type) 13906 && !TYPE_HAS_MUTABLE_P (type)) 13907 { 13908 /* 5.19 says that a constant expression can include an 13909 lvalue-rvalue conversion applied to "a glvalue of literal type 13910 that refers to a non-volatile temporary object initialized 13911 with a constant expression". Rather than try to communicate 13912 that this VAR_DECL is a temporary, just mark it constexpr. */ 13913 DECL_DECLARED_CONSTEXPR_P (var) = true; 13914 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true; 13915 TREE_CONSTANT (var) = true; 13916 TREE_READONLY (var) = true; 13917 } 13918 DECL_INITIAL (var) = init; 13919 init = NULL_TREE; 13920 } 13921 else 13922 /* Create the INIT_EXPR that will initialize the temporary 13923 variable. */ 13924 init = split_nonconstant_init (var, expr); 13925 if (at_function_scope_p ()) 13926 { 13927 add_decl_expr (var); 13928 13929 if (TREE_STATIC (var)) 13930 init = add_stmt_to_compound (init, register_dtor_fn (var)); 13931 else 13932 { 13933 tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error); 13934 if (cleanup) 13935 { 13936 if (cond_guard && cleanup != error_mark_node) 13937 { 13938 if (*cond_guard == NULL_TREE) 13939 { 13940 *cond_guard = build_local_temp (boolean_type_node); 13941 add_decl_expr (*cond_guard); 13942 tree set = cp_build_modify_expr (UNKNOWN_LOCATION, 13943 *cond_guard, NOP_EXPR, 13944 boolean_false_node, 13945 tf_warning_or_error); 13946 finish_expr_stmt (set); 13947 } 13948 cleanup = build3 (COND_EXPR, void_type_node, 13949 *cond_guard, cleanup, NULL_TREE); 13950 } 13951 vec_safe_push (*cleanups, cleanup); 13952 } 13953 } 13954 13955 /* We must be careful to destroy the temporary only 13956 after its initialization has taken place. If the 13957 initialization throws an exception, then the 13958 destructor should not be run. We cannot simply 13959 transform INIT into something like: 13960 13961 (INIT, ({ CLEANUP_STMT; })) 13962 13963 because emit_local_var always treats the 13964 initializer as a full-expression. Thus, the 13965 destructor would run too early; it would run at the 13966 end of initializing the reference variable, rather 13967 than at the end of the block enclosing the 13968 reference variable. 13969 13970 The solution is to pass back a cleanup expression 13971 which the caller is responsible for attaching to 13972 the statement tree. */ 13973 } 13974 else 13975 { 13976 rest_of_decl_compilation (var, /*toplev=*/1, at_eof); 13977 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 13978 { 13979 if (CP_DECL_THREAD_LOCAL_P (var)) 13980 tls_aggregates = tree_cons (NULL_TREE, var, 13981 tls_aggregates); 13982 else 13983 static_aggregates = tree_cons (NULL_TREE, var, 13984 static_aggregates); 13985 } 13986 else 13987 /* Check whether the dtor is callable. */ 13988 cxx_maybe_build_cleanup (var, tf_warning_or_error); 13989 } 13990 /* Avoid -Wunused-variable warning (c++/38958). */ 13991 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) 13992 && VAR_P (decl)) 13993 TREE_USED (decl) = DECL_READ_P (decl) = true; 13994 13995 *initp = init; 13996 return var; 13997 } 13998 13999 /* Convert EXPR to the indicated reference TYPE, in a way suitable for 14000 initializing a variable of that TYPE. */ 14001 14002 tree 14003 initialize_reference (tree type, tree expr, 14004 int flags, tsubst_flags_t complain) 14005 { 14006 conversion *conv; 14007 location_t loc = cp_expr_loc_or_input_loc (expr); 14008 14009 if (type == error_mark_node || error_operand_p (expr)) 14010 return error_mark_node; 14011 14012 conversion_obstack_sentinel cos; 14013 14014 conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false, 14015 flags, complain); 14016 /* If this conversion failed, we're in C++20, and we have something like 14017 A& a(b) where A is an aggregate, try again, this time as A& a{b}. */ 14018 if ((!conv || conv->bad_p) 14019 && (flags & LOOKUP_AGGREGATE_PAREN_INIT)) 14020 { 14021 tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr); 14022 CONSTRUCTOR_IS_DIRECT_INIT (e) = true; 14023 CONSTRUCTOR_IS_PAREN_INIT (e) = true; 14024 conversion *c = reference_binding (type, TREE_TYPE (e), e, 14025 /*c_cast_p=*/false, flags, complain); 14026 /* If this worked, use it. */ 14027 if (c && !c->bad_p) 14028 expr = e, conv = c; 14029 } 14030 if (!conv || conv->bad_p) 14031 { 14032 if (complain & tf_error) 14033 { 14034 if (conv) 14035 convert_like (conv, expr, complain); 14036 else if (!CP_TYPE_CONST_P (TREE_TYPE (type)) 14037 && !TYPE_REF_IS_RVALUE (type) 14038 && !lvalue_p (expr)) 14039 error_at (loc, "invalid initialization of non-const reference of " 14040 "type %qH from an rvalue of type %qI", 14041 type, TREE_TYPE (expr)); 14042 else 14043 error_at (loc, "invalid initialization of reference of type " 14044 "%qH from expression of type %qI", type, 14045 TREE_TYPE (expr)); 14046 } 14047 return error_mark_node; 14048 } 14049 14050 if (conv->kind == ck_ref_bind) 14051 /* Perform the conversion. */ 14052 expr = convert_like (conv, expr, complain); 14053 else if (conv->kind == ck_ambig) 14054 /* We gave an error in build_user_type_conversion_1. */ 14055 expr = error_mark_node; 14056 else 14057 gcc_unreachable (); 14058 14059 return expr; 14060 } 14061 14062 /* Return true if T is std::pair<const T&, const T&>. */ 14063 14064 static bool 14065 std_pair_ref_ref_p (tree t) 14066 { 14067 /* First, check if we have std::pair. */ 14068 if (!NON_UNION_CLASS_TYPE_P (t) 14069 || !CLASSTYPE_TEMPLATE_INSTANTIATION (t)) 14070 return false; 14071 tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (t)); 14072 if (!decl_in_std_namespace_p (tdecl)) 14073 return false; 14074 tree name = DECL_NAME (tdecl); 14075 if (!name || !id_equal (name, "pair")) 14076 return false; 14077 14078 /* Now see if the template arguments are both const T&. */ 14079 tree args = CLASSTYPE_TI_ARGS (t); 14080 if (TREE_VEC_LENGTH (args) != 2) 14081 return false; 14082 for (int i = 0; i < 2; i++) 14083 if (!TYPE_REF_OBJ_P (TREE_VEC_ELT (args, i)) 14084 || !CP_TYPE_CONST_P (TREE_TYPE (TREE_VEC_ELT (args, i)))) 14085 return false; 14086 14087 return true; 14088 } 14089 14090 /* Return true if a class T has a reference member. */ 14091 14092 static bool 14093 class_has_reference_member_p (tree t) 14094 { 14095 for (tree fields = TYPE_FIELDS (t); 14096 fields; 14097 fields = DECL_CHAIN (fields)) 14098 if (TREE_CODE (fields) == FIELD_DECL 14099 && !DECL_ARTIFICIAL (fields) 14100 && TYPE_REF_P (TREE_TYPE (fields))) 14101 return true; 14102 return false; 14103 } 14104 14105 /* A wrapper for the above suitable as a callback for dfs_walk_once. */ 14106 14107 static tree 14108 class_has_reference_member_p_r (tree binfo, void *) 14109 { 14110 return (class_has_reference_member_p (BINFO_TYPE (binfo)) 14111 ? integer_one_node : NULL_TREE); 14112 } 14113 14114 14115 /* Return true if T (either a class or a function) has been marked as 14116 not-dangling. */ 14117 14118 static bool 14119 no_dangling_p (tree t) 14120 { 14121 t = lookup_attribute ("no_dangling", TYPE_ATTRIBUTES (t)); 14122 if (!t) 14123 return false; 14124 14125 t = TREE_VALUE (t); 14126 if (!t) 14127 return true; 14128 14129 t = build_converted_constant_bool_expr (TREE_VALUE (t), tf_warning_or_error); 14130 t = cxx_constant_value (t); 14131 return t == boolean_true_node; 14132 } 14133 14134 /* Return true if a class CTYPE is either std::reference_wrapper or 14135 std::ref_view, or a reference wrapper class. We consider a class 14136 a reference wrapper class if it has a reference member. We no 14137 longer check that it has a constructor taking the same reference type 14138 since that approach still generated too many false positives. */ 14139 14140 static bool 14141 reference_like_class_p (tree ctype) 14142 { 14143 if (!CLASS_TYPE_P (ctype)) 14144 return false; 14145 14146 if (no_dangling_p (ctype)) 14147 return true; 14148 14149 /* Also accept a std::pair<const T&, const T&>. */ 14150 if (std_pair_ref_ref_p (ctype)) 14151 return true; 14152 14153 tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (ctype)); 14154 if (decl_in_std_namespace_p (tdecl)) 14155 { 14156 tree name = DECL_NAME (tdecl); 14157 if (name 14158 && (id_equal (name, "reference_wrapper") 14159 || id_equal (name, "span") 14160 || id_equal (name, "ref_view"))) 14161 return true; 14162 } 14163 14164 /* Avoid warning if CTYPE looks like std::span: it has a T* member and 14165 a trivial destructor. For example, 14166 14167 template<typename T> 14168 struct Span { 14169 T* data_; 14170 std::size len_; 14171 }; 14172 14173 is considered std::span-like. */ 14174 if (NON_UNION_CLASS_TYPE_P (ctype) && TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype)) 14175 for (tree field = next_aggregate_field (TYPE_FIELDS (ctype)); 14176 field; field = next_aggregate_field (DECL_CHAIN (field))) 14177 if (TYPE_PTR_P (TREE_TYPE (field))) 14178 return true; 14179 14180 /* Some classes, such as std::tuple, have the reference member in its 14181 (non-direct) base class. */ 14182 if (dfs_walk_once (TYPE_BINFO (ctype), class_has_reference_member_p_r, 14183 nullptr, nullptr)) 14184 return true; 14185 14186 return false; 14187 } 14188 14189 /* Helper for maybe_warn_dangling_reference to find a problematic CALL_EXPR 14190 that initializes the LHS (and at least one of its arguments represents 14191 a temporary, as outlined in maybe_warn_dangling_reference), or NULL_TREE 14192 if none found. For instance: 14193 14194 const S& s = S().self(); // S::self (&TARGET_EXPR <...>) 14195 const int& r = (42, f(1)); // f(1) 14196 const int& t = b ? f(1) : f(2); // f(1) 14197 const int& u = b ? f(1) : f(g); // f(1) 14198 const int& v = b ? f(g) : f(2); // f(2) 14199 const int& w = b ? f(g) : f(g); // NULL_TREE 14200 const int& y = (f(1), 42); // NULL_TREE 14201 const int& z = f(f(1)); // f(f(1)) 14202 14203 EXPR is the initializer. If ARG_P is true, we're processing an argument 14204 to a function; the point is to distinguish between, for example, 14205 14206 Ref::inner (&TARGET_EXPR <D.2839, F::foo (fm)>) 14207 14208 where we shouldn't warn, and 14209 14210 Ref::inner (&TARGET_EXPR <D.2908, F::foo (&TARGET_EXPR <...>)>) 14211 14212 where we should warn (Ref is a reference_like_class_p so we see through 14213 it. */ 14214 14215 static tree 14216 do_warn_dangling_reference (tree expr, bool arg_p) 14217 { 14218 STRIP_NOPS (expr); 14219 14220 if (arg_p && expr_represents_temporary_p (expr)) 14221 { 14222 /* An attempt to reduce the number of -Wdangling-reference 14223 false positives concerning reference wrappers (c++/107532). 14224 When we encounter a reference_like_class_p, we don't warn 14225 just yet; instead, we keep recursing to see if there were 14226 any temporaries behind the reference-wrapper class. */ 14227 tree e = expr; 14228 while (handled_component_p (e)) 14229 e = TREE_OPERAND (e, 0); 14230 tree type = TREE_TYPE (e); 14231 /* If the temporary represents a lambda, we don't really know 14232 what's going on here. */ 14233 if (!reference_like_class_p (type) && !LAMBDA_TYPE_P (type)) 14234 return expr; 14235 } 14236 14237 switch (TREE_CODE (expr)) 14238 { 14239 case CALL_EXPR: 14240 { 14241 tree fndecl = cp_get_callee_fndecl_nofold (expr); 14242 if (!fndecl 14243 || warning_suppressed_p (fndecl, OPT_Wdangling_reference) 14244 || !warning_enabled_at (DECL_SOURCE_LOCATION (fndecl), 14245 OPT_Wdangling_reference) 14246 /* Don't emit a false positive for: 14247 std::vector<int> v = ...; 14248 std::vector<int>::const_iterator it = v.begin(); 14249 const int &r = *it++; 14250 because R refers to one of the int elements of V, not to 14251 a temporary object. Member operator* may return a reference 14252 but probably not to one of its arguments. */ 14253 || (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl) 14254 && DECL_OVERLOADED_OPERATOR_P (fndecl) 14255 && DECL_OVERLOADED_OPERATOR_IS (fndecl, INDIRECT_REF)) 14256 || no_dangling_p (TREE_TYPE (fndecl))) 14257 return NULL_TREE; 14258 14259 tree rettype = TREE_TYPE (TREE_TYPE (fndecl)); 14260 /* If the function doesn't return a reference, don't warn. This 14261 can be e.g. 14262 const int& z = std::min({1, 2, 3, 4, 5, 6, 7}); 14263 which doesn't dangle: std::min here returns an int. 14264 14265 If the function returns a std::pair<const T&, const T&>, we 14266 warn, to detect e.g. 14267 std::pair<const int&, const int&> v = std::minmax(1, 2); 14268 which also creates a dangling reference, because std::minmax 14269 returns std::pair<const T&, const T&>(b, a). */ 14270 if (!(TYPE_REF_OBJ_P (rettype) || reference_like_class_p (rettype))) 14271 return NULL_TREE; 14272 14273 /* Here we're looking to see if any of the arguments is a temporary 14274 initializing a reference parameter. */ 14275 for (int i = 0; i < call_expr_nargs (expr); ++i) 14276 { 14277 tree arg = CALL_EXPR_ARG (expr, i); 14278 /* Check that this argument initializes a reference, except for 14279 the argument initializing the object of a member function. */ 14280 if (!DECL_IOBJ_MEMBER_FUNCTION_P (fndecl) 14281 && !TYPE_REF_P (TREE_TYPE (arg))) 14282 continue; 14283 STRIP_NOPS (arg); 14284 if (TREE_CODE (arg) == ADDR_EXPR) 14285 arg = TREE_OPERAND (arg, 0); 14286 /* Recurse to see if the argument is a temporary. It could also 14287 be another call taking a temporary and returning it and 14288 initializing this reference parameter. */ 14289 if ((arg = do_warn_dangling_reference (arg, /*arg_p=*/true))) 14290 { 14291 /* If we know the temporary could not bind to the return type, 14292 don't warn. This is for scalars and empty classes only 14293 because for other classes we can't be sure we are not 14294 returning its sub-object. */ 14295 if ((SCALAR_TYPE_P (TREE_TYPE (arg)) 14296 || is_empty_class (TREE_TYPE (arg))) 14297 && TYPE_REF_P (rettype) 14298 && !reference_related_p (TREE_TYPE (rettype), 14299 TREE_TYPE (arg))) 14300 continue; 14301 return expr; 14302 } 14303 /* Don't warn about member functions like: 14304 std::any a(...); 14305 S& s = a.emplace<S>({0}, 0); 14306 which construct a new object and return a reference to it, but 14307 we still want to detect: 14308 struct S { const S& self () { return *this; } }; 14309 const S& s = S().self(); 14310 where 's' dangles. If we've gotten here, the object this function 14311 is invoked on is not a temporary. */ 14312 if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)) 14313 break; 14314 } 14315 return NULL_TREE; 14316 } 14317 case COMPOUND_EXPR: 14318 return do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p); 14319 case COND_EXPR: 14320 if (tree t = do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p)) 14321 return t; 14322 return do_warn_dangling_reference (TREE_OPERAND (expr, 2), arg_p); 14323 case PAREN_EXPR: 14324 return do_warn_dangling_reference (TREE_OPERAND (expr, 0), arg_p); 14325 case TARGET_EXPR: 14326 return do_warn_dangling_reference (TARGET_EXPR_INITIAL (expr), arg_p); 14327 default: 14328 return NULL_TREE; 14329 } 14330 } 14331 14332 /* Implement -Wdangling-reference, to detect cases like 14333 14334 int n = 1; 14335 const int& r = std::max(n - 1, n + 1); // r is dangling 14336 14337 This creates temporaries from the arguments, returns a reference to 14338 one of the temporaries, but both temporaries are destroyed at the end 14339 of the full expression. 14340 14341 This works by checking if a reference is initialized with a function 14342 that returns a reference, and at least one parameter of the function 14343 is a reference that is bound to a temporary. It assumes that such a 14344 function actually returns one of its arguments. 14345 14346 DECL is the reference being initialized, INIT is the initializer. */ 14347 14348 static void 14349 maybe_warn_dangling_reference (const_tree decl, tree init) 14350 { 14351 if (!warn_dangling_reference) 14352 return; 14353 tree type = TREE_TYPE (decl); 14354 /* Only warn if what we're initializing has type T&& or const T&, or 14355 std::pair<const T&, const T&>. (A non-const lvalue reference can't 14356 bind to a temporary.) */ 14357 if (!((TYPE_REF_OBJ_P (type) 14358 && (TYPE_REF_IS_RVALUE (type) 14359 || CP_TYPE_CONST_P (TREE_TYPE (type)))) 14360 || std_pair_ref_ref_p (type))) 14361 return; 14362 /* Don't suppress the diagnostic just because the call comes from 14363 a system header. If the DECL is not in a system header, or if 14364 -Wsystem-headers was provided, warn. */ 14365 auto wsh 14366 = make_temp_override (global_dc->m_warn_system_headers, 14367 (!in_system_header_at (DECL_SOURCE_LOCATION (decl)) 14368 || global_dc->m_warn_system_headers)); 14369 if (tree call = do_warn_dangling_reference (init, /*arg_p=*/false)) 14370 { 14371 auto_diagnostic_group d; 14372 if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wdangling_reference, 14373 "possibly dangling reference to a temporary")) 14374 inform (EXPR_LOCATION (call), "the temporary was destroyed at " 14375 "the end of the full expression %qE", call); 14376 } 14377 } 14378 14379 /* If *P is an xvalue expression, prevent temporary lifetime extension if it 14380 gets used to initialize a reference. */ 14381 14382 static tree 14383 prevent_lifetime_extension (tree t) 14384 { 14385 tree *p = &t; 14386 while (TREE_CODE (*p) == COMPOUND_EXPR) 14387 p = &TREE_OPERAND (*p, 1); 14388 while (handled_component_p (*p)) 14389 p = &TREE_OPERAND (*p, 0); 14390 /* Change a TARGET_EXPR from prvalue to xvalue. */ 14391 if (TREE_CODE (*p) == TARGET_EXPR) 14392 *p = build2 (COMPOUND_EXPR, TREE_TYPE (*p), *p, 14393 move (TARGET_EXPR_SLOT (*p))); 14394 return t; 14395 } 14396 14397 /* Subroutine of extend_ref_init_temps. Possibly extend one initializer, 14398 which is bound either to a reference or a std::initializer_list. */ 14399 14400 static tree 14401 extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups, 14402 tree *cond_guard) 14403 { 14404 /* CWG1299 (C++20): The temporary object to which the reference is bound or 14405 the temporary object that is the complete object of a subobject to which 14406 the reference is bound persists for the lifetime of the reference if the 14407 glvalue to which the reference is bound was obtained through one of the 14408 following: 14409 - a temporary materialization conversion ([conv.rval]), 14410 - ( expression ), where expression is one of these expressions, 14411 - subscripting ([expr.sub]) of an array operand, where that operand is one 14412 of these expressions, 14413 - a class member access ([expr.ref]) using the . operator where the left 14414 operand is one of these expressions and the right operand designates a 14415 non-static data member of non-reference type, 14416 - a pointer-to-member operation ([expr.mptr.oper]) using the .* operator 14417 where the left operand is one of these expressions and the right operand 14418 is a pointer to data member of non-reference type, 14419 - a const_cast ([expr.const.cast]), static_cast ([expr.static.cast]), 14420 dynamic_cast ([expr.dynamic.cast]), or reinterpret_cast 14421 ([expr.reinterpret.cast]) converting, without a user-defined conversion, 14422 a glvalue operand that is one of these expressions to a glvalue that 14423 refers to the object designated by the operand, or to its complete 14424 object or a subobject thereof, 14425 - a conditional expression ([expr.cond]) that is a glvalue where the 14426 second or third operand is one of these expressions, or 14427 - a comma expression ([expr.comma]) that is a glvalue where the right 14428 operand is one of these expressions. */ 14429 14430 /* FIXME several cases are still handled wrong (101572, 81420). */ 14431 14432 tree sub = init; 14433 tree *p; 14434 STRIP_NOPS (sub); 14435 if (TREE_CODE (sub) == COMPOUND_EXPR) 14436 { 14437 TREE_OPERAND (sub, 1) 14438 = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups, 14439 cond_guard); 14440 return init; 14441 } 14442 if (TREE_CODE (sub) == POINTER_PLUS_EXPR 14443 && TYPE_PTRDATAMEM_P (TREE_TYPE (tree_strip_nop_conversions 14444 (TREE_OPERAND (sub, 1))))) 14445 { 14446 /* A pointer-to-member operation. */ 14447 TREE_OPERAND (sub, 0) 14448 = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 0), cleanups, 14449 cond_guard); 14450 return init; 14451 } 14452 if (TREE_CODE (sub) == COND_EXPR) 14453 { 14454 tree cur_cond_guard = NULL_TREE; 14455 if (TREE_OPERAND (sub, 1)) 14456 TREE_OPERAND (sub, 1) 14457 = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups, 14458 &cur_cond_guard); 14459 if (cur_cond_guard) 14460 { 14461 tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard, 14462 NOP_EXPR, boolean_true_node, 14463 tf_warning_or_error); 14464 TREE_OPERAND (sub, 1) 14465 = cp_build_compound_expr (set, TREE_OPERAND (sub, 1), 14466 tf_warning_or_error); 14467 } 14468 cur_cond_guard = NULL_TREE; 14469 if (TREE_OPERAND (sub, 2)) 14470 TREE_OPERAND (sub, 2) 14471 = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 2), cleanups, 14472 &cur_cond_guard); 14473 if (cur_cond_guard) 14474 { 14475 tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard, 14476 NOP_EXPR, boolean_true_node, 14477 tf_warning_or_error); 14478 TREE_OPERAND (sub, 2) 14479 = cp_build_compound_expr (set, TREE_OPERAND (sub, 2), 14480 tf_warning_or_error); 14481 } 14482 return init; 14483 } 14484 if (TREE_CODE (sub) != ADDR_EXPR) 14485 return init; 14486 /* Deal with binding to a subobject. */ 14487 for (p = &TREE_OPERAND (sub, 0); 14488 TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; ) 14489 p = &TREE_OPERAND (*p, 0); 14490 if (TREE_CODE (*p) == TARGET_EXPR) 14491 { 14492 tree subinit = NULL_TREE; 14493 *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit, cond_guard); 14494 recompute_tree_invariant_for_addr_expr (sub); 14495 if (init != sub) 14496 init = fold_convert (TREE_TYPE (init), sub); 14497 if (subinit) 14498 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init); 14499 } 14500 return init; 14501 } 14502 14503 /* INIT is part of the initializer for DECL. If there are any 14504 reference or initializer lists being initialized, extend their 14505 lifetime to match that of DECL. */ 14506 14507 tree 14508 extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups, 14509 tree *cond_guard) 14510 { 14511 tree type = TREE_TYPE (init); 14512 if (processing_template_decl) 14513 return init; 14514 14515 maybe_warn_dangling_reference (decl, init); 14516 14517 if (TYPE_REF_P (type)) 14518 init = extend_ref_init_temps_1 (decl, init, cleanups, cond_guard); 14519 else 14520 { 14521 tree ctor = init; 14522 if (TREE_CODE (ctor) == TARGET_EXPR) 14523 ctor = TARGET_EXPR_INITIAL (ctor); 14524 if (TREE_CODE (ctor) == CONSTRUCTOR) 14525 { 14526 /* [dcl.init] When initializing an aggregate from a parenthesized list 14527 of values... a temporary object bound to a reference does not have 14528 its lifetime extended. */ 14529 if (CONSTRUCTOR_IS_PAREN_INIT (ctor)) 14530 return init; 14531 14532 if (is_std_init_list (type)) 14533 { 14534 /* The temporary array underlying a std::initializer_list 14535 is handled like a reference temporary. */ 14536 tree array = CONSTRUCTOR_ELT (ctor, 0)->value; 14537 array = extend_ref_init_temps_1 (decl, array, cleanups, 14538 cond_guard); 14539 CONSTRUCTOR_ELT (ctor, 0)->value = array; 14540 } 14541 else 14542 { 14543 unsigned i; 14544 constructor_elt *p; 14545 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor); 14546 FOR_EACH_VEC_SAFE_ELT (elts, i, p) 14547 p->value = extend_ref_init_temps (decl, p->value, cleanups, 14548 cond_guard); 14549 } 14550 recompute_constructor_flags (ctor); 14551 if (decl_maybe_constant_var_p (decl) && TREE_CONSTANT (ctor)) 14552 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true; 14553 } 14554 } 14555 14556 return init; 14557 } 14558 14559 /* Returns true iff an initializer for TYPE could contain temporaries that 14560 need to be extended because they are bound to references or 14561 std::initializer_list. */ 14562 14563 bool 14564 type_has_extended_temps (tree type) 14565 { 14566 type = strip_array_types (type); 14567 if (TYPE_REF_P (type)) 14568 return true; 14569 if (CLASS_TYPE_P (type)) 14570 { 14571 if (is_std_init_list (type)) 14572 return true; 14573 for (tree f = next_aggregate_field (TYPE_FIELDS (type)); 14574 f; f = next_aggregate_field (DECL_CHAIN (f))) 14575 if (type_has_extended_temps (TREE_TYPE (f))) 14576 return true; 14577 } 14578 return false; 14579 } 14580 14581 /* Returns true iff TYPE is some variant of std::initializer_list. */ 14582 14583 bool 14584 is_std_init_list (tree type) 14585 { 14586 if (!TYPE_P (type)) 14587 return false; 14588 if (cxx_dialect == cxx98) 14589 return false; 14590 /* Look through typedefs. */ 14591 type = TYPE_MAIN_VARIANT (type); 14592 return (CLASS_TYPE_P (type) 14593 && CP_TYPE_CONTEXT (type) == std_node 14594 && init_list_identifier == DECL_NAME (TYPE_NAME (type))); 14595 } 14596 14597 /* Returns true iff DECL is a list constructor: i.e. a constructor which 14598 will accept an argument list of a single std::initializer_list<T>. */ 14599 14600 bool 14601 is_list_ctor (tree decl) 14602 { 14603 tree args = FUNCTION_FIRST_USER_PARMTYPE (decl); 14604 tree arg; 14605 14606 if (!args || args == void_list_node) 14607 return false; 14608 14609 arg = non_reference (TREE_VALUE (args)); 14610 if (!is_std_init_list (arg)) 14611 return false; 14612 14613 args = TREE_CHAIN (args); 14614 14615 if (args && args != void_list_node && !TREE_PURPOSE (args)) 14616 /* There are more non-defaulted parms. */ 14617 return false; 14618 14619 return true; 14620 } 14621 14622 /* We know that can_convert_arg_bad already said "no" when trying to convert 14623 FROM to TO with ARG and FLAGS. Try to figure out if it was because 14624 an explicit conversion function was skipped when looking for a way to 14625 perform the conversion. At this point we've already printed an error. */ 14626 14627 void 14628 maybe_show_nonconverting_candidate (tree to, tree from, tree arg, int flags) 14629 { 14630 if (!(flags & LOOKUP_ONLYCONVERTING)) 14631 return; 14632 14633 conversion_obstack_sentinel cos; 14634 conversion *c = implicit_conversion (to, from, arg, /*c_cast_p=*/false, 14635 flags & ~LOOKUP_ONLYCONVERTING, tf_none); 14636 if (c && !c->bad_p && c->user_conv_p) 14637 /* Ay, the conversion would have worked in direct-init context. */ 14638 for (; c; c = next_conversion (c)) 14639 if (c->kind == ck_user 14640 && DECL_P (c->cand->fn) 14641 && DECL_NONCONVERTING_P (c->cand->fn)) 14642 inform (DECL_SOURCE_LOCATION (c->cand->fn), "explicit conversion " 14643 "function was not considered"); 14644 } 14645 14646 /* We're converting EXPR to TYPE. If that conversion involves a conversion 14647 function and we're binding EXPR to a reference parameter of that function, 14648 return true. */ 14649 14650 bool 14651 conv_binds_to_reference_parm_p (tree type, tree expr) 14652 { 14653 conversion_obstack_sentinel cos; 14654 conversion *c = implicit_conversion (type, TREE_TYPE (expr), expr, 14655 /*c_cast_p=*/false, LOOKUP_NORMAL, 14656 tf_none); 14657 if (c && !c->bad_p && c->user_conv_p) 14658 for (; c; c = next_conversion (c)) 14659 if (c->kind == ck_user) 14660 for (z_candidate *cand = c->cand; cand; cand = cand->next) 14661 if (cand->viable == 1) 14662 for (size_t i = 0; i < cand->num_convs; ++i) 14663 if (cand->convs[i]->kind == ck_ref_bind 14664 && conv_get_original_expr (cand->convs[i]) == expr) 14665 return true; 14666 14667 return false; 14668 } 14669 14670 #include "gt-cp-call.h" 14671