]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/call.cc
dbb01ec6ee55c49a1987f67a4de7a47145bf1973
[gcc.git] / gcc / cp / call.cc
1 /* Functions related to invoking -*- C++ -*- methods and overloaded functions.
2 Copyright (C) 1987-2022 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 "gcc-rich-location.h"
45
46 /* The various kinds of conversion. */
47
48 enum conversion_kind {
49 ck_identity,
50 ck_lvalue,
51 ck_fnptr,
52 ck_qual,
53 ck_std,
54 ck_ptr,
55 ck_pmem,
56 ck_base,
57 ck_ref_bind,
58 ck_user,
59 ck_ambig,
60 ck_list,
61 ck_aggr,
62 ck_rvalue,
63 /* When LOOKUP_SHORTCUT_BAD_CONVS is set, we may return a conversion of
64 this kind whenever we know the true conversion is either bad or outright
65 invalid, but we don't want to attempt to compute the bad conversion (for
66 sake of avoiding unnecessary instantiation). bad_p should always be set
67 for these. */
68 ck_deferred_bad,
69 };
70
71 /* The rank of the conversion. Order of the enumerals matters; better
72 conversions should come earlier in the list. */
73
74 enum conversion_rank {
75 cr_identity,
76 cr_exact,
77 cr_promotion,
78 cr_std,
79 cr_pbool,
80 cr_user,
81 cr_ellipsis,
82 cr_bad
83 };
84
85 /* An implicit conversion sequence, in the sense of [over.best.ics].
86 The first conversion to be performed is at the end of the chain.
87 That conversion is always a cr_identity conversion. */
88
89 struct conversion {
90 /* The kind of conversion represented by this step. */
91 conversion_kind kind;
92 /* The rank of this conversion. */
93 conversion_rank rank;
94 BOOL_BITFIELD user_conv_p : 1;
95 BOOL_BITFIELD ellipsis_p : 1;
96 BOOL_BITFIELD this_p : 1;
97 /* True if this conversion would be permitted with a bending of
98 language standards, e.g. disregarding pointer qualifiers or
99 converting integers to pointers. */
100 BOOL_BITFIELD bad_p : 1;
101 /* If KIND is ck_ref_bind or ck_base, true to indicate that a
102 temporary should be created to hold the result of the
103 conversion. If KIND is ck_ambig or ck_user, true means force
104 copy-initialization. */
105 BOOL_BITFIELD need_temporary_p : 1;
106 /* If KIND is ck_ptr or ck_pmem, true to indicate that a conversion
107 from a pointer-to-derived to pointer-to-base is being performed. */
108 BOOL_BITFIELD base_p : 1;
109 /* If KIND is ck_ref_bind, true when either an lvalue reference is
110 being bound to an lvalue expression or an rvalue reference is
111 being bound to an rvalue expression. If KIND is ck_rvalue or ck_base,
112 true when we are treating an lvalue as an rvalue (12.8p33). If
113 ck_identity, we will be binding a reference directly or decaying to
114 a pointer. */
115 BOOL_BITFIELD rvaluedness_matches_p: 1;
116 BOOL_BITFIELD check_narrowing: 1;
117 /* Whether check_narrowing should only check TREE_CONSTANTs; used
118 in build_converted_constant_expr. */
119 BOOL_BITFIELD check_narrowing_const_only: 1;
120 /* True if this conversion is taking place in a copy-initialization context
121 and we should only consider converting constructors. Only set in
122 ck_base and ck_rvalue. */
123 BOOL_BITFIELD copy_init_p : 1;
124 /* The type of the expression resulting from the conversion. */
125 tree type;
126 union {
127 /* The next conversion in the chain. Since the conversions are
128 arranged from outermost to innermost, the NEXT conversion will
129 actually be performed before this conversion. This variant is
130 used only when KIND is neither ck_identity, ck_aggr, ck_ambig nor
131 ck_list. Please use the next_conversion function instead
132 of using this field directly. */
133 conversion *next;
134 /* The expression at the beginning of the conversion chain. This
135 variant is used only if KIND is ck_identity, ck_aggr, or ck_ambig.
136 You can use conv_get_original_expr to get this expression. */
137 tree expr;
138 /* The array of conversions for an initializer_list, so this
139 variant is used only when KIN D is ck_list. */
140 conversion **list;
141 } u;
142 /* The function candidate corresponding to this conversion
143 sequence. This field is only used if KIND is ck_user. */
144 struct z_candidate *cand;
145 };
146
147 #define CONVERSION_RANK(NODE) \
148 ((NODE)->bad_p ? cr_bad \
149 : (NODE)->ellipsis_p ? cr_ellipsis \
150 : (NODE)->user_conv_p ? cr_user \
151 : (NODE)->rank)
152
153 #define BAD_CONVERSION_RANK(NODE) \
154 ((NODE)->ellipsis_p ? cr_ellipsis \
155 : (NODE)->user_conv_p ? cr_user \
156 : (NODE)->rank)
157
158 static struct obstack conversion_obstack;
159 static bool conversion_obstack_initialized;
160 struct rejection_reason;
161
162 static struct z_candidate * tourney (struct z_candidate *, tsubst_flags_t);
163 static int equal_functions (tree, tree);
164 static int joust (struct z_candidate *, struct z_candidate *, bool,
165 tsubst_flags_t);
166 static int compare_ics (conversion *, conversion *);
167 static void maybe_warn_class_memaccess (location_t, tree,
168 const vec<tree, va_gc> *);
169 static tree build_over_call (struct z_candidate *, int, tsubst_flags_t);
170 static tree convert_like (conversion *, tree, tsubst_flags_t);
171 static tree convert_like_with_context (conversion *, tree, tree, int,
172 tsubst_flags_t);
173 static void op_error (const op_location_t &, enum tree_code, enum tree_code,
174 tree, tree, tree, bool);
175 static struct z_candidate *build_user_type_conversion_1 (tree, tree, int,
176 tsubst_flags_t);
177 static void print_z_candidate (location_t, const char *, struct z_candidate *);
178 static void print_z_candidates (location_t, struct z_candidate *);
179 static tree build_this (tree);
180 static struct z_candidate *splice_viable (struct z_candidate *, bool, bool *);
181 static bool any_strictly_viable (struct z_candidate *);
182 static struct z_candidate *add_template_candidate
183 (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
184 tree, tree, tree, int, unification_kind_t, bool, tsubst_flags_t);
185 static struct z_candidate *add_template_candidate_real
186 (struct z_candidate **, tree, tree, tree, tree, const vec<tree, va_gc> *,
187 tree, tree, tree, int, tree, unification_kind_t, bool, tsubst_flags_t);
188 static bool is_complete (tree);
189 static struct z_candidate *add_conv_candidate
190 (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, tree,
191 tree, tsubst_flags_t);
192 static struct z_candidate *add_function_candidate
193 (struct z_candidate **, tree, tree, tree, const vec<tree, va_gc> *, tree,
194 tree, int, conversion**, bool, tsubst_flags_t);
195 static conversion *implicit_conversion (tree, tree, tree, bool, int,
196 tsubst_flags_t);
197 static conversion *reference_binding (tree, tree, tree, bool, int,
198 tsubst_flags_t);
199 static conversion *build_conv (conversion_kind, tree, conversion *);
200 static conversion *build_list_conv (tree, tree, int, tsubst_flags_t);
201 static conversion *next_conversion (conversion *);
202 static bool is_subseq (conversion *, conversion *);
203 static conversion *maybe_handle_ref_bind (conversion **);
204 static void maybe_handle_implicit_object (conversion **);
205 static struct z_candidate *add_candidate
206 (struct z_candidate **, tree, tree, const vec<tree, va_gc> *, size_t,
207 conversion **, tree, tree, int, struct rejection_reason *, int);
208 static tree source_type (conversion *);
209 static void add_warning (struct z_candidate *, struct z_candidate *);
210 static conversion *direct_reference_binding (tree, conversion *);
211 static bool promoted_arithmetic_type_p (tree);
212 static conversion *conditional_conversion (tree, tree, tsubst_flags_t);
213 static char *name_as_c_string (tree, tree, bool *);
214 static tree prep_operand (tree);
215 static void add_candidates (tree, tree, const vec<tree, va_gc> *, tree, tree,
216 bool, tree, tree, int, struct z_candidate **,
217 tsubst_flags_t);
218 static conversion *merge_conversion_sequences (conversion *, conversion *);
219 static tree build_temp (tree, tree, int, diagnostic_t *, tsubst_flags_t);
220 static conversion *build_identity_conv (tree, tree);
221 static inline bool conv_binds_to_array_of_unknown_bound (conversion *);
222 static bool conv_is_prvalue (conversion *);
223 static tree prevent_lifetime_extension (tree);
224
225 /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
226 NAME can take many forms... */
227
228 bool
229 check_dtor_name (tree basetype, tree name)
230 {
231 /* Just accept something we've already complained about. */
232 if (name == error_mark_node)
233 return true;
234
235 if (TREE_CODE (name) == TYPE_DECL)
236 name = TREE_TYPE (name);
237 else if (TYPE_P (name))
238 /* OK */;
239 else if (identifier_p (name))
240 {
241 if ((MAYBE_CLASS_TYPE_P (basetype)
242 || TREE_CODE (basetype) == ENUMERAL_TYPE)
243 && name == constructor_name (basetype))
244 return true;
245
246 /* Otherwise lookup the name, it could be an unrelated typedef
247 of the correct type. */
248 name = lookup_name (name, LOOK_want::TYPE);
249 if (!name)
250 return false;
251 name = TREE_TYPE (name);
252 if (name == error_mark_node)
253 return false;
254 }
255 else
256 {
257 /* In the case of:
258
259 template <class T> struct S { ~S(); };
260 int i;
261 i.~S();
262
263 NAME will be a class template. */
264 gcc_assert (DECL_CLASS_TEMPLATE_P (name));
265 return false;
266 }
267
268 return same_type_p (TYPE_MAIN_VARIANT (basetype), TYPE_MAIN_VARIANT (name));
269 }
270
271 /* We want the address of a function or method. We avoid creating a
272 pointer-to-member function. */
273
274 tree
275 build_addr_func (tree function, tsubst_flags_t complain)
276 {
277 tree type = TREE_TYPE (function);
278
279 /* We have to do these by hand to avoid real pointer to member
280 functions. */
281 if (TREE_CODE (type) == METHOD_TYPE)
282 {
283 if (TREE_CODE (function) == OFFSET_REF)
284 {
285 tree object = build_address (TREE_OPERAND (function, 0));
286 return get_member_function_from_ptrfunc (&object,
287 TREE_OPERAND (function, 1),
288 complain);
289 }
290 function = build_address (function);
291 }
292 else if (TREE_CODE (function) == FUNCTION_DECL
293 && DECL_IMMEDIATE_FUNCTION_P (function))
294 function = build_address (function);
295 else
296 function = decay_conversion (function, complain, /*reject_builtin=*/false);
297
298 return function;
299 }
300
301 /* Build a CALL_EXPR, we can handle FUNCTION_TYPEs, METHOD_TYPEs, or
302 POINTER_TYPE to those. Note, pointer to member function types
303 (TYPE_PTRMEMFUNC_P) must be handled by our callers. There are
304 two variants. build_call_a is the primitive taking an array of
305 arguments, while build_call_n is a wrapper that handles varargs. */
306
307 tree
308 build_call_n (tree function, int n, ...)
309 {
310 if (n == 0)
311 return build_call_a (function, 0, NULL);
312 else
313 {
314 tree *argarray = XALLOCAVEC (tree, n);
315 va_list ap;
316 int i;
317
318 va_start (ap, n);
319 for (i = 0; i < n; i++)
320 argarray[i] = va_arg (ap, tree);
321 va_end (ap);
322 return build_call_a (function, n, argarray);
323 }
324 }
325
326 /* Update various flags in cfun and the call itself based on what is being
327 called. Split out of build_call_a so that bot_manip can use it too. */
328
329 void
330 set_flags_from_callee (tree call)
331 {
332 /* Handle both CALL_EXPRs and AGGR_INIT_EXPRs. */
333 tree decl = cp_get_callee_fndecl_nofold (call);
334
335 /* We check both the decl and the type; a function may be known not to
336 throw without being declared throw(). */
337 bool nothrow = decl && TREE_NOTHROW (decl);
338 tree callee = cp_get_callee (call);
339 if (callee)
340 nothrow |= TYPE_NOTHROW_P (TREE_TYPE (TREE_TYPE (callee)));
341 else if (TREE_CODE (call) == CALL_EXPR
342 && internal_fn_flags (CALL_EXPR_IFN (call)) & ECF_NOTHROW)
343 nothrow = true;
344
345 if (cfun && cp_function_chain && !cp_unevaluated_operand)
346 {
347 if (!nothrow && at_function_scope_p ())
348 cp_function_chain->can_throw = 1;
349
350 if (decl && TREE_THIS_VOLATILE (decl))
351 current_function_returns_abnormally = 1;
352 }
353
354 TREE_NOTHROW (call) = nothrow;
355 }
356
357 tree
358 build_call_a (tree function, int n, tree *argarray)
359 {
360 tree decl;
361 tree result_type;
362 tree fntype;
363 int i;
364
365 function = build_addr_func (function, tf_warning_or_error);
366
367 gcc_assert (TYPE_PTR_P (TREE_TYPE (function)));
368 fntype = TREE_TYPE (TREE_TYPE (function));
369 gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype));
370 result_type = TREE_TYPE (fntype);
371 /* An rvalue has no cv-qualifiers. */
372 if (SCALAR_TYPE_P (result_type) || VOID_TYPE_P (result_type))
373 result_type = cv_unqualified (result_type);
374
375 function = build_call_array_loc (input_location,
376 result_type, function, n, argarray);
377 set_flags_from_callee (function);
378
379 decl = get_callee_fndecl (function);
380
381 if (decl && !TREE_USED (decl))
382 {
383 /* We invoke build_call directly for several library
384 functions. These may have been declared normally if
385 we're building libgcc, so we can't just check
386 DECL_ARTIFICIAL. */
387 gcc_assert (DECL_ARTIFICIAL (decl)
388 || !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
389 "__", 2));
390 mark_used (decl);
391 }
392
393 require_complete_eh_spec_types (fntype, decl);
394
395 TREE_HAS_CONSTRUCTOR (function) = (decl && DECL_CONSTRUCTOR_P (decl));
396
397 /* Don't pass empty class objects by value. This is useful
398 for tags in STL, which are used to control overload resolution.
399 We don't need to handle other cases of copying empty classes. */
400 if (!decl || !fndecl_built_in_p (decl))
401 for (i = 0; i < n; i++)
402 {
403 tree arg = CALL_EXPR_ARG (function, i);
404 if (is_empty_class (TREE_TYPE (arg))
405 && simple_empty_class_p (TREE_TYPE (arg), arg, INIT_EXPR))
406 {
407 while (TREE_CODE (arg) == TARGET_EXPR)
408 /* We're disconnecting the initializer from its target,
409 don't create a temporary. */
410 arg = TARGET_EXPR_INITIAL (arg);
411 tree t = build0 (EMPTY_CLASS_EXPR, TREE_TYPE (arg));
412 arg = build2 (COMPOUND_EXPR, TREE_TYPE (t), arg, t);
413 CALL_EXPR_ARG (function, i) = arg;
414 }
415 }
416
417 return function;
418 }
419
420 /* New overloading code. */
421
422 struct z_candidate;
423
424 struct candidate_warning {
425 z_candidate *loser;
426 candidate_warning *next;
427 };
428
429 /* Information for providing diagnostics about why overloading failed. */
430
431 enum rejection_reason_code {
432 rr_none,
433 rr_arity,
434 rr_explicit_conversion,
435 rr_template_conversion,
436 rr_arg_conversion,
437 rr_bad_arg_conversion,
438 rr_template_unification,
439 rr_invalid_copy,
440 rr_inherited_ctor,
441 rr_constraint_failure
442 };
443
444 struct conversion_info {
445 /* The index of the argument, 0-based. */
446 int n_arg;
447 /* The actual argument or its type. */
448 tree from;
449 /* The type of the parameter. */
450 tree to_type;
451 /* The location of the argument. */
452 location_t loc;
453 };
454
455 struct rejection_reason {
456 enum rejection_reason_code code;
457 union {
458 /* Information about an arity mismatch. */
459 struct {
460 /* The expected number of arguments. */
461 int expected;
462 /* The actual number of arguments in the call. */
463 int actual;
464 /* Whether EXPECTED should be treated as a lower bound. */
465 bool least_p;
466 } arity;
467 /* Information about an argument conversion mismatch. */
468 struct conversion_info conversion;
469 /* Same, but for bad argument conversions. */
470 struct conversion_info bad_conversion;
471 /* Information about template unification failures. These are the
472 parameters passed to fn_type_unification. */
473 struct {
474 tree tmpl;
475 tree explicit_targs;
476 int num_targs;
477 const tree *args;
478 unsigned int nargs;
479 tree return_type;
480 unification_kind_t strict;
481 int flags;
482 } template_unification;
483 /* Information about template instantiation failures. These are the
484 parameters passed to instantiate_template. */
485 struct {
486 tree tmpl;
487 tree targs;
488 } template_instantiation;
489 } u;
490 };
491
492 struct z_candidate {
493 /* The FUNCTION_DECL that will be called if this candidate is
494 selected by overload resolution. */
495 tree fn;
496 /* If not NULL_TREE, the first argument to use when calling this
497 function. */
498 tree first_arg;
499 /* The rest of the arguments to use when calling this function. If
500 there are no further arguments this may be NULL or it may be an
501 empty vector. */
502 const vec<tree, va_gc> *args;
503 /* The implicit conversion sequences for each of the arguments to
504 FN. */
505 conversion **convs;
506 /* The number of implicit conversion sequences. */
507 size_t num_convs;
508 /* If FN is a user-defined conversion, the standard conversion
509 sequence from the type returned by FN to the desired destination
510 type. */
511 conversion *second_conv;
512 struct rejection_reason *reason;
513 /* If FN is a member function, the binfo indicating the path used to
514 qualify the name of FN at the call site. This path is used to
515 determine whether or not FN is accessible if it is selected by
516 overload resolution. The DECL_CONTEXT of FN will always be a
517 (possibly improper) base of this binfo. */
518 tree access_path;
519 /* If FN is a non-static member function, the binfo indicating the
520 subobject to which the `this' pointer should be converted if FN
521 is selected by overload resolution. The type pointed to by
522 the `this' pointer must correspond to the most derived class
523 indicated by the CONVERSION_PATH. */
524 tree conversion_path;
525 tree template_decl;
526 tree explicit_targs;
527 candidate_warning *warnings;
528 z_candidate *next;
529 int viable;
530
531 /* The flags active in add_candidate. */
532 int flags;
533
534 bool rewritten () const { return (flags & LOOKUP_REWRITTEN); }
535 bool reversed () const { return (flags & LOOKUP_REVERSED); }
536 };
537
538 /* Returns true iff T is a null pointer constant in the sense of
539 [conv.ptr]. */
540
541 bool
542 null_ptr_cst_p (tree t)
543 {
544 tree type = TREE_TYPE (t);
545
546 /* [conv.ptr]
547
548 A null pointer constant is an integer literal ([lex.icon]) with value
549 zero or a prvalue of type std::nullptr_t. */
550 if (NULLPTR_TYPE_P (type))
551 return true;
552
553 if (cxx_dialect >= cxx11)
554 {
555 STRIP_ANY_LOCATION_WRAPPER (t);
556
557 /* Core issue 903 says only literal 0 is a null pointer constant. */
558 if (TREE_CODE (t) == INTEGER_CST
559 && !TREE_OVERFLOW (t)
560 && TREE_CODE (type) == INTEGER_TYPE
561 && integer_zerop (t)
562 && !char_type_p (type))
563 return true;
564 }
565 else if (CP_INTEGRAL_TYPE_P (type))
566 {
567 t = fold_non_dependent_expr (t, tf_none);
568 STRIP_NOPS (t);
569 if (integer_zerop (t) && !TREE_OVERFLOW (t))
570 return true;
571 }
572
573 return false;
574 }
575
576 /* Returns true iff T is a null member pointer value (4.11). */
577
578 bool
579 null_member_pointer_value_p (tree t)
580 {
581 tree type = TREE_TYPE (t);
582 if (!type)
583 return false;
584 else if (TYPE_PTRMEMFUNC_P (type))
585 return (TREE_CODE (t) == CONSTRUCTOR
586 && CONSTRUCTOR_NELTS (t)
587 && integer_zerop (CONSTRUCTOR_ELT (t, 0)->value));
588 else if (TYPE_PTRDATAMEM_P (type))
589 return integer_all_onesp (t);
590 else
591 return false;
592 }
593
594 /* Returns nonzero if PARMLIST consists of only default parms,
595 ellipsis, and/or undeduced parameter packs. */
596
597 bool
598 sufficient_parms_p (const_tree parmlist)
599 {
600 for (; parmlist && parmlist != void_list_node;
601 parmlist = TREE_CHAIN (parmlist))
602 if (!TREE_PURPOSE (parmlist)
603 && !PACK_EXPANSION_P (TREE_VALUE (parmlist)))
604 return false;
605 return true;
606 }
607
608 /* Allocate N bytes of memory from the conversion obstack. The memory
609 is zeroed before being returned. */
610
611 static void *
612 conversion_obstack_alloc (size_t n)
613 {
614 void *p;
615 if (!conversion_obstack_initialized)
616 {
617 gcc_obstack_init (&conversion_obstack);
618 conversion_obstack_initialized = true;
619 }
620 p = obstack_alloc (&conversion_obstack, n);
621 memset (p, 0, n);
622 return p;
623 }
624
625 /* Allocate rejection reasons. */
626
627 static struct rejection_reason *
628 alloc_rejection (enum rejection_reason_code code)
629 {
630 struct rejection_reason *p;
631 p = (struct rejection_reason *) conversion_obstack_alloc (sizeof *p);
632 p->code = code;
633 return p;
634 }
635
636 static struct rejection_reason *
637 arity_rejection (tree first_arg, int expected, int actual, bool least_p = false)
638 {
639 struct rejection_reason *r = alloc_rejection (rr_arity);
640 int adjust = first_arg != NULL_TREE;
641 r->u.arity.expected = expected - adjust;
642 r->u.arity.actual = actual - adjust;
643 r->u.arity.least_p = least_p;
644 return r;
645 }
646
647 static struct rejection_reason *
648 arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to,
649 location_t loc)
650 {
651 struct rejection_reason *r = alloc_rejection (rr_arg_conversion);
652 int adjust = first_arg != NULL_TREE;
653 r->u.conversion.n_arg = n_arg - adjust;
654 r->u.conversion.from = from;
655 r->u.conversion.to_type = to;
656 r->u.conversion.loc = loc;
657 return r;
658 }
659
660 static struct rejection_reason *
661 bad_arg_conversion_rejection (tree first_arg, int n_arg, tree from, tree to,
662 location_t loc)
663 {
664 struct rejection_reason *r = alloc_rejection (rr_bad_arg_conversion);
665 int adjust = first_arg != NULL_TREE;
666 r->u.bad_conversion.n_arg = n_arg - adjust;
667 r->u.bad_conversion.from = from;
668 r->u.bad_conversion.to_type = to;
669 r->u.bad_conversion.loc = loc;
670 return r;
671 }
672
673 static struct rejection_reason *
674 explicit_conversion_rejection (tree from, tree to)
675 {
676 struct rejection_reason *r = alloc_rejection (rr_explicit_conversion);
677 r->u.conversion.n_arg = 0;
678 r->u.conversion.from = from;
679 r->u.conversion.to_type = to;
680 r->u.conversion.loc = UNKNOWN_LOCATION;
681 return r;
682 }
683
684 static struct rejection_reason *
685 template_conversion_rejection (tree from, tree to)
686 {
687 struct rejection_reason *r = alloc_rejection (rr_template_conversion);
688 r->u.conversion.n_arg = 0;
689 r->u.conversion.from = from;
690 r->u.conversion.to_type = to;
691 r->u.conversion.loc = UNKNOWN_LOCATION;
692 return r;
693 }
694
695 static struct rejection_reason *
696 template_unification_rejection (tree tmpl, tree explicit_targs, tree targs,
697 const tree *args, unsigned int nargs,
698 tree return_type, unification_kind_t strict,
699 int flags)
700 {
701 size_t args_n_bytes = sizeof (*args) * nargs;
702 tree *args1 = (tree *) conversion_obstack_alloc (args_n_bytes);
703 struct rejection_reason *r = alloc_rejection (rr_template_unification);
704 r->u.template_unification.tmpl = tmpl;
705 r->u.template_unification.explicit_targs = explicit_targs;
706 r->u.template_unification.num_targs = TREE_VEC_LENGTH (targs);
707 /* Copy args to our own storage. */
708 memcpy (args1, args, args_n_bytes);
709 r->u.template_unification.args = args1;
710 r->u.template_unification.nargs = nargs;
711 r->u.template_unification.return_type = return_type;
712 r->u.template_unification.strict = strict;
713 r->u.template_unification.flags = flags;
714 return r;
715 }
716
717 static struct rejection_reason *
718 template_unification_error_rejection (void)
719 {
720 return alloc_rejection (rr_template_unification);
721 }
722
723 static struct rejection_reason *
724 invalid_copy_with_fn_template_rejection (void)
725 {
726 struct rejection_reason *r = alloc_rejection (rr_invalid_copy);
727 return r;
728 }
729
730 static struct rejection_reason *
731 inherited_ctor_rejection (void)
732 {
733 struct rejection_reason *r = alloc_rejection (rr_inherited_ctor);
734 return r;
735 }
736
737 /* Build a constraint failure record. */
738
739 static struct rejection_reason *
740 constraint_failure (void)
741 {
742 struct rejection_reason *r = alloc_rejection (rr_constraint_failure);
743 return r;
744 }
745
746 /* Dynamically allocate a conversion. */
747
748 static conversion *
749 alloc_conversion (conversion_kind kind)
750 {
751 conversion *c;
752 c = (conversion *) conversion_obstack_alloc (sizeof (conversion));
753 c->kind = kind;
754 return c;
755 }
756
757 /* Make sure that all memory on the conversion obstack has been
758 freed. */
759
760 void
761 validate_conversion_obstack (void)
762 {
763 if (conversion_obstack_initialized)
764 gcc_assert ((obstack_next_free (&conversion_obstack)
765 == obstack_base (&conversion_obstack)));
766 }
767
768 /* Dynamically allocate an array of N conversions. */
769
770 static conversion **
771 alloc_conversions (size_t n)
772 {
773 return (conversion **) conversion_obstack_alloc (n * sizeof (conversion *));
774 }
775
776 /* True iff the active member of conversion::u for code CODE is NEXT. */
777
778 static inline bool
779 has_next (conversion_kind code)
780 {
781 return !(code == ck_identity
782 || code == ck_ambig
783 || code == ck_list
784 || code == ck_aggr
785 || code == ck_deferred_bad);
786 }
787
788 static conversion *
789 build_conv (conversion_kind code, tree type, conversion *from)
790 {
791 conversion *t;
792 conversion_rank rank = CONVERSION_RANK (from);
793
794 /* Only call this function for conversions that use u.next. */
795 gcc_assert (from == NULL || has_next (code));
796
797 /* Note that the caller is responsible for filling in t->cand for
798 user-defined conversions. */
799 t = alloc_conversion (code);
800 t->type = type;
801 t->u.next = from;
802
803 switch (code)
804 {
805 case ck_ptr:
806 case ck_pmem:
807 case ck_base:
808 case ck_std:
809 if (rank < cr_std)
810 rank = cr_std;
811 break;
812
813 case ck_qual:
814 case ck_fnptr:
815 if (rank < cr_exact)
816 rank = cr_exact;
817 break;
818
819 default:
820 break;
821 }
822 t->rank = rank;
823 t->user_conv_p = (code == ck_user || from->user_conv_p);
824 t->bad_p = from->bad_p;
825 t->base_p = false;
826 return t;
827 }
828
829 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
830 specialization of std::initializer_list<T>, if such a conversion is
831 possible. */
832
833 static conversion *
834 build_list_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
835 {
836 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (type), 0);
837 unsigned len = CONSTRUCTOR_NELTS (ctor);
838 conversion **subconvs = alloc_conversions (len);
839 conversion *t;
840 unsigned i;
841 tree val;
842
843 /* Within a list-initialization we can have more user-defined
844 conversions. */
845 flags &= ~LOOKUP_NO_CONVERSION;
846 /* But no narrowing conversions. */
847 flags |= LOOKUP_NO_NARROWING;
848
849 /* Can't make an array of these types. */
850 if (TYPE_REF_P (elttype)
851 || TREE_CODE (elttype) == FUNCTION_TYPE
852 || VOID_TYPE_P (elttype))
853 return NULL;
854
855 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), i, val)
856 {
857 conversion *sub
858 = implicit_conversion (elttype, TREE_TYPE (val), val,
859 false, flags, complain);
860 if (sub == NULL)
861 return NULL;
862
863 subconvs[i] = sub;
864 }
865
866 t = alloc_conversion (ck_list);
867 t->type = type;
868 t->u.list = subconvs;
869 t->rank = cr_exact;
870
871 for (i = 0; i < len; ++i)
872 {
873 conversion *sub = subconvs[i];
874 if (sub->rank > t->rank)
875 t->rank = sub->rank;
876 if (sub->user_conv_p)
877 t->user_conv_p = true;
878 if (sub->bad_p)
879 t->bad_p = true;
880 }
881
882 return t;
883 }
884
885 /* Return the next conversion of the conversion chain (if applicable),
886 or NULL otherwise. Please use this function instead of directly
887 accessing fields of struct conversion. */
888
889 static conversion *
890 next_conversion (conversion *conv)
891 {
892 if (conv == NULL
893 || !has_next (conv->kind))
894 return NULL;
895 return conv->u.next;
896 }
897
898 /* Strip to the first ck_user, ck_ambig, ck_list, ck_aggr or ck_identity
899 encountered. */
900
901 static conversion *
902 strip_standard_conversion (conversion *conv)
903 {
904 while (conv
905 && conv->kind != ck_user
906 && has_next (conv->kind))
907 conv = next_conversion (conv);
908 return conv;
909 }
910
911 /* Subroutine of build_aggr_conv: check whether FROM is a valid aggregate
912 initializer for array type ATYPE. */
913
914 static bool
915 can_convert_array (tree atype, tree from, int flags, tsubst_flags_t complain)
916 {
917 tree elttype = TREE_TYPE (atype);
918 unsigned i;
919
920 if (TREE_CODE (from) == CONSTRUCTOR)
921 {
922 for (i = 0; i < CONSTRUCTOR_NELTS (from); ++i)
923 {
924 tree val = CONSTRUCTOR_ELT (from, i)->value;
925 bool ok;
926 if (TREE_CODE (elttype) == ARRAY_TYPE)
927 ok = can_convert_array (elttype, val, flags, complain);
928 else
929 ok = can_convert_arg (elttype, TREE_TYPE (val), val, flags,
930 complain);
931 if (!ok)
932 return false;
933 }
934 return true;
935 }
936
937 if (char_type_p (TYPE_MAIN_VARIANT (elttype))
938 && TREE_CODE (tree_strip_any_location_wrapper (from)) == STRING_CST)
939 return array_string_literal_compatible_p (atype, from);
940
941 /* No other valid way to aggregate initialize an array. */
942 return false;
943 }
944
945 /* Helper for build_aggr_conv. Return true if FIELD is in PSET, or if
946 FIELD has ANON_AGGR_TYPE_P and any initializable field in there recursively
947 is in PSET. */
948
949 static bool
950 field_in_pset (hash_set<tree, true> &pset, tree field)
951 {
952 if (pset.contains (field))
953 return true;
954 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
955 for (field = TYPE_FIELDS (TREE_TYPE (field));
956 field; field = DECL_CHAIN (field))
957 {
958 field = next_aggregate_field (field);
959 if (field == NULL_TREE)
960 break;
961 if (field_in_pset (pset, field))
962 return true;
963 }
964 return false;
965 }
966
967 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
968 aggregate class, if such a conversion is possible. */
969
970 static conversion *
971 build_aggr_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
972 {
973 unsigned HOST_WIDE_INT i = 0;
974 conversion *c;
975 tree field = next_aggregate_field (TYPE_FIELDS (type));
976 tree empty_ctor = NULL_TREE;
977 hash_set<tree, true> pset;
978
979 /* We already called reshape_init in implicit_conversion, but it might not
980 have done anything in the case of parenthesized aggr init. */
981
982 /* The conversions within the init-list aren't affected by the enclosing
983 context; they're always simple copy-initialization. */
984 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
985
986 /* For designated initializers, verify that each initializer is convertible
987 to corresponding TREE_TYPE (ce->index) and mark those FIELD_DECLs as
988 visited. In the following loop then ignore already visited
989 FIELD_DECLs. */
990 tree idx, val;
991 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, val)
992 {
993 if (!idx)
994 break;
995
996 gcc_checking_assert (TREE_CODE (idx) == FIELD_DECL);
997
998 tree ftype = TREE_TYPE (idx);
999 bool ok;
1000
1001 if (TREE_CODE (ftype) == ARRAY_TYPE)
1002 ok = can_convert_array (ftype, val, flags, complain);
1003 else
1004 ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1005 complain);
1006
1007 if (!ok)
1008 return NULL;
1009
1010 /* For unions, there should be just one initializer. */
1011 if (TREE_CODE (type) == UNION_TYPE)
1012 {
1013 field = NULL_TREE;
1014 i = 1;
1015 break;
1016 }
1017 pset.add (idx);
1018 }
1019
1020 for (; field; field = next_aggregate_field (DECL_CHAIN (field)))
1021 {
1022 tree ftype = TREE_TYPE (field);
1023 bool ok;
1024
1025 if (!pset.is_empty () && field_in_pset (pset, field))
1026 continue;
1027 if (i < CONSTRUCTOR_NELTS (ctor))
1028 {
1029 constructor_elt *ce = CONSTRUCTOR_ELT (ctor, i);
1030 gcc_checking_assert (!ce->index);
1031 val = ce->value;
1032 ++i;
1033 }
1034 else if (DECL_INITIAL (field))
1035 val = get_nsdmi (field, /*ctor*/false, complain);
1036 else if (TYPE_REF_P (ftype))
1037 /* Value-initialization of reference is ill-formed. */
1038 return NULL;
1039 else
1040 {
1041 if (empty_ctor == NULL_TREE)
1042 empty_ctor = build_constructor (init_list_type_node, NULL);
1043 val = empty_ctor;
1044 }
1045
1046 if (TREE_CODE (ftype) == ARRAY_TYPE)
1047 ok = can_convert_array (ftype, val, flags, complain);
1048 else
1049 ok = can_convert_arg (ftype, TREE_TYPE (val), val, flags,
1050 complain);
1051
1052 if (!ok)
1053 return NULL;
1054
1055 if (TREE_CODE (type) == UNION_TYPE)
1056 break;
1057 }
1058
1059 if (i < CONSTRUCTOR_NELTS (ctor))
1060 return NULL;
1061
1062 c = alloc_conversion (ck_aggr);
1063 c->type = type;
1064 c->rank = cr_exact;
1065 c->user_conv_p = true;
1066 c->check_narrowing = true;
1067 c->u.expr = ctor;
1068 return c;
1069 }
1070
1071 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, an
1072 array type, if such a conversion is possible. */
1073
1074 static conversion *
1075 build_array_conv (tree type, tree ctor, int flags, tsubst_flags_t complain)
1076 {
1077 conversion *c;
1078 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1079 tree elttype = TREE_TYPE (type);
1080 bool bad = false;
1081 bool user = false;
1082 enum conversion_rank rank = cr_exact;
1083
1084 /* We might need to propagate the size from the element to the array. */
1085 complete_type (type);
1086
1087 if (TYPE_DOMAIN (type)
1088 && !variably_modified_type_p (TYPE_DOMAIN (type), NULL_TREE))
1089 {
1090 unsigned HOST_WIDE_INT alen = tree_to_uhwi (array_type_nelts_top (type));
1091 if (alen < len)
1092 return NULL;
1093 }
1094
1095 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1096
1097 for (auto &e: CONSTRUCTOR_ELTS (ctor))
1098 {
1099 conversion *sub
1100 = implicit_conversion (elttype, TREE_TYPE (e.value), e.value,
1101 false, flags, complain);
1102 if (sub == NULL)
1103 return NULL;
1104
1105 if (sub->rank > rank)
1106 rank = sub->rank;
1107 if (sub->user_conv_p)
1108 user = true;
1109 if (sub->bad_p)
1110 bad = true;
1111 }
1112
1113 c = alloc_conversion (ck_aggr);
1114 c->type = type;
1115 c->rank = rank;
1116 c->user_conv_p = user;
1117 c->bad_p = bad;
1118 c->u.expr = ctor;
1119 return c;
1120 }
1121
1122 /* Represent a conversion from CTOR, a braced-init-list, to TYPE, a
1123 complex type, if such a conversion is possible. */
1124
1125 static conversion *
1126 build_complex_conv (tree type, tree ctor, int flags,
1127 tsubst_flags_t complain)
1128 {
1129 conversion *c;
1130 unsigned HOST_WIDE_INT len = CONSTRUCTOR_NELTS (ctor);
1131 tree elttype = TREE_TYPE (type);
1132 bool bad = false;
1133 bool user = false;
1134 enum conversion_rank rank = cr_exact;
1135
1136 if (len != 2)
1137 return NULL;
1138
1139 flags = LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING;
1140
1141 for (auto &e: CONSTRUCTOR_ELTS (ctor))
1142 {
1143 conversion *sub
1144 = implicit_conversion (elttype, TREE_TYPE (e.value), e.value,
1145 false, flags, complain);
1146 if (sub == NULL)
1147 return NULL;
1148
1149 if (sub->rank > rank)
1150 rank = sub->rank;
1151 if (sub->user_conv_p)
1152 user = true;
1153 if (sub->bad_p)
1154 bad = true;
1155 }
1156
1157 c = alloc_conversion (ck_aggr);
1158 c->type = type;
1159 c->rank = rank;
1160 c->user_conv_p = user;
1161 c->bad_p = bad;
1162 c->u.expr = ctor;
1163 return c;
1164 }
1165
1166 /* Build a representation of the identity conversion from EXPR to
1167 itself. The TYPE should match the type of EXPR, if EXPR is non-NULL. */
1168
1169 static conversion *
1170 build_identity_conv (tree type, tree expr)
1171 {
1172 conversion *c;
1173
1174 c = alloc_conversion (ck_identity);
1175 c->type = type;
1176 c->u.expr = expr;
1177
1178 return c;
1179 }
1180
1181 /* Converting from EXPR to TYPE was ambiguous in the sense that there
1182 were multiple user-defined conversions to accomplish the job.
1183 Build a conversion that indicates that ambiguity. */
1184
1185 static conversion *
1186 build_ambiguous_conv (tree type, tree expr)
1187 {
1188 conversion *c;
1189
1190 c = alloc_conversion (ck_ambig);
1191 c->type = type;
1192 c->u.expr = expr;
1193
1194 return c;
1195 }
1196
1197 tree
1198 strip_top_quals (tree t)
1199 {
1200 if (TREE_CODE (t) == ARRAY_TYPE)
1201 return t;
1202 return cp_build_qualified_type (t, 0);
1203 }
1204
1205 /* Returns the standard conversion path (see [conv]) from type FROM to type
1206 TO, if any. For proper handling of null pointer constants, you must
1207 also pass the expression EXPR to convert from. If C_CAST_P is true,
1208 this conversion is coming from a C-style cast. */
1209
1210 static conversion *
1211 standard_conversion (tree to, tree from, tree expr, bool c_cast_p,
1212 int flags, tsubst_flags_t complain)
1213 {
1214 enum tree_code fcode, tcode;
1215 conversion *conv;
1216 bool fromref = false;
1217 tree qualified_to;
1218
1219 to = non_reference (to);
1220 if (TYPE_REF_P (from))
1221 {
1222 fromref = true;
1223 from = TREE_TYPE (from);
1224 }
1225 qualified_to = to;
1226 to = strip_top_quals (to);
1227 from = strip_top_quals (from);
1228
1229 if (expr && type_unknown_p (expr))
1230 {
1231 if (TYPE_PTRFN_P (to) || TYPE_PTRMEMFUNC_P (to))
1232 {
1233 tsubst_flags_t tflags = tf_conv;
1234 expr = instantiate_type (to, expr, tflags);
1235 if (expr == error_mark_node)
1236 return NULL;
1237 from = TREE_TYPE (expr);
1238 }
1239 else if (TREE_CODE (to) == BOOLEAN_TYPE)
1240 {
1241 /* Necessary for eg, TEMPLATE_ID_EXPRs (c++/50961). */
1242 expr = resolve_nondeduced_context (expr, complain);
1243 from = TREE_TYPE (expr);
1244 }
1245 }
1246
1247 fcode = TREE_CODE (from);
1248 tcode = TREE_CODE (to);
1249
1250 conv = build_identity_conv (from, expr);
1251 if (fcode == FUNCTION_TYPE || fcode == ARRAY_TYPE)
1252 {
1253 from = type_decays_to (from);
1254 fcode = TREE_CODE (from);
1255 /* Tell convert_like that we're using the address. */
1256 conv->rvaluedness_matches_p = true;
1257 conv = build_conv (ck_lvalue, from, conv);
1258 }
1259 /* Wrapping a ck_rvalue around a class prvalue (as a result of using
1260 obvalue_p) seems odd, since it's already a prvalue, but that's how we
1261 express the copy constructor call required by copy-initialization. */
1262 else if (fromref || (expr && obvalue_p (expr)))
1263 {
1264 if (expr)
1265 {
1266 tree bitfield_type;
1267 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
1268 if (bitfield_type)
1269 {
1270 from = strip_top_quals (bitfield_type);
1271 fcode = TREE_CODE (from);
1272 }
1273 }
1274 conv = build_conv (ck_rvalue, from, conv);
1275 /* If we're performing copy-initialization, remember to skip
1276 explicit constructors. */
1277 if (flags & LOOKUP_ONLYCONVERTING)
1278 conv->copy_init_p = true;
1279 }
1280
1281 /* Allow conversion between `__complex__' data types. */
1282 if (tcode == COMPLEX_TYPE && fcode == COMPLEX_TYPE)
1283 {
1284 /* The standard conversion sequence to convert FROM to TO is
1285 the standard conversion sequence to perform componentwise
1286 conversion. */
1287 conversion *part_conv = standard_conversion
1288 (TREE_TYPE (to), TREE_TYPE (from), NULL_TREE, c_cast_p, flags,
1289 complain);
1290
1291 if (!part_conv)
1292 conv = NULL;
1293 else if (part_conv->kind == ck_identity)
1294 /* Leave conv alone. */;
1295 else
1296 {
1297 conv = build_conv (part_conv->kind, to, conv);
1298 conv->rank = part_conv->rank;
1299 }
1300
1301 return conv;
1302 }
1303
1304 if (same_type_p (from, to))
1305 {
1306 if (CLASS_TYPE_P (to) && conv->kind == ck_rvalue)
1307 conv->type = qualified_to;
1308 return conv;
1309 }
1310
1311 /* [conv.ptr]
1312 A null pointer constant can be converted to a pointer type; ... A
1313 null pointer constant of integral type can be converted to an
1314 rvalue of type std::nullptr_t. */
1315 if ((tcode == POINTER_TYPE || TYPE_PTRMEM_P (to)
1316 || NULLPTR_TYPE_P (to))
1317 && ((expr && null_ptr_cst_p (expr))
1318 || NULLPTR_TYPE_P (from)))
1319 conv = build_conv (ck_std, to, conv);
1320 else if ((tcode == INTEGER_TYPE && fcode == POINTER_TYPE)
1321 || (tcode == POINTER_TYPE && fcode == INTEGER_TYPE))
1322 {
1323 /* For backwards brain damage compatibility, allow interconversion of
1324 pointers and integers with a pedwarn. */
1325 conv = build_conv (ck_std, to, conv);
1326 conv->bad_p = true;
1327 }
1328 else if (UNSCOPED_ENUM_P (to) && fcode == INTEGER_TYPE)
1329 {
1330 /* For backwards brain damage compatibility, allow interconversion of
1331 enums and integers with a pedwarn. */
1332 conv = build_conv (ck_std, to, conv);
1333 conv->bad_p = true;
1334 }
1335 else if ((tcode == POINTER_TYPE && fcode == POINTER_TYPE)
1336 || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from)))
1337 {
1338 tree to_pointee;
1339 tree from_pointee;
1340
1341 if (tcode == POINTER_TYPE)
1342 {
1343 to_pointee = TREE_TYPE (to);
1344 from_pointee = TREE_TYPE (from);
1345
1346 /* Since this is the target of a pointer, it can't have function
1347 qualifiers, so any TYPE_QUALS must be for attributes const or
1348 noreturn. Strip them. */
1349 if (TREE_CODE (to_pointee) == FUNCTION_TYPE
1350 && TYPE_QUALS (to_pointee))
1351 to_pointee = build_qualified_type (to_pointee, TYPE_UNQUALIFIED);
1352 if (TREE_CODE (from_pointee) == FUNCTION_TYPE
1353 && TYPE_QUALS (from_pointee))
1354 from_pointee = build_qualified_type (from_pointee, TYPE_UNQUALIFIED);
1355 }
1356 else
1357 {
1358 to_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (to);
1359 from_pointee = TYPE_PTRMEM_POINTED_TO_TYPE (from);
1360 }
1361
1362 if (tcode == POINTER_TYPE
1363 && same_type_ignoring_top_level_qualifiers_p (from_pointee,
1364 to_pointee))
1365 ;
1366 else if (VOID_TYPE_P (to_pointee)
1367 && !TYPE_PTRDATAMEM_P (from)
1368 && TREE_CODE (from_pointee) != FUNCTION_TYPE)
1369 {
1370 tree nfrom = TREE_TYPE (from);
1371 /* Don't try to apply restrict to void. */
1372 int quals = cp_type_quals (nfrom) & ~TYPE_QUAL_RESTRICT;
1373 from_pointee = cp_build_qualified_type (void_type_node, quals);
1374 from = build_pointer_type (from_pointee);
1375 conv = build_conv (ck_ptr, from, conv);
1376 }
1377 else if (TYPE_PTRDATAMEM_P (from))
1378 {
1379 tree fbase = TYPE_PTRMEM_CLASS_TYPE (from);
1380 tree tbase = TYPE_PTRMEM_CLASS_TYPE (to);
1381
1382 if (same_type_p (fbase, tbase))
1383 /* No base conversion needed. */;
1384 else if (DERIVED_FROM_P (fbase, tbase)
1385 && (same_type_ignoring_top_level_qualifiers_p
1386 (from_pointee, to_pointee)))
1387 {
1388 from = build_ptrmem_type (tbase, from_pointee);
1389 conv = build_conv (ck_pmem, from, conv);
1390 }
1391 else
1392 return NULL;
1393 }
1394 else if (CLASS_TYPE_P (from_pointee)
1395 && CLASS_TYPE_P (to_pointee)
1396 /* [conv.ptr]
1397
1398 An rvalue of type "pointer to cv D," where D is a
1399 class type, can be converted to an rvalue of type
1400 "pointer to cv B," where B is a base class (clause
1401 _class.derived_) of D. If B is an inaccessible
1402 (clause _class.access_) or ambiguous
1403 (_class.member.lookup_) base class of D, a program
1404 that necessitates this conversion is ill-formed.
1405 Therefore, we use DERIVED_FROM_P, and do not check
1406 access or uniqueness. */
1407 && DERIVED_FROM_P (to_pointee, from_pointee))
1408 {
1409 from_pointee
1410 = cp_build_qualified_type (to_pointee,
1411 cp_type_quals (from_pointee));
1412 from = build_pointer_type (from_pointee);
1413 conv = build_conv (ck_ptr, from, conv);
1414 conv->base_p = true;
1415 }
1416
1417 if (same_type_p (from, to))
1418 /* OK */;
1419 else if (c_cast_p && comp_ptr_ttypes_const (to, from, bounds_either))
1420 /* In a C-style cast, we ignore CV-qualification because we
1421 are allowed to perform a static_cast followed by a
1422 const_cast. */
1423 conv = build_conv (ck_qual, to, conv);
1424 else if (!c_cast_p && comp_ptr_ttypes (to_pointee, from_pointee))
1425 conv = build_conv (ck_qual, to, conv);
1426 else if (expr && string_conv_p (to, expr, 0))
1427 /* converting from string constant to char *. */
1428 conv = build_conv (ck_qual, to, conv);
1429 else if (fnptr_conv_p (to, from))
1430 conv = build_conv (ck_fnptr, to, conv);
1431 /* Allow conversions among compatible ObjC pointer types (base
1432 conversions have been already handled above). */
1433 else if (c_dialect_objc ()
1434 && objc_compare_types (to, from, -4, NULL_TREE))
1435 conv = build_conv (ck_ptr, to, conv);
1436 else if (ptr_reasonably_similar (to_pointee, from_pointee))
1437 {
1438 conv = build_conv (ck_ptr, to, conv);
1439 conv->bad_p = true;
1440 }
1441 else
1442 return NULL;
1443
1444 from = to;
1445 }
1446 else if (TYPE_PTRMEMFUNC_P (to) && TYPE_PTRMEMFUNC_P (from))
1447 {
1448 tree fromfn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (from));
1449 tree tofn = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (to));
1450 tree fbase = class_of_this_parm (fromfn);
1451 tree tbase = class_of_this_parm (tofn);
1452
1453 /* If FBASE and TBASE are equivalent but incomplete, DERIVED_FROM_P
1454 yields false. But a pointer to member of incomplete class is OK. */
1455 if (!same_type_p (fbase, tbase) && !DERIVED_FROM_P (fbase, tbase))
1456 return NULL;
1457
1458 tree fstat = static_fn_type (fromfn);
1459 tree tstat = static_fn_type (tofn);
1460 if (same_type_p (tstat, fstat)
1461 || fnptr_conv_p (tstat, fstat))
1462 /* OK */;
1463 else
1464 return NULL;
1465
1466 if (!same_type_p (fbase, tbase))
1467 {
1468 from = build_memfn_type (fstat,
1469 tbase,
1470 cp_type_quals (tbase),
1471 type_memfn_rqual (tofn));
1472 from = build_ptrmemfunc_type (build_pointer_type (from));
1473 conv = build_conv (ck_pmem, from, conv);
1474 conv->base_p = true;
1475 }
1476 if (fnptr_conv_p (tstat, fstat))
1477 conv = build_conv (ck_fnptr, to, conv);
1478 }
1479 else if (tcode == BOOLEAN_TYPE)
1480 {
1481 /* [conv.bool]
1482
1483 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer
1484 to member type can be converted to a prvalue of type bool. ...
1485 For direct-initialization (8.5 [dcl.init]), a prvalue of type
1486 std::nullptr_t can be converted to a prvalue of type bool; */
1487 if (ARITHMETIC_TYPE_P (from)
1488 || UNSCOPED_ENUM_P (from)
1489 || fcode == POINTER_TYPE
1490 || TYPE_PTRMEM_P (from)
1491 || NULLPTR_TYPE_P (from))
1492 {
1493 conv = build_conv (ck_std, to, conv);
1494 if (fcode == POINTER_TYPE
1495 || TYPE_PTRDATAMEM_P (from)
1496 || (TYPE_PTRMEMFUNC_P (from)
1497 && conv->rank < cr_pbool)
1498 || NULLPTR_TYPE_P (from))
1499 conv->rank = cr_pbool;
1500 if (NULLPTR_TYPE_P (from) && (flags & LOOKUP_ONLYCONVERTING))
1501 conv->bad_p = true;
1502 if (flags & LOOKUP_NO_NARROWING)
1503 conv->check_narrowing = true;
1504 return conv;
1505 }
1506
1507 return NULL;
1508 }
1509 /* We don't check for ENUMERAL_TYPE here because there are no standard
1510 conversions to enum type. */
1511 /* As an extension, allow conversion to complex type. */
1512 else if (ARITHMETIC_TYPE_P (to))
1513 {
1514 if (! (INTEGRAL_CODE_P (fcode)
1515 || (fcode == REAL_TYPE && !(flags & LOOKUP_NO_NON_INTEGRAL)))
1516 || SCOPED_ENUM_P (from))
1517 return NULL;
1518
1519 /* If we're parsing an enum with no fixed underlying type, we're
1520 dealing with an incomplete type, which renders the conversion
1521 ill-formed. */
1522 if (!COMPLETE_TYPE_P (from))
1523 return NULL;
1524
1525 conv = build_conv (ck_std, to, conv);
1526
1527 tree underlying_type = NULL_TREE;
1528 if (TREE_CODE (from) == ENUMERAL_TYPE
1529 && ENUM_FIXED_UNDERLYING_TYPE_P (from))
1530 underlying_type = ENUM_UNDERLYING_TYPE (from);
1531
1532 /* Give this a better rank if it's a promotion.
1533
1534 To handle CWG 1601, also bump the rank if we are converting
1535 an enumeration with a fixed underlying type to the underlying
1536 type. */
1537 if ((same_type_p (to, type_promotes_to (from))
1538 || (underlying_type && same_type_p (to, underlying_type)))
1539 && next_conversion (conv)->rank <= cr_promotion)
1540 conv->rank = cr_promotion;
1541
1542 /* A prvalue of floating-point type can be converted to a prvalue of
1543 another floating-point type with a greater or equal conversion
1544 rank ([conv.rank]). A prvalue of standard floating-point type can
1545 be converted to a prvalue of another standard floating-point type.
1546 For backwards compatibility with handling __float128 and other
1547 non-standard floating point types, allow all implicit floating
1548 point conversions if neither type is extended floating-point
1549 type and if at least one of them is, fail if they have unordered
1550 conversion rank or from has higher conversion rank. */
1551 if (fcode == REAL_TYPE
1552 && tcode == REAL_TYPE
1553 && (extended_float_type_p (from)
1554 || extended_float_type_p (to))
1555 && cp_compare_floating_point_conversion_ranks (from, to) >= 2)
1556 conv->bad_p = true;
1557 }
1558 else if (fcode == VECTOR_TYPE && tcode == VECTOR_TYPE
1559 && vector_types_convertible_p (from, to, false))
1560 return build_conv (ck_std, to, conv);
1561 else if (MAYBE_CLASS_TYPE_P (to) && MAYBE_CLASS_TYPE_P (from)
1562 && is_properly_derived_from (from, to))
1563 {
1564 if (conv->kind == ck_rvalue)
1565 conv = next_conversion (conv);
1566 conv = build_conv (ck_base, to, conv);
1567 /* The derived-to-base conversion indicates the initialization
1568 of a parameter with base type from an object of a derived
1569 type. A temporary object is created to hold the result of
1570 the conversion unless we're binding directly to a reference. */
1571 conv->need_temporary_p = !(flags & LOOKUP_NO_TEMP_BIND);
1572 /* If we're performing copy-initialization, remember to skip
1573 explicit constructors. */
1574 if (flags & LOOKUP_ONLYCONVERTING)
1575 conv->copy_init_p = true;
1576 }
1577 else
1578 return NULL;
1579
1580 if (flags & LOOKUP_NO_NARROWING)
1581 conv->check_narrowing = true;
1582
1583 return conv;
1584 }
1585
1586 /* Returns nonzero if T1 is reference-related to T2. */
1587
1588 bool
1589 reference_related_p (tree t1, tree t2)
1590 {
1591 if (t1 == error_mark_node || t2 == error_mark_node)
1592 return false;
1593
1594 t1 = TYPE_MAIN_VARIANT (t1);
1595 t2 = TYPE_MAIN_VARIANT (t2);
1596
1597 /* [dcl.init.ref]
1598
1599 Given types "cv1 T1" and "cv2 T2," "cv1 T1" is reference-related
1600 to "cv2 T2" if T1 is similar to T2, or T1 is a base class of T2. */
1601 return (similar_type_p (t1, t2)
1602 || (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
1603 && DERIVED_FROM_P (t1, t2)));
1604 }
1605
1606 /* Returns nonzero if T1 is reference-compatible with T2. */
1607
1608 bool
1609 reference_compatible_p (tree t1, tree t2)
1610 {
1611 /* [dcl.init.ref]
1612
1613 "cv1 T1" is reference compatible with "cv2 T2" if
1614 a prvalue of type "pointer to cv2 T2" can be converted to the type
1615 "pointer to cv1 T1" via a standard conversion sequence. */
1616 tree ptype1 = build_pointer_type (t1);
1617 tree ptype2 = build_pointer_type (t2);
1618 conversion *conv = standard_conversion (ptype1, ptype2, NULL_TREE,
1619 /*c_cast_p=*/false, 0, tf_none);
1620 if (!conv || conv->bad_p)
1621 return false;
1622 return true;
1623 }
1624
1625 /* Return true if converting FROM to TO would involve a qualification
1626 conversion. */
1627
1628 static bool
1629 involves_qualification_conversion_p (tree to, tree from)
1630 {
1631 /* If we're not convering a pointer to another one, we won't get
1632 a qualification conversion. */
1633 if (!((TYPE_PTR_P (to) && TYPE_PTR_P (from))
1634 || (TYPE_PTRDATAMEM_P (to) && TYPE_PTRDATAMEM_P (from))))
1635 return false;
1636
1637 conversion *conv = standard_conversion (to, from, NULL_TREE,
1638 /*c_cast_p=*/false, 0, tf_none);
1639 for (conversion *t = conv; t; t = next_conversion (t))
1640 if (t->kind == ck_qual)
1641 return true;
1642
1643 return false;
1644 }
1645
1646 /* A reference of the indicated TYPE is being bound directly to the
1647 expression represented by the implicit conversion sequence CONV.
1648 Return a conversion sequence for this binding. */
1649
1650 static conversion *
1651 direct_reference_binding (tree type, conversion *conv)
1652 {
1653 tree t;
1654
1655 gcc_assert (TYPE_REF_P (type));
1656 gcc_assert (!TYPE_REF_P (conv->type));
1657
1658 t = TREE_TYPE (type);
1659
1660 if (conv->kind == ck_identity)
1661 /* Mark the identity conv as to not decay to rvalue. */
1662 conv->rvaluedness_matches_p = true;
1663
1664 /* [over.ics.rank]
1665
1666 When a parameter of reference type binds directly
1667 (_dcl.init.ref_) to an argument expression, the implicit
1668 conversion sequence is the identity conversion, unless the
1669 argument expression has a type that is a derived class of the
1670 parameter type, in which case the implicit conversion sequence is
1671 a derived-to-base Conversion.
1672
1673 If the parameter binds directly to the result of applying a
1674 conversion function to the argument expression, the implicit
1675 conversion sequence is a user-defined conversion sequence
1676 (_over.ics.user_), with the second standard conversion sequence
1677 either an identity conversion or, if the conversion function
1678 returns an entity of a type that is a derived class of the
1679 parameter type, a derived-to-base conversion. */
1680 if (is_properly_derived_from (conv->type, t))
1681 {
1682 /* Represent the derived-to-base conversion. */
1683 conv = build_conv (ck_base, t, conv);
1684 /* We will actually be binding to the base-class subobject in
1685 the derived class, so we mark this conversion appropriately.
1686 That way, convert_like knows not to generate a temporary. */
1687 conv->need_temporary_p = false;
1688 }
1689 else if (involves_qualification_conversion_p (t, conv->type))
1690 /* Represent the qualification conversion. After DR 2352
1691 #1 and #2 were indistinguishable conversion sequences:
1692
1693 void f(int*); // #1
1694 void f(const int* const &); // #2
1695 void g(int* p) { f(p); }
1696
1697 because the types "int *" and "const int *const" are
1698 reference-related and we were binding both directly and they
1699 had the same rank. To break it up, we add a ck_qual under the
1700 ck_ref_bind so that conversion sequence ranking chooses #1.
1701
1702 We strip_top_quals here which is also what standard_conversion
1703 does. Failure to do so would confuse comp_cv_qual_signature
1704 into thinking that in
1705
1706 void f(const int * const &); // #1
1707 void f(const int *); // #2
1708 int *x;
1709 f(x);
1710
1711 #2 is a better match than #1 even though they're ambiguous (97296). */
1712 conv = build_conv (ck_qual, strip_top_quals (t), conv);
1713
1714 return build_conv (ck_ref_bind, type, conv);
1715 }
1716
1717 /* Returns the conversion path from type FROM to reference type TO for
1718 purposes of reference binding. For lvalue binding, either pass a
1719 reference type to FROM or an lvalue expression to EXPR. If the
1720 reference will be bound to a temporary, NEED_TEMPORARY_P is set for
1721 the conversion returned. If C_CAST_P is true, this
1722 conversion is coming from a C-style cast. */
1723
1724 static conversion *
1725 reference_binding (tree rto, tree rfrom, tree expr, bool c_cast_p, int flags,
1726 tsubst_flags_t complain)
1727 {
1728 conversion *conv = NULL;
1729 tree to = TREE_TYPE (rto);
1730 tree from = rfrom;
1731 tree tfrom;
1732 bool related_p;
1733 bool compatible_p;
1734 cp_lvalue_kind gl_kind;
1735 bool is_lvalue;
1736
1737 if (TREE_CODE (to) == FUNCTION_TYPE && expr && type_unknown_p (expr))
1738 {
1739 expr = instantiate_type (to, expr, tf_none);
1740 if (expr == error_mark_node)
1741 return NULL;
1742 from = TREE_TYPE (expr);
1743 }
1744
1745 bool copy_list_init = false;
1746 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
1747 {
1748 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
1749 /* DR 1288: Otherwise, if the initializer list has a single element
1750 of type E and ... [T's] referenced type is reference-related to E,
1751 the object or reference is initialized from that element...
1752
1753 ??? With P0388R4, we should bind 't' directly to U{}:
1754 using U = A[2];
1755 A (&&t)[] = {U{}};
1756 because A[] and A[2] are reference-related. But we don't do it
1757 because grok_reference_init has deduced the array size (to 1), and
1758 A[1] and A[2] aren't reference-related. */
1759 if (CONSTRUCTOR_NELTS (expr) == 1
1760 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
1761 {
1762 tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
1763 if (error_operand_p (elt))
1764 return NULL;
1765 tree etype = TREE_TYPE (elt);
1766 if (reference_related_p (to, etype))
1767 {
1768 expr = elt;
1769 from = etype;
1770 goto skip;
1771 }
1772 }
1773 /* Otherwise, if T is a reference type, a prvalue temporary of the type
1774 referenced by T is copy-list-initialized, and the reference is bound
1775 to that temporary. */
1776 copy_list_init = true;
1777 skip:;
1778 }
1779
1780 if (TYPE_REF_P (from))
1781 {
1782 from = TREE_TYPE (from);
1783 if (!TYPE_REF_IS_RVALUE (rfrom)
1784 || TREE_CODE (from) == FUNCTION_TYPE)
1785 gl_kind = clk_ordinary;
1786 else
1787 gl_kind = clk_rvalueref;
1788 }
1789 else if (expr)
1790 gl_kind = lvalue_kind (expr);
1791 else if (CLASS_TYPE_P (from)
1792 || TREE_CODE (from) == ARRAY_TYPE)
1793 gl_kind = clk_class;
1794 else
1795 gl_kind = clk_none;
1796
1797 /* Don't allow a class prvalue when LOOKUP_NO_TEMP_BIND. */
1798 if ((flags & LOOKUP_NO_TEMP_BIND)
1799 && (gl_kind & clk_class))
1800 gl_kind = clk_none;
1801
1802 /* Same mask as real_lvalue_p. */
1803 is_lvalue = gl_kind && !(gl_kind & (clk_rvalueref|clk_class));
1804
1805 tfrom = from;
1806 if ((gl_kind & clk_bitfield) != 0)
1807 tfrom = unlowered_expr_type (expr);
1808
1809 /* Figure out whether or not the types are reference-related and
1810 reference compatible. We have to do this after stripping
1811 references from FROM. */
1812 related_p = reference_related_p (to, tfrom);
1813 /* If this is a C cast, first convert to an appropriately qualified
1814 type, so that we can later do a const_cast to the desired type. */
1815 if (related_p && c_cast_p
1816 && !at_least_as_qualified_p (to, tfrom))
1817 to = cp_build_qualified_type (to, cp_type_quals (tfrom));
1818 compatible_p = reference_compatible_p (to, tfrom);
1819
1820 /* Directly bind reference when target expression's type is compatible with
1821 the reference and expression is an lvalue. In DR391, the wording in
1822 [8.5.3/5 dcl.init.ref] is changed to also require direct bindings for
1823 const and rvalue references to rvalues of compatible class type.
1824 We should also do direct bindings for non-class xvalues. */
1825 if ((related_p || compatible_p) && gl_kind)
1826 {
1827 /* [dcl.init.ref]
1828
1829 If the initializer expression
1830
1831 -- is an lvalue (but not an lvalue for a bit-field), and "cv1 T1"
1832 is reference-compatible with "cv2 T2,"
1833
1834 the reference is bound directly to the initializer expression
1835 lvalue.
1836
1837 [...]
1838 If the initializer expression is an rvalue, with T2 a class type,
1839 and "cv1 T1" is reference-compatible with "cv2 T2", the reference
1840 is bound to the object represented by the rvalue or to a sub-object
1841 within that object. */
1842
1843 conv = build_identity_conv (tfrom, expr);
1844 conv = direct_reference_binding (rto, conv);
1845
1846 if (TYPE_REF_P (rfrom))
1847 /* Handle rvalue reference to function properly. */
1848 conv->rvaluedness_matches_p
1849 = (TYPE_REF_IS_RVALUE (rto) == TYPE_REF_IS_RVALUE (rfrom));
1850 else
1851 conv->rvaluedness_matches_p
1852 = (TYPE_REF_IS_RVALUE (rto) == !is_lvalue);
1853
1854 if ((gl_kind & clk_bitfield) != 0
1855 || ((gl_kind & clk_packed) != 0 && !TYPE_PACKED (to)))
1856 /* For the purposes of overload resolution, we ignore the fact
1857 this expression is a bitfield or packed field. (In particular,
1858 [over.ics.ref] says specifically that a function with a
1859 non-const reference parameter is viable even if the
1860 argument is a bitfield.)
1861
1862 However, when we actually call the function we must create
1863 a temporary to which to bind the reference. If the
1864 reference is volatile, or isn't const, then we cannot make
1865 a temporary, so we just issue an error when the conversion
1866 actually occurs. */
1867 conv->need_temporary_p = true;
1868
1869 /* Don't allow binding of lvalues (other than function lvalues) to
1870 rvalue references. */
1871 if (is_lvalue && TYPE_REF_IS_RVALUE (rto)
1872 && TREE_CODE (to) != FUNCTION_TYPE)
1873 conv->bad_p = true;
1874
1875 /* Nor the reverse. */
1876 if (!is_lvalue && !TYPE_REF_IS_RVALUE (rto)
1877 /* Unless it's really a C++20 lvalue being treated as an xvalue.
1878 But in C++23, such an expression is just an xvalue, not a special
1879 lvalue, so the binding is once again ill-formed. */
1880 && !(cxx_dialect <= cxx20
1881 && (gl_kind & clk_implicit_rval))
1882 && (!CP_TYPE_CONST_NON_VOLATILE_P (to)
1883 || (flags & LOOKUP_NO_RVAL_BIND))
1884 && TREE_CODE (to) != FUNCTION_TYPE)
1885 conv->bad_p = true;
1886
1887 if (!compatible_p)
1888 conv->bad_p = true;
1889
1890 return conv;
1891 }
1892 /* [class.conv.fct] A conversion function is never used to convert a
1893 (possibly cv-qualified) object to the (possibly cv-qualified) same
1894 object type (or a reference to it), to a (possibly cv-qualified) base
1895 class of that type (or a reference to it).... */
1896 else if (CLASS_TYPE_P (from) && !related_p
1897 && !(flags & LOOKUP_NO_CONVERSION))
1898 {
1899 /* [dcl.init.ref]
1900
1901 If the initializer expression
1902
1903 -- has a class type (i.e., T2 is a class type) can be
1904 implicitly converted to an lvalue of type "cv3 T3," where
1905 "cv1 T1" is reference-compatible with "cv3 T3". (this
1906 conversion is selected by enumerating the applicable
1907 conversion functions (_over.match.ref_) and choosing the
1908 best one through overload resolution. (_over.match_).
1909
1910 the reference is bound to the lvalue result of the conversion
1911 in the second case. */
1912 z_candidate *cand = build_user_type_conversion_1 (rto, expr, flags,
1913 complain);
1914 if (cand)
1915 return cand->second_conv;
1916 }
1917
1918 /* From this point on, we conceptually need temporaries, even if we
1919 elide them. Only the cases above are "direct bindings". */
1920 if (flags & LOOKUP_NO_TEMP_BIND)
1921 return NULL;
1922
1923 /* [over.ics.rank]
1924
1925 When a parameter of reference type is not bound directly to an
1926 argument expression, the conversion sequence is the one required
1927 to convert the argument expression to the underlying type of the
1928 reference according to _over.best.ics_. Conceptually, this
1929 conversion sequence corresponds to copy-initializing a temporary
1930 of the underlying type with the argument expression. Any
1931 difference in top-level cv-qualification is subsumed by the
1932 initialization itself and does not constitute a conversion. */
1933
1934 bool maybe_valid_p = true;
1935
1936 /* [dcl.init.ref]
1937
1938 Otherwise, the reference shall be an lvalue reference to a
1939 non-volatile const type, or the reference shall be an rvalue
1940 reference. */
1941 if (!CP_TYPE_CONST_NON_VOLATILE_P (to) && !TYPE_REF_IS_RVALUE (rto))
1942 maybe_valid_p = false;
1943
1944 /* [dcl.init.ref]
1945
1946 Otherwise, a temporary of type "cv1 T1" is created and
1947 initialized from the initializer expression using the rules for a
1948 non-reference copy initialization. If T1 is reference-related to
1949 T2, cv1 must be the same cv-qualification as, or greater
1950 cv-qualification than, cv2; otherwise, the program is ill-formed. */
1951 if (related_p && !at_least_as_qualified_p (to, from))
1952 maybe_valid_p = false;
1953
1954 /* We try below to treat an invalid reference binding as a bad conversion
1955 to improve diagnostics, but doing so may cause otherwise unnecessary
1956 instantiations that can lead to a hard error. So during the first pass
1957 of overload resolution wherein we shortcut bad conversions, instead just
1958 produce a special conversion indicating a second pass is necessary if
1959 there's no strictly viable candidate. */
1960 if (!maybe_valid_p && (flags & LOOKUP_SHORTCUT_BAD_CONVS))
1961 {
1962 conv = alloc_conversion (ck_deferred_bad);
1963 conv->bad_p = true;
1964 return conv;
1965 }
1966
1967 /* We're generating a temporary now, but don't bind any more in the
1968 conversion (specifically, don't slice the temporary returned by a
1969 conversion operator). */
1970 flags |= LOOKUP_NO_TEMP_BIND;
1971
1972 /* Core issue 899: When [copy-]initializing a temporary to be bound
1973 to the first parameter of a copy constructor (12.8) called with
1974 a single argument in the context of direct-initialization,
1975 explicit conversion functions are also considered.
1976
1977 So don't set LOOKUP_ONLYCONVERTING in that case. */
1978 if (!(flags & LOOKUP_COPY_PARM))
1979 flags |= LOOKUP_ONLYCONVERTING;
1980
1981 if (!conv)
1982 conv = implicit_conversion (to, from, expr, c_cast_p,
1983 flags, complain);
1984 if (!conv)
1985 return NULL;
1986
1987 if (conv->user_conv_p)
1988 {
1989 if (copy_list_init)
1990 /* Remember this was copy-list-initialization. */
1991 conv->need_temporary_p = true;
1992
1993 /* If initializing the temporary used a conversion function,
1994 recalculate the second conversion sequence. */
1995 for (conversion *t = conv; t; t = next_conversion (t))
1996 if (t->kind == ck_user
1997 && DECL_CONV_FN_P (t->cand->fn))
1998 {
1999 tree ftype = TREE_TYPE (TREE_TYPE (t->cand->fn));
2000 /* A prvalue of non-class type is cv-unqualified. */
2001 if (!TYPE_REF_P (ftype) && !CLASS_TYPE_P (ftype))
2002 ftype = cv_unqualified (ftype);
2003 int sflags = (flags|LOOKUP_NO_CONVERSION)&~LOOKUP_NO_TEMP_BIND;
2004 conversion *new_second
2005 = reference_binding (rto, ftype, NULL_TREE, c_cast_p,
2006 sflags, complain);
2007 if (!new_second)
2008 return NULL;
2009 conv = merge_conversion_sequences (t, new_second);
2010 gcc_assert (maybe_valid_p || conv->bad_p);
2011 return conv;
2012 }
2013 }
2014
2015 conv = build_conv (ck_ref_bind, rto, conv);
2016 /* This reference binding, unlike those above, requires the
2017 creation of a temporary. */
2018 conv->need_temporary_p = true;
2019 conv->rvaluedness_matches_p = TYPE_REF_IS_RVALUE (rto);
2020 conv->bad_p |= !maybe_valid_p;
2021
2022 return conv;
2023 }
2024
2025 /* Most of the implementation of implicit_conversion, with the same
2026 parameters. */
2027
2028 static conversion *
2029 implicit_conversion_1 (tree to, tree from, tree expr, bool c_cast_p,
2030 int flags, tsubst_flags_t complain)
2031 {
2032 conversion *conv;
2033
2034 if (from == error_mark_node || to == error_mark_node
2035 || expr == error_mark_node)
2036 return NULL;
2037
2038 /* Other flags only apply to the primary function in overload
2039 resolution, or after we've chosen one. */
2040 flags &= (LOOKUP_ONLYCONVERTING|LOOKUP_NO_CONVERSION|LOOKUP_COPY_PARM
2041 |LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND|LOOKUP_NO_NARROWING
2042 |LOOKUP_PROTECT|LOOKUP_NO_NON_INTEGRAL|LOOKUP_SHORTCUT_BAD_CONVS);
2043
2044 /* FIXME: actually we don't want warnings either, but we can't just
2045 have 'complain &= ~(tf_warning|tf_error)' because it would cause
2046 the regression of, eg, g++.old-deja/g++.benjamin/16077.C.
2047 We really ought not to issue that warning until we've committed
2048 to that conversion. */
2049 complain &= ~tf_error;
2050
2051 /* Call reshape_init early to remove redundant braces. */
2052 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr)
2053 && CLASS_TYPE_P (to)
2054 && COMPLETE_TYPE_P (complete_type (to))
2055 && !CLASSTYPE_NON_AGGREGATE (to))
2056 {
2057 expr = reshape_init (to, expr, complain);
2058 if (expr == error_mark_node)
2059 return NULL;
2060 from = TREE_TYPE (expr);
2061 }
2062
2063 if (TYPE_REF_P (to))
2064 conv = reference_binding (to, from, expr, c_cast_p, flags, complain);
2065 else
2066 conv = standard_conversion (to, from, expr, c_cast_p, flags, complain);
2067
2068 if (conv)
2069 return conv;
2070
2071 if (expr && BRACE_ENCLOSED_INITIALIZER_P (expr))
2072 {
2073 if (is_std_init_list (to) && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2074 return build_list_conv (to, expr, flags, complain);
2075
2076 /* As an extension, allow list-initialization of _Complex. */
2077 if (TREE_CODE (to) == COMPLEX_TYPE
2078 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2079 {
2080 conv = build_complex_conv (to, expr, flags, complain);
2081 if (conv)
2082 return conv;
2083 }
2084
2085 /* Allow conversion from an initializer-list with one element to a
2086 scalar type. */
2087 if (SCALAR_TYPE_P (to))
2088 {
2089 int nelts = CONSTRUCTOR_NELTS (expr);
2090 tree elt;
2091
2092 if (nelts == 0)
2093 elt = build_value_init (to, tf_none);
2094 else if (nelts == 1 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr))
2095 elt = CONSTRUCTOR_ELT (expr, 0)->value;
2096 else
2097 elt = error_mark_node;
2098
2099 conv = implicit_conversion (to, TREE_TYPE (elt), elt,
2100 c_cast_p, flags, complain);
2101 if (conv)
2102 {
2103 conv->check_narrowing = true;
2104 if (BRACE_ENCLOSED_INITIALIZER_P (elt))
2105 /* Too many levels of braces, i.e. '{{1}}'. */
2106 conv->bad_p = true;
2107 return conv;
2108 }
2109 }
2110 else if (TREE_CODE (to) == ARRAY_TYPE)
2111 return build_array_conv (to, expr, flags, complain);
2112 }
2113
2114 if (expr != NULL_TREE
2115 && (MAYBE_CLASS_TYPE_P (from)
2116 || MAYBE_CLASS_TYPE_P (to))
2117 && (flags & LOOKUP_NO_CONVERSION) == 0)
2118 {
2119 struct z_candidate *cand;
2120
2121 if (CLASS_TYPE_P (to)
2122 && BRACE_ENCLOSED_INITIALIZER_P (expr)
2123 && !CLASSTYPE_NON_AGGREGATE (complete_type (to)))
2124 return build_aggr_conv (to, expr, flags, complain);
2125
2126 cand = build_user_type_conversion_1 (to, expr, flags, complain);
2127 if (cand)
2128 {
2129 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
2130 && CONSTRUCTOR_NELTS (expr) == 1
2131 && !CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
2132 && !is_list_ctor (cand->fn))
2133 {
2134 /* "If C is not an initializer-list constructor and the
2135 initializer list has a single element of type cv U, where U is
2136 X or a class derived from X, the implicit conversion sequence
2137 has Exact Match rank if U is X, or Conversion rank if U is
2138 derived from X." */
2139 tree elt = CONSTRUCTOR_ELT (expr, 0)->value;
2140 tree elttype = TREE_TYPE (elt);
2141 if (reference_related_p (to, elttype))
2142 return implicit_conversion (to, elttype, elt,
2143 c_cast_p, flags, complain);
2144 }
2145 conv = cand->second_conv;
2146 }
2147
2148 /* We used to try to bind a reference to a temporary here, but that
2149 is now handled after the recursive call to this function at the end
2150 of reference_binding. */
2151 return conv;
2152 }
2153
2154 return NULL;
2155 }
2156
2157 /* Returns the implicit conversion sequence (see [over.ics]) from type
2158 FROM to type TO. The optional expression EXPR may affect the
2159 conversion. FLAGS are the usual overloading flags. If C_CAST_P is
2160 true, this conversion is coming from a C-style cast. */
2161
2162 static conversion *
2163 implicit_conversion (tree to, tree from, tree expr, bool c_cast_p,
2164 int flags, tsubst_flags_t complain)
2165 {
2166 conversion *conv = implicit_conversion_1 (to, from, expr, c_cast_p,
2167 flags, complain);
2168 if (!conv || conv->bad_p)
2169 return conv;
2170 if (conv_is_prvalue (conv)
2171 && CLASS_TYPE_P (conv->type)
2172 && CLASSTYPE_PURE_VIRTUALS (conv->type))
2173 conv->bad_p = true;
2174 return conv;
2175 }
2176
2177 /* Like implicit_conversion, but return NULL if the conversion is bad.
2178
2179 This is not static so that check_non_deducible_conversion can call it within
2180 add_template_candidate_real as part of overload resolution; it should not be
2181 called outside of overload resolution. */
2182
2183 conversion *
2184 good_conversion (tree to, tree from, tree expr,
2185 int flags, tsubst_flags_t complain)
2186 {
2187 conversion *c = implicit_conversion (to, from, expr, /*cast*/false,
2188 flags, complain);
2189 if (c && c->bad_p)
2190 c = NULL;
2191 return c;
2192 }
2193
2194 /* Add a new entry to the list of candidates. Used by the add_*_candidate
2195 functions. ARGS will not be changed until a single candidate is
2196 selected. */
2197
2198 static struct z_candidate *
2199 add_candidate (struct z_candidate **candidates,
2200 tree fn, tree first_arg, const vec<tree, va_gc> *args,
2201 size_t num_convs, conversion **convs,
2202 tree access_path, tree conversion_path,
2203 int viable, struct rejection_reason *reason,
2204 int flags)
2205 {
2206 struct z_candidate *cand = (struct z_candidate *)
2207 conversion_obstack_alloc (sizeof (struct z_candidate));
2208
2209 cand->fn = fn;
2210 cand->first_arg = first_arg;
2211 cand->args = args;
2212 cand->convs = convs;
2213 cand->num_convs = num_convs;
2214 cand->access_path = access_path;
2215 cand->conversion_path = conversion_path;
2216 cand->viable = viable;
2217 cand->reason = reason;
2218 cand->next = *candidates;
2219 cand->flags = flags;
2220 *candidates = cand;
2221
2222 if (convs && cand->reversed ())
2223 /* Swap the conversions for comparison in joust; we'll swap them back
2224 before build_over_call. */
2225 std::swap (convs[0], convs[1]);
2226
2227 return cand;
2228 }
2229
2230 /* Return the number of remaining arguments in the parameter list
2231 beginning with ARG. */
2232
2233 int
2234 remaining_arguments (tree arg)
2235 {
2236 int n;
2237
2238 for (n = 0; arg != NULL_TREE && arg != void_list_node;
2239 arg = TREE_CHAIN (arg))
2240 n++;
2241
2242 return n;
2243 }
2244
2245 /* [over.match.copy]: When initializing a temporary object (12.2) to be bound
2246 to the first parameter of a constructor where the parameter is of type
2247 "reference to possibly cv-qualified T" and the constructor is called with a
2248 single argument in the context of direct-initialization of an object of type
2249 "cv2 T", explicit conversion functions are also considered.
2250
2251 So set LOOKUP_COPY_PARM to let reference_binding know that
2252 it's being called in that context. */
2253
2254 int
2255 conv_flags (int i, int nargs, tree fn, tree arg, int flags)
2256 {
2257 int lflags = flags;
2258 tree t;
2259 if (i == 0 && nargs == 1 && DECL_CONSTRUCTOR_P (fn)
2260 && (t = FUNCTION_FIRST_USER_PARMTYPE (fn))
2261 && (same_type_ignoring_top_level_qualifiers_p
2262 (non_reference (TREE_VALUE (t)), DECL_CONTEXT (fn))))
2263 {
2264 if (!(flags & LOOKUP_ONLYCONVERTING))
2265 lflags |= LOOKUP_COPY_PARM;
2266 if ((flags & LOOKUP_LIST_INIT_CTOR)
2267 && BRACE_ENCLOSED_INITIALIZER_P (arg))
2268 lflags |= LOOKUP_NO_CONVERSION;
2269 }
2270 else
2271 lflags |= LOOKUP_ONLYCONVERTING;
2272
2273 return lflags;
2274 }
2275
2276 /* Build an appropriate 'this' conversion for the method FN and class
2277 type CTYPE from the value ARG (having type ARGTYPE) to the type PARMTYPE.
2278 This function modifies PARMTYPE, ARGTYPE and ARG. */
2279
2280 static conversion *
2281 build_this_conversion (tree fn, tree ctype,
2282 tree& parmtype, tree& argtype, tree& arg,
2283 int flags, tsubst_flags_t complain)
2284 {
2285 gcc_assert (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2286 && !DECL_CONSTRUCTOR_P (fn));
2287
2288 /* The type of the implicit object parameter ('this') for
2289 overload resolution is not always the same as for the
2290 function itself; conversion functions are considered to
2291 be members of the class being converted, and functions
2292 introduced by a using-declaration are considered to be
2293 members of the class that uses them.
2294
2295 Since build_over_call ignores the ICS for the `this'
2296 parameter, we can just change the parm type. */
2297 parmtype = cp_build_qualified_type (ctype,
2298 cp_type_quals (TREE_TYPE (parmtype)));
2299 bool this_p = true;
2300 if (FUNCTION_REF_QUALIFIED (TREE_TYPE (fn)))
2301 {
2302 /* If the function has a ref-qualifier, the implicit
2303 object parameter has reference type. */
2304 bool rv = FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn));
2305 parmtype = cp_build_reference_type (parmtype, rv);
2306 /* The special handling of 'this' conversions in compare_ics
2307 does not apply if there is a ref-qualifier. */
2308 this_p = false;
2309 }
2310 else
2311 {
2312 parmtype = build_pointer_type (parmtype);
2313 /* We don't use build_this here because we don't want to
2314 capture the object argument until we've chosen a
2315 non-static member function. */
2316 arg = build_address (arg);
2317 argtype = lvalue_type (arg);
2318 }
2319 flags |= LOOKUP_ONLYCONVERTING;
2320 conversion *t = implicit_conversion (parmtype, argtype, arg,
2321 /*c_cast_p=*/false, flags, complain);
2322 t->this_p = this_p;
2323 return t;
2324 }
2325
2326 /* Create an overload candidate for the function or method FN called
2327 with the argument list FIRST_ARG/ARGS and add it to CANDIDATES.
2328 FLAGS is passed on to implicit_conversion.
2329
2330 This does not change ARGS.
2331
2332 CTYPE, if non-NULL, is the type we want to pretend this function
2333 comes from for purposes of overload resolution.
2334
2335 SHORTCUT_BAD_CONVS controls how we handle "bad" argument conversions.
2336 If true, we stop computing conversions upon seeing the first bad
2337 conversion. This is used by add_candidates to avoid computing
2338 more conversions than necessary in the presence of a strictly viable
2339 candidate, while preserving the defacto behavior of overload resolution
2340 when it turns out there are only non-strictly viable candidates. */
2341
2342 static struct z_candidate *
2343 add_function_candidate (struct z_candidate **candidates,
2344 tree fn, tree ctype, tree first_arg,
2345 const vec<tree, va_gc> *args, tree access_path,
2346 tree conversion_path, int flags,
2347 conversion **convs,
2348 bool shortcut_bad_convs,
2349 tsubst_flags_t complain)
2350 {
2351 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
2352 int i, len;
2353 tree parmnode;
2354 tree orig_first_arg = first_arg;
2355 int skip;
2356 int viable = 1;
2357 struct rejection_reason *reason = NULL;
2358
2359 /* The `this', `in_chrg' and VTT arguments to constructors are not
2360 considered in overload resolution. */
2361 if (DECL_CONSTRUCTOR_P (fn))
2362 {
2363 if (ctor_omit_inherited_parms (fn))
2364 /* Bring back parameters omitted from an inherited ctor. */
2365 parmlist = FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn));
2366 else
2367 parmlist = skip_artificial_parms_for (fn, parmlist);
2368 skip = num_artificial_parms_for (fn);
2369 if (skip > 0 && first_arg != NULL_TREE)
2370 {
2371 --skip;
2372 first_arg = NULL_TREE;
2373 }
2374 }
2375 else
2376 skip = 0;
2377
2378 len = vec_safe_length (args) - skip + (first_arg != NULL_TREE ? 1 : 0);
2379 if (!convs)
2380 convs = alloc_conversions (len);
2381
2382 /* 13.3.2 - Viable functions [over.match.viable]
2383 First, to be a viable function, a candidate function shall have enough
2384 parameters to agree in number with the arguments in the list.
2385
2386 We need to check this first; otherwise, checking the ICSes might cause
2387 us to produce an ill-formed template instantiation. */
2388
2389 parmnode = parmlist;
2390 for (i = 0; i < len; ++i)
2391 {
2392 if (parmnode == NULL_TREE || parmnode == void_list_node)
2393 break;
2394 parmnode = TREE_CHAIN (parmnode);
2395 }
2396
2397 if ((i < len && parmnode)
2398 || !sufficient_parms_p (parmnode))
2399 {
2400 int remaining = remaining_arguments (parmnode);
2401 viable = 0;
2402 reason = arity_rejection (first_arg, i + remaining, len);
2403 }
2404
2405 /* An inherited constructor (12.6.3 [class.inhctor.init]) that has a first
2406 parameter of type "reference to cv C" (including such a constructor
2407 instantiated from a template) is excluded from the set of candidate
2408 functions when used to construct an object of type D with an argument list
2409 containing a single argument if C is reference-related to D. */
2410 if (viable && len == 1 && parmlist && DECL_CONSTRUCTOR_P (fn)
2411 && flag_new_inheriting_ctors
2412 && DECL_INHERITED_CTOR (fn))
2413 {
2414 tree ptype = non_reference (TREE_VALUE (parmlist));
2415 tree dtype = DECL_CONTEXT (fn);
2416 tree btype = DECL_INHERITED_CTOR_BASE (fn);
2417 if (reference_related_p (ptype, dtype)
2418 && reference_related_p (btype, ptype))
2419 {
2420 viable = false;
2421 reason = inherited_ctor_rejection ();
2422 }
2423 }
2424
2425 /* Second, for a function to be viable, its constraints must be
2426 satisfied. */
2427 if (flag_concepts && viable && !constraints_satisfied_p (fn))
2428 {
2429 reason = constraint_failure ();
2430 viable = false;
2431 }
2432
2433 /* When looking for a function from a subobject from an implicit
2434 copy/move constructor/operator=, don't consider anything that takes (a
2435 reference to) an unrelated type. See c++/44909 and core 1092. */
2436 if (viable && parmlist && (flags & LOOKUP_DEFAULTED))
2437 {
2438 if (DECL_CONSTRUCTOR_P (fn))
2439 i = 1;
2440 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2441 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
2442 i = 2;
2443 else
2444 i = 0;
2445 if (i && len == i)
2446 {
2447 parmnode = chain_index (i-1, parmlist);
2448 if (!reference_related_p (non_reference (TREE_VALUE (parmnode)),
2449 ctype))
2450 viable = 0;
2451 }
2452
2453 /* This only applies at the top level. */
2454 flags &= ~LOOKUP_DEFAULTED;
2455 }
2456
2457 if (! viable)
2458 goto out;
2459
2460 if (shortcut_bad_convs)
2461 flags |= LOOKUP_SHORTCUT_BAD_CONVS;
2462 else
2463 flags &= ~LOOKUP_SHORTCUT_BAD_CONVS;
2464
2465 /* Third, for F to be a viable function, there shall exist for each
2466 argument an implicit conversion sequence that converts that argument
2467 to the corresponding parameter of F. */
2468
2469 parmnode = parmlist;
2470
2471 for (i = 0; i < len; ++i)
2472 {
2473 tree argtype, to_type;
2474 tree arg;
2475
2476 if (parmnode == void_list_node)
2477 break;
2478
2479 if (convs[i])
2480 {
2481 /* Already set during deduction. */
2482 parmnode = TREE_CHAIN (parmnode);
2483 continue;
2484 }
2485
2486 if (i == 0 && first_arg != NULL_TREE)
2487 arg = first_arg;
2488 else
2489 arg = CONST_CAST_TREE (
2490 (*args)[i + skip - (first_arg != NULL_TREE ? 1 : 0)]);
2491 argtype = lvalue_type (arg);
2492
2493 conversion *t;
2494 if (parmnode)
2495 {
2496 tree parmtype = TREE_VALUE (parmnode);
2497 if (i == 0
2498 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2499 && !DECL_CONSTRUCTOR_P (fn))
2500 t = build_this_conversion (fn, ctype, parmtype, argtype, arg,
2501 flags, complain);
2502 else
2503 {
2504 int lflags = conv_flags (i, len-skip, fn, arg, flags);
2505 t = implicit_conversion (parmtype, argtype, arg,
2506 /*c_cast_p=*/false, lflags, complain);
2507 }
2508 to_type = parmtype;
2509 parmnode = TREE_CHAIN (parmnode);
2510 }
2511 else
2512 {
2513 t = build_identity_conv (argtype, arg);
2514 t->ellipsis_p = true;
2515 to_type = argtype;
2516 }
2517
2518 convs[i] = t;
2519 if (! t)
2520 {
2521 viable = 0;
2522 reason = arg_conversion_rejection (first_arg, i, argtype, to_type,
2523 EXPR_LOCATION (arg));
2524 break;
2525 }
2526
2527 if (t->bad_p)
2528 {
2529 viable = -1;
2530 reason = bad_arg_conversion_rejection (first_arg, i, arg, to_type,
2531 EXPR_LOCATION (arg));
2532 if (shortcut_bad_convs)
2533 break;
2534 }
2535 }
2536
2537 out:
2538 return add_candidate (candidates, fn, orig_first_arg, args, len, convs,
2539 access_path, conversion_path, viable, reason, flags);
2540 }
2541
2542 /* Create an overload candidate for the conversion function FN which will
2543 be invoked for expression OBJ, producing a pointer-to-function which
2544 will in turn be called with the argument list FIRST_ARG/ARGLIST,
2545 and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
2546 passed on to implicit_conversion.
2547
2548 Actually, we don't really care about FN; we care about the type it
2549 converts to. There may be multiple conversion functions that will
2550 convert to that type, and we rely on build_user_type_conversion_1 to
2551 choose the best one; so when we create our candidate, we record the type
2552 instead of the function. */
2553
2554 static struct z_candidate *
2555 add_conv_candidate (struct z_candidate **candidates, tree fn, tree obj,
2556 const vec<tree, va_gc> *arglist,
2557 tree access_path, tree conversion_path,
2558 tsubst_flags_t complain)
2559 {
2560 tree totype = TREE_TYPE (TREE_TYPE (fn));
2561 int i, len, viable, flags;
2562 tree parmlist, parmnode;
2563 conversion **convs;
2564 struct rejection_reason *reason;
2565
2566 for (parmlist = totype; TREE_CODE (parmlist) != FUNCTION_TYPE; )
2567 parmlist = TREE_TYPE (parmlist);
2568 parmlist = TYPE_ARG_TYPES (parmlist);
2569
2570 len = vec_safe_length (arglist) + 1;
2571 convs = alloc_conversions (len);
2572 parmnode = parmlist;
2573 viable = 1;
2574 flags = LOOKUP_IMPLICIT;
2575 reason = NULL;
2576
2577 /* Don't bother looking up the same type twice. */
2578 if (*candidates && (*candidates)->fn == totype)
2579 return NULL;
2580
2581 for (i = 0; i < len; ++i)
2582 {
2583 tree arg, argtype, convert_type = NULL_TREE;
2584 conversion *t;
2585
2586 if (i == 0)
2587 arg = obj;
2588 else
2589 arg = (*arglist)[i - 1];
2590 argtype = lvalue_type (arg);
2591
2592 if (i == 0)
2593 {
2594 t = build_identity_conv (argtype, NULL_TREE);
2595 t = build_conv (ck_user, totype, t);
2596 /* Leave the 'cand' field null; we'll figure out the conversion in
2597 convert_like if this candidate is chosen. */
2598 convert_type = totype;
2599 }
2600 else if (parmnode == void_list_node)
2601 break;
2602 else if (parmnode)
2603 {
2604 t = implicit_conversion (TREE_VALUE (parmnode), argtype, arg,
2605 /*c_cast_p=*/false, flags, complain);
2606 convert_type = TREE_VALUE (parmnode);
2607 }
2608 else
2609 {
2610 t = build_identity_conv (argtype, arg);
2611 t->ellipsis_p = true;
2612 convert_type = argtype;
2613 }
2614
2615 convs[i] = t;
2616 if (! t)
2617 break;
2618
2619 if (t->bad_p)
2620 {
2621 viable = -1;
2622 reason = bad_arg_conversion_rejection (NULL_TREE, i, arg, convert_type,
2623 EXPR_LOCATION (arg));
2624 }
2625
2626 if (i == 0)
2627 continue;
2628
2629 if (parmnode)
2630 parmnode = TREE_CHAIN (parmnode);
2631 }
2632
2633 if (i < len
2634 || ! sufficient_parms_p (parmnode))
2635 {
2636 int remaining = remaining_arguments (parmnode);
2637 viable = 0;
2638 reason = arity_rejection (NULL_TREE, i + remaining, len);
2639 }
2640
2641 return add_candidate (candidates, totype, obj, arglist, len, convs,
2642 access_path, conversion_path, viable, reason, flags);
2643 }
2644
2645 static void
2646 build_builtin_candidate (struct z_candidate **candidates, tree fnname,
2647 tree type1, tree type2, const vec<tree,va_gc> &args,
2648 tree *argtypes, int flags, tsubst_flags_t complain)
2649 {
2650 conversion *t;
2651 conversion **convs;
2652 size_t num_convs;
2653 int viable = 1;
2654 tree types[2];
2655 struct rejection_reason *reason = NULL;
2656
2657 types[0] = type1;
2658 types[1] = type2;
2659
2660 num_convs = args.length ();
2661 convs = alloc_conversions (num_convs);
2662
2663 /* TRUTH_*_EXPR do "contextual conversion to bool", which means explicit
2664 conversion ops are allowed. We handle that here by just checking for
2665 boolean_type_node because other operators don't ask for it. COND_EXPR
2666 also does contextual conversion to bool for the first operand, but we
2667 handle that in build_conditional_expr, and type1 here is operand 2. */
2668 if (type1 != boolean_type_node)
2669 flags |= LOOKUP_ONLYCONVERTING;
2670
2671 for (unsigned i = 0; i < 2 && i < num_convs; ++i)
2672 {
2673 t = implicit_conversion (types[i], argtypes[i], args[i],
2674 /*c_cast_p=*/false, flags, complain);
2675 if (! t)
2676 {
2677 viable = 0;
2678 /* We need something for printing the candidate. */
2679 t = build_identity_conv (types[i], NULL_TREE);
2680 reason = arg_conversion_rejection (NULL_TREE, i, argtypes[i],
2681 types[i], EXPR_LOCATION (args[i]));
2682 }
2683 else if (t->bad_p)
2684 {
2685 viable = 0;
2686 reason = bad_arg_conversion_rejection (NULL_TREE, i, args[i],
2687 types[i],
2688 EXPR_LOCATION (args[i]));
2689 }
2690 convs[i] = t;
2691 }
2692
2693 /* For COND_EXPR we rearranged the arguments; undo that now. */
2694 if (num_convs == 3)
2695 {
2696 convs[2] = convs[1];
2697 convs[1] = convs[0];
2698 t = implicit_conversion (boolean_type_node, argtypes[2], args[2],
2699 /*c_cast_p=*/false, flags,
2700 complain);
2701 if (t)
2702 convs[0] = t;
2703 else
2704 {
2705 viable = 0;
2706 reason = arg_conversion_rejection (NULL_TREE, 0, argtypes[2],
2707 boolean_type_node,
2708 EXPR_LOCATION (args[2]));
2709 }
2710 }
2711
2712 add_candidate (candidates, fnname, /*first_arg=*/NULL_TREE, /*args=*/NULL,
2713 num_convs, convs,
2714 /*access_path=*/NULL_TREE,
2715 /*conversion_path=*/NULL_TREE,
2716 viable, reason, flags);
2717 }
2718
2719 static bool
2720 is_complete (tree t)
2721 {
2722 return COMPLETE_TYPE_P (complete_type (t));
2723 }
2724
2725 /* Returns nonzero if TYPE is a promoted arithmetic type. */
2726
2727 static bool
2728 promoted_arithmetic_type_p (tree type)
2729 {
2730 /* [over.built]
2731
2732 In this section, the term promoted integral type is used to refer
2733 to those integral types which are preserved by integral promotion
2734 (including e.g. int and long but excluding e.g. char).
2735 Similarly, the term promoted arithmetic type refers to promoted
2736 integral types plus floating types. */
2737 return ((CP_INTEGRAL_TYPE_P (type)
2738 && same_type_p (type_promotes_to (type), type))
2739 || TREE_CODE (type) == REAL_TYPE);
2740 }
2741
2742 /* Create any builtin operator overload candidates for the operator in
2743 question given the converted operand types TYPE1 and TYPE2. The other
2744 args are passed through from add_builtin_candidates to
2745 build_builtin_candidate.
2746
2747 TYPE1 and TYPE2 may not be permissible, and we must filter them.
2748 If CODE is requires candidates operands of the same type of the kind
2749 of which TYPE1 and TYPE2 are, we add both candidates
2750 CODE (TYPE1, TYPE1) and CODE (TYPE2, TYPE2). */
2751
2752 static void
2753 add_builtin_candidate (struct z_candidate **candidates, enum tree_code code,
2754 enum tree_code code2, tree fnname, tree type1,
2755 tree type2, vec<tree,va_gc> &args, tree *argtypes,
2756 int flags, tsubst_flags_t complain)
2757 {
2758 switch (code)
2759 {
2760 case POSTINCREMENT_EXPR:
2761 case POSTDECREMENT_EXPR:
2762 args[1] = integer_zero_node;
2763 type2 = integer_type_node;
2764 break;
2765 default:
2766 break;
2767 }
2768
2769 switch (code)
2770 {
2771
2772 /* 4 For every pair (T, VQ), where T is an arithmetic type other than bool,
2773 and VQ is either volatile or empty, there exist candidate operator
2774 functions of the form
2775 VQ T& operator++(VQ T&);
2776 T operator++(VQ T&, int);
2777 5 For every pair (T, VQ), where T is an arithmetic type other than bool,
2778 and VQ is either volatile or empty, there exist candidate operator
2779 functions of the form
2780 VQ T& operator--(VQ T&);
2781 T operator--(VQ T&, int);
2782 6 For every pair (T, VQ), where T is a cv-qualified or cv-unqualified object
2783 type, and VQ is either volatile or empty, there exist candidate operator
2784 functions of the form
2785 T*VQ& operator++(T*VQ&);
2786 T*VQ& operator--(T*VQ&);
2787 T* operator++(T*VQ&, int);
2788 T* operator--(T*VQ&, int); */
2789
2790 case POSTDECREMENT_EXPR:
2791 case PREDECREMENT_EXPR:
2792 if (TREE_CODE (type1) == BOOLEAN_TYPE)
2793 return;
2794 /* FALLTHRU */
2795 case POSTINCREMENT_EXPR:
2796 case PREINCREMENT_EXPR:
2797 /* P0002R1, Remove deprecated operator++(bool) added "other than bool"
2798 to p4. */
2799 if (TREE_CODE (type1) == BOOLEAN_TYPE && cxx_dialect >= cxx17)
2800 return;
2801 if (ARITHMETIC_TYPE_P (type1) || TYPE_PTROB_P (type1))
2802 {
2803 type1 = build_reference_type (type1);
2804 break;
2805 }
2806 return;
2807
2808 /* 7 For every cv-qualified or cv-unqualified object type T, there
2809 exist candidate operator functions of the form
2810
2811 T& operator*(T*);
2812
2813
2814 8 For every function type T that does not have cv-qualifiers or
2815 a ref-qualifier, there exist candidate operator functions of the form
2816 T& operator*(T*); */
2817
2818 case INDIRECT_REF:
2819 if (TYPE_PTR_P (type1)
2820 && (TYPE_PTROB_P (type1)
2821 || TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE))
2822 break;
2823 return;
2824
2825 /* 9 For every type T, there exist candidate operator functions of the form
2826 T* operator+(T*);
2827
2828 10 For every floating-point or promoted integral type T, there exist
2829 candidate operator functions of the form
2830 T operator+(T);
2831 T operator-(T); */
2832
2833 case UNARY_PLUS_EXPR: /* unary + */
2834 if (TYPE_PTR_P (type1))
2835 break;
2836 /* FALLTHRU */
2837 case NEGATE_EXPR:
2838 if (ARITHMETIC_TYPE_P (type1))
2839 break;
2840 return;
2841
2842 /* 11 For every promoted integral type T, there exist candidate operator
2843 functions of the form
2844 T operator~(T); */
2845
2846 case BIT_NOT_EXPR:
2847 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1))
2848 break;
2849 return;
2850
2851 /* 12 For every quintuple (C1, C2, T, CV1, CV2), where C2 is a class type, C1
2852 is the same type as C2 or is a derived class of C2, and T is an object
2853 type or a function type there exist candidate operator functions of the
2854 form
2855 CV12 T& operator->*(CV1 C1*, CV2 T C2::*);
2856 where CV12 is the union of CV1 and CV2. */
2857
2858 case MEMBER_REF:
2859 if (TYPE_PTR_P (type1) && TYPE_PTRMEM_P (type2))
2860 {
2861 tree c1 = TREE_TYPE (type1);
2862 tree c2 = TYPE_PTRMEM_CLASS_TYPE (type2);
2863
2864 if (CLASS_TYPE_P (c1) && DERIVED_FROM_P (c2, c1)
2865 && (TYPE_PTRMEMFUNC_P (type2)
2866 || is_complete (TYPE_PTRMEM_POINTED_TO_TYPE (type2))))
2867 break;
2868 }
2869 return;
2870
2871 /* 13 For every pair of types L and R, where each of L and R is a floating-point
2872 or promoted integral type, there exist candidate operator functions of the
2873 form
2874 LR operator*(L, R);
2875 LR operator/(L, R);
2876 LR operator+(L, R);
2877 LR operator-(L, R);
2878 bool operator<(L, R);
2879 bool operator>(L, R);
2880 bool operator<=(L, R);
2881 bool operator>=(L, R);
2882 bool operator==(L, R);
2883 bool operator!=(L, R);
2884 where LR is the result of the usual arithmetic conversions between
2885 types L and R.
2886
2887 14 For every integral type T there exists a candidate operator function of
2888 the form
2889
2890 std::strong_ordering operator<=>(T, T);
2891
2892 15 For every pair of floating-point types L and R, there exists a candidate
2893 operator function of the form
2894
2895 std::partial_ordering operator<=>(L, R);
2896
2897 16 For every cv-qualified or cv-unqualified object type T there exist
2898 candidate operator functions of the form
2899 T* operator+(T*, std::ptrdiff_t);
2900 T& operator[](T*, std::ptrdiff_t);
2901 T* operator-(T*, std::ptrdiff_t);
2902 T* operator+(std::ptrdiff_t, T*);
2903 T& operator[](std::ptrdiff_t, T*);
2904
2905 17 For every T, where T is a pointer to object type, there exist candidate
2906 operator functions of the form
2907 std::ptrdiff_t operator-(T, T);
2908
2909 18 For every T, where T is an enumeration type or a pointer type, there
2910 exist candidate operator functions of the form
2911 bool operator<(T, T);
2912 bool operator>(T, T);
2913 bool operator<=(T, T);
2914 bool operator>=(T, T);
2915 bool operator==(T, T);
2916 bool operator!=(T, T);
2917 R operator<=>(T, T);
2918
2919 where R is the result type specified in [expr.spaceship].
2920
2921 19 For every T, where T is a pointer-to-member type or std::nullptr_t,
2922 there exist candidate operator functions of the form
2923 bool operator==(T, T);
2924 bool operator!=(T, T); */
2925
2926 case MINUS_EXPR:
2927 if (TYPE_PTROB_P (type1) && TYPE_PTROB_P (type2))
2928 break;
2929 if (TYPE_PTROB_P (type1)
2930 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
2931 {
2932 type2 = ptrdiff_type_node;
2933 break;
2934 }
2935 /* FALLTHRU */
2936 case MULT_EXPR:
2937 case TRUNC_DIV_EXPR:
2938 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2939 break;
2940 return;
2941
2942 /* This isn't exactly what's specified above for operator<=>, but it's
2943 close enough. In particular, we don't care about the return type
2944 specified above; it doesn't participate in overload resolution and it
2945 doesn't affect the semantics of the built-in operator. */
2946 case SPACESHIP_EXPR:
2947 case EQ_EXPR:
2948 case NE_EXPR:
2949 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
2950 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2)))
2951 break;
2952 if (NULLPTR_TYPE_P (type1) && NULLPTR_TYPE_P (type2))
2953 break;
2954 if (TYPE_PTRMEM_P (type1) && null_ptr_cst_p (args[1]))
2955 {
2956 type2 = type1;
2957 break;
2958 }
2959 if (TYPE_PTRMEM_P (type2) && null_ptr_cst_p (args[0]))
2960 {
2961 type1 = type2;
2962 break;
2963 }
2964 /* Fall through. */
2965 case LT_EXPR:
2966 case GT_EXPR:
2967 case LE_EXPR:
2968 case GE_EXPR:
2969 case MAX_EXPR:
2970 case MIN_EXPR:
2971 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2972 break;
2973 if (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
2974 break;
2975 if (TREE_CODE (type1) == ENUMERAL_TYPE
2976 && TREE_CODE (type2) == ENUMERAL_TYPE)
2977 break;
2978 if (TYPE_PTR_P (type1)
2979 && null_ptr_cst_p (args[1]))
2980 {
2981 type2 = type1;
2982 break;
2983 }
2984 if (null_ptr_cst_p (args[0])
2985 && TYPE_PTR_P (type2))
2986 {
2987 type1 = type2;
2988 break;
2989 }
2990 return;
2991
2992 case PLUS_EXPR:
2993 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
2994 break;
2995 /* FALLTHRU */
2996 case ARRAY_REF:
2997 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && TYPE_PTROB_P (type2))
2998 {
2999 type1 = ptrdiff_type_node;
3000 break;
3001 }
3002 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3003 {
3004 type2 = ptrdiff_type_node;
3005 break;
3006 }
3007 return;
3008
3009 /* 18For every pair of promoted integral types L and R, there exist candi-
3010 date operator functions of the form
3011 LR operator%(L, R);
3012 LR operator&(L, R);
3013 LR operator^(L, R);
3014 LR operator|(L, R);
3015 L operator<<(L, R);
3016 L operator>>(L, R);
3017 where LR is the result of the usual arithmetic conversions between
3018 types L and R. */
3019
3020 case TRUNC_MOD_EXPR:
3021 case BIT_AND_EXPR:
3022 case BIT_IOR_EXPR:
3023 case BIT_XOR_EXPR:
3024 case LSHIFT_EXPR:
3025 case RSHIFT_EXPR:
3026 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3027 break;
3028 return;
3029
3030 /* 19For every triple L, VQ, R), where L is an arithmetic or enumeration
3031 type, VQ is either volatile or empty, and R is a promoted arithmetic
3032 type, there exist candidate operator functions of the form
3033 VQ L& operator=(VQ L&, R);
3034 VQ L& operator*=(VQ L&, R);
3035 VQ L& operator/=(VQ L&, R);
3036 VQ L& operator+=(VQ L&, R);
3037 VQ L& operator-=(VQ L&, R);
3038
3039 20For every pair T, VQ), where T is any type and VQ is either volatile
3040 or empty, there exist candidate operator functions of the form
3041 T*VQ& operator=(T*VQ&, T*);
3042
3043 21For every pair T, VQ), where T is a pointer to member type and VQ is
3044 either volatile or empty, there exist candidate operator functions of
3045 the form
3046 VQ T& operator=(VQ T&, T);
3047
3048 22For every triple T, VQ, I), where T is a cv-qualified or cv-
3049 unqualified complete object type, VQ is either volatile or empty, and
3050 I is a promoted integral type, there exist candidate operator func-
3051 tions of the form
3052 T*VQ& operator+=(T*VQ&, I);
3053 T*VQ& operator-=(T*VQ&, I);
3054
3055 23For every triple L, VQ, R), where L is an integral or enumeration
3056 type, VQ is either volatile or empty, and R is a promoted integral
3057 type, there exist candidate operator functions of the form
3058
3059 VQ L& operator%=(VQ L&, R);
3060 VQ L& operator<<=(VQ L&, R);
3061 VQ L& operator>>=(VQ L&, R);
3062 VQ L& operator&=(VQ L&, R);
3063 VQ L& operator^=(VQ L&, R);
3064 VQ L& operator|=(VQ L&, R); */
3065
3066 case MODIFY_EXPR:
3067 switch (code2)
3068 {
3069 case PLUS_EXPR:
3070 case MINUS_EXPR:
3071 if (TYPE_PTROB_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3072 {
3073 type2 = ptrdiff_type_node;
3074 break;
3075 }
3076 /* FALLTHRU */
3077 case MULT_EXPR:
3078 case TRUNC_DIV_EXPR:
3079 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3080 break;
3081 return;
3082
3083 case TRUNC_MOD_EXPR:
3084 case BIT_AND_EXPR:
3085 case BIT_IOR_EXPR:
3086 case BIT_XOR_EXPR:
3087 case LSHIFT_EXPR:
3088 case RSHIFT_EXPR:
3089 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type1) && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type2))
3090 break;
3091 return;
3092
3093 case NOP_EXPR:
3094 if (ARITHMETIC_TYPE_P (type1) && ARITHMETIC_TYPE_P (type2))
3095 break;
3096 if ((TYPE_PTRMEMFUNC_P (type1) && TYPE_PTRMEMFUNC_P (type2))
3097 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3098 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
3099 || ((TYPE_PTRMEMFUNC_P (type1)
3100 || TYPE_PTR_P (type1))
3101 && null_ptr_cst_p (args[1])))
3102 {
3103 type2 = type1;
3104 break;
3105 }
3106 return;
3107
3108 default:
3109 gcc_unreachable ();
3110 }
3111 type1 = build_reference_type (type1);
3112 break;
3113
3114 case COND_EXPR:
3115 /* [over.built]
3116
3117 For every pair of promoted arithmetic types L and R, there
3118 exist candidate operator functions of the form
3119
3120 LR operator?(bool, L, R);
3121
3122 where LR is the result of the usual arithmetic conversions
3123 between types L and R.
3124
3125 For every type T, where T is a pointer or pointer-to-member
3126 type, there exist candidate operator functions of the form T
3127 operator?(bool, T, T); */
3128
3129 if (promoted_arithmetic_type_p (type1)
3130 && promoted_arithmetic_type_p (type2))
3131 /* That's OK. */
3132 break;
3133
3134 /* Otherwise, the types should be pointers. */
3135 if (!TYPE_PTR_OR_PTRMEM_P (type1) || !TYPE_PTR_OR_PTRMEM_P (type2))
3136 return;
3137
3138 /* We don't check that the two types are the same; the logic
3139 below will actually create two candidates; one in which both
3140 parameter types are TYPE1, and one in which both parameter
3141 types are TYPE2. */
3142 break;
3143
3144 case REALPART_EXPR:
3145 case IMAGPART_EXPR:
3146 if (ARITHMETIC_TYPE_P (type1))
3147 break;
3148 return;
3149
3150 default:
3151 gcc_unreachable ();
3152 }
3153
3154 /* Make sure we don't create builtin candidates with dependent types. */
3155 bool u1 = uses_template_parms (type1);
3156 bool u2 = type2 ? uses_template_parms (type2) : false;
3157 if (u1 || u2)
3158 {
3159 /* Try to recover if one of the types is non-dependent. But if
3160 there's only one type, there's nothing we can do. */
3161 if (!type2)
3162 return;
3163 /* And we lose if both are dependent. */
3164 if (u1 && u2)
3165 return;
3166 /* Or if they have different forms. */
3167 if (TREE_CODE (type1) != TREE_CODE (type2))
3168 return;
3169
3170 if (u1 && !u2)
3171 type1 = type2;
3172 else if (u2 && !u1)
3173 type2 = type1;
3174 }
3175
3176 /* If we're dealing with two pointer types or two enumeral types,
3177 we need candidates for both of them. */
3178 if (type2 && !same_type_p (type1, type2)
3179 && TREE_CODE (type1) == TREE_CODE (type2)
3180 && (TYPE_REF_P (type1)
3181 || (TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
3182 || (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
3183 || TYPE_PTRMEMFUNC_P (type1)
3184 || MAYBE_CLASS_TYPE_P (type1)
3185 || TREE_CODE (type1) == ENUMERAL_TYPE))
3186 {
3187 if (TYPE_PTR_OR_PTRMEM_P (type1))
3188 {
3189 tree cptype = composite_pointer_type (input_location,
3190 type1, type2,
3191 error_mark_node,
3192 error_mark_node,
3193 CPO_CONVERSION,
3194 tf_none);
3195 if (cptype != error_mark_node)
3196 {
3197 build_builtin_candidate
3198 (candidates, fnname, cptype, cptype, args, argtypes,
3199 flags, complain);
3200 return;
3201 }
3202 }
3203
3204 build_builtin_candidate
3205 (candidates, fnname, type1, type1, args, argtypes, flags, complain);
3206 build_builtin_candidate
3207 (candidates, fnname, type2, type2, args, argtypes, flags, complain);
3208 return;
3209 }
3210
3211 build_builtin_candidate
3212 (candidates, fnname, type1, type2, args, argtypes, flags, complain);
3213 }
3214
3215 tree
3216 type_decays_to (tree type)
3217 {
3218 if (TREE_CODE (type) == ARRAY_TYPE)
3219 return build_pointer_type (TREE_TYPE (type));
3220 if (TREE_CODE (type) == FUNCTION_TYPE)
3221 return build_pointer_type (type);
3222 return type;
3223 }
3224
3225 /* There are three conditions of builtin candidates:
3226
3227 1) bool-taking candidates. These are the same regardless of the input.
3228 2) pointer-pair taking candidates. These are generated for each type
3229 one of the input types converts to.
3230 3) arithmetic candidates. According to the standard, we should generate
3231 all of these, but I'm trying not to...
3232
3233 Here we generate a superset of the possible candidates for this particular
3234 case. That is a subset of the full set the standard defines, plus some
3235 other cases which the standard disallows. add_builtin_candidate will
3236 filter out the invalid set. */
3237
3238 static void
3239 add_builtin_candidates (struct z_candidate **candidates, enum tree_code code,
3240 enum tree_code code2, tree fnname,
3241 vec<tree, va_gc> *argv,
3242 int flags, tsubst_flags_t complain)
3243 {
3244 int ref1;
3245 int enum_p = 0;
3246 tree type, argtypes[3], t;
3247 /* TYPES[i] is the set of possible builtin-operator parameter types
3248 we will consider for the Ith argument. */
3249 vec<tree, va_gc> *types[2];
3250 unsigned ix;
3251 vec<tree, va_gc> &args = *argv;
3252 unsigned len = args.length ();
3253
3254 for (unsigned i = 0; i < len; ++i)
3255 {
3256 if (args[i])
3257 argtypes[i] = unlowered_expr_type (args[i]);
3258 else
3259 argtypes[i] = NULL_TREE;
3260 }
3261
3262 switch (code)
3263 {
3264 /* 4 For every pair T, VQ), where T is an arithmetic or enumeration type,
3265 and VQ is either volatile or empty, there exist candidate operator
3266 functions of the form
3267 VQ T& operator++(VQ T&); */
3268
3269 case POSTINCREMENT_EXPR:
3270 case PREINCREMENT_EXPR:
3271 case POSTDECREMENT_EXPR:
3272 case PREDECREMENT_EXPR:
3273 case MODIFY_EXPR:
3274 ref1 = 1;
3275 break;
3276
3277 /* 24There also exist candidate operator functions of the form
3278 bool operator!(bool);
3279 bool operator&&(bool, bool);
3280 bool operator||(bool, bool); */
3281
3282 case TRUTH_NOT_EXPR:
3283 build_builtin_candidate
3284 (candidates, fnname, boolean_type_node,
3285 NULL_TREE, args, argtypes, flags, complain);
3286 return;
3287
3288 case TRUTH_ORIF_EXPR:
3289 case TRUTH_ANDIF_EXPR:
3290 build_builtin_candidate
3291 (candidates, fnname, boolean_type_node,
3292 boolean_type_node, args, argtypes, flags, complain);
3293 return;
3294
3295 case ADDR_EXPR:
3296 case COMPOUND_EXPR:
3297 case COMPONENT_REF:
3298 case CO_AWAIT_EXPR:
3299 return;
3300
3301 case COND_EXPR:
3302 case EQ_EXPR:
3303 case NE_EXPR:
3304 case LT_EXPR:
3305 case LE_EXPR:
3306 case GT_EXPR:
3307 case GE_EXPR:
3308 case SPACESHIP_EXPR:
3309 enum_p = 1;
3310 /* Fall through. */
3311
3312 default:
3313 ref1 = 0;
3314 }
3315
3316 types[0] = make_tree_vector ();
3317 types[1] = make_tree_vector ();
3318
3319 if (len == 3)
3320 len = 2;
3321 for (unsigned i = 0; i < len; ++i)
3322 {
3323 if (MAYBE_CLASS_TYPE_P (argtypes[i]))
3324 {
3325 tree convs;
3326
3327 if (i == 0 && code == MODIFY_EXPR && code2 == NOP_EXPR)
3328 return;
3329
3330 convs = lookup_conversions (argtypes[i]);
3331
3332 if (code == COND_EXPR)
3333 {
3334 if (lvalue_p (args[i]))
3335 vec_safe_push (types[i], build_reference_type (argtypes[i]));
3336
3337 vec_safe_push (types[i], TYPE_MAIN_VARIANT (argtypes[i]));
3338 }
3339
3340 else if (! convs)
3341 return;
3342
3343 for (; convs; convs = TREE_CHAIN (convs))
3344 {
3345 type = TREE_TYPE (convs);
3346
3347 if (i == 0 && ref1
3348 && (!TYPE_REF_P (type)
3349 || CP_TYPE_CONST_P (TREE_TYPE (type))))
3350 continue;
3351
3352 if (code == COND_EXPR && TYPE_REF_P (type))
3353 vec_safe_push (types[i], type);
3354
3355 type = non_reference (type);
3356 if (i != 0 || ! ref1)
3357 {
3358 type = cv_unqualified (type_decays_to (type));
3359 if (enum_p && TREE_CODE (type) == ENUMERAL_TYPE)
3360 vec_safe_push (types[i], type);
3361 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3362 type = type_promotes_to (type);
3363 }
3364
3365 if (! vec_member (type, types[i]))
3366 vec_safe_push (types[i], type);
3367 }
3368 }
3369 else
3370 {
3371 if (code == COND_EXPR && lvalue_p (args[i]))
3372 vec_safe_push (types[i], build_reference_type (argtypes[i]));
3373 type = non_reference (argtypes[i]);
3374 if (i != 0 || ! ref1)
3375 {
3376 type = cv_unqualified (type_decays_to (type));
3377 if (enum_p && UNSCOPED_ENUM_P (type))
3378 vec_safe_push (types[i], type);
3379 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3380 type = type_promotes_to (type);
3381 }
3382 vec_safe_push (types[i], type);
3383 }
3384 }
3385
3386 /* Run through the possible parameter types of both arguments,
3387 creating candidates with those parameter types. */
3388 FOR_EACH_VEC_ELT_REVERSE (*(types[0]), ix, t)
3389 {
3390 unsigned jx;
3391 tree u;
3392
3393 if (!types[1]->is_empty ())
3394 FOR_EACH_VEC_ELT_REVERSE (*(types[1]), jx, u)
3395 add_builtin_candidate
3396 (candidates, code, code2, fnname, t,
3397 u, args, argtypes, flags, complain);
3398 else
3399 add_builtin_candidate
3400 (candidates, code, code2, fnname, t,
3401 NULL_TREE, args, argtypes, flags, complain);
3402 }
3403
3404 release_tree_vector (types[0]);
3405 release_tree_vector (types[1]);
3406 }
3407
3408
3409 /* If TMPL can be successfully instantiated as indicated by
3410 EXPLICIT_TARGS and ARGLIST, adds the instantiation to CANDIDATES.
3411
3412 TMPL is the template. EXPLICIT_TARGS are any explicit template
3413 arguments. ARGLIST is the arguments provided at the call-site.
3414 This does not change ARGLIST. The RETURN_TYPE is the desired type
3415 for conversion operators. If OBJ is NULL_TREE, FLAGS and CTYPE are
3416 as for add_function_candidate. If an OBJ is supplied, FLAGS and
3417 CTYPE are ignored, and OBJ is as for add_conv_candidate.
3418
3419 SHORTCUT_BAD_CONVS is as in add_function_candidate. */
3420
3421 static struct z_candidate*
3422 add_template_candidate_real (struct z_candidate **candidates, tree tmpl,
3423 tree ctype, tree explicit_targs, tree first_arg,
3424 const vec<tree, va_gc> *arglist, tree return_type,
3425 tree access_path, tree conversion_path,
3426 int flags, tree obj, unification_kind_t strict,
3427 bool shortcut_bad_convs, tsubst_flags_t complain)
3428 {
3429 int ntparms = DECL_NTPARMS (tmpl);
3430 tree targs = make_tree_vec (ntparms);
3431 unsigned int len = vec_safe_length (arglist);
3432 unsigned int nargs = (first_arg == NULL_TREE ? 0 : 1) + len;
3433 unsigned int skip_without_in_chrg = 0;
3434 tree first_arg_without_in_chrg = first_arg;
3435 tree *args_without_in_chrg;
3436 unsigned int nargs_without_in_chrg;
3437 unsigned int ia, ix;
3438 tree arg;
3439 struct z_candidate *cand;
3440 tree fn;
3441 struct rejection_reason *reason = NULL;
3442 int errs;
3443 conversion **convs = NULL;
3444
3445 /* We don't do deduction on the in-charge parameter, the VTT
3446 parameter or 'this'. */
3447 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl))
3448 {
3449 if (first_arg_without_in_chrg != NULL_TREE)
3450 first_arg_without_in_chrg = NULL_TREE;
3451 else if (return_type && strict == DEDUCE_CALL)
3452 /* We're deducing for a call to the result of a template conversion
3453 function, so the args don't contain 'this'; leave them alone. */;
3454 else
3455 ++skip_without_in_chrg;
3456 }
3457
3458 if ((DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (tmpl)
3459 || DECL_BASE_CONSTRUCTOR_P (tmpl))
3460 && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (tmpl)))
3461 {
3462 if (first_arg_without_in_chrg != NULL_TREE)
3463 first_arg_without_in_chrg = NULL_TREE;
3464 else
3465 ++skip_without_in_chrg;
3466 }
3467
3468 if (len < skip_without_in_chrg)
3469 return NULL;
3470
3471 if (DECL_CONSTRUCTOR_P (tmpl) && nargs == 2
3472 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (first_arg),
3473 TREE_TYPE ((*arglist)[0])))
3474 {
3475 /* 12.8/6 says, "A declaration of a constructor for a class X is
3476 ill-formed if its first parameter is of type (optionally cv-qualified)
3477 X and either there are no other parameters or else all other
3478 parameters have default arguments. A member function template is never
3479 instantiated to produce such a constructor signature."
3480
3481 So if we're trying to copy an object of the containing class, don't
3482 consider a template constructor that has a first parameter type that
3483 is just a template parameter, as we would deduce a signature that we
3484 would then reject in the code below. */
3485 if (tree firstparm = FUNCTION_FIRST_USER_PARMTYPE (tmpl))
3486 {
3487 firstparm = TREE_VALUE (firstparm);
3488 if (PACK_EXPANSION_P (firstparm))
3489 firstparm = PACK_EXPANSION_PATTERN (firstparm);
3490 if (TREE_CODE (firstparm) == TEMPLATE_TYPE_PARM)
3491 {
3492 gcc_assert (!explicit_targs);
3493 reason = invalid_copy_with_fn_template_rejection ();
3494 goto fail;
3495 }
3496 }
3497 }
3498
3499 nargs_without_in_chrg = ((first_arg_without_in_chrg != NULL_TREE ? 1 : 0)
3500 + (len - skip_without_in_chrg));
3501 args_without_in_chrg = XALLOCAVEC (tree, nargs_without_in_chrg);
3502 ia = 0;
3503 if (first_arg_without_in_chrg != NULL_TREE)
3504 {
3505 args_without_in_chrg[ia] = first_arg_without_in_chrg;
3506 ++ia;
3507 }
3508 for (ix = skip_without_in_chrg;
3509 vec_safe_iterate (arglist, ix, &arg);
3510 ++ix)
3511 {
3512 args_without_in_chrg[ia] = arg;
3513 ++ia;
3514 }
3515 gcc_assert (ia == nargs_without_in_chrg);
3516
3517 if (!obj && explicit_targs)
3518 {
3519 /* Check that there's no obvious arity mismatch before proceeding with
3520 deduction. This avoids substituting explicit template arguments
3521 into the template (which could result in an error outside the
3522 immediate context) when the resulting candidate would be unviable
3523 anyway. */
3524 int min_arity = 0, max_arity = 0;
3525 tree parms = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
3526 parms = skip_artificial_parms_for (tmpl, parms);
3527 for (; parms != void_list_node; parms = TREE_CHAIN (parms))
3528 {
3529 if (!parms || PACK_EXPANSION_P (TREE_VALUE (parms)))
3530 {
3531 max_arity = -1;
3532 break;
3533 }
3534 if (TREE_PURPOSE (parms))
3535 /* A parameter with a default argument. */
3536 ++max_arity;
3537 else
3538 ++min_arity, ++max_arity;
3539 }
3540 if (ia < (unsigned)min_arity)
3541 {
3542 /* Too few arguments. */
3543 reason = arity_rejection (NULL_TREE, min_arity, ia,
3544 /*least_p=*/(max_arity == -1));
3545 goto fail;
3546 }
3547 else if (max_arity != -1 && ia > (unsigned)max_arity)
3548 {
3549 /* Too many arguments. */
3550 reason = arity_rejection (NULL_TREE, max_arity, ia);
3551 goto fail;
3552 }
3553 }
3554
3555 errs = errorcount+sorrycount;
3556 if (!obj)
3557 {
3558 convs = alloc_conversions (nargs);
3559
3560 if (shortcut_bad_convs
3561 && DECL_NONSTATIC_MEMBER_FUNCTION_P (tmpl)
3562 && !DECL_CONSTRUCTOR_P (tmpl))
3563 {
3564 /* Check the 'this' conversion before proceeding with deduction.
3565 This is effectively an extension of the DR 1391 resolution
3566 that we perform in check_non_deducible_conversions, though it's
3567 convenient to do this extra check here instead of there. */
3568 tree parmtype = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (tmpl)));
3569 tree argtype = lvalue_type (first_arg);
3570 tree arg = first_arg;
3571 conversion *t = build_this_conversion (tmpl, ctype,
3572 parmtype, argtype, arg,
3573 flags, complain);
3574 convs[0] = t;
3575 if (t->bad_p)
3576 {
3577 reason = bad_arg_conversion_rejection (first_arg, 0,
3578 arg, parmtype,
3579 EXPR_LOCATION (arg));
3580 goto fail;
3581 }
3582 }
3583 }
3584 fn = fn_type_unification (tmpl, explicit_targs, targs,
3585 args_without_in_chrg,
3586 nargs_without_in_chrg,
3587 return_type, strict, flags, convs,
3588 false, complain & tf_decltype);
3589
3590 if (fn == error_mark_node)
3591 {
3592 /* Don't repeat unification later if it already resulted in errors. */
3593 if (errorcount+sorrycount == errs)
3594 reason = template_unification_rejection (tmpl, explicit_targs,
3595 targs, args_without_in_chrg,
3596 nargs_without_in_chrg,
3597 return_type, strict, flags);
3598 else
3599 reason = template_unification_error_rejection ();
3600 goto fail;
3601 }
3602
3603 /* Now the explicit specifier might have been deduced; check if this
3604 declaration is explicit. If it is and we're ignoring non-converting
3605 constructors, don't add this function to the set of candidates. */
3606 if ((flags & LOOKUP_ONLYCONVERTING) && DECL_NONCONVERTING_P (fn))
3607 return NULL;
3608
3609 if (DECL_CONSTRUCTOR_P (fn) && nargs == 2)
3610 {
3611 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (fn);
3612 if (arg_types && same_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (arg_types)),
3613 ctype))
3614 {
3615 /* We're trying to produce a constructor with a prohibited signature,
3616 as discussed above; handle here any cases we didn't catch then,
3617 such as X(X<T>). */
3618 reason = invalid_copy_with_fn_template_rejection ();
3619 goto fail;
3620 }
3621 }
3622
3623 if (obj != NULL_TREE)
3624 /* Aha, this is a conversion function. */
3625 cand = add_conv_candidate (candidates, fn, obj, arglist,
3626 access_path, conversion_path, complain);
3627 else
3628 cand = add_function_candidate (candidates, fn, ctype,
3629 first_arg, arglist, access_path,
3630 conversion_path, flags, convs,
3631 shortcut_bad_convs, complain);
3632 if (DECL_TI_TEMPLATE (fn) != tmpl)
3633 /* This situation can occur if a member template of a template
3634 class is specialized. Then, instantiate_template might return
3635 an instantiation of the specialization, in which case the
3636 DECL_TI_TEMPLATE field will point at the original
3637 specialization. For example:
3638
3639 template <class T> struct S { template <class U> void f(U);
3640 template <> void f(int) {}; };
3641 S<double> sd;
3642 sd.f(3);
3643
3644 Here, TMPL will be template <class U> S<double>::f(U).
3645 And, instantiate template will give us the specialization
3646 template <> S<double>::f(int). But, the DECL_TI_TEMPLATE field
3647 for this will point at template <class T> template <> S<T>::f(int),
3648 so that we can find the definition. For the purposes of
3649 overload resolution, however, we want the original TMPL. */
3650 cand->template_decl = build_template_info (tmpl, targs);
3651 else
3652 cand->template_decl = DECL_TEMPLATE_INFO (fn);
3653 cand->explicit_targs = explicit_targs;
3654
3655 return cand;
3656 fail:
3657 int viable = (reason->code == rr_bad_arg_conversion ? -1 : 0);
3658 return add_candidate (candidates, tmpl, first_arg, arglist, nargs, convs,
3659 access_path, conversion_path, viable, reason, flags);
3660 }
3661
3662
3663 static struct z_candidate *
3664 add_template_candidate (struct z_candidate **candidates, tree tmpl, tree ctype,
3665 tree explicit_targs, tree first_arg,
3666 const vec<tree, va_gc> *arglist, tree return_type,
3667 tree access_path, tree conversion_path, int flags,
3668 unification_kind_t strict, bool shortcut_bad_convs,
3669 tsubst_flags_t complain)
3670 {
3671 return
3672 add_template_candidate_real (candidates, tmpl, ctype,
3673 explicit_targs, first_arg, arglist,
3674 return_type, access_path, conversion_path,
3675 flags, NULL_TREE, strict, shortcut_bad_convs,
3676 complain);
3677 }
3678
3679 /* Create an overload candidate for the conversion function template TMPL,
3680 returning RETURN_TYPE, which will be invoked for expression OBJ to produce a
3681 pointer-to-function which will in turn be called with the argument list
3682 ARGLIST, and add it to CANDIDATES. This does not change ARGLIST. FLAGS is
3683 passed on to implicit_conversion. */
3684
3685 static struct z_candidate *
3686 add_template_conv_candidate (struct z_candidate **candidates, tree tmpl,
3687 tree obj,
3688 const vec<tree, va_gc> *arglist,
3689 tree return_type, tree access_path,
3690 tree conversion_path, tsubst_flags_t complain)
3691 {
3692 /* Making this work broke PR 71117 and 85118, so until the committee resolves
3693 core issue 2189, let's disable this candidate if there are any call
3694 operators. */
3695 if (*candidates)
3696 return NULL;
3697
3698 return
3699 add_template_candidate_real (candidates, tmpl, NULL_TREE, NULL_TREE,
3700 NULL_TREE, arglist, return_type, access_path,
3701 conversion_path, 0, obj, DEDUCE_CALL,
3702 /*shortcut_bad_convs=*/false, complain);
3703 }
3704
3705 /* The CANDS are the set of candidates that were considered for
3706 overload resolution. Return the set of viable candidates, or CANDS
3707 if none are viable. If any of the candidates were viable, set
3708 *ANY_VIABLE_P to true. STRICT_P is true if a candidate should be
3709 considered viable only if it is strictly viable. */
3710
3711 static struct z_candidate*
3712 splice_viable (struct z_candidate *cands,
3713 bool strict_p,
3714 bool *any_viable_p)
3715 {
3716 struct z_candidate *viable;
3717 struct z_candidate **last_viable;
3718 struct z_candidate **cand;
3719 bool found_strictly_viable = false;
3720
3721 /* Be strict inside templates, since build_over_call won't actually
3722 do the conversions to get pedwarns. */
3723 if (processing_template_decl)
3724 strict_p = true;
3725
3726 viable = NULL;
3727 last_viable = &viable;
3728 *any_viable_p = false;
3729
3730 cand = &cands;
3731 while (*cand)
3732 {
3733 struct z_candidate *c = *cand;
3734 if (!strict_p
3735 && (c->viable == 1 || TREE_CODE (c->fn) == TEMPLATE_DECL))
3736 {
3737 /* Be strict in the presence of a viable candidate. Also if
3738 there are template candidates, so that we get deduction errors
3739 for them instead of silently preferring a bad conversion. */
3740 strict_p = true;
3741 if (viable && !found_strictly_viable)
3742 {
3743 /* Put any spliced near matches back onto the main list so
3744 that we see them if there is no strict match. */
3745 *any_viable_p = false;
3746 *last_viable = cands;
3747 cands = viable;
3748 viable = NULL;
3749 last_viable = &viable;
3750 }
3751 }
3752
3753 if (strict_p ? c->viable == 1 : c->viable)
3754 {
3755 *last_viable = c;
3756 *cand = c->next;
3757 c->next = NULL;
3758 last_viable = &c->next;
3759 *any_viable_p = true;
3760 if (c->viable == 1)
3761 found_strictly_viable = true;
3762 }
3763 else
3764 cand = &c->next;
3765 }
3766
3767 return viable ? viable : cands;
3768 }
3769
3770 static bool
3771 any_strictly_viable (struct z_candidate *cands)
3772 {
3773 for (; cands; cands = cands->next)
3774 if (cands->viable == 1)
3775 return true;
3776 return false;
3777 }
3778
3779 /* OBJ is being used in an expression like "OBJ.f (...)". In other
3780 words, it is about to become the "this" pointer for a member
3781 function call. Take the address of the object. */
3782
3783 static tree
3784 build_this (tree obj)
3785 {
3786 /* In a template, we are only concerned about the type of the
3787 expression, so we can take a shortcut. */
3788 if (processing_template_decl)
3789 return build_address (obj);
3790
3791 return cp_build_addr_expr (obj, tf_warning_or_error);
3792 }
3793
3794 /* Returns true iff functions are equivalent. Equivalent functions are
3795 not '==' only if one is a function-local extern function or if
3796 both are extern "C". */
3797
3798 static inline int
3799 equal_functions (tree fn1, tree fn2)
3800 {
3801 if (TREE_CODE (fn1) != TREE_CODE (fn2))
3802 return 0;
3803 if (TREE_CODE (fn1) == TEMPLATE_DECL)
3804 return fn1 == fn2;
3805 if (DECL_LOCAL_DECL_P (fn1) || DECL_LOCAL_DECL_P (fn2)
3806 || DECL_EXTERN_C_FUNCTION_P (fn1))
3807 return decls_match (fn1, fn2);
3808 return fn1 == fn2;
3809 }
3810
3811 /* Print information about a candidate FN being rejected due to INFO. */
3812
3813 static void
3814 print_conversion_rejection (location_t loc, struct conversion_info *info,
3815 tree fn)
3816 {
3817 tree from = info->from;
3818 if (!TYPE_P (from))
3819 from = lvalue_type (from);
3820 if (info->n_arg == -1)
3821 {
3822 /* Conversion of implicit `this' argument failed. */
3823 if (!TYPE_P (info->from))
3824 /* A bad conversion for 'this' must be discarding cv-quals. */
3825 inform (loc, " passing %qT as %<this%> "
3826 "argument discards qualifiers",
3827 from);
3828 else
3829 inform (loc, " no known conversion for implicit "
3830 "%<this%> parameter from %qH to %qI",
3831 from, info->to_type);
3832 }
3833 else if (!TYPE_P (info->from))
3834 {
3835 if (info->n_arg >= 0)
3836 inform (loc, " conversion of argument %d would be ill-formed:",
3837 info->n_arg + 1);
3838 perform_implicit_conversion (info->to_type, info->from,
3839 tf_warning_or_error);
3840 }
3841 else if (info->n_arg == -2)
3842 /* Conversion of conversion function return value failed. */
3843 inform (loc, " no known conversion from %qH to %qI",
3844 from, info->to_type);
3845 else
3846 {
3847 if (TREE_CODE (fn) == FUNCTION_DECL)
3848 loc = get_fndecl_argument_location (fn, info->n_arg);
3849 inform (loc, " no known conversion for argument %d from %qH to %qI",
3850 info->n_arg + 1, from, info->to_type);
3851 }
3852 }
3853
3854 /* Print information about a candidate with WANT parameters and we found
3855 HAVE. */
3856
3857 static void
3858 print_arity_information (location_t loc, unsigned int have, unsigned int want,
3859 bool least_p)
3860 {
3861 if (least_p)
3862 inform_n (loc, want,
3863 " candidate expects at least %d argument, %d provided",
3864 " candidate expects at least %d arguments, %d provided",
3865 want, have);
3866 else
3867 inform_n (loc, want,
3868 " candidate expects %d argument, %d provided",
3869 " candidate expects %d arguments, %d provided",
3870 want, have);
3871 }
3872
3873 /* Print information about one overload candidate CANDIDATE. MSGSTR
3874 is the text to print before the candidate itself.
3875
3876 NOTE: Unlike most diagnostic functions in GCC, MSGSTR is expected
3877 to have been run through gettext by the caller. This wart makes
3878 life simpler in print_z_candidates and for the translators. */
3879
3880 static void
3881 print_z_candidate (location_t loc, const char *msgstr,
3882 struct z_candidate *candidate)
3883 {
3884 const char *msg = (msgstr == NULL
3885 ? ""
3886 : ACONCAT ((_(msgstr), " ", NULL)));
3887 tree fn = candidate->fn;
3888 if (flag_new_inheriting_ctors)
3889 fn = strip_inheriting_ctors (fn);
3890 location_t cloc = location_of (fn);
3891
3892 if (identifier_p (fn))
3893 {
3894 cloc = loc;
3895 if (candidate->num_convs == 3)
3896 inform (cloc, "%s%<%D(%T, %T, %T)%> (built-in)", msg, fn,
3897 candidate->convs[0]->type,
3898 candidate->convs[1]->type,
3899 candidate->convs[2]->type);
3900 else if (candidate->num_convs == 2)
3901 inform (cloc, "%s%<%D(%T, %T)%> (built-in)", msg, fn,
3902 candidate->convs[0]->type,
3903 candidate->convs[1]->type);
3904 else
3905 inform (cloc, "%s%<%D(%T)%> (built-in)", msg, fn,
3906 candidate->convs[0]->type);
3907 }
3908 else if (TYPE_P (fn))
3909 inform (cloc, "%s%qT (conversion)", msg, fn);
3910 else if (candidate->viable == -1)
3911 inform (cloc, "%s%#qD (near match)", msg, fn);
3912 else if (DECL_DELETED_FN (fn))
3913 inform (cloc, "%s%#qD (deleted)", msg, fn);
3914 else if (candidate->reversed ())
3915 inform (cloc, "%s%#qD (reversed)", msg, fn);
3916 else if (candidate->rewritten ())
3917 inform (cloc, "%s%#qD (rewritten)", msg, fn);
3918 else
3919 inform (cloc, "%s%#qD", msg, fn);
3920 if (fn != candidate->fn)
3921 {
3922 cloc = location_of (candidate->fn);
3923 inform (cloc, " inherited here");
3924 }
3925 /* Give the user some information about why this candidate failed. */
3926 if (candidate->reason != NULL)
3927 {
3928 struct rejection_reason *r = candidate->reason;
3929
3930 switch (r->code)
3931 {
3932 case rr_arity:
3933 print_arity_information (cloc, r->u.arity.actual,
3934 r->u.arity.expected,
3935 r->u.arity.least_p);
3936 break;
3937 case rr_arg_conversion:
3938 print_conversion_rejection (cloc, &r->u.conversion, fn);
3939 break;
3940 case rr_bad_arg_conversion:
3941 print_conversion_rejection (cloc, &r->u.bad_conversion, fn);
3942 break;
3943 case rr_explicit_conversion:
3944 inform (cloc, " return type %qT of explicit conversion function "
3945 "cannot be converted to %qT with a qualification "
3946 "conversion", r->u.conversion.from,
3947 r->u.conversion.to_type);
3948 break;
3949 case rr_template_conversion:
3950 inform (cloc, " conversion from return type %qT of template "
3951 "conversion function specialization to %qT is not an "
3952 "exact match", r->u.conversion.from,
3953 r->u.conversion.to_type);
3954 break;
3955 case rr_template_unification:
3956 /* We use template_unification_error_rejection if unification caused
3957 actual non-SFINAE errors, in which case we don't need to repeat
3958 them here. */
3959 if (r->u.template_unification.tmpl == NULL_TREE)
3960 {
3961 inform (cloc, " substitution of deduced template arguments "
3962 "resulted in errors seen above");
3963 break;
3964 }
3965 /* Re-run template unification with diagnostics. */
3966 inform (cloc, " template argument deduction/substitution failed:");
3967 fn_type_unification (r->u.template_unification.tmpl,
3968 r->u.template_unification.explicit_targs,
3969 (make_tree_vec
3970 (r->u.template_unification.num_targs)),
3971 r->u.template_unification.args,
3972 r->u.template_unification.nargs,
3973 r->u.template_unification.return_type,
3974 r->u.template_unification.strict,
3975 r->u.template_unification.flags,
3976 NULL, true, false);
3977 break;
3978 case rr_invalid_copy:
3979 inform (cloc,
3980 " a constructor taking a single argument of its own "
3981 "class type is invalid");
3982 break;
3983 case rr_constraint_failure:
3984 diagnose_constraints (cloc, fn, NULL_TREE);
3985 break;
3986 case rr_inherited_ctor:
3987 inform (cloc, " an inherited constructor is not a candidate for "
3988 "initialization from an expression of the same or derived "
3989 "type");
3990 break;
3991 case rr_none:
3992 default:
3993 /* This candidate didn't have any issues or we failed to
3994 handle a particular code. Either way... */
3995 gcc_unreachable ();
3996 }
3997 }
3998 }
3999
4000 static void
4001 print_z_candidates (location_t loc, struct z_candidate *candidates)
4002 {
4003 struct z_candidate *cand1;
4004 struct z_candidate **cand2;
4005
4006 if (!candidates)
4007 return;
4008
4009 /* Remove non-viable deleted candidates. */
4010 cand1 = candidates;
4011 for (cand2 = &cand1; *cand2; )
4012 {
4013 if (TREE_CODE ((*cand2)->fn) == FUNCTION_DECL
4014 && !(*cand2)->viable
4015 && DECL_DELETED_FN ((*cand2)->fn))
4016 *cand2 = (*cand2)->next;
4017 else
4018 cand2 = &(*cand2)->next;
4019 }
4020 /* ...if there are any non-deleted ones. */
4021 if (cand1)
4022 candidates = cand1;
4023
4024 /* There may be duplicates in the set of candidates. We put off
4025 checking this condition as long as possible, since we have no way
4026 to eliminate duplicates from a set of functions in less than n^2
4027 time. Now we are about to emit an error message, so it is more
4028 permissible to go slowly. */
4029 for (cand1 = candidates; cand1; cand1 = cand1->next)
4030 {
4031 tree fn = cand1->fn;
4032 /* Skip builtin candidates and conversion functions. */
4033 if (!DECL_P (fn))
4034 continue;
4035 cand2 = &cand1->next;
4036 while (*cand2)
4037 {
4038 if (DECL_P ((*cand2)->fn)
4039 && equal_functions (fn, (*cand2)->fn))
4040 *cand2 = (*cand2)->next;
4041 else
4042 cand2 = &(*cand2)->next;
4043 }
4044 }
4045
4046 for (; candidates; candidates = candidates->next)
4047 print_z_candidate (loc, N_("candidate:"), candidates);
4048 }
4049
4050 /* USER_SEQ is a user-defined conversion sequence, beginning with a
4051 USER_CONV. STD_SEQ is the standard conversion sequence applied to
4052 the result of the conversion function to convert it to the final
4053 desired type. Merge the two sequences into a single sequence,
4054 and return the merged sequence. */
4055
4056 static conversion *
4057 merge_conversion_sequences (conversion *user_seq, conversion *std_seq)
4058 {
4059 conversion **t;
4060 bool bad = user_seq->bad_p;
4061
4062 gcc_assert (user_seq->kind == ck_user);
4063
4064 /* Find the end of the second conversion sequence. */
4065 for (t = &std_seq; (*t)->kind != ck_identity; t = &((*t)->u.next))
4066 {
4067 /* The entire sequence is a user-conversion sequence. */
4068 (*t)->user_conv_p = true;
4069 if (bad)
4070 (*t)->bad_p = true;
4071 }
4072
4073 if ((*t)->rvaluedness_matches_p)
4074 /* We're binding a reference directly to the result of the conversion.
4075 build_user_type_conversion_1 stripped the REFERENCE_TYPE from the return
4076 type, but we want it back. */
4077 user_seq->type = TREE_TYPE (TREE_TYPE (user_seq->cand->fn));
4078
4079 /* Replace the identity conversion with the user conversion
4080 sequence. */
4081 *t = user_seq;
4082
4083 return std_seq;
4084 }
4085
4086 /* Handle overload resolution for initializing an object of class type from
4087 an initializer list. First we look for a suitable constructor that
4088 takes a std::initializer_list; if we don't find one, we then look for a
4089 non-list constructor.
4090
4091 Parameters are as for add_candidates, except that the arguments are in
4092 the form of a CONSTRUCTOR (the initializer list) rather than a vector, and
4093 the RETURN_TYPE parameter is replaced by TOTYPE, the desired type. */
4094
4095 static void
4096 add_list_candidates (tree fns, tree first_arg,
4097 const vec<tree, va_gc> *args, tree totype,
4098 tree explicit_targs, bool template_only,
4099 tree conversion_path, tree access_path,
4100 int flags,
4101 struct z_candidate **candidates,
4102 tsubst_flags_t complain)
4103 {
4104 gcc_assert (*candidates == NULL);
4105
4106 /* We're looking for a ctor for list-initialization. */
4107 flags |= LOOKUP_LIST_INIT_CTOR;
4108 /* And we don't allow narrowing conversions. We also use this flag to
4109 avoid the copy constructor call for copy-list-initialization. */
4110 flags |= LOOKUP_NO_NARROWING;
4111
4112 unsigned nart = num_artificial_parms_for (OVL_FIRST (fns)) - 1;
4113 tree init_list = (*args)[nart];
4114
4115 /* Always use the default constructor if the list is empty (DR 990). */
4116 if (CONSTRUCTOR_NELTS (init_list) == 0
4117 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype))
4118 ;
4119 /* If the class has a list ctor, try passing the list as a single
4120 argument first, but only consider list ctors. */
4121 else if (TYPE_HAS_LIST_CTOR (totype))
4122 {
4123 flags |= LOOKUP_LIST_ONLY;
4124 add_candidates (fns, first_arg, args, NULL_TREE,
4125 explicit_targs, template_only, conversion_path,
4126 access_path, flags, candidates, complain);
4127 if (any_strictly_viable (*candidates))
4128 return;
4129 }
4130 else if (CONSTRUCTOR_IS_DESIGNATED_INIT (init_list)
4131 && !CP_AGGREGATE_TYPE_P (totype))
4132 {
4133 if (complain & tf_error)
4134 error ("designated initializers cannot be used with a "
4135 "non-aggregate type %qT", totype);
4136 return;
4137 }
4138
4139 /* Expand the CONSTRUCTOR into a new argument vec. */
4140 vec<tree, va_gc> *new_args;
4141 vec_alloc (new_args, nart + CONSTRUCTOR_NELTS (init_list));
4142 for (unsigned i = 0; i < nart; ++i)
4143 new_args->quick_push ((*args)[i]);
4144 for (unsigned i = 0; i < CONSTRUCTOR_NELTS (init_list); ++i)
4145 new_args->quick_push (CONSTRUCTOR_ELT (init_list, i)->value);
4146
4147 /* We aren't looking for list-ctors anymore. */
4148 flags &= ~LOOKUP_LIST_ONLY;
4149 /* We allow more user-defined conversions within an init-list. */
4150 flags &= ~LOOKUP_NO_CONVERSION;
4151
4152 add_candidates (fns, first_arg, new_args, NULL_TREE,
4153 explicit_targs, template_only, conversion_path,
4154 access_path, flags, candidates, complain);
4155 }
4156
4157 /* Returns the best overload candidate to perform the requested
4158 conversion. This function is used for three the overloading situations
4159 described in [over.match.copy], [over.match.conv], and [over.match.ref].
4160 If TOTYPE is a REFERENCE_TYPE, we're trying to find a direct binding as
4161 per [dcl.init.ref], so we ignore temporary bindings. */
4162
4163 static struct z_candidate *
4164 build_user_type_conversion_1 (tree totype, tree expr, int flags,
4165 tsubst_flags_t complain)
4166 {
4167 struct z_candidate *candidates, *cand;
4168 tree fromtype;
4169 tree ctors = NULL_TREE;
4170 tree conv_fns = NULL_TREE;
4171 conversion *conv = NULL;
4172 tree first_arg = NULL_TREE;
4173 vec<tree, va_gc> *args = NULL;
4174 bool any_viable_p;
4175 int convflags;
4176
4177 if (!expr)
4178 return NULL;
4179
4180 fromtype = TREE_TYPE (expr);
4181
4182 /* We represent conversion within a hierarchy using RVALUE_CONV and
4183 BASE_CONV, as specified by [over.best.ics]; these become plain
4184 constructor calls, as specified in [dcl.init]. */
4185 gcc_assert (!MAYBE_CLASS_TYPE_P (fromtype) || !MAYBE_CLASS_TYPE_P (totype)
4186 || !DERIVED_FROM_P (totype, fromtype));
4187
4188 if (CLASS_TYPE_P (totype))
4189 /* Use lookup_fnfields_slot instead of lookup_fnfields to avoid
4190 creating a garbage BASELINK; constructors can't be inherited. */
4191 ctors = get_class_binding (totype, complete_ctor_identifier);
4192
4193 tree to_nonref = non_reference (totype);
4194 if (MAYBE_CLASS_TYPE_P (fromtype))
4195 {
4196 if (same_type_ignoring_top_level_qualifiers_p (to_nonref, fromtype) ||
4197 (CLASS_TYPE_P (to_nonref) && CLASS_TYPE_P (fromtype)
4198 && DERIVED_FROM_P (to_nonref, fromtype)))
4199 {
4200 /* [class.conv.fct] A conversion function is never used to
4201 convert a (possibly cv-qualified) object to the (possibly
4202 cv-qualified) same object type (or a reference to it), to a
4203 (possibly cv-qualified) base class of that type (or a
4204 reference to it)... */
4205 }
4206 else
4207 conv_fns = lookup_conversions (fromtype);
4208 }
4209
4210 candidates = 0;
4211 flags |= LOOKUP_NO_CONVERSION;
4212 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4213 flags |= LOOKUP_NO_NARROWING;
4214 /* Prevent add_candidates from treating a non-strictly viable candidate
4215 as unviable. */
4216 complain |= tf_conv;
4217
4218 /* It's OK to bind a temporary for converting constructor arguments, but
4219 not in converting the return value of a conversion operator. */
4220 convflags = ((flags & LOOKUP_NO_TEMP_BIND) | LOOKUP_NO_CONVERSION
4221 | (flags & LOOKUP_NO_NARROWING));
4222 flags &= ~LOOKUP_NO_TEMP_BIND;
4223
4224 if (ctors)
4225 {
4226 int ctorflags = flags;
4227
4228 first_arg = build_dummy_object (totype);
4229
4230 /* We should never try to call the abstract or base constructor
4231 from here. */
4232 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (OVL_FIRST (ctors))
4233 && !DECL_HAS_VTT_PARM_P (OVL_FIRST (ctors)));
4234
4235 args = make_tree_vector_single (expr);
4236 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4237 {
4238 /* List-initialization. */
4239 add_list_candidates (ctors, first_arg, args, totype, NULL_TREE,
4240 false, TYPE_BINFO (totype), TYPE_BINFO (totype),
4241 ctorflags, &candidates, complain);
4242 }
4243 else
4244 {
4245 add_candidates (ctors, first_arg, args, NULL_TREE, NULL_TREE, false,
4246 TYPE_BINFO (totype), TYPE_BINFO (totype),
4247 ctorflags, &candidates, complain);
4248 }
4249
4250 for (cand = candidates; cand; cand = cand->next)
4251 {
4252 cand->second_conv = build_identity_conv (totype, NULL_TREE);
4253
4254 /* If totype isn't a reference, and LOOKUP_ONLYCONVERTING is
4255 set, then this is copy-initialization. In that case, "The
4256 result of the call is then used to direct-initialize the
4257 object that is the destination of the copy-initialization."
4258 [dcl.init]
4259
4260 We represent this in the conversion sequence with an
4261 rvalue conversion, which means a constructor call. */
4262 if (!TYPE_REF_P (totype)
4263 && cxx_dialect < cxx17
4264 && (flags & LOOKUP_ONLYCONVERTING)
4265 && !(convflags & LOOKUP_NO_TEMP_BIND))
4266 cand->second_conv
4267 = build_conv (ck_rvalue, totype, cand->second_conv);
4268 }
4269 }
4270
4271 if (conv_fns)
4272 {
4273 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
4274 first_arg = CONSTRUCTOR_ELT (expr, 0)->value;
4275 else
4276 first_arg = expr;
4277 }
4278
4279 for (; conv_fns; conv_fns = TREE_CHAIN (conv_fns))
4280 {
4281 tree conversion_path = TREE_PURPOSE (conv_fns);
4282 struct z_candidate *old_candidates;
4283
4284 /* If LOOKUP_NO_CONVERSION, don't consider a conversion function that
4285 would need an addional user-defined conversion, i.e. if the return
4286 type differs in class-ness from the desired type. So we avoid
4287 considering operator bool when calling a copy constructor.
4288
4289 This optimization avoids the failure in PR97600, and is allowed by
4290 [temp.inst]/9: "If the function selected by overload resolution can be
4291 determined without instantiating a class template definition, it is
4292 unspecified whether that instantiation actually takes place." */
4293 tree convtype = non_reference (TREE_TYPE (conv_fns));
4294 if ((flags & LOOKUP_NO_CONVERSION)
4295 && !WILDCARD_TYPE_P (convtype)
4296 && (CLASS_TYPE_P (to_nonref)
4297 != CLASS_TYPE_P (convtype)))
4298 continue;
4299
4300 /* If we are called to convert to a reference type, we are trying to
4301 find a direct binding, so don't even consider temporaries. If
4302 we don't find a direct binding, the caller will try again to
4303 look for a temporary binding. */
4304 if (TYPE_REF_P (totype))
4305 convflags |= LOOKUP_NO_TEMP_BIND;
4306
4307 old_candidates = candidates;
4308 add_candidates (TREE_VALUE (conv_fns), first_arg, NULL, totype,
4309 NULL_TREE, false,
4310 conversion_path, TYPE_BINFO (fromtype),
4311 flags, &candidates, complain);
4312
4313 for (cand = candidates; cand != old_candidates; cand = cand->next)
4314 {
4315 if (cand->viable == 0)
4316 /* Already rejected, don't change to -1. */
4317 continue;
4318
4319 tree rettype = TREE_TYPE (TREE_TYPE (cand->fn));
4320 conversion *ics
4321 = implicit_conversion (totype,
4322 rettype,
4323 0,
4324 /*c_cast_p=*/false, convflags,
4325 complain);
4326
4327 /* If LOOKUP_NO_TEMP_BIND isn't set, then this is
4328 copy-initialization. In that case, "The result of the
4329 call is then used to direct-initialize the object that is
4330 the destination of the copy-initialization." [dcl.init]
4331
4332 We represent this in the conversion sequence with an
4333 rvalue conversion, which means a constructor call. But
4334 don't add a second rvalue conversion if there's already
4335 one there. Which there really shouldn't be, but it's
4336 harmless since we'd add it here anyway. */
4337 if (ics && MAYBE_CLASS_TYPE_P (totype) && ics->kind != ck_rvalue
4338 && !(convflags & LOOKUP_NO_TEMP_BIND))
4339 ics = build_conv (ck_rvalue, totype, ics);
4340
4341 cand->second_conv = ics;
4342
4343 if (!ics)
4344 {
4345 cand->viable = 0;
4346 cand->reason = arg_conversion_rejection (NULL_TREE, -2,
4347 rettype, totype,
4348 EXPR_LOCATION (expr));
4349 }
4350 else if (TYPE_REF_P (totype) && !ics->rvaluedness_matches_p
4351 /* Limit this to non-templates for now (PR90546). */
4352 && !cand->template_decl
4353 && TREE_CODE (TREE_TYPE (totype)) != FUNCTION_TYPE)
4354 {
4355 /* If we are called to convert to a reference type, we are trying
4356 to find a direct binding per [over.match.ref], so rvaluedness
4357 must match for non-functions. */
4358 cand->viable = 0;
4359 }
4360 else if (DECL_NONCONVERTING_P (cand->fn)
4361 && ics->rank > cr_exact)
4362 {
4363 /* 13.3.1.5: For direct-initialization, those explicit
4364 conversion functions that are not hidden within S and
4365 yield type T or a type that can be converted to type T
4366 with a qualification conversion (4.4) are also candidate
4367 functions. */
4368 /* 13.3.1.6 doesn't have a parallel restriction, but it should;
4369 I've raised this issue with the committee. --jason 9/2011 */
4370 cand->viable = -1;
4371 cand->reason = explicit_conversion_rejection (rettype, totype);
4372 }
4373 else if (cand->viable == 1 && ics->bad_p)
4374 {
4375 cand->viable = -1;
4376 cand->reason
4377 = bad_arg_conversion_rejection (NULL_TREE, -2,
4378 rettype, totype,
4379 EXPR_LOCATION (expr));
4380 }
4381 else if (primary_template_specialization_p (cand->fn)
4382 && ics->rank > cr_exact)
4383 {
4384 /* 13.3.3.1.2: If the user-defined conversion is specified by
4385 a specialization of a conversion function template, the
4386 second standard conversion sequence shall have exact match
4387 rank. */
4388 cand->viable = -1;
4389 cand->reason = template_conversion_rejection (rettype, totype);
4390 }
4391 }
4392 }
4393
4394 candidates = splice_viable (candidates, false, &any_viable_p);
4395 if (!any_viable_p)
4396 {
4397 if (args)
4398 release_tree_vector (args);
4399 return NULL;
4400 }
4401
4402 cand = tourney (candidates, complain);
4403 if (cand == NULL)
4404 {
4405 if (complain & tf_error)
4406 {
4407 auto_diagnostic_group d;
4408 error_at (cp_expr_loc_or_input_loc (expr),
4409 "conversion from %qH to %qI is ambiguous",
4410 fromtype, totype);
4411 print_z_candidates (location_of (expr), candidates);
4412 }
4413
4414 cand = candidates; /* any one will do */
4415 cand->second_conv = build_ambiguous_conv (totype, expr);
4416 cand->second_conv->user_conv_p = true;
4417 if (!any_strictly_viable (candidates))
4418 cand->second_conv->bad_p = true;
4419 if (flags & LOOKUP_ONLYCONVERTING)
4420 cand->second_conv->need_temporary_p = true;
4421 /* If there are viable candidates, don't set ICS_BAD_FLAG; an
4422 ambiguous conversion is no worse than another user-defined
4423 conversion. */
4424
4425 return cand;
4426 }
4427
4428 tree convtype;
4429 if (!DECL_CONSTRUCTOR_P (cand->fn))
4430 convtype = non_reference (TREE_TYPE (TREE_TYPE (cand->fn)));
4431 else if (cand->second_conv->kind == ck_rvalue)
4432 /* DR 5: [in the first step of copy-initialization]...if the function
4433 is a constructor, the call initializes a temporary of the
4434 cv-unqualified version of the destination type. */
4435 convtype = cv_unqualified (totype);
4436 else
4437 convtype = totype;
4438 /* Build the user conversion sequence. */
4439 conv = build_conv
4440 (ck_user,
4441 convtype,
4442 build_identity_conv (TREE_TYPE (expr), expr));
4443 conv->cand = cand;
4444 if (cand->viable == -1)
4445 conv->bad_p = true;
4446
4447 /* Remember that this was a list-initialization. */
4448 if (flags & LOOKUP_NO_NARROWING)
4449 conv->check_narrowing = true;
4450
4451 /* Combine it with the second conversion sequence. */
4452 cand->second_conv = merge_conversion_sequences (conv,
4453 cand->second_conv);
4454
4455 return cand;
4456 }
4457
4458 /* Wrapper for above. */
4459
4460 tree
4461 build_user_type_conversion (tree totype, tree expr, int flags,
4462 tsubst_flags_t complain)
4463 {
4464 struct z_candidate *cand;
4465 tree ret;
4466
4467 auto_cond_timevar tv (TV_OVERLOAD);
4468 cand = build_user_type_conversion_1 (totype, expr, flags, complain);
4469
4470 if (cand)
4471 {
4472 if (cand->second_conv->kind == ck_ambig)
4473 ret = error_mark_node;
4474 else
4475 {
4476 expr = convert_like (cand->second_conv, expr, complain);
4477 ret = convert_from_reference (expr);
4478 }
4479 }
4480 else
4481 ret = NULL_TREE;
4482
4483 return ret;
4484 }
4485
4486 /* Give a helpful diagnostic when implicit_conversion fails. */
4487
4488 static void
4489 implicit_conversion_error (location_t loc, tree type, tree expr)
4490 {
4491 tsubst_flags_t complain = tf_warning_or_error;
4492
4493 /* If expr has unknown type, then it is an overloaded function.
4494 Call instantiate_type to get good error messages. */
4495 if (TREE_TYPE (expr) == unknown_type_node)
4496 instantiate_type (type, expr, complain);
4497 else if (invalid_nonstatic_memfn_p (loc, expr, complain))
4498 /* We gave an error. */;
4499 else if (BRACE_ENCLOSED_INITIALIZER_P (expr)
4500 && CONSTRUCTOR_IS_DESIGNATED_INIT (expr)
4501 && !CP_AGGREGATE_TYPE_P (type))
4502 error_at (loc, "designated initializers cannot be used with a "
4503 "non-aggregate type %qT", type);
4504 else
4505 {
4506 range_label_for_type_mismatch label (TREE_TYPE (expr), type);
4507 gcc_rich_location rich_loc (loc, &label);
4508 error_at (&rich_loc, "could not convert %qE from %qH to %qI",
4509 expr, TREE_TYPE (expr), type);
4510 }
4511 }
4512
4513 /* Worker for build_converted_constant_expr. */
4514
4515 static tree
4516 build_converted_constant_expr_internal (tree type, tree expr,
4517 int flags, tsubst_flags_t complain)
4518 {
4519 conversion *conv;
4520 void *p;
4521 tree t;
4522 location_t loc = cp_expr_loc_or_input_loc (expr);
4523
4524 if (error_operand_p (expr))
4525 return error_mark_node;
4526
4527 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4528 p = conversion_obstack_alloc (0);
4529
4530 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
4531 /*c_cast_p=*/false, flags, complain);
4532
4533 /* A converted constant expression of type T is an expression, implicitly
4534 converted to type T, where the converted expression is a constant
4535 expression and the implicit conversion sequence contains only
4536
4537 * user-defined conversions,
4538 * lvalue-to-rvalue conversions (7.1),
4539 * array-to-pointer conversions (7.2),
4540 * function-to-pointer conversions (7.3),
4541 * qualification conversions (7.5),
4542 * integral promotions (7.6),
4543 * integral conversions (7.8) other than narrowing conversions (11.6.4),
4544 * null pointer conversions (7.11) from std::nullptr_t,
4545 * null member pointer conversions (7.12) from std::nullptr_t, and
4546 * function pointer conversions (7.13),
4547
4548 and where the reference binding (if any) binds directly. */
4549
4550 for (conversion *c = conv;
4551 c && c->kind != ck_identity;
4552 c = next_conversion (c))
4553 {
4554 switch (c->kind)
4555 {
4556 /* A conversion function is OK. If it isn't constexpr, we'll
4557 complain later that the argument isn't constant. */
4558 case ck_user:
4559 /* List-initialization is OK. */
4560 case ck_aggr:
4561 /* The lvalue-to-rvalue conversion is OK. */
4562 case ck_rvalue:
4563 /* Array-to-pointer and function-to-pointer. */
4564 case ck_lvalue:
4565 /* Function pointer conversions. */
4566 case ck_fnptr:
4567 /* Qualification conversions. */
4568 case ck_qual:
4569 break;
4570
4571 case ck_ref_bind:
4572 if (c->need_temporary_p)
4573 {
4574 if (complain & tf_error)
4575 error_at (loc, "initializing %qH with %qI in converted "
4576 "constant expression does not bind directly",
4577 type, next_conversion (c)->type);
4578 conv = NULL;
4579 }
4580 break;
4581
4582 case ck_base:
4583 case ck_pmem:
4584 case ck_ptr:
4585 case ck_std:
4586 t = next_conversion (c)->type;
4587 if (INTEGRAL_OR_ENUMERATION_TYPE_P (t)
4588 && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4589 /* Integral promotion or conversion. */
4590 break;
4591 if (NULLPTR_TYPE_P (t))
4592 /* Conversion from nullptr to pointer or pointer-to-member. */
4593 break;
4594
4595 if (complain & tf_error)
4596 error_at (loc, "conversion from %qH to %qI in a "
4597 "converted constant expression", t, type);
4598 /* fall through. */
4599
4600 default:
4601 conv = NULL;
4602 break;
4603 }
4604 }
4605
4606 /* Avoid confusing convert_nontype_argument by introducing
4607 a redundant conversion to the same reference type. */
4608 if (conv && conv->kind == ck_ref_bind
4609 && REFERENCE_REF_P (expr))
4610 {
4611 tree ref = TREE_OPERAND (expr, 0);
4612 if (same_type_p (type, TREE_TYPE (ref)))
4613 return ref;
4614 }
4615
4616 if (conv)
4617 {
4618 /* Don't copy a class in a template. */
4619 if (CLASS_TYPE_P (type) && conv->kind == ck_rvalue
4620 && processing_template_decl)
4621 conv = next_conversion (conv);
4622
4623 /* Issuing conversion warnings for value-dependent expressions is
4624 likely too noisy. */
4625 warning_sentinel w (warn_conversion);
4626 conv->check_narrowing = true;
4627 conv->check_narrowing_const_only = true;
4628 expr = convert_like (conv, expr, complain);
4629 }
4630 else
4631 {
4632 if (complain & tf_error)
4633 implicit_conversion_error (loc, type, expr);
4634 expr = error_mark_node;
4635 }
4636
4637 /* Free all the conversions we allocated. */
4638 obstack_free (&conversion_obstack, p);
4639
4640 return expr;
4641 }
4642
4643 /* Subroutine of convert_nontype_argument.
4644
4645 EXPR is an expression used in a context that requires a converted
4646 constant-expression, such as a template non-type parameter. Do any
4647 necessary conversions (that are permitted for converted
4648 constant-expressions) to convert it to the desired type.
4649
4650 This function doesn't consider explicit conversion functions. If
4651 you mean to use "a contextually converted constant expression of type
4652 bool", use build_converted_constant_bool_expr.
4653
4654 If conversion is successful, returns the converted expression;
4655 otherwise, returns error_mark_node. */
4656
4657 tree
4658 build_converted_constant_expr (tree type, tree expr, tsubst_flags_t complain)
4659 {
4660 return build_converted_constant_expr_internal (type, expr, LOOKUP_IMPLICIT,
4661 complain);
4662 }
4663
4664 /* Used to create "a contextually converted constant expression of type
4665 bool". This differs from build_converted_constant_expr in that it
4666 also considers explicit conversion functions. */
4667
4668 tree
4669 build_converted_constant_bool_expr (tree expr, tsubst_flags_t complain)
4670 {
4671 return build_converted_constant_expr_internal (boolean_type_node, expr,
4672 LOOKUP_NORMAL, complain);
4673 }
4674
4675 /* Do any initial processing on the arguments to a function call. */
4676
4677 vec<tree, va_gc> *
4678 resolve_args (vec<tree, va_gc> *args, tsubst_flags_t complain)
4679 {
4680 unsigned int ix;
4681 tree arg;
4682
4683 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
4684 {
4685 if (error_operand_p (arg))
4686 return NULL;
4687 else if (VOID_TYPE_P (TREE_TYPE (arg)))
4688 {
4689 if (complain & tf_error)
4690 error_at (cp_expr_loc_or_input_loc (arg),
4691 "invalid use of void expression");
4692 return NULL;
4693 }
4694 else if (invalid_nonstatic_memfn_p (EXPR_LOCATION (arg), arg, complain))
4695 return NULL;
4696
4697 /* Force auto deduction now. Omit tf_warning to avoid redundant
4698 deprecated warning on deprecated-14.C. */
4699 if (!mark_single_function (arg, complain & ~tf_warning))
4700 return NULL;
4701 }
4702 return args;
4703 }
4704
4705 /* Perform overload resolution on FN, which is called with the ARGS.
4706
4707 Return the candidate function selected by overload resolution, or
4708 NULL if the event that overload resolution failed. In the case
4709 that overload resolution fails, *CANDIDATES will be the set of
4710 candidates considered, and ANY_VIABLE_P will be set to true or
4711 false to indicate whether or not any of the candidates were
4712 viable.
4713
4714 The ARGS should already have gone through RESOLVE_ARGS before this
4715 function is called. */
4716
4717 static struct z_candidate *
4718 perform_overload_resolution (tree fn,
4719 const vec<tree, va_gc> *args,
4720 struct z_candidate **candidates,
4721 bool *any_viable_p, tsubst_flags_t complain)
4722 {
4723 struct z_candidate *cand;
4724 tree explicit_targs;
4725 int template_only;
4726
4727 auto_cond_timevar tv (TV_OVERLOAD);
4728
4729 explicit_targs = NULL_TREE;
4730 template_only = 0;
4731
4732 *candidates = NULL;
4733 *any_viable_p = true;
4734
4735 /* Check FN. */
4736 gcc_assert (OVL_P (fn) || TREE_CODE (fn) == TEMPLATE_ID_EXPR);
4737
4738 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4739 {
4740 explicit_targs = TREE_OPERAND (fn, 1);
4741 fn = TREE_OPERAND (fn, 0);
4742 template_only = 1;
4743 }
4744
4745 /* Add the various candidate functions. */
4746 add_candidates (fn, NULL_TREE, args, NULL_TREE,
4747 explicit_targs, template_only,
4748 /*conversion_path=*/NULL_TREE,
4749 /*access_path=*/NULL_TREE,
4750 LOOKUP_NORMAL,
4751 candidates, complain);
4752
4753 *candidates = splice_viable (*candidates, false, any_viable_p);
4754 if (*any_viable_p)
4755 cand = tourney (*candidates, complain);
4756 else
4757 cand = NULL;
4758
4759 return cand;
4760 }
4761
4762 /* Print an error message about being unable to build a call to FN with
4763 ARGS. ANY_VIABLE_P indicates whether any candidate functions could
4764 be located; CANDIDATES is a possibly empty list of such
4765 functions. */
4766
4767 static void
4768 print_error_for_call_failure (tree fn, const vec<tree, va_gc> *args,
4769 struct z_candidate *candidates)
4770 {
4771 tree targs = NULL_TREE;
4772 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4773 {
4774 targs = TREE_OPERAND (fn, 1);
4775 fn = TREE_OPERAND (fn, 0);
4776 }
4777 tree name = OVL_NAME (fn);
4778 location_t loc = location_of (name);
4779 if (targs)
4780 name = lookup_template_function (name, targs);
4781
4782 auto_diagnostic_group d;
4783 if (!any_strictly_viable (candidates))
4784 error_at (loc, "no matching function for call to %<%D(%A)%>",
4785 name, build_tree_list_vec (args));
4786 else
4787 error_at (loc, "call of overloaded %<%D(%A)%> is ambiguous",
4788 name, build_tree_list_vec (args));
4789 if (candidates)
4790 print_z_candidates (loc, candidates);
4791 }
4792
4793 /* Perform overload resolution on the set of deduction guides DGUIDES
4794 using ARGS. Returns the selected deduction guide, or error_mark_node
4795 if overload resolution fails. */
4796
4797 tree
4798 perform_dguide_overload_resolution (tree dguides, const vec<tree, va_gc> *args,
4799 tsubst_flags_t complain)
4800 {
4801 z_candidate *candidates;
4802 bool any_viable_p;
4803 tree result;
4804
4805 gcc_assert (deduction_guide_p (OVL_FIRST (dguides)));
4806
4807 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4808 void *p = conversion_obstack_alloc (0);
4809
4810 z_candidate *cand = perform_overload_resolution (dguides, args, &candidates,
4811 &any_viable_p, complain);
4812 if (!cand)
4813 {
4814 if (complain & tf_error)
4815 print_error_for_call_failure (dguides, args, candidates);
4816 result = error_mark_node;
4817 }
4818 else
4819 result = cand->fn;
4820
4821 /* Free all the conversions we allocated. */
4822 obstack_free (&conversion_obstack, p);
4823
4824 return result;
4825 }
4826
4827 /* Return an expression for a call to FN (a namespace-scope function,
4828 or a static member function) with the ARGS. This may change
4829 ARGS. */
4830
4831 tree
4832 build_new_function_call (tree fn, vec<tree, va_gc> **args,
4833 tsubst_flags_t complain)
4834 {
4835 struct z_candidate *candidates, *cand;
4836 bool any_viable_p;
4837 void *p;
4838 tree result;
4839
4840 if (args != NULL && *args != NULL)
4841 {
4842 *args = resolve_args (*args, complain);
4843 if (*args == NULL)
4844 return error_mark_node;
4845 }
4846
4847 if (flag_tm)
4848 tm_malloc_replacement (fn);
4849
4850 /* Get the high-water mark for the CONVERSION_OBSTACK. */
4851 p = conversion_obstack_alloc (0);
4852
4853 cand = perform_overload_resolution (fn, *args, &candidates, &any_viable_p,
4854 complain);
4855
4856 if (!cand)
4857 {
4858 if (complain & tf_error)
4859 {
4860 // If there is a single (non-viable) function candidate,
4861 // let the error be diagnosed by cp_build_function_call_vec.
4862 if (!any_viable_p && candidates && ! candidates->next
4863 && (TREE_CODE (candidates->fn) == FUNCTION_DECL))
4864 return cp_build_function_call_vec (candidates->fn, args, complain);
4865
4866 // Otherwise, emit notes for non-viable candidates.
4867 print_error_for_call_failure (fn, *args, candidates);
4868 }
4869 result = error_mark_node;
4870 }
4871 else
4872 {
4873 result = build_over_call (cand, LOOKUP_NORMAL, complain);
4874 }
4875
4876 if (flag_coroutines
4877 && result
4878 && TREE_CODE (result) == CALL_EXPR
4879 && DECL_BUILT_IN_CLASS (TREE_OPERAND (CALL_EXPR_FN (result), 0))
4880 == BUILT_IN_NORMAL)
4881 result = coro_validate_builtin_call (result);
4882
4883 /* Free all the conversions we allocated. */
4884 obstack_free (&conversion_obstack, p);
4885
4886 return result;
4887 }
4888
4889 /* Build a call to a global operator new. FNNAME is the name of the
4890 operator (either "operator new" or "operator new[]") and ARGS are
4891 the arguments provided. This may change ARGS. *SIZE points to the
4892 total number of bytes required by the allocation, and is updated if
4893 that is changed here. *COOKIE_SIZE is non-NULL if a cookie should
4894 be used. If this function determines that no cookie should be
4895 used, after all, *COOKIE_SIZE is set to NULL_TREE. If SIZE_CHECK
4896 is not NULL_TREE, it is evaluated before calculating the final
4897 array size, and if it fails, the array size is replaced with
4898 (size_t)-1 (usually triggering a std::bad_alloc exception). If FN
4899 is non-NULL, it will be set, upon return, to the allocation
4900 function called. */
4901
4902 tree
4903 build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
4904 tree *size, tree *cookie_size,
4905 tree align_arg, tree size_check,
4906 tree *fn, tsubst_flags_t complain)
4907 {
4908 tree original_size = *size;
4909 tree fns;
4910 struct z_candidate *candidates;
4911 struct z_candidate *cand = NULL;
4912 bool any_viable_p;
4913
4914 if (fn)
4915 *fn = NULL_TREE;
4916 /* Set to (size_t)-1 if the size check fails. */
4917 if (size_check != NULL_TREE)
4918 {
4919 tree errval = TYPE_MAX_VALUE (sizetype);
4920 if (cxx_dialect >= cxx11 && flag_exceptions)
4921 errval = throw_bad_array_new_length ();
4922 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4923 original_size, errval);
4924 }
4925 vec_safe_insert (*args, 0, *size);
4926 *args = resolve_args (*args, complain);
4927 if (*args == NULL)
4928 return error_mark_node;
4929
4930 /* Based on:
4931
4932 [expr.new]
4933
4934 If this lookup fails to find the name, or if the allocated type
4935 is not a class type, the allocation function's name is looked
4936 up in the global scope.
4937
4938 we disregard block-scope declarations of "operator new". */
4939 fns = lookup_qualified_name (global_namespace, fnname);
4940
4941 if (align_arg)
4942 {
4943 vec<tree, va_gc>* align_args
4944 = vec_copy_and_insert (*args, align_arg, 1);
4945 cand = perform_overload_resolution (fns, align_args, &candidates,
4946 &any_viable_p, tf_none);
4947 if (cand)
4948 *args = align_args;
4949 /* If no aligned allocation function matches, try again without the
4950 alignment. */
4951 }
4952
4953 /* Figure out what function is being called. */
4954 if (!cand)
4955 cand = perform_overload_resolution (fns, *args, &candidates, &any_viable_p,
4956 complain);
4957
4958 /* If no suitable function could be found, issue an error message
4959 and give up. */
4960 if (!cand)
4961 {
4962 if (complain & tf_error)
4963 print_error_for_call_failure (fns, *args, candidates);
4964 return error_mark_node;
4965 }
4966
4967 /* If a cookie is required, add some extra space. Whether
4968 or not a cookie is required cannot be determined until
4969 after we know which function was called. */
4970 if (*cookie_size)
4971 {
4972 bool use_cookie = true;
4973 tree arg_types;
4974
4975 arg_types = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
4976 /* Skip the size_t parameter. */
4977 arg_types = TREE_CHAIN (arg_types);
4978 /* Check the remaining parameters (if any). */
4979 if (arg_types
4980 && TREE_CHAIN (arg_types) == void_list_node
4981 && same_type_p (TREE_VALUE (arg_types),
4982 ptr_type_node))
4983 use_cookie = false;
4984 /* If we need a cookie, adjust the number of bytes allocated. */
4985 if (use_cookie)
4986 {
4987 /* Update the total size. */
4988 *size = size_binop (PLUS_EXPR, original_size, *cookie_size);
4989 if (size_check)
4990 {
4991 /* Set to (size_t)-1 if the size check fails. */
4992 gcc_assert (size_check != NULL_TREE);
4993 *size = fold_build3 (COND_EXPR, sizetype, size_check,
4994 *size, TYPE_MAX_VALUE (sizetype));
4995 }
4996 /* Update the argument list to reflect the adjusted size. */
4997 (**args)[0] = *size;
4998 }
4999 else
5000 *cookie_size = NULL_TREE;
5001 }
5002
5003 /* Tell our caller which function we decided to call. */
5004 if (fn)
5005 *fn = cand->fn;
5006
5007 /* Build the CALL_EXPR. */
5008 tree ret = build_over_call (cand, LOOKUP_NORMAL, complain);
5009
5010 /* Set this flag for all callers of this function. In addition to
5011 new-expressions, this is called for allocating coroutine state; treat
5012 that as an implicit new-expression. */
5013 tree call = extract_call_expr (ret);
5014 if (TREE_CODE (call) == CALL_EXPR)
5015 CALL_FROM_NEW_OR_DELETE_P (call) = 1;
5016
5017 return ret;
5018 }
5019
5020 /* Build a new call to operator(). This may change ARGS. */
5021
5022 tree
5023 build_op_call (tree obj, vec<tree, va_gc> **args, tsubst_flags_t complain)
5024 {
5025 struct z_candidate *candidates = 0, *cand;
5026 tree fns, convs, first_mem_arg = NULL_TREE;
5027 bool any_viable_p;
5028 tree result = NULL_TREE;
5029 void *p;
5030
5031 auto_cond_timevar tv (TV_OVERLOAD);
5032
5033 obj = mark_lvalue_use (obj);
5034
5035 if (error_operand_p (obj))
5036 return error_mark_node;
5037
5038 tree type = TREE_TYPE (obj);
5039
5040 obj = prep_operand (obj);
5041
5042 if (TYPE_PTRMEMFUNC_P (type))
5043 {
5044 if (complain & tf_error)
5045 /* It's no good looking for an overloaded operator() on a
5046 pointer-to-member-function. */
5047 error ("pointer-to-member function %qE cannot be called without "
5048 "an object; consider using %<.*%> or %<->*%>", obj);
5049 return error_mark_node;
5050 }
5051
5052 if (TYPE_BINFO (type))
5053 {
5054 fns = lookup_fnfields (TYPE_BINFO (type), call_op_identifier, 1, complain);
5055 if (fns == error_mark_node)
5056 return error_mark_node;
5057 }
5058 else
5059 fns = NULL_TREE;
5060
5061 if (args != NULL && *args != NULL)
5062 {
5063 *args = resolve_args (*args, complain);
5064 if (*args == NULL)
5065 return error_mark_node;
5066 }
5067
5068 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5069 p = conversion_obstack_alloc (0);
5070
5071 if (fns)
5072 {
5073 first_mem_arg = obj;
5074
5075 add_candidates (BASELINK_FUNCTIONS (fns),
5076 first_mem_arg, *args, NULL_TREE,
5077 NULL_TREE, false,
5078 BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
5079 LOOKUP_NORMAL, &candidates, complain);
5080 }
5081
5082 convs = lookup_conversions (type);
5083
5084 for (; convs; convs = TREE_CHAIN (convs))
5085 {
5086 tree totype = TREE_TYPE (convs);
5087
5088 if (TYPE_PTRFN_P (totype)
5089 || TYPE_REFFN_P (totype)
5090 || (TYPE_REF_P (totype)
5091 && TYPE_PTRFN_P (TREE_TYPE (totype))))
5092 for (tree fn : ovl_range (TREE_VALUE (convs)))
5093 {
5094 if (DECL_NONCONVERTING_P (fn))
5095 continue;
5096
5097 if (TREE_CODE (fn) == TEMPLATE_DECL)
5098 add_template_conv_candidate
5099 (&candidates, fn, obj, *args, totype,
5100 /*access_path=*/NULL_TREE,
5101 /*conversion_path=*/NULL_TREE, complain);
5102 else
5103 add_conv_candidate (&candidates, fn, obj,
5104 *args, /*conversion_path=*/NULL_TREE,
5105 /*access_path=*/NULL_TREE, complain);
5106 }
5107 }
5108
5109 /* Be strict here because if we choose a bad conversion candidate, the
5110 errors we get won't mention the call context. */
5111 candidates = splice_viable (candidates, true, &any_viable_p);
5112 if (!any_viable_p)
5113 {
5114 if (complain & tf_error)
5115 {
5116 auto_diagnostic_group d;
5117 error ("no match for call to %<(%T) (%A)%>", TREE_TYPE (obj),
5118 build_tree_list_vec (*args));
5119 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
5120 }
5121 result = error_mark_node;
5122 }
5123 else
5124 {
5125 cand = tourney (candidates, complain);
5126 if (cand == 0)
5127 {
5128 if (complain & tf_error)
5129 {
5130 auto_diagnostic_group d;
5131 error ("call of %<(%T) (%A)%> is ambiguous",
5132 TREE_TYPE (obj), build_tree_list_vec (*args));
5133 print_z_candidates (location_of (TREE_TYPE (obj)), candidates);
5134 }
5135 result = error_mark_node;
5136 }
5137 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
5138 && DECL_OVERLOADED_OPERATOR_P (cand->fn)
5139 && DECL_OVERLOADED_OPERATOR_IS (cand->fn, CALL_EXPR))
5140 result = build_over_call (cand, LOOKUP_NORMAL, complain);
5141 else
5142 {
5143 if (TREE_CODE (cand->fn) == FUNCTION_DECL)
5144 obj = convert_like_with_context (cand->convs[0], obj, cand->fn,
5145 -1, complain);
5146 else
5147 {
5148 gcc_checking_assert (TYPE_P (cand->fn));
5149 obj = convert_like (cand->convs[0], obj, complain);
5150 }
5151 obj = convert_from_reference (obj);
5152 result = cp_build_function_call_vec (obj, args, complain);
5153 }
5154 }
5155
5156 /* Free all the conversions we allocated. */
5157 obstack_free (&conversion_obstack, p);
5158
5159 return result;
5160 }
5161
5162 /* Called by op_error to prepare format strings suitable for the error
5163 function. It concatenates a prefix (controlled by MATCH), ERRMSG,
5164 and a suffix (controlled by NTYPES). */
5165
5166 static const char *
5167 op_error_string (const char *errmsg, int ntypes, bool match)
5168 {
5169 const char *msg;
5170
5171 const char *msgp = concat (match ? G_("ambiguous overload for ")
5172 : G_("no match for "), errmsg, NULL);
5173
5174 if (ntypes == 3)
5175 msg = concat (msgp, G_(" (operand types are %qT, %qT, and %qT)"), NULL);
5176 else if (ntypes == 2)
5177 msg = concat (msgp, G_(" (operand types are %qT and %qT)"), NULL);
5178 else
5179 msg = concat (msgp, G_(" (operand type is %qT)"), NULL);
5180
5181 return msg;
5182 }
5183
5184 static void
5185 op_error (const op_location_t &loc,
5186 enum tree_code code, enum tree_code code2,
5187 tree arg1, tree arg2, tree arg3, bool match)
5188 {
5189 bool assop = code == MODIFY_EXPR;
5190 const char *opname = OVL_OP_INFO (assop, assop ? code2 : code)->name;
5191
5192 switch (code)
5193 {
5194 case COND_EXPR:
5195 if (flag_diagnostics_show_caret)
5196 error_at (loc, op_error_string (G_("ternary %<operator?:%>"),
5197 3, match),
5198 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
5199 else
5200 error_at (loc, op_error_string (G_("ternary %<operator?:%> "
5201 "in %<%E ? %E : %E%>"), 3, match),
5202 arg1, arg2, arg3,
5203 TREE_TYPE (arg1), TREE_TYPE (arg2), TREE_TYPE (arg3));
5204 break;
5205
5206 case POSTINCREMENT_EXPR:
5207 case POSTDECREMENT_EXPR:
5208 if (flag_diagnostics_show_caret)
5209 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
5210 opname, TREE_TYPE (arg1));
5211 else
5212 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E%s%>"),
5213 1, match),
5214 opname, arg1, opname, TREE_TYPE (arg1));
5215 break;
5216
5217 case ARRAY_REF:
5218 if (flag_diagnostics_show_caret)
5219 error_at (loc, op_error_string (G_("%<operator[]%>"), 2, match),
5220 TREE_TYPE (arg1), TREE_TYPE (arg2));
5221 else
5222 error_at (loc, op_error_string (G_("%<operator[]%> in %<%E[%E]%>"),
5223 2, match),
5224 arg1, arg2, TREE_TYPE (arg1), TREE_TYPE (arg2));
5225 break;
5226
5227 case REALPART_EXPR:
5228 case IMAGPART_EXPR:
5229 if (flag_diagnostics_show_caret)
5230 error_at (loc, op_error_string (G_("%qs"), 1, match),
5231 opname, TREE_TYPE (arg1));
5232 else
5233 error_at (loc, op_error_string (G_("%qs in %<%s %E%>"), 1, match),
5234 opname, opname, arg1, TREE_TYPE (arg1));
5235 break;
5236
5237 case CO_AWAIT_EXPR:
5238 if (flag_diagnostics_show_caret)
5239 error_at (loc, op_error_string (G_("%<operator %s%>"), 1, match),
5240 opname, TREE_TYPE (arg1));
5241 else
5242 error_at (loc, op_error_string (G_("%<operator %s%> in %<%s%E%>"),
5243 1, match),
5244 opname, opname, arg1, TREE_TYPE (arg1));
5245 break;
5246
5247 default:
5248 if (arg2)
5249 if (flag_diagnostics_show_caret)
5250 {
5251 binary_op_rich_location richloc (loc, arg1, arg2, true);
5252 error_at (&richloc,
5253 op_error_string (G_("%<operator%s%>"), 2, match),
5254 opname, TREE_TYPE (arg1), TREE_TYPE (arg2));
5255 }
5256 else
5257 error_at (loc, op_error_string (G_("%<operator%s%> in %<%E %s %E%>"),
5258 2, match),
5259 opname, arg1, opname, arg2,
5260 TREE_TYPE (arg1), TREE_TYPE (arg2));
5261 else
5262 if (flag_diagnostics_show_caret)
5263 error_at (loc, op_error_string (G_("%<operator%s%>"), 1, match),
5264 opname, TREE_TYPE (arg1));
5265 else
5266 error_at (loc, op_error_string (G_("%<operator%s%> in %<%s%E%>"),
5267 1, match),
5268 opname, opname, arg1, TREE_TYPE (arg1));
5269 break;
5270 }
5271 }
5272
5273 /* Return the implicit conversion sequence that could be used to
5274 convert E1 to E2 in [expr.cond]. */
5275
5276 static conversion *
5277 conditional_conversion (tree e1, tree e2, tsubst_flags_t complain)
5278 {
5279 tree t1 = non_reference (TREE_TYPE (e1));
5280 tree t2 = non_reference (TREE_TYPE (e2));
5281 conversion *conv;
5282 bool good_base;
5283
5284 /* [expr.cond]
5285
5286 If E2 is an lvalue: E1 can be converted to match E2 if E1 can be
5287 implicitly converted (clause _conv_) to the type "lvalue reference to
5288 T2", subject to the constraint that in the conversion the
5289 reference must bind directly (_dcl.init.ref_) to an lvalue.
5290
5291 If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
5292 implicitly converted to the type "rvalue reference to T2", subject to
5293 the constraint that the reference must bind directly. */
5294 if (glvalue_p (e2))
5295 {
5296 tree rtype = cp_build_reference_type (t2, !lvalue_p (e2));
5297 conv = implicit_conversion (rtype,
5298 t1,
5299 e1,
5300 /*c_cast_p=*/false,
5301 LOOKUP_NO_TEMP_BIND|LOOKUP_NO_RVAL_BIND
5302 |LOOKUP_ONLYCONVERTING,
5303 complain);
5304 if (conv && !conv->bad_p)
5305 return conv;
5306 }
5307
5308 /* If E2 is a prvalue or if neither of the conversions above can be done
5309 and at least one of the operands has (possibly cv-qualified) class
5310 type: */
5311 if (!CLASS_TYPE_P (t1) && !CLASS_TYPE_P (t2))
5312 return NULL;
5313
5314 /* [expr.cond]
5315
5316 If E1 and E2 have class type, and the underlying class types are
5317 the same or one is a base class of the other: E1 can be converted
5318 to match E2 if the class of T2 is the same type as, or a base
5319 class of, the class of T1, and the cv-qualification of T2 is the
5320 same cv-qualification as, or a greater cv-qualification than, the
5321 cv-qualification of T1. If the conversion is applied, E1 is
5322 changed to an rvalue of type T2 that still refers to the original
5323 source class object (or the appropriate subobject thereof). */
5324 if (CLASS_TYPE_P (t1) && CLASS_TYPE_P (t2)
5325 && ((good_base = DERIVED_FROM_P (t2, t1)) || DERIVED_FROM_P (t1, t2)))
5326 {
5327 if (good_base && at_least_as_qualified_p (t2, t1))
5328 {
5329 conv = build_identity_conv (t1, e1);
5330 if (!same_type_p (TYPE_MAIN_VARIANT (t1),
5331 TYPE_MAIN_VARIANT (t2)))
5332 conv = build_conv (ck_base, t2, conv);
5333 else
5334 conv = build_conv (ck_rvalue, t2, conv);
5335 return conv;
5336 }
5337 else
5338 return NULL;
5339 }
5340 else
5341 /* [expr.cond]
5342
5343 Otherwise: E1 can be converted to match E2 if E1 can be implicitly
5344 converted to the type that expression E2 would have if E2 were
5345 converted to an rvalue (or the type it has, if E2 is an rvalue). */
5346 return implicit_conversion (t2, t1, e1, /*c_cast_p=*/false,
5347 LOOKUP_IMPLICIT, complain);
5348 }
5349
5350 /* Implement [expr.cond]. ARG1, ARG2, and ARG3 are the three
5351 arguments to the conditional expression. */
5352
5353 tree
5354 build_conditional_expr (const op_location_t &loc,
5355 tree arg1, tree arg2, tree arg3,
5356 tsubst_flags_t complain)
5357 {
5358 tree arg2_type;
5359 tree arg3_type;
5360 tree result = NULL_TREE;
5361 tree result_type = NULL_TREE;
5362 tree semantic_result_type = NULL_TREE;
5363 bool is_glvalue = true;
5364 struct z_candidate *candidates = 0;
5365 struct z_candidate *cand;
5366 void *p;
5367 tree orig_arg2, orig_arg3;
5368
5369 auto_cond_timevar tv (TV_OVERLOAD);
5370
5371 /* As a G++ extension, the second argument to the conditional can be
5372 omitted. (So that `a ? : c' is roughly equivalent to `a ? a :
5373 c'.) If the second operand is omitted, make sure it is
5374 calculated only once. */
5375 if (!arg2)
5376 {
5377 if (complain & tf_error)
5378 pedwarn (loc, OPT_Wpedantic,
5379 "ISO C++ forbids omitting the middle term of "
5380 "a %<?:%> expression");
5381
5382 if ((complain & tf_warning) && !truth_value_p (TREE_CODE (arg1)))
5383 warn_for_omitted_condop (loc, arg1);
5384
5385 /* Make sure that lvalues remain lvalues. See g++.oliva/ext1.C. */
5386 if (glvalue_p (arg1))
5387 {
5388 arg1 = cp_stabilize_reference (arg1);
5389 arg2 = arg1 = prevent_lifetime_extension (arg1);
5390 }
5391 else if (TREE_CODE (arg1) == TARGET_EXPR)
5392 /* arg1 can't be a prvalue result of the conditional
5393 expression, since it needs to be materialized for the
5394 conversion to bool, so treat it as an xvalue in arg2. */
5395 arg2 = move (TARGET_EXPR_SLOT (arg1));
5396 else if (TREE_CODE (arg1) == EXCESS_PRECISION_EXPR)
5397 arg2 = arg1 = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (arg1),
5398 cp_save_expr (TREE_OPERAND (arg1, 0)));
5399 else
5400 arg2 = arg1 = cp_save_expr (arg1);
5401 }
5402
5403 /* If something has already gone wrong, just pass that fact up the
5404 tree. */
5405 if (error_operand_p (arg1)
5406 || error_operand_p (arg2)
5407 || error_operand_p (arg3))
5408 return error_mark_node;
5409
5410 orig_arg2 = arg2;
5411 orig_arg3 = arg3;
5412
5413 if (gnu_vector_type_p (TREE_TYPE (arg1))
5414 && VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg1)))
5415 {
5416 tree arg1_type = TREE_TYPE (arg1);
5417
5418 /* If arg1 is another cond_expr choosing between -1 and 0,
5419 then we can use its comparison. It may help to avoid
5420 additional comparison, produce more accurate diagnostics
5421 and enables folding. */
5422 if (TREE_CODE (arg1) == VEC_COND_EXPR
5423 && integer_minus_onep (TREE_OPERAND (arg1, 1))
5424 && integer_zerop (TREE_OPERAND (arg1, 2)))
5425 arg1 = TREE_OPERAND (arg1, 0);
5426
5427 arg1 = force_rvalue (arg1, complain);
5428 arg2 = force_rvalue (arg2, complain);
5429 arg3 = force_rvalue (arg3, complain);
5430
5431 /* force_rvalue can return error_mark on valid arguments. */
5432 if (error_operand_p (arg1)
5433 || error_operand_p (arg2)
5434 || error_operand_p (arg3))
5435 return error_mark_node;
5436
5437 arg2_type = TREE_TYPE (arg2);
5438 arg3_type = TREE_TYPE (arg3);
5439
5440 if (!VECTOR_TYPE_P (arg2_type)
5441 && !VECTOR_TYPE_P (arg3_type))
5442 {
5443 /* Rely on the error messages of the scalar version. */
5444 tree scal = build_conditional_expr (loc, integer_one_node,
5445 orig_arg2, orig_arg3, complain);
5446 if (scal == error_mark_node)
5447 return error_mark_node;
5448 tree stype = TREE_TYPE (scal);
5449 tree ctype = TREE_TYPE (arg1_type);
5450 if (TYPE_SIZE (stype) != TYPE_SIZE (ctype)
5451 || (!INTEGRAL_TYPE_P (stype) && !SCALAR_FLOAT_TYPE_P (stype)))
5452 {
5453 if (complain & tf_error)
5454 error_at (loc, "inferred scalar type %qT is not an integer or "
5455 "floating-point type of the same size as %qT", stype,
5456 COMPARISON_CLASS_P (arg1)
5457 ? TREE_TYPE (TREE_TYPE (TREE_OPERAND (arg1, 0)))
5458 : ctype);
5459 return error_mark_node;
5460 }
5461
5462 tree vtype = build_opaque_vector_type (stype,
5463 TYPE_VECTOR_SUBPARTS (arg1_type));
5464 /* We could pass complain & tf_warning to unsafe_conversion_p,
5465 but the warnings (like Wsign-conversion) have already been
5466 given by the scalar build_conditional_expr_1. We still check
5467 unsafe_conversion_p to forbid truncating long long -> float. */
5468 if (unsafe_conversion_p (stype, arg2, NULL_TREE, false))
5469 {
5470 if (complain & tf_error)
5471 error_at (loc, "conversion of scalar %qH to vector %qI "
5472 "involves truncation", arg2_type, vtype);
5473 return error_mark_node;
5474 }
5475 if (unsafe_conversion_p (stype, arg3, NULL_TREE, false))
5476 {
5477 if (complain & tf_error)
5478 error_at (loc, "conversion of scalar %qH to vector %qI "
5479 "involves truncation", arg3_type, vtype);
5480 return error_mark_node;
5481 }
5482
5483 arg2 = cp_convert (stype, arg2, complain);
5484 arg2 = save_expr (arg2);
5485 arg2 = build_vector_from_val (vtype, arg2);
5486 arg2_type = vtype;
5487 arg3 = cp_convert (stype, arg3, complain);
5488 arg3 = save_expr (arg3);
5489 arg3 = build_vector_from_val (vtype, arg3);
5490 arg3_type = vtype;
5491 }
5492
5493 if ((gnu_vector_type_p (arg2_type) && !VECTOR_TYPE_P (arg3_type))
5494 || (gnu_vector_type_p (arg3_type) && !VECTOR_TYPE_P (arg2_type)))
5495 {
5496 enum stv_conv convert_flag =
5497 scalar_to_vector (loc, VEC_COND_EXPR, arg2, arg3,
5498 complain & tf_error);
5499
5500 switch (convert_flag)
5501 {
5502 case stv_error:
5503 return error_mark_node;
5504 case stv_firstarg:
5505 {
5506 arg2 = save_expr (arg2);
5507 arg2 = convert (TREE_TYPE (arg3_type), arg2);
5508 arg2 = build_vector_from_val (arg3_type, arg2);
5509 arg2_type = TREE_TYPE (arg2);
5510 break;
5511 }
5512 case stv_secondarg:
5513 {
5514 arg3 = save_expr (arg3);
5515 arg3 = convert (TREE_TYPE (arg2_type), arg3);
5516 arg3 = build_vector_from_val (arg2_type, arg3);
5517 arg3_type = TREE_TYPE (arg3);
5518 break;
5519 }
5520 default:
5521 break;
5522 }
5523 }
5524
5525 if (!gnu_vector_type_p (arg2_type)
5526 || !gnu_vector_type_p (arg3_type)
5527 || !same_type_p (arg2_type, arg3_type)
5528 || maybe_ne (TYPE_VECTOR_SUBPARTS (arg1_type),
5529 TYPE_VECTOR_SUBPARTS (arg2_type))
5530 || TYPE_SIZE (arg1_type) != TYPE_SIZE (arg2_type))
5531 {
5532 if (complain & tf_error)
5533 error_at (loc,
5534 "incompatible vector types in conditional expression: "
5535 "%qT, %qT and %qT", TREE_TYPE (arg1),
5536 TREE_TYPE (orig_arg2), TREE_TYPE (orig_arg3));
5537 return error_mark_node;
5538 }
5539
5540 if (!COMPARISON_CLASS_P (arg1))
5541 {
5542 tree cmp_type = truth_type_for (arg1_type);
5543 arg1 = build2 (NE_EXPR, cmp_type, arg1, build_zero_cst (arg1_type));
5544 }
5545 return build3_loc (loc, VEC_COND_EXPR, arg2_type, arg1, arg2, arg3);
5546 }
5547
5548 /* [expr.cond]
5549
5550 The first expression is implicitly converted to bool (clause
5551 _conv_). */
5552 arg1 = perform_implicit_conversion_flags (boolean_type_node, arg1, complain,
5553 LOOKUP_NORMAL);
5554 if (error_operand_p (arg1))
5555 return error_mark_node;
5556
5557 arg2_type = unlowered_expr_type (arg2);
5558 arg3_type = unlowered_expr_type (arg3);
5559
5560 if ((TREE_CODE (arg2) == EXCESS_PRECISION_EXPR
5561 || TREE_CODE (arg3) == EXCESS_PRECISION_EXPR)
5562 && (TREE_CODE (arg2_type) == INTEGER_TYPE
5563 || TREE_CODE (arg2_type) == REAL_TYPE
5564 || TREE_CODE (arg2_type) == COMPLEX_TYPE)
5565 && (TREE_CODE (arg3_type) == INTEGER_TYPE
5566 || TREE_CODE (arg3_type) == REAL_TYPE
5567 || TREE_CODE (arg3_type) == COMPLEX_TYPE))
5568 {
5569 semantic_result_type
5570 = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
5571 if (semantic_result_type == error_mark_node)
5572 {
5573 tree t1 = arg2_type;
5574 tree t2 = arg3_type;
5575 if (TREE_CODE (t1) == COMPLEX_TYPE)
5576 t1 = TREE_TYPE (t1);
5577 if (TREE_CODE (t2) == COMPLEX_TYPE)
5578 t2 = TREE_TYPE (t2);
5579 gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
5580 && TREE_CODE (t2) == REAL_TYPE
5581 && (extended_float_type_p (t1)
5582 || extended_float_type_p (t2))
5583 && cp_compare_floating_point_conversion_ranks
5584 (t1, t2) == 3);
5585 if (complain & tf_error)
5586 error_at (loc, "operands to %<?:%> of types %qT and %qT "
5587 "have unordered conversion rank",
5588 arg2_type, arg3_type);
5589 return error_mark_node;
5590 }
5591 if (TREE_CODE (arg2) == EXCESS_PRECISION_EXPR)
5592 {
5593 arg2 = TREE_OPERAND (arg2, 0);
5594 arg2_type = TREE_TYPE (arg2);
5595 }
5596 if (TREE_CODE (arg3) == EXCESS_PRECISION_EXPR)
5597 {
5598 arg3 = TREE_OPERAND (arg3, 0);
5599 arg3_type = TREE_TYPE (arg3);
5600 }
5601 }
5602
5603 /* [expr.cond]
5604
5605 If either the second or the third operand has type (possibly
5606 cv-qualified) void, then the lvalue-to-rvalue (_conv.lval_),
5607 array-to-pointer (_conv.array_), and function-to-pointer
5608 (_conv.func_) standard conversions are performed on the second
5609 and third operands. */
5610 if (VOID_TYPE_P (arg2_type) || VOID_TYPE_P (arg3_type))
5611 {
5612 /* 'void' won't help in resolving an overloaded expression on the
5613 other side, so require it to resolve by itself. */
5614 if (arg2_type == unknown_type_node)
5615 {
5616 arg2 = resolve_nondeduced_context_or_error (arg2, complain);
5617 arg2_type = TREE_TYPE (arg2);
5618 }
5619 if (arg3_type == unknown_type_node)
5620 {
5621 arg3 = resolve_nondeduced_context_or_error (arg3, complain);
5622 arg3_type = TREE_TYPE (arg3);
5623 }
5624
5625 /* [expr.cond]
5626
5627 One of the following shall hold:
5628
5629 --The second or the third operand (but not both) is a
5630 throw-expression (_except.throw_); the result is of the type
5631 and value category of the other.
5632
5633 --Both the second and the third operands have type void; the
5634 result is of type void and is a prvalue. */
5635 if (TREE_CODE (arg2) == THROW_EXPR
5636 && TREE_CODE (arg3) != THROW_EXPR)
5637 {
5638 result_type = arg3_type;
5639 is_glvalue = glvalue_p (arg3);
5640 }
5641 else if (TREE_CODE (arg2) != THROW_EXPR
5642 && TREE_CODE (arg3) == THROW_EXPR)
5643 {
5644 result_type = arg2_type;
5645 is_glvalue = glvalue_p (arg2);
5646 }
5647 else if (VOID_TYPE_P (arg2_type) && VOID_TYPE_P (arg3_type))
5648 {
5649 result_type = void_type_node;
5650 is_glvalue = false;
5651 }
5652 else
5653 {
5654 if (complain & tf_error)
5655 {
5656 if (VOID_TYPE_P (arg2_type))
5657 error_at (cp_expr_loc_or_loc (arg3, loc),
5658 "second operand to the conditional operator "
5659 "is of type %<void%>, but the third operand is "
5660 "neither a throw-expression nor of type %<void%>");
5661 else
5662 error_at (cp_expr_loc_or_loc (arg2, loc),
5663 "third operand to the conditional operator "
5664 "is of type %<void%>, but the second operand is "
5665 "neither a throw-expression nor of type %<void%>");
5666 }
5667 return error_mark_node;
5668 }
5669
5670 goto valid_operands;
5671 }
5672 /* [expr.cond]
5673
5674 Otherwise, if the second and third operand have different types,
5675 and either has (possibly cv-qualified) class type, or if both are
5676 glvalues of the same value category and the same type except for
5677 cv-qualification, an attempt is made to convert each of those operands
5678 to the type of the other. */
5679 else if (!same_type_p (arg2_type, arg3_type)
5680 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)
5681 || (same_type_ignoring_top_level_qualifiers_p (arg2_type,
5682 arg3_type)
5683 && glvalue_p (arg2) && glvalue_p (arg3)
5684 && lvalue_p (arg2) == lvalue_p (arg3))))
5685 {
5686 conversion *conv2;
5687 conversion *conv3;
5688 bool converted = false;
5689
5690 /* Get the high-water mark for the CONVERSION_OBSTACK. */
5691 p = conversion_obstack_alloc (0);
5692
5693 conv2 = conditional_conversion (arg2, arg3, complain);
5694 conv3 = conditional_conversion (arg3, arg2, complain);
5695
5696 /* [expr.cond]
5697
5698 If both can be converted, or one can be converted but the
5699 conversion is ambiguous, the program is ill-formed. If
5700 neither can be converted, the operands are left unchanged and
5701 further checking is performed as described below. If exactly
5702 one conversion is possible, that conversion is applied to the
5703 chosen operand and the converted operand is used in place of
5704 the original operand for the remainder of this section. */
5705 if ((conv2 && !conv2->bad_p
5706 && conv3 && !conv3->bad_p)
5707 || (conv2 && conv2->kind == ck_ambig)
5708 || (conv3 && conv3->kind == ck_ambig))
5709 {
5710 if (complain & tf_error)
5711 {
5712 error_at (loc, "operands to %<?:%> have different types "
5713 "%qT and %qT",
5714 arg2_type, arg3_type);
5715 if (conv2 && !conv2->bad_p && conv3 && !conv3->bad_p)
5716 inform (loc, " and each type can be converted to the other");
5717 else if (conv2 && conv2->kind == ck_ambig)
5718 convert_like (conv2, arg2, complain);
5719 else
5720 convert_like (conv3, arg3, complain);
5721 }
5722 result = error_mark_node;
5723 }
5724 else if (conv2 && !conv2->bad_p)
5725 {
5726 arg2 = convert_like (conv2, arg2, complain);
5727 arg2 = convert_from_reference (arg2);
5728 arg2_type = TREE_TYPE (arg2);
5729 /* Even if CONV2 is a valid conversion, the result of the
5730 conversion may be invalid. For example, if ARG3 has type
5731 "volatile X", and X does not have a copy constructor
5732 accepting a "volatile X&", then even if ARG2 can be
5733 converted to X, the conversion will fail. */
5734 if (error_operand_p (arg2))
5735 result = error_mark_node;
5736 converted = true;
5737 }
5738 else if (conv3 && !conv3->bad_p)
5739 {
5740 arg3 = convert_like (conv3, arg3, complain);
5741 arg3 = convert_from_reference (arg3);
5742 arg3_type = TREE_TYPE (arg3);
5743 if (error_operand_p (arg3))
5744 result = error_mark_node;
5745 converted = true;
5746 }
5747
5748 /* Free all the conversions we allocated. */
5749 obstack_free (&conversion_obstack, p);
5750
5751 if (result)
5752 return result;
5753
5754 /* If, after the conversion, both operands have class type,
5755 treat the cv-qualification of both operands as if it were the
5756 union of the cv-qualification of the operands.
5757
5758 The standard is not clear about what to do in this
5759 circumstance. For example, if the first operand has type
5760 "const X" and the second operand has a user-defined
5761 conversion to "volatile X", what is the type of the second
5762 operand after this step? Making it be "const X" (matching
5763 the first operand) seems wrong, as that discards the
5764 qualification without actually performing a copy. Leaving it
5765 as "volatile X" seems wrong as that will result in the
5766 conditional expression failing altogether, even though,
5767 according to this step, the one operand could be converted to
5768 the type of the other. */
5769 if (converted
5770 && CLASS_TYPE_P (arg2_type)
5771 && cp_type_quals (arg2_type) != cp_type_quals (arg3_type))
5772 arg2_type = arg3_type =
5773 cp_build_qualified_type (arg2_type,
5774 cp_type_quals (arg2_type)
5775 | cp_type_quals (arg3_type));
5776 }
5777
5778 /* [expr.cond]
5779
5780 If the second and third operands are glvalues of the same value
5781 category and have the same type, the result is of that type and
5782 value category. */
5783 if (((lvalue_p (arg2) && lvalue_p (arg3))
5784 || (xvalue_p (arg2) && xvalue_p (arg3)))
5785 && same_type_p (arg2_type, arg3_type))
5786 {
5787 result_type = arg2_type;
5788 goto valid_operands;
5789 }
5790
5791 /* [expr.cond]
5792
5793 Otherwise, the result is an rvalue. If the second and third
5794 operand do not have the same type, and either has (possibly
5795 cv-qualified) class type, overload resolution is used to
5796 determine the conversions (if any) to be applied to the operands
5797 (_over.match.oper_, _over.built_). */
5798 is_glvalue = false;
5799 if (!same_type_p (arg2_type, arg3_type)
5800 && (CLASS_TYPE_P (arg2_type) || CLASS_TYPE_P (arg3_type)))
5801 {
5802 releasing_vec args;
5803 conversion *conv;
5804 bool any_viable_p;
5805
5806 /* Rearrange the arguments so that add_builtin_candidate only has
5807 to know about two args. In build_builtin_candidate, the
5808 arguments are unscrambled. */
5809 args->quick_push (arg2);
5810 args->quick_push (arg3);
5811 args->quick_push (arg1);
5812 add_builtin_candidates (&candidates,
5813 COND_EXPR,
5814 NOP_EXPR,
5815 ovl_op_identifier (false, COND_EXPR),
5816 args,
5817 LOOKUP_NORMAL, complain);
5818
5819 /* [expr.cond]
5820
5821 If the overload resolution fails, the program is
5822 ill-formed. */
5823 candidates = splice_viable (candidates, false, &any_viable_p);
5824 if (!any_viable_p)
5825 {
5826 if (complain & tf_error)
5827 error_at (loc, "operands to %<?:%> have different types %qT and %qT",
5828 arg2_type, arg3_type);
5829 return error_mark_node;
5830 }
5831 cand = tourney (candidates, complain);
5832 if (!cand)
5833 {
5834 if (complain & tf_error)
5835 {
5836 auto_diagnostic_group d;
5837 op_error (loc, COND_EXPR, NOP_EXPR, arg1, arg2, arg3, FALSE);
5838 print_z_candidates (loc, candidates);
5839 }
5840 return error_mark_node;
5841 }
5842
5843 /* [expr.cond]
5844
5845 Otherwise, the conversions thus determined are applied, and
5846 the converted operands are used in place of the original
5847 operands for the remainder of this section. */
5848 conv = cand->convs[0];
5849 arg1 = convert_like (conv, arg1, complain);
5850 conv = cand->convs[1];
5851 arg2 = convert_like (conv, arg2, complain);
5852 arg2_type = TREE_TYPE (arg2);
5853 conv = cand->convs[2];
5854 arg3 = convert_like (conv, arg3, complain);
5855 arg3_type = TREE_TYPE (arg3);
5856 }
5857
5858 /* [expr.cond]
5859
5860 Lvalue-to-rvalue (_conv.lval_), array-to-pointer (_conv.array_),
5861 and function-to-pointer (_conv.func_) standard conversions are
5862 performed on the second and third operands.
5863
5864 We need to force the lvalue-to-rvalue conversion here for class types,
5865 so we get TARGET_EXPRs; trying to deal with a COND_EXPR of class rvalues
5866 that isn't wrapped with a TARGET_EXPR plays havoc with exception
5867 regions. */
5868
5869 arg2 = force_rvalue (arg2, complain);
5870 if (!CLASS_TYPE_P (arg2_type))
5871 arg2_type = TREE_TYPE (arg2);
5872
5873 arg3 = force_rvalue (arg3, complain);
5874 if (!CLASS_TYPE_P (arg3_type))
5875 arg3_type = TREE_TYPE (arg3);
5876
5877 if (arg2 == error_mark_node || arg3 == error_mark_node)
5878 return error_mark_node;
5879
5880 /* [expr.cond]
5881
5882 After those conversions, one of the following shall hold:
5883
5884 --The second and third operands have the same type; the result is of
5885 that type. */
5886 if (same_type_p (arg2_type, arg3_type))
5887 result_type = arg2_type;
5888 /* [expr.cond]
5889
5890 --The second and third operands have arithmetic or enumeration
5891 type; the usual arithmetic conversions are performed to bring
5892 them to a common type, and the result is of that type. */
5893 else if ((ARITHMETIC_TYPE_P (arg2_type)
5894 || UNSCOPED_ENUM_P (arg2_type))
5895 && (ARITHMETIC_TYPE_P (arg3_type)
5896 || UNSCOPED_ENUM_P (arg3_type)))
5897 {
5898 /* A conditional expression between a floating-point
5899 type and an integer type should convert the integer type to
5900 the evaluation format of the floating-point type, with
5901 possible excess precision. */
5902 tree eptype2 = arg2_type;
5903 tree eptype3 = arg3_type;
5904 tree eptype;
5905 if (ANY_INTEGRAL_TYPE_P (arg2_type)
5906 && (eptype = excess_precision_type (arg3_type)) != NULL_TREE)
5907 {
5908 eptype3 = eptype;
5909 if (!semantic_result_type)
5910 semantic_result_type
5911 = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
5912 }
5913 else if (ANY_INTEGRAL_TYPE_P (arg3_type)
5914 && (eptype = excess_precision_type (arg2_type)) != NULL_TREE)
5915 {
5916 eptype2 = eptype;
5917 if (!semantic_result_type)
5918 semantic_result_type
5919 = type_after_usual_arithmetic_conversions (arg2_type, arg3_type);
5920 }
5921 result_type = type_after_usual_arithmetic_conversions (eptype2,
5922 eptype3);
5923 if (result_type == error_mark_node)
5924 {
5925 tree t1 = eptype2;
5926 tree t2 = eptype3;
5927 if (TREE_CODE (t1) == COMPLEX_TYPE)
5928 t1 = TREE_TYPE (t1);
5929 if (TREE_CODE (t2) == COMPLEX_TYPE)
5930 t2 = TREE_TYPE (t2);
5931 gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
5932 && TREE_CODE (t2) == REAL_TYPE
5933 && (extended_float_type_p (t1)
5934 || extended_float_type_p (t2))
5935 && cp_compare_floating_point_conversion_ranks
5936 (t1, t2) == 3);
5937 if (complain & tf_error)
5938 error_at (loc, "operands to %<?:%> of types %qT and %qT "
5939 "have unordered conversion rank",
5940 eptype2, eptype3);
5941 return error_mark_node;
5942 }
5943 if (semantic_result_type == error_mark_node)
5944 {
5945 tree t1 = arg2_type;
5946 tree t2 = arg3_type;
5947 if (TREE_CODE (t1) == COMPLEX_TYPE)
5948 t1 = TREE_TYPE (t1);
5949 if (TREE_CODE (t2) == COMPLEX_TYPE)
5950 t2 = TREE_TYPE (t2);
5951 gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
5952 && TREE_CODE (t2) == REAL_TYPE
5953 && (extended_float_type_p (t1)
5954 || extended_float_type_p (t2))
5955 && cp_compare_floating_point_conversion_ranks
5956 (t1, t2) == 3);
5957 if (complain & tf_error)
5958 error_at (loc, "operands to %<?:%> of types %qT and %qT "
5959 "have unordered conversion rank",
5960 arg2_type, arg3_type);
5961 return error_mark_node;
5962 }
5963
5964 if (complain & tf_warning)
5965 do_warn_double_promotion (result_type, arg2_type, arg3_type,
5966 "implicit conversion from %qH to %qI to "
5967 "match other result of conditional",
5968 loc);
5969
5970 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE
5971 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)
5972 {
5973 tree stripped_orig_arg2 = tree_strip_any_location_wrapper (orig_arg2);
5974 tree stripped_orig_arg3 = tree_strip_any_location_wrapper (orig_arg3);
5975 if (TREE_CODE (stripped_orig_arg2) == CONST_DECL
5976 && TREE_CODE (stripped_orig_arg3) == CONST_DECL
5977 && (DECL_CONTEXT (stripped_orig_arg2)
5978 == DECL_CONTEXT (stripped_orig_arg3)))
5979 /* Two enumerators from the same enumeration can have different
5980 types when the enumeration is still being defined. */;
5981 else if (complain & tf_warning)
5982 warning_at (loc, OPT_Wenum_compare, "enumerated mismatch "
5983 "in conditional expression: %qT vs %qT",
5984 arg2_type, arg3_type);
5985 }
5986 else if ((complain & tf_warning)
5987 && warn_deprecated_enum_float_conv
5988 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
5989 && TREE_CODE (arg3_type) == REAL_TYPE)
5990 || (TREE_CODE (arg2_type) == REAL_TYPE
5991 && TREE_CODE (arg3_type) == ENUMERAL_TYPE)))
5992 {
5993 if (TREE_CODE (arg2_type) == ENUMERAL_TYPE)
5994 warning_at (loc, OPT_Wdeprecated_enum_float_conversion,
5995 "conditional expression between enumeration type "
5996 "%qT and floating-point type %qT is deprecated",
5997 arg2_type, arg3_type);
5998 else
5999 warning_at (loc, OPT_Wdeprecated_enum_float_conversion,
6000 "conditional expression between floating-point "
6001 "type %qT and enumeration type %qT is deprecated",
6002 arg2_type, arg3_type);
6003 }
6004 else if ((extra_warnings || warn_enum_conversion)
6005 && ((TREE_CODE (arg2_type) == ENUMERAL_TYPE
6006 && !same_type_p (arg3_type, type_promotes_to (arg2_type)))
6007 || (TREE_CODE (arg3_type) == ENUMERAL_TYPE
6008 && !same_type_p (arg2_type,
6009 type_promotes_to (arg3_type)))))
6010 {
6011 if (complain & tf_warning)
6012 {
6013 enum opt_code opt = (warn_enum_conversion
6014 ? OPT_Wenum_conversion
6015 : OPT_Wextra);
6016 warning_at (loc, opt, "enumerated and "
6017 "non-enumerated type in conditional expression");
6018 }
6019 }
6020
6021 arg2 = perform_implicit_conversion (result_type, arg2, complain);
6022 arg3 = perform_implicit_conversion (result_type, arg3, complain);
6023 }
6024 /* [expr.cond]
6025
6026 --The second and third operands have pointer type, or one has
6027 pointer type and the other is a null pointer constant; pointer
6028 conversions (_conv.ptr_) and qualification conversions
6029 (_conv.qual_) are performed to bring them to their composite
6030 pointer type (_expr.rel_). The result is of the composite
6031 pointer type.
6032
6033 --The second and third operands have pointer to member type, or
6034 one has pointer to member type and the other is a null pointer
6035 constant; pointer to member conversions (_conv.mem_) and
6036 qualification conversions (_conv.qual_) are performed to bring
6037 them to a common type, whose cv-qualification shall match the
6038 cv-qualification of either the second or the third operand.
6039 The result is of the common type. */
6040 else if ((null_ptr_cst_p (arg2)
6041 && TYPE_PTR_OR_PTRMEM_P (arg3_type))
6042 || (null_ptr_cst_p (arg3)
6043 && TYPE_PTR_OR_PTRMEM_P (arg2_type))
6044 || (TYPE_PTR_P (arg2_type) && TYPE_PTR_P (arg3_type))
6045 || (TYPE_PTRDATAMEM_P (arg2_type) && TYPE_PTRDATAMEM_P (arg3_type))
6046 || (TYPE_PTRMEMFUNC_P (arg2_type) && TYPE_PTRMEMFUNC_P (arg3_type)))
6047 {
6048 result_type = composite_pointer_type (loc,
6049 arg2_type, arg3_type, arg2,
6050 arg3, CPO_CONDITIONAL_EXPR,
6051 complain);
6052 if (result_type == error_mark_node)
6053 return error_mark_node;
6054 arg2 = perform_implicit_conversion (result_type, arg2, complain);
6055 arg3 = perform_implicit_conversion (result_type, arg3, complain);
6056 }
6057
6058 if (!result_type)
6059 {
6060 if (complain & tf_error)
6061 error_at (loc, "operands to %<?:%> have different types %qT and %qT",
6062 arg2_type, arg3_type);
6063 return error_mark_node;
6064 }
6065
6066 if (arg2 == error_mark_node || arg3 == error_mark_node)
6067 return error_mark_node;
6068
6069 valid_operands:
6070 if (processing_template_decl && is_glvalue)
6071 {
6072 /* Let lvalue_kind know this was a glvalue. */
6073 tree arg = (result_type == arg2_type ? arg2 : arg3);
6074 result_type = cp_build_reference_type (result_type, xvalue_p (arg));
6075 }
6076
6077 result = build3_loc (loc, COND_EXPR, result_type, arg1, arg2, arg3);
6078
6079 /* If the ARG2 and ARG3 are the same and don't have side-effects,
6080 warn here, because the COND_EXPR will be turned into ARG2. */
6081 if (warn_duplicated_branches
6082 && (complain & tf_warning)
6083 && (arg2 == arg3 || operand_equal_p (arg2, arg3,
6084 OEP_ADDRESS_OF_SAME_FIELD)))
6085 warning_at (EXPR_LOCATION (result), OPT_Wduplicated_branches,
6086 "this condition has identical branches");
6087
6088 /* We can't use result_type below, as fold might have returned a
6089 throw_expr. */
6090
6091 if (!is_glvalue)
6092 {
6093 /* Expand both sides into the same slot, hopefully the target of
6094 the ?: expression. We used to check for TARGET_EXPRs here,
6095 but now we sometimes wrap them in NOP_EXPRs so the test would
6096 fail. */
6097 if (CLASS_TYPE_P (TREE_TYPE (result)))
6098 {
6099 result = get_target_expr (result, complain);
6100 /* Tell gimplify_modify_expr_rhs not to strip this in
6101 assignment context: we want both arms to initialize
6102 the same temporary. */
6103 TARGET_EXPR_NO_ELIDE (result) = true;
6104 }
6105 /* If this expression is an rvalue, but might be mistaken for an
6106 lvalue, we must add a NON_LVALUE_EXPR. */
6107 result = rvalue (result);
6108 if (semantic_result_type)
6109 result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
6110 result);
6111 }
6112 else
6113 {
6114 result = force_paren_expr (result);
6115 gcc_assert (semantic_result_type == NULL_TREE);
6116 }
6117
6118 return result;
6119 }
6120
6121 /* OPERAND is an operand to an expression. Perform necessary steps
6122 required before using it. If OPERAND is NULL_TREE, NULL_TREE is
6123 returned. */
6124
6125 static tree
6126 prep_operand (tree operand)
6127 {
6128 if (operand)
6129 {
6130 if (CLASS_TYPE_P (TREE_TYPE (operand))
6131 && CLASSTYPE_TEMPLATE_INSTANTIATION (TREE_TYPE (operand)))
6132 /* Make sure the template type is instantiated now. */
6133 instantiate_class_template (TYPE_MAIN_VARIANT (TREE_TYPE (operand)));
6134 }
6135
6136 return operand;
6137 }
6138
6139 /* True iff CONV represents a conversion sequence which no other can be better
6140 than under [over.ics.rank]: in other words, a "conversion" to the exact same
6141 type (including binding to a reference to the same type). This is stronger
6142 than the standard's "identity" category, which also includes reference
6143 bindings that add cv-qualifiers or change rvalueness. */
6144
6145 static bool
6146 perfect_conversion_p (conversion *conv)
6147 {
6148 if (CONVERSION_RANK (conv) != cr_identity)
6149 return false;
6150 if (conv->kind == ck_ref_bind)
6151 {
6152 if (!conv->rvaluedness_matches_p)
6153 return false;
6154 if (!same_type_p (TREE_TYPE (conv->type),
6155 next_conversion (conv)->type))
6156 return false;
6157 }
6158 if (conv->check_narrowing)
6159 /* Brace elision is imperfect. */
6160 return false;
6161 return true;
6162 }
6163
6164 /* True if CAND represents a perfect match, i.e. all perfect conversions, so no
6165 other candidate can be a better match. Since the template/non-template
6166 tiebreaker comes immediately after the conversion comparison in
6167 [over.match.best], a perfect non-template candidate is better than all
6168 templates. */
6169
6170 static bool
6171 perfect_candidate_p (z_candidate *cand)
6172 {
6173 if (cand->viable < 1)
6174 return false;
6175 /* CWG1402 makes an implicitly deleted move op worse than other
6176 candidates. */
6177 if (DECL_DELETED_FN (cand->fn) && DECL_DEFAULTED_FN (cand->fn)
6178 && move_fn_p (cand->fn))
6179 return false;
6180 int len = cand->num_convs;
6181 for (int i = 0; i < len; ++i)
6182 if (!perfect_conversion_p (cand->convs[i]))
6183 return false;
6184 if (conversion *conv = cand->second_conv)
6185 if (!perfect_conversion_p (conv))
6186 return false;
6187 return true;
6188 }
6189
6190 /* True iff one of CAND's argument conversions is missing. */
6191
6192 static bool
6193 missing_conversion_p (const z_candidate *cand)
6194 {
6195 for (unsigned i = 0; i < cand->num_convs; ++i)
6196 {
6197 conversion *conv = cand->convs[i];
6198 if (!conv)
6199 return true;
6200 if (conv->kind == ck_deferred_bad)
6201 {
6202 /* We don't know whether this conversion is outright invalid or
6203 just bad, so conservatively assume it's missing. */
6204 gcc_checking_assert (conv->bad_p);
6205 return true;
6206 }
6207 }
6208 return false;
6209 }
6210
6211 /* Add each of the viable functions in FNS (a FUNCTION_DECL or
6212 OVERLOAD) to the CANDIDATES, returning an updated list of
6213 CANDIDATES. The ARGS are the arguments provided to the call;
6214 if FIRST_ARG is non-null it is the implicit object argument,
6215 otherwise the first element of ARGS is used if needed. The
6216 EXPLICIT_TARGS are explicit template arguments provided.
6217 TEMPLATE_ONLY is true if only template functions should be
6218 considered. CONVERSION_PATH, ACCESS_PATH, and FLAGS are as for
6219 add_function_candidate. */
6220
6221 static void
6222 add_candidates (tree fns, tree first_arg, const vec<tree, va_gc> *args,
6223 tree return_type,
6224 tree explicit_targs, bool template_only,
6225 tree conversion_path, tree access_path,
6226 int flags,
6227 struct z_candidate **candidates,
6228 tsubst_flags_t complain)
6229 {
6230 tree ctype;
6231 const vec<tree, va_gc> *non_static_args;
6232 bool check_list_ctor = false;
6233 bool check_converting = false;
6234 unification_kind_t strict;
6235 tree ne_fns = NULL_TREE;
6236
6237 if (!fns)
6238 return;
6239
6240 /* Precalculate special handling of constructors and conversion ops. */
6241 tree fn = OVL_FIRST (fns);
6242 if (DECL_CONV_FN_P (fn))
6243 {
6244 check_list_ctor = false;
6245 check_converting = (flags & LOOKUP_ONLYCONVERTING) != 0;
6246 if (flags & LOOKUP_NO_CONVERSION)
6247 /* We're doing return_type(x). */
6248 strict = DEDUCE_CONV;
6249 else
6250 /* We're doing x.operator return_type(). */
6251 strict = DEDUCE_EXACT;
6252 /* [over.match.funcs] For conversion functions, the function
6253 is considered to be a member of the class of the implicit
6254 object argument for the purpose of defining the type of
6255 the implicit object parameter. */
6256 ctype = TYPE_MAIN_VARIANT (TREE_TYPE (first_arg));
6257 }
6258 else
6259 {
6260 if (DECL_CONSTRUCTOR_P (fn))
6261 {
6262 check_list_ctor = (flags & LOOKUP_LIST_ONLY) != 0;
6263 /* For list-initialization we consider explicit constructors
6264 and complain if one is chosen. */
6265 check_converting
6266 = ((flags & (LOOKUP_ONLYCONVERTING|LOOKUP_LIST_INIT_CTOR))
6267 == LOOKUP_ONLYCONVERTING);
6268 }
6269 strict = DEDUCE_CALL;
6270 ctype = conversion_path ? BINFO_TYPE (conversion_path) : NULL_TREE;
6271 }
6272
6273 /* P2468: Check if operator== is a rewrite target with first operand
6274 (*args)[0]; for now just do the lookups. */
6275 if ((flags & (LOOKUP_REWRITTEN | LOOKUP_REVERSED))
6276 && DECL_OVERLOADED_OPERATOR_IS (fn, EQ_EXPR))
6277 {
6278 tree ne_name = ovl_op_identifier (false, NE_EXPR);
6279 if (DECL_CLASS_SCOPE_P (fn))
6280 {
6281 ne_fns = lookup_fnfields (TREE_TYPE ((*args)[0]), ne_name,
6282 1, tf_none);
6283 if (ne_fns == error_mark_node || ne_fns == NULL_TREE)
6284 ne_fns = NULL_TREE;
6285 else
6286 ne_fns = BASELINK_FUNCTIONS (ne_fns);
6287 }
6288 else
6289 {
6290 tree context = decl_namespace_context (fn);
6291 ne_fns = lookup_qualified_name (context, ne_name, LOOK_want::NORMAL,
6292 /*complain*/false);
6293 if (ne_fns == error_mark_node
6294 || !is_overloaded_fn (ne_fns))
6295 ne_fns = NULL_TREE;
6296 }
6297 }
6298
6299 if (first_arg)
6300 non_static_args = args;
6301 else
6302 /* Delay creating the implicit this parameter until it is needed. */
6303 non_static_args = NULL;
6304
6305 bool seen_strictly_viable = any_strictly_viable (*candidates);
6306 /* If there's a non-template perfect match, we don't need to consider
6307 templates. So check non-templates first. This optimization is only
6308 really needed for the defaulted copy constructor of tuple and the like
6309 (96926), but it seems like we might as well enable it more generally. */
6310 bool seen_perfect = false;
6311 enum { templates, non_templates, either } which = either;
6312 if (template_only)
6313 which = templates;
6314 else /*if (flags & LOOKUP_DEFAULTED)*/
6315 which = non_templates;
6316
6317 /* During overload resolution, we first consider each function under the
6318 assumption that we'll eventually find a strictly viable candidate.
6319 This allows us to circumvent our defacto behavior when checking
6320 argument conversions and shortcut consideration of the candidate
6321 upon encountering the first bad conversion. If this assumption
6322 turns out to be false, and all candidates end up being non-strictly
6323 viable, then we reconsider such candidates under the defacto behavior.
6324 This trick is important for pruning member function overloads according
6325 to their const/ref-qualifiers (since all 'this' conversions are at
6326 worst bad) without breaking -fpermissive. */
6327 tree bad_fns = NULL_TREE;
6328 bool shortcut_bad_convs = true;
6329
6330 again:
6331 for (tree fn : lkp_range (fns))
6332 {
6333 if (check_converting && DECL_NONCONVERTING_P (fn))
6334 continue;
6335 if (check_list_ctor && !is_list_ctor (fn))
6336 continue;
6337 if (which == templates && TREE_CODE (fn) != TEMPLATE_DECL)
6338 continue;
6339 if (which == non_templates && TREE_CODE (fn) == TEMPLATE_DECL)
6340 continue;
6341
6342 tree fn_first_arg = NULL_TREE;
6343 const vec<tree, va_gc> *fn_args = args;
6344
6345 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
6346 {
6347 /* Figure out where the object arg comes from. If this
6348 function is a non-static member and we didn't get an
6349 implicit object argument, move it out of args. */
6350 if (first_arg == NULL_TREE)
6351 {
6352 unsigned int ix;
6353 tree arg;
6354 vec<tree, va_gc> *tempvec;
6355 vec_alloc (tempvec, args->length () - 1);
6356 for (ix = 1; args->iterate (ix, &arg); ++ix)
6357 tempvec->quick_push (arg);
6358 non_static_args = tempvec;
6359 first_arg = (*args)[0];
6360 }
6361
6362 fn_first_arg = first_arg;
6363 fn_args = non_static_args;
6364 }
6365
6366 /* Don't bother reversing an operator with two identical parameters. */
6367 else if (vec_safe_length (args) == 2 && (flags & LOOKUP_REVERSED))
6368 {
6369 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (fn));
6370 if (same_type_p (TREE_VALUE (parmlist),
6371 TREE_VALUE (TREE_CHAIN (parmlist))))
6372 continue;
6373 }
6374
6375 /* When considering reversed operator==, if there's a corresponding
6376 operator!= in the same scope, it's not a rewrite target. */
6377 if (ne_fns)
6378 {
6379 bool found = false;
6380 for (lkp_iterator ne (ne_fns); !found && ne; ++ne)
6381 if (0 && !ne.using_p ()
6382 && DECL_NAMESPACE_SCOPE_P (fn)
6383 && DECL_CONTEXT (*ne) != DECL_CONTEXT (fn))
6384 /* ??? This kludge excludes inline namespace members for the H
6385 test in spaceship-eq15.C, but I don't see why we would want
6386 that behavior. Asked Core 2022-11-04. Disabling for now. */;
6387 else if (fns_correspond (fn, *ne))
6388 {
6389 found = true;
6390 break;
6391 }
6392 if (found)
6393 continue;
6394 }
6395
6396 if (TREE_CODE (fn) == TEMPLATE_DECL)
6397 {
6398 if (!add_template_candidate (candidates,
6399 fn,
6400 ctype,
6401 explicit_targs,
6402 fn_first_arg,
6403 fn_args,
6404 return_type,
6405 access_path,
6406 conversion_path,
6407 flags,
6408 strict,
6409 shortcut_bad_convs,
6410 complain))
6411 continue;
6412 }
6413 else
6414 {
6415 add_function_candidate (candidates,
6416 fn,
6417 ctype,
6418 fn_first_arg,
6419 fn_args,
6420 access_path,
6421 conversion_path,
6422 flags,
6423 NULL,
6424 shortcut_bad_convs,
6425 complain);
6426 if (perfect_candidate_p (*candidates))
6427 seen_perfect = true;
6428 }
6429
6430 z_candidate *cand = *candidates;
6431 if (cand->viable == 1)
6432 seen_strictly_viable = true;
6433
6434 if (cand->viable == -1
6435 && shortcut_bad_convs
6436 && missing_conversion_p (cand))
6437 {
6438 /* This candidate has been tentatively marked non-strictly viable,
6439 and we didn't compute all argument conversions for it (having
6440 stopped at the first bad conversion). Add the function to BAD_FNS
6441 to fully reconsider later if we don't find any strictly viable
6442 candidates. */
6443 if (complain & (tf_error | tf_conv))
6444 {
6445 bad_fns = lookup_add (fn, bad_fns);
6446 *candidates = (*candidates)->next;
6447 }
6448 else
6449 /* But if we're in a SFINAE context, just mark this candidate as
6450 unviable outright and avoid potentially reconsidering it.
6451 This is safe to do because in a SFINAE context, performing a bad
6452 conversion is always an error (even with -fpermissive), so a
6453 non-strictly viable candidate is effectively unviable anyway. */
6454 cand->viable = 0;
6455 }
6456 }
6457 if (which == non_templates && !seen_perfect)
6458 {
6459 which = templates;
6460 goto again;
6461 }
6462 else if (which == templates
6463 && !seen_strictly_viable
6464 && shortcut_bad_convs
6465 && bad_fns)
6466 {
6467 /* None of the candidates are strictly viable, so consider again those
6468 functions in BAD_FNS, this time without shortcutting bad conversions
6469 so that all their argument conversions are computed. */
6470 which = either;
6471 fns = bad_fns;
6472 shortcut_bad_convs = false;
6473 goto again;
6474 }
6475 }
6476
6477 /* Returns 1 if P0145R2 says that the LHS of operator CODE is evaluated first,
6478 -1 if the RHS is evaluated first, or 0 if the order is unspecified. */
6479
6480 static int
6481 op_is_ordered (tree_code code)
6482 {
6483 switch (code)
6484 {
6485 // 5. b @= a
6486 case MODIFY_EXPR:
6487 return (flag_strong_eval_order > 1 ? -1 : 0);
6488
6489 // 6. a[b]
6490 case ARRAY_REF:
6491 return (flag_strong_eval_order > 1 ? 1 : 0);
6492
6493 // 1. a.b
6494 // Not overloadable (yet).
6495 // 2. a->b
6496 // Only one argument.
6497 // 3. a->*b
6498 case MEMBER_REF:
6499 // 7. a << b
6500 case LSHIFT_EXPR:
6501 // 8. a >> b
6502 case RSHIFT_EXPR:
6503 // a && b
6504 // Predates P0145R3.
6505 case TRUTH_ANDIF_EXPR:
6506 // a || b
6507 // Predates P0145R3.
6508 case TRUTH_ORIF_EXPR:
6509 // a , b
6510 // Predates P0145R3.
6511 case COMPOUND_EXPR:
6512 return (flag_strong_eval_order ? 1 : 0);
6513
6514 default:
6515 return 0;
6516 }
6517 }
6518
6519 /* Subroutine of build_new_op: Add to CANDIDATES all candidates for the
6520 operator indicated by CODE/CODE2. This function calls itself recursively to
6521 handle C++20 rewritten comparison operator candidates.
6522
6523 LOOKUPS, if non-NULL, is the set of pertinent namespace-scope operator
6524 overloads to consider. This parameter is used when instantiating a
6525 dependent operator expression and has the same structure as
6526 DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS. */
6527
6528 static tree
6529 add_operator_candidates (z_candidate **candidates,
6530 tree_code code, tree_code code2,
6531 vec<tree, va_gc> *arglist, tree lookups,
6532 int flags, tsubst_flags_t complain)
6533 {
6534 z_candidate *start_candidates = *candidates;
6535 bool ismodop = code2 != ERROR_MARK;
6536 tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
6537
6538 /* LOOKUP_REWRITTEN is set when we're looking for the == or <=> operator to
6539 rewrite from, and also when we're looking for the e.g. < operator to use
6540 on the result of <=>. In the latter case, we don't want the flag set in
6541 the candidate, we just want to suppress looking for rewrites. */
6542 bool rewritten = (flags & LOOKUP_REWRITTEN);
6543 if (rewritten && code != EQ_EXPR && code != SPACESHIP_EXPR)
6544 flags &= ~LOOKUP_REWRITTEN;
6545
6546 bool memonly = false;
6547 switch (code)
6548 {
6549 /* =, ->, [], () must be non-static member functions. */
6550 case MODIFY_EXPR:
6551 if (code2 != NOP_EXPR)
6552 break;
6553 /* FALLTHRU */
6554 case COMPONENT_REF:
6555 case ARRAY_REF:
6556 memonly = true;
6557 break;
6558
6559 default:
6560 break;
6561 }
6562
6563 /* Add namespace-scope operators to the list of functions to
6564 consider. */
6565 if (!memonly)
6566 {
6567 tree fns;
6568 if (!lookups)
6569 fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE);
6570 /* If LOOKUPS is non-NULL, then we're instantiating a dependent operator
6571 expression, and LOOKUPS is the result of stage 1 name lookup. */
6572 else if (tree found = purpose_member (fnname, lookups))
6573 fns = TREE_VALUE (found);
6574 else
6575 fns = NULL_TREE;
6576 fns = lookup_arg_dependent (fnname, fns, arglist);
6577 add_candidates (fns, NULL_TREE, arglist, NULL_TREE,
6578 NULL_TREE, false, NULL_TREE, NULL_TREE,
6579 flags, candidates, complain);
6580 }
6581
6582 /* Add class-member operators to the candidate set. */
6583 tree arg1_type = TREE_TYPE ((*arglist)[0]);
6584 unsigned nargs = arglist->length () > 1 ? 2 : 1;
6585 tree arg2_type = nargs > 1 ? TREE_TYPE ((*arglist)[1]) : NULL_TREE;
6586 if (CLASS_TYPE_P (arg1_type))
6587 {
6588 tree fns = lookup_fnfields (arg1_type, fnname, 1, complain);
6589 if (fns == error_mark_node)
6590 return error_mark_node;
6591 if (fns)
6592 {
6593 if (code == ARRAY_REF)
6594 {
6595 vec<tree,va_gc> *restlist = make_tree_vector ();
6596 for (unsigned i = 1; i < nargs; ++i)
6597 vec_safe_push (restlist, (*arglist)[i]);
6598 z_candidate *save_cand = *candidates;
6599 add_candidates (BASELINK_FUNCTIONS (fns),
6600 (*arglist)[0], restlist, NULL_TREE,
6601 NULL_TREE, false,
6602 BASELINK_BINFO (fns),
6603 BASELINK_ACCESS_BINFO (fns),
6604 flags, candidates, complain);
6605 /* Release the vec if we didn't add a candidate that uses it. */
6606 for (z_candidate *c = *candidates; c != save_cand; c = c->next)
6607 if (c->args == restlist)
6608 {
6609 restlist = NULL;
6610 break;
6611 }
6612 release_tree_vector (restlist);
6613 }
6614 else
6615 add_candidates (BASELINK_FUNCTIONS (fns),
6616 NULL_TREE, arglist, NULL_TREE,
6617 NULL_TREE, false,
6618 BASELINK_BINFO (fns),
6619 BASELINK_ACCESS_BINFO (fns),
6620 flags, candidates, complain);
6621 }
6622 }
6623 /* Per [over.match.oper]3.2, if no operand has a class type, then
6624 only non-member functions that have type T1 or reference to
6625 cv-qualified-opt T1 for the first argument, if the first argument
6626 has an enumeration type, or T2 or reference to cv-qualified-opt
6627 T2 for the second argument, if the second argument has an
6628 enumeration type. Filter out those that don't match. */
6629 else if (! arg2_type || ! CLASS_TYPE_P (arg2_type))
6630 {
6631 struct z_candidate **candp, **next;
6632
6633 for (candp = candidates; *candp != start_candidates; candp = next)
6634 {
6635 unsigned i;
6636 z_candidate *cand = *candp;
6637 next = &cand->next;
6638
6639 tree parmlist = TYPE_ARG_TYPES (TREE_TYPE (cand->fn));
6640
6641 for (i = 0; i < nargs; ++i)
6642 {
6643 tree parmtype = TREE_VALUE (parmlist);
6644 tree argtype = unlowered_expr_type ((*arglist)[i]);
6645
6646 if (TYPE_REF_P (parmtype))
6647 parmtype = TREE_TYPE (parmtype);
6648 if (TREE_CODE (argtype) == ENUMERAL_TYPE
6649 && (same_type_ignoring_top_level_qualifiers_p
6650 (argtype, parmtype)))
6651 break;
6652
6653 parmlist = TREE_CHAIN (parmlist);
6654 }
6655
6656 /* No argument has an appropriate type, so remove this
6657 candidate function from the list. */
6658 if (i == nargs)
6659 {
6660 *candp = cand->next;
6661 next = candp;
6662 }
6663 }
6664 }
6665
6666 if (!rewritten)
6667 {
6668 /* The standard says to rewrite built-in candidates, too,
6669 but there's no point. */
6670 add_builtin_candidates (candidates, code, code2, fnname, arglist,
6671 flags, complain);
6672
6673 /* Maybe add C++20 rewritten comparison candidates. */
6674 tree_code rewrite_code = ERROR_MARK;
6675 if (cxx_dialect >= cxx20
6676 && nargs == 2
6677 && (OVERLOAD_TYPE_P (arg1_type) || OVERLOAD_TYPE_P (arg2_type)))
6678 switch (code)
6679 {
6680 case LT_EXPR:
6681 case LE_EXPR:
6682 case GT_EXPR:
6683 case GE_EXPR:
6684 case SPACESHIP_EXPR:
6685 rewrite_code = SPACESHIP_EXPR;
6686 break;
6687
6688 case NE_EXPR:
6689 case EQ_EXPR:
6690 rewrite_code = EQ_EXPR;
6691 break;
6692
6693 default:;
6694 }
6695
6696 if (rewrite_code)
6697 {
6698 flags |= LOOKUP_REWRITTEN;
6699 if (rewrite_code != code)
6700 /* Add rewritten candidates in same order. */
6701 add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
6702 arglist, lookups, flags, complain);
6703
6704 z_candidate *save_cand = *candidates;
6705
6706 /* Add rewritten candidates in reverse order. */
6707 flags |= LOOKUP_REVERSED;
6708 vec<tree,va_gc> *revlist = make_tree_vector ();
6709 revlist->quick_push ((*arglist)[1]);
6710 revlist->quick_push ((*arglist)[0]);
6711 add_operator_candidates (candidates, rewrite_code, ERROR_MARK,
6712 revlist, lookups, flags, complain);
6713
6714 /* Release the vec if we didn't add a candidate that uses it. */
6715 for (z_candidate *c = *candidates; c != save_cand; c = c->next)
6716 if (c->args == revlist)
6717 {
6718 revlist = NULL;
6719 break;
6720 }
6721 release_tree_vector (revlist);
6722 }
6723 }
6724
6725 return NULL_TREE;
6726 }
6727
6728 tree
6729 build_new_op (const op_location_t &loc, enum tree_code code, int flags,
6730 tree arg1, tree arg2, tree arg3, tree lookups,
6731 tree *overload, tsubst_flags_t complain)
6732 {
6733 struct z_candidate *candidates = 0, *cand;
6734 releasing_vec arglist;
6735 tree result = NULL_TREE;
6736 bool result_valid_p = false;
6737 enum tree_code code2 = ERROR_MARK;
6738 enum tree_code code_orig_arg1 = ERROR_MARK;
6739 enum tree_code code_orig_arg2 = ERROR_MARK;
6740 void *p;
6741 bool strict_p;
6742 bool any_viable_p;
6743
6744 auto_cond_timevar tv (TV_OVERLOAD);
6745
6746 if (error_operand_p (arg1)
6747 || error_operand_p (arg2)
6748 || error_operand_p (arg3))
6749 return error_mark_node;
6750
6751 bool ismodop = code == MODIFY_EXPR;
6752 if (ismodop)
6753 {
6754 code2 = TREE_CODE (arg3);
6755 arg3 = NULL_TREE;
6756 }
6757
6758 tree arg1_type = unlowered_expr_type (arg1);
6759 tree arg2_type = arg2 ? unlowered_expr_type (arg2) : NULL_TREE;
6760
6761 arg1 = prep_operand (arg1);
6762
6763 switch (code)
6764 {
6765 case NEW_EXPR:
6766 case VEC_NEW_EXPR:
6767 case VEC_DELETE_EXPR:
6768 case DELETE_EXPR:
6769 /* Use build_operator_new_call and build_op_delete_call instead. */
6770 gcc_unreachable ();
6771
6772 case CALL_EXPR:
6773 /* Use build_op_call instead. */
6774 gcc_unreachable ();
6775
6776 case TRUTH_ORIF_EXPR:
6777 case TRUTH_ANDIF_EXPR:
6778 case TRUTH_AND_EXPR:
6779 case TRUTH_OR_EXPR:
6780 /* These are saved for the sake of warn_logical_operator. */
6781 code_orig_arg1 = TREE_CODE (arg1);
6782 code_orig_arg2 = TREE_CODE (arg2);
6783 break;
6784 case GT_EXPR:
6785 case LT_EXPR:
6786 case GE_EXPR:
6787 case LE_EXPR:
6788 case EQ_EXPR:
6789 case NE_EXPR:
6790 /* These are saved for the sake of maybe_warn_bool_compare. */
6791 code_orig_arg1 = TREE_CODE (arg1_type);
6792 code_orig_arg2 = TREE_CODE (arg2_type);
6793 break;
6794
6795 default:
6796 break;
6797 }
6798
6799 arg2 = prep_operand (arg2);
6800 arg3 = prep_operand (arg3);
6801
6802 if (code == COND_EXPR)
6803 /* Use build_conditional_expr instead. */
6804 gcc_unreachable ();
6805 else if (! OVERLOAD_TYPE_P (arg1_type)
6806 && (! arg2 || ! OVERLOAD_TYPE_P (arg2_type)))
6807 goto builtin;
6808
6809 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
6810 {
6811 arg2 = integer_zero_node;
6812 arg2_type = integer_type_node;
6813 }
6814
6815 arglist->quick_push (arg1);
6816 if (arg2 != NULL_TREE)
6817 arglist->quick_push (arg2);
6818 if (arg3 != NULL_TREE)
6819 arglist->quick_push (arg3);
6820
6821 /* Get the high-water mark for the CONVERSION_OBSTACK. */
6822 p = conversion_obstack_alloc (0);
6823
6824 result = add_operator_candidates (&candidates, code, code2, arglist,
6825 lookups, flags, complain);
6826 if (result == error_mark_node)
6827 goto user_defined_result_ready;
6828
6829 switch (code)
6830 {
6831 case COMPOUND_EXPR:
6832 case ADDR_EXPR:
6833 /* For these, the built-in candidates set is empty
6834 [over.match.oper]/3. We don't want non-strict matches
6835 because exact matches are always possible with built-in
6836 operators. The built-in candidate set for COMPONENT_REF
6837 would be empty too, but since there are no such built-in
6838 operators, we accept non-strict matches for them. */
6839 strict_p = true;
6840 break;
6841
6842 default:
6843 strict_p = false;
6844 break;
6845 }
6846
6847 candidates = splice_viable (candidates, strict_p, &any_viable_p);
6848 if (!any_viable_p)
6849 {
6850 switch (code)
6851 {
6852 case POSTINCREMENT_EXPR:
6853 case POSTDECREMENT_EXPR:
6854 /* Don't try anything fancy if we're not allowed to produce
6855 errors. */
6856 if (!(complain & tf_error))
6857 return error_mark_node;
6858
6859 /* Look for an `operator++ (int)'. Pre-1985 C++ didn't
6860 distinguish between prefix and postfix ++ and
6861 operator++() was used for both, so we allow this with
6862 -fpermissive. */
6863 else
6864 {
6865 tree fnname = ovl_op_identifier (ismodop, ismodop ? code2 : code);
6866 const char *msg = (flag_permissive)
6867 ? G_("no %<%D(int)%> declared for postfix %qs,"
6868 " trying prefix operator instead")
6869 : G_("no %<%D(int)%> declared for postfix %qs");
6870 permerror (loc, msg, fnname, OVL_OP_INFO (false, code)->name);
6871 }
6872
6873 if (!flag_permissive)
6874 return error_mark_node;
6875
6876 if (code == POSTINCREMENT_EXPR)
6877 code = PREINCREMENT_EXPR;
6878 else
6879 code = PREDECREMENT_EXPR;
6880 result = build_new_op (loc, code, flags, arg1, NULL_TREE,
6881 NULL_TREE, lookups, overload, complain);
6882 break;
6883
6884 /* The caller will deal with these. */
6885 case ADDR_EXPR:
6886 case COMPOUND_EXPR:
6887 case COMPONENT_REF:
6888 case CO_AWAIT_EXPR:
6889 result = NULL_TREE;
6890 result_valid_p = true;
6891 break;
6892
6893 default:
6894 if (complain & tf_error)
6895 {
6896 /* If one of the arguments of the operator represents
6897 an invalid use of member function pointer, try to report
6898 a meaningful error ... */
6899 if (invalid_nonstatic_memfn_p (loc, arg1, tf_error)
6900 || invalid_nonstatic_memfn_p (loc, arg2, tf_error)
6901 || invalid_nonstatic_memfn_p (loc, arg3, tf_error))
6902 /* We displayed the error message. */;
6903 else
6904 {
6905 /* ... Otherwise, report the more generic
6906 "no matching operator found" error */
6907 auto_diagnostic_group d;
6908 op_error (loc, code, code2, arg1, arg2, arg3, FALSE);
6909 print_z_candidates (loc, candidates);
6910 }
6911 }
6912 result = error_mark_node;
6913 break;
6914 }
6915 }
6916 else
6917 {
6918 cand = tourney (candidates, complain);
6919 if (cand == 0)
6920 {
6921 if (complain & tf_error)
6922 {
6923 auto_diagnostic_group d;
6924 op_error (loc, code, code2, arg1, arg2, arg3, TRUE);
6925 print_z_candidates (loc, candidates);
6926 }
6927 result = error_mark_node;
6928 if (overload)
6929 *overload = error_mark_node;
6930 }
6931 else if (TREE_CODE (cand->fn) == FUNCTION_DECL)
6932 {
6933 if (overload)
6934 *overload = cand->fn;
6935
6936 if (resolve_args (arglist, complain) == NULL)
6937 result = error_mark_node;
6938 else
6939 {
6940 tsubst_flags_t ocomplain = complain;
6941 if (cand->rewritten ())
6942 /* We'll wrap this call in another one. */
6943 ocomplain &= ~tf_decltype;
6944 if (cand->reversed ())
6945 {
6946 /* We swapped these in add_candidate, swap them back now. */
6947 std::swap (cand->convs[0], cand->convs[1]);
6948 if (cand->fn == current_function_decl)
6949 warning_at (loc, 0, "in C++20 this comparison calls the "
6950 "current function recursively with reversed "
6951 "arguments");
6952 }
6953 result = build_over_call (cand, LOOKUP_NORMAL, ocomplain);
6954 }
6955
6956 if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
6957 /* There won't be a CALL_EXPR. */;
6958 else if (result && result != error_mark_node)
6959 {
6960 tree call = extract_call_expr (result);
6961 CALL_EXPR_OPERATOR_SYNTAX (call) = true;
6962
6963 /* Specify evaluation order as per P0145R2. */
6964 CALL_EXPR_ORDERED_ARGS (call) = false;
6965 switch (op_is_ordered (code))
6966 {
6967 case -1:
6968 CALL_EXPR_REVERSE_ARGS (call) = true;
6969 break;
6970
6971 case 1:
6972 CALL_EXPR_ORDERED_ARGS (call) = true;
6973 break;
6974
6975 default:
6976 break;
6977 }
6978 }
6979
6980 /* If this was a C++20 rewritten comparison, adjust the result. */
6981 if (cand->rewritten ())
6982 {
6983 /* FIXME build_min_non_dep_op_overload can't handle rewrites. */
6984 if (overload)
6985 *overload = NULL_TREE;
6986 switch (code)
6987 {
6988 case EQ_EXPR:
6989 gcc_checking_assert (cand->reversed ());
6990 gcc_fallthrough ();
6991 case NE_EXPR:
6992 if (result == error_mark_node)
6993 ;
6994 /* If a rewritten operator== candidate is selected by
6995 overload resolution for an operator @, its return type
6996 shall be cv bool.... */
6997 else if (TREE_CODE (TREE_TYPE (result)) != BOOLEAN_TYPE)
6998 {
6999 if (complain & tf_error)
7000 {
7001 auto_diagnostic_group d;
7002 error_at (loc, "return type of %qD is not %qs",
7003 cand->fn, "bool");
7004 inform (loc, "used as rewritten candidate for "
7005 "comparison of %qT and %qT",
7006 arg1_type, arg2_type);
7007 }
7008 result = error_mark_node;
7009 }
7010 else if (code == NE_EXPR)
7011 /* !(y == x) or !(x == y) */
7012 result = build1_loc (loc, TRUTH_NOT_EXPR,
7013 boolean_type_node, result);
7014 break;
7015
7016 /* If a rewritten operator<=> candidate is selected by
7017 overload resolution for an operator @, x @ y is
7018 interpreted as 0 @ (y <=> x) if the selected candidate is
7019 a synthesized candidate with reversed order of parameters,
7020 or (x <=> y) @ 0 otherwise, using the selected rewritten
7021 operator<=> candidate. */
7022 case SPACESHIP_EXPR:
7023 if (!cand->reversed ())
7024 /* We're in the build_new_op call below for an outer
7025 reversed call; we don't need to do anything more. */
7026 break;
7027 gcc_fallthrough ();
7028 case LT_EXPR:
7029 case LE_EXPR:
7030 case GT_EXPR:
7031 case GE_EXPR:
7032 {
7033 tree lhs = result;
7034 tree rhs = integer_zero_node;
7035 if (cand->reversed ())
7036 std::swap (lhs, rhs);
7037 warning_sentinel ws (warn_zero_as_null_pointer_constant);
7038 result = build_new_op (loc, code,
7039 LOOKUP_NORMAL|LOOKUP_REWRITTEN,
7040 lhs, rhs, NULL_TREE, lookups,
7041 NULL, complain);
7042 }
7043 break;
7044
7045 default:
7046 gcc_unreachable ();
7047 }
7048 }
7049 }
7050 else
7051 {
7052 /* Give any warnings we noticed during overload resolution. */
7053 if (cand->warnings && (complain & tf_warning))
7054 {
7055 struct candidate_warning *w;
7056 for (w = cand->warnings; w; w = w->next)
7057 joust (cand, w->loser, 1, complain);
7058 }
7059
7060 /* Check for comparison of different enum types. */
7061 switch (code)
7062 {
7063 case GT_EXPR:
7064 case LT_EXPR:
7065 case GE_EXPR:
7066 case LE_EXPR:
7067 case EQ_EXPR:
7068 case NE_EXPR:
7069 if (TREE_CODE (arg1_type) == ENUMERAL_TYPE
7070 && TREE_CODE (arg2_type) == ENUMERAL_TYPE
7071 && (TYPE_MAIN_VARIANT (arg1_type)
7072 != TYPE_MAIN_VARIANT (arg2_type))
7073 && (complain & tf_warning))
7074 warning_at (loc, OPT_Wenum_compare,
7075 "comparison between %q#T and %q#T",
7076 arg1_type, arg2_type);
7077 break;
7078 default:
7079 break;
7080 }
7081
7082 /* "If a built-in candidate is selected by overload resolution, the
7083 operands of class type are converted to the types of the
7084 corresponding parameters of the selected operation function,
7085 except that the second standard conversion sequence of a
7086 user-defined conversion sequence (12.3.3.1.2) is not applied." */
7087 conversion *conv = cand->convs[0];
7088 if (conv->user_conv_p)
7089 {
7090 conv = strip_standard_conversion (conv);
7091 arg1 = convert_like (conv, arg1, complain);
7092 }
7093
7094 if (arg2)
7095 {
7096 conv = cand->convs[1];
7097 if (conv->user_conv_p)
7098 {
7099 conv = strip_standard_conversion (conv);
7100 arg2 = convert_like (conv, arg2, complain);
7101 }
7102 }
7103
7104 if (arg3)
7105 {
7106 conv = cand->convs[2];
7107 if (conv->user_conv_p)
7108 {
7109 conv = strip_standard_conversion (conv);
7110 arg3 = convert_like (conv, arg3, complain);
7111 }
7112 }
7113 }
7114 }
7115
7116 user_defined_result_ready:
7117
7118 /* Free all the conversions we allocated. */
7119 obstack_free (&conversion_obstack, p);
7120
7121 if (result || result_valid_p)
7122 return result;
7123
7124 builtin:
7125 switch (code)
7126 {
7127 case MODIFY_EXPR:
7128 return cp_build_modify_expr (loc, arg1, code2, arg2, complain);
7129
7130 case INDIRECT_REF:
7131 return cp_build_indirect_ref (loc, arg1, RO_UNARY_STAR, complain);
7132
7133 case TRUTH_ANDIF_EXPR:
7134 case TRUTH_ORIF_EXPR:
7135 case TRUTH_AND_EXPR:
7136 case TRUTH_OR_EXPR:
7137 if (complain & tf_warning)
7138 warn_logical_operator (loc, code, boolean_type_node,
7139 code_orig_arg1, arg1,
7140 code_orig_arg2, arg2);
7141 /* Fall through. */
7142 case GT_EXPR:
7143 case LT_EXPR:
7144 case GE_EXPR:
7145 case LE_EXPR:
7146 case EQ_EXPR:
7147 case NE_EXPR:
7148 if ((complain & tf_warning)
7149 && ((code_orig_arg1 == BOOLEAN_TYPE)
7150 ^ (code_orig_arg2 == BOOLEAN_TYPE)))
7151 maybe_warn_bool_compare (loc, code, arg1, arg2);
7152 if (complain & tf_warning && warn_tautological_compare)
7153 warn_tautological_cmp (loc, code, arg1, arg2);
7154 /* Fall through. */
7155 case SPACESHIP_EXPR:
7156 case PLUS_EXPR:
7157 case MINUS_EXPR:
7158 case MULT_EXPR:
7159 case TRUNC_DIV_EXPR:
7160 case MAX_EXPR:
7161 case MIN_EXPR:
7162 case LSHIFT_EXPR:
7163 case RSHIFT_EXPR:
7164 case TRUNC_MOD_EXPR:
7165 case BIT_AND_EXPR:
7166 case BIT_IOR_EXPR:
7167 case BIT_XOR_EXPR:
7168 return cp_build_binary_op (loc, code, arg1, arg2, complain);
7169
7170 case UNARY_PLUS_EXPR:
7171 case NEGATE_EXPR:
7172 case BIT_NOT_EXPR:
7173 case TRUTH_NOT_EXPR:
7174 case PREINCREMENT_EXPR:
7175 case POSTINCREMENT_EXPR:
7176 case PREDECREMENT_EXPR:
7177 case POSTDECREMENT_EXPR:
7178 case REALPART_EXPR:
7179 case IMAGPART_EXPR:
7180 case ABS_EXPR:
7181 case CO_AWAIT_EXPR:
7182 return cp_build_unary_op (code, arg1, false, complain);
7183
7184 case ARRAY_REF:
7185 return cp_build_array_ref (input_location, arg1, arg2, complain);
7186
7187 case MEMBER_REF:
7188 return build_m_component_ref (cp_build_indirect_ref (loc, arg1,
7189 RO_ARROW_STAR,
7190 complain),
7191 arg2, complain);
7192
7193 /* The caller will deal with these. */
7194 case ADDR_EXPR:
7195 case COMPONENT_REF:
7196 case COMPOUND_EXPR:
7197 return NULL_TREE;
7198
7199 default:
7200 gcc_unreachable ();
7201 }
7202 return NULL_TREE;
7203 }
7204
7205 /* Build a new call to operator[]. This may change ARGS. */
7206
7207 tree
7208 build_op_subscript (const op_location_t &loc, tree obj,
7209 vec<tree, va_gc> **args, tree *overload,
7210 tsubst_flags_t complain)
7211 {
7212 struct z_candidate *candidates = 0, *cand;
7213 tree fns, first_mem_arg = NULL_TREE;
7214 bool any_viable_p;
7215 tree result = NULL_TREE;
7216 void *p;
7217
7218 auto_cond_timevar tv (TV_OVERLOAD);
7219
7220 obj = mark_lvalue_use (obj);
7221
7222 if (error_operand_p (obj))
7223 return error_mark_node;
7224
7225 tree type = TREE_TYPE (obj);
7226
7227 obj = prep_operand (obj);
7228
7229 if (TYPE_BINFO (type))
7230 {
7231 fns = lookup_fnfields (TYPE_BINFO (type), ovl_op_identifier (ARRAY_REF),
7232 1, complain);
7233 if (fns == error_mark_node)
7234 return error_mark_node;
7235 }
7236 else
7237 fns = NULL_TREE;
7238
7239 if (args != NULL && *args != NULL)
7240 {
7241 *args = resolve_args (*args, complain);
7242 if (*args == NULL)
7243 return error_mark_node;
7244 }
7245
7246 /* Get the high-water mark for the CONVERSION_OBSTACK. */
7247 p = conversion_obstack_alloc (0);
7248
7249 if (fns)
7250 {
7251 first_mem_arg = obj;
7252
7253 add_candidates (BASELINK_FUNCTIONS (fns),
7254 first_mem_arg, *args, NULL_TREE,
7255 NULL_TREE, false,
7256 BASELINK_BINFO (fns), BASELINK_ACCESS_BINFO (fns),
7257 LOOKUP_NORMAL, &candidates, complain);
7258 }
7259
7260 /* Be strict here because if we choose a bad conversion candidate, the
7261 errors we get won't mention the call context. */
7262 candidates = splice_viable (candidates, true, &any_viable_p);
7263 if (!any_viable_p)
7264 {
7265 if (complain & tf_error)
7266 {
7267 auto_diagnostic_group d;
7268 error ("no match for call to %<%T::operator[] (%A)%>",
7269 TREE_TYPE (obj), build_tree_list_vec (*args));
7270 print_z_candidates (loc, candidates);
7271 }
7272 result = error_mark_node;
7273 }
7274 else
7275 {
7276 cand = tourney (candidates, complain);
7277 if (cand == 0)
7278 {
7279 if (complain & tf_error)
7280 {
7281 auto_diagnostic_group d;
7282 error ("call of %<%T::operator[] (%A)%> is ambiguous",
7283 TREE_TYPE (obj), build_tree_list_vec (*args));
7284 print_z_candidates (loc, candidates);
7285 }
7286 result = error_mark_node;
7287 }
7288 else if (TREE_CODE (cand->fn) == FUNCTION_DECL
7289 && DECL_OVERLOADED_OPERATOR_P (cand->fn)
7290 && DECL_OVERLOADED_OPERATOR_IS (cand->fn, ARRAY_REF))
7291 {
7292 if (overload)
7293 *overload = cand->fn;
7294 result = build_over_call (cand, LOOKUP_NORMAL, complain);
7295 if (trivial_fn_p (cand->fn) || DECL_IMMEDIATE_FUNCTION_P (cand->fn))
7296 /* There won't be a CALL_EXPR. */;
7297 else if (result && result != error_mark_node)
7298 {
7299 tree call = extract_call_expr (result);
7300 CALL_EXPR_OPERATOR_SYNTAX (call) = true;
7301
7302 /* Specify evaluation order as per P0145R2. */
7303 CALL_EXPR_ORDERED_ARGS (call) = op_is_ordered (ARRAY_REF) == 1;
7304 }
7305 }
7306 else
7307 gcc_unreachable ();
7308 }
7309
7310 /* Free all the conversions we allocated. */
7311 obstack_free (&conversion_obstack, p);
7312
7313 return result;
7314 }
7315
7316 /* CALL was returned by some call-building function; extract the actual
7317 CALL_EXPR from any bits that have been tacked on, e.g. by
7318 convert_from_reference. */
7319
7320 tree
7321 extract_call_expr (tree call)
7322 {
7323 while (TREE_CODE (call) == COMPOUND_EXPR)
7324 call = TREE_OPERAND (call, 1);
7325 if (REFERENCE_REF_P (call))
7326 call = TREE_OPERAND (call, 0);
7327 if (TREE_CODE (call) == TARGET_EXPR)
7328 call = TARGET_EXPR_INITIAL (call);
7329 if (cxx_dialect >= cxx20)
7330 switch (TREE_CODE (call))
7331 {
7332 /* C++20 rewritten comparison operators. */
7333 case TRUTH_NOT_EXPR:
7334 call = TREE_OPERAND (call, 0);
7335 break;
7336 case LT_EXPR:
7337 case LE_EXPR:
7338 case GT_EXPR:
7339 case GE_EXPR:
7340 case SPACESHIP_EXPR:
7341 {
7342 tree op0 = TREE_OPERAND (call, 0);
7343 if (integer_zerop (op0))
7344 call = TREE_OPERAND (call, 1);
7345 else
7346 call = op0;
7347 }
7348 break;
7349 default:;
7350 }
7351
7352 if (TREE_CODE (call) != CALL_EXPR
7353 && TREE_CODE (call) != AGGR_INIT_EXPR
7354 && call != error_mark_node)
7355 return NULL_TREE;
7356 return call;
7357 }
7358
7359 /* Returns true if FN has two parameters, of which the second has type
7360 size_t. */
7361
7362 static bool
7363 second_parm_is_size_t (tree fn)
7364 {
7365 tree t = FUNCTION_ARG_CHAIN (fn);
7366 if (!t || !same_type_p (TREE_VALUE (t), size_type_node))
7367 return false;
7368 t = TREE_CHAIN (t);
7369 if (t == void_list_node)
7370 return true;
7371 return false;
7372 }
7373
7374 /* True if T, an allocation function, has std::align_val_t as its second
7375 argument. */
7376
7377 bool
7378 aligned_allocation_fn_p (tree t)
7379 {
7380 if (!aligned_new_threshold)
7381 return false;
7382
7383 tree a = FUNCTION_ARG_CHAIN (t);
7384 return (a && same_type_p (TREE_VALUE (a), align_type_node));
7385 }
7386
7387 /* True if T is std::destroying_delete_t. */
7388
7389 static bool
7390 std_destroying_delete_t_p (tree t)
7391 {
7392 return (TYPE_CONTEXT (t) == std_node
7393 && id_equal (TYPE_IDENTIFIER (t), "destroying_delete_t"));
7394 }
7395
7396 /* A deallocation function with at least two parameters whose second parameter
7397 type is of type std::destroying_delete_t is a destroying operator delete. A
7398 destroying operator delete shall be a class member function named operator
7399 delete. [ Note: Array deletion cannot use a destroying operator
7400 delete. --end note ] */
7401
7402 tree
7403 destroying_delete_p (tree t)
7404 {
7405 tree a = TYPE_ARG_TYPES (TREE_TYPE (t));
7406 if (!a || !TREE_CHAIN (a))
7407 return NULL_TREE;
7408 tree type = TREE_VALUE (TREE_CHAIN (a));
7409 return std_destroying_delete_t_p (type) ? type : NULL_TREE;
7410 }
7411
7412 struct dealloc_info
7413 {
7414 bool sized;
7415 bool aligned;
7416 tree destroying;
7417 };
7418
7419 /* Returns true iff T, an element of an OVERLOAD chain, is a usual deallocation
7420 function (3.7.4.2 [basic.stc.dynamic.deallocation]). If so, and DI is
7421 non-null, also set *DI. */
7422
7423 static bool
7424 usual_deallocation_fn_p (tree t, dealloc_info *di)
7425 {
7426 if (di) *di = dealloc_info();
7427
7428 /* A template instance is never a usual deallocation function,
7429 regardless of its signature. */
7430 if (TREE_CODE (t) == TEMPLATE_DECL
7431 || primary_template_specialization_p (t))
7432 return false;
7433
7434 /* A usual deallocation function is a deallocation function whose parameters
7435 after the first are
7436 - optionally, a parameter of type std::destroying_delete_t, then
7437 - optionally, a parameter of type std::size_t, then
7438 - optionally, a parameter of type std::align_val_t. */
7439 bool global = DECL_NAMESPACE_SCOPE_P (t);
7440 tree chain = FUNCTION_ARG_CHAIN (t);
7441 if (chain && destroying_delete_p (t))
7442 {
7443 if (di) di->destroying = TREE_VALUE (chain);
7444 chain = TREE_CHAIN (chain);
7445 }
7446 if (chain
7447 && (!global || flag_sized_deallocation)
7448 && same_type_p (TREE_VALUE (chain), size_type_node))
7449 {
7450 if (di) di->sized = true;
7451 chain = TREE_CHAIN (chain);
7452 }
7453 if (chain && aligned_new_threshold
7454 && same_type_p (TREE_VALUE (chain), align_type_node))
7455 {
7456 if (di) di->aligned = true;
7457 chain = TREE_CHAIN (chain);
7458 }
7459 return (chain == void_list_node);
7460 }
7461
7462 /* Just return whether FN is a usual deallocation function. */
7463
7464 bool
7465 usual_deallocation_fn_p (tree fn)
7466 {
7467 return usual_deallocation_fn_p (fn, NULL);
7468 }
7469
7470 /* Build a call to operator delete. This has to be handled very specially,
7471 because the restrictions on what signatures match are different from all
7472 other call instances. For a normal delete, only a delete taking (void *)
7473 or (void *, size_t) is accepted. For a placement delete, only an exact
7474 match with the placement new is accepted.
7475
7476 CODE is either DELETE_EXPR or VEC_DELETE_EXPR.
7477 ADDR is the pointer to be deleted.
7478 SIZE is the size of the memory block to be deleted.
7479 GLOBAL_P is true if the delete-expression should not consider
7480 class-specific delete operators.
7481 PLACEMENT is the corresponding placement new call, or NULL_TREE.
7482
7483 If this call to "operator delete" is being generated as part to
7484 deallocate memory allocated via a new-expression (as per [expr.new]
7485 which requires that if the initialization throws an exception then
7486 we call a deallocation function), then ALLOC_FN is the allocation
7487 function. */
7488
7489 tree
7490 build_op_delete_call (enum tree_code code, tree addr, tree size,
7491 bool global_p, tree placement,
7492 tree alloc_fn, tsubst_flags_t complain)
7493 {
7494 tree fn = NULL_TREE;
7495 tree fns, fnname, type, t;
7496 dealloc_info di_fn = { };
7497
7498 if (addr == error_mark_node)
7499 return error_mark_node;
7500
7501 type = strip_array_types (TREE_TYPE (TREE_TYPE (addr)));
7502
7503 fnname = ovl_op_identifier (false, code);
7504
7505 if (CLASS_TYPE_P (type)
7506 && COMPLETE_TYPE_P (complete_type (type))
7507 && !global_p)
7508 /* In [class.free]
7509
7510 If the result of the lookup is ambiguous or inaccessible, or if
7511 the lookup selects a placement deallocation function, the
7512 program is ill-formed.
7513
7514 Therefore, we ask lookup_fnfields to complain about ambiguity. */
7515 {
7516 fns = lookup_fnfields (TYPE_BINFO (type), fnname, 1, complain);
7517 if (fns == error_mark_node)
7518 return error_mark_node;
7519 }
7520 else
7521 fns = NULL_TREE;
7522
7523 if (fns == NULL_TREE)
7524 fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE);
7525
7526 /* Strip const and volatile from addr. */
7527 tree oaddr = addr;
7528 addr = cp_convert (ptr_type_node, addr, complain);
7529
7530 tree excluded_destroying = NULL_TREE;
7531
7532 if (placement)
7533 {
7534 /* "A declaration of a placement deallocation function matches the
7535 declaration of a placement allocation function if it has the same
7536 number of parameters and, after parameter transformations (8.3.5),
7537 all parameter types except the first are identical."
7538
7539 So we build up the function type we want and ask instantiate_type
7540 to get it for us. */
7541 t = FUNCTION_ARG_CHAIN (alloc_fn);
7542 t = tree_cons (NULL_TREE, ptr_type_node, t);
7543 t = build_function_type (void_type_node, t);
7544
7545 fn = instantiate_type (t, fns, tf_none);
7546 if (fn == error_mark_node)
7547 return NULL_TREE;
7548
7549 fn = MAYBE_BASELINK_FUNCTIONS (fn);
7550
7551 /* "If the lookup finds the two-parameter form of a usual deallocation
7552 function (3.7.4.2) and that function, considered as a placement
7553 deallocation function, would have been selected as a match for the
7554 allocation function, the program is ill-formed." */
7555 if (second_parm_is_size_t (fn))
7556 {
7557 const char *const msg1
7558 = G_("exception cleanup for this placement new selects "
7559 "non-placement %<operator delete%>");
7560 const char *const msg2
7561 = G_("%qD is a usual (non-placement) deallocation "
7562 "function in C++14 (or with %<-fsized-deallocation%>)");
7563
7564 /* But if the class has an operator delete (void *), then that is
7565 the usual deallocation function, so we shouldn't complain
7566 about using the operator delete (void *, size_t). */
7567 if (DECL_CLASS_SCOPE_P (fn))
7568 for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
7569 {
7570 if (usual_deallocation_fn_p (elt)
7571 && FUNCTION_ARG_CHAIN (elt) == void_list_node)
7572 goto ok;
7573 }
7574 /* Before C++14 a two-parameter global deallocation function is
7575 always a placement deallocation function, but warn if
7576 -Wc++14-compat. */
7577 else if (!flag_sized_deallocation)
7578 {
7579 if (complain & tf_warning)
7580 {
7581 auto_diagnostic_group d;
7582 if (warning (OPT_Wc__14_compat, msg1))
7583 inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
7584 }
7585 goto ok;
7586 }
7587
7588 if (complain & tf_warning_or_error)
7589 {
7590 auto_diagnostic_group d;
7591 if (permerror (input_location, msg1))
7592 {
7593 /* Only mention C++14 for namespace-scope delete. */
7594 if (DECL_NAMESPACE_SCOPE_P (fn))
7595 inform (DECL_SOURCE_LOCATION (fn), msg2, fn);
7596 else
7597 inform (DECL_SOURCE_LOCATION (fn),
7598 "%qD is a usual (non-placement) deallocation "
7599 "function", fn);
7600 }
7601 }
7602 else
7603 return error_mark_node;
7604 ok:;
7605 }
7606 }
7607 else
7608 /* "Any non-placement deallocation function matches a non-placement
7609 allocation function. If the lookup finds a single matching
7610 deallocation function, that function will be called; otherwise, no
7611 deallocation function will be called." */
7612 for (tree elt : lkp_range (MAYBE_BASELINK_FUNCTIONS (fns)))
7613 {
7614 dealloc_info di_elt;
7615 if (usual_deallocation_fn_p (elt, &di_elt))
7616 {
7617 /* If we're called for an EH cleanup in a new-expression, we can't
7618 use a destroying delete; the exception was thrown before the
7619 object was constructed. */
7620 if (alloc_fn && di_elt.destroying)
7621 {
7622 excluded_destroying = elt;
7623 continue;
7624 }
7625
7626 if (!fn)
7627 {
7628 fn = elt;
7629 di_fn = di_elt;
7630 continue;
7631 }
7632
7633 /* -- If any of the deallocation functions is a destroying
7634 operator delete, all deallocation functions that are not
7635 destroying operator deletes are eliminated from further
7636 consideration. */
7637 if (di_elt.destroying != di_fn.destroying)
7638 {
7639 if (di_elt.destroying)
7640 {
7641 fn = elt;
7642 di_fn = di_elt;
7643 }
7644 continue;
7645 }
7646
7647 /* -- If the type has new-extended alignment, a function with a
7648 parameter of type std::align_val_t is preferred; otherwise a
7649 function without such a parameter is preferred. If exactly one
7650 preferred function is found, that function is selected and the
7651 selection process terminates. If more than one preferred
7652 function is found, all non-preferred functions are eliminated
7653 from further consideration. */
7654 if (aligned_new_threshold)
7655 {
7656 bool want_align = type_has_new_extended_alignment (type);
7657 if (di_elt.aligned != di_fn.aligned)
7658 {
7659 if (want_align == di_elt.aligned)
7660 {
7661 fn = elt;
7662 di_fn = di_elt;
7663 }
7664 continue;
7665 }
7666 }
7667
7668 /* -- If the deallocation functions have class scope, the one
7669 without a parameter of type std::size_t is selected. */
7670 bool want_size;
7671 if (DECL_CLASS_SCOPE_P (fn))
7672 want_size = false;
7673
7674 /* -- If the type is complete and if, for the second alternative
7675 (delete array) only, the operand is a pointer to a class type
7676 with a non-trivial destructor or a (possibly multi-dimensional)
7677 array thereof, the function with a parameter of type std::size_t
7678 is selected.
7679
7680 -- Otherwise, it is unspecified whether a deallocation function
7681 with a parameter of type std::size_t is selected. */
7682 else
7683 {
7684 want_size = COMPLETE_TYPE_P (type);
7685 if (code == VEC_DELETE_EXPR
7686 && !TYPE_VEC_NEW_USES_COOKIE (type))
7687 /* We need a cookie to determine the array size. */
7688 want_size = false;
7689 }
7690 gcc_assert (di_fn.sized != di_elt.sized);
7691 if (want_size == di_elt.sized)
7692 {
7693 fn = elt;
7694 di_fn = di_elt;
7695 }
7696 }
7697 }
7698
7699 /* If we have a matching function, call it. */
7700 if (fn)
7701 {
7702 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
7703
7704 /* If the FN is a member function, make sure that it is
7705 accessible. */
7706 if (BASELINK_P (fns))
7707 perform_or_defer_access_check (BASELINK_BINFO (fns), fn, fn,
7708 complain);
7709
7710 /* Core issue 901: It's ok to new a type with deleted delete. */
7711 if (DECL_DELETED_FN (fn) && alloc_fn)
7712 return NULL_TREE;
7713
7714 tree ret;
7715 if (placement)
7716 {
7717 /* The placement args might not be suitable for overload
7718 resolution at this point, so build the call directly. */
7719 int nargs = call_expr_nargs (placement);
7720 tree *argarray = XALLOCAVEC (tree, nargs);
7721 int i;
7722 argarray[0] = addr;
7723 for (i = 1; i < nargs; i++)
7724 argarray[i] = CALL_EXPR_ARG (placement, i);
7725 if (!mark_used (fn, complain) && !(complain & tf_error))
7726 return error_mark_node;
7727 ret = build_cxx_call (fn, nargs, argarray, complain);
7728 }
7729 else
7730 {
7731 tree destroying = di_fn.destroying;
7732 if (destroying)
7733 {
7734 /* Strip const and volatile from addr but retain the type of the
7735 object. */
7736 tree rtype = TREE_TYPE (TREE_TYPE (oaddr));
7737 rtype = cv_unqualified (rtype);
7738 rtype = TYPE_POINTER_TO (rtype);
7739 addr = cp_convert (rtype, oaddr, complain);
7740 destroying = build_functional_cast (input_location,
7741 destroying, NULL_TREE,
7742 complain);
7743 }
7744
7745 releasing_vec args;
7746 args->quick_push (addr);
7747 if (destroying)
7748 args->quick_push (destroying);
7749 if (di_fn.sized)
7750 args->quick_push (size);
7751 if (di_fn.aligned)
7752 {
7753 tree al = build_int_cst (align_type_node, TYPE_ALIGN_UNIT (type));
7754 args->quick_push (al);
7755 }
7756 ret = cp_build_function_call_vec (fn, &args, complain);
7757 }
7758
7759 /* Set this flag for all callers of this function. In addition to
7760 delete-expressions, this is called for deallocating coroutine state;
7761 treat that as an implicit delete-expression. This is also called for
7762 the delete if the constructor throws in a new-expression, and for a
7763 deleting destructor (which implements a delete-expression). */
7764 /* But leave this flag off for destroying delete to avoid wrong
7765 assumptions in the optimizers. */
7766 tree call = extract_call_expr (ret);
7767 if (TREE_CODE (call) == CALL_EXPR && !destroying_delete_p (fn))
7768 CALL_FROM_NEW_OR_DELETE_P (call) = 1;
7769
7770 return ret;
7771 }
7772
7773 /* If there's only a destroying delete that we can't use because the
7774 object isn't constructed yet, and we used global new, use global
7775 delete as well. */
7776 if (excluded_destroying
7777 && DECL_NAMESPACE_SCOPE_P (alloc_fn))
7778 return build_op_delete_call (code, addr, size, true, placement,
7779 alloc_fn, complain);
7780
7781 /* [expr.new]
7782
7783 If no unambiguous matching deallocation function can be found,
7784 propagating the exception does not cause the object's memory to
7785 be freed. */
7786 if (alloc_fn)
7787 {
7788 if ((complain & tf_warning)
7789 && !placement)
7790 {
7791 bool w = warning (0,
7792 "no corresponding deallocation function for %qD",
7793 alloc_fn);
7794 if (w && excluded_destroying)
7795 inform (DECL_SOURCE_LOCATION (excluded_destroying), "destroying "
7796 "delete %qD cannot be used to release the allocated memory"
7797 " if the initialization throws because the object is not "
7798 "constructed yet", excluded_destroying);
7799 }
7800 return NULL_TREE;
7801 }
7802
7803 if (complain & tf_error)
7804 error ("no suitable %<operator %s%> for %qT",
7805 OVL_OP_INFO (false, code)->name, type);
7806 return error_mark_node;
7807 }
7808
7809 /* Issue diagnostics about a disallowed access of DECL, using DIAG_DECL
7810 in the diagnostics.
7811
7812 If ISSUE_ERROR is true, then issue an error about the access, followed
7813 by a note showing the declaration. Otherwise, just show the note.
7814
7815 DIAG_DECL and DIAG_LOCATION will almost always be the same.
7816 DIAG_LOCATION is just another DECL. NO_ACCESS_REASON is an optional
7817 parameter used to specify why DECL wasn't accessible (e.g. ak_private
7818 would be because DECL was private). If not using NO_ACCESS_REASON,
7819 then it must be ak_none, and the access failure reason will be
7820 figured out by looking at the protection of DECL. */
7821
7822 void
7823 complain_about_access (tree decl, tree diag_decl, tree diag_location,
7824 bool issue_error, access_kind no_access_reason)
7825 {
7826 /* If we have not already figured out why DECL is inaccessible... */
7827 if (no_access_reason == ak_none)
7828 {
7829 /* Examine the access of DECL to find out why. */
7830 if (TREE_PRIVATE (decl))
7831 no_access_reason = ak_private;
7832 else if (TREE_PROTECTED (decl))
7833 no_access_reason = ak_protected;
7834 }
7835
7836 /* Now generate an error message depending on calculated access. */
7837 if (no_access_reason == ak_private)
7838 {
7839 if (issue_error)
7840 error ("%q#D is private within this context", diag_decl);
7841 inform (DECL_SOURCE_LOCATION (diag_location), "declared private here");
7842 }
7843 else if (no_access_reason == ak_protected)
7844 {
7845 if (issue_error)
7846 error ("%q#D is protected within this context", diag_decl);
7847 inform (DECL_SOURCE_LOCATION (diag_location), "declared protected here");
7848 }
7849 /* Couldn't figure out why DECL is inaccesible, so just say it's
7850 inaccessible. */
7851 else
7852 {
7853 if (issue_error)
7854 error ("%q#D is inaccessible within this context", diag_decl);
7855 inform (DECL_SOURCE_LOCATION (diag_decl), "declared here");
7856 }
7857 }
7858
7859 /* Initialize a temporary of type TYPE with EXPR. The FLAGS are a
7860 bitwise or of LOOKUP_* values. If any errors are warnings are
7861 generated, set *DIAGNOSTIC_FN to "error" or "warning",
7862 respectively. If no diagnostics are generated, set *DIAGNOSTIC_FN
7863 to NULL. */
7864
7865 static tree
7866 build_temp (tree expr, tree type, int flags,
7867 diagnostic_t *diagnostic_kind, tsubst_flags_t complain)
7868 {
7869 int savew, savee;
7870
7871 *diagnostic_kind = DK_UNSPECIFIED;
7872
7873 /* If the source is a packed field, calling the copy constructor will require
7874 binding the field to the reference parameter to the copy constructor, and
7875 we'll end up with an infinite loop. If we can use a bitwise copy, then
7876 do that now. */
7877 if ((lvalue_kind (expr) & clk_packed)
7878 && CLASS_TYPE_P (TREE_TYPE (expr))
7879 && !type_has_nontrivial_copy_init (TREE_TYPE (expr)))
7880 return get_target_expr (expr, complain);
7881
7882 /* In decltype, we might have decided not to wrap this call in a TARGET_EXPR.
7883 But it turns out to be a subexpression, so perform temporary
7884 materialization now. */
7885 if (TREE_CODE (expr) == CALL_EXPR
7886 && CLASS_TYPE_P (type)
7887 && same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7888 expr = build_cplus_new (type, expr, complain);
7889
7890 savew = warningcount + werrorcount, savee = errorcount;
7891 releasing_vec args (make_tree_vector_single (expr));
7892 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
7893 &args, type, flags, complain);
7894 if (warningcount + werrorcount > savew)
7895 *diagnostic_kind = DK_WARNING;
7896 else if (errorcount > savee)
7897 *diagnostic_kind = DK_ERROR;
7898 return expr;
7899 }
7900
7901 /* Get any location for EXPR, falling back to input_location.
7902
7903 If the result is in a system header and is the virtual location for
7904 a token coming from the expansion of a macro, unwind it to the
7905 location of the expansion point of the macro (e.g. to avoid the
7906 diagnostic being suppressed for expansions of NULL where "NULL" is
7907 in a system header). */
7908
7909 static location_t
7910 get_location_for_expr_unwinding_for_system_header (tree expr)
7911 {
7912 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
7913 loc = expansion_point_location_if_in_system_header (loc);
7914 return loc;
7915 }
7916
7917 /* Perform warnings about peculiar, but valid, conversions from/to NULL.
7918 Also handle a subset of zero as null warnings.
7919 EXPR is implicitly converted to type TOTYPE.
7920 FN and ARGNUM are used for diagnostics. */
7921
7922 static void
7923 conversion_null_warnings (tree totype, tree expr, tree fn, int argnum)
7924 {
7925 /* Issue warnings about peculiar, but valid, uses of NULL. */
7926 if (TREE_CODE (totype) != BOOLEAN_TYPE
7927 && ARITHMETIC_TYPE_P (totype)
7928 && null_node_p (expr))
7929 {
7930 location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
7931 if (fn)
7932 {
7933 auto_diagnostic_group d;
7934 if (warning_at (loc, OPT_Wconversion_null,
7935 "passing NULL to non-pointer argument %P of %qD",
7936 argnum, fn))
7937 inform (get_fndecl_argument_location (fn, argnum),
7938 " declared here");
7939 }
7940 else
7941 warning_at (loc, OPT_Wconversion_null,
7942 "converting to non-pointer type %qT from NULL", totype);
7943 }
7944
7945 /* Issue warnings if "false" is converted to a NULL pointer */
7946 else if (TREE_CODE (TREE_TYPE (expr)) == BOOLEAN_TYPE
7947 && TYPE_PTR_P (totype))
7948 {
7949 location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
7950 if (fn)
7951 {
7952 auto_diagnostic_group d;
7953 if (warning_at (loc, OPT_Wconversion_null,
7954 "converting %<false%> to pointer type for argument "
7955 "%P of %qD", argnum, fn))
7956 inform (get_fndecl_argument_location (fn, argnum),
7957 " declared here");
7958 }
7959 else
7960 warning_at (loc, OPT_Wconversion_null,
7961 "converting %<false%> to pointer type %qT", totype);
7962 }
7963 /* Handle zero as null pointer warnings for cases other
7964 than EQ_EXPR and NE_EXPR */
7965 else if ((TYPE_PTR_OR_PTRMEM_P (totype) || NULLPTR_TYPE_P (totype))
7966 && null_ptr_cst_p (expr))
7967 {
7968 location_t loc = get_location_for_expr_unwinding_for_system_header (expr);
7969 maybe_warn_zero_as_null_pointer_constant (expr, loc);
7970 }
7971 }
7972
7973 /* We gave a diagnostic during a conversion. If this was in the second
7974 standard conversion sequence of a user-defined conversion sequence, say
7975 which user-defined conversion. */
7976
7977 static void
7978 maybe_print_user_conv_context (conversion *convs)
7979 {
7980 if (convs->user_conv_p)
7981 for (conversion *t = convs; t; t = next_conversion (t))
7982 if (t->kind == ck_user)
7983 {
7984 print_z_candidate (0, N_(" after user-defined conversion:"),
7985 t->cand);
7986 break;
7987 }
7988 }
7989
7990 /* Locate the parameter with the given index within FNDECL.
7991 ARGNUM is zero based, -1 indicates the `this' argument of a method.
7992 Return the location of the FNDECL itself if there are problems. */
7993
7994 location_t
7995 get_fndecl_argument_location (tree fndecl, int argnum)
7996 {
7997 /* The locations of implicitly-declared functions are likely to be
7998 more meaningful than those of their parameters. */
7999 if (DECL_ARTIFICIAL (fndecl))
8000 return DECL_SOURCE_LOCATION (fndecl);
8001
8002 int i;
8003 tree param;
8004
8005 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
8006 for (i = 0, param = FUNCTION_FIRST_USER_PARM (fndecl);
8007 i < argnum && param;
8008 i++, param = TREE_CHAIN (param))
8009 ;
8010
8011 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
8012 return the location of FNDECL. */
8013 if (param == NULL)
8014 return DECL_SOURCE_LOCATION (fndecl);
8015
8016 return DECL_SOURCE_LOCATION (param);
8017 }
8018
8019 /* If FNDECL is non-NULL, issue a note highlighting ARGNUM
8020 within its declaration (or the fndecl itself if something went
8021 wrong). */
8022
8023 void
8024 maybe_inform_about_fndecl_for_bogus_argument_init (tree fn, int argnum)
8025 {
8026 if (fn)
8027 inform (get_fndecl_argument_location (fn, argnum),
8028 " initializing argument %P of %qD", argnum, fn);
8029 }
8030
8031 /* Maybe warn about C++20 Conversions to arrays of unknown bound. C is
8032 the conversion, EXPR is the expression we're converting. */
8033
8034 static void
8035 maybe_warn_array_conv (location_t loc, conversion *c, tree expr)
8036 {
8037 if (cxx_dialect >= cxx20)
8038 return;
8039
8040 tree type = TREE_TYPE (expr);
8041 type = strip_pointer_operator (type);
8042
8043 if (TREE_CODE (type) != ARRAY_TYPE
8044 || TYPE_DOMAIN (type) == NULL_TREE)
8045 return;
8046
8047 if (pedantic && conv_binds_to_array_of_unknown_bound (c))
8048 pedwarn (loc, OPT_Wc__20_extensions,
8049 "conversions to arrays of unknown bound "
8050 "are only available with %<-std=c++20%> or %<-std=gnu++20%>");
8051 }
8052
8053 /* We call this recursively in convert_like_internal. */
8054 static tree convert_like (conversion *, tree, tree, int, bool, bool, bool,
8055 tsubst_flags_t);
8056
8057 /* Perform the conversions in CONVS on the expression EXPR. FN and
8058 ARGNUM are used for diagnostics. ARGNUM is zero based, -1
8059 indicates the `this' argument of a method. INNER is nonzero when
8060 being called to continue a conversion chain. It is negative when a
8061 reference binding will be applied, positive otherwise. If
8062 ISSUE_CONVERSION_WARNINGS is true, warnings about suspicious
8063 conversions will be emitted if appropriate. If C_CAST_P is true,
8064 this conversion is coming from a C-style cast; in that case,
8065 conversions to inaccessible bases are permitted. */
8066
8067 static tree
8068 convert_like_internal (conversion *convs, tree expr, tree fn, int argnum,
8069 bool issue_conversion_warnings, bool c_cast_p,
8070 bool nested_p, tsubst_flags_t complain)
8071 {
8072 tree totype = convs->type;
8073 diagnostic_t diag_kind;
8074 int flags;
8075 location_t loc = cp_expr_loc_or_input_loc (expr);
8076
8077 if (convs->bad_p && !(complain & tf_error))
8078 return error_mark_node;
8079
8080 if (convs->bad_p
8081 && convs->kind != ck_user
8082 && convs->kind != ck_list
8083 && convs->kind != ck_ambig
8084 && (convs->kind != ck_ref_bind
8085 || (convs->user_conv_p && next_conversion (convs)->bad_p))
8086 && (convs->kind != ck_rvalue
8087 || SCALAR_TYPE_P (totype))
8088 && convs->kind != ck_base)
8089 {
8090 bool complained = false;
8091 conversion *t = convs;
8092
8093 /* Give a helpful error if this is bad because of excess braces. */
8094 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8095 && SCALAR_TYPE_P (totype)
8096 && CONSTRUCTOR_NELTS (expr) > 0
8097 && BRACE_ENCLOSED_INITIALIZER_P (CONSTRUCTOR_ELT (expr, 0)->value))
8098 {
8099 complained = permerror (loc, "too many braces around initializer "
8100 "for %qT", totype);
8101 while (BRACE_ENCLOSED_INITIALIZER_P (expr)
8102 && CONSTRUCTOR_NELTS (expr) == 1)
8103 expr = CONSTRUCTOR_ELT (expr, 0)->value;
8104 }
8105
8106 /* Give a helpful error if this is bad because a conversion to bool
8107 from std::nullptr_t requires direct-initialization. */
8108 if (NULLPTR_TYPE_P (TREE_TYPE (expr))
8109 && TREE_CODE (totype) == BOOLEAN_TYPE)
8110 complained = permerror (loc, "converting to %qH from %qI requires "
8111 "direct-initialization",
8112 totype, TREE_TYPE (expr));
8113
8114 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
8115 && TREE_CODE (totype) == REAL_TYPE
8116 && (extended_float_type_p (TREE_TYPE (expr))
8117 || extended_float_type_p (totype)))
8118 switch (cp_compare_floating_point_conversion_ranks (TREE_TYPE (expr),
8119 totype))
8120 {
8121 case 2:
8122 pedwarn (loc, 0, "converting to %qH from %qI with greater "
8123 "conversion rank", totype, TREE_TYPE (expr));
8124 complained = true;
8125 break;
8126 case 3:
8127 pedwarn (loc, 0, "converting to %qH from %qI with unordered "
8128 "conversion ranks", totype, TREE_TYPE (expr));
8129 complained = true;
8130 break;
8131 default:
8132 break;
8133 }
8134
8135 for (; t ; t = next_conversion (t))
8136 {
8137 if (t->kind == ck_user && t->cand->reason)
8138 {
8139 auto_diagnostic_group d;
8140 complained = permerror (loc, "invalid user-defined conversion "
8141 "from %qH to %qI", TREE_TYPE (expr),
8142 totype);
8143 if (complained)
8144 print_z_candidate (loc, N_("candidate is:"), t->cand);
8145 expr = convert_like (t, expr, fn, argnum,
8146 /*issue_conversion_warnings=*/false,
8147 /*c_cast_p=*/false, /*nested_p=*/true,
8148 complain);
8149 if (convs->kind == ck_ref_bind)
8150 expr = convert_to_reference (totype, expr, CONV_IMPLICIT,
8151 LOOKUP_NORMAL, NULL_TREE,
8152 complain);
8153 else
8154 expr = cp_convert (totype, expr, complain);
8155 if (complained)
8156 maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
8157 return expr;
8158 }
8159 else if (t->kind == ck_user || !t->bad_p)
8160 {
8161 expr = convert_like (t, expr, fn, argnum,
8162 /*issue_conversion_warnings=*/false,
8163 /*c_cast_p=*/false, /*nested_p=*/true,
8164 complain);
8165 break;
8166 }
8167 else if (t->kind == ck_ambig)
8168 return convert_like (t, expr, fn, argnum,
8169 /*issue_conversion_warnings=*/false,
8170 /*c_cast_p=*/false, /*nested_p=*/true,
8171 complain);
8172 else if (t->kind == ck_identity)
8173 break;
8174 }
8175 if (!complained && expr != error_mark_node)
8176 {
8177 range_label_for_type_mismatch label (TREE_TYPE (expr), totype);
8178 gcc_rich_location richloc (loc, &label);
8179 complained = permerror (&richloc,
8180 "invalid conversion from %qH to %qI",
8181 TREE_TYPE (expr), totype);
8182 }
8183 if (complained)
8184 maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
8185
8186 return cp_convert (totype, expr, complain);
8187 }
8188
8189 if (issue_conversion_warnings && (complain & tf_warning))
8190 conversion_null_warnings (totype, expr, fn, argnum);
8191
8192 switch (convs->kind)
8193 {
8194 case ck_user:
8195 {
8196 struct z_candidate *cand = convs->cand;
8197
8198 if (cand == NULL)
8199 /* We chose the surrogate function from add_conv_candidate, now we
8200 actually need to build the conversion. */
8201 cand = build_user_type_conversion_1 (totype, expr,
8202 LOOKUP_NO_CONVERSION, complain);
8203
8204 tree convfn = cand->fn;
8205
8206 /* When converting from an init list we consider explicit
8207 constructors, but actually trying to call one is an error. */
8208 if (DECL_NONCONVERTING_P (convfn) && DECL_CONSTRUCTOR_P (convfn)
8209 && BRACE_ENCLOSED_INITIALIZER_P (expr)
8210 /* Unless this is for direct-list-initialization. */
8211 && (!CONSTRUCTOR_IS_DIRECT_INIT (expr) || convs->need_temporary_p)
8212 /* And in C++98 a default constructor can't be explicit. */
8213 && cxx_dialect >= cxx11)
8214 {
8215 if (!(complain & tf_error))
8216 return error_mark_node;
8217 location_t loc = location_of (expr);
8218 if (CONSTRUCTOR_NELTS (expr) == 0
8219 && FUNCTION_FIRST_USER_PARMTYPE (convfn) != void_list_node)
8220 {
8221 auto_diagnostic_group d;
8222 if (pedwarn (loc, 0, "converting to %qT from initializer list "
8223 "would use explicit constructor %qD",
8224 totype, convfn))
8225 inform (loc, "in C++11 and above a default constructor "
8226 "can be explicit");
8227 }
8228 else
8229 error ("converting to %qT from initializer list would use "
8230 "explicit constructor %qD", totype, convfn);
8231 }
8232
8233 /* If we're initializing from {}, it's value-initialization. */
8234 if (BRACE_ENCLOSED_INITIALIZER_P (expr)
8235 && CONSTRUCTOR_NELTS (expr) == 0
8236 && TYPE_HAS_DEFAULT_CONSTRUCTOR (totype)
8237 && !processing_template_decl)
8238 {
8239 bool direct = CONSTRUCTOR_IS_DIRECT_INIT (expr);
8240 if (abstract_virtuals_error (NULL_TREE, totype, complain))
8241 return error_mark_node;
8242 expr = build_value_init (totype, complain);
8243 expr = get_target_expr (expr, complain);
8244 if (expr != error_mark_node)
8245 {
8246 TARGET_EXPR_LIST_INIT_P (expr) = true;
8247 TARGET_EXPR_DIRECT_INIT_P (expr) = direct;
8248 }
8249 return expr;
8250 }
8251
8252 /* We don't know here whether EXPR is being used as an lvalue or
8253 rvalue, but we know it's read. */
8254 mark_exp_read (expr);
8255
8256 /* Pass LOOKUP_NO_CONVERSION so rvalue/base handling knows not to allow
8257 any more UDCs. */
8258 expr = build_over_call (cand, LOOKUP_NORMAL|LOOKUP_NO_CONVERSION,
8259 complain);
8260
8261 /* If this is a constructor or a function returning an aggr type,
8262 we need to build up a TARGET_EXPR. */
8263 if (DECL_CONSTRUCTOR_P (convfn))
8264 {
8265 expr = build_cplus_new (totype, expr, complain);
8266
8267 /* Remember that this was list-initialization. */
8268 if (convs->check_narrowing && expr != error_mark_node)
8269 TARGET_EXPR_LIST_INIT_P (expr) = true;
8270 }
8271
8272 return expr;
8273 }
8274 case ck_identity:
8275 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
8276 {
8277 int nelts = CONSTRUCTOR_NELTS (expr);
8278 if (nelts == 0)
8279 expr = build_value_init (totype, complain);
8280 else if (nelts == 1)
8281 expr = CONSTRUCTOR_ELT (expr, 0)->value;
8282 else
8283 gcc_unreachable ();
8284 }
8285 expr = mark_use (expr, /*rvalue_p=*/!convs->rvaluedness_matches_p,
8286 /*read_p=*/true, UNKNOWN_LOCATION,
8287 /*reject_builtin=*/true);
8288
8289 if (type_unknown_p (expr))
8290 expr = instantiate_type (totype, expr, complain);
8291 if (!nested_p && TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8292 expr = cp_convert (totype, TREE_OPERAND (expr, 0), complain);
8293 if (expr == null_node
8294 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (totype))
8295 /* If __null has been converted to an integer type, we do not want to
8296 continue to warn about uses of EXPR as an integer, rather than as a
8297 pointer. */
8298 expr = build_int_cst (totype, 0);
8299 return expr;
8300 case ck_ambig:
8301 /* We leave bad_p off ck_ambig because overload resolution considers
8302 it valid, it just fails when we try to perform it. So we need to
8303 check complain here, too. */
8304 if (complain & tf_error)
8305 {
8306 /* Call build_user_type_conversion again for the error. */
8307 int flags = (convs->need_temporary_p
8308 ? LOOKUP_IMPLICIT : LOOKUP_NORMAL);
8309 build_user_type_conversion (totype, convs->u.expr, flags, complain);
8310 gcc_assert (seen_error ());
8311 maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
8312 }
8313 return error_mark_node;
8314
8315 case ck_list:
8316 {
8317 /* Conversion to std::initializer_list<T>. */
8318 tree elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (totype), 0);
8319 unsigned len = CONSTRUCTOR_NELTS (expr);
8320 tree array;
8321
8322 if (len)
8323 {
8324 tree val; unsigned ix;
8325
8326 tree new_ctor = build_constructor (init_list_type_node, NULL);
8327
8328 /* Convert all the elements. */
8329 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), ix, val)
8330 {
8331 tree sub = convert_like (convs->u.list[ix], val, fn,
8332 argnum, false, false,
8333 /*nested_p=*/true, complain);
8334 if (sub == error_mark_node)
8335 return sub;
8336 if (!BRACE_ENCLOSED_INITIALIZER_P (val)
8337 && !check_narrowing (TREE_TYPE (sub), val, complain))
8338 return error_mark_node;
8339 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_ctor),
8340 NULL_TREE, sub);
8341 if (!TREE_CONSTANT (sub))
8342 TREE_CONSTANT (new_ctor) = false;
8343 }
8344 /* Build up the array. */
8345 elttype = cp_build_qualified_type
8346 (elttype, cp_type_quals (elttype) | TYPE_QUAL_CONST);
8347 array = build_array_of_n_type (elttype, len);
8348 array = finish_compound_literal (array, new_ctor, complain);
8349 /* Take the address explicitly rather than via decay_conversion
8350 to avoid the error about taking the address of a temporary. */
8351 array = cp_build_addr_expr (array, complain);
8352 }
8353 else
8354 array = nullptr_node;
8355
8356 array = cp_convert (build_pointer_type (elttype), array, complain);
8357 if (array == error_mark_node)
8358 return error_mark_node;
8359
8360 /* Build up the initializer_list object. Note: fail gracefully
8361 if the object cannot be completed because, for example, no
8362 definition is provided (c++/80956). */
8363 totype = complete_type_or_maybe_complain (totype, NULL_TREE, complain);
8364 if (!totype)
8365 return error_mark_node;
8366 tree field = next_aggregate_field (TYPE_FIELDS (totype));
8367 vec<constructor_elt, va_gc> *vec = NULL;
8368 CONSTRUCTOR_APPEND_ELT (vec, field, array);
8369 field = next_aggregate_field (DECL_CHAIN (field));
8370 CONSTRUCTOR_APPEND_ELT (vec, field, size_int (len));
8371 tree new_ctor = build_constructor (totype, vec);
8372 return get_target_expr (new_ctor, complain);
8373 }
8374
8375 case ck_aggr:
8376 if (TREE_CODE (totype) == COMPLEX_TYPE)
8377 {
8378 tree real = CONSTRUCTOR_ELT (expr, 0)->value;
8379 tree imag = CONSTRUCTOR_ELT (expr, 1)->value;
8380 real = perform_implicit_conversion (TREE_TYPE (totype),
8381 real, complain);
8382 imag = perform_implicit_conversion (TREE_TYPE (totype),
8383 imag, complain);
8384 expr = build2 (COMPLEX_EXPR, totype, real, imag);
8385 return expr;
8386 }
8387 expr = reshape_init (totype, expr, complain);
8388 expr = get_target_expr (digest_init (totype, expr, complain),
8389 complain);
8390 if (expr != error_mark_node)
8391 TARGET_EXPR_LIST_INIT_P (expr) = true;
8392 return expr;
8393
8394 default:
8395 break;
8396 };
8397
8398 expr = convert_like (next_conversion (convs), expr, fn, argnum,
8399 convs->kind == ck_ref_bind
8400 ? issue_conversion_warnings : false,
8401 c_cast_p, /*nested_p=*/true, complain & ~tf_no_cleanup);
8402 if (expr == error_mark_node)
8403 return error_mark_node;
8404
8405 switch (convs->kind)
8406 {
8407 case ck_rvalue:
8408 expr = decay_conversion (expr, complain);
8409 if (expr == error_mark_node)
8410 {
8411 if (complain & tf_error)
8412 {
8413 auto_diagnostic_group d;
8414 maybe_print_user_conv_context (convs);
8415 maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
8416 }
8417 return error_mark_node;
8418 }
8419
8420 if (! MAYBE_CLASS_TYPE_P (totype))
8421 return expr;
8422
8423 /* Don't introduce copies when passing arguments along to the inherited
8424 constructor. */
8425 if (current_function_decl
8426 && flag_new_inheriting_ctors
8427 && DECL_INHERITED_CTOR (current_function_decl))
8428 return expr;
8429
8430 if (TREE_CODE (expr) == TARGET_EXPR
8431 && TARGET_EXPR_LIST_INIT_P (expr))
8432 /* Copy-list-initialization doesn't actually involve a copy. */
8433 return expr;
8434
8435 /* Fall through. */
8436 case ck_base:
8437 if (convs->kind == ck_base && !convs->need_temporary_p)
8438 {
8439 /* We are going to bind a reference directly to a base-class
8440 subobject of EXPR. */
8441 /* Build an expression for `*((base*) &expr)'. */
8442 expr = convert_to_base (expr, totype,
8443 !c_cast_p, /*nonnull=*/true, complain);
8444 return expr;
8445 }
8446
8447 /* Copy-initialization where the cv-unqualified version of the source
8448 type is the same class as, or a derived class of, the class of the
8449 destination [is treated as direct-initialization]. [dcl.init] */
8450 flags = LOOKUP_NORMAL;
8451 /* This conversion is being done in the context of a user-defined
8452 conversion (i.e. the second step of copy-initialization), so
8453 don't allow any more. */
8454 if (convs->user_conv_p)
8455 flags |= LOOKUP_NO_CONVERSION;
8456 /* We might be performing a conversion of the argument
8457 to the user-defined conversion, i.e., not a conversion of the
8458 result of the user-defined conversion. In which case we skip
8459 explicit constructors. */
8460 if (convs->copy_init_p)
8461 flags |= LOOKUP_ONLYCONVERTING;
8462 expr = build_temp (expr, totype, flags, &diag_kind, complain);
8463 if (diag_kind && complain)
8464 {
8465 auto_diagnostic_group d;
8466 maybe_print_user_conv_context (convs);
8467 maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
8468 }
8469
8470 return build_cplus_new (totype, expr, complain);
8471
8472 case ck_ref_bind:
8473 {
8474 tree ref_type = totype;
8475
8476 /* direct_reference_binding might have inserted a ck_qual under
8477 this ck_ref_bind for the benefit of conversion sequence ranking.
8478 Ignore the conversion; we'll create our own below. */
8479 if (next_conversion (convs)->kind == ck_qual
8480 && !convs->need_temporary_p)
8481 {
8482 gcc_assert (same_type_p (TREE_TYPE (expr),
8483 next_conversion (convs)->type));
8484 /* Strip the cast created by the ck_qual; cp_build_addr_expr
8485 below expects an lvalue. */
8486 STRIP_NOPS (expr);
8487 }
8488
8489 if (convs->bad_p && !next_conversion (convs)->bad_p)
8490 {
8491 tree extype = TREE_TYPE (expr);
8492 auto_diagnostic_group d;
8493 if (TYPE_REF_IS_RVALUE (ref_type)
8494 && lvalue_p (expr))
8495 error_at (loc, "cannot bind rvalue reference of type %qH to "
8496 "lvalue of type %qI", totype, extype);
8497 else if (!TYPE_REF_IS_RVALUE (ref_type) && !lvalue_p (expr)
8498 && !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (ref_type)))
8499 {
8500 conversion *next = next_conversion (convs);
8501 if (next->kind == ck_std)
8502 {
8503 next = next_conversion (next);
8504 error_at (loc, "cannot bind non-const lvalue reference of "
8505 "type %qH to a value of type %qI",
8506 totype, next->type);
8507 }
8508 else if (!CP_TYPE_CONST_P (TREE_TYPE (ref_type)))
8509 error_at (loc, "cannot bind non-const lvalue reference of "
8510 "type %qH to an rvalue of type %qI", totype, extype);
8511 else // extype is volatile
8512 error_at (loc, "cannot bind lvalue reference of type "
8513 "%qH to an rvalue of type %qI", totype,
8514 extype);
8515 }
8516 else if (!reference_compatible_p (TREE_TYPE (totype), extype))
8517 {
8518 /* If we're converting from T[] to T[N], don't talk
8519 about discarding qualifiers. (Converting from T[N] to
8520 T[] is allowed by P0388R4.) */
8521 if (TREE_CODE (extype) == ARRAY_TYPE
8522 && TYPE_DOMAIN (extype) == NULL_TREE
8523 && TREE_CODE (TREE_TYPE (totype)) == ARRAY_TYPE
8524 && TYPE_DOMAIN (TREE_TYPE (totype)) != NULL_TREE)
8525 error_at (loc, "cannot bind reference of type %qH to %qI "
8526 "due to different array bounds", totype, extype);
8527 else
8528 error_at (loc, "binding reference of type %qH to %qI "
8529 "discards qualifiers", totype, extype);
8530 }
8531 else
8532 gcc_unreachable ();
8533 maybe_print_user_conv_context (convs);
8534 maybe_inform_about_fndecl_for_bogus_argument_init (fn, argnum);
8535
8536 return error_mark_node;
8537 }
8538 else if (complain & tf_warning)
8539 maybe_warn_array_conv (loc, convs, expr);
8540
8541 /* If necessary, create a temporary.
8542
8543 VA_ARG_EXPR and CONSTRUCTOR expressions are special cases
8544 that need temporaries, even when their types are reference
8545 compatible with the type of reference being bound, so the
8546 upcoming call to cp_build_addr_expr doesn't fail. */
8547 if (convs->need_temporary_p
8548 || TREE_CODE (expr) == CONSTRUCTOR
8549 || TREE_CODE (expr) == VA_ARG_EXPR)
8550 {
8551 /* Otherwise, a temporary of type "cv1 T1" is created and
8552 initialized from the initializer expression using the rules
8553 for a non-reference copy-initialization (8.5). */
8554
8555 tree type = TREE_TYPE (ref_type);
8556 cp_lvalue_kind lvalue = lvalue_kind (expr);
8557
8558 gcc_assert (similar_type_p (type, next_conversion (convs)->type));
8559 if (!CP_TYPE_CONST_NON_VOLATILE_P (type)
8560 && !TYPE_REF_IS_RVALUE (ref_type))
8561 {
8562 /* If the reference is volatile or non-const, we
8563 cannot create a temporary. */
8564 if (complain & tf_error)
8565 {
8566 if (lvalue & clk_bitfield)
8567 error_at (loc, "cannot bind bit-field %qE to %qT",
8568 expr, ref_type);
8569 else if (lvalue & clk_packed)
8570 error_at (loc, "cannot bind packed field %qE to %qT",
8571 expr, ref_type);
8572 else
8573 error_at (loc, "cannot bind rvalue %qE to %qT",
8574 expr, ref_type);
8575 }
8576 return error_mark_node;
8577 }
8578 /* If the source is a packed field, and we must use a copy
8579 constructor, then building the target expr will require
8580 binding the field to the reference parameter to the
8581 copy constructor, and we'll end up with an infinite
8582 loop. If we can use a bitwise copy, then we'll be
8583 OK. */
8584 if ((lvalue & clk_packed)
8585 && CLASS_TYPE_P (type)
8586 && type_has_nontrivial_copy_init (type))
8587 {
8588 error_at (loc, "cannot bind packed field %qE to %qT",
8589 expr, ref_type);
8590 return error_mark_node;
8591 }
8592 if (lvalue & clk_bitfield)
8593 {
8594 expr = convert_bitfield_to_declared_type (expr);
8595 expr = fold_convert (type, expr);
8596 }
8597
8598 /* Creating &TARGET_EXPR<> in a template would break when
8599 tsubsting the expression, so use an IMPLICIT_CONV_EXPR
8600 instead. This can happen even when there's no class
8601 involved, e.g., when converting an integer to a reference
8602 type. */
8603 if (processing_template_decl)
8604 return build1 (IMPLICIT_CONV_EXPR, totype, expr);
8605 expr = build_target_expr_with_type (expr, type, complain);
8606 }
8607
8608 /* Take the address of the thing to which we will bind the
8609 reference. */
8610 expr = cp_build_addr_expr (expr, complain);
8611 if (expr == error_mark_node)
8612 return error_mark_node;
8613
8614 /* Convert it to a pointer to the type referred to by the
8615 reference. This will adjust the pointer if a derived to
8616 base conversion is being performed. */
8617 expr = cp_convert (build_pointer_type (TREE_TYPE (ref_type)),
8618 expr, complain);
8619 /* Convert the pointer to the desired reference type. */
8620 return build_nop (ref_type, expr);
8621 }
8622
8623 case ck_lvalue:
8624 return decay_conversion (expr, complain);
8625
8626 case ck_fnptr:
8627 /* ??? Should the address of a transaction-safe pointer point to the TM
8628 clone, and this conversion look up the primary function? */
8629 return build_nop (totype, expr);
8630
8631 case ck_qual:
8632 /* Warn about deprecated conversion if appropriate. */
8633 if (complain & tf_warning)
8634 {
8635 string_conv_p (totype, expr, 1);
8636 maybe_warn_array_conv (loc, convs, expr);
8637 }
8638 break;
8639
8640 case ck_ptr:
8641 if (convs->base_p)
8642 expr = convert_to_base (expr, totype, !c_cast_p,
8643 /*nonnull=*/false, complain);
8644 return build_nop (totype, expr);
8645
8646 case ck_pmem:
8647 return convert_ptrmem (totype, expr, /*allow_inverse_p=*/false,
8648 c_cast_p, complain);
8649
8650 default:
8651 break;
8652 }
8653
8654 if (convs->check_narrowing
8655 && !check_narrowing (totype, expr, complain,
8656 convs->check_narrowing_const_only))
8657 return error_mark_node;
8658
8659 warning_sentinel w (warn_zero_as_null_pointer_constant);
8660 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
8661 expr = TREE_OPERAND (expr, 0);
8662 if (issue_conversion_warnings)
8663 expr = cp_convert_and_check (totype, expr, complain);
8664 else
8665 expr = cp_convert (totype, expr, complain);
8666
8667 return expr;
8668 }
8669
8670 /* Return true if converting FROM to TO is unsafe in a template. */
8671
8672 static bool
8673 conv_unsafe_in_template_p (tree to, tree from)
8674 {
8675 /* Converting classes involves TARGET_EXPR. */
8676 if (CLASS_TYPE_P (to) || CLASS_TYPE_P (from))
8677 return true;
8678
8679 /* Converting real to integer produces FIX_TRUNC_EXPR which tsubst
8680 doesn't handle. */
8681 if (SCALAR_FLOAT_TYPE_P (from) && INTEGRAL_OR_ENUMERATION_TYPE_P (to))
8682 return true;
8683
8684 /* Converting integer to real isn't a trivial conversion, either. */
8685 if (INTEGRAL_OR_ENUMERATION_TYPE_P (from) && SCALAR_FLOAT_TYPE_P (to))
8686 return true;
8687
8688 return false;
8689 }
8690
8691 /* Wrapper for convert_like_internal that handles creating
8692 IMPLICIT_CONV_EXPR. */
8693
8694 static tree
8695 convert_like (conversion *convs, tree expr, tree fn, int argnum,
8696 bool issue_conversion_warnings, bool c_cast_p, bool nested_p,
8697 tsubst_flags_t complain)
8698 {
8699 /* Creating &TARGET_EXPR<> in a template breaks when substituting,
8700 and creating a CALL_EXPR in a template breaks in finish_call_expr
8701 so use an IMPLICIT_CONV_EXPR for this conversion. We would have
8702 created such codes e.g. when calling a user-defined conversion
8703 function. */
8704 tree conv_expr = NULL_TREE;
8705 if (processing_template_decl
8706 && convs->kind != ck_identity
8707 && conv_unsafe_in_template_p (convs->type, TREE_TYPE (expr)))
8708 {
8709 conv_expr = build1 (IMPLICIT_CONV_EXPR, convs->type, expr);
8710 if (convs->kind != ck_ref_bind)
8711 conv_expr = convert_from_reference (conv_expr);
8712 if (!convs->bad_p)
8713 return conv_expr;
8714 /* Do the normal processing to give the bad_p errors. But we still
8715 need to return the IMPLICIT_CONV_EXPR, unless we're returning
8716 error_mark_node. */
8717 }
8718 expr = convert_like_internal (convs, expr, fn, argnum,
8719 issue_conversion_warnings, c_cast_p,
8720 nested_p, complain);
8721 if (expr == error_mark_node)
8722 return error_mark_node;
8723 return conv_expr ? conv_expr : expr;
8724 }
8725
8726 /* Convenience wrapper for convert_like. */
8727
8728 static inline tree
8729 convert_like (conversion *convs, tree expr, tsubst_flags_t complain)
8730 {
8731 return convert_like (convs, expr, NULL_TREE, 0,
8732 /*issue_conversion_warnings=*/true,
8733 /*c_cast_p=*/false, /*nested_p=*/false, complain);
8734 }
8735
8736 /* Convenience wrapper for convert_like. */
8737
8738 static inline tree
8739 convert_like_with_context (conversion *convs, tree expr, tree fn, int argnum,
8740 tsubst_flags_t complain)
8741 {
8742 return convert_like (convs, expr, fn, argnum,
8743 /*issue_conversion_warnings=*/true,
8744 /*c_cast_p=*/false, /*nested_p=*/false, complain);
8745 }
8746
8747 /* ARG is being passed to a varargs function. Perform any conversions
8748 required. Return the converted value. */
8749
8750 tree
8751 convert_arg_to_ellipsis (tree arg, tsubst_flags_t complain)
8752 {
8753 tree arg_type = TREE_TYPE (arg);
8754 location_t loc = cp_expr_loc_or_input_loc (arg);
8755
8756 /* [expr.call]
8757
8758 If the argument has integral or enumeration type that is subject
8759 to the integral promotions (_conv.prom_), or a floating-point
8760 type that is subject to the floating-point promotion
8761 (_conv.fpprom_), the value of the argument is converted to the
8762 promoted type before the call. */
8763 if (TREE_CODE (arg_type) == REAL_TYPE
8764 && (TYPE_PRECISION (arg_type)
8765 < TYPE_PRECISION (double_type_node))
8766 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (arg_type))
8767 && !extended_float_type_p (arg_type))
8768 {
8769 if ((complain & tf_warning)
8770 && warn_double_promotion && !c_inhibit_evaluation_warnings)
8771 warning_at (loc, OPT_Wdouble_promotion,
8772 "implicit conversion from %qH to %qI when passing "
8773 "argument to function",
8774 arg_type, double_type_node);
8775 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
8776 arg = TREE_OPERAND (arg, 0);
8777 arg = mark_rvalue_use (arg);
8778 arg = convert_to_real_nofold (double_type_node, arg);
8779 }
8780 else if (NULLPTR_TYPE_P (arg_type))
8781 {
8782 arg = mark_rvalue_use (arg);
8783 if (TREE_SIDE_EFFECTS (arg))
8784 {
8785 warning_sentinel w(warn_unused_result);
8786 arg = cp_build_compound_expr (arg, null_pointer_node, complain);
8787 }
8788 else
8789 arg = null_pointer_node;
8790 }
8791 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (arg_type))
8792 {
8793 if (SCOPED_ENUM_P (arg_type))
8794 {
8795 tree prom = cp_convert (ENUM_UNDERLYING_TYPE (arg_type), arg,
8796 complain);
8797 prom = cp_perform_integral_promotions (prom, complain);
8798 if (abi_version_crosses (6)
8799 && TYPE_MODE (TREE_TYPE (prom)) != TYPE_MODE (arg_type)
8800 && (complain & tf_warning))
8801 warning_at (loc, OPT_Wabi, "scoped enum %qT passed through %<...%>"
8802 " as %qT before %<-fabi-version=6%>, %qT after",
8803 arg_type,
8804 TREE_TYPE (prom), ENUM_UNDERLYING_TYPE (arg_type));
8805 if (!abi_version_at_least (6))
8806 arg = prom;
8807 }
8808 else
8809 arg = cp_perform_integral_promotions (arg, complain);
8810 }
8811 else
8812 /* [expr.call]
8813
8814 The lvalue-to-rvalue, array-to-pointer, and function-to-pointer
8815 standard conversions are performed. */
8816 arg = decay_conversion (arg, complain);
8817
8818 arg = require_complete_type (arg, complain);
8819 arg_type = TREE_TYPE (arg);
8820
8821 if (arg != error_mark_node
8822 /* In a template (or ill-formed code), we can have an incomplete type
8823 even after require_complete_type, in which case we don't know
8824 whether it has trivial copy or not. */
8825 && COMPLETE_TYPE_P (arg_type)
8826 && !cp_unevaluated_operand)
8827 {
8828 /* [expr.call] 5.2.2/7:
8829 Passing a potentially-evaluated argument of class type (Clause 9)
8830 with a non-trivial copy constructor or a non-trivial destructor
8831 with no corresponding parameter is conditionally-supported, with
8832 implementation-defined semantics.
8833
8834 We support it as pass-by-invisible-reference, just like a normal
8835 value parameter.
8836
8837 If the call appears in the context of a sizeof expression,
8838 it is not potentially-evaluated. */
8839 if (type_has_nontrivial_copy_init (arg_type)
8840 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (arg_type))
8841 {
8842 arg = force_rvalue (arg, complain);
8843 if (complain & tf_warning)
8844 warning (OPT_Wconditionally_supported,
8845 "passing objects of non-trivially-copyable "
8846 "type %q#T through %<...%> is conditionally supported",
8847 arg_type);
8848 return build1 (ADDR_EXPR, build_reference_type (arg_type), arg);
8849 }
8850 /* Build up a real lvalue-to-rvalue conversion in case the
8851 copy constructor is trivial but not callable. */
8852 else if (CLASS_TYPE_P (arg_type))
8853 force_rvalue (arg, complain);
8854
8855 }
8856
8857 return arg;
8858 }
8859
8860 /* va_arg (EXPR, TYPE) is a builtin. Make sure it is not abused. */
8861
8862 tree
8863 build_x_va_arg (location_t loc, tree expr, tree type)
8864 {
8865 if (processing_template_decl)
8866 {
8867 tree r = build_min (VA_ARG_EXPR, type, expr);
8868 SET_EXPR_LOCATION (r, loc);
8869 return r;
8870 }
8871
8872 type = complete_type_or_else (type, NULL_TREE);
8873
8874 if (expr == error_mark_node || !type)
8875 return error_mark_node;
8876
8877 expr = mark_lvalue_use (expr);
8878
8879 if (TYPE_REF_P (type))
8880 {
8881 error ("cannot receive reference type %qT through %<...%>", type);
8882 return error_mark_node;
8883 }
8884
8885 if (type_has_nontrivial_copy_init (type)
8886 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
8887 {
8888 /* conditionally-supported behavior [expr.call] 5.2.2/7. Let's treat
8889 it as pass by invisible reference. */
8890 warning_at (loc, OPT_Wconditionally_supported,
8891 "receiving objects of non-trivially-copyable type %q#T "
8892 "through %<...%> is conditionally-supported", type);
8893
8894 tree ref = cp_build_reference_type (type, false);
8895 expr = build_va_arg (loc, expr, ref);
8896 return convert_from_reference (expr);
8897 }
8898
8899 tree ret = build_va_arg (loc, expr, type);
8900 if (CLASS_TYPE_P (type))
8901 /* Wrap the VA_ARG_EXPR in a TARGET_EXPR now so other code doesn't need to
8902 know how to handle it. */
8903 ret = get_target_expr (ret);
8904 return ret;
8905 }
8906
8907 /* TYPE has been given to va_arg. Apply the default conversions which
8908 would have happened when passed via ellipsis. Return the promoted
8909 type, or the passed type if there is no change. */
8910
8911 tree
8912 cxx_type_promotes_to (tree type)
8913 {
8914 tree promote;
8915
8916 /* Perform the array-to-pointer and function-to-pointer
8917 conversions. */
8918 type = type_decays_to (type);
8919
8920 promote = type_promotes_to (type);
8921 if (same_type_p (type, promote))
8922 promote = type;
8923
8924 return promote;
8925 }
8926
8927 /* ARG is a default argument expression being passed to a parameter of
8928 the indicated TYPE, which is a parameter to FN. PARMNUM is the
8929 zero-based argument number. Do any required conversions. Return
8930 the converted value. */
8931
8932 static GTY(()) vec<tree, va_gc> *default_arg_context;
8933 void
8934 push_defarg_context (tree fn)
8935 { vec_safe_push (default_arg_context, fn); }
8936
8937 void
8938 pop_defarg_context (void)
8939 { default_arg_context->pop (); }
8940
8941 tree
8942 convert_default_arg (tree type, tree arg, tree fn, int parmnum,
8943 tsubst_flags_t complain)
8944 {
8945 int i;
8946 tree t;
8947
8948 /* See through clones. */
8949 fn = DECL_ORIGIN (fn);
8950 /* And inheriting ctors. */
8951 if (flag_new_inheriting_ctors)
8952 fn = strip_inheriting_ctors (fn);
8953
8954 /* Detect recursion. */
8955 FOR_EACH_VEC_SAFE_ELT (default_arg_context, i, t)
8956 if (t == fn)
8957 {
8958 if (complain & tf_error)
8959 error ("recursive evaluation of default argument for %q#D", fn);
8960 return error_mark_node;
8961 }
8962
8963 /* If the ARG is an unparsed default argument expression, the
8964 conversion cannot be performed. */
8965 if (TREE_CODE (arg) == DEFERRED_PARSE)
8966 {
8967 if (complain & tf_error)
8968 error ("call to %qD uses the default argument for parameter %P, which "
8969 "is not yet defined", fn, parmnum);
8970 return error_mark_node;
8971 }
8972
8973 push_defarg_context (fn);
8974
8975 if (fn && DECL_TEMPLATE_INFO (fn))
8976 arg = tsubst_default_argument (fn, parmnum, type, arg, complain);
8977
8978 /* Due to:
8979
8980 [dcl.fct.default]
8981
8982 The names in the expression are bound, and the semantic
8983 constraints are checked, at the point where the default
8984 expressions appears.
8985
8986 we must not perform access checks here. */
8987 push_deferring_access_checks (dk_no_check);
8988 /* We must make a copy of ARG, in case subsequent processing
8989 alters any part of it. */
8990 arg = break_out_target_exprs (arg, /*clear location*/true);
8991
8992 arg = convert_for_initialization (0, type, arg, LOOKUP_IMPLICIT,
8993 ICR_DEFAULT_ARGUMENT, fn, parmnum,
8994 complain);
8995 arg = convert_for_arg_passing (type, arg, complain);
8996 pop_deferring_access_checks();
8997
8998 pop_defarg_context ();
8999
9000 return arg;
9001 }
9002
9003 /* Returns the type which will really be used for passing an argument of
9004 type TYPE. */
9005
9006 tree
9007 type_passed_as (tree type)
9008 {
9009 /* Pass classes with copy ctors by invisible reference. */
9010 if (TREE_ADDRESSABLE (type))
9011 type = build_reference_type (type);
9012 else if (targetm.calls.promote_prototypes (NULL_TREE)
9013 && INTEGRAL_TYPE_P (type)
9014 && COMPLETE_TYPE_P (type)
9015 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
9016 type = integer_type_node;
9017
9018 return type;
9019 }
9020
9021 /* Actually perform the appropriate conversion. */
9022
9023 tree
9024 convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
9025 {
9026 tree bitfield_type;
9027
9028 /* If VAL is a bitfield, then -- since it has already been converted
9029 to TYPE -- it cannot have a precision greater than TYPE.
9030
9031 If it has a smaller precision, we must widen it here. For
9032 example, passing "int f:3;" to a function expecting an "int" will
9033 not result in any conversion before this point.
9034
9035 If the precision is the same we must not risk widening. For
9036 example, the COMPONENT_REF for a 32-bit "long long" bitfield will
9037 often have type "int", even though the C++ type for the field is
9038 "long long". If the value is being passed to a function
9039 expecting an "int", then no conversions will be required. But,
9040 if we call convert_bitfield_to_declared_type, the bitfield will
9041 be converted to "long long". */
9042 bitfield_type = is_bitfield_expr_with_lowered_type (val);
9043 if (bitfield_type
9044 && TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type))
9045 val = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type), val);
9046
9047 if (val == error_mark_node)
9048 ;
9049 /* Pass classes with copy ctors by invisible reference. */
9050 else if (TREE_ADDRESSABLE (type))
9051 val = build1 (ADDR_EXPR, build_reference_type (type), val);
9052 else if (targetm.calls.promote_prototypes (NULL_TREE)
9053 && INTEGRAL_TYPE_P (type)
9054 && COMPLETE_TYPE_P (type)
9055 && tree_int_cst_lt (TYPE_SIZE (type), TYPE_SIZE (integer_type_node)))
9056 val = cp_perform_integral_promotions (val, complain);
9057 if (complain & tf_warning)
9058 {
9059 if (warn_suggest_attribute_format)
9060 {
9061 tree rhstype = TREE_TYPE (val);
9062 const enum tree_code coder = TREE_CODE (rhstype);
9063 const enum tree_code codel = TREE_CODE (type);
9064 if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
9065 && coder == codel
9066 && check_missing_format_attribute (type, rhstype))
9067 warning (OPT_Wsuggest_attribute_format,
9068 "argument of function call might be a candidate "
9069 "for a format attribute");
9070 }
9071 maybe_warn_parm_abi (type, cp_expr_loc_or_input_loc (val));
9072 }
9073
9074 if (complain & tf_warning)
9075 warn_for_address_or_pointer_of_packed_member (type, val);
9076
9077 return val;
9078 }
9079
9080 /* Returns non-zero iff FN is a function with magic varargs, i.e. ones for
9081 which just decay_conversion or no conversions at all should be done.
9082 This is true for some builtins which don't act like normal functions.
9083 Return 2 if just decay_conversion and removal of excess precision should
9084 be done, 1 if just decay_conversion. Return 3 for special treatment of
9085 the 3rd argument for __builtin_*_overflow_p. */
9086
9087 int
9088 magic_varargs_p (tree fn)
9089 {
9090 if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
9091 switch (DECL_FUNCTION_CODE (fn))
9092 {
9093 case BUILT_IN_CLASSIFY_TYPE:
9094 case BUILT_IN_CONSTANT_P:
9095 case BUILT_IN_NEXT_ARG:
9096 case BUILT_IN_VA_START:
9097 return 1;
9098
9099 case BUILT_IN_ADD_OVERFLOW_P:
9100 case BUILT_IN_SUB_OVERFLOW_P:
9101 case BUILT_IN_MUL_OVERFLOW_P:
9102 return 3;
9103
9104 case BUILT_IN_ISFINITE:
9105 case BUILT_IN_ISINF:
9106 case BUILT_IN_ISINF_SIGN:
9107 case BUILT_IN_ISNAN:
9108 case BUILT_IN_ISNORMAL:
9109 case BUILT_IN_FPCLASSIFY:
9110 return 2;
9111
9112 default:
9113 return lookup_attribute ("type generic",
9114 TYPE_ATTRIBUTES (TREE_TYPE (fn))) != 0;
9115 }
9116
9117 return 0;
9118 }
9119
9120 /* Returns the decl of the dispatcher function if FN is a function version. */
9121
9122 tree
9123 get_function_version_dispatcher (tree fn)
9124 {
9125 tree dispatcher_decl = NULL;
9126
9127 if (DECL_LOCAL_DECL_P (fn))
9128 fn = DECL_LOCAL_DECL_ALIAS (fn);
9129
9130 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
9131 && DECL_FUNCTION_VERSIONED (fn));
9132
9133 gcc_assert (targetm.get_function_versions_dispatcher);
9134 dispatcher_decl = targetm.get_function_versions_dispatcher (fn);
9135
9136 if (dispatcher_decl == NULL)
9137 {
9138 error_at (input_location, "use of multiversioned function "
9139 "without a default");
9140 return NULL;
9141 }
9142
9143 retrofit_lang_decl (dispatcher_decl);
9144 gcc_assert (dispatcher_decl != NULL);
9145 return dispatcher_decl;
9146 }
9147
9148 /* fn is a function version dispatcher that is marked used. Mark all the
9149 semantically identical function versions it will dispatch as used. */
9150
9151 void
9152 mark_versions_used (tree fn)
9153 {
9154 struct cgraph_node *node;
9155 struct cgraph_function_version_info *node_v;
9156 struct cgraph_function_version_info *it_v;
9157
9158 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
9159
9160 node = cgraph_node::get (fn);
9161 if (node == NULL)
9162 return;
9163
9164 gcc_assert (node->dispatcher_function);
9165
9166 node_v = node->function_version ();
9167 if (node_v == NULL)
9168 return;
9169
9170 /* All semantically identical versions are chained. Traverse and mark each
9171 one of them as used. */
9172 it_v = node_v->next;
9173 while (it_v != NULL)
9174 {
9175 mark_used (it_v->this_node->decl);
9176 it_v = it_v->next;
9177 }
9178 }
9179
9180 /* Build a call to "the copy constructor" for the type of A, even if it
9181 wouldn't be selected by normal overload resolution. Used for
9182 diagnostics. */
9183
9184 static tree
9185 call_copy_ctor (tree a, tsubst_flags_t complain)
9186 {
9187 tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (a));
9188 tree binfo = TYPE_BINFO (ctype);
9189 tree copy = get_copy_ctor (ctype, complain);
9190 copy = build_baselink (binfo, binfo, copy, NULL_TREE);
9191 tree ob = build_dummy_object (ctype);
9192 releasing_vec args (make_tree_vector_single (a));
9193 tree r = build_new_method_call (ob, copy, &args, NULL_TREE,
9194 LOOKUP_NORMAL, NULL, complain);
9195 return r;
9196 }
9197
9198 /* Return the base constructor corresponding to COMPLETE_CTOR or NULL_TREE. */
9199
9200 static tree
9201 base_ctor_for (tree complete_ctor)
9202 {
9203 tree clone;
9204 FOR_EACH_CLONE (clone, DECL_CLONED_FUNCTION (complete_ctor))
9205 if (DECL_BASE_CONSTRUCTOR_P (clone))
9206 return clone;
9207 return NULL_TREE;
9208 }
9209
9210 /* Try to make EXP suitable to be used as the initializer for a base subobject,
9211 and return whether we were successful. EXP must have already been cleared
9212 by unsafe_copy_elision_p{,_opt}. */
9213
9214 static bool
9215 make_base_init_ok (tree exp)
9216 {
9217 if (TREE_CODE (exp) == TARGET_EXPR)
9218 exp = TARGET_EXPR_INITIAL (exp);
9219 while (TREE_CODE (exp) == COMPOUND_EXPR)
9220 exp = TREE_OPERAND (exp, 1);
9221 if (TREE_CODE (exp) == COND_EXPR)
9222 {
9223 bool ret = make_base_init_ok (TREE_OPERAND (exp, 2));
9224 if (tree op1 = TREE_OPERAND (exp, 1))
9225 {
9226 bool r1 = make_base_init_ok (op1);
9227 /* If unsafe_copy_elision_p was false, the arms should match. */
9228 gcc_assert (r1 == ret);
9229 }
9230 return ret;
9231 }
9232 if (TREE_CODE (exp) != AGGR_INIT_EXPR)
9233 /* A trivial copy is OK. */
9234 return true;
9235 if (!AGGR_INIT_VIA_CTOR_P (exp))
9236 /* unsafe_copy_elision_p_opt must have said this is OK. */
9237 return true;
9238 tree fn = cp_get_callee_fndecl_nofold (exp);
9239 if (DECL_BASE_CONSTRUCTOR_P (fn))
9240 return true;
9241 gcc_assert (DECL_COMPLETE_CONSTRUCTOR_P (fn));
9242 fn = base_ctor_for (fn);
9243 if (!fn || DECL_HAS_VTT_PARM_P (fn))
9244 /* The base constructor has more parameters, so we can't just change the
9245 call target. It would be possible to splice in the appropriate
9246 arguments, but probably not worth the complexity. */
9247 return false;
9248 mark_used (fn);
9249 AGGR_INIT_EXPR_FN (exp) = build_address (fn);
9250 return true;
9251 }
9252
9253 /* Return 2 if T refers to a base, 1 if a potentially-overlapping field,
9254 neither of which can be used for return by invisible reference. We avoid
9255 doing C++17 mandatory copy elision for either of these cases.
9256
9257 This returns non-zero even if the type of T has no tail padding that other
9258 data could be allocated into, because that depends on the particular ABI.
9259 unsafe_copy_elision_p_opt does consider whether there is padding. */
9260
9261 int
9262 unsafe_return_slot_p (tree t)
9263 {
9264 /* Check empty bases separately, they don't have fields. */
9265 if (is_empty_base_ref (t))
9266 return 2;
9267
9268 /* A delegating constructor might be used to initialize a base. */
9269 if (current_function_decl
9270 && DECL_CONSTRUCTOR_P (current_function_decl)
9271 && (t == current_class_ref
9272 || tree_strip_nop_conversions (t) == current_class_ptr))
9273 return 2;
9274
9275 STRIP_NOPS (t);
9276 if (TREE_CODE (t) == ADDR_EXPR)
9277 t = TREE_OPERAND (t, 0);
9278 if (TREE_CODE (t) == COMPONENT_REF)
9279 t = TREE_OPERAND (t, 1);
9280 if (TREE_CODE (t) != FIELD_DECL)
9281 return false;
9282 if (!CLASS_TYPE_P (TREE_TYPE (t)))
9283 /* The middle-end will do the right thing for scalar types. */
9284 return false;
9285 if (DECL_FIELD_IS_BASE (t))
9286 return 2;
9287 if (lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (t)))
9288 return 1;
9289 return 0;
9290 }
9291
9292 /* True IFF EXP is a prvalue that represents return by invisible reference. */
9293
9294 static bool
9295 init_by_return_slot_p (tree exp)
9296 {
9297 /* Copy elision only happens with a TARGET_EXPR. */
9298 if (TREE_CODE (exp) != TARGET_EXPR)
9299 return false;
9300 tree init = TARGET_EXPR_INITIAL (exp);
9301 /* build_compound_expr pushes COMPOUND_EXPR inside TARGET_EXPR. */
9302 while (TREE_CODE (init) == COMPOUND_EXPR)
9303 init = TREE_OPERAND (init, 1);
9304 if (TREE_CODE (init) == COND_EXPR)
9305 {
9306 /* We'll end up copying from each of the arms of the COND_EXPR directly
9307 into the target, so look at them. */
9308 if (tree op = TREE_OPERAND (init, 1))
9309 if (init_by_return_slot_p (op))
9310 return true;
9311 return init_by_return_slot_p (TREE_OPERAND (init, 2));
9312 }
9313 return (TREE_CODE (init) == AGGR_INIT_EXPR
9314 && !AGGR_INIT_VIA_CTOR_P (init));
9315 }
9316
9317 /* We can't elide a copy from a function returning by value to a
9318 potentially-overlapping subobject, as the callee might clobber tail padding.
9319 Return true iff this could be that case.
9320
9321 Places that use this function (or _opt) to decide to elide a copy should
9322 probably use make_safe_copy_elision instead. */
9323
9324 bool
9325 unsafe_copy_elision_p (tree target, tree exp)
9326 {
9327 return unsafe_return_slot_p (target) && init_by_return_slot_p (exp);
9328 }
9329
9330 /* As above, but for optimization allow more cases that are actually safe. */
9331
9332 static bool
9333 unsafe_copy_elision_p_opt (tree target, tree exp)
9334 {
9335 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
9336 /* It's safe to elide the copy for a class with no tail padding. */
9337 if (!is_empty_class (type)
9338 && tree_int_cst_equal (TYPE_SIZE (type), CLASSTYPE_SIZE (type)))
9339 return false;
9340 return unsafe_copy_elision_p (target, exp);
9341 }
9342
9343 /* Try to make EXP suitable to be used as the initializer for TARGET,
9344 and return whether we were successful. */
9345
9346 bool
9347 make_safe_copy_elision (tree target, tree exp)
9348 {
9349 int uns = unsafe_return_slot_p (target);
9350 if (!uns)
9351 return true;
9352 if (init_by_return_slot_p (exp))
9353 return false;
9354 if (uns == 1)
9355 return true;
9356 return make_base_init_ok (exp);
9357 }
9358
9359 /* True IFF the result of the conversion C is a prvalue. */
9360
9361 static bool
9362 conv_is_prvalue (conversion *c)
9363 {
9364 if (c->kind == ck_rvalue)
9365 return true;
9366 if (c->kind == ck_base && c->need_temporary_p)
9367 return true;
9368 if (c->kind == ck_user && !TYPE_REF_P (c->type))
9369 return true;
9370 if (c->kind == ck_identity && c->u.expr
9371 && TREE_CODE (c->u.expr) == TARGET_EXPR)
9372 return true;
9373
9374 return false;
9375 }
9376
9377 /* True iff C is a conversion that binds a reference to a prvalue. */
9378
9379 static bool
9380 conv_binds_ref_to_prvalue (conversion *c)
9381 {
9382 if (c->kind != ck_ref_bind)
9383 return false;
9384 if (c->need_temporary_p)
9385 return true;
9386
9387 return conv_is_prvalue (next_conversion (c));
9388 }
9389
9390 /* True iff EXPR represents a (subobject of a) temporary. */
9391
9392 static bool
9393 expr_represents_temporary_p (tree expr)
9394 {
9395 while (handled_component_p (expr))
9396 expr = TREE_OPERAND (expr, 0);
9397 return TREE_CODE (expr) == TARGET_EXPR;
9398 }
9399
9400 /* True iff C is a conversion that binds a reference to a temporary.
9401 This is a superset of conv_binds_ref_to_prvalue: here we're also
9402 interested in xvalues. */
9403
9404 static bool
9405 conv_binds_ref_to_temporary (conversion *c)
9406 {
9407 if (conv_binds_ref_to_prvalue (c))
9408 return true;
9409 if (c->kind != ck_ref_bind)
9410 return false;
9411 c = next_conversion (c);
9412 /* This is the case for
9413 struct Base {};
9414 struct Derived : Base {};
9415 const Base& b(Derived{});
9416 where we bind 'b' to the Base subobject of a temporary object of type
9417 Derived. The subobject is an xvalue; the whole object is a prvalue.
9418
9419 The ck_base doesn't have to be present for cases like X{}.m. */
9420 if (c->kind == ck_base)
9421 c = next_conversion (c);
9422 if (c->kind == ck_identity && c->u.expr
9423 && expr_represents_temporary_p (c->u.expr))
9424 return true;
9425 return false;
9426 }
9427
9428 /* Return tristate::TS_TRUE if converting EXPR to a reference type TYPE binds
9429 the reference to a temporary. Return tristate::TS_FALSE if converting
9430 EXPR to a reference type TYPE doesn't bind the reference to a temporary. If
9431 the conversion is invalid or bad, return tristate::TS_UNKNOWN. DIRECT_INIT_P
9432 says whether the conversion should be done in direct- or copy-initialization
9433 context. */
9434
9435 tristate
9436 ref_conv_binds_to_temporary (tree type, tree expr, bool direct_init_p/*=false*/)
9437 {
9438 gcc_assert (TYPE_REF_P (type));
9439
9440 /* Get the high-water mark for the CONVERSION_OBSTACK. */
9441 void *p = conversion_obstack_alloc (0);
9442
9443 const int flags = direct_init_p ? LOOKUP_NORMAL : LOOKUP_IMPLICIT;
9444 conversion *conv = implicit_conversion (type, TREE_TYPE (expr), expr,
9445 /*c_cast_p=*/false, flags, tf_none);
9446 tristate ret (tristate::TS_UNKNOWN);
9447 if (conv && !conv->bad_p)
9448 ret = tristate (conv_binds_ref_to_temporary (conv));
9449
9450 /* Free all the conversions we allocated. */
9451 obstack_free (&conversion_obstack, p);
9452
9453 return ret;
9454 }
9455
9456 /* Call the trivial destructor for INSTANCE, which can be either an lvalue of
9457 class type or a pointer to class type. If NO_PTR_DEREF is true and
9458 INSTANCE has pointer type, clobber the pointer rather than what it points
9459 to. */
9460
9461 tree
9462 build_trivial_dtor_call (tree instance, bool no_ptr_deref)
9463 {
9464 gcc_assert (!is_dummy_object (instance));
9465
9466 if (!flag_lifetime_dse)
9467 {
9468 no_clobber:
9469 return fold_convert (void_type_node, instance);
9470 }
9471
9472 if (INDIRECT_TYPE_P (TREE_TYPE (instance))
9473 && (!no_ptr_deref || TYPE_REF_P (TREE_TYPE (instance))))
9474 {
9475 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
9476 goto no_clobber;
9477 instance = cp_build_fold_indirect_ref (instance);
9478 }
9479
9480 /* A trivial destructor should still clobber the object. */
9481 tree clobber = build_clobber (TREE_TYPE (instance));
9482 return build2 (MODIFY_EXPR, void_type_node,
9483 instance, clobber);
9484 }
9485
9486 /* Return true if in an immediate function context, or an unevaluated operand,
9487 or a default argument/member initializer, or a subexpression of an immediate
9488 invocation. */
9489
9490 bool
9491 in_immediate_context ()
9492 {
9493 return (cp_unevaluated_operand != 0
9494 || (current_function_decl != NULL_TREE
9495 && DECL_IMMEDIATE_FUNCTION_P (current_function_decl))
9496 /* DR 2631: default args and DMI aren't immediately evaluated.
9497 Return true here so immediate_invocation_p returns false. */
9498 || current_binding_level->kind == sk_function_parms
9499 || current_binding_level->kind == sk_template_parms
9500 || parsing_nsdmi ()
9501 || in_consteval_if_p);
9502 }
9503
9504 /* Return true if a call to FN with number of arguments NARGS
9505 is an immediate invocation. */
9506
9507 static bool
9508 immediate_invocation_p (tree fn)
9509 {
9510 return (TREE_CODE (fn) == FUNCTION_DECL
9511 && DECL_IMMEDIATE_FUNCTION_P (fn)
9512 && !in_immediate_context ());
9513 }
9514
9515 /* Subroutine of the various build_*_call functions. Overload resolution
9516 has chosen a winning candidate CAND; build up a CALL_EXPR accordingly.
9517 ARGS is a TREE_LIST of the unconverted arguments to the call. FLAGS is a
9518 bitmask of various LOOKUP_* flags which apply to the call itself. */
9519
9520 static tree
9521 build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
9522 {
9523 tree fn = cand->fn;
9524 const vec<tree, va_gc> *args = cand->args;
9525 tree first_arg = cand->first_arg;
9526 conversion **convs = cand->convs;
9527 conversion *conv;
9528 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
9529 int parmlen;
9530 tree val;
9531 int i = 0;
9532 int j = 0;
9533 unsigned int arg_index = 0;
9534 int is_method = 0;
9535 int nargs;
9536 tree *argarray;
9537 bool already_used = false;
9538
9539 /* In a template, there is no need to perform all of the work that
9540 is normally done. We are only interested in the type of the call
9541 expression, i.e., the return type of the function. Any semantic
9542 errors will be deferred until the template is instantiated. */
9543 if (processing_template_decl)
9544 {
9545 if (undeduced_auto_decl (fn))
9546 mark_used (fn, complain);
9547 else
9548 /* Otherwise set TREE_USED for the benefit of -Wunused-function.
9549 See PR80598. */
9550 TREE_USED (fn) = 1;
9551
9552 tree return_type = TREE_TYPE (TREE_TYPE (fn));
9553 tree callee;
9554 if (first_arg == NULL_TREE)
9555 {
9556 callee = build_addr_func (fn, complain);
9557 if (callee == error_mark_node)
9558 return error_mark_node;
9559 }
9560 else
9561 {
9562 callee = build_baselink (cand->conversion_path, cand->access_path,
9563 fn, NULL_TREE);
9564 callee = build_min (COMPONENT_REF, TREE_TYPE (fn),
9565 first_arg, callee, NULL_TREE);
9566 }
9567
9568 tree expr = build_call_vec (return_type, callee, args);
9569 SET_EXPR_LOCATION (expr, input_location);
9570 if (TREE_THIS_VOLATILE (fn) && cfun)
9571 current_function_returns_abnormally = 1;
9572 if (immediate_invocation_p (fn))
9573 {
9574 tree obj_arg = NULL_TREE, exprimm = expr;
9575 if (DECL_CONSTRUCTOR_P (fn))
9576 obj_arg = first_arg;
9577 if (obj_arg
9578 && is_dummy_object (obj_arg)
9579 && !type_dependent_expression_p (obj_arg))
9580 {
9581 exprimm = build_cplus_new (DECL_CONTEXT (fn), expr, complain);
9582 obj_arg = NULL_TREE;
9583 }
9584 /* Look through *(const T *)&obj. */
9585 else if (obj_arg && TREE_CODE (obj_arg) == INDIRECT_REF)
9586 {
9587 tree addr = TREE_OPERAND (obj_arg, 0);
9588 STRIP_NOPS (addr);
9589 if (TREE_CODE (addr) == ADDR_EXPR)
9590 {
9591 tree typeo = TREE_TYPE (obj_arg);
9592 tree typei = TREE_TYPE (TREE_OPERAND (addr, 0));
9593 if (same_type_ignoring_top_level_qualifiers_p (typeo, typei))
9594 obj_arg = TREE_OPERAND (addr, 0);
9595 }
9596 }
9597 fold_non_dependent_expr (exprimm, complain,
9598 /*manifestly_const_eval=*/true,
9599 obj_arg);
9600 }
9601 return convert_from_reference (expr);
9602 }
9603
9604 /* Give any warnings we noticed during overload resolution. */
9605 if (cand->warnings && (complain & tf_warning))
9606 {
9607 struct candidate_warning *w;
9608 for (w = cand->warnings; w; w = w->next)
9609 joust (cand, w->loser, 1, complain);
9610 }
9611
9612 /* Core issue 2327: P0135 doesn't say how to handle the case where the
9613 argument to the copy constructor ends up being a prvalue after
9614 conversion. Let's do the normal processing, but pretend we aren't
9615 actually using the copy constructor. */
9616 bool force_elide = false;
9617 if (cxx_dialect >= cxx17
9618 && cand->num_convs == 1
9619 && DECL_COMPLETE_CONSTRUCTOR_P (fn)
9620 && (DECL_COPY_CONSTRUCTOR_P (fn)
9621 || DECL_MOVE_CONSTRUCTOR_P (fn))
9622 && !unsafe_return_slot_p (first_arg)
9623 && conv_binds_ref_to_prvalue (convs[0]))
9624 {
9625 force_elide = true;
9626 goto not_really_used;
9627 }
9628
9629 /* OK, we're actually calling this inherited constructor; set its deletedness
9630 appropriately. We can get away with doing this here because calling is
9631 the only way to refer to a constructor. */
9632 if (DECL_INHERITED_CTOR (fn)
9633 && !deduce_inheriting_ctor (fn))
9634 {
9635 if (complain & tf_error)
9636 mark_used (fn);
9637 return error_mark_node;
9638 }
9639
9640 /* Make =delete work with SFINAE. */
9641 if (DECL_DELETED_FN (fn))
9642 {
9643 if (complain & tf_error)
9644 mark_used (fn);
9645 return error_mark_node;
9646 }
9647
9648 if (DECL_FUNCTION_MEMBER_P (fn))
9649 {
9650 tree access_fn;
9651 /* If FN is a template function, two cases must be considered.
9652 For example:
9653
9654 struct A {
9655 protected:
9656 template <class T> void f();
9657 };
9658 template <class T> struct B {
9659 protected:
9660 void g();
9661 };
9662 struct C : A, B<int> {
9663 using A::f; // #1
9664 using B<int>::g; // #2
9665 };
9666
9667 In case #1 where `A::f' is a member template, DECL_ACCESS is
9668 recorded in the primary template but not in its specialization.
9669 We check access of FN using its primary template.
9670
9671 In case #2, where `B<int>::g' has a DECL_TEMPLATE_INFO simply
9672 because it is a member of class template B, DECL_ACCESS is
9673 recorded in the specialization `B<int>::g'. We cannot use its
9674 primary template because `B<T>::g' and `B<int>::g' may have
9675 different access. */
9676 if (DECL_TEMPLATE_INFO (fn)
9677 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
9678 access_fn = DECL_TI_TEMPLATE (fn);
9679 else
9680 access_fn = fn;
9681 if (!perform_or_defer_access_check (cand->access_path, access_fn,
9682 fn, complain))
9683 return error_mark_node;
9684 }
9685
9686 /* If we're checking for implicit delete, don't bother with argument
9687 conversions. */
9688 if (flags & LOOKUP_SPECULATIVE)
9689 {
9690 if (cand->viable == 1)
9691 return fn;
9692 else if (!(complain & tf_error))
9693 /* Reject bad conversions now. */
9694 return error_mark_node;
9695 /* else continue to get conversion error. */
9696 }
9697
9698 not_really_used:
9699
9700 /* N3276 magic doesn't apply to nested calls. */
9701 tsubst_flags_t decltype_flag = (complain & tf_decltype);
9702 complain &= ~tf_decltype;
9703 /* No-Cleanup doesn't apply to nested calls either. */
9704 tsubst_flags_t no_cleanup_complain = complain;
9705 complain &= ~tf_no_cleanup;
9706
9707 /* Find maximum size of vector to hold converted arguments. */
9708 parmlen = list_length (parm);
9709 nargs = vec_safe_length (args) + (first_arg != NULL_TREE ? 1 : 0);
9710 if (parmlen > nargs)
9711 nargs = parmlen;
9712 argarray = XALLOCAVEC (tree, nargs);
9713
9714 in_consteval_if_p_temp_override icip;
9715 /* If the call is immediate function invocation, make sure
9716 taking address of immediate functions is allowed in its arguments. */
9717 if (immediate_invocation_p (STRIP_TEMPLATE (fn)))
9718 in_consteval_if_p = true;
9719
9720 /* The implicit parameters to a constructor are not considered by overload
9721 resolution, and must be of the proper type. */
9722 if (DECL_CONSTRUCTOR_P (fn))
9723 {
9724 tree object_arg;
9725 if (first_arg != NULL_TREE)
9726 {
9727 object_arg = first_arg;
9728 first_arg = NULL_TREE;
9729 }
9730 else
9731 {
9732 object_arg = (*args)[arg_index];
9733 ++arg_index;
9734 }
9735 argarray[j++] = build_this (object_arg);
9736 parm = TREE_CHAIN (parm);
9737 /* We should never try to call the abstract constructor. */
9738 gcc_assert (!DECL_HAS_IN_CHARGE_PARM_P (fn));
9739
9740 if (DECL_HAS_VTT_PARM_P (fn))
9741 {
9742 argarray[j++] = (*args)[arg_index];
9743 ++arg_index;
9744 parm = TREE_CHAIN (parm);
9745 }
9746 }
9747 /* Bypass access control for 'this' parameter. */
9748 else if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
9749 {
9750 tree arg = build_this (first_arg != NULL_TREE
9751 ? first_arg
9752 : (*args)[arg_index]);
9753 tree argtype = TREE_TYPE (arg);
9754
9755 if (arg == error_mark_node)
9756 return error_mark_node;
9757
9758 if (convs[i]->bad_p)
9759 {
9760 if (complain & tf_error)
9761 {
9762 auto_diagnostic_group d;
9763 if (permerror (input_location, "passing %qT as %<this%> "
9764 "argument discards qualifiers",
9765 TREE_TYPE (argtype)))
9766 inform (DECL_SOURCE_LOCATION (fn), " in call to %qD", fn);
9767 }
9768 else
9769 return error_mark_node;
9770 }
9771
9772 /* The class where FN is defined. */
9773 tree ctx = DECL_CONTEXT (fn);
9774
9775 /* See if the function member or the whole class type is declared
9776 final and the call can be devirtualized. */
9777 if (DECL_FINAL_P (fn) || CLASSTYPE_FINAL (ctx))
9778 flags |= LOOKUP_NONVIRTUAL;
9779
9780 /* [class.mfct.non-static]: If a non-static member function of a class
9781 X is called for an object that is not of type X, or of a type
9782 derived from X, the behavior is undefined.
9783
9784 So we can assume that anything passed as 'this' is non-null, and
9785 optimize accordingly. */
9786 /* Check that the base class is accessible. */
9787 if (!accessible_base_p (TREE_TYPE (argtype),
9788 BINFO_TYPE (cand->conversion_path), true))
9789 {
9790 if (complain & tf_error)
9791 error ("%qT is not an accessible base of %qT",
9792 BINFO_TYPE (cand->conversion_path),
9793 TREE_TYPE (argtype));
9794 else
9795 return error_mark_node;
9796 }
9797 /* If fn was found by a using declaration, the conversion path
9798 will be to the derived class, not the base declaring fn. We
9799 must convert to the base. */
9800 tree base_binfo = cand->conversion_path;
9801 if (BINFO_TYPE (base_binfo) != ctx)
9802 {
9803 base_binfo = lookup_base (base_binfo, ctx, ba_unique, NULL, complain);
9804 if (base_binfo == error_mark_node)
9805 return error_mark_node;
9806 }
9807
9808 /* If we know the dynamic type of the object, look up the final overrider
9809 in the BINFO. */
9810 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
9811 && resolves_to_fixed_type_p (arg))
9812 {
9813 tree ov = lookup_vfn_in_binfo (DECL_VINDEX (fn), base_binfo);
9814
9815 /* And unwind base_binfo to match. If we don't find the type we're
9816 looking for in BINFO_INHERITANCE_CHAIN, we're looking at diamond
9817 inheritance; for now do a normal virtual call in that case. */
9818 tree octx = DECL_CONTEXT (ov);
9819 tree obinfo = base_binfo;
9820 while (obinfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (obinfo), octx))
9821 obinfo = BINFO_INHERITANCE_CHAIN (obinfo);
9822 if (obinfo)
9823 {
9824 fn = ov;
9825 base_binfo = obinfo;
9826 flags |= LOOKUP_NONVIRTUAL;
9827 }
9828 }
9829
9830 tree converted_arg = build_base_path (PLUS_EXPR, arg,
9831 base_binfo, 1, complain);
9832
9833 argarray[j++] = converted_arg;
9834 parm = TREE_CHAIN (parm);
9835 if (first_arg != NULL_TREE)
9836 first_arg = NULL_TREE;
9837 else
9838 ++arg_index;
9839 ++i;
9840 is_method = 1;
9841 }
9842
9843 gcc_assert (first_arg == NULL_TREE);
9844 for (; arg_index < vec_safe_length (args) && parm;
9845 parm = TREE_CHAIN (parm), ++arg_index, ++i)
9846 {
9847 tree type = TREE_VALUE (parm);
9848 tree arg = (*args)[arg_index];
9849 bool conversion_warning = true;
9850
9851 conv = convs[i];
9852
9853 /* If the argument is NULL and used to (implicitly) instantiate a
9854 template function (and bind one of the template arguments to
9855 the type of 'long int'), we don't want to warn about passing NULL
9856 to non-pointer argument.
9857 For example, if we have this template function:
9858
9859 template<typename T> void func(T x) {}
9860
9861 we want to warn (when -Wconversion is enabled) in this case:
9862
9863 void foo() {
9864 func<int>(NULL);
9865 }
9866
9867 but not in this case:
9868
9869 void foo() {
9870 func(NULL);
9871 }
9872 */
9873 if (null_node_p (arg)
9874 && DECL_TEMPLATE_INFO (fn)
9875 && cand->template_decl
9876 && !cand->explicit_targs)
9877 conversion_warning = false;
9878
9879 /* Set user_conv_p on the argument conversions, so rvalue/base handling
9880 knows not to allow any more UDCs. This needs to happen after we
9881 process cand->warnings. */
9882 if (flags & LOOKUP_NO_CONVERSION)
9883 conv->user_conv_p = true;
9884
9885 tsubst_flags_t arg_complain = complain;
9886 if (!conversion_warning)
9887 arg_complain &= ~tf_warning;
9888
9889 if (arg_complain & tf_warning)
9890 maybe_warn_pessimizing_move (arg, type, /*return_p*/false);
9891
9892 val = convert_like_with_context (conv, arg, fn, i - is_method,
9893 arg_complain);
9894 val = convert_for_arg_passing (type, val, arg_complain);
9895
9896 if (val == error_mark_node)
9897 return error_mark_node;
9898 else
9899 argarray[j++] = val;
9900 }
9901
9902 /* Default arguments */
9903 for (; parm && parm != void_list_node; parm = TREE_CHAIN (parm), i++)
9904 {
9905 if (TREE_VALUE (parm) == error_mark_node)
9906 return error_mark_node;
9907 val = convert_default_arg (TREE_VALUE (parm),
9908 TREE_PURPOSE (parm),
9909 fn, i - is_method,
9910 complain);
9911 if (val == error_mark_node)
9912 return error_mark_node;
9913 argarray[j++] = val;
9914 }
9915
9916 /* Ellipsis */
9917 int magic = magic_varargs_p (fn);
9918 for (; arg_index < vec_safe_length (args); ++arg_index)
9919 {
9920 tree a = (*args)[arg_index];
9921 if (magic == 3 && arg_index == 2)
9922 {
9923 /* Do no conversions for certain magic varargs. */
9924 a = mark_type_use (a);
9925 if (TREE_CODE (a) == FUNCTION_DECL && reject_gcc_builtin (a))
9926 return error_mark_node;
9927 }
9928 else if (magic != 0)
9929 {
9930 /* Don't truncate excess precision to the semantic type. */
9931 if (magic == 1 && TREE_CODE (a) == EXCESS_PRECISION_EXPR)
9932 a = TREE_OPERAND (a, 0);
9933 /* For other magic varargs only do decay_conversion. */
9934 a = decay_conversion (a, complain);
9935 }
9936 else if (DECL_CONSTRUCTOR_P (fn)
9937 && same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (fn),
9938 TREE_TYPE (a)))
9939 {
9940 /* Avoid infinite recursion trying to call A(...). */
9941 if (complain & tf_error)
9942 /* Try to call the actual copy constructor for a good error. */
9943 call_copy_ctor (a, complain);
9944 return error_mark_node;
9945 }
9946 else
9947 a = convert_arg_to_ellipsis (a, complain);
9948 if (a == error_mark_node)
9949 return error_mark_node;
9950 argarray[j++] = a;
9951 }
9952
9953 gcc_assert (j <= nargs);
9954 nargs = j;
9955 icip.reset ();
9956
9957 /* Avoid performing argument transformation if warnings are disabled.
9958 When tf_warning is set and at least one of the warnings is active
9959 the check_function_arguments function might warn about something. */
9960
9961 bool warned_p = false;
9962 if ((complain & tf_warning)
9963 && (warn_nonnull
9964 || warn_format
9965 || warn_suggest_attribute_format
9966 || warn_restrict))
9967 {
9968 tree *fargs = (!nargs ? argarray
9969 : (tree *) alloca (nargs * sizeof (tree)));
9970 for (j = 0; j < nargs; j++)
9971 {
9972 /* For -Wformat undo the implicit passing by hidden reference
9973 done by convert_arg_to_ellipsis. */
9974 if (TREE_CODE (argarray[j]) == ADDR_EXPR
9975 && TYPE_REF_P (TREE_TYPE (argarray[j])))
9976 fargs[j] = TREE_OPERAND (argarray[j], 0);
9977 else
9978 fargs[j] = argarray[j];
9979 }
9980
9981 warned_p = check_function_arguments (input_location, fn, TREE_TYPE (fn),
9982 nargs, fargs, NULL);
9983 }
9984
9985 if (DECL_INHERITED_CTOR (fn))
9986 {
9987 /* Check for passing ellipsis arguments to an inherited constructor. We
9988 could handle this by open-coding the inherited constructor rather than
9989 defining it, but let's not bother now. */
9990 if (!cp_unevaluated_operand
9991 && cand->num_convs
9992 && cand->convs[cand->num_convs-1]->ellipsis_p)
9993 {
9994 if (complain & tf_error)
9995 {
9996 sorry ("passing arguments to ellipsis of inherited constructor "
9997 "%qD", cand->fn);
9998 inform (DECL_SOURCE_LOCATION (cand->fn), "declared here");
9999 }
10000 return error_mark_node;
10001 }
10002
10003 /* A base constructor inheriting from a virtual base doesn't get the
10004 inherited arguments, just this and __vtt. */
10005 if (ctor_omit_inherited_parms (fn))
10006 nargs = 2;
10007 }
10008
10009 /* Avoid actually calling copy constructors and copy assignment operators,
10010 if possible. */
10011
10012 if (! flag_elide_constructors && !force_elide)
10013 /* Do things the hard way. */;
10014 else if (cand->num_convs == 1
10015 && (DECL_COPY_CONSTRUCTOR_P (fn)
10016 || DECL_MOVE_CONSTRUCTOR_P (fn))
10017 /* It's unsafe to elide the constructor when handling
10018 a noexcept-expression, it may evaluate to the wrong
10019 value (c++/53025). */
10020 && (force_elide || cp_noexcept_operand == 0))
10021 {
10022 tree targ;
10023 tree arg = argarray[num_artificial_parms_for (fn)];
10024 tree fa = argarray[0];
10025 bool trivial = trivial_fn_p (fn);
10026
10027 /* Pull out the real argument, disregarding const-correctness. */
10028 targ = arg;
10029 /* Strip the reference binding for the constructor parameter. */
10030 if (CONVERT_EXPR_P (targ)
10031 && TYPE_REF_P (TREE_TYPE (targ)))
10032 targ = TREE_OPERAND (targ, 0);
10033 /* But don't strip any other reference bindings; binding a temporary to a
10034 reference prevents copy elision. */
10035 while ((CONVERT_EXPR_P (targ)
10036 && !TYPE_REF_P (TREE_TYPE (targ)))
10037 || TREE_CODE (targ) == NON_LVALUE_EXPR)
10038 targ = TREE_OPERAND (targ, 0);
10039 if (TREE_CODE (targ) == ADDR_EXPR)
10040 {
10041 targ = TREE_OPERAND (targ, 0);
10042 if (!same_type_ignoring_top_level_qualifiers_p
10043 (TREE_TYPE (TREE_TYPE (arg)), TREE_TYPE (targ)))
10044 targ = NULL_TREE;
10045 }
10046 else
10047 targ = NULL_TREE;
10048
10049 if (targ)
10050 arg = targ;
10051 else
10052 arg = cp_build_fold_indirect_ref (arg);
10053
10054 /* In C++17 we shouldn't be copying a TARGET_EXPR except into a
10055 potentially-overlapping subobject. */
10056 if (CHECKING_P && cxx_dialect >= cxx17)
10057 gcc_assert (TREE_CODE (arg) != TARGET_EXPR
10058 || force_elide
10059 /* It's from binding the ref parm to a packed field. */
10060 || convs[0]->need_temporary_p
10061 || seen_error ()
10062 /* See unsafe_copy_elision_p. */
10063 || unsafe_return_slot_p (fa));
10064
10065 bool unsafe = unsafe_copy_elision_p_opt (fa, arg);
10066 bool eliding_temp = (TREE_CODE (arg) == TARGET_EXPR && !unsafe);
10067
10068 /* [class.copy]: the copy constructor is implicitly defined even if the
10069 implementation elided its use. But don't warn about deprecation when
10070 eliding a temporary, as then no copy is actually performed. */
10071 warning_sentinel s (warn_deprecated_copy, eliding_temp);
10072 if (force_elide)
10073 /* The language says this isn't called. */;
10074 else if (!trivial)
10075 {
10076 if (!mark_used (fn, complain) && !(complain & tf_error))
10077 return error_mark_node;
10078 already_used = true;
10079 }
10080 else
10081 cp_handle_deprecated_or_unavailable (fn, complain);
10082
10083 if (eliding_temp && DECL_BASE_CONSTRUCTOR_P (fn)
10084 && !make_base_init_ok (arg))
10085 unsafe = true;
10086
10087 /* If we're creating a temp and we already have one, don't create a
10088 new one. If we're not creating a temp but we get one, use
10089 INIT_EXPR to collapse the temp into our target. Otherwise, if the
10090 ctor is trivial, do a bitwise copy with a simple TARGET_EXPR for a
10091 temp or an INIT_EXPR otherwise. */
10092 if (is_dummy_object (fa))
10093 {
10094 if (TREE_CODE (arg) == TARGET_EXPR)
10095 return arg;
10096 else if (trivial)
10097 return force_target_expr (DECL_CONTEXT (fn), arg, complain);
10098 }
10099 else if ((trivial || TREE_CODE (arg) == TARGET_EXPR)
10100 && !unsafe)
10101 {
10102 tree to = cp_build_fold_indirect_ref (fa);
10103 val = cp_build_init_expr (to, arg);
10104 return val;
10105 }
10106 }
10107 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
10108 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)
10109 && trivial_fn_p (fn))
10110 {
10111 /* Don't use cp_build_fold_indirect_ref, op= returns an lvalue even if
10112 the object argument isn't one. */
10113 tree to = cp_build_indirect_ref (input_location, argarray[0],
10114 RO_ARROW, complain);
10115 tree type = TREE_TYPE (to);
10116 tree as_base = CLASSTYPE_AS_BASE (type);
10117 tree arg = argarray[1];
10118 location_t loc = cp_expr_loc_or_input_loc (arg);
10119
10120 if (is_really_empty_class (type, /*ignore_vptr*/true))
10121 {
10122 /* Avoid copying empty classes. */
10123 val = build2 (COMPOUND_EXPR, type, arg, to);
10124 suppress_warning (val, OPT_Wunused);
10125 }
10126 else if (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (as_base)))
10127 {
10128 if (is_std_init_list (type)
10129 && conv_binds_ref_to_prvalue (convs[1]))
10130 warning_at (loc, OPT_Winit_list_lifetime,
10131 "assignment from temporary %<initializer_list%> does "
10132 "not extend the lifetime of the underlying array");
10133 arg = cp_build_fold_indirect_ref (arg);
10134 val = build2 (MODIFY_EXPR, TREE_TYPE (to), to, arg);
10135 }
10136 else
10137 {
10138 /* We must only copy the non-tail padding parts. */
10139 tree arg0, arg2, t;
10140 tree array_type, alias_set;
10141
10142 arg2 = TYPE_SIZE_UNIT (as_base);
10143 to = cp_stabilize_reference (to);
10144 arg0 = cp_build_addr_expr (to, complain);
10145
10146 array_type = build_array_type (unsigned_char_type_node,
10147 build_index_type
10148 (size_binop (MINUS_EXPR,
10149 arg2, size_int (1))));
10150 alias_set = build_int_cst (build_pointer_type (type), 0);
10151 t = build2 (MODIFY_EXPR, void_type_node,
10152 build2 (MEM_REF, array_type, arg0, alias_set),
10153 build2 (MEM_REF, array_type, arg, alias_set));
10154 val = build2 (COMPOUND_EXPR, TREE_TYPE (to), t, to);
10155 suppress_warning (val, OPT_Wunused);
10156 }
10157
10158 cp_handle_deprecated_or_unavailable (fn, complain);
10159
10160 return val;
10161 }
10162 else if (trivial_fn_p (fn))
10163 {
10164 if (DECL_DESTRUCTOR_P (fn))
10165 return build_trivial_dtor_call (argarray[0]);
10166 else if (default_ctor_p (fn))
10167 {
10168 if (is_dummy_object (argarray[0]))
10169 return force_target_expr (DECL_CONTEXT (fn), void_node,
10170 no_cleanup_complain);
10171 else
10172 return cp_build_fold_indirect_ref (argarray[0]);
10173 }
10174 }
10175
10176 gcc_assert (!force_elide);
10177
10178 if (!already_used
10179 && !mark_used (fn, complain))
10180 return error_mark_node;
10181
10182 /* Warn if the built-in writes to an object of a non-trivial type. */
10183 if (warn_class_memaccess
10184 && vec_safe_length (args) >= 2
10185 && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
10186 maybe_warn_class_memaccess (input_location, fn, args);
10187
10188 if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0)
10189 {
10190 tree t;
10191 tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (argarray[0])),
10192 DECL_CONTEXT (fn),
10193 ba_any, NULL, complain);
10194 gcc_assert (binfo && binfo != error_mark_node);
10195
10196 argarray[0] = build_base_path (PLUS_EXPR, argarray[0], binfo, 1,
10197 complain);
10198 if (TREE_SIDE_EFFECTS (argarray[0]))
10199 argarray[0] = save_expr (argarray[0]);
10200 t = build_pointer_type (TREE_TYPE (fn));
10201 fn = build_vfn_ref (argarray[0], DECL_VINDEX (fn));
10202 TREE_TYPE (fn) = t;
10203 }
10204 else
10205 {
10206 /* If FN is marked deprecated, then we've already issued a deprecated-use
10207 warning from mark_used above, so avoid redundantly issuing another one
10208 from build_addr_func. */
10209 warning_sentinel w (warn_deprecated_decl);
10210
10211 fn = build_addr_func (fn, complain);
10212 if (fn == error_mark_node)
10213 return error_mark_node;
10214 }
10215
10216 tree call = build_cxx_call (fn, nargs, argarray, complain|decltype_flag);
10217 if (call == error_mark_node)
10218 return call;
10219 if (cand->flags & LOOKUP_LIST_INIT_CTOR)
10220 {
10221 tree c = extract_call_expr (call);
10222 /* build_new_op will clear this when appropriate. */
10223 CALL_EXPR_ORDERED_ARGS (c) = true;
10224 }
10225 if (warned_p)
10226 {
10227 tree c = extract_call_expr (call);
10228 if (TREE_CODE (c) == CALL_EXPR)
10229 suppress_warning (c /* Suppress all warnings. */);
10230 }
10231 if (TREE_CODE (fn) == ADDR_EXPR)
10232 {
10233 tree fndecl = STRIP_TEMPLATE (TREE_OPERAND (fn, 0));
10234 if (immediate_invocation_p (fndecl))
10235 {
10236 tree obj_arg = NULL_TREE;
10237 /* Undo convert_from_reference called by build_cxx_call. */
10238 if (REFERENCE_REF_P (call))
10239 call = TREE_OPERAND (call, 0);
10240 if (DECL_CONSTRUCTOR_P (fndecl))
10241 obj_arg = cand->first_arg ? cand->first_arg : (*args)[0];
10242 if (obj_arg && is_dummy_object (obj_arg))
10243 {
10244 call = build_cplus_new (DECL_CONTEXT (fndecl), call, complain);
10245 obj_arg = NULL_TREE;
10246 }
10247 /* Look through *(const T *)&obj. */
10248 else if (obj_arg && TREE_CODE (obj_arg) == INDIRECT_REF)
10249 {
10250 tree addr = TREE_OPERAND (obj_arg, 0);
10251 STRIP_NOPS (addr);
10252 if (TREE_CODE (addr) == ADDR_EXPR)
10253 {
10254 tree typeo = TREE_TYPE (obj_arg);
10255 tree typei = TREE_TYPE (TREE_OPERAND (addr, 0));
10256 if (same_type_ignoring_top_level_qualifiers_p (typeo, typei))
10257 obj_arg = TREE_OPERAND (addr, 0);
10258 }
10259 }
10260 call = cxx_constant_value (call, obj_arg, complain);
10261 if (obj_arg && !error_operand_p (call))
10262 call = cp_build_init_expr (obj_arg, call);
10263 call = convert_from_reference (call);
10264 }
10265 }
10266 return call;
10267 }
10268
10269 namespace
10270 {
10271
10272 /* Return the DECL of the first non-static subobject of class TYPE
10273 that satisfies the predicate PRED or null if none can be found. */
10274
10275 template <class Predicate>
10276 tree
10277 first_non_static_field (tree type, Predicate pred)
10278 {
10279 if (!type || !CLASS_TYPE_P (type))
10280 return NULL_TREE;
10281
10282 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
10283 {
10284 if (TREE_CODE (field) != FIELD_DECL)
10285 continue;
10286 if (TREE_STATIC (field))
10287 continue;
10288 if (pred (field))
10289 return field;
10290 }
10291
10292 int i = 0;
10293
10294 for (tree base_binfo, binfo = TYPE_BINFO (type);
10295 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
10296 {
10297 tree base = TREE_TYPE (base_binfo);
10298 if (pred (base))
10299 return base;
10300 if (tree field = first_non_static_field (base, pred))
10301 return field;
10302 }
10303
10304 return NULL_TREE;
10305 }
10306
10307 struct NonPublicField
10308 {
10309 bool operator() (const_tree t) const
10310 {
10311 return DECL_P (t) && (TREE_PRIVATE (t) || TREE_PROTECTED (t));
10312 }
10313 };
10314
10315 /* Return the DECL of the first non-public subobject of class TYPE
10316 or null if none can be found. */
10317
10318 static inline tree
10319 first_non_public_field (tree type)
10320 {
10321 return first_non_static_field (type, NonPublicField ());
10322 }
10323
10324 struct NonTrivialField
10325 {
10326 bool operator() (const_tree t) const
10327 {
10328 return !trivial_type_p (DECL_P (t) ? TREE_TYPE (t) : t);
10329 }
10330 };
10331
10332 /* Return the DECL of the first non-trivial subobject of class TYPE
10333 or null if none can be found. */
10334
10335 static inline tree
10336 first_non_trivial_field (tree type)
10337 {
10338 return first_non_static_field (type, NonTrivialField ());
10339 }
10340
10341 } /* unnamed namespace */
10342
10343 /* Return true if all copy and move assignment operator overloads for
10344 class TYPE are trivial and at least one of them is not deleted and,
10345 when ACCESS is set, accessible. Return false otherwise. Set
10346 HASASSIGN to true when the TYPE has a (not necessarily trivial)
10347 copy or move assignment. */
10348
10349 static bool
10350 has_trivial_copy_assign_p (tree type, bool access, bool *hasassign)
10351 {
10352 tree fns = get_class_binding (type, assign_op_identifier);
10353 bool all_trivial = true;
10354
10355 /* Iterate over overloads of the assignment operator, checking
10356 accessible copy assignments for triviality. */
10357
10358 for (tree f : ovl_range (fns))
10359 {
10360 /* Skip operators that aren't copy assignments. */
10361 if (!copy_fn_p (f))
10362 continue;
10363
10364 bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
10365 || accessible_p (TYPE_BINFO (type), f, true));
10366
10367 /* Skip template assignment operators and deleted functions. */
10368 if (TREE_CODE (f) != FUNCTION_DECL || DECL_DELETED_FN (f))
10369 continue;
10370
10371 if (accessible)
10372 *hasassign = true;
10373
10374 if (!accessible || !trivial_fn_p (f))
10375 all_trivial = false;
10376
10377 /* Break early when both properties have been determined. */
10378 if (*hasassign && !all_trivial)
10379 break;
10380 }
10381
10382 /* Return true if they're all trivial and one of the expressions
10383 TYPE() = TYPE() or TYPE() = (TYPE&)() is valid. */
10384 tree ref = cp_build_reference_type (type, false);
10385 return (all_trivial
10386 && (is_trivially_xible (MODIFY_EXPR, type, type)
10387 || is_trivially_xible (MODIFY_EXPR, type, ref)));
10388 }
10389
10390 /* Return true if all copy and move ctor overloads for class TYPE are
10391 trivial and at least one of them is not deleted and, when ACCESS is
10392 set, accessible. Return false otherwise. Set each element of HASCTOR[]
10393 to true when the TYPE has a (not necessarily trivial) default and copy
10394 (or move) ctor, respectively. */
10395
10396 static bool
10397 has_trivial_copy_p (tree type, bool access, bool hasctor[2])
10398 {
10399 tree fns = get_class_binding (type, complete_ctor_identifier);
10400 bool all_trivial = true;
10401
10402 for (tree f : ovl_range (fns))
10403 {
10404 /* Skip template constructors. */
10405 if (TREE_CODE (f) != FUNCTION_DECL)
10406 continue;
10407
10408 bool cpy_or_move_ctor_p = copy_fn_p (f);
10409
10410 /* Skip ctors other than default, copy, and move. */
10411 if (!cpy_or_move_ctor_p && !default_ctor_p (f))
10412 continue;
10413
10414 if (DECL_DELETED_FN (f))
10415 continue;
10416
10417 bool accessible = (!access || !(TREE_PRIVATE (f) || TREE_PROTECTED (f))
10418 || accessible_p (TYPE_BINFO (type), f, true));
10419
10420 if (accessible)
10421 hasctor[cpy_or_move_ctor_p] = true;
10422
10423 if (cpy_or_move_ctor_p && (!accessible || !trivial_fn_p (f)))
10424 all_trivial = false;
10425
10426 /* Break early when both properties have been determined. */
10427 if (hasctor[0] && hasctor[1] && !all_trivial)
10428 break;
10429 }
10430
10431 return all_trivial;
10432 }
10433
10434 /* Issue a warning on a call to the built-in function FNDECL if it is
10435 a raw memory write whose destination is not an object of (something
10436 like) trivial or standard layout type with a non-deleted assignment
10437 and copy ctor. Detects const correctness violations, corrupting
10438 references, virtual table pointers, and bypassing non-trivial
10439 assignments. */
10440
10441 static void
10442 maybe_warn_class_memaccess (location_t loc, tree fndecl,
10443 const vec<tree, va_gc> *args)
10444 {
10445 /* Except for bcopy where it's second, the destination pointer is
10446 the first argument for all functions handled here. Compute
10447 the index of the destination and source arguments. */
10448 unsigned dstidx = DECL_FUNCTION_CODE (fndecl) == BUILT_IN_BCOPY;
10449 unsigned srcidx = !dstidx;
10450
10451 tree dest = (*args)[dstidx];
10452 if (!TREE_TYPE (dest)
10453 || (TREE_CODE (TREE_TYPE (dest)) != ARRAY_TYPE
10454 && !INDIRECT_TYPE_P (TREE_TYPE (dest))))
10455 return;
10456
10457 tree srctype = NULL_TREE;
10458
10459 /* Determine the type of the pointed-to object and whether it's
10460 a complete class type. */
10461 tree desttype = TREE_TYPE (TREE_TYPE (dest));
10462
10463 if (!desttype || !COMPLETE_TYPE_P (desttype) || !CLASS_TYPE_P (desttype))
10464 return;
10465
10466 /* Check to see if the raw memory call is made by a non-static member
10467 function with THIS as the destination argument for the destination
10468 type. If so, and if the class has no non-trivial bases or members,
10469 be more permissive. */
10470 if (current_function_decl
10471 && DECL_NONSTATIC_MEMBER_FUNCTION_P (current_function_decl)
10472 && is_this_parameter (tree_strip_nop_conversions (dest)))
10473 {
10474 tree ctx = DECL_CONTEXT (current_function_decl);
10475 bool special = same_type_ignoring_top_level_qualifiers_p (ctx, desttype);
10476 tree binfo = TYPE_BINFO (ctx);
10477
10478 if (special
10479 && !BINFO_VTABLE (binfo)
10480 && !first_non_trivial_field (desttype))
10481 return;
10482 }
10483
10484 /* True if the class is trivial. */
10485 bool trivial = trivial_type_p (desttype);
10486
10487 /* Set to true if DESTYPE has an accessible copy assignment. */
10488 bool hasassign = false;
10489 /* True if all of the class' overloaded copy assignment operators
10490 are all trivial (and not deleted) and at least one of them is
10491 accessible. */
10492 bool trivassign = has_trivial_copy_assign_p (desttype, true, &hasassign);
10493
10494 /* Set to true if DESTTYPE has an accessible default and copy ctor,
10495 respectively. */
10496 bool hasctors[2] = { false, false };
10497
10498 /* True if all of the class' overloaded copy constructors are all
10499 trivial (and not deleted) and at least one of them is accessible. */
10500 bool trivcopy = has_trivial_copy_p (desttype, true, hasctors);
10501
10502 /* Set FLD to the first private/protected member of the class. */
10503 tree fld = trivial ? first_non_public_field (desttype) : NULL_TREE;
10504
10505 /* The warning format string. */
10506 const char *warnfmt = NULL;
10507 /* A suggested alternative to offer instead of the raw memory call.
10508 Empty string when none can be come up with. */
10509 const char *suggest = "";
10510 bool warned = false;
10511
10512 switch (DECL_FUNCTION_CODE (fndecl))
10513 {
10514 case BUILT_IN_MEMSET:
10515 if (!integer_zerop (maybe_constant_value ((*args)[1])))
10516 {
10517 /* Diagnose setting non-copy-assignable or non-trivial types,
10518 or types with a private member, to (potentially) non-zero
10519 bytes. Since the value of the bytes being written is unknown,
10520 suggest using assignment instead (if one exists). Also warn
10521 for writes into objects for which zero-initialization doesn't
10522 mean all bits clear (pointer-to-member data, where null is all
10523 bits set). Since the value being written is (most likely)
10524 non-zero, simply suggest assignment (but not copy assignment). */
10525 suggest = "; use assignment instead";
10526 if (!trivassign)
10527 warnfmt = G_("%qD writing to an object of type %#qT with "
10528 "no trivial copy-assignment");
10529 else if (!trivial)
10530 warnfmt = G_("%qD writing to an object of non-trivial type %#qT%s");
10531 else if (fld)
10532 {
10533 const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
10534 warned = warning_at (loc, OPT_Wclass_memaccess,
10535 "%qD writing to an object of type %#qT with "
10536 "%qs member %qD",
10537 fndecl, desttype, access, fld);
10538 }
10539 else if (!zero_init_p (desttype))
10540 warnfmt = G_("%qD writing to an object of type %#qT containing "
10541 "a pointer to data member%s");
10542
10543 break;
10544 }
10545 /* Fall through. */
10546
10547 case BUILT_IN_BZERO:
10548 /* Similarly to the above, diagnose clearing non-trivial or non-
10549 standard layout objects, or objects of types with no assignmenmt.
10550 Since the value being written is known to be zero, suggest either
10551 copy assignment, copy ctor, or default ctor as an alternative,
10552 depending on what's available. */
10553
10554 if (hasassign && hasctors[0])
10555 suggest = G_("; use assignment or value-initialization instead");
10556 else if (hasassign)
10557 suggest = G_("; use assignment instead");
10558 else if (hasctors[0])
10559 suggest = G_("; use value-initialization instead");
10560
10561 if (!trivassign)
10562 warnfmt = G_("%qD clearing an object of type %#qT with "
10563 "no trivial copy-assignment%s");
10564 else if (!trivial)
10565 warnfmt = G_("%qD clearing an object of non-trivial type %#qT%s");
10566 else if (!zero_init_p (desttype))
10567 warnfmt = G_("%qD clearing an object of type %#qT containing "
10568 "a pointer-to-member%s");
10569 break;
10570
10571 case BUILT_IN_BCOPY:
10572 case BUILT_IN_MEMCPY:
10573 case BUILT_IN_MEMMOVE:
10574 case BUILT_IN_MEMPCPY:
10575 /* Determine the type of the source object. */
10576 srctype = TREE_TYPE ((*args)[srcidx]);
10577 if (!srctype || !INDIRECT_TYPE_P (srctype))
10578 srctype = void_type_node;
10579 else
10580 srctype = TREE_TYPE (srctype);
10581
10582 /* Since it's impossible to determine wheter the byte copy is
10583 being used in place of assignment to an existing object or
10584 as a substitute for initialization, assume it's the former.
10585 Determine the best alternative to use instead depending on
10586 what's not deleted. */
10587 if (hasassign && hasctors[1])
10588 suggest = G_("; use copy-assignment or copy-initialization instead");
10589 else if (hasassign)
10590 suggest = G_("; use copy-assignment instead");
10591 else if (hasctors[1])
10592 suggest = G_("; use copy-initialization instead");
10593
10594 if (!trivassign)
10595 warnfmt = G_("%qD writing to an object of type %#qT with no trivial "
10596 "copy-assignment%s");
10597 else if (!trivially_copyable_p (desttype))
10598 warnfmt = G_("%qD writing to an object of non-trivially copyable "
10599 "type %#qT%s");
10600 else if (!trivcopy)
10601 warnfmt = G_("%qD writing to an object with a deleted copy constructor");
10602
10603 else if (!trivial
10604 && !VOID_TYPE_P (srctype)
10605 && !is_byte_access_type (srctype)
10606 && !same_type_ignoring_top_level_qualifiers_p (desttype,
10607 srctype))
10608 {
10609 /* Warn when copying into a non-trivial object from an object
10610 of a different type other than void or char. */
10611 warned = warning_at (loc, OPT_Wclass_memaccess,
10612 "%qD copying an object of non-trivial type "
10613 "%#qT from an array of %#qT",
10614 fndecl, desttype, srctype);
10615 }
10616 else if (fld
10617 && !VOID_TYPE_P (srctype)
10618 && !is_byte_access_type (srctype)
10619 && !same_type_ignoring_top_level_qualifiers_p (desttype,
10620 srctype))
10621 {
10622 const char *access = TREE_PRIVATE (fld) ? "private" : "protected";
10623 warned = warning_at (loc, OPT_Wclass_memaccess,
10624 "%qD copying an object of type %#qT with "
10625 "%qs member %qD from an array of %#qT; use "
10626 "assignment or copy-initialization instead",
10627 fndecl, desttype, access, fld, srctype);
10628 }
10629 else if (!trivial && vec_safe_length (args) > 2)
10630 {
10631 tree sz = maybe_constant_value ((*args)[2]);
10632 if (!tree_fits_uhwi_p (sz))
10633 break;
10634
10635 /* Finally, warn on partial copies. */
10636 unsigned HOST_WIDE_INT typesize
10637 = tree_to_uhwi (TYPE_SIZE_UNIT (desttype));
10638 if (typesize == 0)
10639 break;
10640 if (unsigned HOST_WIDE_INT partial = tree_to_uhwi (sz) % typesize)
10641 warned = warning_at (loc, OPT_Wclass_memaccess,
10642 (typesize - partial > 1
10643 ? G_("%qD writing to an object of "
10644 "a non-trivial type %#qT leaves %wu "
10645 "bytes unchanged")
10646 : G_("%qD writing to an object of "
10647 "a non-trivial type %#qT leaves %wu "
10648 "byte unchanged")),
10649 fndecl, desttype, typesize - partial);
10650 }
10651 break;
10652
10653 case BUILT_IN_REALLOC:
10654
10655 if (!trivially_copyable_p (desttype))
10656 warnfmt = G_("%qD moving an object of non-trivially copyable type "
10657 "%#qT; use %<new%> and %<delete%> instead");
10658 else if (!trivcopy)
10659 warnfmt = G_("%qD moving an object of type %#qT with deleted copy "
10660 "constructor; use %<new%> and %<delete%> instead");
10661 else if (!get_dtor (desttype, tf_none))
10662 warnfmt = G_("%qD moving an object of type %#qT with deleted "
10663 "destructor");
10664 else if (!trivial)
10665 {
10666 tree sz = maybe_constant_value ((*args)[1]);
10667 if (TREE_CODE (sz) == INTEGER_CST
10668 && tree_int_cst_lt (sz, TYPE_SIZE_UNIT (desttype)))
10669 /* Finally, warn on reallocation into insufficient space. */
10670 warned = warning_at (loc, OPT_Wclass_memaccess,
10671 "%qD moving an object of non-trivial type "
10672 "%#qT and size %E into a region of size %E",
10673 fndecl, desttype, TYPE_SIZE_UNIT (desttype),
10674 sz);
10675 }
10676 break;
10677
10678 default:
10679 return;
10680 }
10681
10682 if (warnfmt)
10683 {
10684 if (suggest)
10685 warned = warning_at (loc, OPT_Wclass_memaccess,
10686 warnfmt, fndecl, desttype, suggest);
10687 else
10688 warned = warning_at (loc, OPT_Wclass_memaccess,
10689 warnfmt, fndecl, desttype);
10690 }
10691
10692 if (warned)
10693 inform (location_of (desttype), "%#qT declared here", desttype);
10694 }
10695
10696 /* Build and return a call to FN, using NARGS arguments in ARGARRAY.
10697 If FN is the result of resolving an overloaded target built-in,
10698 ORIG_FNDECL is the original function decl, otherwise it is null.
10699 This function performs no overload resolution, conversion, or other
10700 high-level operations. */
10701
10702 tree
10703 build_cxx_call (tree fn, int nargs, tree *argarray,
10704 tsubst_flags_t complain, tree orig_fndecl)
10705 {
10706 tree fndecl;
10707
10708 /* Remember roughly where this call is. */
10709 location_t loc = cp_expr_loc_or_input_loc (fn);
10710 fn = build_call_a (fn, nargs, argarray);
10711 SET_EXPR_LOCATION (fn, loc);
10712
10713 fndecl = get_callee_fndecl (fn);
10714 if (!orig_fndecl)
10715 orig_fndecl = fndecl;
10716
10717 /* Check that arguments to builtin functions match the expectations. */
10718 if (fndecl
10719 && !processing_template_decl
10720 && fndecl_built_in_p (fndecl))
10721 {
10722 int i;
10723
10724 /* We need to take care that values to BUILT_IN_NORMAL
10725 are reduced. */
10726 for (i = 0; i < nargs; i++)
10727 argarray[i] = maybe_constant_value (argarray[i]);
10728
10729 if (!check_builtin_function_arguments (EXPR_LOCATION (fn), vNULL, fndecl,
10730 orig_fndecl, nargs, argarray))
10731 return error_mark_node;
10732 else if (fndecl_built_in_p (fndecl, BUILT_IN_CLEAR_PADDING))
10733 {
10734 tree arg0 = argarray[0];
10735 STRIP_NOPS (arg0);
10736 if (TREE_CODE (arg0) == ADDR_EXPR
10737 && DECL_P (TREE_OPERAND (arg0, 0))
10738 && same_type_ignoring_top_level_qualifiers_p
10739 (TREE_TYPE (TREE_TYPE (argarray[0])),
10740 TREE_TYPE (TREE_TYPE (arg0))))
10741 /* For __builtin_clear_padding (&var) we know the type
10742 is for a complete object, so there is no risk in clearing
10743 padding that is reused in some derived class member. */;
10744 else if (!trivially_copyable_p (TREE_TYPE (TREE_TYPE (argarray[0]))))
10745 {
10746 error_at (EXPR_LOC_OR_LOC (argarray[0], input_location),
10747 "argument %u in call to function %qE "
10748 "has pointer to a non-trivially-copyable type (%qT)",
10749 1, fndecl, TREE_TYPE (argarray[0]));
10750 return error_mark_node;
10751 }
10752 }
10753 }
10754
10755 if (VOID_TYPE_P (TREE_TYPE (fn)))
10756 return fn;
10757
10758 /* 5.2.2/11: If a function call is a prvalue of object type: if the
10759 function call is either the operand of a decltype-specifier or the
10760 right operand of a comma operator that is the operand of a
10761 decltype-specifier, a temporary object is not introduced for the
10762 prvalue. The type of the prvalue may be incomplete. */
10763 if (!(complain & tf_decltype))
10764 {
10765 fn = require_complete_type (fn, complain);
10766 if (fn == error_mark_node)
10767 return error_mark_node;
10768
10769 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (fn)))
10770 {
10771 fn = build_cplus_new (TREE_TYPE (fn), fn, complain);
10772 maybe_warn_parm_abi (TREE_TYPE (fn), loc);
10773 }
10774 }
10775 return convert_from_reference (fn);
10776 }
10777
10778 /* Returns the value to use for the in-charge parameter when making a
10779 call to a function with the indicated NAME.
10780
10781 FIXME:Can't we find a neater way to do this mapping? */
10782
10783 tree
10784 in_charge_arg_for_name (tree name)
10785 {
10786 if (IDENTIFIER_CTOR_P (name))
10787 {
10788 if (name == complete_ctor_identifier)
10789 return integer_one_node;
10790 gcc_checking_assert (name == base_ctor_identifier);
10791 }
10792 else
10793 {
10794 if (name == complete_dtor_identifier)
10795 return integer_two_node;
10796 else if (name == deleting_dtor_identifier)
10797 return integer_three_node;
10798 gcc_checking_assert (name == base_dtor_identifier);
10799 }
10800
10801 return integer_zero_node;
10802 }
10803
10804 /* We've built up a constructor call RET. Complain if it delegates to the
10805 constructor we're currently compiling. */
10806
10807 static void
10808 check_self_delegation (tree ret)
10809 {
10810 if (TREE_CODE (ret) == TARGET_EXPR)
10811 ret = TARGET_EXPR_INITIAL (ret);
10812 tree fn = cp_get_callee_fndecl_nofold (ret);
10813 if (fn && DECL_ABSTRACT_ORIGIN (fn) == current_function_decl)
10814 error ("constructor delegates to itself");
10815 }
10816
10817 /* Build a call to a constructor, destructor, or an assignment
10818 operator for INSTANCE, an expression with class type. NAME
10819 indicates the special member function to call; *ARGS are the
10820 arguments. ARGS may be NULL. This may change ARGS. BINFO
10821 indicates the base of INSTANCE that is to be passed as the `this'
10822 parameter to the member function called.
10823
10824 FLAGS are the LOOKUP_* flags to use when processing the call.
10825
10826 If NAME indicates a complete object constructor, INSTANCE may be
10827 NULL_TREE. In this case, the caller will call build_cplus_new to
10828 store the newly constructed object into a VAR_DECL. */
10829
10830 tree
10831 build_special_member_call (tree instance, tree name, vec<tree, va_gc> **args,
10832 tree binfo, int flags, tsubst_flags_t complain)
10833 {
10834 tree fns;
10835 /* The type of the subobject to be constructed or destroyed. */
10836 tree class_type;
10837 vec<tree, va_gc> *allocated = NULL;
10838 tree ret;
10839
10840 gcc_assert (IDENTIFIER_CDTOR_P (name) || name == assign_op_identifier);
10841
10842 if (error_operand_p (instance))
10843 return error_mark_node;
10844
10845 if (IDENTIFIER_DTOR_P (name))
10846 {
10847 gcc_assert (args == NULL || vec_safe_is_empty (*args));
10848 if (!type_build_dtor_call (TREE_TYPE (instance)))
10849 /* Shortcut to avoid lazy destructor declaration. */
10850 return build_trivial_dtor_call (instance);
10851 }
10852
10853 if (TYPE_P (binfo))
10854 {
10855 /* Resolve the name. */
10856 if (!complete_type_or_maybe_complain (binfo, NULL_TREE, complain))
10857 return error_mark_node;
10858
10859 binfo = TYPE_BINFO (binfo);
10860 }
10861
10862 gcc_assert (binfo != NULL_TREE);
10863
10864 class_type = BINFO_TYPE (binfo);
10865
10866 /* Handle the special case where INSTANCE is NULL_TREE. */
10867 if (name == complete_ctor_identifier && !instance)
10868 instance = build_dummy_object (class_type);
10869 else
10870 {
10871 /* Convert to the base class, if necessary. */
10872 if (!same_type_ignoring_top_level_qualifiers_p
10873 (TREE_TYPE (instance), BINFO_TYPE (binfo)))
10874 {
10875 if (IDENTIFIER_CDTOR_P (name))
10876 /* For constructors and destructors, either the base is
10877 non-virtual, or it is virtual but we are doing the
10878 conversion from a constructor or destructor for the
10879 complete object. In either case, we can convert
10880 statically. */
10881 instance = convert_to_base_statically (instance, binfo);
10882 else
10883 {
10884 /* However, for assignment operators, we must convert
10885 dynamically if the base is virtual. */
10886 gcc_checking_assert (name == assign_op_identifier);
10887 instance = build_base_path (PLUS_EXPR, instance,
10888 binfo, /*nonnull=*/1, complain);
10889 }
10890 }
10891 }
10892
10893 gcc_assert (instance != NULL_TREE);
10894
10895 /* In C++17, "If the initializer expression is a prvalue and the
10896 cv-unqualified version of the source type is the same class as the class
10897 of the destination, the initializer expression is used to initialize the
10898 destination object." Handle that here to avoid doing overload
10899 resolution. */
10900 if (cxx_dialect >= cxx17
10901 && args && vec_safe_length (*args) == 1
10902 && !unsafe_return_slot_p (instance))
10903 {
10904 tree arg = (**args)[0];
10905
10906 if (BRACE_ENCLOSED_INITIALIZER_P (arg)
10907 && !TYPE_HAS_LIST_CTOR (class_type)
10908 && !CONSTRUCTOR_IS_DESIGNATED_INIT (arg)
10909 && CONSTRUCTOR_NELTS (arg) == 1)
10910 arg = CONSTRUCTOR_ELT (arg, 0)->value;
10911
10912 if ((TREE_CODE (arg) == TARGET_EXPR
10913 || TREE_CODE (arg) == CONSTRUCTOR)
10914 && (same_type_ignoring_top_level_qualifiers_p
10915 (class_type, TREE_TYPE (arg))))
10916 {
10917 if (is_dummy_object (instance))
10918 return arg;
10919 else if (TREE_CODE (arg) == TARGET_EXPR)
10920 TARGET_EXPR_DIRECT_INIT_P (arg) = true;
10921
10922 if ((complain & tf_error)
10923 && (flags & LOOKUP_DELEGATING_CONS))
10924 check_self_delegation (arg);
10925 /* Avoid change of behavior on Wunused-var-2.C. */
10926 instance = mark_lvalue_use (instance);
10927 return cp_build_init_expr (instance, arg);
10928 }
10929 }
10930
10931 fns = lookup_fnfields (binfo, name, 1, complain);
10932
10933 /* When making a call to a constructor or destructor for a subobject
10934 that uses virtual base classes, pass down a pointer to a VTT for
10935 the subobject. */
10936 if ((name == base_ctor_identifier
10937 || name == base_dtor_identifier)
10938 && CLASSTYPE_VBASECLASSES (class_type))
10939 {
10940 tree vtt;
10941 tree sub_vtt;
10942
10943 /* If the current function is a complete object constructor
10944 or destructor, then we fetch the VTT directly.
10945 Otherwise, we look it up using the VTT we were given. */
10946 vtt = DECL_CHAIN (CLASSTYPE_VTABLES (current_class_type));
10947 vtt = decay_conversion (vtt, complain);
10948 if (vtt == error_mark_node)
10949 return error_mark_node;
10950 vtt = build_if_in_charge (vtt, current_vtt_parm);
10951 if (BINFO_SUBVTT_INDEX (binfo))
10952 sub_vtt = fold_build_pointer_plus (vtt, BINFO_SUBVTT_INDEX (binfo));
10953 else
10954 sub_vtt = vtt;
10955
10956 if (args == NULL)
10957 {
10958 allocated = make_tree_vector ();
10959 args = &allocated;
10960 }
10961
10962 vec_safe_insert (*args, 0, sub_vtt);
10963 }
10964
10965 ret = build_new_method_call (instance, fns, args,
10966 TYPE_BINFO (BINFO_TYPE (binfo)),
10967 flags, /*fn=*/NULL,
10968 complain);
10969
10970 if (allocated != NULL)
10971 release_tree_vector (allocated);
10972
10973 if ((complain & tf_error)
10974 && (flags & LOOKUP_DELEGATING_CONS)
10975 && name == complete_ctor_identifier)
10976 check_self_delegation (ret);
10977
10978 return ret;
10979 }
10980
10981 /* Return the NAME, as a C string. The NAME indicates a function that
10982 is a member of TYPE. *FREE_P is set to true if the caller must
10983 free the memory returned.
10984
10985 Rather than go through all of this, we should simply set the names
10986 of constructors and destructors appropriately, and dispense with
10987 ctor_identifier, dtor_identifier, etc. */
10988
10989 static char *
10990 name_as_c_string (tree name, tree type, bool *free_p)
10991 {
10992 const char *pretty_name;
10993
10994 /* Assume that we will not allocate memory. */
10995 *free_p = false;
10996 /* Constructors and destructors are special. */
10997 if (IDENTIFIER_CDTOR_P (name))
10998 {
10999 pretty_name
11000 = identifier_to_locale (IDENTIFIER_POINTER (constructor_name (type)));
11001 /* For a destructor, add the '~'. */
11002 if (IDENTIFIER_DTOR_P (name))
11003 {
11004 pretty_name = concat ("~", pretty_name, NULL);
11005 /* Remember that we need to free the memory allocated. */
11006 *free_p = true;
11007 }
11008 }
11009 else if (IDENTIFIER_CONV_OP_P (name))
11010 {
11011 pretty_name = concat ("operator ",
11012 type_as_string_translate (TREE_TYPE (name),
11013 TFF_PLAIN_IDENTIFIER),
11014 NULL);
11015 /* Remember that we need to free the memory allocated. */
11016 *free_p = true;
11017 }
11018 else
11019 pretty_name = identifier_to_locale (IDENTIFIER_POINTER (name));
11020
11021 return CONST_CAST (char *, pretty_name);
11022 }
11023
11024 /* If CANDIDATES contains exactly one candidate, return it, otherwise
11025 return NULL. */
11026
11027 static z_candidate *
11028 single_z_candidate (z_candidate *candidates)
11029 {
11030 if (candidates == NULL)
11031 return NULL;
11032
11033 if (candidates->next)
11034 return NULL;
11035
11036 return candidates;
11037 }
11038
11039 /* If CANDIDATE is invalid due to a bad argument type, return the
11040 pertinent conversion_info.
11041
11042 Otherwise, return NULL. */
11043
11044 static const conversion_info *
11045 maybe_get_bad_conversion_for_unmatched_call (const z_candidate *candidate)
11046 {
11047 /* Must be an rr_arg_conversion or rr_bad_arg_conversion. */
11048 rejection_reason *r = candidate->reason;
11049
11050 if (r == NULL)
11051 return NULL;
11052
11053 switch (r->code)
11054 {
11055 default:
11056 return NULL;
11057
11058 case rr_arg_conversion:
11059 return &r->u.conversion;
11060
11061 case rr_bad_arg_conversion:
11062 return &r->u.bad_conversion;
11063 }
11064 }
11065
11066 /* Issue an error and note complaining about a bad argument type at a
11067 callsite with a single candidate FNDECL.
11068
11069 ARG_LOC is the location of the argument (or UNKNOWN_LOCATION, in which
11070 case input_location is used).
11071 FROM_TYPE is the type of the actual argument; TO_TYPE is the type of
11072 the formal parameter. */
11073
11074 void
11075 complain_about_bad_argument (location_t arg_loc,
11076 tree from_type, tree to_type,
11077 tree fndecl, int parmnum)
11078 {
11079 auto_diagnostic_group d;
11080 range_label_for_type_mismatch rhs_label (from_type, to_type);
11081 range_label *label = &rhs_label;
11082 if (arg_loc == UNKNOWN_LOCATION)
11083 {
11084 arg_loc = input_location;
11085 label = NULL;
11086 }
11087 gcc_rich_location richloc (arg_loc, label);
11088 error_at (&richloc,
11089 "cannot convert %qH to %qI",
11090 from_type, to_type);
11091 maybe_inform_about_fndecl_for_bogus_argument_init (fndecl,
11092 parmnum);
11093 }
11094
11095 /* Subroutine of build_new_method_call_1, for where there are no viable
11096 candidates for the call. */
11097
11098 static void
11099 complain_about_no_candidates_for_method_call (tree instance,
11100 z_candidate *candidates,
11101 tree explicit_targs,
11102 tree basetype,
11103 tree optype, tree name,
11104 bool skip_first_for_error,
11105 vec<tree, va_gc> *user_args)
11106 {
11107 auto_diagnostic_group d;
11108 if (!COMPLETE_OR_OPEN_TYPE_P (basetype))
11109 cxx_incomplete_type_error (instance, basetype);
11110 else if (optype)
11111 error ("no matching function for call to %<%T::operator %T(%A)%#V%>",
11112 basetype, optype, build_tree_list_vec (user_args),
11113 TREE_TYPE (instance));
11114 else
11115 {
11116 /* Special-case for when there's a single candidate that's failing
11117 due to a bad argument type. */
11118 if (z_candidate *candidate = single_z_candidate (candidates))
11119 if (const conversion_info *conv
11120 = maybe_get_bad_conversion_for_unmatched_call (candidate))
11121 {
11122 tree from_type = conv->from;
11123 if (!TYPE_P (conv->from))
11124 from_type = lvalue_type (conv->from);
11125 complain_about_bad_argument (conv->loc,
11126 from_type, conv->to_type,
11127 candidate->fn, conv->n_arg);
11128 return;
11129 }
11130
11131 tree arglist = build_tree_list_vec (user_args);
11132 tree errname = name;
11133 bool twiddle = false;
11134 if (IDENTIFIER_CDTOR_P (errname))
11135 {
11136 twiddle = IDENTIFIER_DTOR_P (errname);
11137 errname = constructor_name (basetype);
11138 }
11139 if (explicit_targs)
11140 errname = lookup_template_function (errname, explicit_targs);
11141 if (skip_first_for_error)
11142 arglist = TREE_CHAIN (arglist);
11143 error ("no matching function for call to %<%T::%s%E(%A)%#V%>",
11144 basetype, &"~"[!twiddle], errname, arglist,
11145 TREE_TYPE (instance));
11146 }
11147 print_z_candidates (location_of (name), candidates);
11148 }
11149
11150 /* Build a call to "INSTANCE.FN (ARGS)". If FN_P is non-NULL, it will
11151 be set, upon return, to the function called. ARGS may be NULL.
11152 This may change ARGS. */
11153
11154 tree
11155 build_new_method_call (tree instance, tree fns, vec<tree, va_gc> **args,
11156 tree conversion_path, int flags,
11157 tree *fn_p, tsubst_flags_t complain)
11158 {
11159 struct z_candidate *candidates = 0, *cand;
11160 tree explicit_targs = NULL_TREE;
11161 tree basetype = NULL_TREE;
11162 tree access_binfo;
11163 tree optype;
11164 tree first_mem_arg = NULL_TREE;
11165 tree name;
11166 bool skip_first_for_error;
11167 vec<tree, va_gc> *user_args;
11168 tree call;
11169 tree fn;
11170 int template_only = 0;
11171 bool any_viable_p;
11172 tree orig_instance;
11173 tree orig_fns;
11174 vec<tree, va_gc> *orig_args = NULL;
11175 void *p;
11176
11177 auto_cond_timevar tv (TV_OVERLOAD);
11178
11179 gcc_assert (instance != NULL_TREE);
11180
11181 /* We don't know what function we're going to call, yet. */
11182 if (fn_p)
11183 *fn_p = NULL_TREE;
11184
11185 if (error_operand_p (instance)
11186 || !fns || error_operand_p (fns))
11187 return error_mark_node;
11188
11189 if (!BASELINK_P (fns))
11190 {
11191 if (complain & tf_error)
11192 error ("call to non-function %qD", fns);
11193 return error_mark_node;
11194 }
11195
11196 orig_instance = instance;
11197 orig_fns = fns;
11198
11199 /* Dismantle the baselink to collect all the information we need. */
11200 if (!conversion_path)
11201 conversion_path = BASELINK_BINFO (fns);
11202 access_binfo = BASELINK_ACCESS_BINFO (fns);
11203 optype = BASELINK_OPTYPE (fns);
11204 fns = BASELINK_FUNCTIONS (fns);
11205 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
11206 {
11207 explicit_targs = TREE_OPERAND (fns, 1);
11208 fns = TREE_OPERAND (fns, 0);
11209 template_only = 1;
11210 }
11211 gcc_assert (OVL_P (fns));
11212 fn = OVL_FIRST (fns);
11213 name = DECL_NAME (fn);
11214
11215 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (instance));
11216 gcc_assert (CLASS_TYPE_P (basetype));
11217
11218 user_args = args == NULL ? NULL : *args;
11219 /* Under DR 147 A::A() is an invalid constructor call,
11220 not a functional cast. */
11221 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
11222 {
11223 if (! (complain & tf_error))
11224 return error_mark_node;
11225
11226 basetype = DECL_CONTEXT (fn);
11227 name = constructor_name (basetype);
11228 auto_diagnostic_group d;
11229 if (permerror (input_location,
11230 "cannot call constructor %<%T::%D%> directly",
11231 basetype, name))
11232 inform (input_location, "for a function-style cast, remove the "
11233 "redundant %<::%D%>", name);
11234 call = build_functional_cast (input_location, basetype,
11235 build_tree_list_vec (user_args),
11236 complain);
11237 return call;
11238 }
11239
11240 if (processing_template_decl)
11241 {
11242 orig_args = args == NULL ? NULL : make_tree_vector_copy (*args);
11243 instance = build_non_dependent_expr (instance);
11244 if (args != NULL)
11245 make_args_non_dependent (*args);
11246 }
11247
11248 /* Process the argument list. */
11249 if (args != NULL && *args != NULL)
11250 {
11251 *args = resolve_args (*args, complain);
11252 if (*args == NULL)
11253 return error_mark_node;
11254 user_args = *args;
11255 }
11256
11257 /* Consider the object argument to be used even if we end up selecting a
11258 static member function. */
11259 instance = mark_type_use (instance);
11260
11261 /* Figure out whether to skip the first argument for the error
11262 message we will display to users if an error occurs. We don't
11263 want to display any compiler-generated arguments. The "this"
11264 pointer hasn't been added yet. However, we must remove the VTT
11265 pointer if this is a call to a base-class constructor or
11266 destructor. */
11267 skip_first_for_error = false;
11268 if (IDENTIFIER_CDTOR_P (name))
11269 {
11270 /* Callers should explicitly indicate whether they want to ctor
11271 the complete object or just the part without virtual bases. */
11272 gcc_assert (name != ctor_identifier);
11273
11274 /* Remove the VTT pointer, if present. */
11275 if ((name == base_ctor_identifier || name == base_dtor_identifier)
11276 && CLASSTYPE_VBASECLASSES (basetype))
11277 skip_first_for_error = true;
11278
11279 /* It's OK to call destructors and constructors on cv-qualified
11280 objects. Therefore, convert the INSTANCE to the unqualified
11281 type, if necessary. */
11282 if (!same_type_p (basetype, TREE_TYPE (instance)))
11283 {
11284 instance = build_this (instance);
11285 instance = build_nop (build_pointer_type (basetype), instance);
11286 instance = build_fold_indirect_ref (instance);
11287 }
11288 }
11289 else
11290 gcc_assert (!DECL_DESTRUCTOR_P (fn) && !DECL_CONSTRUCTOR_P (fn));
11291
11292 /* For the overload resolution we need to find the actual `this`
11293 that would be captured if the call turns out to be to a
11294 non-static member function. Do not actually capture it at this
11295 point. */
11296 if (DECL_CONSTRUCTOR_P (fn))
11297 /* Constructors don't use the enclosing 'this'. */
11298 first_mem_arg = instance;
11299 else
11300 first_mem_arg = maybe_resolve_dummy (instance, false);
11301
11302 /* Get the high-water mark for the CONVERSION_OBSTACK. */
11303 p = conversion_obstack_alloc (0);
11304
11305 /* The number of arguments artificial parms in ARGS; we subtract one because
11306 there's no 'this' in ARGS. */
11307 unsigned skip = num_artificial_parms_for (fn) - 1;
11308
11309 /* If CONSTRUCTOR_IS_DIRECT_INIT is set, this was a T{ } form
11310 initializer, not T({ }). */
11311 if (DECL_CONSTRUCTOR_P (fn)
11312 && vec_safe_length (user_args) > skip
11313 && DIRECT_LIST_INIT_P ((*user_args)[skip]))
11314 {
11315 tree init_list = (*user_args)[skip];
11316 tree init = NULL_TREE;
11317
11318 gcc_assert (user_args->length () == skip + 1
11319 && !(flags & LOOKUP_ONLYCONVERTING));
11320
11321 /* If the initializer list has no elements and T is a class type with
11322 a default constructor, the object is value-initialized. Handle
11323 this here so we don't need to handle it wherever we use
11324 build_special_member_call. */
11325 if (CONSTRUCTOR_NELTS (init_list) == 0
11326 && TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype)
11327 /* For a user-provided default constructor, use the normal
11328 mechanisms so that protected access works. */
11329 && type_has_non_user_provided_default_constructor (basetype)
11330 && !processing_template_decl)
11331 init = build_value_init (basetype, complain);
11332
11333 /* If BASETYPE is an aggregate, we need to do aggregate
11334 initialization. */
11335 else if (CP_AGGREGATE_TYPE_P (basetype))
11336 {
11337 init = reshape_init (basetype, init_list, complain);
11338 init = digest_init (basetype, init, complain);
11339 }
11340
11341 if (init)
11342 {
11343 if (is_dummy_object (instance))
11344 return get_target_expr (init, complain);
11345 return cp_build_init_expr (instance, init);
11346 }
11347
11348 /* Otherwise go ahead with overload resolution. */
11349 add_list_candidates (fns, first_mem_arg, user_args,
11350 basetype, explicit_targs, template_only,
11351 conversion_path, access_binfo, flags,
11352 &candidates, complain);
11353 }
11354 else
11355 add_candidates (fns, first_mem_arg, user_args, optype,
11356 explicit_targs, template_only, conversion_path,
11357 access_binfo, flags, &candidates, complain);
11358
11359 any_viable_p = false;
11360 candidates = splice_viable (candidates, false, &any_viable_p);
11361
11362 if (!any_viable_p)
11363 {
11364 /* [dcl.init], 17.6.2.2:
11365
11366 Otherwise, if no constructor is viable, the destination type is
11367 a (possibly cv-qualified) aggregate class A, and the initializer
11368 is a parenthesized expression-list, the object is initialized as
11369 follows...
11370
11371 We achieve this by building up a CONSTRUCTOR, as for list-init,
11372 and setting CONSTRUCTOR_IS_PAREN_INIT to distinguish between
11373 the two. */
11374 if (DECL_CONSTRUCTOR_P (fn)
11375 && !(flags & LOOKUP_ONLYCONVERTING)
11376 && cxx_dialect >= cxx20
11377 && CP_AGGREGATE_TYPE_P (basetype)
11378 && !vec_safe_is_empty (user_args))
11379 {
11380 /* Create a CONSTRUCTOR from ARGS, e.g. {1, 2} from <1, 2>. */
11381 tree ctor = build_constructor_from_vec (init_list_type_node,
11382 user_args);
11383 CONSTRUCTOR_IS_DIRECT_INIT (ctor) = true;
11384 CONSTRUCTOR_IS_PAREN_INIT (ctor) = true;
11385 if (is_dummy_object (instance))
11386 return ctor;
11387 else
11388 {
11389 ctor = digest_init (basetype, ctor, complain);
11390 if (ctor == error_mark_node)
11391 return error_mark_node;
11392 return cp_build_init_expr (instance, ctor);
11393 }
11394 }
11395 if (complain & tf_error)
11396 complain_about_no_candidates_for_method_call (instance, candidates,
11397 explicit_targs, basetype,
11398 optype, name,
11399 skip_first_for_error,
11400 user_args);
11401 call = error_mark_node;
11402 }
11403 else
11404 {
11405 cand = tourney (candidates, complain);
11406 if (cand == 0)
11407 {
11408 char *pretty_name;
11409 bool free_p;
11410 tree arglist;
11411
11412 if (complain & tf_error)
11413 {
11414 pretty_name = name_as_c_string (name, basetype, &free_p);
11415 arglist = build_tree_list_vec (user_args);
11416 if (skip_first_for_error)
11417 arglist = TREE_CHAIN (arglist);
11418 auto_diagnostic_group d;
11419 if (!any_strictly_viable (candidates))
11420 error ("no matching function for call to %<%s(%A)%>",
11421 pretty_name, arglist);
11422 else
11423 error ("call of overloaded %<%s(%A)%> is ambiguous",
11424 pretty_name, arglist);
11425 print_z_candidates (location_of (name), candidates);
11426 if (free_p)
11427 free (pretty_name);
11428 }
11429 call = error_mark_node;
11430 if (fn_p)
11431 *fn_p = error_mark_node;
11432 }
11433 else
11434 {
11435 fn = cand->fn;
11436 call = NULL_TREE;
11437
11438 if (!(flags & LOOKUP_NONVIRTUAL)
11439 && DECL_PURE_VIRTUAL_P (fn)
11440 && instance == current_class_ref
11441 && (complain & tf_warning))
11442 {
11443 /* This is not an error, it is runtime undefined
11444 behavior. */
11445 if (!current_function_decl)
11446 warning (0, "pure virtual %q#D called from "
11447 "non-static data member initializer", fn);
11448 else if (DECL_CONSTRUCTOR_P (current_function_decl)
11449 || DECL_DESTRUCTOR_P (current_function_decl))
11450 warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
11451 ? G_("pure virtual %q#D called from constructor")
11452 : G_("pure virtual %q#D called from destructor")),
11453 fn);
11454 }
11455
11456 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
11457 && !DECL_CONSTRUCTOR_P (fn)
11458 && is_dummy_object (instance))
11459 {
11460 instance = maybe_resolve_dummy (instance, true);
11461 if (instance == error_mark_node)
11462 call = error_mark_node;
11463 else if (!is_dummy_object (instance))
11464 {
11465 /* We captured 'this' in the current lambda now that
11466 we know we really need it. */
11467 cand->first_arg = instance;
11468 }
11469 else if (current_class_ptr && any_dependent_bases_p ())
11470 /* We can't tell until instantiation time whether we can use
11471 *this as the implicit object argument. */;
11472 else
11473 {
11474 if (complain & tf_error)
11475 error ("cannot call member function %qD without object",
11476 fn);
11477 call = error_mark_node;
11478 }
11479 }
11480
11481 if (call != error_mark_node)
11482 {
11483 /* Now we know what function is being called. */
11484 if (fn_p)
11485 *fn_p = fn;
11486 /* Build the actual CALL_EXPR. */
11487 call = build_over_call (cand, flags, complain);
11488
11489 /* Suppress warnings for if (my_struct.operator= (x)) where
11490 my_struct is implicitly converted to bool. */
11491 if (TREE_CODE (call) == MODIFY_EXPR)
11492 suppress_warning (call, OPT_Wparentheses);
11493
11494 /* In an expression of the form `a->f()' where `f' turns
11495 out to be a static member function, `a' is
11496 none-the-less evaluated. */
11497 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
11498 && !is_dummy_object (instance)
11499 && TREE_SIDE_EFFECTS (instance))
11500 {
11501 /* But avoid the implicit lvalue-rvalue conversion when 'a'
11502 is volatile. */
11503 tree a = instance;
11504 if (TREE_THIS_VOLATILE (a))
11505 a = build_this (a);
11506 if (TREE_SIDE_EFFECTS (a))
11507 call = build2 (COMPOUND_EXPR, TREE_TYPE (call), a, call);
11508 }
11509 else if (call != error_mark_node
11510 && DECL_DESTRUCTOR_P (cand->fn)
11511 && !VOID_TYPE_P (TREE_TYPE (call)))
11512 /* An explicit call of the form "x->~X()" has type
11513 "void". However, on platforms where destructors
11514 return "this" (i.e., those where
11515 targetm.cxx.cdtor_returns_this is true), such calls
11516 will appear to have a return value of pointer type
11517 to the low-level call machinery. We do not want to
11518 change the low-level machinery, since we want to be
11519 able to optimize "delete f()" on such platforms as
11520 "operator delete(~X(f()))" (rather than generating
11521 "t = f(), ~X(t), operator delete (t)"). */
11522 call = build_nop (void_type_node, call);
11523 }
11524 }
11525 }
11526
11527 if (processing_template_decl && call != error_mark_node)
11528 {
11529 bool cast_to_void = false;
11530
11531 if (TREE_CODE (call) == COMPOUND_EXPR)
11532 call = TREE_OPERAND (call, 1);
11533 else if (TREE_CODE (call) == NOP_EXPR)
11534 {
11535 cast_to_void = true;
11536 call = TREE_OPERAND (call, 0);
11537 }
11538 if (INDIRECT_REF_P (call))
11539 call = TREE_OPERAND (call, 0);
11540
11541 /* Prune all but the selected function from the original overload
11542 set so that we can avoid some duplicate work at instantiation time. */
11543 if (really_overloaded_fn (fns))
11544 {
11545 if (DECL_TEMPLATE_INFO (fn)
11546 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (fn)))
11547 {
11548 /* Use the selected template, not the specialization, so that
11549 this looks like an actual lookup result for sake of
11550 filter_memfn_lookup. */
11551
11552 if (OVL_SINGLE_P (fns))
11553 /* If the original overload set consists of a single function
11554 template, this isn't beneficial. */
11555 goto skip_prune;
11556
11557 fn = ovl_make (DECL_TI_TEMPLATE (fn));
11558 if (template_only)
11559 fn = lookup_template_function (fn, explicit_targs);
11560 }
11561 orig_fns = copy_node (orig_fns);
11562 BASELINK_FUNCTIONS (orig_fns) = fn;
11563 BASELINK_FUNCTIONS_MAYBE_INCOMPLETE_P (orig_fns) = true;
11564 }
11565
11566 skip_prune:
11567 call = (build_min_non_dep_call_vec
11568 (call,
11569 build_min (COMPONENT_REF, TREE_TYPE (CALL_EXPR_FN (call)),
11570 orig_instance, orig_fns, NULL_TREE),
11571 orig_args));
11572 SET_EXPR_LOCATION (call, input_location);
11573 call = convert_from_reference (call);
11574 if (cast_to_void)
11575 call = build_nop (void_type_node, call);
11576 }
11577
11578 /* Free all the conversions we allocated. */
11579 obstack_free (&conversion_obstack, p);
11580
11581 if (orig_args != NULL)
11582 release_tree_vector (orig_args);
11583
11584 return call;
11585 }
11586
11587 /* Returns true iff standard conversion sequence ICS1 is a proper
11588 subsequence of ICS2. */
11589
11590 static bool
11591 is_subseq (conversion *ics1, conversion *ics2)
11592 {
11593 /* We can assume that a conversion of the same code
11594 between the same types indicates a subsequence since we only get
11595 here if the types we are converting from are the same. */
11596
11597 while (ics1->kind == ck_rvalue
11598 || ics1->kind == ck_lvalue)
11599 ics1 = next_conversion (ics1);
11600
11601 while (1)
11602 {
11603 while (ics2->kind == ck_rvalue
11604 || ics2->kind == ck_lvalue)
11605 ics2 = next_conversion (ics2);
11606
11607 if (ics2->kind == ck_user
11608 || !has_next (ics2->kind))
11609 /* At this point, ICS1 cannot be a proper subsequence of
11610 ICS2. We can get a USER_CONV when we are comparing the
11611 second standard conversion sequence of two user conversion
11612 sequences. */
11613 return false;
11614
11615 ics2 = next_conversion (ics2);
11616
11617 while (ics2->kind == ck_rvalue
11618 || ics2->kind == ck_lvalue)
11619 ics2 = next_conversion (ics2);
11620
11621 if (ics2->kind == ics1->kind
11622 && same_type_p (ics2->type, ics1->type)
11623 && (ics1->kind == ck_identity
11624 || same_type_p (next_conversion (ics2)->type,
11625 next_conversion (ics1)->type)))
11626 return true;
11627 }
11628 }
11629
11630 /* Returns nonzero iff DERIVED is derived from BASE. The inputs may
11631 be any _TYPE nodes. */
11632
11633 bool
11634 is_properly_derived_from (tree derived, tree base)
11635 {
11636 if (!CLASS_TYPE_P (derived) || !CLASS_TYPE_P (base))
11637 return false;
11638
11639 /* We only allow proper derivation here. The DERIVED_FROM_P macro
11640 considers every class derived from itself. */
11641 return (!same_type_ignoring_top_level_qualifiers_p (derived, base)
11642 && DERIVED_FROM_P (base, derived));
11643 }
11644
11645 /* We build the ICS for an implicit object parameter as a pointer
11646 conversion sequence. However, such a sequence should be compared
11647 as if it were a reference conversion sequence. If ICS is the
11648 implicit conversion sequence for an implicit object parameter,
11649 modify it accordingly. */
11650
11651 static void
11652 maybe_handle_implicit_object (conversion **ics)
11653 {
11654 if ((*ics)->this_p)
11655 {
11656 /* [over.match.funcs]
11657
11658 For non-static member functions, the type of the
11659 implicit object parameter is "reference to cv X"
11660 where X is the class of which the function is a
11661 member and cv is the cv-qualification on the member
11662 function declaration. */
11663 conversion *t = *ics;
11664 tree reference_type;
11665
11666 /* The `this' parameter is a pointer to a class type. Make the
11667 implicit conversion talk about a reference to that same class
11668 type. */
11669 reference_type = TREE_TYPE (t->type);
11670 reference_type = build_reference_type (reference_type);
11671
11672 if (t->kind == ck_qual)
11673 t = next_conversion (t);
11674 if (t->kind == ck_ptr)
11675 t = next_conversion (t);
11676 t = build_identity_conv (TREE_TYPE (t->type), NULL_TREE);
11677 t = direct_reference_binding (reference_type, t);
11678 t->this_p = 1;
11679 t->rvaluedness_matches_p = 0;
11680 *ics = t;
11681 }
11682 }
11683
11684 /* If *ICS is a REF_BIND set *ICS to the remainder of the conversion,
11685 and return the initial reference binding conversion. Otherwise,
11686 leave *ICS unchanged and return NULL. */
11687
11688 static conversion *
11689 maybe_handle_ref_bind (conversion **ics)
11690 {
11691 if ((*ics)->kind == ck_ref_bind)
11692 {
11693 conversion *old_ics = *ics;
11694 *ics = next_conversion (old_ics);
11695 (*ics)->user_conv_p = old_ics->user_conv_p;
11696 return old_ics;
11697 }
11698
11699 return NULL;
11700 }
11701
11702 /* Get the expression at the beginning of the conversion chain C. */
11703
11704 static tree
11705 conv_get_original_expr (conversion *c)
11706 {
11707 for (; c; c = next_conversion (c))
11708 if (c->kind == ck_identity || c->kind == ck_ambig || c->kind == ck_aggr)
11709 return c->u.expr;
11710 return NULL_TREE;
11711 }
11712
11713 /* Return a tree representing the number of elements initialized by the
11714 list-initialization C. The caller must check that C converts to an
11715 array type. */
11716
11717 static tree
11718 nelts_initialized_by_list_init (conversion *c)
11719 {
11720 /* If the array we're converting to has a dimension, we'll use that. */
11721 if (TYPE_DOMAIN (c->type))
11722 return array_type_nelts_top (c->type);
11723 else
11724 {
11725 /* Otherwise, we look at how many elements the constructor we're
11726 initializing from has. */
11727 tree ctor = conv_get_original_expr (c);
11728 return size_int (CONSTRUCTOR_NELTS (ctor));
11729 }
11730 }
11731
11732 /* True iff C is a conversion that binds a reference or a pointer to
11733 an array of unknown bound. */
11734
11735 static inline bool
11736 conv_binds_to_array_of_unknown_bound (conversion *c)
11737 {
11738 /* ck_ref_bind won't have the reference stripped. */
11739 tree type = non_reference (c->type);
11740 /* ck_qual won't have the pointer stripped. */
11741 type = strip_pointer_operator (type);
11742 return (TREE_CODE (type) == ARRAY_TYPE
11743 && TYPE_DOMAIN (type) == NULL_TREE);
11744 }
11745
11746 /* Compare two implicit conversion sequences according to the rules set out in
11747 [over.ics.rank]. Return values:
11748
11749 1: ics1 is better than ics2
11750 -1: ics2 is better than ics1
11751 0: ics1 and ics2 are indistinguishable */
11752
11753 static int
11754 compare_ics (conversion *ics1, conversion *ics2)
11755 {
11756 tree from_type1;
11757 tree from_type2;
11758 tree to_type1;
11759 tree to_type2;
11760 tree deref_from_type1 = NULL_TREE;
11761 tree deref_from_type2 = NULL_TREE;
11762 tree deref_to_type1 = NULL_TREE;
11763 tree deref_to_type2 = NULL_TREE;
11764 conversion_rank rank1, rank2;
11765
11766 /* REF_BINDING is nonzero if the result of the conversion sequence
11767 is a reference type. In that case REF_CONV is the reference
11768 binding conversion. */
11769 conversion *ref_conv1;
11770 conversion *ref_conv2;
11771
11772 /* Compare badness before stripping the reference conversion. */
11773 if (ics1->bad_p > ics2->bad_p)
11774 return -1;
11775 else if (ics1->bad_p < ics2->bad_p)
11776 return 1;
11777
11778 /* Handle implicit object parameters. */
11779 maybe_handle_implicit_object (&ics1);
11780 maybe_handle_implicit_object (&ics2);
11781
11782 /* Handle reference parameters. */
11783 ref_conv1 = maybe_handle_ref_bind (&ics1);
11784 ref_conv2 = maybe_handle_ref_bind (&ics2);
11785
11786 /* List-initialization sequence L1 is a better conversion sequence than
11787 list-initialization sequence L2 if L1 converts to
11788 std::initializer_list<X> for some X and L2 does not. */
11789 if (ics1->kind == ck_list && ics2->kind != ck_list)
11790 return 1;
11791 if (ics2->kind == ck_list && ics1->kind != ck_list)
11792 return -1;
11793
11794 /* [over.ics.rank]
11795
11796 When comparing the basic forms of implicit conversion sequences (as
11797 defined in _over.best.ics_)
11798
11799 --a standard conversion sequence (_over.ics.scs_) is a better
11800 conversion sequence than a user-defined conversion sequence
11801 or an ellipsis conversion sequence, and
11802
11803 --a user-defined conversion sequence (_over.ics.user_) is a
11804 better conversion sequence than an ellipsis conversion sequence
11805 (_over.ics.ellipsis_). */
11806 /* Use BAD_CONVERSION_RANK because we already checked for a badness
11807 mismatch. If both ICS are bad, we try to make a decision based on
11808 what would have happened if they'd been good. This is not an
11809 extension, we'll still give an error when we build up the call; this
11810 just helps us give a more helpful error message. */
11811 rank1 = BAD_CONVERSION_RANK (ics1);
11812 rank2 = BAD_CONVERSION_RANK (ics2);
11813
11814 if (rank1 > rank2)
11815 return -1;
11816 else if (rank1 < rank2)
11817 return 1;
11818
11819 if (ics1->ellipsis_p)
11820 /* Both conversions are ellipsis conversions. */
11821 return 0;
11822
11823 /* User-defined conversion sequence U1 is a better conversion sequence
11824 than another user-defined conversion sequence U2 if they contain the
11825 same user-defined conversion operator or constructor and if the sec-
11826 ond standard conversion sequence of U1 is better than the second
11827 standard conversion sequence of U2. */
11828
11829 /* Handle list-conversion with the same code even though it isn't always
11830 ranked as a user-defined conversion and it doesn't have a second
11831 standard conversion sequence; it will still have the desired effect.
11832 Specifically, we need to do the reference binding comparison at the
11833 end of this function. */
11834
11835 if (ics1->user_conv_p || ics1->kind == ck_list
11836 || ics1->kind == ck_aggr || ics2->kind == ck_aggr)
11837 {
11838 conversion *t1 = strip_standard_conversion (ics1);
11839 conversion *t2 = strip_standard_conversion (ics2);
11840
11841 if (!t1 || !t2 || t1->kind != t2->kind)
11842 return 0;
11843 else if (t1->kind == ck_user)
11844 {
11845 tree f1 = t1->cand ? t1->cand->fn : t1->type;
11846 tree f2 = t2->cand ? t2->cand->fn : t2->type;
11847 if (f1 != f2)
11848 return 0;
11849 }
11850 /* List-initialization sequence L1 is a better conversion sequence than
11851 list-initialization sequence L2 if
11852
11853 -- L1 and L2 convert to arrays of the same element type, and either
11854 the number of elements n1 initialized by L1 is less than the number
11855 of elements n2 initialized by L2, or n1=n2 and L2 converts to an array
11856 of unknown bound and L1 does not. (Added in CWG 1307 and extended by
11857 P0388R4.) */
11858 else if (t1->kind == ck_aggr
11859 && TREE_CODE (t1->type) == ARRAY_TYPE
11860 && TREE_CODE (t2->type) == ARRAY_TYPE
11861 && same_type_p (TREE_TYPE (t1->type), TREE_TYPE (t2->type)))
11862 {
11863 tree n1 = nelts_initialized_by_list_init (t1);
11864 tree n2 = nelts_initialized_by_list_init (t2);
11865 if (tree_int_cst_lt (n1, n2))
11866 return 1;
11867 else if (tree_int_cst_lt (n2, n1))
11868 return -1;
11869 /* The n1 == n2 case. */
11870 bool c1 = conv_binds_to_array_of_unknown_bound (t1);
11871 bool c2 = conv_binds_to_array_of_unknown_bound (t2);
11872 if (c1 && !c2)
11873 return -1;
11874 else if (!c1 && c2)
11875 return 1;
11876 else
11877 return 0;
11878 }
11879 else
11880 {
11881 /* For ambiguous or aggregate conversions, use the target type as
11882 a proxy for the conversion function. */
11883 if (!same_type_ignoring_top_level_qualifiers_p (t1->type, t2->type))
11884 return 0;
11885 }
11886
11887 /* We can just fall through here, after setting up
11888 FROM_TYPE1 and FROM_TYPE2. */
11889 from_type1 = t1->type;
11890 from_type2 = t2->type;
11891 }
11892 else
11893 {
11894 conversion *t1;
11895 conversion *t2;
11896
11897 /* We're dealing with two standard conversion sequences.
11898
11899 [over.ics.rank]
11900
11901 Standard conversion sequence S1 is a better conversion
11902 sequence than standard conversion sequence S2 if
11903
11904 --S1 is a proper subsequence of S2 (comparing the conversion
11905 sequences in the canonical form defined by _over.ics.scs_,
11906 excluding any Lvalue Transformation; the identity
11907 conversion sequence is considered to be a subsequence of
11908 any non-identity conversion sequence */
11909
11910 t1 = ics1;
11911 while (t1->kind != ck_identity)
11912 t1 = next_conversion (t1);
11913 from_type1 = t1->type;
11914
11915 t2 = ics2;
11916 while (t2->kind != ck_identity)
11917 t2 = next_conversion (t2);
11918 from_type2 = t2->type;
11919 }
11920
11921 /* One sequence can only be a subsequence of the other if they start with
11922 the same type. They can start with different types when comparing the
11923 second standard conversion sequence in two user-defined conversion
11924 sequences. */
11925 if (same_type_p (from_type1, from_type2))
11926 {
11927 if (is_subseq (ics1, ics2))
11928 return 1;
11929 if (is_subseq (ics2, ics1))
11930 return -1;
11931 }
11932
11933 /* [over.ics.rank]
11934
11935 Or, if not that,
11936
11937 --the rank of S1 is better than the rank of S2 (by the rules
11938 defined below):
11939
11940 Standard conversion sequences are ordered by their ranks: an Exact
11941 Match is a better conversion than a Promotion, which is a better
11942 conversion than a Conversion.
11943
11944 Two conversion sequences with the same rank are indistinguishable
11945 unless one of the following rules applies:
11946
11947 --A conversion that does not a convert a pointer, pointer to member,
11948 or std::nullptr_t to bool is better than one that does.
11949
11950 The ICS_STD_RANK automatically handles the pointer-to-bool rule,
11951 so that we do not have to check it explicitly. */
11952 if (ics1->rank < ics2->rank)
11953 return 1;
11954 else if (ics2->rank < ics1->rank)
11955 return -1;
11956
11957 to_type1 = ics1->type;
11958 to_type2 = ics2->type;
11959
11960 /* A conversion from scalar arithmetic type to complex is worse than a
11961 conversion between scalar arithmetic types. */
11962 if (same_type_p (from_type1, from_type2)
11963 && ARITHMETIC_TYPE_P (from_type1)
11964 && ARITHMETIC_TYPE_P (to_type1)
11965 && ARITHMETIC_TYPE_P (to_type2)
11966 && ((TREE_CODE (to_type1) == COMPLEX_TYPE)
11967 != (TREE_CODE (to_type2) == COMPLEX_TYPE)))
11968 {
11969 if (TREE_CODE (to_type1) == COMPLEX_TYPE)
11970 return -1;
11971 else
11972 return 1;
11973 }
11974
11975 {
11976 /* A conversion in either direction between floating-point type FP1 and
11977 floating-point type FP2 is better than a conversion in the same
11978 direction between FP1 and arithmetic type T3 if
11979 - the floating-point conversion rank of FP1 is equal to the rank of
11980 FP2, and
11981 - T3 is not a floating-point type, or T3 is a floating-point type
11982 whose rank is not equal to the rank of FP1, or the floating-point
11983 conversion subrank of FP2 is greater than the subrank of T3. */
11984 tree fp1 = from_type1;
11985 tree fp2 = to_type1;
11986 tree fp3 = from_type2;
11987 tree t3 = to_type2;
11988 int ret = 1;
11989 if (TYPE_MAIN_VARIANT (fp2) == TYPE_MAIN_VARIANT (t3))
11990 {
11991 std::swap (fp1, fp2);
11992 std::swap (fp3, t3);
11993 }
11994 if (TYPE_MAIN_VARIANT (fp1) == TYPE_MAIN_VARIANT (fp3)
11995 && TREE_CODE (fp1) == REAL_TYPE
11996 /* Only apply this rule if at least one of the 3 types is
11997 extended floating-point type, otherwise keep them as
11998 before for compatibility reasons with types like __float128.
11999 float, double and long double alone have different conversion
12000 ranks and so when just those 3 types are involved, this
12001 rule doesn't trigger. */
12002 && (extended_float_type_p (fp1)
12003 || (TREE_CODE (fp2) == REAL_TYPE && extended_float_type_p (fp2))
12004 || (TREE_CODE (t3) == REAL_TYPE && extended_float_type_p (t3))))
12005 {
12006 if (TREE_CODE (fp2) != REAL_TYPE)
12007 {
12008 ret = -ret;
12009 std::swap (fp2, t3);
12010 }
12011 if (TREE_CODE (fp2) == REAL_TYPE)
12012 {
12013 /* cp_compare_floating_point_conversion_ranks returns -1, 0 or 1
12014 if the conversion rank is equal (-1 or 1 if the subrank is
12015 different). */
12016 if (IN_RANGE (cp_compare_floating_point_conversion_ranks (fp1,
12017 fp2),
12018 -1, 1))
12019 {
12020 /* Conversion ranks of FP1 and FP2 are equal. */
12021 if (TREE_CODE (t3) != REAL_TYPE
12022 || !IN_RANGE (cp_compare_floating_point_conversion_ranks
12023 (fp1, t3),
12024 -1, 1))
12025 /* FP1 <-> FP2 conversion is better. */
12026 return ret;
12027 int c = cp_compare_floating_point_conversion_ranks (fp2, t3);
12028 gcc_assert (IN_RANGE (c, -1, 1));
12029 if (c == 1)
12030 /* Conversion subrank of FP2 is greater than subrank of T3.
12031 FP1 <-> FP2 conversion is better. */
12032 return ret;
12033 else if (c == -1)
12034 /* Conversion subrank of FP2 is less than subrank of T3.
12035 FP1 <-> T3 conversion is better. */
12036 return -ret;
12037 }
12038 else if (TREE_CODE (t3) == REAL_TYPE
12039 && IN_RANGE (cp_compare_floating_point_conversion_ranks
12040 (fp1, t3),
12041 -1, 1))
12042 /* Conversion ranks of FP1 and FP2 are not equal, conversion
12043 ranks of FP1 and T3 are equal.
12044 FP1 <-> T3 conversion is better. */
12045 return -ret;
12046 }
12047 }
12048 }
12049
12050 if (TYPE_PTR_P (from_type1)
12051 && TYPE_PTR_P (from_type2)
12052 && TYPE_PTR_P (to_type1)
12053 && TYPE_PTR_P (to_type2))
12054 {
12055 deref_from_type1 = TREE_TYPE (from_type1);
12056 deref_from_type2 = TREE_TYPE (from_type2);
12057 deref_to_type1 = TREE_TYPE (to_type1);
12058 deref_to_type2 = TREE_TYPE (to_type2);
12059 }
12060 /* The rules for pointers to members A::* are just like the rules
12061 for pointers A*, except opposite: if B is derived from A then
12062 A::* converts to B::*, not vice versa. For that reason, we
12063 switch the from_ and to_ variables here. */
12064 else if ((TYPE_PTRDATAMEM_P (from_type1) && TYPE_PTRDATAMEM_P (from_type2)
12065 && TYPE_PTRDATAMEM_P (to_type1) && TYPE_PTRDATAMEM_P (to_type2))
12066 || (TYPE_PTRMEMFUNC_P (from_type1)
12067 && TYPE_PTRMEMFUNC_P (from_type2)
12068 && TYPE_PTRMEMFUNC_P (to_type1)
12069 && TYPE_PTRMEMFUNC_P (to_type2)))
12070 {
12071 deref_to_type1 = TYPE_PTRMEM_CLASS_TYPE (from_type1);
12072 deref_to_type2 = TYPE_PTRMEM_CLASS_TYPE (from_type2);
12073 deref_from_type1 = TYPE_PTRMEM_CLASS_TYPE (to_type1);
12074 deref_from_type2 = TYPE_PTRMEM_CLASS_TYPE (to_type2);
12075 }
12076
12077 if (deref_from_type1 != NULL_TREE
12078 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type1))
12079 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_from_type2)))
12080 {
12081 /* This was one of the pointer or pointer-like conversions.
12082
12083 [over.ics.rank]
12084
12085 --If class B is derived directly or indirectly from class A,
12086 conversion of B* to A* is better than conversion of B* to
12087 void*, and conversion of A* to void* is better than
12088 conversion of B* to void*. */
12089 if (VOID_TYPE_P (deref_to_type1)
12090 && VOID_TYPE_P (deref_to_type2))
12091 {
12092 if (is_properly_derived_from (deref_from_type1,
12093 deref_from_type2))
12094 return -1;
12095 else if (is_properly_derived_from (deref_from_type2,
12096 deref_from_type1))
12097 return 1;
12098 }
12099 else if (VOID_TYPE_P (deref_to_type1)
12100 || VOID_TYPE_P (deref_to_type2))
12101 {
12102 if (same_type_p (deref_from_type1, deref_from_type2))
12103 {
12104 if (VOID_TYPE_P (deref_to_type2))
12105 {
12106 if (is_properly_derived_from (deref_from_type1,
12107 deref_to_type1))
12108 return 1;
12109 }
12110 /* We know that DEREF_TO_TYPE1 is `void' here. */
12111 else if (is_properly_derived_from (deref_from_type1,
12112 deref_to_type2))
12113 return -1;
12114 }
12115 }
12116 else if (RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type1))
12117 && RECORD_OR_UNION_CODE_P (TREE_CODE (deref_to_type2)))
12118 {
12119 /* [over.ics.rank]
12120
12121 --If class B is derived directly or indirectly from class A
12122 and class C is derived directly or indirectly from B,
12123
12124 --conversion of C* to B* is better than conversion of C* to
12125 A*,
12126
12127 --conversion of B* to A* is better than conversion of C* to
12128 A* */
12129 if (same_type_p (deref_from_type1, deref_from_type2))
12130 {
12131 if (is_properly_derived_from (deref_to_type1,
12132 deref_to_type2))
12133 return 1;
12134 else if (is_properly_derived_from (deref_to_type2,
12135 deref_to_type1))
12136 return -1;
12137 }
12138 else if (same_type_p (deref_to_type1, deref_to_type2))
12139 {
12140 if (is_properly_derived_from (deref_from_type2,
12141 deref_from_type1))
12142 return 1;
12143 else if (is_properly_derived_from (deref_from_type1,
12144 deref_from_type2))
12145 return -1;
12146 }
12147 }
12148 }
12149 else if (CLASS_TYPE_P (non_reference (from_type1))
12150 && same_type_p (from_type1, from_type2))
12151 {
12152 tree from = non_reference (from_type1);
12153
12154 /* [over.ics.rank]
12155
12156 --binding of an expression of type C to a reference of type
12157 B& is better than binding an expression of type C to a
12158 reference of type A&
12159
12160 --conversion of C to B is better than conversion of C to A, */
12161 if (is_properly_derived_from (from, to_type1)
12162 && is_properly_derived_from (from, to_type2))
12163 {
12164 if (is_properly_derived_from (to_type1, to_type2))
12165 return 1;
12166 else if (is_properly_derived_from (to_type2, to_type1))
12167 return -1;
12168 }
12169 }
12170 else if (CLASS_TYPE_P (non_reference (to_type1))
12171 && same_type_p (to_type1, to_type2))
12172 {
12173 tree to = non_reference (to_type1);
12174
12175 /* [over.ics.rank]
12176
12177 --binding of an expression of type B to a reference of type
12178 A& is better than binding an expression of type C to a
12179 reference of type A&,
12180
12181 --conversion of B to A is better than conversion of C to A */
12182 if (is_properly_derived_from (from_type1, to)
12183 && is_properly_derived_from (from_type2, to))
12184 {
12185 if (is_properly_derived_from (from_type2, from_type1))
12186 return 1;
12187 else if (is_properly_derived_from (from_type1, from_type2))
12188 return -1;
12189 }
12190 }
12191
12192 /* [over.ics.rank]
12193
12194 --S1 and S2 differ only in their qualification conversion and yield
12195 similar types T1 and T2 (_conv.qual_), respectively, and the cv-
12196 qualification signature of type T1 is a proper subset of the cv-
12197 qualification signature of type T2 */
12198 if (ics1->kind == ck_qual
12199 && ics2->kind == ck_qual
12200 && same_type_p (from_type1, from_type2))
12201 {
12202 int result = comp_cv_qual_signature (to_type1, to_type2);
12203 if (result != 0)
12204 return result;
12205 }
12206
12207 /* [over.ics.rank]
12208
12209 --S1 and S2 are reference bindings (_dcl.init.ref_) and neither refers
12210 to an implicit object parameter of a non-static member function
12211 declared without a ref-qualifier, and either S1 binds an lvalue
12212 reference to an lvalue and S2 binds an rvalue reference or S1 binds an
12213 rvalue reference to an rvalue and S2 binds an lvalue reference (C++0x
12214 draft standard, 13.3.3.2)
12215
12216 --S1 and S2 are reference bindings (_dcl.init.ref_), and the
12217 types to which the references refer are the same type except for
12218 top-level cv-qualifiers, and the type to which the reference
12219 initialized by S2 refers is more cv-qualified than the type to
12220 which the reference initialized by S1 refers.
12221
12222 DR 1328 [over.match.best]: the context is an initialization by
12223 conversion function for direct reference binding (13.3.1.6) of a
12224 reference to function type, the return type of F1 is the same kind of
12225 reference (i.e. lvalue or rvalue) as the reference being initialized,
12226 and the return type of F2 is not. */
12227
12228 if (ref_conv1 && ref_conv2)
12229 {
12230 if (!ref_conv1->this_p && !ref_conv2->this_p
12231 && (ref_conv1->rvaluedness_matches_p
12232 != ref_conv2->rvaluedness_matches_p)
12233 && (same_type_p (ref_conv1->type, ref_conv2->type)
12234 || (TYPE_REF_IS_RVALUE (ref_conv1->type)
12235 != TYPE_REF_IS_RVALUE (ref_conv2->type))))
12236 {
12237 if (ref_conv1->bad_p
12238 && !same_type_p (TREE_TYPE (ref_conv1->type),
12239 TREE_TYPE (ref_conv2->type)))
12240 /* Don't prefer a bad conversion that drops cv-quals to a bad
12241 conversion with the wrong rvalueness. */
12242 return 0;
12243 return (ref_conv1->rvaluedness_matches_p
12244 - ref_conv2->rvaluedness_matches_p);
12245 }
12246
12247 if (same_type_ignoring_top_level_qualifiers_p (to_type1, to_type2))
12248 {
12249 /* Per P0388R4:
12250
12251 void f (int(&)[]), // (1)
12252 f (int(&)[1]), // (2)
12253 f (int*); // (3)
12254
12255 (2) is better than (1), but (3) should be equal to (1) and to
12256 (2). For that reason we don't use ck_qual for (1) which would
12257 give it the cr_exact rank while (3) remains ck_identity.
12258 Therefore we compare (1) and (2) here. For (1) we'll have
12259
12260 ck_ref_bind <- ck_identity
12261 int[] & int[1]
12262
12263 so to handle this we must look at ref_conv. */
12264 bool c1 = conv_binds_to_array_of_unknown_bound (ref_conv1);
12265 bool c2 = conv_binds_to_array_of_unknown_bound (ref_conv2);
12266 if (c1 && !c2)
12267 return -1;
12268 else if (!c1 && c2)
12269 return 1;
12270
12271 int q1 = cp_type_quals (TREE_TYPE (ref_conv1->type));
12272 int q2 = cp_type_quals (TREE_TYPE (ref_conv2->type));
12273 if (ref_conv1->bad_p)
12274 {
12275 /* Prefer the one that drops fewer cv-quals. */
12276 tree ftype = next_conversion (ref_conv1)->type;
12277 int fquals = cp_type_quals (ftype);
12278 q1 ^= fquals;
12279 q2 ^= fquals;
12280 }
12281 return comp_cv_qualification (q2, q1);
12282 }
12283 }
12284
12285 /* [over.ics.rank]
12286
12287 Per CWG 1601:
12288 -- A conversion that promotes an enumeration whose underlying type
12289 is fixed to its underlying type is better than one that promotes to
12290 the promoted underlying type, if the two are different. */
12291 if (ics1->rank == cr_promotion
12292 && ics2->rank == cr_promotion
12293 && UNSCOPED_ENUM_P (from_type1)
12294 && ENUM_FIXED_UNDERLYING_TYPE_P (from_type1)
12295 && same_type_p (from_type1, from_type2))
12296 {
12297 tree utype = ENUM_UNDERLYING_TYPE (from_type1);
12298 tree prom = type_promotes_to (from_type1);
12299 if (!same_type_p (utype, prom))
12300 {
12301 if (same_type_p (to_type1, utype)
12302 && same_type_p (to_type2, prom))
12303 return 1;
12304 else if (same_type_p (to_type2, utype)
12305 && same_type_p (to_type1, prom))
12306 return -1;
12307 }
12308 }
12309
12310 /* Neither conversion sequence is better than the other. */
12311 return 0;
12312 }
12313
12314 /* The source type for this standard conversion sequence. */
12315
12316 static tree
12317 source_type (conversion *t)
12318 {
12319 return strip_standard_conversion (t)->type;
12320 }
12321
12322 /* Note a warning about preferring WINNER to LOSER. We do this by storing
12323 a pointer to LOSER and re-running joust to produce the warning if WINNER
12324 is actually used. */
12325
12326 static void
12327 add_warning (struct z_candidate *winner, struct z_candidate *loser)
12328 {
12329 candidate_warning *cw = (candidate_warning *)
12330 conversion_obstack_alloc (sizeof (candidate_warning));
12331 cw->loser = loser;
12332 cw->next = winner->warnings;
12333 winner->warnings = cw;
12334 }
12335
12336 /* CAND is a constructor candidate in joust in C++17 and up. If it copies a
12337 prvalue returned from a conversion function, replace CAND with the candidate
12338 for the conversion and return true. Otherwise, return false. */
12339
12340 static bool
12341 joust_maybe_elide_copy (z_candidate *&cand)
12342 {
12343 tree fn = cand->fn;
12344 if (!DECL_COPY_CONSTRUCTOR_P (fn) && !DECL_MOVE_CONSTRUCTOR_P (fn))
12345 return false;
12346 conversion *conv = cand->convs[0];
12347 gcc_checking_assert (conv->kind == ck_ref_bind);
12348 conv = next_conversion (conv);
12349 if (conv->kind == ck_user && !TYPE_REF_P (conv->type))
12350 {
12351 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
12352 (conv->type, DECL_CONTEXT (fn)));
12353 z_candidate *uc = conv->cand;
12354 if (DECL_CONV_FN_P (uc->fn))
12355 {
12356 cand = uc;
12357 return true;
12358 }
12359 }
12360 return false;
12361 }
12362
12363 /* True if the defining declarations of the two candidates have equivalent
12364 parameters. */
12365
12366 static bool
12367 cand_parms_match (z_candidate *c1, z_candidate *c2)
12368 {
12369 tree fn1 = c1->fn;
12370 tree fn2 = c2->fn;
12371 if (fn1 == fn2)
12372 return true;
12373 if (identifier_p (fn1) || identifier_p (fn2))
12374 return false;
12375 /* We don't look at c1->template_decl because that's only set for primary
12376 templates, not e.g. non-template member functions of class templates. */
12377 tree t1 = most_general_template (fn1);
12378 tree t2 = most_general_template (fn2);
12379 if (t1 || t2)
12380 {
12381 if (!t1 || !t2)
12382 return false;
12383 if (t1 == t2)
12384 return true;
12385 fn1 = DECL_TEMPLATE_RESULT (t1);
12386 fn2 = DECL_TEMPLATE_RESULT (t2);
12387 }
12388 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn1));
12389 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (fn2));
12390 if (DECL_FUNCTION_MEMBER_P (fn1)
12391 && DECL_FUNCTION_MEMBER_P (fn2)
12392 && (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn1)
12393 != DECL_NONSTATIC_MEMBER_FUNCTION_P (fn2)))
12394 {
12395 /* Ignore 'this' when comparing the parameters of a static member
12396 function with those of a non-static one. */
12397 parms1 = skip_artificial_parms_for (fn1, parms1);
12398 parms2 = skip_artificial_parms_for (fn2, parms2);
12399 }
12400 return compparms (parms1, parms2);
12401 }
12402
12403 /* Compare two candidates for overloading as described in
12404 [over.match.best]. Return values:
12405
12406 1: cand1 is better than cand2
12407 -1: cand2 is better than cand1
12408 0: cand1 and cand2 are indistinguishable */
12409
12410 static int
12411 joust (struct z_candidate *cand1, struct z_candidate *cand2, bool warn,
12412 tsubst_flags_t complain)
12413 {
12414 int winner = 0;
12415 int off1 = 0, off2 = 0;
12416 size_t i;
12417 size_t len;
12418
12419 /* Candidates that involve bad conversions are always worse than those
12420 that don't. */
12421 if (cand1->viable > cand2->viable)
12422 return 1;
12423 if (cand1->viable < cand2->viable)
12424 return -1;
12425
12426 /* If we have two pseudo-candidates for conversions to the same type,
12427 or two candidates for the same function, arbitrarily pick one. */
12428 if (cand1->fn == cand2->fn
12429 && cand1->reversed () == cand2->reversed ()
12430 && (IS_TYPE_OR_DECL_P (cand1->fn)))
12431 return 1;
12432
12433 /* Prefer a non-deleted function over an implicitly deleted move
12434 constructor or assignment operator. This differs slightly from the
12435 wording for issue 1402 (which says the move op is ignored by overload
12436 resolution), but this way produces better error messages. */
12437 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
12438 && TREE_CODE (cand2->fn) == FUNCTION_DECL
12439 && DECL_DELETED_FN (cand1->fn) != DECL_DELETED_FN (cand2->fn))
12440 {
12441 if (DECL_DELETED_FN (cand1->fn) && DECL_DEFAULTED_FN (cand1->fn)
12442 && move_fn_p (cand1->fn))
12443 return -1;
12444 if (DECL_DELETED_FN (cand2->fn) && DECL_DEFAULTED_FN (cand2->fn)
12445 && move_fn_p (cand2->fn))
12446 return 1;
12447 }
12448
12449 /* a viable function F1
12450 is defined to be a better function than another viable function F2 if
12451 for all arguments i, ICSi(F1) is not a worse conversion sequence than
12452 ICSi(F2), and then */
12453
12454 /* for some argument j, ICSj(F1) is a better conversion sequence than
12455 ICSj(F2) */
12456
12457 /* For comparing static and non-static member functions, we ignore
12458 the implicit object parameter of the non-static function. The
12459 standard says to pretend that the static function has an object
12460 parm, but that won't work with operator overloading. */
12461 len = cand1->num_convs;
12462 if (len != cand2->num_convs)
12463 {
12464 int static_1 = (TREE_CODE (cand1->fn) == FUNCTION_DECL
12465 && DECL_STATIC_FUNCTION_P (cand1->fn));
12466 int static_2 = (TREE_CODE (cand2->fn) == FUNCTION_DECL
12467 && DECL_STATIC_FUNCTION_P (cand2->fn));
12468
12469 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
12470 && TREE_CODE (cand2->fn) == FUNCTION_DECL
12471 && DECL_CONSTRUCTOR_P (cand1->fn)
12472 && is_list_ctor (cand1->fn) != is_list_ctor (cand2->fn))
12473 /* We're comparing a near-match list constructor and a near-match
12474 non-list constructor. Just treat them as unordered. */
12475 return 0;
12476
12477 gcc_assert (static_1 != static_2);
12478
12479 if (static_1)
12480 {
12481 /* C++23 [over.best.ics.general] says:
12482 When the parameter is the implicit object parameter of a static
12483 member function, the implicit conversion sequence is a standard
12484 conversion sequence that is neither better nor worse than any
12485 other standard conversion sequence. */
12486 if (CONVERSION_RANK (cand2->convs[0]) >= cr_user)
12487 winner = 1;
12488 off2 = 1;
12489 }
12490 else
12491 {
12492 if (CONVERSION_RANK (cand1->convs[0]) >= cr_user)
12493 winner = -1;
12494 off1 = 1;
12495 --len;
12496 }
12497 }
12498
12499 /* Handle C++17 copy elision in [over.match.ctor] (direct-init) context. The
12500 standard currently says that only constructors are candidates, but if one
12501 copies a prvalue returned by a conversion function we want to treat the
12502 conversion as the candidate instead.
12503
12504 Clang does something similar, as discussed at
12505 http://lists.isocpp.org/core/2017/10/3166.php
12506 http://lists.isocpp.org/core/2019/03/5721.php */
12507 int elided_tiebreaker = 0;
12508 if (len == 1 && cxx_dialect >= cxx17
12509 && DECL_P (cand1->fn)
12510 && DECL_COMPLETE_CONSTRUCTOR_P (cand1->fn)
12511 && !(cand1->flags & LOOKUP_ONLYCONVERTING))
12512 {
12513 bool elided1 = joust_maybe_elide_copy (cand1);
12514 bool elided2 = joust_maybe_elide_copy (cand2);
12515 /* As a tiebreaker below we will prefer a constructor to a conversion
12516 operator exposed this way. */
12517 elided_tiebreaker = elided2 - elided1;
12518 }
12519
12520 for (i = 0; i < len; ++i)
12521 {
12522 conversion *t1 = cand1->convs[i + off1];
12523 conversion *t2 = cand2->convs[i + off2];
12524 int comp = compare_ics (t1, t2);
12525
12526 if (comp != 0)
12527 {
12528 if ((complain & tf_warning)
12529 && warn_sign_promo
12530 && (CONVERSION_RANK (t1) + CONVERSION_RANK (t2)
12531 == cr_std + cr_promotion)
12532 && t1->kind == ck_std
12533 && t2->kind == ck_std
12534 && TREE_CODE (t1->type) == INTEGER_TYPE
12535 && TREE_CODE (t2->type) == INTEGER_TYPE
12536 && (TYPE_PRECISION (t1->type)
12537 == TYPE_PRECISION (t2->type))
12538 && (TYPE_UNSIGNED (next_conversion (t1)->type)
12539 || (TREE_CODE (next_conversion (t1)->type)
12540 == ENUMERAL_TYPE)))
12541 {
12542 tree type = next_conversion (t1)->type;
12543 tree type1, type2;
12544 struct z_candidate *w, *l;
12545 if (comp > 0)
12546 type1 = t1->type, type2 = t2->type,
12547 w = cand1, l = cand2;
12548 else
12549 type1 = t2->type, type2 = t1->type,
12550 w = cand2, l = cand1;
12551
12552 if (warn)
12553 {
12554 warning (OPT_Wsign_promo, "passing %qT chooses %qT over %qT",
12555 type, type1, type2);
12556 warning (OPT_Wsign_promo, " in call to %qD", w->fn);
12557 }
12558 else
12559 add_warning (w, l);
12560 }
12561
12562 if (winner && comp != winner)
12563 {
12564 /* Ambiguity between normal and reversed comparison operators
12565 with the same parameter types. P2468 decided not to go with
12566 this approach to resolving the ambiguity, so pedwarn. */
12567 if ((complain & tf_warning_or_error)
12568 && (cand1->reversed () != cand2->reversed ())
12569 && cand_parms_match (cand1, cand2))
12570 {
12571 struct z_candidate *w, *l;
12572 if (cand2->reversed ())
12573 winner = 1, w = cand1, l = cand2;
12574 else
12575 winner = -1, w = cand2, l = cand1;
12576 if (warn)
12577 {
12578 auto_diagnostic_group d;
12579 if (pedwarn (input_location, 0,
12580 "C++20 says that these are ambiguous, "
12581 "even though the second is reversed:"))
12582 {
12583 print_z_candidate (input_location,
12584 N_("candidate 1:"), w);
12585 print_z_candidate (input_location,
12586 N_("candidate 2:"), l);
12587 if (w->fn == l->fn
12588 && DECL_NONSTATIC_MEMBER_FUNCTION_P (w->fn)
12589 && (type_memfn_quals (TREE_TYPE (w->fn))
12590 & TYPE_QUAL_CONST) == 0)
12591 {
12592 /* Suggest adding const to
12593 struct A { bool operator==(const A&); }; */
12594 tree parmtype
12595 = FUNCTION_FIRST_USER_PARMTYPE (w->fn);
12596 parmtype = TREE_VALUE (parmtype);
12597 if (TYPE_REF_P (parmtype)
12598 && TYPE_READONLY (TREE_TYPE (parmtype))
12599 && (same_type_ignoring_top_level_qualifiers_p
12600 (TREE_TYPE (parmtype),
12601 DECL_CONTEXT (w->fn))))
12602 inform (DECL_SOURCE_LOCATION (w->fn),
12603 "try making the operator a %<const%> "
12604 "member function");
12605 }
12606 }
12607 }
12608 else
12609 add_warning (w, l);
12610 return winner;
12611 }
12612
12613 winner = 0;
12614 goto tweak;
12615 }
12616 winner = comp;
12617 }
12618 }
12619
12620 /* warn about confusing overload resolution for user-defined conversions,
12621 either between a constructor and a conversion op, or between two
12622 conversion ops. */
12623 if ((complain & tf_warning)
12624 /* In C++17, the constructor might have been elided, which means that
12625 an originally null ->second_conv could become non-null. */
12626 && winner && warn_conversion && cand1->second_conv && cand2->second_conv
12627 && (!DECL_CONSTRUCTOR_P (cand1->fn) || !DECL_CONSTRUCTOR_P (cand2->fn))
12628 && winner != compare_ics (cand1->second_conv, cand2->second_conv))
12629 {
12630 struct z_candidate *w, *l;
12631 bool give_warning = false;
12632
12633 if (winner == 1)
12634 w = cand1, l = cand2;
12635 else
12636 w = cand2, l = cand1;
12637
12638 /* We don't want to complain about `X::operator T1 ()'
12639 beating `X::operator T2 () const', when T2 is a no less
12640 cv-qualified version of T1. */
12641 if (DECL_CONTEXT (w->fn) == DECL_CONTEXT (l->fn)
12642 && !DECL_CONSTRUCTOR_P (w->fn) && !DECL_CONSTRUCTOR_P (l->fn))
12643 {
12644 tree t = TREE_TYPE (TREE_TYPE (l->fn));
12645 tree f = TREE_TYPE (TREE_TYPE (w->fn));
12646
12647 if (TREE_CODE (t) == TREE_CODE (f) && INDIRECT_TYPE_P (t))
12648 {
12649 t = TREE_TYPE (t);
12650 f = TREE_TYPE (f);
12651 }
12652 if (!comp_ptr_ttypes (t, f))
12653 give_warning = true;
12654 }
12655 else
12656 give_warning = true;
12657
12658 if (!give_warning)
12659 /*NOP*/;
12660 else if (warn)
12661 {
12662 tree source = source_type (w->convs[0]);
12663 if (INDIRECT_TYPE_P (source))
12664 source = TREE_TYPE (source);
12665 auto_diagnostic_group d;
12666 if (warning (OPT_Wconversion, "choosing %qD over %qD", w->fn, l->fn)
12667 && warning (OPT_Wconversion, " for conversion from %qH to %qI",
12668 source, w->second_conv->type))
12669 {
12670 inform (input_location, " because conversion sequence "
12671 "for the argument is better");
12672 }
12673 }
12674 else
12675 add_warning (w, l);
12676 }
12677
12678 if (winner)
12679 return winner;
12680
12681 /* Put this tiebreaker first, so that we don't try to look at second_conv of
12682 a constructor candidate that doesn't have one. */
12683 if (elided_tiebreaker)
12684 return elided_tiebreaker;
12685
12686 /* DR 495 moved this tiebreaker above the template ones. */
12687 /* or, if not that,
12688 the context is an initialization by user-defined conversion (see
12689 _dcl.init_ and _over.match.user_) and the standard conversion
12690 sequence from the return type of F1 to the destination type (i.e.,
12691 the type of the entity being initialized) is a better conversion
12692 sequence than the standard conversion sequence from the return type
12693 of F2 to the destination type. */
12694
12695 if (cand1->second_conv)
12696 {
12697 winner = compare_ics (cand1->second_conv, cand2->second_conv);
12698 if (winner)
12699 return winner;
12700 }
12701
12702 /* or, if not that,
12703 F1 is a non-template function and F2 is a template function
12704 specialization. */
12705
12706 if (!cand1->template_decl && cand2->template_decl)
12707 return 1;
12708 else if (cand1->template_decl && !cand2->template_decl)
12709 return -1;
12710
12711 /* or, if not that,
12712 F1 and F2 are template functions and the function template for F1 is
12713 more specialized than the template for F2 according to the partial
12714 ordering rules. */
12715
12716 if (cand1->template_decl && cand2->template_decl)
12717 {
12718 winner = more_specialized_fn
12719 (TI_TEMPLATE (cand1->template_decl),
12720 TI_TEMPLATE (cand2->template_decl),
12721 /* [temp.func.order]: The presence of unused ellipsis and default
12722 arguments has no effect on the partial ordering of function
12723 templates. add_function_candidate() will not have
12724 counted the "this" argument for constructors. */
12725 cand1->num_convs + DECL_CONSTRUCTOR_P (cand1->fn));
12726 if (winner)
12727 return winner;
12728 }
12729
12730 /* Concepts: F1 and F2 are non-template functions with the same
12731 parameter-type-lists, and F1 is more constrained than F2 according to the
12732 partial ordering of constraints described in 13.5.4. */
12733
12734 if (flag_concepts && DECL_P (cand1->fn) && DECL_P (cand2->fn)
12735 && !cand1->template_decl && !cand2->template_decl
12736 && cand_parms_match (cand1, cand2))
12737 {
12738 winner = more_constrained (cand1->fn, cand2->fn);
12739 if (winner)
12740 return winner;
12741 }
12742
12743 /* F2 is a rewritten candidate (12.4.1.2) and F1 is not, or F1 and F2 are
12744 rewritten candidates, and F2 is a synthesized candidate with reversed
12745 order of parameters and F1 is not. */
12746 if (cand1->rewritten ())
12747 {
12748 if (!cand2->rewritten ())
12749 return -1;
12750 if (!cand1->reversed () && cand2->reversed ())
12751 return 1;
12752 if (cand1->reversed () && !cand2->reversed ())
12753 return -1;
12754 }
12755 else if (cand2->rewritten ())
12756 return 1;
12757
12758 /* F1 is generated from a deduction-guide (13.3.1.8) and F2 is not */
12759 if (deduction_guide_p (cand1->fn))
12760 {
12761 gcc_assert (deduction_guide_p (cand2->fn));
12762 /* We distinguish between candidates from an explicit deduction guide and
12763 candidates built from a constructor based on DECL_ARTIFICIAL. */
12764 int art1 = DECL_ARTIFICIAL (cand1->fn);
12765 int art2 = DECL_ARTIFICIAL (cand2->fn);
12766 if (art1 != art2)
12767 return art2 - art1;
12768
12769 if (art1)
12770 {
12771 /* Prefer the special copy guide over a declared copy/move
12772 constructor. */
12773 if (copy_guide_p (cand1->fn))
12774 return 1;
12775 if (copy_guide_p (cand2->fn))
12776 return -1;
12777
12778 /* Prefer a candidate generated from a non-template constructor. */
12779 int tg1 = template_guide_p (cand1->fn);
12780 int tg2 = template_guide_p (cand2->fn);
12781 if (tg1 != tg2)
12782 return tg2 - tg1;
12783 }
12784 }
12785
12786 /* F1 is a member of a class D, F2 is a member of a base class B of D, and
12787 for all arguments the corresponding parameters of F1 and F2 have the same
12788 type (CWG 2273/2277). */
12789 if (DECL_P (cand1->fn) && DECL_CLASS_SCOPE_P (cand1->fn)
12790 && !DECL_CONV_FN_P (cand1->fn)
12791 && DECL_P (cand2->fn) && DECL_CLASS_SCOPE_P (cand2->fn)
12792 && !DECL_CONV_FN_P (cand2->fn))
12793 {
12794 tree base1 = DECL_CONTEXT (strip_inheriting_ctors (cand1->fn));
12795 tree base2 = DECL_CONTEXT (strip_inheriting_ctors (cand2->fn));
12796
12797 bool used1 = false;
12798 bool used2 = false;
12799 if (base1 == base2)
12800 /* No difference. */;
12801 else if (DERIVED_FROM_P (base1, base2))
12802 used1 = true;
12803 else if (DERIVED_FROM_P (base2, base1))
12804 used2 = true;
12805
12806 if (int diff = used2 - used1)
12807 {
12808 for (i = 0; i < len; ++i)
12809 {
12810 conversion *t1 = cand1->convs[i + off1];
12811 conversion *t2 = cand2->convs[i + off2];
12812 if (!same_type_p (t1->type, t2->type))
12813 break;
12814 }
12815 if (i == len)
12816 return diff;
12817 }
12818 }
12819
12820 /* Check whether we can discard a builtin candidate, either because we
12821 have two identical ones or matching builtin and non-builtin candidates.
12822
12823 (Pedantically in the latter case the builtin which matched the user
12824 function should not be added to the overload set, but we spot it here.
12825
12826 [over.match.oper]
12827 ... the builtin candidates include ...
12828 - do not have the same parameter type list as any non-template
12829 non-member candidate. */
12830
12831 if (identifier_p (cand1->fn) || identifier_p (cand2->fn))
12832 {
12833 for (i = 0; i < len; ++i)
12834 if (!same_type_p (cand1->convs[i]->type,
12835 cand2->convs[i]->type))
12836 break;
12837 if (i == cand1->num_convs)
12838 {
12839 if (cand1->fn == cand2->fn)
12840 /* Two built-in candidates; arbitrarily pick one. */
12841 return 1;
12842 else if (identifier_p (cand1->fn))
12843 /* cand1 is built-in; prefer cand2. */
12844 return -1;
12845 else
12846 /* cand2 is built-in; prefer cand1. */
12847 return 1;
12848 }
12849 }
12850
12851 /* For candidates of a multi-versioned function, make the version with
12852 the highest priority win. This version will be checked for dispatching
12853 first. If this version can be inlined into the caller, the front-end
12854 will simply make a direct call to this function. */
12855
12856 if (TREE_CODE (cand1->fn) == FUNCTION_DECL
12857 && DECL_FUNCTION_VERSIONED (cand1->fn)
12858 && TREE_CODE (cand2->fn) == FUNCTION_DECL
12859 && DECL_FUNCTION_VERSIONED (cand2->fn))
12860 {
12861 tree f1 = TREE_TYPE (cand1->fn);
12862 tree f2 = TREE_TYPE (cand2->fn);
12863 tree p1 = TYPE_ARG_TYPES (f1);
12864 tree p2 = TYPE_ARG_TYPES (f2);
12865
12866 /* Check if cand1->fn and cand2->fn are versions of the same function. It
12867 is possible that cand1->fn and cand2->fn are function versions but of
12868 different functions. Check types to see if they are versions of the same
12869 function. */
12870 if (compparms (p1, p2)
12871 && same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
12872 {
12873 /* Always make the version with the higher priority, more
12874 specialized, win. */
12875 gcc_assert (targetm.compare_version_priority);
12876 if (targetm.compare_version_priority (cand1->fn, cand2->fn) >= 0)
12877 return 1;
12878 else
12879 return -1;
12880 }
12881 }
12882
12883 /* If the two function declarations represent the same function (this can
12884 happen with declarations in multiple scopes and arg-dependent lookup),
12885 arbitrarily choose one. But first make sure the default args we're
12886 using match. */
12887 if (DECL_P (cand1->fn) && DECL_P (cand2->fn)
12888 && equal_functions (cand1->fn, cand2->fn))
12889 {
12890 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (cand1->fn));
12891 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (cand2->fn));
12892
12893 gcc_assert (!DECL_CONSTRUCTOR_P (cand1->fn));
12894
12895 for (i = 0; i < len; ++i)
12896 {
12897 /* Don't crash if the fn is variadic. */
12898 if (!parms1)
12899 break;
12900 parms1 = TREE_CHAIN (parms1);
12901 parms2 = TREE_CHAIN (parms2);
12902 }
12903
12904 if (off1)
12905 parms1 = TREE_CHAIN (parms1);
12906 else if (off2)
12907 parms2 = TREE_CHAIN (parms2);
12908
12909 for (; parms1; ++i)
12910 {
12911 if (!cp_tree_equal (TREE_PURPOSE (parms1),
12912 TREE_PURPOSE (parms2)))
12913 {
12914 if (warn)
12915 {
12916 if (complain & tf_error)
12917 {
12918 auto_diagnostic_group d;
12919 if (permerror (input_location,
12920 "default argument mismatch in "
12921 "overload resolution"))
12922 {
12923 inform (DECL_SOURCE_LOCATION (cand1->fn),
12924 " candidate 1: %q#F", cand1->fn);
12925 inform (DECL_SOURCE_LOCATION (cand2->fn),
12926 " candidate 2: %q#F", cand2->fn);
12927 }
12928 }
12929 else
12930 return 0;
12931 }
12932 else
12933 add_warning (cand1, cand2);
12934 break;
12935 }
12936 parms1 = TREE_CHAIN (parms1);
12937 parms2 = TREE_CHAIN (parms2);
12938 }
12939
12940 return 1;
12941 }
12942
12943 tweak:
12944
12945 /* Extension: If the worst conversion for one candidate is better than the
12946 worst conversion for the other, take the first. */
12947 if (!pedantic && (complain & tf_warning_or_error))
12948 {
12949 conversion_rank rank1 = cr_identity, rank2 = cr_identity;
12950 struct z_candidate *w = 0, *l = 0;
12951
12952 for (i = 0; i < len; ++i)
12953 {
12954 if (CONVERSION_RANK (cand1->convs[i+off1]) > rank1)
12955 rank1 = CONVERSION_RANK (cand1->convs[i+off1]);
12956 if (CONVERSION_RANK (cand2->convs[i + off2]) > rank2)
12957 rank2 = CONVERSION_RANK (cand2->convs[i + off2]);
12958 }
12959 if (rank1 < rank2)
12960 winner = 1, w = cand1, l = cand2;
12961 if (rank1 > rank2)
12962 winner = -1, w = cand2, l = cand1;
12963 if (winner)
12964 {
12965 /* Don't choose a deleted function over ambiguity. */
12966 if (DECL_P (w->fn) && DECL_DELETED_FN (w->fn))
12967 return 0;
12968 if (warn)
12969 {
12970 auto_diagnostic_group d;
12971 if (pedwarn (input_location, 0,
12972 "ISO C++ says that these are ambiguous, even "
12973 "though the worst conversion for the first is "
12974 "better than the worst conversion for the second:"))
12975 {
12976 print_z_candidate (input_location, N_("candidate 1:"), w);
12977 print_z_candidate (input_location, N_("candidate 2:"), l);
12978 }
12979 }
12980 else
12981 add_warning (w, l);
12982 return winner;
12983 }
12984 }
12985
12986 gcc_assert (!winner);
12987 return 0;
12988 }
12989
12990 /* Given a list of candidates for overloading, find the best one, if any.
12991 This algorithm has a worst case of O(2n) (winner is last), and a best
12992 case of O(n/2) (totally ambiguous); much better than a sorting
12993 algorithm. */
12994
12995 static struct z_candidate *
12996 tourney (struct z_candidate *candidates, tsubst_flags_t complain)
12997 {
12998 struct z_candidate *champ = candidates, *challenger;
12999 int fate;
13000 struct z_candidate *champ_compared_to_predecessor = nullptr;
13001
13002 /* Walk through the list once, comparing each current champ to the next
13003 candidate, knocking out a candidate or two with each comparison. */
13004
13005 for (challenger = champ->next; challenger; )
13006 {
13007 fate = joust (champ, challenger, 0, complain);
13008 if (fate == 1)
13009 challenger = challenger->next;
13010 else
13011 {
13012 if (fate == 0)
13013 {
13014 champ = challenger->next;
13015 if (champ == 0)
13016 return NULL;
13017 champ_compared_to_predecessor = nullptr;
13018 }
13019 else
13020 {
13021 champ_compared_to_predecessor = champ;
13022 champ = challenger;
13023 }
13024
13025 challenger = champ->next;
13026 }
13027 }
13028
13029 /* Make sure the champ is better than all the candidates it hasn't yet
13030 been compared to. */
13031
13032 for (challenger = candidates;
13033 challenger != champ
13034 && challenger != champ_compared_to_predecessor;
13035 challenger = challenger->next)
13036 {
13037 fate = joust (champ, challenger, 0, complain);
13038 if (fate != 1)
13039 return NULL;
13040 }
13041
13042 return champ;
13043 }
13044
13045 /* Returns nonzero if things of type FROM can be converted to TO. */
13046
13047 bool
13048 can_convert (tree to, tree from, tsubst_flags_t complain)
13049 {
13050 tree arg = NULL_TREE;
13051 /* implicit_conversion only considers user-defined conversions
13052 if it has an expression for the call argument list. */
13053 if (CLASS_TYPE_P (from) || CLASS_TYPE_P (to))
13054 arg = build_stub_object (from);
13055 return can_convert_arg (to, from, arg, LOOKUP_IMPLICIT, complain);
13056 }
13057
13058 /* Returns nonzero if things of type FROM can be converted to TO with a
13059 standard conversion. */
13060
13061 bool
13062 can_convert_standard (tree to, tree from, tsubst_flags_t complain)
13063 {
13064 return can_convert_arg (to, from, NULL_TREE, LOOKUP_IMPLICIT, complain);
13065 }
13066
13067 /* Returns nonzero if ARG (of type FROM) can be converted to TO. */
13068
13069 bool
13070 can_convert_arg (tree to, tree from, tree arg, int flags,
13071 tsubst_flags_t complain)
13072 {
13073 conversion *t;
13074 void *p;
13075 bool ok_p;
13076
13077 /* Get the high-water mark for the CONVERSION_OBSTACK. */
13078 p = conversion_obstack_alloc (0);
13079 /* We want to discard any access checks done for this test,
13080 as we might not be in the appropriate access context and
13081 we'll do the check again when we actually perform the
13082 conversion. */
13083 push_deferring_access_checks (dk_deferred);
13084
13085 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
13086 flags, complain);
13087 ok_p = (t && !t->bad_p);
13088
13089 /* Discard the access checks now. */
13090 pop_deferring_access_checks ();
13091 /* Free all the conversions we allocated. */
13092 obstack_free (&conversion_obstack, p);
13093
13094 return ok_p;
13095 }
13096
13097 /* Like can_convert_arg, but allows dubious conversions as well. */
13098
13099 bool
13100 can_convert_arg_bad (tree to, tree from, tree arg, int flags,
13101 tsubst_flags_t complain)
13102 {
13103 conversion *t;
13104 void *p;
13105
13106 /* Get the high-water mark for the CONVERSION_OBSTACK. */
13107 p = conversion_obstack_alloc (0);
13108 /* Try to perform the conversion. */
13109 t = implicit_conversion (to, from, arg, /*c_cast_p=*/false,
13110 flags, complain);
13111 /* Free all the conversions we allocated. */
13112 obstack_free (&conversion_obstack, p);
13113
13114 return t != NULL;
13115 }
13116
13117 /* Return an IMPLICIT_CONV_EXPR from EXPR to TYPE with bits set from overload
13118 resolution FLAGS. */
13119
13120 tree
13121 build_implicit_conv_flags (tree type, tree expr, int flags)
13122 {
13123 /* In a template, we are only concerned about determining the
13124 type of non-dependent expressions, so we do not have to
13125 perform the actual conversion. But for initializers, we
13126 need to be able to perform it at instantiation
13127 (or instantiate_non_dependent_expr) time. */
13128 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
13129 if (!(flags & LOOKUP_ONLYCONVERTING))
13130 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
13131 if (flags & LOOKUP_NO_NARROWING)
13132 IMPLICIT_CONV_EXPR_BRACED_INIT (expr) = true;
13133 return expr;
13134 }
13135
13136 /* Convert EXPR to TYPE. Return the converted expression.
13137
13138 Note that we allow bad conversions here because by the time we get to
13139 this point we are committed to doing the conversion. If we end up
13140 doing a bad conversion, convert_like will complain. */
13141
13142 tree
13143 perform_implicit_conversion_flags (tree type, tree expr,
13144 tsubst_flags_t complain, int flags)
13145 {
13146 conversion *conv;
13147 void *p;
13148 location_t loc = cp_expr_loc_or_input_loc (expr);
13149
13150 if (TYPE_REF_P (type))
13151 expr = mark_lvalue_use (expr);
13152 else
13153 expr = mark_rvalue_use (expr);
13154
13155 if (error_operand_p (expr))
13156 return error_mark_node;
13157
13158 /* Get the high-water mark for the CONVERSION_OBSTACK. */
13159 p = conversion_obstack_alloc (0);
13160
13161 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
13162 /*c_cast_p=*/false,
13163 flags, complain);
13164
13165 if (!conv)
13166 {
13167 if (complain & tf_error)
13168 implicit_conversion_error (loc, type, expr);
13169 expr = error_mark_node;
13170 }
13171 else if (processing_template_decl && conv->kind != ck_identity)
13172 expr = build_implicit_conv_flags (type, expr, flags);
13173 else
13174 {
13175 /* Give a conversion call the same location as expr. */
13176 iloc_sentinel il (loc);
13177 expr = convert_like (conv, expr, complain);
13178 }
13179
13180 /* Free all the conversions we allocated. */
13181 obstack_free (&conversion_obstack, p);
13182
13183 return expr;
13184 }
13185
13186 tree
13187 perform_implicit_conversion (tree type, tree expr, tsubst_flags_t complain)
13188 {
13189 return perform_implicit_conversion_flags (type, expr, complain,
13190 LOOKUP_IMPLICIT);
13191 }
13192
13193 /* Convert EXPR to TYPE (as a direct-initialization) if that is
13194 permitted. If the conversion is valid, the converted expression is
13195 returned. Otherwise, NULL_TREE is returned, except in the case
13196 that TYPE is a class type; in that case, an error is issued. If
13197 C_CAST_P is true, then this direct-initialization is taking
13198 place as part of a static_cast being attempted as part of a C-style
13199 cast. */
13200
13201 tree
13202 perform_direct_initialization_if_possible (tree type,
13203 tree expr,
13204 bool c_cast_p,
13205 tsubst_flags_t complain)
13206 {
13207 conversion *conv;
13208 void *p;
13209
13210 if (type == error_mark_node || error_operand_p (expr))
13211 return error_mark_node;
13212 /* [dcl.init]
13213
13214 If the destination type is a (possibly cv-qualified) class type:
13215
13216 -- If the initialization is direct-initialization ...,
13217 constructors are considered.
13218
13219 -- If overload resolution is successful, the selected constructor
13220 is called to initialize the object, with the initializer expression
13221 or expression-list as its argument(s).
13222
13223 -- Otherwise, if no constructor is viable, the destination type is
13224 a (possibly cv-qualified) aggregate class A, and the initializer is
13225 a parenthesized expression-list, the object is initialized as
13226 follows... */
13227 if (CLASS_TYPE_P (type))
13228 {
13229 releasing_vec args (make_tree_vector_single (expr));
13230 expr = build_special_member_call (NULL_TREE, complete_ctor_identifier,
13231 &args, type, LOOKUP_NORMAL, complain);
13232 return build_cplus_new (type, expr, complain);
13233 }
13234
13235 /* Get the high-water mark for the CONVERSION_OBSTACK. */
13236 p = conversion_obstack_alloc (0);
13237
13238 conv = implicit_conversion (type, TREE_TYPE (expr), expr,
13239 c_cast_p,
13240 LOOKUP_NORMAL, complain);
13241 if (!conv || conv->bad_p)
13242 expr = NULL_TREE;
13243 else if (processing_template_decl && conv->kind != ck_identity)
13244 {
13245 /* In a template, we are only concerned about determining the
13246 type of non-dependent expressions, so we do not have to
13247 perform the actual conversion. But for initializers, we
13248 need to be able to perform it at instantiation
13249 (or instantiate_non_dependent_expr) time. */
13250 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
13251 IMPLICIT_CONV_EXPR_DIRECT_INIT (expr) = true;
13252 }
13253 else
13254 expr = convert_like (conv, expr, NULL_TREE, 0,
13255 /*issue_conversion_warnings=*/false,
13256 c_cast_p, /*nested_p=*/false, complain);
13257
13258 /* Free all the conversions we allocated. */
13259 obstack_free (&conversion_obstack, p);
13260
13261 return expr;
13262 }
13263
13264 /* When initializing a reference that lasts longer than a full-expression,
13265 this special rule applies:
13266
13267 [class.temporary]
13268
13269 The temporary to which the reference is bound or the temporary
13270 that is the complete object to which the reference is bound
13271 persists for the lifetime of the reference.
13272
13273 The temporaries created during the evaluation of the expression
13274 initializing the reference, except the temporary to which the
13275 reference is bound, are destroyed at the end of the
13276 full-expression in which they are created.
13277
13278 In that case, we store the converted expression into a new
13279 VAR_DECL in a new scope.
13280
13281 However, we want to be careful not to create temporaries when
13282 they are not required. For example, given:
13283
13284 struct B {};
13285 struct D : public B {};
13286 D f();
13287 const B& b = f();
13288
13289 there is no need to copy the return value from "f"; we can just
13290 extend its lifetime. Similarly, given:
13291
13292 struct S {};
13293 struct T { operator S(); };
13294 T t;
13295 const S& s = t;
13296
13297 we can extend the lifetime of the return value of the conversion
13298 operator.
13299
13300 The next several functions are involved in this lifetime extension. */
13301
13302 /* DECL is a VAR_DECL or FIELD_DECL whose type is a REFERENCE_TYPE. The
13303 reference is being bound to a temporary. Create and return a new
13304 VAR_DECL with the indicated TYPE; this variable will store the value to
13305 which the reference is bound. */
13306
13307 tree
13308 make_temporary_var_for_ref_to_temp (tree decl, tree type)
13309 {
13310 tree var = create_temporary_var (type);
13311
13312 /* Register the variable. */
13313 if (VAR_P (decl)
13314 && (TREE_STATIC (decl) || CP_DECL_THREAD_LOCAL_P (decl)))
13315 {
13316 /* Namespace-scope or local static; give it a mangled name. */
13317
13318 /* If an initializer is visible to multiple translation units, those
13319 translation units must agree on the addresses of the
13320 temporaries. Therefore the temporaries must be given a consistent name
13321 and vague linkage. The mangled name of a temporary is the name of the
13322 non-temporary object in whose initializer they appear, prefixed with
13323 GR and suffixed with a sequence number mangled using the usual rules
13324 for a seq-id. Temporaries are numbered with a pre-order, depth-first,
13325 left-to-right walk of the complete initializer. */
13326 copy_linkage (var, decl);
13327
13328 tree name = mangle_ref_init_variable (decl);
13329 DECL_NAME (var) = name;
13330 SET_DECL_ASSEMBLER_NAME (var, name);
13331 }
13332 else
13333 /* Create a new cleanup level if necessary. */
13334 maybe_push_cleanup_level (type);
13335
13336 return pushdecl (var);
13337 }
13338
13339 /* EXPR is the initializer for a variable DECL of reference or
13340 std::initializer_list type. Create, push and return a new VAR_DECL
13341 for the initializer so that it will live as long as DECL. Any
13342 cleanup for the new variable is returned through CLEANUP, and the
13343 code to initialize the new variable is returned through INITP. */
13344
13345 static tree
13346 set_up_extended_ref_temp (tree decl, tree expr, vec<tree, va_gc> **cleanups,
13347 tree *initp, tree *cond_guard)
13348 {
13349 tree init;
13350 tree type;
13351 tree var;
13352
13353 /* Create the temporary variable. */
13354 type = TREE_TYPE (expr);
13355 var = make_temporary_var_for_ref_to_temp (decl, type);
13356 layout_decl (var, 0);
13357 /* If the rvalue is the result of a function call it will be
13358 a TARGET_EXPR. If it is some other construct (such as a
13359 member access expression where the underlying object is
13360 itself the result of a function call), turn it into a
13361 TARGET_EXPR here. It is important that EXPR be a
13362 TARGET_EXPR below since otherwise the INIT_EXPR will
13363 attempt to make a bitwise copy of EXPR to initialize
13364 VAR. */
13365 if (TREE_CODE (expr) != TARGET_EXPR)
13366 expr = get_target_expr (expr);
13367 else if (TREE_ADDRESSABLE (expr))
13368 TREE_ADDRESSABLE (var) = 1;
13369
13370 if (TREE_CODE (decl) == FIELD_DECL
13371 && extra_warnings && !warning_suppressed_p (decl))
13372 {
13373 warning (OPT_Wextra, "a temporary bound to %qD only persists "
13374 "until the constructor exits", decl);
13375 suppress_warning (decl);
13376 }
13377
13378 /* Recursively extend temps in this initializer. */
13379 TARGET_EXPR_INITIAL (expr)
13380 = extend_ref_init_temps (decl, TARGET_EXPR_INITIAL (expr), cleanups,
13381 cond_guard);
13382
13383 /* Any reference temp has a non-trivial initializer. */
13384 DECL_NONTRIVIALLY_INITIALIZED_P (var) = true;
13385
13386 /* If the initializer is constant, put it in DECL_INITIAL so we get
13387 static initialization and use in constant expressions. */
13388 init = maybe_constant_init (expr);
13389 /* As in store_init_value. */
13390 init = cp_fully_fold (init);
13391 if (TREE_CONSTANT (init))
13392 {
13393 if (literal_type_p (type) && CP_TYPE_CONST_NON_VOLATILE_P (type))
13394 {
13395 /* 5.19 says that a constant expression can include an
13396 lvalue-rvalue conversion applied to "a glvalue of literal type
13397 that refers to a non-volatile temporary object initialized
13398 with a constant expression". Rather than try to communicate
13399 that this VAR_DECL is a temporary, just mark it constexpr. */
13400 DECL_DECLARED_CONSTEXPR_P (var) = true;
13401 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var) = true;
13402 TREE_CONSTANT (var) = true;
13403 TREE_READONLY (var) = true;
13404 }
13405 DECL_INITIAL (var) = init;
13406 init = NULL_TREE;
13407 }
13408 else
13409 /* Create the INIT_EXPR that will initialize the temporary
13410 variable. */
13411 init = split_nonconstant_init (var, expr);
13412 if (at_function_scope_p ())
13413 {
13414 add_decl_expr (var);
13415
13416 if (TREE_STATIC (var))
13417 init = add_stmt_to_compound (init, register_dtor_fn (var));
13418 else
13419 {
13420 tree cleanup = cxx_maybe_build_cleanup (var, tf_warning_or_error);
13421 if (cleanup)
13422 {
13423 if (cond_guard && cleanup != error_mark_node)
13424 {
13425 if (*cond_guard == NULL_TREE)
13426 {
13427 *cond_guard = build_local_temp (boolean_type_node);
13428 add_decl_expr (*cond_guard);
13429 tree set = cp_build_modify_expr (UNKNOWN_LOCATION,
13430 *cond_guard, NOP_EXPR,
13431 boolean_false_node,
13432 tf_warning_or_error);
13433 finish_expr_stmt (set);
13434 }
13435 cleanup = build3 (COND_EXPR, void_type_node,
13436 *cond_guard, cleanup, NULL_TREE);
13437 }
13438 vec_safe_push (*cleanups, cleanup);
13439 }
13440 }
13441
13442 /* We must be careful to destroy the temporary only
13443 after its initialization has taken place. If the
13444 initialization throws an exception, then the
13445 destructor should not be run. We cannot simply
13446 transform INIT into something like:
13447
13448 (INIT, ({ CLEANUP_STMT; }))
13449
13450 because emit_local_var always treats the
13451 initializer as a full-expression. Thus, the
13452 destructor would run too early; it would run at the
13453 end of initializing the reference variable, rather
13454 than at the end of the block enclosing the
13455 reference variable.
13456
13457 The solution is to pass back a cleanup expression
13458 which the caller is responsible for attaching to
13459 the statement tree. */
13460 }
13461 else
13462 {
13463 rest_of_decl_compilation (var, /*toplev=*/1, at_eof);
13464 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
13465 {
13466 if (CP_DECL_THREAD_LOCAL_P (var))
13467 tls_aggregates = tree_cons (NULL_TREE, var,
13468 tls_aggregates);
13469 else
13470 static_aggregates = tree_cons (NULL_TREE, var,
13471 static_aggregates);
13472 }
13473 else
13474 /* Check whether the dtor is callable. */
13475 cxx_maybe_build_cleanup (var, tf_warning_or_error);
13476 }
13477 /* Avoid -Wunused-variable warning (c++/38958). */
13478 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
13479 && VAR_P (decl))
13480 TREE_USED (decl) = DECL_READ_P (decl) = true;
13481
13482 *initp = init;
13483 return var;
13484 }
13485
13486 /* Convert EXPR to the indicated reference TYPE, in a way suitable for
13487 initializing a variable of that TYPE. */
13488
13489 tree
13490 initialize_reference (tree type, tree expr,
13491 int flags, tsubst_flags_t complain)
13492 {
13493 conversion *conv;
13494 void *p;
13495 location_t loc = cp_expr_loc_or_input_loc (expr);
13496
13497 if (type == error_mark_node || error_operand_p (expr))
13498 return error_mark_node;
13499
13500 /* Get the high-water mark for the CONVERSION_OBSTACK. */
13501 p = conversion_obstack_alloc (0);
13502
13503 conv = reference_binding (type, TREE_TYPE (expr), expr, /*c_cast_p=*/false,
13504 flags, complain);
13505 /* If this conversion failed, we're in C++20, and we have something like
13506 A& a(b) where A is an aggregate, try again, this time as A& a{b}. */
13507 if ((!conv || conv->bad_p)
13508 && (flags & LOOKUP_AGGREGATE_PAREN_INIT))
13509 {
13510 tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
13511 CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
13512 CONSTRUCTOR_IS_PAREN_INIT (e) = true;
13513 conversion *c = reference_binding (type, TREE_TYPE (e), e,
13514 /*c_cast_p=*/false, flags, complain);
13515 /* If this worked, use it. */
13516 if (c && !c->bad_p)
13517 expr = e, conv = c;
13518 }
13519 if (!conv || conv->bad_p)
13520 {
13521 if (complain & tf_error)
13522 {
13523 if (conv)
13524 convert_like (conv, expr, complain);
13525 else if (!CP_TYPE_CONST_P (TREE_TYPE (type))
13526 && !TYPE_REF_IS_RVALUE (type)
13527 && !lvalue_p (expr))
13528 error_at (loc, "invalid initialization of non-const reference of "
13529 "type %qH from an rvalue of type %qI",
13530 type, TREE_TYPE (expr));
13531 else
13532 error_at (loc, "invalid initialization of reference of type "
13533 "%qH from expression of type %qI", type,
13534 TREE_TYPE (expr));
13535 }
13536 return error_mark_node;
13537 }
13538
13539 if (conv->kind == ck_ref_bind)
13540 /* Perform the conversion. */
13541 expr = convert_like (conv, expr, complain);
13542 else if (conv->kind == ck_ambig)
13543 /* We gave an error in build_user_type_conversion_1. */
13544 expr = error_mark_node;
13545 else
13546 gcc_unreachable ();
13547
13548 /* Free all the conversions we allocated. */
13549 obstack_free (&conversion_obstack, p);
13550
13551 return expr;
13552 }
13553
13554 /* Return true if T is std::pair<const T&, const T&>. */
13555
13556 static bool
13557 std_pair_ref_ref_p (tree t)
13558 {
13559 /* First, check if we have std::pair. */
13560 if (!NON_UNION_CLASS_TYPE_P (t)
13561 || !CLASSTYPE_TEMPLATE_INSTANTIATION (t))
13562 return false;
13563 tree tdecl = TYPE_NAME (TYPE_MAIN_VARIANT (t));
13564 if (!decl_in_std_namespace_p (tdecl))
13565 return false;
13566 tree name = DECL_NAME (tdecl);
13567 if (!name || !id_equal (name, "pair"))
13568 return false;
13569
13570 /* Now see if the template arguments are both const T&. */
13571 tree args = CLASSTYPE_TI_ARGS (t);
13572 if (TREE_VEC_LENGTH (args) != 2)
13573 return false;
13574 for (int i = 0; i < 2; i++)
13575 if (!TYPE_REF_OBJ_P (TREE_VEC_ELT (args, i))
13576 || !CP_TYPE_CONST_P (TREE_TYPE (TREE_VEC_ELT (args, i))))
13577 return false;
13578
13579 return true;
13580 }
13581
13582 /* Helper for maybe_warn_dangling_reference to find a problematic CALL_EXPR
13583 that initializes the LHS (and at least one of its arguments represents
13584 a temporary, as outlined in maybe_warn_dangling_reference), or NULL_TREE
13585 if none found. For instance:
13586
13587 const S& s = S().self(); // S::self (&TARGET_EXPR <...>)
13588 const int& r = (42, f(1)); // f(1)
13589 const int& t = b ? f(1) : f(2); // f(1)
13590 const int& u = b ? f(1) : f(g); // f(1)
13591 const int& v = b ? f(g) : f(2); // f(2)
13592 const int& w = b ? f(g) : f(g); // NULL_TREE
13593 const int& y = (f(1), 42); // NULL_TREE
13594 const int& z = f(f(1)); // f(f(1))
13595
13596 EXPR is the initializer. */
13597
13598 static tree
13599 do_warn_dangling_reference (tree expr)
13600 {
13601 STRIP_NOPS (expr);
13602 switch (TREE_CODE (expr))
13603 {
13604 case CALL_EXPR:
13605 {
13606 tree fndecl = cp_get_callee_fndecl_nofold (expr);
13607 if (!fndecl
13608 || warning_suppressed_p (fndecl, OPT_Wdangling_reference)
13609 || !warning_enabled_at (DECL_SOURCE_LOCATION (fndecl),
13610 OPT_Wdangling_reference)
13611 /* Don't emit a false positive for:
13612 std::vector<int> v = ...;
13613 std::vector<int>::const_iterator it = v.begin();
13614 const int &r = *it++;
13615 because R refers to one of the int elements of V, not to
13616 a temporary object. Member operator* may return a reference
13617 but probably not to one of its arguments. */
13618 || (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
13619 && DECL_OVERLOADED_OPERATOR_P (fndecl)
13620 && DECL_OVERLOADED_OPERATOR_IS (fndecl, INDIRECT_REF)))
13621 return NULL_TREE;
13622
13623 tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
13624 /* If the function doesn't return a reference, don't warn. This
13625 can be e.g.
13626 const int& z = std::min({1, 2, 3, 4, 5, 6, 7});
13627 which doesn't dangle: std::min here returns an int.
13628
13629 If the function returns a std::pair<const T&, const T&>, we
13630 warn, to detect e.g.
13631 std::pair<const int&, const int&> v = std::minmax(1, 2);
13632 which also creates a dangling reference, because std::minmax
13633 returns std::pair<const T&, const T&>(b, a). */
13634 if (!(TYPE_REF_OBJ_P (rettype) || std_pair_ref_ref_p (rettype)))
13635 return NULL_TREE;
13636
13637 /* Here we're looking to see if any of the arguments is a temporary
13638 initializing a reference parameter. */
13639 for (int i = 0; i < call_expr_nargs (expr); ++i)
13640 {
13641 tree arg = CALL_EXPR_ARG (expr, i);
13642 /* Check that this argument initializes a reference, except for
13643 the argument initializing the object of a member function. */
13644 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
13645 && !TYPE_REF_P (TREE_TYPE (arg)))
13646 continue;
13647 /* It could also be another call taking a temporary and returning
13648 it and initializing this reference parameter. */
13649 if (do_warn_dangling_reference (arg))
13650 return expr;
13651 STRIP_NOPS (arg);
13652 if (TREE_CODE (arg) == ADDR_EXPR)
13653 arg = TREE_OPERAND (arg, 0);
13654 if (expr_represents_temporary_p (arg))
13655 return expr;
13656 /* Don't warn about member function like:
13657 std::any a(...);
13658 S& s = a.emplace<S>({0}, 0);
13659 which constructs a new object and returns a reference to it, but
13660 we still want to detect:
13661 struct S { const S& self () { return *this; } };
13662 const S& s = S().self();
13663 where 's' dangles. If we've gotten here, the object this function
13664 is invoked on is not a temporary. */
13665 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl))
13666 break;
13667 }
13668 return NULL_TREE;
13669 }
13670 case COMPOUND_EXPR:
13671 return do_warn_dangling_reference (TREE_OPERAND (expr, 1));
13672 case COND_EXPR:
13673 if (tree t = do_warn_dangling_reference (TREE_OPERAND (expr, 1)))
13674 return t;
13675 return do_warn_dangling_reference (TREE_OPERAND (expr, 2));
13676 case PAREN_EXPR:
13677 return do_warn_dangling_reference (TREE_OPERAND (expr, 0));
13678 case TARGET_EXPR:
13679 return do_warn_dangling_reference (TARGET_EXPR_INITIAL (expr));
13680 default:
13681 return NULL_TREE;
13682 }
13683 }
13684
13685 /* Implement -Wdangling-reference, to detect cases like
13686
13687 int n = 1;
13688 const int& r = std::max(n - 1, n + 1); // r is dangling
13689
13690 This creates temporaries from the arguments, returns a reference to
13691 one of the temporaries, but both temporaries are destroyed at the end
13692 of the full expression.
13693
13694 This works by checking if a reference is initialized with a function
13695 that returns a reference, and at least one parameter of the function
13696 is a reference that is bound to a temporary. It assumes that such a
13697 function actually returns one of its arguments.
13698
13699 DECL is the reference being initialized, INIT is the initializer. */
13700
13701 static void
13702 maybe_warn_dangling_reference (const_tree decl, tree init)
13703 {
13704 if (!warn_dangling_reference)
13705 return;
13706 tree type = TREE_TYPE (decl);
13707 /* Only warn if what we're initializing has type T&& or const T&, or
13708 std::pair<const T&, const T&>. (A non-const lvalue reference can't
13709 bind to a temporary.) */
13710 if (!((TYPE_REF_OBJ_P (type)
13711 && (TYPE_REF_IS_RVALUE (type)
13712 || CP_TYPE_CONST_P (TREE_TYPE (type))))
13713 || std_pair_ref_ref_p (type)))
13714 return;
13715 /* Don't suppress the diagnostic just because the call comes from
13716 a system header. If the DECL is not in a system header, or if
13717 -Wsystem-headers was provided, warn. */
13718 auto wsh
13719 = make_temp_override (global_dc->dc_warn_system_headers,
13720 (!in_system_header_at (DECL_SOURCE_LOCATION (decl))
13721 || global_dc->dc_warn_system_headers));
13722 if (tree call = do_warn_dangling_reference (init))
13723 {
13724 auto_diagnostic_group d;
13725 if (warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wdangling_reference,
13726 "possibly dangling reference to a temporary"))
13727 inform (EXPR_LOCATION (call), "the temporary was destroyed at "
13728 "the end of the full expression %qE", call);
13729 }
13730 }
13731
13732 /* If *P is an xvalue expression, prevent temporary lifetime extension if it
13733 gets used to initialize a reference. */
13734
13735 static tree
13736 prevent_lifetime_extension (tree t)
13737 {
13738 tree *p = &t;
13739 while (TREE_CODE (*p) == COMPOUND_EXPR)
13740 p = &TREE_OPERAND (*p, 1);
13741 while (handled_component_p (*p))
13742 p = &TREE_OPERAND (*p, 0);
13743 /* Change a TARGET_EXPR from prvalue to xvalue. */
13744 if (TREE_CODE (*p) == TARGET_EXPR)
13745 *p = build2 (COMPOUND_EXPR, TREE_TYPE (*p), *p,
13746 move (TARGET_EXPR_SLOT (*p)));
13747 return t;
13748 }
13749
13750 /* Subroutine of extend_ref_init_temps. Possibly extend one initializer,
13751 which is bound either to a reference or a std::initializer_list. */
13752
13753 static tree
13754 extend_ref_init_temps_1 (tree decl, tree init, vec<tree, va_gc> **cleanups,
13755 tree *cond_guard)
13756 {
13757 tree sub = init;
13758 tree *p;
13759 STRIP_NOPS (sub);
13760 if (TREE_CODE (sub) == COMPOUND_EXPR)
13761 {
13762 TREE_OPERAND (sub, 1)
13763 = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
13764 cond_guard);
13765 return init;
13766 }
13767 if (TREE_CODE (sub) == COND_EXPR)
13768 {
13769 tree cur_cond_guard = NULL_TREE;
13770 if (TREE_OPERAND (sub, 1))
13771 TREE_OPERAND (sub, 1)
13772 = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 1), cleanups,
13773 &cur_cond_guard);
13774 if (cur_cond_guard)
13775 {
13776 tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
13777 NOP_EXPR, boolean_true_node,
13778 tf_warning_or_error);
13779 TREE_OPERAND (sub, 1)
13780 = cp_build_compound_expr (set, TREE_OPERAND (sub, 1),
13781 tf_warning_or_error);
13782 }
13783 cur_cond_guard = NULL_TREE;
13784 if (TREE_OPERAND (sub, 2))
13785 TREE_OPERAND (sub, 2)
13786 = extend_ref_init_temps_1 (decl, TREE_OPERAND (sub, 2), cleanups,
13787 &cur_cond_guard);
13788 if (cur_cond_guard)
13789 {
13790 tree set = cp_build_modify_expr (UNKNOWN_LOCATION, cur_cond_guard,
13791 NOP_EXPR, boolean_true_node,
13792 tf_warning_or_error);
13793 TREE_OPERAND (sub, 2)
13794 = cp_build_compound_expr (set, TREE_OPERAND (sub, 2),
13795 tf_warning_or_error);
13796 }
13797 return init;
13798 }
13799 if (TREE_CODE (sub) != ADDR_EXPR)
13800 return init;
13801 /* Deal with binding to a subobject. */
13802 for (p = &TREE_OPERAND (sub, 0);
13803 TREE_CODE (*p) == COMPONENT_REF || TREE_CODE (*p) == ARRAY_REF; )
13804 p = &TREE_OPERAND (*p, 0);
13805 if (TREE_CODE (*p) == TARGET_EXPR)
13806 {
13807 tree subinit = NULL_TREE;
13808 *p = set_up_extended_ref_temp (decl, *p, cleanups, &subinit, cond_guard);
13809 recompute_tree_invariant_for_addr_expr (sub);
13810 if (init != sub)
13811 init = fold_convert (TREE_TYPE (init), sub);
13812 if (subinit)
13813 init = build2 (COMPOUND_EXPR, TREE_TYPE (init), subinit, init);
13814 }
13815 return init;
13816 }
13817
13818 /* INIT is part of the initializer for DECL. If there are any
13819 reference or initializer lists being initialized, extend their
13820 lifetime to match that of DECL. */
13821
13822 tree
13823 extend_ref_init_temps (tree decl, tree init, vec<tree, va_gc> **cleanups,
13824 tree *cond_guard)
13825 {
13826 tree type = TREE_TYPE (init);
13827 if (processing_template_decl)
13828 return init;
13829
13830 maybe_warn_dangling_reference (decl, init);
13831
13832 if (TYPE_REF_P (type))
13833 init = extend_ref_init_temps_1 (decl, init, cleanups, cond_guard);
13834 else
13835 {
13836 tree ctor = init;
13837 if (TREE_CODE (ctor) == TARGET_EXPR)
13838 ctor = TARGET_EXPR_INITIAL (ctor);
13839 if (TREE_CODE (ctor) == CONSTRUCTOR)
13840 {
13841 /* [dcl.init] When initializing an aggregate from a parenthesized list
13842 of values... a temporary object bound to a reference does not have
13843 its lifetime extended. */
13844 if (CONSTRUCTOR_IS_PAREN_INIT (ctor))
13845 return init;
13846
13847 if (is_std_init_list (type))
13848 {
13849 /* The temporary array underlying a std::initializer_list
13850 is handled like a reference temporary. */
13851 tree array = CONSTRUCTOR_ELT (ctor, 0)->value;
13852 array = extend_ref_init_temps_1 (decl, array, cleanups,
13853 cond_guard);
13854 CONSTRUCTOR_ELT (ctor, 0)->value = array;
13855 }
13856 else
13857 {
13858 unsigned i;
13859 constructor_elt *p;
13860 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
13861 FOR_EACH_VEC_SAFE_ELT (elts, i, p)
13862 p->value = extend_ref_init_temps (decl, p->value, cleanups,
13863 cond_guard);
13864 }
13865 recompute_constructor_flags (ctor);
13866 if (decl_maybe_constant_var_p (decl) && TREE_CONSTANT (ctor))
13867 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
13868 }
13869 }
13870
13871 return init;
13872 }
13873
13874 /* Returns true iff an initializer for TYPE could contain temporaries that
13875 need to be extended because they are bound to references or
13876 std::initializer_list. */
13877
13878 bool
13879 type_has_extended_temps (tree type)
13880 {
13881 type = strip_array_types (type);
13882 if (TYPE_REF_P (type))
13883 return true;
13884 if (CLASS_TYPE_P (type))
13885 {
13886 if (is_std_init_list (type))
13887 return true;
13888 for (tree f = next_aggregate_field (TYPE_FIELDS (type));
13889 f; f = next_aggregate_field (DECL_CHAIN (f)))
13890 if (type_has_extended_temps (TREE_TYPE (f)))
13891 return true;
13892 }
13893 return false;
13894 }
13895
13896 /* Returns true iff TYPE is some variant of std::initializer_list. */
13897
13898 bool
13899 is_std_init_list (tree type)
13900 {
13901 if (!TYPE_P (type))
13902 return false;
13903 if (cxx_dialect == cxx98)
13904 return false;
13905 /* Look through typedefs. */
13906 type = TYPE_MAIN_VARIANT (type);
13907 return (CLASS_TYPE_P (type)
13908 && CP_TYPE_CONTEXT (type) == std_node
13909 && init_list_identifier == DECL_NAME (TYPE_NAME (type)));
13910 }
13911
13912 /* Returns true iff DECL is a list constructor: i.e. a constructor which
13913 will accept an argument list of a single std::initializer_list<T>. */
13914
13915 bool
13916 is_list_ctor (tree decl)
13917 {
13918 tree args = FUNCTION_FIRST_USER_PARMTYPE (decl);
13919 tree arg;
13920
13921 if (!args || args == void_list_node)
13922 return false;
13923
13924 arg = non_reference (TREE_VALUE (args));
13925 if (!is_std_init_list (arg))
13926 return false;
13927
13928 args = TREE_CHAIN (args);
13929
13930 if (args && args != void_list_node && !TREE_PURPOSE (args))
13931 /* There are more non-defaulted parms. */
13932 return false;
13933
13934 return true;
13935 }
13936
13937 #include "gt-cp-call.h"
This page took 0.64944 seconds and 4 git commands to generate.