| 1 | /* Functions related to invoking -*- C++ -*- methods and overloaded functions. |
| 2 | Copyright (C) 1987-2025 Free Software Foundation, Inc. |
| 3 | Contributed by Michael Tiemann (tiemann@cygnus.com) and |
| 4 | modified by Brendan Kehoe (brendan@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 "c-family/c-type-mismatch.h" |
| 46 | #include "tristate.h" |
| 47 | #include "tree-pretty-print-markup.h" |
| 48 | |
| 49 | /* The various kinds of conversion. */ |
| 50 | |
| 51 | enum conversion_kind { |
| 52 | ck_identity, |
| 53 | ck_lvalue, |
| 54 | ck_fnptr, |
| 55 | ck_qual, |
| 56 | ck_std, |
| 57 | ck_ptr, |
| 58 | ck_pmem, |
| 59 | ck_base, |
| 60 | ck_ref_bind, |
| 61 | ck_user, |
| 62 | ck_ambig, |
| 63 | ck_list, |
| 64 | ck_aggr, |
| 65 | ck_rvalue, |
| 66 | /* When LOOKUP_SHORTCUT_BAD_CONVS is set, we may return a conversion of |
| 67 | this kind whenever we know the true conversion is either bad or outright |
| 68 | invalid, but we don't want to attempt to compute the bad conversion (for |
| 69 | sake of avoiding unnecessary instantiation). bad_p should always be set |
| 70 | for these. */ |
| 71 | ck_deferred_bad, |
| 72 | }; |
| 73 | |
| 74 | /* The rank of the conversion. Order of the enumerals matters; better |
| 75 | conversions should come earlier in the list. */ |
| 76 | |
| 77 | enum conversion_rank { |
| 78 | cr_identity, |
| 79 | cr_exact, |
| 80 | cr_promotion, |
| 81 | cr_std, |
| 82 | cr_pbool, |
| 83 | cr_user, |
| 84 | cr_ellipsis, |
| 85 | cr_bad |
| 86 | }; |
| 87 | |
| 88 | /* An implicit conversion sequence, in the sense of [over.best.ics]. |
| 89 | The first conversion to be performed is at the end of the chain. |
| 90 | That conversion is always a cr_identity conversion. */ |
| 91 | |
| 92 | struct conversion { |
| 93 | /* The kind of conversion represented by this step. */ |
| 94 | conversion_kind kind; |
| 95 | /* The rank of this conversion. */ |
| 96 | conversion_rank rank; |
| 97 | BOOL_BITFIELD user_conv_p : 1; |
| 98 | BOOL_BITFIELD ellipsis_p : 1; |
| 99 | BOOL_BITFIELD this_p : 1; |
| 100 | /* True if this conversion would be permitted with a bending of |
| 101 | language standards, e.g. disregarding pointer qualifiers or |
| 102 | converting integers to pointers. */ |
| 103 | BOOL_BITFIELD bad_p : 1; |
| 104 | /* If KIND is ck_ref_bind or ck_base, true to indicate that a |
| 105 | temporary should be created to hold the result of the |
| 106 | conversion. If KIND is ck_ambig or ck_user, true means force |
| 107 | copy-initialization. */ |
| 108 | BOOL_BITFIELD need_temporary_p : 1; |
| 109 | /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion |
| 110 | from a pointer-to-derived to pointer-to-base is being performed. */ |
| 111 | BOOL_BITFIELD base_p : 1; |
| 112 | /* If KIND is ck_ref_bind, true when either an lvalue reference is |
| 113 | being bound to an lvalue expression or an rvalue reference is |
| 114 | being bound to an rvalue expression. If KIND is ck_rvalue or ck_base, |
| 115 | true when we are treating an lvalue as an rvalue (12.8p33). If |
| 116 | ck_identity, we will be binding a reference directly or decaying to |
| 117 | a pointer. */ |
| 118 | BOOL_BITFIELD rvaluedness_matches_p: 1; |
| 119 | BOOL_BITFIELD check_narrowing: 1; |
| 120 | /* Whether check_narrowing should only check TREE_CONSTANTs; used |
| 121 | in build_converted_constant_expr. */ |
| 122 | BOOL_BITFIELD check_narrowing_const_only: 1; |
| 123 | /* True if this conversion is taking place in a copy-initialization context |
| 124 | and we should only consider converting constructors. Only set in |
| 125 | ck_base and ck_rvalue. */ |
| 126 | BOOL_BITFIELD copy_init_p : 1; |
| 127 | /* The type of the expression resulting from the conversion. */ |
| 128 | tree type; |
| 129 | union { |
| 130 | /* The next conversion in the chain. Since the conversions are |
| 131 | arranged from outermost to innermost, the NEXT conversion will |
| 132 | actually be performed before this conversion. This variant is |
| 133 | used only when KIND is neither ck_identity, ck_aggr, ck_ambig nor |
| 134 | ck_list. Please use the next_conversion function instead |
| 135 | of using this field directly. */ |
| 136 | conversion *next; |
| 137 | /* The expression at the beginning of the conversion chain. This |
| 138 | variant is used only if KIND is ck_identity, ck_aggr, or ck_ambig. |
| 139 | You can use conv_get_original_expr to get this expression. */ |
| 140 | tree expr; |
| 141 | /* The array of conversions for an initializer_list, so this |
| 142 | variant is used only when KIN D is ck_list. */ |
| 143 | conversion **list; |
| 144 | } u; |
| 145 | /* The function candidate corresponding to this conversion |
| 146 | sequence. This field is only used if KIND is ck_user. */ |
| 147 | struct z_candidate *cand; |
| 148 | }; |
| 149 | |
| 150 | #define CONVERSION_RANK(NODE) \ |
| 151 | ((NODE)->bad_p ? cr_bad \ |
| 152 | : (NODE)->ellipsis_p ? cr_ellipsis \ |
| 153 | : (NODE)->user_conv_p ? cr_user \ |
| 154 | : (NODE)->rank) |
| 155 | |
| 156 | #define BAD_CONVERSION_RANK(NODE) \ |
| 157 | ((NODE)->ellipsis_p ? cr_ellipsis \ |
| 158 | : (NODE)->user_conv_p ? cr_user \ |
| 159 | : (NODE)->rank) |
| 160 | |
| 161 | static struct obstack conversion_obstack; |
| 162 | static bool conversion_obstack_initialized; |
| 163 | struct rejection_reason; |
| 164 | |
| 165 | static struct z_candidate * tourney (struct z_candidate *, tsubst_flags_t); |
| 166 | static int equal_functions (tree, tree); |
| 167 | static int joust (struct z_candidate *, struct z_candidate *, bool, |
| 168 | tsubst_flags_t); |
| 169 | static int compare_ics (conversion *, conversion *); |
| 170 | static void maybe_warn_class_memaccess (location_t, tree, |
| 171 | const vec<tree, va_gc> *); |
| 172 | static tree build_over_call (struct z_candidate *, int, tsubst_flags_t); |
| 173 | static tree convert_like (conversion *, tree, tsubst_flags_t); |
| 174 | static tree convert_like_with_context (conversion *, tree, tree, int, |
| 175 | tsubst_flags_t); |
| 176 | static void op_error (const op_location_t &, enum tree_code, enum tree_code, |
| 177 | tree, tree, tree, bool); |
| 178 | static struct z_candidate *build_user_type_conversion_1 (tree, tree, int, |
| 179 | tsubst_flags_t); |
| 180 | static void print_z_candidate (location_t, const char *, struct z_candidate *); |
| 181 | static void print_z_candidates (location_t, struct z_candidate *, |
| 182 | tristate = tristate::unknown ()); |
| 183 | static tree build_this (tree); |
| 184 | static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *); |
| 185 | static bool any_strictly_viable (struct z_candidate *); |
| 186 | static struct z_candidate *add_template_candidate |
| 187 | (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *, |
| 188 | tree, tree, tree, int, unification_kind_t, bool, tsubst_flags_t); |
| 189 | static struct z_candidate *add_template_candidate_real |
| 190 | (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *, |
| 191 | tree, tree, tree, int, tree, unification_kind_t, bool, tsubst_flags_t); |
| 192 | static bool is_complete (tree); |
| 193 | static struct z_candidate *add_conv_candidate |
| 194 | (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, tree, |
| 195 | tree, tsubst_flags_t); |
| 196 | static struct z_candidate *add_function_candidate |
| 197 | (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *, tree, |
| 198 | tree, int, conversion**, bool, tsubst_flags_t); |
| 199 | static conversion *implicit_conversion (tree, tree, tree, bool, int, |
| 200 | tsubst_flags_t); |
| 201 | static conversion *reference_binding (tree, tree, tree, bool, int, |
| 202 | tsubst_flags_t); |
| 203 | static conversion *build_conv (conversion_kind, tree, conversion *); |
| 204 | static conversion *build_list_conv (tree, tree, int, tsubst_flags_t); |
| 205 | static conversion *next_conversion (conversion *); |
| 206 | static bool is_subseq (conversion *, conversion *); |
| 207 | static conversion *maybe_handle_ref_bind (conversion **); |
| 208 | static void maybe_handle_implicit_object (conversion **); |
| 209 | static struct z_candidate *add_candidate |
| 210 | (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, size_t, |
| 211 | conversion **, tree, tree, int, struct rejection_reason *, int); |
| 212 | static tree source_type (conversion *); |
| 213 | static void add_warning (struct z_candidate *, struct z_candidate *); |
| 214 | static conversion *direct_reference_binding (tree, conversion *); |
| 215 | static bool promoted_arithmetic_type_p (tree); |
| 216 | static conversion *conditional_conversion (tree, tree, tsubst_flags_t); |
| 217 | static char *name_as_c_string (tree, tree, bool *); |
| 218 | static tree prep_operand (tree); |
| 219 | static void add_candidates (tree, tree, const vec<tree, va_gc> *, tree, tree, |
| 220 | bool, tree, tree, int, struct z_candidate **, |
| 221 | tsubst_flags_t); |
| 222 | static conversion *merge_conversion_sequences (conversion *, conversion *); |
| 223 | static tree build_temp (tree, tree, int, diagnostic_t *, tsubst_flags_t); |
| 224 | static conversion *build_identity_conv (tree, tree); |
| 225 | static inline bool conv_binds_to_array_of_unknown_bound (conversion *); |
| 226 | static bool conv_is_prvalue (conversion *); |
| 227 | static tree prevent_lifetime_extension (tree); |
| 228 | |
| 229 | /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE. |
| 230 | NAME can take many forms... */ |
| 231 | |
| 232 | bool |
| 233 | check_dtor_name (tree basetype, tree name) |
| 234 | { |
| 235 | /* Just accept something we've already complained about. */ |
| 236 | if (name == error_mark_node) |
| 237 | return true; |
| 238 | |
| 239 | if (TREE_CODE (name) == TYPE_DECL) |
| 240 | name = TREE_TYPE (name); |
| 241 | else if (TYPE_P (name)) |
| 242 | /* OK */; |
| 243 | else if (identifier_p (t: name)) |
| 244 | { |
| 245 | if ((MAYBE_CLASS_TYPE_P (basetype) |
| 246 | || TREE_CODE (basetype) == ENUMERAL_TYPE) |
| 247 | && name == constructor_name (basetype)) |
| 248 | return true; |
| 249 | |
| 250 | /* Otherwise lookup the name, it could be an unrelated typedef |
| 251 | of the correct type. */ |
| 252 | name = lookup_name (name, want: LOOK_want::TYPE); |
| 253 | if (!name) |
| 254 | return false; |
| 255 | name = TREE_TYPE (name); |
| 256 | if (name == error_mark_node) |
| 257 | return false; |
| 258 | } |
| 259 | else |
| 260 | { |
| 261 | /* In the case of: |
| 262 | |
| 263 | template <class T> struct S { ~S(); }; |
| 264 | int i; |
| 265 | i.~S(); |
| 266 | |
| 267 | NAME will be a class template. */ |
| 268 | gcc_assert (DECL_CLASS_TEMPLATE_P (name)); |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name)); |
| 273 | } |
| 274 | |
| 275 | /* We want the address of a function or method. We avoid creating a |
| 276 | pointer-to-member function. */ |
| 277 | |
| 278 | tree |
| 279 | build_addr_func (tree function, tsubst_flags_t complain) |
| 280 | { |
| 281 | tree type = TREE_TYPE (function); |
| 282 | |
| 283 | /* We have to do these by hand to avoid real pointer to member |
| 284 | functions. */ |
| 285 | if (TREE_CODE (type) == METHOD_TYPE) |
| 286 | { |
| 287 | if (TREE_CODE (function) == OFFSET_REF) |
| 288 | { |
| 289 | tree object = build_address (TREE_OPERAND (function, 0)); |
| 290 | return get_member_function_from_ptrfunc (&object, |
| 291 | TREE_OPERAND (function, 1), |
| 292 | complain); |
| 293 | } |
| 294 | function = build_address (function); |
| 295 | } |
| 296 | else if (TREE_CODE (function) == FUNCTION_DECL |
| 297 | && DECL_IMMEDIATE_FUNCTION_P (function)) |
| 298 | function = build_address (function); |
| 299 | else |
| 300 | function = decay_conversion (function, complain, /*reject_builtin=*/false); |
| 301 | |
| 302 | return function; |
| 303 | } |
| 304 | |
| 305 | /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or |
| 306 | POINTER_TYPE to those. Note, pointer to member function types |
| 307 | (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are |
| 308 | two variants. build_call_a is the primitive taking an array of |
| 309 | arguments, while build_call_n is a wrapper that handles varargs. */ |
| 310 | |
| 311 | tree |
| 312 | build_call_n (tree function, int n, ...) |
| 313 | { |
| 314 | if (n == 0) |
| 315 | return build_call_a (function, 0, NULL); |
| 316 | else |
| 317 | { |
| 318 | tree *argarray = XALLOCAVEC (tree, n); |
| 319 | va_list ap; |
| 320 | int i; |
| 321 | |
| 322 | va_start (ap, n); |
| 323 | for (i = 0; i < n; i++) |
| 324 | argarray[i] = va_arg (ap, tree); |
| 325 | va_end (ap); |
| 326 | return build_call_a (function, n, argarray); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | /* Update various flags in cfun and the call itself based on what is being |
| 331 | called. Split out of build_call_a so that bot_manip can use it too. */ |
| 332 | |
| 333 | void |
| 334 | set_flags_from_callee (tree call) |
| 335 | { |
| 336 | /* Handle both CALL_EXPRs and AGGR_INIT_EXPRs. */ |
| 337 | tree decl = cp_get_callee_fndecl_nofold (call); |
| 338 | |
| 339 | /* We check both the decl and the type; a function may be known not to |
| 340 | throw without being declared throw(). */ |
| 341 | bool nothrow = decl && TREE_NOTHROW (decl); |
| 342 | tree callee = cp_get_callee (call); |
| 343 | if (callee) |
| 344 | nothrow |= TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (callee))); |
| 345 | else if (TREE_CODE (call) == CALL_EXPR |
| 346 | && internal_fn_flags (CALL_EXPR_IFN (call)) & ECF_NOTHROW) |
| 347 | nothrow = true; |
| 348 | |
| 349 | if (cfun && cp_function_chain && !cp_unevaluated_operand) |
| 350 | { |
| 351 | if (!nothrow && at_function_scope_p ()) |
| 352 | cp_function_chain->can_throw = 1; |
| 353 | |
| 354 | if (decl && TREE_THIS_VOLATILE (decl)) |
| 355 | current_function_returns_abnormally = 1; |
| 356 | } |
| 357 | |
| 358 | TREE_NOTHROW (call) = nothrow; |
| 359 | } |
| 360 | |
| 361 | tree |
| 362 | build_call_a (tree function, int n, tree *argarray) |
| 363 | { |
| 364 | tree decl; |
| 365 | tree result_type; |
| 366 | tree fntype; |
| 367 | int i; |
| 368 | |
| 369 | function = build_addr_func (function, complain: tf_warning_or_error); |
| 370 | |
| 371 | gcc_assert (TYPE_PTR_P (TREE_TYPE (function))); |
| 372 | fntype = TREE_TYPE (TREE_TYPE (function)); |
| 373 | gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype)); |
| 374 | result_type = TREE_TYPE (fntype); |
| 375 | /* An rvalue has no cv-qualifiers. */ |
| 376 | if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type)) |
| 377 | result_type = cv_unqualified (result_type); |
| 378 | |
| 379 | function = build_call_array_loc (input_location, |
| 380 | result_type, function, n, argarray); |
| 381 | set_flags_from_callee (function); |
| 382 | |
| 383 | decl = get_callee_fndecl (function); |
| 384 | |
| 385 | if (decl && !TREE_USED (decl)) |
| 386 | { |
| 387 | /* We invoke build_call directly for several library |
| 388 | functions. These may have been declared normally if |
| 389 | we're building libgcc, so we can't just check |
| 390 | DECL_ARTIFICIAL. */ |
| 391 | gcc_assert (DECL_ARTIFICIAL (decl) |
| 392 | || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), |
| 393 | "__" , 2)); |
| 394 | mark_used (decl); |
| 395 | } |
| 396 | |
| 397 | require_complete_eh_spec_types (fntype, decl); |
| 398 | |
| 399 | TREE_HAS_CONSTRUCTOR (function) = (decl && DECL_CONSTRUCTOR_P (decl)); |
| 400 | |
| 401 | /* Don't pass empty class objects by value. This is useful |
| 402 | for tags in STL, which are used to control overload resolution. |
| 403 | We don't need to handle other cases of copying empty classes. */ |
| 404 | if (!decl || !fndecl_built_in_p (node: decl)) |
| 405 | for (i = 0; i < n; i++) |
| 406 | { |
| 407 | tree arg = CALL_EXPR_ARG (function, i); |
| 408 | if (is_empty_class (TREE_TYPE (arg)) |
| 409 | && simple_empty_class_p (TREE_TYPE (arg), arg, INIT_EXPR)) |
| 410 | { |
| 411 | while (TREE_CODE (arg) == TARGET_EXPR) |
| 412 | /* We're disconnecting the initializer from its target, |
| 413 | don't create a temporary. */ |
| 414 | arg = TARGET_EXPR_INITIAL (arg); |
| 415 | tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg)); |
| 416 | arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t); |
| 417 | CALL_EXPR_ARG (function, i) = arg; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | return function; |
| 422 | } |
| 423 | |
| 424 | /* New overloading code. */ |
| 425 | |
| 426 | struct z_candidate; |
| 427 | |
| 428 | struct candidate_warning { |
| 429 | z_candidate *loser; |
| 430 | candidate_warning *next; |
| 431 | }; |
| 432 | |
| 433 | /* Information for providing diagnostics about why overloading failed. */ |
| 434 | |
| 435 | enum rejection_reason_code { |
| 436 | rr_none, |
| 437 | rr_arity, |
| 438 | rr_explicit_conversion, |
| 439 | rr_template_conversion, |
| 440 | rr_arg_conversion, |
| 441 | rr_bad_arg_conversion, |
| 442 | rr_template_unification, |
| 443 | rr_invalid_copy, |
| 444 | rr_inherited_ctor, |
| 445 | rr_constraint_failure, |
| 446 | rr_ignored, |
| 447 | }; |
| 448 | |
| 449 | struct conversion_info { |
| 450 | /* The index of the argument, 0-based. */ |
| 451 | int n_arg; |
| 452 | /* The actual argument or its type. */ |
| 453 | tree from; |
| 454 | /* The type of the parameter. */ |
| 455 | tree to_type; |
| 456 | /* The location of the argument. */ |
| 457 | location_t loc; |
| 458 | }; |
| 459 | |
| 460 | struct rejection_reason { |
| 461 | enum rejection_reason_code code; |
| 462 | union { |
| 463 | /* Information about an arity mismatch. */ |
| 464 | struct { |
| 465 | /* The expected number of arguments. */ |
| 466 | int expected; |
| 467 | /* The actual number of arguments in the call. */ |
| 468 | int actual; |
| 469 | /* Whether EXPECTED should be treated as a lower bound. */ |
| 470 | bool least_p; |
| 471 | } arity; |
| 472 | /* Information about an argument conversion mismatch. */ |
| 473 | struct conversion_info conversion; |
| 474 | /* Same, but for bad argument conversions. */ |
| 475 | struct conversion_info bad_conversion; |
| 476 | /* Information about template unification failures. These are the |
| 477 | parameters passed to fn_type_unification. */ |
| 478 | struct { |
| 479 | tree tmpl; |
| 480 | tree explicit_targs; |
| 481 | int num_targs; |
| 482 | const tree *args; |
| 483 | unsigned int nargs; |
| 484 | tree return_type; |
| 485 | unification_kind_t strict; |
| 486 | int flags; |
| 487 | } template_unification; |
| 488 | /* Information about template instantiation failures. These are the |
| 489 | parameters passed to instantiate_template. */ |
| 490 | struct { |
| 491 | tree tmpl; |
| 492 | tree targs; |
| 493 | } template_instantiation; |
| 494 | } u; |
| 495 | }; |
| 496 | |
| 497 | struct z_candidate { |
| 498 | /* The FUNCTION_DECL that will be called if this candidate is |
| 499 | selected by overload resolution. */ |
| 500 | tree fn; |
| 501 | /* If not NULL_TREE, the first argument to use when calling this |
| 502 | function. */ |
| 503 | tree first_arg; |
| 504 | /* The rest of the arguments to use when calling this function. If |
| 505 | there are no further arguments this may be NULL or it may be an |
| 506 | empty vector. */ |
| 507 | const vec<tree, va_gc> *args; |
| 508 | /* The implicit conversion sequences for each of the arguments to |
| 509 | FN. */ |
| 510 | conversion **convs; |
| 511 | /* The number of implicit conversion sequences. */ |
| 512 | size_t num_convs; |
| 513 | /* If FN is a user-defined conversion, the standard conversion |
| 514 | sequence from the type returned by FN to the desired destination |
| 515 | type. */ |
| 516 | conversion *second_conv; |
| 517 | struct rejection_reason *reason; |
| 518 | /* If FN is a member function, the binfo indicating the path used to |
| 519 | qualify the name of FN at the call site. This path is used to |
| 520 | determine whether or not FN is accessible if it is selected by |
| 521 | overload resolution. The DECL_CONTEXT of FN will always be a |
| 522 | (possibly improper) base of this binfo. */ |
| 523 | tree access_path; |
| 524 | /* If FN is a non-static member function, the binfo indicating the |
| 525 | subobject to which the `this' pointer should be converted if FN |
| 526 | is selected by overload resolution. The type pointed to by |
| 527 | the `this' pointer must correspond to the most derived class |
| 528 | indicated by the CONVERSION_PATH. */ |
| 529 | tree conversion_path; |
| 530 | tree template_decl; |
| 531 | tree explicit_targs; |
| 532 | candidate_warning *warnings; |
| 533 | z_candidate *next; |
| 534 | int viable; |
| 535 | |
| 536 | /* The flags active in add_candidate. */ |
| 537 | int flags; |
| 538 | |
| 539 | bool rewritten () const { return (flags & LOOKUP_REWRITTEN); } |
| 540 | bool reversed () const { return (flags & LOOKUP_REVERSED); } |
| 541 | }; |
| 542 | |
| 543 | /* Returns true iff T is a null pointer constant in the sense of |
| 544 | [conv.ptr]. */ |
| 545 | |
| 546 | bool |
| 547 | null_ptr_cst_p (tree t) |
| 548 | { |
| 549 | tree type = TREE_TYPE (t); |
| 550 | |
| 551 | /* [conv.ptr] |
| 552 | |
| 553 | A null pointer constant is an integer literal ([lex.icon]) with value |
| 554 | zero or a prvalue of type std::nullptr_t. */ |
| 555 | if (NULLPTR_TYPE_P (type)) |
| 556 | return true; |
| 557 | |
| 558 | if (cxx_dialect >= cxx11) |
| 559 | { |
| 560 | STRIP_ANY_LOCATION_WRAPPER (t); |
| 561 | |
| 562 | /* Core issue 903 says only literal 0 is a null pointer constant. */ |
| 563 | if (TREE_CODE (t) == INTEGER_CST |
| 564 | && !TREE_OVERFLOW (t) |
| 565 | && TREE_CODE (type) == INTEGER_TYPE |
| 566 | && integer_zerop (t) |
| 567 | && !char_type_p (type)) |
| 568 | return true; |
| 569 | } |
| 570 | else if (CP_INTEGRAL_TYPE_P (type)) |
| 571 | { |
| 572 | t = fold_non_dependent_expr (t, tf_none); |
| 573 | STRIP_NOPS (t); |
| 574 | if (integer_zerop (t) && !TREE_OVERFLOW (t)) |
| 575 | return true; |
| 576 | } |
| 577 | |
| 578 | return false; |
| 579 | } |
| 580 | |
| 581 | /* Returns true iff T is a null member pointer value (4.11). */ |
| 582 | |
| 583 | bool |
| 584 | null_member_pointer_value_p (tree t) |
| 585 | { |
| 586 | tree type = TREE_TYPE (t); |
| 587 | if (!type) |
| 588 | return false; |
| 589 | else if (TYPE_PTRMEMFUNC_P (type)) |
| 590 | return (TREE_CODE (t) == CONSTRUCTOR |
| 591 | && CONSTRUCTOR_NELTS (t) |
| 592 | && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value)); |
| 593 | else if (TYPE_PTRDATAMEM_P (type)) |
| 594 | return integer_all_onesp (t); |
| 595 | else |
| 596 | return false; |
| 597 | } |
| 598 | |
| 599 | /* Returns nonzero if PARMLIST consists of only default parms, |
| 600 | ellipsis, and/or undeduced parameter packs. */ |
| 601 | |
| 602 | bool |
| 603 | sufficient_parms_p (const_tree parmlist) |
| 604 | { |
| 605 | for (; parmlist && parmlist != void_list_node; |
| 606 | parmlist = TREE_CHAIN (parmlist)) |
| 607 | if (!TREE_PURPOSE (parmlist) |
| 608 | && !PACK_EXPANSION_P (TREE_VALUE (parmlist))) |
| 609 | return false; |
| 610 | return true; |
| 611 | } |
| 612 | |
| 613 | /* Allocate N bytes of memory from the conversion obstack. The memory |
| 614 | is zeroed before being returned. */ |
| 615 | |
| 616 | static void * |
| 617 | conversion_obstack_alloc (size_t n) |
| 618 | { |
| 619 | void *p; |
| 620 | if (!conversion_obstack_initialized) |
| 621 | { |
| 622 | gcc_obstack_init (&conversion_obstack); |
| 623 | conversion_obstack_initialized = true; |
| 624 | } |
| 625 | p = obstack_alloc (&conversion_obstack, n); |
| 626 | memset (s: p, c: 0, n: n); |
| 627 | return p; |
| 628 | } |
| 629 | |
| 630 | /* RAII class to discard anything added to conversion_obstack. */ |
| 631 | |
| 632 | struct conversion_obstack_sentinel |
| 633 | { |
| 634 | void *p; |
| 635 | conversion_obstack_sentinel (): p (conversion_obstack_alloc (n: 0)) {} |
| 636 | ~conversion_obstack_sentinel () { obstack_free (&conversion_obstack, p); } |
| 637 | }; |
| 638 | |
| 639 | /* Allocate rejection reasons. */ |
| 640 | |
| 641 | static struct rejection_reason * |
| 642 | alloc_rejection (enum rejection_reason_code code) |
| 643 | { |
| 644 | struct rejection_reason *p; |
| 645 | p = (struct rejection_reason *) conversion_obstack_alloc (n: sizeof *p); |
| 646 | p->code = code; |
| 647 | return p; |
| 648 | } |
| 649 | |
| 650 | static struct rejection_reason * |
| 651 | arity_rejection (tree first_arg, int expected, int actual, bool least_p = false) |
| 652 | { |
| 653 | struct rejection_reason *r = alloc_rejection (code: rr_arity); |
| 654 | int adjust = first_arg != NULL_TREE; |
| 655 | r->u.arity.expected = expected - adjust; |
| 656 | r->u.arity.actual = actual - adjust; |
| 657 | r->u.arity.least_p = least_p; |
| 658 | return r; |
| 659 | } |
| 660 | |
| 661 | static struct rejection_reason * |
| 662 | arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to, |
| 663 | location_t loc) |
| 664 | { |
| 665 | struct rejection_reason *r = alloc_rejection (code: rr_arg_conversion); |
| 666 | int adjust = first_arg != NULL_TREE; |
| 667 | r->u.conversion.n_arg = n_arg - adjust; |
| 668 | r->u.conversion.from = from; |
| 669 | r->u.conversion.to_type = to; |
| 670 | r->u.conversion.loc = loc; |
| 671 | return r; |
| 672 | } |
| 673 | |
| 674 | static struct rejection_reason * |
| 675 | bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to, |
| 676 | location_t loc) |
| 677 | { |
| 678 | struct rejection_reason *r = alloc_rejection (code: rr_bad_arg_conversion); |
| 679 | int adjust = first_arg != NULL_TREE; |
| 680 | r->u.bad_conversion.n_arg = n_arg - adjust; |
| 681 | r->u.bad_conversion.from = from; |
| 682 | r->u.bad_conversion.to_type = to; |
| 683 | r->u.bad_conversion.loc = loc; |
| 684 | return r; |
| 685 | } |
| 686 | |
| 687 | static struct rejection_reason * |
| 688 | explicit_conversion_rejection (tree from, tree to) |
| 689 | { |
| 690 | struct rejection_reason *r = alloc_rejection (code: rr_explicit_conversion); |
| 691 | r->u.conversion.n_arg = 0; |
| 692 | r->u.conversion.from = from; |
| 693 | r->u.conversion.to_type = to; |
| 694 | r->u.conversion.loc = UNKNOWN_LOCATION; |
| 695 | return r; |
| 696 | } |
| 697 | |
| 698 | static struct rejection_reason * |
| 699 | template_conversion_rejection (tree from, tree to) |
| 700 | { |
| 701 | struct rejection_reason *r = alloc_rejection (code: rr_template_conversion); |
| 702 | r->u.conversion.n_arg = 0; |
| 703 | r->u.conversion.from = from; |
| 704 | r->u.conversion.to_type = to; |
| 705 | r->u.conversion.loc = UNKNOWN_LOCATION; |
| 706 | return r; |
| 707 | } |
| 708 | |
| 709 | static struct rejection_reason * |
| 710 | template_unification_rejection (tree tmpl, tree explicit_targs, tree targs, |
| 711 | const tree *args, unsigned int nargs, |
| 712 | tree return_type, unification_kind_t strict, |
| 713 | int flags) |
| 714 | { |
| 715 | size_t args_n_bytes = sizeof (*args) * nargs; |
| 716 | tree *args1 = (tree *) conversion_obstack_alloc (n: args_n_bytes); |
| 717 | struct rejection_reason *r = alloc_rejection (code: rr_template_unification); |
| 718 | r->u.template_unification.tmpl = tmpl; |
| 719 | r->u.template_unification.explicit_targs = explicit_targs; |
| 720 | r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs); |
| 721 | /* Copy args to our own storage. */ |
| 722 | memcpy (dest: args1, src: args, n: args_n_bytes); |
| 723 | r->u.template_unification.args = args1; |
| 724 | r->u.template_unification.nargs = nargs; |
| 725 | r->u.template_unification.return_type = return_type; |
| 726 | r->u.template_unification.strict = strict; |
| 727 | r->u.template_unification.flags = flags; |
| 728 | return r; |
| 729 | } |
| 730 | |
| 731 | static struct rejection_reason * |
| 732 | template_unification_error_rejection (void) |
| 733 | { |
| 734 | return alloc_rejection (code: rr_template_unification); |
| 735 | } |
| 736 | |
| 737 | static struct rejection_reason * |
| 738 | invalid_copy_with_fn_template_rejection (void) |
| 739 | { |
| 740 | struct rejection_reason *r = alloc_rejection (code: rr_invalid_copy); |
| 741 | return r; |
| 742 | } |
| 743 | |
| 744 | static struct rejection_reason * |
| 745 | inherited_ctor_rejection (void) |
| 746 | { |
| 747 | struct rejection_reason *r = alloc_rejection (code: rr_inherited_ctor); |
| 748 | return r; |
| 749 | } |
| 750 | |
| 751 | /* Build a constraint failure record. */ |
| 752 | |
| 753 | static struct rejection_reason * |
| 754 | constraint_failure (void) |
| 755 | { |
| 756 | struct rejection_reason *r = alloc_rejection (code: rr_constraint_failure); |
| 757 | return r; |
| 758 | } |
| 759 | |
| 760 | /* Dynamically allocate a conversion. */ |
| 761 | |
| 762 | static conversion * |
| 763 | alloc_conversion (conversion_kind kind) |
| 764 | { |
| 765 | conversion *c; |
| 766 | c = (conversion *) conversion_obstack_alloc (n: sizeof (conversion)); |
| 767 | c->kind = kind; |
| 768 | return c; |
| 769 | } |
| 770 | |
| 771 | /* Make sure that all memory on the conversion obstack has been |
| 772 | freed. */ |
| 773 | |
| 774 | void |
| 775 | validate_conversion_obstack (void) |
| 776 | { |
| 777 | if (conversion_obstack_initialized) |
| 778 | gcc_assert ((obstack_next_free (&conversion_obstack) |
| 779 | == obstack_base (&conversion_obstack))); |
| 780 | } |
| 781 | |
| 782 | /* Dynamically allocate an array of N conversions. */ |
| 783 | |
| 784 | static conversion ** |
| 785 | alloc_conversions (size_t n) |
| 786 | { |
| 787 | return (conversion **) conversion_obstack_alloc (n: n * sizeof (conversion *)); |
| 788 | } |
| 789 | |
| 790 | /* True iff the active member of conversion::u for code CODE is NEXT. */ |
| 791 | |
| 792 | static inline bool |
| 793 | has_next (conversion_kind code) |
| 794 | { |
| 795 | return !(code == ck_identity |
| 796 | || code == ck_ambig |
| 797 | || code == ck_list |
| 798 | || code == ck_aggr |
| 799 | || code == ck_deferred_bad); |
| 800 | } |
| 801 | |
| 802 | static conversion * |
| 803 | build_conv (conversion_kind code, tree type, conversion *from) |
| 804 | { |
| 805 | conversion *t; |
| 806 | conversion_rank rank = CONVERSION_RANK (from); |
| 807 | |
| 808 | /* Only call this function for conversions that use u.next. */ |
| 809 | gcc_assert (from == NULL || has_next (code)); |
| 810 | |
| 811 | /* Note that the caller is responsible for filling in t->cand for |
| 812 | user-defined conversions. */ |
| 813 | t = alloc_conversion (kind: code); |
| 814 | t->type = type; |
| 815 | t->u.next = from; |
| 816 | |
| 817 | switch (code) |
| 818 | { |
| 819 | case ck_ptr: |
| 820 | case ck_pmem: |
| 821 | case ck_base: |
| 822 | case ck_std: |
| 823 | if (rank < cr_std) |
| 824 | rank = cr_std; |
| 825 | break; |
| 826 | |
| 827 | case ck_qual: |
| 828 | case ck_fnptr: |
| 829 | if (rank < cr_exact) |
| 830 | rank = cr_exact; |
| 831 | break; |
| 832 | |
| 833 | default: |
| 834 | break; |
| 835 | } |
| 836 | t->rank = rank; |
| 837 | t->user_conv_p = (code == ck_user || from->user_conv_p); |
| 838 | t->bad_p = from->bad_p; |
| 839 | t->base_p = false; |
| 840 | return t; |
| 841 | } |
| 842 | |
| 843 | /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a |
| 844 | specialization of std::initializer_list<T>, if such a conversion is |
| 845 | possible. */ |
| 846 | |
| 847 | static conversion * |
| 848 | build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain) |
| 849 | { |
| 850 | tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0); |
| 851 | unsigned len = CONSTRUCTOR_NELTS (ctor); |
| 852 | conversion **subconvs = alloc_conversions (n: len); |
| 853 | conversion *t; |
| 854 | unsigned i; |
| 855 | tree val; |
| 856 | |
| 857 | /* Within a list-initialization we can have more user-defined |
| 858 | conversions. */ |
| 859 | flags &= ~LOOKUP_NO_CONVERSION; |
| 860 | /* But no narrowing conversions. */ |
| 861 | flags |= LOOKUP_NO_NARROWING; |
| 862 | |
| 863 | /* Can't make an array of these types. */ |
| 864 | if (TYPE_REF_P (elttype) |
| 865 | || TREE_CODE (elttype) == FUNCTION_TYPE |
| 866 | || VOID_TYPE_P (elttype)) |
| 867 | return NULL; |
| 868 | |
| 869 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val) |
| 870 | { |
| 871 | if (TREE_CODE (val) == RAW_DATA_CST) |
| 872 | { |
| 873 | tree elt |
| 874 | = build_int_cst (TREE_TYPE (val), RAW_DATA_UCHAR_ELT (val, 0)); |
| 875 | conversion *sub |
| 876 | = implicit_conversion (elttype, TREE_TYPE (val), elt, |
| 877 | false, flags, complain); |
| 878 | conversion *next; |
| 879 | if (sub == NULL) |
| 880 | return NULL; |
| 881 | /* For conversion to initializer_list<unsigned char> or |
| 882 | initializer_list<char> or initializer_list<signed char> |
| 883 | we can optimize and keep RAW_DATA_CST with adjusted |
| 884 | type if we report narrowing errors if needed. |
| 885 | Use just one subconversion for that case. */ |
| 886 | if (sub->kind == ck_std |
| 887 | && sub->type |
| 888 | && (TREE_CODE (sub->type) == INTEGER_TYPE |
| 889 | || is_byte_access_type (sub->type)) |
| 890 | && TYPE_PRECISION (sub->type) == CHAR_BIT |
| 891 | && (next = next_conversion (sub)) |
| 892 | && next->kind == ck_identity) |
| 893 | { |
| 894 | subconvs[i] = sub; |
| 895 | continue; |
| 896 | } |
| 897 | /* Otherwise. build separate subconv for each RAW_DATA_CST |
| 898 | byte. Wrap those into an artificial ck_list which convert_like |
| 899 | will then handle. */ |
| 900 | conversion **subsubconvs = alloc_conversions (RAW_DATA_LENGTH (val)); |
| 901 | unsigned int j; |
| 902 | subsubconvs[0] = sub; |
| 903 | for (j = 1; j < (unsigned) RAW_DATA_LENGTH (val); ++j) |
| 904 | { |
| 905 | elt = build_int_cst (TREE_TYPE (val), |
| 906 | RAW_DATA_UCHAR_ELT (val, j)); |
| 907 | sub = implicit_conversion (elttype, TREE_TYPE (val), elt, |
| 908 | false, flags, complain); |
| 909 | if (sub == NULL) |
| 910 | return NULL; |
| 911 | subsubconvs[j] = sub; |
| 912 | } |
| 913 | |
| 914 | t = alloc_conversion (kind: ck_list); |
| 915 | t->type = type; |
| 916 | t->u.list = subsubconvs; |
| 917 | t->rank = cr_exact; |
| 918 | for (j = 0; j < (unsigned) RAW_DATA_LENGTH (val); ++j) |
| 919 | { |
| 920 | sub = subsubconvs[j]; |
| 921 | if (sub->rank > t->rank) |
| 922 | t->rank = sub->rank; |
| 923 | if (sub->user_conv_p) |
| 924 | t->user_conv_p = true; |
| 925 | if (sub->bad_p) |
| 926 | t->bad_p = true; |
| 927 | } |
| 928 | subconvs[i] = t; |
| 929 | continue; |
| 930 | } |
| 931 | |
| 932 | conversion *sub |
| 933 | = implicit_conversion (elttype, TREE_TYPE (val), val, |
| 934 | false, flags, complain); |
| 935 | if (sub == NULL) |
| 936 | return NULL; |
| 937 | |
| 938 | subconvs[i] = sub; |
| 939 | } |
| 940 | |
| 941 | t = alloc_conversion (kind: ck_list); |
| 942 | t->type = type; |
| 943 | t->u.list = subconvs; |
| 944 | t->rank = cr_exact; |
| 945 | |
| 946 | for (i = 0; i < len; ++i) |
| 947 | { |
| 948 | conversion *sub = subconvs[i]; |
| 949 | if (sub->rank > t->rank) |
| 950 | t->rank = sub->rank; |
| 951 | if (sub->user_conv_p) |
| 952 | t->user_conv_p = true; |
| 953 | if (sub->bad_p) |
| 954 | t->bad_p = true; |
| 955 | } |
| 956 | |
| 957 | return t; |
| 958 | } |
| 959 | |
| 960 | /* Return the next conversion of the conversion chain (if applicable), |
| 961 | or NULL otherwise. Please use this function instead of directly |
| 962 | accessing fields of struct conversion. */ |
| 963 | |
| 964 | static conversion * |
| 965 | next_conversion (conversion *conv) |
| 966 | { |
| 967 | if (conv == NULL |
| 968 | || !has_next (code: conv->kind)) |
| 969 | return NULL; |
| 970 | return conv->u.next; |
| 971 | } |
| 972 | |
| 973 | /* Strip to the first ck_user, ck_ambig, ck_list, ck_aggr or ck_identity |
| 974 | encountered. */ |
| 975 | |
| 976 | static conversion * |
| 977 | strip_standard_conversion (conversion *conv) |
| 978 | { |
| 979 | while (conv |
| 980 | && conv->kind != ck_user |
| 981 | && has_next (code: conv->kind)) |
| 982 | conv = next_conversion (conv); |
| 983 | return conv; |
| 984 | } |
| 985 | |
| 986 | /* Subroutine of build_aggr_conv: check whether FROM is a valid aggregate |
| 987 | initializer for array type ATYPE. */ |
| 988 | |
| 989 | static bool |
| 990 | can_convert_array (tree atype, tree from, int flags, tsubst_flags_t complain) |
| 991 | { |
| 992 | tree elttype = TREE_TYPE (atype); |
| 993 | unsigned i; |
| 994 | |
| 995 | if (TREE_CODE (from) == CONSTRUCTOR) |
| 996 | { |
| 997 | for (i = 0; i < CONSTRUCTOR_NELTS (from); ++i) |
| 998 | { |
| 999 | tree val = CONSTRUCTOR_ELT (from, i)->value; |
| 1000 | bool ok; |
| 1001 | if (TREE_CODE (elttype) == ARRAY_TYPE) |
| 1002 | ok = can_convert_array (atype: elttype, from: val, flags, complain); |
| 1003 | else |
| 1004 | ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags, |
| 1005 | complain); |
| 1006 | if (!ok) |
| 1007 | return false; |
| 1008 | } |
| 1009 | return true; |
| 1010 | } |
| 1011 | |
| 1012 | if (char_type_p (TYPE_MAIN_VARIANT (elttype)) |
| 1013 | && TREE_CODE (tree_strip_any_location_wrapper (from)) == STRING_CST) |
| 1014 | return array_string_literal_compatible_p (atype, from); |
| 1015 | |
| 1016 | /* No other valid way to aggregate initialize an array. */ |
| 1017 | return false; |
| 1018 | } |
| 1019 | |
| 1020 | /* Helper for build_aggr_conv. Return true if FIELD is in PSET, or if |
| 1021 | FIELD has ANON_AGGR_TYPE_P and any initializable field in there recursively |
| 1022 | is in PSET. */ |
| 1023 | |
| 1024 | static bool |
| 1025 | field_in_pset (hash_set<tree, true> &pset, tree field) |
| 1026 | { |
| 1027 | if (pset.contains (k: field)) |
| 1028 | return true; |
| 1029 | if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) |
| 1030 | for (field = TYPE_FIELDS (TREE_TYPE (field)); |
| 1031 | field; field = DECL_CHAIN (field)) |
| 1032 | { |
| 1033 | field = next_aggregate_field (field); |
| 1034 | if (field == NULL_TREE) |
| 1035 | break; |
| 1036 | if (field_in_pset (pset, field)) |
| 1037 | return true; |
| 1038 | } |
| 1039 | return false; |
| 1040 | } |
| 1041 | |
| 1042 | /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an |
| 1043 | aggregate class, if such a conversion is possible. */ |
| 1044 | |
| 1045 | static conversion * |
| 1046 | build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain) |
| 1047 | { |
| 1048 | unsigned HOST_WIDE_INT i = 0; |
| 1049 | conversion *c; |
| 1050 | tree field = next_aggregate_field (TYPE_FIELDS (type)); |
| 1051 | tree empty_ctor = NULL_TREE; |
| 1052 | hash_set<tree, true> pset; |
| 1053 | |
| 1054 | /* We already called reshape_init in implicit_conversion, but it might not |
| 1055 | have done anything in the case of parenthesized aggr init. */ |
| 1056 | |
| 1057 | /* The conversions within the init-list aren't affected by the enclosing |
| 1058 | context; they're always simple copy-initialization. */ |
| 1059 | flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING; |
| 1060 | |
| 1061 | /* For designated initializers, verify that each initializer is convertible |
| 1062 | to corresponding TREE_TYPE (ce->index) and mark those FIELD_DECLs as |
| 1063 | visited. In the following loop then ignore already visited |
| 1064 | FIELD_DECLs. */ |
| 1065 | tree idx, val; |
| 1066 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, val) |
| 1067 | { |
| 1068 | if (!idx) |
| 1069 | break; |
| 1070 | |
| 1071 | gcc_checking_assert (TREE_CODE (idx) == FIELD_DECL); |
| 1072 | |
| 1073 | tree ftype = TREE_TYPE (idx); |
| 1074 | bool ok; |
| 1075 | |
| 1076 | if (TREE_CODE (ftype) == ARRAY_TYPE) |
| 1077 | ok = can_convert_array (atype: ftype, from: val, flags, complain); |
| 1078 | else |
| 1079 | ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags, |
| 1080 | complain); |
| 1081 | |
| 1082 | if (!ok) |
| 1083 | return NULL; |
| 1084 | |
| 1085 | /* For unions, there should be just one initializer. */ |
| 1086 | if (TREE_CODE (type) == UNION_TYPE) |
| 1087 | { |
| 1088 | field = NULL_TREE; |
| 1089 | i = 1; |
| 1090 | break; |
| 1091 | } |
| 1092 | pset.add (k: idx); |
| 1093 | } |
| 1094 | |
| 1095 | for (; field; field = next_aggregate_field (DECL_CHAIN (field))) |
| 1096 | { |
| 1097 | tree ftype = TREE_TYPE (field); |
| 1098 | bool ok; |
| 1099 | |
| 1100 | if (!pset.is_empty () && field_in_pset (pset, field)) |
| 1101 | continue; |
| 1102 | if (i < CONSTRUCTOR_NELTS (ctor)) |
| 1103 | { |
| 1104 | constructor_elt *ce = CONSTRUCTOR_ELT (ctor, i); |
| 1105 | gcc_checking_assert (!ce->index); |
| 1106 | val = ce->value; |
| 1107 | ++i; |
| 1108 | } |
| 1109 | else if (DECL_INITIAL (field)) |
| 1110 | val = get_nsdmi (field, /*ctor*/false, complain); |
| 1111 | else if (TYPE_REF_P (ftype)) |
| 1112 | /* Value-initialization of reference is ill-formed. */ |
| 1113 | return NULL; |
| 1114 | else |
| 1115 | { |
| 1116 | if (empty_ctor == NULL_TREE) |
| 1117 | empty_ctor = build_constructor (init_list_type_node, NULL); |
| 1118 | val = empty_ctor; |
| 1119 | } |
| 1120 | |
| 1121 | if (TREE_CODE (ftype) == ARRAY_TYPE) |
| 1122 | ok = can_convert_array (atype: ftype, from: val, flags, complain); |
| 1123 | else |
| 1124 | ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags, |
| 1125 | complain); |
| 1126 | |
| 1127 | if (!ok) |
| 1128 | return NULL; |
| 1129 | |
| 1130 | if (TREE_CODE (type) == UNION_TYPE) |
| 1131 | break; |
| 1132 | } |
| 1133 | |
| 1134 | if (i < CONSTRUCTOR_NELTS (ctor)) |
| 1135 | return NULL; |
| 1136 | |
| 1137 | c = alloc_conversion (kind: ck_aggr); |
| 1138 | c->type = type; |
| 1139 | c->rank = cr_exact; |
| 1140 | c->user_conv_p = true; |
| 1141 | c->check_narrowing = true; |
| 1142 | c->u.expr = ctor; |
| 1143 | return c; |
| 1144 | } |
| 1145 | |
| 1146 | /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an |
| 1147 | array type, if such a conversion is possible. */ |
| 1148 | |
| 1149 | static conversion * |
| 1150 | build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain) |
| 1151 | { |
| 1152 | conversion *c; |
| 1153 | unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor); |
| 1154 | tree elttype = TREE_TYPE (type); |
| 1155 | bool bad = false; |
| 1156 | bool user = false; |
| 1157 | enum conversion_rank rank = cr_exact; |
| 1158 | |
| 1159 | /* We might need to propagate the size from the element to the array. */ |
| 1160 | complete_type (type); |
| 1161 | |
| 1162 | if (TYPE_DOMAIN (type) |
| 1163 | && !variably_modified_type_p (TYPE_DOMAIN (type), NULL_TREE)) |
| 1164 | { |
| 1165 | unsigned HOST_WIDE_INT alen = tree_to_uhwi (array_type_nelts_top (type)); |
| 1166 | if (alen < len) |
| 1167 | return NULL; |
| 1168 | } |
| 1169 | |
| 1170 | flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING; |
| 1171 | |
| 1172 | for (auto &e: CONSTRUCTOR_ELTS (ctor)) |
| 1173 | { |
| 1174 | conversion *sub |
| 1175 | = implicit_conversion (elttype, TREE_TYPE (e.value), e.value, |
| 1176 | false, flags, complain); |
| 1177 | if (sub == NULL) |
| 1178 | return NULL; |
| 1179 | |
| 1180 | if (sub->rank > rank) |
| 1181 | rank = sub->rank; |
| 1182 | if (sub->user_conv_p) |
| 1183 | user = true; |
| 1184 | if (sub->bad_p) |
| 1185 | bad = true; |
| 1186 | } |
| 1187 | |
| 1188 | c = alloc_conversion (kind: ck_aggr); |
| 1189 | c->type = type; |
| 1190 | c->rank = rank; |
| 1191 | c->user_conv_p = user; |
| 1192 | c->bad_p = bad; |
| 1193 | c->u.expr = ctor; |
| 1194 | return c; |
| 1195 | } |
| 1196 | |
| 1197 | /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a |
| 1198 | complex type, if such a conversion is possible. */ |
| 1199 | |
| 1200 | static conversion * |
| 1201 | build_complex_conv (tree type, tree ctor, int flags, |
| 1202 | tsubst_flags_t complain) |
| 1203 | { |
| 1204 | conversion *c; |
| 1205 | unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor); |
| 1206 | tree elttype = TREE_TYPE (type); |
| 1207 | bool bad = false; |
| 1208 | bool user = false; |
| 1209 | enum conversion_rank rank = cr_exact; |
| 1210 | |
| 1211 | if (len != 2) |
| 1212 | return NULL; |
| 1213 | |
| 1214 | flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING; |
| 1215 | |
| 1216 | for (auto &e: CONSTRUCTOR_ELTS (ctor)) |
| 1217 | { |
| 1218 | conversion *sub |
| 1219 | = implicit_conversion (elttype, TREE_TYPE (e.value), e.value, |
| 1220 | false, flags, complain); |
| 1221 | if (sub == NULL) |
| 1222 | return NULL; |
| 1223 | |
| 1224 | if (sub->rank > rank) |
| 1225 | rank = sub->rank; |
| 1226 | if (sub->user_conv_p) |
| 1227 | user = true; |
| 1228 | if (sub->bad_p) |
| 1229 | bad = true; |
| 1230 | } |
| 1231 | |
| 1232 | c = alloc_conversion (kind: ck_aggr); |
| 1233 | c->type = type; |
| 1234 | c->rank = rank; |
| 1235 | c->user_conv_p = user; |
| 1236 | c->bad_p = bad; |
| 1237 | c->u.expr = ctor; |
| 1238 | return c; |
| 1239 | } |
| 1240 | |
| 1241 | /* Build a representation of the identity conversion from EXPR to |
| 1242 | itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */ |
| 1243 | |
| 1244 | static conversion * |
| 1245 | build_identity_conv (tree type, tree expr) |
| 1246 | { |
| 1247 | conversion *c; |
| 1248 | |
| 1249 | c = alloc_conversion (kind: ck_identity); |
| 1250 | c->type = type; |
| 1251 | c->u.expr = expr; |
| 1252 | |
| 1253 | return c; |
| 1254 | } |
| 1255 | |
| 1256 | /* Converting from EXPR to TYPE was ambiguous in the sense that there |
| 1257 | were multiple user-defined conversions to accomplish the job. |
| 1258 | Build a conversion that indicates that ambiguity. */ |
| 1259 | |
| 1260 | static conversion * |
| 1261 | build_ambiguous_conv (tree type, tree expr) |
| 1262 | { |
| 1263 | conversion *c; |
| 1264 | |
| 1265 | c = alloc_conversion (kind: ck_ambig); |
| 1266 | c->type = type; |
| 1267 | c->u.expr = expr; |
| 1268 | |
| 1269 | return c; |
| 1270 | } |
| 1271 | |
| 1272 | tree |
| 1273 | strip_top_quals (tree t) |
| 1274 | { |
| 1275 | if (TREE_CODE (t) == ARRAY_TYPE) |
| 1276 | return t; |
| 1277 | return cp_build_qualified_type (t, 0); |
| 1278 | } |
| 1279 | |
| 1280 | /* Returns the standard conversion path (see [conv]) from type FROM to type |
| 1281 | TO, if any. For proper handling of null pointer constants, you must |
| 1282 | also pass the expression EXPR to convert from. If C_CAST_P is true, |
| 1283 | this conversion is coming from a C-style cast. */ |
| 1284 | |
| 1285 | static conversion * |
| 1286 | standard_conversion (tree to, tree from, tree expr, bool c_cast_p, |
| 1287 | int flags, tsubst_flags_t complain) |
| 1288 | { |
| 1289 | enum tree_code fcode, tcode; |
| 1290 | conversion *conv; |
| 1291 | bool fromref = false; |
| 1292 | tree qualified_to; |
| 1293 | |
| 1294 | to = non_reference (to); |
| 1295 | if (TYPE_REF_P (from)) |
| 1296 | { |
| 1297 | fromref = true; |
| 1298 | from = TREE_TYPE (from); |
| 1299 | } |
| 1300 | qualified_to = to; |
| 1301 | to = strip_top_quals (t: to); |
| 1302 | from = strip_top_quals (t: from); |
| 1303 | |
| 1304 | if (expr && type_unknown_p (expr)) |
| 1305 | { |
| 1306 | if (TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to)) |
| 1307 | { |
| 1308 | tsubst_flags_t tflags = tf_conv; |
| 1309 | expr = instantiate_type (to, expr, tflags); |
| 1310 | if (expr == error_mark_node) |
| 1311 | return NULL; |
| 1312 | from = TREE_TYPE (expr); |
| 1313 | } |
| 1314 | else if (TREE_CODE (to) == BOOLEAN_TYPE) |
| 1315 | { |
| 1316 | /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */ |
| 1317 | expr = resolve_nondeduced_context (expr, complain); |
| 1318 | from = TREE_TYPE (expr); |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | fcode = TREE_CODE (from); |
| 1323 | tcode = TREE_CODE (to); |
| 1324 | |
| 1325 | conv = build_identity_conv (type: from, expr); |
| 1326 | if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE) |
| 1327 | { |
| 1328 | from = type_decays_to (from); |
| 1329 | fcode = TREE_CODE (from); |
| 1330 | /* Tell convert_like that we're using the address. */ |
| 1331 | conv->rvaluedness_matches_p = true; |
| 1332 | conv = build_conv (code: ck_lvalue, type: from, from: conv); |
| 1333 | } |
| 1334 | /* Wrapping a ck_rvalue around a class prvalue (as a result of using |
| 1335 | obvalue_p) seems odd, since it's already a prvalue, but that's how we |
| 1336 | express the copy constructor call required by copy-initialization. */ |
| 1337 | else if (fromref || (expr && obvalue_p (expr))) |
| 1338 | { |
| 1339 | if (expr) |
| 1340 | { |
| 1341 | tree bitfield_type; |
| 1342 | bitfield_type = is_bitfield_expr_with_lowered_type (expr); |
| 1343 | if (bitfield_type) |
| 1344 | { |
| 1345 | from = strip_top_quals (t: bitfield_type); |
| 1346 | fcode = TREE_CODE (from); |
| 1347 | } |
| 1348 | } |
| 1349 | conv = build_conv (code: ck_rvalue, type: from, from: conv); |
| 1350 | /* If we're performing copy-initialization, remember to skip |
| 1351 | explicit constructors. */ |
| 1352 | if (flags & LOOKUP_ONLYCONVERTING) |
| 1353 | conv->copy_init_p = true; |
| 1354 | } |
| 1355 | |
| 1356 | /* Allow conversion between `__complex__' data types. */ |
| 1357 | if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE) |
| 1358 | { |
| 1359 | /* The standard conversion sequence to convert FROM to TO is |
| 1360 | the standard conversion sequence to perform componentwise |
| 1361 | conversion. */ |
| 1362 | conversion *part_conv = standard_conversion |
| 1363 | (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags, |
| 1364 | complain); |
| 1365 | |
| 1366 | if (!part_conv) |
| 1367 | conv = NULL; |
| 1368 | else if (part_conv->kind == ck_identity) |
| 1369 | /* Leave conv alone. */; |
| 1370 | else |
| 1371 | { |
| 1372 | conv = build_conv (code: part_conv->kind, type: to, from: conv); |
| 1373 | conv->rank = part_conv->rank; |
| 1374 | } |
| 1375 | |
| 1376 | return conv; |
| 1377 | } |
| 1378 | |
| 1379 | if (same_type_p (from, to)) |
| 1380 | { |
| 1381 | if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue) |
| 1382 | conv->type = qualified_to; |
| 1383 | else if (from != to) |
| 1384 | /* Use TO in order to not lose TO in diagnostics. */ |
| 1385 | conv->type = to; |
| 1386 | return conv; |
| 1387 | } |
| 1388 | |
| 1389 | /* [conv.ptr] |
| 1390 | A null pointer constant can be converted to a pointer type; ... A |
| 1391 | null pointer constant of integral type can be converted to an |
| 1392 | rvalue of type std::nullptr_t. */ |
| 1393 | if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to) |
| 1394 | || NULLPTR_TYPE_P (to)) |
| 1395 | && ((expr && null_ptr_cst_p (t: expr)) |
| 1396 | || NULLPTR_TYPE_P (from))) |
| 1397 | conv = build_conv (code: ck_std, type: to, from: conv); |
| 1398 | else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE) |
| 1399 | || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE)) |
| 1400 | { |
| 1401 | /* For backwards brain damage compatibility, allow interconversion of |
| 1402 | pointers and integers with a pedwarn. */ |
| 1403 | conv = build_conv (code: ck_std, type: to, from: conv); |
| 1404 | conv->bad_p = true; |
| 1405 | } |
| 1406 | else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE) |
| 1407 | { |
| 1408 | /* For backwards brain damage compatibility, allow interconversion of |
| 1409 | enums and integers with a pedwarn. */ |
| 1410 | conv = build_conv (code: ck_std, type: to, from: conv); |
| 1411 | conv->bad_p = true; |
| 1412 | } |
| 1413 | else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE) |
| 1414 | || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from))) |
| 1415 | { |
| 1416 | tree to_pointee; |
| 1417 | tree from_pointee; |
| 1418 | |
| 1419 | if (tcode == POINTER_TYPE) |
| 1420 | { |
| 1421 | to_pointee = TREE_TYPE (to); |
| 1422 | from_pointee = TREE_TYPE (from); |
| 1423 | |
| 1424 | /* Since this is the target of a pointer, it can't have function |
| 1425 | qualifiers, so any TYPE_QUALS must be for attributes const or |
| 1426 | noreturn. Strip them. */ |
| 1427 | if (TREE_CODE (to_pointee) == FUNCTION_TYPE |
| 1428 | && TYPE_QUALS (to_pointee)) |
| 1429 | to_pointee = build_qualified_type (to_pointee, TYPE_UNQUALIFIED); |
| 1430 | if (TREE_CODE (from_pointee) == FUNCTION_TYPE |
| 1431 | && TYPE_QUALS (from_pointee)) |
| 1432 | from_pointee = build_qualified_type (from_pointee, TYPE_UNQUALIFIED); |
| 1433 | } |
| 1434 | else |
| 1435 | { |
| 1436 | to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to); |
| 1437 | from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from); |
| 1438 | } |
| 1439 | |
| 1440 | if (tcode == POINTER_TYPE |
| 1441 | && same_type_ignoring_top_level_qualifiers_p (from_pointee, |
| 1442 | to_pointee)) |
| 1443 | ; |
| 1444 | else if (VOID_TYPE_P (to_pointee) |
| 1445 | && !TYPE_PTRDATAMEM_P (from) |
| 1446 | && TREE_CODE (from_pointee) != FUNCTION_TYPE) |
| 1447 | { |
| 1448 | tree nfrom = TREE_TYPE (from); |
| 1449 | /* Don't try to apply restrict to void. */ |
| 1450 | int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT; |
| 1451 | from_pointee = cp_build_qualified_type (void_type_node, quals); |
| 1452 | from = build_pointer_type (from_pointee); |
| 1453 | conv = build_conv (code: ck_ptr, type: from, from: conv); |
| 1454 | } |
| 1455 | else if (TYPE_PTRDATAMEM_P (from)) |
| 1456 | { |
| 1457 | tree fbase = TYPE_PTRMEM_CLASS_TYPE (from); |
| 1458 | tree tbase = TYPE_PTRMEM_CLASS_TYPE (to); |
| 1459 | |
| 1460 | if (same_type_p (fbase, tbase)) |
| 1461 | /* No base conversion needed. */; |
| 1462 | else if (DERIVED_FROM_P (fbase, tbase) |
| 1463 | && (same_type_ignoring_top_level_qualifiers_p |
| 1464 | (from_pointee, to_pointee))) |
| 1465 | { |
| 1466 | from = build_ptrmem_type (tbase, from_pointee); |
| 1467 | conv = build_conv (code: ck_pmem, type: from, from: conv); |
| 1468 | } |
| 1469 | else |
| 1470 | return NULL; |
| 1471 | } |
| 1472 | else if (CLASS_TYPE_P (from_pointee) |
| 1473 | && CLASS_TYPE_P (to_pointee) |
| 1474 | /* [conv.ptr] |
| 1475 | |
| 1476 | An rvalue of type "pointer to cv D," where D is a |
| 1477 | class type, can be converted to an rvalue of type |
| 1478 | "pointer to cv B," where B is a base class (clause |
| 1479 | _class.derived_) of D. If B is an inaccessible |
| 1480 | (clause _class.access_) or ambiguous |
| 1481 | (_class.member.lookup_) base class of D, a program |
| 1482 | that necessitates this conversion is ill-formed. |
| 1483 | Therefore, we use DERIVED_FROM_P, and do not check |
| 1484 | access or uniqueness. */ |
| 1485 | && DERIVED_FROM_P (to_pointee, from_pointee)) |
| 1486 | { |
| 1487 | from_pointee |
| 1488 | = cp_build_qualified_type (to_pointee, |
| 1489 | cp_type_quals (from_pointee)); |
| 1490 | from = build_pointer_type (from_pointee); |
| 1491 | conv = build_conv (code: ck_ptr, type: from, from: conv); |
| 1492 | conv->base_p = true; |
| 1493 | } |
| 1494 | |
| 1495 | if (same_type_p (from, to)) |
| 1496 | /* OK */; |
| 1497 | else if (c_cast_p && comp_ptr_ttypes_const (to, from, bounds_either)) |
| 1498 | /* In a C-style cast, we ignore CV-qualification because we |
| 1499 | are allowed to perform a static_cast followed by a |
| 1500 | const_cast. */ |
| 1501 | conv = build_conv (code: ck_qual, type: to, from: conv); |
| 1502 | else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee)) |
| 1503 | conv = build_conv (code: ck_qual, type: to, from: conv); |
| 1504 | else if (expr && string_conv_p (to, expr, 0)) |
| 1505 | /* converting from string constant to char *. */ |
| 1506 | conv = build_conv (code: ck_qual, type: to, from: conv); |
| 1507 | else if (fnptr_conv_p (to, from)) |
| 1508 | conv = build_conv (code: ck_fnptr, type: to, from: conv); |
| 1509 | /* Allow conversions among compatible ObjC pointer types (base |
| 1510 | conversions have been already handled above). */ |
| 1511 | else if (c_dialect_objc () |
| 1512 | && objc_compare_types (to, from, -4, NULL_TREE)) |
| 1513 | conv = build_conv (code: ck_ptr, type: to, from: conv); |
| 1514 | else if (ptr_reasonably_similar (to_pointee, from_pointee)) |
| 1515 | { |
| 1516 | conv = build_conv (code: ck_ptr, type: to, from: conv); |
| 1517 | conv->bad_p = true; |
| 1518 | } |
| 1519 | else |
| 1520 | return NULL; |
| 1521 | |
| 1522 | from = to; |
| 1523 | } |
| 1524 | else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from)) |
| 1525 | { |
| 1526 | tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from)); |
| 1527 | tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to)); |
| 1528 | tree fbase = class_of_this_parm (fntype: fromfn); |
| 1529 | tree tbase = class_of_this_parm (fntype: tofn); |
| 1530 | |
| 1531 | /* If FBASE and TBASE are equivalent but incomplete, DERIVED_FROM_P |
| 1532 | yields false. But a pointer to member of incomplete class is OK. */ |
| 1533 | if (!same_type_p (fbase, tbase) && !DERIVED_FROM_P (fbase, tbase)) |
| 1534 | return NULL; |
| 1535 | |
| 1536 | tree fstat = static_fn_type (fromfn); |
| 1537 | tree tstat = static_fn_type (tofn); |
| 1538 | if (same_type_p (tstat, fstat) |
| 1539 | || fnptr_conv_p (tstat, fstat)) |
| 1540 | /* OK */; |
| 1541 | else |
| 1542 | return NULL; |
| 1543 | |
| 1544 | if (!same_type_p (fbase, tbase)) |
| 1545 | { |
| 1546 | from = build_memfn_type (fstat, |
| 1547 | tbase, |
| 1548 | cp_type_quals (tbase), |
| 1549 | type_memfn_rqual (tofn)); |
| 1550 | from = build_ptrmemfunc_type (build_pointer_type (from)); |
| 1551 | conv = build_conv (code: ck_pmem, type: from, from: conv); |
| 1552 | conv->base_p = true; |
| 1553 | } |
| 1554 | if (fnptr_conv_p (tstat, fstat)) |
| 1555 | conv = build_conv (code: ck_fnptr, type: to, from: conv); |
| 1556 | } |
| 1557 | else if (tcode == BOOLEAN_TYPE) |
| 1558 | { |
| 1559 | /* [conv.bool] |
| 1560 | |
| 1561 | A prvalue of arithmetic, unscoped enumeration, pointer, or pointer |
| 1562 | to member type can be converted to a prvalue of type bool. ... |
| 1563 | For direct-initialization (8.5 [dcl.init]), a prvalue of type |
| 1564 | std::nullptr_t can be converted to a prvalue of type bool; */ |
| 1565 | if (ARITHMETIC_TYPE_P (from) |
| 1566 | || UNSCOPED_ENUM_P (from) |
| 1567 | || fcode == POINTER_TYPE |
| 1568 | || TYPE_PTRMEM_P (from) |
| 1569 | || NULLPTR_TYPE_P (from)) |
| 1570 | { |
| 1571 | conv = build_conv (code: ck_std, type: to, from: conv); |
| 1572 | if (fcode == POINTER_TYPE |
| 1573 | || TYPE_PTRDATAMEM_P (from) |
| 1574 | || (TYPE_PTRMEMFUNC_P (from) |
| 1575 | && conv->rank < cr_pbool) |
| 1576 | || NULLPTR_TYPE_P (from)) |
| 1577 | conv->rank = cr_pbool; |
| 1578 | if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING)) |
| 1579 | conv->bad_p = true; |
| 1580 | if (flags & LOOKUP_NO_NARROWING) |
| 1581 | conv->check_narrowing = true; |
| 1582 | return conv; |
| 1583 | } |
| 1584 | |
| 1585 | return NULL; |
| 1586 | } |
| 1587 | /* We don't check for ENUMERAL_TYPE here because there are no standard |
| 1588 | conversions to enum type. */ |
| 1589 | /* As an extension, allow conversion to complex type. */ |
| 1590 | else if (ARITHMETIC_TYPE_P (to)) |
| 1591 | { |
| 1592 | if (! (INTEGRAL_CODE_P (fcode) |
| 1593 | || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL))) |
| 1594 | || SCOPED_ENUM_P (from)) |
| 1595 | return NULL; |
| 1596 | |
| 1597 | /* If we're parsing an enum with no fixed underlying type, we're |
| 1598 | dealing with an incomplete type, which renders the conversion |
| 1599 | ill-formed. */ |
| 1600 | if (!COMPLETE_TYPE_P (from)) |
| 1601 | return NULL; |
| 1602 | |
| 1603 | conv = build_conv (code: ck_std, type: to, from: conv); |
| 1604 | |
| 1605 | tree underlying_type = NULL_TREE; |
| 1606 | if (TREE_CODE (from) == ENUMERAL_TYPE |
| 1607 | && ENUM_FIXED_UNDERLYING_TYPE_P (from)) |
| 1608 | underlying_type = ENUM_UNDERLYING_TYPE (from); |
| 1609 | |
| 1610 | /* Give this a better rank if it's a promotion. |
| 1611 | |
| 1612 | To handle CWG 1601, also bump the rank if we are converting |
| 1613 | an enumeration with a fixed underlying type to the underlying |
| 1614 | type. */ |
| 1615 | if ((same_type_p (to, type_promotes_to (from)) |
| 1616 | || (underlying_type && same_type_p (to, underlying_type))) |
| 1617 | && next_conversion (conv)->rank <= cr_promotion) |
| 1618 | conv->rank = cr_promotion; |
| 1619 | |
| 1620 | /* A prvalue of floating-point type can be converted to a prvalue of |
| 1621 | another floating-point type with a greater or equal conversion |
| 1622 | rank ([conv.rank]). A prvalue of standard floating-point type can |
| 1623 | be converted to a prvalue of another standard floating-point type. |
| 1624 | For backwards compatibility with handling __float128 and other |
| 1625 | non-standard floating point types, allow all implicit floating |
| 1626 | point conversions if neither type is extended floating-point |
| 1627 | type and if at least one of them is, fail if they have unordered |
| 1628 | conversion rank or from has higher conversion rank. */ |
| 1629 | if (fcode == REAL_TYPE |
| 1630 | && tcode == REAL_TYPE |
| 1631 | && (extended_float_type_p (type: from) |
| 1632 | || extended_float_type_p (type: to)) |
| 1633 | && cp_compare_floating_point_conversion_ranks (from, to) >= 2) |
| 1634 | conv->bad_p = true; |
| 1635 | } |
| 1636 | else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE |
| 1637 | && vector_types_convertible_p (t1: from, t2: to, emit_lax_note: false)) |
| 1638 | return build_conv (code: ck_std, type: to, from: conv); |
| 1639 | else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from) |
| 1640 | && is_properly_derived_from (from, to)) |
| 1641 | { |
| 1642 | if (conv->kind == ck_rvalue) |
| 1643 | conv = next_conversion (conv); |
| 1644 | conv = build_conv (code: ck_base, type: to, from: conv); |
| 1645 | /* The derived-to-base conversion indicates the initialization |
| 1646 | of a parameter with base type from an object of a derived |
| 1647 | type. A temporary object is created to hold the result of |
| 1648 | the conversion unless we're binding directly to a reference. */ |
| 1649 | conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND); |
| 1650 | /* If we're performing copy-initialization, remember to skip |
| 1651 | explicit constructors. */ |
| 1652 | if (flags & LOOKUP_ONLYCONVERTING) |
| 1653 | conv->copy_init_p = true; |
| 1654 | } |
| 1655 | else |
| 1656 | return NULL; |
| 1657 | |
| 1658 | if (flags & LOOKUP_NO_NARROWING) |
| 1659 | conv->check_narrowing = true; |
| 1660 | |
| 1661 | return conv; |
| 1662 | } |
| 1663 | |
| 1664 | /* Returns nonzero if T1 is reference-related to T2. |
| 1665 | |
| 1666 | This is considered when a reference to T1 is initialized by a T2. */ |
| 1667 | |
| 1668 | bool |
| 1669 | reference_related_p (tree t1, tree t2) |
| 1670 | { |
| 1671 | if (t1 == error_mark_node || t2 == error_mark_node) |
| 1672 | return false; |
| 1673 | |
| 1674 | t1 = TYPE_MAIN_VARIANT (t1); |
| 1675 | t2 = TYPE_MAIN_VARIANT (t2); |
| 1676 | |
| 1677 | /* [dcl.init.ref] |
| 1678 | |
| 1679 | Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related |
| 1680 | to "cv2 T2" if T1 is similar to T2, or T1 is a base class of T2. */ |
| 1681 | return (similar_type_p (t1, t2) |
| 1682 | || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2) |
| 1683 | && DERIVED_FROM_P (t1, t2))); |
| 1684 | } |
| 1685 | |
| 1686 | /* Returns nonzero if T1 is reference-compatible with T2. */ |
| 1687 | |
| 1688 | bool |
| 1689 | reference_compatible_p (tree t1, tree t2) |
| 1690 | { |
| 1691 | /* [dcl.init.ref] |
| 1692 | |
| 1693 | "cv1 T1" is reference compatible with "cv2 T2" if |
| 1694 | a prvalue of type "pointer to cv2 T2" can be converted to the type |
| 1695 | "pointer to cv1 T1" via a standard conversion sequence. */ |
| 1696 | tree ptype1 = build_pointer_type (t1); |
| 1697 | tree ptype2 = build_pointer_type (t2); |
| 1698 | conversion *conv = standard_conversion (to: ptype1, from: ptype2, NULL_TREE, |
| 1699 | /*c_cast_p=*/false, flags: 0, complain: tf_none); |
| 1700 | if (!conv || conv->bad_p) |
| 1701 | return false; |
| 1702 | return true; |
| 1703 | } |
| 1704 | |
| 1705 | /* Return true if converting FROM to TO would involve a qualification |
| 1706 | conversion. */ |
| 1707 | |
| 1708 | static bool |
| 1709 | involves_qualification_conversion_p (tree to, tree from) |
| 1710 | { |
| 1711 | /* If we're not convering a pointer to another one, we won't get |
| 1712 | a qualification conversion. */ |
| 1713 | if (!((TYPE_PTR_P (to) && TYPE_PTR_P (from)) |
| 1714 | || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))) |
| 1715 | return false; |
| 1716 | |
| 1717 | conversion *conv = standard_conversion (to, from, NULL_TREE, |
| 1718 | /*c_cast_p=*/false, flags: 0, complain: tf_none); |
| 1719 | for (conversion *t = conv; t; t = next_conversion (conv: t)) |
| 1720 | if (t->kind == ck_qual) |
| 1721 | return true; |
| 1722 | |
| 1723 | return false; |
| 1724 | } |
| 1725 | |
| 1726 | /* A reference of the indicated TYPE is being bound directly to the |
| 1727 | expression represented by the implicit conversion sequence CONV. |
| 1728 | Return a conversion sequence for this binding. */ |
| 1729 | |
| 1730 | static conversion * |
| 1731 | direct_reference_binding (tree type, conversion *conv) |
| 1732 | { |
| 1733 | tree t; |
| 1734 | |
| 1735 | gcc_assert (TYPE_REF_P (type)); |
| 1736 | gcc_assert (!TYPE_REF_P (conv->type)); |
| 1737 | |
| 1738 | t = TREE_TYPE (type); |
| 1739 | |
| 1740 | if (conv->kind == ck_identity) |
| 1741 | /* Mark the identity conv as to not decay to rvalue. */ |
| 1742 | conv->rvaluedness_matches_p = true; |
| 1743 | |
| 1744 | /* [over.ics.rank] |
| 1745 | |
| 1746 | When a parameter of reference type binds directly |
| 1747 | (_dcl.init.ref_) to an argument expression, the implicit |
| 1748 | conversion sequence is the identity conversion, unless the |
| 1749 | argument expression has a type that is a derived class of the |
| 1750 | parameter type, in which case the implicit conversion sequence is |
| 1751 | a derived-to-base Conversion. |
| 1752 | |
| 1753 | If the parameter binds directly to the result of applying a |
| 1754 | conversion function to the argument expression, the implicit |
| 1755 | conversion sequence is a user-defined conversion sequence |
| 1756 | (_over.ics.user_), with the second standard conversion sequence |
| 1757 | either an identity conversion or, if the conversion function |
| 1758 | returns an entity of a type that is a derived class of the |
| 1759 | parameter type, a derived-to-base conversion. */ |
| 1760 | if (is_properly_derived_from (conv->type, t)) |
| 1761 | { |
| 1762 | /* Represent the derived-to-base conversion. */ |
| 1763 | conv = build_conv (code: ck_base, type: t, from: conv); |
| 1764 | /* We will actually be binding to the base-class subobject in |
| 1765 | the derived class, so we mark this conversion appropriately. |
| 1766 | That way, convert_like knows not to generate a temporary. */ |
| 1767 | conv->need_temporary_p = false; |
| 1768 | } |
| 1769 | else if (involves_qualification_conversion_p (to: t, from: conv->type)) |
| 1770 | /* Represent the qualification conversion. After DR 2352 |
| 1771 | #1 and #2 were indistinguishable conversion sequences: |
| 1772 | |
| 1773 | void f(int*); // #1 |
| 1774 | void f(const int* const &); // #2 |
| 1775 | void g(int* p) { f(p); } |
| 1776 | |
| 1777 | because the types "int *" and "const int *const" are |
| 1778 | reference-related and we were binding both directly and they |
| 1779 | had the same rank. To break it up, we add a ck_qual under the |
| 1780 | ck_ref_bind so that conversion sequence ranking chooses #1. |
| 1781 | |
| 1782 | We strip_top_quals here which is also what standard_conversion |
| 1783 | does. Failure to do so would confuse comp_cv_qual_signature |
| 1784 | into thinking that in |
| 1785 | |
| 1786 | void f(const int * const &); // #1 |
| 1787 | void f(const int *); // #2 |
| 1788 | int *x; |
| 1789 | f(x); |
| 1790 | |
| 1791 | #2 is a better match than #1 even though they're ambiguous (97296). */ |
| 1792 | conv = build_conv (code: ck_qual, type: strip_top_quals (t), from: conv); |
| 1793 | |
| 1794 | return build_conv (code: ck_ref_bind, type, from: conv); |
| 1795 | } |
| 1796 | |
| 1797 | /* Returns the conversion path from type FROM to reference type TO for |
| 1798 | purposes of reference binding. For lvalue binding, either pass a |
| 1799 | reference type to FROM or an lvalue expression to EXPR. If the |
| 1800 | reference will be bound to a temporary, NEED_TEMPORARY_P is set for |
| 1801 | the conversion returned. If C_CAST_P is true, this |
| 1802 | conversion is coming from a C-style cast. */ |
| 1803 | |
| 1804 | static conversion * |
| 1805 | reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags, |
| 1806 | tsubst_flags_t complain) |
| 1807 | { |
| 1808 | conversion *conv = NULL; |
| 1809 | conversion *bad_direct_conv = nullptr; |
| 1810 | tree to = TREE_TYPE (rto); |
| 1811 | tree from = rfrom; |
| 1812 | tree tfrom; |
| 1813 | bool related_p; |
| 1814 | bool compatible_p; |
| 1815 | cp_lvalue_kind gl_kind; |
| 1816 | bool is_lvalue; |
| 1817 | |
| 1818 | if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr)) |
| 1819 | { |
| 1820 | expr = instantiate_type (to, expr, tf_none); |
| 1821 | if (expr == error_mark_node) |
| 1822 | return NULL; |
| 1823 | from = TREE_TYPE (expr); |
| 1824 | } |
| 1825 | |
| 1826 | bool copy_list_init = false; |
| 1827 | bool single_list_conv = false; |
| 1828 | if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr)) |
| 1829 | { |
| 1830 | maybe_warn_cpp0x (str: CPP0X_INITIALIZER_LISTS); |
| 1831 | /* DR 1288: Otherwise, if the initializer list has a single element |
| 1832 | of type E and ... [T's] referenced type is reference-related to E, |
| 1833 | the object or reference is initialized from that element... |
| 1834 | |
| 1835 | ??? With P0388R4, we should bind 't' directly to U{}: |
| 1836 | using U = A[2]; |
| 1837 | A (&&t)[] = {U{}}; |
| 1838 | because A[] and A[2] are reference-related. But we don't do it |
| 1839 | because grok_reference_init has deduced the array size (to 1), and |
| 1840 | A[1] and A[2] aren't reference-related. */ |
| 1841 | if (CONSTRUCTOR_NELTS (expr) == 1 |
| 1842 | && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr)) |
| 1843 | { |
| 1844 | tree elt = CONSTRUCTOR_ELT (expr, 0)->value; |
| 1845 | if (error_operand_p (t: elt)) |
| 1846 | return NULL; |
| 1847 | tree etype = TREE_TYPE (elt); |
| 1848 | if (reference_related_p (t1: to, t2: etype)) |
| 1849 | { |
| 1850 | expr = elt; |
| 1851 | from = etype; |
| 1852 | goto skip; |
| 1853 | } |
| 1854 | else if (CLASS_TYPE_P (etype) && TYPE_HAS_CONVERSION (etype)) |
| 1855 | /* CWG1996: jason's proposed drafting adds "or initializing T from E |
| 1856 | would bind directly". We check that in the direct binding with |
| 1857 | conversion code below. */ |
| 1858 | single_list_conv = true; |
| 1859 | } |
| 1860 | /* Otherwise, if T is a reference type, a prvalue temporary of the type |
| 1861 | referenced by T is copy-list-initialized, and the reference is bound |
| 1862 | to that temporary. */ |
| 1863 | copy_list_init = true; |
| 1864 | skip:; |
| 1865 | } |
| 1866 | |
| 1867 | if (TYPE_REF_P (from)) |
| 1868 | { |
| 1869 | from = TREE_TYPE (from); |
| 1870 | if (!TYPE_REF_IS_RVALUE (rfrom) |
| 1871 | || TREE_CODE (from) == FUNCTION_TYPE) |
| 1872 | gl_kind = clk_ordinary; |
| 1873 | else |
| 1874 | gl_kind = clk_rvalueref; |
| 1875 | } |
| 1876 | else if (expr) |
| 1877 | gl_kind = lvalue_kind (expr); |
| 1878 | else if (CLASS_TYPE_P (from) |
| 1879 | || TREE_CODE (from) == ARRAY_TYPE) |
| 1880 | gl_kind = clk_class; |
| 1881 | else |
| 1882 | gl_kind = clk_none; |
| 1883 | |
| 1884 | /* Don't allow a class prvalue when LOOKUP_NO_TEMP_BIND. */ |
| 1885 | if ((flags & LOOKUP_NO_TEMP_BIND) |
| 1886 | && (gl_kind & clk_class)) |
| 1887 | gl_kind = clk_none; |
| 1888 | |
| 1889 | /* Same mask as real_lvalue_p. */ |
| 1890 | is_lvalue = gl_kind && !(gl_kind & (clk_rvalueref|clk_class)); |
| 1891 | |
| 1892 | tfrom = from; |
| 1893 | if ((gl_kind & clk_bitfield) != 0) |
| 1894 | tfrom = unlowered_expr_type (expr); |
| 1895 | |
| 1896 | /* Figure out whether or not the types are reference-related and |
| 1897 | reference compatible. We have to do this after stripping |
| 1898 | references from FROM. */ |
| 1899 | related_p = reference_related_p (t1: to, t2: tfrom); |
| 1900 | /* If this is a C cast, first convert to an appropriately qualified |
| 1901 | type, so that we can later do a const_cast to the desired type. */ |
| 1902 | if (related_p && c_cast_p |
| 1903 | && !at_least_as_qualified_p (to, tfrom)) |
| 1904 | to = cp_build_qualified_type (to, cp_type_quals (tfrom)); |
| 1905 | compatible_p = reference_compatible_p (t1: to, t2: tfrom); |
| 1906 | |
| 1907 | /* Directly bind reference when target expression's type is compatible with |
| 1908 | the reference and expression is an lvalue. In DR391, the wording in |
| 1909 | [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for |
| 1910 | const and rvalue references to rvalues of compatible class type. |
| 1911 | We should also do direct bindings for non-class xvalues. */ |
| 1912 | if ((related_p || compatible_p) && gl_kind) |
| 1913 | { |
| 1914 | /* [dcl.init.ref] |
| 1915 | |
| 1916 | If the initializer expression |
| 1917 | |
| 1918 | -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1" |
| 1919 | is reference-compatible with "cv2 T2," |
| 1920 | |
| 1921 | the reference is bound directly to the initializer expression |
| 1922 | lvalue. |
| 1923 | |
| 1924 | [...] |
| 1925 | If the initializer expression is an rvalue, with T2 a class type, |
| 1926 | and "cv1 T1" is reference-compatible with "cv2 T2", the reference |
| 1927 | is bound to the object represented by the rvalue or to a sub-object |
| 1928 | within that object. */ |
| 1929 | |
| 1930 | conv = build_identity_conv (type: tfrom, expr); |
| 1931 | conv = direct_reference_binding (type: rto, conv); |
| 1932 | |
| 1933 | if (TYPE_REF_P (rfrom)) |
| 1934 | /* Handle rvalue reference to function properly. */ |
| 1935 | conv->rvaluedness_matches_p |
| 1936 | = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom)); |
| 1937 | else |
| 1938 | conv->rvaluedness_matches_p |
| 1939 | = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue); |
| 1940 | |
| 1941 | if ((gl_kind & clk_bitfield) != 0 |
| 1942 | || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to))) |
| 1943 | /* For the purposes of overload resolution, we ignore the fact |
| 1944 | this expression is a bitfield or packed field. (In particular, |
| 1945 | [over.ics.ref] says specifically that a function with a |
| 1946 | non-const reference parameter is viable even if the |
| 1947 | argument is a bitfield.) |
| 1948 | |
| 1949 | However, when we actually call the function we must create |
| 1950 | a temporary to which to bind the reference. If the |
| 1951 | reference is volatile, or isn't const, then we cannot make |
| 1952 | a temporary, so we just issue an error when the conversion |
| 1953 | actually occurs. */ |
| 1954 | conv->need_temporary_p = true; |
| 1955 | |
| 1956 | /* Don't allow binding of lvalues (other than function lvalues) to |
| 1957 | rvalue references. */ |
| 1958 | if (is_lvalue && TYPE_REF_IS_RVALUE (rto) |
| 1959 | && TREE_CODE (to) != FUNCTION_TYPE) |
| 1960 | conv->bad_p = true; |
| 1961 | |
| 1962 | /* Nor the reverse. */ |
| 1963 | if (!is_lvalue && !TYPE_REF_IS_RVALUE (rto) |
| 1964 | /* Unless it's really a C++20 lvalue being treated as an xvalue. |
| 1965 | But in C++23, such an expression is just an xvalue, not a special |
| 1966 | lvalue, so the binding is once again ill-formed. */ |
| 1967 | && !(cxx_dialect <= cxx20 |
| 1968 | && (gl_kind & clk_implicit_rval)) |
| 1969 | && (!CP_TYPE_CONST_NON_VOLATILE_P (to) |
| 1970 | || (flags & LOOKUP_NO_RVAL_BIND)) |
| 1971 | && TREE_CODE (to) != FUNCTION_TYPE) |
| 1972 | conv->bad_p = true; |
| 1973 | |
| 1974 | if (!compatible_p) |
| 1975 | conv->bad_p = true; |
| 1976 | |
| 1977 | return conv; |
| 1978 | } |
| 1979 | /* [class.conv.fct] A conversion function is never used to convert a |
| 1980 | (possibly cv-qualified) object to the (possibly cv-qualified) same |
| 1981 | object type (or a reference to it), to a (possibly cv-qualified) base |
| 1982 | class of that type (or a reference to it).... */ |
| 1983 | else if (!related_p |
| 1984 | && !(flags & LOOKUP_NO_CONVERSION) |
| 1985 | && (CLASS_TYPE_P (from) || single_list_conv)) |
| 1986 | { |
| 1987 | tree rexpr = expr; |
| 1988 | if (single_list_conv) |
| 1989 | rexpr = CONSTRUCTOR_ELT (expr, 0)->value; |
| 1990 | |
| 1991 | /* [dcl.init.ref] |
| 1992 | |
| 1993 | If the initializer expression |
| 1994 | |
| 1995 | -- has a class type (i.e., T2 is a class type) can be |
| 1996 | implicitly converted to an lvalue of type "cv3 T3," where |
| 1997 | "cv1 T1" is reference-compatible with "cv3 T3". (this |
| 1998 | conversion is selected by enumerating the applicable |
| 1999 | conversion functions (_over.match.ref_) and choosing the |
| 2000 | best one through overload resolution. (_over.match_). |
| 2001 | |
| 2002 | the reference is bound to the lvalue result of the conversion |
| 2003 | in the second case. */ |
| 2004 | z_candidate *cand = build_user_type_conversion_1 (rto, rexpr, flags, |
| 2005 | complain); |
| 2006 | if (cand) |
| 2007 | { |
| 2008 | if (!cand->second_conv->bad_p) |
| 2009 | return cand->second_conv; |
| 2010 | |
| 2011 | /* Direct reference binding wasn't successful and yielded a bad |
| 2012 | conversion. Proceed with trying to go through a temporary |
| 2013 | instead, and if that also fails then we'll return this bad |
| 2014 | conversion rather than no conversion for sake of better |
| 2015 | diagnostics. */ |
| 2016 | bad_direct_conv = cand->second_conv; |
| 2017 | } |
| 2018 | } |
| 2019 | |
| 2020 | /* From this point on, we conceptually need temporaries, even if we |
| 2021 | elide them. Only the cases above are "direct bindings". */ |
| 2022 | if (flags & LOOKUP_NO_TEMP_BIND) |
| 2023 | return bad_direct_conv ? bad_direct_conv : nullptr; |
| 2024 | |
| 2025 | /* [over.ics.rank] |
| 2026 | |
| 2027 | When a parameter of reference type is not bound directly to an |
| 2028 | argument expression, the conversion sequence is the one required |
| 2029 | to convert the argument expression to the underlying type of the |
| 2030 | reference according to _over.best.ics_. Conceptually, this |
| 2031 | conversion sequence corresponds to copy-initializing a temporary |
| 2032 | of the underlying type with the argument expression. Any |
| 2033 | difference in top-level cv-qualification is subsumed by the |
| 2034 | initialization itself and does not constitute a conversion. */ |
| 2035 | |
| 2036 | bool maybe_valid_p = true; |
| 2037 | |
| 2038 | /* [dcl.init.ref] |
| 2039 | |
| 2040 | Otherwise, the reference shall be an lvalue reference to a |
| 2041 | non-volatile const type, or the reference shall be an rvalue |
| 2042 | reference. */ |
| 2043 | if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto)) |
| 2044 | maybe_valid_p = false; |
| 2045 | |
| 2046 | /* [dcl.init.ref] |
| 2047 | |
| 2048 | Otherwise, a temporary of type "cv1 T1" is created and |
| 2049 | initialized from the initializer expression using the rules for a |
| 2050 | non-reference copy initialization. If T1 is reference-related to |
| 2051 | T2, cv1 must be the same cv-qualification as, or greater |
| 2052 | cv-qualification than, cv2; otherwise, the program is ill-formed. */ |
| 2053 | if (related_p && !at_least_as_qualified_p (to, from)) |
| 2054 | maybe_valid_p = false; |
| 2055 | |
| 2056 | /* We try below to treat an invalid reference binding as a bad conversion |
| 2057 | to improve diagnostics, but doing so may cause otherwise unnecessary |
| 2058 | instantiations that can lead to a hard error. So during the first pass |
| 2059 | of overload resolution wherein we shortcut bad conversions, instead just |
| 2060 | produce a special conversion indicating a second pass is necessary if |
| 2061 | there's no strictly viable candidate. */ |
| 2062 | if (!maybe_valid_p && (flags & LOOKUP_SHORTCUT_BAD_CONVS)) |
| 2063 | { |
| 2064 | if (bad_direct_conv) |
| 2065 | return bad_direct_conv; |
| 2066 | |
| 2067 | conv = alloc_conversion (kind: ck_deferred_bad); |
| 2068 | conv->bad_p = true; |
| 2069 | return conv; |
| 2070 | } |
| 2071 | |
| 2072 | /* We're generating a temporary now, but don't bind any more in the |
| 2073 | conversion (specifically, don't slice the temporary returned by a |
| 2074 | conversion operator). */ |
| 2075 | flags |= LOOKUP_NO_TEMP_BIND; |
| 2076 | |
| 2077 | /* Core issue 899: When [copy-]initializing a temporary to be bound |
| 2078 | to the first parameter of a copy constructor (12.8) called with |
| 2079 | a single argument in the context of direct-initialization, |
| 2080 | explicit conversion functions are also considered. |
| 2081 | |
| 2082 | So don't set LOOKUP_ONLYCONVERTING in that case. */ |
| 2083 | if (!(flags & LOOKUP_COPY_PARM)) |
| 2084 | flags |= LOOKUP_ONLYCONVERTING; |
| 2085 | |
| 2086 | if (!conv) |
| 2087 | conv = implicit_conversion (to, from, expr, c_cast_p, |
| 2088 | flags, complain); |
| 2089 | if (!conv) |
| 2090 | return bad_direct_conv ? bad_direct_conv : nullptr; |
| 2091 | |
| 2092 | if (conv->user_conv_p) |
| 2093 | { |
| 2094 | if (copy_list_init) |
| 2095 | /* Remember this was copy-list-initialization. */ |
| 2096 | conv->need_temporary_p = true; |
| 2097 | |
| 2098 | /* If initializing the temporary used a conversion function, |
| 2099 | recalculate the second conversion sequence. */ |
| 2100 | for (conversion *t = conv; t; t = next_conversion (conv: t)) |
| 2101 | if (t->kind == ck_user |
| 2102 | && c_cast_p && !maybe_valid_p) |
| 2103 | { |
| 2104 | if (complain & tf_warning) |
| 2105 | warning (OPT_Wcast_user_defined, |
| 2106 | "casting %qT to %qT does not use %qD" , |
| 2107 | from, rto, t->cand->fn); |
| 2108 | /* Don't let recalculation try to make this valid. */ |
| 2109 | break; |
| 2110 | } |
| 2111 | else if (t->kind == ck_user |
| 2112 | && DECL_CONV_FN_P (t->cand->fn)) |
| 2113 | { |
| 2114 | tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn)); |
| 2115 | /* A prvalue of non-class type is cv-unqualified. */ |
| 2116 | if (!TYPE_REF_P (ftype) && !CLASS_TYPE_P (ftype)) |
| 2117 | ftype = cv_unqualified (ftype); |
| 2118 | int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND; |
| 2119 | conversion *new_second |
| 2120 | = reference_binding (rto, rfrom: ftype, NULL_TREE, c_cast_p, |
| 2121 | flags: sflags, complain); |
| 2122 | if (!new_second) |
| 2123 | return bad_direct_conv ? bad_direct_conv : nullptr; |
| 2124 | conv = merge_conversion_sequences (t, new_second); |
| 2125 | gcc_assert (maybe_valid_p || conv->bad_p); |
| 2126 | return conv; |
| 2127 | } |
| 2128 | } |
| 2129 | |
| 2130 | conv = build_conv (code: ck_ref_bind, type: rto, from: conv); |
| 2131 | /* This reference binding, unlike those above, requires the |
| 2132 | creation of a temporary. */ |
| 2133 | conv->need_temporary_p = true; |
| 2134 | conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto); |
| 2135 | conv->bad_p |= !maybe_valid_p; |
| 2136 | |
| 2137 | return conv; |
| 2138 | } |
| 2139 | |
| 2140 | /* Returns the implicit conversion sequence (see [over.ics]) from type |
| 2141 | FROM to type TO. The optional expression EXPR may affect the |
| 2142 | conversion. FLAGS are the usual overloading flags. If C_CAST_P is |
| 2143 | true, this conversion is coming from a C-style cast. */ |
| 2144 | |
| 2145 | static conversion * |
| 2146 | implicit_conversion (tree to, tree from, tree expr, bool c_cast_p, |
| 2147 | int flags, tsubst_flags_t complain) |
| 2148 | { |
| 2149 | conversion *conv; |
| 2150 | |
| 2151 | if (from == error_mark_node || to == error_mark_node |
| 2152 | || expr == error_mark_node) |
| 2153 | return NULL; |
| 2154 | |
| 2155 | /* Other flags only apply to the primary function in overload |
| 2156 | resolution, or after we've chosen one. */ |
| 2157 | flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM |
| 2158 | |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_NO_NARROWING |
| 2159 | |LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL|LOOKUP_SHORTCUT_BAD_CONVS); |
| 2160 | |
| 2161 | /* FIXME: actually we don't want warnings either, but we can't just |
| 2162 | have 'complain &= ~(tf_warning|tf_error)' because it would cause |
| 2163 | the regression of, eg, g++.old-deja/g++.benjamin/16077.C. |
| 2164 | We really ought not to issue that warning until we've committed |
| 2165 | to that conversion. */ |
| 2166 | complain &= ~tf_error; |
| 2167 | |
| 2168 | /* Call reshape_init early to remove redundant braces. */ |
| 2169 | if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr) && CLASS_TYPE_P (to)) |
| 2170 | { |
| 2171 | to = complete_type (to); |
| 2172 | if (!COMPLETE_TYPE_P (to)) |
| 2173 | return nullptr; |
| 2174 | if (!CLASSTYPE_NON_AGGREGATE (to)) |
| 2175 | { |
| 2176 | expr = reshape_init (to, expr, complain); |
| 2177 | if (expr == error_mark_node) |
| 2178 | return nullptr; |
| 2179 | from = TREE_TYPE (expr); |
| 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | /* An argument should have gone through convert_from_reference. */ |
| 2184 | gcc_checking_assert (!expr || !TYPE_REF_P (from)); |
| 2185 | |
| 2186 | if (TYPE_REF_P (to)) |
| 2187 | conv = reference_binding (rto: to, rfrom: from, expr, c_cast_p, flags, complain); |
| 2188 | else |
| 2189 | conv = standard_conversion (to, from, expr, c_cast_p, flags, complain); |
| 2190 | |
| 2191 | if (conv) |
| 2192 | return conv; |
| 2193 | |
| 2194 | if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr)) |
| 2195 | { |
| 2196 | if (is_std_init_list (to) && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr)) |
| 2197 | return build_list_conv (type: to, ctor: expr, flags, complain); |
| 2198 | |
| 2199 | /* As an extension, allow list-initialization of _Complex. */ |
| 2200 | if (TREE_CODE (to) == COMPLEX_TYPE |
| 2201 | && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr)) |
| 2202 | { |
| 2203 | conv = build_complex_conv (type: to, ctor: expr, flags, complain); |
| 2204 | if (conv) |
| 2205 | return conv; |
| 2206 | } |
| 2207 | |
| 2208 | /* Allow conversion from an initializer-list with one element to a |
| 2209 | scalar type. */ |
| 2210 | if (SCALAR_TYPE_P (to)) |
| 2211 | { |
| 2212 | int nelts = CONSTRUCTOR_NELTS (expr); |
| 2213 | tree elt; |
| 2214 | |
| 2215 | if (nelts == 0) |
| 2216 | elt = build_value_init (to, tf_none); |
| 2217 | else if (nelts == 1 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr)) |
| 2218 | elt = CONSTRUCTOR_ELT (expr, 0)->value; |
| 2219 | else |
| 2220 | elt = error_mark_node; |
| 2221 | |
| 2222 | conv = implicit_conversion (to, TREE_TYPE (elt), expr: elt, |
| 2223 | c_cast_p, flags, complain); |
| 2224 | if (conv) |
| 2225 | { |
| 2226 | conv->check_narrowing = true; |
| 2227 | if (BRACE_ENCLOSED_INITIALIZER_P (elt)) |
| 2228 | /* Too many levels of braces, i.e. '{{1}}'. */ |
| 2229 | conv->bad_p = true; |
| 2230 | return conv; |
| 2231 | } |
| 2232 | } |
| 2233 | else if (TREE_CODE (to) == ARRAY_TYPE) |
| 2234 | return build_array_conv (type: to, ctor: expr, flags, complain); |
| 2235 | } |
| 2236 | |
| 2237 | if (expr != NULL_TREE |
| 2238 | && (MAYBE_CLASS_TYPE_P (from) |
| 2239 | || MAYBE_CLASS_TYPE_P (to)) |
| 2240 | && (flags & LOOKUP_NO_CONVERSION) == 0) |
| 2241 | { |
| 2242 | struct z_candidate *cand; |
| 2243 | |
| 2244 | if (CLASS_TYPE_P (to) |
| 2245 | && BRACE_ENCLOSED_INITIALIZER_P (expr) |
| 2246 | && !CLASSTYPE_NON_AGGREGATE (complete_type (to))) |
| 2247 | return build_aggr_conv (type: to, ctor: expr, flags, complain); |
| 2248 | |
| 2249 | cand = build_user_type_conversion_1 (to, expr, flags, complain); |
| 2250 | if (cand) |
| 2251 | { |
| 2252 | if (BRACE_ENCLOSED_INITIALIZER_P (expr) |
| 2253 | && CONSTRUCTOR_NELTS (expr) == 1 |
| 2254 | && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr) |
| 2255 | && !is_list_ctor (cand->fn)) |
| 2256 | { |
| 2257 | /* "If C is not an initializer-list constructor and the |
| 2258 | initializer list has a single element of type cv U, where U is |
| 2259 | X or a class derived from X, the implicit conversion sequence |
| 2260 | has Exact Match rank if U is X, or Conversion rank if U is |
| 2261 | derived from X." */ |
| 2262 | tree elt = CONSTRUCTOR_ELT (expr, 0)->value; |
| 2263 | tree elttype = TREE_TYPE (elt); |
| 2264 | if (reference_related_p (t1: to, t2: elttype)) |
| 2265 | return implicit_conversion (to, from: elttype, expr: elt, |
| 2266 | c_cast_p, flags, complain); |
| 2267 | } |
| 2268 | conv = cand->second_conv; |
| 2269 | } |
| 2270 | |
| 2271 | /* We used to try to bind a reference to a temporary here, but that |
| 2272 | is now handled after the recursive call to this function at the end |
| 2273 | of reference_binding. */ |
| 2274 | return conv; |
| 2275 | } |
| 2276 | |
| 2277 | return NULL; |
| 2278 | } |
| 2279 | |
| 2280 | /* Like implicit_conversion, but return NULL if the conversion is bad. |
| 2281 | |
| 2282 | This is not static so that check_non_deducible_conversion can call it within |
| 2283 | add_template_candidate_real as part of overload resolution; it should not be |
| 2284 | called outside of overload resolution. */ |
| 2285 | |
| 2286 | conversion * |
| 2287 | good_conversion (tree to, tree from, tree expr, |
| 2288 | int flags, tsubst_flags_t complain) |
| 2289 | { |
| 2290 | conversion *c = implicit_conversion (to, from, expr, /*cast*/c_cast_p: false, |
| 2291 | flags, complain); |
| 2292 | if (c && c->bad_p) |
| 2293 | c = NULL; |
| 2294 | return c; |
| 2295 | } |
| 2296 | |
| 2297 | /* Add a new entry to the list of candidates. Used by the add_*_candidate |
| 2298 | functions. ARGS will not be changed until a single candidate is |
| 2299 | selected. */ |
| 2300 | |
| 2301 | static struct z_candidate * |
| 2302 | add_candidate (struct z_candidate **candidates, |
| 2303 | tree fn, tree first_arg, const vec<tree, va_gc> *args, |
| 2304 | size_t num_convs, conversion **convs, |
| 2305 | tree access_path, tree conversion_path, |
| 2306 | int viable, struct rejection_reason *reason, |
| 2307 | int flags) |
| 2308 | { |
| 2309 | struct z_candidate *cand = (struct z_candidate *) |
| 2310 | conversion_obstack_alloc (n: sizeof (struct z_candidate)); |
| 2311 | |
| 2312 | cand->fn = fn; |
| 2313 | cand->first_arg = first_arg; |
| 2314 | cand->args = args; |
| 2315 | cand->convs = convs; |
| 2316 | cand->num_convs = num_convs; |
| 2317 | cand->access_path = access_path; |
| 2318 | cand->conversion_path = conversion_path; |
| 2319 | cand->viable = viable; |
| 2320 | cand->reason = reason; |
| 2321 | cand->next = *candidates; |
| 2322 | cand->flags = flags; |
| 2323 | *candidates = cand; |
| 2324 | |
| 2325 | if (convs && cand->reversed ()) |
| 2326 | /* Swap the conversions for comparison in joust; we'll swap them back |
| 2327 | before build_over_call. */ |
| 2328 | std::swap (a&: convs[0], b&: convs[1]); |
| 2329 | |
| 2330 | return cand; |
| 2331 | } |
| 2332 | |
| 2333 | /* FN is a function from the overload set that we outright didn't even |
| 2334 | consider (for some reason); add it to the list as an non-viable "ignored" |
| 2335 | candidate. */ |
| 2336 | |
| 2337 | static z_candidate * |
| 2338 | add_ignored_candidate (z_candidate **candidates, tree fn) |
| 2339 | { |
| 2340 | /* No need to dynamically allocate these. */ |
| 2341 | static const rejection_reason reason_ignored = { .code: rr_ignored, .u: {} }; |
| 2342 | |
| 2343 | struct z_candidate *cand = (struct z_candidate *) |
| 2344 | conversion_obstack_alloc (n: sizeof (struct z_candidate)); |
| 2345 | |
| 2346 | cand->fn = fn; |
| 2347 | cand->reason = const_cast<rejection_reason *> (&reason_ignored); |
| 2348 | cand->next = *candidates; |
| 2349 | *candidates = cand; |
| 2350 | |
| 2351 | return cand; |
| 2352 | } |
| 2353 | |
| 2354 | /* True iff CAND is a candidate added by add_ignored_candidate. */ |
| 2355 | |
| 2356 | static bool |
| 2357 | ignored_candidate_p (const z_candidate *cand) |
| 2358 | { |
| 2359 | return cand->reason && cand->reason->code == rr_ignored; |
| 2360 | } |
| 2361 | |
| 2362 | /* Return the number of remaining arguments in the parameter list |
| 2363 | beginning with ARG. */ |
| 2364 | |
| 2365 | int |
| 2366 | remaining_arguments (tree arg) |
| 2367 | { |
| 2368 | int n; |
| 2369 | |
| 2370 | for (n = 0; arg != NULL_TREE && arg != void_list_node; |
| 2371 | arg = TREE_CHAIN (arg)) |
| 2372 | n++; |
| 2373 | |
| 2374 | return n; |
| 2375 | } |
| 2376 | |
| 2377 | /* [over.match.copy]: When initializing a temporary object (12.2) to be bound |
| 2378 | to the first parameter of a constructor where the parameter is of type |
| 2379 | "reference to possibly cv-qualified T" and the constructor is called with a |
| 2380 | single argument in the context of direct-initialization of an object of type |
| 2381 | "cv2 T", explicit conversion functions are also considered. |
| 2382 | |
| 2383 | So set LOOKUP_COPY_PARM to let reference_binding know that |
| 2384 | it's being called in that context. */ |
| 2385 | |
| 2386 | int |
| 2387 | conv_flags (int i, int nargs, tree fn, tree arg, int flags) |
| 2388 | { |
| 2389 | int lflags = flags; |
| 2390 | tree t; |
| 2391 | if (i == 0 && nargs == 1 && DECL_CONSTRUCTOR_P (fn) |
| 2392 | && (t = FUNCTION_FIRST_USER_PARMTYPE (fn)) |
| 2393 | && (same_type_ignoring_top_level_qualifiers_p |
| 2394 | (non_reference (TREE_VALUE (t)), DECL_CONTEXT (fn)))) |
| 2395 | { |
| 2396 | if (!(flags & LOOKUP_ONLYCONVERTING)) |
| 2397 | lflags |= LOOKUP_COPY_PARM; |
| 2398 | if ((flags & LOOKUP_LIST_INIT_CTOR) |
| 2399 | && BRACE_ENCLOSED_INITIALIZER_P (arg)) |
| 2400 | lflags |= LOOKUP_NO_CONVERSION; |
| 2401 | } |
| 2402 | else |
| 2403 | lflags |= LOOKUP_ONLYCONVERTING; |
| 2404 | |
| 2405 | return lflags; |
| 2406 | } |
| 2407 | |
| 2408 | /* Build an appropriate 'this' conversion for the method FN and class |
| 2409 | type CTYPE from the value ARG (having type ARGTYPE) to the type PARMTYPE. |
| 2410 | This function modifies PARMTYPE, ARGTYPE and ARG. */ |
| 2411 | |
| 2412 | static conversion * |
| 2413 | build_this_conversion (tree fn, tree ctype, |
| 2414 | tree& parmtype, tree& argtype, tree& arg, |
| 2415 | int flags, tsubst_flags_t complain) |
| 2416 | { |
| 2417 | gcc_assert (DECL_IOBJ_MEMBER_FUNCTION_P (fn) |
| 2418 | && !DECL_CONSTRUCTOR_P (fn)); |
| 2419 | |
| 2420 | /* The type of the implicit object parameter ('this') for |
| 2421 | overload resolution is not always the same as for the |
| 2422 | function itself; conversion functions are considered to |
| 2423 | be members of the class being converted, and functions |
| 2424 | introduced by a using-declaration are considered to be |
| 2425 | members of the class that uses them. |
| 2426 | |
| 2427 | Since build_over_call ignores the ICS for the `this' |
| 2428 | parameter, we can just change the parm type. */ |
| 2429 | parmtype = cp_build_qualified_type (ctype, |
| 2430 | cp_type_quals (TREE_TYPE (parmtype))); |
| 2431 | bool this_p = true; |
| 2432 | if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn))) |
| 2433 | { |
| 2434 | /* If the function has a ref-qualifier, the implicit |
| 2435 | object parameter has reference type. */ |
| 2436 | bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn)); |
| 2437 | parmtype = cp_build_reference_type (parmtype, rv); |
| 2438 | /* The special handling of 'this' conversions in compare_ics |
| 2439 | does not apply if there is a ref-qualifier. */ |
| 2440 | this_p = false; |
| 2441 | } |
| 2442 | else |
| 2443 | { |
| 2444 | parmtype = build_pointer_type (parmtype); |
| 2445 | /* We don't use build_this here because we don't want to |
| 2446 | capture the object argument until we've chosen a |
| 2447 | non-static member function. */ |
| 2448 | arg = build_address (arg); |
| 2449 | argtype = lvalue_type (arg); |
| 2450 | } |
| 2451 | flags |= LOOKUP_ONLYCONVERTING; |
| 2452 | conversion *t = implicit_conversion (to: parmtype, from: argtype, expr: arg, |
| 2453 | /*c_cast_p=*/false, flags, complain); |
| 2454 | t->this_p = this_p; |
| 2455 | return t; |
| 2456 | } |
| 2457 | |
| 2458 | /* Create an overload candidate for the function or method FN called |
| 2459 | with the argument list FIRST_ARG/ARGS and add it to CANDIDATES. |
| 2460 | FLAGS is passed on to implicit_conversion. |
| 2461 | |
| 2462 | This does not change ARGS. |
| 2463 | |
| 2464 | CTYPE, if non-NULL, is the type we want to pretend this function |
| 2465 | comes from for purposes of overload resolution. |
| 2466 | |
| 2467 | SHORTCUT_BAD_CONVS controls how we handle "bad" argument conversions. |
| 2468 | If true, we stop computing conversions upon seeing the first bad |
| 2469 | conversion. This is used by add_candidates to avoid computing |
| 2470 | more conversions than necessary in the presence of a strictly viable |
| 2471 | candidate, while preserving the defacto behavior of overload resolution |
| 2472 | when it turns out there are only non-strictly viable candidates. */ |
| 2473 | |
| 2474 | static struct z_candidate * |
| 2475 | add_function_candidate (struct z_candidate **candidates, |
| 2476 | tree fn, tree ctype, tree first_arg, |
| 2477 | const vec<tree, va_gc> *args, tree access_path, |
| 2478 | tree conversion_path, int flags, |
| 2479 | conversion **convs, |
| 2480 | bool shortcut_bad_convs, |
| 2481 | tsubst_flags_t complain) |
| 2482 | { |
| 2483 | tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
| 2484 | int i, len; |
| 2485 | tree parmnode; |
| 2486 | tree orig_first_arg = first_arg; |
| 2487 | int skip; |
| 2488 | int viable = 1; |
| 2489 | struct rejection_reason *reason = NULL; |
| 2490 | |
| 2491 | /* The `this', `in_chrg' and VTT arguments to constructors are not |
| 2492 | considered in overload resolution. */ |
| 2493 | if (DECL_CONSTRUCTOR_P (fn)) |
| 2494 | { |
| 2495 | if (ctor_omit_inherited_parms (fn)) |
| 2496 | /* Bring back parameters omitted from an inherited ctor. */ |
| 2497 | parmlist = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn)); |
| 2498 | else |
| 2499 | parmlist = skip_artificial_parms_for (fn, parmlist); |
| 2500 | skip = num_artificial_parms_for (fn); |
| 2501 | if (skip > 0 && first_arg != NULL_TREE) |
| 2502 | { |
| 2503 | --skip; |
| 2504 | first_arg = NULL_TREE; |
| 2505 | } |
| 2506 | } |
| 2507 | else |
| 2508 | skip = 0; |
| 2509 | |
| 2510 | len = vec_safe_length (v: args) - skip + (first_arg != NULL_TREE ? 1 : 0); |
| 2511 | if (!convs) |
| 2512 | convs = alloc_conversions (n: len); |
| 2513 | |
| 2514 | /* 13.3.2 - Viable functions [over.match.viable] |
| 2515 | First, to be a viable function, a candidate function shall have enough |
| 2516 | parameters to agree in number with the arguments in the list. |
| 2517 | |
| 2518 | We need to check this first; otherwise, checking the ICSes might cause |
| 2519 | us to produce an ill-formed template instantiation. */ |
| 2520 | |
| 2521 | parmnode = parmlist; |
| 2522 | for (i = 0; i < len; ++i) |
| 2523 | { |
| 2524 | if (parmnode == NULL_TREE || parmnode == void_list_node) |
| 2525 | break; |
| 2526 | parmnode = TREE_CHAIN (parmnode); |
| 2527 | } |
| 2528 | |
| 2529 | if ((i < len && parmnode) |
| 2530 | || !sufficient_parms_p (parmlist: parmnode)) |
| 2531 | { |
| 2532 | int remaining = remaining_arguments (arg: parmnode); |
| 2533 | viable = 0; |
| 2534 | reason = arity_rejection (first_arg, expected: i + remaining, actual: len); |
| 2535 | } |
| 2536 | |
| 2537 | /* An inherited constructor (12.6.3 [class.inhctor.init]) that has a first |
| 2538 | parameter of type "reference to cv C" (including such a constructor |
| 2539 | instantiated from a template) is excluded from the set of candidate |
| 2540 | functions when used to construct an object of type D with an argument list |
| 2541 | containing a single argument if C is reference-related to D. */ |
| 2542 | if (viable && len == 1 && parmlist && DECL_CONSTRUCTOR_P (fn) |
| 2543 | && flag_new_inheriting_ctors |
| 2544 | && DECL_INHERITED_CTOR (fn)) |
| 2545 | { |
| 2546 | tree ptype = non_reference (TREE_VALUE (parmlist)); |
| 2547 | tree dtype = DECL_CONTEXT (fn); |
| 2548 | tree btype = DECL_INHERITED_CTOR_BASE (fn); |
| 2549 | if (reference_related_p (t1: ptype, t2: dtype) |
| 2550 | && reference_related_p (t1: btype, t2: ptype)) |
| 2551 | { |
| 2552 | viable = false; |
| 2553 | reason = inherited_ctor_rejection (); |
| 2554 | } |
| 2555 | } |
| 2556 | |
| 2557 | /* Second, for a function to be viable, its constraints must be |
| 2558 | satisfied. */ |
| 2559 | if (flag_concepts && viable && !constraints_satisfied_p (fn)) |
| 2560 | { |
| 2561 | reason = constraint_failure (); |
| 2562 | viable = false; |
| 2563 | } |
| 2564 | |
| 2565 | /* When looking for a function from a subobject from an implicit |
| 2566 | copy/move constructor/operator=, don't consider anything that takes (a |
| 2567 | reference to) an unrelated type. See c++/44909 and core 1092. */ |
| 2568 | if (viable && parmlist && (flags & LOOKUP_DEFAULTED)) |
| 2569 | { |
| 2570 | if (DECL_CONSTRUCTOR_P (fn)) |
| 2571 | i = 1; |
| 2572 | else if (DECL_ASSIGNMENT_OPERATOR_P (fn) |
| 2573 | && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)) |
| 2574 | i = 2; |
| 2575 | else |
| 2576 | i = 0; |
| 2577 | if (i && len == i) |
| 2578 | { |
| 2579 | parmnode = chain_index (i-1, parmlist); |
| 2580 | if (!reference_related_p (t1: non_reference (TREE_VALUE (parmnode)), |
| 2581 | t2: ctype)) |
| 2582 | viable = 0; |
| 2583 | } |
| 2584 | |
| 2585 | /* This only applies at the top level. */ |
| 2586 | flags &= ~LOOKUP_DEFAULTED; |
| 2587 | } |
| 2588 | |
| 2589 | if (! viable) |
| 2590 | goto out; |
| 2591 | |
| 2592 | if (shortcut_bad_convs) |
| 2593 | flags |= LOOKUP_SHORTCUT_BAD_CONVS; |
| 2594 | else |
| 2595 | flags &= ~LOOKUP_SHORTCUT_BAD_CONVS; |
| 2596 | |
| 2597 | /* Third, for F to be a viable function, there shall exist for each |
| 2598 | argument an implicit conversion sequence that converts that argument |
| 2599 | to the corresponding parameter of F. */ |
| 2600 | |
| 2601 | parmnode = parmlist; |
| 2602 | |
| 2603 | for (i = 0; i < len; ++i) |
| 2604 | { |
| 2605 | tree argtype, to_type; |
| 2606 | tree arg; |
| 2607 | |
| 2608 | if (parmnode == void_list_node) |
| 2609 | break; |
| 2610 | |
| 2611 | if (convs[i]) |
| 2612 | { |
| 2613 | /* Already set during deduction. */ |
| 2614 | parmnode = TREE_CHAIN (parmnode); |
| 2615 | continue; |
| 2616 | } |
| 2617 | |
| 2618 | if (i == 0 && first_arg != NULL_TREE) |
| 2619 | arg = first_arg; |
| 2620 | else |
| 2621 | arg = CONST_CAST_TREE ( |
| 2622 | (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]); |
| 2623 | argtype = lvalue_type (arg); |
| 2624 | |
| 2625 | conversion *t; |
| 2626 | if (parmnode) |
| 2627 | { |
| 2628 | tree parmtype = TREE_VALUE (parmnode); |
| 2629 | if (i == 0 |
| 2630 | && DECL_IOBJ_MEMBER_FUNCTION_P (fn) |
| 2631 | && !DECL_CONSTRUCTOR_P (fn)) |
| 2632 | t = build_this_conversion (fn, ctype, parmtype, argtype, arg, |
| 2633 | flags, complain); |
| 2634 | else |
| 2635 | { |
| 2636 | int lflags = conv_flags (i, nargs: len-skip, fn, arg, flags); |
| 2637 | t = implicit_conversion (to: parmtype, from: argtype, expr: arg, |
| 2638 | /*c_cast_p=*/false, flags: lflags, complain); |
| 2639 | } |
| 2640 | to_type = parmtype; |
| 2641 | parmnode = TREE_CHAIN (parmnode); |
| 2642 | } |
| 2643 | else |
| 2644 | { |
| 2645 | t = build_identity_conv (type: argtype, expr: arg); |
| 2646 | t->ellipsis_p = true; |
| 2647 | to_type = argtype; |
| 2648 | } |
| 2649 | |
| 2650 | convs[i] = t; |
| 2651 | if (! t) |
| 2652 | { |
| 2653 | viable = 0; |
| 2654 | reason = arg_conversion_rejection (first_arg, n_arg: i, from: argtype, to: to_type, |
| 2655 | EXPR_LOCATION (arg)); |
| 2656 | break; |
| 2657 | } |
| 2658 | |
| 2659 | if (t->bad_p) |
| 2660 | { |
| 2661 | viable = -1; |
| 2662 | reason = bad_arg_conversion_rejection (first_arg, n_arg: i, from: arg, to: to_type, |
| 2663 | EXPR_LOCATION (arg)); |
| 2664 | if (shortcut_bad_convs) |
| 2665 | break; |
| 2666 | } |
| 2667 | } |
| 2668 | |
| 2669 | out: |
| 2670 | return add_candidate (candidates, fn, first_arg: orig_first_arg, args, num_convs: len, convs, |
| 2671 | access_path, conversion_path, viable, reason, flags); |
| 2672 | } |
| 2673 | |
| 2674 | /* Create an overload candidate for the conversion function FN which will |
| 2675 | be invoked for expression OBJ, producing a pointer-to-function which |
| 2676 | will in turn be called with the argument list FIRST_ARG/ARGLIST, |
| 2677 | and add it to CANDIDATES. This does not change ARGLIST. FLAGS is |
| 2678 | passed on to implicit_conversion. |
| 2679 | |
| 2680 | Actually, we don't really care about FN; we care about the type it |
| 2681 | converts to. There may be multiple conversion functions that will |
| 2682 | convert to that type, and we rely on build_user_type_conversion_1 to |
| 2683 | choose the best one; so when we create our candidate, we record the type |
| 2684 | instead of the function. */ |
| 2685 | |
| 2686 | static struct z_candidate * |
| 2687 | add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj, |
| 2688 | const vec<tree, va_gc> *arglist, |
| 2689 | tree access_path, tree conversion_path, |
| 2690 | tsubst_flags_t complain) |
| 2691 | { |
| 2692 | tree totype = TREE_TYPE (TREE_TYPE (fn)); |
| 2693 | int i, len, viable, flags; |
| 2694 | tree parmlist, parmnode; |
| 2695 | conversion **convs; |
| 2696 | struct rejection_reason *reason; |
| 2697 | |
| 2698 | for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; ) |
| 2699 | parmlist = TREE_TYPE (parmlist); |
| 2700 | parmlist = TYPE_ARG_TYPES (parmlist); |
| 2701 | |
| 2702 | len = vec_safe_length (v: arglist) + 1; |
| 2703 | convs = alloc_conversions (n: len); |
| 2704 | parmnode = parmlist; |
| 2705 | viable = 1; |
| 2706 | flags = LOOKUP_IMPLICIT; |
| 2707 | reason = NULL; |
| 2708 | |
| 2709 | /* Don't bother looking up the same type twice. */ |
| 2710 | if (*candidates && (*candidates)->fn == totype) |
| 2711 | return NULL; |
| 2712 | |
| 2713 | if (!constraints_satisfied_p (fn)) |
| 2714 | { |
| 2715 | reason = constraint_failure (); |
| 2716 | viable = 0; |
| 2717 | return add_candidate (candidates, fn, first_arg: obj, args: arglist, num_convs: len, convs, |
| 2718 | access_path, conversion_path, viable, reason, flags); |
| 2719 | } |
| 2720 | |
| 2721 | for (i = 0; i < len; ++i) |
| 2722 | { |
| 2723 | tree arg, argtype, convert_type = NULL_TREE; |
| 2724 | conversion *t; |
| 2725 | |
| 2726 | if (i == 0) |
| 2727 | arg = obj; |
| 2728 | else |
| 2729 | arg = (*arglist)[i - 1]; |
| 2730 | argtype = lvalue_type (arg); |
| 2731 | |
| 2732 | if (i == 0) |
| 2733 | { |
| 2734 | t = build_identity_conv (type: argtype, NULL_TREE); |
| 2735 | t = build_conv (code: ck_user, type: totype, from: t); |
| 2736 | /* Leave the 'cand' field null; we'll figure out the conversion in |
| 2737 | convert_like if this candidate is chosen. */ |
| 2738 | convert_type = totype; |
| 2739 | } |
| 2740 | else if (parmnode == void_list_node) |
| 2741 | break; |
| 2742 | else if (parmnode) |
| 2743 | { |
| 2744 | t = implicit_conversion (TREE_VALUE (parmnode), from: argtype, expr: arg, |
| 2745 | /*c_cast_p=*/false, flags, complain); |
| 2746 | convert_type = TREE_VALUE (parmnode); |
| 2747 | } |
| 2748 | else |
| 2749 | { |
| 2750 | t = build_identity_conv (type: argtype, expr: arg); |
| 2751 | t->ellipsis_p = true; |
| 2752 | convert_type = argtype; |
| 2753 | } |
| 2754 | |
| 2755 | convs[i] = t; |
| 2756 | if (! t) |
| 2757 | break; |
| 2758 | |
| 2759 | if (t->bad_p) |
| 2760 | { |
| 2761 | viable = -1; |
| 2762 | reason = bad_arg_conversion_rejection (NULL_TREE, n_arg: i, from: arg, to: convert_type, |
| 2763 | EXPR_LOCATION (arg)); |
| 2764 | } |
| 2765 | |
| 2766 | if (i == 0) |
| 2767 | continue; |
| 2768 | |
| 2769 | if (parmnode) |
| 2770 | parmnode = TREE_CHAIN (parmnode); |
| 2771 | } |
| 2772 | |
| 2773 | if (i < len |
| 2774 | || ! sufficient_parms_p (parmlist: parmnode)) |
| 2775 | { |
| 2776 | int remaining = remaining_arguments (arg: parmnode); |
| 2777 | viable = 0; |
| 2778 | reason = arity_rejection (NULL_TREE, expected: i + remaining, actual: len); |
| 2779 | } |
| 2780 | |
| 2781 | return add_candidate (candidates, fn: totype, first_arg: obj, args: arglist, num_convs: len, convs, |
| 2782 | access_path, conversion_path, viable, reason, flags); |
| 2783 | } |
| 2784 | |
| 2785 | static void |
| 2786 | build_builtin_candidate (struct z_candidate **candidates, tree fnname, |
| 2787 | tree type1, tree type2, const vec<tree,va_gc> &args, |
| 2788 | tree *argtypes, int flags, tsubst_flags_t complain) |
| 2789 | { |
| 2790 | conversion *t; |
| 2791 | conversion **convs; |
| 2792 | size_t num_convs; |
| 2793 | int viable = 1; |
| 2794 | tree types[2]; |
| 2795 | struct rejection_reason *reason = NULL; |
| 2796 | |
| 2797 | types[0] = type1; |
| 2798 | types[1] = type2; |
| 2799 | |
| 2800 | num_convs = args.length (); |
| 2801 | convs = alloc_conversions (n: num_convs); |
| 2802 | |
| 2803 | /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit |
| 2804 | conversion ops are allowed. We handle that here by just checking for |
| 2805 | boolean_type_node because other operators don't ask for it. COND_EXPR |
| 2806 | also does contextual conversion to bool for the first operand, but we |
| 2807 | handle that in build_conditional_expr, and type1 here is operand 2. */ |
| 2808 | if (type1 != boolean_type_node) |
| 2809 | flags |= LOOKUP_ONLYCONVERTING; |
| 2810 | |
| 2811 | for (unsigned i = 0; i < 2 && i < num_convs; ++i) |
| 2812 | { |
| 2813 | t = implicit_conversion (to: types[i], from: argtypes[i], expr: args[i], |
| 2814 | /*c_cast_p=*/false, flags, complain); |
| 2815 | if (! t) |
| 2816 | { |
| 2817 | viable = 0; |
| 2818 | /* We need something for printing the candidate. */ |
| 2819 | t = build_identity_conv (type: types[i], NULL_TREE); |
| 2820 | reason = arg_conversion_rejection (NULL_TREE, n_arg: i, from: argtypes[i], |
| 2821 | to: types[i], EXPR_LOCATION (args[i])); |
| 2822 | } |
| 2823 | else if (t->bad_p) |
| 2824 | { |
| 2825 | viable = 0; |
| 2826 | reason = bad_arg_conversion_rejection (NULL_TREE, n_arg: i, from: args[i], |
| 2827 | to: types[i], |
| 2828 | EXPR_LOCATION (args[i])); |
| 2829 | } |
| 2830 | convs[i] = t; |
| 2831 | } |
| 2832 | |
| 2833 | /* For COND_EXPR we rearranged the arguments; undo that now. */ |
| 2834 | if (num_convs == 3) |
| 2835 | { |
| 2836 | convs[2] = convs[1]; |
| 2837 | convs[1] = convs[0]; |
| 2838 | t = implicit_conversion (boolean_type_node, from: argtypes[2], expr: args[2], |
| 2839 | /*c_cast_p=*/false, flags, |
| 2840 | complain); |
| 2841 | if (t) |
| 2842 | convs[0] = t; |
| 2843 | else |
| 2844 | { |
| 2845 | viable = 0; |
| 2846 | reason = arg_conversion_rejection (NULL_TREE, n_arg: 0, from: argtypes[2], |
| 2847 | boolean_type_node, |
| 2848 | EXPR_LOCATION (args[2])); |
| 2849 | } |
| 2850 | } |
| 2851 | |
| 2852 | add_candidate (candidates, fn: fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL, |
| 2853 | num_convs, convs, |
| 2854 | /*access_path=*/NULL_TREE, |
| 2855 | /*conversion_path=*/NULL_TREE, |
| 2856 | viable, reason, flags); |
| 2857 | } |
| 2858 | |
| 2859 | static bool |
| 2860 | is_complete (tree t) |
| 2861 | { |
| 2862 | return COMPLETE_TYPE_P (complete_type (t)); |
| 2863 | } |
| 2864 | |
| 2865 | /* Returns nonzero if TYPE is a promoted arithmetic type. */ |
| 2866 | |
| 2867 | static bool |
| 2868 | promoted_arithmetic_type_p (tree type) |
| 2869 | { |
| 2870 | /* [over.built] |
| 2871 | |
| 2872 | In this section, the term promoted integral type is used to refer |
| 2873 | to those integral types which are preserved by integral promotion |
| 2874 | (including e.g. int and long but excluding e.g. char). |
| 2875 | Similarly, the term promoted arithmetic type refers to promoted |
| 2876 | integral types plus floating types. */ |
| 2877 | return ((CP_INTEGRAL_TYPE_P (type) |
| 2878 | && same_type_p (type_promotes_to (type), type)) |
| 2879 | || SCALAR_FLOAT_TYPE_P (type)); |
| 2880 | } |
| 2881 | |
| 2882 | /* Create any builtin operator overload candidates for the operator in |
| 2883 | question given the converted operand types TYPE1 and TYPE2. The other |
| 2884 | args are passed through from add_builtin_candidates to |
| 2885 | build_builtin_candidate. |
| 2886 | |
| 2887 | TYPE1 and TYPE2 may not be permissible, and we must filter them. |
| 2888 | If CODE is requires candidates operands of the same type of the kind |
| 2889 | of which TYPE1 and TYPE2 are, we add both candidates |
| 2890 | CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */ |
| 2891 | |
| 2892 | static void |
| 2893 | add_builtin_candidate (struct z_candidate **candidates, enum tree_code code, |
| 2894 | enum tree_code code2, tree fnname, tree type1, |
| 2895 | tree type2, vec<tree,va_gc> &args, tree *argtypes, |
| 2896 | int flags, tsubst_flags_t complain) |
| 2897 | { |
| 2898 | switch (code) |
| 2899 | { |
| 2900 | case POSTINCREMENT_EXPR: |
| 2901 | case POSTDECREMENT_EXPR: |
| 2902 | args[1] = integer_zero_node; |
| 2903 | type2 = integer_type_node; |
| 2904 | break; |
| 2905 | default: |
| 2906 | break; |
| 2907 | } |
| 2908 | |
| 2909 | switch (code) |
| 2910 | { |
| 2911 | |
| 2912 | /* 4 For every pair (T, VQ), where T is an arithmetic type other than bool, |
| 2913 | and VQ is either volatile or empty, there exist candidate operator |
| 2914 | functions of the form |
| 2915 | VQ T& operator++(VQ T&); |
| 2916 | T operator++(VQ T&, int); |
| 2917 | 5 For every pair (T, VQ), where T is an arithmetic type other than bool, |
| 2918 | and VQ is either volatile or empty, there exist candidate operator |
| 2919 | functions of the form |
| 2920 | VQ T& operator--(VQ T&); |
| 2921 | T operator--(VQ T&, int); |
| 2922 | 6 For every pair (T, VQ), where T is a cv-qualified or cv-unqualified object |
| 2923 | type, and VQ is either volatile or empty, there exist candidate operator |
| 2924 | functions of the form |
| 2925 | T*VQ& operator++(T*VQ&); |
| 2926 | T*VQ& operator--(T*VQ&); |
| 2927 | T* operator++(T*VQ&, int); |
| 2928 | T* operator--(T*VQ&, int); */ |
| 2929 | |
| 2930 | case POSTDECREMENT_EXPR: |
| 2931 | case PREDECREMENT_EXPR: |
| 2932 | if (TREE_CODE (type1) == BOOLEAN_TYPE) |
| 2933 | return; |
| 2934 | /* FALLTHRU */ |
| 2935 | case POSTINCREMENT_EXPR: |
| 2936 | case PREINCREMENT_EXPR: |
| 2937 | /* P0002R1, Remove deprecated operator++(bool) added "other than bool" |
| 2938 | to p4. */ |
| 2939 | if (TREE_CODE (type1) == BOOLEAN_TYPE && cxx_dialect >= cxx17) |
| 2940 | return; |
| 2941 | if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1)) |
| 2942 | { |
| 2943 | type1 = build_reference_type (type1); |
| 2944 | break; |
| 2945 | } |
| 2946 | return; |
| 2947 | |
| 2948 | /* 7 For every cv-qualified or cv-unqualified object type T, there |
| 2949 | exist candidate operator functions of the form |
| 2950 | |
| 2951 | T& operator*(T*); |
| 2952 | |
| 2953 | |
| 2954 | 8 For every function type T that does not have cv-qualifiers or |
| 2955 | a ref-qualifier, there exist candidate operator functions of the form |
| 2956 | T& operator*(T*); */ |
| 2957 | |
| 2958 | case INDIRECT_REF: |
| 2959 | if (TYPE_PTR_P (type1) |
| 2960 | && (TYPE_PTROB_P (type1) |
| 2961 | || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)) |
| 2962 | break; |
| 2963 | return; |
| 2964 | |
| 2965 | /* 9 For every type T, there exist candidate operator functions of the form |
| 2966 | T* operator+(T*); |
| 2967 | |
| 2968 | 10 For every floating-point or promoted integral type T, there exist |
| 2969 | candidate operator functions of the form |
| 2970 | T operator+(T); |
| 2971 | T operator-(T); */ |
| 2972 | |
| 2973 | case UNARY_PLUS_EXPR: /* unary + */ |
| 2974 | if (TYPE_PTR_P (type1)) |
| 2975 | break; |
| 2976 | /* FALLTHRU */ |
| 2977 | case NEGATE_EXPR: |
| 2978 | if (ARITHMETIC_TYPE_P (type1)) |
| 2979 | break; |
| 2980 | return; |
| 2981 | |
| 2982 | /* 11 For every promoted integral type T, there exist candidate operator |
| 2983 | functions of the form |
| 2984 | T operator~(T); */ |
| 2985 | |
| 2986 | case BIT_NOT_EXPR: |
| 2987 | if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1)) |
| 2988 | break; |
| 2989 | return; |
| 2990 | |
| 2991 | /* 12 For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, C1 |
| 2992 | is the same type as C2 or is a derived class of C2, and T is an object |
| 2993 | type or a function type there exist candidate operator functions of the |
| 2994 | form |
| 2995 | CV12 T& operator->*(CV1 C1*, CV2 T C2::*); |
| 2996 | where CV12 is the union of CV1 and CV2. */ |
| 2997 | |
| 2998 | case MEMBER_REF: |
| 2999 | if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2)) |
| 3000 | { |
| 3001 | tree c1 = TREE_TYPE (type1); |
| 3002 | tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2); |
| 3003 | |
| 3004 | if (CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1) |
| 3005 | && (TYPE_PTRMEMFUNC_P (type2) |
| 3006 | || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2)))) |
| 3007 | break; |
| 3008 | } |
| 3009 | return; |
| 3010 | |
| 3011 | /* 13 For every pair of types L and R, where each of L and R is a floating-point |
| 3012 | or promoted integral type, there exist candidate operator functions of the |
| 3013 | form |
| 3014 | LR operator*(L, R); |
| 3015 | LR operator/(L, R); |
| 3016 | LR operator+(L, R); |
| 3017 | LR operator-(L, R); |
| 3018 | bool operator<(L, R); |
| 3019 | bool operator>(L, R); |
| 3020 | bool operator<=(L, R); |
| 3021 | bool operator>=(L, R); |
| 3022 | bool operator==(L, R); |
| 3023 | bool operator!=(L, R); |
| 3024 | where LR is the result of the usual arithmetic conversions between |
| 3025 | types L and R. |
| 3026 | |
| 3027 | 14 For every integral type T there exists a candidate operator function of |
| 3028 | the form |
| 3029 | |
| 3030 | std::strong_ordering operator<=>(T, T); |
| 3031 | |
| 3032 | 15 For every pair of floating-point types L and R, there exists a candidate |
| 3033 | operator function of the form |
| 3034 | |
| 3035 | std::partial_ordering operator<=>(L, R); |
| 3036 | |
| 3037 | 16 For every cv-qualified or cv-unqualified object type T there exist |
| 3038 | candidate operator functions of the form |
| 3039 | T* operator+(T*, std::ptrdiff_t); |
| 3040 | T& operator[](T*, std::ptrdiff_t); |
| 3041 | T* operator-(T*, std::ptrdiff_t); |
| 3042 | T* operator+(std::ptrdiff_t, T*); |
| 3043 | T& operator[](std::ptrdiff_t, T*); |
| 3044 | |
| 3045 | 17 For every T, where T is a pointer to object type, there exist candidate |
| 3046 | operator functions of the form |
| 3047 | std::ptrdiff_t operator-(T, T); |
| 3048 | |
| 3049 | 18 For every T, where T is an enumeration type or a pointer type, there |
| 3050 | exist candidate operator functions of the form |
| 3051 | bool operator<(T, T); |
| 3052 | bool operator>(T, T); |
| 3053 | bool operator<=(T, T); |
| 3054 | bool operator>=(T, T); |
| 3055 | bool operator==(T, T); |
| 3056 | bool operator!=(T, T); |
| 3057 | R operator<=>(T, T); |
| 3058 | |
| 3059 | where R is the result type specified in [expr.spaceship]. |
| 3060 | |
| 3061 | 19 For every T, where T is a pointer-to-member type or std::nullptr_t, |
| 3062 | there exist candidate operator functions of the form |
| 3063 | bool operator==(T, T); |
| 3064 | bool operator!=(T, T); */ |
| 3065 | |
| 3066 | case MINUS_EXPR: |
| 3067 | if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2)) |
| 3068 | break; |
| 3069 | if (TYPE_PTROB_P (type1) |
| 3070 | && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2)) |
| 3071 | { |
| 3072 | type2 = ptrdiff_type_node; |
| 3073 | break; |
| 3074 | } |
| 3075 | /* FALLTHRU */ |
| 3076 | case MULT_EXPR: |
| 3077 | case TRUNC_DIV_EXPR: |
| 3078 | if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) |
| 3079 | break; |
| 3080 | return; |
| 3081 | |
| 3082 | /* This isn't exactly what's specified above for operator<=>, but it's |
| 3083 | close enough. In particular, we don't care about the return type |
| 3084 | specified above; it doesn't participate in overload resolution and it |
| 3085 | doesn't affect the semantics of the built-in operator. */ |
| 3086 | case SPACESHIP_EXPR: |
| 3087 | case EQ_EXPR: |
| 3088 | case NE_EXPR: |
| 3089 | if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2)) |
| 3090 | || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))) |
| 3091 | break; |
| 3092 | if (NULLPTR_TYPE_P (type1) && NULLPTR_TYPE_P (type2)) |
| 3093 | break; |
| 3094 | if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (t: args[1])) |
| 3095 | { |
| 3096 | type2 = type1; |
| 3097 | break; |
| 3098 | } |
| 3099 | if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (t: args[0])) |
| 3100 | { |
| 3101 | type1 = type2; |
| 3102 | break; |
| 3103 | } |
| 3104 | /* Fall through. */ |
| 3105 | case LT_EXPR: |
| 3106 | case GT_EXPR: |
| 3107 | case LE_EXPR: |
| 3108 | case GE_EXPR: |
| 3109 | case MAX_EXPR: |
| 3110 | case MIN_EXPR: |
| 3111 | if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) |
| 3112 | break; |
| 3113 | if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)) |
| 3114 | break; |
| 3115 | if (TREE_CODE (type1) == ENUMERAL_TYPE |
| 3116 | && TREE_CODE (type2) == ENUMERAL_TYPE) |
| 3117 | break; |
| 3118 | if (TYPE_PTR_P (type1) |
| 3119 | && null_ptr_cst_p (t: args[1])) |
| 3120 | { |
| 3121 | type2 = type1; |
| 3122 | break; |
| 3123 | } |
| 3124 | if (null_ptr_cst_p (t: args[0]) |
| 3125 | && TYPE_PTR_P (type2)) |
| 3126 | { |
| 3127 | type1 = type2; |
| 3128 | break; |
| 3129 | } |
| 3130 | return; |
| 3131 | |
| 3132 | case PLUS_EXPR: |
| 3133 | if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) |
| 3134 | break; |
| 3135 | /* FALLTHRU */ |
| 3136 | case ARRAY_REF: |
| 3137 | if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2)) |
| 3138 | { |
| 3139 | type1 = ptrdiff_type_node; |
| 3140 | break; |
| 3141 | } |
| 3142 | if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2)) |
| 3143 | { |
| 3144 | type2 = ptrdiff_type_node; |
| 3145 | break; |
| 3146 | } |
| 3147 | return; |
| 3148 | |
| 3149 | /* 18For every pair of promoted integral types L and R, there exist candi- |
| 3150 | date operator functions of the form |
| 3151 | LR operator%(L, R); |
| 3152 | LR operator&(L, R); |
| 3153 | LR operator^(L, R); |
| 3154 | LR operator|(L, R); |
| 3155 | L operator<<(L, R); |
| 3156 | L operator>>(L, R); |
| 3157 | where LR is the result of the usual arithmetic conversions between |
| 3158 | types L and R. */ |
| 3159 | |
| 3160 | case TRUNC_MOD_EXPR: |
| 3161 | case BIT_AND_EXPR: |
| 3162 | case BIT_IOR_EXPR: |
| 3163 | case BIT_XOR_EXPR: |
| 3164 | case LSHIFT_EXPR: |
| 3165 | case RSHIFT_EXPR: |
| 3166 | if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2)) |
| 3167 | break; |
| 3168 | return; |
| 3169 | |
| 3170 | /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration |
| 3171 | type, VQ is either volatile or empty, and R is a promoted arithmetic |
| 3172 | type, there exist candidate operator functions of the form |
| 3173 | VQ L& operator=(VQ L&, R); |
| 3174 | VQ L& operator*=(VQ L&, R); |
| 3175 | VQ L& operator/=(VQ L&, R); |
| 3176 | VQ L& operator+=(VQ L&, R); |
| 3177 | VQ L& operator-=(VQ L&, R); |
| 3178 | |
| 3179 | 20For every pair T, VQ), where T is any type and VQ is either volatile |
| 3180 | or empty, there exist candidate operator functions of the form |
| 3181 | T*VQ& operator=(T*VQ&, T*); |
| 3182 | |
| 3183 | 21For every pair T, VQ), where T is a pointer to member type and VQ is |
| 3184 | either volatile or empty, there exist candidate operator functions of |
| 3185 | the form |
| 3186 | VQ T& operator=(VQ T&, T); |
| 3187 | |
| 3188 | 22For every triple T, VQ, I), where T is a cv-qualified or cv- |
| 3189 | unqualified complete object type, VQ is either volatile or empty, and |
| 3190 | I is a promoted integral type, there exist candidate operator func- |
| 3191 | tions of the form |
| 3192 | T*VQ& operator+=(T*VQ&, I); |
| 3193 | T*VQ& operator-=(T*VQ&, I); |
| 3194 | |
| 3195 | 23For every triple L, VQ, R), where L is an integral or enumeration |
| 3196 | type, VQ is either volatile or empty, and R is a promoted integral |
| 3197 | type, there exist candidate operator functions of the form |
| 3198 | |
| 3199 | VQ L& operator%=(VQ L&, R); |
| 3200 | VQ L& operator<<=(VQ L&, R); |
| 3201 | VQ L& operator>>=(VQ L&, R); |
| 3202 | VQ L& operator&=(VQ L&, R); |
| 3203 | VQ L& operator^=(VQ L&, R); |
| 3204 | VQ L& operator|=(VQ L&, R); */ |
| 3205 | |
| 3206 | case MODIFY_EXPR: |
| 3207 | switch (code2) |
| 3208 | { |
| 3209 | case PLUS_EXPR: |
| 3210 | case MINUS_EXPR: |
| 3211 | if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2)) |
| 3212 | { |
| 3213 | type2 = ptrdiff_type_node; |
| 3214 | break; |
| 3215 | } |
| 3216 | /* FALLTHRU */ |
| 3217 | case MULT_EXPR: |
| 3218 | case TRUNC_DIV_EXPR: |
| 3219 | if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) |
| 3220 | break; |
| 3221 | return; |
| 3222 | |
| 3223 | case TRUNC_MOD_EXPR: |
| 3224 | case BIT_AND_EXPR: |
| 3225 | case BIT_IOR_EXPR: |
| 3226 | case BIT_XOR_EXPR: |
| 3227 | case LSHIFT_EXPR: |
| 3228 | case RSHIFT_EXPR: |
| 3229 | if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2)) |
| 3230 | break; |
| 3231 | return; |
| 3232 | |
| 3233 | case NOP_EXPR: |
| 3234 | if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2)) |
| 3235 | break; |
| 3236 | if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2)) |
| 3237 | || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)) |
| 3238 | || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)) |
| 3239 | || ((TYPE_PTRMEMFUNC_P (type1) |
| 3240 | || TYPE_PTR_P (type1)) |
| 3241 | && null_ptr_cst_p (t: args[1]))) |
| 3242 | { |
| 3243 | type2 = type1; |
| 3244 | break; |
| 3245 | } |
| 3246 | return; |
| 3247 | |
| 3248 | default: |
| 3249 | gcc_unreachable (); |
| 3250 | } |
| 3251 | type1 = build_reference_type (type1); |
| 3252 | break; |
| 3253 | |
| 3254 | case COND_EXPR: |
| 3255 | /* [over.built] |
| 3256 | |
| 3257 | For every pair of promoted arithmetic types L and R, there |
| 3258 | exist candidate operator functions of the form |
| 3259 | |
| 3260 | LR operator?(bool, L, R); |
| 3261 | |
| 3262 | where LR is the result of the usual arithmetic conversions |
| 3263 | between types L and R. |
| 3264 | |
| 3265 | For every type T, where T is a pointer or pointer-to-member |
| 3266 | type, there exist candidate operator functions of the form T |
| 3267 | operator?(bool, T, T); */ |
| 3268 | |
| 3269 | if (promoted_arithmetic_type_p (type: type1) |
| 3270 | && promoted_arithmetic_type_p (type: type2)) |
| 3271 | /* That's OK. */ |
| 3272 | break; |
| 3273 | |
| 3274 | /* Otherwise, the types should be pointers. */ |
| 3275 | if (!((TYPE_PTR_OR_PTRMEM_P (type1) || null_ptr_cst_p (t: args[0])) |
| 3276 | && (TYPE_PTR_OR_PTRMEM_P (type2) || null_ptr_cst_p (t: args[1])))) |
| 3277 | return; |
| 3278 | |
| 3279 | /* We don't check that the two types are the same; the logic |
| 3280 | below will actually create two candidates; one in which both |
| 3281 | parameter types are TYPE1, and one in which both parameter |
| 3282 | types are TYPE2. */ |
| 3283 | break; |
| 3284 | |
| 3285 | case REALPART_EXPR: |
| 3286 | case IMAGPART_EXPR: |
| 3287 | if (ARITHMETIC_TYPE_P (type1)) |
| 3288 | break; |
| 3289 | return; |
| 3290 | |
| 3291 | default: |
| 3292 | gcc_unreachable (); |
| 3293 | } |
| 3294 | |
| 3295 | /* Make sure we don't create builtin candidates with dependent types. */ |
| 3296 | bool u1 = uses_template_parms (type1); |
| 3297 | bool u2 = type2 ? uses_template_parms (type2) : false; |
| 3298 | if (u1 || u2) |
| 3299 | { |
| 3300 | /* Try to recover if one of the types is non-dependent. But if |
| 3301 | there's only one type, there's nothing we can do. */ |
| 3302 | if (!type2) |
| 3303 | return; |
| 3304 | /* And we lose if both are dependent. */ |
| 3305 | if (u1 && u2) |
| 3306 | return; |
| 3307 | /* Or if they have different forms. */ |
| 3308 | if (TREE_CODE (type1) != TREE_CODE (type2)) |
| 3309 | return; |
| 3310 | |
| 3311 | if (u1 && !u2) |
| 3312 | type1 = type2; |
| 3313 | else if (u2 && !u1) |
| 3314 | type2 = type1; |
| 3315 | } |
| 3316 | |
| 3317 | /* If we're dealing with two pointer types or two enumeral types, |
| 3318 | we need candidates for both of them. */ |
| 3319 | if (type2 && !same_type_p (type1, type2) |
| 3320 | && TREE_CODE (type1) == TREE_CODE (type2) |
| 3321 | && (TYPE_REF_P (type1) |
| 3322 | || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2)) |
| 3323 | || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)) |
| 3324 | || TYPE_PTRMEMFUNC_P (type1) |
| 3325 | || MAYBE_CLASS_TYPE_P (type1) |
| 3326 | || TREE_CODE (type1) == ENUMERAL_TYPE)) |
| 3327 | { |
| 3328 | if (TYPE_PTR_OR_PTRMEM_P (type1)) |
| 3329 | { |
| 3330 | tree cptype = composite_pointer_type (input_location, |
| 3331 | type1, type2, |
| 3332 | error_mark_node, |
| 3333 | error_mark_node, |
| 3334 | CPO_CONVERSION, |
| 3335 | tf_none); |
| 3336 | if (cptype != error_mark_node) |
| 3337 | { |
| 3338 | build_builtin_candidate |
| 3339 | (candidates, fnname, type1: cptype, type2: cptype, args, argtypes, |
| 3340 | flags, complain); |
| 3341 | return; |
| 3342 | } |
| 3343 | } |
| 3344 | |
| 3345 | build_builtin_candidate |
| 3346 | (candidates, fnname, type1, type2: type1, args, argtypes, flags, complain); |
| 3347 | build_builtin_candidate |
| 3348 | (candidates, fnname, type1: type2, type2, args, argtypes, flags, complain); |
| 3349 | return; |
| 3350 | } |
| 3351 | |
| 3352 | build_builtin_candidate |
| 3353 | (candidates, fnname, type1, type2, args, argtypes, flags, complain); |
| 3354 | } |
| 3355 | |
| 3356 | tree |
| 3357 | type_decays_to (tree type) |
| 3358 | { |
| 3359 | if (TREE_CODE (type) == ARRAY_TYPE) |
| 3360 | return build_pointer_type (TREE_TYPE (type)); |
| 3361 | if (TREE_CODE (type) == FUNCTION_TYPE) |
| 3362 | return build_pointer_type (type); |
| 3363 | return type; |
| 3364 | } |
| 3365 | |
| 3366 | /* There are three conditions of builtin candidates: |
| 3367 | |
| 3368 | 1) bool-taking candidates. These are the same regardless of the input. |
| 3369 | 2) pointer-pair taking candidates. These are generated for each type |
| 3370 | one of the input types converts to. |
| 3371 | 3) arithmetic candidates. According to the standard, we should generate |
| 3372 | all of these, but I'm trying not to... |
| 3373 | |
| 3374 | Here we generate a superset of the possible candidates for this particular |
| 3375 | case. That is a subset of the full set the standard defines, plus some |
| 3376 | other cases which the standard disallows. add_builtin_candidate will |
| 3377 | filter out the invalid set. */ |
| 3378 | |
| 3379 | static void |
| 3380 | add_builtin_candidates (struct z_candidate **candidates, enum tree_code code, |
| 3381 | enum tree_code code2, tree fnname, |
| 3382 | vec<tree, va_gc> *argv, |
| 3383 | int flags, tsubst_flags_t complain) |
| 3384 | { |
| 3385 | int ref1; |
| 3386 | int enum_p = 0; |
| 3387 | tree type, argtypes[3], t; |
| 3388 | /* TYPES[i] is the set of possible builtin-operator parameter types |
| 3389 | we will consider for the Ith argument. */ |
| 3390 | vec<tree, va_gc> *types[2]; |
| 3391 | unsigned ix; |
| 3392 | vec<tree, va_gc> &args = *argv; |
| 3393 | unsigned len = args.length (); |
| 3394 | |
| 3395 | for (unsigned i = 0; i < len; ++i) |
| 3396 | { |
| 3397 | if (args[i]) |
| 3398 | argtypes[i] = unlowered_expr_type (args[i]); |
| 3399 | else |
| 3400 | argtypes[i] = NULL_TREE; |
| 3401 | } |
| 3402 | |
| 3403 | switch (code) |
| 3404 | { |
| 3405 | /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type, |
| 3406 | and VQ is either volatile or empty, there exist candidate operator |
| 3407 | functions of the form |
| 3408 | VQ T& operator++(VQ T&); */ |
| 3409 | |
| 3410 | case POSTINCREMENT_EXPR: |
| 3411 | case PREINCREMENT_EXPR: |
| 3412 | case POSTDECREMENT_EXPR: |
| 3413 | case PREDECREMENT_EXPR: |
| 3414 | case MODIFY_EXPR: |
| 3415 | ref1 = 1; |
| 3416 | break; |
| 3417 | |
| 3418 | /* 24There also exist candidate operator functions of the form |
| 3419 | bool operator!(bool); |
| 3420 | bool operator&&(bool, bool); |
| 3421 | bool operator||(bool, bool); */ |
| 3422 | |
| 3423 | case TRUTH_NOT_EXPR: |
| 3424 | build_builtin_candidate |
| 3425 | (candidates, fnname, boolean_type_node, |
| 3426 | NULL_TREE, args, argtypes, flags, complain); |
| 3427 | return; |
| 3428 | |
| 3429 | case TRUTH_ORIF_EXPR: |
| 3430 | case TRUTH_ANDIF_EXPR: |
| 3431 | build_builtin_candidate |
| 3432 | (candidates, fnname, boolean_type_node, |
| 3433 | boolean_type_node, args, argtypes, flags, complain); |
| 3434 | return; |
| 3435 | |
| 3436 | case ADDR_EXPR: |
| 3437 | case COMPOUND_EXPR: |
| 3438 | case COMPONENT_REF: |
| 3439 | case CO_AWAIT_EXPR: |
| 3440 | return; |
| 3441 | |
| 3442 | case COND_EXPR: |
| 3443 | case EQ_EXPR: |
| 3444 | case NE_EXPR: |
| 3445 | case LT_EXPR: |
| 3446 | case LE_EXPR: |
| 3447 | case GT_EXPR: |
| 3448 | case GE_EXPR: |
| 3449 | case SPACESHIP_EXPR: |
| 3450 | enum_p = 1; |
| 3451 | /* Fall through. */ |
| 3452 | |
| 3453 | default: |
| 3454 | ref1 = 0; |
| 3455 | } |
| 3456 | |
| 3457 | types[0] = make_tree_vector (); |
| 3458 | types[1] = make_tree_vector (); |
| 3459 | |
| 3460 | if (len == 3) |
| 3461 | len = 2; |
| 3462 | for (unsigned i = 0; i < len; ++i) |
| 3463 | { |
| 3464 | if (MAYBE_CLASS_TYPE_P (argtypes[i])) |
| 3465 | { |
| 3466 | tree convs; |
| 3467 | |
| 3468 | if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR) |
| 3469 | return; |
| 3470 | |
| 3471 | convs = lookup_conversions (argtypes[i]); |
| 3472 | |
| 3473 | if (code == COND_EXPR) |
| 3474 | { |
| 3475 | if (lvalue_p (args[i])) |
| 3476 | vec_safe_push (v&: types[i], obj: build_reference_type (argtypes[i])); |
| 3477 | |
| 3478 | vec_safe_push (v&: types[i], TYPE_MAIN_VARIANT (argtypes[i])); |
| 3479 | } |
| 3480 | |
| 3481 | else if (! convs) |
| 3482 | return; |
| 3483 | |
| 3484 | for (; convs; convs = TREE_CHAIN (convs)) |
| 3485 | { |
| 3486 | type = TREE_TYPE (convs); |
| 3487 | |
| 3488 | if (i == 0 && ref1 |
| 3489 | && (!TYPE_REF_P (type) |
| 3490 | || CP_TYPE_CONST_P (TREE_TYPE (type)))) |
| 3491 | continue; |
| 3492 | |
| 3493 | if (code == COND_EXPR && TYPE_REF_P (type)) |
| 3494 | vec_safe_push (v&: types[i], obj: type); |
| 3495 | |
| 3496 | type = non_reference (type); |
| 3497 | if (i != 0 || ! ref1) |
| 3498 | { |
| 3499 | type = cv_unqualified (type_decays_to (type)); |
| 3500 | if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE) |
| 3501 | vec_safe_push (v&: types[i], obj: type); |
| 3502 | if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type)) |
| 3503 | type = type_promotes_to (type); |
| 3504 | } |
| 3505 | |
| 3506 | if (! vec_member (type, types[i])) |
| 3507 | vec_safe_push (v&: types[i], obj: type); |
| 3508 | } |
| 3509 | } |
| 3510 | else |
| 3511 | { |
| 3512 | if (code == COND_EXPR && lvalue_p (args[i])) |
| 3513 | vec_safe_push (v&: types[i], obj: build_reference_type (argtypes[i])); |
| 3514 | type = non_reference (argtypes[i]); |
| 3515 | if (i != 0 || ! ref1) |
| 3516 | { |
| 3517 | type = cv_unqualified (type_decays_to (type)); |
| 3518 | if (enum_p && UNSCOPED_ENUM_P (type)) |
| 3519 | vec_safe_push (v&: types[i], obj: type); |
| 3520 | if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type)) |
| 3521 | type = type_promotes_to (type); |
| 3522 | } |
| 3523 | vec_safe_push (v&: types[i], obj: type); |
| 3524 | } |
| 3525 | } |
| 3526 | |
| 3527 | /* Run through the possible parameter types of both arguments, |
| 3528 | creating candidates with those parameter types. */ |
| 3529 | FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t) |
| 3530 | { |
| 3531 | unsigned jx; |
| 3532 | tree u; |
| 3533 | |
| 3534 | if (!types[1]->is_empty ()) |
| 3535 | FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u) |
| 3536 | add_builtin_candidate |
| 3537 | (candidates, code, code2, fnname, type1: t, |
| 3538 | type2: u, args, argtypes, flags, complain); |
| 3539 | else |
| 3540 | add_builtin_candidate |
| 3541 | (candidates, code, code2, fnname, type1: t, |
| 3542 | NULL_TREE, args, argtypes, flags, complain); |
| 3543 | } |
| 3544 | |
| 3545 | release_tree_vector (types[0]); |
| 3546 | release_tree_vector (types[1]); |
| 3547 | } |
| 3548 | |
| 3549 | |
| 3550 | /* If TMPL can be successfully instantiated as indicated by |
| 3551 | EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES. |
| 3552 | |
| 3553 | TMPL is the template. EXPLICIT_TARGS are any explicit template |
| 3554 | arguments. ARGLIST is the arguments provided at the call-site. |
| 3555 | This does not change ARGLIST. The RETURN_TYPE is the desired type |
| 3556 | for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are |
| 3557 | as for add_function_candidate. If an OBJ is supplied, FLAGS and |
| 3558 | CTYPE are ignored, and OBJ is as for add_conv_candidate. |
| 3559 | |
| 3560 | SHORTCUT_BAD_CONVS is as in add_function_candidate. */ |
| 3561 | |
| 3562 | static struct z_candidate* |
| 3563 | add_template_candidate_real (struct z_candidate **candidates, tree tmpl, |
| 3564 | tree ctype, tree explicit_targs, tree first_arg, |
| 3565 | const vec<tree, va_gc> *arglist, tree return_type, |
| 3566 | tree access_path, tree conversion_path, |
| 3567 | int flags, tree obj, unification_kind_t strict, |
| 3568 | bool shortcut_bad_convs, tsubst_flags_t complain) |
| 3569 | { |
| 3570 | int ntparms = DECL_NTPARMS (tmpl); |
| 3571 | tree targs = make_tree_vec (ntparms); |
| 3572 | unsigned int len = vec_safe_length (v: arglist); |
| 3573 | unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len; |
| 3574 | unsigned int skip_without_in_chrg = 0; |
| 3575 | tree first_arg_without_in_chrg = first_arg; |
| 3576 | tree *args_without_in_chrg; |
| 3577 | unsigned int nargs_without_in_chrg; |
| 3578 | unsigned int ia, ix; |
| 3579 | tree arg; |
| 3580 | struct z_candidate *cand; |
| 3581 | tree fn; |
| 3582 | struct rejection_reason *reason = NULL; |
| 3583 | int errs; |
| 3584 | conversion **convs = NULL; |
| 3585 | |
| 3586 | /* We don't do deduction on the in-charge parameter, the VTT |
| 3587 | parameter or 'this'. */ |
| 3588 | if (DECL_IOBJ_MEMBER_FUNCTION_P (tmpl)) |
| 3589 | { |
| 3590 | if (first_arg_without_in_chrg != NULL_TREE) |
| 3591 | first_arg_without_in_chrg = NULL_TREE; |
| 3592 | else if (return_type && strict == DEDUCE_CALL) |
| 3593 | /* We're deducing for a call to the result of a template conversion |
| 3594 | function, so the args don't contain 'this'; leave them alone. */; |
| 3595 | else |
| 3596 | ++skip_without_in_chrg; |
| 3597 | } |
| 3598 | |
| 3599 | if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl) |
| 3600 | || DECL_BASE_CONSTRUCTOR_P (tmpl)) |
| 3601 | && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl))) |
| 3602 | { |
| 3603 | if (first_arg_without_in_chrg != NULL_TREE) |
| 3604 | first_arg_without_in_chrg = NULL_TREE; |
| 3605 | else |
| 3606 | ++skip_without_in_chrg; |
| 3607 | } |
| 3608 | |
| 3609 | if (len < skip_without_in_chrg) |
| 3610 | return add_ignored_candidate (candidates, fn: tmpl); |
| 3611 | |
| 3612 | if (DECL_CONSTRUCTOR_P (tmpl) && nargs == 2 |
| 3613 | && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg), |
| 3614 | TREE_TYPE ((*arglist)[0]))) |
| 3615 | { |
| 3616 | /* 12.8/6 says, "A declaration of a constructor for a class X is |
| 3617 | ill-formed if its first parameter is of type (optionally cv-qualified) |
| 3618 | X and either there are no other parameters or else all other |
| 3619 | parameters have default arguments. A member function template is never |
| 3620 | instantiated to produce such a constructor signature." |
| 3621 | |
| 3622 | So if we're trying to copy an object of the containing class, don't |
| 3623 | consider a template constructor that has a first parameter type that |
| 3624 | is just a template parameter, as we would deduce a signature that we |
| 3625 | would then reject in the code below. */ |
| 3626 | if (tree firstparm = FUNCTION_FIRST_USER_PARMTYPE (tmpl)) |
| 3627 | { |
| 3628 | firstparm = TREE_VALUE (firstparm); |
| 3629 | if (PACK_EXPANSION_P (firstparm)) |
| 3630 | firstparm = PACK_EXPANSION_PATTERN (firstparm); |
| 3631 | if (TREE_CODE (firstparm) == TEMPLATE_TYPE_PARM) |
| 3632 | { |
| 3633 | gcc_assert (!explicit_targs); |
| 3634 | reason = invalid_copy_with_fn_template_rejection (); |
| 3635 | goto fail; |
| 3636 | } |
| 3637 | } |
| 3638 | } |
| 3639 | |
| 3640 | nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0) |
| 3641 | + (len - skip_without_in_chrg)); |
| 3642 | args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg); |
| 3643 | ia = 0; |
| 3644 | if (first_arg_without_in_chrg != NULL_TREE) |
| 3645 | { |
| 3646 | args_without_in_chrg[ia] = first_arg_without_in_chrg; |
| 3647 | ++ia; |
| 3648 | } |
| 3649 | for (ix = skip_without_in_chrg; |
| 3650 | vec_safe_iterate (v: arglist, ix, ptr: &arg); |
| 3651 | ++ix) |
| 3652 | { |
| 3653 | args_without_in_chrg[ia] = arg; |
| 3654 | ++ia; |
| 3655 | } |
| 3656 | gcc_assert (ia == nargs_without_in_chrg); |
| 3657 | |
| 3658 | if (!obj) |
| 3659 | { |
| 3660 | /* Check that there's no obvious arity mismatch before proceeding with |
| 3661 | deduction. This avoids substituting explicit template arguments |
| 3662 | into the template or e.g. derived-to-base parm/arg unification |
| 3663 | (which could result in an error outside the immediate context) when |
| 3664 | the resulting candidate would be unviable anyway. */ |
| 3665 | int min_arity = 0, max_arity = 0; |
| 3666 | tree parms = TYPE_ARG_TYPES (TREE_TYPE (tmpl)); |
| 3667 | parms = skip_artificial_parms_for (tmpl, parms); |
| 3668 | for (; parms != void_list_node; parms = TREE_CHAIN (parms)) |
| 3669 | { |
| 3670 | if (!parms || PACK_EXPANSION_P (TREE_VALUE (parms))) |
| 3671 | { |
| 3672 | max_arity = -1; |
| 3673 | break; |
| 3674 | } |
| 3675 | if (TREE_PURPOSE (parms)) |
| 3676 | /* A parameter with a default argument. */ |
| 3677 | ++max_arity; |
| 3678 | else |
| 3679 | ++min_arity, ++max_arity; |
| 3680 | } |
| 3681 | if (ia < (unsigned)min_arity) |
| 3682 | { |
| 3683 | /* Too few arguments. */ |
| 3684 | reason = arity_rejection (NULL_TREE, expected: min_arity, actual: ia, |
| 3685 | /*least_p=*/(max_arity == -1)); |
| 3686 | goto fail; |
| 3687 | } |
| 3688 | else if (max_arity != -1 && ia > (unsigned)max_arity) |
| 3689 | { |
| 3690 | /* Too many arguments. */ |
| 3691 | reason = arity_rejection (NULL_TREE, expected: max_arity, actual: ia); |
| 3692 | goto fail; |
| 3693 | } |
| 3694 | |
| 3695 | convs = alloc_conversions (n: nargs); |
| 3696 | |
| 3697 | if (shortcut_bad_convs |
| 3698 | && DECL_IOBJ_MEMBER_FUNCTION_P (tmpl) |
| 3699 | && !DECL_CONSTRUCTOR_P (tmpl)) |
| 3700 | { |
| 3701 | /* Check the 'this' conversion before proceeding with deduction. |
| 3702 | This is effectively an extension of the DR 1391 resolution |
| 3703 | that we perform in check_non_deducible_conversions, though it's |
| 3704 | convenient to do this extra check here instead of there. */ |
| 3705 | tree parmtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (tmpl))); |
| 3706 | tree argtype = lvalue_type (first_arg); |
| 3707 | tree arg = first_arg; |
| 3708 | conversion *t = build_this_conversion (fn: tmpl, ctype, |
| 3709 | parmtype, argtype, arg, |
| 3710 | flags, complain); |
| 3711 | convs[0] = t; |
| 3712 | if (t->bad_p) |
| 3713 | { |
| 3714 | reason = bad_arg_conversion_rejection (first_arg, n_arg: 0, |
| 3715 | from: arg, to: parmtype, |
| 3716 | EXPR_LOCATION (arg)); |
| 3717 | goto fail; |
| 3718 | } |
| 3719 | } |
| 3720 | } |
| 3721 | |
| 3722 | errs = errorcount+sorrycount; |
| 3723 | fn = fn_type_unification (tmpl, explicit_targs, targs, |
| 3724 | args_without_in_chrg, |
| 3725 | nargs_without_in_chrg, |
| 3726 | return_type, strict, flags, convs, |
| 3727 | false, complain & tf_decltype); |
| 3728 | |
| 3729 | if (fn == error_mark_node) |
| 3730 | { |
| 3731 | /* Don't repeat unification later if it already resulted in errors. */ |
| 3732 | if (errorcount+sorrycount == errs) |
| 3733 | reason = template_unification_rejection (tmpl, explicit_targs, |
| 3734 | targs, args: args_without_in_chrg, |
| 3735 | nargs: nargs_without_in_chrg, |
| 3736 | return_type, strict, flags); |
| 3737 | else |
| 3738 | reason = template_unification_error_rejection (); |
| 3739 | goto fail; |
| 3740 | } |
| 3741 | |
| 3742 | /* Now the explicit specifier might have been deduced; check if this |
| 3743 | declaration is explicit. If it is and we're ignoring non-converting |
| 3744 | constructors, don't add this function to the set of candidates. */ |
| 3745 | if (((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR)) |
| 3746 | == LOOKUP_ONLYCONVERTING) |
| 3747 | && DECL_NONCONVERTING_P (fn)) |
| 3748 | return add_ignored_candidate (candidates, fn); |
| 3749 | |
| 3750 | if (DECL_CONSTRUCTOR_P (fn) && nargs == 2) |
| 3751 | { |
| 3752 | tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn); |
| 3753 | if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)), |
| 3754 | ctype)) |
| 3755 | { |
| 3756 | /* We're trying to produce a constructor with a prohibited signature, |
| 3757 | as discussed above; handle here any cases we didn't catch then, |
| 3758 | such as X(X<T>). */ |
| 3759 | reason = invalid_copy_with_fn_template_rejection (); |
| 3760 | goto fail; |
| 3761 | } |
| 3762 | } |
| 3763 | |
| 3764 | if (obj != NULL_TREE) |
| 3765 | /* Aha, this is a conversion function. */ |
| 3766 | cand = add_conv_candidate (candidates, fn, obj, arglist, |
| 3767 | access_path, conversion_path, complain); |
| 3768 | else |
| 3769 | cand = add_function_candidate (candidates, fn, ctype, |
| 3770 | first_arg, args: arglist, access_path, |
| 3771 | conversion_path, flags, convs, |
| 3772 | shortcut_bad_convs, complain); |
| 3773 | if (DECL_TI_TEMPLATE (fn) != tmpl) |
| 3774 | /* This situation can occur if a member template of a template |
| 3775 | class is specialized. Then, instantiate_template might return |
| 3776 | an instantiation of the specialization, in which case the |
| 3777 | DECL_TI_TEMPLATE field will point at the original |
| 3778 | specialization. For example: |
| 3779 | |
| 3780 | template <class T> struct S { template <class U> void f(U); |
| 3781 | template <> void f(int) {}; }; |
| 3782 | S<double> sd; |
| 3783 | sd.f(3); |
| 3784 | |
| 3785 | Here, TMPL will be template <class U> S<double>::f(U). |
| 3786 | And, instantiate template will give us the specialization |
| 3787 | template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field |
| 3788 | for this will point at template <class T> template <> S<T>::f(int), |
| 3789 | so that we can find the definition. For the purposes of |
| 3790 | overload resolution, however, we want the original TMPL. */ |
| 3791 | cand->template_decl = build_template_info (tmpl, targs); |
| 3792 | else |
| 3793 | cand->template_decl = DECL_TEMPLATE_INFO (fn); |
| 3794 | cand->explicit_targs = explicit_targs; |
| 3795 | |
| 3796 | return cand; |
| 3797 | fail: |
| 3798 | int viable = (reason->code == rr_bad_arg_conversion ? -1 : 0); |
| 3799 | return add_candidate (candidates, fn: tmpl, first_arg, args: arglist, num_convs: nargs, convs, |
| 3800 | access_path, conversion_path, viable, reason, flags); |
| 3801 | } |
| 3802 | |
| 3803 | |
| 3804 | static struct z_candidate * |
| 3805 | add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype, |
| 3806 | tree explicit_targs, tree first_arg, |
| 3807 | const vec<tree, va_gc> *arglist, tree return_type, |
| 3808 | tree access_path, tree conversion_path, int flags, |
| 3809 | unification_kind_t strict, bool shortcut_bad_convs, |
| 3810 | tsubst_flags_t complain) |
| 3811 | { |
| 3812 | return |
| 3813 | add_template_candidate_real (candidates, tmpl, ctype, |
| 3814 | explicit_targs, first_arg, arglist, |
| 3815 | return_type, access_path, conversion_path, |
| 3816 | flags, NULL_TREE, strict, shortcut_bad_convs, |
| 3817 | complain); |
| 3818 | } |
| 3819 | |
| 3820 | /* Create an overload candidate for the conversion function template TMPL, |
| 3821 | returning RETURN_TYPE, which will be invoked for expression OBJ to produce a |
| 3822 | pointer-to-function which will in turn be called with the argument list |
| 3823 | ARGLIST, and add it to CANDIDATES. This does not change ARGLIST. FLAGS is |
| 3824 | passed on to implicit_conversion. */ |
| 3825 | |
| 3826 | static struct z_candidate * |
| 3827 | add_template_conv_candidate (struct z_candidate **candidates, tree tmpl, |
| 3828 | tree obj, |
| 3829 | const vec<tree, va_gc> *arglist, |
| 3830 | tree return_type, tree access_path, |
| 3831 | tree conversion_path, tsubst_flags_t complain) |
| 3832 | { |
| 3833 | return |
| 3834 | add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE, |
| 3835 | NULL_TREE, arglist, return_type, access_path, |
| 3836 | conversion_path, flags: 0, obj, strict: DEDUCE_CALL, |
| 3837 | /*shortcut_bad_convs=*/false, complain); |
| 3838 | } |
| 3839 | |
| 3840 | /* The CANDS are the set of candidates that were considered for |
| 3841 | overload resolution. Sort CANDS so that the strictly viable |
| 3842 | candidates appear first, followed by non-strictly viable candidates, |
| 3843 | followed by non-viable candidates. Returns the first candidate |
| 3844 | in this sorted list. If any of the candidates were viable, set |
| 3845 | *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be |
| 3846 | considered viable only if it is strictly viable when setting |
| 3847 | *ANY_VIABLE_P. */ |
| 3848 | |
| 3849 | static struct z_candidate* |
| 3850 | splice_viable (struct z_candidate *cands, |
| 3851 | bool strict_p, |
| 3852 | bool *any_viable_p) |
| 3853 | { |
| 3854 | z_candidate *strictly_viable = nullptr; |
| 3855 | z_candidate **strictly_viable_tail = &strictly_viable; |
| 3856 | |
| 3857 | z_candidate *non_strictly_viable = nullptr; |
| 3858 | z_candidate **non_strictly_viable_tail = &non_strictly_viable; |
| 3859 | |
| 3860 | z_candidate *non_viable = nullptr; |
| 3861 | z_candidate **non_viable_tail = &non_viable; |
| 3862 | |
| 3863 | z_candidate *non_viable_ignored = nullptr; |
| 3864 | z_candidate **non_viable_ignored_tail = &non_viable_ignored; |
| 3865 | |
| 3866 | /* Be strict inside templates, since build_over_call won't actually |
| 3867 | do the conversions to get pedwarns. */ |
| 3868 | if (processing_template_decl) |
| 3869 | strict_p = true; |
| 3870 | |
| 3871 | for (z_candidate *cand = cands; cand; cand = cand->next) |
| 3872 | { |
| 3873 | if (!strict_p |
| 3874 | && (cand->viable == 1 || TREE_CODE (cand->fn) == TEMPLATE_DECL)) |
| 3875 | /* Be strict in the presence of a viable candidate. Also if |
| 3876 | there are template candidates, so that we get deduction errors |
| 3877 | for them instead of silently preferring a bad conversion. */ |
| 3878 | strict_p = true; |
| 3879 | |
| 3880 | /* Move this candidate to the appropriate list according to |
| 3881 | its viability. */ |
| 3882 | auto& tail = (cand->viable == 1 ? strictly_viable_tail |
| 3883 | : cand->viable == -1 ? non_strictly_viable_tail |
| 3884 | : ignored_candidate_p (cand) ? non_viable_ignored_tail |
| 3885 | : non_viable_tail); |
| 3886 | *tail = cand; |
| 3887 | tail = &cand->next; |
| 3888 | } |
| 3889 | |
| 3890 | *any_viable_p = (strictly_viable != nullptr |
| 3891 | || (!strict_p && non_strictly_viable != nullptr)); |
| 3892 | |
| 3893 | /* Combine the lists. */ |
| 3894 | *non_viable_ignored_tail = nullptr; |
| 3895 | *non_viable_tail = non_viable_ignored; |
| 3896 | *non_strictly_viable_tail = non_viable; |
| 3897 | *strictly_viable_tail = non_strictly_viable; |
| 3898 | |
| 3899 | return strictly_viable; |
| 3900 | } |
| 3901 | |
| 3902 | static bool |
| 3903 | any_strictly_viable (struct z_candidate *cands) |
| 3904 | { |
| 3905 | for (; cands; cands = cands->next) |
| 3906 | if (cands->viable == 1) |
| 3907 | return true; |
| 3908 | return false; |
| 3909 | } |
| 3910 | |
| 3911 | /* OBJ is being used in an expression like "OBJ.f (...)". In other |
| 3912 | words, it is about to become the "this" pointer for a member |
| 3913 | function call. Take the address of the object. */ |
| 3914 | |
| 3915 | static tree |
| 3916 | build_this (tree obj) |
| 3917 | { |
| 3918 | /* In a template, we are only concerned about the type of the |
| 3919 | expression, so we can take a shortcut. */ |
| 3920 | if (processing_template_decl) |
| 3921 | return build_address (obj); |
| 3922 | |
| 3923 | return cp_build_addr_expr (obj, tf_warning_or_error); |
| 3924 | } |
| 3925 | |
| 3926 | /* Returns true iff functions are equivalent. Equivalent functions are |
| 3927 | not '==' only if one is a function-local extern function or if |
| 3928 | both are extern "C". */ |
| 3929 | |
| 3930 | static inline int |
| 3931 | equal_functions (tree fn1, tree fn2) |
| 3932 | { |
| 3933 | if (TREE_CODE (fn1) != TREE_CODE (fn2)) |
| 3934 | return 0; |
| 3935 | if (TREE_CODE (fn1) == TEMPLATE_DECL) |
| 3936 | return fn1 == fn2; |
| 3937 | if (DECL_LOCAL_DECL_P (fn1) || DECL_LOCAL_DECL_P (fn2) |
| 3938 | || DECL_EXTERN_C_FUNCTION_P (fn1)) |
| 3939 | return decls_match (fn1, fn2); |
| 3940 | return fn1 == fn2; |
| 3941 | } |
| 3942 | |
| 3943 | /* Print information about a candidate FN being rejected due to INFO. */ |
| 3944 | |
| 3945 | static void |
| 3946 | print_conversion_rejection (location_t loc, struct conversion_info *info, |
| 3947 | tree fn) |
| 3948 | { |
| 3949 | tree from = info->from; |
| 3950 | if (!TYPE_P (from)) |
| 3951 | from = lvalue_type (from); |
| 3952 | if (info->n_arg == -1) |
| 3953 | { |
| 3954 | /* Conversion of implicit `this' argument failed. */ |
| 3955 | if (!TYPE_P (info->from)) |
| 3956 | /* A bad conversion for 'this' must be discarding cv-quals. */ |
| 3957 | inform (loc, "passing %qT as %<this%> " |
| 3958 | "argument discards qualifiers" , |
| 3959 | from); |
| 3960 | else |
| 3961 | inform (loc, "no known conversion for implicit " |
| 3962 | "%<this%> parameter from %qH to %qI" , |
| 3963 | from, info->to_type); |
| 3964 | } |
| 3965 | else if (!TYPE_P (info->from)) |
| 3966 | { |
| 3967 | if (info->n_arg >= 0) |
| 3968 | inform (loc, "conversion of argument %d would be ill-formed:" , |
| 3969 | info->n_arg + 1); |
| 3970 | iloc_sentinel ils = loc; |
| 3971 | perform_implicit_conversion (info->to_type, info->from, |
| 3972 | tf_warning_or_error); |
| 3973 | } |
| 3974 | else if (info->n_arg == -2) |
| 3975 | /* Conversion of conversion function return value failed. */ |
| 3976 | inform (loc, "no known conversion from %qH to %qI" , |
| 3977 | from, info->to_type); |
| 3978 | else |
| 3979 | { |
| 3980 | if (TREE_CODE (fn) == FUNCTION_DECL) |
| 3981 | loc = get_fndecl_argument_location (fn, info->n_arg); |
| 3982 | inform (loc, "no known conversion for argument %d from %qH to %qI" , |
| 3983 | info->n_arg + 1, from, info->to_type); |
| 3984 | } |
| 3985 | } |
| 3986 | |
| 3987 | /* Print information about a candidate with WANT parameters and we found |
| 3988 | HAVE. */ |
| 3989 | |
| 3990 | static void |
| 3991 | print_arity_information (location_t loc, unsigned int have, unsigned int want, |
| 3992 | bool least_p) |
| 3993 | { |
| 3994 | if (least_p) |
| 3995 | inform_n (loc, want, |
| 3996 | "candidate expects at least %d argument, %d provided" , |
| 3997 | "candidate expects at least %d arguments, %d provided" , |
| 3998 | want, have); |
| 3999 | else |
| 4000 | inform_n (loc, want, |
| 4001 | "candidate expects %d argument, %d provided" , |
| 4002 | "candidate expects %d arguments, %d provided" , |
| 4003 | want, have); |
| 4004 | } |
| 4005 | |
| 4006 | /* Print information about one overload candidate CANDIDATE. MSGSTR |
| 4007 | is the text to print before the candidate itself. |
| 4008 | |
| 4009 | NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected |
| 4010 | to have been run through gettext by the caller. This wart makes |
| 4011 | life simpler in print_z_candidates and for the translators. */ |
| 4012 | |
| 4013 | static void |
| 4014 | print_z_candidate (location_t loc, const char *msgstr, |
| 4015 | struct z_candidate *candidate) |
| 4016 | { |
| 4017 | const char *msg = (msgstr == NULL |
| 4018 | ? "" |
| 4019 | : ACONCAT ((_(msgstr), " " , NULL))); |
| 4020 | tree fn = candidate->fn; |
| 4021 | if (flag_new_inheriting_ctors) |
| 4022 | fn = strip_inheriting_ctors (fn); |
| 4023 | location_t cloc = location_of (fn); |
| 4024 | |
| 4025 | if (identifier_p (t: fn)) |
| 4026 | { |
| 4027 | cloc = loc; |
| 4028 | if (candidate->num_convs == 3) |
| 4029 | inform (cloc, "%s%<%D(%T, %T, %T)%> (built-in)" , msg, fn, |
| 4030 | candidate->convs[0]->type, |
| 4031 | candidate->convs[1]->type, |
| 4032 | candidate->convs[2]->type); |
| 4033 | else if (candidate->num_convs == 2) |
| 4034 | inform (cloc, "%s%<%D(%T, %T)%> (built-in)" , msg, fn, |
| 4035 | candidate->convs[0]->type, |
| 4036 | candidate->convs[1]->type); |
| 4037 | else |
| 4038 | inform (cloc, "%s%<%D(%T)%> (built-in)" , msg, fn, |
| 4039 | candidate->convs[0]->type); |
| 4040 | } |
| 4041 | else if (TYPE_P (fn)) |
| 4042 | inform (cloc, "%s%qT (conversion)" , msg, fn); |
| 4043 | else if (candidate->viable == -1) |
| 4044 | inform (cloc, "%s%#qD (near match)" , msg, fn); |
| 4045 | else if (ignored_candidate_p (cand: candidate)) |
| 4046 | inform (cloc, "%s%#qD (ignored)" , msg, fn); |
| 4047 | else if (DECL_DELETED_FN (fn)) |
| 4048 | inform (cloc, "%s%#qD (deleted)" , msg, fn); |
| 4049 | else if (candidate->reversed ()) |
| 4050 | inform (cloc, "%s%#qD (reversed)" , msg, fn); |
| 4051 | else if (candidate->rewritten ()) |
| 4052 | inform (cloc, "%s%#qD (rewritten)" , msg, fn); |
| 4053 | else |
| 4054 | inform (cloc, "%s%#qD" , msg, fn); |
| 4055 | if (fn != candidate->fn) |
| 4056 | { |
| 4057 | cloc = location_of (candidate->fn); |
| 4058 | inform (cloc, "inherited here" ); |
| 4059 | } |
| 4060 | /* Give the user some information about why this candidate failed. */ |
| 4061 | if (candidate->reason != NULL) |
| 4062 | { |
| 4063 | auto_diagnostic_nesting_level sentinel; |
| 4064 | struct rejection_reason *r = candidate->reason; |
| 4065 | |
| 4066 | switch (r->code) |
| 4067 | { |
| 4068 | case rr_arity: |
| 4069 | print_arity_information (loc: cloc, have: r->u.arity.actual, |
| 4070 | want: r->u.arity.expected, |
| 4071 | least_p: r->u.arity.least_p); |
| 4072 | break; |
| 4073 | case rr_arg_conversion: |
| 4074 | print_conversion_rejection (loc: cloc, info: &r->u.conversion, fn); |
| 4075 | break; |
| 4076 | case rr_bad_arg_conversion: |
| 4077 | print_conversion_rejection (loc: cloc, info: &r->u.bad_conversion, fn); |
| 4078 | break; |
| 4079 | case rr_explicit_conversion: |
| 4080 | inform (cloc, "return type %qT of explicit conversion function " |
| 4081 | "cannot be converted to %qT with a qualification " |
| 4082 | "conversion" , r->u.conversion.from, |
| 4083 | r->u.conversion.to_type); |
| 4084 | break; |
| 4085 | case rr_template_conversion: |
| 4086 | inform (cloc, "conversion from return type %qT of template " |
| 4087 | "conversion function specialization to %qT is not an " |
| 4088 | "exact match" , r->u.conversion.from, |
| 4089 | r->u.conversion.to_type); |
| 4090 | break; |
| 4091 | case rr_template_unification: |
| 4092 | /* We use template_unification_error_rejection if unification caused |
| 4093 | actual non-SFINAE errors, in which case we don't need to repeat |
| 4094 | them here. */ |
| 4095 | if (r->u.template_unification.tmpl == NULL_TREE) |
| 4096 | { |
| 4097 | inform (cloc, "substitution of deduced template arguments " |
| 4098 | "resulted in errors seen above" ); |
| 4099 | break; |
| 4100 | } |
| 4101 | /* Re-run template unification with diagnostics. */ |
| 4102 | inform (cloc, "template argument deduction/substitution failed:" ); |
| 4103 | { |
| 4104 | auto_diagnostic_nesting_level sentinel; |
| 4105 | fn_type_unification (r->u.template_unification.tmpl, |
| 4106 | r->u.template_unification.explicit_targs, |
| 4107 | (make_tree_vec |
| 4108 | (r->u.template_unification.num_targs)), |
| 4109 | r->u.template_unification.args, |
| 4110 | r->u.template_unification.nargs, |
| 4111 | r->u.template_unification.return_type, |
| 4112 | r->u.template_unification.strict, |
| 4113 | r->u.template_unification.flags, |
| 4114 | NULL, true, false); |
| 4115 | } |
| 4116 | break; |
| 4117 | case rr_invalid_copy: |
| 4118 | inform (cloc, |
| 4119 | "a constructor taking a single argument of its own " |
| 4120 | "class type is invalid" ); |
| 4121 | break; |
| 4122 | case rr_constraint_failure: |
| 4123 | { |
| 4124 | auto_diagnostic_nesting_level sentinel; |
| 4125 | diagnose_constraints (cloc, fn, NULL_TREE); |
| 4126 | } |
| 4127 | break; |
| 4128 | case rr_inherited_ctor: |
| 4129 | inform (cloc, "an inherited constructor is not a candidate for " |
| 4130 | "initialization from an expression of the same or derived " |
| 4131 | "type" ); |
| 4132 | break; |
| 4133 | case rr_ignored: |
| 4134 | break; |
| 4135 | case rr_none: |
| 4136 | default: |
| 4137 | /* This candidate didn't have any issues or we failed to |
| 4138 | handle a particular code. Either way... */ |
| 4139 | gcc_unreachable (); |
| 4140 | } |
| 4141 | } |
| 4142 | } |
| 4143 | |
| 4144 | /* Print information about each overload candidate in CANDIDATES, |
| 4145 | which is assumed to have gone through splice_viable and tourney |
| 4146 | (if splice_viable succeeded). */ |
| 4147 | |
| 4148 | static void |
| 4149 | print_z_candidates (location_t loc, struct z_candidate *candidates, |
| 4150 | tristate only_viable_p /* = tristate::unknown () */) |
| 4151 | { |
| 4152 | struct z_candidate *cand1; |
| 4153 | struct z_candidate **cand2; |
| 4154 | |
| 4155 | if (!candidates) |
| 4156 | return; |
| 4157 | |
| 4158 | /* Remove non-viable deleted candidates. */ |
| 4159 | cand1 = candidates; |
| 4160 | for (cand2 = &cand1; *cand2; ) |
| 4161 | { |
| 4162 | if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL |
| 4163 | && !(*cand2)->viable |
| 4164 | && DECL_DELETED_FN ((*cand2)->fn)) |
| 4165 | *cand2 = (*cand2)->next; |
| 4166 | else |
| 4167 | cand2 = &(*cand2)->next; |
| 4168 | } |
| 4169 | /* ...if there are any non-deleted ones. */ |
| 4170 | if (cand1) |
| 4171 | candidates = cand1; |
| 4172 | |
| 4173 | /* There may be duplicates in the set of candidates. We put off |
| 4174 | checking this condition as long as possible, since we have no way |
| 4175 | to eliminate duplicates from a set of functions in less than n^2 |
| 4176 | time. Now we are about to emit an error message, so it is more |
| 4177 | permissible to go slowly. */ |
| 4178 | for (cand1 = candidates; cand1; cand1 = cand1->next) |
| 4179 | { |
| 4180 | tree fn = cand1->fn; |
| 4181 | /* Skip builtin candidates and conversion functions. */ |
| 4182 | if (!DECL_P (fn)) |
| 4183 | continue; |
| 4184 | cand2 = &cand1->next; |
| 4185 | while (*cand2) |
| 4186 | { |
| 4187 | if (DECL_P ((*cand2)->fn) |
| 4188 | && equal_functions (fn1: fn, fn2: (*cand2)->fn)) |
| 4189 | *cand2 = (*cand2)->next; |
| 4190 | else |
| 4191 | cand2 = &(*cand2)->next; |
| 4192 | } |
| 4193 | } |
| 4194 | |
| 4195 | /* Unless otherwise specified, if there's a (strictly) viable candidate |
| 4196 | then we assume we're being called as part of diagnosing ambiguity, in |
| 4197 | which case we want to print only viable candidates since non-viable |
| 4198 | candidates couldn't have contributed to the ambiguity. */ |
| 4199 | if (only_viable_p.is_unknown ()) |
| 4200 | only_viable_p = candidates->viable == 1; |
| 4201 | |
| 4202 | auto_diagnostic_nesting_level sentinel; |
| 4203 | |
| 4204 | int num_candidates = 0; |
| 4205 | for (auto iter = candidates; iter; iter = iter->next) |
| 4206 | ++num_candidates; |
| 4207 | |
| 4208 | inform_n (loc, |
| 4209 | num_candidates, "there is %i candidate" , "there are %i candidates" , |
| 4210 | num_candidates); |
| 4211 | auto_diagnostic_nesting_level sentinel2; |
| 4212 | |
| 4213 | int candidate_idx = 0; |
| 4214 | for (; candidates; candidates = candidates->next) |
| 4215 | { |
| 4216 | if (only_viable_p.is_true () && candidates->viable != 1) |
| 4217 | break; |
| 4218 | if (ignored_candidate_p (cand: candidates) && !flag_diagnostics_all_candidates) |
| 4219 | { |
| 4220 | inform (loc, "some candidates omitted; " |
| 4221 | "use %<-fdiagnostics-all-candidates%> to display them" ); |
| 4222 | break; |
| 4223 | } |
| 4224 | pretty_printer pp; |
| 4225 | pp_printf (&pp, N_("candidate %i:" ), candidate_idx + 1); |
| 4226 | const char *const msgstr = pp_formatted_text (&pp); |
| 4227 | print_z_candidate (loc, msgstr, candidate: candidates); |
| 4228 | ++candidate_idx; |
| 4229 | } |
| 4230 | } |
| 4231 | |
| 4232 | /* USER_SEQ is a user-defined conversion sequence, beginning with a |
| 4233 | USER_CONV. STD_SEQ is the standard conversion sequence applied to |
| 4234 | the result of the conversion function to convert it to the final |
| 4235 | desired type. Merge the two sequences into a single sequence, |
| 4236 | and return the merged sequence. */ |
| 4237 | |
| 4238 | static conversion * |
| 4239 | merge_conversion_sequences (conversion *user_seq, conversion *std_seq) |
| 4240 | { |
| 4241 | conversion **t; |
| 4242 | bool bad = user_seq->bad_p; |
| 4243 | |
| 4244 | gcc_assert (user_seq->kind == ck_user); |
| 4245 | |
| 4246 | /* Find the end of the second conversion sequence. */ |
| 4247 | for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next)) |
| 4248 | { |
| 4249 | /* The entire sequence is a user-conversion sequence. */ |
| 4250 | (*t)->user_conv_p = true; |
| 4251 | if (bad) |
| 4252 | (*t)->bad_p = true; |
| 4253 | } |
| 4254 | |
| 4255 | if ((*t)->rvaluedness_matches_p) |
| 4256 | /* We're binding a reference directly to the result of the conversion. |
| 4257 | build_user_type_conversion_1 stripped the REFERENCE_TYPE from the return |
| 4258 | type, but we want it back. */ |
| 4259 | user_seq->type = TREE_TYPE (TREE_TYPE (user_seq->cand->fn)); |
| 4260 | |
| 4261 | /* Replace the identity conversion with the user conversion |
| 4262 | sequence. */ |
| 4263 | *t = user_seq; |
| 4264 | |
| 4265 | return std_seq; |
| 4266 | } |
| 4267 | |
| 4268 | /* Handle overload resolution for initializing an object of class type from |
| 4269 | an initializer list. First we look for a suitable constructor that |
| 4270 | takes a std::initializer_list; if we don't find one, we then look for a |
| 4271 | non-list constructor. |
| 4272 | |
| 4273 | Parameters are as for add_candidates, except that the arguments are in |
| 4274 | the form of a CONSTRUCTOR (the initializer list) rather than a vector, and |
| 4275 | the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */ |
| 4276 | |
| 4277 | static void |
| 4278 | add_list_candidates (tree fns, tree first_arg, |
| 4279 | const vec<tree, va_gc> *args, tree totype, |
| 4280 | tree explicit_targs, bool template_only, |
| 4281 | tree conversion_path, tree access_path, |
| 4282 | int flags, |
| 4283 | struct z_candidate **candidates, |
| 4284 | tsubst_flags_t complain) |
| 4285 | { |
| 4286 | gcc_assert (*candidates == NULL); |
| 4287 | |
| 4288 | /* We're looking for a ctor for list-initialization. */ |
| 4289 | flags |= LOOKUP_LIST_INIT_CTOR; |
| 4290 | /* And we don't allow narrowing conversions. We also use this flag to |
| 4291 | avoid the copy constructor call for copy-list-initialization. */ |
| 4292 | flags |= LOOKUP_NO_NARROWING; |
| 4293 | |
| 4294 | unsigned nart = num_artificial_parms_for (OVL_FIRST (fns)) - 1; |
| 4295 | tree init_list = (*args)[nart]; |
| 4296 | |
| 4297 | /* Always use the default constructor if the list is empty (DR 990). */ |
| 4298 | if (CONSTRUCTOR_NELTS (init_list) == 0 |
| 4299 | && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype)) |
| 4300 | ; |
| 4301 | else if (CONSTRUCTOR_IS_DESIGNATED_INIT (init_list) |
| 4302 | && !CP_AGGREGATE_TYPE_P (totype)) |
| 4303 | { |
| 4304 | if (complain & tf_error) |
| 4305 | error ("designated initializers cannot be used with a " |
| 4306 | "non-aggregate type %qT" , totype); |
| 4307 | return; |
| 4308 | } |
| 4309 | /* If the class has a list ctor, try passing the list as a single |
| 4310 | argument first, but only consider list ctors. */ |
| 4311 | else if (TYPE_HAS_LIST_CTOR (totype)) |
| 4312 | { |
| 4313 | flags |= LOOKUP_LIST_ONLY; |
| 4314 | add_candidates (fns, first_arg, args, NULL_TREE, |
| 4315 | explicit_targs, template_only, conversion_path, |
| 4316 | access_path, flags, candidates, complain); |
| 4317 | if (any_strictly_viable (cands: *candidates)) |
| 4318 | return; |
| 4319 | } |
| 4320 | |
| 4321 | /* Expand the CONSTRUCTOR into a new argument vec. */ |
| 4322 | vec<tree, va_gc> *new_args; |
| 4323 | vec_alloc (v&: new_args, nelems: nart + CONSTRUCTOR_NELTS (init_list)); |
| 4324 | for (unsigned i = 0; i < nart; ++i) |
| 4325 | new_args->quick_push (obj: (*args)[i]); |
| 4326 | new_args = append_ctor_to_tree_vector (new_args, init_list); |
| 4327 | |
| 4328 | /* We aren't looking for list-ctors anymore. */ |
| 4329 | flags &= ~LOOKUP_LIST_ONLY; |
| 4330 | /* We allow more user-defined conversions within an init-list. */ |
| 4331 | flags &= ~LOOKUP_NO_CONVERSION; |
| 4332 | |
| 4333 | add_candidates (fns, first_arg, new_args, NULL_TREE, |
| 4334 | explicit_targs, template_only, conversion_path, |
| 4335 | access_path, flags, candidates, complain); |
| 4336 | } |
| 4337 | |
| 4338 | /* Given C(std::initializer_list<A>), return A. */ |
| 4339 | |
| 4340 | static tree |
| 4341 | list_ctor_element_type (tree fn) |
| 4342 | { |
| 4343 | gcc_checking_assert (is_list_ctor (fn)); |
| 4344 | |
| 4345 | tree parm = FUNCTION_FIRST_USER_PARMTYPE (fn); |
| 4346 | parm = non_reference (TREE_VALUE (parm)); |
| 4347 | return TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0); |
| 4348 | } |
| 4349 | |
| 4350 | /* If EXPR is a braced-init-list where the elements all decay to the same type, |
| 4351 | return that type. */ |
| 4352 | |
| 4353 | static tree |
| 4354 | braced_init_element_type (tree expr) |
| 4355 | { |
| 4356 | if (TREE_CODE (expr) == CONSTRUCTOR |
| 4357 | && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE) |
| 4358 | return TREE_TYPE (TREE_TYPE (expr)); |
| 4359 | if (!BRACE_ENCLOSED_INITIALIZER_P (expr)) |
| 4360 | return NULL_TREE; |
| 4361 | |
| 4362 | tree elttype = NULL_TREE; |
| 4363 | for (constructor_elt &e: CONSTRUCTOR_ELTS (expr)) |
| 4364 | { |
| 4365 | tree type = TREE_TYPE (e.value); |
| 4366 | type = type_decays_to (type); |
| 4367 | if (!elttype) |
| 4368 | elttype = type; |
| 4369 | else if (!same_type_p (type, elttype)) |
| 4370 | return NULL_TREE; |
| 4371 | } |
| 4372 | return elttype; |
| 4373 | } |
| 4374 | |
| 4375 | /* True iff EXPR contains any temporaries with non-trivial destruction. |
| 4376 | |
| 4377 | ??? Also ignore classes with non-trivial but no-op destruction other than |
| 4378 | std::allocator? */ |
| 4379 | |
| 4380 | static bool |
| 4381 | has_non_trivial_temporaries (tree expr) |
| 4382 | { |
| 4383 | auto_vec<tree*> temps; |
| 4384 | cp_walk_tree_without_duplicates (&expr, find_temps_r, &temps); |
| 4385 | for (tree *p : temps) |
| 4386 | { |
| 4387 | tree t = TREE_TYPE (*p); |
| 4388 | if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (t) |
| 4389 | && !is_std_allocator (t)) |
| 4390 | return true; |
| 4391 | } |
| 4392 | return false; |
| 4393 | } |
| 4394 | |
| 4395 | /* Return number of initialized elements in CTOR. */ |
| 4396 | |
| 4397 | unsigned HOST_WIDE_INT |
| 4398 | count_ctor_elements (tree ctor) |
| 4399 | { |
| 4400 | unsigned HOST_WIDE_INT len = 0; |
| 4401 | for (constructor_elt &e: CONSTRUCTOR_ELTS (ctor)) |
| 4402 | if (TREE_CODE (e.value) == RAW_DATA_CST) |
| 4403 | len += RAW_DATA_LENGTH (e.value); |
| 4404 | else |
| 4405 | ++len; |
| 4406 | return len; |
| 4407 | } |
| 4408 | |
| 4409 | /* We're initializing an array of ELTTYPE from INIT. If it seems useful, |
| 4410 | return INIT as an array (of its own type) so the caller can initialize the |
| 4411 | target array in a loop. */ |
| 4412 | |
| 4413 | static tree |
| 4414 | maybe_init_list_as_array (tree elttype, tree init) |
| 4415 | { |
| 4416 | /* Only do this if the array can go in rodata but not once converted. */ |
| 4417 | if (!TYPE_NON_AGGREGATE_CLASS (elttype)) |
| 4418 | return NULL_TREE; |
| 4419 | tree init_elttype = braced_init_element_type (expr: init); |
| 4420 | if (!init_elttype || !SCALAR_TYPE_P (init_elttype) || !TREE_CONSTANT (init)) |
| 4421 | return NULL_TREE; |
| 4422 | |
| 4423 | /* Check with a stub expression to weed out special cases, and check whether |
| 4424 | we call the same function for direct-init as copy-list-init. */ |
| 4425 | conversion_obstack_sentinel cos; |
| 4426 | init_elttype = cp_build_qualified_type (init_elttype, TYPE_QUAL_CONST); |
| 4427 | tree arg = build_stub_object (init_elttype); |
| 4428 | conversion *c = implicit_conversion (to: elttype, from: init_elttype, expr: arg, c_cast_p: false, |
| 4429 | LOOKUP_NORMAL, complain: tf_none); |
| 4430 | if (c && c->kind == ck_rvalue) |
| 4431 | c = next_conversion (conv: c); |
| 4432 | if (!c || c->kind != ck_user) |
| 4433 | return NULL_TREE; |
| 4434 | /* Check that we actually can perform the conversion. */ |
| 4435 | if (convert_like (c, arg, tf_none) == error_mark_node) |
| 4436 | /* Let the normal code give the error. */ |
| 4437 | return NULL_TREE; |
| 4438 | |
| 4439 | /* A glvalue initializer might be significant to a reference constructor |
| 4440 | or conversion operator. */ |
| 4441 | if (!DECL_CONSTRUCTOR_P (c->cand->fn) |
| 4442 | || (TYPE_REF_P (TREE_VALUE |
| 4443 | (FUNCTION_FIRST_USER_PARMTYPE (c->cand->fn))))) |
| 4444 | for (auto &ce : CONSTRUCTOR_ELTS (init)) |
| 4445 | if (non_mergeable_glvalue_p (ce.value)) |
| 4446 | return NULL_TREE; |
| 4447 | |
| 4448 | tree first = CONSTRUCTOR_ELT (init, 0)->value; |
| 4449 | conversion *fc = implicit_conversion (to: elttype, from: init_elttype, expr: first, c_cast_p: false, |
| 4450 | LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING, |
| 4451 | complain: tf_none); |
| 4452 | if (fc && fc->kind == ck_rvalue) |
| 4453 | fc = next_conversion (conv: fc); |
| 4454 | if (!fc || fc->kind != ck_user || fc->cand->fn != c->cand->fn) |
| 4455 | return NULL_TREE; |
| 4456 | first = convert_like (fc, first, tf_none); |
| 4457 | if (first == error_mark_node) |
| 4458 | /* Let the normal code give the error. */ |
| 4459 | return NULL_TREE; |
| 4460 | |
| 4461 | /* Don't do this if the conversion would be constant. */ |
| 4462 | first = maybe_constant_init (first); |
| 4463 | if (TREE_CONSTANT (first)) |
| 4464 | return NULL_TREE; |
| 4465 | |
| 4466 | /* We can't do this if the conversion creates temporaries that need |
| 4467 | to live until the whole array is initialized. */ |
| 4468 | if (has_non_trivial_temporaries (expr: first)) |
| 4469 | return NULL_TREE; |
| 4470 | |
| 4471 | /* We can't do this if copying from the initializer_list would be |
| 4472 | ill-formed. */ |
| 4473 | tree copy_argtypes = make_tree_vec (1); |
| 4474 | TREE_VEC_ELT (copy_argtypes, 0) |
| 4475 | = cp_build_qualified_type (elttype, TYPE_QUAL_CONST); |
| 4476 | if (!is_xible (INIT_EXPR, elttype, copy_argtypes)) |
| 4477 | return NULL_TREE; |
| 4478 | |
| 4479 | unsigned HOST_WIDE_INT len = count_ctor_elements (ctor: init); |
| 4480 | tree arr = build_array_of_n_type (init_elttype, len); |
| 4481 | arr = finish_compound_literal (arr, init, tf_none); |
| 4482 | DECL_MERGEABLE (TARGET_EXPR_SLOT (arr)) = true; |
| 4483 | return arr; |
| 4484 | } |
| 4485 | |
| 4486 | /* If we were going to call e.g. vector(initializer_list<string>) starting |
| 4487 | with a list of string-literals (which is inefficient, see PR105838), |
| 4488 | instead build an array of const char* and pass it to the range constructor. |
| 4489 | But only do this for standard library types, where we can assume the |
| 4490 | transformation makes sense. |
| 4491 | |
| 4492 | Really the container classes should have initializer_list<U> constructors to |
| 4493 | get the same effect more simply; this is working around that lack. */ |
| 4494 | |
| 4495 | static tree |
| 4496 | maybe_init_list_as_range (tree fn, tree expr) |
| 4497 | { |
| 4498 | if (!processing_template_decl |
| 4499 | && BRACE_ENCLOSED_INITIALIZER_P (expr) |
| 4500 | && is_list_ctor (fn) |
| 4501 | && decl_in_std_namespace_p (fn)) |
| 4502 | { |
| 4503 | tree to = list_ctor_element_type (fn); |
| 4504 | if (tree init = maybe_init_list_as_array (elttype: to, init: expr)) |
| 4505 | { |
| 4506 | tree begin = decay_conversion (TARGET_EXPR_SLOT (init), tf_none); |
| 4507 | tree nelts = array_type_nelts_top (TREE_TYPE (init)); |
| 4508 | tree end = cp_build_binary_op (input_location, PLUS_EXPR, begin, |
| 4509 | nelts, tf_none); |
| 4510 | begin = cp_build_compound_expr (init, begin, tf_none); |
| 4511 | return build_constructor_va (init_list_type_node, 2, |
| 4512 | NULL_TREE, begin, NULL_TREE, end); |
| 4513 | } |
| 4514 | } |
| 4515 | |
| 4516 | return NULL_TREE; |
| 4517 | } |
| 4518 | |
| 4519 | /* Returns the best overload candidate to perform the requested |
| 4520 | conversion. This function is used for three the overloading situations |
| 4521 | described in [over.match.copy], [over.match.conv], and [over.match.ref]. |
| 4522 | If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as |
| 4523 | per [dcl.init.ref], so we ignore temporary bindings. */ |
| 4524 | |
| 4525 | static struct z_candidate * |
| 4526 | build_user_type_conversion_1 (tree totype, tree expr, int flags, |
| 4527 | tsubst_flags_t complain) |
| 4528 | { |
| 4529 | struct z_candidate *candidates, *cand; |
| 4530 | tree fromtype; |
| 4531 | tree ctors = NULL_TREE; |
| 4532 | tree conv_fns = NULL_TREE; |
| 4533 | conversion *conv = NULL; |
| 4534 | tree first_arg = NULL_TREE; |
| 4535 | vec<tree, va_gc> *args = NULL; |
| 4536 | bool any_viable_p; |
| 4537 | int convflags; |
| 4538 | |
| 4539 | if (!expr) |
| 4540 | return NULL; |
| 4541 | |
| 4542 | fromtype = TREE_TYPE (expr); |
| 4543 | |
| 4544 | /* We represent conversion within a hierarchy using RVALUE_CONV and |
| 4545 | BASE_CONV, as specified by [over.best.ics]; these become plain |
| 4546 | constructor calls, as specified in [dcl.init]. */ |
| 4547 | gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype) |
| 4548 | || !DERIVED_FROM_P (totype, fromtype)); |
| 4549 | |
| 4550 | if (CLASS_TYPE_P (totype)) |
| 4551 | /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid |
| 4552 | creating a garbage BASELINK; constructors can't be inherited. */ |
| 4553 | ctors = get_class_binding (totype, complete_ctor_identifier); |
| 4554 | |
| 4555 | tree to_nonref = non_reference (totype); |
| 4556 | if (MAYBE_CLASS_TYPE_P (fromtype)) |
| 4557 | { |
| 4558 | if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) || |
| 4559 | (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype) |
| 4560 | && DERIVED_FROM_P (to_nonref, fromtype))) |
| 4561 | { |
| 4562 | /* [class.conv.fct] A conversion function is never used to |
| 4563 | convert a (possibly cv-qualified) object to the (possibly |
| 4564 | cv-qualified) same object type (or a reference to it), to a |
| 4565 | (possibly cv-qualified) base class of that type (or a |
| 4566 | reference to it)... */ |
| 4567 | } |
| 4568 | else |
| 4569 | conv_fns = lookup_conversions (fromtype); |
| 4570 | } |
| 4571 | |
| 4572 | candidates = 0; |
| 4573 | flags |= LOOKUP_NO_CONVERSION; |
| 4574 | if (BRACE_ENCLOSED_INITIALIZER_P (expr)) |
| 4575 | flags |= LOOKUP_NO_NARROWING; |
| 4576 | /* Prevent add_candidates from treating a non-strictly viable candidate |
| 4577 | as unviable. */ |
| 4578 | complain |= tf_conv; |
| 4579 | |
| 4580 | /* It's OK to bind a temporary for converting constructor arguments, but |
| 4581 | not in converting the return value of a conversion operator. */ |
| 4582 | convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION |
| 4583 | | (flags & LOOKUP_NO_NARROWING)); |
| 4584 | flags &= ~LOOKUP_NO_TEMP_BIND; |
| 4585 | |
| 4586 | if (ctors) |
| 4587 | { |
| 4588 | int ctorflags = flags; |
| 4589 | |
| 4590 | first_arg = build_dummy_object (totype); |
| 4591 | |
| 4592 | /* We should never try to call the abstract or base constructor |
| 4593 | from here. */ |
| 4594 | gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors)) |
| 4595 | && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors))); |
| 4596 | |
| 4597 | args = make_tree_vector_single (expr); |
| 4598 | if (BRACE_ENCLOSED_INITIALIZER_P (expr)) |
| 4599 | { |
| 4600 | /* List-initialization. */ |
| 4601 | add_list_candidates (fns: ctors, first_arg, args, totype, NULL_TREE, |
| 4602 | template_only: false, TYPE_BINFO (totype), TYPE_BINFO (totype), |
| 4603 | flags: ctorflags, candidates: &candidates, complain); |
| 4604 | } |
| 4605 | else |
| 4606 | { |
| 4607 | add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false, |
| 4608 | TYPE_BINFO (totype), TYPE_BINFO (totype), |
| 4609 | ctorflags, &candidates, complain); |
| 4610 | } |
| 4611 | |
| 4612 | for (cand = candidates; cand; cand = cand->next) |
| 4613 | { |
| 4614 | cand->second_conv = build_identity_conv (type: totype, NULL_TREE); |
| 4615 | |
| 4616 | /* If totype isn't a reference, and LOOKUP_ONLYCONVERTING is |
| 4617 | set, then this is copy-initialization. In that case, "The |
| 4618 | result of the call is then used to direct-initialize the |
| 4619 | object that is the destination of the copy-initialization." |
| 4620 | [dcl.init] |
| 4621 | |
| 4622 | We represent this in the conversion sequence with an |
| 4623 | rvalue conversion, which means a constructor call. */ |
| 4624 | if (!TYPE_REF_P (totype) |
| 4625 | && cxx_dialect < cxx17 |
| 4626 | && (flags & LOOKUP_ONLYCONVERTING) |
| 4627 | && !(convflags & LOOKUP_NO_TEMP_BIND)) |
| 4628 | cand->second_conv |
| 4629 | = build_conv (code: ck_rvalue, type: totype, from: cand->second_conv); |
| 4630 | } |
| 4631 | } |
| 4632 | |
| 4633 | if (conv_fns) |
| 4634 | { |
| 4635 | if (BRACE_ENCLOSED_INITIALIZER_P (expr)) |
| 4636 | first_arg = CONSTRUCTOR_ELT (expr, 0)->value; |
| 4637 | else |
| 4638 | first_arg = expr; |
| 4639 | } |
| 4640 | |
| 4641 | for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns)) |
| 4642 | { |
| 4643 | tree conversion_path = TREE_PURPOSE (conv_fns); |
| 4644 | struct z_candidate *old_candidates; |
| 4645 | |
| 4646 | /* If LOOKUP_NO_CONVERSION, don't consider a conversion function that |
| 4647 | would need an addional user-defined conversion, i.e. if the return |
| 4648 | type differs in class-ness from the desired type. So we avoid |
| 4649 | considering operator bool when calling a copy constructor. |
| 4650 | |
| 4651 | This optimization avoids the failure in PR97600, and is allowed by |
| 4652 | [temp.inst]/9: "If the function selected by overload resolution can be |
| 4653 | determined without instantiating a class template definition, it is |
| 4654 | unspecified whether that instantiation actually takes place." */ |
| 4655 | tree convtype = non_reference (TREE_TYPE (conv_fns)); |
| 4656 | if ((flags & LOOKUP_NO_CONVERSION) |
| 4657 | && !WILDCARD_TYPE_P (convtype) |
| 4658 | && (CLASS_TYPE_P (to_nonref) |
| 4659 | != CLASS_TYPE_P (convtype))) |
| 4660 | continue; |
| 4661 | |
| 4662 | /* If we are called to convert to a reference type, we are trying to |
| 4663 | find a direct binding, so don't even consider temporaries. If |
| 4664 | we don't find a direct binding, the caller will try again to |
| 4665 | look for a temporary binding. */ |
| 4666 | if (TYPE_REF_P (totype)) |
| 4667 | convflags |= LOOKUP_NO_TEMP_BIND; |
| 4668 | |
| 4669 | old_candidates = candidates; |
| 4670 | add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype, |
| 4671 | NULL_TREE, false, |
| 4672 | conversion_path, TYPE_BINFO (fromtype), |
| 4673 | flags, &candidates, complain); |
| 4674 | |
| 4675 | for (cand = candidates; cand != old_candidates; cand = cand->next) |
| 4676 | { |
| 4677 | if (cand->viable == 0) |
| 4678 | /* Already rejected, don't change to -1. */ |
| 4679 | continue; |
| 4680 | |
| 4681 | tree rettype = TREE_TYPE (TREE_TYPE (cand->fn)); |
| 4682 | conversion *ics |
| 4683 | = implicit_conversion (to: totype, |
| 4684 | from: rettype, |
| 4685 | expr: 0, |
| 4686 | /*c_cast_p=*/false, flags: convflags, |
| 4687 | complain); |
| 4688 | |
| 4689 | /* If LOOKUP_NO_TEMP_BIND isn't set, then this is |
| 4690 | copy-initialization. In that case, "The result of the |
| 4691 | call is then used to direct-initialize the object that is |
| 4692 | the destination of the copy-initialization." [dcl.init] |
| 4693 | |
| 4694 | We represent this in the conversion sequence with an |
| 4695 | rvalue conversion, which means a constructor call. But |
| 4696 | don't add a second rvalue conversion if there's already |
| 4697 | one there. Which there really shouldn't be, but it's |
| 4698 | harmless since we'd add it here anyway. */ |
| 4699 | if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue |
| 4700 | && !(convflags & LOOKUP_NO_TEMP_BIND)) |
| 4701 | ics = build_conv (code: ck_rvalue, type: totype, from: ics); |
| 4702 | |
| 4703 | cand->second_conv = ics; |
| 4704 | |
| 4705 | if (!ics) |
| 4706 | { |
| 4707 | cand->viable = 0; |
| 4708 | cand->reason = arg_conversion_rejection (NULL_TREE, n_arg: -2, |
| 4709 | from: rettype, to: totype, |
| 4710 | EXPR_LOCATION (expr)); |
| 4711 | } |
| 4712 | else if (TYPE_REF_P (totype) && !ics->rvaluedness_matches_p |
| 4713 | /* Limit this to non-templates for now (PR90546). */ |
| 4714 | && !cand->template_decl |
| 4715 | && TREE_CODE (TREE_TYPE (totype)) != FUNCTION_TYPE) |
| 4716 | { |
| 4717 | /* If we are called to convert to a reference type, we are trying |
| 4718 | to find a direct binding per [over.match.ref], so rvaluedness |
| 4719 | must match for non-functions. */ |
| 4720 | cand->viable = 0; |
| 4721 | } |
| 4722 | else if (DECL_NONCONVERTING_P (cand->fn) |
| 4723 | && ics->rank > cr_exact) |
| 4724 | { |
| 4725 | /* 13.3.1.5: For direct-initialization, those explicit |
| 4726 | conversion functions that are not hidden within S and |
| 4727 | yield type T or a type that can be converted to type T |
| 4728 | with a qualification conversion (4.4) are also candidate |
| 4729 | functions. */ |
| 4730 | /* 13.3.1.6 doesn't have a parallel restriction, but it should; |
| 4731 | I've raised this issue with the committee. --jason 9/2011 */ |
| 4732 | cand->viable = -1; |
| 4733 | cand->reason = explicit_conversion_rejection (from: rettype, to: totype); |
| 4734 | } |
| 4735 | else if (cand->viable == 1 && ics->bad_p) |
| 4736 | { |
| 4737 | cand->viable = -1; |
| 4738 | cand->reason |
| 4739 | = bad_arg_conversion_rejection (NULL_TREE, n_arg: -2, |
| 4740 | from: rettype, to: totype, |
| 4741 | EXPR_LOCATION (expr)); |
| 4742 | } |
| 4743 | else if (primary_template_specialization_p (cand->fn) |
| 4744 | && ics->rank > cr_exact) |
| 4745 | { |
| 4746 | /* 13.3.3.1.2: If the user-defined conversion is specified by |
| 4747 | a specialization of a conversion function template, the |
| 4748 | second standard conversion sequence shall have exact match |
| 4749 | rank. */ |
| 4750 | cand->viable = -1; |
| 4751 | cand->reason = template_conversion_rejection (from: rettype, to: totype); |
| 4752 | } |
| 4753 | } |
| 4754 | } |
| 4755 | |
| 4756 | candidates = splice_viable (cands: candidates, strict_p: false, any_viable_p: &any_viable_p); |
| 4757 | if (!any_viable_p) |
| 4758 | { |
| 4759 | if (args) |
| 4760 | release_tree_vector (args); |
| 4761 | return NULL; |
| 4762 | } |
| 4763 | |
| 4764 | cand = tourney (candidates, complain); |
| 4765 | if (cand == NULL) |
| 4766 | { |
| 4767 | if (complain & tf_error) |
| 4768 | { |
| 4769 | auto_diagnostic_group d; |
| 4770 | error_at (cp_expr_loc_or_input_loc (t: expr), |
| 4771 | "conversion from %qH to %qI is ambiguous" , |
| 4772 | fromtype, totype); |
| 4773 | print_z_candidates (loc: location_of (expr), candidates); |
| 4774 | } |
| 4775 | |
| 4776 | cand = candidates; /* any one will do */ |
| 4777 | cand->second_conv = build_ambiguous_conv (type: totype, expr); |
| 4778 | cand->second_conv->user_conv_p = true; |
| 4779 | if (!any_strictly_viable (cands: candidates)) |
| 4780 | cand->second_conv->bad_p = true; |
| 4781 | if (flags & LOOKUP_ONLYCONVERTING) |
| 4782 | cand->second_conv->need_temporary_p = true; |
| 4783 | /* If there are viable candidates, don't set ICS_BAD_FLAG; an |
| 4784 | ambiguous conversion is no worse than another user-defined |
| 4785 | conversion. */ |
| 4786 | |
| 4787 | return cand; |
| 4788 | } |
| 4789 | |
| 4790 | /* Maybe pass { } as iterators instead of an initializer_list. */ |
| 4791 | if (tree iters = maybe_init_list_as_range (fn: cand->fn, expr)) |
| 4792 | if (z_candidate *cand2 |
| 4793 | = build_user_type_conversion_1 (totype, expr: iters, flags, complain: tf_none)) |
| 4794 | if (cand2->viable == 1 && !is_list_ctor (cand2->fn)) |
| 4795 | { |
| 4796 | cand = cand2; |
| 4797 | expr = iters; |
| 4798 | } |
| 4799 | |
| 4800 | tree convtype; |
| 4801 | if (!DECL_CONSTRUCTOR_P (cand->fn)) |
| 4802 | convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn))); |
| 4803 | else if (cand->second_conv->kind == ck_rvalue) |
| 4804 | /* DR 5: [in the first step of copy-initialization]...if the function |
| 4805 | is a constructor, the call initializes a temporary of the |
| 4806 | cv-unqualified version of the destination type. */ |
| 4807 | convtype = cv_unqualified (totype); |
| 4808 | else |
| 4809 | convtype = totype; |
| 4810 | /* Build the user conversion sequence. */ |
| 4811 | conv = build_conv |
| 4812 | (code: ck_user, |
| 4813 | type: convtype, |
| 4814 | from: build_identity_conv (TREE_TYPE (expr), expr)); |
| 4815 | conv->cand = cand; |
| 4816 | if (cand->viable == -1) |
| 4817 | conv->bad_p = true; |
| 4818 | |
| 4819 | /* Remember that this was a list-initialization. */ |
| 4820 | if (flags & LOOKUP_NO_NARROWING) |
| 4821 | conv->check_narrowing = true; |
| 4822 | |
| 4823 | /* Combine it with the second conversion sequence. */ |
| 4824 | cand->second_conv = merge_conversion_sequences (user_seq: conv, |
| 4825 | std_seq: cand->second_conv); |
| 4826 | |
| 4827 | return cand; |
| 4828 | } |
| 4829 | |
| 4830 | /* Wrapper for above. */ |
| 4831 | |
| 4832 | tree |
| 4833 | build_user_type_conversion (tree totype, tree expr, int flags, |
| 4834 | tsubst_flags_t complain) |
| 4835 | { |
| 4836 | struct z_candidate *cand; |
| 4837 | tree ret; |
| 4838 | |
| 4839 | auto_cond_timevar tv (TV_OVERLOAD); |
| 4840 | |
| 4841 | conversion_obstack_sentinel cos; |
| 4842 | |
| 4843 | cand = build_user_type_conversion_1 (totype, expr, flags, complain); |
| 4844 | |
| 4845 | if (cand) |
| 4846 | { |
| 4847 | if (cand->second_conv->kind == ck_ambig) |
| 4848 | ret = error_mark_node; |
| 4849 | else |
| 4850 | { |
| 4851 | expr = convert_like (cand->second_conv, expr, complain); |
| 4852 | ret = convert_from_reference (expr); |
| 4853 | } |
| 4854 | } |
| 4855 | else |
| 4856 | ret = NULL_TREE; |
| 4857 | |
| 4858 | return ret; |
| 4859 | } |
| 4860 | |
| 4861 | /* Give a helpful diagnostic when implicit_conversion fails. */ |
| 4862 | |
| 4863 | static void |
| 4864 | implicit_conversion_error (location_t loc, tree type, tree expr) |
| 4865 | { |
| 4866 | tsubst_flags_t complain = tf_warning_or_error; |
| 4867 | |
| 4868 | /* If expr has unknown type, then it is an overloaded function. |
| 4869 | Call instantiate_type to get good error messages. */ |
| 4870 | if (TREE_TYPE (expr) == unknown_type_node) |
| 4871 | instantiate_type (type, expr, complain); |
| 4872 | else if (invalid_nonstatic_memfn_p (loc, expr, complain)) |
| 4873 | /* We gave an error. */; |
| 4874 | else if (BRACE_ENCLOSED_INITIALIZER_P (expr) |
| 4875 | && CONSTRUCTOR_IS_DESIGNATED_INIT (expr) |
| 4876 | && !CP_AGGREGATE_TYPE_P (type)) |
| 4877 | error_at (loc, "designated initializers cannot be used with a " |
| 4878 | "non-aggregate type %qT" , type); |
| 4879 | else |
| 4880 | { |
| 4881 | range_label_for_type_mismatch label (TREE_TYPE (expr), type); |
| 4882 | gcc_rich_location rich_loc (loc, &label, |
| 4883 | highlight_colors::percent_h); |
| 4884 | error_at (&rich_loc, "could not convert %qE from %qH to %qI" , |
| 4885 | expr, TREE_TYPE (expr), type); |
| 4886 | } |
| 4887 | } |
| 4888 | |
| 4889 | /* Worker for build_converted_constant_expr. */ |
| 4890 | |
| 4891 | static tree |
| 4892 | build_converted_constant_expr_internal (tree type, tree expr, |
| 4893 | int flags, tsubst_flags_t complain) |
| 4894 | { |
| 4895 | conversion *conv; |
| 4896 | tree t; |
| 4897 | location_t loc = cp_expr_loc_or_input_loc (t: expr); |
| 4898 | |
| 4899 | if (error_operand_p (t: expr)) |
| 4900 | return error_mark_node; |
| 4901 | |
| 4902 | conversion_obstack_sentinel cos; |
| 4903 | |
| 4904 | conv = implicit_conversion (to: type, TREE_TYPE (expr), expr, |
| 4905 | /*c_cast_p=*/false, flags, complain); |
| 4906 | |
| 4907 | /* A converted constant expression of type T is an expression, implicitly |
| 4908 | converted to type T, where the converted expression is a constant |
| 4909 | expression and the implicit conversion sequence contains only |
| 4910 | |
| 4911 | * user-defined conversions, |
| 4912 | * lvalue-to-rvalue conversions (7.1), |
| 4913 | * array-to-pointer conversions (7.2), |
| 4914 | * function-to-pointer conversions (7.3), |
| 4915 | * qualification conversions (7.5), |
| 4916 | * integral promotions (7.6), |
| 4917 | * integral conversions (7.8) other than narrowing conversions (11.6.4), |
| 4918 | * null pointer conversions (7.11) from std::nullptr_t, |
| 4919 | * null member pointer conversions (7.12) from std::nullptr_t, and |
| 4920 | * function pointer conversions (7.13), |
| 4921 | |
| 4922 | and where the reference binding (if any) binds directly. */ |
| 4923 | |
| 4924 | for (conversion *c = conv; |
| 4925 | c && c->kind != ck_identity; |
| 4926 | c = next_conversion (conv: c)) |
| 4927 | { |
| 4928 | switch (c->kind) |
| 4929 | { |
| 4930 | /* A conversion function is OK. If it isn't constexpr, we'll |
| 4931 | complain later that the argument isn't constant. */ |
| 4932 | case ck_user: |
| 4933 | /* List-initialization is OK. */ |
| 4934 | case ck_aggr: |
| 4935 | /* The lvalue-to-rvalue conversion is OK. */ |
| 4936 | case ck_rvalue: |
| 4937 | /* Array-to-pointer and function-to-pointer. */ |
| 4938 | case ck_lvalue: |
| 4939 | /* Function pointer conversions. */ |
| 4940 | case ck_fnptr: |
| 4941 | /* Qualification conversions. */ |
| 4942 | case ck_qual: |
| 4943 | break; |
| 4944 | |
| 4945 | case ck_ref_bind: |
| 4946 | if (c->need_temporary_p) |
| 4947 | { |
| 4948 | if (complain & tf_error) |
| 4949 | error_at (loc, "initializing %qH with %qI in converted " |
| 4950 | "constant expression does not bind directly" , |
| 4951 | type, next_conversion (conv: c)->type); |
| 4952 | conv = NULL; |
| 4953 | } |
| 4954 | break; |
| 4955 | |
| 4956 | case ck_base: |
| 4957 | case ck_pmem: |
| 4958 | case ck_ptr: |
| 4959 | case ck_std: |
| 4960 | t = next_conversion (conv: c)->type; |
| 4961 | if (INTEGRAL_OR_ENUMERATION_TYPE_P (t) |
| 4962 | && INTEGRAL_OR_ENUMERATION_TYPE_P (type)) |
| 4963 | /* Integral promotion or conversion. */ |
| 4964 | break; |
| 4965 | if (NULLPTR_TYPE_P (t)) |
| 4966 | /* Conversion from nullptr to pointer or pointer-to-member. */ |
| 4967 | break; |
| 4968 | |
| 4969 | if (complain & tf_error) |
| 4970 | error_at (loc, "conversion from %qH to %qI in a " |
| 4971 | "converted constant expression" , t, type); |
| 4972 | /* fall through. */ |
| 4973 | |
| 4974 | default: |
| 4975 | conv = NULL; |
| 4976 | break; |
| 4977 | } |
| 4978 | } |
| 4979 | |
| 4980 | /* Avoid confusing convert_nontype_argument by introducing |
| 4981 | a redundant conversion to the same reference type. */ |
| 4982 | if (conv && conv->kind == ck_ref_bind |
| 4983 | && REFERENCE_REF_P (expr)) |
| 4984 | { |
| 4985 | tree ref = TREE_OPERAND (expr, 0); |
| 4986 | if (same_type_p (type, TREE_TYPE (ref))) |
| 4987 | return ref; |
| 4988 | } |
| 4989 | |
| 4990 | if (conv) |
| 4991 | { |
| 4992 | /* Don't copy a class in a template. */ |
| 4993 | if (CLASS_TYPE_P (type) && conv->kind == ck_rvalue |
| 4994 | && processing_template_decl) |
| 4995 | conv = next_conversion (conv); |
| 4996 | |
| 4997 | /* Issuing conversion warnings for value-dependent expressions is |
| 4998 | likely too noisy. */ |
| 4999 | warning_sentinel w (warn_conversion); |
| 5000 | conv->check_narrowing = true; |
| 5001 | conv->check_narrowing_const_only = true; |
| 5002 | expr = convert_like (conv, expr, complain); |
| 5003 | } |
| 5004 | else |
| 5005 | { |
| 5006 | if (complain & tf_error) |
| 5007 | implicit_conversion_error (loc, type, expr); |
| 5008 | expr = error_mark_node; |
| 5009 | } |
| 5010 | |
| 5011 | return expr; |
| 5012 | } |
| 5013 | |
| 5014 | /* Subroutine of convert_nontype_argument. |
| 5015 | |
| 5016 | EXPR is an expression used in a context that requires a converted |
| 5017 | constant-expression, such as a template non-type parameter. Do any |
| 5018 | necessary conversions (that are permitted for converted |
| 5019 | constant-expressions) to convert it to the desired type. |
| 5020 | |
| 5021 | This function doesn't consider explicit conversion functions. If |
| 5022 | you mean to use "a contextually converted constant expression of type |
| 5023 | bool", use build_converted_constant_bool_expr. |
| 5024 | |
| 5025 | If conversion is successful, returns the converted expression; |
| 5026 | otherwise, returns error_mark_node. */ |
| 5027 | |
| 5028 | tree |
| 5029 | build_converted_constant_expr (tree type, tree expr, tsubst_flags_t complain) |
| 5030 | { |
| 5031 | return build_converted_constant_expr_internal (type, expr, LOOKUP_IMPLICIT, |
| 5032 | complain); |
| 5033 | } |
| 5034 | |
| 5035 | /* Used to create "a contextually converted constant expression of type |
| 5036 | bool". This differs from build_converted_constant_expr in that it |
| 5037 | also considers explicit conversion functions. */ |
| 5038 | |
| 5039 | tree |
| 5040 | build_converted_constant_bool_expr (tree expr, tsubst_flags_t complain) |
| 5041 | { |
| 5042 | return build_converted_constant_expr_internal (boolean_type_node, expr, |
| 5043 | LOOKUP_NORMAL, complain); |
| 5044 | } |
| 5045 | |
| 5046 | /* Do any initial processing on the arguments to a function call. */ |
| 5047 | |
| 5048 | vec<tree, va_gc> * |
| 5049 | resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain) |
| 5050 | { |
| 5051 | unsigned int ix; |
| 5052 | tree arg; |
| 5053 | |
| 5054 | FOR_EACH_VEC_SAFE_ELT (args, ix, arg) |
| 5055 | { |
| 5056 | if (error_operand_p (t: arg)) |
| 5057 | return NULL; |
| 5058 | else if (VOID_TYPE_P (TREE_TYPE (arg))) |
| 5059 | { |
| 5060 | if (complain & tf_error) |
| 5061 | error_at (cp_expr_loc_or_input_loc (t: arg), |
| 5062 | "invalid use of void expression" ); |
| 5063 | return NULL; |
| 5064 | } |
| 5065 | else if (invalid_nonstatic_memfn_p (EXPR_LOCATION (arg), arg, complain)) |
| 5066 | return NULL; |
| 5067 | |
| 5068 | /* Force auto deduction now. Omit tf_warning to avoid redundant |
| 5069 | deprecated warning on deprecated-14.C. */ |
| 5070 | if (!mark_single_function (arg, complain & ~tf_warning)) |
| 5071 | return NULL; |
| 5072 | } |
| 5073 | return args; |
| 5074 | } |
| 5075 | |
| 5076 | /* Perform overload resolution on FN, which is called with the ARGS. |
| 5077 | |
| 5078 | Return the candidate function selected by overload resolution, or |
| 5079 | NULL if the event that overload resolution failed. In the case |
| 5080 | that overload resolution fails, *CANDIDATES will be the set of |
| 5081 | candidates considered, and ANY_VIABLE_P will be set to true or |
| 5082 | false to indicate whether or not any of the candidates were |
| 5083 | viable. |
| 5084 | |
| 5085 | The ARGS should already have gone through RESOLVE_ARGS before this |
| 5086 | function is called. */ |
| 5087 | |
| 5088 | static struct z_candidate * |
| 5089 | perform_overload_resolution (tree fn, |
| 5090 | const vec<tree, va_gc> *args, |
| 5091 | struct z_candidate **candidates, |
| 5092 | bool *any_viable_p, tsubst_flags_t complain) |
| 5093 | { |
| 5094 | struct z_candidate *cand; |
| 5095 | tree explicit_targs; |
| 5096 | int template_only; |
| 5097 | |
| 5098 | auto_cond_timevar tv (TV_OVERLOAD); |
| 5099 | |
| 5100 | explicit_targs = NULL_TREE; |
| 5101 | template_only = 0; |
| 5102 | |
| 5103 | *candidates = NULL; |
| 5104 | *any_viable_p = true; |
| 5105 | |
| 5106 | /* Check FN. */ |
| 5107 | gcc_assert (OVL_P (fn) || TREE_CODE (fn) == TEMPLATE_ID_EXPR); |
| 5108 | |
| 5109 | if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) |
| 5110 | { |
| 5111 | explicit_targs = TREE_OPERAND (fn, 1); |
| 5112 | fn = TREE_OPERAND (fn, 0); |
| 5113 | template_only = 1; |
| 5114 | } |
| 5115 | |
| 5116 | /* Add the various candidate functions. */ |
| 5117 | add_candidates (fn, NULL_TREE, args, NULL_TREE, |
| 5118 | explicit_targs, template_only, |
| 5119 | /*conversion_path=*/NULL_TREE, |
| 5120 | /*access_path=*/NULL_TREE, |
| 5121 | LOOKUP_NORMAL, |
| 5122 | candidates, complain); |
| 5123 | |
| 5124 | *candidates = splice_viable (cands: *candidates, strict_p: false, any_viable_p); |
| 5125 | if (*any_viable_p) |
| 5126 | cand = tourney (*candidates, complain); |
| 5127 | else |
| 5128 | cand = NULL; |
| 5129 | |
| 5130 | return cand; |
| 5131 | } |
| 5132 | |
| 5133 | /* Print an error message about being unable to build a call to FN with |
| 5134 | ARGS. ANY_VIABLE_P indicates whether any candidate functions could |
| 5135 | be located; CANDIDATES is a possibly empty list of such |
| 5136 | functions. */ |
| 5137 | |
| 5138 | static void |
| 5139 | print_error_for_call_failure (tree fn, const vec<tree, va_gc> *args, |
| 5140 | struct z_candidate *candidates) |
| 5141 | { |
| 5142 | tree targs = NULL_TREE; |
| 5143 | if (TREE_CODE (fn) == TEMPLATE_ID_EXPR) |
| 5144 | { |
| 5145 | targs = TREE_OPERAND (fn, 1); |
| 5146 | fn = TREE_OPERAND (fn, 0); |
| 5147 | } |
| 5148 | tree name = OVL_NAME (fn); |
| 5149 | location_t loc = location_of (name); |
| 5150 | if (targs) |
| 5151 | name = lookup_template_function (name, targs); |
| 5152 | |
| 5153 | auto_diagnostic_group d; |
| 5154 | if (!any_strictly_viable (cands: candidates)) |
| 5155 | error_at (loc, "no matching function for call to %<%D(%A)%>" , |
| 5156 | name, build_tree_list_vec (args)); |
| 5157 | else |
| 5158 | error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous" , |
| 5159 | name, build_tree_list_vec (args)); |
| 5160 | if (candidates) |
| 5161 | print_z_candidates (loc, candidates); |
| 5162 | } |
| 5163 | |
| 5164 | /* Perform overload resolution on the set of deduction guides DGUIDES |
| 5165 | using ARGS. Returns the selected deduction guide, or error_mark_node |
| 5166 | if overload resolution fails. */ |
| 5167 | |
| 5168 | tree |
| 5169 | perform_dguide_overload_resolution (tree dguides, const vec<tree, va_gc> *args, |
| 5170 | tsubst_flags_t complain) |
| 5171 | { |
| 5172 | z_candidate *candidates; |
| 5173 | bool any_viable_p; |
| 5174 | tree result; |
| 5175 | |
| 5176 | gcc_assert (deduction_guide_p (OVL_FIRST (dguides))); |
| 5177 | |
| 5178 | conversion_obstack_sentinel cos; |
| 5179 | |
| 5180 | z_candidate *cand = perform_overload_resolution (fn: dguides, args, candidates: &candidates, |
| 5181 | any_viable_p: &any_viable_p, complain); |
| 5182 | if (!cand) |
| 5183 | { |
| 5184 | if (complain & tf_error) |
| 5185 | print_error_for_call_failure (fn: dguides, args, candidates); |
| 5186 | result = error_mark_node; |
| 5187 | } |
| 5188 | else |
| 5189 | result = cand->fn; |
| 5190 | |
| 5191 | return result; |
| 5192 | } |
| 5193 | |
| 5194 | /* Return an expression for a call to FN (a namespace-scope function, |
| 5195 | or a static member function) with the ARGS. This may change |
| 5196 | ARGS. */ |
| 5197 | |
| 5198 | tree |
| 5199 | build_new_function_call (tree fn, vec<tree, va_gc> **args, |
| 5200 | tsubst_flags_t complain) |
| 5201 | { |
| 5202 | struct z_candidate *candidates, *cand; |
| 5203 | bool any_viable_p; |
| 5204 | tree result; |
| 5205 | |
| 5206 | if (args != NULL && *args != NULL) |
| 5207 | { |
| 5208 | *args = resolve_args (args: *args, complain); |
| 5209 | if (*args == NULL) |
| 5210 | return error_mark_node; |
| 5211 | } |
| 5212 | |
| 5213 | if (flag_tm) |
| 5214 | tm_malloc_replacement (fn); |
| 5215 | |
| 5216 | conversion_obstack_sentinel cos; |
| 5217 | |
| 5218 | cand = perform_overload_resolution (fn, args: *args, candidates: &candidates, any_viable_p: &any_viable_p, |
| 5219 | complain); |
| 5220 | |
| 5221 | if (!cand) |
| 5222 | { |
| 5223 | if (complain & tf_error) |
| 5224 | { |
| 5225 | // If there is a single (non-viable) function candidate, |
| 5226 | // let the error be diagnosed by cp_build_function_call_vec. |
| 5227 | if (!any_viable_p && candidates && ! candidates->next |
| 5228 | && TREE_CODE (candidates->fn) == FUNCTION_DECL |
| 5229 | /* A template-id callee consisting of a single (ignored) |
| 5230 | non-template candidate needs to be diagnosed the |
| 5231 | ordinary way. */ |
| 5232 | && (TREE_CODE (fn) != TEMPLATE_ID_EXPR |
| 5233 | || candidates->template_decl)) |
| 5234 | return cp_build_function_call_vec (candidates->fn, args, complain); |
| 5235 | |
| 5236 | // Otherwise, emit notes for non-viable candidates. |
| 5237 | print_error_for_call_failure (fn, args: *args, candidates); |
| 5238 | } |
| 5239 | result = error_mark_node; |
| 5240 | } |
| 5241 | else |
| 5242 | { |
| 5243 | result = build_over_call (cand, LOOKUP_NORMAL, complain); |
| 5244 | } |
| 5245 | |
| 5246 | if (flag_coroutines |
| 5247 | && result |
| 5248 | && TREE_CODE (result) == CALL_EXPR |
| 5249 | && DECL_BUILT_IN_CLASS (TREE_OPERAND (CALL_EXPR_FN (result), 0)) |
| 5250 | == BUILT_IN_NORMAL) |
| 5251 | result = coro_validate_builtin_call (result); |
| 5252 | |
| 5253 | return result; |
| 5254 | } |
| 5255 | |
| 5256 | /* Build a call to a global operator new. FNNAME is the name of the |
| 5257 | operator (either "operator new" or "operator new[]") and ARGS are |
| 5258 | the arguments provided. This may change ARGS. *SIZE points to the |
| 5259 | total number of bytes required by the allocation, and is updated if |
| 5260 | that is changed here. *COOKIE_SIZE is non-NULL if a cookie should |
| 5261 | be used. If this function determines that no cookie should be |
| 5262 | used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK |
| 5263 | is not NULL_TREE, it is evaluated before calculating the final |
| 5264 | array size, and if it fails, the array size is replaced with |
| 5265 | (size_t)-1 (usually triggering a std::bad_alloc exception). If FN |
| 5266 | is non-NULL, it will be set, upon return, to the allocation |
| 5267 | function called. */ |
| 5268 | |
| 5269 | tree |
| 5270 | build_operator_new_call (tree fnname, vec<tree, va_gc> **args, |
| 5271 | tree *size, tree *cookie_size, |
| 5272 | tree align_arg, tree size_check, |
| 5273 | tree *fn, tsubst_flags_t complain) |
| 5274 | { |
| 5275 | tree original_size = *size; |
| 5276 | tree fns; |
| 5277 | struct z_candidate *candidates; |
| 5278 | struct z_candidate *cand = NULL; |
| 5279 | bool any_viable_p; |
| 5280 | |
| 5281 | if (fn) |
| 5282 | *fn = NULL_TREE; |
| 5283 | /* Set to (size_t)-1 if the size check fails. */ |
| 5284 | if (size_check != NULL_TREE) |
| 5285 | { |
| 5286 | tree errval = TYPE_MAX_VALUE (sizetype); |
| 5287 | if (cxx_dialect >= cxx11 && flag_exceptions) |
| 5288 | errval = throw_bad_array_new_length (); |
| 5289 | *size = fold_build3 (COND_EXPR, sizetype, size_check, |
| 5290 | original_size, errval); |
| 5291 | } |
| 5292 | vec_safe_insert (v&: *args, ix: 0, obj: *size); |
| 5293 | *args = resolve_args (args: *args, complain); |
| 5294 | if (*args == NULL) |
| 5295 | return error_mark_node; |
| 5296 | |
| 5297 | conversion_obstack_sentinel cos; |
| 5298 | |
| 5299 | /* Based on: |
| 5300 | |
| 5301 | [expr.new] |
| 5302 | |
| 5303 | If this lookup fails to find the name, or if the allocated type |
| 5304 | is not a class type, the allocation function's name is looked |
| 5305 | up in the global scope. |
| 5306 | |
| 5307 | we disregard block-scope declarations of "operator new". */ |
| 5308 | fns = lookup_qualified_name (global_namespace, name: fnname); |
| 5309 | |
| 5310 | if (align_arg) |
| 5311 | { |
| 5312 | vec<tree, va_gc>* align_args |
| 5313 | = vec_copy_and_insert (*args, align_arg, 1); |
| 5314 | cand = perform_overload_resolution (fn: fns, args: align_args, candidates: &candidates, |
| 5315 | any_viable_p: &any_viable_p, complain: tf_none); |
| 5316 | if (cand) |
| 5317 | *args = align_args; |
| 5318 | /* If no aligned allocation function matches, try again without the |
| 5319 | alignment. */ |
| 5320 | } |
| 5321 | |
| 5322 | /* Figure out what function is being called. */ |
| 5323 | if (!cand) |
| 5324 | cand = perform_overload_resolution (fn: fns, args: *args, candidates: &candidates, any_viable_p: &any_viable_p, |
| 5325 | complain); |
| 5326 | |
| 5327 | /* If no suitable function could be found, issue an error message |
| 5328 | and give up. */ |
| 5329 | if (!cand) |
| 5330 | { |
| 5331 | if (complain & tf_error) |
| 5332 | print_error_for_call_failure (fn: fns, args: *args, candidates); |
| 5333 | return error_mark_node; |
| 5334 | } |
| 5335 | |
| 5336 | /* If a cookie is required, add some extra space. Whether |
| 5337 | or not a cookie is required cannot be determined until |
| 5338 | after we know which function was called. */ |
| 5339 | if (*cookie_size) |
| 5340 | { |
| 5341 | bool use_cookie = true; |
| 5342 | tree arg_types; |
| 5343 | |
| 5344 | arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn)); |
| 5345 | /* Skip the size_t parameter. */ |
| 5346 | arg_types = TREE_CHAIN (arg_types); |
| 5347 | /* Check the remaining parameters (if any). */ |
| 5348 | if (arg_types |
| 5349 | && TREE_CHAIN (arg_types) == void_list_node |
| 5350 | && same_type_p (TREE_VALUE (arg_types), |
| 5351 | ptr_type_node)) |
| 5352 | use_cookie = false; |
| 5353 | /* If we need a cookie, adjust the number of bytes allocated. */ |
| 5354 | if (use_cookie) |
| 5355 | { |
| 5356 | /* Update the total size. */ |
| 5357 | *size = size_binop (PLUS_EXPR, original_size, *cookie_size); |
| 5358 | if (size_check) |
| 5359 | { |
| 5360 | /* Set to (size_t)-1 if the size check fails. */ |
| 5361 | gcc_assert (size_check != NULL_TREE); |
| 5362 | *size = fold_build3 (COND_EXPR, sizetype, size_check, |
| 5363 | *size, TYPE_MAX_VALUE (sizetype)); |
| 5364 | } |
| 5365 | /* Update the argument list to reflect the adjusted size. */ |
| 5366 | (**args)[0] = *size; |
| 5367 | } |
| 5368 | else |
| 5369 | *cookie_size = NULL_TREE; |
| 5370 | } |
| 5371 | |
| 5372 | /* Tell our caller which function we decided to call. */ |
| 5373 | if (fn) |
| 5374 | *fn = cand->fn; |
| 5375 | |
| 5376 | /* Build the CALL_EXPR. */ |
| 5377 | tree ret = build_over_call (cand, LOOKUP_NORMAL, complain); |
| 5378 | |
| 5379 | /* Set this flag for all callers of this function. In addition to |
| 5380 | new-expressions, this is called for allocating coroutine state; treat |
| 5381 | that as an implicit new-expression. */ |
| 5382 | tree call = extract_call_expr (ret); |
| 5383 | if (TREE_CODE (call) == CALL_EXPR) |
| 5384 | CALL_FROM_NEW_OR_DELETE_P (call) = 1; |
| 5385 | |
| 5386 | return ret; |
| 5387 | } |
| 5388 | |
| 5389 | /* Evaluate side-effects from OBJ before evaluating call |
| 5390 | to FN in RESULT expression. |
| 5391 | This is for expressions of the form `obj->fn(...)' |
| 5392 | where `fn' turns out to be a static member function and |
| 5393 | `obj' needs to be evaluated. `fn' could be also static operator[] |
| 5394 | or static operator(), in which cases the source expression |
| 5395 | would be `obj[...]' or `obj(...)'. */ |
| 5396 | |
| 5397 | tree |
| 5398 | keep_unused_object_arg (tree result, tree obj, tree fn) |
| 5399 | { |
| 5400 | if (result == NULL_TREE |
| 5401 | || result == error_mark_node |
| 5402 | || DECL_OBJECT_MEMBER_FUNCTION_P (fn) |
| 5403 | || !TREE_SIDE_EFFECTS (obj)) |
| 5404 | return result; |
| 5405 | |
| 5406 | /* But avoid the implicit lvalue-rvalue conversion when `obj' is |
| 5407 | volatile. */ |
| 5408 | tree a = obj; |
| 5409 | if (TREE_THIS_VOLATILE (a)) |
| 5410 | a = build_this (obj: a); |
| 5411 | if (TREE_SIDE_EFFECTS (a)) |
| 5412 | return cp_build_compound_expr (a, result, tf_error); |
| 5413 | return result; |
| 5414 | } |
| 5415 | |
| 5416 | /* Build a new call to operator(). This may change ARGS. */ |
| 5417 | |
| 5418 | tree |
| 5419 | build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain) |
| 5420 | { |
| 5421 | struct z_candidate *candidates = 0, *cand; |
| 5422 | tree fns, convs, first_mem_arg = NULL_TREE; |
| 5423 | bool any_viable_p; |
| 5424 | tree result = NULL_TREE; |
| 5425 | |
| 5426 | auto_cond_timevar tv (TV_OVERLOAD); |
| 5427 | |
| 5428 | obj = mark_lvalue_use (obj); |
| 5429 | |
| 5430 | if (error_operand_p (t: obj)) |
| 5431 | return error_mark_node; |
| 5432 | |
| 5433 | tree type = TREE_TYPE (obj); |
| 5434 | |
| 5435 | obj = prep_operand (obj); |
| 5436 | |
| 5437 | if (TYPE_PTRMEMFUNC_P (type)) |
| 5438 | { |
| 5439 | if (complain & tf_error) |
| 5440 | /* It's no good looking for an overloaded operator() on a |
| 5441 | pointer-to-member-function. */ |
| 5442 | error ("pointer-to-member function %qE cannot be called without " |
| 5443 | "an object; consider using %<.*%> or %<->*%>" , obj); |
| 5444 | return error_mark_node; |
| 5445 | } |
| 5446 | |
| 5447 | if (TYPE_BINFO (type)) |
| 5448 | { |
| 5449 | fns = lookup_fnfields (TYPE_BINFO (type), call_op_identifier, 1, complain); |
| 5450 | if (fns == error_mark_node) |
| 5451 | return error_mark_node; |
| 5452 | } |
| 5453 | else |
| 5454 | fns = NULL_TREE; |
| 5455 | |
| 5456 | if (args != NULL && *args != NULL) |
| 5457 | { |
| 5458 | *args = resolve_args (args: *args, complain); |
| 5459 | if (*args == NULL) |
| 5460 | return error_mark_node; |
| 5461 | } |
| 5462 | |
| 5463 | conversion_obstack_sentinel cos; |
| 5464 | |
| 5465 | if (fns) |
| 5466 | { |
| 5467 | first_mem_arg = obj; |
| 5468 | |
| 5469 | add_candidates (BASELINK_FUNCTIONS (fns), |
| 5470 | first_mem_arg, *args, NULL_TREE, |
| 5471 | NULL_TREE, false, |
| 5472 | BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns), |
| 5473 | LOOKUP_NORMAL, &candidates, complain); |
| 5474 | } |
| 5475 | |
| 5476 | bool any_call_ops = candidates != nullptr; |
| 5477 | |
| 5478 | convs = lookup_conversions (type); |
| 5479 | |
| 5480 | for (; convs; convs = TREE_CHAIN (convs)) |
| 5481 | { |
| 5482 | tree totype = TREE_TYPE (convs); |
| 5483 | |
| 5484 | if (TYPE_PTRFN_P (totype) |
| 5485 | || TYPE_REFFN_P (totype) |
| 5486 | || (TYPE_REF_P (totype) |
| 5487 | && TYPE_PTRFN_P (TREE_TYPE (totype)))) |
| 5488 | for (tree fn : ovl_range (TREE_VALUE (convs))) |
| 5489 | { |
| 5490 | if (DECL_NONCONVERTING_P (fn)) |
| 5491 | continue; |
| 5492 | |
| 5493 | if (TREE_CODE (fn) == TEMPLATE_DECL) |
| 5494 | { |
| 5495 | /* Making this work broke PR 71117 and 85118, so until the |
| 5496 | committee resolves core issue 2189, let's disable this |
| 5497 | candidate if there are any call operators. */ |
| 5498 | if (any_call_ops) |
| 5499 | continue; |
| 5500 | |
| 5501 | add_template_conv_candidate |
| 5502 | (candidates: &candidates, tmpl: fn, obj, arglist: *args, return_type: totype, |
| 5503 | /*access_path=*/NULL_TREE, |
| 5504 | /*conversion_path=*/NULL_TREE, complain); |
| 5505 | } |
| 5506 | else |
| 5507 | add_conv_candidate (candidates: &candidates, fn, obj, |
| 5508 | arglist: *args, /*conversion_path=*/NULL_TREE, |
| 5509 | /*access_path=*/NULL_TREE, complain); |
| 5510 | } |
| 5511 | } |
| 5512 | |
| 5513 | /* Be strict here because if we choose a bad conversion candidate, the |
| 5514 | errors we get won't mention the call context. */ |
| 5515 | candidates = splice_viable (cands: candidates, strict_p: true, any_viable_p: &any_viable_p); |
| 5516 | if (!any_viable_p) |
| 5517 | { |
| 5518 | if (complain & tf_error) |
| 5519 | { |
| 5520 | auto_diagnostic_group d; |
| 5521 | error ("no match for call to %<(%T) (%A)%>" , TREE_TYPE (obj), |
| 5522 | build_tree_list_vec (*args)); |
| 5523 | print_z_candidates (loc: location_of (TREE_TYPE (obj)), candidates); |
| 5524 | } |
| 5525 | result = error_mark_node; |
| 5526 | } |
| 5527 | else |
| 5528 | { |
| 5529 | cand = tourney (candidates, complain); |
| 5530 | if (cand == 0) |
| 5531 | { |
| 5532 | if (complain & tf_error) |
| 5533 | { |
| 5534 | auto_diagnostic_group d; |
| 5535 | error ("call of %<(%T) (%A)%> is ambiguous" , |
| 5536 | TREE_TYPE (obj), build_tree_list_vec (*args)); |
| 5537 | print_z_candidates (loc: location_of (TREE_TYPE (obj)), candidates); |
| 5538 | } |
| 5539 | result = error_mark_node; |
| 5540 | } |
| 5541 | else if (TREE_CODE (cand->fn) == FUNCTION_DECL |
| 5542 | && DECL_OVERLOADED_OPERATOR_P (cand->fn) |
| 5543 | && DECL_OVERLOADED_OPERATOR_IS (cand->fn, CALL_EXPR)) |
| 5544 | { |
| 5545 | result = build_over_call (cand, LOOKUP_NORMAL, complain); |
| 5546 | /* In an expression of the form `a()' where cand->fn |
| 5547 | which is operator() turns out to be a static member function, |
| 5548 | `a' is none-the-less evaluated. */ |
| 5549 | result = keep_unused_object_arg (result, obj, fn: cand->fn); |
| 5550 | } |
| 5551 | else |
| 5552 | { |
| 5553 | if (TREE_CODE (cand->fn) == FUNCTION_DECL) |
| 5554 | obj = convert_like_with_context (cand->convs[0], obj, cand->fn, |
| 5555 | -1, complain); |
| 5556 | else |
| 5557 | { |
| 5558 | gcc_checking_assert (TYPE_P (cand->fn)); |
| 5559 | obj = convert_like (cand->convs[0], obj, complain); |
| 5560 | } |
| 5561 | obj = convert_from_reference (obj); |
| 5562 | result = cp_build_function_call_vec (obj, args, complain); |
| 5563 | } |
| 5564 | } |
| 5565 | |
| 5566 | return result; |
| 5567 | } |
| 5568 | |
| 5569 | /* Subroutine for preparing format strings suitable for the error |
| 5570 | function. It concatenates a prefix (controlled by MATCH), ERRMSG, |
| 5571 | and SUFFIX. */ |
| 5572 | |
| 5573 | static const char * |
| 5574 | concat_op_error_string (bool match, const char *errmsg, const char *suffix) |
| 5575 | { |
| 5576 | return concat (match |
| 5577 | ? G_("ambiguous overload for " ) |
| 5578 | : G_("no match for " ), |
| 5579 | errmsg, suffix, nullptr); |
| 5580 | } |
| 5581 | |
| 5582 | /* Called by op_error to prepare format strings suitable for the error |
| 5583 | function. It concatenates a prefix (controlled by MATCH), ERRMSG, |
| 5584 | and a suffix (controlled by NTYPES). */ |
| 5585 | |
| 5586 | static const char * |
| 5587 | op_error_string (const char *errmsg, int ntypes, bool match) |
| 5588 | { |
| 5589 | const char *suffix; |
| 5590 | if (ntypes == 3) |
| 5591 | suffix = G_(" (operand types are %qT, %qT, and %qT)" ); |
| 5592 | else if (ntypes == 2) |
| 5593 | suffix = G_(" (operand types are %qT and %qT)" ); |
| 5594 | else |
| 5595 | suffix = G_(" (operand type is %qT)" ); |
| 5596 | return concat_op_error_string (match, errmsg, suffix); |
| 5597 | } |
| 5598 | |
| 5599 | /* Similar to op_error_string, but a special-case for binary ops that |
| 5600 | use %e for the args, rather than %qT. */ |
| 5601 | |
| 5602 | static const char * |
| 5603 | binop_error_string (const char *errmsg, bool match) |
| 5604 | { |
| 5605 | return concat_op_error_string (match, errmsg, |
| 5606 | G_(" (operand types are %e and %e)" )); |
| 5607 | } |
| 5608 | |
| 5609 | static void |
| 5610 | op_error (const op_location_t &loc, |
| 5611 | enum tree_code code, enum tree_code code2, |
| 5612 | tree arg1, tree arg2, tree arg3, bool match) |
| 5613 | { |
| 5614 | bool assop = code == MODIFY_EXPR; |
| 5615 | const char *opname = OVL_OP_INFO (assop, assop ? code2 : code)->name; |
| 5616 | |
| 5617 | switch (code) |
| 5618 | { |
| 5619 | case COND_EXPR: |
| 5620 | if (flag_diagnostics_show_caret) |
| 5621 | error_at (loc, op_error_string (G_("ternary %<operator?:%>" ), |
| 5622 | ntypes: 3, match), |
| 5623 | TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3)); |
| 5624 | else |
| 5625 | error_at (loc, op_error_string (G_("ternary %<operator?:%> " |
| 5626 | "in %<%E ? %E : %E%>" ), ntypes: 3, match), |
| 5627 | arg1, arg2, arg3, |
| 5628 | TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3)); |
| 5629 | break; |
| 5630 | |
| 5631 | case POSTINCREMENT_EXPR: |
| 5632 | case POSTDECREMENT_EXPR: |
| 5633 | if (flag_diagnostics_show_caret) |
| 5634 | error_at (loc, op_error_string (G_("%<operator%s%>" ), ntypes: 1, match), |
| 5635 | opname, TREE_TYPE (arg1)); |
| 5636 | else |
| 5637 | error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>" ), |
| 5638 | ntypes: 1, match), |
| 5639 | opname, arg1, opname, TREE_TYPE (arg1)); |
| 5640 | break; |
| 5641 | |
| 5642 | case ARRAY_REF: |
| 5643 | if (flag_diagnostics_show_caret) |
| 5644 | error_at (loc, op_error_string (G_("%<operator[]%>" ), ntypes: 2, match), |
| 5645 | TREE_TYPE (arg1), TREE_TYPE (arg2)); |
| 5646 | else |
| 5647 | error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>" ), |
| 5648 | ntypes: 2, match), |
| 5649 | arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2)); |
| 5650 | break; |
| 5651 | |
| 5652 | case REALPART_EXPR: |
| 5653 | case IMAGPART_EXPR: |
| 5654 | if (flag_diagnostics_show_caret) |
| 5655 | error_at (loc, op_error_string (G_("%qs" ), ntypes: 1, match), |
| 5656 | opname, TREE_TYPE (arg1)); |
| 5657 | else |
| 5658 | error_at (loc, op_error_string (G_("%qs in %<%s %E%>" ), ntypes: 1, match), |
| 5659 | opname, opname, arg1, TREE_TYPE (arg1)); |
| 5660 | break; |
| 5661 | |
| 5662 | case CO_AWAIT_EXPR: |
| 5663 | if (flag_diagnostics_show_caret) |
| 5664 | error_at (loc, op_error_string (G_("%<operator %s%>" ), ntypes: 1, match), |
| 5665 | opname, TREE_TYPE (arg1)); |
| 5666 | else |
| 5667 | error_at (loc, op_error_string (G_("%<operator %s%> in %<%s%E%>" ), |
| 5668 | ntypes: 1, match), |
| 5669 | opname, opname, arg1, TREE_TYPE (arg1)); |
| 5670 | break; |
| 5671 | |
| 5672 | default: |
| 5673 | if (arg2) |
| 5674 | if (flag_diagnostics_show_caret) |
| 5675 | { |
| 5676 | binary_op_rich_location richloc (loc, arg1, arg2, true); |
| 5677 | pp_markup::element_quoted_type element_0 |
| 5678 | (TREE_TYPE (arg1), highlight_colors::lhs); |
| 5679 | pp_markup::element_quoted_type element_1 |
| 5680 | (TREE_TYPE (arg2), highlight_colors::rhs); |
| 5681 | error_at (&richloc, |
| 5682 | binop_error_string (G_("%<operator%s%>" ), match), |
| 5683 | opname, &element_0, &element_1); |
| 5684 | } |
| 5685 | else |
| 5686 | error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>" ), |
| 5687 | ntypes: 2, match), |
| 5688 | opname, arg1, opname, arg2, |
| 5689 | TREE_TYPE (arg1), TREE_TYPE (arg2)); |
| 5690 | else |
| 5691 | if (flag_diagnostics_show_caret) |
| 5692 | error_at (loc, op_error_string (G_("%<operator%s%>" ), ntypes: 1, match), |
| 5693 | opname, TREE_TYPE (arg1)); |
| 5694 | else |
| 5695 | error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>" ), |
| 5696 | ntypes: 1, match), |
| 5697 | opname, opname, arg1, TREE_TYPE (arg1)); |
| 5698 | break; |
| 5699 | } |
| 5700 | } |
| 5701 | |
| 5702 | /* Return the implicit conversion sequence that could be used to |
| 5703 | convert E1 to E2 in [expr.cond]. */ |
| 5704 | |
| 5705 | static conversion * |
| 5706 | conditional_conversion (tree e1, tree e2, tsubst_flags_t complain) |
| 5707 | { |
| 5708 | tree t1 = non_reference (TREE_TYPE (e1)); |
| 5709 | tree t2 = non_reference (TREE_TYPE (e2)); |
| 5710 | conversion *conv; |
| 5711 | bool good_base; |
| 5712 | |
| 5713 | /* [expr.cond] |
| 5714 | |
| 5715 | If E2 is an lvalue: E1 can be converted to match E2 if E1 can be |
| 5716 | implicitly converted (clause _conv_) to the type "lvalue reference to |
| 5717 | T2", subject to the constraint that in the conversion the |
| 5718 | reference must bind directly (_dcl.init.ref_) to an lvalue. |
| 5719 | |
| 5720 | If E2 is an xvalue: E1 can be converted to match E2 if E1 can be |
| 5721 | implicitly converted to the type "rvalue reference to T2", subject to |
| 5722 | the constraint that the reference must bind directly. */ |
| 5723 | if (glvalue_p (e2)) |
| 5724 | { |
| 5725 | tree rtype = cp_build_reference_type (t2, !lvalue_p (e2)); |
| 5726 | conv = implicit_conversion (to: rtype, |
| 5727 | from: t1, |
| 5728 | expr: e1, |
| 5729 | /*c_cast_p=*/false, |
| 5730 | LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND |
| 5731 | |LOOKUP_ONLYCONVERTING, |
| 5732 | complain); |
| 5733 | if (conv && !conv->bad_p) |
| 5734 | return conv; |
| 5735 | } |
| 5736 | |
| 5737 | /* If E2 is a prvalue or if neither of the conversions above can be done |
| 5738 | and at least one of the operands has (possibly cv-qualified) class |
| 5739 | type: */ |
| 5740 | if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2)) |
| 5741 | return NULL; |
| 5742 | |
| 5743 | /* [expr.cond] |
| 5744 | |
| 5745 | If E1 and E2 have class type, and the underlying class types are |
| 5746 | the same or one is a base class of the other: E1 can be converted |
| 5747 | to match E2 if the class of T2 is the same type as, or a base |
| 5748 | class of, the class of T1, and the cv-qualification of T2 is the |
| 5749 | same cv-qualification as, or a greater cv-qualification than, the |
| 5750 | cv-qualification of T1. If the conversion is applied, E1 is |
| 5751 | changed to an rvalue of type T2 that still refers to the original |
| 5752 | source class object (or the appropriate subobject thereof). */ |
| 5753 | if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2) |
| 5754 | && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2))) |
| 5755 | { |
| 5756 | if (good_base && at_least_as_qualified_p (t2, t1)) |
| 5757 | { |
| 5758 | conv = build_identity_conv (type: t1, expr: e1); |
| 5759 | if (!same_type_p (TYPE_MAIN_VARIANT (t1), |
| 5760 | TYPE_MAIN_VARIANT (t2))) |
| 5761 | conv = build_conv (code: ck_base, type: t2, from: conv); |
| 5762 | else |
| 5763 | conv = build_conv (code: ck_rvalue, type: t2, from: conv); |
| 5764 | return conv; |
| 5765 | } |
| 5766 | else |
| 5767 | return NULL; |
| 5768 | } |
| 5769 | else |
| 5770 | /* [expr.cond] |
| 5771 | |
| 5772 | Otherwise: E1 can be converted to match E2 if E1 can be implicitly |
| 5773 | converted to the type that expression E2 would have if E2 were |
| 5774 | converted to an rvalue (or the type it has, if E2 is an rvalue). */ |
| 5775 | return implicit_conversion (to: t2, from: t1, expr: e1, /*c_cast_p=*/false, |
| 5776 | LOOKUP_IMPLICIT, complain); |
| 5777 | } |
| 5778 | |
| 5779 | /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three |
| 5780 | arguments to the conditional expression. */ |
| 5781 | |
| 5782 | tree |
| 5783 | build_conditional_expr (const op_location_t &loc, |
| 5784 | tree arg1, tree arg2, tree arg3, |
| 5785 | tsubst_flags_t complain) |
| 5786 | { |
| 5787 | tree arg2_type; |
| 5788 | tree arg3_type; |
| 5789 | tree result = NULL_TREE; |
| 5790 | tree result_type = NULL_TREE; |
| 5791 | tree semantic_result_type = NULL_TREE; |
| 5792 | bool is_glvalue = true; |
| 5793 | struct z_candidate *candidates = 0; |
| 5794 | struct z_candidate *cand; |
| 5795 | tree orig_arg2, orig_arg3; |
| 5796 | |
| 5797 | auto_cond_timevar tv (TV_OVERLOAD); |
| 5798 | |
| 5799 | /* As a G++ extension, the second argument to the conditional can be |
| 5800 | omitted. (So that `a ? : c' is roughly equivalent to `a ? a : |
| 5801 | c'.) If the second operand is omitted, make sure it is |
| 5802 | calculated only once. */ |
| 5803 | if (!arg2) |
| 5804 | { |
| 5805 | if (complain & tf_error) |
| 5806 | pedwarn (loc, OPT_Wpedantic, |
| 5807 | "ISO C++ forbids omitting the middle term of " |
| 5808 | "a %<?:%> expression" ); |
| 5809 | |
| 5810 | if ((complain & tf_warning) && !truth_value_p (TREE_CODE (arg1))) |
| 5811 | warn_for_omitted_condop (loc, arg1); |
| 5812 | |
| 5813 | /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */ |
| 5814 | if (glvalue_p (arg1)) |
| 5815 | { |
| 5816 | arg1 = cp_stabilize_reference (arg1); |
| 5817 | arg2 = arg1 = prevent_lifetime_extension (arg1); |
| 5818 | } |
| 5819 | else if (TREE_CODE (arg1) == TARGET_EXPR) |
| 5820 | /* arg1 can't be a prvalue result of the conditional |
| 5821 | expression, since it needs to be materialized for the |
| 5822 | conversion to bool, so treat it as an xvalue in arg2. */ |
| 5823 | arg2 = move (TARGET_EXPR_SLOT (arg1)); |
| 5824 | else if (TREE_CODE (arg1) == EXCESS_PRECISION_EXPR) |
| 5825 | arg2 = arg1 = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (arg1), |
| 5826 | cp_save_expr (TREE_OPERAND (arg1, 0))); |
| 5827 | else |
| 5828 | arg2 = arg1 = cp_save_expr (arg1); |
| 5829 | } |
| 5830 | |
| 5831 | /* If something has already gone wrong, just pass that fact up the |
| 5832 | tree. */ |
| 5833 | if (error_operand_p (t: arg1) |
| 5834 | || error_operand_p (t: arg2) |
| 5835 | || error_operand_p (t: arg3)) |
| 5836 | return error_mark_node; |
| 5837 | |
| 5838 | conversion_obstack_sentinel cos; |
| 5839 | |
| 5840 | orig_arg2 = arg2; |
| 5841 | orig_arg3 = arg3; |
| 5842 | |
| 5843 | if (gnu_vector_type_p (TREE_TYPE (arg1)) |
| 5844 | && VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1))) |
| 5845 | { |
| 5846 | tree arg1_type = TREE_TYPE (arg1); |
| 5847 | |
| 5848 | /* If arg1 is another cond_expr choosing between -1 and 0, |
| 5849 | then we can use its comparison. It may help to avoid |
| 5850 | additional comparison, produce more accurate diagnostics |
| 5851 | and enables folding. */ |
| 5852 | if (TREE_CODE (arg1) == VEC_COND_EXPR |
| 5853 | && integer_minus_onep (TREE_OPERAND (arg1, 1)) |
| 5854 | && integer_zerop (TREE_OPERAND (arg1, 2))) |
| 5855 | arg1 = TREE_OPERAND (arg1, 0); |
| 5856 | |
| 5857 | arg1 = force_rvalue (arg1, complain); |
| 5858 | arg2 = force_rvalue (arg2, complain); |
| 5859 | arg3 = force_rvalue (arg3, complain); |
| 5860 | |
| 5861 | /* force_rvalue can return error_mark on valid arguments. */ |
| 5862 | if (error_operand_p (t: arg1) |
| 5863 | || error_operand_p (t: arg2) |
| 5864 | || error_operand_p (t: arg3)) |
| 5865 | return error_mark_node; |
| 5866 | |
| 5867 | arg2_type = TREE_TYPE (arg2); |
| 5868 | arg3_type = TREE_TYPE (arg3); |
| 5869 | |
| 5870 | if (!VECTOR_TYPE_P (arg2_type) |
| 5871 | && !VECTOR_TYPE_P (arg3_type)) |
| 5872 | { |
| 5873 | /* Rely on the error messages of the scalar version. */ |
| 5874 | tree scal = build_conditional_expr (loc, integer_one_node, |
| 5875 | arg2: orig_arg2, arg3: orig_arg3, complain); |
| 5876 | if (scal == error_mark_node) |
| 5877 | return error_mark_node; |
| 5878 | tree stype = TREE_TYPE (scal); |
| 5879 | tree ctype = TREE_TYPE (arg1_type); |
| 5880 | if (TYPE_SIZE (stype) != TYPE_SIZE (ctype) |
| 5881 | || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype))) |
| 5882 | { |
| 5883 | if (complain & tf_error) |
| 5884 | error_at (loc, "inferred scalar type %qT is not an integer or " |
| 5885 | "floating-point type of the same size as %qT" , stype, |
| 5886 | COMPARISON_CLASS_P (arg1) |
| 5887 | ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0))) |
| 5888 | : ctype); |
| 5889 | return error_mark_node; |
| 5890 | } |
| 5891 | |
| 5892 | tree vtype = build_opaque_vector_type (stype, |
| 5893 | TYPE_VECTOR_SUBPARTS (node: arg1_type)); |
| 5894 | /* We could pass complain & tf_warning to unsafe_conversion_p, |
| 5895 | but the warnings (like Wsign-conversion) have already been |
| 5896 | given by the scalar build_conditional_expr_1. We still check |
| 5897 | unsafe_conversion_p to forbid truncating long long -> float. */ |
| 5898 | if (unsafe_conversion_p (stype, arg2, NULL_TREE, false)) |
| 5899 | { |
| 5900 | if (complain & tf_error) |
| 5901 | error_at (loc, "conversion of scalar %qH to vector %qI " |
| 5902 | "involves truncation" , arg2_type, vtype); |
| 5903 | return error_mark_node; |
| 5904 | } |
| 5905 | if (unsafe_conversion_p (stype, arg3, NULL_TREE, false)) |
| 5906 | { |
| 5907 | if (complain & tf_error) |
| 5908 | error_at (loc, "conversion of scalar %qH to vector %qI " |
| 5909 | "involves truncation" , arg3_type, vtype); |
| 5910 | return error_mark_node; |
| 5911 | } |
| 5912 | |
| 5913 | arg2 = cp_convert (stype, arg2, complain); |
| 5914 | arg2 = save_expr (arg2); |
| 5915 | arg2 = build_vector_from_val (vtype, arg2); |
| 5916 | arg2_type = vtype; |
| 5917 | arg3 = cp_convert (stype, arg3, complain); |
| 5918 | arg3 = save_expr (arg3); |
| 5919 | arg3 = build_vector_from_val (vtype, arg3); |
| 5920 | arg3_type = vtype; |
| 5921 | } |
| 5922 | |
| 5923 | if ((gnu_vector_type_p (type: arg2_type) && !VECTOR_TYPE_P (arg3_type)) |
| 5924 | || (gnu_vector_type_p (type: arg3_type) && !VECTOR_TYPE_P (arg2_type))) |
| 5925 | { |
| 5926 | enum stv_conv convert_flag = |
| 5927 | scalar_to_vector (loc, code: VEC_COND_EXPR, op0: arg2, op1: arg3, |
| 5928 | complain & tf_error); |
| 5929 | |
| 5930 | switch (convert_flag) |
| 5931 | { |
| 5932 | case stv_error: |
| 5933 | return error_mark_node; |
| 5934 | case stv_firstarg: |
| 5935 | { |
| 5936 | arg2 = save_expr (arg2); |
| 5937 | arg2 = convert (TREE_TYPE (arg3_type), arg2); |
| 5938 | arg2 = build_vector_from_val (arg3_type, arg2); |
| 5939 | arg2_type = TREE_TYPE (arg2); |
| 5940 | break; |
| 5941 | } |
| 5942 | case stv_secondarg: |
| 5943 | { |
| 5944 | arg3 = save_expr (arg3); |
| 5945 | arg3 = convert (TREE_TYPE (arg2_type), arg3); |
| 5946 | arg3 = build_vector_from_val (arg2_type, arg3); |
| 5947 | arg3_type = TREE_TYPE (arg3); |
| 5948 | break; |
| 5949 | } |
| 5950 | default: |
| 5951 | break; |
| 5952 | } |
| 5953 | } |
| 5954 | |
| 5955 | if (!gnu_vector_type_p (type: arg2_type) |
| 5956 | || !gnu_vector_type_p (type: arg3_type) |
| 5957 | || !same_type_p (arg2_type, arg3_type) |
| 5958 | || maybe_ne (a: TYPE_VECTOR_SUBPARTS (node: arg1_type), |
| 5959 | b: TYPE_VECTOR_SUBPARTS (node: arg2_type)) |
| 5960 | || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type)) |
| 5961 | { |
| 5962 | if (complain & tf_error) |
| 5963 | error_at (loc, |
| 5964 | "incompatible vector types in conditional expression: " |
| 5965 | "%qT, %qT and %qT" , TREE_TYPE (arg1), |
| 5966 | TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3)); |
| 5967 | return error_mark_node; |
| 5968 | } |
| 5969 | |
| 5970 | if (!COMPARISON_CLASS_P (arg1)) |
| 5971 | { |
| 5972 | tree cmp_type = truth_type_for (arg1_type); |
| 5973 | arg1 = build2 (NE_EXPR, cmp_type, arg1, build_zero_cst (arg1_type)); |
| 5974 | } |
| 5975 | return build3_loc (loc, code: VEC_COND_EXPR, type: arg2_type, arg0: arg1, arg1: arg2, arg2: arg3); |
| 5976 | } |
| 5977 | |
| 5978 | /* [expr.cond] |
| 5979 | |
| 5980 | The first expression is implicitly converted to bool (clause |
| 5981 | _conv_). */ |
| 5982 | arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain, |
| 5983 | LOOKUP_NORMAL); |
| 5984 | if (error_operand_p (t: arg1)) |
| 5985 | return error_mark_node; |
| 5986 | |
| 5987 | arg2_type = unlowered_expr_type (arg2); |
| 5988 | arg3_type = unlowered_expr_type (arg3); |
| 5989 | |
| 5990 | if ((TREE_CODE (arg2) == EXCESS_PRECISION_EXPR |
| 5991 | || TREE_CODE (arg3) == EXCESS_PRECISION_EXPR) |
| 5992 | && (TREE_CODE (arg2_type) == INTEGER_TYPE |
| 5993 | || SCALAR_FLOAT_TYPE_P (arg2_type) |
| 5994 | || TREE_CODE (arg2_type) == COMPLEX_TYPE) |
| 5995 | && (TREE_CODE (arg3_type) == INTEGER_TYPE |
| 5996 | || SCALAR_FLOAT_TYPE_P (arg3_type) |
| 5997 | || TREE_CODE (arg3_type) == COMPLEX_TYPE)) |
| 5998 | { |
| 5999 | semantic_result_type |
| 6000 | = type_after_usual_arithmetic_conversions (arg2_type, arg3_type); |
| 6001 | if (semantic_result_type == error_mark_node) |
| 6002 | { |
| 6003 | tree t1 = arg2_type; |
| 6004 | tree t2 = arg3_type; |
| 6005 | if (TREE_CODE (t1) == COMPLEX_TYPE) |
| 6006 | t1 = TREE_TYPE (t1); |
| 6007 | if (TREE_CODE (t2) == COMPLEX_TYPE) |
| 6008 | t2 = TREE_TYPE (t2); |
| 6009 | gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1) |
| 6010 | && SCALAR_FLOAT_TYPE_P (t2) |
| 6011 | && (extended_float_type_p (t1) |
| 6012 | || extended_float_type_p (t2)) |
| 6013 | && cp_compare_floating_point_conversion_ranks |
| 6014 | (t1, t2) == 3); |
| 6015 | if (complain & tf_error) |
| 6016 | error_at (loc, "operands to %<?:%> of types %qT and %qT " |
| 6017 | "have unordered conversion rank" , |
| 6018 | arg2_type, arg3_type); |
| 6019 | return error_mark_node; |
| 6020 | } |
| 6021 | if (TREE_CODE (arg2) == EXCESS_PRECISION_EXPR) |
| 6022 | { |
| 6023 | arg2 = TREE_OPERAND (arg2, 0); |
| 6024 | arg2_type = TREE_TYPE (arg2); |
| 6025 | } |
| 6026 | if (TREE_CODE (arg3) == EXCESS_PRECISION_EXPR) |
| 6027 | { |
| 6028 | arg3 = TREE_OPERAND (arg3, 0); |
| 6029 | arg3_type = TREE_TYPE (arg3); |
| 6030 | } |
| 6031 | } |
| 6032 | |
| 6033 | /* [expr.cond] |
| 6034 | |
| 6035 | If either the second or the third operand has type (possibly |
| 6036 | cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_), |
| 6037 | array-to-pointer (_conv.array_), and function-to-pointer |
| 6038 | (_conv.func_) standard conversions are performed on the second |
| 6039 | and third operands. */ |
| 6040 | if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type)) |
| 6041 | { |
| 6042 | /* 'void' won't help in resolving an overloaded expression on the |
| 6043 | other side, so require it to resolve by itself. */ |
| 6044 | if (arg2_type == unknown_type_node) |
| 6045 | { |
| 6046 | arg2 = resolve_nondeduced_context_or_error (arg2, complain); |
| 6047 | arg2_type = TREE_TYPE (arg2); |
| 6048 | } |
| 6049 | if (arg3_type == unknown_type_node) |
| 6050 | { |
| 6051 | arg3 = resolve_nondeduced_context_or_error (arg3, complain); |
| 6052 | arg3_type = TREE_TYPE (arg3); |
| 6053 | } |
| 6054 | |
| 6055 | /* [expr.cond] |
| 6056 | |
| 6057 | One of the following shall hold: |
| 6058 | |
| 6059 | --The second or the third operand (but not both) is a |
| 6060 | throw-expression (_except.throw_); the result is of the type |
| 6061 | and value category of the other. |
| 6062 | |
| 6063 | --Both the second and the third operands have type void; the |
| 6064 | result is of type void and is a prvalue. */ |
| 6065 | if (TREE_CODE (arg2) == THROW_EXPR |
| 6066 | && TREE_CODE (arg3) != THROW_EXPR) |
| 6067 | { |
| 6068 | result_type = arg3_type; |
| 6069 | is_glvalue = glvalue_p (arg3); |
| 6070 | } |
| 6071 | else if (TREE_CODE (arg2) != THROW_EXPR |
| 6072 | && TREE_CODE (arg3) == THROW_EXPR) |
| 6073 | { |
| 6074 | result_type = arg2_type; |
| 6075 | is_glvalue = glvalue_p (arg2); |
| 6076 | } |
| 6077 | else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type)) |
| 6078 | { |
| 6079 | result_type = void_type_node; |
| 6080 | is_glvalue = false; |
| 6081 | } |
| 6082 | else |
| 6083 | { |
| 6084 | if (complain & tf_error) |
| 6085 | { |
| 6086 | if (VOID_TYPE_P (arg2_type)) |
| 6087 | error_at (cp_expr_loc_or_loc (t: arg3, or_loc: loc), |
| 6088 | "second operand to the conditional operator " |
| 6089 | "is of type %<void%>, but the third operand is " |
| 6090 | "neither a throw-expression nor of type %<void%>" ); |
| 6091 | else |
| 6092 | error_at (cp_expr_loc_or_loc (t: arg2, or_loc: loc), |
| 6093 | "third operand to the conditional operator " |
| 6094 | "is of type %<void%>, but the second operand is " |
| 6095 | "neither a throw-expression nor of type %<void%>" ); |
| 6096 | } |
| 6097 | return error_mark_node; |
| 6098 | } |
| 6099 | |
| 6100 | goto valid_operands; |
| 6101 | } |
| 6102 | /* [expr.cond] |
| 6103 | |
| 6104 | Otherwise, if the second and third operand have different types, |
| 6105 | and either has (possibly cv-qualified) class type, or if both are |
| 6106 | glvalues of the same value category and the same type except for |
| 6107 | cv-qualification, an attempt is made to convert each of those operands |
| 6108 | to the type of the other. */ |
| 6109 | else if (!same_type_p (arg2_type, arg3_type) |
| 6110 | && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type) |
| 6111 | || (same_type_ignoring_top_level_qualifiers_p (arg2_type, |
| 6112 | arg3_type) |
| 6113 | && glvalue_p (arg2) && glvalue_p (arg3) |
| 6114 | && lvalue_p (arg2) == lvalue_p (arg3)))) |
| 6115 | { |
| 6116 | conversion *conv2; |
| 6117 | conversion *conv3; |
| 6118 | bool converted = false; |
| 6119 | |
| 6120 | conv2 = conditional_conversion (e1: arg2, e2: arg3, complain); |
| 6121 | conv3 = conditional_conversion (e1: arg3, e2: arg2, complain); |
| 6122 | |
| 6123 | /* [expr.cond] |
| 6124 | |
| 6125 | If both can be converted, or one can be converted but the |
| 6126 | conversion is ambiguous, the program is ill-formed. If |
| 6127 | neither can be converted, the operands are left unchanged and |
| 6128 | further checking is performed as described below. If exactly |
| 6129 | one conversion is possible, that conversion is applied to the |
| 6130 | chosen operand and the converted operand is used in place of |
| 6131 | the original operand for the remainder of this section. */ |
| 6132 | if ((conv2 && !conv2->bad_p |
| 6133 | && conv3 && !conv3->bad_p) |
| 6134 | || (conv2 && conv2->kind == ck_ambig) |
| 6135 | || (conv3 && conv3->kind == ck_ambig)) |
| 6136 | { |
| 6137 | if (complain & tf_error) |
| 6138 | { |
| 6139 | error_at (loc, "operands to %<?:%> have different types " |
| 6140 | "%qT and %qT" , |
| 6141 | arg2_type, arg3_type); |
| 6142 | if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p) |
| 6143 | inform (loc, " and each type can be converted to the other" ); |
| 6144 | else if (conv2 && conv2->kind == ck_ambig) |
| 6145 | convert_like (conv2, arg2, complain); |
| 6146 | else |
| 6147 | convert_like (conv3, arg3, complain); |
| 6148 | } |
| 6149 | result = error_mark_node; |
| 6150 | } |
| 6151 | else if (conv2 && !conv2->bad_p) |
| 6152 | { |
| 6153 | arg2 = convert_like (conv2, arg2, complain); |
| 6154 | arg2 = convert_from_reference (arg2); |
| 6155 | arg2_type = TREE_TYPE (arg2); |
| 6156 | /* Even if CONV2 is a valid conversion, the result of the |
| 6157 | conversion may be invalid. For example, if ARG3 has type |
| 6158 | "volatile X", and X does not have a copy constructor |
| 6159 | accepting a "volatile X&", then even if ARG2 can be |
| 6160 | converted to X, the conversion will fail. */ |
| 6161 | if (error_operand_p (t: arg2)) |
| 6162 | result = error_mark_node; |
| 6163 | converted = true; |
| 6164 | } |
| 6165 | else if (conv3 && !conv3->bad_p) |
| 6166 | { |
| 6167 | arg3 = convert_like (conv3, arg3, complain); |
| 6168 | arg3 = convert_from_reference (arg3); |
| 6169 | arg3_type = TREE_TYPE (arg3); |
| 6170 | if (error_operand_p (t: arg3)) |
| 6171 | result = error_mark_node; |
| 6172 | converted = true; |
| 6173 | } |
| 6174 | |
| 6175 | if (result) |
| 6176 | return result; |
| 6177 | |
| 6178 | /* If, after the conversion, both operands have class type, |
| 6179 | treat the cv-qualification of both operands as if it were the |
| 6180 | union of the cv-qualification of the operands. |
| 6181 | |
| 6182 | The standard is not clear about what to do in this |
| 6183 | circumstance. For example, if the first operand has type |
| 6184 | "const X" and the second operand has a user-defined |
| 6185 | conversion to "volatile X", what is the type of the second |
| 6186 | operand after this step? Making it be "const X" (matching |
| 6187 | the first operand) seems wrong, as that discards the |
| 6188 | qualification without actually performing a copy. Leaving it |
| 6189 | as "volatile X" seems wrong as that will result in the |
| 6190 | conditional expression failing altogether, even though, |
| 6191 | according to this step, the one operand could be converted to |
| 6192 | the type of the other. */ |
| 6193 | if (converted |
| 6194 | && CLASS_TYPE_P (arg2_type) |
| 6195 | && cp_type_quals (arg2_type) != cp_type_quals (arg3_type)) |
| 6196 | arg2_type = arg3_type = |
| 6197 | cp_build_qualified_type (arg2_type, |
| 6198 | cp_type_quals (arg2_type) |
| 6199 | | cp_type_quals (arg3_type)); |
| 6200 | } |
| 6201 | |
| 6202 | /* [expr.cond] |
| 6203 | |
| 6204 | If the second and third operands are glvalues of the same value |
| 6205 | category and have the same type, the result is of that type and |
| 6206 | value category. */ |
| 6207 | if (((lvalue_p (arg2) && lvalue_p (arg3)) |
| 6208 | || (xvalue_p (arg2) && xvalue_p (arg3))) |
| 6209 | && same_type_p (arg2_type, arg3_type)) |
| 6210 | { |
| 6211 | result_type = arg2_type; |
| 6212 | goto valid_operands; |
| 6213 | } |
| 6214 | |
| 6215 | /* [expr.cond] |
| 6216 | |
| 6217 | Otherwise, the result is an rvalue. If the second and third |
| 6218 | operand do not have the same type, and either has (possibly |
| 6219 | cv-qualified) class type, overload resolution is used to |
| 6220 | determine the conversions (if any) to be applied to the operands |
| 6221 | (_over.match.oper_, _over.built_). */ |
| 6222 | is_glvalue = false; |
| 6223 | if (!same_type_p (arg2_type, arg3_type) |
| 6224 | && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type))) |
| 6225 | { |
| 6226 | releasing_vec args; |
| 6227 | conversion *conv; |
| 6228 | bool any_viable_p; |
| 6229 | |
| 6230 | /* Rearrange the arguments so that add_builtin_candidate only has |
| 6231 | to know about two args. In build_builtin_candidate, the |
| 6232 | arguments are unscrambled. */ |
| 6233 | args->quick_push (obj: arg2); |
| 6234 | args->quick_push (obj: arg3); |
| 6235 | args->quick_push (obj: arg1); |
| 6236 | add_builtin_candidates (candidates: &candidates, |
| 6237 | code: COND_EXPR, |
| 6238 | code2: NOP_EXPR, |
| 6239 | fnname: ovl_op_identifier (isass: false, code: COND_EXPR), |
| 6240 | argv: args, |
| 6241 | LOOKUP_NORMAL, complain); |
| 6242 | |
| 6243 | /* [expr.cond] |
| 6244 | |
| 6245 | If the overload resolution fails, the program is |
| 6246 | ill-formed. */ |
| 6247 | candidates = splice_viable (cands: candidates, strict_p: false, any_viable_p: &any_viable_p); |
| 6248 | if (!any_viable_p) |
| 6249 | { |
| 6250 | if (complain & tf_error) |
| 6251 | error_at (loc, "operands to %<?:%> have different types %qT and %qT" , |
| 6252 | arg2_type, arg3_type); |
| 6253 | return error_mark_node; |
| 6254 | } |
| 6255 | cand = tourney (candidates, complain); |
| 6256 | if (!cand) |
| 6257 | { |
| 6258 | if (complain & tf_error) |
| 6259 | { |
| 6260 | auto_diagnostic_group d; |
| 6261 | op_error (loc, code: COND_EXPR, code2: NOP_EXPR, arg1, arg2, arg3, match: false); |
| 6262 | print_z_candidates (loc, candidates); |
| 6263 | } |
| 6264 | return error_mark_node; |
| 6265 | } |
| 6266 | |
| 6267 | /* [expr.cond] |
| 6268 | |
| 6269 | Otherwise, the conversions thus determined are applied, and |
| 6270 | the converted operands are used in place of the original |
| 6271 | operands for the remainder of this section. */ |
| 6272 | conv = cand->convs[0]; |
| 6273 | arg1 = convert_like (conv, arg1, complain); |
| 6274 | conv = cand->convs[1]; |
| 6275 | arg2 = convert_like (conv, arg2, complain); |
| 6276 | arg2_type = TREE_TYPE (arg2); |
| 6277 | conv = cand->convs[2]; |
| 6278 | arg3 = convert_like (conv, arg3, complain); |
| 6279 | arg3_type = TREE_TYPE (arg3); |
| 6280 | } |
| 6281 | |
| 6282 | /* [expr.cond] |
| 6283 | |
| 6284 | Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_), |
| 6285 | and function-to-pointer (_conv.func_) standard conversions are |
| 6286 | performed on the second and third operands. |
| 6287 | |
| 6288 | We need to force the lvalue-to-rvalue conversion here for class types, |
| 6289 | so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues |
| 6290 | that isn't wrapped with a TARGET_EXPR plays havoc with exception |
| 6291 | regions. */ |
| 6292 | |
| 6293 | arg2 = force_rvalue (arg2, complain); |
| 6294 | if (!CLASS_TYPE_P (arg2_type)) |
| 6295 | arg2_type = TREE_TYPE (arg2); |
| 6296 | |
| 6297 | arg3 = force_rvalue (arg3, complain); |
| 6298 | if (!CLASS_TYPE_P (arg3_type)) |
| 6299 | arg3_type = TREE_TYPE (arg3); |
| 6300 | |
| 6301 | if (arg2 == error_mark_node || arg3 == error_mark_node) |
| 6302 | return error_mark_node; |
| 6303 | |
| 6304 | /* [expr.cond] |
| 6305 | |
| 6306 | After those conversions, one of the following shall hold: |
| 6307 | |
| 6308 | --The second and third operands have the same type; the result is of |
| 6309 | that type. */ |
| 6310 | if (same_type_p (arg2_type, arg3_type)) |
| 6311 | result_type = arg2_type; |
| 6312 | /* [expr.cond] |
| 6313 | |
| 6314 | --The second and third operands have arithmetic or enumeration |
| 6315 | type; the usual arithmetic conversions are performed to bring |
| 6316 | them to a common type, and the result is of that type. */ |
| 6317 | else if ((ARITHMETIC_TYPE_P (arg2_type) |
| 6318 | || UNSCOPED_ENUM_P (arg2_type)) |
| 6319 | && (ARITHMETIC_TYPE_P (arg3_type) |
| 6320 | || UNSCOPED_ENUM_P (arg3_type))) |
| 6321 | { |
| 6322 | /* A conditional expression between a floating-point |
| 6323 | type and an integer type should convert the integer type to |
| 6324 | the evaluation format of the floating-point type, with |
| 6325 | possible excess precision. */ |
| 6326 | tree eptype2 = arg2_type; |
| 6327 | tree eptype3 = arg3_type; |
| 6328 | tree eptype; |
| 6329 | if (ANY_INTEGRAL_TYPE_P (arg2_type) |
| 6330 | && (eptype = excess_precision_type (arg3_type)) != NULL_TREE) |
| 6331 | { |
| 6332 | eptype3 = eptype; |
| 6333 | if (!semantic_result_type) |
| 6334 | semantic_result_type |
| 6335 | = type_after_usual_arithmetic_conversions (arg2_type, arg3_type); |
| 6336 | } |
| 6337 | else if (ANY_INTEGRAL_TYPE_P (arg3_type) |
| 6338 | && (eptype = excess_precision_type (arg2_type)) != NULL_TREE) |
| 6339 | { |
| 6340 | eptype2 = eptype; |
| 6341 | if (!semantic_result_type) |
| 6342 | semantic_result_type |
| 6343 | = type_after_usual_arithmetic_conversions (arg2_type, arg3_type); |
| 6344 | } |
| 6345 | result_type = type_after_usual_arithmetic_conversions (eptype2, |
| 6346 | eptype3); |
| 6347 | if (result_type == error_mark_node) |
| 6348 | { |
| 6349 | tree t1 = eptype2; |
| 6350 | tree t2 = eptype3; |
| 6351 | if (TREE_CODE (t1) == COMPLEX_TYPE) |
| 6352 | t1 = TREE_TYPE (t1); |
| 6353 | if (TREE_CODE (t2) == COMPLEX_TYPE) |
| 6354 | t2 = TREE_TYPE (t2); |
| 6355 | gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1) |
| 6356 | && SCALAR_FLOAT_TYPE_P (t2) |
| 6357 | && (extended_float_type_p (t1) |
| 6358 | || extended_float_type_p (t2)) |
| 6359 | && cp_compare_floating_point_conversion_ranks |
| 6360 | (t1, t2) == 3); |
| 6361 | if (complain & tf_error) |
| 6362 | error_at (loc, "operands to %<?:%> of types %qT and %qT " |
| 6363 | "have unordered conversion rank" , |
| 6364 | eptype2, eptype3); |
| 6365 | return error_mark_node; |
| 6366 | } |
| 6367 | if (semantic_result_type == error_mark_node) |
| 6368 | { |
| 6369 | tree t1 = arg2_type; |
| 6370 | tree t2 = arg3_type; |
| 6371 | if (TREE_CODE (t1) == COMPLEX_TYPE) |
| 6372 | t1 = TREE_TYPE (t1); |
| 6373 | if (TREE_CODE (t2) == COMPLEX_TYPE) |
| 6374 | t2 = TREE_TYPE (t2); |
| 6375 | gcc_checking_assert (SCALAR_FLOAT_TYPE_P (t1) |
| 6376 | && SCALAR_FLOAT_TYPE_P (t2) |
| 6377 | && (extended_float_type_p (t1) |
| 6378 | || extended_float_type_p (t2)) |
| 6379 | && cp_compare_floating_point_conversion_ranks |
| 6380 | (t1, t2) == 3); |
| 6381 | if (complain & tf_error) |
| 6382 | error_at (loc, "operands to %<?:%> of types %qT and %qT " |
| 6383 | "have unordered conversion rank" , |
| 6384 | arg2_type, arg3_type); |
| 6385 | return error_mark_node; |
| 6386 | } |
| 6387 | |
| 6388 | if (complain & tf_warning) |
| 6389 | do_warn_double_promotion (result_type, arg2_type, arg3_type, |
| 6390 | "implicit conversion from %qH to %qI to " |
| 6391 | "match other result of conditional" , |
| 6392 | loc); |
| 6393 | |
| 6394 | if (TREE_CODE (arg2_type) == ENUMERAL_TYPE |
| 6395 | && TREE_CODE (arg3_type) == ENUMERAL_TYPE) |
| 6396 | { |
| 6397 | tree stripped_orig_arg2 = tree_strip_any_location_wrapper (exp: orig_arg2); |
| 6398 | tree stripped_orig_arg3 = tree_strip_any_location_wrapper (exp: orig_arg3); |
| 6399 | if (TREE_CODE (stripped_orig_arg2) == CONST_DECL |
| 6400 | && TREE_CODE (stripped_orig_arg3) == CONST_DECL |
| 6401 | && (DECL_CONTEXT (stripped_orig_arg2) |
| 6402 | == DECL_CONTEXT (stripped_orig_arg3))) |
| 6403 | /* Two enumerators from the same enumeration can have different |
| 6404 | types when the enumeration is still being defined. */; |
| 6405 | else if (complain & (cxx_dialect >= cxx26 |
| 6406 | ? tf_warning_or_error : tf_warning)) |
| 6407 | emit_diagnostic (cxx_dialect >= cxx26 ? DK_PEDWARN : DK_WARNING, |
| 6408 | loc, OPT_Wenum_compare, "enumerated mismatch " |
| 6409 | "in conditional expression: %qT vs %qT" , |
| 6410 | arg2_type, arg3_type); |
| 6411 | else if (cxx_dialect >= cxx26) |
| 6412 | return error_mark_node; |
| 6413 | } |
| 6414 | else if ((((complain & (cxx_dialect >= cxx26 |
| 6415 | ? tf_warning_or_error : tf_warning)) |
| 6416 | && warn_deprecated_enum_float_conv) |
| 6417 | || (cxx_dialect >= cxx26 |
| 6418 | && (complain & tf_warning_or_error) == 0)) |
| 6419 | && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE |
| 6420 | && SCALAR_FLOAT_TYPE_P (arg3_type)) |
| 6421 | || (SCALAR_FLOAT_TYPE_P (arg2_type) |
| 6422 | && TREE_CODE (arg3_type) == ENUMERAL_TYPE))) |
| 6423 | { |
| 6424 | if (cxx_dialect >= cxx26 && (complain & tf_warning_or_error) == 0) |
| 6425 | return error_mark_node; |
| 6426 | if (cxx_dialect >= cxx26 && TREE_CODE (arg2_type) == ENUMERAL_TYPE) |
| 6427 | pedwarn (loc, OPT_Wdeprecated_enum_float_conversion, |
| 6428 | "conditional expression between enumeration type " |
| 6429 | "%qT and floating-point type %qT" , arg2_type, arg3_type); |
| 6430 | else if (cxx_dialect >= cxx26) |
| 6431 | pedwarn (loc, OPT_Wdeprecated_enum_float_conversion, |
| 6432 | "conditional expression between floating-point type " |
| 6433 | "%qT and enumeration type %qT" , arg2_type, arg3_type); |
| 6434 | else if (TREE_CODE (arg2_type) == ENUMERAL_TYPE) |
| 6435 | warning_at (loc, OPT_Wdeprecated_enum_float_conversion, |
| 6436 | "conditional expression between enumeration type " |
| 6437 | "%qT and floating-point type %qT is deprecated" , |
| 6438 | arg2_type, arg3_type); |
| 6439 | else |
| 6440 | warning_at (loc, OPT_Wdeprecated_enum_float_conversion, |
| 6441 | "conditional expression between floating-point " |
| 6442 | "type %qT and enumeration type %qT is deprecated" , |
| 6443 | arg2_type, arg3_type); |
| 6444 | } |
| 6445 | else if ((extra_warnings || warn_enum_conversion) |
| 6446 | && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE |
| 6447 | && !same_type_p (arg3_type, type_promotes_to (arg2_type))) |
| 6448 | || (TREE_CODE (arg3_type) == ENUMERAL_TYPE |
| 6449 | && !same_type_p (arg2_type, |
| 6450 | type_promotes_to (arg3_type))))) |
| 6451 | { |
| 6452 | if (complain & tf_warning) |
| 6453 | { |
| 6454 | enum opt_code opt = (warn_enum_conversion |
| 6455 | ? OPT_Wenum_conversion |
| 6456 | : OPT_Wextra); |
| 6457 | warning_at (loc, opt, "enumerated and " |
| 6458 | "non-enumerated type in conditional expression" ); |
| 6459 | } |
| 6460 | } |
| 6461 | |
| 6462 | arg2 = perform_implicit_conversion (result_type, arg2, complain); |
| 6463 | arg3 = perform_implicit_conversion (result_type, arg3, complain); |
| 6464 | } |
| 6465 | /* [expr.cond] |
| 6466 | |
| 6467 | --The second and third operands have pointer type, or one has |
| 6468 | pointer type and the other is a null pointer constant; pointer |
| 6469 | conversions (_conv.ptr_) and qualification conversions |
| 6470 | (_conv.qual_) are performed to bring them to their composite |
| 6471 | pointer type (_expr.rel_). The result is of the composite |
| 6472 | pointer type. |
| 6473 | |
| 6474 | --The second and third operands have pointer to member type, or |
| 6475 | one has pointer to member type and the other is a null pointer |
| 6476 | constant; pointer to member conversions (_conv.mem_) and |
| 6477 | qualification conversions (_conv.qual_) are performed to bring |
| 6478 | them to a common type, whose cv-qualification shall match the |
| 6479 | cv-qualification of either the second or the third operand. |
| 6480 | The result is of the common type. */ |
| 6481 | else if ((null_ptr_cst_p (t: arg2) |
| 6482 | && TYPE_PTR_OR_PTRMEM_P (arg3_type)) |
| 6483 | || (null_ptr_cst_p (t: arg3) |
| 6484 | && TYPE_PTR_OR_PTRMEM_P (arg2_type)) |
| 6485 | || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type)) |
| 6486 | || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type)) |
| 6487 | || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type))) |
| 6488 | { |
| 6489 | result_type = composite_pointer_type (loc, |
| 6490 | arg2_type, arg3_type, arg2, |
| 6491 | arg3, CPO_CONDITIONAL_EXPR, |
| 6492 | complain); |
| 6493 | if (result_type == error_mark_node) |
| 6494 | return error_mark_node; |
| 6495 | arg2 = perform_implicit_conversion (result_type, arg2, complain); |
| 6496 | arg3 = perform_implicit_conversion (result_type, arg3, complain); |
| 6497 | } |
| 6498 | |
| 6499 | if (!result_type) |
| 6500 | { |
| 6501 | if (complain & tf_error) |
| 6502 | error_at (loc, "operands to %<?:%> have different types %qT and %qT" , |
| 6503 | arg2_type, arg3_type); |
| 6504 | return error_mark_node; |
| 6505 | } |
| 6506 | |
| 6507 | if (arg2 == error_mark_node || arg3 == error_mark_node) |
| 6508 | return error_mark_node; |
| 6509 | |
| 6510 | valid_operands: |
| 6511 | if (processing_template_decl && is_glvalue) |
| 6512 | { |
| 6513 | /* Let lvalue_kind know this was a glvalue. */ |
| 6514 | tree arg = (result_type == arg2_type ? arg2 : arg3); |
| 6515 | result_type = cp_build_reference_type (result_type, xvalue_p (arg)); |
| 6516 | } |
| 6517 | |
| 6518 | result = build3_loc (loc, code: COND_EXPR, type: result_type, arg0: arg1, arg1: arg2, arg2: arg3); |
| 6519 | |
| 6520 | /* If the ARG2 and ARG3 are the same and don't have side-effects, |
| 6521 | warn here, because the COND_EXPR will be turned into ARG2. */ |
| 6522 | if (warn_duplicated_branches |
| 6523 | && (complain & tf_warning) |
| 6524 | && (arg2 == arg3 || operand_equal_p (arg2, arg3, |
| 6525 | flags: OEP_ADDRESS_OF_SAME_FIELD))) |
| 6526 | warning_at (EXPR_LOCATION (result), OPT_Wduplicated_branches, |
| 6527 | "this condition has identical branches" ); |
| 6528 | |
| 6529 | /* We can't use result_type below, as fold might have returned a |
| 6530 | throw_expr. */ |
| 6531 | |
| 6532 | if (!is_glvalue) |
| 6533 | { |
| 6534 | /* Expand both sides into the same slot, hopefully the target of |
| 6535 | the ?: expression. We used to check for TARGET_EXPRs here, |
| 6536 | but now we sometimes wrap them in NOP_EXPRs so the test would |
| 6537 | fail. */ |
| 6538 | if (CLASS_TYPE_P (TREE_TYPE (result))) |
| 6539 | { |
| 6540 | result = get_target_expr (result, complain); |
| 6541 | /* Tell gimplify_modify_expr_rhs not to strip this in |
| 6542 | assignment context: we want both arms to initialize |
| 6543 | the same temporary. */ |
| 6544 | TARGET_EXPR_NO_ELIDE (result) = true; |
| 6545 | } |
| 6546 | /* If this expression is an rvalue, but might be mistaken for an |
| 6547 | lvalue, we must add a NON_LVALUE_EXPR. */ |
| 6548 | result = rvalue (result); |
| 6549 | if (semantic_result_type) |
| 6550 | result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, |
| 6551 | result); |
| 6552 | } |
| 6553 | else |
| 6554 | { |
| 6555 | result = force_paren_expr (result); |
| 6556 | gcc_assert (semantic_result_type == NULL_TREE); |
| 6557 | } |
| 6558 | |
| 6559 | return result; |
| 6560 | } |
| 6561 | |
| 6562 | /* OPERAND is an operand to an expression. Perform necessary steps |
| 6563 | required before using it. If OPERAND is NULL_TREE, NULL_TREE is |
| 6564 | returned. */ |
| 6565 | |
| 6566 | static tree |
| 6567 | prep_operand (tree operand) |
| 6568 | { |
| 6569 | if (operand) |
| 6570 | { |
| 6571 | if (CLASS_TYPE_P (TREE_TYPE (operand)) |
| 6572 | && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand))) |
| 6573 | /* Make sure the template type is instantiated now. */ |
| 6574 | instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand))); |
| 6575 | } |
| 6576 | |
| 6577 | return operand; |
| 6578 | } |
| 6579 | |
| 6580 | /* True iff CONV represents a conversion sequence which no other can be better |
| 6581 | than under [over.ics.rank]: in other words, a "conversion" to the exact same |
| 6582 | type (including binding to a reference to the same type). This is stronger |
| 6583 | than the standard's "identity" category, which also includes reference |
| 6584 | bindings that add cv-qualifiers or change rvalueness. */ |
| 6585 | |
| 6586 | static bool |
| 6587 | perfect_conversion_p (conversion *conv) |
| 6588 | { |
| 6589 | if (CONVERSION_RANK (conv) != cr_identity) |
| 6590 | return false; |
| 6591 | if (conv->kind == ck_ref_bind) |
| 6592 | { |
| 6593 | if (!conv->rvaluedness_matches_p) |
| 6594 | return false; |
| 6595 | if (!same_type_p (TREE_TYPE (conv->type), |
| 6596 | next_conversion (conv)->type)) |
| 6597 | return false; |
| 6598 | } |
| 6599 | if (conv->check_narrowing) |
| 6600 | /* Brace elision is imperfect. */ |
| 6601 | return false; |
| 6602 | return true; |
| 6603 | } |
| 6604 | |
| 6605 | /* True if CAND represents a perfect match, i.e. all perfect conversions, so no |
| 6606 | other candidate can be a better match. Since the template/non-template |
| 6607 | tiebreaker comes immediately after the conversion comparison in |
| 6608 | [over.match.best], a perfect non-template candidate is better than all |
| 6609 | templates. */ |
| 6610 | |
| 6611 | static bool |
| 6612 | perfect_candidate_p (z_candidate *cand) |
| 6613 | { |
| 6614 | if (cand->viable < 1) |
| 6615 | return false; |
| 6616 | /* CWG1402 makes an implicitly deleted move op worse than other |
| 6617 | candidates. */ |
| 6618 | if (DECL_DELETED_FN (cand->fn) && DECL_DEFAULTED_FN (cand->fn) |
| 6619 | && move_fn_p (cand->fn)) |
| 6620 | return false; |
| 6621 | int len = cand->num_convs; |
| 6622 | for (int i = 0; i < len; ++i) |
| 6623 | if (!perfect_conversion_p (conv: cand->convs[i])) |
| 6624 | return false; |
| 6625 | if (conversion *conv = cand->second_conv) |
| 6626 | if (!perfect_conversion_p (conv)) |
| 6627 | return false; |
| 6628 | return true; |
| 6629 | } |
| 6630 | |
| 6631 | /* True iff one of CAND's argument conversions is missing. */ |
| 6632 | |
| 6633 | static bool |
| 6634 | missing_conversion_p (const z_candidate *cand) |
| 6635 | { |
| 6636 | for (unsigned i = 0; i < cand->num_convs; ++i) |
| 6637 | { |
| 6638 | conversion *conv = cand->convs[i]; |
| 6639 | if (!conv) |
| 6640 | return true; |
| 6641 | if (conv->kind == ck_deferred_bad) |
| 6642 | { |
| 6643 | /* We don't know whether this conversion is outright invalid or |
| 6644 | just bad, so conservatively assume it's missing. */ |
| 6645 | gcc_checking_assert (conv->bad_p); |
| 6646 | return true; |
| 6647 | } |
| 6648 | } |
| 6649 | return false; |
| 6650 | } |
| 6651 | |
| 6652 | /* Add each of the viable functions in FNS (a FUNCTION_DECL or |
| 6653 | OVERLOAD) to the CANDIDATES, returning an updated list of |
| 6654 | CANDIDATES. The ARGS are the arguments provided to the call; |
| 6655 | if FIRST_ARG is non-null it is the implicit object argument, |
| 6656 | otherwise the first element of ARGS is used if needed. The |
| 6657 | EXPLICIT_TARGS are explicit template arguments provided. |
| 6658 | TEMPLATE_ONLY is true if only template functions should be |
| 6659 | considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for |
| 6660 | add_function_candidate. */ |
| 6661 | |
| 6662 | static void |
| 6663 | add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args, |
| 6664 | tree return_type, |
| 6665 | tree explicit_targs, bool template_only, |
| 6666 | tree conversion_path, tree access_path, |
| 6667 | int flags, |
| 6668 | struct z_candidate **candidates, |
| 6669 | tsubst_flags_t complain) |
| 6670 | { |
| 6671 | tree ctype; |
| 6672 | const vec<tree, va_gc> *non_static_args; |
| 6673 | bool check_list_ctor = false; |
| 6674 | bool check_converting = false; |
| 6675 | unification_kind_t strict; |
| 6676 | tree ne_context = NULL_TREE; |
| 6677 | tree ne_fns = NULL_TREE; |
| 6678 | |
| 6679 | if (!fns) |
| 6680 | return; |
| 6681 | |
| 6682 | /* Precalculate special handling of constructors and conversion ops. */ |
| 6683 | tree fn = OVL_FIRST (fns); |
| 6684 | if (DECL_CONV_FN_P (fn)) |
| 6685 | { |
| 6686 | check_list_ctor = false; |
| 6687 | check_converting = (flags & LOOKUP_ONLYCONVERTING) != 0; |
| 6688 | if (flags & LOOKUP_NO_CONVERSION) |
| 6689 | /* We're doing return_type(x). */ |
| 6690 | strict = DEDUCE_CONV; |
| 6691 | else |
| 6692 | /* We're doing x.operator return_type(). */ |
| 6693 | strict = DEDUCE_EXACT; |
| 6694 | /* [over.match.funcs] For conversion functions, the function |
| 6695 | is considered to be a member of the class of the implicit |
| 6696 | object argument for the purpose of defining the type of |
| 6697 | the implicit object parameter. */ |
| 6698 | ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg)); |
| 6699 | } |
| 6700 | else |
| 6701 | { |
| 6702 | if (DECL_CONSTRUCTOR_P (fn)) |
| 6703 | { |
| 6704 | check_list_ctor = (flags & LOOKUP_LIST_ONLY) != 0; |
| 6705 | /* For list-initialization we consider explicit constructors |
| 6706 | and complain if one is chosen. */ |
| 6707 | check_converting |
| 6708 | = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR)) |
| 6709 | == LOOKUP_ONLYCONVERTING); |
| 6710 | } |
| 6711 | strict = DEDUCE_CALL; |
| 6712 | ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE; |
| 6713 | } |
| 6714 | |
| 6715 | /* P2468: Check if operator== is a rewrite target with first operand |
| 6716 | (*args)[0]; for now just do the lookups. */ |
| 6717 | if ((flags & (LOOKUP_REWRITTEN | LOOKUP_REVERSED)) |
| 6718 | && DECL_OVERLOADED_OPERATOR_IS (fn, EQ_EXPR)) |
| 6719 | { |
| 6720 | tree ne_name = ovl_op_identifier (isass: false, code: NE_EXPR); |
| 6721 | if (DECL_CLASS_SCOPE_P (fn)) |
| 6722 | { |
| 6723 | ne_context = DECL_CONTEXT (fn); |
| 6724 | ne_fns = lookup_fnfields (TREE_TYPE ((*args)[0]), ne_name, |
| 6725 | 1, tf_none); |
| 6726 | if (ne_fns == error_mark_node || ne_fns == NULL_TREE) |
| 6727 | ne_fns = NULL_TREE; |
| 6728 | else |
| 6729 | ne_fns = BASELINK_FUNCTIONS (ne_fns); |
| 6730 | } |
| 6731 | else |
| 6732 | { |
| 6733 | ne_context = decl_namespace_context (fn); |
| 6734 | ne_fns = lookup_qualified_name (scope: ne_context, name: ne_name, |
| 6735 | LOOK_want::NORMAL, |
| 6736 | /*complain*/false); |
| 6737 | if (ne_fns == error_mark_node |
| 6738 | || !is_overloaded_fn (ne_fns)) |
| 6739 | ne_fns = NULL_TREE; |
| 6740 | } |
| 6741 | } |
| 6742 | |
| 6743 | if (first_arg) |
| 6744 | non_static_args = args; |
| 6745 | else |
| 6746 | /* Delay creating the implicit this parameter until it is needed. */ |
| 6747 | non_static_args = NULL; |
| 6748 | |
| 6749 | bool seen_strictly_viable = any_strictly_viable (cands: *candidates); |
| 6750 | /* If there's a non-template perfect match, we don't need to consider |
| 6751 | templates. So check non-templates first. This optimization is only |
| 6752 | really needed for the defaulted copy constructor of tuple and the like |
| 6753 | (96926), but it seems like we might as well enable it more generally. */ |
| 6754 | bool seen_perfect = false; |
| 6755 | enum { templates, non_templates, either } which = either; |
| 6756 | if (template_only) |
| 6757 | which = templates; |
| 6758 | else /*if (flags & LOOKUP_DEFAULTED)*/ |
| 6759 | which = non_templates; |
| 6760 | |
| 6761 | /* Template candidates that we'll potentially ignore if the |
| 6762 | perfect candidate optimization succeeds. */ |
| 6763 | z_candidate *ignored_template_cands = nullptr; |
| 6764 | |
| 6765 | /* During overload resolution, we first consider each function under the |
| 6766 | assumption that we'll eventually find a strictly viable candidate. |
| 6767 | This allows us to circumvent our defacto behavior when checking |
| 6768 | argument conversions and shortcut consideration of the candidate |
| 6769 | upon encountering the first bad conversion. If this assumption |
| 6770 | turns out to be false, and all candidates end up being non-strictly |
| 6771 | viable, then we reconsider such candidates under the defacto behavior. |
| 6772 | This trick is important for pruning member function overloads according |
| 6773 | to their const/ref-qualifiers (since all 'this' conversions are at |
| 6774 | worst bad) without breaking -fpermissive. */ |
| 6775 | z_candidate *bad_cands = nullptr; |
| 6776 | bool shortcut_bad_convs = true; |
| 6777 | |
| 6778 | again: |
| 6779 | for (tree fn : lkp_range (fns)) |
| 6780 | { |
| 6781 | if (which == templates && TREE_CODE (fn) != TEMPLATE_DECL) |
| 6782 | { |
| 6783 | if (template_only) |
| 6784 | add_ignored_candidate (candidates, fn); |
| 6785 | continue; |
| 6786 | } |
| 6787 | if (which == non_templates && TREE_CODE (fn) == TEMPLATE_DECL) |
| 6788 | { |
| 6789 | add_ignored_candidate (candidates: &ignored_template_cands, fn); |
| 6790 | continue; |
| 6791 | } |
| 6792 | if ((check_converting && DECL_NONCONVERTING_P (fn)) |
| 6793 | || (check_list_ctor && !is_list_ctor (fn))) |
| 6794 | { |
| 6795 | add_ignored_candidate (candidates, fn); |
| 6796 | continue; |
| 6797 | } |
| 6798 | |
| 6799 | tree fn_first_arg = NULL_TREE; |
| 6800 | const vec<tree, va_gc> *fn_args = args; |
| 6801 | |
| 6802 | if (DECL_OBJECT_MEMBER_FUNCTION_P (fn)) |
| 6803 | { |
| 6804 | /* Figure out where the object arg comes from. If this |
| 6805 | function is a non-static member and we didn't get an |
| 6806 | implicit object argument, move it out of args. */ |
| 6807 | if (first_arg == NULL_TREE) |
| 6808 | { |
| 6809 | unsigned int ix; |
| 6810 | tree arg; |
| 6811 | vec<tree, va_gc> *tempvec; |
| 6812 | vec_alloc (v&: tempvec, nelems: args->length () - 1); |
| 6813 | for (ix = 1; args->iterate (ix, ptr: &arg); ++ix) |
| 6814 | tempvec->quick_push (obj: arg); |
| 6815 | non_static_args = tempvec; |
| 6816 | first_arg = (*args)[0]; |
| 6817 | } |
| 6818 | |
| 6819 | fn_first_arg = first_arg; |
| 6820 | fn_args = non_static_args; |
| 6821 | } |
| 6822 | |
| 6823 | /* Don't bother reversing an operator with two identical parameters. */ |
| 6824 | else if (vec_safe_length (v: args) == 2 && (flags & LOOKUP_REVERSED)) |
| 6825 | { |
| 6826 | tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
| 6827 | if (same_type_p (TREE_VALUE (parmlist), |
| 6828 | TREE_VALUE (TREE_CHAIN (parmlist)))) |
| 6829 | continue; |
| 6830 | } |
| 6831 | |
| 6832 | /* When considering reversed operator==, if there's a corresponding |
| 6833 | operator!= in the same scope, it's not a rewrite target. */ |
| 6834 | if (ne_context) |
| 6835 | { |
| 6836 | if (TREE_CODE (ne_context) == NAMESPACE_DECL) |
| 6837 | { |
| 6838 | /* With argument-dependent lookup, fns can span multiple |
| 6839 | namespaces; make sure we look in the fn's namespace for a |
| 6840 | corresponding operator!=. */ |
| 6841 | tree fn_ns = decl_namespace_context (fn); |
| 6842 | if (fn_ns != ne_context) |
| 6843 | { |
| 6844 | ne_context = fn_ns; |
| 6845 | tree ne_name = ovl_op_identifier (isass: false, code: NE_EXPR); |
| 6846 | ne_fns = lookup_qualified_name (scope: ne_context, name: ne_name, |
| 6847 | LOOK_want::NORMAL, |
| 6848 | /*complain*/false); |
| 6849 | if (ne_fns == error_mark_node |
| 6850 | || !is_overloaded_fn (ne_fns)) |
| 6851 | ne_fns = NULL_TREE; |
| 6852 | } |
| 6853 | } |
| 6854 | bool found = false; |
| 6855 | for (lkp_iterator ne (ne_fns); !found && ne; ++ne) |
| 6856 | if (0 && !ne.using_p () |
| 6857 | && DECL_NAMESPACE_SCOPE_P (fn) |
| 6858 | && DECL_CONTEXT (*ne) != DECL_CONTEXT (fn)) |
| 6859 | /* ??? This kludge excludes inline namespace members for the H |
| 6860 | test in spaceship-eq15.C, but I don't see why we would want |
| 6861 | that behavior. Asked Core 2022-11-04. Disabling for now. */; |
| 6862 | else if (fns_correspond (fn, *ne)) |
| 6863 | { |
| 6864 | found = true; |
| 6865 | break; |
| 6866 | } |
| 6867 | if (found) |
| 6868 | continue; |
| 6869 | } |
| 6870 | |
| 6871 | if (TREE_CODE (fn) == TEMPLATE_DECL) |
| 6872 | add_template_candidate (candidates, |
| 6873 | tmpl: fn, |
| 6874 | ctype, |
| 6875 | explicit_targs, |
| 6876 | first_arg: fn_first_arg, |
| 6877 | arglist: fn_args, |
| 6878 | return_type, |
| 6879 | access_path, |
| 6880 | conversion_path, |
| 6881 | flags, |
| 6882 | strict, |
| 6883 | shortcut_bad_convs, |
| 6884 | complain); |
| 6885 | else |
| 6886 | { |
| 6887 | add_function_candidate (candidates, |
| 6888 | fn, |
| 6889 | ctype, |
| 6890 | first_arg: fn_first_arg, |
| 6891 | args: fn_args, |
| 6892 | access_path, |
| 6893 | conversion_path, |
| 6894 | flags, |
| 6895 | NULL, |
| 6896 | shortcut_bad_convs, |
| 6897 | complain); |
| 6898 | if (perfect_candidate_p (cand: *candidates)) |
| 6899 | seen_perfect = true; |
| 6900 | } |
| 6901 | |
| 6902 | z_candidate *cand = *candidates; |
| 6903 | if (cand->viable == 1) |
| 6904 | seen_strictly_viable = true; |
| 6905 | |
| 6906 | if (cand->viable == -1 |
| 6907 | && shortcut_bad_convs |
| 6908 | && (missing_conversion_p (cand) |
| 6909 | || TREE_CODE (cand->fn) == TEMPLATE_DECL)) |
| 6910 | { |
| 6911 | /* This candidate has been tentatively marked non-strictly viable, |
| 6912 | and we didn't compute all argument conversions for it (having |
| 6913 | stopped at the first bad conversion). Move it to BAD_CANDS to |
| 6914 | to fully reconsider later if we don't find any strictly viable |
| 6915 | candidates. */ |
| 6916 | if (complain & (tf_error | tf_conv)) |
| 6917 | { |
| 6918 | *candidates = cand->next; |
| 6919 | cand->next = bad_cands; |
| 6920 | bad_cands = cand; |
| 6921 | } |
| 6922 | else |
| 6923 | /* But if we're in a SFINAE context, just mark this candidate as |
| 6924 | unviable outright and avoid potentially reconsidering it. |
| 6925 | This is safe to do because in a SFINAE context, performing a bad |
| 6926 | conversion is always an error (even with -fpermissive), so a |
| 6927 | non-strictly viable candidate is effectively unviable anyway. */ |
| 6928 | cand->viable = 0; |
| 6929 | } |
| 6930 | } |
| 6931 | if (which == non_templates && !seen_perfect) |
| 6932 | { |
| 6933 | which = templates; |
| 6934 | ignored_template_cands = nullptr; |
| 6935 | goto again; |
| 6936 | } |
| 6937 | else if (which == templates |
| 6938 | && !seen_strictly_viable |
| 6939 | && shortcut_bad_convs |
| 6940 | && bad_cands) |
| 6941 | { |
| 6942 | /* None of the candidates are strictly viable, so consider again those |
| 6943 | functions in BAD_CANDS, this time without shortcutting bad conversions |
| 6944 | so that all their argument conversions are computed. */ |
| 6945 | which = either; |
| 6946 | fns = NULL_TREE; |
| 6947 | for (z_candidate *cand = bad_cands; cand; cand = cand->next) |
| 6948 | { |
| 6949 | tree fn = cand->fn; |
| 6950 | if (tree ti = cand->template_decl) |
| 6951 | fn = TI_TEMPLATE (ti); |
| 6952 | fns = ovl_make (fn, next: fns); |
| 6953 | } |
| 6954 | shortcut_bad_convs = false; |
| 6955 | bad_cands = nullptr; |
| 6956 | goto again; |
| 6957 | } |
| 6958 | |
| 6959 | if (complain & tf_error) |
| 6960 | { |
| 6961 | /* Remember any omitted candidates; we may want to print all candidates |
| 6962 | as part of overload resolution failure diagnostics. */ |
| 6963 | for (z_candidate *omitted_cands : { ignored_template_cands, bad_cands }) |
| 6964 | { |
| 6965 | z_candidate **omitted_cands_tail = &omitted_cands; |
| 6966 | while (*omitted_cands_tail) |
| 6967 | omitted_cands_tail = &(*omitted_cands_tail)->next; |
| 6968 | *omitted_cands_tail = *candidates; |
| 6969 | *candidates = omitted_cands; |
| 6970 | } |
| 6971 | } |
| 6972 | } |
| 6973 | |
| 6974 | /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first, |
| 6975 | -1 if the RHS is evaluated first, or 0 if the order is unspecified. */ |
| 6976 | |
| 6977 | static int |
| 6978 | op_is_ordered (tree_code code) |
| 6979 | { |
| 6980 | switch (code) |
| 6981 | { |
| 6982 | // 5. b @= a |
| 6983 | case MODIFY_EXPR: |
| 6984 | return (flag_strong_eval_order > 1 ? -1 : 0); |
| 6985 | |
| 6986 | // 6. a[b] |
| 6987 | case ARRAY_REF: |
| 6988 | return (flag_strong_eval_order > 1 ? 1 : 0); |
| 6989 | |
| 6990 | // 1. a.b |
| 6991 | // Not overloadable (yet). |
| 6992 | // 2. a->b |
| 6993 | // Only one argument. |
| 6994 | // 3. a->*b |
| 6995 | case MEMBER_REF: |
| 6996 | // 7. a << b |
| 6997 | case LSHIFT_EXPR: |
| 6998 | // 8. a >> b |
| 6999 | case RSHIFT_EXPR: |
| 7000 | // a && b |
| 7001 | // Predates P0145R3. |
| 7002 | case TRUTH_ANDIF_EXPR: |
| 7003 | // a || b |
| 7004 | // Predates P0145R3. |
| 7005 | case TRUTH_ORIF_EXPR: |
| 7006 | // a , b |
| 7007 | // Predates P0145R3. |
| 7008 | case COMPOUND_EXPR: |
| 7009 | return (flag_strong_eval_order ? 1 : 0); |
| 7010 | |
| 7011 | default: |
| 7012 | return 0; |
| 7013 | } |
| 7014 | } |
| 7015 | |
| 7016 | /* Subroutine of build_new_op: Add to CANDIDATES all candidates for the |
| 7017 | operator indicated by CODE/CODE2. This function calls itself recursively to |
| 7018 | handle C++20 rewritten comparison operator candidates. Returns NULL_TREE |
| 7019 | upon success, and error_mark_node if something went wrong that prevented |
| 7020 | us from performing overload resolution (e.g. ambiguous member name lookup). |
| 7021 | |
| 7022 | LOOKUPS, if non-NULL, is the set of pertinent namespace-scope operator |
| 7023 | overloads to consider. This parameter is used when instantiating a |
| 7024 | dependent operator expression and has the same structure as |
| 7025 | DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS. */ |
| 7026 | |
| 7027 | static tree |
| 7028 | add_operator_candidates (z_candidate **candidates, |
| 7029 | tree_code code, tree_code code2, |
| 7030 | vec<tree, va_gc> *arglist, tree lookups, |
| 7031 | int flags, tsubst_flags_t complain) |
| 7032 | { |
| 7033 | z_candidate *start_candidates = *candidates; |
| 7034 | bool ismodop = code2 != ERROR_MARK; |
| 7035 | tree fnname = ovl_op_identifier (isass: ismodop, code: ismodop ? code2 : code); |
| 7036 | |
| 7037 | /* LOOKUP_REWRITTEN is set when we're looking for the == or <=> operator to |
| 7038 | rewrite from, and also when we're looking for the e.g. < operator to use |
| 7039 | on the result of <=>. In the latter case, we don't want the flag set in |
| 7040 | the candidate, we just want to suppress looking for rewrites. */ |
| 7041 | bool rewritten = (flags & LOOKUP_REWRITTEN); |
| 7042 | if (rewritten && code != EQ_EXPR && code != SPACESHIP_EXPR) |
| 7043 | flags &= ~LOOKUP_REWRITTEN; |
| 7044 | |
| 7045 | bool memonly = false; |
| 7046 | switch (code) |
| 7047 | { |
| 7048 | /* =, ->, [], () must be non-static member functions. */ |
| 7049 | case MODIFY_EXPR: |
| 7050 | if (code2 != NOP_EXPR) |
| 7051 | break; |
| 7052 | /* FALLTHRU */ |
| 7053 | case COMPONENT_REF: |
| 7054 | case ARRAY_REF: |
| 7055 | memonly = true; |
| 7056 | break; |
| 7057 | |
| 7058 | default: |
| 7059 | break; |
| 7060 | } |
| 7061 | |
| 7062 | /* Add namespace-scope operators to the list of functions to |
| 7063 | consider. */ |
| 7064 | if (!memonly) |
| 7065 | { |
| 7066 | tree fns; |
| 7067 | if (!lookups) |
| 7068 | fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE); |
| 7069 | /* If LOOKUPS is non-NULL, then we're instantiating a dependent operator |
| 7070 | expression, and LOOKUPS is the result of stage 1 name lookup. */ |
| 7071 | else if (tree found = purpose_member (fnname, lookups)) |
| 7072 | fns = TREE_VALUE (found); |
| 7073 | else |
| 7074 | fns = NULL_TREE; |
| 7075 | fns = lookup_arg_dependent (fnname, fns, arglist); |
| 7076 | add_candidates (fns, NULL_TREE, args: arglist, NULL_TREE, |
| 7077 | NULL_TREE, template_only: false, NULL_TREE, NULL_TREE, |
| 7078 | flags, candidates, complain); |
| 7079 | } |
| 7080 | |
| 7081 | /* Add class-member operators to the candidate set. */ |
| 7082 | tree arg1_type = TREE_TYPE ((*arglist)[0]); |
| 7083 | unsigned nargs = arglist->length () > 1 ? 2 : 1; |
| 7084 | tree arg2_type = nargs > 1 ? TREE_TYPE ((*arglist)[1]) : NULL_TREE; |
| 7085 | if (CLASS_TYPE_P (arg1_type)) |
| 7086 | { |
| 7087 | tree fns = lookup_fnfields (arg1_type, fnname, 1, complain); |
| 7088 | if (fns == error_mark_node) |
| 7089 | return error_mark_node; |
| 7090 | if (fns) |
| 7091 | { |
| 7092 | if (code == ARRAY_REF) |
| 7093 | { |
| 7094 | vec<tree,va_gc> *restlist = make_tree_vector (); |
| 7095 | for (unsigned i = 1; i < nargs; ++i) |
| 7096 | vec_safe_push (v&: restlist, obj: (*arglist)[i]); |
| 7097 | z_candidate *save_cand = *candidates; |
| 7098 | add_candidates (BASELINK_FUNCTIONS (fns), |
| 7099 | first_arg: (*arglist)[0], args: restlist, NULL_TREE, |
| 7100 | NULL_TREE, template_only: false, |
| 7101 | BASELINK_BINFO (fns), |
| 7102 | BASELINK_ACCESS_BINFO (fns), |
| 7103 | flags, candidates, complain); |
| 7104 | /* Release the vec if we didn't add a candidate that uses it. */ |
| 7105 | for (z_candidate *c = *candidates; c != save_cand; c = c->next) |
| 7106 | if (c->args == restlist) |
| 7107 | { |
| 7108 | restlist = NULL; |
| 7109 | break; |
| 7110 | } |
| 7111 | release_tree_vector (restlist); |
| 7112 | } |
| 7113 | else |
| 7114 | add_candidates (BASELINK_FUNCTIONS (fns), |
| 7115 | NULL_TREE, args: arglist, NULL_TREE, |
| 7116 | NULL_TREE, template_only: false, |
| 7117 | BASELINK_BINFO (fns), |
| 7118 | BASELINK_ACCESS_BINFO (fns), |
| 7119 | flags, candidates, complain); |
| 7120 | } |
| 7121 | } |
| 7122 | /* Per [over.match.oper]3.2, if no operand has a class type, then |
| 7123 | only non-member functions that have type T1 or reference to |
| 7124 | cv-qualified-opt T1 for the first argument, if the first argument |
| 7125 | has an enumeration type, or T2 or reference to cv-qualified-opt |
| 7126 | T2 for the second argument, if the second argument has an |
| 7127 | enumeration type. Filter out those that don't match. */ |
| 7128 | else if (! arg2_type || ! CLASS_TYPE_P (arg2_type)) |
| 7129 | { |
| 7130 | struct z_candidate **candp, **next; |
| 7131 | |
| 7132 | for (candp = candidates; *candp != start_candidates; candp = next) |
| 7133 | { |
| 7134 | unsigned i; |
| 7135 | z_candidate *cand = *candp; |
| 7136 | next = &cand->next; |
| 7137 | |
| 7138 | tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn)); |
| 7139 | |
| 7140 | for (i = 0; i < nargs; ++i) |
| 7141 | { |
| 7142 | tree parmtype = TREE_VALUE (parmlist); |
| 7143 | tree argtype = unlowered_expr_type ((*arglist)[i]); |
| 7144 | |
| 7145 | if (TYPE_REF_P (parmtype)) |
| 7146 | parmtype = TREE_TYPE (parmtype); |
| 7147 | if (TREE_CODE (argtype) == ENUMERAL_TYPE |
| 7148 | && (same_type_ignoring_top_level_qualifiers_p |
| 7149 | (argtype, parmtype))) |
| 7150 | break; |
| 7151 | |
| 7152 | parmlist = TREE_CHAIN (parmlist); |
| 7153 | } |
| 7154 | |
| 7155 | /* No argument has an appropriate type, so remove this |
| 7156 | candidate function from the list. */ |
| 7157 | if (i == nargs) |
| 7158 | { |
| 7159 | *candp = cand->next; |
| 7160 | next = candp; |
| 7161 | } |
| 7162 | } |
| 7163 | } |
| 7164 | |
| 7165 | if (!rewritten) |
| 7166 | { |
| 7167 | /* The standard says to rewrite built-in candidates, too, |
| 7168 | but there's no point. */ |
| 7169 | add_builtin_candidates (candidates, code, code2, fnname, argv: arglist, |
| 7170 | flags, complain); |
| 7171 | |
| 7172 | /* Maybe add C++20 rewritten comparison candidates. */ |
| 7173 | tree_code rewrite_code = ERROR_MARK; |
| 7174 | if (cxx_dialect >= cxx20 |
| 7175 | && nargs == 2 |
| 7176 | && (OVERLOAD_TYPE_P (arg1_type) || OVERLOAD_TYPE_P (arg2_type))) |
| 7177 | switch (code) |
| 7178 | { |
| 7179 | case LT_EXPR: |
| 7180 | case LE_EXPR: |
| 7181 | case GT_EXPR: |
| 7182 | case GE_EXPR: |
| 7183 | case SPACESHIP_EXPR: |
| 7184 | rewrite_code = SPACESHIP_EXPR; |
| 7185 | break; |
| 7186 | |
| 7187 | case NE_EXPR: |
| 7188 | case EQ_EXPR: |
| 7189 | rewrite_code = EQ_EXPR; |
| 7190 | break; |
| 7191 | |
| 7192 | default:; |
| 7193 | } |
| 7194 | |
| 7195 | if (rewrite_code) |
| 7196 | { |
| 7197 | tree r; |
| 7198 | flags |= LOOKUP_REWRITTEN; |
| 7199 | if (rewrite_code != code) |
| 7200 | { |
| 7201 | /* Add rewritten candidates in same order. */ |
| 7202 | r = add_operator_candidates (candidates, code: rewrite_code, code2: ERROR_MARK, |
| 7203 | arglist, lookups, flags, complain); |
| 7204 | if (r == error_mark_node) |
| 7205 | return error_mark_node; |
| 7206 | } |
| 7207 | |
| 7208 | z_candidate *save_cand = *candidates; |
| 7209 | |
| 7210 | /* Add rewritten candidates in reverse order. */ |
| 7211 | flags |= LOOKUP_REVERSED; |
| 7212 | vec<tree,va_gc> *revlist = make_tree_vector (); |
| 7213 | revlist->quick_push (obj: (*arglist)[1]); |
| 7214 | revlist->quick_push (obj: (*arglist)[0]); |
| 7215 | r = add_operator_candidates (candidates, code: rewrite_code, code2: ERROR_MARK, |
| 7216 | arglist: revlist, lookups, flags, complain); |
| 7217 | if (r == error_mark_node) |
| 7218 | return error_mark_node; |
| 7219 | |
| 7220 | /* Release the vec if we didn't add a candidate that uses it. */ |
| 7221 | for (z_candidate *c = *candidates; c != save_cand; c = c->next) |
| 7222 | if (c->args == revlist) |
| 7223 | { |
| 7224 | revlist = NULL; |
| 7225 | break; |
| 7226 | } |
| 7227 | release_tree_vector (revlist); |
| 7228 | } |
| 7229 | } |
| 7230 | |
| 7231 | return NULL_TREE; |
| 7232 | } |
| 7233 | |
| 7234 | tree |
| 7235 | build_new_op (const op_location_t &loc, enum tree_code code, int flags, |
| 7236 | tree arg1, tree arg2, tree arg3, tree lookups, |
| 7237 | tree *overload, tsubst_flags_t complain) |
| 7238 | { |
| 7239 | struct z_candidate *candidates = 0, *cand; |
| 7240 | releasing_vec arglist; |
| 7241 | tree result = NULL_TREE; |
| 7242 | bool result_valid_p = false; |
| 7243 | enum tree_code code2 = ERROR_MARK; |
| 7244 | enum tree_code code_orig_arg1 = ERROR_MARK; |
| 7245 | enum tree_code code_orig_arg2 = ERROR_MARK; |
| 7246 | bool strict_p; |
| 7247 | bool any_viable_p; |
| 7248 | |
| 7249 | auto_cond_timevar tv (TV_OVERLOAD); |
| 7250 | |
| 7251 | if (error_operand_p (t: arg1) |
| 7252 | || error_operand_p (t: arg2) |
| 7253 | || error_operand_p (t: arg3)) |
| 7254 | return error_mark_node; |
| 7255 | |
| 7256 | conversion_obstack_sentinel cos; |
| 7257 | |
| 7258 | bool ismodop = code == MODIFY_EXPR; |
| 7259 | if (ismodop) |
| 7260 | { |
| 7261 | code2 = TREE_CODE (arg3); |
| 7262 | arg3 = NULL_TREE; |
| 7263 | } |
| 7264 | |
| 7265 | tree arg1_type = unlowered_expr_type (arg1); |
| 7266 | tree arg2_type = arg2 ? unlowered_expr_type (arg2) : NULL_TREE; |
| 7267 | |
| 7268 | arg1 = prep_operand (operand: arg1); |
| 7269 | |
| 7270 | switch (code) |
| 7271 | { |
| 7272 | case NEW_EXPR: |
| 7273 | case VEC_NEW_EXPR: |
| 7274 | case VEC_DELETE_EXPR: |
| 7275 | case DELETE_EXPR: |
| 7276 | /* Use build_operator_new_call and build_op_delete_call instead. */ |
| 7277 | gcc_unreachable (); |
| 7278 | |
| 7279 | case CALL_EXPR: |
| 7280 | /* Use build_op_call instead. */ |
| 7281 | gcc_unreachable (); |
| 7282 | |
| 7283 | case TRUTH_ORIF_EXPR: |
| 7284 | case TRUTH_ANDIF_EXPR: |
| 7285 | case TRUTH_AND_EXPR: |
| 7286 | case TRUTH_OR_EXPR: |
| 7287 | /* These are saved for the sake of warn_logical_operator. */ |
| 7288 | code_orig_arg1 = TREE_CODE (arg1); |
| 7289 | code_orig_arg2 = TREE_CODE (arg2); |
| 7290 | break; |
| 7291 | case GT_EXPR: |
| 7292 | case LT_EXPR: |
| 7293 | case GE_EXPR: |
| 7294 | case LE_EXPR: |
| 7295 | case EQ_EXPR: |
| 7296 | case NE_EXPR: |
| 7297 | /* These are saved for the sake of maybe_warn_bool_compare. */ |
| 7298 | code_orig_arg1 = TREE_CODE (arg1_type); |
| 7299 | code_orig_arg2 = TREE_CODE (arg2_type); |
| 7300 | break; |
| 7301 | |
| 7302 | default: |
| 7303 | break; |
| 7304 | } |
| 7305 | |
| 7306 | arg2 = prep_operand (operand: arg2); |
| 7307 | arg3 = prep_operand (operand: arg3); |
| 7308 | |
| 7309 | if (code == COND_EXPR) |
| 7310 | /* Use build_conditional_expr instead. */ |
| 7311 | gcc_unreachable (); |
| 7312 | else if (! OVERLOAD_TYPE_P (arg1_type) |
| 7313 | && (! arg2 || ! OVERLOAD_TYPE_P (arg2_type))) |
| 7314 | goto builtin; |
| 7315 | |
| 7316 | if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR) |
| 7317 | { |
| 7318 | arg2 = integer_zero_node; |
| 7319 | arg2_type = integer_type_node; |
| 7320 | } |
| 7321 | |
| 7322 | arglist->quick_push (obj: arg1); |
| 7323 | if (arg2 != NULL_TREE) |
| 7324 | arglist->quick_push (obj: arg2); |
| 7325 | if (arg3 != NULL_TREE) |
| 7326 | arglist->quick_push (obj: arg3); |
| 7327 | |
| 7328 | result = add_operator_candidates (candidates: &candidates, code, code2, arglist, |
| 7329 | lookups, flags, complain); |
| 7330 | if (result == error_mark_node) |
| 7331 | return error_mark_node; |
| 7332 | |
| 7333 | switch (code) |
| 7334 | { |
| 7335 | case COMPOUND_EXPR: |
| 7336 | case ADDR_EXPR: |
| 7337 | /* For these, the built-in candidates set is empty |
| 7338 | [over.match.oper]/3. We don't want non-strict matches |
| 7339 | because exact matches are always possible with built-in |
| 7340 | operators. The built-in candidate set for COMPONENT_REF |
| 7341 | would be empty too, but since there are no such built-in |
| 7342 | operators, we accept non-strict matches for them. */ |
| 7343 | strict_p = true; |
| 7344 | break; |
| 7345 | |
| 7346 | default: |
| 7347 | strict_p = false; |
| 7348 | break; |
| 7349 | } |
| 7350 | |
| 7351 | candidates = splice_viable (cands: candidates, strict_p, any_viable_p: &any_viable_p); |
| 7352 | if (!any_viable_p) |
| 7353 | { |
| 7354 | switch (code) |
| 7355 | { |
| 7356 | case POSTINCREMENT_EXPR: |
| 7357 | case POSTDECREMENT_EXPR: |
| 7358 | /* Don't try anything fancy if we're not allowed to produce |
| 7359 | errors. */ |
| 7360 | if (!(complain & tf_error)) |
| 7361 | return error_mark_node; |
| 7362 | |
| 7363 | /* Look for an `operator++ (int)'. Pre-1985 C++ didn't |
| 7364 | distinguish between prefix and postfix ++ and |
| 7365 | operator++() was used for both, so we allow this with |
| 7366 | -fpermissive. */ |
| 7367 | else |
| 7368 | { |
| 7369 | tree fnname = ovl_op_identifier (isass: ismodop, code: ismodop ? code2 : code); |
| 7370 | const char *msg = (flag_permissive) |
| 7371 | ? G_("no %<%D(int)%> declared for postfix %qs," |
| 7372 | " trying prefix operator instead" ) |
| 7373 | : G_("no %<%D(int)%> declared for postfix %qs" ); |
| 7374 | permerror (loc, msg, fnname, OVL_OP_INFO (false, code)->name); |
| 7375 | } |
| 7376 | |
| 7377 | if (!flag_permissive) |
| 7378 | return error_mark_node; |
| 7379 | |
| 7380 | if (code == POSTINCREMENT_EXPR) |
| 7381 | code = PREINCREMENT_EXPR; |
| 7382 | else |
| 7383 | code = PREDECREMENT_EXPR; |
| 7384 | result = build_new_op (loc, code, flags, arg1, NULL_TREE, |
| 7385 | NULL_TREE, lookups, overload, complain); |
| 7386 | break; |
| 7387 | |
| 7388 | /* The caller will deal with these. */ |
| 7389 | case ADDR_EXPR: |
| 7390 | case COMPOUND_EXPR: |
| 7391 | case COMPONENT_REF: |
| 7392 | case CO_AWAIT_EXPR: |
| 7393 | result = NULL_TREE; |
| 7394 | result_valid_p = true; |
| 7395 | break; |
| 7396 | |
| 7397 | default: |
| 7398 | if (complain & tf_error) |
| 7399 | { |
| 7400 | /* If one of the arguments of the operator represents |
| 7401 | an invalid use of member function pointer, try to report |
| 7402 | a meaningful error ... */ |
| 7403 | if (invalid_nonstatic_memfn_p (loc, arg1, tf_error) |
| 7404 | || invalid_nonstatic_memfn_p (loc, arg2, tf_error) |
| 7405 | || invalid_nonstatic_memfn_p (loc, arg3, tf_error)) |
| 7406 | /* We displayed the error message. */; |
| 7407 | else |
| 7408 | { |
| 7409 | /* ... Otherwise, report the more generic |
| 7410 | "no matching operator found" error */ |
| 7411 | auto_diagnostic_group d; |
| 7412 | op_error (loc, code, code2, arg1, arg2, arg3, match: false); |
| 7413 | print_z_candidates (loc, candidates); |
| 7414 | } |
| 7415 | } |
| 7416 | result = error_mark_node; |
| 7417 | break; |
| 7418 | } |
| 7419 | } |
| 7420 | else |
| 7421 | { |
| 7422 | cand = tourney (candidates, complain); |
| 7423 | if (cand == 0) |
| 7424 | { |
| 7425 | if (complain & tf_error) |
| 7426 | { |
| 7427 | auto_diagnostic_group d; |
| 7428 | op_error (loc, code, code2, arg1, arg2, arg3, match: true); |
| 7429 | print_z_candidates (loc, candidates); |
| 7430 | } |
| 7431 | result = error_mark_node; |
| 7432 | if (overload) |
| 7433 | *overload = error_mark_node; |
| 7434 | } |
| 7435 | else if (TREE_CODE (cand->fn) == FUNCTION_DECL) |
| 7436 | { |
| 7437 | if (overload) |
| 7438 | *overload = cand->fn; |
| 7439 | |
| 7440 | if (resolve_args (args: arglist, complain) == NULL) |
| 7441 | result = error_mark_node; |
| 7442 | else |
| 7443 | { |
| 7444 | tsubst_flags_t ocomplain = complain; |
| 7445 | if (cand->rewritten ()) |
| 7446 | /* We'll wrap this call in another one. */ |
| 7447 | ocomplain &= ~tf_decltype; |
| 7448 | if (cand->reversed ()) |
| 7449 | { |
| 7450 | /* We swapped these in add_candidate, swap them back now. */ |
| 7451 | std::swap (a&: cand->convs[0], b&: cand->convs[1]); |
| 7452 | if (cand->fn == current_function_decl) |
| 7453 | warning_at (loc, 0, "in C++20 this comparison calls the " |
| 7454 | "current function recursively with reversed " |
| 7455 | "arguments" ); |
| 7456 | } |
| 7457 | result = build_over_call (cand, LOOKUP_NORMAL, ocomplain); |
| 7458 | } |
| 7459 | |
| 7460 | if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn)) |
| 7461 | /* There won't be a CALL_EXPR. */; |
| 7462 | else if (result && result != error_mark_node) |
| 7463 | { |
| 7464 | tree call = extract_call_expr (result); |
| 7465 | CALL_EXPR_OPERATOR_SYNTAX (call) = true; |
| 7466 | |
| 7467 | /* Specify evaluation order as per P0145R2. */ |
| 7468 | CALL_EXPR_ORDERED_ARGS (call) = false; |
| 7469 | switch (op_is_ordered (code)) |
| 7470 | { |
| 7471 | case -1: |
| 7472 | CALL_EXPR_REVERSE_ARGS (call) = true; |
| 7473 | break; |
| 7474 | |
| 7475 | case 1: |
| 7476 | CALL_EXPR_ORDERED_ARGS (call) = true; |
| 7477 | break; |
| 7478 | |
| 7479 | default: |
| 7480 | break; |
| 7481 | } |
| 7482 | } |
| 7483 | |
| 7484 | /* If this was a C++20 rewritten comparison, adjust the result. */ |
| 7485 | if (cand->rewritten ()) |
| 7486 | { |
| 7487 | /* FIXME build_min_non_dep_op_overload can't handle rewrites. */ |
| 7488 | if (overload) |
| 7489 | *overload = NULL_TREE; |
| 7490 | switch (code) |
| 7491 | { |
| 7492 | case EQ_EXPR: |
| 7493 | gcc_checking_assert (cand->reversed ()); |
| 7494 | gcc_fallthrough (); |
| 7495 | case NE_EXPR: |
| 7496 | if (result == error_mark_node) |
| 7497 | ; |
| 7498 | /* If a rewritten operator== candidate is selected by |
| 7499 | overload resolution for an operator @, its return type |
| 7500 | shall be cv bool.... */ |
| 7501 | else if (TREE_CODE (TREE_TYPE (result)) != BOOLEAN_TYPE) |
| 7502 | { |
| 7503 | if (complain & tf_error) |
| 7504 | { |
| 7505 | auto_diagnostic_group d; |
| 7506 | error_at (loc, "return type of %qD is not %qs" , |
| 7507 | cand->fn, "bool" ); |
| 7508 | inform (loc, "used as rewritten candidate for " |
| 7509 | "comparison of %qT and %qT" , |
| 7510 | arg1_type, arg2_type); |
| 7511 | } |
| 7512 | result = error_mark_node; |
| 7513 | } |
| 7514 | else if (code == NE_EXPR) |
| 7515 | /* !(y == x) or !(x == y) */ |
| 7516 | result = build1_loc (loc, code: TRUTH_NOT_EXPR, |
| 7517 | boolean_type_node, arg1: result); |
| 7518 | break; |
| 7519 | |
| 7520 | /* If a rewritten operator<=> candidate is selected by |
| 7521 | overload resolution for an operator @, x @ y is |
| 7522 | interpreted as 0 @ (y <=> x) if the selected candidate is |
| 7523 | a synthesized candidate with reversed order of parameters, |
| 7524 | or (x <=> y) @ 0 otherwise, using the selected rewritten |
| 7525 | operator<=> candidate. */ |
| 7526 | case SPACESHIP_EXPR: |
| 7527 | if (!cand->reversed ()) |
| 7528 | /* We're in the build_new_op call below for an outer |
| 7529 | reversed call; we don't need to do anything more. */ |
| 7530 | break; |
| 7531 | gcc_fallthrough (); |
| 7532 | case LT_EXPR: |
| 7533 | case LE_EXPR: |
| 7534 | case GT_EXPR: |
| 7535 | case GE_EXPR: |
| 7536 | { |
| 7537 | tree lhs = result; |
| 7538 | tree rhs = integer_zero_node; |
| 7539 | if (cand->reversed ()) |
| 7540 | std::swap (a&: lhs, b&: rhs); |
| 7541 | warning_sentinel ws (warn_zero_as_null_pointer_constant); |
| 7542 | result = build_new_op (loc, code, |
| 7543 | LOOKUP_NORMAL|LOOKUP_REWRITTEN, |
| 7544 | arg1: lhs, arg2: rhs, NULL_TREE, lookups, |
| 7545 | NULL, complain); |
| 7546 | } |
| 7547 | break; |
| 7548 | |
| 7549 | default: |
| 7550 | gcc_unreachable (); |
| 7551 | } |
| 7552 | } |
| 7553 | |
| 7554 | /* In an expression of the form `a[]' where cand->fn |
| 7555 | which is operator[] turns out to be a static member function, |
| 7556 | `a' is none-the-less evaluated. */ |
| 7557 | if (code == ARRAY_REF) |
| 7558 | result = keep_unused_object_arg (result, obj: arg1, fn: cand->fn); |
| 7559 | } |
| 7560 | else |
| 7561 | { |
| 7562 | /* Give any warnings we noticed during overload resolution. */ |
| 7563 | if (cand->warnings && (complain & tf_warning)) |
| 7564 | { |
| 7565 | struct candidate_warning *w; |
| 7566 | for (w = cand->warnings; w; w = w->next) |
| 7567 | joust (cand, w->loser, 1, complain); |
| 7568 | } |
| 7569 | |
| 7570 | /* Check for comparison of different enum types. */ |
| 7571 | switch (code) |
| 7572 | { |
| 7573 | case GT_EXPR: |
| 7574 | case LT_EXPR: |
| 7575 | case GE_EXPR: |
| 7576 | case LE_EXPR: |
| 7577 | case EQ_EXPR: |
| 7578 | case NE_EXPR: |
| 7579 | if (TREE_CODE (arg1_type) == ENUMERAL_TYPE |
| 7580 | && TREE_CODE (arg2_type) == ENUMERAL_TYPE |
| 7581 | && (TYPE_MAIN_VARIANT (arg1_type) |
| 7582 | != TYPE_MAIN_VARIANT (arg2_type))) |
| 7583 | { |
| 7584 | if (cxx_dialect >= cxx26 |
| 7585 | && (complain & tf_warning_or_error) == 0) |
| 7586 | result = error_mark_node; |
| 7587 | else if (cxx_dialect >= cxx26 || (complain & tf_warning)) |
| 7588 | emit_diagnostic (cxx_dialect >= cxx26 |
| 7589 | ? DK_PEDWARN : DK_WARNING, |
| 7590 | loc, OPT_Wenum_compare, |
| 7591 | "comparison between %q#T and %q#T" , |
| 7592 | arg1_type, arg2_type); |
| 7593 | } |
| 7594 | break; |
| 7595 | default: |
| 7596 | break; |
| 7597 | } |
| 7598 | |
| 7599 | /* "If a built-in candidate is selected by overload resolution, the |
| 7600 | operands of class type are converted to the types of the |
| 7601 | corresponding parameters of the selected operation function, |
| 7602 | except that the second standard conversion sequence of a |
| 7603 | user-defined conversion sequence (12.3.3.1.2) is not applied." */ |
| 7604 | conversion *conv = cand->convs[0]; |
| 7605 | if (conv->user_conv_p) |
| 7606 | { |
| 7607 | conv = strip_standard_conversion (conv); |
| 7608 | arg1 = convert_like (conv, arg1, complain); |
| 7609 | } |
| 7610 | |
| 7611 | if (arg2) |
| 7612 | { |
| 7613 | conv = cand->convs[1]; |
| 7614 | if (conv->user_conv_p) |
| 7615 | { |
| 7616 | conv = strip_standard_conversion (conv); |
| 7617 | arg2 = convert_like (conv, arg2, complain); |
| 7618 | } |
| 7619 | } |
| 7620 | |
| 7621 | if (arg3) |
| 7622 | { |
| 7623 | conv = cand->convs[2]; |
| 7624 | if (conv->user_conv_p) |
| 7625 | { |
| 7626 | conv = strip_standard_conversion (conv); |
| 7627 | arg3 = convert_like (conv, arg3, complain); |
| 7628 | } |
| 7629 | } |
| 7630 | } |
| 7631 | } |
| 7632 | |
| 7633 | if (result || result_valid_p) |
| 7634 | return result; |
| 7635 | |
| 7636 | builtin: |
| 7637 | switch (code) |
| 7638 | { |
| 7639 | case MODIFY_EXPR: |
| 7640 | return cp_build_modify_expr (loc, arg1, code2, arg2, complain); |
| 7641 | |
| 7642 | case INDIRECT_REF: |
| 7643 | return cp_build_indirect_ref (loc, arg1, RO_UNARY_STAR, complain); |
| 7644 | |
| 7645 | case TRUTH_ANDIF_EXPR: |
| 7646 | case TRUTH_ORIF_EXPR: |
| 7647 | case TRUTH_AND_EXPR: |
| 7648 | case TRUTH_OR_EXPR: |
| 7649 | if ((complain & tf_warning) && !processing_template_decl) |
| 7650 | warn_logical_operator (loc, code, boolean_type_node, |
| 7651 | code_orig_arg1, arg1, |
| 7652 | code_orig_arg2, arg2); |
| 7653 | /* Fall through. */ |
| 7654 | case GT_EXPR: |
| 7655 | case LT_EXPR: |
| 7656 | case GE_EXPR: |
| 7657 | case LE_EXPR: |
| 7658 | case EQ_EXPR: |
| 7659 | case NE_EXPR: |
| 7660 | if ((complain & tf_warning) |
| 7661 | && ((code_orig_arg1 == BOOLEAN_TYPE) |
| 7662 | ^ (code_orig_arg2 == BOOLEAN_TYPE))) |
| 7663 | maybe_warn_bool_compare (loc, code, arg1, arg2); |
| 7664 | if (complain & tf_warning && warn_tautological_compare) |
| 7665 | warn_tautological_cmp (loc, code, arg1, arg2); |
| 7666 | /* Fall through. */ |
| 7667 | case SPACESHIP_EXPR: |
| 7668 | case PLUS_EXPR: |
| 7669 | case MINUS_EXPR: |
| 7670 | case MULT_EXPR: |
| 7671 | case TRUNC_DIV_EXPR: |
| 7672 | case MAX_EXPR: |
| 7673 | case MIN_EXPR: |
| 7674 | case LSHIFT_EXPR: |
| 7675 | case RSHIFT_EXPR: |
| 7676 | case TRUNC_MOD_EXPR: |
| 7677 | case BIT_AND_EXPR: |
| 7678 | case BIT_IOR_EXPR: |
| 7679 | case BIT_XOR_EXPR: |
| 7680 | return cp_build_binary_op (loc, code, arg1, arg2, complain); |
| 7681 | |
| 7682 | case UNARY_PLUS_EXPR: |
| 7683 | case NEGATE_EXPR: |
| 7684 | case BIT_NOT_EXPR: |
| 7685 | case TRUTH_NOT_EXPR: |
| 7686 | case PREINCREMENT_EXPR: |
| 7687 | case POSTINCREMENT_EXPR: |
| 7688 | case PREDECREMENT_EXPR: |
| 7689 | case POSTDECREMENT_EXPR: |
| 7690 | case REALPART_EXPR: |
| 7691 | case IMAGPART_EXPR: |
| 7692 | case ABS_EXPR: |
| 7693 | case CO_AWAIT_EXPR: |
| 7694 | return cp_build_unary_op (code, arg1, false, complain); |
| 7695 | |
| 7696 | case ARRAY_REF: |
| 7697 | return cp_build_array_ref (input_location, arg1, arg2, complain); |
| 7698 | |
| 7699 | case MEMBER_REF: |
| 7700 | return build_m_component_ref (cp_build_indirect_ref (loc, arg1, |
| 7701 | RO_ARROW_STAR, |
| 7702 | complain), |
| 7703 | arg2, complain); |
| 7704 | |
| 7705 | /* The caller will deal with these. */ |
| 7706 | case ADDR_EXPR: |
| 7707 | case COMPONENT_REF: |
| 7708 | case COMPOUND_EXPR: |
| 7709 | return NULL_TREE; |
| 7710 | |
| 7711 | default: |
| 7712 | gcc_unreachable (); |
| 7713 | } |
| 7714 | return NULL_TREE; |
| 7715 | } |
| 7716 | |
| 7717 | /* Build a new call to operator[]. This may change ARGS. */ |
| 7718 | |
| 7719 | tree |
| 7720 | build_op_subscript (const op_location_t &loc, tree obj, |
| 7721 | vec<tree, va_gc> **args, tree *overload, |
| 7722 | tsubst_flags_t complain) |
| 7723 | { |
| 7724 | struct z_candidate *candidates = 0, *cand; |
| 7725 | tree fns, first_mem_arg = NULL_TREE; |
| 7726 | bool any_viable_p; |
| 7727 | tree result = NULL_TREE; |
| 7728 | |
| 7729 | auto_cond_timevar tv (TV_OVERLOAD); |
| 7730 | |
| 7731 | obj = mark_lvalue_use (obj); |
| 7732 | |
| 7733 | if (error_operand_p (t: obj)) |
| 7734 | return error_mark_node; |
| 7735 | |
| 7736 | tree type = TREE_TYPE (obj); |
| 7737 | |
| 7738 | obj = prep_operand (operand: obj); |
| 7739 | |
| 7740 | if (TYPE_BINFO (type)) |
| 7741 | { |
| 7742 | fns = lookup_fnfields (TYPE_BINFO (type), ovl_op_identifier (code: ARRAY_REF), |
| 7743 | 1, complain); |
| 7744 | if (fns == error_mark_node) |
| 7745 | return error_mark_node; |
| 7746 | } |
| 7747 | else |
| 7748 | fns = NULL_TREE; |
| 7749 | |
| 7750 | if (args != NULL && *args != NULL) |
| 7751 | { |
| 7752 | *args = resolve_args (args: *args, complain); |
| 7753 | if (*args == NULL) |
| 7754 | return error_mark_node; |
| 7755 | } |
| 7756 | |
| 7757 | conversion_obstack_sentinel cos; |
| 7758 | |
| 7759 | if (fns) |
| 7760 | { |
| 7761 | first_mem_arg = obj; |
| 7762 | |
| 7763 | add_candidates (BASELINK_FUNCTIONS (fns), |
| 7764 | first_arg: first_mem_arg, args: *args, NULL_TREE, |
| 7765 | NULL_TREE, template_only: false, |
| 7766 | BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns), |
| 7767 | LOOKUP_NORMAL, candidates: &candidates, complain); |
| 7768 | } |
| 7769 | |
| 7770 | /* Be strict here because if we choose a bad conversion candidate, the |
| 7771 | errors we get won't mention the call context. */ |
| 7772 | candidates = splice_viable (cands: candidates, strict_p: true, any_viable_p: &any_viable_p); |
| 7773 | if (!any_viable_p) |
| 7774 | { |
| 7775 | if (complain & tf_error) |
| 7776 | { |
| 7777 | auto_diagnostic_group d; |
| 7778 | error ("no match for call to %<%T::operator[] (%A)%>" , |
| 7779 | TREE_TYPE (obj), build_tree_list_vec (*args)); |
| 7780 | print_z_candidates (loc, candidates); |
| 7781 | } |
| 7782 | result = error_mark_node; |
| 7783 | } |
| 7784 | else |
| 7785 | { |
| 7786 | cand = tourney (candidates, complain); |
| 7787 | if (cand == 0) |
| 7788 | { |
| 7789 | if (complain & tf_error) |
| 7790 | { |
| 7791 | auto_diagnostic_group d; |
| 7792 | error ("call of %<%T::operator[] (%A)%> is ambiguous" , |
| 7793 | TREE_TYPE (obj), build_tree_list_vec (*args)); |
| 7794 | print_z_candidates (loc, candidates); |
| 7795 | } |
| 7796 | result = error_mark_node; |
| 7797 | } |
| 7798 | else if (TREE_CODE (cand->fn) == FUNCTION_DECL |
| 7799 | && DECL_OVERLOADED_OPERATOR_P (cand->fn) |
| 7800 | && DECL_OVERLOADED_OPERATOR_IS (cand->fn, ARRAY_REF)) |
| 7801 | { |
| 7802 | if (overload) |
| 7803 | *overload = cand->fn; |
| 7804 | result = build_over_call (cand, LOOKUP_NORMAL, complain); |
| 7805 | if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn)) |
| 7806 | /* There won't be a CALL_EXPR. */; |
| 7807 | else if (result && result != error_mark_node) |
| 7808 | { |
| 7809 | tree call = extract_call_expr (result); |
| 7810 | CALL_EXPR_OPERATOR_SYNTAX (call) = true; |
| 7811 | |
| 7812 | /* Specify evaluation order as per P0145R2. */ |
| 7813 | CALL_EXPR_ORDERED_ARGS (call) = op_is_ordered (code: ARRAY_REF) == 1; |
| 7814 | } |
| 7815 | |
| 7816 | /* In an expression of the form `a[]' where cand->fn |
| 7817 | which is operator[] turns out to be a static member function, |
| 7818 | `a' is none-the-less evaluated. */ |
| 7819 | result = keep_unused_object_arg (result, obj, fn: cand->fn); |
| 7820 | } |
| 7821 | else |
| 7822 | gcc_unreachable (); |
| 7823 | } |
| 7824 | |
| 7825 | return result; |
| 7826 | } |
| 7827 | |
| 7828 | /* CALL was returned by some call-building function; extract the actual |
| 7829 | CALL_EXPR from any bits that have been tacked on, e.g. by |
| 7830 | convert_from_reference. */ |
| 7831 | |
| 7832 | tree |
| 7833 | (tree call) |
| 7834 | { |
| 7835 | while (TREE_CODE (call) == COMPOUND_EXPR) |
| 7836 | call = TREE_OPERAND (call, 1); |
| 7837 | if (REFERENCE_REF_P (call)) |
| 7838 | call = TREE_OPERAND (call, 0); |
| 7839 | if (TREE_CODE (call) == TARGET_EXPR) |
| 7840 | call = TARGET_EXPR_INITIAL (call); |
| 7841 | if (cxx_dialect >= cxx20) |
| 7842 | switch (TREE_CODE (call)) |
| 7843 | { |
| 7844 | /* C++20 rewritten comparison operators. */ |
| 7845 | case TRUTH_NOT_EXPR: |
| 7846 | call = TREE_OPERAND (call, 0); |
| 7847 | break; |
| 7848 | case LT_EXPR: |
| 7849 | case LE_EXPR: |
| 7850 | case GT_EXPR: |
| 7851 | case GE_EXPR: |
| 7852 | case SPACESHIP_EXPR: |
| 7853 | { |
| 7854 | tree op0 = TREE_OPERAND (call, 0); |
| 7855 | if (integer_zerop (op0)) |
| 7856 | call = TREE_OPERAND (call, 1); |
| 7857 | else |
| 7858 | call = op0; |
| 7859 | } |
| 7860 | break; |
| 7861 | default:; |
| 7862 | } |
| 7863 | |
| 7864 | if (TREE_CODE (call) != CALL_EXPR |
| 7865 | && TREE_CODE (call) != AGGR_INIT_EXPR |
| 7866 | && call != error_mark_node) |
| 7867 | return NULL_TREE; |
| 7868 | return call; |
| 7869 | } |
| 7870 | |
| 7871 | /* Returns true if FN has two parameters, of which the second has type |
| 7872 | size_t. */ |
| 7873 | |
| 7874 | static bool |
| 7875 | second_parm_is_size_t (tree fn) |
| 7876 | { |
| 7877 | tree t = FUNCTION_ARG_CHAIN (fn); |
| 7878 | if (!t || !same_type_p (TREE_VALUE (t), size_type_node)) |
| 7879 | return false; |
| 7880 | t = TREE_CHAIN (t); |
| 7881 | if (t == void_list_node) |
| 7882 | return true; |
| 7883 | return false; |
| 7884 | } |
| 7885 | |
| 7886 | /* True if T, an allocation function, has std::align_val_t as its second |
| 7887 | argument. */ |
| 7888 | |
| 7889 | bool |
| 7890 | aligned_allocation_fn_p (tree t) |
| 7891 | { |
| 7892 | if (!aligned_new_threshold) |
| 7893 | return false; |
| 7894 | |
| 7895 | tree a = FUNCTION_ARG_CHAIN (t); |
| 7896 | return (a && same_type_p (TREE_VALUE (a), align_type_node)); |
| 7897 | } |
| 7898 | |
| 7899 | /* True if T is std::destroying_delete_t. */ |
| 7900 | |
| 7901 | static bool |
| 7902 | std_destroying_delete_t_p (tree t) |
| 7903 | { |
| 7904 | return (TYPE_CONTEXT (t) == std_node |
| 7905 | && id_equal (TYPE_IDENTIFIER (t), str: "destroying_delete_t" )); |
| 7906 | } |
| 7907 | |
| 7908 | /* A deallocation function with at least two parameters whose second parameter |
| 7909 | type is of type std::destroying_delete_t is a destroying operator delete. A |
| 7910 | destroying operator delete shall be a class member function named operator |
| 7911 | delete. [ Note: Array deletion cannot use a destroying operator |
| 7912 | delete. --end note ] */ |
| 7913 | |
| 7914 | tree |
| 7915 | destroying_delete_p (tree t) |
| 7916 | { |
| 7917 | tree a = TYPE_ARG_TYPES (TREE_TYPE (t)); |
| 7918 | if (!a || !TREE_CHAIN (a)) |
| 7919 | return NULL_TREE; |
| 7920 | tree type = TREE_VALUE (TREE_CHAIN (a)); |
| 7921 | return std_destroying_delete_t_p (t: type) ? type : NULL_TREE; |
| 7922 | } |
| 7923 | |
| 7924 | struct dealloc_info |
| 7925 | { |
| 7926 | bool sized; |
| 7927 | bool aligned; |
| 7928 | tree destroying; |
| 7929 | }; |
| 7930 | |
| 7931 | /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation |
| 7932 | function (3.7.4.2 [basic.stc.dynamic.deallocation]). If so, and DI is |
| 7933 | non-null, also set *DI. */ |
| 7934 | |
| 7935 | static bool |
| 7936 | usual_deallocation_fn_p (tree t, dealloc_info *di) |
| 7937 | { |
| 7938 | if (di) *di = dealloc_info(); |
| 7939 | |
| 7940 | /* A template instance is never a usual deallocation function, |
| 7941 | regardless of its signature. */ |
| 7942 | if (TREE_CODE (t) == TEMPLATE_DECL |
| 7943 | || primary_template_specialization_p (t)) |
| 7944 | return false; |
| 7945 | |
| 7946 | /* A usual deallocation function is a deallocation function whose parameters |
| 7947 | after the first are |
| 7948 | - optionally, a parameter of type std::destroying_delete_t, then |
| 7949 | - optionally, a parameter of type std::size_t, then |
| 7950 | - optionally, a parameter of type std::align_val_t. */ |
| 7951 | bool global = DECL_NAMESPACE_SCOPE_P (t); |
| 7952 | tree chain = FUNCTION_ARG_CHAIN (t); |
| 7953 | if (chain && destroying_delete_p (t)) |
| 7954 | { |
| 7955 | if (di) di->destroying = TREE_VALUE (chain); |
| 7956 | chain = TREE_CHAIN (chain); |
| 7957 | } |
| 7958 | if (chain |
| 7959 | && (!global || flag_sized_deallocation) |
| 7960 | && same_type_p (TREE_VALUE (chain), size_type_node)) |
| 7961 | { |
| 7962 | if (di) di->sized = true; |
| 7963 | chain = TREE_CHAIN (chain); |
| 7964 | } |
| 7965 | if (chain && aligned_new_threshold |
| 7966 | && same_type_p (TREE_VALUE (chain), align_type_node)) |
| 7967 | { |
| 7968 | if (di) di->aligned = true; |
| 7969 | chain = TREE_CHAIN (chain); |
| 7970 | } |
| 7971 | return (chain == void_list_node); |
| 7972 | } |
| 7973 | |
| 7974 | /* Just return whether FN is a usual deallocation function. */ |
| 7975 | |
| 7976 | bool |
| 7977 | usual_deallocation_fn_p (tree fn) |
| 7978 | { |
| 7979 | return usual_deallocation_fn_p (t: fn, NULL); |
| 7980 | } |
| 7981 | |
| 7982 | /* Build a call to operator delete. This has to be handled very specially, |
| 7983 | because the restrictions on what signatures match are different from all |
| 7984 | other call instances. For a normal delete, only a delete taking (void *) |
| 7985 | or (void *, size_t) is accepted. For a placement delete, only an exact |
| 7986 | match with the placement new is accepted. |
| 7987 | |
| 7988 | CODE is either DELETE_EXPR or VEC_DELETE_EXPR. |
| 7989 | ADDR is the pointer to be deleted. |
| 7990 | SIZE is the size of the memory block to be deleted. |
| 7991 | GLOBAL_P is true if the delete-expression should not consider |
| 7992 | class-specific delete operators. |
| 7993 | CORO_P is true if the allocation is for a coroutine, where the two argument |
| 7994 | usual deallocation should be chosen in preference to the single argument |
| 7995 | version in a class context. |
| 7996 | PLACEMENT is the corresponding placement new call, or NULL_TREE. |
| 7997 | |
| 7998 | If this call to "operator delete" is being generated as part to |
| 7999 | deallocate memory allocated via a new-expression (as per [expr.new] |
| 8000 | which requires that if the initialization throws an exception then |
| 8001 | we call a deallocation function), then ALLOC_FN is the allocation |
| 8002 | function. */ |
| 8003 | |
| 8004 | static tree |
| 8005 | build_op_delete_call_1 (enum tree_code code, tree addr, tree size, |
| 8006 | bool global_p, bool coro_p, tree placement, |
| 8007 | tree alloc_fn, tsubst_flags_t complain) |
| 8008 | { |
| 8009 | tree fn = NULL_TREE; |
| 8010 | tree fns, fnname, type, t; |
| 8011 | dealloc_info di_fn = { }; |
| 8012 | |
| 8013 | if (addr == error_mark_node) |
| 8014 | return error_mark_node; |
| 8015 | |
| 8016 | type = strip_array_types (TREE_TYPE (TREE_TYPE (addr))); |
| 8017 | |
| 8018 | fnname = ovl_op_identifier (isass: false, code); |
| 8019 | |
| 8020 | if (CLASS_TYPE_P (type) |
| 8021 | && COMPLETE_TYPE_P (complete_type (type)) |
| 8022 | && !global_p) |
| 8023 | /* In [class.free] |
| 8024 | |
| 8025 | If the result of the lookup is ambiguous or inaccessible, or if |
| 8026 | the lookup selects a placement deallocation function, the |
| 8027 | program is ill-formed. |
| 8028 | |
| 8029 | Therefore, we ask lookup_fnfields to complain about ambiguity. */ |
| 8030 | { |
| 8031 | fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1, complain); |
| 8032 | if (fns == error_mark_node) |
| 8033 | return error_mark_node; |
| 8034 | } |
| 8035 | else |
| 8036 | fns = NULL_TREE; |
| 8037 | |
| 8038 | if (fns == NULL_TREE) |
| 8039 | fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE); |
| 8040 | |
| 8041 | /* Strip const and volatile from addr. */ |
| 8042 | tree oaddr = addr; |
| 8043 | addr = cp_convert (ptr_type_node, addr, complain); |
| 8044 | |
| 8045 | tree excluded_destroying = NULL_TREE; |
| 8046 | |
| 8047 | if (placement) |
| 8048 | { |
| 8049 | /* "A declaration of a placement deallocation function matches the |
| 8050 | declaration of a placement allocation function if it has the same |
| 8051 | number of parameters and, after parameter transformations (8.3.5), |
| 8052 | all parameter types except the first are identical." |
| 8053 | |
| 8054 | So we build up the function type we want and ask instantiate_type |
| 8055 | to get it for us. */ |
| 8056 | t = FUNCTION_ARG_CHAIN (alloc_fn); |
| 8057 | t = tree_cons (NULL_TREE, ptr_type_node, t); |
| 8058 | t = build_function_type (void_type_node, t); |
| 8059 | |
| 8060 | fn = instantiate_type (t, fns, tf_none); |
| 8061 | if (fn == error_mark_node) |
| 8062 | return NULL_TREE; |
| 8063 | |
| 8064 | fn = MAYBE_BASELINK_FUNCTIONS (fn); |
| 8065 | |
| 8066 | /* "If the lookup finds the two-parameter form of a usual deallocation |
| 8067 | function (3.7.4.2) and that function, considered as a placement |
| 8068 | deallocation function, would have been selected as a match for the |
| 8069 | allocation function, the program is ill-formed." */ |
| 8070 | if (second_parm_is_size_t (fn)) |
| 8071 | { |
| 8072 | const char *const msg1 |
| 8073 | = G_("exception cleanup for this placement new selects " |
| 8074 | "non-placement %<operator delete%>" ); |
| 8075 | const char *const msg2 |
| 8076 | = G_("%qD is a usual (non-placement) deallocation " |
| 8077 | "function in C++14 (or with %<-fsized-deallocation%>)" ); |
| 8078 | |
| 8079 | /* But if the class has an operator delete (void *), then that is |
| 8080 | the usual deallocation function, so we shouldn't complain |
| 8081 | about using the operator delete (void *, size_t). */ |
| 8082 | if (DECL_CLASS_SCOPE_P (fn)) |
| 8083 | for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns))) |
| 8084 | { |
| 8085 | if (usual_deallocation_fn_p (fn: elt) |
| 8086 | && FUNCTION_ARG_CHAIN (elt) == void_list_node) |
| 8087 | goto ok; |
| 8088 | } |
| 8089 | /* Before C++14 a two-parameter global deallocation function is |
| 8090 | always a placement deallocation function, but warn if |
| 8091 | -Wc++14-compat. */ |
| 8092 | else if (!flag_sized_deallocation) |
| 8093 | { |
| 8094 | if (complain & tf_warning) |
| 8095 | { |
| 8096 | auto_diagnostic_group d; |
| 8097 | if (warning (OPT_Wc__14_compat, msg1)) |
| 8098 | inform (DECL_SOURCE_LOCATION (fn), msg2, fn); |
| 8099 | } |
| 8100 | goto ok; |
| 8101 | } |
| 8102 | |
| 8103 | if (complain & tf_warning_or_error) |
| 8104 | { |
| 8105 | auto_diagnostic_group d; |
| 8106 | if (permerror (input_location, msg1)) |
| 8107 | { |
| 8108 | /* Only mention C++14 for namespace-scope delete. */ |
| 8109 | if (DECL_NAMESPACE_SCOPE_P (fn)) |
| 8110 | inform (DECL_SOURCE_LOCATION (fn), msg2, fn); |
| 8111 | else |
| 8112 | inform (DECL_SOURCE_LOCATION (fn), |
| 8113 | "%qD is a usual (non-placement) deallocation " |
| 8114 | "function" , fn); |
| 8115 | } |
| 8116 | } |
| 8117 | else |
| 8118 | return error_mark_node; |
| 8119 | ok:; |
| 8120 | } |
| 8121 | } |
| 8122 | else |
| 8123 | /* "Any non-placement deallocation function matches a non-placement |
| 8124 | allocation function. If the lookup finds a single matching |
| 8125 | deallocation function, that function will be called; otherwise, no |
| 8126 | deallocation function will be called." */ |
| 8127 | for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns))) |
| 8128 | { |
| 8129 | dealloc_info di_elt; |
| 8130 | if (usual_deallocation_fn_p (t: elt, di: &di_elt)) |
| 8131 | { |
| 8132 | /* If we're called for an EH cleanup in a new-expression, we can't |
| 8133 | use a destroying delete; the exception was thrown before the |
| 8134 | object was constructed. */ |
| 8135 | if (alloc_fn && di_elt.destroying) |
| 8136 | { |
| 8137 | excluded_destroying = elt; |
| 8138 | continue; |
| 8139 | } |
| 8140 | |
| 8141 | if (!fn) |
| 8142 | { |
| 8143 | fn = elt; |
| 8144 | di_fn = di_elt; |
| 8145 | continue; |
| 8146 | } |
| 8147 | |
| 8148 | /* -- If any of the deallocation functions is a destroying |
| 8149 | operator delete, all deallocation functions that are not |
| 8150 | destroying operator deletes are eliminated from further |
| 8151 | consideration. */ |
| 8152 | if (di_elt.destroying != di_fn.destroying) |
| 8153 | { |
| 8154 | if (di_elt.destroying) |
| 8155 | { |
| 8156 | fn = elt; |
| 8157 | di_fn = di_elt; |
| 8158 | } |
| 8159 | continue; |
| 8160 | } |
| 8161 | |
| 8162 | /* -- If the type has new-extended alignment, a function with a |
| 8163 | parameter of type std::align_val_t is preferred; otherwise a |
| 8164 | function without such a parameter is preferred. If exactly one |
| 8165 | preferred function is found, that function is selected and the |
| 8166 | selection process terminates. If more than one preferred |
| 8167 | function is found, all non-preferred functions are eliminated |
| 8168 | from further consideration. */ |
| 8169 | if (aligned_new_threshold) |
| 8170 | { |
| 8171 | bool want_align = type_has_new_extended_alignment (type); |
| 8172 | if (di_elt.aligned != di_fn.aligned) |
| 8173 | { |
| 8174 | if (want_align == di_elt.aligned) |
| 8175 | { |
| 8176 | fn = elt; |
| 8177 | di_fn = di_elt; |
| 8178 | } |
| 8179 | continue; |
| 8180 | } |
| 8181 | } |
| 8182 | |
| 8183 | /* -- If the deallocation functions have class scope, the one |
| 8184 | without a parameter of type std::size_t is selected. */ |
| 8185 | bool want_size; |
| 8186 | if (DECL_CLASS_SCOPE_P (fn) && !coro_p) |
| 8187 | want_size = false; |
| 8188 | |
| 8189 | /* -- If the type is complete and if, for the second alternative |
| 8190 | (delete array) only, the operand is a pointer to a class type |
| 8191 | with a non-trivial destructor or a (possibly multi-dimensional) |
| 8192 | array thereof, the function with a parameter of type std::size_t |
| 8193 | is selected. |
| 8194 | |
| 8195 | -- Otherwise, it is unspecified whether a deallocation function |
| 8196 | with a parameter of type std::size_t is selected. */ |
| 8197 | else |
| 8198 | { |
| 8199 | want_size = COMPLETE_TYPE_P (type); |
| 8200 | if (code == VEC_DELETE_EXPR |
| 8201 | && !TYPE_VEC_NEW_USES_COOKIE (type)) |
| 8202 | /* We need a cookie to determine the array size. */ |
| 8203 | want_size = false; |
| 8204 | } |
| 8205 | gcc_assert (di_fn.sized != di_elt.sized); |
| 8206 | if (want_size == di_elt.sized) |
| 8207 | { |
| 8208 | fn = elt; |
| 8209 | di_fn = di_elt; |
| 8210 | } |
| 8211 | } |
| 8212 | } |
| 8213 | |
| 8214 | /* If we have a matching function, call it. */ |
| 8215 | if (fn) |
| 8216 | { |
| 8217 | gcc_assert (TREE_CODE (fn) == FUNCTION_DECL); |
| 8218 | |
| 8219 | /* If the FN is a member function, make sure that it is |
| 8220 | accessible. */ |
| 8221 | if (BASELINK_P (fns)) |
| 8222 | perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn, |
| 8223 | complain); |
| 8224 | |
| 8225 | /* Core issue 901: It's ok to new a type with deleted delete. */ |
| 8226 | if (DECL_DELETED_FN (fn) && alloc_fn) |
| 8227 | return NULL_TREE; |
| 8228 | |
| 8229 | tree ret; |
| 8230 | if (placement) |
| 8231 | { |
| 8232 | /* The placement args might not be suitable for overload |
| 8233 | resolution at this point, so build the call directly. */ |
| 8234 | int nargs = call_expr_nargs (placement); |
| 8235 | tree *argarray = XALLOCAVEC (tree, nargs); |
| 8236 | int i; |
| 8237 | argarray[0] = addr; |
| 8238 | for (i = 1; i < nargs; i++) |
| 8239 | argarray[i] = CALL_EXPR_ARG (placement, i); |
| 8240 | if (!mark_used (fn, complain) && !(complain & tf_error)) |
| 8241 | return error_mark_node; |
| 8242 | ret = build_cxx_call (fn, nargs, argarray, complain); |
| 8243 | } |
| 8244 | else |
| 8245 | { |
| 8246 | tree destroying = di_fn.destroying; |
| 8247 | if (destroying) |
| 8248 | { |
| 8249 | /* Strip const and volatile from addr but retain the type of the |
| 8250 | object. */ |
| 8251 | tree rtype = TREE_TYPE (TREE_TYPE (oaddr)); |
| 8252 | rtype = cv_unqualified (rtype); |
| 8253 | rtype = TYPE_POINTER_TO (rtype); |
| 8254 | addr = cp_convert (rtype, oaddr, complain); |
| 8255 | destroying = build_functional_cast (input_location, |
| 8256 | destroying, NULL_TREE, |
| 8257 | complain); |
| 8258 | } |
| 8259 | |
| 8260 | releasing_vec args; |
| 8261 | args->quick_push (obj: addr); |
| 8262 | if (destroying) |
| 8263 | args->quick_push (obj: destroying); |
| 8264 | if (di_fn.sized) |
| 8265 | args->quick_push (obj: size); |
| 8266 | if (di_fn.aligned) |
| 8267 | { |
| 8268 | tree al = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (type)); |
| 8269 | args->quick_push (obj: al); |
| 8270 | } |
| 8271 | ret = cp_build_function_call_vec (fn, &args, complain); |
| 8272 | } |
| 8273 | |
| 8274 | /* Set this flag for all callers of this function. In addition to |
| 8275 | delete-expressions, this is called for deallocating coroutine state; |
| 8276 | treat that as an implicit delete-expression. This is also called for |
| 8277 | the delete if the constructor throws in a new-expression, and for a |
| 8278 | deleting destructor (which implements a delete-expression). */ |
| 8279 | /* But leave this flag off for destroying delete to avoid wrong |
| 8280 | assumptions in the optimizers. */ |
| 8281 | tree call = extract_call_expr (call: ret); |
| 8282 | if (TREE_CODE (call) == CALL_EXPR && !destroying_delete_p (t: fn)) |
| 8283 | CALL_FROM_NEW_OR_DELETE_P (call) = 1; |
| 8284 | |
| 8285 | return ret; |
| 8286 | } |
| 8287 | |
| 8288 | /* If there's only a destroying delete that we can't use because the |
| 8289 | object isn't constructed yet, and we used global new, use global |
| 8290 | delete as well. */ |
| 8291 | if (excluded_destroying |
| 8292 | && DECL_NAMESPACE_SCOPE_P (alloc_fn)) |
| 8293 | return build_op_delete_call (code, addr, size, true, placement, |
| 8294 | alloc_fn, complain); |
| 8295 | |
| 8296 | /* [expr.new] |
| 8297 | |
| 8298 | If no unambiguous matching deallocation function can be found, |
| 8299 | propagating the exception does not cause the object's memory to |
| 8300 | be freed. */ |
| 8301 | if (alloc_fn) |
| 8302 | { |
| 8303 | if ((complain & tf_warning) |
| 8304 | && !placement) |
| 8305 | { |
| 8306 | bool w = warning (0, |
| 8307 | "no corresponding deallocation function for %qD" , |
| 8308 | alloc_fn); |
| 8309 | if (w && excluded_destroying) |
| 8310 | inform (DECL_SOURCE_LOCATION (excluded_destroying), "destroying " |
| 8311 | "delete %qD cannot be used to release the allocated memory" |
| 8312 | " if the initialization throws because the object is not " |
| 8313 | "constructed yet" , excluded_destroying); |
| 8314 | } |
| 8315 | return NULL_TREE; |
| 8316 | } |
| 8317 | |
| 8318 | if (complain & tf_error) |
| 8319 | error ("no suitable %<operator %s%> for %qT" , |
| 8320 | OVL_OP_INFO (false, code)->name, type); |
| 8321 | return error_mark_node; |
| 8322 | } |
| 8323 | |
| 8324 | /* Arguments as per build_op_delete_call_1 (). */ |
| 8325 | |
| 8326 | tree |
| 8327 | build_op_delete_call (enum tree_code code, tree addr, tree size, bool global_p, |
| 8328 | tree placement, tree alloc_fn, tsubst_flags_t complain) |
| 8329 | { |
| 8330 | return build_op_delete_call_1 (code, addr, size, global_p, /*coro_p*/false, |
| 8331 | placement, alloc_fn, complain); |
| 8332 | } |
| 8333 | |
| 8334 | /* Arguments as per build_op_delete_call_1 (). */ |
| 8335 | |
| 8336 | tree |
| 8337 | build_coroutine_op_delete_call (enum tree_code code, tree addr, tree size, |
| 8338 | bool global_p, tree placement, tree alloc_fn, |
| 8339 | tsubst_flags_t complain) |
| 8340 | { |
| 8341 | return build_op_delete_call_1 (code, addr, size, global_p, /*coro_p*/true, |
| 8342 | placement, alloc_fn, complain); |
| 8343 | } |
| 8344 | |
| 8345 | /* Issue diagnostics about a disallowed access of DECL, using DIAG_DECL |
| 8346 | in the diagnostics. |
| 8347 | |
| 8348 | If ISSUE_ERROR is true, then issue an error about the access, followed |
| 8349 | by a note showing the declaration. Otherwise, just show the note. |
| 8350 | |
| 8351 | DIAG_DECL and DIAG_LOCATION will almost always be the same. |
| 8352 | DIAG_LOCATION is just another DECL. NO_ACCESS_REASON is an optional |
| 8353 | parameter used to specify why DECL wasn't accessible (e.g. ak_private |
| 8354 | would be because DECL was private). If not using NO_ACCESS_REASON, |
| 8355 | then it must be ak_none, and the access failure reason will be |
| 8356 | figured out by looking at the protection of DECL. */ |
| 8357 | |
| 8358 | void |
| 8359 | complain_about_access (tree decl, tree diag_decl, tree diag_location, |
| 8360 | bool issue_error, access_kind no_access_reason) |
| 8361 | { |
| 8362 | /* If we have not already figured out why DECL is inaccessible... */ |
| 8363 | if (no_access_reason == ak_none) |
| 8364 | { |
| 8365 | /* Examine the access of DECL to find out why. */ |
| 8366 | if (TREE_PRIVATE (decl)) |
| 8367 | no_access_reason = ak_private; |
| 8368 | else if (TREE_PROTECTED (decl)) |
| 8369 | no_access_reason = ak_protected; |
| 8370 | } |
| 8371 | |
| 8372 | /* Now generate an error message depending on calculated access. */ |
| 8373 | if (no_access_reason == ak_private) |
| 8374 | { |
| 8375 | if (issue_error) |
| 8376 | error ("%q#D is private within this context" , diag_decl); |
| 8377 | inform (DECL_SOURCE_LOCATION (diag_location), "declared private here" ); |
| 8378 | } |
| 8379 | else if (no_access_reason == ak_protected) |
| 8380 | { |
| 8381 | if (issue_error) |
| 8382 | error ("%q#D is protected within this context" , diag_decl); |
| 8383 | inform (DECL_SOURCE_LOCATION (diag_location), "declared protected here" ); |
| 8384 | } |
| 8385 | /* Couldn't figure out why DECL is inaccesible, so just say it's |
| 8386 | inaccessible. */ |
| 8387 | else |
| 8388 | { |
| 8389 | if (issue_error) |
| 8390 | error ("%q#D is inaccessible within this context" , diag_decl); |
| 8391 | inform (DECL_SOURCE_LOCATION (diag_decl), "declared here" ); |
| 8392 | } |
| 8393 | } |
| 8394 | |
| 8395 | /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a |
| 8396 | bitwise or of LOOKUP_* values. If any errors are warnings are |
| 8397 | generated, set *DIAGNOSTIC_FN to "error" or "warning", |
| 8398 | respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN |
| 8399 | to NULL. */ |
| 8400 | |
| 8401 | static tree |
| 8402 | build_temp (tree expr, tree type, int flags, |
| 8403 | diagnostic_t *diagnostic_kind, tsubst_flags_t complain) |
| 8404 | { |
| 8405 | int savew, savee; |
| 8406 | |
| 8407 | *diagnostic_kind = DK_UNSPECIFIED; |
| 8408 | |
| 8409 | /* If the source is a packed field, calling the copy constructor will require |
| 8410 | binding the field to the reference parameter to the copy constructor, and |
| 8411 | we'll end up with an infinite loop. If we can use a bitwise copy, then |
| 8412 | do that now. */ |
| 8413 | if ((lvalue_kind (expr) & clk_packed) |
| 8414 | && CLASS_TYPE_P (TREE_TYPE (expr)) |
| 8415 | && !type_has_nontrivial_copy_init (TREE_TYPE (expr))) |
| 8416 | return get_target_expr (expr, complain); |
| 8417 | |
| 8418 | /* In decltype, we might have decided not to wrap this call in a TARGET_EXPR. |
| 8419 | But it turns out to be a subexpression, so perform temporary |
| 8420 | materialization now. */ |
| 8421 | if (TREE_CODE (expr) == CALL_EXPR |
| 8422 | && CLASS_TYPE_P (type) |
| 8423 | && same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr))) |
| 8424 | expr = build_cplus_new (type, expr, complain); |
| 8425 | |
| 8426 | savew = warningcount + werrorcount, savee = errorcount; |
| 8427 | releasing_vec args (make_tree_vector_single (expr)); |
| 8428 | expr = build_special_member_call (NULL_TREE, complete_ctor_identifier, |
| 8429 | &args, type, flags, complain); |
| 8430 | if (warningcount + werrorcount > savew) |
| 8431 | *diagnostic_kind = DK_WARNING; |
| 8432 | else if (errorcount > savee) |
| 8433 | *diagnostic_kind = DK_ERROR; |
| 8434 | return expr; |
| 8435 | } |
| 8436 | |
| 8437 | /* Get any location for EXPR, falling back to input_location. |
| 8438 | |
| 8439 | If the result is in a system header and is the virtual location for |
| 8440 | a token coming from the expansion of a macro, unwind it to the |
| 8441 | location of the expansion point of the macro (e.g. to avoid the |
| 8442 | diagnostic being suppressed for expansions of NULL where "NULL" is |
| 8443 | in a system header). */ |
| 8444 | |
| 8445 | static location_t |
| 8446 | (tree expr) |
| 8447 | { |
| 8448 | location_t loc = EXPR_LOC_OR_LOC (expr, input_location); |
| 8449 | loc = expansion_point_location_if_in_system_header (loc); |
| 8450 | return loc; |
| 8451 | } |
| 8452 | |
| 8453 | /* Perform warnings about peculiar, but valid, conversions from/to NULL. |
| 8454 | Also handle a subset of zero as null warnings. |
| 8455 | EXPR is implicitly converted to type TOTYPE. |
| 8456 | FN and ARGNUM are used for diagnostics. */ |
| 8457 | |
| 8458 | static void |
| 8459 | conversion_null_warnings (tree totype, tree expr, tree fn, int argnum) |
| 8460 | { |
| 8461 | /* Issue warnings about peculiar, but valid, uses of NULL. */ |
| 8462 | if (TREE_CODE (totype) != BOOLEAN_TYPE |
| 8463 | && ARITHMETIC_TYPE_P (totype) |
| 8464 | && null_node_p (expr)) |
| 8465 | { |
| 8466 | location_t loc = get_location_for_expr_unwinding_for_system_header (expr); |
| 8467 | if (fn) |
| 8468 | { |
| 8469 | auto_diagnostic_group d; |
| 8470 | if (warning_at (loc, OPT_Wconversion_null, |
| 8471 | "passing NULL to non-pointer argument %P of %qD" , |
| 8472 | argnum, fn)) |
| 8473 | inform (get_fndecl_argument_location (fn, argnum), |
| 8474 | "declared here" ); |
| 8475 | } |
| 8476 | else |
| 8477 | warning_at (loc, OPT_Wconversion_null, |
| 8478 | "converting to non-pointer type %qT from NULL" , totype); |
| 8479 | } |
| 8480 | |
| 8481 | /* Issue warnings if "false" is converted to a NULL pointer */ |
| 8482 | else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE |
| 8483 | && TYPE_PTR_P (totype)) |
| 8484 | { |
| 8485 | location_t loc = get_location_for_expr_unwinding_for_system_header (expr); |
| 8486 | if (fn) |
| 8487 | { |
| 8488 | auto_diagnostic_group d; |
| 8489 | if (warning_at (loc, OPT_Wconversion_null, |
| 8490 | "converting %<false%> to pointer type for argument " |
| 8491 | "%P of %qD" , argnum, fn)) |
| 8492 | inform (get_fndecl_argument_location (fn, argnum), |
| 8493 | "declared here" ); |
| 8494 | } |
| 8495 | else |
| 8496 | warning_at (loc, OPT_Wconversion_null, |
| 8497 | "converting %<false%> to pointer type %qT" , totype); |
| 8498 | } |
| 8499 | /* Handle zero as null pointer warnings for cases other |
| 8500 | than EQ_EXPR and NE_EXPR */ |
| 8501 | else if ((TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype)) |
| 8502 | && null_ptr_cst_p (t: expr)) |
| 8503 | { |
| 8504 | location_t loc = get_location_for_expr_unwinding_for_system_header (expr); |
| 8505 | maybe_warn_zero_as_null_pointer_constant (expr, loc); |
| 8506 | } |
| 8507 | } |
| 8508 | |
| 8509 | /* We gave a diagnostic during a conversion. If this was in the second |
| 8510 | standard conversion sequence of a user-defined conversion sequence, say |
| 8511 | which user-defined conversion. */ |
| 8512 | |
| 8513 | static void |
| 8514 | maybe_print_user_conv_context (conversion *convs) |
| 8515 | { |
| 8516 | if (convs->user_conv_p) |
| 8517 | for (conversion *t = convs; t; t = next_conversion (conv: t)) |
| 8518 | if (t->kind == ck_user) |
| 8519 | { |
| 8520 | print_z_candidate (loc: 0, N_(" after user-defined conversion:" ), |
| 8521 | candidate: t->cand); |
| 8522 | break; |
| 8523 | } |
| 8524 | } |
| 8525 | |
| 8526 | /* Locate the parameter with the given index within FNDECL. |
| 8527 | ARGNUM is zero based, -1 indicates the `this' argument of a method. |
| 8528 | Return the location of the FNDECL itself if there are problems. */ |
| 8529 | |
| 8530 | location_t |
| 8531 | get_fndecl_argument_location (tree fndecl, int argnum) |
| 8532 | { |
| 8533 | /* The locations of implicitly-declared functions are likely to be |
| 8534 | more meaningful than those of their parameters. */ |
| 8535 | if (DECL_ARTIFICIAL (fndecl)) |
| 8536 | return DECL_SOURCE_LOCATION (fndecl); |
| 8537 | |
| 8538 | int i; |
| 8539 | tree param; |
| 8540 | |
| 8541 | /* Locate param by index within DECL_ARGUMENTS (fndecl). */ |
| 8542 | for (i = 0, param = FUNCTION_FIRST_USER_PARM (fndecl); |
| 8543 | i < argnum && param; |
| 8544 | i++, param = TREE_CHAIN (param)) |
| 8545 | ; |
| 8546 | |
| 8547 | /* If something went wrong (e.g. if we have a builtin and thus no arguments), |
| 8548 | return the location of FNDECL. */ |
| 8549 | if (param == NULL) |
| 8550 | return DECL_SOURCE_LOCATION (fndecl); |
| 8551 | |
| 8552 | return DECL_SOURCE_LOCATION (param); |
| 8553 | } |
| 8554 | |
| 8555 | /* If FNDECL is non-NULL, issue a note highlighting ARGNUM |
| 8556 | within its declaration (or the fndecl itself if something went |
| 8557 | wrong). */ |
| 8558 | |
| 8559 | void |
| 8560 | maybe_inform_about_fndecl_for_bogus_argument_init (tree fn, int argnum, |
| 8561 | const char *highlight_color) |
| 8562 | { |
| 8563 | if (fn) |
| 8564 | { |
| 8565 | gcc_rich_location richloc (get_fndecl_argument_location (fndecl: fn, argnum)); |
| 8566 | richloc.set_highlight_color (highlight_color); |
| 8567 | inform (&richloc, |
| 8568 | "initializing argument %P of %qD" , argnum, fn); |
| 8569 | } |
| 8570 | } |
| 8571 | |
| 8572 | /* Maybe warn about C++20 Conversions to arrays of unknown bound. C is |
| 8573 | the conversion, EXPR is the expression we're converting. */ |
| 8574 | |
| 8575 | static void |
| 8576 | maybe_warn_array_conv (location_t loc, conversion *c, tree expr) |
| 8577 | { |
| 8578 | if (cxx_dialect >= cxx20) |
| 8579 | return; |
| 8580 | |
| 8581 | tree type = TREE_TYPE (expr); |
| 8582 | type = strip_pointer_operator (type); |
| 8583 | |
| 8584 | if (TREE_CODE (type) != ARRAY_TYPE |
| 8585 | || TYPE_DOMAIN (type) == NULL_TREE) |
| 8586 | return; |
| 8587 | |
| 8588 | if (pedantic && conv_binds_to_array_of_unknown_bound (c)) |
| 8589 | pedwarn (loc, OPT_Wc__20_extensions, |
| 8590 | "conversions to arrays of unknown bound " |
| 8591 | "are only available with %<-std=c++20%> or %<-std=gnu++20%>" ); |
| 8592 | } |
| 8593 | |
| 8594 | /* We call this recursively in convert_like_internal. */ |
| 8595 | static tree convert_like (conversion *, tree, tree, int, bool, bool, bool, |
| 8596 | tsubst_flags_t); |
| 8597 | |
| 8598 | /* Adjust the result EXPR of a conversion to the expected type TOTYPE, which |
| 8599 | must be equivalent but might be a typedef. */ |
| 8600 | |
| 8601 | static tree |
| 8602 | maybe_adjust_type_name (tree type, tree expr, conversion_kind kind) |
| 8603 | { |
| 8604 | if (expr == error_mark_node |
| 8605 | || processing_template_decl) |
| 8606 | return expr; |
| 8607 | |
| 8608 | tree etype = TREE_TYPE (expr); |
| 8609 | if (etype == type) |
| 8610 | return expr; |
| 8611 | |
| 8612 | gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p (etype, type) |
| 8613 | || is_bitfield_expr_with_lowered_type (expr) |
| 8614 | || seen_error ()); |
| 8615 | |
| 8616 | if (SCALAR_TYPE_P (type) |
| 8617 | && (kind == ck_rvalue |
| 8618 | /* ??? We should be able to do this for ck_identity of more prvalue |
| 8619 | expressions, but checking !obvalue_p here breaks, so for now let's |
| 8620 | just handle NON_LVALUE_EXPR (such as the location wrapper for a |
| 8621 | literal). Maybe we want to express already-rvalue in the |
| 8622 | conversion somehow? */ |
| 8623 | || TREE_CODE (expr) == NON_LVALUE_EXPR)) |
| 8624 | expr = build_nop (type, expr); |
| 8625 | |
| 8626 | return expr; |
| 8627 | } |
| 8628 | |
| 8629 | /* Perform the conversions in CONVS on the expression EXPR. FN and |
| 8630 | ARGNUM are used for diagnostics. ARGNUM is zero based, -1 |
| 8631 | indicates the `this' argument of a method. INNER is nonzero when |
| 8632 | being called to continue a conversion chain. It is negative when a |
| 8633 | reference binding will be applied, positive otherwise. If |
| 8634 | ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious |
| 8635 | conversions will be emitted if appropriate. If C_CAST_P is true, |
| 8636 | this conversion is coming from a C-style cast; in that case, |
| 8637 | conversions to inaccessible bases are permitted. */ |
| 8638 | |
| 8639 | static tree |
| 8640 | convert_like_internal (conversion *convs, tree expr, tree fn, int argnum, |
| 8641 | bool issue_conversion_warnings, bool c_cast_p, |
| 8642 | bool nested_p, tsubst_flags_t complain) |
| 8643 | { |
| 8644 | tree totype = convs->type; |
| 8645 | diagnostic_t diag_kind; |
| 8646 | int flags; |
| 8647 | location_t loc = cp_expr_loc_or_input_loc (t: expr); |
| 8648 | |
| 8649 | if (convs->bad_p && !(complain & tf_error)) |
| 8650 | return error_mark_node; |
| 8651 | |
| 8652 | gcc_checking_assert (!TYPE_REF_P (TREE_TYPE (expr))); |
| 8653 | |
| 8654 | if (convs->bad_p |
| 8655 | && convs->kind != ck_user |
| 8656 | && convs->kind != ck_list |
| 8657 | && convs->kind != ck_ambig |
| 8658 | && (convs->kind != ck_ref_bind |
| 8659 | || (convs->user_conv_p && next_conversion (conv: convs)->bad_p)) |
| 8660 | && (convs->kind != ck_rvalue |
| 8661 | || SCALAR_TYPE_P (totype)) |
| 8662 | && convs->kind != ck_base) |
| 8663 | { |
| 8664 | int complained = 0; |
| 8665 | conversion *t = convs; |
| 8666 | |
| 8667 | /* Give a helpful error if this is bad because of excess braces. */ |
| 8668 | if (BRACE_ENCLOSED_INITIALIZER_P (expr) |
| 8669 | && SCALAR_TYPE_P (totype) |
| 8670 | && CONSTRUCTOR_NELTS (expr) > 0 |
| 8671 | && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value)) |
| 8672 | { |
| 8673 | complained = permerror (loc, "too many braces around initializer " |
| 8674 | "for %qT" , totype); |
| 8675 | while (BRACE_ENCLOSED_INITIALIZER_P (expr) |
| 8676 | && CONSTRUCTOR_NELTS (expr) == 1) |
| 8677 | expr = CONSTRUCTOR_ELT (expr, 0)->value; |
| 8678 | } |
| 8679 | |
| 8680 | /* Give a helpful error if this is bad because a conversion to bool |
| 8681 | from std::nullptr_t requires direct-initialization. */ |
| 8682 | if (NULLPTR_TYPE_P (TREE_TYPE (expr)) |
| 8683 | && TREE_CODE (totype) == BOOLEAN_TYPE) |
| 8684 | complained = permerror (loc, "converting to %qH from %qI requires " |
| 8685 | "direct-initialization" , |
| 8686 | totype, TREE_TYPE (expr)); |
| 8687 | |
| 8688 | if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (expr)) |
| 8689 | && SCALAR_FLOAT_TYPE_P (totype) |
| 8690 | && (extended_float_type_p (TREE_TYPE (expr)) |
| 8691 | || extended_float_type_p (type: totype))) |
| 8692 | switch (cp_compare_floating_point_conversion_ranks (TREE_TYPE (expr), |
| 8693 | totype)) |
| 8694 | { |
| 8695 | case 2: |
| 8696 | if (pedwarn (loc, OPT_Wnarrowing, "ISO C++ does not allow " |
| 8697 | "converting to %qH from %qI with greater " |
| 8698 | "conversion rank" , totype, TREE_TYPE (expr))) |
| 8699 | complained = 1; |
| 8700 | else if (!complained) |
| 8701 | complained = -1; |
| 8702 | break; |
| 8703 | case 3: |
| 8704 | if (pedwarn (loc, OPT_Wnarrowing, "ISO C++ does not allow " |
| 8705 | "converting to %qH from %qI with unordered " |
| 8706 | "conversion rank" , totype, TREE_TYPE (expr))) |
| 8707 | complained = 1; |
| 8708 | else if (!complained) |
| 8709 | complained = -1; |
| 8710 | break; |
| 8711 | default: |
| 8712 | break; |
| 8713 | } |
| 8714 | |
| 8715 | for (; t ; t = next_conversion (conv: t)) |
| 8716 | { |
| 8717 | if (t->kind == ck_user && t->cand->reason) |
| 8718 | { |
| 8719 | auto_diagnostic_group d; |
| 8720 | complained = permerror (loc, "invalid user-defined conversion " |
| 8721 | "from %qH to %qI" , TREE_TYPE (expr), |
| 8722 | totype); |
| 8723 | if (complained) |
| 8724 | print_z_candidate (loc, N_("candidate is:" ), candidate: t->cand); |
| 8725 | expr = convert_like (t, expr, fn, argnum, |
| 8726 | /*issue_conversion_warnings=*/false, |
| 8727 | /*c_cast_p=*/false, /*nested_p=*/true, |
| 8728 | complain); |
| 8729 | break; |
| 8730 | } |
| 8731 | else if (t->kind == ck_user || !t->bad_p) |
| 8732 | { |
| 8733 | expr = convert_like (t, expr, fn, argnum, |
| 8734 | /*issue_conversion_warnings=*/false, |
| 8735 | /*c_cast_p=*/false, /*nested_p=*/true, |
| 8736 | complain); |
| 8737 | if (t->bad_p) |
| 8738 | complained = 1; |
| 8739 | break; |
| 8740 | } |
| 8741 | else if (t->kind == ck_ambig) |
| 8742 | return convert_like (t, expr, fn, argnum, |
| 8743 | /*issue_conversion_warnings=*/false, |
| 8744 | /*c_cast_p=*/false, /*nested_p=*/true, |
| 8745 | complain); |
| 8746 | else if (t->kind == ck_identity) |
| 8747 | break; |
| 8748 | } |
| 8749 | if (!complained && expr != error_mark_node) |
| 8750 | { |
| 8751 | range_label_for_type_mismatch label (TREE_TYPE (expr), totype); |
| 8752 | gcc_rich_location richloc (loc, &label, highlight_colors::percent_h); |
| 8753 | complained = permerror (&richloc, |
| 8754 | "invalid conversion from %qH to %qI" , |
| 8755 | TREE_TYPE (expr), totype); |
| 8756 | if (complained) |
| 8757 | maybe_emit_indirection_note (loc, expr, expected_type: totype); |
| 8758 | } |
| 8759 | if (convs->kind == ck_ref_bind) |
| 8760 | expr = convert_to_reference (totype, expr, CONV_IMPLICIT, |
| 8761 | LOOKUP_NORMAL, NULL_TREE, |
| 8762 | complain); |
| 8763 | else |
| 8764 | expr = cp_convert (totype, expr, complain); |
| 8765 | if (complained == 1) |
| 8766 | maybe_inform_about_fndecl_for_bogus_argument_init |
| 8767 | (fn, argnum, highlight_color: highlight_colors::percent_i); |
| 8768 | return expr; |
| 8769 | } |
| 8770 | |
| 8771 | if (issue_conversion_warnings && (complain & tf_warning)) |
| 8772 | conversion_null_warnings (totype, expr, fn, argnum); |
| 8773 | |
| 8774 | switch (convs->kind) |
| 8775 | { |
| 8776 | case ck_user: |
| 8777 | { |
| 8778 | struct z_candidate *cand = convs->cand; |
| 8779 | |
| 8780 | if (cand == NULL) |
| 8781 | /* We chose the surrogate function from add_conv_candidate, now we |
| 8782 | actually need to build the conversion. */ |
| 8783 | cand = build_user_type_conversion_1 (totype, expr, |
| 8784 | LOOKUP_NO_CONVERSION, complain); |
| 8785 | |
| 8786 | tree convfn = cand->fn; |
| 8787 | |
| 8788 | /* When converting from an init list we consider explicit |
| 8789 | constructors, but actually trying to call one is an error. */ |
| 8790 | if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn) |
| 8791 | && BRACE_ENCLOSED_INITIALIZER_P (expr) |
| 8792 | /* Unless this is for direct-list-initialization. */ |
| 8793 | && (!CONSTRUCTOR_IS_DIRECT_INIT (expr) || convs->need_temporary_p) |
| 8794 | /* And in C++98 a default constructor can't be explicit. */ |
| 8795 | && cxx_dialect >= cxx11) |
| 8796 | { |
| 8797 | if (!(complain & tf_error)) |
| 8798 | return error_mark_node; |
| 8799 | location_t loc = location_of (expr); |
| 8800 | if (CONSTRUCTOR_NELTS (expr) == 0 |
| 8801 | && FUNCTION_FIRST_USER_PARMTYPE (convfn) != void_list_node) |
| 8802 | { |
| 8803 | auto_diagnostic_group d; |
| 8804 | if (pedwarn (loc, 0, "converting to %qT from initializer list " |
| 8805 | "would use explicit constructor %qD" , |
| 8806 | totype, convfn)) |
| 8807 | { |
| 8808 | inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here" , |
| 8809 | convfn); |
| 8810 | inform (loc, "in C++11 and above a default constructor " |
| 8811 | "can be explicit" ); |
| 8812 | } |
| 8813 | } |
| 8814 | else |
| 8815 | { |
| 8816 | auto_diagnostic_group d; |
| 8817 | error ("converting to %qT from initializer list would use " |
| 8818 | "explicit constructor %qD" , totype, convfn); |
| 8819 | inform (DECL_SOURCE_LOCATION (convfn), "%qD declared here" , |
| 8820 | convfn); |
| 8821 | } |
| 8822 | } |
| 8823 | |
| 8824 | /* If we're initializing from {}, it's value-initialization. */ |
| 8825 | if (BRACE_ENCLOSED_INITIALIZER_P (expr) |
| 8826 | && CONSTRUCTOR_NELTS (expr) == 0 |
| 8827 | && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype) |
| 8828 | && !processing_template_decl) |
| 8829 | { |
| 8830 | if (abstract_virtuals_error (NULL_TREE, totype, complain)) |
| 8831 | return error_mark_node; |
| 8832 | expr = build_value_init (totype, complain); |
| 8833 | expr = get_target_expr (expr, complain); |
| 8834 | if (expr != error_mark_node) |
| 8835 | TARGET_EXPR_LIST_INIT_P (expr) = true; |
| 8836 | return expr; |
| 8837 | } |
| 8838 | |
| 8839 | /* We don't know here whether EXPR is being used as an lvalue or |
| 8840 | rvalue, but we know it's read. */ |
| 8841 | mark_exp_read (expr); |
| 8842 | |
| 8843 | /* Give the conversion call the location of EXPR rather than the |
| 8844 | location of the context that caused the conversion. */ |
| 8845 | iloc_sentinel ils (loc); |
| 8846 | |
| 8847 | /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow |
| 8848 | any more UDCs. */ |
| 8849 | expr = build_over_call (cand, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION, |
| 8850 | complain); |
| 8851 | |
| 8852 | /* If this is a constructor or a function returning an aggr type, |
| 8853 | we need to build up a TARGET_EXPR. */ |
| 8854 | if (DECL_CONSTRUCTOR_P (convfn)) |
| 8855 | { |
| 8856 | expr = build_cplus_new (totype, expr, complain); |
| 8857 | |
| 8858 | /* Remember that this was list-initialization. */ |
| 8859 | if (convs->check_narrowing && expr != error_mark_node) |
| 8860 | TARGET_EXPR_LIST_INIT_P (expr) = true; |
| 8861 | } |
| 8862 | |
| 8863 | return expr; |
| 8864 | } |
| 8865 | case ck_identity: |
| 8866 | if (BRACE_ENCLOSED_INITIALIZER_P (expr)) |
| 8867 | { |
| 8868 | int nelts = CONSTRUCTOR_NELTS (expr); |
| 8869 | if (nelts == 0) |
| 8870 | expr = build_value_init (totype, complain); |
| 8871 | else if (nelts == 1) |
| 8872 | expr = CONSTRUCTOR_ELT (expr, 0)->value; |
| 8873 | else |
| 8874 | gcc_unreachable (); |
| 8875 | } |
| 8876 | expr = mark_use (expr, /*rvalue_p=*/!convs->rvaluedness_matches_p, |
| 8877 | /*read_p=*/true, UNKNOWN_LOCATION, |
| 8878 | /*reject_builtin=*/true); |
| 8879 | |
| 8880 | if (type_unknown_p (expr)) |
| 8881 | expr = instantiate_type (totype, expr, complain); |
| 8882 | if (!nested_p && TREE_CODE (expr) == EXCESS_PRECISION_EXPR) |
| 8883 | expr = cp_convert (totype, TREE_OPERAND (expr, 0), complain); |
| 8884 | if (expr == null_node |
| 8885 | && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype)) |
| 8886 | /* If __null has been converted to an integer type, we do not want to |
| 8887 | continue to warn about uses of EXPR as an integer, rather than as a |
| 8888 | pointer. */ |
| 8889 | expr = build_int_cst (totype, 0); |
| 8890 | return maybe_adjust_type_name (type: totype, expr, kind: convs->kind); |
| 8891 | case ck_ambig: |
| 8892 | /* We leave bad_p off ck_ambig because overload resolution considers |
| 8893 | it valid, it just fails when we try to perform it. So we need to |
| 8894 | check complain here, too. */ |
| 8895 | if (complain & tf_error) |
| 8896 | { |
| 8897 | /* Call build_user_type_conversion again for the error. */ |
| 8898 | int flags = (convs->need_temporary_p |
| 8899 | ? LOOKUP_IMPLICIT : LOOKUP_NORMAL); |
| 8900 | build_user_type_conversion (totype, expr: convs->u.expr, flags, complain); |
| 8901 | gcc_assert (seen_error ()); |
| 8902 | maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum); |
| 8903 | } |
| 8904 | return error_mark_node; |
| 8905 | |
| 8906 | case ck_list: |
| 8907 | { |
| 8908 | /* Conversion to std::initializer_list<T>. */ |
| 8909 | tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0); |
| 8910 | unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (expr); |
| 8911 | tree array; |
| 8912 | |
| 8913 | if (tree init = maybe_init_list_as_array (elttype, init: expr)) |
| 8914 | { |
| 8915 | elttype |
| 8916 | = cp_build_qualified_type (elttype, (cp_type_quals (elttype) |
| 8917 | | TYPE_QUAL_CONST)); |
| 8918 | tree index_type = TYPE_DOMAIN (TREE_TYPE (init)); |
| 8919 | array = build_cplus_array_type (elttype, index_type); |
| 8920 | len = TREE_INT_CST_LOW (TYPE_MAX_VALUE (index_type)) + 1; |
| 8921 | array = build_vec_init_expr (array, init, complain); |
| 8922 | array = get_target_expr (array); |
| 8923 | array = cp_build_addr_expr (array, complain); |
| 8924 | } |
| 8925 | else if (len) |
| 8926 | { |
| 8927 | tree val; |
| 8928 | unsigned ix; |
| 8929 | tree new_ctor = build_constructor (init_list_type_node, NULL); |
| 8930 | |
| 8931 | /* Convert all the elements. */ |
| 8932 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val) |
| 8933 | { |
| 8934 | if (TREE_CODE (val) == RAW_DATA_CST) |
| 8935 | { |
| 8936 | /* For conversion to initializer_list<unsigned char> or |
| 8937 | initializer_list<char> or initializer_list<signed char> |
| 8938 | we can optimize and keep RAW_DATA_CST with adjusted |
| 8939 | type if we report narrowing errors if needed, for |
| 8940 | others this converts each element separately. */ |
| 8941 | if (convs->u.list[ix]->kind == ck_std) |
| 8942 | { |
| 8943 | tree et = convs->u.list[ix]->type; |
| 8944 | conversion *next = next_conversion (conv: convs->u.list[ix]); |
| 8945 | gcc_assert (et |
| 8946 | && (TREE_CODE (et) == INTEGER_TYPE |
| 8947 | || is_byte_access_type (et)) |
| 8948 | && TYPE_PRECISION (et) == CHAR_BIT |
| 8949 | && next |
| 8950 | && next->kind == ck_identity); |
| 8951 | if (!TYPE_UNSIGNED (et) |
| 8952 | /* For RAW_DATA_CST, TREE_TYPE (val) can be |
| 8953 | either integer_type_node (when it has been |
| 8954 | created by the lexer from CPP_EMBED) or |
| 8955 | after digestion/conversion some integral |
| 8956 | type with CHAR_BIT precision. For int with |
| 8957 | precision higher than CHAR_BIT or unsigned char |
| 8958 | diagnose narrowing conversions from |
| 8959 | that int/unsigned char to signed char if any |
| 8960 | byte has most significant bit set. */ |
| 8961 | && (TYPE_UNSIGNED (TREE_TYPE (val)) |
| 8962 | || (TYPE_PRECISION (TREE_TYPE (val)) |
| 8963 | > CHAR_BIT))) |
| 8964 | for (int i = 0; i < RAW_DATA_LENGTH (val); ++i) |
| 8965 | { |
| 8966 | if (RAW_DATA_SCHAR_ELT (val, i) >= 0) |
| 8967 | continue; |
| 8968 | else if (complain & tf_error) |
| 8969 | { |
| 8970 | location_t loc |
| 8971 | = cp_expr_loc_or_input_loc (t: val); |
| 8972 | int savederrorcount = errorcount; |
| 8973 | permerror_opt (loc, OPT_Wnarrowing, |
| 8974 | "narrowing conversion of " |
| 8975 | "%qd from %qH to %qI" , |
| 8976 | RAW_DATA_UCHAR_ELT (val, i), |
| 8977 | TREE_TYPE (val), et); |
| 8978 | if (errorcount != savederrorcount) |
| 8979 | return error_mark_node; |
| 8980 | } |
| 8981 | else |
| 8982 | return error_mark_node; |
| 8983 | } |
| 8984 | tree sub = copy_node (val); |
| 8985 | TREE_TYPE (sub) = et; |
| 8986 | CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor), |
| 8987 | NULL_TREE, sub); |
| 8988 | } |
| 8989 | else |
| 8990 | { |
| 8991 | conversion *conv = convs->u.list[ix]; |
| 8992 | gcc_assert (conv->kind == ck_list); |
| 8993 | for (int i = 0; i < RAW_DATA_LENGTH (val); ++i) |
| 8994 | { |
| 8995 | tree elt |
| 8996 | = build_int_cst (TREE_TYPE (val), |
| 8997 | RAW_DATA_UCHAR_ELT (val, i)); |
| 8998 | tree sub |
| 8999 | = convert_like (conv->u.list[i], elt, |
| 9000 | fn, argnum, false, false, |
| 9001 | /*nested_p=*/true, complain); |
| 9002 | if (sub == error_mark_node) |
| 9003 | return sub; |
| 9004 | if (!check_narrowing (TREE_TYPE (sub), elt, |
| 9005 | complain)) |
| 9006 | return error_mark_node; |
| 9007 | tree nc = new_ctor; |
| 9008 | CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (nc), |
| 9009 | NULL_TREE, sub); |
| 9010 | if (!TREE_CONSTANT (sub)) |
| 9011 | TREE_CONSTANT (new_ctor) = false; |
| 9012 | } |
| 9013 | } |
| 9014 | len += RAW_DATA_LENGTH (val) - 1; |
| 9015 | continue; |
| 9016 | } |
| 9017 | tree sub = convert_like (convs->u.list[ix], val, fn, |
| 9018 | argnum, false, false, |
| 9019 | /*nested_p=*/true, complain); |
| 9020 | if (sub == error_mark_node) |
| 9021 | return sub; |
| 9022 | if (!BRACE_ENCLOSED_INITIALIZER_P (val) |
| 9023 | && !check_narrowing (TREE_TYPE (sub), val, complain)) |
| 9024 | return error_mark_node; |
| 9025 | CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor), |
| 9026 | NULL_TREE, sub); |
| 9027 | if (!TREE_CONSTANT (sub)) |
| 9028 | TREE_CONSTANT (new_ctor) = false; |
| 9029 | } |
| 9030 | /* Build up the array. */ |
| 9031 | elttype |
| 9032 | = cp_build_qualified_type (elttype, (cp_type_quals (elttype) |
| 9033 | | TYPE_QUAL_CONST)); |
| 9034 | array = build_array_of_n_type (elttype, len); |
| 9035 | array = finish_compound_literal (array, new_ctor, complain); |
| 9036 | /* This is dubious now, should be blessed by P2752. */ |
| 9037 | DECL_MERGEABLE (TARGET_EXPR_SLOT (array)) = true; |
| 9038 | array = cp_build_addr_expr (array, complain); |
| 9039 | } |
| 9040 | else |
| 9041 | array = nullptr_node; |
| 9042 | |
| 9043 | array = cp_convert (build_pointer_type (elttype), array, complain); |
| 9044 | if (array == error_mark_node) |
| 9045 | return error_mark_node; |
| 9046 | |
| 9047 | /* Build up the initializer_list object. Note: fail gracefully |
| 9048 | if the object cannot be completed because, for example, no |
| 9049 | definition is provided (c++/80956). */ |
| 9050 | totype = complete_type_or_maybe_complain (totype, NULL_TREE, complain); |
| 9051 | if (!totype) |
| 9052 | return error_mark_node; |
| 9053 | tree field = next_aggregate_field (TYPE_FIELDS (totype)); |
| 9054 | vec<constructor_elt, va_gc> *vec = NULL; |
| 9055 | CONSTRUCTOR_APPEND_ELT (vec, field, array); |
| 9056 | field = next_aggregate_field (DECL_CHAIN (field)); |
| 9057 | CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len)); |
| 9058 | tree new_ctor = build_constructor (totype, vec); |
| 9059 | return get_target_expr (new_ctor, complain); |
| 9060 | } |
| 9061 | |
| 9062 | case ck_aggr: |
| 9063 | if (TREE_CODE (totype) == COMPLEX_TYPE) |
| 9064 | { |
| 9065 | tree real = CONSTRUCTOR_ELT (expr, 0)->value; |
| 9066 | tree imag = CONSTRUCTOR_ELT (expr, 1)->value; |
| 9067 | real = perform_implicit_conversion (TREE_TYPE (totype), |
| 9068 | real, complain); |
| 9069 | imag = perform_implicit_conversion (TREE_TYPE (totype), |
| 9070 | imag, complain); |
| 9071 | expr = build2 (COMPLEX_EXPR, totype, real, imag); |
| 9072 | return expr; |
| 9073 | } |
| 9074 | expr = reshape_init (totype, expr, complain); |
| 9075 | expr = get_target_expr (digest_init (totype, expr, complain), |
| 9076 | complain); |
| 9077 | if (expr != error_mark_node) |
| 9078 | TARGET_EXPR_LIST_INIT_P (expr) = true; |
| 9079 | return expr; |
| 9080 | |
| 9081 | default: |
| 9082 | break; |
| 9083 | }; |
| 9084 | |
| 9085 | conversion *nc = next_conversion (conv: convs); |
| 9086 | if (convs->kind == ck_ref_bind && nc->kind == ck_qual |
| 9087 | && !convs->need_temporary_p) |
| 9088 | /* direct_reference_binding might have inserted a ck_qual under |
| 9089 | this ck_ref_bind for the benefit of conversion sequence ranking. |
| 9090 | Don't actually perform that conversion. */ |
| 9091 | nc = next_conversion (conv: nc); |
| 9092 | |
| 9093 | expr = convert_like (nc, expr, fn, argnum, |
| 9094 | convs->kind == ck_ref_bind |
| 9095 | ? issue_conversion_warnings : false, |
| 9096 | c_cast_p, /*nested_p=*/true, complain & ~tf_no_cleanup); |
| 9097 | if (expr == error_mark_node) |
| 9098 | return error_mark_node; |
| 9099 | |
| 9100 | switch (convs->kind) |
| 9101 | { |
| 9102 | case ck_rvalue: |
| 9103 | expr = decay_conversion (expr, complain); |
| 9104 | if (expr == error_mark_node) |
| 9105 | { |
| 9106 | if (complain & tf_error) |
| 9107 | { |
| 9108 | auto_diagnostic_group d; |
| 9109 | maybe_print_user_conv_context (convs); |
| 9110 | maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum); |
| 9111 | } |
| 9112 | return error_mark_node; |
| 9113 | } |
| 9114 | |
| 9115 | if ((complain & tf_warning) && fn |
| 9116 | && warn_suggest_attribute_format) |
| 9117 | { |
| 9118 | tree rhstype = TREE_TYPE (expr); |
| 9119 | const enum tree_code coder = TREE_CODE (rhstype); |
| 9120 | const enum tree_code codel = TREE_CODE (totype); |
| 9121 | if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE) |
| 9122 | && coder == codel |
| 9123 | && check_missing_format_attribute (totype, rhstype)) |
| 9124 | warning (OPT_Wsuggest_attribute_format, |
| 9125 | "argument of function call might be a candidate " |
| 9126 | "for a format attribute" ); |
| 9127 | } |
| 9128 | |
| 9129 | if (! MAYBE_CLASS_TYPE_P (totype)) |
| 9130 | return maybe_adjust_type_name (type: totype, expr, kind: convs->kind); |
| 9131 | |
| 9132 | /* Don't introduce copies when passing arguments along to the inherited |
| 9133 | constructor. */ |
| 9134 | if (current_function_decl |
| 9135 | && flag_new_inheriting_ctors |
| 9136 | && DECL_INHERITED_CTOR (current_function_decl)) |
| 9137 | return expr; |
| 9138 | |
| 9139 | if (TREE_CODE (expr) == TARGET_EXPR |
| 9140 | && TARGET_EXPR_LIST_INIT_P (expr)) |
| 9141 | /* Copy-list-initialization doesn't actually involve a copy. */ |
| 9142 | return expr; |
| 9143 | |
| 9144 | /* Fall through. */ |
| 9145 | case ck_base: |
| 9146 | if (convs->kind == ck_base && !convs->need_temporary_p) |
| 9147 | { |
| 9148 | /* We are going to bind a reference directly to a base-class |
| 9149 | subobject of EXPR. */ |
| 9150 | /* Build an expression for `*((base*) &expr)'. */ |
| 9151 | expr = convert_to_base (expr, totype, |
| 9152 | !c_cast_p, /*nonnull=*/true, complain); |
| 9153 | return expr; |
| 9154 | } |
| 9155 | |
| 9156 | /* Copy-initialization where the cv-unqualified version of the source |
| 9157 | type is the same class as, or a derived class of, the class of the |
| 9158 | destination [is treated as direct-initialization]. [dcl.init] */ |
| 9159 | flags = LOOKUP_NORMAL; |
| 9160 | /* This conversion is being done in the context of a user-defined |
| 9161 | conversion (i.e. the second step of copy-initialization), so |
| 9162 | don't allow any more. */ |
| 9163 | if (convs->user_conv_p) |
| 9164 | flags |= LOOKUP_NO_CONVERSION; |
| 9165 | /* We might be performing a conversion of the argument |
| 9166 | to the user-defined conversion, i.e., not a conversion of the |
| 9167 | result of the user-defined conversion. In which case we skip |
| 9168 | explicit constructors. */ |
| 9169 | if (convs->copy_init_p) |
| 9170 | flags |= LOOKUP_ONLYCONVERTING; |
| 9171 | expr = build_temp (expr, type: totype, flags, diagnostic_kind: &diag_kind, complain); |
| 9172 | if (diag_kind && complain) |
| 9173 | { |
| 9174 | auto_diagnostic_group d; |
| 9175 | maybe_print_user_conv_context (convs); |
| 9176 | maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum); |
| 9177 | } |
| 9178 | |
| 9179 | return build_cplus_new (totype, expr, complain); |
| 9180 | |
| 9181 | case ck_ref_bind: |
| 9182 | { |
| 9183 | tree ref_type = totype; |
| 9184 | |
| 9185 | if (convs->bad_p && !next_conversion (conv: convs)->bad_p) |
| 9186 | { |
| 9187 | tree extype = TREE_TYPE (expr); |
| 9188 | auto_diagnostic_group d; |
| 9189 | if (TYPE_REF_IS_RVALUE (ref_type) |
| 9190 | && lvalue_p (expr)) |
| 9191 | error_at (loc, "cannot bind rvalue reference of type %qH to " |
| 9192 | "lvalue of type %qI" , totype, extype); |
| 9193 | else if (!TYPE_REF_IS_RVALUE (ref_type) && !lvalue_p (expr) |
| 9194 | && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type))) |
| 9195 | { |
| 9196 | conversion *next = next_conversion (conv: convs); |
| 9197 | if (next->kind == ck_std) |
| 9198 | { |
| 9199 | next = next_conversion (conv: next); |
| 9200 | error_at (loc, "cannot bind non-const lvalue reference of " |
| 9201 | "type %qH to a value of type %qI" , |
| 9202 | totype, next->type); |
| 9203 | } |
| 9204 | else if (!CP_TYPE_CONST_P (TREE_TYPE (ref_type))) |
| 9205 | error_at (loc, "cannot bind non-const lvalue reference of " |
| 9206 | "type %qH to an rvalue of type %qI" , totype, extype); |
| 9207 | else // extype is volatile |
| 9208 | error_at (loc, "cannot bind lvalue reference of type " |
| 9209 | "%qH to an rvalue of type %qI" , totype, |
| 9210 | extype); |
| 9211 | } |
| 9212 | else if (!reference_compatible_p (TREE_TYPE (totype), t2: extype)) |
| 9213 | { |
| 9214 | /* If we're converting from T[] to T[N], don't talk |
| 9215 | about discarding qualifiers. (Converting from T[N] to |
| 9216 | T[] is allowed by P0388R4.) */ |
| 9217 | if (TREE_CODE (extype) == ARRAY_TYPE |
| 9218 | && TYPE_DOMAIN (extype) == NULL_TREE |
| 9219 | && TREE_CODE (TREE_TYPE (totype)) == ARRAY_TYPE |
| 9220 | && TYPE_DOMAIN (TREE_TYPE (totype)) != NULL_TREE) |
| 9221 | error_at (loc, "cannot bind reference of type %qH to %qI " |
| 9222 | "due to different array bounds" , totype, extype); |
| 9223 | else |
| 9224 | error_at (loc, "binding reference of type %qH to %qI " |
| 9225 | "discards qualifiers" , totype, extype); |
| 9226 | } |
| 9227 | else |
| 9228 | gcc_unreachable (); |
| 9229 | maybe_print_user_conv_context (convs); |
| 9230 | maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum); |
| 9231 | |
| 9232 | return error_mark_node; |
| 9233 | } |
| 9234 | else if (complain & tf_warning) |
| 9235 | maybe_warn_array_conv (loc, c: convs, expr); |
| 9236 | |
| 9237 | /* If necessary, create a temporary. |
| 9238 | |
| 9239 | VA_ARG_EXPR and CONSTRUCTOR expressions are special cases |
| 9240 | that need temporaries, even when their types are reference |
| 9241 | compatible with the type of reference being bound, so the |
| 9242 | upcoming call to cp_build_addr_expr doesn't fail. */ |
| 9243 | if (convs->need_temporary_p |
| 9244 | || TREE_CODE (expr) == CONSTRUCTOR |
| 9245 | || TREE_CODE (expr) == VA_ARG_EXPR) |
| 9246 | { |
| 9247 | /* Otherwise, a temporary of type "cv1 T1" is created and |
| 9248 | initialized from the initializer expression using the rules |
| 9249 | for a non-reference copy-initialization (8.5). */ |
| 9250 | |
| 9251 | tree type = TREE_TYPE (ref_type); |
| 9252 | cp_lvalue_kind lvalue = lvalue_kind (expr); |
| 9253 | |
| 9254 | gcc_assert (similar_type_p (type, next_conversion (convs)->type)); |
| 9255 | if (!CP_TYPE_CONST_NON_VOLATILE_P (type) |
| 9256 | && !TYPE_REF_IS_RVALUE (ref_type)) |
| 9257 | { |
| 9258 | /* If the reference is volatile or non-const, we |
| 9259 | cannot create a temporary. */ |
| 9260 | if (complain & tf_error) |
| 9261 | { |
| 9262 | if (lvalue & clk_bitfield) |
| 9263 | error_at (loc, "cannot bind bit-field %qE to %qT" , |
| 9264 | expr, ref_type); |
| 9265 | else if (lvalue & clk_packed) |
| 9266 | error_at (loc, "cannot bind packed field %qE to %qT" , |
| 9267 | expr, ref_type); |
| 9268 | else |
| 9269 | error_at (loc, "cannot bind rvalue %qE to %qT" , |
| 9270 | expr, ref_type); |
| 9271 | } |
| 9272 | return error_mark_node; |
| 9273 | } |
| 9274 | /* If the source is a packed field, and we must use a copy |
| 9275 | constructor, then building the target expr will require |
| 9276 | binding the field to the reference parameter to the |
| 9277 | copy constructor, and we'll end up with an infinite |
| 9278 | loop. If we can use a bitwise copy, then we'll be |
| 9279 | OK. */ |
| 9280 | if ((lvalue & clk_packed) |
| 9281 | && CLASS_TYPE_P (type) |
| 9282 | && type_has_nontrivial_copy_init (type)) |
| 9283 | { |
| 9284 | error_at (loc, "cannot bind packed field %qE to %qT" , |
| 9285 | expr, ref_type); |
| 9286 | return error_mark_node; |
| 9287 | } |
| 9288 | if (lvalue & clk_bitfield) |
| 9289 | { |
| 9290 | expr = convert_bitfield_to_declared_type (expr); |
| 9291 | expr = fold_convert (type, expr); |
| 9292 | } |
| 9293 | |
| 9294 | /* Creating &TARGET_EXPR<> in a template would break when |
| 9295 | tsubsting the expression, so use an IMPLICIT_CONV_EXPR |
| 9296 | instead. This can happen even when there's no class |
| 9297 | involved, e.g., when converting an integer to a reference |
| 9298 | type. */ |
| 9299 | if (processing_template_decl) |
| 9300 | return build1 (IMPLICIT_CONV_EXPR, totype, expr); |
| 9301 | expr = build_target_expr_with_type (expr, type, complain); |
| 9302 | } |
| 9303 | |
| 9304 | /* Take the address of the thing to which we will bind the |
| 9305 | reference. */ |
| 9306 | expr = cp_build_addr_expr (expr, complain); |
| 9307 | if (expr == error_mark_node) |
| 9308 | return error_mark_node; |
| 9309 | |
| 9310 | /* Convert it to a pointer to the type referred to by the |
| 9311 | reference. This will adjust the pointer if a derived to |
| 9312 | base conversion is being performed. */ |
| 9313 | expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)), |
| 9314 | expr, complain); |
| 9315 | /* Convert the pointer to the desired reference type. */ |
| 9316 | return build_nop (ref_type, expr); |
| 9317 | } |
| 9318 | |
| 9319 | case ck_lvalue: |
| 9320 | return decay_conversion (expr, complain); |
| 9321 | |
| 9322 | case ck_fnptr: |
| 9323 | /* ??? Should the address of a transaction-safe pointer point to the TM |
| 9324 | clone, and this conversion look up the primary function? */ |
| 9325 | return build_nop (totype, expr); |
| 9326 | |
| 9327 | case ck_qual: |
| 9328 | /* Warn about deprecated conversion if appropriate. */ |
| 9329 | if (complain & tf_warning) |
| 9330 | { |
| 9331 | string_conv_p (totype, expr, 1); |
| 9332 | maybe_warn_array_conv (loc, c: convs, expr); |
| 9333 | } |
| 9334 | break; |
| 9335 | |
| 9336 | case ck_ptr: |
| 9337 | if (convs->base_p) |
| 9338 | expr = convert_to_base (expr, totype, !c_cast_p, |
| 9339 | /*nonnull=*/false, complain); |
| 9340 | return build_nop (totype, expr); |
| 9341 | |
| 9342 | case ck_pmem: |
| 9343 | return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false, |
| 9344 | c_cast_p, complain); |
| 9345 | |
| 9346 | default: |
| 9347 | break; |
| 9348 | } |
| 9349 | |
| 9350 | if (convs->check_narrowing |
| 9351 | && !check_narrowing (totype, expr, complain, |
| 9352 | convs->check_narrowing_const_only)) |
| 9353 | return error_mark_node; |
| 9354 | |
| 9355 | warning_sentinel w (warn_zero_as_null_pointer_constant); |
| 9356 | if (issue_conversion_warnings) |
| 9357 | expr = cp_convert_and_check (totype, expr, complain); |
| 9358 | else |
| 9359 | { |
| 9360 | if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR) |
| 9361 | expr = TREE_OPERAND (expr, 0); |
| 9362 | expr = cp_convert (totype, expr, complain); |
| 9363 | } |
| 9364 | |
| 9365 | return expr; |
| 9366 | } |
| 9367 | |
| 9368 | /* Return true if converting FROM to TO is unsafe in a template. */ |
| 9369 | |
| 9370 | static bool |
| 9371 | conv_unsafe_in_template_p (tree to, tree from) |
| 9372 | { |
| 9373 | /* Converting classes involves TARGET_EXPR. */ |
| 9374 | if (CLASS_TYPE_P (to) || CLASS_TYPE_P (from)) |
| 9375 | return true; |
| 9376 | |
| 9377 | /* Converting real to integer produces FIX_TRUNC_EXPR which tsubst |
| 9378 | doesn't handle. */ |
| 9379 | if (SCALAR_FLOAT_TYPE_P (from) && INTEGRAL_OR_ENUMERATION_TYPE_P (to)) |
| 9380 | return true; |
| 9381 | |
| 9382 | /* Converting integer to real isn't a trivial conversion, either. */ |
| 9383 | if (INTEGRAL_OR_ENUMERATION_TYPE_P (from) && SCALAR_FLOAT_TYPE_P (to)) |
| 9384 | return true; |
| 9385 | |
| 9386 | return false; |
| 9387 | } |
| 9388 | |
| 9389 | /* Wrapper for convert_like_internal that handles creating |
| 9390 | IMPLICIT_CONV_EXPR. */ |
| 9391 | |
| 9392 | static tree |
| 9393 | convert_like (conversion *convs, tree expr, tree fn, int argnum, |
| 9394 | bool issue_conversion_warnings, bool c_cast_p, bool nested_p, |
| 9395 | tsubst_flags_t complain) |
| 9396 | { |
| 9397 | /* Creating &TARGET_EXPR<> in a template breaks when substituting, |
| 9398 | and creating a CALL_EXPR in a template breaks in finish_call_expr |
| 9399 | so use an IMPLICIT_CONV_EXPR for this conversion. We would have |
| 9400 | created such codes e.g. when calling a user-defined conversion |
| 9401 | function. */ |
| 9402 | tree conv_expr = NULL_TREE; |
| 9403 | if (processing_template_decl |
| 9404 | && convs->kind != ck_identity |
| 9405 | && conv_unsafe_in_template_p (to: convs->type, TREE_TYPE (expr))) |
| 9406 | { |
| 9407 | conv_expr = build1 (IMPLICIT_CONV_EXPR, convs->type, expr); |
| 9408 | if (convs->kind != ck_ref_bind) |
| 9409 | conv_expr = convert_from_reference (conv_expr); |
| 9410 | if (!convs->bad_p) |
| 9411 | return conv_expr; |
| 9412 | /* Do the normal processing to give the bad_p errors. But we still |
| 9413 | need to return the IMPLICIT_CONV_EXPR, unless we're returning |
| 9414 | error_mark_node. */ |
| 9415 | } |
| 9416 | expr = convert_like_internal (convs, expr, fn, argnum, |
| 9417 | issue_conversion_warnings, c_cast_p, |
| 9418 | nested_p, complain); |
| 9419 | if (expr == error_mark_node) |
| 9420 | return error_mark_node; |
| 9421 | return conv_expr ? conv_expr : expr; |
| 9422 | } |
| 9423 | |
| 9424 | /* Convenience wrapper for convert_like. */ |
| 9425 | |
| 9426 | static inline tree |
| 9427 | convert_like (conversion *convs, tree expr, tsubst_flags_t complain) |
| 9428 | { |
| 9429 | return convert_like (convs, expr, NULL_TREE, argnum: 0, |
| 9430 | /*issue_conversion_warnings=*/true, |
| 9431 | /*c_cast_p=*/false, /*nested_p=*/false, complain); |
| 9432 | } |
| 9433 | |
| 9434 | /* Convenience wrapper for convert_like. */ |
| 9435 | |
| 9436 | static inline tree |
| 9437 | convert_like_with_context (conversion *convs, tree expr, tree fn, int argnum, |
| 9438 | tsubst_flags_t complain) |
| 9439 | { |
| 9440 | return convert_like (convs, expr, fn, argnum, |
| 9441 | /*issue_conversion_warnings=*/true, |
| 9442 | /*c_cast_p=*/false, /*nested_p=*/false, complain); |
| 9443 | } |
| 9444 | |
| 9445 | /* ARG is being passed to a varargs function. Perform any conversions |
| 9446 | required. Return the converted value. */ |
| 9447 | |
| 9448 | tree |
| 9449 | convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain) |
| 9450 | { |
| 9451 | tree arg_type = TREE_TYPE (arg); |
| 9452 | location_t loc = cp_expr_loc_or_input_loc (t: arg); |
| 9453 | |
| 9454 | /* [expr.call] |
| 9455 | |
| 9456 | If the argument has integral or enumeration type that is subject |
| 9457 | to the integral promotions (_conv.prom_), or a floating-point |
| 9458 | type that is subject to the floating-point promotion |
| 9459 | (_conv.fpprom_), the value of the argument is converted to the |
| 9460 | promoted type before the call. */ |
| 9461 | if (SCALAR_FLOAT_TYPE_P (arg_type) |
| 9462 | && (TYPE_PRECISION (arg_type) |
| 9463 | < TYPE_PRECISION (double_type_node)) |
| 9464 | && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type)) |
| 9465 | && !extended_float_type_p (type: arg_type)) |
| 9466 | { |
| 9467 | if ((complain & tf_warning) |
| 9468 | && warn_double_promotion && !c_inhibit_evaluation_warnings) |
| 9469 | warning_at (loc, OPT_Wdouble_promotion, |
| 9470 | "implicit conversion from %qH to %qI when passing " |
| 9471 | "argument to function" , |
| 9472 | arg_type, double_type_node); |
| 9473 | if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR) |
| 9474 | arg = TREE_OPERAND (arg, 0); |
| 9475 | arg = mark_rvalue_use (arg); |
| 9476 | arg = convert_to_real_nofold (double_type_node, x: arg); |
| 9477 | } |
| 9478 | else if (NULLPTR_TYPE_P (arg_type)) |
| 9479 | { |
| 9480 | arg = mark_rvalue_use (arg); |
| 9481 | if (TREE_SIDE_EFFECTS (arg)) |
| 9482 | { |
| 9483 | warning_sentinel w(warn_unused_result); |
| 9484 | arg = cp_build_compound_expr (arg, null_pointer_node, complain); |
| 9485 | } |
| 9486 | else |
| 9487 | arg = null_pointer_node; |
| 9488 | } |
| 9489 | else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type)) |
| 9490 | { |
| 9491 | if (SCOPED_ENUM_P (arg_type)) |
| 9492 | { |
| 9493 | tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg, |
| 9494 | complain); |
| 9495 | prom = cp_perform_integral_promotions (prom, complain); |
| 9496 | if (abi_version_crosses (6) |
| 9497 | && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type) |
| 9498 | && (complain & tf_warning)) |
| 9499 | warning_at (loc, OPT_Wabi, "scoped enum %qT passed through %<...%>" |
| 9500 | " as %qT before %<-fabi-version=6%>, %qT after" , |
| 9501 | arg_type, |
| 9502 | TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type)); |
| 9503 | if (!abi_version_at_least (6)) |
| 9504 | arg = prom; |
| 9505 | } |
| 9506 | else |
| 9507 | arg = cp_perform_integral_promotions (arg, complain); |
| 9508 | } |
| 9509 | else |
| 9510 | /* [expr.call] |
| 9511 | |
| 9512 | The lvalue-to-rvalue, array-to-pointer, and function-to-pointer |
| 9513 | standard conversions are performed. */ |
| 9514 | arg = decay_conversion (arg, complain); |
| 9515 | |
| 9516 | arg = require_complete_type (arg, complain); |
| 9517 | arg_type = TREE_TYPE (arg); |
| 9518 | |
| 9519 | if (arg != error_mark_node |
| 9520 | /* In a template (or ill-formed code), we can have an incomplete type |
| 9521 | even after require_complete_type, in which case we don't know |
| 9522 | whether it has trivial copy or not. */ |
| 9523 | && COMPLETE_TYPE_P (arg_type) |
| 9524 | && !cp_unevaluated_operand) |
| 9525 | { |
| 9526 | /* [expr.call] 5.2.2/7: |
| 9527 | Passing a potentially-evaluated argument of class type (Clause 9) |
| 9528 | with a non-trivial copy constructor or a non-trivial destructor |
| 9529 | with no corresponding parameter is conditionally-supported, with |
| 9530 | implementation-defined semantics. |
| 9531 | |
| 9532 | We support it as pass-by-invisible-reference, just like a normal |
| 9533 | value parameter. |
| 9534 | |
| 9535 | If the call appears in the context of a sizeof expression, |
| 9536 | it is not potentially-evaluated. */ |
| 9537 | if (type_has_nontrivial_copy_init (arg_type) |
| 9538 | || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type)) |
| 9539 | { |
| 9540 | arg = force_rvalue (arg, complain); |
| 9541 | if (complain & tf_warning) |
| 9542 | warning (OPT_Wconditionally_supported, |
| 9543 | "passing objects of non-trivially-copyable " |
| 9544 | "type %q#T through %<...%> is conditionally supported" , |
| 9545 | arg_type); |
| 9546 | return build1 (ADDR_EXPR, build_reference_type (arg_type), arg); |
| 9547 | } |
| 9548 | /* Build up a real lvalue-to-rvalue conversion in case the |
| 9549 | copy constructor is trivial but not callable. */ |
| 9550 | else if (CLASS_TYPE_P (arg_type)) |
| 9551 | force_rvalue (arg, complain); |
| 9552 | |
| 9553 | } |
| 9554 | |
| 9555 | return arg; |
| 9556 | } |
| 9557 | |
| 9558 | /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */ |
| 9559 | |
| 9560 | tree |
| 9561 | build_x_va_arg (location_t loc, tree expr, tree type) |
| 9562 | { |
| 9563 | if (processing_template_decl) |
| 9564 | { |
| 9565 | tree r = build_min (VA_ARG_EXPR, type, expr); |
| 9566 | SET_EXPR_LOCATION (r, loc); |
| 9567 | return r; |
| 9568 | } |
| 9569 | |
| 9570 | type = complete_type_or_else (type, NULL_TREE); |
| 9571 | |
| 9572 | if (expr == error_mark_node || !type) |
| 9573 | return error_mark_node; |
| 9574 | |
| 9575 | expr = mark_lvalue_use (expr); |
| 9576 | |
| 9577 | if (TYPE_REF_P (type)) |
| 9578 | { |
| 9579 | error ("cannot receive reference type %qT through %<...%>" , type); |
| 9580 | return error_mark_node; |
| 9581 | } |
| 9582 | |
| 9583 | if (type_has_nontrivial_copy_init (type) |
| 9584 | || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
| 9585 | { |
| 9586 | /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat |
| 9587 | it as pass by invisible reference. */ |
| 9588 | warning_at (loc, OPT_Wconditionally_supported, |
| 9589 | "receiving objects of non-trivially-copyable type %q#T " |
| 9590 | "through %<...%> is conditionally-supported" , type); |
| 9591 | |
| 9592 | tree ref = cp_build_reference_type (type, false); |
| 9593 | expr = build_va_arg (loc, expr, ref); |
| 9594 | return convert_from_reference (expr); |
| 9595 | } |
| 9596 | |
| 9597 | tree ret = build_va_arg (loc, expr, type); |
| 9598 | if (CLASS_TYPE_P (type)) |
| 9599 | /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to |
| 9600 | know how to handle it. */ |
| 9601 | ret = get_target_expr (ret); |
| 9602 | return ret; |
| 9603 | } |
| 9604 | |
| 9605 | /* TYPE has been given to va_arg. Apply the default conversions which |
| 9606 | would have happened when passed via ellipsis. Return the promoted |
| 9607 | type, or the passed type if there is no change. */ |
| 9608 | |
| 9609 | tree |
| 9610 | cxx_type_promotes_to (tree type) |
| 9611 | { |
| 9612 | tree promote; |
| 9613 | |
| 9614 | /* Perform the array-to-pointer and function-to-pointer |
| 9615 | conversions. */ |
| 9616 | type = type_decays_to (type); |
| 9617 | |
| 9618 | promote = type_promotes_to (type); |
| 9619 | if (same_type_p (type, promote)) |
| 9620 | promote = type; |
| 9621 | |
| 9622 | return promote; |
| 9623 | } |
| 9624 | |
| 9625 | /* ARG is a default argument expression being passed to a parameter of |
| 9626 | the indicated TYPE, which is a parameter to FN. PARMNUM is the |
| 9627 | zero-based argument number. Do any required conversions. Return |
| 9628 | the converted value. */ |
| 9629 | |
| 9630 | static GTY(()) vec<tree, va_gc> *default_arg_context; |
| 9631 | void |
| 9632 | push_defarg_context (tree fn) |
| 9633 | { vec_safe_push (v&: default_arg_context, obj: fn); } |
| 9634 | |
| 9635 | void |
| 9636 | pop_defarg_context (void) |
| 9637 | { default_arg_context->pop (); } |
| 9638 | |
| 9639 | tree |
| 9640 | convert_default_arg (tree type, tree arg, tree fn, int parmnum, |
| 9641 | tsubst_flags_t complain) |
| 9642 | { |
| 9643 | int i; |
| 9644 | tree t; |
| 9645 | |
| 9646 | /* See through clones. */ |
| 9647 | fn = DECL_ORIGIN (fn); |
| 9648 | /* And inheriting ctors. */ |
| 9649 | if (flag_new_inheriting_ctors) |
| 9650 | fn = strip_inheriting_ctors (fn); |
| 9651 | |
| 9652 | /* Detect recursion. */ |
| 9653 | FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t) |
| 9654 | if (t == fn) |
| 9655 | { |
| 9656 | if (complain & tf_error) |
| 9657 | error ("recursive evaluation of default argument for %q#D" , fn); |
| 9658 | return error_mark_node; |
| 9659 | } |
| 9660 | |
| 9661 | /* If the ARG is an unparsed default argument expression, the |
| 9662 | conversion cannot be performed. */ |
| 9663 | if (TREE_CODE (arg) == DEFERRED_PARSE) |
| 9664 | { |
| 9665 | if (complain & tf_error) |
| 9666 | error ("call to %qD uses the default argument for parameter %P, which " |
| 9667 | "is not yet defined" , fn, parmnum); |
| 9668 | return error_mark_node; |
| 9669 | } |
| 9670 | |
| 9671 | push_defarg_context (fn); |
| 9672 | |
| 9673 | if (fn && DECL_TEMPLATE_INFO (fn)) |
| 9674 | arg = tsubst_default_argument (fn, parmnum, type, arg, complain); |
| 9675 | |
| 9676 | /* Due to: |
| 9677 | |
| 9678 | [dcl.fct.default] |
| 9679 | |
| 9680 | The names in the expression are bound, and the semantic |
| 9681 | constraints are checked, at the point where the default |
| 9682 | expressions appears. |
| 9683 | |
| 9684 | we must not perform access checks here. */ |
| 9685 | push_deferring_access_checks (dk_no_check); |
| 9686 | /* We must make a copy of ARG, in case subsequent processing |
| 9687 | alters any part of it. */ |
| 9688 | arg = break_out_target_exprs (arg, /*clear location*/true); |
| 9689 | |
| 9690 | arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT, |
| 9691 | ICR_DEFAULT_ARGUMENT, fn, parmnum, |
| 9692 | complain); |
| 9693 | arg = convert_for_arg_passing (type, arg, complain); |
| 9694 | pop_deferring_access_checks(); |
| 9695 | |
| 9696 | pop_defarg_context (); |
| 9697 | |
| 9698 | return arg; |
| 9699 | } |
| 9700 | |
| 9701 | /* Returns the type which will really be used for passing an argument of |
| 9702 | type TYPE. */ |
| 9703 | |
| 9704 | tree |
| 9705 | type_passed_as (tree type) |
| 9706 | { |
| 9707 | /* Pass classes with copy ctors by invisible reference. */ |
| 9708 | if (TREE_ADDRESSABLE (type)) |
| 9709 | type = build_reference_type (type); |
| 9710 | |
| 9711 | return type; |
| 9712 | } |
| 9713 | |
| 9714 | /* Actually perform the appropriate conversion. */ |
| 9715 | |
| 9716 | tree |
| 9717 | convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain) |
| 9718 | { |
| 9719 | tree bitfield_type; |
| 9720 | |
| 9721 | /* If VAL is a bitfield, then -- since it has already been converted |
| 9722 | to TYPE -- it cannot have a precision greater than TYPE. |
| 9723 | |
| 9724 | If it has a smaller precision, we must widen it here. For |
| 9725 | example, passing "int f:3;" to a function expecting an "int" will |
| 9726 | not result in any conversion before this point. |
| 9727 | |
| 9728 | If the precision is the same we must not risk widening. For |
| 9729 | example, the COMPONENT_REF for a 32-bit "long long" bitfield will |
| 9730 | often have type "int", even though the C++ type for the field is |
| 9731 | "long long". If the value is being passed to a function |
| 9732 | expecting an "int", then no conversions will be required. But, |
| 9733 | if we call convert_bitfield_to_declared_type, the bitfield will |
| 9734 | be converted to "long long". */ |
| 9735 | bitfield_type = is_bitfield_expr_with_lowered_type (val); |
| 9736 | if (bitfield_type |
| 9737 | && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)) |
| 9738 | val = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type), x: val); |
| 9739 | |
| 9740 | if (val == error_mark_node) |
| 9741 | ; |
| 9742 | /* Pass classes with copy ctors by invisible reference. */ |
| 9743 | else if (TREE_ADDRESSABLE (type)) |
| 9744 | val = build1 (ADDR_EXPR, build_reference_type (type), val); |
| 9745 | if (complain & tf_warning) |
| 9746 | maybe_warn_parm_abi (type, cp_expr_loc_or_input_loc (t: val)); |
| 9747 | |
| 9748 | if (complain & tf_warning) |
| 9749 | warn_for_address_of_packed_member (type, val); |
| 9750 | |
| 9751 | /* gimplify_arg elides TARGET_EXPRs that initialize a function argument, |
| 9752 | unless the initializer is a CONSTRUCTOR. In that case, we fail to |
| 9753 | elide the copy anyway. See that function for more information. */ |
| 9754 | if (SIMPLE_TARGET_EXPR_P (val) |
| 9755 | && TREE_CODE (TARGET_EXPR_INITIAL (val)) != CONSTRUCTOR) |
| 9756 | set_target_expr_eliding (val); |
| 9757 | |
| 9758 | return val; |
| 9759 | } |
| 9760 | |
| 9761 | /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for |
| 9762 | which just decay_conversion or no conversions at all should be done. |
| 9763 | This is true for some builtins which don't act like normal functions. |
| 9764 | Return 2 if just decay_conversion and removal of excess precision should |
| 9765 | be done, 1 if just decay_conversion. Return 3 for special treatment of |
| 9766 | the 3rd argument for __builtin_*_overflow_p. Return 4 for special |
| 9767 | treatment of the 1st argument for |
| 9768 | __builtin_{clz,ctz,clrsb,ffs,parity,popcount}g. */ |
| 9769 | |
| 9770 | int |
| 9771 | magic_varargs_p (tree fn) |
| 9772 | { |
| 9773 | if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL) |
| 9774 | switch (DECL_FUNCTION_CODE (decl: fn)) |
| 9775 | { |
| 9776 | case BUILT_IN_CLASSIFY_TYPE: |
| 9777 | case BUILT_IN_CONSTANT_P: |
| 9778 | case BUILT_IN_NEXT_ARG: |
| 9779 | case BUILT_IN_VA_START: |
| 9780 | return 1; |
| 9781 | |
| 9782 | case BUILT_IN_ADD_OVERFLOW_P: |
| 9783 | case BUILT_IN_SUB_OVERFLOW_P: |
| 9784 | case BUILT_IN_MUL_OVERFLOW_P: |
| 9785 | return 3; |
| 9786 | |
| 9787 | case BUILT_IN_ISFINITE: |
| 9788 | case BUILT_IN_ISINF: |
| 9789 | case BUILT_IN_ISINF_SIGN: |
| 9790 | case BUILT_IN_ISNAN: |
| 9791 | case BUILT_IN_ISNORMAL: |
| 9792 | case BUILT_IN_FPCLASSIFY: |
| 9793 | return 2; |
| 9794 | |
| 9795 | case BUILT_IN_CLZG: |
| 9796 | case BUILT_IN_CTZG: |
| 9797 | case BUILT_IN_CLRSBG: |
| 9798 | case BUILT_IN_FFSG: |
| 9799 | case BUILT_IN_PARITYG: |
| 9800 | case BUILT_IN_POPCOUNTG: |
| 9801 | return 4; |
| 9802 | |
| 9803 | default: |
| 9804 | return lookup_attribute (attr_name: "type generic" , |
| 9805 | TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0; |
| 9806 | } |
| 9807 | |
| 9808 | return 0; |
| 9809 | } |
| 9810 | |
| 9811 | /* Returns the decl of the dispatcher function if FN is a function version. */ |
| 9812 | |
| 9813 | tree |
| 9814 | get_function_version_dispatcher (tree fn) |
| 9815 | { |
| 9816 | tree dispatcher_decl = NULL; |
| 9817 | |
| 9818 | if (DECL_LOCAL_DECL_P (fn)) |
| 9819 | fn = DECL_LOCAL_DECL_ALIAS (fn); |
| 9820 | |
| 9821 | gcc_assert (TREE_CODE (fn) == FUNCTION_DECL |
| 9822 | && DECL_FUNCTION_VERSIONED (fn)); |
| 9823 | |
| 9824 | gcc_assert (targetm.get_function_versions_dispatcher); |
| 9825 | dispatcher_decl = targetm.get_function_versions_dispatcher (fn); |
| 9826 | |
| 9827 | if (dispatcher_decl == NULL) |
| 9828 | { |
| 9829 | error_at (input_location, "use of multiversioned function " |
| 9830 | "without a default" ); |
| 9831 | return NULL; |
| 9832 | } |
| 9833 | |
| 9834 | retrofit_lang_decl (dispatcher_decl); |
| 9835 | gcc_assert (dispatcher_decl != NULL); |
| 9836 | return dispatcher_decl; |
| 9837 | } |
| 9838 | |
| 9839 | /* fn is a function version dispatcher that is marked used. Mark all the |
| 9840 | semantically identical function versions it will dispatch as used. */ |
| 9841 | |
| 9842 | void |
| 9843 | mark_versions_used (tree fn) |
| 9844 | { |
| 9845 | struct cgraph_node *node; |
| 9846 | struct cgraph_function_version_info *node_v; |
| 9847 | struct cgraph_function_version_info *it_v; |
| 9848 | |
| 9849 | gcc_assert (TREE_CODE (fn) == FUNCTION_DECL); |
| 9850 | |
| 9851 | node = cgraph_node::get (decl: fn); |
| 9852 | if (node == NULL) |
| 9853 | return; |
| 9854 | |
| 9855 | gcc_assert (node->dispatcher_function); |
| 9856 | |
| 9857 | node_v = node->function_version (); |
| 9858 | if (node_v == NULL) |
| 9859 | return; |
| 9860 | |
| 9861 | /* All semantically identical versions are chained. Traverse and mark each |
| 9862 | one of them as used. */ |
| 9863 | it_v = node_v->next; |
| 9864 | while (it_v != NULL) |
| 9865 | { |
| 9866 | mark_used (it_v->this_node->decl); |
| 9867 | it_v = it_v->next; |
| 9868 | } |
| 9869 | } |
| 9870 | |
| 9871 | /* Build a call to "the copy constructor" for the type of A, even if it |
| 9872 | wouldn't be selected by normal overload resolution. Used for |
| 9873 | diagnostics. */ |
| 9874 | |
| 9875 | static tree |
| 9876 | call_copy_ctor (tree a, tsubst_flags_t complain) |
| 9877 | { |
| 9878 | tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (a)); |
| 9879 | tree binfo = TYPE_BINFO (ctype); |
| 9880 | tree copy = get_copy_ctor (ctype, complain); |
| 9881 | copy = build_baselink (binfo, binfo, copy, NULL_TREE); |
| 9882 | tree ob = build_dummy_object (ctype); |
| 9883 | releasing_vec args (make_tree_vector_single (a)); |
| 9884 | tree r = build_new_method_call (ob, copy, &args, NULL_TREE, |
| 9885 | LOOKUP_NORMAL, NULL, complain); |
| 9886 | return r; |
| 9887 | } |
| 9888 | |
| 9889 | /* Return the base constructor corresponding to COMPLETE_CTOR or NULL_TREE. */ |
| 9890 | |
| 9891 | static tree |
| 9892 | base_ctor_for (tree complete_ctor) |
| 9893 | { |
| 9894 | tree clone; |
| 9895 | FOR_EACH_CLONE (clone, DECL_CLONED_FUNCTION (complete_ctor)) |
| 9896 | if (DECL_BASE_CONSTRUCTOR_P (clone)) |
| 9897 | return clone; |
| 9898 | return NULL_TREE; |
| 9899 | } |
| 9900 | |
| 9901 | /* Try to make EXP suitable to be used as the initializer for a base subobject, |
| 9902 | and return whether we were successful. EXP must have already been cleared |
| 9903 | by unsafe_copy_elision_p{,_opt}. */ |
| 9904 | |
| 9905 | static bool |
| 9906 | make_base_init_ok (tree exp) |
| 9907 | { |
| 9908 | if (TREE_CODE (exp) == TARGET_EXPR) |
| 9909 | exp = TARGET_EXPR_INITIAL (exp); |
| 9910 | while (TREE_CODE (exp) == COMPOUND_EXPR) |
| 9911 | exp = TREE_OPERAND (exp, 1); |
| 9912 | if (TREE_CODE (exp) == COND_EXPR) |
| 9913 | { |
| 9914 | bool ret = make_base_init_ok (TREE_OPERAND (exp, 2)); |
| 9915 | if (tree op1 = TREE_OPERAND (exp, 1)) |
| 9916 | { |
| 9917 | bool r1 = make_base_init_ok (exp: op1); |
| 9918 | /* If unsafe_copy_elision_p was false, the arms should match. */ |
| 9919 | gcc_assert (r1 == ret); |
| 9920 | } |
| 9921 | return ret; |
| 9922 | } |
| 9923 | if (TREE_CODE (exp) != AGGR_INIT_EXPR) |
| 9924 | /* A trivial copy is OK. */ |
| 9925 | return true; |
| 9926 | if (!AGGR_INIT_VIA_CTOR_P (exp)) |
| 9927 | /* unsafe_copy_elision_p_opt must have said this is OK. */ |
| 9928 | return true; |
| 9929 | tree fn = cp_get_callee_fndecl_nofold (exp); |
| 9930 | if (DECL_BASE_CONSTRUCTOR_P (fn)) |
| 9931 | return true; |
| 9932 | gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (fn)); |
| 9933 | fn = base_ctor_for (complete_ctor: fn); |
| 9934 | if (!fn || DECL_HAS_VTT_PARM_P (fn)) |
| 9935 | /* The base constructor has more parameters, so we can't just change the |
| 9936 | call target. It would be possible to splice in the appropriate |
| 9937 | arguments, but probably not worth the complexity. */ |
| 9938 | return false; |
| 9939 | mark_used (fn); |
| 9940 | AGGR_INIT_EXPR_FN (exp) = build_address (fn); |
| 9941 | return true; |
| 9942 | } |
| 9943 | |
| 9944 | /* Return 2 if T refers to a base, 1 if a potentially-overlapping field, |
| 9945 | neither of which can be used for return by invisible reference. We avoid |
| 9946 | doing C++17 mandatory copy elision for either of these cases. |
| 9947 | |
| 9948 | This returns non-zero even if the type of T has no tail padding that other |
| 9949 | data could be allocated into, because that depends on the particular ABI. |
| 9950 | unsafe_copy_elision_p_opt does consider whether there is padding. */ |
| 9951 | |
| 9952 | int |
| 9953 | unsafe_return_slot_p (tree t) |
| 9954 | { |
| 9955 | /* Check empty bases separately, they don't have fields. */ |
| 9956 | if (is_empty_base_ref (t)) |
| 9957 | return 2; |
| 9958 | |
| 9959 | /* A delegating constructor might be used to initialize a base. */ |
| 9960 | if (current_function_decl |
| 9961 | && DECL_CONSTRUCTOR_P (current_function_decl) |
| 9962 | && (t == current_class_ref |
| 9963 | || tree_strip_nop_conversions (t) == current_class_ptr)) |
| 9964 | return 2; |
| 9965 | |
| 9966 | STRIP_NOPS (t); |
| 9967 | if (TREE_CODE (t) == ADDR_EXPR) |
| 9968 | t = TREE_OPERAND (t, 0); |
| 9969 | if (TREE_CODE (t) == COMPONENT_REF) |
| 9970 | t = TREE_OPERAND (t, 1); |
| 9971 | if (TREE_CODE (t) != FIELD_DECL) |
| 9972 | return false; |
| 9973 | if (!CLASS_TYPE_P (TREE_TYPE (t))) |
| 9974 | /* The middle-end will do the right thing for scalar types. */ |
| 9975 | return false; |
| 9976 | if (DECL_FIELD_IS_BASE (t)) |
| 9977 | return 2; |
| 9978 | if (lookup_attribute (attr_name: "no_unique_address" , DECL_ATTRIBUTES (t))) |
| 9979 | return 1; |
| 9980 | return 0; |
| 9981 | } |
| 9982 | |
| 9983 | /* True IFF EXP is a prvalue that represents return by invisible reference. */ |
| 9984 | |
| 9985 | static bool |
| 9986 | init_by_return_slot_p (tree exp) |
| 9987 | { |
| 9988 | /* Copy elision only happens with a TARGET_EXPR. */ |
| 9989 | if (TREE_CODE (exp) != TARGET_EXPR) |
| 9990 | return false; |
| 9991 | tree init = TARGET_EXPR_INITIAL (exp); |
| 9992 | /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */ |
| 9993 | while (TREE_CODE (init) == COMPOUND_EXPR) |
| 9994 | init = TREE_OPERAND (init, 1); |
| 9995 | if (TREE_CODE (init) == COND_EXPR) |
| 9996 | { |
| 9997 | /* We'll end up copying from each of the arms of the COND_EXPR directly |
| 9998 | into the target, so look at them. */ |
| 9999 | if (tree op = TREE_OPERAND (init, 1)) |
| 10000 | if (init_by_return_slot_p (exp: op)) |
| 10001 | return true; |
| 10002 | return init_by_return_slot_p (TREE_OPERAND (init, 2)); |
| 10003 | } |
| 10004 | return (TREE_CODE (init) == AGGR_INIT_EXPR |
| 10005 | && !AGGR_INIT_VIA_CTOR_P (init)); |
| 10006 | } |
| 10007 | |
| 10008 | /* We can't elide a copy from a function returning by value to a |
| 10009 | potentially-overlapping subobject, as the callee might clobber tail padding. |
| 10010 | Return true iff this could be that case. |
| 10011 | |
| 10012 | Places that use this function (or _opt) to decide to elide a copy should |
| 10013 | probably use make_safe_copy_elision instead. */ |
| 10014 | |
| 10015 | bool |
| 10016 | unsafe_copy_elision_p (tree target, tree exp) |
| 10017 | { |
| 10018 | return unsafe_return_slot_p (t: target) && init_by_return_slot_p (exp); |
| 10019 | } |
| 10020 | |
| 10021 | /* As above, but for optimization allow more cases that are actually safe. */ |
| 10022 | |
| 10023 | static bool |
| 10024 | unsafe_copy_elision_p_opt (tree target, tree exp) |
| 10025 | { |
| 10026 | tree type = TYPE_MAIN_VARIANT (TREE_TYPE (exp)); |
| 10027 | /* It's safe to elide the copy for a class with no tail padding. */ |
| 10028 | if (!is_empty_class (type) |
| 10029 | && tree_int_cst_equal (TYPE_SIZE (type), CLASSTYPE_SIZE (type))) |
| 10030 | return false; |
| 10031 | return unsafe_copy_elision_p (target, exp); |
| 10032 | } |
| 10033 | |
| 10034 | /* Try to make EXP suitable to be used as the initializer for TARGET, |
| 10035 | and return whether we were successful. */ |
| 10036 | |
| 10037 | bool |
| 10038 | make_safe_copy_elision (tree target, tree exp) |
| 10039 | { |
| 10040 | int uns = unsafe_return_slot_p (t: target); |
| 10041 | if (!uns) |
| 10042 | return true; |
| 10043 | if (init_by_return_slot_p (exp)) |
| 10044 | return false; |
| 10045 | if (uns == 1) |
| 10046 | return true; |
| 10047 | return make_base_init_ok (exp); |
| 10048 | } |
| 10049 | |
| 10050 | /* True IFF the result of the conversion C is a prvalue. */ |
| 10051 | |
| 10052 | static bool |
| 10053 | conv_is_prvalue (conversion *c) |
| 10054 | { |
| 10055 | if (c->kind == ck_rvalue) |
| 10056 | return true; |
| 10057 | if (c->kind == ck_base && c->need_temporary_p) |
| 10058 | return true; |
| 10059 | if (c->kind == ck_user && !TYPE_REF_P (c->type)) |
| 10060 | return true; |
| 10061 | if (c->kind == ck_identity && c->u.expr |
| 10062 | && TREE_CODE (c->u.expr) == TARGET_EXPR) |
| 10063 | return true; |
| 10064 | |
| 10065 | return false; |
| 10066 | } |
| 10067 | |
| 10068 | /* True iff C is a conversion that binds a reference to a prvalue. */ |
| 10069 | |
| 10070 | static bool |
| 10071 | conv_binds_ref_to_prvalue (conversion *c) |
| 10072 | { |
| 10073 | if (c->kind != ck_ref_bind) |
| 10074 | return false; |
| 10075 | if (c->need_temporary_p) |
| 10076 | return true; |
| 10077 | |
| 10078 | return conv_is_prvalue (c: next_conversion (conv: c)); |
| 10079 | } |
| 10080 | |
| 10081 | /* True iff EXPR represents a (subobject of a) temporary. */ |
| 10082 | |
| 10083 | static bool |
| 10084 | expr_represents_temporary_p (tree expr) |
| 10085 | { |
| 10086 | while (handled_component_p (t: expr)) |
| 10087 | expr = TREE_OPERAND (expr, 0); |
| 10088 | return TREE_CODE (expr) == TARGET_EXPR; |
| 10089 | } |
| 10090 | |
| 10091 | /* True iff C is a conversion that binds a reference to a temporary. |
| 10092 | This is a superset of conv_binds_ref_to_prvalue: here we're also |
| 10093 | interested in xvalues. */ |
| 10094 | |
| 10095 | static bool |
| 10096 | conv_binds_ref_to_temporary (conversion *c) |
| 10097 | { |
| 10098 | if (conv_binds_ref_to_prvalue (c)) |
| 10099 | return true; |
| 10100 | if (c->kind != ck_ref_bind) |
| 10101 | return false; |
| 10102 | c = next_conversion (conv: c); |
| 10103 | /* This is the case for |
| 10104 | struct Base {}; |
| 10105 | struct Derived : Base {}; |
| 10106 | const Base& b(Derived{}); |
| 10107 | where we bind 'b' to the Base subobject of a temporary object of type |
| 10108 | Derived. The subobject is an xvalue; the whole object is a prvalue. |
| 10109 | |
| 10110 | The ck_base doesn't have to be present for cases like X{}.m. */ |
| 10111 | if (c->kind == ck_base) |
| 10112 | c = next_conversion (conv: c); |
| 10113 | if (c->kind == ck_identity && c->u.expr |
| 10114 | && expr_represents_temporary_p (expr: c->u.expr)) |
| 10115 | return true; |
| 10116 | return false; |
| 10117 | } |
| 10118 | |
| 10119 | /* Return tristate::TS_TRUE if converting EXPR to a reference type TYPE binds |
| 10120 | the reference to a temporary. Return tristate::TS_FALSE if converting |
| 10121 | EXPR to a reference type TYPE doesn't bind the reference to a temporary. If |
| 10122 | the conversion is invalid or bad, return tristate::TS_UNKNOWN. DIRECT_INIT_P |
| 10123 | says whether the conversion should be done in direct- or copy-initialization |
| 10124 | context. */ |
| 10125 | |
| 10126 | tristate |
| 10127 | ref_conv_binds_to_temporary (tree type, tree expr, bool direct_init_p/*=false*/) |
| 10128 | { |
| 10129 | gcc_assert (TYPE_REF_P (type)); |
| 10130 | |
| 10131 | conversion_obstack_sentinel cos; |
| 10132 | |
| 10133 | const int flags = direct_init_p ? LOOKUP_NORMAL : LOOKUP_IMPLICIT; |
| 10134 | conversion *conv = implicit_conversion (to: type, TREE_TYPE (expr), expr, |
| 10135 | /*c_cast_p=*/false, flags, complain: tf_none); |
| 10136 | tristate ret (tristate::TS_UNKNOWN); |
| 10137 | if (conv && !conv->bad_p) |
| 10138 | ret = tristate (conv_binds_ref_to_temporary (c: conv)); |
| 10139 | |
| 10140 | return ret; |
| 10141 | } |
| 10142 | |
| 10143 | /* Call the trivial destructor for INSTANCE, which can be either an lvalue of |
| 10144 | class type or a pointer to class type. If NO_PTR_DEREF is true and |
| 10145 | INSTANCE has pointer type, clobber the pointer rather than what it points |
| 10146 | to. */ |
| 10147 | |
| 10148 | tree |
| 10149 | build_trivial_dtor_call (tree instance, bool no_ptr_deref) |
| 10150 | { |
| 10151 | gcc_assert (!is_dummy_object (instance)); |
| 10152 | |
| 10153 | if (!flag_lifetime_dse) |
| 10154 | { |
| 10155 | no_clobber: |
| 10156 | return fold_convert (void_type_node, instance); |
| 10157 | } |
| 10158 | |
| 10159 | if (INDIRECT_TYPE_P (TREE_TYPE (instance)) |
| 10160 | && (!no_ptr_deref || TYPE_REF_P (TREE_TYPE (instance)))) |
| 10161 | { |
| 10162 | if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (instance)))) |
| 10163 | goto no_clobber; |
| 10164 | instance = cp_build_fold_indirect_ref (instance); |
| 10165 | } |
| 10166 | |
| 10167 | /* A trivial destructor should still clobber the object. */ |
| 10168 | tree clobber = build_clobber (TREE_TYPE (instance), CLOBBER_OBJECT_END); |
| 10169 | return build2 (MODIFY_EXPR, void_type_node, |
| 10170 | instance, clobber); |
| 10171 | } |
| 10172 | |
| 10173 | /* Return true if in an immediate function context, or an unevaluated operand, |
| 10174 | or a default argument/member initializer, or a subexpression of an immediate |
| 10175 | invocation. */ |
| 10176 | |
| 10177 | bool |
| 10178 | in_immediate_context () |
| 10179 | { |
| 10180 | return (cp_unevaluated_operand != 0 |
| 10181 | || (current_function_decl != NULL_TREE |
| 10182 | && DECL_IMMEDIATE_FUNCTION_P (current_function_decl)) |
| 10183 | /* DR 2631: default args and DMI aren't immediately evaluated. |
| 10184 | Return true here so immediate_invocation_p returns false. */ |
| 10185 | || current_binding_level->kind == sk_function_parms |
| 10186 | || current_binding_level->kind == sk_template_parms |
| 10187 | || parsing_nsdmi () |
| 10188 | || in_consteval_if_p); |
| 10189 | } |
| 10190 | |
| 10191 | /* Return true if a call to FN with number of arguments NARGS |
| 10192 | is an immediate invocation. */ |
| 10193 | |
| 10194 | bool |
| 10195 | immediate_invocation_p (tree fn) |
| 10196 | { |
| 10197 | return (TREE_CODE (fn) == FUNCTION_DECL |
| 10198 | && DECL_IMMEDIATE_FUNCTION_P (fn) |
| 10199 | && !in_immediate_context ()); |
| 10200 | } |
| 10201 | |
| 10202 | /* Subroutine of the various build_*_call functions. Overload resolution |
| 10203 | has chosen a winning candidate CAND; build up a CALL_EXPR accordingly. |
| 10204 | ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a |
| 10205 | bitmask of various LOOKUP_* flags which apply to the call itself. */ |
| 10206 | |
| 10207 | static tree |
| 10208 | build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain) |
| 10209 | { |
| 10210 | tree fn = cand->fn; |
| 10211 | const vec<tree, va_gc> *args = cand->args; |
| 10212 | tree first_arg = cand->first_arg; |
| 10213 | conversion **convs = cand->convs; |
| 10214 | tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn)); |
| 10215 | int parmlen; |
| 10216 | tree val; |
| 10217 | int nargs; |
| 10218 | tree *argarray; |
| 10219 | bool already_used = false; |
| 10220 | |
| 10221 | /* In a template, there is no need to perform all of the work that |
| 10222 | is normally done. We are only interested in the type of the call |
| 10223 | expression, i.e., the return type of the function. Any semantic |
| 10224 | errors will be deferred until the template is instantiated. */ |
| 10225 | if (processing_template_decl) |
| 10226 | { |
| 10227 | if (undeduced_auto_decl (fn)) |
| 10228 | mark_used (fn, complain); |
| 10229 | else |
| 10230 | /* Otherwise set TREE_USED for the benefit of -Wunused-function. |
| 10231 | See PR80598. */ |
| 10232 | TREE_USED (fn) = 1; |
| 10233 | |
| 10234 | tree return_type = TREE_TYPE (TREE_TYPE (fn)); |
| 10235 | tree callee; |
| 10236 | if (first_arg == NULL_TREE) |
| 10237 | { |
| 10238 | callee = build_addr_func (function: fn, complain); |
| 10239 | if (callee == error_mark_node) |
| 10240 | return error_mark_node; |
| 10241 | } |
| 10242 | else |
| 10243 | { |
| 10244 | callee = build_baselink (cand->conversion_path, cand->access_path, |
| 10245 | fn, NULL_TREE); |
| 10246 | callee = build_min (COMPONENT_REF, TREE_TYPE (fn), |
| 10247 | first_arg, callee, NULL_TREE); |
| 10248 | } |
| 10249 | |
| 10250 | tree expr = build_call_vec (return_type, callee, args); |
| 10251 | SET_EXPR_LOCATION (expr, input_location); |
| 10252 | if (TREE_THIS_VOLATILE (fn) && cfun) |
| 10253 | current_function_returns_abnormally = 1; |
| 10254 | if (TREE_DEPRECATED (fn) |
| 10255 | && warning_suppressed_at (input_location, |
| 10256 | OPT_Wdeprecated_declarations)) |
| 10257 | /* Make the expr consistent with the location. */ |
| 10258 | TREE_NO_WARNING (expr) = true; |
| 10259 | if (immediate_invocation_p (fn)) |
| 10260 | { |
| 10261 | tree obj_arg = NULL_TREE, exprimm = expr; |
| 10262 | if (DECL_CONSTRUCTOR_P (fn)) |
| 10263 | obj_arg = first_arg; |
| 10264 | if (obj_arg |
| 10265 | && is_dummy_object (obj_arg) |
| 10266 | && !type_dependent_expression_p (obj_arg)) |
| 10267 | { |
| 10268 | exprimm = build_cplus_new (DECL_CONTEXT (fn), expr, complain); |
| 10269 | obj_arg = NULL_TREE; |
| 10270 | } |
| 10271 | /* Look through *(const T *)&obj. */ |
| 10272 | else if (obj_arg && INDIRECT_REF_P (obj_arg)) |
| 10273 | { |
| 10274 | tree addr = TREE_OPERAND (obj_arg, 0); |
| 10275 | STRIP_NOPS (addr); |
| 10276 | if (TREE_CODE (addr) == ADDR_EXPR) |
| 10277 | { |
| 10278 | tree typeo = TREE_TYPE (obj_arg); |
| 10279 | tree typei = TREE_TYPE (TREE_OPERAND (addr, 0)); |
| 10280 | if (same_type_ignoring_top_level_qualifiers_p (typeo, typei)) |
| 10281 | obj_arg = TREE_OPERAND (addr, 0); |
| 10282 | } |
| 10283 | } |
| 10284 | fold_non_dependent_expr (exprimm, complain, |
| 10285 | /*manifestly_const_eval=*/true, |
| 10286 | obj_arg); |
| 10287 | } |
| 10288 | return convert_from_reference (expr); |
| 10289 | } |
| 10290 | |
| 10291 | /* Give any warnings we noticed during overload resolution. */ |
| 10292 | if (cand->warnings && (complain & tf_warning)) |
| 10293 | { |
| 10294 | struct candidate_warning *w; |
| 10295 | for (w = cand->warnings; w; w = w->next) |
| 10296 | joust (cand, w->loser, 1, complain); |
| 10297 | } |
| 10298 | |
| 10299 | /* Core issue 2327: P0135 doesn't say how to handle the case where the |
| 10300 | argument to the copy constructor ends up being a prvalue after |
| 10301 | conversion. Let's do the normal processing, but pretend we aren't |
| 10302 | actually using the copy constructor. */ |
| 10303 | bool force_elide = false; |
| 10304 | if (cxx_dialect >= cxx17 |
| 10305 | && cand->num_convs == 1 |
| 10306 | && DECL_COMPLETE_CONSTRUCTOR_P (fn) |
| 10307 | && (DECL_COPY_CONSTRUCTOR_P (fn) |
| 10308 | || DECL_MOVE_CONSTRUCTOR_P (fn)) |
| 10309 | && !unsafe_return_slot_p (t: first_arg) |
| 10310 | && conv_binds_ref_to_prvalue (c: convs[0])) |
| 10311 | { |
| 10312 | force_elide = true; |
| 10313 | goto not_really_used; |
| 10314 | } |
| 10315 | |
| 10316 | /* OK, we're actually calling this inherited constructor; set its deletedness |
| 10317 | appropriately. We can get away with doing this here because calling is |
| 10318 | the only way to refer to a constructor. */ |
| 10319 | if (DECL_INHERITED_CTOR (fn) |
| 10320 | && !deduce_inheriting_ctor (fn)) |
| 10321 | { |
| 10322 | if (complain & tf_error) |
| 10323 | mark_used (fn); |
| 10324 | return error_mark_node; |
| 10325 | } |
| 10326 | |
| 10327 | /* Make =delete work with SFINAE. */ |
| 10328 | if (DECL_DELETED_FN (fn)) |
| 10329 | { |
| 10330 | if (complain & tf_error) |
| 10331 | { |
| 10332 | mark_used (fn); |
| 10333 | if (cand->next) |
| 10334 | { |
| 10335 | if (flag_diagnostics_all_candidates) |
| 10336 | print_z_candidates (loc: input_location, candidates: cand, /*only_viable_p=*/false); |
| 10337 | else |
| 10338 | inform (input_location, |
| 10339 | "use %<-fdiagnostics-all-candidates%> to display " |
| 10340 | "considered candidates" ); |
| 10341 | } |
| 10342 | } |
| 10343 | return error_mark_node; |
| 10344 | } |
| 10345 | |
| 10346 | if (DECL_FUNCTION_MEMBER_P (fn)) |
| 10347 | { |
| 10348 | tree access_fn; |
| 10349 | /* If FN is a template function, two cases must be considered. |
| 10350 | For example: |
| 10351 | |
| 10352 | struct A { |
| 10353 | protected: |
| 10354 | template <class T> void f(); |
| 10355 | }; |
| 10356 | template <class T> struct B { |
| 10357 | protected: |
| 10358 | void g(); |
| 10359 | }; |
| 10360 | struct C : A, B<int> { |
| 10361 | using A::f; // #1 |
| 10362 | using B<int>::g; // #2 |
| 10363 | }; |
| 10364 | |
| 10365 | In case #1 where `A::f' is a member template, DECL_ACCESS is |
| 10366 | recorded in the primary template but not in its specialization. |
| 10367 | We check access of FN using its primary template. |
| 10368 | |
| 10369 | In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply |
| 10370 | because it is a member of class template B, DECL_ACCESS is |
| 10371 | recorded in the specialization `B<int>::g'. We cannot use its |
| 10372 | primary template because `B<T>::g' and `B<int>::g' may have |
| 10373 | different access. */ |
| 10374 | if (DECL_TEMPLATE_INFO (fn) |
| 10375 | && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn))) |
| 10376 | access_fn = DECL_TI_TEMPLATE (fn); |
| 10377 | else |
| 10378 | access_fn = fn; |
| 10379 | if (!perform_or_defer_access_check (cand->access_path, access_fn, |
| 10380 | fn, complain)) |
| 10381 | return error_mark_node; |
| 10382 | } |
| 10383 | |
| 10384 | /* If we're checking for implicit delete, don't bother with argument |
| 10385 | conversions. */ |
| 10386 | if (flags & LOOKUP_SPECULATIVE) |
| 10387 | { |
| 10388 | if (cand->viable == 1) |
| 10389 | return fn; |
| 10390 | else if (!(complain & tf_error)) |
| 10391 | /* Reject bad conversions now. */ |
| 10392 | return error_mark_node; |
| 10393 | /* else continue to get conversion error. */ |
| 10394 | } |
| 10395 | |
| 10396 | not_really_used: |
| 10397 | |
| 10398 | /* N3276 magic doesn't apply to nested calls. */ |
| 10399 | tsubst_flags_t decltype_flag = (complain & tf_decltype); |
| 10400 | complain &= ~tf_decltype; |
| 10401 | /* No-Cleanup doesn't apply to nested calls either. */ |
| 10402 | tsubst_flags_t no_cleanup_complain = complain; |
| 10403 | complain &= ~tf_no_cleanup; |
| 10404 | |
| 10405 | /* Find maximum size of vector to hold converted arguments. */ |
| 10406 | parmlen = list_length (parm); |
| 10407 | nargs = vec_safe_length (v: args) + (first_arg != NULL_TREE ? 1 : 0); |
| 10408 | if (parmlen > nargs) |
| 10409 | nargs = parmlen; |
| 10410 | argarray = XALLOCAVEC (tree, nargs); |
| 10411 | |
| 10412 | in_consteval_if_p_temp_override icip; |
| 10413 | /* If the call is immediate function invocation, make sure |
| 10414 | taking address of immediate functions is allowed in its arguments. */ |
| 10415 | if (immediate_invocation_p (STRIP_TEMPLATE (fn))) |
| 10416 | in_consteval_if_p = true; |
| 10417 | |
| 10418 | int argarray_size = 0; |
| 10419 | unsigned int arg_index = 0; |
| 10420 | int conv_index = 0; |
| 10421 | int param_index = 0; |
| 10422 | |
| 10423 | auto consume_object_arg = [&arg_index, &first_arg, args]() |
| 10424 | { |
| 10425 | if (!first_arg) |
| 10426 | return (*args)[arg_index++]; |
| 10427 | tree object_arg = first_arg; |
| 10428 | first_arg = NULL_TREE; |
| 10429 | return object_arg; |
| 10430 | }; |
| 10431 | |
| 10432 | /* The implicit parameters to a constructor are not considered by overload |
| 10433 | resolution, and must be of the proper type. */ |
| 10434 | if (DECL_CONSTRUCTOR_P (fn)) |
| 10435 | { |
| 10436 | tree object_arg = consume_object_arg (); |
| 10437 | argarray[argarray_size++] = build_this (obj: object_arg); |
| 10438 | parm = TREE_CHAIN (parm); |
| 10439 | /* We should never try to call the abstract constructor. */ |
| 10440 | gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn)); |
| 10441 | |
| 10442 | if (DECL_HAS_VTT_PARM_P (fn)) |
| 10443 | { |
| 10444 | argarray[argarray_size++] = (*args)[arg_index]; |
| 10445 | ++arg_index; |
| 10446 | parm = TREE_CHAIN (parm); |
| 10447 | } |
| 10448 | } |
| 10449 | /* Bypass access control for 'this' parameter. */ |
| 10450 | else if (DECL_IOBJ_MEMBER_FUNCTION_P (fn)) |
| 10451 | { |
| 10452 | tree arg = build_this (obj: consume_object_arg ()); |
| 10453 | tree argtype = TREE_TYPE (arg); |
| 10454 | |
| 10455 | if (arg == error_mark_node) |
| 10456 | return error_mark_node; |
| 10457 | if (convs[conv_index++]->bad_p) |
| 10458 | { |
| 10459 | if (complain & tf_error) |
| 10460 | { |
| 10461 | auto_diagnostic_group d; |
| 10462 | if (permerror (input_location, "passing %qT as %<this%> " |
| 10463 | "argument discards qualifiers" , |
| 10464 | TREE_TYPE (argtype))) |
| 10465 | inform (DECL_SOURCE_LOCATION (fn), " in call to %qD" , fn); |
| 10466 | } |
| 10467 | else |
| 10468 | return error_mark_node; |
| 10469 | } |
| 10470 | |
| 10471 | /* The class where FN is defined. */ |
| 10472 | tree ctx = DECL_CONTEXT (fn); |
| 10473 | |
| 10474 | /* See if the function member or the whole class type is declared |
| 10475 | final and the call can be devirtualized. */ |
| 10476 | if (DECL_FINAL_P (fn) || CLASSTYPE_FINAL (ctx)) |
| 10477 | flags |= LOOKUP_NONVIRTUAL; |
| 10478 | |
| 10479 | /* [class.mfct.non-static]: If a non-static member function of a class |
| 10480 | X is called for an object that is not of type X, or of a type |
| 10481 | derived from X, the behavior is undefined. |
| 10482 | |
| 10483 | So we can assume that anything passed as 'this' is non-null, and |
| 10484 | optimize accordingly. */ |
| 10485 | /* Check that the base class is accessible. */ |
| 10486 | if (!accessible_base_p (TREE_TYPE (argtype), |
| 10487 | BINFO_TYPE (cand->conversion_path), true)) |
| 10488 | { |
| 10489 | if (complain & tf_error) |
| 10490 | error ("%qT is not an accessible base of %qT" , |
| 10491 | BINFO_TYPE (cand->conversion_path), |
| 10492 | TREE_TYPE (argtype)); |
| 10493 | else |
| 10494 | return error_mark_node; |
| 10495 | } |
| 10496 | /* If fn was found by a using declaration, the conversion path |
| 10497 | will be to the derived class, not the base declaring fn. We |
| 10498 | must convert to the base. */ |
| 10499 | tree base_binfo = cand->conversion_path; |
| 10500 | if (BINFO_TYPE (base_binfo) != ctx) |
| 10501 | { |
| 10502 | base_binfo = lookup_base (base_binfo, ctx, ba_unique, NULL, complain); |
| 10503 | if (base_binfo == error_mark_node) |
| 10504 | return error_mark_node; |
| 10505 | } |
| 10506 | |
| 10507 | /* If we know the dynamic type of the object, look up the final overrider |
| 10508 | in the BINFO. */ |
| 10509 | if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0 |
| 10510 | && resolves_to_fixed_type_p (arg)) |
| 10511 | { |
| 10512 | tree ov = lookup_vfn_in_binfo (DECL_VINDEX (fn), base_binfo); |
| 10513 | |
| 10514 | /* And unwind base_binfo to match. If we don't find the type we're |
| 10515 | looking for in BINFO_INHERITANCE_CHAIN, we're looking at diamond |
| 10516 | inheritance; for now do a normal virtual call in that case. */ |
| 10517 | tree octx = DECL_CONTEXT (ov); |
| 10518 | tree obinfo = base_binfo; |
| 10519 | while (obinfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (obinfo), octx)) |
| 10520 | obinfo = BINFO_INHERITANCE_CHAIN (obinfo); |
| 10521 | if (obinfo) |
| 10522 | { |
| 10523 | fn = ov; |
| 10524 | base_binfo = obinfo; |
| 10525 | flags |= LOOKUP_NONVIRTUAL; |
| 10526 | } |
| 10527 | } |
| 10528 | |
| 10529 | tree converted_arg = build_base_path (PLUS_EXPR, arg, |
| 10530 | base_binfo, 1, complain); |
| 10531 | |
| 10532 | argarray[argarray_size++] = converted_arg; |
| 10533 | parm = TREE_CHAIN (parm); |
| 10534 | } |
| 10535 | |
| 10536 | auto handle_arg = [fn, flags](tree type, |
| 10537 | tree arg, |
| 10538 | int const param_index, |
| 10539 | conversion *conv, |
| 10540 | tsubst_flags_t const arg_complain) |
| 10541 | { |
| 10542 | /* Set user_conv_p on the argument conversions, so rvalue/base handling |
| 10543 | knows not to allow any more UDCs. This needs to happen after we |
| 10544 | process cand->warnings. */ |
| 10545 | if (flags & LOOKUP_NO_CONVERSION) |
| 10546 | conv->user_conv_p = true; |
| 10547 | |
| 10548 | if (arg_complain & tf_warning) |
| 10549 | maybe_warn_pessimizing_move (arg, type, /*return_p=*/false); |
| 10550 | |
| 10551 | tree val = convert_like_with_context (convs: conv, expr: arg, fn, |
| 10552 | argnum: param_index, complain: arg_complain); |
| 10553 | val = convert_for_arg_passing (type, val, complain: arg_complain); |
| 10554 | return val; |
| 10555 | }; |
| 10556 | |
| 10557 | if (DECL_XOBJ_MEMBER_FUNCTION_P (fn)) |
| 10558 | { |
| 10559 | gcc_assert (cand->num_convs > 0); |
| 10560 | tree object_arg = consume_object_arg (); |
| 10561 | val = handle_arg (TREE_VALUE (parm), |
| 10562 | object_arg, |
| 10563 | param_index++, |
| 10564 | convs[conv_index++], |
| 10565 | complain); |
| 10566 | |
| 10567 | if (val == error_mark_node) |
| 10568 | return error_mark_node; |
| 10569 | else |
| 10570 | argarray[argarray_size++] = val; |
| 10571 | parm = TREE_CHAIN (parm); |
| 10572 | } |
| 10573 | |
| 10574 | gcc_assert (first_arg == NULL_TREE); |
| 10575 | for (; arg_index < vec_safe_length (v: args) && parm; |
| 10576 | parm = TREE_CHAIN (parm), ++arg_index, ++param_index, ++conv_index) |
| 10577 | { |
| 10578 | tree current_arg = (*args)[arg_index]; |
| 10579 | |
| 10580 | /* If the argument is NULL and used to (implicitly) instantiate a |
| 10581 | template function (and bind one of the template arguments to |
| 10582 | the type of 'long int'), we don't want to warn about passing NULL |
| 10583 | to non-pointer argument. |
| 10584 | For example, if we have this template function: |
| 10585 | |
| 10586 | template<typename T> void func(T x) {} |
| 10587 | |
| 10588 | we want to warn (when -Wconversion is enabled) in this case: |
| 10589 | |
| 10590 | void foo() { |
| 10591 | func<int>(NULL); |
| 10592 | } |
| 10593 | |
| 10594 | but not in this case: |
| 10595 | |
| 10596 | void foo() { |
| 10597 | func(NULL); |
| 10598 | } |
| 10599 | */ |
| 10600 | bool const conversion_warning = !(null_node_p (expr: current_arg) |
| 10601 | && DECL_TEMPLATE_INFO (fn) |
| 10602 | && cand->template_decl |
| 10603 | && !cand->explicit_targs); |
| 10604 | |
| 10605 | tsubst_flags_t const arg_complain |
| 10606 | = conversion_warning ? complain : complain & ~tf_warning; |
| 10607 | |
| 10608 | val = handle_arg (TREE_VALUE (parm), |
| 10609 | current_arg, |
| 10610 | param_index, |
| 10611 | convs[conv_index], |
| 10612 | arg_complain); |
| 10613 | |
| 10614 | if (val == error_mark_node) |
| 10615 | return error_mark_node; |
| 10616 | else |
| 10617 | argarray[argarray_size++] = val; |
| 10618 | } |
| 10619 | |
| 10620 | /* Default arguments */ |
| 10621 | for (; parm && parm != void_list_node; |
| 10622 | parm = TREE_CHAIN (parm), param_index++) |
| 10623 | { |
| 10624 | if (TREE_VALUE (parm) == error_mark_node) |
| 10625 | return error_mark_node; |
| 10626 | val = convert_default_arg (TREE_VALUE (parm), |
| 10627 | TREE_PURPOSE (parm), |
| 10628 | fn, parmnum: param_index, |
| 10629 | complain); |
| 10630 | if (val == error_mark_node) |
| 10631 | return error_mark_node; |
| 10632 | argarray[argarray_size++] = val; |
| 10633 | } |
| 10634 | |
| 10635 | /* Ellipsis */ |
| 10636 | int magic = magic_varargs_p (fn); |
| 10637 | for (; arg_index < vec_safe_length (v: args); ++arg_index) |
| 10638 | { |
| 10639 | tree a = (*args)[arg_index]; |
| 10640 | if ((magic == 3 && arg_index == 2) || (magic == 4 && arg_index == 0)) |
| 10641 | { |
| 10642 | /* Do no conversions for certain magic varargs. */ |
| 10643 | a = mark_type_use (a); |
| 10644 | if (TREE_CODE (a) == FUNCTION_DECL && reject_gcc_builtin (a)) |
| 10645 | return error_mark_node; |
| 10646 | } |
| 10647 | else if (magic != 0) |
| 10648 | { |
| 10649 | /* Don't truncate excess precision to the semantic type. */ |
| 10650 | if (magic == 1 && TREE_CODE (a) == EXCESS_PRECISION_EXPR) |
| 10651 | a = TREE_OPERAND (a, 0); |
| 10652 | /* For other magic varargs only do decay_conversion. */ |
| 10653 | a = decay_conversion (a, complain); |
| 10654 | } |
| 10655 | else if (DECL_CONSTRUCTOR_P (fn) |
| 10656 | && vec_safe_length (v: args) == 1 |
| 10657 | && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn), |
| 10658 | TREE_TYPE (a))) |
| 10659 | { |
| 10660 | /* Avoid infinite recursion trying to call A(...). */ |
| 10661 | if (complain & tf_error) |
| 10662 | /* Try to call the actual copy constructor for a good error. */ |
| 10663 | call_copy_ctor (a, complain); |
| 10664 | return error_mark_node; |
| 10665 | } |
| 10666 | else |
| 10667 | a = convert_arg_to_ellipsis (arg: a, complain); |
| 10668 | if (a == error_mark_node) |
| 10669 | return error_mark_node; |
| 10670 | argarray[argarray_size++] = a; |
| 10671 | } |
| 10672 | |
| 10673 | gcc_assert (argarray_size <= nargs); |
| 10674 | nargs = argarray_size; |
| 10675 | icip.reset (); |
| 10676 | |
| 10677 | /* Avoid performing argument transformation if warnings are disabled. |
| 10678 | When tf_warning is set and at least one of the warnings is active |
| 10679 | the check_function_arguments function might warn about something. */ |
| 10680 | |
| 10681 | bool warned_p = false; |
| 10682 | if ((complain & tf_warning) |
| 10683 | && (warn_nonnull |
| 10684 | || warn_format |
| 10685 | || warn_suggest_attribute_format |
| 10686 | || warn_restrict)) |
| 10687 | { |
| 10688 | tree *fargs = (!nargs ? argarray |
| 10689 | : (tree *) alloca (nargs * sizeof (tree))); |
| 10690 | for (int j = 0; j < nargs; j++) |
| 10691 | { |
| 10692 | /* For -Wformat undo the implicit passing by hidden reference |
| 10693 | done by convert_arg_to_ellipsis. */ |
| 10694 | if (TREE_CODE (argarray[j]) == ADDR_EXPR |
| 10695 | && TYPE_REF_P (TREE_TYPE (argarray[j]))) |
| 10696 | fargs[j] = TREE_OPERAND (argarray[j], 0); |
| 10697 | else |
| 10698 | fargs[j] = argarray[j]; |
| 10699 | } |
| 10700 | |
| 10701 | warned_p = check_function_arguments (loc: input_location, fn, TREE_TYPE (fn), |
| 10702 | nargs, fargs, NULL, |
| 10703 | comp_types: cp_comp_parm_types); |
| 10704 | } |
| 10705 | |
| 10706 | if (DECL_INHERITED_CTOR (fn)) |
| 10707 | { |
| 10708 | /* Check for passing ellipsis arguments to an inherited constructor. We |
| 10709 | could handle this by open-coding the inherited constructor rather than |
| 10710 | defining it, but let's not bother now. */ |
| 10711 | if (!cp_unevaluated_operand |
| 10712 | && cand->num_convs |
| 10713 | && cand->convs[cand->num_convs-1]->ellipsis_p) |
| 10714 | { |
| 10715 | if (complain & tf_error) |
| 10716 | { |
| 10717 | sorry ("passing arguments to ellipsis of inherited constructor " |
| 10718 | "%qD" , cand->fn); |
| 10719 | inform (DECL_SOURCE_LOCATION (cand->fn), "declared here" ); |
| 10720 | } |
| 10721 | return error_mark_node; |
| 10722 | } |
| 10723 | |
| 10724 | /* A base constructor inheriting from a virtual base doesn't get the |
| 10725 | inherited arguments, just this and __vtt. */ |
| 10726 | if (ctor_omit_inherited_parms (fn)) |
| 10727 | nargs = 2; |
| 10728 | } |
| 10729 | |
| 10730 | /* Avoid actually calling copy constructors and copy assignment operators, |
| 10731 | if possible. */ |
| 10732 | |
| 10733 | if (!force_elide |
| 10734 | && (!flag_elide_constructors |
| 10735 | /* It's unsafe to elide the operation when handling |
| 10736 | a noexcept-expression, it may evaluate to the wrong |
| 10737 | value (c++/53025, c++/96090). */ |
| 10738 | || cp_noexcept_operand != 0)) |
| 10739 | /* Do things the hard way. */; |
| 10740 | else if (cand->num_convs == 1 |
| 10741 | && (DECL_COPY_CONSTRUCTOR_P (fn) |
| 10742 | || DECL_MOVE_CONSTRUCTOR_P (fn))) |
| 10743 | { |
| 10744 | tree targ; |
| 10745 | tree arg = argarray[num_artificial_parms_for (fn)]; |
| 10746 | tree fa = argarray[0]; |
| 10747 | bool trivial = trivial_fn_p (fn); |
| 10748 | |
| 10749 | /* Pull out the real argument, disregarding const-correctness. */ |
| 10750 | targ = arg; |
| 10751 | /* Strip the reference binding for the constructor parameter. */ |
| 10752 | if (CONVERT_EXPR_P (targ) |
| 10753 | && TYPE_REF_P (TREE_TYPE (targ))) |
| 10754 | targ = TREE_OPERAND (targ, 0); |
| 10755 | /* But don't strip any other reference bindings; binding a temporary to a |
| 10756 | reference prevents copy elision. */ |
| 10757 | while ((CONVERT_EXPR_P (targ) |
| 10758 | && !TYPE_REF_P (TREE_TYPE (targ))) |
| 10759 | || TREE_CODE (targ) == NON_LVALUE_EXPR) |
| 10760 | targ = TREE_OPERAND (targ, 0); |
| 10761 | if (TREE_CODE (targ) == ADDR_EXPR) |
| 10762 | { |
| 10763 | targ = TREE_OPERAND (targ, 0); |
| 10764 | if (!same_type_ignoring_top_level_qualifiers_p |
| 10765 | (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ))) |
| 10766 | targ = NULL_TREE; |
| 10767 | } |
| 10768 | else |
| 10769 | targ = NULL_TREE; |
| 10770 | |
| 10771 | if (targ) |
| 10772 | arg = targ; |
| 10773 | else |
| 10774 | arg = cp_build_fold_indirect_ref (arg); |
| 10775 | |
| 10776 | /* In C++17 we shouldn't be copying a TARGET_EXPR except into a |
| 10777 | potentially-overlapping subobject. */ |
| 10778 | if (CHECKING_P && cxx_dialect >= cxx17) |
| 10779 | gcc_assert (TREE_CODE (arg) != TARGET_EXPR |
| 10780 | || force_elide |
| 10781 | /* It's from binding the ref parm to a packed field. */ |
| 10782 | || convs[0]->need_temporary_p |
| 10783 | || seen_error () |
| 10784 | /* See unsafe_copy_elision_p. */ |
| 10785 | || unsafe_return_slot_p (fa)); |
| 10786 | |
| 10787 | bool unsafe = unsafe_copy_elision_p_opt (target: fa, exp: arg); |
| 10788 | bool eliding_temp = (TREE_CODE (arg) == TARGET_EXPR && !unsafe); |
| 10789 | |
| 10790 | /* [class.copy]: the copy constructor is implicitly defined even if the |
| 10791 | implementation elided its use. But don't warn about deprecation when |
| 10792 | eliding a temporary, as then no copy is actually performed. */ |
| 10793 | warning_sentinel s (warn_deprecated_copy, eliding_temp); |
| 10794 | if (force_elide) |
| 10795 | /* The language says this isn't called. */; |
| 10796 | else if (!trivial) |
| 10797 | { |
| 10798 | if (!mark_used (fn, complain) && !(complain & tf_error)) |
| 10799 | return error_mark_node; |
| 10800 | already_used = true; |
| 10801 | } |
| 10802 | else |
| 10803 | cp_handle_deprecated_or_unavailable (fn, complain); |
| 10804 | |
| 10805 | if (eliding_temp && DECL_BASE_CONSTRUCTOR_P (fn) |
| 10806 | && !make_base_init_ok (exp: arg)) |
| 10807 | unsafe = true; |
| 10808 | |
| 10809 | /* If we're creating a temp and we already have one, don't create a |
| 10810 | new one. If we're not creating a temp but we get one, use |
| 10811 | INIT_EXPR to collapse the temp into our target. Otherwise, if the |
| 10812 | ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a |
| 10813 | temp or an INIT_EXPR otherwise. */ |
| 10814 | if (is_dummy_object (fa)) |
| 10815 | { |
| 10816 | if (TREE_CODE (arg) == TARGET_EXPR) |
| 10817 | return arg; |
| 10818 | else if (trivial) |
| 10819 | return force_target_expr (DECL_CONTEXT (fn), arg, complain); |
| 10820 | } |
| 10821 | else if ((trivial || TREE_CODE (arg) == TARGET_EXPR) |
| 10822 | && !unsafe) |
| 10823 | { |
| 10824 | tree to = cp_build_fold_indirect_ref (fa); |
| 10825 | val = cp_build_init_expr (t: to, i: arg); |
| 10826 | return val; |
| 10827 | } |
| 10828 | } |
| 10829 | else if (DECL_ASSIGNMENT_OPERATOR_P (fn) |
| 10830 | && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR) |
| 10831 | && trivial_fn_p (fn)) |
| 10832 | { |
| 10833 | tree to = cp_build_fold_indirect_ref (argarray[0]); |
| 10834 | tree type = TREE_TYPE (to); |
| 10835 | tree as_base = CLASSTYPE_AS_BASE (type); |
| 10836 | tree arg = argarray[1]; |
| 10837 | location_t loc = cp_expr_loc_or_input_loc (t: arg); |
| 10838 | |
| 10839 | if (is_really_empty_class (type, /*ignore_vptr*/true)) |
| 10840 | { |
| 10841 | /* Avoid copying empty classes, but ensure op= returns an lvalue even |
| 10842 | if the object argument isn't one. */ |
| 10843 | to = force_lvalue (to, complain); |
| 10844 | val = build2 (COMPOUND_EXPR, type, arg, to); |
| 10845 | suppress_warning (val, OPT_Wunused); |
| 10846 | } |
| 10847 | else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base))) |
| 10848 | { |
| 10849 | if (is_std_init_list (type) |
| 10850 | && conv_binds_ref_to_prvalue (c: convs[1])) |
| 10851 | warning_at (loc, OPT_Winit_list_lifetime, |
| 10852 | "assignment from temporary %<initializer_list%> does " |
| 10853 | "not extend the lifetime of the underlying array" ); |
| 10854 | arg = cp_build_fold_indirect_ref (arg); |
| 10855 | val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg); |
| 10856 | } |
| 10857 | else |
| 10858 | { |
| 10859 | /* We must only copy the non-tail padding parts. */ |
| 10860 | tree arg0, arg2, t; |
| 10861 | tree array_type, alias_set; |
| 10862 | |
| 10863 | arg2 = TYPE_SIZE_UNIT (as_base); |
| 10864 | /* Ensure op= returns an lvalue even if the object argument isn't |
| 10865 | one. */ |
| 10866 | to = force_lvalue (to, complain); |
| 10867 | to = cp_stabilize_reference (to); |
| 10868 | arg0 = cp_build_addr_expr (to, complain); |
| 10869 | |
| 10870 | array_type = build_array_type (unsigned_char_type_node, |
| 10871 | build_index_type |
| 10872 | (size_binop (MINUS_EXPR, |
| 10873 | arg2, size_int (1)))); |
| 10874 | alias_set = build_int_cst (build_pointer_type (type), 0); |
| 10875 | t = build2 (MODIFY_EXPR, void_type_node, |
| 10876 | build2 (MEM_REF, array_type, arg0, alias_set), |
| 10877 | build2 (MEM_REF, array_type, arg, alias_set)); |
| 10878 | val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to); |
| 10879 | suppress_warning (val, OPT_Wunused); |
| 10880 | } |
| 10881 | |
| 10882 | cp_handle_deprecated_or_unavailable (fn, complain); |
| 10883 | |
| 10884 | return val; |
| 10885 | } |
| 10886 | else if (trivial_fn_p (fn)) |
| 10887 | { |
| 10888 | if (DECL_DESTRUCTOR_P (fn)) |
| 10889 | return build_trivial_dtor_call (instance: argarray[0]); |
| 10890 | else if (default_ctor_p (fn)) |
| 10891 | { |
| 10892 | if (is_dummy_object (argarray[0])) |
| 10893 | return force_target_expr (DECL_CONTEXT (fn), void_node, |
| 10894 | no_cleanup_complain); |
| 10895 | else |
| 10896 | return cp_build_fold_indirect_ref (argarray[0]); |
| 10897 | } |
| 10898 | } |
| 10899 | |
| 10900 | gcc_assert (!force_elide); |
| 10901 | |
| 10902 | if (!already_used |
| 10903 | && !mark_used (fn, complain)) |
| 10904 | return error_mark_node; |
| 10905 | |
| 10906 | /* Warn if the built-in writes to an object of a non-trivial type. */ |
| 10907 | if (warn_class_memaccess |
| 10908 | && vec_safe_length (v: args) >= 2 |
| 10909 | && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL) |
| 10910 | maybe_warn_class_memaccess (input_location, fn, args); |
| 10911 | |
| 10912 | if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0) |
| 10913 | { |
| 10914 | tree t; |
| 10915 | tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])), |
| 10916 | DECL_CONTEXT (fn), |
| 10917 | ba_any, NULL, complain); |
| 10918 | gcc_assert (binfo && binfo != error_mark_node); |
| 10919 | |
| 10920 | argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1, |
| 10921 | complain); |
| 10922 | if (TREE_SIDE_EFFECTS (argarray[0])) |
| 10923 | argarray[0] = save_expr (argarray[0]); |
| 10924 | t = build_pointer_type (TREE_TYPE (fn)); |
| 10925 | fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn)); |
| 10926 | TREE_TYPE (fn) = t; |
| 10927 | } |
| 10928 | else |
| 10929 | { |
| 10930 | /* If FN is marked deprecated or unavailable, then we've already |
| 10931 | issued a diagnostic from mark_used above, so avoid redundantly |
| 10932 | issuing another one from build_addr_func. */ |
| 10933 | auto w = make_temp_override (var&: deprecated_state, |
| 10934 | overrider: UNAVAILABLE_DEPRECATED_SUPPRESS); |
| 10935 | |
| 10936 | fn = build_addr_func (function: fn, complain); |
| 10937 | if (fn == error_mark_node) |
| 10938 | return error_mark_node; |
| 10939 | |
| 10940 | /* We're actually invoking the function. (Immediate functions get an |
| 10941 | & when invoking it even though the user didn't use &.) */ |
| 10942 | ADDR_EXPR_DENOTES_CALL_P (fn) = true; |
| 10943 | } |
| 10944 | |
| 10945 | tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag); |
| 10946 | if (call == error_mark_node) |
| 10947 | return call; |
| 10948 | if (cand->flags & LOOKUP_LIST_INIT_CTOR) |
| 10949 | { |
| 10950 | tree c = extract_call_expr (call); |
| 10951 | /* build_new_op will clear this when appropriate. */ |
| 10952 | CALL_EXPR_ORDERED_ARGS (c) = true; |
| 10953 | } |
| 10954 | if (warned_p) |
| 10955 | { |
| 10956 | tree c = extract_call_expr (call); |
| 10957 | if (TREE_CODE (c) == CALL_EXPR) |
| 10958 | suppress_warning (c /* Suppress all warnings. */); |
| 10959 | } |
| 10960 | else if (TREE_DEPRECATED (fn) |
| 10961 | && warning_suppressed_at (input_location, |
| 10962 | OPT_Wdeprecated_declarations)) |
| 10963 | { |
| 10964 | tree c = extract_call_expr (call); |
| 10965 | if (TREE_CODE (c) == CALL_EXPR) |
| 10966 | TREE_NO_WARNING (c) = true; |
| 10967 | } |
| 10968 | |
| 10969 | return call; |
| 10970 | } |
| 10971 | |
| 10972 | namespace |
| 10973 | { |
| 10974 | |
| 10975 | /* Return the DECL of the first non-static subobject of class TYPE |
| 10976 | that satisfies the predicate PRED or null if none can be found. */ |
| 10977 | |
| 10978 | template <class Predicate> |
| 10979 | tree |
| 10980 | first_non_static_field (tree type, Predicate pred) |
| 10981 | { |
| 10982 | if (!type || !CLASS_TYPE_P (type)) |
| 10983 | return NULL_TREE; |
| 10984 | |
| 10985 | for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
| 10986 | { |
| 10987 | if (TREE_CODE (field) != FIELD_DECL) |
| 10988 | continue; |
| 10989 | if (TREE_STATIC (field)) |
| 10990 | continue; |
| 10991 | if (pred (field)) |
| 10992 | return field; |
| 10993 | } |
| 10994 | |
| 10995 | int i = 0; |
| 10996 | |
| 10997 | for (tree base_binfo, binfo = TYPE_BINFO (type); |
| 10998 | BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) |
| 10999 | { |
| 11000 | tree base = TREE_TYPE (base_binfo); |
| 11001 | if (pred (base)) |
| 11002 | return base; |
| 11003 | if (tree field = first_non_static_field (base, pred)) |
| 11004 | return field; |
| 11005 | } |
| 11006 | |
| 11007 | return NULL_TREE; |
| 11008 | } |
| 11009 | |
| 11010 | struct NonPublicField |
| 11011 | { |
| 11012 | bool operator() (const_tree t) const |
| 11013 | { |
| 11014 | return DECL_P (t) && (TREE_PRIVATE (t) || TREE_PROTECTED (t)); |
| 11015 | } |
| 11016 | }; |
| 11017 | |
| 11018 | /* Return the DECL of the first non-public subobject of class TYPE |
| 11019 | or null if none can be found. */ |
| 11020 | |
| 11021 | static inline tree |
| 11022 | first_non_public_field (tree type) |
| 11023 | { |
| 11024 | return first_non_static_field (type, pred: NonPublicField ()); |
| 11025 | } |
| 11026 | |
| 11027 | struct NonTrivialField |
| 11028 | { |
| 11029 | bool operator() (const_tree t) const |
| 11030 | { |
| 11031 | return !trivial_type_p (DECL_P (t) ? TREE_TYPE (t) : t); |
| 11032 | } |
| 11033 | }; |
| 11034 | |
| 11035 | /* Return the DECL of the first non-trivial subobject of class TYPE |
| 11036 | or null if none can be found. */ |
| 11037 | |
| 11038 | static inline tree |
| 11039 | first_non_trivial_field (tree type) |
| 11040 | { |
| 11041 | return first_non_static_field (type, pred: NonTrivialField ()); |
| 11042 | } |
| 11043 | |
| 11044 | } /* unnamed namespace */ |
| 11045 | |
| 11046 | /* Return true if all copy and move assignment operator overloads for |
| 11047 | class TYPE are trivial and at least one of them is not deleted and, |
| 11048 | when ACCESS is set, accessible. Return false otherwise. Set |
| 11049 | HASASSIGN to true when the TYPE has a (not necessarily trivial) |
| 11050 | copy or move assignment. */ |
| 11051 | |
| 11052 | static bool |
| 11053 | has_trivial_copy_assign_p (tree type, bool access, bool *hasassign) |
| 11054 | { |
| 11055 | tree fns = get_class_binding (type, assign_op_identifier); |
| 11056 | bool all_trivial = true; |
| 11057 | |
| 11058 | /* Iterate over overloads of the assignment operator, checking |
| 11059 | accessible copy assignments for triviality. */ |
| 11060 | |
| 11061 | for (tree f : ovl_range (fns)) |
| 11062 | { |
| 11063 | /* Skip operators that aren't copy assignments. */ |
| 11064 | if (!copy_fn_p (f)) |
| 11065 | continue; |
| 11066 | |
| 11067 | bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f)) |
| 11068 | || accessible_p (TYPE_BINFO (type), f, true)); |
| 11069 | |
| 11070 | /* Skip template assignment operators and deleted functions. */ |
| 11071 | if (TREE_CODE (f) != FUNCTION_DECL || DECL_DELETED_FN (f)) |
| 11072 | continue; |
| 11073 | |
| 11074 | if (accessible) |
| 11075 | *hasassign = true; |
| 11076 | |
| 11077 | if (!accessible || !trivial_fn_p (f)) |
| 11078 | all_trivial = false; |
| 11079 | |
| 11080 | /* Break early when both properties have been determined. */ |
| 11081 | if (*hasassign && !all_trivial) |
| 11082 | break; |
| 11083 | } |
| 11084 | |
| 11085 | /* Return true if they're all trivial and one of the expressions |
| 11086 | TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */ |
| 11087 | tree ref = cp_build_reference_type (type, false); |
| 11088 | return (all_trivial |
| 11089 | && (is_trivially_xible (MODIFY_EXPR, type, type) |
| 11090 | || is_trivially_xible (MODIFY_EXPR, type, ref))); |
| 11091 | } |
| 11092 | |
| 11093 | /* Return true if all copy and move ctor overloads for class TYPE are |
| 11094 | trivial and at least one of them is not deleted and, when ACCESS is |
| 11095 | set, accessible. Return false otherwise. Set each element of HASCTOR[] |
| 11096 | to true when the TYPE has a (not necessarily trivial) default and copy |
| 11097 | (or move) ctor, respectively. */ |
| 11098 | |
| 11099 | static bool |
| 11100 | has_trivial_copy_p (tree type, bool access, bool hasctor[2]) |
| 11101 | { |
| 11102 | tree fns = get_class_binding (type, complete_ctor_identifier); |
| 11103 | bool all_trivial = true; |
| 11104 | |
| 11105 | for (tree f : ovl_range (fns)) |
| 11106 | { |
| 11107 | /* Skip template constructors. */ |
| 11108 | if (TREE_CODE (f) != FUNCTION_DECL) |
| 11109 | continue; |
| 11110 | |
| 11111 | bool cpy_or_move_ctor_p = copy_fn_p (f); |
| 11112 | |
| 11113 | /* Skip ctors other than default, copy, and move. */ |
| 11114 | if (!cpy_or_move_ctor_p && !default_ctor_p (f)) |
| 11115 | continue; |
| 11116 | |
| 11117 | if (DECL_DELETED_FN (f)) |
| 11118 | continue; |
| 11119 | |
| 11120 | bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f)) |
| 11121 | || accessible_p (TYPE_BINFO (type), f, true)); |
| 11122 | |
| 11123 | if (accessible) |
| 11124 | hasctor[cpy_or_move_ctor_p] = true; |
| 11125 | |
| 11126 | if (cpy_or_move_ctor_p && (!accessible || !trivial_fn_p (f))) |
| 11127 | all_trivial = false; |
| 11128 | |
| 11129 | /* Break early when both properties have been determined. */ |
| 11130 | if (hasctor[0] && hasctor[1] && !all_trivial) |
| 11131 | break; |
| 11132 | } |
| 11133 | |
| 11134 | return all_trivial; |
| 11135 | } |
| 11136 | |
| 11137 | /* Issue a warning on a call to the built-in function FNDECL if it is |
| 11138 | a raw memory write whose destination is not an object of (something |
| 11139 | like) trivial or standard layout type with a non-deleted assignment |
| 11140 | and copy ctor. Detects const correctness violations, corrupting |
| 11141 | references, virtual table pointers, and bypassing non-trivial |
| 11142 | assignments. */ |
| 11143 | |
| 11144 | static void |
| 11145 | maybe_warn_class_memaccess (location_t loc, tree fndecl, |
| 11146 | const vec<tree, va_gc> *args) |
| 11147 | { |
| 11148 | /* Except for bcopy where it's second, the destination pointer is |
| 11149 | the first argument for all functions handled here. Compute |
| 11150 | the index of the destination and source arguments. */ |
| 11151 | unsigned dstidx = DECL_FUNCTION_CODE (decl: fndecl) == BUILT_IN_BCOPY; |
| 11152 | unsigned srcidx = !dstidx; |
| 11153 | |
| 11154 | tree dest = (*args)[dstidx]; |
| 11155 | if (!TREE_TYPE (dest) |
| 11156 | || (TREE_CODE (TREE_TYPE (dest)) != ARRAY_TYPE |
| 11157 | && !INDIRECT_TYPE_P (TREE_TYPE (dest)))) |
| 11158 | return; |
| 11159 | |
| 11160 | tree srctype = NULL_TREE; |
| 11161 | |
| 11162 | /* Determine the type of the pointed-to object and whether it's |
| 11163 | a complete class type. */ |
| 11164 | tree desttype = TREE_TYPE (TREE_TYPE (dest)); |
| 11165 | |
| 11166 | if (!desttype || !COMPLETE_TYPE_P (desttype) || !CLASS_TYPE_P (desttype)) |
| 11167 | return; |
| 11168 | |
| 11169 | /* Check to see if the raw memory call is made by a non-static member |
| 11170 | function with THIS as the destination argument for the destination |
| 11171 | type. If so, and if the class has no non-trivial bases or members, |
| 11172 | be more permissive. */ |
| 11173 | if (current_function_decl |
| 11174 | && DECL_OBJECT_MEMBER_FUNCTION_P (current_function_decl) |
| 11175 | && is_object_parameter (tree_strip_nop_conversions (dest))) |
| 11176 | { |
| 11177 | tree ctx = DECL_CONTEXT (current_function_decl); |
| 11178 | bool special = same_type_ignoring_top_level_qualifiers_p (ctx, desttype); |
| 11179 | tree binfo = TYPE_BINFO (ctx); |
| 11180 | |
| 11181 | if (special |
| 11182 | && !BINFO_VTABLE (binfo) |
| 11183 | && !first_non_trivial_field (type: desttype)) |
| 11184 | return; |
| 11185 | } |
| 11186 | |
| 11187 | /* True if the class is trivial. */ |
| 11188 | bool trivial = trivial_type_p (desttype); |
| 11189 | |
| 11190 | /* Set to true if DESTYPE has an accessible copy assignment. */ |
| 11191 | bool hasassign = false; |
| 11192 | /* True if all of the class' overloaded copy assignment operators |
| 11193 | are all trivial (and not deleted) and at least one of them is |
| 11194 | accessible. */ |
| 11195 | bool trivassign = has_trivial_copy_assign_p (type: desttype, access: true, hasassign: &hasassign); |
| 11196 | |
| 11197 | /* Set to true if DESTTYPE has an accessible default and copy ctor, |
| 11198 | respectively. */ |
| 11199 | bool hasctors[2] = { false, false }; |
| 11200 | |
| 11201 | /* True if all of the class' overloaded copy constructors are all |
| 11202 | trivial (and not deleted) and at least one of them is accessible. */ |
| 11203 | bool trivcopy = has_trivial_copy_p (type: desttype, access: true, hasctor: hasctors); |
| 11204 | |
| 11205 | /* Set FLD to the first private/protected member of the class. */ |
| 11206 | tree fld = trivial ? first_non_public_field (type: desttype) : NULL_TREE; |
| 11207 | |
| 11208 | /* The warning format string. */ |
| 11209 | const char *warnfmt = NULL; |
| 11210 | /* A suggested alternative to offer instead of the raw memory call. |
| 11211 | Empty string when none can be come up with. */ |
| 11212 | const char *suggest = "" ; |
| 11213 | bool warned = false; |
| 11214 | |
| 11215 | switch (DECL_FUNCTION_CODE (decl: fndecl)) |
| 11216 | { |
| 11217 | case BUILT_IN_MEMSET: |
| 11218 | if (!integer_zerop (maybe_constant_value ((*args)[1]))) |
| 11219 | { |
| 11220 | /* Diagnose setting non-copy-assignable or non-trivial types, |
| 11221 | or types with a private member, to (potentially) non-zero |
| 11222 | bytes. Since the value of the bytes being written is unknown, |
| 11223 | suggest using assignment instead (if one exists). Also warn |
| 11224 | for writes into objects for which zero-initialization doesn't |
| 11225 | mean all bits clear (pointer-to-member data, where null is all |
| 11226 | bits set). Since the value being written is (most likely) |
| 11227 | non-zero, simply suggest assignment (but not copy assignment). */ |
| 11228 | suggest = "; use assignment instead" ; |
| 11229 | if (!trivassign) |
| 11230 | warnfmt = G_("%qD writing to an object of type %#qT with " |
| 11231 | "no trivial copy-assignment" ); |
| 11232 | else if (!trivial) |
| 11233 | warnfmt = G_("%qD writing to an object of non-trivial type %#qT%s" ); |
| 11234 | else if (fld) |
| 11235 | { |
| 11236 | const char *access = TREE_PRIVATE (fld) ? "private" : "protected" ; |
| 11237 | warned = warning_at (loc, OPT_Wclass_memaccess, |
| 11238 | "%qD writing to an object of type %#qT with " |
| 11239 | "%qs member %qD" , |
| 11240 | fndecl, desttype, access, fld); |
| 11241 | } |
| 11242 | else if (!zero_init_p (desttype)) |
| 11243 | warnfmt = G_("%qD writing to an object of type %#qT containing " |
| 11244 | "a pointer to data member%s" ); |
| 11245 | |
| 11246 | break; |
| 11247 | } |
| 11248 | /* Fall through. */ |
| 11249 | |
| 11250 | case BUILT_IN_BZERO: |
| 11251 | /* Similarly to the above, diagnose clearing non-trivial or non- |
| 11252 | standard layout objects, or objects of types with no assignmenmt. |
| 11253 | Since the value being written is known to be zero, suggest either |
| 11254 | copy assignment, copy ctor, or default ctor as an alternative, |
| 11255 | depending on what's available. */ |
| 11256 | |
| 11257 | if (hasassign && hasctors[0]) |
| 11258 | suggest = G_("; use assignment or value-initialization instead" ); |
| 11259 | else if (hasassign) |
| 11260 | suggest = G_("; use assignment instead" ); |
| 11261 | else if (hasctors[0]) |
| 11262 | suggest = G_("; use value-initialization instead" ); |
| 11263 | |
| 11264 | if (!trivassign) |
| 11265 | warnfmt = G_("%qD clearing an object of type %#qT with " |
| 11266 | "no trivial copy-assignment%s" ); |
| 11267 | else if (!trivial) |
| 11268 | warnfmt = G_("%qD clearing an object of non-trivial type %#qT%s" ); |
| 11269 | else if (!zero_init_p (desttype)) |
| 11270 | warnfmt = G_("%qD clearing an object of type %#qT containing " |
| 11271 | "a pointer-to-member%s" ); |
| 11272 | break; |
| 11273 | |
| 11274 | case BUILT_IN_BCOPY: |
| 11275 | case BUILT_IN_MEMCPY: |
| 11276 | case BUILT_IN_MEMMOVE: |
| 11277 | case BUILT_IN_MEMPCPY: |
| 11278 | /* Determine the type of the source object. */ |
| 11279 | srctype = TREE_TYPE ((*args)[srcidx]); |
| 11280 | if (!srctype || !INDIRECT_TYPE_P (srctype)) |
| 11281 | srctype = void_type_node; |
| 11282 | else |
| 11283 | srctype = TREE_TYPE (srctype); |
| 11284 | |
| 11285 | /* Since it's impossible to determine wheter the byte copy is |
| 11286 | being used in place of assignment to an existing object or |
| 11287 | as a substitute for initialization, assume it's the former. |
| 11288 | Determine the best alternative to use instead depending on |
| 11289 | what's not deleted. */ |
| 11290 | if (hasassign && hasctors[1]) |
| 11291 | suggest = G_("; use copy-assignment or copy-initialization instead" ); |
| 11292 | else if (hasassign) |
| 11293 | suggest = G_("; use copy-assignment instead" ); |
| 11294 | else if (hasctors[1]) |
| 11295 | suggest = G_("; use copy-initialization instead" ); |
| 11296 | |
| 11297 | if (!trivassign) |
| 11298 | warnfmt = G_("%qD writing to an object of type %#qT with no trivial " |
| 11299 | "copy-assignment%s" ); |
| 11300 | else if (!trivially_copyable_p (desttype)) |
| 11301 | warnfmt = G_("%qD writing to an object of non-trivially copyable " |
| 11302 | "type %#qT%s" ); |
| 11303 | else if (!trivcopy) |
| 11304 | warnfmt = G_("%qD writing to an object with a deleted copy constructor" ); |
| 11305 | |
| 11306 | else if (!trivial |
| 11307 | && !VOID_TYPE_P (srctype) |
| 11308 | && !is_byte_access_type (srctype) |
| 11309 | && !same_type_ignoring_top_level_qualifiers_p (desttype, |
| 11310 | srctype)) |
| 11311 | { |
| 11312 | /* Warn when copying into a non-trivial object from an object |
| 11313 | of a different type other than void or char. */ |
| 11314 | warned = warning_at (loc, OPT_Wclass_memaccess, |
| 11315 | "%qD copying an object of non-trivial type " |
| 11316 | "%#qT from an array of %#qT" , |
| 11317 | fndecl, desttype, srctype); |
| 11318 | } |
| 11319 | else if (fld |
| 11320 | && !VOID_TYPE_P (srctype) |
| 11321 | && !is_byte_access_type (srctype) |
| 11322 | && !same_type_ignoring_top_level_qualifiers_p (desttype, |
| 11323 | srctype)) |
| 11324 | { |
| 11325 | const char *access = TREE_PRIVATE (fld) ? "private" : "protected" ; |
| 11326 | warned = warning_at (loc, OPT_Wclass_memaccess, |
| 11327 | "%qD copying an object of type %#qT with " |
| 11328 | "%qs member %qD from an array of %#qT; use " |
| 11329 | "assignment or copy-initialization instead" , |
| 11330 | fndecl, desttype, access, fld, srctype); |
| 11331 | } |
| 11332 | else if (!trivial && vec_safe_length (v: args) > 2) |
| 11333 | { |
| 11334 | tree sz = maybe_constant_value ((*args)[2]); |
| 11335 | if (!tree_fits_uhwi_p (sz)) |
| 11336 | break; |
| 11337 | |
| 11338 | /* Finally, warn on partial copies. */ |
| 11339 | unsigned HOST_WIDE_INT typesize |
| 11340 | = tree_to_uhwi (TYPE_SIZE_UNIT (desttype)); |
| 11341 | if (typesize == 0) |
| 11342 | break; |
| 11343 | if (unsigned HOST_WIDE_INT partial = tree_to_uhwi (sz) % typesize) |
| 11344 | warned = warning_at (loc, OPT_Wclass_memaccess, |
| 11345 | (typesize - partial > 1 |
| 11346 | ? G_("%qD writing to an object of " |
| 11347 | "a non-trivial type %#qT leaves %wu " |
| 11348 | "bytes unchanged" ) |
| 11349 | : G_("%qD writing to an object of " |
| 11350 | "a non-trivial type %#qT leaves %wu " |
| 11351 | "byte unchanged" )), |
| 11352 | fndecl, desttype, typesize - partial); |
| 11353 | } |
| 11354 | break; |
| 11355 | |
| 11356 | case BUILT_IN_REALLOC: |
| 11357 | |
| 11358 | if (!trivially_copyable_p (desttype)) |
| 11359 | warnfmt = G_("%qD moving an object of non-trivially copyable type " |
| 11360 | "%#qT; use %<new%> and %<delete%> instead" ); |
| 11361 | else if (!trivcopy) |
| 11362 | warnfmt = G_("%qD moving an object of type %#qT with deleted copy " |
| 11363 | "constructor; use %<new%> and %<delete%> instead" ); |
| 11364 | else if (!get_dtor (desttype, tf_none)) |
| 11365 | warnfmt = G_("%qD moving an object of type %#qT with deleted " |
| 11366 | "destructor" ); |
| 11367 | else if (!trivial) |
| 11368 | { |
| 11369 | tree sz = maybe_constant_value ((*args)[1]); |
| 11370 | if (TREE_CODE (sz) == INTEGER_CST |
| 11371 | && tree_int_cst_lt (t1: sz, TYPE_SIZE_UNIT (desttype))) |
| 11372 | /* Finally, warn on reallocation into insufficient space. */ |
| 11373 | warned = warning_at (loc, OPT_Wclass_memaccess, |
| 11374 | "%qD moving an object of non-trivial type " |
| 11375 | "%#qT and size %E into a region of size %E" , |
| 11376 | fndecl, desttype, TYPE_SIZE_UNIT (desttype), |
| 11377 | sz); |
| 11378 | } |
| 11379 | break; |
| 11380 | |
| 11381 | default: |
| 11382 | return; |
| 11383 | } |
| 11384 | |
| 11385 | if (warnfmt) |
| 11386 | { |
| 11387 | if (suggest) |
| 11388 | warned = warning_at (loc, OPT_Wclass_memaccess, |
| 11389 | warnfmt, fndecl, desttype, suggest); |
| 11390 | else |
| 11391 | warned = warning_at (loc, OPT_Wclass_memaccess, |
| 11392 | warnfmt, fndecl, desttype); |
| 11393 | } |
| 11394 | |
| 11395 | if (warned) |
| 11396 | inform (location_of (desttype), "%#qT declared here" , desttype); |
| 11397 | } |
| 11398 | |
| 11399 | /* Build and return a call to FN, using NARGS arguments in ARGARRAY. |
| 11400 | If FN is the result of resolving an overloaded target built-in, |
| 11401 | ORIG_FNDECL is the original function decl, otherwise it is null. |
| 11402 | This function performs no overload resolution, conversion, or other |
| 11403 | high-level operations. */ |
| 11404 | |
| 11405 | tree |
| 11406 | build_cxx_call (tree fn, int nargs, tree *argarray, |
| 11407 | tsubst_flags_t complain, tree orig_fndecl) |
| 11408 | { |
| 11409 | tree fndecl; |
| 11410 | |
| 11411 | /* Remember roughly where this call is. */ |
| 11412 | location_t loc = cp_expr_loc_or_input_loc (t: fn); |
| 11413 | fn = build_call_a (function: fn, n: nargs, argarray); |
| 11414 | SET_EXPR_LOCATION (fn, loc); |
| 11415 | |
| 11416 | fndecl = get_callee_fndecl (fn); |
| 11417 | if (!orig_fndecl) |
| 11418 | orig_fndecl = fndecl; |
| 11419 | |
| 11420 | /* Check that arguments to builtin functions match the expectations. */ |
| 11421 | if (fndecl |
| 11422 | && !processing_template_decl |
| 11423 | && fndecl_built_in_p (node: fndecl)) |
| 11424 | { |
| 11425 | int i; |
| 11426 | |
| 11427 | /* We need to take care that values to BUILT_IN_NORMAL |
| 11428 | are reduced. */ |
| 11429 | for (i = 0; i < nargs; i++) |
| 11430 | argarray[i] = maybe_constant_value (argarray[i]); |
| 11431 | |
| 11432 | if (!check_builtin_function_arguments (EXPR_LOCATION (fn), vNULL, fndecl, |
| 11433 | orig_fndecl, nargs, argarray, |
| 11434 | complain & tf_error)) |
| 11435 | return error_mark_node; |
| 11436 | else if (fndecl_built_in_p (node: fndecl, name1: BUILT_IN_CLEAR_PADDING)) |
| 11437 | { |
| 11438 | tree arg0 = argarray[0]; |
| 11439 | STRIP_NOPS (arg0); |
| 11440 | if (TREE_CODE (arg0) == ADDR_EXPR |
| 11441 | && DECL_P (TREE_OPERAND (arg0, 0)) |
| 11442 | && same_type_ignoring_top_level_qualifiers_p |
| 11443 | (TREE_TYPE (TREE_TYPE (argarray[0])), |
| 11444 | TREE_TYPE (TREE_TYPE (arg0)))) |
| 11445 | /* For __builtin_clear_padding (&var) we know the type |
| 11446 | is for a complete object, so there is no risk in clearing |
| 11447 | padding that is reused in some derived class member. */; |
| 11448 | else if (!trivially_copyable_p (TREE_TYPE (TREE_TYPE (argarray[0])))) |
| 11449 | { |
| 11450 | error_at (EXPR_LOC_OR_LOC (argarray[0], input_location), |
| 11451 | "argument %u in call to function %qE " |
| 11452 | "has pointer to a non-trivially-copyable type (%qT)" , |
| 11453 | 1, fndecl, TREE_TYPE (argarray[0])); |
| 11454 | return error_mark_node; |
| 11455 | } |
| 11456 | } |
| 11457 | } |
| 11458 | |
| 11459 | if (VOID_TYPE_P (TREE_TYPE (fn))) |
| 11460 | return fn; |
| 11461 | |
| 11462 | /* 5.2.2/11: If a function call is a prvalue of object type: if the |
| 11463 | function call is either the operand of a decltype-specifier or the |
| 11464 | right operand of a comma operator that is the operand of a |
| 11465 | decltype-specifier, a temporary object is not introduced for the |
| 11466 | prvalue. The type of the prvalue may be incomplete. */ |
| 11467 | if (!(complain & tf_decltype)) |
| 11468 | { |
| 11469 | fn = require_complete_type (fn, complain); |
| 11470 | if (fn == error_mark_node) |
| 11471 | return error_mark_node; |
| 11472 | |
| 11473 | if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn))) |
| 11474 | { |
| 11475 | fn = build_cplus_new (TREE_TYPE (fn), fn, complain); |
| 11476 | maybe_warn_parm_abi (TREE_TYPE (fn), loc); |
| 11477 | } |
| 11478 | } |
| 11479 | return convert_from_reference (fn); |
| 11480 | } |
| 11481 | |
| 11482 | /* Returns the value to use for the in-charge parameter when making a |
| 11483 | call to a function with the indicated NAME. |
| 11484 | |
| 11485 | FIXME:Can't we find a neater way to do this mapping? */ |
| 11486 | |
| 11487 | tree |
| 11488 | in_charge_arg_for_name (tree name) |
| 11489 | { |
| 11490 | if (IDENTIFIER_CTOR_P (name)) |
| 11491 | { |
| 11492 | if (name == complete_ctor_identifier) |
| 11493 | return integer_one_node; |
| 11494 | gcc_checking_assert (name == base_ctor_identifier); |
| 11495 | } |
| 11496 | else |
| 11497 | { |
| 11498 | if (name == complete_dtor_identifier) |
| 11499 | return integer_two_node; |
| 11500 | else if (name == deleting_dtor_identifier) |
| 11501 | /* The deleting dtor should now be handled by |
| 11502 | build_delete_destructor_body. */ |
| 11503 | gcc_unreachable (); |
| 11504 | gcc_checking_assert (name == base_dtor_identifier); |
| 11505 | } |
| 11506 | |
| 11507 | return integer_zero_node; |
| 11508 | } |
| 11509 | |
| 11510 | /* We've built up a constructor call RET. Complain if it delegates to the |
| 11511 | constructor we're currently compiling. */ |
| 11512 | |
| 11513 | static void |
| 11514 | check_self_delegation (tree ret) |
| 11515 | { |
| 11516 | if (TREE_CODE (ret) == TARGET_EXPR) |
| 11517 | ret = TARGET_EXPR_INITIAL (ret); |
| 11518 | tree fn = cp_get_callee_fndecl_nofold (ret); |
| 11519 | if (fn && DECL_ABSTRACT_ORIGIN (fn) == current_function_decl) |
| 11520 | error ("constructor delegates to itself" ); |
| 11521 | } |
| 11522 | |
| 11523 | /* Build a call to a constructor, destructor, or an assignment |
| 11524 | operator for INSTANCE, an expression with class type. NAME |
| 11525 | indicates the special member function to call; *ARGS are the |
| 11526 | arguments. ARGS may be NULL. This may change ARGS. BINFO |
| 11527 | indicates the base of INSTANCE that is to be passed as the `this' |
| 11528 | parameter to the member function called. |
| 11529 | |
| 11530 | FLAGS are the LOOKUP_* flags to use when processing the call. |
| 11531 | |
| 11532 | If NAME indicates a complete object constructor, INSTANCE may be |
| 11533 | NULL_TREE. In this case, the caller will call build_cplus_new to |
| 11534 | store the newly constructed object into a VAR_DECL. */ |
| 11535 | |
| 11536 | tree |
| 11537 | build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args, |
| 11538 | tree binfo, int flags, tsubst_flags_t complain) |
| 11539 | { |
| 11540 | tree fns; |
| 11541 | /* The type of the subobject to be constructed or destroyed. */ |
| 11542 | tree class_type; |
| 11543 | vec<tree, va_gc> *allocated = NULL; |
| 11544 | tree ret; |
| 11545 | |
| 11546 | gcc_assert (IDENTIFIER_CDTOR_P (name) || name == assign_op_identifier); |
| 11547 | |
| 11548 | if (error_operand_p (t: instance)) |
| 11549 | return error_mark_node; |
| 11550 | |
| 11551 | if (IDENTIFIER_DTOR_P (name)) |
| 11552 | { |
| 11553 | gcc_assert (args == NULL || vec_safe_is_empty (*args)); |
| 11554 | if (!type_build_dtor_call (TREE_TYPE (instance))) |
| 11555 | /* Shortcut to avoid lazy destructor declaration. */ |
| 11556 | return build_trivial_dtor_call (instance); |
| 11557 | } |
| 11558 | |
| 11559 | if (TYPE_P (binfo)) |
| 11560 | { |
| 11561 | /* Resolve the name. */ |
| 11562 | if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain)) |
| 11563 | return error_mark_node; |
| 11564 | |
| 11565 | binfo = TYPE_BINFO (binfo); |
| 11566 | } |
| 11567 | |
| 11568 | gcc_assert (binfo != NULL_TREE); |
| 11569 | |
| 11570 | class_type = BINFO_TYPE (binfo); |
| 11571 | |
| 11572 | /* Handle the special case where INSTANCE is NULL_TREE. */ |
| 11573 | if (name == complete_ctor_identifier && !instance) |
| 11574 | instance = build_dummy_object (class_type); |
| 11575 | else |
| 11576 | { |
| 11577 | /* Convert to the base class, if necessary. */ |
| 11578 | if (!same_type_ignoring_top_level_qualifiers_p |
| 11579 | (TREE_TYPE (instance), BINFO_TYPE (binfo))) |
| 11580 | { |
| 11581 | if (IDENTIFIER_CDTOR_P (name)) |
| 11582 | /* For constructors and destructors, either the base is |
| 11583 | non-virtual, or it is virtual but we are doing the |
| 11584 | conversion from a constructor or destructor for the |
| 11585 | complete object. In either case, we can convert |
| 11586 | statically. */ |
| 11587 | instance = convert_to_base_statically (instance, binfo); |
| 11588 | else |
| 11589 | { |
| 11590 | /* However, for assignment operators, we must convert |
| 11591 | dynamically if the base is virtual. */ |
| 11592 | gcc_checking_assert (name == assign_op_identifier); |
| 11593 | instance = build_base_path (PLUS_EXPR, instance, |
| 11594 | binfo, /*nonnull=*/1, complain); |
| 11595 | } |
| 11596 | } |
| 11597 | } |
| 11598 | |
| 11599 | gcc_assert (instance != NULL_TREE); |
| 11600 | |
| 11601 | /* In C++17, "If the initializer expression is a prvalue and the |
| 11602 | cv-unqualified version of the source type is the same class as the class |
| 11603 | of the destination, the initializer expression is used to initialize the |
| 11604 | destination object." Handle that here to avoid doing overload |
| 11605 | resolution. */ |
| 11606 | if (cxx_dialect >= cxx17 |
| 11607 | && args && vec_safe_length (v: *args) == 1 |
| 11608 | && !unsafe_return_slot_p (t: instance)) |
| 11609 | { |
| 11610 | tree arg = (**args)[0]; |
| 11611 | |
| 11612 | if (BRACE_ENCLOSED_INITIALIZER_P (arg) |
| 11613 | && !TYPE_HAS_LIST_CTOR (class_type) |
| 11614 | && !CONSTRUCTOR_IS_DESIGNATED_INIT (arg) |
| 11615 | && CONSTRUCTOR_NELTS (arg) == 1) |
| 11616 | arg = CONSTRUCTOR_ELT (arg, 0)->value; |
| 11617 | |
| 11618 | if ((TREE_CODE (arg) == TARGET_EXPR |
| 11619 | || TREE_CODE (arg) == CONSTRUCTOR) |
| 11620 | && (same_type_ignoring_top_level_qualifiers_p |
| 11621 | (class_type, TREE_TYPE (arg)))) |
| 11622 | { |
| 11623 | if (is_dummy_object (instance)) |
| 11624 | return arg; |
| 11625 | else if (TREE_CODE (arg) == TARGET_EXPR) |
| 11626 | TARGET_EXPR_DIRECT_INIT_P (arg) = true; |
| 11627 | |
| 11628 | if ((complain & tf_error) |
| 11629 | && (flags & LOOKUP_DELEGATING_CONS)) |
| 11630 | check_self_delegation (ret: arg); |
| 11631 | /* Avoid change of behavior on Wunused-var-2.C. */ |
| 11632 | instance = mark_lvalue_use (instance); |
| 11633 | return cp_build_init_expr (t: instance, i: arg); |
| 11634 | } |
| 11635 | } |
| 11636 | |
| 11637 | fns = lookup_fnfields (binfo, name, 1, complain); |
| 11638 | |
| 11639 | /* When making a call to a constructor or destructor for a subobject |
| 11640 | that uses virtual base classes, pass down a pointer to a VTT for |
| 11641 | the subobject. */ |
| 11642 | if ((name == base_ctor_identifier |
| 11643 | || name == base_dtor_identifier) |
| 11644 | && CLASSTYPE_VBASECLASSES (class_type)) |
| 11645 | { |
| 11646 | tree vtt; |
| 11647 | tree sub_vtt; |
| 11648 | |
| 11649 | /* If the current function is a complete object constructor |
| 11650 | or destructor, then we fetch the VTT directly. |
| 11651 | Otherwise, we look it up using the VTT we were given. */ |
| 11652 | vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type)); |
| 11653 | vtt = decay_conversion (vtt, complain); |
| 11654 | if (vtt == error_mark_node) |
| 11655 | return error_mark_node; |
| 11656 | vtt = build_if_in_charge (true_stmt: vtt, current_vtt_parm); |
| 11657 | if (BINFO_SUBVTT_INDEX (binfo)) |
| 11658 | sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo)); |
| 11659 | else |
| 11660 | sub_vtt = vtt; |
| 11661 | |
| 11662 | if (args == NULL) |
| 11663 | { |
| 11664 | allocated = make_tree_vector (); |
| 11665 | args = &allocated; |
| 11666 | } |
| 11667 | |
| 11668 | vec_safe_insert (v&: *args, ix: 0, obj: sub_vtt); |
| 11669 | } |
| 11670 | |
| 11671 | ret = build_new_method_call (instance, fns, args, |
| 11672 | TYPE_BINFO (BINFO_TYPE (binfo)), |
| 11673 | flags, /*fn=*/NULL, |
| 11674 | complain); |
| 11675 | |
| 11676 | if (allocated != NULL) |
| 11677 | release_tree_vector (allocated); |
| 11678 | |
| 11679 | if ((complain & tf_error) |
| 11680 | && (flags & LOOKUP_DELEGATING_CONS) |
| 11681 | && name == complete_ctor_identifier) |
| 11682 | check_self_delegation (ret); |
| 11683 | |
| 11684 | return ret; |
| 11685 | } |
| 11686 | |
| 11687 | /* Return the NAME, as a C string. The NAME indicates a function that |
| 11688 | is a member of TYPE. *FREE_P is set to true if the caller must |
| 11689 | free the memory returned. |
| 11690 | |
| 11691 | Rather than go through all of this, we should simply set the names |
| 11692 | of constructors and destructors appropriately, and dispense with |
| 11693 | ctor_identifier, dtor_identifier, etc. */ |
| 11694 | |
| 11695 | static char * |
| 11696 | name_as_c_string (tree name, tree type, bool *free_p) |
| 11697 | { |
| 11698 | const char *pretty_name; |
| 11699 | |
| 11700 | /* Assume that we will not allocate memory. */ |
| 11701 | *free_p = false; |
| 11702 | /* Constructors and destructors are special. */ |
| 11703 | if (IDENTIFIER_CDTOR_P (name)) |
| 11704 | { |
| 11705 | pretty_name |
| 11706 | = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type))); |
| 11707 | /* For a destructor, add the '~'. */ |
| 11708 | if (IDENTIFIER_DTOR_P (name)) |
| 11709 | { |
| 11710 | pretty_name = concat ("~" , pretty_name, NULL); |
| 11711 | /* Remember that we need to free the memory allocated. */ |
| 11712 | *free_p = true; |
| 11713 | } |
| 11714 | } |
| 11715 | else if (IDENTIFIER_CONV_OP_P (name)) |
| 11716 | { |
| 11717 | pretty_name = concat ("operator " , |
| 11718 | type_as_string_translate (TREE_TYPE (name), |
| 11719 | TFF_PLAIN_IDENTIFIER), |
| 11720 | NULL); |
| 11721 | /* Remember that we need to free the memory allocated. */ |
| 11722 | *free_p = true; |
| 11723 | } |
| 11724 | else |
| 11725 | pretty_name = identifier_to_locale (IDENTIFIER_POINTER (name)); |
| 11726 | |
| 11727 | return CONST_CAST (char *, pretty_name); |
| 11728 | } |
| 11729 | |
| 11730 | /* If CANDIDATES contains exactly one candidate, return it, otherwise |
| 11731 | return NULL. */ |
| 11732 | |
| 11733 | static z_candidate * |
| 11734 | single_z_candidate (z_candidate *candidates) |
| 11735 | { |
| 11736 | if (candidates == NULL) |
| 11737 | return NULL; |
| 11738 | |
| 11739 | if (candidates->next) |
| 11740 | return NULL; |
| 11741 | |
| 11742 | return candidates; |
| 11743 | } |
| 11744 | |
| 11745 | /* If CANDIDATE is invalid due to a bad argument type, return the |
| 11746 | pertinent conversion_info. |
| 11747 | |
| 11748 | Otherwise, return NULL. */ |
| 11749 | |
| 11750 | static const conversion_info * |
| 11751 | maybe_get_bad_conversion_for_unmatched_call (const z_candidate *candidate) |
| 11752 | { |
| 11753 | /* Must be an rr_arg_conversion or rr_bad_arg_conversion. */ |
| 11754 | rejection_reason *r = candidate->reason; |
| 11755 | |
| 11756 | if (r == NULL) |
| 11757 | return NULL; |
| 11758 | |
| 11759 | switch (r->code) |
| 11760 | { |
| 11761 | default: |
| 11762 | return NULL; |
| 11763 | |
| 11764 | case rr_arg_conversion: |
| 11765 | return &r->u.conversion; |
| 11766 | |
| 11767 | case rr_bad_arg_conversion: |
| 11768 | return &r->u.bad_conversion; |
| 11769 | } |
| 11770 | } |
| 11771 | |
| 11772 | /* Issue an error and note complaining about a bad argument type at a |
| 11773 | callsite with a single candidate FNDECL. |
| 11774 | |
| 11775 | ARG_LOC is the location of the argument (or UNKNOWN_LOCATION, in which |
| 11776 | case input_location is used). |
| 11777 | FROM_TYPE is the type of the actual argument; TO_TYPE is the type of |
| 11778 | the formal parameter. */ |
| 11779 | |
| 11780 | void |
| 11781 | complain_about_bad_argument (location_t arg_loc, |
| 11782 | tree from_type, tree to_type, |
| 11783 | tree fndecl, int parmnum) |
| 11784 | { |
| 11785 | auto_diagnostic_group d; |
| 11786 | range_label_for_type_mismatch rhs_label (from_type, to_type); |
| 11787 | range_label *label = &rhs_label; |
| 11788 | if (arg_loc == UNKNOWN_LOCATION) |
| 11789 | { |
| 11790 | arg_loc = input_location; |
| 11791 | label = NULL; |
| 11792 | } |
| 11793 | gcc_rich_location richloc (arg_loc, label, highlight_colors::percent_h); |
| 11794 | error_at (&richloc, |
| 11795 | "cannot convert %qH to %qI" , |
| 11796 | from_type, to_type); |
| 11797 | maybe_inform_about_fndecl_for_bogus_argument_init |
| 11798 | (fn: fndecl, |
| 11799 | argnum: parmnum, |
| 11800 | highlight_color: highlight_colors::percent_i); |
| 11801 | } |
| 11802 | |
| 11803 | /* Subroutine of build_new_method_call_1, for where there are no viable |
| 11804 | candidates for the call. */ |
| 11805 | |
| 11806 | static void |
| 11807 | complain_about_no_candidates_for_method_call (tree instance, |
| 11808 | z_candidate *candidates, |
| 11809 | tree explicit_targs, |
| 11810 | tree basetype, |
| 11811 | tree optype, tree name, |
| 11812 | bool skip_first_for_error, |
| 11813 | vec<tree, va_gc> *user_args) |
| 11814 | { |
| 11815 | auto_diagnostic_group d; |
| 11816 | if (!COMPLETE_OR_OPEN_TYPE_P (basetype)) |
| 11817 | cxx_incomplete_type_error (value: instance, type: basetype); |
| 11818 | else if (optype) |
| 11819 | error ("no matching function for call to %<%T::operator %T(%A)%#V%>" , |
| 11820 | basetype, optype, build_tree_list_vec (user_args), |
| 11821 | TREE_TYPE (instance)); |
| 11822 | else |
| 11823 | { |
| 11824 | /* Special-case for when there's a single candidate that's failing |
| 11825 | due to a bad argument type. */ |
| 11826 | if (z_candidate *candidate = single_z_candidate (candidates)) |
| 11827 | if (const conversion_info *conv |
| 11828 | = maybe_get_bad_conversion_for_unmatched_call (candidate)) |
| 11829 | { |
| 11830 | tree from_type = conv->from; |
| 11831 | if (!TYPE_P (conv->from)) |
| 11832 | from_type = lvalue_type (conv->from); |
| 11833 | complain_about_bad_argument (arg_loc: conv->loc, |
| 11834 | from_type, to_type: conv->to_type, |
| 11835 | fndecl: candidate->fn, parmnum: conv->n_arg); |
| 11836 | return; |
| 11837 | } |
| 11838 | |
| 11839 | tree arglist = build_tree_list_vec (user_args); |
| 11840 | tree errname = name; |
| 11841 | bool twiddle = false; |
| 11842 | if (IDENTIFIER_CDTOR_P (errname)) |
| 11843 | { |
| 11844 | twiddle = IDENTIFIER_DTOR_P (errname); |
| 11845 | errname = constructor_name (basetype); |
| 11846 | } |
| 11847 | if (explicit_targs) |
| 11848 | errname = lookup_template_function (errname, explicit_targs); |
| 11849 | if (skip_first_for_error) |
| 11850 | arglist = TREE_CHAIN (arglist); |
| 11851 | error ("no matching function for call to %<%T::%s%E(%A)%#V%>" , |
| 11852 | basetype, &"~" [!twiddle], errname, arglist, |
| 11853 | TREE_TYPE (instance)); |
| 11854 | } |
| 11855 | print_z_candidates (loc: location_of (name), candidates); |
| 11856 | } |
| 11857 | |
| 11858 | /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will |
| 11859 | be set, upon return, to the function called. ARGS may be NULL. |
| 11860 | This may change ARGS. */ |
| 11861 | |
| 11862 | tree |
| 11863 | build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args, |
| 11864 | tree conversion_path, int flags, |
| 11865 | tree *fn_p, tsubst_flags_t complain) |
| 11866 | { |
| 11867 | struct z_candidate *candidates = 0, *cand; |
| 11868 | tree explicit_targs = NULL_TREE; |
| 11869 | tree basetype = NULL_TREE; |
| 11870 | tree access_binfo; |
| 11871 | tree optype; |
| 11872 | tree first_mem_arg = NULL_TREE; |
| 11873 | tree name; |
| 11874 | bool skip_first_for_error; |
| 11875 | vec<tree, va_gc> *user_args; |
| 11876 | tree call; |
| 11877 | tree fn; |
| 11878 | int template_only = 0; |
| 11879 | bool any_viable_p; |
| 11880 | tree orig_instance; |
| 11881 | tree orig_fns; |
| 11882 | vec<tree, va_gc> *orig_args = NULL; |
| 11883 | |
| 11884 | auto_cond_timevar tv (TV_OVERLOAD); |
| 11885 | |
| 11886 | gcc_assert (instance != NULL_TREE); |
| 11887 | |
| 11888 | /* We don't know what function we're going to call, yet. */ |
| 11889 | if (fn_p) |
| 11890 | *fn_p = NULL_TREE; |
| 11891 | |
| 11892 | if (error_operand_p (t: instance) |
| 11893 | || !fns || error_operand_p (t: fns)) |
| 11894 | return error_mark_node; |
| 11895 | |
| 11896 | if (!BASELINK_P (fns)) |
| 11897 | { |
| 11898 | if (complain & tf_error) |
| 11899 | error ("call to non-function %qD" , fns); |
| 11900 | return error_mark_node; |
| 11901 | } |
| 11902 | |
| 11903 | orig_instance = instance; |
| 11904 | orig_fns = fns; |
| 11905 | |
| 11906 | /* Dismantle the baselink to collect all the information we need. */ |
| 11907 | if (!conversion_path) |
| 11908 | conversion_path = BASELINK_BINFO (fns); |
| 11909 | access_binfo = BASELINK_ACCESS_BINFO (fns); |
| 11910 | optype = BASELINK_OPTYPE (fns); |
| 11911 | fns = BASELINK_FUNCTIONS (fns); |
| 11912 | if (TREE_CODE (fns) == TEMPLATE_ID_EXPR) |
| 11913 | { |
| 11914 | explicit_targs = TREE_OPERAND (fns, 1); |
| 11915 | fns = TREE_OPERAND (fns, 0); |
| 11916 | template_only = 1; |
| 11917 | } |
| 11918 | gcc_assert (OVL_P (fns)); |
| 11919 | fn = OVL_FIRST (fns); |
| 11920 | name = DECL_NAME (fn); |
| 11921 | |
| 11922 | basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance)); |
| 11923 | gcc_assert (CLASS_TYPE_P (basetype)); |
| 11924 | |
| 11925 | user_args = args == NULL ? NULL : *args; |
| 11926 | /* Under DR 147 A::A() is an invalid constructor call, |
| 11927 | not a functional cast. */ |
| 11928 | if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)) |
| 11929 | { |
| 11930 | if (! (complain & tf_error)) |
| 11931 | return error_mark_node; |
| 11932 | |
| 11933 | basetype = DECL_CONTEXT (fn); |
| 11934 | name = constructor_name (basetype); |
| 11935 | auto_diagnostic_group d; |
| 11936 | if (permerror (input_location, |
| 11937 | "cannot call constructor %<%T::%D%> directly" , |
| 11938 | basetype, name)) |
| 11939 | inform (input_location, "for a function-style cast, remove the " |
| 11940 | "redundant %<::%D%>" , name); |
| 11941 | call = build_functional_cast (input_location, basetype, |
| 11942 | build_tree_list_vec (user_args), |
| 11943 | complain); |
| 11944 | return call; |
| 11945 | } |
| 11946 | |
| 11947 | if (processing_template_decl) |
| 11948 | orig_args = args == NULL ? NULL : make_tree_vector_copy (*args); |
| 11949 | |
| 11950 | /* Process the argument list. */ |
| 11951 | if (args != NULL && *args != NULL) |
| 11952 | { |
| 11953 | *args = resolve_args (args: *args, complain); |
| 11954 | if (*args == NULL) |
| 11955 | return error_mark_node; |
| 11956 | user_args = *args; |
| 11957 | } |
| 11958 | |
| 11959 | /* Consider the object argument to be used even if we end up selecting a |
| 11960 | static member function. */ |
| 11961 | instance = mark_type_use (instance); |
| 11962 | |
| 11963 | /* Figure out whether to skip the first argument for the error |
| 11964 | message we will display to users if an error occurs. We don't |
| 11965 | want to display any compiler-generated arguments. The "this" |
| 11966 | pointer hasn't been added yet. However, we must remove the VTT |
| 11967 | pointer if this is a call to a base-class constructor or |
| 11968 | destructor. */ |
| 11969 | skip_first_for_error = false; |
| 11970 | if (IDENTIFIER_CDTOR_P (name)) |
| 11971 | { |
| 11972 | /* Callers should explicitly indicate whether they want to ctor |
| 11973 | the complete object or just the part without virtual bases. */ |
| 11974 | gcc_assert (name != ctor_identifier); |
| 11975 | |
| 11976 | /* Remove the VTT pointer, if present. */ |
| 11977 | if ((name == base_ctor_identifier || name == base_dtor_identifier) |
| 11978 | && CLASSTYPE_VBASECLASSES (basetype)) |
| 11979 | skip_first_for_error = true; |
| 11980 | |
| 11981 | /* It's OK to call destructors and constructors on cv-qualified |
| 11982 | objects. Therefore, convert the INSTANCE to the unqualified |
| 11983 | type, if necessary. */ |
| 11984 | if (!same_type_p (basetype, TREE_TYPE (instance))) |
| 11985 | { |
| 11986 | instance = build_this (obj: instance); |
| 11987 | instance = build_nop (build_pointer_type (basetype), instance); |
| 11988 | instance = build_fold_indirect_ref (instance); |
| 11989 | } |
| 11990 | } |
| 11991 | else |
| 11992 | gcc_assert (!DECL_DESTRUCTOR_P (fn) && !DECL_CONSTRUCTOR_P (fn)); |
| 11993 | |
| 11994 | /* For the overload resolution we need to find the actual `this` |
| 11995 | that would be captured if the call turns out to be to a |
| 11996 | non-static member function. Do not actually capture it at this |
| 11997 | point. */ |
| 11998 | if (DECL_CONSTRUCTOR_P (fn)) |
| 11999 | /* Constructors don't use the enclosing 'this'. */ |
| 12000 | first_mem_arg = instance; |
| 12001 | else |
| 12002 | first_mem_arg = maybe_resolve_dummy (instance, false); |
| 12003 | |
| 12004 | conversion_obstack_sentinel cos; |
| 12005 | |
| 12006 | /* The number of arguments artificial parms in ARGS; we subtract one because |
| 12007 | there's no 'this' in ARGS. */ |
| 12008 | unsigned skip = num_artificial_parms_for (fn) - 1; |
| 12009 | |
| 12010 | /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form |
| 12011 | initializer, not T({ }). */ |
| 12012 | if (DECL_CONSTRUCTOR_P (fn) |
| 12013 | && vec_safe_length (v: user_args) > skip |
| 12014 | && DIRECT_LIST_INIT_P ((*user_args)[skip])) |
| 12015 | { |
| 12016 | tree init_list = (*user_args)[skip]; |
| 12017 | tree init = NULL_TREE; |
| 12018 | |
| 12019 | gcc_assert (user_args->length () == skip + 1 |
| 12020 | && !(flags & LOOKUP_ONLYCONVERTING)); |
| 12021 | |
| 12022 | /* If the initializer list has no elements and T is a class type with |
| 12023 | a default constructor, the object is value-initialized. Handle |
| 12024 | this here so we don't need to handle it wherever we use |
| 12025 | build_special_member_call. */ |
| 12026 | if (CONSTRUCTOR_NELTS (init_list) == 0 |
| 12027 | && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype) |
| 12028 | /* For a user-provided default constructor, use the normal |
| 12029 | mechanisms so that protected access works. */ |
| 12030 | && type_has_non_user_provided_default_constructor (basetype) |
| 12031 | && !processing_template_decl) |
| 12032 | init = build_value_init (basetype, complain); |
| 12033 | |
| 12034 | /* If BASETYPE is an aggregate, we need to do aggregate |
| 12035 | initialization. */ |
| 12036 | else if (CP_AGGREGATE_TYPE_P (basetype)) |
| 12037 | { |
| 12038 | init = reshape_init (basetype, init_list, complain); |
| 12039 | init = digest_init (basetype, init, complain); |
| 12040 | } |
| 12041 | |
| 12042 | if (init) |
| 12043 | { |
| 12044 | if (is_dummy_object (instance)) |
| 12045 | return get_target_expr (init, complain); |
| 12046 | return cp_build_init_expr (t: instance, i: init); |
| 12047 | } |
| 12048 | |
| 12049 | /* Otherwise go ahead with overload resolution. */ |
| 12050 | add_list_candidates (fns, first_arg: first_mem_arg, args: user_args, |
| 12051 | totype: basetype, explicit_targs, template_only, |
| 12052 | conversion_path, access_path: access_binfo, flags, |
| 12053 | candidates: &candidates, complain); |
| 12054 | } |
| 12055 | else |
| 12056 | add_candidates (fns, first_arg: first_mem_arg, args: user_args, return_type: optype, |
| 12057 | explicit_targs, template_only, conversion_path, |
| 12058 | access_path: access_binfo, flags, candidates: &candidates, complain); |
| 12059 | |
| 12060 | any_viable_p = false; |
| 12061 | candidates = splice_viable (cands: candidates, strict_p: false, any_viable_p: &any_viable_p); |
| 12062 | |
| 12063 | if (!any_viable_p) |
| 12064 | { |
| 12065 | /* [dcl.init], 17.6.2.2: |
| 12066 | |
| 12067 | Otherwise, if no constructor is viable, the destination type is |
| 12068 | a (possibly cv-qualified) aggregate class A, and the initializer |
| 12069 | is a parenthesized expression-list, the object is initialized as |
| 12070 | follows... |
| 12071 | |
| 12072 | We achieve this by building up a CONSTRUCTOR, as for list-init, |
| 12073 | and setting CONSTRUCTOR_IS_PAREN_INIT to distinguish between |
| 12074 | the two. */ |
| 12075 | if (DECL_CONSTRUCTOR_P (fn) |
| 12076 | && !(flags & LOOKUP_ONLYCONVERTING) |
| 12077 | && cxx_dialect >= cxx20 |
| 12078 | && CP_AGGREGATE_TYPE_P (basetype) |
| 12079 | && !vec_safe_is_empty (v: user_args)) |
| 12080 | { |
| 12081 | /* Create a CONSTRUCTOR from ARGS, e.g. {1, 2} from <1, 2>. */ |
| 12082 | tree ctor = build_constructor_from_vec (init_list_type_node, |
| 12083 | user_args); |
| 12084 | CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true; |
| 12085 | CONSTRUCTOR_IS_PAREN_INIT (ctor) = true; |
| 12086 | if (is_dummy_object (instance)) |
| 12087 | return ctor; |
| 12088 | else |
| 12089 | { |
| 12090 | ctor = digest_init (basetype, ctor, complain); |
| 12091 | if (ctor == error_mark_node) |
| 12092 | return error_mark_node; |
| 12093 | return cp_build_init_expr (t: instance, i: ctor); |
| 12094 | } |
| 12095 | } |
| 12096 | if (complain & tf_error) |
| 12097 | complain_about_no_candidates_for_method_call (instance, candidates, |
| 12098 | explicit_targs, basetype, |
| 12099 | optype, name, |
| 12100 | skip_first_for_error, |
| 12101 | user_args); |
| 12102 | call = error_mark_node; |
| 12103 | } |
| 12104 | else |
| 12105 | { |
| 12106 | cand = tourney (candidates, complain); |
| 12107 | if (cand == 0) |
| 12108 | { |
| 12109 | char *pretty_name; |
| 12110 | bool free_p; |
| 12111 | tree arglist; |
| 12112 | |
| 12113 | if (complain & tf_error) |
| 12114 | { |
| 12115 | pretty_name = name_as_c_string (name, type: basetype, free_p: &free_p); |
| 12116 | arglist = build_tree_list_vec (user_args); |
| 12117 | if (skip_first_for_error) |
| 12118 | arglist = TREE_CHAIN (arglist); |
| 12119 | auto_diagnostic_group d; |
| 12120 | if (!any_strictly_viable (cands: candidates)) |
| 12121 | error ("no matching function for call to %<%s(%A)%>" , |
| 12122 | pretty_name, arglist); |
| 12123 | else |
| 12124 | error ("call of overloaded %<%s(%A)%> is ambiguous" , |
| 12125 | pretty_name, arglist); |
| 12126 | print_z_candidates (loc: location_of (name), candidates); |
| 12127 | if (free_p) |
| 12128 | free (ptr: pretty_name); |
| 12129 | } |
| 12130 | call = error_mark_node; |
| 12131 | if (fn_p) |
| 12132 | *fn_p = error_mark_node; |
| 12133 | } |
| 12134 | else |
| 12135 | { |
| 12136 | fn = cand->fn; |
| 12137 | call = NULL_TREE; |
| 12138 | |
| 12139 | if (!(flags & LOOKUP_NONVIRTUAL) |
| 12140 | && DECL_PURE_VIRTUAL_P (fn) |
| 12141 | && instance == current_class_ref |
| 12142 | && (complain & tf_warning)) |
| 12143 | { |
| 12144 | /* This is not an error, it is runtime undefined |
| 12145 | behavior. */ |
| 12146 | if (!current_function_decl) |
| 12147 | warning (0, "pure virtual %q#D called from " |
| 12148 | "non-static data member initializer" , fn); |
| 12149 | else if (DECL_CONSTRUCTOR_P (current_function_decl) |
| 12150 | || DECL_DESTRUCTOR_P (current_function_decl)) |
| 12151 | warning (0, (DECL_CONSTRUCTOR_P (current_function_decl) |
| 12152 | ? G_("pure virtual %q#D called from constructor" ) |
| 12153 | : G_("pure virtual %q#D called from destructor" )), |
| 12154 | fn); |
| 12155 | } |
| 12156 | |
| 12157 | if (DECL_OBJECT_MEMBER_FUNCTION_P (fn) |
| 12158 | && !DECL_CONSTRUCTOR_P (fn) |
| 12159 | && is_dummy_object (instance)) |
| 12160 | { |
| 12161 | instance = maybe_resolve_dummy (instance, true); |
| 12162 | if (instance == error_mark_node) |
| 12163 | call = error_mark_node; |
| 12164 | else if (!is_dummy_object (instance)) |
| 12165 | { |
| 12166 | /* We captured 'this' in the current lambda now that |
| 12167 | we know we really need it. */ |
| 12168 | cand->first_arg = instance; |
| 12169 | } |
| 12170 | else if (current_class_ptr && any_dependent_bases_p ()) |
| 12171 | /* We can't tell until instantiation time whether we can use |
| 12172 | *this as the implicit object argument. */; |
| 12173 | else |
| 12174 | { |
| 12175 | if (complain & tf_error) |
| 12176 | error ("cannot call member function %qD without object" , |
| 12177 | fn); |
| 12178 | call = error_mark_node; |
| 12179 | } |
| 12180 | } |
| 12181 | |
| 12182 | if (call != error_mark_node) |
| 12183 | { |
| 12184 | /* Now we know what function is being called. */ |
| 12185 | if (fn_p) |
| 12186 | *fn_p = fn; |
| 12187 | /* Build the actual CALL_EXPR. */ |
| 12188 | call = build_over_call (cand, flags, complain); |
| 12189 | |
| 12190 | /* Suppress warnings for if (my_struct.operator= (x)) where |
| 12191 | my_struct is implicitly converted to bool. */ |
| 12192 | if (TREE_CODE (call) == MODIFY_EXPR) |
| 12193 | suppress_warning (call, OPT_Wparentheses); |
| 12194 | |
| 12195 | /* In an expression of the form `a->f()' where `f' turns |
| 12196 | out to be a static member function, `a' is |
| 12197 | none-the-less evaluated. */ |
| 12198 | if (!is_dummy_object (instance)) |
| 12199 | call = keep_unused_object_arg (result: call, obj: instance, fn); |
| 12200 | if (call != error_mark_node |
| 12201 | && DECL_DESTRUCTOR_P (cand->fn) |
| 12202 | && !VOID_TYPE_P (TREE_TYPE (call))) |
| 12203 | /* An explicit call of the form "x->~X()" has type |
| 12204 | "void". However, on platforms where destructors |
| 12205 | return "this" (i.e., those where |
| 12206 | targetm.cxx.cdtor_returns_this is true), such calls |
| 12207 | will appear to have a return value of pointer type |
| 12208 | to the low-level call machinery. We do not want to |
| 12209 | change the low-level machinery, since we want to be |
| 12210 | able to optimize "delete f()" on such platforms as |
| 12211 | "operator delete(~X(f()))" (rather than generating |
| 12212 | "t = f(), ~X(t), operator delete (t)"). */ |
| 12213 | call = build_nop (void_type_node, call); |
| 12214 | } |
| 12215 | } |
| 12216 | } |
| 12217 | |
| 12218 | if (processing_template_decl && call != error_mark_node) |
| 12219 | { |
| 12220 | bool cast_to_void = false; |
| 12221 | |
| 12222 | if (TREE_CODE (call) == COMPOUND_EXPR) |
| 12223 | call = TREE_OPERAND (call, 1); |
| 12224 | else if (TREE_CODE (call) == NOP_EXPR) |
| 12225 | { |
| 12226 | cast_to_void = true; |
| 12227 | call = TREE_OPERAND (call, 0); |
| 12228 | } |
| 12229 | if (INDIRECT_REF_P (call)) |
| 12230 | call = TREE_OPERAND (call, 0); |
| 12231 | |
| 12232 | /* Prune all but the selected function from the original overload |
| 12233 | set so that we can avoid some duplicate work at instantiation time. */ |
| 12234 | if (really_overloaded_fn (fns)) |
| 12235 | { |
| 12236 | if (DECL_TEMPLATE_INFO (fn) |
| 12237 | && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn))) |
| 12238 | { |
| 12239 | /* Use the selected template, not the specialization, so that |
| 12240 | this looks like an actual lookup result for sake of |
| 12241 | filter_memfn_lookup. */ |
| 12242 | |
| 12243 | if (OVL_SINGLE_P (fns)) |
| 12244 | /* If the original overload set consists of a single function |
| 12245 | template, this isn't beneficial. */ |
| 12246 | goto skip_prune; |
| 12247 | |
| 12248 | fn = ovl_make (DECL_TI_TEMPLATE (fn)); |
| 12249 | if (template_only) |
| 12250 | fn = lookup_template_function (fn, explicit_targs); |
| 12251 | } |
| 12252 | orig_fns = copy_node (orig_fns); |
| 12253 | BASELINK_FUNCTIONS (orig_fns) = fn; |
| 12254 | BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (orig_fns) = true; |
| 12255 | } |
| 12256 | |
| 12257 | skip_prune: |
| 12258 | call = (build_min_non_dep_call_vec |
| 12259 | (call, |
| 12260 | build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)), |
| 12261 | orig_instance, orig_fns, NULL_TREE), |
| 12262 | orig_args)); |
| 12263 | SET_EXPR_LOCATION (call, input_location); |
| 12264 | call = convert_from_reference (call); |
| 12265 | if (cast_to_void) |
| 12266 | call = build_nop (void_type_node, call); |
| 12267 | } |
| 12268 | |
| 12269 | if (orig_args != NULL) |
| 12270 | release_tree_vector (orig_args); |
| 12271 | |
| 12272 | return call; |
| 12273 | } |
| 12274 | |
| 12275 | /* Returns true iff standard conversion sequence ICS1 is a proper |
| 12276 | subsequence of ICS2. */ |
| 12277 | |
| 12278 | static bool |
| 12279 | is_subseq (conversion *ics1, conversion *ics2) |
| 12280 | { |
| 12281 | /* We can assume that a conversion of the same code |
| 12282 | between the same types indicates a subsequence since we only get |
| 12283 | here if the types we are converting from are the same. */ |
| 12284 | |
| 12285 | while (ics1->kind == ck_rvalue |
| 12286 | || ics1->kind == ck_lvalue) |
| 12287 | ics1 = next_conversion (conv: ics1); |
| 12288 | |
| 12289 | while (1) |
| 12290 | { |
| 12291 | while (ics2->kind == ck_rvalue |
| 12292 | || ics2->kind == ck_lvalue) |
| 12293 | ics2 = next_conversion (conv: ics2); |
| 12294 | |
| 12295 | if (ics2->kind == ck_user |
| 12296 | || !has_next (code: ics2->kind)) |
| 12297 | /* At this point, ICS1 cannot be a proper subsequence of |
| 12298 | ICS2. We can get a USER_CONV when we are comparing the |
| 12299 | second standard conversion sequence of two user conversion |
| 12300 | sequences. */ |
| 12301 | return false; |
| 12302 | |
| 12303 | ics2 = next_conversion (conv: ics2); |
| 12304 | |
| 12305 | while (ics2->kind == ck_rvalue |
| 12306 | || ics2->kind == ck_lvalue) |
| 12307 | ics2 = next_conversion (conv: ics2); |
| 12308 | |
| 12309 | if (ics2->kind == ics1->kind |
| 12310 | && same_type_p (ics2->type, ics1->type) |
| 12311 | && (ics1->kind == ck_identity |
| 12312 | || same_type_p (next_conversion (ics2)->type, |
| 12313 | next_conversion (ics1)->type))) |
| 12314 | return true; |
| 12315 | } |
| 12316 | } |
| 12317 | |
| 12318 | /* Returns nonzero iff DERIVED is derived from BASE. The inputs may |
| 12319 | be any _TYPE nodes. */ |
| 12320 | |
| 12321 | bool |
| 12322 | is_properly_derived_from (tree derived, tree base) |
| 12323 | { |
| 12324 | if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base)) |
| 12325 | return false; |
| 12326 | |
| 12327 | /* We only allow proper derivation here. The DERIVED_FROM_P macro |
| 12328 | considers every class derived from itself. */ |
| 12329 | return (!same_type_ignoring_top_level_qualifiers_p (derived, base) |
| 12330 | && DERIVED_FROM_P (base, derived)); |
| 12331 | } |
| 12332 | |
| 12333 | /* We build the ICS for an implicit object parameter as a pointer |
| 12334 | conversion sequence. However, such a sequence should be compared |
| 12335 | as if it were a reference conversion sequence. If ICS is the |
| 12336 | implicit conversion sequence for an implicit object parameter, |
| 12337 | modify it accordingly. */ |
| 12338 | |
| 12339 | static void |
| 12340 | maybe_handle_implicit_object (conversion **ics) |
| 12341 | { |
| 12342 | if ((*ics)->this_p) |
| 12343 | { |
| 12344 | /* [over.match.funcs] |
| 12345 | |
| 12346 | For non-static member functions, the type of the |
| 12347 | implicit object parameter is "reference to cv X" |
| 12348 | where X is the class of which the function is a |
| 12349 | member and cv is the cv-qualification on the member |
| 12350 | function declaration. */ |
| 12351 | conversion *t = *ics; |
| 12352 | tree reference_type; |
| 12353 | |
| 12354 | /* The `this' parameter is a pointer to a class type. Make the |
| 12355 | implicit conversion talk about a reference to that same class |
| 12356 | type. */ |
| 12357 | reference_type = TREE_TYPE (t->type); |
| 12358 | reference_type = build_reference_type (reference_type); |
| 12359 | |
| 12360 | if (t->kind == ck_qual) |
| 12361 | t = next_conversion (conv: t); |
| 12362 | if (t->kind == ck_ptr) |
| 12363 | t = next_conversion (conv: t); |
| 12364 | t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE); |
| 12365 | t = direct_reference_binding (type: reference_type, conv: t); |
| 12366 | t->this_p = 1; |
| 12367 | t->rvaluedness_matches_p = 0; |
| 12368 | *ics = t; |
| 12369 | } |
| 12370 | } |
| 12371 | |
| 12372 | /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion, |
| 12373 | and return the initial reference binding conversion. Otherwise, |
| 12374 | leave *ICS unchanged and return NULL. */ |
| 12375 | |
| 12376 | static conversion * |
| 12377 | maybe_handle_ref_bind (conversion **ics) |
| 12378 | { |
| 12379 | if ((*ics)->kind == ck_ref_bind) |
| 12380 | { |
| 12381 | conversion *old_ics = *ics; |
| 12382 | *ics = next_conversion (conv: old_ics); |
| 12383 | (*ics)->user_conv_p = old_ics->user_conv_p; |
| 12384 | return old_ics; |
| 12385 | } |
| 12386 | |
| 12387 | return NULL; |
| 12388 | } |
| 12389 | |
| 12390 | /* Get the expression at the beginning of the conversion chain C. */ |
| 12391 | |
| 12392 | static tree |
| 12393 | conv_get_original_expr (conversion *c) |
| 12394 | { |
| 12395 | for (; c; c = next_conversion (conv: c)) |
| 12396 | if (c->kind == ck_identity || c->kind == ck_ambig || c->kind == ck_aggr) |
| 12397 | return c->u.expr; |
| 12398 | return NULL_TREE; |
| 12399 | } |
| 12400 | |
| 12401 | /* Return a tree representing the number of elements initialized by the |
| 12402 | list-initialization C. The caller must check that C converts to an |
| 12403 | array type. */ |
| 12404 | |
| 12405 | static tree |
| 12406 | nelts_initialized_by_list_init (conversion *c) |
| 12407 | { |
| 12408 | /* If the array we're converting to has a dimension, we'll use that. */ |
| 12409 | if (TYPE_DOMAIN (c->type)) |
| 12410 | return array_type_nelts_top (c->type); |
| 12411 | else |
| 12412 | { |
| 12413 | /* Otherwise, we look at how many elements the constructor we're |
| 12414 | initializing from has. */ |
| 12415 | tree ctor = conv_get_original_expr (c); |
| 12416 | return size_int (CONSTRUCTOR_NELTS (ctor)); |
| 12417 | } |
| 12418 | } |
| 12419 | |
| 12420 | /* True iff C is a conversion that binds a reference or a pointer to |
| 12421 | an array of unknown bound. */ |
| 12422 | |
| 12423 | static inline bool |
| 12424 | conv_binds_to_array_of_unknown_bound (conversion *c) |
| 12425 | { |
| 12426 | /* ck_ref_bind won't have the reference stripped. */ |
| 12427 | tree type = non_reference (c->type); |
| 12428 | /* ck_qual won't have the pointer stripped. */ |
| 12429 | type = strip_pointer_operator (type); |
| 12430 | return (TREE_CODE (type) == ARRAY_TYPE |
| 12431 | && TYPE_DOMAIN (type) == NULL_TREE); |
| 12432 | } |
| 12433 | |
| 12434 | /* Compare two implicit conversion sequences according to the rules set out in |
| 12435 | [over.ics.rank]. Return values: |
| 12436 | |
| 12437 | 1: ics1 is better than ics2 |
| 12438 | -1: ics2 is better than ics1 |
| 12439 | 0: ics1 and ics2 are indistinguishable */ |
| 12440 | |
| 12441 | static int |
| 12442 | compare_ics (conversion *ics1, conversion *ics2) |
| 12443 | { |
| 12444 | tree from_type1; |
| 12445 | tree from_type2; |
| 12446 | tree to_type1; |
| 12447 | tree to_type2; |
| 12448 | tree deref_from_type1 = NULL_TREE; |
| 12449 | tree deref_from_type2 = NULL_TREE; |
| 12450 | tree deref_to_type1 = NULL_TREE; |
| 12451 | tree deref_to_type2 = NULL_TREE; |
| 12452 | conversion_rank rank1, rank2; |
| 12453 | |
| 12454 | /* REF_BINDING is nonzero if the result of the conversion sequence |
| 12455 | is a reference type. In that case REF_CONV is the reference |
| 12456 | binding conversion. */ |
| 12457 | conversion *ref_conv1; |
| 12458 | conversion *ref_conv2; |
| 12459 | |
| 12460 | /* Compare badness before stripping the reference conversion. */ |
| 12461 | if (ics1->bad_p > ics2->bad_p) |
| 12462 | return -1; |
| 12463 | else if (ics1->bad_p < ics2->bad_p) |
| 12464 | return 1; |
| 12465 | |
| 12466 | /* Handle implicit object parameters. */ |
| 12467 | maybe_handle_implicit_object (ics: &ics1); |
| 12468 | maybe_handle_implicit_object (ics: &ics2); |
| 12469 | |
| 12470 | /* Handle reference parameters. */ |
| 12471 | ref_conv1 = maybe_handle_ref_bind (ics: &ics1); |
| 12472 | ref_conv2 = maybe_handle_ref_bind (ics: &ics2); |
| 12473 | |
| 12474 | /* List-initialization sequence L1 is a better conversion sequence than |
| 12475 | list-initialization sequence L2 if L1 converts to |
| 12476 | std::initializer_list<X> for some X and L2 does not. */ |
| 12477 | if (ics1->kind == ck_list && ics2->kind != ck_list) |
| 12478 | return 1; |
| 12479 | if (ics2->kind == ck_list && ics1->kind != ck_list) |
| 12480 | return -1; |
| 12481 | |
| 12482 | /* [over.ics.rank] |
| 12483 | |
| 12484 | When comparing the basic forms of implicit conversion sequences (as |
| 12485 | defined in _over.best.ics_) |
| 12486 | |
| 12487 | --a standard conversion sequence (_over.ics.scs_) is a better |
| 12488 | conversion sequence than a user-defined conversion sequence |
| 12489 | or an ellipsis conversion sequence, and |
| 12490 | |
| 12491 | --a user-defined conversion sequence (_over.ics.user_) is a |
| 12492 | better conversion sequence than an ellipsis conversion sequence |
| 12493 | (_over.ics.ellipsis_). */ |
| 12494 | /* Use BAD_CONVERSION_RANK because we already checked for a badness |
| 12495 | mismatch. If both ICS are bad, we try to make a decision based on |
| 12496 | what would have happened if they'd been good. This is not an |
| 12497 | extension, we'll still give an error when we build up the call; this |
| 12498 | just helps us give a more helpful error message. */ |
| 12499 | rank1 = BAD_CONVERSION_RANK (ics1); |
| 12500 | rank2 = BAD_CONVERSION_RANK (ics2); |
| 12501 | |
| 12502 | if (rank1 > rank2) |
| 12503 | return -1; |
| 12504 | else if (rank1 < rank2) |
| 12505 | return 1; |
| 12506 | |
| 12507 | if (ics1->ellipsis_p) |
| 12508 | /* Both conversions are ellipsis conversions. */ |
| 12509 | return 0; |
| 12510 | |
| 12511 | /* User-defined conversion sequence U1 is a better conversion sequence |
| 12512 | than another user-defined conversion sequence U2 if they contain the |
| 12513 | same user-defined conversion operator or constructor and if the sec- |
| 12514 | ond standard conversion sequence of U1 is better than the second |
| 12515 | standard conversion sequence of U2. */ |
| 12516 | |
| 12517 | /* Handle list-conversion with the same code even though it isn't always |
| 12518 | ranked as a user-defined conversion and it doesn't have a second |
| 12519 | standard conversion sequence; it will still have the desired effect. |
| 12520 | Specifically, we need to do the reference binding comparison at the |
| 12521 | end of this function. */ |
| 12522 | |
| 12523 | if (ics1->user_conv_p || ics1->kind == ck_list |
| 12524 | || ics1->kind == ck_aggr || ics2->kind == ck_aggr) |
| 12525 | { |
| 12526 | conversion *t1 = strip_standard_conversion (conv: ics1); |
| 12527 | conversion *t2 = strip_standard_conversion (conv: ics2); |
| 12528 | |
| 12529 | if (!t1 || !t2 || t1->kind != t2->kind) |
| 12530 | return 0; |
| 12531 | else if (t1->kind == ck_user) |
| 12532 | { |
| 12533 | tree f1 = t1->cand ? t1->cand->fn : t1->type; |
| 12534 | tree f2 = t2->cand ? t2->cand->fn : t2->type; |
| 12535 | if (f1 != f2) |
| 12536 | return 0; |
| 12537 | } |
| 12538 | /* List-initialization sequence L1 is a better conversion sequence than |
| 12539 | list-initialization sequence L2 if |
| 12540 | |
| 12541 | -- L1 and L2 convert to arrays of the same element type, and either |
| 12542 | the number of elements n1 initialized by L1 is less than the number |
| 12543 | of elements n2 initialized by L2, or n1=n2 and L2 converts to an array |
| 12544 | of unknown bound and L1 does not. (Added in CWG 1307 and extended by |
| 12545 | P0388R4.) */ |
| 12546 | else if (t1->kind == ck_aggr |
| 12547 | && TREE_CODE (t1->type) == ARRAY_TYPE |
| 12548 | && TREE_CODE (t2->type) == ARRAY_TYPE |
| 12549 | && same_type_p (TREE_TYPE (t1->type), TREE_TYPE (t2->type))) |
| 12550 | { |
| 12551 | tree n1 = nelts_initialized_by_list_init (c: t1); |
| 12552 | tree n2 = nelts_initialized_by_list_init (c: t2); |
| 12553 | if (tree_int_cst_lt (t1: n1, t2: n2)) |
| 12554 | return 1; |
| 12555 | else if (tree_int_cst_lt (t1: n2, t2: n1)) |
| 12556 | return -1; |
| 12557 | /* The n1 == n2 case. */ |
| 12558 | bool c1 = conv_binds_to_array_of_unknown_bound (c: t1); |
| 12559 | bool c2 = conv_binds_to_array_of_unknown_bound (c: t2); |
| 12560 | if (c1 && !c2) |
| 12561 | return -1; |
| 12562 | else if (!c1 && c2) |
| 12563 | return 1; |
| 12564 | else |
| 12565 | return 0; |
| 12566 | } |
| 12567 | else |
| 12568 | { |
| 12569 | /* For ambiguous or aggregate conversions, use the target type as |
| 12570 | a proxy for the conversion function. */ |
| 12571 | if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type)) |
| 12572 | return 0; |
| 12573 | } |
| 12574 | |
| 12575 | /* We can just fall through here, after setting up |
| 12576 | FROM_TYPE1 and FROM_TYPE2. */ |
| 12577 | from_type1 = t1->type; |
| 12578 | from_type2 = t2->type; |
| 12579 | } |
| 12580 | else |
| 12581 | { |
| 12582 | conversion *t1; |
| 12583 | conversion *t2; |
| 12584 | |
| 12585 | /* We're dealing with two standard conversion sequences. |
| 12586 | |
| 12587 | [over.ics.rank] |
| 12588 | |
| 12589 | Standard conversion sequence S1 is a better conversion |
| 12590 | sequence than standard conversion sequence S2 if |
| 12591 | |
| 12592 | --S1 is a proper subsequence of S2 (comparing the conversion |
| 12593 | sequences in the canonical form defined by _over.ics.scs_, |
| 12594 | excluding any Lvalue Transformation; the identity |
| 12595 | conversion sequence is considered to be a subsequence of |
| 12596 | any non-identity conversion sequence */ |
| 12597 | |
| 12598 | t1 = ics1; |
| 12599 | while (t1->kind != ck_identity) |
| 12600 | t1 = next_conversion (conv: t1); |
| 12601 | from_type1 = t1->type; |
| 12602 | |
| 12603 | t2 = ics2; |
| 12604 | while (t2->kind != ck_identity) |
| 12605 | t2 = next_conversion (conv: t2); |
| 12606 | from_type2 = t2->type; |
| 12607 | } |
| 12608 | |
| 12609 | /* One sequence can only be a subsequence of the other if they start with |
| 12610 | the same type. They can start with different types when comparing the |
| 12611 | second standard conversion sequence in two user-defined conversion |
| 12612 | sequences. */ |
| 12613 | if (same_type_p (from_type1, from_type2)) |
| 12614 | { |
| 12615 | if (is_subseq (ics1, ics2)) |
| 12616 | return 1; |
| 12617 | if (is_subseq (ics1: ics2, ics2: ics1)) |
| 12618 | return -1; |
| 12619 | } |
| 12620 | |
| 12621 | /* [over.ics.rank] |
| 12622 | |
| 12623 | Or, if not that, |
| 12624 | |
| 12625 | --the rank of S1 is better than the rank of S2 (by the rules |
| 12626 | defined below): |
| 12627 | |
| 12628 | Standard conversion sequences are ordered by their ranks: an Exact |
| 12629 | Match is a better conversion than a Promotion, which is a better |
| 12630 | conversion than a Conversion. |
| 12631 | |
| 12632 | Two conversion sequences with the same rank are indistinguishable |
| 12633 | unless one of the following rules applies: |
| 12634 | |
| 12635 | --A conversion that does not a convert a pointer, pointer to member, |
| 12636 | or std::nullptr_t to bool is better than one that does. |
| 12637 | |
| 12638 | The ICS_STD_RANK automatically handles the pointer-to-bool rule, |
| 12639 | so that we do not have to check it explicitly. */ |
| 12640 | if (ics1->rank < ics2->rank) |
| 12641 | return 1; |
| 12642 | else if (ics2->rank < ics1->rank) |
| 12643 | return -1; |
| 12644 | |
| 12645 | to_type1 = ics1->type; |
| 12646 | to_type2 = ics2->type; |
| 12647 | |
| 12648 | /* A conversion from scalar arithmetic type to complex is worse than a |
| 12649 | conversion between scalar arithmetic types. */ |
| 12650 | if (same_type_p (from_type1, from_type2) |
| 12651 | && ARITHMETIC_TYPE_P (from_type1) |
| 12652 | && ARITHMETIC_TYPE_P (to_type1) |
| 12653 | && ARITHMETIC_TYPE_P (to_type2) |
| 12654 | && ((TREE_CODE (to_type1) == COMPLEX_TYPE) |
| 12655 | != (TREE_CODE (to_type2) == COMPLEX_TYPE))) |
| 12656 | { |
| 12657 | if (TREE_CODE (to_type1) == COMPLEX_TYPE) |
| 12658 | return -1; |
| 12659 | else |
| 12660 | return 1; |
| 12661 | } |
| 12662 | |
| 12663 | { |
| 12664 | /* A conversion in either direction between floating-point type FP1 and |
| 12665 | floating-point type FP2 is better than a conversion in the same |
| 12666 | direction between FP1 and arithmetic type T3 if |
| 12667 | - the floating-point conversion rank of FP1 is equal to the rank of |
| 12668 | FP2, and |
| 12669 | - T3 is not a floating-point type, or T3 is a floating-point type |
| 12670 | whose rank is not equal to the rank of FP1, or the floating-point |
| 12671 | conversion subrank of FP2 is greater than the subrank of T3. */ |
| 12672 | tree fp1 = from_type1; |
| 12673 | tree fp2 = to_type1; |
| 12674 | tree fp3 = from_type2; |
| 12675 | tree t3 = to_type2; |
| 12676 | int ret = 1; |
| 12677 | if (TYPE_MAIN_VARIANT (fp2) == TYPE_MAIN_VARIANT (t3)) |
| 12678 | { |
| 12679 | std::swap (a&: fp1, b&: fp2); |
| 12680 | std::swap (a&: fp3, b&: t3); |
| 12681 | } |
| 12682 | if (TYPE_MAIN_VARIANT (fp1) == TYPE_MAIN_VARIANT (fp3) |
| 12683 | && SCALAR_FLOAT_TYPE_P (fp1) |
| 12684 | /* Only apply this rule if at least one of the 3 types is |
| 12685 | extended floating-point type, otherwise keep them as |
| 12686 | before for compatibility reasons with types like __float128. |
| 12687 | float, double and long double alone have different conversion |
| 12688 | ranks and so when just those 3 types are involved, this |
| 12689 | rule doesn't trigger. */ |
| 12690 | && (extended_float_type_p (type: fp1) |
| 12691 | || (SCALAR_FLOAT_TYPE_P (fp2) && extended_float_type_p (type: fp2)) |
| 12692 | || (SCALAR_FLOAT_TYPE_P (t3) && extended_float_type_p (type: t3)))) |
| 12693 | { |
| 12694 | if (TREE_CODE (fp2) != REAL_TYPE) |
| 12695 | { |
| 12696 | ret = -ret; |
| 12697 | std::swap (a&: fp2, b&: t3); |
| 12698 | } |
| 12699 | if (SCALAR_FLOAT_TYPE_P (fp2)) |
| 12700 | { |
| 12701 | /* cp_compare_floating_point_conversion_ranks returns -1, 0 or 1 |
| 12702 | if the conversion rank is equal (-1 or 1 if the subrank is |
| 12703 | different). */ |
| 12704 | if (IN_RANGE (cp_compare_floating_point_conversion_ranks (fp1, |
| 12705 | fp2), |
| 12706 | -1, 1)) |
| 12707 | { |
| 12708 | /* Conversion ranks of FP1 and FP2 are equal. */ |
| 12709 | if (TREE_CODE (t3) != REAL_TYPE |
| 12710 | || !IN_RANGE (cp_compare_floating_point_conversion_ranks |
| 12711 | (fp1, t3), |
| 12712 | -1, 1)) |
| 12713 | /* FP1 <-> FP2 conversion is better. */ |
| 12714 | return ret; |
| 12715 | int c = cp_compare_floating_point_conversion_ranks (fp2, t3); |
| 12716 | gcc_assert (IN_RANGE (c, -1, 1)); |
| 12717 | if (c == 1) |
| 12718 | /* Conversion subrank of FP2 is greater than subrank of T3. |
| 12719 | FP1 <-> FP2 conversion is better. */ |
| 12720 | return ret; |
| 12721 | else if (c == -1) |
| 12722 | /* Conversion subrank of FP2 is less than subrank of T3. |
| 12723 | FP1 <-> T3 conversion is better. */ |
| 12724 | return -ret; |
| 12725 | } |
| 12726 | else if (SCALAR_FLOAT_TYPE_P (t3) |
| 12727 | && IN_RANGE (cp_compare_floating_point_conversion_ranks |
| 12728 | (fp1, t3), |
| 12729 | -1, 1)) |
| 12730 | /* Conversion ranks of FP1 and FP2 are not equal, conversion |
| 12731 | ranks of FP1 and T3 are equal. |
| 12732 | FP1 <-> T3 conversion is better. */ |
| 12733 | return -ret; |
| 12734 | } |
| 12735 | } |
| 12736 | } |
| 12737 | |
| 12738 | if (TYPE_PTR_P (from_type1) |
| 12739 | && TYPE_PTR_P (from_type2) |
| 12740 | && TYPE_PTR_P (to_type1) |
| 12741 | && TYPE_PTR_P (to_type2)) |
| 12742 | { |
| 12743 | deref_from_type1 = TREE_TYPE (from_type1); |
| 12744 | deref_from_type2 = TREE_TYPE (from_type2); |
| 12745 | deref_to_type1 = TREE_TYPE (to_type1); |
| 12746 | deref_to_type2 = TREE_TYPE (to_type2); |
| 12747 | } |
| 12748 | /* The rules for pointers to members A::* are just like the rules |
| 12749 | for pointers A*, except opposite: if B is derived from A then |
| 12750 | A::* converts to B::*, not vice versa. For that reason, we |
| 12751 | switch the from_ and to_ variables here. */ |
| 12752 | else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2) |
| 12753 | && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2)) |
| 12754 | || (TYPE_PTRMEMFUNC_P (from_type1) |
| 12755 | && TYPE_PTRMEMFUNC_P (from_type2) |
| 12756 | && TYPE_PTRMEMFUNC_P (to_type1) |
| 12757 | && TYPE_PTRMEMFUNC_P (to_type2))) |
| 12758 | { |
| 12759 | deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1); |
| 12760 | deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2); |
| 12761 | deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1); |
| 12762 | deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2); |
| 12763 | } |
| 12764 | |
| 12765 | if (deref_from_type1 != NULL_TREE |
| 12766 | && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1)) |
| 12767 | && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2))) |
| 12768 | { |
| 12769 | /* This was one of the pointer or pointer-like conversions. |
| 12770 | |
| 12771 | [over.ics.rank] |
| 12772 | |
| 12773 | --If class B is derived directly or indirectly from class A, |
| 12774 | conversion of B* to A* is better than conversion of B* to |
| 12775 | void*, and conversion of A* to void* is better than |
| 12776 | conversion of B* to void*. */ |
| 12777 | if (VOID_TYPE_P (deref_to_type1) |
| 12778 | && VOID_TYPE_P (deref_to_type2)) |
| 12779 | { |
| 12780 | if (is_properly_derived_from (derived: deref_from_type1, |
| 12781 | base: deref_from_type2)) |
| 12782 | return -1; |
| 12783 | else if (is_properly_derived_from (derived: deref_from_type2, |
| 12784 | base: deref_from_type1)) |
| 12785 | return 1; |
| 12786 | } |
| 12787 | else if (VOID_TYPE_P (deref_to_type1) |
| 12788 | || VOID_TYPE_P (deref_to_type2)) |
| 12789 | { |
| 12790 | if (same_type_p (deref_from_type1, deref_from_type2)) |
| 12791 | { |
| 12792 | if (VOID_TYPE_P (deref_to_type2)) |
| 12793 | { |
| 12794 | if (is_properly_derived_from (derived: deref_from_type1, |
| 12795 | base: deref_to_type1)) |
| 12796 | return 1; |
| 12797 | } |
| 12798 | /* We know that DEREF_TO_TYPE1 is `void' here. */ |
| 12799 | else if (is_properly_derived_from (derived: deref_from_type1, |
| 12800 | base: deref_to_type2)) |
| 12801 | return -1; |
| 12802 | } |
| 12803 | } |
| 12804 | else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1)) |
| 12805 | && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2))) |
| 12806 | { |
| 12807 | /* [over.ics.rank] |
| 12808 | |
| 12809 | --If class B is derived directly or indirectly from class A |
| 12810 | and class C is derived directly or indirectly from B, |
| 12811 | |
| 12812 | --conversion of C* to B* is better than conversion of C* to |
| 12813 | A*, |
| 12814 | |
| 12815 | --conversion of B* to A* is better than conversion of C* to |
| 12816 | A* */ |
| 12817 | if (same_type_p (deref_from_type1, deref_from_type2)) |
| 12818 | { |
| 12819 | if (is_properly_derived_from (derived: deref_to_type1, |
| 12820 | base: deref_to_type2)) |
| 12821 | return 1; |
| 12822 | else if (is_properly_derived_from (derived: deref_to_type2, |
| 12823 | base: deref_to_type1)) |
| 12824 | return -1; |
| 12825 | } |
| 12826 | else if (same_type_p (deref_to_type1, deref_to_type2)) |
| 12827 | { |
| 12828 | if (is_properly_derived_from (derived: deref_from_type2, |
| 12829 | base: deref_from_type1)) |
| 12830 | return 1; |
| 12831 | else if (is_properly_derived_from (derived: deref_from_type1, |
| 12832 | base: deref_from_type2)) |
| 12833 | return -1; |
| 12834 | } |
| 12835 | } |
| 12836 | } |
| 12837 | else if (CLASS_TYPE_P (non_reference (from_type1)) |
| 12838 | && same_type_p (from_type1, from_type2)) |
| 12839 | { |
| 12840 | tree from = non_reference (from_type1); |
| 12841 | |
| 12842 | /* [over.ics.rank] |
| 12843 | |
| 12844 | --binding of an expression of type C to a reference of type |
| 12845 | B& is better than binding an expression of type C to a |
| 12846 | reference of type A& |
| 12847 | |
| 12848 | --conversion of C to B is better than conversion of C to A, */ |
| 12849 | if (is_properly_derived_from (derived: from, base: to_type1) |
| 12850 | && is_properly_derived_from (derived: from, base: to_type2)) |
| 12851 | { |
| 12852 | if (is_properly_derived_from (derived: to_type1, base: to_type2)) |
| 12853 | return 1; |
| 12854 | else if (is_properly_derived_from (derived: to_type2, base: to_type1)) |
| 12855 | return -1; |
| 12856 | } |
| 12857 | } |
| 12858 | else if (CLASS_TYPE_P (non_reference (to_type1)) |
| 12859 | && same_type_p (to_type1, to_type2)) |
| 12860 | { |
| 12861 | tree to = non_reference (to_type1); |
| 12862 | |
| 12863 | /* [over.ics.rank] |
| 12864 | |
| 12865 | --binding of an expression of type B to a reference of type |
| 12866 | A& is better than binding an expression of type C to a |
| 12867 | reference of type A&, |
| 12868 | |
| 12869 | --conversion of B to A is better than conversion of C to A */ |
| 12870 | if (is_properly_derived_from (derived: from_type1, base: to) |
| 12871 | && is_properly_derived_from (derived: from_type2, base: to)) |
| 12872 | { |
| 12873 | if (is_properly_derived_from (derived: from_type2, base: from_type1)) |
| 12874 | return 1; |
| 12875 | else if (is_properly_derived_from (derived: from_type1, base: from_type2)) |
| 12876 | return -1; |
| 12877 | } |
| 12878 | } |
| 12879 | |
| 12880 | /* [over.ics.rank] |
| 12881 | |
| 12882 | --S1 and S2 differ only in their qualification conversion and yield |
| 12883 | similar types T1 and T2 (_conv.qual_), respectively, and the cv- |
| 12884 | qualification signature of type T1 is a proper subset of the cv- |
| 12885 | qualification signature of type T2 */ |
| 12886 | if (ics1->kind == ck_qual |
| 12887 | && ics2->kind == ck_qual |
| 12888 | && same_type_p (from_type1, from_type2)) |
| 12889 | { |
| 12890 | int result = comp_cv_qual_signature (to_type1, to_type2); |
| 12891 | if (result != 0) |
| 12892 | return result; |
| 12893 | } |
| 12894 | |
| 12895 | /* [over.ics.rank] |
| 12896 | |
| 12897 | --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers |
| 12898 | to an implicit object parameter of a non-static member function |
| 12899 | declared without a ref-qualifier, and either S1 binds an lvalue |
| 12900 | reference to an lvalue and S2 binds an rvalue reference or S1 binds an |
| 12901 | rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x |
| 12902 | draft standard, 13.3.3.2) |
| 12903 | |
| 12904 | --S1 and S2 are reference bindings (_dcl.init.ref_), and the |
| 12905 | types to which the references refer are the same type except for |
| 12906 | top-level cv-qualifiers, and the type to which the reference |
| 12907 | initialized by S2 refers is more cv-qualified than the type to |
| 12908 | which the reference initialized by S1 refers. |
| 12909 | |
| 12910 | DR 1328 [over.match.best]: the context is an initialization by |
| 12911 | conversion function for direct reference binding (13.3.1.6) of a |
| 12912 | reference to function type, the return type of F1 is the same kind of |
| 12913 | reference (i.e. lvalue or rvalue) as the reference being initialized, |
| 12914 | and the return type of F2 is not. */ |
| 12915 | |
| 12916 | if (ref_conv1 && ref_conv2) |
| 12917 | { |
| 12918 | if (!ref_conv1->this_p && !ref_conv2->this_p |
| 12919 | && (ref_conv1->rvaluedness_matches_p |
| 12920 | != ref_conv2->rvaluedness_matches_p) |
| 12921 | && (same_type_p (ref_conv1->type, ref_conv2->type) |
| 12922 | || (TYPE_REF_IS_RVALUE (ref_conv1->type) |
| 12923 | != TYPE_REF_IS_RVALUE (ref_conv2->type)))) |
| 12924 | { |
| 12925 | if (ref_conv1->bad_p |
| 12926 | && !same_type_p (TREE_TYPE (ref_conv1->type), |
| 12927 | TREE_TYPE (ref_conv2->type))) |
| 12928 | /* Don't prefer a bad conversion that drops cv-quals to a bad |
| 12929 | conversion with the wrong rvalueness. */ |
| 12930 | return 0; |
| 12931 | return (ref_conv1->rvaluedness_matches_p |
| 12932 | - ref_conv2->rvaluedness_matches_p); |
| 12933 | } |
| 12934 | |
| 12935 | if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2)) |
| 12936 | { |
| 12937 | /* Per P0388R4: |
| 12938 | |
| 12939 | void f (int(&)[]), // (1) |
| 12940 | f (int(&)[1]), // (2) |
| 12941 | f (int*); // (3) |
| 12942 | |
| 12943 | (2) is better than (1), but (3) should be equal to (1) and to |
| 12944 | (2). For that reason we don't use ck_qual for (1) which would |
| 12945 | give it the cr_exact rank while (3) remains ck_identity. |
| 12946 | Therefore we compare (1) and (2) here. For (1) we'll have |
| 12947 | |
| 12948 | ck_ref_bind <- ck_identity |
| 12949 | int[] & int[1] |
| 12950 | |
| 12951 | so to handle this we must look at ref_conv. */ |
| 12952 | bool c1 = conv_binds_to_array_of_unknown_bound (c: ref_conv1); |
| 12953 | bool c2 = conv_binds_to_array_of_unknown_bound (c: ref_conv2); |
| 12954 | if (c1 && !c2) |
| 12955 | return -1; |
| 12956 | else if (!c1 && c2) |
| 12957 | return 1; |
| 12958 | |
| 12959 | int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type)); |
| 12960 | int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type)); |
| 12961 | if (ref_conv1->bad_p) |
| 12962 | { |
| 12963 | /* Prefer the one that drops fewer cv-quals. */ |
| 12964 | tree ftype = next_conversion (conv: ref_conv1)->type; |
| 12965 | int fquals = cp_type_quals (ftype); |
| 12966 | q1 ^= fquals; |
| 12967 | q2 ^= fquals; |
| 12968 | } |
| 12969 | return comp_cv_qualification (q2, q1); |
| 12970 | } |
| 12971 | } |
| 12972 | |
| 12973 | /* [over.ics.rank] |
| 12974 | |
| 12975 | Per CWG 1601: |
| 12976 | -- A conversion that promotes an enumeration whose underlying type |
| 12977 | is fixed to its underlying type is better than one that promotes to |
| 12978 | the promoted underlying type, if the two are different. */ |
| 12979 | if (ics1->rank == cr_promotion |
| 12980 | && ics2->rank == cr_promotion |
| 12981 | && UNSCOPED_ENUM_P (from_type1) |
| 12982 | && ENUM_FIXED_UNDERLYING_TYPE_P (from_type1) |
| 12983 | && same_type_p (from_type1, from_type2)) |
| 12984 | { |
| 12985 | tree utype = ENUM_UNDERLYING_TYPE (from_type1); |
| 12986 | tree prom = type_promotes_to (from_type1); |
| 12987 | if (!same_type_p (utype, prom)) |
| 12988 | { |
| 12989 | if (same_type_p (to_type1, utype) |
| 12990 | && same_type_p (to_type2, prom)) |
| 12991 | return 1; |
| 12992 | else if (same_type_p (to_type2, utype) |
| 12993 | && same_type_p (to_type1, prom)) |
| 12994 | return -1; |
| 12995 | } |
| 12996 | } |
| 12997 | |
| 12998 | /* Neither conversion sequence is better than the other. */ |
| 12999 | return 0; |
| 13000 | } |
| 13001 | |
| 13002 | /* The source type for this standard conversion sequence. */ |
| 13003 | |
| 13004 | static tree |
| 13005 | source_type (conversion *t) |
| 13006 | { |
| 13007 | return strip_standard_conversion (conv: t)->type; |
| 13008 | } |
| 13009 | |
| 13010 | /* Note a warning about preferring WINNER to LOSER. We do this by storing |
| 13011 | a pointer to LOSER and re-running joust to produce the warning if WINNER |
| 13012 | is actually used. */ |
| 13013 | |
| 13014 | static void |
| 13015 | add_warning (struct z_candidate *winner, struct z_candidate *loser) |
| 13016 | { |
| 13017 | candidate_warning *cw = (candidate_warning *) |
| 13018 | conversion_obstack_alloc (n: sizeof (candidate_warning)); |
| 13019 | cw->loser = loser; |
| 13020 | cw->next = winner->warnings; |
| 13021 | winner->warnings = cw; |
| 13022 | } |
| 13023 | |
| 13024 | /* CAND is a constructor candidate in joust in C++17 and up. If it copies a |
| 13025 | prvalue returned from a conversion function, return true. Otherwise, return |
| 13026 | false. */ |
| 13027 | |
| 13028 | static bool |
| 13029 | joust_maybe_elide_copy (z_candidate *cand) |
| 13030 | { |
| 13031 | tree fn = cand->fn; |
| 13032 | if (!DECL_COPY_CONSTRUCTOR_P (fn) && !DECL_MOVE_CONSTRUCTOR_P (fn)) |
| 13033 | return false; |
| 13034 | conversion *conv = cand->convs[0]; |
| 13035 | if (conv->kind == ck_ambig) |
| 13036 | return false; |
| 13037 | gcc_checking_assert (conv->kind == ck_ref_bind); |
| 13038 | conv = next_conversion (conv); |
| 13039 | if (conv->kind == ck_user && !TYPE_REF_P (conv->type)) |
| 13040 | { |
| 13041 | gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p |
| 13042 | (conv->type, DECL_CONTEXT (fn))); |
| 13043 | z_candidate *uc = conv->cand; |
| 13044 | if (DECL_CONV_FN_P (uc->fn)) |
| 13045 | return true; |
| 13046 | } |
| 13047 | return false; |
| 13048 | } |
| 13049 | |
| 13050 | /* Return the class that CAND's implicit object parameter refers to. */ |
| 13051 | |
| 13052 | static tree |
| 13053 | class_of_implicit_object (z_candidate *cand) |
| 13054 | { |
| 13055 | if (!DECL_IOBJ_MEMBER_FUNCTION_P (cand->fn)) |
| 13056 | return NULL_TREE; |
| 13057 | |
| 13058 | /* "For conversion functions that are implicit object member functions, |
| 13059 | the function is considered to be a member of the class of the implied |
| 13060 | object argument for the purpose of defining the type of the implicit |
| 13061 | object parameter." */ |
| 13062 | if (DECL_CONV_FN_P (cand->fn)) |
| 13063 | return TYPE_MAIN_VARIANT (TREE_TYPE (cand->first_arg)); |
| 13064 | |
| 13065 | /* "For non-conversion functions that are implicit object member |
| 13066 | functions nominated by a using-declaration in a derived class, the |
| 13067 | function is considered to be a member of the derived class for the |
| 13068 | purpose of defining the type of the implicit object parameter." |
| 13069 | |
| 13070 | That derived class is reflected in the conversion_path binfo. */ |
| 13071 | return BINFO_TYPE (cand->conversion_path); |
| 13072 | } |
| 13073 | |
| 13074 | /* Return whether the first parameter of C1 matches the second parameter |
| 13075 | of C2. */ |
| 13076 | |
| 13077 | static bool |
| 13078 | reversed_match (z_candidate *c1, z_candidate *c2) |
| 13079 | { |
| 13080 | tree fn1 = c1->fn; |
| 13081 | tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (c2->fn)); |
| 13082 | tree parm2 = TREE_VALUE (TREE_CHAIN (parms2)); |
| 13083 | if (DECL_IOBJ_MEMBER_FUNCTION_P (fn1)) |
| 13084 | { |
| 13085 | tree ctx = class_of_implicit_object (cand: c1); |
| 13086 | return iobj_parm_corresponds_to (fn1, parm2, ctx); |
| 13087 | } |
| 13088 | else |
| 13089 | { |
| 13090 | tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1)); |
| 13091 | tree parm1 = TREE_VALUE (parms1); |
| 13092 | return same_type_p (parm1, parm2); |
| 13093 | } |
| 13094 | } |
| 13095 | |
| 13096 | /* True if the defining declarations of the two candidates have equivalent |
| 13097 | parameters. MATCH_KIND controls whether we're trying to compare the |
| 13098 | original declarations (for a warning) or the actual candidates. */ |
| 13099 | |
| 13100 | enum class pmatch { original, current }; |
| 13101 | |
| 13102 | static bool |
| 13103 | cand_parms_match (z_candidate *c1, z_candidate *c2, pmatch match_kind) |
| 13104 | { |
| 13105 | tree fn1 = c1->fn; |
| 13106 | tree fn2 = c2->fn; |
| 13107 | bool reversed = (match_kind == pmatch::current |
| 13108 | && c1->reversed () != c2->reversed ()); |
| 13109 | if (fn1 == fn2 && !reversed) |
| 13110 | return true; |
| 13111 | if (identifier_p (t: fn1) || identifier_p (t: fn2)) |
| 13112 | return false; |
| 13113 | if (match_kind == pmatch::original) |
| 13114 | { |
| 13115 | /* We don't look at c1->template_decl because that's only set for |
| 13116 | primary templates, not e.g. non-template member functions of |
| 13117 | class templates. */ |
| 13118 | tree t1 = most_general_template (fn1); |
| 13119 | tree t2 = most_general_template (fn2); |
| 13120 | if (t1 || t2) |
| 13121 | { |
| 13122 | if (!t1 || !t2) |
| 13123 | return false; |
| 13124 | if (t1 == t2) |
| 13125 | return true; |
| 13126 | fn1 = DECL_TEMPLATE_RESULT (t1); |
| 13127 | fn2 = DECL_TEMPLATE_RESULT (t2); |
| 13128 | } |
| 13129 | } |
| 13130 | |
| 13131 | tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1)); |
| 13132 | tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (fn2)); |
| 13133 | |
| 13134 | if (DECL_FUNCTION_MEMBER_P (fn1) |
| 13135 | && DECL_FUNCTION_MEMBER_P (fn2)) |
| 13136 | { |
| 13137 | tree base1 = DECL_CONTEXT (strip_inheriting_ctors (fn1)); |
| 13138 | tree base2 = DECL_CONTEXT (strip_inheriting_ctors (fn2)); |
| 13139 | if (base1 != base2) |
| 13140 | return false; |
| 13141 | |
| 13142 | if (reversed) |
| 13143 | return (reversed_match (c1, c2) |
| 13144 | && reversed_match (c1: c2, c2: c1)); |
| 13145 | |
| 13146 | /* Use object_parms_correspond to simplify comparing iobj/xobj/static |
| 13147 | member functions. */ |
| 13148 | if (!object_parms_correspond (fn1, fn2, base1)) |
| 13149 | return false; |
| 13150 | |
| 13151 | /* We just compared the object parameters, if they don't correspond |
| 13152 | we already returned false. */ |
| 13153 | auto skip_parms = [] (tree fn, tree parms) |
| 13154 | { |
| 13155 | if (DECL_XOBJ_MEMBER_FUNCTION_P (fn)) |
| 13156 | return TREE_CHAIN (parms); |
| 13157 | else |
| 13158 | return skip_artificial_parms_for (fn, parms); |
| 13159 | }; |
| 13160 | parms1 = skip_parms (fn1, parms1); |
| 13161 | parms2 = skip_parms (fn2, parms2); |
| 13162 | } |
| 13163 | else if (reversed) |
| 13164 | return (reversed_match (c1, c2) |
| 13165 | && reversed_match (c1: c2, c2: c1)); |
| 13166 | return compparms (parms1, parms2); |
| 13167 | } |
| 13168 | |
| 13169 | /* True iff FN is a copy or move constructor or assignment operator. */ |
| 13170 | |
| 13171 | static bool |
| 13172 | sfk_copy_or_move (tree fn) |
| 13173 | { |
| 13174 | if (TREE_CODE (fn) != FUNCTION_DECL) |
| 13175 | return false; |
| 13176 | special_function_kind sfk = special_function_p (fn); |
| 13177 | return sfk >= sfk_copy_constructor && sfk <= sfk_move_assignment; |
| 13178 | } |
| 13179 | |
| 13180 | /* Compare two candidates for overloading as described in |
| 13181 | [over.match.best]. Return values: |
| 13182 | |
| 13183 | 1: cand1 is better than cand2 |
| 13184 | -1: cand2 is better than cand1 |
| 13185 | 0: cand1 and cand2 are indistinguishable */ |
| 13186 | |
| 13187 | static int |
| 13188 | joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn, |
| 13189 | tsubst_flags_t complain) |
| 13190 | { |
| 13191 | int winner = 0; |
| 13192 | int off1 = 0, off2 = 0; |
| 13193 | size_t i; |
| 13194 | size_t len; |
| 13195 | |
| 13196 | /* Candidates that involve bad conversions are always worse than those |
| 13197 | that don't. */ |
| 13198 | if (cand1->viable > cand2->viable) |
| 13199 | return 1; |
| 13200 | if (cand1->viable < cand2->viable) |
| 13201 | return -1; |
| 13202 | |
| 13203 | /* If we have two pseudo-candidates for conversions to the same type, |
| 13204 | or two candidates for the same function, arbitrarily pick one. */ |
| 13205 | if (cand1->fn == cand2->fn |
| 13206 | && cand1->reversed () == cand2->reversed () |
| 13207 | && (IS_TYPE_OR_DECL_P (cand1->fn))) |
| 13208 | return 1; |
| 13209 | |
| 13210 | /* Prefer a non-deleted function over an implicitly deleted move |
| 13211 | constructor or assignment operator. This differs slightly from the |
| 13212 | wording for issue 1402 (which says the move op is ignored by overload |
| 13213 | resolution), but this way produces better error messages. */ |
| 13214 | if (TREE_CODE (cand1->fn) == FUNCTION_DECL |
| 13215 | && TREE_CODE (cand2->fn) == FUNCTION_DECL |
| 13216 | && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn)) |
| 13217 | { |
| 13218 | if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn) |
| 13219 | && move_fn_p (cand1->fn)) |
| 13220 | return -1; |
| 13221 | if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn) |
| 13222 | && move_fn_p (cand2->fn)) |
| 13223 | return 1; |
| 13224 | } |
| 13225 | |
| 13226 | /* a viable function F1 |
| 13227 | is defined to be a better function than another viable function F2 if |
| 13228 | for all arguments i, ICSi(F1) is not a worse conversion sequence than |
| 13229 | ICSi(F2), and then */ |
| 13230 | |
| 13231 | /* for some argument j, ICSj(F1) is a better conversion sequence than |
| 13232 | ICSj(F2) */ |
| 13233 | |
| 13234 | /* For comparing static and non-static member functions, we ignore |
| 13235 | the implicit object parameter of the non-static function. The |
| 13236 | standard says to pretend that the static function has an object |
| 13237 | parm, but that won't work with operator overloading. */ |
| 13238 | len = cand1->num_convs; |
| 13239 | if (len != cand2->num_convs) |
| 13240 | { |
| 13241 | int static_1 = (TREE_CODE (cand1->fn) == FUNCTION_DECL |
| 13242 | && DECL_STATIC_FUNCTION_P (cand1->fn)); |
| 13243 | int static_2 = (TREE_CODE (cand2->fn) == FUNCTION_DECL |
| 13244 | && DECL_STATIC_FUNCTION_P (cand2->fn)); |
| 13245 | |
| 13246 | if (TREE_CODE (cand1->fn) == FUNCTION_DECL |
| 13247 | && TREE_CODE (cand2->fn) == FUNCTION_DECL |
| 13248 | && DECL_CONSTRUCTOR_P (cand1->fn) |
| 13249 | && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn)) |
| 13250 | /* We're comparing a near-match list constructor and a near-match |
| 13251 | non-list constructor. Just treat them as unordered. */ |
| 13252 | return 0; |
| 13253 | |
| 13254 | gcc_assert (static_1 != static_2); |
| 13255 | |
| 13256 | if (static_1) |
| 13257 | { |
| 13258 | /* C++23 [over.best.ics.general] says: |
| 13259 | When the parameter is the implicit object parameter of a static |
| 13260 | member function, the implicit conversion sequence is a standard |
| 13261 | conversion sequence that is neither better nor worse than any |
| 13262 | other standard conversion sequence. */ |
| 13263 | if (CONVERSION_RANK (cand2->convs[0]) >= cr_user) |
| 13264 | winner = 1; |
| 13265 | off2 = 1; |
| 13266 | } |
| 13267 | else |
| 13268 | { |
| 13269 | if (CONVERSION_RANK (cand1->convs[0]) >= cr_user) |
| 13270 | winner = -1; |
| 13271 | off1 = 1; |
| 13272 | --len; |
| 13273 | } |
| 13274 | } |
| 13275 | |
| 13276 | for (i = 0; i < len; ++i) |
| 13277 | { |
| 13278 | conversion *t1 = cand1->convs[i + off1]; |
| 13279 | conversion *t2 = cand2->convs[i + off2]; |
| 13280 | int comp = compare_ics (ics1: t1, ics2: t2); |
| 13281 | |
| 13282 | if (comp != 0) |
| 13283 | { |
| 13284 | if ((complain & tf_warning) |
| 13285 | && warn_sign_promo |
| 13286 | && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2) |
| 13287 | == cr_std + cr_promotion) |
| 13288 | && t1->kind == ck_std |
| 13289 | && t2->kind == ck_std |
| 13290 | && TREE_CODE (t1->type) == INTEGER_TYPE |
| 13291 | && TREE_CODE (t2->type) == INTEGER_TYPE |
| 13292 | && (TYPE_PRECISION (t1->type) |
| 13293 | == TYPE_PRECISION (t2->type)) |
| 13294 | && (TYPE_UNSIGNED (next_conversion (t1)->type) |
| 13295 | || (TREE_CODE (next_conversion (t1)->type) |
| 13296 | == ENUMERAL_TYPE))) |
| 13297 | { |
| 13298 | tree type = next_conversion (conv: t1)->type; |
| 13299 | tree type1, type2; |
| 13300 | struct z_candidate *w, *l; |
| 13301 | if (comp > 0) |
| 13302 | type1 = t1->type, type2 = t2->type, |
| 13303 | w = cand1, l = cand2; |
| 13304 | else |
| 13305 | type1 = t2->type, type2 = t1->type, |
| 13306 | w = cand2, l = cand1; |
| 13307 | |
| 13308 | if (warn) |
| 13309 | { |
| 13310 | warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT" , |
| 13311 | type, type1, type2); |
| 13312 | warning (OPT_Wsign_promo, " in call to %qD" , w->fn); |
| 13313 | } |
| 13314 | else |
| 13315 | add_warning (winner: w, loser: l); |
| 13316 | } |
| 13317 | |
| 13318 | if (winner && comp != winner) |
| 13319 | { |
| 13320 | /* Ambiguity between normal and reversed comparison operators |
| 13321 | with the same parameter types. P2468 decided not to go with |
| 13322 | this approach to resolving the ambiguity, so pedwarn. */ |
| 13323 | if ((complain & tf_warning_or_error) |
| 13324 | && (cand1->reversed () != cand2->reversed ()) |
| 13325 | && cand_parms_match (c1: cand1, c2: cand2, match_kind: pmatch::original)) |
| 13326 | { |
| 13327 | struct z_candidate *w, *l; |
| 13328 | if (cand2->reversed ()) |
| 13329 | winner = 1, w = cand1, l = cand2; |
| 13330 | else |
| 13331 | winner = -1, w = cand2, l = cand1; |
| 13332 | if (warn) |
| 13333 | { |
| 13334 | auto_diagnostic_group d; |
| 13335 | if (pedwarn (input_location, 0, |
| 13336 | "C++20 says that these are ambiguous, " |
| 13337 | "even though the second is reversed:" )) |
| 13338 | { |
| 13339 | print_z_candidate (loc: input_location, |
| 13340 | N_("candidate 1:" ), candidate: w); |
| 13341 | print_z_candidate (loc: input_location, |
| 13342 | N_("candidate 2:" ), candidate: l); |
| 13343 | if (w->fn == l->fn |
| 13344 | && DECL_IOBJ_MEMBER_FUNCTION_P (w->fn) |
| 13345 | && (type_memfn_quals (TREE_TYPE (w->fn)) |
| 13346 | & TYPE_QUAL_CONST) == 0) |
| 13347 | { |
| 13348 | /* Suggest adding const to |
| 13349 | struct A { bool operator==(const A&); }; */ |
| 13350 | tree parmtype |
| 13351 | = FUNCTION_FIRST_USER_PARMTYPE (w->fn); |
| 13352 | parmtype = TREE_VALUE (parmtype); |
| 13353 | if (TYPE_REF_P (parmtype) |
| 13354 | && TYPE_READONLY (TREE_TYPE (parmtype)) |
| 13355 | && (same_type_ignoring_top_level_qualifiers_p |
| 13356 | (TREE_TYPE (parmtype), |
| 13357 | DECL_CONTEXT (w->fn)))) |
| 13358 | inform (DECL_SOURCE_LOCATION (w->fn), |
| 13359 | "try making the operator a %<const%> " |
| 13360 | "member function" ); |
| 13361 | } |
| 13362 | } |
| 13363 | } |
| 13364 | else |
| 13365 | add_warning (winner: w, loser: l); |
| 13366 | return winner; |
| 13367 | } |
| 13368 | |
| 13369 | winner = 0; |
| 13370 | goto tweak; |
| 13371 | } |
| 13372 | winner = comp; |
| 13373 | } |
| 13374 | } |
| 13375 | |
| 13376 | /* warn about confusing overload resolution for user-defined conversions, |
| 13377 | either between a constructor and a conversion op, or between two |
| 13378 | conversion ops. */ |
| 13379 | if ((complain & tf_warning) |
| 13380 | /* In C++17, the constructor might have been elided, which means that |
| 13381 | an originally null ->second_conv could become non-null. */ |
| 13382 | && winner && warn_conversion && cand1->second_conv && cand2->second_conv |
| 13383 | && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn)) |
| 13384 | && winner != compare_ics (ics1: cand1->second_conv, ics2: cand2->second_conv)) |
| 13385 | { |
| 13386 | struct z_candidate *w, *l; |
| 13387 | bool give_warning = false; |
| 13388 | |
| 13389 | if (winner == 1) |
| 13390 | w = cand1, l = cand2; |
| 13391 | else |
| 13392 | w = cand2, l = cand1; |
| 13393 | |
| 13394 | /* We don't want to complain about `X::operator T1 ()' |
| 13395 | beating `X::operator T2 () const', when T2 is a no less |
| 13396 | cv-qualified version of T1. */ |
| 13397 | if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn) |
| 13398 | && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn)) |
| 13399 | { |
| 13400 | tree t = TREE_TYPE (TREE_TYPE (l->fn)); |
| 13401 | tree f = TREE_TYPE (TREE_TYPE (w->fn)); |
| 13402 | |
| 13403 | if (TREE_CODE (t) == TREE_CODE (f) && INDIRECT_TYPE_P (t)) |
| 13404 | { |
| 13405 | t = TREE_TYPE (t); |
| 13406 | f = TREE_TYPE (f); |
| 13407 | } |
| 13408 | if (!comp_ptr_ttypes (t, f)) |
| 13409 | give_warning = true; |
| 13410 | } |
| 13411 | else |
| 13412 | give_warning = true; |
| 13413 | |
| 13414 | if (!give_warning) |
| 13415 | /*NOP*/; |
| 13416 | else if (warn) |
| 13417 | { |
| 13418 | tree source = source_type (t: w->convs[0]); |
| 13419 | if (INDIRECT_TYPE_P (source)) |
| 13420 | source = TREE_TYPE (source); |
| 13421 | auto_diagnostic_group d; |
| 13422 | if (warning (OPT_Wconversion, "choosing %qD over %qD" , w->fn, l->fn) |
| 13423 | && warning (OPT_Wconversion, " for conversion from %qH to %qI" , |
| 13424 | source, w->second_conv->type)) |
| 13425 | { |
| 13426 | inform (input_location, " because conversion sequence " |
| 13427 | "for the argument is better" ); |
| 13428 | } |
| 13429 | } |
| 13430 | else |
| 13431 | add_warning (winner: w, loser: l); |
| 13432 | } |
| 13433 | |
| 13434 | if (winner) |
| 13435 | return winner; |
| 13436 | |
| 13437 | /* DR 495 moved this tiebreaker above the template ones. */ |
| 13438 | /* or, if not that, |
| 13439 | the context is an initialization by user-defined conversion (see |
| 13440 | _dcl.init_ and _over.match.user_) and the standard conversion |
| 13441 | sequence from the return type of F1 to the destination type (i.e., |
| 13442 | the type of the entity being initialized) is a better conversion |
| 13443 | sequence than the standard conversion sequence from the return type |
| 13444 | of F2 to the destination type. */ |
| 13445 | |
| 13446 | if (cand1->second_conv) |
| 13447 | { |
| 13448 | winner = compare_ics (ics1: cand1->second_conv, ics2: cand2->second_conv); |
| 13449 | if (winner) |
| 13450 | return winner; |
| 13451 | } |
| 13452 | |
| 13453 | /* CWG2735 (PR109247): A copy/move ctor/op= for which its operand uses an |
| 13454 | explicit conversion (due to list-initialization) is worse. */ |
| 13455 | { |
| 13456 | z_candidate *sp = nullptr; |
| 13457 | if (sfk_copy_or_move (fn: cand1->fn)) |
| 13458 | sp = cand1; |
| 13459 | if (sfk_copy_or_move (fn: cand2->fn)) |
| 13460 | sp = sp ? nullptr : cand2; |
| 13461 | if (sp) |
| 13462 | { |
| 13463 | conversion *conv = sp->convs[!DECL_CONSTRUCTOR_P (sp->fn)]; |
| 13464 | if (conv->user_conv_p) |
| 13465 | for (; conv; conv = next_conversion (conv)) |
| 13466 | if (conv->kind == ck_user |
| 13467 | && DECL_P (conv->cand->fn) |
| 13468 | && DECL_NONCONVERTING_P (conv->cand->fn)) |
| 13469 | return (sp == cand1) ? -1 : 1; |
| 13470 | } |
| 13471 | } |
| 13472 | |
| 13473 | /* DR2327: C++17 copy elision in [over.match.ctor] (direct-init) context. |
| 13474 | The standard currently says that only constructors are candidates, but if |
| 13475 | one copies a prvalue returned by a conversion function we prefer that. |
| 13476 | |
| 13477 | Clang does something similar, as discussed at |
| 13478 | http://lists.isocpp.org/core/2017/10/3166.php |
| 13479 | http://lists.isocpp.org/core/2019/03/5721.php */ |
| 13480 | if (len == 1 && cxx_dialect >= cxx17 |
| 13481 | && DECL_P (cand1->fn) |
| 13482 | && DECL_COMPLETE_CONSTRUCTOR_P (cand1->fn) |
| 13483 | && !(cand1->flags & LOOKUP_ONLYCONVERTING)) |
| 13484 | { |
| 13485 | bool elided1 = joust_maybe_elide_copy (cand: cand1); |
| 13486 | bool elided2 = joust_maybe_elide_copy (cand: cand2); |
| 13487 | winner = elided1 - elided2; |
| 13488 | if (winner) |
| 13489 | return winner; |
| 13490 | } |
| 13491 | |
| 13492 | /* or, if not that, |
| 13493 | F1 is a non-template function and F2 is a template function |
| 13494 | specialization. */ |
| 13495 | |
| 13496 | if (!cand1->template_decl && cand2->template_decl) |
| 13497 | return 1; |
| 13498 | else if (cand1->template_decl && !cand2->template_decl) |
| 13499 | return -1; |
| 13500 | |
| 13501 | /* or, if not that, |
| 13502 | F1 and F2 are template functions and the function template for F1 is |
| 13503 | more specialized than the template for F2 according to the partial |
| 13504 | ordering rules. */ |
| 13505 | |
| 13506 | if (cand1->template_decl && cand2->template_decl) |
| 13507 | { |
| 13508 | winner = more_specialized_fn |
| 13509 | (TI_TEMPLATE (cand1->template_decl), |
| 13510 | TI_TEMPLATE (cand2->template_decl), |
| 13511 | /* [temp.func.order]: The presence of unused ellipsis and default |
| 13512 | arguments has no effect on the partial ordering of function |
| 13513 | templates. add_function_candidate() will not have |
| 13514 | counted the "this" argument for constructors. */ |
| 13515 | cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn)); |
| 13516 | if (winner) |
| 13517 | return winner; |
| 13518 | } |
| 13519 | |
| 13520 | /* F1 and F2 are non-template functions and |
| 13521 | - they have the same non-object-parameter-type-lists ([dcl.fct]), and |
| 13522 | - if they are member functions, both are direct members of the same |
| 13523 | class, and |
| 13524 | - if both are non-static member functions, they have the same types for |
| 13525 | their object parameters, and |
| 13526 | - F1 is more constrained than F2 according to the partial ordering of |
| 13527 | constraints described in [temp.constr.order]. */ |
| 13528 | if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn) |
| 13529 | && !cand1->template_decl && !cand2->template_decl |
| 13530 | && cand_parms_match (c1: cand1, c2: cand2, match_kind: pmatch::current)) |
| 13531 | { |
| 13532 | winner = more_constrained (cand1->fn, cand2->fn); |
| 13533 | if (winner) |
| 13534 | return winner; |
| 13535 | } |
| 13536 | |
| 13537 | /* F2 is a rewritten candidate (12.4.1.2) and F1 is not, or F1 and F2 are |
| 13538 | rewritten candidates, and F2 is a synthesized candidate with reversed |
| 13539 | order of parameters and F1 is not. */ |
| 13540 | if (cand1->rewritten ()) |
| 13541 | { |
| 13542 | if (!cand2->rewritten ()) |
| 13543 | return -1; |
| 13544 | if (!cand1->reversed () && cand2->reversed ()) |
| 13545 | return 1; |
| 13546 | if (cand1->reversed () && !cand2->reversed ()) |
| 13547 | return -1; |
| 13548 | } |
| 13549 | else if (cand2->rewritten ()) |
| 13550 | return 1; |
| 13551 | |
| 13552 | if (deduction_guide_p (cand1->fn)) |
| 13553 | { |
| 13554 | gcc_assert (deduction_guide_p (cand2->fn)); |
| 13555 | |
| 13556 | /* F1 and F2 are generated from class template argument deduction for a |
| 13557 | class D, and F2 is generated from inheriting constructors from a base |
| 13558 | class of D while F1 is not, and for each explicit function argument, |
| 13559 | the corresponding parameters of F1 and F2 are either both ellipses or |
| 13560 | have the same type. */ |
| 13561 | bool inherited1 = inherited_guide_p (cand1->fn); |
| 13562 | bool inherited2 = inherited_guide_p (cand2->fn); |
| 13563 | if (int diff = inherited2 - inherited1) |
| 13564 | { |
| 13565 | for (i = 0; i < len; ++i) |
| 13566 | { |
| 13567 | conversion *t1 = cand1->convs[i + off1]; |
| 13568 | conversion *t2 = cand2->convs[i + off2]; |
| 13569 | /* ??? It seems the ellipses part of this tiebreaker isn't |
| 13570 | needed since a mismatch should have broken the tie earlier |
| 13571 | during ICS comparison. */ |
| 13572 | gcc_checking_assert (t1->ellipsis_p == t2->ellipsis_p); |
| 13573 | if (!same_type_p (t1->type, t2->type)) |
| 13574 | break; |
| 13575 | } |
| 13576 | if (i == len) |
| 13577 | return diff; |
| 13578 | } |
| 13579 | |
| 13580 | /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */ |
| 13581 | /* We distinguish between candidates from an explicit deduction guide and |
| 13582 | candidates built from a constructor based on DECL_ARTIFICIAL. */ |
| 13583 | int art1 = DECL_ARTIFICIAL (cand1->fn); |
| 13584 | int art2 = DECL_ARTIFICIAL (cand2->fn); |
| 13585 | if (art1 != art2) |
| 13586 | return art2 - art1; |
| 13587 | |
| 13588 | if (art1) |
| 13589 | { |
| 13590 | /* Prefer the special copy guide over a declared copy/move |
| 13591 | constructor. */ |
| 13592 | if (copy_guide_p (cand1->fn)) |
| 13593 | return 1; |
| 13594 | if (copy_guide_p (cand2->fn)) |
| 13595 | return -1; |
| 13596 | |
| 13597 | /* Prefer a candidate generated from a non-template constructor. */ |
| 13598 | int tg1 = template_guide_p (cand1->fn); |
| 13599 | int tg2 = template_guide_p (cand2->fn); |
| 13600 | if (tg1 != tg2) |
| 13601 | return tg2 - tg1; |
| 13602 | } |
| 13603 | } |
| 13604 | |
| 13605 | /* F1 is a constructor for a class D, F2 is a constructor for a base class B |
| 13606 | of D, and for all arguments the corresponding parameters of F1 and F2 have |
| 13607 | the same type (CWG 2273/2277). */ |
| 13608 | if (DECL_INHERITED_CTOR (cand1->fn) || DECL_INHERITED_CTOR (cand2->fn)) |
| 13609 | { |
| 13610 | tree base1 = DECL_CONTEXT (strip_inheriting_ctors (cand1->fn)); |
| 13611 | tree base2 = DECL_CONTEXT (strip_inheriting_ctors (cand2->fn)); |
| 13612 | |
| 13613 | bool used1 = false; |
| 13614 | bool used2 = false; |
| 13615 | if (base1 == base2) |
| 13616 | /* No difference. */; |
| 13617 | else if (DERIVED_FROM_P (base1, base2)) |
| 13618 | used1 = true; |
| 13619 | else if (DERIVED_FROM_P (base2, base1)) |
| 13620 | used2 = true; |
| 13621 | |
| 13622 | if (int diff = used2 - used1) |
| 13623 | { |
| 13624 | for (i = 0; i < len; ++i) |
| 13625 | { |
| 13626 | conversion *t1 = cand1->convs[i + off1]; |
| 13627 | conversion *t2 = cand2->convs[i + off2]; |
| 13628 | if (!same_type_p (t1->type, t2->type)) |
| 13629 | break; |
| 13630 | } |
| 13631 | if (i == len) |
| 13632 | return diff; |
| 13633 | } |
| 13634 | } |
| 13635 | |
| 13636 | /* Check whether we can discard a builtin candidate, either because we |
| 13637 | have two identical ones or matching builtin and non-builtin candidates. |
| 13638 | |
| 13639 | (Pedantically in the latter case the builtin which matched the user |
| 13640 | function should not be added to the overload set, but we spot it here. |
| 13641 | |
| 13642 | [over.match.oper] |
| 13643 | ... the builtin candidates include ... |
| 13644 | - do not have the same parameter type list as any non-template |
| 13645 | non-member candidate. */ |
| 13646 | |
| 13647 | if (identifier_p (t: cand1->fn) || identifier_p (t: cand2->fn)) |
| 13648 | { |
| 13649 | for (i = 0; i < len; ++i) |
| 13650 | if (!same_type_p (cand1->convs[i]->type, |
| 13651 | cand2->convs[i]->type)) |
| 13652 | break; |
| 13653 | if (i == cand1->num_convs) |
| 13654 | { |
| 13655 | if (cand1->fn == cand2->fn) |
| 13656 | /* Two built-in candidates; arbitrarily pick one. */ |
| 13657 | return 1; |
| 13658 | else if (identifier_p (t: cand1->fn)) |
| 13659 | /* cand1 is built-in; prefer cand2. */ |
| 13660 | return -1; |
| 13661 | else |
| 13662 | /* cand2 is built-in; prefer cand1. */ |
| 13663 | return 1; |
| 13664 | } |
| 13665 | } |
| 13666 | |
| 13667 | /* For candidates of a multi-versioned function, make the version with |
| 13668 | the highest priority win. This version will be checked for dispatching |
| 13669 | first. If this version can be inlined into the caller, the front-end |
| 13670 | will simply make a direct call to this function. */ |
| 13671 | |
| 13672 | if (TREE_CODE (cand1->fn) == FUNCTION_DECL |
| 13673 | && DECL_FUNCTION_VERSIONED (cand1->fn) |
| 13674 | && TREE_CODE (cand2->fn) == FUNCTION_DECL |
| 13675 | && DECL_FUNCTION_VERSIONED (cand2->fn)) |
| 13676 | { |
| 13677 | tree f1 = TREE_TYPE (cand1->fn); |
| 13678 | tree f2 = TREE_TYPE (cand2->fn); |
| 13679 | tree p1 = TYPE_ARG_TYPES (f1); |
| 13680 | tree p2 = TYPE_ARG_TYPES (f2); |
| 13681 | |
| 13682 | /* Check if cand1->fn and cand2->fn are versions of the same function. It |
| 13683 | is possible that cand1->fn and cand2->fn are function versions but of |
| 13684 | different functions. Check types to see if they are versions of the same |
| 13685 | function. */ |
| 13686 | if (compparms (p1, p2) |
| 13687 | && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2))) |
| 13688 | { |
| 13689 | /* Always make the version with the higher priority, more |
| 13690 | specialized, win. */ |
| 13691 | gcc_assert (targetm.compare_version_priority); |
| 13692 | if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0) |
| 13693 | return 1; |
| 13694 | else |
| 13695 | return -1; |
| 13696 | } |
| 13697 | } |
| 13698 | |
| 13699 | /* If the two function declarations represent the same function (this can |
| 13700 | happen with declarations in multiple scopes and arg-dependent lookup), |
| 13701 | arbitrarily choose one. But first make sure the default args we're |
| 13702 | using match. */ |
| 13703 | if (DECL_P (cand1->fn) && DECL_P (cand2->fn) |
| 13704 | && equal_functions (fn1: cand1->fn, fn2: cand2->fn)) |
| 13705 | { |
| 13706 | tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn)); |
| 13707 | tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn)); |
| 13708 | |
| 13709 | gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn)); |
| 13710 | |
| 13711 | for (i = 0; i < len; ++i) |
| 13712 | { |
| 13713 | /* Don't crash if the fn is variadic. */ |
| 13714 | if (!parms1) |
| 13715 | break; |
| 13716 | parms1 = TREE_CHAIN (parms1); |
| 13717 | parms2 = TREE_CHAIN (parms2); |
| 13718 | } |
| 13719 | |
| 13720 | if (off1) |
| 13721 | parms1 = TREE_CHAIN (parms1); |
| 13722 | else if (off2) |
| 13723 | parms2 = TREE_CHAIN (parms2); |
| 13724 | |
| 13725 | for (; parms1; ++i) |
| 13726 | { |
| 13727 | if (!cp_tree_equal (TREE_PURPOSE (parms1), |
| 13728 | TREE_PURPOSE (parms2))) |
| 13729 | { |
| 13730 | if (warn) |
| 13731 | { |
| 13732 | if (complain & tf_error) |
| 13733 | { |
| 13734 | auto_diagnostic_group d; |
| 13735 | if (permerror (input_location, |
| 13736 | "default argument mismatch in " |
| 13737 | "overload resolution" )) |
| 13738 | { |
| 13739 | inform (DECL_SOURCE_LOCATION (cand1->fn), |
| 13740 | " candidate 1: %q#F" , cand1->fn); |
| 13741 | inform (DECL_SOURCE_LOCATION (cand2->fn), |
| 13742 | " candidate 2: %q#F" , cand2->fn); |
| 13743 | } |
| 13744 | } |
| 13745 | else |
| 13746 | return 0; |
| 13747 | } |
| 13748 | else |
| 13749 | add_warning (winner: cand1, loser: cand2); |
| 13750 | break; |
| 13751 | } |
| 13752 | parms1 = TREE_CHAIN (parms1); |
| 13753 | parms2 = TREE_CHAIN (parms2); |
| 13754 | } |
| 13755 | |
| 13756 | return 1; |
| 13757 | } |
| 13758 | |
| 13759 | tweak: |
| 13760 | |
| 13761 | /* Extension: If the worst conversion for one candidate is better than the |
| 13762 | worst conversion for the other, take the first. */ |
| 13763 | if (!pedantic && (complain & tf_warning_or_error)) |
| 13764 | { |
| 13765 | conversion_rank rank1 = cr_identity, rank2 = cr_identity; |
| 13766 | struct z_candidate *w = 0, *l = 0; |
| 13767 | |
| 13768 | for (i = 0; i < len; ++i) |
| 13769 | { |
| 13770 | if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1) |
| 13771 | rank1 = CONVERSION_RANK (cand1->convs[i+off1]); |
| 13772 | if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2) |
| 13773 | rank2 = CONVERSION_RANK (cand2->convs[i + off2]); |
| 13774 | } |
| 13775 | if (rank1 < rank2) |
| 13776 | winner = 1, w = cand1, l = cand2; |
| 13777 | if (rank1 > rank2) |
| 13778 | winner = -1, w = cand2, l = cand1; |
| 13779 | if (winner) |
| 13780 | { |
| 13781 | /* Don't choose a deleted function over ambiguity. */ |
| 13782 | if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn)) |
| 13783 | return 0; |
| 13784 | if (warn) |
| 13785 | { |
| 13786 | auto_diagnostic_group d; |
| 13787 | if (pedwarn (input_location, 0, |
| 13788 | "ISO C++ says that these are ambiguous, even " |
| 13789 | "though the worst conversion for the first is " |
| 13790 | "better than the worst conversion for the second:" )) |
| 13791 | { |
| 13792 | print_z_candidate (loc: input_location, N_("candidate 1:" ), candidate: w); |
| 13793 | print_z_candidate (loc: input_location, N_("candidate 2:" ), candidate: l); |
| 13794 | } |
| 13795 | } |
| 13796 | else |
| 13797 | add_warning (winner: w, loser: l); |
| 13798 | return winner; |
| 13799 | } |
| 13800 | } |
| 13801 | |
| 13802 | gcc_assert (!winner); |
| 13803 | return 0; |
| 13804 | } |
| 13805 | |
| 13806 | /* Given a list of candidates for overloading, find the best one, if any. |
| 13807 | This algorithm has a worst case of O(2n) (winner is last), and a best |
| 13808 | case of O(n/2) (totally ambiguous); much better than a sorting |
| 13809 | algorithm. The candidates list is assumed to be sorted according |
| 13810 | to viability (via splice_viable). */ |
| 13811 | |
| 13812 | static struct z_candidate * |
| 13813 | tourney (struct z_candidate *candidates, tsubst_flags_t complain) |
| 13814 | { |
| 13815 | struct z_candidate **champ = &candidates, **challenger; |
| 13816 | int fate; |
| 13817 | struct z_candidate *previous_worse_champ = nullptr; |
| 13818 | |
| 13819 | /* Walk through the list once, comparing each current champ to the next |
| 13820 | candidate, knocking out a candidate or two with each comparison. */ |
| 13821 | |
| 13822 | for (challenger = &candidates->next; *challenger && (*challenger)->viable; ) |
| 13823 | { |
| 13824 | fate = joust (cand1: *champ, cand2: *challenger, warn: 0, complain); |
| 13825 | if (fate == 1) |
| 13826 | challenger = &(*challenger)->next; |
| 13827 | else if (fate == -1) |
| 13828 | { |
| 13829 | previous_worse_champ = *champ; |
| 13830 | champ = challenger; |
| 13831 | challenger = &(*challenger)->next; |
| 13832 | } |
| 13833 | else |
| 13834 | { |
| 13835 | previous_worse_champ = nullptr; |
| 13836 | champ = &(*challenger)->next; |
| 13837 | if (!*champ || !(*champ)->viable |
| 13838 | || (*champ)->viable < (*challenger)->viable) |
| 13839 | { |
| 13840 | champ = nullptr; |
| 13841 | break; |
| 13842 | } |
| 13843 | challenger = &(*champ)->next; |
| 13844 | } |
| 13845 | } |
| 13846 | |
| 13847 | /* Make sure the champ is better than all the candidates it hasn't yet |
| 13848 | been compared to. */ |
| 13849 | |
| 13850 | if (champ) |
| 13851 | for (challenger = &candidates; |
| 13852 | challenger != champ; |
| 13853 | challenger = &(*challenger)->next) |
| 13854 | { |
| 13855 | if (*challenger == previous_worse_champ) |
| 13856 | /* We already know this candidate is worse than the champ. */ |
| 13857 | continue; |
| 13858 | fate = joust (cand1: *champ, cand2: *challenger, warn: 0, complain); |
| 13859 | if (fate != 1) |
| 13860 | { |
| 13861 | champ = nullptr; |
| 13862 | break; |
| 13863 | } |
| 13864 | } |
| 13865 | |
| 13866 | if (!champ) |
| 13867 | return nullptr; |
| 13868 | |
| 13869 | /* Move the champ to the front of the candidate list. */ |
| 13870 | |
| 13871 | if (champ != &candidates) |
| 13872 | { |
| 13873 | z_candidate *saved_champ = *champ; |
| 13874 | *champ = saved_champ->next; |
| 13875 | saved_champ->next = candidates; |
| 13876 | candidates = saved_champ; |
| 13877 | } |
| 13878 | |
| 13879 | return candidates; |
| 13880 | } |
| 13881 | |
| 13882 | /* Returns nonzero if things of type FROM can be converted to TO. */ |
| 13883 | |
| 13884 | bool |
| 13885 | can_convert (tree to, tree from, tsubst_flags_t complain) |
| 13886 | { |
| 13887 | tree arg = NULL_TREE; |
| 13888 | /* implicit_conversion only considers user-defined conversions |
| 13889 | if it has an expression for the call argument list. */ |
| 13890 | if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to)) |
| 13891 | arg = build_stub_object (from); |
| 13892 | return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain); |
| 13893 | } |
| 13894 | |
| 13895 | /* Returns nonzero if things of type FROM can be converted to TO with a |
| 13896 | standard conversion. */ |
| 13897 | |
| 13898 | bool |
| 13899 | can_convert_standard (tree to, tree from, tsubst_flags_t complain) |
| 13900 | { |
| 13901 | return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain); |
| 13902 | } |
| 13903 | |
| 13904 | /* Returns nonzero if ARG (of type FROM) can be converted to TO. */ |
| 13905 | |
| 13906 | bool |
| 13907 | can_convert_arg (tree to, tree from, tree arg, int flags, |
| 13908 | tsubst_flags_t complain) |
| 13909 | { |
| 13910 | conversion *t; |
| 13911 | bool ok_p; |
| 13912 | |
| 13913 | conversion_obstack_sentinel cos; |
| 13914 | /* We want to discard any access checks done for this test, |
| 13915 | as we might not be in the appropriate access context and |
| 13916 | we'll do the check again when we actually perform the |
| 13917 | conversion. */ |
| 13918 | push_deferring_access_checks (dk_deferred); |
| 13919 | |
| 13920 | /* Handle callers like check_local_shadow forgetting to |
| 13921 | convert_from_reference. */ |
| 13922 | if (TYPE_REF_P (from) && arg) |
| 13923 | { |
| 13924 | arg = convert_from_reference (arg); |
| 13925 | from = TREE_TYPE (arg); |
| 13926 | } |
| 13927 | |
| 13928 | t = implicit_conversion (to, from, expr: arg, /*c_cast_p=*/false, |
| 13929 | flags, complain); |
| 13930 | ok_p = (t && !t->bad_p); |
| 13931 | |
| 13932 | /* Discard the access checks now. */ |
| 13933 | pop_deferring_access_checks (); |
| 13934 | |
| 13935 | return ok_p; |
| 13936 | } |
| 13937 | |
| 13938 | /* Like can_convert_arg, but allows dubious conversions as well. */ |
| 13939 | |
| 13940 | bool |
| 13941 | can_convert_arg_bad (tree to, tree from, tree arg, int flags, |
| 13942 | tsubst_flags_t complain) |
| 13943 | { |
| 13944 | conversion *t; |
| 13945 | |
| 13946 | conversion_obstack_sentinel cos; |
| 13947 | /* Try to perform the conversion. */ |
| 13948 | t = implicit_conversion (to, from, expr: arg, /*c_cast_p=*/false, |
| 13949 | flags, complain); |
| 13950 | |
| 13951 | return t != NULL; |
| 13952 | } |
| 13953 | |
| 13954 | /* Return an IMPLICIT_CONV_EXPR from EXPR to TYPE with bits set from overload |
| 13955 | resolution FLAGS. */ |
| 13956 | |
| 13957 | tree |
| 13958 | build_implicit_conv_flags (tree type, tree expr, int flags) |
| 13959 | { |
| 13960 | /* In a template, we are only concerned about determining the |
| 13961 | type of non-dependent expressions, so we do not have to |
| 13962 | perform the actual conversion. But for initializers, we |
| 13963 | need to be able to perform it at instantiation |
| 13964 | (or instantiate_non_dependent_expr) time. */ |
| 13965 | expr = build1 (IMPLICIT_CONV_EXPR, type, expr); |
| 13966 | if (!(flags & LOOKUP_ONLYCONVERTING)) |
| 13967 | IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true; |
| 13968 | if (flags & LOOKUP_NO_NARROWING) |
| 13969 | IMPLICIT_CONV_EXPR_BRACED_INIT (expr) = true; |
| 13970 | return expr; |
| 13971 | } |
| 13972 | |
| 13973 | /* Convert EXPR to TYPE. Return the converted expression. |
| 13974 | |
| 13975 | Note that we allow bad conversions here because by the time we get to |
| 13976 | this point we are committed to doing the conversion. If we end up |
| 13977 | doing a bad conversion, convert_like will complain. */ |
| 13978 | |
| 13979 | tree |
| 13980 | perform_implicit_conversion_flags (tree type, tree expr, |
| 13981 | tsubst_flags_t complain, int flags) |
| 13982 | { |
| 13983 | conversion *conv; |
| 13984 | location_t loc = cp_expr_loc_or_input_loc (t: expr); |
| 13985 | |
| 13986 | if (error_operand_p (t: expr)) |
| 13987 | return error_mark_node; |
| 13988 | |
| 13989 | conversion_obstack_sentinel cos; |
| 13990 | |
| 13991 | conv = implicit_conversion (to: type, TREE_TYPE (expr), expr, |
| 13992 | /*c_cast_p=*/false, |
| 13993 | flags, complain); |
| 13994 | |
| 13995 | if (!conv) |
| 13996 | { |
| 13997 | if (complain & tf_error) |
| 13998 | implicit_conversion_error (loc, type, expr); |
| 13999 | expr = error_mark_node; |
| 14000 | } |
| 14001 | else if (processing_template_decl && conv->kind != ck_identity) |
| 14002 | expr = build_implicit_conv_flags (type, expr, flags); |
| 14003 | else |
| 14004 | { |
| 14005 | /* Give a conversion call the same location as expr. */ |
| 14006 | iloc_sentinel il (loc); |
| 14007 | expr = convert_like (convs: conv, expr, complain); |
| 14008 | } |
| 14009 | |
| 14010 | return expr; |
| 14011 | } |
| 14012 | |
| 14013 | tree |
| 14014 | perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain) |
| 14015 | { |
| 14016 | return perform_implicit_conversion_flags (type, expr, complain, |
| 14017 | LOOKUP_IMPLICIT); |
| 14018 | } |
| 14019 | |
| 14020 | /* Convert EXPR to TYPE (as a direct-initialization) if that is |
| 14021 | permitted. If the conversion is valid, the converted expression is |
| 14022 | returned. Otherwise, NULL_TREE is returned, except in the case |
| 14023 | that TYPE is a class type; in that case, an error is issued. If |
| 14024 | C_CAST_P is true, then this direct-initialization is taking |
| 14025 | place as part of a static_cast being attempted as part of a C-style |
| 14026 | cast. */ |
| 14027 | |
| 14028 | tree |
| 14029 | perform_direct_initialization_if_possible (tree type, |
| 14030 | tree expr, |
| 14031 | bool c_cast_p, |
| 14032 | tsubst_flags_t complain) |
| 14033 | { |
| 14034 | conversion *conv; |
| 14035 | |
| 14036 | if (type == error_mark_node || error_operand_p (t: expr)) |
| 14037 | return error_mark_node; |
| 14038 | /* [dcl.init] |
| 14039 | |
| 14040 | If the destination type is a (possibly cv-qualified) class type: |
| 14041 | |
| 14042 | -- If the initialization is direct-initialization ..., |
| 14043 | constructors are considered. |
| 14044 | |
| 14045 | -- If overload resolution is successful, the selected constructor |
| 14046 | is called to initialize the object, with the initializer expression |
| 14047 | or expression-list as its argument(s). |
| 14048 | |
| 14049 | -- Otherwise, if no constructor is viable, the destination type is |
| 14050 | a (possibly cv-qualified) aggregate class A, and the initializer is |
| 14051 | a parenthesized expression-list, the object is initialized as |
| 14052 | follows... */ |
| 14053 | if (CLASS_TYPE_P (type)) |
| 14054 | { |
| 14055 | releasing_vec args (make_tree_vector_single (expr)); |
| 14056 | expr = build_special_member_call (NULL_TREE, complete_ctor_identifier, |
| 14057 | args: &args, binfo: type, LOOKUP_NORMAL, complain); |
| 14058 | return build_cplus_new (type, expr, complain); |
| 14059 | } |
| 14060 | |
| 14061 | conversion_obstack_sentinel cos; |
| 14062 | |
| 14063 | conv = implicit_conversion (to: type, TREE_TYPE (expr), expr, |
| 14064 | c_cast_p, |
| 14065 | LOOKUP_NORMAL, complain); |
| 14066 | if (!conv || conv->bad_p) |
| 14067 | expr = NULL_TREE; |
| 14068 | else if (processing_template_decl && conv->kind != ck_identity) |
| 14069 | { |
| 14070 | /* In a template, we are only concerned about determining the |
| 14071 | type of non-dependent expressions, so we do not have to |
| 14072 | perform the actual conversion. But for initializers, we |
| 14073 | need to be able to perform it at instantiation |
| 14074 | (or instantiate_non_dependent_expr) time. */ |
| 14075 | expr = build1 (IMPLICIT_CONV_EXPR, type, expr); |
| 14076 | IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true; |
| 14077 | } |
| 14078 | else |
| 14079 | expr = convert_like (convs: conv, expr, NULL_TREE, argnum: 0, |
| 14080 | /*issue_conversion_warnings=*/false, |
| 14081 | c_cast_p, /*nested_p=*/false, complain); |
| 14082 | |
| 14083 | return expr; |
| 14084 | } |
| 14085 | |
| 14086 | /* When initializing a reference that lasts longer than a full-expression, |
| 14087 | this special rule applies: |
| 14088 | |
| 14089 | [class.temporary] |
| 14090 | |
| 14091 | The temporary to which the reference is bound or the temporary |
| 14092 | that is the complete object to which the reference is bound |
| 14093 | persists for the lifetime of the reference. |
| 14094 | |
| 14095 | The temporaries created during the evaluation of the expression |
| 14096 | initializing the reference, except the temporary to which the |
| 14097 | reference is bound, are destroyed at the end of the |
| 14098 | full-expression in which they are created. |
| 14099 | |
| 14100 | In that case, we store the converted expression into a new |
| 14101 | VAR_DECL in a new scope. |
| 14102 | |
| 14103 | However, we want to be careful not to create temporaries when |
| 14104 | they are not required. For example, given: |
| 14105 | |
| 14106 | struct B {}; |
| 14107 | struct D : public B {}; |
| 14108 | D f(); |
| 14109 | const B& b = f(); |
| 14110 | |
| 14111 | there is no need to copy the return value from "f"; we can just |
| 14112 | extend its lifetime. Similarly, given: |
| 14113 | |
| 14114 | struct S {}; |
| 14115 | struct T { operator S(); }; |
| 14116 | T t; |
| 14117 | const S& s = t; |
| 14118 | |
| 14119 | we can extend the lifetime of the return value of the conversion |
| 14120 | operator. |
| 14121 | |
| 14122 | The next several functions are involved in this lifetime extension. */ |
| 14123 | |
| 14124 | /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The |
| 14125 | reference is being bound to a temporary. Create and return a new |
| 14126 | VAR_DECL with the indicated TYPE; this variable will store the value to |
| 14127 | which the reference is bound. */ |
| 14128 | |
| 14129 | tree |
| 14130 | make_temporary_var_for_ref_to_temp (tree decl, tree type) |
| 14131 | { |
| 14132 | tree var = create_temporary_var (type); |
| 14133 | |
| 14134 | /* Register the variable. */ |
| 14135 | if (VAR_P (decl) |
| 14136 | && (TREE_STATIC (decl) || CP_DECL_THREAD_LOCAL_P (decl))) |
| 14137 | { |
| 14138 | /* Namespace-scope or local static; give it a mangled name. */ |
| 14139 | |
| 14140 | /* If an initializer is visible to multiple translation units, those |
| 14141 | translation units must agree on the addresses of the |
| 14142 | temporaries. Therefore the temporaries must be given a consistent name |
| 14143 | and vague linkage. The mangled name of a temporary is the name of the |
| 14144 | non-temporary object in whose initializer they appear, prefixed with |
| 14145 | GR and suffixed with a sequence number mangled using the usual rules |
| 14146 | for a seq-id. Temporaries are numbered with a pre-order, depth-first, |
| 14147 | left-to-right walk of the complete initializer. */ |
| 14148 | copy_linkage (var, decl); |
| 14149 | |
| 14150 | tree name = mangle_ref_init_variable (decl); |
| 14151 | DECL_NAME (var) = name; |
| 14152 | SET_DECL_ASSEMBLER_NAME (var, name); |
| 14153 | |
| 14154 | /* Set the context to make the variable mergeable in modules. */ |
| 14155 | DECL_CONTEXT (var) = current_scope (); |
| 14156 | } |
| 14157 | else |
| 14158 | /* Create a new cleanup level if necessary. */ |
| 14159 | maybe_push_cleanup_level (type); |
| 14160 | |
| 14161 | return pushdecl (var); |
| 14162 | } |
| 14163 | |
| 14164 | static tree extend_temps_r (tree *, int *, void *); |
| 14165 | |
| 14166 | /* EXPR is the initializer for a variable DECL of reference or |
| 14167 | std::initializer_list type. Create, push and return a new VAR_DECL |
| 14168 | for the initializer so that it will live as long as DECL. Any |
| 14169 | cleanup for the new variable is returned through CLEANUP, and the |
| 14170 | code to initialize the new variable is returned through INITP. */ |
| 14171 | |
| 14172 | static tree |
| 14173 | set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups, |
| 14174 | tree *initp, tree *cond_guard, |
| 14175 | void *walk_data) |
| 14176 | { |
| 14177 | tree init; |
| 14178 | tree type; |
| 14179 | tree var; |
| 14180 | |
| 14181 | /* Create the temporary variable. */ |
| 14182 | type = TREE_TYPE (expr); |
| 14183 | var = make_temporary_var_for_ref_to_temp (decl, type); |
| 14184 | layout_decl (var, 0); |
| 14185 | /* If the rvalue is the result of a function call it will be |
| 14186 | a TARGET_EXPR. If it is some other construct (such as a |
| 14187 | member access expression where the underlying object is |
| 14188 | itself the result of a function call), turn it into a |
| 14189 | TARGET_EXPR here. It is important that EXPR be a |
| 14190 | TARGET_EXPR below since otherwise the INIT_EXPR will |
| 14191 | attempt to make a bitwise copy of EXPR to initialize |
| 14192 | VAR. */ |
| 14193 | if (TREE_CODE (expr) != TARGET_EXPR) |
| 14194 | expr = get_target_expr (expr); |
| 14195 | else |
| 14196 | { |
| 14197 | if (TREE_ADDRESSABLE (expr)) |
| 14198 | TREE_ADDRESSABLE (var) = 1; |
| 14199 | if (DECL_MERGEABLE (TARGET_EXPR_SLOT (expr))) |
| 14200 | DECL_MERGEABLE (var) = true; |
| 14201 | } |
| 14202 | |
| 14203 | if (TREE_CODE (decl) == FIELD_DECL |
| 14204 | && extra_warnings && !warning_suppressed_p (decl)) |
| 14205 | { |
| 14206 | warning (OPT_Wextra, "a temporary bound to %qD only persists " |
| 14207 | "until the constructor exits" , decl); |
| 14208 | suppress_warning (decl); |
| 14209 | } |
| 14210 | |
| 14211 | /* Recursively extend temps in this initializer. The recursion needs to come |
| 14212 | after creating the variable to conform to the mangling ABI, and before |
| 14213 | maybe_constant_init because the extension might change its result. */ |
| 14214 | if (walk_data) |
| 14215 | cp_walk_tree (&TARGET_EXPR_INITIAL (expr), extend_temps_r, |
| 14216 | walk_data, nullptr); |
| 14217 | else |
| 14218 | TARGET_EXPR_INITIAL (expr) |
| 14219 | = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups, |
| 14220 | cond_guard); |
| 14221 | |
| 14222 | /* Any reference temp has a non-trivial initializer. */ |
| 14223 | DECL_NONTRIVIALLY_INITIALIZED_P (var) = true; |
| 14224 | |
| 14225 | /* If the initializer is constant, put it in DECL_INITIAL so we get |
| 14226 | static initialization and use in constant expressions. */ |
| 14227 | init = maybe_constant_init (expr, var, /*manifestly_const_eval=*/true); |
| 14228 | /* As in store_init_value. */ |
| 14229 | init = cp_fully_fold (init); |
| 14230 | if (TREE_CONSTANT (init)) |
| 14231 | { |
| 14232 | if (literal_type_p (type) |
| 14233 | && CP_TYPE_CONST_NON_VOLATILE_P (type) |
| 14234 | && !TYPE_HAS_MUTABLE_P (type)) |
| 14235 | { |
| 14236 | /* 5.19 says that a constant expression can include an |
| 14237 | lvalue-rvalue conversion applied to "a glvalue of literal type |
| 14238 | that refers to a non-volatile temporary object initialized |
| 14239 | with a constant expression". Rather than try to communicate |
| 14240 | that this VAR_DECL is a temporary, just mark it constexpr. */ |
| 14241 | DECL_DECLARED_CONSTEXPR_P (var) = true; |
| 14242 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true; |
| 14243 | TREE_CONSTANT (var) = true; |
| 14244 | TREE_READONLY (var) = true; |
| 14245 | } |
| 14246 | DECL_INITIAL (var) = init; |
| 14247 | init = NULL_TREE; |
| 14248 | } |
| 14249 | else |
| 14250 | /* Create the INIT_EXPR that will initialize the temporary |
| 14251 | variable. */ |
| 14252 | init = split_nonconstant_init (var, expr); |
| 14253 | if (at_function_scope_p ()) |
| 14254 | { |
| 14255 | add_decl_expr (var); |
| 14256 | |
| 14257 | if (TREE_STATIC (var)) |
| 14258 | init = add_stmt_to_compound (init, register_dtor_fn (var)); |
| 14259 | else |
| 14260 | { |
| 14261 | /* ??? Instead of rebuilding the cleanup, we could replace the slot |
| 14262 | with var in TARGET_EXPR_CLEANUP (expr). */ |
| 14263 | tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error); |
| 14264 | if (cleanup) |
| 14265 | { |
| 14266 | if (cond_guard && cleanup != error_mark_node) |
| 14267 | { |
| 14268 | if (*cond_guard == NULL_TREE) |
| 14269 | { |
| 14270 | *cond_guard = build_local_temp (boolean_type_node); |
| 14271 | add_decl_expr (*cond_guard); |
| 14272 | tree set = cp_build_modify_expr (UNKNOWN_LOCATION, |
| 14273 | *cond_guard, NOP_EXPR, |
| 14274 | boolean_false_node, |
| 14275 | tf_warning_or_error); |
| 14276 | finish_expr_stmt (set); |
| 14277 | } |
| 14278 | cleanup = build3 (COND_EXPR, void_type_node, |
| 14279 | *cond_guard, cleanup, NULL_TREE); |
| 14280 | } |
| 14281 | if (flag_exceptions && TREE_CODE (TREE_TYPE (var)) != ARRAY_TYPE) |
| 14282 | { |
| 14283 | /* The normal cleanup for this extended variable isn't pushed |
| 14284 | until cp_finish_decl, so we need to retain a TARGET_EXPR |
| 14285 | to clean it up in case a later initializer throws |
| 14286 | (g++.dg/eh/ref-temp3.C). |
| 14287 | |
| 14288 | We don't do this for array temporaries because they have |
| 14289 | the array cleanup region from build_vec_init. |
| 14290 | |
| 14291 | Unlike maybe_push_temp_cleanup, we don't actually need a |
| 14292 | flag, but a TARGET_EXPR needs a TARGET_EXPR_SLOT. |
| 14293 | Perhaps this could use WITH_CLEANUP_EXPR instead, but |
| 14294 | gimplify.cc doesn't handle that, and front-end handling |
| 14295 | was removed in r8-1725 and r8-1818. |
| 14296 | |
| 14297 | Alternately it might be preferable to flatten an |
| 14298 | initialization with extended temps into a sequence of |
| 14299 | (non-full-expression) statements, so we could immediately |
| 14300 | push_cleanup here for only a single cleanup region, but we |
| 14301 | don't have a mechanism for that in the front-end, only the |
| 14302 | gimplifier. */ |
| 14303 | tree targ = get_internal_target_expr (boolean_true_node); |
| 14304 | TARGET_EXPR_CLEANUP (targ) = cleanup; |
| 14305 | CLEANUP_EH_ONLY (targ) = true; |
| 14306 | /* Don't actually initialize the bool. */ |
| 14307 | init = (!init ? void_node |
| 14308 | : convert_to_void (init, ICV_STATEMENT, tf_none)); |
| 14309 | TARGET_EXPR_INITIAL (targ) = init; |
| 14310 | init = targ; |
| 14311 | } |
| 14312 | vec_safe_push (v&: *cleanups, obj: cleanup); |
| 14313 | } |
| 14314 | } |
| 14315 | |
| 14316 | /* We must be careful to destroy the temporary only |
| 14317 | after its initialization has taken place. If the |
| 14318 | initialization throws an exception, then the |
| 14319 | destructor should not be run. We cannot simply |
| 14320 | transform INIT into something like: |
| 14321 | |
| 14322 | (INIT, ({ CLEANUP_STMT; })) |
| 14323 | |
| 14324 | because emit_local_var always treats the |
| 14325 | initializer as a full-expression. Thus, the |
| 14326 | destructor would run too early; it would run at the |
| 14327 | end of initializing the reference variable, rather |
| 14328 | than at the end of the block enclosing the |
| 14329 | reference variable. |
| 14330 | |
| 14331 | The solution is to pass back a cleanup expression |
| 14332 | which the caller is responsible for attaching to |
| 14333 | the statement tree. */ |
| 14334 | } |
| 14335 | else |
| 14336 | { |
| 14337 | rest_of_decl_compilation (var, /*toplev=*/1, at_eof); |
| 14338 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
| 14339 | { |
| 14340 | if (CP_DECL_THREAD_LOCAL_P (var)) |
| 14341 | tls_aggregates = tree_cons (NULL_TREE, var, |
| 14342 | tls_aggregates); |
| 14343 | else |
| 14344 | static_aggregates = tree_cons (NULL_TREE, var, |
| 14345 | static_aggregates); |
| 14346 | } |
| 14347 | else |
| 14348 | /* Check whether the dtor is callable. */ |
| 14349 | cxx_maybe_build_cleanup (var, tf_warning_or_error); |
| 14350 | } |
| 14351 | /* Avoid -Wunused-variable warning (c++/38958). */ |
| 14352 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) |
| 14353 | && VAR_P (decl)) |
| 14354 | TREE_USED (decl) = DECL_READ_P (decl) = true; |
| 14355 | |
| 14356 | *initp = init; |
| 14357 | return var; |
| 14358 | } |
| 14359 | |
| 14360 | /* Convert EXPR to the indicated reference TYPE, in a way suitable for |
| 14361 | initializing a variable of that TYPE. */ |
| 14362 | |
| 14363 | tree |
| 14364 | initialize_reference (tree type, tree expr, |
| 14365 | int flags, tsubst_flags_t complain) |
| 14366 | { |
| 14367 | conversion *conv; |
| 14368 | location_t loc = cp_expr_loc_or_input_loc (t: expr); |
| 14369 | |
| 14370 | if (type == error_mark_node || error_operand_p (t: expr)) |
| 14371 | return error_mark_node; |
| 14372 | |
| 14373 | conversion_obstack_sentinel cos; |
| 14374 | |
| 14375 | conv = reference_binding (rto: type, TREE_TYPE (expr), expr, /*c_cast_p=*/false, |
| 14376 | flags, complain); |
| 14377 | /* If this conversion failed, we're in C++20, and we have something like |
| 14378 | A& a(b) where A is an aggregate, try again, this time as A& a{b}. */ |
| 14379 | if ((!conv || conv->bad_p) |
| 14380 | && (flags & LOOKUP_AGGREGATE_PAREN_INIT)) |
| 14381 | { |
| 14382 | tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr); |
| 14383 | CONSTRUCTOR_IS_DIRECT_INIT (e) = true; |
| 14384 | CONSTRUCTOR_IS_PAREN_INIT (e) = true; |
| 14385 | conversion *c = reference_binding (rto: type, TREE_TYPE (e), expr: e, |
| 14386 | /*c_cast_p=*/false, flags, complain); |
| 14387 | /* If this worked, use it. */ |
| 14388 | if (c && !c->bad_p) |
| 14389 | expr = e, conv = c; |
| 14390 | } |
| 14391 | if (!conv || conv->bad_p) |
| 14392 | { |
| 14393 | if (complain & tf_error) |
| 14394 | { |
| 14395 | if (conv) |
| 14396 | convert_like (convs: conv, expr, complain); |
| 14397 | else if (!CP_TYPE_CONST_P (TREE_TYPE (type)) |
| 14398 | && !TYPE_REF_IS_RVALUE (type) |
| 14399 | && !lvalue_p (expr)) |
| 14400 | error_at (loc, "invalid initialization of non-const reference of " |
| 14401 | "type %qH from an rvalue of type %qI" , |
| 14402 | type, TREE_TYPE (expr)); |
| 14403 | else |
| 14404 | error_at (loc, "invalid initialization of reference of type " |
| 14405 | "%qH from expression of type %qI" , type, |
| 14406 | TREE_TYPE (expr)); |
| 14407 | } |
| 14408 | return error_mark_node; |
| 14409 | } |
| 14410 | |
| 14411 | if (conv->kind == ck_ref_bind) |
| 14412 | /* Perform the conversion. */ |
| 14413 | expr = convert_like (convs: conv, expr, complain); |
| 14414 | else if (conv->kind == ck_ambig) |
| 14415 | /* We gave an error in build_user_type_conversion_1. */ |
| 14416 | expr = error_mark_node; |
| 14417 | else |
| 14418 | gcc_unreachable (); |
| 14419 | |
| 14420 | return expr; |
| 14421 | } |
| 14422 | |
| 14423 | /* Return true if T is std::pair<const T&, const T&>. */ |
| 14424 | |
| 14425 | static bool |
| 14426 | std_pair_ref_ref_p (tree t) |
| 14427 | { |
| 14428 | /* First, check if we have std::pair. */ |
| 14429 | if (!NON_UNION_CLASS_TYPE_P (t) |
| 14430 | || !CLASSTYPE_TEMPLATE_INSTANTIATION (t)) |
| 14431 | return false; |
| 14432 | tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (t)); |
| 14433 | if (!decl_in_std_namespace_p (tdecl)) |
| 14434 | return false; |
| 14435 | tree name = DECL_NAME (tdecl); |
| 14436 | if (!name || !id_equal (id: name, str: "pair" )) |
| 14437 | return false; |
| 14438 | |
| 14439 | /* Now see if the template arguments are both const T&. */ |
| 14440 | tree args = CLASSTYPE_TI_ARGS (t); |
| 14441 | if (TREE_VEC_LENGTH (args) != 2) |
| 14442 | return false; |
| 14443 | for (int i = 0; i < 2; i++) |
| 14444 | if (!TYPE_REF_OBJ_P (TREE_VEC_ELT (args, i)) |
| 14445 | || !CP_TYPE_CONST_P (TREE_TYPE (TREE_VEC_ELT (args, i)))) |
| 14446 | return false; |
| 14447 | |
| 14448 | return true; |
| 14449 | } |
| 14450 | |
| 14451 | /* Return true if a class T has a reference member. */ |
| 14452 | |
| 14453 | static bool |
| 14454 | class_has_reference_member_p (tree t) |
| 14455 | { |
| 14456 | for (tree fields = TYPE_FIELDS (t); |
| 14457 | fields; |
| 14458 | fields = DECL_CHAIN (fields)) |
| 14459 | if (TREE_CODE (fields) == FIELD_DECL |
| 14460 | && !DECL_ARTIFICIAL (fields) |
| 14461 | && TYPE_REF_P (TREE_TYPE (fields))) |
| 14462 | return true; |
| 14463 | return false; |
| 14464 | } |
| 14465 | |
| 14466 | /* A wrapper for the above suitable as a callback for dfs_walk_once. */ |
| 14467 | |
| 14468 | static tree |
| 14469 | class_has_reference_member_p_r (tree binfo, void *) |
| 14470 | { |
| 14471 | return (class_has_reference_member_p (BINFO_TYPE (binfo)) |
| 14472 | ? integer_one_node : NULL_TREE); |
| 14473 | } |
| 14474 | |
| 14475 | |
| 14476 | /* Return true if T (either a class or a function) has been marked as |
| 14477 | not-dangling. */ |
| 14478 | |
| 14479 | static bool |
| 14480 | no_dangling_p (tree t) |
| 14481 | { |
| 14482 | t = lookup_attribute (attr_name: "no_dangling" , TYPE_ATTRIBUTES (t)); |
| 14483 | if (!t) |
| 14484 | return false; |
| 14485 | |
| 14486 | t = TREE_VALUE (t); |
| 14487 | if (!t) |
| 14488 | return true; |
| 14489 | |
| 14490 | t = build_converted_constant_bool_expr (TREE_VALUE (t), complain: tf_warning_or_error); |
| 14491 | t = cxx_constant_value (t); |
| 14492 | return t == boolean_true_node; |
| 14493 | } |
| 14494 | |
| 14495 | /* Return true if a class CTYPE is either std::reference_wrapper or |
| 14496 | std::ref_view, or a reference wrapper class. We consider a class |
| 14497 | a reference wrapper class if it has a reference member. We no |
| 14498 | longer check that it has a constructor taking the same reference type |
| 14499 | since that approach still generated too many false positives. */ |
| 14500 | |
| 14501 | static bool |
| 14502 | reference_like_class_p (tree ctype) |
| 14503 | { |
| 14504 | if (!CLASS_TYPE_P (ctype)) |
| 14505 | return false; |
| 14506 | |
| 14507 | if (no_dangling_p (t: ctype)) |
| 14508 | return true; |
| 14509 | |
| 14510 | /* Also accept a std::pair<const T&, const T&>. */ |
| 14511 | if (std_pair_ref_ref_p (t: ctype)) |
| 14512 | return true; |
| 14513 | |
| 14514 | tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (ctype)); |
| 14515 | if (decl_in_std_namespace_p (tdecl)) |
| 14516 | { |
| 14517 | tree name = DECL_NAME (tdecl); |
| 14518 | if (name |
| 14519 | && (id_equal (id: name, str: "reference_wrapper" ) |
| 14520 | || id_equal (id: name, str: "span" ) |
| 14521 | || id_equal (id: name, str: "ref_view" ))) |
| 14522 | return true; |
| 14523 | } |
| 14524 | |
| 14525 | /* Avoid warning if CTYPE looks like std::span: it has a T* member and |
| 14526 | a trivial destructor. For example, |
| 14527 | |
| 14528 | template<typename T> |
| 14529 | struct Span { |
| 14530 | T* data_; |
| 14531 | std::size len_; |
| 14532 | }; |
| 14533 | |
| 14534 | is considered std::span-like. */ |
| 14535 | if (NON_UNION_CLASS_TYPE_P (ctype) && TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype)) |
| 14536 | for (tree field = next_aggregate_field (TYPE_FIELDS (ctype)); |
| 14537 | field; field = next_aggregate_field (DECL_CHAIN (field))) |
| 14538 | if (TYPE_PTR_P (TREE_TYPE (field))) |
| 14539 | return true; |
| 14540 | |
| 14541 | /* Some classes, such as std::tuple, have the reference member in its |
| 14542 | (non-direct) base class. */ |
| 14543 | if (dfs_walk_once (TYPE_BINFO (ctype), class_has_reference_member_p_r, |
| 14544 | nullptr, nullptr)) |
| 14545 | return true; |
| 14546 | |
| 14547 | return false; |
| 14548 | } |
| 14549 | |
| 14550 | /* Helper for maybe_warn_dangling_reference to find a problematic temporary |
| 14551 | in EXPR (as outlined in maybe_warn_dangling_reference), or NULL_TREE |
| 14552 | if none found. For instance: |
| 14553 | |
| 14554 | const S& s = S().self(); // S() |
| 14555 | const int& r = (42, f(1)); // temporary for passing 1 to f |
| 14556 | const int& t = b ? f(1) : f(2); // temporary for 1 |
| 14557 | const int& u = b ? f(1) : f(g); // temporary for 1 |
| 14558 | const int& v = b ? f(g) : f(2); // temporary for 2 |
| 14559 | const int& w = b ? f(g) : f(g); // NULL_TREE |
| 14560 | const int& y = (f(1), 42); // NULL_TREE |
| 14561 | const int& z = f(f(1)); // temporary for 1 |
| 14562 | |
| 14563 | EXPR is the initializer. If ARG_P is true, we're processing an argument |
| 14564 | to a function; the point is to distinguish between, for example, |
| 14565 | |
| 14566 | Ref::inner (&TARGET_EXPR <D.2839, F::foo (fm)>) |
| 14567 | |
| 14568 | where we shouldn't warn, and |
| 14569 | |
| 14570 | Ref::inner (&TARGET_EXPR <D.2908, F::foo (&TARGET_EXPR <...>)>) |
| 14571 | |
| 14572 | where we should warn (Ref is a reference_like_class_p so we see through |
| 14573 | it. */ |
| 14574 | |
| 14575 | static tree |
| 14576 | do_warn_dangling_reference (tree expr, bool arg_p) |
| 14577 | { |
| 14578 | STRIP_NOPS (expr); |
| 14579 | |
| 14580 | if (arg_p && expr_represents_temporary_p (expr)) |
| 14581 | { |
| 14582 | /* An attempt to reduce the number of -Wdangling-reference |
| 14583 | false positives concerning reference wrappers (c++/107532). |
| 14584 | When we encounter a reference_like_class_p, we don't warn |
| 14585 | just yet; instead, we keep recursing to see if there were |
| 14586 | any temporaries behind the reference-wrapper class. */ |
| 14587 | tree e = expr; |
| 14588 | while (handled_component_p (t: e)) |
| 14589 | e = TREE_OPERAND (e, 0); |
| 14590 | tree type = TREE_TYPE (e); |
| 14591 | /* If the temporary represents a lambda, we don't really know |
| 14592 | what's going on here. */ |
| 14593 | if (!reference_like_class_p (ctype: type) && !LAMBDA_TYPE_P (type)) |
| 14594 | return expr; |
| 14595 | } |
| 14596 | |
| 14597 | switch (TREE_CODE (expr)) |
| 14598 | { |
| 14599 | case CALL_EXPR: |
| 14600 | { |
| 14601 | tree fndecl = cp_get_callee_fndecl_nofold (expr); |
| 14602 | if (!fndecl |
| 14603 | || warning_suppressed_p (fndecl, OPT_Wdangling_reference) |
| 14604 | || !warning_enabled_at (DECL_SOURCE_LOCATION (fndecl), |
| 14605 | option_id: OPT_Wdangling_reference) |
| 14606 | /* Don't emit a false positive for: |
| 14607 | std::vector<int> v = ...; |
| 14608 | std::vector<int>::const_iterator it = v.begin(); |
| 14609 | const int &r = *it++; |
| 14610 | because R refers to one of the int elements of V, not to |
| 14611 | a temporary object. Member operator* may return a reference |
| 14612 | but probably not to one of its arguments. */ |
| 14613 | || (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl) |
| 14614 | && DECL_OVERLOADED_OPERATOR_P (fndecl) |
| 14615 | && DECL_OVERLOADED_OPERATOR_IS (fndecl, INDIRECT_REF)) |
| 14616 | || no_dangling_p (TREE_TYPE (fndecl))) |
| 14617 | return NULL_TREE; |
| 14618 | |
| 14619 | tree rettype = TREE_TYPE (TREE_TYPE (fndecl)); |
| 14620 | /* If the function doesn't return a reference, don't warn. This |
| 14621 | can be e.g. |
| 14622 | const int& z = std::min({1, 2, 3, 4, 5, 6, 7}); |
| 14623 | which doesn't dangle: std::min here returns an int. |
| 14624 | |
| 14625 | If the function returns a std::pair<const T&, const T&>, we |
| 14626 | warn, to detect e.g. |
| 14627 | std::pair<const int&, const int&> v = std::minmax(1, 2); |
| 14628 | which also creates a dangling reference, because std::minmax |
| 14629 | returns std::pair<const T&, const T&>(b, a). */ |
| 14630 | if (!(TYPE_REF_OBJ_P (rettype) || reference_like_class_p (ctype: rettype))) |
| 14631 | return NULL_TREE; |
| 14632 | |
| 14633 | /* Here we're looking to see if any of the arguments is a temporary |
| 14634 | initializing a reference parameter. */ |
| 14635 | for (int i = 0; i < call_expr_nargs (expr); ++i) |
| 14636 | { |
| 14637 | tree arg = CALL_EXPR_ARG (expr, i); |
| 14638 | /* Check that this argument initializes a reference, except for |
| 14639 | the argument initializing the object of a member function. */ |
| 14640 | if (!DECL_IOBJ_MEMBER_FUNCTION_P (fndecl) |
| 14641 | && !TYPE_REF_P (TREE_TYPE (arg))) |
| 14642 | continue; |
| 14643 | STRIP_NOPS (arg); |
| 14644 | if (TREE_CODE (arg) == ADDR_EXPR) |
| 14645 | arg = TREE_OPERAND (arg, 0); |
| 14646 | /* Recurse to see if the argument is a temporary. It could also |
| 14647 | be another call taking a temporary and returning it and |
| 14648 | initializing this reference parameter. */ |
| 14649 | if ((arg = do_warn_dangling_reference (expr: arg, /*arg_p=*/true))) |
| 14650 | { |
| 14651 | /* If we know the temporary could not bind to the return type, |
| 14652 | don't warn. This is for scalars and empty classes only |
| 14653 | because for other classes we can't be sure we are not |
| 14654 | returning its sub-object. */ |
| 14655 | if ((SCALAR_TYPE_P (TREE_TYPE (arg)) |
| 14656 | || is_empty_class (TREE_TYPE (arg))) |
| 14657 | && TYPE_REF_P (rettype) |
| 14658 | && !reference_related_p (TREE_TYPE (rettype), |
| 14659 | TREE_TYPE (arg))) |
| 14660 | continue; |
| 14661 | return arg; |
| 14662 | } |
| 14663 | /* Don't warn about member functions like: |
| 14664 | std::any a(...); |
| 14665 | S& s = a.emplace<S>({0}, 0); |
| 14666 | which construct a new object and return a reference to it, but |
| 14667 | we still want to detect: |
| 14668 | struct S { const S& self () { return *this; } }; |
| 14669 | const S& s = S().self(); |
| 14670 | where 's' dangles. If we've gotten here, the object this function |
| 14671 | is invoked on is not a temporary. */ |
| 14672 | if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl)) |
| 14673 | break; |
| 14674 | } |
| 14675 | return NULL_TREE; |
| 14676 | } |
| 14677 | case COMPOUND_EXPR: |
| 14678 | return do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p); |
| 14679 | case COND_EXPR: |
| 14680 | if (tree t = do_warn_dangling_reference (TREE_OPERAND (expr, 1), arg_p)) |
| 14681 | return t; |
| 14682 | return do_warn_dangling_reference (TREE_OPERAND (expr, 2), arg_p); |
| 14683 | case PAREN_EXPR: |
| 14684 | return do_warn_dangling_reference (TREE_OPERAND (expr, 0), arg_p); |
| 14685 | case TARGET_EXPR: |
| 14686 | return do_warn_dangling_reference (TARGET_EXPR_INITIAL (expr), arg_p); |
| 14687 | default: |
| 14688 | return NULL_TREE; |
| 14689 | } |
| 14690 | } |
| 14691 | |
| 14692 | /* Implement -Wdangling-reference, to detect cases like |
| 14693 | |
| 14694 | int n = 1; |
| 14695 | const int& r = std::max(n - 1, n + 1); // r is dangling |
| 14696 | |
| 14697 | This creates temporaries from the arguments, returns a reference to |
| 14698 | one of the temporaries, but both temporaries are destroyed at the end |
| 14699 | of the full expression. |
| 14700 | |
| 14701 | This works by checking if a reference is initialized with a function |
| 14702 | that returns a reference, and at least one parameter of the function |
| 14703 | is a reference that is bound to a temporary. It assumes that such a |
| 14704 | function actually returns one of its arguments. |
| 14705 | |
| 14706 | DECL is the reference being initialized, INIT is the initializer. */ |
| 14707 | |
| 14708 | static void |
| 14709 | maybe_warn_dangling_reference (const_tree decl, tree init) |
| 14710 | { |
| 14711 | if (!warn_dangling_reference) |
| 14712 | return; |
| 14713 | tree type = TREE_TYPE (decl); |
| 14714 | /* Only warn if what we're initializing has type T&& or const T&, or |
| 14715 | std::pair<const T&, const T&>. (A non-const lvalue reference can't |
| 14716 | bind to a temporary.) */ |
| 14717 | if (!((TYPE_REF_OBJ_P (type) |
| 14718 | && (TYPE_REF_IS_RVALUE (type) |
| 14719 | || CP_TYPE_CONST_P (TREE_TYPE (type)))) |
| 14720 | || std_pair_ref_ref_p (t: type))) |
| 14721 | return; |
| 14722 | /* Don't suppress the diagnostic just because the call comes from |
| 14723 | a system header. If the DECL is not in a system header, or if |
| 14724 | -Wsystem-headers was provided, warn. */ |
| 14725 | auto wsh |
| 14726 | = make_temp_override (var&: global_dc->m_warn_system_headers, |
| 14727 | overrider: (!in_system_header_at (DECL_SOURCE_LOCATION (decl)) |
| 14728 | || global_dc->m_warn_system_headers)); |
| 14729 | if (tree call = do_warn_dangling_reference (expr: init, /*arg_p=*/false)) |
| 14730 | { |
| 14731 | auto_diagnostic_group d; |
| 14732 | if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wdangling_reference, |
| 14733 | "possibly dangling reference to a temporary" )) |
| 14734 | inform (EXPR_LOCATION (call), "%qT temporary created here" , |
| 14735 | TREE_TYPE (call)); |
| 14736 | } |
| 14737 | } |
| 14738 | |
| 14739 | /* If *P is an xvalue expression, prevent temporary lifetime extension if it |
| 14740 | gets used to initialize a reference. */ |
| 14741 | |
| 14742 | static tree |
| 14743 | prevent_lifetime_extension (tree t) |
| 14744 | { |
| 14745 | tree *p = &t; |
| 14746 | while (TREE_CODE (*p) == COMPOUND_EXPR) |
| 14747 | p = &TREE_OPERAND (*p, 1); |
| 14748 | while (handled_component_p (t: *p)) |
| 14749 | p = &TREE_OPERAND (*p, 0); |
| 14750 | /* Change a TARGET_EXPR from prvalue to xvalue. */ |
| 14751 | if (TREE_CODE (*p) == TARGET_EXPR) |
| 14752 | *p = build2 (COMPOUND_EXPR, TREE_TYPE (*p), *p, |
| 14753 | move (TARGET_EXPR_SLOT (*p))); |
| 14754 | return t; |
| 14755 | } |
| 14756 | |
| 14757 | /* Subroutine of extend_ref_init_temps. Possibly extend one initializer, |
| 14758 | which is bound either to a reference or a std::initializer_list. */ |
| 14759 | |
| 14760 | static tree |
| 14761 | extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups, |
| 14762 | tree *cond_guard) |
| 14763 | { |
| 14764 | /* CWG1299 (C++20): The temporary object to which the reference is bound or |
| 14765 | the temporary object that is the complete object of a subobject to which |
| 14766 | the reference is bound persists for the lifetime of the reference if the |
| 14767 | glvalue to which the reference is bound was obtained through one of the |
| 14768 | following: |
| 14769 | - a temporary materialization conversion ([conv.rval]), |
| 14770 | - ( expression ), where expression is one of these expressions, |
| 14771 | - subscripting ([expr.sub]) of an array operand, where that operand is one |
| 14772 | of these expressions, |
| 14773 | - a class member access ([expr.ref]) using the . operator where the left |
| 14774 | operand is one of these expressions and the right operand designates a |
| 14775 | non-static data member of non-reference type, |
| 14776 | - a pointer-to-member operation ([expr.mptr.oper]) using the .* operator |
| 14777 | where the left operand is one of these expressions and the right operand |
| 14778 | is a pointer to data member of non-reference type, |
| 14779 | - a const_cast ([expr.const.cast]), static_cast ([expr.static.cast]), |
| 14780 | dynamic_cast ([expr.dynamic.cast]), or reinterpret_cast |
| 14781 | ([expr.reinterpret.cast]) converting, without a user-defined conversion, |
| 14782 | a glvalue operand that is one of these expressions to a glvalue that |
| 14783 | refers to the object designated by the operand, or to its complete |
| 14784 | object or a subobject thereof, |
| 14785 | - a conditional expression ([expr.cond]) that is a glvalue where the |
| 14786 | second or third operand is one of these expressions, or |
| 14787 | - a comma expression ([expr.comma]) that is a glvalue where the right |
| 14788 | operand is one of these expressions. */ |
| 14789 | |
| 14790 | /* FIXME several cases are still handled wrong (101572, 81420). */ |
| 14791 | |
| 14792 | tree sub = init; |
| 14793 | tree *p; |
| 14794 | STRIP_NOPS (sub); |
| 14795 | if (TREE_CODE (sub) == COMPOUND_EXPR) |
| 14796 | { |
| 14797 | TREE_OPERAND (sub, 1) |
| 14798 | = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups, |
| 14799 | cond_guard); |
| 14800 | return init; |
| 14801 | } |
| 14802 | if (TREE_CODE (sub) == POINTER_PLUS_EXPR |
| 14803 | && TYPE_PTRDATAMEM_P (TREE_TYPE (tree_strip_nop_conversions |
| 14804 | (TREE_OPERAND (sub, 1))))) |
| 14805 | { |
| 14806 | /* A pointer-to-member operation. */ |
| 14807 | TREE_OPERAND (sub, 0) |
| 14808 | = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 0), cleanups, |
| 14809 | cond_guard); |
| 14810 | return init; |
| 14811 | } |
| 14812 | if (TREE_CODE (sub) == COND_EXPR) |
| 14813 | { |
| 14814 | tree cur_cond_guard = NULL_TREE; |
| 14815 | if (TREE_OPERAND (sub, 1)) |
| 14816 | TREE_OPERAND (sub, 1) |
| 14817 | = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups, |
| 14818 | cond_guard: &cur_cond_guard); |
| 14819 | if (cur_cond_guard) |
| 14820 | { |
| 14821 | tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard, |
| 14822 | NOP_EXPR, boolean_true_node, |
| 14823 | tf_warning_or_error); |
| 14824 | TREE_OPERAND (sub, 1) |
| 14825 | = cp_build_compound_expr (set, TREE_OPERAND (sub, 1), |
| 14826 | tf_warning_or_error); |
| 14827 | } |
| 14828 | cur_cond_guard = NULL_TREE; |
| 14829 | if (TREE_OPERAND (sub, 2)) |
| 14830 | TREE_OPERAND (sub, 2) |
| 14831 | = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 2), cleanups, |
| 14832 | cond_guard: &cur_cond_guard); |
| 14833 | if (cur_cond_guard) |
| 14834 | { |
| 14835 | tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard, |
| 14836 | NOP_EXPR, boolean_true_node, |
| 14837 | tf_warning_or_error); |
| 14838 | TREE_OPERAND (sub, 2) |
| 14839 | = cp_build_compound_expr (set, TREE_OPERAND (sub, 2), |
| 14840 | tf_warning_or_error); |
| 14841 | } |
| 14842 | return init; |
| 14843 | } |
| 14844 | if (TREE_CODE (sub) != ADDR_EXPR) |
| 14845 | return init; |
| 14846 | /* Deal with binding to a subobject. */ |
| 14847 | for (p = &TREE_OPERAND (sub, 0); |
| 14848 | TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; ) |
| 14849 | p = &TREE_OPERAND (*p, 0); |
| 14850 | if (TREE_CODE (*p) == TARGET_EXPR) |
| 14851 | { |
| 14852 | tree subinit = NULL_TREE; |
| 14853 | *p = set_up_extended_ref_temp (decl, expr: *p, cleanups, initp: &subinit, |
| 14854 | cond_guard, walk_data: nullptr); |
| 14855 | recompute_tree_invariant_for_addr_expr (sub); |
| 14856 | if (init != sub) |
| 14857 | init = fold_convert (TREE_TYPE (init), sub); |
| 14858 | if (subinit) |
| 14859 | init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init); |
| 14860 | } |
| 14861 | return init; |
| 14862 | } |
| 14863 | |
| 14864 | /* Data for extend_temps_r, mostly matching the parameters of |
| 14865 | extend_ref_init_temps. */ |
| 14866 | |
| 14867 | struct extend_temps_data |
| 14868 | { |
| 14869 | tree decl; |
| 14870 | tree init; |
| 14871 | vec<tree, va_gc> **cleanups; |
| 14872 | tree* cond_guard; |
| 14873 | hash_set<tree> *pset; // For avoiding redundant walk_tree. |
| 14874 | hash_map<tree, tree> *var_map; // For remapping extended temps. |
| 14875 | }; |
| 14876 | |
| 14877 | /* Tree walk function for extend_all_temps. Generally parallel to |
| 14878 | extend_ref_init_temps_1, but adapted for walk_tree. */ |
| 14879 | |
| 14880 | tree |
| 14881 | extend_temps_r (tree *tp, int *walk_subtrees, void *data) |
| 14882 | { |
| 14883 | extend_temps_data *d = (extend_temps_data *)data; |
| 14884 | |
| 14885 | if (TREE_CODE (*tp) == VAR_DECL) |
| 14886 | { |
| 14887 | if (tree *r = d->var_map->get (k: *tp)) |
| 14888 | *tp = *r; |
| 14889 | return NULL_TREE; |
| 14890 | } |
| 14891 | |
| 14892 | if (TYPE_P (*tp) || TREE_CODE (*tp) == CLEANUP_POINT_EXPR |
| 14893 | || d->pset->add (k: *tp)) |
| 14894 | { |
| 14895 | *walk_subtrees = 0; |
| 14896 | return NULL_TREE; |
| 14897 | } |
| 14898 | |
| 14899 | if (TREE_CODE (*tp) == COND_EXPR) |
| 14900 | { |
| 14901 | cp_walk_tree (&TREE_OPERAND (*tp, 0), extend_temps_r, d, nullptr); |
| 14902 | |
| 14903 | auto walk_arm = [d](tree &op) |
| 14904 | { |
| 14905 | tree cur_cond_guard = NULL_TREE; |
| 14906 | auto ov = make_temp_override (var&: d->cond_guard, overrider: &cur_cond_guard); |
| 14907 | cp_walk_tree (&op, extend_temps_r, d, nullptr); |
| 14908 | if (cur_cond_guard) |
| 14909 | { |
| 14910 | tree set = build2 (MODIFY_EXPR, boolean_type_node, |
| 14911 | cur_cond_guard, boolean_true_node); |
| 14912 | op = cp_build_compound_expr (set, op, tf_none); |
| 14913 | } |
| 14914 | }; |
| 14915 | walk_arm (TREE_OPERAND (*tp, 1)); |
| 14916 | walk_arm (TREE_OPERAND (*tp, 2)); |
| 14917 | |
| 14918 | *walk_subtrees = 0; |
| 14919 | return NULL_TREE; |
| 14920 | } |
| 14921 | |
| 14922 | tree *p = tp; |
| 14923 | |
| 14924 | if (TREE_CODE (*tp) == ADDR_EXPR) |
| 14925 | for (p = &TREE_OPERAND (*tp, 0); |
| 14926 | TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; ) |
| 14927 | p = &TREE_OPERAND (*p, 0); |
| 14928 | |
| 14929 | if (TREE_CODE (*p) == TARGET_EXPR |
| 14930 | /* An eliding TARGET_EXPR isn't a temporary at all. */ |
| 14931 | && !TARGET_EXPR_ELIDING_P (*p) |
| 14932 | /* A TARGET_EXPR with TARGET_EXPR_INTERNAL_P is an artificial variable |
| 14933 | used during initialization that need not be extended. */ |
| 14934 | && !TARGET_EXPR_INTERNAL_P (*p)) |
| 14935 | { |
| 14936 | /* A CLEANUP_EH_ONLY expr should also have TARGET_EXPR_INTERNAL_P. */ |
| 14937 | gcc_checking_assert (!CLEANUP_EH_ONLY (*p)); |
| 14938 | |
| 14939 | tree subinit = NULL_TREE; |
| 14940 | tree slot = TARGET_EXPR_SLOT (*p); |
| 14941 | *p = set_up_extended_ref_temp (decl: d->decl, expr: *p, cleanups: d->cleanups, initp: &subinit, |
| 14942 | cond_guard: d->cond_guard, walk_data: d); |
| 14943 | if (TREE_CODE (*tp) == ADDR_EXPR) |
| 14944 | recompute_tree_invariant_for_addr_expr (*tp); |
| 14945 | if (subinit) |
| 14946 | *tp = cp_build_compound_expr (subinit, *tp, tf_none); |
| 14947 | d->var_map->put (k: slot, v: *p); |
| 14948 | } |
| 14949 | |
| 14950 | return NULL_TREE; |
| 14951 | } |
| 14952 | |
| 14953 | /* Extend all the temporaries in a for-range-initializer. */ |
| 14954 | |
| 14955 | static tree |
| 14956 | extend_all_temps (tree decl, tree init, vec<tree, va_gc> **cleanups) |
| 14957 | { |
| 14958 | hash_set<tree> pset; |
| 14959 | hash_map<tree, tree> map; |
| 14960 | gcc_assert (!TREE_STATIC (decl)); |
| 14961 | extend_temps_data d = { .decl: decl, .init: init, .cleanups: cleanups, .cond_guard: nullptr, .pset: &pset, .var_map: &map }; |
| 14962 | cp_walk_tree (&init, extend_temps_r, &d, nullptr); |
| 14963 | return init; |
| 14964 | } |
| 14965 | |
| 14966 | /* INIT is part of the initializer for DECL. If there are any |
| 14967 | reference or initializer lists being initialized, extend their |
| 14968 | lifetime to match that of DECL. */ |
| 14969 | |
| 14970 | tree |
| 14971 | extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups, |
| 14972 | tree *cond_guard) |
| 14973 | { |
| 14974 | tree type = TREE_TYPE (init); |
| 14975 | if (processing_template_decl) |
| 14976 | return init; |
| 14977 | |
| 14978 | /* P2718R0 - in C++23 for-range-initializer, extend all temps. */ |
| 14979 | if (DECL_NAME (decl) == for_range__identifier |
| 14980 | && flag_range_for_ext_temps) |
| 14981 | { |
| 14982 | gcc_checking_assert (!cond_guard); |
| 14983 | return extend_all_temps (decl, init, cleanups); |
| 14984 | } |
| 14985 | |
| 14986 | maybe_warn_dangling_reference (decl, init); |
| 14987 | |
| 14988 | if (TYPE_REF_P (type)) |
| 14989 | init = extend_ref_init_temps_1 (decl, init, cleanups, cond_guard); |
| 14990 | else |
| 14991 | { |
| 14992 | tree ctor = init; |
| 14993 | if (TREE_CODE (ctor) == TARGET_EXPR) |
| 14994 | ctor = TARGET_EXPR_INITIAL (ctor); |
| 14995 | if (TREE_CODE (ctor) == CONSTRUCTOR) |
| 14996 | { |
| 14997 | /* [dcl.init] When initializing an aggregate from a parenthesized list |
| 14998 | of values... a temporary object bound to a reference does not have |
| 14999 | its lifetime extended. */ |
| 15000 | if (CONSTRUCTOR_IS_PAREN_INIT (ctor)) |
| 15001 | return init; |
| 15002 | |
| 15003 | if (is_std_init_list (type)) |
| 15004 | { |
| 15005 | /* The temporary array underlying a std::initializer_list |
| 15006 | is handled like a reference temporary. */ |
| 15007 | tree array = CONSTRUCTOR_ELT (ctor, 0)->value; |
| 15008 | array = extend_ref_init_temps_1 (decl, init: array, cleanups, |
| 15009 | cond_guard); |
| 15010 | CONSTRUCTOR_ELT (ctor, 0)->value = array; |
| 15011 | } |
| 15012 | else |
| 15013 | { |
| 15014 | unsigned i; |
| 15015 | constructor_elt *p; |
| 15016 | vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor); |
| 15017 | FOR_EACH_VEC_SAFE_ELT (elts, i, p) |
| 15018 | p->value = extend_ref_init_temps (decl, init: p->value, cleanups, |
| 15019 | cond_guard); |
| 15020 | } |
| 15021 | recompute_constructor_flags (ctor); |
| 15022 | if (decl_maybe_constant_var_p (decl) && TREE_CONSTANT (ctor)) |
| 15023 | DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true; |
| 15024 | } |
| 15025 | } |
| 15026 | |
| 15027 | return init; |
| 15028 | } |
| 15029 | |
| 15030 | /* Returns true iff an initializer for TYPE could contain temporaries that |
| 15031 | need to be extended because they are bound to references or |
| 15032 | std::initializer_list. */ |
| 15033 | |
| 15034 | bool |
| 15035 | type_has_extended_temps (tree type) |
| 15036 | { |
| 15037 | type = strip_array_types (type); |
| 15038 | if (TYPE_REF_P (type)) |
| 15039 | return true; |
| 15040 | if (CLASS_TYPE_P (type)) |
| 15041 | { |
| 15042 | if (is_std_init_list (type)) |
| 15043 | return true; |
| 15044 | for (tree f = next_aggregate_field (TYPE_FIELDS (type)); |
| 15045 | f; f = next_aggregate_field (DECL_CHAIN (f))) |
| 15046 | if (type_has_extended_temps (TREE_TYPE (f))) |
| 15047 | return true; |
| 15048 | } |
| 15049 | return false; |
| 15050 | } |
| 15051 | |
| 15052 | /* Returns true iff TYPE is some variant of std::initializer_list. */ |
| 15053 | |
| 15054 | bool |
| 15055 | is_std_init_list (tree type) |
| 15056 | { |
| 15057 | if (!TYPE_P (type)) |
| 15058 | return false; |
| 15059 | if (cxx_dialect == cxx98) |
| 15060 | return false; |
| 15061 | /* Look through typedefs. */ |
| 15062 | type = TYPE_MAIN_VARIANT (type); |
| 15063 | return (CLASS_TYPE_P (type) |
| 15064 | && CP_TYPE_CONTEXT (type) == std_node |
| 15065 | && init_list_identifier == DECL_NAME (TYPE_NAME (type))); |
| 15066 | } |
| 15067 | |
| 15068 | /* Returns true iff DECL is a list constructor: i.e. a constructor which |
| 15069 | will accept an argument list of a single std::initializer_list<T>. */ |
| 15070 | |
| 15071 | bool |
| 15072 | is_list_ctor (tree decl) |
| 15073 | { |
| 15074 | tree args = FUNCTION_FIRST_USER_PARMTYPE (decl); |
| 15075 | tree arg; |
| 15076 | |
| 15077 | if (!args || args == void_list_node) |
| 15078 | return false; |
| 15079 | |
| 15080 | arg = non_reference (TREE_VALUE (args)); |
| 15081 | if (!is_std_init_list (type: arg)) |
| 15082 | return false; |
| 15083 | |
| 15084 | args = TREE_CHAIN (args); |
| 15085 | |
| 15086 | if (args && args != void_list_node && !TREE_PURPOSE (args)) |
| 15087 | /* There are more non-defaulted parms. */ |
| 15088 | return false; |
| 15089 | |
| 15090 | return true; |
| 15091 | } |
| 15092 | |
| 15093 | /* We know that can_convert_arg_bad already said "no" when trying to convert |
| 15094 | FROM to TO with ARG and FLAGS. Try to figure out if it was because |
| 15095 | an explicit conversion function was skipped when looking for a way to |
| 15096 | perform the conversion. At this point we've already printed an error. */ |
| 15097 | |
| 15098 | void |
| 15099 | maybe_show_nonconverting_candidate (tree to, tree from, tree arg, int flags) |
| 15100 | { |
| 15101 | if (!(flags & LOOKUP_ONLYCONVERTING)) |
| 15102 | return; |
| 15103 | |
| 15104 | conversion_obstack_sentinel cos; |
| 15105 | conversion *c = implicit_conversion (to, from, expr: arg, /*c_cast_p=*/false, |
| 15106 | flags: flags & ~LOOKUP_ONLYCONVERTING, complain: tf_none); |
| 15107 | if (c && !c->bad_p && c->user_conv_p) |
| 15108 | /* Ay, the conversion would have worked in direct-init context. */ |
| 15109 | for (; c; c = next_conversion (conv: c)) |
| 15110 | if (c->kind == ck_user |
| 15111 | && DECL_P (c->cand->fn) |
| 15112 | && DECL_NONCONVERTING_P (c->cand->fn)) |
| 15113 | inform (DECL_SOURCE_LOCATION (c->cand->fn), "explicit conversion " |
| 15114 | "function was not considered" ); |
| 15115 | } |
| 15116 | |
| 15117 | /* We're converting EXPR to TYPE. If that conversion involves a conversion |
| 15118 | function and we're binding EXPR to a reference parameter of that function, |
| 15119 | return true. */ |
| 15120 | |
| 15121 | bool |
| 15122 | conv_binds_to_reference_parm_p (tree type, tree expr) |
| 15123 | { |
| 15124 | conversion_obstack_sentinel cos; |
| 15125 | conversion *c = implicit_conversion (to: type, TREE_TYPE (expr), expr, |
| 15126 | /*c_cast_p=*/false, LOOKUP_NORMAL, |
| 15127 | complain: tf_none); |
| 15128 | if (c && !c->bad_p && c->user_conv_p) |
| 15129 | for (; c; c = next_conversion (conv: c)) |
| 15130 | if (c->kind == ck_user) |
| 15131 | for (z_candidate *cand = c->cand; cand; cand = cand->next) |
| 15132 | if (cand->viable == 1) |
| 15133 | for (size_t i = 0; i < cand->num_convs; ++i) |
| 15134 | if (cand->convs[i]->kind == ck_ref_bind |
| 15135 | && conv_get_original_expr (c: cand->convs[i]) == expr) |
| 15136 | return true; |
| 15137 | |
| 15138 | return false; |
| 15139 | } |
| 15140 | |
| 15141 | #include "gt-cp-call.h" |
| 15142 | |